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