src/share/vm/oops/constantPoolOop.cpp

Thu, 13 Jan 2011 22:15:41 -0800

author
never
date
Thu, 13 Jan 2011 22:15:41 -0800
changeset 2462
8012aa3ccede
parent 2353
dad31fc330cd
child 2497
3582bf76420e
permissions
-rw-r--r--

4926272: methodOopDesc::method_from_bcp is unsafe
Reviewed-by: coleenp, jrose, kvn, dcubed

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

mercurial