annotate src/share/vm/oops/constantPoolOop.cpp @ 1924:dad31fc330cd

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