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