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