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