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