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