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