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