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