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