duke@435: /* coleenp@4037: * Copyright (c) 1997, 2012, Oracle and/or its affiliates. All rights reserved. duke@435: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. duke@435: * duke@435: * This code is free software; you can redistribute it and/or modify it duke@435: * under the terms of the GNU General Public License version 2 only, as duke@435: * published by the Free Software Foundation. duke@435: * duke@435: * This code is distributed in the hope that it will be useful, but WITHOUT duke@435: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or duke@435: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License duke@435: * version 2 for more details (a copy is included in the LICENSE file that duke@435: * accompanied this code). duke@435: * duke@435: * You should have received a copy of the GNU General Public License version duke@435: * 2 along with this work; if not, write to the Free Software Foundation, duke@435: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. duke@435: * trims@1907: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA trims@1907: * or visit www.oracle.com if you need additional information or have any trims@1907: * questions. duke@435: * duke@435: */ duke@435: stefank@2314: #include "precompiled.hpp" stefank@2314: #include "asm/assembler.hpp" stefank@2314: #include "interpreter/bytecodeHistogram.hpp" stefank@2314: #include "interpreter/bytecodeInterpreter.hpp" stefank@2314: #include "interpreter/interpreter.hpp" stefank@2314: #include "interpreter/interpreterRuntime.hpp" stefank@2314: #include "interpreter/templateTable.hpp" stefank@2314: #include "memory/allocation.inline.hpp" stefank@2314: #include "memory/resourceArea.hpp" stefank@2314: #include "oops/arrayOop.hpp" coleenp@4037: #include "oops/methodData.hpp" coleenp@4037: #include "oops/method.hpp" stefank@2314: #include "oops/oop.inline.hpp" stefank@2314: #include "prims/forte.hpp" stefank@2314: #include "prims/jvmtiExport.hpp" twisti@3969: #include "prims/methodHandles.hpp" stefank@2314: #include "runtime/handles.inline.hpp" stefank@2314: #include "runtime/sharedRuntime.hpp" stefank@2314: #include "runtime/stubRoutines.hpp" stefank@2314: #include "runtime/timer.hpp" duke@435: duke@435: # define __ _masm-> duke@435: duke@435: duke@435: //------------------------------------------------------------------------------------------------------------------------ duke@435: // Implementation of InterpreterCodelet duke@435: duke@435: void InterpreterCodelet::initialize(const char* description, Bytecodes::Code bytecode) { duke@435: _description = description; duke@435: _bytecode = bytecode; duke@435: } duke@435: duke@435: duke@435: void InterpreterCodelet::verify() { duke@435: } duke@435: duke@435: bobv@2036: void InterpreterCodelet::print_on(outputStream* st) const { kvn@4107: ttyLocker ttyl; kvn@4107: duke@435: if (PrintInterpreter) { bobv@2036: st->cr(); bobv@2036: st->print_cr("----------------------------------------------------------------------"); duke@435: } duke@435: bobv@2036: if (description() != NULL) st->print("%s ", description()); bobv@2036: if (bytecode() >= 0 ) st->print("%d %s ", bytecode(), Bytecodes::name(bytecode())); bobv@2036: st->print_cr("[" INTPTR_FORMAT ", " INTPTR_FORMAT "] %d bytes", duke@435: code_begin(), code_end(), code_size()); duke@435: duke@435: if (PrintInterpreter) { bobv@2036: st->cr(); kvn@4107: Disassembler::decode(code_begin(), code_end(), st, DEBUG_ONLY(_comments) NOT_DEBUG(CodeComments())); duke@435: } duke@435: } duke@435: duke@435: duke@435: //------------------------------------------------------------------------------------------------------------------------ duke@435: // Implementation of platform independent aspects of Interpreter duke@435: duke@435: void AbstractInterpreter::initialize() { duke@435: if (_code != NULL) return; duke@435: duke@435: // make sure 'imported' classes are initialized duke@435: if (CountBytecodes || TraceBytecodes || StopInterpreterAt) BytecodeCounter::reset(); duke@435: if (PrintBytecodeHistogram) BytecodeHistogram::reset(); duke@435: if (PrintBytecodePairHistogram) BytecodePairHistogram::reset(); duke@435: duke@435: InvocationCounter::reinitialize(DelayCompilationDuringStartup); duke@435: duke@435: } duke@435: duke@435: void AbstractInterpreter::print() { duke@435: tty->cr(); duke@435: tty->print_cr("----------------------------------------------------------------------"); duke@435: tty->print_cr("Interpreter"); duke@435: tty->cr(); duke@435: tty->print_cr("code size = %6dK bytes", (int)_code->used_space()/1024); duke@435: tty->print_cr("total space = %6dK bytes", (int)_code->total_space()/1024); duke@435: tty->print_cr("wasted space = %6dK bytes", (int)_code->available_space()/1024); duke@435: tty->cr(); duke@435: tty->print_cr("# of codelets = %6d" , _code->number_of_stubs()); duke@435: tty->print_cr("avg codelet size = %6d bytes", _code->used_space() / _code->number_of_stubs()); duke@435: tty->cr(); duke@435: _code->print(); duke@435: tty->print_cr("----------------------------------------------------------------------"); duke@435: tty->cr(); duke@435: } duke@435: duke@435: duke@435: void interpreter_init() { duke@435: Interpreter::initialize(); duke@435: #ifndef PRODUCT duke@435: if (TraceBytecodes) BytecodeTracer::set_closure(BytecodeTracer::std_closure()); duke@435: #endif // PRODUCT duke@435: // need to hit every safepoint in order to call zapping routine duke@435: // register the interpreter duke@435: Forte::register_stub( duke@435: "Interpreter", duke@435: AbstractInterpreter::code()->code_start(), duke@435: AbstractInterpreter::code()->code_end() duke@435: ); duke@435: duke@435: // notify JVMTI profiler duke@435: if (JvmtiExport::should_post_dynamic_code_generated()) { duke@435: JvmtiExport::post_dynamic_code_generated("Interpreter", duke@435: AbstractInterpreter::code()->code_start(), duke@435: AbstractInterpreter::code()->code_end()); duke@435: } duke@435: } duke@435: duke@435: //------------------------------------------------------------------------------------------------------------------------ duke@435: // Implementation of interpreter duke@435: duke@435: StubQueue* AbstractInterpreter::_code = NULL; duke@435: bool AbstractInterpreter::_notice_safepoints = false; duke@435: address AbstractInterpreter::_rethrow_exception_entry = NULL; duke@435: duke@435: address AbstractInterpreter::_native_entry_begin = NULL; duke@435: address AbstractInterpreter::_native_entry_end = NULL; duke@435: address AbstractInterpreter::_slow_signature_handler; duke@435: address AbstractInterpreter::_entry_table [AbstractInterpreter::number_of_method_entries]; duke@435: address AbstractInterpreter::_native_abi_to_tosca [AbstractInterpreter::number_of_result_handlers]; duke@435: duke@435: //------------------------------------------------------------------------------------------------------------------------ duke@435: // Generation of complete interpreter duke@435: duke@435: AbstractInterpreterGenerator::AbstractInterpreterGenerator(StubQueue* _code) { duke@435: _masm = NULL; duke@435: } duke@435: duke@435: duke@435: static const BasicType types[Interpreter::number_of_result_handlers] = { duke@435: T_BOOLEAN, duke@435: T_CHAR , duke@435: T_BYTE , duke@435: T_SHORT , duke@435: T_INT , duke@435: T_LONG , duke@435: T_VOID , duke@435: T_FLOAT , duke@435: T_DOUBLE , duke@435: T_OBJECT duke@435: }; duke@435: duke@435: void AbstractInterpreterGenerator::generate_all() { duke@435: duke@435: duke@435: { CodeletMark cm(_masm, "slow signature handler"); duke@435: Interpreter::_slow_signature_handler = generate_slow_signature_handler(); duke@435: } duke@435: duke@435: } duke@435: duke@435: //------------------------------------------------------------------------------------------------------------------------ duke@435: // Entry points duke@435: duke@435: AbstractInterpreter::MethodKind AbstractInterpreter::method_kind(methodHandle m) { duke@435: // Abstract method? duke@435: if (m->is_abstract()) return abstract; duke@435: twisti@3969: // Method handle primitive? twisti@3969: if (m->is_method_handle_intrinsic()) { twisti@3969: vmIntrinsics::ID id = m->intrinsic_id(); twisti@3969: assert(MethodHandles::is_signature_polymorphic(id), "must match an intrinsic"); twisti@3969: MethodKind kind = (MethodKind)( method_handle_invoke_FIRST + twisti@3969: ((int)id - vmIntrinsics::FIRST_MH_SIG_POLY) ); twisti@3969: assert(kind <= method_handle_invoke_LAST, "parallel enum ranges"); twisti@3969: return kind; twisti@3969: } jrose@1145: duke@435: // Native method? duke@435: // Note: This test must come _before_ the test for intrinsic duke@435: // methods. See also comments below. duke@435: if (m->is_native()) { twisti@3969: assert(!m->is_method_handle_intrinsic(), "overlapping bits here, watch out"); duke@435: return m->is_synchronized() ? native_synchronized : native; duke@435: } duke@435: duke@435: // Synchronized? duke@435: if (m->is_synchronized()) { duke@435: return zerolocals_synchronized; duke@435: } duke@435: duke@435: if (RegisterFinalizersAtInit && m->code_size() == 1 && duke@435: m->intrinsic_id() == vmIntrinsics::_Object_init) { duke@435: // We need to execute the special return bytecode to check for duke@435: // finalizer registration so create a normal frame. duke@435: return zerolocals; duke@435: } duke@435: duke@435: // Empty method? duke@435: if (m->is_empty_method()) { duke@435: return empty; duke@435: } duke@435: duke@435: // Special intrinsic method? duke@435: // Note: This test must come _after_ the test for native methods, duke@435: // otherwise we will run into problems with JDK 1.2, see also duke@435: // AbstractInterpreterGenerator::generate_method_entry() for duke@435: // for details. duke@435: switch (m->intrinsic_id()) { duke@435: case vmIntrinsics::_dsin : return java_lang_math_sin ; duke@435: case vmIntrinsics::_dcos : return java_lang_math_cos ; duke@435: case vmIntrinsics::_dtan : return java_lang_math_tan ; duke@435: case vmIntrinsics::_dabs : return java_lang_math_abs ; duke@435: case vmIntrinsics::_dsqrt : return java_lang_math_sqrt ; duke@435: case vmIntrinsics::_dlog : return java_lang_math_log ; duke@435: case vmIntrinsics::_dlog10: return java_lang_math_log10; roland@3787: case vmIntrinsics::_dpow : return java_lang_math_pow ; roland@3787: case vmIntrinsics::_dexp : return java_lang_math_exp ; johnc@2781: johnc@2781: case vmIntrinsics::_Reference_get: johnc@2781: return java_lang_ref_reference_get; johnc@2781: } johnc@2781: johnc@2781: // Accessor method? johnc@2781: if (m->is_accessor()) { johnc@2781: assert(m->size_of_parameters() == 1, "fast code for accessors assumes parameter size = 1"); johnc@2781: return accessor; duke@435: } duke@435: duke@435: // Note: for now: zero locals for all non-empty methods duke@435: return zerolocals; duke@435: } duke@435: duke@435: twisti@3969: void AbstractInterpreter::set_entry_for_kind(AbstractInterpreter::MethodKind kind, address entry) { twisti@3969: assert(kind >= method_handle_invoke_FIRST && twisti@3969: kind <= method_handle_invoke_LAST, "late initialization only for MH entry points"); twisti@3969: assert(_entry_table[kind] == _entry_table[abstract], "previous value must be AME entry"); twisti@3969: _entry_table[kind] = entry; twisti@3969: } twisti@3969: twisti@3969: duke@435: // Return true if the interpreter can prove that the given bytecode has duke@435: // not yet been executed (in Java semantics, not in actual operation). duke@435: bool AbstractInterpreter::is_not_reached(methodHandle method, int bci) { never@2462: Bytecodes::Code code = method()->code_at(bci); duke@435: never@2462: if (!Bytecodes::must_rewrite(code)) { duke@435: // might have been reached duke@435: return false; duke@435: } duke@435: duke@435: // the bytecode might not be rewritten if the method is an accessor, etc. duke@435: address ientry = method->interpreter_entry(); duke@435: if (ientry != entry_for_kind(AbstractInterpreter::zerolocals) && duke@435: ientry != entry_for_kind(AbstractInterpreter::zerolocals_synchronized)) duke@435: return false; // interpreter does not run this method! duke@435: duke@435: // otherwise, we can be sure this bytecode has never been executed duke@435: return true; duke@435: } duke@435: duke@435: duke@435: #ifndef PRODUCT duke@435: void AbstractInterpreter::print_method_kind(MethodKind kind) { duke@435: switch (kind) { duke@435: case zerolocals : tty->print("zerolocals" ); break; duke@435: case zerolocals_synchronized: tty->print("zerolocals_synchronized"); break; duke@435: case native : tty->print("native" ); break; duke@435: case native_synchronized : tty->print("native_synchronized" ); break; duke@435: case empty : tty->print("empty" ); break; duke@435: case accessor : tty->print("accessor" ); break; duke@435: case abstract : tty->print("abstract" ); break; duke@435: case java_lang_math_sin : tty->print("java_lang_math_sin" ); break; duke@435: case java_lang_math_cos : tty->print("java_lang_math_cos" ); break; duke@435: case java_lang_math_tan : tty->print("java_lang_math_tan" ); break; duke@435: case java_lang_math_abs : tty->print("java_lang_math_abs" ); break; duke@435: case java_lang_math_sqrt : tty->print("java_lang_math_sqrt" ); break; duke@435: case java_lang_math_log : tty->print("java_lang_math_log" ); break; duke@435: case java_lang_math_log10 : tty->print("java_lang_math_log10" ); break; twisti@3969: default: twisti@3969: if (kind >= method_handle_invoke_FIRST && twisti@3969: kind <= method_handle_invoke_LAST) { twisti@3969: const char* kind_name = vmIntrinsics::name_at(method_handle_intrinsic(kind)); twisti@3969: if (kind_name[0] == '_') kind_name = &kind_name[1]; // '_invokeExact' => 'invokeExact' twisti@3969: tty->print("method_handle_%s", kind_name); twisti@3969: break; twisti@3969: } twisti@3969: ShouldNotReachHere(); twisti@3969: break; duke@435: } duke@435: } duke@435: #endif // PRODUCT duke@435: duke@435: duke@435: //------------------------------------------------------------------------------------------------------------------------ duke@435: // Deoptimization support duke@435: cfang@1335: // If deoptimization happens, this function returns the point of next bytecode to continue execution coleenp@4037: address AbstractInterpreter::deopt_continue_after_entry(Method* method, address bcp, int callee_parameters, bool is_top_frame) { duke@435: assert(method->contains(bcp), "just checkin'"); never@2462: Bytecodes::Code code = Bytecodes::java_code_at(method, bcp); cfang@1335: assert(!Interpreter::bytecode_should_reexecute(code), "should not reexecute"); duke@435: int bci = method->bci_from(bcp); duke@435: int length = -1; // initial value for debugging duke@435: // compute continuation length never@2462: length = Bytecodes::length_at(method, bcp); duke@435: // compute result type duke@435: BasicType type = T_ILLEGAL; cfang@1335: duke@435: switch (code) { duke@435: case Bytecodes::_invokevirtual : duke@435: case Bytecodes::_invokespecial : duke@435: case Bytecodes::_invokestatic : duke@435: case Bytecodes::_invokeinterface: { duke@435: Thread *thread = Thread::current(); duke@435: ResourceMark rm(thread); duke@435: methodHandle mh(thread, method); coleenp@2497: type = Bytecode_invoke(mh, bci).result_type(); duke@435: // since the cache entry might not be initialized: duke@435: // (NOT needed for the old calling convension) duke@435: if (!is_top_frame) { duke@435: int index = Bytes::get_native_u2(bcp+1); duke@435: method->constants()->cache()->entry_at(index)->set_parameter_size(callee_parameters); duke@435: } duke@435: break; duke@435: } duke@435: jrose@1494: case Bytecodes::_invokedynamic: { jrose@1494: Thread *thread = Thread::current(); jrose@1494: ResourceMark rm(thread); jrose@1494: methodHandle mh(thread, method); coleenp@2497: type = Bytecode_invoke(mh, bci).result_type(); jrose@1494: // since the cache entry might not be initialized: jrose@1494: // (NOT needed for the old calling convension) jrose@1494: if (!is_top_frame) { jrose@1494: int index = Bytes::get_native_u4(bcp+1); coleenp@4037: method->constants()->invokedynamic_cp_cache_entry_at(index)->set_parameter_size(callee_parameters); jrose@1494: } jrose@1494: break; jrose@1494: } jrose@1494: duke@435: case Bytecodes::_ldc : duke@435: case Bytecodes::_ldc_w : // fall through duke@435: case Bytecodes::_ldc2_w: jrose@1957: { jrose@1957: Thread *thread = Thread::current(); jrose@1957: ResourceMark rm(thread); jrose@1957: methodHandle mh(thread, method); never@2462: type = Bytecode_loadconstant(mh, bci).result_type(); jrose@1957: break; jrose@1957: } duke@435: duke@435: default: duke@435: type = Bytecodes::result_type(code); duke@435: break; duke@435: } duke@435: duke@435: // return entry point for computed continuation state & bytecode length duke@435: return duke@435: is_top_frame duke@435: ? Interpreter::deopt_entry (as_TosState(type), length) duke@435: : Interpreter::return_entry(as_TosState(type), length); duke@435: } duke@435: cfang@1335: // If deoptimization happens, this function returns the point where the interpreter reexecutes cfang@1335: // the bytecode. cfang@1335: // Note: Bytecodes::_athrow is a special case in that it does not return cfang@1335: // Interpreter::deopt_entry(vtos, 0) like others coleenp@4037: address AbstractInterpreter::deopt_reexecute_entry(Method* method, address bcp) { cfang@1335: assert(method->contains(bcp), "just checkin'"); never@2462: Bytecodes::Code code = Bytecodes::java_code_at(method, bcp); cfang@1335: #ifdef COMPILER1 cfang@1335: if(code == Bytecodes::_athrow ) { cfang@1335: return Interpreter::rethrow_exception_entry(); cfang@1335: } cfang@1335: #endif /* COMPILER1 */ cfang@1335: return Interpreter::deopt_entry(vtos, 0); cfang@1335: } cfang@1335: cfang@1335: // If deoptimization happens, the interpreter should reexecute these bytecodes. cfang@1335: // This function mainly helps the compilers to set up the reexecute bit. cfang@1335: bool AbstractInterpreter::bytecode_should_reexecute(Bytecodes::Code code) { cfang@1335: switch (code) { cfang@1335: case Bytecodes::_lookupswitch: cfang@1335: case Bytecodes::_tableswitch: cfang@1335: case Bytecodes::_fast_binaryswitch: cfang@1335: case Bytecodes::_fast_linearswitch: cfang@1335: // recompute condtional expression folded into _if cfang@1335: case Bytecodes::_lcmp : cfang@1335: case Bytecodes::_fcmpl : cfang@1335: case Bytecodes::_fcmpg : cfang@1335: case Bytecodes::_dcmpl : cfang@1335: case Bytecodes::_dcmpg : cfang@1335: case Bytecodes::_ifnull : cfang@1335: case Bytecodes::_ifnonnull : cfang@1335: case Bytecodes::_goto : cfang@1335: case Bytecodes::_goto_w : cfang@1335: case Bytecodes::_ifeq : cfang@1335: case Bytecodes::_ifne : cfang@1335: case Bytecodes::_iflt : cfang@1335: case Bytecodes::_ifge : cfang@1335: case Bytecodes::_ifgt : cfang@1335: case Bytecodes::_ifle : cfang@1335: case Bytecodes::_if_icmpeq : cfang@1335: case Bytecodes::_if_icmpne : cfang@1335: case Bytecodes::_if_icmplt : cfang@1335: case Bytecodes::_if_icmpge : cfang@1335: case Bytecodes::_if_icmpgt : cfang@1335: case Bytecodes::_if_icmple : cfang@1335: case Bytecodes::_if_acmpeq : cfang@1335: case Bytecodes::_if_acmpne : cfang@1335: // special cases cfang@1335: case Bytecodes::_getfield : cfang@1335: case Bytecodes::_putfield : cfang@1335: case Bytecodes::_getstatic : cfang@1335: case Bytecodes::_putstatic : cfang@1335: case Bytecodes::_aastore : cfang@1335: #ifdef COMPILER1 cfang@1335: //special case of reexecution cfang@1335: case Bytecodes::_athrow : cfang@1335: #endif cfang@1335: return true; cfang@1335: cfang@1335: default: cfang@1335: return false; cfang@1335: } cfang@1335: } cfang@1335: duke@435: void AbstractInterpreterGenerator::bang_stack_shadow_pages(bool native_call) { duke@435: // Quick & dirty stack overflow checking: bang the stack & handle trap. duke@435: // Note that we do the banging after the frame is setup, since the exception duke@435: // handling code expects to find a valid interpreter frame on the stack. duke@435: // Doing the banging earlier fails if the caller frame is not an interpreter duke@435: // frame. duke@435: // (Also, the exception throwing code expects to unlock any synchronized duke@435: // method receiever, so do the banging after locking the receiver.) duke@435: duke@435: // Bang each page in the shadow zone. We can't assume it's been done for duke@435: // an interpreter frame with greater than a page of locals, so each page duke@435: // needs to be checked. Only true for non-native. duke@435: if (UseStackBanging) { duke@435: const int start_page = native_call ? StackShadowPages : 1; duke@435: const int page_size = os::vm_page_size(); duke@435: for (int pages = start_page; pages <= StackShadowPages ; pages++) { duke@435: __ bang_stack_with_offset(pages*page_size); duke@435: } duke@435: } duke@435: }