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