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