duke@435: /* coleenp@4490: * 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@4490: #include "classfile/metadataOnStackMark.hpp" stefank@2314: #include "classfile/systemDictionary.hpp" stefank@2314: #include "code/debugInfoRec.hpp" stefank@2314: #include "gc_interface/collectedHeap.inline.hpp" stefank@2314: #include "interpreter/bytecodeStream.hpp" stefank@2314: #include "interpreter/bytecodeTracer.hpp" stefank@2314: #include "interpreter/bytecodes.hpp" stefank@2314: #include "interpreter/interpreter.hpp" stefank@2314: #include "interpreter/oopMapCache.hpp" stefank@2314: #include "memory/gcLocker.hpp" stefank@2314: #include "memory/generation.hpp" acorn@4497: #include "memory/heapInspection.hpp" coleenp@4037: #include "memory/metadataFactory.hpp" stefank@2314: #include "memory/oopFactory.hpp" kamg@4245: #include "oops/constMethod.hpp" coleenp@4037: #include "oops/methodData.hpp" coleenp@4037: #include "oops/method.hpp" stefank@2314: #include "oops/oop.inline.hpp" coleenp@2497: #include "oops/symbol.hpp" stefank@2314: #include "prims/jvmtiExport.hpp" twisti@3969: #include "prims/methodHandles.hpp" stefank@2314: #include "prims/nativeLookup.hpp" stefank@2314: #include "runtime/arguments.hpp" stefank@2314: #include "runtime/compilationPolicy.hpp" stefank@2314: #include "runtime/frame.inline.hpp" stefank@2314: #include "runtime/handles.inline.hpp" stefank@2314: #include "runtime/relocator.hpp" stefank@2314: #include "runtime/sharedRuntime.hpp" stefank@2314: #include "runtime/signature.hpp" brutisso@2976: #include "utilities/quickSort.hpp" stefank@2314: #include "utilities/xmlstream.hpp" duke@435: duke@435: coleenp@4037: // Implementation of Method duke@435: coleenp@4037: Method* Method::allocate(ClassLoaderData* loader_data, kamg@4245: int byte_code_size, kamg@4245: AccessFlags access_flags, coleenp@4572: InlineTableSizes* sizes, kamg@4245: ConstMethod::MethodType method_type, kamg@4245: TRAPS) { coleenp@4037: assert(!access_flags.is_native() || byte_code_size == 0, coleenp@4037: "native methods should not contain byte codes"); coleenp@4037: ConstMethod* cm = ConstMethod::allocate(loader_data, kamg@4245: byte_code_size, coleenp@4572: sizes, kamg@4245: method_type, kamg@4245: CHECK_NULL); coleenp@4037: coleenp@4037: int size = Method::size(access_flags.is_native()); coleenp@4037: coleenp@4037: return new (loader_data, size, false, THREAD) Method(cm, access_flags, size); coleenp@4037: } coleenp@4037: coleenp@4712: Method::Method(ConstMethod* xconst, AccessFlags access_flags, int size) { coleenp@4037: No_Safepoint_Verifier no_safepoint; coleenp@4037: set_constMethod(xconst); coleenp@4037: set_access_flags(access_flags); coleenp@4037: set_method_size(size); coleenp@4037: #ifdef CC_INTERP coleenp@4037: set_result_index(T_VOID); coleenp@4037: #endif coleenp@4037: set_intrinsic_id(vmIntrinsics::_none); coleenp@4037: set_jfr_towrite(false); coleenp@4037: set_method_data(NULL); coleenp@4037: set_interpreter_throwout_count(0); coleenp@4037: set_vtable_index(Method::garbage_vtable_index); coleenp@4037: coleenp@4037: // Fix and bury in Method* coleenp@4037: set_interpreter_entry(NULL); // sets i2i entry and from_int coleenp@4037: set_adapter_entry(NULL); coleenp@4037: clear_code(); // from_c/from_i get set to c2i/i2i coleenp@4037: coleenp@4037: if (access_flags.is_native()) { coleenp@4037: clear_native_function(); coleenp@4037: set_signature_handler(NULL); coleenp@4037: } coleenp@4037: coleenp@4037: NOT_PRODUCT(set_compiled_invocation_count(0);) coleenp@4037: set_interpreter_invocation_count(0); coleenp@4037: invocation_counter()->init(); coleenp@4037: backedge_counter()->init(); coleenp@4037: clear_number_of_breakpoints(); coleenp@4037: coleenp@4037: #ifdef TIERED coleenp@4037: set_rate(0); coleenp@4037: set_prev_event_count(0); coleenp@4037: set_prev_time(0); coleenp@4037: #endif coleenp@4037: } coleenp@4037: coleenp@4037: // Release Method*. The nmethod will be gone when we get here because coleenp@4037: // we've walked the code cache. coleenp@4037: void Method::deallocate_contents(ClassLoaderData* loader_data) { coleenp@4037: MetadataFactory::free_metadata(loader_data, constMethod()); coleenp@4037: set_constMethod(NULL); coleenp@4037: MetadataFactory::free_metadata(loader_data, method_data()); coleenp@4037: set_method_data(NULL); coleenp@4037: // The nmethod will be gone when we get here. coleenp@4037: if (code() != NULL) _code = NULL; coleenp@4037: } coleenp@4037: coleenp@4037: address Method::get_i2c_entry() { duke@435: assert(_adapter != NULL, "must have"); duke@435: return _adapter->get_i2c_entry(); duke@435: } duke@435: coleenp@4037: address Method::get_c2i_entry() { duke@435: assert(_adapter != NULL, "must have"); duke@435: return _adapter->get_c2i_entry(); duke@435: } duke@435: coleenp@4037: address Method::get_c2i_unverified_entry() { duke@435: assert(_adapter != NULL, "must have"); duke@435: return _adapter->get_c2i_unverified_entry(); duke@435: } duke@435: coleenp@4037: char* Method::name_and_sig_as_C_string() const { hseigel@4278: return name_and_sig_as_C_string(constants()->pool_holder(), name(), signature()); duke@435: } duke@435: coleenp@4037: char* Method::name_and_sig_as_C_string(char* buf, int size) const { hseigel@4278: return name_and_sig_as_C_string(constants()->pool_holder(), name(), signature(), buf, size); duke@435: } duke@435: coleenp@4037: char* Method::name_and_sig_as_C_string(Klass* klass, Symbol* method_name, Symbol* signature) { duke@435: const char* klass_name = klass->external_name(); duke@435: int klass_name_len = (int)strlen(klass_name); duke@435: int method_name_len = method_name->utf8_length(); duke@435: int len = klass_name_len + 1 + method_name_len + signature->utf8_length(); duke@435: char* dest = NEW_RESOURCE_ARRAY(char, len + 1); duke@435: strcpy(dest, klass_name); duke@435: dest[klass_name_len] = '.'; duke@435: strcpy(&dest[klass_name_len + 1], method_name->as_C_string()); duke@435: strcpy(&dest[klass_name_len + 1 + method_name_len], signature->as_C_string()); duke@435: dest[len] = 0; duke@435: return dest; duke@435: } duke@435: coleenp@4037: char* Method::name_and_sig_as_C_string(Klass* klass, Symbol* method_name, Symbol* signature, char* buf, int size) { coleenp@2497: Symbol* klass_name = klass->name(); duke@435: klass_name->as_klass_external_name(buf, size); duke@435: int len = (int)strlen(buf); duke@435: duke@435: if (len < size - 1) { duke@435: buf[len++] = '.'; duke@435: duke@435: method_name->as_C_string(&(buf[len]), size - len); duke@435: len = (int)strlen(buf); duke@435: duke@435: signature->as_C_string(&(buf[len]), size - len); duke@435: } duke@435: duke@435: return buf; duke@435: } duke@435: jiangli@4405: int Method::fast_exception_handler_bci_for(methodHandle mh, KlassHandle ex_klass, int throw_bci, TRAPS) { duke@435: // exception table holds quadruple entries of the form (beg_bci, end_bci, handler_bci, klass_index) duke@435: // access exception table jiangli@4405: ExceptionTable table(mh()); jiangli@3917: int length = table.length(); duke@435: // iterate through all entries sequentially jiangli@4405: constantPoolHandle pool(THREAD, mh->constants()); jiangli@3917: for (int i = 0; i < length; i ++) { jiangli@3917: //reacquire the table in case a GC happened jiangli@4405: ExceptionTable table(mh()); jiangli@3917: int beg_bci = table.start_pc(i); jiangli@3917: int end_bci = table.end_pc(i); duke@435: assert(beg_bci <= end_bci, "inconsistent exception table"); duke@435: if (beg_bci <= throw_bci && throw_bci < end_bci) { duke@435: // exception handler bci range covers throw_bci => investigate further jiangli@3917: int handler_bci = table.handler_pc(i); jiangli@3917: int klass_index = table.catch_type_index(i); duke@435: if (klass_index == 0) { duke@435: return handler_bci; duke@435: } else if (ex_klass.is_null()) { duke@435: return handler_bci; duke@435: } else { duke@435: // we know the exception class => get the constraint class duke@435: // this may require loading of the constraint class; if verification duke@435: // fails or some other exception occurs, return handler_bci coleenp@4037: Klass* k = pool->klass_at(klass_index, CHECK_(handler_bci)); duke@435: KlassHandle klass = KlassHandle(THREAD, k); duke@435: assert(klass.not_null(), "klass not loaded"); duke@435: if (ex_klass->is_subtype_of(klass())) { duke@435: return handler_bci; duke@435: } duke@435: } duke@435: } duke@435: } duke@435: duke@435: return -1; duke@435: } duke@435: coleenp@4037: void Method::mask_for(int bci, InterpreterOopMap* mask) { duke@435: duke@435: Thread* myThread = Thread::current(); duke@435: methodHandle h_this(myThread, this); duke@435: #ifdef ASSERT duke@435: bool has_capability = myThread->is_VM_thread() || duke@435: myThread->is_ConcurrentGC_thread() || duke@435: myThread->is_GC_task_thread(); duke@435: duke@435: if (!has_capability) { duke@435: if (!VerifyStack && !VerifyLastFrame) { duke@435: // verify stack calls this outside VM thread duke@435: warning("oopmap should only be accessed by the " duke@435: "VM, GC task or CMS threads (or during debugging)"); duke@435: InterpreterOopMap local_mask; coleenp@4251: method_holder()->mask_for(h_this, bci, &local_mask); duke@435: local_mask.print(); duke@435: } duke@435: } duke@435: #endif coleenp@4251: method_holder()->mask_for(h_this, bci, mask); duke@435: return; duke@435: } duke@435: duke@435: coleenp@4037: int Method::bci_from(address bcp) const { kvn@4102: #ifdef ASSERT kvn@4102: { ResourceMark rm; twisti@3848: assert(is_native() && bcp == code_base() || contains(bcp) || is_error_reported(), twisti@3848: err_msg("bcp doesn't belong to this method: bcp: " INTPTR_FORMAT ", method: %s", bcp, name_and_sig_as_C_string())); kvn@4102: } kvn@4102: #endif duke@435: return bcp - code_base(); duke@435: } duke@435: duke@435: duke@435: // Return (int)bcx if it appears to be a valid BCI. duke@435: // Return bci_from((address)bcx) if it appears to be a valid BCP. duke@435: // Return -1 otherwise. duke@435: // Used by profiling code, when invalid data is a possibility. coleenp@4037: // The caller is responsible for validating the Method* itself. coleenp@4037: int Method::validate_bci_from_bcx(intptr_t bcx) const { duke@435: // keep bci as -1 if not a valid bci duke@435: int bci = -1; duke@435: if (bcx == 0 || (address)bcx == code_base()) { duke@435: // code_size() may return 0 and we allow 0 here duke@435: // the method may be native duke@435: bci = 0; duke@435: } else if (frame::is_bci(bcx)) { duke@435: if (bcx < code_size()) { duke@435: bci = (int)bcx; duke@435: } duke@435: } else if (contains((address)bcx)) { duke@435: bci = (address)bcx - code_base(); duke@435: } duke@435: // Assert that if we have dodged any asserts, bci is negative. duke@435: assert(bci == -1 || bci == bci_from(bcp_from(bci)), "sane bci if >=0"); duke@435: return bci; duke@435: } duke@435: coleenp@4037: address Method::bcp_from(int bci) const { duke@435: assert((is_native() && bci == 0) || (!is_native() && 0 <= bci && bci < code_size()), "illegal bci"); duke@435: address bcp = code_base() + bci; duke@435: assert(is_native() && bcp == code_base() || contains(bcp), "bcp doesn't belong to this method"); duke@435: return bcp; duke@435: } duke@435: duke@435: coleenp@4037: int Method::size(bool is_native) { duke@435: // If native, then include pointers for native_function and signature_handler duke@435: int extra_bytes = (is_native) ? 2*sizeof(address*) : 0; duke@435: int extra_words = align_size_up(extra_bytes, BytesPerWord) / BytesPerWord; duke@435: return align_object_size(header_size() + extra_words); duke@435: } duke@435: duke@435: coleenp@4037: Symbol* Method::klass_name() const { coleenp@4037: Klass* k = method_holder(); duke@435: assert(k->is_klass(), "must be klass"); coleenp@4037: InstanceKlass* ik = (InstanceKlass*) k; duke@435: return ik->name(); duke@435: } duke@435: duke@435: duke@435: // Attempt to return method oop to original state. Clear any pointers duke@435: // (to objects outside the shared spaces). We won't be able to predict duke@435: // where they should point in a new JVM. Further initialize some duke@435: // entries now in order allow them to be write protected later. duke@435: coleenp@4037: void Method::remove_unshareable_info() { duke@435: unlink_method(); duke@435: } duke@435: duke@435: coleenp@4037: bool Method::was_executed_more_than(int n) { coleenp@4037: // Invocation counter is reset when the Method* is compiled. duke@435: // If the method has compiled code we therefore assume it has duke@435: // be excuted more than n times. duke@435: if (is_accessor() || is_empty_method() || (code() != NULL)) { duke@435: // interpreter doesn't bump invocation counter of trivial methods duke@435: // compiler does not bump invocation counter of compiled methods duke@435: return true; iveresov@2138: } iveresov@2138: else if (_invocation_counter.carry() || (method_data() != NULL && method_data()->invocation_counter()->carry())) { duke@435: // The carry bit is set when the counter overflows and causes duke@435: // a compilation to occur. We don't know how many times duke@435: // the counter has been reset, so we simply assume it has duke@435: // been executed more than n times. duke@435: return true; duke@435: } else { duke@435: return invocation_count() > n; duke@435: } duke@435: } duke@435: duke@435: #ifndef PRODUCT coleenp@4037: void Method::print_invocation_count() { duke@435: if (is_static()) tty->print("static "); duke@435: if (is_final()) tty->print("final "); duke@435: if (is_synchronized()) tty->print("synchronized "); duke@435: if (is_native()) tty->print("native "); coleenp@4037: method_holder()->name()->print_symbol_on(tty); duke@435: tty->print("."); duke@435: name()->print_symbol_on(tty); duke@435: signature()->print_symbol_on(tty); duke@435: duke@435: if (WizardMode) { duke@435: // dump the size of the byte codes duke@435: tty->print(" {%d}", code_size()); duke@435: } duke@435: tty->cr(); duke@435: duke@435: tty->print_cr (" interpreter_invocation_count: %8d ", interpreter_invocation_count()); duke@435: tty->print_cr (" invocation_counter: %8d ", invocation_count()); duke@435: tty->print_cr (" backedge_counter: %8d ", backedge_count()); duke@435: if (CountCompiledCalls) { duke@435: tty->print_cr (" compiled_invocation_count: %8d ", compiled_invocation_count()); duke@435: } duke@435: duke@435: } duke@435: #endif duke@435: coleenp@4037: // Build a MethodData* object to hold information about this method duke@435: // collected in the interpreter. coleenp@4037: void Method::build_interpreter_method_data(methodHandle method, TRAPS) { coleenp@2363: // Do not profile method if current thread holds the pending list lock, coleenp@2363: // which avoids deadlock for acquiring the MethodData_lock. coleenp@4047: if (InstanceRefKlass::owns_pending_list_lock((JavaThread*)THREAD)) { coleenp@2363: return; coleenp@2363: } coleenp@2363: duke@435: // Grab a lock here to prevent multiple coleenp@4037: // MethodData*s from being created. duke@435: MutexLocker ml(MethodData_lock, THREAD); duke@435: if (method->method_data() == NULL) { coleenp@4037: ClassLoaderData* loader_data = method->method_holder()->class_loader_data(); coleenp@4037: MethodData* method_data = MethodData::allocate(loader_data, method, CHECK); duke@435: method->set_method_data(method_data); duke@435: if (PrintMethodData && (Verbose || WizardMode)) { duke@435: ResourceMark rm(THREAD); duke@435: tty->print("build_interpreter_method_data for "); duke@435: method->print_name(tty); duke@435: tty->cr(); duke@435: // At the end of the run, the MDO, full of data, will be dumped. duke@435: } duke@435: } duke@435: } duke@435: coleenp@4037: void Method::cleanup_inline_caches() { duke@435: // The current system doesn't use inline caches in the interpreter duke@435: // => nothing to do (keep this method around for future use) duke@435: } duke@435: duke@435: coleenp@4037: int Method::extra_stack_words() { jrose@1145: // not an inline function, to avoid a header dependency on Interpreter twisti@1861: return extra_stack_entries() * Interpreter::stackElementSize; jrose@1145: } jrose@1145: jrose@1145: coleenp@4037: void Method::compute_size_of_parameters(Thread *thread) { coleenp@2497: ArgumentSizeComputer asc(signature()); duke@435: set_size_of_parameters(asc.size() + (is_static() ? 0 : 1)); duke@435: } duke@435: duke@435: #ifdef CC_INTERP coleenp@4037: void Method::set_result_index(BasicType type) { duke@435: _result_index = Interpreter::BasicType_as_index(type); duke@435: } duke@435: #endif duke@435: coleenp@4037: BasicType Method::result_type() const { duke@435: ResultTypeFinder rtf(signature()); duke@435: return rtf.type(); duke@435: } duke@435: duke@435: coleenp@4037: bool Method::is_empty_method() const { duke@435: return code_size() == 1 duke@435: && *code_base() == Bytecodes::_return; duke@435: } duke@435: duke@435: coleenp@4037: bool Method::is_vanilla_constructor() const { duke@435: // Returns true if this method is a vanilla constructor, i.e. an "" "()V" method duke@435: // which only calls the superclass vanilla constructor and possibly does stores of duke@435: // zero constants to local fields: duke@435: // duke@435: // aload_0 duke@435: // invokespecial duke@435: // indexbyte1 duke@435: // indexbyte2 duke@435: // duke@435: // followed by an (optional) sequence of: duke@435: // duke@435: // aload_0 duke@435: // aconst_null / iconst_0 / fconst_0 / dconst_0 duke@435: // putfield duke@435: // indexbyte1 duke@435: // indexbyte2 duke@435: // duke@435: // followed by: duke@435: // duke@435: // return duke@435: duke@435: assert(name() == vmSymbols::object_initializer_name(), "Should only be called for default constructors"); duke@435: assert(signature() == vmSymbols::void_method_signature(), "Should only be called for default constructors"); duke@435: int size = code_size(); duke@435: // Check if size match duke@435: if (size == 0 || size % 5 != 0) return false; duke@435: address cb = code_base(); duke@435: int last = size - 1; duke@435: if (cb[0] != Bytecodes::_aload_0 || cb[1] != Bytecodes::_invokespecial || cb[last] != Bytecodes::_return) { duke@435: // Does not call superclass default constructor duke@435: return false; duke@435: } duke@435: // Check optional sequence duke@435: for (int i = 4; i < last; i += 5) { duke@435: if (cb[i] != Bytecodes::_aload_0) return false; duke@435: if (!Bytecodes::is_zero_const(Bytecodes::cast(cb[i+1]))) return false; duke@435: if (cb[i+2] != Bytecodes::_putfield) return false; duke@435: } duke@435: return true; duke@435: } duke@435: duke@435: coleenp@4037: bool Method::compute_has_loops_flag() { coleenp@4037: BytecodeStream bcs(this); duke@435: Bytecodes::Code bc; duke@435: duke@435: while ((bc = bcs.next()) >= 0) { duke@435: switch( bc ) { duke@435: case Bytecodes::_ifeq: duke@435: case Bytecodes::_ifnull: duke@435: case Bytecodes::_iflt: duke@435: case Bytecodes::_ifle: duke@435: case Bytecodes::_ifne: duke@435: case Bytecodes::_ifnonnull: duke@435: case Bytecodes::_ifgt: duke@435: case Bytecodes::_ifge: duke@435: case Bytecodes::_if_icmpeq: duke@435: case Bytecodes::_if_icmpne: duke@435: case Bytecodes::_if_icmplt: duke@435: case Bytecodes::_if_icmpgt: duke@435: case Bytecodes::_if_icmple: duke@435: case Bytecodes::_if_icmpge: duke@435: case Bytecodes::_if_acmpeq: duke@435: case Bytecodes::_if_acmpne: duke@435: case Bytecodes::_goto: duke@435: case Bytecodes::_jsr: duke@435: if( bcs.dest() < bcs.next_bci() ) _access_flags.set_has_loops(); duke@435: break; duke@435: duke@435: case Bytecodes::_goto_w: duke@435: case Bytecodes::_jsr_w: duke@435: if( bcs.dest_w() < bcs.next_bci() ) _access_flags.set_has_loops(); duke@435: break; duke@435: } duke@435: } duke@435: _access_flags.set_loops_flag_init(); duke@435: return _access_flags.has_loops(); duke@435: } duke@435: duke@435: coleenp@4037: bool Method::is_final_method() const { duke@435: // %%% Should return true for private methods also, duke@435: // since there is no way to override them. coleenp@4251: return is_final() || method_holder()->is_final(); duke@435: } duke@435: duke@435: coleenp@4037: bool Method::is_strict_method() const { duke@435: return is_strict(); duke@435: } duke@435: duke@435: coleenp@4037: bool Method::can_be_statically_bound() const { duke@435: if (is_final_method()) return true; duke@435: return vtable_index() == nonvirtual_vtable_index; duke@435: } duke@435: duke@435: coleenp@4037: bool Method::is_accessor() const { duke@435: if (code_size() != 5) return false; duke@435: if (size_of_parameters() != 1) return false; never@2462: if (java_code_at(0) != Bytecodes::_aload_0 ) return false; never@2462: if (java_code_at(1) != Bytecodes::_getfield) return false; never@2462: if (java_code_at(4) != Bytecodes::_areturn && never@2462: java_code_at(4) != Bytecodes::_ireturn ) return false; duke@435: return true; duke@435: } duke@435: duke@435: coleenp@4037: bool Method::is_initializer() const { kamg@2616: return name() == vmSymbols::object_initializer_name() || is_static_initializer(); kamg@2616: } kamg@2616: coleenp@4037: bool Method::has_valid_initializer_flags() const { kamg@2616: return (is_static() || coleenp@4251: method_holder()->major_version() < 51); kamg@2616: } kamg@2616: coleenp@4037: bool Method::is_static_initializer() const { kamg@2616: // For classfiles version 51 or greater, ensure that the clinit method is kamg@2616: // static. Non-static methods with the name "" are not static kamg@2616: // initializers. (older classfiles exempted for backward compatibility) kamg@2616: return name() == vmSymbols::class_initializer_name() && kamg@2616: has_valid_initializer_flags(); duke@435: } duke@435: duke@435: coleenp@4037: objArrayHandle Method::resolved_checked_exceptions_impl(Method* this_oop, TRAPS) { duke@435: int length = this_oop->checked_exceptions_length(); duke@435: if (length == 0) { // common case duke@435: return objArrayHandle(THREAD, Universe::the_empty_class_klass_array()); duke@435: } else { duke@435: methodHandle h_this(THREAD, this_oop); never@1577: objArrayOop m_oop = oopFactory::new_objArray(SystemDictionary::Class_klass(), length, CHECK_(objArrayHandle())); duke@435: objArrayHandle mirrors (THREAD, m_oop); duke@435: for (int i = 0; i < length; i++) { duke@435: CheckedExceptionElement* table = h_this->checked_exceptions_start(); // recompute on each iteration, not gc safe coleenp@4037: Klass* k = h_this->constants()->klass_at(table[i].class_cp_index, CHECK_(objArrayHandle())); hseigel@4278: assert(k->is_subclass_of(SystemDictionary::Throwable_klass()), "invalid exception class"); hseigel@4278: mirrors->obj_at_put(i, k->java_mirror()); duke@435: } duke@435: return mirrors; duke@435: } duke@435: }; duke@435: duke@435: coleenp@4037: int Method::line_number_from_bci(int bci) const { duke@435: if (bci == SynchronizationEntryBCI) bci = 0; duke@435: assert(bci == 0 || 0 <= bci && bci < code_size(), "illegal bci"); duke@435: int best_bci = 0; duke@435: int best_line = -1; duke@435: duke@435: if (has_linenumber_table()) { duke@435: // The line numbers are a short array of 2-tuples [start_pc, line_number]. duke@435: // Not necessarily sorted and not necessarily one-to-one. duke@435: CompressedLineNumberReadStream stream(compressed_linenumber_table()); duke@435: while (stream.read_pair()) { duke@435: if (stream.bci() == bci) { duke@435: // perfect match duke@435: return stream.line(); duke@435: } else { duke@435: // update best_bci/line duke@435: if (stream.bci() < bci && stream.bci() >= best_bci) { duke@435: best_bci = stream.bci(); duke@435: best_line = stream.line(); duke@435: } duke@435: } duke@435: } duke@435: } duke@435: return best_line; duke@435: } duke@435: duke@435: coleenp@4037: bool Method::is_klass_loaded_by_klass_index(int klass_index) const { jiangli@3826: if( constants()->tag_at(klass_index).is_unresolved_klass() ) { duke@435: Thread *thread = Thread::current(); jiangli@3826: Symbol* klass_name = constants()->klass_name_at(klass_index); coleenp@4251: Handle loader(thread, method_holder()->class_loader()); hseigel@4278: Handle prot (thread, method_holder()->protection_domain()); duke@435: return SystemDictionary::find(klass_name, loader, prot, thread) != NULL; duke@435: } else { duke@435: return true; duke@435: } duke@435: } duke@435: duke@435: coleenp@4037: bool Method::is_klass_loaded(int refinfo_index, bool must_be_resolved) const { jiangli@3826: int klass_index = constants()->klass_ref_index_at(refinfo_index); duke@435: if (must_be_resolved) { duke@435: // Make sure klass is resolved in constantpool. duke@435: if (constants()->tag_at(klass_index).is_unresolved_klass()) return false; duke@435: } duke@435: return is_klass_loaded_by_klass_index(klass_index); duke@435: } duke@435: duke@435: coleenp@4037: void Method::set_native_function(address function, bool post_event_flag) { duke@435: assert(function != NULL, "use clear_native_function to unregister natives"); twisti@3969: assert(!is_method_handle_intrinsic() || function == SharedRuntime::native_method_throw_unsatisfied_link_error_entry(), ""); duke@435: address* native_function = native_function_addr(); duke@435: duke@435: // We can see racers trying to place the same native function into place. Once duke@435: // is plenty. duke@435: address current = *native_function; duke@435: if (current == function) return; duke@435: if (post_event_flag && JvmtiExport::should_post_native_method_bind() && duke@435: function != NULL) { duke@435: // native_method_throw_unsatisfied_link_error_entry() should only duke@435: // be passed when post_event_flag is false. duke@435: assert(function != duke@435: SharedRuntime::native_method_throw_unsatisfied_link_error_entry(), duke@435: "post_event_flag mis-match"); duke@435: duke@435: // post the bind event, and possible change the bind function duke@435: JvmtiExport::post_native_method_bind(this, &function); duke@435: } duke@435: *native_function = function; duke@435: // This function can be called more than once. We must make sure that we always duke@435: // use the latest registered method -> check if a stub already has been generated. duke@435: // If so, we have to make it not_entrant. duke@435: nmethod* nm = code(); // Put it into local variable to guard against concurrent updates duke@435: if (nm != NULL) { duke@435: nm->make_not_entrant(); duke@435: } duke@435: } duke@435: duke@435: coleenp@4037: bool Method::has_native_function() const { twisti@3970: if (is_method_handle_intrinsic()) twisti@3970: return false; // special-cased in SharedRuntime::generate_native_wrapper duke@435: address func = native_function(); duke@435: return (func != NULL && func != SharedRuntime::native_method_throw_unsatisfied_link_error_entry()); duke@435: } duke@435: duke@435: coleenp@4037: void Method::clear_native_function() { twisti@3969: // Note: is_method_handle_intrinsic() is allowed here. duke@435: set_native_function( duke@435: SharedRuntime::native_method_throw_unsatisfied_link_error_entry(), duke@435: !native_bind_event_is_interesting); duke@435: clear_code(); duke@435: } duke@435: coleenp@4037: address Method::critical_native_function() { never@3500: methodHandle mh(this); never@3500: return NativeLookup::lookup_critical_entry(mh); never@3500: } never@3500: duke@435: coleenp@4037: void Method::set_signature_handler(address handler) { duke@435: address* signature_handler = signature_handler_addr(); duke@435: *signature_handler = handler; duke@435: } duke@435: duke@435: vlivanov@4539: void Method::print_made_not_compilable(int comp_level, bool is_osr, bool report, const char* reason) { kvn@1643: if (PrintCompilation && report) { kvn@1641: ttyLocker ttyl; twisti@4111: tty->print("made not %scompilable on ", is_osr ? "OSR " : ""); twisti@4111: if (comp_level == CompLevel_all) { twisti@4111: tty->print("all levels "); twisti@4111: } else { twisti@4111: tty->print("levels "); twisti@4111: for (int i = (int)CompLevel_none; i <= comp_level; i++) { twisti@4111: tty->print("%d ", i); twisti@4111: } twisti@4111: } kvn@1641: this->print_short_name(tty); kvn@1641: int size = this->code_size(); vlivanov@4539: if (size > 0) { kvn@1641: tty->print(" (%d bytes)", size); vlivanov@4539: } vlivanov@4539: if (reason != NULL) { vlivanov@4539: tty->print(" %s", reason); vlivanov@4539: } kvn@1641: tty->cr(); kvn@1641: } duke@435: if ((TraceDeoptimization || LogCompilation) && (xtty != NULL)) { duke@435: ttyLocker ttyl; vlivanov@4154: xtty->begin_elem("make_not_%scompilable thread='" UINTX_FORMAT "'", vlivanov@4154: is_osr ? "osr_" : "", os::current_thread_id()); vlivanov@4539: if (reason != NULL) { vlivanov@4539: xtty->print(" reason=\'%s\'", reason); vlivanov@4539: } coleenp@4037: xtty->method(this); duke@435: xtty->stamp(); duke@435: xtty->end_elem(); duke@435: } twisti@4111: } twisti@4111: twisti@4111: bool Method::is_not_compilable(int comp_level) const { twisti@4111: if (number_of_breakpoints() > 0) twisti@4111: return true; twisti@4111: if (is_method_handle_intrinsic()) twisti@4111: return !is_synthetic(); // the generated adapters must be compiled twisti@4111: if (comp_level == CompLevel_any) twisti@4111: return is_not_c1_compilable() || is_not_c2_compilable(); twisti@4111: if (is_c1_compile(comp_level)) twisti@4111: return is_not_c1_compilable(); twisti@4111: if (is_c2_compile(comp_level)) twisti@4111: return is_not_c2_compilable(); twisti@4111: return false; twisti@4111: } twisti@4111: twisti@4111: // call this when compiler finds that this method is not compilable vlivanov@4539: void Method::set_not_compilable(int comp_level, bool report, const char* reason) { vlivanov@4539: print_made_not_compilable(comp_level, /*is_osr*/ false, report, reason); iveresov@2138: if (comp_level == CompLevel_all) { iveresov@2138: set_not_c1_compilable(); iveresov@2138: set_not_c2_compilable(); iveresov@2138: } else { twisti@4111: if (is_c1_compile(comp_level)) iveresov@2138: set_not_c1_compilable(); twisti@4111: if (is_c2_compile(comp_level)) twisti@4111: set_not_c2_compilable(); twisti@4111: } twisti@4111: CompilationPolicy::policy()->disable_compilation(this); twisti@4111: } twisti@4111: twisti@4111: bool Method::is_not_osr_compilable(int comp_level) const { twisti@4111: if (is_not_compilable(comp_level)) twisti@4111: return true; twisti@4111: if (comp_level == CompLevel_any) twisti@4111: return is_not_c1_osr_compilable() || is_not_c2_osr_compilable(); twisti@4111: if (is_c1_compile(comp_level)) twisti@4111: return is_not_c1_osr_compilable(); twisti@4111: if (is_c2_compile(comp_level)) twisti@4111: return is_not_c2_osr_compilable(); twisti@4111: return false; twisti@4111: } twisti@4111: vlivanov@4539: void Method::set_not_osr_compilable(int comp_level, bool report, const char* reason) { vlivanov@4539: print_made_not_compilable(comp_level, /*is_osr*/ true, report, reason); twisti@4111: if (comp_level == CompLevel_all) { twisti@4111: set_not_c1_osr_compilable(); twisti@4111: set_not_c2_osr_compilable(); twisti@4111: } else { twisti@4111: if (is_c1_compile(comp_level)) twisti@4111: set_not_c1_osr_compilable(); twisti@4111: if (is_c2_compile(comp_level)) twisti@4111: set_not_c2_osr_compilable(); duke@435: } iveresov@2138: CompilationPolicy::policy()->disable_compilation(this); duke@435: } duke@435: duke@435: // Revert to using the interpreter and clear out the nmethod coleenp@4037: void Method::clear_code() { duke@435: duke@435: // this may be NULL if c2i adapters have not been made yet duke@435: // Only should happen at allocate time. duke@435: if (_adapter == NULL) { duke@435: _from_compiled_entry = NULL; duke@435: } else { duke@435: _from_compiled_entry = _adapter->get_c2i_entry(); duke@435: } duke@435: OrderAccess::storestore(); duke@435: _from_interpreted_entry = _i2i_entry; duke@435: OrderAccess::storestore(); duke@435: _code = NULL; duke@435: } duke@435: duke@435: // Called by class data sharing to remove any entry points (which are not shared) coleenp@4037: void Method::unlink_method() { duke@435: _code = NULL; duke@435: _i2i_entry = NULL; duke@435: _from_interpreted_entry = NULL; duke@435: if (is_native()) { duke@435: *native_function_addr() = NULL; duke@435: set_signature_handler(NULL); duke@435: } duke@435: NOT_PRODUCT(set_compiled_invocation_count(0);) duke@435: invocation_counter()->reset(); duke@435: backedge_counter()->reset(); duke@435: _adapter = NULL; duke@435: _from_compiled_entry = NULL; duke@435: assert(_method_data == NULL, "unexpected method data?"); duke@435: set_method_data(NULL); duke@435: set_interpreter_throwout_count(0); duke@435: set_interpreter_invocation_count(0); duke@435: } duke@435: duke@435: // Called when the method_holder is getting linked. Setup entrypoints so the method duke@435: // is ready to be called from interpreter, compiler, and vtables. coleenp@4037: void Method::link_method(methodHandle h_method, TRAPS) { coleenp@2945: // If the code cache is full, we may reenter this function for the coleenp@2945: // leftover methods that weren't linked. coleenp@2945: if (_i2i_entry != NULL) return; coleenp@2945: duke@435: assert(_adapter == NULL, "init'd to NULL" ); duke@435: assert( _code == NULL, "nothing compiled yet" ); duke@435: duke@435: // Setup interpreter entrypoint duke@435: assert(this == h_method(), "wrong h_method()" ); duke@435: address entry = Interpreter::entry_for_method(h_method); duke@435: assert(entry != NULL, "interpreter entry must be non-null"); duke@435: // Sets both _i2i_entry and _from_interpreted_entry duke@435: set_interpreter_entry(entry); twisti@3969: if (is_native() && !is_method_handle_intrinsic()) { duke@435: set_native_function( duke@435: SharedRuntime::native_method_throw_unsatisfied_link_error_entry(), duke@435: !native_bind_event_is_interesting); duke@435: } duke@435: duke@435: // Setup compiler entrypoint. This is made eagerly, so we do not need duke@435: // special handling of vtables. An alternative is to make adapters more duke@435: // lazily by calling make_adapter() from from_compiled_entry() for the duke@435: // normal calls. For vtable calls life gets more complicated. When a duke@435: // call-site goes mega-morphic we need adapters in all methods which can be duke@435: // called from the vtable. We need adapters on such methods that get loaded duke@435: // later. Ditto for mega-morphic itable calls. If this proves to be a duke@435: // problem we'll make these lazily later. coleenp@2946: (void) make_adapters(h_method, CHECK); duke@435: duke@435: // ONLY USE the h_method now as make_adapter may have blocked duke@435: duke@435: } duke@435: coleenp@4037: address Method::make_adapters(methodHandle mh, TRAPS) { duke@435: // Adapters for compiled code are made eagerly here. They are fairly duke@435: // small (generally < 100 bytes) and quick to make (and cached and shared) duke@435: // so making them eagerly shouldn't be too expensive. duke@435: AdapterHandlerEntry* adapter = AdapterHandlerLibrary::get_adapter(mh); duke@435: if (adapter == NULL ) { never@1622: THROW_MSG_NULL(vmSymbols::java_lang_VirtualMachineError(), "out of space in CodeCache for adapters"); duke@435: } duke@435: duke@435: mh->set_adapter_entry(adapter); duke@435: mh->_from_compiled_entry = adapter->get_c2i_entry(); duke@435: return adapter->get_c2i_entry(); duke@435: } duke@435: duke@435: // The verified_code_entry() must be called when a invoke is resolved duke@435: // on this method. duke@435: duke@435: // It returns the compiled code entry point, after asserting not null. duke@435: // This function is called after potential safepoints so that nmethod duke@435: // or adapter that it points to is still live and valid. duke@435: // This function must not hit a safepoint! coleenp@4037: address Method::verified_code_entry() { duke@435: debug_only(No_Safepoint_Verifier nsv;) kvn@1637: nmethod *code = (nmethod *)OrderAccess::load_ptr_acquire(&_code); kvn@1637: if (code == NULL && UseCodeCacheFlushing) { kvn@1637: nmethod *saved_code = CodeCache::find_and_remove_saved_code(this); kvn@1637: if (saved_code != NULL) { kvn@1637: methodHandle method(this); kvn@1637: assert( ! saved_code->is_osr_method(), "should not get here for osr" ); kvn@1637: set_code( method, saved_code ); kvn@1637: } kvn@1637: } kvn@1637: duke@435: assert(_from_compiled_entry != NULL, "must be set"); duke@435: return _from_compiled_entry; duke@435: } duke@435: duke@435: // Check that if an nmethod ref exists, it has a backlink to this or no backlink at all duke@435: // (could be racing a deopt). duke@435: // Not inline to avoid circular ref. coleenp@4037: bool Method::check_code() const { duke@435: // cached in a register or local. There's a race on the value of the field. duke@435: nmethod *code = (nmethod *)OrderAccess::load_ptr_acquire(&_code); coleenp@4037: return code == NULL || (code->method() == NULL) || (code->method() == (Method*)this && !code->is_osr_method()); duke@435: } duke@435: duke@435: // Install compiled code. Instantly it can execute. coleenp@4037: void Method::set_code(methodHandle mh, nmethod *code) { duke@435: assert( code, "use clear_code to remove code" ); duke@435: assert( mh->check_code(), "" ); duke@435: duke@435: guarantee(mh->adapter() != NULL, "Adapter blob must already exist!"); duke@435: duke@435: // These writes must happen in this order, because the interpreter will duke@435: // directly jump to from_interpreted_entry which jumps to an i2c adapter duke@435: // which jumps to _from_compiled_entry. duke@435: mh->_code = code; // Assign before allowing compiled code to exec duke@435: duke@435: int comp_level = code->comp_level(); duke@435: // In theory there could be a race here. In practice it is unlikely duke@435: // and not worth worrying about. iveresov@2138: if (comp_level > mh->highest_comp_level()) { iveresov@2138: mh->set_highest_comp_level(comp_level); duke@435: } duke@435: duke@435: OrderAccess::storestore(); twisti@2047: #ifdef SHARK twisti@2200: mh->_from_interpreted_entry = code->insts_begin(); twisti@3969: #else //!SHARK duke@435: mh->_from_compiled_entry = code->verified_entry_point(); duke@435: OrderAccess::storestore(); duke@435: // Instantly compiled code can execute. twisti@3969: if (!mh->is_method_handle_intrinsic()) twisti@3969: mh->_from_interpreted_entry = mh->get_i2c_entry(); twisti@3969: #endif //!SHARK duke@435: } duke@435: duke@435: coleenp@4037: bool Method::is_overridden_in(Klass* k) const { coleenp@4037: InstanceKlass* ik = InstanceKlass::cast(k); duke@435: duke@435: if (ik->is_interface()) return false; duke@435: duke@435: // If method is an interface, we skip it - except if it duke@435: // is a miranda method coleenp@4251: if (method_holder()->is_interface()) { duke@435: // Check that method is not a miranda method duke@435: if (ik->lookup_method(name(), signature()) == NULL) { duke@435: // No implementation exist - so miranda method duke@435: return false; duke@435: } duke@435: return true; duke@435: } duke@435: duke@435: assert(ik->is_subclass_of(method_holder()), "should be subklass"); duke@435: assert(ik->vtable() != NULL, "vtable should exist"); duke@435: if (vtable_index() == nonvirtual_vtable_index) { duke@435: return false; duke@435: } else { coleenp@4037: Method* vt_m = ik->method_at_vtable(vtable_index()); coleenp@4037: return vt_m != this; duke@435: } duke@435: } duke@435: duke@435: coleenp@4037: // give advice about whether this Method* should be cached or not coleenp@4037: bool Method::should_not_be_cached() const { dcubed@483: if (is_old()) { dcubed@483: // This method has been redefined. It is either EMCP or obsolete dcubed@483: // and we don't want to cache it because that would pin the method dcubed@483: // down and prevent it from being collectible if and when it dcubed@483: // finishes executing. dcubed@483: return true; dcubed@483: } dcubed@483: dcubed@483: // caching this method should be just fine dcubed@483: return false; dcubed@483: } dcubed@483: jrose@1145: // Constant pool structure for invoke methods: jrose@1145: enum { twisti@3969: _imcp_invoke_name = 1, // utf8: 'invokeExact', etc. coleenp@2497: _imcp_invoke_signature, // utf8: (variable Symbol*) jrose@1145: _imcp_limit jrose@1145: }; jrose@1145: twisti@3969: // Test if this method is an MH adapter frame generated by Java code. twisti@3969: // Cf. java/lang/invoke/InvokerBytecodeGenerator coleenp@4037: bool Method::is_compiled_lambda_form() const { twisti@3969: return intrinsic_id() == vmIntrinsics::_compiledLambdaForm; jrose@1145: } jrose@1145: twisti@3969: // Test if this method is an internal MH primitive method. coleenp@4037: bool Method::is_method_handle_intrinsic() const { twisti@3969: vmIntrinsics::ID iid = intrinsic_id(); twisti@3969: return (MethodHandles::is_signature_polymorphic(iid) && twisti@3969: MethodHandles::is_signature_polymorphic_intrinsic(iid)); jrose@1145: } jrose@1145: coleenp@4037: bool Method::has_member_arg() const { twisti@3969: vmIntrinsics::ID iid = intrinsic_id(); twisti@3969: return (MethodHandles::is_signature_polymorphic(iid) && twisti@3969: MethodHandles::has_member_arg(iid)); twisti@1587: } twisti@1587: twisti@3969: // Make an instance of a signature-polymorphic internal MH primitive. coleenp@4037: methodHandle Method::make_method_handle_intrinsic(vmIntrinsics::ID iid, twisti@3969: Symbol* signature, twisti@3969: TRAPS) { never@3091: ResourceMark rm; jrose@1145: methodHandle empty; jrose@1145: twisti@3969: KlassHandle holder = SystemDictionary::MethodHandle_klass(); twisti@3969: Symbol* name = MethodHandles::signature_polymorphic_intrinsic_name(iid); twisti@3969: assert(iid == MethodHandles::signature_polymorphic_name_id(name), ""); jrose@1145: if (TraceMethodHandles) { twisti@3969: tty->print_cr("make_method_handle_intrinsic MH.%s%s", name->as_C_string(), signature->as_C_string()); jrose@1145: } jrose@1145: jrose@2760: // invariant: cp->symbol_at_put is preceded by a refcount increment (more usually a lookup) jrose@2760: name->increment_refcount(); jrose@2760: signature->increment_refcount(); jrose@2760: twisti@3969: int cp_length = _imcp_limit; coleenp@4037: ClassLoaderData* loader_data = holder->class_loader_data(); jrose@1145: constantPoolHandle cp; jrose@1145: { coleenp@4037: ConstantPool* cp_oop = ConstantPool::allocate(loader_data, cp_length, CHECK_(empty)); jrose@1145: cp = constantPoolHandle(THREAD, cp_oop); jrose@1145: } coleenp@4251: cp->set_pool_holder(InstanceKlass::cast(holder())); coleenp@2497: cp->symbol_at_put(_imcp_invoke_name, name); coleenp@2497: cp->symbol_at_put(_imcp_invoke_signature, signature); coleenp@4490: cp->set_has_preresolution(); jrose@1145: twisti@3969: // decide on access bits: public or not? twisti@3969: int flags_bits = (JVM_ACC_NATIVE | JVM_ACC_SYNTHETIC | JVM_ACC_FINAL); twisti@3969: bool must_be_static = MethodHandles::is_signature_polymorphic_static(iid); twisti@3969: if (must_be_static) flags_bits |= JVM_ACC_STATIC; twisti@3969: assert((flags_bits & JVM_ACC_PUBLIC) == 0, "do not expose these methods"); twisti@3969: jrose@1145: methodHandle m; jrose@1145: { coleenp@4572: InlineTableSizes sizes; coleenp@4398: Method* m_oop = Method::allocate(loader_data, 0, coleenp@4572: accessFlags_from(flags_bits), &sizes, coleenp@4398: ConstMethod::NORMAL, CHECK_(empty)); jrose@1145: m = methodHandle(THREAD, m_oop); jrose@1145: } jrose@1145: m->set_constants(cp()); jrose@1145: m->set_name_index(_imcp_invoke_name); jrose@1145: m->set_signature_index(_imcp_invoke_signature); twisti@3969: assert(MethodHandles::is_signature_polymorphic_name(m->name()), ""); coleenp@2497: assert(m->signature() == signature, ""); jrose@1145: #ifdef CC_INTERP twisti@2563: ResultTypeFinder rtf(signature); jrose@1145: m->set_result_index(rtf.type()); jrose@1145: #endif jrose@1145: m->compute_size_of_parameters(THREAD); jrose@2148: m->init_intrinsic_id(); twisti@3969: assert(m->is_method_handle_intrinsic(), ""); twisti@3969: #ifdef ASSERT twisti@3969: if (!MethodHandles::is_signature_polymorphic(m->intrinsic_id())) m->print(); twisti@3969: assert(MethodHandles::is_signature_polymorphic(m->intrinsic_id()), "must be an invoker"); twisti@3969: assert(m->intrinsic_id() == iid, "correctly predicted iid"); twisti@3969: #endif //ASSERT jrose@1145: jrose@1145: // Finally, set up its entry points. jrose@1145: assert(m->can_be_statically_bound(), ""); coleenp@4037: m->set_vtable_index(Method::nonvirtual_vtable_index); jrose@1145: m->link_method(m, CHECK_(empty)); jrose@1145: jrose@1474: if (TraceMethodHandles && (Verbose || WizardMode)) jrose@1145: m->print_on(tty); jrose@1145: jrose@1145: return m; jrose@1145: } jrose@1145: coleenp@4037: Klass* Method::check_non_bcp_klass(Klass* klass) { hseigel@4278: if (klass != NULL && klass->class_loader() != NULL) { hseigel@4278: if (klass->oop_is_objArray()) coleenp@4142: klass = ObjArrayKlass::cast(klass)->bottom_klass(); jrose@2982: return klass; jrose@2982: } jrose@2982: return NULL; jrose@2982: } jrose@1145: dcubed@483: coleenp@4037: methodHandle Method::clone_with_new_data(methodHandle m, u_char* new_code, int new_code_length, duke@435: u_char* new_compressed_linenumber_table, int new_compressed_linenumber_size, TRAPS) { duke@435: // Code below does not work for native methods - they should never get rewritten anyway duke@435: assert(!m->is_native(), "cannot rewrite native methods"); coleenp@4037: // Allocate new Method* duke@435: AccessFlags flags = m->access_flags(); coleenp@4572: coleenp@4572: ConstMethod* cm = m->constMethod(); coleenp@4572: int checked_exceptions_len = cm->checked_exceptions_length(); coleenp@4572: int localvariable_len = cm->localvariable_table_length(); coleenp@4572: int exception_table_len = cm->exception_table_length(); coleenp@4572: int method_parameters_len = cm->method_parameters_length(); coleenp@4572: int method_annotations_len = cm->method_annotations_length(); coleenp@4572: int parameter_annotations_len = cm->parameter_annotations_length(); coleenp@4572: int type_annotations_len = cm->type_annotations_length(); coleenp@4572: int default_annotations_len = cm->default_annotations_length(); coleenp@4572: coleenp@4572: InlineTableSizes sizes( coleenp@4572: localvariable_len, coleenp@4572: new_compressed_linenumber_size, coleenp@4572: exception_table_len, coleenp@4572: checked_exceptions_len, coleenp@4572: method_parameters_len, coleenp@4572: cm->generic_signature_index(), coleenp@4572: method_annotations_len, coleenp@4572: parameter_annotations_len, coleenp@4572: type_annotations_len, coleenp@4572: default_annotations_len, coleenp@4572: 0); coleenp@4037: kamg@4245: ClassLoaderData* loader_data = m->method_holder()->class_loader_data(); coleenp@4037: Method* newm_oop = Method::allocate(loader_data, kamg@4245: new_code_length, kamg@4245: flags, coleenp@4572: &sizes, kamg@4245: m->method_type(), kamg@4245: CHECK_(methodHandle())); duke@435: methodHandle newm (THREAD, newm_oop); duke@435: int new_method_size = newm->method_size(); jmasa@953: coleenp@4037: // Create a shallow copy of Method part, but be careful to preserve the new ConstMethod* coleenp@4037: ConstMethod* newcm = newm->constMethod(); coleenp@4037: int new_const_method_size = newm->constMethod()->size(); ysr@2533: coleenp@4037: memcpy(newm(), m(), sizeof(Method)); coleenp@4037: coleenp@4037: // Create shallow copy of ConstMethod. coleenp@4037: memcpy(newcm, m->constMethod(), sizeof(ConstMethod)); ysr@2533: duke@435: // Reset correct method/const method, method size, and parameter info duke@435: newm->set_constMethod(newcm); duke@435: newm->constMethod()->set_code_size(new_code_length); duke@435: newm->constMethod()->set_constMethod_size(new_const_method_size); duke@435: newm->set_method_size(new_method_size); duke@435: assert(newm->code_size() == new_code_length, "check"); duke@435: assert(newm->checked_exceptions_length() == checked_exceptions_len, "check"); jiangli@3920: assert(newm->exception_table_length() == exception_table_len, "check"); duke@435: assert(newm->localvariable_table_length() == localvariable_len, "check"); duke@435: // Copy new byte codes duke@435: memcpy(newm->code_base(), new_code, new_code_length); duke@435: // Copy line number table duke@435: if (new_compressed_linenumber_size > 0) { duke@435: memcpy(newm->compressed_linenumber_table(), duke@435: new_compressed_linenumber_table, duke@435: new_compressed_linenumber_size); duke@435: } duke@435: // Copy checked_exceptions duke@435: if (checked_exceptions_len > 0) { duke@435: memcpy(newm->checked_exceptions_start(), duke@435: m->checked_exceptions_start(), duke@435: checked_exceptions_len * sizeof(CheckedExceptionElement)); duke@435: } jiangli@3920: // Copy exception table jiangli@3920: if (exception_table_len > 0) { jiangli@3920: memcpy(newm->exception_table_start(), jiangli@3920: m->exception_table_start(), jiangli@3920: exception_table_len * sizeof(ExceptionTableElement)); jiangli@3920: } duke@435: // Copy local variable number table duke@435: if (localvariable_len > 0) { duke@435: memcpy(newm->localvariable_table_start(), duke@435: m->localvariable_table_start(), duke@435: localvariable_len * sizeof(LocalVariableTableElement)); duke@435: } coleenp@4037: // Copy stackmap table coleenp@4037: if (m->has_stackmap_table()) { coleenp@4037: int code_attribute_length = m->stackmap_data()->length(); coleenp@4037: Array* stackmap_data = coleenp@4037: MetadataFactory::new_array(loader_data, code_attribute_length, 0, CHECK_NULL); coleenp@4037: memcpy((void*)stackmap_data->adr_at(0), coleenp@4037: (void*)m->stackmap_data()->adr_at(0), code_attribute_length); coleenp@4037: newm->set_stackmap_data(stackmap_data); coleenp@4037: } jmasa@953: duke@435: return newm; duke@435: } duke@435: coleenp@4037: vmSymbols::SID Method::klass_id_for_intrinsics(Klass* holder) { duke@435: // if loader is not the default loader (i.e., != NULL), we can't know the intrinsics duke@435: // because we are not loading from core libraries kvn@4205: // exception: the AES intrinsics come from lib/ext/sunjce_provider.jar kvn@4205: // which does not use the class default class loader so we check for its loader here kvn@4205: if ((InstanceKlass::cast(holder)->class_loader() != NULL) && kvn@4205: InstanceKlass::cast(holder)->class_loader()->klass()->name() != vmSymbols::sun_misc_Launcher_ExtClassLoader()) { jrose@1291: return vmSymbols::NO_SID; // regardless of name, no intrinsics here kvn@4205: } duke@435: duke@435: // see if the klass name is well-known: coleenp@4037: Symbol* klass_name = InstanceKlass::cast(holder)->name(); jrose@1291: return vmSymbols::find_sid(klass_name); jrose@1291: } jrose@1291: coleenp@4037: void Method::init_intrinsic_id() { jrose@1291: assert(_intrinsic_id == vmIntrinsics::_none, "do this just once"); jrose@1291: const uintptr_t max_id_uint = right_n_bits((int)(sizeof(_intrinsic_id) * BitsPerByte)); jrose@1291: assert((uintptr_t)vmIntrinsics::ID_LIMIT <= max_id_uint, "else fix size"); jrose@2148: assert(intrinsic_id_size_in_bytes() == sizeof(_intrinsic_id), ""); jrose@1291: jrose@1291: // the klass name is well-known: jrose@1291: vmSymbols::SID klass_id = klass_id_for_intrinsics(method_holder()); jrose@1291: assert(klass_id != vmSymbols::NO_SID, "caller responsibility"); duke@435: duke@435: // ditto for method and signature: duke@435: vmSymbols::SID name_id = vmSymbols::find_sid(name()); twisti@3969: if (klass_id != vmSymbols::VM_SYMBOL_ENUM_NAME(java_lang_invoke_MethodHandle) twisti@3969: && name_id == vmSymbols::NO_SID) twisti@3969: return; duke@435: vmSymbols::SID sig_id = vmSymbols::find_sid(signature()); jrose@2639: if (klass_id != vmSymbols::VM_SYMBOL_ENUM_NAME(java_lang_invoke_MethodHandle) jrose@2148: && sig_id == vmSymbols::NO_SID) return; duke@435: jshort flags = access_flags().as_short(); duke@435: jrose@1291: vmIntrinsics::ID id = vmIntrinsics::find_id(klass_id, name_id, sig_id, flags); jrose@1291: if (id != vmIntrinsics::_none) { jrose@1291: set_intrinsic_id(id); jrose@1291: return; jrose@1291: } jrose@1291: duke@435: // A few slightly irregular cases: duke@435: switch (klass_id) { duke@435: case vmSymbols::VM_SYMBOL_ENUM_NAME(java_lang_StrictMath): duke@435: // Second chance: check in regular Math. duke@435: switch (name_id) { duke@435: case vmSymbols::VM_SYMBOL_ENUM_NAME(min_name): duke@435: case vmSymbols::VM_SYMBOL_ENUM_NAME(max_name): duke@435: case vmSymbols::VM_SYMBOL_ENUM_NAME(sqrt_name): duke@435: // pretend it is the corresponding method in the non-strict class: duke@435: klass_id = vmSymbols::VM_SYMBOL_ENUM_NAME(java_lang_Math); jrose@1291: id = vmIntrinsics::find_id(klass_id, name_id, sig_id, flags); duke@435: break; duke@435: } jrose@1862: break; jrose@1862: jrose@1862: // Signature-polymorphic methods: MethodHandle.invoke*, InvokeDynamic.*. jrose@2639: case vmSymbols::VM_SYMBOL_ENUM_NAME(java_lang_invoke_MethodHandle): twisti@3969: if (!is_native()) break; twisti@3969: id = MethodHandles::signature_polymorphic_name_id(method_holder(), name()); twisti@3969: if (is_static() != MethodHandles::is_signature_polymorphic_static(id)) twisti@3969: id = vmIntrinsics::_none; jrose@1862: break; duke@435: } duke@435: jrose@1291: if (id != vmIntrinsics::_none) { jrose@1291: // Set up its iid. It is an alias method. jrose@1291: set_intrinsic_id(id); jrose@1291: return; jrose@1291: } duke@435: } duke@435: coleenp@4037: // These two methods are static since a GC may move the Method coleenp@4037: bool Method::load_signature_classes(methodHandle m, TRAPS) { twisti@3969: if (THREAD->is_Compiler_thread()) { twisti@3969: // There is nothing useful this routine can do from within the Compile thread. twisti@3969: // Hopefully, the signature contains only well-known classes. twisti@3969: // We could scan for this and return true/false, but the caller won't care. twisti@3969: return false; twisti@3969: } duke@435: bool sig_is_loaded = true; coleenp@4251: Handle class_loader(THREAD, m->method_holder()->class_loader()); coleenp@4251: Handle protection_domain(THREAD, m->method_holder()->protection_domain()); coleenp@2497: ResourceMark rm(THREAD); coleenp@2497: Symbol* signature = m->signature(); duke@435: for(SignatureStream ss(signature); !ss.is_done(); ss.next()) { duke@435: if (ss.is_object()) { coleenp@2497: Symbol* sym = ss.as_symbol(CHECK_(false)); coleenp@2497: Symbol* name = sym; coleenp@4037: Klass* klass = SystemDictionary::resolve_or_null(name, class_loader, duke@435: protection_domain, THREAD); rasbold@539: // We are loading classes eagerly. If a ClassNotFoundException or rasbold@539: // a LinkageError was generated, be sure to ignore it. duke@435: if (HAS_PENDING_EXCEPTION) { never@1577: if (PENDING_EXCEPTION->is_a(SystemDictionary::ClassNotFoundException_klass()) || never@1577: PENDING_EXCEPTION->is_a(SystemDictionary::LinkageError_klass())) { duke@435: CLEAR_PENDING_EXCEPTION; duke@435: } else { duke@435: return false; duke@435: } duke@435: } duke@435: if( klass == NULL) { sig_is_loaded = false; } duke@435: } duke@435: } duke@435: return sig_is_loaded; duke@435: } duke@435: coleenp@4037: bool Method::has_unloaded_classes_in_signature(methodHandle m, TRAPS) { coleenp@4251: Handle class_loader(THREAD, m->method_holder()->class_loader()); coleenp@4251: Handle protection_domain(THREAD, m->method_holder()->protection_domain()); coleenp@2497: ResourceMark rm(THREAD); coleenp@2497: Symbol* signature = m->signature(); duke@435: for(SignatureStream ss(signature); !ss.is_done(); ss.next()) { duke@435: if (ss.type() == T_OBJECT) { coleenp@2497: Symbol* name = ss.as_symbol_or_null(); coleenp@2497: if (name == NULL) return true; coleenp@4037: Klass* klass = SystemDictionary::find(name, class_loader, protection_domain, THREAD); duke@435: if (klass == NULL) return true; duke@435: } duke@435: } duke@435: return false; duke@435: } duke@435: duke@435: // Exposed so field engineers can debug VM coleenp@4037: void Method::print_short_name(outputStream* st) { duke@435: ResourceMark rm; duke@435: #ifdef PRODUCT coleenp@4037: st->print(" %s::", method_holder()->external_name()); duke@435: #else coleenp@4037: st->print(" %s::", method_holder()->internal_name()); duke@435: #endif duke@435: name()->print_symbol_on(st); duke@435: if (WizardMode) signature()->print_symbol_on(st); twisti@3969: else if (MethodHandles::is_signature_polymorphic(intrinsic_id())) twisti@3969: MethodHandles::print_as_basic_type_signature_on(st, signature(), true); duke@435: } duke@435: brutisso@2976: // Comparer for sorting an object array containing coleenp@4037: // Method*s. coleenp@4037: static int method_comparator(Method* a, Method* b) { coleenp@4037: return a->name()->fast_compare(b->name()); kvn@3128: } duke@435: duke@435: // This is only done during class loading, so it is OK to assume method_idnum matches the methods() array coleenp@4572: void Method::sort_methods(Array* methods, bool idempotent) { duke@435: int length = methods->length(); duke@435: if (length > 1) { brutisso@2976: { brutisso@2976: No_Safepoint_Verifier nsv; coleenp@4037: QuickSort::sort(methods->data(), length, method_comparator, idempotent); duke@435: } duke@435: // Reset method ordering duke@435: for (int i = 0; i < length; i++) { coleenp@4037: Method* m = methods->at(i); duke@435: m->set_method_idnum(i); duke@435: } duke@435: } duke@435: } duke@435: duke@435: duke@435: //----------------------------------------------------------------------------------- dcubed@4562: // Non-product code unless JVM/TI needs it duke@435: dcubed@4562: #if !defined(PRODUCT) || INCLUDE_JVMTI duke@435: class SignatureTypePrinter : public SignatureTypeNames { duke@435: private: duke@435: outputStream* _st; duke@435: bool _use_separator; duke@435: duke@435: void type_name(const char* name) { duke@435: if (_use_separator) _st->print(", "); duke@435: _st->print(name); duke@435: _use_separator = true; duke@435: } duke@435: duke@435: public: coleenp@2497: SignatureTypePrinter(Symbol* signature, outputStream* st) : SignatureTypeNames(signature) { duke@435: _st = st; duke@435: _use_separator = false; duke@435: } duke@435: duke@435: void print_parameters() { _use_separator = false; iterate_parameters(); } duke@435: void print_returntype() { _use_separator = false; iterate_returntype(); } duke@435: }; duke@435: duke@435: coleenp@4037: void Method::print_name(outputStream* st) { duke@435: Thread *thread = Thread::current(); duke@435: ResourceMark rm(thread); duke@435: SignatureTypePrinter sig(signature(), st); duke@435: st->print("%s ", is_static() ? "static" : "virtual"); duke@435: sig.print_returntype(); coleenp@4037: st->print(" %s.", method_holder()->internal_name()); duke@435: name()->print_symbol_on(st); duke@435: st->print("("); duke@435: sig.print_parameters(); duke@435: st->print(")"); duke@435: } dcubed@4562: #endif // !PRODUCT || INCLUDE_JVMTI duke@435: duke@435: dcubed@4562: //----------------------------------------------------------------------------------- dcubed@4562: // Non-product code dcubed@4562: dcubed@4562: #ifndef PRODUCT coleenp@4037: void Method::print_codes_on(outputStream* st) const { duke@435: print_codes_on(0, code_size(), st); duke@435: } duke@435: coleenp@4037: void Method::print_codes_on(int from, int to, outputStream* st) const { duke@435: Thread *thread = Thread::current(); duke@435: ResourceMark rm(thread); coleenp@4037: methodHandle mh (thread, (Method*)this); duke@435: BytecodeStream s(mh); duke@435: s.set_interval(from, to); duke@435: BytecodeTracer::set_closure(BytecodeTracer::std_closure()); duke@435: while (s.next() >= 0) BytecodeTracer::trace(mh, s.bcp(), st); duke@435: } duke@435: #endif // not PRODUCT duke@435: duke@435: duke@435: // Simple compression of line number tables. We use a regular compressed stream, except that we compress deltas duke@435: // between (bci,line) pairs since they are smaller. If (bci delta, line delta) fits in (5-bit unsigned, 3-bit unsigned) duke@435: // we save it as one byte, otherwise we write a 0xFF escape character and use regular compression. 0x0 is used duke@435: // as end-of-stream terminator. duke@435: duke@435: void CompressedLineNumberWriteStream::write_pair_regular(int bci_delta, int line_delta) { duke@435: // bci and line number does not compress into single byte. duke@435: // Write out escape character and use regular compression for bci and line number. duke@435: write_byte((jubyte)0xFF); duke@435: write_signed_int(bci_delta); duke@435: write_signed_int(line_delta); duke@435: } duke@435: coleenp@4037: // See comment in method.hpp which explains why this exists. sla@2540: #if defined(_M_AMD64) && _MSC_VER >= 1400 duke@435: #pragma optimize("", off) duke@435: void CompressedLineNumberWriteStream::write_pair(int bci, int line) { duke@435: write_pair_inline(bci, line); duke@435: } duke@435: #pragma optimize("", on) duke@435: #endif duke@435: duke@435: CompressedLineNumberReadStream::CompressedLineNumberReadStream(u_char* buffer) : CompressedReadStream(buffer) { duke@435: _bci = 0; duke@435: _line = 0; duke@435: }; duke@435: duke@435: duke@435: bool CompressedLineNumberReadStream::read_pair() { duke@435: jubyte next = read_byte(); duke@435: // Check for terminator duke@435: if (next == 0) return false; duke@435: if (next == 0xFF) { duke@435: // Escape character, regular compression used duke@435: _bci += read_signed_int(); duke@435: _line += read_signed_int(); duke@435: } else { duke@435: // Single byte compression used duke@435: _bci += next >> 3; duke@435: _line += next & 0x7; duke@435: } duke@435: return true; duke@435: } duke@435: duke@435: coleenp@4037: Bytecodes::Code Method::orig_bytecode_at(int bci) const { coleenp@4251: BreakpointInfo* bp = method_holder()->breakpoints(); duke@435: for (; bp != NULL; bp = bp->next()) { duke@435: if (bp->match(this, bci)) { duke@435: return bp->orig_bytecode(); duke@435: } duke@435: } duke@435: ShouldNotReachHere(); duke@435: return Bytecodes::_shouldnotreachhere; duke@435: } duke@435: coleenp@4037: void Method::set_orig_bytecode_at(int bci, Bytecodes::Code code) { duke@435: assert(code != Bytecodes::_breakpoint, "cannot patch breakpoints this way"); coleenp@4251: BreakpointInfo* bp = method_holder()->breakpoints(); duke@435: for (; bp != NULL; bp = bp->next()) { duke@435: if (bp->match(this, bci)) { duke@435: bp->set_orig_bytecode(code); duke@435: // and continue, in case there is more than one duke@435: } duke@435: } duke@435: } duke@435: coleenp@4037: void Method::set_breakpoint(int bci) { coleenp@4251: InstanceKlass* ik = method_holder(); duke@435: BreakpointInfo *bp = new BreakpointInfo(this, bci); duke@435: bp->set_next(ik->breakpoints()); duke@435: ik->set_breakpoints(bp); duke@435: // do this last: duke@435: bp->set(this); duke@435: } duke@435: coleenp@4037: static void clear_matches(Method* m, int bci) { coleenp@4251: InstanceKlass* ik = m->method_holder(); duke@435: BreakpointInfo* prev_bp = NULL; duke@435: BreakpointInfo* next_bp; duke@435: for (BreakpointInfo* bp = ik->breakpoints(); bp != NULL; bp = next_bp) { duke@435: next_bp = bp->next(); duke@435: // bci value of -1 is used to delete all breakpoints in method m (ex: clear_all_breakpoint). duke@435: if (bci >= 0 ? bp->match(m, bci) : bp->match(m)) { duke@435: // do this first: duke@435: bp->clear(m); duke@435: // unhook it duke@435: if (prev_bp != NULL) duke@435: prev_bp->set_next(next_bp); duke@435: else duke@435: ik->set_breakpoints(next_bp); duke@435: delete bp; duke@435: // When class is redefined JVMTI sets breakpoint in all versions of EMCP methods duke@435: // at same location. So we have multiple matching (method_index and bci) duke@435: // BreakpointInfo nodes in BreakpointInfo list. We should just delete one duke@435: // breakpoint for clear_breakpoint request and keep all other method versions duke@435: // BreakpointInfo for future clear_breakpoint request. duke@435: // bcivalue of -1 is used to clear all breakpoints (see clear_all_breakpoints) duke@435: // which is being called when class is unloaded. We delete all the Breakpoint duke@435: // information for all versions of method. We may not correctly restore the original duke@435: // bytecode in all method versions, but that is ok. Because the class is being unloaded duke@435: // so these methods won't be used anymore. duke@435: if (bci >= 0) { duke@435: break; duke@435: } duke@435: } else { duke@435: // This one is a keeper. duke@435: prev_bp = bp; duke@435: } duke@435: } duke@435: } duke@435: coleenp@4037: void Method::clear_breakpoint(int bci) { duke@435: assert(bci >= 0, ""); duke@435: clear_matches(this, bci); duke@435: } duke@435: coleenp@4037: void Method::clear_all_breakpoints() { duke@435: clear_matches(this, -1); duke@435: } duke@435: duke@435: coleenp@4037: int Method::invocation_count() { iveresov@2138: if (TieredCompilation) { coleenp@4037: MethodData* const mdo = method_data(); iveresov@2138: if (invocation_counter()->carry() || ((mdo != NULL) ? mdo->invocation_counter()->carry() : false)) { iveresov@2138: return InvocationCounter::count_limit; iveresov@2138: } else { iveresov@2138: return invocation_counter()->count() + ((mdo != NULL) ? mdo->invocation_counter()->count() : 0); iveresov@2138: } iveresov@2138: } else { iveresov@2138: return invocation_counter()->count(); iveresov@2138: } iveresov@2138: } iveresov@2138: coleenp@4037: int Method::backedge_count() { iveresov@2138: if (TieredCompilation) { coleenp@4037: MethodData* const mdo = method_data(); iveresov@2138: if (backedge_counter()->carry() || ((mdo != NULL) ? mdo->backedge_counter()->carry() : false)) { iveresov@2138: return InvocationCounter::count_limit; iveresov@2138: } else { iveresov@2138: return backedge_counter()->count() + ((mdo != NULL) ? mdo->backedge_counter()->count() : 0); iveresov@2138: } iveresov@2138: } else { iveresov@2138: return backedge_counter()->count(); iveresov@2138: } iveresov@2138: } iveresov@2138: coleenp@4037: int Method::highest_comp_level() const { coleenp@4037: MethodData* mdo = method_data(); iveresov@2138: if (mdo != NULL) { iveresov@2138: return mdo->highest_comp_level(); iveresov@2138: } else { iveresov@2138: return CompLevel_none; iveresov@2138: } iveresov@2138: } iveresov@2138: coleenp@4037: int Method::highest_osr_comp_level() const { coleenp@4037: MethodData* mdo = method_data(); iveresov@2138: if (mdo != NULL) { iveresov@2138: return mdo->highest_osr_comp_level(); iveresov@2138: } else { iveresov@2138: return CompLevel_none; iveresov@2138: } iveresov@2138: } iveresov@2138: coleenp@4037: void Method::set_highest_comp_level(int level) { coleenp@4037: MethodData* mdo = method_data(); iveresov@2138: if (mdo != NULL) { iveresov@2138: mdo->set_highest_comp_level(level); iveresov@2138: } iveresov@2138: } iveresov@2138: coleenp@4037: void Method::set_highest_osr_comp_level(int level) { coleenp@4037: MethodData* mdo = method_data(); iveresov@2138: if (mdo != NULL) { iveresov@2138: mdo->set_highest_osr_comp_level(level); iveresov@2138: } iveresov@2138: } iveresov@2138: coleenp@4037: BreakpointInfo::BreakpointInfo(Method* m, int bci) { duke@435: _bci = bci; duke@435: _name_index = m->name_index(); duke@435: _signature_index = m->signature_index(); duke@435: _orig_bytecode = (Bytecodes::Code) *m->bcp_from(_bci); duke@435: if (_orig_bytecode == Bytecodes::_breakpoint) duke@435: _orig_bytecode = m->orig_bytecode_at(_bci); duke@435: _next = NULL; duke@435: } duke@435: coleenp@4037: void BreakpointInfo::set(Method* method) { duke@435: #ifdef ASSERT duke@435: { duke@435: Bytecodes::Code code = (Bytecodes::Code) *method->bcp_from(_bci); duke@435: if (code == Bytecodes::_breakpoint) duke@435: code = method->orig_bytecode_at(_bci); duke@435: assert(orig_bytecode() == code, "original bytecode must be the same"); duke@435: } duke@435: #endif duke@435: *method->bcp_from(_bci) = Bytecodes::_breakpoint; duke@435: method->incr_number_of_breakpoints(); duke@435: SystemDictionary::notice_modification(); duke@435: { duke@435: // Deoptimize all dependents on this method duke@435: Thread *thread = Thread::current(); duke@435: HandleMark hm(thread); duke@435: methodHandle mh(thread, method); duke@435: Universe::flush_dependents_on_method(mh); duke@435: } duke@435: } duke@435: coleenp@4037: void BreakpointInfo::clear(Method* method) { duke@435: *method->bcp_from(_bci) = orig_bytecode(); duke@435: assert(method->number_of_breakpoints() > 0, "must not go negative"); duke@435: method->decr_number_of_breakpoints(); duke@435: } coleenp@4037: coleenp@4037: // jmethodID handling coleenp@4037: coleenp@4037: // This is a block allocating object, sort of like JNIHandleBlock, only a coleenp@4037: // lot simpler. There aren't many of these, they aren't long, they are rarely coleenp@4037: // deleted and so we can do some suboptimal things. coleenp@4037: // It's allocated on the CHeap because once we allocate a jmethodID, we can coleenp@4037: // never get rid of it. coleenp@4037: // It would be nice to be able to parameterize the number of methods for coleenp@4037: // the null_class_loader but then we'd have to turn this and ClassLoaderData coleenp@4037: // into templates. coleenp@4037: coleenp@4037: // I feel like this brain dead class should exist somewhere in the STL coleenp@4037: coleenp@4037: class JNIMethodBlock : public CHeapObj { coleenp@4037: enum { number_of_methods = 8 }; coleenp@4037: coleenp@4037: Method* _methods[number_of_methods]; coleenp@4037: int _top; coleenp@4037: JNIMethodBlock* _next; coleenp@4037: public: coleenp@4037: static Method* const _free_method; coleenp@4037: coleenp@4037: JNIMethodBlock() : _next(NULL), _top(0) { coleenp@4037: for (int i = 0; i< number_of_methods; i++) _methods[i] = _free_method; coleenp@4037: } coleenp@4037: coleenp@4037: Method** add_method(Method* m) { coleenp@4037: if (_top < number_of_methods) { coleenp@4037: // top points to the next free entry. coleenp@4037: int i = _top; coleenp@4037: _methods[i] = m; coleenp@4037: _top++; coleenp@4037: return &_methods[i]; coleenp@4037: } else if (_top == number_of_methods) { coleenp@4037: // if the next free entry ran off the block see if there's a free entry coleenp@4037: for (int i = 0; i< number_of_methods; i++) { coleenp@4037: if (_methods[i] == _free_method) { coleenp@4037: _methods[i] = m; coleenp@4037: return &_methods[i]; coleenp@4037: } coleenp@4037: } coleenp@4037: // Only check each block once for frees. They're very unlikely. coleenp@4037: // Increment top past the end of the block. coleenp@4037: _top++; coleenp@4037: } coleenp@4037: // need to allocate a next block. coleenp@4037: if (_next == NULL) { coleenp@4037: _next = new JNIMethodBlock(); coleenp@4037: } coleenp@4037: return _next->add_method(m); coleenp@4037: } coleenp@4037: coleenp@4037: bool contains(Method** m) { coleenp@4037: for (JNIMethodBlock* b = this; b != NULL; b = b->_next) { coleenp@4037: for (int i = 0; i< number_of_methods; i++) { coleenp@4037: if (&(b->_methods[i]) == m) { coleenp@4037: return true; coleenp@4037: } coleenp@4037: } coleenp@4037: } coleenp@4037: return false; // not found coleenp@4037: } coleenp@4037: coleenp@4037: // Doesn't really destroy it, just marks it as free so it can be reused. coleenp@4037: void destroy_method(Method** m) { coleenp@4037: #ifdef ASSERT coleenp@4037: assert(contains(m), "should be a methodID"); coleenp@4037: #endif // ASSERT coleenp@4037: *m = _free_method; coleenp@4037: } coleenp@4037: coleenp@4037: // During class unloading the methods are cleared, which is different coleenp@4037: // than freed. coleenp@4037: void clear_all_methods() { coleenp@4037: for (JNIMethodBlock* b = this; b != NULL; b = b->_next) { coleenp@4037: for (int i = 0; i< number_of_methods; i++) { coleenp@4037: _methods[i] = NULL; coleenp@4037: } coleenp@4037: } coleenp@4037: } coleenp@4037: #ifndef PRODUCT coleenp@4037: int count_methods() { coleenp@4037: // count all allocated methods coleenp@4037: int count = 0; coleenp@4037: for (JNIMethodBlock* b = this; b != NULL; b = b->_next) { coleenp@4037: for (int i = 0; i< number_of_methods; i++) { coleenp@4037: if (_methods[i] != _free_method) count++; coleenp@4037: } coleenp@4037: } coleenp@4037: return count; coleenp@4037: } coleenp@4037: #endif // PRODUCT coleenp@4037: }; coleenp@4037: coleenp@4037: // Something that can't be mistaken for an address or a markOop coleenp@4037: Method* const JNIMethodBlock::_free_method = (Method*)55; coleenp@4037: coleenp@4037: // Add a method id to the jmethod_ids coleenp@4037: jmethodID Method::make_jmethod_id(ClassLoaderData* loader_data, Method* m) { coleenp@4037: ClassLoaderData* cld = loader_data; coleenp@4037: coleenp@4037: if (!SafepointSynchronize::is_at_safepoint()) { coleenp@4037: // Have to add jmethod_ids() to class loader data thread-safely. coleenp@4037: // Also have to add the method to the list safely, which the cld lock coleenp@4037: // protects as well. coleenp@4037: MutexLockerEx ml(cld->metaspace_lock(), Mutex::_no_safepoint_check_flag); coleenp@4037: if (cld->jmethod_ids() == NULL) { coleenp@4037: cld->set_jmethod_ids(new JNIMethodBlock()); coleenp@4037: } coleenp@4037: // jmethodID is a pointer to Method* coleenp@4037: return (jmethodID)cld->jmethod_ids()->add_method(m); coleenp@4037: } else { coleenp@4037: // At safepoint, we are single threaded and can set this. coleenp@4037: if (cld->jmethod_ids() == NULL) { coleenp@4037: cld->set_jmethod_ids(new JNIMethodBlock()); coleenp@4037: } coleenp@4037: // jmethodID is a pointer to Method* coleenp@4037: return (jmethodID)cld->jmethod_ids()->add_method(m); coleenp@4037: } coleenp@4037: } coleenp@4037: coleenp@4037: // Mark a jmethodID as free. This is called when there is a data race in coleenp@4037: // InstanceKlass while creating the jmethodID cache. coleenp@4037: void Method::destroy_jmethod_id(ClassLoaderData* loader_data, jmethodID m) { coleenp@4037: ClassLoaderData* cld = loader_data; coleenp@4037: Method** ptr = (Method**)m; coleenp@4037: assert(cld->jmethod_ids() != NULL, "should have method handles"); coleenp@4037: cld->jmethod_ids()->destroy_method(ptr); coleenp@4037: } coleenp@4037: coleenp@4037: void Method::change_method_associated_with_jmethod_id(jmethodID jmid, Method* new_method) { coleenp@4037: // Can't assert the method_holder is the same because the new method has the coleenp@4037: // scratch method holder. coleenp@4037: assert(resolve_jmethod_id(jmid)->method_holder()->class_loader() coleenp@4037: == new_method->method_holder()->class_loader(), coleenp@4037: "changing to a different class loader"); coleenp@4037: // Just change the method in place, jmethodID pointer doesn't change. coleenp@4037: *((Method**)jmid) = new_method; coleenp@4037: } coleenp@4037: coleenp@4037: bool Method::is_method_id(jmethodID mid) { coleenp@4037: Method* m = resolve_jmethod_id(mid); coleenp@4037: assert(m != NULL, "should be called with non-null method"); coleenp@4251: InstanceKlass* ik = m->method_holder(); coleenp@4037: ClassLoaderData* cld = ik->class_loader_data(); coleenp@4037: if (cld->jmethod_ids() == NULL) return false; coleenp@4037: return (cld->jmethod_ids()->contains((Method**)mid)); coleenp@4037: } coleenp@4037: coleenp@4037: Method* Method::checked_resolve_jmethod_id(jmethodID mid) { coleenp@4037: if (mid == NULL) return NULL; coleenp@4037: Method* o = resolve_jmethod_id(mid); coleenp@4037: if (o == NULL || o == JNIMethodBlock::_free_method || !((Metadata*)o)->is_method()) { coleenp@4037: return NULL; coleenp@4037: } coleenp@4037: return o; coleenp@4037: }; coleenp@4037: coleenp@4037: void Method::set_on_stack(const bool value) { coleenp@4037: // Set both the method itself and its constant pool. The constant pool coleenp@4037: // on stack means some method referring to it is also on the stack. coleenp@4037: _access_flags.set_on_stack(value); coleenp@4037: constants()->set_on_stack(value); coleenp@4037: if (value) MetadataOnStackMark::record(this); coleenp@4037: } coleenp@4037: coleenp@4037: // Called when the class loader is unloaded to make all methods weak. coleenp@4037: void Method::clear_jmethod_ids(ClassLoaderData* loader_data) { coleenp@4037: loader_data->jmethod_ids()->clear_all_methods(); coleenp@4037: } coleenp@4037: coleenp@4295: coleenp@4295: // Check that this pointer is valid by checking that the vtbl pointer matches coleenp@4295: bool Method::is_valid_method() const { coleenp@4295: if (this == NULL) { coleenp@4295: return false; coleenp@4295: } else if (!is_metaspace_object()) { coleenp@4295: return false; coleenp@4295: } else { coleenp@4295: Method m; coleenp@4295: // This assumes that the vtbl pointer is the first word of a C++ object. coleenp@4295: // This assumption is also in universe.cpp patch_klass_vtble coleenp@4295: void* vtbl2 = dereference_vptr((void*)&m); coleenp@4295: void* this_vtbl = dereference_vptr((void*)this); coleenp@4295: return vtbl2 == this_vtbl; coleenp@4295: } coleenp@4295: } coleenp@4295: coleenp@4037: #ifndef PRODUCT coleenp@4037: void Method::print_jmethod_ids(ClassLoaderData* loader_data, outputStream* out) { coleenp@4037: out->print_cr("jni_method_id count = %d", loader_data->jmethod_ids()->count_methods()); coleenp@4037: } coleenp@4037: #endif // PRODUCT coleenp@4037: coleenp@4037: coleenp@4037: // Printing coleenp@4037: coleenp@4037: #ifndef PRODUCT coleenp@4037: coleenp@4037: void Method::print_on(outputStream* st) const { coleenp@4037: ResourceMark rm; coleenp@4037: assert(is_method(), "must be method"); coleenp@4037: st->print_cr(internal_name()); coleenp@4037: // get the effect of PrintOopAddress, always, for methods: coleenp@4037: st->print_cr(" - this oop: "INTPTR_FORMAT, (intptr_t)this); coleenp@4037: st->print (" - method holder: "); method_holder()->print_value_on(st); st->cr(); coleenp@4037: st->print (" - constants: "INTPTR_FORMAT" ", (address)constants()); coleenp@4037: constants()->print_value_on(st); st->cr(); coleenp@4037: st->print (" - access: 0x%x ", access_flags().as_int()); access_flags().print_on(st); st->cr(); coleenp@4037: st->print (" - name: "); name()->print_value_on(st); st->cr(); coleenp@4037: st->print (" - signature: "); signature()->print_value_on(st); st->cr(); coleenp@4037: st->print_cr(" - max stack: %d", max_stack()); coleenp@4037: st->print_cr(" - max locals: %d", max_locals()); coleenp@4037: st->print_cr(" - size of params: %d", size_of_parameters()); coleenp@4037: st->print_cr(" - method size: %d", method_size()); coleenp@4037: if (intrinsic_id() != vmIntrinsics::_none) coleenp@4037: st->print_cr(" - intrinsic id: %d %s", intrinsic_id(), vmIntrinsics::name_at(intrinsic_id())); coleenp@4037: if (highest_comp_level() != CompLevel_none) coleenp@4037: st->print_cr(" - highest level: %d", highest_comp_level()); coleenp@4037: st->print_cr(" - vtable index: %d", _vtable_index); coleenp@4037: st->print_cr(" - i2i entry: " INTPTR_FORMAT, interpreter_entry()); coleenp@4037: st->print( " - adapters: "); coleenp@4037: AdapterHandlerEntry* a = ((Method*)this)->adapter(); coleenp@4037: if (a == NULL) coleenp@4037: st->print_cr(INTPTR_FORMAT, a); coleenp@4037: else coleenp@4037: a->print_adapter_on(st); coleenp@4037: st->print_cr(" - compiled entry " INTPTR_FORMAT, from_compiled_entry()); coleenp@4037: st->print_cr(" - code size: %d", code_size()); coleenp@4037: if (code_size() != 0) { coleenp@4037: st->print_cr(" - code start: " INTPTR_FORMAT, code_base()); coleenp@4037: st->print_cr(" - code end (excl): " INTPTR_FORMAT, code_base() + code_size()); coleenp@4037: } coleenp@4037: if (method_data() != NULL) { coleenp@4037: st->print_cr(" - method data: " INTPTR_FORMAT, (address)method_data()); coleenp@4037: } coleenp@4037: st->print_cr(" - checked ex length: %d", checked_exceptions_length()); coleenp@4037: if (checked_exceptions_length() > 0) { coleenp@4037: CheckedExceptionElement* table = checked_exceptions_start(); coleenp@4037: st->print_cr(" - checked ex start: " INTPTR_FORMAT, table); coleenp@4037: if (Verbose) { coleenp@4037: for (int i = 0; i < checked_exceptions_length(); i++) { coleenp@4037: st->print_cr(" - throws %s", constants()->printable_name_at(table[i].class_cp_index)); coleenp@4037: } coleenp@4037: } coleenp@4037: } coleenp@4037: if (has_linenumber_table()) { coleenp@4037: u_char* table = compressed_linenumber_table(); coleenp@4037: st->print_cr(" - linenumber start: " INTPTR_FORMAT, table); coleenp@4037: if (Verbose) { coleenp@4037: CompressedLineNumberReadStream stream(table); coleenp@4037: while (stream.read_pair()) { coleenp@4037: st->print_cr(" - line %d: %d", stream.line(), stream.bci()); coleenp@4037: } coleenp@4037: } coleenp@4037: } coleenp@4037: st->print_cr(" - localvar length: %d", localvariable_table_length()); coleenp@4037: if (localvariable_table_length() > 0) { coleenp@4037: LocalVariableTableElement* table = localvariable_table_start(); coleenp@4037: st->print_cr(" - localvar start: " INTPTR_FORMAT, table); coleenp@4037: if (Verbose) { coleenp@4037: for (int i = 0; i < localvariable_table_length(); i++) { coleenp@4037: int bci = table[i].start_bci; coleenp@4037: int len = table[i].length; coleenp@4037: const char* name = constants()->printable_name_at(table[i].name_cp_index); coleenp@4037: const char* desc = constants()->printable_name_at(table[i].descriptor_cp_index); coleenp@4037: int slot = table[i].slot; coleenp@4037: st->print_cr(" - %s %s bci=%d len=%d slot=%d", desc, name, bci, len, slot); coleenp@4037: } coleenp@4037: } coleenp@4037: } coleenp@4037: if (code() != NULL) { coleenp@4037: st->print (" - compiled code: "); coleenp@4037: code()->print_value_on(st); coleenp@4037: } coleenp@4037: if (is_native()) { coleenp@4037: st->print_cr(" - native function: " INTPTR_FORMAT, native_function()); coleenp@4037: st->print_cr(" - signature handler: " INTPTR_FORMAT, signature_handler()); coleenp@4037: } coleenp@4037: } coleenp@4037: coleenp@4037: #endif //PRODUCT coleenp@4037: coleenp@4037: void Method::print_value_on(outputStream* st) const { coleenp@4037: assert(is_method(), "must be method"); coleenp@4037: st->print_cr(internal_name()); coleenp@4037: print_address_on(st); coleenp@4037: st->print(" "); coleenp@4037: name()->print_value_on(st); coleenp@4037: st->print(" "); coleenp@4037: signature()->print_value_on(st); coleenp@4037: st->print(" in "); coleenp@4037: method_holder()->print_value_on(st); coleenp@4037: if (WizardMode) st->print("[%d,%d]", size_of_parameters(), max_locals()); coleenp@4037: if (WizardMode && code() != NULL) st->print(" ((nmethod*)%p)", code()); coleenp@4037: } coleenp@4037: acorn@4497: #if INCLUDE_SERVICES acorn@4497: // Size Statistics acorn@4497: void Method::collect_statistics(KlassSizeStats *sz) const { acorn@4497: int mysize = sz->count(this); acorn@4497: sz->_method_bytes += mysize; acorn@4497: sz->_method_all_bytes += mysize; acorn@4497: sz->_rw_bytes += mysize; acorn@4497: acorn@4497: if (constMethod()) { acorn@4497: constMethod()->collect_statistics(sz); acorn@4497: } acorn@4497: if (method_data()) { acorn@4497: method_data()->collect_statistics(sz); acorn@4497: } acorn@4497: } acorn@4497: #endif // INCLUDE_SERVICES coleenp@4037: coleenp@4037: // Verification coleenp@4037: coleenp@4037: void Method::verify_on(outputStream* st) { coleenp@4037: guarantee(is_method(), "object must be method"); coleenp@4037: guarantee(is_metadata(), "should be metadata"); coleenp@4037: guarantee(constants()->is_constantPool(), "should be constant pool"); coleenp@4037: guarantee(constants()->is_metadata(), "should be metadata"); coleenp@4037: guarantee(constMethod()->is_constMethod(), "should be ConstMethod*"); coleenp@4037: guarantee(constMethod()->is_metadata(), "should be metadata"); coleenp@4037: MethodData* md = method_data(); coleenp@4037: guarantee(md == NULL || coleenp@4295: md->is_metadata(), "should be metadata"); coleenp@4037: guarantee(md == NULL || coleenp@4037: md->is_methodData(), "should be method data"); coleenp@4037: }