1 /*
   2  * Copyright (c) 2003, 2024, 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 "precompiled.hpp"
  27 #include "asm/macroAssembler.inline.hpp"
  28 #include "compiler/compiler_globals.hpp"
  29 #include "gc/shared/barrierSet.hpp"
  30 #include "gc/shared/barrierSetAssembler.hpp"
  31 #include "interp_masm_aarch64.hpp"
  32 #include "interpreter/interpreter.hpp"
  33 #include "interpreter/interpreterRuntime.hpp"
  34 #include "logging/log.hpp"
  35 #include "oops/arrayOop.hpp"
  36 #include "oops/markWord.hpp"
  37 #include "oops/method.hpp"
  38 #include "oops/methodData.hpp"
  39 #include "oops/resolvedFieldEntry.hpp"
  40 #include "oops/resolvedIndyEntry.hpp"
  41 #include "oops/resolvedMethodEntry.hpp"
  42 #include "prims/jvmtiExport.hpp"
  43 #include "prims/jvmtiThreadState.hpp"
  44 #include "runtime/basicLock.hpp"
  45 #include "runtime/frame.inline.hpp"
  46 #include "runtime/javaThread.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     Unimplemented();
 397   }
 398   if (verifyoop) {
 399     interp_verify_oop(r0, state);
 400   }
 401 
 402   Label safepoint;
 403   address* const safepoint_table = Interpreter::safept_table(state);
 404   bool needs_thread_local_poll = generate_poll && table != safepoint_table;
 405 
 406   if (needs_thread_local_poll) {
 407     NOT_PRODUCT(block_comment("Thread-local Safepoint poll"));
 408     ldr(rscratch2, Address(rthread, JavaThread::polling_word_offset()));
 409     tbnz(rscratch2, exact_log2(SafepointMechanism::poll_bit()), safepoint);
 410   }
 411 
 412   if (table == Interpreter::dispatch_table(state)) {
 413     addw(rscratch2, rscratch1, Interpreter::distance_from_dispatch_table(state));
 414     ldr(rscratch2, Address(rdispatch, rscratch2, Address::uxtw(3)));
 415   } else {
 416     mov(rscratch2, (address)table);
 417     ldr(rscratch2, Address(rscratch2, rscratch1, Address::uxtw(3)));
 418   }
 419   br(rscratch2);
 420 
 421   if (needs_thread_local_poll) {
 422     bind(safepoint);
 423     lea(rscratch2, ExternalAddress((address)safepoint_table));
 424     ldr(rscratch2, Address(rscratch2, rscratch1, Address::uxtw(3)));
 425     br(rscratch2);
 426   }
 427 }
 428 
 429 void InterpreterMacroAssembler::dispatch_only(TosState state, bool generate_poll) {
 430   dispatch_base(state, Interpreter::dispatch_table(state), true, generate_poll);
 431 }
 432 
 433 void InterpreterMacroAssembler::dispatch_only_normal(TosState state) {
 434   dispatch_base(state, Interpreter::normal_table(state));
 435 }
 436 
 437 void InterpreterMacroAssembler::dispatch_only_noverify(TosState state) {
 438   dispatch_base(state, Interpreter::normal_table(state), false);
 439 }
 440 
 441 
 442 void InterpreterMacroAssembler::dispatch_next(TosState state, int step, bool generate_poll) {
 443   // load next bytecode
 444   ldrb(rscratch1, Address(pre(rbcp, step)));
 445   dispatch_base(state, Interpreter::dispatch_table(state), /*verifyoop*/true, generate_poll);
 446 }
 447 
 448 void InterpreterMacroAssembler::dispatch_via(TosState state, address* table) {
 449   // load current bytecode
 450   ldrb(rscratch1, Address(rbcp, 0));
 451   dispatch_base(state, table);
 452 }
 453 
 454 // remove activation
 455 //
 456 // Apply stack watermark barrier.
 457 // Unlock the receiver if this is a synchronized method.
 458 // Unlock any Java monitors from synchronized blocks.
 459 // Remove the activation from the stack.
 460 //
 461 // If there are locked Java monitors
 462 //    If throw_monitor_exception
 463 //       throws IllegalMonitorStateException
 464 //    Else if install_monitor_exception
 465 //       installs IllegalMonitorStateException
 466 //    Else
 467 //       no error processing
 468 void InterpreterMacroAssembler::remove_activation(
 469         TosState state,
 470         bool throw_monitor_exception,
 471         bool install_monitor_exception,
 472         bool notify_jvmdi) {
 473   // Note: Registers r3 xmm0 may be in use for the
 474   // result check if synchronized method
 475   Label unlocked, unlock, no_unlock;
 476 
 477   // The below poll is for the stack watermark barrier. It allows fixing up frames lazily,
 478   // that would normally not be safe to use. Such bad returns into unsafe territory of
 479   // the stack, will call InterpreterRuntime::at_unwind.
 480   Label slow_path;
 481   Label fast_path;
 482   safepoint_poll(slow_path, true /* at_return */, false /* acquire */, false /* in_nmethod */);
 483   br(Assembler::AL, fast_path);
 484   bind(slow_path);
 485   push(state);
 486   set_last_Java_frame(esp, rfp, (address)pc(), rscratch1);
 487   super_call_VM_leaf(CAST_FROM_FN_PTR(address, InterpreterRuntime::at_unwind), rthread);
 488   reset_last_Java_frame(true);
 489   pop(state);
 490   bind(fast_path);
 491 
 492   // get the value of _do_not_unlock_if_synchronized into r3
 493   const Address do_not_unlock_if_synchronized(rthread,
 494     in_bytes(JavaThread::do_not_unlock_if_synchronized_offset()));
 495   ldrb(r3, do_not_unlock_if_synchronized);
 496   strb(zr, do_not_unlock_if_synchronized); // reset the flag
 497 
 498  // get method access flags
 499   ldr(r1, Address(rfp, frame::interpreter_frame_method_offset * wordSize));
 500   ldr(r2, Address(r1, Method::access_flags_offset()));
 501   tbz(r2, exact_log2(JVM_ACC_SYNCHRONIZED), unlocked);
 502 
 503   // Don't unlock anything if the _do_not_unlock_if_synchronized flag
 504   // is set.
 505   cbnz(r3, no_unlock);
 506 
 507   // unlock monitor
 508   push(state); // save result
 509 
 510   // BasicObjectLock will be first in list, since this is a
 511   // synchronized method. However, need to check that the object has
 512   // not been unlocked by an explicit monitorexit bytecode.
 513   const Address monitor(rfp, frame::interpreter_frame_initial_sp_offset *
 514                         wordSize - (int) sizeof(BasicObjectLock));
 515   // We use c_rarg1 so that if we go slow path it will be the correct
 516   // register for unlock_object to pass to VM directly
 517   lea(c_rarg1, monitor); // address of first monitor
 518 
 519   ldr(r0, Address(c_rarg1, BasicObjectLock::obj_offset()));
 520   cbnz(r0, unlock);
 521 
 522   pop(state);
 523   if (throw_monitor_exception) {
 524     // Entry already unlocked, need to throw exception
 525     call_VM(noreg, CAST_FROM_FN_PTR(address,
 526                    InterpreterRuntime::throw_illegal_monitor_state_exception));
 527     should_not_reach_here();
 528   } else {
 529     // Monitor already unlocked during a stack unroll. If requested,
 530     // install an illegal_monitor_state_exception.  Continue with
 531     // stack unrolling.
 532     if (install_monitor_exception) {
 533       call_VM(noreg, CAST_FROM_FN_PTR(address,
 534                      InterpreterRuntime::new_illegal_monitor_state_exception));
 535     }
 536     b(unlocked);
 537   }
 538 
 539   bind(unlock);
 540   unlock_object(c_rarg1);
 541   pop(state);
 542 
 543   // Check that for block-structured locking (i.e., that all locked
 544   // objects has been unlocked)
 545   bind(unlocked);
 546 
 547   // r0: Might contain return value
 548 
 549   // Check that all monitors are unlocked
 550   {
 551     Label loop, exception, entry, restart;
 552     const int entry_size = frame::interpreter_frame_monitor_size_in_bytes();
 553     const Address monitor_block_top(
 554         rfp, frame::interpreter_frame_monitor_block_top_offset * wordSize);
 555     const Address monitor_block_bot(
 556         rfp, frame::interpreter_frame_initial_sp_offset * wordSize);
 557 
 558     bind(restart);
 559     // We use c_rarg1 so that if we go slow path it will be the correct
 560     // register for unlock_object to pass to VM directly
 561     ldr(c_rarg1, monitor_block_top); // derelativize pointer
 562     lea(c_rarg1, Address(rfp, c_rarg1, Address::lsl(Interpreter::logStackElementSize)));
 563     // c_rarg1 points to current entry, starting with top-most entry
 564 
 565     lea(r19, monitor_block_bot);  // points to word before bottom of
 566                                   // monitor block
 567     b(entry);
 568 
 569     // Entry already locked, need to throw exception
 570     bind(exception);
 571 
 572     if (throw_monitor_exception) {
 573       // Throw exception
 574       MacroAssembler::call_VM(noreg,
 575                               CAST_FROM_FN_PTR(address, InterpreterRuntime::
 576                                    throw_illegal_monitor_state_exception));
 577       should_not_reach_here();
 578     } else {
 579       // Stack unrolling. Unlock object and install illegal_monitor_exception.
 580       // Unlock does not block, so don't have to worry about the frame.
 581       // We don't have to preserve c_rarg1 since we are going to throw an exception.
 582 
 583       push(state);
 584       unlock_object(c_rarg1);
 585       pop(state);
 586 
 587       if (install_monitor_exception) {
 588         call_VM(noreg, CAST_FROM_FN_PTR(address,
 589                                         InterpreterRuntime::
 590                                         new_illegal_monitor_state_exception));
 591       }
 592 
 593       b(restart);
 594     }
 595 
 596     bind(loop);
 597     // check if current entry is used
 598     ldr(rscratch1, Address(c_rarg1, BasicObjectLock::obj_offset()));
 599     cbnz(rscratch1, exception);
 600 
 601     add(c_rarg1, c_rarg1, entry_size); // otherwise advance to next entry
 602     bind(entry);
 603     cmp(c_rarg1, r19); // check if bottom reached
 604     br(Assembler::NE, loop); // if not at bottom then check this entry
 605   }
 606 
 607   bind(no_unlock);
 608 
 609   // jvmti support
 610   if (notify_jvmdi) {
 611     notify_method_exit(state, NotifyJVMTI);    // preserve TOSCA
 612   } else {
 613     notify_method_exit(state, SkipNotifyJVMTI); // preserve TOSCA
 614   }
 615 
 616   // remove activation
 617   // get sender esp
 618   ldr(rscratch2,
 619       Address(rfp, frame::interpreter_frame_sender_sp_offset * wordSize));
 620   if (StackReservedPages > 0) {
 621     // testing if reserved zone needs to be re-enabled
 622     Label no_reserved_zone_enabling;
 623 
 624     // check if already enabled - if so no re-enabling needed
 625     assert(sizeof(StackOverflow::StackGuardState) == 4, "unexpected size");
 626     ldrw(rscratch1, Address(rthread, JavaThread::stack_guard_state_offset()));
 627     cmpw(rscratch1, (u1)StackOverflow::stack_guard_enabled);
 628     br(Assembler::EQ, no_reserved_zone_enabling);
 629 
 630     // look for an overflow into the stack reserved zone, i.e.
 631     // interpreter_frame_sender_sp <= JavaThread::reserved_stack_activation
 632     ldr(rscratch1, Address(rthread, JavaThread::reserved_stack_activation_offset()));
 633     cmp(rscratch2, rscratch1);
 634     br(Assembler::LS, no_reserved_zone_enabling);
 635 
 636     call_VM_leaf(
 637       CAST_FROM_FN_PTR(address, SharedRuntime::enable_stack_reserved_zone), rthread);
 638     call_VM(noreg, CAST_FROM_FN_PTR(address,
 639                    InterpreterRuntime::throw_delayed_StackOverflowError));
 640     should_not_reach_here();
 641 
 642     bind(no_reserved_zone_enabling);
 643   }
 644 
 645   // restore sender esp
 646   mov(esp, rscratch2);
 647   // remove frame anchor
 648   leave();
 649   // If we're returning to interpreted code we will shortly be
 650   // adjusting SP to allow some space for ESP.  If we're returning to
 651   // compiled code the saved sender SP was saved in sender_sp, so this
 652   // restores it.
 653   andr(sp, esp, -16);
 654 }
 655 
 656 // Lock object
 657 //
 658 // Args:
 659 //      c_rarg1: BasicObjectLock to be used for locking
 660 //
 661 // Kills:
 662 //      r0
 663 //      c_rarg0, c_rarg1, c_rarg2, c_rarg3, c_rarg4, .. (param regs)
 664 //      rscratch1, rscratch2 (scratch regs)
 665 void InterpreterMacroAssembler::lock_object(Register lock_reg)
 666 {
 667   assert(lock_reg == c_rarg1, "The argument is only for looks. It must be c_rarg1");
 668   if (LockingMode == LM_MONITOR) {
 669     call_VM(noreg,
 670             CAST_FROM_FN_PTR(address, InterpreterRuntime::monitorenter),
 671             lock_reg);
 672   } else {
 673     Label count, done;
 674 
 675     const Register swap_reg = r0;
 676     const Register tmp = c_rarg2;
 677     const Register obj_reg = c_rarg3; // Will contain the oop
 678     const Register tmp2 = c_rarg4;
 679     const Register tmp3 = c_rarg5;
 680 
 681     const int obj_offset = in_bytes(BasicObjectLock::obj_offset());
 682     const int lock_offset = in_bytes(BasicObjectLock::lock_offset());
 683     const int mark_offset = lock_offset +
 684                             BasicLock::displaced_header_offset_in_bytes();
 685 
 686     Label slow_case;
 687 
 688     // Load object pointer into obj_reg %c_rarg3
 689     ldr(obj_reg, Address(lock_reg, obj_offset));
 690 
 691     if (DiagnoseSyncOnValueBasedClasses != 0) {
 692       load_klass(tmp, obj_reg);
 693       ldrw(tmp, Address(tmp, Klass::access_flags_offset()));
 694       tstw(tmp, JVM_ACC_IS_VALUE_BASED_CLASS);
 695       br(Assembler::NE, slow_case);
 696     }
 697 
 698     if (LockingMode == LM_LIGHTWEIGHT) {
 699       lightweight_lock(obj_reg, tmp, tmp2, tmp3, slow_case);
 700       b(done);
 701     } else if (LockingMode == LM_LEGACY) {
 702       // Load (object->mark() | 1) into swap_reg
 703       ldr(rscratch1, Address(obj_reg, oopDesc::mark_offset_in_bytes()));
 704       orr(swap_reg, rscratch1, 1);
 705 
 706       // Save (object->mark() | 1) into BasicLock's displaced header
 707       str(swap_reg, Address(lock_reg, mark_offset));
 708 
 709       assert(lock_offset == 0,
 710              "displached header must be first word in BasicObjectLock");
 711 
 712       Label fail;
 713       cmpxchg_obj_header(swap_reg, lock_reg, obj_reg, rscratch1, count, /*fallthrough*/nullptr);
 714 
 715       // Fast check for recursive lock.
 716       //
 717       // Can apply the optimization only if this is a stack lock
 718       // allocated in this thread. For efficiency, we can focus on
 719       // recently allocated stack locks (instead of reading the stack
 720       // base and checking whether 'mark' points inside the current
 721       // thread stack):
 722       //  1) (mark & 7) == 0, and
 723       //  2) sp <= mark < mark + os::pagesize()
 724       //
 725       // Warning: sp + os::pagesize can overflow the stack base. We must
 726       // neither apply the optimization for an inflated lock allocated
 727       // just above the thread stack (this is why condition 1 matters)
 728       // nor apply the optimization if the stack lock is inside the stack
 729       // of another thread. The latter is avoided even in case of overflow
 730       // because we have guard pages at the end of all stacks. Hence, if
 731       // we go over the stack base and hit the stack of another thread,
 732       // this should not be in a writeable area that could contain a
 733       // stack lock allocated by that thread. As a consequence, a stack
 734       // lock less than page size away from sp is guaranteed to be
 735       // owned by the current thread.
 736       //
 737       // These 3 tests can be done by evaluating the following
 738       // expression: ((mark - sp) & (7 - os::vm_page_size())),
 739       // assuming both stack pointer and pagesize have their
 740       // least significant 3 bits clear.
 741       // NOTE: the mark is in swap_reg %r0 as the result of cmpxchg
 742       // NOTE2: aarch64 does not like to subtract sp from rn so take a
 743       // copy
 744       mov(rscratch1, sp);
 745       sub(swap_reg, swap_reg, rscratch1);
 746       ands(swap_reg, swap_reg, (uint64_t)(7 - (int)os::vm_page_size()));
 747 
 748       // Save the test result, for recursive case, the result is zero
 749       str(swap_reg, Address(lock_reg, mark_offset));
 750       br(Assembler::NE, slow_case);
 751       b(done);
 752     }
 753 
 754     bind(count);
 755     inc_held_monitor_count();
 756     b(done);
 757 
 758     bind(slow_case);
 759 
 760     //    push_cont_fastpath();
 761     // Call the runtime routine for slow case
 762     if (LockingMode == LM_LIGHTWEIGHT) {
 763       call_VM(noreg,
 764               CAST_FROM_FN_PTR(address, InterpreterRuntime::monitorenter_obj),
 765               obj_reg);
 766     } else {
 767       call_VM(noreg,
 768               CAST_FROM_FN_PTR(address, InterpreterRuntime::monitorenter),
 769               lock_reg);
 770     }
 771     //    pop_cont_fastpath();
 772     bind(done);
 773   }
 774 }
 775 
 776 
 777 // Unlocks an object. Used in monitorexit bytecode and
 778 // remove_activation.  Throws an IllegalMonitorException if object is
 779 // not locked by current thread.
 780 //
 781 // Args:
 782 //      c_rarg1: BasicObjectLock for lock
 783 //
 784 // Kills:
 785 //      r0
 786 //      c_rarg0, c_rarg1, c_rarg2, c_rarg3, ... (param regs)
 787 //      rscratch1, rscratch2 (scratch regs)
 788 void InterpreterMacroAssembler::unlock_object(Register lock_reg)
 789 {
 790   assert(lock_reg == c_rarg1, "The argument is only for looks. It must be rarg1");
 791 
 792   if (LockingMode == LM_MONITOR) {
 793     call_VM_leaf(CAST_FROM_FN_PTR(address, InterpreterRuntime::monitorexit), lock_reg);
 794   } else {
 795     Label count, done;
 796 
 797     const Register swap_reg   = r0;
 798     const Register header_reg = c_rarg2;  // Will contain the old oopMark
 799     const Register obj_reg    = c_rarg3;  // Will contain the oop
 800     const Register tmp_reg    = c_rarg4;  // Temporary used by lightweight_unlock
 801 
 802     save_bcp(); // Save in case of exception
 803 
 804     if (LockingMode != LM_LIGHTWEIGHT) {
 805       // Convert from BasicObjectLock structure to object and BasicLock
 806       // structure Store the BasicLock address into %r0
 807       lea(swap_reg, Address(lock_reg, BasicObjectLock::lock_offset()));
 808     }
 809 
 810     // Load oop into obj_reg(%c_rarg3)
 811     ldr(obj_reg, Address(lock_reg, BasicObjectLock::obj_offset()));
 812 
 813     // Free entry
 814     str(zr, Address(lock_reg, BasicObjectLock::obj_offset()));
 815 
 816     Label slow_case;
 817     if (LockingMode == LM_LIGHTWEIGHT) {
 818       lightweight_unlock(obj_reg, header_reg, swap_reg, tmp_reg, slow_case);
 819       b(done);
 820     } else if (LockingMode == LM_LEGACY) {
 821       // Load the old header from BasicLock structure
 822       ldr(header_reg, Address(swap_reg,
 823                               BasicLock::displaced_header_offset_in_bytes()));
 824 
 825       // Test for recursion
 826       cbz(header_reg, done);
 827 
 828       // Atomic swap back the old header
 829       cmpxchg_obj_header(swap_reg, header_reg, obj_reg, rscratch1, count, &slow_case);
 830     }
 831 
 832     bind(count);
 833     dec_held_monitor_count();
 834     b(done);
 835 
 836     bind(slow_case);
 837     // Call the runtime routine for slow case.
 838     str(obj_reg, Address(lock_reg, BasicObjectLock::obj_offset())); // restore obj
 839     call_VM_leaf(CAST_FROM_FN_PTR(address, InterpreterRuntime::monitorexit), lock_reg);
 840     bind(done);
 841     restore_bcp();
 842   }
 843 }
 844 
 845 void InterpreterMacroAssembler::test_method_data_pointer(Register mdp,
 846                                                          Label& zero_continue) {
 847   assert(ProfileInterpreter, "must be profiling interpreter");
 848   ldr(mdp, Address(rfp, frame::interpreter_frame_mdp_offset * wordSize));
 849   cbz(mdp, zero_continue);
 850 }
 851 
 852 // Set the method data pointer for the current bcp.
 853 void InterpreterMacroAssembler::set_method_data_pointer_for_bcp() {
 854   assert(ProfileInterpreter, "must be profiling interpreter");
 855   Label set_mdp;
 856   stp(r0, r1, Address(pre(sp, -2 * wordSize)));
 857 
 858   // Test MDO to avoid the call if it is null.
 859   ldr(r0, Address(rmethod, in_bytes(Method::method_data_offset())));
 860   cbz(r0, set_mdp);
 861   call_VM_leaf(CAST_FROM_FN_PTR(address, InterpreterRuntime::bcp_to_di), rmethod, rbcp);
 862   // r0: mdi
 863   // mdo is guaranteed to be non-zero here, we checked for it before the call.
 864   ldr(r1, Address(rmethod, in_bytes(Method::method_data_offset())));
 865   lea(r1, Address(r1, in_bytes(MethodData::data_offset())));
 866   add(r0, r1, r0);
 867   str(r0, Address(rfp, frame::interpreter_frame_mdp_offset * wordSize));
 868   bind(set_mdp);
 869   ldp(r0, r1, Address(post(sp, 2 * wordSize)));
 870 }
 871 
 872 void InterpreterMacroAssembler::verify_method_data_pointer() {
 873   assert(ProfileInterpreter, "must be profiling interpreter");
 874 #ifdef ASSERT
 875   Label verify_continue;
 876   stp(r0, r1, Address(pre(sp, -2 * wordSize)));
 877   stp(r2, r3, Address(pre(sp, -2 * wordSize)));
 878   test_method_data_pointer(r3, verify_continue); // If mdp is zero, continue
 879   get_method(r1);
 880 
 881   // If the mdp is valid, it will point to a DataLayout header which is
 882   // consistent with the bcp.  The converse is highly probable also.
 883   ldrsh(r2, Address(r3, in_bytes(DataLayout::bci_offset())));
 884   ldr(rscratch1, Address(r1, Method::const_offset()));
 885   add(r2, r2, rscratch1, Assembler::LSL);
 886   lea(r2, Address(r2, ConstMethod::codes_offset()));
 887   cmp(r2, rbcp);
 888   br(Assembler::EQ, verify_continue);
 889   // r1: method
 890   // rbcp: bcp // rbcp == 22
 891   // r3: mdp
 892   call_VM_leaf(CAST_FROM_FN_PTR(address, InterpreterRuntime::verify_mdp),
 893                r1, rbcp, r3);
 894   bind(verify_continue);
 895   ldp(r2, r3, Address(post(sp, 2 * wordSize)));
 896   ldp(r0, r1, Address(post(sp, 2 * wordSize)));
 897 #endif // ASSERT
 898 }
 899 
 900 
 901 void InterpreterMacroAssembler::set_mdp_data_at(Register mdp_in,
 902                                                 int constant,
 903                                                 Register value) {
 904   assert(ProfileInterpreter, "must be profiling interpreter");
 905   Address data(mdp_in, constant);
 906   str(value, data);
 907 }
 908 
 909 
 910 void InterpreterMacroAssembler::increment_mdp_data_at(Register mdp_in,
 911                                                       int constant,
 912                                                       bool decrement) {
 913   increment_mdp_data_at(mdp_in, noreg, constant, decrement);
 914 }
 915 
 916 void InterpreterMacroAssembler::increment_mdp_data_at(Register mdp_in,
 917                                                       Register reg,
 918                                                       int constant,
 919                                                       bool decrement) {
 920   assert(ProfileInterpreter, "must be profiling interpreter");
 921   // %%% this does 64bit counters at best it is wasting space
 922   // at worst it is a rare bug when counters overflow
 923 
 924   assert_different_registers(rscratch2, rscratch1, mdp_in, reg);
 925 
 926   Address addr1(mdp_in, constant);
 927   Address addr2(rscratch2, reg, Address::lsl(0));
 928   Address &addr = addr1;
 929   if (reg != noreg) {
 930     lea(rscratch2, addr1);
 931     addr = addr2;
 932   }
 933 
 934   if (decrement) {
 935     // Decrement the register.  Set condition codes.
 936     // Intel does this
 937     // addptr(data, (int32_t) -DataLayout::counter_increment);
 938     // If the decrement causes the counter to overflow, stay negative
 939     // Label L;
 940     // jcc(Assembler::negative, L);
 941     // addptr(data, (int32_t) DataLayout::counter_increment);
 942     // so we do this
 943     ldr(rscratch1, addr);
 944     subs(rscratch1, rscratch1, (unsigned)DataLayout::counter_increment);
 945     Label L;
 946     br(Assembler::LO, L);       // skip store if counter underflow
 947     str(rscratch1, addr);
 948     bind(L);
 949   } else {
 950     assert(DataLayout::counter_increment == 1,
 951            "flow-free idiom only works with 1");
 952     // Intel does this
 953     // Increment the register.  Set carry flag.
 954     // addptr(data, DataLayout::counter_increment);
 955     // If the increment causes the counter to overflow, pull back by 1.
 956     // sbbptr(data, (int32_t)0);
 957     // so we do this
 958     ldr(rscratch1, addr);
 959     adds(rscratch1, rscratch1, DataLayout::counter_increment);
 960     Label L;
 961     br(Assembler::CS, L);       // skip store if counter overflow
 962     str(rscratch1, addr);
 963     bind(L);
 964   }
 965 }
 966 
 967 void InterpreterMacroAssembler::set_mdp_flag_at(Register mdp_in,
 968                                                 int flag_byte_constant) {
 969   assert(ProfileInterpreter, "must be profiling interpreter");
 970   int flags_offset = in_bytes(DataLayout::flags_offset());
 971   // Set the flag
 972   ldrb(rscratch1, Address(mdp_in, flags_offset));
 973   orr(rscratch1, rscratch1, flag_byte_constant);
 974   strb(rscratch1, Address(mdp_in, flags_offset));
 975 }
 976 
 977 
 978 void InterpreterMacroAssembler::test_mdp_data_at(Register mdp_in,
 979                                                  int offset,
 980                                                  Register value,
 981                                                  Register test_value_out,
 982                                                  Label& not_equal_continue) {
 983   assert(ProfileInterpreter, "must be profiling interpreter");
 984   if (test_value_out == noreg) {
 985     ldr(rscratch1, Address(mdp_in, offset));
 986     cmp(value, rscratch1);
 987   } else {
 988     // Put the test value into a register, so caller can use it:
 989     ldr(test_value_out, Address(mdp_in, offset));
 990     cmp(value, test_value_out);
 991   }
 992   br(Assembler::NE, not_equal_continue);
 993 }
 994 
 995 
 996 void InterpreterMacroAssembler::update_mdp_by_offset(Register mdp_in,
 997                                                      int offset_of_disp) {
 998   assert(ProfileInterpreter, "must be profiling interpreter");
 999   ldr(rscratch1, Address(mdp_in, offset_of_disp));
1000   add(mdp_in, mdp_in, rscratch1, LSL);
1001   str(mdp_in, Address(rfp, frame::interpreter_frame_mdp_offset * wordSize));
1002 }
1003 
1004 
1005 void InterpreterMacroAssembler::update_mdp_by_offset(Register mdp_in,
1006                                                      Register reg,
1007                                                      int offset_of_disp) {
1008   assert(ProfileInterpreter, "must be profiling interpreter");
1009   lea(rscratch1, Address(mdp_in, offset_of_disp));
1010   ldr(rscratch1, Address(rscratch1, reg, Address::lsl(0)));
1011   add(mdp_in, mdp_in, rscratch1, LSL);
1012   str(mdp_in, Address(rfp, frame::interpreter_frame_mdp_offset * wordSize));
1013 }
1014 
1015 
1016 void InterpreterMacroAssembler::update_mdp_by_constant(Register mdp_in,
1017                                                        int constant) {
1018   assert(ProfileInterpreter, "must be profiling interpreter");
1019   add(mdp_in, mdp_in, (unsigned)constant);
1020   str(mdp_in, Address(rfp, frame::interpreter_frame_mdp_offset * wordSize));
1021 }
1022 
1023 
1024 void InterpreterMacroAssembler::update_mdp_for_ret(Register return_bci) {
1025   assert(ProfileInterpreter, "must be profiling interpreter");
1026   // save/restore across call_VM
1027   stp(zr, return_bci, Address(pre(sp, -2 * wordSize)));
1028   call_VM(noreg,
1029           CAST_FROM_FN_PTR(address, InterpreterRuntime::update_mdp_for_ret),
1030           return_bci);
1031   ldp(zr, return_bci, Address(post(sp, 2 * wordSize)));
1032 }
1033 
1034 
1035 void InterpreterMacroAssembler::profile_taken_branch(Register mdp,
1036                                                      Register bumped_count) {
1037   if (ProfileInterpreter) {
1038     Label profile_continue;
1039 
1040     // If no method data exists, go to profile_continue.
1041     // Otherwise, assign to mdp
1042     test_method_data_pointer(mdp, profile_continue);
1043 
1044     // We are taking a branch.  Increment the taken count.
1045     // We inline increment_mdp_data_at to return bumped_count in a register
1046     //increment_mdp_data_at(mdp, in_bytes(JumpData::taken_offset()));
1047     Address data(mdp, in_bytes(JumpData::taken_offset()));
1048     ldr(bumped_count, data);
1049     assert(DataLayout::counter_increment == 1,
1050             "flow-free idiom only works with 1");
1051     // Intel does this to catch overflow
1052     // addptr(bumped_count, DataLayout::counter_increment);
1053     // sbbptr(bumped_count, 0);
1054     // so we do this
1055     adds(bumped_count, bumped_count, DataLayout::counter_increment);
1056     Label L;
1057     br(Assembler::CS, L);       // skip store if counter overflow
1058     str(bumped_count, data);
1059     bind(L);
1060     // The method data pointer needs to be updated to reflect the new target.
1061     update_mdp_by_offset(mdp, in_bytes(JumpData::displacement_offset()));
1062     bind(profile_continue);
1063   }
1064 }
1065 
1066 
1067 void InterpreterMacroAssembler::profile_not_taken_branch(Register mdp) {
1068   if (ProfileInterpreter) {
1069     Label profile_continue;
1070 
1071     // If no method data exists, go to profile_continue.
1072     test_method_data_pointer(mdp, profile_continue);
1073 
1074     // We are taking a branch.  Increment the not taken count.
1075     increment_mdp_data_at(mdp, in_bytes(BranchData::not_taken_offset()));
1076 
1077     // The method data pointer needs to be updated to correspond to
1078     // the next bytecode
1079     update_mdp_by_constant(mdp, in_bytes(BranchData::branch_data_size()));
1080     bind(profile_continue);
1081   }
1082 }
1083 
1084 
1085 void InterpreterMacroAssembler::profile_call(Register mdp) {
1086   if (ProfileInterpreter) {
1087     Label profile_continue;
1088 
1089     // If no method data exists, go to profile_continue.
1090     test_method_data_pointer(mdp, profile_continue);
1091 
1092     // We are making a call.  Increment the count.
1093     increment_mdp_data_at(mdp, in_bytes(CounterData::count_offset()));
1094 
1095     // The method data pointer needs to be updated to reflect the new target.
1096     update_mdp_by_constant(mdp, in_bytes(CounterData::counter_data_size()));
1097     bind(profile_continue);
1098   }
1099 }
1100 
1101 void InterpreterMacroAssembler::profile_final_call(Register mdp) {
1102   if (ProfileInterpreter) {
1103     Label profile_continue;
1104 
1105     // If no method data exists, go to profile_continue.
1106     test_method_data_pointer(mdp, profile_continue);
1107 
1108     // We are making a call.  Increment the count.
1109     increment_mdp_data_at(mdp, in_bytes(CounterData::count_offset()));
1110 
1111     // The method data pointer needs to be updated to reflect the new target.
1112     update_mdp_by_constant(mdp,
1113                            in_bytes(VirtualCallData::
1114                                     virtual_call_data_size()));
1115     bind(profile_continue);
1116   }
1117 }
1118 
1119 
1120 void InterpreterMacroAssembler::profile_virtual_call(Register receiver,
1121                                                      Register mdp,
1122                                                      Register reg2,
1123                                                      bool receiver_can_be_null) {
1124   if (ProfileInterpreter) {
1125     Label profile_continue;
1126 
1127     // If no method data exists, go to profile_continue.
1128     test_method_data_pointer(mdp, profile_continue);
1129 
1130     Label skip_receiver_profile;
1131     if (receiver_can_be_null) {
1132       Label not_null;
1133       // We are making a call.  Increment the count for null receiver.
1134       increment_mdp_data_at(mdp, in_bytes(CounterData::count_offset()));
1135       b(skip_receiver_profile);
1136       bind(not_null);
1137     }
1138 
1139     // Record the receiver type.
1140     record_klass_in_profile(receiver, mdp, reg2);
1141     bind(skip_receiver_profile);
1142 
1143     // The method data pointer needs to be updated to reflect the new target.
1144     update_mdp_by_constant(mdp, in_bytes(VirtualCallData::virtual_call_data_size()));
1145     bind(profile_continue);
1146   }
1147 }
1148 
1149 // This routine creates a state machine for updating the multi-row
1150 // type profile at a virtual call site (or other type-sensitive bytecode).
1151 // The machine visits each row (of receiver/count) until the receiver type
1152 // is found, or until it runs out of rows.  At the same time, it remembers
1153 // the location of the first empty row.  (An empty row records null for its
1154 // receiver, and can be allocated for a newly-observed receiver type.)
1155 // Because there are two degrees of freedom in the state, a simple linear
1156 // search will not work; it must be a decision tree.  Hence this helper
1157 // function is recursive, to generate the required tree structured code.
1158 // It's the interpreter, so we are trading off code space for speed.
1159 // See below for example code.
1160 void InterpreterMacroAssembler::record_klass_in_profile_helper(
1161                                         Register receiver, Register mdp,
1162                                         Register reg2, int start_row,
1163                                         Label& done) {
1164   if (TypeProfileWidth == 0) {
1165     increment_mdp_data_at(mdp, in_bytes(CounterData::count_offset()));
1166   } else {
1167     record_item_in_profile_helper(receiver, mdp, reg2, 0, done, TypeProfileWidth,
1168         &VirtualCallData::receiver_offset, &VirtualCallData::receiver_count_offset);
1169   }
1170 }
1171 
1172 void InterpreterMacroAssembler::record_item_in_profile_helper(Register item, Register mdp,
1173                                         Register reg2, int start_row, Label& done, int total_rows,
1174                                         OffsetFunction item_offset_fn, OffsetFunction item_count_offset_fn) {
1175   int last_row = total_rows - 1;
1176   assert(start_row <= last_row, "must be work left to do");
1177   // Test this row for both the item and for null.
1178   // Take any of three different outcomes:
1179   //   1. found item => increment count and goto done
1180   //   2. found null => keep looking for case 1, maybe allocate this cell
1181   //   3. found something else => keep looking for cases 1 and 2
1182   // Case 3 is handled by a recursive call.
1183   for (int row = start_row; row <= last_row; row++) {
1184     Label next_test;
1185     bool test_for_null_also = (row == start_row);
1186 
1187     // See if the item is item[n].
1188     int item_offset = in_bytes(item_offset_fn(row));
1189     test_mdp_data_at(mdp, item_offset, item,
1190                      (test_for_null_also ? reg2 : noreg),
1191                      next_test);
1192     // (Reg2 now contains the item from the CallData.)
1193 
1194     // The item is item[n].  Increment count[n].
1195     int count_offset = in_bytes(item_count_offset_fn(row));
1196     increment_mdp_data_at(mdp, count_offset);
1197     b(done);
1198     bind(next_test);
1199 
1200     if (test_for_null_also) {
1201       Label found_null;
1202       // Failed the equality check on item[n]...  Test for null.
1203       if (start_row == last_row) {
1204         // The only thing left to do is handle the null case.
1205         cbz(reg2, found_null);
1206         // Item did not match any saved item and there is no empty row for it.
1207         // Increment total counter to indicate polymorphic case.
1208         increment_mdp_data_at(mdp, in_bytes(CounterData::count_offset()));
1209         b(done);
1210         bind(found_null);
1211         break;
1212       }
1213       // Since null is rare, make it be the branch-taken case.
1214       cbz(reg2, found_null);
1215 
1216       // Put all the "Case 3" tests here.
1217       record_item_in_profile_helper(item, mdp, reg2, start_row + 1, done, total_rows,
1218         item_offset_fn, item_count_offset_fn);
1219 
1220       // Found a null.  Keep searching for a matching item,
1221       // but remember that this is an empty (unused) slot.
1222       bind(found_null);
1223     }
1224   }
1225 
1226   // In the fall-through case, we found no matching item, but we
1227   // observed the item[start_row] is null.
1228 
1229   // Fill in the item field and increment the count.
1230   int item_offset = in_bytes(item_offset_fn(start_row));
1231   set_mdp_data_at(mdp, item_offset, item);
1232   int count_offset = in_bytes(item_count_offset_fn(start_row));
1233   mov(reg2, DataLayout::counter_increment);
1234   set_mdp_data_at(mdp, count_offset, reg2);
1235   if (start_row > 0) {
1236     b(done);
1237   }
1238 }
1239 
1240 // Example state machine code for three profile rows:
1241 //   // main copy of decision tree, rooted at row[1]
1242 //   if (row[0].rec == rec) { row[0].incr(); goto done; }
1243 //   if (row[0].rec != nullptr) {
1244 //     // inner copy of decision tree, rooted at row[1]
1245 //     if (row[1].rec == rec) { row[1].incr(); goto done; }
1246 //     if (row[1].rec != nullptr) {
1247 //       // degenerate decision tree, rooted at row[2]
1248 //       if (row[2].rec == rec) { row[2].incr(); goto done; }
1249 //       if (row[2].rec != nullptr) { count.incr(); goto done; } // overflow
1250 //       row[2].init(rec); goto done;
1251 //     } else {
1252 //       // remember row[1] is empty
1253 //       if (row[2].rec == rec) { row[2].incr(); goto done; }
1254 //       row[1].init(rec); goto done;
1255 //     }
1256 //   } else {
1257 //     // remember row[0] is empty
1258 //     if (row[1].rec == rec) { row[1].incr(); goto done; }
1259 //     if (row[2].rec == rec) { row[2].incr(); goto done; }
1260 //     row[0].init(rec); goto done;
1261 //   }
1262 //   done:
1263 
1264 void InterpreterMacroAssembler::record_klass_in_profile(Register receiver,
1265                                                         Register mdp, Register reg2) {
1266   assert(ProfileInterpreter, "must be profiling");
1267   Label done;
1268 
1269   record_klass_in_profile_helper(receiver, mdp, reg2, 0, done);
1270 
1271   bind (done);
1272 }
1273 
1274 void InterpreterMacroAssembler::profile_ret(Register return_bci,
1275                                             Register mdp) {
1276   if (ProfileInterpreter) {
1277     Label profile_continue;
1278     uint row;
1279 
1280     // If no method data exists, go to profile_continue.
1281     test_method_data_pointer(mdp, profile_continue);
1282 
1283     // Update the total ret count.
1284     increment_mdp_data_at(mdp, in_bytes(CounterData::count_offset()));
1285 
1286     for (row = 0; row < RetData::row_limit(); row++) {
1287       Label next_test;
1288 
1289       // See if return_bci is equal to bci[n]:
1290       test_mdp_data_at(mdp,
1291                        in_bytes(RetData::bci_offset(row)),
1292                        return_bci, noreg,
1293                        next_test);
1294 
1295       // return_bci is equal to bci[n].  Increment the count.
1296       increment_mdp_data_at(mdp, in_bytes(RetData::bci_count_offset(row)));
1297 
1298       // The method data pointer needs to be updated to reflect the new target.
1299       update_mdp_by_offset(mdp,
1300                            in_bytes(RetData::bci_displacement_offset(row)));
1301       b(profile_continue);
1302       bind(next_test);
1303     }
1304 
1305     update_mdp_for_ret(return_bci);
1306 
1307     bind(profile_continue);
1308   }
1309 }
1310 
1311 void InterpreterMacroAssembler::profile_null_seen(Register mdp) {
1312   if (ProfileInterpreter) {
1313     Label profile_continue;
1314 
1315     // If no method data exists, go to profile_continue.
1316     test_method_data_pointer(mdp, profile_continue);
1317 
1318     set_mdp_flag_at(mdp, BitData::null_seen_byte_constant());
1319 
1320     // The method data pointer needs to be updated.
1321     int mdp_delta = in_bytes(BitData::bit_data_size());
1322     if (TypeProfileCasts) {
1323       mdp_delta = in_bytes(VirtualCallData::virtual_call_data_size());
1324     }
1325     update_mdp_by_constant(mdp, mdp_delta);
1326 
1327     bind(profile_continue);
1328   }
1329 }
1330 
1331 void InterpreterMacroAssembler::profile_typecheck(Register mdp, Register klass, Register reg2) {
1332   if (ProfileInterpreter) {
1333     Label profile_continue;
1334 
1335     // If no method data exists, go to profile_continue.
1336     test_method_data_pointer(mdp, profile_continue);
1337 
1338     // The method data pointer needs to be updated.
1339     int mdp_delta = in_bytes(BitData::bit_data_size());
1340     if (TypeProfileCasts) {
1341       mdp_delta = in_bytes(VirtualCallData::virtual_call_data_size());
1342 
1343       // Record the object type.
1344       record_klass_in_profile(klass, mdp, reg2);
1345     }
1346     update_mdp_by_constant(mdp, mdp_delta);
1347 
1348     bind(profile_continue);
1349   }
1350 }
1351 
1352 void InterpreterMacroAssembler::profile_switch_default(Register mdp) {
1353   if (ProfileInterpreter) {
1354     Label profile_continue;
1355 
1356     // If no method data exists, go to profile_continue.
1357     test_method_data_pointer(mdp, profile_continue);
1358 
1359     // Update the default case count
1360     increment_mdp_data_at(mdp,
1361                           in_bytes(MultiBranchData::default_count_offset()));
1362 
1363     // The method data pointer needs to be updated.
1364     update_mdp_by_offset(mdp,
1365                          in_bytes(MultiBranchData::
1366                                   default_displacement_offset()));
1367 
1368     bind(profile_continue);
1369   }
1370 }
1371 
1372 void InterpreterMacroAssembler::profile_switch_case(Register index,
1373                                                     Register mdp,
1374                                                     Register reg2) {
1375   if (ProfileInterpreter) {
1376     Label profile_continue;
1377 
1378     // If no method data exists, go to profile_continue.
1379     test_method_data_pointer(mdp, profile_continue);
1380 
1381     // Build the base (index * per_case_size_in_bytes()) +
1382     // case_array_offset_in_bytes()
1383     movw(reg2, in_bytes(MultiBranchData::per_case_size()));
1384     movw(rscratch1, in_bytes(MultiBranchData::case_array_offset()));
1385     Assembler::maddw(index, index, reg2, rscratch1);
1386 
1387     // Update the case count
1388     increment_mdp_data_at(mdp,
1389                           index,
1390                           in_bytes(MultiBranchData::relative_count_offset()));
1391 
1392     // The method data pointer needs to be updated.
1393     update_mdp_by_offset(mdp,
1394                          index,
1395                          in_bytes(MultiBranchData::
1396                                   relative_displacement_offset()));
1397 
1398     bind(profile_continue);
1399   }
1400 }
1401 
1402 void InterpreterMacroAssembler::_interp_verify_oop(Register reg, TosState state, const char* file, int line) {
1403   if (state == atos) {
1404     MacroAssembler::_verify_oop_checked(reg, "broken oop", file, line);
1405   }
1406 }
1407 
1408 void InterpreterMacroAssembler::verify_FPU(int stack_depth, TosState state) { ; }
1409 
1410 
1411 void InterpreterMacroAssembler::notify_method_entry() {
1412   // Whenever JVMTI is interp_only_mode, method entry/exit events are sent to
1413   // track stack depth.  If it is possible to enter interp_only_mode we add
1414   // the code to check if the event should be sent.
1415   if (JvmtiExport::can_post_interpreter_events()) {
1416     Label L;
1417     ldrw(r3, Address(rthread, JavaThread::interp_only_mode_offset()));
1418     cbzw(r3, L);
1419     call_VM(noreg, CAST_FROM_FN_PTR(address,
1420                                     InterpreterRuntime::post_method_entry));
1421     bind(L);
1422   }
1423 
1424   if (DTraceMethodProbes) {
1425     get_method(c_rarg1);
1426     call_VM_leaf(CAST_FROM_FN_PTR(address, SharedRuntime::dtrace_method_entry),
1427                  rthread, c_rarg1);
1428   }
1429 
1430   // RedefineClasses() tracing support for obsolete method entry
1431   if (log_is_enabled(Trace, redefine, class, obsolete)) {
1432     get_method(c_rarg1);
1433     call_VM_leaf(
1434       CAST_FROM_FN_PTR(address, SharedRuntime::rc_trace_method_entry),
1435       rthread, c_rarg1);
1436   }
1437 
1438  }
1439 
1440 
1441 void InterpreterMacroAssembler::notify_method_exit(
1442     TosState state, NotifyMethodExitMode mode) {
1443   // Whenever JVMTI is interp_only_mode, method entry/exit events are sent to
1444   // track stack depth.  If it is possible to enter interp_only_mode we add
1445   // the code to check if the event should be sent.
1446   if (mode == NotifyJVMTI && JvmtiExport::can_post_interpreter_events()) {
1447     Label L;
1448     // Note: frame::interpreter_frame_result has a dependency on how the
1449     // method result is saved across the call to post_method_exit. If this
1450     // is changed then the interpreter_frame_result implementation will
1451     // need to be updated too.
1452 
1453     // template interpreter will leave the result on the top of the stack.
1454     push(state);
1455     ldrw(r3, Address(rthread, JavaThread::interp_only_mode_offset()));
1456     cbz(r3, L);
1457     call_VM(noreg,
1458             CAST_FROM_FN_PTR(address, InterpreterRuntime::post_method_exit));
1459     bind(L);
1460     pop(state);
1461   }
1462 
1463   if (DTraceMethodProbes) {
1464     push(state);
1465     get_method(c_rarg1);
1466     call_VM_leaf(CAST_FROM_FN_PTR(address, SharedRuntime::dtrace_method_exit),
1467                  rthread, c_rarg1);
1468     pop(state);
1469   }
1470 }
1471 
1472 
1473 // Jump if ((*counter_addr += increment) & mask) satisfies the condition.
1474 void InterpreterMacroAssembler::increment_mask_and_jump(Address counter_addr,
1475                                                         int increment, Address mask,
1476                                                         Register scratch, Register scratch2,
1477                                                         bool preloaded, Condition cond,
1478                                                         Label* where) {
1479   if (!preloaded) {
1480     ldrw(scratch, counter_addr);
1481   }
1482   add(scratch, scratch, increment);
1483   strw(scratch, counter_addr);
1484   ldrw(scratch2, mask);
1485   ands(scratch, scratch, scratch2);
1486   br(cond, *where);
1487 }
1488 
1489 void InterpreterMacroAssembler::call_VM_leaf_base(address entry_point,
1490                                                   int number_of_arguments) {
1491   // interpreter specific
1492   //
1493   // Note: No need to save/restore rbcp & rlocals pointer since these
1494   //       are callee saved registers and no blocking/ GC can happen
1495   //       in leaf calls.
1496 #ifdef ASSERT
1497   {
1498     Label L;
1499     ldr(rscratch1, Address(rfp, frame::interpreter_frame_last_sp_offset * wordSize));
1500     cbz(rscratch1, L);
1501     stop("InterpreterMacroAssembler::call_VM_leaf_base:"
1502          " last_sp != nullptr");
1503     bind(L);
1504   }
1505 #endif /* ASSERT */
1506   // super call
1507   MacroAssembler::call_VM_leaf_base(entry_point, number_of_arguments);
1508 }
1509 
1510 void InterpreterMacroAssembler::call_VM_base(Register oop_result,
1511                                              Register java_thread,
1512                                              Register last_java_sp,
1513                                              address  entry_point,
1514                                              int      number_of_arguments,
1515                                              bool     check_exceptions) {
1516   // interpreter specific
1517   //
1518   // Note: Could avoid restoring locals ptr (callee saved) - however doesn't
1519   //       really make a difference for these runtime calls, since they are
1520   //       slow anyway. Btw., bcp must be saved/restored since it may change
1521   //       due to GC.
1522   // assert(java_thread == noreg , "not expecting a precomputed java thread");
1523   save_bcp();
1524 #ifdef ASSERT
1525   {
1526     Label L;
1527     ldr(rscratch1, Address(rfp, frame::interpreter_frame_last_sp_offset * wordSize));
1528     cbz(rscratch1, L);
1529     stop("InterpreterMacroAssembler::call_VM_base:"
1530          " last_sp != nullptr");
1531     bind(L);
1532   }
1533 #endif /* ASSERT */
1534   // super call
1535   MacroAssembler::call_VM_base(oop_result, noreg, last_java_sp,
1536                                entry_point, number_of_arguments,
1537                      check_exceptions);
1538 // interpreter specific
1539   restore_bcp();
1540   restore_locals();
1541 }
1542 
1543 void InterpreterMacroAssembler::profile_obj_type(Register obj, const Address& mdo_addr) {
1544   assert_different_registers(obj, rscratch1, mdo_addr.base(), mdo_addr.index());
1545   Label update, next, none;
1546 
1547   verify_oop(obj);
1548 
1549   cbnz(obj, update);
1550   orptr(mdo_addr, TypeEntries::null_seen);
1551   b(next);
1552 
1553   bind(update);
1554   load_klass(obj, obj);
1555 
1556   ldr(rscratch1, mdo_addr);
1557   eor(obj, obj, rscratch1);
1558   tst(obj, TypeEntries::type_klass_mask);
1559   br(Assembler::EQ, next); // klass seen before, nothing to
1560                            // do. The unknown bit may have been
1561                            // set already but no need to check.
1562 
1563   tbnz(obj, exact_log2(TypeEntries::type_unknown), next);
1564   // already unknown. Nothing to do anymore.
1565 
1566   cbz(rscratch1, none);
1567   cmp(rscratch1, (u1)TypeEntries::null_seen);
1568   br(Assembler::EQ, none);
1569   // There is a chance that the checks above
1570   // fail if another thread has just set the
1571   // profiling to this obj's klass
1572   eor(obj, obj, rscratch1); // get back original value before XOR
1573   ldr(rscratch1, mdo_addr);
1574   eor(obj, obj, rscratch1);
1575   tst(obj, TypeEntries::type_klass_mask);
1576   br(Assembler::EQ, next);
1577 
1578   // different than before. Cannot keep accurate profile.
1579   orptr(mdo_addr, TypeEntries::type_unknown);
1580   b(next);
1581 
1582   bind(none);
1583   // first time here. Set profile type.
1584   str(obj, mdo_addr);
1585 #ifdef ASSERT
1586   andr(obj, obj, TypeEntries::type_mask);
1587   verify_klass_ptr(obj);
1588 #endif
1589 
1590   bind(next);
1591 }
1592 
1593 void InterpreterMacroAssembler::profile_arguments_type(Register mdp, Register callee, Register tmp, bool is_virtual) {
1594   if (!ProfileInterpreter) {
1595     return;
1596   }
1597 
1598   if (MethodData::profile_arguments() || MethodData::profile_return()) {
1599     Label profile_continue;
1600 
1601     test_method_data_pointer(mdp, profile_continue);
1602 
1603     int off_to_start = is_virtual ? in_bytes(VirtualCallData::virtual_call_data_size()) : in_bytes(CounterData::counter_data_size());
1604 
1605     ldrb(rscratch1, Address(mdp, in_bytes(DataLayout::tag_offset()) - off_to_start));
1606     cmp(rscratch1, u1(is_virtual ? DataLayout::virtual_call_type_data_tag : DataLayout::call_type_data_tag));
1607     br(Assembler::NE, profile_continue);
1608 
1609     if (MethodData::profile_arguments()) {
1610       Label done;
1611       int off_to_args = in_bytes(TypeEntriesAtCall::args_data_offset());
1612 
1613       for (int i = 0; i < TypeProfileArgsLimit; i++) {
1614         if (i > 0 || MethodData::profile_return()) {
1615           // If return value type is profiled we may have no argument to profile
1616           ldr(tmp, Address(mdp, in_bytes(TypeEntriesAtCall::cell_count_offset())));
1617           sub(tmp, tmp, i*TypeStackSlotEntries::per_arg_count());
1618           cmp(tmp, (u1)TypeStackSlotEntries::per_arg_count());
1619           add(rscratch1, mdp, off_to_args);
1620           br(Assembler::LT, done);
1621         }
1622         ldr(tmp, Address(callee, Method::const_offset()));
1623         load_unsigned_short(tmp, Address(tmp, ConstMethod::size_of_parameters_offset()));
1624         // stack offset o (zero based) from the start of the argument
1625         // list, for n arguments translates into offset n - o - 1 from
1626         // the end of the argument list
1627         ldr(rscratch1, Address(mdp, in_bytes(TypeEntriesAtCall::stack_slot_offset(i))));
1628         sub(tmp, tmp, rscratch1);
1629         sub(tmp, tmp, 1);
1630         Address arg_addr = argument_address(tmp);
1631         ldr(tmp, arg_addr);
1632 
1633         Address mdo_arg_addr(mdp, in_bytes(TypeEntriesAtCall::argument_type_offset(i)));
1634         profile_obj_type(tmp, mdo_arg_addr);
1635 
1636         int to_add = in_bytes(TypeStackSlotEntries::per_arg_size());
1637         off_to_args += to_add;
1638       }
1639 
1640       if (MethodData::profile_return()) {
1641         ldr(tmp, Address(mdp, in_bytes(TypeEntriesAtCall::cell_count_offset())));
1642         sub(tmp, tmp, TypeProfileArgsLimit*TypeStackSlotEntries::per_arg_count());
1643       }
1644 
1645       add(rscratch1, mdp, off_to_args);
1646       bind(done);
1647       mov(mdp, rscratch1);
1648 
1649       if (MethodData::profile_return()) {
1650         // We're right after the type profile for the last
1651         // argument. tmp is the number of cells left in the
1652         // CallTypeData/VirtualCallTypeData to reach its end. Non null
1653         // if there's a return to profile.
1654         assert(ReturnTypeEntry::static_cell_count() < TypeStackSlotEntries::per_arg_count(), "can't move past ret type");
1655         add(mdp, mdp, tmp, LSL, exact_log2(DataLayout::cell_size));
1656       }
1657       str(mdp, Address(rfp, frame::interpreter_frame_mdp_offset * wordSize));
1658     } else {
1659       assert(MethodData::profile_return(), "either profile call args or call ret");
1660       update_mdp_by_constant(mdp, in_bytes(TypeEntriesAtCall::return_only_size()));
1661     }
1662 
1663     // mdp points right after the end of the
1664     // CallTypeData/VirtualCallTypeData, right after the cells for the
1665     // return value type if there's one
1666 
1667     bind(profile_continue);
1668   }
1669 }
1670 
1671 void InterpreterMacroAssembler::profile_return_type(Register mdp, Register ret, Register tmp) {
1672   assert_different_registers(mdp, ret, tmp, rbcp);
1673   if (ProfileInterpreter && MethodData::profile_return()) {
1674     Label profile_continue, done;
1675 
1676     test_method_data_pointer(mdp, profile_continue);
1677 
1678     if (MethodData::profile_return_jsr292_only()) {
1679       assert(Method::intrinsic_id_size_in_bytes() == 2, "assuming Method::_intrinsic_id is u2");
1680 
1681       // If we don't profile all invoke bytecodes we must make sure
1682       // it's a bytecode we indeed profile. We can't go back to the
1683       // beginning of the ProfileData we intend to update to check its
1684       // type because we're right after it and we don't known its
1685       // length
1686       Label do_profile;
1687       ldrb(rscratch1, Address(rbcp, 0));
1688       cmp(rscratch1, (u1)Bytecodes::_invokedynamic);
1689       br(Assembler::EQ, do_profile);
1690       cmp(rscratch1, (u1)Bytecodes::_invokehandle);
1691       br(Assembler::EQ, do_profile);
1692       get_method(tmp);
1693       ldrh(rscratch1, Address(tmp, Method::intrinsic_id_offset()));
1694       subs(zr, rscratch1, static_cast<int>(vmIntrinsics::_compiledLambdaForm));
1695       br(Assembler::NE, profile_continue);
1696 
1697       bind(do_profile);
1698     }
1699 
1700     Address mdo_ret_addr(mdp, -in_bytes(ReturnTypeEntry::size()));
1701     mov(tmp, ret);
1702     profile_obj_type(tmp, mdo_ret_addr);
1703 
1704     bind(profile_continue);
1705   }
1706 }
1707 
1708 void InterpreterMacroAssembler::profile_parameters_type(Register mdp, Register tmp1, Register tmp2) {
1709   assert_different_registers(rscratch1, rscratch2, mdp, tmp1, tmp2);
1710   if (ProfileInterpreter && MethodData::profile_parameters()) {
1711     Label profile_continue, done;
1712 
1713     test_method_data_pointer(mdp, profile_continue);
1714 
1715     // Load the offset of the area within the MDO used for
1716     // parameters. If it's negative we're not profiling any parameters
1717     ldrw(tmp1, Address(mdp, in_bytes(MethodData::parameters_type_data_di_offset()) - in_bytes(MethodData::data_offset())));
1718     tbnz(tmp1, 31, profile_continue);  // i.e. sign bit set
1719 
1720     // Compute a pointer to the area for parameters from the offset
1721     // and move the pointer to the slot for the last
1722     // parameters. Collect profiling from last parameter down.
1723     // mdo start + parameters offset + array length - 1
1724     add(mdp, mdp, tmp1);
1725     ldr(tmp1, Address(mdp, ArrayData::array_len_offset()));
1726     sub(tmp1, tmp1, TypeStackSlotEntries::per_arg_count());
1727 
1728     Label loop;
1729     bind(loop);
1730 
1731     int off_base = in_bytes(ParametersTypeData::stack_slot_offset(0));
1732     int type_base = in_bytes(ParametersTypeData::type_offset(0));
1733     int per_arg_scale = exact_log2(DataLayout::cell_size);
1734     add(rscratch1, mdp, off_base);
1735     add(rscratch2, mdp, type_base);
1736 
1737     Address arg_off(rscratch1, tmp1, Address::lsl(per_arg_scale));
1738     Address arg_type(rscratch2, tmp1, Address::lsl(per_arg_scale));
1739 
1740     // load offset on the stack from the slot for this parameter
1741     ldr(tmp2, arg_off);
1742     neg(tmp2, tmp2);
1743     // read the parameter from the local area
1744     ldr(tmp2, Address(rlocals, tmp2, Address::lsl(Interpreter::logStackElementSize)));
1745 
1746     // profile the parameter
1747     profile_obj_type(tmp2, arg_type);
1748 
1749     // go to next parameter
1750     subs(tmp1, tmp1, TypeStackSlotEntries::per_arg_count());
1751     br(Assembler::GE, loop);
1752 
1753     bind(profile_continue);
1754   }
1755 }
1756 
1757 void InterpreterMacroAssembler::load_resolved_indy_entry(Register cache, Register index) {
1758   // Get index out of bytecode pointer, get_cache_entry_pointer_at_bcp
1759   get_cache_index_at_bcp(index, 1, sizeof(u4));
1760   // Get address of invokedynamic array
1761   ldr(cache, Address(rcpool, in_bytes(ConstantPoolCache::invokedynamic_entries_offset())));
1762   // Scale the index to be the entry index * sizeof(ResolvedIndyEntry)
1763   lsl(index, index, log2i_exact(sizeof(ResolvedIndyEntry)));
1764   add(cache, cache, Array<ResolvedIndyEntry>::base_offset_in_bytes());
1765   lea(cache, Address(cache, index));
1766 }
1767 
1768 void InterpreterMacroAssembler::load_field_entry(Register cache, Register index, int bcp_offset) {
1769   // Get index out of bytecode pointer
1770   get_cache_index_at_bcp(index, bcp_offset, sizeof(u2));
1771   // Take shortcut if the size is a power of 2
1772   if (is_power_of_2(sizeof(ResolvedFieldEntry))) {
1773     lsl(index, index, log2i_exact(sizeof(ResolvedFieldEntry))); // Scale index by power of 2
1774   } else {
1775     mov(cache, sizeof(ResolvedFieldEntry));
1776     mul(index, index, cache); // Scale the index to be the entry index * sizeof(ResolvedFieldEntry)
1777   }
1778   // Get address of field entries array
1779   ldr(cache, Address(rcpool, ConstantPoolCache::field_entries_offset()));
1780   add(cache, cache, Array<ResolvedFieldEntry>::base_offset_in_bytes());
1781   lea(cache, Address(cache, index));
1782   // Prevents stale data from being read after the bytecode is patched to the fast bytecode
1783   membar(MacroAssembler::LoadLoad);
1784 }
1785 
1786 void InterpreterMacroAssembler::load_method_entry(Register cache, Register index, int bcp_offset) {
1787   // Get index out of bytecode pointer
1788   get_cache_index_at_bcp(index, bcp_offset, sizeof(u2));
1789   mov(cache, sizeof(ResolvedMethodEntry));
1790   mul(index, index, cache); // Scale the index to be the entry index * sizeof(ResolvedMethodEntry)
1791 
1792   // Get address of field entries array
1793   ldr(cache, Address(rcpool, ConstantPoolCache::method_entries_offset()));
1794   add(cache, cache, Array<ResolvedMethodEntry>::base_offset_in_bytes());
1795   lea(cache, Address(cache, index));
1796 }