1 /*
   2  * Copyright (c) 1997, 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 "compiler/compiler_globals.hpp"
  26 #include "interp_masm_x86.hpp"
  27 #include "interpreter/interpreter.hpp"
  28 #include "interpreter/interpreterRuntime.hpp"
  29 #include "logging/log.hpp"
  30 #include "oops/arrayOop.hpp"
  31 #include "oops/markWord.hpp"
  32 #include "oops/methodData.hpp"
  33 #include "oops/method.hpp"
  34 #include "oops/resolvedFieldEntry.hpp"
  35 #include "oops/resolvedIndyEntry.hpp"
  36 #include "oops/resolvedMethodEntry.hpp"
  37 #include "prims/jvmtiExport.hpp"
  38 #include "prims/jvmtiThreadState.hpp"
  39 #include "runtime/basicLock.hpp"
  40 #include "runtime/frame.inline.hpp"
  41 #include "runtime/javaThread.hpp"
  42 #include "runtime/runtimeUpcalls.hpp"
  43 #include "runtime/safepointMechanism.hpp"
  44 #include "runtime/sharedRuntime.hpp"
  45 #include "utilities/powerOfTwo.hpp"
  46 
  47 // Implementation of InterpreterMacroAssembler
  48 
  49 void InterpreterMacroAssembler::jump_to_entry(address entry) {
  50   assert(entry, "Entry must have been generated by now");
  51   jump(RuntimeAddress(entry));
  52 }
  53 
  54 void InterpreterMacroAssembler::profile_obj_type(Register obj, const Address& mdo_addr) {
  55   Label update, next, none;
  56 
  57   assert_different_registers(obj, rscratch1, mdo_addr.base(), mdo_addr.index());
  58 
  59   interp_verify_oop(obj, atos);
  60 
  61   testptr(obj, obj);
  62   jccb(Assembler::notZero, update);
  63   testptr(mdo_addr, TypeEntries::null_seen);
  64   jccb(Assembler::notZero, next); // null already seen. Nothing to do anymore.
  65   // atomic update to prevent overwriting Klass* with 0
  66   lock();
  67   orptr(mdo_addr, TypeEntries::null_seen);
  68   jmpb(next);
  69 
  70   bind(update);
  71   load_klass(obj, obj, rscratch1);
  72   mov(rscratch1, obj);
  73 
  74   xorptr(obj, mdo_addr);
  75   testptr(obj, TypeEntries::type_klass_mask);
  76   jccb(Assembler::zero, next); // klass seen before, nothing to
  77                                // do. The unknown bit may have been
  78                                // set already but no need to check.
  79 
  80   testptr(obj, TypeEntries::type_unknown);
  81   jccb(Assembler::notZero, next); // already unknown. Nothing to do anymore.
  82 
  83   cmpptr(mdo_addr, 0);
  84   jccb(Assembler::equal, none);
  85   cmpptr(mdo_addr, TypeEntries::null_seen);
  86   jccb(Assembler::equal, none);
  87 
  88   // There is a chance that the checks above (re-reading profiling
  89   // data from memory) fail if another thread has just set the
  90   // profiling to this obj's klass
  91   mov(obj, rscratch1);
  92   xorptr(obj, mdo_addr);
  93   testptr(obj, TypeEntries::type_klass_mask);
  94   jccb(Assembler::zero, next);
  95 
  96   // different than before. Cannot keep accurate profile.
  97   orptr(mdo_addr, TypeEntries::type_unknown);
  98   jmpb(next);
  99 
 100   bind(none);
 101   // first time here. Set profile type.
 102   movptr(mdo_addr, obj);
 103 #ifdef ASSERT
 104   andptr(obj, TypeEntries::type_klass_mask);
 105   verify_klass_ptr(obj);
 106 #endif
 107 
 108   bind(next);
 109 }
 110 
 111 void InterpreterMacroAssembler::profile_arguments_type(Register mdp, Register callee, Register tmp, bool is_virtual) {
 112   if (!ProfileInterpreter) {
 113     return;
 114   }
 115 
 116   if (MethodData::profile_arguments() || MethodData::profile_return()) {
 117     Label profile_continue;
 118 
 119     test_method_data_pointer(mdp, profile_continue);
 120 
 121     int off_to_start = is_virtual ? in_bytes(VirtualCallData::virtual_call_data_size()) : in_bytes(CounterData::counter_data_size());
 122 
 123     cmpb(Address(mdp, in_bytes(DataLayout::tag_offset()) - off_to_start), is_virtual ? DataLayout::virtual_call_type_data_tag : DataLayout::call_type_data_tag);
 124     jcc(Assembler::notEqual, profile_continue);
 125 
 126     if (MethodData::profile_arguments()) {
 127       Label done;
 128       int off_to_args = in_bytes(TypeEntriesAtCall::args_data_offset());
 129       addptr(mdp, off_to_args);
 130 
 131       for (int i = 0; i < TypeProfileArgsLimit; i++) {
 132         if (i > 0 || MethodData::profile_return()) {
 133           // If return value type is profiled we may have no argument to profile
 134           movptr(tmp, Address(mdp, in_bytes(TypeEntriesAtCall::cell_count_offset())-off_to_args));
 135           subl(tmp, i*TypeStackSlotEntries::per_arg_count());
 136           cmpl(tmp, TypeStackSlotEntries::per_arg_count());
 137           jcc(Assembler::less, done);
 138         }
 139         movptr(tmp, Address(callee, Method::const_offset()));
 140         load_unsigned_short(tmp, Address(tmp, ConstMethod::size_of_parameters_offset()));
 141         // stack offset o (zero based) from the start of the argument
 142         // list, for n arguments translates into offset n - o - 1 from
 143         // the end of the argument list
 144         subptr(tmp, Address(mdp, in_bytes(TypeEntriesAtCall::stack_slot_offset(i))-off_to_args));
 145         subl(tmp, 1);
 146         Address arg_addr = argument_address(tmp);
 147         movptr(tmp, arg_addr);
 148 
 149         Address mdo_arg_addr(mdp, in_bytes(TypeEntriesAtCall::argument_type_offset(i))-off_to_args);
 150         profile_obj_type(tmp, mdo_arg_addr);
 151 
 152         int to_add = in_bytes(TypeStackSlotEntries::per_arg_size());
 153         addptr(mdp, to_add);
 154         off_to_args += to_add;
 155       }
 156 
 157       if (MethodData::profile_return()) {
 158         movptr(tmp, Address(mdp, in_bytes(TypeEntriesAtCall::cell_count_offset())-off_to_args));
 159         subl(tmp, TypeProfileArgsLimit*TypeStackSlotEntries::per_arg_count());
 160       }
 161 
 162       bind(done);
 163 
 164       if (MethodData::profile_return()) {
 165         // We're right after the type profile for the last
 166         // argument. tmp is the number of cells left in the
 167         // CallTypeData/VirtualCallTypeData to reach its end. Non null
 168         // if there's a return to profile.
 169         assert(ReturnTypeEntry::static_cell_count() < TypeStackSlotEntries::per_arg_count(), "can't move past ret type");
 170         shll(tmp, log2i_exact((int)DataLayout::cell_size));
 171         addptr(mdp, tmp);
 172       }
 173       movptr(Address(rbp, frame::interpreter_frame_mdp_offset * wordSize), mdp);
 174     } else {
 175       assert(MethodData::profile_return(), "either profile call args or call ret");
 176       update_mdp_by_constant(mdp, in_bytes(TypeEntriesAtCall::return_only_size()));
 177     }
 178 
 179     // mdp points right after the end of the
 180     // CallTypeData/VirtualCallTypeData, right after the cells for the
 181     // return value type if there's one
 182 
 183     bind(profile_continue);
 184   }
 185 }
 186 
 187 void InterpreterMacroAssembler::profile_return_type(Register mdp, Register ret, Register tmp) {
 188   assert_different_registers(mdp, ret, tmp, _bcp_register);
 189   if (ProfileInterpreter && MethodData::profile_return()) {
 190     Label profile_continue;
 191 
 192     test_method_data_pointer(mdp, profile_continue);
 193 
 194     if (MethodData::profile_return_jsr292_only()) {
 195       assert(Method::intrinsic_id_size_in_bytes() == 2, "assuming Method::_intrinsic_id is u2");
 196 
 197       // If we don't profile all invoke bytecodes we must make sure
 198       // it's a bytecode we indeed profile. We can't go back to the
 199       // beginning of the ProfileData we intend to update to check its
 200       // type because we're right after it and we don't known its
 201       // length
 202       Label do_profile;
 203       cmpb(Address(_bcp_register, 0), Bytecodes::_invokedynamic);
 204       jcc(Assembler::equal, do_profile);
 205       cmpb(Address(_bcp_register, 0), Bytecodes::_invokehandle);
 206       jcc(Assembler::equal, do_profile);
 207       get_method(tmp);
 208       cmpw(Address(tmp, Method::intrinsic_id_offset()), static_cast<int>(vmIntrinsics::_compiledLambdaForm));
 209       jcc(Assembler::notEqual, profile_continue);
 210 
 211       bind(do_profile);
 212     }
 213 
 214     Address mdo_ret_addr(mdp, -in_bytes(ReturnTypeEntry::size()));
 215     mov(tmp, ret);
 216     profile_obj_type(tmp, mdo_ret_addr);
 217 
 218     bind(profile_continue);
 219   }
 220 }
 221 
 222 void InterpreterMacroAssembler::profile_parameters_type(Register mdp, Register tmp1, Register tmp2) {
 223   if (ProfileInterpreter && MethodData::profile_parameters()) {
 224     Label profile_continue;
 225 
 226     test_method_data_pointer(mdp, profile_continue);
 227 
 228     // Load the offset of the area within the MDO used for
 229     // parameters. If it's negative we're not profiling any parameters
 230     movl(tmp1, Address(mdp, in_bytes(MethodData::parameters_type_data_di_offset()) - in_bytes(MethodData::data_offset())));
 231     testl(tmp1, tmp1);
 232     jcc(Assembler::negative, profile_continue);
 233 
 234     // Compute a pointer to the area for parameters from the offset
 235     // and move the pointer to the slot for the last
 236     // parameters. Collect profiling from last parameter down.
 237     // mdo start + parameters offset + array length - 1
 238     addptr(mdp, tmp1);
 239     movptr(tmp1, Address(mdp, ArrayData::array_len_offset()));
 240     decrement(tmp1, TypeStackSlotEntries::per_arg_count());
 241 
 242     Label loop;
 243     bind(loop);
 244 
 245     int off_base = in_bytes(ParametersTypeData::stack_slot_offset(0));
 246     int type_base = in_bytes(ParametersTypeData::type_offset(0));
 247     Address::ScaleFactor per_arg_scale = Address::times(DataLayout::cell_size);
 248     Address arg_off(mdp, tmp1, per_arg_scale, off_base);
 249     Address arg_type(mdp, tmp1, per_arg_scale, type_base);
 250 
 251     // load offset on the stack from the slot for this parameter
 252     movptr(tmp2, arg_off);
 253     negptr(tmp2);
 254     // read the parameter from the local area
 255     movptr(tmp2, Address(_locals_register, tmp2, Interpreter::stackElementScale()));
 256 
 257     // profile the parameter
 258     profile_obj_type(tmp2, arg_type);
 259 
 260     // go to next parameter
 261     decrement(tmp1, TypeStackSlotEntries::per_arg_count());
 262     jcc(Assembler::positive, loop);
 263 
 264     bind(profile_continue);
 265   }
 266 }
 267 
 268 void InterpreterMacroAssembler::call_VM_leaf_base(address entry_point,
 269                                                   int number_of_arguments) {
 270   // interpreter specific
 271   //
 272   // Note: No need to save/restore bcp & locals registers
 273   //       since these are callee saved registers and no blocking/
 274   //       GC can happen in leaf calls.
 275   // Further Note: DO NOT save/restore bcp/locals. If a caller has
 276   // already saved them so that it can use rsi/rdi as temporaries
 277   // then a save/restore here will DESTROY the copy the caller
 278   // saved! There used to be a save_bcp() that only happened in
 279   // the ASSERT path (no restore_bcp). Which caused bizarre failures
 280   // when jvm built with ASSERTs.
 281 #ifdef ASSERT
 282   {
 283     Label L;
 284     cmpptr(Address(rbp, frame::interpreter_frame_last_sp_offset * wordSize), NULL_WORD);
 285     jcc(Assembler::equal, L);
 286     stop("InterpreterMacroAssembler::call_VM_leaf_base:"
 287          " last_sp != null");
 288     bind(L);
 289   }
 290 #endif
 291   // super call
 292   MacroAssembler::call_VM_leaf_base(entry_point, number_of_arguments);
 293   // interpreter specific
 294   // LP64: Used to ASSERT that r13/r14 were equal to frame's bcp/locals
 295   // but since they may not have been saved (and we don't want to
 296   // save them here (see note above) the assert is invalid.
 297 }
 298 
 299 void InterpreterMacroAssembler::call_VM_base(Register oop_result,
 300                                              Register last_java_sp,
 301                                              address  entry_point,
 302                                              int      number_of_arguments,
 303                                              bool     check_exceptions) {
 304   // interpreter specific
 305   //
 306   // Note: Could avoid restoring locals ptr (callee saved) - however doesn't
 307   //       really make a difference for these runtime calls, since they are
 308   //       slow anyway. Btw., bcp must be saved/restored since it may change
 309   //       due to GC.
 310   save_bcp();
 311 #ifdef ASSERT
 312   {
 313     Label L;
 314     cmpptr(Address(rbp, frame::interpreter_frame_last_sp_offset * wordSize), NULL_WORD);
 315     jcc(Assembler::equal, L);
 316     stop("InterpreterMacroAssembler::call_VM_base:"
 317          " last_sp isn't null");
 318     bind(L);
 319   }
 320 #endif /* ASSERT */
 321   // super call
 322   MacroAssembler::call_VM_base(oop_result, last_java_sp,
 323                                entry_point, number_of_arguments,
 324                                check_exceptions);
 325   // interpreter specific
 326   restore_bcp();
 327   restore_locals();
 328 }
 329 
 330 void InterpreterMacroAssembler::call_VM_preemptable_helper(Register oop_result,
 331                                                            address entry_point,
 332                                                            int number_of_arguments,
 333                                                            bool check_exceptions) {
 334   assert(InterpreterRuntime::is_preemptable_call(entry_point), "VM call not preemptable, should use call_VM()");
 335   Label resume_pc, not_preempted;
 336 
 337 #ifdef ASSERT
 338   {
 339     Label L1, L2;
 340     cmpptr(Address(r15_thread, JavaThread::preempt_alternate_return_offset()), NULL_WORD);
 341     jcc(Assembler::equal, L1);
 342     stop("call_VM_preemptable_helper: should not have alternate return address set");
 343     bind(L1);
 344     // We check this counter in patch_return_pc_with_preempt_stub() during freeze.
 345     incrementl(Address(r15_thread, JavaThread::interp_at_preemptable_vmcall_cnt_offset()));
 346     cmpl(Address(r15_thread, JavaThread::interp_at_preemptable_vmcall_cnt_offset()), 0);
 347     jcc(Assembler::greater, L2);
 348     stop("call_VM_preemptable_helper: should be > 0");
 349     bind(L2);
 350   }
 351 #endif /* ASSERT */
 352 
 353   // Force freeze slow path.
 354   push_cont_fastpath();
 355 
 356   // Make VM call. In case of preemption set last_pc to the one we want to resume to.
 357   lea(rscratch1, resume_pc);
 358   push(rscratch1);
 359   lea(rax, Address(rsp, wordSize));
 360   call_VM_base(noreg, rax, entry_point, number_of_arguments, false);
 361   pop(rscratch1);
 362 
 363   pop_cont_fastpath();
 364 
 365 #ifdef ASSERT
 366   {
 367     Label L;
 368     decrementl(Address(r15_thread, JavaThread::interp_at_preemptable_vmcall_cnt_offset()));
 369     cmpl(Address(r15_thread, JavaThread::interp_at_preemptable_vmcall_cnt_offset()), 0);
 370     jcc(Assembler::greaterEqual, L);
 371     stop("call_VM_preemptable_helper: should be >= 0");
 372     bind(L);
 373   }
 374 #endif /* ASSERT */
 375 
 376   // Check if preempted.
 377   movptr(rscratch1, Address(r15_thread, JavaThread::preempt_alternate_return_offset()));
 378   cmpptr(rscratch1, NULL_WORD);
 379   jccb(Assembler::zero, not_preempted);
 380   movptr(Address(r15_thread, JavaThread::preempt_alternate_return_offset()), NULL_WORD);
 381   jmp(rscratch1);
 382 
 383   // In case of preemption, this is where we will resume once we finally acquire the monitor.
 384   bind(resume_pc);
 385   restore_after_resume(false /* is_native */);
 386 
 387   bind(not_preempted);
 388   if (check_exceptions) {
 389     // check for pending exceptions
 390     cmpptr(Address(r15_thread, Thread::pending_exception_offset()), NULL_WORD);
 391     Label ok;
 392     jcc(Assembler::equal, ok);
 393     // Exception stub expects return pc to be at top of stack. We only need
 394     // it to check Interpreter::contains(return_address) so anything will do.
 395     lea(rscratch1, resume_pc);
 396     push(rscratch1);
 397     jump(RuntimeAddress(StubRoutines::forward_exception_entry()));
 398     bind(ok);
 399   }
 400 
 401   // get oop result if there is one and reset the value in the thread
 402   if (oop_result->is_valid()) {
 403     get_vm_result_oop(oop_result);
 404   }
 405 }
 406 
 407 static void pass_arg1(MacroAssembler* masm, Register arg) {
 408   if (c_rarg1 != arg ) {
 409     masm->mov(c_rarg1, arg);
 410   }
 411 }
 412 
 413 static void pass_arg2(MacroAssembler* masm, Register arg) {
 414   if (c_rarg2 != arg ) {
 415     masm->mov(c_rarg2, arg);
 416   }
 417 }
 418 
 419 void InterpreterMacroAssembler::call_VM_preemptable(Register oop_result,
 420                                          address entry_point,
 421                                          Register arg_1,
 422                                          bool check_exceptions) {
 423   pass_arg1(this, arg_1);
 424   call_VM_preemptable_helper(oop_result, entry_point, 1, check_exceptions);
 425 }
 426 
 427 void InterpreterMacroAssembler::call_VM_preemptable(Register oop_result,
 428                                          address entry_point,
 429                                          Register arg_1,
 430                                          Register arg_2,
 431                                          bool check_exceptions) {
 432   LP64_ONLY(assert_different_registers(arg_1, c_rarg2));
 433   pass_arg2(this, arg_2);
 434   pass_arg1(this, arg_1);
 435   call_VM_preemptable_helper(oop_result, entry_point, 2, check_exceptions);
 436 }
 437 
 438 void InterpreterMacroAssembler::restore_after_resume(bool is_native) {
 439   lea(rscratch1, ExternalAddress(Interpreter::cont_resume_interpreter_adapter()));
 440   call(rscratch1);
 441   if (is_native) {
 442     // On resume we need to set up stack as expected.
 443     push(dtos);
 444     push(ltos);
 445   }
 446 }
 447 
 448 void InterpreterMacroAssembler::check_and_handle_popframe() {
 449   if (JvmtiExport::can_pop_frame()) {
 450     Label L;
 451     // Initiate popframe handling only if it is not already being
 452     // processed.  If the flag has the popframe_processing bit set, it
 453     // means that this code is called *during* popframe handling - we
 454     // don't want to reenter.
 455     // This method is only called just after the call into the vm in
 456     // call_VM_base, so the arg registers are available.
 457     Register pop_cond = c_rarg0;
 458     movl(pop_cond, Address(r15_thread, JavaThread::popframe_condition_offset()));
 459     testl(pop_cond, JavaThread::popframe_pending_bit);
 460     jcc(Assembler::zero, L);
 461     testl(pop_cond, JavaThread::popframe_processing_bit);
 462     jcc(Assembler::notZero, L);
 463     // Call Interpreter::remove_activation_preserving_args_entry() to get the
 464     // address of the same-named entrypoint in the generated interpreter code.
 465     call_VM_leaf(CAST_FROM_FN_PTR(address, Interpreter::remove_activation_preserving_args_entry));
 466     jmp(rax);
 467     bind(L);
 468   }
 469 }
 470 
 471 void InterpreterMacroAssembler::load_earlyret_value(TosState state) {
 472   movptr(rcx, Address(r15_thread, JavaThread::jvmti_thread_state_offset()));
 473   const Address tos_addr(rcx, JvmtiThreadState::earlyret_tos_offset());
 474   const Address oop_addr(rcx, JvmtiThreadState::earlyret_oop_offset());
 475   const Address val_addr(rcx, JvmtiThreadState::earlyret_value_offset());
 476 
 477   switch (state) {
 478     case atos: movptr(rax, oop_addr);
 479                movptr(oop_addr, NULL_WORD);
 480                interp_verify_oop(rax, state);         break;
 481     case ltos: movptr(rax, val_addr);                 break;
 482     case btos:                                   // fall through
 483     case ztos:                                   // fall through
 484     case ctos:                                   // fall through
 485     case stos:                                   // fall through
 486     case itos: movl(rax, val_addr);                 break;
 487     case ftos: movflt(xmm0, val_addr);              break;
 488     case dtos: movdbl(xmm0, val_addr);              break;
 489     case vtos: /* nothing to do */                  break;
 490     default  : ShouldNotReachHere();
 491   }
 492 
 493   // Clean up tos value in the thread object
 494   movl(tos_addr, ilgl);
 495   movptr(val_addr, NULL_WORD);
 496 }
 497 
 498 
 499 void InterpreterMacroAssembler::check_and_handle_earlyret() {
 500   if (JvmtiExport::can_force_early_return()) {
 501     Label L;
 502     Register tmp = c_rarg0;
 503     Register rthread = r15_thread;
 504 
 505     movptr(tmp, Address(rthread, JavaThread::jvmti_thread_state_offset()));
 506     testptr(tmp, tmp);
 507     jcc(Assembler::zero, L); // if (thread->jvmti_thread_state() == nullptr) exit;
 508 
 509     // Initiate earlyret handling only if it is not already being processed.
 510     // If the flag has the earlyret_processing bit set, it means that this code
 511     // is called *during* earlyret handling - we don't want to reenter.
 512     movl(tmp, Address(tmp, JvmtiThreadState::earlyret_state_offset()));
 513     cmpl(tmp, JvmtiThreadState::earlyret_pending);
 514     jcc(Assembler::notEqual, L);
 515 
 516     // Call Interpreter::remove_activation_early_entry() to get the address of the
 517     // same-named entrypoint in the generated interpreter code.
 518     movptr(tmp, Address(rthread, JavaThread::jvmti_thread_state_offset()));
 519     movl(tmp, Address(tmp, JvmtiThreadState::earlyret_tos_offset()));
 520     call_VM_leaf(CAST_FROM_FN_PTR(address, Interpreter::remove_activation_early_entry), tmp);
 521     jmp(rax);
 522     bind(L);
 523   }
 524 }
 525 
 526 void InterpreterMacroAssembler::get_unsigned_2_byte_index_at_bcp(Register reg, int bcp_offset) {
 527   assert(bcp_offset >= 0, "bcp is still pointing to start of bytecode");
 528   load_unsigned_short(reg, Address(_bcp_register, bcp_offset));
 529   bswapl(reg);
 530   shrl(reg, 16);
 531 }
 532 
 533 void InterpreterMacroAssembler::get_cache_index_at_bcp(Register index,
 534                                                        int bcp_offset,
 535                                                        size_t index_size) {
 536   assert(bcp_offset > 0, "bcp is still pointing to start of bytecode");
 537   if (index_size == sizeof(u2)) {
 538     load_unsigned_short(index, Address(_bcp_register, bcp_offset));
 539   } else if (index_size == sizeof(u4)) {
 540     movl(index, Address(_bcp_register, bcp_offset));
 541   } else if (index_size == sizeof(u1)) {
 542     load_unsigned_byte(index, Address(_bcp_register, bcp_offset));
 543   } else {
 544     ShouldNotReachHere();
 545   }
 546 }
 547 
 548 // Load object from cpool->resolved_references(index)
 549 void InterpreterMacroAssembler::load_resolved_reference_at_index(Register result,
 550                                                                  Register index,
 551                                                                  Register tmp) {
 552   assert_different_registers(result, index);
 553 
 554   get_constant_pool(result);
 555   // load pointer for resolved_references[] objArray
 556   movptr(result, Address(result, ConstantPool::cache_offset()));
 557   movptr(result, Address(result, ConstantPoolCache::resolved_references_offset()));
 558   resolve_oop_handle(result, tmp);
 559   load_heap_oop(result, Address(result, index,
 560                                 UseCompressedOops ? Address::times_4 : Address::times_ptr,
 561                                 arrayOopDesc::base_offset_in_bytes(T_OBJECT)), tmp);
 562 }
 563 
 564 // load cpool->resolved_klass_at(index)
 565 void InterpreterMacroAssembler::load_resolved_klass_at_index(Register klass,
 566                                                              Register cpool,
 567                                                              Register index) {
 568   assert_different_registers(cpool, index);
 569 
 570   movw(index, Address(cpool, index, Address::times_ptr, sizeof(ConstantPool)));
 571   Register resolved_klasses = cpool;
 572   movptr(resolved_klasses, Address(cpool, ConstantPool::resolved_klasses_offset()));
 573   movptr(klass, Address(resolved_klasses, index, Address::times_ptr, Array<Klass*>::base_offset_in_bytes()));
 574 }
 575 
 576 // Generate a subtype check: branch to ok_is_subtype if sub_klass is a
 577 // subtype of super_klass.
 578 //
 579 // Args:
 580 //      rax: superklass
 581 //      Rsub_klass: subklass
 582 //
 583 // Kills:
 584 //      rcx, rdi
 585 void InterpreterMacroAssembler::gen_subtype_check(Register Rsub_klass,
 586                                                   Label& ok_is_subtype) {
 587   assert(Rsub_klass != rax, "rax holds superklass");
 588   assert(Rsub_klass != r14, "r14 holds locals");
 589   assert(Rsub_klass != r13, "r13 holds bcp");
 590   assert(Rsub_klass != rcx, "rcx holds 2ndary super array length");
 591   assert(Rsub_klass != rdi, "rdi holds 2ndary super array scan ptr");
 592 
 593   // Profile the not-null value's klass.
 594   profile_typecheck(rcx, Rsub_klass, rdi); // blows rcx, reloads rdi
 595 
 596   // Do the check.
 597   check_klass_subtype(Rsub_klass, rax, rcx, ok_is_subtype); // blows rcx
 598 }
 599 
 600 
 601 // Java Expression Stack
 602 
 603 void InterpreterMacroAssembler::pop_ptr(Register r) {
 604   pop(r);
 605 }
 606 
 607 void InterpreterMacroAssembler::push_ptr(Register r) {
 608   push(r);
 609 }
 610 
 611 void InterpreterMacroAssembler::push_i(Register r) {
 612   push(r);
 613 }
 614 
 615 void InterpreterMacroAssembler::push_i_or_ptr(Register r) {
 616   push(r);
 617 }
 618 
 619 void InterpreterMacroAssembler::push_f(XMMRegister r) {
 620   subptr(rsp, wordSize);
 621   movflt(Address(rsp, 0), r);
 622 }
 623 
 624 void InterpreterMacroAssembler::pop_f(XMMRegister r) {
 625   movflt(r, Address(rsp, 0));
 626   addptr(rsp, wordSize);
 627 }
 628 
 629 void InterpreterMacroAssembler::push_d(XMMRegister r) {
 630   subptr(rsp, 2 * wordSize);
 631   movdbl(Address(rsp, 0), r);
 632 }
 633 
 634 void InterpreterMacroAssembler::pop_d(XMMRegister r) {
 635   movdbl(r, Address(rsp, 0));
 636   addptr(rsp, 2 * Interpreter::stackElementSize);
 637 }
 638 
 639 void InterpreterMacroAssembler::pop_i(Register r) {
 640   // XXX can't use pop currently, upper half non clean
 641   movl(r, Address(rsp, 0));
 642   addptr(rsp, wordSize);
 643 }
 644 
 645 void InterpreterMacroAssembler::pop_l(Register r) {
 646   movq(r, Address(rsp, 0));
 647   addptr(rsp, 2 * Interpreter::stackElementSize);
 648 }
 649 
 650 void InterpreterMacroAssembler::push_l(Register r) {
 651   subptr(rsp, 2 * wordSize);
 652   movptr(Address(rsp, Interpreter::expr_offset_in_bytes(0)), r         );
 653   movptr(Address(rsp, Interpreter::expr_offset_in_bytes(1)), NULL_WORD );
 654 }
 655 
 656 void InterpreterMacroAssembler::pop(TosState state) {
 657   switch (state) {
 658   case atos: pop_ptr();                 break;
 659   case btos:
 660   case ztos:
 661   case ctos:
 662   case stos:
 663   case itos: pop_i();                   break;
 664   case ltos: pop_l();                   break;
 665   case ftos: pop_f(xmm0);               break;
 666   case dtos: pop_d(xmm0);               break;
 667   case vtos: /* nothing to do */        break;
 668   default:   ShouldNotReachHere();
 669   }
 670   interp_verify_oop(rax, state);
 671 }
 672 
 673 void InterpreterMacroAssembler::push(TosState state) {
 674   interp_verify_oop(rax, state);
 675   switch (state) {
 676   case atos: push_ptr();                break;
 677   case btos:
 678   case ztos:
 679   case ctos:
 680   case stos:
 681   case itos: push_i();                  break;
 682   case ltos: push_l();                  break;
 683   case ftos: push_f(xmm0);              break;
 684   case dtos: push_d(xmm0);              break;
 685   case vtos: /* nothing to do */        break;
 686   default  : ShouldNotReachHere();
 687   }
 688 }
 689 
 690 // Helpers for swap and dup
 691 void InterpreterMacroAssembler::load_ptr(int n, Register val) {
 692   movptr(val, Address(rsp, Interpreter::expr_offset_in_bytes(n)));
 693 }
 694 
 695 void InterpreterMacroAssembler::store_ptr(int n, Register val) {
 696   movptr(Address(rsp, Interpreter::expr_offset_in_bytes(n)), val);
 697 }
 698 
 699 
 700 void InterpreterMacroAssembler::prepare_to_jump_from_interpreted() {
 701   // set sender sp
 702   lea(_bcp_register, Address(rsp, wordSize));
 703   // record last_sp
 704   mov(rcx, _bcp_register);
 705   subptr(rcx, rbp);
 706   sarptr(rcx, LogBytesPerWord);
 707   movptr(Address(rbp, frame::interpreter_frame_last_sp_offset * wordSize), rcx);
 708 }
 709 
 710 
 711 // Jump to from_interpreted entry of a call unless single stepping is possible
 712 // in this thread in which case we must call the i2i entry
 713 void InterpreterMacroAssembler::jump_from_interpreted(Register method, Register temp) {
 714   prepare_to_jump_from_interpreted();
 715 
 716   if (JvmtiExport::can_post_interpreter_events()) {
 717     Label run_compiled_code;
 718     // JVMTI events, such as single-stepping, are implemented partly by avoiding running
 719     // compiled code in threads for which the event is enabled.  Check here for
 720     // interp_only_mode if these events CAN be enabled.
 721     // interp_only is an int, on little endian it is sufficient to test the byte only
 722     // Is a cmpl faster?
 723     cmpb(Address(r15_thread, JavaThread::interp_only_mode_offset()), 0);
 724     jccb(Assembler::zero, run_compiled_code);
 725     jmp(Address(method, Method::interpreter_entry_offset()));
 726     bind(run_compiled_code);
 727   }
 728 
 729   jmp(Address(method, Method::from_interpreted_offset()));
 730 }
 731 
 732 // The following two routines provide a hook so that an implementation
 733 // can schedule the dispatch in two parts.  x86 does not do this.
 734 void InterpreterMacroAssembler::dispatch_prolog(TosState state, int step) {
 735   // Nothing x86 specific to be done here
 736 }
 737 
 738 void InterpreterMacroAssembler::dispatch_epilog(TosState state, int step) {
 739   dispatch_next(state, step);
 740 }
 741 
 742 void InterpreterMacroAssembler::dispatch_base(TosState state,
 743                                               address* table,
 744                                               bool verifyoop,
 745                                               bool generate_poll) {
 746   if (VerifyActivationFrameSize) {
 747     Label L;
 748     mov(rcx, rbp);
 749     subptr(rcx, rsp);
 750     int32_t min_frame_size =
 751       (frame::link_offset - frame::interpreter_frame_initial_sp_offset) *
 752       wordSize;
 753     cmpptr(rcx, min_frame_size);
 754     jcc(Assembler::greaterEqual, L);
 755     stop("broken stack frame");
 756     bind(L);
 757   }
 758   if (verifyoop) {
 759     interp_verify_oop(rax, state);
 760   }
 761 
 762   address* const safepoint_table = Interpreter::safept_table(state);
 763   Label no_safepoint, dispatch;
 764   if (table != safepoint_table && generate_poll) {
 765     NOT_PRODUCT(block_comment("Thread-local Safepoint poll"));
 766     testb(Address(r15_thread, JavaThread::polling_word_offset()), SafepointMechanism::poll_bit());
 767 
 768     jccb(Assembler::zero, no_safepoint);
 769     lea(rscratch1, ExternalAddress((address)safepoint_table));
 770     jmpb(dispatch);
 771   }
 772 
 773   bind(no_safepoint);
 774   lea(rscratch1, ExternalAddress((address)table));
 775   bind(dispatch);
 776   jmp(Address(rscratch1, rbx, Address::times_8));
 777 }
 778 
 779 void InterpreterMacroAssembler::dispatch_only(TosState state, bool generate_poll) {
 780   dispatch_base(state, Interpreter::dispatch_table(state), true, generate_poll);
 781 }
 782 
 783 void InterpreterMacroAssembler::dispatch_only_normal(TosState state) {
 784   dispatch_base(state, Interpreter::normal_table(state));
 785 }
 786 
 787 void InterpreterMacroAssembler::dispatch_only_noverify(TosState state) {
 788   dispatch_base(state, Interpreter::normal_table(state), false);
 789 }
 790 
 791 
 792 void InterpreterMacroAssembler::dispatch_next(TosState state, int step, bool generate_poll) {
 793   // load next bytecode (load before advancing _bcp_register to prevent AGI)
 794   load_unsigned_byte(rbx, Address(_bcp_register, step));
 795   // advance _bcp_register
 796   increment(_bcp_register, step);
 797   dispatch_base(state, Interpreter::dispatch_table(state), true, generate_poll);
 798 }
 799 
 800 void InterpreterMacroAssembler::dispatch_via(TosState state, address* table) {
 801   // load current bytecode
 802   load_unsigned_byte(rbx, Address(_bcp_register, 0));
 803   dispatch_base(state, table);
 804 }
 805 
 806 void InterpreterMacroAssembler::narrow(Register result) {
 807 
 808   // Get method->_constMethod->_result_type
 809   movptr(rcx, Address(rbp, frame::interpreter_frame_method_offset * wordSize));
 810   movptr(rcx, Address(rcx, Method::const_offset()));
 811   load_unsigned_byte(rcx, Address(rcx, ConstMethod::result_type_offset()));
 812 
 813   Label done, notBool, notByte, notChar;
 814 
 815   // common case first
 816   cmpl(rcx, T_INT);
 817   jcc(Assembler::equal, done);
 818 
 819   // mask integer result to narrower return type.
 820   cmpl(rcx, T_BOOLEAN);
 821   jcc(Assembler::notEqual, notBool);
 822   andl(result, 0x1);
 823   jmp(done);
 824 
 825   bind(notBool);
 826   cmpl(rcx, T_BYTE);
 827   jcc(Assembler::notEqual, notByte);
 828   movsbl(result, result);
 829   jmp(done);
 830 
 831   bind(notByte);
 832   cmpl(rcx, T_CHAR);
 833   jcc(Assembler::notEqual, notChar);
 834   movzwl(result, result);
 835   jmp(done);
 836 
 837   bind(notChar);
 838   // cmpl(rcx, T_SHORT);  // all that's left
 839   // jcc(Assembler::notEqual, done);
 840   movswl(result, result);
 841 
 842   // Nothing to do for T_INT
 843   bind(done);
 844 }
 845 
 846 // remove activation
 847 //
 848 // Unlock the receiver if this is a synchronized method.
 849 // Unlock any Java monitors from synchronized blocks.
 850 // Apply stack watermark barrier.
 851 // Notify JVMTI.
 852 // Remove the activation from the stack.
 853 //
 854 // If there are locked Java monitors
 855 //    If throw_monitor_exception
 856 //       throws IllegalMonitorStateException
 857 //    Else if install_monitor_exception
 858 //       installs IllegalMonitorStateException
 859 //    Else
 860 //       no error processing
 861 void InterpreterMacroAssembler::remove_activation(TosState state,
 862                                                   Register ret_addr,
 863                                                   bool throw_monitor_exception,
 864                                                   bool install_monitor_exception,
 865                                                   bool notify_jvmdi) {
 866   // Note: Registers rdx xmm0 may be in use for the
 867   // result check if synchronized method
 868   Label unlocked, unlock, no_unlock;
 869 
 870 #ifdef ASSERT
 871   Label not_preempted;
 872   cmpptr(Address(r15_thread, JavaThread::preempt_alternate_return_offset()), NULL_WORD);
 873   jcc(Assembler::equal, not_preempted);
 874   stop("remove_activation: should not have alternate return address set");
 875   bind(not_preempted);
 876 #endif /* ASSERT */
 877 
 878   const Register rthread = r15_thread;
 879   const Register robj    = c_rarg1;
 880   const Register rmon    = c_rarg1;
 881 
 882   // get the value of _do_not_unlock_if_synchronized into rdx
 883   const Address do_not_unlock_if_synchronized(rthread,
 884     in_bytes(JavaThread::do_not_unlock_if_synchronized_offset()));
 885   movbool(rbx, do_not_unlock_if_synchronized);
 886   movbool(do_not_unlock_if_synchronized, false); // reset the flag
 887 
 888  // get method access flags
 889   movptr(rcx, Address(rbp, frame::interpreter_frame_method_offset * wordSize));
 890   load_unsigned_short(rcx, Address(rcx, Method::access_flags_offset()));
 891   testl(rcx, JVM_ACC_SYNCHRONIZED);
 892   jcc(Assembler::zero, unlocked);
 893 
 894   // Don't unlock anything if the _do_not_unlock_if_synchronized flag
 895   // is set.
 896   testbool(rbx);
 897   jcc(Assembler::notZero, no_unlock);
 898 
 899   // unlock monitor
 900   push(state); // save result
 901 
 902   // BasicObjectLock will be first in list, since this is a
 903   // synchronized method. However, need to check that the object has
 904   // not been unlocked by an explicit monitorexit bytecode.
 905   const Address monitor(rbp, frame::interpreter_frame_initial_sp_offset *
 906                         wordSize - (int) sizeof(BasicObjectLock));
 907   // We use c_rarg1/rdx so that if we go slow path it will be the correct
 908   // register for unlock_object to pass to VM directly
 909   lea(robj, monitor); // address of first monitor
 910 
 911   movptr(rax, Address(robj, BasicObjectLock::obj_offset()));
 912   testptr(rax, rax);
 913   jcc(Assembler::notZero, unlock);
 914 
 915   pop(state);
 916   if (throw_monitor_exception) {
 917     // Entry already unlocked, need to throw exception
 918     call_VM(noreg, CAST_FROM_FN_PTR(address,
 919                    InterpreterRuntime::throw_illegal_monitor_state_exception));
 920     should_not_reach_here();
 921   } else {
 922     // Monitor already unlocked during a stack unroll. If requested,
 923     // install an illegal_monitor_state_exception.  Continue with
 924     // stack unrolling.
 925     if (install_monitor_exception) {
 926       call_VM(noreg, CAST_FROM_FN_PTR(address,
 927                      InterpreterRuntime::new_illegal_monitor_state_exception));
 928     }
 929     jmp(unlocked);
 930   }
 931 
 932   bind(unlock);
 933   unlock_object(robj);
 934   pop(state);
 935 
 936   // Check that for block-structured locking (i.e., that all locked
 937   // objects has been unlocked)
 938   bind(unlocked);
 939 
 940   // rax, rdx: Might contain return value
 941 
 942   // Check that all monitors are unlocked
 943   {
 944     Label loop, exception, entry, restart;
 945     const int entry_size = frame::interpreter_frame_monitor_size_in_bytes();
 946     const Address monitor_block_top(
 947         rbp, frame::interpreter_frame_monitor_block_top_offset * wordSize);
 948     const Address monitor_block_bot(
 949         rbp, frame::interpreter_frame_initial_sp_offset * wordSize);
 950 
 951     bind(restart);
 952     // We use c_rarg1 so that if we go slow path it will be the correct
 953     // register for unlock_object to pass to VM directly
 954     movptr(rmon, monitor_block_top); // derelativize pointer
 955     lea(rmon, Address(rbp, rmon, Address::times_ptr));
 956     // c_rarg1 points to current entry, starting with top-most entry
 957 
 958     lea(rbx, monitor_block_bot);  // points to word before bottom of
 959                                   // monitor block
 960     jmp(entry);
 961 
 962     // Entry already locked, need to throw exception
 963     bind(exception);
 964 
 965     if (throw_monitor_exception) {
 966       // Throw exception
 967       MacroAssembler::call_VM(noreg,
 968                               CAST_FROM_FN_PTR(address, InterpreterRuntime::
 969                                    throw_illegal_monitor_state_exception));
 970       should_not_reach_here();
 971     } else {
 972       // Stack unrolling. Unlock object and install illegal_monitor_exception.
 973       // Unlock does not block, so don't have to worry about the frame.
 974       // We don't have to preserve c_rarg1 since we are going to throw an exception.
 975 
 976       push(state);
 977       mov(robj, rmon);   // nop if robj and rmon are the same
 978       unlock_object(robj);
 979       pop(state);
 980 
 981       if (install_monitor_exception) {
 982         call_VM(noreg, CAST_FROM_FN_PTR(address,
 983                                         InterpreterRuntime::
 984                                         new_illegal_monitor_state_exception));
 985       }
 986 
 987       jmp(restart);
 988     }
 989 
 990     bind(loop);
 991     // check if current entry is used
 992     cmpptr(Address(rmon, BasicObjectLock::obj_offset()), NULL_WORD);
 993     jcc(Assembler::notEqual, exception);
 994 
 995     addptr(rmon, entry_size); // otherwise advance to next entry
 996     bind(entry);
 997     cmpptr(rmon, rbx); // check if bottom reached
 998     jcc(Assembler::notEqual, loop); // if not at bottom then check this entry
 999   }
1000 
1001   bind(no_unlock);
1002 
1003   JFR_ONLY(enter_jfr_critical_section();)
1004 
1005   // The below poll is for the stack watermark barrier. It allows fixing up frames lazily,
1006   // that would normally not be safe to use. Such bad returns into unsafe territory of
1007   // the stack, will call InterpreterRuntime::at_unwind.
1008   Label slow_path;
1009   Label fast_path;
1010   safepoint_poll(slow_path, true /* at_return */, false /* in_nmethod */);
1011   jmp(fast_path);
1012   bind(slow_path);
1013   push(state);
1014   set_last_Java_frame(noreg, rbp, (address)pc(), rscratch1);
1015   super_call_VM_leaf(CAST_FROM_FN_PTR(address, InterpreterRuntime::at_unwind), r15_thread);
1016   reset_last_Java_frame(true);
1017   pop(state);
1018   bind(fast_path);
1019 
1020   // JVMTI support. Make sure the safepoint poll test is issued prior.
1021   if (notify_jvmdi) {
1022     notify_method_exit(state, NotifyJVMTI);    // preserve TOSCA
1023   } else {
1024     notify_method_exit(state, SkipNotifyJVMTI); // preserve TOSCA
1025   }
1026 
1027   // remove activation
1028   // get sender sp
1029   movptr(rbx,
1030          Address(rbp, frame::interpreter_frame_sender_sp_offset * wordSize));
1031   if (StackReservedPages > 0) {
1032     // testing if reserved zone needs to be re-enabled
1033     Register rthread = r15_thread;
1034     Label no_reserved_zone_enabling;
1035 
1036     // check if already enabled - if so no re-enabling needed
1037     assert(sizeof(StackOverflow::StackGuardState) == 4, "unexpected size");
1038     cmpl(Address(rthread, JavaThread::stack_guard_state_offset()), StackOverflow::stack_guard_enabled);
1039     jcc(Assembler::equal, no_reserved_zone_enabling);
1040 
1041     cmpptr(rbx, Address(rthread, JavaThread::reserved_stack_activation_offset()));
1042     jcc(Assembler::lessEqual, no_reserved_zone_enabling);
1043 
1044     JFR_ONLY(leave_jfr_critical_section();)
1045 
1046     call_VM_leaf(
1047       CAST_FROM_FN_PTR(address, SharedRuntime::enable_stack_reserved_zone), rthread);
1048     call_VM(noreg, CAST_FROM_FN_PTR(address,
1049                    InterpreterRuntime::throw_delayed_StackOverflowError));
1050     should_not_reach_here();
1051 
1052     bind(no_reserved_zone_enabling);
1053   }
1054 
1055   leave();                           // remove frame anchor
1056 
1057   JFR_ONLY(leave_jfr_critical_section();)
1058 
1059   pop(ret_addr);                     // get return address
1060   mov(rsp, rbx);                     // set sp to sender sp
1061   pop_cont_fastpath();
1062 
1063 }
1064 
1065 #if INCLUDE_JFR
1066 void InterpreterMacroAssembler::enter_jfr_critical_section() {
1067   const Address sampling_critical_section(r15_thread, in_bytes(SAMPLING_CRITICAL_SECTION_OFFSET_JFR));
1068   movbool(sampling_critical_section, true);
1069 }
1070 
1071 void InterpreterMacroAssembler::leave_jfr_critical_section() {
1072   const Address sampling_critical_section(r15_thread, in_bytes(SAMPLING_CRITICAL_SECTION_OFFSET_JFR));
1073   movbool(sampling_critical_section, false);
1074 }
1075 #endif // INCLUDE_JFR
1076 
1077 void InterpreterMacroAssembler::get_method_counters(Register method,
1078                                                     Register mcs, Label& skip) {
1079   Label has_counters;
1080   movptr(mcs, Address(method, Method::method_counters_offset()));
1081   testptr(mcs, mcs);
1082   jcc(Assembler::notZero, has_counters);
1083   call_VM(noreg, CAST_FROM_FN_PTR(address,
1084           InterpreterRuntime::build_method_counters), method);
1085   movptr(mcs, Address(method,Method::method_counters_offset()));
1086   testptr(mcs, mcs);
1087   jcc(Assembler::zero, skip); // No MethodCounters allocated, OutOfMemory
1088   bind(has_counters);
1089 }
1090 
1091 
1092 // Lock object
1093 //
1094 // Args:
1095 //      rdx, c_rarg1: BasicObjectLock to be used for locking
1096 //
1097 // Kills:
1098 //      rax, rbx
1099 void InterpreterMacroAssembler::lock_object(Register lock_reg) {
1100   assert(lock_reg == c_rarg1, "The argument is only for looks. It must be c_rarg1");
1101 
1102   Label done, slow_case;
1103 
1104   const Register swap_reg = rax; // Must use rax for cmpxchg instruction
1105   const Register tmp_reg = rbx;
1106   const Register obj_reg = c_rarg3; // Will contain the oop
1107 
1108   // Load object pointer into obj_reg
1109   movptr(obj_reg, Address(lock_reg, BasicObjectLock::obj_offset()));
1110 
1111   fast_lock(lock_reg, obj_reg, swap_reg, tmp_reg, slow_case);
1112   jmp(done);
1113 
1114   bind(slow_case);
1115 
1116   // Call the runtime routine for slow case
1117   call_VM_preemptable(noreg,
1118           CAST_FROM_FN_PTR(address, InterpreterRuntime::monitorenter),
1119           lock_reg);
1120   bind(done);
1121 }
1122 
1123 
1124 // Unlocks an object. Used in monitorexit bytecode and
1125 // remove_activation.  Throws an IllegalMonitorException if object is
1126 // not locked by current thread.
1127 //
1128 // Args:
1129 //      rdx, c_rarg1: BasicObjectLock for lock
1130 //
1131 // Kills:
1132 //      rax
1133 //      c_rarg0, c_rarg1, c_rarg2, c_rarg3, ... (param regs)
1134 //      rscratch1 (scratch reg)
1135 // rax, rbx, rcx, rdx
1136 void InterpreterMacroAssembler::unlock_object(Register lock_reg) {
1137   assert(lock_reg == c_rarg1, "The argument is only for looks. It must be c_rarg1");
1138 
1139   Label done, slow_case;
1140 
1141   const Register swap_reg   = rax;  // Must use rax for cmpxchg instruction
1142   const Register header_reg = c_rarg2;  // Will contain the old oopMark
1143   const Register obj_reg    = c_rarg3;  // Will contain the oop
1144 
1145   save_bcp(); // Save in case of exception
1146 
1147   // Load oop into obj_reg(%c_rarg3)
1148   movptr(obj_reg, Address(lock_reg, BasicObjectLock::obj_offset()));
1149 
1150   // Free entry
1151   movptr(Address(lock_reg, BasicObjectLock::obj_offset()), NULL_WORD);
1152 
1153   fast_unlock(obj_reg, swap_reg, header_reg, slow_case);
1154   jmp(done);
1155 
1156   bind(slow_case);
1157   // Call the runtime routine for slow case.
1158   movptr(Address(lock_reg, BasicObjectLock::obj_offset()), obj_reg); // restore obj
1159   call_VM_leaf(CAST_FROM_FN_PTR(address, InterpreterRuntime::monitorexit), lock_reg);
1160 
1161   bind(done);
1162 
1163   restore_bcp();
1164 }
1165 
1166 void InterpreterMacroAssembler::test_method_data_pointer(Register mdp,
1167                                                          Label& zero_continue) {
1168   assert(ProfileInterpreter, "must be profiling interpreter");
1169   movptr(mdp, Address(rbp, frame::interpreter_frame_mdp_offset * wordSize));
1170   testptr(mdp, mdp);
1171   jcc(Assembler::zero, zero_continue);
1172 }
1173 
1174 
1175 // Set the method data pointer for the current bcp.
1176 void InterpreterMacroAssembler::set_method_data_pointer_for_bcp() {
1177   assert(ProfileInterpreter, "must be profiling interpreter");
1178   Label set_mdp;
1179   push(rax);
1180   push(rbx);
1181 
1182   get_method(rbx);
1183   // Test MDO to avoid the call if it is null.
1184   movptr(rax, Address(rbx, in_bytes(Method::method_data_offset())));
1185   testptr(rax, rax);
1186   jcc(Assembler::zero, set_mdp);
1187   // rbx: method
1188   // _bcp_register: bcp
1189   call_VM_leaf(CAST_FROM_FN_PTR(address, InterpreterRuntime::bcp_to_di), rbx, _bcp_register);
1190   // rax: mdi
1191   // mdo is guaranteed to be non-zero here, we checked for it before the call.
1192   movptr(rbx, Address(rbx, in_bytes(Method::method_data_offset())));
1193   addptr(rbx, in_bytes(MethodData::data_offset()));
1194   addptr(rax, rbx);
1195   bind(set_mdp);
1196   movptr(Address(rbp, frame::interpreter_frame_mdp_offset * wordSize), rax);
1197   pop(rbx);
1198   pop(rax);
1199 }
1200 
1201 void InterpreterMacroAssembler::verify_method_data_pointer() {
1202   assert(ProfileInterpreter, "must be profiling interpreter");
1203 #ifdef ASSERT
1204   Label verify_continue;
1205   push(rax);
1206   push(rbx);
1207   Register arg3_reg = c_rarg3;
1208   Register arg2_reg = c_rarg2;
1209   push(arg3_reg);
1210   push(arg2_reg);
1211   test_method_data_pointer(arg3_reg, verify_continue); // If mdp is zero, continue
1212   get_method(rbx);
1213 
1214   // If the mdp is valid, it will point to a DataLayout header which is
1215   // consistent with the bcp.  The converse is highly probable also.
1216   load_unsigned_short(arg2_reg,
1217                       Address(arg3_reg, in_bytes(DataLayout::bci_offset())));
1218   addptr(arg2_reg, Address(rbx, Method::const_offset()));
1219   lea(arg2_reg, Address(arg2_reg, ConstMethod::codes_offset()));
1220   cmpptr(arg2_reg, _bcp_register);
1221   jcc(Assembler::equal, verify_continue);
1222   // rbx: method
1223   // _bcp_register: bcp
1224   // c_rarg3: mdp
1225   call_VM_leaf(CAST_FROM_FN_PTR(address, InterpreterRuntime::verify_mdp),
1226                rbx, _bcp_register, arg3_reg);
1227   bind(verify_continue);
1228   pop(arg2_reg);
1229   pop(arg3_reg);
1230   pop(rbx);
1231   pop(rax);
1232 #endif // ASSERT
1233 }
1234 
1235 
1236 void InterpreterMacroAssembler::set_mdp_data_at(Register mdp_in,
1237                                                 int constant,
1238                                                 Register value) {
1239   assert(ProfileInterpreter, "must be profiling interpreter");
1240   Address data(mdp_in, constant);
1241   movptr(data, value);
1242 }
1243 
1244 
1245 void InterpreterMacroAssembler::increment_mdp_data_at(Register mdp_in,
1246                                                       int constant) {
1247   assert(ProfileInterpreter, "must be profiling interpreter");
1248   Address data(mdp_in, constant);
1249   addptr(data, DataLayout::counter_increment);
1250 }
1251 
1252 
1253 void InterpreterMacroAssembler::increment_mdp_data_at(Register mdp_in,
1254                                                       Register index,
1255                                                       int constant) {
1256   assert(ProfileInterpreter, "must be profiling interpreter");
1257   Address data(mdp_in, index, Address::times_1, constant);
1258   addptr(data, DataLayout::counter_increment);
1259 }
1260 
1261 void InterpreterMacroAssembler::set_mdp_flag_at(Register mdp_in,
1262                                                 int flag_byte_constant) {
1263   assert(ProfileInterpreter, "must be profiling interpreter");
1264   int header_offset = in_bytes(DataLayout::flags_offset());
1265   int header_bits = flag_byte_constant;
1266   // Set the flag
1267   orb(Address(mdp_in, header_offset), header_bits);
1268 }
1269 
1270 
1271 
1272 void InterpreterMacroAssembler::test_mdp_data_at(Register mdp_in,
1273                                                  int offset,
1274                                                  Register value,
1275                                                  Register test_value_out,
1276                                                  Label& not_equal_continue) {
1277   assert(ProfileInterpreter, "must be profiling interpreter");
1278   if (test_value_out == noreg) {
1279     cmpptr(value, Address(mdp_in, offset));
1280   } else {
1281     // Put the test value into a register, so caller can use it:
1282     movptr(test_value_out, Address(mdp_in, offset));
1283     cmpptr(test_value_out, value);
1284   }
1285   jcc(Assembler::notEqual, not_equal_continue);
1286 }
1287 
1288 
1289 void InterpreterMacroAssembler::update_mdp_by_offset(Register mdp_in,
1290                                                      int offset_of_disp) {
1291   assert(ProfileInterpreter, "must be profiling interpreter");
1292   Address disp_address(mdp_in, offset_of_disp);
1293   addptr(mdp_in, disp_address);
1294   movptr(Address(rbp, frame::interpreter_frame_mdp_offset * wordSize), mdp_in);
1295 }
1296 
1297 
1298 void InterpreterMacroAssembler::update_mdp_by_offset(Register mdp_in,
1299                                                      Register reg,
1300                                                      int offset_of_disp) {
1301   assert(ProfileInterpreter, "must be profiling interpreter");
1302   Address disp_address(mdp_in, reg, Address::times_1, offset_of_disp);
1303   addptr(mdp_in, disp_address);
1304   movptr(Address(rbp, frame::interpreter_frame_mdp_offset * wordSize), mdp_in);
1305 }
1306 
1307 
1308 void InterpreterMacroAssembler::update_mdp_by_constant(Register mdp_in,
1309                                                        int constant) {
1310   assert(ProfileInterpreter, "must be profiling interpreter");
1311   addptr(mdp_in, constant);
1312   movptr(Address(rbp, frame::interpreter_frame_mdp_offset * wordSize), mdp_in);
1313 }
1314 
1315 
1316 void InterpreterMacroAssembler::update_mdp_for_ret(Register return_bci) {
1317   assert(ProfileInterpreter, "must be profiling interpreter");
1318   push(return_bci); // save/restore across call_VM
1319   call_VM(noreg,
1320           CAST_FROM_FN_PTR(address, InterpreterRuntime::update_mdp_for_ret),
1321           return_bci);
1322   pop(return_bci);
1323 }
1324 
1325 
1326 void InterpreterMacroAssembler::profile_taken_branch(Register mdp) {
1327   if (ProfileInterpreter) {
1328     Label profile_continue;
1329 
1330     // If no method data exists, go to profile_continue.
1331     test_method_data_pointer(mdp, profile_continue);
1332 
1333     // We are taking a branch.  Increment the taken count.
1334     increment_mdp_data_at(mdp, in_bytes(JumpData::taken_offset()));
1335 
1336     // The method data pointer needs to be updated to reflect the new target.
1337     update_mdp_by_offset(mdp, in_bytes(JumpData::displacement_offset()));
1338     bind(profile_continue);
1339   }
1340 }
1341 
1342 
1343 void InterpreterMacroAssembler::profile_not_taken_branch(Register mdp) {
1344   if (ProfileInterpreter) {
1345     Label profile_continue;
1346 
1347     // If no method data exists, go to profile_continue.
1348     test_method_data_pointer(mdp, profile_continue);
1349 
1350     // We are not taking a branch.  Increment the not taken count.
1351     increment_mdp_data_at(mdp, in_bytes(BranchData::not_taken_offset()));
1352 
1353     // The method data pointer needs to be updated to correspond to
1354     // the next bytecode
1355     update_mdp_by_constant(mdp, in_bytes(BranchData::branch_data_size()));
1356     bind(profile_continue);
1357   }
1358 }
1359 
1360 void InterpreterMacroAssembler::profile_call(Register mdp) {
1361   if (ProfileInterpreter) {
1362     Label profile_continue;
1363 
1364     // If no method data exists, go to profile_continue.
1365     test_method_data_pointer(mdp, profile_continue);
1366 
1367     // We are making a call.  Increment the count.
1368     increment_mdp_data_at(mdp, in_bytes(CounterData::count_offset()));
1369 
1370     // The method data pointer needs to be updated to reflect the new target.
1371     update_mdp_by_constant(mdp, in_bytes(CounterData::counter_data_size()));
1372     bind(profile_continue);
1373   }
1374 }
1375 
1376 
1377 void InterpreterMacroAssembler::profile_final_call(Register mdp) {
1378   if (ProfileInterpreter) {
1379     Label profile_continue;
1380 
1381     // If no method data exists, go to profile_continue.
1382     test_method_data_pointer(mdp, profile_continue);
1383 
1384     // We are making a call.  Increment the count.
1385     increment_mdp_data_at(mdp, in_bytes(CounterData::count_offset()));
1386 
1387     // The method data pointer needs to be updated to reflect the new target.
1388     update_mdp_by_constant(mdp,
1389                            in_bytes(VirtualCallData::
1390                                     virtual_call_data_size()));
1391     bind(profile_continue);
1392   }
1393 }
1394 
1395 
1396 void InterpreterMacroAssembler::profile_virtual_call(Register receiver,
1397                                                      Register mdp,
1398                                                      Register reg2,
1399                                                      bool receiver_can_be_null) {
1400   if (ProfileInterpreter) {
1401     Label profile_continue;
1402 
1403     // If no method data exists, go to profile_continue.
1404     test_method_data_pointer(mdp, profile_continue);
1405 
1406     Label skip_receiver_profile;
1407     if (receiver_can_be_null) {
1408       Label not_null;
1409       testptr(receiver, receiver);
1410       jccb(Assembler::notZero, not_null);
1411       // We are making a call.  Increment the count for null receiver.
1412       increment_mdp_data_at(mdp, in_bytes(CounterData::count_offset()));
1413       jmp(skip_receiver_profile);
1414       bind(not_null);
1415     }
1416 
1417     // Record the receiver type.
1418     record_klass_in_profile(receiver, mdp, reg2, true);
1419     bind(skip_receiver_profile);
1420 
1421     // The method data pointer needs to be updated to reflect the new target.
1422     update_mdp_by_constant(mdp, in_bytes(VirtualCallData::virtual_call_data_size()));
1423     bind(profile_continue);
1424   }
1425 }
1426 
1427 // This routine creates a state machine for updating the multi-row
1428 // type profile at a virtual call site (or other type-sensitive bytecode).
1429 // The machine visits each row (of receiver/count) until the receiver type
1430 // is found, or until it runs out of rows.  At the same time, it remembers
1431 // the location of the first empty row.  (An empty row records null for its
1432 // receiver, and can be allocated for a newly-observed receiver type.)
1433 // Because there are two degrees of freedom in the state, a simple linear
1434 // search will not work; it must be a decision tree.  Hence this helper
1435 // function is recursive, to generate the required tree structured code.
1436 // It's the interpreter, so we are trading off code space for speed.
1437 // See below for example code.
1438 void InterpreterMacroAssembler::record_klass_in_profile_helper(
1439                                         Register receiver, Register mdp,
1440                                         Register reg2, int start_row,
1441                                         Label& done, bool is_virtual_call) {
1442   if (TypeProfileWidth == 0) {
1443     increment_mdp_data_at(mdp, in_bytes(CounterData::count_offset()));
1444   } else {
1445     record_item_in_profile_helper(receiver, mdp, reg2, 0, done, TypeProfileWidth,
1446                                   &VirtualCallData::receiver_offset, &VirtualCallData::receiver_count_offset);
1447   }
1448 }
1449 
1450 void InterpreterMacroAssembler::record_item_in_profile_helper(Register item, Register mdp, Register reg2, int start_row,
1451                                                               Label& done, int total_rows,
1452                                                               OffsetFunction item_offset_fn,
1453                                                               OffsetFunction item_count_offset_fn) {
1454   int last_row = total_rows - 1;
1455   assert(start_row <= last_row, "must be work left to do");
1456   // Test this row for both the item and for null.
1457   // Take any of three different outcomes:
1458   //   1. found item => increment count and goto done
1459   //   2. found null => keep looking for case 1, maybe allocate this cell
1460   //   3. found something else => keep looking for cases 1 and 2
1461   // Case 3 is handled by a recursive call.
1462   for (int row = start_row; row <= last_row; row++) {
1463     Label next_test;
1464     bool test_for_null_also = (row == start_row);
1465 
1466     // See if the item is item[n].
1467     int item_offset = in_bytes(item_offset_fn(row));
1468     test_mdp_data_at(mdp, item_offset, item,
1469                      (test_for_null_also ? reg2 : noreg),
1470                      next_test);
1471     // (Reg2 now contains the item from the CallData.)
1472 
1473     // The item is item[n].  Increment count[n].
1474     int count_offset = in_bytes(item_count_offset_fn(row));
1475     increment_mdp_data_at(mdp, count_offset);
1476     jmp(done);
1477     bind(next_test);
1478 
1479     if (test_for_null_also) {
1480       // Failed the equality check on item[n]...  Test for null.
1481       testptr(reg2, reg2);
1482       if (start_row == last_row) {
1483         // The only thing left to do is handle the null case.
1484         Label found_null;
1485         jccb(Assembler::zero, found_null);
1486         // Item did not match any saved item and there is no empty row for it.
1487         // Increment total counter to indicate polymorphic case.
1488         increment_mdp_data_at(mdp, in_bytes(CounterData::count_offset()));
1489         jmp(done);
1490         bind(found_null);
1491         break;
1492       }
1493       Label found_null;
1494       // Since null is rare, make it be the branch-taken case.
1495       jcc(Assembler::zero, found_null);
1496 
1497       // Put all the "Case 3" tests here.
1498       record_item_in_profile_helper(item, mdp, reg2, start_row + 1, done, total_rows,
1499                                     item_offset_fn, item_count_offset_fn);
1500 
1501       // Found a null.  Keep searching for a matching item,
1502       // but remember that this is an empty (unused) slot.
1503       bind(found_null);
1504     }
1505   }
1506 
1507   // In the fall-through case, we found no matching item, but we
1508   // observed the item[start_row] is null.
1509 
1510   // Fill in the item field and increment the count.
1511   int item_offset = in_bytes(item_offset_fn(start_row));
1512   set_mdp_data_at(mdp, item_offset, item);
1513   int count_offset = in_bytes(item_count_offset_fn(start_row));
1514   movl(reg2, DataLayout::counter_increment);
1515   set_mdp_data_at(mdp, count_offset, reg2);
1516   if (start_row > 0) {
1517     jmp(done);
1518   }
1519 }
1520 
1521 // Example state machine code for three profile rows:
1522 //   // main copy of decision tree, rooted at row[1]
1523 //   if (row[0].rec == rec) { row[0].incr(); goto done; }
1524 //   if (row[0].rec != nullptr) {
1525 //     // inner copy of decision tree, rooted at row[1]
1526 //     if (row[1].rec == rec) { row[1].incr(); goto done; }
1527 //     if (row[1].rec != nullptr) {
1528 //       // degenerate decision tree, rooted at row[2]
1529 //       if (row[2].rec == rec) { row[2].incr(); goto done; }
1530 //       if (row[2].rec != nullptr) { count.incr(); goto done; } // overflow
1531 //       row[2].init(rec); goto done;
1532 //     } else {
1533 //       // remember row[1] is empty
1534 //       if (row[2].rec == rec) { row[2].incr(); goto done; }
1535 //       row[1].init(rec); goto done;
1536 //     }
1537 //   } else {
1538 //     // remember row[0] is empty
1539 //     if (row[1].rec == rec) { row[1].incr(); goto done; }
1540 //     if (row[2].rec == rec) { row[2].incr(); goto done; }
1541 //     row[0].init(rec); goto done;
1542 //   }
1543 //   done:
1544 
1545 void InterpreterMacroAssembler::record_klass_in_profile(Register receiver,
1546                                                         Register mdp, Register reg2,
1547                                                         bool is_virtual_call) {
1548   assert(ProfileInterpreter, "must be profiling");
1549   Label done;
1550 
1551   record_klass_in_profile_helper(receiver, mdp, reg2, 0, done, is_virtual_call);
1552 
1553   bind (done);
1554 }
1555 
1556 void InterpreterMacroAssembler::profile_ret(Register return_bci,
1557                                             Register mdp) {
1558   if (ProfileInterpreter) {
1559     Label profile_continue;
1560     uint row;
1561 
1562     // If no method data exists, go to profile_continue.
1563     test_method_data_pointer(mdp, profile_continue);
1564 
1565     // Update the total ret count.
1566     increment_mdp_data_at(mdp, in_bytes(CounterData::count_offset()));
1567 
1568     for (row = 0; row < RetData::row_limit(); row++) {
1569       Label next_test;
1570 
1571       // See if return_bci is equal to bci[n]:
1572       test_mdp_data_at(mdp,
1573                        in_bytes(RetData::bci_offset(row)),
1574                        return_bci, noreg,
1575                        next_test);
1576 
1577       // return_bci is equal to bci[n].  Increment the count.
1578       increment_mdp_data_at(mdp, in_bytes(RetData::bci_count_offset(row)));
1579 
1580       // The method data pointer needs to be updated to reflect the new target.
1581       update_mdp_by_offset(mdp,
1582                            in_bytes(RetData::bci_displacement_offset(row)));
1583       jmp(profile_continue);
1584       bind(next_test);
1585     }
1586 
1587     update_mdp_for_ret(return_bci);
1588 
1589     bind(profile_continue);
1590   }
1591 }
1592 
1593 
1594 void InterpreterMacroAssembler::profile_null_seen(Register mdp) {
1595   if (ProfileInterpreter) {
1596     Label profile_continue;
1597 
1598     // If no method data exists, go to profile_continue.
1599     test_method_data_pointer(mdp, profile_continue);
1600 
1601     set_mdp_flag_at(mdp, BitData::null_seen_byte_constant());
1602 
1603     // The method data pointer needs to be updated.
1604     int mdp_delta = in_bytes(BitData::bit_data_size());
1605     if (TypeProfileCasts) {
1606       mdp_delta = in_bytes(VirtualCallData::virtual_call_data_size());
1607     }
1608     update_mdp_by_constant(mdp, mdp_delta);
1609 
1610     bind(profile_continue);
1611   }
1612 }
1613 
1614 
1615 void InterpreterMacroAssembler::profile_typecheck(Register mdp, Register klass, Register reg2) {
1616   if (ProfileInterpreter) {
1617     Label profile_continue;
1618 
1619     // If no method data exists, go to profile_continue.
1620     test_method_data_pointer(mdp, profile_continue);
1621 
1622     // The method data pointer needs to be updated.
1623     int mdp_delta = in_bytes(BitData::bit_data_size());
1624     if (TypeProfileCasts) {
1625       mdp_delta = in_bytes(VirtualCallData::virtual_call_data_size());
1626 
1627       // Record the object type.
1628       record_klass_in_profile(klass, mdp, reg2, false);
1629     }
1630     update_mdp_by_constant(mdp, mdp_delta);
1631 
1632     bind(profile_continue);
1633   }
1634 }
1635 
1636 
1637 void InterpreterMacroAssembler::profile_switch_default(Register mdp) {
1638   if (ProfileInterpreter) {
1639     Label profile_continue;
1640 
1641     // If no method data exists, go to profile_continue.
1642     test_method_data_pointer(mdp, profile_continue);
1643 
1644     // Update the default case count
1645     increment_mdp_data_at(mdp,
1646                           in_bytes(MultiBranchData::default_count_offset()));
1647 
1648     // The method data pointer needs to be updated.
1649     update_mdp_by_offset(mdp,
1650                          in_bytes(MultiBranchData::
1651                                   default_displacement_offset()));
1652 
1653     bind(profile_continue);
1654   }
1655 }
1656 
1657 
1658 void InterpreterMacroAssembler::profile_switch_case(Register index,
1659                                                     Register mdp,
1660                                                     Register reg2) {
1661   if (ProfileInterpreter) {
1662     Label profile_continue;
1663 
1664     // If no method data exists, go to profile_continue.
1665     test_method_data_pointer(mdp, profile_continue);
1666 
1667     // Build the base (index * per_case_size_in_bytes()) +
1668     // case_array_offset_in_bytes()
1669     movl(reg2, in_bytes(MultiBranchData::per_case_size()));
1670     imulptr(index, reg2); // XXX l ?
1671     addptr(index, in_bytes(MultiBranchData::case_array_offset())); // XXX l ?
1672 
1673     // Update the case count
1674     increment_mdp_data_at(mdp,
1675                           index,
1676                           in_bytes(MultiBranchData::relative_count_offset()));
1677 
1678     // The method data pointer needs to be updated.
1679     update_mdp_by_offset(mdp,
1680                          index,
1681                          in_bytes(MultiBranchData::
1682                                   relative_displacement_offset()));
1683 
1684     bind(profile_continue);
1685   }
1686 }
1687 
1688 
1689 
1690 void InterpreterMacroAssembler::_interp_verify_oop(Register reg, TosState state, const char* file, int line) {
1691   if (state == atos) {
1692     MacroAssembler::_verify_oop_checked(reg, "broken oop", file, line);
1693   }
1694 }
1695 
1696 
1697 // Jump if ((*counter_addr += increment) & mask) == 0
1698 void InterpreterMacroAssembler::increment_mask_and_jump(Address counter_addr, Address mask,
1699                                                         Register scratch, Label* where) {
1700   // This update is actually not atomic and can lose a number of updates
1701   // under heavy contention, but the alternative of using the (contended)
1702   // atomic update here penalizes profiling paths too much.
1703   movl(scratch, counter_addr);
1704   incrementl(scratch, InvocationCounter::count_increment);
1705   movl(counter_addr, scratch);
1706   andl(scratch, mask);
1707   if (where != nullptr) {
1708     jcc(Assembler::zero, *where);
1709   }
1710 }
1711 
1712 void InterpreterMacroAssembler::generate_runtime_upcalls_on_method_entry()
1713 {
1714   address upcall = RuntimeUpcalls::on_method_entry_upcall_address();
1715   if (RuntimeUpcalls::does_upcall_need_method_parameter(upcall)) {
1716     get_method(c_rarg1);
1717     call_VM(noreg,upcall, c_rarg1);
1718   } else {
1719     call_VM(noreg,upcall);
1720   }
1721 }
1722 
1723 void InterpreterMacroAssembler::notify_method_entry() {
1724   // Whenever JVMTI is interp_only_mode, method entry/exit events are sent to
1725   // track stack depth.  If it is possible to enter interp_only_mode we add
1726   // the code to check if the event should be sent.
1727   Register rthread = r15_thread;
1728   Register rarg = c_rarg1;
1729   if (JvmtiExport::can_post_interpreter_events()) {
1730     Label L;
1731     movl(rdx, Address(rthread, JavaThread::interp_only_mode_offset()));
1732     testl(rdx, rdx);
1733     jcc(Assembler::zero, L);
1734     call_VM(noreg, CAST_FROM_FN_PTR(address,
1735                                     InterpreterRuntime::post_method_entry));
1736     bind(L);
1737   }
1738 
1739   if (DTraceMethodProbes) {
1740     get_method(rarg);
1741     call_VM_leaf(CAST_FROM_FN_PTR(address, SharedRuntime::dtrace_method_entry),
1742                  rthread, rarg);
1743   }
1744 
1745   // RedefineClasses() tracing support for obsolete method entry
1746   if (log_is_enabled(Trace, redefine, class, obsolete)) {
1747     get_method(rarg);
1748     call_VM_leaf(
1749       CAST_FROM_FN_PTR(address, SharedRuntime::rc_trace_method_entry),
1750       rthread, rarg);
1751   }
1752 }
1753 
1754 
1755 void InterpreterMacroAssembler::notify_method_exit(
1756     TosState state, NotifyMethodExitMode mode) {
1757   // Whenever JVMTI is interp_only_mode, method entry/exit events are sent to
1758   // track stack depth.  If it is possible to enter interp_only_mode we add
1759   // the code to check if the event should be sent.
1760   Register rthread = r15_thread;
1761   Register rarg = c_rarg1;
1762   if (mode == NotifyJVMTI && JvmtiExport::can_post_interpreter_events()) {
1763     Label L;
1764     // Note: frame::interpreter_frame_result has a dependency on how the
1765     // method result is saved across the call to post_method_exit. If this
1766     // is changed then the interpreter_frame_result implementation will
1767     // need to be updated too.
1768 
1769     // template interpreter will leave the result on the top of the stack.
1770     push(state);
1771     movl(rdx, Address(rthread, JavaThread::interp_only_mode_offset()));
1772     testl(rdx, rdx);
1773     jcc(Assembler::zero, L);
1774     call_VM(noreg,
1775             CAST_FROM_FN_PTR(address, InterpreterRuntime::post_method_exit));
1776     bind(L);
1777     pop(state);
1778   }
1779 
1780   if (DTraceMethodProbes) {
1781     push(state);
1782     get_method(rarg);
1783     call_VM_leaf(CAST_FROM_FN_PTR(address, SharedRuntime::dtrace_method_exit),
1784                  rthread, rarg);
1785     pop(state);
1786   }
1787 }
1788 
1789 void InterpreterMacroAssembler::load_resolved_indy_entry(Register cache, Register index) {
1790   // Get index out of bytecode pointer
1791   get_cache_index_at_bcp(index, 1, sizeof(u4));
1792   // Get address of invokedynamic array
1793   movptr(cache, Address(rbp, frame::interpreter_frame_cache_offset * wordSize));
1794   movptr(cache, Address(cache, in_bytes(ConstantPoolCache::invokedynamic_entries_offset())));
1795   if (is_power_of_2(sizeof(ResolvedIndyEntry))) {
1796     shll(index, log2i_exact(sizeof(ResolvedIndyEntry))); // Scale index by power of 2
1797   } else {
1798     imull(index, index, sizeof(ResolvedIndyEntry)); // Scale the index to be the entry index * sizeof(ResolvedIndyEntry)
1799   }
1800   lea(cache, Address(cache, index, Address::times_1, Array<ResolvedIndyEntry>::base_offset_in_bytes()));
1801 }
1802 
1803 void InterpreterMacroAssembler::load_field_entry(Register cache, Register index, int bcp_offset) {
1804   // Get index out of bytecode pointer
1805   movptr(cache, Address(rbp, frame::interpreter_frame_cache_offset * wordSize));
1806   get_cache_index_at_bcp(index, bcp_offset, sizeof(u2));
1807 
1808   movptr(cache, Address(cache, ConstantPoolCache::field_entries_offset()));
1809   // Take shortcut if the size is a power of 2
1810   if (is_power_of_2(sizeof(ResolvedFieldEntry))) {
1811     shll(index, log2i_exact(sizeof(ResolvedFieldEntry))); // Scale index by power of 2
1812   } else {
1813     imull(index, index, sizeof(ResolvedFieldEntry)); // Scale the index to be the entry index * sizeof(ResolvedFieldEntry)
1814   }
1815   lea(cache, Address(cache, index, Address::times_1, Array<ResolvedFieldEntry>::base_offset_in_bytes()));
1816 }
1817 
1818 void InterpreterMacroAssembler::load_method_entry(Register cache, Register index, int bcp_offset) {
1819   // Get index out of bytecode pointer
1820   movptr(cache, Address(rbp, frame::interpreter_frame_cache_offset * wordSize));
1821   get_cache_index_at_bcp(index, bcp_offset, sizeof(u2));
1822 
1823   movptr(cache, Address(cache, ConstantPoolCache::method_entries_offset()));
1824   imull(index, index, sizeof(ResolvedMethodEntry)); // Scale the index to be the entry index * sizeof(ResolvedMethodEntry)
1825   lea(cache, Address(cache, index, Address::times_1, Array<ResolvedMethodEntry>::base_offset_in_bytes()));
1826 }