src/share/vm/oops/constantPool.cpp

Tue, 26 Nov 2013 14:35:38 +0100

author
sjohanss
date
Tue, 26 Nov 2013 14:35:38 +0100
changeset 6148
55a0da3d420b
parent 5971
b8860472c377
child 6307
10c9507f544a
permissions
-rw-r--r--

8027675: Full collections with Serial slower in JDK 8 compared to 7u40
Summary: Reduced the number of calls to follow_class_loader and instead marked and pushed the klass holder directly. Also removed unneeded calls to adjust_klass.
Reviewed-by: coleenp, jmasa, mgerdin, tschatzl

     1 /*
     2  * Copyright (c) 1997, 2013, 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/classLoaderData.hpp"
    27 #include "classfile/javaClasses.hpp"
    28 #include "classfile/metadataOnStackMark.hpp"
    29 #include "classfile/symbolTable.hpp"
    30 #include "classfile/systemDictionary.hpp"
    31 #include "classfile/vmSymbols.hpp"
    32 #include "interpreter/linkResolver.hpp"
    33 #include "memory/heapInspection.hpp"
    34 #include "memory/metadataFactory.hpp"
    35 #include "memory/oopFactory.hpp"
    36 #include "oops/constantPool.hpp"
    37 #include "oops/instanceKlass.hpp"
    38 #include "oops/objArrayKlass.hpp"
    39 #include "runtime/fieldType.hpp"
    40 #include "runtime/init.hpp"
    41 #include "runtime/javaCalls.hpp"
    42 #include "runtime/signature.hpp"
    43 #include "runtime/vframe.hpp"
    45 ConstantPool* ConstantPool::allocate(ClassLoaderData* loader_data, int length, TRAPS) {
    46   // Tags are RW but comment below applies to tags also.
    47   Array<u1>* tags = MetadataFactory::new_writeable_array<u1>(loader_data, length, 0, CHECK_NULL);
    49   int size = ConstantPool::size(length);
    51   // CDS considerations:
    52   // Allocate read-write but may be able to move to read-only at dumping time
    53   // if all the klasses are resolved.  The only other field that is writable is
    54   // the resolved_references array, which is recreated at startup time.
    55   // But that could be moved to InstanceKlass (although a pain to access from
    56   // assembly code).  Maybe it could be moved to the cpCache which is RW.
    57   return new (loader_data, size, false, MetaspaceObj::ConstantPoolType, THREAD) ConstantPool(tags);
    58 }
    60 ConstantPool::ConstantPool(Array<u1>* tags) {
    61   set_length(tags->length());
    62   set_tags(NULL);
    63   set_cache(NULL);
    64   set_reference_map(NULL);
    65   set_resolved_references(NULL);
    66   set_operands(NULL);
    67   set_pool_holder(NULL);
    68   set_flags(0);
    70   // only set to non-zero if constant pool is merged by RedefineClasses
    71   set_version(0);
    72   set_lock(new Monitor(Monitor::nonleaf + 2, "A constant pool lock"));
    74   // initialize tag array
    75   int length = tags->length();
    76   for (int index = 0; index < length; index++) {
    77     tags->at_put(index, JVM_CONSTANT_Invalid);
    78   }
    79   set_tags(tags);
    80 }
    82 void ConstantPool::deallocate_contents(ClassLoaderData* loader_data) {
    83   MetadataFactory::free_metadata(loader_data, cache());
    84   set_cache(NULL);
    85   MetadataFactory::free_array<jushort>(loader_data, operands());
    86   set_operands(NULL);
    88   release_C_heap_structures();
    90   // free tag array
    91   MetadataFactory::free_array<u1>(loader_data, tags());
    92   set_tags(NULL);
    93 }
    95 void ConstantPool::release_C_heap_structures() {
    96   // walk constant pool and decrement symbol reference counts
    97   unreference_symbols();
    99   delete _lock;
   100   set_lock(NULL);
   101 }
   103 objArrayOop ConstantPool::resolved_references() const {
   104   return (objArrayOop)JNIHandles::resolve(_resolved_references);
   105 }
   107 // Create resolved_references array and mapping array for original cp indexes
   108 // The ldc bytecode was rewritten to have the resolved reference array index so need a way
   109 // to map it back for resolving and some unlikely miscellaneous uses.
   110 // The objects created by invokedynamic are appended to this list.
   111 void ConstantPool::initialize_resolved_references(ClassLoaderData* loader_data,
   112                                                   intStack reference_map,
   113                                                   int constant_pool_map_length,
   114                                                   TRAPS) {
   115   // Initialized the resolved object cache.
   116   int map_length = reference_map.length();
   117   if (map_length > 0) {
   118     // Only need mapping back to constant pool entries.  The map isn't used for
   119     // invokedynamic resolved_reference entries.  For invokedynamic entries,
   120     // the constant pool cache index has the mapping back to both the constant
   121     // pool and to the resolved reference index.
   122     if (constant_pool_map_length > 0) {
   123       Array<u2>* om = MetadataFactory::new_array<u2>(loader_data, constant_pool_map_length, CHECK);
   125       for (int i = 0; i < constant_pool_map_length; i++) {
   126         int x = reference_map.at(i);
   127         assert(x == (int)(jushort) x, "klass index is too big");
   128         om->at_put(i, (jushort)x);
   129       }
   130       set_reference_map(om);
   131     }
   133     // Create Java array for holding resolved strings, methodHandles,
   134     // methodTypes, invokedynamic and invokehandle appendix objects, etc.
   135     objArrayOop stom = oopFactory::new_objArray(SystemDictionary::Object_klass(), map_length, CHECK);
   136     Handle refs_handle (THREAD, (oop)stom);  // must handleize.
   137     set_resolved_references(loader_data->add_handle(refs_handle));
   138   }
   139 }
   141 // CDS support. Create a new resolved_references array.
   142 void ConstantPool::restore_unshareable_info(TRAPS) {
   144   // restore the C++ vtable from the shared archive
   145   restore_vtable();
   147   if (SystemDictionary::Object_klass_loaded()) {
   148     // Recreate the object array and add to ClassLoaderData.
   149     int map_length = resolved_reference_length();
   150     if (map_length > 0) {
   151       objArrayOop stom = oopFactory::new_objArray(SystemDictionary::Object_klass(), map_length, CHECK);
   152       Handle refs_handle (THREAD, (oop)stom);  // must handleize.
   154       ClassLoaderData* loader_data = pool_holder()->class_loader_data();
   155       set_resolved_references(loader_data->add_handle(refs_handle));
   156     }
   158     // Also need to recreate the mutex.  Make sure this matches the constructor
   159     set_lock(new Monitor(Monitor::nonleaf + 2, "A constant pool lock"));
   160   }
   161 }
   163 void ConstantPool::remove_unshareable_info() {
   164   // Resolved references are not in the shared archive.
   165   // Save the length for restoration.  It is not necessarily the same length
   166   // as reference_map.length() if invokedynamic is saved.
   167   set_resolved_reference_length(
   168     resolved_references() != NULL ? resolved_references()->length() : 0);
   169   set_resolved_references(NULL);
   170   set_lock(NULL);
   171 }
   173 int ConstantPool::cp_to_object_index(int cp_index) {
   174   // this is harder don't do this so much.
   175   int i = reference_map()->find(cp_index);
   176   // We might not find the index for jsr292 call.
   177   return (i < 0) ? _no_index_sentinel : i;
   178 }
   180 Klass* ConstantPool::klass_at_impl(constantPoolHandle this_oop, int which, TRAPS) {
   181   // A resolved constantPool entry will contain a Klass*, otherwise a Symbol*.
   182   // It is not safe to rely on the tag bit's here, since we don't have a lock, and the entry and
   183   // tag is not updated atomicly.
   185   CPSlot entry = this_oop->slot_at(which);
   186   if (entry.is_resolved()) {
   187     assert(entry.get_klass()->is_klass(), "must be");
   188     // Already resolved - return entry.
   189     return entry.get_klass();
   190   }
   192   // Acquire lock on constant oop while doing update. After we get the lock, we check if another object
   193   // already has updated the object
   194   assert(THREAD->is_Java_thread(), "must be a Java thread");
   195   bool do_resolve = false;
   196   bool in_error = false;
   198   // Create a handle for the mirror. This will preserve the resolved class
   199   // until the loader_data is registered.
   200   Handle mirror_handle;
   202   Symbol* name = NULL;
   203   Handle       loader;
   204   {  MonitorLockerEx ml(this_oop->lock());
   206     if (this_oop->tag_at(which).is_unresolved_klass()) {
   207       if (this_oop->tag_at(which).is_unresolved_klass_in_error()) {
   208         in_error = true;
   209       } else {
   210         do_resolve = true;
   211         name   = this_oop->unresolved_klass_at(which);
   212         loader = Handle(THREAD, this_oop->pool_holder()->class_loader());
   213       }
   214     }
   215   } // unlocking constantPool
   218   // The original attempt to resolve this constant pool entry failed so find the
   219   // original error and throw it again (JVMS 5.4.3).
   220   if (in_error) {
   221     Symbol* error = SystemDictionary::find_resolution_error(this_oop, which);
   222     guarantee(error != (Symbol*)NULL, "tag mismatch with resolution error table");
   223     ResourceMark rm;
   224     // exception text will be the class name
   225     const char* className = this_oop->unresolved_klass_at(which)->as_C_string();
   226     THROW_MSG_0(error, className);
   227   }
   229   if (do_resolve) {
   230     // this_oop must be unlocked during resolve_or_fail
   231     oop protection_domain = this_oop->pool_holder()->protection_domain();
   232     Handle h_prot (THREAD, protection_domain);
   233     Klass* k_oop = SystemDictionary::resolve_or_fail(name, loader, h_prot, true, THREAD);
   234     KlassHandle k;
   235     if (!HAS_PENDING_EXCEPTION) {
   236       k = KlassHandle(THREAD, k_oop);
   237       // preserve the resolved klass.
   238       mirror_handle = Handle(THREAD, k_oop->java_mirror());
   239       // Do access check for klasses
   240       verify_constant_pool_resolve(this_oop, k, THREAD);
   241     }
   243     // Failed to resolve class. We must record the errors so that subsequent attempts
   244     // to resolve this constant pool entry fail with the same error (JVMS 5.4.3).
   245     if (HAS_PENDING_EXCEPTION) {
   246       ResourceMark rm;
   247       Symbol* error = PENDING_EXCEPTION->klass()->name();
   249       bool throw_orig_error = false;
   250       {
   251         MonitorLockerEx ml(this_oop->lock());
   253         // some other thread has beaten us and has resolved the class.
   254         if (this_oop->tag_at(which).is_klass()) {
   255           CLEAR_PENDING_EXCEPTION;
   256           entry = this_oop->resolved_klass_at(which);
   257           return entry.get_klass();
   258         }
   260         if (!PENDING_EXCEPTION->
   261               is_a(SystemDictionary::LinkageError_klass())) {
   262           // Just throw the exception and don't prevent these classes from
   263           // being loaded due to virtual machine errors like StackOverflow
   264           // and OutOfMemoryError, etc, or if the thread was hit by stop()
   265           // Needs clarification to section 5.4.3 of the VM spec (see 6308271)
   266         }
   267         else if (!this_oop->tag_at(which).is_unresolved_klass_in_error()) {
   268           SystemDictionary::add_resolution_error(this_oop, which, error);
   269           this_oop->tag_at_put(which, JVM_CONSTANT_UnresolvedClassInError);
   270         } else {
   271           // some other thread has put the class in error state.
   272           error = SystemDictionary::find_resolution_error(this_oop, which);
   273           assert(error != NULL, "checking");
   274           throw_orig_error = true;
   275         }
   276       } // unlocked
   278       if (throw_orig_error) {
   279         CLEAR_PENDING_EXCEPTION;
   280         ResourceMark rm;
   281         const char* className = this_oop->unresolved_klass_at(which)->as_C_string();
   282         THROW_MSG_0(error, className);
   283       }
   285       return 0;
   286     }
   288     if (TraceClassResolution && !k()->oop_is_array()) {
   289       // skip resolving the constant pool so that this code get's
   290       // called the next time some bytecodes refer to this class.
   291       ResourceMark rm;
   292       int line_number = -1;
   293       const char * source_file = NULL;
   294       if (JavaThread::current()->has_last_Java_frame()) {
   295         // try to identify the method which called this function.
   296         vframeStream vfst(JavaThread::current());
   297         if (!vfst.at_end()) {
   298           line_number = vfst.method()->line_number_from_bci(vfst.bci());
   299           Symbol* s = vfst.method()->method_holder()->source_file_name();
   300           if (s != NULL) {
   301             source_file = s->as_C_string();
   302           }
   303         }
   304       }
   305       if (k() != this_oop->pool_holder()) {
   306         // only print something if the classes are different
   307         if (source_file != NULL) {
   308           tty->print("RESOLVE %s %s %s:%d\n",
   309                      this_oop->pool_holder()->external_name(),
   310                      InstanceKlass::cast(k())->external_name(), source_file, line_number);
   311         } else {
   312           tty->print("RESOLVE %s %s\n",
   313                      this_oop->pool_holder()->external_name(),
   314                      InstanceKlass::cast(k())->external_name());
   315         }
   316       }
   317       return k();
   318     } else {
   319       MonitorLockerEx ml(this_oop->lock());
   320       // Only updated constant pool - if it is resolved.
   321       do_resolve = this_oop->tag_at(which).is_unresolved_klass();
   322       if (do_resolve) {
   323         ClassLoaderData* this_key = this_oop->pool_holder()->class_loader_data();
   324         this_key->record_dependency(k(), CHECK_NULL); // Can throw OOM
   325         this_oop->klass_at_put(which, k());
   326       }
   327     }
   328   }
   330   entry = this_oop->resolved_klass_at(which);
   331   assert(entry.is_resolved() && entry.get_klass()->is_klass(), "must be resolved at this point");
   332   return entry.get_klass();
   333 }
   336 // Does not update ConstantPool* - to avoid any exception throwing. Used
   337 // by compiler and exception handling.  Also used to avoid classloads for
   338 // instanceof operations. Returns NULL if the class has not been loaded or
   339 // if the verification of constant pool failed
   340 Klass* ConstantPool::klass_at_if_loaded(constantPoolHandle this_oop, int which) {
   341   CPSlot entry = this_oop->slot_at(which);
   342   if (entry.is_resolved()) {
   343     assert(entry.get_klass()->is_klass(), "must be");
   344     return entry.get_klass();
   345   } else {
   346     assert(entry.is_unresolved(), "must be either symbol or klass");
   347     Thread *thread = Thread::current();
   348     Symbol* name = entry.get_symbol();
   349     oop loader = this_oop->pool_holder()->class_loader();
   350     oop protection_domain = this_oop->pool_holder()->protection_domain();
   351     Handle h_prot (thread, protection_domain);
   352     Handle h_loader (thread, loader);
   353     Klass* k = SystemDictionary::find(name, h_loader, h_prot, thread);
   355     if (k != NULL) {
   356       // Make sure that resolving is legal
   357       EXCEPTION_MARK;
   358       KlassHandle klass(THREAD, k);
   359       // return NULL if verification fails
   360       verify_constant_pool_resolve(this_oop, klass, THREAD);
   361       if (HAS_PENDING_EXCEPTION) {
   362         CLEAR_PENDING_EXCEPTION;
   363         return NULL;
   364       }
   365       return klass();
   366     } else {
   367       return k;
   368     }
   369   }
   370 }
   373 Klass* ConstantPool::klass_ref_at_if_loaded(constantPoolHandle this_oop, int which) {
   374   return klass_at_if_loaded(this_oop, this_oop->klass_ref_index_at(which));
   375 }
   378 Method* ConstantPool::method_at_if_loaded(constantPoolHandle cpool,
   379                                                    int which) {
   380   if (cpool->cache() == NULL)  return NULL;  // nothing to load yet
   381   int cache_index = decode_cpcache_index(which, true);
   382   if (!(cache_index >= 0 && cache_index < cpool->cache()->length())) {
   383     // FIXME: should be an assert
   384     if (PrintMiscellaneous && (Verbose||WizardMode)) {
   385       tty->print_cr("bad operand %d in:", which); cpool->print();
   386     }
   387     return NULL;
   388   }
   389   ConstantPoolCacheEntry* e = cpool->cache()->entry_at(cache_index);
   390   return e->method_if_resolved(cpool);
   391 }
   394 bool ConstantPool::has_appendix_at_if_loaded(constantPoolHandle cpool, int which) {
   395   if (cpool->cache() == NULL)  return false;  // nothing to load yet
   396   int cache_index = decode_cpcache_index(which, true);
   397   ConstantPoolCacheEntry* e = cpool->cache()->entry_at(cache_index);
   398   return e->has_appendix();
   399 }
   401 oop ConstantPool::appendix_at_if_loaded(constantPoolHandle cpool, int which) {
   402   if (cpool->cache() == NULL)  return NULL;  // nothing to load yet
   403   int cache_index = decode_cpcache_index(which, true);
   404   ConstantPoolCacheEntry* e = cpool->cache()->entry_at(cache_index);
   405   return e->appendix_if_resolved(cpool);
   406 }
   409 bool ConstantPool::has_method_type_at_if_loaded(constantPoolHandle cpool, int which) {
   410   if (cpool->cache() == NULL)  return false;  // nothing to load yet
   411   int cache_index = decode_cpcache_index(which, true);
   412   ConstantPoolCacheEntry* e = cpool->cache()->entry_at(cache_index);
   413   return e->has_method_type();
   414 }
   416 oop ConstantPool::method_type_at_if_loaded(constantPoolHandle cpool, int which) {
   417   if (cpool->cache() == NULL)  return NULL;  // nothing to load yet
   418   int cache_index = decode_cpcache_index(which, true);
   419   ConstantPoolCacheEntry* e = cpool->cache()->entry_at(cache_index);
   420   return e->method_type_if_resolved(cpool);
   421 }
   424 Symbol* ConstantPool::impl_name_ref_at(int which, bool uncached) {
   425   int name_index = name_ref_index_at(impl_name_and_type_ref_index_at(which, uncached));
   426   return symbol_at(name_index);
   427 }
   430 Symbol* ConstantPool::impl_signature_ref_at(int which, bool uncached) {
   431   int signature_index = signature_ref_index_at(impl_name_and_type_ref_index_at(which, uncached));
   432   return symbol_at(signature_index);
   433 }
   436 int ConstantPool::impl_name_and_type_ref_index_at(int which, bool uncached) {
   437   int i = which;
   438   if (!uncached && cache() != NULL) {
   439     if (ConstantPool::is_invokedynamic_index(which)) {
   440       // Invokedynamic index is index into resolved_references
   441       int pool_index = invokedynamic_cp_cache_entry_at(which)->constant_pool_index();
   442       pool_index = invoke_dynamic_name_and_type_ref_index_at(pool_index);
   443       assert(tag_at(pool_index).is_name_and_type(), "");
   444       return pool_index;
   445     }
   446     // change byte-ordering and go via cache
   447     i = remap_instruction_operand_from_cache(which);
   448   } else {
   449     if (tag_at(which).is_invoke_dynamic()) {
   450       int pool_index = invoke_dynamic_name_and_type_ref_index_at(which);
   451       assert(tag_at(pool_index).is_name_and_type(), "");
   452       return pool_index;
   453     }
   454   }
   455   assert(tag_at(i).is_field_or_method(), "Corrupted constant pool");
   456   assert(!tag_at(i).is_invoke_dynamic(), "Must be handled above");
   457   jint ref_index = *int_at_addr(i);
   458   return extract_high_short_from_int(ref_index);
   459 }
   462 int ConstantPool::impl_klass_ref_index_at(int which, bool uncached) {
   463   guarantee(!ConstantPool::is_invokedynamic_index(which),
   464             "an invokedynamic instruction does not have a klass");
   465   int i = which;
   466   if (!uncached && cache() != NULL) {
   467     // change byte-ordering and go via cache
   468     i = remap_instruction_operand_from_cache(which);
   469   }
   470   assert(tag_at(i).is_field_or_method(), "Corrupted constant pool");
   471   jint ref_index = *int_at_addr(i);
   472   return extract_low_short_from_int(ref_index);
   473 }
   477 int ConstantPool::remap_instruction_operand_from_cache(int operand) {
   478   int cpc_index = operand;
   479   DEBUG_ONLY(cpc_index -= CPCACHE_INDEX_TAG);
   480   assert((int)(u2)cpc_index == cpc_index, "clean u2");
   481   int member_index = cache()->entry_at(cpc_index)->constant_pool_index();
   482   return member_index;
   483 }
   486 void ConstantPool::verify_constant_pool_resolve(constantPoolHandle this_oop, KlassHandle k, TRAPS) {
   487  if (k->oop_is_instance() || k->oop_is_objArray()) {
   488     instanceKlassHandle holder (THREAD, this_oop->pool_holder());
   489     Klass* elem_oop = k->oop_is_instance() ? k() : ObjArrayKlass::cast(k())->bottom_klass();
   490     KlassHandle element (THREAD, elem_oop);
   492     // The element type could be a typeArray - we only need the access check if it is
   493     // an reference to another class
   494     if (element->oop_is_instance()) {
   495       LinkResolver::check_klass_accessability(holder, element, CHECK);
   496     }
   497   }
   498 }
   501 int ConstantPool::name_ref_index_at(int which_nt) {
   502   jint ref_index = name_and_type_at(which_nt);
   503   return extract_low_short_from_int(ref_index);
   504 }
   507 int ConstantPool::signature_ref_index_at(int which_nt) {
   508   jint ref_index = name_and_type_at(which_nt);
   509   return extract_high_short_from_int(ref_index);
   510 }
   513 Klass* ConstantPool::klass_ref_at(int which, TRAPS) {
   514   return klass_at(klass_ref_index_at(which), CHECK_NULL);
   515 }
   518 Symbol* ConstantPool::klass_name_at(int which) {
   519   assert(tag_at(which).is_unresolved_klass() || tag_at(which).is_klass(),
   520          "Corrupted constant pool");
   521   // A resolved constantPool entry will contain a Klass*, otherwise a Symbol*.
   522   // It is not safe to rely on the tag bit's here, since we don't have a lock, and the entry and
   523   // tag is not updated atomicly.
   524   CPSlot entry = slot_at(which);
   525   if (entry.is_resolved()) {
   526     // Already resolved - return entry's name.
   527     assert(entry.get_klass()->is_klass(), "must be");
   528     return entry.get_klass()->name();
   529   } else {
   530     assert(entry.is_unresolved(), "must be either symbol or klass");
   531     return entry.get_symbol();
   532   }
   533 }
   535 Symbol* ConstantPool::klass_ref_at_noresolve(int which) {
   536   jint ref_index = klass_ref_index_at(which);
   537   return klass_at_noresolve(ref_index);
   538 }
   540 Symbol* ConstantPool::uncached_klass_ref_at_noresolve(int which) {
   541   jint ref_index = uncached_klass_ref_index_at(which);
   542   return klass_at_noresolve(ref_index);
   543 }
   545 char* ConstantPool::string_at_noresolve(int which) {
   546   Symbol* s = unresolved_string_at(which);
   547   if (s == NULL) {
   548     return (char*)"<pseudo-string>";
   549   } else {
   550     return unresolved_string_at(which)->as_C_string();
   551   }
   552 }
   554 BasicType ConstantPool::basic_type_for_signature_at(int which) {
   555   return FieldType::basic_type(symbol_at(which));
   556 }
   559 void ConstantPool::resolve_string_constants_impl(constantPoolHandle this_oop, TRAPS) {
   560   for (int index = 1; index < this_oop->length(); index++) { // Index 0 is unused
   561     if (this_oop->tag_at(index).is_string()) {
   562       this_oop->string_at(index, CHECK);
   563     }
   564   }
   565 }
   567 // Resolve all the classes in the constant pool.  If they are all resolved,
   568 // the constant pool is read-only.  Enhancement: allocate cp entries to
   569 // another metaspace, and copy to read-only or read-write space if this
   570 // bit is set.
   571 bool ConstantPool::resolve_class_constants(TRAPS) {
   572   constantPoolHandle cp(THREAD, this);
   573   for (int index = 1; index < length(); index++) { // Index 0 is unused
   574     if (tag_at(index).is_unresolved_klass() &&
   575         klass_at_if_loaded(cp, index) == NULL) {
   576       return false;
   577   }
   578   }
   579   // set_preresolution(); or some bit for future use
   580   return true;
   581 }
   583 // If resolution for MethodHandle or MethodType fails, save the exception
   584 // in the resolution error table, so that the same exception is thrown again.
   585 void ConstantPool::save_and_throw_exception(constantPoolHandle this_oop, int which,
   586                                      int tag, TRAPS) {
   587   ResourceMark rm;
   588   Symbol* error = PENDING_EXCEPTION->klass()->name();
   589   MonitorLockerEx ml(this_oop->lock());  // lock cpool to change tag.
   591   int error_tag = (tag == JVM_CONSTANT_MethodHandle) ?
   592            JVM_CONSTANT_MethodHandleInError : JVM_CONSTANT_MethodTypeInError;
   594   if (!PENDING_EXCEPTION->
   595     is_a(SystemDictionary::LinkageError_klass())) {
   596     // Just throw the exception and don't prevent these classes from
   597     // being loaded due to virtual machine errors like StackOverflow
   598     // and OutOfMemoryError, etc, or if the thread was hit by stop()
   599     // Needs clarification to section 5.4.3 of the VM spec (see 6308271)
   601   } else if (this_oop->tag_at(which).value() != error_tag) {
   602     SystemDictionary::add_resolution_error(this_oop, which, error);
   603     this_oop->tag_at_put(which, error_tag);
   604   } else {
   605     // some other thread has put the class in error state.
   606     error = SystemDictionary::find_resolution_error(this_oop, which);
   607     assert(error != NULL, "checking");
   608     CLEAR_PENDING_EXCEPTION;
   609     THROW_MSG(error, "");
   610   }
   611 }
   614 // Called to resolve constants in the constant pool and return an oop.
   615 // Some constant pool entries cache their resolved oop. This is also
   616 // called to create oops from constants to use in arguments for invokedynamic
   617 oop ConstantPool::resolve_constant_at_impl(constantPoolHandle this_oop, int index, int cache_index, TRAPS) {
   618   oop result_oop = NULL;
   619   Handle throw_exception;
   621   if (cache_index == _possible_index_sentinel) {
   622     // It is possible that this constant is one which is cached in the objects.
   623     // We'll do a linear search.  This should be OK because this usage is rare.
   624     assert(index > 0, "valid index");
   625     cache_index = this_oop->cp_to_object_index(index);
   626   }
   627   assert(cache_index == _no_index_sentinel || cache_index >= 0, "");
   628   assert(index == _no_index_sentinel || index >= 0, "");
   630   if (cache_index >= 0) {
   631     result_oop = this_oop->resolved_references()->obj_at(cache_index);
   632     if (result_oop != NULL) {
   633       return result_oop;
   634       // That was easy...
   635     }
   636     index = this_oop->object_to_cp_index(cache_index);
   637   }
   639   jvalue prim_value;  // temp used only in a few cases below
   641   int tag_value = this_oop->tag_at(index).value();
   643   switch (tag_value) {
   645   case JVM_CONSTANT_UnresolvedClass:
   646   case JVM_CONSTANT_UnresolvedClassInError:
   647   case JVM_CONSTANT_Class:
   648     {
   649       assert(cache_index == _no_index_sentinel, "should not have been set");
   650       Klass* resolved = klass_at_impl(this_oop, index, CHECK_NULL);
   651       // ldc wants the java mirror.
   652       result_oop = resolved->java_mirror();
   653       break;
   654     }
   656   case JVM_CONSTANT_String:
   657     assert(cache_index != _no_index_sentinel, "should have been set");
   658     if (this_oop->is_pseudo_string_at(index)) {
   659       result_oop = this_oop->pseudo_string_at(index, cache_index);
   660       break;
   661     }
   662     result_oop = string_at_impl(this_oop, index, cache_index, CHECK_NULL);
   663     break;
   665   case JVM_CONSTANT_MethodHandleInError:
   666   case JVM_CONSTANT_MethodTypeInError:
   667     {
   668       Symbol* error = SystemDictionary::find_resolution_error(this_oop, index);
   669       guarantee(error != (Symbol*)NULL, "tag mismatch with resolution error table");
   670       ResourceMark rm;
   671       THROW_MSG_0(error, "");
   672       break;
   673     }
   675   case JVM_CONSTANT_MethodHandle:
   676     {
   677       int ref_kind                 = this_oop->method_handle_ref_kind_at(index);
   678       int callee_index             = this_oop->method_handle_klass_index_at(index);
   679       Symbol*  name =      this_oop->method_handle_name_ref_at(index);
   680       Symbol*  signature = this_oop->method_handle_signature_ref_at(index);
   681       if (PrintMiscellaneous)
   682         tty->print_cr("resolve JVM_CONSTANT_MethodHandle:%d [%d/%d/%d] %s.%s",
   683                       ref_kind, index, this_oop->method_handle_index_at(index),
   684                       callee_index, name->as_C_string(), signature->as_C_string());
   685       KlassHandle callee;
   686       { Klass* k = klass_at_impl(this_oop, callee_index, CHECK_NULL);
   687         callee = KlassHandle(THREAD, k);
   688       }
   689       KlassHandle klass(THREAD, this_oop->pool_holder());
   690       Handle value = SystemDictionary::link_method_handle_constant(klass, ref_kind,
   691                                                                    callee, name, signature,
   692                                                                    THREAD);
   693       result_oop = value();
   694       if (HAS_PENDING_EXCEPTION) {
   695         save_and_throw_exception(this_oop, index, tag_value, CHECK_NULL);
   696       }
   697       break;
   698     }
   700   case JVM_CONSTANT_MethodType:
   701     {
   702       Symbol*  signature = this_oop->method_type_signature_at(index);
   703       if (PrintMiscellaneous)
   704         tty->print_cr("resolve JVM_CONSTANT_MethodType [%d/%d] %s",
   705                       index, this_oop->method_type_index_at(index),
   706                       signature->as_C_string());
   707       KlassHandle klass(THREAD, this_oop->pool_holder());
   708       Handle value = SystemDictionary::find_method_handle_type(signature, klass, THREAD);
   709       result_oop = value();
   710       if (HAS_PENDING_EXCEPTION) {
   711         save_and_throw_exception(this_oop, index, tag_value, CHECK_NULL);
   712       }
   713       break;
   714     }
   716   case JVM_CONSTANT_Integer:
   717     assert(cache_index == _no_index_sentinel, "should not have been set");
   718     prim_value.i = this_oop->int_at(index);
   719     result_oop = java_lang_boxing_object::create(T_INT, &prim_value, CHECK_NULL);
   720     break;
   722   case JVM_CONSTANT_Float:
   723     assert(cache_index == _no_index_sentinel, "should not have been set");
   724     prim_value.f = this_oop->float_at(index);
   725     result_oop = java_lang_boxing_object::create(T_FLOAT, &prim_value, CHECK_NULL);
   726     break;
   728   case JVM_CONSTANT_Long:
   729     assert(cache_index == _no_index_sentinel, "should not have been set");
   730     prim_value.j = this_oop->long_at(index);
   731     result_oop = java_lang_boxing_object::create(T_LONG, &prim_value, CHECK_NULL);
   732     break;
   734   case JVM_CONSTANT_Double:
   735     assert(cache_index == _no_index_sentinel, "should not have been set");
   736     prim_value.d = this_oop->double_at(index);
   737     result_oop = java_lang_boxing_object::create(T_DOUBLE, &prim_value, CHECK_NULL);
   738     break;
   740   default:
   741     DEBUG_ONLY( tty->print_cr("*** %p: tag at CP[%d/%d] = %d",
   742                               this_oop(), index, cache_index, tag_value) );
   743     assert(false, "unexpected constant tag");
   744     break;
   745   }
   747   if (cache_index >= 0) {
   748     // Cache the oop here also.
   749     Handle result_handle(THREAD, result_oop);
   750     MonitorLockerEx ml(this_oop->lock());  // don't know if we really need this
   751     oop result = this_oop->resolved_references()->obj_at(cache_index);
   752     // Benign race condition:  resolved_references may already be filled in while we were trying to lock.
   753     // The important thing here is that all threads pick up the same result.
   754     // It doesn't matter which racing thread wins, as long as only one
   755     // result is used by all threads, and all future queries.
   756     // That result may be either a resolved constant or a failure exception.
   757     if (result == NULL) {
   758       this_oop->resolved_references()->obj_at_put(cache_index, result_handle());
   759       return result_handle();
   760     } else {
   761       // Return the winning thread's result.  This can be different than
   762       // result_handle() for MethodHandles.
   763       return result;
   764     }
   765   } else {
   766     return result_oop;
   767   }
   768 }
   770 oop ConstantPool::uncached_string_at(int which, TRAPS) {
   771   Symbol* sym = unresolved_string_at(which);
   772   oop str = StringTable::intern(sym, CHECK_(NULL));
   773   assert(java_lang_String::is_instance(str), "must be string");
   774   return str;
   775 }
   778 oop ConstantPool::resolve_bootstrap_specifier_at_impl(constantPoolHandle this_oop, int index, TRAPS) {
   779   assert(this_oop->tag_at(index).is_invoke_dynamic(), "Corrupted constant pool");
   781   Handle bsm;
   782   int argc;
   783   {
   784     // JVM_CONSTANT_InvokeDynamic is an ordered pair of [bootm, name&type], plus optional arguments
   785     // The bootm, being a JVM_CONSTANT_MethodHandle, has its own cache entry.
   786     // It is accompanied by the optional arguments.
   787     int bsm_index = this_oop->invoke_dynamic_bootstrap_method_ref_index_at(index);
   788     oop bsm_oop = this_oop->resolve_possibly_cached_constant_at(bsm_index, CHECK_NULL);
   789     if (!java_lang_invoke_MethodHandle::is_instance(bsm_oop)) {
   790       THROW_MSG_NULL(vmSymbols::java_lang_LinkageError(), "BSM not an MethodHandle");
   791     }
   793     // Extract the optional static arguments.
   794     argc = this_oop->invoke_dynamic_argument_count_at(index);
   795     if (argc == 0)  return bsm_oop;
   797     bsm = Handle(THREAD, bsm_oop);
   798   }
   800   objArrayHandle info;
   801   {
   802     objArrayOop info_oop = oopFactory::new_objArray(SystemDictionary::Object_klass(), 1+argc, CHECK_NULL);
   803     info = objArrayHandle(THREAD, info_oop);
   804   }
   806   info->obj_at_put(0, bsm());
   807   for (int i = 0; i < argc; i++) {
   808     int arg_index = this_oop->invoke_dynamic_argument_index_at(index, i);
   809     oop arg_oop = this_oop->resolve_possibly_cached_constant_at(arg_index, CHECK_NULL);
   810     info->obj_at_put(1+i, arg_oop);
   811   }
   813   return info();
   814 }
   816 oop ConstantPool::string_at_impl(constantPoolHandle this_oop, int which, int obj_index, TRAPS) {
   817   // If the string has already been interned, this entry will be non-null
   818   oop str = this_oop->resolved_references()->obj_at(obj_index);
   819   if (str != NULL) return str;
   820   Symbol* sym = this_oop->unresolved_string_at(which);
   821   str = StringTable::intern(sym, CHECK_(NULL));
   822   this_oop->string_at_put(which, obj_index, str);
   823   assert(java_lang_String::is_instance(str), "must be string");
   824   return str;
   825 }
   828 bool ConstantPool::klass_name_at_matches(instanceKlassHandle k,
   829                                                 int which) {
   830   // Names are interned, so we can compare Symbol*s directly
   831   Symbol* cp_name = klass_name_at(which);
   832   return (cp_name == k->name());
   833 }
   836 // Iterate over symbols and decrement ones which are Symbol*s.
   837 // This is done during GC so do not need to lock constantPool unless we
   838 // have per-thread safepoints.
   839 // Only decrement the UTF8 symbols. Unresolved classes and strings point to
   840 // these symbols but didn't increment the reference count.
   841 void ConstantPool::unreference_symbols() {
   842   for (int index = 1; index < length(); index++) { // Index 0 is unused
   843     constantTag tag = tag_at(index);
   844     if (tag.is_symbol()) {
   845       symbol_at(index)->decrement_refcount();
   846     }
   847   }
   848 }
   851 // Compare this constant pool's entry at index1 to the constant pool
   852 // cp2's entry at index2.
   853 bool ConstantPool::compare_entry_to(int index1, constantPoolHandle cp2,
   854        int index2, TRAPS) {
   856   // The error tags are equivalent to non-error tags when comparing
   857   jbyte t1 = tag_at(index1).non_error_value();
   858   jbyte t2 = cp2->tag_at(index2).non_error_value();
   860   if (t1 != t2) {
   861     // Not the same entry type so there is nothing else to check. Note
   862     // that this style of checking will consider resolved/unresolved
   863     // class pairs as different.
   864     // From the ConstantPool* API point of view, this is correct
   865     // behavior. See VM_RedefineClasses::merge_constant_pools() to see how this
   866     // plays out in the context of ConstantPool* merging.
   867     return false;
   868   }
   870   switch (t1) {
   871   case JVM_CONSTANT_Class:
   872   {
   873     Klass* k1 = klass_at(index1, CHECK_false);
   874     Klass* k2 = cp2->klass_at(index2, CHECK_false);
   875     if (k1 == k2) {
   876       return true;
   877     }
   878   } break;
   880   case JVM_CONSTANT_ClassIndex:
   881   {
   882     int recur1 = klass_index_at(index1);
   883     int recur2 = cp2->klass_index_at(index2);
   884     bool match = compare_entry_to(recur1, cp2, recur2, CHECK_false);
   885     if (match) {
   886       return true;
   887     }
   888   } break;
   890   case JVM_CONSTANT_Double:
   891   {
   892     jdouble d1 = double_at(index1);
   893     jdouble d2 = cp2->double_at(index2);
   894     if (d1 == d2) {
   895       return true;
   896     }
   897   } break;
   899   case JVM_CONSTANT_Fieldref:
   900   case JVM_CONSTANT_InterfaceMethodref:
   901   case JVM_CONSTANT_Methodref:
   902   {
   903     int recur1 = uncached_klass_ref_index_at(index1);
   904     int recur2 = cp2->uncached_klass_ref_index_at(index2);
   905     bool match = compare_entry_to(recur1, cp2, recur2, CHECK_false);
   906     if (match) {
   907       recur1 = uncached_name_and_type_ref_index_at(index1);
   908       recur2 = cp2->uncached_name_and_type_ref_index_at(index2);
   909       match = compare_entry_to(recur1, cp2, recur2, CHECK_false);
   910       if (match) {
   911         return true;
   912       }
   913     }
   914   } break;
   916   case JVM_CONSTANT_Float:
   917   {
   918     jfloat f1 = float_at(index1);
   919     jfloat f2 = cp2->float_at(index2);
   920     if (f1 == f2) {
   921       return true;
   922     }
   923   } break;
   925   case JVM_CONSTANT_Integer:
   926   {
   927     jint i1 = int_at(index1);
   928     jint i2 = cp2->int_at(index2);
   929     if (i1 == i2) {
   930       return true;
   931     }
   932   } break;
   934   case JVM_CONSTANT_Long:
   935   {
   936     jlong l1 = long_at(index1);
   937     jlong l2 = cp2->long_at(index2);
   938     if (l1 == l2) {
   939       return true;
   940     }
   941   } break;
   943   case JVM_CONSTANT_NameAndType:
   944   {
   945     int recur1 = name_ref_index_at(index1);
   946     int recur2 = cp2->name_ref_index_at(index2);
   947     bool match = compare_entry_to(recur1, cp2, recur2, CHECK_false);
   948     if (match) {
   949       recur1 = signature_ref_index_at(index1);
   950       recur2 = cp2->signature_ref_index_at(index2);
   951       match = compare_entry_to(recur1, cp2, recur2, CHECK_false);
   952       if (match) {
   953         return true;
   954       }
   955     }
   956   } break;
   958   case JVM_CONSTANT_StringIndex:
   959   {
   960     int recur1 = string_index_at(index1);
   961     int recur2 = cp2->string_index_at(index2);
   962     bool match = compare_entry_to(recur1, cp2, recur2, CHECK_false);
   963     if (match) {
   964       return true;
   965     }
   966   } break;
   968   case JVM_CONSTANT_UnresolvedClass:
   969   {
   970     Symbol* k1 = unresolved_klass_at(index1);
   971     Symbol* k2 = cp2->unresolved_klass_at(index2);
   972     if (k1 == k2) {
   973       return true;
   974     }
   975   } break;
   977   case JVM_CONSTANT_MethodType:
   978   {
   979     int k1 = method_type_index_at_error_ok(index1);
   980     int k2 = cp2->method_type_index_at_error_ok(index2);
   981     bool match = compare_entry_to(k1, cp2, k2, CHECK_false);
   982     if (match) {
   983       return true;
   984     }
   985   } break;
   987   case JVM_CONSTANT_MethodHandle:
   988   {
   989     int k1 = method_handle_ref_kind_at_error_ok(index1);
   990     int k2 = cp2->method_handle_ref_kind_at_error_ok(index2);
   991     if (k1 == k2) {
   992       int i1 = method_handle_index_at_error_ok(index1);
   993       int i2 = cp2->method_handle_index_at_error_ok(index2);
   994       bool match = compare_entry_to(i1, cp2, i2, CHECK_false);
   995       if (match) {
   996         return true;
   997       }
   998     }
   999   } break;
  1001   case JVM_CONSTANT_InvokeDynamic:
  1003     int k1 = invoke_dynamic_name_and_type_ref_index_at(index1);
  1004     int k2 = cp2->invoke_dynamic_name_and_type_ref_index_at(index2);
  1005     int i1 = invoke_dynamic_bootstrap_specifier_index(index1);
  1006     int i2 = cp2->invoke_dynamic_bootstrap_specifier_index(index2);
  1007     // separate statements and variables because CHECK_false is used
  1008     bool match_entry = compare_entry_to(k1, cp2, k2, CHECK_false);
  1009     bool match_operand = compare_operand_to(i1, cp2, i2, CHECK_false);
  1010     return (match_entry && match_operand);
  1011   } break;
  1013   case JVM_CONSTANT_String:
  1015     Symbol* s1 = unresolved_string_at(index1);
  1016     Symbol* s2 = cp2->unresolved_string_at(index2);
  1017     if (s1 == s2) {
  1018       return true;
  1020   } break;
  1022   case JVM_CONSTANT_Utf8:
  1024     Symbol* s1 = symbol_at(index1);
  1025     Symbol* s2 = cp2->symbol_at(index2);
  1026     if (s1 == s2) {
  1027       return true;
  1029   } break;
  1031   // Invalid is used as the tag for the second constant pool entry
  1032   // occupied by JVM_CONSTANT_Double or JVM_CONSTANT_Long. It should
  1033   // not be seen by itself.
  1034   case JVM_CONSTANT_Invalid: // fall through
  1036   default:
  1037     ShouldNotReachHere();
  1038     break;
  1041   return false;
  1042 } // end compare_entry_to()
  1045 // Resize the operands array with delta_len and delta_size.
  1046 // Used in RedefineClasses for CP merge.
  1047 void ConstantPool::resize_operands(int delta_len, int delta_size, TRAPS) {
  1048   int old_len  = operand_array_length(operands());
  1049   int new_len  = old_len + delta_len;
  1050   int min_len  = (delta_len > 0) ? old_len : new_len;
  1052   int old_size = operands()->length();
  1053   int new_size = old_size + delta_size;
  1054   int min_size = (delta_size > 0) ? old_size : new_size;
  1056   ClassLoaderData* loader_data = pool_holder()->class_loader_data();
  1057   Array<u2>* new_ops = MetadataFactory::new_array<u2>(loader_data, new_size, CHECK);
  1059   // Set index in the resized array for existing elements only
  1060   for (int idx = 0; idx < min_len; idx++) {
  1061     int offset = operand_offset_at(idx);                       // offset in original array
  1062     operand_offset_at_put(new_ops, idx, offset + 2*delta_len); // offset in resized array
  1064   // Copy the bootstrap specifiers only
  1065   Copy::conjoint_memory_atomic(operands()->adr_at(2*old_len),
  1066                                new_ops->adr_at(2*new_len),
  1067                                (min_size - 2*min_len) * sizeof(u2));
  1068   // Explicitly deallocate old operands array.
  1069   // Note, it is not needed for 7u backport.
  1070   if ( operands() != NULL) { // the safety check
  1071     MetadataFactory::free_array<u2>(loader_data, operands());
  1073   set_operands(new_ops);
  1074 } // end resize_operands()
  1077 // Extend the operands array with the length and size of the ext_cp operands.
  1078 // Used in RedefineClasses for CP merge.
  1079 void ConstantPool::extend_operands(constantPoolHandle ext_cp, TRAPS) {
  1080   int delta_len = operand_array_length(ext_cp->operands());
  1081   if (delta_len == 0) {
  1082     return; // nothing to do
  1084   int delta_size = ext_cp->operands()->length();
  1086   assert(delta_len  > 0 && delta_size > 0, "extended operands array must be bigger");
  1088   if (operand_array_length(operands()) == 0) {
  1089     ClassLoaderData* loader_data = pool_holder()->class_loader_data();
  1090     Array<u2>* new_ops = MetadataFactory::new_array<u2>(loader_data, delta_size, CHECK);
  1091     // The first element index defines the offset of second part
  1092     operand_offset_at_put(new_ops, 0, 2*delta_len); // offset in new array
  1093     set_operands(new_ops);
  1094   } else {
  1095     resize_operands(delta_len, delta_size, CHECK);
  1098 } // end extend_operands()
  1101 // Shrink the operands array to a smaller array with new_len length.
  1102 // Used in RedefineClasses for CP merge.
  1103 void ConstantPool::shrink_operands(int new_len, TRAPS) {
  1104   int old_len = operand_array_length(operands());
  1105   if (new_len == old_len) {
  1106     return; // nothing to do
  1108   assert(new_len < old_len, "shrunken operands array must be smaller");
  1110   int free_base  = operand_next_offset_at(new_len - 1);
  1111   int delta_len  = new_len - old_len;
  1112   int delta_size = 2*delta_len + free_base - operands()->length();
  1114   resize_operands(delta_len, delta_size, CHECK);
  1116 } // end shrink_operands()
  1119 void ConstantPool::copy_operands(constantPoolHandle from_cp,
  1120                                  constantPoolHandle to_cp,
  1121                                  TRAPS) {
  1123   int from_oplen = operand_array_length(from_cp->operands());
  1124   int old_oplen  = operand_array_length(to_cp->operands());
  1125   if (from_oplen != 0) {
  1126     ClassLoaderData* loader_data = to_cp->pool_holder()->class_loader_data();
  1127     // append my operands to the target's operands array
  1128     if (old_oplen == 0) {
  1129       // Can't just reuse from_cp's operand list because of deallocation issues
  1130       int len = from_cp->operands()->length();
  1131       Array<u2>* new_ops = MetadataFactory::new_array<u2>(loader_data, len, CHECK);
  1132       Copy::conjoint_memory_atomic(
  1133           from_cp->operands()->adr_at(0), new_ops->adr_at(0), len * sizeof(u2));
  1134       to_cp->set_operands(new_ops);
  1135     } else {
  1136       int old_len  = to_cp->operands()->length();
  1137       int from_len = from_cp->operands()->length();
  1138       int old_off  = old_oplen * sizeof(u2);
  1139       int from_off = from_oplen * sizeof(u2);
  1140       // Use the metaspace for the destination constant pool
  1141       Array<u2>* new_operands = MetadataFactory::new_array<u2>(loader_data, old_len + from_len, CHECK);
  1142       int fillp = 0, len = 0;
  1143       // first part of dest
  1144       Copy::conjoint_memory_atomic(to_cp->operands()->adr_at(0),
  1145                                    new_operands->adr_at(fillp),
  1146                                    (len = old_off) * sizeof(u2));
  1147       fillp += len;
  1148       // first part of src
  1149       Copy::conjoint_memory_atomic(from_cp->operands()->adr_at(0),
  1150                                    new_operands->adr_at(fillp),
  1151                                    (len = from_off) * sizeof(u2));
  1152       fillp += len;
  1153       // second part of dest
  1154       Copy::conjoint_memory_atomic(to_cp->operands()->adr_at(old_off),
  1155                                    new_operands->adr_at(fillp),
  1156                                    (len = old_len - old_off) * sizeof(u2));
  1157       fillp += len;
  1158       // second part of src
  1159       Copy::conjoint_memory_atomic(from_cp->operands()->adr_at(from_off),
  1160                                    new_operands->adr_at(fillp),
  1161                                    (len = from_len - from_off) * sizeof(u2));
  1162       fillp += len;
  1163       assert(fillp == new_operands->length(), "");
  1165       // Adjust indexes in the first part of the copied operands array.
  1166       for (int j = 0; j < from_oplen; j++) {
  1167         int offset = operand_offset_at(new_operands, old_oplen + j);
  1168         assert(offset == operand_offset_at(from_cp->operands(), j), "correct copy");
  1169         offset += old_len;  // every new tuple is preceded by old_len extra u2's
  1170         operand_offset_at_put(new_operands, old_oplen + j, offset);
  1173       // replace target operands array with combined array
  1174       to_cp->set_operands(new_operands);
  1177 } // end copy_operands()
  1180 // Copy this constant pool's entries at start_i to end_i (inclusive)
  1181 // to the constant pool to_cp's entries starting at to_i. A total of
  1182 // (end_i - start_i) + 1 entries are copied.
  1183 void ConstantPool::copy_cp_to_impl(constantPoolHandle from_cp, int start_i, int end_i,
  1184        constantPoolHandle to_cp, int to_i, TRAPS) {
  1187   int dest_i = to_i;  // leave original alone for debug purposes
  1189   for (int src_i = start_i; src_i <= end_i; /* see loop bottom */ ) {
  1190     copy_entry_to(from_cp, src_i, to_cp, dest_i, CHECK);
  1192     switch (from_cp->tag_at(src_i).value()) {
  1193     case JVM_CONSTANT_Double:
  1194     case JVM_CONSTANT_Long:
  1195       // double and long take two constant pool entries
  1196       src_i += 2;
  1197       dest_i += 2;
  1198       break;
  1200     default:
  1201       // all others take one constant pool entry
  1202       src_i++;
  1203       dest_i++;
  1204       break;
  1207   copy_operands(from_cp, to_cp, CHECK);
  1209 } // end copy_cp_to_impl()
  1212 // Copy this constant pool's entry at from_i to the constant pool
  1213 // to_cp's entry at to_i.
  1214 void ConstantPool::copy_entry_to(constantPoolHandle from_cp, int from_i,
  1215                                         constantPoolHandle to_cp, int to_i,
  1216                                         TRAPS) {
  1218   int tag = from_cp->tag_at(from_i).value();
  1219   switch (tag) {
  1220   case JVM_CONSTANT_Class:
  1222     Klass* k = from_cp->klass_at(from_i, CHECK);
  1223     to_cp->klass_at_put(to_i, k);
  1224   } break;
  1226   case JVM_CONSTANT_ClassIndex:
  1228     jint ki = from_cp->klass_index_at(from_i);
  1229     to_cp->klass_index_at_put(to_i, ki);
  1230   } break;
  1232   case JVM_CONSTANT_Double:
  1234     jdouble d = from_cp->double_at(from_i);
  1235     to_cp->double_at_put(to_i, d);
  1236     // double takes two constant pool entries so init second entry's tag
  1237     to_cp->tag_at_put(to_i + 1, JVM_CONSTANT_Invalid);
  1238   } break;
  1240   case JVM_CONSTANT_Fieldref:
  1242     int class_index = from_cp->uncached_klass_ref_index_at(from_i);
  1243     int name_and_type_index = from_cp->uncached_name_and_type_ref_index_at(from_i);
  1244     to_cp->field_at_put(to_i, class_index, name_and_type_index);
  1245   } break;
  1247   case JVM_CONSTANT_Float:
  1249     jfloat f = from_cp->float_at(from_i);
  1250     to_cp->float_at_put(to_i, f);
  1251   } break;
  1253   case JVM_CONSTANT_Integer:
  1255     jint i = from_cp->int_at(from_i);
  1256     to_cp->int_at_put(to_i, i);
  1257   } break;
  1259   case JVM_CONSTANT_InterfaceMethodref:
  1261     int class_index = from_cp->uncached_klass_ref_index_at(from_i);
  1262     int name_and_type_index = from_cp->uncached_name_and_type_ref_index_at(from_i);
  1263     to_cp->interface_method_at_put(to_i, class_index, name_and_type_index);
  1264   } break;
  1266   case JVM_CONSTANT_Long:
  1268     jlong l = from_cp->long_at(from_i);
  1269     to_cp->long_at_put(to_i, l);
  1270     // long takes two constant pool entries so init second entry's tag
  1271     to_cp->tag_at_put(to_i + 1, JVM_CONSTANT_Invalid);
  1272   } break;
  1274   case JVM_CONSTANT_Methodref:
  1276     int class_index = from_cp->uncached_klass_ref_index_at(from_i);
  1277     int name_and_type_index = from_cp->uncached_name_and_type_ref_index_at(from_i);
  1278     to_cp->method_at_put(to_i, class_index, name_and_type_index);
  1279   } break;
  1281   case JVM_CONSTANT_NameAndType:
  1283     int name_ref_index = from_cp->name_ref_index_at(from_i);
  1284     int signature_ref_index = from_cp->signature_ref_index_at(from_i);
  1285     to_cp->name_and_type_at_put(to_i, name_ref_index, signature_ref_index);
  1286   } break;
  1288   case JVM_CONSTANT_StringIndex:
  1290     jint si = from_cp->string_index_at(from_i);
  1291     to_cp->string_index_at_put(to_i, si);
  1292   } break;
  1294   case JVM_CONSTANT_UnresolvedClass:
  1296     // Can be resolved after checking tag, so check the slot first.
  1297     CPSlot entry = from_cp->slot_at(from_i);
  1298     if (entry.is_resolved()) {
  1299       assert(entry.get_klass()->is_klass(), "must be");
  1300       // Already resolved
  1301       to_cp->klass_at_put(to_i, entry.get_klass());
  1302     } else {
  1303       to_cp->unresolved_klass_at_put(to_i, entry.get_symbol());
  1305   } break;
  1307   case JVM_CONSTANT_String:
  1309     Symbol* s = from_cp->unresolved_string_at(from_i);
  1310     to_cp->unresolved_string_at_put(to_i, s);
  1311   } break;
  1313   case JVM_CONSTANT_Utf8:
  1315     Symbol* s = from_cp->symbol_at(from_i);
  1316     // Need to increase refcount, the old one will be thrown away and deferenced
  1317     s->increment_refcount();
  1318     to_cp->symbol_at_put(to_i, s);
  1319   } break;
  1321   case JVM_CONSTANT_MethodType:
  1322   case JVM_CONSTANT_MethodTypeInError:
  1324     jint k = from_cp->method_type_index_at_error_ok(from_i);
  1325     to_cp->method_type_index_at_put(to_i, k);
  1326   } break;
  1328   case JVM_CONSTANT_MethodHandle:
  1329   case JVM_CONSTANT_MethodHandleInError:
  1331     int k1 = from_cp->method_handle_ref_kind_at_error_ok(from_i);
  1332     int k2 = from_cp->method_handle_index_at_error_ok(from_i);
  1333     to_cp->method_handle_index_at_put(to_i, k1, k2);
  1334   } break;
  1336   case JVM_CONSTANT_InvokeDynamic:
  1338     int k1 = from_cp->invoke_dynamic_bootstrap_specifier_index(from_i);
  1339     int k2 = from_cp->invoke_dynamic_name_and_type_ref_index_at(from_i);
  1340     k1 += operand_array_length(to_cp->operands());  // to_cp might already have operands
  1341     to_cp->invoke_dynamic_at_put(to_i, k1, k2);
  1342   } break;
  1344   // Invalid is used as the tag for the second constant pool entry
  1345   // occupied by JVM_CONSTANT_Double or JVM_CONSTANT_Long. It should
  1346   // not be seen by itself.
  1347   case JVM_CONSTANT_Invalid: // fall through
  1349   default:
  1351     ShouldNotReachHere();
  1352   } break;
  1354 } // end copy_entry_to()
  1357 // Search constant pool search_cp for an entry that matches this
  1358 // constant pool's entry at pattern_i. Returns the index of a
  1359 // matching entry or zero (0) if there is no matching entry.
  1360 int ConstantPool::find_matching_entry(int pattern_i,
  1361       constantPoolHandle search_cp, TRAPS) {
  1363   // index zero (0) is not used
  1364   for (int i = 1; i < search_cp->length(); i++) {
  1365     bool found = compare_entry_to(pattern_i, search_cp, i, CHECK_0);
  1366     if (found) {
  1367       return i;
  1371   return 0;  // entry not found; return unused index zero (0)
  1372 } // end find_matching_entry()
  1375 // Compare this constant pool's bootstrap specifier at idx1 to the constant pool
  1376 // cp2's bootstrap specifier at idx2.
  1377 bool ConstantPool::compare_operand_to(int idx1, constantPoolHandle cp2, int idx2, TRAPS) {
  1378   int k1 = operand_bootstrap_method_ref_index_at(idx1);
  1379   int k2 = cp2->operand_bootstrap_method_ref_index_at(idx2);
  1380   bool match = compare_entry_to(k1, cp2, k2, CHECK_false);
  1382   if (!match) {
  1383     return false;
  1385   int argc = operand_argument_count_at(idx1);
  1386   if (argc == cp2->operand_argument_count_at(idx2)) {
  1387     for (int j = 0; j < argc; j++) {
  1388       k1 = operand_argument_index_at(idx1, j);
  1389       k2 = cp2->operand_argument_index_at(idx2, j);
  1390       match = compare_entry_to(k1, cp2, k2, CHECK_false);
  1391       if (!match) {
  1392         return false;
  1395     return true;           // got through loop; all elements equal
  1397   return false;
  1398 } // end compare_operand_to()
  1400 // Search constant pool search_cp for a bootstrap specifier that matches
  1401 // this constant pool's bootstrap specifier at pattern_i index.
  1402 // Return the index of a matching bootstrap specifier or (-1) if there is no match.
  1403 int ConstantPool::find_matching_operand(int pattern_i,
  1404                     constantPoolHandle search_cp, int search_len, TRAPS) {
  1405   for (int i = 0; i < search_len; i++) {
  1406     bool found = compare_operand_to(pattern_i, search_cp, i, CHECK_(-1));
  1407     if (found) {
  1408       return i;
  1411   return -1;  // bootstrap specifier not found; return unused index (-1)
  1412 } // end find_matching_operand()
  1415 #ifndef PRODUCT
  1417 const char* ConstantPool::printable_name_at(int which) {
  1419   constantTag tag = tag_at(which);
  1421   if (tag.is_string()) {
  1422     return string_at_noresolve(which);
  1423   } else if (tag.is_klass() || tag.is_unresolved_klass()) {
  1424     return klass_name_at(which)->as_C_string();
  1425   } else if (tag.is_symbol()) {
  1426     return symbol_at(which)->as_C_string();
  1428   return "";
  1431 #endif // PRODUCT
  1434 // JVMTI GetConstantPool support
  1436 // For debugging of constant pool
  1437 const bool debug_cpool = false;
  1439 #define DBG(code) do { if (debug_cpool) { (code); } } while(0)
  1441 static void print_cpool_bytes(jint cnt, u1 *bytes) {
  1442   const char* WARN_MSG = "Must not be such entry!";
  1443   jint size = 0;
  1444   u2   idx1, idx2;
  1446   for (jint idx = 1; idx < cnt; idx++) {
  1447     jint ent_size = 0;
  1448     u1   tag  = *bytes++;
  1449     size++;                       // count tag
  1451     printf("const #%03d, tag: %02d ", idx, tag);
  1452     switch(tag) {
  1453       case JVM_CONSTANT_Invalid: {
  1454         printf("Invalid");
  1455         break;
  1457       case JVM_CONSTANT_Unicode: {
  1458         printf("Unicode      %s", WARN_MSG);
  1459         break;
  1461       case JVM_CONSTANT_Utf8: {
  1462         u2 len = Bytes::get_Java_u2(bytes);
  1463         char str[128];
  1464         if (len > 127) {
  1465            len = 127;
  1467         strncpy(str, (char *) (bytes+2), len);
  1468         str[len] = '\0';
  1469         printf("Utf8          \"%s\"", str);
  1470         ent_size = 2 + len;
  1471         break;
  1473       case JVM_CONSTANT_Integer: {
  1474         u4 val = Bytes::get_Java_u4(bytes);
  1475         printf("int          %d", *(int *) &val);
  1476         ent_size = 4;
  1477         break;
  1479       case JVM_CONSTANT_Float: {
  1480         u4 val = Bytes::get_Java_u4(bytes);
  1481         printf("float        %5.3ff", *(float *) &val);
  1482         ent_size = 4;
  1483         break;
  1485       case JVM_CONSTANT_Long: {
  1486         u8 val = Bytes::get_Java_u8(bytes);
  1487         printf("long         "INT64_FORMAT, (int64_t) *(jlong *) &val);
  1488         ent_size = 8;
  1489         idx++; // Long takes two cpool slots
  1490         break;
  1492       case JVM_CONSTANT_Double: {
  1493         u8 val = Bytes::get_Java_u8(bytes);
  1494         printf("double       %5.3fd", *(jdouble *)&val);
  1495         ent_size = 8;
  1496         idx++; // Double takes two cpool slots
  1497         break;
  1499       case JVM_CONSTANT_Class: {
  1500         idx1 = Bytes::get_Java_u2(bytes);
  1501         printf("class        #%03d", idx1);
  1502         ent_size = 2;
  1503         break;
  1505       case JVM_CONSTANT_String: {
  1506         idx1 = Bytes::get_Java_u2(bytes);
  1507         printf("String       #%03d", idx1);
  1508         ent_size = 2;
  1509         break;
  1511       case JVM_CONSTANT_Fieldref: {
  1512         idx1 = Bytes::get_Java_u2(bytes);
  1513         idx2 = Bytes::get_Java_u2(bytes+2);
  1514         printf("Field        #%03d, #%03d", (int) idx1, (int) idx2);
  1515         ent_size = 4;
  1516         break;
  1518       case JVM_CONSTANT_Methodref: {
  1519         idx1 = Bytes::get_Java_u2(bytes);
  1520         idx2 = Bytes::get_Java_u2(bytes+2);
  1521         printf("Method       #%03d, #%03d", idx1, idx2);
  1522         ent_size = 4;
  1523         break;
  1525       case JVM_CONSTANT_InterfaceMethodref: {
  1526         idx1 = Bytes::get_Java_u2(bytes);
  1527         idx2 = Bytes::get_Java_u2(bytes+2);
  1528         printf("InterfMethod #%03d, #%03d", idx1, idx2);
  1529         ent_size = 4;
  1530         break;
  1532       case JVM_CONSTANT_NameAndType: {
  1533         idx1 = Bytes::get_Java_u2(bytes);
  1534         idx2 = Bytes::get_Java_u2(bytes+2);
  1535         printf("NameAndType  #%03d, #%03d", idx1, idx2);
  1536         ent_size = 4;
  1537         break;
  1539       case JVM_CONSTANT_ClassIndex: {
  1540         printf("ClassIndex  %s", WARN_MSG);
  1541         break;
  1543       case JVM_CONSTANT_UnresolvedClass: {
  1544         printf("UnresolvedClass: %s", WARN_MSG);
  1545         break;
  1547       case JVM_CONSTANT_UnresolvedClassInError: {
  1548         printf("UnresolvedClassInErr: %s", WARN_MSG);
  1549         break;
  1551       case JVM_CONSTANT_StringIndex: {
  1552         printf("StringIndex: %s", WARN_MSG);
  1553         break;
  1556     printf(";\n");
  1557     bytes += ent_size;
  1558     size  += ent_size;
  1560   printf("Cpool size: %d\n", size);
  1561   fflush(0);
  1562   return;
  1563 } /* end print_cpool_bytes */
  1566 // Returns size of constant pool entry.
  1567 jint ConstantPool::cpool_entry_size(jint idx) {
  1568   switch(tag_at(idx).value()) {
  1569     case JVM_CONSTANT_Invalid:
  1570     case JVM_CONSTANT_Unicode:
  1571       return 1;
  1573     case JVM_CONSTANT_Utf8:
  1574       return 3 + symbol_at(idx)->utf8_length();
  1576     case JVM_CONSTANT_Class:
  1577     case JVM_CONSTANT_String:
  1578     case JVM_CONSTANT_ClassIndex:
  1579     case JVM_CONSTANT_UnresolvedClass:
  1580     case JVM_CONSTANT_UnresolvedClassInError:
  1581     case JVM_CONSTANT_StringIndex:
  1582     case JVM_CONSTANT_MethodType:
  1583     case JVM_CONSTANT_MethodTypeInError:
  1584       return 3;
  1586     case JVM_CONSTANT_MethodHandle:
  1587     case JVM_CONSTANT_MethodHandleInError:
  1588       return 4; //tag, ref_kind, ref_index
  1590     case JVM_CONSTANT_Integer:
  1591     case JVM_CONSTANT_Float:
  1592     case JVM_CONSTANT_Fieldref:
  1593     case JVM_CONSTANT_Methodref:
  1594     case JVM_CONSTANT_InterfaceMethodref:
  1595     case JVM_CONSTANT_NameAndType:
  1596       return 5;
  1598     case JVM_CONSTANT_InvokeDynamic:
  1599       // u1 tag, u2 bsm, u2 nt
  1600       return 5;
  1602     case JVM_CONSTANT_Long:
  1603     case JVM_CONSTANT_Double:
  1604       return 9;
  1606   assert(false, "cpool_entry_size: Invalid constant pool entry tag");
  1607   return 1;
  1608 } /* end cpool_entry_size */
  1611 // SymbolHashMap is used to find a constant pool index from a string.
  1612 // This function fills in SymbolHashMaps, one for utf8s and one for
  1613 // class names, returns size of the cpool raw bytes.
  1614 jint ConstantPool::hash_entries_to(SymbolHashMap *symmap,
  1615                                           SymbolHashMap *classmap) {
  1616   jint size = 0;
  1618   for (u2 idx = 1; idx < length(); idx++) {
  1619     u2 tag = tag_at(idx).value();
  1620     size += cpool_entry_size(idx);
  1622     switch(tag) {
  1623       case JVM_CONSTANT_Utf8: {
  1624         Symbol* sym = symbol_at(idx);
  1625         symmap->add_entry(sym, idx);
  1626         DBG(printf("adding symbol entry %s = %d\n", sym->as_utf8(), idx));
  1627         break;
  1629       case JVM_CONSTANT_Class:
  1630       case JVM_CONSTANT_UnresolvedClass:
  1631       case JVM_CONSTANT_UnresolvedClassInError: {
  1632         Symbol* sym = klass_name_at(idx);
  1633         classmap->add_entry(sym, idx);
  1634         DBG(printf("adding class entry %s = %d\n", sym->as_utf8(), idx));
  1635         break;
  1637       case JVM_CONSTANT_Long:
  1638       case JVM_CONSTANT_Double: {
  1639         idx++; // Both Long and Double take two cpool slots
  1640         break;
  1644   return size;
  1645 } /* end hash_utf8_entries_to */
  1648 // Copy cpool bytes.
  1649 // Returns:
  1650 //    0, in case of OutOfMemoryError
  1651 //   -1, in case of internal error
  1652 //  > 0, count of the raw cpool bytes that have been copied
  1653 int ConstantPool::copy_cpool_bytes(int cpool_size,
  1654                                           SymbolHashMap* tbl,
  1655                                           unsigned char *bytes) {
  1656   u2   idx1, idx2;
  1657   jint size  = 0;
  1658   jint cnt   = length();
  1659   unsigned char *start_bytes = bytes;
  1661   for (jint idx = 1; idx < cnt; idx++) {
  1662     u1   tag      = tag_at(idx).value();
  1663     jint ent_size = cpool_entry_size(idx);
  1665     assert(size + ent_size <= cpool_size, "Size mismatch");
  1667     *bytes = tag;
  1668     DBG(printf("#%03hd tag=%03hd, ", idx, tag));
  1669     switch(tag) {
  1670       case JVM_CONSTANT_Invalid: {
  1671         DBG(printf("JVM_CONSTANT_Invalid"));
  1672         break;
  1674       case JVM_CONSTANT_Unicode: {
  1675         assert(false, "Wrong constant pool tag: JVM_CONSTANT_Unicode");
  1676         DBG(printf("JVM_CONSTANT_Unicode"));
  1677         break;
  1679       case JVM_CONSTANT_Utf8: {
  1680         Symbol* sym = symbol_at(idx);
  1681         char*     str = sym->as_utf8();
  1682         // Warning! It's crashing on x86 with len = sym->utf8_length()
  1683         int       len = (int) strlen(str);
  1684         Bytes::put_Java_u2((address) (bytes+1), (u2) len);
  1685         for (int i = 0; i < len; i++) {
  1686             bytes[3+i] = (u1) str[i];
  1688         DBG(printf("JVM_CONSTANT_Utf8: %s ", str));
  1689         break;
  1691       case JVM_CONSTANT_Integer: {
  1692         jint val = int_at(idx);
  1693         Bytes::put_Java_u4((address) (bytes+1), *(u4*)&val);
  1694         break;
  1696       case JVM_CONSTANT_Float: {
  1697         jfloat val = float_at(idx);
  1698         Bytes::put_Java_u4((address) (bytes+1), *(u4*)&val);
  1699         break;
  1701       case JVM_CONSTANT_Long: {
  1702         jlong val = long_at(idx);
  1703         Bytes::put_Java_u8((address) (bytes+1), *(u8*)&val);
  1704         idx++;             // Long takes two cpool slots
  1705         break;
  1707       case JVM_CONSTANT_Double: {
  1708         jdouble val = double_at(idx);
  1709         Bytes::put_Java_u8((address) (bytes+1), *(u8*)&val);
  1710         idx++;             // Double takes two cpool slots
  1711         break;
  1713       case JVM_CONSTANT_Class:
  1714       case JVM_CONSTANT_UnresolvedClass:
  1715       case JVM_CONSTANT_UnresolvedClassInError: {
  1716         *bytes = JVM_CONSTANT_Class;
  1717         Symbol* sym = klass_name_at(idx);
  1718         idx1 = tbl->symbol_to_value(sym);
  1719         assert(idx1 != 0, "Have not found a hashtable entry");
  1720         Bytes::put_Java_u2((address) (bytes+1), idx1);
  1721         DBG(printf("JVM_CONSTANT_Class: idx=#%03hd, %s", idx1, sym->as_utf8()));
  1722         break;
  1724       case JVM_CONSTANT_String: {
  1725         *bytes = JVM_CONSTANT_String;
  1726         Symbol* sym = unresolved_string_at(idx);
  1727         idx1 = tbl->symbol_to_value(sym);
  1728         assert(idx1 != 0, "Have not found a hashtable entry");
  1729         Bytes::put_Java_u2((address) (bytes+1), idx1);
  1730         DBG(printf("JVM_CONSTANT_String: idx=#%03hd, %s", idx1, sym->as_utf8()));
  1731         break;
  1733       case JVM_CONSTANT_Fieldref:
  1734       case JVM_CONSTANT_Methodref:
  1735       case JVM_CONSTANT_InterfaceMethodref: {
  1736         idx1 = uncached_klass_ref_index_at(idx);
  1737         idx2 = uncached_name_and_type_ref_index_at(idx);
  1738         Bytes::put_Java_u2((address) (bytes+1), idx1);
  1739         Bytes::put_Java_u2((address) (bytes+3), idx2);
  1740         DBG(printf("JVM_CONSTANT_Methodref: %hd %hd", idx1, idx2));
  1741         break;
  1743       case JVM_CONSTANT_NameAndType: {
  1744         idx1 = name_ref_index_at(idx);
  1745         idx2 = signature_ref_index_at(idx);
  1746         Bytes::put_Java_u2((address) (bytes+1), idx1);
  1747         Bytes::put_Java_u2((address) (bytes+3), idx2);
  1748         DBG(printf("JVM_CONSTANT_NameAndType: %hd %hd", idx1, idx2));
  1749         break;
  1751       case JVM_CONSTANT_ClassIndex: {
  1752         *bytes = JVM_CONSTANT_Class;
  1753         idx1 = klass_index_at(idx);
  1754         Bytes::put_Java_u2((address) (bytes+1), idx1);
  1755         DBG(printf("JVM_CONSTANT_ClassIndex: %hd", idx1));
  1756         break;
  1758       case JVM_CONSTANT_StringIndex: {
  1759         *bytes = JVM_CONSTANT_String;
  1760         idx1 = string_index_at(idx);
  1761         Bytes::put_Java_u2((address) (bytes+1), idx1);
  1762         DBG(printf("JVM_CONSTANT_StringIndex: %hd", idx1));
  1763         break;
  1765       case JVM_CONSTANT_MethodHandle:
  1766       case JVM_CONSTANT_MethodHandleInError: {
  1767         *bytes = JVM_CONSTANT_MethodHandle;
  1768         int kind = method_handle_ref_kind_at_error_ok(idx);
  1769         idx1 = method_handle_index_at_error_ok(idx);
  1770         *(bytes+1) = (unsigned char) kind;
  1771         Bytes::put_Java_u2((address) (bytes+2), idx1);
  1772         DBG(printf("JVM_CONSTANT_MethodHandle: %d %hd", kind, idx1));
  1773         break;
  1775       case JVM_CONSTANT_MethodType:
  1776       case JVM_CONSTANT_MethodTypeInError: {
  1777         *bytes = JVM_CONSTANT_MethodType;
  1778         idx1 = method_type_index_at_error_ok(idx);
  1779         Bytes::put_Java_u2((address) (bytes+1), idx1);
  1780         DBG(printf("JVM_CONSTANT_MethodType: %hd", idx1));
  1781         break;
  1783       case JVM_CONSTANT_InvokeDynamic: {
  1784         *bytes = tag;
  1785         idx1 = extract_low_short_from_int(*int_at_addr(idx));
  1786         idx2 = extract_high_short_from_int(*int_at_addr(idx));
  1787         assert(idx2 == invoke_dynamic_name_and_type_ref_index_at(idx), "correct half of u4");
  1788         Bytes::put_Java_u2((address) (bytes+1), idx1);
  1789         Bytes::put_Java_u2((address) (bytes+3), idx2);
  1790         DBG(printf("JVM_CONSTANT_InvokeDynamic: %hd %hd", idx1, idx2));
  1791         break;
  1794     DBG(printf("\n"));
  1795     bytes += ent_size;
  1796     size  += ent_size;
  1798   assert(size == cpool_size, "Size mismatch");
  1800   // Keep temorarily for debugging until it's stable.
  1801   DBG(print_cpool_bytes(cnt, start_bytes));
  1802   return (int)(bytes - start_bytes);
  1803 } /* end copy_cpool_bytes */
  1805 #undef DBG
  1808 void ConstantPool::set_on_stack(const bool value) {
  1809   if (value) {
  1810     _flags |= _on_stack;
  1811   } else {
  1812     _flags &= ~_on_stack;
  1814   if (value) MetadataOnStackMark::record(this);
  1817 // JSR 292 support for patching constant pool oops after the class is linked and
  1818 // the oop array for resolved references are created.
  1819 // We can't do this during classfile parsing, which is how the other indexes are
  1820 // patched.  The other patches are applied early for some error checking
  1821 // so only defer the pseudo_strings.
  1822 void ConstantPool::patch_resolved_references(
  1823                                             GrowableArray<Handle>* cp_patches) {
  1824   assert(EnableInvokeDynamic, "");
  1825   for (int index = 1; index < cp_patches->length(); index++) { // Index 0 is unused
  1826     Handle patch = cp_patches->at(index);
  1827     if (patch.not_null()) {
  1828       assert (tag_at(index).is_string(), "should only be string left");
  1829       // Patching a string means pre-resolving it.
  1830       // The spelling in the constant pool is ignored.
  1831       // The constant reference may be any object whatever.
  1832       // If it is not a real interned string, the constant is referred
  1833       // to as a "pseudo-string", and must be presented to the CP
  1834       // explicitly, because it may require scavenging.
  1835       int obj_index = cp_to_object_index(index);
  1836       pseudo_string_at_put(index, obj_index, patch());
  1837       DEBUG_ONLY(cp_patches->at_put(index, Handle());)
  1840 #ifdef ASSERT
  1841   // Ensure that all the patches have been used.
  1842   for (int index = 0; index < cp_patches->length(); index++) {
  1843     assert(cp_patches->at(index).is_null(),
  1844            err_msg("Unused constant pool patch at %d in class file %s",
  1845                    index,
  1846                    pool_holder()->external_name()));
  1848 #endif // ASSERT
  1851 #ifndef PRODUCT
  1853 // CompileTheWorld support. Preload all classes loaded references in the passed in constantpool
  1854 void ConstantPool::preload_and_initialize_all_classes(ConstantPool* obj, TRAPS) {
  1855   guarantee(obj->is_constantPool(), "object must be constant pool");
  1856   constantPoolHandle cp(THREAD, (ConstantPool*)obj);
  1857   guarantee(cp->pool_holder() != NULL, "must be fully loaded");
  1859   for (int i = 0; i< cp->length();  i++) {
  1860     if (cp->tag_at(i).is_unresolved_klass()) {
  1861       // This will force loading of the class
  1862       Klass* klass = cp->klass_at(i, CHECK);
  1863       if (klass->oop_is_instance()) {
  1864         // Force initialization of class
  1865         InstanceKlass::cast(klass)->initialize(CHECK);
  1871 #endif
  1874 // Printing
  1876 void ConstantPool::print_on(outputStream* st) const {
  1877   EXCEPTION_MARK;
  1878   assert(is_constantPool(), "must be constantPool");
  1879   st->print_cr(internal_name());
  1880   if (flags() != 0) {
  1881     st->print(" - flags: 0x%x", flags());
  1882     if (has_preresolution()) st->print(" has_preresolution");
  1883     if (on_stack()) st->print(" on_stack");
  1884     st->cr();
  1886   if (pool_holder() != NULL) {
  1887     st->print_cr(" - holder: " INTPTR_FORMAT, pool_holder());
  1889   st->print_cr(" - cache: " INTPTR_FORMAT, cache());
  1890   st->print_cr(" - resolved_references: " INTPTR_FORMAT, (void *)resolved_references());
  1891   st->print_cr(" - reference_map: " INTPTR_FORMAT, reference_map());
  1893   for (int index = 1; index < length(); index++) {      // Index 0 is unused
  1894     ((ConstantPool*)this)->print_entry_on(index, st);
  1895     switch (tag_at(index).value()) {
  1896       case JVM_CONSTANT_Long :
  1897       case JVM_CONSTANT_Double :
  1898         index++;   // Skip entry following eigth-byte constant
  1902   st->cr();
  1905 // Print one constant pool entry
  1906 void ConstantPool::print_entry_on(const int index, outputStream* st) {
  1907   EXCEPTION_MARK;
  1908   st->print(" - %3d : ", index);
  1909   tag_at(index).print_on(st);
  1910   st->print(" : ");
  1911   switch (tag_at(index).value()) {
  1912     case JVM_CONSTANT_Class :
  1913       { Klass* k = klass_at(index, CATCH);
  1914         guarantee(k != NULL, "need klass");
  1915         k->print_value_on(st);
  1916         st->print(" {0x%lx}", (address)k);
  1918       break;
  1919     case JVM_CONSTANT_Fieldref :
  1920     case JVM_CONSTANT_Methodref :
  1921     case JVM_CONSTANT_InterfaceMethodref :
  1922       st->print("klass_index=%d", uncached_klass_ref_index_at(index));
  1923       st->print(" name_and_type_index=%d", uncached_name_and_type_ref_index_at(index));
  1924       break;
  1925     case JVM_CONSTANT_String :
  1926       if (is_pseudo_string_at(index)) {
  1927         oop anObj = pseudo_string_at(index);
  1928         anObj->print_value_on(st);
  1929         st->print(" {0x%lx}", (address)anObj);
  1930       } else {
  1931         unresolved_string_at(index)->print_value_on(st);
  1933       break;
  1934     case JVM_CONSTANT_Integer :
  1935       st->print("%d", int_at(index));
  1936       break;
  1937     case JVM_CONSTANT_Float :
  1938       st->print("%f", float_at(index));
  1939       break;
  1940     case JVM_CONSTANT_Long :
  1941       st->print_jlong(long_at(index));
  1942       break;
  1943     case JVM_CONSTANT_Double :
  1944       st->print("%lf", double_at(index));
  1945       break;
  1946     case JVM_CONSTANT_NameAndType :
  1947       st->print("name_index=%d", name_ref_index_at(index));
  1948       st->print(" signature_index=%d", signature_ref_index_at(index));
  1949       break;
  1950     case JVM_CONSTANT_Utf8 :
  1951       symbol_at(index)->print_value_on(st);
  1952       break;
  1953     case JVM_CONSTANT_UnresolvedClass :               // fall-through
  1954     case JVM_CONSTANT_UnresolvedClassInError: {
  1955       // unresolved_klass_at requires lock or safe world.
  1956       CPSlot entry = slot_at(index);
  1957       if (entry.is_resolved()) {
  1958         entry.get_klass()->print_value_on(st);
  1959       } else {
  1960         entry.get_symbol()->print_value_on(st);
  1963       break;
  1964     case JVM_CONSTANT_MethodHandle :
  1965     case JVM_CONSTANT_MethodHandleInError :
  1966       st->print("ref_kind=%d", method_handle_ref_kind_at_error_ok(index));
  1967       st->print(" ref_index=%d", method_handle_index_at_error_ok(index));
  1968       break;
  1969     case JVM_CONSTANT_MethodType :
  1970     case JVM_CONSTANT_MethodTypeInError :
  1971       st->print("signature_index=%d", method_type_index_at_error_ok(index));
  1972       break;
  1973     case JVM_CONSTANT_InvokeDynamic :
  1975         st->print("bootstrap_method_index=%d", invoke_dynamic_bootstrap_method_ref_index_at(index));
  1976         st->print(" name_and_type_index=%d", invoke_dynamic_name_and_type_ref_index_at(index));
  1977         int argc = invoke_dynamic_argument_count_at(index);
  1978         if (argc > 0) {
  1979           for (int arg_i = 0; arg_i < argc; arg_i++) {
  1980             int arg = invoke_dynamic_argument_index_at(index, arg_i);
  1981             st->print((arg_i == 0 ? " arguments={%d" : ", %d"), arg);
  1983           st->print("}");
  1986       break;
  1987     default:
  1988       ShouldNotReachHere();
  1989       break;
  1991   st->cr();
  1994 void ConstantPool::print_value_on(outputStream* st) const {
  1995   assert(is_constantPool(), "must be constantPool");
  1996   st->print("constant pool [%d]", length());
  1997   if (has_preresolution()) st->print("/preresolution");
  1998   if (operands() != NULL)  st->print("/operands[%d]", operands()->length());
  1999   print_address_on(st);
  2000   st->print(" for ");
  2001   pool_holder()->print_value_on(st);
  2002   if (pool_holder() != NULL) {
  2003     bool extra = (pool_holder()->constants() != this);
  2004     if (extra)  st->print(" (extra)");
  2006   if (cache() != NULL) {
  2007     st->print(" cache=" PTR_FORMAT, cache());
  2011 #if INCLUDE_SERVICES
  2012 // Size Statistics
  2013 void ConstantPool::collect_statistics(KlassSizeStats *sz) const {
  2014   sz->_cp_all_bytes += (sz->_cp_bytes          = sz->count(this));
  2015   sz->_cp_all_bytes += (sz->_cp_tags_bytes     = sz->count_array(tags()));
  2016   sz->_cp_all_bytes += (sz->_cp_cache_bytes    = sz->count(cache()));
  2017   sz->_cp_all_bytes += (sz->_cp_operands_bytes = sz->count_array(operands()));
  2018   sz->_cp_all_bytes += (sz->_cp_refmap_bytes   = sz->count_array(reference_map()));
  2020   sz->_ro_bytes += sz->_cp_operands_bytes + sz->_cp_tags_bytes +
  2021                    sz->_cp_refmap_bytes;
  2022   sz->_rw_bytes += sz->_cp_bytes + sz->_cp_cache_bytes;
  2024 #endif // INCLUDE_SERVICES
  2026 // Verification
  2028 void ConstantPool::verify_on(outputStream* st) {
  2029   guarantee(is_constantPool(), "object must be constant pool");
  2030   for (int i = 0; i< length();  i++) {
  2031     constantTag tag = tag_at(i);
  2032     CPSlot entry = slot_at(i);
  2033     if (tag.is_klass()) {
  2034       if (entry.is_resolved()) {
  2035         guarantee(entry.get_klass()->is_klass(),    "should be klass");
  2037     } else if (tag.is_unresolved_klass()) {
  2038       if (entry.is_resolved()) {
  2039         guarantee(entry.get_klass()->is_klass(),    "should be klass");
  2041     } else if (tag.is_symbol()) {
  2042       guarantee(entry.get_symbol()->refcount() != 0, "should have nonzero reference count");
  2043     } else if (tag.is_string()) {
  2044       guarantee(entry.get_symbol()->refcount() != 0, "should have nonzero reference count");
  2047   if (cache() != NULL) {
  2048     // Note: cache() can be NULL before a class is completely setup or
  2049     // in temporary constant pools used during constant pool merging
  2050     guarantee(cache()->is_constantPoolCache(), "should be constant pool cache");
  2052   if (pool_holder() != NULL) {
  2053     // Note: pool_holder() can be NULL in temporary constant pools
  2054     // used during constant pool merging
  2055     guarantee(pool_holder()->is_klass(),    "should be klass");
  2060 void SymbolHashMap::add_entry(Symbol* sym, u2 value) {
  2061   char *str = sym->as_utf8();
  2062   unsigned int hash = compute_hash(str, sym->utf8_length());
  2063   unsigned int index = hash % table_size();
  2065   // check if already in map
  2066   // we prefer the first entry since it is more likely to be what was used in
  2067   // the class file
  2068   for (SymbolHashMapEntry *en = bucket(index); en != NULL; en = en->next()) {
  2069     assert(en->symbol() != NULL, "SymbolHashMapEntry symbol is NULL");
  2070     if (en->hash() == hash && en->symbol() == sym) {
  2071         return;  // already there
  2075   SymbolHashMapEntry* entry = new SymbolHashMapEntry(hash, sym, value);
  2076   entry->set_next(bucket(index));
  2077   _buckets[index].set_entry(entry);
  2078   assert(entry->symbol() != NULL, "SymbolHashMapEntry symbol is NULL");
  2081 SymbolHashMapEntry* SymbolHashMap::find_entry(Symbol* sym) {
  2082   assert(sym != NULL, "SymbolHashMap::find_entry - symbol is NULL");
  2083   char *str = sym->as_utf8();
  2084   int   len = sym->utf8_length();
  2085   unsigned int hash = SymbolHashMap::compute_hash(str, len);
  2086   unsigned int index = hash % table_size();
  2087   for (SymbolHashMapEntry *en = bucket(index); en != NULL; en = en->next()) {
  2088     assert(en->symbol() != NULL, "SymbolHashMapEntry symbol is NULL");
  2089     if (en->hash() == hash && en->symbol() == sym) {
  2090       return en;
  2093   return NULL;

mercurial