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