annotate src/share/vm/oops/constantPoolOop.cpp @ 3067:f457154eee8b

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