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