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