duke@435: /* xdono@1014: * Copyright 1997-2009 Sun Microsystems, Inc. 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: * duke@435: * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, duke@435: * CA 95054 USA or visit www.sun.com if you need additional information or duke@435: * have any questions. duke@435: * duke@435: */ duke@435: duke@435: # include "incls/_precompiled.incl" duke@435: # include "incls/_constantPoolOop.cpp.incl" duke@435: jrose@866: void constantPoolOopDesc::set_flag_at(FlagBit fb) { jrose@866: const int MAX_STATE_CHANGES = 2; jrose@866: for (int i = MAX_STATE_CHANGES + 10; i > 0; i--) { jrose@866: int oflags = _flags; jrose@866: int nflags = oflags | (1 << (int)fb); jrose@866: if (Atomic::cmpxchg(nflags, &_flags, oflags) == oflags) jrose@866: return; jrose@866: } jrose@866: assert(false, "failed to cmpxchg flags"); jrose@866: _flags |= (1 << (int)fb); // better than nothing jrose@866: } jrose@866: duke@435: klassOop constantPoolOopDesc::klass_at_impl(constantPoolHandle this_oop, int which, TRAPS) { duke@435: // A resolved constantPool entry will contain a klassOop, otherwise a symbolOop. 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. duke@435: oop entry = *(this_oop->obj_at_addr(which)); duke@435: if (entry->is_klass()) { duke@435: // Already resolved - return entry. duke@435: return (klassOop)entry; 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: duke@435: symbolHandle name; duke@435: Handle loader; duke@435: { ObjectLocker ol(this_oop, THREAD); 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; duke@435: name = symbolHandle(THREAD, this_oop->unresolved_klass_at(which)); duke@435: loader = Handle(THREAD, instanceKlass::cast(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) { duke@435: symbolOop error = SystemDictionary::find_resolution_error(this_oop, which); duke@435: guarantee(error != (symbolOop)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 duke@435: oop protection_domain = Klass::cast(this_oop->pool_holder())->protection_domain(); duke@435: Handle h_prot (THREAD, protection_domain); duke@435: klassOop 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); 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; duke@435: symbolHandle error(PENDING_EXCEPTION->klass()->klass_part()->name()); duke@435: duke@435: bool throw_orig_error = false; duke@435: { duke@435: ObjectLocker ol (this_oop, THREAD); 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); duke@435: return (klassOop)entry; duke@435: } duke@435: duke@435: if (!PENDING_EXCEPTION-> duke@435: 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. duke@435: error = symbolHandle(SystemDictionary::find_resolution_error(this_oop, which)); duke@435: assert(!error.is_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: duke@435: if (TraceClassResolution && !k()->klass_part()->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()); duke@435: symbolOop s = instanceKlass::cast(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", duke@435: instanceKlass::cast(this_oop->pool_holder())->external_name(), duke@435: instanceKlass::cast(k())->external_name(), source_file, line_number); duke@435: } else { duke@435: tty->print("RESOLVE %s %s\n", duke@435: instanceKlass::cast(this_oop->pool_holder())->external_name(), duke@435: instanceKlass::cast(k())->external_name()); duke@435: } duke@435: } duke@435: return k(); duke@435: } else { duke@435: ObjectLocker ol (this_oop, THREAD); 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) { 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); duke@435: assert(entry->is_klass(), "must be resolved at this point"); duke@435: return (klassOop)entry; duke@435: } duke@435: duke@435: duke@435: // Does not update constantPoolOop - 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 duke@435: klassOop constantPoolOopDesc::klass_at_if_loaded(constantPoolHandle this_oop, int which) { duke@435: oop entry = *this_oop->obj_at_addr(which); duke@435: if (entry->is_klass()) { duke@435: return (klassOop)entry; duke@435: } else { duke@435: assert(entry->is_symbol(), "must be either symbol or klass"); duke@435: Thread *thread = Thread::current(); duke@435: symbolHandle name (thread, (symbolOop)entry); duke@435: oop loader = instanceKlass::cast(this_oop->pool_holder())->class_loader(); duke@435: oop protection_domain = Klass::cast(this_oop->pool_holder())->protection_domain(); duke@435: Handle h_prot (thread, protection_domain); duke@435: Handle h_loader (thread, loader); duke@435: klassOop 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: duke@435: klassOop constantPoolOopDesc::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: duke@435: // This is an interface for the compiler that allows accessing non-resolved entries duke@435: // in the constant pool - but still performs the validations tests. Must be used duke@435: // in a pre-parse of the compiler - to determine what it can do and not do. duke@435: // Note: We cannot update the ConstantPool from the vm_thread. duke@435: klassOop constantPoolOopDesc::klass_ref_at_if_loaded_check(constantPoolHandle this_oop, int index, TRAPS) { duke@435: int which = this_oop->klass_ref_index_at(index); duke@435: oop entry = *this_oop->obj_at_addr(which); duke@435: if (entry->is_klass()) { duke@435: return (klassOop)entry; duke@435: } else { duke@435: assert(entry->is_symbol(), "must be either symbol or klass"); duke@435: symbolHandle name (THREAD, (symbolOop)entry); duke@435: oop loader = instanceKlass::cast(this_oop->pool_holder())->class_loader(); duke@435: oop protection_domain = Klass::cast(this_oop->pool_holder())->protection_domain(); duke@435: Handle h_loader(THREAD, loader); duke@435: Handle h_prot (THREAD, protection_domain); duke@435: KlassHandle k(THREAD, SystemDictionary::find(name, h_loader, h_prot, THREAD)); duke@435: duke@435: // Do access check for klasses duke@435: if( k.not_null() ) verify_constant_pool_resolve(this_oop, k, CHECK_NULL); duke@435: return k(); duke@435: } duke@435: } duke@435: duke@435: jrose@1161: symbolOop constantPoolOopDesc::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: jrose@1161: symbolOop constantPoolOopDesc::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: jrose@1161: int constantPoolOopDesc::impl_name_and_type_ref_index_at(int which, bool uncached) { jrose@1494: int i = which; jrose@1494: if (!uncached && cache() != NULL) { jrose@1494: if (constantPoolCacheOopDesc::is_secondary_index(which)) jrose@1494: // Invokedynamic indexes are always processed in native order jrose@1494: // so there is no question of reading a native u2 in Java order here. jrose@1494: return cache()->main_entry_at(which)->constant_pool_index(); jrose@1494: // change byte-ordering and go via cache jrose@1494: i = remap_instruction_operand_from_cache(which); jrose@1494: } else { jrose@1494: if (tag_at(which).is_name_and_type()) jrose@1494: // invokedynamic index is a simple name-and-type jrose@1494: return 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_high_short_from_int(ref_index); duke@435: } duke@435: duke@435: jrose@1161: int constantPoolOopDesc::impl_klass_ref_index_at(int which, bool uncached) { jrose@1494: guarantee(!constantPoolCacheOopDesc::is_secondary_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: jrose@1494: int constantPoolOopDesc::remap_instruction_operand_from_cache(int operand) { jrose@1494: // Operand was fetched by a stream using get_Java_u2, yet was stored jrose@1494: // by Rewriter::rewrite_member_reference in native order. jrose@1494: // So now we have to fix the damage by swapping back to native order. jrose@1161: assert((int)(u2)operand == operand, "clean u2"); jrose@1494: int cpc_index = Bytes::swap_u2(operand); jrose@1494: int member_index = cache()->entry_at(cpc_index)->constant_pool_index(); jrose@1494: return member_index; jrose@1161: } jrose@1161: jrose@1161: duke@435: void constantPoolOopDesc::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()); duke@435: klassOop 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: jrose@1161: int constantPoolOopDesc::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: jrose@1161: int constantPoolOopDesc::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: duke@435: klassOop constantPoolOopDesc::klass_ref_at(int which, TRAPS) { duke@435: return klass_at(klass_ref_index_at(which), CHECK_NULL); duke@435: } duke@435: duke@435: duke@435: symbolOop constantPoolOopDesc::klass_name_at(int which) { duke@435: assert(tag_at(which).is_unresolved_klass() || tag_at(which).is_klass(), duke@435: "Corrupted constant pool"); duke@435: // A resolved constantPool entry will contain a klassOop, otherwise a symbolOop. 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. duke@435: oop entry = *(obj_at_addr(which)); duke@435: if (entry->is_klass()) { duke@435: // Already resolved - return entry's name. duke@435: return klassOop(entry)->klass_part()->name(); duke@435: } else { duke@435: assert(entry->is_symbol(), "must be either symbol or klass"); duke@435: return (symbolOop)entry; duke@435: } duke@435: } duke@435: duke@435: symbolOop constantPoolOopDesc::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: duke@435: char* constantPoolOopDesc::string_at_noresolve(int which) { duke@435: // Test entry type in case string is resolved while in here. duke@435: oop entry = *(obj_at_addr(which)); duke@435: if (entry->is_symbol()) { duke@435: return ((symbolOop)entry)->as_C_string(); jrose@866: } else if (java_lang_String::is_instance(entry)) { jrose@866: return java_lang_String::as_utf8_string(entry); duke@435: } else { jrose@866: return (char*)""; duke@435: } duke@435: } duke@435: duke@435: duke@435: BasicType constantPoolOopDesc::basic_type_for_signature_at(int which) { duke@435: return FieldType::basic_type(symbol_at(which)); duke@435: } duke@435: duke@435: duke@435: void constantPoolOopDesc::resolve_string_constants_impl(constantPoolHandle this_oop, TRAPS) { duke@435: for (int index = 1; index < this_oop->length(); index++) { // Index 0 is unused duke@435: if (this_oop->tag_at(index).is_unresolved_string()) { duke@435: this_oop->string_at(index, CHECK); duke@435: } duke@435: } duke@435: } duke@435: duke@435: oop constantPoolOopDesc::string_at_impl(constantPoolHandle this_oop, int which, TRAPS) { duke@435: oop entry = *(this_oop->obj_at_addr(which)); duke@435: if (entry->is_symbol()) { duke@435: ObjectLocker ol(this_oop, THREAD); duke@435: if (this_oop->tag_at(which).is_unresolved_string()) { duke@435: // Intern string duke@435: symbolOop sym = this_oop->unresolved_string_at(which); duke@435: entry = StringTable::intern(sym, CHECK_(constantPoolOop(NULL))); duke@435: this_oop->string_at_put(which, entry); duke@435: } else { duke@435: // Another thread beat us and interned string, read string from constant pool duke@435: entry = this_oop->resolved_string_at(which); duke@435: } duke@435: } duke@435: assert(java_lang_String::is_instance(entry), "must be string"); duke@435: return entry; duke@435: } duke@435: duke@435: jrose@866: bool constantPoolOopDesc::is_pseudo_string_at(int which) { jrose@866: oop entry = *(obj_at_addr(which)); jrose@866: if (entry->is_symbol()) jrose@866: // Not yet resolved, but it will resolve to a string. jrose@866: return false; jrose@866: else if (java_lang_String::is_instance(entry)) jrose@866: return false; // actually, it might be a non-interned or non-perm string jrose@866: else jrose@866: // truly pseudo jrose@866: return true; jrose@866: } jrose@866: jrose@866: duke@435: bool constantPoolOopDesc::klass_name_at_matches(instanceKlassHandle k, duke@435: int which) { duke@435: // Names are interned, so we can compare symbolOops directly duke@435: symbolOop cp_name = klass_name_at(which); duke@435: return (cp_name == k->name()); duke@435: } duke@435: duke@435: duke@435: int constantPoolOopDesc::pre_resolve_shared_klasses(TRAPS) { duke@435: ResourceMark rm; duke@435: int count = 0; duke@435: for (int index = 1; index < tags()->length(); index++) { // Index 0 is unused duke@435: if (tag_at(index).is_unresolved_string()) { duke@435: // Intern string duke@435: symbolOop sym = unresolved_string_at(index); duke@435: oop entry = StringTable::intern(sym, CHECK_(-1)); duke@435: string_at_put(index, entry); duke@435: } duke@435: } duke@435: return count; duke@435: } duke@435: duke@435: duke@435: // Iterate over symbols which are used as class, field, method names and duke@435: // signatures (in preparation for writing to the shared archive). duke@435: duke@435: void constantPoolOopDesc::shared_symbols_iterate(OopClosure* closure) { duke@435: for (int index = 1; index < length(); index++) { // Index 0 is unused duke@435: switch (tag_at(index).value()) { duke@435: duke@435: case JVM_CONSTANT_UnresolvedClass: duke@435: closure->do_oop(obj_at_addr(index)); duke@435: break; duke@435: duke@435: case JVM_CONSTANT_NameAndType: duke@435: { duke@435: int i = *int_at_addr(index); duke@435: closure->do_oop(obj_at_addr((unsigned)i >> 16)); duke@435: closure->do_oop(obj_at_addr((unsigned)i & 0xffff)); duke@435: } duke@435: break; duke@435: duke@435: case JVM_CONSTANT_Class: duke@435: case JVM_CONSTANT_InterfaceMethodref: duke@435: case JVM_CONSTANT_Fieldref: duke@435: case JVM_CONSTANT_Methodref: duke@435: case JVM_CONSTANT_Integer: duke@435: case JVM_CONSTANT_Float: duke@435: // Do nothing! Not an oop. duke@435: // These constant types do not reference symbols at this point. duke@435: break; duke@435: duke@435: case JVM_CONSTANT_String: duke@435: // Do nothing! Not a symbol. duke@435: break; duke@435: duke@435: case JVM_CONSTANT_UnresolvedString: duke@435: case JVM_CONSTANT_Utf8: duke@435: // These constants are symbols, but unless these symbols are duke@435: // actually to be used for something, we don't want to mark them. duke@435: break; duke@435: duke@435: case JVM_CONSTANT_Long: duke@435: case JVM_CONSTANT_Double: duke@435: // Do nothing! Not an oop. (But takes two pool entries.) duke@435: ++index; duke@435: break; duke@435: duke@435: default: duke@435: ShouldNotReachHere(); duke@435: break; duke@435: } duke@435: } duke@435: } duke@435: duke@435: duke@435: // Iterate over the [one] tags array (in preparation for writing to the duke@435: // shared archive). duke@435: duke@435: void constantPoolOopDesc::shared_tags_iterate(OopClosure* closure) { duke@435: closure->do_oop(tags_addr()); duke@435: } duke@435: duke@435: duke@435: // Iterate over String objects (in preparation for writing to the shared duke@435: // archive). duke@435: duke@435: void constantPoolOopDesc::shared_strings_iterate(OopClosure* closure) { duke@435: for (int index = 1; index < length(); index++) { // Index 0 is unused duke@435: switch (tag_at(index).value()) { duke@435: duke@435: case JVM_CONSTANT_UnresolvedClass: duke@435: case JVM_CONSTANT_NameAndType: duke@435: // Do nothing! Not a String. duke@435: break; duke@435: duke@435: case JVM_CONSTANT_Class: duke@435: case JVM_CONSTANT_InterfaceMethodref: duke@435: case JVM_CONSTANT_Fieldref: duke@435: case JVM_CONSTANT_Methodref: duke@435: case JVM_CONSTANT_Integer: duke@435: case JVM_CONSTANT_Float: duke@435: // Do nothing! Not an oop. duke@435: // These constant types do not reference symbols at this point. duke@435: break; duke@435: duke@435: case JVM_CONSTANT_String: duke@435: closure->do_oop(obj_at_addr(index)); duke@435: break; duke@435: duke@435: case JVM_CONSTANT_UnresolvedString: duke@435: case JVM_CONSTANT_Utf8: duke@435: // These constants are symbols, but unless these symbols are duke@435: // actually to be used for something, we don't want to mark them. duke@435: break; duke@435: duke@435: case JVM_CONSTANT_Long: duke@435: case JVM_CONSTANT_Double: duke@435: // Do nothing! Not an oop. (But takes two pool entries.) duke@435: ++index; duke@435: break; duke@435: duke@435: default: duke@435: ShouldNotReachHere(); duke@435: break; duke@435: } duke@435: } duke@435: } 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. duke@435: bool constantPoolOopDesc::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 duke@435: // class pairs and resolved/unresolved string pairs as different. duke@435: // From the constantPoolOop API point of view, this is correct duke@435: // behavior. See constantPoolKlass::merge() to see how this plays duke@435: // out in the context of constantPoolOop merging. duke@435: return false; duke@435: } duke@435: duke@435: switch (t1) { duke@435: case JVM_CONSTANT_Class: duke@435: { duke@435: klassOop k1 = klass_at(index1, CHECK_false); duke@435: klassOop 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_String: duke@435: { duke@435: oop s1 = string_at(index1, CHECK_false); duke@435: oop s2 = cp2->string_at(index2, CHECK_false); duke@435: if (s1 == s2) { duke@435: return true; 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: { duke@435: symbolOop k1 = unresolved_klass_at(index1); duke@435: symbolOop k2 = cp2->unresolved_klass_at(index2); duke@435: if (k1 == k2) { duke@435: return true; duke@435: } duke@435: } break; duke@435: duke@435: case JVM_CONSTANT_UnresolvedString: duke@435: { duke@435: symbolOop s1 = unresolved_string_at(index1); duke@435: symbolOop 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: { duke@435: symbolOop s1 = symbol_at(index1); duke@435: symbolOop 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: duke@435: // Copy this constant pool's entries at start_i to end_i (inclusive) duke@435: // to the constant pool to_cp's entries starting at to_i. A total of duke@435: // (end_i - start_i) + 1 entries are copied. duke@435: void constantPoolOopDesc::copy_cp_to(int start_i, int end_i, duke@435: constantPoolHandle to_cp, int to_i, TRAPS) { duke@435: duke@435: int dest_i = to_i; // leave original alone for debug purposes duke@435: duke@435: for (int src_i = start_i; src_i <= end_i; /* see loop bottom */ ) { duke@435: copy_entry_to(src_i, to_cp, dest_i, CHECK); duke@435: duke@435: switch (tag_at(src_i).value()) { duke@435: case JVM_CONSTANT_Double: duke@435: case JVM_CONSTANT_Long: duke@435: // double and long take two constant pool entries duke@435: src_i += 2; duke@435: dest_i += 2; duke@435: break; duke@435: duke@435: default: duke@435: // all others take one constant pool entry duke@435: src_i++; duke@435: dest_i++; duke@435: break; duke@435: } duke@435: } duke@435: } // end copy_cp_to() 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. duke@435: void constantPoolOopDesc::copy_entry_to(int from_i, constantPoolHandle to_cp, duke@435: int to_i, TRAPS) { duke@435: duke@435: switch (tag_at(from_i).value()) { duke@435: case JVM_CONSTANT_Class: duke@435: { duke@435: klassOop k = 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: { duke@435: jint ki = 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: { duke@435: jdouble d = 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: { duke@435: int class_index = uncached_klass_ref_index_at(from_i); duke@435: int name_and_type_index = 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: { duke@435: jfloat f = 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: { duke@435: jint i = 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: { duke@435: int class_index = uncached_klass_ref_index_at(from_i); duke@435: int name_and_type_index = 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: { duke@435: jlong l = 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: { duke@435: int class_index = uncached_klass_ref_index_at(from_i); duke@435: int name_and_type_index = 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: { duke@435: int name_ref_index = name_ref_index_at(from_i); duke@435: int signature_ref_index = 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_String: duke@435: { duke@435: oop s = string_at(from_i, CHECK); duke@435: to_cp->string_at_put(to_i, s); duke@435: } break; duke@435: duke@435: case JVM_CONSTANT_StringIndex: duke@435: { duke@435: jint si = 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: { duke@435: symbolOop k = unresolved_klass_at(from_i); duke@435: to_cp->unresolved_klass_at_put(to_i, k); duke@435: } break; duke@435: duke@435: case JVM_CONSTANT_UnresolvedClassInError: duke@435: { duke@435: symbolOop k = 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: duke@435: case JVM_CONSTANT_UnresolvedString: duke@435: { duke@435: symbolOop s = unresolved_string_at(from_i); duke@435: to_cp->unresolved_string_at_put(to_i, s); duke@435: } break; duke@435: duke@435: case JVM_CONSTANT_Utf8: duke@435: { duke@435: symbolOop s = symbol_at(from_i); duke@435: to_cp->symbol_at_put(to_i, s); 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: { duke@435: jbyte bad_value = tag_at(from_i).value(); // leave a breadcrumb 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. duke@435: int constantPoolOopDesc::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: duke@435: #ifndef PRODUCT duke@435: duke@435: const char* constantPoolOopDesc::printable_name_at(int which) { duke@435: duke@435: constantTag tag = tag_at(which); duke@435: duke@435: if (tag.is_unresolved_string() || 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: duke@435: // For temporary use until code is stable. duke@435: #define DBG(code) duke@435: duke@435: static const char* WARN_MSG = "Must not be such entry!"; duke@435: duke@435: static void print_cpool_bytes(jint cnt, u1 *bytes) { 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); xlu@948: printf("long "INT64_FORMAT, *(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: case JVM_CONSTANT_UnresolvedString: { duke@435: printf("UnresolvedString: %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. duke@435: jint constantPoolOopDesc::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: duke@435: case JVM_CONSTANT_UnresolvedString: duke@435: return 3; duke@435: 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: duke@435: 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. duke@435: jint constantPoolOopDesc::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: { duke@435: symbolOop 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: { duke@435: symbolOop 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 duke@435: int constantPoolOopDesc::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: { duke@435: symbolOop 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; duke@435: symbolOop 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: unsigned int hash; duke@435: char *str = string_at_noresolve(idx); duke@435: symbolOop sym = SymbolTable::lookup_only(str, (int) strlen(str), hash); thurka@1208: if (sym == NULL) { thurka@1208: // sym can be NULL if string refers to incorrectly encoded JVM_CONSTANT_Utf8 thurka@1208: // this can happen with JVM TI; see CR 6839599 for more details thurka@1208: oop string = *(obj_at_addr(idx)); thurka@1208: assert(java_lang_String::is_instance(string),"Not a String"); thurka@1208: DBG(printf("Error #%03hd tag=%03hd\n", idx, tag)); thurka@1208: idx1 = 0; thurka@1208: for (int j = 0; j < tbl->table_size() && idx1 == 0; j++) { thurka@1208: for (SymbolHashMapEntry* cur = tbl->bucket(j); cur != NULL; cur = cur->next()) { thurka@1208: int length; thurka@1208: sym = cur->symbol(); thurka@1208: jchar* chars = sym->as_unicode(length); thurka@1208: if (java_lang_String::equals(string, chars, length)) { thurka@1208: idx1 = cur->value(); thurka@1208: DBG(printf("Index found: %d\n",idx1)); thurka@1208: break; thurka@1208: } thurka@1208: } thurka@1208: } thurka@1208: } else { thurka@1208: idx1 = tbl->symbol_to_value(sym); thurka@1208: } 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_String: idx=#%03hd, %s", idx1, str)); duke@435: break; duke@435: } duke@435: case JVM_CONSTANT_UnresolvedString: { duke@435: *bytes = JVM_CONSTANT_String; duke@435: symbolOop 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); duke@435: DBG(char *str = sym->as_utf8()); duke@435: DBG(printf("JVM_CONSTANT_UnresolvedString: idx=#%03hd, %s", idx1, str)); 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: } 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: duke@435: duke@435: void SymbolHashMap::add_entry(symbolOop 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: duke@435: SymbolHashMapEntry* SymbolHashMap::find_entry(symbolOop 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: }