duke@435: /* coleenp@4466: * Copyright (c) 1997, 2013, Oracle and/or its affiliates. All rights reserved. duke@435: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. duke@435: * duke@435: * This code is free software; you can redistribute it and/or modify it duke@435: * under the terms of the GNU General Public License version 2 only, as duke@435: * published by the Free Software Foundation. duke@435: * duke@435: * This code is distributed in the hope that it will be useful, but WITHOUT duke@435: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or duke@435: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License duke@435: * version 2 for more details (a copy is included in the LICENSE file that duke@435: * accompanied this code). duke@435: * duke@435: * You should have received a copy of the GNU General Public License version duke@435: * 2 along with this work; if not, write to the Free Software Foundation, duke@435: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. duke@435: * trims@1907: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA trims@1907: * or visit www.oracle.com if you need additional information or have any trims@1907: * questions. duke@435: * duke@435: */ duke@435: stefank@2314: #include "precompiled.hpp" coleenp@4037: #include "classfile/classLoaderData.hpp" stefank@2314: #include "classfile/javaClasses.hpp" coleenp@4490: #include "classfile/metadataOnStackMark.hpp" stefank@2314: #include "classfile/symbolTable.hpp" stefank@2314: #include "classfile/systemDictionary.hpp" stefank@2314: #include "classfile/vmSymbols.hpp" stefank@2314: #include "interpreter/linkResolver.hpp" acorn@4497: #include "memory/heapInspection.hpp" coleenp@4037: #include "memory/metadataFactory.hpp" stefank@2314: #include "memory/oopFactory.hpp" coleenp@4037: #include "oops/constantPool.hpp" stefank@2314: #include "oops/instanceKlass.hpp" stefank@2314: #include "oops/objArrayKlass.hpp" stefank@2314: #include "runtime/fieldType.hpp" stefank@2314: #include "runtime/init.hpp" coleenp@4037: #include "runtime/javaCalls.hpp" stefank@2314: #include "runtime/signature.hpp" iklam@4984: #include "runtime/synchronizer.hpp" stefank@2314: #include "runtime/vframe.hpp" duke@435: coleenp@4037: ConstantPool* ConstantPool::allocate(ClassLoaderData* loader_data, int length, TRAPS) { coleenp@4037: // Tags are RW but comment below applies to tags also. coleenp@4037: Array* tags = MetadataFactory::new_writeable_array(loader_data, length, 0, CHECK_NULL); coleenp@4037: coleenp@4037: int size = ConstantPool::size(length); coleenp@4037: coleenp@4037: // CDS considerations: coleenp@4037: // Allocate read-write but may be able to move to read-only at dumping time coleenp@4037: // if all the klasses are resolved. The only other field that is writable is coleenp@4037: // the resolved_references array, which is recreated at startup time. coleenp@4037: // But that could be moved to InstanceKlass (although a pain to access from coleenp@4037: // assembly code). Maybe it could be moved to the cpCache which is RW. iklam@5208: return new (loader_data, size, false, MetaspaceObj::ConstantPoolType, THREAD) ConstantPool(tags); coleenp@4037: } coleenp@4037: coleenp@4037: ConstantPool::ConstantPool(Array* tags) { coleenp@4037: set_length(tags->length()); coleenp@4037: set_tags(NULL); coleenp@4037: set_cache(NULL); coleenp@4037: set_reference_map(NULL); coleenp@4037: set_resolved_references(NULL); coleenp@4037: set_operands(NULL); coleenp@4037: set_pool_holder(NULL); coleenp@4037: set_flags(0); coleenp@4490: coleenp@4037: // only set to non-zero if constant pool is merged by RedefineClasses coleenp@4466: set_version(0); coleenp@4037: coleenp@4037: // initialize tag array coleenp@4037: int length = tags->length(); coleenp@4037: for (int index = 0; index < length; index++) { coleenp@4037: tags->at_put(index, JVM_CONSTANT_Invalid); coleenp@4037: } coleenp@4037: set_tags(tags); coleenp@4037: } coleenp@4037: coleenp@4037: void ConstantPool::deallocate_contents(ClassLoaderData* loader_data) { coleenp@4037: MetadataFactory::free_metadata(loader_data, cache()); coleenp@4037: set_cache(NULL); coleenp@4037: MetadataFactory::free_array(loader_data, operands()); coleenp@4037: set_operands(NULL); coleenp@4037: coleenp@4037: release_C_heap_structures(); coleenp@4037: coleenp@4037: // free tag array coleenp@4037: MetadataFactory::free_array(loader_data, tags()); coleenp@4037: set_tags(NULL); coleenp@4037: } coleenp@4037: coleenp@4037: void ConstantPool::release_C_heap_structures() { coleenp@4037: // walk constant pool and decrement symbol reference counts coleenp@4037: unreference_symbols(); coleenp@4037: } coleenp@4037: coleenp@4037: objArrayOop ConstantPool::resolved_references() const { coleenp@4037: return (objArrayOop)JNIHandles::resolve(_resolved_references); coleenp@4037: } coleenp@4037: coleenp@4037: // Create resolved_references array and mapping array for original cp indexes coleenp@4037: // The ldc bytecode was rewritten to have the resolved reference array index so need a way coleenp@4037: // to map it back for resolving and some unlikely miscellaneous uses. coleenp@4037: // The objects created by invokedynamic are appended to this list. coleenp@4037: void ConstantPool::initialize_resolved_references(ClassLoaderData* loader_data, coleenp@4037: intStack reference_map, coleenp@4037: int constant_pool_map_length, coleenp@5753: TRAPS) { coleenp@4037: // Initialized the resolved object cache. coleenp@4037: int map_length = reference_map.length(); coleenp@4037: if (map_length > 0) { coleenp@4037: // Only need mapping back to constant pool entries. The map isn't used for coleenp@5753: // invokedynamic resolved_reference entries. For invokedynamic entries, coleenp@5753: // the constant pool cache index has the mapping back to both the constant coleenp@5753: // pool and to the resolved reference index. coleenp@4037: if (constant_pool_map_length > 0) { coleenp@5753: Array* om = MetadataFactory::new_array(loader_data, constant_pool_map_length, CHECK); coleenp@4037: coleenp@4037: for (int i = 0; i < constant_pool_map_length; i++) { coleenp@4037: int x = reference_map.at(i); coleenp@4037: assert(x == (int)(jushort) x, "klass index is too big"); coleenp@4037: om->at_put(i, (jushort)x); coleenp@4037: } coleenp@4037: set_reference_map(om); coleenp@4037: } coleenp@4037: coleenp@4037: // Create Java array for holding resolved strings, methodHandles, coleenp@4037: // methodTypes, invokedynamic and invokehandle appendix objects, etc. coleenp@4037: objArrayOop stom = oopFactory::new_objArray(SystemDictionary::Object_klass(), map_length, CHECK); coleenp@4037: Handle refs_handle (THREAD, (oop)stom); // must handleize. coleenp@4037: set_resolved_references(loader_data->add_handle(refs_handle)); coleenp@4037: } coleenp@4037: } coleenp@4037: coleenp@4037: // CDS support. Create a new resolved_references array. coleenp@4037: void ConstantPool::restore_unshareable_info(TRAPS) { coleenp@4045: coleenp@4045: // restore the C++ vtable from the shared archive coleenp@4045: restore_vtable(); coleenp@4045: coleenp@4037: if (SystemDictionary::Object_klass_loaded()) { coleenp@4037: // Recreate the object array and add to ClassLoaderData. coleenp@4037: int map_length = resolved_reference_length(); coleenp@4037: if (map_length > 0) { coleenp@4037: objArrayOop stom = oopFactory::new_objArray(SystemDictionary::Object_klass(), map_length, CHECK); coleenp@4037: Handle refs_handle (THREAD, (oop)stom); // must handleize. coleenp@4037: coleenp@4037: ClassLoaderData* loader_data = pool_holder()->class_loader_data(); coleenp@4037: set_resolved_references(loader_data->add_handle(refs_handle)); coleenp@4037: } coleenp@4037: } coleenp@4037: } coleenp@4037: coleenp@4037: void ConstantPool::remove_unshareable_info() { coleenp@4037: // Resolved references are not in the shared archive. coleenp@4037: // Save the length for restoration. It is not necessarily the same length coleenp@4037: // as reference_map.length() if invokedynamic is saved. coleenp@4037: set_resolved_reference_length( coleenp@4037: resolved_references() != NULL ? resolved_references()->length() : 0); coleenp@4037: set_resolved_references(NULL); iklam@4984: } iklam@4984: iklam@4984: oop ConstantPool::lock() { iklam@4984: if (_pool_holder) { iklam@4984: // We re-use the _pool_holder's init_lock to reduce footprint. iklam@4984: // Notes on deadlocks: iklam@4984: // [1] This lock is a Java oop, so it can be recursively locked by iklam@4984: // the same thread without self-deadlocks. iklam@4984: // [2] Deadlock will happen if there is circular dependency between iklam@4984: // the of two Java classes. However, in this case, iklam@4984: // the deadlock would have happened long before we reach iklam@4984: // ConstantPool::lock(), so reusing init_lock does not iklam@4984: // increase the possibility of deadlock. iklam@4984: return _pool_holder->init_lock(); iklam@4984: } else { iklam@4984: return NULL; iklam@4984: } coleenp@4037: } coleenp@4037: coleenp@4037: int ConstantPool::cp_to_object_index(int cp_index) { coleenp@4037: // this is harder don't do this so much. coleenp@5753: int i = reference_map()->find(cp_index); coleenp@5753: // We might not find the index for jsr292 call. coleenp@5753: return (i < 0) ? _no_index_sentinel : i; coleenp@4037: } coleenp@4037: coleenp@4037: Klass* ConstantPool::klass_at_impl(constantPoolHandle this_oop, int which, TRAPS) { coleenp@4037: // A resolved constantPool entry will contain a Klass*, otherwise a Symbol*. duke@435: // 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: // tag is not updated atomicly. coleenp@4037: coleenp@2497: CPSlot entry = this_oop->slot_at(which); coleenp@4037: if (entry.is_resolved()) { coleenp@4037: assert(entry.get_klass()->is_klass(), "must be"); duke@435: // Already resolved - return entry. coleenp@4037: return entry.get_klass(); duke@435: } duke@435: duke@435: // Acquire lock on constant oop while doing update. After we get the lock, we check if another object duke@435: // already has updated the object duke@435: assert(THREAD->is_Java_thread(), "must be a Java thread"); duke@435: bool do_resolve = false; duke@435: bool in_error = false; duke@435: coleenp@4037: // Create a handle for the mirror. This will preserve the resolved class coleenp@4037: // until the loader_data is registered. coleenp@4037: Handle mirror_handle; coleenp@4037: coleenp@2497: Symbol* name = NULL; duke@435: Handle loader; iklam@4984: { iklam@4984: oop cplock = this_oop->lock(); iklam@4984: ObjectLocker ol(cplock , THREAD, cplock != NULL); duke@435: duke@435: if (this_oop->tag_at(which).is_unresolved_klass()) { duke@435: if (this_oop->tag_at(which).is_unresolved_klass_in_error()) { duke@435: in_error = true; duke@435: } else { duke@435: do_resolve = true; coleenp@2497: name = this_oop->unresolved_klass_at(which); coleenp@4251: loader = Handle(THREAD, this_oop->pool_holder()->class_loader()); duke@435: } duke@435: } duke@435: } // unlocking constantPool duke@435: duke@435: duke@435: // The original attempt to resolve this constant pool entry failed so find the duke@435: // original error and throw it again (JVMS 5.4.3). duke@435: if (in_error) { coleenp@2497: Symbol* error = SystemDictionary::find_resolution_error(this_oop, which); coleenp@2497: guarantee(error != (Symbol*)NULL, "tag mismatch with resolution error table"); duke@435: ResourceMark rm; duke@435: // exception text will be the class name duke@435: const char* className = this_oop->unresolved_klass_at(which)->as_C_string(); duke@435: THROW_MSG_0(error, className); duke@435: } duke@435: duke@435: if (do_resolve) { duke@435: // this_oop must be unlocked during resolve_or_fail coleenp@4251: oop protection_domain = this_oop->pool_holder()->protection_domain(); duke@435: Handle h_prot (THREAD, protection_domain); coleenp@4037: Klass* k_oop = SystemDictionary::resolve_or_fail(name, loader, h_prot, true, THREAD); duke@435: KlassHandle k; duke@435: if (!HAS_PENDING_EXCEPTION) { duke@435: k = KlassHandle(THREAD, k_oop); coleenp@4037: // preserve the resolved klass. coleenp@4037: mirror_handle = Handle(THREAD, k_oop->java_mirror()); duke@435: // Do access check for klasses duke@435: verify_constant_pool_resolve(this_oop, k, THREAD); duke@435: } duke@435: duke@435: // Failed to resolve class. We must record the errors so that subsequent attempts duke@435: // to resolve this constant pool entry fail with the same error (JVMS 5.4.3). duke@435: if (HAS_PENDING_EXCEPTION) { duke@435: ResourceMark rm; coleenp@4037: Symbol* error = PENDING_EXCEPTION->klass()->name(); duke@435: duke@435: bool throw_orig_error = false; duke@435: { iklam@4984: oop cplock = this_oop->lock(); iklam@4984: ObjectLocker ol(cplock, THREAD, cplock != NULL); duke@435: duke@435: // some other thread has beaten us and has resolved the class. duke@435: if (this_oop->tag_at(which).is_klass()) { duke@435: CLEAR_PENDING_EXCEPTION; duke@435: entry = this_oop->resolved_klass_at(which); coleenp@4037: return entry.get_klass(); duke@435: } duke@435: duke@435: if (!PENDING_EXCEPTION-> never@1577: is_a(SystemDictionary::LinkageError_klass())) { duke@435: // Just throw the exception and don't prevent these classes from duke@435: // being loaded due to virtual machine errors like StackOverflow duke@435: // and OutOfMemoryError, etc, or if the thread was hit by stop() duke@435: // Needs clarification to section 5.4.3 of the VM spec (see 6308271) duke@435: } duke@435: else if (!this_oop->tag_at(which).is_unresolved_klass_in_error()) { duke@435: SystemDictionary::add_resolution_error(this_oop, which, error); duke@435: this_oop->tag_at_put(which, JVM_CONSTANT_UnresolvedClassInError); duke@435: } else { duke@435: // some other thread has put the class in error state. coleenp@2497: error = SystemDictionary::find_resolution_error(this_oop, which); coleenp@2497: assert(error != NULL, "checking"); duke@435: throw_orig_error = true; duke@435: } duke@435: } // unlocked duke@435: duke@435: if (throw_orig_error) { duke@435: CLEAR_PENDING_EXCEPTION; duke@435: ResourceMark rm; duke@435: const char* className = this_oop->unresolved_klass_at(which)->as_C_string(); duke@435: THROW_MSG_0(error, className); duke@435: } duke@435: duke@435: return 0; duke@435: } duke@435: coleenp@4037: if (TraceClassResolution && !k()->oop_is_array()) { duke@435: // skip resolving the constant pool so that this code get's duke@435: // called the next time some bytecodes refer to this class. duke@435: ResourceMark rm; duke@435: int line_number = -1; duke@435: const char * source_file = NULL; duke@435: if (JavaThread::current()->has_last_Java_frame()) { duke@435: // try to identify the method which called this function. duke@435: vframeStream vfst(JavaThread::current()); duke@435: if (!vfst.at_end()) { duke@435: line_number = vfst.method()->line_number_from_bci(vfst.bci()); coleenp@4251: Symbol* s = vfst.method()->method_holder()->source_file_name(); duke@435: if (s != NULL) { duke@435: source_file = s->as_C_string(); duke@435: } duke@435: } duke@435: } duke@435: if (k() != this_oop->pool_holder()) { duke@435: // only print something if the classes are different duke@435: if (source_file != NULL) { duke@435: tty->print("RESOLVE %s %s %s:%d\n", coleenp@4251: this_oop->pool_holder()->external_name(), coleenp@4037: InstanceKlass::cast(k())->external_name(), source_file, line_number); duke@435: } else { duke@435: tty->print("RESOLVE %s %s\n", coleenp@4251: this_oop->pool_holder()->external_name(), coleenp@4037: InstanceKlass::cast(k())->external_name()); duke@435: } duke@435: } duke@435: return k(); duke@435: } else { iklam@4984: oop cplock = this_oop->lock(); iklam@4984: ObjectLocker ol(cplock, THREAD, cplock != NULL); duke@435: // Only updated constant pool - if it is resolved. duke@435: do_resolve = this_oop->tag_at(which).is_unresolved_klass(); duke@435: if (do_resolve) { coleenp@4251: ClassLoaderData* this_key = this_oop->pool_holder()->class_loader_data(); coleenp@4304: this_key->record_dependency(k(), CHECK_NULL); // Can throw OOM duke@435: this_oop->klass_at_put(which, k()); duke@435: } duke@435: } duke@435: } duke@435: duke@435: entry = this_oop->resolved_klass_at(which); coleenp@4037: assert(entry.is_resolved() && entry.get_klass()->is_klass(), "must be resolved at this point"); coleenp@4037: return entry.get_klass(); duke@435: } duke@435: duke@435: coleenp@4037: // Does not update ConstantPool* - to avoid any exception throwing. Used duke@435: // by compiler and exception handling. Also used to avoid classloads for duke@435: // instanceof operations. Returns NULL if the class has not been loaded or duke@435: // if the verification of constant pool failed coleenp@4037: Klass* ConstantPool::klass_at_if_loaded(constantPoolHandle this_oop, int which) { coleenp@2497: CPSlot entry = this_oop->slot_at(which); coleenp@4037: if (entry.is_resolved()) { coleenp@4037: assert(entry.get_klass()->is_klass(), "must be"); coleenp@4037: return entry.get_klass(); duke@435: } else { coleenp@4037: assert(entry.is_unresolved(), "must be either symbol or klass"); duke@435: Thread *thread = Thread::current(); coleenp@2497: Symbol* name = entry.get_symbol(); coleenp@4251: oop loader = this_oop->pool_holder()->class_loader(); coleenp@4251: oop protection_domain = this_oop->pool_holder()->protection_domain(); duke@435: Handle h_prot (thread, protection_domain); duke@435: Handle h_loader (thread, loader); coleenp@4037: Klass* k = SystemDictionary::find(name, h_loader, h_prot, thread); duke@435: duke@435: if (k != NULL) { duke@435: // Make sure that resolving is legal duke@435: EXCEPTION_MARK; duke@435: KlassHandle klass(THREAD, k); duke@435: // return NULL if verification fails duke@435: verify_constant_pool_resolve(this_oop, klass, THREAD); duke@435: if (HAS_PENDING_EXCEPTION) { duke@435: CLEAR_PENDING_EXCEPTION; duke@435: return NULL; duke@435: } duke@435: return klass(); duke@435: } else { duke@435: return k; duke@435: } duke@435: } duke@435: } duke@435: duke@435: coleenp@4037: Klass* ConstantPool::klass_ref_at_if_loaded(constantPoolHandle this_oop, int which) { duke@435: return klass_at_if_loaded(this_oop, this_oop->klass_ref_index_at(which)); duke@435: } duke@435: duke@435: coleenp@4037: Method* ConstantPool::method_at_if_loaded(constantPoolHandle cpool, twisti@3969: int which) { brutisso@3489: if (cpool->cache() == NULL) return NULL; // nothing to load yet coleenp@4037: int cache_index = decode_cpcache_index(which, true); jrose@2982: if (!(cache_index >= 0 && cache_index < cpool->cache()->length())) { coleenp@4037: // FIXME: should be an assert jrose@2982: if (PrintMiscellaneous && (Verbose||WizardMode)) { twisti@3969: tty->print_cr("bad operand %d in:", which); cpool->print(); jrose@2982: } jrose@2982: return NULL; jrose@2982: } jrose@2982: ConstantPoolCacheEntry* e = cpool->cache()->entry_at(cache_index); twisti@3969: return e->method_if_resolved(cpool); twisti@3969: } twisti@3969: twisti@3969: coleenp@4037: bool ConstantPool::has_appendix_at_if_loaded(constantPoolHandle cpool, int which) { twisti@3969: if (cpool->cache() == NULL) return false; // nothing to load yet coleenp@4037: int cache_index = decode_cpcache_index(which, true); coleenp@4037: ConstantPoolCacheEntry* e = cpool->cache()->entry_at(cache_index); twisti@3969: return e->has_appendix(); twisti@3969: } twisti@3969: coleenp@4037: oop ConstantPool::appendix_at_if_loaded(constantPoolHandle cpool, int which) { twisti@3969: if (cpool->cache() == NULL) return NULL; // nothing to load yet coleenp@4037: int cache_index = decode_cpcache_index(which, true); coleenp@4037: ConstantPoolCacheEntry* e = cpool->cache()->entry_at(cache_index); coleenp@4037: return e->appendix_if_resolved(cpool); jrose@2982: } jrose@2982: jrose@2982: twisti@4133: bool ConstantPool::has_method_type_at_if_loaded(constantPoolHandle cpool, int which) { twisti@4133: if (cpool->cache() == NULL) return false; // nothing to load yet twisti@4133: int cache_index = decode_cpcache_index(which, true); twisti@4133: ConstantPoolCacheEntry* e = cpool->cache()->entry_at(cache_index); twisti@4133: return e->has_method_type(); twisti@4133: } twisti@4133: twisti@4133: oop ConstantPool::method_type_at_if_loaded(constantPoolHandle cpool, int which) { twisti@4133: if (cpool->cache() == NULL) return NULL; // nothing to load yet twisti@4133: int cache_index = decode_cpcache_index(which, true); twisti@4133: ConstantPoolCacheEntry* e = cpool->cache()->entry_at(cache_index); twisti@4133: return e->method_type_if_resolved(cpool); twisti@4133: } twisti@4133: twisti@4133: coleenp@4037: Symbol* ConstantPool::impl_name_ref_at(int which, bool uncached) { jrose@1161: int name_index = name_ref_index_at(impl_name_and_type_ref_index_at(which, uncached)); duke@435: return symbol_at(name_index); duke@435: } duke@435: duke@435: coleenp@4037: Symbol* ConstantPool::impl_signature_ref_at(int which, bool uncached) { jrose@1161: int signature_index = signature_ref_index_at(impl_name_and_type_ref_index_at(which, uncached)); duke@435: return symbol_at(signature_index); duke@435: } duke@435: duke@435: coleenp@4037: int ConstantPool::impl_name_and_type_ref_index_at(int which, bool uncached) { jrose@1494: int i = which; jrose@1494: if (!uncached && cache() != NULL) { coleenp@4037: if (ConstantPool::is_invokedynamic_index(which)) { coleenp@4037: // Invokedynamic index is index into resolved_references coleenp@4037: int pool_index = invokedynamic_cp_cache_entry_at(which)->constant_pool_index(); jrose@2742: pool_index = invoke_dynamic_name_and_type_ref_index_at(pool_index); jrose@2015: assert(tag_at(pool_index).is_name_and_type(), ""); jrose@2015: return pool_index; jrose@2015: } jrose@1494: // change byte-ordering and go via cache jrose@1494: i = remap_instruction_operand_from_cache(which); jrose@1494: } else { jrose@2268: if (tag_at(which).is_invoke_dynamic()) { jrose@2268: int pool_index = invoke_dynamic_name_and_type_ref_index_at(which); jrose@2268: assert(tag_at(pool_index).is_name_and_type(), ""); jrose@2268: return pool_index; jrose@2268: } jrose@1494: } jrose@1494: assert(tag_at(i).is_field_or_method(), "Corrupted constant pool"); jrose@2268: assert(!tag_at(i).is_invoke_dynamic(), "Must be handled above"); jrose@1494: jint ref_index = *int_at_addr(i); duke@435: return extract_high_short_from_int(ref_index); duke@435: } duke@435: duke@435: coleenp@4037: int ConstantPool::impl_klass_ref_index_at(int which, bool uncached) { coleenp@4037: guarantee(!ConstantPool::is_invokedynamic_index(which), jrose@1494: "an invokedynamic instruction does not have a klass"); jrose@1494: int i = which; jrose@1494: if (!uncached && cache() != NULL) { jrose@1494: // change byte-ordering and go via cache jrose@1494: i = remap_instruction_operand_from_cache(which); jrose@1494: } jrose@1494: assert(tag_at(i).is_field_or_method(), "Corrupted constant pool"); jrose@1494: jint ref_index = *int_at_addr(i); duke@435: return extract_low_short_from_int(ref_index); duke@435: } duke@435: duke@435: jrose@1161: coleenp@4037: int ConstantPool::remap_instruction_operand_from_cache(int operand) { jrose@1920: int cpc_index = operand; jrose@1920: DEBUG_ONLY(cpc_index -= CPCACHE_INDEX_TAG); jrose@1920: assert((int)(u2)cpc_index == cpc_index, "clean u2"); jrose@1494: int member_index = cache()->entry_at(cpc_index)->constant_pool_index(); jrose@1494: return member_index; jrose@1161: } jrose@1161: jrose@1161: coleenp@4037: void ConstantPool::verify_constant_pool_resolve(constantPoolHandle this_oop, KlassHandle k, TRAPS) { duke@435: if (k->oop_is_instance() || k->oop_is_objArray()) { duke@435: instanceKlassHandle holder (THREAD, this_oop->pool_holder()); coleenp@4142: Klass* elem_oop = k->oop_is_instance() ? k() : ObjArrayKlass::cast(k())->bottom_klass(); duke@435: KlassHandle element (THREAD, elem_oop); duke@435: duke@435: // The element type could be a typeArray - we only need the access check if it is duke@435: // an reference to another class duke@435: if (element->oop_is_instance()) { duke@435: LinkResolver::check_klass_accessability(holder, element, CHECK); duke@435: } duke@435: } duke@435: } duke@435: duke@435: coleenp@4037: int ConstantPool::name_ref_index_at(int which_nt) { jrose@1161: jint ref_index = name_and_type_at(which_nt); duke@435: return extract_low_short_from_int(ref_index); duke@435: } duke@435: duke@435: coleenp@4037: int ConstantPool::signature_ref_index_at(int which_nt) { jrose@1161: jint ref_index = name_and_type_at(which_nt); duke@435: return extract_high_short_from_int(ref_index); duke@435: } duke@435: duke@435: coleenp@4037: Klass* ConstantPool::klass_ref_at(int which, TRAPS) { duke@435: return klass_at(klass_ref_index_at(which), CHECK_NULL); duke@435: } duke@435: duke@435: coleenp@4037: Symbol* ConstantPool::klass_name_at(int which) { duke@435: assert(tag_at(which).is_unresolved_klass() || tag_at(which).is_klass(), duke@435: "Corrupted constant pool"); coleenp@4037: // A resolved constantPool entry will contain a Klass*, otherwise a Symbol*. duke@435: // 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: // tag is not updated atomicly. coleenp@2497: CPSlot entry = slot_at(which); coleenp@4037: if (entry.is_resolved()) { duke@435: // Already resolved - return entry's name. coleenp@4037: assert(entry.get_klass()->is_klass(), "must be"); coleenp@4037: return entry.get_klass()->name(); duke@435: } else { coleenp@4037: assert(entry.is_unresolved(), "must be either symbol or klass"); coleenp@2497: return entry.get_symbol(); duke@435: } duke@435: } duke@435: coleenp@4037: Symbol* ConstantPool::klass_ref_at_noresolve(int which) { duke@435: jint ref_index = klass_ref_index_at(which); duke@435: return klass_at_noresolve(ref_index); duke@435: } duke@435: coleenp@4037: Symbol* ConstantPool::uncached_klass_ref_at_noresolve(int which) { jrose@1957: jint ref_index = uncached_klass_ref_index_at(which); jrose@1957: return klass_at_noresolve(ref_index); jrose@1957: } jrose@1957: coleenp@4037: char* ConstantPool::string_at_noresolve(int which) { coleenp@4037: Symbol* s = unresolved_string_at(which); coleenp@4037: if (s == NULL) { coleenp@4037: return (char*)""; duke@435: } else { coleenp@4037: return unresolved_string_at(which)->as_C_string(); duke@435: } duke@435: } duke@435: coleenp@4037: BasicType ConstantPool::basic_type_for_signature_at(int which) { duke@435: return FieldType::basic_type(symbol_at(which)); duke@435: } duke@435: duke@435: coleenp@4037: void ConstantPool::resolve_string_constants_impl(constantPoolHandle this_oop, TRAPS) { duke@435: for (int index = 1; index < this_oop->length(); index++) { // Index 0 is unused coleenp@4037: if (this_oop->tag_at(index).is_string()) { duke@435: this_oop->string_at(index, CHECK); duke@435: } duke@435: } duke@435: } duke@435: coleenp@4037: // Resolve all the classes in the constant pool. If they are all resolved, coleenp@4037: // the constant pool is read-only. Enhancement: allocate cp entries to coleenp@4037: // another metaspace, and copy to read-only or read-write space if this coleenp@4037: // bit is set. coleenp@4037: bool ConstantPool::resolve_class_constants(TRAPS) { coleenp@4037: constantPoolHandle cp(THREAD, this); coleenp@4037: for (int index = 1; index < length(); index++) { // Index 0 is unused coleenp@4037: if (tag_at(index).is_unresolved_klass() && coleenp@4037: klass_at_if_loaded(cp, index) == NULL) { coleenp@4037: return false; jrose@2268: } coleenp@4037: } coleenp@4037: // set_preresolution(); or some bit for future use coleenp@4037: return true; jrose@2268: } jrose@2268: coleenp@4037: // If resolution for MethodHandle or MethodType fails, save the exception coleenp@4037: // in the resolution error table, so that the same exception is thrown again. coleenp@4037: void ConstantPool::save_and_throw_exception(constantPoolHandle this_oop, int which, coleenp@4037: int tag, TRAPS) { coleenp@4037: ResourceMark rm; coleenp@4037: Symbol* error = PENDING_EXCEPTION->klass()->name(); iklam@4984: oop cplock = this_oop->lock(); iklam@4984: ObjectLocker ol(cplock, THREAD, cplock != NULL); // lock cpool to change tag. coleenp@4037: coleenp@4037: int error_tag = (tag == JVM_CONSTANT_MethodHandle) ? coleenp@4037: JVM_CONSTANT_MethodHandleInError : JVM_CONSTANT_MethodTypeInError; coleenp@4037: coleenp@4037: if (!PENDING_EXCEPTION-> coleenp@4037: is_a(SystemDictionary::LinkageError_klass())) { coleenp@4037: // Just throw the exception and don't prevent these classes from coleenp@4037: // being loaded due to virtual machine errors like StackOverflow coleenp@4037: // and OutOfMemoryError, etc, or if the thread was hit by stop() coleenp@4037: // Needs clarification to section 5.4.3 of the VM spec (see 6308271) coleenp@4037: coleenp@4037: } else if (this_oop->tag_at(which).value() != error_tag) { coleenp@4037: SystemDictionary::add_resolution_error(this_oop, which, error); coleenp@4037: this_oop->tag_at_put(which, error_tag); coleenp@4037: } else { coleenp@4037: // some other thread has put the class in error state. coleenp@4037: error = SystemDictionary::find_resolution_error(this_oop, which); coleenp@4037: assert(error != NULL, "checking"); coleenp@4037: CLEAR_PENDING_EXCEPTION; coleenp@4037: THROW_MSG(error, ""); coleenp@4037: } coleenp@4037: } coleenp@4037: coleenp@4037: coleenp@4037: // Called to resolve constants in the constant pool and return an oop. coleenp@4037: // Some constant pool entries cache their resolved oop. This is also coleenp@4037: // called to create oops from constants to use in arguments for invokedynamic coleenp@4037: oop ConstantPool::resolve_constant_at_impl(constantPoolHandle this_oop, int index, int cache_index, TRAPS) { jrose@1957: oop result_oop = NULL; jrose@2268: Handle throw_exception; jrose@2268: jrose@2268: if (cache_index == _possible_index_sentinel) { coleenp@4037: // It is possible that this constant is one which is cached in the objects. jrose@2268: // We'll do a linear search. This should be OK because this usage is rare. jrose@2268: assert(index > 0, "valid index"); coleenp@4037: cache_index = this_oop->cp_to_object_index(index); jrose@2268: } jrose@2268: assert(cache_index == _no_index_sentinel || cache_index >= 0, ""); jrose@2268: assert(index == _no_index_sentinel || index >= 0, ""); jrose@2268: jrose@1957: if (cache_index >= 0) { coleenp@4037: result_oop = this_oop->resolved_references()->obj_at(cache_index); jrose@1957: if (result_oop != NULL) { coleenp@4037: return result_oop; jrose@2268: // That was easy... jrose@1957: } coleenp@4037: index = this_oop->object_to_cp_index(cache_index); jrose@1957: } jrose@1957: jrose@2268: jvalue prim_value; // temp used only in a few cases below jrose@2268: jrose@1957: int tag_value = this_oop->tag_at(index).value(); coleenp@4037: jrose@1957: switch (tag_value) { jrose@1957: jrose@1957: case JVM_CONSTANT_UnresolvedClass: jrose@1957: case JVM_CONSTANT_UnresolvedClassInError: jrose@1957: case JVM_CONSTANT_Class: jrose@1957: { coleenp@4037: assert(cache_index == _no_index_sentinel, "should not have been set"); coleenp@4037: Klass* resolved = klass_at_impl(this_oop, index, CHECK_NULL); jrose@1957: // ldc wants the java mirror. never@2658: result_oop = resolved->java_mirror(); jrose@1957: break; jrose@1957: } jrose@1957: jrose@1957: case JVM_CONSTANT_String: coleenp@4037: assert(cache_index != _no_index_sentinel, "should have been set"); jrose@1957: if (this_oop->is_pseudo_string_at(index)) { coleenp@4037: result_oop = this_oop->pseudo_string_at(index, cache_index); jrose@1957: break; jrose@1957: } coleenp@4037: result_oop = string_at_impl(this_oop, index, cache_index, CHECK_NULL); jrose@1957: break; jrose@1957: coleenp@4037: case JVM_CONSTANT_MethodHandleInError: coleenp@4037: case JVM_CONSTANT_MethodTypeInError: coleenp@4037: { coleenp@4037: Symbol* error = SystemDictionary::find_resolution_error(this_oop, index); coleenp@4037: guarantee(error != (Symbol*)NULL, "tag mismatch with resolution error table"); coleenp@4037: ResourceMark rm; coleenp@4037: THROW_MSG_0(error, ""); coleenp@4037: break; coleenp@4037: } coleenp@4037: jrose@1957: case JVM_CONSTANT_MethodHandle: jrose@1957: { jrose@1957: int ref_kind = this_oop->method_handle_ref_kind_at(index); jrose@1957: int callee_index = this_oop->method_handle_klass_index_at(index); coleenp@2497: Symbol* name = this_oop->method_handle_name_ref_at(index); coleenp@2497: Symbol* signature = this_oop->method_handle_signature_ref_at(index); jrose@1957: if (PrintMiscellaneous) jrose@1957: tty->print_cr("resolve JVM_CONSTANT_MethodHandle:%d [%d/%d/%d] %s.%s", jrose@1957: ref_kind, index, this_oop->method_handle_index_at(index), jrose@1957: callee_index, name->as_C_string(), signature->as_C_string()); jrose@1957: KlassHandle callee; coleenp@4037: { Klass* k = klass_at_impl(this_oop, callee_index, CHECK_NULL); jrose@1957: callee = KlassHandle(THREAD, k); jrose@1957: } jrose@1957: KlassHandle klass(THREAD, this_oop->pool_holder()); jrose@1957: Handle value = SystemDictionary::link_method_handle_constant(klass, ref_kind, jrose@1957: callee, name, signature, jrose@2268: THREAD); coleenp@4037: result_oop = value(); jrose@2268: if (HAS_PENDING_EXCEPTION) { coleenp@4037: save_and_throw_exception(this_oop, index, tag_value, CHECK_NULL); jrose@2268: } jrose@1957: break; jrose@1957: } jrose@1957: jrose@1957: case JVM_CONSTANT_MethodType: jrose@1957: { coleenp@2497: Symbol* signature = this_oop->method_type_signature_at(index); jrose@1957: if (PrintMiscellaneous) jrose@1957: tty->print_cr("resolve JVM_CONSTANT_MethodType [%d/%d] %s", jrose@1957: index, this_oop->method_type_index_at(index), jrose@1957: signature->as_C_string()); jrose@1957: KlassHandle klass(THREAD, this_oop->pool_holder()); twisti@3969: Handle value = SystemDictionary::find_method_handle_type(signature, klass, THREAD); coleenp@4037: result_oop = value(); jrose@2268: if (HAS_PENDING_EXCEPTION) { coleenp@4037: save_and_throw_exception(this_oop, index, tag_value, CHECK_NULL); jrose@2268: } jrose@1957: break; jrose@1957: } jrose@1957: jrose@1957: case JVM_CONSTANT_Integer: coleenp@4037: assert(cache_index == _no_index_sentinel, "should not have been set"); jrose@2268: prim_value.i = this_oop->int_at(index); jrose@2268: result_oop = java_lang_boxing_object::create(T_INT, &prim_value, CHECK_NULL); jrose@2268: break; jrose@2268: jrose@1957: case JVM_CONSTANT_Float: coleenp@4037: assert(cache_index == _no_index_sentinel, "should not have been set"); jrose@2268: prim_value.f = this_oop->float_at(index); jrose@2268: result_oop = java_lang_boxing_object::create(T_FLOAT, &prim_value, CHECK_NULL); jrose@2268: break; jrose@2268: jrose@1957: case JVM_CONSTANT_Long: coleenp@4037: assert(cache_index == _no_index_sentinel, "should not have been set"); jrose@2268: prim_value.j = this_oop->long_at(index); jrose@2268: result_oop = java_lang_boxing_object::create(T_LONG, &prim_value, CHECK_NULL); jrose@2268: break; jrose@2268: jrose@1957: case JVM_CONSTANT_Double: coleenp@4037: assert(cache_index == _no_index_sentinel, "should not have been set"); jrose@2268: prim_value.d = this_oop->double_at(index); jrose@2268: result_oop = java_lang_boxing_object::create(T_DOUBLE, &prim_value, CHECK_NULL); jrose@1957: break; jrose@1957: jrose@1957: default: jrose@1957: DEBUG_ONLY( tty->print_cr("*** %p: tag at CP[%d/%d] = %d", jrose@1957: this_oop(), index, cache_index, tag_value) ); jrose@1957: assert(false, "unexpected constant tag"); jrose@1957: break; jrose@1957: } jrose@1957: jrose@1957: if (cache_index >= 0) { jrose@1957: // Cache the oop here also. jrose@2268: Handle result_handle(THREAD, result_oop); iklam@4984: oop cplock = this_oop->lock(); iklam@4984: ObjectLocker ol(cplock, THREAD, cplock != NULL); // don't know if we really need this coleenp@4037: oop result = this_oop->resolved_references()->obj_at(cache_index); coleenp@4037: // Benign race condition: resolved_references may already be filled in while we were trying to lock. jrose@2268: // The important thing here is that all threads pick up the same result. jrose@2268: // It doesn't matter which racing thread wins, as long as only one jrose@2268: // result is used by all threads, and all future queries. jrose@2268: // That result may be either a resolved constant or a failure exception. coleenp@4037: if (result == NULL) { coleenp@4037: this_oop->resolved_references()->obj_at_put(cache_index, result_handle()); coleenp@4037: return result_handle(); coleenp@4037: } else { coleenp@4037: // Return the winning thread's result. This can be different than coleenp@4037: // result_handle() for MethodHandles. coleenp@4037: return result; jrose@1957: } jrose@1957: } else { jrose@1957: return result_oop; jrose@1957: } jrose@1957: } jrose@1957: coleenp@4037: oop ConstantPool::uncached_string_at(int which, TRAPS) { coleenp@4037: Symbol* sym = unresolved_string_at(which); coleenp@4037: oop str = StringTable::intern(sym, CHECK_(NULL)); coleenp@4037: assert(java_lang_String::is_instance(str), "must be string"); coleenp@4037: return str; coleenp@4037: } twisti@3969: coleenp@4037: coleenp@4037: oop ConstantPool::resolve_bootstrap_specifier_at_impl(constantPoolHandle this_oop, int index, TRAPS) { twisti@3969: assert(this_oop->tag_at(index).is_invoke_dynamic(), "Corrupted constant pool"); twisti@3969: twisti@3969: Handle bsm; twisti@3969: int argc; twisti@3969: { twisti@3969: // JVM_CONSTANT_InvokeDynamic is an ordered pair of [bootm, name&type], plus optional arguments twisti@3969: // The bootm, being a JVM_CONSTANT_MethodHandle, has its own cache entry. twisti@3969: // It is accompanied by the optional arguments. twisti@3969: int bsm_index = this_oop->invoke_dynamic_bootstrap_method_ref_index_at(index); twisti@3969: oop bsm_oop = this_oop->resolve_possibly_cached_constant_at(bsm_index, CHECK_NULL); twisti@3969: if (!java_lang_invoke_MethodHandle::is_instance(bsm_oop)) { twisti@3969: THROW_MSG_NULL(vmSymbols::java_lang_LinkageError(), "BSM not an MethodHandle"); twisti@3969: } twisti@3969: twisti@3969: // Extract the optional static arguments. twisti@3969: argc = this_oop->invoke_dynamic_argument_count_at(index); twisti@3969: if (argc == 0) return bsm_oop; twisti@3969: twisti@3969: bsm = Handle(THREAD, bsm_oop); twisti@3969: } twisti@3969: twisti@3969: objArrayHandle info; twisti@3969: { twisti@3969: objArrayOop info_oop = oopFactory::new_objArray(SystemDictionary::Object_klass(), 1+argc, CHECK_NULL); twisti@3969: info = objArrayHandle(THREAD, info_oop); twisti@3969: } twisti@3969: twisti@3969: info->obj_at_put(0, bsm()); twisti@3969: for (int i = 0; i < argc; i++) { twisti@3969: int arg_index = this_oop->invoke_dynamic_argument_index_at(index, i); twisti@3969: oop arg_oop = this_oop->resolve_possibly_cached_constant_at(arg_index, CHECK_NULL); twisti@3969: info->obj_at_put(1+i, arg_oop); twisti@3969: } twisti@3969: twisti@3969: return info(); twisti@3969: } twisti@3969: coleenp@4037: oop ConstantPool::string_at_impl(constantPoolHandle this_oop, int which, int obj_index, TRAPS) { coleenp@4037: // If the string has already been interned, this entry will be non-null coleenp@4037: oop str = this_oop->resolved_references()->obj_at(obj_index); coleenp@4037: if (str != NULL) return str; coleenp@5753: Symbol* sym = this_oop->unresolved_string_at(which); coleenp@4037: str = StringTable::intern(sym, CHECK_(NULL)); coleenp@4037: this_oop->string_at_put(which, obj_index, str); coleenp@2497: assert(java_lang_String::is_instance(str), "must be string"); coleenp@2497: return str; duke@435: } duke@435: duke@435: coleenp@4037: bool ConstantPool::klass_name_at_matches(instanceKlassHandle k, duke@435: int which) { coleenp@2497: // Names are interned, so we can compare Symbol*s directly coleenp@2497: Symbol* cp_name = klass_name_at(which); duke@435: return (cp_name == k->name()); duke@435: } duke@435: duke@435: coleenp@2497: // Iterate over symbols and decrement ones which are Symbol*s. coleenp@2497: // This is done during GC so do not need to lock constantPool unless we coleenp@2497: // have per-thread safepoints. coleenp@2497: // Only decrement the UTF8 symbols. Unresolved classes and strings point to coleenp@2497: // these symbols but didn't increment the reference count. coleenp@4037: void ConstantPool::unreference_symbols() { coleenp@2497: for (int index = 1; index < length(); index++) { // Index 0 is unused coleenp@2497: constantTag tag = tag_at(index); coleenp@2497: if (tag.is_symbol()) { coleenp@2497: symbol_at(index)->decrement_refcount(); coleenp@2497: } coleenp@2497: } coleenp@2497: } duke@435: duke@435: duke@435: // Compare this constant pool's entry at index1 to the constant pool duke@435: // cp2's entry at index2. coleenp@4037: bool ConstantPool::compare_entry_to(int index1, constantPoolHandle cp2, duke@435: int index2, TRAPS) { duke@435: duke@435: jbyte t1 = tag_at(index1).value(); duke@435: jbyte t2 = cp2->tag_at(index2).value(); duke@435: duke@435: duke@435: // JVM_CONSTANT_UnresolvedClassInError is equal to JVM_CONSTANT_UnresolvedClass duke@435: // when comparing duke@435: if (t1 == JVM_CONSTANT_UnresolvedClassInError) { duke@435: t1 = JVM_CONSTANT_UnresolvedClass; duke@435: } duke@435: if (t2 == JVM_CONSTANT_UnresolvedClassInError) { duke@435: t2 = JVM_CONSTANT_UnresolvedClass; duke@435: } duke@435: duke@435: if (t1 != t2) { duke@435: // Not the same entry type so there is nothing else to check. Note duke@435: // that this style of checking will consider resolved/unresolved coleenp@4037: // class pairs as different. coleenp@4037: // From the ConstantPool* API point of view, this is correct coleenp@4037: // behavior. See VM_RedefineClasses::merge_constant_pools() to see how this coleenp@4037: // plays out in the context of ConstantPool* merging. duke@435: return false; duke@435: } duke@435: duke@435: switch (t1) { duke@435: case JVM_CONSTANT_Class: duke@435: { coleenp@4037: Klass* k1 = klass_at(index1, CHECK_false); coleenp@4037: Klass* k2 = cp2->klass_at(index2, CHECK_false); duke@435: if (k1 == k2) { duke@435: return true; duke@435: } duke@435: } break; duke@435: duke@435: case JVM_CONSTANT_ClassIndex: duke@435: { duke@435: int recur1 = klass_index_at(index1); duke@435: int recur2 = cp2->klass_index_at(index2); duke@435: bool match = compare_entry_to(recur1, cp2, recur2, CHECK_false); duke@435: if (match) { duke@435: return true; duke@435: } duke@435: } break; duke@435: duke@435: case JVM_CONSTANT_Double: duke@435: { duke@435: jdouble d1 = double_at(index1); duke@435: jdouble d2 = cp2->double_at(index2); duke@435: if (d1 == d2) { duke@435: return true; duke@435: } duke@435: } break; duke@435: duke@435: case JVM_CONSTANT_Fieldref: duke@435: case JVM_CONSTANT_InterfaceMethodref: duke@435: case JVM_CONSTANT_Methodref: duke@435: { duke@435: int recur1 = uncached_klass_ref_index_at(index1); duke@435: int recur2 = cp2->uncached_klass_ref_index_at(index2); duke@435: bool match = compare_entry_to(recur1, cp2, recur2, CHECK_false); duke@435: if (match) { duke@435: recur1 = uncached_name_and_type_ref_index_at(index1); duke@435: recur2 = cp2->uncached_name_and_type_ref_index_at(index2); duke@435: match = compare_entry_to(recur1, cp2, recur2, CHECK_false); duke@435: if (match) { duke@435: return true; duke@435: } duke@435: } duke@435: } break; duke@435: duke@435: case JVM_CONSTANT_Float: duke@435: { duke@435: jfloat f1 = float_at(index1); duke@435: jfloat f2 = cp2->float_at(index2); duke@435: if (f1 == f2) { duke@435: return true; duke@435: } duke@435: } break; duke@435: duke@435: case JVM_CONSTANT_Integer: duke@435: { duke@435: jint i1 = int_at(index1); duke@435: jint i2 = cp2->int_at(index2); duke@435: if (i1 == i2) { duke@435: return true; duke@435: } duke@435: } break; duke@435: duke@435: case JVM_CONSTANT_Long: duke@435: { duke@435: jlong l1 = long_at(index1); duke@435: jlong l2 = cp2->long_at(index2); duke@435: if (l1 == l2) { duke@435: return true; duke@435: } duke@435: } break; duke@435: duke@435: case JVM_CONSTANT_NameAndType: duke@435: { duke@435: int recur1 = name_ref_index_at(index1); duke@435: int recur2 = cp2->name_ref_index_at(index2); duke@435: bool match = compare_entry_to(recur1, cp2, recur2, CHECK_false); duke@435: if (match) { duke@435: recur1 = signature_ref_index_at(index1); duke@435: recur2 = cp2->signature_ref_index_at(index2); duke@435: match = compare_entry_to(recur1, cp2, recur2, CHECK_false); duke@435: if (match) { duke@435: return true; duke@435: } duke@435: } duke@435: } break; duke@435: duke@435: case JVM_CONSTANT_StringIndex: duke@435: { duke@435: int recur1 = string_index_at(index1); duke@435: int recur2 = cp2->string_index_at(index2); duke@435: bool match = compare_entry_to(recur1, cp2, recur2, CHECK_false); duke@435: if (match) { duke@435: return true; duke@435: } duke@435: } break; duke@435: duke@435: case JVM_CONSTANT_UnresolvedClass: duke@435: { coleenp@2497: Symbol* k1 = unresolved_klass_at(index1); coleenp@2497: Symbol* k2 = cp2->unresolved_klass_at(index2); duke@435: if (k1 == k2) { duke@435: return true; duke@435: } duke@435: } break; duke@435: jrose@1957: case JVM_CONSTANT_MethodType: jrose@1957: { jrose@1957: int k1 = method_type_index_at(index1); jrose@1957: int k2 = cp2->method_type_index_at(index2); jrose@2353: bool match = compare_entry_to(k1, cp2, k2, CHECK_false); jrose@2353: if (match) { jrose@1957: return true; jrose@1957: } jrose@1957: } break; jrose@1957: jrose@1957: case JVM_CONSTANT_MethodHandle: jrose@1957: { jrose@1957: int k1 = method_handle_ref_kind_at(index1); jrose@1957: int k2 = cp2->method_handle_ref_kind_at(index2); jrose@1957: if (k1 == k2) { jrose@1957: int i1 = method_handle_index_at(index1); jrose@1957: int i2 = cp2->method_handle_index_at(index2); jrose@2353: bool match = compare_entry_to(i1, cp2, i2, CHECK_false); jrose@2353: if (match) { jrose@1957: return true; jrose@1957: } jrose@1957: } jrose@1957: } break; jrose@1957: jrose@2015: case JVM_CONSTANT_InvokeDynamic: jrose@2015: { sspitsyn@4983: int k1 = invoke_dynamic_name_and_type_ref_index_at(index1); sspitsyn@4983: int k2 = cp2->invoke_dynamic_name_and_type_ref_index_at(index2); sspitsyn@4983: int i1 = invoke_dynamic_bootstrap_specifier_index(index1); sspitsyn@4983: int i2 = cp2->invoke_dynamic_bootstrap_specifier_index(index2); ccheung@5183: // separate statements and variables because CHECK_false is used ccheung@5183: bool match_entry = compare_entry_to(k1, cp2, k2, CHECK_false); ccheung@5183: bool match_operand = compare_operand_to(i1, cp2, i2, CHECK_false); ccheung@5183: return (match_entry && match_operand); jrose@2015: } break; jrose@2015: coleenp@4037: case JVM_CONSTANT_String: duke@435: { coleenp@2497: Symbol* s1 = unresolved_string_at(index1); coleenp@2497: Symbol* s2 = cp2->unresolved_string_at(index2); duke@435: if (s1 == s2) { duke@435: return true; duke@435: } duke@435: } break; duke@435: duke@435: case JVM_CONSTANT_Utf8: duke@435: { coleenp@2497: Symbol* s1 = symbol_at(index1); coleenp@2497: Symbol* s2 = cp2->symbol_at(index2); duke@435: if (s1 == s2) { duke@435: return true; duke@435: } duke@435: } break; duke@435: duke@435: // Invalid is used as the tag for the second constant pool entry duke@435: // occupied by JVM_CONSTANT_Double or JVM_CONSTANT_Long. It should duke@435: // not be seen by itself. duke@435: case JVM_CONSTANT_Invalid: // fall through duke@435: duke@435: default: duke@435: ShouldNotReachHere(); duke@435: break; duke@435: } duke@435: duke@435: return false; duke@435: } // end compare_entry_to() duke@435: duke@435: sspitsyn@4983: // Resize the operands array with delta_len and delta_size. sspitsyn@4983: // Used in RedefineClasses for CP merge. sspitsyn@4983: void ConstantPool::resize_operands(int delta_len, int delta_size, TRAPS) { sspitsyn@4983: int old_len = operand_array_length(operands()); sspitsyn@4983: int new_len = old_len + delta_len; sspitsyn@4983: int min_len = (delta_len > 0) ? old_len : new_len; sspitsyn@4983: sspitsyn@4983: int old_size = operands()->length(); sspitsyn@4983: int new_size = old_size + delta_size; sspitsyn@4983: int min_size = (delta_size > 0) ? old_size : new_size; sspitsyn@4983: sspitsyn@4983: ClassLoaderData* loader_data = pool_holder()->class_loader_data(); sspitsyn@4983: Array* new_ops = MetadataFactory::new_array(loader_data, new_size, CHECK); sspitsyn@4983: sspitsyn@4983: // Set index in the resized array for existing elements only sspitsyn@4983: for (int idx = 0; idx < min_len; idx++) { sspitsyn@4983: int offset = operand_offset_at(idx); // offset in original array sspitsyn@4983: operand_offset_at_put(new_ops, idx, offset + 2*delta_len); // offset in resized array sspitsyn@4983: } sspitsyn@4983: // Copy the bootstrap specifiers only sspitsyn@4983: Copy::conjoint_memory_atomic(operands()->adr_at(2*old_len), sspitsyn@4983: new_ops->adr_at(2*new_len), sspitsyn@4983: (min_size - 2*min_len) * sizeof(u2)); sspitsyn@4983: // Explicitly deallocate old operands array. sspitsyn@4983: // Note, it is not needed for 7u backport. sspitsyn@4983: if ( operands() != NULL) { // the safety check sspitsyn@4983: MetadataFactory::free_array(loader_data, operands()); sspitsyn@4983: } sspitsyn@4983: set_operands(new_ops); sspitsyn@4983: } // end resize_operands() sspitsyn@4983: sspitsyn@4983: sspitsyn@4983: // Extend the operands array with the length and size of the ext_cp operands. sspitsyn@4983: // Used in RedefineClasses for CP merge. sspitsyn@4983: void ConstantPool::extend_operands(constantPoolHandle ext_cp, TRAPS) { sspitsyn@4983: int delta_len = operand_array_length(ext_cp->operands()); sspitsyn@4983: if (delta_len == 0) { sspitsyn@4983: return; // nothing to do sspitsyn@4983: } sspitsyn@4983: int delta_size = ext_cp->operands()->length(); sspitsyn@4983: sspitsyn@4983: assert(delta_len > 0 && delta_size > 0, "extended operands array must be bigger"); sspitsyn@4983: sspitsyn@4983: if (operand_array_length(operands()) == 0) { sspitsyn@4983: ClassLoaderData* loader_data = pool_holder()->class_loader_data(); sspitsyn@4983: Array* new_ops = MetadataFactory::new_array(loader_data, delta_size, CHECK); sspitsyn@4983: // The first element index defines the offset of second part sspitsyn@4983: operand_offset_at_put(new_ops, 0, 2*delta_len); // offset in new array sspitsyn@4983: set_operands(new_ops); sspitsyn@4983: } else { sspitsyn@4983: resize_operands(delta_len, delta_size, CHECK); sspitsyn@4983: } sspitsyn@4983: sspitsyn@4983: } // end extend_operands() sspitsyn@4983: sspitsyn@4983: sspitsyn@4983: // Shrink the operands array to a smaller array with new_len length. sspitsyn@4983: // Used in RedefineClasses for CP merge. sspitsyn@4983: void ConstantPool::shrink_operands(int new_len, TRAPS) { sspitsyn@4983: int old_len = operand_array_length(operands()); sspitsyn@4983: if (new_len == old_len) { sspitsyn@4983: return; // nothing to do sspitsyn@4983: } sspitsyn@4983: assert(new_len < old_len, "shrunken operands array must be smaller"); sspitsyn@4983: sspitsyn@4983: int free_base = operand_next_offset_at(new_len - 1); sspitsyn@4983: int delta_len = new_len - old_len; sspitsyn@4983: int delta_size = 2*delta_len + free_base - operands()->length(); sspitsyn@4983: sspitsyn@4983: resize_operands(delta_len, delta_size, CHECK); sspitsyn@4983: sspitsyn@4983: } // end shrink_operands() sspitsyn@4983: sspitsyn@4983: sspitsyn@4493: void ConstantPool::copy_operands(constantPoolHandle from_cp, sspitsyn@4493: constantPoolHandle to_cp, sspitsyn@4493: TRAPS) { jrose@2353: jrose@2353: int from_oplen = operand_array_length(from_cp->operands()); jrose@2353: int old_oplen = operand_array_length(to_cp->operands()); jrose@2353: if (from_oplen != 0) { kamg@4245: ClassLoaderData* loader_data = to_cp->pool_holder()->class_loader_data(); jrose@2353: // append my operands to the target's operands array jrose@2353: if (old_oplen == 0) { kamg@4245: // Can't just reuse from_cp's operand list because of deallocation issues kamg@4245: int len = from_cp->operands()->length(); kamg@4245: Array* new_ops = MetadataFactory::new_array(loader_data, len, CHECK); kamg@4245: Copy::conjoint_memory_atomic( kamg@4245: from_cp->operands()->adr_at(0), new_ops->adr_at(0), len * sizeof(u2)); kamg@4245: to_cp->set_operands(new_ops); jrose@2353: } else { jrose@2353: int old_len = to_cp->operands()->length(); jrose@2353: int from_len = from_cp->operands()->length(); jrose@2353: int old_off = old_oplen * sizeof(u2); jrose@2353: int from_off = from_oplen * sizeof(u2); coleenp@4037: // Use the metaspace for the destination constant pool coleenp@4037: Array* new_operands = MetadataFactory::new_array(loader_data, old_len + from_len, CHECK); jrose@2353: int fillp = 0, len = 0; jrose@2353: // first part of dest coleenp@4037: Copy::conjoint_memory_atomic(to_cp->operands()->adr_at(0), coleenp@4037: new_operands->adr_at(fillp), jrose@2353: (len = old_off) * sizeof(u2)); jrose@2353: fillp += len; jrose@2353: // first part of src sspitsyn@4505: Copy::conjoint_memory_atomic(from_cp->operands()->adr_at(0), coleenp@4037: new_operands->adr_at(fillp), jrose@2353: (len = from_off) * sizeof(u2)); jrose@2353: fillp += len; jrose@2353: // second part of dest coleenp@4037: Copy::conjoint_memory_atomic(to_cp->operands()->adr_at(old_off), coleenp@4037: new_operands->adr_at(fillp), jrose@2353: (len = old_len - old_off) * sizeof(u2)); jrose@2353: fillp += len; jrose@2353: // second part of src sspitsyn@4505: Copy::conjoint_memory_atomic(from_cp->operands()->adr_at(from_off), coleenp@4037: new_operands->adr_at(fillp), jrose@2353: (len = from_len - from_off) * sizeof(u2)); jrose@2353: fillp += len; jrose@2353: assert(fillp == new_operands->length(), ""); jrose@2353: jrose@2353: // Adjust indexes in the first part of the copied operands array. jrose@2353: for (int j = 0; j < from_oplen; j++) { coleenp@4037: int offset = operand_offset_at(new_operands, old_oplen + j); jrose@2353: assert(offset == operand_offset_at(from_cp->operands(), j), "correct copy"); jrose@2353: offset += old_len; // every new tuple is preceded by old_len extra u2's coleenp@4037: operand_offset_at_put(new_operands, old_oplen + j, offset); jrose@2353: } jrose@2353: jrose@2353: // replace target operands array with combined array coleenp@4037: to_cp->set_operands(new_operands); jrose@2353: } jrose@2353: } sspitsyn@4493: } // end copy_operands() jrose@2353: sspitsyn@4493: sspitsyn@4493: // Copy this constant pool's entries at start_i to end_i (inclusive) sspitsyn@4493: // to the constant pool to_cp's entries starting at to_i. A total of sspitsyn@4493: // (end_i - start_i) + 1 entries are copied. sspitsyn@4493: void ConstantPool::copy_cp_to_impl(constantPoolHandle from_cp, int start_i, int end_i, sspitsyn@4493: constantPoolHandle to_cp, int to_i, TRAPS) { sspitsyn@4493: sspitsyn@4493: sspitsyn@4493: int dest_i = to_i; // leave original alone for debug purposes sspitsyn@4493: sspitsyn@4493: for (int src_i = start_i; src_i <= end_i; /* see loop bottom */ ) { sspitsyn@4493: copy_entry_to(from_cp, src_i, to_cp, dest_i, CHECK); sspitsyn@4493: sspitsyn@4493: switch (from_cp->tag_at(src_i).value()) { sspitsyn@4493: case JVM_CONSTANT_Double: sspitsyn@4493: case JVM_CONSTANT_Long: sspitsyn@4493: // double and long take two constant pool entries sspitsyn@4493: src_i += 2; sspitsyn@4493: dest_i += 2; sspitsyn@4493: break; sspitsyn@4493: sspitsyn@4493: default: sspitsyn@4493: // all others take one constant pool entry sspitsyn@4493: src_i++; sspitsyn@4493: dest_i++; sspitsyn@4493: break; sspitsyn@4493: } sspitsyn@4493: } sspitsyn@4493: copy_operands(from_cp, to_cp, CHECK); sspitsyn@4493: sspitsyn@4493: } // end copy_cp_to_impl() duke@435: duke@435: duke@435: // Copy this constant pool's entry at from_i to the constant pool duke@435: // to_cp's entry at to_i. coleenp@4037: void ConstantPool::copy_entry_to(constantPoolHandle from_cp, int from_i, jrose@2353: constantPoolHandle to_cp, int to_i, jrose@2353: TRAPS) { duke@435: jrose@2353: int tag = from_cp->tag_at(from_i).value(); jrose@2353: switch (tag) { duke@435: case JVM_CONSTANT_Class: duke@435: { coleenp@4037: Klass* k = from_cp->klass_at(from_i, CHECK); duke@435: to_cp->klass_at_put(to_i, k); duke@435: } break; duke@435: duke@435: case JVM_CONSTANT_ClassIndex: duke@435: { jrose@2353: jint ki = from_cp->klass_index_at(from_i); duke@435: to_cp->klass_index_at_put(to_i, ki); duke@435: } break; duke@435: duke@435: case JVM_CONSTANT_Double: duke@435: { jrose@2353: jdouble d = from_cp->double_at(from_i); duke@435: to_cp->double_at_put(to_i, d); duke@435: // double takes two constant pool entries so init second entry's tag duke@435: to_cp->tag_at_put(to_i + 1, JVM_CONSTANT_Invalid); duke@435: } break; duke@435: duke@435: case JVM_CONSTANT_Fieldref: duke@435: { jrose@2353: int class_index = from_cp->uncached_klass_ref_index_at(from_i); jrose@2353: int name_and_type_index = from_cp->uncached_name_and_type_ref_index_at(from_i); duke@435: to_cp->field_at_put(to_i, class_index, name_and_type_index); duke@435: } break; duke@435: duke@435: case JVM_CONSTANT_Float: duke@435: { jrose@2353: jfloat f = from_cp->float_at(from_i); duke@435: to_cp->float_at_put(to_i, f); duke@435: } break; duke@435: duke@435: case JVM_CONSTANT_Integer: duke@435: { jrose@2353: jint i = from_cp->int_at(from_i); duke@435: to_cp->int_at_put(to_i, i); duke@435: } break; duke@435: duke@435: case JVM_CONSTANT_InterfaceMethodref: duke@435: { jrose@2353: int class_index = from_cp->uncached_klass_ref_index_at(from_i); jrose@2353: int name_and_type_index = from_cp->uncached_name_and_type_ref_index_at(from_i); duke@435: to_cp->interface_method_at_put(to_i, class_index, name_and_type_index); duke@435: } break; duke@435: duke@435: case JVM_CONSTANT_Long: duke@435: { jrose@2353: jlong l = from_cp->long_at(from_i); duke@435: to_cp->long_at_put(to_i, l); duke@435: // long takes two constant pool entries so init second entry's tag duke@435: to_cp->tag_at_put(to_i + 1, JVM_CONSTANT_Invalid); duke@435: } break; duke@435: duke@435: case JVM_CONSTANT_Methodref: duke@435: { jrose@2353: int class_index = from_cp->uncached_klass_ref_index_at(from_i); jrose@2353: int name_and_type_index = from_cp->uncached_name_and_type_ref_index_at(from_i); duke@435: to_cp->method_at_put(to_i, class_index, name_and_type_index); duke@435: } break; duke@435: duke@435: case JVM_CONSTANT_NameAndType: duke@435: { jrose@2353: int name_ref_index = from_cp->name_ref_index_at(from_i); jrose@2353: int signature_ref_index = from_cp->signature_ref_index_at(from_i); duke@435: to_cp->name_and_type_at_put(to_i, name_ref_index, signature_ref_index); duke@435: } break; duke@435: duke@435: case JVM_CONSTANT_StringIndex: duke@435: { jrose@2353: jint si = from_cp->string_index_at(from_i); duke@435: to_cp->string_index_at_put(to_i, si); duke@435: } break; duke@435: duke@435: case JVM_CONSTANT_UnresolvedClass: duke@435: { coleenp@2614: // Can be resolved after checking tag, so check the slot first. coleenp@2614: CPSlot entry = from_cp->slot_at(from_i); coleenp@4037: if (entry.is_resolved()) { coleenp@4037: assert(entry.get_klass()->is_klass(), "must be"); coleenp@2614: // Already resolved coleenp@4037: to_cp->klass_at_put(to_i, entry.get_klass()); coleenp@2614: } else { coleenp@2614: to_cp->unresolved_klass_at_put(to_i, entry.get_symbol()); coleenp@2614: } duke@435: } break; duke@435: duke@435: case JVM_CONSTANT_UnresolvedClassInError: duke@435: { coleenp@2497: Symbol* k = from_cp->unresolved_klass_at(from_i); duke@435: to_cp->unresolved_klass_at_put(to_i, k); duke@435: to_cp->tag_at_put(to_i, JVM_CONSTANT_UnresolvedClassInError); duke@435: } break; duke@435: duke@435: coleenp@4037: case JVM_CONSTANT_String: duke@435: { coleenp@4037: Symbol* s = from_cp->unresolved_string_at(from_i); coleenp@4037: to_cp->unresolved_string_at_put(to_i, s); duke@435: } break; duke@435: duke@435: case JVM_CONSTANT_Utf8: duke@435: { coleenp@2497: Symbol* s = from_cp->symbol_at(from_i); coleenp@4037: // Need to increase refcount, the old one will be thrown away and deferenced coleenp@4037: s->increment_refcount(); duke@435: to_cp->symbol_at_put(to_i, s); duke@435: } break; duke@435: jrose@1957: case JVM_CONSTANT_MethodType: jrose@1957: { jrose@2353: jint k = from_cp->method_type_index_at(from_i); jrose@1957: to_cp->method_type_index_at_put(to_i, k); jrose@1957: } break; jrose@1957: jrose@1957: case JVM_CONSTANT_MethodHandle: jrose@1957: { jrose@2353: int k1 = from_cp->method_handle_ref_kind_at(from_i); jrose@2353: int k2 = from_cp->method_handle_index_at(from_i); jrose@1957: to_cp->method_handle_index_at_put(to_i, k1, k2); jrose@1957: } break; jrose@1957: jrose@2015: case JVM_CONSTANT_InvokeDynamic: jrose@2015: { jrose@2353: int k1 = from_cp->invoke_dynamic_bootstrap_specifier_index(from_i); jrose@2353: int k2 = from_cp->invoke_dynamic_name_and_type_ref_index_at(from_i); jrose@2353: k1 += operand_array_length(to_cp->operands()); // to_cp might already have operands jrose@2353: to_cp->invoke_dynamic_at_put(to_i, k1, k2); jrose@2015: } break; jrose@2015: duke@435: // Invalid is used as the tag for the second constant pool entry duke@435: // occupied by JVM_CONSTANT_Double or JVM_CONSTANT_Long. It should duke@435: // not be seen by itself. duke@435: case JVM_CONSTANT_Invalid: // fall through duke@435: duke@435: default: duke@435: { duke@435: ShouldNotReachHere(); duke@435: } break; duke@435: } duke@435: } // end copy_entry_to() duke@435: duke@435: duke@435: // Search constant pool search_cp for an entry that matches this duke@435: // constant pool's entry at pattern_i. Returns the index of a duke@435: // matching entry or zero (0) if there is no matching entry. coleenp@4037: int ConstantPool::find_matching_entry(int pattern_i, duke@435: constantPoolHandle search_cp, TRAPS) { duke@435: duke@435: // index zero (0) is not used duke@435: for (int i = 1; i < search_cp->length(); i++) { duke@435: bool found = compare_entry_to(pattern_i, search_cp, i, CHECK_0); duke@435: if (found) { duke@435: return i; duke@435: } duke@435: } duke@435: duke@435: return 0; // entry not found; return unused index zero (0) duke@435: } // end find_matching_entry() duke@435: duke@435: sspitsyn@4983: // Compare this constant pool's bootstrap specifier at idx1 to the constant pool sspitsyn@4983: // cp2's bootstrap specifier at idx2. sspitsyn@4983: bool ConstantPool::compare_operand_to(int idx1, constantPoolHandle cp2, int idx2, TRAPS) { sspitsyn@4983: int k1 = operand_bootstrap_method_ref_index_at(idx1); sspitsyn@4983: int k2 = cp2->operand_bootstrap_method_ref_index_at(idx2); sspitsyn@4983: bool match = compare_entry_to(k1, cp2, k2, CHECK_false); sspitsyn@4983: sspitsyn@4983: if (!match) { sspitsyn@4983: return false; sspitsyn@4983: } sspitsyn@4983: int argc = operand_argument_count_at(idx1); sspitsyn@4983: if (argc == cp2->operand_argument_count_at(idx2)) { sspitsyn@4983: for (int j = 0; j < argc; j++) { sspitsyn@4983: k1 = operand_argument_index_at(idx1, j); sspitsyn@4983: k2 = cp2->operand_argument_index_at(idx2, j); sspitsyn@4983: match = compare_entry_to(k1, cp2, k2, CHECK_false); sspitsyn@4983: if (!match) { sspitsyn@4983: return false; sspitsyn@4983: } sspitsyn@4983: } sspitsyn@4983: return true; // got through loop; all elements equal sspitsyn@4983: } sspitsyn@4983: return false; sspitsyn@4983: } // end compare_operand_to() sspitsyn@4983: sspitsyn@4983: // Search constant pool search_cp for a bootstrap specifier that matches sspitsyn@4983: // this constant pool's bootstrap specifier at pattern_i index. sspitsyn@4983: // Return the index of a matching bootstrap specifier or (-1) if there is no match. sspitsyn@4983: int ConstantPool::find_matching_operand(int pattern_i, sspitsyn@4983: constantPoolHandle search_cp, int search_len, TRAPS) { sspitsyn@4983: for (int i = 0; i < search_len; i++) { sspitsyn@4983: bool found = compare_operand_to(pattern_i, search_cp, i, CHECK_(-1)); sspitsyn@4983: if (found) { sspitsyn@4983: return i; sspitsyn@4983: } sspitsyn@4983: } sspitsyn@4983: return -1; // bootstrap specifier not found; return unused index (-1) sspitsyn@4983: } // end find_matching_operand() sspitsyn@4983: sspitsyn@4983: duke@435: #ifndef PRODUCT duke@435: coleenp@4037: const char* ConstantPool::printable_name_at(int which) { duke@435: duke@435: constantTag tag = tag_at(which); duke@435: coleenp@4037: if (tag.is_string()) { duke@435: return string_at_noresolve(which); duke@435: } else if (tag.is_klass() || tag.is_unresolved_klass()) { duke@435: return klass_name_at(which)->as_C_string(); duke@435: } else if (tag.is_symbol()) { duke@435: return symbol_at(which)->as_C_string(); duke@435: } duke@435: return ""; duke@435: } duke@435: duke@435: #endif // PRODUCT duke@435: duke@435: duke@435: // JVMTI GetConstantPool support duke@435: mikael@4889: // For debugging of constant pool mikael@4889: const bool debug_cpool = false; duke@435: mikael@4889: #define DBG(code) do { if (debug_cpool) { (code); } } while(0) duke@435: duke@435: static void print_cpool_bytes(jint cnt, u1 *bytes) { mikael@4889: const char* WARN_MSG = "Must not be such entry!"; duke@435: jint size = 0; duke@435: u2 idx1, idx2; duke@435: duke@435: for (jint idx = 1; idx < cnt; idx++) { duke@435: jint ent_size = 0; duke@435: u1 tag = *bytes++; duke@435: size++; // count tag duke@435: duke@435: printf("const #%03d, tag: %02d ", idx, tag); duke@435: switch(tag) { duke@435: case JVM_CONSTANT_Invalid: { duke@435: printf("Invalid"); duke@435: break; duke@435: } duke@435: case JVM_CONSTANT_Unicode: { duke@435: printf("Unicode %s", WARN_MSG); duke@435: break; duke@435: } duke@435: case JVM_CONSTANT_Utf8: { duke@435: u2 len = Bytes::get_Java_u2(bytes); duke@435: char str[128]; duke@435: if (len > 127) { duke@435: len = 127; duke@435: } duke@435: strncpy(str, (char *) (bytes+2), len); duke@435: str[len] = '\0'; duke@435: printf("Utf8 \"%s\"", str); duke@435: ent_size = 2 + len; duke@435: break; duke@435: } duke@435: case JVM_CONSTANT_Integer: { duke@435: u4 val = Bytes::get_Java_u4(bytes); duke@435: printf("int %d", *(int *) &val); duke@435: ent_size = 4; duke@435: break; duke@435: } duke@435: case JVM_CONSTANT_Float: { duke@435: u4 val = Bytes::get_Java_u4(bytes); duke@435: printf("float %5.3ff", *(float *) &val); duke@435: ent_size = 4; duke@435: break; duke@435: } duke@435: case JVM_CONSTANT_Long: { duke@435: u8 val = Bytes::get_Java_u8(bytes); never@3156: printf("long "INT64_FORMAT, (int64_t) *(jlong *) &val); duke@435: ent_size = 8; duke@435: idx++; // Long takes two cpool slots duke@435: break; duke@435: } duke@435: case JVM_CONSTANT_Double: { duke@435: u8 val = Bytes::get_Java_u8(bytes); duke@435: printf("double %5.3fd", *(jdouble *)&val); duke@435: ent_size = 8; duke@435: idx++; // Double takes two cpool slots duke@435: break; duke@435: } duke@435: case JVM_CONSTANT_Class: { duke@435: idx1 = Bytes::get_Java_u2(bytes); duke@435: printf("class #%03d", idx1); duke@435: ent_size = 2; duke@435: break; duke@435: } duke@435: case JVM_CONSTANT_String: { duke@435: idx1 = Bytes::get_Java_u2(bytes); duke@435: printf("String #%03d", idx1); duke@435: ent_size = 2; duke@435: break; duke@435: } duke@435: case JVM_CONSTANT_Fieldref: { duke@435: idx1 = Bytes::get_Java_u2(bytes); duke@435: idx2 = Bytes::get_Java_u2(bytes+2); duke@435: printf("Field #%03d, #%03d", (int) idx1, (int) idx2); duke@435: ent_size = 4; duke@435: break; duke@435: } duke@435: case JVM_CONSTANT_Methodref: { duke@435: idx1 = Bytes::get_Java_u2(bytes); duke@435: idx2 = Bytes::get_Java_u2(bytes+2); duke@435: printf("Method #%03d, #%03d", idx1, idx2); duke@435: ent_size = 4; duke@435: break; duke@435: } duke@435: case JVM_CONSTANT_InterfaceMethodref: { duke@435: idx1 = Bytes::get_Java_u2(bytes); duke@435: idx2 = Bytes::get_Java_u2(bytes+2); duke@435: printf("InterfMethod #%03d, #%03d", idx1, idx2); duke@435: ent_size = 4; duke@435: break; duke@435: } duke@435: case JVM_CONSTANT_NameAndType: { duke@435: idx1 = Bytes::get_Java_u2(bytes); duke@435: idx2 = Bytes::get_Java_u2(bytes+2); duke@435: printf("NameAndType #%03d, #%03d", idx1, idx2); duke@435: ent_size = 4; duke@435: break; duke@435: } duke@435: case JVM_CONSTANT_ClassIndex: { duke@435: printf("ClassIndex %s", WARN_MSG); duke@435: break; duke@435: } duke@435: case JVM_CONSTANT_UnresolvedClass: { duke@435: printf("UnresolvedClass: %s", WARN_MSG); duke@435: break; duke@435: } duke@435: case JVM_CONSTANT_UnresolvedClassInError: { duke@435: printf("UnresolvedClassInErr: %s", WARN_MSG); duke@435: break; duke@435: } duke@435: case JVM_CONSTANT_StringIndex: { duke@435: printf("StringIndex: %s", WARN_MSG); duke@435: break; duke@435: } duke@435: } duke@435: printf(";\n"); duke@435: bytes += ent_size; duke@435: size += ent_size; duke@435: } duke@435: printf("Cpool size: %d\n", size); duke@435: fflush(0); duke@435: return; duke@435: } /* end print_cpool_bytes */ duke@435: duke@435: duke@435: // Returns size of constant pool entry. coleenp@4037: jint ConstantPool::cpool_entry_size(jint idx) { duke@435: switch(tag_at(idx).value()) { duke@435: case JVM_CONSTANT_Invalid: duke@435: case JVM_CONSTANT_Unicode: duke@435: return 1; duke@435: duke@435: case JVM_CONSTANT_Utf8: duke@435: return 3 + symbol_at(idx)->utf8_length(); duke@435: duke@435: case JVM_CONSTANT_Class: duke@435: case JVM_CONSTANT_String: duke@435: case JVM_CONSTANT_ClassIndex: duke@435: case JVM_CONSTANT_UnresolvedClass: duke@435: case JVM_CONSTANT_UnresolvedClassInError: duke@435: case JVM_CONSTANT_StringIndex: jrose@1957: case JVM_CONSTANT_MethodType: iklam@5760: case JVM_CONSTANT_MethodTypeInError: duke@435: return 3; duke@435: jrose@1957: case JVM_CONSTANT_MethodHandle: iklam@5760: case JVM_CONSTANT_MethodHandleInError: jrose@1957: return 4; //tag, ref_kind, ref_index jrose@1957: duke@435: case JVM_CONSTANT_Integer: duke@435: case JVM_CONSTANT_Float: duke@435: case JVM_CONSTANT_Fieldref: duke@435: case JVM_CONSTANT_Methodref: duke@435: case JVM_CONSTANT_InterfaceMethodref: duke@435: case JVM_CONSTANT_NameAndType: jrose@2268: return 5; jrose@2268: jrose@2015: case JVM_CONSTANT_InvokeDynamic: jrose@2353: // u1 tag, u2 bsm, u2 nt jrose@2353: return 5; duke@435: duke@435: case JVM_CONSTANT_Long: duke@435: case JVM_CONSTANT_Double: duke@435: return 9; duke@435: } duke@435: assert(false, "cpool_entry_size: Invalid constant pool entry tag"); duke@435: return 1; duke@435: } /* end cpool_entry_size */ duke@435: duke@435: duke@435: // SymbolHashMap is used to find a constant pool index from a string. duke@435: // This function fills in SymbolHashMaps, one for utf8s and one for duke@435: // class names, returns size of the cpool raw bytes. coleenp@4037: jint ConstantPool::hash_entries_to(SymbolHashMap *symmap, duke@435: SymbolHashMap *classmap) { duke@435: jint size = 0; duke@435: duke@435: for (u2 idx = 1; idx < length(); idx++) { duke@435: u2 tag = tag_at(idx).value(); duke@435: size += cpool_entry_size(idx); duke@435: duke@435: switch(tag) { duke@435: case JVM_CONSTANT_Utf8: { coleenp@2497: Symbol* sym = symbol_at(idx); duke@435: symmap->add_entry(sym, idx); duke@435: DBG(printf("adding symbol entry %s = %d\n", sym->as_utf8(), idx)); duke@435: break; duke@435: } duke@435: case JVM_CONSTANT_Class: duke@435: case JVM_CONSTANT_UnresolvedClass: duke@435: case JVM_CONSTANT_UnresolvedClassInError: { coleenp@2497: Symbol* sym = klass_name_at(idx); duke@435: classmap->add_entry(sym, idx); duke@435: DBG(printf("adding class entry %s = %d\n", sym->as_utf8(), idx)); duke@435: break; duke@435: } duke@435: case JVM_CONSTANT_Long: duke@435: case JVM_CONSTANT_Double: { duke@435: idx++; // Both Long and Double take two cpool slots duke@435: break; duke@435: } duke@435: } duke@435: } duke@435: return size; duke@435: } /* end hash_utf8_entries_to */ duke@435: duke@435: duke@435: // Copy cpool bytes. duke@435: // Returns: duke@435: // 0, in case of OutOfMemoryError duke@435: // -1, in case of internal error duke@435: // > 0, count of the raw cpool bytes that have been copied coleenp@4037: int ConstantPool::copy_cpool_bytes(int cpool_size, duke@435: SymbolHashMap* tbl, duke@435: unsigned char *bytes) { duke@435: u2 idx1, idx2; duke@435: jint size = 0; duke@435: jint cnt = length(); duke@435: unsigned char *start_bytes = bytes; duke@435: duke@435: for (jint idx = 1; idx < cnt; idx++) { duke@435: u1 tag = tag_at(idx).value(); duke@435: jint ent_size = cpool_entry_size(idx); duke@435: duke@435: assert(size + ent_size <= cpool_size, "Size mismatch"); duke@435: duke@435: *bytes = tag; duke@435: DBG(printf("#%03hd tag=%03hd, ", idx, tag)); duke@435: switch(tag) { duke@435: case JVM_CONSTANT_Invalid: { duke@435: DBG(printf("JVM_CONSTANT_Invalid")); duke@435: break; duke@435: } duke@435: case JVM_CONSTANT_Unicode: { duke@435: assert(false, "Wrong constant pool tag: JVM_CONSTANT_Unicode"); duke@435: DBG(printf("JVM_CONSTANT_Unicode")); duke@435: break; duke@435: } duke@435: case JVM_CONSTANT_Utf8: { coleenp@2497: Symbol* sym = symbol_at(idx); duke@435: char* str = sym->as_utf8(); duke@435: // Warning! It's crashing on x86 with len = sym->utf8_length() duke@435: int len = (int) strlen(str); duke@435: Bytes::put_Java_u2((address) (bytes+1), (u2) len); duke@435: for (int i = 0; i < len; i++) { duke@435: bytes[3+i] = (u1) str[i]; duke@435: } duke@435: DBG(printf("JVM_CONSTANT_Utf8: %s ", str)); duke@435: break; duke@435: } duke@435: case JVM_CONSTANT_Integer: { duke@435: jint val = int_at(idx); duke@435: Bytes::put_Java_u4((address) (bytes+1), *(u4*)&val); duke@435: break; duke@435: } duke@435: case JVM_CONSTANT_Float: { duke@435: jfloat val = float_at(idx); duke@435: Bytes::put_Java_u4((address) (bytes+1), *(u4*)&val); duke@435: break; duke@435: } duke@435: case JVM_CONSTANT_Long: { duke@435: jlong val = long_at(idx); duke@435: Bytes::put_Java_u8((address) (bytes+1), *(u8*)&val); duke@435: idx++; // Long takes two cpool slots duke@435: break; duke@435: } duke@435: case JVM_CONSTANT_Double: { duke@435: jdouble val = double_at(idx); duke@435: Bytes::put_Java_u8((address) (bytes+1), *(u8*)&val); duke@435: idx++; // Double takes two cpool slots duke@435: break; duke@435: } duke@435: case JVM_CONSTANT_Class: duke@435: case JVM_CONSTANT_UnresolvedClass: duke@435: case JVM_CONSTANT_UnresolvedClassInError: { duke@435: *bytes = JVM_CONSTANT_Class; coleenp@2497: Symbol* sym = klass_name_at(idx); duke@435: idx1 = tbl->symbol_to_value(sym); duke@435: assert(idx1 != 0, "Have not found a hashtable entry"); duke@435: Bytes::put_Java_u2((address) (bytes+1), idx1); duke@435: DBG(printf("JVM_CONSTANT_Class: idx=#%03hd, %s", idx1, sym->as_utf8())); duke@435: break; duke@435: } duke@435: case JVM_CONSTANT_String: { duke@435: *bytes = JVM_CONSTANT_String; coleenp@2497: Symbol* sym = unresolved_string_at(idx); duke@435: idx1 = tbl->symbol_to_value(sym); duke@435: assert(idx1 != 0, "Have not found a hashtable entry"); duke@435: Bytes::put_Java_u2((address) (bytes+1), idx1); mikael@4889: DBG(printf("JVM_CONSTANT_String: idx=#%03hd, %s", idx1, sym->as_utf8())); duke@435: break; duke@435: } duke@435: case JVM_CONSTANT_Fieldref: duke@435: case JVM_CONSTANT_Methodref: duke@435: case JVM_CONSTANT_InterfaceMethodref: { duke@435: idx1 = uncached_klass_ref_index_at(idx); duke@435: idx2 = uncached_name_and_type_ref_index_at(idx); duke@435: Bytes::put_Java_u2((address) (bytes+1), idx1); duke@435: Bytes::put_Java_u2((address) (bytes+3), idx2); duke@435: DBG(printf("JVM_CONSTANT_Methodref: %hd %hd", idx1, idx2)); duke@435: break; duke@435: } duke@435: case JVM_CONSTANT_NameAndType: { duke@435: idx1 = name_ref_index_at(idx); duke@435: idx2 = signature_ref_index_at(idx); duke@435: Bytes::put_Java_u2((address) (bytes+1), idx1); duke@435: Bytes::put_Java_u2((address) (bytes+3), idx2); duke@435: DBG(printf("JVM_CONSTANT_NameAndType: %hd %hd", idx1, idx2)); duke@435: break; duke@435: } duke@435: case JVM_CONSTANT_ClassIndex: { duke@435: *bytes = JVM_CONSTANT_Class; duke@435: idx1 = klass_index_at(idx); duke@435: Bytes::put_Java_u2((address) (bytes+1), idx1); duke@435: DBG(printf("JVM_CONSTANT_ClassIndex: %hd", idx1)); duke@435: break; duke@435: } duke@435: case JVM_CONSTANT_StringIndex: { duke@435: *bytes = JVM_CONSTANT_String; duke@435: idx1 = string_index_at(idx); duke@435: Bytes::put_Java_u2((address) (bytes+1), idx1); duke@435: DBG(printf("JVM_CONSTANT_StringIndex: %hd", idx1)); duke@435: break; duke@435: } coleenp@4037: case JVM_CONSTANT_MethodHandle: coleenp@4037: case JVM_CONSTANT_MethodHandleInError: { jrose@1957: *bytes = JVM_CONSTANT_MethodHandle; iklam@5760: int kind = method_handle_ref_kind_at_error_ok(idx); iklam@5760: idx1 = method_handle_index_at_error_ok(idx); jrose@1957: *(bytes+1) = (unsigned char) kind; jrose@1957: Bytes::put_Java_u2((address) (bytes+2), idx1); jrose@1957: DBG(printf("JVM_CONSTANT_MethodHandle: %d %hd", kind, idx1)); jrose@1957: break; jrose@1957: } coleenp@4037: case JVM_CONSTANT_MethodType: coleenp@4037: case JVM_CONSTANT_MethodTypeInError: { jrose@1957: *bytes = JVM_CONSTANT_MethodType; iklam@5760: idx1 = method_type_index_at_error_ok(idx); jrose@1957: Bytes::put_Java_u2((address) (bytes+1), idx1); jrose@1957: DBG(printf("JVM_CONSTANT_MethodType: %hd", idx1)); jrose@1957: break; jrose@1957: } jrose@2015: case JVM_CONSTANT_InvokeDynamic: { jrose@2353: *bytes = tag; jrose@2353: idx1 = extract_low_short_from_int(*int_at_addr(idx)); jrose@2353: idx2 = extract_high_short_from_int(*int_at_addr(idx)); jrose@2353: assert(idx2 == invoke_dynamic_name_and_type_ref_index_at(idx), "correct half of u4"); jrose@2015: Bytes::put_Java_u2((address) (bytes+1), idx1); jrose@2015: Bytes::put_Java_u2((address) (bytes+3), idx2); jrose@2353: DBG(printf("JVM_CONSTANT_InvokeDynamic: %hd %hd", idx1, idx2)); jrose@2015: break; jrose@2015: } duke@435: } duke@435: DBG(printf("\n")); duke@435: bytes += ent_size; duke@435: size += ent_size; duke@435: } duke@435: assert(size == cpool_size, "Size mismatch"); duke@435: duke@435: // Keep temorarily for debugging until it's stable. duke@435: DBG(print_cpool_bytes(cnt, start_bytes)); duke@435: return (int)(bytes - start_bytes); duke@435: } /* end copy_cpool_bytes */ duke@435: mikael@4889: #undef DBG mikael@4889: duke@435: coleenp@4037: void ConstantPool::set_on_stack(const bool value) { coleenp@4490: if (value) { coleenp@4490: _flags |= _on_stack; coleenp@4490: } else { coleenp@4490: _flags &= ~_on_stack; coleenp@4490: } coleenp@4037: if (value) MetadataOnStackMark::record(this); coleenp@4037: } coleenp@4037: coleenp@4037: // JSR 292 support for patching constant pool oops after the class is linked and coleenp@4037: // the oop array for resolved references are created. coleenp@4037: // We can't do this during classfile parsing, which is how the other indexes are coleenp@4037: // patched. The other patches are applied early for some error checking coleenp@4037: // so only defer the pseudo_strings. coleenp@4037: void ConstantPool::patch_resolved_references( coleenp@4037: GrowableArray* cp_patches) { coleenp@4037: assert(EnableInvokeDynamic, ""); coleenp@4037: for (int index = 1; index < cp_patches->length(); index++) { // Index 0 is unused coleenp@4037: Handle patch = cp_patches->at(index); coleenp@4037: if (patch.not_null()) { coleenp@4037: assert (tag_at(index).is_string(), "should only be string left"); coleenp@4037: // Patching a string means pre-resolving it. coleenp@4037: // The spelling in the constant pool is ignored. coleenp@4037: // The constant reference may be any object whatever. coleenp@4037: // If it is not a real interned string, the constant is referred coleenp@4037: // to as a "pseudo-string", and must be presented to the CP coleenp@4037: // explicitly, because it may require scavenging. coleenp@4037: int obj_index = cp_to_object_index(index); coleenp@4037: pseudo_string_at_put(index, obj_index, patch()); coleenp@4037: DEBUG_ONLY(cp_patches->at_put(index, Handle());) coleenp@4037: } coleenp@4037: } coleenp@4037: #ifdef ASSERT coleenp@4037: // Ensure that all the patches have been used. coleenp@4037: for (int index = 0; index < cp_patches->length(); index++) { coleenp@4037: assert(cp_patches->at(index).is_null(), coleenp@4037: err_msg("Unused constant pool patch at %d in class file %s", coleenp@4037: index, coleenp@4251: pool_holder()->external_name())); coleenp@4037: } coleenp@4037: #endif // ASSERT coleenp@4037: } coleenp@4037: coleenp@4037: #ifndef PRODUCT coleenp@4037: coleenp@4037: // CompileTheWorld support. Preload all classes loaded references in the passed in constantpool coleenp@4037: void ConstantPool::preload_and_initialize_all_classes(ConstantPool* obj, TRAPS) { coleenp@4037: guarantee(obj->is_constantPool(), "object must be constant pool"); coleenp@4037: constantPoolHandle cp(THREAD, (ConstantPool*)obj); coleenp@4037: guarantee(cp->pool_holder() != NULL, "must be fully loaded"); coleenp@4037: coleenp@4037: for (int i = 0; i< cp->length(); i++) { coleenp@4037: if (cp->tag_at(i).is_unresolved_klass()) { coleenp@4037: // This will force loading of the class coleenp@4037: Klass* klass = cp->klass_at(i, CHECK); coleenp@4037: if (klass->oop_is_instance()) { coleenp@4037: // Force initialization of class coleenp@4037: InstanceKlass::cast(klass)->initialize(CHECK); coleenp@4037: } coleenp@4037: } coleenp@4037: } coleenp@4037: } coleenp@4037: coleenp@4037: #endif coleenp@4037: coleenp@4037: coleenp@4037: // Printing coleenp@4037: coleenp@4037: void ConstantPool::print_on(outputStream* st) const { coleenp@4037: EXCEPTION_MARK; coleenp@4037: assert(is_constantPool(), "must be constantPool"); coleenp@4037: st->print_cr(internal_name()); coleenp@4037: if (flags() != 0) { coleenp@4037: st->print(" - flags: 0x%x", flags()); coleenp@4037: if (has_preresolution()) st->print(" has_preresolution"); coleenp@4490: if (on_stack()) st->print(" on_stack"); coleenp@4037: st->cr(); coleenp@4037: } coleenp@4037: if (pool_holder() != NULL) { coleenp@4037: st->print_cr(" - holder: " INTPTR_FORMAT, pool_holder()); coleenp@4037: } coleenp@4037: st->print_cr(" - cache: " INTPTR_FORMAT, cache()); hseigel@5784: st->print_cr(" - resolved_references: " INTPTR_FORMAT, (void *)resolved_references()); coleenp@4037: st->print_cr(" - reference_map: " INTPTR_FORMAT, reference_map()); coleenp@4037: coleenp@4037: for (int index = 1; index < length(); index++) { // Index 0 is unused coleenp@4037: ((ConstantPool*)this)->print_entry_on(index, st); coleenp@4037: switch (tag_at(index).value()) { coleenp@4037: case JVM_CONSTANT_Long : coleenp@4037: case JVM_CONSTANT_Double : coleenp@4037: index++; // Skip entry following eigth-byte constant coleenp@4037: } coleenp@4037: coleenp@4037: } coleenp@4037: st->cr(); coleenp@4037: } coleenp@4037: coleenp@4037: // Print one constant pool entry coleenp@4037: void ConstantPool::print_entry_on(const int index, outputStream* st) { coleenp@4037: EXCEPTION_MARK; coleenp@4037: st->print(" - %3d : ", index); coleenp@4037: tag_at(index).print_on(st); coleenp@4037: st->print(" : "); coleenp@4037: switch (tag_at(index).value()) { coleenp@4037: case JVM_CONSTANT_Class : coleenp@4037: { Klass* k = klass_at(index, CATCH); morris@4782: guarantee(k != NULL, "need klass"); coleenp@4037: k->print_value_on(st); coleenp@4037: st->print(" {0x%lx}", (address)k); coleenp@4037: } coleenp@4037: break; coleenp@4037: case JVM_CONSTANT_Fieldref : coleenp@4037: case JVM_CONSTANT_Methodref : coleenp@4037: case JVM_CONSTANT_InterfaceMethodref : coleenp@4037: st->print("klass_index=%d", uncached_klass_ref_index_at(index)); coleenp@4037: st->print(" name_and_type_index=%d", uncached_name_and_type_ref_index_at(index)); coleenp@4037: break; coleenp@4037: case JVM_CONSTANT_String : coleenp@4643: if (is_pseudo_string_at(index)) { coleenp@4643: oop anObj = pseudo_string_at(index); coleenp@4643: anObj->print_value_on(st); coleenp@4643: st->print(" {0x%lx}", (address)anObj); coleenp@4643: } else { coleenp@4643: unresolved_string_at(index)->print_value_on(st); coleenp@4643: } coleenp@4037: break; coleenp@4037: case JVM_CONSTANT_Integer : coleenp@4037: st->print("%d", int_at(index)); coleenp@4037: break; coleenp@4037: case JVM_CONSTANT_Float : coleenp@4037: st->print("%f", float_at(index)); coleenp@4037: break; coleenp@4037: case JVM_CONSTANT_Long : coleenp@4037: st->print_jlong(long_at(index)); coleenp@4037: break; coleenp@4037: case JVM_CONSTANT_Double : coleenp@4037: st->print("%lf", double_at(index)); coleenp@4037: break; coleenp@4037: case JVM_CONSTANT_NameAndType : coleenp@4037: st->print("name_index=%d", name_ref_index_at(index)); coleenp@4037: st->print(" signature_index=%d", signature_ref_index_at(index)); coleenp@4037: break; coleenp@4037: case JVM_CONSTANT_Utf8 : coleenp@4037: symbol_at(index)->print_value_on(st); coleenp@4037: break; coleenp@4037: case JVM_CONSTANT_UnresolvedClass : // fall-through coleenp@4037: case JVM_CONSTANT_UnresolvedClassInError: { coleenp@4037: // unresolved_klass_at requires lock or safe world. coleenp@4037: CPSlot entry = slot_at(index); coleenp@4037: if (entry.is_resolved()) { coleenp@4037: entry.get_klass()->print_value_on(st); coleenp@4037: } else { coleenp@4037: entry.get_symbol()->print_value_on(st); coleenp@4037: } coleenp@4037: } coleenp@4037: break; coleenp@4037: case JVM_CONSTANT_MethodHandle : coleenp@4037: case JVM_CONSTANT_MethodHandleInError : iklam@5760: st->print("ref_kind=%d", method_handle_ref_kind_at_error_ok(index)); iklam@5760: st->print(" ref_index=%d", method_handle_index_at_error_ok(index)); coleenp@4037: break; coleenp@4037: case JVM_CONSTANT_MethodType : coleenp@4037: case JVM_CONSTANT_MethodTypeInError : iklam@5760: st->print("signature_index=%d", method_type_index_at_error_ok(index)); coleenp@4037: break; coleenp@4037: case JVM_CONSTANT_InvokeDynamic : coleenp@4037: { coleenp@4037: st->print("bootstrap_method_index=%d", invoke_dynamic_bootstrap_method_ref_index_at(index)); coleenp@4037: st->print(" name_and_type_index=%d", invoke_dynamic_name_and_type_ref_index_at(index)); coleenp@4037: int argc = invoke_dynamic_argument_count_at(index); coleenp@4037: if (argc > 0) { coleenp@4037: for (int arg_i = 0; arg_i < argc; arg_i++) { coleenp@4037: int arg = invoke_dynamic_argument_index_at(index, arg_i); coleenp@4037: st->print((arg_i == 0 ? " arguments={%d" : ", %d"), arg); coleenp@4037: } coleenp@4037: st->print("}"); coleenp@4037: } coleenp@4037: } coleenp@4037: break; coleenp@4037: default: coleenp@4037: ShouldNotReachHere(); coleenp@4037: break; coleenp@4037: } coleenp@4037: st->cr(); coleenp@4037: } coleenp@4037: coleenp@4037: void ConstantPool::print_value_on(outputStream* st) const { coleenp@4037: assert(is_constantPool(), "must be constantPool"); coleenp@4037: st->print("constant pool [%d]", length()); coleenp@4037: if (has_preresolution()) st->print("/preresolution"); coleenp@4037: if (operands() != NULL) st->print("/operands[%d]", operands()->length()); coleenp@4037: print_address_on(st); coleenp@4037: st->print(" for "); coleenp@4037: pool_holder()->print_value_on(st); coleenp@4037: if (pool_holder() != NULL) { coleenp@4251: bool extra = (pool_holder()->constants() != this); coleenp@4037: if (extra) st->print(" (extra)"); coleenp@4037: } coleenp@4037: if (cache() != NULL) { coleenp@4037: st->print(" cache=" PTR_FORMAT, cache()); coleenp@4037: } coleenp@4037: } coleenp@4037: acorn@4497: #if INCLUDE_SERVICES acorn@4497: // Size Statistics acorn@4497: void ConstantPool::collect_statistics(KlassSizeStats *sz) const { acorn@4497: sz->_cp_all_bytes += (sz->_cp_bytes = sz->count(this)); acorn@4497: sz->_cp_all_bytes += (sz->_cp_tags_bytes = sz->count_array(tags())); acorn@4497: sz->_cp_all_bytes += (sz->_cp_cache_bytes = sz->count(cache())); acorn@4497: sz->_cp_all_bytes += (sz->_cp_operands_bytes = sz->count_array(operands())); acorn@4497: sz->_cp_all_bytes += (sz->_cp_refmap_bytes = sz->count_array(reference_map())); acorn@4497: acorn@4497: sz->_ro_bytes += sz->_cp_operands_bytes + sz->_cp_tags_bytes + acorn@4497: sz->_cp_refmap_bytes; acorn@4497: sz->_rw_bytes += sz->_cp_bytes + sz->_cp_cache_bytes; acorn@4497: } acorn@4497: #endif // INCLUDE_SERVICES coleenp@4037: coleenp@4037: // Verification coleenp@4037: coleenp@4037: void ConstantPool::verify_on(outputStream* st) { coleenp@4037: guarantee(is_constantPool(), "object must be constant pool"); coleenp@4037: for (int i = 0; i< length(); i++) { coleenp@4037: constantTag tag = tag_at(i); coleenp@4037: CPSlot entry = slot_at(i); coleenp@4037: if (tag.is_klass()) { coleenp@4037: if (entry.is_resolved()) { coleenp@4037: guarantee(entry.get_klass()->is_klass(), "should be klass"); coleenp@4037: } coleenp@4037: } else if (tag.is_unresolved_klass()) { coleenp@4037: if (entry.is_resolved()) { coleenp@4037: guarantee(entry.get_klass()->is_klass(), "should be klass"); coleenp@4037: } coleenp@4037: } else if (tag.is_symbol()) { coleenp@4037: guarantee(entry.get_symbol()->refcount() != 0, "should have nonzero reference count"); coleenp@4037: } else if (tag.is_string()) { coleenp@4037: guarantee(entry.get_symbol()->refcount() != 0, "should have nonzero reference count"); coleenp@4037: } coleenp@4037: } coleenp@4037: if (cache() != NULL) { coleenp@4037: // Note: cache() can be NULL before a class is completely setup or coleenp@4037: // in temporary constant pools used during constant pool merging coleenp@4037: guarantee(cache()->is_constantPoolCache(), "should be constant pool cache"); coleenp@4037: } coleenp@4037: if (pool_holder() != NULL) { coleenp@4037: // Note: pool_holder() can be NULL in temporary constant pools coleenp@4037: // used during constant pool merging coleenp@4037: guarantee(pool_holder()->is_klass(), "should be klass"); coleenp@4037: } coleenp@4037: } coleenp@4037: coleenp@4037: coleenp@2497: void SymbolHashMap::add_entry(Symbol* sym, u2 value) { duke@435: char *str = sym->as_utf8(); duke@435: unsigned int hash = compute_hash(str, sym->utf8_length()); duke@435: unsigned int index = hash % table_size(); duke@435: duke@435: // check if already in map duke@435: // we prefer the first entry since it is more likely to be what was used in duke@435: // the class file duke@435: for (SymbolHashMapEntry *en = bucket(index); en != NULL; en = en->next()) { duke@435: assert(en->symbol() != NULL, "SymbolHashMapEntry symbol is NULL"); duke@435: if (en->hash() == hash && en->symbol() == sym) { duke@435: return; // already there duke@435: } duke@435: } duke@435: duke@435: SymbolHashMapEntry* entry = new SymbolHashMapEntry(hash, sym, value); duke@435: entry->set_next(bucket(index)); duke@435: _buckets[index].set_entry(entry); duke@435: assert(entry->symbol() != NULL, "SymbolHashMapEntry symbol is NULL"); duke@435: } duke@435: coleenp@2497: SymbolHashMapEntry* SymbolHashMap::find_entry(Symbol* sym) { duke@435: assert(sym != NULL, "SymbolHashMap::find_entry - symbol is NULL"); duke@435: char *str = sym->as_utf8(); duke@435: int len = sym->utf8_length(); duke@435: unsigned int hash = SymbolHashMap::compute_hash(str, len); duke@435: unsigned int index = hash % table_size(); duke@435: for (SymbolHashMapEntry *en = bucket(index); en != NULL; en = en->next()) { duke@435: assert(en->symbol() != NULL, "SymbolHashMapEntry symbol is NULL"); duke@435: if (en->hash() == hash && en->symbol() == sym) { duke@435: return en; duke@435: } duke@435: } duke@435: return NULL; duke@435: }