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