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