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