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