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/runtimeUpcalls.hpp"
  47 #include "runtime/safepointMechanism.hpp"
  48 #include "runtime/sharedRuntime.hpp"
  49 #include "utilities/powerOfTwo.hpp"
  50 
  51 void InterpreterMacroAssembler::narrow(Register result) {
  52 
  53   // Get method->_constMethod->_result_type
  54   ldr(rscratch1, Address(rfp, frame::interpreter_frame_method_offset * wordSize));
  55   ldr(rscratch1, Address(rscratch1, Method::const_offset()));
  56   ldrb(rscratch1, Address(rscratch1, ConstMethod::result_type_offset()));
  57 
  58   Label done, notBool, notByte, notChar;
  59 
  60   // common case first
  61   cmpw(rscratch1, T_INT);
  62   br(Assembler::EQ, done);
  63 
  64   // mask integer result to narrower return type.
  65   cmpw(rscratch1, T_BOOLEAN);
  66   br(Assembler::NE, notBool);
  67   andw(result, result, 0x1);
  68   b(done);
  69 
  70   bind(notBool);
  71   cmpw(rscratch1, T_BYTE);
  72   br(Assembler::NE, notByte);
  73   sbfx(result, result, 0, 8);
  74   b(done);
  75 
  76   bind(notByte);
  77   cmpw(rscratch1, T_CHAR);
  78   br(Assembler::NE, notChar);
  79   ubfx(result, result, 0, 16);  // truncate upper 16 bits
  80   b(done);
  81 
  82   bind(notChar);
  83   sbfx(result, result, 0, 16);     // sign-extend short
  84 
  85   // Nothing to do for T_INT
  86   bind(done);
  87 }
  88 
  89 void InterpreterMacroAssembler::jump_to_entry(address entry) {
  90   assert(entry, "Entry must have been generated by now");
  91   b(entry);
  92 }
  93 
  94 void InterpreterMacroAssembler::check_and_handle_popframe(Register java_thread) {
  95   if (JvmtiExport::can_pop_frame()) {
  96     Label L;
  97     // Initiate popframe handling only if it is not already being
  98     // processed.  If the flag has the popframe_processing bit set, it
  99     // means that this code is called *during* popframe handling - we
 100     // don't want to reenter.
 101     // This method is only called just after the call into the vm in
 102     // call_VM_base, so the arg registers are available.
 103     ldrw(rscratch1, Address(rthread, JavaThread::popframe_condition_offset()));
 104     tbz(rscratch1, exact_log2(JavaThread::popframe_pending_bit), L);
 105     tbnz(rscratch1, exact_log2(JavaThread::popframe_processing_bit), L);
 106     // Call Interpreter::remove_activation_preserving_args_entry() to get the
 107     // address of the same-named entrypoint in the generated interpreter code.
 108     call_VM_leaf(CAST_FROM_FN_PTR(address, Interpreter::remove_activation_preserving_args_entry));
 109     br(r0);
 110     bind(L);
 111   }
 112 }
 113 
 114 
 115 void InterpreterMacroAssembler::load_earlyret_value(TosState state) {
 116   ldr(r2, Address(rthread, JavaThread::jvmti_thread_state_offset()));
 117   const Address tos_addr(r2, JvmtiThreadState::earlyret_tos_offset());
 118   const Address oop_addr(r2, JvmtiThreadState::earlyret_oop_offset());
 119   const Address val_addr(r2, JvmtiThreadState::earlyret_value_offset());
 120   switch (state) {
 121     case atos: ldr(r0, oop_addr);
 122                str(zr, oop_addr);
 123                interp_verify_oop(r0, state);        break;
 124     case ltos: ldr(r0, val_addr);                   break;
 125     case btos:                                   // fall through
 126     case ztos:                                   // fall through
 127     case ctos:                                   // fall through
 128     case stos:                                   // fall through
 129     case itos: ldrw(r0, val_addr);                  break;
 130     case ftos: ldrs(v0, val_addr);                  break;
 131     case dtos: ldrd(v0, val_addr);                  break;
 132     case vtos: /* nothing to do */                  break;
 133     default  : ShouldNotReachHere();
 134   }
 135   // Clean up tos value in the thread object
 136   movw(rscratch1, (int) ilgl);
 137   strw(rscratch1, tos_addr);
 138   strw(zr, val_addr);
 139 }
 140 
 141 
 142 void InterpreterMacroAssembler::check_and_handle_earlyret(Register java_thread) {
 143   if (JvmtiExport::can_force_early_return()) {
 144     Label L;
 145     ldr(rscratch1, Address(rthread, JavaThread::jvmti_thread_state_offset()));
 146     cbz(rscratch1, L); // if (thread->jvmti_thread_state() == nullptr) exit;
 147 
 148     // Initiate earlyret handling only if it is not already being processed.
 149     // If the flag has the earlyret_processing bit set, it means that this code
 150     // is called *during* earlyret handling - we don't want to reenter.
 151     ldrw(rscratch1, Address(rscratch1, JvmtiThreadState::earlyret_state_offset()));
 152     cmpw(rscratch1, JvmtiThreadState::earlyret_pending);
 153     br(Assembler::NE, L);
 154 
 155     // Call Interpreter::remove_activation_early_entry() to get the address of the
 156     // same-named entrypoint in the generated interpreter code.
 157     ldr(rscratch1, Address(rthread, JavaThread::jvmti_thread_state_offset()));
 158     ldrw(rscratch1, Address(rscratch1, JvmtiThreadState::earlyret_tos_offset()));
 159     call_VM_leaf(CAST_FROM_FN_PTR(address, Interpreter::remove_activation_early_entry), rscratch1);
 160     br(r0);
 161     bind(L);
 162   }
 163 }
 164 
 165 void InterpreterMacroAssembler::get_unsigned_2_byte_index_at_bcp(
 166   Register reg,
 167   int bcp_offset) {
 168   assert(bcp_offset >= 0, "bcp is still pointing to start of bytecode");
 169   ldrh(reg, Address(rbcp, bcp_offset));
 170   rev16(reg, reg);
 171 }
 172 
 173 void InterpreterMacroAssembler::get_dispatch() {
 174   uint64_t offset;
 175   adrp(rdispatch, ExternalAddress((address)Interpreter::dispatch_table()), offset);
 176   // Use add() here after ARDP, rather than lea().
 177   // lea() does not generate anything if its offset is zero.
 178   // However, relocs expect to find either an ADD or a load/store
 179   // insn after an ADRP.  add() always generates an ADD insn, even
 180   // for add(Rn, Rn, 0).
 181   add(rdispatch, rdispatch, offset);
 182 }
 183 
 184 void InterpreterMacroAssembler::get_cache_index_at_bcp(Register index,
 185                                                        int bcp_offset,
 186                                                        size_t index_size) {
 187   assert(bcp_offset > 0, "bcp is still pointing to start of bytecode");
 188   if (index_size == sizeof(u2)) {
 189     load_unsigned_short(index, Address(rbcp, bcp_offset));
 190   } else if (index_size == sizeof(u4)) {
 191     // assert(EnableInvokeDynamic, "giant index used only for JSR 292");
 192     ldrw(index, Address(rbcp, bcp_offset));
 193   } else if (index_size == sizeof(u1)) {
 194     load_unsigned_byte(index, Address(rbcp, bcp_offset));
 195   } else {
 196     ShouldNotReachHere();
 197   }
 198 }
 199 
 200 void InterpreterMacroAssembler::get_method_counters(Register method,
 201                                                     Register mcs, Label& skip) {
 202   Label has_counters;
 203   ldr(mcs, Address(method, Method::method_counters_offset()));
 204   cbnz(mcs, has_counters);
 205   call_VM(noreg, CAST_FROM_FN_PTR(address,
 206           InterpreterRuntime::build_method_counters), method);
 207   ldr(mcs, Address(method, Method::method_counters_offset()));
 208   cbz(mcs, skip); // No MethodCounters allocated, OutOfMemory
 209   bind(has_counters);
 210 }
 211 
 212 // Load object from cpool->resolved_references(index)
 213 void InterpreterMacroAssembler::load_resolved_reference_at_index(
 214                                            Register result, Register index, Register tmp) {
 215   assert_different_registers(result, index);
 216 
 217   get_constant_pool(result);
 218   // load pointer for resolved_references[] objArray
 219   ldr(result, Address(result, ConstantPool::cache_offset()));
 220   ldr(result, Address(result, ConstantPoolCache::resolved_references_offset()));
 221   resolve_oop_handle(result, tmp, rscratch2);
 222   // Add in the index
 223   add(index, index, arrayOopDesc::base_offset_in_bytes(T_OBJECT) >> LogBytesPerHeapOop);
 224   load_heap_oop(result, Address(result, index, Address::uxtw(LogBytesPerHeapOop)), tmp, rscratch2);
 225 }
 226 
 227 void InterpreterMacroAssembler::load_resolved_klass_at_offset(
 228                              Register cpool, Register index, Register klass, Register temp) {
 229   add(temp, cpool, index, LSL, LogBytesPerWord);
 230   ldrh(temp, Address(temp, sizeof(ConstantPool))); // temp = resolved_klass_index
 231   ldr(klass, Address(cpool,  ConstantPool::resolved_klasses_offset())); // klass = cpool->_resolved_klasses
 232   add(klass, klass, temp, LSL, LogBytesPerWord);
 233   ldr(klass, Address(klass, Array<Klass*>::base_offset_in_bytes()));
 234 }
 235 
 236 // Generate a subtype check: branch to ok_is_subtype if sub_klass is a
 237 // subtype of super_klass.
 238 //
 239 // Args:
 240 //      r0: superklass
 241 //      Rsub_klass: subklass
 242 //
 243 // Kills:
 244 //      r2
 245 void InterpreterMacroAssembler::gen_subtype_check(Register Rsub_klass,
 246                                                   Label& ok_is_subtype) {
 247   assert(Rsub_klass != r0, "r0 holds superklass");
 248   assert(Rsub_klass != r2, "r2 holds 2ndary super array length");
 249 
 250   // Profile the not-null value's klass.
 251   profile_typecheck(r2, Rsub_klass); // blows r2
 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 #ifdef ASSERT
 483   Label not_preempted;
 484   ldr(rscratch1, Address(rthread, JavaThread::preempt_alternate_return_offset()));
 485   cbz(rscratch1, not_preempted);
 486   stop("remove_activation: should not have alternate return address set");
 487   bind(not_preempted);
 488 #endif /* ASSERT */
 489 
 490   // get the value of _do_not_unlock_if_synchronized into r3
 491   const Address do_not_unlock_if_synchronized(rthread,
 492     in_bytes(JavaThread::do_not_unlock_if_synchronized_offset()));
 493   ldrb(r3, do_not_unlock_if_synchronized);
 494   strb(zr, do_not_unlock_if_synchronized); // reset the flag
 495 
 496  // get method access flags
 497   ldr(r1, Address(rfp, frame::interpreter_frame_method_offset * wordSize));
 498   ldrh(r2, Address(r1, Method::access_flags_offset()));
 499   tbz(r2, exact_log2(JVM_ACC_SYNCHRONIZED), unlocked);
 500 
 501   // Don't unlock anything if the _do_not_unlock_if_synchronized flag
 502   // is set.
 503   cbnz(r3, no_unlock);
 504 
 505   // unlock monitor
 506   push(state); // save result
 507 
 508   // BasicObjectLock will be first in list, since this is a
 509   // synchronized method. However, need to check that the object has
 510   // not been unlocked by an explicit monitorexit bytecode.
 511   const Address monitor(rfp, frame::interpreter_frame_initial_sp_offset *
 512                         wordSize - (int) sizeof(BasicObjectLock));
 513   // We use c_rarg1 so that if we go slow path it will be the correct
 514   // register for unlock_object to pass to VM directly
 515   lea(c_rarg1, monitor); // address of first monitor
 516 
 517   ldr(r0, Address(c_rarg1, BasicObjectLock::obj_offset()));
 518   cbnz(r0, unlock);
 519 
 520   pop(state);
 521   if (throw_monitor_exception) {
 522     // Entry already unlocked, need to throw exception
 523     call_VM(noreg, CAST_FROM_FN_PTR(address,
 524                    InterpreterRuntime::throw_illegal_monitor_state_exception));
 525     should_not_reach_here();
 526   } else {
 527     // Monitor already unlocked during a stack unroll. If requested,
 528     // install an illegal_monitor_state_exception.  Continue with
 529     // stack unrolling.
 530     if (install_monitor_exception) {
 531       call_VM(noreg, CAST_FROM_FN_PTR(address,
 532                      InterpreterRuntime::new_illegal_monitor_state_exception));
 533     }
 534     b(unlocked);
 535   }
 536 
 537   bind(unlock);
 538   unlock_object(c_rarg1);
 539   pop(state);
 540 
 541   // Check that for block-structured locking (i.e., that all locked
 542   // objects has been unlocked)
 543   bind(unlocked);
 544 
 545   // r0: Might contain return value
 546 
 547   // Check that all monitors are unlocked
 548   {
 549     Label loop, exception, entry, restart;
 550     const int entry_size = frame::interpreter_frame_monitor_size_in_bytes();
 551     const Address monitor_block_top(
 552         rfp, frame::interpreter_frame_monitor_block_top_offset * wordSize);
 553     const Address monitor_block_bot(
 554         rfp, frame::interpreter_frame_initial_sp_offset * wordSize);
 555 
 556     bind(restart);
 557     // We use c_rarg1 so that if we go slow path it will be the correct
 558     // register for unlock_object to pass to VM directly
 559     ldr(c_rarg1, monitor_block_top); // derelativize pointer
 560     lea(c_rarg1, Address(rfp, c_rarg1, Address::lsl(Interpreter::logStackElementSize)));
 561     // c_rarg1 points to current entry, starting with top-most entry
 562 
 563     lea(r19, monitor_block_bot);  // points to word before bottom of
 564                                   // monitor block
 565     b(entry);
 566 
 567     // Entry already locked, need to throw exception
 568     bind(exception);
 569 
 570     if (throw_monitor_exception) {
 571       // Throw exception
 572       MacroAssembler::call_VM(noreg,
 573                               CAST_FROM_FN_PTR(address, InterpreterRuntime::
 574                                    throw_illegal_monitor_state_exception));
 575       should_not_reach_here();
 576     } else {
 577       // Stack unrolling. Unlock object and install illegal_monitor_exception.
 578       // Unlock does not block, so don't have to worry about the frame.
 579       // We don't have to preserve c_rarg1 since we are going to throw an exception.
 580 
 581       push(state);
 582       unlock_object(c_rarg1);
 583       pop(state);
 584 
 585       if (install_monitor_exception) {
 586         call_VM(noreg, CAST_FROM_FN_PTR(address,
 587                                         InterpreterRuntime::
 588                                         new_illegal_monitor_state_exception));
 589       }
 590 
 591       b(restart);
 592     }
 593 
 594     bind(loop);
 595     // check if current entry is used
 596     ldr(rscratch1, Address(c_rarg1, BasicObjectLock::obj_offset()));
 597     cbnz(rscratch1, exception);
 598 
 599     add(c_rarg1, c_rarg1, entry_size); // otherwise advance to next entry
 600     bind(entry);
 601     cmp(c_rarg1, r19); // check if bottom reached
 602     br(Assembler::NE, loop); // if not at bottom then check this entry
 603   }
 604 
 605   bind(no_unlock);
 606 
 607   JFR_ONLY(enter_jfr_critical_section();)
 608 
 609   // The below poll is for the stack watermark barrier. It allows fixing up frames lazily,
 610   // that would normally not be safe to use. Such bad returns into unsafe territory of
 611   // the stack, will call InterpreterRuntime::at_unwind.
 612   Label slow_path;
 613   Label fast_path;
 614   safepoint_poll(slow_path, true /* at_return */, false /* in_nmethod */);
 615   br(Assembler::AL, fast_path);
 616   bind(slow_path);
 617   push(state);
 618   set_last_Java_frame(esp, rfp, pc(), rscratch1);
 619   super_call_VM_leaf(CAST_FROM_FN_PTR(address, InterpreterRuntime::at_unwind), rthread);
 620   reset_last_Java_frame(true);
 621   pop(state);
 622   bind(fast_path);
 623 
 624   // JVMTI support. Make sure the safepoint poll test is issued prior.
 625   if (notify_jvmdi) {
 626     notify_method_exit(state, NotifyJVMTI);    // preserve TOSCA
 627   } else {
 628     notify_method_exit(state, SkipNotifyJVMTI); // preserve TOSCA
 629   }
 630 
 631   // remove activation
 632   // get sender esp
 633   ldr(rscratch2,
 634       Address(rfp, frame::interpreter_frame_sender_sp_offset * wordSize));
 635   if (StackReservedPages > 0) {
 636     // testing if reserved zone needs to be re-enabled
 637     Label no_reserved_zone_enabling;
 638 
 639     // check if already enabled - if so no re-enabling needed
 640     assert(sizeof(StackOverflow::StackGuardState) == 4, "unexpected size");
 641     ldrw(rscratch1, Address(rthread, JavaThread::stack_guard_state_offset()));
 642     cmpw(rscratch1, (u1)StackOverflow::stack_guard_enabled);
 643     br(Assembler::EQ, no_reserved_zone_enabling);
 644 
 645     // look for an overflow into the stack reserved zone, i.e.
 646     // interpreter_frame_sender_sp <= JavaThread::reserved_stack_activation
 647     ldr(rscratch1, Address(rthread, JavaThread::reserved_stack_activation_offset()));
 648     cmp(rscratch2, rscratch1);
 649     br(Assembler::LS, no_reserved_zone_enabling);
 650 
 651     JFR_ONLY(leave_jfr_critical_section();)
 652 
 653     call_VM_leaf(
 654       CAST_FROM_FN_PTR(address, SharedRuntime::enable_stack_reserved_zone), rthread);
 655     call_VM(noreg, CAST_FROM_FN_PTR(address,
 656                    InterpreterRuntime::throw_delayed_StackOverflowError));
 657     should_not_reach_here();
 658 
 659     bind(no_reserved_zone_enabling);
 660   }
 661 
 662   // remove frame anchor
 663   leave();
 664 
 665   JFR_ONLY(leave_jfr_critical_section();)
 666 
 667   // restore sender esp
 668   mov(esp, rscratch2);
 669 
 670   // If we're returning to interpreted code we will shortly be
 671   // adjusting SP to allow some space for ESP.  If we're returning to
 672   // compiled code the saved sender SP was saved in sender_sp, so this
 673   // restores it.
 674   andr(sp, esp, -16);
 675 }
 676 
 677 #if INCLUDE_JFR
 678 void InterpreterMacroAssembler::enter_jfr_critical_section() {
 679   const Address sampling_critical_section(rthread, in_bytes(SAMPLING_CRITICAL_SECTION_OFFSET_JFR));
 680   mov(rscratch1, true);
 681   strb(rscratch1, sampling_critical_section);
 682 }
 683 
 684 void InterpreterMacroAssembler::leave_jfr_critical_section() {
 685   const Address sampling_critical_section(rthread, in_bytes(SAMPLING_CRITICAL_SECTION_OFFSET_JFR));
 686   strb(zr, sampling_critical_section);
 687 }
 688 #endif // INCLUDE_JFR
 689 
 690 // Lock object
 691 //
 692 // Args:
 693 //      c_rarg1: BasicObjectLock to be used for locking
 694 //
 695 // Kills:
 696 //      r0
 697 //      c_rarg0, c_rarg1, c_rarg2, c_rarg3, c_rarg4, .. (param regs)
 698 //      rscratch1, rscratch2 (scratch regs)
 699 void InterpreterMacroAssembler::lock_object(Register lock_reg)
 700 {
 701   assert(lock_reg == c_rarg1, "The argument is only for looks. It must be c_rarg1");
 702 
 703   const Register tmp = c_rarg2;
 704   const Register obj_reg = c_rarg3; // Will contain the oop
 705   const Register tmp2 = c_rarg4;
 706   const Register tmp3 = c_rarg5;
 707 
 708   // Load object pointer into obj_reg %c_rarg3
 709   ldr(obj_reg, Address(lock_reg, BasicObjectLock::obj_offset()));
 710 
 711   Label slow_case, done;
 712   fast_lock(lock_reg, obj_reg, tmp, tmp2, tmp3, slow_case);
 713   b(done);
 714 
 715   bind(slow_case);
 716 
 717   // Call the runtime routine for slow case
 718   call_VM_preemptable(noreg,
 719           CAST_FROM_FN_PTR(address, InterpreterRuntime::monitorenter),
 720           lock_reg);
 721 
 722   bind(done);
 723 }
 724 
 725 
 726 // Unlocks an object. Used in monitorexit bytecode and
 727 // remove_activation.  Throws an IllegalMonitorException if object is
 728 // not locked by current thread.
 729 //
 730 // Args:
 731 //      c_rarg1: BasicObjectLock for lock
 732 //
 733 // Kills:
 734 //      r0
 735 //      c_rarg0, c_rarg1, c_rarg2, c_rarg3, ... (param regs)
 736 //      rscratch1, rscratch2 (scratch regs)
 737 void InterpreterMacroAssembler::unlock_object(Register lock_reg)
 738 {
 739   assert(lock_reg == c_rarg1, "The argument is only for looks. It must be rarg1");
 740 
 741   const Register swap_reg   = r0;
 742   const Register header_reg = c_rarg2;  // Will contain the old oopMark
 743   const Register obj_reg    = c_rarg3;  // Will contain the oop
 744   const Register tmp_reg    = c_rarg4;  // Temporary used by fast_unlock
 745 
 746   save_bcp(); // Save in case of exception
 747 
 748   // Load oop into obj_reg(%c_rarg3)
 749   ldr(obj_reg, Address(lock_reg, BasicObjectLock::obj_offset()));
 750 
 751   // Free entry
 752   str(zr, Address(lock_reg, BasicObjectLock::obj_offset()));
 753 
 754   Label slow_case, done;
 755   fast_unlock(obj_reg, header_reg, swap_reg, tmp_reg, slow_case);
 756   b(done);
 757 
 758   bind(slow_case);
 759   // Call the runtime routine for slow case.
 760   str(obj_reg, Address(lock_reg, BasicObjectLock::obj_offset())); // restore obj
 761   call_VM_leaf(CAST_FROM_FN_PTR(address, InterpreterRuntime::monitorexit), lock_reg);
 762   bind(done);
 763   restore_bcp();
 764 }
 765 
 766 void InterpreterMacroAssembler::test_method_data_pointer(Register mdp,
 767                                                          Label& zero_continue) {
 768   assert(ProfileInterpreter, "must be profiling interpreter");
 769   ldr(mdp, Address(rfp, frame::interpreter_frame_mdp_offset * wordSize));
 770   cbz(mdp, zero_continue);
 771 }
 772 
 773 // Set the method data pointer for the current bcp.
 774 void InterpreterMacroAssembler::set_method_data_pointer_for_bcp() {
 775   assert(ProfileInterpreter, "must be profiling interpreter");
 776   Label set_mdp;
 777   stp(r0, r1, Address(pre(sp, -2 * wordSize)));
 778 
 779   // Test MDO to avoid the call if it is null.
 780   ldr(r0, Address(rmethod, in_bytes(Method::method_data_offset())));
 781   cbz(r0, set_mdp);
 782   call_VM_leaf(CAST_FROM_FN_PTR(address, InterpreterRuntime::bcp_to_di), rmethod, rbcp);
 783   // r0: mdi
 784   // mdo is guaranteed to be non-zero here, we checked for it before the call.
 785   ldr(r1, Address(rmethod, in_bytes(Method::method_data_offset())));
 786   lea(r1, Address(r1, in_bytes(MethodData::data_offset())));
 787   add(r0, r1, r0);
 788   str(r0, Address(rfp, frame::interpreter_frame_mdp_offset * wordSize));
 789   bind(set_mdp);
 790   ldp(r0, r1, Address(post(sp, 2 * wordSize)));
 791 }
 792 
 793 void InterpreterMacroAssembler::verify_method_data_pointer() {
 794   assert(ProfileInterpreter, "must be profiling interpreter");
 795 #ifdef ASSERT
 796   Label verify_continue;
 797   stp(r0, r1, Address(pre(sp, -2 * wordSize)));
 798   stp(r2, r3, Address(pre(sp, -2 * wordSize)));
 799   test_method_data_pointer(r3, verify_continue); // If mdp is zero, continue
 800   get_method(r1);
 801 
 802   // If the mdp is valid, it will point to a DataLayout header which is
 803   // consistent with the bcp.  The converse is highly probable also.
 804   ldrsh(r2, Address(r3, in_bytes(DataLayout::bci_offset())));
 805   ldr(rscratch1, Address(r1, Method::const_offset()));
 806   add(r2, r2, rscratch1, Assembler::LSL);
 807   lea(r2, Address(r2, ConstMethod::codes_offset()));
 808   cmp(r2, rbcp);
 809   br(Assembler::EQ, verify_continue);
 810   // r1: method
 811   // rbcp: bcp // rbcp == 22
 812   // r3: mdp
 813   call_VM_leaf(CAST_FROM_FN_PTR(address, InterpreterRuntime::verify_mdp),
 814                r1, rbcp, r3);
 815   bind(verify_continue);
 816   ldp(r2, r3, Address(post(sp, 2 * wordSize)));
 817   ldp(r0, r1, Address(post(sp, 2 * wordSize)));
 818 #endif // ASSERT
 819 }
 820 
 821 
 822 void InterpreterMacroAssembler::set_mdp_data_at(Register mdp_in,
 823                                                 int constant,
 824                                                 Register value) {
 825   assert(ProfileInterpreter, "must be profiling interpreter");
 826   Address data(mdp_in, constant);
 827   str(value, data);
 828 }
 829 
 830 
 831 void InterpreterMacroAssembler::increment_mdp_data_at(Register mdp_in,
 832                                                       int constant) {
 833   increment_mdp_data_at(mdp_in, noreg, constant);
 834 }
 835 
 836 void InterpreterMacroAssembler::increment_mdp_data_at(Register mdp_in,
 837                                                       Register index,
 838                                                       int constant) {
 839   assert(ProfileInterpreter, "must be profiling interpreter");
 840 
 841   assert_different_registers(rscratch2, rscratch1, mdp_in, index);
 842 
 843   Address addr1(mdp_in, constant);
 844   Address addr2(rscratch2, index, Address::lsl(0));
 845   Address &addr = addr1;
 846   if (index != noreg) {
 847     lea(rscratch2, addr1);
 848     addr = addr2;
 849   }
 850 
 851   increment(addr, DataLayout::counter_increment);
 852 }
 853 
 854 void InterpreterMacroAssembler::set_mdp_flag_at(Register mdp_in,
 855                                                 int flag_byte_constant) {
 856   assert(ProfileInterpreter, "must be profiling interpreter");
 857   int flags_offset = in_bytes(DataLayout::flags_offset());
 858   // Set the flag
 859   ldrb(rscratch1, Address(mdp_in, flags_offset));
 860   orr(rscratch1, rscratch1, flag_byte_constant);
 861   strb(rscratch1, Address(mdp_in, flags_offset));
 862 }
 863 
 864 
 865 void InterpreterMacroAssembler::test_mdp_data_at(Register mdp_in,
 866                                                  int offset,
 867                                                  Register value,
 868                                                  Register test_value_out,
 869                                                  Label& not_equal_continue) {
 870   assert(ProfileInterpreter, "must be profiling interpreter");
 871   if (test_value_out == noreg) {
 872     ldr(rscratch1, Address(mdp_in, offset));
 873     cmp(value, rscratch1);
 874   } else {
 875     // Put the test value into a register, so caller can use it:
 876     ldr(test_value_out, Address(mdp_in, offset));
 877     cmp(value, test_value_out);
 878   }
 879   br(Assembler::NE, not_equal_continue);
 880 }
 881 
 882 
 883 void InterpreterMacroAssembler::update_mdp_by_offset(Register mdp_in,
 884                                                      int offset_of_disp) {
 885   assert(ProfileInterpreter, "must be profiling interpreter");
 886   ldr(rscratch1, Address(mdp_in, offset_of_disp));
 887   add(mdp_in, mdp_in, rscratch1, LSL);
 888   str(mdp_in, Address(rfp, frame::interpreter_frame_mdp_offset * wordSize));
 889 }
 890 
 891 
 892 void InterpreterMacroAssembler::update_mdp_by_offset(Register mdp_in,
 893                                                      Register reg,
 894                                                      int offset_of_disp) {
 895   assert(ProfileInterpreter, "must be profiling interpreter");
 896   lea(rscratch1, Address(mdp_in, offset_of_disp));
 897   ldr(rscratch1, Address(rscratch1, reg, Address::lsl(0)));
 898   add(mdp_in, mdp_in, rscratch1, LSL);
 899   str(mdp_in, Address(rfp, frame::interpreter_frame_mdp_offset * wordSize));
 900 }
 901 
 902 
 903 void InterpreterMacroAssembler::update_mdp_by_constant(Register mdp_in,
 904                                                        int constant) {
 905   assert(ProfileInterpreter, "must be profiling interpreter");
 906   add(mdp_in, mdp_in, (unsigned)constant);
 907   str(mdp_in, Address(rfp, frame::interpreter_frame_mdp_offset * wordSize));
 908 }
 909 
 910 
 911 void InterpreterMacroAssembler::update_mdp_for_ret(Register return_bci) {
 912   assert(ProfileInterpreter, "must be profiling interpreter");
 913   // save/restore across call_VM
 914   stp(zr, return_bci, Address(pre(sp, -2 * wordSize)));
 915   call_VM(noreg,
 916           CAST_FROM_FN_PTR(address, InterpreterRuntime::update_mdp_for_ret),
 917           return_bci);
 918   ldp(zr, return_bci, Address(post(sp, 2 * wordSize)));
 919 }
 920 
 921 
 922 void InterpreterMacroAssembler::profile_taken_branch(Register mdp) {
 923   if (ProfileInterpreter) {
 924     Label profile_continue;
 925 
 926     // If no method data exists, go to profile_continue.
 927     test_method_data_pointer(mdp, profile_continue);
 928 
 929     // We are taking a branch.  Increment the taken count.
 930     increment_mdp_data_at(mdp, in_bytes(JumpData::taken_offset()));
 931 
 932     // The method data pointer needs to be updated to reflect the new target.
 933     update_mdp_by_offset(mdp, in_bytes(JumpData::displacement_offset()));
 934     bind(profile_continue);
 935   }
 936 }
 937 
 938 
 939 void InterpreterMacroAssembler::profile_not_taken_branch(Register mdp) {
 940   if (ProfileInterpreter) {
 941     Label profile_continue;
 942 
 943     // If no method data exists, go to profile_continue.
 944     test_method_data_pointer(mdp, profile_continue);
 945 
 946     // We are not taking a branch.  Increment the not taken count.
 947     increment_mdp_data_at(mdp, in_bytes(BranchData::not_taken_offset()));
 948 
 949     // The method data pointer needs to be updated to correspond to
 950     // the next bytecode
 951     update_mdp_by_constant(mdp, in_bytes(BranchData::branch_data_size()));
 952     bind(profile_continue);
 953   }
 954 }
 955 
 956 
 957 void InterpreterMacroAssembler::profile_call(Register mdp) {
 958   if (ProfileInterpreter) {
 959     Label profile_continue;
 960 
 961     // If no method data exists, go to profile_continue.
 962     test_method_data_pointer(mdp, profile_continue);
 963 
 964     // We are making a call.  Increment the count.
 965     increment_mdp_data_at(mdp, in_bytes(CounterData::count_offset()));
 966 
 967     // The method data pointer needs to be updated to reflect the new target.
 968     update_mdp_by_constant(mdp, in_bytes(CounterData::counter_data_size()));
 969     bind(profile_continue);
 970   }
 971 }
 972 
 973 void InterpreterMacroAssembler::profile_final_call(Register mdp) {
 974   if (ProfileInterpreter) {
 975     Label profile_continue;
 976 
 977     // If no method data exists, go to profile_continue.
 978     test_method_data_pointer(mdp, profile_continue);
 979 
 980     // We are making a call.  Increment the count.
 981     increment_mdp_data_at(mdp, in_bytes(CounterData::count_offset()));
 982 
 983     // The method data pointer needs to be updated to reflect the new target.
 984     update_mdp_by_constant(mdp,
 985                            in_bytes(VirtualCallData::
 986                                     virtual_call_data_size()));
 987     bind(profile_continue);
 988   }
 989 }
 990 
 991 
 992 void InterpreterMacroAssembler::profile_virtual_call(Register receiver,
 993                                                      Register mdp,
 994                                                      bool receiver_can_be_null) {
 995   if (ProfileInterpreter) {
 996     Label profile_continue;
 997 
 998     // If no method data exists, go to profile_continue.
 999     test_method_data_pointer(mdp, profile_continue);
1000 
1001     Label skip_receiver_profile;
1002     if (receiver_can_be_null) {
1003       Label not_null;
1004       // We are making a call.  Increment the count for null receiver.
1005       increment_mdp_data_at(mdp, in_bytes(CounterData::count_offset()));
1006       b(skip_receiver_profile);
1007       bind(not_null);
1008     }
1009 
1010     // Record the receiver type.
1011     profile_receiver_type(receiver, mdp, 0);
1012     bind(skip_receiver_profile);
1013 
1014     // The method data pointer needs to be updated to reflect the new target.
1015     update_mdp_by_constant(mdp, in_bytes(VirtualCallData::virtual_call_data_size()));
1016     bind(profile_continue);
1017   }
1018 }
1019 
1020 void InterpreterMacroAssembler::profile_ret(Register return_bci,
1021                                             Register mdp) {
1022   if (ProfileInterpreter) {
1023     Label profile_continue;
1024     uint row;
1025 
1026     // If no method data exists, go to profile_continue.
1027     test_method_data_pointer(mdp, profile_continue);
1028 
1029     // Update the total ret count.
1030     increment_mdp_data_at(mdp, in_bytes(CounterData::count_offset()));
1031 
1032     for (row = 0; row < RetData::row_limit(); row++) {
1033       Label next_test;
1034 
1035       // See if return_bci is equal to bci[n]:
1036       test_mdp_data_at(mdp,
1037                        in_bytes(RetData::bci_offset(row)),
1038                        return_bci, noreg,
1039                        next_test);
1040 
1041       // return_bci is equal to bci[n].  Increment the count.
1042       increment_mdp_data_at(mdp, in_bytes(RetData::bci_count_offset(row)));
1043 
1044       // The method data pointer needs to be updated to reflect the new target.
1045       update_mdp_by_offset(mdp,
1046                            in_bytes(RetData::bci_displacement_offset(row)));
1047       b(profile_continue);
1048       bind(next_test);
1049     }
1050 
1051     update_mdp_for_ret(return_bci);
1052 
1053     bind(profile_continue);
1054   }
1055 }
1056 
1057 void InterpreterMacroAssembler::profile_null_seen(Register mdp) {
1058   if (ProfileInterpreter) {
1059     Label profile_continue;
1060 
1061     // If no method data exists, go to profile_continue.
1062     test_method_data_pointer(mdp, profile_continue);
1063 
1064     set_mdp_flag_at(mdp, BitData::null_seen_byte_constant());
1065 
1066     // The method data pointer needs to be updated.
1067     int mdp_delta = in_bytes(BitData::bit_data_size());
1068     if (TypeProfileCasts) {
1069       mdp_delta = in_bytes(VirtualCallData::virtual_call_data_size());
1070     }
1071     update_mdp_by_constant(mdp, mdp_delta);
1072 
1073     bind(profile_continue);
1074   }
1075 }
1076 
1077 void InterpreterMacroAssembler::profile_typecheck(Register mdp, Register klass) {
1078   if (ProfileInterpreter) {
1079     Label profile_continue;
1080 
1081     // If no method data exists, go to profile_continue.
1082     test_method_data_pointer(mdp, profile_continue);
1083 
1084     // The method data pointer needs to be updated.
1085     int mdp_delta = in_bytes(BitData::bit_data_size());
1086     if (TypeProfileCasts) {
1087       mdp_delta = in_bytes(VirtualCallData::virtual_call_data_size());
1088 
1089       // Record the object type.
1090       profile_receiver_type(klass, mdp, 0);
1091     }
1092     update_mdp_by_constant(mdp, mdp_delta);
1093 
1094     bind(profile_continue);
1095   }
1096 }
1097 
1098 void InterpreterMacroAssembler::profile_switch_default(Register mdp) {
1099   if (ProfileInterpreter) {
1100     Label profile_continue;
1101 
1102     // If no method data exists, go to profile_continue.
1103     test_method_data_pointer(mdp, profile_continue);
1104 
1105     // Update the default case count
1106     increment_mdp_data_at(mdp,
1107                           in_bytes(MultiBranchData::default_count_offset()));
1108 
1109     // The method data pointer needs to be updated.
1110     update_mdp_by_offset(mdp,
1111                          in_bytes(MultiBranchData::
1112                                   default_displacement_offset()));
1113 
1114     bind(profile_continue);
1115   }
1116 }
1117 
1118 void InterpreterMacroAssembler::profile_switch_case(Register index,
1119                                                     Register mdp,
1120                                                     Register reg2) {
1121   if (ProfileInterpreter) {
1122     Label profile_continue;
1123 
1124     // If no method data exists, go to profile_continue.
1125     test_method_data_pointer(mdp, profile_continue);
1126 
1127     // Build the base (index * per_case_size_in_bytes()) +
1128     // case_array_offset_in_bytes()
1129     movw(reg2, in_bytes(MultiBranchData::per_case_size()));
1130     movw(rscratch1, in_bytes(MultiBranchData::case_array_offset()));
1131     Assembler::maddw(index, index, reg2, rscratch1);
1132 
1133     // Update the case count
1134     increment_mdp_data_at(mdp,
1135                           index,
1136                           in_bytes(MultiBranchData::relative_count_offset()));
1137 
1138     // The method data pointer needs to be updated.
1139     update_mdp_by_offset(mdp,
1140                          index,
1141                          in_bytes(MultiBranchData::
1142                                   relative_displacement_offset()));
1143 
1144     bind(profile_continue);
1145   }
1146 }
1147 
1148 void InterpreterMacroAssembler::_interp_verify_oop(Register reg, TosState state, const char* file, int line) {
1149   if (state == atos) {
1150     MacroAssembler::_verify_oop_checked(reg, "broken oop", file, line);
1151   }
1152 }
1153 
1154 void InterpreterMacroAssembler::generate_runtime_upcalls_on_method_entry()
1155 {
1156   address upcall = RuntimeUpcalls::on_method_entry_upcall_address();
1157   if (RuntimeUpcalls::does_upcall_need_method_parameter(upcall)) {
1158     get_method(c_rarg1);
1159     call_VM(noreg,upcall, c_rarg1);
1160   } else {
1161     call_VM(noreg,upcall);
1162   }
1163 }
1164 
1165 void InterpreterMacroAssembler::notify_method_entry() {
1166   // Whenever JVMTI is interp_only_mode, method entry/exit events are sent to
1167   // track stack depth.  If it is possible to enter interp_only_mode we add
1168   // the code to check if the event should be sent.
1169   if (JvmtiExport::can_post_interpreter_events()) {
1170     Label L;
1171     ldrw(r3, Address(rthread, JavaThread::interp_only_mode_offset()));
1172     cbzw(r3, L);
1173     call_VM(noreg, CAST_FROM_FN_PTR(address,
1174                                     InterpreterRuntime::post_method_entry));
1175     bind(L);
1176   }
1177 
1178   if (DTraceMethodProbes) {
1179     get_method(c_rarg1);
1180     call_VM_leaf(CAST_FROM_FN_PTR(address, SharedRuntime::dtrace_method_entry),
1181                  rthread, c_rarg1);
1182   }
1183 
1184   // RedefineClasses() tracing support for obsolete method entry
1185   if (log_is_enabled(Trace, redefine, class, obsolete) ||
1186       log_is_enabled(Trace, interpreter, bytecode)) {
1187     get_method(c_rarg1);
1188     call_VM_leaf(
1189       CAST_FROM_FN_PTR(address, SharedRuntime::rc_trace_method_entry),
1190       rthread, c_rarg1);
1191   }
1192 
1193  }
1194 
1195 
1196 void InterpreterMacroAssembler::notify_method_exit(
1197     TosState state, NotifyMethodExitMode mode) {
1198   // Whenever JVMTI is interp_only_mode, method entry/exit events are sent to
1199   // track stack depth.  If it is possible to enter interp_only_mode we add
1200   // the code to check if the event should be sent.
1201   if (mode == NotifyJVMTI && JvmtiExport::can_post_interpreter_events()) {
1202     Label L;
1203     // Note: frame::interpreter_frame_result has a dependency on how the
1204     // method result is saved across the call to post_method_exit. If this
1205     // is changed then the interpreter_frame_result implementation will
1206     // need to be updated too.
1207 
1208     // template interpreter will leave the result on the top of the stack.
1209     push(state);
1210     ldrw(r3, Address(rthread, JavaThread::interp_only_mode_offset()));
1211     cbz(r3, L);
1212     call_VM(noreg,
1213             CAST_FROM_FN_PTR(address, InterpreterRuntime::post_method_exit));
1214     bind(L);
1215     pop(state);
1216   }
1217 
1218   if (DTraceMethodProbes) {
1219     push(state);
1220     get_method(c_rarg1);
1221     call_VM_leaf(CAST_FROM_FN_PTR(address, SharedRuntime::dtrace_method_exit),
1222                  rthread, c_rarg1);
1223     pop(state);
1224   }
1225 }
1226 
1227 
1228 // Jump if ((*counter_addr += increment) & mask) satisfies the condition.
1229 void InterpreterMacroAssembler::increment_mask_and_jump(Address counter_addr,
1230                                                         int increment, Address mask,
1231                                                         Register scratch, Register scratch2,
1232                                                         bool preloaded, Condition cond,
1233                                                         Label* where) {
1234   if (!preloaded) {
1235     ldrw(scratch, counter_addr);
1236   }
1237   add(scratch, scratch, increment);
1238   strw(scratch, counter_addr);
1239   ldrw(scratch2, mask);
1240   ands(scratch, scratch, scratch2);
1241   br(cond, *where);
1242 }
1243 
1244 void InterpreterMacroAssembler::call_VM_leaf_base(address entry_point,
1245                                                   int number_of_arguments) {
1246   // interpreter specific
1247   //
1248   // Note: No need to save/restore rbcp & rlocals pointer since these
1249   //       are callee saved registers and no blocking/ GC can happen
1250   //       in leaf calls.
1251 #ifdef ASSERT
1252   {
1253     Label L;
1254     ldr(rscratch1, Address(rfp, frame::interpreter_frame_last_sp_offset * wordSize));
1255     cbz(rscratch1, L);
1256     stop("InterpreterMacroAssembler::call_VM_leaf_base:"
1257          " last_sp != nullptr");
1258     bind(L);
1259   }
1260 #endif /* ASSERT */
1261   // super call
1262   MacroAssembler::call_VM_leaf_base(entry_point, number_of_arguments);
1263 }
1264 
1265 void InterpreterMacroAssembler::call_VM_base(Register oop_result,
1266                                              Register java_thread,
1267                                              Register last_java_sp,
1268                                              Label*   return_pc,
1269                                              address  entry_point,
1270                                              int      number_of_arguments,
1271                                              bool     check_exceptions) {
1272   // interpreter specific
1273   //
1274   // Note: Could avoid restoring locals ptr (callee saved) - however doesn't
1275   //       really make a difference for these runtime calls, since they are
1276   //       slow anyway. Btw., bcp must be saved/restored since it may change
1277   //       due to GC.
1278   // assert(java_thread == noreg , "not expecting a precomputed java thread");
1279   save_bcp();
1280 #ifdef ASSERT
1281   {
1282     Label L;
1283     ldr(rscratch1, Address(rfp, frame::interpreter_frame_last_sp_offset * wordSize));
1284     cbz(rscratch1, L);
1285     stop("InterpreterMacroAssembler::call_VM_base:"
1286          " last_sp != nullptr");
1287     bind(L);
1288   }
1289 #endif /* ASSERT */
1290   // super call
1291   MacroAssembler::call_VM_base(oop_result, noreg, last_java_sp,
1292                                return_pc, entry_point,
1293                                number_of_arguments, check_exceptions);
1294 // interpreter specific
1295   restore_bcp();
1296   restore_locals();
1297 }
1298 
1299 void InterpreterMacroAssembler::call_VM_preemptable_helper(Register oop_result,
1300                                                            address entry_point,
1301                                                            int number_of_arguments,
1302                                                            bool check_exceptions) {
1303   assert(InterpreterRuntime::is_preemptable_call(entry_point), "VM call not preemptable, should use call_VM()");
1304   Label resume_pc, not_preempted;
1305 
1306 #ifdef ASSERT
1307   {
1308     Label L1, L2;
1309     ldr(rscratch1, Address(rthread, JavaThread::preempt_alternate_return_offset()));
1310     cbz(rscratch1, L1);
1311     stop("call_VM_preemptable_helper: Should not have alternate return address set");
1312     bind(L1);
1313     // We check this counter in patch_return_pc_with_preempt_stub() during freeze.
1314     incrementw(Address(rthread, JavaThread::interp_at_preemptable_vmcall_cnt_offset()));
1315     ldrw(rscratch1, Address(rthread, JavaThread::interp_at_preemptable_vmcall_cnt_offset()));
1316     cmpw(rscratch1, 0);
1317     br(Assembler::GT, L2);
1318     stop("call_VM_preemptable_helper: should be > 0");
1319     bind(L2);
1320   }
1321 #endif /* ASSERT */
1322 
1323   // Force freeze slow path.
1324   push_cont_fastpath();
1325 
1326   // Make VM call. In case of preemption set last_pc to the one we want to resume to.
1327   // Note: call_VM_base will use resume_pc label to set last_Java_pc.
1328   call_VM_base(noreg, noreg, noreg, &resume_pc, entry_point, number_of_arguments, false /*check_exceptions*/);
1329 
1330   pop_cont_fastpath();
1331 
1332 #ifdef ASSERT
1333   {
1334     Label L;
1335     decrementw(Address(rthread, JavaThread::interp_at_preemptable_vmcall_cnt_offset()));
1336     ldrw(rscratch1, Address(rthread, JavaThread::interp_at_preemptable_vmcall_cnt_offset()));
1337     cmpw(rscratch1, 0);
1338     br(Assembler::GE, L);
1339     stop("call_VM_preemptable_helper: should be >= 0");
1340     bind(L);
1341   }
1342 #endif /* ASSERT */
1343 
1344   // Check if preempted.
1345   ldr(rscratch1, Address(rthread, JavaThread::preempt_alternate_return_offset()));
1346   cbz(rscratch1, not_preempted);
1347   str(zr, Address(rthread, JavaThread::preempt_alternate_return_offset()));
1348   br(rscratch1);
1349 
1350   // In case of preemption, this is where we will resume once we finally acquire the monitor.
1351   bind(resume_pc);
1352   restore_after_resume(false /* is_native */);
1353 
1354   bind(not_preempted);
1355   if (check_exceptions) {
1356     // check for pending exceptions
1357     ldr(rscratch1, Address(rthread, in_bytes(Thread::pending_exception_offset())));
1358     Label ok;
1359     cbz(rscratch1, ok);
1360     lea(rscratch1, RuntimeAddress(StubRoutines::forward_exception_entry()));
1361     br(rscratch1);
1362     bind(ok);
1363   }
1364 
1365   // get oop result if there is one and reset the value in the thread
1366   if (oop_result->is_valid()) {
1367     get_vm_result_oop(oop_result, rthread);
1368   }
1369 }
1370 
1371 static void pass_arg1(MacroAssembler* masm, Register arg) {
1372   if (c_rarg1 != arg ) {
1373     masm->mov(c_rarg1, arg);
1374   }
1375 }
1376 
1377 static void pass_arg2(MacroAssembler* masm, Register arg) {
1378   if (c_rarg2 != arg ) {
1379     masm->mov(c_rarg2, arg);
1380   }
1381 }
1382 
1383 void InterpreterMacroAssembler::call_VM_preemptable(Register oop_result,
1384                                          address entry_point,
1385                                          Register arg_1,
1386                                          bool check_exceptions) {
1387   pass_arg1(this, arg_1);
1388   call_VM_preemptable_helper(oop_result, entry_point, 1, check_exceptions);
1389 }
1390 
1391 void InterpreterMacroAssembler::call_VM_preemptable(Register oop_result,
1392                                          address entry_point,
1393                                          Register arg_1,
1394                                          Register arg_2,
1395                                          bool check_exceptions) {
1396   LP64_ONLY(assert_different_registers(arg_1, c_rarg2));
1397   pass_arg2(this, arg_2);
1398   pass_arg1(this, arg_1);
1399   call_VM_preemptable_helper(oop_result, entry_point, 2, check_exceptions);
1400 }
1401 
1402 void InterpreterMacroAssembler::restore_after_resume(bool is_native) {
1403   lea(rscratch1, ExternalAddress(Interpreter::cont_resume_interpreter_adapter()));
1404   blr(rscratch1);
1405   if (is_native) {
1406     // On resume we need to set up stack as expected
1407     push(dtos);
1408     push(ltos);
1409   }
1410 }
1411 
1412 void InterpreterMacroAssembler::profile_obj_type(Register obj, const Address& mdo_addr) {
1413   assert_different_registers(obj, rscratch1, mdo_addr.base(), mdo_addr.index());
1414   Label update, next, none;
1415 
1416   verify_oop(obj);
1417 
1418   cbnz(obj, update);
1419   orptr(mdo_addr, TypeEntries::null_seen);
1420   b(next);
1421 
1422   bind(update);
1423   load_klass(obj, obj);
1424 
1425   ldr(rscratch1, mdo_addr);
1426   eor(obj, obj, rscratch1);
1427   tst(obj, TypeEntries::type_klass_mask);
1428   br(Assembler::EQ, next); // klass seen before, nothing to
1429                            // do. The unknown bit may have been
1430                            // set already but no need to check.
1431 
1432   tbnz(obj, exact_log2(TypeEntries::type_unknown), next);
1433   // already unknown. Nothing to do anymore.
1434 
1435   cbz(rscratch1, none);
1436   cmp(rscratch1, (u1)TypeEntries::null_seen);
1437   br(Assembler::EQ, none);
1438   // There is a chance that the checks above
1439   // fail if another thread has just set the
1440   // profiling to this obj's klass
1441   eor(obj, obj, rscratch1); // get back original value before XOR
1442   ldr(rscratch1, mdo_addr);
1443   eor(obj, obj, rscratch1);
1444   tst(obj, TypeEntries::type_klass_mask);
1445   br(Assembler::EQ, next);
1446 
1447   // different than before. Cannot keep accurate profile.
1448   orptr(mdo_addr, TypeEntries::type_unknown);
1449   b(next);
1450 
1451   bind(none);
1452   // first time here. Set profile type.
1453   str(obj, mdo_addr);
1454 #ifdef ASSERT
1455   andr(obj, obj, TypeEntries::type_mask);
1456   verify_klass_ptr(obj);
1457 #endif
1458 
1459   bind(next);
1460 }
1461 
1462 void InterpreterMacroAssembler::profile_arguments_type(Register mdp, Register callee, Register tmp, bool is_virtual) {
1463   if (!ProfileInterpreter) {
1464     return;
1465   }
1466 
1467   if (MethodData::profile_arguments() || MethodData::profile_return()) {
1468     Label profile_continue;
1469 
1470     test_method_data_pointer(mdp, profile_continue);
1471 
1472     int off_to_start = is_virtual ? in_bytes(VirtualCallData::virtual_call_data_size()) : in_bytes(CounterData::counter_data_size());
1473 
1474     ldrb(rscratch1, Address(mdp, in_bytes(DataLayout::tag_offset()) - off_to_start));
1475     cmp(rscratch1, u1(is_virtual ? DataLayout::virtual_call_type_data_tag : DataLayout::call_type_data_tag));
1476     br(Assembler::NE, profile_continue);
1477 
1478     if (MethodData::profile_arguments()) {
1479       Label done;
1480       int off_to_args = in_bytes(TypeEntriesAtCall::args_data_offset());
1481 
1482       for (int i = 0; i < TypeProfileArgsLimit; i++) {
1483         if (i > 0 || MethodData::profile_return()) {
1484           // If return value type is profiled we may have no argument to profile
1485           ldr(tmp, Address(mdp, in_bytes(TypeEntriesAtCall::cell_count_offset())));
1486           sub(tmp, tmp, i*TypeStackSlotEntries::per_arg_count());
1487           cmp(tmp, (u1)TypeStackSlotEntries::per_arg_count());
1488           add(rscratch1, mdp, off_to_args);
1489           br(Assembler::LT, done);
1490         }
1491         ldr(tmp, Address(callee, Method::const_offset()));
1492         load_unsigned_short(tmp, Address(tmp, ConstMethod::size_of_parameters_offset()));
1493         // stack offset o (zero based) from the start of the argument
1494         // list, for n arguments translates into offset n - o - 1 from
1495         // the end of the argument list
1496         ldr(rscratch1, Address(mdp, in_bytes(TypeEntriesAtCall::stack_slot_offset(i))));
1497         sub(tmp, tmp, rscratch1);
1498         sub(tmp, tmp, 1);
1499         Address arg_addr = argument_address(tmp);
1500         ldr(tmp, arg_addr);
1501 
1502         Address mdo_arg_addr(mdp, in_bytes(TypeEntriesAtCall::argument_type_offset(i)));
1503         profile_obj_type(tmp, mdo_arg_addr);
1504 
1505         int to_add = in_bytes(TypeStackSlotEntries::per_arg_size());
1506         off_to_args += to_add;
1507       }
1508 
1509       if (MethodData::profile_return()) {
1510         ldr(tmp, Address(mdp, in_bytes(TypeEntriesAtCall::cell_count_offset())));
1511         sub(tmp, tmp, TypeProfileArgsLimit*TypeStackSlotEntries::per_arg_count());
1512       }
1513 
1514       add(rscratch1, mdp, off_to_args);
1515       bind(done);
1516       mov(mdp, rscratch1);
1517 
1518       if (MethodData::profile_return()) {
1519         // We're right after the type profile for the last
1520         // argument. tmp is the number of cells left in the
1521         // CallTypeData/VirtualCallTypeData to reach its end. Non null
1522         // if there's a return to profile.
1523         assert(ReturnTypeEntry::static_cell_count() < TypeStackSlotEntries::per_arg_count(), "can't move past ret type");
1524         add(mdp, mdp, tmp, LSL, exact_log2(DataLayout::cell_size));
1525       }
1526       str(mdp, Address(rfp, frame::interpreter_frame_mdp_offset * wordSize));
1527     } else {
1528       assert(MethodData::profile_return(), "either profile call args or call ret");
1529       update_mdp_by_constant(mdp, in_bytes(TypeEntriesAtCall::return_only_size()));
1530     }
1531 
1532     // mdp points right after the end of the
1533     // CallTypeData/VirtualCallTypeData, right after the cells for the
1534     // return value type if there's one
1535 
1536     bind(profile_continue);
1537   }
1538 }
1539 
1540 void InterpreterMacroAssembler::profile_return_type(Register mdp, Register ret, Register tmp) {
1541   assert_different_registers(mdp, ret, tmp, rbcp);
1542   if (ProfileInterpreter && MethodData::profile_return()) {
1543     Label profile_continue, done;
1544 
1545     test_method_data_pointer(mdp, profile_continue);
1546 
1547     if (MethodData::profile_return_jsr292_only()) {
1548       assert(Method::intrinsic_id_size_in_bytes() == 2, "assuming Method::_intrinsic_id is u2");
1549 
1550       // If we don't profile all invoke bytecodes we must make sure
1551       // it's a bytecode we indeed profile. We can't go back to the
1552       // beginning of the ProfileData we intend to update to check its
1553       // type because we're right after it and we don't known its
1554       // length
1555       Label do_profile;
1556       ldrb(rscratch1, Address(rbcp, 0));
1557       cmp(rscratch1, (u1)Bytecodes::_invokedynamic);
1558       br(Assembler::EQ, do_profile);
1559       cmp(rscratch1, (u1)Bytecodes::_invokehandle);
1560       br(Assembler::EQ, do_profile);
1561       get_method(tmp);
1562       ldrh(rscratch1, Address(tmp, Method::intrinsic_id_offset()));
1563       subs(zr, rscratch1, static_cast<int>(vmIntrinsics::_compiledLambdaForm));
1564       br(Assembler::NE, profile_continue);
1565 
1566       bind(do_profile);
1567     }
1568 
1569     Address mdo_ret_addr(mdp, -in_bytes(ReturnTypeEntry::size()));
1570     mov(tmp, ret);
1571     profile_obj_type(tmp, mdo_ret_addr);
1572 
1573     bind(profile_continue);
1574   }
1575 }
1576 
1577 void InterpreterMacroAssembler::profile_parameters_type(Register mdp, Register tmp1, Register tmp2) {
1578   assert_different_registers(rscratch1, rscratch2, mdp, tmp1, tmp2);
1579   if (ProfileInterpreter && MethodData::profile_parameters()) {
1580     Label profile_continue, done;
1581 
1582     test_method_data_pointer(mdp, profile_continue);
1583 
1584     // Load the offset of the area within the MDO used for
1585     // parameters. If it's negative we're not profiling any parameters
1586     ldrw(tmp1, Address(mdp, in_bytes(MethodData::parameters_type_data_di_offset()) - in_bytes(MethodData::data_offset())));
1587     tbnz(tmp1, 31, profile_continue);  // i.e. sign bit set
1588 
1589     // Compute a pointer to the area for parameters from the offset
1590     // and move the pointer to the slot for the last
1591     // parameters. Collect profiling from last parameter down.
1592     // mdo start + parameters offset + array length - 1
1593     add(mdp, mdp, tmp1);
1594     ldr(tmp1, Address(mdp, ArrayData::array_len_offset()));
1595     sub(tmp1, tmp1, TypeStackSlotEntries::per_arg_count());
1596 
1597     Label loop;
1598     bind(loop);
1599 
1600     int off_base = in_bytes(ParametersTypeData::stack_slot_offset(0));
1601     int type_base = in_bytes(ParametersTypeData::type_offset(0));
1602     int per_arg_scale = exact_log2(DataLayout::cell_size);
1603     add(rscratch1, mdp, off_base);
1604     add(rscratch2, mdp, type_base);
1605 
1606     Address arg_off(rscratch1, tmp1, Address::lsl(per_arg_scale));
1607     Address arg_type(rscratch2, tmp1, Address::lsl(per_arg_scale));
1608 
1609     // load offset on the stack from the slot for this parameter
1610     ldr(tmp2, arg_off);
1611     neg(tmp2, tmp2);
1612     // read the parameter from the local area
1613     ldr(tmp2, Address(rlocals, tmp2, Address::lsl(Interpreter::logStackElementSize)));
1614 
1615     // profile the parameter
1616     profile_obj_type(tmp2, arg_type);
1617 
1618     // go to next parameter
1619     subs(tmp1, tmp1, TypeStackSlotEntries::per_arg_count());
1620     br(Assembler::GE, loop);
1621 
1622     bind(profile_continue);
1623   }
1624 }
1625 
1626 void InterpreterMacroAssembler::load_resolved_indy_entry(Register cache, Register index) {
1627   // Get index out of bytecode pointer, get_cache_entry_pointer_at_bcp
1628   get_cache_index_at_bcp(index, 1, sizeof(u4));
1629   // Get address of invokedynamic array
1630   ldr(cache, Address(rcpool, in_bytes(ConstantPoolCache::invokedynamic_entries_offset())));
1631   // Scale the index to be the entry index * sizeof(ResolvedIndyEntry)
1632   lsl(index, index, log2i_exact(sizeof(ResolvedIndyEntry)));
1633   add(cache, cache, Array<ResolvedIndyEntry>::base_offset_in_bytes());
1634   lea(cache, Address(cache, index));
1635 }
1636 
1637 void InterpreterMacroAssembler::load_field_entry(Register cache, Register index, int bcp_offset) {
1638   // Get index out of bytecode pointer
1639   get_cache_index_at_bcp(index, bcp_offset, sizeof(u2));
1640   // Take shortcut if the size is a power of 2
1641   if (is_power_of_2(sizeof(ResolvedFieldEntry))) {
1642     lsl(index, index, log2i_exact(sizeof(ResolvedFieldEntry))); // Scale index by power of 2
1643   } else {
1644     mov(cache, sizeof(ResolvedFieldEntry));
1645     mul(index, index, cache); // Scale the index to be the entry index * sizeof(ResolvedFieldEntry)
1646   }
1647   // Get address of field entries array
1648   ldr(cache, Address(rcpool, ConstantPoolCache::field_entries_offset()));
1649   add(cache, cache, Array<ResolvedFieldEntry>::base_offset_in_bytes());
1650   lea(cache, Address(cache, index));
1651   // Prevents stale data from being read after the bytecode is patched to the fast bytecode
1652   membar(MacroAssembler::LoadLoad);
1653 }
1654 
1655 void InterpreterMacroAssembler::load_method_entry(Register cache, Register index, int bcp_offset) {
1656   // Get index out of bytecode pointer
1657   get_cache_index_at_bcp(index, bcp_offset, sizeof(u2));
1658   mov(cache, sizeof(ResolvedMethodEntry));
1659   mul(index, index, cache); // Scale the index to be the entry index * sizeof(ResolvedMethodEntry)
1660 
1661   // Get address of field entries array
1662   ldr(cache, Address(rcpool, ConstantPoolCache::method_entries_offset()));
1663   add(cache, cache, Array<ResolvedMethodEntry>::base_offset_in_bytes());
1664   lea(cache, Address(cache, index));
1665 }
1666 
1667 #ifdef ASSERT
1668 void InterpreterMacroAssembler::verify_field_offset(Register reg) {
1669   // Verify the field offset is not in the header, implicitly checks for 0
1670   Label L;
1671   subs(zr, reg, oopDesc::base_offset_in_bytes());
1672   br(Assembler::GE, L);
1673   stop("bad field offset");
1674   bind(L);
1675 }
1676 #endif