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