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