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 #ifdef ASSERT
 522   Label not_preempted;
 523   ld(t0, Address(xthread, JavaThread::preempt_alternate_return_offset()));
 524   beqz(t0, not_preempted);
 525   stop("remove_activation: should not have alternate return address set");
 526   bind(not_preempted);
 527 #endif /* ASSERT */
 528 
 529   // get the value of _do_not_unlock_if_synchronized into x13
 530   const Address do_not_unlock_if_synchronized(xthread,
 531     in_bytes(JavaThread::do_not_unlock_if_synchronized_offset()));
 532   lbu(x13, do_not_unlock_if_synchronized);
 533   sb(zr, do_not_unlock_if_synchronized); // reset the flag
 534 
 535   // get method access flags
 536   ld(x11, Address(fp, frame::interpreter_frame_method_offset * wordSize));
 537   load_unsigned_short(x12, Address(x11, Method::access_flags_offset()));
 538   test_bit(t0, x12, exact_log2(JVM_ACC_SYNCHRONIZED));
 539   beqz(t0, unlocked);
 540 
 541   // Don't unlock anything if the _do_not_unlock_if_synchronized flag
 542   // is set.
 543   bnez(x13, no_unlock);
 544 
 545   // unlock monitor
 546   push(state); // save result
 547 
 548   // BasicObjectLock will be first in list, since this is a
 549   // synchronized method. However, need to check that the object has
 550   // not been unlocked by an explicit monitorexit bytecode.
 551   const Address monitor(fp, frame::interpreter_frame_initial_sp_offset *
 552                         wordSize - (int) sizeof(BasicObjectLock));
 553   // We use c_rarg1 so that if we go slow path it will be the correct
 554   // register for unlock_object to pass to VM directly
 555   la(c_rarg1, monitor); // address of first monitor
 556 
 557   ld(x10, Address(c_rarg1, BasicObjectLock::obj_offset()));
 558   bnez(x10, unlock);
 559 
 560   pop(state);
 561   if (throw_monitor_exception) {
 562     // Entry already unlocked, need to throw exception
 563     call_VM(noreg, CAST_FROM_FN_PTR(address,
 564                                     InterpreterRuntime::throw_illegal_monitor_state_exception));
 565     should_not_reach_here();
 566   } else {
 567     // Monitor already unlocked during a stack unroll. If requested,
 568     // install an illegal_monitor_state_exception.  Continue with
 569     // stack unrolling.
 570     if (install_monitor_exception) {
 571       call_VM(noreg, CAST_FROM_FN_PTR(address,
 572                                       InterpreterRuntime::new_illegal_monitor_state_exception));
 573     }
 574     j(unlocked);
 575   }
 576 
 577   bind(unlock);
 578   unlock_object(c_rarg1);
 579   pop(state);
 580 
 581   // Check that for block-structured locking (i.e., that all locked
 582   // objects has been unlocked)
 583   bind(unlocked);
 584 
 585   // x10: Might contain return value
 586 
 587   // Check that all monitors are unlocked
 588   {
 589     Label loop, exception, entry, restart;
 590     const int entry_size = frame::interpreter_frame_monitor_size_in_bytes();
 591     const Address monitor_block_top(
 592       fp, frame::interpreter_frame_monitor_block_top_offset * wordSize);
 593     const Address monitor_block_bot(
 594       fp, frame::interpreter_frame_initial_sp_offset * wordSize);
 595 
 596     bind(restart);
 597     // We use c_rarg1 so that if we go slow path it will be the correct
 598     // register for unlock_object to pass to VM directly
 599     ld(c_rarg1, monitor_block_top); // derelativize pointer
 600     shadd(c_rarg1, c_rarg1, fp, c_rarg1, LogBytesPerWord);
 601     // c_rarg1 points to current entry, starting with top-most entry
 602 
 603     la(x9, monitor_block_bot);  // points to word before bottom of
 604                                   // monitor block
 605 
 606     j(entry);
 607 
 608     // Entry already locked, need to throw exception
 609     bind(exception);
 610 
 611     if (throw_monitor_exception) {
 612       // Throw exception
 613       MacroAssembler::call_VM(noreg,
 614                               CAST_FROM_FN_PTR(address, InterpreterRuntime::
 615                                                throw_illegal_monitor_state_exception));
 616 
 617       should_not_reach_here();
 618     } else {
 619       // Stack unrolling. Unlock object and install illegal_monitor_exception.
 620       // Unlock does not block, so don't have to worry about the frame.
 621       // We don't have to preserve c_rarg1 since we are going to throw an exception.
 622 
 623       push(state);
 624       unlock_object(c_rarg1);
 625       pop(state);
 626 
 627       if (install_monitor_exception) {
 628         call_VM(noreg, CAST_FROM_FN_PTR(address,
 629                                         InterpreterRuntime::
 630                                         new_illegal_monitor_state_exception));
 631       }
 632 
 633       j(restart);
 634     }
 635 
 636     bind(loop);
 637     // check if current entry is used
 638     add(t0, c_rarg1, in_bytes(BasicObjectLock::obj_offset()));
 639     ld(t0, Address(t0, 0));
 640     bnez(t0, exception);
 641 
 642     add(c_rarg1, c_rarg1, entry_size); // otherwise advance to next entry
 643     bind(entry);
 644     bne(c_rarg1, x9, loop); // check if bottom reached if not at bottom then check this entry
 645   }
 646 
 647   bind(no_unlock);
 648 
 649   JFR_ONLY(enter_jfr_critical_section();)
 650 
 651   // The below poll is for the stack watermark barrier. It allows fixing up frames lazily,
 652   // that would normally not be safe to use. Such bad returns into unsafe territory of
 653   // the stack, will call InterpreterRuntime::at_unwind.
 654   Label slow_path;
 655   Label fast_path;
 656   safepoint_poll(slow_path, true /* at_return */, false /* in_nmethod */);
 657   j(fast_path);
 658 
 659   bind(slow_path);
 660   push(state);
 661   set_last_Java_frame(esp, fp, pc(), t0);
 662   super_call_VM_leaf(CAST_FROM_FN_PTR(address, InterpreterRuntime::at_unwind), xthread);
 663   reset_last_Java_frame(true);
 664   pop(state);
 665   bind(fast_path);
 666 
 667   // JVMTI support. Make sure the safepoint poll test is issued prior.
 668   if (notify_jvmdi) {
 669     notify_method_exit(state, NotifyJVMTI); // preserve TOSCA
 670   } else {
 671     notify_method_exit(state, SkipNotifyJVMTI); // preserve TOSCA
 672   }
 673 
 674   // remove activation
 675   // get sender esp
 676   ld(t1,
 677      Address(fp, frame::interpreter_frame_sender_sp_offset * wordSize));
 678   if (StackReservedPages > 0) {
 679     // testing if reserved zone needs to be re-enabled
 680     Label no_reserved_zone_enabling;
 681 
 682     // check if already enabled - if so no re-enabling needed
 683     assert(sizeof(StackOverflow::StackGuardState) == 4, "unexpected size");
 684     lw(t0, Address(xthread, JavaThread::stack_guard_state_offset()));
 685     subw(t0, t0, StackOverflow::stack_guard_enabled);
 686     beqz(t0, no_reserved_zone_enabling);
 687 
 688     // look for an overflow into the stack reserved zone, i.e.
 689     // interpreter_frame_sender_sp <= JavaThread::reserved_stack_activation
 690     ld(t0, Address(xthread, JavaThread::reserved_stack_activation_offset()));
 691     ble(t1, t0, no_reserved_zone_enabling);
 692 
 693     JFR_ONLY(leave_jfr_critical_section();)
 694 
 695     call_VM_leaf(
 696       CAST_FROM_FN_PTR(address, SharedRuntime::enable_stack_reserved_zone), xthread);
 697     call_VM(noreg, CAST_FROM_FN_PTR(address,
 698                                     InterpreterRuntime::throw_delayed_StackOverflowError));
 699     should_not_reach_here();
 700 
 701     bind(no_reserved_zone_enabling);
 702   }
 703 
 704   // remove frame anchor
 705   leave();
 706 
 707   JFR_ONLY(leave_jfr_critical_section();)
 708 
 709   // restore sender esp
 710   mv(esp, t1);
 711 
 712   // If we're returning to interpreted code we will shortly be
 713   // adjusting SP to allow some space for ESP.  If we're returning to
 714   // compiled code the saved sender SP was saved in sender_sp, so this
 715   // restores it.
 716   andi(sp, esp, -16);
 717 }
 718 
 719 #if INCLUDE_JFR
 720 void InterpreterMacroAssembler::enter_jfr_critical_section() {
 721   const Address sampling_critical_section(xthread, in_bytes(SAMPLING_CRITICAL_SECTION_OFFSET_JFR));
 722   mv(t0, true);
 723   sb(t0, sampling_critical_section);
 724 }
 725 
 726 void InterpreterMacroAssembler::leave_jfr_critical_section() {
 727   const Address sampling_critical_section(xthread, in_bytes(SAMPLING_CRITICAL_SECTION_OFFSET_JFR));
 728   sb(zr, sampling_critical_section);
 729 }
 730 #endif // INCLUDE_JFR
 731 
 732 // Lock object
 733 //
 734 // Args:
 735 //      c_rarg1: BasicObjectLock to be used for locking
 736 //
 737 // Kills:
 738 //      x10
 739 //      c_rarg0, c_rarg1, c_rarg2, c_rarg3, c_rarg4, c_rarg5, .. (param regs)
 740 //      t0, t1 (temp regs)
 741 void InterpreterMacroAssembler::lock_object(Register lock_reg)
 742 {
 743   assert(lock_reg == c_rarg1, "The argument is only for looks. It must be c_rarg1");
 744 
 745   const Register tmp = c_rarg2;
 746   const Register obj_reg = c_rarg3; // Will contain the oop
 747   const Register tmp2 = c_rarg4;
 748   const Register tmp3 = c_rarg5;
 749 
 750   // Load object pointer into obj_reg (c_rarg3)
 751   ld(obj_reg, Address(lock_reg, BasicObjectLock::obj_offset()));
 752 
 753   Label done, slow_case;
 754   lightweight_lock(lock_reg, obj_reg, tmp, tmp2, tmp3, slow_case);
 755   j(done);
 756 
 757   bind(slow_case);
 758   // Call the runtime routine for slow case
 759   call_VM_preemptable(noreg,
 760           CAST_FROM_FN_PTR(address, InterpreterRuntime::monitorenter),
 761           lock_reg);
 762 
 763   bind(done);
 764 }
 765 
 766 
 767 // Unlocks an object. Used in monitorexit bytecode and
 768 // remove_activation.  Throws an IllegalMonitorException if object is
 769 // not locked by current thread.
 770 //
 771 // Args:
 772 //      c_rarg1: BasicObjectLock for lock
 773 //
 774 // Kills:
 775 //      x10
 776 //      c_rarg0, c_rarg1, c_rarg2, c_rarg3, c_rarg4, ... (param regs)
 777 //      t0, t1 (temp regs)
 778 void InterpreterMacroAssembler::unlock_object(Register lock_reg)
 779 {
 780   assert(lock_reg == c_rarg1, "The argument is only for looks. It must be rarg1");
 781 
 782   const Register swap_reg   = x10;
 783   const Register header_reg = c_rarg2;  // Will contain the old oopMark
 784   const Register obj_reg    = c_rarg3;  // Will contain the oop
 785   const Register tmp_reg    = c_rarg4;  // Temporary used by lightweight_unlock
 786 
 787   save_bcp(); // Save in case of exception
 788 
 789   // Load oop into obj_reg (c_rarg3)
 790   ld(obj_reg, Address(lock_reg, BasicObjectLock::obj_offset()));
 791 
 792   // Free entry
 793   sd(zr, Address(lock_reg, BasicObjectLock::obj_offset()));
 794 
 795   Label done, slow_case;
 796   lightweight_unlock(obj_reg, header_reg, swap_reg, tmp_reg, slow_case);
 797   j(done);
 798 
 799   bind(slow_case);
 800   // Call the runtime routine for slow case.
 801   sd(obj_reg, Address(lock_reg, BasicObjectLock::obj_offset())); // restore obj
 802   call_VM_leaf(CAST_FROM_FN_PTR(address, InterpreterRuntime::monitorexit), lock_reg);
 803 
 804   bind(done);
 805   restore_bcp();
 806 }
 807 
 808 
 809 void InterpreterMacroAssembler::test_method_data_pointer(Register mdp,
 810                                                          Label& zero_continue) {
 811   assert(ProfileInterpreter, "must be profiling interpreter");
 812   ld(mdp, Address(fp, frame::interpreter_frame_mdp_offset * wordSize));
 813   beqz(mdp, zero_continue);
 814 }
 815 
 816 // Set the method data pointer for the current bcp.
 817 void InterpreterMacroAssembler::set_method_data_pointer_for_bcp() {
 818   assert(ProfileInterpreter, "must be profiling interpreter");
 819   Label set_mdp;
 820   push_reg(RegSet::of(x10, x11), sp); // save x10, x11
 821 
 822   // Test MDO to avoid the call if it is null.
 823   ld(x10, Address(xmethod, in_bytes(Method::method_data_offset())));
 824   beqz(x10, set_mdp);
 825   call_VM_leaf(CAST_FROM_FN_PTR(address, InterpreterRuntime::bcp_to_di), xmethod, xbcp);
 826   // x10: mdi
 827   // mdo is guaranteed to be non-zero here, we checked for it before the call.
 828   ld(x11, Address(xmethod, in_bytes(Method::method_data_offset())));
 829   la(x11, Address(x11, in_bytes(MethodData::data_offset())));
 830   add(x10, x11, x10);
 831   sd(x10, Address(fp, frame::interpreter_frame_mdp_offset * wordSize));
 832   bind(set_mdp);
 833   pop_reg(RegSet::of(x10, x11), sp);
 834 }
 835 
 836 void InterpreterMacroAssembler::verify_method_data_pointer() {
 837   assert(ProfileInterpreter, "must be profiling interpreter");
 838 #ifdef ASSERT
 839   Label verify_continue;
 840   subi(sp, sp, 4 * wordSize);
 841   sd(x10, Address(sp, 0));
 842   sd(x11, Address(sp, wordSize));
 843   sd(x12, Address(sp, 2 * wordSize));
 844   sd(x13, Address(sp, 3 * wordSize));
 845   test_method_data_pointer(x13, verify_continue); // If mdp is zero, continue
 846   get_method(x11);
 847 
 848   // If the mdp is valid, it will point to a DataLayout header which is
 849   // consistent with the bcp.  The converse is highly probable also.
 850   lh(x12, Address(x13, in_bytes(DataLayout::bci_offset())));
 851   ld(t0, Address(x11, Method::const_offset()));
 852   add(x12, x12, t0);
 853   la(x12, Address(x12, ConstMethod::codes_offset()));
 854   beq(x12, xbcp, verify_continue);
 855   // x10: method
 856   // xbcp: bcp // xbcp == 22
 857   // x13: mdp
 858   call_VM_leaf(CAST_FROM_FN_PTR(address, InterpreterRuntime::verify_mdp),
 859                x11, xbcp, x13);
 860   bind(verify_continue);
 861   ld(x10, Address(sp, 0));
 862   ld(x11, Address(sp, wordSize));
 863   ld(x12, Address(sp, 2 * wordSize));
 864   ld(x13, Address(sp, 3 * wordSize));
 865   addi(sp, sp, 4 * wordSize);
 866 #endif // ASSERT
 867 }
 868 
 869 
 870 void InterpreterMacroAssembler::set_mdp_data_at(Register mdp_in,
 871                                                 int constant,
 872                                                 Register value) {
 873   assert(ProfileInterpreter, "must be profiling interpreter");
 874   Address data(mdp_in, constant);
 875   sd(value, data);
 876 }
 877 
 878 
 879 void InterpreterMacroAssembler::increment_mdp_data_at(Register mdp_in,
 880                                                       int constant) {
 881   increment_mdp_data_at(mdp_in, noreg, constant);
 882 }
 883 
 884 void InterpreterMacroAssembler::increment_mdp_data_at(Register mdp_in,
 885                                                       Register index,
 886                                                       int constant) {
 887   assert(ProfileInterpreter, "must be profiling interpreter");
 888 
 889   assert_different_registers(t1, t0, mdp_in, index);
 890 
 891   Address addr1(mdp_in, constant);
 892   Address addr2(t1, 0);
 893   Address &addr = addr1;
 894   if (index != noreg) {
 895     la(t1, addr1);
 896     add(t1, t1, index);
 897     addr = addr2;
 898   }
 899 
 900   ld(t0, addr);
 901   addi(t0, t0, DataLayout::counter_increment);
 902   sd(t0, addr);
 903 }
 904 
 905 void InterpreterMacroAssembler::set_mdp_flag_at(Register mdp_in,
 906                                                 int flag_byte_constant) {
 907   assert(ProfileInterpreter, "must be profiling interpreter");
 908   int flags_offset = in_bytes(DataLayout::flags_offset());
 909   // Set the flag
 910   lbu(t1, Address(mdp_in, flags_offset));
 911   ori(t1, t1, flag_byte_constant);
 912   sb(t1, Address(mdp_in, flags_offset));
 913 }
 914 
 915 
 916 void InterpreterMacroAssembler::test_mdp_data_at(Register mdp_in,
 917                                                  int offset,
 918                                                  Register value,
 919                                                  Register test_value_out,
 920                                                  Label& not_equal_continue) {
 921   assert(ProfileInterpreter, "must be profiling interpreter");
 922   if (test_value_out == noreg) {
 923     ld(t1, Address(mdp_in, offset));
 924     bne(value, t1, not_equal_continue);
 925   } else {
 926     // Put the test value into a register, so caller can use it:
 927     ld(test_value_out, Address(mdp_in, offset));
 928     bne(value, test_value_out, not_equal_continue);
 929   }
 930 }
 931 
 932 
 933 void InterpreterMacroAssembler::update_mdp_by_offset(Register mdp_in,
 934                                                      int offset_of_disp) {
 935   assert(ProfileInterpreter, "must be profiling interpreter");
 936   ld(t1, Address(mdp_in, offset_of_disp));
 937   add(mdp_in, mdp_in, t1);
 938   sd(mdp_in, Address(fp, frame::interpreter_frame_mdp_offset * wordSize));
 939 }
 940 
 941 void InterpreterMacroAssembler::update_mdp_by_offset(Register mdp_in,
 942                                                      Register reg,
 943                                                      int offset_of_disp) {
 944   assert(ProfileInterpreter, "must be profiling interpreter");
 945   add(t1, mdp_in, reg);
 946   ld(t1, Address(t1, offset_of_disp));
 947   add(mdp_in, mdp_in, t1);
 948   sd(mdp_in, Address(fp, frame::interpreter_frame_mdp_offset * wordSize));
 949 }
 950 
 951 
 952 void InterpreterMacroAssembler::update_mdp_by_constant(Register mdp_in,
 953                                                        int constant) {
 954   assert(ProfileInterpreter, "must be profiling interpreter");
 955   add(mdp_in, mdp_in, (unsigned)constant);
 956   sd(mdp_in, Address(fp, frame::interpreter_frame_mdp_offset * wordSize));
 957 }
 958 
 959 
 960 void InterpreterMacroAssembler::update_mdp_for_ret(Register return_bci) {
 961   assert(ProfileInterpreter, "must be profiling interpreter");
 962 
 963   // save/restore across call_VM
 964   subi(sp, sp, 2 * wordSize);
 965   sd(zr, Address(sp, 0));
 966   sd(return_bci, Address(sp, wordSize));
 967   call_VM(noreg,
 968           CAST_FROM_FN_PTR(address, InterpreterRuntime::update_mdp_for_ret),
 969           return_bci);
 970   ld(zr, Address(sp, 0));
 971   ld(return_bci, Address(sp, wordSize));
 972   addi(sp, sp, 2 * wordSize);
 973 }
 974 
 975 void InterpreterMacroAssembler::profile_taken_branch(Register mdp) {
 976   if (ProfileInterpreter) {
 977     Label profile_continue;
 978 
 979     // If no method data exists, go to profile_continue.
 980     test_method_data_pointer(mdp, profile_continue);
 981 
 982     // We are taking a branch.  Increment the taken count.
 983     increment_mdp_data_at(mdp, in_bytes(JumpData::taken_offset()));
 984 
 985     // The method data pointer needs to be updated to reflect the new target.
 986     update_mdp_by_offset(mdp, in_bytes(JumpData::displacement_offset()));
 987     bind(profile_continue);
 988   }
 989 }
 990 
 991 void InterpreterMacroAssembler::profile_not_taken_branch(Register mdp) {
 992   if (ProfileInterpreter) {
 993     Label profile_continue;
 994 
 995     // If no method data exists, go to profile_continue.
 996     test_method_data_pointer(mdp, profile_continue);
 997 
 998     // We are not taking a branch.  Increment the not taken count.
 999     increment_mdp_data_at(mdp, in_bytes(BranchData::not_taken_offset()));
1000 
1001     // The method data pointer needs to be updated to correspond to
1002     // the next bytecode
1003     update_mdp_by_constant(mdp, in_bytes(BranchData::branch_data_size()));
1004     bind(profile_continue);
1005   }
1006 }
1007 
1008 void InterpreterMacroAssembler::profile_call(Register mdp) {
1009   if (ProfileInterpreter) {
1010     Label profile_continue;
1011 
1012     // If no method data exists, go to profile_continue.
1013     test_method_data_pointer(mdp, profile_continue);
1014 
1015     // We are making a call.  Increment the count.
1016     increment_mdp_data_at(mdp, in_bytes(CounterData::count_offset()));
1017 
1018     // The method data pointer needs to be updated to reflect the new target.
1019     update_mdp_by_constant(mdp, in_bytes(CounterData::counter_data_size()));
1020     bind(profile_continue);
1021   }
1022 }
1023 
1024 void InterpreterMacroAssembler::profile_final_call(Register mdp) {
1025   if (ProfileInterpreter) {
1026     Label profile_continue;
1027 
1028     // If no method data exists, go to profile_continue.
1029     test_method_data_pointer(mdp, profile_continue);
1030 
1031     // We are making a call.  Increment the count.
1032     increment_mdp_data_at(mdp, in_bytes(CounterData::count_offset()));
1033 
1034     // The method data pointer needs to be updated to reflect the new target.
1035     update_mdp_by_constant(mdp,
1036                            in_bytes(VirtualCallData::
1037                                     virtual_call_data_size()));
1038     bind(profile_continue);
1039   }
1040 }
1041 
1042 
1043 void InterpreterMacroAssembler::profile_virtual_call(Register receiver,
1044                                                      Register mdp,
1045                                                      Register reg2,
1046                                                      bool receiver_can_be_null) {
1047   if (ProfileInterpreter) {
1048     Label profile_continue;
1049 
1050     // If no method data exists, go to profile_continue.
1051     test_method_data_pointer(mdp, profile_continue);
1052 
1053     Label skip_receiver_profile;
1054     if (receiver_can_be_null) {
1055       Label not_null;
1056       // We are making a call.  Increment the count for null receiver.
1057       increment_mdp_data_at(mdp, in_bytes(CounterData::count_offset()));
1058       j(skip_receiver_profile);
1059       bind(not_null);
1060     }
1061 
1062     // Record the receiver type.
1063     record_klass_in_profile(receiver, mdp, reg2);
1064     bind(skip_receiver_profile);
1065 
1066     // The method data pointer needs to be updated to reflect the new target.
1067 
1068     update_mdp_by_constant(mdp,
1069                            in_bytes(VirtualCallData::
1070                                     virtual_call_data_size()));
1071     bind(profile_continue);
1072   }
1073 }
1074 
1075 // This routine creates a state machine for updating the multi-row
1076 // type profile at a virtual call site (or other type-sensitive bytecode).
1077 // The machine visits each row (of receiver/count) until the receiver type
1078 // is found, or until it runs out of rows.  At the same time, it remembers
1079 // the location of the first empty row.  (An empty row records null for its
1080 // receiver, and can be allocated for a newly-observed receiver type.)
1081 // Because there are two degrees of freedom in the state, a simple linear
1082 // search will not work; it must be a decision tree.  Hence this helper
1083 // function is recursive, to generate the required tree structured code.
1084 // It's the interpreter, so we are trading off code space for speed.
1085 // See below for example code.
1086 void InterpreterMacroAssembler::record_klass_in_profile_helper(
1087                                 Register receiver, Register mdp,
1088                                 Register reg2, Label& done) {
1089   if (TypeProfileWidth == 0) {
1090     increment_mdp_data_at(mdp, in_bytes(CounterData::count_offset()));
1091   } else {
1092     record_item_in_profile_helper(receiver, mdp, reg2, 0, done, TypeProfileWidth,
1093         &VirtualCallData::receiver_offset, &VirtualCallData::receiver_count_offset);
1094   }
1095 }
1096 
1097 void InterpreterMacroAssembler::record_item_in_profile_helper(Register item, Register mdp,
1098                                         Register reg2, int start_row, Label& done, int total_rows,
1099                                         OffsetFunction item_offset_fn, OffsetFunction item_count_offset_fn) {
1100   int last_row = total_rows - 1;
1101   assert(start_row <= last_row, "must be work left to do");
1102   // Test this row for both the item and for null.
1103   // Take any of three different outcomes:
1104   //   1. found item => increment count and goto done
1105   //   2. found null => keep looking for case 1, maybe allocate this cell
1106   //   3. found something else => keep looking for cases 1 and 2
1107   // Case 3 is handled by a recursive call.
1108   for (int row = start_row; row <= last_row; row++) {
1109     Label next_test;
1110     bool test_for_null_also = (row == start_row);
1111 
1112     // See if the item is item[n].
1113     int item_offset = in_bytes(item_offset_fn(row));
1114     test_mdp_data_at(mdp, item_offset, item,
1115                      (test_for_null_also ? reg2 : noreg),
1116                      next_test);
1117     // (Reg2 now contains the item from the CallData.)
1118 
1119     // The item is item[n].  Increment count[n].
1120     int count_offset = in_bytes(item_count_offset_fn(row));
1121     increment_mdp_data_at(mdp, count_offset);
1122     j(done);
1123     bind(next_test);
1124 
1125     if (test_for_null_also) {
1126       Label found_null;
1127       // Failed the equality check on item[n]...  Test for null.
1128       if (start_row == last_row) {
1129         // The only thing left to do is handle the null case.
1130         beqz(reg2, found_null);
1131         // Item did not match any saved item and there is no empty row for it.
1132         // Increment total counter to indicate polymorphic case.
1133         increment_mdp_data_at(mdp, in_bytes(CounterData::count_offset()));
1134         j(done);
1135         bind(found_null);
1136         break;
1137       }
1138       // Since null is rare, make it be the branch-taken case.
1139       beqz(reg2, found_null);
1140 
1141       // Put all the "Case 3" tests here.
1142       record_item_in_profile_helper(item, mdp, reg2, start_row + 1, done, total_rows,
1143           item_offset_fn, item_count_offset_fn);
1144 
1145       // Found a null.  Keep searching for a matching item,
1146       // but remember that this is an empty (unused) slot.
1147       bind(found_null);
1148     }
1149   }
1150 
1151   // In the fall-through case, we found no matching item, but we
1152   // observed the item[start_row] is null.
1153   // Fill in the item field and increment the count.
1154   int item_offset = in_bytes(item_offset_fn(start_row));
1155   set_mdp_data_at(mdp, item_offset, item);
1156   int count_offset = in_bytes(item_count_offset_fn(start_row));
1157   mv(reg2, DataLayout::counter_increment);
1158   set_mdp_data_at(mdp, count_offset, reg2);
1159   if (start_row > 0) {
1160     j(done);
1161   }
1162 }
1163 
1164 // Example state machine code for three profile rows:
1165 //   # main copy of decision tree, rooted at row[1]
1166 //   if (row[0].rec == rec) then [
1167 //     row[0].incr()
1168 //     goto done
1169 //   ]
1170 //   if (row[0].rec != nullptr) then [
1171 //     # inner copy of decision tree, rooted at row[1]
1172 //     if (row[1].rec == rec) then [
1173 //       row[1].incr()
1174 //       goto done
1175 //     ]
1176 //     if (row[1].rec != nullptr) then [
1177 //       # degenerate decision tree, rooted at row[2]
1178 //       if (row[2].rec == rec) then [
1179 //         row[2].incr()
1180 //         goto done
1181 //       ]
1182 //       if (row[2].rec != nullptr) then [
1183 //         count.incr()
1184 //         goto done
1185 //       ] # overflow
1186 //       row[2].init(rec)
1187 //       goto done
1188 //     ] else [
1189 //       # remember row[1] is empty
1190 //       if (row[2].rec == rec) then [
1191 //         row[2].incr()
1192 //         goto done
1193 //       ]
1194 //       row[1].init(rec)
1195 //       goto done
1196 //     ]
1197 //   else [
1198 //     # remember row[0] is empty
1199 //     if (row[1].rec == rec) then [
1200 //       row[1].incr()
1201 //       goto done
1202 //     ]
1203 //     if (row[2].rec == rec) then [
1204 //       row[2].incr()
1205 //       goto done
1206 //     ]
1207 //     row[0].init(rec)
1208 //     goto done
1209 //   ]
1210 //   done:
1211 
1212 void InterpreterMacroAssembler::record_klass_in_profile(Register receiver,
1213                                                         Register mdp, Register reg2) {
1214   assert(ProfileInterpreter, "must be profiling");
1215   Label done;
1216 
1217   record_klass_in_profile_helper(receiver, mdp, reg2, done);
1218 
1219   bind(done);
1220 }
1221 
1222 void InterpreterMacroAssembler::profile_ret(Register return_bci, Register mdp) {
1223   if (ProfileInterpreter) {
1224     Label profile_continue;
1225 
1226     // If no method data exists, go to profile_continue.
1227     test_method_data_pointer(mdp, profile_continue);
1228 
1229     // Update the total ret count.
1230     increment_mdp_data_at(mdp, in_bytes(CounterData::count_offset()));
1231 
1232     for (uint row = 0; row < RetData::row_limit(); row++) {
1233       Label next_test;
1234 
1235       // See if return_bci is equal to bci[n]:
1236       test_mdp_data_at(mdp,
1237                        in_bytes(RetData::bci_offset(row)),
1238                        return_bci, noreg,
1239                        next_test);
1240 
1241       // return_bci is equal to bci[n].  Increment the count.
1242       increment_mdp_data_at(mdp, in_bytes(RetData::bci_count_offset(row)));
1243 
1244       // The method data pointer needs to be updated to reflect the new target.
1245       update_mdp_by_offset(mdp,
1246                            in_bytes(RetData::bci_displacement_offset(row)));
1247       j(profile_continue);
1248       bind(next_test);
1249     }
1250 
1251     update_mdp_for_ret(return_bci);
1252 
1253     bind(profile_continue);
1254   }
1255 }
1256 
1257 void InterpreterMacroAssembler::profile_null_seen(Register mdp) {
1258   if (ProfileInterpreter) {
1259     Label profile_continue;
1260 
1261     // If no method data exists, go to profile_continue.
1262     test_method_data_pointer(mdp, profile_continue);
1263 
1264     set_mdp_flag_at(mdp, BitData::null_seen_byte_constant());
1265 
1266     // The method data pointer needs to be updated.
1267     int mdp_delta = in_bytes(BitData::bit_data_size());
1268     if (TypeProfileCasts) {
1269       mdp_delta = in_bytes(VirtualCallData::virtual_call_data_size());
1270     }
1271     update_mdp_by_constant(mdp, mdp_delta);
1272 
1273     bind(profile_continue);
1274   }
1275 }
1276 
1277 void InterpreterMacroAssembler::profile_typecheck(Register mdp, Register klass, Register reg2) {
1278   if (ProfileInterpreter) {
1279     Label profile_continue;
1280 
1281     // If no method data exists, go to profile_continue.
1282     test_method_data_pointer(mdp, profile_continue);
1283 
1284     // The method data pointer needs to be updated.
1285     int mdp_delta = in_bytes(BitData::bit_data_size());
1286     if (TypeProfileCasts) {
1287       mdp_delta = in_bytes(VirtualCallData::virtual_call_data_size());
1288 
1289       // Record the object type.
1290       record_klass_in_profile(klass, mdp, reg2);
1291     }
1292     update_mdp_by_constant(mdp, mdp_delta);
1293 
1294     bind(profile_continue);
1295   }
1296 }
1297 
1298 void InterpreterMacroAssembler::profile_switch_default(Register mdp) {
1299   if (ProfileInterpreter) {
1300     Label profile_continue;
1301 
1302     // If no method data exists, go to profile_continue.
1303     test_method_data_pointer(mdp, profile_continue);
1304 
1305     // Update the default case count
1306     increment_mdp_data_at(mdp,
1307                           in_bytes(MultiBranchData::default_count_offset()));
1308 
1309     // The method data pointer needs to be updated.
1310     update_mdp_by_offset(mdp,
1311                          in_bytes(MultiBranchData::
1312                                   default_displacement_offset()));
1313 
1314     bind(profile_continue);
1315   }
1316 }
1317 
1318 void InterpreterMacroAssembler::profile_switch_case(Register index,
1319                                                     Register mdp,
1320                                                     Register reg2) {
1321   if (ProfileInterpreter) {
1322     Label profile_continue;
1323 
1324     // If no method data exists, go to profile_continue.
1325     test_method_data_pointer(mdp, profile_continue);
1326 
1327     // Build the base (index * per_case_size_in_bytes()) +
1328     // case_array_offset_in_bytes()
1329     mv(reg2, in_bytes(MultiBranchData::per_case_size()));
1330     mv(t0, in_bytes(MultiBranchData::case_array_offset()));
1331     Assembler::mul(index, index, reg2);
1332     Assembler::add(index, index, t0);
1333 
1334     // Update the case count
1335     increment_mdp_data_at(mdp,
1336                           index,
1337                           in_bytes(MultiBranchData::relative_count_offset()));
1338 
1339     // The method data pointer need to be updated.
1340     update_mdp_by_offset(mdp,
1341                          index,
1342                          in_bytes(MultiBranchData::
1343                                   relative_displacement_offset()));
1344 
1345     bind(profile_continue);
1346   }
1347 }
1348 
1349 void InterpreterMacroAssembler::notify_method_entry() {
1350   // Whenever JVMTI is interp_only_mode, method entry/exit events are sent to
1351   // track stack depth.  If it is possible to enter interp_only_mode we add
1352   // the code to check if the event should be sent.
1353   if (JvmtiExport::can_post_interpreter_events()) {
1354     Label L;
1355     lwu(x13, Address(xthread, JavaThread::interp_only_mode_offset()));
1356     beqz(x13, L);
1357     call_VM(noreg, CAST_FROM_FN_PTR(address,
1358                                     InterpreterRuntime::post_method_entry));
1359     bind(L);
1360   }
1361 
1362   if (DTraceMethodProbes) {
1363     get_method(c_rarg1);
1364     call_VM_leaf(CAST_FROM_FN_PTR(address, SharedRuntime::dtrace_method_entry),
1365                  xthread, c_rarg1);
1366   }
1367 
1368   // RedefineClasses() tracing support for obsolete method entry
1369   if (log_is_enabled(Trace, redefine, class, obsolete)) {
1370     get_method(c_rarg1);
1371     call_VM_leaf(
1372       CAST_FROM_FN_PTR(address, SharedRuntime::rc_trace_method_entry),
1373       xthread, c_rarg1);
1374   }
1375 }
1376 
1377 
1378 void InterpreterMacroAssembler::notify_method_exit(
1379     TosState state, NotifyMethodExitMode mode) {
1380   // Whenever JVMTI is interp_only_mode, method entry/exit events are sent to
1381   // track stack depth.  If it is possible to enter interp_only_mode we add
1382   // the code to check if the event should be sent.
1383   if (mode == NotifyJVMTI && JvmtiExport::can_post_interpreter_events()) {
1384     Label L;
1385     // Note: frame::interpreter_frame_result has a dependency on how the
1386     // method result is saved across the call to post_method_exit. If this
1387     // is changed then the interpreter_frame_result implementation will
1388     // need to be updated too.
1389 
1390     // template interpreter will leave the result on the top of the stack.
1391     push(state);
1392     lwu(x13, Address(xthread, JavaThread::interp_only_mode_offset()));
1393     beqz(x13, L);
1394     call_VM(noreg,
1395             CAST_FROM_FN_PTR(address, InterpreterRuntime::post_method_exit));
1396     bind(L);
1397     pop(state);
1398   }
1399 
1400   if (DTraceMethodProbes) {
1401     push(state);
1402     get_method(c_rarg1);
1403     call_VM_leaf(CAST_FROM_FN_PTR(address, SharedRuntime::dtrace_method_exit),
1404                  xthread, c_rarg1);
1405     pop(state);
1406   }
1407 }
1408 
1409 
1410 // Jump if ((*counter_addr += increment) & mask) satisfies the condition.
1411 void InterpreterMacroAssembler::increment_mask_and_jump(Address counter_addr,
1412                                                         int increment, Address mask,
1413                                                         Register tmp1, Register tmp2,
1414                                                         bool preloaded, Label* where) {
1415   Label done;
1416   if (!preloaded) {
1417     lwu(tmp1, counter_addr);
1418   }
1419   add(tmp1, tmp1, increment);
1420   sw(tmp1, counter_addr);
1421   lwu(tmp2, mask);
1422   andr(tmp1, tmp1, tmp2);
1423   bnez(tmp1, done);
1424   j(*where); // offset is too large so we have to use j instead of beqz here
1425   bind(done);
1426 }
1427 
1428 void InterpreterMacroAssembler::call_VM_leaf_base(address entry_point,
1429                                                   int number_of_arguments) {
1430   // interpreter specific
1431   //
1432   // Note: No need to save/restore xbcp & xlocals pointer since these
1433   //       are callee saved registers and no blocking/ GC can happen
1434   //       in leaf calls.
1435 #ifdef ASSERT
1436   {
1437    Label L;
1438    ld(t0, Address(fp, frame::interpreter_frame_last_sp_offset * wordSize));
1439    beqz(t0, L);
1440    stop("InterpreterMacroAssembler::call_VM_leaf_base:"
1441         " last_sp isn't null");
1442    bind(L);
1443   }
1444 #endif /* ASSERT */
1445   // super call
1446   MacroAssembler::call_VM_leaf_base(entry_point, number_of_arguments);
1447 }
1448 
1449 void InterpreterMacroAssembler::call_VM_base(Register oop_result,
1450                                              Register java_thread,
1451                                              Register last_java_sp,
1452                                              Label*   return_pc,
1453                                              address  entry_point,
1454                                              int      number_of_arguments,
1455                                              bool     check_exceptions) {
1456   // interpreter specific
1457   //
1458   // Note: Could avoid restoring locals ptr (callee saved) - however doesn't
1459   //       really make a difference for these runtime calls, since they are
1460   //       slow anyway. Btw., bcp must be saved/restored since it may change
1461   //       due to GC.
1462   save_bcp();
1463 #ifdef ASSERT
1464   {
1465     Label L;
1466     ld(t0, Address(fp, frame::interpreter_frame_last_sp_offset * wordSize));
1467     beqz(t0, L);
1468     stop("InterpreterMacroAssembler::call_VM_base:"
1469          " last_sp isn't null");
1470     bind(L);
1471   }
1472 #endif /* ASSERT */
1473   // super call
1474   MacroAssembler::call_VM_base(oop_result, noreg, last_java_sp,
1475                                return_pc, entry_point,
1476                                number_of_arguments, check_exceptions);
1477   // interpreter specific
1478   restore_bcp();
1479   restore_locals();
1480 }
1481 
1482 void InterpreterMacroAssembler::call_VM_preemptable_helper(Register oop_result,
1483                                                            address entry_point,
1484                                                            int number_of_arguments,
1485                                                            bool check_exceptions) {
1486   assert(InterpreterRuntime::is_preemptable_call(entry_point),
1487          "VM call not preemptable, should use call_VM()");
1488   Label resume_pc, not_preempted;
1489 
1490 #ifdef ASSERT
1491   {
1492     Label L1, L2;
1493     ld(t0, Address(xthread, JavaThread::preempt_alternate_return_offset()));
1494     beqz(t0, L1);
1495     stop("call_VM_preemptable_helper: Should not have alternate return address set");
1496     bind(L1);
1497     // We check this counter in patch_return_pc_with_preempt_stub() during freeze.
1498     incrementw(Address(xthread, JavaThread::interp_at_preemptable_vmcall_cnt_offset()));
1499     lw(t0, Address(xthread, JavaThread::interp_at_preemptable_vmcall_cnt_offset()));
1500     bgtz(t0, L2);
1501     stop("call_VM_preemptable_helper: should be > 0");
1502     bind(L2);
1503   }
1504 #endif /* ASSERT */
1505 
1506   // Force freeze slow path.
1507   push_cont_fastpath();
1508 
1509   // Make VM call. In case of preemption set last_pc to the one we want to resume to.
1510   // Note: call_VM_base will use resume_pc label to set last_Java_pc.
1511   call_VM_base(noreg, noreg, noreg, &resume_pc, entry_point, number_of_arguments, false /*check_exceptions*/);
1512 
1513   pop_cont_fastpath();
1514 
1515 #ifdef ASSERT
1516   {
1517     Label L;
1518     decrementw(Address(xthread, JavaThread::interp_at_preemptable_vmcall_cnt_offset()));
1519     lw(t0, Address(xthread, JavaThread::interp_at_preemptable_vmcall_cnt_offset()));
1520     bgez(t0, L);
1521     stop("call_VM_preemptable_helper: should be >= 0");
1522     bind(L);
1523   }
1524 #endif /* ASSERT */
1525 
1526   // Check if preempted.
1527   ld(t1, Address(xthread, JavaThread::preempt_alternate_return_offset()));
1528   beqz(t1, not_preempted);
1529   sd(zr, Address(xthread, JavaThread::preempt_alternate_return_offset()));
1530   jr(t1);
1531 
1532   // In case of preemption, this is where we will resume once we finally acquire the monitor.
1533   bind(resume_pc);
1534   restore_after_resume(false /* is_native */);
1535 
1536   bind(not_preempted);
1537   if (check_exceptions) {
1538     // check for pending exceptions
1539     ld(t0, Address(xthread, in_bytes(Thread::pending_exception_offset())));
1540     Label ok;
1541     beqz(t0, ok);
1542     la(t1, RuntimeAddress(StubRoutines::forward_exception_entry()));
1543     jr(t1);
1544     bind(ok);
1545   }
1546 
1547   // get oop result if there is one and reset the value in the thread
1548   if (oop_result->is_valid()) {
1549     get_vm_result_oop(oop_result, xthread);
1550   }
1551 }
1552 
1553 static void pass_arg1(MacroAssembler* masm, Register arg) {
1554   if (c_rarg1 != arg) {
1555     masm->mv(c_rarg1, arg);
1556   }
1557 }
1558 
1559 static void pass_arg2(MacroAssembler* masm, Register arg) {
1560   if (c_rarg2 != arg) {
1561     masm->mv(c_rarg2, arg);
1562   }
1563 }
1564 
1565 void InterpreterMacroAssembler::call_VM_preemptable(Register oop_result,
1566                                          address entry_point,
1567                                          Register arg_1,
1568                                          bool check_exceptions) {
1569   pass_arg1(this, arg_1);
1570   call_VM_preemptable_helper(oop_result, entry_point, 1, check_exceptions);
1571 }
1572 
1573 void InterpreterMacroAssembler::call_VM_preemptable(Register oop_result,
1574                                          address entry_point,
1575                                          Register arg_1,
1576                                          Register arg_2,
1577                                          bool check_exceptions) {
1578   LP64_ONLY(assert_different_registers(arg_1, c_rarg2));
1579   pass_arg2(this, arg_2);
1580   pass_arg1(this, arg_1);
1581   call_VM_preemptable_helper(oop_result, entry_point, 2, check_exceptions);
1582 }
1583 
1584 void InterpreterMacroAssembler::restore_after_resume(bool is_native) {
1585   la(t1, ExternalAddress(Interpreter::cont_resume_interpreter_adapter()));
1586   jalr(t1);
1587   if (is_native) {
1588     // On resume we need to set up stack as expected
1589     push(dtos);
1590     push(ltos);
1591   }
1592 }
1593 
1594 void InterpreterMacroAssembler::profile_obj_type(Register obj, const Address& mdo_addr, Register tmp) {
1595   assert_different_registers(obj, tmp, t0, mdo_addr.base());
1596   Label update, next, none;
1597 
1598   verify_oop(obj);
1599 
1600   bnez(obj, update);
1601   orptr(mdo_addr, TypeEntries::null_seen, t0, tmp);
1602   j(next);
1603 
1604   bind(update);
1605   load_klass(obj, obj);
1606 
1607   ld(tmp, mdo_addr);
1608   xorr(obj, obj, tmp);
1609   andi(t0, obj, TypeEntries::type_klass_mask);
1610   beqz(t0, next); // klass seen before, nothing to
1611                   // do. The unknown bit may have been
1612                   // set already but no need to check.
1613 
1614   test_bit(t0, obj, exact_log2(TypeEntries::type_unknown));
1615   bnez(t0, next);
1616   // already unknown. Nothing to do anymore.
1617 
1618   beqz(tmp, none);
1619   mv(t0, (u1)TypeEntries::null_seen);
1620   beq(tmp, t0, none);
1621   // There is a chance that the checks above
1622   // fail if another thread has just set the
1623   // profiling to this obj's klass
1624   xorr(obj, obj, tmp); // get back original value before XOR
1625   ld(tmp, mdo_addr);
1626   xorr(obj, obj, tmp);
1627   andi(t0, obj, TypeEntries::type_klass_mask);
1628   beqz(t0, next);
1629 
1630   // different than before. Cannot keep accurate profile.
1631   orptr(mdo_addr, TypeEntries::type_unknown, t0, tmp);
1632   j(next);
1633 
1634   bind(none);
1635   // first time here. Set profile type.
1636   sd(obj, mdo_addr);
1637 #ifdef ASSERT
1638   andi(obj, obj, TypeEntries::type_mask);
1639   verify_klass_ptr(obj);
1640 #endif
1641 
1642   bind(next);
1643 }
1644 
1645 void InterpreterMacroAssembler::profile_arguments_type(Register mdp, Register callee, Register tmp, bool is_virtual) {
1646   if (!ProfileInterpreter) {
1647     return;
1648   }
1649 
1650   if (MethodData::profile_arguments() || MethodData::profile_return()) {
1651     Label profile_continue;
1652 
1653     test_method_data_pointer(mdp, profile_continue);
1654 
1655     int off_to_start = is_virtual ? in_bytes(VirtualCallData::virtual_call_data_size()) : in_bytes(CounterData::counter_data_size());
1656 
1657     lbu(t0, Address(mdp, in_bytes(DataLayout::tag_offset()) - off_to_start));
1658     if (is_virtual) {
1659       mv(tmp, (u1)DataLayout::virtual_call_type_data_tag);
1660       bne(t0, tmp, profile_continue);
1661     } else {
1662       mv(tmp, (u1)DataLayout::call_type_data_tag);
1663       bne(t0, tmp, profile_continue);
1664     }
1665 
1666     // calculate slot step
1667     static int stack_slot_offset0 = in_bytes(TypeEntriesAtCall::stack_slot_offset(0));
1668     static int slot_step = in_bytes(TypeEntriesAtCall::stack_slot_offset(1)) - stack_slot_offset0;
1669 
1670     // calculate type step
1671     static int argument_type_offset0 = in_bytes(TypeEntriesAtCall::argument_type_offset(0));
1672     static int type_step = in_bytes(TypeEntriesAtCall::argument_type_offset(1)) - argument_type_offset0;
1673 
1674     if (MethodData::profile_arguments()) {
1675       Label done, loop, loopEnd, profileArgument, profileReturnType;
1676       RegSet pushed_registers;
1677       pushed_registers += x15;
1678       pushed_registers += x16;
1679       pushed_registers += x17;
1680       Register mdo_addr = x15;
1681       Register index = x16;
1682       Register off_to_args = x17;
1683       push_reg(pushed_registers, sp);
1684 
1685       mv(off_to_args, in_bytes(TypeEntriesAtCall::args_data_offset()));
1686       mv(t0, TypeProfileArgsLimit);
1687       beqz(t0, loopEnd);
1688 
1689       mv(index, zr); // index < TypeProfileArgsLimit
1690       bind(loop);
1691       bgtz(index, profileReturnType);
1692       mv(t0, (int)MethodData::profile_return());
1693       beqz(t0, profileArgument); // (index > 0 || MethodData::profile_return()) == false
1694       bind(profileReturnType);
1695       // If return value type is profiled we may have no argument to profile
1696       ld(tmp, Address(mdp, in_bytes(TypeEntriesAtCall::cell_count_offset())));
1697       mv(t1, - TypeStackSlotEntries::per_arg_count());
1698       mul(t1, index, t1);
1699       add(tmp, tmp, t1);
1700       mv(t1, TypeStackSlotEntries::per_arg_count());
1701       add(t0, mdp, off_to_args);
1702       blt(tmp, t1, done);
1703 
1704       bind(profileArgument);
1705 
1706       ld(tmp, Address(callee, Method::const_offset()));
1707       load_unsigned_short(tmp, Address(tmp, ConstMethod::size_of_parameters_offset()));
1708       // stack offset o (zero based) from the start of the argument
1709       // list, for n arguments translates into offset n - o - 1 from
1710       // the end of the argument list
1711       mv(t0, stack_slot_offset0);
1712       mv(t1, slot_step);
1713       mul(t1, index, t1);
1714       add(t0, t0, t1);
1715       add(t0, mdp, t0);
1716       ld(t0, Address(t0));
1717       sub(tmp, tmp, t0);
1718       subi(tmp, tmp, 1);
1719       Address arg_addr = argument_address(tmp);
1720       ld(tmp, arg_addr);
1721 
1722       mv(t0, argument_type_offset0);
1723       mv(t1, type_step);
1724       mul(t1, index, t1);
1725       add(t0, t0, t1);
1726       add(mdo_addr, mdp, t0);
1727       Address mdo_arg_addr(mdo_addr, 0);
1728       profile_obj_type(tmp, mdo_arg_addr, t1);
1729 
1730       int to_add = in_bytes(TypeStackSlotEntries::per_arg_size());
1731       addi(off_to_args, off_to_args, to_add);
1732 
1733       // increment index by 1
1734       addi(index, index, 1);
1735       mv(t1, TypeProfileArgsLimit);
1736       blt(index, t1, loop);
1737       bind(loopEnd);
1738 
1739       if (MethodData::profile_return()) {
1740         ld(tmp, Address(mdp, in_bytes(TypeEntriesAtCall::cell_count_offset())));
1741         sub(tmp, tmp, TypeProfileArgsLimit * TypeStackSlotEntries::per_arg_count());
1742       }
1743 
1744       add(t0, mdp, off_to_args);
1745       bind(done);
1746       mv(mdp, t0);
1747 
1748       // unspill the clobbered registers
1749       pop_reg(pushed_registers, sp);
1750 
1751       if (MethodData::profile_return()) {
1752         // We're right after the type profile for the last
1753         // argument. tmp is the number of cells left in the
1754         // CallTypeData/VirtualCallTypeData to reach its end. Non null
1755         // if there's a return to profile.
1756         assert(ReturnTypeEntry::static_cell_count() < TypeStackSlotEntries::per_arg_count(), "can't move past ret type");
1757         shadd(mdp, tmp, mdp, tmp, exact_log2(DataLayout::cell_size));
1758       }
1759       sd(mdp, Address(fp, frame::interpreter_frame_mdp_offset * wordSize));
1760     } else {
1761       assert(MethodData::profile_return(), "either profile call args or call ret");
1762       update_mdp_by_constant(mdp, in_bytes(TypeEntriesAtCall::return_only_size()));
1763     }
1764 
1765     // mdp points right after the end of the
1766     // CallTypeData/VirtualCallTypeData, right after the cells for the
1767     // return value type if there's one
1768 
1769     bind(profile_continue);
1770   }
1771 }
1772 
1773 void InterpreterMacroAssembler::profile_return_type(Register mdp, Register ret, Register tmp) {
1774   assert_different_registers(mdp, ret, tmp, xbcp, t0, t1);
1775   if (ProfileInterpreter && MethodData::profile_return()) {
1776     Label profile_continue, done;
1777 
1778     test_method_data_pointer(mdp, profile_continue);
1779 
1780     if (MethodData::profile_return_jsr292_only()) {
1781       assert(Method::intrinsic_id_size_in_bytes() == 2, "assuming Method::_intrinsic_id is u2");
1782 
1783       // If we don't profile all invoke bytecodes we must make sure
1784       // it's a bytecode we indeed profile. We can't go back to the
1785       // beginning of the ProfileData we intend to update to check its
1786       // type because we're right after it and we don't known its
1787       // length
1788       Label do_profile;
1789       lbu(t0, Address(xbcp, 0));
1790       mv(tmp, (u1)Bytecodes::_invokedynamic);
1791       beq(t0, tmp, do_profile);
1792       mv(tmp, (u1)Bytecodes::_invokehandle);
1793       beq(t0, tmp, do_profile);
1794       get_method(tmp);
1795       lhu(t0, Address(tmp, Method::intrinsic_id_offset()));
1796       mv(t1, static_cast<int>(vmIntrinsics::_compiledLambdaForm));
1797       bne(t0, t1, profile_continue);
1798       bind(do_profile);
1799     }
1800 
1801     Address mdo_ret_addr(mdp, -in_bytes(ReturnTypeEntry::size()));
1802     mv(tmp, ret);
1803     profile_obj_type(tmp, mdo_ret_addr, t1);
1804 
1805     bind(profile_continue);
1806   }
1807 }
1808 
1809 void InterpreterMacroAssembler::profile_parameters_type(Register mdp, Register tmp1, Register tmp2, Register tmp3) {
1810   assert_different_registers(t0, t1, mdp, tmp1, tmp2, tmp3);
1811   if (ProfileInterpreter && MethodData::profile_parameters()) {
1812     Label profile_continue, done;
1813 
1814     test_method_data_pointer(mdp, profile_continue);
1815 
1816     // Load the offset of the area within the MDO used for
1817     // parameters. If it's negative we're not profiling any parameters
1818     lwu(tmp1, Address(mdp, in_bytes(MethodData::parameters_type_data_di_offset()) - in_bytes(MethodData::data_offset())));
1819     srli(tmp2, tmp1, 31);
1820     bnez(tmp2, profile_continue);  // i.e. sign bit set
1821 
1822     // Compute a pointer to the area for parameters from the offset
1823     // and move the pointer to the slot for the last
1824     // parameters. Collect profiling from last parameter down.
1825     // mdo start + parameters offset + array length - 1
1826     add(mdp, mdp, tmp1);
1827     ld(tmp1, Address(mdp, ArrayData::array_len_offset()));
1828     subi(tmp1, tmp1, TypeStackSlotEntries::per_arg_count());
1829 
1830     Label loop;
1831     bind(loop);
1832 
1833     int off_base = in_bytes(ParametersTypeData::stack_slot_offset(0));
1834     int type_base = in_bytes(ParametersTypeData::type_offset(0));
1835     int per_arg_scale = exact_log2(DataLayout::cell_size);
1836     add(t0, mdp, off_base);
1837     add(t1, mdp, type_base);
1838 
1839     shadd(tmp2, tmp1, t0, tmp2, per_arg_scale);
1840     // load offset on the stack from the slot for this parameter
1841     ld(tmp2, Address(tmp2, 0));
1842     neg(tmp2, tmp2);
1843 
1844     // read the parameter from the local area
1845     shadd(tmp2, tmp2, xlocals, tmp2, Interpreter::logStackElementSize);
1846     ld(tmp2, Address(tmp2, 0));
1847 
1848     // profile the parameter
1849     shadd(t1, tmp1, t1, t0, per_arg_scale);
1850     Address arg_type(t1, 0);
1851     profile_obj_type(tmp2, arg_type, tmp3);
1852 
1853     // go to next parameter
1854     subi(tmp1, tmp1, TypeStackSlotEntries::per_arg_count());
1855     bgez(tmp1, loop);
1856 
1857     bind(profile_continue);
1858   }
1859 }
1860 
1861 void InterpreterMacroAssembler::load_resolved_indy_entry(Register cache, Register index) {
1862   // Get index out of bytecode pointer, get_cache_entry_pointer_at_bcp
1863   // register "cache" is trashed in next ld, so lets use it as a temporary register
1864   get_cache_index_at_bcp(index, cache, 1, sizeof(u4));
1865   // Get address of invokedynamic array
1866   ld(cache, Address(xcpool, in_bytes(ConstantPoolCache::invokedynamic_entries_offset())));
1867   // Scale the index to be the entry index * sizeof(ResolvedIndyEntry)
1868   slli(index, index, log2i_exact(sizeof(ResolvedIndyEntry)));
1869   addi(cache, cache, Array<ResolvedIndyEntry>::base_offset_in_bytes());
1870   add(cache, cache, index);
1871 }
1872 
1873 void InterpreterMacroAssembler::load_field_entry(Register cache, Register index, int bcp_offset) {
1874   // Get index out of bytecode pointer
1875   get_cache_index_at_bcp(index, cache, bcp_offset, sizeof(u2));
1876   // Take shortcut if the size is a power of 2
1877   if (is_power_of_2(sizeof(ResolvedFieldEntry))) {
1878     slli(index, index, log2i_exact(sizeof(ResolvedFieldEntry))); // Scale index by power of 2
1879   } else {
1880     mv(cache, sizeof(ResolvedFieldEntry));
1881     mul(index, index, cache); // Scale the index to be the entry index * sizeof(ResolvedIndyEntry)
1882   }
1883   // Get address of field entries array
1884   ld(cache, Address(xcpool, ConstantPoolCache::field_entries_offset()));
1885   addi(cache, cache, Array<ResolvedIndyEntry>::base_offset_in_bytes());
1886   add(cache, cache, index);
1887   // Prevents stale data from being read after the bytecode is patched to the fast bytecode
1888   membar(MacroAssembler::LoadLoad);
1889 }
1890 
1891 void InterpreterMacroAssembler::get_method_counters(Register method,
1892                                                     Register mcs, Label& skip) {
1893   Label has_counters;
1894   ld(mcs, Address(method, Method::method_counters_offset()));
1895   bnez(mcs, has_counters);
1896   call_VM(noreg, CAST_FROM_FN_PTR(address,
1897           InterpreterRuntime::build_method_counters), method);
1898   ld(mcs, Address(method, Method::method_counters_offset()));
1899   beqz(mcs, skip); // No MethodCounters allocated, OutOfMemory
1900   bind(has_counters);
1901 }
1902 
1903 void InterpreterMacroAssembler::load_method_entry(Register cache, Register index, int bcp_offset) {
1904   // Get index out of bytecode pointer
1905   get_cache_index_at_bcp(index, cache, bcp_offset, sizeof(u2));
1906   mv(cache, sizeof(ResolvedMethodEntry));
1907   mul(index, index, cache); // Scale the index to be the entry index * sizeof(ResolvedMethodEntry)
1908 
1909   // Get address of field entries array
1910   ld(cache, Address(xcpool, ConstantPoolCache::method_entries_offset()));
1911   addi(cache, cache, Array<ResolvedMethodEntry>::base_offset_in_bytes());
1912   add(cache, cache, index);
1913 }
1914 
1915 #ifdef ASSERT
1916 void InterpreterMacroAssembler::verify_field_offset(Register reg) {
1917   // Verify the field offset is not in the header, implicitly checks for 0
1918   Label L;
1919   mv(t0, oopDesc::base_offset_in_bytes());
1920   bge(reg, t0, L);
1921   stop("bad field offset");
1922   bind(L);
1923 }
1924 
1925 void InterpreterMacroAssembler::verify_access_flags(Register access_flags, uint32_t flag,
1926                                                     const char* msg, bool stop_by_hit) {
1927   Label L;
1928   test_bit(t0, access_flags, exact_log2(flag));
1929   if (stop_by_hit) {
1930     beqz(t0, L);
1931   } else {
1932     bnez(t0, L);
1933   }
1934   stop(msg);
1935   bind(L);
1936 }
1937 
1938 void InterpreterMacroAssembler::verify_frame_setup() {
1939   Label L;
1940   const Address monitor_block_top(fp, frame::interpreter_frame_monitor_block_top_offset * wordSize);
1941   ld(t0, monitor_block_top);
1942   shadd(t0, t0, fp, t0, LogBytesPerWord);
1943   beq(esp, t0, L);
1944   stop("broken stack frame setup in interpreter");
1945   bind(L);
1946 }
1947 #endif