1 /*
   2  * Copyright (c) 2003, 2023, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  *
  23  */
  24 
  25 #include "precompiled.hpp"
  26 #include "asm/macroAssembler.hpp"
  27 #include "classfile/javaClasses.hpp"
  28 #include "compiler/compiler_globals.hpp"
  29 #include "compiler/disassembler.hpp"
  30 #include "gc/shared/barrierSetAssembler.hpp"
  31 #include "interpreter/bytecodeHistogram.hpp"
  32 #include "interpreter/interp_masm.hpp"
  33 #include "interpreter/interpreter.hpp"
  34 #include "interpreter/interpreterRuntime.hpp"
  35 #include "interpreter/templateInterpreterGenerator.hpp"
  36 #include "interpreter/templateTable.hpp"
  37 #include "oops/arrayOop.hpp"
  38 #include "oops/methodCounters.hpp"
  39 #include "oops/methodData.hpp"
  40 #include "oops/method.hpp"
  41 #include "oops/oop.inline.hpp"
  42 #include "oops/resolvedIndyEntry.hpp"
  43 #include "oops/resolvedMethodEntry.hpp"
  44 #include "prims/jvmtiExport.hpp"
  45 #include "prims/jvmtiThreadState.hpp"
  46 #include "runtime/continuation.hpp"
  47 #include "runtime/deoptimization.hpp"
  48 #include "runtime/frame.inline.hpp"
  49 #include "runtime/globals.hpp"
  50 #include "runtime/jniHandles.hpp"
  51 #include "runtime/sharedRuntime.hpp"
  52 #include "runtime/stubRoutines.hpp"
  53 #include "runtime/synchronizer.hpp"
  54 #include "runtime/timer.hpp"
  55 #include "runtime/vframeArray.hpp"
  56 #include "utilities/checkedCast.hpp"
  57 #include "utilities/debug.hpp"
  58 #include "utilities/macros.hpp"
  59 
  60 #define __ Disassembler::hook<InterpreterMacroAssembler>(__FILE__, __LINE__, _masm)->
  61 
  62 // Size of interpreter code.  Increase if too small.  Interpreter will
  63 // fail with a guarantee ("not enough space for interpreter generation");
  64 // if too small.
  65 // Run with +PrintInterpreter to get the VM to print out the size.
  66 // Max size with JVMTI
  67 #ifdef AMD64
  68 int TemplateInterpreter::InterpreterCodeSize = JVMCI_ONLY(268) NOT_JVMCI(256) * 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(ExternalAddress(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(rcx, Address(rbp, frame::interpreter_frame_last_sp_offset * wordSize));
 214   __ lea(rsp, Address(rbp, rcx, Address::times_ptr));
 215   // and null it as marker that esp is now tos until next java call
 216   __ movptr(Address(rbp, frame::interpreter_frame_last_sp_offset * wordSize), NULL_WORD);
 217 
 218   __ restore_bcp();
 219   __ restore_locals();
 220 
 221   if (state == atos) {
 222     Register mdp = rbx;
 223     Register tmp = rcx;
 224     __ profile_return_type(mdp, rax, tmp);
 225   }
 226 
 227   const Register cache = rbx;
 228   const Register index = rcx;
 229   if (index_size == sizeof(u4)) {
 230     __ load_resolved_indy_entry(cache, index);
 231     __ load_unsigned_short(cache, Address(cache, in_bytes(ResolvedIndyEntry::num_parameters_offset())));
 232     __ lea(rsp, Address(rsp, cache, Interpreter::stackElementScale()));
 233   } else {
 234     assert(index_size == sizeof(u2), "Can only be u2");
 235     __ load_method_entry(cache, index);
 236     __ load_unsigned_short(cache, Address(cache, in_bytes(ResolvedMethodEntry::num_parameters_offset())));
 237     __ lea(rsp, Address(rsp, cache, Interpreter::stackElementScale()));
 238   }
 239 
 240    const Register java_thread = NOT_LP64(rcx) LP64_ONLY(r15_thread);
 241    if (JvmtiExport::can_pop_frame()) {
 242      NOT_LP64(__ get_thread(java_thread));
 243      __ check_and_handle_popframe(java_thread);
 244    }
 245    if (JvmtiExport::can_force_early_return()) {
 246      NOT_LP64(__ get_thread(java_thread));
 247      __ check_and_handle_earlyret(java_thread);
 248    }
 249 
 250   __ dispatch_next(state, step);
 251 
 252   return entry;
 253 }
 254 
 255 
 256 address TemplateInterpreterGenerator::generate_deopt_entry_for(TosState state, int step, address continuation) {
 257   address entry = __ pc();
 258 
 259 #ifndef _LP64
 260   if (state == ftos) {
 261     __ MacroAssembler::verify_FPU(UseSSE >= 1 ? 0 : 1, "generate_deopt_entry_for in interpreter");
 262   } else if (state == dtos) {
 263     __ MacroAssembler::verify_FPU(UseSSE >= 2 ? 0 : 1, "generate_deopt_entry_for in interpreter");
 264   }
 265 #endif // _LP64
 266 
 267   // null last_sp until next java call
 268   __ movptr(Address(rbp, frame::interpreter_frame_last_sp_offset * wordSize), NULL_WORD);
 269   __ restore_bcp();
 270   __ restore_locals();
 271   const Register thread = NOT_LP64(rcx) LP64_ONLY(r15_thread);
 272   NOT_LP64(__ get_thread(thread));
 273 #if INCLUDE_JVMCI
 274   // Check if we need to take lock at entry of synchronized method.  This can
 275   // only occur on method entry so emit it only for vtos with step 0.
 276   if (EnableJVMCI && state == vtos && step == 0) {
 277     Label L;
 278     __ cmpb(Address(thread, JavaThread::pending_monitorenter_offset()), 0);
 279     __ jcc(Assembler::zero, L);
 280     // Clear flag.
 281     __ movb(Address(thread, JavaThread::pending_monitorenter_offset()), 0);
 282     // Satisfy calling convention for lock_method().
 283     __ get_method(rbx);
 284     // Take lock.
 285     lock_method();
 286     __ bind(L);
 287   } else {
 288 #ifdef ASSERT
 289     if (EnableJVMCI) {
 290       Label L;
 291       __ cmpb(Address(r15_thread, JavaThread::pending_monitorenter_offset()), 0);
 292       __ jcc(Assembler::zero, L);
 293       __ stop("unexpected pending monitor in deopt entry");
 294       __ bind(L);
 295     }
 296 #endif
 297   }
 298 #endif
 299   // handle exceptions
 300   {
 301     Label L;
 302     __ cmpptr(Address(thread, Thread::pending_exception_offset()), NULL_WORD);
 303     __ jcc(Assembler::zero, L);
 304     __ call_VM(noreg,
 305                CAST_FROM_FN_PTR(address,
 306                                 InterpreterRuntime::throw_pending_exception));
 307     __ should_not_reach_here();
 308     __ bind(L);
 309   }
 310   if (continuation == nullptr) {
 311     __ dispatch_next(state, step);
 312   } else {
 313     __ jump_to_entry(continuation);
 314   }
 315   return entry;
 316 }
 317 
 318 address TemplateInterpreterGenerator::generate_result_handler_for(
 319         BasicType type) {
 320   address entry = __ pc();
 321   switch (type) {
 322   case T_BOOLEAN: __ c2bool(rax);            break;
 323 #ifndef _LP64
 324   case T_CHAR   : __ andptr(rax, 0xFFFF);    break;
 325 #else
 326   case T_CHAR   : __ movzwl(rax, rax);       break;
 327 #endif // _LP64
 328   case T_BYTE   : __ sign_extend_byte(rax);  break;
 329   case T_SHORT  : __ sign_extend_short(rax); break;
 330   case T_INT    : /* nothing to do */        break;
 331   case T_LONG   : /* nothing to do */        break;
 332   case T_VOID   : /* nothing to do */        break;
 333 #ifndef _LP64
 334   case T_DOUBLE :
 335   case T_FLOAT  :
 336     { const Register t = InterpreterRuntime::SignatureHandlerGenerator::temp();
 337       __ pop(t);                            // remove return address first
 338       // Must return a result for interpreter or compiler. In SSE
 339       // mode, results are returned in xmm0 and the FPU stack must
 340       // be empty.
 341       if (type == T_FLOAT && UseSSE >= 1) {
 342         // Load ST0
 343         __ fld_d(Address(rsp, 0));
 344         // Store as float and empty fpu stack
 345         __ fstp_s(Address(rsp, 0));
 346         // and reload
 347         __ movflt(xmm0, Address(rsp, 0));
 348       } else if (type == T_DOUBLE && UseSSE >= 2 ) {
 349         __ movdbl(xmm0, Address(rsp, 0));
 350       } else {
 351         // restore ST0
 352         __ fld_d(Address(rsp, 0));
 353       }
 354       // and pop the temp
 355       __ addptr(rsp, 2 * wordSize);
 356       __ push(t);                           // restore return address
 357     }
 358     break;
 359 #else
 360   case T_FLOAT  : /* nothing to do */        break;
 361   case T_DOUBLE : /* nothing to do */        break;
 362 #endif // _LP64
 363 
 364   case T_OBJECT :
 365     // retrieve result from frame
 366     __ movptr(rax, Address(rbp, frame::interpreter_frame_oop_temp_offset*wordSize));
 367     // and verify it
 368     __ verify_oop(rax);
 369     break;
 370   default       : ShouldNotReachHere();
 371   }
 372   __ ret(0);                                   // return from result handler
 373   return entry;
 374 }
 375 
 376 address TemplateInterpreterGenerator::generate_safept_entry_for(
 377         TosState state,
 378         address runtime_entry) {
 379   address entry = __ pc();
 380 
 381   __ push(state);
 382   __ push_cont_fastpath();
 383   __ call_VM(noreg, runtime_entry);
 384   __ pop_cont_fastpath();
 385 
 386   __ dispatch_via(vtos, Interpreter::_normal_table.table_for(vtos));
 387   return entry;
 388 }
 389 
 390 address TemplateInterpreterGenerator::generate_cont_preempt_rerun_interpreter_adapter() {
 391   if (!Continuations::enabled()) return nullptr;
 392   address start = __ pc();
 393 
 394   __ pop(rbp);
 395 
 396   // We will return to the intermediate call made in call_VM skipping the restoration
 397   // of bcp and locals done in InterpreterMacroAssembler::call_VM_base, so fix them here.
 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(StubRoutines::throw_StackOverflowError_entry() != nullptr, "stub not yet generated");
 573   __ jump(ExternalAddress(StubRoutines::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     __ movl(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     __ movl(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   __ movl(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       __ movl(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     __ movl(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    __ set_last_Java_frame(rsp, rbp, (address) __ pc(), rscratch1);
1057 #endif // _LP64
1058 
1059   // change thread state
1060 #ifdef ASSERT
1061   {
1062     Label L;
1063     __ movl(t, Address(thread, JavaThread::thread_state_offset()));
1064     __ cmpl(t, _thread_in_Java);
1065     __ jcc(Assembler::equal, L);
1066     __ stop("Wrong thread state in native stub");
1067     __ bind(L);
1068   }
1069 #endif
1070 
1071   // Change state to native
1072 
1073   __ movl(Address(thread, JavaThread::thread_state_offset()),
1074           _thread_in_native);
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   // Verify or restore cpu control state after JNI call
1082   __ restore_cpu_control_state_after_jni(rscratch1);
1083 
1084   // NOTE: The order of these pushes is known to frame::interpreter_frame_result
1085   // in order to extract the result of a method call. If the order of these
1086   // pushes change or anything else is added to the stack then the code in
1087   // interpreter_frame_result must also change.
1088 
1089 #ifndef _LP64
1090   // save potential result in ST(0) & rdx:rax
1091   // (if result handler is the T_FLOAT or T_DOUBLE handler, result must be in ST0 -
1092   // the check is necessary to avoid potential Intel FPU overflow problems by saving/restoring 'empty' FPU registers)
1093   // It is safe to do this push because state is _thread_in_native and return address will be found
1094   // via _last_native_pc and not via _last_jave_sp
1095 
1096   // NOTE: the order of these push(es) is known to frame::interpreter_frame_result.
1097   // If the order changes or anything else is added to the stack the code in
1098   // interpreter_frame_result will have to be changed.
1099 
1100   { Label L;
1101     Label push_double;
1102     ExternalAddress float_handler(AbstractInterpreter::result_handler(T_FLOAT));
1103     ExternalAddress double_handler(AbstractInterpreter::result_handler(T_DOUBLE));
1104     __ cmpptr(Address(rbp, (frame::interpreter_frame_oop_temp_offset + 1)*wordSize),
1105               float_handler.addr(), noreg);
1106     __ jcc(Assembler::equal, push_double);
1107     __ cmpptr(Address(rbp, (frame::interpreter_frame_oop_temp_offset + 1)*wordSize),
1108               double_handler.addr(), noreg);
1109     __ jcc(Assembler::notEqual, L);
1110     __ bind(push_double);
1111     __ push_d(); // FP values are returned using the FPU, so push FPU contents (even if UseSSE > 0).
1112     __ bind(L);
1113   }
1114 #else
1115   __ push(dtos);
1116 #endif // _LP64
1117 
1118   __ push(ltos);
1119 
1120   // change thread state
1121   NOT_LP64(__ get_thread(thread));
1122   __ movl(Address(thread, JavaThread::thread_state_offset()),
1123           _thread_in_native_trans);
1124 
1125   // Force this write out before the read below
1126   if (!UseSystemMemoryBarrier) {
1127     __ membar(Assembler::Membar_mask_bits(
1128                 Assembler::LoadLoad | Assembler::LoadStore |
1129                 Assembler::StoreLoad | Assembler::StoreStore));
1130   }
1131 #ifndef _LP64
1132   if (AlwaysRestoreFPU) {
1133     //  Make sure the control word is correct.
1134     __ fldcw(ExternalAddress(StubRoutines::x86::addr_fpu_cntrl_wrd_std()));
1135   }
1136 #endif // _LP64
1137 
1138   // check for safepoint operation in progress and/or pending suspend requests
1139   {
1140     Label Continue;
1141     Label slow_path;
1142 
1143     __ safepoint_poll(slow_path, thread, true /* at_return */, false /* in_nmethod */);
1144 
1145     __ cmpl(Address(thread, JavaThread::suspend_flags_offset()), 0);
1146     __ jcc(Assembler::equal, Continue);
1147     __ bind(slow_path);
1148 
1149     // Don't use call_VM as it will see a possible pending exception
1150     // and forward it and never return here preventing us from
1151     // clearing _last_native_pc down below.  Also can't use
1152     // call_VM_leaf either as it will check to see if r13 & r14 are
1153     // preserved and correspond to the bcp/locals pointers. So we do a
1154     // runtime call by hand.
1155     //
1156 #ifndef _LP64
1157     __ push(thread);
1158     __ call(RuntimeAddress(CAST_FROM_FN_PTR(address,
1159                                             JavaThread::check_special_condition_for_native_trans)));
1160     __ increment(rsp, wordSize);
1161     __ get_thread(thread);
1162 #else
1163     __ mov(c_rarg0, r15_thread);
1164     __ mov(r12, rsp); // remember sp (can only use r12 if not using call_VM)
1165     __ subptr(rsp, frame::arg_reg_save_area_bytes); // windows
1166     __ andptr(rsp, -16); // align stack as required by ABI
1167     __ call(RuntimeAddress(CAST_FROM_FN_PTR(address, JavaThread::check_special_condition_for_native_trans)));
1168     __ mov(rsp, r12); // restore sp
1169     __ reinit_heapbase();
1170 #endif // _LP64
1171     __ bind(Continue);
1172   }
1173 
1174   // change thread state
1175   __ movl(Address(thread, JavaThread::thread_state_offset()), _thread_in_Java);
1176 
1177   // reset_last_Java_frame
1178   __ reset_last_Java_frame(thread, true);
1179 
1180   if (CheckJNICalls) {
1181     // clear_pending_jni_exception_check
1182     __ movptr(Address(thread, JavaThread::pending_jni_exception_check_fn_offset()), NULL_WORD);
1183   }
1184 
1185   // reset handle block
1186   __ movptr(t, Address(thread, JavaThread::active_handles_offset()));
1187   __ movl(Address(t, JNIHandleBlock::top_offset()), NULL_WORD);
1188 
1189   // If result is an oop unbox and store it in frame where gc will see it
1190   // and result handler will pick it up
1191 
1192   {
1193     Label no_oop;
1194     __ lea(t, ExternalAddress(AbstractInterpreter::result_handler(T_OBJECT)));
1195     __ cmpptr(t, Address(rbp, frame::interpreter_frame_result_handler_offset*wordSize));
1196     __ jcc(Assembler::notEqual, no_oop);
1197     // retrieve result
1198     __ pop(ltos);
1199     // Unbox oop result, e.g. JNIHandles::resolve value.
1200     __ resolve_jobject(rax /* value */,
1201                        thread /* thread */,
1202                        t /* tmp */);
1203     __ movptr(Address(rbp, frame::interpreter_frame_oop_temp_offset*wordSize), rax);
1204     // keep stack depth as expected by pushing oop which will eventually be discarded
1205     __ push(ltos);
1206     __ bind(no_oop);
1207   }
1208 
1209 
1210   {
1211     Label no_reguard;
1212     __ cmpl(Address(thread, JavaThread::stack_guard_state_offset()),
1213             StackOverflow::stack_guard_yellow_reserved_disabled);
1214     __ jcc(Assembler::notEqual, no_reguard);
1215 
1216     __ pusha(); // XXX only save smashed registers
1217 #ifndef _LP64
1218     __ call(RuntimeAddress(CAST_FROM_FN_PTR(address, SharedRuntime::reguard_yellow_pages)));
1219     __ popa();
1220 #else
1221     __ mov(r12, rsp); // remember sp (can only use r12 if not using call_VM)
1222     __ subptr(rsp, frame::arg_reg_save_area_bytes); // windows
1223     __ andptr(rsp, -16); // align stack as required by ABI
1224     __ call(RuntimeAddress(CAST_FROM_FN_PTR(address, SharedRuntime::reguard_yellow_pages)));
1225     __ mov(rsp, r12); // restore sp
1226     __ popa(); // XXX only restore smashed registers
1227     __ reinit_heapbase();
1228 #endif // _LP64
1229 
1230     __ bind(no_reguard);
1231   }
1232 
1233 
1234   // The method register is junk from after the thread_in_native transition
1235   // until here.  Also can't call_VM until the bcp has been
1236   // restored.  Need bcp for throwing exception below so get it now.
1237   __ get_method(method);
1238 
1239   // restore to have legal interpreter frame, i.e., bci == 0 <=> code_base()
1240   __ movptr(rbcp, Address(method, Method::const_offset()));   // get ConstMethod*
1241   __ lea(rbcp, Address(rbcp, ConstMethod::codes_offset()));    // get codebase
1242 
1243   // handle exceptions (exception handling will handle unlocking!)
1244   {
1245     Label L;
1246     __ cmpptr(Address(thread, Thread::pending_exception_offset()), NULL_WORD);
1247     __ jcc(Assembler::zero, L);
1248     // Note: At some point we may want to unify this with the code
1249     // used in call_VM_base(); i.e., we should use the
1250     // StubRoutines::forward_exception code. For now this doesn't work
1251     // here because the rsp is not correctly set at this point.
1252     __ MacroAssembler::call_VM(noreg,
1253                                CAST_FROM_FN_PTR(address,
1254                                InterpreterRuntime::throw_pending_exception));
1255     __ should_not_reach_here();
1256     __ bind(L);
1257   }
1258 
1259   // do unlocking if necessary
1260   {
1261     Label L;
1262     __ movl(t, Address(method, Method::access_flags_offset()));
1263     __ testl(t, JVM_ACC_SYNCHRONIZED);
1264     __ jcc(Assembler::zero, L);
1265     // the code below should be shared with interpreter macro
1266     // assembler implementation
1267     {
1268       Label unlock;
1269       // BasicObjectLock will be first in list, since this is a
1270       // synchronized method. However, need to check that the object
1271       // has not been unlocked by an explicit monitorexit bytecode.
1272       const Address monitor(rbp,
1273                             (intptr_t)(frame::interpreter_frame_initial_sp_offset *
1274                                        wordSize - (int)sizeof(BasicObjectLock)));
1275 
1276       const Register regmon = NOT_LP64(rdx) LP64_ONLY(c_rarg1);
1277 
1278       // monitor expect in c_rarg1 for slow unlock path
1279       __ lea(regmon, monitor); // address of first monitor
1280 
1281       __ movptr(t, Address(regmon, BasicObjectLock::obj_offset()));
1282       __ testptr(t, t);
1283       __ jcc(Assembler::notZero, unlock);
1284 
1285       // Entry already unlocked, need to throw exception
1286       __ MacroAssembler::call_VM(noreg,
1287                                  CAST_FROM_FN_PTR(address,
1288                    InterpreterRuntime::throw_illegal_monitor_state_exception));
1289       __ should_not_reach_here();
1290 
1291       __ bind(unlock);
1292       __ unlock_object(regmon);
1293     }
1294     __ bind(L);
1295   }
1296 
1297   // jvmti support
1298   // Note: This must happen _after_ handling/throwing any exceptions since
1299   //       the exception handler code notifies the runtime of method exits
1300   //       too. If this happens before, method entry/exit notifications are
1301   //       not properly paired (was bug - gri 11/22/99).
1302   __ notify_method_exit(vtos, InterpreterMacroAssembler::NotifyJVMTI);
1303 
1304   // restore potential result in edx:eax, call result handler to
1305   // restore potential result in ST0 & handle result
1306 
1307   __ pop(ltos);
1308   LP64_ONLY( __ pop(dtos));
1309 
1310   __ movptr(t, Address(rbp,
1311                        (frame::interpreter_frame_result_handler_offset) * wordSize));
1312   __ call(t);
1313 
1314   // remove activation
1315   __ movptr(t, Address(rbp,
1316                        frame::interpreter_frame_sender_sp_offset *
1317                        wordSize)); // get sender sp
1318   __ leave();                                // remove frame anchor
1319   __ pop(rdi);                               // get return address
1320   __ mov(rsp, t);                            // set sp to sender sp
1321   __ jmp(rdi);
1322 
1323   if (inc_counter) {
1324     // Handle overflow of counter and compile method
1325     __ bind(invocation_counter_overflow);
1326     generate_counter_overflow(continue_after_compile);
1327   }
1328 
1329   return entry_point;
1330 }
1331 
1332 // Abstract method entry
1333 // Attempt to execute abstract method. Throw exception
1334 address TemplateInterpreterGenerator::generate_abstract_entry(void) {
1335 
1336   address entry_point = __ pc();
1337 
1338   // abstract method entry
1339 
1340   //  pop return address, reset last_sp to null
1341   __ empty_expression_stack();
1342   __ restore_bcp();      // rsi must be correct for exception handler   (was destroyed)
1343   __ restore_locals();   // make sure locals pointer is correct as well (was destroyed)
1344 
1345   // throw exception
1346   __ call_VM(noreg, CAST_FROM_FN_PTR(address, InterpreterRuntime::throw_AbstractMethodErrorWithMethod), rbx);
1347   // the call_VM checks for exception, so we should never return here.
1348   __ should_not_reach_here();
1349 
1350   return entry_point;
1351 }
1352 
1353 //
1354 // Generic interpreted method entry to (asm) interpreter
1355 //
1356 address TemplateInterpreterGenerator::generate_normal_entry(bool synchronized) {
1357   // determine code generation flags
1358   bool inc_counter  = UseCompiler || CountCompiledCalls;
1359 
1360   // ebx: Method*
1361   // rbcp: sender sp (set in InterpreterMacroAssembler::prepare_to_jump_from_interpreted / generate_call_stub)
1362   address entry_point = __ pc();
1363 
1364   const Address constMethod(rbx, Method::const_offset());
1365   const Address access_flags(rbx, Method::access_flags_offset());
1366   const Address size_of_parameters(rdx,
1367                                    ConstMethod::size_of_parameters_offset());
1368   const Address size_of_locals(rdx, ConstMethod::size_of_locals_offset());
1369 
1370 
1371   // get parameter size (always needed)
1372   __ movptr(rdx, constMethod);
1373   __ load_unsigned_short(rcx, size_of_parameters);
1374 
1375   // rbx: Method*
1376   // rcx: size of parameters
1377   // rbcp: sender_sp (could differ from sp+wordSize if we were called via c2i )
1378 
1379   __ load_unsigned_short(rdx, size_of_locals); // get size of locals in words
1380   __ subl(rdx, rcx); // rdx = no. of additional locals
1381 
1382   // YYY
1383 //   __ incrementl(rdx);
1384 //   __ andl(rdx, -2);
1385 
1386   // see if we've got enough room on the stack for locals plus overhead.
1387   generate_stack_overflow_check();
1388 
1389   // get return address
1390   __ pop(rax);
1391 
1392   // compute beginning of parameters
1393   __ lea(rlocals, Address(rsp, rcx, Interpreter::stackElementScale(), -wordSize));
1394 
1395   // rdx - # of additional locals
1396   // allocate space for locals
1397   // explicitly initialize locals
1398   {
1399     Label exit, loop;
1400     __ testl(rdx, rdx);
1401     __ jcc(Assembler::lessEqual, exit); // do nothing if rdx <= 0
1402     __ bind(loop);
1403     __ push(NULL_WORD); // initialize local variables
1404     __ decrementl(rdx); // until everything initialized
1405     __ jcc(Assembler::greater, loop);
1406     __ bind(exit);
1407   }
1408 
1409   // initialize fixed part of activation frame
1410   generate_fixed_frame(false);
1411 
1412   // make sure method is not native & not abstract
1413 #ifdef ASSERT
1414   __ movl(rax, access_flags);
1415   {
1416     Label L;
1417     __ testl(rax, JVM_ACC_NATIVE);
1418     __ jcc(Assembler::zero, L);
1419     __ stop("tried to execute native method as non-native");
1420     __ bind(L);
1421   }
1422   {
1423     Label L;
1424     __ testl(rax, JVM_ACC_ABSTRACT);
1425     __ jcc(Assembler::zero, L);
1426     __ stop("tried to execute abstract method in interpreter");
1427     __ bind(L);
1428   }
1429 #endif
1430 
1431   // Since at this point in the method invocation the exception
1432   // handler would try to exit the monitor of synchronized methods
1433   // which hasn't been entered yet, we set the thread local variable
1434   // _do_not_unlock_if_synchronized to true. The remove_activation
1435   // will check this flag.
1436 
1437   const Register thread = NOT_LP64(rax) LP64_ONLY(r15_thread);
1438   NOT_LP64(__ get_thread(thread));
1439   const Address do_not_unlock_if_synchronized(thread,
1440         in_bytes(JavaThread::do_not_unlock_if_synchronized_offset()));
1441   __ movbool(do_not_unlock_if_synchronized, true);
1442 
1443   __ profile_parameters_type(rax, rcx, rdx);
1444   // increment invocation count & check for overflow
1445   Label invocation_counter_overflow;
1446   if (inc_counter) {
1447     generate_counter_incr(&invocation_counter_overflow);
1448   }
1449 
1450   Label continue_after_compile;
1451   __ bind(continue_after_compile);
1452 
1453   // check for synchronized interpreted methods
1454   bang_stack_shadow_pages(false);
1455 
1456   // reset the _do_not_unlock_if_synchronized flag
1457   NOT_LP64(__ get_thread(thread));
1458   __ movbool(do_not_unlock_if_synchronized, false);
1459 
1460   // check for synchronized methods
1461   // Must happen AFTER invocation_counter check and stack overflow check,
1462   // so method is not locked if overflows.
1463   if (synchronized) {
1464     // Allocate monitor and lock method
1465     lock_method();
1466   } else {
1467     // no synchronization necessary
1468 #ifdef ASSERT
1469     {
1470       Label L;
1471       __ movl(rax, access_flags);
1472       __ testl(rax, JVM_ACC_SYNCHRONIZED);
1473       __ jcc(Assembler::zero, L);
1474       __ stop("method needs synchronization");
1475       __ bind(L);
1476     }
1477 #endif
1478   }
1479 
1480   // start execution
1481 #ifdef ASSERT
1482   {
1483     Label L;
1484      const Address monitor_block_top (rbp,
1485                  frame::interpreter_frame_monitor_block_top_offset * wordSize);
1486     __ movptr(rax, monitor_block_top);
1487     __ lea(rax, Address(rbp, rax, Address::times_ptr));
1488     __ cmpptr(rax, rsp);
1489     __ jcc(Assembler::equal, L);
1490     __ stop("broken stack frame setup in interpreter 6");
1491     __ bind(L);
1492   }
1493 #endif
1494 
1495   // jvmti support
1496   __ notify_method_entry();
1497 
1498   __ dispatch_next(vtos);
1499 
1500   // invocation counter overflow
1501   if (inc_counter) {
1502     // Handle overflow of counter and compile method
1503     __ bind(invocation_counter_overflow);
1504     generate_counter_overflow(continue_after_compile);
1505   }
1506 
1507   return entry_point;
1508 }
1509 
1510 //-----------------------------------------------------------------------------
1511 // Exceptions
1512 
1513 void TemplateInterpreterGenerator::generate_throw_exception() {
1514   // Entry point in previous activation (i.e., if the caller was
1515   // interpreted)
1516   Interpreter::_rethrow_exception_entry = __ pc();
1517   // Restore sp to interpreter_frame_last_sp even though we are going
1518   // to empty the expression stack for the exception processing.
1519   __ movptr(Address(rbp, frame::interpreter_frame_last_sp_offset * wordSize), NULL_WORD);
1520   // rax: exception
1521   // rdx: return address/pc that threw exception
1522   __ restore_bcp();    // r13/rsi points to call/send
1523   __ restore_locals();
1524   LP64_ONLY(__ reinit_heapbase());  // restore r12 as heapbase.
1525   // Entry point for exceptions thrown within interpreter code
1526   Interpreter::_throw_exception_entry = __ pc();
1527   // expression stack is undefined here
1528   // rax: exception
1529   // r13/rsi: exception bcp
1530   __ verify_oop(rax);
1531   Register rarg = NOT_LP64(rax) LP64_ONLY(c_rarg1);
1532   LP64_ONLY(__ mov(c_rarg1, rax));
1533 
1534   // expression stack must be empty before entering the VM in case of
1535   // an exception
1536   __ empty_expression_stack();
1537   // find exception handler address and preserve exception oop
1538   __ call_VM(rdx,
1539              CAST_FROM_FN_PTR(address,
1540                           InterpreterRuntime::exception_handler_for_exception),
1541              rarg);
1542   // rax: exception handler entry point
1543   // rdx: preserved exception oop
1544   // r13/rsi: bcp for exception handler
1545   __ push_ptr(rdx); // push exception which is now the only value on the stack
1546   __ jmp(rax); // jump to exception handler (may be _remove_activation_entry!)
1547 
1548   // If the exception is not handled in the current frame the frame is
1549   // removed and the exception is rethrown (i.e. exception
1550   // continuation is _rethrow_exception).
1551   //
1552   // Note: At this point the bci is still the bxi for the instruction
1553   // which caused the exception and the expression stack is
1554   // empty. Thus, for any VM calls at this point, GC will find a legal
1555   // oop map (with empty expression stack).
1556 
1557   // In current activation
1558   // tos: exception
1559   // esi: exception bcp
1560 
1561   //
1562   // JVMTI PopFrame support
1563   //
1564 
1565   Interpreter::_remove_activation_preserving_args_entry = __ pc();
1566   __ empty_expression_stack();
1567   // Set the popframe_processing bit in pending_popframe_condition
1568   // indicating that we are currently handling popframe, so that
1569   // call_VMs that may happen later do not trigger new popframe
1570   // handling cycles.
1571   const Register thread = NOT_LP64(rcx) LP64_ONLY(r15_thread);
1572   NOT_LP64(__ get_thread(thread));
1573   __ movl(rdx, Address(thread, JavaThread::popframe_condition_offset()));
1574   __ orl(rdx, JavaThread::popframe_processing_bit);
1575   __ movl(Address(thread, JavaThread::popframe_condition_offset()), rdx);
1576 
1577   {
1578     // Check to see whether we are returning to a deoptimized frame.
1579     // (The PopFrame call ensures that the caller of the popped frame is
1580     // either interpreted or compiled and deoptimizes it if compiled.)
1581     // In this case, we can't call dispatch_next() after the frame is
1582     // popped, but instead must save the incoming arguments and restore
1583     // them after deoptimization has occurred.
1584     //
1585     // Note that we don't compare the return PC against the
1586     // deoptimization blob's unpack entry because of the presence of
1587     // adapter frames in C2.
1588     Label caller_not_deoptimized;
1589     Register rarg = NOT_LP64(rdx) LP64_ONLY(c_rarg1);
1590     __ movptr(rarg, Address(rbp, frame::return_addr_offset * wordSize));
1591     __ super_call_VM_leaf(CAST_FROM_FN_PTR(address,
1592                                InterpreterRuntime::interpreter_contains), rarg);
1593     __ testl(rax, rax);
1594     __ jcc(Assembler::notZero, caller_not_deoptimized);
1595 
1596     // Compute size of arguments for saving when returning to
1597     // deoptimized caller
1598     __ get_method(rax);
1599     __ movptr(rax, Address(rax, Method::const_offset()));
1600     __ load_unsigned_short(rax, Address(rax, in_bytes(ConstMethod::
1601                                                 size_of_parameters_offset())));
1602     __ shll(rax, Interpreter::logStackElementSize);
1603     __ restore_locals();
1604     __ subptr(rlocals, rax);
1605     __ addptr(rlocals, wordSize);
1606     // Save these arguments
1607     NOT_LP64(__ get_thread(thread));
1608     __ super_call_VM_leaf(CAST_FROM_FN_PTR(address,
1609                                            Deoptimization::
1610                                            popframe_preserve_args),
1611                           thread, rax, rlocals);
1612 
1613     __ remove_activation(vtos, rdx,
1614                          /* throw_monitor_exception */ false,
1615                          /* install_monitor_exception */ false,
1616                          /* notify_jvmdi */ false);
1617 
1618     // Inform deoptimization that it is responsible for restoring
1619     // these arguments
1620     NOT_LP64(__ get_thread(thread));
1621     __ movl(Address(thread, JavaThread::popframe_condition_offset()),
1622             JavaThread::popframe_force_deopt_reexecution_bit);
1623 
1624     // Continue in deoptimization handler
1625     __ jmp(rdx);
1626 
1627     __ bind(caller_not_deoptimized);
1628   }
1629 
1630   __ remove_activation(vtos, rdx, /* rdx result (retaddr) is not used */
1631                        /* throw_monitor_exception */ false,
1632                        /* install_monitor_exception */ false,
1633                        /* notify_jvmdi */ false);
1634 
1635   // Finish with popframe handling
1636   // A previous I2C followed by a deoptimization might have moved the
1637   // outgoing arguments further up the stack. PopFrame expects the
1638   // mutations to those outgoing arguments to be preserved and other
1639   // constraints basically require this frame to look exactly as
1640   // though it had previously invoked an interpreted activation with
1641   // no space between the top of the expression stack (current
1642   // last_sp) and the top of stack. Rather than force deopt to
1643   // maintain this kind of invariant all the time we call a small
1644   // fixup routine to move the mutated arguments onto the top of our
1645   // expression stack if necessary.
1646 #ifndef _LP64
1647   __ mov(rax, rsp);
1648   __ movptr(rbx, Address(rbp, frame::interpreter_frame_last_sp_offset * wordSize));
1649   __ lea(rbx, Address(rbp, rbx, Address::times_ptr));
1650   __ get_thread(thread);
1651   // PC must point into interpreter here
1652   __ set_last_Java_frame(thread, noreg, rbp, __ pc(), noreg);
1653   __ super_call_VM_leaf(CAST_FROM_FN_PTR(address, InterpreterRuntime::popframe_move_outgoing_args), thread, rax, rbx);
1654   __ get_thread(thread);
1655 #else
1656   __ mov(c_rarg1, rsp);
1657   __ movptr(c_rarg2, Address(rbp, frame::interpreter_frame_last_sp_offset * wordSize));
1658   __ lea(c_rarg2, Address(rbp, c_rarg2, Address::times_ptr));
1659   // PC must point into interpreter here
1660   __ set_last_Java_frame(noreg, rbp, __ pc(), rscratch1);
1661   __ super_call_VM_leaf(CAST_FROM_FN_PTR(address, InterpreterRuntime::popframe_move_outgoing_args), r15_thread, c_rarg1, c_rarg2);
1662 #endif
1663   __ reset_last_Java_frame(thread, true);
1664 
1665   // Restore the last_sp and null it out
1666   __ movptr(rcx, Address(rbp, frame::interpreter_frame_last_sp_offset * wordSize));
1667   __ lea(rsp, Address(rbp, rcx, Address::times_ptr));
1668   __ movptr(Address(rbp, frame::interpreter_frame_last_sp_offset * wordSize), NULL_WORD);
1669 
1670   __ restore_bcp();
1671   __ restore_locals();
1672   // The method data pointer was incremented already during
1673   // call profiling. We have to restore the mdp for the current bcp.
1674   if (ProfileInterpreter) {
1675     __ set_method_data_pointer_for_bcp();
1676   }
1677 
1678   // Clear the popframe condition flag
1679   NOT_LP64(__ get_thread(thread));
1680   __ movl(Address(thread, JavaThread::popframe_condition_offset()),
1681           JavaThread::popframe_inactive);
1682 
1683 #if INCLUDE_JVMTI
1684   {
1685     Label L_done;
1686     const Register local0 = rlocals;
1687 
1688     __ cmpb(Address(rbcp, 0), Bytecodes::_invokestatic);
1689     __ jcc(Assembler::notEqual, L_done);
1690 
1691     // The member name argument must be restored if _invokestatic is re-executed after a PopFrame call.
1692     // Detect such a case in the InterpreterRuntime function and return the member name argument, or null.
1693 
1694     __ get_method(rdx);
1695     __ movptr(rax, Address(local0, 0));
1696     __ call_VM(rax, CAST_FROM_FN_PTR(address, InterpreterRuntime::member_name_arg_or_null), rax, rdx, rbcp);
1697 
1698     __ testptr(rax, rax);
1699     __ jcc(Assembler::zero, L_done);
1700 
1701     __ movptr(Address(rbx, 0), rax);
1702     __ bind(L_done);
1703   }
1704 #endif // INCLUDE_JVMTI
1705 
1706   __ dispatch_next(vtos);
1707   // end of PopFrame support
1708 
1709   Interpreter::_remove_activation_entry = __ pc();
1710 
1711   // preserve exception over this code sequence
1712   __ pop_ptr(rax);
1713   NOT_LP64(__ get_thread(thread));
1714   __ movptr(Address(thread, JavaThread::vm_result_offset()), rax);
1715   // remove the activation (without doing throws on illegalMonitorExceptions)
1716   __ remove_activation(vtos, rdx, false, true, false);
1717   // restore exception
1718   NOT_LP64(__ get_thread(thread));
1719   __ get_vm_result(rax, thread);
1720 
1721   // In between activations - previous activation type unknown yet
1722   // compute continuation point - the continuation point expects the
1723   // following registers set up:
1724   //
1725   // rax: exception
1726   // rdx: return address/pc that threw exception
1727   // rsp: expression stack of caller
1728   // rbp: ebp of caller
1729   __ push(rax);                                  // save exception
1730   __ push(rdx);                                  // save return address
1731   __ super_call_VM_leaf(CAST_FROM_FN_PTR(address,
1732                           SharedRuntime::exception_handler_for_return_address),
1733                         thread, rdx);
1734   __ mov(rbx, rax);                              // save exception handler
1735   __ pop(rdx);                                   // restore return address
1736   __ pop(rax);                                   // restore exception
1737   // Note that an "issuing PC" is actually the next PC after the call
1738   __ jmp(rbx);                                   // jump to exception
1739                                                  // handler of caller
1740 }
1741 
1742 
1743 //
1744 // JVMTI ForceEarlyReturn support
1745 //
1746 address TemplateInterpreterGenerator::generate_earlyret_entry_for(TosState state) {
1747   address entry = __ pc();
1748 
1749   __ restore_bcp();
1750   __ restore_locals();
1751   __ empty_expression_stack();
1752   __ load_earlyret_value(state);  // 32 bits returns value in rdx, so don't reuse
1753 
1754   const Register thread = NOT_LP64(rcx) LP64_ONLY(r15_thread);
1755   NOT_LP64(__ get_thread(thread));
1756   __ movptr(rcx, Address(thread, JavaThread::jvmti_thread_state_offset()));
1757   Address cond_addr(rcx, JvmtiThreadState::earlyret_state_offset());
1758 
1759   // Clear the earlyret state
1760   __ movl(cond_addr, JvmtiThreadState::earlyret_inactive);
1761 
1762   __ remove_activation(state, rsi,
1763                        false, /* throw_monitor_exception */
1764                        false, /* install_monitor_exception */
1765                        true); /* notify_jvmdi */
1766   __ jmp(rsi);
1767 
1768   return entry;
1769 } // end of ForceEarlyReturn support
1770 
1771 
1772 //-----------------------------------------------------------------------------
1773 // Helper for vtos entry point generation
1774 
1775 void TemplateInterpreterGenerator::set_vtos_entry_points(Template* t,
1776                                                          address& bep,
1777                                                          address& cep,
1778                                                          address& sep,
1779                                                          address& aep,
1780                                                          address& iep,
1781                                                          address& lep,
1782                                                          address& fep,
1783                                                          address& dep,
1784                                                          address& vep) {
1785   assert(t->is_valid() && t->tos_in() == vtos, "illegal template");
1786   Label L;
1787 #ifndef _LP64
1788   fep = __ pc();     // ftos entry point
1789       __ push(ftos);
1790       __ jmpb(L);
1791   dep = __ pc();     // dtos entry point
1792       __ push(dtos);
1793       __ jmpb(L);
1794 #else
1795   fep = __ pc();     // ftos entry point
1796       __ push_f(xmm0);
1797       __ jmpb(L);
1798   dep = __ pc();     // dtos entry point
1799       __ push_d(xmm0);
1800       __ jmpb(L);
1801 #endif // _LP64
1802   lep = __ pc();     // ltos entry point
1803       __ push_l();
1804       __ jmpb(L);
1805   aep = bep = cep = sep = iep = __ pc();      // [abcsi]tos entry point
1806       __ push_i_or_ptr();
1807   vep = __ pc();    // vtos entry point
1808   __ bind(L);
1809   generate_and_dispatch(t);
1810 }
1811 
1812 //-----------------------------------------------------------------------------
1813 
1814 // Non-product code
1815 #ifndef PRODUCT
1816 
1817 address TemplateInterpreterGenerator::generate_trace_code(TosState state) {
1818   address entry = __ pc();
1819 
1820 #ifndef _LP64
1821   // prepare expression stack
1822   __ pop(rcx);          // pop return address so expression stack is 'pure'
1823   __ push(state);       // save tosca
1824 
1825   // pass tosca registers as arguments & call tracer
1826   __ call_VM(noreg, CAST_FROM_FN_PTR(address, InterpreterRuntime::trace_bytecode), rcx, rax, rdx);
1827   __ mov(rcx, rax);     // make sure return address is not destroyed by pop(state)
1828   __ pop(state);        // restore tosca
1829 
1830   // return
1831   __ jmp(rcx);
1832 #else
1833   __ push(state);
1834   __ push(c_rarg0);
1835   __ push(c_rarg1);
1836   __ push(c_rarg2);
1837   __ push(c_rarg3);
1838   __ mov(c_rarg2, rax);  // Pass itos
1839 #ifdef _WIN64
1840   __ movflt(xmm3, xmm0); // Pass ftos
1841 #endif
1842   __ call_VM(noreg,
1843              CAST_FROM_FN_PTR(address, InterpreterRuntime::trace_bytecode),
1844              c_rarg1, c_rarg2, c_rarg3);
1845   __ pop(c_rarg3);
1846   __ pop(c_rarg2);
1847   __ pop(c_rarg1);
1848   __ pop(c_rarg0);
1849   __ pop(state);
1850   __ ret(0);                                   // return from result handler
1851 #endif // _LP64
1852 
1853   return entry;
1854 }
1855 
1856 void TemplateInterpreterGenerator::count_bytecode() {
1857   __ incrementl(ExternalAddress((address) &BytecodeCounter::_counter_value), rscratch1);
1858 }
1859 
1860 void TemplateInterpreterGenerator::histogram_bytecode(Template* t) {
1861   __ incrementl(ExternalAddress((address) &BytecodeHistogram::_counters[t->bytecode()]), rscratch1);
1862 }
1863 
1864 void TemplateInterpreterGenerator::histogram_bytecode_pair(Template* t) {
1865   __ mov32(rbx, ExternalAddress((address) &BytecodePairHistogram::_index));
1866   __ shrl(rbx, BytecodePairHistogram::log2_number_of_codes);
1867   __ orl(rbx,
1868          ((int) t->bytecode()) <<
1869          BytecodePairHistogram::log2_number_of_codes);
1870   __ mov32(ExternalAddress((address) &BytecodePairHistogram::_index), rbx, rscratch1);
1871   __ lea(rscratch1, ExternalAddress((address) BytecodePairHistogram::_counters));
1872   __ incrementl(Address(rscratch1, rbx, Address::times_4));
1873 }
1874 
1875 
1876 void TemplateInterpreterGenerator::trace_bytecode(Template* t) {
1877   // Call a little run-time stub to avoid blow-up for each bytecode.
1878   // The run-time runtime saves the right registers, depending on
1879   // the tosca in-state for the given template.
1880 
1881   assert(Interpreter::trace_code(t->tos_in()) != nullptr,
1882          "entry must have been generated");
1883 #ifndef _LP64
1884   __ call(RuntimeAddress(Interpreter::trace_code(t->tos_in())));
1885 #else
1886   __ mov(r12, rsp); // remember sp (can only use r12 if not using call_VM)
1887   __ andptr(rsp, -16); // align stack as required by ABI
1888   __ call(RuntimeAddress(Interpreter::trace_code(t->tos_in())));
1889   __ mov(rsp, r12); // restore sp
1890   __ reinit_heapbase();
1891 #endif // _LP64
1892 }
1893 
1894 
1895 void TemplateInterpreterGenerator::stop_interpreter_at() {
1896   Label L;
1897   __ cmp32(ExternalAddress((address) &BytecodeCounter::_counter_value),
1898            StopInterpreterAt,
1899            rscratch1);
1900   __ jcc(Assembler::notEqual, L);
1901   __ int3();
1902   __ bind(L);
1903 }
1904 #endif // !PRODUCT