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