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