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