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   // Force this write out before the read below
1151   __ membar(Assembler::Membar_mask_bits(
1152               Assembler::LoadLoad | Assembler::LoadStore |
1153               Assembler::StoreLoad | Assembler::StoreStore));
1154 
1155 #ifndef _LP64
1156   if (AlwaysRestoreFPU) {
1157     //  Make sure the control word is correct.
1158     __ fldcw(ExternalAddress(StubRoutines::addr_fpu_cntrl_wrd_std()));
1159   }
1160 #endif // _LP64
1161 
1162   // check for safepoint operation in progress and/or pending suspend requests
1163   {
1164     Label Continue;
1165     Label slow_path;
1166 
1167 #ifndef _LP64
1168     __ safepoint_poll(slow_path, thread, noreg);
1169 #else
1170     __ safepoint_poll(slow_path, r15_thread, rscratch1);
1171 #endif
1172 
1173     __ cmpl(Address(thread, JavaThread::suspend_flags_offset()), 0);
1174     __ jcc(Assembler::equal, Continue);
1175     __ bind(slow_path);
1176 
1177     // Don't use call_VM as it will see a possible pending exception
1178     // and forward it and never return here preventing us from
1179     // clearing _last_native_pc down below.  Also can't use
1180     // call_VM_leaf either as it will check to see if r13 & r14 are
1181     // preserved and correspond to the bcp/locals pointers. So we do a
1182     // runtime call by hand.
1183     //
1184 #ifndef _LP64
1185     __ push(thread);
1186     __ call(RuntimeAddress(CAST_FROM_FN_PTR(address,
1187                                             JavaThread::check_special_condition_for_native_trans)));
1188     __ increment(rsp, wordSize);
1189     __ get_thread(thread);
1190 #else
1191     __ mov(c_rarg0, r15_thread);
1192     __ mov(r12, rsp); // remember sp (can only use r12 if not using call_VM)
1193     __ subptr(rsp, frame::arg_reg_save_area_bytes); // windows
1194     __ andptr(rsp, -16); // align stack as required by ABI
1195     __ call(RuntimeAddress(CAST_FROM_FN_PTR(address, JavaThread::check_special_condition_for_native_trans)));
1196     __ mov(rsp, r12); // restore sp
1197     __ reinit_heapbase();
1198 #endif // _LP64
1199     __ bind(Continue);
1200   }
1201 
1202   // change thread state
1203   __ movl(Address(thread, JavaThread::thread_state_offset()), _thread_in_Java);
1204 
1205   // reset_last_Java_frame
1206   __ reset_last_Java_frame(thread, true);
1207 
1208   if (CheckJNICalls) {
1209     // clear_pending_jni_exception_check
1210     __ movptr(Address(thread, JavaThread::pending_jni_exception_check_fn_offset()), NULL_WORD);
1211   }
1212 
1213   // reset handle block
1214   __ movptr(t, Address(thread, JavaThread::active_handles_offset()));
1215   __ movl(Address(t, JNIHandleBlock::top_offset_in_bytes()), (int32_t)NULL_WORD);
1216 
1217   // If result is an oop unbox and store it in frame where gc will see it
1218   // and result handler will pick it up
1219 
1220   {
1221     Label no_oop;
1222     __ lea(t, ExternalAddress(AbstractInterpreter::result_handler(T_OBJECT)));
1223     __ cmpptr(t, Address(rbp, frame::interpreter_frame_result_handler_offset*wordSize));
1224     __ jcc(Assembler::notEqual, no_oop);
1225     // retrieve result
1226     __ pop(ltos);
1227     // Unbox oop result, e.g. JNIHandles::resolve value.
1228     __ resolve_jobject(rax /* value */,
1229                        thread /* thread */,
1230                        t /* tmp */);
1231     __ movptr(Address(rbp, frame::interpreter_frame_oop_temp_offset*wordSize), rax);
1232     // keep stack depth as expected by pushing oop which will eventually be discarded
1233     __ push(ltos);
1234     __ bind(no_oop);
1235   }
1236 
1237 
1238   {
1239     Label no_reguard;
1240     __ cmpl(Address(thread, JavaThread::stack_guard_state_offset()),
1241             JavaThread::stack_guard_yellow_reserved_disabled);
1242     __ jcc(Assembler::notEqual, no_reguard);
1243 
1244     __ pusha(); // XXX only save smashed registers
1245 #ifndef _LP64
1246     __ call(RuntimeAddress(CAST_FROM_FN_PTR(address, SharedRuntime::reguard_yellow_pages)));
1247     __ popa();
1248 #else
1249     __ mov(r12, rsp); // remember sp (can only use r12 if not using call_VM)
1250     __ subptr(rsp, frame::arg_reg_save_area_bytes); // windows
1251     __ andptr(rsp, -16); // align stack as required by ABI
1252     __ call(RuntimeAddress(CAST_FROM_FN_PTR(address, SharedRuntime::reguard_yellow_pages)));
1253     __ mov(rsp, r12); // restore sp
1254     __ popa(); // XXX only restore smashed registers
1255     __ reinit_heapbase();
1256 #endif // _LP64
1257 
1258     __ bind(no_reguard);
1259   }
1260 
1261 
1262   // The method register is junk from after the thread_in_native transition
1263   // until here.  Also can't call_VM until the bcp has been
1264   // restored.  Need bcp for throwing exception below so get it now.
1265   __ get_method(method);
1266 
1267   // restore to have legal interpreter frame, i.e., bci == 0 <=> code_base()
1268   __ movptr(rbcp, Address(method, Method::const_offset()));   // get ConstMethod*
1269   __ lea(rbcp, Address(rbcp, ConstMethod::codes_offset()));    // get codebase
1270 
1271   // handle exceptions (exception handling will handle unlocking!)
1272   {
1273     Label L;
1274     __ cmpptr(Address(thread, Thread::pending_exception_offset()), (int32_t) NULL_WORD);
1275     __ jcc(Assembler::zero, L);
1276     // Note: At some point we may want to unify this with the code
1277     // used in call_VM_base(); i.e., we should use the
1278     // StubRoutines::forward_exception code. For now this doesn't work
1279     // here because the rsp is not correctly set at this point.
1280     __ MacroAssembler::call_VM(noreg,
1281                                CAST_FROM_FN_PTR(address,
1282                                InterpreterRuntime::throw_pending_exception));
1283     __ should_not_reach_here();
1284     __ bind(L);
1285   }
1286 
1287   // do unlocking if necessary
1288   {
1289     Label L;
1290     __ movl(t, Address(method, Method::access_flags_offset()));
1291     __ testl(t, JVM_ACC_SYNCHRONIZED);
1292     __ jcc(Assembler::zero, L);
1293     // the code below should be shared with interpreter macro
1294     // assembler implementation
1295     {
1296       Label unlock;
1297       // BasicObjectLock will be first in list, since this is a
1298       // synchronized method. However, need to check that the object
1299       // has not been unlocked by an explicit monitorexit bytecode.
1300       const Address monitor(rbp,
1301                             (intptr_t)(frame::interpreter_frame_initial_sp_offset *
1302                                        wordSize - (int)sizeof(BasicObjectLock)));
1303 
1304       const Register regmon = NOT_LP64(rdx) LP64_ONLY(c_rarg1);
1305 
1306       // monitor expect in c_rarg1 for slow unlock path
1307       __ lea(regmon, monitor); // address of first monitor
1308 
1309       __ movptr(t, Address(regmon, BasicObjectLock::obj_offset_in_bytes()));
1310       __ testptr(t, t);
1311       __ jcc(Assembler::notZero, unlock);
1312 
1313       // Entry already unlocked, need to throw exception
1314       __ MacroAssembler::call_VM(noreg,
1315                                  CAST_FROM_FN_PTR(address,
1316                    InterpreterRuntime::throw_illegal_monitor_state_exception));
1317       __ should_not_reach_here();
1318 
1319       __ bind(unlock);
1320       __ unlock_object(regmon);
1321     }
1322     __ bind(L);
1323   }
1324 
1325   // jvmti support
1326   // Note: This must happen _after_ handling/throwing any exceptions since
1327   //       the exception handler code notifies the runtime of method exits
1328   //       too. If this happens before, method entry/exit notifications are
1329   //       not properly paired (was bug - gri 11/22/99).
1330   __ notify_method_exit(vtos, InterpreterMacroAssembler::NotifyJVMTI);
1331 
1332   // restore potential result in edx:eax, call result handler to
1333   // restore potential result in ST0 & handle result
1334 
1335   __ pop(ltos);
1336   LP64_ONLY( __ pop(dtos));
1337 
1338   __ movptr(t, Address(rbp,
1339                        (frame::interpreter_frame_result_handler_offset) * wordSize));
1340   __ call(t);
1341 
1342   // remove activation
1343   __ movptr(t, Address(rbp,
1344                        frame::interpreter_frame_sender_sp_offset *
1345                        wordSize)); // get sender sp
1346   __ leave();                                // remove frame anchor
1347   __ pop(rdi);                               // get return address
1348   __ mov(rsp, t);                            // set sp to sender sp
1349   __ jmp(rdi);
1350 
1351   if (inc_counter) {
1352     // Handle overflow of counter and compile method
1353     __ bind(invocation_counter_overflow);
1354     generate_counter_overflow(continue_after_compile);
1355   }
1356 
1357   return entry_point;
1358 }
1359 
1360 // Abstract method entry
1361 // Attempt to execute abstract method. Throw exception
1362 address TemplateInterpreterGenerator::generate_abstract_entry(void) {
1363 
1364   address entry_point = __ pc();
1365 
1366   // abstract method entry
1367 
1368   //  pop return address, reset last_sp to NULL
1369   __ empty_expression_stack();
1370   __ restore_bcp();      // rsi must be correct for exception handler   (was destroyed)
1371   __ restore_locals();   // make sure locals pointer is correct as well (was destroyed)
1372 
1373   // throw exception
1374   __ call_VM(noreg, CAST_FROM_FN_PTR(address, InterpreterRuntime::throw_AbstractMethodErrorWithMethod), rbx);
1375   // the call_VM checks for exception, so we should never return here.
1376   __ should_not_reach_here();
1377 
1378   return entry_point;
1379 }
1380 
1381 //
1382 // Generic interpreted method entry to (asm) interpreter
1383 //
1384 address TemplateInterpreterGenerator::generate_normal_entry(bool synchronized) {
1385   // determine code generation flags
1386   bool inc_counter  = UseCompiler || CountCompiledCalls || LogTouchedMethods;
1387 
1388   // ebx: Method*
1389   // rbcp: sender sp
1390   address entry_point = __ pc();
1391 
1392   const Address constMethod(rbx, Method::const_offset());
1393   const Address access_flags(rbx, Method::access_flags_offset());
1394   const Address size_of_parameters(rdx,
1395                                    ConstMethod::size_of_parameters_offset());
1396   const Address size_of_locals(rdx, ConstMethod::size_of_locals_offset());
1397 
1398 
1399   // get parameter size (always needed)
1400   __ movptr(rdx, constMethod);
1401   __ load_unsigned_short(rcx, size_of_parameters);
1402 
1403   // rbx: Method*
1404   // rcx: size of parameters
1405   // rbcp: sender_sp (could differ from sp+wordSize if we were called via c2i )
1406 
1407   __ load_unsigned_short(rdx, size_of_locals); // get size of locals in words
1408   __ subl(rdx, rcx); // rdx = no. of additional locals
1409 
1410   // YYY
1411 //   __ incrementl(rdx);
1412 //   __ andl(rdx, -2);
1413 
1414   // see if we've got enough room on the stack for locals plus overhead.
1415   generate_stack_overflow_check();
1416 
1417   // get return address
1418   __ pop(rax);
1419 
1420   // compute beginning of parameters
1421   __ lea(rlocals, Address(rsp, rcx, Interpreter::stackElementScale(), -wordSize));
1422 
1423   // rdx - # of additional locals
1424   // allocate space for locals
1425   // explicitly initialize locals
1426   {
1427     Label exit, loop;
1428     __ testl(rdx, rdx);
1429     __ jcc(Assembler::lessEqual, exit); // do nothing if rdx <= 0
1430     __ bind(loop);
1431     __ push((int) NULL_WORD); // initialize local variables
1432     __ decrementl(rdx); // until everything initialized
1433     __ jcc(Assembler::greater, loop);
1434     __ bind(exit);
1435   }
1436 
1437   // initialize fixed part of activation frame
1438   generate_fixed_frame(false);
1439 
1440   // make sure method is not native & not abstract
1441 #ifdef ASSERT
1442   __ movl(rax, access_flags);
1443   {
1444     Label L;
1445     __ testl(rax, JVM_ACC_NATIVE);
1446     __ jcc(Assembler::zero, L);
1447     __ stop("tried to execute native method as non-native");
1448     __ bind(L);
1449   }
1450   {
1451     Label L;
1452     __ testl(rax, JVM_ACC_ABSTRACT);
1453     __ jcc(Assembler::zero, L);
1454     __ stop("tried to execute abstract method in interpreter");
1455     __ bind(L);
1456   }
1457 #endif
1458 
1459   // Since at this point in the method invocation the exception
1460   // handler would try to exit the monitor of synchronized methods
1461   // which hasn't been entered yet, we set the thread local variable
1462   // _do_not_unlock_if_synchronized to true. The remove_activation
1463   // will check this flag.
1464 
1465   const Register thread = NOT_LP64(rax) LP64_ONLY(r15_thread);
1466   NOT_LP64(__ get_thread(thread));
1467   const Address do_not_unlock_if_synchronized(thread,
1468         in_bytes(JavaThread::do_not_unlock_if_synchronized_offset()));
1469   __ movbool(do_not_unlock_if_synchronized, true);
1470 
1471   __ profile_parameters_type(rax, rcx, rdx);
1472   // increment invocation count & check for overflow
1473   Label invocation_counter_overflow;
1474   Label profile_method;
1475   Label profile_method_continue;
1476   if (inc_counter) {
1477     generate_counter_incr(&invocation_counter_overflow,
1478                           &profile_method,
1479                           &profile_method_continue);
1480     if (ProfileInterpreter) {
1481       __ bind(profile_method_continue);
1482     }
1483   }
1484 
1485   Label continue_after_compile;
1486   __ bind(continue_after_compile);
1487 
1488   // check for synchronized interpreted methods
1489   bang_stack_shadow_pages(false);
1490 
1491   // reset the _do_not_unlock_if_synchronized flag
1492   NOT_LP64(__ get_thread(thread));
1493   __ movbool(do_not_unlock_if_synchronized, false);
1494 
1495   // check for synchronized methods
1496   // Must happen AFTER invocation_counter check and stack overflow check,
1497   // so method is not locked if overflows.
1498   if (synchronized) {
1499     // Allocate monitor and lock method
1500     lock_method();
1501   } else {
1502     // no synchronization necessary
1503 #ifdef ASSERT
1504     {
1505       Label L;
1506       __ movl(rax, access_flags);
1507       __ testl(rax, JVM_ACC_SYNCHRONIZED);
1508       __ jcc(Assembler::zero, L);
1509       __ stop("method needs synchronization");
1510       __ bind(L);
1511     }
1512 #endif
1513   }
1514 
1515   // start execution
1516 #ifdef ASSERT
1517   {
1518     Label L;
1519      const Address monitor_block_top (rbp,
1520                  frame::interpreter_frame_monitor_block_top_offset * wordSize);
1521     __ movptr(rax, monitor_block_top);
1522     __ cmpptr(rax, rsp);
1523     __ jcc(Assembler::equal, L);
1524     __ stop("broken stack frame setup in interpreter");
1525     __ bind(L);
1526   }
1527 #endif
1528 
1529   // jvmti support
1530   __ notify_method_entry();
1531 
1532   __ dispatch_next(vtos);
1533 
1534   // invocation counter overflow
1535   if (inc_counter) {
1536     if (ProfileInterpreter) {
1537       // We have decided to profile this method in the interpreter
1538       __ bind(profile_method);
1539       __ call_VM(noreg, CAST_FROM_FN_PTR(address, InterpreterRuntime::profile_method));
1540       __ set_method_data_pointer_for_bcp();
1541       __ get_method(rbx);
1542       __ jmp(profile_method_continue);
1543     }
1544     // Handle overflow of counter and compile method
1545     __ bind(invocation_counter_overflow);
1546     generate_counter_overflow(continue_after_compile);
1547   }
1548 
1549   return entry_point;
1550 }
1551 
1552 //-----------------------------------------------------------------------------
1553 // Exceptions
1554 
1555 void TemplateInterpreterGenerator::generate_throw_exception() {
1556   // Entry point in previous activation (i.e., if the caller was
1557   // interpreted)
1558   Interpreter::_rethrow_exception_entry = __ pc();
1559   // Restore sp to interpreter_frame_last_sp even though we are going
1560   // to empty the expression stack for the exception processing.
1561   __ movptr(Address(rbp, frame::interpreter_frame_last_sp_offset * wordSize), (int32_t)NULL_WORD);
1562   // rax: exception
1563   // rdx: return address/pc that threw exception
1564   __ restore_bcp();    // r13/rsi points to call/send
1565   __ restore_locals();
1566   LP64_ONLY(__ reinit_heapbase());  // restore r12 as heapbase.
1567   // Entry point for exceptions thrown within interpreter code
1568   Interpreter::_throw_exception_entry = __ pc();
1569   // expression stack is undefined here
1570   // rax: exception
1571   // r13/rsi: exception bcp
1572   __ verify_oop(rax);
1573   Register rarg = NOT_LP64(rax) LP64_ONLY(c_rarg1);
1574   LP64_ONLY(__ mov(c_rarg1, rax));
1575 
1576   // expression stack must be empty before entering the VM in case of
1577   // an exception
1578   __ empty_expression_stack();
1579   // find exception handler address and preserve exception oop
1580   __ call_VM(rdx,
1581              CAST_FROM_FN_PTR(address,
1582                           InterpreterRuntime::exception_handler_for_exception),
1583              rarg);
1584   // rax: exception handler entry point
1585   // rdx: preserved exception oop
1586   // r13/rsi: bcp for exception handler
1587   __ push_ptr(rdx); // push exception which is now the only value on the stack
1588   __ jmp(rax); // jump to exception handler (may be _remove_activation_entry!)
1589 
1590   // If the exception is not handled in the current frame the frame is
1591   // removed and the exception is rethrown (i.e. exception
1592   // continuation is _rethrow_exception).
1593   //
1594   // Note: At this point the bci is still the bxi for the instruction
1595   // which caused the exception and the expression stack is
1596   // empty. Thus, for any VM calls at this point, GC will find a legal
1597   // oop map (with empty expression stack).
1598 
1599   // In current activation
1600   // tos: exception
1601   // esi: exception bcp
1602 
1603   //
1604   // JVMTI PopFrame support
1605   //
1606 
1607   Interpreter::_remove_activation_preserving_args_entry = __ pc();
1608   __ empty_expression_stack();
1609   // Set the popframe_processing bit in pending_popframe_condition
1610   // indicating that we are currently handling popframe, so that
1611   // call_VMs that may happen later do not trigger new popframe
1612   // handling cycles.
1613   const Register thread = NOT_LP64(rcx) LP64_ONLY(r15_thread);
1614   NOT_LP64(__ get_thread(thread));
1615   __ movl(rdx, Address(thread, JavaThread::popframe_condition_offset()));
1616   __ orl(rdx, JavaThread::popframe_processing_bit);
1617   __ movl(Address(thread, JavaThread::popframe_condition_offset()), rdx);
1618 
1619   {
1620     // Check to see whether we are returning to a deoptimized frame.
1621     // (The PopFrame call ensures that the caller of the popped frame is
1622     // either interpreted or compiled and deoptimizes it if compiled.)
1623     // In this case, we can't call dispatch_next() after the frame is
1624     // popped, but instead must save the incoming arguments and restore
1625     // them after deoptimization has occurred.
1626     //
1627     // Note that we don't compare the return PC against the
1628     // deoptimization blob's unpack entry because of the presence of
1629     // adapter frames in C2.
1630     Label caller_not_deoptimized;
1631     Register rarg = NOT_LP64(rdx) LP64_ONLY(c_rarg1);
1632     __ movptr(rarg, Address(rbp, frame::return_addr_offset * wordSize));
1633     __ super_call_VM_leaf(CAST_FROM_FN_PTR(address,
1634                                InterpreterRuntime::interpreter_contains), rarg);
1635     __ testl(rax, rax);
1636     __ jcc(Assembler::notZero, caller_not_deoptimized);
1637 
1638     // Compute size of arguments for saving when returning to
1639     // deoptimized caller
1640     __ get_method(rax);
1641     __ movptr(rax, Address(rax, Method::const_offset()));
1642     __ load_unsigned_short(rax, Address(rax, in_bytes(ConstMethod::
1643                                                 size_of_parameters_offset())));
1644     __ shll(rax, Interpreter::logStackElementSize);
1645     __ restore_locals();
1646     __ subptr(rlocals, rax);
1647     __ addptr(rlocals, wordSize);
1648     // Save these arguments
1649     NOT_LP64(__ get_thread(thread));
1650     __ super_call_VM_leaf(CAST_FROM_FN_PTR(address,
1651                                            Deoptimization::
1652                                            popframe_preserve_args),
1653                           thread, rax, rlocals);
1654 
1655     __ remove_activation(vtos, rdx,
1656                          /* throw_monitor_exception */ false,
1657                          /* install_monitor_exception */ false,
1658                          /* notify_jvmdi */ false);
1659 
1660     // Inform deoptimization that it is responsible for restoring
1661     // these arguments
1662     NOT_LP64(__ get_thread(thread));
1663     __ movl(Address(thread, JavaThread::popframe_condition_offset()),
1664             JavaThread::popframe_force_deopt_reexecution_bit);
1665 
1666     // Continue in deoptimization handler
1667     __ jmp(rdx);
1668 
1669     __ bind(caller_not_deoptimized);
1670   }
1671 
1672   __ remove_activation(vtos, rdx, /* rdx result (retaddr) is not used */
1673                        /* throw_monitor_exception */ false,
1674                        /* install_monitor_exception */ false,
1675                        /* notify_jvmdi */ false);
1676 
1677   // Finish with popframe handling
1678   // A previous I2C followed by a deoptimization might have moved the
1679   // outgoing arguments further up the stack. PopFrame expects the
1680   // mutations to those outgoing arguments to be preserved and other
1681   // constraints basically require this frame to look exactly as
1682   // though it had previously invoked an interpreted activation with
1683   // no space between the top of the expression stack (current
1684   // last_sp) and the top of stack. Rather than force deopt to
1685   // maintain this kind of invariant all the time we call a small
1686   // fixup routine to move the mutated arguments onto the top of our
1687   // expression stack if necessary.
1688 #ifndef _LP64
1689   __ mov(rax, rsp);
1690   __ movptr(rbx, Address(rbp, frame::interpreter_frame_last_sp_offset * wordSize));
1691   __ get_thread(thread);
1692   // PC must point into interpreter here
1693   __ set_last_Java_frame(thread, noreg, rbp, __ pc());
1694   __ super_call_VM_leaf(CAST_FROM_FN_PTR(address, InterpreterRuntime::popframe_move_outgoing_args), thread, rax, rbx);
1695   __ get_thread(thread);
1696 #else
1697   __ mov(c_rarg1, rsp);
1698   __ movptr(c_rarg2, Address(rbp, frame::interpreter_frame_last_sp_offset * wordSize));
1699   // PC must point into interpreter here
1700   __ set_last_Java_frame(noreg, rbp, __ pc());
1701   __ super_call_VM_leaf(CAST_FROM_FN_PTR(address, InterpreterRuntime::popframe_move_outgoing_args), r15_thread, c_rarg1, c_rarg2);
1702 #endif
1703   __ reset_last_Java_frame(thread, true);
1704 
1705   // Restore the last_sp and null it out
1706   __ movptr(rsp, Address(rbp, frame::interpreter_frame_last_sp_offset * wordSize));
1707   __ movptr(Address(rbp, frame::interpreter_frame_last_sp_offset * wordSize), (int32_t)NULL_WORD);
1708 
1709   __ restore_bcp();
1710   __ restore_locals();
1711   // The method data pointer was incremented already during
1712   // call profiling. We have to restore the mdp for the current bcp.
1713   if (ProfileInterpreter) {
1714     __ set_method_data_pointer_for_bcp();
1715   }
1716 
1717   // Clear the popframe condition flag
1718   NOT_LP64(__ get_thread(thread));
1719   __ movl(Address(thread, JavaThread::popframe_condition_offset()),
1720           JavaThread::popframe_inactive);
1721 
1722 #if INCLUDE_JVMTI
1723   {
1724     Label L_done;
1725     const Register local0 = rlocals;
1726 
1727     __ cmpb(Address(rbcp, 0), Bytecodes::_invokestatic);
1728     __ jcc(Assembler::notEqual, L_done);
1729 
1730     // The member name argument must be restored if _invokestatic is re-executed after a PopFrame call.
1731     // Detect such a case in the InterpreterRuntime function and return the member name argument, or NULL.
1732 
1733     __ get_method(rdx);
1734     __ movptr(rax, Address(local0, 0));
1735     __ call_VM(rax, CAST_FROM_FN_PTR(address, InterpreterRuntime::member_name_arg_or_null), rax, rdx, rbcp);
1736 
1737     __ testptr(rax, rax);
1738     __ jcc(Assembler::zero, L_done);
1739 
1740     __ movptr(Address(rbx, 0), rax);
1741     __ bind(L_done);
1742   }
1743 #endif // INCLUDE_JVMTI
1744 
1745   __ dispatch_next(vtos);
1746   // end of PopFrame support
1747 
1748   Interpreter::_remove_activation_entry = __ pc();
1749 
1750   // preserve exception over this code sequence
1751   __ pop_ptr(rax);
1752   NOT_LP64(__ get_thread(thread));
1753   __ movptr(Address(thread, JavaThread::vm_result_offset()), rax);
1754   // remove the activation (without doing throws on illegalMonitorExceptions)
1755   __ remove_activation(vtos, rdx, false, true, false);
1756   // restore exception
1757   NOT_LP64(__ get_thread(thread));
1758   __ get_vm_result(rax, thread);
1759 
1760   // In between activations - previous activation type unknown yet
1761   // compute continuation point - the continuation point expects the
1762   // following registers set up:
1763   //
1764   // rax: exception
1765   // rdx: return address/pc that threw exception
1766   // rsp: expression stack of caller
1767   // rbp: ebp of caller
1768   __ push(rax);                                  // save exception
1769   __ push(rdx);                                  // save return address
1770   __ super_call_VM_leaf(CAST_FROM_FN_PTR(address,
1771                           SharedRuntime::exception_handler_for_return_address),
1772                         thread, rdx);
1773   __ mov(rbx, rax);                              // save exception handler
1774   __ pop(rdx);                                   // restore return address
1775   __ pop(rax);                                   // restore exception
1776   // Note that an "issuing PC" is actually the next PC after the call
1777   __ jmp(rbx);                                   // jump to exception
1778                                                  // handler of caller
1779 }
1780 
1781 
1782 //
1783 // JVMTI ForceEarlyReturn support
1784 //
1785 address TemplateInterpreterGenerator::generate_earlyret_entry_for(TosState state) {
1786   address entry = __ pc();
1787 
1788   __ restore_bcp();
1789   __ restore_locals();
1790   __ empty_expression_stack();
1791   __ load_earlyret_value(state);  // 32 bits returns value in rdx, so don't reuse
1792 
1793   const Register thread = NOT_LP64(rcx) LP64_ONLY(r15_thread);
1794   NOT_LP64(__ get_thread(thread));
1795   __ movptr(rcx, Address(thread, JavaThread::jvmti_thread_state_offset()));
1796   Address cond_addr(rcx, JvmtiThreadState::earlyret_state_offset());
1797 
1798   // Clear the earlyret state
1799   __ movl(cond_addr, JvmtiThreadState::earlyret_inactive);
1800 
1801   __ remove_activation(state, rsi,
1802                        false, /* throw_monitor_exception */
1803                        false, /* install_monitor_exception */
1804                        true); /* notify_jvmdi */
1805   __ jmp(rsi);
1806 
1807   return entry;
1808 } // end of ForceEarlyReturn support
1809 
1810 
1811 //-----------------------------------------------------------------------------
1812 // Helper for vtos entry point generation
1813 
1814 void TemplateInterpreterGenerator::set_vtos_entry_points(Template* t,
1815                                                          address& bep,
1816                                                          address& cep,
1817                                                          address& sep,
1818                                                          address& aep,
1819                                                          address& iep,
1820                                                          address& lep,
1821                                                          address& fep,
1822                                                          address& dep,
1823                                                          address& vep) {
1824   assert(t->is_valid() && t->tos_in() == vtos, "illegal template");
1825   Label L;
1826   aep = __ pc();     // atos entry point
1827       __ push_ptr();
1828       __ jmp(L);
1829 #ifndef _LP64
1830   fep = __ pc();     // ftos entry point
1831       __ push(ftos);
1832       __ jmp(L);
1833   dep = __ pc();     // dtos entry point
1834       __ push(dtos);
1835       __ jmp(L);
1836 #else
1837   fep = __ pc();     // ftos entry point
1838       __ push_f(xmm0);
1839       __ jmp(L);
1840   dep = __ pc();     // dtos entry point
1841       __ push_d(xmm0);
1842       __ jmp(L);
1843 #endif // _LP64
1844   lep = __ pc();     // ltos entry point
1845       __ push_l();
1846       __ jmp(L);
1847   bep = cep = sep = iep = __ pc();      // [bcsi]tos entry point
1848       __ push_i();
1849   vep = __ pc();    // vtos entry point
1850   __ bind(L);
1851   generate_and_dispatch(t);
1852 }
1853 
1854 //-----------------------------------------------------------------------------
1855 
1856 // Non-product code
1857 #ifndef PRODUCT
1858 
1859 address TemplateInterpreterGenerator::generate_trace_code(TosState state) {
1860   address entry = __ pc();
1861 
1862 #ifndef _LP64
1863   // prepare expression stack
1864   __ pop(rcx);          // pop return address so expression stack is 'pure'
1865   __ push(state);       // save tosca
1866 
1867   // pass tosca registers as arguments & call tracer
1868   __ call_VM(noreg, CAST_FROM_FN_PTR(address, InterpreterRuntime::trace_bytecode), rcx, rax, rdx);
1869   __ mov(rcx, rax);     // make sure return address is not destroyed by pop(state)
1870   __ pop(state);        // restore tosca
1871 
1872   // return
1873   __ jmp(rcx);
1874 #else
1875   __ push(state);
1876   __ push(c_rarg0);
1877   __ push(c_rarg1);
1878   __ push(c_rarg2);
1879   __ push(c_rarg3);
1880   __ mov(c_rarg2, rax);  // Pass itos
1881 #ifdef _WIN64
1882   __ movflt(xmm3, xmm0); // Pass ftos
1883 #endif
1884   __ call_VM(noreg,
1885              CAST_FROM_FN_PTR(address, InterpreterRuntime::trace_bytecode),
1886              c_rarg1, c_rarg2, c_rarg3);
1887   __ pop(c_rarg3);
1888   __ pop(c_rarg2);
1889   __ pop(c_rarg1);
1890   __ pop(c_rarg0);
1891   __ pop(state);
1892   __ ret(0);                                   // return from result handler
1893 #endif // _LP64
1894 
1895   return entry;
1896 }
1897 
1898 void TemplateInterpreterGenerator::count_bytecode() {
1899   __ incrementl(ExternalAddress((address) &BytecodeCounter::_counter_value));
1900 }
1901 
1902 void TemplateInterpreterGenerator::histogram_bytecode(Template* t) {
1903   __ incrementl(ExternalAddress((address) &BytecodeHistogram::_counters[t->bytecode()]));
1904 }
1905 
1906 void TemplateInterpreterGenerator::histogram_bytecode_pair(Template* t) {
1907   __ mov32(rbx, ExternalAddress((address) &BytecodePairHistogram::_index));
1908   __ shrl(rbx, BytecodePairHistogram::log2_number_of_codes);
1909   __ orl(rbx,
1910          ((int) t->bytecode()) <<
1911          BytecodePairHistogram::log2_number_of_codes);
1912   __ mov32(ExternalAddress((address) &BytecodePairHistogram::_index), rbx);
1913   __ lea(rscratch1, ExternalAddress((address) BytecodePairHistogram::_counters));
1914   __ incrementl(Address(rscratch1, rbx, Address::times_4));
1915 }
1916 
1917 
1918 void TemplateInterpreterGenerator::trace_bytecode(Template* t) {
1919   // Call a little run-time stub to avoid blow-up for each bytecode.
1920   // The run-time runtime saves the right registers, depending on
1921   // the tosca in-state for the given template.
1922 
1923   assert(Interpreter::trace_code(t->tos_in()) != NULL,
1924          "entry must have been generated");
1925 #ifndef _LP64
1926   __ call(RuntimeAddress(Interpreter::trace_code(t->tos_in())));
1927 #else
1928   __ mov(r12, rsp); // remember sp (can only use r12 if not using call_VM)
1929   __ andptr(rsp, -16); // align stack as required by ABI
1930   __ call(RuntimeAddress(Interpreter::trace_code(t->tos_in())));
1931   __ mov(rsp, r12); // restore sp
1932   __ reinit_heapbase();
1933 #endif // _LP64
1934 }
1935 
1936 
1937 void TemplateInterpreterGenerator::stop_interpreter_at() {
1938   Label L;
1939   __ cmp32(ExternalAddress((address) &BytecodeCounter::_counter_value),
1940            StopInterpreterAt);
1941   __ jcc(Assembler::notEqual, L);
1942   __ int3();
1943   __ bind(L);
1944 }
1945 #endif // !PRODUCT