constantPoolOop.cpp 57.7 KB
Newer Older
D
duke 已提交
1
/*
2
 * Copyright (c) 1997, 2011, Oracle and/or its affiliates. All rights reserved.
D
duke 已提交
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
 *
 * This code is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License version 2 only, as
 * published by the Free Software Foundation.
 *
 * This code is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 * version 2 for more details (a copy is included in the LICENSE file that
 * accompanied this code).
 *
 * You should have received a copy of the GNU General Public License version
 * 2 along with this work; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 *
19 20 21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 * or visit www.oracle.com if you need additional information or have any
 * questions.
D
duke 已提交
22 23 24
 *
 */

25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40
#include "precompiled.hpp"
#include "classfile/javaClasses.hpp"
#include "classfile/symbolTable.hpp"
#include "classfile/systemDictionary.hpp"
#include "classfile/vmSymbols.hpp"
#include "interpreter/linkResolver.hpp"
#include "memory/oopFactory.hpp"
#include "memory/universe.inline.hpp"
#include "oops/constantPoolOop.hpp"
#include "oops/instanceKlass.hpp"
#include "oops/objArrayKlass.hpp"
#include "oops/oop.inline.hpp"
#include "runtime/fieldType.hpp"
#include "runtime/init.hpp"
#include "runtime/signature.hpp"
#include "runtime/vframe.hpp"
D
duke 已提交
41

42 43 44 45 46 47 48 49 50 51 52 53
void constantPoolOopDesc::set_flag_at(FlagBit fb) {
  const int MAX_STATE_CHANGES = 2;
  for (int i = MAX_STATE_CHANGES + 10; i > 0; i--) {
    int oflags = _flags;
    int nflags = oflags | (1 << (int)fb);
    if (Atomic::cmpxchg(nflags, &_flags, oflags) == oflags)
      return;
  }
  assert(false, "failed to cmpxchg flags");
  _flags |= (1 << (int)fb);     // better than nothing
}

D
duke 已提交
54
klassOop constantPoolOopDesc::klass_at_impl(constantPoolHandle this_oop, int which, TRAPS) {
55
  // A resolved constantPool entry will contain a klassOop, otherwise a Symbol*.
D
duke 已提交
56 57
  // It is not safe to rely on the tag bit's here, since we don't have a lock, and the entry and
  // tag is not updated atomicly.
58 59 60
  CPSlot entry = this_oop->slot_at(which);
  if (entry.is_oop()) {
    assert(entry.get_oop()->is_klass(), "must be");
D
duke 已提交
61
    // Already resolved - return entry.
62
    return (klassOop)entry.get_oop();
D
duke 已提交
63 64 65 66 67 68 69 70
  }

  // Acquire lock on constant oop while doing update. After we get the lock, we check if another object
  // already has updated the object
  assert(THREAD->is_Java_thread(), "must be a Java thread");
  bool do_resolve = false;
  bool in_error = false;

71
  Symbol* name = NULL;
D
duke 已提交
72 73 74 75 76 77 78 79
  Handle       loader;
  { ObjectLocker ol(this_oop, THREAD);

    if (this_oop->tag_at(which).is_unresolved_klass()) {
      if (this_oop->tag_at(which).is_unresolved_klass_in_error()) {
        in_error = true;
      } else {
        do_resolve = true;
80
        name   = this_oop->unresolved_klass_at(which);
D
duke 已提交
81 82 83 84 85 86 87 88 89
        loader = Handle(THREAD, instanceKlass::cast(this_oop->pool_holder())->class_loader());
      }
    }
  } // unlocking constantPool


  // The original attempt to resolve this constant pool entry failed so find the
  // original error and throw it again (JVMS 5.4.3).
  if (in_error) {
90 91
    Symbol* error = SystemDictionary::find_resolution_error(this_oop, which);
    guarantee(error != (Symbol*)NULL, "tag mismatch with resolution error table");
D
duke 已提交
92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113
    ResourceMark rm;
    // exception text will be the class name
    const char* className = this_oop->unresolved_klass_at(which)->as_C_string();
    THROW_MSG_0(error, className);
  }

  if (do_resolve) {
    // this_oop must be unlocked during resolve_or_fail
    oop protection_domain = Klass::cast(this_oop->pool_holder())->protection_domain();
    Handle h_prot (THREAD, protection_domain);
    klassOop k_oop = SystemDictionary::resolve_or_fail(name, loader, h_prot, true, THREAD);
    KlassHandle k;
    if (!HAS_PENDING_EXCEPTION) {
      k = KlassHandle(THREAD, k_oop);
      // Do access check for klasses
      verify_constant_pool_resolve(this_oop, k, THREAD);
    }

    // Failed to resolve class. We must record the errors so that subsequent attempts
    // to resolve this constant pool entry fail with the same error (JVMS 5.4.3).
    if (HAS_PENDING_EXCEPTION) {
      ResourceMark rm;
114
      Symbol* error = PENDING_EXCEPTION->klass()->klass_part()->name();
D
duke 已提交
115 116 117 118 119 120 121 122 123

      bool throw_orig_error = false;
      {
        ObjectLocker ol (this_oop, THREAD);

        // some other thread has beaten us and has resolved the class.
        if (this_oop->tag_at(which).is_klass()) {
          CLEAR_PENDING_EXCEPTION;
          entry = this_oop->resolved_klass_at(which);
124
          return (klassOop)entry.get_oop();
D
duke 已提交
125 126 127
        }

        if (!PENDING_EXCEPTION->
128
              is_a(SystemDictionary::LinkageError_klass())) {
D
duke 已提交
129 130 131 132 133 134 135 136 137 138
          // Just throw the exception and don't prevent these classes from
          // being loaded due to virtual machine errors like StackOverflow
          // and OutOfMemoryError, etc, or if the thread was hit by stop()
          // Needs clarification to section 5.4.3 of the VM spec (see 6308271)
        }
        else if (!this_oop->tag_at(which).is_unresolved_klass_in_error()) {
          SystemDictionary::add_resolution_error(this_oop, which, error);
          this_oop->tag_at_put(which, JVM_CONSTANT_UnresolvedClassInError);
        } else {
          // some other thread has put the class in error state.
139 140
          error = SystemDictionary::find_resolution_error(this_oop, which);
          assert(error != NULL, "checking");
D
duke 已提交
141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165
          throw_orig_error = true;
        }
      } // unlocked

      if (throw_orig_error) {
        CLEAR_PENDING_EXCEPTION;
        ResourceMark rm;
        const char* className = this_oop->unresolved_klass_at(which)->as_C_string();
        THROW_MSG_0(error, className);
      }

      return 0;
    }

    if (TraceClassResolution && !k()->klass_part()->oop_is_array()) {
      // skip resolving the constant pool so that this code get's
      // called the next time some bytecodes refer to this class.
      ResourceMark rm;
      int line_number = -1;
      const char * source_file = NULL;
      if (JavaThread::current()->has_last_Java_frame()) {
        // try to identify the method which called this function.
        vframeStream vfst(JavaThread::current());
        if (!vfst.at_end()) {
          line_number = vfst.method()->line_number_from_bci(vfst.bci());
166
          Symbol* s = instanceKlass::cast(vfst.method()->method_holder())->source_file_name();
D
duke 已提交
167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195
          if (s != NULL) {
            source_file = s->as_C_string();
          }
        }
      }
      if (k() != this_oop->pool_holder()) {
        // only print something if the classes are different
        if (source_file != NULL) {
          tty->print("RESOLVE %s %s %s:%d\n",
                     instanceKlass::cast(this_oop->pool_holder())->external_name(),
                     instanceKlass::cast(k())->external_name(), source_file, line_number);
        } else {
          tty->print("RESOLVE %s %s\n",
                     instanceKlass::cast(this_oop->pool_holder())->external_name(),
                     instanceKlass::cast(k())->external_name());
        }
      }
      return k();
    } else {
      ObjectLocker ol (this_oop, THREAD);
      // Only updated constant pool - if it is resolved.
      do_resolve = this_oop->tag_at(which).is_unresolved_klass();
      if (do_resolve) {
        this_oop->klass_at_put(which, k());
      }
    }
  }

  entry = this_oop->resolved_klass_at(which);
196 197
  assert(entry.is_oop() && entry.get_oop()->is_klass(), "must be resolved at this point");
  return (klassOop)entry.get_oop();
D
duke 已提交
198 199 200 201 202 203 204 205
}


// Does not update constantPoolOop - to avoid any exception throwing. Used
// by compiler and exception handling.  Also used to avoid classloads for
// instanceof operations. Returns NULL if the class has not been loaded or
// if the verification of constant pool failed
klassOop constantPoolOopDesc::klass_at_if_loaded(constantPoolHandle this_oop, int which) {
206 207 208 209
  CPSlot entry = this_oop->slot_at(which);
  if (entry.is_oop()) {
    assert(entry.get_oop()->is_klass(), "must be");
    return (klassOop)entry.get_oop();
D
duke 已提交
210
  } else {
211
    assert(entry.is_metadata(), "must be either symbol or klass");
D
duke 已提交
212
    Thread *thread = Thread::current();
213
    Symbol* name = entry.get_symbol();
D
duke 已提交
214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248
    oop loader = instanceKlass::cast(this_oop->pool_holder())->class_loader();
    oop protection_domain = Klass::cast(this_oop->pool_holder())->protection_domain();
    Handle h_prot (thread, protection_domain);
    Handle h_loader (thread, loader);
    klassOop k = SystemDictionary::find(name, h_loader, h_prot, thread);

    if (k != NULL) {
      // Make sure that resolving is legal
      EXCEPTION_MARK;
      KlassHandle klass(THREAD, k);
      // return NULL if verification fails
      verify_constant_pool_resolve(this_oop, klass, THREAD);
      if (HAS_PENDING_EXCEPTION) {
        CLEAR_PENDING_EXCEPTION;
        return NULL;
      }
      return klass();
    } else {
      return k;
    }
  }
}


klassOop constantPoolOopDesc::klass_ref_at_if_loaded(constantPoolHandle this_oop, int which) {
  return klass_at_if_loaded(this_oop, this_oop->klass_ref_index_at(which));
}


// This is an interface for the compiler that allows accessing non-resolved entries
// in the constant pool - but still performs the validations tests. Must be used
// in a pre-parse of the compiler - to determine what it can do and not do.
// Note: We cannot update the ConstantPool from the vm_thread.
klassOop constantPoolOopDesc::klass_ref_at_if_loaded_check(constantPoolHandle this_oop, int index, TRAPS) {
  int which = this_oop->klass_ref_index_at(index);
249 250 251 252
  CPSlot entry = this_oop->slot_at(which);
  if (entry.is_oop()) {
    assert(entry.get_oop()->is_klass(), "must be");
    return (klassOop)entry.get_oop();
D
duke 已提交
253
  } else {
254 255
    assert(entry.is_metadata(), "must be either symbol or klass");
    Symbol*  name  = entry.get_symbol();
D
duke 已提交
256 257 258 259 260 261 262 263 264 265 266 267 268
    oop loader = instanceKlass::cast(this_oop->pool_holder())->class_loader();
    oop protection_domain = Klass::cast(this_oop->pool_holder())->protection_domain();
    Handle h_loader(THREAD, loader);
    Handle h_prot  (THREAD, protection_domain);
    KlassHandle k(THREAD, SystemDictionary::find(name, h_loader, h_prot, THREAD));

    // Do access check for klasses
    if( k.not_null() ) verify_constant_pool_resolve(this_oop, k, CHECK_NULL);
    return k();
  }
}


269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291
methodOop constantPoolOopDesc::method_at_if_loaded(constantPoolHandle cpool,
                                                   int which, Bytecodes::Code invoke_code) {
  assert(!constantPoolCacheOopDesc::is_secondary_index(which), "no indy instruction here");
  if (cpool->cache() == NULL)  return false;  // nothing to load yet
  int cache_index = which - CPCACHE_INDEX_TAG;
  if (!(cache_index >= 0 && cache_index < cpool->cache()->length())) {
    if (PrintMiscellaneous && (Verbose||WizardMode)) {
      tty->print_cr("bad operand %d for %d in:", which, invoke_code); cpool->print();
    }
    return NULL;
  }
  ConstantPoolCacheEntry* e = cpool->cache()->entry_at(cache_index);
  if (invoke_code != Bytecodes::_illegal)
    return e->get_method_if_resolved(invoke_code, cpool);
  Bytecodes::Code bc;
  if ((bc = e->bytecode_1()) != (Bytecodes::Code)0)
    return e->get_method_if_resolved(bc, cpool);
  if ((bc = e->bytecode_2()) != (Bytecodes::Code)0)
    return e->get_method_if_resolved(bc, cpool);
  return NULL;
}


292
Symbol* constantPoolOopDesc::impl_name_ref_at(int which, bool uncached) {
293
  int name_index = name_ref_index_at(impl_name_and_type_ref_index_at(which, uncached));
D
duke 已提交
294 295 296 297
  return symbol_at(name_index);
}


298
Symbol* constantPoolOopDesc::impl_signature_ref_at(int which, bool uncached) {
299
  int signature_index = signature_ref_index_at(impl_name_and_type_ref_index_at(which, uncached));
D
duke 已提交
300 301 302 303
  return symbol_at(signature_index);
}


304
int constantPoolOopDesc::impl_name_and_type_ref_index_at(int which, bool uncached) {
305 306
  int i = which;
  if (!uncached && cache() != NULL) {
307
    if (constantPoolCacheOopDesc::is_secondary_index(which)) {
308
      // Invokedynamic index.
309
      int pool_index = cache()->main_entry_at(which)->constant_pool_index();
310
      pool_index = invoke_dynamic_name_and_type_ref_index_at(pool_index);
311 312 313
      assert(tag_at(pool_index).is_name_and_type(), "");
      return pool_index;
    }
314 315 316
    // change byte-ordering and go via cache
    i = remap_instruction_operand_from_cache(which);
  } else {
317 318 319 320 321
    if (tag_at(which).is_invoke_dynamic()) {
      int pool_index = invoke_dynamic_name_and_type_ref_index_at(which);
      assert(tag_at(pool_index).is_name_and_type(), "");
      return pool_index;
    }
322 323
  }
  assert(tag_at(i).is_field_or_method(), "Corrupted constant pool");
324
  assert(!tag_at(i).is_invoke_dynamic(), "Must be handled above");
325
  jint ref_index = *int_at_addr(i);
D
duke 已提交
326 327 328 329
  return extract_high_short_from_int(ref_index);
}


330
int constantPoolOopDesc::impl_klass_ref_index_at(int which, bool uncached) {
331 332 333 334 335 336 337 338 339
  guarantee(!constantPoolCacheOopDesc::is_secondary_index(which),
            "an invokedynamic instruction does not have a klass");
  int i = which;
  if (!uncached && cache() != NULL) {
    // change byte-ordering and go via cache
    i = remap_instruction_operand_from_cache(which);
  }
  assert(tag_at(i).is_field_or_method(), "Corrupted constant pool");
  jint ref_index = *int_at_addr(i);
D
duke 已提交
340 341 342 343
  return extract_low_short_from_int(ref_index);
}


344

345
int constantPoolOopDesc::remap_instruction_operand_from_cache(int operand) {
346 347 348
  int cpc_index = operand;
  DEBUG_ONLY(cpc_index -= CPCACHE_INDEX_TAG);
  assert((int)(u2)cpc_index == cpc_index, "clean u2");
349 350
  int member_index = cache()->entry_at(cpc_index)->constant_pool_index();
  return member_index;
351 352 353
}


D
duke 已提交
354 355 356 357 358 359 360 361 362 363 364 365 366 367 368
void constantPoolOopDesc::verify_constant_pool_resolve(constantPoolHandle this_oop, KlassHandle k, TRAPS) {
 if (k->oop_is_instance() || k->oop_is_objArray()) {
    instanceKlassHandle holder (THREAD, this_oop->pool_holder());
    klassOop elem_oop = k->oop_is_instance() ? k() : objArrayKlass::cast(k())->bottom_klass();
    KlassHandle element (THREAD, elem_oop);

    // The element type could be a typeArray - we only need the access check if it is
    // an reference to another class
    if (element->oop_is_instance()) {
      LinkResolver::check_klass_accessability(holder, element, CHECK);
    }
  }
}


369 370
int constantPoolOopDesc::name_ref_index_at(int which_nt) {
  jint ref_index = name_and_type_at(which_nt);
D
duke 已提交
371 372 373 374
  return extract_low_short_from_int(ref_index);
}


375 376
int constantPoolOopDesc::signature_ref_index_at(int which_nt) {
  jint ref_index = name_and_type_at(which_nt);
D
duke 已提交
377 378 379 380 381 382 383 384 385
  return extract_high_short_from_int(ref_index);
}


klassOop constantPoolOopDesc::klass_ref_at(int which, TRAPS) {
  return klass_at(klass_ref_index_at(which), CHECK_NULL);
}


386
Symbol* constantPoolOopDesc::klass_name_at(int which) {
D
duke 已提交
387 388
  assert(tag_at(which).is_unresolved_klass() || tag_at(which).is_klass(),
         "Corrupted constant pool");
389
  // A resolved constantPool entry will contain a klassOop, otherwise a Symbol*.
D
duke 已提交
390 391
  // It is not safe to rely on the tag bit's here, since we don't have a lock, and the entry and
  // tag is not updated atomicly.
392 393
  CPSlot entry = slot_at(which);
  if (entry.is_oop()) {
D
duke 已提交
394
    // Already resolved - return entry's name.
395 396
    assert(entry.get_oop()->is_klass(), "must be");
    return klassOop(entry.get_oop())->klass_part()->name();
D
duke 已提交
397
  } else {
398 399
    assert(entry.is_metadata(), "must be either symbol or klass");
    return entry.get_symbol();
D
duke 已提交
400 401 402
  }
}

403
Symbol* constantPoolOopDesc::klass_ref_at_noresolve(int which) {
D
duke 已提交
404 405 406 407
  jint ref_index = klass_ref_index_at(which);
  return klass_at_noresolve(ref_index);
}

408
Symbol* constantPoolOopDesc::uncached_klass_ref_at_noresolve(int which) {
409 410 411 412
  jint ref_index = uncached_klass_ref_index_at(which);
  return klass_at_noresolve(ref_index);
}

D
duke 已提交
413 414
char* constantPoolOopDesc::string_at_noresolve(int which) {
  // Test entry type in case string is resolved while in here.
415 416 417 418 419
  CPSlot entry = slot_at(which);
  if (entry.is_metadata()) {
    return (entry.get_symbol())->as_C_string();
  } else if (java_lang_String::is_instance(entry.get_oop())) {
    return java_lang_String::as_utf8_string(entry.get_oop());
420 421
  } else {
    return (char*)"<pseudo-string>";
D
duke 已提交
422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438
  }
}


BasicType constantPoolOopDesc::basic_type_for_signature_at(int which) {
  return FieldType::basic_type(symbol_at(which));
}


void constantPoolOopDesc::resolve_string_constants_impl(constantPoolHandle this_oop, TRAPS) {
  for (int index = 1; index < this_oop->length(); index++) { // Index 0 is unused
    if (this_oop->tag_at(index).is_unresolved_string()) {
      this_oop->string_at(index, CHECK);
    }
  }
}

439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456
// A resolved constant value in the CP cache is represented as a non-null
// value.  As a special case, this value can be a 'systemObjArray'
// which masks an exception object to throw.
// This allows a MethodHandle constant reference to throw a consistent
// exception every time, if it fails to resolve.
static oop decode_exception_from_f1(oop result_oop, TRAPS) {
  if (result_oop->klass() != Universe::systemObjArrayKlassObj())
    return result_oop;

  // Special cases here:  Masked null, saved exception.
  objArrayOop sys_array = (objArrayOop) result_oop;
  assert(sys_array->length() == 1, "bad system array");
  if (sys_array->length() == 1) {
    THROW_OOP_(sys_array->obj_at(0), NULL);
  }
  return NULL;
}

457 458
oop constantPoolOopDesc::resolve_constant_at_impl(constantPoolHandle this_oop, int index, int cache_index, TRAPS) {
  oop result_oop = NULL;
459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480
  Handle throw_exception;

  if (cache_index == _possible_index_sentinel) {
    // It is possible that this constant is one which is cached in the CP cache.
    // We'll do a linear search.  This should be OK because this usage is rare.
    assert(index > 0, "valid index");
    constantPoolCacheOop cache = this_oop()->cache();
    for (int i = 0, len = cache->length(); i < len; i++) {
      ConstantPoolCacheEntry* cpc_entry = cache->entry_at(i);
      if (!cpc_entry->is_secondary_entry() && cpc_entry->constant_pool_index() == index) {
        // Switch the query to use this CPC entry.
        cache_index = i;
        index = _no_index_sentinel;
        break;
      }
    }
    if (cache_index == _possible_index_sentinel)
      cache_index = _no_index_sentinel;  // not found
  }
  assert(cache_index == _no_index_sentinel || cache_index >= 0, "");
  assert(index == _no_index_sentinel || index >= 0, "");

481
  if (cache_index >= 0) {
482
    assert(index == _no_index_sentinel, "only one kind of index at a time");
483 484 485
    ConstantPoolCacheEntry* cpc_entry = this_oop->cache()->entry_at(cache_index);
    result_oop = cpc_entry->f1();
    if (result_oop != NULL) {
486 487
      return decode_exception_from_f1(result_oop, THREAD);
      // That was easy...
488 489 490 491
    }
    index = cpc_entry->constant_pool_index();
  }

492 493
  jvalue prim_value;  // temp used only in a few cases below

494 495 496 497 498 499 500 501 502
  int tag_value = this_oop->tag_at(index).value();
  switch (tag_value) {

  case JVM_CONSTANT_UnresolvedClass:
  case JVM_CONSTANT_UnresolvedClassInError:
  case JVM_CONSTANT_Class:
    {
      klassOop resolved = klass_at_impl(this_oop, index, CHECK_NULL);
      // ldc wants the java mirror.
503
      result_oop = resolved->java_mirror();
504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523
      break;
    }

  case JVM_CONSTANT_String:
  case JVM_CONSTANT_UnresolvedString:
    if (this_oop->is_pseudo_string_at(index)) {
      result_oop = this_oop->pseudo_string_at(index);
      break;
    }
    result_oop = string_at_impl(this_oop, index, CHECK_NULL);
    break;

  case JVM_CONSTANT_Object:
    result_oop = this_oop->object_at(index);
    break;

  case JVM_CONSTANT_MethodHandle:
    {
      int ref_kind                 = this_oop->method_handle_ref_kind_at(index);
      int callee_index             = this_oop->method_handle_klass_index_at(index);
524 525
      Symbol*  name =      this_oop->method_handle_name_ref_at(index);
      Symbol*  signature = this_oop->method_handle_signature_ref_at(index);
526 527 528 529 530 531 532 533 534 535 536
      if (PrintMiscellaneous)
        tty->print_cr("resolve JVM_CONSTANT_MethodHandle:%d [%d/%d/%d] %s.%s",
                      ref_kind, index, this_oop->method_handle_index_at(index),
                      callee_index, name->as_C_string(), signature->as_C_string());
      KlassHandle callee;
      { klassOop k = klass_at_impl(this_oop, callee_index, CHECK_NULL);
        callee = KlassHandle(THREAD, k);
      }
      KlassHandle klass(THREAD, this_oop->pool_holder());
      Handle value = SystemDictionary::link_method_handle_constant(klass, ref_kind,
                                                                   callee, name, signature,
537 538 539 540 541 542
                                                                   THREAD);
      if (HAS_PENDING_EXCEPTION) {
        throw_exception = Handle(THREAD, PENDING_EXCEPTION);
        CLEAR_PENDING_EXCEPTION;
        break;
      }
543
      result_oop = value();
544
      assert(result_oop != NULL, "");
545 546 547 548 549
      break;
    }

  case JVM_CONSTANT_MethodType:
    {
550
      Symbol*  signature = this_oop->method_type_signature_at(index);
551 552 553 554 555 556 557 558
      if (PrintMiscellaneous)
        tty->print_cr("resolve JVM_CONSTANT_MethodType [%d/%d] %s",
                      index, this_oop->method_type_index_at(index),
                      signature->as_C_string());
      KlassHandle klass(THREAD, this_oop->pool_holder());
      bool ignore_is_on_bcp = false;
      Handle value = SystemDictionary::find_method_handle_type(signature,
                                                               klass,
559
                                                               false,
560
                                                               ignore_is_on_bcp,
561 562 563 564 565 566
                                                               THREAD);
      if (HAS_PENDING_EXCEPTION) {
        throw_exception = Handle(THREAD, PENDING_EXCEPTION);
        CLEAR_PENDING_EXCEPTION;
        break;
      }
567
      result_oop = value();
568
      assert(result_oop != NULL, "");
569 570 571 572
      break;
    }

  case JVM_CONSTANT_Integer:
573 574 575 576
    prim_value.i = this_oop->int_at(index);
    result_oop = java_lang_boxing_object::create(T_INT, &prim_value, CHECK_NULL);
    break;

577
  case JVM_CONSTANT_Float:
578 579 580 581
    prim_value.f = this_oop->float_at(index);
    result_oop = java_lang_boxing_object::create(T_FLOAT, &prim_value, CHECK_NULL);
    break;

582
  case JVM_CONSTANT_Long:
583 584 585 586
    prim_value.j = this_oop->long_at(index);
    result_oop = java_lang_boxing_object::create(T_LONG, &prim_value, CHECK_NULL);
    break;

587
  case JVM_CONSTANT_Double:
588 589
    prim_value.d = this_oop->double_at(index);
    result_oop = java_lang_boxing_object::create(T_DOUBLE, &prim_value, CHECK_NULL);
590 591 592 593 594 595 596 597 598 599 600
    break;

  default:
    DEBUG_ONLY( tty->print_cr("*** %p: tag at CP[%d/%d] = %d",
                              this_oop(), index, cache_index, tag_value) );
    assert(false, "unexpected constant tag");
    break;
  }

  if (cache_index >= 0) {
    // Cache the oop here also.
601 602 603 604 605 606 607
    if (throw_exception.not_null()) {
      objArrayOop sys_array = oopFactory::new_system_objArray(1, CHECK_NULL);
      sys_array->obj_at_put(0, throw_exception());
      result_oop = sys_array;
      throw_exception = Handle();  // be tidy
    }
    Handle result_handle(THREAD, result_oop);
608 609 610
    result_oop = NULL;  // safety
    ObjectLocker ol(this_oop, THREAD);
    ConstantPoolCacheEntry* cpc_entry = this_oop->cache()->entry_at(cache_index);
611 612 613 614 615 616 617 618 619
    result_oop = cpc_entry->f1();
    // Benign race condition:  f1 may already be filled in while we were trying to lock.
    // The important thing here is that all threads pick up the same result.
    // It doesn't matter which racing thread wins, as long as only one
    // result is used by all threads, and all future queries.
    // That result may be either a resolved constant or a failure exception.
    if (result_oop == NULL) {
      result_oop = result_handle();
      cpc_entry->set_f1(result_oop);
620
    }
621
    return decode_exception_from_f1(result_oop, THREAD);
622
  } else {
623 624 625
    if (throw_exception.not_null()) {
      THROW_HANDLE_(throw_exception, NULL);
    }
626 627 628 629
    return result_oop;
  }
}

D
duke 已提交
630
oop constantPoolOopDesc::string_at_impl(constantPoolHandle this_oop, int which, TRAPS) {
631 632 633
  oop str = NULL;
  CPSlot entry = this_oop->slot_at(which);
  if (entry.is_metadata()) {
D
duke 已提交
634 635 636
    ObjectLocker ol(this_oop, THREAD);
    if (this_oop->tag_at(which).is_unresolved_string()) {
      // Intern string
637 638 639
      Symbol* sym = this_oop->unresolved_string_at(which);
      str = StringTable::intern(sym, CHECK_(constantPoolOop(NULL)));
      this_oop->string_at_put(which, str);
D
duke 已提交
640 641
    } else {
      // Another thread beat us and interned string, read string from constant pool
642
      str = this_oop->resolved_string_at(which);
D
duke 已提交
643
    }
644 645
  } else {
    str = entry.get_oop();
D
duke 已提交
646
  }
647 648
  assert(java_lang_String::is_instance(str), "must be string");
  return str;
D
duke 已提交
649 650 651
}


652
bool constantPoolOopDesc::is_pseudo_string_at(int which) {
653 654
  CPSlot entry = slot_at(which);
  if (entry.is_metadata())
655 656
    // Not yet resolved, but it will resolve to a string.
    return false;
657
  else if (java_lang_String::is_instance(entry.get_oop()))
658 659 660 661 662 663 664
    return false; // actually, it might be a non-interned or non-perm string
  else
    // truly pseudo
    return true;
}


D
duke 已提交
665 666
bool constantPoolOopDesc::klass_name_at_matches(instanceKlassHandle k,
                                                int which) {
667 668
  // Names are interned, so we can compare Symbol*s directly
  Symbol* cp_name = klass_name_at(which);
D
duke 已提交
669 670 671 672 673 674 675 676 677 678
  return (cp_name == k->name());
}


int constantPoolOopDesc::pre_resolve_shared_klasses(TRAPS) {
  ResourceMark rm;
  int count = 0;
  for (int index = 1; index < tags()->length(); index++) { // Index 0 is unused
    if (tag_at(index).is_unresolved_string()) {
      // Intern string
679
      Symbol* sym = unresolved_string_at(index);
D
duke 已提交
680 681 682 683 684 685 686
      oop entry = StringTable::intern(sym, CHECK_(-1));
      string_at_put(index, entry);
    }
  }
  return count;
}

687 688 689 690 691 692 693 694 695 696 697 698 699
// Iterate over symbols and decrement ones which are Symbol*s.
// This is done during GC so do not need to lock constantPool unless we
// have per-thread safepoints.
// Only decrement the UTF8 symbols. Unresolved classes and strings point to
// these symbols but didn't increment the reference count.
void constantPoolOopDesc::unreference_symbols() {
  for (int index = 1; index < length(); index++) { // Index 0 is unused
    constantTag tag = tag_at(index);
    if (tag.is_symbol()) {
      symbol_at(index)->decrement_refcount();
    }
  }
}
D
duke 已提交
700 701 702 703

// Iterate over symbols which are used as class, field, method names and
// signatures (in preparation for writing to the shared archive).

704
void constantPoolOopDesc::shared_symbols_iterate(SymbolClosure* closure) {
D
duke 已提交
705 706 707 708
  for (int index = 1; index < length(); index++) { // Index 0 is unused
    switch (tag_at(index).value()) {

    case JVM_CONSTANT_UnresolvedClass:
709 710 711 712
    case JVM_CONSTANT_UnresolvedString:
    case JVM_CONSTANT_Utf8:
      assert(slot_at(index).is_metadata(), "must be symbol");
      closure->do_symbol(symbol_at_addr(index));
D
duke 已提交
713 714 715 716 717
      break;

    case JVM_CONSTANT_NameAndType:
      {
        int i = *int_at_addr(index);
718 719
        closure->do_symbol(symbol_at_addr((unsigned)i >> 16));
        closure->do_symbol(symbol_at_addr((unsigned)i & 0xffff));
D
duke 已提交
720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755
      }
      break;

    case JVM_CONSTANT_Class:
    case JVM_CONSTANT_InterfaceMethodref:
    case JVM_CONSTANT_Fieldref:
    case JVM_CONSTANT_Methodref:
    case JVM_CONSTANT_Integer:
    case JVM_CONSTANT_Float:
      // Do nothing!  Not an oop.
      // These constant types do not reference symbols at this point.
      break;

    case JVM_CONSTANT_String:
      // Do nothing!  Not a symbol.
      break;

    case JVM_CONSTANT_Long:
    case JVM_CONSTANT_Double:
      // Do nothing!  Not an oop. (But takes two pool entries.)
      ++index;
      break;

    default:
      ShouldNotReachHere();
      break;
    }
  }
}


// Iterate over the [one] tags array (in preparation for writing to the
// shared archive).

void constantPoolOopDesc::shared_tags_iterate(OopClosure* closure) {
  closure->do_oop(tags_addr());
756
  closure->do_oop(operands_addr());
D
duke 已提交
757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782
}


// Iterate over String objects (in preparation for writing to the shared
// archive).

void constantPoolOopDesc::shared_strings_iterate(OopClosure* closure) {
  for (int index = 1; index < length(); index++) { // Index 0 is unused
    switch (tag_at(index).value()) {

    case JVM_CONSTANT_UnresolvedClass:
    case JVM_CONSTANT_NameAndType:
      // Do nothing!  Not a String.
      break;

    case JVM_CONSTANT_Class:
    case JVM_CONSTANT_InterfaceMethodref:
    case JVM_CONSTANT_Fieldref:
    case JVM_CONSTANT_Methodref:
    case JVM_CONSTANT_Integer:
    case JVM_CONSTANT_Float:
      // Do nothing!  Not an oop.
      // These constant types do not reference symbols at this point.
      break;

    case JVM_CONSTANT_String:
783
      closure->do_oop(obj_at_addr_raw(index));
D
duke 已提交
784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942
      break;

    case JVM_CONSTANT_UnresolvedString:
    case JVM_CONSTANT_Utf8:
      // These constants are symbols, but unless these symbols are
      // actually to be used for something, we don't want to mark them.
      break;

    case JVM_CONSTANT_Long:
    case JVM_CONSTANT_Double:
      // Do nothing!  Not an oop. (But takes two pool entries.)
      ++index;
      break;

    default:
      ShouldNotReachHere();
      break;
    }
  }
}


// Compare this constant pool's entry at index1 to the constant pool
// cp2's entry at index2.
bool constantPoolOopDesc::compare_entry_to(int index1, constantPoolHandle cp2,
       int index2, TRAPS) {

  jbyte t1 = tag_at(index1).value();
  jbyte t2 = cp2->tag_at(index2).value();


  // JVM_CONSTANT_UnresolvedClassInError is equal to JVM_CONSTANT_UnresolvedClass
  // when comparing
  if (t1 == JVM_CONSTANT_UnresolvedClassInError) {
    t1 = JVM_CONSTANT_UnresolvedClass;
  }
  if (t2 == JVM_CONSTANT_UnresolvedClassInError) {
    t2 = JVM_CONSTANT_UnresolvedClass;
  }

  if (t1 != t2) {
    // Not the same entry type so there is nothing else to check. Note
    // that this style of checking will consider resolved/unresolved
    // class pairs and resolved/unresolved string pairs as different.
    // From the constantPoolOop API point of view, this is correct
    // behavior. See constantPoolKlass::merge() to see how this plays
    // out in the context of constantPoolOop merging.
    return false;
  }

  switch (t1) {
  case JVM_CONSTANT_Class:
  {
    klassOop k1 = klass_at(index1, CHECK_false);
    klassOop k2 = cp2->klass_at(index2, CHECK_false);
    if (k1 == k2) {
      return true;
    }
  } break;

  case JVM_CONSTANT_ClassIndex:
  {
    int recur1 = klass_index_at(index1);
    int recur2 = cp2->klass_index_at(index2);
    bool match = compare_entry_to(recur1, cp2, recur2, CHECK_false);
    if (match) {
      return true;
    }
  } break;

  case JVM_CONSTANT_Double:
  {
    jdouble d1 = double_at(index1);
    jdouble d2 = cp2->double_at(index2);
    if (d1 == d2) {
      return true;
    }
  } break;

  case JVM_CONSTANT_Fieldref:
  case JVM_CONSTANT_InterfaceMethodref:
  case JVM_CONSTANT_Methodref:
  {
    int recur1 = uncached_klass_ref_index_at(index1);
    int recur2 = cp2->uncached_klass_ref_index_at(index2);
    bool match = compare_entry_to(recur1, cp2, recur2, CHECK_false);
    if (match) {
      recur1 = uncached_name_and_type_ref_index_at(index1);
      recur2 = cp2->uncached_name_and_type_ref_index_at(index2);
      match = compare_entry_to(recur1, cp2, recur2, CHECK_false);
      if (match) {
        return true;
      }
    }
  } break;

  case JVM_CONSTANT_Float:
  {
    jfloat f1 = float_at(index1);
    jfloat f2 = cp2->float_at(index2);
    if (f1 == f2) {
      return true;
    }
  } break;

  case JVM_CONSTANT_Integer:
  {
    jint i1 = int_at(index1);
    jint i2 = cp2->int_at(index2);
    if (i1 == i2) {
      return true;
    }
  } break;

  case JVM_CONSTANT_Long:
  {
    jlong l1 = long_at(index1);
    jlong l2 = cp2->long_at(index2);
    if (l1 == l2) {
      return true;
    }
  } break;

  case JVM_CONSTANT_NameAndType:
  {
    int recur1 = name_ref_index_at(index1);
    int recur2 = cp2->name_ref_index_at(index2);
    bool match = compare_entry_to(recur1, cp2, recur2, CHECK_false);
    if (match) {
      recur1 = signature_ref_index_at(index1);
      recur2 = cp2->signature_ref_index_at(index2);
      match = compare_entry_to(recur1, cp2, recur2, CHECK_false);
      if (match) {
        return true;
      }
    }
  } break;

  case JVM_CONSTANT_String:
  {
    oop s1 = string_at(index1, CHECK_false);
    oop s2 = cp2->string_at(index2, CHECK_false);
    if (s1 == s2) {
      return true;
    }
  } break;

  case JVM_CONSTANT_StringIndex:
  {
    int recur1 = string_index_at(index1);
    int recur2 = cp2->string_index_at(index2);
    bool match = compare_entry_to(recur1, cp2, recur2, CHECK_false);
    if (match) {
      return true;
    }
  } break;

  case JVM_CONSTANT_UnresolvedClass:
  {
943 944
    Symbol* k1 = unresolved_klass_at(index1);
    Symbol* k2 = cp2->unresolved_klass_at(index2);
D
duke 已提交
945 946 947 948 949
    if (k1 == k2) {
      return true;
    }
  } break;

950 951 952 953
  case JVM_CONSTANT_MethodType:
  {
    int k1 = method_type_index_at(index1);
    int k2 = cp2->method_type_index_at(index2);
954 955
    bool match = compare_entry_to(k1, cp2, k2, CHECK_false);
    if (match) {
956 957 958 959 960 961 962 963 964 965 966
      return true;
    }
  } break;

  case JVM_CONSTANT_MethodHandle:
  {
    int k1 = method_handle_ref_kind_at(index1);
    int k2 = cp2->method_handle_ref_kind_at(index2);
    if (k1 == k2) {
      int i1 = method_handle_index_at(index1);
      int i2 = cp2->method_handle_index_at(index2);
967 968
      bool match = compare_entry_to(i1, cp2, i2, CHECK_false);
      if (match) {
969 970 971 972 973
        return true;
      }
    }
  } break;

974 975
  case JVM_CONSTANT_InvokeDynamic:
  {
976 977 978 979 980 981 982 983 984 985 986 987 988 989 990
    int k1 = invoke_dynamic_bootstrap_method_ref_index_at(index1);
    int k2 = cp2->invoke_dynamic_bootstrap_method_ref_index_at(index2);
    bool match = compare_entry_to(k1, cp2, k2, CHECK_false);
    if (!match)  return false;
    k1 = invoke_dynamic_name_and_type_ref_index_at(index1);
    k2 = cp2->invoke_dynamic_name_and_type_ref_index_at(index2);
    match = compare_entry_to(k1, cp2, k2, CHECK_false);
    if (!match)  return false;
    int argc = invoke_dynamic_argument_count_at(index1);
    if (argc == cp2->invoke_dynamic_argument_count_at(index2)) {
      for (int j = 0; j < argc; j++) {
        k1 = invoke_dynamic_argument_index_at(index1, j);
        k2 = cp2->invoke_dynamic_argument_index_at(index2, j);
        match = compare_entry_to(k1, cp2, k2, CHECK_false);
        if (!match)  return false;
991
      }
992
      return true;           // got through loop; all elements equal
993 994 995
    }
  } break;

D
duke 已提交
996 997
  case JVM_CONSTANT_UnresolvedString:
  {
998 999
    Symbol* s1 = unresolved_string_at(index1);
    Symbol* s2 = cp2->unresolved_string_at(index2);
D
duke 已提交
1000 1001 1002 1003 1004 1005 1006
    if (s1 == s2) {
      return true;
    }
  } break;

  case JVM_CONSTANT_Utf8:
  {
1007 1008
    Symbol* s1 = symbol_at(index1);
    Symbol* s2 = cp2->symbol_at(index2);
D
duke 已提交
1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030
    if (s1 == s2) {
      return true;
    }
  } break;

  // Invalid is used as the tag for the second constant pool entry
  // occupied by JVM_CONSTANT_Double or JVM_CONSTANT_Long. It should
  // not be seen by itself.
  case JVM_CONSTANT_Invalid: // fall through

  default:
    ShouldNotReachHere();
    break;
  }

  return false;
} // end compare_entry_to()


// Copy this constant pool's entries at start_i to end_i (inclusive)
// to the constant pool to_cp's entries starting at to_i. A total of
// (end_i - start_i) + 1 entries are copied.
1031
void constantPoolOopDesc::copy_cp_to_impl(constantPoolHandle from_cp, int start_i, int end_i,
D
duke 已提交
1032 1033 1034 1035 1036
       constantPoolHandle to_cp, int to_i, TRAPS) {

  int dest_i = to_i;  // leave original alone for debug purposes

  for (int src_i = start_i; src_i <= end_i; /* see loop bottom */ ) {
1037
    copy_entry_to(from_cp, src_i, to_cp, dest_i, CHECK);
D
duke 已提交
1038

1039
    switch (from_cp->tag_at(src_i).value()) {
D
duke 已提交
1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053
    case JVM_CONSTANT_Double:
    case JVM_CONSTANT_Long:
      // double and long take two constant pool entries
      src_i += 2;
      dest_i += 2;
      break;

    default:
      // all others take one constant pool entry
      src_i++;
      dest_i++;
      break;
    }
  }
1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102

  int from_oplen = operand_array_length(from_cp->operands());
  int old_oplen  = operand_array_length(to_cp->operands());
  if (from_oplen != 0) {
    // append my operands to the target's operands array
    if (old_oplen == 0) {
      to_cp->set_operands(from_cp->operands());  // reuse; do not merge
    } else {
      int old_len  = to_cp->operands()->length();
      int from_len = from_cp->operands()->length();
      int old_off  = old_oplen * sizeof(u2);
      int from_off = from_oplen * sizeof(u2);
      typeArrayHandle new_operands = oopFactory::new_permanent_shortArray(old_len + from_len, CHECK);
      int fillp = 0, len = 0;
      // first part of dest
      Copy::conjoint_memory_atomic(to_cp->operands()->short_at_addr(0),
                                   new_operands->short_at_addr(fillp),
                                   (len = old_off) * sizeof(u2));
      fillp += len;
      // first part of src
      Copy::conjoint_memory_atomic(to_cp->operands()->short_at_addr(0),
                                   new_operands->short_at_addr(fillp),
                                   (len = from_off) * sizeof(u2));
      fillp += len;
      // second part of dest
      Copy::conjoint_memory_atomic(to_cp->operands()->short_at_addr(old_off),
                                   new_operands->short_at_addr(fillp),
                                   (len = old_len - old_off) * sizeof(u2));
      fillp += len;
      // second part of src
      Copy::conjoint_memory_atomic(to_cp->operands()->short_at_addr(from_off),
                                   new_operands->short_at_addr(fillp),
                                   (len = from_len - from_off) * sizeof(u2));
      fillp += len;
      assert(fillp == new_operands->length(), "");

      // Adjust indexes in the first part of the copied operands array.
      for (int j = 0; j < from_oplen; j++) {
        int offset = operand_offset_at(new_operands(), old_oplen + j);
        assert(offset == operand_offset_at(from_cp->operands(), j), "correct copy");
        offset += old_len;  // every new tuple is preceded by old_len extra u2's
        operand_offset_at_put(new_operands(), old_oplen + j, offset);
      }

      // replace target operands array with combined array
      to_cp->set_operands(new_operands());
    }
  }

D
duke 已提交
1103 1104 1105 1106 1107
} // end copy_cp_to()


// Copy this constant pool's entry at from_i to the constant pool
// to_cp's entry at to_i.
1108 1109 1110
void constantPoolOopDesc::copy_entry_to(constantPoolHandle from_cp, int from_i,
                                        constantPoolHandle to_cp, int to_i,
                                        TRAPS) {
D
duke 已提交
1111

1112 1113
  int tag = from_cp->tag_at(from_i).value();
  switch (tag) {
D
duke 已提交
1114 1115
  case JVM_CONSTANT_Class:
  {
1116
    klassOop k = from_cp->klass_at(from_i, CHECK);
D
duke 已提交
1117 1118 1119 1120 1121
    to_cp->klass_at_put(to_i, k);
  } break;

  case JVM_CONSTANT_ClassIndex:
  {
1122
    jint ki = from_cp->klass_index_at(from_i);
D
duke 已提交
1123 1124 1125 1126 1127
    to_cp->klass_index_at_put(to_i, ki);
  } break;

  case JVM_CONSTANT_Double:
  {
1128
    jdouble d = from_cp->double_at(from_i);
D
duke 已提交
1129 1130 1131 1132 1133 1134 1135
    to_cp->double_at_put(to_i, d);
    // double takes two constant pool entries so init second entry's tag
    to_cp->tag_at_put(to_i + 1, JVM_CONSTANT_Invalid);
  } break;

  case JVM_CONSTANT_Fieldref:
  {
1136 1137
    int class_index = from_cp->uncached_klass_ref_index_at(from_i);
    int name_and_type_index = from_cp->uncached_name_and_type_ref_index_at(from_i);
D
duke 已提交
1138 1139 1140 1141 1142
    to_cp->field_at_put(to_i, class_index, name_and_type_index);
  } break;

  case JVM_CONSTANT_Float:
  {
1143
    jfloat f = from_cp->float_at(from_i);
D
duke 已提交
1144 1145 1146 1147 1148
    to_cp->float_at_put(to_i, f);
  } break;

  case JVM_CONSTANT_Integer:
  {
1149
    jint i = from_cp->int_at(from_i);
D
duke 已提交
1150 1151 1152 1153 1154
    to_cp->int_at_put(to_i, i);
  } break;

  case JVM_CONSTANT_InterfaceMethodref:
  {
1155 1156
    int class_index = from_cp->uncached_klass_ref_index_at(from_i);
    int name_and_type_index = from_cp->uncached_name_and_type_ref_index_at(from_i);
D
duke 已提交
1157 1158 1159 1160 1161
    to_cp->interface_method_at_put(to_i, class_index, name_and_type_index);
  } break;

  case JVM_CONSTANT_Long:
  {
1162
    jlong l = from_cp->long_at(from_i);
D
duke 已提交
1163 1164 1165 1166 1167 1168 1169
    to_cp->long_at_put(to_i, l);
    // long takes two constant pool entries so init second entry's tag
    to_cp->tag_at_put(to_i + 1, JVM_CONSTANT_Invalid);
  } break;

  case JVM_CONSTANT_Methodref:
  {
1170 1171
    int class_index = from_cp->uncached_klass_ref_index_at(from_i);
    int name_and_type_index = from_cp->uncached_name_and_type_ref_index_at(from_i);
D
duke 已提交
1172 1173 1174 1175 1176
    to_cp->method_at_put(to_i, class_index, name_and_type_index);
  } break;

  case JVM_CONSTANT_NameAndType:
  {
1177 1178
    int name_ref_index = from_cp->name_ref_index_at(from_i);
    int signature_ref_index = from_cp->signature_ref_index_at(from_i);
D
duke 已提交
1179 1180 1181 1182 1183
    to_cp->name_and_type_at_put(to_i, name_ref_index, signature_ref_index);
  } break;

  case JVM_CONSTANT_String:
  {
1184
    oop s = from_cp->string_at(from_i, CHECK);
D
duke 已提交
1185 1186 1187 1188 1189
    to_cp->string_at_put(to_i, s);
  } break;

  case JVM_CONSTANT_StringIndex:
  {
1190
    jint si = from_cp->string_index_at(from_i);
D
duke 已提交
1191 1192 1193 1194 1195
    to_cp->string_index_at_put(to_i, si);
  } break;

  case JVM_CONSTANT_UnresolvedClass:
  {
1196 1197 1198 1199 1200 1201 1202 1203 1204
    // Can be resolved after checking tag, so check the slot first.
    CPSlot entry = from_cp->slot_at(from_i);
    if (entry.is_oop()) {
      assert(entry.get_oop()->is_klass(), "must be");
      // Already resolved
      to_cp->klass_at_put(to_i, (klassOop)entry.get_oop());
    } else {
      to_cp->unresolved_klass_at_put(to_i, entry.get_symbol());
    }
D
duke 已提交
1205 1206 1207 1208
  } break;

  case JVM_CONSTANT_UnresolvedClassInError:
  {
1209
    Symbol* k = from_cp->unresolved_klass_at(from_i);
D
duke 已提交
1210 1211 1212 1213 1214 1215 1216
    to_cp->unresolved_klass_at_put(to_i, k);
    to_cp->tag_at_put(to_i, JVM_CONSTANT_UnresolvedClassInError);
  } break;


  case JVM_CONSTANT_UnresolvedString:
  {
1217 1218 1219 1220 1221 1222 1223 1224
    // Can be resolved after checking tag, so check the slot first.
    CPSlot entry = from_cp->slot_at(from_i);
    if (entry.is_oop()) {
      // Already resolved (either string or pseudo-string)
      to_cp->string_at_put(to_i, entry.get_oop());
    } else {
      to_cp->unresolved_string_at_put(to_i, entry.get_symbol());
    }
D
duke 已提交
1225 1226 1227 1228
  } break;

  case JVM_CONSTANT_Utf8:
  {
1229
    Symbol* s = from_cp->symbol_at(from_i);
D
duke 已提交
1230
    to_cp->symbol_at_put(to_i, s);
1231 1232
    // This constantPool has the same lifetime as the original, so don't
    // increase reference counts for the copy.
D
duke 已提交
1233 1234
  } break;

1235 1236
  case JVM_CONSTANT_MethodType:
  {
1237
    jint k = from_cp->method_type_index_at(from_i);
1238 1239 1240 1241 1242
    to_cp->method_type_index_at_put(to_i, k);
  } break;

  case JVM_CONSTANT_MethodHandle:
  {
1243 1244
    int k1 = from_cp->method_handle_ref_kind_at(from_i);
    int k2 = from_cp->method_handle_index_at(from_i);
1245 1246 1247
    to_cp->method_handle_index_at_put(to_i, k1, k2);
  } break;

1248 1249
  case JVM_CONSTANT_InvokeDynamic:
  {
1250 1251 1252 1253
    int k1 = from_cp->invoke_dynamic_bootstrap_specifier_index(from_i);
    int k2 = from_cp->invoke_dynamic_name_and_type_ref_index_at(from_i);
    k1 += operand_array_length(to_cp->operands());  // to_cp might already have operands
    to_cp->invoke_dynamic_at_put(to_i, k1, k2);
1254 1255
  } break;

D
duke 已提交
1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357
  // Invalid is used as the tag for the second constant pool entry
  // occupied by JVM_CONSTANT_Double or JVM_CONSTANT_Long. It should
  // not be seen by itself.
  case JVM_CONSTANT_Invalid: // fall through

  default:
  {
    ShouldNotReachHere();
  } break;
  }
} // end copy_entry_to()


// Search constant pool search_cp for an entry that matches this
// constant pool's entry at pattern_i. Returns the index of a
// matching entry or zero (0) if there is no matching entry.
int constantPoolOopDesc::find_matching_entry(int pattern_i,
      constantPoolHandle search_cp, TRAPS) {

  // index zero (0) is not used
  for (int i = 1; i < search_cp->length(); i++) {
    bool found = compare_entry_to(pattern_i, search_cp, i, CHECK_0);
    if (found) {
      return i;
    }
  }

  return 0;  // entry not found; return unused index zero (0)
} // end find_matching_entry()


#ifndef PRODUCT

const char* constantPoolOopDesc::printable_name_at(int which) {

  constantTag tag = tag_at(which);

  if (tag.is_unresolved_string() || tag.is_string()) {
    return string_at_noresolve(which);
  } else if (tag.is_klass() || tag.is_unresolved_klass()) {
    return klass_name_at(which)->as_C_string();
  } else if (tag.is_symbol()) {
    return symbol_at(which)->as_C_string();
  }
  return "";
}

#endif // PRODUCT


// JVMTI GetConstantPool support

// For temporary use until code is stable.
#define DBG(code)

static const char* WARN_MSG = "Must not be such entry!";

static void print_cpool_bytes(jint cnt, u1 *bytes) {
  jint size = 0;
  u2   idx1, idx2;

  for (jint idx = 1; idx < cnt; idx++) {
    jint ent_size = 0;
    u1   tag  = *bytes++;
    size++;                       // count tag

    printf("const #%03d, tag: %02d ", idx, tag);
    switch(tag) {
      case JVM_CONSTANT_Invalid: {
        printf("Invalid");
        break;
      }
      case JVM_CONSTANT_Unicode: {
        printf("Unicode      %s", WARN_MSG);
        break;
      }
      case JVM_CONSTANT_Utf8: {
        u2 len = Bytes::get_Java_u2(bytes);
        char str[128];
        if (len > 127) {
           len = 127;
        }
        strncpy(str, (char *) (bytes+2), len);
        str[len] = '\0';
        printf("Utf8          \"%s\"", str);
        ent_size = 2 + len;
        break;
      }
      case JVM_CONSTANT_Integer: {
        u4 val = Bytes::get_Java_u4(bytes);
        printf("int          %d", *(int *) &val);
        ent_size = 4;
        break;
      }
      case JVM_CONSTANT_Float: {
        u4 val = Bytes::get_Java_u4(bytes);
        printf("float        %5.3ff", *(float *) &val);
        ent_size = 4;
        break;
      }
      case JVM_CONSTANT_Long: {
        u8 val = Bytes::get_Java_u8(bytes);
N
never 已提交
1358
        printf("long         "INT64_FORMAT, (int64_t) *(jlong *) &val);
D
duke 已提交
1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457
        ent_size = 8;
        idx++; // Long takes two cpool slots
        break;
      }
      case JVM_CONSTANT_Double: {
        u8 val = Bytes::get_Java_u8(bytes);
        printf("double       %5.3fd", *(jdouble *)&val);
        ent_size = 8;
        idx++; // Double takes two cpool slots
        break;
      }
      case JVM_CONSTANT_Class: {
        idx1 = Bytes::get_Java_u2(bytes);
        printf("class        #%03d", idx1);
        ent_size = 2;
        break;
      }
      case JVM_CONSTANT_String: {
        idx1 = Bytes::get_Java_u2(bytes);
        printf("String       #%03d", idx1);
        ent_size = 2;
        break;
      }
      case JVM_CONSTANT_Fieldref: {
        idx1 = Bytes::get_Java_u2(bytes);
        idx2 = Bytes::get_Java_u2(bytes+2);
        printf("Field        #%03d, #%03d", (int) idx1, (int) idx2);
        ent_size = 4;
        break;
      }
      case JVM_CONSTANT_Methodref: {
        idx1 = Bytes::get_Java_u2(bytes);
        idx2 = Bytes::get_Java_u2(bytes+2);
        printf("Method       #%03d, #%03d", idx1, idx2);
        ent_size = 4;
        break;
      }
      case JVM_CONSTANT_InterfaceMethodref: {
        idx1 = Bytes::get_Java_u2(bytes);
        idx2 = Bytes::get_Java_u2(bytes+2);
        printf("InterfMethod #%03d, #%03d", idx1, idx2);
        ent_size = 4;
        break;
      }
      case JVM_CONSTANT_NameAndType: {
        idx1 = Bytes::get_Java_u2(bytes);
        idx2 = Bytes::get_Java_u2(bytes+2);
        printf("NameAndType  #%03d, #%03d", idx1, idx2);
        ent_size = 4;
        break;
      }
      case JVM_CONSTANT_ClassIndex: {
        printf("ClassIndex  %s", WARN_MSG);
        break;
      }
      case JVM_CONSTANT_UnresolvedClass: {
        printf("UnresolvedClass: %s", WARN_MSG);
        break;
      }
      case JVM_CONSTANT_UnresolvedClassInError: {
        printf("UnresolvedClassInErr: %s", WARN_MSG);
        break;
      }
      case JVM_CONSTANT_StringIndex: {
        printf("StringIndex: %s", WARN_MSG);
        break;
      }
      case JVM_CONSTANT_UnresolvedString: {
        printf("UnresolvedString: %s", WARN_MSG);
        break;
      }
    }
    printf(";\n");
    bytes += ent_size;
    size  += ent_size;
  }
  printf("Cpool size: %d\n", size);
  fflush(0);
  return;
} /* end print_cpool_bytes */


// Returns size of constant pool entry.
jint constantPoolOopDesc::cpool_entry_size(jint idx) {
  switch(tag_at(idx).value()) {
    case JVM_CONSTANT_Invalid:
    case JVM_CONSTANT_Unicode:
      return 1;

    case JVM_CONSTANT_Utf8:
      return 3 + symbol_at(idx)->utf8_length();

    case JVM_CONSTANT_Class:
    case JVM_CONSTANT_String:
    case JVM_CONSTANT_ClassIndex:
    case JVM_CONSTANT_UnresolvedClass:
    case JVM_CONSTANT_UnresolvedClassInError:
    case JVM_CONSTANT_StringIndex:
    case JVM_CONSTANT_UnresolvedString:
1458
    case JVM_CONSTANT_MethodType:
D
duke 已提交
1459 1460
      return 3;

1461 1462 1463
    case JVM_CONSTANT_MethodHandle:
      return 4; //tag, ref_kind, ref_index

D
duke 已提交
1464 1465 1466 1467 1468 1469 1470 1471
    case JVM_CONSTANT_Integer:
    case JVM_CONSTANT_Float:
    case JVM_CONSTANT_Fieldref:
    case JVM_CONSTANT_Methodref:
    case JVM_CONSTANT_InterfaceMethodref:
    case JVM_CONSTANT_NameAndType:
      return 5;

1472
    case JVM_CONSTANT_InvokeDynamic:
1473 1474
      // u1 tag, u2 bsm, u2 nt
      return 5;
1475

D
duke 已提交
1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497
    case JVM_CONSTANT_Long:
    case JVM_CONSTANT_Double:
      return 9;
  }
  assert(false, "cpool_entry_size: Invalid constant pool entry tag");
  return 1;
} /* end cpool_entry_size */


// SymbolHashMap is used to find a constant pool index from a string.
// This function fills in SymbolHashMaps, one for utf8s and one for
// class names, returns size of the cpool raw bytes.
jint constantPoolOopDesc::hash_entries_to(SymbolHashMap *symmap,
                                          SymbolHashMap *classmap) {
  jint size = 0;

  for (u2 idx = 1; idx < length(); idx++) {
    u2 tag = tag_at(idx).value();
    size += cpool_entry_size(idx);

    switch(tag) {
      case JVM_CONSTANT_Utf8: {
1498
        Symbol* sym = symbol_at(idx);
D
duke 已提交
1499 1500 1501 1502 1503 1504 1505
        symmap->add_entry(sym, idx);
        DBG(printf("adding symbol entry %s = %d\n", sym->as_utf8(), idx));
        break;
      }
      case JVM_CONSTANT_Class:
      case JVM_CONSTANT_UnresolvedClass:
      case JVM_CONSTANT_UnresolvedClassInError: {
1506
        Symbol* sym = klass_name_at(idx);
D
duke 已提交
1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553
        classmap->add_entry(sym, idx);
        DBG(printf("adding class entry %s = %d\n", sym->as_utf8(), idx));
        break;
      }
      case JVM_CONSTANT_Long:
      case JVM_CONSTANT_Double: {
        idx++; // Both Long and Double take two cpool slots
        break;
      }
    }
  }
  return size;
} /* end hash_utf8_entries_to */


// Copy cpool bytes.
// Returns:
//    0, in case of OutOfMemoryError
//   -1, in case of internal error
//  > 0, count of the raw cpool bytes that have been copied
int constantPoolOopDesc::copy_cpool_bytes(int cpool_size,
                                          SymbolHashMap* tbl,
                                          unsigned char *bytes) {
  u2   idx1, idx2;
  jint size  = 0;
  jint cnt   = length();
  unsigned char *start_bytes = bytes;

  for (jint idx = 1; idx < cnt; idx++) {
    u1   tag      = tag_at(idx).value();
    jint ent_size = cpool_entry_size(idx);

    assert(size + ent_size <= cpool_size, "Size mismatch");

    *bytes = tag;
    DBG(printf("#%03hd tag=%03hd, ", idx, tag));
    switch(tag) {
      case JVM_CONSTANT_Invalid: {
        DBG(printf("JVM_CONSTANT_Invalid"));
        break;
      }
      case JVM_CONSTANT_Unicode: {
        assert(false, "Wrong constant pool tag: JVM_CONSTANT_Unicode");
        DBG(printf("JVM_CONSTANT_Unicode"));
        break;
      }
      case JVM_CONSTANT_Utf8: {
1554
        Symbol* sym = symbol_at(idx);
D
duke 已提交
1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590
        char*     str = sym->as_utf8();
        // Warning! It's crashing on x86 with len = sym->utf8_length()
        int       len = (int) strlen(str);
        Bytes::put_Java_u2((address) (bytes+1), (u2) len);
        for (int i = 0; i < len; i++) {
            bytes[3+i] = (u1) str[i];
        }
        DBG(printf("JVM_CONSTANT_Utf8: %s ", str));
        break;
      }
      case JVM_CONSTANT_Integer: {
        jint val = int_at(idx);
        Bytes::put_Java_u4((address) (bytes+1), *(u4*)&val);
        break;
      }
      case JVM_CONSTANT_Float: {
        jfloat val = float_at(idx);
        Bytes::put_Java_u4((address) (bytes+1), *(u4*)&val);
        break;
      }
      case JVM_CONSTANT_Long: {
        jlong val = long_at(idx);
        Bytes::put_Java_u8((address) (bytes+1), *(u8*)&val);
        idx++;             // Long takes two cpool slots
        break;
      }
      case JVM_CONSTANT_Double: {
        jdouble val = double_at(idx);
        Bytes::put_Java_u8((address) (bytes+1), *(u8*)&val);
        idx++;             // Double takes two cpool slots
        break;
      }
      case JVM_CONSTANT_Class:
      case JVM_CONSTANT_UnresolvedClass:
      case JVM_CONSTANT_UnresolvedClassInError: {
        *bytes = JVM_CONSTANT_Class;
1591
        Symbol* sym = klass_name_at(idx);
D
duke 已提交
1592 1593 1594 1595 1596 1597 1598 1599 1600
        idx1 = tbl->symbol_to_value(sym);
        assert(idx1 != 0, "Have not found a hashtable entry");
        Bytes::put_Java_u2((address) (bytes+1), idx1);
        DBG(printf("JVM_CONSTANT_Class: idx=#%03hd, %s", idx1, sym->as_utf8()));
        break;
      }
      case JVM_CONSTANT_String: {
        unsigned int hash;
        char *str = string_at_noresolve(idx);
1601
        TempNewSymbol sym = SymbolTable::lookup_only(str, (int) strlen(str), hash);
1602 1603 1604
        if (sym == NULL) {
          // sym can be NULL if string refers to incorrectly encoded JVM_CONSTANT_Utf8
          // this can happen with JVM TI; see CR 6839599 for more details
1605
          oop string = *(obj_at_addr_raw(idx));
1606 1607 1608 1609 1610 1611
          assert(java_lang_String::is_instance(string),"Not a String");
          DBG(printf("Error #%03hd tag=%03hd\n", idx, tag));
          idx1 = 0;
          for (int j = 0; j < tbl->table_size() && idx1 == 0; j++) {
            for (SymbolHashMapEntry* cur = tbl->bucket(j); cur != NULL; cur = cur->next()) {
              int length;
1612 1613
              Symbol* s = cur->symbol();
              jchar* chars = s->as_unicode(length);
1614 1615 1616 1617 1618 1619 1620 1621 1622 1623
              if (java_lang_String::equals(string, chars, length)) {
                idx1 = cur->value();
                DBG(printf("Index found: %d\n",idx1));
                break;
              }
            }
          }
        } else {
          idx1 = tbl->symbol_to_value(sym);
        }
D
duke 已提交
1624 1625 1626 1627 1628 1629 1630
        assert(idx1 != 0, "Have not found a hashtable entry");
        Bytes::put_Java_u2((address) (bytes+1), idx1);
        DBG(printf("JVM_CONSTANT_String: idx=#%03hd, %s", idx1, str));
        break;
      }
      case JVM_CONSTANT_UnresolvedString: {
        *bytes = JVM_CONSTANT_String;
1631
        Symbol* sym = unresolved_string_at(idx);
D
duke 已提交
1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670
        idx1 = tbl->symbol_to_value(sym);
        assert(idx1 != 0, "Have not found a hashtable entry");
        Bytes::put_Java_u2((address) (bytes+1), idx1);
        DBG(char *str = sym->as_utf8());
        DBG(printf("JVM_CONSTANT_UnresolvedString: idx=#%03hd, %s", idx1, str));
        break;
      }
      case JVM_CONSTANT_Fieldref:
      case JVM_CONSTANT_Methodref:
      case JVM_CONSTANT_InterfaceMethodref: {
        idx1 = uncached_klass_ref_index_at(idx);
        idx2 = uncached_name_and_type_ref_index_at(idx);
        Bytes::put_Java_u2((address) (bytes+1), idx1);
        Bytes::put_Java_u2((address) (bytes+3), idx2);
        DBG(printf("JVM_CONSTANT_Methodref: %hd %hd", idx1, idx2));
        break;
      }
      case JVM_CONSTANT_NameAndType: {
        idx1 = name_ref_index_at(idx);
        idx2 = signature_ref_index_at(idx);
        Bytes::put_Java_u2((address) (bytes+1), idx1);
        Bytes::put_Java_u2((address) (bytes+3), idx2);
        DBG(printf("JVM_CONSTANT_NameAndType: %hd %hd", idx1, idx2));
        break;
      }
      case JVM_CONSTANT_ClassIndex: {
        *bytes = JVM_CONSTANT_Class;
        idx1 = klass_index_at(idx);
        Bytes::put_Java_u2((address) (bytes+1), idx1);
        DBG(printf("JVM_CONSTANT_ClassIndex: %hd", idx1));
        break;
      }
      case JVM_CONSTANT_StringIndex: {
        *bytes = JVM_CONSTANT_String;
        idx1 = string_index_at(idx);
        Bytes::put_Java_u2((address) (bytes+1), idx1);
        DBG(printf("JVM_CONSTANT_StringIndex: %hd", idx1));
        break;
      }
1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686
      case JVM_CONSTANT_MethodHandle: {
        *bytes = JVM_CONSTANT_MethodHandle;
        int kind = method_handle_ref_kind_at(idx);
        idx1 = method_handle_index_at(idx);
        *(bytes+1) = (unsigned char) kind;
        Bytes::put_Java_u2((address) (bytes+2), idx1);
        DBG(printf("JVM_CONSTANT_MethodHandle: %d %hd", kind, idx1));
        break;
      }
      case JVM_CONSTANT_MethodType: {
        *bytes = JVM_CONSTANT_MethodType;
        idx1 = method_type_index_at(idx);
        Bytes::put_Java_u2((address) (bytes+1), idx1);
        DBG(printf("JVM_CONSTANT_MethodType: %hd", idx1));
        break;
      }
1687
      case JVM_CONSTANT_InvokeDynamic: {
1688 1689 1690 1691
        *bytes = tag;
        idx1 = extract_low_short_from_int(*int_at_addr(idx));
        idx2 = extract_high_short_from_int(*int_at_addr(idx));
        assert(idx2 == invoke_dynamic_name_and_type_ref_index_at(idx), "correct half of u4");
1692 1693
        Bytes::put_Java_u2((address) (bytes+1), idx1);
        Bytes::put_Java_u2((address) (bytes+3), idx2);
1694
        DBG(printf("JVM_CONSTANT_InvokeDynamic: %hd %hd", idx1, idx2));
1695 1696
        break;
      }
D
duke 已提交
1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709
    }
    DBG(printf("\n"));
    bytes += ent_size;
    size  += ent_size;
  }
  assert(size == cpool_size, "Size mismatch");

  // Keep temorarily for debugging until it's stable.
  DBG(print_cpool_bytes(cnt, start_bytes));
  return (int)(bytes - start_bytes);
} /* end copy_cpool_bytes */


1710
void SymbolHashMap::add_entry(Symbol* sym, u2 value) {
D
duke 已提交
1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730
  char *str = sym->as_utf8();
  unsigned int hash = compute_hash(str, sym->utf8_length());
  unsigned int index = hash % table_size();

  // check if already in map
  // we prefer the first entry since it is more likely to be what was used in
  // the class file
  for (SymbolHashMapEntry *en = bucket(index); en != NULL; en = en->next()) {
    assert(en->symbol() != NULL, "SymbolHashMapEntry symbol is NULL");
    if (en->hash() == hash && en->symbol() == sym) {
        return;  // already there
    }
  }

  SymbolHashMapEntry* entry = new SymbolHashMapEntry(hash, sym, value);
  entry->set_next(bucket(index));
  _buckets[index].set_entry(entry);
  assert(entry->symbol() != NULL, "SymbolHashMapEntry symbol is NULL");
}

1731
SymbolHashMapEntry* SymbolHashMap::find_entry(Symbol* sym) {
D
duke 已提交
1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744
  assert(sym != NULL, "SymbolHashMap::find_entry - symbol is NULL");
  char *str = sym->as_utf8();
  int   len = sym->utf8_length();
  unsigned int hash = SymbolHashMap::compute_hash(str, len);
  unsigned int index = hash % table_size();
  for (SymbolHashMapEntry *en = bucket(index); en != NULL; en = en->next()) {
    assert(en->symbol() != NULL, "SymbolHashMapEntry symbol is NULL");
    if (en->hash() == hash && en->symbol() == sym) {
      return en;
    }
  }
  return NULL;
}