1 /*
   2  * Copyright (c) 1997, 2017, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  *
  23  */
  24 
  25 #include "precompiled.hpp"
  26 #include "jvm.h"
  27 #include "classfile/classLoaderData.hpp"
  28 #include "classfile/javaClasses.inline.hpp"
  29 #include "classfile/metadataOnStackMark.hpp"
  30 #include "classfile/stringTable.hpp"
  31 #include "classfile/systemDictionary.hpp"
  32 #include "classfile/vmSymbols.hpp"
  33 #include "interpreter/linkResolver.hpp"
  34 #include "memory/allocation.inline.hpp"
  35 #include "memory/heapInspection.hpp"
  36 #include "memory/metadataFactory.hpp"
  37 #include "memory/metaspaceClosure.hpp"
  38 #include "memory/metaspaceShared.hpp"
  39 #include "memory/oopFactory.hpp"
  40 #include "memory/resourceArea.hpp"
  41 #include "oops/constantPool.hpp"
  42 #include "oops/instanceKlass.hpp"
  43 #include "oops/objArrayKlass.hpp"
  44 #include "oops/objArrayOop.inline.hpp"
  45 #include "oops/oop.inline.hpp"
  46 #include "runtime/fieldType.hpp"
  47 #include "runtime/init.hpp"
  48 #include "runtime/javaCalls.hpp"
  49 #include "runtime/signature.hpp"
  50 #include "runtime/vframe.hpp"
  51 #include "utilities/copy.hpp"
  52 
  53 ConstantPool* ConstantPool::allocate(ClassLoaderData* loader_data, int length, TRAPS) {
  54   Array<u1>* tags = MetadataFactory::new_array<u1>(loader_data, length, 0, CHECK_NULL);
  55   int size = ConstantPool::size(length);
  56   return new (loader_data, size, MetaspaceObj::ConstantPoolType, THREAD) ConstantPool(tags);
  57 }
  58 
  59 #ifdef ASSERT
  60 
  61 // MetaspaceObj allocation invariant is calloc equivalent memory
  62 // simple verification of this here (JVM_CONSTANT_Invalid == 0 )
  63 static bool tag_array_is_zero_initialized(Array<u1>* tags) {
  64   assert(tags != NULL, "invariant");
  65   const int length = tags->length();
  66   for (int index = 0; index < length; ++index) {
  67     if (JVM_CONSTANT_Invalid != tags->at(index)) {
  68       return false;
  69     }
  70   }
  71   return true;
  72 }
  73 
  74 #endif
  75 
  76 ConstantPool::ConstantPool(Array<u1>* tags) :
  77   _tags(tags),
  78   _length(tags->length()) {
  79 
  80     assert(_tags != NULL, "invariant");
  81     assert(tags->length() == _length, "invariant");
  82     assert(tag_array_is_zero_initialized(tags), "invariant");
  83     assert(0 == flags(), "invariant");
  84     assert(0 == version(), "invariant");
  85     assert(NULL == _pool_holder, "invariant");
  86 }
  87 
  88 void ConstantPool::deallocate_contents(ClassLoaderData* loader_data) {
  89   if (cache() != NULL) {
  90     MetadataFactory::free_metadata(loader_data, cache());
  91     set_cache(NULL);
  92   }
  93 
  94   MetadataFactory::free_array<Klass*>(loader_data, resolved_klasses());
  95   set_resolved_klasses(NULL);
  96 
  97   MetadataFactory::free_array<jushort>(loader_data, operands());
  98   set_operands(NULL);
  99 
 100   release_C_heap_structures();
 101 
 102   // free tag array
 103   MetadataFactory::free_array<u1>(loader_data, tags());
 104   set_tags(NULL);
 105 }
 106 
 107 void ConstantPool::release_C_heap_structures() {
 108   // walk constant pool and decrement symbol reference counts
 109   unreference_symbols();
 110 }
 111 
 112 void ConstantPool::metaspace_pointers_do(MetaspaceClosure* it) {
 113   log_trace(cds)("Iter(ConstantPool): %p", this);
 114 
 115   it->push(&_tags, MetaspaceClosure::_writable);
 116   it->push(&_cache);
 117   it->push(&_pool_holder);
 118   it->push(&_operands);
 119   it->push(&_resolved_klasses, MetaspaceClosure::_writable);
 120 
 121   for (int i = 0; i < length(); i++) {
 122     // The only MSO's embedded in the CP entries are Symbols:
 123     //   JVM_CONSTANT_String (normal and pseudo)
 124     //   JVM_CONSTANT_Utf8
 125     constantTag ctag = tag_at(i);
 126     if (ctag.is_string() || ctag.is_utf8()) {
 127       it->push(symbol_at_addr(i));
 128     }
 129   }
 130 }
 131 
 132 objArrayOop ConstantPool::resolved_references() const {
 133   return (objArrayOop)_cache->resolved_references();
 134 }
 135 
 136 // Called from outside constant pool resolution where a resolved_reference array
 137 // may not be present.
 138 objArrayOop ConstantPool::resolved_references_or_null() const {
 139   if (_cache == NULL) {
 140     return NULL;
 141   } else {
 142     return (objArrayOop)_cache->resolved_references();
 143   }
 144 }
 145 
 146 // Create resolved_references array and mapping array for original cp indexes
 147 // The ldc bytecode was rewritten to have the resolved reference array index so need a way
 148 // to map it back for resolving and some unlikely miscellaneous uses.
 149 // The objects created by invokedynamic are appended to this list.
 150 void ConstantPool::initialize_resolved_references(ClassLoaderData* loader_data,
 151                                                   const intStack& reference_map,
 152                                                   int constant_pool_map_length,
 153                                                   TRAPS) {
 154   // Initialized the resolved object cache.
 155   int map_length = reference_map.length();
 156   if (map_length > 0) {
 157     // Only need mapping back to constant pool entries.  The map isn't used for
 158     // invokedynamic resolved_reference entries.  For invokedynamic entries,
 159     // the constant pool cache index has the mapping back to both the constant
 160     // pool and to the resolved reference index.
 161     if (constant_pool_map_length > 0) {
 162       Array<u2>* om = MetadataFactory::new_array<u2>(loader_data, constant_pool_map_length, CHECK);
 163 
 164       for (int i = 0; i < constant_pool_map_length; i++) {
 165         int x = reference_map.at(i);
 166         assert(x == (int)(jushort) x, "klass index is too big");
 167         om->at_put(i, (jushort)x);
 168       }
 169       set_reference_map(om);
 170     }
 171 
 172     // Create Java array for holding resolved strings, methodHandles,
 173     // methodTypes, invokedynamic and invokehandle appendix objects, etc.
 174     objArrayOop stom = oopFactory::new_objArray(SystemDictionary::Object_klass(), map_length, CHECK);
 175     Handle refs_handle (THREAD, (oop)stom);  // must handleize.
 176     set_resolved_references(loader_data->add_handle(refs_handle));
 177   }
 178 }
 179 
 180 void ConstantPool::allocate_resolved_klasses(ClassLoaderData* loader_data, int num_klasses, TRAPS) {
 181   // A ConstantPool can't possibly have 0xffff valid class entries,
 182   // because entry #0 must be CONSTANT_Invalid, and each class entry must refer to a UTF8
 183   // entry for the class's name. So at most we will have 0xfffe class entries.
 184   // This allows us to use 0xffff (ConstantPool::_temp_resolved_klass_index) to indicate
 185   // UnresolvedKlass entries that are temporarily created during class redefinition.
 186   assert(num_klasses < CPKlassSlot::_temp_resolved_klass_index, "sanity");
 187   assert(resolved_klasses() == NULL, "sanity");
 188   Array<Klass*>* rk = MetadataFactory::new_array<Klass*>(loader_data, num_klasses, CHECK);
 189   set_resolved_klasses(rk);
 190 }
 191 
 192 void ConstantPool::initialize_unresolved_klasses(ClassLoaderData* loader_data, TRAPS) {
 193   int len = length();
 194   int num_klasses = 0;
 195   for (int i = 1; i <len; i++) {
 196     switch (tag_at(i).value()) {
 197     case JVM_CONSTANT_ClassIndex:
 198       {
 199         const int class_index = klass_index_at(i);
 200         unresolved_klass_at_put(i, class_index, num_klasses++);
 201       }
 202       break;
 203 #ifndef PRODUCT
 204     case JVM_CONSTANT_Class:
 205     case JVM_CONSTANT_UnresolvedClass:
 206     case JVM_CONSTANT_UnresolvedClassInError:
 207       // All of these should have been reverted back to ClassIndex before calling
 208       // this function.
 209       ShouldNotReachHere();
 210 #endif
 211     }
 212   }
 213   allocate_resolved_klasses(loader_data, num_klasses, THREAD);
 214 }
 215 
 216 // Anonymous class support:
 217 void ConstantPool::klass_at_put(int class_index, int name_index, int resolved_klass_index, Klass* k, Symbol* name) {
 218   assert(is_within_bounds(class_index), "index out of bounds");
 219   assert(is_within_bounds(name_index), "index out of bounds");
 220   assert((resolved_klass_index & 0xffff0000) == 0, "must be");
 221   *int_at_addr(class_index) =
 222     build_int_from_shorts((jushort)resolved_klass_index, (jushort)name_index);
 223 
 224   symbol_at_put(name_index, name);
 225   name->increment_refcount();
 226   Klass** adr = resolved_klasses()->adr_at(resolved_klass_index);
 227   OrderAccess::release_store(adr, k);
 228 
 229   // The interpreter assumes when the tag is stored, the klass is resolved
 230   // and the Klass* non-NULL, so we need hardware store ordering here.
 231   if (k != NULL) {
 232     release_tag_at_put(class_index, JVM_CONSTANT_Class);
 233   } else {
 234     release_tag_at_put(class_index, JVM_CONSTANT_UnresolvedClass);
 235   }
 236 }
 237 
 238 // Anonymous class support:
 239 void ConstantPool::klass_at_put(int class_index, Klass* k) {
 240   assert(k != NULL, "must be valid klass");
 241   CPKlassSlot kslot = klass_slot_at(class_index);
 242   int resolved_klass_index = kslot.resolved_klass_index();
 243   Klass** adr = resolved_klasses()->adr_at(resolved_klass_index);
 244   OrderAccess::release_store(adr, k);
 245 
 246   // The interpreter assumes when the tag is stored, the klass is resolved
 247   // and the Klass* non-NULL, so we need hardware store ordering here.
 248   release_tag_at_put(class_index, JVM_CONSTANT_Class);
 249 }
 250 
 251 #if INCLUDE_CDS_JAVA_HEAP
 252 // Archive the resolved references
 253 void ConstantPool::archive_resolved_references(Thread* THREAD) {
 254   if (_cache == NULL) {
 255     return; // nothing to do
 256   }
 257 
 258   InstanceKlass *ik = pool_holder();
 259   if (!(ik->is_shared_boot_class() || ik->is_shared_platform_class() ||
 260         ik->is_shared_app_class())) {
 261     // Archiving resolved references for classes from non-builtin loaders
 262     // is not yet supported.
 263     set_resolved_references(NULL);
 264     return;
 265   }
 266 
 267   objArrayOop rr = resolved_references();
 268   Array<u2>* ref_map = reference_map();
 269   if (rr != NULL) {
 270     int ref_map_len = ref_map == NULL ? 0 : ref_map->length();
 271     int rr_len = rr->length();
 272     for (int i = 0; i < rr_len; i++) {
 273       oop p = rr->obj_at(i);
 274       rr->obj_at_put(i, NULL);
 275       if (p != NULL && i < ref_map_len) {
 276         int index = object_to_cp_index(i);
 277         // Skip the entry if the string hash code is 0 since the string
 278         // is not included in the shared string_table, see StringTable::copy_shared_string.
 279         if (tag_at(index).is_string() && java_lang_String::hash_code(p) != 0) {
 280           oop op = StringTable::create_archived_string(p, THREAD);
 281           // If the String object is not archived (possibly too large),
 282           // NULL is returned. Also set it in the array, so we won't
 283           // have a 'bad' reference in the archived resolved_reference
 284           // array.
 285           rr->obj_at_put(i, op);
 286         }
 287       }
 288     }
 289 
 290     oop archived = MetaspaceShared::archive_heap_object(rr, THREAD);
 291     _cache->set_archived_references(archived);
 292     set_resolved_references(NULL);
 293   }
 294 }
 295 
 296 void ConstantPool::resolve_class_constants(TRAPS) {
 297   assert(DumpSharedSpaces, "used during dump time only");
 298   // The _cache may be NULL if the _pool_holder klass fails verification
 299   // at dump time due to missing dependencies.
 300   if (cache() == NULL || reference_map() == NULL) {
 301     return; // nothing to do
 302   }
 303 
 304   constantPoolHandle cp(THREAD, this);
 305   for (int index = 1; index < length(); index++) { // Index 0 is unused
 306     if (tag_at(index).is_string() && !cp->is_pseudo_string_at(index)) {
 307       int cache_index = cp->cp_to_object_index(index);
 308       string_at_impl(cp, index, cache_index, CHECK);
 309     }
 310   }
 311 }
 312 #endif
 313 
 314 // CDS support. Create a new resolved_references array.
 315 void ConstantPool::restore_unshareable_info(TRAPS) {
 316   assert(is_constantPool(), "ensure C++ vtable is restored");
 317   assert(on_stack(), "should always be set for shared constant pools");
 318   assert(is_shared(), "should always be set for shared constant pools");
 319   assert(_cache != NULL, "constant pool _cache should not be NULL");
 320 
 321   // Only create the new resolved references array if it hasn't been attempted before
 322   if (resolved_references() != NULL) return;
 323 
 324   // restore the C++ vtable from the shared archive
 325   restore_vtable();
 326 
 327   if (SystemDictionary::Object_klass_loaded()) {
 328     ClassLoaderData* loader_data = pool_holder()->class_loader_data();
 329 #if INCLUDE_CDS_JAVA_HEAP
 330     if (MetaspaceShared::open_archive_heap_region_mapped()) {
 331       oop archived = _cache->archived_references();
 332       if (archived != NULL) {
 333         // Create handle for the archived resolved reference array object
 334         Handle refs_handle(THREAD, archived);
 335         set_resolved_references(loader_data->add_handle(refs_handle));
 336       }
 337     } else
 338 #endif
 339     {
 340       // No mapped archived resolved reference array
 341       // Recreate the object array and add to ClassLoaderData.
 342       int map_length = resolved_reference_length();
 343       if (map_length > 0) {
 344         objArrayOop stom = oopFactory::new_objArray(SystemDictionary::Object_klass(), map_length, CHECK);
 345         Handle refs_handle(THREAD, (oop)stom);  // must handleize.
 346         set_resolved_references(loader_data->add_handle(refs_handle));
 347       }
 348     }
 349   }
 350 }
 351 
 352 void ConstantPool::remove_unshareable_info() {
 353   // Resolved references are not in the shared archive.
 354   // Save the length for restoration.  It is not necessarily the same length
 355   // as reference_map.length() if invokedynamic is saved. It is needed when
 356   // re-creating the resolved reference array if archived heap data cannot be map
 357   // at runtime.
 358   set_resolved_reference_length(
 359     resolved_references() != NULL ? resolved_references()->length() : 0);
 360 
 361   // If archiving heap objects is not allowed, clear the resolved references.
 362   // Otherwise, it is cleared after the resolved references array is cached
 363   // (see archive_resolved_references()).
 364   if (!MetaspaceShared::is_heap_object_archiving_allowed()) {
 365     set_resolved_references(NULL);
 366   }
 367 
 368   // Shared ConstantPools are in the RO region, so the _flags cannot be modified.
 369   // The _on_stack flag is used to prevent ConstantPools from deallocation during
 370   // class redefinition. Since shared ConstantPools cannot be deallocated anyway,
 371   // we always set _on_stack to true to avoid having to change _flags during runtime.
 372   _flags |= (_on_stack | _is_shared);
 373   int num_klasses = 0;
 374   for (int index = 1; index < length(); index++) { // Index 0 is unused
 375     assert(!tag_at(index).is_unresolved_klass_in_error(), "This must not happen during dump time");
 376     if (tag_at(index).is_klass()) {
 377       // This class was resolved as a side effect of executing Java code
 378       // during dump time. We need to restore it back to an UnresolvedClass,
 379       // so that the proper class loading and initialization can happen
 380       // at runtime.
 381       CPKlassSlot kslot = klass_slot_at(index);
 382       int resolved_klass_index = kslot.resolved_klass_index();
 383       int name_index = kslot.name_index();
 384       assert(tag_at(name_index).is_symbol(), "sanity");
 385       resolved_klasses()->at_put(resolved_klass_index, NULL);
 386       tag_at_put(index, JVM_CONSTANT_UnresolvedClass);
 387       assert(klass_name_at(index) == symbol_at(name_index), "sanity");
 388     }
 389   }
 390   if (cache() != NULL) {
 391     cache()->remove_unshareable_info();
 392   }
 393 }
 394 
 395 int ConstantPool::cp_to_object_index(int cp_index) {
 396   // this is harder don't do this so much.
 397   int i = reference_map()->find(cp_index);
 398   // We might not find the index for jsr292 call.
 399   return (i < 0) ? _no_index_sentinel : i;
 400 }
 401 
 402 void ConstantPool::string_at_put(int which, int obj_index, oop str) {
 403   resolved_references()->obj_at_put(obj_index, str);
 404 }
 405 
 406 void ConstantPool::trace_class_resolution(const constantPoolHandle& this_cp, Klass* k) {
 407   ResourceMark rm;
 408   int line_number = -1;
 409   const char * source_file = NULL;
 410   if (JavaThread::current()->has_last_Java_frame()) {
 411     // try to identify the method which called this function.
 412     vframeStream vfst(JavaThread::current());
 413     if (!vfst.at_end()) {
 414       line_number = vfst.method()->line_number_from_bci(vfst.bci());
 415       Symbol* s = vfst.method()->method_holder()->source_file_name();
 416       if (s != NULL) {
 417         source_file = s->as_C_string();
 418       }
 419     }
 420   }
 421   if (k != this_cp->pool_holder()) {
 422     // only print something if the classes are different
 423     if (source_file != NULL) {
 424       log_debug(class, resolve)("%s %s %s:%d",
 425                  this_cp->pool_holder()->external_name(),
 426                  k->external_name(), source_file, line_number);
 427     } else {
 428       log_debug(class, resolve)("%s %s",
 429                  this_cp->pool_holder()->external_name(),
 430                  k->external_name());
 431     }
 432   }
 433 }
 434 
 435 Klass* ConstantPool::klass_at_impl(const constantPoolHandle& this_cp, int which,
 436                                    bool save_resolution_error, TRAPS) {
 437   assert(THREAD->is_Java_thread(), "must be a Java thread");
 438 
 439   // A resolved constantPool entry will contain a Klass*, otherwise a Symbol*.
 440   // It is not safe to rely on the tag bit's here, since we don't have a lock, and
 441   // the entry and tag is not updated atomicly.
 442   CPKlassSlot kslot = this_cp->klass_slot_at(which);
 443   int resolved_klass_index = kslot.resolved_klass_index();
 444   int name_index = kslot.name_index();
 445   assert(this_cp->tag_at(name_index).is_symbol(), "sanity");
 446 
 447   Klass* klass = this_cp->resolved_klasses()->at(resolved_klass_index);
 448   if (klass != NULL) {
 449     return klass;
 450   }
 451 
 452   // This tag doesn't change back to unresolved class unless at a safepoint.
 453   if (this_cp->tag_at(which).is_unresolved_klass_in_error()) {
 454     // The original attempt to resolve this constant pool entry failed so find the
 455     // class of the original error and throw another error of the same class
 456     // (JVMS 5.4.3).
 457     // If there is a detail message, pass that detail message to the error.
 458     // The JVMS does not strictly require us to duplicate the same detail message,
 459     // or any internal exception fields such as cause or stacktrace.  But since the
 460     // detail message is often a class name or other literal string, we will repeat it
 461     // if we can find it in the symbol table.
 462     throw_resolution_error(this_cp, which, CHECK_0);
 463     ShouldNotReachHere();
 464   }
 465 
 466   Handle mirror_handle;
 467   Symbol* name = this_cp->symbol_at(name_index);
 468   Handle loader (THREAD, this_cp->pool_holder()->class_loader());
 469   Handle protection_domain (THREAD, this_cp->pool_holder()->protection_domain());
 470   Klass* k = SystemDictionary::resolve_or_fail(name, loader, protection_domain, true, THREAD);
 471   if (!HAS_PENDING_EXCEPTION) {
 472     // preserve the resolved klass from unloading
 473     mirror_handle = Handle(THREAD, k->java_mirror());
 474     // Do access check for klasses
 475     verify_constant_pool_resolve(this_cp, k, THREAD);
 476   }
 477 
 478   // Failed to resolve class. We must record the errors so that subsequent attempts
 479   // to resolve this constant pool entry fail with the same error (JVMS 5.4.3).
 480   if (HAS_PENDING_EXCEPTION) {
 481     if (save_resolution_error) {
 482       save_and_throw_exception(this_cp, which, constantTag(JVM_CONSTANT_UnresolvedClass), CHECK_NULL);
 483       // If CHECK_NULL above doesn't return the exception, that means that
 484       // some other thread has beaten us and has resolved the class.
 485       // To preserve old behavior, we return the resolved class.
 486       klass = this_cp->resolved_klasses()->at(resolved_klass_index);
 487       assert(klass != NULL, "must be resolved if exception was cleared");
 488       return klass;
 489     } else {
 490       return NULL;  // return the pending exception
 491     }
 492   }
 493 
 494   // Make this class loader depend upon the class loader owning the class reference
 495   ClassLoaderData* this_key = this_cp->pool_holder()->class_loader_data();
 496   this_key->record_dependency(k, CHECK_NULL); // Can throw OOM
 497 
 498   // logging for class+resolve.
 499   if (log_is_enabled(Debug, class, resolve)){
 500     trace_class_resolution(this_cp, k);
 501   }
 502   Klass** adr = this_cp->resolved_klasses()->adr_at(resolved_klass_index);
 503   OrderAccess::release_store(adr, k);
 504   // The interpreter assumes when the tag is stored, the klass is resolved
 505   // and the Klass* stored in _resolved_klasses is non-NULL, so we need
 506   // hardware store ordering here.
 507   this_cp->release_tag_at_put(which, JVM_CONSTANT_Class);
 508   return k;
 509 }
 510 
 511 
 512 // Does not update ConstantPool* - to avoid any exception throwing. Used
 513 // by compiler and exception handling.  Also used to avoid classloads for
 514 // instanceof operations. Returns NULL if the class has not been loaded or
 515 // if the verification of constant pool failed
 516 Klass* ConstantPool::klass_at_if_loaded(const constantPoolHandle& this_cp, int which) {
 517   CPKlassSlot kslot = this_cp->klass_slot_at(which);
 518   int resolved_klass_index = kslot.resolved_klass_index();
 519   int name_index = kslot.name_index();
 520   assert(this_cp->tag_at(name_index).is_symbol(), "sanity");
 521 
 522   Klass* k = this_cp->resolved_klasses()->at(resolved_klass_index);
 523   if (k != NULL) {
 524     return k;
 525   } else {
 526     Thread *thread = Thread::current();
 527     Symbol* name = this_cp->symbol_at(name_index);
 528     oop loader = this_cp->pool_holder()->class_loader();
 529     oop protection_domain = this_cp->pool_holder()->protection_domain();
 530     Handle h_prot (thread, protection_domain);
 531     Handle h_loader (thread, loader);
 532     Klass* k = SystemDictionary::find(name, h_loader, h_prot, thread);
 533 
 534     if (k != NULL) {
 535       // Make sure that resolving is legal
 536       EXCEPTION_MARK;
 537       // return NULL if verification fails
 538       verify_constant_pool_resolve(this_cp, k, THREAD);
 539       if (HAS_PENDING_EXCEPTION) {
 540         CLEAR_PENDING_EXCEPTION;
 541         return NULL;
 542       }
 543       return k;
 544     } else {
 545       return k;
 546     }
 547   }
 548 }
 549 
 550 
 551 Klass* ConstantPool::klass_ref_at_if_loaded(const constantPoolHandle& this_cp, int which) {
 552   return klass_at_if_loaded(this_cp, this_cp->klass_ref_index_at(which));
 553 }
 554 
 555 
 556 Method* ConstantPool::method_at_if_loaded(const constantPoolHandle& cpool,
 557                                                    int which) {
 558   if (cpool->cache() == NULL)  return NULL;  // nothing to load yet
 559   int cache_index = decode_cpcache_index(which, true);
 560   if (!(cache_index >= 0 && cache_index < cpool->cache()->length())) {
 561     // FIXME: should be an assert
 562     log_debug(class, resolve)("bad operand %d in:", which); cpool->print();
 563     return NULL;
 564   }
 565   ConstantPoolCacheEntry* e = cpool->cache()->entry_at(cache_index);
 566   return e->method_if_resolved(cpool);
 567 }
 568 
 569 
 570 bool ConstantPool::has_appendix_at_if_loaded(const constantPoolHandle& cpool, int which) {
 571   if (cpool->cache() == NULL)  return false;  // nothing to load yet
 572   int cache_index = decode_cpcache_index(which, true);
 573   ConstantPoolCacheEntry* e = cpool->cache()->entry_at(cache_index);
 574   return e->has_appendix();
 575 }
 576 
 577 oop ConstantPool::appendix_at_if_loaded(const constantPoolHandle& cpool, int which) {
 578   if (cpool->cache() == NULL)  return NULL;  // nothing to load yet
 579   int cache_index = decode_cpcache_index(which, true);
 580   ConstantPoolCacheEntry* e = cpool->cache()->entry_at(cache_index);
 581   return e->appendix_if_resolved(cpool);
 582 }
 583 
 584 
 585 bool ConstantPool::has_method_type_at_if_loaded(const constantPoolHandle& cpool, int which) {
 586   if (cpool->cache() == NULL)  return false;  // nothing to load yet
 587   int cache_index = decode_cpcache_index(which, true);
 588   ConstantPoolCacheEntry* e = cpool->cache()->entry_at(cache_index);
 589   return e->has_method_type();
 590 }
 591 
 592 oop ConstantPool::method_type_at_if_loaded(const constantPoolHandle& cpool, int which) {
 593   if (cpool->cache() == NULL)  return NULL;  // nothing to load yet
 594   int cache_index = decode_cpcache_index(which, true);
 595   ConstantPoolCacheEntry* e = cpool->cache()->entry_at(cache_index);
 596   return e->method_type_if_resolved(cpool);
 597 }
 598 
 599 
 600 Symbol* ConstantPool::impl_name_ref_at(int which, bool uncached) {
 601   int name_index = name_ref_index_at(impl_name_and_type_ref_index_at(which, uncached));
 602   return symbol_at(name_index);
 603 }
 604 
 605 
 606 Symbol* ConstantPool::impl_signature_ref_at(int which, bool uncached) {
 607   int signature_index = signature_ref_index_at(impl_name_and_type_ref_index_at(which, uncached));
 608   return symbol_at(signature_index);
 609 }
 610 
 611 
 612 int ConstantPool::impl_name_and_type_ref_index_at(int which, bool uncached) {
 613   int i = which;
 614   if (!uncached && cache() != NULL) {
 615     if (ConstantPool::is_invokedynamic_index(which)) {
 616       // Invokedynamic index is index into the constant pool cache
 617       int pool_index = invokedynamic_cp_cache_entry_at(which)->constant_pool_index();
 618       pool_index = invoke_dynamic_name_and_type_ref_index_at(pool_index);
 619       assert(tag_at(pool_index).is_name_and_type(), "");
 620       return pool_index;
 621     }
 622     // change byte-ordering and go via cache
 623     i = remap_instruction_operand_from_cache(which);
 624   } else {
 625     if (tag_at(which).is_invoke_dynamic()) {
 626       int pool_index = invoke_dynamic_name_and_type_ref_index_at(which);
 627       assert(tag_at(pool_index).is_name_and_type(), "");
 628       return pool_index;
 629     }
 630   }
 631   assert(tag_at(i).is_field_or_method(), "Corrupted constant pool");
 632   assert(!tag_at(i).is_invoke_dynamic(), "Must be handled above");
 633   jint ref_index = *int_at_addr(i);
 634   return extract_high_short_from_int(ref_index);
 635 }
 636 
 637 constantTag ConstantPool::impl_tag_ref_at(int which, bool uncached) {
 638   int pool_index = which;
 639   if (!uncached && cache() != NULL) {
 640     if (ConstantPool::is_invokedynamic_index(which)) {
 641       // Invokedynamic index is index into resolved_references
 642       pool_index = invokedynamic_cp_cache_entry_at(which)->constant_pool_index();
 643     } else {
 644       // change byte-ordering and go via cache
 645       pool_index = remap_instruction_operand_from_cache(which);
 646     }
 647   }
 648   return tag_at(pool_index);
 649 }
 650 
 651 int ConstantPool::impl_klass_ref_index_at(int which, bool uncached) {
 652   guarantee(!ConstantPool::is_invokedynamic_index(which),
 653             "an invokedynamic instruction does not have a klass");
 654   int i = which;
 655   if (!uncached && cache() != NULL) {
 656     // change byte-ordering and go via cache
 657     i = remap_instruction_operand_from_cache(which);
 658   }
 659   assert(tag_at(i).is_field_or_method(), "Corrupted constant pool");
 660   jint ref_index = *int_at_addr(i);
 661   return extract_low_short_from_int(ref_index);
 662 }
 663 
 664 
 665 
 666 int ConstantPool::remap_instruction_operand_from_cache(int operand) {
 667   int cpc_index = operand;
 668   DEBUG_ONLY(cpc_index -= CPCACHE_INDEX_TAG);
 669   assert((int)(u2)cpc_index == cpc_index, "clean u2");
 670   int member_index = cache()->entry_at(cpc_index)->constant_pool_index();
 671   return member_index;
 672 }
 673 
 674 
 675 void ConstantPool::verify_constant_pool_resolve(const constantPoolHandle& this_cp, Klass* k, TRAPS) {
 676  if (k->is_instance_klass() || k->is_objArray_klass()) {
 677     InstanceKlass* holder = this_cp->pool_holder();
 678     Klass* elem = k->is_instance_klass() ? k : ObjArrayKlass::cast(k)->bottom_klass();
 679 
 680     // The element type could be a typeArray - we only need the access check if it is
 681     // an reference to another class
 682     if (elem->is_instance_klass()) {
 683       LinkResolver::check_klass_accessability(holder, elem, CHECK);
 684     }
 685   }
 686 }
 687 
 688 
 689 int ConstantPool::name_ref_index_at(int which_nt) {
 690   jint ref_index = name_and_type_at(which_nt);
 691   return extract_low_short_from_int(ref_index);
 692 }
 693 
 694 
 695 int ConstantPool::signature_ref_index_at(int which_nt) {
 696   jint ref_index = name_and_type_at(which_nt);
 697   return extract_high_short_from_int(ref_index);
 698 }
 699 
 700 
 701 Klass* ConstantPool::klass_ref_at(int which, TRAPS) {
 702   return klass_at(klass_ref_index_at(which), THREAD);
 703 }
 704 
 705 Symbol* ConstantPool::klass_name_at(int which) const {
 706   return symbol_at(klass_slot_at(which).name_index());
 707 }
 708 
 709 Symbol* ConstantPool::klass_ref_at_noresolve(int which) {
 710   jint ref_index = klass_ref_index_at(which);
 711   return klass_at_noresolve(ref_index);
 712 }
 713 
 714 Symbol* ConstantPool::uncached_klass_ref_at_noresolve(int which) {
 715   jint ref_index = uncached_klass_ref_index_at(which);
 716   return klass_at_noresolve(ref_index);
 717 }
 718 
 719 char* ConstantPool::string_at_noresolve(int which) {
 720   return unresolved_string_at(which)->as_C_string();
 721 }
 722 
 723 BasicType ConstantPool::basic_type_for_signature_at(int which) const {
 724   return FieldType::basic_type(symbol_at(which));
 725 }
 726 
 727 
 728 void ConstantPool::resolve_string_constants_impl(const constantPoolHandle& this_cp, TRAPS) {
 729   for (int index = 1; index < this_cp->length(); index++) { // Index 0 is unused
 730     if (this_cp->tag_at(index).is_string()) {
 731       this_cp->string_at(index, CHECK);
 732     }
 733   }
 734 }
 735 
 736 Symbol* ConstantPool::exception_message(const constantPoolHandle& this_cp, int which, constantTag tag, oop pending_exception) {
 737   // Dig out the detailed message to reuse if possible
 738   Symbol* message = java_lang_Throwable::detail_message(pending_exception);
 739   if (message != NULL) {
 740     return message;
 741   }
 742 
 743   // Return specific message for the tag
 744   switch (tag.value()) {
 745   case JVM_CONSTANT_UnresolvedClass:
 746     // return the class name in the error message
 747     message = this_cp->klass_name_at(which);
 748     break;
 749   case JVM_CONSTANT_MethodHandle:
 750     // return the method handle name in the error message
 751     message = this_cp->method_handle_name_ref_at(which);
 752     break;
 753   case JVM_CONSTANT_MethodType:
 754     // return the method type signature in the error message
 755     message = this_cp->method_type_signature_at(which);
 756     break;
 757   default:
 758     ShouldNotReachHere();
 759   }
 760 
 761   return message;
 762 }
 763 
 764 void ConstantPool::throw_resolution_error(const constantPoolHandle& this_cp, int which, TRAPS) {
 765   Symbol* message = NULL;
 766   Symbol* error = SystemDictionary::find_resolution_error(this_cp, which, &message);
 767   assert(error != NULL && message != NULL, "checking");
 768   CLEAR_PENDING_EXCEPTION;
 769   ResourceMark rm;
 770   THROW_MSG(error, message->as_C_string());
 771 }
 772 
 773 // If resolution for Class, MethodHandle or MethodType fails, save the exception
 774 // in the resolution error table, so that the same exception is thrown again.
 775 void ConstantPool::save_and_throw_exception(const constantPoolHandle& this_cp, int which,
 776                                             constantTag tag, TRAPS) {
 777   Symbol* error = PENDING_EXCEPTION->klass()->name();
 778 
 779   int error_tag = tag.error_value();
 780 
 781   if (!PENDING_EXCEPTION->
 782     is_a(SystemDictionary::LinkageError_klass())) {
 783     // Just throw the exception and don't prevent these classes from
 784     // being loaded due to virtual machine errors like StackOverflow
 785     // and OutOfMemoryError, etc, or if the thread was hit by stop()
 786     // Needs clarification to section 5.4.3 of the VM spec (see 6308271)
 787   } else if (this_cp->tag_at(which).value() != error_tag) {
 788     Symbol* message = exception_message(this_cp, which, tag, PENDING_EXCEPTION);
 789     SystemDictionary::add_resolution_error(this_cp, which, error, message);
 790     // CAS in the tag.  If a thread beat us to registering this error that's fine.
 791     // If another thread resolved the reference, this is a race condition. This
 792     // thread may have had a security manager or something temporary.
 793     // This doesn't deterministically get an error.   So why do we save this?
 794     // We save this because jvmti can add classes to the bootclass path after
 795     // this error, so it needs to get the same error if the error is first.
 796     jbyte old_tag = Atomic::cmpxchg((jbyte)error_tag,
 797                             (jbyte*)this_cp->tag_addr_at(which), (jbyte)tag.value());
 798     if (old_tag != error_tag && old_tag != tag.value()) {
 799       // MethodHandles and MethodType doesn't change to resolved version.
 800       assert(this_cp->tag_at(which).is_klass(), "Wrong tag value");
 801       // Forget the exception and use the resolved class.
 802       CLEAR_PENDING_EXCEPTION;
 803     }
 804   } else {
 805     // some other thread put this in error state
 806     throw_resolution_error(this_cp, which, CHECK);
 807   }
 808 }
 809 
 810 // Called to resolve constants in the constant pool and return an oop.
 811 // Some constant pool entries cache their resolved oop. This is also
 812 // called to create oops from constants to use in arguments for invokedynamic
 813 oop ConstantPool::resolve_constant_at_impl(const constantPoolHandle& this_cp, int index, int cache_index, TRAPS) {
 814   oop result_oop = NULL;
 815   Handle throw_exception;
 816 
 817   if (cache_index == _possible_index_sentinel) {
 818     // It is possible that this constant is one which is cached in the objects.
 819     // We'll do a linear search.  This should be OK because this usage is rare.
 820     assert(index > 0, "valid index");
 821     cache_index = this_cp->cp_to_object_index(index);
 822   }
 823   assert(cache_index == _no_index_sentinel || cache_index >= 0, "");
 824   assert(index == _no_index_sentinel || index >= 0, "");
 825 
 826   if (cache_index >= 0) {
 827     result_oop = this_cp->resolved_references()->obj_at(cache_index);
 828     if (result_oop != NULL) {
 829       return result_oop;
 830       // That was easy...
 831     }
 832     index = this_cp->object_to_cp_index(cache_index);
 833   }
 834 
 835   jvalue prim_value;  // temp used only in a few cases below
 836 
 837   constantTag tag = this_cp->tag_at(index);
 838 
 839   switch (tag.value()) {
 840 
 841   case JVM_CONSTANT_UnresolvedClass:
 842   case JVM_CONSTANT_UnresolvedClassInError:
 843   case JVM_CONSTANT_Class:
 844     {
 845       assert(cache_index == _no_index_sentinel, "should not have been set");
 846       Klass* resolved = klass_at_impl(this_cp, index, true, CHECK_NULL);
 847       // ldc wants the java mirror.
 848       result_oop = resolved->java_mirror();
 849       break;
 850     }
 851 
 852   case JVM_CONSTANT_String:
 853     assert(cache_index != _no_index_sentinel, "should have been set");
 854     if (this_cp->is_pseudo_string_at(index)) {
 855       result_oop = this_cp->pseudo_string_at(index, cache_index);
 856       break;
 857     }
 858     result_oop = string_at_impl(this_cp, index, cache_index, CHECK_NULL);
 859     break;
 860 
 861   case JVM_CONSTANT_MethodHandleInError:
 862   case JVM_CONSTANT_MethodTypeInError:
 863     {
 864       throw_resolution_error(this_cp, index, CHECK_NULL);
 865       break;
 866     }
 867 
 868   case JVM_CONSTANT_MethodHandle:
 869     {
 870       int ref_kind                 = this_cp->method_handle_ref_kind_at(index);
 871       int callee_index             = this_cp->method_handle_klass_index_at(index);
 872       Symbol*  name =      this_cp->method_handle_name_ref_at(index);
 873       Symbol*  signature = this_cp->method_handle_signature_ref_at(index);
 874       constantTag m_tag  = this_cp->tag_at(this_cp->method_handle_index_at(index));
 875       { ResourceMark rm(THREAD);
 876         log_debug(class, resolve)("resolve JVM_CONSTANT_MethodHandle:%d [%d/%d/%d] %s.%s",
 877                               ref_kind, index, this_cp->method_handle_index_at(index),
 878                               callee_index, name->as_C_string(), signature->as_C_string());
 879       }
 880 
 881       Klass* callee = klass_at_impl(this_cp, callee_index, true, CHECK_NULL);
 882 
 883       // Check constant pool method consistency
 884       if ((callee->is_interface() && m_tag.is_method()) ||
 885           ((!callee->is_interface() && m_tag.is_interface_method()))) {
 886         ResourceMark rm(THREAD);
 887         char buf[400];
 888         jio_snprintf(buf, sizeof(buf),
 889           "Inconsistent constant pool data in classfile for class %s. "
 890           "Method %s%s at index %d is %s and should be %s",
 891           callee->name()->as_C_string(), name->as_C_string(), signature->as_C_string(), index,
 892           callee->is_interface() ? "CONSTANT_MethodRef" : "CONSTANT_InterfaceMethodRef",
 893           callee->is_interface() ? "CONSTANT_InterfaceMethodRef" : "CONSTANT_MethodRef");
 894         THROW_MSG_NULL(vmSymbols::java_lang_IncompatibleClassChangeError(), buf);
 895       }
 896 
 897       Klass* klass = this_cp->pool_holder();
 898       Handle value = SystemDictionary::link_method_handle_constant(klass, ref_kind,
 899                                                                    callee, name, signature,
 900                                                                    THREAD);
 901       result_oop = value();
 902       if (HAS_PENDING_EXCEPTION) {
 903         save_and_throw_exception(this_cp, index, tag, CHECK_NULL);
 904       }
 905       break;
 906     }
 907 
 908   case JVM_CONSTANT_MethodType:
 909     {
 910       Symbol*  signature = this_cp->method_type_signature_at(index);
 911       { ResourceMark rm(THREAD);
 912         log_debug(class, resolve)("resolve JVM_CONSTANT_MethodType [%d/%d] %s",
 913                               index, this_cp->method_type_index_at(index),
 914                               signature->as_C_string());
 915       }
 916       Klass* klass = this_cp->pool_holder();
 917       Handle value = SystemDictionary::find_method_handle_type(signature, klass, THREAD);
 918       result_oop = value();
 919       if (HAS_PENDING_EXCEPTION) {
 920         save_and_throw_exception(this_cp, index, tag, CHECK_NULL);
 921       }
 922       break;
 923     }
 924 
 925   case JVM_CONSTANT_Integer:
 926     assert(cache_index == _no_index_sentinel, "should not have been set");
 927     prim_value.i = this_cp->int_at(index);
 928     result_oop = java_lang_boxing_object::create(T_INT, &prim_value, CHECK_NULL);
 929     break;
 930 
 931   case JVM_CONSTANT_Float:
 932     assert(cache_index == _no_index_sentinel, "should not have been set");
 933     prim_value.f = this_cp->float_at(index);
 934     result_oop = java_lang_boxing_object::create(T_FLOAT, &prim_value, CHECK_NULL);
 935     break;
 936 
 937   case JVM_CONSTANT_Long:
 938     assert(cache_index == _no_index_sentinel, "should not have been set");
 939     prim_value.j = this_cp->long_at(index);
 940     result_oop = java_lang_boxing_object::create(T_LONG, &prim_value, CHECK_NULL);
 941     break;
 942 
 943   case JVM_CONSTANT_Double:
 944     assert(cache_index == _no_index_sentinel, "should not have been set");
 945     prim_value.d = this_cp->double_at(index);
 946     result_oop = java_lang_boxing_object::create(T_DOUBLE, &prim_value, CHECK_NULL);
 947     break;
 948 
 949   default:
 950     DEBUG_ONLY( tty->print_cr("*** %p: tag at CP[%d/%d] = %d",
 951                               this_cp(), index, cache_index, tag.value()));
 952     assert(false, "unexpected constant tag");
 953     break;
 954   }
 955 
 956   if (cache_index >= 0) {
 957     // Benign race condition:  resolved_references may already be filled in.
 958     // The important thing here is that all threads pick up the same result.
 959     // It doesn't matter which racing thread wins, as long as only one
 960     // result is used by all threads, and all future queries.
 961     oop old_result = this_cp->resolved_references()->atomic_compare_exchange_oop(cache_index, result_oop, NULL);
 962     if (old_result == NULL) {
 963       return result_oop;  // was installed
 964     } else {
 965       // Return the winning thread's result.  This can be different than
 966       // the result here for MethodHandles.
 967       return old_result;
 968     }
 969   } else {
 970     return result_oop;
 971   }
 972 }
 973 
 974 oop ConstantPool::uncached_string_at(int which, TRAPS) {
 975   Symbol* sym = unresolved_string_at(which);
 976   oop str = StringTable::intern(sym, CHECK_(NULL));
 977   assert(java_lang_String::is_instance(str), "must be string");
 978   return str;
 979 }
 980 
 981 
 982 oop ConstantPool::resolve_bootstrap_specifier_at_impl(const constantPoolHandle& this_cp, int index, TRAPS) {
 983   assert(this_cp->tag_at(index).is_invoke_dynamic(), "Corrupted constant pool");
 984 
 985   Handle bsm;
 986   int argc;
 987   {
 988     // JVM_CONSTANT_InvokeDynamic is an ordered pair of [bootm, name&type], plus optional arguments
 989     // The bootm, being a JVM_CONSTANT_MethodHandle, has its own cache entry.
 990     // It is accompanied by the optional arguments.
 991     int bsm_index = this_cp->invoke_dynamic_bootstrap_method_ref_index_at(index);
 992     oop bsm_oop = this_cp->resolve_possibly_cached_constant_at(bsm_index, CHECK_NULL);
 993     if (!java_lang_invoke_MethodHandle::is_instance(bsm_oop)) {
 994       THROW_MSG_NULL(vmSymbols::java_lang_LinkageError(), "BSM not an MethodHandle");
 995     }
 996 
 997     // Extract the optional static arguments.
 998     argc = this_cp->invoke_dynamic_argument_count_at(index);
 999     if (argc == 0)  return bsm_oop;
1000 
1001     bsm = Handle(THREAD, bsm_oop);
1002   }
1003 
1004   objArrayHandle info;
1005   {
1006     objArrayOop info_oop = oopFactory::new_objArray(SystemDictionary::Object_klass(), 1+argc, CHECK_NULL);
1007     info = objArrayHandle(THREAD, info_oop);
1008   }
1009 
1010   info->obj_at_put(0, bsm());
1011   for (int i = 0; i < argc; i++) {
1012     int arg_index = this_cp->invoke_dynamic_argument_index_at(index, i);
1013     oop arg_oop = this_cp->resolve_possibly_cached_constant_at(arg_index, CHECK_NULL);
1014     info->obj_at_put(1+i, arg_oop);
1015   }
1016 
1017   return info();
1018 }
1019 
1020 oop ConstantPool::string_at_impl(const constantPoolHandle& this_cp, int which, int obj_index, TRAPS) {
1021   // If the string has already been interned, this entry will be non-null
1022   oop str = this_cp->resolved_references()->obj_at(obj_index);
1023   if (str != NULL) return str;
1024   Symbol* sym = this_cp->unresolved_string_at(which);
1025   str = StringTable::intern(sym, CHECK_(NULL));
1026   this_cp->string_at_put(which, obj_index, str);
1027   assert(java_lang_String::is_instance(str), "must be string");
1028   return str;
1029 }
1030 
1031 
1032 bool ConstantPool::klass_name_at_matches(const InstanceKlass* k, int which) {
1033   // Names are interned, so we can compare Symbol*s directly
1034   Symbol* cp_name = klass_name_at(which);
1035   return (cp_name == k->name());
1036 }
1037 
1038 
1039 // Iterate over symbols and decrement ones which are Symbol*s
1040 // This is done during GC.
1041 // Only decrement the UTF8 symbols. Strings point to
1042 // these symbols but didn't increment the reference count.
1043 void ConstantPool::unreference_symbols() {
1044   for (int index = 1; index < length(); index++) { // Index 0 is unused
1045     constantTag tag = tag_at(index);
1046     if (tag.is_symbol()) {
1047       symbol_at(index)->decrement_refcount();
1048     }
1049   }
1050 }
1051 
1052 
1053 // Compare this constant pool's entry at index1 to the constant pool
1054 // cp2's entry at index2.
1055 bool ConstantPool::compare_entry_to(int index1, const constantPoolHandle& cp2,
1056        int index2, TRAPS) {
1057 
1058   // The error tags are equivalent to non-error tags when comparing
1059   jbyte t1 = tag_at(index1).non_error_value();
1060   jbyte t2 = cp2->tag_at(index2).non_error_value();
1061 
1062   if (t1 != t2) {
1063     // Not the same entry type so there is nothing else to check. Note
1064     // that this style of checking will consider resolved/unresolved
1065     // class pairs as different.
1066     // From the ConstantPool* API point of view, this is correct
1067     // behavior. See VM_RedefineClasses::merge_constant_pools() to see how this
1068     // plays out in the context of ConstantPool* merging.
1069     return false;
1070   }
1071 
1072   switch (t1) {
1073   case JVM_CONSTANT_Class:
1074   {
1075     Klass* k1 = klass_at(index1, CHECK_false);
1076     Klass* k2 = cp2->klass_at(index2, CHECK_false);
1077     if (k1 == k2) {
1078       return true;
1079     }
1080   } break;
1081 
1082   case JVM_CONSTANT_ClassIndex:
1083   {
1084     int recur1 = klass_index_at(index1);
1085     int recur2 = cp2->klass_index_at(index2);
1086     bool match = compare_entry_to(recur1, cp2, recur2, CHECK_false);
1087     if (match) {
1088       return true;
1089     }
1090   } break;
1091 
1092   case JVM_CONSTANT_Double:
1093   {
1094     jdouble d1 = double_at(index1);
1095     jdouble d2 = cp2->double_at(index2);
1096     if (d1 == d2) {
1097       return true;
1098     }
1099   } break;
1100 
1101   case JVM_CONSTANT_Fieldref:
1102   case JVM_CONSTANT_InterfaceMethodref:
1103   case JVM_CONSTANT_Methodref:
1104   {
1105     int recur1 = uncached_klass_ref_index_at(index1);
1106     int recur2 = cp2->uncached_klass_ref_index_at(index2);
1107     bool match = compare_entry_to(recur1, cp2, recur2, CHECK_false);
1108     if (match) {
1109       recur1 = uncached_name_and_type_ref_index_at(index1);
1110       recur2 = cp2->uncached_name_and_type_ref_index_at(index2);
1111       match = compare_entry_to(recur1, cp2, recur2, CHECK_false);
1112       if (match) {
1113         return true;
1114       }
1115     }
1116   } break;
1117 
1118   case JVM_CONSTANT_Float:
1119   {
1120     jfloat f1 = float_at(index1);
1121     jfloat f2 = cp2->float_at(index2);
1122     if (f1 == f2) {
1123       return true;
1124     }
1125   } break;
1126 
1127   case JVM_CONSTANT_Integer:
1128   {
1129     jint i1 = int_at(index1);
1130     jint i2 = cp2->int_at(index2);
1131     if (i1 == i2) {
1132       return true;
1133     }
1134   } break;
1135 
1136   case JVM_CONSTANT_Long:
1137   {
1138     jlong l1 = long_at(index1);
1139     jlong l2 = cp2->long_at(index2);
1140     if (l1 == l2) {
1141       return true;
1142     }
1143   } break;
1144 
1145   case JVM_CONSTANT_NameAndType:
1146   {
1147     int recur1 = name_ref_index_at(index1);
1148     int recur2 = cp2->name_ref_index_at(index2);
1149     bool match = compare_entry_to(recur1, cp2, recur2, CHECK_false);
1150     if (match) {
1151       recur1 = signature_ref_index_at(index1);
1152       recur2 = cp2->signature_ref_index_at(index2);
1153       match = compare_entry_to(recur1, cp2, recur2, CHECK_false);
1154       if (match) {
1155         return true;
1156       }
1157     }
1158   } break;
1159 
1160   case JVM_CONSTANT_StringIndex:
1161   {
1162     int recur1 = string_index_at(index1);
1163     int recur2 = cp2->string_index_at(index2);
1164     bool match = compare_entry_to(recur1, cp2, recur2, CHECK_false);
1165     if (match) {
1166       return true;
1167     }
1168   } break;
1169 
1170   case JVM_CONSTANT_UnresolvedClass:
1171   {
1172     Symbol* k1 = klass_name_at(index1);
1173     Symbol* k2 = cp2->klass_name_at(index2);
1174     if (k1 == k2) {
1175       return true;
1176     }
1177   } break;
1178 
1179   case JVM_CONSTANT_MethodType:
1180   {
1181     int k1 = method_type_index_at(index1);
1182     int k2 = cp2->method_type_index_at(index2);
1183     bool match = compare_entry_to(k1, cp2, k2, CHECK_false);
1184     if (match) {
1185       return true;
1186     }
1187   } break;
1188 
1189   case JVM_CONSTANT_MethodHandle:
1190   {
1191     int k1 = method_handle_ref_kind_at(index1);
1192     int k2 = cp2->method_handle_ref_kind_at(index2);
1193     if (k1 == k2) {
1194       int i1 = method_handle_index_at(index1);
1195       int i2 = cp2->method_handle_index_at(index2);
1196       bool match = compare_entry_to(i1, cp2, i2, CHECK_false);
1197       if (match) {
1198         return true;
1199       }
1200     }
1201   } break;
1202 
1203   case JVM_CONSTANT_InvokeDynamic:
1204   {
1205     int k1 = invoke_dynamic_name_and_type_ref_index_at(index1);
1206     int k2 = cp2->invoke_dynamic_name_and_type_ref_index_at(index2);
1207     int i1 = invoke_dynamic_bootstrap_specifier_index(index1);
1208     int i2 = cp2->invoke_dynamic_bootstrap_specifier_index(index2);
1209     // separate statements and variables because CHECK_false is used
1210     bool match_entry = compare_entry_to(k1, cp2, k2, CHECK_false);
1211     bool match_operand = compare_operand_to(i1, cp2, i2, CHECK_false);
1212     return (match_entry && match_operand);
1213   } break;
1214 
1215   case JVM_CONSTANT_String:
1216   {
1217     Symbol* s1 = unresolved_string_at(index1);
1218     Symbol* s2 = cp2->unresolved_string_at(index2);
1219     if (s1 == s2) {
1220       return true;
1221     }
1222   } break;
1223 
1224   case JVM_CONSTANT_Utf8:
1225   {
1226     Symbol* s1 = symbol_at(index1);
1227     Symbol* s2 = cp2->symbol_at(index2);
1228     if (s1 == s2) {
1229       return true;
1230     }
1231   } break;
1232 
1233   // Invalid is used as the tag for the second constant pool entry
1234   // occupied by JVM_CONSTANT_Double or JVM_CONSTANT_Long. It should
1235   // not be seen by itself.
1236   case JVM_CONSTANT_Invalid: // fall through
1237 
1238   default:
1239     ShouldNotReachHere();
1240     break;
1241   }
1242 
1243   return false;
1244 } // end compare_entry_to()
1245 
1246 
1247 // Resize the operands array with delta_len and delta_size.
1248 // Used in RedefineClasses for CP merge.
1249 void ConstantPool::resize_operands(int delta_len, int delta_size, TRAPS) {
1250   int old_len  = operand_array_length(operands());
1251   int new_len  = old_len + delta_len;
1252   int min_len  = (delta_len > 0) ? old_len : new_len;
1253 
1254   int old_size = operands()->length();
1255   int new_size = old_size + delta_size;
1256   int min_size = (delta_size > 0) ? old_size : new_size;
1257 
1258   ClassLoaderData* loader_data = pool_holder()->class_loader_data();
1259   Array<u2>* new_ops = MetadataFactory::new_array<u2>(loader_data, new_size, CHECK);
1260 
1261   // Set index in the resized array for existing elements only
1262   for (int idx = 0; idx < min_len; idx++) {
1263     int offset = operand_offset_at(idx);                       // offset in original array
1264     operand_offset_at_put(new_ops, idx, offset + 2*delta_len); // offset in resized array
1265   }
1266   // Copy the bootstrap specifiers only
1267   Copy::conjoint_memory_atomic(operands()->adr_at(2*old_len),
1268                                new_ops->adr_at(2*new_len),
1269                                (min_size - 2*min_len) * sizeof(u2));
1270   // Explicitly deallocate old operands array.
1271   // Note, it is not needed for 7u backport.
1272   if ( operands() != NULL) { // the safety check
1273     MetadataFactory::free_array<u2>(loader_data, operands());
1274   }
1275   set_operands(new_ops);
1276 } // end resize_operands()
1277 
1278 
1279 // Extend the operands array with the length and size of the ext_cp operands.
1280 // Used in RedefineClasses for CP merge.
1281 void ConstantPool::extend_operands(const constantPoolHandle& ext_cp, TRAPS) {
1282   int delta_len = operand_array_length(ext_cp->operands());
1283   if (delta_len == 0) {
1284     return; // nothing to do
1285   }
1286   int delta_size = ext_cp->operands()->length();
1287 
1288   assert(delta_len  > 0 && delta_size > 0, "extended operands array must be bigger");
1289 
1290   if (operand_array_length(operands()) == 0) {
1291     ClassLoaderData* loader_data = pool_holder()->class_loader_data();
1292     Array<u2>* new_ops = MetadataFactory::new_array<u2>(loader_data, delta_size, CHECK);
1293     // The first element index defines the offset of second part
1294     operand_offset_at_put(new_ops, 0, 2*delta_len); // offset in new array
1295     set_operands(new_ops);
1296   } else {
1297     resize_operands(delta_len, delta_size, CHECK);
1298   }
1299 
1300 } // end extend_operands()
1301 
1302 
1303 // Shrink the operands array to a smaller array with new_len length.
1304 // Used in RedefineClasses for CP merge.
1305 void ConstantPool::shrink_operands(int new_len, TRAPS) {
1306   int old_len = operand_array_length(operands());
1307   if (new_len == old_len) {
1308     return; // nothing to do
1309   }
1310   assert(new_len < old_len, "shrunken operands array must be smaller");
1311 
1312   int free_base  = operand_next_offset_at(new_len - 1);
1313   int delta_len  = new_len - old_len;
1314   int delta_size = 2*delta_len + free_base - operands()->length();
1315 
1316   resize_operands(delta_len, delta_size, CHECK);
1317 
1318 } // end shrink_operands()
1319 
1320 
1321 void ConstantPool::copy_operands(const constantPoolHandle& from_cp,
1322                                  const constantPoolHandle& to_cp,
1323                                  TRAPS) {
1324 
1325   int from_oplen = operand_array_length(from_cp->operands());
1326   int old_oplen  = operand_array_length(to_cp->operands());
1327   if (from_oplen != 0) {
1328     ClassLoaderData* loader_data = to_cp->pool_holder()->class_loader_data();
1329     // append my operands to the target's operands array
1330     if (old_oplen == 0) {
1331       // Can't just reuse from_cp's operand list because of deallocation issues
1332       int len = from_cp->operands()->length();
1333       Array<u2>* new_ops = MetadataFactory::new_array<u2>(loader_data, len, CHECK);
1334       Copy::conjoint_memory_atomic(
1335           from_cp->operands()->adr_at(0), new_ops->adr_at(0), len * sizeof(u2));
1336       to_cp->set_operands(new_ops);
1337     } else {
1338       int old_len  = to_cp->operands()->length();
1339       int from_len = from_cp->operands()->length();
1340       int old_off  = old_oplen * sizeof(u2);
1341       int from_off = from_oplen * sizeof(u2);
1342       // Use the metaspace for the destination constant pool
1343       Array<u2>* new_operands = MetadataFactory::new_array<u2>(loader_data, old_len + from_len, CHECK);
1344       int fillp = 0, len = 0;
1345       // first part of dest
1346       Copy::conjoint_memory_atomic(to_cp->operands()->adr_at(0),
1347                                    new_operands->adr_at(fillp),
1348                                    (len = old_off) * sizeof(u2));
1349       fillp += len;
1350       // first part of src
1351       Copy::conjoint_memory_atomic(from_cp->operands()->adr_at(0),
1352                                    new_operands->adr_at(fillp),
1353                                    (len = from_off) * sizeof(u2));
1354       fillp += len;
1355       // second part of dest
1356       Copy::conjoint_memory_atomic(to_cp->operands()->adr_at(old_off),
1357                                    new_operands->adr_at(fillp),
1358                                    (len = old_len - old_off) * sizeof(u2));
1359       fillp += len;
1360       // second part of src
1361       Copy::conjoint_memory_atomic(from_cp->operands()->adr_at(from_off),
1362                                    new_operands->adr_at(fillp),
1363                                    (len = from_len - from_off) * sizeof(u2));
1364       fillp += len;
1365       assert(fillp == new_operands->length(), "");
1366 
1367       // Adjust indexes in the first part of the copied operands array.
1368       for (int j = 0; j < from_oplen; j++) {
1369         int offset = operand_offset_at(new_operands, old_oplen + j);
1370         assert(offset == operand_offset_at(from_cp->operands(), j), "correct copy");
1371         offset += old_len;  // every new tuple is preceded by old_len extra u2's
1372         operand_offset_at_put(new_operands, old_oplen + j, offset);
1373       }
1374 
1375       // replace target operands array with combined array
1376       to_cp->set_operands(new_operands);
1377     }
1378   }
1379 } // end copy_operands()
1380 
1381 
1382 // Copy this constant pool's entries at start_i to end_i (inclusive)
1383 // to the constant pool to_cp's entries starting at to_i. A total of
1384 // (end_i - start_i) + 1 entries are copied.
1385 void ConstantPool::copy_cp_to_impl(const constantPoolHandle& from_cp, int start_i, int end_i,
1386        const constantPoolHandle& to_cp, int to_i, TRAPS) {
1387 
1388 
1389   int dest_i = to_i;  // leave original alone for debug purposes
1390 
1391   for (int src_i = start_i; src_i <= end_i; /* see loop bottom */ ) {
1392     copy_entry_to(from_cp, src_i, to_cp, dest_i, CHECK);
1393 
1394     switch (from_cp->tag_at(src_i).value()) {
1395     case JVM_CONSTANT_Double:
1396     case JVM_CONSTANT_Long:
1397       // double and long take two constant pool entries
1398       src_i += 2;
1399       dest_i += 2;
1400       break;
1401 
1402     default:
1403       // all others take one constant pool entry
1404       src_i++;
1405       dest_i++;
1406       break;
1407     }
1408   }
1409   copy_operands(from_cp, to_cp, CHECK);
1410 
1411 } // end copy_cp_to_impl()
1412 
1413 
1414 // Copy this constant pool's entry at from_i to the constant pool
1415 // to_cp's entry at to_i.
1416 void ConstantPool::copy_entry_to(const constantPoolHandle& from_cp, int from_i,
1417                                         const constantPoolHandle& to_cp, int to_i,
1418                                         TRAPS) {
1419 
1420   int tag = from_cp->tag_at(from_i).value();
1421   switch (tag) {
1422   case JVM_CONSTANT_ClassIndex:
1423   {
1424     jint ki = from_cp->klass_index_at(from_i);
1425     to_cp->klass_index_at_put(to_i, ki);
1426   } break;
1427 
1428   case JVM_CONSTANT_Double:
1429   {
1430     jdouble d = from_cp->double_at(from_i);
1431     to_cp->double_at_put(to_i, d);
1432     // double takes two constant pool entries so init second entry's tag
1433     to_cp->tag_at_put(to_i + 1, JVM_CONSTANT_Invalid);
1434   } break;
1435 
1436   case JVM_CONSTANT_Fieldref:
1437   {
1438     int class_index = from_cp->uncached_klass_ref_index_at(from_i);
1439     int name_and_type_index = from_cp->uncached_name_and_type_ref_index_at(from_i);
1440     to_cp->field_at_put(to_i, class_index, name_and_type_index);
1441   } break;
1442 
1443   case JVM_CONSTANT_Float:
1444   {
1445     jfloat f = from_cp->float_at(from_i);
1446     to_cp->float_at_put(to_i, f);
1447   } break;
1448 
1449   case JVM_CONSTANT_Integer:
1450   {
1451     jint i = from_cp->int_at(from_i);
1452     to_cp->int_at_put(to_i, i);
1453   } break;
1454 
1455   case JVM_CONSTANT_InterfaceMethodref:
1456   {
1457     int class_index = from_cp->uncached_klass_ref_index_at(from_i);
1458     int name_and_type_index = from_cp->uncached_name_and_type_ref_index_at(from_i);
1459     to_cp->interface_method_at_put(to_i, class_index, name_and_type_index);
1460   } break;
1461 
1462   case JVM_CONSTANT_Long:
1463   {
1464     jlong l = from_cp->long_at(from_i);
1465     to_cp->long_at_put(to_i, l);
1466     // long takes two constant pool entries so init second entry's tag
1467     to_cp->tag_at_put(to_i + 1, JVM_CONSTANT_Invalid);
1468   } break;
1469 
1470   case JVM_CONSTANT_Methodref:
1471   {
1472     int class_index = from_cp->uncached_klass_ref_index_at(from_i);
1473     int name_and_type_index = from_cp->uncached_name_and_type_ref_index_at(from_i);
1474     to_cp->method_at_put(to_i, class_index, name_and_type_index);
1475   } break;
1476 
1477   case JVM_CONSTANT_NameAndType:
1478   {
1479     int name_ref_index = from_cp->name_ref_index_at(from_i);
1480     int signature_ref_index = from_cp->signature_ref_index_at(from_i);
1481     to_cp->name_and_type_at_put(to_i, name_ref_index, signature_ref_index);
1482   } break;
1483 
1484   case JVM_CONSTANT_StringIndex:
1485   {
1486     jint si = from_cp->string_index_at(from_i);
1487     to_cp->string_index_at_put(to_i, si);
1488   } break;
1489 
1490   case JVM_CONSTANT_Class:
1491   case JVM_CONSTANT_UnresolvedClass:
1492   case JVM_CONSTANT_UnresolvedClassInError:
1493   {
1494     // Revert to JVM_CONSTANT_ClassIndex
1495     int name_index = from_cp->klass_slot_at(from_i).name_index();
1496     assert(from_cp->tag_at(name_index).is_symbol(), "sanity");
1497     to_cp->klass_index_at_put(to_i, name_index);
1498   } break;
1499 
1500   case JVM_CONSTANT_String:
1501   {
1502     Symbol* s = from_cp->unresolved_string_at(from_i);
1503     to_cp->unresolved_string_at_put(to_i, s);
1504   } break;
1505 
1506   case JVM_CONSTANT_Utf8:
1507   {
1508     Symbol* s = from_cp->symbol_at(from_i);
1509     // Need to increase refcount, the old one will be thrown away and deferenced
1510     s->increment_refcount();
1511     to_cp->symbol_at_put(to_i, s);
1512   } break;
1513 
1514   case JVM_CONSTANT_MethodType:
1515   case JVM_CONSTANT_MethodTypeInError:
1516   {
1517     jint k = from_cp->method_type_index_at(from_i);
1518     to_cp->method_type_index_at_put(to_i, k);
1519   } break;
1520 
1521   case JVM_CONSTANT_MethodHandle:
1522   case JVM_CONSTANT_MethodHandleInError:
1523   {
1524     int k1 = from_cp->method_handle_ref_kind_at(from_i);
1525     int k2 = from_cp->method_handle_index_at(from_i);
1526     to_cp->method_handle_index_at_put(to_i, k1, k2);
1527   } break;
1528 
1529   case JVM_CONSTANT_InvokeDynamic:
1530   {
1531     int k1 = from_cp->invoke_dynamic_bootstrap_specifier_index(from_i);
1532     int k2 = from_cp->invoke_dynamic_name_and_type_ref_index_at(from_i);
1533     k1 += operand_array_length(to_cp->operands());  // to_cp might already have operands
1534     to_cp->invoke_dynamic_at_put(to_i, k1, k2);
1535   } break;
1536 
1537   // Invalid is used as the tag for the second constant pool entry
1538   // occupied by JVM_CONSTANT_Double or JVM_CONSTANT_Long. It should
1539   // not be seen by itself.
1540   case JVM_CONSTANT_Invalid: // fall through
1541 
1542   default:
1543   {
1544     ShouldNotReachHere();
1545   } break;
1546   }
1547 } // end copy_entry_to()
1548 
1549 // Search constant pool search_cp for an entry that matches this
1550 // constant pool's entry at pattern_i. Returns the index of a
1551 // matching entry or zero (0) if there is no matching entry.
1552 int ConstantPool::find_matching_entry(int pattern_i,
1553       const constantPoolHandle& search_cp, TRAPS) {
1554 
1555   // index zero (0) is not used
1556   for (int i = 1; i < search_cp->length(); i++) {
1557     bool found = compare_entry_to(pattern_i, search_cp, i, CHECK_0);
1558     if (found) {
1559       return i;
1560     }
1561   }
1562 
1563   return 0;  // entry not found; return unused index zero (0)
1564 } // end find_matching_entry()
1565 
1566 
1567 // Compare this constant pool's bootstrap specifier at idx1 to the constant pool
1568 // cp2's bootstrap specifier at idx2.
1569 bool ConstantPool::compare_operand_to(int idx1, const constantPoolHandle& cp2, int idx2, TRAPS) {
1570   int k1 = operand_bootstrap_method_ref_index_at(idx1);
1571   int k2 = cp2->operand_bootstrap_method_ref_index_at(idx2);
1572   bool match = compare_entry_to(k1, cp2, k2, CHECK_false);
1573 
1574   if (!match) {
1575     return false;
1576   }
1577   int argc = operand_argument_count_at(idx1);
1578   if (argc == cp2->operand_argument_count_at(idx2)) {
1579     for (int j = 0; j < argc; j++) {
1580       k1 = operand_argument_index_at(idx1, j);
1581       k2 = cp2->operand_argument_index_at(idx2, j);
1582       match = compare_entry_to(k1, cp2, k2, CHECK_false);
1583       if (!match) {
1584         return false;
1585       }
1586     }
1587     return true;           // got through loop; all elements equal
1588   }
1589   return false;
1590 } // end compare_operand_to()
1591 
1592 // Search constant pool search_cp for a bootstrap specifier that matches
1593 // this constant pool's bootstrap specifier at pattern_i index.
1594 // Return the index of a matching bootstrap specifier or (-1) if there is no match.
1595 int ConstantPool::find_matching_operand(int pattern_i,
1596                     const constantPoolHandle& search_cp, int search_len, TRAPS) {
1597   for (int i = 0; i < search_len; i++) {
1598     bool found = compare_operand_to(pattern_i, search_cp, i, CHECK_(-1));
1599     if (found) {
1600       return i;
1601     }
1602   }
1603   return -1;  // bootstrap specifier not found; return unused index (-1)
1604 } // end find_matching_operand()
1605 
1606 
1607 #ifndef PRODUCT
1608 
1609 const char* ConstantPool::printable_name_at(int which) {
1610 
1611   constantTag tag = tag_at(which);
1612 
1613   if (tag.is_string()) {
1614     return string_at_noresolve(which);
1615   } else if (tag.is_klass() || tag.is_unresolved_klass()) {
1616     return klass_name_at(which)->as_C_string();
1617   } else if (tag.is_symbol()) {
1618     return symbol_at(which)->as_C_string();
1619   }
1620   return "";
1621 }
1622 
1623 #endif // PRODUCT
1624 
1625 
1626 // JVMTI GetConstantPool support
1627 
1628 // For debugging of constant pool
1629 const bool debug_cpool = false;
1630 
1631 #define DBG(code) do { if (debug_cpool) { (code); } } while(0)
1632 
1633 static void print_cpool_bytes(jint cnt, u1 *bytes) {
1634   const char* WARN_MSG = "Must not be such entry!";
1635   jint size = 0;
1636   u2   idx1, idx2;
1637 
1638   for (jint idx = 1; idx < cnt; idx++) {
1639     jint ent_size = 0;
1640     u1   tag  = *bytes++;
1641     size++;                       // count tag
1642 
1643     printf("const #%03d, tag: %02d ", idx, tag);
1644     switch(tag) {
1645       case JVM_CONSTANT_Invalid: {
1646         printf("Invalid");
1647         break;
1648       }
1649       case JVM_CONSTANT_Unicode: {
1650         printf("Unicode      %s", WARN_MSG);
1651         break;
1652       }
1653       case JVM_CONSTANT_Utf8: {
1654         u2 len = Bytes::get_Java_u2(bytes);
1655         char str[128];
1656         if (len > 127) {
1657            len = 127;
1658         }
1659         strncpy(str, (char *) (bytes+2), len);
1660         str[len] = '\0';
1661         printf("Utf8          \"%s\"", str);
1662         ent_size = 2 + len;
1663         break;
1664       }
1665       case JVM_CONSTANT_Integer: {
1666         u4 val = Bytes::get_Java_u4(bytes);
1667         printf("int          %d", *(int *) &val);
1668         ent_size = 4;
1669         break;
1670       }
1671       case JVM_CONSTANT_Float: {
1672         u4 val = Bytes::get_Java_u4(bytes);
1673         printf("float        %5.3ff", *(float *) &val);
1674         ent_size = 4;
1675         break;
1676       }
1677       case JVM_CONSTANT_Long: {
1678         u8 val = Bytes::get_Java_u8(bytes);
1679         printf("long         " INT64_FORMAT, (int64_t) *(jlong *) &val);
1680         ent_size = 8;
1681         idx++; // Long takes two cpool slots
1682         break;
1683       }
1684       case JVM_CONSTANT_Double: {
1685         u8 val = Bytes::get_Java_u8(bytes);
1686         printf("double       %5.3fd", *(jdouble *)&val);
1687         ent_size = 8;
1688         idx++; // Double takes two cpool slots
1689         break;
1690       }
1691       case JVM_CONSTANT_Class: {
1692         idx1 = Bytes::get_Java_u2(bytes);
1693         printf("class        #%03d", idx1);
1694         ent_size = 2;
1695         break;
1696       }
1697       case JVM_CONSTANT_String: {
1698         idx1 = Bytes::get_Java_u2(bytes);
1699         printf("String       #%03d", idx1);
1700         ent_size = 2;
1701         break;
1702       }
1703       case JVM_CONSTANT_Fieldref: {
1704         idx1 = Bytes::get_Java_u2(bytes);
1705         idx2 = Bytes::get_Java_u2(bytes+2);
1706         printf("Field        #%03d, #%03d", (int) idx1, (int) idx2);
1707         ent_size = 4;
1708         break;
1709       }
1710       case JVM_CONSTANT_Methodref: {
1711         idx1 = Bytes::get_Java_u2(bytes);
1712         idx2 = Bytes::get_Java_u2(bytes+2);
1713         printf("Method       #%03d, #%03d", idx1, idx2);
1714         ent_size = 4;
1715         break;
1716       }
1717       case JVM_CONSTANT_InterfaceMethodref: {
1718         idx1 = Bytes::get_Java_u2(bytes);
1719         idx2 = Bytes::get_Java_u2(bytes+2);
1720         printf("InterfMethod #%03d, #%03d", idx1, idx2);
1721         ent_size = 4;
1722         break;
1723       }
1724       case JVM_CONSTANT_NameAndType: {
1725         idx1 = Bytes::get_Java_u2(bytes);
1726         idx2 = Bytes::get_Java_u2(bytes+2);
1727         printf("NameAndType  #%03d, #%03d", idx1, idx2);
1728         ent_size = 4;
1729         break;
1730       }
1731       case JVM_CONSTANT_ClassIndex: {
1732         printf("ClassIndex  %s", WARN_MSG);
1733         break;
1734       }
1735       case JVM_CONSTANT_UnresolvedClass: {
1736         printf("UnresolvedClass: %s", WARN_MSG);
1737         break;
1738       }
1739       case JVM_CONSTANT_UnresolvedClassInError: {
1740         printf("UnresolvedClassInErr: %s", WARN_MSG);
1741         break;
1742       }
1743       case JVM_CONSTANT_StringIndex: {
1744         printf("StringIndex: %s", WARN_MSG);
1745         break;
1746       }
1747     }
1748     printf(";\n");
1749     bytes += ent_size;
1750     size  += ent_size;
1751   }
1752   printf("Cpool size: %d\n", size);
1753   fflush(0);
1754   return;
1755 } /* end print_cpool_bytes */
1756 
1757 
1758 // Returns size of constant pool entry.
1759 jint ConstantPool::cpool_entry_size(jint idx) {
1760   switch(tag_at(idx).value()) {
1761     case JVM_CONSTANT_Invalid:
1762     case JVM_CONSTANT_Unicode:
1763       return 1;
1764 
1765     case JVM_CONSTANT_Utf8:
1766       return 3 + symbol_at(idx)->utf8_length();
1767 
1768     case JVM_CONSTANT_Class:
1769     case JVM_CONSTANT_String:
1770     case JVM_CONSTANT_ClassIndex:
1771     case JVM_CONSTANT_UnresolvedClass:
1772     case JVM_CONSTANT_UnresolvedClassInError:
1773     case JVM_CONSTANT_StringIndex:
1774     case JVM_CONSTANT_MethodType:
1775     case JVM_CONSTANT_MethodTypeInError:
1776       return 3;
1777 
1778     case JVM_CONSTANT_MethodHandle:
1779     case JVM_CONSTANT_MethodHandleInError:
1780       return 4; //tag, ref_kind, ref_index
1781 
1782     case JVM_CONSTANT_Integer:
1783     case JVM_CONSTANT_Float:
1784     case JVM_CONSTANT_Fieldref:
1785     case JVM_CONSTANT_Methodref:
1786     case JVM_CONSTANT_InterfaceMethodref:
1787     case JVM_CONSTANT_NameAndType:
1788       return 5;
1789 
1790     case JVM_CONSTANT_InvokeDynamic:
1791       // u1 tag, u2 bsm, u2 nt
1792       return 5;
1793 
1794     case JVM_CONSTANT_Long:
1795     case JVM_CONSTANT_Double:
1796       return 9;
1797   }
1798   assert(false, "cpool_entry_size: Invalid constant pool entry tag");
1799   return 1;
1800 } /* end cpool_entry_size */
1801 
1802 
1803 // SymbolHashMap is used to find a constant pool index from a string.
1804 // This function fills in SymbolHashMaps, one for utf8s and one for
1805 // class names, returns size of the cpool raw bytes.
1806 jint ConstantPool::hash_entries_to(SymbolHashMap *symmap,
1807                                           SymbolHashMap *classmap) {
1808   jint size = 0;
1809 
1810   for (u2 idx = 1; idx < length(); idx++) {
1811     u2 tag = tag_at(idx).value();
1812     size += cpool_entry_size(idx);
1813 
1814     switch(tag) {
1815       case JVM_CONSTANT_Utf8: {
1816         Symbol* sym = symbol_at(idx);
1817         symmap->add_entry(sym, idx);
1818         DBG(printf("adding symbol entry %s = %d\n", sym->as_utf8(), idx));
1819         break;
1820       }
1821       case JVM_CONSTANT_Class:
1822       case JVM_CONSTANT_UnresolvedClass:
1823       case JVM_CONSTANT_UnresolvedClassInError: {
1824         Symbol* sym = klass_name_at(idx);
1825         classmap->add_entry(sym, idx);
1826         DBG(printf("adding class entry %s = %d\n", sym->as_utf8(), idx));
1827         break;
1828       }
1829       case JVM_CONSTANT_Long:
1830       case JVM_CONSTANT_Double: {
1831         idx++; // Both Long and Double take two cpool slots
1832         break;
1833       }
1834     }
1835   }
1836   return size;
1837 } /* end hash_utf8_entries_to */
1838 
1839 
1840 // Copy cpool bytes.
1841 // Returns:
1842 //    0, in case of OutOfMemoryError
1843 //   -1, in case of internal error
1844 //  > 0, count of the raw cpool bytes that have been copied
1845 int ConstantPool::copy_cpool_bytes(int cpool_size,
1846                                           SymbolHashMap* tbl,
1847                                           unsigned char *bytes) {
1848   u2   idx1, idx2;
1849   jint size  = 0;
1850   jint cnt   = length();
1851   unsigned char *start_bytes = bytes;
1852 
1853   for (jint idx = 1; idx < cnt; idx++) {
1854     u1   tag      = tag_at(idx).value();
1855     jint ent_size = cpool_entry_size(idx);
1856 
1857     assert(size + ent_size <= cpool_size, "Size mismatch");
1858 
1859     *bytes = tag;
1860     DBG(printf("#%03hd tag=%03hd, ", (short)idx, (short)tag));
1861     switch(tag) {
1862       case JVM_CONSTANT_Invalid: {
1863         DBG(printf("JVM_CONSTANT_Invalid"));
1864         break;
1865       }
1866       case JVM_CONSTANT_Unicode: {
1867         assert(false, "Wrong constant pool tag: JVM_CONSTANT_Unicode");
1868         DBG(printf("JVM_CONSTANT_Unicode"));
1869         break;
1870       }
1871       case JVM_CONSTANT_Utf8: {
1872         Symbol* sym = symbol_at(idx);
1873         char*     str = sym->as_utf8();
1874         // Warning! It's crashing on x86 with len = sym->utf8_length()
1875         int       len = (int) strlen(str);
1876         Bytes::put_Java_u2((address) (bytes+1), (u2) len);
1877         for (int i = 0; i < len; i++) {
1878             bytes[3+i] = (u1) str[i];
1879         }
1880         DBG(printf("JVM_CONSTANT_Utf8: %s ", str));
1881         break;
1882       }
1883       case JVM_CONSTANT_Integer: {
1884         jint val = int_at(idx);
1885         Bytes::put_Java_u4((address) (bytes+1), *(u4*)&val);
1886         break;
1887       }
1888       case JVM_CONSTANT_Float: {
1889         jfloat val = float_at(idx);
1890         Bytes::put_Java_u4((address) (bytes+1), *(u4*)&val);
1891         break;
1892       }
1893       case JVM_CONSTANT_Long: {
1894         jlong val = long_at(idx);
1895         Bytes::put_Java_u8((address) (bytes+1), *(u8*)&val);
1896         idx++;             // Long takes two cpool slots
1897         break;
1898       }
1899       case JVM_CONSTANT_Double: {
1900         jdouble val = double_at(idx);
1901         Bytes::put_Java_u8((address) (bytes+1), *(u8*)&val);
1902         idx++;             // Double takes two cpool slots
1903         break;
1904       }
1905       case JVM_CONSTANT_Class:
1906       case JVM_CONSTANT_UnresolvedClass:
1907       case JVM_CONSTANT_UnresolvedClassInError: {
1908         *bytes = JVM_CONSTANT_Class;
1909         Symbol* sym = klass_name_at(idx);
1910         idx1 = tbl->symbol_to_value(sym);
1911         assert(idx1 != 0, "Have not found a hashtable entry");
1912         Bytes::put_Java_u2((address) (bytes+1), idx1);
1913         DBG(printf("JVM_CONSTANT_Class: idx=#%03hd, %s", idx1, sym->as_utf8()));
1914         break;
1915       }
1916       case JVM_CONSTANT_String: {
1917         *bytes = JVM_CONSTANT_String;
1918         Symbol* sym = unresolved_string_at(idx);
1919         idx1 = tbl->symbol_to_value(sym);
1920         assert(idx1 != 0, "Have not found a hashtable entry");
1921         Bytes::put_Java_u2((address) (bytes+1), idx1);
1922         DBG(printf("JVM_CONSTANT_String: idx=#%03hd, %s", idx1, sym->as_utf8()));
1923         break;
1924       }
1925       case JVM_CONSTANT_Fieldref:
1926       case JVM_CONSTANT_Methodref:
1927       case JVM_CONSTANT_InterfaceMethodref: {
1928         idx1 = uncached_klass_ref_index_at(idx);
1929         idx2 = uncached_name_and_type_ref_index_at(idx);
1930         Bytes::put_Java_u2((address) (bytes+1), idx1);
1931         Bytes::put_Java_u2((address) (bytes+3), idx2);
1932         DBG(printf("JVM_CONSTANT_Methodref: %hd %hd", idx1, idx2));
1933         break;
1934       }
1935       case JVM_CONSTANT_NameAndType: {
1936         idx1 = name_ref_index_at(idx);
1937         idx2 = signature_ref_index_at(idx);
1938         Bytes::put_Java_u2((address) (bytes+1), idx1);
1939         Bytes::put_Java_u2((address) (bytes+3), idx2);
1940         DBG(printf("JVM_CONSTANT_NameAndType: %hd %hd", idx1, idx2));
1941         break;
1942       }
1943       case JVM_CONSTANT_ClassIndex: {
1944         *bytes = JVM_CONSTANT_Class;
1945         idx1 = klass_index_at(idx);
1946         Bytes::put_Java_u2((address) (bytes+1), idx1);
1947         DBG(printf("JVM_CONSTANT_ClassIndex: %hd", idx1));
1948         break;
1949       }
1950       case JVM_CONSTANT_StringIndex: {
1951         *bytes = JVM_CONSTANT_String;
1952         idx1 = string_index_at(idx);
1953         Bytes::put_Java_u2((address) (bytes+1), idx1);
1954         DBG(printf("JVM_CONSTANT_StringIndex: %hd", idx1));
1955         break;
1956       }
1957       case JVM_CONSTANT_MethodHandle:
1958       case JVM_CONSTANT_MethodHandleInError: {
1959         *bytes = JVM_CONSTANT_MethodHandle;
1960         int kind = method_handle_ref_kind_at(idx);
1961         idx1 = method_handle_index_at(idx);
1962         *(bytes+1) = (unsigned char) kind;
1963         Bytes::put_Java_u2((address) (bytes+2), idx1);
1964         DBG(printf("JVM_CONSTANT_MethodHandle: %d %hd", kind, idx1));
1965         break;
1966       }
1967       case JVM_CONSTANT_MethodType:
1968       case JVM_CONSTANT_MethodTypeInError: {
1969         *bytes = JVM_CONSTANT_MethodType;
1970         idx1 = method_type_index_at(idx);
1971         Bytes::put_Java_u2((address) (bytes+1), idx1);
1972         DBG(printf("JVM_CONSTANT_MethodType: %hd", idx1));
1973         break;
1974       }
1975       case JVM_CONSTANT_InvokeDynamic: {
1976         *bytes = tag;
1977         idx1 = extract_low_short_from_int(*int_at_addr(idx));
1978         idx2 = extract_high_short_from_int(*int_at_addr(idx));
1979         assert(idx2 == invoke_dynamic_name_and_type_ref_index_at(idx), "correct half of u4");
1980         Bytes::put_Java_u2((address) (bytes+1), idx1);
1981         Bytes::put_Java_u2((address) (bytes+3), idx2);
1982         DBG(printf("JVM_CONSTANT_InvokeDynamic: %hd %hd", idx1, idx2));
1983         break;
1984       }
1985     }
1986     DBG(printf("\n"));
1987     bytes += ent_size;
1988     size  += ent_size;
1989   }
1990   assert(size == cpool_size, "Size mismatch");
1991 
1992   // Keep temorarily for debugging until it's stable.
1993   DBG(print_cpool_bytes(cnt, start_bytes));
1994   return (int)(bytes - start_bytes);
1995 } /* end copy_cpool_bytes */
1996 
1997 #undef DBG
1998 
1999 
2000 void ConstantPool::set_on_stack(const bool value) {
2001   if (value) {
2002     // Only record if it's not already set.
2003     if (!on_stack()) {
2004       assert(!is_shared(), "should always be set for shared constant pools");
2005       _flags |= _on_stack;
2006       MetadataOnStackMark::record(this);
2007     }
2008   } else {
2009     // Clearing is done single-threadedly.
2010     if (!is_shared()) {
2011       _flags &= ~_on_stack;
2012     }
2013   }
2014 }
2015 
2016 // JSR 292 support for patching constant pool oops after the class is linked and
2017 // the oop array for resolved references are created.
2018 // We can't do this during classfile parsing, which is how the other indexes are
2019 // patched.  The other patches are applied early for some error checking
2020 // so only defer the pseudo_strings.
2021 void ConstantPool::patch_resolved_references(GrowableArray<Handle>* cp_patches) {
2022   for (int index = 1; index < cp_patches->length(); index++) { // Index 0 is unused
2023     Handle patch = cp_patches->at(index);
2024     if (patch.not_null()) {
2025       assert (tag_at(index).is_string(), "should only be string left");
2026       // Patching a string means pre-resolving it.
2027       // The spelling in the constant pool is ignored.
2028       // The constant reference may be any object whatever.
2029       // If it is not a real interned string, the constant is referred
2030       // to as a "pseudo-string", and must be presented to the CP
2031       // explicitly, because it may require scavenging.
2032       int obj_index = cp_to_object_index(index);
2033       pseudo_string_at_put(index, obj_index, patch());
2034      DEBUG_ONLY(cp_patches->at_put(index, Handle());)
2035     }
2036   }
2037 #ifdef ASSERT
2038   // Ensure that all the patches have been used.
2039   for (int index = 0; index < cp_patches->length(); index++) {
2040     assert(cp_patches->at(index).is_null(),
2041            "Unused constant pool patch at %d in class file %s",
2042            index,
2043            pool_holder()->external_name());
2044   }
2045 #endif // ASSERT
2046 }
2047 
2048 #ifndef PRODUCT
2049 
2050 // CompileTheWorld support. Preload all classes loaded references in the passed in constantpool
2051 void ConstantPool::preload_and_initialize_all_classes(ConstantPool* obj, TRAPS) {
2052   guarantee(obj->is_constantPool(), "object must be constant pool");
2053   constantPoolHandle cp(THREAD, (ConstantPool*)obj);
2054   guarantee(cp->pool_holder() != NULL, "must be fully loaded");
2055 
2056   for (int i = 0; i< cp->length();  i++) {
2057     if (cp->tag_at(i).is_unresolved_klass()) {
2058       // This will force loading of the class
2059       Klass* klass = cp->klass_at(i, CHECK);
2060       if (klass->is_instance_klass()) {
2061         // Force initialization of class
2062         InstanceKlass::cast(klass)->initialize(CHECK);
2063       }
2064     }
2065   }
2066 }
2067 
2068 #endif
2069 
2070 
2071 // Printing
2072 
2073 void ConstantPool::print_on(outputStream* st) const {
2074   assert(is_constantPool(), "must be constantPool");
2075   st->print_cr("%s", internal_name());
2076   if (flags() != 0) {
2077     st->print(" - flags: 0x%x", flags());
2078     if (has_preresolution()) st->print(" has_preresolution");
2079     if (on_stack()) st->print(" on_stack");
2080     st->cr();
2081   }
2082   if (pool_holder() != NULL) {
2083     st->print_cr(" - holder: " INTPTR_FORMAT, p2i(pool_holder()));
2084   }
2085   st->print_cr(" - cache: " INTPTR_FORMAT, p2i(cache()));
2086   st->print_cr(" - resolved_references: " INTPTR_FORMAT, p2i(resolved_references()));
2087   st->print_cr(" - reference_map: " INTPTR_FORMAT, p2i(reference_map()));
2088   st->print_cr(" - resolved_klasses: " INTPTR_FORMAT, p2i(resolved_klasses()));
2089 
2090   for (int index = 1; index < length(); index++) {      // Index 0 is unused
2091     ((ConstantPool*)this)->print_entry_on(index, st);
2092     switch (tag_at(index).value()) {
2093       case JVM_CONSTANT_Long :
2094       case JVM_CONSTANT_Double :
2095         index++;   // Skip entry following eigth-byte constant
2096     }
2097 
2098   }
2099   st->cr();
2100 }
2101 
2102 // Print one constant pool entry
2103 void ConstantPool::print_entry_on(const int index, outputStream* st) {
2104   EXCEPTION_MARK;
2105   st->print(" - %3d : ", index);
2106   tag_at(index).print_on(st);
2107   st->print(" : ");
2108   switch (tag_at(index).value()) {
2109     case JVM_CONSTANT_Class :
2110       { Klass* k = klass_at(index, CATCH);
2111         guarantee(k != NULL, "need klass");
2112         k->print_value_on(st);
2113         st->print(" {" PTR_FORMAT "}", p2i(k));
2114       }
2115       break;
2116     case JVM_CONSTANT_Fieldref :
2117     case JVM_CONSTANT_Methodref :
2118     case JVM_CONSTANT_InterfaceMethodref :
2119       st->print("klass_index=%d", uncached_klass_ref_index_at(index));
2120       st->print(" name_and_type_index=%d", uncached_name_and_type_ref_index_at(index));
2121       break;
2122     case JVM_CONSTANT_String :
2123       if (is_pseudo_string_at(index)) {
2124         oop anObj = pseudo_string_at(index);
2125         anObj->print_value_on(st);
2126         st->print(" {" PTR_FORMAT "}", p2i(anObj));
2127       } else {
2128         unresolved_string_at(index)->print_value_on(st);
2129       }
2130       break;
2131     case JVM_CONSTANT_Integer :
2132       st->print("%d", int_at(index));
2133       break;
2134     case JVM_CONSTANT_Float :
2135       st->print("%f", float_at(index));
2136       break;
2137     case JVM_CONSTANT_Long :
2138       st->print_jlong(long_at(index));
2139       break;
2140     case JVM_CONSTANT_Double :
2141       st->print("%lf", double_at(index));
2142       break;
2143     case JVM_CONSTANT_NameAndType :
2144       st->print("name_index=%d", name_ref_index_at(index));
2145       st->print(" signature_index=%d", signature_ref_index_at(index));
2146       break;
2147     case JVM_CONSTANT_Utf8 :
2148       symbol_at(index)->print_value_on(st);
2149       break;
2150     case JVM_CONSTANT_ClassIndex: {
2151         int name_index = *int_at_addr(index);
2152         st->print("klass_index=%d ", name_index);
2153         symbol_at(name_index)->print_value_on(st);
2154       }
2155       break;
2156     case JVM_CONSTANT_UnresolvedClass :               // fall-through
2157     case JVM_CONSTANT_UnresolvedClassInError: {
2158         CPKlassSlot kslot = klass_slot_at(index);
2159         int resolved_klass_index = kslot.resolved_klass_index();
2160         int name_index = kslot.name_index();
2161         assert(tag_at(name_index).is_symbol(), "sanity");
2162 
2163         Klass* klass = resolved_klasses()->at(resolved_klass_index);
2164         if (klass != NULL) {
2165           klass->print_value_on(st);
2166         } else {
2167           symbol_at(name_index)->print_value_on(st);
2168         }
2169       }
2170       break;
2171     case JVM_CONSTANT_MethodHandle :
2172     case JVM_CONSTANT_MethodHandleInError :
2173       st->print("ref_kind=%d", method_handle_ref_kind_at(index));
2174       st->print(" ref_index=%d", method_handle_index_at(index));
2175       break;
2176     case JVM_CONSTANT_MethodType :
2177     case JVM_CONSTANT_MethodTypeInError :
2178       st->print("signature_index=%d", method_type_index_at(index));
2179       break;
2180     case JVM_CONSTANT_InvokeDynamic :
2181       {
2182         st->print("bootstrap_method_index=%d", invoke_dynamic_bootstrap_method_ref_index_at(index));
2183         st->print(" name_and_type_index=%d", invoke_dynamic_name_and_type_ref_index_at(index));
2184         int argc = invoke_dynamic_argument_count_at(index);
2185         if (argc > 0) {
2186           for (int arg_i = 0; arg_i < argc; arg_i++) {
2187             int arg = invoke_dynamic_argument_index_at(index, arg_i);
2188             st->print((arg_i == 0 ? " arguments={%d" : ", %d"), arg);
2189           }
2190           st->print("}");
2191         }
2192       }
2193       break;
2194     default:
2195       ShouldNotReachHere();
2196       break;
2197   }
2198   st->cr();
2199 }
2200 
2201 void ConstantPool::print_value_on(outputStream* st) const {
2202   assert(is_constantPool(), "must be constantPool");
2203   st->print("constant pool [%d]", length());
2204   if (has_preresolution()) st->print("/preresolution");
2205   if (operands() != NULL)  st->print("/operands[%d]", operands()->length());
2206   print_address_on(st);
2207   st->print(" for ");
2208   pool_holder()->print_value_on(st);
2209   if (pool_holder() != NULL) {
2210     bool extra = (pool_holder()->constants() != this);
2211     if (extra)  st->print(" (extra)");
2212   }
2213   if (cache() != NULL) {
2214     st->print(" cache=" PTR_FORMAT, p2i(cache()));
2215   }
2216 }
2217 
2218 #if INCLUDE_SERVICES
2219 // Size Statistics
2220 void ConstantPool::collect_statistics(KlassSizeStats *sz) const {
2221   sz->_cp_all_bytes += (sz->_cp_bytes          = sz->count(this));
2222   sz->_cp_all_bytes += (sz->_cp_tags_bytes     = sz->count_array(tags()));
2223   sz->_cp_all_bytes += (sz->_cp_cache_bytes    = sz->count(cache()));
2224   sz->_cp_all_bytes += (sz->_cp_operands_bytes = sz->count_array(operands()));
2225   sz->_cp_all_bytes += (sz->_cp_refmap_bytes   = sz->count_array(reference_map()));
2226 
2227   sz->_ro_bytes += sz->_cp_operands_bytes + sz->_cp_tags_bytes +
2228                    sz->_cp_refmap_bytes;
2229   sz->_rw_bytes += sz->_cp_bytes + sz->_cp_cache_bytes;
2230 }
2231 #endif // INCLUDE_SERVICES
2232 
2233 // Verification
2234 
2235 void ConstantPool::verify_on(outputStream* st) {
2236   guarantee(is_constantPool(), "object must be constant pool");
2237   for (int i = 0; i< length();  i++) {
2238     constantTag tag = tag_at(i);
2239     if (tag.is_klass() || tag.is_unresolved_klass()) {
2240       guarantee(klass_name_at(i)->refcount() != 0, "should have nonzero reference count");
2241     } else if (tag.is_symbol()) {
2242       CPSlot entry = slot_at(i);
2243       guarantee(entry.get_symbol()->refcount() != 0, "should have nonzero reference count");
2244     } else if (tag.is_string()) {
2245       CPSlot entry = slot_at(i);
2246       guarantee(entry.get_symbol()->refcount() != 0, "should have nonzero reference count");
2247     }
2248   }
2249   if (cache() != NULL) {
2250     // Note: cache() can be NULL before a class is completely setup or
2251     // in temporary constant pools used during constant pool merging
2252     guarantee(cache()->is_constantPoolCache(), "should be constant pool cache");
2253   }
2254   if (pool_holder() != NULL) {
2255     // Note: pool_holder() can be NULL in temporary constant pools
2256     // used during constant pool merging
2257     guarantee(pool_holder()->is_klass(),    "should be klass");
2258   }
2259 }
2260 
2261 
2262 void SymbolHashMap::add_entry(Symbol* sym, u2 value) {
2263   char *str = sym->as_utf8();
2264   unsigned int hash = compute_hash(str, sym->utf8_length());
2265   unsigned int index = hash % table_size();
2266 
2267   // check if already in map
2268   // we prefer the first entry since it is more likely to be what was used in
2269   // the class file
2270   for (SymbolHashMapEntry *en = bucket(index); en != NULL; en = en->next()) {
2271     assert(en->symbol() != NULL, "SymbolHashMapEntry symbol is NULL");
2272     if (en->hash() == hash && en->symbol() == sym) {
2273         return;  // already there
2274     }
2275   }
2276 
2277   SymbolHashMapEntry* entry = new SymbolHashMapEntry(hash, sym, value);
2278   entry->set_next(bucket(index));
2279   _buckets[index].set_entry(entry);
2280   assert(entry->symbol() != NULL, "SymbolHashMapEntry symbol is NULL");
2281 }
2282 
2283 SymbolHashMapEntry* SymbolHashMap::find_entry(Symbol* sym) {
2284   assert(sym != NULL, "SymbolHashMap::find_entry - symbol is NULL");
2285   char *str = sym->as_utf8();
2286   int   len = sym->utf8_length();
2287   unsigned int hash = SymbolHashMap::compute_hash(str, len);
2288   unsigned int index = hash % table_size();
2289   for (SymbolHashMapEntry *en = bucket(index); en != NULL; en = en->next()) {
2290     assert(en->symbol() != NULL, "SymbolHashMapEntry symbol is NULL");
2291     if (en->hash() == hash && en->symbol() == sym) {
2292       return en;
2293     }
2294   }
2295   return NULL;
2296 }
2297 
2298 void SymbolHashMap::initialize_table(int table_size) {
2299   _table_size = table_size;
2300   _buckets = NEW_C_HEAP_ARRAY(SymbolHashMapBucket, table_size, mtSymbol);
2301   for (int index = 0; index < table_size; index++) {
2302     _buckets[index].clear();
2303   }
2304 }