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