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