Path: blob/master/src/hotspot/share/interpreter/abstractInterpreter.cpp
40949 views
/*1* Copyright (c) 1997, 2021, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*22*/2324#include "precompiled.hpp"25#include "asm/macroAssembler.hpp"26#include "asm/macroAssembler.inline.hpp"27#include "cds/metaspaceShared.hpp"28#include "compiler/disassembler.hpp"29#include "interpreter/bytecodeHistogram.hpp"30#include "interpreter/bytecodeStream.hpp"31#include "interpreter/interpreter.hpp"32#include "interpreter/interpreterRuntime.hpp"33#include "interpreter/interp_masm.hpp"34#include "interpreter/templateTable.hpp"35#include "memory/allocation.inline.hpp"36#include "memory/resourceArea.hpp"37#include "oops/arrayOop.hpp"38#include "oops/constantPool.hpp"39#include "oops/cpCache.inline.hpp"40#include "oops/methodData.hpp"41#include "oops/method.inline.hpp"42#include "oops/oop.inline.hpp"43#include "prims/forte.hpp"44#include "prims/jvmtiExport.hpp"45#include "prims/methodHandles.hpp"46#include "runtime/handles.inline.hpp"47#include "runtime/sharedRuntime.hpp"48#include "runtime/stubRoutines.hpp"49#include "runtime/timer.hpp"5051# define __ _masm->5253//------------------------------------------------------------------------------------------------------------------------54// Implementation of platform independent aspects of Interpreter5556void AbstractInterpreter::initialize() {57// make sure 'imported' classes are initialized58if (CountBytecodes || TraceBytecodes || StopInterpreterAt) BytecodeCounter::reset();59if (PrintBytecodeHistogram) BytecodeHistogram::reset();60if (PrintBytecodePairHistogram) BytecodePairHistogram::reset();61}6263void AbstractInterpreter::print() {64tty->cr();65tty->print_cr("----------------------------------------------------------------------");66tty->print_cr("Interpreter");67tty->cr();68tty->print_cr("code size = %6dK bytes", (int)_code->used_space()/1024);69tty->print_cr("total space = %6dK bytes", (int)_code->total_space()/1024);70tty->print_cr("wasted space = %6dK bytes", (int)_code->available_space()/1024);71tty->cr();72tty->print_cr("# of codelets = %6d" , _code->number_of_stubs());73if (_code->number_of_stubs() != 0) {74tty->print_cr("avg codelet size = %6d bytes", _code->used_space() / _code->number_of_stubs());75tty->cr();76}77_code->print();78tty->print_cr("----------------------------------------------------------------------");79tty->cr();80}818283//------------------------------------------------------------------------------------------------------------------------84// Implementation of interpreter8586StubQueue* AbstractInterpreter::_code = NULL;87bool AbstractInterpreter::_notice_safepoints = false;88address AbstractInterpreter::_rethrow_exception_entry = NULL;8990address AbstractInterpreter::_native_entry_begin = NULL;91address AbstractInterpreter::_native_entry_end = NULL;92address AbstractInterpreter::_slow_signature_handler;93address AbstractInterpreter::_entry_table [AbstractInterpreter::number_of_method_entries];94address AbstractInterpreter::_native_abi_to_tosca [AbstractInterpreter::number_of_result_handlers];9596//------------------------------------------------------------------------------------------------------------------------97// Generation of complete interpreter9899AbstractInterpreterGenerator::AbstractInterpreterGenerator(StubQueue* _code) {100_masm = NULL;101}102103104//------------------------------------------------------------------------------------------------------------------------105// Entry points106107AbstractInterpreter::MethodKind AbstractInterpreter::method_kind(const methodHandle& m) {108// Abstract method?109if (m->is_abstract()) return abstract;110111// Method handle primitive?112vmIntrinsics::ID iid = m->intrinsic_id();113if (iid != vmIntrinsics::_none) {114if (m->is_method_handle_intrinsic()) {115assert(MethodHandles::is_signature_polymorphic(iid), "must match an intrinsic");116MethodKind kind = (MethodKind)(method_handle_invoke_FIRST +117vmIntrinsics::as_int(iid) -118static_cast<int>(vmIntrinsics::FIRST_MH_SIG_POLY));119assert(kind <= method_handle_invoke_LAST, "parallel enum ranges");120return kind;121}122123switch (iid) {124#ifndef ZERO125// Use optimized stub code for CRC32 native methods.126case vmIntrinsics::_updateCRC32: return java_util_zip_CRC32_update;127case vmIntrinsics::_updateBytesCRC32: return java_util_zip_CRC32_updateBytes;128case vmIntrinsics::_updateByteBufferCRC32: return java_util_zip_CRC32_updateByteBuffer;129// Use optimized stub code for CRC32C methods.130case vmIntrinsics::_updateBytesCRC32C: return java_util_zip_CRC32C_updateBytes;131case vmIntrinsics::_updateDirectByteBufferCRC32C: return java_util_zip_CRC32C_updateDirectByteBuffer;132case vmIntrinsics::_intBitsToFloat: return java_lang_Float_intBitsToFloat;133case vmIntrinsics::_floatToRawIntBits: return java_lang_Float_floatToRawIntBits;134case vmIntrinsics::_longBitsToDouble: return java_lang_Double_longBitsToDouble;135case vmIntrinsics::_doubleToRawLongBits: return java_lang_Double_doubleToRawLongBits;136#endif // ZERO137case vmIntrinsics::_dsin: return java_lang_math_sin;138case vmIntrinsics::_dcos: return java_lang_math_cos;139case vmIntrinsics::_dtan: return java_lang_math_tan;140case vmIntrinsics::_dabs: return java_lang_math_abs;141case vmIntrinsics::_dlog: return java_lang_math_log;142case vmIntrinsics::_dlog10: return java_lang_math_log10;143case vmIntrinsics::_dpow: return java_lang_math_pow;144case vmIntrinsics::_dexp: return java_lang_math_exp;145case vmIntrinsics::_fmaD: return java_lang_math_fmaD;146case vmIntrinsics::_fmaF: return java_lang_math_fmaF;147case vmIntrinsics::_Reference_get: return java_lang_ref_reference_get;148case vmIntrinsics::_dsqrt:149// _dsqrt will be selected for both Math::sqrt and StrictMath::sqrt, but the latter150// is native. Keep treating it like a native method in the interpreter151assert(m->name() == vmSymbols::sqrt_name() &&152(m->klass_name() == vmSymbols::java_lang_Math() ||153m->klass_name() == vmSymbols::java_lang_StrictMath()), "must be");154return m->is_native() ? native : java_lang_math_sqrt;155case vmIntrinsics::_Object_init:156if (RegisterFinalizersAtInit && m->code_size() == 1) {157// We need to execute the special return bytecode to check for158// finalizer registration so create a normal frame.159return zerolocals;160}161break;162default: break;163}164}165166// Native method?167if (m->is_native()) {168assert(!m->is_method_handle_intrinsic(), "overlapping bits here, watch out");169return m->is_synchronized() ? native_synchronized : native;170}171172// Synchronized?173if (m->is_synchronized()) {174return zerolocals_synchronized;175}176177// Empty method?178if (m->is_empty_method()) {179return empty;180}181182// Getter method?183if (m->is_getter()) {184return getter;185}186187// Setter method?188if (m->is_setter()) {189return setter;190}191192// Note: for now: zero locals for all non-empty methods193return zerolocals;194}195196void AbstractInterpreter::set_entry_for_kind(AbstractInterpreter::MethodKind kind, address entry) {197assert(kind >= method_handle_invoke_FIRST &&198kind <= method_handle_invoke_LAST, "late initialization only for MH entry points");199assert(_entry_table[kind] == _entry_table[abstract], "previous value must be AME entry");200_entry_table[kind] = entry;201}202203// Return true if the interpreter can prove that the given bytecode has204// not yet been executed (in Java semantics, not in actual operation).205bool AbstractInterpreter::is_not_reached(const methodHandle& method, int bci) {206BytecodeStream s(method, bci);207Bytecodes::Code code = s.next();208209if (Bytecodes::is_invoke(code)) {210assert(!Bytecodes::must_rewrite(code), "invokes aren't rewritten");211ConstantPool* cpool = method()->constants();212213Bytecode invoke_bc(s.bytecode());214215switch (code) {216case Bytecodes::_invokedynamic: {217assert(invoke_bc.has_index_u4(code), "sanity");218int method_index = invoke_bc.get_index_u4(code);219return cpool->invokedynamic_cp_cache_entry_at(method_index)->is_f1_null();220}221case Bytecodes::_invokevirtual: // fall-through222case Bytecodes::_invokeinterface: // fall-through223case Bytecodes::_invokespecial: // fall-through224case Bytecodes::_invokestatic: {225if (cpool->has_preresolution()) {226return false; // might have been reached227}228assert(!invoke_bc.has_index_u4(code), "sanity");229int method_index = invoke_bc.get_index_u2_cpcache(code);230constantPoolHandle cp(Thread::current(), cpool);231Method* resolved_method = ConstantPool::method_at_if_loaded(cp, method_index);232return (resolved_method == NULL);233}234default: ShouldNotReachHere();235}236} else if (!Bytecodes::must_rewrite(code)) {237// might have been reached238return false;239}240241// the bytecode might not be rewritten if the method is an accessor, etc.242address ientry = method->interpreter_entry();243if (ientry != entry_for_kind(AbstractInterpreter::zerolocals) &&244ientry != entry_for_kind(AbstractInterpreter::zerolocals_synchronized))245return false; // interpreter does not run this method!246247// otherwise, we can be sure this bytecode has never been executed248return true;249}250251252#ifndef PRODUCT253void AbstractInterpreter::print_method_kind(MethodKind kind) {254switch (kind) {255case zerolocals : tty->print("zerolocals" ); break;256case zerolocals_synchronized: tty->print("zerolocals_synchronized"); break;257case native : tty->print("native" ); break;258case native_synchronized : tty->print("native_synchronized" ); break;259case empty : tty->print("empty" ); break;260case getter : tty->print("getter" ); break;261case setter : tty->print("setter" ); break;262case abstract : tty->print("abstract" ); break;263case java_lang_math_sin : tty->print("java_lang_math_sin" ); break;264case java_lang_math_cos : tty->print("java_lang_math_cos" ); break;265case java_lang_math_tan : tty->print("java_lang_math_tan" ); break;266case java_lang_math_abs : tty->print("java_lang_math_abs" ); break;267case java_lang_math_sqrt : tty->print("java_lang_math_sqrt" ); break;268case java_lang_math_log : tty->print("java_lang_math_log" ); break;269case java_lang_math_log10 : tty->print("java_lang_math_log10" ); break;270case java_lang_math_fmaD : tty->print("java_lang_math_fmaD" ); break;271case java_lang_math_fmaF : tty->print("java_lang_math_fmaF" ); break;272case java_util_zip_CRC32_update : tty->print("java_util_zip_CRC32_update"); break;273case java_util_zip_CRC32_updateBytes : tty->print("java_util_zip_CRC32_updateBytes"); break;274case java_util_zip_CRC32_updateByteBuffer : tty->print("java_util_zip_CRC32_updateByteBuffer"); break;275case java_util_zip_CRC32C_updateBytes : tty->print("java_util_zip_CRC32C_updateBytes"); break;276case java_util_zip_CRC32C_updateDirectByteBuffer: tty->print("java_util_zip_CRC32C_updateDirectByteByffer"); break;277default:278if (kind >= method_handle_invoke_FIRST &&279kind <= method_handle_invoke_LAST) {280const char* kind_name = vmIntrinsics::name_at(method_handle_intrinsic(kind));281if (kind_name[0] == '_') kind_name = &kind_name[1]; // '_invokeExact' => 'invokeExact'282tty->print("method_handle_%s", kind_name);283break;284}285ShouldNotReachHere();286break;287}288}289#endif // PRODUCT290291292//------------------------------------------------------------------------------------------------------------------------293// Deoptimization support294295/**296* If a deoptimization happens, this function returns the point of next bytecode to continue execution.297*/298address AbstractInterpreter::deopt_continue_after_entry(Method* method, address bcp, int callee_parameters, bool is_top_frame) {299assert(method->contains(bcp), "just checkin'");300301// Get the original and rewritten bytecode.302Bytecodes::Code code = Bytecodes::java_code_at(method, bcp);303assert(!Interpreter::bytecode_should_reexecute(code), "should not reexecute");304305const int bci = method->bci_from(bcp);306307// compute continuation length308const int length = Bytecodes::length_at(method, bcp);309310// compute result type311BasicType type = T_ILLEGAL;312313switch (code) {314case Bytecodes::_invokevirtual :315case Bytecodes::_invokespecial :316case Bytecodes::_invokestatic :317case Bytecodes::_invokeinterface: {318Thread *thread = Thread::current();319ResourceMark rm(thread);320methodHandle mh(thread, method);321type = Bytecode_invoke(mh, bci).result_type();322// since the cache entry might not be initialized:323// (NOT needed for the old calling convension)324if (!is_top_frame) {325int index = Bytes::get_native_u2(bcp+1);326method->constants()->cache()->entry_at(index)->set_parameter_size(callee_parameters);327}328break;329}330331case Bytecodes::_invokedynamic: {332Thread *thread = Thread::current();333ResourceMark rm(thread);334methodHandle mh(thread, method);335type = Bytecode_invoke(mh, bci).result_type();336// since the cache entry might not be initialized:337// (NOT needed for the old calling convension)338if (!is_top_frame) {339int index = Bytes::get_native_u4(bcp+1);340method->constants()->invokedynamic_cp_cache_entry_at(index)->set_parameter_size(callee_parameters);341}342break;343}344345case Bytecodes::_ldc :346case Bytecodes::_ldc_w : // fall through347case Bytecodes::_ldc2_w:348{349Thread *thread = Thread::current();350ResourceMark rm(thread);351methodHandle mh(thread, method);352type = Bytecode_loadconstant(mh, bci).result_type();353break;354}355356default:357type = Bytecodes::result_type(code);358break;359}360361// return entry point for computed continuation state & bytecode length362return363is_top_frame364? Interpreter::deopt_entry (as_TosState(type), length)365: Interpreter::return_entry(as_TosState(type), length, code);366}367368// If deoptimization happens, this function returns the point where the interpreter reexecutes369// the bytecode.370// Note: Bytecodes::_athrow is a special case in that it does not return371// Interpreter::deopt_entry(vtos, 0) like others372address AbstractInterpreter::deopt_reexecute_entry(Method* method, address bcp) {373assert(method->contains(bcp), "just checkin'");374Bytecodes::Code code = Bytecodes::java_code_at(method, bcp);375#if defined(COMPILER1) || INCLUDE_JVMCI376if(code == Bytecodes::_athrow ) {377return Interpreter::rethrow_exception_entry();378}379#endif /* COMPILER1 || INCLUDE_JVMCI */380return Interpreter::deopt_entry(vtos, 0);381}382383// If deoptimization happens, the interpreter should reexecute these bytecodes.384// This function mainly helps the compilers to set up the reexecute bit.385bool AbstractInterpreter::bytecode_should_reexecute(Bytecodes::Code code) {386switch (code) {387case Bytecodes::_lookupswitch:388case Bytecodes::_tableswitch:389case Bytecodes::_fast_binaryswitch:390case Bytecodes::_fast_linearswitch:391// recompute condtional expression folded into _if<cond>392case Bytecodes::_lcmp :393case Bytecodes::_fcmpl :394case Bytecodes::_fcmpg :395case Bytecodes::_dcmpl :396case Bytecodes::_dcmpg :397case Bytecodes::_ifnull :398case Bytecodes::_ifnonnull :399case Bytecodes::_goto :400case Bytecodes::_goto_w :401case Bytecodes::_ifeq :402case Bytecodes::_ifne :403case Bytecodes::_iflt :404case Bytecodes::_ifge :405case Bytecodes::_ifgt :406case Bytecodes::_ifle :407case Bytecodes::_if_icmpeq :408case Bytecodes::_if_icmpne :409case Bytecodes::_if_icmplt :410case Bytecodes::_if_icmpge :411case Bytecodes::_if_icmpgt :412case Bytecodes::_if_icmple :413case Bytecodes::_if_acmpeq :414case Bytecodes::_if_acmpne :415// special cases416case Bytecodes::_getfield :417case Bytecodes::_putfield :418case Bytecodes::_getstatic :419case Bytecodes::_putstatic :420case Bytecodes::_aastore :421#ifdef COMPILER1422//special case of reexecution423case Bytecodes::_athrow :424#endif425return true;426427default:428return false;429}430}431432void AbstractInterpreter::initialize_method_handle_entries() {433// method handle entry kinds are generated later in MethodHandlesAdapterGenerator::generate:434for (int i = method_handle_invoke_FIRST; i <= method_handle_invoke_LAST; i++) {435MethodKind kind = (MethodKind) i;436_entry_table[kind] = _entry_table[Interpreter::abstract];437}438}439440441