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