1 /*
   2  * Copyright (c) 2003, 2025, 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 "asm/macroAssembler.hpp"
  26 #include "classfile/javaClasses.hpp"
  27 #include "compiler/compiler_globals.hpp"
  28 #include "compiler/disassembler.hpp"
  29 #include "gc/shared/barrierSetAssembler.hpp"
  30 #include "interpreter/bytecodeHistogram.hpp"
  31 #include "interpreter/interp_masm.hpp"
  32 #include "interpreter/interpreter.hpp"
  33 #include "interpreter/interpreterRuntime.hpp"
  34 #include "interpreter/templateInterpreterGenerator.hpp"
  35 #include "interpreter/templateTable.hpp"
  36 #include "oops/arrayOop.hpp"
  37 #include "oops/methodCounters.hpp"
  38 #include "oops/methodData.hpp"
  39 #include "oops/method.hpp"
  40 #include "oops/oop.inline.hpp"
  41 #include "oops/inlineKlass.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 AMD64
  68 int TemplateInterpreter::InterpreterCodeSize = JVMCI_ONLY(280) NOT_JVMCI(268) * 1024;
  69 #else
  70 int TemplateInterpreter::InterpreterCodeSize = 224 * 1024;
  71 #endif // AMD64
  72 
  73 // Global Register Names
  74 static const Register rbcp     = LP64_ONLY(r13) NOT_LP64(rsi);
  75 static const Register rlocals  = LP64_ONLY(r14) NOT_LP64(rdi);
  76 
  77 const int method_offset = frame::interpreter_frame_method_offset * wordSize;
  78 const int bcp_offset    = frame::interpreter_frame_bcp_offset    * wordSize;
  79 const int locals_offset = frame::interpreter_frame_locals_offset * wordSize;
  80 
  81 
  82 //-----------------------------------------------------------------------------
  83 
  84 address TemplateInterpreterGenerator::generate_StackOverflowError_handler() {
  85   address entry = __ pc();
  86 
  87 #ifdef ASSERT
  88   {
  89     Label L;
  90     __ movptr(rax, Address(rbp,
  91                            frame::interpreter_frame_monitor_block_top_offset *
  92                            wordSize));
  93     __ lea(rax, Address(rbp, rax, Address::times_ptr));
  94     __ cmpptr(rax, rsp); // rax = maximal rsp for current rbp (stack
  95                          // grows negative)
  96     __ jcc(Assembler::aboveEqual, L); // check if frame is complete
  97     __ stop ("interpreter frame not set up");
  98     __ bind(L);
  99   }
 100 #endif // ASSERT
 101   // Restore bcp under the assumption that the current frame is still
 102   // interpreted
 103   __ restore_bcp();
 104 
 105   // expression stack must be empty before entering the VM if an
 106   // exception happened
 107   __ empty_expression_stack();
 108   // throw exception
 109   __ call_VM(noreg,
 110              CAST_FROM_FN_PTR(address,
 111                               InterpreterRuntime::throw_StackOverflowError));
 112   return entry;
 113 }
 114 
 115 address TemplateInterpreterGenerator::generate_ArrayIndexOutOfBounds_handler() {
 116   address entry = __ pc();
 117   // The expression stack must be empty before entering the VM if an
 118   // 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.
 124   Register rarg = NOT_LP64(rax) LP64_ONLY(c_rarg1);
 125   __ call_VM(noreg,
 126              CAST_FROM_FN_PTR(address,
 127                               InterpreterRuntime::
 128                               throw_ArrayIndexOutOfBoundsException),
 129              rarg, rbx);
 130   return entry;
 131 }
 132 
 133 address TemplateInterpreterGenerator::generate_ClassCastException_handler() {
 134   address entry = __ pc();
 135 
 136   // object is at TOS
 137   Register rarg = NOT_LP64(rax) LP64_ONLY(c_rarg1);
 138   __ pop(rarg);
 139 
 140   // expression stack must be empty before entering the VM if an
 141   // exception happened
 142   __ empty_expression_stack();
 143 
 144   __ call_VM(noreg,
 145              CAST_FROM_FN_PTR(address,
 146                               InterpreterRuntime::
 147                               throw_ClassCastException),
 148              rarg);
 149   return entry;
 150 }
 151 
 152 address TemplateInterpreterGenerator::generate_exception_handler_common(
 153         const char* name, const char* message, bool pass_oop) {
 154   assert(!pass_oop || message == nullptr, "either oop or message but not both");
 155   address entry = __ pc();
 156 
 157   Register rarg = NOT_LP64(rax) LP64_ONLY(c_rarg1);
 158   Register rarg2 = NOT_LP64(rbx) LP64_ONLY(c_rarg2);
 159 
 160   if (pass_oop) {
 161     // object is at TOS
 162     __ pop(rarg2);
 163   }
 164   // expression stack must be empty before entering the VM if an
 165   // exception happened
 166   __ empty_expression_stack();
 167   // setup parameters
 168   __ lea(rarg, ExternalAddress((address)name));
 169   if (pass_oop) {
 170     __ call_VM(rax, CAST_FROM_FN_PTR(address,
 171                                      InterpreterRuntime::
 172                                      create_klass_exception),
 173                rarg, rarg2);
 174   } else {
 175     __ lea(rarg2, ExternalAddress((address)message));
 176     __ call_VM(rax,
 177                CAST_FROM_FN_PTR(address, InterpreterRuntime::create_exception),
 178                rarg, rarg2);
 179   }
 180   // throw exception
 181   __ jump(RuntimeAddress(Interpreter::throw_exception_entry()));
 182   return entry;
 183 }
 184 
 185 address TemplateInterpreterGenerator::generate_return_entry_for(TosState state, int step, size_t index_size) {
 186   address entry = __ pc();
 187 
 188 #ifndef _LP64
 189 #ifdef COMPILER2
 190   // The FPU stack is clean if UseSSE >= 2 but must be cleaned in other cases
 191   if ((state == ftos && UseSSE < 1) || (state == dtos && UseSSE < 2)) {
 192     for (int i = 1; i < 8; i++) {
 193         __ ffree(i);
 194     }
 195   } else if (UseSSE < 2) {
 196     __ empty_FPU_stack();
 197   }
 198 #endif // COMPILER2
 199   if ((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 
 205   if (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 // _LP64
 211 
 212   // Restore stack bottom in case i2c adjusted stack
 213   __ movptr(rscratch1, Address(rbp, frame::interpreter_frame_last_sp_offset * wordSize));
 214   __ lea(rsp, Address(rbp, rscratch1, Address::times_ptr));
 215   // and null it as marker that esp is now tos until next java call
 216   __ movptr(Address(rbp, frame::interpreter_frame_last_sp_offset * wordSize), NULL_WORD);
 217 
 218   if (state == atos && InlineTypeReturnedAsFields) {
 219     __ store_inline_type_fields_to_buf(nullptr);
 220   }
 221 
 222   __ restore_bcp();
 223   __ restore_locals();
 224 
 225   if (state == atos) {
 226     Register mdp = rbx;
 227     Register tmp = rcx;
 228     __ profile_return_type(mdp, rax, tmp);
 229   }
 230 
 231   const Register cache = rbx;
 232   const Register index = rcx;
 233   if (index_size == sizeof(u4)) {
 234     __ load_resolved_indy_entry(cache, index);
 235     __ load_unsigned_short(cache, Address(cache, in_bytes(ResolvedIndyEntry::num_parameters_offset())));
 236     __ lea(rsp, Address(rsp, cache, Interpreter::stackElementScale()));
 237   } else {
 238     assert(index_size == sizeof(u2), "Can only be u2");
 239     __ load_method_entry(cache, index);
 240     __ load_unsigned_short(cache, Address(cache, in_bytes(ResolvedMethodEntry::num_parameters_offset())));
 241     __ lea(rsp, Address(rsp, cache, Interpreter::stackElementScale()));
 242   }
 243 
 244    const Register java_thread = NOT_LP64(rcx) LP64_ONLY(r15_thread);
 245    if (JvmtiExport::can_pop_frame()) {
 246      NOT_LP64(__ get_thread(java_thread));
 247      __ check_and_handle_popframe(java_thread);
 248    }
 249    if (JvmtiExport::can_force_early_return()) {
 250      NOT_LP64(__ get_thread(java_thread));
 251      __ check_and_handle_earlyret(java_thread);
 252    }
 253 
 254   __ dispatch_next(state, step);
 255 
 256   return entry;
 257 }
 258 
 259 
 260 address TemplateInterpreterGenerator::generate_deopt_entry_for(TosState state, int step, address continuation) {
 261   address entry = __ pc();
 262 
 263 #ifndef _LP64
 264   if (state == ftos) {
 265     __ MacroAssembler::verify_FPU(UseSSE >= 1 ? 0 : 1, "generate_deopt_entry_for in interpreter");
 266   } else if (state == dtos) {
 267     __ MacroAssembler::verify_FPU(UseSSE >= 2 ? 0 : 1, "generate_deopt_entry_for in interpreter");
 268   }
 269 #endif // _LP64
 270 
 271   // null last_sp until next java call
 272   __ movptr(Address(rbp, frame::interpreter_frame_last_sp_offset * wordSize), NULL_WORD);
 273   __ restore_bcp();
 274   __ restore_locals();
 275   const Register thread = NOT_LP64(rcx) LP64_ONLY(r15_thread);
 276   NOT_LP64(__ get_thread(thread));
 277 #if INCLUDE_JVMCI
 278   // Check if we need to take lock at entry of synchronized method.  This can
 279   // only occur on method entry so emit it only for vtos with step 0.
 280   if (EnableJVMCI && state == vtos && step == 0) {
 281     Label L;
 282     __ cmpb(Address(thread, JavaThread::pending_monitorenter_offset()), 0);
 283     __ jcc(Assembler::zero, L);
 284     // Clear flag.
 285     __ movb(Address(thread, JavaThread::pending_monitorenter_offset()), 0);
 286     // Satisfy calling convention for lock_method().
 287     __ get_method(rbx);
 288     // Take lock.
 289     lock_method();
 290     __ bind(L);
 291   } else {
 292 #ifdef ASSERT
 293     if (EnableJVMCI) {
 294       Label L;
 295       __ cmpb(Address(r15_thread, JavaThread::pending_monitorenter_offset()), 0);
 296       __ jcc(Assembler::zero, L);
 297       __ stop("unexpected pending monitor in deopt entry");
 298       __ bind(L);
 299     }
 300 #endif
 301   }
 302 #endif
 303   // handle exceptions
 304   {
 305     Label L;
 306     __ cmpptr(Address(thread, Thread::pending_exception_offset()), NULL_WORD);
 307     __ jcc(Assembler::zero, L);
 308     __ call_VM(noreg,
 309                CAST_FROM_FN_PTR(address,
 310                                 InterpreterRuntime::throw_pending_exception));
 311     __ should_not_reach_here();
 312     __ bind(L);
 313   }
 314   if (continuation == nullptr) {
 315     __ dispatch_next(state, step);
 316   } else {
 317     __ jump_to_entry(continuation);
 318   }
 319   return entry;
 320 }
 321 
 322 address TemplateInterpreterGenerator::generate_result_handler_for(
 323         BasicType type) {
 324   address entry = __ pc();
 325   switch (type) {
 326   case T_BOOLEAN: __ c2bool(rax);            break;
 327 #ifndef _LP64
 328   case T_CHAR   : __ andptr(rax, 0xFFFF);    break;
 329 #else
 330   case T_CHAR   : __ movzwl(rax, rax);       break;
 331 #endif // _LP64
 332   case T_BYTE   : __ sign_extend_byte(rax);  break;
 333   case T_SHORT  : __ sign_extend_short(rax); break;
 334   case T_INT    : /* nothing to do */        break;
 335   case T_LONG   : /* nothing to do */        break;
 336   case T_VOID   : /* nothing to do */        break;
 337 #ifndef _LP64
 338   case T_DOUBLE :
 339   case T_FLOAT  :
 340     { const Register t = InterpreterRuntime::SignatureHandlerGenerator::temp();
 341       __ pop(t);                            // remove return address first
 342       // Must return a result for interpreter or compiler. In SSE
 343       // mode, results are returned in xmm0 and the FPU stack must
 344       // be empty.
 345       if (type == T_FLOAT && UseSSE >= 1) {
 346         // Load ST0
 347         __ fld_d(Address(rsp, 0));
 348         // Store as float and empty fpu stack
 349         __ fstp_s(Address(rsp, 0));
 350         // and reload
 351         __ movflt(xmm0, Address(rsp, 0));
 352       } else if (type == T_DOUBLE && UseSSE >= 2 ) {
 353         __ movdbl(xmm0, Address(rsp, 0));
 354       } else {
 355         // restore ST0
 356         __ fld_d(Address(rsp, 0));
 357       }
 358       // and pop the temp
 359       __ addptr(rsp, 2 * wordSize);
 360       __ push(t);                           // restore return address
 361     }
 362     break;
 363 #else
 364   case T_FLOAT  : /* nothing to do */        break;
 365   case T_DOUBLE : /* nothing to do */        break;
 366 #endif // _LP64
 367 
 368   case T_OBJECT :
 369     // retrieve result from frame
 370     __ movptr(rax, Address(rbp, frame::interpreter_frame_oop_temp_offset*wordSize));
 371     // and verify it
 372     __ verify_oop(rax);
 373     break;
 374   default       : ShouldNotReachHere();
 375   }
 376   __ ret(0);                                   // return from result handler
 377   return entry;
 378 }
 379 
 380 address TemplateInterpreterGenerator::generate_safept_entry_for(
 381         TosState state,
 382         address runtime_entry) {
 383   address entry = __ pc();
 384 
 385   __ push(state);
 386   __ push_cont_fastpath();
 387   __ call_VM(noreg, runtime_entry);
 388   __ pop_cont_fastpath();
 389 
 390   __ dispatch_via(vtos, Interpreter::_normal_table.table_for(vtos));
 391   return entry;
 392 }
 393 
 394 address TemplateInterpreterGenerator::generate_cont_resume_interpreter_adapter() {
 395   if (!Continuations::enabled()) return nullptr;
 396   address start = __ pc();
 397 
 398   __ restore_bcp();
 399   __ restore_locals();
 400 
 401   // Get return address before adjusting rsp
 402   __ movptr(rax, Address(rsp, 0));
 403 
 404   // Restore stack bottom
 405   __ movptr(rcx, Address(rbp, frame::interpreter_frame_last_sp_offset * wordSize));
 406   __ lea(rsp, Address(rbp, rcx, Address::times_ptr));
 407   // and null it as marker that esp is now tos until next java call
 408   __ movptr(Address(rbp, frame::interpreter_frame_last_sp_offset * wordSize), NULL_WORD);
 409 
 410   __ jmp(rax);
 411 
 412   return start;
 413 }
 414 
 415 
 416 // Helpers for commoning out cases in the various type of method entries.
 417 //
 418 
 419 
 420 // increment invocation count & check for overflow
 421 //
 422 // Note: checking for negative value instead of overflow
 423 //       so we have a 'sticky' overflow test
 424 //
 425 // rbx: method
 426 // rcx: invocation counter
 427 //
 428 void TemplateInterpreterGenerator::generate_counter_incr(Label* overflow) {
 429   Label done;
 430   // Note: In tiered we increment either counters in Method* or in MDO depending if we're profiling or not.
 431   Label no_mdo;
 432   if (ProfileInterpreter) {
 433     // Are we profiling?
 434     __ movptr(rax, Address(rbx, Method::method_data_offset()));
 435     __ testptr(rax, rax);
 436     __ jccb(Assembler::zero, no_mdo);
 437     // Increment counter in the MDO
 438     const Address mdo_invocation_counter(rax, in_bytes(MethodData::invocation_counter_offset()) +
 439         in_bytes(InvocationCounter::counter_offset()));
 440     const Address mask(rax, in_bytes(MethodData::invoke_mask_offset()));
 441     __ increment_mask_and_jump(mdo_invocation_counter, mask, rcx, overflow);
 442     __ jmp(done);
 443   }
 444   __ bind(no_mdo);
 445   // Increment counter in MethodCounters
 446   const Address invocation_counter(rax,
 447       MethodCounters::invocation_counter_offset() +
 448       InvocationCounter::counter_offset());
 449   __ get_method_counters(rbx, rax, done);
 450   const Address mask(rax, in_bytes(MethodCounters::invoke_mask_offset()));
 451   __ increment_mask_and_jump(invocation_counter, mask, rcx, overflow);
 452   __ bind(done);
 453 }
 454 
 455 void TemplateInterpreterGenerator::generate_counter_overflow(Label& do_continue) {
 456 
 457   // Asm interpreter on entry
 458   // r14/rdi - locals
 459   // r13/rsi - bcp
 460   // rbx - method
 461   // rdx - cpool --- DOES NOT APPEAR TO BE TRUE
 462   // rbp - interpreter frame
 463 
 464   // On return (i.e. jump to entry_point) [ back to invocation of interpreter ]
 465   // Everything as it was on entry
 466   // rdx is not restored. Doesn't appear to really be set.
 467 
 468   // InterpreterRuntime::frequency_counter_overflow takes two
 469   // arguments, the first (thread) is passed by call_VM, the second
 470   // indicates if the counter overflow occurs at a backwards branch
 471   // (null bcp).  We pass zero for it.  The call returns the address
 472   // of the verified entry point for the method or null if the
 473   // compilation did not complete (either went background or bailed
 474   // out).
 475   Register rarg = NOT_LP64(rax) LP64_ONLY(c_rarg1);
 476   __ movl(rarg, 0);
 477   __ call_VM(noreg,
 478              CAST_FROM_FN_PTR(address,
 479                               InterpreterRuntime::frequency_counter_overflow),
 480              rarg);
 481 
 482   __ movptr(rbx, Address(rbp, method_offset));   // restore Method*
 483   // Preserve invariant that r13/r14 contain bcp/locals of sender frame
 484   // and jump to the interpreted entry.
 485   __ jmp(do_continue, relocInfo::none);
 486 }
 487 
 488 // See if we've got enough room on the stack for locals plus overhead below
 489 // JavaThread::stack_overflow_limit(). If not, throw a StackOverflowError
 490 // without going through the signal handler, i.e., reserved and yellow zones
 491 // will not be made usable. The shadow zone must suffice to handle the
 492 // overflow.
 493 // The expression stack grows down incrementally, so the normal guard
 494 // page mechanism will work for that.
 495 //
 496 // NOTE: Since the additional locals are also always pushed (wasn't
 497 // obvious in generate_fixed_frame) so the guard should work for them
 498 // too.
 499 //
 500 // Args:
 501 //      rdx: number of additional locals this frame needs (what we must check)
 502 //      rbx: Method*
 503 //
 504 // Kills:
 505 //      rax
 506 void TemplateInterpreterGenerator::generate_stack_overflow_check(void) {
 507 
 508   // monitor entry size: see picture of stack in frame_x86.hpp
 509   const int entry_size = frame::interpreter_frame_monitor_size_in_bytes();
 510 
 511   // total overhead size: entry_size + (saved rbp through expr stack
 512   // bottom).  be sure to change this if you add/subtract anything
 513   // to/from the overhead area
 514   const int overhead_size =
 515     -(frame::interpreter_frame_initial_sp_offset * wordSize) + entry_size;
 516 
 517   const int page_size = (int)os::vm_page_size();
 518 
 519   Label after_frame_check;
 520 
 521   // see if the frame is greater than one page in size. If so,
 522   // then we need to verify there is enough stack space remaining
 523   // for the additional locals.
 524   __ cmpl(rdx, (page_size - overhead_size) / Interpreter::stackElementSize);
 525   __ jcc(Assembler::belowEqual, after_frame_check);
 526 
 527   // compute rsp as if this were going to be the last frame on
 528   // the stack before the red zone
 529 
 530   Label after_frame_check_pop;
 531   const Register thread = NOT_LP64(rsi) LP64_ONLY(r15_thread);
 532 #ifndef _LP64
 533   __ push(thread);
 534   __ get_thread(thread);
 535 #endif
 536 
 537   const Address stack_limit(thread, JavaThread::stack_overflow_limit_offset());
 538 
 539   // locals + overhead, in bytes
 540   __ mov(rax, rdx);
 541   __ shlptr(rax, Interpreter::logStackElementSize); // Convert parameter count to bytes.
 542   __ addptr(rax, overhead_size);
 543 
 544 #ifdef ASSERT
 545   Label limit_okay;
 546   // Verify that thread stack overflow limit is non-zero.
 547   __ cmpptr(stack_limit, NULL_WORD);
 548   __ jcc(Assembler::notEqual, limit_okay);
 549   __ stop("stack overflow limit is zero");
 550   __ bind(limit_okay);
 551 #endif
 552 
 553   // Add locals/frame size to stack limit.
 554   __ addptr(rax, stack_limit);
 555 
 556   // Check against the current stack bottom.
 557   __ cmpptr(rsp, rax);
 558 
 559   __ jcc(Assembler::above, after_frame_check_pop);
 560   NOT_LP64(__ pop(rsi));  // get saved bcp
 561 
 562   // Restore sender's sp as SP. This is necessary if the sender's
 563   // frame is an extended compiled frame (see gen_c2i_adapter())
 564   // and safer anyway in case of JSR292 adaptations.
 565 
 566   __ pop(rax); // return address must be moved if SP is changed
 567   __ mov(rsp, rbcp);
 568   __ push(rax);
 569 
 570   // Note: the restored frame is not necessarily interpreted.
 571   // Use the shared runtime version of the StackOverflowError.
 572   assert(SharedRuntime::throw_StackOverflowError_entry() != nullptr, "stub not yet generated");
 573   __ jump(RuntimeAddress(SharedRuntime::throw_StackOverflowError_entry()));
 574   // all done with frame size check
 575   __ bind(after_frame_check_pop);
 576   NOT_LP64(__ pop(rsi));
 577 
 578   // all done with frame size check
 579   __ bind(after_frame_check);
 580 }
 581 
 582 // Allocate monitor and lock method (asm interpreter)
 583 //
 584 // Args:
 585 //      rbx: Method*
 586 //      r14/rdi: locals
 587 //
 588 // Kills:
 589 //      rax
 590 //      c_rarg0, c_rarg1, c_rarg2, c_rarg3, ...(param regs)
 591 //      rscratch1, rscratch2 (scratch regs)
 592 void TemplateInterpreterGenerator::lock_method() {
 593   // synchronize method
 594   const Address access_flags(rbx, Method::access_flags_offset());
 595   const Address monitor_block_top(
 596         rbp,
 597         frame::interpreter_frame_monitor_block_top_offset * wordSize);
 598   const int entry_size = frame::interpreter_frame_monitor_size_in_bytes();
 599 
 600 #ifdef ASSERT
 601   {
 602     Label L;
 603     __ load_unsigned_short(rax, access_flags);
 604     __ testl(rax, JVM_ACC_SYNCHRONIZED);
 605     __ jcc(Assembler::notZero, L);
 606     __ stop("method doesn't need synchronization");
 607     __ bind(L);
 608   }
 609 #endif // ASSERT
 610 
 611   // get synchronization object
 612   {
 613     Label done;
 614     __ load_unsigned_short(rax, access_flags);
 615     __ testl(rax, JVM_ACC_STATIC);
 616     // get receiver (assume this is frequent case)
 617     __ movptr(rax, Address(rlocals, Interpreter::local_offset_in_bytes(0)));
 618     __ jcc(Assembler::zero, done);
 619     __ load_mirror(rax, rbx, rscratch2);
 620 
 621 #ifdef ASSERT
 622     {
 623       Label L;
 624       __ testptr(rax, rax);
 625       __ jcc(Assembler::notZero, L);
 626       __ stop("synchronization object is null");
 627       __ bind(L);
 628     }
 629 #endif // ASSERT
 630 
 631     __ bind(done);
 632   }
 633 
 634   // add space for monitor & lock
 635   __ subptr(rsp, entry_size); // add space for a monitor entry
 636   __ subptr(monitor_block_top, entry_size / wordSize); // set new monitor block top
 637   // store object
 638   __ movptr(Address(rsp, BasicObjectLock::obj_offset()), rax);
 639   const Register lockreg = NOT_LP64(rdx) LP64_ONLY(c_rarg1);
 640   __ movptr(lockreg, rsp); // object address
 641   __ lock_object(lockreg);
 642 }
 643 
 644 // Generate a fixed interpreter frame. This is identical setup for
 645 // interpreted methods and for native methods hence the shared code.
 646 //
 647 // Args:
 648 //      rax: return address
 649 //      rbx: Method*
 650 //      r14/rdi: pointer to locals
 651 //      r13/rsi: sender sp
 652 //      rdx: cp cache
 653 void TemplateInterpreterGenerator::generate_fixed_frame(bool native_call) {
 654   // initialize fixed part of activation frame
 655   __ push(rax);        // save return address
 656   __ enter();          // save old & set new rbp
 657   __ push(rbcp);        // set sender sp
 658   __ push(NULL_WORD); // leave last_sp as null
 659   __ movptr(rbcp, Address(rbx, Method::const_offset()));      // get ConstMethod*
 660   __ lea(rbcp, Address(rbcp, ConstMethod::codes_offset())); // get codebase
 661   __ push(rbx);        // save Method*
 662   // Get mirror and store it in the frame as GC root for this Method*
 663   __ load_mirror(rdx, rbx, rscratch2);
 664   __ push(rdx);
 665   if (ProfileInterpreter) {
 666     Label method_data_continue;
 667     __ movptr(rdx, Address(rbx, in_bytes(Method::method_data_offset())));
 668     __ testptr(rdx, rdx);
 669     __ jcc(Assembler::zero, method_data_continue);
 670     __ addptr(rdx, in_bytes(MethodData::data_offset()));
 671     __ bind(method_data_continue);
 672     __ push(rdx);      // set the mdp (method data pointer)
 673   } else {
 674     __ push(0);
 675   }
 676 
 677   __ movptr(rdx, Address(rbx, Method::const_offset()));
 678   __ movptr(rdx, Address(rdx, ConstMethod::constants_offset()));
 679   __ movptr(rdx, Address(rdx, ConstantPool::cache_offset()));
 680   __ push(rdx); // set constant pool cache
 681 
 682   __ movptr(rax, rlocals);
 683   __ subptr(rax, rbp);
 684   __ shrptr(rax, Interpreter::logStackElementSize);  // rax = rlocals - fp();
 685   __ push(rax); // set relativized rlocals, see frame::interpreter_frame_locals()
 686 
 687   if (native_call) {
 688     __ push(0); // no bcp
 689   } else {
 690     __ push(rbcp); // set bcp
 691   }
 692   // initialize relativized pointer to expression stack bottom
 693   __ push(frame::interpreter_frame_initial_sp_offset);
 694 }
 695 
 696 // End of helpers
 697 
 698 // Method entry for java.lang.ref.Reference.get.
 699 address TemplateInterpreterGenerator::generate_Reference_get_entry(void) {
 700   // Code: _aload_0, _getfield, _areturn
 701   // parameter size = 1
 702   //
 703   // The code that gets generated by this routine is split into 2 parts:
 704   //    1. The "intrinsified" code performing an ON_WEAK_OOP_REF load,
 705   //    2. The slow path - which is an expansion of the regular method entry.
 706   //
 707   // Notes:-
 708   // * An intrinsic is always executed, where an ON_WEAK_OOP_REF load is performed.
 709   // * We may jump to the slow path iff the receiver is null. If the
 710   //   Reference object is null then we no longer perform an ON_WEAK_OOP_REF load
 711   //   Thus we can use the regular method entry code to generate the NPE.
 712   //
 713   // rbx: Method*
 714 
 715   // r13: senderSP must preserve for slow path, set SP to it on fast path
 716 
 717   address entry = __ pc();
 718 
 719   const int referent_offset = java_lang_ref_Reference::referent_offset();
 720 
 721   Label slow_path;
 722   // rbx: method
 723 
 724   // Check if local 0 != null
 725   // If the receiver is null then it is OK to jump to the slow path.
 726   __ movptr(rax, Address(rsp, wordSize));
 727 
 728   __ testptr(rax, rax);
 729   __ jcc(Assembler::zero, slow_path);
 730 
 731   // rax: local 0
 732   // rbx: method (but can be used as scratch now)
 733   // rdx: scratch
 734   // rdi: scratch
 735 
 736   // Preserve the sender sp in case the load barrier
 737   // calls the runtime
 738   NOT_LP64(__ push(rsi));
 739 
 740   // Load the value of the referent field.
 741   const Address field_address(rax, referent_offset);
 742   __ load_heap_oop(rax, field_address, /*tmp1*/ rbx, /*tmp_thread*/ rdx, ON_WEAK_OOP_REF);
 743 
 744   // _areturn
 745   const Register sender_sp = NOT_LP64(rsi) LP64_ONLY(r13);
 746   NOT_LP64(__ pop(rsi));      // get sender sp
 747   __ pop(rdi);                // get return address
 748   __ mov(rsp, sender_sp);     // set sp to sender sp
 749   __ jmp(rdi);
 750   __ ret(0);
 751 
 752   // generate a vanilla interpreter entry as the slow path
 753   __ bind(slow_path);
 754   __ jump_to_entry(Interpreter::entry_for_kind(Interpreter::zerolocals));
 755   return entry;
 756 }
 757 
 758 void TemplateInterpreterGenerator::bang_stack_shadow_pages(bool native_call) {
 759   // See more discussion in stackOverflow.hpp.
 760 
 761   // Note that we do the banging after the frame is setup, since the exception
 762   // handling code expects to find a valid interpreter frame on the stack.
 763   // Doing the banging earlier fails if the caller frame is not an interpreter
 764   // frame.
 765   // (Also, the exception throwing code expects to unlock any synchronized
 766   // method receiver, so do the banging after locking the receiver.)
 767 
 768   const int shadow_zone_size = checked_cast<int>(StackOverflow::stack_shadow_zone_size());
 769   const int page_size = (int)os::vm_page_size();
 770   const int n_shadow_pages = shadow_zone_size / page_size;
 771 
 772   const Register thread = NOT_LP64(rsi) LP64_ONLY(r15_thread);
 773 #ifndef _LP64
 774   __ push(thread);
 775   __ get_thread(thread);
 776 #endif
 777 
 778 #ifdef ASSERT
 779   Label L_good_limit;
 780   __ cmpptr(Address(thread, JavaThread::shadow_zone_safe_limit()), NULL_WORD);
 781   __ jcc(Assembler::notEqual, L_good_limit);
 782   __ stop("shadow zone safe limit is not initialized");
 783   __ bind(L_good_limit);
 784 
 785   Label L_good_watermark;
 786   __ cmpptr(Address(thread, JavaThread::shadow_zone_growth_watermark()), NULL_WORD);
 787   __ jcc(Assembler::notEqual, L_good_watermark);
 788   __ stop("shadow zone growth watermark is not initialized");
 789   __ bind(L_good_watermark);
 790 #endif
 791 
 792   Label L_done;
 793 
 794   __ cmpptr(rsp, Address(thread, JavaThread::shadow_zone_growth_watermark()));
 795   __ jcc(Assembler::above, L_done);
 796 
 797   for (int p = 1; p <= n_shadow_pages; p++) {
 798     __ bang_stack_with_offset(p*page_size);
 799   }
 800 
 801   // Record the new watermark, but only if update is above the safe limit.
 802   // Otherwise, the next time around the check above would pass the safe limit.
 803   __ cmpptr(rsp, Address(thread, JavaThread::shadow_zone_safe_limit()));
 804   __ jccb(Assembler::belowEqual, L_done);
 805   __ movptr(Address(thread, JavaThread::shadow_zone_growth_watermark()), rsp);
 806 
 807   __ bind(L_done);
 808 
 809 #ifndef _LP64
 810   __ pop(thread);
 811 #endif
 812 }
 813 
 814 // Interpreter stub for calling a native method. (asm interpreter)
 815 // This sets up a somewhat different looking stack for calling the
 816 // native method than the typical interpreter frame setup.
 817 address TemplateInterpreterGenerator::generate_native_entry(bool synchronized) {
 818   // determine code generation flags
 819   bool inc_counter  = UseCompiler || CountCompiledCalls;
 820 
 821   // rbx: Method*
 822   // rbcp: sender sp
 823 
 824   address entry_point = __ pc();
 825 
 826   const Address constMethod       (rbx, Method::const_offset());
 827   const Address access_flags      (rbx, Method::access_flags_offset());
 828   const Address size_of_parameters(rcx, ConstMethod::
 829                                         size_of_parameters_offset());
 830 
 831 
 832   // get parameter size (always needed)
 833   __ movptr(rcx, constMethod);
 834   __ load_unsigned_short(rcx, size_of_parameters);
 835 
 836   // native calls don't need the stack size check since they have no
 837   // expression stack and the arguments are already on the stack and
 838   // we only add a handful of words to the stack
 839 
 840   // rbx: Method*
 841   // rcx: size of parameters
 842   // rbcp: sender sp
 843   __ pop(rax);                                       // get return address
 844 
 845   // for natives the size of locals is zero
 846 
 847   // compute beginning of parameters
 848   __ lea(rlocals, Address(rsp, rcx, Interpreter::stackElementScale(), -wordSize));
 849 
 850   // add 2 zero-initialized slots for native calls
 851   // initialize result_handler slot
 852   __ push(NULL_WORD);
 853   // slot for oop temp
 854   // (static native method holder mirror/jni oop result)
 855   __ push(NULL_WORD);
 856 
 857   // initialize fixed part of activation frame
 858   generate_fixed_frame(true);
 859 
 860   // make sure method is native & not abstract
 861 #ifdef ASSERT
 862   __ load_unsigned_short(rax, access_flags);
 863   {
 864     Label L;
 865     __ testl(rax, JVM_ACC_NATIVE);
 866     __ jcc(Assembler::notZero, L);
 867     __ stop("tried to execute non-native method as native");
 868     __ bind(L);
 869   }
 870   {
 871     Label L;
 872     __ testl(rax, JVM_ACC_ABSTRACT);
 873     __ jcc(Assembler::zero, L);
 874     __ stop("tried to execute abstract method in interpreter");
 875     __ bind(L);
 876   }
 877 #endif
 878 
 879   // Since at this point in the method invocation the exception handler
 880   // would try to exit the monitor of synchronized methods which hasn't
 881   // been entered yet, we set the thread local variable
 882   // _do_not_unlock_if_synchronized to true. The remove_activation will
 883   // check this flag.
 884 
 885   const Register thread1 = NOT_LP64(rax) LP64_ONLY(r15_thread);
 886   NOT_LP64(__ get_thread(thread1));
 887   const Address do_not_unlock_if_synchronized(thread1,
 888         in_bytes(JavaThread::do_not_unlock_if_synchronized_offset()));
 889   __ movbool(do_not_unlock_if_synchronized, true);
 890 
 891   // increment invocation count & check for overflow
 892   Label invocation_counter_overflow;
 893   if (inc_counter) {
 894     generate_counter_incr(&invocation_counter_overflow);
 895   }
 896 
 897   Label continue_after_compile;
 898   __ bind(continue_after_compile);
 899 
 900   bang_stack_shadow_pages(true);
 901 
 902   // reset the _do_not_unlock_if_synchronized flag
 903   NOT_LP64(__ get_thread(thread1));
 904   __ movbool(do_not_unlock_if_synchronized, false);
 905 
 906   // check for synchronized methods
 907   // Must happen AFTER invocation_counter check and stack overflow check,
 908   // so method is not locked if overflows.
 909   if (synchronized) {
 910     lock_method();
 911   } else {
 912     // no synchronization necessary
 913 #ifdef ASSERT
 914     {
 915       Label L;
 916       __ load_unsigned_short(rax, access_flags);
 917       __ testl(rax, JVM_ACC_SYNCHRONIZED);
 918       __ jcc(Assembler::zero, L);
 919       __ stop("method needs synchronization");
 920       __ bind(L);
 921     }
 922 #endif
 923   }
 924 
 925   // start execution
 926 #ifdef ASSERT
 927   {
 928     Label L;
 929     const Address monitor_block_top(rbp,
 930                  frame::interpreter_frame_monitor_block_top_offset * wordSize);
 931     __ movptr(rax, monitor_block_top);
 932     __ lea(rax, Address(rbp, rax, Address::times_ptr));
 933     __ cmpptr(rax, rsp);
 934     __ jcc(Assembler::equal, L);
 935     __ stop("broken stack frame setup in interpreter 5");
 936     __ bind(L);
 937   }
 938 #endif
 939 
 940   // jvmti support
 941   __ notify_method_entry();
 942 
 943   // work registers
 944   const Register method = rbx;
 945   const Register thread = NOT_LP64(rdi) LP64_ONLY(r15_thread);
 946   const Register t      = NOT_LP64(rcx) LP64_ONLY(r11);
 947 
 948   // allocate space for parameters
 949   __ get_method(method);
 950   __ movptr(t, Address(method, Method::const_offset()));
 951   __ load_unsigned_short(t, Address(t, ConstMethod::size_of_parameters_offset()));
 952 
 953 #ifndef _LP64
 954   __ shlptr(t, Interpreter::logStackElementSize); // Convert parameter count to bytes.
 955   __ addptr(t, 2*wordSize);     // allocate two more slots for JNIEnv and possible mirror
 956   __ subptr(rsp, t);
 957   __ andptr(rsp, -(StackAlignmentInBytes)); // gcc needs 16 byte aligned stacks to do XMM intrinsics
 958 #else
 959   __ shll(t, Interpreter::logStackElementSize);
 960 
 961   __ subptr(rsp, t);
 962   __ subptr(rsp, frame::arg_reg_save_area_bytes); // windows
 963   __ andptr(rsp, -16); // must be 16 byte boundary (see amd64 ABI)
 964 #endif // _LP64
 965 
 966   // get signature handler
 967   {
 968     Label L;
 969     __ movptr(t, Address(method, Method::signature_handler_offset()));
 970     __ testptr(t, t);
 971     __ jcc(Assembler::notZero, L);
 972     __ call_VM(noreg,
 973                CAST_FROM_FN_PTR(address,
 974                                 InterpreterRuntime::prepare_native_call),
 975                method);
 976     __ get_method(method);
 977     __ movptr(t, Address(method, Method::signature_handler_offset()));
 978     __ bind(L);
 979   }
 980 
 981   // call signature handler
 982   assert(InterpreterRuntime::SignatureHandlerGenerator::from() == rlocals,
 983          "adjust this code");
 984   assert(InterpreterRuntime::SignatureHandlerGenerator::to() == rsp,
 985          "adjust this code");
 986   assert(InterpreterRuntime::SignatureHandlerGenerator::temp() == NOT_LP64(t) LP64_ONLY(rscratch1),
 987          "adjust this code");
 988 
 989   // The generated handlers do not touch RBX (the method).
 990   // However, large signatures cannot be cached and are generated
 991   // each time here.  The slow-path generator can do a GC on return,
 992   // so we must reload it after the call.
 993   __ call(t);
 994   __ get_method(method);        // slow path can do a GC, reload RBX
 995 
 996 
 997   // result handler is in rax
 998   // set result handler
 999   __ movptr(Address(rbp,
1000                     (frame::interpreter_frame_result_handler_offset) * wordSize),
1001             rax);
1002 
1003   // pass mirror handle if static call
1004   {
1005     Label L;
1006     __ load_unsigned_short(t, Address(method, Method::access_flags_offset()));
1007     __ testl(t, JVM_ACC_STATIC);
1008     __ jcc(Assembler::zero, L);
1009     // get mirror
1010     __ load_mirror(t, method, rax);
1011     // copy mirror into activation frame
1012     __ movptr(Address(rbp, frame::interpreter_frame_oop_temp_offset * wordSize),
1013             t);
1014     // pass handle to mirror
1015 #ifndef _LP64
1016     __ lea(t, Address(rbp, frame::interpreter_frame_oop_temp_offset * wordSize));
1017     __ movptr(Address(rsp, wordSize), t);
1018 #else
1019     __ lea(c_rarg1,
1020            Address(rbp, frame::interpreter_frame_oop_temp_offset * wordSize));
1021 #endif // _LP64
1022     __ bind(L);
1023   }
1024 
1025   // get native function entry point
1026   {
1027     Label L;
1028     __ movptr(rax, Address(method, Method::native_function_offset()));
1029     ExternalAddress unsatisfied(SharedRuntime::native_method_throw_unsatisfied_link_error_entry());
1030     __ cmpptr(rax, unsatisfied.addr(), rscratch1);
1031     __ jcc(Assembler::notEqual, L);
1032     __ call_VM(noreg,
1033                CAST_FROM_FN_PTR(address,
1034                                 InterpreterRuntime::prepare_native_call),
1035                method);
1036     __ get_method(method);
1037     __ movptr(rax, Address(method, Method::native_function_offset()));
1038     __ bind(L);
1039   }
1040 
1041   // pass JNIEnv
1042 #ifndef _LP64
1043    __ get_thread(thread);
1044    __ lea(t, Address(thread, JavaThread::jni_environment_offset()));
1045    __ movptr(Address(rsp, 0), t);
1046 
1047    // set_last_Java_frame_before_call
1048    // It is enough that the pc()
1049    // points into the right code segment. It does not have to be the correct return pc.
1050    __ set_last_Java_frame(thread, noreg, rbp, __ pc(), noreg);
1051 #else
1052    __ lea(c_rarg0, Address(r15_thread, JavaThread::jni_environment_offset()));
1053 
1054    // It is enough that the pc() points into the right code
1055    // segment. It does not have to be the correct return pc.
1056    // For convenience we use the pc we want to resume to in
1057    // case of preemption on Object.wait.
1058    Label native_return;
1059    __ set_last_Java_frame(rsp, rbp, native_return, rscratch1);
1060 #endif // _LP64
1061 
1062   // change thread state
1063 #ifdef ASSERT
1064   {
1065     Label L;
1066     __ movl(t, Address(thread, JavaThread::thread_state_offset()));
1067     __ cmpl(t, _thread_in_Java);
1068     __ jcc(Assembler::equal, L);
1069     __ stop("Wrong thread state in native stub");
1070     __ bind(L);
1071   }
1072 #endif
1073 
1074   // Change state to native
1075 
1076   __ movl(Address(thread, JavaThread::thread_state_offset()),
1077           _thread_in_native);
1078 
1079   __ push_cont_fastpath();
1080 
1081   // Call the native method.
1082   __ call(rax);
1083   // 32: result potentially in rdx:rax or ST0
1084   // 64: result potentially in rax or xmm0
1085 
1086   __ pop_cont_fastpath();
1087 
1088   // Verify or restore cpu control state after JNI call
1089   __ restore_cpu_control_state_after_jni(rscratch1);
1090 
1091   // NOTE: The order of these pushes is known to frame::interpreter_frame_result
1092   // in order to extract the result of a method call. If the order of these
1093   // pushes change or anything else is added to the stack then the code in
1094   // interpreter_frame_result must also change.
1095 
1096 #ifndef _LP64
1097   // save potential result in ST(0) & rdx:rax
1098   // (if result handler is the T_FLOAT or T_DOUBLE handler, result must be in ST0 -
1099   // the check is necessary to avoid potential Intel FPU overflow problems by saving/restoring 'empty' FPU registers)
1100   // It is safe to do this push because state is _thread_in_native and return address will be found
1101   // via _last_native_pc and not via _last_jave_sp
1102 
1103   // NOTE: the order of these push(es) is known to frame::interpreter_frame_result.
1104   // If the order changes or anything else is added to the stack the code in
1105   // interpreter_frame_result will have to be changed.
1106 
1107   { Label L;
1108     Label push_double;
1109     ExternalAddress float_handler(AbstractInterpreter::result_handler(T_FLOAT));
1110     ExternalAddress double_handler(AbstractInterpreter::result_handler(T_DOUBLE));
1111     __ cmpptr(Address(rbp, (frame::interpreter_frame_result_handler_offset)*wordSize),
1112               float_handler.addr(), noreg);
1113     __ jcc(Assembler::equal, push_double);
1114     __ cmpptr(Address(rbp, (frame::interpreter_frame_result_handler_offset)*wordSize),
1115               double_handler.addr(), noreg);
1116     __ jcc(Assembler::notEqual, L);
1117     __ bind(push_double);
1118     __ push_d(); // FP values are returned using the FPU, so push FPU contents (even if UseSSE > 0).
1119     __ bind(L);
1120   }
1121 #else
1122   __ push(dtos);
1123 #endif // _LP64
1124 
1125   __ push(ltos);
1126 
1127   // change thread state
1128   NOT_LP64(__ get_thread(thread));
1129   __ movl(Address(thread, JavaThread::thread_state_offset()),
1130           _thread_in_native_trans);
1131 
1132   // Force this write out before the read below
1133   if (!UseSystemMemoryBarrier) {
1134     __ membar(Assembler::Membar_mask_bits(
1135                 Assembler::LoadLoad | Assembler::LoadStore |
1136                 Assembler::StoreLoad | Assembler::StoreStore));
1137   }
1138 #ifndef _LP64
1139   if (AlwaysRestoreFPU) {
1140     //  Make sure the control word is correct.
1141     __ fldcw(ExternalAddress(StubRoutines::x86::addr_fpu_cntrl_wrd_std()));
1142   }
1143 #endif // _LP64
1144 
1145   // check for safepoint operation in progress and/or pending suspend requests
1146   {
1147     Label Continue;
1148     Label slow_path;
1149 
1150     __ safepoint_poll(slow_path, thread, true /* at_return */, false /* in_nmethod */);
1151 
1152     __ cmpl(Address(thread, JavaThread::suspend_flags_offset()), 0);
1153     __ jcc(Assembler::equal, Continue);
1154     __ bind(slow_path);
1155 
1156     // Don't use call_VM as it will see a possible pending exception
1157     // and forward it and never return here preventing us from
1158     // clearing _last_native_pc down below.  Also can't use
1159     // call_VM_leaf either as it will check to see if r13 & r14 are
1160     // preserved and correspond to the bcp/locals pointers. So we do a
1161     // runtime call by hand.
1162     //
1163 #ifndef _LP64
1164     __ push(thread);
1165     __ call(RuntimeAddress(CAST_FROM_FN_PTR(address,
1166                                             JavaThread::check_special_condition_for_native_trans)));
1167     __ increment(rsp, wordSize);
1168     __ get_thread(thread);
1169 #else
1170     __ mov(c_rarg0, r15_thread);
1171     __ mov(r12, rsp); // remember sp (can only use r12 if not using call_VM)
1172     __ subptr(rsp, frame::arg_reg_save_area_bytes); // windows
1173     __ andptr(rsp, -16); // align stack as required by ABI
1174     __ call(RuntimeAddress(CAST_FROM_FN_PTR(address, JavaThread::check_special_condition_for_native_trans)));
1175     __ mov(rsp, r12); // restore sp
1176     __ reinit_heapbase();
1177 #endif // _LP64
1178     __ bind(Continue);
1179   }
1180 
1181   // change thread state
1182   __ movl(Address(thread, JavaThread::thread_state_offset()), _thread_in_Java);
1183 
1184 #ifdef _LP64
1185   if (LockingMode != LM_LEGACY) {
1186     // Check preemption for Object.wait()
1187     Label not_preempted;
1188     __ movptr(rscratch1, Address(r15_thread, JavaThread::preempt_alternate_return_offset()));
1189     __ cmpptr(rscratch1, NULL_WORD);
1190     __ jccb(Assembler::equal, not_preempted);
1191     __ movptr(Address(r15_thread, JavaThread::preempt_alternate_return_offset()), NULL_WORD);
1192     __ jmp(rscratch1);
1193     __ bind(native_return);
1194     __ restore_after_resume(true /* is_native */);
1195     __ bind(not_preempted);
1196   } else {
1197     // any pc will do so just use this one for LM_LEGACY to keep code together.
1198     __ bind(native_return);
1199   }
1200 #endif // _LP64
1201 
1202   // reset_last_Java_frame
1203   __ reset_last_Java_frame(thread, true);
1204 
1205   if (CheckJNICalls) {
1206     // clear_pending_jni_exception_check
1207     __ movptr(Address(thread, JavaThread::pending_jni_exception_check_fn_offset()), NULL_WORD);
1208   }
1209 
1210   // reset handle block
1211   __ movptr(t, Address(thread, JavaThread::active_handles_offset()));
1212   __ movl(Address(t, JNIHandleBlock::top_offset()), NULL_WORD);
1213 
1214   // If result is an oop unbox and store it in frame where gc will see it
1215   // and result handler will pick it up
1216 
1217   {
1218     Label no_oop;
1219     __ lea(t, ExternalAddress(AbstractInterpreter::result_handler(T_OBJECT)));
1220     __ cmpptr(t, Address(rbp, frame::interpreter_frame_result_handler_offset*wordSize));
1221     __ jcc(Assembler::notEqual, no_oop);
1222     // retrieve result
1223     __ pop(ltos);
1224     // Unbox oop result, e.g. JNIHandles::resolve value.
1225     __ resolve_jobject(rax /* value */,
1226                        thread /* thread */,
1227                        t /* tmp */);
1228     __ movptr(Address(rbp, frame::interpreter_frame_oop_temp_offset*wordSize), rax);
1229     // keep stack depth as expected by pushing oop which will eventually be discarded
1230     __ push(ltos);
1231     __ bind(no_oop);
1232   }
1233 
1234 
1235   {
1236     Label no_reguard;
1237     __ cmpl(Address(thread, JavaThread::stack_guard_state_offset()),
1238             StackOverflow::stack_guard_yellow_reserved_disabled);
1239     __ jcc(Assembler::notEqual, no_reguard);
1240 
1241     __ pusha(); // XXX only save smashed registers
1242 #ifndef _LP64
1243     __ call(RuntimeAddress(CAST_FROM_FN_PTR(address, SharedRuntime::reguard_yellow_pages)));
1244     __ popa();
1245 #else
1246     __ mov(r12, rsp); // remember sp (can only use r12 if not using call_VM)
1247     __ subptr(rsp, frame::arg_reg_save_area_bytes); // windows
1248     __ andptr(rsp, -16); // align stack as required by ABI
1249     __ call(RuntimeAddress(CAST_FROM_FN_PTR(address, SharedRuntime::reguard_yellow_pages)));
1250     __ mov(rsp, r12); // restore sp
1251     __ popa(); // XXX only restore smashed registers
1252     __ reinit_heapbase();
1253 #endif // _LP64
1254 
1255     __ bind(no_reguard);
1256   }
1257 
1258 
1259   // The method register is junk from after the thread_in_native transition
1260   // until here.  Also can't call_VM until the bcp has been
1261   // restored.  Need bcp for throwing exception below so get it now.
1262   __ get_method(method);
1263 
1264   // restore to have legal interpreter frame, i.e., bci == 0 <=> code_base()
1265   __ movptr(rbcp, Address(method, Method::const_offset()));   // get ConstMethod*
1266   __ lea(rbcp, Address(rbcp, ConstMethod::codes_offset()));    // get codebase
1267 
1268   // handle exceptions (exception handling will handle unlocking!)
1269   {
1270     Label L;
1271     __ cmpptr(Address(thread, Thread::pending_exception_offset()), NULL_WORD);
1272     __ jcc(Assembler::zero, L);
1273     // Note: At some point we may want to unify this with the code
1274     // used in call_VM_base(); i.e., we should use the
1275     // StubRoutines::forward_exception code. For now this doesn't work
1276     // here because the rsp is not correctly set at this point.
1277     __ MacroAssembler::call_VM(noreg,
1278                                CAST_FROM_FN_PTR(address,
1279                                InterpreterRuntime::throw_pending_exception));
1280     __ should_not_reach_here();
1281     __ bind(L);
1282   }
1283 
1284   // do unlocking if necessary
1285   {
1286     Label L;
1287     __ load_unsigned_short(t, Address(method, Method::access_flags_offset()));
1288     __ testl(t, JVM_ACC_SYNCHRONIZED);
1289     __ jcc(Assembler::zero, L);
1290     // the code below should be shared with interpreter macro
1291     // assembler implementation
1292     {
1293       Label unlock;
1294       // BasicObjectLock will be first in list, since this is a
1295       // synchronized method. However, need to check that the object
1296       // has not been unlocked by an explicit monitorexit bytecode.
1297       const Address monitor(rbp,
1298                             (intptr_t)(frame::interpreter_frame_initial_sp_offset *
1299                                        wordSize - (int)sizeof(BasicObjectLock)));
1300 
1301       const Register regmon = NOT_LP64(rdx) LP64_ONLY(c_rarg1);
1302 
1303       // monitor expect in c_rarg1 for slow unlock path
1304       __ lea(regmon, monitor); // address of first monitor
1305 
1306       __ movptr(t, Address(regmon, BasicObjectLock::obj_offset()));
1307       __ testptr(t, t);
1308       __ jcc(Assembler::notZero, unlock);
1309 
1310       // Entry already unlocked, need to throw exception
1311       __ MacroAssembler::call_VM(noreg,
1312                                  CAST_FROM_FN_PTR(address,
1313                    InterpreterRuntime::throw_illegal_monitor_state_exception));
1314       __ should_not_reach_here();
1315 
1316       __ bind(unlock);
1317       __ unlock_object(regmon);
1318     }
1319     __ bind(L);
1320   }
1321 
1322   // jvmti support
1323   // Note: This must happen _after_ handling/throwing any exceptions since
1324   //       the exception handler code notifies the runtime of method exits
1325   //       too. If this happens before, method entry/exit notifications are
1326   //       not properly paired (was bug - gri 11/22/99).
1327   __ notify_method_exit(vtos, InterpreterMacroAssembler::NotifyJVMTI);
1328 
1329   // restore potential result in edx:eax, call result handler to
1330   // restore potential result in ST0 & handle result
1331 
1332   __ pop(ltos);
1333   LP64_ONLY( __ pop(dtos));
1334 
1335   __ movptr(t, Address(rbp,
1336                        (frame::interpreter_frame_result_handler_offset) * wordSize));
1337   __ call(t);
1338 
1339   // remove activation
1340   __ movptr(t, Address(rbp,
1341                        frame::interpreter_frame_sender_sp_offset *
1342                        wordSize)); // get sender sp
1343   __ leave();                                // remove frame anchor
1344   __ pop(rdi);                               // get return address
1345   __ mov(rsp, t);                            // set sp to sender sp
1346   __ jmp(rdi);
1347 
1348   if (inc_counter) {
1349     // Handle overflow of counter and compile method
1350     __ bind(invocation_counter_overflow);
1351     generate_counter_overflow(continue_after_compile);
1352   }
1353 
1354   return entry_point;
1355 }
1356 
1357 // Abstract method entry
1358 // Attempt to execute abstract method. Throw exception
1359 address TemplateInterpreterGenerator::generate_abstract_entry(void) {
1360 
1361   address entry_point = __ pc();
1362 
1363   // abstract method entry
1364 
1365   //  pop return address, reset last_sp to null
1366   __ empty_expression_stack();
1367   __ restore_bcp();      // rsi must be correct for exception handler   (was destroyed)
1368   __ restore_locals();   // make sure locals pointer is correct as well (was destroyed)
1369 
1370   // throw exception
1371   __ call_VM(noreg, CAST_FROM_FN_PTR(address, InterpreterRuntime::throw_AbstractMethodErrorWithMethod), rbx);
1372   // the call_VM checks for exception, so we should never return here.
1373   __ should_not_reach_here();
1374 
1375   return entry_point;
1376 }
1377 
1378 //
1379 // Generic interpreted method entry to (asm) interpreter
1380 //
1381 address TemplateInterpreterGenerator::generate_normal_entry(bool synchronized) {
1382   // determine code generation flags
1383   bool inc_counter  = UseCompiler || CountCompiledCalls;
1384 
1385   // ebx: Method*
1386   // rbcp: sender sp (set in InterpreterMacroAssembler::prepare_to_jump_from_interpreted / generate_call_stub)
1387   address entry_point = __ pc();
1388 
1389   const Address constMethod(rbx, Method::const_offset());
1390   const Address access_flags(rbx, Method::access_flags_offset());
1391   const Address size_of_parameters(rdx,
1392                                    ConstMethod::size_of_parameters_offset());
1393   const Address size_of_locals(rdx, ConstMethod::size_of_locals_offset());
1394 
1395 
1396   // get parameter size (always needed)
1397   __ movptr(rdx, constMethod);
1398   __ load_unsigned_short(rcx, size_of_parameters);
1399 
1400   // rbx: Method*
1401   // rcx: size of parameters
1402   // rbcp: sender_sp (could differ from sp+wordSize if we were called via c2i )
1403 
1404   __ load_unsigned_short(rdx, size_of_locals); // get size of locals in words
1405   __ subl(rdx, rcx); // rdx = no. of additional locals
1406 
1407   // YYY
1408 //   __ incrementl(rdx);
1409 //   __ andl(rdx, -2);
1410 
1411   // see if we've got enough room on the stack for locals plus overhead.
1412   generate_stack_overflow_check();
1413 
1414   // get return address
1415   __ pop(rax);
1416 
1417   // compute beginning of parameters
1418   __ lea(rlocals, Address(rsp, rcx, Interpreter::stackElementScale(), -wordSize));
1419 
1420   // rdx - # of additional locals
1421   // allocate space for locals
1422   // explicitly initialize locals
1423   {
1424     Label exit, loop;
1425     __ testl(rdx, rdx);
1426     __ jcc(Assembler::lessEqual, exit); // do nothing if rdx <= 0
1427     __ bind(loop);
1428     __ push(NULL_WORD); // initialize local variables
1429     __ decrementl(rdx); // until everything initialized
1430     __ jcc(Assembler::greater, loop);
1431     __ bind(exit);
1432   }
1433 
1434   // initialize fixed part of activation frame
1435   generate_fixed_frame(false);
1436 
1437   // make sure method is not native & not abstract
1438 #ifdef ASSERT
1439   __ load_unsigned_short(rax, access_flags);
1440   {
1441     Label L;
1442     __ testl(rax, JVM_ACC_NATIVE);
1443     __ jcc(Assembler::zero, L);
1444     __ stop("tried to execute native method as non-native");
1445     __ bind(L);
1446   }
1447   {
1448     Label L;
1449     __ testl(rax, JVM_ACC_ABSTRACT);
1450     __ jcc(Assembler::zero, L);
1451     __ stop("tried to execute abstract method in interpreter");
1452     __ bind(L);
1453   }
1454 #endif
1455 
1456   // Since at this point in the method invocation the exception
1457   // handler would try to exit the monitor of synchronized methods
1458   // which hasn't been entered yet, we set the thread local variable
1459   // _do_not_unlock_if_synchronized to true. The remove_activation
1460   // will check this flag.
1461 
1462   const Register thread = NOT_LP64(rax) LP64_ONLY(r15_thread);
1463   NOT_LP64(__ get_thread(thread));
1464   const Address do_not_unlock_if_synchronized(thread,
1465         in_bytes(JavaThread::do_not_unlock_if_synchronized_offset()));
1466   __ movbool(do_not_unlock_if_synchronized, true);
1467 
1468   __ profile_parameters_type(rax, rcx, rdx);
1469   // increment invocation count & check for overflow
1470   Label invocation_counter_overflow;
1471   if (inc_counter) {
1472     generate_counter_incr(&invocation_counter_overflow);
1473   }
1474 
1475   Label continue_after_compile;
1476   __ bind(continue_after_compile);
1477 
1478   // check for synchronized interpreted methods
1479   bang_stack_shadow_pages(false);
1480 
1481   // reset the _do_not_unlock_if_synchronized flag
1482   NOT_LP64(__ get_thread(thread));
1483   __ movbool(do_not_unlock_if_synchronized, false);
1484 
1485   // check for synchronized methods
1486   // Must happen AFTER invocation_counter check and stack overflow check,
1487   // so method is not locked if overflows.
1488   if (synchronized) {
1489     // Allocate monitor and lock method
1490     lock_method();
1491   } else {
1492     // no synchronization necessary
1493 #ifdef ASSERT
1494     {
1495       Label L;
1496       __ load_unsigned_short(rax, access_flags);
1497       __ testl(rax, JVM_ACC_SYNCHRONIZED);
1498       __ jcc(Assembler::zero, L);
1499       __ stop("method needs synchronization");
1500       __ bind(L);
1501     }
1502 #endif
1503   }
1504 
1505   // start execution
1506 #ifdef ASSERT
1507   {
1508     Label L;
1509      const Address monitor_block_top (rbp,
1510                  frame::interpreter_frame_monitor_block_top_offset * wordSize);
1511     __ movptr(rax, monitor_block_top);
1512     __ lea(rax, Address(rbp, rax, Address::times_ptr));
1513     __ cmpptr(rax, rsp);
1514     __ jcc(Assembler::equal, L);
1515     __ stop("broken stack frame setup in interpreter 6");
1516     __ bind(L);
1517   }
1518 #endif
1519 
1520   // jvmti support
1521   __ notify_method_entry();
1522 
1523   __ dispatch_next(vtos);
1524 
1525   // invocation counter overflow
1526   if (inc_counter) {
1527     // Handle overflow of counter and compile method
1528     __ bind(invocation_counter_overflow);
1529     generate_counter_overflow(continue_after_compile);
1530   }
1531 
1532   return entry_point;
1533 }
1534 
1535 //-----------------------------------------------------------------------------
1536 // Exceptions
1537 
1538 void TemplateInterpreterGenerator::generate_throw_exception() {
1539   // Entry point in previous activation (i.e., if the caller was
1540   // interpreted)
1541   Interpreter::_rethrow_exception_entry = __ pc();
1542   // Restore sp to interpreter_frame_last_sp even though we are going
1543   // to empty the expression stack for the exception processing.
1544   __ movptr(Address(rbp, frame::interpreter_frame_last_sp_offset * wordSize), NULL_WORD);
1545   // rax: exception
1546   // rdx: return address/pc that threw exception
1547   __ restore_bcp();    // r13/rsi points to call/send
1548   __ restore_locals();
1549   LP64_ONLY(__ reinit_heapbase());  // restore r12 as heapbase.
1550   // Entry point for exceptions thrown within interpreter code
1551   Interpreter::_throw_exception_entry = __ pc();
1552   // expression stack is undefined here
1553   // rax: exception
1554   // r13/rsi: exception bcp
1555   __ verify_oop(rax);
1556   Register rarg = NOT_LP64(rax) LP64_ONLY(c_rarg1);
1557   LP64_ONLY(__ mov(c_rarg1, rax));
1558 
1559   // expression stack must be empty before entering the VM in case of
1560   // an exception
1561   __ empty_expression_stack();
1562   // find exception handler address and preserve exception oop
1563   __ call_VM(rdx,
1564              CAST_FROM_FN_PTR(address,
1565                           InterpreterRuntime::exception_handler_for_exception),
1566              rarg);
1567   // rax: exception handler entry point
1568   // rdx: preserved exception oop
1569   // r13/rsi: bcp for exception handler
1570   __ push_ptr(rdx); // push exception which is now the only value on the stack
1571   __ jmp(rax); // jump to exception handler (may be _remove_activation_entry!)
1572 
1573   // If the exception is not handled in the current frame the frame is
1574   // removed and the exception is rethrown (i.e. exception
1575   // continuation is _rethrow_exception).
1576   //
1577   // Note: At this point the bci is still the bxi for the instruction
1578   // which caused the exception and the expression stack is
1579   // empty. Thus, for any VM calls at this point, GC will find a legal
1580   // oop map (with empty expression stack).
1581 
1582   // In current activation
1583   // tos: exception
1584   // esi: exception bcp
1585 
1586   //
1587   // JVMTI PopFrame support
1588   //
1589 
1590   Interpreter::_remove_activation_preserving_args_entry = __ pc();
1591   __ empty_expression_stack();
1592   // Set the popframe_processing bit in pending_popframe_condition
1593   // indicating that we are currently handling popframe, so that
1594   // call_VMs that may happen later do not trigger new popframe
1595   // handling cycles.
1596   const Register thread = NOT_LP64(rcx) LP64_ONLY(r15_thread);
1597   NOT_LP64(__ get_thread(thread));
1598   __ movl(rdx, Address(thread, JavaThread::popframe_condition_offset()));
1599   __ orl(rdx, JavaThread::popframe_processing_bit);
1600   __ movl(Address(thread, JavaThread::popframe_condition_offset()), rdx);
1601 
1602   {
1603     // Check to see whether we are returning to a deoptimized frame.
1604     // (The PopFrame call ensures that the caller of the popped frame is
1605     // either interpreted or compiled and deoptimizes it if compiled.)
1606     // In this case, we can't call dispatch_next() after the frame is
1607     // popped, but instead must save the incoming arguments and restore
1608     // them after deoptimization has occurred.
1609     //
1610     // Note that we don't compare the return PC against the
1611     // deoptimization blob's unpack entry because of the presence of
1612     // adapter frames in C2.
1613     Label caller_not_deoptimized;
1614     Register rarg = NOT_LP64(rdx) LP64_ONLY(c_rarg1);
1615     __ movptr(rarg, Address(rbp, frame::return_addr_offset * wordSize));
1616     __ super_call_VM_leaf(CAST_FROM_FN_PTR(address,
1617                                InterpreterRuntime::interpreter_contains), rarg);
1618     __ testl(rax, rax);
1619     __ jcc(Assembler::notZero, caller_not_deoptimized);
1620 
1621     // Compute size of arguments for saving when returning to
1622     // deoptimized caller
1623     __ get_method(rax);
1624     __ movptr(rax, Address(rax, Method::const_offset()));
1625     __ load_unsigned_short(rax, Address(rax, in_bytes(ConstMethod::
1626                                                 size_of_parameters_offset())));
1627     __ shll(rax, Interpreter::logStackElementSize);
1628     __ restore_locals();
1629     __ subptr(rlocals, rax);
1630     __ addptr(rlocals, wordSize);
1631     // Save these arguments
1632     NOT_LP64(__ get_thread(thread));
1633     __ super_call_VM_leaf(CAST_FROM_FN_PTR(address,
1634                                            Deoptimization::
1635                                            popframe_preserve_args),
1636                           thread, rax, rlocals);
1637 
1638     __ remove_activation(vtos, rdx,
1639                          /* throw_monitor_exception */ false,
1640                          /* install_monitor_exception */ false,
1641                          /* notify_jvmdi */ false);
1642 
1643     // Inform deoptimization that it is responsible for restoring
1644     // these arguments
1645     NOT_LP64(__ get_thread(thread));
1646     __ movl(Address(thread, JavaThread::popframe_condition_offset()),
1647             JavaThread::popframe_force_deopt_reexecution_bit);
1648 
1649     // Continue in deoptimization handler
1650     __ jmp(rdx);
1651 
1652     __ bind(caller_not_deoptimized);
1653   }
1654 
1655   __ remove_activation(vtos, rdx, /* rdx result (retaddr) is not used */
1656                        /* throw_monitor_exception */ false,
1657                        /* install_monitor_exception */ false,
1658                        /* notify_jvmdi */ false);
1659 
1660   // Finish with popframe handling
1661   // A previous I2C followed by a deoptimization might have moved the
1662   // outgoing arguments further up the stack. PopFrame expects the
1663   // mutations to those outgoing arguments to be preserved and other
1664   // constraints basically require this frame to look exactly as
1665   // though it had previously invoked an interpreted activation with
1666   // no space between the top of the expression stack (current
1667   // last_sp) and the top of stack. Rather than force deopt to
1668   // maintain this kind of invariant all the time we call a small
1669   // fixup routine to move the mutated arguments onto the top of our
1670   // expression stack if necessary.
1671 #ifndef _LP64
1672   __ mov(rax, rsp);
1673   __ movptr(rbx, Address(rbp, frame::interpreter_frame_last_sp_offset * wordSize));
1674   __ lea(rbx, Address(rbp, rbx, Address::times_ptr));
1675   __ get_thread(thread);
1676   // PC must point into interpreter here
1677   __ set_last_Java_frame(thread, noreg, rbp, __ pc(), noreg);
1678   __ super_call_VM_leaf(CAST_FROM_FN_PTR(address, InterpreterRuntime::popframe_move_outgoing_args), thread, rax, rbx);
1679   __ get_thread(thread);
1680 #else
1681   __ mov(c_rarg1, rsp);
1682   __ movptr(c_rarg2, Address(rbp, frame::interpreter_frame_last_sp_offset * wordSize));
1683   __ lea(c_rarg2, Address(rbp, c_rarg2, Address::times_ptr));
1684   // PC must point into interpreter here
1685   __ set_last_Java_frame(noreg, rbp, __ pc(), rscratch1);
1686   __ super_call_VM_leaf(CAST_FROM_FN_PTR(address, InterpreterRuntime::popframe_move_outgoing_args), r15_thread, c_rarg1, c_rarg2);
1687 #endif
1688   __ reset_last_Java_frame(thread, true);
1689 
1690   // Restore the last_sp and null it out
1691   __ movptr(rcx, Address(rbp, frame::interpreter_frame_last_sp_offset * wordSize));
1692   __ lea(rsp, Address(rbp, rcx, Address::times_ptr));
1693   __ movptr(Address(rbp, frame::interpreter_frame_last_sp_offset * wordSize), NULL_WORD);
1694 
1695   __ restore_bcp();
1696   __ restore_locals();
1697   // The method data pointer was incremented already during
1698   // call profiling. We have to restore the mdp for the current bcp.
1699   if (ProfileInterpreter) {
1700     __ set_method_data_pointer_for_bcp();
1701   }
1702 
1703   // Clear the popframe condition flag
1704   NOT_LP64(__ get_thread(thread));
1705   __ movl(Address(thread, JavaThread::popframe_condition_offset()),
1706           JavaThread::popframe_inactive);
1707 
1708 #if INCLUDE_JVMTI
1709   {
1710     Label L_done;
1711     const Register local0 = rlocals;
1712 
1713     __ cmpb(Address(rbcp, 0), Bytecodes::_invokestatic);
1714     __ jcc(Assembler::notEqual, L_done);
1715 
1716     // The member name argument must be restored if _invokestatic is re-executed after a PopFrame call.
1717     // Detect such a case in the InterpreterRuntime function and return the member name argument, or null.
1718 
1719     __ get_method(rdx);
1720     __ movptr(rax, Address(local0, 0));
1721     __ call_VM(rax, CAST_FROM_FN_PTR(address, InterpreterRuntime::member_name_arg_or_null), rax, rdx, rbcp);
1722 
1723     __ testptr(rax, rax);
1724     __ jcc(Assembler::zero, L_done);
1725 
1726     __ movptr(Address(rbx, 0), rax);
1727     __ bind(L_done);
1728   }
1729 #endif // INCLUDE_JVMTI
1730 
1731   __ dispatch_next(vtos);
1732   // end of PopFrame support
1733 
1734   Interpreter::_remove_activation_entry = __ pc();
1735 
1736   // preserve exception over this code sequence
1737   __ pop_ptr(rax);
1738   NOT_LP64(__ get_thread(thread));
1739   __ movptr(Address(thread, JavaThread::vm_result_offset()), rax);
1740   // remove the activation (without doing throws on illegalMonitorExceptions)
1741   __ remove_activation(vtos, rdx, false, true, false);
1742   // restore exception
1743   NOT_LP64(__ get_thread(thread));
1744   __ get_vm_result(rax, thread);
1745 
1746   // In between activations - previous activation type unknown yet
1747   // compute continuation point - the continuation point expects the
1748   // following registers set up:
1749   //
1750   // rax: exception
1751   // rdx: return address/pc that threw exception
1752   // rsp: expression stack of caller
1753   // rbp: ebp of caller
1754   __ push(rax);                                  // save exception
1755   __ push(rdx);                                  // save return address
1756   __ super_call_VM_leaf(CAST_FROM_FN_PTR(address,
1757                           SharedRuntime::exception_handler_for_return_address),
1758                         thread, rdx);
1759   __ mov(rbx, rax);                              // save exception handler
1760   __ pop(rdx);                                   // restore return address
1761   __ pop(rax);                                   // restore exception
1762   // Note that an "issuing PC" is actually the next PC after the call
1763   __ jmp(rbx);                                   // jump to exception
1764                                                  // handler of caller
1765 }
1766 
1767 
1768 //
1769 // JVMTI ForceEarlyReturn support
1770 //
1771 address TemplateInterpreterGenerator::generate_earlyret_entry_for(TosState state) {
1772   address entry = __ pc();
1773 
1774   __ restore_bcp();
1775   __ restore_locals();
1776   __ empty_expression_stack();
1777   __ load_earlyret_value(state);  // 32 bits returns value in rdx, so don't reuse
1778 
1779   const Register thread = NOT_LP64(rcx) LP64_ONLY(r15_thread);
1780   NOT_LP64(__ get_thread(thread));
1781   __ movptr(rcx, Address(thread, JavaThread::jvmti_thread_state_offset()));
1782   Address cond_addr(rcx, JvmtiThreadState::earlyret_state_offset());
1783 
1784   // Clear the earlyret state
1785   __ movl(cond_addr, JvmtiThreadState::earlyret_inactive);
1786 
1787   __ remove_activation(state, rsi,
1788                        false, /* throw_monitor_exception */
1789                        false, /* install_monitor_exception */
1790                        true); /* notify_jvmdi */
1791   __ jmp(rsi);
1792 
1793   return entry;
1794 } // end of ForceEarlyReturn support
1795 
1796 
1797 //-----------------------------------------------------------------------------
1798 // Helper for vtos entry point generation
1799 
1800 void TemplateInterpreterGenerator::set_vtos_entry_points(Template* t,
1801                                                          address& bep,
1802                                                          address& cep,
1803                                                          address& sep,
1804                                                          address& aep,
1805                                                          address& iep,
1806                                                          address& lep,
1807                                                          address& fep,
1808                                                          address& dep,
1809                                                          address& vep) {
1810   assert(t->is_valid() && t->tos_in() == vtos, "illegal template");
1811   Label L;
1812 #ifndef _LP64
1813   fep = __ pc();     // ftos entry point
1814       __ push(ftos);
1815       __ jmpb(L);
1816   dep = __ pc();     // dtos entry point
1817       __ push(dtos);
1818       __ jmpb(L);
1819 #else
1820   fep = __ pc();     // ftos entry point
1821       __ push_f(xmm0);
1822       __ jmpb(L);
1823   dep = __ pc();     // dtos entry point
1824       __ push_d(xmm0);
1825       __ jmpb(L);
1826 #endif // _LP64
1827   lep = __ pc();     // ltos entry point
1828       __ push_l();
1829       __ jmpb(L);
1830   aep = bep = cep = sep = iep = __ pc();      // [abcsi]tos entry point
1831       __ push_i_or_ptr();
1832   vep = __ pc();    // vtos entry point
1833   __ bind(L);
1834   generate_and_dispatch(t);
1835 }
1836 
1837 //-----------------------------------------------------------------------------
1838 
1839 // Non-product code
1840 #ifndef PRODUCT
1841 
1842 address TemplateInterpreterGenerator::generate_trace_code(TosState state) {
1843   address entry = __ pc();
1844 
1845 #ifndef _LP64
1846   // prepare expression stack
1847   __ pop(rcx);          // pop return address so expression stack is 'pure'
1848   __ push(state);       // save tosca
1849 
1850   // pass tosca registers as arguments & call tracer
1851   __ call_VM(noreg, CAST_FROM_FN_PTR(address, InterpreterRuntime::trace_bytecode), rcx, rax, rdx);
1852   __ mov(rcx, rax);     // make sure return address is not destroyed by pop(state)
1853   __ pop(state);        // restore tosca
1854 
1855   // return
1856   __ jmp(rcx);
1857 #else
1858   __ push(state);
1859   __ push(c_rarg0);
1860   __ push(c_rarg1);
1861   __ push(c_rarg2);
1862   __ push(c_rarg3);
1863   __ mov(c_rarg2, rax);  // Pass itos
1864 #ifdef _WIN64
1865   __ movflt(xmm3, xmm0); // Pass ftos
1866 #endif
1867   __ call_VM(noreg,
1868              CAST_FROM_FN_PTR(address, InterpreterRuntime::trace_bytecode),
1869              c_rarg1, c_rarg2, c_rarg3);
1870   __ pop(c_rarg3);
1871   __ pop(c_rarg2);
1872   __ pop(c_rarg1);
1873   __ pop(c_rarg0);
1874   __ pop(state);
1875   __ ret(0);                                   // return from result handler
1876 #endif // _LP64
1877 
1878   return entry;
1879 }
1880 
1881 void TemplateInterpreterGenerator::count_bytecode() {
1882   __ incrementl(ExternalAddress((address) &BytecodeCounter::_counter_value), rscratch1);
1883 }
1884 
1885 void TemplateInterpreterGenerator::histogram_bytecode(Template* t) {
1886   __ incrementl(ExternalAddress((address) &BytecodeHistogram::_counters[t->bytecode()]), rscratch1);
1887 }
1888 
1889 void TemplateInterpreterGenerator::histogram_bytecode_pair(Template* t) {
1890   __ mov32(rbx, ExternalAddress((address) &BytecodePairHistogram::_index));
1891   __ shrl(rbx, BytecodePairHistogram::log2_number_of_codes);
1892   __ orl(rbx,
1893          ((int) t->bytecode()) <<
1894          BytecodePairHistogram::log2_number_of_codes);
1895   __ mov32(ExternalAddress((address) &BytecodePairHistogram::_index), rbx, rscratch1);
1896   __ lea(rscratch1, ExternalAddress((address) BytecodePairHistogram::_counters));
1897   __ incrementl(Address(rscratch1, rbx, Address::times_4));
1898 }
1899 
1900 
1901 void TemplateInterpreterGenerator::trace_bytecode(Template* t) {
1902   // Call a little run-time stub to avoid blow-up for each bytecode.
1903   // The run-time runtime saves the right registers, depending on
1904   // the tosca in-state for the given template.
1905 
1906   assert(Interpreter::trace_code(t->tos_in()) != nullptr,
1907          "entry must have been generated");
1908 #ifndef _LP64
1909   __ call(RuntimeAddress(Interpreter::trace_code(t->tos_in())));
1910 #else
1911   __ mov(r12, rsp); // remember sp (can only use r12 if not using call_VM)
1912   __ andptr(rsp, -16); // align stack as required by ABI
1913   __ call(RuntimeAddress(Interpreter::trace_code(t->tos_in())));
1914   __ mov(rsp, r12); // restore sp
1915   __ reinit_heapbase();
1916 #endif // _LP64
1917 }
1918 
1919 
1920 void TemplateInterpreterGenerator::stop_interpreter_at() {
1921   Label L;
1922   __ cmp32(ExternalAddress((address) &BytecodeCounter::_counter_value),
1923            StopInterpreterAt,
1924            rscratch1);
1925   __ jcc(Assembler::notEqual, L);
1926   __ int3();
1927   __ bind(L);
1928 }
1929 #endif // !PRODUCT