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