duke@435: /* jrose@1920: * Copyright 1997-2010 Sun Microsystems, Inc. All Rights Reserved. duke@435: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. duke@435: * duke@435: * This code is free software; you can redistribute it and/or modify it duke@435: * under the terms of the GNU General Public License version 2 only, as duke@435: * published by the Free Software Foundation. duke@435: * duke@435: * This code is distributed in the hope that it will be useful, but WITHOUT duke@435: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or duke@435: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License duke@435: * version 2 for more details (a copy is included in the LICENSE file that duke@435: * accompanied this code). duke@435: * duke@435: * You should have received a copy of the GNU General Public License version duke@435: * 2 along with this work; if not, write to the Free Software Foundation, duke@435: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. duke@435: * duke@435: * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, duke@435: * CA 95054 USA or visit www.sun.com if you need additional information or duke@435: * have any questions. duke@435: * duke@435: */ duke@435: duke@435: #include "incls/_precompiled.incl" duke@435: #include "incls/_interpreter.cpp.incl" 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: duke@435: void InterpreterCodelet::print() { duke@435: if (PrintInterpreter) { duke@435: tty->cr(); duke@435: tty->print_cr("----------------------------------------------------------------------"); duke@435: } duke@435: duke@435: if (description() != NULL) tty->print("%s ", description()); duke@435: if (bytecode() >= 0 ) tty->print("%d %s ", bytecode(), Bytecodes::name(bytecode())); duke@435: tty->print_cr("[" INTPTR_FORMAT ", " INTPTR_FORMAT "] %d bytes", duke@435: code_begin(), code_end(), code_size()); duke@435: duke@435: if (PrintInterpreter) { duke@435: tty->cr(); duke@435: Disassembler::decode(code_begin(), code_end(), tty); 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: VTune::register_stub( duke@435: "Interpreter", duke@435: AbstractInterpreter::code()->code_start(), duke@435: AbstractInterpreter::code()->code_end() duke@435: ); 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: jrose@1145: // Invoker for method handles? jrose@1145: if (m->is_method_handle_invoke()) return method_handle; 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()) { jrose@1145: assert(!m->is_method_handle_invoke(), "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: // Accessor method? duke@435: if (m->is_accessor()) { duke@435: assert(m->size_of_parameters() == 1, "fast code for accessors assumes parameter size = 1"); duke@435: return accessor; 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; 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: 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) { duke@435: address bcp = method->bcp_from(bci); jrose@1920: Bytecodes::Code code = Bytecodes::code_at(bcp, method()); duke@435: jrose@1920: if (!Bytecode_at(bcp)->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; jrose@1145: case method_handle : tty->print("method_handle" ); 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; duke@435: default : ShouldNotReachHere(); duke@435: } duke@435: } duke@435: #endif // PRODUCT duke@435: duke@435: static BasicType constant_pool_type(methodOop method, int index) { duke@435: constantTag tag = method->constants()->tag_at(index); duke@435: if (tag.is_int ()) return T_INT; duke@435: else if (tag.is_float ()) return T_FLOAT; duke@435: else if (tag.is_long ()) return T_LONG; duke@435: else if (tag.is_double ()) return T_DOUBLE; duke@435: else if (tag.is_string ()) return T_OBJECT; duke@435: else if (tag.is_unresolved_string()) return T_OBJECT; duke@435: else if (tag.is_klass ()) return T_OBJECT; duke@435: else if (tag.is_unresolved_klass ()) return T_OBJECT; duke@435: ShouldNotReachHere(); duke@435: return T_ILLEGAL; duke@435: } 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 cfang@1335: address AbstractInterpreter::deopt_continue_after_entry(methodOop method, address bcp, int callee_parameters, bool is_top_frame) { duke@435: assert(method->contains(bcp), "just checkin'"); duke@435: Bytecodes::Code code = Bytecodes::java_code_at(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 duke@435: length = Bytecodes::length_at(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); duke@435: type = Bytecode_invoke_at(mh, bci)->result_type(thread); 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); jrose@1494: type = Bytecode_invoke_at(mh, bci)->result_type(thread); 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); twisti@1570: method->constants()->cache()->secondary_entry_at(index)->set_parameter_size(callee_parameters); jrose@1494: } jrose@1494: break; jrose@1494: } jrose@1494: duke@435: case Bytecodes::_ldc : duke@435: type = constant_pool_type( method, *(bcp+1) ); duke@435: break; duke@435: duke@435: case Bytecodes::_ldc_w : // fall through duke@435: case Bytecodes::_ldc2_w: duke@435: type = constant_pool_type( method, Bytes::get_Java_u2(bcp+1) ); duke@435: break; 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 cfang@1335: address AbstractInterpreter::deopt_reexecute_entry(methodOop method, address bcp) { cfang@1335: assert(method->contains(bcp), "just checkin'"); cfang@1335: Bytecodes::Code code = Bytecodes::java_code_at(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: }