vmSymbols.cpp revision 0
0N/A/*
0N/A * Copyright 1997-2006 Sun Microsystems, Inc. All Rights Reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
0N/A * published by the Free Software Foundation.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
0N/A * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
0N/A * CA 95054 USA or visit www.sun.com if you need additional information or
0N/A * have any questions.
0N/A *
0N/A */
0N/A
0N/A# include "incls/_precompiled.incl"
0N/A# include "incls/_vmSymbols.cpp.incl"
0N/A
0N/A
0N/AsymbolOop vmSymbols::_symbols[vmSymbols::SID_LIMIT];
0N/A
0N/AsymbolOop vmSymbols::_type_signatures[T_VOID+1] = { NULL /*, NULL...*/ };
0N/A
0N/Ainline int compare_symbol(symbolOop a, symbolOop b) {
0N/A if (a == b) return 0;
0N/A // follow the natural address order:
0N/A return (address)a > (address)b ? +1 : -1;
0N/A}
0N/A
0N/Astatic vmSymbols::SID vm_symbol_index[vmSymbols::SID_LIMIT];
0N/Aextern "C" {
0N/A static int compare_vmsymbol_sid(const void* void_a, const void* void_b) {
0N/A symbolOop a = vmSymbols::symbol_at(*((vmSymbols::SID*) void_a));
0N/A symbolOop b = vmSymbols::symbol_at(*((vmSymbols::SID*) void_b));
0N/A return compare_symbol(a, b);
0N/A }
0N/A}
0N/A
0N/A#ifndef PRODUCT
0N/A#define VM_SYMBOL_ENUM_NAME_BODY(name, string) #name "\0"
0N/Astatic const char* vm_symbol_enum_names =
0N/A VM_SYMBOLS_DO(VM_SYMBOL_ENUM_NAME_BODY, VM_ALIAS_IGNORE)
0N/A "\0";
0N/Astatic const char* vm_symbol_enum_name(vmSymbols::SID sid) {
0N/A const char* string = &vm_symbol_enum_names[0];
0N/A int skip = (int)sid - (int)vmSymbols::FIRST_SID;
0N/A for (; skip != 0; skip--) {
0N/A size_t skiplen = strlen(string);
0N/A if (skiplen == 0) return "<unknown>"; // overflow
0N/A string += skiplen+1;
0N/A }
0N/A return string;
0N/A}
0N/A#endif //PRODUCT
0N/A
0N/A// Put all the VM symbol strings in one place.
0N/A// Makes for a more compact libjvm.
0N/A#define VM_SYMBOL_BODY(name, string) string "\0"
0N/Astatic const char* vm_symbol_bodies = VM_SYMBOLS_DO(VM_SYMBOL_BODY, VM_ALIAS_IGNORE);
0N/A
0N/Avoid vmSymbols::initialize(TRAPS) {
0N/A assert((int)SID_LIMIT <= (1<<log2_SID_LIMIT), "must fit in this bitfield");
0N/A assert((int)SID_LIMIT*5 > (1<<log2_SID_LIMIT), "make the bitfield smaller, please");
0N/A
0N/A if (!UseSharedSpaces) {
0N/A const char* string = &vm_symbol_bodies[0];
0N/A for (int index = (int)FIRST_SID; index < (int)SID_LIMIT; index++) {
0N/A symbolOop sym = oopFactory::new_symbol(string, CHECK);
0N/A _symbols[index] = sym;
0N/A string += strlen(string); // skip string body
0N/A string += 1; // skip trailing null
0N/A }
0N/A
0N/A _type_signatures[T_BYTE] = byte_signature();
0N/A _type_signatures[T_CHAR] = char_signature();
0N/A _type_signatures[T_DOUBLE] = double_signature();
0N/A _type_signatures[T_FLOAT] = float_signature();
0N/A _type_signatures[T_INT] = int_signature();
0N/A _type_signatures[T_LONG] = long_signature();
0N/A _type_signatures[T_SHORT] = short_signature();
0N/A _type_signatures[T_BOOLEAN] = bool_signature();
0N/A _type_signatures[T_VOID] = void_signature();
0N/A // no single signatures for T_OBJECT or T_ARRAY
0N/A }
0N/A
0N/A#ifdef ASSERT
0N/A // Check for duplicates:
0N/A for (int i1 = (int)FIRST_SID; i1 < (int)SID_LIMIT; i1++) {
0N/A symbolOop sym = symbol_at((SID)i1);
0N/A for (int i2 = (int)FIRST_SID; i2 < i1; i2++) {
0N/A if (symbol_at((SID)i2) == sym) {
0N/A tty->print("*** Duplicate VM symbol SIDs %s(%d) and %s(%d): \"",
0N/A vm_symbol_enum_name((SID)i2), i2,
0N/A vm_symbol_enum_name((SID)i1), i1);
0N/A sym->print_symbol_on(tty);
0N/A tty->print_cr("\"");
0N/A }
0N/A }
0N/A }
0N/A#endif //ASSERT
0N/A
0N/A // Create an index for find_id:
0N/A {
0N/A for (int index = (int)FIRST_SID; index < (int)SID_LIMIT; index++) {
0N/A vm_symbol_index[index] = (SID)index;
0N/A }
0N/A int num_sids = SID_LIMIT-FIRST_SID;
0N/A qsort(&vm_symbol_index[FIRST_SID], num_sids, sizeof(vm_symbol_index[0]),
0N/A compare_vmsymbol_sid);
0N/A }
0N/A
0N/A#ifdef ASSERT
0N/A {
0N/A // Spot-check correspondence between strings, symbols, and enums:
0N/A assert(_symbols[NO_SID] == NULL, "must be");
0N/A const char* str = "java/lang/Object";
0N/A symbolOop sym = oopFactory::new_symbol(str, CHECK);
0N/A assert(strcmp(str, (char*)sym->base()) == 0, "");
0N/A assert(sym == java_lang_Object(), "");
0N/A SID sid = VM_SYMBOL_ENUM_NAME(java_lang_Object);
0N/A assert(find_sid(sym) == sid, "");
0N/A assert(symbol_at(sid) == sym, "");
0N/A
0N/A // Make sure find_sid produces the right answer in each case.
0N/A for (int index = (int)FIRST_SID; index < (int)SID_LIMIT; index++) {
0N/A sym = symbol_at((SID)index);
0N/A sid = find_sid(sym);
0N/A assert(sid == (SID)index, "symbol index works");
0N/A // Note: If there are duplicates, this assert will fail.
0N/A // A "Duplicate VM symbol" message will have already been printed.
0N/A }
0N/A
0N/A // The string "format" happens (at the moment) not to be a vmSymbol,
0N/A // though it is a method name in java.lang.String.
0N/A str = "format";
0N/A sym = oopFactory::new_symbol(str, CHECK);
0N/A sid = find_sid(sym);
0N/A assert(sid == NO_SID, "symbol index works (negative test)");
0N/A }
0N/A#endif
0N/A}
0N/A
0N/A
0N/A#ifndef PRODUCT
0N/Aconst char* vmSymbols::name_for(vmSymbols::SID sid) {
0N/A if (sid == NO_SID)
0N/A return "NO_SID";
0N/A const char* string = &vm_symbol_bodies[0];
0N/A for (int index = (int)FIRST_SID; index < (int)SID_LIMIT; index++) {
0N/A if (index == (int)sid)
0N/A return string;
0N/A string += strlen(string); // skip string body
0N/A string += 1; // skip trailing null
0N/A }
0N/A return "BAD_SID";
0N/A}
0N/A#endif
0N/A
0N/A
0N/A
0N/Avoid vmSymbols::oops_do(OopClosure* f, bool do_all) {
0N/A for (int index = (int)FIRST_SID; index < (int)SID_LIMIT; index++) {
0N/A f->do_oop((oop*) &_symbols[index]);
0N/A }
0N/A for (int i = 0; i < T_VOID+1; i++) {
0N/A if (_type_signatures[i] != NULL) {
0N/A assert(i >= T_BOOLEAN, "checking");
0N/A f->do_oop((oop*)&_type_signatures[i]);
0N/A } else if (do_all) {
0N/A f->do_oop((oop*)&_type_signatures[i]);
0N/A }
0N/A }
0N/A}
0N/A
0N/A
0N/ABasicType vmSymbols::signature_type(symbolOop s) {
0N/A assert(s != NULL, "checking");
0N/A for (int i = T_BOOLEAN; i < T_VOID+1; i++) {
0N/A if (s == _type_signatures[i]) {
0N/A return (BasicType)i;
0N/A }
0N/A }
0N/A return T_OBJECT;
0N/A}
0N/A
0N/A
0N/Astatic int mid_hint = (int)vmSymbols::FIRST_SID+1;
0N/A
0N/A#ifndef PRODUCT
0N/Astatic int find_sid_calls, find_sid_probes;
0N/A// (Typical counts are calls=7000 and probes=17000.)
0N/A#endif
0N/A
0N/AvmSymbols::SID vmSymbols::find_sid(symbolOop symbol) {
0N/A // Handle the majority of misses by a bounds check.
0N/A // Then, use a binary search over the index.
0N/A // Expected trip count is less than log2_SID_LIMIT, about eight.
0N/A // This is slow but acceptable, given that calls are not
0N/A // dynamically common. (methodOop::intrinsic_id has a cache.)
0N/A NOT_PRODUCT(find_sid_calls++);
0N/A int min = (int)FIRST_SID, max = (int)SID_LIMIT - 1;
0N/A SID sid = NO_SID, sid1;
0N/A int cmp1;
0N/A sid1 = vm_symbol_index[min];
0N/A cmp1 = compare_symbol(symbol, symbol_at(sid1));
0N/A if (cmp1 <= 0) { // before the first
0N/A if (cmp1 == 0) sid = sid1;
0N/A } else {
0N/A sid1 = vm_symbol_index[max];
0N/A cmp1 = compare_symbol(symbol, symbol_at(sid1));
0N/A if (cmp1 >= 0) { // after the last
0N/A if (cmp1 == 0) sid = sid1;
0N/A } else {
0N/A // After checking the extremes, do a binary search.
0N/A ++min; --max; // endpoints are done
0N/A int mid = mid_hint; // start at previous success
0N/A while (max >= min) {
0N/A assert(mid >= min && mid <= max, "");
0N/A NOT_PRODUCT(find_sid_probes++);
0N/A sid1 = vm_symbol_index[mid];
0N/A cmp1 = compare_symbol(symbol, symbol_at(sid1));
0N/A if (cmp1 == 0) {
0N/A mid_hint = mid;
0N/A sid = sid1;
0N/A break;
0N/A }
0N/A if (cmp1 < 0)
0N/A max = mid - 1; // symbol < symbol_at(sid)
0N/A else
0N/A min = mid + 1;
0N/A
0N/A // Pick a new probe point:
0N/A mid = (max + min) / 2;
0N/A }
0N/A }
0N/A }
0N/A
0N/A#ifdef ASSERT
0N/A // Perform the exhaustive self-check the first 1000 calls,
0N/A // and every 100 calls thereafter.
0N/A static int find_sid_check_count = -2000;
0N/A if ((uint)++find_sid_check_count > (uint)100) {
0N/A if (find_sid_check_count > 0) find_sid_check_count = 0;
0N/A
0N/A // Make sure this is the right answer, using linear search.
0N/A // (We have already proven that there are no duplicates in the list.)
0N/A SID sid2 = NO_SID;
0N/A for (int index = (int)FIRST_SID; index < (int)SID_LIMIT; index++) {
0N/A symbolOop sym2 = symbol_at((SID)index);
0N/A if (sym2 == symbol) {
0N/A sid2 = (SID)index;
0N/A break;
0N/A }
0N/A }
0N/A // Unless it's a duplicate, assert that the sids are the same.
0N/A if (_symbols[sid] != _symbols[sid2]) {
0N/A assert(sid == sid2, "binary same as linear search");
0N/A }
0N/A }
0N/A#endif //ASSERT
0N/A
0N/A return sid;
0N/A}
0N/A
0N/A
0N/A#define VM_INTRINSIC_INITIALIZE(id, klass, name, sig, flags) #id "\0"
0N/Astatic const char* vm_intrinsic_name_bodies =
0N/A VM_INTRINSICS_DO(VM_INTRINSIC_INITIALIZE,
0N/A VM_SYMBOL_IGNORE, VM_SYMBOL_IGNORE, VM_SYMBOL_IGNORE, VM_ALIAS_IGNORE);
0N/A
0N/Astatic const char* vm_intrinsic_name_table[vmIntrinsics::ID_LIMIT];
0N/A
0N/Aconst char* vmIntrinsics::name_at(vmIntrinsics::ID id) {
0N/A const char** nt = &vm_intrinsic_name_table[0];
0N/A if (nt[_none] == NULL) {
0N/A char* string = (char*) &vm_intrinsic_name_bodies[0];
0N/A for (int index = FIRST_ID; index < ID_LIMIT; index++) {
0N/A nt[index] = string;
0N/A string += strlen(string); // skip string body
0N/A string += 1; // skip trailing null
0N/A }
0N/A assert(!strcmp(nt[_hashCode], "_hashCode"), "lined up");
0N/A nt[_none] = "_none";
0N/A }
0N/A if ((uint)id < (uint)ID_LIMIT)
0N/A return vm_intrinsic_name_table[(uint)id];
0N/A else
0N/A return "(unknown intrinsic)";
0N/A}
0N/A
0N/A// These are flag-matching functions:
0N/Ainline bool match_F_R(jshort flags) {
0N/A const int req = 0;
0N/A const int neg = JVM_ACC_STATIC | JVM_ACC_SYNCHRONIZED;
0N/A return (flags & (req | neg)) == req;
0N/A}
0N/Ainline bool match_F_RN(jshort flags) {
0N/A const int req = JVM_ACC_NATIVE;
0N/A const int neg = JVM_ACC_STATIC | JVM_ACC_SYNCHRONIZED;
0N/A return (flags & (req | neg)) == req;
0N/A}
0N/Ainline bool match_F_S(jshort flags) {
0N/A const int req = JVM_ACC_STATIC;
0N/A const int neg = JVM_ACC_SYNCHRONIZED;
0N/A return (flags & (req | neg)) == req;
0N/A}
0N/Ainline bool match_F_SN(jshort flags) {
0N/A const int req = JVM_ACC_STATIC | JVM_ACC_NATIVE;
0N/A const int neg = JVM_ACC_SYNCHRONIZED;
0N/A return (flags & (req | neg)) == req;
0N/A}
0N/A
0N/A// These are for forming case labels:
0N/A#define ID3(x, y, z) (( jint)(z) + \
0N/A ((jint)(y) << vmSymbols::log2_SID_LIMIT) + \
0N/A ((jint)(x) << (2*vmSymbols::log2_SID_LIMIT)) )
0N/A#define SID_ENUM(n) vmSymbols::VM_SYMBOL_ENUM_NAME(n)
0N/A
0N/AvmIntrinsics::ID vmIntrinsics::find_id(vmSymbols::SID holder,
0N/A vmSymbols::SID name,
0N/A vmSymbols::SID sig,
0N/A jshort flags) {
0N/A assert((int)vmSymbols::SID_LIMIT <= (1<<vmSymbols::log2_SID_LIMIT), "must fit");
0N/A
0N/A // Let the C compiler build the decision tree.
0N/A
0N/A#define VM_INTRINSIC_CASE(id, klass, name, sig, fcode) \
0N/A case ID3(SID_ENUM(klass), SID_ENUM(name), SID_ENUM(sig)): \
0N/A if (!match_##fcode(flags)) break; \
0N/A return id;
0N/A
0N/A switch (ID3(holder, name, sig)) {
0N/A VM_INTRINSICS_DO(VM_INTRINSIC_CASE,
0N/A VM_SYMBOL_IGNORE, VM_SYMBOL_IGNORE, VM_SYMBOL_IGNORE, VM_ALIAS_IGNORE);
0N/A }
0N/A return vmIntrinsics::_none;
0N/A
0N/A#undef VM_INTRINSIC_CASE
0N/A}
0N/A
0N/A
0N/Aconst char* vmIntrinsics::short_name_as_C_string(vmIntrinsics::ID id, char* buf, int buflen) {
0N/A const char* str = name_at(id);
0N/A#ifndef PRODUCT
0N/A const char* kname = vmSymbols::name_for(class_for(id));
0N/A const char* mname = vmSymbols::name_for(name_for(id));
0N/A const char* sname = vmSymbols::name_for(signature_for(id));
0N/A const char* fname = "";
0N/A switch (flags_for(id)) {
0N/A case F_RN: fname = "native "; break;
0N/A case F_SN: fname = "native static "; break;
0N/A case F_S: fname = "static "; break;
0N/A }
0N/A const char* kptr = strrchr(kname, '/');
0N/A if (kptr != NULL) kname = kptr + 1;
0N/A int len = jio_snprintf(buf, buflen, "%s: %s%s.%s%s",
0N/A str, fname, kname, mname, sname);
0N/A if (len < buflen)
0N/A str = buf;
0N/A#endif //PRODUCT
0N/A return str;
0N/A}
0N/A
0N/A
0N/A// These are for friendly printouts of intrinsics:
0N/A
0N/AvmSymbols::SID vmIntrinsics::class_for(vmIntrinsics::ID id) {
0N/A#ifndef PRODUCT
0N/A#define VM_INTRINSIC_CASE(id, klass, name, sig, fcode) \
0N/A case id: return SID_ENUM(klass);
0N/A
0N/A switch (id) {
0N/A VM_INTRINSICS_DO(VM_INTRINSIC_CASE,
0N/A VM_SYMBOL_IGNORE, VM_SYMBOL_IGNORE, VM_SYMBOL_IGNORE, VM_ALIAS_IGNORE);
0N/A }
0N/A#undef VM_INTRINSIC_CASE
0N/A#endif //PRODUCT
0N/A return vmSymbols::NO_SID;
0N/A}
0N/A
0N/AvmSymbols::SID vmIntrinsics::name_for(vmIntrinsics::ID id) {
0N/A#ifndef PRODUCT
0N/A#define VM_INTRINSIC_CASE(id, klass, name, sig, fcode) \
0N/A case id: return SID_ENUM(name);
0N/A
0N/A switch (id) {
0N/A VM_INTRINSICS_DO(VM_INTRINSIC_CASE,
0N/A VM_SYMBOL_IGNORE, VM_SYMBOL_IGNORE, VM_SYMBOL_IGNORE, VM_ALIAS_IGNORE);
0N/A }
0N/A#undef VM_INTRINSIC_CASE
0N/A#endif //PRODUCT
0N/A return vmSymbols::NO_SID;
0N/A}
0N/A
0N/AvmSymbols::SID vmIntrinsics::signature_for(vmIntrinsics::ID id) {
0N/A#ifndef PRODUCT
0N/A#define VM_INTRINSIC_CASE(id, klass, name, sig, fcode) \
0N/A case id: return SID_ENUM(sig);
0N/A
0N/A switch (id) {
0N/A VM_INTRINSICS_DO(VM_INTRINSIC_CASE,
0N/A VM_SYMBOL_IGNORE, VM_SYMBOL_IGNORE, VM_SYMBOL_IGNORE, VM_ALIAS_IGNORE);
0N/A }
0N/A#undef VM_INTRINSIC_CASE
0N/A#endif //PRODUCT
0N/A return vmSymbols::NO_SID;
0N/A}
0N/A
0N/AvmIntrinsics::Flags vmIntrinsics::flags_for(vmIntrinsics::ID id) {
0N/A#ifndef PRODUCT
0N/A#define VM_INTRINSIC_CASE(id, klass, name, sig, fcode) \
0N/A case id: return fcode;
0N/A
0N/A switch (id) {
0N/A VM_INTRINSICS_DO(VM_INTRINSIC_CASE,
0N/A VM_SYMBOL_IGNORE, VM_SYMBOL_IGNORE, VM_SYMBOL_IGNORE, VM_ALIAS_IGNORE);
0N/A }
0N/A#undef VM_INTRINSIC_CASE
0N/A#endif //PRODUCT
0N/A return F_none;
0N/A}
0N/A
0N/A
0N/A#ifndef PRODUCT
0N/A// verify_method performs an extra check on a matched intrinsic method
0N/A
0N/Astatic bool match_method(methodOop m, symbolOop n, symbolOop s) {
0N/A return (m->name() == n &&
0N/A m->signature() == s);
0N/A}
0N/A
0N/Astatic vmIntrinsics::ID match_method_with_klass(methodOop m, symbolOop mk) {
0N/A#define VM_INTRINSIC_MATCH(id, klassname, namepart, sigpart, flags) \
0N/A { symbolOop k = vmSymbols::klassname(); \
0N/A if (mk == k) { \
0N/A symbolOop n = vmSymbols::namepart(); \
0N/A symbolOop s = vmSymbols::sigpart(); \
0N/A if (match_method(m, n, s)) \
0N/A return vmIntrinsics::id; \
0N/A } }
0N/A VM_INTRINSICS_DO(VM_INTRINSIC_MATCH,
0N/A VM_SYMBOL_IGNORE, VM_SYMBOL_IGNORE, VM_SYMBOL_IGNORE, VM_ALIAS_IGNORE);
0N/A return vmIntrinsics::_none;
0N/A#undef VM_INTRINSIC_MATCH
0N/A}
0N/A
0N/Avoid vmIntrinsics::verify_method(ID actual_id, methodOop m) {
0N/A symbolOop mk = Klass::cast(m->method_holder())->name();
0N/A ID declared_id = match_method_with_klass(m, mk);
0N/A
0N/A if (declared_id == actual_id) return; // success
0N/A
0N/A if (declared_id == _none && actual_id != _none && mk == vmSymbols::java_lang_StrictMath()) {
0N/A // Here are a few special cases in StrictMath not declared in vmSymbols.hpp.
0N/A switch (actual_id) {
0N/A case _min:
0N/A case _max:
0N/A case _dsqrt:
0N/A declared_id = match_method_with_klass(m, vmSymbols::java_lang_Math());
0N/A if (declared_id == actual_id) return; // acceptable alias
0N/A break;
0N/A }
0N/A }
0N/A
0N/A const char* declared_name = name_at(declared_id);
0N/A const char* actual_name = name_at(actual_id);
0N/A methodHandle mh = m;
0N/A m = NULL;
0N/A ttyLocker ttyl;
0N/A if (xtty != NULL) {
0N/A xtty->begin_elem("intrinsic_misdeclared actual='%s' declared='%s'",
0N/A actual_name, declared_name);
0N/A xtty->method(mh);
0N/A xtty->end_elem("");
0N/A }
0N/A if (PrintMiscellaneous && (WizardMode || Verbose)) {
0N/A tty->print_cr("*** misidentified method; %s(%d) should be %s(%d):",
0N/A declared_name, declared_id, actual_name, actual_id);
0N/A m->print_short_name(tty);
0N/A tty->cr();
0N/A }
0N/A}
0N/A#endif //PRODUCT