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