annotate src/share/vm/oops/constantPoolOop.cpp @ 1885:f95d63e2154a

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