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