annotate src/share/vm/oops/constantPoolOop.cpp @ 1838:3b2dea75431e

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