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