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