1 /*
   2  * Copyright (c) 1997, 2010, 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 "incls/_precompiled.incl"
  26 # include "incls/_constantPoolOop.cpp.incl"
  27 
  28 void constantPoolOopDesc::set_flag_at(FlagBit fb) {
  29   const int MAX_STATE_CHANGES = 2;
  30   for (int i = MAX_STATE_CHANGES + 10; i > 0; i--) {
  31     int oflags = _flags;
  32     int nflags = oflags | (1 << (int)fb);
  33     if (Atomic::cmpxchg(nflags, &_flags, oflags) == oflags)
  34       return;
  35   }
  36   assert(false, "failed to cmpxchg flags");
  37   _flags |= (1 << (int)fb);     // better than nothing
  38 }
  39 
  40 klassOop constantPoolOopDesc::klass_at_impl(constantPoolHandle this_oop, int which, TRAPS) {
  41   // A resolved constantPool entry will contain a klassOop, otherwise a symbolOop.
  42   // It is not safe to rely on the tag bit's here, since we don't have a lock, and the entry and
  43   // tag is not updated atomicly.
  44   oop entry = *(this_oop->obj_at_addr(which));
  45   if (entry->is_klass()) {
  46     // Already resolved - return entry.
  47     return (klassOop)entry;
  48   }
  49 
  50   // Acquire lock on constant oop while doing update. After we get the lock, we check if another object
  51   // already has updated the object
  52   assert(THREAD->is_Java_thread(), "must be a Java thread");
  53   bool do_resolve = false;
  54   bool in_error = false;
  55 
  56   symbolHandle name;
  57   Handle       loader;
  58   { ObjectLocker ol(this_oop, THREAD);
  59 
  60     if (this_oop->tag_at(which).is_unresolved_klass()) {
  61       if (this_oop->tag_at(which).is_unresolved_klass_in_error()) {
  62         in_error = true;
  63       } else {
  64         do_resolve = true;
  65         name   = symbolHandle(THREAD, this_oop->unresolved_klass_at(which));
  66         loader = Handle(THREAD, instanceKlass::cast(this_oop->pool_holder())->class_loader());
  67       }
  68     }
  69   } // unlocking constantPool
  70 
  71 
  72   // The original attempt to resolve this constant pool entry failed so find the
  73   // original error and throw it again (JVMS 5.4.3).
  74   if (in_error) {
  75     symbolOop error = SystemDictionary::find_resolution_error(this_oop, which);
  76     guarantee(error != (symbolOop)NULL, "tag mismatch with resolution error table");
  77     ResourceMark rm;
  78     // exception text will be the class name
  79     const char* className = this_oop->unresolved_klass_at(which)->as_C_string();
  80     THROW_MSG_0(error, className);
  81   }
  82 
  83   if (do_resolve) {
  84     // this_oop must be unlocked during resolve_or_fail
  85     oop protection_domain = Klass::cast(this_oop->pool_holder())->protection_domain();
  86     Handle h_prot (THREAD, protection_domain);
  87     klassOop k_oop = SystemDictionary::resolve_or_fail(name, loader, h_prot, true, THREAD);
  88     KlassHandle k;
  89     if (!HAS_PENDING_EXCEPTION) {
  90       k = KlassHandle(THREAD, k_oop);
  91       // Do access check for klasses
  92       verify_constant_pool_resolve(this_oop, k, THREAD);
  93     }
  94 
  95     // Failed to resolve class. We must record the errors so that subsequent attempts
  96     // to resolve this constant pool entry fail with the same error (JVMS 5.4.3).
  97     if (HAS_PENDING_EXCEPTION) {
  98       ResourceMark rm;
  99       symbolHandle error(PENDING_EXCEPTION->klass()->klass_part()->name());
 100 
 101       bool throw_orig_error = false;
 102       {
 103         ObjectLocker ol (this_oop, THREAD);
 104 
 105         // some other thread has beaten us and has resolved the class.
 106         if (this_oop->tag_at(which).is_klass()) {
 107           CLEAR_PENDING_EXCEPTION;
 108           entry = this_oop->resolved_klass_at(which);
 109           return (klassOop)entry;
 110         }
 111 
 112         if (!PENDING_EXCEPTION->
 113               is_a(SystemDictionary::LinkageError_klass())) {
 114           // Just throw the exception and don't prevent these classes from
 115           // being loaded due to virtual machine errors like StackOverflow
 116           // and OutOfMemoryError, etc, or if the thread was hit by stop()
 117           // Needs clarification to section 5.4.3 of the VM spec (see 6308271)
 118         }
 119         else if (!this_oop->tag_at(which).is_unresolved_klass_in_error()) {
 120           SystemDictionary::add_resolution_error(this_oop, which, error);
 121           this_oop->tag_at_put(which, JVM_CONSTANT_UnresolvedClassInError);
 122         } else {
 123           // some other thread has put the class in error state.
 124           error = symbolHandle(SystemDictionary::find_resolution_error(this_oop, which));
 125           assert(!error.is_null(), "checking");
 126           throw_orig_error = true;
 127         }
 128       } // unlocked
 129 
 130       if (throw_orig_error) {
 131         CLEAR_PENDING_EXCEPTION;
 132         ResourceMark rm;
 133         const char* className = this_oop->unresolved_klass_at(which)->as_C_string();
 134         THROW_MSG_0(error, className);
 135       }
 136 
 137       return 0;
 138     }
 139 
 140     if (TraceClassResolution && !k()->klass_part()->oop_is_array()) {
 141       // skip resolving the constant pool so that this code get's
 142       // called the next time some bytecodes refer to this class.
 143       ResourceMark rm;
 144       int line_number = -1;
 145       const char * source_file = NULL;
 146       if (JavaThread::current()->has_last_Java_frame()) {
 147         // try to identify the method which called this function.
 148         vframeStream vfst(JavaThread::current());
 149         if (!vfst.at_end()) {
 150           line_number = vfst.method()->line_number_from_bci(vfst.bci());
 151           symbolOop s = instanceKlass::cast(vfst.method()->method_holder())->source_file_name();
 152           if (s != NULL) {
 153             source_file = s->as_C_string();
 154           }
 155         }
 156       }
 157       if (k() != this_oop->pool_holder()) {
 158         // only print something if the classes are different
 159         if (source_file != NULL) {
 160           tty->print("RESOLVE %s %s %s:%d\n",
 161                      instanceKlass::cast(this_oop->pool_holder())->external_name(),
 162                      instanceKlass::cast(k())->external_name(), source_file, line_number);
 163         } else {
 164           tty->print("RESOLVE %s %s\n",
 165                      instanceKlass::cast(this_oop->pool_holder())->external_name(),
 166                      instanceKlass::cast(k())->external_name());
 167         }
 168       }
 169       return k();
 170     } else {
 171       ObjectLocker ol (this_oop, THREAD);
 172       // Only updated constant pool - if it is resolved.
 173       do_resolve = this_oop->tag_at(which).is_unresolved_klass();
 174       if (do_resolve) {
 175         this_oop->klass_at_put(which, k());
 176       }
 177     }
 178   }
 179 
 180   entry = this_oop->resolved_klass_at(which);
 181   assert(entry->is_klass(), "must be resolved at this point");
 182   return (klassOop)entry;
 183 }
 184 
 185 
 186 // Does not update constantPoolOop - to avoid any exception throwing. Used
 187 // by compiler and exception handling.  Also used to avoid classloads for
 188 // instanceof operations. Returns NULL if the class has not been loaded or
 189 // if the verification of constant pool failed
 190 klassOop constantPoolOopDesc::klass_at_if_loaded(constantPoolHandle this_oop, int which) {
 191   oop entry = *this_oop->obj_at_addr(which);
 192   if (entry->is_klass()) {
 193     return (klassOop)entry;
 194   } else {
 195     assert(entry->is_symbol(), "must be either symbol or klass");
 196     Thread *thread = Thread::current();
 197     symbolHandle name (thread, (symbolOop)entry);
 198     oop loader = instanceKlass::cast(this_oop->pool_holder())->class_loader();
 199     oop protection_domain = Klass::cast(this_oop->pool_holder())->protection_domain();
 200     Handle h_prot (thread, protection_domain);
 201     Handle h_loader (thread, loader);
 202     klassOop k = SystemDictionary::find(name, h_loader, h_prot, thread);
 203 
 204     if (k != NULL) {
 205       // Make sure that resolving is legal
 206       EXCEPTION_MARK;
 207       KlassHandle klass(THREAD, k);
 208       // return NULL if verification fails
 209       verify_constant_pool_resolve(this_oop, klass, THREAD);
 210       if (HAS_PENDING_EXCEPTION) {
 211         CLEAR_PENDING_EXCEPTION;
 212         return NULL;
 213       }
 214       return klass();
 215     } else {
 216       return k;
 217     }
 218   }
 219 }
 220 
 221 
 222 klassOop constantPoolOopDesc::klass_ref_at_if_loaded(constantPoolHandle this_oop, int which) {
 223   return klass_at_if_loaded(this_oop, this_oop->klass_ref_index_at(which));
 224 }
 225 
 226 
 227 // This is an interface for the compiler that allows accessing non-resolved entries
 228 // in the constant pool - but still performs the validations tests. Must be used
 229 // in a pre-parse of the compiler - to determine what it can do and not do.
 230 // Note: We cannot update the ConstantPool from the vm_thread.
 231 klassOop constantPoolOopDesc::klass_ref_at_if_loaded_check(constantPoolHandle this_oop, int index, TRAPS) {
 232   int which = this_oop->klass_ref_index_at(index);
 233   oop entry = *this_oop->obj_at_addr(which);
 234   if (entry->is_klass()) {
 235     return (klassOop)entry;
 236   } else {
 237     assert(entry->is_symbol(), "must be either symbol or klass");
 238     symbolHandle name (THREAD, (symbolOop)entry);
 239     oop loader = instanceKlass::cast(this_oop->pool_holder())->class_loader();
 240     oop protection_domain = Klass::cast(this_oop->pool_holder())->protection_domain();
 241     Handle h_loader(THREAD, loader);
 242     Handle h_prot  (THREAD, protection_domain);
 243     KlassHandle k(THREAD, SystemDictionary::find(name, h_loader, h_prot, THREAD));
 244 
 245     // Do access check for klasses
 246     if( k.not_null() ) verify_constant_pool_resolve(this_oop, k, CHECK_NULL);
 247     return k();
 248   }
 249 }
 250 
 251 
 252 symbolOop constantPoolOopDesc::impl_name_ref_at(int which, bool uncached) {
 253   int name_index = name_ref_index_at(impl_name_and_type_ref_index_at(which, uncached));
 254   return symbol_at(name_index);
 255 }
 256 
 257 
 258 symbolOop constantPoolOopDesc::impl_signature_ref_at(int which, bool uncached) {
 259   int signature_index = signature_ref_index_at(impl_name_and_type_ref_index_at(which, uncached));
 260   return symbol_at(signature_index);
 261 }
 262 
 263 
 264 int constantPoolOopDesc::impl_name_and_type_ref_index_at(int which, bool uncached) {
 265   int i = which;
 266   if (!uncached && cache() != NULL) {
 267     if (constantPoolCacheOopDesc::is_secondary_index(which)) {
 268       // Invokedynamic indexes are always processed in native order
 269       // so there is no question of reading a native u2 in Java order here.
 270       int pool_index = cache()->main_entry_at(which)->constant_pool_index();
 271       if (tag_at(pool_index).is_invoke_dynamic())
 272         pool_index = invoke_dynamic_name_and_type_ref_index_at(pool_index);
 273       assert(tag_at(pool_index).is_name_and_type(), "");
 274       return pool_index;
 275     }
 276     // change byte-ordering and go via cache
 277     i = remap_instruction_operand_from_cache(which);
 278   } else {
 279     if (tag_at(which).is_name_and_type())
 280       // invokedynamic index is a simple name-and-type
 281       return which;
 282   }
 283   assert(tag_at(i).is_field_or_method(), "Corrupted constant pool");
 284   jint ref_index = *int_at_addr(i);
 285   return extract_high_short_from_int(ref_index);
 286 }
 287 
 288 
 289 int constantPoolOopDesc::impl_klass_ref_index_at(int which, bool uncached) {
 290   guarantee(!constantPoolCacheOopDesc::is_secondary_index(which),
 291             "an invokedynamic instruction does not have a klass");
 292   int i = which;
 293   if (!uncached && cache() != NULL) {
 294     // change byte-ordering and go via cache
 295     i = remap_instruction_operand_from_cache(which);
 296   }
 297   assert(tag_at(i).is_field_or_method(), "Corrupted constant pool");
 298   jint ref_index = *int_at_addr(i);
 299   return extract_low_short_from_int(ref_index);
 300 }
 301 
 302 
 303 
 304 int constantPoolOopDesc::remap_instruction_operand_from_cache(int operand) {
 305   int cpc_index = operand;
 306   DEBUG_ONLY(cpc_index -= CPCACHE_INDEX_TAG);
 307   assert((int)(u2)cpc_index == cpc_index, "clean u2");
 308   int member_index = cache()->entry_at(cpc_index)->constant_pool_index();
 309   return member_index;
 310 }
 311 
 312 
 313 void constantPoolOopDesc::verify_constant_pool_resolve(constantPoolHandle this_oop, KlassHandle k, TRAPS) {
 314  if (k->oop_is_instance() || k->oop_is_objArray()) {
 315     instanceKlassHandle holder (THREAD, this_oop->pool_holder());
 316     klassOop elem_oop = k->oop_is_instance() ? k() : objArrayKlass::cast(k())->bottom_klass();
 317     KlassHandle element (THREAD, elem_oop);
 318 
 319     // The element type could be a typeArray - we only need the access check if it is
 320     // an reference to another class
 321     if (element->oop_is_instance()) {
 322       LinkResolver::check_klass_accessability(holder, element, CHECK);
 323     }
 324   }
 325 }
 326 
 327 
 328 int constantPoolOopDesc::name_ref_index_at(int which_nt) {
 329   jint ref_index = name_and_type_at(which_nt);
 330   return extract_low_short_from_int(ref_index);
 331 }
 332 
 333 
 334 int constantPoolOopDesc::signature_ref_index_at(int which_nt) {
 335   jint ref_index = name_and_type_at(which_nt);
 336   return extract_high_short_from_int(ref_index);
 337 }
 338 
 339 
 340 klassOop constantPoolOopDesc::klass_ref_at(int which, TRAPS) {
 341   return klass_at(klass_ref_index_at(which), CHECK_NULL);
 342 }
 343 
 344 
 345 symbolOop constantPoolOopDesc::klass_name_at(int which) {
 346   assert(tag_at(which).is_unresolved_klass() || tag_at(which).is_klass(),
 347          "Corrupted constant pool");
 348   // A resolved constantPool entry will contain a klassOop, otherwise a symbolOop.
 349   // It is not safe to rely on the tag bit's here, since we don't have a lock, and the entry and
 350   // tag is not updated atomicly.
 351   oop entry = *(obj_at_addr(which));
 352   if (entry->is_klass()) {
 353     // Already resolved - return entry's name.
 354     return klassOop(entry)->klass_part()->name();
 355   } else {
 356     assert(entry->is_symbol(), "must be either symbol or klass");
 357     return (symbolOop)entry;
 358   }
 359 }
 360 
 361 symbolOop constantPoolOopDesc::klass_ref_at_noresolve(int which) {
 362   jint ref_index = klass_ref_index_at(which);
 363   return klass_at_noresolve(ref_index);
 364 }
 365 
 366 symbolOop constantPoolOopDesc::uncached_klass_ref_at_noresolve(int which) {
 367   jint ref_index = uncached_klass_ref_index_at(which);
 368   return klass_at_noresolve(ref_index);
 369 }
 370 
 371 char* constantPoolOopDesc::string_at_noresolve(int which) {
 372   // Test entry type in case string is resolved while in here.
 373   oop entry = *(obj_at_addr(which));
 374   if (entry->is_symbol()) {
 375     return ((symbolOop)entry)->as_C_string();
 376   } else if (java_lang_String::is_instance(entry)) {
 377     return java_lang_String::as_utf8_string(entry);
 378   } else {
 379     return (char*)"<pseudo-string>";
 380   }
 381 }
 382 
 383 
 384 BasicType constantPoolOopDesc::basic_type_for_signature_at(int which) {
 385   return FieldType::basic_type(symbol_at(which));
 386 }
 387 
 388 
 389 void constantPoolOopDesc::resolve_string_constants_impl(constantPoolHandle this_oop, TRAPS) {
 390   for (int index = 1; index < this_oop->length(); index++) { // Index 0 is unused
 391     if (this_oop->tag_at(index).is_unresolved_string()) {
 392       this_oop->string_at(index, CHECK);
 393     }
 394   }
 395 }
 396 
 397 oop constantPoolOopDesc::resolve_constant_at_impl(constantPoolHandle this_oop, int index, int cache_index, TRAPS) {
 398   oop result_oop = NULL;
 399   if (cache_index >= 0) {
 400     assert(index < 0, "only one kind of index at a time");
 401     ConstantPoolCacheEntry* cpc_entry = this_oop->cache()->entry_at(cache_index);
 402     result_oop = cpc_entry->f1();
 403     if (result_oop != NULL) {
 404       return result_oop;  // that was easy...
 405     }
 406     index = cpc_entry->constant_pool_index();
 407   }
 408 
 409   int tag_value = this_oop->tag_at(index).value();
 410   switch (tag_value) {
 411 
 412   case JVM_CONSTANT_UnresolvedClass:
 413   case JVM_CONSTANT_UnresolvedClassInError:
 414   case JVM_CONSTANT_Class:
 415     {
 416       klassOop resolved = klass_at_impl(this_oop, index, CHECK_NULL);
 417       // ldc wants the java mirror.
 418       result_oop = resolved->klass_part()->java_mirror();
 419       break;
 420     }
 421 
 422   case JVM_CONSTANT_String:
 423   case JVM_CONSTANT_UnresolvedString:
 424     if (this_oop->is_pseudo_string_at(index)) {
 425       result_oop = this_oop->pseudo_string_at(index);
 426       break;
 427     }
 428     result_oop = string_at_impl(this_oop, index, CHECK_NULL);
 429     break;
 430 
 431   case JVM_CONSTANT_Object:
 432     result_oop = this_oop->object_at(index);
 433     break;
 434 
 435   case JVM_CONSTANT_MethodHandle:
 436     {
 437       int ref_kind                 = this_oop->method_handle_ref_kind_at(index);
 438       int callee_index             = this_oop->method_handle_klass_index_at(index);
 439       symbolHandle name(THREAD,      this_oop->method_handle_name_ref_at(index));
 440       symbolHandle signature(THREAD, this_oop->method_handle_signature_ref_at(index));
 441       if (PrintMiscellaneous)
 442         tty->print_cr("resolve JVM_CONSTANT_MethodHandle:%d [%d/%d/%d] %s.%s",
 443                       ref_kind, index, this_oop->method_handle_index_at(index),
 444                       callee_index, name->as_C_string(), signature->as_C_string());
 445       KlassHandle callee;
 446       { klassOop k = klass_at_impl(this_oop, callee_index, CHECK_NULL);
 447         callee = KlassHandle(THREAD, k);
 448       }
 449       KlassHandle klass(THREAD, this_oop->pool_holder());
 450       Handle value = SystemDictionary::link_method_handle_constant(klass, ref_kind,
 451                                                                    callee, name, signature,
 452                                                                    CHECK_NULL);
 453       result_oop = value();
 454       // FIXME: Uniquify errors, using SystemDictionary::find_resolution_error.
 455       break;
 456     }
 457 
 458   case JVM_CONSTANT_MethodType:
 459     {
 460       symbolHandle signature(THREAD, this_oop->method_type_signature_at(index));
 461       if (PrintMiscellaneous)
 462         tty->print_cr("resolve JVM_CONSTANT_MethodType [%d/%d] %s",
 463                       index, this_oop->method_type_index_at(index),
 464                       signature->as_C_string());
 465       KlassHandle klass(THREAD, this_oop->pool_holder());
 466       bool ignore_is_on_bcp = false;
 467       Handle value = SystemDictionary::find_method_handle_type(signature,
 468                                                                klass,
 469                                                                false,
 470                                                                ignore_is_on_bcp,
 471                                                                CHECK_NULL);
 472       result_oop = value();
 473       // FIXME: Uniquify errors, using SystemDictionary::find_resolution_error.
 474       break;
 475     }
 476 
 477     /* maybe some day
 478   case JVM_CONSTANT_Integer:
 479   case JVM_CONSTANT_Float:
 480   case JVM_CONSTANT_Long:
 481   case JVM_CONSTANT_Double:
 482     result_oop = java_lang_boxing_object::create(...);
 483     break;
 484     */
 485 
 486   default:
 487     DEBUG_ONLY( tty->print_cr("*** %p: tag at CP[%d/%d] = %d",
 488                               this_oop(), index, cache_index, tag_value) );
 489     assert(false, "unexpected constant tag");
 490     break;
 491   }
 492 
 493   if (cache_index >= 0) {
 494     // Cache the oop here also.
 495     Handle result(THREAD, result_oop);
 496     result_oop = NULL;  // safety
 497     ObjectLocker ol(this_oop, THREAD);
 498     ConstantPoolCacheEntry* cpc_entry = this_oop->cache()->entry_at(cache_index);
 499     oop result_oop2 = cpc_entry->f1();
 500     if (result_oop2 != NULL) {
 501       // Race condition:  May already be filled in while we were trying to lock.
 502       return result_oop2;
 503     }
 504     cpc_entry->set_f1(result());
 505     return result();
 506   } else {
 507     return result_oop;
 508   }
 509 }
 510 
 511 oop constantPoolOopDesc::string_at_impl(constantPoolHandle this_oop, int which, TRAPS) {
 512   oop entry = *(this_oop->obj_at_addr(which));
 513   if (entry->is_symbol()) {
 514     ObjectLocker ol(this_oop, THREAD);
 515     if (this_oop->tag_at(which).is_unresolved_string()) {
 516       // Intern string
 517       symbolOop sym = this_oop->unresolved_string_at(which);
 518       entry = StringTable::intern(sym, CHECK_(constantPoolOop(NULL)));
 519       this_oop->string_at_put(which, entry);
 520     } else {
 521       // Another thread beat us and interned string, read string from constant pool
 522       entry = this_oop->resolved_string_at(which);
 523     }
 524   }
 525   assert(java_lang_String::is_instance(entry), "must be string");
 526   return entry;
 527 }
 528 
 529 
 530 bool constantPoolOopDesc::is_pseudo_string_at(int which) {
 531   oop entry = *(obj_at_addr(which));
 532   if (entry->is_symbol())
 533     // Not yet resolved, but it will resolve to a string.
 534     return false;
 535   else if (java_lang_String::is_instance(entry))
 536     return false; // actually, it might be a non-interned or non-perm string
 537   else
 538     // truly pseudo
 539     return true;
 540 }
 541 
 542 
 543 bool constantPoolOopDesc::klass_name_at_matches(instanceKlassHandle k,
 544                                                 int which) {
 545   // Names are interned, so we can compare symbolOops directly
 546   symbolOop cp_name = klass_name_at(which);
 547   return (cp_name == k->name());
 548 }
 549 
 550 
 551 int constantPoolOopDesc::pre_resolve_shared_klasses(TRAPS) {
 552   ResourceMark rm;
 553   int count = 0;
 554   for (int index = 1; index < tags()->length(); index++) { // Index 0 is unused
 555     if (tag_at(index).is_unresolved_string()) {
 556       // Intern string
 557       symbolOop sym = unresolved_string_at(index);
 558       oop entry = StringTable::intern(sym, CHECK_(-1));
 559       string_at_put(index, entry);
 560     }
 561   }
 562   return count;
 563 }
 564 
 565 
 566 // Iterate over symbols which are used as class, field, method names and
 567 // signatures (in preparation for writing to the shared archive).
 568 
 569 void constantPoolOopDesc::shared_symbols_iterate(OopClosure* closure) {
 570   for (int index = 1; index < length(); index++) { // Index 0 is unused
 571     switch (tag_at(index).value()) {
 572 
 573     case JVM_CONSTANT_UnresolvedClass:
 574       closure->do_oop(obj_at_addr(index));
 575       break;
 576 
 577     case JVM_CONSTANT_NameAndType:
 578       {
 579         int i = *int_at_addr(index);
 580         closure->do_oop(obj_at_addr((unsigned)i >> 16));
 581         closure->do_oop(obj_at_addr((unsigned)i & 0xffff));
 582       }
 583       break;
 584 
 585     case JVM_CONSTANT_Class:
 586     case JVM_CONSTANT_InterfaceMethodref:
 587     case JVM_CONSTANT_Fieldref:
 588     case JVM_CONSTANT_Methodref:
 589     case JVM_CONSTANT_Integer:
 590     case JVM_CONSTANT_Float:
 591       // Do nothing!  Not an oop.
 592       // These constant types do not reference symbols at this point.
 593       break;
 594 
 595     case JVM_CONSTANT_String:
 596       // Do nothing!  Not a symbol.
 597       break;
 598 
 599     case JVM_CONSTANT_UnresolvedString:
 600     case JVM_CONSTANT_Utf8:
 601       // These constants are symbols, but unless these symbols are
 602       // actually to be used for something, we don't want to mark them.
 603       break;
 604 
 605     case JVM_CONSTANT_Long:
 606     case JVM_CONSTANT_Double:
 607       // Do nothing!  Not an oop. (But takes two pool entries.)
 608       ++index;
 609       break;
 610 
 611     default:
 612       ShouldNotReachHere();
 613       break;
 614     }
 615   }
 616 }
 617 
 618 
 619 // Iterate over the [one] tags array (in preparation for writing to the
 620 // shared archive).
 621 
 622 void constantPoolOopDesc::shared_tags_iterate(OopClosure* closure) {
 623   closure->do_oop(tags_addr());
 624 }
 625 
 626 
 627 // Iterate over String objects (in preparation for writing to the shared
 628 // archive).
 629 
 630 void constantPoolOopDesc::shared_strings_iterate(OopClosure* closure) {
 631   for (int index = 1; index < length(); index++) { // Index 0 is unused
 632     switch (tag_at(index).value()) {
 633 
 634     case JVM_CONSTANT_UnresolvedClass:
 635     case JVM_CONSTANT_NameAndType:
 636       // Do nothing!  Not a String.
 637       break;
 638 
 639     case JVM_CONSTANT_Class:
 640     case JVM_CONSTANT_InterfaceMethodref:
 641     case JVM_CONSTANT_Fieldref:
 642     case JVM_CONSTANT_Methodref:
 643     case JVM_CONSTANT_Integer:
 644     case JVM_CONSTANT_Float:
 645       // Do nothing!  Not an oop.
 646       // These constant types do not reference symbols at this point.
 647       break;
 648 
 649     case JVM_CONSTANT_String:
 650       closure->do_oop(obj_at_addr(index));
 651       break;
 652 
 653     case JVM_CONSTANT_UnresolvedString:
 654     case JVM_CONSTANT_Utf8:
 655       // These constants are symbols, but unless these symbols are
 656       // actually to be used for something, we don't want to mark them.
 657       break;
 658 
 659     case JVM_CONSTANT_Long:
 660     case JVM_CONSTANT_Double:
 661       // Do nothing!  Not an oop. (But takes two pool entries.)
 662       ++index;
 663       break;
 664 
 665     default:
 666       ShouldNotReachHere();
 667       break;
 668     }
 669   }
 670 }
 671 
 672 
 673 // Compare this constant pool's entry at index1 to the constant pool
 674 // cp2's entry at index2.
 675 bool constantPoolOopDesc::compare_entry_to(int index1, constantPoolHandle cp2,
 676        int index2, TRAPS) {
 677 
 678   jbyte t1 = tag_at(index1).value();
 679   jbyte t2 = cp2->tag_at(index2).value();
 680 
 681 
 682   // JVM_CONSTANT_UnresolvedClassInError is equal to JVM_CONSTANT_UnresolvedClass
 683   // when comparing
 684   if (t1 == JVM_CONSTANT_UnresolvedClassInError) {
 685     t1 = JVM_CONSTANT_UnresolvedClass;
 686   }
 687   if (t2 == JVM_CONSTANT_UnresolvedClassInError) {
 688     t2 = JVM_CONSTANT_UnresolvedClass;
 689   }
 690 
 691   if (t1 != t2) {
 692     // Not the same entry type so there is nothing else to check. Note
 693     // that this style of checking will consider resolved/unresolved
 694     // class pairs and resolved/unresolved string pairs as different.
 695     // From the constantPoolOop API point of view, this is correct
 696     // behavior. See constantPoolKlass::merge() to see how this plays
 697     // out in the context of constantPoolOop merging.
 698     return false;
 699   }
 700 
 701   switch (t1) {
 702   case JVM_CONSTANT_Class:
 703   {
 704     klassOop k1 = klass_at(index1, CHECK_false);
 705     klassOop k2 = cp2->klass_at(index2, CHECK_false);
 706     if (k1 == k2) {
 707       return true;
 708     }
 709   } break;
 710 
 711   case JVM_CONSTANT_ClassIndex:
 712   {
 713     int recur1 = klass_index_at(index1);
 714     int recur2 = cp2->klass_index_at(index2);
 715     bool match = compare_entry_to(recur1, cp2, recur2, CHECK_false);
 716     if (match) {
 717       return true;
 718     }
 719   } break;
 720 
 721   case JVM_CONSTANT_Double:
 722   {
 723     jdouble d1 = double_at(index1);
 724     jdouble d2 = cp2->double_at(index2);
 725     if (d1 == d2) {
 726       return true;
 727     }
 728   } break;
 729 
 730   case JVM_CONSTANT_Fieldref:
 731   case JVM_CONSTANT_InterfaceMethodref:
 732   case JVM_CONSTANT_Methodref:
 733   {
 734     int recur1 = uncached_klass_ref_index_at(index1);
 735     int recur2 = cp2->uncached_klass_ref_index_at(index2);
 736     bool match = compare_entry_to(recur1, cp2, recur2, CHECK_false);
 737     if (match) {
 738       recur1 = uncached_name_and_type_ref_index_at(index1);
 739       recur2 = cp2->uncached_name_and_type_ref_index_at(index2);
 740       match = compare_entry_to(recur1, cp2, recur2, CHECK_false);
 741       if (match) {
 742         return true;
 743       }
 744     }
 745   } break;
 746 
 747   case JVM_CONSTANT_Float:
 748   {
 749     jfloat f1 = float_at(index1);
 750     jfloat f2 = cp2->float_at(index2);
 751     if (f1 == f2) {
 752       return true;
 753     }
 754   } break;
 755 
 756   case JVM_CONSTANT_Integer:
 757   {
 758     jint i1 = int_at(index1);
 759     jint i2 = cp2->int_at(index2);
 760     if (i1 == i2) {
 761       return true;
 762     }
 763   } break;
 764 
 765   case JVM_CONSTANT_Long:
 766   {
 767     jlong l1 = long_at(index1);
 768     jlong l2 = cp2->long_at(index2);
 769     if (l1 == l2) {
 770       return true;
 771     }
 772   } break;
 773 
 774   case JVM_CONSTANT_NameAndType:
 775   {
 776     int recur1 = name_ref_index_at(index1);
 777     int recur2 = cp2->name_ref_index_at(index2);
 778     bool match = compare_entry_to(recur1, cp2, recur2, CHECK_false);
 779     if (match) {
 780       recur1 = signature_ref_index_at(index1);
 781       recur2 = cp2->signature_ref_index_at(index2);
 782       match = compare_entry_to(recur1, cp2, recur2, CHECK_false);
 783       if (match) {
 784         return true;
 785       }
 786     }
 787   } break;
 788 
 789   case JVM_CONSTANT_String:
 790   {
 791     oop s1 = string_at(index1, CHECK_false);
 792     oop s2 = cp2->string_at(index2, CHECK_false);
 793     if (s1 == s2) {
 794       return true;
 795     }
 796   } break;
 797 
 798   case JVM_CONSTANT_StringIndex:
 799   {
 800     int recur1 = string_index_at(index1);
 801     int recur2 = cp2->string_index_at(index2);
 802     bool match = compare_entry_to(recur1, cp2, recur2, CHECK_false);
 803     if (match) {
 804       return true;
 805     }
 806   } break;
 807 
 808   case JVM_CONSTANT_UnresolvedClass:
 809   {
 810     symbolOop k1 = unresolved_klass_at(index1);
 811     symbolOop k2 = cp2->unresolved_klass_at(index2);
 812     if (k1 == k2) {
 813       return true;
 814     }
 815   } break;
 816 
 817   case JVM_CONSTANT_MethodType:
 818   {
 819     int k1 = method_type_index_at(index1);
 820     int k2 = cp2->method_type_index_at(index2);
 821     if (k1 == k2) {
 822       return true;
 823     }
 824   } break;
 825 
 826   case JVM_CONSTANT_MethodHandle:
 827   {
 828     int k1 = method_handle_ref_kind_at(index1);
 829     int k2 = cp2->method_handle_ref_kind_at(index2);
 830     if (k1 == k2) {
 831       int i1 = method_handle_index_at(index1);
 832       int i2 = cp2->method_handle_index_at(index2);
 833       if (i1 == i2) {
 834         return true;
 835       }
 836     }
 837   } break;
 838 
 839   case JVM_CONSTANT_InvokeDynamic:
 840   {
 841     int k1 = invoke_dynamic_bootstrap_method_ref_index_at(index1);
 842     int k2 = cp2->invoke_dynamic_bootstrap_method_ref_index_at(index2);
 843     if (k1 == k2) {
 844       int i1 = invoke_dynamic_name_and_type_ref_index_at(index1);
 845       int i2 = cp2->invoke_dynamic_name_and_type_ref_index_at(index2);
 846       if (i1 == i2) {
 847         return true;
 848       }
 849     }
 850   } break;
 851 
 852   case JVM_CONSTANT_UnresolvedString:
 853   {
 854     symbolOop s1 = unresolved_string_at(index1);
 855     symbolOop s2 = cp2->unresolved_string_at(index2);
 856     if (s1 == s2) {
 857       return true;
 858     }
 859   } break;
 860 
 861   case JVM_CONSTANT_Utf8:
 862   {
 863     symbolOop s1 = symbol_at(index1);
 864     symbolOop s2 = cp2->symbol_at(index2);
 865     if (s1 == s2) {
 866       return true;
 867     }
 868   } break;
 869 
 870   // Invalid is used as the tag for the second constant pool entry
 871   // occupied by JVM_CONSTANT_Double or JVM_CONSTANT_Long. It should
 872   // not be seen by itself.
 873   case JVM_CONSTANT_Invalid: // fall through
 874 
 875   default:
 876     ShouldNotReachHere();
 877     break;
 878   }
 879 
 880   return false;
 881 } // end compare_entry_to()
 882 
 883 
 884 // Copy this constant pool's entries at start_i to end_i (inclusive)
 885 // to the constant pool to_cp's entries starting at to_i. A total of
 886 // (end_i - start_i) + 1 entries are copied.
 887 void constantPoolOopDesc::copy_cp_to(int start_i, int end_i,
 888        constantPoolHandle to_cp, int to_i, TRAPS) {
 889 
 890   int dest_i = to_i;  // leave original alone for debug purposes
 891 
 892   for (int src_i = start_i; src_i <= end_i; /* see loop bottom */ ) {
 893     copy_entry_to(src_i, to_cp, dest_i, CHECK);
 894 
 895     switch (tag_at(src_i).value()) {
 896     case JVM_CONSTANT_Double:
 897     case JVM_CONSTANT_Long:
 898       // double and long take two constant pool entries
 899       src_i += 2;
 900       dest_i += 2;
 901       break;
 902 
 903     default:
 904       // all others take one constant pool entry
 905       src_i++;
 906       dest_i++;
 907       break;
 908     }
 909   }
 910 } // end copy_cp_to()
 911 
 912 
 913 // Copy this constant pool's entry at from_i to the constant pool
 914 // to_cp's entry at to_i.
 915 void constantPoolOopDesc::copy_entry_to(int from_i, constantPoolHandle to_cp,
 916        int to_i, TRAPS) {
 917 
 918   switch (tag_at(from_i).value()) {
 919   case JVM_CONSTANT_Class:
 920   {
 921     klassOop k = klass_at(from_i, CHECK);
 922     to_cp->klass_at_put(to_i, k);
 923   } break;
 924 
 925   case JVM_CONSTANT_ClassIndex:
 926   {
 927     jint ki = klass_index_at(from_i);
 928     to_cp->klass_index_at_put(to_i, ki);
 929   } break;
 930 
 931   case JVM_CONSTANT_Double:
 932   {
 933     jdouble d = double_at(from_i);
 934     to_cp->double_at_put(to_i, d);
 935     // double takes two constant pool entries so init second entry's tag
 936     to_cp->tag_at_put(to_i + 1, JVM_CONSTANT_Invalid);
 937   } break;
 938 
 939   case JVM_CONSTANT_Fieldref:
 940   {
 941     int class_index = uncached_klass_ref_index_at(from_i);
 942     int name_and_type_index = uncached_name_and_type_ref_index_at(from_i);
 943     to_cp->field_at_put(to_i, class_index, name_and_type_index);
 944   } break;
 945 
 946   case JVM_CONSTANT_Float:
 947   {
 948     jfloat f = float_at(from_i);
 949     to_cp->float_at_put(to_i, f);
 950   } break;
 951 
 952   case JVM_CONSTANT_Integer:
 953   {
 954     jint i = int_at(from_i);
 955     to_cp->int_at_put(to_i, i);
 956   } break;
 957 
 958   case JVM_CONSTANT_InterfaceMethodref:
 959   {
 960     int class_index = uncached_klass_ref_index_at(from_i);
 961     int name_and_type_index = uncached_name_and_type_ref_index_at(from_i);
 962     to_cp->interface_method_at_put(to_i, class_index, name_and_type_index);
 963   } break;
 964 
 965   case JVM_CONSTANT_Long:
 966   {
 967     jlong l = long_at(from_i);
 968     to_cp->long_at_put(to_i, l);
 969     // long takes two constant pool entries so init second entry's tag
 970     to_cp->tag_at_put(to_i + 1, JVM_CONSTANT_Invalid);
 971   } break;
 972 
 973   case JVM_CONSTANT_Methodref:
 974   {
 975     int class_index = uncached_klass_ref_index_at(from_i);
 976     int name_and_type_index = uncached_name_and_type_ref_index_at(from_i);
 977     to_cp->method_at_put(to_i, class_index, name_and_type_index);
 978   } break;
 979 
 980   case JVM_CONSTANT_NameAndType:
 981   {
 982     int name_ref_index = name_ref_index_at(from_i);
 983     int signature_ref_index = signature_ref_index_at(from_i);
 984     to_cp->name_and_type_at_put(to_i, name_ref_index, signature_ref_index);
 985   } break;
 986 
 987   case JVM_CONSTANT_String:
 988   {
 989     oop s = string_at(from_i, CHECK);
 990     to_cp->string_at_put(to_i, s);
 991   } break;
 992 
 993   case JVM_CONSTANT_StringIndex:
 994   {
 995     jint si = string_index_at(from_i);
 996     to_cp->string_index_at_put(to_i, si);
 997   } break;
 998 
 999   case JVM_CONSTANT_UnresolvedClass:
1000   {
1001     symbolOop k = unresolved_klass_at(from_i);
1002     to_cp->unresolved_klass_at_put(to_i, k);
1003   } break;
1004 
1005   case JVM_CONSTANT_UnresolvedClassInError:
1006   {
1007     symbolOop k = unresolved_klass_at(from_i);
1008     to_cp->unresolved_klass_at_put(to_i, k);
1009     to_cp->tag_at_put(to_i, JVM_CONSTANT_UnresolvedClassInError);
1010   } break;
1011 
1012 
1013   case JVM_CONSTANT_UnresolvedString:
1014   {
1015     symbolOop s = unresolved_string_at(from_i);
1016     to_cp->unresolved_string_at_put(to_i, s);
1017   } break;
1018 
1019   case JVM_CONSTANT_Utf8:
1020   {
1021     symbolOop s = symbol_at(from_i);
1022     to_cp->symbol_at_put(to_i, s);
1023   } break;
1024 
1025   case JVM_CONSTANT_MethodType:
1026   {
1027     jint k = method_type_index_at(from_i);
1028     to_cp->method_type_index_at_put(to_i, k);
1029   } break;
1030 
1031   case JVM_CONSTANT_MethodHandle:
1032   {
1033     int k1 = method_handle_ref_kind_at(from_i);
1034     int k2 = method_handle_index_at(from_i);
1035     to_cp->method_handle_index_at_put(to_i, k1, k2);
1036   } break;
1037 
1038   case JVM_CONSTANT_InvokeDynamic:
1039   {
1040     int k1 = invoke_dynamic_bootstrap_method_ref_index_at(from_i);
1041     int k2 = invoke_dynamic_name_and_type_ref_index_at(from_i);
1042     to_cp->invoke_dynamic_at_put(to_i, k1, k2);
1043   } break;
1044 
1045   // Invalid is used as the tag for the second constant pool entry
1046   // occupied by JVM_CONSTANT_Double or JVM_CONSTANT_Long. It should
1047   // not be seen by itself.
1048   case JVM_CONSTANT_Invalid: // fall through
1049 
1050   default:
1051   {
1052     jbyte bad_value = tag_at(from_i).value(); // leave a breadcrumb
1053     ShouldNotReachHere();
1054   } break;
1055   }
1056 } // end copy_entry_to()
1057 
1058 
1059 // Search constant pool search_cp for an entry that matches this
1060 // constant pool's entry at pattern_i. Returns the index of a
1061 // matching entry or zero (0) if there is no matching entry.
1062 int constantPoolOopDesc::find_matching_entry(int pattern_i,
1063       constantPoolHandle search_cp, TRAPS) {
1064 
1065   // index zero (0) is not used
1066   for (int i = 1; i < search_cp->length(); i++) {
1067     bool found = compare_entry_to(pattern_i, search_cp, i, CHECK_0);
1068     if (found) {
1069       return i;
1070     }
1071   }
1072 
1073   return 0;  // entry not found; return unused index zero (0)
1074 } // end find_matching_entry()
1075 
1076 
1077 #ifndef PRODUCT
1078 
1079 const char* constantPoolOopDesc::printable_name_at(int which) {
1080 
1081   constantTag tag = tag_at(which);
1082 
1083   if (tag.is_unresolved_string() || tag.is_string()) {
1084     return string_at_noresolve(which);
1085   } else if (tag.is_klass() || tag.is_unresolved_klass()) {
1086     return klass_name_at(which)->as_C_string();
1087   } else if (tag.is_symbol()) {
1088     return symbol_at(which)->as_C_string();
1089   }
1090   return "";
1091 }
1092 
1093 #endif // PRODUCT
1094 
1095 
1096 // JVMTI GetConstantPool support
1097 
1098 // For temporary use until code is stable.
1099 #define DBG(code)
1100 
1101 static const char* WARN_MSG = "Must not be such entry!";
1102 
1103 static void print_cpool_bytes(jint cnt, u1 *bytes) {
1104   jint size = 0;
1105   u2   idx1, idx2;
1106 
1107   for (jint idx = 1; idx < cnt; idx++) {
1108     jint ent_size = 0;
1109     u1   tag  = *bytes++;
1110     size++;                       // count tag
1111 
1112     printf("const #%03d, tag: %02d ", idx, tag);
1113     switch(tag) {
1114       case JVM_CONSTANT_Invalid: {
1115         printf("Invalid");
1116         break;
1117       }
1118       case JVM_CONSTANT_Unicode: {
1119         printf("Unicode      %s", WARN_MSG);
1120         break;
1121       }
1122       case JVM_CONSTANT_Utf8: {
1123         u2 len = Bytes::get_Java_u2(bytes);
1124         char str[128];
1125         if (len > 127) {
1126            len = 127;
1127         }
1128         strncpy(str, (char *) (bytes+2), len);
1129         str[len] = '\0';
1130         printf("Utf8          \"%s\"", str);
1131         ent_size = 2 + len;
1132         break;
1133       }
1134       case JVM_CONSTANT_Integer: {
1135         u4 val = Bytes::get_Java_u4(bytes);
1136         printf("int          %d", *(int *) &val);
1137         ent_size = 4;
1138         break;
1139       }
1140       case JVM_CONSTANT_Float: {
1141         u4 val = Bytes::get_Java_u4(bytes);
1142         printf("float        %5.3ff", *(float *) &val);
1143         ent_size = 4;
1144         break;
1145       }
1146       case JVM_CONSTANT_Long: {
1147         u8 val = Bytes::get_Java_u8(bytes);
1148         printf("long         "INT64_FORMAT, *(jlong *) &val);
1149         ent_size = 8;
1150         idx++; // Long takes two cpool slots
1151         break;
1152       }
1153       case JVM_CONSTANT_Double: {
1154         u8 val = Bytes::get_Java_u8(bytes);
1155         printf("double       %5.3fd", *(jdouble *)&val);
1156         ent_size = 8;
1157         idx++; // Double takes two cpool slots
1158         break;
1159       }
1160       case JVM_CONSTANT_Class: {
1161         idx1 = Bytes::get_Java_u2(bytes);
1162         printf("class        #%03d", idx1);
1163         ent_size = 2;
1164         break;
1165       }
1166       case JVM_CONSTANT_String: {
1167         idx1 = Bytes::get_Java_u2(bytes);
1168         printf("String       #%03d", idx1);
1169         ent_size = 2;
1170         break;
1171       }
1172       case JVM_CONSTANT_Fieldref: {
1173         idx1 = Bytes::get_Java_u2(bytes);
1174         idx2 = Bytes::get_Java_u2(bytes+2);
1175         printf("Field        #%03d, #%03d", (int) idx1, (int) idx2);
1176         ent_size = 4;
1177         break;
1178       }
1179       case JVM_CONSTANT_Methodref: {
1180         idx1 = Bytes::get_Java_u2(bytes);
1181         idx2 = Bytes::get_Java_u2(bytes+2);
1182         printf("Method       #%03d, #%03d", idx1, idx2);
1183         ent_size = 4;
1184         break;
1185       }
1186       case JVM_CONSTANT_InterfaceMethodref: {
1187         idx1 = Bytes::get_Java_u2(bytes);
1188         idx2 = Bytes::get_Java_u2(bytes+2);
1189         printf("InterfMethod #%03d, #%03d", idx1, idx2);
1190         ent_size = 4;
1191         break;
1192       }
1193       case JVM_CONSTANT_NameAndType: {
1194         idx1 = Bytes::get_Java_u2(bytes);
1195         idx2 = Bytes::get_Java_u2(bytes+2);
1196         printf("NameAndType  #%03d, #%03d", idx1, idx2);
1197         ent_size = 4;
1198         break;
1199       }
1200       case JVM_CONSTANT_ClassIndex: {
1201         printf("ClassIndex  %s", WARN_MSG);
1202         break;
1203       }
1204       case JVM_CONSTANT_UnresolvedClass: {
1205         printf("UnresolvedClass: %s", WARN_MSG);
1206         break;
1207       }
1208       case JVM_CONSTANT_UnresolvedClassInError: {
1209         printf("UnresolvedClassInErr: %s", WARN_MSG);
1210         break;
1211       }
1212       case JVM_CONSTANT_StringIndex: {
1213         printf("StringIndex: %s", WARN_MSG);
1214         break;
1215       }
1216       case JVM_CONSTANT_UnresolvedString: {
1217         printf("UnresolvedString: %s", WARN_MSG);
1218         break;
1219       }
1220     }
1221     printf(";\n");
1222     bytes += ent_size;
1223     size  += ent_size;
1224   }
1225   printf("Cpool size: %d\n", size);
1226   fflush(0);
1227   return;
1228 } /* end print_cpool_bytes */
1229 
1230 
1231 // Returns size of constant pool entry.
1232 jint constantPoolOopDesc::cpool_entry_size(jint idx) {
1233   switch(tag_at(idx).value()) {
1234     case JVM_CONSTANT_Invalid:
1235     case JVM_CONSTANT_Unicode:
1236       return 1;
1237 
1238     case JVM_CONSTANT_Utf8:
1239       return 3 + symbol_at(idx)->utf8_length();
1240 
1241     case JVM_CONSTANT_Class:
1242     case JVM_CONSTANT_String:
1243     case JVM_CONSTANT_ClassIndex:
1244     case JVM_CONSTANT_UnresolvedClass:
1245     case JVM_CONSTANT_UnresolvedClassInError:
1246     case JVM_CONSTANT_StringIndex:
1247     case JVM_CONSTANT_UnresolvedString:
1248     case JVM_CONSTANT_MethodType:
1249       return 3;
1250 
1251     case JVM_CONSTANT_MethodHandle:
1252       return 4; //tag, ref_kind, ref_index
1253 
1254     case JVM_CONSTANT_Integer:
1255     case JVM_CONSTANT_Float:
1256     case JVM_CONSTANT_Fieldref:
1257     case JVM_CONSTANT_Methodref:
1258     case JVM_CONSTANT_InterfaceMethodref:
1259     case JVM_CONSTANT_NameAndType:
1260     case JVM_CONSTANT_InvokeDynamic:
1261       return 5;
1262 
1263     case JVM_CONSTANT_Long:
1264     case JVM_CONSTANT_Double:
1265       return 9;
1266   }
1267   assert(false, "cpool_entry_size: Invalid constant pool entry tag");
1268   return 1;
1269 } /* end cpool_entry_size */
1270 
1271 
1272 // SymbolHashMap is used to find a constant pool index from a string.
1273 // This function fills in SymbolHashMaps, one for utf8s and one for
1274 // class names, returns size of the cpool raw bytes.
1275 jint constantPoolOopDesc::hash_entries_to(SymbolHashMap *symmap,
1276                                           SymbolHashMap *classmap) {
1277   jint size = 0;
1278 
1279   for (u2 idx = 1; idx < length(); idx++) {
1280     u2 tag = tag_at(idx).value();
1281     size += cpool_entry_size(idx);
1282 
1283     switch(tag) {
1284       case JVM_CONSTANT_Utf8: {
1285         symbolOop sym = symbol_at(idx);
1286         symmap->add_entry(sym, idx);
1287         DBG(printf("adding symbol entry %s = %d\n", sym->as_utf8(), idx));
1288         break;
1289       }
1290       case JVM_CONSTANT_Class:
1291       case JVM_CONSTANT_UnresolvedClass:
1292       case JVM_CONSTANT_UnresolvedClassInError: {
1293         symbolOop sym = klass_name_at(idx);
1294         classmap->add_entry(sym, idx);
1295         DBG(printf("adding class entry %s = %d\n", sym->as_utf8(), idx));
1296         break;
1297       }
1298       case JVM_CONSTANT_Long:
1299       case JVM_CONSTANT_Double: {
1300         idx++; // Both Long and Double take two cpool slots
1301         break;
1302       }
1303     }
1304   }
1305   return size;
1306 } /* end hash_utf8_entries_to */
1307 
1308 
1309 // Copy cpool bytes.
1310 // Returns:
1311 //    0, in case of OutOfMemoryError
1312 //   -1, in case of internal error
1313 //  > 0, count of the raw cpool bytes that have been copied
1314 int constantPoolOopDesc::copy_cpool_bytes(int cpool_size,
1315                                           SymbolHashMap* tbl,
1316                                           unsigned char *bytes) {
1317   u2   idx1, idx2;
1318   jint size  = 0;
1319   jint cnt   = length();
1320   unsigned char *start_bytes = bytes;
1321 
1322   for (jint idx = 1; idx < cnt; idx++) {
1323     u1   tag      = tag_at(idx).value();
1324     jint ent_size = cpool_entry_size(idx);
1325 
1326     assert(size + ent_size <= cpool_size, "Size mismatch");
1327 
1328     *bytes = tag;
1329     DBG(printf("#%03hd tag=%03hd, ", idx, tag));
1330     switch(tag) {
1331       case JVM_CONSTANT_Invalid: {
1332         DBG(printf("JVM_CONSTANT_Invalid"));
1333         break;
1334       }
1335       case JVM_CONSTANT_Unicode: {
1336         assert(false, "Wrong constant pool tag: JVM_CONSTANT_Unicode");
1337         DBG(printf("JVM_CONSTANT_Unicode"));
1338         break;
1339       }
1340       case JVM_CONSTANT_Utf8: {
1341         symbolOop sym = symbol_at(idx);
1342         char*     str = sym->as_utf8();
1343         // Warning! It's crashing on x86 with len = sym->utf8_length()
1344         int       len = (int) strlen(str);
1345         Bytes::put_Java_u2((address) (bytes+1), (u2) len);
1346         for (int i = 0; i < len; i++) {
1347             bytes[3+i] = (u1) str[i];
1348         }
1349         DBG(printf("JVM_CONSTANT_Utf8: %s ", str));
1350         break;
1351       }
1352       case JVM_CONSTANT_Integer: {
1353         jint val = int_at(idx);
1354         Bytes::put_Java_u4((address) (bytes+1), *(u4*)&val);
1355         break;
1356       }
1357       case JVM_CONSTANT_Float: {
1358         jfloat val = float_at(idx);
1359         Bytes::put_Java_u4((address) (bytes+1), *(u4*)&val);
1360         break;
1361       }
1362       case JVM_CONSTANT_Long: {
1363         jlong val = long_at(idx);
1364         Bytes::put_Java_u8((address) (bytes+1), *(u8*)&val);
1365         idx++;             // Long takes two cpool slots
1366         break;
1367       }
1368       case JVM_CONSTANT_Double: {
1369         jdouble val = double_at(idx);
1370         Bytes::put_Java_u8((address) (bytes+1), *(u8*)&val);
1371         idx++;             // Double takes two cpool slots
1372         break;
1373       }
1374       case JVM_CONSTANT_Class:
1375       case JVM_CONSTANT_UnresolvedClass:
1376       case JVM_CONSTANT_UnresolvedClassInError: {
1377         *bytes = JVM_CONSTANT_Class;
1378         symbolOop sym = klass_name_at(idx);
1379         idx1 = tbl->symbol_to_value(sym);
1380         assert(idx1 != 0, "Have not found a hashtable entry");
1381         Bytes::put_Java_u2((address) (bytes+1), idx1);
1382         DBG(printf("JVM_CONSTANT_Class: idx=#%03hd, %s", idx1, sym->as_utf8()));
1383         break;
1384       }
1385       case JVM_CONSTANT_String: {
1386         unsigned int hash;
1387         char *str = string_at_noresolve(idx);
1388         symbolOop sym = SymbolTable::lookup_only(str, (int) strlen(str), hash);
1389         if (sym == NULL) {
1390           // sym can be NULL if string refers to incorrectly encoded JVM_CONSTANT_Utf8
1391           // this can happen with JVM TI; see CR 6839599 for more details
1392           oop string = *(obj_at_addr(idx));
1393           assert(java_lang_String::is_instance(string),"Not a String");
1394           DBG(printf("Error #%03hd tag=%03hd\n", idx, tag));
1395           idx1 = 0;
1396           for (int j = 0; j < tbl->table_size() && idx1 == 0; j++) {
1397             for (SymbolHashMapEntry* cur = tbl->bucket(j); cur != NULL; cur = cur->next()) {
1398               int length;
1399               sym = cur->symbol();
1400               jchar* chars = sym->as_unicode(length);
1401               if (java_lang_String::equals(string, chars, length)) {
1402                 idx1 = cur->value();
1403                 DBG(printf("Index found: %d\n",idx1));
1404                 break;
1405               }
1406             }
1407           }
1408         } else {
1409           idx1 = tbl->symbol_to_value(sym);
1410         }
1411         assert(idx1 != 0, "Have not found a hashtable entry");
1412         Bytes::put_Java_u2((address) (bytes+1), idx1);
1413         DBG(printf("JVM_CONSTANT_String: idx=#%03hd, %s", idx1, str));
1414         break;
1415       }
1416       case JVM_CONSTANT_UnresolvedString: {
1417         *bytes = JVM_CONSTANT_String;
1418         symbolOop sym = unresolved_string_at(idx);
1419         idx1 = tbl->symbol_to_value(sym);
1420         assert(idx1 != 0, "Have not found a hashtable entry");
1421         Bytes::put_Java_u2((address) (bytes+1), idx1);
1422         DBG(char *str = sym->as_utf8());
1423         DBG(printf("JVM_CONSTANT_UnresolvedString: idx=#%03hd, %s", idx1, str));
1424         break;
1425       }
1426       case JVM_CONSTANT_Fieldref:
1427       case JVM_CONSTANT_Methodref:
1428       case JVM_CONSTANT_InterfaceMethodref: {
1429         idx1 = uncached_klass_ref_index_at(idx);
1430         idx2 = uncached_name_and_type_ref_index_at(idx);
1431         Bytes::put_Java_u2((address) (bytes+1), idx1);
1432         Bytes::put_Java_u2((address) (bytes+3), idx2);
1433         DBG(printf("JVM_CONSTANT_Methodref: %hd %hd", idx1, idx2));
1434         break;
1435       }
1436       case JVM_CONSTANT_NameAndType: {
1437         idx1 = name_ref_index_at(idx);
1438         idx2 = signature_ref_index_at(idx);
1439         Bytes::put_Java_u2((address) (bytes+1), idx1);
1440         Bytes::put_Java_u2((address) (bytes+3), idx2);
1441         DBG(printf("JVM_CONSTANT_NameAndType: %hd %hd", idx1, idx2));
1442         break;
1443       }
1444       case JVM_CONSTANT_ClassIndex: {
1445         *bytes = JVM_CONSTANT_Class;
1446         idx1 = klass_index_at(idx);
1447         Bytes::put_Java_u2((address) (bytes+1), idx1);
1448         DBG(printf("JVM_CONSTANT_ClassIndex: %hd", idx1));
1449         break;
1450       }
1451       case JVM_CONSTANT_StringIndex: {
1452         *bytes = JVM_CONSTANT_String;
1453         idx1 = string_index_at(idx);
1454         Bytes::put_Java_u2((address) (bytes+1), idx1);
1455         DBG(printf("JVM_CONSTANT_StringIndex: %hd", idx1));
1456         break;
1457       }
1458       case JVM_CONSTANT_MethodHandle: {
1459         *bytes = JVM_CONSTANT_MethodHandle;
1460         int kind = method_handle_ref_kind_at(idx);
1461         idx1 = method_handle_index_at(idx);
1462         *(bytes+1) = (unsigned char) kind;
1463         Bytes::put_Java_u2((address) (bytes+2), idx1);
1464         DBG(printf("JVM_CONSTANT_MethodHandle: %d %hd", kind, idx1));
1465         break;
1466       }
1467       case JVM_CONSTANT_MethodType: {
1468         *bytes = JVM_CONSTANT_MethodType;
1469         idx1 = method_type_index_at(idx);
1470         Bytes::put_Java_u2((address) (bytes+1), idx1);
1471         DBG(printf("JVM_CONSTANT_MethodType: %hd", idx1));
1472         break;
1473       }
1474       case JVM_CONSTANT_InvokeDynamic: {
1475         *bytes = JVM_CONSTANT_InvokeDynamic;
1476         idx1 = invoke_dynamic_bootstrap_method_ref_index_at(idx);
1477         idx2 = invoke_dynamic_name_and_type_ref_index_at(idx);
1478         Bytes::put_Java_u2((address) (bytes+1), idx1);
1479         Bytes::put_Java_u2((address) (bytes+3), idx2);
1480         DBG(printf("JVM_CONSTANT_InvokeDynamic: %hd %hd", idx1, idx2));
1481         break;
1482       }
1483     }
1484     DBG(printf("\n"));
1485     bytes += ent_size;
1486     size  += ent_size;
1487   }
1488   assert(size == cpool_size, "Size mismatch");
1489 
1490   // Keep temorarily for debugging until it's stable.
1491   DBG(print_cpool_bytes(cnt, start_bytes));
1492   return (int)(bytes - start_bytes);
1493 } /* end copy_cpool_bytes */
1494 
1495 
1496 void SymbolHashMap::add_entry(symbolOop sym, u2 value) {
1497   char *str = sym->as_utf8();
1498   unsigned int hash = compute_hash(str, sym->utf8_length());
1499   unsigned int index = hash % table_size();
1500 
1501   // check if already in map
1502   // we prefer the first entry since it is more likely to be what was used in
1503   // the class file
1504   for (SymbolHashMapEntry *en = bucket(index); en != NULL; en = en->next()) {
1505     assert(en->symbol() != NULL, "SymbolHashMapEntry symbol is NULL");
1506     if (en->hash() == hash && en->symbol() == sym) {
1507         return;  // already there
1508     }
1509   }
1510 
1511   SymbolHashMapEntry* entry = new SymbolHashMapEntry(hash, sym, value);
1512   entry->set_next(bucket(index));
1513   _buckets[index].set_entry(entry);
1514   assert(entry->symbol() != NULL, "SymbolHashMapEntry symbol is NULL");
1515 }
1516 
1517 SymbolHashMapEntry* SymbolHashMap::find_entry(symbolOop sym) {
1518   assert(sym != NULL, "SymbolHashMap::find_entry - symbol is NULL");
1519   char *str = sym->as_utf8();
1520   int   len = sym->utf8_length();
1521   unsigned int hash = SymbolHashMap::compute_hash(str, len);
1522   unsigned int index = hash % table_size();
1523   for (SymbolHashMapEntry *en = bucket(index); en != NULL; en = en->next()) {
1524     assert(en->symbol() != NULL, "SymbolHashMapEntry symbol is NULL");
1525     if (en->hash() == hash && en->symbol() == sym) {
1526       return en;
1527     }
1528   }
1529   return NULL;
1530 }