src/share/vm/oops/constantPoolOop.cpp

Thu, 07 Apr 2011 17:02:30 -0700

author
jrose
date
Thu, 07 Apr 2011 17:02:30 -0700
changeset 2742
ed69575596ac
parent 2658
c7f3d0b4570f
child 2982
ddd894528dbc
permissions
-rw-r--r--

6981791: remove experimental code for JSR 292
Reviewed-by: twisti

     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 Symbol* constantPoolOopDesc::impl_name_ref_at(int which, bool uncached) {
   270   int name_index = name_ref_index_at(impl_name_and_type_ref_index_at(which, uncached));
   271   return symbol_at(name_index);
   272 }
   275 Symbol* constantPoolOopDesc::impl_signature_ref_at(int which, bool uncached) {
   276   int signature_index = signature_ref_index_at(impl_name_and_type_ref_index_at(which, uncached));
   277   return symbol_at(signature_index);
   278 }
   281 int constantPoolOopDesc::impl_name_and_type_ref_index_at(int which, bool uncached) {
   282   int i = which;
   283   if (!uncached && cache() != NULL) {
   284     if (constantPoolCacheOopDesc::is_secondary_index(which)) {
   285       // Invokedynamic index.
   286       int pool_index = cache()->main_entry_at(which)->constant_pool_index();
   287       pool_index = invoke_dynamic_name_and_type_ref_index_at(pool_index);
   288       assert(tag_at(pool_index).is_name_and_type(), "");
   289       return pool_index;
   290     }
   291     // change byte-ordering and go via cache
   292     i = remap_instruction_operand_from_cache(which);
   293   } else {
   294     if (tag_at(which).is_invoke_dynamic()) {
   295       int pool_index = invoke_dynamic_name_and_type_ref_index_at(which);
   296       assert(tag_at(pool_index).is_name_and_type(), "");
   297       return pool_index;
   298     }
   299   }
   300   assert(tag_at(i).is_field_or_method(), "Corrupted constant pool");
   301   assert(!tag_at(i).is_invoke_dynamic(), "Must be handled above");
   302   jint ref_index = *int_at_addr(i);
   303   return extract_high_short_from_int(ref_index);
   304 }
   307 int constantPoolOopDesc::impl_klass_ref_index_at(int which, bool uncached) {
   308   guarantee(!constantPoolCacheOopDesc::is_secondary_index(which),
   309             "an invokedynamic instruction does not have a klass");
   310   int i = which;
   311   if (!uncached && cache() != NULL) {
   312     // change byte-ordering and go via cache
   313     i = remap_instruction_operand_from_cache(which);
   314   }
   315   assert(tag_at(i).is_field_or_method(), "Corrupted constant pool");
   316   jint ref_index = *int_at_addr(i);
   317   return extract_low_short_from_int(ref_index);
   318 }
   322 int constantPoolOopDesc::remap_instruction_operand_from_cache(int operand) {
   323   int cpc_index = operand;
   324   DEBUG_ONLY(cpc_index -= CPCACHE_INDEX_TAG);
   325   assert((int)(u2)cpc_index == cpc_index, "clean u2");
   326   int member_index = cache()->entry_at(cpc_index)->constant_pool_index();
   327   return member_index;
   328 }
   331 void constantPoolOopDesc::verify_constant_pool_resolve(constantPoolHandle this_oop, KlassHandle k, TRAPS) {
   332  if (k->oop_is_instance() || k->oop_is_objArray()) {
   333     instanceKlassHandle holder (THREAD, this_oop->pool_holder());
   334     klassOop elem_oop = k->oop_is_instance() ? k() : objArrayKlass::cast(k())->bottom_klass();
   335     KlassHandle element (THREAD, elem_oop);
   337     // The element type could be a typeArray - we only need the access check if it is
   338     // an reference to another class
   339     if (element->oop_is_instance()) {
   340       LinkResolver::check_klass_accessability(holder, element, CHECK);
   341     }
   342   }
   343 }
   346 int constantPoolOopDesc::name_ref_index_at(int which_nt) {
   347   jint ref_index = name_and_type_at(which_nt);
   348   return extract_low_short_from_int(ref_index);
   349 }
   352 int constantPoolOopDesc::signature_ref_index_at(int which_nt) {
   353   jint ref_index = name_and_type_at(which_nt);
   354   return extract_high_short_from_int(ref_index);
   355 }
   358 klassOop constantPoolOopDesc::klass_ref_at(int which, TRAPS) {
   359   return klass_at(klass_ref_index_at(which), CHECK_NULL);
   360 }
   363 Symbol* constantPoolOopDesc::klass_name_at(int which) {
   364   assert(tag_at(which).is_unresolved_klass() || tag_at(which).is_klass(),
   365          "Corrupted constant pool");
   366   // A resolved constantPool entry will contain a klassOop, otherwise a Symbol*.
   367   // It is not safe to rely on the tag bit's here, since we don't have a lock, and the entry and
   368   // tag is not updated atomicly.
   369   CPSlot entry = slot_at(which);
   370   if (entry.is_oop()) {
   371     // Already resolved - return entry's name.
   372     assert(entry.get_oop()->is_klass(), "must be");
   373     return klassOop(entry.get_oop())->klass_part()->name();
   374   } else {
   375     assert(entry.is_metadata(), "must be either symbol or klass");
   376     return entry.get_symbol();
   377   }
   378 }
   380 Symbol* constantPoolOopDesc::klass_ref_at_noresolve(int which) {
   381   jint ref_index = klass_ref_index_at(which);
   382   return klass_at_noresolve(ref_index);
   383 }
   385 Symbol* constantPoolOopDesc::uncached_klass_ref_at_noresolve(int which) {
   386   jint ref_index = uncached_klass_ref_index_at(which);
   387   return klass_at_noresolve(ref_index);
   388 }
   390 char* constantPoolOopDesc::string_at_noresolve(int which) {
   391   // Test entry type in case string is resolved while in here.
   392   CPSlot entry = slot_at(which);
   393   if (entry.is_metadata()) {
   394     return (entry.get_symbol())->as_C_string();
   395   } else if (java_lang_String::is_instance(entry.get_oop())) {
   396     return java_lang_String::as_utf8_string(entry.get_oop());
   397   } else {
   398     return (char*)"<pseudo-string>";
   399   }
   400 }
   403 BasicType constantPoolOopDesc::basic_type_for_signature_at(int which) {
   404   return FieldType::basic_type(symbol_at(which));
   405 }
   408 void constantPoolOopDesc::resolve_string_constants_impl(constantPoolHandle this_oop, TRAPS) {
   409   for (int index = 1; index < this_oop->length(); index++) { // Index 0 is unused
   410     if (this_oop->tag_at(index).is_unresolved_string()) {
   411       this_oop->string_at(index, CHECK);
   412     }
   413   }
   414 }
   416 // A resolved constant value in the CP cache is represented as a non-null
   417 // value.  As a special case, this value can be a 'systemObjArray'
   418 // which masks an exception object to throw.
   419 // This allows a MethodHandle constant reference to throw a consistent
   420 // exception every time, if it fails to resolve.
   421 static oop decode_exception_from_f1(oop result_oop, TRAPS) {
   422   if (result_oop->klass() != Universe::systemObjArrayKlassObj())
   423     return result_oop;
   425   // Special cases here:  Masked null, saved exception.
   426   objArrayOop sys_array = (objArrayOop) result_oop;
   427   assert(sys_array->length() == 1, "bad system array");
   428   if (sys_array->length() == 1) {
   429     THROW_OOP_(sys_array->obj_at(0), NULL);
   430   }
   431   return NULL;
   432 }
   434 oop constantPoolOopDesc::resolve_constant_at_impl(constantPoolHandle this_oop, int index, int cache_index, TRAPS) {
   435   oop result_oop = NULL;
   436   Handle throw_exception;
   438   if (cache_index == _possible_index_sentinel) {
   439     // It is possible that this constant is one which is cached in the CP cache.
   440     // We'll do a linear search.  This should be OK because this usage is rare.
   441     assert(index > 0, "valid index");
   442     constantPoolCacheOop cache = this_oop()->cache();
   443     for (int i = 0, len = cache->length(); i < len; i++) {
   444       ConstantPoolCacheEntry* cpc_entry = cache->entry_at(i);
   445       if (!cpc_entry->is_secondary_entry() && cpc_entry->constant_pool_index() == index) {
   446         // Switch the query to use this CPC entry.
   447         cache_index = i;
   448         index = _no_index_sentinel;
   449         break;
   450       }
   451     }
   452     if (cache_index == _possible_index_sentinel)
   453       cache_index = _no_index_sentinel;  // not found
   454   }
   455   assert(cache_index == _no_index_sentinel || cache_index >= 0, "");
   456   assert(index == _no_index_sentinel || index >= 0, "");
   458   if (cache_index >= 0) {
   459     assert(index == _no_index_sentinel, "only one kind of index at a time");
   460     ConstantPoolCacheEntry* cpc_entry = this_oop->cache()->entry_at(cache_index);
   461     result_oop = cpc_entry->f1();
   462     if (result_oop != NULL) {
   463       return decode_exception_from_f1(result_oop, THREAD);
   464       // That was easy...
   465     }
   466     index = cpc_entry->constant_pool_index();
   467   }
   469   jvalue prim_value;  // temp used only in a few cases below
   471   int tag_value = this_oop->tag_at(index).value();
   472   switch (tag_value) {
   474   case JVM_CONSTANT_UnresolvedClass:
   475   case JVM_CONSTANT_UnresolvedClassInError:
   476   case JVM_CONSTANT_Class:
   477     {
   478       klassOop resolved = klass_at_impl(this_oop, index, CHECK_NULL);
   479       // ldc wants the java mirror.
   480       result_oop = resolved->java_mirror();
   481       break;
   482     }
   484   case JVM_CONSTANT_String:
   485   case JVM_CONSTANT_UnresolvedString:
   486     if (this_oop->is_pseudo_string_at(index)) {
   487       result_oop = this_oop->pseudo_string_at(index);
   488       break;
   489     }
   490     result_oop = string_at_impl(this_oop, index, CHECK_NULL);
   491     break;
   493   case JVM_CONSTANT_Object:
   494     result_oop = this_oop->object_at(index);
   495     break;
   497   case JVM_CONSTANT_MethodHandle:
   498     {
   499       int ref_kind                 = this_oop->method_handle_ref_kind_at(index);
   500       int callee_index             = this_oop->method_handle_klass_index_at(index);
   501       Symbol*  name =      this_oop->method_handle_name_ref_at(index);
   502       Symbol*  signature = this_oop->method_handle_signature_ref_at(index);
   503       if (PrintMiscellaneous)
   504         tty->print_cr("resolve JVM_CONSTANT_MethodHandle:%d [%d/%d/%d] %s.%s",
   505                       ref_kind, index, this_oop->method_handle_index_at(index),
   506                       callee_index, name->as_C_string(), signature->as_C_string());
   507       KlassHandle callee;
   508       { klassOop k = klass_at_impl(this_oop, callee_index, CHECK_NULL);
   509         callee = KlassHandle(THREAD, k);
   510       }
   511       KlassHandle klass(THREAD, this_oop->pool_holder());
   512       Handle value = SystemDictionary::link_method_handle_constant(klass, ref_kind,
   513                                                                    callee, name, signature,
   514                                                                    THREAD);
   515       if (HAS_PENDING_EXCEPTION) {
   516         throw_exception = Handle(THREAD, PENDING_EXCEPTION);
   517         CLEAR_PENDING_EXCEPTION;
   518         break;
   519       }
   520       result_oop = value();
   521       assert(result_oop != NULL, "");
   522       break;
   523     }
   525   case JVM_CONSTANT_MethodType:
   526     {
   527       Symbol*  signature = this_oop->method_type_signature_at(index);
   528       if (PrintMiscellaneous)
   529         tty->print_cr("resolve JVM_CONSTANT_MethodType [%d/%d] %s",
   530                       index, this_oop->method_type_index_at(index),
   531                       signature->as_C_string());
   532       KlassHandle klass(THREAD, this_oop->pool_holder());
   533       bool ignore_is_on_bcp = false;
   534       Handle value = SystemDictionary::find_method_handle_type(signature,
   535                                                                klass,
   536                                                                false,
   537                                                                ignore_is_on_bcp,
   538                                                                THREAD);
   539       if (HAS_PENDING_EXCEPTION) {
   540         throw_exception = Handle(THREAD, PENDING_EXCEPTION);
   541         CLEAR_PENDING_EXCEPTION;
   542         break;
   543       }
   544       result_oop = value();
   545       assert(result_oop != NULL, "");
   546       break;
   547     }
   549   case JVM_CONSTANT_Integer:
   550     prim_value.i = this_oop->int_at(index);
   551     result_oop = java_lang_boxing_object::create(T_INT, &prim_value, CHECK_NULL);
   552     break;
   554   case JVM_CONSTANT_Float:
   555     prim_value.f = this_oop->float_at(index);
   556     result_oop = java_lang_boxing_object::create(T_FLOAT, &prim_value, CHECK_NULL);
   557     break;
   559   case JVM_CONSTANT_Long:
   560     prim_value.j = this_oop->long_at(index);
   561     result_oop = java_lang_boxing_object::create(T_LONG, &prim_value, CHECK_NULL);
   562     break;
   564   case JVM_CONSTANT_Double:
   565     prim_value.d = this_oop->double_at(index);
   566     result_oop = java_lang_boxing_object::create(T_DOUBLE, &prim_value, CHECK_NULL);
   567     break;
   569   default:
   570     DEBUG_ONLY( tty->print_cr("*** %p: tag at CP[%d/%d] = %d",
   571                               this_oop(), index, cache_index, tag_value) );
   572     assert(false, "unexpected constant tag");
   573     break;
   574   }
   576   if (cache_index >= 0) {
   577     // Cache the oop here also.
   578     if (throw_exception.not_null()) {
   579       objArrayOop sys_array = oopFactory::new_system_objArray(1, CHECK_NULL);
   580       sys_array->obj_at_put(0, throw_exception());
   581       result_oop = sys_array;
   582       throw_exception = Handle();  // be tidy
   583     }
   584     Handle result_handle(THREAD, result_oop);
   585     result_oop = NULL;  // safety
   586     ObjectLocker ol(this_oop, THREAD);
   587     ConstantPoolCacheEntry* cpc_entry = this_oop->cache()->entry_at(cache_index);
   588     result_oop = cpc_entry->f1();
   589     // Benign race condition:  f1 may already be filled in while we were trying to lock.
   590     // The important thing here is that all threads pick up the same result.
   591     // It doesn't matter which racing thread wins, as long as only one
   592     // result is used by all threads, and all future queries.
   593     // That result may be either a resolved constant or a failure exception.
   594     if (result_oop == NULL) {
   595       result_oop = result_handle();
   596       cpc_entry->set_f1(result_oop);
   597     }
   598     return decode_exception_from_f1(result_oop, THREAD);
   599   } else {
   600     if (throw_exception.not_null()) {
   601       THROW_HANDLE_(throw_exception, NULL);
   602     }
   603     return result_oop;
   604   }
   605 }
   607 oop constantPoolOopDesc::string_at_impl(constantPoolHandle this_oop, int which, TRAPS) {
   608   oop str = NULL;
   609   CPSlot entry = this_oop->slot_at(which);
   610   if (entry.is_metadata()) {
   611     ObjectLocker ol(this_oop, THREAD);
   612     if (this_oop->tag_at(which).is_unresolved_string()) {
   613       // Intern string
   614       Symbol* sym = this_oop->unresolved_string_at(which);
   615       str = StringTable::intern(sym, CHECK_(constantPoolOop(NULL)));
   616       this_oop->string_at_put(which, str);
   617     } else {
   618       // Another thread beat us and interned string, read string from constant pool
   619       str = this_oop->resolved_string_at(which);
   620     }
   621   } else {
   622     str = entry.get_oop();
   623   }
   624   assert(java_lang_String::is_instance(str), "must be string");
   625   return str;
   626 }
   629 bool constantPoolOopDesc::is_pseudo_string_at(int which) {
   630   CPSlot entry = slot_at(which);
   631   if (entry.is_metadata())
   632     // Not yet resolved, but it will resolve to a string.
   633     return false;
   634   else if (java_lang_String::is_instance(entry.get_oop()))
   635     return false; // actually, it might be a non-interned or non-perm string
   636   else
   637     // truly pseudo
   638     return true;
   639 }
   642 bool constantPoolOopDesc::klass_name_at_matches(instanceKlassHandle k,
   643                                                 int which) {
   644   // Names are interned, so we can compare Symbol*s directly
   645   Symbol* cp_name = klass_name_at(which);
   646   return (cp_name == k->name());
   647 }
   650 int constantPoolOopDesc::pre_resolve_shared_klasses(TRAPS) {
   651   ResourceMark rm;
   652   int count = 0;
   653   for (int index = 1; index < tags()->length(); index++) { // Index 0 is unused
   654     if (tag_at(index).is_unresolved_string()) {
   655       // Intern string
   656       Symbol* sym = unresolved_string_at(index);
   657       oop entry = StringTable::intern(sym, CHECK_(-1));
   658       string_at_put(index, entry);
   659     }
   660   }
   661   return count;
   662 }
   664 // Iterate over symbols and decrement ones which are Symbol*s.
   665 // This is done during GC so do not need to lock constantPool unless we
   666 // have per-thread safepoints.
   667 // Only decrement the UTF8 symbols. Unresolved classes and strings point to
   668 // these symbols but didn't increment the reference count.
   669 void constantPoolOopDesc::unreference_symbols() {
   670   for (int index = 1; index < length(); index++) { // Index 0 is unused
   671     constantTag tag = tag_at(index);
   672     if (tag.is_symbol()) {
   673       symbol_at(index)->decrement_refcount();
   674     }
   675   }
   676 }
   678 // Iterate over symbols which are used as class, field, method names and
   679 // signatures (in preparation for writing to the shared archive).
   681 void constantPoolOopDesc::shared_symbols_iterate(SymbolClosure* closure) {
   682   for (int index = 1; index < length(); index++) { // Index 0 is unused
   683     switch (tag_at(index).value()) {
   685     case JVM_CONSTANT_UnresolvedClass:
   686     case JVM_CONSTANT_UnresolvedString:
   687     case JVM_CONSTANT_Utf8:
   688       assert(slot_at(index).is_metadata(), "must be symbol");
   689       closure->do_symbol(symbol_at_addr(index));
   690       break;
   692     case JVM_CONSTANT_NameAndType:
   693       {
   694         int i = *int_at_addr(index);
   695         closure->do_symbol(symbol_at_addr((unsigned)i >> 16));
   696         closure->do_symbol(symbol_at_addr((unsigned)i & 0xffff));
   697       }
   698       break;
   700     case JVM_CONSTANT_Class:
   701     case JVM_CONSTANT_InterfaceMethodref:
   702     case JVM_CONSTANT_Fieldref:
   703     case JVM_CONSTANT_Methodref:
   704     case JVM_CONSTANT_Integer:
   705     case JVM_CONSTANT_Float:
   706       // Do nothing!  Not an oop.
   707       // These constant types do not reference symbols at this point.
   708       break;
   710     case JVM_CONSTANT_String:
   711       // Do nothing!  Not a symbol.
   712       break;
   714     case JVM_CONSTANT_Long:
   715     case JVM_CONSTANT_Double:
   716       // Do nothing!  Not an oop. (But takes two pool entries.)
   717       ++index;
   718       break;
   720     default:
   721       ShouldNotReachHere();
   722       break;
   723     }
   724   }
   725 }
   728 // Iterate over the [one] tags array (in preparation for writing to the
   729 // shared archive).
   731 void constantPoolOopDesc::shared_tags_iterate(OopClosure* closure) {
   732   closure->do_oop(tags_addr());
   733   closure->do_oop(operands_addr());
   734 }
   737 // Iterate over String objects (in preparation for writing to the shared
   738 // archive).
   740 void constantPoolOopDesc::shared_strings_iterate(OopClosure* closure) {
   741   for (int index = 1; index < length(); index++) { // Index 0 is unused
   742     switch (tag_at(index).value()) {
   744     case JVM_CONSTANT_UnresolvedClass:
   745     case JVM_CONSTANT_NameAndType:
   746       // Do nothing!  Not a String.
   747       break;
   749     case JVM_CONSTANT_Class:
   750     case JVM_CONSTANT_InterfaceMethodref:
   751     case JVM_CONSTANT_Fieldref:
   752     case JVM_CONSTANT_Methodref:
   753     case JVM_CONSTANT_Integer:
   754     case JVM_CONSTANT_Float:
   755       // Do nothing!  Not an oop.
   756       // These constant types do not reference symbols at this point.
   757       break;
   759     case JVM_CONSTANT_String:
   760       closure->do_oop(obj_at_addr_raw(index));
   761       break;
   763     case JVM_CONSTANT_UnresolvedString:
   764     case JVM_CONSTANT_Utf8:
   765       // These constants are symbols, but unless these symbols are
   766       // actually to be used for something, we don't want to mark them.
   767       break;
   769     case JVM_CONSTANT_Long:
   770     case JVM_CONSTANT_Double:
   771       // Do nothing!  Not an oop. (But takes two pool entries.)
   772       ++index;
   773       break;
   775     default:
   776       ShouldNotReachHere();
   777       break;
   778     }
   779   }
   780 }
   783 // Compare this constant pool's entry at index1 to the constant pool
   784 // cp2's entry at index2.
   785 bool constantPoolOopDesc::compare_entry_to(int index1, constantPoolHandle cp2,
   786        int index2, TRAPS) {
   788   jbyte t1 = tag_at(index1).value();
   789   jbyte t2 = cp2->tag_at(index2).value();
   792   // JVM_CONSTANT_UnresolvedClassInError is equal to JVM_CONSTANT_UnresolvedClass
   793   // when comparing
   794   if (t1 == JVM_CONSTANT_UnresolvedClassInError) {
   795     t1 = JVM_CONSTANT_UnresolvedClass;
   796   }
   797   if (t2 == JVM_CONSTANT_UnresolvedClassInError) {
   798     t2 = JVM_CONSTANT_UnresolvedClass;
   799   }
   801   if (t1 != t2) {
   802     // Not the same entry type so there is nothing else to check. Note
   803     // that this style of checking will consider resolved/unresolved
   804     // class pairs and resolved/unresolved string pairs as different.
   805     // From the constantPoolOop API point of view, this is correct
   806     // behavior. See constantPoolKlass::merge() to see how this plays
   807     // out in the context of constantPoolOop merging.
   808     return false;
   809   }
   811   switch (t1) {
   812   case JVM_CONSTANT_Class:
   813   {
   814     klassOop k1 = klass_at(index1, CHECK_false);
   815     klassOop k2 = cp2->klass_at(index2, CHECK_false);
   816     if (k1 == k2) {
   817       return true;
   818     }
   819   } break;
   821   case JVM_CONSTANT_ClassIndex:
   822   {
   823     int recur1 = klass_index_at(index1);
   824     int recur2 = cp2->klass_index_at(index2);
   825     bool match = compare_entry_to(recur1, cp2, recur2, CHECK_false);
   826     if (match) {
   827       return true;
   828     }
   829   } break;
   831   case JVM_CONSTANT_Double:
   832   {
   833     jdouble d1 = double_at(index1);
   834     jdouble d2 = cp2->double_at(index2);
   835     if (d1 == d2) {
   836       return true;
   837     }
   838   } break;
   840   case JVM_CONSTANT_Fieldref:
   841   case JVM_CONSTANT_InterfaceMethodref:
   842   case JVM_CONSTANT_Methodref:
   843   {
   844     int recur1 = uncached_klass_ref_index_at(index1);
   845     int recur2 = cp2->uncached_klass_ref_index_at(index2);
   846     bool match = compare_entry_to(recur1, cp2, recur2, CHECK_false);
   847     if (match) {
   848       recur1 = uncached_name_and_type_ref_index_at(index1);
   849       recur2 = cp2->uncached_name_and_type_ref_index_at(index2);
   850       match = compare_entry_to(recur1, cp2, recur2, CHECK_false);
   851       if (match) {
   852         return true;
   853       }
   854     }
   855   } break;
   857   case JVM_CONSTANT_Float:
   858   {
   859     jfloat f1 = float_at(index1);
   860     jfloat f2 = cp2->float_at(index2);
   861     if (f1 == f2) {
   862       return true;
   863     }
   864   } break;
   866   case JVM_CONSTANT_Integer:
   867   {
   868     jint i1 = int_at(index1);
   869     jint i2 = cp2->int_at(index2);
   870     if (i1 == i2) {
   871       return true;
   872     }
   873   } break;
   875   case JVM_CONSTANT_Long:
   876   {
   877     jlong l1 = long_at(index1);
   878     jlong l2 = cp2->long_at(index2);
   879     if (l1 == l2) {
   880       return true;
   881     }
   882   } break;
   884   case JVM_CONSTANT_NameAndType:
   885   {
   886     int recur1 = name_ref_index_at(index1);
   887     int recur2 = cp2->name_ref_index_at(index2);
   888     bool match = compare_entry_to(recur1, cp2, recur2, CHECK_false);
   889     if (match) {
   890       recur1 = signature_ref_index_at(index1);
   891       recur2 = cp2->signature_ref_index_at(index2);
   892       match = compare_entry_to(recur1, cp2, recur2, CHECK_false);
   893       if (match) {
   894         return true;
   895       }
   896     }
   897   } break;
   899   case JVM_CONSTANT_String:
   900   {
   901     oop s1 = string_at(index1, CHECK_false);
   902     oop s2 = cp2->string_at(index2, CHECK_false);
   903     if (s1 == s2) {
   904       return true;
   905     }
   906   } break;
   908   case JVM_CONSTANT_StringIndex:
   909   {
   910     int recur1 = string_index_at(index1);
   911     int recur2 = cp2->string_index_at(index2);
   912     bool match = compare_entry_to(recur1, cp2, recur2, CHECK_false);
   913     if (match) {
   914       return true;
   915     }
   916   } break;
   918   case JVM_CONSTANT_UnresolvedClass:
   919   {
   920     Symbol* k1 = unresolved_klass_at(index1);
   921     Symbol* k2 = cp2->unresolved_klass_at(index2);
   922     if (k1 == k2) {
   923       return true;
   924     }
   925   } break;
   927   case JVM_CONSTANT_MethodType:
   928   {
   929     int k1 = method_type_index_at(index1);
   930     int k2 = cp2->method_type_index_at(index2);
   931     bool match = compare_entry_to(k1, cp2, k2, CHECK_false);
   932     if (match) {
   933       return true;
   934     }
   935   } break;
   937   case JVM_CONSTANT_MethodHandle:
   938   {
   939     int k1 = method_handle_ref_kind_at(index1);
   940     int k2 = cp2->method_handle_ref_kind_at(index2);
   941     if (k1 == k2) {
   942       int i1 = method_handle_index_at(index1);
   943       int i2 = cp2->method_handle_index_at(index2);
   944       bool match = compare_entry_to(i1, cp2, i2, CHECK_false);
   945       if (match) {
   946         return true;
   947       }
   948     }
   949   } break;
   951   case JVM_CONSTANT_InvokeDynamic:
   952   {
   953     int k1 = invoke_dynamic_bootstrap_method_ref_index_at(index1);
   954     int k2 = cp2->invoke_dynamic_bootstrap_method_ref_index_at(index2);
   955     bool match = compare_entry_to(k1, cp2, k2, CHECK_false);
   956     if (!match)  return false;
   957     k1 = invoke_dynamic_name_and_type_ref_index_at(index1);
   958     k2 = cp2->invoke_dynamic_name_and_type_ref_index_at(index2);
   959     match = compare_entry_to(k1, cp2, k2, CHECK_false);
   960     if (!match)  return false;
   961     int argc = invoke_dynamic_argument_count_at(index1);
   962     if (argc == cp2->invoke_dynamic_argument_count_at(index2)) {
   963       for (int j = 0; j < argc; j++) {
   964         k1 = invoke_dynamic_argument_index_at(index1, j);
   965         k2 = cp2->invoke_dynamic_argument_index_at(index2, j);
   966         match = compare_entry_to(k1, cp2, k2, CHECK_false);
   967         if (!match)  return false;
   968       }
   969       return true;           // got through loop; all elements equal
   970     }
   971   } break;
   973   case JVM_CONSTANT_UnresolvedString:
   974   {
   975     Symbol* s1 = unresolved_string_at(index1);
   976     Symbol* s2 = cp2->unresolved_string_at(index2);
   977     if (s1 == s2) {
   978       return true;
   979     }
   980   } break;
   982   case JVM_CONSTANT_Utf8:
   983   {
   984     Symbol* s1 = symbol_at(index1);
   985     Symbol* s2 = cp2->symbol_at(index2);
   986     if (s1 == s2) {
   987       return true;
   988     }
   989   } break;
   991   // Invalid is used as the tag for the second constant pool entry
   992   // occupied by JVM_CONSTANT_Double or JVM_CONSTANT_Long. It should
   993   // not be seen by itself.
   994   case JVM_CONSTANT_Invalid: // fall through
   996   default:
   997     ShouldNotReachHere();
   998     break;
   999   }
  1001   return false;
  1002 } // end compare_entry_to()
  1005 // Copy this constant pool's entries at start_i to end_i (inclusive)
  1006 // to the constant pool to_cp's entries starting at to_i. A total of
  1007 // (end_i - start_i) + 1 entries are copied.
  1008 void constantPoolOopDesc::copy_cp_to_impl(constantPoolHandle from_cp, int start_i, int end_i,
  1009        constantPoolHandle to_cp, int to_i, TRAPS) {
  1011   int dest_i = to_i;  // leave original alone for debug purposes
  1013   for (int src_i = start_i; src_i <= end_i; /* see loop bottom */ ) {
  1014     copy_entry_to(from_cp, src_i, to_cp, dest_i, CHECK);
  1016     switch (from_cp->tag_at(src_i).value()) {
  1017     case JVM_CONSTANT_Double:
  1018     case JVM_CONSTANT_Long:
  1019       // double and long take two constant pool entries
  1020       src_i += 2;
  1021       dest_i += 2;
  1022       break;
  1024     default:
  1025       // all others take one constant pool entry
  1026       src_i++;
  1027       dest_i++;
  1028       break;
  1032   int from_oplen = operand_array_length(from_cp->operands());
  1033   int old_oplen  = operand_array_length(to_cp->operands());
  1034   if (from_oplen != 0) {
  1035     // append my operands to the target's operands array
  1036     if (old_oplen == 0) {
  1037       to_cp->set_operands(from_cp->operands());  // reuse; do not merge
  1038     } else {
  1039       int old_len  = to_cp->operands()->length();
  1040       int from_len = from_cp->operands()->length();
  1041       int old_off  = old_oplen * sizeof(u2);
  1042       int from_off = from_oplen * sizeof(u2);
  1043       typeArrayHandle new_operands = oopFactory::new_permanent_shortArray(old_len + from_len, CHECK);
  1044       int fillp = 0, len = 0;
  1045       // first part of dest
  1046       Copy::conjoint_memory_atomic(to_cp->operands()->short_at_addr(0),
  1047                                    new_operands->short_at_addr(fillp),
  1048                                    (len = old_off) * sizeof(u2));
  1049       fillp += len;
  1050       // first part of src
  1051       Copy::conjoint_memory_atomic(to_cp->operands()->short_at_addr(0),
  1052                                    new_operands->short_at_addr(fillp),
  1053                                    (len = from_off) * sizeof(u2));
  1054       fillp += len;
  1055       // second part of dest
  1056       Copy::conjoint_memory_atomic(to_cp->operands()->short_at_addr(old_off),
  1057                                    new_operands->short_at_addr(fillp),
  1058                                    (len = old_len - old_off) * sizeof(u2));
  1059       fillp += len;
  1060       // second part of src
  1061       Copy::conjoint_memory_atomic(to_cp->operands()->short_at_addr(from_off),
  1062                                    new_operands->short_at_addr(fillp),
  1063                                    (len = from_len - from_off) * sizeof(u2));
  1064       fillp += len;
  1065       assert(fillp == new_operands->length(), "");
  1067       // Adjust indexes in the first part of the copied operands array.
  1068       for (int j = 0; j < from_oplen; j++) {
  1069         int offset = operand_offset_at(new_operands(), old_oplen + j);
  1070         assert(offset == operand_offset_at(from_cp->operands(), j), "correct copy");
  1071         offset += old_len;  // every new tuple is preceded by old_len extra u2's
  1072         operand_offset_at_put(new_operands(), old_oplen + j, offset);
  1075       // replace target operands array with combined array
  1076       to_cp->set_operands(new_operands());
  1080 } // end copy_cp_to()
  1083 // Copy this constant pool's entry at from_i to the constant pool
  1084 // to_cp's entry at to_i.
  1085 void constantPoolOopDesc::copy_entry_to(constantPoolHandle from_cp, int from_i,
  1086                                         constantPoolHandle to_cp, int to_i,
  1087                                         TRAPS) {
  1089   int tag = from_cp->tag_at(from_i).value();
  1090   switch (tag) {
  1091   case JVM_CONSTANT_Class:
  1093     klassOop k = from_cp->klass_at(from_i, CHECK);
  1094     to_cp->klass_at_put(to_i, k);
  1095   } break;
  1097   case JVM_CONSTANT_ClassIndex:
  1099     jint ki = from_cp->klass_index_at(from_i);
  1100     to_cp->klass_index_at_put(to_i, ki);
  1101   } break;
  1103   case JVM_CONSTANT_Double:
  1105     jdouble d = from_cp->double_at(from_i);
  1106     to_cp->double_at_put(to_i, d);
  1107     // double takes two constant pool entries so init second entry's tag
  1108     to_cp->tag_at_put(to_i + 1, JVM_CONSTANT_Invalid);
  1109   } break;
  1111   case JVM_CONSTANT_Fieldref:
  1113     int class_index = from_cp->uncached_klass_ref_index_at(from_i);
  1114     int name_and_type_index = from_cp->uncached_name_and_type_ref_index_at(from_i);
  1115     to_cp->field_at_put(to_i, class_index, name_and_type_index);
  1116   } break;
  1118   case JVM_CONSTANT_Float:
  1120     jfloat f = from_cp->float_at(from_i);
  1121     to_cp->float_at_put(to_i, f);
  1122   } break;
  1124   case JVM_CONSTANT_Integer:
  1126     jint i = from_cp->int_at(from_i);
  1127     to_cp->int_at_put(to_i, i);
  1128   } break;
  1130   case JVM_CONSTANT_InterfaceMethodref:
  1132     int class_index = from_cp->uncached_klass_ref_index_at(from_i);
  1133     int name_and_type_index = from_cp->uncached_name_and_type_ref_index_at(from_i);
  1134     to_cp->interface_method_at_put(to_i, class_index, name_and_type_index);
  1135   } break;
  1137   case JVM_CONSTANT_Long:
  1139     jlong l = from_cp->long_at(from_i);
  1140     to_cp->long_at_put(to_i, l);
  1141     // long takes two constant pool entries so init second entry's tag
  1142     to_cp->tag_at_put(to_i + 1, JVM_CONSTANT_Invalid);
  1143   } break;
  1145   case JVM_CONSTANT_Methodref:
  1147     int class_index = from_cp->uncached_klass_ref_index_at(from_i);
  1148     int name_and_type_index = from_cp->uncached_name_and_type_ref_index_at(from_i);
  1149     to_cp->method_at_put(to_i, class_index, name_and_type_index);
  1150   } break;
  1152   case JVM_CONSTANT_NameAndType:
  1154     int name_ref_index = from_cp->name_ref_index_at(from_i);
  1155     int signature_ref_index = from_cp->signature_ref_index_at(from_i);
  1156     to_cp->name_and_type_at_put(to_i, name_ref_index, signature_ref_index);
  1157   } break;
  1159   case JVM_CONSTANT_String:
  1161     oop s = from_cp->string_at(from_i, CHECK);
  1162     to_cp->string_at_put(to_i, s);
  1163   } break;
  1165   case JVM_CONSTANT_StringIndex:
  1167     jint si = from_cp->string_index_at(from_i);
  1168     to_cp->string_index_at_put(to_i, si);
  1169   } break;
  1171   case JVM_CONSTANT_UnresolvedClass:
  1173     // Can be resolved after checking tag, so check the slot first.
  1174     CPSlot entry = from_cp->slot_at(from_i);
  1175     if (entry.is_oop()) {
  1176       assert(entry.get_oop()->is_klass(), "must be");
  1177       // Already resolved
  1178       to_cp->klass_at_put(to_i, (klassOop)entry.get_oop());
  1179     } else {
  1180       to_cp->unresolved_klass_at_put(to_i, entry.get_symbol());
  1182   } break;
  1184   case JVM_CONSTANT_UnresolvedClassInError:
  1186     Symbol* k = from_cp->unresolved_klass_at(from_i);
  1187     to_cp->unresolved_klass_at_put(to_i, k);
  1188     to_cp->tag_at_put(to_i, JVM_CONSTANT_UnresolvedClassInError);
  1189   } break;
  1192   case JVM_CONSTANT_UnresolvedString:
  1194     // Can be resolved after checking tag, so check the slot first.
  1195     CPSlot entry = from_cp->slot_at(from_i);
  1196     if (entry.is_oop()) {
  1197       // Already resolved (either string or pseudo-string)
  1198       to_cp->string_at_put(to_i, entry.get_oop());
  1199     } else {
  1200       to_cp->unresolved_string_at_put(to_i, entry.get_symbol());
  1202   } break;
  1204   case JVM_CONSTANT_Utf8:
  1206     Symbol* s = from_cp->symbol_at(from_i);
  1207     to_cp->symbol_at_put(to_i, s);
  1208     // This constantPool has the same lifetime as the original, so don't
  1209     // increase reference counts for the copy.
  1210   } break;
  1212   case JVM_CONSTANT_MethodType:
  1214     jint k = from_cp->method_type_index_at(from_i);
  1215     to_cp->method_type_index_at_put(to_i, k);
  1216   } break;
  1218   case JVM_CONSTANT_MethodHandle:
  1220     int k1 = from_cp->method_handle_ref_kind_at(from_i);
  1221     int k2 = from_cp->method_handle_index_at(from_i);
  1222     to_cp->method_handle_index_at_put(to_i, k1, k2);
  1223   } break;
  1225   case JVM_CONSTANT_InvokeDynamic:
  1227     int k1 = from_cp->invoke_dynamic_bootstrap_specifier_index(from_i);
  1228     int k2 = from_cp->invoke_dynamic_name_and_type_ref_index_at(from_i);
  1229     k1 += operand_array_length(to_cp->operands());  // to_cp might already have operands
  1230     to_cp->invoke_dynamic_at_put(to_i, k1, k2);
  1231   } break;
  1233   // Invalid is used as the tag for the second constant pool entry
  1234   // occupied by JVM_CONSTANT_Double or JVM_CONSTANT_Long. It should
  1235   // not be seen by itself.
  1236   case JVM_CONSTANT_Invalid: // fall through
  1238   default:
  1240     ShouldNotReachHere();
  1241   } break;
  1243 } // end copy_entry_to()
  1246 // Search constant pool search_cp for an entry that matches this
  1247 // constant pool's entry at pattern_i. Returns the index of a
  1248 // matching entry or zero (0) if there is no matching entry.
  1249 int constantPoolOopDesc::find_matching_entry(int pattern_i,
  1250       constantPoolHandle search_cp, TRAPS) {
  1252   // index zero (0) is not used
  1253   for (int i = 1; i < search_cp->length(); i++) {
  1254     bool found = compare_entry_to(pattern_i, search_cp, i, CHECK_0);
  1255     if (found) {
  1256       return i;
  1260   return 0;  // entry not found; return unused index zero (0)
  1261 } // end find_matching_entry()
  1264 #ifndef PRODUCT
  1266 const char* constantPoolOopDesc::printable_name_at(int which) {
  1268   constantTag tag = tag_at(which);
  1270   if (tag.is_unresolved_string() || tag.is_string()) {
  1271     return string_at_noresolve(which);
  1272   } else if (tag.is_klass() || tag.is_unresolved_klass()) {
  1273     return klass_name_at(which)->as_C_string();
  1274   } else if (tag.is_symbol()) {
  1275     return symbol_at(which)->as_C_string();
  1277   return "";
  1280 #endif // PRODUCT
  1283 // JVMTI GetConstantPool support
  1285 // For temporary use until code is stable.
  1286 #define DBG(code)
  1288 static const char* WARN_MSG = "Must not be such entry!";
  1290 static void print_cpool_bytes(jint cnt, u1 *bytes) {
  1291   jint size = 0;
  1292   u2   idx1, idx2;
  1294   for (jint idx = 1; idx < cnt; idx++) {
  1295     jint ent_size = 0;
  1296     u1   tag  = *bytes++;
  1297     size++;                       // count tag
  1299     printf("const #%03d, tag: %02d ", idx, tag);
  1300     switch(tag) {
  1301       case JVM_CONSTANT_Invalid: {
  1302         printf("Invalid");
  1303         break;
  1305       case JVM_CONSTANT_Unicode: {
  1306         printf("Unicode      %s", WARN_MSG);
  1307         break;
  1309       case JVM_CONSTANT_Utf8: {
  1310         u2 len = Bytes::get_Java_u2(bytes);
  1311         char str[128];
  1312         if (len > 127) {
  1313            len = 127;
  1315         strncpy(str, (char *) (bytes+2), len);
  1316         str[len] = '\0';
  1317         printf("Utf8          \"%s\"", str);
  1318         ent_size = 2 + len;
  1319         break;
  1321       case JVM_CONSTANT_Integer: {
  1322         u4 val = Bytes::get_Java_u4(bytes);
  1323         printf("int          %d", *(int *) &val);
  1324         ent_size = 4;
  1325         break;
  1327       case JVM_CONSTANT_Float: {
  1328         u4 val = Bytes::get_Java_u4(bytes);
  1329         printf("float        %5.3ff", *(float *) &val);
  1330         ent_size = 4;
  1331         break;
  1333       case JVM_CONSTANT_Long: {
  1334         u8 val = Bytes::get_Java_u8(bytes);
  1335         printf("long         "INT64_FORMAT, *(jlong *) &val);
  1336         ent_size = 8;
  1337         idx++; // Long takes two cpool slots
  1338         break;
  1340       case JVM_CONSTANT_Double: {
  1341         u8 val = Bytes::get_Java_u8(bytes);
  1342         printf("double       %5.3fd", *(jdouble *)&val);
  1343         ent_size = 8;
  1344         idx++; // Double takes two cpool slots
  1345         break;
  1347       case JVM_CONSTANT_Class: {
  1348         idx1 = Bytes::get_Java_u2(bytes);
  1349         printf("class        #%03d", idx1);
  1350         ent_size = 2;
  1351         break;
  1353       case JVM_CONSTANT_String: {
  1354         idx1 = Bytes::get_Java_u2(bytes);
  1355         printf("String       #%03d", idx1);
  1356         ent_size = 2;
  1357         break;
  1359       case JVM_CONSTANT_Fieldref: {
  1360         idx1 = Bytes::get_Java_u2(bytes);
  1361         idx2 = Bytes::get_Java_u2(bytes+2);
  1362         printf("Field        #%03d, #%03d", (int) idx1, (int) idx2);
  1363         ent_size = 4;
  1364         break;
  1366       case JVM_CONSTANT_Methodref: {
  1367         idx1 = Bytes::get_Java_u2(bytes);
  1368         idx2 = Bytes::get_Java_u2(bytes+2);
  1369         printf("Method       #%03d, #%03d", idx1, idx2);
  1370         ent_size = 4;
  1371         break;
  1373       case JVM_CONSTANT_InterfaceMethodref: {
  1374         idx1 = Bytes::get_Java_u2(bytes);
  1375         idx2 = Bytes::get_Java_u2(bytes+2);
  1376         printf("InterfMethod #%03d, #%03d", idx1, idx2);
  1377         ent_size = 4;
  1378         break;
  1380       case JVM_CONSTANT_NameAndType: {
  1381         idx1 = Bytes::get_Java_u2(bytes);
  1382         idx2 = Bytes::get_Java_u2(bytes+2);
  1383         printf("NameAndType  #%03d, #%03d", idx1, idx2);
  1384         ent_size = 4;
  1385         break;
  1387       case JVM_CONSTANT_ClassIndex: {
  1388         printf("ClassIndex  %s", WARN_MSG);
  1389         break;
  1391       case JVM_CONSTANT_UnresolvedClass: {
  1392         printf("UnresolvedClass: %s", WARN_MSG);
  1393         break;
  1395       case JVM_CONSTANT_UnresolvedClassInError: {
  1396         printf("UnresolvedClassInErr: %s", WARN_MSG);
  1397         break;
  1399       case JVM_CONSTANT_StringIndex: {
  1400         printf("StringIndex: %s", WARN_MSG);
  1401         break;
  1403       case JVM_CONSTANT_UnresolvedString: {
  1404         printf("UnresolvedString: %s", WARN_MSG);
  1405         break;
  1408     printf(";\n");
  1409     bytes += ent_size;
  1410     size  += ent_size;
  1412   printf("Cpool size: %d\n", size);
  1413   fflush(0);
  1414   return;
  1415 } /* end print_cpool_bytes */
  1418 // Returns size of constant pool entry.
  1419 jint constantPoolOopDesc::cpool_entry_size(jint idx) {
  1420   switch(tag_at(idx).value()) {
  1421     case JVM_CONSTANT_Invalid:
  1422     case JVM_CONSTANT_Unicode:
  1423       return 1;
  1425     case JVM_CONSTANT_Utf8:
  1426       return 3 + symbol_at(idx)->utf8_length();
  1428     case JVM_CONSTANT_Class:
  1429     case JVM_CONSTANT_String:
  1430     case JVM_CONSTANT_ClassIndex:
  1431     case JVM_CONSTANT_UnresolvedClass:
  1432     case JVM_CONSTANT_UnresolvedClassInError:
  1433     case JVM_CONSTANT_StringIndex:
  1434     case JVM_CONSTANT_UnresolvedString:
  1435     case JVM_CONSTANT_MethodType:
  1436       return 3;
  1438     case JVM_CONSTANT_MethodHandle:
  1439       return 4; //tag, ref_kind, ref_index
  1441     case JVM_CONSTANT_Integer:
  1442     case JVM_CONSTANT_Float:
  1443     case JVM_CONSTANT_Fieldref:
  1444     case JVM_CONSTANT_Methodref:
  1445     case JVM_CONSTANT_InterfaceMethodref:
  1446     case JVM_CONSTANT_NameAndType:
  1447       return 5;
  1449     case JVM_CONSTANT_InvokeDynamic:
  1450       // u1 tag, u2 bsm, u2 nt
  1451       return 5;
  1453     case JVM_CONSTANT_Long:
  1454     case JVM_CONSTANT_Double:
  1455       return 9;
  1457   assert(false, "cpool_entry_size: Invalid constant pool entry tag");
  1458   return 1;
  1459 } /* end cpool_entry_size */
  1462 // SymbolHashMap is used to find a constant pool index from a string.
  1463 // This function fills in SymbolHashMaps, one for utf8s and one for
  1464 // class names, returns size of the cpool raw bytes.
  1465 jint constantPoolOopDesc::hash_entries_to(SymbolHashMap *symmap,
  1466                                           SymbolHashMap *classmap) {
  1467   jint size = 0;
  1469   for (u2 idx = 1; idx < length(); idx++) {
  1470     u2 tag = tag_at(idx).value();
  1471     size += cpool_entry_size(idx);
  1473     switch(tag) {
  1474       case JVM_CONSTANT_Utf8: {
  1475         Symbol* sym = symbol_at(idx);
  1476         symmap->add_entry(sym, idx);
  1477         DBG(printf("adding symbol entry %s = %d\n", sym->as_utf8(), idx));
  1478         break;
  1480       case JVM_CONSTANT_Class:
  1481       case JVM_CONSTANT_UnresolvedClass:
  1482       case JVM_CONSTANT_UnresolvedClassInError: {
  1483         Symbol* sym = klass_name_at(idx);
  1484         classmap->add_entry(sym, idx);
  1485         DBG(printf("adding class entry %s = %d\n", sym->as_utf8(), idx));
  1486         break;
  1488       case JVM_CONSTANT_Long:
  1489       case JVM_CONSTANT_Double: {
  1490         idx++; // Both Long and Double take two cpool slots
  1491         break;
  1495   return size;
  1496 } /* end hash_utf8_entries_to */
  1499 // Copy cpool bytes.
  1500 // Returns:
  1501 //    0, in case of OutOfMemoryError
  1502 //   -1, in case of internal error
  1503 //  > 0, count of the raw cpool bytes that have been copied
  1504 int constantPoolOopDesc::copy_cpool_bytes(int cpool_size,
  1505                                           SymbolHashMap* tbl,
  1506                                           unsigned char *bytes) {
  1507   u2   idx1, idx2;
  1508   jint size  = 0;
  1509   jint cnt   = length();
  1510   unsigned char *start_bytes = bytes;
  1512   for (jint idx = 1; idx < cnt; idx++) {
  1513     u1   tag      = tag_at(idx).value();
  1514     jint ent_size = cpool_entry_size(idx);
  1516     assert(size + ent_size <= cpool_size, "Size mismatch");
  1518     *bytes = tag;
  1519     DBG(printf("#%03hd tag=%03hd, ", idx, tag));
  1520     switch(tag) {
  1521       case JVM_CONSTANT_Invalid: {
  1522         DBG(printf("JVM_CONSTANT_Invalid"));
  1523         break;
  1525       case JVM_CONSTANT_Unicode: {
  1526         assert(false, "Wrong constant pool tag: JVM_CONSTANT_Unicode");
  1527         DBG(printf("JVM_CONSTANT_Unicode"));
  1528         break;
  1530       case JVM_CONSTANT_Utf8: {
  1531         Symbol* sym = symbol_at(idx);
  1532         char*     str = sym->as_utf8();
  1533         // Warning! It's crashing on x86 with len = sym->utf8_length()
  1534         int       len = (int) strlen(str);
  1535         Bytes::put_Java_u2((address) (bytes+1), (u2) len);
  1536         for (int i = 0; i < len; i++) {
  1537             bytes[3+i] = (u1) str[i];
  1539         DBG(printf("JVM_CONSTANT_Utf8: %s ", str));
  1540         break;
  1542       case JVM_CONSTANT_Integer: {
  1543         jint val = int_at(idx);
  1544         Bytes::put_Java_u4((address) (bytes+1), *(u4*)&val);
  1545         break;
  1547       case JVM_CONSTANT_Float: {
  1548         jfloat val = float_at(idx);
  1549         Bytes::put_Java_u4((address) (bytes+1), *(u4*)&val);
  1550         break;
  1552       case JVM_CONSTANT_Long: {
  1553         jlong val = long_at(idx);
  1554         Bytes::put_Java_u8((address) (bytes+1), *(u8*)&val);
  1555         idx++;             // Long takes two cpool slots
  1556         break;
  1558       case JVM_CONSTANT_Double: {
  1559         jdouble val = double_at(idx);
  1560         Bytes::put_Java_u8((address) (bytes+1), *(u8*)&val);
  1561         idx++;             // Double takes two cpool slots
  1562         break;
  1564       case JVM_CONSTANT_Class:
  1565       case JVM_CONSTANT_UnresolvedClass:
  1566       case JVM_CONSTANT_UnresolvedClassInError: {
  1567         *bytes = JVM_CONSTANT_Class;
  1568         Symbol* sym = klass_name_at(idx);
  1569         idx1 = tbl->symbol_to_value(sym);
  1570         assert(idx1 != 0, "Have not found a hashtable entry");
  1571         Bytes::put_Java_u2((address) (bytes+1), idx1);
  1572         DBG(printf("JVM_CONSTANT_Class: idx=#%03hd, %s", idx1, sym->as_utf8()));
  1573         break;
  1575       case JVM_CONSTANT_String: {
  1576         unsigned int hash;
  1577         char *str = string_at_noresolve(idx);
  1578         TempNewSymbol sym = SymbolTable::lookup_only(str, (int) strlen(str), hash);
  1579         if (sym == NULL) {
  1580           // sym can be NULL if string refers to incorrectly encoded JVM_CONSTANT_Utf8
  1581           // this can happen with JVM TI; see CR 6839599 for more details
  1582           oop string = *(obj_at_addr_raw(idx));
  1583           assert(java_lang_String::is_instance(string),"Not a String");
  1584           DBG(printf("Error #%03hd tag=%03hd\n", idx, tag));
  1585           idx1 = 0;
  1586           for (int j = 0; j < tbl->table_size() && idx1 == 0; j++) {
  1587             for (SymbolHashMapEntry* cur = tbl->bucket(j); cur != NULL; cur = cur->next()) {
  1588               int length;
  1589               Symbol* s = cur->symbol();
  1590               jchar* chars = s->as_unicode(length);
  1591               if (java_lang_String::equals(string, chars, length)) {
  1592                 idx1 = cur->value();
  1593                 DBG(printf("Index found: %d\n",idx1));
  1594                 break;
  1598         } else {
  1599           idx1 = tbl->symbol_to_value(sym);
  1601         assert(idx1 != 0, "Have not found a hashtable entry");
  1602         Bytes::put_Java_u2((address) (bytes+1), idx1);
  1603         DBG(printf("JVM_CONSTANT_String: idx=#%03hd, %s", idx1, str));
  1604         break;
  1606       case JVM_CONSTANT_UnresolvedString: {
  1607         *bytes = JVM_CONSTANT_String;
  1608         Symbol* sym = unresolved_string_at(idx);
  1609         idx1 = tbl->symbol_to_value(sym);
  1610         assert(idx1 != 0, "Have not found a hashtable entry");
  1611         Bytes::put_Java_u2((address) (bytes+1), idx1);
  1612         DBG(char *str = sym->as_utf8());
  1613         DBG(printf("JVM_CONSTANT_UnresolvedString: idx=#%03hd, %s", idx1, str));
  1614         break;
  1616       case JVM_CONSTANT_Fieldref:
  1617       case JVM_CONSTANT_Methodref:
  1618       case JVM_CONSTANT_InterfaceMethodref: {
  1619         idx1 = uncached_klass_ref_index_at(idx);
  1620         idx2 = uncached_name_and_type_ref_index_at(idx);
  1621         Bytes::put_Java_u2((address) (bytes+1), idx1);
  1622         Bytes::put_Java_u2((address) (bytes+3), idx2);
  1623         DBG(printf("JVM_CONSTANT_Methodref: %hd %hd", idx1, idx2));
  1624         break;
  1626       case JVM_CONSTANT_NameAndType: {
  1627         idx1 = name_ref_index_at(idx);
  1628         idx2 = signature_ref_index_at(idx);
  1629         Bytes::put_Java_u2((address) (bytes+1), idx1);
  1630         Bytes::put_Java_u2((address) (bytes+3), idx2);
  1631         DBG(printf("JVM_CONSTANT_NameAndType: %hd %hd", idx1, idx2));
  1632         break;
  1634       case JVM_CONSTANT_ClassIndex: {
  1635         *bytes = JVM_CONSTANT_Class;
  1636         idx1 = klass_index_at(idx);
  1637         Bytes::put_Java_u2((address) (bytes+1), idx1);
  1638         DBG(printf("JVM_CONSTANT_ClassIndex: %hd", idx1));
  1639         break;
  1641       case JVM_CONSTANT_StringIndex: {
  1642         *bytes = JVM_CONSTANT_String;
  1643         idx1 = string_index_at(idx);
  1644         Bytes::put_Java_u2((address) (bytes+1), idx1);
  1645         DBG(printf("JVM_CONSTANT_StringIndex: %hd", idx1));
  1646         break;
  1648       case JVM_CONSTANT_MethodHandle: {
  1649         *bytes = JVM_CONSTANT_MethodHandle;
  1650         int kind = method_handle_ref_kind_at(idx);
  1651         idx1 = method_handle_index_at(idx);
  1652         *(bytes+1) = (unsigned char) kind;
  1653         Bytes::put_Java_u2((address) (bytes+2), idx1);
  1654         DBG(printf("JVM_CONSTANT_MethodHandle: %d %hd", kind, idx1));
  1655         break;
  1657       case JVM_CONSTANT_MethodType: {
  1658         *bytes = JVM_CONSTANT_MethodType;
  1659         idx1 = method_type_index_at(idx);
  1660         Bytes::put_Java_u2((address) (bytes+1), idx1);
  1661         DBG(printf("JVM_CONSTANT_MethodType: %hd", idx1));
  1662         break;
  1664       case JVM_CONSTANT_InvokeDynamic: {
  1665         *bytes = tag;
  1666         idx1 = extract_low_short_from_int(*int_at_addr(idx));
  1667         idx2 = extract_high_short_from_int(*int_at_addr(idx));
  1668         assert(idx2 == invoke_dynamic_name_and_type_ref_index_at(idx), "correct half of u4");
  1669         Bytes::put_Java_u2((address) (bytes+1), idx1);
  1670         Bytes::put_Java_u2((address) (bytes+3), idx2);
  1671         DBG(printf("JVM_CONSTANT_InvokeDynamic: %hd %hd", idx1, idx2));
  1672         break;
  1675     DBG(printf("\n"));
  1676     bytes += ent_size;
  1677     size  += ent_size;
  1679   assert(size == cpool_size, "Size mismatch");
  1681   // Keep temorarily for debugging until it's stable.
  1682   DBG(print_cpool_bytes(cnt, start_bytes));
  1683   return (int)(bytes - start_bytes);
  1684 } /* end copy_cpool_bytes */
  1687 void SymbolHashMap::add_entry(Symbol* sym, u2 value) {
  1688   char *str = sym->as_utf8();
  1689   unsigned int hash = compute_hash(str, sym->utf8_length());
  1690   unsigned int index = hash % table_size();
  1692   // check if already in map
  1693   // we prefer the first entry since it is more likely to be what was used in
  1694   // the class file
  1695   for (SymbolHashMapEntry *en = bucket(index); en != NULL; en = en->next()) {
  1696     assert(en->symbol() != NULL, "SymbolHashMapEntry symbol is NULL");
  1697     if (en->hash() == hash && en->symbol() == sym) {
  1698         return;  // already there
  1702   SymbolHashMapEntry* entry = new SymbolHashMapEntry(hash, sym, value);
  1703   entry->set_next(bucket(index));
  1704   _buckets[index].set_entry(entry);
  1705   assert(entry->symbol() != NULL, "SymbolHashMapEntry symbol is NULL");
  1708 SymbolHashMapEntry* SymbolHashMap::find_entry(Symbol* sym) {
  1709   assert(sym != NULL, "SymbolHashMap::find_entry - symbol is NULL");
  1710   char *str = sym->as_utf8();
  1711   int   len = sym->utf8_length();
  1712   unsigned int hash = SymbolHashMap::compute_hash(str, len);
  1713   unsigned int index = hash % table_size();
  1714   for (SymbolHashMapEntry *en = bucket(index); en != NULL; en = en->next()) {
  1715     assert(en->symbol() != NULL, "SymbolHashMapEntry symbol is NULL");
  1716     if (en->hash() == hash && en->symbol() == sym) {
  1717       return en;
  1720   return NULL;

mercurial