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