src/share/vm/oops/constantPoolOop.cpp

Wed, 25 Jan 2012 17:40:51 -0500

author
jiangli
date
Wed, 25 Jan 2012 17:40:51 -0500
changeset 3526
a79cb7c55012
parent 3156
f08d439fab8c
child 3489
f457154eee8b
permissions
-rw-r--r--

7132690: InstanceKlass:_reference_type should be u1 type
Summary: Change InstanceKlass::_reference_type to u1 type.
Reviewed-by: dholmes, coleenp, acorn
Contributed-by: Jiangli Zhou <jiangli.zhou@oracle.com>

     1 /*
     2  * Copyright (c) 1997, 2011, Oracle and/or its affiliates. All rights reserved.
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     4  *
     5  * This code is free software; you can redistribute it and/or modify it
     6  * under the terms of the GNU General Public License version 2 only, as
     7  * published by the Free Software Foundation.
     8  *
     9  * This code is distributed in the hope that it will be useful, but WITHOUT
    10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
    11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
    12  * version 2 for more details (a copy is included in the LICENSE file that
    13  * accompanied this code).
    14  *
    15  * You should have received a copy of the GNU General Public License version
    16  * 2 along with this work; if not, write to the Free Software Foundation,
    17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
    18  *
    19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
    20  * or visit www.oracle.com if you need additional information or have any
    21  * questions.
    22  *
    23  */
    25 #include "precompiled.hpp"
    26 #include "classfile/javaClasses.hpp"
    27 #include "classfile/symbolTable.hpp"
    28 #include "classfile/systemDictionary.hpp"
    29 #include "classfile/vmSymbols.hpp"
    30 #include "interpreter/linkResolver.hpp"
    31 #include "memory/oopFactory.hpp"
    32 #include "memory/universe.inline.hpp"
    33 #include "oops/constantPoolOop.hpp"
    34 #include "oops/instanceKlass.hpp"
    35 #include "oops/objArrayKlass.hpp"
    36 #include "oops/oop.inline.hpp"
    37 #include "runtime/fieldType.hpp"
    38 #include "runtime/init.hpp"
    39 #include "runtime/signature.hpp"
    40 #include "runtime/vframe.hpp"
    42 void constantPoolOopDesc::set_flag_at(FlagBit fb) {
    43   const int MAX_STATE_CHANGES = 2;
    44   for (int i = MAX_STATE_CHANGES + 10; i > 0; i--) {
    45     int oflags = _flags;
    46     int nflags = oflags | (1 << (int)fb);
    47     if (Atomic::cmpxchg(nflags, &_flags, oflags) == oflags)
    48       return;
    49   }
    50   assert(false, "failed to cmpxchg flags");
    51   _flags |= (1 << (int)fb);     // better than nothing
    52 }
    54 klassOop constantPoolOopDesc::klass_at_impl(constantPoolHandle this_oop, int which, TRAPS) {
    55   // A resolved constantPool entry will contain a klassOop, otherwise a Symbol*.
    56   // It is not safe to rely on the tag bit's here, since we don't have a lock, and the entry and
    57   // tag is not updated atomicly.
    58   CPSlot entry = this_oop->slot_at(which);
    59   if (entry.is_oop()) {
    60     assert(entry.get_oop()->is_klass(), "must be");
    61     // Already resolved - return entry.
    62     return (klassOop)entry.get_oop();
    63   }
    65   // Acquire lock on constant oop while doing update. After we get the lock, we check if another object
    66   // already has updated the object
    67   assert(THREAD->is_Java_thread(), "must be a Java thread");
    68   bool do_resolve = false;
    69   bool in_error = false;
    71   Symbol* name = NULL;
    72   Handle       loader;
    73   { ObjectLocker ol(this_oop, THREAD);
    75     if (this_oop->tag_at(which).is_unresolved_klass()) {
    76       if (this_oop->tag_at(which).is_unresolved_klass_in_error()) {
    77         in_error = true;
    78       } else {
    79         do_resolve = true;
    80         name   = this_oop->unresolved_klass_at(which);
    81         loader = Handle(THREAD, instanceKlass::cast(this_oop->pool_holder())->class_loader());
    82       }
    83     }
    84   } // unlocking constantPool
    87   // The original attempt to resolve this constant pool entry failed so find the
    88   // original error and throw it again (JVMS 5.4.3).
    89   if (in_error) {
    90     Symbol* error = SystemDictionary::find_resolution_error(this_oop, which);
    91     guarantee(error != (Symbol*)NULL, "tag mismatch with resolution error table");
    92     ResourceMark rm;
    93     // exception text will be the class name
    94     const char* className = this_oop->unresolved_klass_at(which)->as_C_string();
    95     THROW_MSG_0(error, className);
    96   }
    98   if (do_resolve) {
    99     // this_oop must be unlocked during resolve_or_fail
   100     oop protection_domain = Klass::cast(this_oop->pool_holder())->protection_domain();
   101     Handle h_prot (THREAD, protection_domain);
   102     klassOop k_oop = SystemDictionary::resolve_or_fail(name, loader, h_prot, true, THREAD);
   103     KlassHandle k;
   104     if (!HAS_PENDING_EXCEPTION) {
   105       k = KlassHandle(THREAD, k_oop);
   106       // Do access check for klasses
   107       verify_constant_pool_resolve(this_oop, k, THREAD);
   108     }
   110     // Failed to resolve class. We must record the errors so that subsequent attempts
   111     // to resolve this constant pool entry fail with the same error (JVMS 5.4.3).
   112     if (HAS_PENDING_EXCEPTION) {
   113       ResourceMark rm;
   114       Symbol* error = PENDING_EXCEPTION->klass()->klass_part()->name();
   116       bool throw_orig_error = false;
   117       {
   118         ObjectLocker ol (this_oop, THREAD);
   120         // some other thread has beaten us and has resolved the class.
   121         if (this_oop->tag_at(which).is_klass()) {
   122           CLEAR_PENDING_EXCEPTION;
   123           entry = this_oop->resolved_klass_at(which);
   124           return (klassOop)entry.get_oop();
   125         }
   127         if (!PENDING_EXCEPTION->
   128               is_a(SystemDictionary::LinkageError_klass())) {
   129           // Just throw the exception and don't prevent these classes from
   130           // being loaded due to virtual machine errors like StackOverflow
   131           // and OutOfMemoryError, etc, or if the thread was hit by stop()
   132           // Needs clarification to section 5.4.3 of the VM spec (see 6308271)
   133         }
   134         else if (!this_oop->tag_at(which).is_unresolved_klass_in_error()) {
   135           SystemDictionary::add_resolution_error(this_oop, which, error);
   136           this_oop->tag_at_put(which, JVM_CONSTANT_UnresolvedClassInError);
   137         } else {
   138           // some other thread has put the class in error state.
   139           error = SystemDictionary::find_resolution_error(this_oop, which);
   140           assert(error != NULL, "checking");
   141           throw_orig_error = true;
   142         }
   143       } // unlocked
   145       if (throw_orig_error) {
   146         CLEAR_PENDING_EXCEPTION;
   147         ResourceMark rm;
   148         const char* className = this_oop->unresolved_klass_at(which)->as_C_string();
   149         THROW_MSG_0(error, className);
   150       }
   152       return 0;
   153     }
   155     if (TraceClassResolution && !k()->klass_part()->oop_is_array()) {
   156       // skip resolving the constant pool so that this code get's
   157       // called the next time some bytecodes refer to this class.
   158       ResourceMark rm;
   159       int line_number = -1;
   160       const char * source_file = NULL;
   161       if (JavaThread::current()->has_last_Java_frame()) {
   162         // try to identify the method which called this function.
   163         vframeStream vfst(JavaThread::current());
   164         if (!vfst.at_end()) {
   165           line_number = vfst.method()->line_number_from_bci(vfst.bci());
   166           Symbol* s = instanceKlass::cast(vfst.method()->method_holder())->source_file_name();
   167           if (s != NULL) {
   168             source_file = s->as_C_string();
   169           }
   170         }
   171       }
   172       if (k() != this_oop->pool_holder()) {
   173         // only print something if the classes are different
   174         if (source_file != NULL) {
   175           tty->print("RESOLVE %s %s %s:%d\n",
   176                      instanceKlass::cast(this_oop->pool_holder())->external_name(),
   177                      instanceKlass::cast(k())->external_name(), source_file, line_number);
   178         } else {
   179           tty->print("RESOLVE %s %s\n",
   180                      instanceKlass::cast(this_oop->pool_holder())->external_name(),
   181                      instanceKlass::cast(k())->external_name());
   182         }
   183       }
   184       return k();
   185     } else {
   186       ObjectLocker ol (this_oop, THREAD);
   187       // Only updated constant pool - if it is resolved.
   188       do_resolve = this_oop->tag_at(which).is_unresolved_klass();
   189       if (do_resolve) {
   190         this_oop->klass_at_put(which, k());
   191       }
   192     }
   193   }
   195   entry = this_oop->resolved_klass_at(which);
   196   assert(entry.is_oop() && entry.get_oop()->is_klass(), "must be resolved at this point");
   197   return (klassOop)entry.get_oop();
   198 }
   201 // Does not update constantPoolOop - to avoid any exception throwing. Used
   202 // by compiler and exception handling.  Also used to avoid classloads for
   203 // instanceof operations. Returns NULL if the class has not been loaded or
   204 // if the verification of constant pool failed
   205 klassOop constantPoolOopDesc::klass_at_if_loaded(constantPoolHandle this_oop, int which) {
   206   CPSlot entry = this_oop->slot_at(which);
   207   if (entry.is_oop()) {
   208     assert(entry.get_oop()->is_klass(), "must be");
   209     return (klassOop)entry.get_oop();
   210   } else {
   211     assert(entry.is_metadata(), "must be either symbol or klass");
   212     Thread *thread = Thread::current();
   213     Symbol* name = entry.get_symbol();
   214     oop loader = instanceKlass::cast(this_oop->pool_holder())->class_loader();
   215     oop protection_domain = Klass::cast(this_oop->pool_holder())->protection_domain();
   216     Handle h_prot (thread, protection_domain);
   217     Handle h_loader (thread, loader);
   218     klassOop k = SystemDictionary::find(name, h_loader, h_prot, thread);
   220     if (k != NULL) {
   221       // Make sure that resolving is legal
   222       EXCEPTION_MARK;
   223       KlassHandle klass(THREAD, k);
   224       // return NULL if verification fails
   225       verify_constant_pool_resolve(this_oop, klass, THREAD);
   226       if (HAS_PENDING_EXCEPTION) {
   227         CLEAR_PENDING_EXCEPTION;
   228         return NULL;
   229       }
   230       return klass();
   231     } else {
   232       return k;
   233     }
   234   }
   235 }
   238 klassOop constantPoolOopDesc::klass_ref_at_if_loaded(constantPoolHandle this_oop, int which) {
   239   return klass_at_if_loaded(this_oop, this_oop->klass_ref_index_at(which));
   240 }
   243 // This is an interface for the compiler that allows accessing non-resolved entries
   244 // in the constant pool - but still performs the validations tests. Must be used
   245 // in a pre-parse of the compiler - to determine what it can do and not do.
   246 // Note: We cannot update the ConstantPool from the vm_thread.
   247 klassOop constantPoolOopDesc::klass_ref_at_if_loaded_check(constantPoolHandle this_oop, int index, TRAPS) {
   248   int which = this_oop->klass_ref_index_at(index);
   249   CPSlot entry = this_oop->slot_at(which);
   250   if (entry.is_oop()) {
   251     assert(entry.get_oop()->is_klass(), "must be");
   252     return (klassOop)entry.get_oop();
   253   } else {
   254     assert(entry.is_metadata(), "must be either symbol or klass");
   255     Symbol*  name  = entry.get_symbol();
   256     oop loader = instanceKlass::cast(this_oop->pool_holder())->class_loader();
   257     oop protection_domain = Klass::cast(this_oop->pool_holder())->protection_domain();
   258     Handle h_loader(THREAD, loader);
   259     Handle h_prot  (THREAD, protection_domain);
   260     KlassHandle k(THREAD, SystemDictionary::find(name, h_loader, h_prot, THREAD));
   262     // Do access check for klasses
   263     if( k.not_null() ) verify_constant_pool_resolve(this_oop, k, CHECK_NULL);
   264     return k();
   265   }
   266 }
   269 methodOop constantPoolOopDesc::method_at_if_loaded(constantPoolHandle cpool,
   270                                                    int which, Bytecodes::Code invoke_code) {
   271   assert(!constantPoolCacheOopDesc::is_secondary_index(which), "no indy instruction here");
   272   if (cpool->cache() == NULL)  return false;  // nothing to load yet
   273   int cache_index = which - CPCACHE_INDEX_TAG;
   274   if (!(cache_index >= 0 && cache_index < cpool->cache()->length())) {
   275     if (PrintMiscellaneous && (Verbose||WizardMode)) {
   276       tty->print_cr("bad operand %d for %d in:", which, invoke_code); cpool->print();
   277     }
   278     return NULL;
   279   }
   280   ConstantPoolCacheEntry* e = cpool->cache()->entry_at(cache_index);
   281   if (invoke_code != Bytecodes::_illegal)
   282     return e->get_method_if_resolved(invoke_code, cpool);
   283   Bytecodes::Code bc;
   284   if ((bc = e->bytecode_1()) != (Bytecodes::Code)0)
   285     return e->get_method_if_resolved(bc, cpool);
   286   if ((bc = e->bytecode_2()) != (Bytecodes::Code)0)
   287     return e->get_method_if_resolved(bc, cpool);
   288   return NULL;
   289 }
   292 Symbol* constantPoolOopDesc::impl_name_ref_at(int which, bool uncached) {
   293   int name_index = name_ref_index_at(impl_name_and_type_ref_index_at(which, uncached));
   294   return symbol_at(name_index);
   295 }
   298 Symbol* constantPoolOopDesc::impl_signature_ref_at(int which, bool uncached) {
   299   int signature_index = signature_ref_index_at(impl_name_and_type_ref_index_at(which, uncached));
   300   return symbol_at(signature_index);
   301 }
   304 int constantPoolOopDesc::impl_name_and_type_ref_index_at(int which, bool uncached) {
   305   int i = which;
   306   if (!uncached && cache() != NULL) {
   307     if (constantPoolCacheOopDesc::is_secondary_index(which)) {
   308       // Invokedynamic index.
   309       int pool_index = cache()->main_entry_at(which)->constant_pool_index();
   310       pool_index = invoke_dynamic_name_and_type_ref_index_at(pool_index);
   311       assert(tag_at(pool_index).is_name_and_type(), "");
   312       return pool_index;
   313     }
   314     // change byte-ordering and go via cache
   315     i = remap_instruction_operand_from_cache(which);
   316   } else {
   317     if (tag_at(which).is_invoke_dynamic()) {
   318       int pool_index = invoke_dynamic_name_and_type_ref_index_at(which);
   319       assert(tag_at(pool_index).is_name_and_type(), "");
   320       return pool_index;
   321     }
   322   }
   323   assert(tag_at(i).is_field_or_method(), "Corrupted constant pool");
   324   assert(!tag_at(i).is_invoke_dynamic(), "Must be handled above");
   325   jint ref_index = *int_at_addr(i);
   326   return extract_high_short_from_int(ref_index);
   327 }
   330 int constantPoolOopDesc::impl_klass_ref_index_at(int which, bool uncached) {
   331   guarantee(!constantPoolCacheOopDesc::is_secondary_index(which),
   332             "an invokedynamic instruction does not have a klass");
   333   int i = which;
   334   if (!uncached && cache() != NULL) {
   335     // change byte-ordering and go via cache
   336     i = remap_instruction_operand_from_cache(which);
   337   }
   338   assert(tag_at(i).is_field_or_method(), "Corrupted constant pool");
   339   jint ref_index = *int_at_addr(i);
   340   return extract_low_short_from_int(ref_index);
   341 }
   345 int constantPoolOopDesc::remap_instruction_operand_from_cache(int operand) {
   346   int cpc_index = operand;
   347   DEBUG_ONLY(cpc_index -= CPCACHE_INDEX_TAG);
   348   assert((int)(u2)cpc_index == cpc_index, "clean u2");
   349   int member_index = cache()->entry_at(cpc_index)->constant_pool_index();
   350   return member_index;
   351 }
   354 void constantPoolOopDesc::verify_constant_pool_resolve(constantPoolHandle this_oop, KlassHandle k, TRAPS) {
   355  if (k->oop_is_instance() || k->oop_is_objArray()) {
   356     instanceKlassHandle holder (THREAD, this_oop->pool_holder());
   357     klassOop elem_oop = k->oop_is_instance() ? k() : objArrayKlass::cast(k())->bottom_klass();
   358     KlassHandle element (THREAD, elem_oop);
   360     // The element type could be a typeArray - we only need the access check if it is
   361     // an reference to another class
   362     if (element->oop_is_instance()) {
   363       LinkResolver::check_klass_accessability(holder, element, CHECK);
   364     }
   365   }
   366 }
   369 int constantPoolOopDesc::name_ref_index_at(int which_nt) {
   370   jint ref_index = name_and_type_at(which_nt);
   371   return extract_low_short_from_int(ref_index);
   372 }
   375 int constantPoolOopDesc::signature_ref_index_at(int which_nt) {
   376   jint ref_index = name_and_type_at(which_nt);
   377   return extract_high_short_from_int(ref_index);
   378 }
   381 klassOop constantPoolOopDesc::klass_ref_at(int which, TRAPS) {
   382   return klass_at(klass_ref_index_at(which), CHECK_NULL);
   383 }
   386 Symbol* constantPoolOopDesc::klass_name_at(int which) {
   387   assert(tag_at(which).is_unresolved_klass() || tag_at(which).is_klass(),
   388          "Corrupted constant pool");
   389   // A resolved constantPool entry will contain a klassOop, otherwise a Symbol*.
   390   // It is not safe to rely on the tag bit's here, since we don't have a lock, and the entry and
   391   // tag is not updated atomicly.
   392   CPSlot entry = slot_at(which);
   393   if (entry.is_oop()) {
   394     // Already resolved - return entry's name.
   395     assert(entry.get_oop()->is_klass(), "must be");
   396     return klassOop(entry.get_oop())->klass_part()->name();
   397   } else {
   398     assert(entry.is_metadata(), "must be either symbol or klass");
   399     return entry.get_symbol();
   400   }
   401 }
   403 Symbol* constantPoolOopDesc::klass_ref_at_noresolve(int which) {
   404   jint ref_index = klass_ref_index_at(which);
   405   return klass_at_noresolve(ref_index);
   406 }
   408 Symbol* constantPoolOopDesc::uncached_klass_ref_at_noresolve(int which) {
   409   jint ref_index = uncached_klass_ref_index_at(which);
   410   return klass_at_noresolve(ref_index);
   411 }
   413 char* constantPoolOopDesc::string_at_noresolve(int which) {
   414   // Test entry type in case string is resolved while in here.
   415   CPSlot entry = slot_at(which);
   416   if (entry.is_metadata()) {
   417     return (entry.get_symbol())->as_C_string();
   418   } else if (java_lang_String::is_instance(entry.get_oop())) {
   419     return java_lang_String::as_utf8_string(entry.get_oop());
   420   } else {
   421     return (char*)"<pseudo-string>";
   422   }
   423 }
   426 BasicType constantPoolOopDesc::basic_type_for_signature_at(int which) {
   427   return FieldType::basic_type(symbol_at(which));
   428 }
   431 void constantPoolOopDesc::resolve_string_constants_impl(constantPoolHandle this_oop, TRAPS) {
   432   for (int index = 1; index < this_oop->length(); index++) { // Index 0 is unused
   433     if (this_oop->tag_at(index).is_unresolved_string()) {
   434       this_oop->string_at(index, CHECK);
   435     }
   436   }
   437 }
   439 // A resolved constant value in the CP cache is represented as a non-null
   440 // value.  As a special case, this value can be a 'systemObjArray'
   441 // which masks an exception object to throw.
   442 // This allows a MethodHandle constant reference to throw a consistent
   443 // exception every time, if it fails to resolve.
   444 static oop decode_exception_from_f1(oop result_oop, TRAPS) {
   445   if (result_oop->klass() != Universe::systemObjArrayKlassObj())
   446     return result_oop;
   448   // Special cases here:  Masked null, saved exception.
   449   objArrayOop sys_array = (objArrayOop) result_oop;
   450   assert(sys_array->length() == 1, "bad system array");
   451   if (sys_array->length() == 1) {
   452     THROW_OOP_(sys_array->obj_at(0), NULL);
   453   }
   454   return NULL;
   455 }
   457 oop constantPoolOopDesc::resolve_constant_at_impl(constantPoolHandle this_oop, int index, int cache_index, TRAPS) {
   458   oop result_oop = NULL;
   459   Handle throw_exception;
   461   if (cache_index == _possible_index_sentinel) {
   462     // It is possible that this constant is one which is cached in the CP cache.
   463     // We'll do a linear search.  This should be OK because this usage is rare.
   464     assert(index > 0, "valid index");
   465     constantPoolCacheOop cache = this_oop()->cache();
   466     for (int i = 0, len = cache->length(); i < len; i++) {
   467       ConstantPoolCacheEntry* cpc_entry = cache->entry_at(i);
   468       if (!cpc_entry->is_secondary_entry() && cpc_entry->constant_pool_index() == index) {
   469         // Switch the query to use this CPC entry.
   470         cache_index = i;
   471         index = _no_index_sentinel;
   472         break;
   473       }
   474     }
   475     if (cache_index == _possible_index_sentinel)
   476       cache_index = _no_index_sentinel;  // not found
   477   }
   478   assert(cache_index == _no_index_sentinel || cache_index >= 0, "");
   479   assert(index == _no_index_sentinel || index >= 0, "");
   481   if (cache_index >= 0) {
   482     assert(index == _no_index_sentinel, "only one kind of index at a time");
   483     ConstantPoolCacheEntry* cpc_entry = this_oop->cache()->entry_at(cache_index);
   484     result_oop = cpc_entry->f1();
   485     if (result_oop != NULL) {
   486       return decode_exception_from_f1(result_oop, THREAD);
   487       // That was easy...
   488     }
   489     index = cpc_entry->constant_pool_index();
   490   }
   492   jvalue prim_value;  // temp used only in a few cases below
   494   int tag_value = this_oop->tag_at(index).value();
   495   switch (tag_value) {
   497   case JVM_CONSTANT_UnresolvedClass:
   498   case JVM_CONSTANT_UnresolvedClassInError:
   499   case JVM_CONSTANT_Class:
   500     {
   501       klassOop resolved = klass_at_impl(this_oop, index, CHECK_NULL);
   502       // ldc wants the java mirror.
   503       result_oop = resolved->java_mirror();
   504       break;
   505     }
   507   case JVM_CONSTANT_String:
   508   case JVM_CONSTANT_UnresolvedString:
   509     if (this_oop->is_pseudo_string_at(index)) {
   510       result_oop = this_oop->pseudo_string_at(index);
   511       break;
   512     }
   513     result_oop = string_at_impl(this_oop, index, CHECK_NULL);
   514     break;
   516   case JVM_CONSTANT_Object:
   517     result_oop = this_oop->object_at(index);
   518     break;
   520   case JVM_CONSTANT_MethodHandle:
   521     {
   522       int ref_kind                 = this_oop->method_handle_ref_kind_at(index);
   523       int callee_index             = this_oop->method_handle_klass_index_at(index);
   524       Symbol*  name =      this_oop->method_handle_name_ref_at(index);
   525       Symbol*  signature = this_oop->method_handle_signature_ref_at(index);
   526       if (PrintMiscellaneous)
   527         tty->print_cr("resolve JVM_CONSTANT_MethodHandle:%d [%d/%d/%d] %s.%s",
   528                       ref_kind, index, this_oop->method_handle_index_at(index),
   529                       callee_index, name->as_C_string(), signature->as_C_string());
   530       KlassHandle callee;
   531       { klassOop k = klass_at_impl(this_oop, callee_index, CHECK_NULL);
   532         callee = KlassHandle(THREAD, k);
   533       }
   534       KlassHandle klass(THREAD, this_oop->pool_holder());
   535       Handle value = SystemDictionary::link_method_handle_constant(klass, ref_kind,
   536                                                                    callee, name, signature,
   537                                                                    THREAD);
   538       if (HAS_PENDING_EXCEPTION) {
   539         throw_exception = Handle(THREAD, PENDING_EXCEPTION);
   540         CLEAR_PENDING_EXCEPTION;
   541         break;
   542       }
   543       result_oop = value();
   544       assert(result_oop != NULL, "");
   545       break;
   546     }
   548   case JVM_CONSTANT_MethodType:
   549     {
   550       Symbol*  signature = this_oop->method_type_signature_at(index);
   551       if (PrintMiscellaneous)
   552         tty->print_cr("resolve JVM_CONSTANT_MethodType [%d/%d] %s",
   553                       index, this_oop->method_type_index_at(index),
   554                       signature->as_C_string());
   555       KlassHandle klass(THREAD, this_oop->pool_holder());
   556       bool ignore_is_on_bcp = false;
   557       Handle value = SystemDictionary::find_method_handle_type(signature,
   558                                                                klass,
   559                                                                false,
   560                                                                ignore_is_on_bcp,
   561                                                                THREAD);
   562       if (HAS_PENDING_EXCEPTION) {
   563         throw_exception = Handle(THREAD, PENDING_EXCEPTION);
   564         CLEAR_PENDING_EXCEPTION;
   565         break;
   566       }
   567       result_oop = value();
   568       assert(result_oop != NULL, "");
   569       break;
   570     }
   572   case JVM_CONSTANT_Integer:
   573     prim_value.i = this_oop->int_at(index);
   574     result_oop = java_lang_boxing_object::create(T_INT, &prim_value, CHECK_NULL);
   575     break;
   577   case JVM_CONSTANT_Float:
   578     prim_value.f = this_oop->float_at(index);
   579     result_oop = java_lang_boxing_object::create(T_FLOAT, &prim_value, CHECK_NULL);
   580     break;
   582   case JVM_CONSTANT_Long:
   583     prim_value.j = this_oop->long_at(index);
   584     result_oop = java_lang_boxing_object::create(T_LONG, &prim_value, CHECK_NULL);
   585     break;
   587   case JVM_CONSTANT_Double:
   588     prim_value.d = this_oop->double_at(index);
   589     result_oop = java_lang_boxing_object::create(T_DOUBLE, &prim_value, CHECK_NULL);
   590     break;
   592   default:
   593     DEBUG_ONLY( tty->print_cr("*** %p: tag at CP[%d/%d] = %d",
   594                               this_oop(), index, cache_index, tag_value) );
   595     assert(false, "unexpected constant tag");
   596     break;
   597   }
   599   if (cache_index >= 0) {
   600     // Cache the oop here also.
   601     if (throw_exception.not_null()) {
   602       objArrayOop sys_array = oopFactory::new_system_objArray(1, CHECK_NULL);
   603       sys_array->obj_at_put(0, throw_exception());
   604       result_oop = sys_array;
   605       throw_exception = Handle();  // be tidy
   606     }
   607     Handle result_handle(THREAD, result_oop);
   608     result_oop = NULL;  // safety
   609     ObjectLocker ol(this_oop, THREAD);
   610     ConstantPoolCacheEntry* cpc_entry = this_oop->cache()->entry_at(cache_index);
   611     result_oop = cpc_entry->f1();
   612     // Benign race condition:  f1 may already be filled in while we were trying to lock.
   613     // The important thing here is that all threads pick up the same result.
   614     // It doesn't matter which racing thread wins, as long as only one
   615     // result is used by all threads, and all future queries.
   616     // That result may be either a resolved constant or a failure exception.
   617     if (result_oop == NULL) {
   618       result_oop = result_handle();
   619       cpc_entry->set_f1(result_oop);
   620     }
   621     return decode_exception_from_f1(result_oop, THREAD);
   622   } else {
   623     if (throw_exception.not_null()) {
   624       THROW_HANDLE_(throw_exception, NULL);
   625     }
   626     return result_oop;
   627   }
   628 }
   630 oop constantPoolOopDesc::string_at_impl(constantPoolHandle this_oop, int which, TRAPS) {
   631   oop str = NULL;
   632   CPSlot entry = this_oop->slot_at(which);
   633   if (entry.is_metadata()) {
   634     ObjectLocker ol(this_oop, THREAD);
   635     if (this_oop->tag_at(which).is_unresolved_string()) {
   636       // Intern string
   637       Symbol* sym = this_oop->unresolved_string_at(which);
   638       str = StringTable::intern(sym, CHECK_(constantPoolOop(NULL)));
   639       this_oop->string_at_put(which, str);
   640     } else {
   641       // Another thread beat us and interned string, read string from constant pool
   642       str = this_oop->resolved_string_at(which);
   643     }
   644   } else {
   645     str = entry.get_oop();
   646   }
   647   assert(java_lang_String::is_instance(str), "must be string");
   648   return str;
   649 }
   652 bool constantPoolOopDesc::is_pseudo_string_at(int which) {
   653   CPSlot entry = slot_at(which);
   654   if (entry.is_metadata())
   655     // Not yet resolved, but it will resolve to a string.
   656     return false;
   657   else if (java_lang_String::is_instance(entry.get_oop()))
   658     return false; // actually, it might be a non-interned or non-perm string
   659   else
   660     // truly pseudo
   661     return true;
   662 }
   665 bool constantPoolOopDesc::klass_name_at_matches(instanceKlassHandle k,
   666                                                 int which) {
   667   // Names are interned, so we can compare Symbol*s directly
   668   Symbol* cp_name = klass_name_at(which);
   669   return (cp_name == k->name());
   670 }
   673 int constantPoolOopDesc::pre_resolve_shared_klasses(TRAPS) {
   674   ResourceMark rm;
   675   int count = 0;
   676   for (int index = 1; index < tags()->length(); index++) { // Index 0 is unused
   677     if (tag_at(index).is_unresolved_string()) {
   678       // Intern string
   679       Symbol* sym = unresolved_string_at(index);
   680       oop entry = StringTable::intern(sym, CHECK_(-1));
   681       string_at_put(index, entry);
   682     }
   683   }
   684   return count;
   685 }
   687 // Iterate over symbols and decrement ones which are Symbol*s.
   688 // This is done during GC so do not need to lock constantPool unless we
   689 // have per-thread safepoints.
   690 // Only decrement the UTF8 symbols. Unresolved classes and strings point to
   691 // these symbols but didn't increment the reference count.
   692 void constantPoolOopDesc::unreference_symbols() {
   693   for (int index = 1; index < length(); index++) { // Index 0 is unused
   694     constantTag tag = tag_at(index);
   695     if (tag.is_symbol()) {
   696       symbol_at(index)->decrement_refcount();
   697     }
   698   }
   699 }
   701 // Iterate over symbols which are used as class, field, method names and
   702 // signatures (in preparation for writing to the shared archive).
   704 void constantPoolOopDesc::shared_symbols_iterate(SymbolClosure* closure) {
   705   for (int index = 1; index < length(); index++) { // Index 0 is unused
   706     switch (tag_at(index).value()) {
   708     case JVM_CONSTANT_UnresolvedClass:
   709     case JVM_CONSTANT_UnresolvedString:
   710     case JVM_CONSTANT_Utf8:
   711       assert(slot_at(index).is_metadata(), "must be symbol");
   712       closure->do_symbol(symbol_at_addr(index));
   713       break;
   715     case JVM_CONSTANT_NameAndType:
   716       {
   717         int i = *int_at_addr(index);
   718         closure->do_symbol(symbol_at_addr((unsigned)i >> 16));
   719         closure->do_symbol(symbol_at_addr((unsigned)i & 0xffff));
   720       }
   721       break;
   723     case JVM_CONSTANT_Class:
   724     case JVM_CONSTANT_InterfaceMethodref:
   725     case JVM_CONSTANT_Fieldref:
   726     case JVM_CONSTANT_Methodref:
   727     case JVM_CONSTANT_Integer:
   728     case JVM_CONSTANT_Float:
   729       // Do nothing!  Not an oop.
   730       // These constant types do not reference symbols at this point.
   731       break;
   733     case JVM_CONSTANT_String:
   734       // Do nothing!  Not a symbol.
   735       break;
   737     case JVM_CONSTANT_Long:
   738     case JVM_CONSTANT_Double:
   739       // Do nothing!  Not an oop. (But takes two pool entries.)
   740       ++index;
   741       break;
   743     default:
   744       ShouldNotReachHere();
   745       break;
   746     }
   747   }
   748 }
   751 // Iterate over the [one] tags array (in preparation for writing to the
   752 // shared archive).
   754 void constantPoolOopDesc::shared_tags_iterate(OopClosure* closure) {
   755   closure->do_oop(tags_addr());
   756   closure->do_oop(operands_addr());
   757 }
   760 // Iterate over String objects (in preparation for writing to the shared
   761 // archive).
   763 void constantPoolOopDesc::shared_strings_iterate(OopClosure* closure) {
   764   for (int index = 1; index < length(); index++) { // Index 0 is unused
   765     switch (tag_at(index).value()) {
   767     case JVM_CONSTANT_UnresolvedClass:
   768     case JVM_CONSTANT_NameAndType:
   769       // Do nothing!  Not a String.
   770       break;
   772     case JVM_CONSTANT_Class:
   773     case JVM_CONSTANT_InterfaceMethodref:
   774     case JVM_CONSTANT_Fieldref:
   775     case JVM_CONSTANT_Methodref:
   776     case JVM_CONSTANT_Integer:
   777     case JVM_CONSTANT_Float:
   778       // Do nothing!  Not an oop.
   779       // These constant types do not reference symbols at this point.
   780       break;
   782     case JVM_CONSTANT_String:
   783       closure->do_oop(obj_at_addr_raw(index));
   784       break;
   786     case JVM_CONSTANT_UnresolvedString:
   787     case JVM_CONSTANT_Utf8:
   788       // These constants are symbols, but unless these symbols are
   789       // actually to be used for something, we don't want to mark them.
   790       break;
   792     case JVM_CONSTANT_Long:
   793     case JVM_CONSTANT_Double:
   794       // Do nothing!  Not an oop. (But takes two pool entries.)
   795       ++index;
   796       break;
   798     default:
   799       ShouldNotReachHere();
   800       break;
   801     }
   802   }
   803 }
   806 // Compare this constant pool's entry at index1 to the constant pool
   807 // cp2's entry at index2.
   808 bool constantPoolOopDesc::compare_entry_to(int index1, constantPoolHandle cp2,
   809        int index2, TRAPS) {
   811   jbyte t1 = tag_at(index1).value();
   812   jbyte t2 = cp2->tag_at(index2).value();
   815   // JVM_CONSTANT_UnresolvedClassInError is equal to JVM_CONSTANT_UnresolvedClass
   816   // when comparing
   817   if (t1 == JVM_CONSTANT_UnresolvedClassInError) {
   818     t1 = JVM_CONSTANT_UnresolvedClass;
   819   }
   820   if (t2 == JVM_CONSTANT_UnresolvedClassInError) {
   821     t2 = JVM_CONSTANT_UnresolvedClass;
   822   }
   824   if (t1 != t2) {
   825     // Not the same entry type so there is nothing else to check. Note
   826     // that this style of checking will consider resolved/unresolved
   827     // class pairs and resolved/unresolved string pairs as different.
   828     // From the constantPoolOop API point of view, this is correct
   829     // behavior. See constantPoolKlass::merge() to see how this plays
   830     // out in the context of constantPoolOop merging.
   831     return false;
   832   }
   834   switch (t1) {
   835   case JVM_CONSTANT_Class:
   836   {
   837     klassOop k1 = klass_at(index1, CHECK_false);
   838     klassOop k2 = cp2->klass_at(index2, CHECK_false);
   839     if (k1 == k2) {
   840       return true;
   841     }
   842   } break;
   844   case JVM_CONSTANT_ClassIndex:
   845   {
   846     int recur1 = klass_index_at(index1);
   847     int recur2 = cp2->klass_index_at(index2);
   848     bool match = compare_entry_to(recur1, cp2, recur2, CHECK_false);
   849     if (match) {
   850       return true;
   851     }
   852   } break;
   854   case JVM_CONSTANT_Double:
   855   {
   856     jdouble d1 = double_at(index1);
   857     jdouble d2 = cp2->double_at(index2);
   858     if (d1 == d2) {
   859       return true;
   860     }
   861   } break;
   863   case JVM_CONSTANT_Fieldref:
   864   case JVM_CONSTANT_InterfaceMethodref:
   865   case JVM_CONSTANT_Methodref:
   866   {
   867     int recur1 = uncached_klass_ref_index_at(index1);
   868     int recur2 = cp2->uncached_klass_ref_index_at(index2);
   869     bool match = compare_entry_to(recur1, cp2, recur2, CHECK_false);
   870     if (match) {
   871       recur1 = uncached_name_and_type_ref_index_at(index1);
   872       recur2 = cp2->uncached_name_and_type_ref_index_at(index2);
   873       match = compare_entry_to(recur1, cp2, recur2, CHECK_false);
   874       if (match) {
   875         return true;
   876       }
   877     }
   878   } break;
   880   case JVM_CONSTANT_Float:
   881   {
   882     jfloat f1 = float_at(index1);
   883     jfloat f2 = cp2->float_at(index2);
   884     if (f1 == f2) {
   885       return true;
   886     }
   887   } break;
   889   case JVM_CONSTANT_Integer:
   890   {
   891     jint i1 = int_at(index1);
   892     jint i2 = cp2->int_at(index2);
   893     if (i1 == i2) {
   894       return true;
   895     }
   896   } break;
   898   case JVM_CONSTANT_Long:
   899   {
   900     jlong l1 = long_at(index1);
   901     jlong l2 = cp2->long_at(index2);
   902     if (l1 == l2) {
   903       return true;
   904     }
   905   } break;
   907   case JVM_CONSTANT_NameAndType:
   908   {
   909     int recur1 = name_ref_index_at(index1);
   910     int recur2 = cp2->name_ref_index_at(index2);
   911     bool match = compare_entry_to(recur1, cp2, recur2, CHECK_false);
   912     if (match) {
   913       recur1 = signature_ref_index_at(index1);
   914       recur2 = cp2->signature_ref_index_at(index2);
   915       match = compare_entry_to(recur1, cp2, recur2, CHECK_false);
   916       if (match) {
   917         return true;
   918       }
   919     }
   920   } break;
   922   case JVM_CONSTANT_String:
   923   {
   924     oop s1 = string_at(index1, CHECK_false);
   925     oop s2 = cp2->string_at(index2, CHECK_false);
   926     if (s1 == s2) {
   927       return true;
   928     }
   929   } break;
   931   case JVM_CONSTANT_StringIndex:
   932   {
   933     int recur1 = string_index_at(index1);
   934     int recur2 = cp2->string_index_at(index2);
   935     bool match = compare_entry_to(recur1, cp2, recur2, CHECK_false);
   936     if (match) {
   937       return true;
   938     }
   939   } break;
   941   case JVM_CONSTANT_UnresolvedClass:
   942   {
   943     Symbol* k1 = unresolved_klass_at(index1);
   944     Symbol* k2 = cp2->unresolved_klass_at(index2);
   945     if (k1 == k2) {
   946       return true;
   947     }
   948   } break;
   950   case JVM_CONSTANT_MethodType:
   951   {
   952     int k1 = method_type_index_at(index1);
   953     int k2 = cp2->method_type_index_at(index2);
   954     bool match = compare_entry_to(k1, cp2, k2, CHECK_false);
   955     if (match) {
   956       return true;
   957     }
   958   } break;
   960   case JVM_CONSTANT_MethodHandle:
   961   {
   962     int k1 = method_handle_ref_kind_at(index1);
   963     int k2 = cp2->method_handle_ref_kind_at(index2);
   964     if (k1 == k2) {
   965       int i1 = method_handle_index_at(index1);
   966       int i2 = cp2->method_handle_index_at(index2);
   967       bool match = compare_entry_to(i1, cp2, i2, CHECK_false);
   968       if (match) {
   969         return true;
   970       }
   971     }
   972   } break;
   974   case JVM_CONSTANT_InvokeDynamic:
   975   {
   976     int k1 = invoke_dynamic_bootstrap_method_ref_index_at(index1);
   977     int k2 = cp2->invoke_dynamic_bootstrap_method_ref_index_at(index2);
   978     bool match = compare_entry_to(k1, cp2, k2, CHECK_false);
   979     if (!match)  return false;
   980     k1 = invoke_dynamic_name_and_type_ref_index_at(index1);
   981     k2 = cp2->invoke_dynamic_name_and_type_ref_index_at(index2);
   982     match = compare_entry_to(k1, cp2, k2, CHECK_false);
   983     if (!match)  return false;
   984     int argc = invoke_dynamic_argument_count_at(index1);
   985     if (argc == cp2->invoke_dynamic_argument_count_at(index2)) {
   986       for (int j = 0; j < argc; j++) {
   987         k1 = invoke_dynamic_argument_index_at(index1, j);
   988         k2 = cp2->invoke_dynamic_argument_index_at(index2, j);
   989         match = compare_entry_to(k1, cp2, k2, CHECK_false);
   990         if (!match)  return false;
   991       }
   992       return true;           // got through loop; all elements equal
   993     }
   994   } break;
   996   case JVM_CONSTANT_UnresolvedString:
   997   {
   998     Symbol* s1 = unresolved_string_at(index1);
   999     Symbol* s2 = cp2->unresolved_string_at(index2);
  1000     if (s1 == s2) {
  1001       return true;
  1003   } break;
  1005   case JVM_CONSTANT_Utf8:
  1007     Symbol* s1 = symbol_at(index1);
  1008     Symbol* s2 = cp2->symbol_at(index2);
  1009     if (s1 == s2) {
  1010       return true;
  1012   } break;
  1014   // Invalid is used as the tag for the second constant pool entry
  1015   // occupied by JVM_CONSTANT_Double or JVM_CONSTANT_Long. It should
  1016   // not be seen by itself.
  1017   case JVM_CONSTANT_Invalid: // fall through
  1019   default:
  1020     ShouldNotReachHere();
  1021     break;
  1024   return false;
  1025 } // end compare_entry_to()
  1028 // Copy this constant pool's entries at start_i to end_i (inclusive)
  1029 // to the constant pool to_cp's entries starting at to_i. A total of
  1030 // (end_i - start_i) + 1 entries are copied.
  1031 void constantPoolOopDesc::copy_cp_to_impl(constantPoolHandle from_cp, int start_i, int end_i,
  1032        constantPoolHandle to_cp, int to_i, TRAPS) {
  1034   int dest_i = to_i;  // leave original alone for debug purposes
  1036   for (int src_i = start_i; src_i <= end_i; /* see loop bottom */ ) {
  1037     copy_entry_to(from_cp, src_i, to_cp, dest_i, CHECK);
  1039     switch (from_cp->tag_at(src_i).value()) {
  1040     case JVM_CONSTANT_Double:
  1041     case JVM_CONSTANT_Long:
  1042       // double and long take two constant pool entries
  1043       src_i += 2;
  1044       dest_i += 2;
  1045       break;
  1047     default:
  1048       // all others take one constant pool entry
  1049       src_i++;
  1050       dest_i++;
  1051       break;
  1055   int from_oplen = operand_array_length(from_cp->operands());
  1056   int old_oplen  = operand_array_length(to_cp->operands());
  1057   if (from_oplen != 0) {
  1058     // append my operands to the target's operands array
  1059     if (old_oplen == 0) {
  1060       to_cp->set_operands(from_cp->operands());  // reuse; do not merge
  1061     } else {
  1062       int old_len  = to_cp->operands()->length();
  1063       int from_len = from_cp->operands()->length();
  1064       int old_off  = old_oplen * sizeof(u2);
  1065       int from_off = from_oplen * sizeof(u2);
  1066       typeArrayHandle new_operands = oopFactory::new_permanent_shortArray(old_len + from_len, CHECK);
  1067       int fillp = 0, len = 0;
  1068       // first part of dest
  1069       Copy::conjoint_memory_atomic(to_cp->operands()->short_at_addr(0),
  1070                                    new_operands->short_at_addr(fillp),
  1071                                    (len = old_off) * sizeof(u2));
  1072       fillp += len;
  1073       // first part of src
  1074       Copy::conjoint_memory_atomic(to_cp->operands()->short_at_addr(0),
  1075                                    new_operands->short_at_addr(fillp),
  1076                                    (len = from_off) * sizeof(u2));
  1077       fillp += len;
  1078       // second part of dest
  1079       Copy::conjoint_memory_atomic(to_cp->operands()->short_at_addr(old_off),
  1080                                    new_operands->short_at_addr(fillp),
  1081                                    (len = old_len - old_off) * sizeof(u2));
  1082       fillp += len;
  1083       // second part of src
  1084       Copy::conjoint_memory_atomic(to_cp->operands()->short_at_addr(from_off),
  1085                                    new_operands->short_at_addr(fillp),
  1086                                    (len = from_len - from_off) * sizeof(u2));
  1087       fillp += len;
  1088       assert(fillp == new_operands->length(), "");
  1090       // Adjust indexes in the first part of the copied operands array.
  1091       for (int j = 0; j < from_oplen; j++) {
  1092         int offset = operand_offset_at(new_operands(), old_oplen + j);
  1093         assert(offset == operand_offset_at(from_cp->operands(), j), "correct copy");
  1094         offset += old_len;  // every new tuple is preceded by old_len extra u2's
  1095         operand_offset_at_put(new_operands(), old_oplen + j, offset);
  1098       // replace target operands array with combined array
  1099       to_cp->set_operands(new_operands());
  1103 } // end copy_cp_to()
  1106 // Copy this constant pool's entry at from_i to the constant pool
  1107 // to_cp's entry at to_i.
  1108 void constantPoolOopDesc::copy_entry_to(constantPoolHandle from_cp, int from_i,
  1109                                         constantPoolHandle to_cp, int to_i,
  1110                                         TRAPS) {
  1112   int tag = from_cp->tag_at(from_i).value();
  1113   switch (tag) {
  1114   case JVM_CONSTANT_Class:
  1116     klassOop k = from_cp->klass_at(from_i, CHECK);
  1117     to_cp->klass_at_put(to_i, k);
  1118   } break;
  1120   case JVM_CONSTANT_ClassIndex:
  1122     jint ki = from_cp->klass_index_at(from_i);
  1123     to_cp->klass_index_at_put(to_i, ki);
  1124   } break;
  1126   case JVM_CONSTANT_Double:
  1128     jdouble d = from_cp->double_at(from_i);
  1129     to_cp->double_at_put(to_i, d);
  1130     // double takes two constant pool entries so init second entry's tag
  1131     to_cp->tag_at_put(to_i + 1, JVM_CONSTANT_Invalid);
  1132   } break;
  1134   case JVM_CONSTANT_Fieldref:
  1136     int class_index = from_cp->uncached_klass_ref_index_at(from_i);
  1137     int name_and_type_index = from_cp->uncached_name_and_type_ref_index_at(from_i);
  1138     to_cp->field_at_put(to_i, class_index, name_and_type_index);
  1139   } break;
  1141   case JVM_CONSTANT_Float:
  1143     jfloat f = from_cp->float_at(from_i);
  1144     to_cp->float_at_put(to_i, f);
  1145   } break;
  1147   case JVM_CONSTANT_Integer:
  1149     jint i = from_cp->int_at(from_i);
  1150     to_cp->int_at_put(to_i, i);
  1151   } break;
  1153   case JVM_CONSTANT_InterfaceMethodref:
  1155     int class_index = from_cp->uncached_klass_ref_index_at(from_i);
  1156     int name_and_type_index = from_cp->uncached_name_and_type_ref_index_at(from_i);
  1157     to_cp->interface_method_at_put(to_i, class_index, name_and_type_index);
  1158   } break;
  1160   case JVM_CONSTANT_Long:
  1162     jlong l = from_cp->long_at(from_i);
  1163     to_cp->long_at_put(to_i, l);
  1164     // long takes two constant pool entries so init second entry's tag
  1165     to_cp->tag_at_put(to_i + 1, JVM_CONSTANT_Invalid);
  1166   } break;
  1168   case JVM_CONSTANT_Methodref:
  1170     int class_index = from_cp->uncached_klass_ref_index_at(from_i);
  1171     int name_and_type_index = from_cp->uncached_name_and_type_ref_index_at(from_i);
  1172     to_cp->method_at_put(to_i, class_index, name_and_type_index);
  1173   } break;
  1175   case JVM_CONSTANT_NameAndType:
  1177     int name_ref_index = from_cp->name_ref_index_at(from_i);
  1178     int signature_ref_index = from_cp->signature_ref_index_at(from_i);
  1179     to_cp->name_and_type_at_put(to_i, name_ref_index, signature_ref_index);
  1180   } break;
  1182   case JVM_CONSTANT_String:
  1184     oop s = from_cp->string_at(from_i, CHECK);
  1185     to_cp->string_at_put(to_i, s);
  1186   } break;
  1188   case JVM_CONSTANT_StringIndex:
  1190     jint si = from_cp->string_index_at(from_i);
  1191     to_cp->string_index_at_put(to_i, si);
  1192   } break;
  1194   case JVM_CONSTANT_UnresolvedClass:
  1196     // Can be resolved after checking tag, so check the slot first.
  1197     CPSlot entry = from_cp->slot_at(from_i);
  1198     if (entry.is_oop()) {
  1199       assert(entry.get_oop()->is_klass(), "must be");
  1200       // Already resolved
  1201       to_cp->klass_at_put(to_i, (klassOop)entry.get_oop());
  1202     } else {
  1203       to_cp->unresolved_klass_at_put(to_i, entry.get_symbol());
  1205   } break;
  1207   case JVM_CONSTANT_UnresolvedClassInError:
  1209     Symbol* k = from_cp->unresolved_klass_at(from_i);
  1210     to_cp->unresolved_klass_at_put(to_i, k);
  1211     to_cp->tag_at_put(to_i, JVM_CONSTANT_UnresolvedClassInError);
  1212   } break;
  1215   case JVM_CONSTANT_UnresolvedString:
  1217     // Can be resolved after checking tag, so check the slot first.
  1218     CPSlot entry = from_cp->slot_at(from_i);
  1219     if (entry.is_oop()) {
  1220       // Already resolved (either string or pseudo-string)
  1221       to_cp->string_at_put(to_i, entry.get_oop());
  1222     } else {
  1223       to_cp->unresolved_string_at_put(to_i, entry.get_symbol());
  1225   } break;
  1227   case JVM_CONSTANT_Utf8:
  1229     Symbol* s = from_cp->symbol_at(from_i);
  1230     to_cp->symbol_at_put(to_i, s);
  1231     // This constantPool has the same lifetime as the original, so don't
  1232     // increase reference counts for the copy.
  1233   } break;
  1235   case JVM_CONSTANT_MethodType:
  1237     jint k = from_cp->method_type_index_at(from_i);
  1238     to_cp->method_type_index_at_put(to_i, k);
  1239   } break;
  1241   case JVM_CONSTANT_MethodHandle:
  1243     int k1 = from_cp->method_handle_ref_kind_at(from_i);
  1244     int k2 = from_cp->method_handle_index_at(from_i);
  1245     to_cp->method_handle_index_at_put(to_i, k1, k2);
  1246   } break;
  1248   case JVM_CONSTANT_InvokeDynamic:
  1250     int k1 = from_cp->invoke_dynamic_bootstrap_specifier_index(from_i);
  1251     int k2 = from_cp->invoke_dynamic_name_and_type_ref_index_at(from_i);
  1252     k1 += operand_array_length(to_cp->operands());  // to_cp might already have operands
  1253     to_cp->invoke_dynamic_at_put(to_i, k1, k2);
  1254   } break;
  1256   // Invalid is used as the tag for the second constant pool entry
  1257   // occupied by JVM_CONSTANT_Double or JVM_CONSTANT_Long. It should
  1258   // not be seen by itself.
  1259   case JVM_CONSTANT_Invalid: // fall through
  1261   default:
  1263     ShouldNotReachHere();
  1264   } break;
  1266 } // end copy_entry_to()
  1269 // Search constant pool search_cp for an entry that matches this
  1270 // constant pool's entry at pattern_i. Returns the index of a
  1271 // matching entry or zero (0) if there is no matching entry.
  1272 int constantPoolOopDesc::find_matching_entry(int pattern_i,
  1273       constantPoolHandle search_cp, TRAPS) {
  1275   // index zero (0) is not used
  1276   for (int i = 1; i < search_cp->length(); i++) {
  1277     bool found = compare_entry_to(pattern_i, search_cp, i, CHECK_0);
  1278     if (found) {
  1279       return i;
  1283   return 0;  // entry not found; return unused index zero (0)
  1284 } // end find_matching_entry()
  1287 #ifndef PRODUCT
  1289 const char* constantPoolOopDesc::printable_name_at(int which) {
  1291   constantTag tag = tag_at(which);
  1293   if (tag.is_unresolved_string() || tag.is_string()) {
  1294     return string_at_noresolve(which);
  1295   } else if (tag.is_klass() || tag.is_unresolved_klass()) {
  1296     return klass_name_at(which)->as_C_string();
  1297   } else if (tag.is_symbol()) {
  1298     return symbol_at(which)->as_C_string();
  1300   return "";
  1303 #endif // PRODUCT
  1306 // JVMTI GetConstantPool support
  1308 // For temporary use until code is stable.
  1309 #define DBG(code)
  1311 static const char* WARN_MSG = "Must not be such entry!";
  1313 static void print_cpool_bytes(jint cnt, u1 *bytes) {
  1314   jint size = 0;
  1315   u2   idx1, idx2;
  1317   for (jint idx = 1; idx < cnt; idx++) {
  1318     jint ent_size = 0;
  1319     u1   tag  = *bytes++;
  1320     size++;                       // count tag
  1322     printf("const #%03d, tag: %02d ", idx, tag);
  1323     switch(tag) {
  1324       case JVM_CONSTANT_Invalid: {
  1325         printf("Invalid");
  1326         break;
  1328       case JVM_CONSTANT_Unicode: {
  1329         printf("Unicode      %s", WARN_MSG);
  1330         break;
  1332       case JVM_CONSTANT_Utf8: {
  1333         u2 len = Bytes::get_Java_u2(bytes);
  1334         char str[128];
  1335         if (len > 127) {
  1336            len = 127;
  1338         strncpy(str, (char *) (bytes+2), len);
  1339         str[len] = '\0';
  1340         printf("Utf8          \"%s\"", str);
  1341         ent_size = 2 + len;
  1342         break;
  1344       case JVM_CONSTANT_Integer: {
  1345         u4 val = Bytes::get_Java_u4(bytes);
  1346         printf("int          %d", *(int *) &val);
  1347         ent_size = 4;
  1348         break;
  1350       case JVM_CONSTANT_Float: {
  1351         u4 val = Bytes::get_Java_u4(bytes);
  1352         printf("float        %5.3ff", *(float *) &val);
  1353         ent_size = 4;
  1354         break;
  1356       case JVM_CONSTANT_Long: {
  1357         u8 val = Bytes::get_Java_u8(bytes);
  1358         printf("long         "INT64_FORMAT, (int64_t) *(jlong *) &val);
  1359         ent_size = 8;
  1360         idx++; // Long takes two cpool slots
  1361         break;
  1363       case JVM_CONSTANT_Double: {
  1364         u8 val = Bytes::get_Java_u8(bytes);
  1365         printf("double       %5.3fd", *(jdouble *)&val);
  1366         ent_size = 8;
  1367         idx++; // Double takes two cpool slots
  1368         break;
  1370       case JVM_CONSTANT_Class: {
  1371         idx1 = Bytes::get_Java_u2(bytes);
  1372         printf("class        #%03d", idx1);
  1373         ent_size = 2;
  1374         break;
  1376       case JVM_CONSTANT_String: {
  1377         idx1 = Bytes::get_Java_u2(bytes);
  1378         printf("String       #%03d", idx1);
  1379         ent_size = 2;
  1380         break;
  1382       case JVM_CONSTANT_Fieldref: {
  1383         idx1 = Bytes::get_Java_u2(bytes);
  1384         idx2 = Bytes::get_Java_u2(bytes+2);
  1385         printf("Field        #%03d, #%03d", (int) idx1, (int) idx2);
  1386         ent_size = 4;
  1387         break;
  1389       case JVM_CONSTANT_Methodref: {
  1390         idx1 = Bytes::get_Java_u2(bytes);
  1391         idx2 = Bytes::get_Java_u2(bytes+2);
  1392         printf("Method       #%03d, #%03d", idx1, idx2);
  1393         ent_size = 4;
  1394         break;
  1396       case JVM_CONSTANT_InterfaceMethodref: {
  1397         idx1 = Bytes::get_Java_u2(bytes);
  1398         idx2 = Bytes::get_Java_u2(bytes+2);
  1399         printf("InterfMethod #%03d, #%03d", idx1, idx2);
  1400         ent_size = 4;
  1401         break;
  1403       case JVM_CONSTANT_NameAndType: {
  1404         idx1 = Bytes::get_Java_u2(bytes);
  1405         idx2 = Bytes::get_Java_u2(bytes+2);
  1406         printf("NameAndType  #%03d, #%03d", idx1, idx2);
  1407         ent_size = 4;
  1408         break;
  1410       case JVM_CONSTANT_ClassIndex: {
  1411         printf("ClassIndex  %s", WARN_MSG);
  1412         break;
  1414       case JVM_CONSTANT_UnresolvedClass: {
  1415         printf("UnresolvedClass: %s", WARN_MSG);
  1416         break;
  1418       case JVM_CONSTANT_UnresolvedClassInError: {
  1419         printf("UnresolvedClassInErr: %s", WARN_MSG);
  1420         break;
  1422       case JVM_CONSTANT_StringIndex: {
  1423         printf("StringIndex: %s", WARN_MSG);
  1424         break;
  1426       case JVM_CONSTANT_UnresolvedString: {
  1427         printf("UnresolvedString: %s", WARN_MSG);
  1428         break;
  1431     printf(";\n");
  1432     bytes += ent_size;
  1433     size  += ent_size;
  1435   printf("Cpool size: %d\n", size);
  1436   fflush(0);
  1437   return;
  1438 } /* end print_cpool_bytes */
  1441 // Returns size of constant pool entry.
  1442 jint constantPoolOopDesc::cpool_entry_size(jint idx) {
  1443   switch(tag_at(idx).value()) {
  1444     case JVM_CONSTANT_Invalid:
  1445     case JVM_CONSTANT_Unicode:
  1446       return 1;
  1448     case JVM_CONSTANT_Utf8:
  1449       return 3 + symbol_at(idx)->utf8_length();
  1451     case JVM_CONSTANT_Class:
  1452     case JVM_CONSTANT_String:
  1453     case JVM_CONSTANT_ClassIndex:
  1454     case JVM_CONSTANT_UnresolvedClass:
  1455     case JVM_CONSTANT_UnresolvedClassInError:
  1456     case JVM_CONSTANT_StringIndex:
  1457     case JVM_CONSTANT_UnresolvedString:
  1458     case JVM_CONSTANT_MethodType:
  1459       return 3;
  1461     case JVM_CONSTANT_MethodHandle:
  1462       return 4; //tag, ref_kind, ref_index
  1464     case JVM_CONSTANT_Integer:
  1465     case JVM_CONSTANT_Float:
  1466     case JVM_CONSTANT_Fieldref:
  1467     case JVM_CONSTANT_Methodref:
  1468     case JVM_CONSTANT_InterfaceMethodref:
  1469     case JVM_CONSTANT_NameAndType:
  1470       return 5;
  1472     case JVM_CONSTANT_InvokeDynamic:
  1473       // u1 tag, u2 bsm, u2 nt
  1474       return 5;
  1476     case JVM_CONSTANT_Long:
  1477     case JVM_CONSTANT_Double:
  1478       return 9;
  1480   assert(false, "cpool_entry_size: Invalid constant pool entry tag");
  1481   return 1;
  1482 } /* end cpool_entry_size */
  1485 // SymbolHashMap is used to find a constant pool index from a string.
  1486 // This function fills in SymbolHashMaps, one for utf8s and one for
  1487 // class names, returns size of the cpool raw bytes.
  1488 jint constantPoolOopDesc::hash_entries_to(SymbolHashMap *symmap,
  1489                                           SymbolHashMap *classmap) {
  1490   jint size = 0;
  1492   for (u2 idx = 1; idx < length(); idx++) {
  1493     u2 tag = tag_at(idx).value();
  1494     size += cpool_entry_size(idx);
  1496     switch(tag) {
  1497       case JVM_CONSTANT_Utf8: {
  1498         Symbol* sym = symbol_at(idx);
  1499         symmap->add_entry(sym, idx);
  1500         DBG(printf("adding symbol entry %s = %d\n", sym->as_utf8(), idx));
  1501         break;
  1503       case JVM_CONSTANT_Class:
  1504       case JVM_CONSTANT_UnresolvedClass:
  1505       case JVM_CONSTANT_UnresolvedClassInError: {
  1506         Symbol* sym = klass_name_at(idx);
  1507         classmap->add_entry(sym, idx);
  1508         DBG(printf("adding class entry %s = %d\n", sym->as_utf8(), idx));
  1509         break;
  1511       case JVM_CONSTANT_Long:
  1512       case JVM_CONSTANT_Double: {
  1513         idx++; // Both Long and Double take two cpool slots
  1514         break;
  1518   return size;
  1519 } /* end hash_utf8_entries_to */
  1522 // Copy cpool bytes.
  1523 // Returns:
  1524 //    0, in case of OutOfMemoryError
  1525 //   -1, in case of internal error
  1526 //  > 0, count of the raw cpool bytes that have been copied
  1527 int constantPoolOopDesc::copy_cpool_bytes(int cpool_size,
  1528                                           SymbolHashMap* tbl,
  1529                                           unsigned char *bytes) {
  1530   u2   idx1, idx2;
  1531   jint size  = 0;
  1532   jint cnt   = length();
  1533   unsigned char *start_bytes = bytes;
  1535   for (jint idx = 1; idx < cnt; idx++) {
  1536     u1   tag      = tag_at(idx).value();
  1537     jint ent_size = cpool_entry_size(idx);
  1539     assert(size + ent_size <= cpool_size, "Size mismatch");
  1541     *bytes = tag;
  1542     DBG(printf("#%03hd tag=%03hd, ", idx, tag));
  1543     switch(tag) {
  1544       case JVM_CONSTANT_Invalid: {
  1545         DBG(printf("JVM_CONSTANT_Invalid"));
  1546         break;
  1548       case JVM_CONSTANT_Unicode: {
  1549         assert(false, "Wrong constant pool tag: JVM_CONSTANT_Unicode");
  1550         DBG(printf("JVM_CONSTANT_Unicode"));
  1551         break;
  1553       case JVM_CONSTANT_Utf8: {
  1554         Symbol* sym = symbol_at(idx);
  1555         char*     str = sym->as_utf8();
  1556         // Warning! It's crashing on x86 with len = sym->utf8_length()
  1557         int       len = (int) strlen(str);
  1558         Bytes::put_Java_u2((address) (bytes+1), (u2) len);
  1559         for (int i = 0; i < len; i++) {
  1560             bytes[3+i] = (u1) str[i];
  1562         DBG(printf("JVM_CONSTANT_Utf8: %s ", str));
  1563         break;
  1565       case JVM_CONSTANT_Integer: {
  1566         jint val = int_at(idx);
  1567         Bytes::put_Java_u4((address) (bytes+1), *(u4*)&val);
  1568         break;
  1570       case JVM_CONSTANT_Float: {
  1571         jfloat val = float_at(idx);
  1572         Bytes::put_Java_u4((address) (bytes+1), *(u4*)&val);
  1573         break;
  1575       case JVM_CONSTANT_Long: {
  1576         jlong val = long_at(idx);
  1577         Bytes::put_Java_u8((address) (bytes+1), *(u8*)&val);
  1578         idx++;             // Long takes two cpool slots
  1579         break;
  1581       case JVM_CONSTANT_Double: {
  1582         jdouble val = double_at(idx);
  1583         Bytes::put_Java_u8((address) (bytes+1), *(u8*)&val);
  1584         idx++;             // Double takes two cpool slots
  1585         break;
  1587       case JVM_CONSTANT_Class:
  1588       case JVM_CONSTANT_UnresolvedClass:
  1589       case JVM_CONSTANT_UnresolvedClassInError: {
  1590         *bytes = JVM_CONSTANT_Class;
  1591         Symbol* sym = klass_name_at(idx);
  1592         idx1 = tbl->symbol_to_value(sym);
  1593         assert(idx1 != 0, "Have not found a hashtable entry");
  1594         Bytes::put_Java_u2((address) (bytes+1), idx1);
  1595         DBG(printf("JVM_CONSTANT_Class: idx=#%03hd, %s", idx1, sym->as_utf8()));
  1596         break;
  1598       case JVM_CONSTANT_String: {
  1599         unsigned int hash;
  1600         char *str = string_at_noresolve(idx);
  1601         TempNewSymbol sym = SymbolTable::lookup_only(str, (int) strlen(str), hash);
  1602         if (sym == NULL) {
  1603           // sym can be NULL if string refers to incorrectly encoded JVM_CONSTANT_Utf8
  1604           // this can happen with JVM TI; see CR 6839599 for more details
  1605           oop string = *(obj_at_addr_raw(idx));
  1606           assert(java_lang_String::is_instance(string),"Not a String");
  1607           DBG(printf("Error #%03hd tag=%03hd\n", idx, tag));
  1608           idx1 = 0;
  1609           for (int j = 0; j < tbl->table_size() && idx1 == 0; j++) {
  1610             for (SymbolHashMapEntry* cur = tbl->bucket(j); cur != NULL; cur = cur->next()) {
  1611               int length;
  1612               Symbol* s = cur->symbol();
  1613               jchar* chars = s->as_unicode(length);
  1614               if (java_lang_String::equals(string, chars, length)) {
  1615                 idx1 = cur->value();
  1616                 DBG(printf("Index found: %d\n",idx1));
  1617                 break;
  1621         } else {
  1622           idx1 = tbl->symbol_to_value(sym);
  1624         assert(idx1 != 0, "Have not found a hashtable entry");
  1625         Bytes::put_Java_u2((address) (bytes+1), idx1);
  1626         DBG(printf("JVM_CONSTANT_String: idx=#%03hd, %s", idx1, str));
  1627         break;
  1629       case JVM_CONSTANT_UnresolvedString: {
  1630         *bytes = JVM_CONSTANT_String;
  1631         Symbol* sym = unresolved_string_at(idx);
  1632         idx1 = tbl->symbol_to_value(sym);
  1633         assert(idx1 != 0, "Have not found a hashtable entry");
  1634         Bytes::put_Java_u2((address) (bytes+1), idx1);
  1635         DBG(char *str = sym->as_utf8());
  1636         DBG(printf("JVM_CONSTANT_UnresolvedString: idx=#%03hd, %s", idx1, str));
  1637         break;
  1639       case JVM_CONSTANT_Fieldref:
  1640       case JVM_CONSTANT_Methodref:
  1641       case JVM_CONSTANT_InterfaceMethodref: {
  1642         idx1 = uncached_klass_ref_index_at(idx);
  1643         idx2 = uncached_name_and_type_ref_index_at(idx);
  1644         Bytes::put_Java_u2((address) (bytes+1), idx1);
  1645         Bytes::put_Java_u2((address) (bytes+3), idx2);
  1646         DBG(printf("JVM_CONSTANT_Methodref: %hd %hd", idx1, idx2));
  1647         break;
  1649       case JVM_CONSTANT_NameAndType: {
  1650         idx1 = name_ref_index_at(idx);
  1651         idx2 = signature_ref_index_at(idx);
  1652         Bytes::put_Java_u2((address) (bytes+1), idx1);
  1653         Bytes::put_Java_u2((address) (bytes+3), idx2);
  1654         DBG(printf("JVM_CONSTANT_NameAndType: %hd %hd", idx1, idx2));
  1655         break;
  1657       case JVM_CONSTANT_ClassIndex: {
  1658         *bytes = JVM_CONSTANT_Class;
  1659         idx1 = klass_index_at(idx);
  1660         Bytes::put_Java_u2((address) (bytes+1), idx1);
  1661         DBG(printf("JVM_CONSTANT_ClassIndex: %hd", idx1));
  1662         break;
  1664       case JVM_CONSTANT_StringIndex: {
  1665         *bytes = JVM_CONSTANT_String;
  1666         idx1 = string_index_at(idx);
  1667         Bytes::put_Java_u2((address) (bytes+1), idx1);
  1668         DBG(printf("JVM_CONSTANT_StringIndex: %hd", idx1));
  1669         break;
  1671       case JVM_CONSTANT_MethodHandle: {
  1672         *bytes = JVM_CONSTANT_MethodHandle;
  1673         int kind = method_handle_ref_kind_at(idx);
  1674         idx1 = method_handle_index_at(idx);
  1675         *(bytes+1) = (unsigned char) kind;
  1676         Bytes::put_Java_u2((address) (bytes+2), idx1);
  1677         DBG(printf("JVM_CONSTANT_MethodHandle: %d %hd", kind, idx1));
  1678         break;
  1680       case JVM_CONSTANT_MethodType: {
  1681         *bytes = JVM_CONSTANT_MethodType;
  1682         idx1 = method_type_index_at(idx);
  1683         Bytes::put_Java_u2((address) (bytes+1), idx1);
  1684         DBG(printf("JVM_CONSTANT_MethodType: %hd", idx1));
  1685         break;
  1687       case JVM_CONSTANT_InvokeDynamic: {
  1688         *bytes = tag;
  1689         idx1 = extract_low_short_from_int(*int_at_addr(idx));
  1690         idx2 = extract_high_short_from_int(*int_at_addr(idx));
  1691         assert(idx2 == invoke_dynamic_name_and_type_ref_index_at(idx), "correct half of u4");
  1692         Bytes::put_Java_u2((address) (bytes+1), idx1);
  1693         Bytes::put_Java_u2((address) (bytes+3), idx2);
  1694         DBG(printf("JVM_CONSTANT_InvokeDynamic: %hd %hd", idx1, idx2));
  1695         break;
  1698     DBG(printf("\n"));
  1699     bytes += ent_size;
  1700     size  += ent_size;
  1702   assert(size == cpool_size, "Size mismatch");
  1704   // Keep temorarily for debugging until it's stable.
  1705   DBG(print_cpool_bytes(cnt, start_bytes));
  1706   return (int)(bytes - start_bytes);
  1707 } /* end copy_cpool_bytes */
  1710 void SymbolHashMap::add_entry(Symbol* sym, u2 value) {
  1711   char *str = sym->as_utf8();
  1712   unsigned int hash = compute_hash(str, sym->utf8_length());
  1713   unsigned int index = hash % table_size();
  1715   // check if already in map
  1716   // we prefer the first entry since it is more likely to be what was used in
  1717   // the class file
  1718   for (SymbolHashMapEntry *en = bucket(index); en != NULL; en = en->next()) {
  1719     assert(en->symbol() != NULL, "SymbolHashMapEntry symbol is NULL");
  1720     if (en->hash() == hash && en->symbol() == sym) {
  1721         return;  // already there
  1725   SymbolHashMapEntry* entry = new SymbolHashMapEntry(hash, sym, value);
  1726   entry->set_next(bucket(index));
  1727   _buckets[index].set_entry(entry);
  1728   assert(entry->symbol() != NULL, "SymbolHashMapEntry symbol is NULL");
  1731 SymbolHashMapEntry* SymbolHashMap::find_entry(Symbol* sym) {
  1732   assert(sym != NULL, "SymbolHashMap::find_entry - symbol is NULL");
  1733   char *str = sym->as_utf8();
  1734   int   len = sym->utf8_length();
  1735   unsigned int hash = SymbolHashMap::compute_hash(str, len);
  1736   unsigned int index = hash % table_size();
  1737   for (SymbolHashMapEntry *en = bucket(index); en != NULL; en = en->next()) {
  1738     assert(en->symbol() != NULL, "SymbolHashMapEntry symbol is NULL");
  1739     if (en->hash() == hash && en->symbol() == sym) {
  1740       return en;
  1743   return NULL;

mercurial