< prev index next >

src/share/vm/oops/constantPool.cpp

Print this page


  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 "memory/resourceArea.hpp"
  37 #include "oops/constantPool.hpp"
  38 #include "oops/instanceKlass.hpp"
  39 #include "oops/objArrayKlass.hpp"
  40 #include "oops/objArrayOop.inline.hpp"
  41 #include "oops/oop.inline.hpp"
  42 #include "runtime/fieldType.hpp"
  43 #include "runtime/init.hpp"
  44 #include "runtime/javaCalls.hpp"
  45 #include "runtime/signature.hpp"
  46 #include "runtime/vframe.hpp"
  47 #include "utilities/copy.hpp"
  48 
  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);
  52 
  53   int size = ConstantPool::size(length);
  54 
  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 }
  63 
  64 #ifdef ASSERT
  65 
  66 // MetaspaceObj allocation invariant is calloc equivalent memory
  67 // simple verification of this here (JVM_CONSTANT_Invalid == 0 )
  68 static bool tag_array_is_zero_initialized(Array<u1>* tags) {
  69   assert(tags != NULL, "invariant");
  70   const int length = tags->length();
  71   for (int index = 0; index < length; ++index) {
  72     if (JVM_CONSTANT_Invalid != tags->at(index)) {
  73       return false;
  74     }
  75   }
  76   return true;
  77 }
  78 
  79 #endif
  80 
  81 ConstantPool::ConstantPool(Array<u1>* tags) :
  82   _tags(tags),
  83   _length(tags->length()),
  84   _flags(0) {
  85 
  86     assert(_tags != NULL, "invariant");
  87     assert(tags->length() == _length, "invariant");
  88     assert(tag_array_is_zero_initialized(tags), "invariant");
  89     assert(0 == _flags, "invariant");
  90     assert(0 == version(), "invariant");
  91     assert(NULL == _pool_holder, "invariant");
  92 }
  93 
  94 void ConstantPool::deallocate_contents(ClassLoaderData* loader_data) {
  95   MetadataFactory::free_metadata(loader_data, cache());
  96   set_cache(NULL);
  97   MetadataFactory::free_array<u2>(loader_data, reference_map());
  98   set_reference_map(NULL);






  99 
 100   MetadataFactory::free_array<jushort>(loader_data, operands());
 101   set_operands(NULL);
 102 
 103   release_C_heap_structures();
 104 
 105   // free tag array
 106   MetadataFactory::free_array<u1>(loader_data, tags());
 107   set_tags(NULL);
 108 }
 109 
 110 void ConstantPool::release_C_heap_structures() {
 111   // walk constant pool and decrement symbol reference counts
 112   unreference_symbols();
 113 }
 114 
 115 objArrayOop ConstantPool::resolved_references() const {
 116   return (objArrayOop)JNIHandles::resolve(_resolved_references);
 117 }
 118 
 119 // 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
 121 // to map it back for resolving and some unlikely miscellaneous uses.
 122 // The objects created by invokedynamic are appended to this list.
 123 void ConstantPool::initialize_resolved_references(ClassLoaderData* loader_data,
 124                                                   const intStack& reference_map,
 125                                                   int constant_pool_map_length,
 126                                                   TRAPS) {
 127   // Initialized the resolved object cache.
 128   int map_length = reference_map.length();
 129   if (map_length > 0) {
 130     // Only need mapping back to constant pool entries.  The map isn't used for
 131     // invokedynamic resolved_reference entries.  For invokedynamic entries,
 132     // the constant pool cache index has the mapping back to both the constant
 133     // pool and to the resolved reference index.
 134     if (constant_pool_map_length > 0) {
 135       Array<u2>* om = MetadataFactory::new_array<u2>(loader_data, constant_pool_map_length, CHECK);
 136 
 137       for (int i = 0; i < constant_pool_map_length; i++) {
 138         int x = reference_map.at(i);
 139         assert(x == (int)(jushort) x, "klass index is too big");
 140         om->at_put(i, (jushort)x);
 141       }
 142       set_reference_map(om);
 143     }
 144 
 145     // Create Java array for holding resolved strings, methodHandles,
 146     // methodTypes, invokedynamic and invokehandle appendix objects, etc.
 147     objArrayOop stom = oopFactory::new_objArray(SystemDictionary::Object_klass(), map_length, CHECK);
 148     Handle refs_handle (THREAD, (oop)stom);  // must handleize.
 149     set_resolved_references(loader_data->add_handle(refs_handle));
 150   }
 151 }
 152 

























































 153 // CDS support. Create a new resolved_references array.
 154 void ConstantPool::restore_unshareable_info(TRAPS) {
 155   assert(is_constantPool(), "ensure C++ vtable is restored");


 156 
 157   // Only create the new resolved references array if it hasn't been attempted before
 158   if (resolved_references() != NULL) return;
 159 
 160   // restore the C++ vtable from the shared archive
 161   restore_vtable();
 162 
 163   if (SystemDictionary::Object_klass_loaded()) {
 164     // Recreate the object array and add to ClassLoaderData.
 165     int map_length = resolved_reference_length();
 166     if (map_length > 0) {
 167       objArrayOop stom = oopFactory::new_objArray(SystemDictionary::Object_klass(), map_length, CHECK);
 168       Handle refs_handle (THREAD, (oop)stom);  // must handleize.
 169 
 170       ClassLoaderData* loader_data = pool_holder()->class_loader_data();
 171       set_resolved_references(loader_data->add_handle(refs_handle));
 172     }
 173   }
 174 }
 175 
 176 void ConstantPool::remove_unshareable_info() {
 177   // Resolved references are not in the shared archive.
 178   // Save the length for restoration.  It is not necessarily the same length
 179   // as reference_map.length() if invokedynamic is saved.
 180   set_resolved_reference_length(
 181     resolved_references() != NULL ? resolved_references()->length() : 0);
 182   set_resolved_references(NULL);






 183 }
 184 
 185 int ConstantPool::cp_to_object_index(int cp_index) {
 186   // this is harder don't do this so much.
 187   int i = reference_map()->find(cp_index);
 188   // We might not find the index for jsr292 call.
 189   return (i < 0) ? _no_index_sentinel : i;
 190 }
 191 
 192 void ConstantPool::string_at_put(int which, int obj_index, oop str) {
 193   resolved_references()->obj_at_put(obj_index, str);
 194 }
 195 
 196 void ConstantPool::trace_class_resolution(const constantPoolHandle& this_cp, Klass* k) {
 197   ResourceMark rm;
 198   int line_number = -1;
 199   const char * source_file = NULL;
 200   if (JavaThread::current()->has_last_Java_frame()) {
 201     // try to identify the method which called this function.
 202     vframeStream vfst(JavaThread::current());


 212     // only print something if the classes are different
 213     if (source_file != NULL) {
 214       log_debug(class, resolve)("%s %s %s:%d",
 215                  this_cp->pool_holder()->external_name(),
 216                  k->external_name(), source_file, line_number);
 217     } else {
 218       log_debug(class, resolve)("%s %s",
 219                  this_cp->pool_holder()->external_name(),
 220                  k->external_name());
 221     }
 222   }
 223 }
 224 
 225 Klass* ConstantPool::klass_at_impl(const constantPoolHandle& this_cp, int which,
 226                                    bool save_resolution_error, TRAPS) {
 227   assert(THREAD->is_Java_thread(), "must be a Java thread");
 228 
 229   // 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
 231   // the entry and tag is not updated atomicly.
 232   CPSlot entry = this_cp->slot_at(which);
 233   if (entry.is_resolved()) {
 234     assert(entry.get_klass()->is_klass(), "must be");
 235     // Already resolved - return entry.
 236     return entry.get_klass();



 237   }
 238 
 239   // 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()) {
 241     // The original attempt to resolve this constant pool entry failed so find the
 242     // class of the original error and throw another error of the same class
 243     // (JVMS 5.4.3).
 244     // If there is a detail message, pass that detail message to the error.
 245     // The JVMS does not strictly require us to duplicate the same detail message,
 246     // or any internal exception fields such as cause or stacktrace.  But since the
 247     // detail message is often a class name or other literal string, we will repeat it
 248     // if we can find it in the symbol table.
 249     throw_resolution_error(this_cp, which, CHECK_0);
 250     ShouldNotReachHere();
 251   }
 252 
 253   Handle mirror_handle;
 254   Symbol* name = entry.get_symbol();
 255   Handle loader (THREAD, this_cp->pool_holder()->class_loader());
 256   Handle protection_domain (THREAD, this_cp->pool_holder()->protection_domain());
 257   Klass* k = SystemDictionary::resolve_or_fail(name, loader, protection_domain, true, THREAD);
 258   if (!HAS_PENDING_EXCEPTION) {
 259     // preserve the resolved klass from unloading
 260     mirror_handle = Handle(THREAD, k->java_mirror());
 261     // Do access check for klasses
 262     verify_constant_pool_resolve(this_cp, k, THREAD);
 263   }
 264 
 265   // Failed to resolve class. We must record the errors so that subsequent attempts
 266   // to resolve this constant pool entry fail with the same error (JVMS 5.4.3).
 267   if (HAS_PENDING_EXCEPTION) {
 268     if (save_resolution_error) {
 269       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
 271       // some other thread has beaten us and has resolved the class.
 272       // To preserve old behavior, we return the resolved class.
 273       entry = this_cp->resolved_klass_at(which);
 274       assert(entry.is_resolved(), "must be resolved if exception was cleared");
 275       assert(entry.get_klass()->is_klass(), "must be resolved to a klass");
 276       return entry.get_klass();
 277     } else {
 278       return NULL;  // return the pending exception
 279     }
 280   }
 281 
 282   // Make this class loader depend upon the class loader owning the class reference
 283   ClassLoaderData* this_key = this_cp->pool_holder()->class_loader_data();
 284   this_key->record_dependency(k, CHECK_NULL); // Can throw OOM
 285 
 286   // logging for class+resolve.
 287   if (log_is_enabled(Debug, class, resolve)){
 288     trace_class_resolution(this_cp, k);
 289   }
 290   this_cp->klass_at_put(which, k);
 291   entry = this_cp->resolved_klass_at(which);
 292   assert(entry.is_resolved() && entry.get_klass()->is_klass(), "must be resolved at this point");
 293   return entry.get_klass();



 294 }
 295 
 296 
 297 // Does not update ConstantPool* - to avoid any exception throwing. Used
 298 // by compiler and exception handling.  Also used to avoid classloads for
 299 // instanceof operations. Returns NULL if the class has not been loaded or
 300 // if the verification of constant pool failed
 301 Klass* ConstantPool::klass_at_if_loaded(const constantPoolHandle& this_cp, int which) {
 302   CPSlot entry = this_cp->slot_at(which);
 303   if (entry.is_resolved()) {
 304     assert(entry.get_klass()->is_klass(), "must be");
 305     return entry.get_klass();




 306   } else {
 307     assert(entry.is_unresolved(), "must be either symbol or klass");
 308     Thread *thread = Thread::current();
 309     Symbol* name = entry.get_symbol();
 310     oop loader = this_cp->pool_holder()->class_loader();
 311     oop protection_domain = this_cp->pool_holder()->protection_domain();
 312     Handle h_prot (thread, protection_domain);
 313     Handle h_loader (thread, loader);
 314     Klass* k = SystemDictionary::find(name, h_loader, h_prot, thread);
 315 
 316     if (k != NULL) {
 317       // Make sure that resolving is legal
 318       EXCEPTION_MARK;
 319       // return NULL if verification fails
 320       verify_constant_pool_resolve(this_cp, k, THREAD);
 321       if (HAS_PENDING_EXCEPTION) {
 322         CLEAR_PENDING_EXCEPTION;
 323         return NULL;
 324       }
 325       return k;
 326     } else {
 327       return k;
 328     }
 329   }


 471 int ConstantPool::name_ref_index_at(int which_nt) {
 472   jint ref_index = name_and_type_at(which_nt);
 473   return extract_low_short_from_int(ref_index);
 474 }
 475 
 476 
 477 int ConstantPool::signature_ref_index_at(int which_nt) {
 478   jint ref_index = name_and_type_at(which_nt);
 479   return extract_high_short_from_int(ref_index);
 480 }
 481 
 482 
 483 Klass* ConstantPool::klass_ref_at(int which, TRAPS) {
 484   return klass_at(klass_ref_index_at(which), THREAD);
 485 }
 486 
 487 
 488 Symbol* ConstantPool::klass_name_at(int which) const {
 489   assert(tag_at(which).is_unresolved_klass() || tag_at(which).is_klass(),
 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 }
 504 
 505 Symbol* ConstantPool::klass_ref_at_noresolve(int which) {
 506   jint ref_index = klass_ref_index_at(which);
 507   return klass_at_noresolve(ref_index);
 508 }
 509 
 510 Symbol* ConstantPool::uncached_klass_ref_at_noresolve(int which) {
 511   jint ref_index = uncached_klass_ref_index_at(which);
 512   return klass_at_noresolve(ref_index);
 513 }
 514 
 515 char* ConstantPool::string_at_noresolve(int which) {
 516   return unresolved_string_at(which)->as_C_string();
 517 }
 518 
 519 BasicType ConstantPool::basic_type_for_signature_at(int which) const {
 520   return FieldType::basic_type(symbol_at(which));
 521 }
 522 


 833   // If the string has already been interned, this entry will be non-null
 834   oop str = this_cp->resolved_references()->obj_at(obj_index);
 835   if (str != NULL) return str;
 836   Symbol* sym = this_cp->unresolved_string_at(which);
 837   str = StringTable::intern(sym, CHECK_(NULL));
 838   this_cp->string_at_put(which, obj_index, str);
 839   assert(java_lang_String::is_instance(str), "must be string");
 840   return str;
 841 }
 842 
 843 
 844 bool ConstantPool::klass_name_at_matches(const InstanceKlass* k, int which) {
 845   // Names are interned, so we can compare Symbol*s directly
 846   Symbol* cp_name = klass_name_at(which);
 847   return (cp_name == k->name());
 848 }
 849 
 850 
 851 // Iterate over symbols and decrement ones which are Symbol*s
 852 // This is done during GC.
 853 // Only decrement the UTF8 symbols. Unresolved classes and strings point to
 854 // these symbols but didn't increment the reference count.
 855 void ConstantPool::unreference_symbols() {
 856   for (int index = 1; index < length(); index++) { // Index 0 is unused
 857     constantTag tag = tag_at(index);
 858     if (tag.is_symbol()) {
 859       symbol_at(index)->decrement_refcount();
 860     }
 861   }
 862 }
 863 
 864 
 865 // Compare this constant pool's entry at index1 to the constant pool
 866 // cp2's entry at index2.
 867 bool ConstantPool::compare_entry_to(int index1, const constantPoolHandle& cp2,
 868        int index2, TRAPS) {
 869 
 870   // The error tags are equivalent to non-error tags when comparing
 871   jbyte t1 = tag_at(index1).non_error_value();
 872   jbyte t2 = cp2->tag_at(index2).non_error_value();
 873 


1214     default:
1215       // all others take one constant pool entry
1216       src_i++;
1217       dest_i++;
1218       break;
1219     }
1220   }
1221   copy_operands(from_cp, to_cp, CHECK);
1222 
1223 } // end copy_cp_to_impl()
1224 
1225 
1226 // Copy this constant pool's entry at from_i to the constant pool
1227 // to_cp's entry at to_i.
1228 void ConstantPool::copy_entry_to(const constantPoolHandle& from_cp, int from_i,
1229                                         const constantPoolHandle& to_cp, int to_i,
1230                                         TRAPS) {
1231 
1232   int tag = from_cp->tag_at(from_i).value();
1233   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:
1241   {
1242     jint ki = from_cp->klass_index_at(from_i);
1243     to_cp->klass_index_at_put(to_i, ki);
1244   } break;
1245 
1246   case JVM_CONSTANT_Double:
1247   {
1248     jdouble d = from_cp->double_at(from_i);
1249     to_cp->double_at_put(to_i, d);
1250     // double takes two constant pool entries so init second entry's tag
1251     to_cp->tag_at_put(to_i + 1, JVM_CONSTANT_Invalid);
1252   } break;
1253 
1254   case JVM_CONSTANT_Fieldref:
1255   {
1256     int class_index = from_cp->uncached_klass_ref_index_at(from_i);
1257     int name_and_type_index = from_cp->uncached_name_and_type_ref_index_at(from_i);
1258     to_cp->field_at_put(to_i, class_index, name_and_type_index);
1259   } break;


1288   case JVM_CONSTANT_Methodref:
1289   {
1290     int class_index = from_cp->uncached_klass_ref_index_at(from_i);
1291     int name_and_type_index = from_cp->uncached_name_and_type_ref_index_at(from_i);
1292     to_cp->method_at_put(to_i, class_index, name_and_type_index);
1293   } break;
1294 
1295   case JVM_CONSTANT_NameAndType:
1296   {
1297     int name_ref_index = from_cp->name_ref_index_at(from_i);
1298     int signature_ref_index = from_cp->signature_ref_index_at(from_i);
1299     to_cp->name_and_type_at_put(to_i, name_ref_index, signature_ref_index);
1300   } break;
1301 
1302   case JVM_CONSTANT_StringIndex:
1303   {
1304     jint si = from_cp->string_index_at(from_i);
1305     to_cp->string_index_at_put(to_i, si);
1306   } break;
1307 

1308   case JVM_CONSTANT_UnresolvedClass:
1309   case JVM_CONSTANT_UnresolvedClassInError:
1310   {
1311     // Can be resolved after checking tag, so check the slot first.
1312     CPSlot entry = from_cp->slot_at(from_i);
1313     if (entry.is_resolved()) {
1314       assert(entry.get_klass()->is_klass(), "must be");
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;
1321 
1322   case JVM_CONSTANT_String:
1323   {
1324     Symbol* s = from_cp->unresolved_string_at(from_i);
1325     to_cp->unresolved_string_at_put(to_i, s);
1326   } break;
1327 
1328   case JVM_CONSTANT_Utf8:
1329   {
1330     Symbol* s = from_cp->symbol_at(from_i);
1331     // Need to increase refcount, the old one will be thrown away and deferenced
1332     s->increment_refcount();
1333     to_cp->symbol_at_put(to_i, s);
1334   } break;
1335 
1336   case JVM_CONSTANT_MethodType:
1337   case JVM_CONSTANT_MethodTypeInError:
1338   {
1339     jint k = from_cp->method_type_index_at(from_i);


1351   case JVM_CONSTANT_InvokeDynamic:
1352   {
1353     int k1 = from_cp->invoke_dynamic_bootstrap_specifier_index(from_i);
1354     int k2 = from_cp->invoke_dynamic_name_and_type_ref_index_at(from_i);
1355     k1 += operand_array_length(to_cp->operands());  // to_cp might already have operands
1356     to_cp->invoke_dynamic_at_put(to_i, k1, k2);
1357   } break;
1358 
1359   // Invalid is used as the tag for the second constant pool entry
1360   // occupied by JVM_CONSTANT_Double or JVM_CONSTANT_Long. It should
1361   // not be seen by itself.
1362   case JVM_CONSTANT_Invalid: // fall through
1363 
1364   default:
1365   {
1366     ShouldNotReachHere();
1367   } break;
1368   }
1369 } // end copy_entry_to()
1370 
1371 
1372 // Search constant pool search_cp for an entry that matches this
1373 // constant pool's entry at pattern_i. Returns the index of a
1374 // matching entry or zero (0) if there is no matching entry.
1375 int ConstantPool::find_matching_entry(int pattern_i,
1376       const constantPoolHandle& search_cp, TRAPS) {
1377 
1378   // index zero (0) is not used
1379   for (int i = 1; i < search_cp->length(); i++) {
1380     bool found = compare_entry_to(pattern_i, search_cp, i, CHECK_0);
1381     if (found) {
1382       return i;
1383     }
1384   }
1385 
1386   return 0;  // entry not found; return unused index zero (0)
1387 } // end find_matching_entry()
1388 
1389 
1390 // Compare this constant pool's bootstrap specifier at idx1 to the constant pool
1391 // cp2's bootstrap specifier at idx2.


1807       }
1808     }
1809     DBG(printf("\n"));
1810     bytes += ent_size;
1811     size  += ent_size;
1812   }
1813   assert(size == cpool_size, "Size mismatch");
1814 
1815   // Keep temorarily for debugging until it's stable.
1816   DBG(print_cpool_bytes(cnt, start_bytes));
1817   return (int)(bytes - start_bytes);
1818 } /* end copy_cpool_bytes */
1819 
1820 #undef DBG
1821 
1822 
1823 void ConstantPool::set_on_stack(const bool value) {
1824   if (value) {
1825     // Only record if it's not already set.
1826     if (!on_stack()) {

1827       _flags |= _on_stack;
1828       MetadataOnStackMark::record(this);
1829     }
1830   } else {
1831     // Clearing is done single-threadedly.

1832     _flags &= ~_on_stack;
1833   }

1834 }
1835 
1836 // JSR 292 support for patching constant pool oops after the class is linked and
1837 // the oop array for resolved references are created.
1838 // We can't do this during classfile parsing, which is how the other indexes are
1839 // patched.  The other patches are applied early for some error checking
1840 // so only defer the pseudo_strings.
1841 void ConstantPool::patch_resolved_references(GrowableArray<Handle>* cp_patches) {
1842   for (int index = 1; index < cp_patches->length(); index++) { // Index 0 is unused
1843     Handle patch = cp_patches->at(index);
1844     if (patch.not_null()) {
1845       assert (tag_at(index).is_string(), "should only be string left");
1846       // Patching a string means pre-resolving it.
1847       // The spelling in the constant pool is ignored.
1848       // The constant reference may be any object whatever.
1849       // If it is not a real interned string, the constant is referred
1850       // to as a "pseudo-string", and must be presented to the CP
1851       // explicitly, because it may require scavenging.
1852       int obj_index = cp_to_object_index(index);
1853       pseudo_string_at_put(index, obj_index, patch());


1888 #endif
1889 
1890 
1891 // Printing
1892 
1893 void ConstantPool::print_on(outputStream* st) const {
1894   assert(is_constantPool(), "must be constantPool");
1895   st->print_cr("%s", internal_name());
1896   if (flags() != 0) {
1897     st->print(" - flags: 0x%x", flags());
1898     if (has_preresolution()) st->print(" has_preresolution");
1899     if (on_stack()) st->print(" on_stack");
1900     st->cr();
1901   }
1902   if (pool_holder() != NULL) {
1903     st->print_cr(" - holder: " INTPTR_FORMAT, p2i(pool_holder()));
1904   }
1905   st->print_cr(" - cache: " INTPTR_FORMAT, p2i(cache()));
1906   st->print_cr(" - resolved_references: " INTPTR_FORMAT, p2i(resolved_references()));
1907   st->print_cr(" - reference_map: " INTPTR_FORMAT, p2i(reference_map()));

1908 
1909   for (int index = 1; index < length(); index++) {      // Index 0 is unused
1910     ((ConstantPool*)this)->print_entry_on(index, st);
1911     switch (tag_at(index).value()) {
1912       case JVM_CONSTANT_Long :
1913       case JVM_CONSTANT_Double :
1914         index++;   // Skip entry following eigth-byte constant
1915     }
1916 
1917   }
1918   st->cr();
1919 }
1920 
1921 // Print one constant pool entry
1922 void ConstantPool::print_entry_on(const int index, outputStream* st) {
1923   EXCEPTION_MARK;
1924   st->print(" - %3d : ", index);
1925   tag_at(index).print_on(st);
1926   st->print(" : ");
1927   switch (tag_at(index).value()) {


1949       break;
1950     case JVM_CONSTANT_Integer :
1951       st->print("%d", int_at(index));
1952       break;
1953     case JVM_CONSTANT_Float :
1954       st->print("%f", float_at(index));
1955       break;
1956     case JVM_CONSTANT_Long :
1957       st->print_jlong(long_at(index));
1958       break;
1959     case JVM_CONSTANT_Double :
1960       st->print("%lf", double_at(index));
1961       break;
1962     case JVM_CONSTANT_NameAndType :
1963       st->print("name_index=%d", name_ref_index_at(index));
1964       st->print(" signature_index=%d", signature_ref_index_at(index));
1965       break;
1966     case JVM_CONSTANT_Utf8 :
1967       symbol_at(index)->print_value_on(st);
1968       break;






1969     case JVM_CONSTANT_UnresolvedClass :               // fall-through
1970     case JVM_CONSTANT_UnresolvedClassInError: {
1971       CPSlot entry = slot_at(index);
1972       if (entry.is_resolved()) {
1973         entry.get_klass()->print_value_on(st);





1974       } else {
1975         entry.get_symbol()->print_value_on(st);
1976       }
1977       }
1978       break;
1979     case JVM_CONSTANT_MethodHandle :
1980     case JVM_CONSTANT_MethodHandleInError :
1981       st->print("ref_kind=%d", method_handle_ref_kind_at(index));
1982       st->print(" ref_index=%d", method_handle_index_at(index));
1983       break;
1984     case JVM_CONSTANT_MethodType :
1985     case JVM_CONSTANT_MethodTypeInError :
1986       st->print("signature_index=%d", method_type_index_at(index));
1987       break;
1988     case JVM_CONSTANT_InvokeDynamic :
1989       {
1990         st->print("bootstrap_method_index=%d", invoke_dynamic_bootstrap_method_ref_index_at(index));
1991         st->print(" name_and_type_index=%d", invoke_dynamic_name_and_type_ref_index_at(index));
1992         int argc = invoke_dynamic_argument_count_at(index);
1993         if (argc > 0) {
1994           for (int arg_i = 0; arg_i < argc; arg_i++) {
1995             int arg = invoke_dynamic_argument_index_at(index, arg_i);


2027 // Size Statistics
2028 void ConstantPool::collect_statistics(KlassSizeStats *sz) const {
2029   sz->_cp_all_bytes += (sz->_cp_bytes          = sz->count(this));
2030   sz->_cp_all_bytes += (sz->_cp_tags_bytes     = sz->count_array(tags()));
2031   sz->_cp_all_bytes += (sz->_cp_cache_bytes    = sz->count(cache()));
2032   sz->_cp_all_bytes += (sz->_cp_operands_bytes = sz->count_array(operands()));
2033   sz->_cp_all_bytes += (sz->_cp_refmap_bytes   = sz->count_array(reference_map()));
2034 
2035   sz->_ro_bytes += sz->_cp_operands_bytes + sz->_cp_tags_bytes +
2036                    sz->_cp_refmap_bytes;
2037   sz->_rw_bytes += sz->_cp_bytes + sz->_cp_cache_bytes;
2038 }
2039 #endif // INCLUDE_SERVICES
2040 
2041 // Verification
2042 
2043 void ConstantPool::verify_on(outputStream* st) {
2044   guarantee(is_constantPool(), "object must be constant pool");
2045   for (int i = 0; i< length();  i++) {
2046     constantTag tag = tag_at(i);
2047     CPSlot entry = slot_at(i);
2048     if (tag.is_klass()) {
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()) {

2057       guarantee(entry.get_symbol()->refcount() != 0, "should have nonzero reference count");
2058     } else if (tag.is_string()) {

2059       guarantee(entry.get_symbol()->refcount() != 0, "should have nonzero reference count");
2060     }
2061   }
2062   if (cache() != NULL) {
2063     // Note: cache() can be NULL before a class is completely setup or
2064     // in temporary constant pools used during constant pool merging
2065     guarantee(cache()->is_constantPoolCache(), "should be constant pool cache");
2066   }
2067   if (pool_holder() != NULL) {
2068     // Note: pool_holder() can be NULL in temporary constant pools
2069     // used during constant pool merging
2070     guarantee(pool_holder()->is_klass(),    "should be klass");
2071   }
2072 }
2073 
2074 
2075 void SymbolHashMap::add_entry(Symbol* sym, u2 value) {
2076   char *str = sym->as_utf8();
2077   unsigned int hash = compute_hash(str, sym->utf8_length());
2078   unsigned int index = hash % table_size();




  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 "memory/resourceArea.hpp"
  37 #include "oops/constantPool.hpp"
  38 #include "oops/instanceKlass.hpp"
  39 #include "oops/objArrayKlass.hpp"
  40 #include "oops/objArrayOop.inline.hpp"
  41 #include "oops/oop.inline.hpp"
  42 #include "runtime/fieldType.hpp"
  43 #include "runtime/init.hpp"
  44 #include "runtime/javaCalls.hpp"
  45 #include "runtime/signature.hpp"
  46 #include "runtime/vframe.hpp"
  47 #include "utilities/copy.hpp"
  48 
  49 ConstantPool* ConstantPool::allocate(ClassLoaderData* loader_data, int length, TRAPS) {
  50   // Add one extra element to tags for storing ConstantPool::flags().
  51   Array<u1>* tags = MetadataFactory::new_writeable_array<u1>(loader_data, length+1, 0, CHECK_NULL);

  52   int size = ConstantPool::size(length);
  53   return new (loader_data, size, true, MetaspaceObj::ConstantPoolType, THREAD) ConstantPool(tags);







  54 }
  55 
  56 #ifdef ASSERT
  57 
  58 // MetaspaceObj allocation invariant is calloc equivalent memory
  59 // simple verification of this here (JVM_CONSTANT_Invalid == 0 )
  60 static bool tag_array_is_zero_initialized(Array<u1>* tags) {
  61   assert(tags != NULL, "invariant");
  62   const int length = tags->length();
  63   for (int index = 0; index < length; ++index) {
  64     if (JVM_CONSTANT_Invalid != tags->at(index)) {
  65       return false;
  66     }
  67   }
  68   return true;
  69 }
  70 
  71 #endif
  72 
  73 ConstantPool::ConstantPool(Array<u1>* tags) :
  74   _tags(tags),
  75   _length(tags->length()-1) {

  76 
  77     assert(_tags != NULL, "invariant");
  78     assert(tags->length()-1 == _length, "invariant"); // tags->at(_length) is flags()
  79     assert(tag_array_is_zero_initialized(tags), "invariant");
  80     assert(0 == flags(), "invariant");
  81     assert(0 == version(), "invariant");
  82     assert(NULL == _pool_holder, "invariant");
  83 }
  84 
  85 void ConstantPool::deallocate_contents(ClassLoaderData* loader_data) {
  86   if (cache() != NULL) {

  87     MetadataFactory::free_array<u2>(loader_data, reference_map());
  88     set_reference_map(NULL);
  89     MetadataFactory::free_metadata(loader_data, cache());
  90     set_cache(NULL);
  91   }
  92 
  93   MetadataFactory::free_array<Klass*>(loader_data, resolved_klasses());
  94   set_resolved_klasses(NULL);
  95 
  96   MetadataFactory::free_array<jushort>(loader_data, operands());
  97   set_operands(NULL);
  98 
  99   release_C_heap_structures();
 100 
 101   // free tag array
 102   MetadataFactory::free_array<u1>(loader_data, tags());
 103   set_tags(NULL);
 104 }
 105 
 106 void ConstantPool::release_C_heap_structures() {
 107   // walk constant pool and decrement symbol reference counts
 108   unreference_symbols();
 109 }
 110 
 111 objArrayOop ConstantPool::resolved_references() const {
 112   return (objArrayOop)JNIHandles::resolve(_cache->resolved_references());
 113 }
 114 
 115 // Create resolved_references array and mapping array for original cp indexes
 116 // The ldc bytecode was rewritten to have the resolved reference array index so need a way
 117 // to map it back for resolving and some unlikely miscellaneous uses.
 118 // The objects created by invokedynamic are appended to this list.
 119 void ConstantPool::initialize_resolved_references(ClassLoaderData* loader_data,
 120                                                   const intStack& reference_map,
 121                                                   int constant_pool_map_length,
 122                                                   TRAPS) {
 123   // Initialized the resolved object cache.
 124   int map_length = reference_map.length();
 125   if (map_length > 0) {
 126     // Only need mapping back to constant pool entries.  The map isn't used for
 127     // invokedynamic resolved_reference entries.  For invokedynamic entries,
 128     // the constant pool cache index has the mapping back to both the constant
 129     // pool and to the resolved reference index.
 130     if (constant_pool_map_length > 0) {
 131       Array<u2>* om = MetadataFactory::new_array<u2>(loader_data, constant_pool_map_length, CHECK);
 132 
 133       for (int i = 0; i < constant_pool_map_length; i++) {
 134         int x = reference_map.at(i);
 135         assert(x == (int)(jushort) x, "klass index is too big");
 136         om->at_put(i, (jushort)x);
 137       }
 138       set_reference_map(om);
 139     }
 140 
 141     // Create Java array for holding resolved strings, methodHandles,
 142     // methodTypes, invokedynamic and invokehandle appendix objects, etc.
 143     objArrayOop stom = oopFactory::new_objArray(SystemDictionary::Object_klass(), map_length, CHECK);
 144     Handle refs_handle (THREAD, (oop)stom);  // must handleize.
 145     set_resolved_references(loader_data->add_handle(refs_handle));
 146   }
 147 }
 148 
 149 void ConstantPool::allocate_resolved_klasses(ClassLoaderData* loader_data, int num_klasses, TRAPS) {
 150   // A ConstantPool can't possibly have 0xffff valid class entries,
 151   // because entry #0 must be CONSTANT_Invalid, and each class entry must refer to a UTF8
 152   // entry for the class's name. So at most we will have 0xfffe class entries.
 153   // This allows us to use 0xffff (ConstantPool::_invalid_resolved_klass_index) to indicate
 154   // UnresolvedKlass entries that are temporarily created during class redefinition.
 155   assert(num_klasses < _invalid_resolved_klass_index, "sanity");
 156   assert(resolved_klasses() == NULL, "sanity");
 157   Array<Klass*>* rk = MetadataFactory::new_writeable_array<Klass*>(loader_data, num_klasses, CHECK);
 158   set_resolved_klasses(rk);
 159 }
 160 
 161 void ConstantPool::initialize_unresolved_klasses(ClassLoaderData* loader_data, TRAPS) {
 162   int len = length();
 163   int num_klasses = 0;
 164   for (int i = 1; i <len; i++) {
 165     switch (tag_at(i).value()) {
 166     case JVM_CONSTANT_ClassIndex:
 167       {
 168         const int class_index = klass_index_at(i);
 169         unresolved_klass_at_put(i, class_index, num_klasses++);
 170       }
 171       break;
 172 #ifndef PRODUCT
 173     case JVM_CONSTANT_Class:
 174     case JVM_CONSTANT_UnresolvedClass:
 175     case JVM_CONSTANT_UnresolvedClassInError:
 176       // All of these should have been reverted back to ClassIndex before calling
 177       // this function.
 178       ShouldNotReachHere();
 179 #endif
 180     }
 181   }
 182   allocate_resolved_klasses(loader_data, num_klasses, THREAD);
 183 }
 184 
 185 void ConstantPool::klass_at_put(int which, int name_index, int resolved_klass_index, Klass* k, Symbol* name) {
 186   assert(is_within_bounds(which), "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(which) =
 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(which, JVM_CONSTANT_Class);
 201   } else {
 202     release_tag_at_put(which, JVM_CONSTANT_UnresolvedClass);
 203   }
 204 }
 205 
 206 // CDS support. Create a new resolved_references array.
 207 void ConstantPool::restore_unshareable_info(TRAPS) {
 208   assert(is_constantPool(), "ensure C++ vtable is restored");
 209   assert(on_stack(), "should always be set for shared constant pools");
 210   assert(is_shared_quick(), "should always be set for shared constant pools");
 211 
 212   // Only create the new resolved references array if it hasn't been attempted before
 213   if (resolved_references() != NULL) return;
 214 
 215   // restore the C++ vtable from the shared archive
 216   restore_vtable();
 217 
 218   if (SystemDictionary::Object_klass_loaded()) {
 219     // Recreate the object array and add to ClassLoaderData.
 220     int map_length = resolved_reference_length();
 221     if (map_length > 0) {
 222       objArrayOop stom = oopFactory::new_objArray(SystemDictionary::Object_klass(), map_length, CHECK);
 223       Handle refs_handle (THREAD, (oop)stom);  // must handleize.
 224 
 225       ClassLoaderData* loader_data = pool_holder()->class_loader_data();
 226       set_resolved_references(loader_data->add_handle(refs_handle));
 227     }
 228   }
 229 }
 230 
 231 void ConstantPool::remove_unshareable_info() {
 232   // Resolved references are not in the shared archive.
 233   // Save the length for restoration.  It is not necessarily the same length
 234   // as reference_map.length() if invokedynamic is saved.
 235   set_resolved_reference_length(
 236     resolved_references() != NULL ? resolved_references()->length() : 0);
 237   set_resolved_references(NULL);
 238 
 239   // Shared ConstantPools are in the RO region, so the _flags cannot be modified.
 240   // The _on_stack flag is used to prevent ConstantPools from deallocation during
 241   // class redefinition. Since shared ConstantPools cannot be deallocated anyway,
 242   // we always set _on_stack to true to avoid having to change _flags during runtime.
 243   _flags |= (_on_stack | _is_shared_quick);
 244 }
 245 
 246 int ConstantPool::cp_to_object_index(int cp_index) {
 247   // this is harder don't do this so much.
 248   int i = reference_map()->find(cp_index);
 249   // We might not find the index for jsr292 call.
 250   return (i < 0) ? _no_index_sentinel : i;
 251 }
 252 
 253 void ConstantPool::string_at_put(int which, int obj_index, oop str) {
 254   resolved_references()->obj_at_put(obj_index, str);
 255 }
 256 
 257 void ConstantPool::trace_class_resolution(const constantPoolHandle& this_cp, Klass* k) {
 258   ResourceMark rm;
 259   int line_number = -1;
 260   const char * source_file = NULL;
 261   if (JavaThread::current()->has_last_Java_frame()) {
 262     // try to identify the method which called this function.
 263     vframeStream vfst(JavaThread::current());


 273     // only print something if the classes are different
 274     if (source_file != NULL) {
 275       log_debug(class, resolve)("%s %s %s:%d",
 276                  this_cp->pool_holder()->external_name(),
 277                  k->external_name(), source_file, line_number);
 278     } else {
 279       log_debug(class, resolve)("%s %s",
 280                  this_cp->pool_holder()->external_name(),
 281                  k->external_name());
 282     }
 283   }
 284 }
 285 
 286 Klass* ConstantPool::klass_at_impl(const constantPoolHandle& this_cp, int which,
 287                                    bool save_resolution_error, TRAPS) {
 288   assert(THREAD->is_Java_thread(), "must be a Java thread");
 289 
 290   // A resolved constantPool entry will contain a Klass*, otherwise a Symbol*.
 291   // It is not safe to rely on the tag bit's here, since we don't have a lock, and
 292   // the entry and tag is not updated atomicly.
 293   int value = *this_cp->int_at_addr(which);
 294   int resolved_klass_index = extract_low_short_from_int(value);
 295   int name_index = extract_high_short_from_int(value);
 296   assert(this_cp->tag_at(name_index).is_symbol(), "sanity");
 297 
 298   Klass* klass = this_cp->resolved_klasses()->at(resolved_klass_index);
 299   if (klass != NULL) {
 300     return klass;
 301   }
 302 
 303   // This tag doesn't change back to unresolved class unless at a safepoint.
 304   if (this_cp->tag_at(which).is_unresolved_klass_in_error()) {
 305     // The original attempt to resolve this constant pool entry failed so find the
 306     // class of the original error and throw another error of the same class
 307     // (JVMS 5.4.3).
 308     // If there is a detail message, pass that detail message to the error.
 309     // The JVMS does not strictly require us to duplicate the same detail message,
 310     // or any internal exception fields such as cause or stacktrace.  But since the
 311     // detail message is often a class name or other literal string, we will repeat it
 312     // if we can find it in the symbol table.
 313     throw_resolution_error(this_cp, which, CHECK_0);
 314     ShouldNotReachHere();
 315   }
 316 
 317   Handle mirror_handle;
 318   Symbol* name = this_cp->symbol_at(name_index);
 319   Handle loader (THREAD, this_cp->pool_holder()->class_loader());
 320   Handle protection_domain (THREAD, this_cp->pool_holder()->protection_domain());
 321   Klass* k = SystemDictionary::resolve_or_fail(name, loader, protection_domain, true, THREAD);
 322   if (!HAS_PENDING_EXCEPTION) {
 323     // preserve the resolved klass from unloading
 324     mirror_handle = Handle(THREAD, k->java_mirror());
 325     // Do access check for klasses
 326     verify_constant_pool_resolve(this_cp, k, THREAD);
 327   }
 328 
 329   // Failed to resolve class. We must record the errors so that subsequent attempts
 330   // to resolve this constant pool entry fail with the same error (JVMS 5.4.3).
 331   if (HAS_PENDING_EXCEPTION) {
 332     if (save_resolution_error) {
 333       save_and_throw_exception(this_cp, which, constantTag(JVM_CONSTANT_UnresolvedClass), CHECK_NULL);
 334       // If CHECK_NULL above doesn't return the exception, that means that
 335       // some other thread has beaten us and has resolved the class.
 336       // To preserve old behavior, we return the resolved class.
 337       klass = this_cp->resolved_klasses()->at(resolved_klass_index);
 338       assert(klass != NULL, "must be resolved if exception was cleared");
 339       return klass;

 340     } else {
 341       return NULL;  // return the pending exception
 342     }
 343   }
 344 
 345   // Make this class loader depend upon the class loader owning the class reference
 346   ClassLoaderData* this_key = this_cp->pool_holder()->class_loader_data();
 347   this_key->record_dependency(k, CHECK_NULL); // Can throw OOM
 348 
 349   // logging for class+resolve.
 350   if (log_is_enabled(Debug, class, resolve)){
 351     trace_class_resolution(this_cp, k);
 352   }
 353   Klass** adr = this_cp->resolved_klasses()->adr_at(resolved_klass_index);
 354   OrderAccess::release_store_ptr((Klass* volatile *)adr, k);
 355   // The interpreter assumes when the tag is stored, the klass is resolved
 356   // and the Klass* is a klass rather than a Symbol*, so we need
 357   // hardware store ordering here.
 358   this_cp->release_tag_at_put(which, JVM_CONSTANT_Class);
 359   return k;
 360 }
 361 
 362 
 363 // Does not update ConstantPool* - to avoid any exception throwing. Used
 364 // by compiler and exception handling.  Also used to avoid classloads for
 365 // instanceof operations. Returns NULL if the class has not been loaded or
 366 // if the verification of constant pool failed
 367 Klass* ConstantPool::klass_at_if_loaded(const constantPoolHandle& this_cp, int which) {
 368   int value = *this_cp->int_at_addr(which);
 369   int resolved_klass_index = extract_low_short_from_int(value);
 370   int name_index = extract_high_short_from_int(value);
 371   assert(this_cp->tag_at(name_index).is_symbol(), "sanity");
 372 
 373   Klass* k = this_cp->resolved_klasses()->at(resolved_klass_index);
 374   if (k != NULL) {
 375     return k;
 376   } else {

 377     Thread *thread = Thread::current();
 378     Symbol* name = this_cp->symbol_at(name_index);
 379     oop loader = this_cp->pool_holder()->class_loader();
 380     oop protection_domain = this_cp->pool_holder()->protection_domain();
 381     Handle h_prot (thread, protection_domain);
 382     Handle h_loader (thread, loader);
 383     Klass* k = SystemDictionary::find(name, h_loader, h_prot, thread);
 384 
 385     if (k != NULL) {
 386       // Make sure that resolving is legal
 387       EXCEPTION_MARK;
 388       // return NULL if verification fails
 389       verify_constant_pool_resolve(this_cp, k, THREAD);
 390       if (HAS_PENDING_EXCEPTION) {
 391         CLEAR_PENDING_EXCEPTION;
 392         return NULL;
 393       }
 394       return k;
 395     } else {
 396       return k;
 397     }
 398   }


 540 int ConstantPool::name_ref_index_at(int which_nt) {
 541   jint ref_index = name_and_type_at(which_nt);
 542   return extract_low_short_from_int(ref_index);
 543 }
 544 
 545 
 546 int ConstantPool::signature_ref_index_at(int which_nt) {
 547   jint ref_index = name_and_type_at(which_nt);
 548   return extract_high_short_from_int(ref_index);
 549 }
 550 
 551 
 552 Klass* ConstantPool::klass_ref_at(int which, TRAPS) {
 553   return klass_at(klass_ref_index_at(which), THREAD);
 554 }
 555 
 556 
 557 Symbol* ConstantPool::klass_name_at(int which) const {
 558   assert(tag_at(which).is_unresolved_klass() || tag_at(which).is_klass(),
 559          "Corrupted constant pool");
 560   int name_index = extract_high_short_from_int(*int_at_addr(which));
 561   return symbol_at(name_index);
 562 }
 563 
 564 int ConstantPool::klass_name_index_at(int which) const {
 565   assert(tag_at(which).is_unresolved_klass() || tag_at(which).is_klass(),
 566          "Corrupted constant pool");
 567   int name_index = extract_high_short_from_int(*int_at_addr(which));
 568   return name_index;



 569 }
 570 
 571 Symbol* ConstantPool::klass_ref_at_noresolve(int which) {
 572   jint ref_index = klass_ref_index_at(which);
 573   return klass_at_noresolve(ref_index);
 574 }
 575 
 576 Symbol* ConstantPool::uncached_klass_ref_at_noresolve(int which) {
 577   jint ref_index = uncached_klass_ref_index_at(which);
 578   return klass_at_noresolve(ref_index);
 579 }
 580 
 581 char* ConstantPool::string_at_noresolve(int which) {
 582   return unresolved_string_at(which)->as_C_string();
 583 }
 584 
 585 BasicType ConstantPool::basic_type_for_signature_at(int which) const {
 586   return FieldType::basic_type(symbol_at(which));
 587 }
 588 


 899   // If the string has already been interned, this entry will be non-null
 900   oop str = this_cp->resolved_references()->obj_at(obj_index);
 901   if (str != NULL) return str;
 902   Symbol* sym = this_cp->unresolved_string_at(which);
 903   str = StringTable::intern(sym, CHECK_(NULL));
 904   this_cp->string_at_put(which, obj_index, str);
 905   assert(java_lang_String::is_instance(str), "must be string");
 906   return str;
 907 }
 908 
 909 
 910 bool ConstantPool::klass_name_at_matches(const InstanceKlass* k, int which) {
 911   // Names are interned, so we can compare Symbol*s directly
 912   Symbol* cp_name = klass_name_at(which);
 913   return (cp_name == k->name());
 914 }
 915 
 916 
 917 // Iterate over symbols and decrement ones which are Symbol*s
 918 // This is done during GC.
 919 // Only decrement the UTF8 symbols. Strings point to
 920 // these symbols but didn't increment the reference count.
 921 void ConstantPool::unreference_symbols() {
 922   for (int index = 1; index < length(); index++) { // Index 0 is unused
 923     constantTag tag = tag_at(index);
 924     if (tag.is_symbol()) {
 925       symbol_at(index)->decrement_refcount();
 926     }
 927   }
 928 }
 929 
 930 
 931 // Compare this constant pool's entry at index1 to the constant pool
 932 // cp2's entry at index2.
 933 bool ConstantPool::compare_entry_to(int index1, const constantPoolHandle& cp2,
 934        int index2, TRAPS) {
 935 
 936   // The error tags are equivalent to non-error tags when comparing
 937   jbyte t1 = tag_at(index1).non_error_value();
 938   jbyte t2 = cp2->tag_at(index2).non_error_value();
 939 


1280     default:
1281       // all others take one constant pool entry
1282       src_i++;
1283       dest_i++;
1284       break;
1285     }
1286   }
1287   copy_operands(from_cp, to_cp, CHECK);
1288 
1289 } // end copy_cp_to_impl()
1290 
1291 
1292 // Copy this constant pool's entry at from_i to the constant pool
1293 // to_cp's entry at to_i.
1294 void ConstantPool::copy_entry_to(const constantPoolHandle& from_cp, int from_i,
1295                                         const constantPoolHandle& to_cp, int to_i,
1296                                         TRAPS) {
1297 
1298   int tag = from_cp->tag_at(from_i).value();
1299   switch (tag) {






1300   case JVM_CONSTANT_ClassIndex:
1301   {
1302     jint ki = from_cp->klass_index_at(from_i);
1303     to_cp->klass_index_at_put(to_i, ki);
1304   } break;
1305 
1306   case JVM_CONSTANT_Double:
1307   {
1308     jdouble d = from_cp->double_at(from_i);
1309     to_cp->double_at_put(to_i, d);
1310     // double takes two constant pool entries so init second entry's tag
1311     to_cp->tag_at_put(to_i + 1, JVM_CONSTANT_Invalid);
1312   } break;
1313 
1314   case JVM_CONSTANT_Fieldref:
1315   {
1316     int class_index = from_cp->uncached_klass_ref_index_at(from_i);
1317     int name_and_type_index = from_cp->uncached_name_and_type_ref_index_at(from_i);
1318     to_cp->field_at_put(to_i, class_index, name_and_type_index);
1319   } break;


1348   case JVM_CONSTANT_Methodref:
1349   {
1350     int class_index = from_cp->uncached_klass_ref_index_at(from_i);
1351     int name_and_type_index = from_cp->uncached_name_and_type_ref_index_at(from_i);
1352     to_cp->method_at_put(to_i, class_index, name_and_type_index);
1353   } break;
1354 
1355   case JVM_CONSTANT_NameAndType:
1356   {
1357     int name_ref_index = from_cp->name_ref_index_at(from_i);
1358     int signature_ref_index = from_cp->signature_ref_index_at(from_i);
1359     to_cp->name_and_type_at_put(to_i, name_ref_index, signature_ref_index);
1360   } break;
1361 
1362   case JVM_CONSTANT_StringIndex:
1363   {
1364     jint si = from_cp->string_index_at(from_i);
1365     to_cp->string_index_at_put(to_i, si);
1366   } break;
1367 
1368   case JVM_CONSTANT_Class:
1369   case JVM_CONSTANT_UnresolvedClass:
1370   case JVM_CONSTANT_UnresolvedClassInError:
1371   {
1372     // Revert to JVM_CONSTANT_ClassIndex
1373     int name_index = extract_high_short_from_int(*from_cp->int_at_addr(from_i));
1374     assert(from_cp->tag_at(name_index).is_symbol(), "sanity");
1375     to_cp->klass_index_at_put(to_i, name_index);





1376   } break;
1377 
1378   case JVM_CONSTANT_String:
1379   {
1380     Symbol* s = from_cp->unresolved_string_at(from_i);
1381     to_cp->unresolved_string_at_put(to_i, s);
1382   } break;
1383 
1384   case JVM_CONSTANT_Utf8:
1385   {
1386     Symbol* s = from_cp->symbol_at(from_i);
1387     // Need to increase refcount, the old one will be thrown away and deferenced
1388     s->increment_refcount();
1389     to_cp->symbol_at_put(to_i, s);
1390   } break;
1391 
1392   case JVM_CONSTANT_MethodType:
1393   case JVM_CONSTANT_MethodTypeInError:
1394   {
1395     jint k = from_cp->method_type_index_at(from_i);


1407   case JVM_CONSTANT_InvokeDynamic:
1408   {
1409     int k1 = from_cp->invoke_dynamic_bootstrap_specifier_index(from_i);
1410     int k2 = from_cp->invoke_dynamic_name_and_type_ref_index_at(from_i);
1411     k1 += operand_array_length(to_cp->operands());  // to_cp might already have operands
1412     to_cp->invoke_dynamic_at_put(to_i, k1, k2);
1413   } break;
1414 
1415   // Invalid is used as the tag for the second constant pool entry
1416   // occupied by JVM_CONSTANT_Double or JVM_CONSTANT_Long. It should
1417   // not be seen by itself.
1418   case JVM_CONSTANT_Invalid: // fall through
1419 
1420   default:
1421   {
1422     ShouldNotReachHere();
1423   } break;
1424   }
1425 } // end copy_entry_to()
1426 

1427 // Search constant pool search_cp for an entry that matches this
1428 // constant pool's entry at pattern_i. Returns the index of a
1429 // matching entry or zero (0) if there is no matching entry.
1430 int ConstantPool::find_matching_entry(int pattern_i,
1431       const constantPoolHandle& search_cp, TRAPS) {
1432 
1433   // index zero (0) is not used
1434   for (int i = 1; i < search_cp->length(); i++) {
1435     bool found = compare_entry_to(pattern_i, search_cp, i, CHECK_0);
1436     if (found) {
1437       return i;
1438     }
1439   }
1440 
1441   return 0;  // entry not found; return unused index zero (0)
1442 } // end find_matching_entry()
1443 
1444 
1445 // Compare this constant pool's bootstrap specifier at idx1 to the constant pool
1446 // cp2's bootstrap specifier at idx2.


1862       }
1863     }
1864     DBG(printf("\n"));
1865     bytes += ent_size;
1866     size  += ent_size;
1867   }
1868   assert(size == cpool_size, "Size mismatch");
1869 
1870   // Keep temorarily for debugging until it's stable.
1871   DBG(print_cpool_bytes(cnt, start_bytes));
1872   return (int)(bytes - start_bytes);
1873 } /* end copy_cpool_bytes */
1874 
1875 #undef DBG
1876 
1877 
1878 void ConstantPool::set_on_stack(const bool value) {
1879   if (value) {
1880     // Only record if it's not already set.
1881     if (!on_stack()) {
1882       assert(!is_shared_quick(), "should always be set for shared constant pools");
1883       _flags |= _on_stack;
1884       MetadataOnStackMark::record(this);
1885     }
1886   } else {
1887     // Clearing is done single-threadedly.
1888     if (!is_shared_quick()) {
1889       _flags &= ~_on_stack;
1890     }
1891   }
1892 }
1893 
1894 // JSR 292 support for patching constant pool oops after the class is linked and
1895 // the oop array for resolved references are created.
1896 // We can't do this during classfile parsing, which is how the other indexes are
1897 // patched.  The other patches are applied early for some error checking
1898 // so only defer the pseudo_strings.
1899 void ConstantPool::patch_resolved_references(GrowableArray<Handle>* cp_patches) {
1900   for (int index = 1; index < cp_patches->length(); index++) { // Index 0 is unused
1901     Handle patch = cp_patches->at(index);
1902     if (patch.not_null()) {
1903       assert (tag_at(index).is_string(), "should only be string left");
1904       // Patching a string means pre-resolving it.
1905       // The spelling in the constant pool is ignored.
1906       // The constant reference may be any object whatever.
1907       // If it is not a real interned string, the constant is referred
1908       // to as a "pseudo-string", and must be presented to the CP
1909       // explicitly, because it may require scavenging.
1910       int obj_index = cp_to_object_index(index);
1911       pseudo_string_at_put(index, obj_index, patch());


1946 #endif
1947 
1948 
1949 // Printing
1950 
1951 void ConstantPool::print_on(outputStream* st) const {
1952   assert(is_constantPool(), "must be constantPool");
1953   st->print_cr("%s", internal_name());
1954   if (flags() != 0) {
1955     st->print(" - flags: 0x%x", flags());
1956     if (has_preresolution()) st->print(" has_preresolution");
1957     if (on_stack()) st->print(" on_stack");
1958     st->cr();
1959   }
1960   if (pool_holder() != NULL) {
1961     st->print_cr(" - holder: " INTPTR_FORMAT, p2i(pool_holder()));
1962   }
1963   st->print_cr(" - cache: " INTPTR_FORMAT, p2i(cache()));
1964   st->print_cr(" - resolved_references: " INTPTR_FORMAT, p2i(resolved_references()));
1965   st->print_cr(" - reference_map: " INTPTR_FORMAT, p2i(reference_map()));
1966   st->print_cr(" - resolved_klasses: " INTPTR_FORMAT, p2i(resolved_klasses()));
1967 
1968   for (int index = 1; index < length(); index++) {      // Index 0 is unused
1969     ((ConstantPool*)this)->print_entry_on(index, st);
1970     switch (tag_at(index).value()) {
1971       case JVM_CONSTANT_Long :
1972       case JVM_CONSTANT_Double :
1973         index++;   // Skip entry following eigth-byte constant
1974     }
1975 
1976   }
1977   st->cr();
1978 }
1979 
1980 // Print one constant pool entry
1981 void ConstantPool::print_entry_on(const int index, outputStream* st) {
1982   EXCEPTION_MARK;
1983   st->print(" - %3d : ", index);
1984   tag_at(index).print_on(st);
1985   st->print(" : ");
1986   switch (tag_at(index).value()) {


2008       break;
2009     case JVM_CONSTANT_Integer :
2010       st->print("%d", int_at(index));
2011       break;
2012     case JVM_CONSTANT_Float :
2013       st->print("%f", float_at(index));
2014       break;
2015     case JVM_CONSTANT_Long :
2016       st->print_jlong(long_at(index));
2017       break;
2018     case JVM_CONSTANT_Double :
2019       st->print("%lf", double_at(index));
2020       break;
2021     case JVM_CONSTANT_NameAndType :
2022       st->print("name_index=%d", name_ref_index_at(index));
2023       st->print(" signature_index=%d", signature_ref_index_at(index));
2024       break;
2025     case JVM_CONSTANT_Utf8 :
2026       symbol_at(index)->print_value_on(st);
2027       break;
2028     case JVM_CONSTANT_ClassIndex: {
2029         int name_index = *int_at_addr(index);
2030         st->print("klass_index=%d ", name_index);
2031         symbol_at(name_index)->print_value_on(st);
2032       }
2033       break;
2034     case JVM_CONSTANT_UnresolvedClass :               // fall-through
2035     case JVM_CONSTANT_UnresolvedClassInError: {
2036         int value = *int_at_addr(index);
2037         int resolved_klass_index = extract_low_short_from_int(value);
2038         int name_index = extract_high_short_from_int(value);
2039         assert(tag_at(name_index).is_symbol(), "sanity");
2040 
2041         Klass* klass = resolved_klasses()->at(resolved_klass_index);
2042         if (klass != NULL) {
2043           klass->print_value_on(st);
2044         } else {
2045           symbol_at(name_index)->print_value_on(st);
2046         }
2047       }
2048       break;
2049     case JVM_CONSTANT_MethodHandle :
2050     case JVM_CONSTANT_MethodHandleInError :
2051       st->print("ref_kind=%d", method_handle_ref_kind_at(index));
2052       st->print(" ref_index=%d", method_handle_index_at(index));
2053       break;
2054     case JVM_CONSTANT_MethodType :
2055     case JVM_CONSTANT_MethodTypeInError :
2056       st->print("signature_index=%d", method_type_index_at(index));
2057       break;
2058     case JVM_CONSTANT_InvokeDynamic :
2059       {
2060         st->print("bootstrap_method_index=%d", invoke_dynamic_bootstrap_method_ref_index_at(index));
2061         st->print(" name_and_type_index=%d", invoke_dynamic_name_and_type_ref_index_at(index));
2062         int argc = invoke_dynamic_argument_count_at(index);
2063         if (argc > 0) {
2064           for (int arg_i = 0; arg_i < argc; arg_i++) {
2065             int arg = invoke_dynamic_argument_index_at(index, arg_i);


2097 // Size Statistics
2098 void ConstantPool::collect_statistics(KlassSizeStats *sz) const {
2099   sz->_cp_all_bytes += (sz->_cp_bytes          = sz->count(this));
2100   sz->_cp_all_bytes += (sz->_cp_tags_bytes     = sz->count_array(tags()));
2101   sz->_cp_all_bytes += (sz->_cp_cache_bytes    = sz->count(cache()));
2102   sz->_cp_all_bytes += (sz->_cp_operands_bytes = sz->count_array(operands()));
2103   sz->_cp_all_bytes += (sz->_cp_refmap_bytes   = sz->count_array(reference_map()));
2104 
2105   sz->_ro_bytes += sz->_cp_operands_bytes + sz->_cp_tags_bytes +
2106                    sz->_cp_refmap_bytes;
2107   sz->_rw_bytes += sz->_cp_bytes + sz->_cp_cache_bytes;
2108 }
2109 #endif // INCLUDE_SERVICES
2110 
2111 // Verification
2112 
2113 void ConstantPool::verify_on(outputStream* st) {
2114   guarantee(is_constantPool(), "object must be constant pool");
2115   for (int i = 0; i< length();  i++) {
2116     constantTag tag = tag_at(i);
2117     if (tag.is_klass() || tag.is_unresolved_klass()) {
2118       guarantee(klass_name_at(i)->refcount() != 0, "should have nonzero reference count");







2119     } else if (tag.is_symbol()) {
2120       CPSlot entry = slot_at(i);
2121       guarantee(entry.get_symbol()->refcount() != 0, "should have nonzero reference count");
2122     } else if (tag.is_string()) {
2123       CPSlot entry = slot_at(i);
2124       guarantee(entry.get_symbol()->refcount() != 0, "should have nonzero reference count");
2125     }
2126   }
2127   if (cache() != NULL) {
2128     // Note: cache() can be NULL before a class is completely setup or
2129     // in temporary constant pools used during constant pool merging
2130     guarantee(cache()->is_constantPoolCache(), "should be constant pool cache");
2131   }
2132   if (pool_holder() != NULL) {
2133     // Note: pool_holder() can be NULL in temporary constant pools
2134     // used during constant pool merging
2135     guarantee(pool_holder()->is_klass(),    "should be klass");
2136   }
2137 }
2138 
2139 
2140 void SymbolHashMap::add_entry(Symbol* sym, u2 value) {
2141   char *str = sym->as_utf8();
2142   unsigned int hash = compute_hash(str, sym->utf8_length());
2143   unsigned int index = hash % table_size();


< prev index next >