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