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 #ifndef SHARE_VM_OOPS_CONSTANTPOOLOOP_HPP
  26 #define SHARE_VM_OOPS_CONSTANTPOOLOOP_HPP
  27 
  28 #include "oops/arrayOop.hpp"
  29 #include "oops/cpCacheOop.hpp"
  30 #include "oops/typeArrayOop.hpp"
  31 #include "utilities/constantTag.hpp"
  32 #ifdef TARGET_ARCH_x86
  33 # include "bytes_x86.hpp"
  34 #endif
  35 #ifdef TARGET_ARCH_sparc
  36 # include "bytes_sparc.hpp"
  37 #endif
  38 #ifdef TARGET_ARCH_zero
  39 # include "bytes_zero.hpp"
  40 #endif
  41 
  42 // A constantPool is an array containing class constants as described in the
  43 // class file.
  44 //
  45 // Most of the constant pool entries are written during class parsing, which
  46 // is safe.  For klass and string types, the constant pool entry is
  47 // modified when the entry is resolved.  If a klass or string constant pool
  48 // entry is read without a lock, only the resolved state guarantees that
  49 // the entry in the constant pool is a klass or String object and
  50 // not a symbolOop.
  51 
  52 class SymbolHashMap;
  53 
  54 class constantPoolOopDesc : public oopDesc {
  55   friend class VMStructs;
  56   friend class BytecodeInterpreter;  // Directly extracts an oop in the pool for fast instanceof/checkcast
  57  private:
  58   typeArrayOop         _tags; // the tag array describing the constant pool's contents
  59   constantPoolCacheOop _cache;         // the cache holding interpreter runtime information
  60   klassOop             _pool_holder;   // the corresponding class
  61   int                  _flags;         // a few header bits to describe contents for GC
  62   int                  _length; // number of elements in the array
  63   volatile bool        _is_conc_safe; // if true, safe for concurrent
  64                                       // GC processing
  65   // only set to non-zero if constant pool is merged by RedefineClasses
  66   int                  _orig_length;
  67 
  68   void set_tags(typeArrayOop tags)             { oop_store_without_check((oop*)&_tags, tags); }
  69   void tag_at_put(int which, jbyte t)          { tags()->byte_at_put(which, t); }
  70   void release_tag_at_put(int which, jbyte t)  { tags()->release_byte_at_put(which, t); }
  71 
  72   enum FlagBit {
  73     FB_has_invokedynamic = 1,
  74     FB_has_pseudo_string = 2
  75   };
  76 
  77   int flags() const                         { return _flags; }
  78   void set_flags(int f)                     { _flags = f; }
  79   bool flag_at(FlagBit fb) const            { return (_flags & (1 << (int)fb)) != 0; }
  80   void set_flag_at(FlagBit fb);
  81   // no clear_flag_at function; they only increase
  82 
  83  private:
  84   intptr_t* base() const { return (intptr_t*) (((char*) this) + sizeof(constantPoolOopDesc)); }
  85   oop* tags_addr()       { return (oop*)&_tags; }
  86   oop* cache_addr()      { return (oop*)&_cache; }
  87 
  88   oop* obj_at_addr(int which) const {
  89     assert(is_within_bounds(which), "index out of bounds");
  90     return (oop*) &base()[which];
  91   }
  92 
  93   jint* int_at_addr(int which) const {
  94     assert(is_within_bounds(which), "index out of bounds");
  95     return (jint*) &base()[which];
  96   }
  97 
  98   jlong* long_at_addr(int which) const {
  99     assert(is_within_bounds(which), "index out of bounds");
 100     return (jlong*) &base()[which];
 101   }
 102 
 103   jfloat* float_at_addr(int which) const {
 104     assert(is_within_bounds(which), "index out of bounds");
 105     return (jfloat*) &base()[which];
 106   }
 107 
 108   jdouble* double_at_addr(int which) const {
 109     assert(is_within_bounds(which), "index out of bounds");
 110     return (jdouble*) &base()[which];
 111   }
 112 
 113  public:
 114   typeArrayOop tags() const                 { return _tags; }
 115 
 116   bool has_pseudo_string() const            { return flag_at(FB_has_pseudo_string); }
 117   bool has_invokedynamic() const            { return flag_at(FB_has_invokedynamic); }
 118   void set_pseudo_string()                  {    set_flag_at(FB_has_pseudo_string); }
 119   void set_invokedynamic()                  {    set_flag_at(FB_has_invokedynamic); }
 120 
 121   // Klass holding pool
 122   klassOop pool_holder() const              { return _pool_holder; }
 123   void set_pool_holder(klassOop k)          { oop_store_without_check((oop*)&_pool_holder, (oop) k); }
 124   oop* pool_holder_addr()                   { return (oop*)&_pool_holder; }
 125 
 126   // Interpreter runtime support
 127   constantPoolCacheOop cache() const        { return _cache; }
 128   void set_cache(constantPoolCacheOop cache){ oop_store((oop*)&_cache, cache); }
 129 
 130   // Assembly code support
 131   static int tags_offset_in_bytes()         { return offset_of(constantPoolOopDesc, _tags); }
 132   static int cache_offset_in_bytes()        { return offset_of(constantPoolOopDesc, _cache); }
 133   static int pool_holder_offset_in_bytes()  { return offset_of(constantPoolOopDesc, _pool_holder); }
 134 
 135   // Storing constants
 136 
 137   void klass_at_put(int which, klassOop k) {
 138     oop_store_without_check((volatile oop *)obj_at_addr(which), oop(k));
 139     // The interpreter assumes when the tag is stored, the klass is resolved
 140     // and the klassOop is a klass rather than a symbolOop, so we need
 141     // hardware store ordering here.
 142     release_tag_at_put(which, JVM_CONSTANT_Class);
 143     if (UseConcMarkSweepGC) {
 144       // In case the earlier card-mark was consumed by a concurrent
 145       // marking thread before the tag was updated, redirty the card.
 146       oop_store_without_check((volatile oop *)obj_at_addr(which), oop(k));
 147     }
 148   }
 149 
 150   // For temporary use while constructing constant pool
 151   void klass_index_at_put(int which, int name_index) {
 152     tag_at_put(which, JVM_CONSTANT_ClassIndex);
 153     *int_at_addr(which) = name_index;
 154   }
 155 
 156   // Temporary until actual use
 157   void unresolved_klass_at_put(int which, symbolOop s) {
 158     // Overwrite the old index with a GC friendly value so
 159     // that if GC looks during the transition it won't try
 160     // to treat a small integer as oop.
 161     *obj_at_addr(which) = NULL;
 162     release_tag_at_put(which, JVM_CONSTANT_UnresolvedClass);
 163     oop_store_without_check(obj_at_addr(which), oop(s));
 164   }
 165 
 166   void method_handle_index_at_put(int which, int ref_kind, int ref_index) {
 167     tag_at_put(which, JVM_CONSTANT_MethodHandle);
 168     *int_at_addr(which) = ((jint) ref_index<<16) | ref_kind;
 169   }
 170 
 171   void method_type_index_at_put(int which, int ref_index) {
 172     tag_at_put(which, JVM_CONSTANT_MethodType);
 173     *int_at_addr(which) = ref_index;
 174   }
 175 
 176   void invoke_dynamic_at_put(int which, int bootstrap_method_index, int name_and_type_index) {
 177     tag_at_put(which, JVM_CONSTANT_InvokeDynamic);
 178     *int_at_addr(which) = ((jint) name_and_type_index<<16) | bootstrap_method_index;
 179   }
 180 
 181   // Temporary until actual use
 182   void unresolved_string_at_put(int which, symbolOop s) {
 183     *obj_at_addr(which) = NULL;
 184     release_tag_at_put(which, JVM_CONSTANT_UnresolvedString);
 185     oop_store_without_check(obj_at_addr(which), oop(s));
 186   }
 187 
 188   void int_at_put(int which, jint i) {
 189     tag_at_put(which, JVM_CONSTANT_Integer);
 190     *int_at_addr(which) = i;
 191   }
 192 
 193   void long_at_put(int which, jlong l) {
 194     tag_at_put(which, JVM_CONSTANT_Long);
 195     // *long_at_addr(which) = l;
 196     Bytes::put_native_u8((address)long_at_addr(which), *((u8*) &l));
 197   }
 198 
 199   void float_at_put(int which, jfloat f) {
 200     tag_at_put(which, JVM_CONSTANT_Float);
 201     *float_at_addr(which) = f;
 202   }
 203 
 204   void double_at_put(int which, jdouble d) {
 205     tag_at_put(which, JVM_CONSTANT_Double);
 206     // *double_at_addr(which) = d;
 207     // u8 temp = *(u8*) &d;
 208     Bytes::put_native_u8((address) double_at_addr(which), *((u8*) &d));
 209   }
 210 
 211   void symbol_at_put(int which, symbolOop s) {
 212     tag_at_put(which, JVM_CONSTANT_Utf8);
 213     oop_store_without_check(obj_at_addr(which), oop(s));
 214   }
 215 
 216   void string_at_put(int which, oop str) {
 217     oop_store((volatile oop*)obj_at_addr(which), str);
 218     release_tag_at_put(which, JVM_CONSTANT_String);
 219     if (UseConcMarkSweepGC) {
 220       // In case the earlier card-mark was consumed by a concurrent
 221       // marking thread before the tag was updated, redirty the card.
 222       oop_store_without_check((volatile oop *)obj_at_addr(which), str);
 223     }
 224   }
 225 
 226   void object_at_put(int which, oop str) {
 227     oop_store((volatile oop*) obj_at_addr(which), str);
 228     release_tag_at_put(which, JVM_CONSTANT_Object);
 229     if (UseConcMarkSweepGC) {
 230       // In case the earlier card-mark was consumed by a concurrent
 231       // marking thread before the tag was updated, redirty the card.
 232       oop_store_without_check((volatile oop*) obj_at_addr(which), str);
 233     }
 234   }
 235 
 236   // For temporary use while constructing constant pool
 237   void string_index_at_put(int which, int string_index) {
 238     tag_at_put(which, JVM_CONSTANT_StringIndex);
 239     *int_at_addr(which) = string_index;
 240   }
 241 
 242   void field_at_put(int which, int class_index, int name_and_type_index) {
 243     tag_at_put(which, JVM_CONSTANT_Fieldref);
 244     *int_at_addr(which) = ((jint) name_and_type_index<<16) | class_index;
 245   }
 246 
 247   void method_at_put(int which, int class_index, int name_and_type_index) {
 248     tag_at_put(which, JVM_CONSTANT_Methodref);
 249     *int_at_addr(which) = ((jint) name_and_type_index<<16) | class_index;
 250   }
 251 
 252   void interface_method_at_put(int which, int class_index, int name_and_type_index) {
 253     tag_at_put(which, JVM_CONSTANT_InterfaceMethodref);
 254     *int_at_addr(which) = ((jint) name_and_type_index<<16) | class_index;  // Not so nice
 255   }
 256 
 257   void name_and_type_at_put(int which, int name_index, int signature_index) {
 258     tag_at_put(which, JVM_CONSTANT_NameAndType);
 259     *int_at_addr(which) = ((jint) signature_index<<16) | name_index;  // Not so nice
 260   }
 261 
 262   // Tag query
 263 
 264   constantTag tag_at(int which) const { return (constantTag)tags()->byte_at_acquire(which); }
 265 
 266   // Whether the entry is a pointer that must be GC'd.
 267   bool is_pointer_entry(int which) {
 268     constantTag tag = tag_at(which);
 269     return tag.is_klass() ||
 270       tag.is_unresolved_klass() ||
 271       tag.is_symbol() ||
 272       tag.is_unresolved_string() ||
 273       tag.is_string() ||
 274       tag.is_object();
 275   }
 276 
 277   // Fetching constants
 278 
 279   klassOop klass_at(int which, TRAPS) {
 280     constantPoolHandle h_this(THREAD, this);
 281     return klass_at_impl(h_this, which, CHECK_NULL);
 282   }
 283 
 284   symbolOop klass_name_at(int which);  // Returns the name, w/o resolving.
 285 
 286   klassOop resolved_klass_at(int which) {  // Used by Compiler
 287     guarantee(tag_at(which).is_klass(), "Corrupted constant pool");
 288     // Must do an acquire here in case another thread resolved the klass
 289     // behind our back, lest we later load stale values thru the oop.
 290     return klassOop((oop)OrderAccess::load_ptr_acquire(obj_at_addr(which)));
 291   }
 292 
 293   // This method should only be used with a cpool lock or during parsing or gc
 294   symbolOop unresolved_klass_at(int which) {     // Temporary until actual use
 295     symbolOop s = symbolOop((oop)OrderAccess::load_ptr_acquire(obj_at_addr(which)));
 296     // check that the klass is still unresolved.
 297     assert(tag_at(which).is_unresolved_klass(), "Corrupted constant pool");
 298     return s;
 299   }
 300 
 301   // RedefineClasses() API support:
 302   symbolOop klass_at_noresolve(int which) { return klass_name_at(which); }
 303 
 304   jint int_at(int which) {
 305     assert(tag_at(which).is_int(), "Corrupted constant pool");
 306     return *int_at_addr(which);
 307   }
 308 
 309   jlong long_at(int which) {
 310     assert(tag_at(which).is_long(), "Corrupted constant pool");
 311     // return *long_at_addr(which);
 312     u8 tmp = Bytes::get_native_u8((address)&base()[which]);
 313     return *((jlong*)&tmp);
 314   }
 315 
 316   jfloat float_at(int which) {
 317     assert(tag_at(which).is_float(), "Corrupted constant pool");
 318     return *float_at_addr(which);
 319   }
 320 
 321   jdouble double_at(int which) {
 322     assert(tag_at(which).is_double(), "Corrupted constant pool");
 323     u8 tmp = Bytes::get_native_u8((address)&base()[which]);
 324     return *((jdouble*)&tmp);
 325   }
 326 
 327   symbolOop symbol_at(int which) {
 328     assert(tag_at(which).is_utf8(), "Corrupted constant pool");
 329     return symbolOop(*obj_at_addr(which));
 330   }
 331 
 332   oop string_at(int which, TRAPS) {
 333     constantPoolHandle h_this(THREAD, this);
 334     return string_at_impl(h_this, which, CHECK_NULL);
 335   }
 336 
 337   oop object_at(int which) {
 338     assert(tag_at(which).is_object(), "Corrupted constant pool");
 339     return *obj_at_addr(which);
 340   }
 341 
 342   // A "pseudo-string" is an non-string oop that has found is way into
 343   // a String entry.
 344   // Under AnonymousClasses this can happen if the user patches a live
 345   // object into a CONSTANT_String entry of an anonymous class.
 346   // Method oops internally created for method handles may also
 347   // use pseudo-strings to link themselves to related metaobjects.
 348 
 349   bool is_pseudo_string_at(int which);
 350 
 351   oop pseudo_string_at(int which) {
 352     assert(tag_at(which).is_string(), "Corrupted constant pool");
 353     return *obj_at_addr(which);
 354   }
 355 
 356   void pseudo_string_at_put(int which, oop x) {
 357     assert(AnonymousClasses, "");
 358     set_pseudo_string();        // mark header
 359     assert(tag_at(which).is_string() || tag_at(which).is_unresolved_string(), "Corrupted constant pool");
 360     string_at_put(which, x);    // this works just fine
 361   }
 362 
 363   // only called when we are sure a string entry is already resolved (via an
 364   // earlier string_at call.
 365   oop resolved_string_at(int which) {
 366     assert(tag_at(which).is_string(), "Corrupted constant pool");
 367     // Must do an acquire here in case another thread resolved the klass
 368     // behind our back, lest we later load stale values thru the oop.
 369     return (oop)OrderAccess::load_ptr_acquire(obj_at_addr(which));
 370   }
 371 
 372   // This method should only be used with a cpool lock or during parsing or gc
 373   symbolOop unresolved_string_at(int which) {    // Temporary until actual use
 374     symbolOop s = symbolOop((oop)OrderAccess::load_ptr_acquire(obj_at_addr(which)));
 375     // check that the string is still unresolved.
 376     assert(tag_at(which).is_unresolved_string(), "Corrupted constant pool");
 377     return s;
 378   }
 379 
 380   // Returns an UTF8 for a CONSTANT_String entry at a given index.
 381   // UTF8 char* representation was chosen to avoid conversion of
 382   // java_lang_Strings at resolved entries into symbolOops
 383   // or vice versa.
 384   // Caller is responsible for checking for pseudo-strings.
 385   char* string_at_noresolve(int which);
 386 
 387   jint name_and_type_at(int which) {
 388     assert(tag_at(which).is_name_and_type(), "Corrupted constant pool");
 389     return *int_at_addr(which);
 390   }
 391 
 392   int method_handle_ref_kind_at(int which) {
 393     assert(tag_at(which).is_method_handle(), "Corrupted constant pool");
 394     return extract_low_short_from_int(*int_at_addr(which));  // mask out unwanted ref_index bits
 395   }
 396   int method_handle_index_at(int which) {
 397     assert(tag_at(which).is_method_handle(), "Corrupted constant pool");
 398     return extract_high_short_from_int(*int_at_addr(which));  // shift out unwanted ref_kind bits
 399   }
 400   int method_type_index_at(int which) {
 401     assert(tag_at(which).is_method_type(), "Corrupted constant pool");
 402     return *int_at_addr(which);
 403   }
 404   // Derived queries:
 405   symbolOop method_handle_name_ref_at(int which) {
 406     int member = method_handle_index_at(which);
 407     return impl_name_ref_at(member, true);
 408   }
 409   symbolOop method_handle_signature_ref_at(int which) {
 410     int member = method_handle_index_at(which);
 411     return impl_signature_ref_at(member, true);
 412   }
 413   int method_handle_klass_index_at(int which) {
 414     int member = method_handle_index_at(which);
 415     return impl_klass_ref_index_at(member, true);
 416   }
 417   symbolOop method_type_signature_at(int which) {
 418     int sym = method_type_index_at(which);
 419     return symbol_at(sym);
 420   }
 421   int invoke_dynamic_bootstrap_method_ref_index_at(int which) {
 422     assert(tag_at(which).is_invoke_dynamic(), "Corrupted constant pool");
 423     jint ref_index = *int_at_addr(which);
 424     return extract_low_short_from_int(ref_index);
 425   }
 426   int invoke_dynamic_name_and_type_ref_index_at(int which) {
 427     assert(tag_at(which).is_invoke_dynamic(), "Corrupted constant pool");
 428     jint ref_index = *int_at_addr(which);
 429     return extract_high_short_from_int(ref_index);
 430   }
 431 
 432   // The following methods (name/signature/klass_ref_at, klass_ref_at_noresolve,
 433   // name_and_type_ref_index_at) all expect to be passed indices obtained
 434   // directly from the bytecode, and extracted according to java byte order.
 435   // If the indices are meant to refer to fields or methods, they are
 436   // actually potentially byte-swapped, rewritten constant pool cache indices.
 437   // The routine remap_instruction_operand_from_cache manages the adjustment
 438   // of these values back to constant pool indices.
 439 
 440   // There are also "uncached" versions which do not adjust the operand index; see below.
 441 
 442   // Lookup for entries consisting of (klass_index, name_and_type index)
 443   klassOop klass_ref_at(int which, TRAPS);
 444   symbolOop klass_ref_at_noresolve(int which);
 445   symbolOop name_ref_at(int which)                { return impl_name_ref_at(which, false); }
 446   symbolOop signature_ref_at(int which)           { return impl_signature_ref_at(which, false); }
 447 
 448   int klass_ref_index_at(int which)               { return impl_klass_ref_index_at(which, false); }
 449   int name_and_type_ref_index_at(int which)       { return impl_name_and_type_ref_index_at(which, false); }
 450 
 451   // Lookup for entries consisting of (name_index, signature_index)
 452   int name_ref_index_at(int which_nt);            // ==  low-order jshort of name_and_type_at(which_nt)
 453   int signature_ref_index_at(int which_nt);       // == high-order jshort of name_and_type_at(which_nt)
 454 
 455   BasicType basic_type_for_signature_at(int which);
 456 
 457   // Resolve string constants (to prevent allocation during compilation)
 458   void resolve_string_constants(TRAPS) {
 459     constantPoolHandle h_this(THREAD, this);
 460     resolve_string_constants_impl(h_this, CHECK);
 461   }
 462 
 463   // Resolve late bound constants.
 464   oop resolve_constant_at(int index, TRAPS) {
 465     constantPoolHandle h_this(THREAD, this);
 466     return resolve_constant_at_impl(h_this, index, -1, THREAD);
 467   }
 468 
 469   oop resolve_cached_constant_at(int cache_index, TRAPS) {
 470     constantPoolHandle h_this(THREAD, this);
 471     return resolve_constant_at_impl(h_this, -1, cache_index, THREAD);
 472   }
 473 
 474   // Klass name matches name at offset
 475   bool klass_name_at_matches(instanceKlassHandle k, int which);
 476 
 477   // Sizing
 478   int length() const                   { return _length; }
 479   void set_length(int length)          { _length = length; }
 480 
 481   // Tells whether index is within bounds.
 482   bool is_within_bounds(int index) const {
 483     return 0 <= index && index < length();
 484   }
 485 
 486   static int header_size()             { return sizeof(constantPoolOopDesc)/HeapWordSize; }
 487   static int object_size(int length)   { return align_object_size(header_size() + length); }
 488   int object_size()                    { return object_size(length()); }
 489 
 490   bool is_conc_safe()                  { return _is_conc_safe; }
 491   void set_is_conc_safe(bool v)        { _is_conc_safe = v; }
 492 
 493   friend class constantPoolKlass;
 494   friend class ClassFileParser;
 495   friend class SystemDictionary;
 496 
 497   // Used by compiler to prevent classloading.
 498   static klassOop klass_at_if_loaded          (constantPoolHandle this_oop, int which);
 499   static klassOop klass_ref_at_if_loaded      (constantPoolHandle this_oop, int which);
 500   // Same as above - but does LinkResolving.
 501   static klassOop klass_ref_at_if_loaded_check(constantPoolHandle this_oop, int which, TRAPS);
 502 
 503   // Routines currently used for annotations (only called by jvm.cpp) but which might be used in the
 504   // future by other Java code. These take constant pool indices rather than possibly-byte-swapped
 505   // constant pool cache indices as do the peer methods above.
 506   symbolOop uncached_klass_ref_at_noresolve(int which);
 507   symbolOop uncached_name_ref_at(int which)                 { return impl_name_ref_at(which, true); }
 508   symbolOop uncached_signature_ref_at(int which)            { return impl_signature_ref_at(which, true); }
 509   int       uncached_klass_ref_index_at(int which)          { return impl_klass_ref_index_at(which, true); }
 510   int       uncached_name_and_type_ref_index_at(int which)  { return impl_name_and_type_ref_index_at(which, true); }
 511 
 512   // Sharing
 513   int pre_resolve_shared_klasses(TRAPS);
 514   void shared_symbols_iterate(OopClosure* closure0);
 515   void shared_tags_iterate(OopClosure* closure0);
 516   void shared_strings_iterate(OopClosure* closure0);
 517 
 518   // Debugging
 519   const char* printable_name_at(int which) PRODUCT_RETURN0;
 520 
 521 #ifdef ASSERT
 522   enum { CPCACHE_INDEX_TAG = 0x10000 };  // helps keep CP cache indices distinct from CP indices
 523 #else
 524   enum { CPCACHE_INDEX_TAG = 0 };        // in product mode, this zero value is a no-op
 525 #endif //ASSERT
 526 
 527  private:
 528 
 529   symbolOop impl_name_ref_at(int which, bool uncached);
 530   symbolOop impl_signature_ref_at(int which, bool uncached);
 531   int       impl_klass_ref_index_at(int which, bool uncached);
 532   int       impl_name_and_type_ref_index_at(int which, bool uncached);
 533 
 534   int remap_instruction_operand_from_cache(int operand);  // operand must be biased by CPCACHE_INDEX_TAG
 535 
 536   // Used while constructing constant pool (only by ClassFileParser)
 537   jint klass_index_at(int which) {
 538     assert(tag_at(which).is_klass_index(), "Corrupted constant pool");
 539     return *int_at_addr(which);
 540   }
 541 
 542   jint string_index_at(int which) {
 543     assert(tag_at(which).is_string_index(), "Corrupted constant pool");
 544     return *int_at_addr(which);
 545   }
 546 
 547   // Performs the LinkResolver checks
 548   static void verify_constant_pool_resolve(constantPoolHandle this_oop, KlassHandle klass, TRAPS);
 549 
 550   // Implementation of methods that needs an exposed 'this' pointer, in order to
 551   // handle GC while executing the method
 552   static klassOop klass_at_impl(constantPoolHandle this_oop, int which, TRAPS);
 553   static oop string_at_impl(constantPoolHandle this_oop, int which, TRAPS);
 554 
 555   // Resolve string constants (to prevent allocation during compilation)
 556   static void resolve_string_constants_impl(constantPoolHandle this_oop, TRAPS);
 557 
 558   static oop resolve_constant_at_impl(constantPoolHandle this_oop, int index, int cache_index, TRAPS);
 559 
 560  public:
 561   // Merging constantPoolOop support:
 562   bool compare_entry_to(int index1, constantPoolHandle cp2, int index2, TRAPS);
 563   void copy_cp_to(int start_i, int end_i, constantPoolHandle to_cp, int to_i,
 564     TRAPS);
 565   void copy_entry_to(int from_i, constantPoolHandle to_cp, int to_i, TRAPS);
 566   int  find_matching_entry(int pattern_i, constantPoolHandle search_cp, TRAPS);
 567   int  orig_length() const                { return _orig_length; }
 568   void set_orig_length(int orig_length)   { _orig_length = orig_length; }
 569 
 570 
 571   // JVMTI accesss - GetConstantPool, RetransformClasses, ...
 572   friend class JvmtiConstantPoolReconstituter;
 573 
 574  private:
 575   jint cpool_entry_size(jint idx);
 576   jint hash_entries_to(SymbolHashMap *symmap, SymbolHashMap *classmap);
 577 
 578   // Copy cpool bytes into byte array.
 579   // Returns:
 580   //  int > 0, count of the raw cpool bytes that have been copied
 581   //        0, OutOfMemory error
 582   //       -1, Internal error
 583   int  copy_cpool_bytes(int cpool_size,
 584                         SymbolHashMap* tbl,
 585                         unsigned char *bytes);
 586 };
 587 
 588 class SymbolHashMapEntry : public CHeapObj {
 589  private:
 590   unsigned int        _hash;   // 32-bit hash for item
 591   SymbolHashMapEntry* _next;   // Next element in the linked list for this bucket
 592   symbolOop           _symbol; // 1-st part of the mapping: symbol => value
 593   u2                  _value;  // 2-nd part of the mapping: symbol => value
 594 
 595  public:
 596   unsigned   int hash() const             { return _hash;   }
 597   void       set_hash(unsigned int hash)  { _hash = hash;   }
 598 
 599   SymbolHashMapEntry* next() const        { return _next;   }
 600   void set_next(SymbolHashMapEntry* next) { _next = next;   }
 601 
 602   symbolOop  symbol() const               { return _symbol; }
 603   void       set_symbol(symbolOop sym)    { _symbol = sym;  }
 604 
 605   u2         value() const                {  return _value; }
 606   void       set_value(u2 value)          { _value = value; }
 607 
 608   SymbolHashMapEntry(unsigned int hash, symbolOop symbol, u2 value)
 609     : _hash(hash), _symbol(symbol), _value(value), _next(NULL) {}
 610 
 611 }; // End SymbolHashMapEntry class
 612 
 613 
 614 class SymbolHashMapBucket : public CHeapObj {
 615 
 616 private:
 617   SymbolHashMapEntry*    _entry;
 618 
 619 public:
 620   SymbolHashMapEntry* entry() const         {  return _entry; }
 621   void set_entry(SymbolHashMapEntry* entry) { _entry = entry; }
 622   void clear()                              { _entry = NULL;  }
 623 
 624 }; // End SymbolHashMapBucket class
 625 
 626 
 627 class SymbolHashMap: public CHeapObj {
 628 
 629  private:
 630   // Default number of entries in the table
 631   enum SymbolHashMap_Constants {
 632     _Def_HashMap_Size = 256
 633   };
 634 
 635   int                   _table_size;
 636   SymbolHashMapBucket*  _buckets;
 637 
 638   void initialize_table(int table_size) {
 639     _table_size = table_size;
 640     _buckets = NEW_C_HEAP_ARRAY(SymbolHashMapBucket, table_size);
 641     for (int index = 0; index < table_size; index++) {
 642       _buckets[index].clear();
 643     }
 644   }
 645 
 646  public:
 647 
 648   int table_size() const        { return _table_size; }
 649 
 650   SymbolHashMap()               { initialize_table(_Def_HashMap_Size); }
 651   SymbolHashMap(int table_size) { initialize_table(table_size); }
 652 
 653   // hash P(31) from Kernighan & Ritchie
 654   static unsigned int compute_hash(const char* str, int len) {
 655     unsigned int hash = 0;
 656     while (len-- > 0) {
 657       hash = 31*hash + (unsigned) *str;
 658       str++;
 659     }
 660     return hash;
 661   }
 662 
 663   SymbolHashMapEntry* bucket(int i) {
 664     return _buckets[i].entry();
 665   }
 666 
 667   void add_entry(symbolOop sym, u2 value);
 668   SymbolHashMapEntry* find_entry(symbolOop sym);
 669 
 670   u2 symbol_to_value(symbolOop sym) {
 671     SymbolHashMapEntry *entry = find_entry(sym);
 672     return (entry == NULL) ? 0 : entry->value();
 673   }
 674 
 675   ~SymbolHashMap() {
 676     SymbolHashMapEntry* next;
 677     for (int i = 0; i < _table_size; i++) {
 678       for (SymbolHashMapEntry* cur = bucket(i); cur != NULL; cur = next) {
 679         next = cur->next();
 680         delete(cur);
 681       }
 682     }
 683     delete _buckets;
 684   }
 685 }; // End SymbolHashMap class
 686 
 687 #endif // SHARE_VM_OOPS_CONSTANTPOOLOOP_HPP