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