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