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
 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 
 249   // Profile the not-null value's klass.
 250   profile_typecheck(r2, Rsub_klass); // blows r2
 251 
 252   // Do the check.
 253   check_klass_subtype(Rsub_klass, r0, r2, ok_is_subtype); // blows r2
 254 }
 255 
 256 // Java Expression Stack
 257 
 258 void InterpreterMacroAssembler::pop_ptr(Register r) {
 259   ldr(r, post(esp, wordSize));
 260 }
 261 
 262 void InterpreterMacroAssembler::pop_i(Register r) {
 263   ldrw(r, post(esp, wordSize));
 264 }
 265 
 266 void InterpreterMacroAssembler::pop_l(Register r) {
 267   ldr(r, post(esp, 2 * Interpreter::stackElementSize));
 268 }
 269 
 270 void InterpreterMacroAssembler::push_ptr(Register r) {
 271   str(r, pre(esp, -wordSize));
 272  }
 273 
 274 void InterpreterMacroAssembler::push_i(Register r) {
 275   str(r, pre(esp, -wordSize));
 276 }
 277 
 278 void InterpreterMacroAssembler::push_l(Register r) {
 279   str(zr, pre(esp, -wordSize));
 280   str(r, pre(esp, - wordSize));
 281 }
 282 
 283 void InterpreterMacroAssembler::pop_f(FloatRegister r) {
 284   ldrs(r, post(esp, wordSize));
 285 }
 286 
 287 void InterpreterMacroAssembler::pop_d(FloatRegister r) {
 288   ldrd(r, post(esp, 2 * Interpreter::stackElementSize));
 289 }
 290 
 291 void InterpreterMacroAssembler::push_f(FloatRegister r) {
 292   strs(r, pre(esp, -wordSize));
 293 }
 294 
 295 void InterpreterMacroAssembler::push_d(FloatRegister r) {
 296   strd(r, pre(esp, 2* -wordSize));
 297 }
 298 
 299 void InterpreterMacroAssembler::pop(TosState state) {
 300   switch (state) {
 301   case atos: pop_ptr();                 break;
 302   case btos:
 303   case ztos:
 304   case ctos:
 305   case stos:
 306   case itos: pop_i();                   break;
 307   case ltos: pop_l();                   break;
 308   case ftos: pop_f();                   break;
 309   case dtos: pop_d();                   break;
 310   case vtos: /* nothing to do */        break;
 311   default:   ShouldNotReachHere();
 312   }
 313   interp_verify_oop(r0, state);
 314 }
 315 
 316 void InterpreterMacroAssembler::push(TosState state) {
 317   interp_verify_oop(r0, state);
 318   switch (state) {
 319   case atos: push_ptr();                break;
 320   case btos:
 321   case ztos:
 322   case ctos:
 323   case stos:
 324   case itos: push_i();                  break;
 325   case ltos: push_l();                  break;
 326   case ftos: push_f();                  break;
 327   case dtos: push_d();                  break;
 328   case vtos: /* nothing to do */        break;
 329   default  : ShouldNotReachHere();
 330   }
 331 }
 332 
 333 // Helpers for swap and dup
 334 void InterpreterMacroAssembler::load_ptr(int n, Register val) {
 335   ldr(val, Address(esp, Interpreter::expr_offset_in_bytes(n)));
 336 }
 337 
 338 void InterpreterMacroAssembler::store_ptr(int n, Register val) {
 339   str(val, Address(esp, Interpreter::expr_offset_in_bytes(n)));
 340 }
 341 
 342 void InterpreterMacroAssembler::load_float(Address src) {
 343   ldrs(v0, src);
 344 }
 345 
 346 void InterpreterMacroAssembler::load_double(Address src) {
 347   ldrd(v0, src);
 348 }
 349 
 350 void InterpreterMacroAssembler::prepare_to_jump_from_interpreted() {
 351   // set sender sp
 352   mov(r19_sender_sp, sp);
 353   // record last_sp
 354   sub(rscratch1, esp, rfp);
 355   asr(rscratch1, rscratch1, Interpreter::logStackElementSize);
 356   str(rscratch1, Address(rfp, frame::interpreter_frame_last_sp_offset * wordSize));
 357 }
 358 
 359 // Jump to from_interpreted entry of a call unless single stepping is possible
 360 // in this thread in which case we must call the i2i entry
 361 void InterpreterMacroAssembler::jump_from_interpreted(Register method, Register temp) {
 362   prepare_to_jump_from_interpreted();
 363 
 364   if (JvmtiExport::can_post_interpreter_events()) {
 365     Label run_compiled_code;
 366     // JVMTI events, such as single-stepping, are implemented partly by avoiding running
 367     // compiled code in threads for which the event is enabled.  Check here for
 368     // interp_only_mode if these events CAN be enabled.
 369     ldrw(rscratch1, Address(rthread, JavaThread::interp_only_mode_offset()));
 370     cbzw(rscratch1, run_compiled_code);
 371     ldr(rscratch1, Address(method, Method::interpreter_entry_offset()));
 372     br(rscratch1);
 373     bind(run_compiled_code);
 374   }
 375 
 376   ldr(rscratch1, Address(method, Method::from_interpreted_offset()));
 377   br(rscratch1);
 378 }
 379 
 380 // The following two routines provide a hook so that an implementation
 381 // can schedule the dispatch in two parts.  amd64 does not do this.
 382 void InterpreterMacroAssembler::dispatch_prolog(TosState state, int step) {
 383 }
 384 
 385 void InterpreterMacroAssembler::dispatch_epilog(TosState state, int step) {
 386     dispatch_next(state, step);
 387 }
 388 
 389 void InterpreterMacroAssembler::dispatch_base(TosState state,
 390                                               address* table,
 391                                               bool verifyoop,
 392                                               bool generate_poll) {
 393   if (VerifyActivationFrameSize) {
 394     Label L;
 395     sub(rscratch2, rfp, esp);
 396     int min_frame_size = (frame::link_offset - frame::interpreter_frame_initial_sp_offset) * wordSize;
 397     subs(rscratch2, rscratch2, min_frame_size);
 398     br(Assembler::GE, L);
 399     stop("broken stack frame");
 400     bind(L);
 401   }
 402   if (verifyoop) {
 403     interp_verify_oop(r0, state);
 404   }
 405 
 406   Label safepoint;
 407   address* const safepoint_table = Interpreter::safept_table(state);
 408   bool needs_thread_local_poll = generate_poll && table != safepoint_table;
 409 
 410   if (needs_thread_local_poll) {
 411     NOT_PRODUCT(block_comment("Thread-local Safepoint poll"));
 412     ldr(rscratch2, Address(rthread, JavaThread::polling_word_offset()));
 413     tbnz(rscratch2, exact_log2(SafepointMechanism::poll_bit()), safepoint);
 414   }
 415 
 416   if (table == Interpreter::dispatch_table(state)) {
 417     addw(rscratch2, rscratch1, Interpreter::distance_from_dispatch_table(state));
 418     ldr(rscratch2, Address(rdispatch, rscratch2, Address::uxtw(3)));
 419   } else {
 420     mov(rscratch2, (address)table);
 421     ldr(rscratch2, Address(rscratch2, rscratch1, Address::uxtw(3)));
 422   }
 423   br(rscratch2);
 424 
 425   if (needs_thread_local_poll) {
 426     bind(safepoint);
 427     lea(rscratch2, ExternalAddress((address)safepoint_table));
 428     ldr(rscratch2, Address(rscratch2, rscratch1, Address::uxtw(3)));
 429     br(rscratch2);
 430   }
 431 }
 432 
 433 void InterpreterMacroAssembler::dispatch_only(TosState state, bool generate_poll) {
 434   dispatch_base(state, Interpreter::dispatch_table(state), true, generate_poll);
 435 }
 436 
 437 void InterpreterMacroAssembler::dispatch_only_normal(TosState state) {
 438   dispatch_base(state, Interpreter::normal_table(state));
 439 }
 440 
 441 void InterpreterMacroAssembler::dispatch_only_noverify(TosState state) {
 442   dispatch_base(state, Interpreter::normal_table(state), false);
 443 }
 444 
 445 
 446 void InterpreterMacroAssembler::dispatch_next(TosState state, int step, bool generate_poll) {
 447   // load next bytecode
 448   ldrb(rscratch1, Address(pre(rbcp, step)));
 449   dispatch_base(state, Interpreter::dispatch_table(state), /*verifyoop*/true, generate_poll);
 450 }
 451 
 452 void InterpreterMacroAssembler::dispatch_via(TosState state, address* table) {
 453   // load current bytecode
 454   ldrb(rscratch1, Address(rbcp, 0));
 455   dispatch_base(state, table);
 456 }
 457 
 458 // remove activation
 459 //
 460 // Unlock the receiver if this is a synchronized method.
 461 // Unlock any Java monitors from synchronized blocks.
 462 // Apply stack watermark barrier.
 463 // Notify JVMTI.
 464 // Remove the activation from the stack.
 465 //
 466 // If there are locked Java monitors
 467 //    If throw_monitor_exception
 468 //       throws IllegalMonitorStateException
 469 //    Else if install_monitor_exception
 470 //       installs IllegalMonitorStateException
 471 //    Else
 472 //       no error processing
 473 void InterpreterMacroAssembler::remove_activation(TosState state,
 474                                                   bool throw_monitor_exception,
 475                                                   bool install_monitor_exception,
 476                                                   bool notify_jvmdi) {
 477   // Note: Registers r3 xmm0 may be in use for the
 478   // result check if synchronized method
 479   Label unlocked, unlock, no_unlock;
 480 
 481 #ifdef ASSERT
 482   Label not_preempted;
 483   ldr(rscratch1, Address(rthread, JavaThread::preempt_alternate_return_offset()));
 484   cbz(rscratch1, not_preempted);
 485   stop("remove_activation: should not have alternate return address set");
 486   bind(not_preempted);
 487 #endif /* ASSERT */
 488 
 489   // get the value of _do_not_unlock_if_synchronized into r3
 490   const Address do_not_unlock_if_synchronized(rthread,
 491     in_bytes(JavaThread::do_not_unlock_if_synchronized_offset()));
 492   ldrb(r3, do_not_unlock_if_synchronized);
 493   strb(zr, do_not_unlock_if_synchronized); // reset the flag
 494 
 495  // get method access flags
 496   ldr(r1, Address(rfp, frame::interpreter_frame_method_offset * wordSize));
 497   ldrh(r2, Address(r1, Method::access_flags_offset()));
 498   tbz(r2, exact_log2(JVM_ACC_SYNCHRONIZED), unlocked);
 499 
 500   // Don't unlock anything if the _do_not_unlock_if_synchronized flag
 501   // is set.
 502   cbnz(r3, no_unlock);
 503 
 504   // unlock monitor
 505   push(state); // save result
 506 
 507   // BasicObjectLock will be first in list, since this is a
 508   // synchronized method. However, need to check that the object has
 509   // not been unlocked by an explicit monitorexit bytecode.
 510   const Address monitor(rfp, frame::interpreter_frame_initial_sp_offset *
 511                         wordSize - (int) sizeof(BasicObjectLock));
 512   // We use c_rarg1 so that if we go slow path it will be the correct
 513   // register for unlock_object to pass to VM directly
 514   lea(c_rarg1, monitor); // address of first monitor
 515 
 516   ldr(r0, Address(c_rarg1, BasicObjectLock::obj_offset()));
 517   cbnz(r0, unlock);
 518 
 519   pop(state);
 520   if (throw_monitor_exception) {
 521     // Entry already unlocked, need to throw exception
 522     call_VM(noreg, CAST_FROM_FN_PTR(address,
 523                    InterpreterRuntime::throw_illegal_monitor_state_exception));
 524     should_not_reach_here();
 525   } else {
 526     // Monitor already unlocked during a stack unroll. If requested,
 527     // install an illegal_monitor_state_exception.  Continue with
 528     // stack unrolling.
 529     if (install_monitor_exception) {
 530       call_VM(noreg, CAST_FROM_FN_PTR(address,
 531                      InterpreterRuntime::new_illegal_monitor_state_exception));
 532     }
 533     b(unlocked);
 534   }
 535 
 536   bind(unlock);
 537   unlock_object(c_rarg1);
 538   pop(state);
 539 
 540   // Check that for block-structured locking (i.e., that all locked
 541   // objects has been unlocked)
 542   bind(unlocked);
 543 
 544   // r0: Might contain return value
 545 
 546   // Check that all monitors are unlocked
 547   {
 548     Label loop, exception, entry, restart;
 549     const int entry_size = frame::interpreter_frame_monitor_size_in_bytes();
 550     const Address monitor_block_top(
 551         rfp, frame::interpreter_frame_monitor_block_top_offset * wordSize);
 552     const Address monitor_block_bot(
 553         rfp, frame::interpreter_frame_initial_sp_offset * wordSize);
 554 
 555     bind(restart);
 556     // We use c_rarg1 so that if we go slow path it will be the correct
 557     // register for unlock_object to pass to VM directly
 558     ldr(c_rarg1, monitor_block_top); // derelativize pointer
 559     lea(c_rarg1, Address(rfp, c_rarg1, Address::lsl(Interpreter::logStackElementSize)));
 560     // c_rarg1 points to current entry, starting with top-most entry
 561 
 562     lea(r19, monitor_block_bot);  // points to word before bottom of
 563                                   // monitor block
 564     b(entry);
 565 
 566     // Entry already locked, need to throw exception
 567     bind(exception);
 568 
 569     if (throw_monitor_exception) {
 570       // Throw exception
 571       MacroAssembler::call_VM(noreg,
 572                               CAST_FROM_FN_PTR(address, InterpreterRuntime::
 573                                    throw_illegal_monitor_state_exception));
 574       should_not_reach_here();
 575     } else {
 576       // Stack unrolling. Unlock object and install illegal_monitor_exception.
 577       // Unlock does not block, so don't have to worry about the frame.
 578       // We don't have to preserve c_rarg1 since we are going to throw an exception.
 579 
 580       push(state);
 581       unlock_object(c_rarg1);
 582       pop(state);
 583 
 584       if (install_monitor_exception) {
 585         call_VM(noreg, CAST_FROM_FN_PTR(address,
 586                                         InterpreterRuntime::
 587                                         new_illegal_monitor_state_exception));
 588       }
 589 
 590       b(restart);
 591     }
 592 
 593     bind(loop);
 594     // check if current entry is used
 595     ldr(rscratch1, Address(c_rarg1, BasicObjectLock::obj_offset()));
 596     cbnz(rscratch1, exception);
 597 
 598     add(c_rarg1, c_rarg1, entry_size); // otherwise advance to next entry
 599     bind(entry);
 600     cmp(c_rarg1, r19); // check if bottom reached
 601     br(Assembler::NE, loop); // if not at bottom then check this entry
 602   }
 603 
 604   bind(no_unlock);
 605 
 606   JFR_ONLY(enter_jfr_critical_section();)
 607 
 608   // The below poll is for the stack watermark barrier. It allows fixing up frames lazily,
 609   // that would normally not be safe to use. Such bad returns into unsafe territory of
 610   // the stack, will call InterpreterRuntime::at_unwind.
 611   Label slow_path;
 612   Label fast_path;
 613   safepoint_poll(slow_path, true /* at_return */, false /* in_nmethod */);
 614   br(Assembler::AL, fast_path);
 615   bind(slow_path);
 616   push(state);
 617   set_last_Java_frame(esp, rfp, pc(), rscratch1);
 618   super_call_VM_leaf(CAST_FROM_FN_PTR(address, InterpreterRuntime::at_unwind), rthread);
 619   reset_last_Java_frame(true);
 620   pop(state);
 621   bind(fast_path);
 622 
 623   // JVMTI support. Make sure the safepoint poll test is issued prior.
 624   if (notify_jvmdi) {
 625     notify_method_exit(state, NotifyJVMTI);    // preserve TOSCA
 626   } else {
 627     notify_method_exit(state, SkipNotifyJVMTI); // preserve TOSCA
 628   }
 629 
 630   // remove activation
 631   // get sender esp
 632   ldr(rscratch2,
 633       Address(rfp, frame::interpreter_frame_sender_sp_offset * wordSize));
 634   if (StackReservedPages > 0) {
 635     // testing if reserved zone needs to be re-enabled
 636     Label no_reserved_zone_enabling;
 637 
 638     // check if already enabled - if so no re-enabling needed
 639     assert(sizeof(StackOverflow::StackGuardState) == 4, "unexpected size");
 640     ldrw(rscratch1, Address(rthread, JavaThread::stack_guard_state_offset()));
 641     cmpw(rscratch1, (u1)StackOverflow::stack_guard_enabled);
 642     br(Assembler::EQ, no_reserved_zone_enabling);
 643 
 644     // look for an overflow into the stack reserved zone, i.e.
 645     // interpreter_frame_sender_sp <= JavaThread::reserved_stack_activation
 646     ldr(rscratch1, Address(rthread, JavaThread::reserved_stack_activation_offset()));
 647     cmp(rscratch2, rscratch1);
 648     br(Assembler::LS, no_reserved_zone_enabling);
 649 
 650     JFR_ONLY(leave_jfr_critical_section();)
 651 
 652     call_VM_leaf(
 653       CAST_FROM_FN_PTR(address, SharedRuntime::enable_stack_reserved_zone), rthread);
 654     call_VM(noreg, CAST_FROM_FN_PTR(address,
 655                    InterpreterRuntime::throw_delayed_StackOverflowError));
 656     should_not_reach_here();
 657 
 658     bind(no_reserved_zone_enabling);
 659   }
 660 
 661   // remove frame anchor
 662   leave();
 663 
 664   JFR_ONLY(leave_jfr_critical_section();)
 665 
 666   // restore sender esp
 667   mov(esp, rscratch2);
 668 
 669   // If we're returning to interpreted code we will shortly be
 670   // adjusting SP to allow some space for ESP.  If we're returning to
 671   // compiled code the saved sender SP was saved in sender_sp, so this
 672   // restores it.
 673   andr(sp, esp, -16);
 674 }
 675 
 676 #if INCLUDE_JFR
 677 void InterpreterMacroAssembler::enter_jfr_critical_section() {
 678   const Address sampling_critical_section(rthread, in_bytes(SAMPLING_CRITICAL_SECTION_OFFSET_JFR));
 679   mov(rscratch1, true);
 680   strb(rscratch1, sampling_critical_section);
 681 }
 682 
 683 void InterpreterMacroAssembler::leave_jfr_critical_section() {
 684   const Address sampling_critical_section(rthread, in_bytes(SAMPLING_CRITICAL_SECTION_OFFSET_JFR));
 685   strb(zr, sampling_critical_section);
 686 }
 687 #endif // INCLUDE_JFR
 688 
 689 // Lock object
 690 //
 691 // Args:
 692 //      c_rarg1: BasicObjectLock to be used for locking
 693 //
 694 // Kills:
 695 //      r0
 696 //      c_rarg0, c_rarg1, c_rarg2, c_rarg3, c_rarg4, .. (param regs)
 697 //      rscratch1, rscratch2 (scratch regs)
 698 void InterpreterMacroAssembler::lock_object(Register lock_reg)
 699 {
 700   assert(lock_reg == c_rarg1, "The argument is only for looks. It must be c_rarg1");
 701 
 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   // Load object pointer into obj_reg %c_rarg3
 708   ldr(obj_reg, Address(lock_reg, BasicObjectLock::obj_offset()));
 709 
 710   Label slow_case, done;
 711   fast_lock(lock_reg, obj_reg, tmp, tmp2, tmp3, slow_case);
 712   b(done);
 713 
 714   bind(slow_case);
 715 
 716   // Call the runtime routine for slow case
 717   call_VM_preemptable(noreg,
 718           CAST_FROM_FN_PTR(address, InterpreterRuntime::monitorenter),
 719           lock_reg);
 720 
 721   bind(done);
 722 }
 723 
 724 
 725 // Unlocks an object. Used in monitorexit bytecode and
 726 // remove_activation.  Throws an IllegalMonitorException if object is
 727 // not locked by current thread.
 728 //
 729 // Args:
 730 //      c_rarg1: BasicObjectLock for lock
 731 //
 732 // Kills:
 733 //      r0
 734 //      c_rarg0, c_rarg1, c_rarg2, c_rarg3, ... (param regs)
 735 //      rscratch1, rscratch2 (scratch regs)
 736 void InterpreterMacroAssembler::unlock_object(Register lock_reg)
 737 {
 738   assert(lock_reg == c_rarg1, "The argument is only for looks. It must be rarg1");
 739 
 740   const Register swap_reg   = r0;
 741   const Register header_reg = c_rarg2;  // Will contain the old oopMark
 742   const Register obj_reg    = c_rarg3;  // Will contain the oop
 743   const Register tmp_reg    = c_rarg4;  // Temporary used by fast_unlock
 744 
 745   save_bcp(); // Save in case of exception
 746 
 747   // Load oop into obj_reg(%c_rarg3)
 748   ldr(obj_reg, Address(lock_reg, BasicObjectLock::obj_offset()));
 749 
 750   // Free entry
 751   str(zr, Address(lock_reg, BasicObjectLock::obj_offset()));
 752 
 753   Label slow_case, done;
 754   fast_unlock(obj_reg, header_reg, swap_reg, tmp_reg, slow_case);
 755   b(done);
 756 
 757   bind(slow_case);
 758   // Call the runtime routine for slow case.
 759   str(obj_reg, Address(lock_reg, BasicObjectLock::obj_offset())); // restore obj
 760   call_VM_leaf(CAST_FROM_FN_PTR(address, InterpreterRuntime::monitorexit), lock_reg);
 761   bind(done);
 762   restore_bcp();
 763 }
 764 
 765 void InterpreterMacroAssembler::test_method_data_pointer(Register mdp,
 766                                                          Label& zero_continue) {
 767   assert(ProfileInterpreter, "must be profiling interpreter");
 768   ldr(mdp, Address(rfp, frame::interpreter_frame_mdp_offset * wordSize));
 769   cbz(mdp, zero_continue);
 770 }
 771 
 772 // Set the method data pointer for the current bcp.
 773 void InterpreterMacroAssembler::set_method_data_pointer_for_bcp() {
 774   assert(ProfileInterpreter, "must be profiling interpreter");
 775   Label set_mdp;
 776   stp(r0, r1, Address(pre(sp, -2 * wordSize)));
 777 
 778   // Test MDO to avoid the call if it is null.
 779   ldr(r0, Address(rmethod, in_bytes(Method::method_data_offset())));
 780   cbz(r0, set_mdp);
 781   call_VM_leaf(CAST_FROM_FN_PTR(address, InterpreterRuntime::bcp_to_di), rmethod, rbcp);
 782   // r0: mdi
 783   // mdo is guaranteed to be non-zero here, we checked for it before the call.
 784   ldr(r1, Address(rmethod, in_bytes(Method::method_data_offset())));
 785   lea(r1, Address(r1, in_bytes(MethodData::data_offset())));
 786   add(r0, r1, r0);
 787   str(r0, Address(rfp, frame::interpreter_frame_mdp_offset * wordSize));
 788   bind(set_mdp);
 789   ldp(r0, r1, Address(post(sp, 2 * wordSize)));
 790 }
 791 
 792 void InterpreterMacroAssembler::verify_method_data_pointer() {
 793   assert(ProfileInterpreter, "must be profiling interpreter");
 794 #ifdef ASSERT
 795   Label verify_continue;
 796   stp(r0, r1, Address(pre(sp, -2 * wordSize)));
 797   stp(r2, r3, Address(pre(sp, -2 * wordSize)));
 798   test_method_data_pointer(r3, verify_continue); // If mdp is zero, continue
 799   get_method(r1);
 800 
 801   // If the mdp is valid, it will point to a DataLayout header which is
 802   // consistent with the bcp.  The converse is highly probable also.
 803   ldrsh(r2, Address(r3, in_bytes(DataLayout::bci_offset())));
 804   ldr(rscratch1, Address(r1, Method::const_offset()));
 805   add(r2, r2, rscratch1, Assembler::LSL);
 806   lea(r2, Address(r2, ConstMethod::codes_offset()));
 807   cmp(r2, rbcp);
 808   br(Assembler::EQ, verify_continue);
 809   // r1: method
 810   // rbcp: bcp // rbcp == 22
 811   // r3: mdp
 812   call_VM_leaf(CAST_FROM_FN_PTR(address, InterpreterRuntime::verify_mdp),
 813                r1, rbcp, r3);
 814   bind(verify_continue);
 815   ldp(r2, r3, Address(post(sp, 2 * wordSize)));
 816   ldp(r0, r1, Address(post(sp, 2 * wordSize)));
 817 #endif // ASSERT
 818 }
 819 
 820 
 821 void InterpreterMacroAssembler::set_mdp_data_at(Register mdp_in,
 822                                                 int constant,
 823                                                 Register value) {
 824   assert(ProfileInterpreter, "must be profiling interpreter");
 825   Address data(mdp_in, constant);
 826   str(value, data);
 827 }
 828 
 829 
 830 void InterpreterMacroAssembler::increment_mdp_data_at(Register mdp_in,
 831                                                       int constant) {
 832   increment_mdp_data_at(mdp_in, noreg, constant);
 833 }
 834 
 835 void InterpreterMacroAssembler::increment_mdp_data_at(Register mdp_in,
 836                                                       Register index,
 837                                                       int constant) {
 838   assert(ProfileInterpreter, "must be profiling interpreter");
 839 
 840   assert_different_registers(rscratch2, rscratch1, mdp_in, index);
 841 
 842   Address addr1(mdp_in, constant);
 843   Address addr2(rscratch2, index, Address::lsl(0));
 844   Address &addr = addr1;
 845   if (index != noreg) {
 846     lea(rscratch2, addr1);
 847     addr = addr2;
 848   }
 849 
 850   increment(addr, DataLayout::counter_increment);
 851 }
 852 
 853 void InterpreterMacroAssembler::set_mdp_flag_at(Register mdp_in,
 854                                                 int flag_byte_constant) {
 855   assert(ProfileInterpreter, "must be profiling interpreter");
 856   int flags_offset = in_bytes(DataLayout::flags_offset());
 857   // Set the flag
 858   ldrb(rscratch1, Address(mdp_in, flags_offset));
 859   orr(rscratch1, rscratch1, flag_byte_constant);
 860   strb(rscratch1, Address(mdp_in, flags_offset));
 861 }
 862 
 863 
 864 void InterpreterMacroAssembler::test_mdp_data_at(Register mdp_in,
 865                                                  int offset,
 866                                                  Register value,
 867                                                  Register test_value_out,
 868                                                  Label& not_equal_continue) {
 869   assert(ProfileInterpreter, "must be profiling interpreter");
 870   if (test_value_out == noreg) {
 871     ldr(rscratch1, Address(mdp_in, offset));
 872     cmp(value, rscratch1);
 873   } else {
 874     // Put the test value into a register, so caller can use it:
 875     ldr(test_value_out, Address(mdp_in, offset));
 876     cmp(value, test_value_out);
 877   }
 878   br(Assembler::NE, not_equal_continue);
 879 }
 880 
 881 
 882 void InterpreterMacroAssembler::update_mdp_by_offset(Register mdp_in,
 883                                                      int offset_of_disp) {
 884   assert(ProfileInterpreter, "must be profiling interpreter");
 885   ldr(rscratch1, Address(mdp_in, offset_of_disp));
 886   add(mdp_in, mdp_in, rscratch1, LSL);
 887   str(mdp_in, Address(rfp, frame::interpreter_frame_mdp_offset * wordSize));
 888 }
 889 
 890 
 891 void InterpreterMacroAssembler::update_mdp_by_offset(Register mdp_in,
 892                                                      Register reg,
 893                                                      int offset_of_disp) {
 894   assert(ProfileInterpreter, "must be profiling interpreter");
 895   lea(rscratch1, Address(mdp_in, offset_of_disp));
 896   ldr(rscratch1, Address(rscratch1, reg, Address::lsl(0)));
 897   add(mdp_in, mdp_in, rscratch1, LSL);
 898   str(mdp_in, Address(rfp, frame::interpreter_frame_mdp_offset * wordSize));
 899 }
 900 
 901 
 902 void InterpreterMacroAssembler::update_mdp_by_constant(Register mdp_in,
 903                                                        int constant) {
 904   assert(ProfileInterpreter, "must be profiling interpreter");
 905   add(mdp_in, mdp_in, (unsigned)constant);
 906   str(mdp_in, Address(rfp, frame::interpreter_frame_mdp_offset * wordSize));
 907 }
 908 
 909 
 910 void InterpreterMacroAssembler::update_mdp_for_ret(Register return_bci) {
 911   assert(ProfileInterpreter, "must be profiling interpreter");
 912   // save/restore across call_VM
 913   stp(zr, return_bci, Address(pre(sp, -2 * wordSize)));
 914   call_VM(noreg,
 915           CAST_FROM_FN_PTR(address, InterpreterRuntime::update_mdp_for_ret),
 916           return_bci);
 917   ldp(zr, return_bci, Address(post(sp, 2 * wordSize)));
 918 }
 919 
 920 
 921 void InterpreterMacroAssembler::profile_taken_branch(Register mdp) {
 922   if (ProfileInterpreter) {
 923     Label profile_continue;
 924 
 925     // If no method data exists, go to profile_continue.
 926     test_method_data_pointer(mdp, profile_continue);
 927 
 928     // We are taking a branch.  Increment the taken count.
 929     increment_mdp_data_at(mdp, in_bytes(JumpData::taken_offset()));
 930 
 931     // The method data pointer needs to be updated to reflect the new target.
 932     update_mdp_by_offset(mdp, in_bytes(JumpData::displacement_offset()));
 933     bind(profile_continue);
 934   }
 935 }
 936 
 937 
 938 void InterpreterMacroAssembler::profile_not_taken_branch(Register mdp) {
 939   if (ProfileInterpreter) {
 940     Label profile_continue;
 941 
 942     // If no method data exists, go to profile_continue.
 943     test_method_data_pointer(mdp, profile_continue);
 944 
 945     // We are not taking a branch.  Increment the not taken count.
 946     increment_mdp_data_at(mdp, in_bytes(BranchData::not_taken_offset()));
 947 
 948     // The method data pointer needs to be updated to correspond to
 949     // the next bytecode
 950     update_mdp_by_constant(mdp, in_bytes(BranchData::branch_data_size()));
 951     bind(profile_continue);
 952   }
 953 }
 954 
 955 
 956 void InterpreterMacroAssembler::profile_call(Register mdp) {
 957   if (ProfileInterpreter) {
 958     Label profile_continue;
 959 
 960     // If no method data exists, go to profile_continue.
 961     test_method_data_pointer(mdp, profile_continue);
 962 
 963     // We are making a call.  Increment the count.
 964     increment_mdp_data_at(mdp, in_bytes(CounterData::count_offset()));
 965 
 966     // The method data pointer needs to be updated to reflect the new target.
 967     update_mdp_by_constant(mdp, in_bytes(CounterData::counter_data_size()));
 968     bind(profile_continue);
 969   }
 970 }
 971 
 972 void InterpreterMacroAssembler::profile_final_call(Register mdp) {
 973   if (ProfileInterpreter) {
 974     Label profile_continue;
 975 
 976     // If no method data exists, go to profile_continue.
 977     test_method_data_pointer(mdp, profile_continue);
 978 
 979     // We are making a call.  Increment the count.
 980     increment_mdp_data_at(mdp, in_bytes(CounterData::count_offset()));
 981 
 982     // The method data pointer needs to be updated to reflect the new target.
 983     update_mdp_by_constant(mdp,
 984                            in_bytes(VirtualCallData::
 985                                     virtual_call_data_size()));
 986     bind(profile_continue);
 987   }
 988 }
 989 
 990 
 991 void InterpreterMacroAssembler::profile_virtual_call(Register receiver,
 992                                                      Register mdp,
 993                                                      bool receiver_can_be_null) {
 994   if (ProfileInterpreter) {
 995     Label profile_continue;
 996 
 997     // If no method data exists, go to profile_continue.
 998     test_method_data_pointer(mdp, profile_continue);
 999 
1000     Label skip_receiver_profile;
1001     if (receiver_can_be_null) {
1002       Label not_null;
1003       // We are making a call.  Increment the count for null receiver.
1004       increment_mdp_data_at(mdp, in_bytes(CounterData::count_offset()));
1005       b(skip_receiver_profile);
1006       bind(not_null);
1007     }
1008 
1009     // Record the receiver type.
1010     profile_receiver_type(receiver, mdp, 0);
1011     bind(skip_receiver_profile);
1012 
1013     // The method data pointer needs to be updated to reflect the new target.
1014     update_mdp_by_constant(mdp, in_bytes(VirtualCallData::virtual_call_data_size()));
1015     bind(profile_continue);
1016   }
1017 }
1018 
1019 void InterpreterMacroAssembler::profile_ret(Register return_bci,
1020                                             Register mdp) {
1021   if (ProfileInterpreter) {
1022     Label profile_continue;
1023     uint row;
1024 
1025     // If no method data exists, go to profile_continue.
1026     test_method_data_pointer(mdp, profile_continue);
1027 
1028     // Update the total ret count.
1029     increment_mdp_data_at(mdp, in_bytes(CounterData::count_offset()));
1030 
1031     for (row = 0; row < RetData::row_limit(); row++) {
1032       Label next_test;
1033 
1034       // See if return_bci is equal to bci[n]:
1035       test_mdp_data_at(mdp,
1036                        in_bytes(RetData::bci_offset(row)),
1037                        return_bci, noreg,
1038                        next_test);
1039 
1040       // return_bci is equal to bci[n].  Increment the count.
1041       increment_mdp_data_at(mdp, in_bytes(RetData::bci_count_offset(row)));
1042 
1043       // The method data pointer needs to be updated to reflect the new target.
1044       update_mdp_by_offset(mdp,
1045                            in_bytes(RetData::bci_displacement_offset(row)));
1046       b(profile_continue);
1047       bind(next_test);
1048     }
1049 
1050     update_mdp_for_ret(return_bci);
1051 
1052     bind(profile_continue);
1053   }
1054 }
1055 
1056 void InterpreterMacroAssembler::profile_null_seen(Register mdp) {
1057   if (ProfileInterpreter) {
1058     Label profile_continue;
1059 
1060     // If no method data exists, go to profile_continue.
1061     test_method_data_pointer(mdp, profile_continue);
1062 
1063     set_mdp_flag_at(mdp, BitData::null_seen_byte_constant());
1064 
1065     // The method data pointer needs to be updated.
1066     int mdp_delta = in_bytes(BitData::bit_data_size());
1067     if (TypeProfileCasts) {
1068       mdp_delta = in_bytes(VirtualCallData::virtual_call_data_size());
1069     }
1070     update_mdp_by_constant(mdp, mdp_delta);
1071 
1072     bind(profile_continue);
1073   }
1074 }
1075 
1076 void InterpreterMacroAssembler::profile_typecheck(Register mdp, Register klass) {
1077   if (ProfileInterpreter) {
1078     Label profile_continue;
1079 
1080     // If no method data exists, go to profile_continue.
1081     test_method_data_pointer(mdp, profile_continue);
1082 
1083     // The method data pointer needs to be updated.
1084     int mdp_delta = in_bytes(BitData::bit_data_size());
1085     if (TypeProfileCasts) {
1086       mdp_delta = in_bytes(VirtualCallData::virtual_call_data_size());
1087 
1088       // Record the object type.
1089       profile_receiver_type(klass, mdp, 0);
1090     }
1091     update_mdp_by_constant(mdp, mdp_delta);
1092 
1093     bind(profile_continue);
1094   }
1095 }
1096 
1097 void InterpreterMacroAssembler::profile_switch_default(Register mdp) {
1098   if (ProfileInterpreter) {
1099     Label profile_continue;
1100 
1101     // If no method data exists, go to profile_continue.
1102     test_method_data_pointer(mdp, profile_continue);
1103 
1104     // Update the default case count
1105     increment_mdp_data_at(mdp,
1106                           in_bytes(MultiBranchData::default_count_offset()));
1107 
1108     // The method data pointer needs to be updated.
1109     update_mdp_by_offset(mdp,
1110                          in_bytes(MultiBranchData::
1111                                   default_displacement_offset()));
1112 
1113     bind(profile_continue);
1114   }
1115 }
1116 
1117 void InterpreterMacroAssembler::profile_switch_case(Register index,
1118                                                     Register mdp,
1119                                                     Register reg2) {
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     // Build the base (index * per_case_size_in_bytes()) +
1127     // case_array_offset_in_bytes()
1128     movw(reg2, in_bytes(MultiBranchData::per_case_size()));
1129     movw(rscratch1, in_bytes(MultiBranchData::case_array_offset()));
1130     Assembler::maddw(index, index, reg2, rscratch1);
1131 
1132     // Update the case count
1133     increment_mdp_data_at(mdp,
1134                           index,
1135                           in_bytes(MultiBranchData::relative_count_offset()));
1136 
1137     // The method data pointer needs to be updated.
1138     update_mdp_by_offset(mdp,
1139                          index,
1140                          in_bytes(MultiBranchData::
1141                                   relative_displacement_offset()));
1142 
1143     bind(profile_continue);
1144   }
1145 }
1146 
1147 void InterpreterMacroAssembler::_interp_verify_oop(Register reg, TosState state, const char* file, int line) {
1148   if (state == atos) {
1149     MacroAssembler::_verify_oop_checked(reg, "broken oop", file, line);
1150   }
1151 }
1152 
1153 void InterpreterMacroAssembler::notify_method_entry() {
1154   // Whenever JVMTI is interp_only_mode, method entry/exit events are sent to
1155   // track stack depth.  If it is possible to enter interp_only_mode we add
1156   // the code to check if the event should be sent.
1157   if (JvmtiExport::can_post_interpreter_events()) {
1158     Label L;
1159     ldrw(r3, Address(rthread, JavaThread::interp_only_mode_offset()));
1160     cbzw(r3, L);
1161     call_VM(noreg, CAST_FROM_FN_PTR(address,
1162                                     InterpreterRuntime::post_method_entry));
1163     bind(L);
1164   }
1165 
1166   if (DTraceMethodProbes) {
1167     get_method(c_rarg1);
1168     call_VM_leaf(CAST_FROM_FN_PTR(address, SharedRuntime::dtrace_method_entry),
1169                  rthread, c_rarg1);
1170   }
1171 
1172   // RedefineClasses() tracing support for obsolete method entry
1173   if (log_is_enabled(Trace, redefine, class, obsolete)) {
1174     get_method(c_rarg1);
1175     call_VM_leaf(
1176       CAST_FROM_FN_PTR(address, SharedRuntime::rc_trace_method_entry),
1177       rthread, c_rarg1);
1178   }
1179 
1180  }
1181 
1182 
1183 void InterpreterMacroAssembler::notify_method_exit(
1184     TosState state, NotifyMethodExitMode mode) {
1185   // Whenever JVMTI is interp_only_mode, method entry/exit events are sent to
1186   // track stack depth.  If it is possible to enter interp_only_mode we add
1187   // the code to check if the event should be sent.
1188   if (mode == NotifyJVMTI && JvmtiExport::can_post_interpreter_events()) {
1189     Label L;
1190     // Note: frame::interpreter_frame_result has a dependency on how the
1191     // method result is saved across the call to post_method_exit. If this
1192     // is changed then the interpreter_frame_result implementation will
1193     // need to be updated too.
1194 
1195     // template interpreter will leave the result on the top of the stack.
1196     push(state);
1197     ldrw(r3, Address(rthread, JavaThread::interp_only_mode_offset()));
1198     cbz(r3, L);
1199     call_VM(noreg,
1200             CAST_FROM_FN_PTR(address, InterpreterRuntime::post_method_exit));
1201     bind(L);
1202     pop(state);
1203   }
1204 
1205   if (DTraceMethodProbes) {
1206     push(state);
1207     get_method(c_rarg1);
1208     call_VM_leaf(CAST_FROM_FN_PTR(address, SharedRuntime::dtrace_method_exit),
1209                  rthread, c_rarg1);
1210     pop(state);
1211   }
1212 }
1213 
1214 
1215 // Jump if ((*counter_addr += increment) & mask) satisfies the condition.
1216 void InterpreterMacroAssembler::increment_mask_and_jump(Address counter_addr,
1217                                                         int increment, Address mask,
1218                                                         Register scratch, Register scratch2,
1219                                                         bool preloaded, Condition cond,
1220                                                         Label* where) {
1221   if (!preloaded) {
1222     ldrw(scratch, counter_addr);
1223   }
1224   add(scratch, scratch, increment);
1225   strw(scratch, counter_addr);
1226   ldrw(scratch2, mask);
1227   ands(scratch, scratch, scratch2);
1228   br(cond, *where);
1229 }
1230 
1231 void InterpreterMacroAssembler::call_VM_leaf_base(address entry_point,
1232                                                   int number_of_arguments) {
1233   // interpreter specific
1234   //
1235   // Note: No need to save/restore rbcp & rlocals pointer since these
1236   //       are callee saved registers and no blocking/ GC can happen
1237   //       in leaf calls.
1238 #ifdef ASSERT
1239   {
1240     Label L;
1241     ldr(rscratch1, Address(rfp, frame::interpreter_frame_last_sp_offset * wordSize));
1242     cbz(rscratch1, L);
1243     stop("InterpreterMacroAssembler::call_VM_leaf_base:"
1244          " last_sp != nullptr");
1245     bind(L);
1246   }
1247 #endif /* ASSERT */
1248   // super call
1249   MacroAssembler::call_VM_leaf_base(entry_point, number_of_arguments);
1250 }
1251 
1252 void InterpreterMacroAssembler::call_VM_base(Register oop_result,
1253                                              Register java_thread,
1254                                              Register last_java_sp,
1255                                              Label*   return_pc,
1256                                              address  entry_point,
1257                                              int      number_of_arguments,
1258                                              bool     check_exceptions) {
1259   // interpreter specific
1260   //
1261   // Note: Could avoid restoring locals ptr (callee saved) - however doesn't
1262   //       really make a difference for these runtime calls, since they are
1263   //       slow anyway. Btw., bcp must be saved/restored since it may change
1264   //       due to GC.
1265   // assert(java_thread == noreg , "not expecting a precomputed java thread");
1266   save_bcp();
1267 #ifdef ASSERT
1268   {
1269     Label L;
1270     ldr(rscratch1, Address(rfp, frame::interpreter_frame_last_sp_offset * wordSize));
1271     cbz(rscratch1, L);
1272     stop("InterpreterMacroAssembler::call_VM_base:"
1273          " last_sp != nullptr");
1274     bind(L);
1275   }
1276 #endif /* ASSERT */
1277   // super call
1278   MacroAssembler::call_VM_base(oop_result, noreg, last_java_sp,
1279                                return_pc, entry_point,
1280                                number_of_arguments, check_exceptions);
1281 // interpreter specific
1282   restore_bcp();
1283   restore_locals();
1284 }
1285 
1286 void InterpreterMacroAssembler::call_VM_preemptable_helper(Register oop_result,
1287                                                            address entry_point,
1288                                                            int number_of_arguments,
1289                                                            bool check_exceptions) {
1290   assert(InterpreterRuntime::is_preemptable_call(entry_point), "VM call not preemptable, should use call_VM()");
1291   Label resume_pc, not_preempted;
1292 
1293 #ifdef ASSERT
1294   {
1295     Label L1, L2;
1296     ldr(rscratch1, Address(rthread, JavaThread::preempt_alternate_return_offset()));
1297     cbz(rscratch1, L1);
1298     stop("call_VM_preemptable_helper: Should not have alternate return address set");
1299     bind(L1);
1300     // We check this counter in patch_return_pc_with_preempt_stub() during freeze.
1301     incrementw(Address(rthread, JavaThread::interp_at_preemptable_vmcall_cnt_offset()));
1302     ldrw(rscratch1, Address(rthread, JavaThread::interp_at_preemptable_vmcall_cnt_offset()));
1303     cmpw(rscratch1, 0);
1304     br(Assembler::GT, L2);
1305     stop("call_VM_preemptable_helper: should be > 0");
1306     bind(L2);
1307   }
1308 #endif /* ASSERT */
1309 
1310   // Force freeze slow path.
1311   push_cont_fastpath();
1312 
1313   // Make VM call. In case of preemption set last_pc to the one we want to resume to.
1314   // Note: call_VM_base will use resume_pc label to set last_Java_pc.
1315   call_VM_base(noreg, noreg, noreg, &resume_pc, entry_point, number_of_arguments, false /*check_exceptions*/);
1316 
1317   pop_cont_fastpath();
1318 
1319 #ifdef ASSERT
1320   {
1321     Label L;
1322     decrementw(Address(rthread, JavaThread::interp_at_preemptable_vmcall_cnt_offset()));
1323     ldrw(rscratch1, Address(rthread, JavaThread::interp_at_preemptable_vmcall_cnt_offset()));
1324     cmpw(rscratch1, 0);
1325     br(Assembler::GE, L);
1326     stop("call_VM_preemptable_helper: should be >= 0");
1327     bind(L);
1328   }
1329 #endif /* ASSERT */
1330 
1331   // Check if preempted.
1332   ldr(rscratch1, Address(rthread, JavaThread::preempt_alternate_return_offset()));
1333   cbz(rscratch1, not_preempted);
1334   str(zr, Address(rthread, JavaThread::preempt_alternate_return_offset()));
1335   br(rscratch1);
1336 
1337   // In case of preemption, this is where we will resume once we finally acquire the monitor.
1338   bind(resume_pc);
1339   restore_after_resume(false /* is_native */);
1340 
1341   bind(not_preempted);
1342   if (check_exceptions) {
1343     // check for pending exceptions
1344     ldr(rscratch1, Address(rthread, in_bytes(Thread::pending_exception_offset())));
1345     Label ok;
1346     cbz(rscratch1, ok);
1347     lea(rscratch1, RuntimeAddress(StubRoutines::forward_exception_entry()));
1348     br(rscratch1);
1349     bind(ok);
1350   }
1351 
1352   // get oop result if there is one and reset the value in the thread
1353   if (oop_result->is_valid()) {
1354     get_vm_result_oop(oop_result, rthread);
1355   }
1356 }
1357 
1358 static void pass_arg1(MacroAssembler* masm, Register arg) {
1359   if (c_rarg1 != arg ) {
1360     masm->mov(c_rarg1, arg);
1361   }
1362 }
1363 
1364 static void pass_arg2(MacroAssembler* masm, Register arg) {
1365   if (c_rarg2 != arg ) {
1366     masm->mov(c_rarg2, arg);
1367   }
1368 }
1369 
1370 void InterpreterMacroAssembler::call_VM_preemptable(Register oop_result,
1371                                          address entry_point,
1372                                          Register arg_1,
1373                                          bool check_exceptions) {
1374   pass_arg1(this, arg_1);
1375   call_VM_preemptable_helper(oop_result, entry_point, 1, check_exceptions);
1376 }
1377 
1378 void InterpreterMacroAssembler::call_VM_preemptable(Register oop_result,
1379                                          address entry_point,
1380                                          Register arg_1,
1381                                          Register arg_2,
1382                                          bool check_exceptions) {
1383   LP64_ONLY(assert_different_registers(arg_1, c_rarg2));
1384   pass_arg2(this, arg_2);
1385   pass_arg1(this, arg_1);
1386   call_VM_preemptable_helper(oop_result, entry_point, 2, check_exceptions);
1387 }
1388 
1389 void InterpreterMacroAssembler::restore_after_resume(bool is_native) {
1390   lea(rscratch1, ExternalAddress(Interpreter::cont_resume_interpreter_adapter()));
1391   blr(rscratch1);
1392   if (is_native) {
1393     // On resume we need to set up stack as expected
1394     push(dtos);
1395     push(ltos);
1396   }
1397 }
1398 
1399 void InterpreterMacroAssembler::profile_obj_type(Register obj, const Address& mdo_addr) {
1400   assert_different_registers(obj, rscratch1, mdo_addr.base(), mdo_addr.index());
1401   Label update, next, none;
1402 
1403   verify_oop(obj);
1404 
1405   cbnz(obj, update);
1406   orptr(mdo_addr, TypeEntries::null_seen);
1407   b(next);
1408 
1409   bind(update);
1410   load_klass(obj, obj);
1411 
1412   ldr(rscratch1, mdo_addr);
1413   eor(obj, obj, rscratch1);
1414   tst(obj, TypeEntries::type_klass_mask);
1415   br(Assembler::EQ, next); // klass seen before, nothing to
1416                            // do. The unknown bit may have been
1417                            // set already but no need to check.
1418 
1419   tbnz(obj, exact_log2(TypeEntries::type_unknown), next);
1420   // already unknown. Nothing to do anymore.
1421 
1422   cbz(rscratch1, none);
1423   cmp(rscratch1, (u1)TypeEntries::null_seen);
1424   br(Assembler::EQ, none);
1425   // There is a chance that the checks above
1426   // fail if another thread has just set the
1427   // profiling to this obj's klass
1428   eor(obj, obj, rscratch1); // get back original value before XOR
1429   ldr(rscratch1, mdo_addr);
1430   eor(obj, obj, rscratch1);
1431   tst(obj, TypeEntries::type_klass_mask);
1432   br(Assembler::EQ, next);
1433 
1434   // different than before. Cannot keep accurate profile.
1435   orptr(mdo_addr, TypeEntries::type_unknown);
1436   b(next);
1437 
1438   bind(none);
1439   // first time here. Set profile type.
1440   str(obj, mdo_addr);
1441 #ifdef ASSERT
1442   andr(obj, obj, TypeEntries::type_mask);
1443   verify_klass_ptr(obj);
1444 #endif
1445 
1446   bind(next);
1447 }
1448 
1449 void InterpreterMacroAssembler::profile_arguments_type(Register mdp, Register callee, Register tmp, bool is_virtual) {
1450   if (!ProfileInterpreter) {
1451     return;
1452   }
1453 
1454   if (MethodData::profile_arguments() || MethodData::profile_return()) {
1455     Label profile_continue;
1456 
1457     test_method_data_pointer(mdp, profile_continue);
1458 
1459     int off_to_start = is_virtual ? in_bytes(VirtualCallData::virtual_call_data_size()) : in_bytes(CounterData::counter_data_size());
1460 
1461     ldrb(rscratch1, Address(mdp, in_bytes(DataLayout::tag_offset()) - off_to_start));
1462     cmp(rscratch1, u1(is_virtual ? DataLayout::virtual_call_type_data_tag : DataLayout::call_type_data_tag));
1463     br(Assembler::NE, profile_continue);
1464 
1465     if (MethodData::profile_arguments()) {
1466       Label done;
1467       int off_to_args = in_bytes(TypeEntriesAtCall::args_data_offset());
1468 
1469       for (int i = 0; i < TypeProfileArgsLimit; i++) {
1470         if (i > 0 || MethodData::profile_return()) {
1471           // If return value type is profiled we may have no argument to profile
1472           ldr(tmp, Address(mdp, in_bytes(TypeEntriesAtCall::cell_count_offset())));
1473           sub(tmp, tmp, i*TypeStackSlotEntries::per_arg_count());
1474           cmp(tmp, (u1)TypeStackSlotEntries::per_arg_count());
1475           add(rscratch1, mdp, off_to_args);
1476           br(Assembler::LT, done);
1477         }
1478         ldr(tmp, Address(callee, Method::const_offset()));
1479         load_unsigned_short(tmp, Address(tmp, ConstMethod::size_of_parameters_offset()));
1480         // stack offset o (zero based) from the start of the argument
1481         // list, for n arguments translates into offset n - o - 1 from
1482         // the end of the argument list
1483         ldr(rscratch1, Address(mdp, in_bytes(TypeEntriesAtCall::stack_slot_offset(i))));
1484         sub(tmp, tmp, rscratch1);
1485         sub(tmp, tmp, 1);
1486         Address arg_addr = argument_address(tmp);
1487         ldr(tmp, arg_addr);
1488 
1489         Address mdo_arg_addr(mdp, in_bytes(TypeEntriesAtCall::argument_type_offset(i)));
1490         profile_obj_type(tmp, mdo_arg_addr);
1491 
1492         int to_add = in_bytes(TypeStackSlotEntries::per_arg_size());
1493         off_to_args += to_add;
1494       }
1495 
1496       if (MethodData::profile_return()) {
1497         ldr(tmp, Address(mdp, in_bytes(TypeEntriesAtCall::cell_count_offset())));
1498         sub(tmp, tmp, TypeProfileArgsLimit*TypeStackSlotEntries::per_arg_count());
1499       }
1500 
1501       add(rscratch1, mdp, off_to_args);
1502       bind(done);
1503       mov(mdp, rscratch1);
1504 
1505       if (MethodData::profile_return()) {
1506         // We're right after the type profile for the last
1507         // argument. tmp is the number of cells left in the
1508         // CallTypeData/VirtualCallTypeData to reach its end. Non null
1509         // if there's a return to profile.
1510         assert(ReturnTypeEntry::static_cell_count() < TypeStackSlotEntries::per_arg_count(), "can't move past ret type");
1511         add(mdp, mdp, tmp, LSL, exact_log2(DataLayout::cell_size));
1512       }
1513       str(mdp, Address(rfp, frame::interpreter_frame_mdp_offset * wordSize));
1514     } else {
1515       assert(MethodData::profile_return(), "either profile call args or call ret");
1516       update_mdp_by_constant(mdp, in_bytes(TypeEntriesAtCall::return_only_size()));
1517     }
1518 
1519     // mdp points right after the end of the
1520     // CallTypeData/VirtualCallTypeData, right after the cells for the
1521     // return value type if there's one
1522 
1523     bind(profile_continue);
1524   }
1525 }
1526 
1527 void InterpreterMacroAssembler::profile_return_type(Register mdp, Register ret, Register tmp) {
1528   assert_different_registers(mdp, ret, tmp, rbcp);
1529   if (ProfileInterpreter && MethodData::profile_return()) {
1530     Label profile_continue, done;
1531 
1532     test_method_data_pointer(mdp, profile_continue);
1533 
1534     if (MethodData::profile_return_jsr292_only()) {
1535       assert(Method::intrinsic_id_size_in_bytes() == 2, "assuming Method::_intrinsic_id is u2");
1536 
1537       // If we don't profile all invoke bytecodes we must make sure
1538       // it's a bytecode we indeed profile. We can't go back to the
1539       // beginning of the ProfileData we intend to update to check its
1540       // type because we're right after it and we don't known its
1541       // length
1542       Label do_profile;
1543       ldrb(rscratch1, Address(rbcp, 0));
1544       cmp(rscratch1, (u1)Bytecodes::_invokedynamic);
1545       br(Assembler::EQ, do_profile);
1546       cmp(rscratch1, (u1)Bytecodes::_invokehandle);
1547       br(Assembler::EQ, do_profile);
1548       get_method(tmp);
1549       ldrh(rscratch1, Address(tmp, Method::intrinsic_id_offset()));
1550       subs(zr, rscratch1, static_cast<int>(vmIntrinsics::_compiledLambdaForm));
1551       br(Assembler::NE, profile_continue);
1552 
1553       bind(do_profile);
1554     }
1555 
1556     Address mdo_ret_addr(mdp, -in_bytes(ReturnTypeEntry::size()));
1557     mov(tmp, ret);
1558     profile_obj_type(tmp, mdo_ret_addr);
1559 
1560     bind(profile_continue);
1561   }
1562 }
1563 
1564 void InterpreterMacroAssembler::profile_parameters_type(Register mdp, Register tmp1, Register tmp2) {
1565   assert_different_registers(rscratch1, rscratch2, mdp, tmp1, tmp2);
1566   if (ProfileInterpreter && MethodData::profile_parameters()) {
1567     Label profile_continue, done;
1568 
1569     test_method_data_pointer(mdp, profile_continue);
1570 
1571     // Load the offset of the area within the MDO used for
1572     // parameters. If it's negative we're not profiling any parameters
1573     ldrw(tmp1, Address(mdp, in_bytes(MethodData::parameters_type_data_di_offset()) - in_bytes(MethodData::data_offset())));
1574     tbnz(tmp1, 31, profile_continue);  // i.e. sign bit set
1575 
1576     // Compute a pointer to the area for parameters from the offset
1577     // and move the pointer to the slot for the last
1578     // parameters. Collect profiling from last parameter down.
1579     // mdo start + parameters offset + array length - 1
1580     add(mdp, mdp, tmp1);
1581     ldr(tmp1, Address(mdp, ArrayData::array_len_offset()));
1582     sub(tmp1, tmp1, TypeStackSlotEntries::per_arg_count());
1583 
1584     Label loop;
1585     bind(loop);
1586 
1587     int off_base = in_bytes(ParametersTypeData::stack_slot_offset(0));
1588     int type_base = in_bytes(ParametersTypeData::type_offset(0));
1589     int per_arg_scale = exact_log2(DataLayout::cell_size);
1590     add(rscratch1, mdp, off_base);
1591     add(rscratch2, mdp, type_base);
1592 
1593     Address arg_off(rscratch1, tmp1, Address::lsl(per_arg_scale));
1594     Address arg_type(rscratch2, tmp1, Address::lsl(per_arg_scale));
1595 
1596     // load offset on the stack from the slot for this parameter
1597     ldr(tmp2, arg_off);
1598     neg(tmp2, tmp2);
1599     // read the parameter from the local area
1600     ldr(tmp2, Address(rlocals, tmp2, Address::lsl(Interpreter::logStackElementSize)));
1601 
1602     // profile the parameter
1603     profile_obj_type(tmp2, arg_type);
1604 
1605     // go to next parameter
1606     subs(tmp1, tmp1, TypeStackSlotEntries::per_arg_count());
1607     br(Assembler::GE, loop);
1608 
1609     bind(profile_continue);
1610   }
1611 }
1612 
1613 void InterpreterMacroAssembler::load_resolved_indy_entry(Register cache, Register index) {
1614   // Get index out of bytecode pointer, get_cache_entry_pointer_at_bcp
1615   get_cache_index_at_bcp(index, 1, sizeof(u4));
1616   // Get address of invokedynamic array
1617   ldr(cache, Address(rcpool, in_bytes(ConstantPoolCache::invokedynamic_entries_offset())));
1618   // Scale the index to be the entry index * sizeof(ResolvedIndyEntry)
1619   lsl(index, index, log2i_exact(sizeof(ResolvedIndyEntry)));
1620   add(cache, cache, Array<ResolvedIndyEntry>::base_offset_in_bytes());
1621   lea(cache, Address(cache, index));
1622 }
1623 
1624 void InterpreterMacroAssembler::load_field_entry(Register cache, Register index, int bcp_offset) {
1625   // Get index out of bytecode pointer
1626   get_cache_index_at_bcp(index, bcp_offset, sizeof(u2));
1627   // Take shortcut if the size is a power of 2
1628   if (is_power_of_2(sizeof(ResolvedFieldEntry))) {
1629     lsl(index, index, log2i_exact(sizeof(ResolvedFieldEntry))); // Scale index by power of 2
1630   } else {
1631     mov(cache, sizeof(ResolvedFieldEntry));
1632     mul(index, index, cache); // Scale the index to be the entry index * sizeof(ResolvedFieldEntry)
1633   }
1634   // Get address of field entries array
1635   ldr(cache, Address(rcpool, ConstantPoolCache::field_entries_offset()));
1636   add(cache, cache, Array<ResolvedFieldEntry>::base_offset_in_bytes());
1637   lea(cache, Address(cache, index));
1638   // Prevents stale data from being read after the bytecode is patched to the fast bytecode
1639   membar(MacroAssembler::LoadLoad);
1640 }
1641 
1642 void InterpreterMacroAssembler::load_method_entry(Register cache, Register index, int bcp_offset) {
1643   // Get index out of bytecode pointer
1644   get_cache_index_at_bcp(index, bcp_offset, sizeof(u2));
1645   mov(cache, sizeof(ResolvedMethodEntry));
1646   mul(index, index, cache); // Scale the index to be the entry index * sizeof(ResolvedMethodEntry)
1647 
1648   // Get address of field entries array
1649   ldr(cache, Address(rcpool, ConstantPoolCache::method_entries_offset()));
1650   add(cache, cache, Array<ResolvedMethodEntry>::base_offset_in_bytes());
1651   lea(cache, Address(cache, index));
1652 }
1653 
1654 #ifdef ASSERT
1655 void InterpreterMacroAssembler::verify_field_offset(Register reg) {
1656   // Verify the field offset is not in the header, implicitly checks for 0
1657   Label L;
1658   subs(zr, reg, oopDesc::base_offset_in_bytes());
1659   br(Assembler::GE, L);
1660   stop("bad field offset");
1661   bind(L);
1662 }
1663 #endif