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