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