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