jdk
1/*
2* Copyright (c) 2003, 2023, Oracle and/or its affiliates. All rights reserved.
3* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4*
5* This code is free software; you can redistribute it and/or modify it
6* under the terms of the GNU General Public License version 2 only, as
7* published by the Free Software Foundation.
8*
9* This code is distributed in the hope that it will be useful, but WITHOUT
10* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12* version 2 for more details (a copy is included in the LICENSE file that
13* accompanied this code).
14*
15* You should have received a copy of the GNU General Public License version
16* 2 along with this work; if not, write to the Free Software Foundation,
17* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18*
19* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20* or visit www.oracle.com if you need additional information or have any
21* questions.
22*
23*/
24
25#include "precompiled.hpp"26#include "asm/macroAssembler.hpp"27#include "classfile/javaClasses.hpp"28#include "compiler/compiler_globals.hpp"29#include "compiler/disassembler.hpp"30#include "gc/shared/barrierSetAssembler.hpp"31#include "interpreter/bytecodeHistogram.hpp"32#include "interpreter/interp_masm.hpp"33#include "interpreter/interpreter.hpp"34#include "interpreter/interpreterRuntime.hpp"35#include "interpreter/templateInterpreterGenerator.hpp"36#include "interpreter/templateTable.hpp"37#include "oops/arrayOop.hpp"38#include "oops/methodCounters.hpp"39#include "oops/methodData.hpp"40#include "oops/method.hpp"41#include "oops/oop.inline.hpp"42#include "oops/resolvedIndyEntry.hpp"43#include "oops/resolvedMethodEntry.hpp"44#include "prims/jvmtiExport.hpp"45#include "prims/jvmtiThreadState.hpp"46#include "runtime/continuation.hpp"47#include "runtime/deoptimization.hpp"48#include "runtime/frame.inline.hpp"49#include "runtime/globals.hpp"50#include "runtime/jniHandles.hpp"51#include "runtime/sharedRuntime.hpp"52#include "runtime/stubRoutines.hpp"53#include "runtime/synchronizer.hpp"54#include "runtime/timer.hpp"55#include "runtime/vframeArray.hpp"56#include "utilities/checkedCast.hpp"57#include "utilities/debug.hpp"58#include "utilities/macros.hpp"59
60#define __ Disassembler::hook<InterpreterMacroAssembler>(__FILE__, __LINE__, _masm)->61
62// Size of interpreter code. Increase if too small. Interpreter will
63// fail with a guarantee ("not enough space for interpreter generation");
64// if too small.
65// Run with +PrintInterpreter to get the VM to print out the size.
66// Max size with JVMTI
67#ifdef AMD6468int TemplateInterpreter::InterpreterCodeSize = JVMCI_ONLY(268) NOT_JVMCI(256) * 1024;69#else70int TemplateInterpreter::InterpreterCodeSize = 224 * 1024;71#endif // AMD6472
73// Global Register Names
74static const Register rbcp = LP64_ONLY(r13) NOT_LP64(rsi);75static const Register rlocals = LP64_ONLY(r14) NOT_LP64(rdi);76
77const int method_offset = frame::interpreter_frame_method_offset * wordSize;78const int bcp_offset = frame::interpreter_frame_bcp_offset * wordSize;79const int locals_offset = frame::interpreter_frame_locals_offset * wordSize;80
81
82//-----------------------------------------------------------------------------
83
84address TemplateInterpreterGenerator::generate_StackOverflowError_handler() {85address entry = __ pc();86
87#ifdef ASSERT88{89Label L;90__ movptr(rax, Address(rbp,91frame::interpreter_frame_monitor_block_top_offset *92wordSize));93__ lea(rax, Address(rbp, rax, Address::times_ptr));94__ cmpptr(rax, rsp); // rax = maximal rsp for current rbp (stack95// grows negative)96__ jcc(Assembler::aboveEqual, L); // check if frame is complete97__ stop ("interpreter frame not set up");98__ bind(L);99}100#endif // ASSERT101// Restore bcp under the assumption that the current frame is still102// interpreted103__ restore_bcp();104
105// expression stack must be empty before entering the VM if an106// exception happened107__ empty_expression_stack();108// throw exception109__ call_VM(noreg,110CAST_FROM_FN_PTR(address,111InterpreterRuntime::throw_StackOverflowError));112return entry;113}
114
115address TemplateInterpreterGenerator::generate_ArrayIndexOutOfBounds_handler() {116address entry = __ pc();117// The expression stack must be empty before entering the VM if an118// exception happened.119__ empty_expression_stack();120
121// Setup parameters.122// ??? convention: expect aberrant index in register ebx/rbx.123// Pass array to create more detailed exceptions.124Register rarg = NOT_LP64(rax) LP64_ONLY(c_rarg1);125__ call_VM(noreg,126CAST_FROM_FN_PTR(address,127InterpreterRuntime::128throw_ArrayIndexOutOfBoundsException),129rarg, rbx);130return entry;131}
132
133address TemplateInterpreterGenerator::generate_ClassCastException_handler() {134address entry = __ pc();135
136// object is at TOS137Register rarg = NOT_LP64(rax) LP64_ONLY(c_rarg1);138__ pop(rarg);139
140// expression stack must be empty before entering the VM if an141// exception happened142__ empty_expression_stack();143
144__ call_VM(noreg,145CAST_FROM_FN_PTR(address,146InterpreterRuntime::147throw_ClassCastException),148rarg);149return entry;150}
151
152address TemplateInterpreterGenerator::generate_exception_handler_common(153const char* name, const char* message, bool pass_oop) {154assert(!pass_oop || message == nullptr, "either oop or message but not both");155address entry = __ pc();156
157Register rarg = NOT_LP64(rax) LP64_ONLY(c_rarg1);158Register rarg2 = NOT_LP64(rbx) LP64_ONLY(c_rarg2);159
160if (pass_oop) {161// object is at TOS162__ pop(rarg2);163}164// expression stack must be empty before entering the VM if an165// exception happened166__ empty_expression_stack();167// setup parameters168__ lea(rarg, ExternalAddress((address)name));169if (pass_oop) {170__ call_VM(rax, CAST_FROM_FN_PTR(address,171InterpreterRuntime::172create_klass_exception),173rarg, rarg2);174} else {175__ lea(rarg2, ExternalAddress((address)message));176__ call_VM(rax,177CAST_FROM_FN_PTR(address, InterpreterRuntime::create_exception),178rarg, rarg2);179}180// throw exception181__ jump(ExternalAddress(Interpreter::throw_exception_entry()));182return entry;183}
184
185address TemplateInterpreterGenerator::generate_return_entry_for(TosState state, int step, size_t index_size) {186address entry = __ pc();187
188#ifndef _LP64189#ifdef COMPILER2190// The FPU stack is clean if UseSSE >= 2 but must be cleaned in other cases191if ((state == ftos && UseSSE < 1) || (state == dtos && UseSSE < 2)) {192for (int i = 1; i < 8; i++) {193__ ffree(i);194}195} else if (UseSSE < 2) {196__ empty_FPU_stack();197}198#endif // COMPILER2199if ((state == ftos && UseSSE < 1) || (state == dtos && UseSSE < 2)) {200__ MacroAssembler::verify_FPU(1, "generate_return_entry_for compiled");201} else {202__ MacroAssembler::verify_FPU(0, "generate_return_entry_for compiled");203}204
205if (state == ftos) {206__ MacroAssembler::verify_FPU(UseSSE >= 1 ? 0 : 1, "generate_return_entry_for in interpreter");207} else if (state == dtos) {208__ MacroAssembler::verify_FPU(UseSSE >= 2 ? 0 : 1, "generate_return_entry_for in interpreter");209}210#endif // _LP64211
212// Restore stack bottom in case i2c adjusted stack213__ movptr(rcx, Address(rbp, frame::interpreter_frame_last_sp_offset * wordSize));214__ lea(rsp, Address(rbp, rcx, Address::times_ptr));215// and null it as marker that esp is now tos until next java call216__ movptr(Address(rbp, frame::interpreter_frame_last_sp_offset * wordSize), NULL_WORD);217
218__ restore_bcp();219__ restore_locals();220
221if (state == atos) {222Register mdp = rbx;223Register tmp = rcx;224__ profile_return_type(mdp, rax, tmp);225}226
227const Register cache = rbx;228const Register index = rcx;229if (index_size == sizeof(u4)) {230__ load_resolved_indy_entry(cache, index);231__ load_unsigned_short(cache, Address(cache, in_bytes(ResolvedIndyEntry::num_parameters_offset())));232__ lea(rsp, Address(rsp, cache, Interpreter::stackElementScale()));233} else {234assert(index_size == sizeof(u2), "Can only be u2");235__ load_method_entry(cache, index);236__ load_unsigned_short(cache, Address(cache, in_bytes(ResolvedMethodEntry::num_parameters_offset())));237__ lea(rsp, Address(rsp, cache, Interpreter::stackElementScale()));238}239
240const Register java_thread = NOT_LP64(rcx) LP64_ONLY(r15_thread);241if (JvmtiExport::can_pop_frame()) {242NOT_LP64(__ get_thread(java_thread));243__ check_and_handle_popframe(java_thread);244}245if (JvmtiExport::can_force_early_return()) {246NOT_LP64(__ get_thread(java_thread));247__ check_and_handle_earlyret(java_thread);248}249
250__ dispatch_next(state, step);251
252return entry;253}
254
255
256address TemplateInterpreterGenerator::generate_deopt_entry_for(TosState state, int step, address continuation) {257address entry = __ pc();258
259#ifndef _LP64260if (state == ftos) {261__ MacroAssembler::verify_FPU(UseSSE >= 1 ? 0 : 1, "generate_deopt_entry_for in interpreter");262} else if (state == dtos) {263__ MacroAssembler::verify_FPU(UseSSE >= 2 ? 0 : 1, "generate_deopt_entry_for in interpreter");264}265#endif // _LP64266
267// null last_sp until next java call268__ movptr(Address(rbp, frame::interpreter_frame_last_sp_offset * wordSize), NULL_WORD);269__ restore_bcp();270__ restore_locals();271const Register thread = NOT_LP64(rcx) LP64_ONLY(r15_thread);272NOT_LP64(__ get_thread(thread));273#if INCLUDE_JVMCI274// Check if we need to take lock at entry of synchronized method. This can275// only occur on method entry so emit it only for vtos with step 0.276if (EnableJVMCI && state == vtos && step == 0) {277Label L;278__ cmpb(Address(thread, JavaThread::pending_monitorenter_offset()), 0);279__ jcc(Assembler::zero, L);280// Clear flag.281__ movb(Address(thread, JavaThread::pending_monitorenter_offset()), 0);282// Satisfy calling convention for lock_method().283__ get_method(rbx);284// Take lock.285lock_method();286__ bind(L);287} else {288#ifdef ASSERT289if (EnableJVMCI) {290Label L;291__ cmpb(Address(r15_thread, JavaThread::pending_monitorenter_offset()), 0);292__ jcc(Assembler::zero, L);293__ stop("unexpected pending monitor in deopt entry");294__ bind(L);295}296#endif297}298#endif299// handle exceptions300{301Label L;302__ cmpptr(Address(thread, Thread::pending_exception_offset()), NULL_WORD);303__ jcc(Assembler::zero, L);304__ call_VM(noreg,305CAST_FROM_FN_PTR(address,306InterpreterRuntime::throw_pending_exception));307__ should_not_reach_here();308__ bind(L);309}310if (continuation == nullptr) {311__ dispatch_next(state, step);312} else {313__ jump_to_entry(continuation);314}315return entry;316}
317
318address TemplateInterpreterGenerator::generate_result_handler_for(319BasicType type) {320address entry = __ pc();321switch (type) {322case T_BOOLEAN: __ c2bool(rax); break;323#ifndef _LP64324case T_CHAR : __ andptr(rax, 0xFFFF); break;325#else326case T_CHAR : __ movzwl(rax, rax); break;327#endif // _LP64328case T_BYTE : __ sign_extend_byte(rax); break;329case T_SHORT : __ sign_extend_short(rax); break;330case T_INT : /* nothing to do */ break;331case T_LONG : /* nothing to do */ break;332case T_VOID : /* nothing to do */ break;333#ifndef _LP64334case T_DOUBLE :335case T_FLOAT :336{ const Register t = InterpreterRuntime::SignatureHandlerGenerator::temp();337__ pop(t); // remove return address first338// Must return a result for interpreter or compiler. In SSE339// mode, results are returned in xmm0 and the FPU stack must340// be empty.341if (type == T_FLOAT && UseSSE >= 1) {342// Load ST0343__ fld_d(Address(rsp, 0));344// Store as float and empty fpu stack345__ fstp_s(Address(rsp, 0));346// and reload347__ movflt(xmm0, Address(rsp, 0));348} else if (type == T_DOUBLE && UseSSE >= 2 ) {349__ movdbl(xmm0, Address(rsp, 0));350} else {351// restore ST0352__ fld_d(Address(rsp, 0));353}354// and pop the temp355__ addptr(rsp, 2 * wordSize);356__ push(t); // restore return address357}358break;359#else360case T_FLOAT : /* nothing to do */ break;361case T_DOUBLE : /* nothing to do */ break;362#endif // _LP64363
364case T_OBJECT :365// retrieve result from frame366__ movptr(rax, Address(rbp, frame::interpreter_frame_oop_temp_offset*wordSize));367// and verify it368__ verify_oop(rax);369break;370default : ShouldNotReachHere();371}372__ ret(0); // return from result handler373return entry;374}
375
376address TemplateInterpreterGenerator::generate_safept_entry_for(377TosState state,378address runtime_entry) {379address entry = __ pc();380
381__ push(state);382__ push_cont_fastpath();383__ call_VM(noreg, runtime_entry);384__ pop_cont_fastpath();385
386__ dispatch_via(vtos, Interpreter::_normal_table.table_for(vtos));387return entry;388}
389
390
391
392// Helpers for commoning out cases in the various type of method entries.
393//
394
395
396// increment invocation count & check for overflow
397//
398// Note: checking for negative value instead of overflow
399// so we have a 'sticky' overflow test
400//
401// rbx: method
402// rcx: invocation counter
403//
404void TemplateInterpreterGenerator::generate_counter_incr(Label* overflow) {405Label done;406// Note: In tiered we increment either counters in Method* or in MDO depending if we're profiling or not.407Label no_mdo;408if (ProfileInterpreter) {409// Are we profiling?410__ movptr(rax, Address(rbx, Method::method_data_offset()));411__ testptr(rax, rax);412__ jccb(Assembler::zero, no_mdo);413// Increment counter in the MDO414const Address mdo_invocation_counter(rax, in_bytes(MethodData::invocation_counter_offset()) +415in_bytes(InvocationCounter::counter_offset()));416const Address mask(rax, in_bytes(MethodData::invoke_mask_offset()));417__ increment_mask_and_jump(mdo_invocation_counter, mask, rcx, overflow);418__ jmp(done);419}420__ bind(no_mdo);421// Increment counter in MethodCounters422const Address invocation_counter(rax,423MethodCounters::invocation_counter_offset() +424InvocationCounter::counter_offset());425__ get_method_counters(rbx, rax, done);426const Address mask(rax, in_bytes(MethodCounters::invoke_mask_offset()));427__ increment_mask_and_jump(invocation_counter, mask, rcx, overflow);428__ bind(done);429}
430
431void TemplateInterpreterGenerator::generate_counter_overflow(Label& do_continue) {432
433// Asm interpreter on entry434// r14/rdi - locals435// r13/rsi - bcp436// rbx - method437// rdx - cpool --- DOES NOT APPEAR TO BE TRUE438// rbp - interpreter frame439
440// On return (i.e. jump to entry_point) [ back to invocation of interpreter ]441// Everything as it was on entry442// rdx is not restored. Doesn't appear to really be set.443
444// InterpreterRuntime::frequency_counter_overflow takes two445// arguments, the first (thread) is passed by call_VM, the second446// indicates if the counter overflow occurs at a backwards branch447// (null bcp). We pass zero for it. The call returns the address448// of the verified entry point for the method or null if the449// compilation did not complete (either went background or bailed450// out).451Register rarg = NOT_LP64(rax) LP64_ONLY(c_rarg1);452__ movl(rarg, 0);453__ call_VM(noreg,454CAST_FROM_FN_PTR(address,455InterpreterRuntime::frequency_counter_overflow),456rarg);457
458__ movptr(rbx, Address(rbp, method_offset)); // restore Method*459// Preserve invariant that r13/r14 contain bcp/locals of sender frame460// and jump to the interpreted entry.461__ jmp(do_continue, relocInfo::none);462}
463
464// See if we've got enough room on the stack for locals plus overhead below
465// JavaThread::stack_overflow_limit(). If not, throw a StackOverflowError
466// without going through the signal handler, i.e., reserved and yellow zones
467// will not be made usable. The shadow zone must suffice to handle the
468// overflow.
469// The expression stack grows down incrementally, so the normal guard
470// page mechanism will work for that.
471//
472// NOTE: Since the additional locals are also always pushed (wasn't
473// obvious in generate_fixed_frame) so the guard should work for them
474// too.
475//
476// Args:
477// rdx: number of additional locals this frame needs (what we must check)
478// rbx: Method*
479//
480// Kills:
481// rax
482void TemplateInterpreterGenerator::generate_stack_overflow_check(void) {483
484// monitor entry size: see picture of stack in frame_x86.hpp485const int entry_size = frame::interpreter_frame_monitor_size_in_bytes();486
487// total overhead size: entry_size + (saved rbp through expr stack488// bottom). be sure to change this if you add/subtract anything489// to/from the overhead area490const int overhead_size =491-(frame::interpreter_frame_initial_sp_offset * wordSize) + entry_size;492
493const int page_size = (int)os::vm_page_size();494
495Label after_frame_check;496
497// see if the frame is greater than one page in size. If so,498// then we need to verify there is enough stack space remaining499// for the additional locals.500__ cmpl(rdx, (page_size - overhead_size) / Interpreter::stackElementSize);501__ jcc(Assembler::belowEqual, after_frame_check);502
503// compute rsp as if this were going to be the last frame on504// the stack before the red zone505
506Label after_frame_check_pop;507const Register thread = NOT_LP64(rsi) LP64_ONLY(r15_thread);508#ifndef _LP64509__ push(thread);510__ get_thread(thread);511#endif512
513const Address stack_limit(thread, JavaThread::stack_overflow_limit_offset());514
515// locals + overhead, in bytes516__ mov(rax, rdx);517__ shlptr(rax, Interpreter::logStackElementSize); // Convert parameter count to bytes.518__ addptr(rax, overhead_size);519
520#ifdef ASSERT521Label limit_okay;522// Verify that thread stack overflow limit is non-zero.523__ cmpptr(stack_limit, NULL_WORD);524__ jcc(Assembler::notEqual, limit_okay);525__ stop("stack overflow limit is zero");526__ bind(limit_okay);527#endif528
529// Add locals/frame size to stack limit.530__ addptr(rax, stack_limit);531
532// Check against the current stack bottom.533__ cmpptr(rsp, rax);534
535__ jcc(Assembler::above, after_frame_check_pop);536NOT_LP64(__ pop(rsi)); // get saved bcp537
538// Restore sender's sp as SP. This is necessary if the sender's539// frame is an extended compiled frame (see gen_c2i_adapter())540// and safer anyway in case of JSR292 adaptations.541
542__ pop(rax); // return address must be moved if SP is changed543__ mov(rsp, rbcp);544__ push(rax);545
546// Note: the restored frame is not necessarily interpreted.547// Use the shared runtime version of the StackOverflowError.548assert(StubRoutines::throw_StackOverflowError_entry() != nullptr, "stub not yet generated");549__ jump(ExternalAddress(StubRoutines::throw_StackOverflowError_entry()));550// all done with frame size check551__ bind(after_frame_check_pop);552NOT_LP64(__ pop(rsi));553
554// all done with frame size check555__ bind(after_frame_check);556}
557
558// Allocate monitor and lock method (asm interpreter)
559//
560// Args:
561// rbx: Method*
562// r14/rdi: locals
563//
564// Kills:
565// rax
566// c_rarg0, c_rarg1, c_rarg2, c_rarg3, ...(param regs)
567// rscratch1, rscratch2 (scratch regs)
568void TemplateInterpreterGenerator::lock_method() {569// synchronize method570const Address access_flags(rbx, Method::access_flags_offset());571const Address monitor_block_top(572rbp,573frame::interpreter_frame_monitor_block_top_offset * wordSize);574const int entry_size = frame::interpreter_frame_monitor_size_in_bytes();575
576#ifdef ASSERT577{578Label L;579__ movl(rax, access_flags);580__ testl(rax, JVM_ACC_SYNCHRONIZED);581__ jcc(Assembler::notZero, L);582__ stop("method doesn't need synchronization");583__ bind(L);584}585#endif // ASSERT586
587// get synchronization object588{589Label done;590__ movl(rax, access_flags);591__ testl(rax, JVM_ACC_STATIC);592// get receiver (assume this is frequent case)593__ movptr(rax, Address(rlocals, Interpreter::local_offset_in_bytes(0)));594__ jcc(Assembler::zero, done);595__ load_mirror(rax, rbx, rscratch2);596
597#ifdef ASSERT598{599Label L;600__ testptr(rax, rax);601__ jcc(Assembler::notZero, L);602__ stop("synchronization object is null");603__ bind(L);604}605#endif // ASSERT606
607__ bind(done);608}609
610// add space for monitor & lock611__ subptr(rsp, entry_size); // add space for a monitor entry612__ subptr(monitor_block_top, entry_size / wordSize); // set new monitor block top613// store object614__ movptr(Address(rsp, BasicObjectLock::obj_offset()), rax);615const Register lockreg = NOT_LP64(rdx) LP64_ONLY(c_rarg1);616__ movptr(lockreg, rsp); // object address617__ lock_object(lockreg);618}
619
620// Generate a fixed interpreter frame. This is identical setup for
621// interpreted methods and for native methods hence the shared code.
622//
623// Args:
624// rax: return address
625// rbx: Method*
626// r14/rdi: pointer to locals
627// r13/rsi: sender sp
628// rdx: cp cache
629void TemplateInterpreterGenerator::generate_fixed_frame(bool native_call) {630// initialize fixed part of activation frame631__ push(rax); // save return address632__ enter(); // save old & set new rbp633__ push(rbcp); // set sender sp634__ push(NULL_WORD); // leave last_sp as null635__ movptr(rbcp, Address(rbx, Method::const_offset())); // get ConstMethod*636__ lea(rbcp, Address(rbcp, ConstMethod::codes_offset())); // get codebase637__ push(rbx); // save Method*638// Get mirror and store it in the frame as GC root for this Method*639__ load_mirror(rdx, rbx, rscratch2);640__ push(rdx);641if (ProfileInterpreter) {642Label method_data_continue;643__ movptr(rdx, Address(rbx, in_bytes(Method::method_data_offset())));644__ testptr(rdx, rdx);645__ jcc(Assembler::zero, method_data_continue);646__ addptr(rdx, in_bytes(MethodData::data_offset()));647__ bind(method_data_continue);648__ push(rdx); // set the mdp (method data pointer)649} else {650__ push(0);651}652
653__ movptr(rdx, Address(rbx, Method::const_offset()));654__ movptr(rdx, Address(rdx, ConstMethod::constants_offset()));655__ movptr(rdx, Address(rdx, ConstantPool::cache_offset()));656__ push(rdx); // set constant pool cache657
658__ movptr(rax, rlocals);659__ subptr(rax, rbp);660__ shrptr(rax, Interpreter::logStackElementSize); // rax = rlocals - fp();661__ push(rax); // set relativized rlocals, see frame::interpreter_frame_locals()662
663if (native_call) {664__ push(0); // no bcp665} else {666__ push(rbcp); // set bcp667}668// initialize relativized pointer to expression stack bottom669__ push(frame::interpreter_frame_initial_sp_offset);670}
671
672// End of helpers
673
674// Method entry for java.lang.ref.Reference.get.
675address TemplateInterpreterGenerator::generate_Reference_get_entry(void) {676// Code: _aload_0, _getfield, _areturn677// parameter size = 1678//679// The code that gets generated by this routine is split into 2 parts:680// 1. The "intrinsified" code performing an ON_WEAK_OOP_REF load,681// 2. The slow path - which is an expansion of the regular method entry.682//683// Notes:-684// * An intrinsic is always executed, where an ON_WEAK_OOP_REF load is performed.685// * We may jump to the slow path iff the receiver is null. If the686// Reference object is null then we no longer perform an ON_WEAK_OOP_REF load687// Thus we can use the regular method entry code to generate the NPE.688//689// rbx: Method*690
691// r13: senderSP must preserve for slow path, set SP to it on fast path692
693address entry = __ pc();694
695const int referent_offset = java_lang_ref_Reference::referent_offset();696
697Label slow_path;698// rbx: method699
700// Check if local 0 != null701// If the receiver is null then it is OK to jump to the slow path.702__ movptr(rax, Address(rsp, wordSize));703
704__ testptr(rax, rax);705__ jcc(Assembler::zero, slow_path);706
707// rax: local 0708// rbx: method (but can be used as scratch now)709// rdx: scratch710// rdi: scratch711
712// Preserve the sender sp in case the load barrier713// calls the runtime714NOT_LP64(__ push(rsi));715
716// Load the value of the referent field.717const Address field_address(rax, referent_offset);718__ load_heap_oop(rax, field_address, /*tmp1*/ rbx, /*tmp_thread*/ rdx, ON_WEAK_OOP_REF);719
720// _areturn721const Register sender_sp = NOT_LP64(rsi) LP64_ONLY(r13);722NOT_LP64(__ pop(rsi)); // get sender sp723__ pop(rdi); // get return address724__ mov(rsp, sender_sp); // set sp to sender sp725__ jmp(rdi);726__ ret(0);727
728// generate a vanilla interpreter entry as the slow path729__ bind(slow_path);730__ jump_to_entry(Interpreter::entry_for_kind(Interpreter::zerolocals));731return entry;732}
733
734void TemplateInterpreterGenerator::bang_stack_shadow_pages(bool native_call) {735// See more discussion in stackOverflow.hpp.736
737// Note that we do the banging after the frame is setup, since the exception738// handling code expects to find a valid interpreter frame on the stack.739// Doing the banging earlier fails if the caller frame is not an interpreter740// frame.741// (Also, the exception throwing code expects to unlock any synchronized742// method receiver, so do the banging after locking the receiver.)743
744const int shadow_zone_size = checked_cast<int>(StackOverflow::stack_shadow_zone_size());745const int page_size = (int)os::vm_page_size();746const int n_shadow_pages = shadow_zone_size / page_size;747
748const Register thread = NOT_LP64(rsi) LP64_ONLY(r15_thread);749#ifndef _LP64750__ push(thread);751__ get_thread(thread);752#endif753
754#ifdef ASSERT755Label L_good_limit;756__ cmpptr(Address(thread, JavaThread::shadow_zone_safe_limit()), NULL_WORD);757__ jcc(Assembler::notEqual, L_good_limit);758__ stop("shadow zone safe limit is not initialized");759__ bind(L_good_limit);760
761Label L_good_watermark;762__ cmpptr(Address(thread, JavaThread::shadow_zone_growth_watermark()), NULL_WORD);763__ jcc(Assembler::notEqual, L_good_watermark);764__ stop("shadow zone growth watermark is not initialized");765__ bind(L_good_watermark);766#endif767
768Label L_done;769
770__ cmpptr(rsp, Address(thread, JavaThread::shadow_zone_growth_watermark()));771__ jcc(Assembler::above, L_done);772
773for (int p = 1; p <= n_shadow_pages; p++) {774__ bang_stack_with_offset(p*page_size);775}776
777// Record the new watermark, but only if update is above the safe limit.778// Otherwise, the next time around the check above would pass the safe limit.779__ cmpptr(rsp, Address(thread, JavaThread::shadow_zone_safe_limit()));780__ jccb(Assembler::belowEqual, L_done);781__ movptr(Address(thread, JavaThread::shadow_zone_growth_watermark()), rsp);782
783__ bind(L_done);784
785#ifndef _LP64786__ pop(thread);787#endif788}
789
790// Interpreter stub for calling a native method. (asm interpreter)
791// This sets up a somewhat different looking stack for calling the
792// native method than the typical interpreter frame setup.
793address TemplateInterpreterGenerator::generate_native_entry(bool synchronized) {794// determine code generation flags795bool inc_counter = UseCompiler || CountCompiledCalls;796
797// rbx: Method*798// rbcp: sender sp799
800address entry_point = __ pc();801
802const Address constMethod (rbx, Method::const_offset());803const Address access_flags (rbx, Method::access_flags_offset());804const Address size_of_parameters(rcx, ConstMethod::805size_of_parameters_offset());806
807
808// get parameter size (always needed)809__ movptr(rcx, constMethod);810__ load_unsigned_short(rcx, size_of_parameters);811
812// native calls don't need the stack size check since they have no813// expression stack and the arguments are already on the stack and814// we only add a handful of words to the stack815
816// rbx: Method*817// rcx: size of parameters818// rbcp: sender sp819__ pop(rax); // get return address820
821// for natives the size of locals is zero822
823// compute beginning of parameters824__ lea(rlocals, Address(rsp, rcx, Interpreter::stackElementScale(), -wordSize));825
826// add 2 zero-initialized slots for native calls827// initialize result_handler slot828__ push(NULL_WORD);829// slot for oop temp830// (static native method holder mirror/jni oop result)831__ push(NULL_WORD);832
833// initialize fixed part of activation frame834generate_fixed_frame(true);835
836// make sure method is native & not abstract837#ifdef ASSERT838__ movl(rax, access_flags);839{840Label L;841__ testl(rax, JVM_ACC_NATIVE);842__ jcc(Assembler::notZero, L);843__ stop("tried to execute non-native method as native");844__ bind(L);845}846{847Label L;848__ testl(rax, JVM_ACC_ABSTRACT);849__ jcc(Assembler::zero, L);850__ stop("tried to execute abstract method in interpreter");851__ bind(L);852}853#endif854
855// Since at this point in the method invocation the exception handler856// would try to exit the monitor of synchronized methods which hasn't857// been entered yet, we set the thread local variable858// _do_not_unlock_if_synchronized to true. The remove_activation will859// check this flag.860
861const Register thread1 = NOT_LP64(rax) LP64_ONLY(r15_thread);862NOT_LP64(__ get_thread(thread1));863const Address do_not_unlock_if_synchronized(thread1,864in_bytes(JavaThread::do_not_unlock_if_synchronized_offset()));865__ movbool(do_not_unlock_if_synchronized, true);866
867// increment invocation count & check for overflow868Label invocation_counter_overflow;869if (inc_counter) {870generate_counter_incr(&invocation_counter_overflow);871}872
873Label continue_after_compile;874__ bind(continue_after_compile);875
876bang_stack_shadow_pages(true);877
878// reset the _do_not_unlock_if_synchronized flag879NOT_LP64(__ get_thread(thread1));880__ movbool(do_not_unlock_if_synchronized, false);881
882// check for synchronized methods883// Must happen AFTER invocation_counter check and stack overflow check,884// so method is not locked if overflows.885if (synchronized) {886lock_method();887} else {888// no synchronization necessary889#ifdef ASSERT890{891Label L;892__ movl(rax, access_flags);893__ testl(rax, JVM_ACC_SYNCHRONIZED);894__ jcc(Assembler::zero, L);895__ stop("method needs synchronization");896__ bind(L);897}898#endif899}900
901// start execution902#ifdef ASSERT903{904Label L;905const Address monitor_block_top(rbp,906frame::interpreter_frame_monitor_block_top_offset * wordSize);907__ movptr(rax, monitor_block_top);908__ lea(rax, Address(rbp, rax, Address::times_ptr));909__ cmpptr(rax, rsp);910__ jcc(Assembler::equal, L);911__ stop("broken stack frame setup in interpreter 5");912__ bind(L);913}914#endif915
916// jvmti support917__ notify_method_entry();918
919// work registers920const Register method = rbx;921const Register thread = NOT_LP64(rdi) LP64_ONLY(r15_thread);922const Register t = NOT_LP64(rcx) LP64_ONLY(r11);923
924// allocate space for parameters925__ get_method(method);926__ movptr(t, Address(method, Method::const_offset()));927__ load_unsigned_short(t, Address(t, ConstMethod::size_of_parameters_offset()));928
929#ifndef _LP64930__ shlptr(t, Interpreter::logStackElementSize); // Convert parameter count to bytes.931__ addptr(t, 2*wordSize); // allocate two more slots for JNIEnv and possible mirror932__ subptr(rsp, t);933__ andptr(rsp, -(StackAlignmentInBytes)); // gcc needs 16 byte aligned stacks to do XMM intrinsics934#else935__ shll(t, Interpreter::logStackElementSize);936
937__ subptr(rsp, t);938__ subptr(rsp, frame::arg_reg_save_area_bytes); // windows939__ andptr(rsp, -16); // must be 16 byte boundary (see amd64 ABI)940#endif // _LP64941
942// get signature handler943{944Label L;945__ movptr(t, Address(method, Method::signature_handler_offset()));946__ testptr(t, t);947__ jcc(Assembler::notZero, L);948__ call_VM(noreg,949CAST_FROM_FN_PTR(address,950InterpreterRuntime::prepare_native_call),951method);952__ get_method(method);953__ movptr(t, Address(method, Method::signature_handler_offset()));954__ bind(L);955}956
957// call signature handler958assert(InterpreterRuntime::SignatureHandlerGenerator::from() == rlocals,959"adjust this code");960assert(InterpreterRuntime::SignatureHandlerGenerator::to() == rsp,961"adjust this code");962assert(InterpreterRuntime::SignatureHandlerGenerator::temp() == NOT_LP64(t) LP64_ONLY(rscratch1),963"adjust this code");964
965// The generated handlers do not touch RBX (the method).966// However, large signatures cannot be cached and are generated967// each time here. The slow-path generator can do a GC on return,968// so we must reload it after the call.969__ call(t);970__ get_method(method); // slow path can do a GC, reload RBX971
972
973// result handler is in rax974// set result handler975__ movptr(Address(rbp,976(frame::interpreter_frame_result_handler_offset) * wordSize),977rax);978
979// pass mirror handle if static call980{981Label L;982__ movl(t, Address(method, Method::access_flags_offset()));983__ testl(t, JVM_ACC_STATIC);984__ jcc(Assembler::zero, L);985// get mirror986__ load_mirror(t, method, rax);987// copy mirror into activation frame988__ movptr(Address(rbp, frame::interpreter_frame_oop_temp_offset * wordSize),989t);990// pass handle to mirror991#ifndef _LP64992__ lea(t, Address(rbp, frame::interpreter_frame_oop_temp_offset * wordSize));993__ movptr(Address(rsp, wordSize), t);994#else995__ lea(c_rarg1,996Address(rbp, frame::interpreter_frame_oop_temp_offset * wordSize));997#endif // _LP64998__ bind(L);999}1000
1001// get native function entry point1002{1003Label L;1004__ movptr(rax, Address(method, Method::native_function_offset()));1005ExternalAddress unsatisfied(SharedRuntime::native_method_throw_unsatisfied_link_error_entry());1006__ cmpptr(rax, unsatisfied.addr(), rscratch1);1007__ jcc(Assembler::notEqual, L);1008__ call_VM(noreg,1009CAST_FROM_FN_PTR(address,1010InterpreterRuntime::prepare_native_call),1011method);1012__ get_method(method);1013__ movptr(rax, Address(method, Method::native_function_offset()));1014__ bind(L);1015}1016
1017// pass JNIEnv1018#ifndef _LP641019__ get_thread(thread);1020__ lea(t, Address(thread, JavaThread::jni_environment_offset()));1021__ movptr(Address(rsp, 0), t);1022
1023// set_last_Java_frame_before_call1024// It is enough that the pc()1025// points into the right code segment. It does not have to be the correct return pc.1026__ set_last_Java_frame(thread, noreg, rbp, __ pc(), noreg);1027#else1028__ lea(c_rarg0, Address(r15_thread, JavaThread::jni_environment_offset()));1029
1030// It is enough that the pc() points into the right code1031// segment. It does not have to be the correct return pc.1032__ set_last_Java_frame(rsp, rbp, (address) __ pc(), rscratch1);1033#endif // _LP641034
1035// change thread state1036#ifdef ASSERT1037{1038Label L;1039__ movl(t, Address(thread, JavaThread::thread_state_offset()));1040__ cmpl(t, _thread_in_Java);1041__ jcc(Assembler::equal, L);1042__ stop("Wrong thread state in native stub");1043__ bind(L);1044}1045#endif1046
1047// Change state to native1048
1049__ movl(Address(thread, JavaThread::thread_state_offset()),1050_thread_in_native);1051
1052// Call the native method.1053__ call(rax);1054// 32: result potentially in rdx:rax or ST01055// 64: result potentially in rax or xmm01056
1057// Verify or restore cpu control state after JNI call1058__ restore_cpu_control_state_after_jni(rscratch1);1059
1060// NOTE: The order of these pushes is known to frame::interpreter_frame_result1061// in order to extract the result of a method call. If the order of these1062// pushes change or anything else is added to the stack then the code in1063// interpreter_frame_result must also change.1064
1065#ifndef _LP641066// save potential result in ST(0) & rdx:rax1067// (if result handler is the T_FLOAT or T_DOUBLE handler, result must be in ST0 -1068// the check is necessary to avoid potential Intel FPU overflow problems by saving/restoring 'empty' FPU registers)1069// It is safe to do this push because state is _thread_in_native and return address will be found1070// via _last_native_pc and not via _last_jave_sp1071
1072// NOTE: the order of these push(es) is known to frame::interpreter_frame_result.1073// If the order changes or anything else is added to the stack the code in1074// interpreter_frame_result will have to be changed.1075
1076{ Label L;1077Label push_double;1078ExternalAddress float_handler(AbstractInterpreter::result_handler(T_FLOAT));1079ExternalAddress double_handler(AbstractInterpreter::result_handler(T_DOUBLE));1080__ cmpptr(Address(rbp, (frame::interpreter_frame_oop_temp_offset + 1)*wordSize),1081float_handler.addr(), noreg);1082__ jcc(Assembler::equal, push_double);1083__ cmpptr(Address(rbp, (frame::interpreter_frame_oop_temp_offset + 1)*wordSize),1084double_handler.addr(), noreg);1085__ jcc(Assembler::notEqual, L);1086__ bind(push_double);1087__ push_d(); // FP values are returned using the FPU, so push FPU contents (even if UseSSE > 0).1088__ bind(L);1089}1090#else1091__ push(dtos);1092#endif // _LP641093
1094__ push(ltos);1095
1096// change thread state1097NOT_LP64(__ get_thread(thread));1098__ movl(Address(thread, JavaThread::thread_state_offset()),1099_thread_in_native_trans);1100
1101// Force this write out before the read below1102if (!UseSystemMemoryBarrier) {1103__ membar(Assembler::Membar_mask_bits(1104Assembler::LoadLoad | Assembler::LoadStore |1105Assembler::StoreLoad | Assembler::StoreStore));1106}1107#ifndef _LP641108if (AlwaysRestoreFPU) {1109// Make sure the control word is correct.1110__ fldcw(ExternalAddress(StubRoutines::x86::addr_fpu_cntrl_wrd_std()));1111}1112#endif // _LP641113
1114// check for safepoint operation in progress and/or pending suspend requests1115{1116Label Continue;1117Label slow_path;1118
1119__ safepoint_poll(slow_path, thread, true /* at_return */, false /* in_nmethod */);1120
1121__ cmpl(Address(thread, JavaThread::suspend_flags_offset()), 0);1122__ jcc(Assembler::equal, Continue);1123__ bind(slow_path);1124
1125// Don't use call_VM as it will see a possible pending exception1126// and forward it and never return here preventing us from1127// clearing _last_native_pc down below. Also can't use1128// call_VM_leaf either as it will check to see if r13 & r14 are1129// preserved and correspond to the bcp/locals pointers. So we do a1130// runtime call by hand.1131//1132#ifndef _LP641133__ push(thread);1134__ call(RuntimeAddress(CAST_FROM_FN_PTR(address,1135JavaThread::check_special_condition_for_native_trans)));1136__ increment(rsp, wordSize);1137__ get_thread(thread);1138#else1139__ mov(c_rarg0, r15_thread);1140__ mov(r12, rsp); // remember sp (can only use r12 if not using call_VM)1141__ subptr(rsp, frame::arg_reg_save_area_bytes); // windows1142__ andptr(rsp, -16); // align stack as required by ABI1143__ call(RuntimeAddress(CAST_FROM_FN_PTR(address, JavaThread::check_special_condition_for_native_trans)));1144__ mov(rsp, r12); // restore sp1145__ reinit_heapbase();1146#endif // _LP641147__ bind(Continue);1148}1149
1150// change thread state1151__ movl(Address(thread, JavaThread::thread_state_offset()), _thread_in_Java);1152
1153// reset_last_Java_frame1154__ reset_last_Java_frame(thread, true);1155
1156if (CheckJNICalls) {1157// clear_pending_jni_exception_check1158__ movptr(Address(thread, JavaThread::pending_jni_exception_check_fn_offset()), NULL_WORD);1159}1160
1161// reset handle block1162__ movptr(t, Address(thread, JavaThread::active_handles_offset()));1163__ movl(Address(t, JNIHandleBlock::top_offset()), NULL_WORD);1164
1165// If result is an oop unbox and store it in frame where gc will see it1166// and result handler will pick it up1167
1168{1169Label no_oop;1170__ lea(t, ExternalAddress(AbstractInterpreter::result_handler(T_OBJECT)));1171__ cmpptr(t, Address(rbp, frame::interpreter_frame_result_handler_offset*wordSize));1172__ jcc(Assembler::notEqual, no_oop);1173// retrieve result1174__ pop(ltos);1175// Unbox oop result, e.g. JNIHandles::resolve value.1176__ resolve_jobject(rax /* value */,1177thread /* thread */,1178t /* tmp */);1179__ movptr(Address(rbp, frame::interpreter_frame_oop_temp_offset*wordSize), rax);1180// keep stack depth as expected by pushing oop which will eventually be discarded1181__ push(ltos);1182__ bind(no_oop);1183}1184
1185
1186{1187Label no_reguard;1188__ cmpl(Address(thread, JavaThread::stack_guard_state_offset()),1189StackOverflow::stack_guard_yellow_reserved_disabled);1190__ jcc(Assembler::notEqual, no_reguard);1191
1192__ pusha(); // XXX only save smashed registers1193#ifndef _LP641194__ call(RuntimeAddress(CAST_FROM_FN_PTR(address, SharedRuntime::reguard_yellow_pages)));1195__ popa();1196#else1197__ mov(r12, rsp); // remember sp (can only use r12 if not using call_VM)1198__ subptr(rsp, frame::arg_reg_save_area_bytes); // windows1199__ andptr(rsp, -16); // align stack as required by ABI1200__ call(RuntimeAddress(CAST_FROM_FN_PTR(address, SharedRuntime::reguard_yellow_pages)));1201__ mov(rsp, r12); // restore sp1202__ popa(); // XXX only restore smashed registers1203__ reinit_heapbase();1204#endif // _LP641205
1206__ bind(no_reguard);1207}1208
1209
1210// The method register is junk from after the thread_in_native transition1211// until here. Also can't call_VM until the bcp has been1212// restored. Need bcp for throwing exception below so get it now.1213__ get_method(method);1214
1215// restore to have legal interpreter frame, i.e., bci == 0 <=> code_base()1216__ movptr(rbcp, Address(method, Method::const_offset())); // get ConstMethod*1217__ lea(rbcp, Address(rbcp, ConstMethod::codes_offset())); // get codebase1218
1219// handle exceptions (exception handling will handle unlocking!)1220{1221Label L;1222__ cmpptr(Address(thread, Thread::pending_exception_offset()), NULL_WORD);1223__ jcc(Assembler::zero, L);1224// Note: At some point we may want to unify this with the code1225// used in call_VM_base(); i.e., we should use the1226// StubRoutines::forward_exception code. For now this doesn't work1227// here because the rsp is not correctly set at this point.1228__ MacroAssembler::call_VM(noreg,1229CAST_FROM_FN_PTR(address,1230InterpreterRuntime::throw_pending_exception));1231__ should_not_reach_here();1232__ bind(L);1233}1234
1235// do unlocking if necessary1236{1237Label L;1238__ movl(t, Address(method, Method::access_flags_offset()));1239__ testl(t, JVM_ACC_SYNCHRONIZED);1240__ jcc(Assembler::zero, L);1241// the code below should be shared with interpreter macro1242// assembler implementation1243{1244Label unlock;1245// BasicObjectLock will be first in list, since this is a1246// synchronized method. However, need to check that the object1247// has not been unlocked by an explicit monitorexit bytecode.1248const Address monitor(rbp,1249(intptr_t)(frame::interpreter_frame_initial_sp_offset *1250wordSize - (int)sizeof(BasicObjectLock)));1251
1252const Register regmon = NOT_LP64(rdx) LP64_ONLY(c_rarg1);1253
1254// monitor expect in c_rarg1 for slow unlock path1255__ lea(regmon, monitor); // address of first monitor1256
1257__ movptr(t, Address(regmon, BasicObjectLock::obj_offset()));1258__ testptr(t, t);1259__ jcc(Assembler::notZero, unlock);1260
1261// Entry already unlocked, need to throw exception1262__ MacroAssembler::call_VM(noreg,1263CAST_FROM_FN_PTR(address,1264InterpreterRuntime::throw_illegal_monitor_state_exception));1265__ should_not_reach_here();1266
1267__ bind(unlock);1268__ unlock_object(regmon);1269}1270__ bind(L);1271}1272
1273// jvmti support1274// Note: This must happen _after_ handling/throwing any exceptions since1275// the exception handler code notifies the runtime of method exits1276// too. If this happens before, method entry/exit notifications are1277// not properly paired (was bug - gri 11/22/99).1278__ notify_method_exit(vtos, InterpreterMacroAssembler::NotifyJVMTI);1279
1280// restore potential result in edx:eax, call result handler to1281// restore potential result in ST0 & handle result1282
1283__ pop(ltos);1284LP64_ONLY( __ pop(dtos));1285
1286__ movptr(t, Address(rbp,1287(frame::interpreter_frame_result_handler_offset) * wordSize));1288__ call(t);1289
1290// remove activation1291__ movptr(t, Address(rbp,1292frame::interpreter_frame_sender_sp_offset *1293wordSize)); // get sender sp1294__ leave(); // remove frame anchor1295__ pop(rdi); // get return address1296__ mov(rsp, t); // set sp to sender sp1297__ jmp(rdi);1298
1299if (inc_counter) {1300// Handle overflow of counter and compile method1301__ bind(invocation_counter_overflow);1302generate_counter_overflow(continue_after_compile);1303}1304
1305return entry_point;1306}
1307
1308// Abstract method entry
1309// Attempt to execute abstract method. Throw exception
1310address TemplateInterpreterGenerator::generate_abstract_entry(void) {1311
1312address entry_point = __ pc();1313
1314// abstract method entry1315
1316// pop return address, reset last_sp to null1317__ empty_expression_stack();1318__ restore_bcp(); // rsi must be correct for exception handler (was destroyed)1319__ restore_locals(); // make sure locals pointer is correct as well (was destroyed)1320
1321// throw exception1322__ call_VM(noreg, CAST_FROM_FN_PTR(address, InterpreterRuntime::throw_AbstractMethodErrorWithMethod), rbx);1323// the call_VM checks for exception, so we should never return here.1324__ should_not_reach_here();1325
1326return entry_point;1327}
1328
1329//
1330// Generic interpreted method entry to (asm) interpreter
1331//
1332address TemplateInterpreterGenerator::generate_normal_entry(bool synchronized) {1333// determine code generation flags1334bool inc_counter = UseCompiler || CountCompiledCalls;1335
1336// ebx: Method*1337// rbcp: sender sp (set in InterpreterMacroAssembler::prepare_to_jump_from_interpreted / generate_call_stub)1338address entry_point = __ pc();1339
1340const Address constMethod(rbx, Method::const_offset());1341const Address access_flags(rbx, Method::access_flags_offset());1342const Address size_of_parameters(rdx,1343ConstMethod::size_of_parameters_offset());1344const Address size_of_locals(rdx, ConstMethod::size_of_locals_offset());1345
1346
1347// get parameter size (always needed)1348__ movptr(rdx, constMethod);1349__ load_unsigned_short(rcx, size_of_parameters);1350
1351// rbx: Method*1352// rcx: size of parameters1353// rbcp: sender_sp (could differ from sp+wordSize if we were called via c2i )1354
1355__ load_unsigned_short(rdx, size_of_locals); // get size of locals in words1356__ subl(rdx, rcx); // rdx = no. of additional locals1357
1358// YYY1359// __ incrementl(rdx);
1360// __ andl(rdx, -2);
1361
1362// see if we've got enough room on the stack for locals plus overhead.1363generate_stack_overflow_check();1364
1365// get return address1366__ pop(rax);1367
1368// compute beginning of parameters1369__ lea(rlocals, Address(rsp, rcx, Interpreter::stackElementScale(), -wordSize));1370
1371// rdx - # of additional locals1372// allocate space for locals1373// explicitly initialize locals1374{1375Label exit, loop;1376__ testl(rdx, rdx);1377__ jcc(Assembler::lessEqual, exit); // do nothing if rdx <= 01378__ bind(loop);1379__ push(NULL_WORD); // initialize local variables1380__ decrementl(rdx); // until everything initialized1381__ jcc(Assembler::greater, loop);1382__ bind(exit);1383}1384
1385// initialize fixed part of activation frame1386generate_fixed_frame(false);1387
1388// make sure method is not native & not abstract1389#ifdef ASSERT1390__ movl(rax, access_flags);1391{1392Label L;1393__ testl(rax, JVM_ACC_NATIVE);1394__ jcc(Assembler::zero, L);1395__ stop("tried to execute native method as non-native");1396__ bind(L);1397}1398{1399Label L;1400__ testl(rax, JVM_ACC_ABSTRACT);1401__ jcc(Assembler::zero, L);1402__ stop("tried to execute abstract method in interpreter");1403__ bind(L);1404}1405#endif1406
1407// Since at this point in the method invocation the exception1408// handler would try to exit the monitor of synchronized methods1409// which hasn't been entered yet, we set the thread local variable1410// _do_not_unlock_if_synchronized to true. The remove_activation1411// will check this flag.1412
1413const Register thread = NOT_LP64(rax) LP64_ONLY(r15_thread);1414NOT_LP64(__ get_thread(thread));1415const Address do_not_unlock_if_synchronized(thread,1416in_bytes(JavaThread::do_not_unlock_if_synchronized_offset()));1417__ movbool(do_not_unlock_if_synchronized, true);1418
1419__ profile_parameters_type(rax, rcx, rdx);1420// increment invocation count & check for overflow1421Label invocation_counter_overflow;1422if (inc_counter) {1423generate_counter_incr(&invocation_counter_overflow);1424}1425
1426Label continue_after_compile;1427__ bind(continue_after_compile);1428
1429// check for synchronized interpreted methods1430bang_stack_shadow_pages(false);1431
1432// reset the _do_not_unlock_if_synchronized flag1433NOT_LP64(__ get_thread(thread));1434__ movbool(do_not_unlock_if_synchronized, false);1435
1436// check for synchronized methods1437// Must happen AFTER invocation_counter check and stack overflow check,1438// so method is not locked if overflows.1439if (synchronized) {1440// Allocate monitor and lock method1441lock_method();1442} else {1443// no synchronization necessary1444#ifdef ASSERT1445{1446Label L;1447__ movl(rax, access_flags);1448__ testl(rax, JVM_ACC_SYNCHRONIZED);1449__ jcc(Assembler::zero, L);1450__ stop("method needs synchronization");1451__ bind(L);1452}1453#endif1454}1455
1456// start execution1457#ifdef ASSERT1458{1459Label L;1460const Address monitor_block_top (rbp,1461frame::interpreter_frame_monitor_block_top_offset * wordSize);1462__ movptr(rax, monitor_block_top);1463__ lea(rax, Address(rbp, rax, Address::times_ptr));1464__ cmpptr(rax, rsp);1465__ jcc(Assembler::equal, L);1466__ stop("broken stack frame setup in interpreter 6");1467__ bind(L);1468}1469#endif1470
1471// jvmti support1472__ notify_method_entry();1473
1474__ dispatch_next(vtos);1475
1476// invocation counter overflow1477if (inc_counter) {1478// Handle overflow of counter and compile method1479__ bind(invocation_counter_overflow);1480generate_counter_overflow(continue_after_compile);1481}1482
1483return entry_point;1484}
1485
1486//-----------------------------------------------------------------------------
1487// Exceptions
1488
1489void TemplateInterpreterGenerator::generate_throw_exception() {1490// Entry point in previous activation (i.e., if the caller was1491// interpreted)1492Interpreter::_rethrow_exception_entry = __ pc();1493// Restore sp to interpreter_frame_last_sp even though we are going1494// to empty the expression stack for the exception processing.1495__ movptr(Address(rbp, frame::interpreter_frame_last_sp_offset * wordSize), NULL_WORD);1496// rax: exception1497// rdx: return address/pc that threw exception1498__ restore_bcp(); // r13/rsi points to call/send1499__ restore_locals();1500LP64_ONLY(__ reinit_heapbase()); // restore r12 as heapbase.1501// Entry point for exceptions thrown within interpreter code1502Interpreter::_throw_exception_entry = __ pc();1503// expression stack is undefined here1504// rax: exception1505// r13/rsi: exception bcp1506__ verify_oop(rax);1507Register rarg = NOT_LP64(rax) LP64_ONLY(c_rarg1);1508LP64_ONLY(__ mov(c_rarg1, rax));1509
1510// expression stack must be empty before entering the VM in case of1511// an exception1512__ empty_expression_stack();1513// find exception handler address and preserve exception oop1514__ call_VM(rdx,1515CAST_FROM_FN_PTR(address,1516InterpreterRuntime::exception_handler_for_exception),1517rarg);1518// rax: exception handler entry point1519// rdx: preserved exception oop1520// r13/rsi: bcp for exception handler1521__ push_ptr(rdx); // push exception which is now the only value on the stack1522__ jmp(rax); // jump to exception handler (may be _remove_activation_entry!)1523
1524// If the exception is not handled in the current frame the frame is1525// removed and the exception is rethrown (i.e. exception1526// continuation is _rethrow_exception).1527//1528// Note: At this point the bci is still the bxi for the instruction1529// which caused the exception and the expression stack is1530// empty. Thus, for any VM calls at this point, GC will find a legal1531// oop map (with empty expression stack).1532
1533// In current activation1534// tos: exception1535// esi: exception bcp1536
1537//1538// JVMTI PopFrame support1539//1540
1541Interpreter::_remove_activation_preserving_args_entry = __ pc();1542__ empty_expression_stack();1543// Set the popframe_processing bit in pending_popframe_condition1544// indicating that we are currently handling popframe, so that1545// call_VMs that may happen later do not trigger new popframe1546// handling cycles.1547const Register thread = NOT_LP64(rcx) LP64_ONLY(r15_thread);1548NOT_LP64(__ get_thread(thread));1549__ movl(rdx, Address(thread, JavaThread::popframe_condition_offset()));1550__ orl(rdx, JavaThread::popframe_processing_bit);1551__ movl(Address(thread, JavaThread::popframe_condition_offset()), rdx);1552
1553{1554// Check to see whether we are returning to a deoptimized frame.1555// (The PopFrame call ensures that the caller of the popped frame is1556// either interpreted or compiled and deoptimizes it if compiled.)1557// In this case, we can't call dispatch_next() after the frame is1558// popped, but instead must save the incoming arguments and restore1559// them after deoptimization has occurred.1560//1561// Note that we don't compare the return PC against the1562// deoptimization blob's unpack entry because of the presence of1563// adapter frames in C2.1564Label caller_not_deoptimized;1565Register rarg = NOT_LP64(rdx) LP64_ONLY(c_rarg1);1566__ movptr(rarg, Address(rbp, frame::return_addr_offset * wordSize));1567__ super_call_VM_leaf(CAST_FROM_FN_PTR(address,1568InterpreterRuntime::interpreter_contains), rarg);1569__ testl(rax, rax);1570__ jcc(Assembler::notZero, caller_not_deoptimized);1571
1572// Compute size of arguments for saving when returning to1573// deoptimized caller1574__ get_method(rax);1575__ movptr(rax, Address(rax, Method::const_offset()));1576__ load_unsigned_short(rax, Address(rax, in_bytes(ConstMethod::1577size_of_parameters_offset())));1578__ shll(rax, Interpreter::logStackElementSize);1579__ restore_locals();1580__ subptr(rlocals, rax);1581__ addptr(rlocals, wordSize);1582// Save these arguments1583NOT_LP64(__ get_thread(thread));1584__ super_call_VM_leaf(CAST_FROM_FN_PTR(address,1585Deoptimization::1586popframe_preserve_args),1587thread, rax, rlocals);1588
1589__ remove_activation(vtos, rdx,1590/* throw_monitor_exception */ false,1591/* install_monitor_exception */ false,1592/* notify_jvmdi */ false);1593
1594// Inform deoptimization that it is responsible for restoring1595// these arguments1596NOT_LP64(__ get_thread(thread));1597__ movl(Address(thread, JavaThread::popframe_condition_offset()),1598JavaThread::popframe_force_deopt_reexecution_bit);1599
1600// Continue in deoptimization handler1601__ jmp(rdx);1602
1603__ bind(caller_not_deoptimized);1604}1605
1606__ remove_activation(vtos, rdx, /* rdx result (retaddr) is not used */1607/* throw_monitor_exception */ false,1608/* install_monitor_exception */ false,1609/* notify_jvmdi */ false);1610
1611// Finish with popframe handling1612// A previous I2C followed by a deoptimization might have moved the1613// outgoing arguments further up the stack. PopFrame expects the1614// mutations to those outgoing arguments to be preserved and other1615// constraints basically require this frame to look exactly as1616// though it had previously invoked an interpreted activation with1617// no space between the top of the expression stack (current1618// last_sp) and the top of stack. Rather than force deopt to1619// maintain this kind of invariant all the time we call a small1620// fixup routine to move the mutated arguments onto the top of our1621// expression stack if necessary.1622#ifndef _LP641623__ mov(rax, rsp);1624__ movptr(rbx, Address(rbp, frame::interpreter_frame_last_sp_offset * wordSize));1625__ lea(rbx, Address(rbp, rbx, Address::times_ptr));1626__ get_thread(thread);1627// PC must point into interpreter here1628__ set_last_Java_frame(thread, noreg, rbp, __ pc(), noreg);1629__ super_call_VM_leaf(CAST_FROM_FN_PTR(address, InterpreterRuntime::popframe_move_outgoing_args), thread, rax, rbx);1630__ get_thread(thread);1631#else1632__ mov(c_rarg1, rsp);1633__ movptr(c_rarg2, Address(rbp, frame::interpreter_frame_last_sp_offset * wordSize));1634__ lea(c_rarg2, Address(rbp, c_rarg2, Address::times_ptr));1635// PC must point into interpreter here1636__ set_last_Java_frame(noreg, rbp, __ pc(), rscratch1);1637__ super_call_VM_leaf(CAST_FROM_FN_PTR(address, InterpreterRuntime::popframe_move_outgoing_args), r15_thread, c_rarg1, c_rarg2);1638#endif1639__ reset_last_Java_frame(thread, true);1640
1641// Restore the last_sp and null it out1642__ movptr(rcx, Address(rbp, frame::interpreter_frame_last_sp_offset * wordSize));1643__ lea(rsp, Address(rbp, rcx, Address::times_ptr));1644__ movptr(Address(rbp, frame::interpreter_frame_last_sp_offset * wordSize), NULL_WORD);1645
1646__ restore_bcp();1647__ restore_locals();1648// The method data pointer was incremented already during1649// call profiling. We have to restore the mdp for the current bcp.1650if (ProfileInterpreter) {1651__ set_method_data_pointer_for_bcp();1652}1653
1654// Clear the popframe condition flag1655NOT_LP64(__ get_thread(thread));1656__ movl(Address(thread, JavaThread::popframe_condition_offset()),1657JavaThread::popframe_inactive);1658
1659#if INCLUDE_JVMTI1660{1661Label L_done;1662const Register local0 = rlocals;1663
1664__ cmpb(Address(rbcp, 0), Bytecodes::_invokestatic);1665__ jcc(Assembler::notEqual, L_done);1666
1667// The member name argument must be restored if _invokestatic is re-executed after a PopFrame call.1668// Detect such a case in the InterpreterRuntime function and return the member name argument, or null.1669
1670__ get_method(rdx);1671__ movptr(rax, Address(local0, 0));1672__ call_VM(rax, CAST_FROM_FN_PTR(address, InterpreterRuntime::member_name_arg_or_null), rax, rdx, rbcp);1673
1674__ testptr(rax, rax);1675__ jcc(Assembler::zero, L_done);1676
1677__ movptr(Address(rbx, 0), rax);1678__ bind(L_done);1679}1680#endif // INCLUDE_JVMTI1681
1682__ dispatch_next(vtos);1683// end of PopFrame support1684
1685Interpreter::_remove_activation_entry = __ pc();1686
1687// preserve exception over this code sequence1688__ pop_ptr(rax);1689NOT_LP64(__ get_thread(thread));1690__ movptr(Address(thread, JavaThread::vm_result_offset()), rax);1691// remove the activation (without doing throws on illegalMonitorExceptions)1692__ remove_activation(vtos, rdx, false, true, false);1693// restore exception1694NOT_LP64(__ get_thread(thread));1695__ get_vm_result(rax, thread);1696
1697// In between activations - previous activation type unknown yet1698// compute continuation point - the continuation point expects the1699// following registers set up:1700//1701// rax: exception1702// rdx: return address/pc that threw exception1703// rsp: expression stack of caller1704// rbp: ebp of caller1705__ push(rax); // save exception1706__ push(rdx); // save return address1707__ super_call_VM_leaf(CAST_FROM_FN_PTR(address,1708SharedRuntime::exception_handler_for_return_address),1709thread, rdx);1710__ mov(rbx, rax); // save exception handler1711__ pop(rdx); // restore return address1712__ pop(rax); // restore exception1713// Note that an "issuing PC" is actually the next PC after the call1714__ jmp(rbx); // jump to exception1715// handler of caller1716}
1717
1718
1719//
1720// JVMTI ForceEarlyReturn support
1721//
1722address TemplateInterpreterGenerator::generate_earlyret_entry_for(TosState state) {1723address entry = __ pc();1724
1725__ restore_bcp();1726__ restore_locals();1727__ empty_expression_stack();1728__ load_earlyret_value(state); // 32 bits returns value in rdx, so don't reuse1729
1730const Register thread = NOT_LP64(rcx) LP64_ONLY(r15_thread);1731NOT_LP64(__ get_thread(thread));1732__ movptr(rcx, Address(thread, JavaThread::jvmti_thread_state_offset()));1733Address cond_addr(rcx, JvmtiThreadState::earlyret_state_offset());1734
1735// Clear the earlyret state1736__ movl(cond_addr, JvmtiThreadState::earlyret_inactive);1737
1738__ remove_activation(state, rsi,1739false, /* throw_monitor_exception */1740false, /* install_monitor_exception */1741true); /* notify_jvmdi */1742__ jmp(rsi);1743
1744return entry;1745} // end of ForceEarlyReturn support1746
1747
1748//-----------------------------------------------------------------------------
1749// Helper for vtos entry point generation
1750
1751void TemplateInterpreterGenerator::set_vtos_entry_points(Template* t,1752address& bep,1753address& cep,1754address& sep,1755address& aep,1756address& iep,1757address& lep,1758address& fep,1759address& dep,1760address& vep) {1761assert(t->is_valid() && t->tos_in() == vtos, "illegal template");1762Label L;1763#ifndef _LP641764fep = __ pc(); // ftos entry point1765__ push(ftos);1766__ jmpb(L);1767dep = __ pc(); // dtos entry point1768__ push(dtos);1769__ jmpb(L);1770#else1771fep = __ pc(); // ftos entry point1772__ push_f(xmm0);1773__ jmpb(L);1774dep = __ pc(); // dtos entry point1775__ push_d(xmm0);1776__ jmpb(L);1777#endif // _LP641778lep = __ pc(); // ltos entry point1779__ push_l();1780__ jmpb(L);1781aep = bep = cep = sep = iep = __ pc(); // [abcsi]tos entry point1782__ push_i_or_ptr();1783vep = __ pc(); // vtos entry point1784__ bind(L);1785generate_and_dispatch(t);1786}
1787
1788//-----------------------------------------------------------------------------
1789
1790// Non-product code
1791#ifndef PRODUCT1792
1793address TemplateInterpreterGenerator::generate_trace_code(TosState state) {1794address entry = __ pc();1795
1796#ifndef _LP641797// prepare expression stack1798__ pop(rcx); // pop return address so expression stack is 'pure'1799__ push(state); // save tosca1800
1801// pass tosca registers as arguments & call tracer1802__ call_VM(noreg, CAST_FROM_FN_PTR(address, InterpreterRuntime::trace_bytecode), rcx, rax, rdx);1803__ mov(rcx, rax); // make sure return address is not destroyed by pop(state)1804__ pop(state); // restore tosca1805
1806// return1807__ jmp(rcx);1808#else1809__ push(state);1810__ push(c_rarg0);1811__ push(c_rarg1);1812__ push(c_rarg2);1813__ push(c_rarg3);1814__ mov(c_rarg2, rax); // Pass itos1815#ifdef _WIN641816__ movflt(xmm3, xmm0); // Pass ftos1817#endif1818__ call_VM(noreg,1819CAST_FROM_FN_PTR(address, InterpreterRuntime::trace_bytecode),1820c_rarg1, c_rarg2, c_rarg3);1821__ pop(c_rarg3);1822__ pop(c_rarg2);1823__ pop(c_rarg1);1824__ pop(c_rarg0);1825__ pop(state);1826__ ret(0); // return from result handler1827#endif // _LP641828
1829return entry;1830}
1831
1832void TemplateInterpreterGenerator::count_bytecode() {1833__ incrementl(ExternalAddress((address) &BytecodeCounter::_counter_value), rscratch1);1834}
1835
1836void TemplateInterpreterGenerator::histogram_bytecode(Template* t) {1837__ incrementl(ExternalAddress((address) &BytecodeHistogram::_counters[t->bytecode()]), rscratch1);1838}
1839
1840void TemplateInterpreterGenerator::histogram_bytecode_pair(Template* t) {1841__ mov32(rbx, ExternalAddress((address) &BytecodePairHistogram::_index));1842__ shrl(rbx, BytecodePairHistogram::log2_number_of_codes);1843__ orl(rbx,1844((int) t->bytecode()) <<1845BytecodePairHistogram::log2_number_of_codes);1846__ mov32(ExternalAddress((address) &BytecodePairHistogram::_index), rbx, rscratch1);1847__ lea(rscratch1, ExternalAddress((address) BytecodePairHistogram::_counters));1848__ incrementl(Address(rscratch1, rbx, Address::times_4));1849}
1850
1851
1852void TemplateInterpreterGenerator::trace_bytecode(Template* t) {1853// Call a little run-time stub to avoid blow-up for each bytecode.1854// The run-time runtime saves the right registers, depending on1855// the tosca in-state for the given template.1856
1857assert(Interpreter::trace_code(t->tos_in()) != nullptr,1858"entry must have been generated");1859#ifndef _LP641860__ call(RuntimeAddress(Interpreter::trace_code(t->tos_in())));1861#else1862__ mov(r12, rsp); // remember sp (can only use r12 if not using call_VM)1863__ andptr(rsp, -16); // align stack as required by ABI1864__ call(RuntimeAddress(Interpreter::trace_code(t->tos_in())));1865__ mov(rsp, r12); // restore sp1866__ reinit_heapbase();1867#endif // _LP641868}
1869
1870
1871void TemplateInterpreterGenerator::stop_interpreter_at() {1872Label L;1873__ cmp32(ExternalAddress((address) &BytecodeCounter::_counter_value),1874StopInterpreterAt,1875rscratch1);1876__ jcc(Assembler::notEqual, L);1877__ int3();1878__ bind(L);1879}
1880#endif // !PRODUCT1881