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