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_resume_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   __ push_cont_fastpath();
1077 
1078   // Call the native method.
1079   __ call(rax);
1080   // 32: result potentially in rdx:rax or ST0
1081   // 64: result potentially in rax or xmm0
1082 
1083   __ pop_cont_fastpath();
1084 
1085   // Verify or restore cpu control state after JNI call
1086   __ restore_cpu_control_state_after_jni(rscratch1);
1087 
1088   // NOTE: The order of these pushes is known to frame::interpreter_frame_result
1089   // in order to extract the result of a method call. If the order of these
1090   // pushes change or anything else is added to the stack then the code in
1091   // interpreter_frame_result must also change.
1092 
1093 #ifndef _LP64
1094   // save potential result in ST(0) & rdx:rax
1095   // (if result handler is the T_FLOAT or T_DOUBLE handler, result must be in ST0 -
1096   // the check is necessary to avoid potential Intel FPU overflow problems by saving/restoring 'empty' FPU registers)
1097   // It is safe to do this push because state is _thread_in_native and return address will be found
1098   // via _last_native_pc and not via _last_jave_sp
1099 
1100   // NOTE: the order of these push(es) is known to frame::interpreter_frame_result.
1101   // If the order changes or anything else is added to the stack the code in
1102   // interpreter_frame_result will have to be changed.
1103 
1104   { Label L;
1105     Label push_double;
1106     ExternalAddress float_handler(AbstractInterpreter::result_handler(T_FLOAT));
1107     ExternalAddress double_handler(AbstractInterpreter::result_handler(T_DOUBLE));
1108     __ cmpptr(Address(rbp, (frame::interpreter_frame_result_handler_offset)*wordSize),
1109               float_handler.addr(), noreg);
1110     __ jcc(Assembler::equal, push_double);
1111     __ cmpptr(Address(rbp, (frame::interpreter_frame_result_handler_offset)*wordSize),
1112               double_handler.addr(), noreg);
1113     __ jcc(Assembler::notEqual, L);
1114     __ bind(push_double);
1115     __ push_d(); // FP values are returned using the FPU, so push FPU contents (even if UseSSE > 0).
1116     __ bind(L);
1117   }
1118 #else
1119   __ push(dtos);
1120 #endif // _LP64
1121 
1122   __ push(ltos);
1123 
1124   // change thread state
1125   NOT_LP64(__ get_thread(thread));
1126   __ movl(Address(thread, JavaThread::thread_state_offset()),
1127           _thread_in_native_trans);
1128 
1129   // Force this write out before the read below
1130   if (!UseSystemMemoryBarrier) {
1131     __ membar(Assembler::Membar_mask_bits(
1132                 Assembler::LoadLoad | Assembler::LoadStore |
1133                 Assembler::StoreLoad | Assembler::StoreStore));
1134   }
1135 #ifndef _LP64
1136   if (AlwaysRestoreFPU) {
1137     //  Make sure the control word is correct.
1138     __ fldcw(ExternalAddress(StubRoutines::x86::addr_fpu_cntrl_wrd_std()));
1139   }
1140 #endif // _LP64
1141 
1142   // check for safepoint operation in progress and/or pending suspend requests
1143   {
1144     Label Continue;
1145     Label slow_path;
1146 
1147     __ safepoint_poll(slow_path, thread, true /* at_return */, false /* in_nmethod */);
1148 
1149     __ cmpl(Address(thread, JavaThread::suspend_flags_offset()), 0);
1150     __ jcc(Assembler::equal, Continue);
1151     __ bind(slow_path);
1152 
1153     // Don't use call_VM as it will see a possible pending exception
1154     // and forward it and never return here preventing us from
1155     // clearing _last_native_pc down below.  Also can't use
1156     // call_VM_leaf either as it will check to see if r13 & r14 are
1157     // preserved and correspond to the bcp/locals pointers. So we do a
1158     // runtime call by hand.
1159     //
1160 #ifndef _LP64
1161     __ push(thread);
1162     __ call(RuntimeAddress(CAST_FROM_FN_PTR(address,
1163                                             JavaThread::check_special_condition_for_native_trans)));
1164     __ increment(rsp, wordSize);
1165     __ get_thread(thread);
1166 #else
1167     __ mov(c_rarg0, r15_thread);
1168     __ mov(r12, rsp); // remember sp (can only use r12 if not using call_VM)
1169     __ subptr(rsp, frame::arg_reg_save_area_bytes); // windows
1170     __ andptr(rsp, -16); // align stack as required by ABI
1171     __ call(RuntimeAddress(CAST_FROM_FN_PTR(address, JavaThread::check_special_condition_for_native_trans)));
1172     __ mov(rsp, r12); // restore sp
1173     __ reinit_heapbase();
1174 #endif // _LP64
1175     __ bind(Continue);
1176   }
1177 
1178   // change thread state
1179   __ movl(Address(thread, JavaThread::thread_state_offset()), _thread_in_Java);
1180 
1181   // Check preemption for Object.wait()
1182   Label not_preempted;
1183   __ movptr(rscratch1, Address(r15_thread, JavaThread::preempt_alternate_return_offset()));
1184   __ cmpptr(rscratch1, NULL_WORD);
1185   __ jccb(Assembler::equal, not_preempted);
1186   __ movptr(Address(r15_thread, JavaThread::preempt_alternate_return_offset()), NULL_WORD);
1187   __ jmp(rscratch1);
1188   Interpreter::_native_frame_resume_entry = __ pc();
1189   // On resume we need to set up stack as expected
1190   __ push(dtos);
1191   __ push(ltos);
1192   __ bind(not_preempted);
1193 
1194   // reset_last_Java_frame
1195   __ reset_last_Java_frame(thread, true);
1196 
1197   if (CheckJNICalls) {
1198     // clear_pending_jni_exception_check
1199     __ movptr(Address(thread, JavaThread::pending_jni_exception_check_fn_offset()), NULL_WORD);
1200   }
1201 
1202   // reset handle block
1203   __ movptr(t, Address(thread, JavaThread::active_handles_offset()));
1204   __ movl(Address(t, JNIHandleBlock::top_offset()), NULL_WORD);
1205 
1206   // If result is an oop unbox and store it in frame where gc will see it
1207   // and result handler will pick it up
1208 
1209   {
1210     Label no_oop;
1211     __ lea(t, ExternalAddress(AbstractInterpreter::result_handler(T_OBJECT)));
1212     __ cmpptr(t, Address(rbp, frame::interpreter_frame_result_handler_offset*wordSize));
1213     __ jcc(Assembler::notEqual, no_oop);
1214     // retrieve result
1215     __ pop(ltos);
1216     // Unbox oop result, e.g. JNIHandles::resolve value.
1217     __ resolve_jobject(rax /* value */,
1218                        thread /* thread */,
1219                        t /* tmp */);
1220     __ movptr(Address(rbp, frame::interpreter_frame_oop_temp_offset*wordSize), rax);
1221     // keep stack depth as expected by pushing oop which will eventually be discarded
1222     __ push(ltos);
1223     __ bind(no_oop);
1224   }
1225 
1226 
1227   {
1228     Label no_reguard;
1229     __ cmpl(Address(thread, JavaThread::stack_guard_state_offset()),
1230             StackOverflow::stack_guard_yellow_reserved_disabled);
1231     __ jcc(Assembler::notEqual, no_reguard);
1232 
1233     __ pusha(); // XXX only save smashed registers
1234 #ifndef _LP64
1235     __ call(RuntimeAddress(CAST_FROM_FN_PTR(address, SharedRuntime::reguard_yellow_pages)));
1236     __ popa();
1237 #else
1238     __ mov(r12, rsp); // remember sp (can only use r12 if not using call_VM)
1239     __ subptr(rsp, frame::arg_reg_save_area_bytes); // windows
1240     __ andptr(rsp, -16); // align stack as required by ABI
1241     __ call(RuntimeAddress(CAST_FROM_FN_PTR(address, SharedRuntime::reguard_yellow_pages)));
1242     __ mov(rsp, r12); // restore sp
1243     __ popa(); // XXX only restore smashed registers
1244     __ reinit_heapbase();
1245 #endif // _LP64
1246 
1247     __ bind(no_reguard);
1248   }
1249 
1250 
1251   // The method register is junk from after the thread_in_native transition
1252   // until here.  Also can't call_VM until the bcp has been
1253   // restored.  Need bcp for throwing exception below so get it now.
1254   __ get_method(method);
1255 
1256   // restore to have legal interpreter frame, i.e., bci == 0 <=> code_base()
1257   __ movptr(rbcp, Address(method, Method::const_offset()));   // get ConstMethod*
1258   __ lea(rbcp, Address(rbcp, ConstMethod::codes_offset()));    // get codebase
1259 
1260   // handle exceptions (exception handling will handle unlocking!)
1261   {
1262     Label L;
1263     __ cmpptr(Address(thread, Thread::pending_exception_offset()), NULL_WORD);
1264     __ jcc(Assembler::zero, L);
1265     // Note: At some point we may want to unify this with the code
1266     // used in call_VM_base(); i.e., we should use the
1267     // StubRoutines::forward_exception code. For now this doesn't work
1268     // here because the rsp is not correctly set at this point.
1269     __ MacroAssembler::call_VM(noreg,
1270                                CAST_FROM_FN_PTR(address,
1271                                InterpreterRuntime::throw_pending_exception));
1272     __ should_not_reach_here();
1273     __ bind(L);
1274   }
1275 
1276   // do unlocking if necessary
1277   {
1278     Label L;
1279     __ movl(t, Address(method, Method::access_flags_offset()));
1280     __ testl(t, JVM_ACC_SYNCHRONIZED);
1281     __ jcc(Assembler::zero, L);
1282     // the code below should be shared with interpreter macro
1283     // assembler implementation
1284     {
1285       Label unlock;
1286       // BasicObjectLock will be first in list, since this is a
1287       // synchronized method. However, need to check that the object
1288       // has not been unlocked by an explicit monitorexit bytecode.
1289       const Address monitor(rbp,
1290                             (intptr_t)(frame::interpreter_frame_initial_sp_offset *
1291                                        wordSize - (int)sizeof(BasicObjectLock)));
1292 
1293       const Register regmon = NOT_LP64(rdx) LP64_ONLY(c_rarg1);
1294 
1295       // monitor expect in c_rarg1 for slow unlock path
1296       __ lea(regmon, monitor); // address of first monitor
1297 
1298       __ movptr(t, Address(regmon, BasicObjectLock::obj_offset()));
1299       __ testptr(t, t);
1300       __ jcc(Assembler::notZero, unlock);
1301 
1302       // Entry already unlocked, need to throw exception
1303       __ MacroAssembler::call_VM(noreg,
1304                                  CAST_FROM_FN_PTR(address,
1305                    InterpreterRuntime::throw_illegal_monitor_state_exception));
1306       __ should_not_reach_here();
1307 
1308       __ bind(unlock);
1309       __ unlock_object(regmon);
1310     }
1311     __ bind(L);
1312   }
1313 
1314   // jvmti support
1315   // Note: This must happen _after_ handling/throwing any exceptions since
1316   //       the exception handler code notifies the runtime of method exits
1317   //       too. If this happens before, method entry/exit notifications are
1318   //       not properly paired (was bug - gri 11/22/99).
1319   __ notify_method_exit(vtos, InterpreterMacroAssembler::NotifyJVMTI);
1320 
1321   // restore potential result in edx:eax, call result handler to
1322   // restore potential result in ST0 & handle result
1323 
1324   __ pop(ltos);
1325   LP64_ONLY( __ pop(dtos));
1326 
1327   __ movptr(t, Address(rbp,
1328                        (frame::interpreter_frame_result_handler_offset) * wordSize));
1329   __ call(t);
1330 
1331   // remove activation
1332   __ movptr(t, Address(rbp,
1333                        frame::interpreter_frame_sender_sp_offset *
1334                        wordSize)); // get sender sp
1335   __ leave();                                // remove frame anchor
1336   __ pop(rdi);                               // get return address
1337   __ mov(rsp, t);                            // set sp to sender sp
1338   __ jmp(rdi);
1339 
1340   if (inc_counter) {
1341     // Handle overflow of counter and compile method
1342     __ bind(invocation_counter_overflow);
1343     generate_counter_overflow(continue_after_compile);
1344   }
1345 
1346   return entry_point;
1347 }
1348 
1349 // Abstract method entry
1350 // Attempt to execute abstract method. Throw exception
1351 address TemplateInterpreterGenerator::generate_abstract_entry(void) {
1352 
1353   address entry_point = __ pc();
1354 
1355   // abstract method entry
1356 
1357   //  pop return address, reset last_sp to null
1358   __ empty_expression_stack();
1359   __ restore_bcp();      // rsi must be correct for exception handler   (was destroyed)
1360   __ restore_locals();   // make sure locals pointer is correct as well (was destroyed)
1361 
1362   // throw exception
1363   __ call_VM(noreg, CAST_FROM_FN_PTR(address, InterpreterRuntime::throw_AbstractMethodErrorWithMethod), rbx);
1364   // the call_VM checks for exception, so we should never return here.
1365   __ should_not_reach_here();
1366 
1367   return entry_point;
1368 }
1369 
1370 //
1371 // Generic interpreted method entry to (asm) interpreter
1372 //
1373 address TemplateInterpreterGenerator::generate_normal_entry(bool synchronized) {
1374   // determine code generation flags
1375   bool inc_counter  = UseCompiler || CountCompiledCalls;
1376 
1377   // ebx: Method*
1378   // rbcp: sender sp (set in InterpreterMacroAssembler::prepare_to_jump_from_interpreted / generate_call_stub)
1379   address entry_point = __ pc();
1380 
1381   const Address constMethod(rbx, Method::const_offset());
1382   const Address access_flags(rbx, Method::access_flags_offset());
1383   const Address size_of_parameters(rdx,
1384                                    ConstMethod::size_of_parameters_offset());
1385   const Address size_of_locals(rdx, ConstMethod::size_of_locals_offset());
1386 
1387 
1388   // get parameter size (always needed)
1389   __ movptr(rdx, constMethod);
1390   __ load_unsigned_short(rcx, size_of_parameters);
1391 
1392   // rbx: Method*
1393   // rcx: size of parameters
1394   // rbcp: sender_sp (could differ from sp+wordSize if we were called via c2i )
1395 
1396   __ load_unsigned_short(rdx, size_of_locals); // get size of locals in words
1397   __ subl(rdx, rcx); // rdx = no. of additional locals
1398 
1399   // YYY
1400 //   __ incrementl(rdx);
1401 //   __ andl(rdx, -2);
1402 
1403   // see if we've got enough room on the stack for locals plus overhead.
1404   generate_stack_overflow_check();
1405 
1406   // get return address
1407   __ pop(rax);
1408 
1409   // compute beginning of parameters
1410   __ lea(rlocals, Address(rsp, rcx, Interpreter::stackElementScale(), -wordSize));
1411 
1412   // rdx - # of additional locals
1413   // allocate space for locals
1414   // explicitly initialize locals
1415   {
1416     Label exit, loop;
1417     __ testl(rdx, rdx);
1418     __ jcc(Assembler::lessEqual, exit); // do nothing if rdx <= 0
1419     __ bind(loop);
1420     __ push(NULL_WORD); // initialize local variables
1421     __ decrementl(rdx); // until everything initialized
1422     __ jcc(Assembler::greater, loop);
1423     __ bind(exit);
1424   }
1425 
1426   // initialize fixed part of activation frame
1427   generate_fixed_frame(false);
1428 
1429   // make sure method is not native & not abstract
1430 #ifdef ASSERT
1431   __ movl(rax, access_flags);
1432   {
1433     Label L;
1434     __ testl(rax, JVM_ACC_NATIVE);
1435     __ jcc(Assembler::zero, L);
1436     __ stop("tried to execute native method as non-native");
1437     __ bind(L);
1438   }
1439   {
1440     Label L;
1441     __ testl(rax, JVM_ACC_ABSTRACT);
1442     __ jcc(Assembler::zero, L);
1443     __ stop("tried to execute abstract method in interpreter");
1444     __ bind(L);
1445   }
1446 #endif
1447 
1448   // Since at this point in the method invocation the exception
1449   // handler would try to exit the monitor of synchronized methods
1450   // which hasn't been entered yet, we set the thread local variable
1451   // _do_not_unlock_if_synchronized to true. The remove_activation
1452   // will check this flag.
1453 
1454   const Register thread = NOT_LP64(rax) LP64_ONLY(r15_thread);
1455   NOT_LP64(__ get_thread(thread));
1456   const Address do_not_unlock_if_synchronized(thread,
1457         in_bytes(JavaThread::do_not_unlock_if_synchronized_offset()));
1458   __ movbool(do_not_unlock_if_synchronized, true);
1459 
1460   __ profile_parameters_type(rax, rcx, rdx);
1461   // increment invocation count & check for overflow
1462   Label invocation_counter_overflow;
1463   if (inc_counter) {
1464     generate_counter_incr(&invocation_counter_overflow);
1465   }
1466 
1467   Label continue_after_compile;
1468   __ bind(continue_after_compile);
1469 
1470   // check for synchronized interpreted methods
1471   bang_stack_shadow_pages(false);
1472 
1473   // reset the _do_not_unlock_if_synchronized flag
1474   NOT_LP64(__ get_thread(thread));
1475   __ movbool(do_not_unlock_if_synchronized, false);
1476 
1477   // check for synchronized methods
1478   // Must happen AFTER invocation_counter check and stack overflow check,
1479   // so method is not locked if overflows.
1480   if (synchronized) {
1481     // Allocate monitor and lock method
1482     lock_method();
1483   } else {
1484     // no synchronization necessary
1485 #ifdef ASSERT
1486     {
1487       Label L;
1488       __ movl(rax, access_flags);
1489       __ testl(rax, JVM_ACC_SYNCHRONIZED);
1490       __ jcc(Assembler::zero, L);
1491       __ stop("method needs synchronization");
1492       __ bind(L);
1493     }
1494 #endif
1495   }
1496 
1497   // start execution
1498 #ifdef ASSERT
1499   {
1500     Label L;
1501      const Address monitor_block_top (rbp,
1502                  frame::interpreter_frame_monitor_block_top_offset * wordSize);
1503     __ movptr(rax, monitor_block_top);
1504     __ lea(rax, Address(rbp, rax, Address::times_ptr));
1505     __ cmpptr(rax, rsp);
1506     __ jcc(Assembler::equal, L);
1507     __ stop("broken stack frame setup in interpreter 6");
1508     __ bind(L);
1509   }
1510 #endif
1511 
1512   // jvmti support
1513   __ notify_method_entry();
1514 
1515   __ dispatch_next(vtos);
1516 
1517   // invocation counter overflow
1518   if (inc_counter) {
1519     // Handle overflow of counter and compile method
1520     __ bind(invocation_counter_overflow);
1521     generate_counter_overflow(continue_after_compile);
1522   }
1523 
1524   return entry_point;
1525 }
1526 
1527 //-----------------------------------------------------------------------------
1528 // Exceptions
1529 
1530 void TemplateInterpreterGenerator::generate_throw_exception() {
1531   // Entry point in previous activation (i.e., if the caller was
1532   // interpreted)
1533   Interpreter::_rethrow_exception_entry = __ pc();
1534   // Restore sp to interpreter_frame_last_sp even though we are going
1535   // to empty the expression stack for the exception processing.
1536   __ movptr(Address(rbp, frame::interpreter_frame_last_sp_offset * wordSize), NULL_WORD);
1537   // rax: exception
1538   // rdx: return address/pc that threw exception
1539   __ restore_bcp();    // r13/rsi points to call/send
1540   __ restore_locals();
1541   LP64_ONLY(__ reinit_heapbase());  // restore r12 as heapbase.
1542   // Entry point for exceptions thrown within interpreter code
1543   Interpreter::_throw_exception_entry = __ pc();
1544   // expression stack is undefined here
1545   // rax: exception
1546   // r13/rsi: exception bcp
1547   __ verify_oop(rax);
1548   Register rarg = NOT_LP64(rax) LP64_ONLY(c_rarg1);
1549   LP64_ONLY(__ mov(c_rarg1, rax));
1550 
1551   // expression stack must be empty before entering the VM in case of
1552   // an exception
1553   __ empty_expression_stack();
1554   // find exception handler address and preserve exception oop
1555   __ call_VM(rdx,
1556              CAST_FROM_FN_PTR(address,
1557                           InterpreterRuntime::exception_handler_for_exception),
1558              rarg);
1559   // rax: exception handler entry point
1560   // rdx: preserved exception oop
1561   // r13/rsi: bcp for exception handler
1562   __ push_ptr(rdx); // push exception which is now the only value on the stack
1563   __ jmp(rax); // jump to exception handler (may be _remove_activation_entry!)
1564 
1565   // If the exception is not handled in the current frame the frame is
1566   // removed and the exception is rethrown (i.e. exception
1567   // continuation is _rethrow_exception).
1568   //
1569   // Note: At this point the bci is still the bxi for the instruction
1570   // which caused the exception and the expression stack is
1571   // empty. Thus, for any VM calls at this point, GC will find a legal
1572   // oop map (with empty expression stack).
1573 
1574   // In current activation
1575   // tos: exception
1576   // esi: exception bcp
1577 
1578   //
1579   // JVMTI PopFrame support
1580   //
1581 
1582   Interpreter::_remove_activation_preserving_args_entry = __ pc();
1583   __ empty_expression_stack();
1584   // Set the popframe_processing bit in pending_popframe_condition
1585   // indicating that we are currently handling popframe, so that
1586   // call_VMs that may happen later do not trigger new popframe
1587   // handling cycles.
1588   const Register thread = NOT_LP64(rcx) LP64_ONLY(r15_thread);
1589   NOT_LP64(__ get_thread(thread));
1590   __ movl(rdx, Address(thread, JavaThread::popframe_condition_offset()));
1591   __ orl(rdx, JavaThread::popframe_processing_bit);
1592   __ movl(Address(thread, JavaThread::popframe_condition_offset()), rdx);
1593 
1594   {
1595     // Check to see whether we are returning to a deoptimized frame.
1596     // (The PopFrame call ensures that the caller of the popped frame is
1597     // either interpreted or compiled and deoptimizes it if compiled.)
1598     // In this case, we can't call dispatch_next() after the frame is
1599     // popped, but instead must save the incoming arguments and restore
1600     // them after deoptimization has occurred.
1601     //
1602     // Note that we don't compare the return PC against the
1603     // deoptimization blob's unpack entry because of the presence of
1604     // adapter frames in C2.
1605     Label caller_not_deoptimized;
1606     Register rarg = NOT_LP64(rdx) LP64_ONLY(c_rarg1);
1607     __ movptr(rarg, Address(rbp, frame::return_addr_offset * wordSize));
1608     __ super_call_VM_leaf(CAST_FROM_FN_PTR(address,
1609                                InterpreterRuntime::interpreter_contains), rarg);
1610     __ testl(rax, rax);
1611     __ jcc(Assembler::notZero, caller_not_deoptimized);
1612 
1613     // Compute size of arguments for saving when returning to
1614     // deoptimized caller
1615     __ get_method(rax);
1616     __ movptr(rax, Address(rax, Method::const_offset()));
1617     __ load_unsigned_short(rax, Address(rax, in_bytes(ConstMethod::
1618                                                 size_of_parameters_offset())));
1619     __ shll(rax, Interpreter::logStackElementSize);
1620     __ restore_locals();
1621     __ subptr(rlocals, rax);
1622     __ addptr(rlocals, wordSize);
1623     // Save these arguments
1624     NOT_LP64(__ get_thread(thread));
1625     __ super_call_VM_leaf(CAST_FROM_FN_PTR(address,
1626                                            Deoptimization::
1627                                            popframe_preserve_args),
1628                           thread, rax, rlocals);
1629 
1630     __ remove_activation(vtos, rdx,
1631                          /* throw_monitor_exception */ false,
1632                          /* install_monitor_exception */ false,
1633                          /* notify_jvmdi */ false);
1634 
1635     // Inform deoptimization that it is responsible for restoring
1636     // these arguments
1637     NOT_LP64(__ get_thread(thread));
1638     __ movl(Address(thread, JavaThread::popframe_condition_offset()),
1639             JavaThread::popframe_force_deopt_reexecution_bit);
1640 
1641     // Continue in deoptimization handler
1642     __ jmp(rdx);
1643 
1644     __ bind(caller_not_deoptimized);
1645   }
1646 
1647   __ remove_activation(vtos, rdx, /* rdx result (retaddr) is not used */
1648                        /* throw_monitor_exception */ false,
1649                        /* install_monitor_exception */ false,
1650                        /* notify_jvmdi */ false);
1651 
1652   // Finish with popframe handling
1653   // A previous I2C followed by a deoptimization might have moved the
1654   // outgoing arguments further up the stack. PopFrame expects the
1655   // mutations to those outgoing arguments to be preserved and other
1656   // constraints basically require this frame to look exactly as
1657   // though it had previously invoked an interpreted activation with
1658   // no space between the top of the expression stack (current
1659   // last_sp) and the top of stack. Rather than force deopt to
1660   // maintain this kind of invariant all the time we call a small
1661   // fixup routine to move the mutated arguments onto the top of our
1662   // expression stack if necessary.
1663 #ifndef _LP64
1664   __ mov(rax, rsp);
1665   __ movptr(rbx, Address(rbp, frame::interpreter_frame_last_sp_offset * wordSize));
1666   __ lea(rbx, Address(rbp, rbx, Address::times_ptr));
1667   __ get_thread(thread);
1668   // PC must point into interpreter here
1669   __ set_last_Java_frame(thread, noreg, rbp, __ pc(), noreg);
1670   __ super_call_VM_leaf(CAST_FROM_FN_PTR(address, InterpreterRuntime::popframe_move_outgoing_args), thread, rax, rbx);
1671   __ get_thread(thread);
1672 #else
1673   __ mov(c_rarg1, rsp);
1674   __ movptr(c_rarg2, Address(rbp, frame::interpreter_frame_last_sp_offset * wordSize));
1675   __ lea(c_rarg2, Address(rbp, c_rarg2, Address::times_ptr));
1676   // PC must point into interpreter here
1677   __ set_last_Java_frame(noreg, rbp, __ pc(), rscratch1);
1678   __ super_call_VM_leaf(CAST_FROM_FN_PTR(address, InterpreterRuntime::popframe_move_outgoing_args), r15_thread, c_rarg1, c_rarg2);
1679 #endif
1680   __ reset_last_Java_frame(thread, true);
1681 
1682   // Restore the last_sp and null it out
1683   __ movptr(rcx, Address(rbp, frame::interpreter_frame_last_sp_offset * wordSize));
1684   __ lea(rsp, Address(rbp, rcx, Address::times_ptr));
1685   __ movptr(Address(rbp, frame::interpreter_frame_last_sp_offset * wordSize), NULL_WORD);
1686 
1687   __ restore_bcp();
1688   __ restore_locals();
1689   // The method data pointer was incremented already during
1690   // call profiling. We have to restore the mdp for the current bcp.
1691   if (ProfileInterpreter) {
1692     __ set_method_data_pointer_for_bcp();
1693   }
1694 
1695   // Clear the popframe condition flag
1696   NOT_LP64(__ get_thread(thread));
1697   __ movl(Address(thread, JavaThread::popframe_condition_offset()),
1698           JavaThread::popframe_inactive);
1699 
1700 #if INCLUDE_JVMTI
1701   {
1702     Label L_done;
1703     const Register local0 = rlocals;
1704 
1705     __ cmpb(Address(rbcp, 0), Bytecodes::_invokestatic);
1706     __ jcc(Assembler::notEqual, L_done);
1707 
1708     // The member name argument must be restored if _invokestatic is re-executed after a PopFrame call.
1709     // Detect such a case in the InterpreterRuntime function and return the member name argument, or null.
1710 
1711     __ get_method(rdx);
1712     __ movptr(rax, Address(local0, 0));
1713     __ call_VM(rax, CAST_FROM_FN_PTR(address, InterpreterRuntime::member_name_arg_or_null), rax, rdx, rbcp);
1714 
1715     __ testptr(rax, rax);
1716     __ jcc(Assembler::zero, L_done);
1717 
1718     __ movptr(Address(rbx, 0), rax);
1719     __ bind(L_done);
1720   }
1721 #endif // INCLUDE_JVMTI
1722 
1723   __ dispatch_next(vtos);
1724   // end of PopFrame support
1725 
1726   Interpreter::_remove_activation_entry = __ pc();
1727 
1728   // preserve exception over this code sequence
1729   __ pop_ptr(rax);
1730   NOT_LP64(__ get_thread(thread));
1731   __ movptr(Address(thread, JavaThread::vm_result_offset()), rax);
1732   // remove the activation (without doing throws on illegalMonitorExceptions)
1733   __ remove_activation(vtos, rdx, false, true, false);
1734   // restore exception
1735   NOT_LP64(__ get_thread(thread));
1736   __ get_vm_result(rax, thread);
1737 
1738   // In between activations - previous activation type unknown yet
1739   // compute continuation point - the continuation point expects the
1740   // following registers set up:
1741   //
1742   // rax: exception
1743   // rdx: return address/pc that threw exception
1744   // rsp: expression stack of caller
1745   // rbp: ebp of caller
1746   __ push(rax);                                  // save exception
1747   __ push(rdx);                                  // save return address
1748   __ super_call_VM_leaf(CAST_FROM_FN_PTR(address,
1749                           SharedRuntime::exception_handler_for_return_address),
1750                         thread, rdx);
1751   __ mov(rbx, rax);                              // save exception handler
1752   __ pop(rdx);                                   // restore return address
1753   __ pop(rax);                                   // restore exception
1754   // Note that an "issuing PC" is actually the next PC after the call
1755   __ jmp(rbx);                                   // jump to exception
1756                                                  // handler of caller
1757 }
1758 
1759 
1760 //
1761 // JVMTI ForceEarlyReturn support
1762 //
1763 address TemplateInterpreterGenerator::generate_earlyret_entry_for(TosState state) {
1764   address entry = __ pc();
1765 
1766   __ restore_bcp();
1767   __ restore_locals();
1768   __ empty_expression_stack();
1769   __ load_earlyret_value(state);  // 32 bits returns value in rdx, so don't reuse
1770 
1771   const Register thread = NOT_LP64(rcx) LP64_ONLY(r15_thread);
1772   NOT_LP64(__ get_thread(thread));
1773   __ movptr(rcx, Address(thread, JavaThread::jvmti_thread_state_offset()));
1774   Address cond_addr(rcx, JvmtiThreadState::earlyret_state_offset());
1775 
1776   // Clear the earlyret state
1777   __ movl(cond_addr, JvmtiThreadState::earlyret_inactive);
1778 
1779   __ remove_activation(state, rsi,
1780                        false, /* throw_monitor_exception */
1781                        false, /* install_monitor_exception */
1782                        true); /* notify_jvmdi */
1783   __ jmp(rsi);
1784 
1785   return entry;
1786 } // end of ForceEarlyReturn support
1787 
1788 
1789 //-----------------------------------------------------------------------------
1790 // Helper for vtos entry point generation
1791 
1792 void TemplateInterpreterGenerator::set_vtos_entry_points(Template* t,
1793                                                          address& bep,
1794                                                          address& cep,
1795                                                          address& sep,
1796                                                          address& aep,
1797                                                          address& iep,
1798                                                          address& lep,
1799                                                          address& fep,
1800                                                          address& dep,
1801                                                          address& vep) {
1802   assert(t->is_valid() && t->tos_in() == vtos, "illegal template");
1803   Label L;
1804 #ifndef _LP64
1805   fep = __ pc();     // ftos entry point
1806       __ push(ftos);
1807       __ jmpb(L);
1808   dep = __ pc();     // dtos entry point
1809       __ push(dtos);
1810       __ jmpb(L);
1811 #else
1812   fep = __ pc();     // ftos entry point
1813       __ push_f(xmm0);
1814       __ jmpb(L);
1815   dep = __ pc();     // dtos entry point
1816       __ push_d(xmm0);
1817       __ jmpb(L);
1818 #endif // _LP64
1819   lep = __ pc();     // ltos entry point
1820       __ push_l();
1821       __ jmpb(L);
1822   aep = bep = cep = sep = iep = __ pc();      // [abcsi]tos entry point
1823       __ push_i_or_ptr();
1824   vep = __ pc();    // vtos entry point
1825   __ bind(L);
1826   generate_and_dispatch(t);
1827 }
1828 
1829 //-----------------------------------------------------------------------------
1830 
1831 // Non-product code
1832 #ifndef PRODUCT
1833 
1834 address TemplateInterpreterGenerator::generate_trace_code(TosState state) {
1835   address entry = __ pc();
1836 
1837 #ifndef _LP64
1838   // prepare expression stack
1839   __ pop(rcx);          // pop return address so expression stack is 'pure'
1840   __ push(state);       // save tosca
1841 
1842   // pass tosca registers as arguments & call tracer
1843   __ call_VM(noreg, CAST_FROM_FN_PTR(address, InterpreterRuntime::trace_bytecode), rcx, rax, rdx);
1844   __ mov(rcx, rax);     // make sure return address is not destroyed by pop(state)
1845   __ pop(state);        // restore tosca
1846 
1847   // return
1848   __ jmp(rcx);
1849 #else
1850   __ push(state);
1851   __ push(c_rarg0);
1852   __ push(c_rarg1);
1853   __ push(c_rarg2);
1854   __ push(c_rarg3);
1855   __ mov(c_rarg2, rax);  // Pass itos
1856 #ifdef _WIN64
1857   __ movflt(xmm3, xmm0); // Pass ftos
1858 #endif
1859   __ call_VM(noreg,
1860              CAST_FROM_FN_PTR(address, InterpreterRuntime::trace_bytecode),
1861              c_rarg1, c_rarg2, c_rarg3);
1862   __ pop(c_rarg3);
1863   __ pop(c_rarg2);
1864   __ pop(c_rarg1);
1865   __ pop(c_rarg0);
1866   __ pop(state);
1867   __ ret(0);                                   // return from result handler
1868 #endif // _LP64
1869 
1870   return entry;
1871 }
1872 
1873 void TemplateInterpreterGenerator::count_bytecode() {
1874   __ incrementl(ExternalAddress((address) &BytecodeCounter::_counter_value), rscratch1);
1875 }
1876 
1877 void TemplateInterpreterGenerator::histogram_bytecode(Template* t) {
1878   __ incrementl(ExternalAddress((address) &BytecodeHistogram::_counters[t->bytecode()]), rscratch1);
1879 }
1880 
1881 void TemplateInterpreterGenerator::histogram_bytecode_pair(Template* t) {
1882   __ mov32(rbx, ExternalAddress((address) &BytecodePairHistogram::_index));
1883   __ shrl(rbx, BytecodePairHistogram::log2_number_of_codes);
1884   __ orl(rbx,
1885          ((int) t->bytecode()) <<
1886          BytecodePairHistogram::log2_number_of_codes);
1887   __ mov32(ExternalAddress((address) &BytecodePairHistogram::_index), rbx, rscratch1);
1888   __ lea(rscratch1, ExternalAddress((address) BytecodePairHistogram::_counters));
1889   __ incrementl(Address(rscratch1, rbx, Address::times_4));
1890 }
1891 
1892 
1893 void TemplateInterpreterGenerator::trace_bytecode(Template* t) {
1894   // Call a little run-time stub to avoid blow-up for each bytecode.
1895   // The run-time runtime saves the right registers, depending on
1896   // the tosca in-state for the given template.
1897 
1898   assert(Interpreter::trace_code(t->tos_in()) != nullptr,
1899          "entry must have been generated");
1900 #ifndef _LP64
1901   __ call(RuntimeAddress(Interpreter::trace_code(t->tos_in())));
1902 #else
1903   __ mov(r12, rsp); // remember sp (can only use r12 if not using call_VM)
1904   __ andptr(rsp, -16); // align stack as required by ABI
1905   __ call(RuntimeAddress(Interpreter::trace_code(t->tos_in())));
1906   __ mov(rsp, r12); // restore sp
1907   __ reinit_heapbase();
1908 #endif // _LP64
1909 }
1910 
1911 
1912 void TemplateInterpreterGenerator::stop_interpreter_at() {
1913   Label L;
1914   __ cmp32(ExternalAddress((address) &BytecodeCounter::_counter_value),
1915            StopInterpreterAt,
1916            rscratch1);
1917   __ jcc(Assembler::notEqual, L);
1918   __ int3();
1919   __ bind(L);
1920 }
1921 #endif // !PRODUCT