hotspot/src/share/vm/oops/constantPool.cpp
changeset 46427 54713555867e
parent 46329 53ccc37bda19
child 46560 388aa8d67c80
equal deleted inserted replaced
46426:02a1fc064144 46427:54713555867e
    45 #include "runtime/signature.hpp"
    45 #include "runtime/signature.hpp"
    46 #include "runtime/vframe.hpp"
    46 #include "runtime/vframe.hpp"
    47 #include "utilities/copy.hpp"
    47 #include "utilities/copy.hpp"
    48 
    48 
    49 ConstantPool* ConstantPool::allocate(ClassLoaderData* loader_data, int length, TRAPS) {
    49 ConstantPool* ConstantPool::allocate(ClassLoaderData* loader_data, int length, TRAPS) {
    50   // Tags are RW but comment below applies to tags also.
       
    51   Array<u1>* tags = MetadataFactory::new_writeable_array<u1>(loader_data, length, 0, CHECK_NULL);
    50   Array<u1>* tags = MetadataFactory::new_writeable_array<u1>(loader_data, length, 0, CHECK_NULL);
    52 
       
    53   int size = ConstantPool::size(length);
    51   int size = ConstantPool::size(length);
    54 
    52   return new (loader_data, size, true, MetaspaceObj::ConstantPoolType, THREAD) ConstantPool(tags);
    55   // CDS considerations:
       
    56   // Allocate read-write but may be able to move to read-only at dumping time
       
    57   // if all the klasses are resolved.  The only other field that is writable is
       
    58   // the resolved_references array, which is recreated at startup time.
       
    59   // But that could be moved to InstanceKlass (although a pain to access from
       
    60   // assembly code).  Maybe it could be moved to the cpCache which is RW.
       
    61   return new (loader_data, size, false, MetaspaceObj::ConstantPoolType, THREAD) ConstantPool(tags);
       
    62 }
    53 }
    63 
    54 
    64 #ifdef ASSERT
    55 #ifdef ASSERT
    65 
    56 
    66 // MetaspaceObj allocation invariant is calloc equivalent memory
    57 // MetaspaceObj allocation invariant is calloc equivalent memory
    78 
    69 
    79 #endif
    70 #endif
    80 
    71 
    81 ConstantPool::ConstantPool(Array<u1>* tags) :
    72 ConstantPool::ConstantPool(Array<u1>* tags) :
    82   _tags(tags),
    73   _tags(tags),
    83   _length(tags->length()),
    74   _length(tags->length()) {
    84   _flags(0) {
       
    85 
    75 
    86     assert(_tags != NULL, "invariant");
    76     assert(_tags != NULL, "invariant");
    87     assert(tags->length() == _length, "invariant");
    77     assert(tags->length() == _length, "invariant");
    88     assert(tag_array_is_zero_initialized(tags), "invariant");
    78     assert(tag_array_is_zero_initialized(tags), "invariant");
    89     assert(0 == _flags, "invariant");
    79     assert(0 == flags(), "invariant");
    90     assert(0 == version(), "invariant");
    80     assert(0 == version(), "invariant");
    91     assert(NULL == _pool_holder, "invariant");
    81     assert(NULL == _pool_holder, "invariant");
    92 }
    82 }
    93 
    83 
    94 void ConstantPool::deallocate_contents(ClassLoaderData* loader_data) {
    84 void ConstantPool::deallocate_contents(ClassLoaderData* loader_data) {
    95   MetadataFactory::free_metadata(loader_data, cache());
    85   if (cache() != NULL) {
    96   set_cache(NULL);
    86     MetadataFactory::free_array<u2>(loader_data, reference_map());
    97   MetadataFactory::free_array<u2>(loader_data, reference_map());
    87     set_reference_map(NULL);
    98   set_reference_map(NULL);
    88     MetadataFactory::free_metadata(loader_data, cache());
       
    89     set_cache(NULL);
       
    90   }
       
    91 
       
    92   MetadataFactory::free_array<Klass*>(loader_data, resolved_klasses());
       
    93   set_resolved_klasses(NULL);
    99 
    94 
   100   MetadataFactory::free_array<jushort>(loader_data, operands());
    95   MetadataFactory::free_array<jushort>(loader_data, operands());
   101   set_operands(NULL);
    96   set_operands(NULL);
   102 
    97 
   103   release_C_heap_structures();
    98   release_C_heap_structures();
   111   // walk constant pool and decrement symbol reference counts
   106   // walk constant pool and decrement symbol reference counts
   112   unreference_symbols();
   107   unreference_symbols();
   113 }
   108 }
   114 
   109 
   115 objArrayOop ConstantPool::resolved_references() const {
   110 objArrayOop ConstantPool::resolved_references() const {
   116   return (objArrayOop)JNIHandles::resolve(_resolved_references);
   111   return (objArrayOop)JNIHandles::resolve(_cache->resolved_references());
   117 }
   112 }
   118 
   113 
   119 // Create resolved_references array and mapping array for original cp indexes
   114 // Create resolved_references array and mapping array for original cp indexes
   120 // The ldc bytecode was rewritten to have the resolved reference array index so need a way
   115 // The ldc bytecode was rewritten to have the resolved reference array index so need a way
   121 // to map it back for resolving and some unlikely miscellaneous uses.
   116 // to map it back for resolving and some unlikely miscellaneous uses.
   148     Handle refs_handle (THREAD, (oop)stom);  // must handleize.
   143     Handle refs_handle (THREAD, (oop)stom);  // must handleize.
   149     set_resolved_references(loader_data->add_handle(refs_handle));
   144     set_resolved_references(loader_data->add_handle(refs_handle));
   150   }
   145   }
   151 }
   146 }
   152 
   147 
       
   148 void ConstantPool::allocate_resolved_klasses(ClassLoaderData* loader_data, int num_klasses, TRAPS) {
       
   149   // A ConstantPool can't possibly have 0xffff valid class entries,
       
   150   // because entry #0 must be CONSTANT_Invalid, and each class entry must refer to a UTF8
       
   151   // entry for the class's name. So at most we will have 0xfffe class entries.
       
   152   // This allows us to use 0xffff (ConstantPool::_temp_resolved_klass_index) to indicate
       
   153   // UnresolvedKlass entries that are temporarily created during class redefinition.
       
   154   assert(num_klasses < CPKlassSlot::_temp_resolved_klass_index, "sanity");
       
   155   assert(resolved_klasses() == NULL, "sanity");
       
   156   Array<Klass*>* rk = MetadataFactory::new_writeable_array<Klass*>(loader_data, num_klasses, CHECK);
       
   157   set_resolved_klasses(rk);
       
   158 }
       
   159 
       
   160 void ConstantPool::initialize_unresolved_klasses(ClassLoaderData* loader_data, TRAPS) {
       
   161   int len = length();
       
   162   int num_klasses = 0;
       
   163   for (int i = 1; i <len; i++) {
       
   164     switch (tag_at(i).value()) {
       
   165     case JVM_CONSTANT_ClassIndex:
       
   166       {
       
   167         const int class_index = klass_index_at(i);
       
   168         unresolved_klass_at_put(i, class_index, num_klasses++);
       
   169       }
       
   170       break;
       
   171 #ifndef PRODUCT
       
   172     case JVM_CONSTANT_Class:
       
   173     case JVM_CONSTANT_UnresolvedClass:
       
   174     case JVM_CONSTANT_UnresolvedClassInError:
       
   175       // All of these should have been reverted back to ClassIndex before calling
       
   176       // this function.
       
   177       ShouldNotReachHere();
       
   178 #endif
       
   179     }
       
   180   }
       
   181   allocate_resolved_klasses(loader_data, num_klasses, THREAD);
       
   182 }
       
   183 
       
   184 // Anonymous class support:
       
   185 void ConstantPool::klass_at_put(int class_index, int name_index, int resolved_klass_index, Klass* k, Symbol* name) {
       
   186   assert(is_within_bounds(class_index), "index out of bounds");
       
   187   assert(is_within_bounds(name_index), "index out of bounds");
       
   188   assert((resolved_klass_index & 0xffff0000) == 0, "must be");
       
   189   *int_at_addr(class_index) =
       
   190     build_int_from_shorts((jushort)resolved_klass_index, (jushort)name_index);
       
   191 
       
   192   symbol_at_put(name_index, name);
       
   193   name->increment_refcount();
       
   194   Klass** adr = resolved_klasses()->adr_at(resolved_klass_index);
       
   195   OrderAccess::release_store_ptr((Klass* volatile *)adr, k);
       
   196 
       
   197   // The interpreter assumes when the tag is stored, the klass is resolved
       
   198   // and the Klass* non-NULL, so we need hardware store ordering here.
       
   199   if (k != NULL) {
       
   200     release_tag_at_put(class_index, JVM_CONSTANT_Class);
       
   201   } else {
       
   202     release_tag_at_put(class_index, JVM_CONSTANT_UnresolvedClass);
       
   203   }
       
   204 }
       
   205 
       
   206 // Anonymous class support:
       
   207 void ConstantPool::klass_at_put(int class_index, Klass* k) {
       
   208   assert(k != NULL, "must be valid klass");
       
   209   CPKlassSlot kslot = klass_slot_at(class_index);
       
   210   int resolved_klass_index = kslot.resolved_klass_index();
       
   211   Klass** adr = resolved_klasses()->adr_at(resolved_klass_index);
       
   212   OrderAccess::release_store_ptr((Klass* volatile *)adr, k);
       
   213 
       
   214   // The interpreter assumes when the tag is stored, the klass is resolved
       
   215   // and the Klass* non-NULL, so we need hardware store ordering here.
       
   216   release_tag_at_put(class_index, JVM_CONSTANT_Class);
       
   217 }
       
   218 
   153 // CDS support. Create a new resolved_references array.
   219 // CDS support. Create a new resolved_references array.
   154 void ConstantPool::restore_unshareable_info(TRAPS) {
   220 void ConstantPool::restore_unshareable_info(TRAPS) {
   155   assert(is_constantPool(), "ensure C++ vtable is restored");
   221   assert(is_constantPool(), "ensure C++ vtable is restored");
       
   222   assert(on_stack(), "should always be set for shared constant pools");
       
   223   assert(is_shared(), "should always be set for shared constant pools");
   156 
   224 
   157   // Only create the new resolved references array if it hasn't been attempted before
   225   // Only create the new resolved references array if it hasn't been attempted before
   158   if (resolved_references() != NULL) return;
   226   if (resolved_references() != NULL) return;
   159 
   227 
   160   // restore the C++ vtable from the shared archive
   228   // restore the C++ vtable from the shared archive
   178   // Save the length for restoration.  It is not necessarily the same length
   246   // Save the length for restoration.  It is not necessarily the same length
   179   // as reference_map.length() if invokedynamic is saved.
   247   // as reference_map.length() if invokedynamic is saved.
   180   set_resolved_reference_length(
   248   set_resolved_reference_length(
   181     resolved_references() != NULL ? resolved_references()->length() : 0);
   249     resolved_references() != NULL ? resolved_references()->length() : 0);
   182   set_resolved_references(NULL);
   250   set_resolved_references(NULL);
       
   251 
       
   252   // Shared ConstantPools are in the RO region, so the _flags cannot be modified.
       
   253   // The _on_stack flag is used to prevent ConstantPools from deallocation during
       
   254   // class redefinition. Since shared ConstantPools cannot be deallocated anyway,
       
   255   // we always set _on_stack to true to avoid having to change _flags during runtime.
       
   256   _flags |= (_on_stack | _is_shared);
   183 }
   257 }
   184 
   258 
   185 int ConstantPool::cp_to_object_index(int cp_index) {
   259 int ConstantPool::cp_to_object_index(int cp_index) {
   186   // this is harder don't do this so much.
   260   // this is harder don't do this so much.
   187   int i = reference_map()->find(cp_index);
   261   int i = reference_map()->find(cp_index);
   227   assert(THREAD->is_Java_thread(), "must be a Java thread");
   301   assert(THREAD->is_Java_thread(), "must be a Java thread");
   228 
   302 
   229   // A resolved constantPool entry will contain a Klass*, otherwise a Symbol*.
   303   // A resolved constantPool entry will contain a Klass*, otherwise a Symbol*.
   230   // It is not safe to rely on the tag bit's here, since we don't have a lock, and
   304   // It is not safe to rely on the tag bit's here, since we don't have a lock, and
   231   // the entry and tag is not updated atomicly.
   305   // the entry and tag is not updated atomicly.
   232   CPSlot entry = this_cp->slot_at(which);
   306   CPKlassSlot kslot = this_cp->klass_slot_at(which);
   233   if (entry.is_resolved()) {
   307   int resolved_klass_index = kslot.resolved_klass_index();
   234     assert(entry.get_klass()->is_klass(), "must be");
   308   int name_index = kslot.name_index();
   235     // Already resolved - return entry.
   309   assert(this_cp->tag_at(name_index).is_symbol(), "sanity");
   236     return entry.get_klass();
   310 
       
   311   Klass* klass = this_cp->resolved_klasses()->at(resolved_klass_index);
       
   312   if (klass != NULL) {
       
   313     return klass;
   237   }
   314   }
   238 
   315 
   239   // This tag doesn't change back to unresolved class unless at a safepoint.
   316   // This tag doesn't change back to unresolved class unless at a safepoint.
   240   if (this_cp->tag_at(which).is_unresolved_klass_in_error()) {
   317   if (this_cp->tag_at(which).is_unresolved_klass_in_error()) {
   241     // The original attempt to resolve this constant pool entry failed so find the
   318     // The original attempt to resolve this constant pool entry failed so find the
   249     throw_resolution_error(this_cp, which, CHECK_0);
   326     throw_resolution_error(this_cp, which, CHECK_0);
   250     ShouldNotReachHere();
   327     ShouldNotReachHere();
   251   }
   328   }
   252 
   329 
   253   Handle mirror_handle;
   330   Handle mirror_handle;
   254   Symbol* name = entry.get_symbol();
   331   Symbol* name = this_cp->symbol_at(name_index);
   255   Handle loader (THREAD, this_cp->pool_holder()->class_loader());
   332   Handle loader (THREAD, this_cp->pool_holder()->class_loader());
   256   Handle protection_domain (THREAD, this_cp->pool_holder()->protection_domain());
   333   Handle protection_domain (THREAD, this_cp->pool_holder()->protection_domain());
   257   Klass* k = SystemDictionary::resolve_or_fail(name, loader, protection_domain, true, THREAD);
   334   Klass* k = SystemDictionary::resolve_or_fail(name, loader, protection_domain, true, THREAD);
   258   if (!HAS_PENDING_EXCEPTION) {
   335   if (!HAS_PENDING_EXCEPTION) {
   259     // preserve the resolved klass from unloading
   336     // preserve the resolved klass from unloading
   268     if (save_resolution_error) {
   345     if (save_resolution_error) {
   269       save_and_throw_exception(this_cp, which, constantTag(JVM_CONSTANT_UnresolvedClass), CHECK_NULL);
   346       save_and_throw_exception(this_cp, which, constantTag(JVM_CONSTANT_UnresolvedClass), CHECK_NULL);
   270       // If CHECK_NULL above doesn't return the exception, that means that
   347       // If CHECK_NULL above doesn't return the exception, that means that
   271       // some other thread has beaten us and has resolved the class.
   348       // some other thread has beaten us and has resolved the class.
   272       // To preserve old behavior, we return the resolved class.
   349       // To preserve old behavior, we return the resolved class.
   273       entry = this_cp->resolved_klass_at(which);
   350       klass = this_cp->resolved_klasses()->at(resolved_klass_index);
   274       assert(entry.is_resolved(), "must be resolved if exception was cleared");
   351       assert(klass != NULL, "must be resolved if exception was cleared");
   275       assert(entry.get_klass()->is_klass(), "must be resolved to a klass");
   352       return klass;
   276       return entry.get_klass();
       
   277     } else {
   353     } else {
   278       return NULL;  // return the pending exception
   354       return NULL;  // return the pending exception
   279     }
   355     }
   280   }
   356   }
   281 
   357 
   285 
   361 
   286   // logging for class+resolve.
   362   // logging for class+resolve.
   287   if (log_is_enabled(Debug, class, resolve)){
   363   if (log_is_enabled(Debug, class, resolve)){
   288     trace_class_resolution(this_cp, k);
   364     trace_class_resolution(this_cp, k);
   289   }
   365   }
   290   this_cp->klass_at_put(which, k);
   366   Klass** adr = this_cp->resolved_klasses()->adr_at(resolved_klass_index);
   291   entry = this_cp->resolved_klass_at(which);
   367   OrderAccess::release_store_ptr((Klass* volatile *)adr, k);
   292   assert(entry.is_resolved() && entry.get_klass()->is_klass(), "must be resolved at this point");
   368   // The interpreter assumes when the tag is stored, the klass is resolved
   293   return entry.get_klass();
   369   // and the Klass* stored in _resolved_klasses is non-NULL, so we need
       
   370   // hardware store ordering here.
       
   371   this_cp->release_tag_at_put(which, JVM_CONSTANT_Class);
       
   372   return k;
   294 }
   373 }
   295 
   374 
   296 
   375 
   297 // Does not update ConstantPool* - to avoid any exception throwing. Used
   376 // Does not update ConstantPool* - to avoid any exception throwing. Used
   298 // by compiler and exception handling.  Also used to avoid classloads for
   377 // by compiler and exception handling.  Also used to avoid classloads for
   299 // instanceof operations. Returns NULL if the class has not been loaded or
   378 // instanceof operations. Returns NULL if the class has not been loaded or
   300 // if the verification of constant pool failed
   379 // if the verification of constant pool failed
   301 Klass* ConstantPool::klass_at_if_loaded(const constantPoolHandle& this_cp, int which) {
   380 Klass* ConstantPool::klass_at_if_loaded(const constantPoolHandle& this_cp, int which) {
   302   CPSlot entry = this_cp->slot_at(which);
   381   CPKlassSlot kslot = this_cp->klass_slot_at(which);
   303   if (entry.is_resolved()) {
   382   int resolved_klass_index = kslot.resolved_klass_index();
   304     assert(entry.get_klass()->is_klass(), "must be");
   383   int name_index = kslot.name_index();
   305     return entry.get_klass();
   384   assert(this_cp->tag_at(name_index).is_symbol(), "sanity");
       
   385 
       
   386   Klass* k = this_cp->resolved_klasses()->at(resolved_klass_index);
       
   387   if (k != NULL) {
       
   388     return k;
   306   } else {
   389   } else {
   307     assert(entry.is_unresolved(), "must be either symbol or klass");
       
   308     Thread *thread = Thread::current();
   390     Thread *thread = Thread::current();
   309     Symbol* name = entry.get_symbol();
   391     Symbol* name = this_cp->symbol_at(name_index);
   310     oop loader = this_cp->pool_holder()->class_loader();
   392     oop loader = this_cp->pool_holder()->class_loader();
   311     oop protection_domain = this_cp->pool_holder()->protection_domain();
   393     oop protection_domain = this_cp->pool_holder()->protection_domain();
   312     Handle h_prot (thread, protection_domain);
   394     Handle h_prot (thread, protection_domain);
   313     Handle h_loader (thread, loader);
   395     Handle h_loader (thread, loader);
   314     Klass* k = SystemDictionary::find(name, h_loader, h_prot, thread);
   396     Klass* k = SystemDictionary::find(name, h_loader, h_prot, thread);
   482 
   564 
   483 Klass* ConstantPool::klass_ref_at(int which, TRAPS) {
   565 Klass* ConstantPool::klass_ref_at(int which, TRAPS) {
   484   return klass_at(klass_ref_index_at(which), THREAD);
   566   return klass_at(klass_ref_index_at(which), THREAD);
   485 }
   567 }
   486 
   568 
   487 
       
   488 Symbol* ConstantPool::klass_name_at(int which) const {
   569 Symbol* ConstantPool::klass_name_at(int which) const {
   489   assert(tag_at(which).is_unresolved_klass() || tag_at(which).is_klass(),
   570   return symbol_at(klass_slot_at(which).name_index());
   490          "Corrupted constant pool");
       
   491   // A resolved constantPool entry will contain a Klass*, otherwise a Symbol*.
       
   492   // It is not safe to rely on the tag bit's here, since we don't have a lock, and the entry and
       
   493   // tag is not updated atomicly.
       
   494   CPSlot entry = slot_at(which);
       
   495   if (entry.is_resolved()) {
       
   496     // Already resolved - return entry's name.
       
   497     assert(entry.get_klass()->is_klass(), "must be");
       
   498     return entry.get_klass()->name();
       
   499   } else {
       
   500     assert(entry.is_unresolved(), "must be either symbol or klass");
       
   501     return entry.get_symbol();
       
   502   }
       
   503 }
   571 }
   504 
   572 
   505 Symbol* ConstantPool::klass_ref_at_noresolve(int which) {
   573 Symbol* ConstantPool::klass_ref_at_noresolve(int which) {
   506   jint ref_index = klass_ref_index_at(which);
   574   jint ref_index = klass_ref_index_at(which);
   507   return klass_at_noresolve(ref_index);
   575   return klass_at_noresolve(ref_index);
   848 }
   916 }
   849 
   917 
   850 
   918 
   851 // Iterate over symbols and decrement ones which are Symbol*s
   919 // Iterate over symbols and decrement ones which are Symbol*s
   852 // This is done during GC.
   920 // This is done during GC.
   853 // Only decrement the UTF8 symbols. Unresolved classes and strings point to
   921 // Only decrement the UTF8 symbols. Strings point to
   854 // these symbols but didn't increment the reference count.
   922 // these symbols but didn't increment the reference count.
   855 void ConstantPool::unreference_symbols() {
   923 void ConstantPool::unreference_symbols() {
   856   for (int index = 1; index < length(); index++) { // Index 0 is unused
   924   for (int index = 1; index < length(); index++) { // Index 0 is unused
   857     constantTag tag = tag_at(index);
   925     constantTag tag = tag_at(index);
   858     if (tag.is_symbol()) {
   926     if (tag.is_symbol()) {
  1229                                         const constantPoolHandle& to_cp, int to_i,
  1297                                         const constantPoolHandle& to_cp, int to_i,
  1230                                         TRAPS) {
  1298                                         TRAPS) {
  1231 
  1299 
  1232   int tag = from_cp->tag_at(from_i).value();
  1300   int tag = from_cp->tag_at(from_i).value();
  1233   switch (tag) {
  1301   switch (tag) {
  1234   case JVM_CONSTANT_Class:
       
  1235   {
       
  1236     Klass* k = from_cp->klass_at(from_i, CHECK);
       
  1237     to_cp->klass_at_put(to_i, k);
       
  1238   } break;
       
  1239 
       
  1240   case JVM_CONSTANT_ClassIndex:
  1302   case JVM_CONSTANT_ClassIndex:
  1241   {
  1303   {
  1242     jint ki = from_cp->klass_index_at(from_i);
  1304     jint ki = from_cp->klass_index_at(from_i);
  1243     to_cp->klass_index_at_put(to_i, ki);
  1305     to_cp->klass_index_at_put(to_i, ki);
  1244   } break;
  1306   } break;
  1303   {
  1365   {
  1304     jint si = from_cp->string_index_at(from_i);
  1366     jint si = from_cp->string_index_at(from_i);
  1305     to_cp->string_index_at_put(to_i, si);
  1367     to_cp->string_index_at_put(to_i, si);
  1306   } break;
  1368   } break;
  1307 
  1369 
       
  1370   case JVM_CONSTANT_Class:
  1308   case JVM_CONSTANT_UnresolvedClass:
  1371   case JVM_CONSTANT_UnresolvedClass:
  1309   case JVM_CONSTANT_UnresolvedClassInError:
  1372   case JVM_CONSTANT_UnresolvedClassInError:
  1310   {
  1373   {
  1311     // Can be resolved after checking tag, so check the slot first.
  1374     // Revert to JVM_CONSTANT_ClassIndex
  1312     CPSlot entry = from_cp->slot_at(from_i);
  1375     int name_index = from_cp->klass_slot_at(from_i).name_index();
  1313     if (entry.is_resolved()) {
  1376     assert(from_cp->tag_at(name_index).is_symbol(), "sanity");
  1314       assert(entry.get_klass()->is_klass(), "must be");
  1377     to_cp->klass_index_at_put(to_i, name_index);
  1315       // Already resolved
       
  1316       to_cp->klass_at_put(to_i, entry.get_klass());
       
  1317     } else {
       
  1318       to_cp->unresolved_klass_at_put(to_i, entry.get_symbol());
       
  1319     }
       
  1320   } break;
  1378   } break;
  1321 
  1379 
  1322   case JVM_CONSTANT_String:
  1380   case JVM_CONSTANT_String:
  1323   {
  1381   {
  1324     Symbol* s = from_cp->unresolved_string_at(from_i);
  1382     Symbol* s = from_cp->unresolved_string_at(from_i);
  1365   {
  1423   {
  1366     ShouldNotReachHere();
  1424     ShouldNotReachHere();
  1367   } break;
  1425   } break;
  1368   }
  1426   }
  1369 } // end copy_entry_to()
  1427 } // end copy_entry_to()
  1370 
       
  1371 
  1428 
  1372 // Search constant pool search_cp for an entry that matches this
  1429 // Search constant pool search_cp for an entry that matches this
  1373 // constant pool's entry at pattern_i. Returns the index of a
  1430 // constant pool's entry at pattern_i. Returns the index of a
  1374 // matching entry or zero (0) if there is no matching entry.
  1431 // matching entry or zero (0) if there is no matching entry.
  1375 int ConstantPool::find_matching_entry(int pattern_i,
  1432 int ConstantPool::find_matching_entry(int pattern_i,
  1822 
  1879 
  1823 void ConstantPool::set_on_stack(const bool value) {
  1880 void ConstantPool::set_on_stack(const bool value) {
  1824   if (value) {
  1881   if (value) {
  1825     // Only record if it's not already set.
  1882     // Only record if it's not already set.
  1826     if (!on_stack()) {
  1883     if (!on_stack()) {
       
  1884       assert(!is_shared(), "should always be set for shared constant pools");
  1827       _flags |= _on_stack;
  1885       _flags |= _on_stack;
  1828       MetadataOnStackMark::record(this);
  1886       MetadataOnStackMark::record(this);
  1829     }
  1887     }
  1830   } else {
  1888   } else {
  1831     // Clearing is done single-threadedly.
  1889     // Clearing is done single-threadedly.
  1832     _flags &= ~_on_stack;
  1890     if (!is_shared()) {
       
  1891       _flags &= ~_on_stack;
       
  1892     }
  1833   }
  1893   }
  1834 }
  1894 }
  1835 
  1895 
  1836 // JSR 292 support for patching constant pool oops after the class is linked and
  1896 // JSR 292 support for patching constant pool oops after the class is linked and
  1837 // the oop array for resolved references are created.
  1897 // the oop array for resolved references are created.
  1903     st->print_cr(" - holder: " INTPTR_FORMAT, p2i(pool_holder()));
  1963     st->print_cr(" - holder: " INTPTR_FORMAT, p2i(pool_holder()));
  1904   }
  1964   }
  1905   st->print_cr(" - cache: " INTPTR_FORMAT, p2i(cache()));
  1965   st->print_cr(" - cache: " INTPTR_FORMAT, p2i(cache()));
  1906   st->print_cr(" - resolved_references: " INTPTR_FORMAT, p2i(resolved_references()));
  1966   st->print_cr(" - resolved_references: " INTPTR_FORMAT, p2i(resolved_references()));
  1907   st->print_cr(" - reference_map: " INTPTR_FORMAT, p2i(reference_map()));
  1967   st->print_cr(" - reference_map: " INTPTR_FORMAT, p2i(reference_map()));
       
  1968   st->print_cr(" - resolved_klasses: " INTPTR_FORMAT, p2i(resolved_klasses()));
  1908 
  1969 
  1909   for (int index = 1; index < length(); index++) {      // Index 0 is unused
  1970   for (int index = 1; index < length(); index++) {      // Index 0 is unused
  1910     ((ConstantPool*)this)->print_entry_on(index, st);
  1971     ((ConstantPool*)this)->print_entry_on(index, st);
  1911     switch (tag_at(index).value()) {
  1972     switch (tag_at(index).value()) {
  1912       case JVM_CONSTANT_Long :
  1973       case JVM_CONSTANT_Long :
  1964       st->print(" signature_index=%d", signature_ref_index_at(index));
  2025       st->print(" signature_index=%d", signature_ref_index_at(index));
  1965       break;
  2026       break;
  1966     case JVM_CONSTANT_Utf8 :
  2027     case JVM_CONSTANT_Utf8 :
  1967       symbol_at(index)->print_value_on(st);
  2028       symbol_at(index)->print_value_on(st);
  1968       break;
  2029       break;
       
  2030     case JVM_CONSTANT_ClassIndex: {
       
  2031         int name_index = *int_at_addr(index);
       
  2032         st->print("klass_index=%d ", name_index);
       
  2033         symbol_at(name_index)->print_value_on(st);
       
  2034       }
       
  2035       break;
  1969     case JVM_CONSTANT_UnresolvedClass :               // fall-through
  2036     case JVM_CONSTANT_UnresolvedClass :               // fall-through
  1970     case JVM_CONSTANT_UnresolvedClassInError: {
  2037     case JVM_CONSTANT_UnresolvedClassInError: {
  1971       CPSlot entry = slot_at(index);
  2038         CPKlassSlot kslot = klass_slot_at(index);
  1972       if (entry.is_resolved()) {
  2039         int resolved_klass_index = kslot.resolved_klass_index();
  1973         entry.get_klass()->print_value_on(st);
  2040         int name_index = kslot.name_index();
  1974       } else {
  2041         assert(tag_at(name_index).is_symbol(), "sanity");
  1975         entry.get_symbol()->print_value_on(st);
  2042 
  1976       }
  2043         Klass* klass = resolved_klasses()->at(resolved_klass_index);
       
  2044         if (klass != NULL) {
       
  2045           klass->print_value_on(st);
       
  2046         } else {
       
  2047           symbol_at(name_index)->print_value_on(st);
       
  2048         }
  1977       }
  2049       }
  1978       break;
  2050       break;
  1979     case JVM_CONSTANT_MethodHandle :
  2051     case JVM_CONSTANT_MethodHandle :
  1980     case JVM_CONSTANT_MethodHandleInError :
  2052     case JVM_CONSTANT_MethodHandleInError :
  1981       st->print("ref_kind=%d", method_handle_ref_kind_at(index));
  2053       st->print("ref_kind=%d", method_handle_ref_kind_at(index));
  2042 
  2114 
  2043 void ConstantPool::verify_on(outputStream* st) {
  2115 void ConstantPool::verify_on(outputStream* st) {
  2044   guarantee(is_constantPool(), "object must be constant pool");
  2116   guarantee(is_constantPool(), "object must be constant pool");
  2045   for (int i = 0; i< length();  i++) {
  2117   for (int i = 0; i< length();  i++) {
  2046     constantTag tag = tag_at(i);
  2118     constantTag tag = tag_at(i);
  2047     CPSlot entry = slot_at(i);
  2119     if (tag.is_klass() || tag.is_unresolved_klass()) {
  2048     if (tag.is_klass()) {
  2120       guarantee(klass_name_at(i)->refcount() != 0, "should have nonzero reference count");
  2049       if (entry.is_resolved()) {
       
  2050         guarantee(entry.get_klass()->is_klass(),    "should be klass");
       
  2051       }
       
  2052     } else if (tag.is_unresolved_klass()) {
       
  2053       if (entry.is_resolved()) {
       
  2054         guarantee(entry.get_klass()->is_klass(),    "should be klass");
       
  2055       }
       
  2056     } else if (tag.is_symbol()) {
  2121     } else if (tag.is_symbol()) {
       
  2122       CPSlot entry = slot_at(i);
  2057       guarantee(entry.get_symbol()->refcount() != 0, "should have nonzero reference count");
  2123       guarantee(entry.get_symbol()->refcount() != 0, "should have nonzero reference count");
  2058     } else if (tag.is_string()) {
  2124     } else if (tag.is_string()) {
       
  2125       CPSlot entry = slot_at(i);
  2059       guarantee(entry.get_symbol()->refcount() != 0, "should have nonzero reference count");
  2126       guarantee(entry.get_symbol()->refcount() != 0, "should have nonzero reference count");
  2060     }
  2127     }
  2061   }
  2128   }
  2062   if (cache() != NULL) {
  2129   if (cache() != NULL) {
  2063     // Note: cache() can be NULL before a class is completely setup or
  2130     // Note: cache() can be NULL before a class is completely setup or