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