src/share/vm/oops/constantPool.cpp

Thu, 12 Oct 2017 21:27:07 +0800

author
aoqi
date
Thu, 12 Oct 2017 21:27:07 +0800
changeset 7535
7ae4e26cb1e0
parent 7333
b12a2a9b05ca
parent 6876
710a3c8b516e
child 9448
73d689add964
permissions
-rw-r--r--

merge

aoqi@0 1 /*
aoqi@0 2 * Copyright (c) 1997, 2014, Oracle and/or its affiliates. All rights reserved.
aoqi@0 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
aoqi@0 4 *
aoqi@0 5 * This code is free software; you can redistribute it and/or modify it
aoqi@0 6 * under the terms of the GNU General Public License version 2 only, as
aoqi@0 7 * published by the Free Software Foundation.
aoqi@0 8 *
aoqi@0 9 * This code is distributed in the hope that it will be useful, but WITHOUT
aoqi@0 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
aoqi@0 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
aoqi@0 12 * version 2 for more details (a copy is included in the LICENSE file that
aoqi@0 13 * accompanied this code).
aoqi@0 14 *
aoqi@0 15 * You should have received a copy of the GNU General Public License version
aoqi@0 16 * 2 along with this work; if not, write to the Free Software Foundation,
aoqi@0 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
aoqi@0 18 *
aoqi@0 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
aoqi@0 20 * or visit www.oracle.com if you need additional information or have any
aoqi@0 21 * questions.
aoqi@0 22 *
aoqi@0 23 */
aoqi@0 24
aoqi@0 25 #include "precompiled.hpp"
aoqi@0 26 #include "classfile/classLoaderData.hpp"
aoqi@0 27 #include "classfile/javaClasses.hpp"
aoqi@0 28 #include "classfile/metadataOnStackMark.hpp"
aoqi@0 29 #include "classfile/symbolTable.hpp"
aoqi@0 30 #include "classfile/systemDictionary.hpp"
aoqi@0 31 #include "classfile/vmSymbols.hpp"
aoqi@0 32 #include "interpreter/linkResolver.hpp"
aoqi@0 33 #include "memory/heapInspection.hpp"
aoqi@0 34 #include "memory/metadataFactory.hpp"
aoqi@0 35 #include "memory/oopFactory.hpp"
aoqi@0 36 #include "oops/constantPool.hpp"
aoqi@0 37 #include "oops/instanceKlass.hpp"
aoqi@0 38 #include "oops/objArrayKlass.hpp"
aoqi@0 39 #include "runtime/fieldType.hpp"
aoqi@0 40 #include "runtime/init.hpp"
aoqi@0 41 #include "runtime/javaCalls.hpp"
aoqi@0 42 #include "runtime/signature.hpp"
aoqi@0 43 #include "runtime/vframe.hpp"
aoqi@0 44
aoqi@0 45 PRAGMA_FORMAT_MUTE_WARNINGS_FOR_GCC
aoqi@0 46
aoqi@0 47 ConstantPool* ConstantPool::allocate(ClassLoaderData* loader_data, int length, TRAPS) {
aoqi@0 48 // Tags are RW but comment below applies to tags also.
aoqi@0 49 Array<u1>* tags = MetadataFactory::new_writeable_array<u1>(loader_data, length, 0, CHECK_NULL);
aoqi@0 50
aoqi@0 51 int size = ConstantPool::size(length);
aoqi@0 52
aoqi@0 53 // CDS considerations:
aoqi@0 54 // Allocate read-write but may be able to move to read-only at dumping time
aoqi@0 55 // if all the klasses are resolved. The only other field that is writable is
aoqi@0 56 // the resolved_references array, which is recreated at startup time.
aoqi@0 57 // But that could be moved to InstanceKlass (although a pain to access from
aoqi@0 58 // assembly code). Maybe it could be moved to the cpCache which is RW.
aoqi@0 59 return new (loader_data, size, false, MetaspaceObj::ConstantPoolType, THREAD) ConstantPool(tags);
aoqi@0 60 }
aoqi@0 61
aoqi@0 62 ConstantPool::ConstantPool(Array<u1>* tags) {
aoqi@0 63 set_length(tags->length());
aoqi@0 64 set_tags(NULL);
aoqi@0 65 set_cache(NULL);
aoqi@0 66 set_reference_map(NULL);
aoqi@0 67 set_resolved_references(NULL);
aoqi@0 68 set_operands(NULL);
aoqi@0 69 set_pool_holder(NULL);
aoqi@0 70 set_flags(0);
aoqi@0 71
aoqi@0 72 // only set to non-zero if constant pool is merged by RedefineClasses
aoqi@0 73 set_version(0);
aoqi@0 74 set_lock(new Monitor(Monitor::nonleaf + 2, "A constant pool lock"));
aoqi@0 75
aoqi@0 76 // initialize tag array
aoqi@0 77 int length = tags->length();
aoqi@0 78 for (int index = 0; index < length; index++) {
aoqi@0 79 tags->at_put(index, JVM_CONSTANT_Invalid);
aoqi@0 80 }
aoqi@0 81 set_tags(tags);
aoqi@0 82 }
aoqi@0 83
aoqi@0 84 void ConstantPool::deallocate_contents(ClassLoaderData* loader_data) {
aoqi@0 85 MetadataFactory::free_metadata(loader_data, cache());
aoqi@0 86 set_cache(NULL);
aoqi@0 87 MetadataFactory::free_array<u2>(loader_data, reference_map());
aoqi@0 88 set_reference_map(NULL);
aoqi@0 89
aoqi@0 90 MetadataFactory::free_array<jushort>(loader_data, operands());
aoqi@0 91 set_operands(NULL);
aoqi@0 92
aoqi@0 93 release_C_heap_structures();
aoqi@0 94
aoqi@0 95 // free tag array
aoqi@0 96 MetadataFactory::free_array<u1>(loader_data, tags());
aoqi@0 97 set_tags(NULL);
aoqi@0 98 }
aoqi@0 99
aoqi@0 100 void ConstantPool::release_C_heap_structures() {
aoqi@0 101 // walk constant pool and decrement symbol reference counts
aoqi@0 102 unreference_symbols();
aoqi@0 103
aoqi@0 104 delete _lock;
aoqi@0 105 set_lock(NULL);
aoqi@0 106 }
aoqi@0 107
aoqi@0 108 objArrayOop ConstantPool::resolved_references() const {
aoqi@0 109 return (objArrayOop)JNIHandles::resolve(_resolved_references);
aoqi@0 110 }
aoqi@0 111
aoqi@0 112 // Create resolved_references array and mapping array for original cp indexes
aoqi@0 113 // The ldc bytecode was rewritten to have the resolved reference array index so need a way
aoqi@0 114 // to map it back for resolving and some unlikely miscellaneous uses.
aoqi@0 115 // The objects created by invokedynamic are appended to this list.
aoqi@0 116 void ConstantPool::initialize_resolved_references(ClassLoaderData* loader_data,
aoqi@0 117 intStack reference_map,
aoqi@0 118 int constant_pool_map_length,
aoqi@0 119 TRAPS) {
aoqi@0 120 // Initialized the resolved object cache.
aoqi@0 121 int map_length = reference_map.length();
aoqi@0 122 if (map_length > 0) {
aoqi@0 123 // Only need mapping back to constant pool entries. The map isn't used for
aoqi@0 124 // invokedynamic resolved_reference entries. For invokedynamic entries,
aoqi@0 125 // the constant pool cache index has the mapping back to both the constant
aoqi@0 126 // pool and to the resolved reference index.
aoqi@0 127 if (constant_pool_map_length > 0) {
aoqi@0 128 Array<u2>* om = MetadataFactory::new_array<u2>(loader_data, constant_pool_map_length, CHECK);
aoqi@0 129
aoqi@0 130 for (int i = 0; i < constant_pool_map_length; i++) {
aoqi@0 131 int x = reference_map.at(i);
aoqi@0 132 assert(x == (int)(jushort) x, "klass index is too big");
aoqi@0 133 om->at_put(i, (jushort)x);
aoqi@0 134 }
aoqi@0 135 set_reference_map(om);
aoqi@0 136 }
aoqi@0 137
aoqi@0 138 // Create Java array for holding resolved strings, methodHandles,
aoqi@0 139 // methodTypes, invokedynamic and invokehandle appendix objects, etc.
aoqi@0 140 objArrayOop stom = oopFactory::new_objArray(SystemDictionary::Object_klass(), map_length, CHECK);
aoqi@0 141 Handle refs_handle (THREAD, (oop)stom); // must handleize.
aoqi@0 142 set_resolved_references(loader_data->add_handle(refs_handle));
aoqi@0 143 }
aoqi@0 144 }
aoqi@0 145
aoqi@0 146 // CDS support. Create a new resolved_references array.
aoqi@0 147 void ConstantPool::restore_unshareable_info(TRAPS) {
aoqi@0 148
aoqi@0 149 // Only create the new resolved references array and lock if it hasn't been
aoqi@0 150 // attempted before
aoqi@0 151 if (resolved_references() != NULL) return;
aoqi@0 152
aoqi@0 153 // restore the C++ vtable from the shared archive
aoqi@0 154 restore_vtable();
aoqi@0 155
aoqi@0 156 if (SystemDictionary::Object_klass_loaded()) {
aoqi@0 157 // Recreate the object array and add to ClassLoaderData.
aoqi@0 158 int map_length = resolved_reference_length();
aoqi@0 159 if (map_length > 0) {
aoqi@0 160 objArrayOop stom = oopFactory::new_objArray(SystemDictionary::Object_klass(), map_length, CHECK);
aoqi@0 161 Handle refs_handle (THREAD, (oop)stom); // must handleize.
aoqi@0 162
aoqi@0 163 ClassLoaderData* loader_data = pool_holder()->class_loader_data();
aoqi@0 164 set_resolved_references(loader_data->add_handle(refs_handle));
aoqi@0 165 }
aoqi@0 166
aoqi@0 167 // Also need to recreate the mutex. Make sure this matches the constructor
aoqi@0 168 set_lock(new Monitor(Monitor::nonleaf + 2, "A constant pool lock"));
aoqi@0 169 }
aoqi@0 170 }
aoqi@0 171
aoqi@0 172 void ConstantPool::remove_unshareable_info() {
aoqi@0 173 // Resolved references are not in the shared archive.
aoqi@0 174 // Save the length for restoration. It is not necessarily the same length
aoqi@0 175 // as reference_map.length() if invokedynamic is saved.
aoqi@0 176 set_resolved_reference_length(
aoqi@0 177 resolved_references() != NULL ? resolved_references()->length() : 0);
aoqi@0 178 set_resolved_references(NULL);
aoqi@0 179 set_lock(NULL);
aoqi@0 180 }
aoqi@0 181
aoqi@0 182 int ConstantPool::cp_to_object_index(int cp_index) {
aoqi@0 183 // this is harder don't do this so much.
aoqi@0 184 int i = reference_map()->find(cp_index);
aoqi@0 185 // We might not find the index for jsr292 call.
aoqi@0 186 return (i < 0) ? _no_index_sentinel : i;
aoqi@0 187 }
aoqi@0 188
aoqi@0 189 Klass* ConstantPool::klass_at_impl(constantPoolHandle this_oop, int which, TRAPS) {
aoqi@0 190 // A resolved constantPool entry will contain a Klass*, otherwise a Symbol*.
aoqi@0 191 // It is not safe to rely on the tag bit's here, since we don't have a lock, and the entry and
aoqi@0 192 // tag is not updated atomicly.
aoqi@0 193
aoqi@0 194 CPSlot entry = this_oop->slot_at(which);
aoqi@0 195 if (entry.is_resolved()) {
aoqi@0 196 assert(entry.get_klass()->is_klass(), "must be");
aoqi@0 197 // Already resolved - return entry.
aoqi@0 198 return entry.get_klass();
aoqi@0 199 }
aoqi@0 200
aoqi@0 201 // Acquire lock on constant oop while doing update. After we get the lock, we check if another object
aoqi@0 202 // already has updated the object
aoqi@0 203 assert(THREAD->is_Java_thread(), "must be a Java thread");
aoqi@0 204 bool do_resolve = false;
aoqi@0 205 bool in_error = false;
aoqi@0 206
aoqi@0 207 // Create a handle for the mirror. This will preserve the resolved class
aoqi@0 208 // until the loader_data is registered.
aoqi@0 209 Handle mirror_handle;
aoqi@0 210
aoqi@0 211 Symbol* name = NULL;
aoqi@0 212 Handle loader;
aoqi@0 213 { MonitorLockerEx ml(this_oop->lock());
aoqi@0 214
aoqi@0 215 if (this_oop->tag_at(which).is_unresolved_klass()) {
aoqi@0 216 if (this_oop->tag_at(which).is_unresolved_klass_in_error()) {
aoqi@0 217 in_error = true;
aoqi@0 218 } else {
aoqi@0 219 do_resolve = true;
aoqi@0 220 name = this_oop->unresolved_klass_at(which);
aoqi@0 221 loader = Handle(THREAD, this_oop->pool_holder()->class_loader());
aoqi@0 222 }
aoqi@0 223 }
aoqi@0 224 } // unlocking constantPool
aoqi@0 225
aoqi@0 226
aoqi@0 227 // The original attempt to resolve this constant pool entry failed so find the
aoqi@0 228 // original error and throw it again (JVMS 5.4.3).
aoqi@0 229 if (in_error) {
aoqi@0 230 Symbol* error = SystemDictionary::find_resolution_error(this_oop, which);
aoqi@0 231 guarantee(error != (Symbol*)NULL, "tag mismatch with resolution error table");
aoqi@0 232 ResourceMark rm;
aoqi@0 233 // exception text will be the class name
aoqi@0 234 const char* className = this_oop->unresolved_klass_at(which)->as_C_string();
aoqi@0 235 THROW_MSG_0(error, className);
aoqi@0 236 }
aoqi@0 237
aoqi@0 238 if (do_resolve) {
aoqi@0 239 // this_oop must be unlocked during resolve_or_fail
aoqi@0 240 oop protection_domain = this_oop->pool_holder()->protection_domain();
aoqi@0 241 Handle h_prot (THREAD, protection_domain);
aoqi@0 242 Klass* k_oop = SystemDictionary::resolve_or_fail(name, loader, h_prot, true, THREAD);
aoqi@0 243 KlassHandle k;
aoqi@0 244 if (!HAS_PENDING_EXCEPTION) {
aoqi@0 245 k = KlassHandle(THREAD, k_oop);
aoqi@0 246 // preserve the resolved klass.
aoqi@0 247 mirror_handle = Handle(THREAD, k_oop->java_mirror());
aoqi@0 248 // Do access check for klasses
aoqi@0 249 verify_constant_pool_resolve(this_oop, k, THREAD);
aoqi@0 250 }
aoqi@0 251
aoqi@0 252 // Failed to resolve class. We must record the errors so that subsequent attempts
aoqi@0 253 // to resolve this constant pool entry fail with the same error (JVMS 5.4.3).
aoqi@0 254 if (HAS_PENDING_EXCEPTION) {
aoqi@0 255 ResourceMark rm;
aoqi@0 256 Symbol* error = PENDING_EXCEPTION->klass()->name();
aoqi@0 257
aoqi@0 258 bool throw_orig_error = false;
aoqi@0 259 {
aoqi@0 260 MonitorLockerEx ml(this_oop->lock());
aoqi@0 261
aoqi@0 262 // some other thread has beaten us and has resolved the class.
aoqi@0 263 if (this_oop->tag_at(which).is_klass()) {
aoqi@0 264 CLEAR_PENDING_EXCEPTION;
aoqi@0 265 entry = this_oop->resolved_klass_at(which);
aoqi@0 266 return entry.get_klass();
aoqi@0 267 }
aoqi@0 268
aoqi@0 269 if (!PENDING_EXCEPTION->
aoqi@0 270 is_a(SystemDictionary::LinkageError_klass())) {
aoqi@0 271 // Just throw the exception and don't prevent these classes from
aoqi@0 272 // being loaded due to virtual machine errors like StackOverflow
aoqi@0 273 // and OutOfMemoryError, etc, or if the thread was hit by stop()
aoqi@0 274 // Needs clarification to section 5.4.3 of the VM spec (see 6308271)
aoqi@0 275 }
aoqi@0 276 else if (!this_oop->tag_at(which).is_unresolved_klass_in_error()) {
aoqi@0 277 SystemDictionary::add_resolution_error(this_oop, which, error);
aoqi@0 278 this_oop->tag_at_put(which, JVM_CONSTANT_UnresolvedClassInError);
aoqi@0 279 } else {
aoqi@0 280 // some other thread has put the class in error state.
aoqi@0 281 error = SystemDictionary::find_resolution_error(this_oop, which);
aoqi@0 282 assert(error != NULL, "checking");
aoqi@0 283 throw_orig_error = true;
aoqi@0 284 }
aoqi@0 285 } // unlocked
aoqi@0 286
aoqi@0 287 if (throw_orig_error) {
aoqi@0 288 CLEAR_PENDING_EXCEPTION;
aoqi@0 289 ResourceMark rm;
aoqi@0 290 const char* className = this_oop->unresolved_klass_at(which)->as_C_string();
aoqi@0 291 THROW_MSG_0(error, className);
aoqi@0 292 }
aoqi@0 293
aoqi@0 294 return 0;
aoqi@0 295 }
aoqi@0 296
aoqi@0 297 if (TraceClassResolution && !k()->oop_is_array()) {
aoqi@0 298 // skip resolving the constant pool so that this code get's
aoqi@0 299 // called the next time some bytecodes refer to this class.
aoqi@0 300 ResourceMark rm;
aoqi@0 301 int line_number = -1;
aoqi@0 302 const char * source_file = NULL;
aoqi@0 303 if (JavaThread::current()->has_last_Java_frame()) {
aoqi@0 304 // try to identify the method which called this function.
aoqi@0 305 vframeStream vfst(JavaThread::current());
aoqi@0 306 if (!vfst.at_end()) {
aoqi@0 307 line_number = vfst.method()->line_number_from_bci(vfst.bci());
aoqi@0 308 Symbol* s = vfst.method()->method_holder()->source_file_name();
aoqi@0 309 if (s != NULL) {
aoqi@0 310 source_file = s->as_C_string();
aoqi@0 311 }
aoqi@0 312 }
aoqi@0 313 }
aoqi@0 314 if (k() != this_oop->pool_holder()) {
aoqi@0 315 // only print something if the classes are different
aoqi@0 316 if (source_file != NULL) {
aoqi@0 317 tty->print("RESOLVE %s %s %s:%d\n",
aoqi@0 318 this_oop->pool_holder()->external_name(),
aoqi@0 319 InstanceKlass::cast(k())->external_name(), source_file, line_number);
aoqi@0 320 } else {
aoqi@0 321 tty->print("RESOLVE %s %s\n",
aoqi@0 322 this_oop->pool_holder()->external_name(),
aoqi@0 323 InstanceKlass::cast(k())->external_name());
aoqi@0 324 }
aoqi@0 325 }
aoqi@0 326 return k();
aoqi@0 327 } else {
aoqi@0 328 MonitorLockerEx ml(this_oop->lock());
aoqi@0 329 // Only updated constant pool - if it is resolved.
aoqi@0 330 do_resolve = this_oop->tag_at(which).is_unresolved_klass();
aoqi@0 331 if (do_resolve) {
aoqi@0 332 ClassLoaderData* this_key = this_oop->pool_holder()->class_loader_data();
aoqi@0 333 this_key->record_dependency(k(), CHECK_NULL); // Can throw OOM
aoqi@0 334 this_oop->klass_at_put(which, k());
aoqi@0 335 }
aoqi@0 336 }
aoqi@0 337 }
aoqi@0 338
aoqi@0 339 entry = this_oop->resolved_klass_at(which);
aoqi@0 340 assert(entry.is_resolved() && entry.get_klass()->is_klass(), "must be resolved at this point");
aoqi@0 341 return entry.get_klass();
aoqi@0 342 }
aoqi@0 343
aoqi@0 344
aoqi@0 345 // Does not update ConstantPool* - to avoid any exception throwing. Used
aoqi@0 346 // by compiler and exception handling. Also used to avoid classloads for
aoqi@0 347 // instanceof operations. Returns NULL if the class has not been loaded or
aoqi@0 348 // if the verification of constant pool failed
aoqi@0 349 Klass* ConstantPool::klass_at_if_loaded(constantPoolHandle this_oop, int which) {
aoqi@0 350 CPSlot entry = this_oop->slot_at(which);
aoqi@0 351 if (entry.is_resolved()) {
aoqi@0 352 assert(entry.get_klass()->is_klass(), "must be");
aoqi@0 353 return entry.get_klass();
aoqi@0 354 } else {
aoqi@0 355 assert(entry.is_unresolved(), "must be either symbol or klass");
aoqi@0 356 Thread *thread = Thread::current();
aoqi@0 357 Symbol* name = entry.get_symbol();
aoqi@0 358 oop loader = this_oop->pool_holder()->class_loader();
aoqi@0 359 oop protection_domain = this_oop->pool_holder()->protection_domain();
aoqi@0 360 Handle h_prot (thread, protection_domain);
aoqi@0 361 Handle h_loader (thread, loader);
aoqi@0 362 Klass* k = SystemDictionary::find(name, h_loader, h_prot, thread);
aoqi@0 363
aoqi@0 364 if (k != NULL) {
aoqi@0 365 // Make sure that resolving is legal
aoqi@0 366 EXCEPTION_MARK;
aoqi@0 367 KlassHandle klass(THREAD, k);
aoqi@0 368 // return NULL if verification fails
aoqi@0 369 verify_constant_pool_resolve(this_oop, klass, THREAD);
aoqi@0 370 if (HAS_PENDING_EXCEPTION) {
aoqi@0 371 CLEAR_PENDING_EXCEPTION;
aoqi@0 372 return NULL;
aoqi@0 373 }
aoqi@0 374 return klass();
aoqi@0 375 } else {
aoqi@0 376 return k;
aoqi@0 377 }
aoqi@0 378 }
aoqi@0 379 }
aoqi@0 380
aoqi@0 381
aoqi@0 382 Klass* ConstantPool::klass_ref_at_if_loaded(constantPoolHandle this_oop, int which) {
aoqi@0 383 return klass_at_if_loaded(this_oop, this_oop->klass_ref_index_at(which));
aoqi@0 384 }
aoqi@0 385
aoqi@0 386
aoqi@0 387 Method* ConstantPool::method_at_if_loaded(constantPoolHandle cpool,
aoqi@0 388 int which) {
aoqi@0 389 if (cpool->cache() == NULL) return NULL; // nothing to load yet
aoqi@0 390 int cache_index = decode_cpcache_index(which, true);
aoqi@0 391 if (!(cache_index >= 0 && cache_index < cpool->cache()->length())) {
aoqi@0 392 // FIXME: should be an assert
aoqi@0 393 if (PrintMiscellaneous && (Verbose||WizardMode)) {
aoqi@0 394 tty->print_cr("bad operand %d in:", which); cpool->print();
aoqi@0 395 }
aoqi@0 396 return NULL;
aoqi@0 397 }
aoqi@0 398 ConstantPoolCacheEntry* e = cpool->cache()->entry_at(cache_index);
aoqi@0 399 return e->method_if_resolved(cpool);
aoqi@0 400 }
aoqi@0 401
aoqi@0 402
aoqi@0 403 bool ConstantPool::has_appendix_at_if_loaded(constantPoolHandle cpool, int which) {
aoqi@0 404 if (cpool->cache() == NULL) return false; // nothing to load yet
aoqi@0 405 int cache_index = decode_cpcache_index(which, true);
aoqi@0 406 ConstantPoolCacheEntry* e = cpool->cache()->entry_at(cache_index);
aoqi@0 407 return e->has_appendix();
aoqi@0 408 }
aoqi@0 409
aoqi@0 410 oop ConstantPool::appendix_at_if_loaded(constantPoolHandle cpool, int which) {
aoqi@0 411 if (cpool->cache() == NULL) return NULL; // nothing to load yet
aoqi@0 412 int cache_index = decode_cpcache_index(which, true);
aoqi@0 413 ConstantPoolCacheEntry* e = cpool->cache()->entry_at(cache_index);
aoqi@0 414 return e->appendix_if_resolved(cpool);
aoqi@0 415 }
aoqi@0 416
aoqi@0 417
aoqi@0 418 bool ConstantPool::has_method_type_at_if_loaded(constantPoolHandle cpool, int which) {
aoqi@0 419 if (cpool->cache() == NULL) return false; // nothing to load yet
aoqi@0 420 int cache_index = decode_cpcache_index(which, true);
aoqi@0 421 ConstantPoolCacheEntry* e = cpool->cache()->entry_at(cache_index);
aoqi@0 422 return e->has_method_type();
aoqi@0 423 }
aoqi@0 424
aoqi@0 425 oop ConstantPool::method_type_at_if_loaded(constantPoolHandle cpool, int which) {
aoqi@0 426 if (cpool->cache() == NULL) return NULL; // nothing to load yet
aoqi@0 427 int cache_index = decode_cpcache_index(which, true);
aoqi@0 428 ConstantPoolCacheEntry* e = cpool->cache()->entry_at(cache_index);
aoqi@0 429 return e->method_type_if_resolved(cpool);
aoqi@0 430 }
aoqi@0 431
aoqi@0 432
aoqi@0 433 Symbol* ConstantPool::impl_name_ref_at(int which, bool uncached) {
aoqi@0 434 int name_index = name_ref_index_at(impl_name_and_type_ref_index_at(which, uncached));
aoqi@0 435 return symbol_at(name_index);
aoqi@0 436 }
aoqi@0 437
aoqi@0 438
aoqi@0 439 Symbol* ConstantPool::impl_signature_ref_at(int which, bool uncached) {
aoqi@0 440 int signature_index = signature_ref_index_at(impl_name_and_type_ref_index_at(which, uncached));
aoqi@0 441 return symbol_at(signature_index);
aoqi@0 442 }
aoqi@0 443
aoqi@0 444
aoqi@0 445 int ConstantPool::impl_name_and_type_ref_index_at(int which, bool uncached) {
aoqi@0 446 int i = which;
aoqi@0 447 if (!uncached && cache() != NULL) {
aoqi@0 448 if (ConstantPool::is_invokedynamic_index(which)) {
aoqi@0 449 // Invokedynamic index is index into resolved_references
aoqi@0 450 int pool_index = invokedynamic_cp_cache_entry_at(which)->constant_pool_index();
aoqi@0 451 pool_index = invoke_dynamic_name_and_type_ref_index_at(pool_index);
aoqi@0 452 assert(tag_at(pool_index).is_name_and_type(), "");
aoqi@0 453 return pool_index;
aoqi@0 454 }
aoqi@0 455 // change byte-ordering and go via cache
aoqi@0 456 i = remap_instruction_operand_from_cache(which);
aoqi@0 457 } else {
aoqi@0 458 if (tag_at(which).is_invoke_dynamic()) {
aoqi@0 459 int pool_index = invoke_dynamic_name_and_type_ref_index_at(which);
aoqi@0 460 assert(tag_at(pool_index).is_name_and_type(), "");
aoqi@0 461 return pool_index;
aoqi@0 462 }
aoqi@0 463 }
aoqi@0 464 assert(tag_at(i).is_field_or_method(), "Corrupted constant pool");
aoqi@0 465 assert(!tag_at(i).is_invoke_dynamic(), "Must be handled above");
aoqi@0 466 jint ref_index = *int_at_addr(i);
aoqi@0 467 return extract_high_short_from_int(ref_index);
aoqi@0 468 }
aoqi@0 469
aoqi@0 470
aoqi@0 471 int ConstantPool::impl_klass_ref_index_at(int which, bool uncached) {
aoqi@0 472 guarantee(!ConstantPool::is_invokedynamic_index(which),
aoqi@0 473 "an invokedynamic instruction does not have a klass");
aoqi@0 474 int i = which;
aoqi@0 475 if (!uncached && cache() != NULL) {
aoqi@0 476 // change byte-ordering and go via cache
aoqi@0 477 i = remap_instruction_operand_from_cache(which);
aoqi@0 478 }
aoqi@0 479 assert(tag_at(i).is_field_or_method(), "Corrupted constant pool");
aoqi@0 480 jint ref_index = *int_at_addr(i);
aoqi@0 481 return extract_low_short_from_int(ref_index);
aoqi@0 482 }
aoqi@0 483
aoqi@0 484
aoqi@0 485
aoqi@0 486 int ConstantPool::remap_instruction_operand_from_cache(int operand) {
aoqi@0 487 int cpc_index = operand;
aoqi@0 488 DEBUG_ONLY(cpc_index -= CPCACHE_INDEX_TAG);
aoqi@0 489 assert((int)(u2)cpc_index == cpc_index, "clean u2");
aoqi@0 490 int member_index = cache()->entry_at(cpc_index)->constant_pool_index();
aoqi@0 491 return member_index;
aoqi@0 492 }
aoqi@0 493
aoqi@0 494
aoqi@0 495 void ConstantPool::verify_constant_pool_resolve(constantPoolHandle this_oop, KlassHandle k, TRAPS) {
aoqi@0 496 if (k->oop_is_instance() || k->oop_is_objArray()) {
aoqi@0 497 instanceKlassHandle holder (THREAD, this_oop->pool_holder());
aoqi@0 498 Klass* elem_oop = k->oop_is_instance() ? k() : ObjArrayKlass::cast(k())->bottom_klass();
aoqi@0 499 KlassHandle element (THREAD, elem_oop);
aoqi@0 500
aoqi@0 501 // The element type could be a typeArray - we only need the access check if it is
aoqi@0 502 // an reference to another class
aoqi@0 503 if (element->oop_is_instance()) {
aoqi@0 504 LinkResolver::check_klass_accessability(holder, element, CHECK);
aoqi@0 505 }
aoqi@0 506 }
aoqi@0 507 }
aoqi@0 508
aoqi@0 509
aoqi@0 510 int ConstantPool::name_ref_index_at(int which_nt) {
aoqi@0 511 jint ref_index = name_and_type_at(which_nt);
aoqi@0 512 return extract_low_short_from_int(ref_index);
aoqi@0 513 }
aoqi@0 514
aoqi@0 515
aoqi@0 516 int ConstantPool::signature_ref_index_at(int which_nt) {
aoqi@0 517 jint ref_index = name_and_type_at(which_nt);
aoqi@0 518 return extract_high_short_from_int(ref_index);
aoqi@0 519 }
aoqi@0 520
aoqi@0 521
aoqi@0 522 Klass* ConstantPool::klass_ref_at(int which, TRAPS) {
aoqi@0 523 return klass_at(klass_ref_index_at(which), CHECK_NULL);
aoqi@0 524 }
aoqi@0 525
aoqi@0 526
aoqi@0 527 Symbol* ConstantPool::klass_name_at(int which) {
aoqi@0 528 assert(tag_at(which).is_unresolved_klass() || tag_at(which).is_klass(),
aoqi@0 529 "Corrupted constant pool");
aoqi@0 530 // A resolved constantPool entry will contain a Klass*, otherwise a Symbol*.
aoqi@0 531 // It is not safe to rely on the tag bit's here, since we don't have a lock, and the entry and
aoqi@0 532 // tag is not updated atomicly.
aoqi@0 533 CPSlot entry = slot_at(which);
aoqi@0 534 if (entry.is_resolved()) {
aoqi@0 535 // Already resolved - return entry's name.
aoqi@0 536 assert(entry.get_klass()->is_klass(), "must be");
aoqi@0 537 return entry.get_klass()->name();
aoqi@0 538 } else {
aoqi@0 539 assert(entry.is_unresolved(), "must be either symbol or klass");
aoqi@0 540 return entry.get_symbol();
aoqi@0 541 }
aoqi@0 542 }
aoqi@0 543
aoqi@0 544 Symbol* ConstantPool::klass_ref_at_noresolve(int which) {
aoqi@0 545 jint ref_index = klass_ref_index_at(which);
aoqi@0 546 return klass_at_noresolve(ref_index);
aoqi@0 547 }
aoqi@0 548
aoqi@0 549 Symbol* ConstantPool::uncached_klass_ref_at_noresolve(int which) {
aoqi@0 550 jint ref_index = uncached_klass_ref_index_at(which);
aoqi@0 551 return klass_at_noresolve(ref_index);
aoqi@0 552 }
aoqi@0 553
aoqi@0 554 char* ConstantPool::string_at_noresolve(int which) {
aoqi@0 555 Symbol* s = unresolved_string_at(which);
aoqi@0 556 if (s == NULL) {
aoqi@0 557 return (char*)"<pseudo-string>";
aoqi@0 558 } else {
aoqi@0 559 return unresolved_string_at(which)->as_C_string();
aoqi@0 560 }
aoqi@0 561 }
aoqi@0 562
aoqi@0 563 BasicType ConstantPool::basic_type_for_signature_at(int which) {
aoqi@0 564 return FieldType::basic_type(symbol_at(which));
aoqi@0 565 }
aoqi@0 566
aoqi@0 567
aoqi@0 568 void ConstantPool::resolve_string_constants_impl(constantPoolHandle this_oop, TRAPS) {
aoqi@0 569 for (int index = 1; index < this_oop->length(); index++) { // Index 0 is unused
aoqi@0 570 if (this_oop->tag_at(index).is_string()) {
aoqi@0 571 this_oop->string_at(index, CHECK);
aoqi@0 572 }
aoqi@0 573 }
aoqi@0 574 }
aoqi@0 575
aoqi@0 576 // Resolve all the classes in the constant pool. If they are all resolved,
aoqi@0 577 // the constant pool is read-only. Enhancement: allocate cp entries to
aoqi@0 578 // another metaspace, and copy to read-only or read-write space if this
aoqi@0 579 // bit is set.
aoqi@0 580 bool ConstantPool::resolve_class_constants(TRAPS) {
aoqi@0 581 constantPoolHandle cp(THREAD, this);
aoqi@0 582 for (int index = 1; index < length(); index++) { // Index 0 is unused
aoqi@0 583 if (tag_at(index).is_unresolved_klass() &&
aoqi@0 584 klass_at_if_loaded(cp, index) == NULL) {
aoqi@0 585 return false;
aoqi@0 586 }
aoqi@0 587 }
aoqi@0 588 // set_preresolution(); or some bit for future use
aoqi@0 589 return true;
aoqi@0 590 }
aoqi@0 591
aoqi@0 592 // If resolution for MethodHandle or MethodType fails, save the exception
aoqi@0 593 // in the resolution error table, so that the same exception is thrown again.
aoqi@0 594 void ConstantPool::save_and_throw_exception(constantPoolHandle this_oop, int which,
aoqi@0 595 int tag, TRAPS) {
aoqi@0 596 ResourceMark rm;
aoqi@0 597 Symbol* error = PENDING_EXCEPTION->klass()->name();
aoqi@0 598 MonitorLockerEx ml(this_oop->lock()); // lock cpool to change tag.
aoqi@0 599
aoqi@0 600 int error_tag = (tag == JVM_CONSTANT_MethodHandle) ?
aoqi@0 601 JVM_CONSTANT_MethodHandleInError : JVM_CONSTANT_MethodTypeInError;
aoqi@0 602
aoqi@0 603 if (!PENDING_EXCEPTION->
aoqi@0 604 is_a(SystemDictionary::LinkageError_klass())) {
aoqi@0 605 // Just throw the exception and don't prevent these classes from
aoqi@0 606 // being loaded due to virtual machine errors like StackOverflow
aoqi@0 607 // and OutOfMemoryError, etc, or if the thread was hit by stop()
aoqi@0 608 // Needs clarification to section 5.4.3 of the VM spec (see 6308271)
aoqi@0 609
aoqi@0 610 } else if (this_oop->tag_at(which).value() != error_tag) {
aoqi@0 611 SystemDictionary::add_resolution_error(this_oop, which, error);
aoqi@0 612 this_oop->tag_at_put(which, error_tag);
aoqi@0 613 } else {
aoqi@0 614 // some other thread has put the class in error state.
aoqi@0 615 error = SystemDictionary::find_resolution_error(this_oop, which);
aoqi@0 616 assert(error != NULL, "checking");
aoqi@0 617 CLEAR_PENDING_EXCEPTION;
aoqi@0 618 THROW_MSG(error, "");
aoqi@0 619 }
aoqi@0 620 }
aoqi@0 621
aoqi@0 622
aoqi@0 623 // Called to resolve constants in the constant pool and return an oop.
aoqi@0 624 // Some constant pool entries cache their resolved oop. This is also
aoqi@0 625 // called to create oops from constants to use in arguments for invokedynamic
aoqi@0 626 oop ConstantPool::resolve_constant_at_impl(constantPoolHandle this_oop, int index, int cache_index, TRAPS) {
aoqi@0 627 oop result_oop = NULL;
aoqi@0 628 Handle throw_exception;
aoqi@0 629
aoqi@0 630 if (cache_index == _possible_index_sentinel) {
aoqi@0 631 // It is possible that this constant is one which is cached in the objects.
aoqi@0 632 // We'll do a linear search. This should be OK because this usage is rare.
aoqi@0 633 assert(index > 0, "valid index");
aoqi@0 634 cache_index = this_oop->cp_to_object_index(index);
aoqi@0 635 }
aoqi@0 636 assert(cache_index == _no_index_sentinel || cache_index >= 0, "");
aoqi@0 637 assert(index == _no_index_sentinel || index >= 0, "");
aoqi@0 638
aoqi@0 639 if (cache_index >= 0) {
aoqi@0 640 result_oop = this_oop->resolved_references()->obj_at(cache_index);
aoqi@0 641 if (result_oop != NULL) {
aoqi@0 642 return result_oop;
aoqi@0 643 // That was easy...
aoqi@0 644 }
aoqi@0 645 index = this_oop->object_to_cp_index(cache_index);
aoqi@0 646 }
aoqi@0 647
aoqi@0 648 jvalue prim_value; // temp used only in a few cases below
aoqi@0 649
aoqi@0 650 int tag_value = this_oop->tag_at(index).value();
aoqi@0 651
aoqi@0 652 switch (tag_value) {
aoqi@0 653
aoqi@0 654 case JVM_CONSTANT_UnresolvedClass:
aoqi@0 655 case JVM_CONSTANT_UnresolvedClassInError:
aoqi@0 656 case JVM_CONSTANT_Class:
aoqi@0 657 {
aoqi@0 658 assert(cache_index == _no_index_sentinel, "should not have been set");
aoqi@0 659 Klass* resolved = klass_at_impl(this_oop, index, CHECK_NULL);
aoqi@0 660 // ldc wants the java mirror.
aoqi@0 661 result_oop = resolved->java_mirror();
aoqi@0 662 break;
aoqi@0 663 }
aoqi@0 664
aoqi@0 665 case JVM_CONSTANT_String:
aoqi@0 666 assert(cache_index != _no_index_sentinel, "should have been set");
aoqi@0 667 if (this_oop->is_pseudo_string_at(index)) {
aoqi@0 668 result_oop = this_oop->pseudo_string_at(index, cache_index);
aoqi@0 669 break;
aoqi@0 670 }
aoqi@0 671 result_oop = string_at_impl(this_oop, index, cache_index, CHECK_NULL);
aoqi@0 672 break;
aoqi@0 673
aoqi@0 674 case JVM_CONSTANT_MethodHandleInError:
aoqi@0 675 case JVM_CONSTANT_MethodTypeInError:
aoqi@0 676 {
aoqi@0 677 Symbol* error = SystemDictionary::find_resolution_error(this_oop, index);
aoqi@0 678 guarantee(error != (Symbol*)NULL, "tag mismatch with resolution error table");
aoqi@0 679 ResourceMark rm;
aoqi@0 680 THROW_MSG_0(error, "");
aoqi@0 681 break;
aoqi@0 682 }
aoqi@0 683
aoqi@0 684 case JVM_CONSTANT_MethodHandle:
aoqi@0 685 {
aoqi@0 686 int ref_kind = this_oop->method_handle_ref_kind_at(index);
aoqi@0 687 int callee_index = this_oop->method_handle_klass_index_at(index);
aoqi@0 688 Symbol* name = this_oop->method_handle_name_ref_at(index);
aoqi@0 689 Symbol* signature = this_oop->method_handle_signature_ref_at(index);
aoqi@0 690 if (PrintMiscellaneous)
aoqi@0 691 tty->print_cr("resolve JVM_CONSTANT_MethodHandle:%d [%d/%d/%d] %s.%s",
aoqi@0 692 ref_kind, index, this_oop->method_handle_index_at(index),
aoqi@0 693 callee_index, name->as_C_string(), signature->as_C_string());
aoqi@0 694 KlassHandle callee;
aoqi@0 695 { Klass* k = klass_at_impl(this_oop, callee_index, CHECK_NULL);
aoqi@0 696 callee = KlassHandle(THREAD, k);
aoqi@0 697 }
aoqi@0 698 KlassHandle klass(THREAD, this_oop->pool_holder());
aoqi@0 699 Handle value = SystemDictionary::link_method_handle_constant(klass, ref_kind,
aoqi@0 700 callee, name, signature,
aoqi@0 701 THREAD);
aoqi@0 702 result_oop = value();
aoqi@0 703 if (HAS_PENDING_EXCEPTION) {
aoqi@0 704 save_and_throw_exception(this_oop, index, tag_value, CHECK_NULL);
aoqi@0 705 }
aoqi@0 706 break;
aoqi@0 707 }
aoqi@0 708
aoqi@0 709 case JVM_CONSTANT_MethodType:
aoqi@0 710 {
aoqi@0 711 Symbol* signature = this_oop->method_type_signature_at(index);
aoqi@0 712 if (PrintMiscellaneous)
aoqi@0 713 tty->print_cr("resolve JVM_CONSTANT_MethodType [%d/%d] %s",
aoqi@0 714 index, this_oop->method_type_index_at(index),
aoqi@0 715 signature->as_C_string());
aoqi@0 716 KlassHandle klass(THREAD, this_oop->pool_holder());
aoqi@0 717 Handle value = SystemDictionary::find_method_handle_type(signature, klass, THREAD);
aoqi@0 718 result_oop = value();
aoqi@0 719 if (HAS_PENDING_EXCEPTION) {
aoqi@0 720 save_and_throw_exception(this_oop, index, tag_value, CHECK_NULL);
aoqi@0 721 }
aoqi@0 722 break;
aoqi@0 723 }
aoqi@0 724
aoqi@0 725 case JVM_CONSTANT_Integer:
aoqi@0 726 assert(cache_index == _no_index_sentinel, "should not have been set");
aoqi@0 727 prim_value.i = this_oop->int_at(index);
aoqi@0 728 result_oop = java_lang_boxing_object::create(T_INT, &prim_value, CHECK_NULL);
aoqi@0 729 break;
aoqi@0 730
aoqi@0 731 case JVM_CONSTANT_Float:
aoqi@0 732 assert(cache_index == _no_index_sentinel, "should not have been set");
aoqi@0 733 prim_value.f = this_oop->float_at(index);
aoqi@0 734 result_oop = java_lang_boxing_object::create(T_FLOAT, &prim_value, CHECK_NULL);
aoqi@0 735 break;
aoqi@0 736
aoqi@0 737 case JVM_CONSTANT_Long:
aoqi@0 738 assert(cache_index == _no_index_sentinel, "should not have been set");
aoqi@0 739 prim_value.j = this_oop->long_at(index);
aoqi@0 740 result_oop = java_lang_boxing_object::create(T_LONG, &prim_value, CHECK_NULL);
aoqi@0 741 break;
aoqi@0 742
aoqi@0 743 case JVM_CONSTANT_Double:
aoqi@0 744 assert(cache_index == _no_index_sentinel, "should not have been set");
aoqi@0 745 prim_value.d = this_oop->double_at(index);
aoqi@0 746 result_oop = java_lang_boxing_object::create(T_DOUBLE, &prim_value, CHECK_NULL);
aoqi@0 747 break;
aoqi@0 748
aoqi@0 749 default:
aoqi@0 750 DEBUG_ONLY( tty->print_cr("*** %p: tag at CP[%d/%d] = %d",
aoqi@0 751 this_oop(), index, cache_index, tag_value) );
aoqi@0 752 assert(false, "unexpected constant tag");
aoqi@0 753 break;
aoqi@0 754 }
aoqi@0 755
aoqi@0 756 if (cache_index >= 0) {
aoqi@0 757 // Cache the oop here also.
aoqi@0 758 Handle result_handle(THREAD, result_oop);
aoqi@0 759 MonitorLockerEx ml(this_oop->lock()); // don't know if we really need this
aoqi@0 760 oop result = this_oop->resolved_references()->obj_at(cache_index);
aoqi@0 761 // Benign race condition: resolved_references may already be filled in while we were trying to lock.
aoqi@0 762 // The important thing here is that all threads pick up the same result.
aoqi@0 763 // It doesn't matter which racing thread wins, as long as only one
aoqi@0 764 // result is used by all threads, and all future queries.
aoqi@0 765 // That result may be either a resolved constant or a failure exception.
aoqi@0 766 if (result == NULL) {
aoqi@0 767 this_oop->resolved_references()->obj_at_put(cache_index, result_handle());
aoqi@0 768 return result_handle();
aoqi@0 769 } else {
aoqi@0 770 // Return the winning thread's result. This can be different than
aoqi@0 771 // result_handle() for MethodHandles.
aoqi@0 772 return result;
aoqi@0 773 }
aoqi@0 774 } else {
aoqi@0 775 return result_oop;
aoqi@0 776 }
aoqi@0 777 }
aoqi@0 778
aoqi@0 779 oop ConstantPool::uncached_string_at(int which, TRAPS) {
aoqi@0 780 Symbol* sym = unresolved_string_at(which);
aoqi@0 781 oop str = StringTable::intern(sym, CHECK_(NULL));
aoqi@0 782 assert(java_lang_String::is_instance(str), "must be string");
aoqi@0 783 return str;
aoqi@0 784 }
aoqi@0 785
aoqi@0 786
aoqi@0 787 oop ConstantPool::resolve_bootstrap_specifier_at_impl(constantPoolHandle this_oop, int index, TRAPS) {
aoqi@0 788 assert(this_oop->tag_at(index).is_invoke_dynamic(), "Corrupted constant pool");
aoqi@0 789
aoqi@0 790 Handle bsm;
aoqi@0 791 int argc;
aoqi@0 792 {
aoqi@0 793 // JVM_CONSTANT_InvokeDynamic is an ordered pair of [bootm, name&type], plus optional arguments
aoqi@0 794 // The bootm, being a JVM_CONSTANT_MethodHandle, has its own cache entry.
aoqi@0 795 // It is accompanied by the optional arguments.
aoqi@0 796 int bsm_index = this_oop->invoke_dynamic_bootstrap_method_ref_index_at(index);
aoqi@0 797 oop bsm_oop = this_oop->resolve_possibly_cached_constant_at(bsm_index, CHECK_NULL);
aoqi@0 798 if (!java_lang_invoke_MethodHandle::is_instance(bsm_oop)) {
aoqi@0 799 THROW_MSG_NULL(vmSymbols::java_lang_LinkageError(), "BSM not an MethodHandle");
aoqi@0 800 }
aoqi@0 801
aoqi@0 802 // Extract the optional static arguments.
aoqi@0 803 argc = this_oop->invoke_dynamic_argument_count_at(index);
aoqi@0 804 if (argc == 0) return bsm_oop;
aoqi@0 805
aoqi@0 806 bsm = Handle(THREAD, bsm_oop);
aoqi@0 807 }
aoqi@0 808
aoqi@0 809 objArrayHandle info;
aoqi@0 810 {
aoqi@0 811 objArrayOop info_oop = oopFactory::new_objArray(SystemDictionary::Object_klass(), 1+argc, CHECK_NULL);
aoqi@0 812 info = objArrayHandle(THREAD, info_oop);
aoqi@0 813 }
aoqi@0 814
aoqi@0 815 info->obj_at_put(0, bsm());
aoqi@0 816 for (int i = 0; i < argc; i++) {
aoqi@0 817 int arg_index = this_oop->invoke_dynamic_argument_index_at(index, i);
aoqi@0 818 oop arg_oop = this_oop->resolve_possibly_cached_constant_at(arg_index, CHECK_NULL);
aoqi@0 819 info->obj_at_put(1+i, arg_oop);
aoqi@0 820 }
aoqi@0 821
aoqi@0 822 return info();
aoqi@0 823 }
aoqi@0 824
aoqi@0 825 oop ConstantPool::string_at_impl(constantPoolHandle this_oop, int which, int obj_index, TRAPS) {
aoqi@0 826 // If the string has already been interned, this entry will be non-null
aoqi@0 827 oop str = this_oop->resolved_references()->obj_at(obj_index);
aoqi@0 828 if (str != NULL) return str;
aoqi@0 829 Symbol* sym = this_oop->unresolved_string_at(which);
aoqi@0 830 str = StringTable::intern(sym, CHECK_(NULL));
aoqi@0 831 this_oop->string_at_put(which, obj_index, str);
aoqi@0 832 assert(java_lang_String::is_instance(str), "must be string");
aoqi@0 833 return str;
aoqi@0 834 }
aoqi@0 835
aoqi@0 836
aoqi@0 837 bool ConstantPool::klass_name_at_matches(instanceKlassHandle k,
aoqi@0 838 int which) {
aoqi@0 839 // Names are interned, so we can compare Symbol*s directly
aoqi@0 840 Symbol* cp_name = klass_name_at(which);
aoqi@0 841 return (cp_name == k->name());
aoqi@0 842 }
aoqi@0 843
aoqi@0 844
aoqi@0 845 // Iterate over symbols and decrement ones which are Symbol*s.
aoqi@0 846 // This is done during GC so do not need to lock constantPool unless we
aoqi@0 847 // have per-thread safepoints.
aoqi@0 848 // Only decrement the UTF8 symbols. Unresolved classes and strings point to
aoqi@0 849 // these symbols but didn't increment the reference count.
aoqi@0 850 void ConstantPool::unreference_symbols() {
aoqi@0 851 for (int index = 1; index < length(); index++) { // Index 0 is unused
aoqi@0 852 constantTag tag = tag_at(index);
aoqi@0 853 if (tag.is_symbol()) {
aoqi@0 854 symbol_at(index)->decrement_refcount();
aoqi@0 855 }
aoqi@0 856 }
aoqi@0 857 }
aoqi@0 858
aoqi@0 859
aoqi@0 860 // Compare this constant pool's entry at index1 to the constant pool
aoqi@0 861 // cp2's entry at index2.
aoqi@0 862 bool ConstantPool::compare_entry_to(int index1, constantPoolHandle cp2,
aoqi@0 863 int index2, TRAPS) {
aoqi@0 864
aoqi@0 865 // The error tags are equivalent to non-error tags when comparing
aoqi@0 866 jbyte t1 = tag_at(index1).non_error_value();
aoqi@0 867 jbyte t2 = cp2->tag_at(index2).non_error_value();
aoqi@0 868
aoqi@0 869 if (t1 != t2) {
aoqi@0 870 // Not the same entry type so there is nothing else to check. Note
aoqi@0 871 // that this style of checking will consider resolved/unresolved
aoqi@0 872 // class pairs as different.
aoqi@0 873 // From the ConstantPool* API point of view, this is correct
aoqi@0 874 // behavior. See VM_RedefineClasses::merge_constant_pools() to see how this
aoqi@0 875 // plays out in the context of ConstantPool* merging.
aoqi@0 876 return false;
aoqi@0 877 }
aoqi@0 878
aoqi@0 879 switch (t1) {
aoqi@0 880 case JVM_CONSTANT_Class:
aoqi@0 881 {
aoqi@0 882 Klass* k1 = klass_at(index1, CHECK_false);
aoqi@0 883 Klass* k2 = cp2->klass_at(index2, CHECK_false);
aoqi@0 884 if (k1 == k2) {
aoqi@0 885 return true;
aoqi@0 886 }
aoqi@0 887 } break;
aoqi@0 888
aoqi@0 889 case JVM_CONSTANT_ClassIndex:
aoqi@0 890 {
aoqi@0 891 int recur1 = klass_index_at(index1);
aoqi@0 892 int recur2 = cp2->klass_index_at(index2);
aoqi@0 893 bool match = compare_entry_to(recur1, cp2, recur2, CHECK_false);
aoqi@0 894 if (match) {
aoqi@0 895 return true;
aoqi@0 896 }
aoqi@0 897 } break;
aoqi@0 898
aoqi@0 899 case JVM_CONSTANT_Double:
aoqi@0 900 {
aoqi@0 901 jdouble d1 = double_at(index1);
aoqi@0 902 jdouble d2 = cp2->double_at(index2);
aoqi@0 903 if (d1 == d2) {
aoqi@0 904 return true;
aoqi@0 905 }
aoqi@0 906 } break;
aoqi@0 907
aoqi@0 908 case JVM_CONSTANT_Fieldref:
aoqi@0 909 case JVM_CONSTANT_InterfaceMethodref:
aoqi@0 910 case JVM_CONSTANT_Methodref:
aoqi@0 911 {
aoqi@0 912 int recur1 = uncached_klass_ref_index_at(index1);
aoqi@0 913 int recur2 = cp2->uncached_klass_ref_index_at(index2);
aoqi@0 914 bool match = compare_entry_to(recur1, cp2, recur2, CHECK_false);
aoqi@0 915 if (match) {
aoqi@0 916 recur1 = uncached_name_and_type_ref_index_at(index1);
aoqi@0 917 recur2 = cp2->uncached_name_and_type_ref_index_at(index2);
aoqi@0 918 match = compare_entry_to(recur1, cp2, recur2, CHECK_false);
aoqi@0 919 if (match) {
aoqi@0 920 return true;
aoqi@0 921 }
aoqi@0 922 }
aoqi@0 923 } break;
aoqi@0 924
aoqi@0 925 case JVM_CONSTANT_Float:
aoqi@0 926 {
aoqi@0 927 jfloat f1 = float_at(index1);
aoqi@0 928 jfloat f2 = cp2->float_at(index2);
aoqi@0 929 if (f1 == f2) {
aoqi@0 930 return true;
aoqi@0 931 }
aoqi@0 932 } break;
aoqi@0 933
aoqi@0 934 case JVM_CONSTANT_Integer:
aoqi@0 935 {
aoqi@0 936 jint i1 = int_at(index1);
aoqi@0 937 jint i2 = cp2->int_at(index2);
aoqi@0 938 if (i1 == i2) {
aoqi@0 939 return true;
aoqi@0 940 }
aoqi@0 941 } break;
aoqi@0 942
aoqi@0 943 case JVM_CONSTANT_Long:
aoqi@0 944 {
aoqi@0 945 jlong l1 = long_at(index1);
aoqi@0 946 jlong l2 = cp2->long_at(index2);
aoqi@0 947 if (l1 == l2) {
aoqi@0 948 return true;
aoqi@0 949 }
aoqi@0 950 } break;
aoqi@0 951
aoqi@0 952 case JVM_CONSTANT_NameAndType:
aoqi@0 953 {
aoqi@0 954 int recur1 = name_ref_index_at(index1);
aoqi@0 955 int recur2 = cp2->name_ref_index_at(index2);
aoqi@0 956 bool match = compare_entry_to(recur1, cp2, recur2, CHECK_false);
aoqi@0 957 if (match) {
aoqi@0 958 recur1 = signature_ref_index_at(index1);
aoqi@0 959 recur2 = cp2->signature_ref_index_at(index2);
aoqi@0 960 match = compare_entry_to(recur1, cp2, recur2, CHECK_false);
aoqi@0 961 if (match) {
aoqi@0 962 return true;
aoqi@0 963 }
aoqi@0 964 }
aoqi@0 965 } break;
aoqi@0 966
aoqi@0 967 case JVM_CONSTANT_StringIndex:
aoqi@0 968 {
aoqi@0 969 int recur1 = string_index_at(index1);
aoqi@0 970 int recur2 = cp2->string_index_at(index2);
aoqi@0 971 bool match = compare_entry_to(recur1, cp2, recur2, CHECK_false);
aoqi@0 972 if (match) {
aoqi@0 973 return true;
aoqi@0 974 }
aoqi@0 975 } break;
aoqi@0 976
aoqi@0 977 case JVM_CONSTANT_UnresolvedClass:
aoqi@0 978 {
aoqi@0 979 Symbol* k1 = unresolved_klass_at(index1);
aoqi@0 980 Symbol* k2 = cp2->unresolved_klass_at(index2);
aoqi@0 981 if (k1 == k2) {
aoqi@0 982 return true;
aoqi@0 983 }
aoqi@0 984 } break;
aoqi@0 985
aoqi@0 986 case JVM_CONSTANT_MethodType:
aoqi@0 987 {
aoqi@0 988 int k1 = method_type_index_at_error_ok(index1);
aoqi@0 989 int k2 = cp2->method_type_index_at_error_ok(index2);
aoqi@0 990 bool match = compare_entry_to(k1, cp2, k2, CHECK_false);
aoqi@0 991 if (match) {
aoqi@0 992 return true;
aoqi@0 993 }
aoqi@0 994 } break;
aoqi@0 995
aoqi@0 996 case JVM_CONSTANT_MethodHandle:
aoqi@0 997 {
aoqi@0 998 int k1 = method_handle_ref_kind_at_error_ok(index1);
aoqi@0 999 int k2 = cp2->method_handle_ref_kind_at_error_ok(index2);
aoqi@0 1000 if (k1 == k2) {
aoqi@0 1001 int i1 = method_handle_index_at_error_ok(index1);
aoqi@0 1002 int i2 = cp2->method_handle_index_at_error_ok(index2);
aoqi@0 1003 bool match = compare_entry_to(i1, cp2, i2, CHECK_false);
aoqi@0 1004 if (match) {
aoqi@0 1005 return true;
aoqi@0 1006 }
aoqi@0 1007 }
aoqi@0 1008 } break;
aoqi@0 1009
aoqi@0 1010 case JVM_CONSTANT_InvokeDynamic:
aoqi@0 1011 {
aoqi@0 1012 int k1 = invoke_dynamic_name_and_type_ref_index_at(index1);
aoqi@0 1013 int k2 = cp2->invoke_dynamic_name_and_type_ref_index_at(index2);
aoqi@0 1014 int i1 = invoke_dynamic_bootstrap_specifier_index(index1);
aoqi@0 1015 int i2 = cp2->invoke_dynamic_bootstrap_specifier_index(index2);
aoqi@0 1016 // separate statements and variables because CHECK_false is used
aoqi@0 1017 bool match_entry = compare_entry_to(k1, cp2, k2, CHECK_false);
aoqi@0 1018 bool match_operand = compare_operand_to(i1, cp2, i2, CHECK_false);
aoqi@0 1019 return (match_entry && match_operand);
aoqi@0 1020 } break;
aoqi@0 1021
aoqi@0 1022 case JVM_CONSTANT_String:
aoqi@0 1023 {
aoqi@0 1024 Symbol* s1 = unresolved_string_at(index1);
aoqi@0 1025 Symbol* s2 = cp2->unresolved_string_at(index2);
aoqi@0 1026 if (s1 == s2) {
aoqi@0 1027 return true;
aoqi@0 1028 }
aoqi@0 1029 } break;
aoqi@0 1030
aoqi@0 1031 case JVM_CONSTANT_Utf8:
aoqi@0 1032 {
aoqi@0 1033 Symbol* s1 = symbol_at(index1);
aoqi@0 1034 Symbol* s2 = cp2->symbol_at(index2);
aoqi@0 1035 if (s1 == s2) {
aoqi@0 1036 return true;
aoqi@0 1037 }
aoqi@0 1038 } break;
aoqi@0 1039
aoqi@0 1040 // Invalid is used as the tag for the second constant pool entry
aoqi@0 1041 // occupied by JVM_CONSTANT_Double or JVM_CONSTANT_Long. It should
aoqi@0 1042 // not be seen by itself.
aoqi@0 1043 case JVM_CONSTANT_Invalid: // fall through
aoqi@0 1044
aoqi@0 1045 default:
aoqi@0 1046 ShouldNotReachHere();
aoqi@0 1047 break;
aoqi@0 1048 }
aoqi@0 1049
aoqi@0 1050 return false;
aoqi@0 1051 } // end compare_entry_to()
aoqi@0 1052
aoqi@0 1053
aoqi@0 1054 // Resize the operands array with delta_len and delta_size.
aoqi@0 1055 // Used in RedefineClasses for CP merge.
aoqi@0 1056 void ConstantPool::resize_operands(int delta_len, int delta_size, TRAPS) {
aoqi@0 1057 int old_len = operand_array_length(operands());
aoqi@0 1058 int new_len = old_len + delta_len;
aoqi@0 1059 int min_len = (delta_len > 0) ? old_len : new_len;
aoqi@0 1060
aoqi@0 1061 int old_size = operands()->length();
aoqi@0 1062 int new_size = old_size + delta_size;
aoqi@0 1063 int min_size = (delta_size > 0) ? old_size : new_size;
aoqi@0 1064
aoqi@0 1065 ClassLoaderData* loader_data = pool_holder()->class_loader_data();
aoqi@0 1066 Array<u2>* new_ops = MetadataFactory::new_array<u2>(loader_data, new_size, CHECK);
aoqi@0 1067
aoqi@0 1068 // Set index in the resized array for existing elements only
aoqi@0 1069 for (int idx = 0; idx < min_len; idx++) {
aoqi@0 1070 int offset = operand_offset_at(idx); // offset in original array
aoqi@0 1071 operand_offset_at_put(new_ops, idx, offset + 2*delta_len); // offset in resized array
aoqi@0 1072 }
aoqi@0 1073 // Copy the bootstrap specifiers only
aoqi@0 1074 Copy::conjoint_memory_atomic(operands()->adr_at(2*old_len),
aoqi@0 1075 new_ops->adr_at(2*new_len),
aoqi@0 1076 (min_size - 2*min_len) * sizeof(u2));
aoqi@0 1077 // Explicitly deallocate old operands array.
aoqi@0 1078 // Note, it is not needed for 7u backport.
aoqi@0 1079 if ( operands() != NULL) { // the safety check
aoqi@0 1080 MetadataFactory::free_array<u2>(loader_data, operands());
aoqi@0 1081 }
aoqi@0 1082 set_operands(new_ops);
aoqi@0 1083 } // end resize_operands()
aoqi@0 1084
aoqi@0 1085
aoqi@0 1086 // Extend the operands array with the length and size of the ext_cp operands.
aoqi@0 1087 // Used in RedefineClasses for CP merge.
aoqi@0 1088 void ConstantPool::extend_operands(constantPoolHandle ext_cp, TRAPS) {
aoqi@0 1089 int delta_len = operand_array_length(ext_cp->operands());
aoqi@0 1090 if (delta_len == 0) {
aoqi@0 1091 return; // nothing to do
aoqi@0 1092 }
aoqi@0 1093 int delta_size = ext_cp->operands()->length();
aoqi@0 1094
aoqi@0 1095 assert(delta_len > 0 && delta_size > 0, "extended operands array must be bigger");
aoqi@0 1096
aoqi@0 1097 if (operand_array_length(operands()) == 0) {
aoqi@0 1098 ClassLoaderData* loader_data = pool_holder()->class_loader_data();
aoqi@0 1099 Array<u2>* new_ops = MetadataFactory::new_array<u2>(loader_data, delta_size, CHECK);
aoqi@0 1100 // The first element index defines the offset of second part
aoqi@0 1101 operand_offset_at_put(new_ops, 0, 2*delta_len); // offset in new array
aoqi@0 1102 set_operands(new_ops);
aoqi@0 1103 } else {
aoqi@0 1104 resize_operands(delta_len, delta_size, CHECK);
aoqi@0 1105 }
aoqi@0 1106
aoqi@0 1107 } // end extend_operands()
aoqi@0 1108
aoqi@0 1109
aoqi@0 1110 // Shrink the operands array to a smaller array with new_len length.
aoqi@0 1111 // Used in RedefineClasses for CP merge.
aoqi@0 1112 void ConstantPool::shrink_operands(int new_len, TRAPS) {
aoqi@0 1113 int old_len = operand_array_length(operands());
aoqi@0 1114 if (new_len == old_len) {
aoqi@0 1115 return; // nothing to do
aoqi@0 1116 }
aoqi@0 1117 assert(new_len < old_len, "shrunken operands array must be smaller");
aoqi@0 1118
aoqi@0 1119 int free_base = operand_next_offset_at(new_len - 1);
aoqi@0 1120 int delta_len = new_len - old_len;
aoqi@0 1121 int delta_size = 2*delta_len + free_base - operands()->length();
aoqi@0 1122
aoqi@0 1123 resize_operands(delta_len, delta_size, CHECK);
aoqi@0 1124
aoqi@0 1125 } // end shrink_operands()
aoqi@0 1126
aoqi@0 1127
aoqi@0 1128 void ConstantPool::copy_operands(constantPoolHandle from_cp,
aoqi@0 1129 constantPoolHandle to_cp,
aoqi@0 1130 TRAPS) {
aoqi@0 1131
aoqi@0 1132 int from_oplen = operand_array_length(from_cp->operands());
aoqi@0 1133 int old_oplen = operand_array_length(to_cp->operands());
aoqi@0 1134 if (from_oplen != 0) {
aoqi@0 1135 ClassLoaderData* loader_data = to_cp->pool_holder()->class_loader_data();
aoqi@0 1136 // append my operands to the target's operands array
aoqi@0 1137 if (old_oplen == 0) {
aoqi@0 1138 // Can't just reuse from_cp's operand list because of deallocation issues
aoqi@0 1139 int len = from_cp->operands()->length();
aoqi@0 1140 Array<u2>* new_ops = MetadataFactory::new_array<u2>(loader_data, len, CHECK);
aoqi@0 1141 Copy::conjoint_memory_atomic(
aoqi@0 1142 from_cp->operands()->adr_at(0), new_ops->adr_at(0), len * sizeof(u2));
aoqi@0 1143 to_cp->set_operands(new_ops);
aoqi@0 1144 } else {
aoqi@0 1145 int old_len = to_cp->operands()->length();
aoqi@0 1146 int from_len = from_cp->operands()->length();
aoqi@0 1147 int old_off = old_oplen * sizeof(u2);
aoqi@0 1148 int from_off = from_oplen * sizeof(u2);
aoqi@0 1149 // Use the metaspace for the destination constant pool
aoqi@0 1150 Array<u2>* new_operands = MetadataFactory::new_array<u2>(loader_data, old_len + from_len, CHECK);
aoqi@0 1151 int fillp = 0, len = 0;
aoqi@0 1152 // first part of dest
aoqi@0 1153 Copy::conjoint_memory_atomic(to_cp->operands()->adr_at(0),
aoqi@0 1154 new_operands->adr_at(fillp),
aoqi@0 1155 (len = old_off) * sizeof(u2));
aoqi@0 1156 fillp += len;
aoqi@0 1157 // first part of src
aoqi@0 1158 Copy::conjoint_memory_atomic(from_cp->operands()->adr_at(0),
aoqi@0 1159 new_operands->adr_at(fillp),
aoqi@0 1160 (len = from_off) * sizeof(u2));
aoqi@0 1161 fillp += len;
aoqi@0 1162 // second part of dest
aoqi@0 1163 Copy::conjoint_memory_atomic(to_cp->operands()->adr_at(old_off),
aoqi@0 1164 new_operands->adr_at(fillp),
aoqi@0 1165 (len = old_len - old_off) * sizeof(u2));
aoqi@0 1166 fillp += len;
aoqi@0 1167 // second part of src
aoqi@0 1168 Copy::conjoint_memory_atomic(from_cp->operands()->adr_at(from_off),
aoqi@0 1169 new_operands->adr_at(fillp),
aoqi@0 1170 (len = from_len - from_off) * sizeof(u2));
aoqi@0 1171 fillp += len;
aoqi@0 1172 assert(fillp == new_operands->length(), "");
aoqi@0 1173
aoqi@0 1174 // Adjust indexes in the first part of the copied operands array.
aoqi@0 1175 for (int j = 0; j < from_oplen; j++) {
aoqi@0 1176 int offset = operand_offset_at(new_operands, old_oplen + j);
aoqi@0 1177 assert(offset == operand_offset_at(from_cp->operands(), j), "correct copy");
aoqi@0 1178 offset += old_len; // every new tuple is preceded by old_len extra u2's
aoqi@0 1179 operand_offset_at_put(new_operands, old_oplen + j, offset);
aoqi@0 1180 }
aoqi@0 1181
aoqi@0 1182 // replace target operands array with combined array
aoqi@0 1183 to_cp->set_operands(new_operands);
aoqi@0 1184 }
aoqi@0 1185 }
aoqi@0 1186 } // end copy_operands()
aoqi@0 1187
aoqi@0 1188
aoqi@0 1189 // Copy this constant pool's entries at start_i to end_i (inclusive)
aoqi@0 1190 // to the constant pool to_cp's entries starting at to_i. A total of
aoqi@0 1191 // (end_i - start_i) + 1 entries are copied.
aoqi@0 1192 void ConstantPool::copy_cp_to_impl(constantPoolHandle from_cp, int start_i, int end_i,
aoqi@0 1193 constantPoolHandle to_cp, int to_i, TRAPS) {
aoqi@0 1194
aoqi@0 1195
aoqi@0 1196 int dest_i = to_i; // leave original alone for debug purposes
aoqi@0 1197
aoqi@0 1198 for (int src_i = start_i; src_i <= end_i; /* see loop bottom */ ) {
aoqi@0 1199 copy_entry_to(from_cp, src_i, to_cp, dest_i, CHECK);
aoqi@0 1200
aoqi@0 1201 switch (from_cp->tag_at(src_i).value()) {
aoqi@0 1202 case JVM_CONSTANT_Double:
aoqi@0 1203 case JVM_CONSTANT_Long:
aoqi@0 1204 // double and long take two constant pool entries
aoqi@0 1205 src_i += 2;
aoqi@0 1206 dest_i += 2;
aoqi@0 1207 break;
aoqi@0 1208
aoqi@0 1209 default:
aoqi@0 1210 // all others take one constant pool entry
aoqi@0 1211 src_i++;
aoqi@0 1212 dest_i++;
aoqi@0 1213 break;
aoqi@0 1214 }
aoqi@0 1215 }
aoqi@0 1216 copy_operands(from_cp, to_cp, CHECK);
aoqi@0 1217
aoqi@0 1218 } // end copy_cp_to_impl()
aoqi@0 1219
aoqi@0 1220
aoqi@0 1221 // Copy this constant pool's entry at from_i to the constant pool
aoqi@0 1222 // to_cp's entry at to_i.
aoqi@0 1223 void ConstantPool::copy_entry_to(constantPoolHandle from_cp, int from_i,
aoqi@0 1224 constantPoolHandle to_cp, int to_i,
aoqi@0 1225 TRAPS) {
aoqi@0 1226
aoqi@0 1227 int tag = from_cp->tag_at(from_i).value();
aoqi@0 1228 switch (tag) {
aoqi@0 1229 case JVM_CONSTANT_Class:
aoqi@0 1230 {
aoqi@0 1231 Klass* k = from_cp->klass_at(from_i, CHECK);
aoqi@0 1232 to_cp->klass_at_put(to_i, k);
aoqi@0 1233 } break;
aoqi@0 1234
aoqi@0 1235 case JVM_CONSTANT_ClassIndex:
aoqi@0 1236 {
aoqi@0 1237 jint ki = from_cp->klass_index_at(from_i);
aoqi@0 1238 to_cp->klass_index_at_put(to_i, ki);
aoqi@0 1239 } break;
aoqi@0 1240
aoqi@0 1241 case JVM_CONSTANT_Double:
aoqi@0 1242 {
aoqi@0 1243 jdouble d = from_cp->double_at(from_i);
aoqi@0 1244 to_cp->double_at_put(to_i, d);
aoqi@0 1245 // double takes two constant pool entries so init second entry's tag
aoqi@0 1246 to_cp->tag_at_put(to_i + 1, JVM_CONSTANT_Invalid);
aoqi@0 1247 } break;
aoqi@0 1248
aoqi@0 1249 case JVM_CONSTANT_Fieldref:
aoqi@0 1250 {
aoqi@0 1251 int class_index = from_cp->uncached_klass_ref_index_at(from_i);
aoqi@0 1252 int name_and_type_index = from_cp->uncached_name_and_type_ref_index_at(from_i);
aoqi@0 1253 to_cp->field_at_put(to_i, class_index, name_and_type_index);
aoqi@0 1254 } break;
aoqi@0 1255
aoqi@0 1256 case JVM_CONSTANT_Float:
aoqi@0 1257 {
aoqi@0 1258 jfloat f = from_cp->float_at(from_i);
aoqi@0 1259 to_cp->float_at_put(to_i, f);
aoqi@0 1260 } break;
aoqi@0 1261
aoqi@0 1262 case JVM_CONSTANT_Integer:
aoqi@0 1263 {
aoqi@0 1264 jint i = from_cp->int_at(from_i);
aoqi@0 1265 to_cp->int_at_put(to_i, i);
aoqi@0 1266 } break;
aoqi@0 1267
aoqi@0 1268 case JVM_CONSTANT_InterfaceMethodref:
aoqi@0 1269 {
aoqi@0 1270 int class_index = from_cp->uncached_klass_ref_index_at(from_i);
aoqi@0 1271 int name_and_type_index = from_cp->uncached_name_and_type_ref_index_at(from_i);
aoqi@0 1272 to_cp->interface_method_at_put(to_i, class_index, name_and_type_index);
aoqi@0 1273 } break;
aoqi@0 1274
aoqi@0 1275 case JVM_CONSTANT_Long:
aoqi@0 1276 {
aoqi@0 1277 jlong l = from_cp->long_at(from_i);
aoqi@0 1278 to_cp->long_at_put(to_i, l);
aoqi@0 1279 // long takes two constant pool entries so init second entry's tag
aoqi@0 1280 to_cp->tag_at_put(to_i + 1, JVM_CONSTANT_Invalid);
aoqi@0 1281 } break;
aoqi@0 1282
aoqi@0 1283 case JVM_CONSTANT_Methodref:
aoqi@0 1284 {
aoqi@0 1285 int class_index = from_cp->uncached_klass_ref_index_at(from_i);
aoqi@0 1286 int name_and_type_index = from_cp->uncached_name_and_type_ref_index_at(from_i);
aoqi@0 1287 to_cp->method_at_put(to_i, class_index, name_and_type_index);
aoqi@0 1288 } break;
aoqi@0 1289
aoqi@0 1290 case JVM_CONSTANT_NameAndType:
aoqi@0 1291 {
aoqi@0 1292 int name_ref_index = from_cp->name_ref_index_at(from_i);
aoqi@0 1293 int signature_ref_index = from_cp->signature_ref_index_at(from_i);
aoqi@0 1294 to_cp->name_and_type_at_put(to_i, name_ref_index, signature_ref_index);
aoqi@0 1295 } break;
aoqi@0 1296
aoqi@0 1297 case JVM_CONSTANT_StringIndex:
aoqi@0 1298 {
aoqi@0 1299 jint si = from_cp->string_index_at(from_i);
aoqi@0 1300 to_cp->string_index_at_put(to_i, si);
aoqi@0 1301 } break;
aoqi@0 1302
aoqi@0 1303 case JVM_CONSTANT_UnresolvedClass:
aoqi@0 1304 case JVM_CONSTANT_UnresolvedClassInError:
aoqi@0 1305 {
aoqi@0 1306 // Can be resolved after checking tag, so check the slot first.
aoqi@0 1307 CPSlot entry = from_cp->slot_at(from_i);
aoqi@0 1308 if (entry.is_resolved()) {
aoqi@0 1309 assert(entry.get_klass()->is_klass(), "must be");
aoqi@0 1310 // Already resolved
aoqi@0 1311 to_cp->klass_at_put(to_i, entry.get_klass());
aoqi@0 1312 } else {
aoqi@0 1313 to_cp->unresolved_klass_at_put(to_i, entry.get_symbol());
aoqi@0 1314 }
aoqi@0 1315 } break;
aoqi@0 1316
aoqi@0 1317 case JVM_CONSTANT_String:
aoqi@0 1318 {
aoqi@0 1319 Symbol* s = from_cp->unresolved_string_at(from_i);
aoqi@0 1320 to_cp->unresolved_string_at_put(to_i, s);
aoqi@0 1321 } break;
aoqi@0 1322
aoqi@0 1323 case JVM_CONSTANT_Utf8:
aoqi@0 1324 {
aoqi@0 1325 Symbol* s = from_cp->symbol_at(from_i);
aoqi@0 1326 // Need to increase refcount, the old one will be thrown away and deferenced
aoqi@0 1327 s->increment_refcount();
aoqi@0 1328 to_cp->symbol_at_put(to_i, s);
aoqi@0 1329 } break;
aoqi@0 1330
aoqi@0 1331 case JVM_CONSTANT_MethodType:
aoqi@0 1332 case JVM_CONSTANT_MethodTypeInError:
aoqi@0 1333 {
aoqi@0 1334 jint k = from_cp->method_type_index_at_error_ok(from_i);
aoqi@0 1335 to_cp->method_type_index_at_put(to_i, k);
aoqi@0 1336 } break;
aoqi@0 1337
aoqi@0 1338 case JVM_CONSTANT_MethodHandle:
aoqi@0 1339 case JVM_CONSTANT_MethodHandleInError:
aoqi@0 1340 {
aoqi@0 1341 int k1 = from_cp->method_handle_ref_kind_at_error_ok(from_i);
aoqi@0 1342 int k2 = from_cp->method_handle_index_at_error_ok(from_i);
aoqi@0 1343 to_cp->method_handle_index_at_put(to_i, k1, k2);
aoqi@0 1344 } break;
aoqi@0 1345
aoqi@0 1346 case JVM_CONSTANT_InvokeDynamic:
aoqi@0 1347 {
aoqi@0 1348 int k1 = from_cp->invoke_dynamic_bootstrap_specifier_index(from_i);
aoqi@0 1349 int k2 = from_cp->invoke_dynamic_name_and_type_ref_index_at(from_i);
aoqi@0 1350 k1 += operand_array_length(to_cp->operands()); // to_cp might already have operands
aoqi@0 1351 to_cp->invoke_dynamic_at_put(to_i, k1, k2);
aoqi@0 1352 } break;
aoqi@0 1353
aoqi@0 1354 // Invalid is used as the tag for the second constant pool entry
aoqi@0 1355 // occupied by JVM_CONSTANT_Double or JVM_CONSTANT_Long. It should
aoqi@0 1356 // not be seen by itself.
aoqi@0 1357 case JVM_CONSTANT_Invalid: // fall through
aoqi@0 1358
aoqi@0 1359 default:
aoqi@0 1360 {
aoqi@0 1361 ShouldNotReachHere();
aoqi@0 1362 } break;
aoqi@0 1363 }
aoqi@0 1364 } // end copy_entry_to()
aoqi@0 1365
aoqi@0 1366
aoqi@0 1367 // Search constant pool search_cp for an entry that matches this
aoqi@0 1368 // constant pool's entry at pattern_i. Returns the index of a
aoqi@0 1369 // matching entry or zero (0) if there is no matching entry.
aoqi@0 1370 int ConstantPool::find_matching_entry(int pattern_i,
aoqi@0 1371 constantPoolHandle search_cp, TRAPS) {
aoqi@0 1372
aoqi@0 1373 // index zero (0) is not used
aoqi@0 1374 for (int i = 1; i < search_cp->length(); i++) {
aoqi@0 1375 bool found = compare_entry_to(pattern_i, search_cp, i, CHECK_0);
aoqi@0 1376 if (found) {
aoqi@0 1377 return i;
aoqi@0 1378 }
aoqi@0 1379 }
aoqi@0 1380
aoqi@0 1381 return 0; // entry not found; return unused index zero (0)
aoqi@0 1382 } // end find_matching_entry()
aoqi@0 1383
aoqi@0 1384
aoqi@0 1385 // Compare this constant pool's bootstrap specifier at idx1 to the constant pool
aoqi@0 1386 // cp2's bootstrap specifier at idx2.
aoqi@0 1387 bool ConstantPool::compare_operand_to(int idx1, constantPoolHandle cp2, int idx2, TRAPS) {
aoqi@0 1388 int k1 = operand_bootstrap_method_ref_index_at(idx1);
aoqi@0 1389 int k2 = cp2->operand_bootstrap_method_ref_index_at(idx2);
aoqi@0 1390 bool match = compare_entry_to(k1, cp2, k2, CHECK_false);
aoqi@0 1391
aoqi@0 1392 if (!match) {
aoqi@0 1393 return false;
aoqi@0 1394 }
aoqi@0 1395 int argc = operand_argument_count_at(idx1);
aoqi@0 1396 if (argc == cp2->operand_argument_count_at(idx2)) {
aoqi@0 1397 for (int j = 0; j < argc; j++) {
aoqi@0 1398 k1 = operand_argument_index_at(idx1, j);
aoqi@0 1399 k2 = cp2->operand_argument_index_at(idx2, j);
aoqi@0 1400 match = compare_entry_to(k1, cp2, k2, CHECK_false);
aoqi@0 1401 if (!match) {
aoqi@0 1402 return false;
aoqi@0 1403 }
aoqi@0 1404 }
aoqi@0 1405 return true; // got through loop; all elements equal
aoqi@0 1406 }
aoqi@0 1407 return false;
aoqi@0 1408 } // end compare_operand_to()
aoqi@0 1409
aoqi@0 1410 // Search constant pool search_cp for a bootstrap specifier that matches
aoqi@0 1411 // this constant pool's bootstrap specifier at pattern_i index.
aoqi@0 1412 // Return the index of a matching bootstrap specifier or (-1) if there is no match.
aoqi@0 1413 int ConstantPool::find_matching_operand(int pattern_i,
aoqi@0 1414 constantPoolHandle search_cp, int search_len, TRAPS) {
aoqi@0 1415 for (int i = 0; i < search_len; i++) {
aoqi@0 1416 bool found = compare_operand_to(pattern_i, search_cp, i, CHECK_(-1));
aoqi@0 1417 if (found) {
aoqi@0 1418 return i;
aoqi@0 1419 }
aoqi@0 1420 }
aoqi@0 1421 return -1; // bootstrap specifier not found; return unused index (-1)
aoqi@0 1422 } // end find_matching_operand()
aoqi@0 1423
aoqi@0 1424
aoqi@0 1425 #ifndef PRODUCT
aoqi@0 1426
aoqi@0 1427 const char* ConstantPool::printable_name_at(int which) {
aoqi@0 1428
aoqi@0 1429 constantTag tag = tag_at(which);
aoqi@0 1430
aoqi@0 1431 if (tag.is_string()) {
aoqi@0 1432 return string_at_noresolve(which);
aoqi@0 1433 } else if (tag.is_klass() || tag.is_unresolved_klass()) {
aoqi@0 1434 return klass_name_at(which)->as_C_string();
aoqi@0 1435 } else if (tag.is_symbol()) {
aoqi@0 1436 return symbol_at(which)->as_C_string();
aoqi@0 1437 }
aoqi@0 1438 return "";
aoqi@0 1439 }
aoqi@0 1440
aoqi@0 1441 #endif // PRODUCT
aoqi@0 1442
aoqi@0 1443
aoqi@0 1444 // JVMTI GetConstantPool support
aoqi@0 1445
aoqi@0 1446 // For debugging of constant pool
aoqi@0 1447 const bool debug_cpool = false;
aoqi@0 1448
aoqi@0 1449 #define DBG(code) do { if (debug_cpool) { (code); } } while(0)
aoqi@0 1450
aoqi@0 1451 static void print_cpool_bytes(jint cnt, u1 *bytes) {
aoqi@0 1452 const char* WARN_MSG = "Must not be such entry!";
aoqi@0 1453 jint size = 0;
aoqi@0 1454 u2 idx1, idx2;
aoqi@0 1455
aoqi@0 1456 for (jint idx = 1; idx < cnt; idx++) {
aoqi@0 1457 jint ent_size = 0;
aoqi@0 1458 u1 tag = *bytes++;
aoqi@0 1459 size++; // count tag
aoqi@0 1460
aoqi@0 1461 printf("const #%03d, tag: %02d ", idx, tag);
aoqi@0 1462 switch(tag) {
aoqi@0 1463 case JVM_CONSTANT_Invalid: {
aoqi@0 1464 printf("Invalid");
aoqi@0 1465 break;
aoqi@0 1466 }
aoqi@0 1467 case JVM_CONSTANT_Unicode: {
aoqi@0 1468 printf("Unicode %s", WARN_MSG);
aoqi@0 1469 break;
aoqi@0 1470 }
aoqi@0 1471 case JVM_CONSTANT_Utf8: {
aoqi@0 1472 u2 len = Bytes::get_Java_u2(bytes);
aoqi@0 1473 char str[128];
aoqi@0 1474 if (len > 127) {
aoqi@0 1475 len = 127;
aoqi@0 1476 }
aoqi@0 1477 strncpy(str, (char *) (bytes+2), len);
aoqi@0 1478 str[len] = '\0';
aoqi@0 1479 printf("Utf8 \"%s\"", str);
aoqi@0 1480 ent_size = 2 + len;
aoqi@0 1481 break;
aoqi@0 1482 }
aoqi@0 1483 case JVM_CONSTANT_Integer: {
aoqi@0 1484 u4 val = Bytes::get_Java_u4(bytes);
aoqi@0 1485 printf("int %d", *(int *) &val);
aoqi@0 1486 ent_size = 4;
aoqi@0 1487 break;
aoqi@0 1488 }
aoqi@0 1489 case JVM_CONSTANT_Float: {
aoqi@0 1490 u4 val = Bytes::get_Java_u4(bytes);
aoqi@0 1491 printf("float %5.3ff", *(float *) &val);
aoqi@0 1492 ent_size = 4;
aoqi@0 1493 break;
aoqi@0 1494 }
aoqi@0 1495 case JVM_CONSTANT_Long: {
aoqi@0 1496 u8 val = Bytes::get_Java_u8(bytes);
aoqi@0 1497 printf("long "INT64_FORMAT, (int64_t) *(jlong *) &val);
aoqi@0 1498 ent_size = 8;
aoqi@0 1499 idx++; // Long takes two cpool slots
aoqi@0 1500 break;
aoqi@0 1501 }
aoqi@0 1502 case JVM_CONSTANT_Double: {
aoqi@0 1503 u8 val = Bytes::get_Java_u8(bytes);
aoqi@0 1504 printf("double %5.3fd", *(jdouble *)&val);
aoqi@0 1505 ent_size = 8;
aoqi@0 1506 idx++; // Double takes two cpool slots
aoqi@0 1507 break;
aoqi@0 1508 }
aoqi@0 1509 case JVM_CONSTANT_Class: {
aoqi@0 1510 idx1 = Bytes::get_Java_u2(bytes);
aoqi@0 1511 printf("class #%03d", idx1);
aoqi@0 1512 ent_size = 2;
aoqi@0 1513 break;
aoqi@0 1514 }
aoqi@0 1515 case JVM_CONSTANT_String: {
aoqi@0 1516 idx1 = Bytes::get_Java_u2(bytes);
aoqi@0 1517 printf("String #%03d", idx1);
aoqi@0 1518 ent_size = 2;
aoqi@0 1519 break;
aoqi@0 1520 }
aoqi@0 1521 case JVM_CONSTANT_Fieldref: {
aoqi@0 1522 idx1 = Bytes::get_Java_u2(bytes);
aoqi@0 1523 idx2 = Bytes::get_Java_u2(bytes+2);
aoqi@0 1524 printf("Field #%03d, #%03d", (int) idx1, (int) idx2);
aoqi@0 1525 ent_size = 4;
aoqi@0 1526 break;
aoqi@0 1527 }
aoqi@0 1528 case JVM_CONSTANT_Methodref: {
aoqi@0 1529 idx1 = Bytes::get_Java_u2(bytes);
aoqi@0 1530 idx2 = Bytes::get_Java_u2(bytes+2);
aoqi@0 1531 printf("Method #%03d, #%03d", idx1, idx2);
aoqi@0 1532 ent_size = 4;
aoqi@0 1533 break;
aoqi@0 1534 }
aoqi@0 1535 case JVM_CONSTANT_InterfaceMethodref: {
aoqi@0 1536 idx1 = Bytes::get_Java_u2(bytes);
aoqi@0 1537 idx2 = Bytes::get_Java_u2(bytes+2);
aoqi@0 1538 printf("InterfMethod #%03d, #%03d", idx1, idx2);
aoqi@0 1539 ent_size = 4;
aoqi@0 1540 break;
aoqi@0 1541 }
aoqi@0 1542 case JVM_CONSTANT_NameAndType: {
aoqi@0 1543 idx1 = Bytes::get_Java_u2(bytes);
aoqi@0 1544 idx2 = Bytes::get_Java_u2(bytes+2);
aoqi@0 1545 printf("NameAndType #%03d, #%03d", idx1, idx2);
aoqi@0 1546 ent_size = 4;
aoqi@0 1547 break;
aoqi@0 1548 }
aoqi@0 1549 case JVM_CONSTANT_ClassIndex: {
aoqi@0 1550 printf("ClassIndex %s", WARN_MSG);
aoqi@0 1551 break;
aoqi@0 1552 }
aoqi@0 1553 case JVM_CONSTANT_UnresolvedClass: {
aoqi@0 1554 printf("UnresolvedClass: %s", WARN_MSG);
aoqi@0 1555 break;
aoqi@0 1556 }
aoqi@0 1557 case JVM_CONSTANT_UnresolvedClassInError: {
aoqi@0 1558 printf("UnresolvedClassInErr: %s", WARN_MSG);
aoqi@0 1559 break;
aoqi@0 1560 }
aoqi@0 1561 case JVM_CONSTANT_StringIndex: {
aoqi@0 1562 printf("StringIndex: %s", WARN_MSG);
aoqi@0 1563 break;
aoqi@0 1564 }
aoqi@0 1565 }
aoqi@0 1566 printf(";\n");
aoqi@0 1567 bytes += ent_size;
aoqi@0 1568 size += ent_size;
aoqi@0 1569 }
aoqi@0 1570 printf("Cpool size: %d\n", size);
aoqi@0 1571 fflush(0);
aoqi@0 1572 return;
aoqi@0 1573 } /* end print_cpool_bytes */
aoqi@0 1574
aoqi@0 1575
aoqi@0 1576 // Returns size of constant pool entry.
aoqi@0 1577 jint ConstantPool::cpool_entry_size(jint idx) {
aoqi@0 1578 switch(tag_at(idx).value()) {
aoqi@0 1579 case JVM_CONSTANT_Invalid:
aoqi@0 1580 case JVM_CONSTANT_Unicode:
aoqi@0 1581 return 1;
aoqi@0 1582
aoqi@0 1583 case JVM_CONSTANT_Utf8:
aoqi@0 1584 return 3 + symbol_at(idx)->utf8_length();
aoqi@0 1585
aoqi@0 1586 case JVM_CONSTANT_Class:
aoqi@0 1587 case JVM_CONSTANT_String:
aoqi@0 1588 case JVM_CONSTANT_ClassIndex:
aoqi@0 1589 case JVM_CONSTANT_UnresolvedClass:
aoqi@0 1590 case JVM_CONSTANT_UnresolvedClassInError:
aoqi@0 1591 case JVM_CONSTANT_StringIndex:
aoqi@0 1592 case JVM_CONSTANT_MethodType:
aoqi@0 1593 case JVM_CONSTANT_MethodTypeInError:
aoqi@0 1594 return 3;
aoqi@0 1595
aoqi@0 1596 case JVM_CONSTANT_MethodHandle:
aoqi@0 1597 case JVM_CONSTANT_MethodHandleInError:
aoqi@0 1598 return 4; //tag, ref_kind, ref_index
aoqi@0 1599
aoqi@0 1600 case JVM_CONSTANT_Integer:
aoqi@0 1601 case JVM_CONSTANT_Float:
aoqi@0 1602 case JVM_CONSTANT_Fieldref:
aoqi@0 1603 case JVM_CONSTANT_Methodref:
aoqi@0 1604 case JVM_CONSTANT_InterfaceMethodref:
aoqi@0 1605 case JVM_CONSTANT_NameAndType:
aoqi@0 1606 return 5;
aoqi@0 1607
aoqi@0 1608 case JVM_CONSTANT_InvokeDynamic:
aoqi@0 1609 // u1 tag, u2 bsm, u2 nt
aoqi@0 1610 return 5;
aoqi@0 1611
aoqi@0 1612 case JVM_CONSTANT_Long:
aoqi@0 1613 case JVM_CONSTANT_Double:
aoqi@0 1614 return 9;
aoqi@0 1615 }
aoqi@0 1616 assert(false, "cpool_entry_size: Invalid constant pool entry tag");
aoqi@0 1617 return 1;
aoqi@0 1618 } /* end cpool_entry_size */
aoqi@0 1619
aoqi@0 1620
aoqi@0 1621 // SymbolHashMap is used to find a constant pool index from a string.
aoqi@0 1622 // This function fills in SymbolHashMaps, one for utf8s and one for
aoqi@0 1623 // class names, returns size of the cpool raw bytes.
aoqi@0 1624 jint ConstantPool::hash_entries_to(SymbolHashMap *symmap,
aoqi@0 1625 SymbolHashMap *classmap) {
aoqi@0 1626 jint size = 0;
aoqi@0 1627
aoqi@0 1628 for (u2 idx = 1; idx < length(); idx++) {
aoqi@0 1629 u2 tag = tag_at(idx).value();
aoqi@0 1630 size += cpool_entry_size(idx);
aoqi@0 1631
aoqi@0 1632 switch(tag) {
aoqi@0 1633 case JVM_CONSTANT_Utf8: {
aoqi@0 1634 Symbol* sym = symbol_at(idx);
aoqi@0 1635 symmap->add_entry(sym, idx);
aoqi@0 1636 DBG(printf("adding symbol entry %s = %d\n", sym->as_utf8(), idx));
aoqi@0 1637 break;
aoqi@0 1638 }
aoqi@0 1639 case JVM_CONSTANT_Class:
aoqi@0 1640 case JVM_CONSTANT_UnresolvedClass:
aoqi@0 1641 case JVM_CONSTANT_UnresolvedClassInError: {
aoqi@0 1642 Symbol* sym = klass_name_at(idx);
aoqi@0 1643 classmap->add_entry(sym, idx);
aoqi@0 1644 DBG(printf("adding class entry %s = %d\n", sym->as_utf8(), idx));
aoqi@0 1645 break;
aoqi@0 1646 }
aoqi@0 1647 case JVM_CONSTANT_Long:
aoqi@0 1648 case JVM_CONSTANT_Double: {
aoqi@0 1649 idx++; // Both Long and Double take two cpool slots
aoqi@0 1650 break;
aoqi@0 1651 }
aoqi@0 1652 }
aoqi@0 1653 }
aoqi@0 1654 return size;
aoqi@0 1655 } /* end hash_utf8_entries_to */
aoqi@0 1656
aoqi@0 1657
aoqi@0 1658 // Copy cpool bytes.
aoqi@0 1659 // Returns:
aoqi@0 1660 // 0, in case of OutOfMemoryError
aoqi@0 1661 // -1, in case of internal error
aoqi@0 1662 // > 0, count of the raw cpool bytes that have been copied
aoqi@0 1663 int ConstantPool::copy_cpool_bytes(int cpool_size,
aoqi@0 1664 SymbolHashMap* tbl,
aoqi@0 1665 unsigned char *bytes) {
aoqi@0 1666 u2 idx1, idx2;
aoqi@0 1667 jint size = 0;
aoqi@0 1668 jint cnt = length();
aoqi@0 1669 unsigned char *start_bytes = bytes;
aoqi@0 1670
aoqi@0 1671 for (jint idx = 1; idx < cnt; idx++) {
aoqi@0 1672 u1 tag = tag_at(idx).value();
aoqi@0 1673 jint ent_size = cpool_entry_size(idx);
aoqi@0 1674
aoqi@0 1675 assert(size + ent_size <= cpool_size, "Size mismatch");
aoqi@0 1676
aoqi@0 1677 *bytes = tag;
aoqi@0 1678 DBG(printf("#%03hd tag=%03hd, ", idx, tag));
aoqi@0 1679 switch(tag) {
aoqi@0 1680 case JVM_CONSTANT_Invalid: {
aoqi@0 1681 DBG(printf("JVM_CONSTANT_Invalid"));
aoqi@0 1682 break;
aoqi@0 1683 }
aoqi@0 1684 case JVM_CONSTANT_Unicode: {
aoqi@0 1685 assert(false, "Wrong constant pool tag: JVM_CONSTANT_Unicode");
aoqi@0 1686 DBG(printf("JVM_CONSTANT_Unicode"));
aoqi@0 1687 break;
aoqi@0 1688 }
aoqi@0 1689 case JVM_CONSTANT_Utf8: {
aoqi@0 1690 Symbol* sym = symbol_at(idx);
aoqi@0 1691 char* str = sym->as_utf8();
aoqi@0 1692 // Warning! It's crashing on x86 with len = sym->utf8_length()
aoqi@0 1693 int len = (int) strlen(str);
aoqi@0 1694 Bytes::put_Java_u2((address) (bytes+1), (u2) len);
aoqi@0 1695 for (int i = 0; i < len; i++) {
aoqi@0 1696 bytes[3+i] = (u1) str[i];
aoqi@0 1697 }
aoqi@0 1698 DBG(printf("JVM_CONSTANT_Utf8: %s ", str));
aoqi@0 1699 break;
aoqi@0 1700 }
aoqi@0 1701 case JVM_CONSTANT_Integer: {
aoqi@0 1702 jint val = int_at(idx);
aoqi@0 1703 Bytes::put_Java_u4((address) (bytes+1), *(u4*)&val);
aoqi@0 1704 break;
aoqi@0 1705 }
aoqi@0 1706 case JVM_CONSTANT_Float: {
aoqi@0 1707 jfloat val = float_at(idx);
aoqi@0 1708 Bytes::put_Java_u4((address) (bytes+1), *(u4*)&val);
aoqi@0 1709 break;
aoqi@0 1710 }
aoqi@0 1711 case JVM_CONSTANT_Long: {
aoqi@0 1712 jlong val = long_at(idx);
aoqi@0 1713 Bytes::put_Java_u8((address) (bytes+1), *(u8*)&val);
aoqi@0 1714 idx++; // Long takes two cpool slots
aoqi@0 1715 break;
aoqi@0 1716 }
aoqi@0 1717 case JVM_CONSTANT_Double: {
aoqi@0 1718 jdouble val = double_at(idx);
aoqi@0 1719 Bytes::put_Java_u8((address) (bytes+1), *(u8*)&val);
aoqi@0 1720 idx++; // Double takes two cpool slots
aoqi@0 1721 break;
aoqi@0 1722 }
aoqi@0 1723 case JVM_CONSTANT_Class:
aoqi@0 1724 case JVM_CONSTANT_UnresolvedClass:
aoqi@0 1725 case JVM_CONSTANT_UnresolvedClassInError: {
aoqi@0 1726 *bytes = JVM_CONSTANT_Class;
aoqi@0 1727 Symbol* sym = klass_name_at(idx);
aoqi@0 1728 idx1 = tbl->symbol_to_value(sym);
aoqi@0 1729 assert(idx1 != 0, "Have not found a hashtable entry");
aoqi@0 1730 Bytes::put_Java_u2((address) (bytes+1), idx1);
aoqi@0 1731 DBG(printf("JVM_CONSTANT_Class: idx=#%03hd, %s", idx1, sym->as_utf8()));
aoqi@0 1732 break;
aoqi@0 1733 }
aoqi@0 1734 case JVM_CONSTANT_String: {
aoqi@0 1735 *bytes = JVM_CONSTANT_String;
aoqi@0 1736 Symbol* sym = unresolved_string_at(idx);
aoqi@0 1737 idx1 = tbl->symbol_to_value(sym);
aoqi@0 1738 assert(idx1 != 0, "Have not found a hashtable entry");
aoqi@0 1739 Bytes::put_Java_u2((address) (bytes+1), idx1);
aoqi@0 1740 DBG(printf("JVM_CONSTANT_String: idx=#%03hd, %s", idx1, sym->as_utf8()));
aoqi@0 1741 break;
aoqi@0 1742 }
aoqi@0 1743 case JVM_CONSTANT_Fieldref:
aoqi@0 1744 case JVM_CONSTANT_Methodref:
aoqi@0 1745 case JVM_CONSTANT_InterfaceMethodref: {
aoqi@0 1746 idx1 = uncached_klass_ref_index_at(idx);
aoqi@0 1747 idx2 = uncached_name_and_type_ref_index_at(idx);
aoqi@0 1748 Bytes::put_Java_u2((address) (bytes+1), idx1);
aoqi@0 1749 Bytes::put_Java_u2((address) (bytes+3), idx2);
aoqi@0 1750 DBG(printf("JVM_CONSTANT_Methodref: %hd %hd", idx1, idx2));
aoqi@0 1751 break;
aoqi@0 1752 }
aoqi@0 1753 case JVM_CONSTANT_NameAndType: {
aoqi@0 1754 idx1 = name_ref_index_at(idx);
aoqi@0 1755 idx2 = signature_ref_index_at(idx);
aoqi@0 1756 Bytes::put_Java_u2((address) (bytes+1), idx1);
aoqi@0 1757 Bytes::put_Java_u2((address) (bytes+3), idx2);
aoqi@0 1758 DBG(printf("JVM_CONSTANT_NameAndType: %hd %hd", idx1, idx2));
aoqi@0 1759 break;
aoqi@0 1760 }
aoqi@0 1761 case JVM_CONSTANT_ClassIndex: {
aoqi@0 1762 *bytes = JVM_CONSTANT_Class;
aoqi@0 1763 idx1 = klass_index_at(idx);
aoqi@0 1764 Bytes::put_Java_u2((address) (bytes+1), idx1);
aoqi@0 1765 DBG(printf("JVM_CONSTANT_ClassIndex: %hd", idx1));
aoqi@0 1766 break;
aoqi@0 1767 }
aoqi@0 1768 case JVM_CONSTANT_StringIndex: {
aoqi@0 1769 *bytes = JVM_CONSTANT_String;
aoqi@0 1770 idx1 = string_index_at(idx);
aoqi@0 1771 Bytes::put_Java_u2((address) (bytes+1), idx1);
aoqi@0 1772 DBG(printf("JVM_CONSTANT_StringIndex: %hd", idx1));
aoqi@0 1773 break;
aoqi@0 1774 }
aoqi@0 1775 case JVM_CONSTANT_MethodHandle:
aoqi@0 1776 case JVM_CONSTANT_MethodHandleInError: {
aoqi@0 1777 *bytes = JVM_CONSTANT_MethodHandle;
aoqi@0 1778 int kind = method_handle_ref_kind_at_error_ok(idx);
aoqi@0 1779 idx1 = method_handle_index_at_error_ok(idx);
aoqi@0 1780 *(bytes+1) = (unsigned char) kind;
aoqi@0 1781 Bytes::put_Java_u2((address) (bytes+2), idx1);
aoqi@0 1782 DBG(printf("JVM_CONSTANT_MethodHandle: %d %hd", kind, idx1));
aoqi@0 1783 break;
aoqi@0 1784 }
aoqi@0 1785 case JVM_CONSTANT_MethodType:
aoqi@0 1786 case JVM_CONSTANT_MethodTypeInError: {
aoqi@0 1787 *bytes = JVM_CONSTANT_MethodType;
aoqi@0 1788 idx1 = method_type_index_at_error_ok(idx);
aoqi@0 1789 Bytes::put_Java_u2((address) (bytes+1), idx1);
aoqi@0 1790 DBG(printf("JVM_CONSTANT_MethodType: %hd", idx1));
aoqi@0 1791 break;
aoqi@0 1792 }
aoqi@0 1793 case JVM_CONSTANT_InvokeDynamic: {
aoqi@0 1794 *bytes = tag;
aoqi@0 1795 idx1 = extract_low_short_from_int(*int_at_addr(idx));
aoqi@0 1796 idx2 = extract_high_short_from_int(*int_at_addr(idx));
aoqi@0 1797 assert(idx2 == invoke_dynamic_name_and_type_ref_index_at(idx), "correct half of u4");
aoqi@0 1798 Bytes::put_Java_u2((address) (bytes+1), idx1);
aoqi@0 1799 Bytes::put_Java_u2((address) (bytes+3), idx2);
aoqi@0 1800 DBG(printf("JVM_CONSTANT_InvokeDynamic: %hd %hd", idx1, idx2));
aoqi@0 1801 break;
aoqi@0 1802 }
aoqi@0 1803 }
aoqi@0 1804 DBG(printf("\n"));
aoqi@0 1805 bytes += ent_size;
aoqi@0 1806 size += ent_size;
aoqi@0 1807 }
aoqi@0 1808 assert(size == cpool_size, "Size mismatch");
aoqi@0 1809
aoqi@0 1810 // Keep temorarily for debugging until it's stable.
aoqi@0 1811 DBG(print_cpool_bytes(cnt, start_bytes));
aoqi@0 1812 return (int)(bytes - start_bytes);
aoqi@0 1813 } /* end copy_cpool_bytes */
aoqi@0 1814
aoqi@0 1815 #undef DBG
aoqi@0 1816
aoqi@0 1817
aoqi@0 1818 void ConstantPool::set_on_stack(const bool value) {
aoqi@0 1819 if (value) {
stefank@7333 1820 int old_flags = *const_cast<volatile int *>(&_flags);
stefank@7333 1821 while ((old_flags & _on_stack) == 0) {
stefank@7333 1822 int new_flags = old_flags | _on_stack;
stefank@7333 1823 int result = Atomic::cmpxchg(new_flags, &_flags, old_flags);
stefank@7333 1824
stefank@7333 1825 if (result == old_flags) {
stefank@7333 1826 // Succeeded.
stefank@7333 1827 MetadataOnStackMark::record(this, Thread::current());
stefank@7333 1828 return;
stefank@7333 1829 }
stefank@7333 1830 old_flags = result;
stefank@7333 1831 }
aoqi@0 1832 } else {
stefank@7333 1833 // Clearing is done single-threadedly.
aoqi@0 1834 _flags &= ~_on_stack;
aoqi@0 1835 }
aoqi@0 1836 }
aoqi@0 1837
aoqi@0 1838 // JSR 292 support for patching constant pool oops after the class is linked and
aoqi@0 1839 // the oop array for resolved references are created.
aoqi@0 1840 // We can't do this during classfile parsing, which is how the other indexes are
aoqi@0 1841 // patched. The other patches are applied early for some error checking
aoqi@0 1842 // so only defer the pseudo_strings.
aoqi@0 1843 void ConstantPool::patch_resolved_references(
aoqi@0 1844 GrowableArray<Handle>* cp_patches) {
aoqi@0 1845 assert(EnableInvokeDynamic, "");
aoqi@0 1846 for (int index = 1; index < cp_patches->length(); index++) { // Index 0 is unused
aoqi@0 1847 Handle patch = cp_patches->at(index);
aoqi@0 1848 if (patch.not_null()) {
aoqi@0 1849 assert (tag_at(index).is_string(), "should only be string left");
aoqi@0 1850 // Patching a string means pre-resolving it.
aoqi@0 1851 // The spelling in the constant pool is ignored.
aoqi@0 1852 // The constant reference may be any object whatever.
aoqi@0 1853 // If it is not a real interned string, the constant is referred
aoqi@0 1854 // to as a "pseudo-string", and must be presented to the CP
aoqi@0 1855 // explicitly, because it may require scavenging.
aoqi@0 1856 int obj_index = cp_to_object_index(index);
aoqi@0 1857 pseudo_string_at_put(index, obj_index, patch());
aoqi@0 1858 DEBUG_ONLY(cp_patches->at_put(index, Handle());)
aoqi@0 1859 }
aoqi@0 1860 }
aoqi@0 1861 #ifdef ASSERT
aoqi@0 1862 // Ensure that all the patches have been used.
aoqi@0 1863 for (int index = 0; index < cp_patches->length(); index++) {
aoqi@0 1864 assert(cp_patches->at(index).is_null(),
aoqi@0 1865 err_msg("Unused constant pool patch at %d in class file %s",
aoqi@0 1866 index,
aoqi@0 1867 pool_holder()->external_name()));
aoqi@0 1868 }
aoqi@0 1869 #endif // ASSERT
aoqi@0 1870 }
aoqi@0 1871
aoqi@0 1872 #ifndef PRODUCT
aoqi@0 1873
aoqi@0 1874 // CompileTheWorld support. Preload all classes loaded references in the passed in constantpool
aoqi@0 1875 void ConstantPool::preload_and_initialize_all_classes(ConstantPool* obj, TRAPS) {
aoqi@0 1876 guarantee(obj->is_constantPool(), "object must be constant pool");
aoqi@0 1877 constantPoolHandle cp(THREAD, (ConstantPool*)obj);
aoqi@0 1878 guarantee(cp->pool_holder() != NULL, "must be fully loaded");
aoqi@0 1879
aoqi@0 1880 for (int i = 0; i< cp->length(); i++) {
aoqi@0 1881 if (cp->tag_at(i).is_unresolved_klass()) {
aoqi@0 1882 // This will force loading of the class
aoqi@0 1883 Klass* klass = cp->klass_at(i, CHECK);
aoqi@0 1884 if (klass->oop_is_instance()) {
aoqi@0 1885 // Force initialization of class
aoqi@0 1886 InstanceKlass::cast(klass)->initialize(CHECK);
aoqi@0 1887 }
aoqi@0 1888 }
aoqi@0 1889 }
aoqi@0 1890 }
aoqi@0 1891
aoqi@0 1892 #endif
aoqi@0 1893
aoqi@0 1894
aoqi@0 1895 // Printing
aoqi@0 1896
aoqi@0 1897 void ConstantPool::print_on(outputStream* st) const {
aoqi@0 1898 EXCEPTION_MARK;
aoqi@0 1899 assert(is_constantPool(), "must be constantPool");
aoqi@0 1900 st->print_cr("%s", internal_name());
aoqi@0 1901 if (flags() != 0) {
aoqi@0 1902 st->print(" - flags: 0x%x", flags());
aoqi@0 1903 if (has_preresolution()) st->print(" has_preresolution");
aoqi@0 1904 if (on_stack()) st->print(" on_stack");
aoqi@0 1905 st->cr();
aoqi@0 1906 }
aoqi@0 1907 if (pool_holder() != NULL) {
aoqi@0 1908 st->print_cr(" - holder: " INTPTR_FORMAT, pool_holder());
aoqi@0 1909 }
aoqi@0 1910 st->print_cr(" - cache: " INTPTR_FORMAT, cache());
aoqi@0 1911 st->print_cr(" - resolved_references: " INTPTR_FORMAT, (void *)resolved_references());
aoqi@0 1912 st->print_cr(" - reference_map: " INTPTR_FORMAT, reference_map());
aoqi@0 1913
aoqi@0 1914 for (int index = 1; index < length(); index++) { // Index 0 is unused
aoqi@0 1915 ((ConstantPool*)this)->print_entry_on(index, st);
aoqi@0 1916 switch (tag_at(index).value()) {
aoqi@0 1917 case JVM_CONSTANT_Long :
aoqi@0 1918 case JVM_CONSTANT_Double :
aoqi@0 1919 index++; // Skip entry following eigth-byte constant
aoqi@0 1920 }
aoqi@0 1921
aoqi@0 1922 }
aoqi@0 1923 st->cr();
aoqi@0 1924 }
aoqi@0 1925
aoqi@0 1926 // Print one constant pool entry
aoqi@0 1927 void ConstantPool::print_entry_on(const int index, outputStream* st) {
aoqi@0 1928 EXCEPTION_MARK;
aoqi@0 1929 st->print(" - %3d : ", index);
aoqi@0 1930 tag_at(index).print_on(st);
aoqi@0 1931 st->print(" : ");
aoqi@0 1932 switch (tag_at(index).value()) {
aoqi@0 1933 case JVM_CONSTANT_Class :
aoqi@0 1934 { Klass* k = klass_at(index, CATCH);
aoqi@0 1935 guarantee(k != NULL, "need klass");
aoqi@0 1936 k->print_value_on(st);
aoqi@0 1937 st->print(" {0x%lx}", (address)k);
aoqi@0 1938 }
aoqi@0 1939 break;
aoqi@0 1940 case JVM_CONSTANT_Fieldref :
aoqi@0 1941 case JVM_CONSTANT_Methodref :
aoqi@0 1942 case JVM_CONSTANT_InterfaceMethodref :
aoqi@0 1943 st->print("klass_index=%d", uncached_klass_ref_index_at(index));
aoqi@0 1944 st->print(" name_and_type_index=%d", uncached_name_and_type_ref_index_at(index));
aoqi@0 1945 break;
aoqi@0 1946 case JVM_CONSTANT_String :
aoqi@0 1947 if (is_pseudo_string_at(index)) {
aoqi@0 1948 oop anObj = pseudo_string_at(index);
aoqi@0 1949 anObj->print_value_on(st);
aoqi@0 1950 st->print(" {0x%lx}", (address)anObj);
aoqi@0 1951 } else {
aoqi@0 1952 unresolved_string_at(index)->print_value_on(st);
aoqi@0 1953 }
aoqi@0 1954 break;
aoqi@0 1955 case JVM_CONSTANT_Integer :
aoqi@0 1956 st->print("%d", int_at(index));
aoqi@0 1957 break;
aoqi@0 1958 case JVM_CONSTANT_Float :
aoqi@0 1959 st->print("%f", float_at(index));
aoqi@0 1960 break;
aoqi@0 1961 case JVM_CONSTANT_Long :
aoqi@0 1962 st->print_jlong(long_at(index));
aoqi@0 1963 break;
aoqi@0 1964 case JVM_CONSTANT_Double :
aoqi@0 1965 st->print("%lf", double_at(index));
aoqi@0 1966 break;
aoqi@0 1967 case JVM_CONSTANT_NameAndType :
aoqi@0 1968 st->print("name_index=%d", name_ref_index_at(index));
aoqi@0 1969 st->print(" signature_index=%d", signature_ref_index_at(index));
aoqi@0 1970 break;
aoqi@0 1971 case JVM_CONSTANT_Utf8 :
aoqi@0 1972 symbol_at(index)->print_value_on(st);
aoqi@0 1973 break;
aoqi@0 1974 case JVM_CONSTANT_UnresolvedClass : // fall-through
aoqi@0 1975 case JVM_CONSTANT_UnresolvedClassInError: {
aoqi@0 1976 // unresolved_klass_at requires lock or safe world.
aoqi@0 1977 CPSlot entry = slot_at(index);
aoqi@0 1978 if (entry.is_resolved()) {
aoqi@0 1979 entry.get_klass()->print_value_on(st);
aoqi@0 1980 } else {
aoqi@0 1981 entry.get_symbol()->print_value_on(st);
aoqi@0 1982 }
aoqi@0 1983 }
aoqi@0 1984 break;
aoqi@0 1985 case JVM_CONSTANT_MethodHandle :
aoqi@0 1986 case JVM_CONSTANT_MethodHandleInError :
aoqi@0 1987 st->print("ref_kind=%d", method_handle_ref_kind_at_error_ok(index));
aoqi@0 1988 st->print(" ref_index=%d", method_handle_index_at_error_ok(index));
aoqi@0 1989 break;
aoqi@0 1990 case JVM_CONSTANT_MethodType :
aoqi@0 1991 case JVM_CONSTANT_MethodTypeInError :
aoqi@0 1992 st->print("signature_index=%d", method_type_index_at_error_ok(index));
aoqi@0 1993 break;
aoqi@0 1994 case JVM_CONSTANT_InvokeDynamic :
aoqi@0 1995 {
aoqi@0 1996 st->print("bootstrap_method_index=%d", invoke_dynamic_bootstrap_method_ref_index_at(index));
aoqi@0 1997 st->print(" name_and_type_index=%d", invoke_dynamic_name_and_type_ref_index_at(index));
aoqi@0 1998 int argc = invoke_dynamic_argument_count_at(index);
aoqi@0 1999 if (argc > 0) {
aoqi@0 2000 for (int arg_i = 0; arg_i < argc; arg_i++) {
aoqi@0 2001 int arg = invoke_dynamic_argument_index_at(index, arg_i);
aoqi@0 2002 st->print((arg_i == 0 ? " arguments={%d" : ", %d"), arg);
aoqi@0 2003 }
aoqi@0 2004 st->print("}");
aoqi@0 2005 }
aoqi@0 2006 }
aoqi@0 2007 break;
aoqi@0 2008 default:
aoqi@0 2009 ShouldNotReachHere();
aoqi@0 2010 break;
aoqi@0 2011 }
aoqi@0 2012 st->cr();
aoqi@0 2013 }
aoqi@0 2014
aoqi@0 2015 void ConstantPool::print_value_on(outputStream* st) const {
aoqi@0 2016 assert(is_constantPool(), "must be constantPool");
aoqi@0 2017 st->print("constant pool [%d]", length());
aoqi@0 2018 if (has_preresolution()) st->print("/preresolution");
aoqi@0 2019 if (operands() != NULL) st->print("/operands[%d]", operands()->length());
aoqi@0 2020 print_address_on(st);
aoqi@0 2021 st->print(" for ");
aoqi@0 2022 pool_holder()->print_value_on(st);
aoqi@0 2023 if (pool_holder() != NULL) {
aoqi@0 2024 bool extra = (pool_holder()->constants() != this);
aoqi@0 2025 if (extra) st->print(" (extra)");
aoqi@0 2026 }
aoqi@0 2027 if (cache() != NULL) {
aoqi@0 2028 st->print(" cache=" PTR_FORMAT, cache());
aoqi@0 2029 }
aoqi@0 2030 }
aoqi@0 2031
aoqi@0 2032 #if INCLUDE_SERVICES
aoqi@0 2033 // Size Statistics
aoqi@0 2034 void ConstantPool::collect_statistics(KlassSizeStats *sz) const {
aoqi@0 2035 sz->_cp_all_bytes += (sz->_cp_bytes = sz->count(this));
aoqi@0 2036 sz->_cp_all_bytes += (sz->_cp_tags_bytes = sz->count_array(tags()));
aoqi@0 2037 sz->_cp_all_bytes += (sz->_cp_cache_bytes = sz->count(cache()));
aoqi@0 2038 sz->_cp_all_bytes += (sz->_cp_operands_bytes = sz->count_array(operands()));
aoqi@0 2039 sz->_cp_all_bytes += (sz->_cp_refmap_bytes = sz->count_array(reference_map()));
aoqi@0 2040
aoqi@0 2041 sz->_ro_bytes += sz->_cp_operands_bytes + sz->_cp_tags_bytes +
aoqi@0 2042 sz->_cp_refmap_bytes;
aoqi@0 2043 sz->_rw_bytes += sz->_cp_bytes + sz->_cp_cache_bytes;
aoqi@0 2044 }
aoqi@0 2045 #endif // INCLUDE_SERVICES
aoqi@0 2046
aoqi@0 2047 // Verification
aoqi@0 2048
aoqi@0 2049 void ConstantPool::verify_on(outputStream* st) {
aoqi@0 2050 guarantee(is_constantPool(), "object must be constant pool");
aoqi@0 2051 for (int i = 0; i< length(); i++) {
aoqi@0 2052 constantTag tag = tag_at(i);
aoqi@0 2053 CPSlot entry = slot_at(i);
aoqi@0 2054 if (tag.is_klass()) {
aoqi@0 2055 if (entry.is_resolved()) {
aoqi@0 2056 guarantee(entry.get_klass()->is_klass(), "should be klass");
aoqi@0 2057 }
aoqi@0 2058 } else if (tag.is_unresolved_klass()) {
aoqi@0 2059 if (entry.is_resolved()) {
aoqi@0 2060 guarantee(entry.get_klass()->is_klass(), "should be klass");
aoqi@0 2061 }
aoqi@0 2062 } else if (tag.is_symbol()) {
aoqi@0 2063 guarantee(entry.get_symbol()->refcount() != 0, "should have nonzero reference count");
aoqi@0 2064 } else if (tag.is_string()) {
aoqi@0 2065 guarantee(entry.get_symbol()->refcount() != 0, "should have nonzero reference count");
aoqi@0 2066 }
aoqi@0 2067 }
aoqi@0 2068 if (cache() != NULL) {
aoqi@0 2069 // Note: cache() can be NULL before a class is completely setup or
aoqi@0 2070 // in temporary constant pools used during constant pool merging
aoqi@0 2071 guarantee(cache()->is_constantPoolCache(), "should be constant pool cache");
aoqi@0 2072 }
aoqi@0 2073 if (pool_holder() != NULL) {
aoqi@0 2074 // Note: pool_holder() can be NULL in temporary constant pools
aoqi@0 2075 // used during constant pool merging
aoqi@0 2076 guarantee(pool_holder()->is_klass(), "should be klass");
aoqi@0 2077 }
aoqi@0 2078 }
aoqi@0 2079
aoqi@0 2080
aoqi@0 2081 void SymbolHashMap::add_entry(Symbol* sym, u2 value) {
aoqi@0 2082 char *str = sym->as_utf8();
aoqi@0 2083 unsigned int hash = compute_hash(str, sym->utf8_length());
aoqi@0 2084 unsigned int index = hash % table_size();
aoqi@0 2085
aoqi@0 2086 // check if already in map
aoqi@0 2087 // we prefer the first entry since it is more likely to be what was used in
aoqi@0 2088 // the class file
aoqi@0 2089 for (SymbolHashMapEntry *en = bucket(index); en != NULL; en = en->next()) {
aoqi@0 2090 assert(en->symbol() != NULL, "SymbolHashMapEntry symbol is NULL");
aoqi@0 2091 if (en->hash() == hash && en->symbol() == sym) {
aoqi@0 2092 return; // already there
aoqi@0 2093 }
aoqi@0 2094 }
aoqi@0 2095
aoqi@0 2096 SymbolHashMapEntry* entry = new SymbolHashMapEntry(hash, sym, value);
aoqi@0 2097 entry->set_next(bucket(index));
aoqi@0 2098 _buckets[index].set_entry(entry);
aoqi@0 2099 assert(entry->symbol() != NULL, "SymbolHashMapEntry symbol is NULL");
aoqi@0 2100 }
aoqi@0 2101
aoqi@0 2102 SymbolHashMapEntry* SymbolHashMap::find_entry(Symbol* sym) {
aoqi@0 2103 assert(sym != NULL, "SymbolHashMap::find_entry - symbol is NULL");
aoqi@0 2104 char *str = sym->as_utf8();
aoqi@0 2105 int len = sym->utf8_length();
aoqi@0 2106 unsigned int hash = SymbolHashMap::compute_hash(str, len);
aoqi@0 2107 unsigned int index = hash % table_size();
aoqi@0 2108 for (SymbolHashMapEntry *en = bucket(index); en != NULL; en = en->next()) {
aoqi@0 2109 assert(en->symbol() != NULL, "SymbolHashMapEntry symbol is NULL");
aoqi@0 2110 if (en->hash() == hash && en->symbol() == sym) {
aoqi@0 2111 return en;
aoqi@0 2112 }
aoqi@0 2113 }
aoqi@0 2114 return NULL;
aoqi@0 2115 }

mercurial