duke@435: /* coleenp@3682: * Copyright (c) 1997, 2012, 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" stefank@2314: #include "classfile/vmSymbols.hpp" stefank@2314: #include "memory/oopFactory.hpp" stefank@2314: #include "oops/oop.inline.hpp" stefank@2314: #include "runtime/handles.inline.hpp" stefank@2314: #include "utilities/xmlstream.hpp" duke@435: duke@435: coleenp@2497: Symbol* vmSymbols::_symbols[vmSymbols::SID_LIMIT]; duke@435: coleenp@2497: Symbol* vmSymbols::_type_signatures[T_VOID+1] = { NULL /*, NULL...*/ }; duke@435: coleenp@2497: inline int compare_symbol(Symbol* a, Symbol* b) { duke@435: if (a == b) return 0; duke@435: // follow the natural address order: duke@435: return (address)a > (address)b ? +1 : -1; duke@435: } duke@435: duke@435: static vmSymbols::SID vm_symbol_index[vmSymbols::SID_LIMIT]; duke@435: extern "C" { duke@435: static int compare_vmsymbol_sid(const void* void_a, const void* void_b) { coleenp@2497: Symbol* a = vmSymbols::symbol_at(*((vmSymbols::SID*) void_a)); coleenp@2497: Symbol* b = vmSymbols::symbol_at(*((vmSymbols::SID*) void_b)); duke@435: return compare_symbol(a, b); duke@435: } duke@435: } duke@435: duke@435: #ifndef PRODUCT duke@435: #define VM_SYMBOL_ENUM_NAME_BODY(name, string) #name "\0" duke@435: static const char* vm_symbol_enum_names = duke@435: VM_SYMBOLS_DO(VM_SYMBOL_ENUM_NAME_BODY, VM_ALIAS_IGNORE) duke@435: "\0"; duke@435: static const char* vm_symbol_enum_name(vmSymbols::SID sid) { duke@435: const char* string = &vm_symbol_enum_names[0]; duke@435: int skip = (int)sid - (int)vmSymbols::FIRST_SID; duke@435: for (; skip != 0; skip--) { duke@435: size_t skiplen = strlen(string); duke@435: if (skiplen == 0) return ""; // overflow duke@435: string += skiplen+1; duke@435: } duke@435: return string; duke@435: } duke@435: #endif //PRODUCT duke@435: duke@435: // Put all the VM symbol strings in one place. duke@435: // Makes for a more compact libjvm. duke@435: #define VM_SYMBOL_BODY(name, string) string "\0" duke@435: static const char* vm_symbol_bodies = VM_SYMBOLS_DO(VM_SYMBOL_BODY, VM_ALIAS_IGNORE); duke@435: duke@435: void vmSymbols::initialize(TRAPS) { duke@435: assert((int)SID_LIMIT <= (1< (1<print("*** Duplicate VM symbol SIDs %s(%d) and %s(%d): \"", duke@435: vm_symbol_enum_name((SID)i2), i2, duke@435: vm_symbol_enum_name((SID)i1), i1); duke@435: sym->print_symbol_on(tty); duke@435: tty->print_cr("\""); duke@435: } duke@435: } duke@435: } duke@435: #endif //ASSERT duke@435: duke@435: // Create an index for find_id: duke@435: { duke@435: for (int index = (int)FIRST_SID; index < (int)SID_LIMIT; index++) { duke@435: vm_symbol_index[index] = (SID)index; duke@435: } duke@435: int num_sids = SID_LIMIT-FIRST_SID; duke@435: qsort(&vm_symbol_index[FIRST_SID], num_sids, sizeof(vm_symbol_index[0]), duke@435: compare_vmsymbol_sid); duke@435: } duke@435: duke@435: #ifdef ASSERT duke@435: { duke@435: // Spot-check correspondence between strings, symbols, and enums: duke@435: assert(_symbols[NO_SID] == NULL, "must be"); duke@435: const char* str = "java/lang/Object"; coleenp@3682: TempNewSymbol jlo = SymbolTable::new_permanent_symbol(str, CHECK); coleenp@2497: assert(strncmp(str, (char*)jlo->base(), jlo->utf8_length()) == 0, ""); coleenp@2497: assert(jlo == java_lang_Object(), ""); duke@435: SID sid = VM_SYMBOL_ENUM_NAME(java_lang_Object); coleenp@2497: assert(find_sid(jlo) == sid, ""); coleenp@2497: assert(symbol_at(sid) == jlo, ""); duke@435: duke@435: // Make sure find_sid produces the right answer in each case. duke@435: for (int index = (int)FIRST_SID; index < (int)SID_LIMIT; index++) { coleenp@2497: Symbol* sym = symbol_at((SID)index); duke@435: sid = find_sid(sym); duke@435: assert(sid == (SID)index, "symbol index works"); duke@435: // Note: If there are duplicates, this assert will fail. duke@435: // A "Duplicate VM symbol" message will have already been printed. duke@435: } duke@435: duke@435: // The string "format" happens (at the moment) not to be a vmSymbol, duke@435: // though it is a method name in java.lang.String. duke@435: str = "format"; coleenp@3682: TempNewSymbol fmt = SymbolTable::new_permanent_symbol(str, CHECK); coleenp@2497: sid = find_sid(fmt); duke@435: assert(sid == NO_SID, "symbol index works (negative test)"); duke@435: } duke@435: #endif duke@435: } duke@435: duke@435: duke@435: #ifndef PRODUCT duke@435: const char* vmSymbols::name_for(vmSymbols::SID sid) { duke@435: if (sid == NO_SID) duke@435: return "NO_SID"; duke@435: const char* string = &vm_symbol_bodies[0]; duke@435: for (int index = (int)FIRST_SID; index < (int)SID_LIMIT; index++) { duke@435: if (index == (int)sid) duke@435: return string; duke@435: string += strlen(string); // skip string body duke@435: string += 1; // skip trailing null duke@435: } duke@435: return "BAD_SID"; duke@435: } duke@435: #endif duke@435: duke@435: duke@435: coleenp@2497: void vmSymbols::symbols_do(SymbolClosure* f) { duke@435: for (int index = (int)FIRST_SID; index < (int)SID_LIMIT; index++) { coleenp@2497: f->do_symbol(&_symbols[index]); duke@435: } duke@435: for (int i = 0; i < T_VOID+1; i++) { coleenp@2497: f->do_symbol(&_type_signatures[i]); duke@435: } duke@435: } duke@435: coleenp@2497: void vmSymbols::serialize(SerializeOopClosure* soc) { coleenp@2497: soc->do_region((u_char*)&_symbols[FIRST_SID], coleenp@2497: (SID_LIMIT - FIRST_SID) * sizeof(_symbols[0])); coleenp@2497: soc->do_region((u_char*)_type_signatures, sizeof(_type_signatures)); coleenp@2497: } duke@435: coleenp@2497: coleenp@2497: BasicType vmSymbols::signature_type(Symbol* s) { duke@435: assert(s != NULL, "checking"); duke@435: for (int i = T_BOOLEAN; i < T_VOID+1; i++) { duke@435: if (s == _type_signatures[i]) { duke@435: return (BasicType)i; duke@435: } duke@435: } duke@435: return T_OBJECT; duke@435: } duke@435: duke@435: duke@435: static int mid_hint = (int)vmSymbols::FIRST_SID+1; duke@435: duke@435: #ifndef PRODUCT duke@435: static int find_sid_calls, find_sid_probes; duke@435: // (Typical counts are calls=7000 and probes=17000.) duke@435: #endif duke@435: coleenp@2497: vmSymbols::SID vmSymbols::find_sid(Symbol* symbol) { duke@435: // Handle the majority of misses by a bounds check. duke@435: // Then, use a binary search over the index. duke@435: // Expected trip count is less than log2_SID_LIMIT, about eight. duke@435: // This is slow but acceptable, given that calls are not duke@435: // dynamically common. (methodOop::intrinsic_id has a cache.) duke@435: NOT_PRODUCT(find_sid_calls++); duke@435: int min = (int)FIRST_SID, max = (int)SID_LIMIT - 1; duke@435: SID sid = NO_SID, sid1; duke@435: int cmp1; duke@435: sid1 = vm_symbol_index[min]; duke@435: cmp1 = compare_symbol(symbol, symbol_at(sid1)); duke@435: if (cmp1 <= 0) { // before the first duke@435: if (cmp1 == 0) sid = sid1; duke@435: } else { duke@435: sid1 = vm_symbol_index[max]; duke@435: cmp1 = compare_symbol(symbol, symbol_at(sid1)); duke@435: if (cmp1 >= 0) { // after the last duke@435: if (cmp1 == 0) sid = sid1; duke@435: } else { duke@435: // After checking the extremes, do a binary search. duke@435: ++min; --max; // endpoints are done duke@435: int mid = mid_hint; // start at previous success duke@435: while (max >= min) { duke@435: assert(mid >= min && mid <= max, ""); duke@435: NOT_PRODUCT(find_sid_probes++); duke@435: sid1 = vm_symbol_index[mid]; duke@435: cmp1 = compare_symbol(symbol, symbol_at(sid1)); duke@435: if (cmp1 == 0) { duke@435: mid_hint = mid; duke@435: sid = sid1; duke@435: break; duke@435: } duke@435: if (cmp1 < 0) duke@435: max = mid - 1; // symbol < symbol_at(sid) duke@435: else duke@435: min = mid + 1; duke@435: duke@435: // Pick a new probe point: duke@435: mid = (max + min) / 2; duke@435: } duke@435: } duke@435: } duke@435: duke@435: #ifdef ASSERT duke@435: // Perform the exhaustive self-check the first 1000 calls, duke@435: // and every 100 calls thereafter. duke@435: static int find_sid_check_count = -2000; duke@435: if ((uint)++find_sid_check_count > (uint)100) { duke@435: if (find_sid_check_count > 0) find_sid_check_count = 0; duke@435: duke@435: // Make sure this is the right answer, using linear search. duke@435: // (We have already proven that there are no duplicates in the list.) duke@435: SID sid2 = NO_SID; duke@435: for (int index = (int)FIRST_SID; index < (int)SID_LIMIT; index++) { coleenp@2497: Symbol* sym2 = symbol_at((SID)index); duke@435: if (sym2 == symbol) { duke@435: sid2 = (SID)index; duke@435: break; duke@435: } duke@435: } duke@435: // Unless it's a duplicate, assert that the sids are the same. duke@435: if (_symbols[sid] != _symbols[sid2]) { duke@435: assert(sid == sid2, "binary same as linear search"); duke@435: } duke@435: } duke@435: #endif //ASSERT duke@435: duke@435: return sid; duke@435: } duke@435: jrose@2638: vmSymbols::SID vmSymbols::find_sid(const char* symbol_name) { jrose@2638: Symbol* symbol = SymbolTable::probe(symbol_name, (int) strlen(symbol_name)); jrose@2638: if (symbol == NULL) return NO_SID; jrose@2638: return find_sid(symbol); jrose@2638: } jrose@2638: twisti@1568: static vmIntrinsics::ID wrapper_intrinsic(BasicType type, bool unboxing) { twisti@1568: #define TYPE2(type, unboxing) ((int)(type)*2 + ((unboxing) ? 1 : 0)) twisti@1568: switch (TYPE2(type, unboxing)) { twisti@1568: #define BASIC_TYPE_CASE(type, box, unbox) \ twisti@1568: case TYPE2(type, false): return vmIntrinsics::box; \ twisti@1568: case TYPE2(type, true): return vmIntrinsics::unbox twisti@1568: BASIC_TYPE_CASE(T_BOOLEAN, _Boolean_valueOf, _booleanValue); twisti@1568: BASIC_TYPE_CASE(T_BYTE, _Byte_valueOf, _byteValue); twisti@1568: BASIC_TYPE_CASE(T_CHAR, _Character_valueOf, _charValue); twisti@1568: BASIC_TYPE_CASE(T_SHORT, _Short_valueOf, _shortValue); twisti@1568: BASIC_TYPE_CASE(T_INT, _Integer_valueOf, _intValue); twisti@1568: BASIC_TYPE_CASE(T_LONG, _Long_valueOf, _longValue); twisti@1568: BASIC_TYPE_CASE(T_FLOAT, _Float_valueOf, _floatValue); twisti@1568: BASIC_TYPE_CASE(T_DOUBLE, _Double_valueOf, _doubleValue); twisti@1568: #undef BASIC_TYPE_CASE twisti@1568: } twisti@1568: #undef TYPE2 twisti@1568: return vmIntrinsics::_none; twisti@1568: } twisti@1568: twisti@1568: vmIntrinsics::ID vmIntrinsics::for_boxing(BasicType type) { twisti@1568: return wrapper_intrinsic(type, false); twisti@1568: } twisti@1568: vmIntrinsics::ID vmIntrinsics::for_unboxing(BasicType type) { twisti@1568: return wrapper_intrinsic(type, true); twisti@1568: } twisti@1568: twisti@1573: vmIntrinsics::ID vmIntrinsics::for_raw_conversion(BasicType src, BasicType dest) { twisti@1573: #define SRC_DEST(s,d) (((int)(s) << 4) + (int)(d)) twisti@1573: switch (SRC_DEST(src, dest)) { twisti@1573: case SRC_DEST(T_INT, T_FLOAT): return vmIntrinsics::_intBitsToFloat; twisti@1573: case SRC_DEST(T_FLOAT, T_INT): return vmIntrinsics::_floatToRawIntBits; twisti@1573: twisti@1573: case SRC_DEST(T_LONG, T_DOUBLE): return vmIntrinsics::_longBitsToDouble; twisti@1573: case SRC_DEST(T_DOUBLE, T_LONG): return vmIntrinsics::_doubleToRawLongBits; twisti@1573: } twisti@1573: #undef SRC_DEST twisti@1573: twisti@1573: return vmIntrinsics::_none; twisti@1573: } twisti@1573: twisti@1568: methodOop vmIntrinsics::method_for(vmIntrinsics::ID id) { twisti@1568: if (id == _none) return NULL; coleenp@2497: Symbol* cname = vmSymbols::symbol_at(class_for(id)); coleenp@2497: Symbol* mname = vmSymbols::symbol_at(name_for(id)); coleenp@2497: Symbol* msig = vmSymbols::symbol_at(signature_for(id)); twisti@1568: if (cname == NULL || mname == NULL || msig == NULL) return NULL; twisti@1568: klassOop k = SystemDictionary::find_well_known_klass(cname); twisti@1568: if (k == NULL) return NULL; twisti@1568: return instanceKlass::cast(k)->find_method(mname, msig); twisti@1568: } twisti@1568: duke@435: duke@435: #define VM_INTRINSIC_INITIALIZE(id, klass, name, sig, flags) #id "\0" duke@435: static const char* vm_intrinsic_name_bodies = duke@435: VM_INTRINSICS_DO(VM_INTRINSIC_INITIALIZE, duke@435: VM_SYMBOL_IGNORE, VM_SYMBOL_IGNORE, VM_SYMBOL_IGNORE, VM_ALIAS_IGNORE); duke@435: duke@435: static const char* vm_intrinsic_name_table[vmIntrinsics::ID_LIMIT]; duke@435: duke@435: const char* vmIntrinsics::name_at(vmIntrinsics::ID id) { duke@435: const char** nt = &vm_intrinsic_name_table[0]; duke@435: if (nt[_none] == NULL) { duke@435: char* string = (char*) &vm_intrinsic_name_bodies[0]; duke@435: for (int index = FIRST_ID; index < ID_LIMIT; index++) { duke@435: nt[index] = string; duke@435: string += strlen(string); // skip string body duke@435: string += 1; // skip trailing null duke@435: } duke@435: assert(!strcmp(nt[_hashCode], "_hashCode"), "lined up"); duke@435: nt[_none] = "_none"; duke@435: } duke@435: if ((uint)id < (uint)ID_LIMIT) duke@435: return vm_intrinsic_name_table[(uint)id]; duke@435: else duke@435: return "(unknown intrinsic)"; duke@435: } duke@435: duke@435: // These are flag-matching functions: duke@435: inline bool match_F_R(jshort flags) { duke@435: const int req = 0; duke@435: const int neg = JVM_ACC_STATIC | JVM_ACC_SYNCHRONIZED; duke@435: return (flags & (req | neg)) == req; duke@435: } never@1515: inline bool match_F_Y(jshort flags) { never@1515: const int req = JVM_ACC_SYNCHRONIZED; never@1515: const int neg = JVM_ACC_STATIC; never@1515: return (flags & (req | neg)) == req; never@1515: } duke@435: inline bool match_F_RN(jshort flags) { duke@435: const int req = JVM_ACC_NATIVE; duke@435: const int neg = JVM_ACC_STATIC | JVM_ACC_SYNCHRONIZED; duke@435: return (flags & (req | neg)) == req; duke@435: } duke@435: inline bool match_F_S(jshort flags) { duke@435: const int req = JVM_ACC_STATIC; duke@435: const int neg = JVM_ACC_SYNCHRONIZED; duke@435: return (flags & (req | neg)) == req; duke@435: } duke@435: inline bool match_F_SN(jshort flags) { duke@435: const int req = JVM_ACC_STATIC | JVM_ACC_NATIVE; duke@435: const int neg = JVM_ACC_SYNCHRONIZED; duke@435: return (flags & (req | neg)) == req; duke@435: } kvn@480: inline bool match_F_RNY(jshort flags) { kvn@480: const int req = JVM_ACC_NATIVE | JVM_ACC_SYNCHRONIZED; kvn@480: const int neg = JVM_ACC_STATIC; kvn@480: return (flags & (req | neg)) == req; kvn@480: } duke@435: duke@435: // These are for forming case labels: twisti@1568: #define ID3(x, y, z) (( jlong)(z) + \ twisti@1568: ((jlong)(y) << vmSymbols::log2_SID_LIMIT) + \ twisti@1568: ((jlong)(x) << (2*vmSymbols::log2_SID_LIMIT)) ) duke@435: #define SID_ENUM(n) vmSymbols::VM_SYMBOL_ENUM_NAME(n) duke@435: twisti@1568: vmIntrinsics::ID vmIntrinsics::find_id_impl(vmSymbols::SID holder, twisti@1568: vmSymbols::SID name, twisti@1568: vmSymbols::SID sig, twisti@1568: jshort flags) { duke@435: assert((int)vmSymbols::SID_LIMIT <= (1<> shift) & mask) == 1021, ""); twisti@1568: return vmSymbols::SID( (info >> shift) & mask ); duke@435: } duke@435: duke@435: vmSymbols::SID vmIntrinsics::name_for(vmIntrinsics::ID id) { twisti@1568: jlong info = intrinsic_info(id); twisti@1568: int shift = vmSymbols::log2_SID_LIMIT + log2_FLAG_LIMIT, mask = right_n_bits(vmSymbols::log2_SID_LIMIT); twisti@1568: assert(((ID4(1021,1022,1023,15) >> shift) & mask) == 1022, ""); twisti@1568: return vmSymbols::SID( (info >> shift) & mask ); duke@435: } duke@435: duke@435: vmSymbols::SID vmIntrinsics::signature_for(vmIntrinsics::ID id) { twisti@1568: jlong info = intrinsic_info(id); twisti@1568: int shift = log2_FLAG_LIMIT, mask = right_n_bits(vmSymbols::log2_SID_LIMIT); twisti@1568: assert(((ID4(1021,1022,1023,15) >> shift) & mask) == 1023, ""); twisti@1568: return vmSymbols::SID( (info >> shift) & mask ); duke@435: } duke@435: duke@435: vmIntrinsics::Flags vmIntrinsics::flags_for(vmIntrinsics::ID id) { twisti@1568: jlong info = intrinsic_info(id); twisti@1568: int shift = 0, mask = right_n_bits(log2_FLAG_LIMIT); twisti@1568: assert(((ID4(1021,1022,1023,15) >> shift) & mask) == 15, ""); twisti@1568: return Flags( (info >> shift) & mask ); duke@435: } duke@435: duke@435: duke@435: #ifndef PRODUCT duke@435: // verify_method performs an extra check on a matched intrinsic method duke@435: coleenp@2497: static bool match_method(methodOop m, Symbol* n, Symbol* s) { duke@435: return (m->name() == n && duke@435: m->signature() == s); duke@435: } duke@435: coleenp@2497: static vmIntrinsics::ID match_method_with_klass(methodOop m, Symbol* mk) { duke@435: #define VM_INTRINSIC_MATCH(id, klassname, namepart, sigpart, flags) \ coleenp@2497: { Symbol* k = vmSymbols::klassname(); \ duke@435: if (mk == k) { \ coleenp@2497: Symbol* n = vmSymbols::namepart(); \ coleenp@2497: Symbol* s = vmSymbols::sigpart(); \ duke@435: if (match_method(m, n, s)) \ duke@435: return vmIntrinsics::id; \ duke@435: } } duke@435: VM_INTRINSICS_DO(VM_INTRINSIC_MATCH, duke@435: VM_SYMBOL_IGNORE, VM_SYMBOL_IGNORE, VM_SYMBOL_IGNORE, VM_ALIAS_IGNORE); duke@435: return vmIntrinsics::_none; duke@435: #undef VM_INTRINSIC_MATCH duke@435: } duke@435: duke@435: void vmIntrinsics::verify_method(ID actual_id, methodOop m) { coleenp@2497: Symbol* mk = Klass::cast(m->method_holder())->name(); duke@435: ID declared_id = match_method_with_klass(m, mk); duke@435: duke@435: if (declared_id == actual_id) return; // success duke@435: duke@435: if (declared_id == _none && actual_id != _none && mk == vmSymbols::java_lang_StrictMath()) { duke@435: // Here are a few special cases in StrictMath not declared in vmSymbols.hpp. duke@435: switch (actual_id) { duke@435: case _min: duke@435: case _max: duke@435: case _dsqrt: duke@435: declared_id = match_method_with_klass(m, vmSymbols::java_lang_Math()); duke@435: if (declared_id == actual_id) return; // acceptable alias duke@435: break; duke@435: } duke@435: } duke@435: duke@435: const char* declared_name = name_at(declared_id); duke@435: const char* actual_name = name_at(actual_id); duke@435: methodHandle mh = m; duke@435: m = NULL; duke@435: ttyLocker ttyl; duke@435: if (xtty != NULL) { duke@435: xtty->begin_elem("intrinsic_misdeclared actual='%s' declared='%s'", duke@435: actual_name, declared_name); duke@435: xtty->method(mh); duke@435: xtty->end_elem(""); duke@435: } duke@435: if (PrintMiscellaneous && (WizardMode || Verbose)) { duke@435: tty->print_cr("*** misidentified method; %s(%d) should be %s(%d):", duke@435: declared_name, declared_id, actual_name, actual_id); kvn@480: mh()->print_short_name(tty); duke@435: tty->cr(); duke@435: } duke@435: } duke@435: #endif //PRODUCT