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