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