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