annotate src/share/vm/oops/constantPoolOop.cpp @ 2231:c7f3d0b4570f

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