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