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 
 661   if (StackReservedPages > 0) {
 662     // testing if reserved zone needs to be re-enabled
 663     Label no_reserved_zone_enabling;
 664 
 665     ldr(rscratch1, Address(rthread, JavaThread::reserved_stack_activation_offset()));
 666     cmp(esp, rscratch1);
 667     br(Assembler::LS, no_reserved_zone_enabling);
 668 
 669     call_VM_leaf(
 670       CAST_FROM_FN_PTR(address, SharedRuntime::enable_stack_reserved_zone), rthread);
 671     call_VM(noreg, CAST_FROM_FN_PTR(address,
 672                    InterpreterRuntime::throw_delayed_StackOverflowError));
 673     should_not_reach_here();
 674 
 675     bind(no_reserved_zone_enabling);
 676   }
 677 
 678 
 679   if (state == atos && ValueTypeReturnedAsFields) {
 680     Label skip;
 681     // Test if the return type is a value type
 682     ldr(rscratch1, Address(rfp, frame::interpreter_frame_method_offset * wordSize));
 683     ldr(rscratch1, Address(rscratch1, Method::const_offset()));
 684     ldrb(rscratch1, Address(rscratch1, ConstMethod::result_type_offset()));
 685     cmpw(rscratch1, (u1) T_VALUETYPE);
 686     br(Assembler::NE, skip);
 687 
 688     // We are returning a value type, load its fields into registers
 689     // Load fields from a buffered value with a value class specific handler
 690 
 691     load_klass(rscratch1 /*dst*/, r0 /*src*/);
 692     ldr(rscratch1, Address(rscratch1, InstanceKlass::adr_valueklass_fixed_block_offset()));
 693     ldr(rscratch1, Address(rscratch1, ValueKlass::unpack_handler_offset()));
 694     cbz(rscratch1, skip); 
 695 
 696     blr(rscratch1);
 697 
 698     // call above kills the value in r1. Reload it.
 699     ldr(r1, Address(rfp, frame::interpreter_frame_sender_sp_offset * wordSize));
 700     bind(skip);
 701   }
 702 
 703 
 704   // remove frame anchor
 705   leave();
 706   // If we're returning to interpreted code we will shortly be
 707   // adjusting SP to allow some space for ESP.  If we're returning to
 708   // compiled code the saved sender SP was saved in sender_sp, so this
 709   // restores it.
 710   andr(sp, esp, -16);
 711 }
 712 
 713 // Lock object
 714 //
 715 // Args:
 716 //      c_rarg1: BasicObjectLock to be used for locking
 717 //
 718 // Kills:
 719 //      r0
 720 //      c_rarg0, c_rarg1, c_rarg2, c_rarg3, .. (param regs)
 721 //      rscratch1, rscratch2 (scratch regs)
 722 void InterpreterMacroAssembler::lock_object(Register lock_reg)
 723 {
 724   assert(lock_reg == c_rarg1, "The argument is only for looks. It must be c_rarg1");
 725   if (UseHeavyMonitors) {
 726     call_VM(noreg,
 727             CAST_FROM_FN_PTR(address, InterpreterRuntime::monitorenter),
 728             lock_reg);
 729   } else {
 730     Label done;
 731 
 732     const Register swap_reg = r0;
 733     const Register tmp = c_rarg2;
 734     const Register obj_reg = c_rarg3; // Will contain the oop
 735 
 736     const int obj_offset = BasicObjectLock::obj_offset_in_bytes();
 737     const int lock_offset = BasicObjectLock::lock_offset_in_bytes ();
 738     const int mark_offset = lock_offset +
 739                             BasicLock::displaced_header_offset_in_bytes();
 740 
 741     Label slow_case;
 742 
 743     // Load object pointer into obj_reg %c_rarg3
 744     ldr(obj_reg, Address(lock_reg, obj_offset));
 745 
 746     if (UseBiasedLocking) {
 747       biased_locking_enter(lock_reg, obj_reg, swap_reg, tmp, false, done, &slow_case);
 748     }
 749 
 750     // Load (object->mark() | 1) into swap_reg
 751     ldr(rscratch1, Address(obj_reg, oopDesc::mark_offset_in_bytes()));
 752     orr(swap_reg, rscratch1, 1);
 753 
 754     // Save (object->mark() | 1) into BasicLock's displaced header
 755     str(swap_reg, Address(lock_reg, mark_offset));
 756 
 757     if (EnableValhalla && !UseBiasedLocking) { 
 758       // For slow path is_always_locked, using biased, which is never natural for !UseBiasLocking
 759       andr(swap_reg, swap_reg, ~markOopDesc::biased_lock_bit_in_place);
 760     }
 761 
 762     assert(lock_offset == 0,
 763            "displached header must be first word in BasicObjectLock");
 764 
 765     Label fail;
 766     if (PrintBiasedLockingStatistics) {
 767       Label fast;
 768       cmpxchg_obj_header(swap_reg, lock_reg, obj_reg, rscratch1, fast, &fail);
 769       bind(fast);
 770       atomic_incw(Address((address)BiasedLocking::fast_path_entry_count_addr()),
 771                   rscratch2, rscratch1, tmp);
 772       b(done);
 773       bind(fail);
 774     } else {
 775       cmpxchg_obj_header(swap_reg, lock_reg, obj_reg, rscratch1, done, /*fallthrough*/NULL);
 776     }
 777 
 778     // Test if the oopMark is an obvious stack pointer, i.e.,
 779     //  1) (mark & 7) == 0, and
 780     //  2) rsp <= mark < mark + os::pagesize()
 781     //
 782     // These 3 tests can be done by evaluating the following
 783     // expression: ((mark - rsp) & (7 - os::vm_page_size())),
 784     // assuming both stack pointer and pagesize have their
 785     // least significant 3 bits clear.
 786     // NOTE: the oopMark is in swap_reg %r0 as the result of cmpxchg
 787     // NOTE2: aarch64 does not like to subtract sp from rn so take a
 788     // copy
 789     mov(rscratch1, sp);
 790     sub(swap_reg, swap_reg, rscratch1);
 791     ands(swap_reg, swap_reg, (unsigned long)(7 - os::vm_page_size()));
 792 
 793     // Save the test result, for recursive case, the result is zero
 794     str(swap_reg, Address(lock_reg, mark_offset));
 795 
 796     if (PrintBiasedLockingStatistics) {
 797       br(Assembler::NE, slow_case);
 798       atomic_incw(Address((address)BiasedLocking::fast_path_entry_count_addr()),
 799                   rscratch2, rscratch1, tmp);
 800     }
 801     br(Assembler::EQ, done);
 802 
 803     bind(slow_case);
 804 
 805     // Call the runtime routine for slow case
 806     call_VM(noreg,
 807             CAST_FROM_FN_PTR(address, InterpreterRuntime::monitorenter),
 808             lock_reg);
 809 
 810     bind(done);
 811   }
 812 }
 813 
 814 
 815 // Unlocks an object. Used in monitorexit bytecode and
 816 // remove_activation.  Throws an IllegalMonitorException if object is
 817 // not locked by current thread.
 818 //
 819 // Args:
 820 //      c_rarg1: BasicObjectLock for lock
 821 //
 822 // Kills:
 823 //      r0
 824 //      c_rarg0, c_rarg1, c_rarg2, c_rarg3, ... (param regs)
 825 //      rscratch1, rscratch2 (scratch regs)
 826 void InterpreterMacroAssembler::unlock_object(Register lock_reg)
 827 {
 828   assert(lock_reg == c_rarg1, "The argument is only for looks. It must be rarg1");
 829 
 830   if (UseHeavyMonitors) {
 831     call_VM(noreg,
 832             CAST_FROM_FN_PTR(address, InterpreterRuntime::monitorexit),
 833             lock_reg);
 834   } else {
 835     Label done;
 836 
 837     const Register swap_reg   = r0;
 838     const Register header_reg = c_rarg2;  // Will contain the old oopMark
 839     const Register obj_reg    = c_rarg3;  // Will contain the oop
 840 
 841     save_bcp(); // Save in case of exception
 842 
 843     // Convert from BasicObjectLock structure to object and BasicLock
 844     // structure Store the BasicLock address into %r0
 845     lea(swap_reg, Address(lock_reg, BasicObjectLock::lock_offset_in_bytes()));
 846 
 847     // Load oop into obj_reg(%c_rarg3)
 848     ldr(obj_reg, Address(lock_reg, BasicObjectLock::obj_offset_in_bytes()));
 849 
 850     // Free entry
 851     str(zr, Address(lock_reg, BasicObjectLock::obj_offset_in_bytes()));
 852 
 853     if (UseBiasedLocking) {
 854       biased_locking_exit(obj_reg, header_reg, done);
 855     }
 856 
 857     // Load the old header from BasicLock structure
 858     ldr(header_reg, Address(swap_reg,
 859                             BasicLock::displaced_header_offset_in_bytes()));
 860 
 861     // Test for recursion
 862     cbz(header_reg, done);
 863 
 864     // Atomic swap back the old header
 865     cmpxchg_obj_header(swap_reg, header_reg, obj_reg, rscratch1, done, /*fallthrough*/NULL);
 866 
 867     // Call the runtime routine for slow case.
 868     str(obj_reg, Address(lock_reg, BasicObjectLock::obj_offset_in_bytes())); // restore obj
 869     call_VM(noreg,
 870             CAST_FROM_FN_PTR(address, InterpreterRuntime::monitorexit),
 871             lock_reg);
 872 
 873     bind(done);
 874 
 875     restore_bcp();
 876   }
 877 }
 878 
 879 void InterpreterMacroAssembler::test_method_data_pointer(Register mdp,
 880                                                          Label& zero_continue) {
 881   assert(ProfileInterpreter, "must be profiling interpreter");
 882   ldr(mdp, Address(rfp, frame::interpreter_frame_mdp_offset * wordSize));
 883   cbz(mdp, zero_continue);
 884 }
 885 
 886 // Set the method data pointer for the current bcp.
 887 void InterpreterMacroAssembler::set_method_data_pointer_for_bcp() {
 888   assert(ProfileInterpreter, "must be profiling interpreter");
 889   Label set_mdp;
 890   stp(r0, r1, Address(pre(sp, -2 * wordSize)));
 891 
 892   // Test MDO to avoid the call if it is NULL.
 893   ldr(r0, Address(rmethod, in_bytes(Method::method_data_offset())));
 894   cbz(r0, set_mdp);
 895   call_VM_leaf(CAST_FROM_FN_PTR(address, InterpreterRuntime::bcp_to_di), rmethod, rbcp);
 896   // r0: mdi
 897   // mdo is guaranteed to be non-zero here, we checked for it before the call.
 898   ldr(r1, Address(rmethod, in_bytes(Method::method_data_offset())));
 899   lea(r1, Address(r1, in_bytes(MethodData::data_offset())));
 900   add(r0, r1, r0);
 901   str(r0, Address(rfp, frame::interpreter_frame_mdp_offset * wordSize));
 902   bind(set_mdp);
 903   ldp(r0, r1, Address(post(sp, 2 * wordSize)));
 904 }
 905 
 906 void InterpreterMacroAssembler::verify_method_data_pointer() {
 907   assert(ProfileInterpreter, "must be profiling interpreter");
 908 #ifdef ASSERT
 909   Label verify_continue;
 910   stp(r0, r1, Address(pre(sp, -2 * wordSize)));
 911   stp(r2, r3, Address(pre(sp, -2 * wordSize)));
 912   test_method_data_pointer(r3, verify_continue); // If mdp is zero, continue
 913   get_method(r1);
 914 
 915   // If the mdp is valid, it will point to a DataLayout header which is
 916   // consistent with the bcp.  The converse is highly probable also.
 917   ldrsh(r2, Address(r3, in_bytes(DataLayout::bci_offset())));
 918   ldr(rscratch1, Address(r1, Method::const_offset()));
 919   add(r2, r2, rscratch1, Assembler::LSL);
 920   lea(r2, Address(r2, ConstMethod::codes_offset()));
 921   cmp(r2, rbcp);
 922   br(Assembler::EQ, verify_continue);
 923   // r1: method
 924   // rbcp: bcp // rbcp == 22
 925   // r3: mdp
 926   call_VM_leaf(CAST_FROM_FN_PTR(address, InterpreterRuntime::verify_mdp),
 927                r1, rbcp, r3);
 928   bind(verify_continue);
 929   ldp(r2, r3, Address(post(sp, 2 * wordSize)));
 930   ldp(r0, r1, Address(post(sp, 2 * wordSize)));
 931 #endif // ASSERT
 932 }
 933 
 934 
 935 void InterpreterMacroAssembler::set_mdp_data_at(Register mdp_in,
 936                                                 int constant,
 937                                                 Register value) {
 938   assert(ProfileInterpreter, "must be profiling interpreter");
 939   Address data(mdp_in, constant);
 940   str(value, data);
 941 }
 942 
 943 
 944 void InterpreterMacroAssembler::increment_mdp_data_at(Register mdp_in,
 945                                                       int constant,
 946                                                       bool decrement) {
 947   increment_mdp_data_at(mdp_in, noreg, constant, decrement);
 948 }
 949 
 950 void InterpreterMacroAssembler::increment_mdp_data_at(Register mdp_in,
 951                                                       Register reg,
 952                                                       int constant,
 953                                                       bool decrement) {
 954   assert(ProfileInterpreter, "must be profiling interpreter");
 955   // %%% this does 64bit counters at best it is wasting space
 956   // at worst it is a rare bug when counters overflow
 957 
 958   assert_different_registers(rscratch2, rscratch1, mdp_in, reg);
 959 
 960   Address addr1(mdp_in, constant);
 961   Address addr2(rscratch2, reg, Address::lsl(0));
 962   Address &addr = addr1;
 963   if (reg != noreg) {
 964     lea(rscratch2, addr1);
 965     addr = addr2;
 966   }
 967 
 968   if (decrement) {
 969     // Decrement the register.  Set condition codes.
 970     // Intel does this
 971     // addptr(data, (int32_t) -DataLayout::counter_increment);
 972     // If the decrement causes the counter to overflow, stay negative
 973     // Label L;
 974     // jcc(Assembler::negative, L);
 975     // addptr(data, (int32_t) DataLayout::counter_increment);
 976     // so we do this
 977     ldr(rscratch1, addr);
 978     subs(rscratch1, rscratch1, (unsigned)DataLayout::counter_increment);
 979     Label L;
 980     br(Assembler::LO, L);       // skip store if counter underflow
 981     str(rscratch1, addr);
 982     bind(L);
 983   } else {
 984     assert(DataLayout::counter_increment == 1,
 985            "flow-free idiom only works with 1");
 986     // Intel does this
 987     // Increment the register.  Set carry flag.
 988     // addptr(data, DataLayout::counter_increment);
 989     // If the increment causes the counter to overflow, pull back by 1.
 990     // sbbptr(data, (int32_t)0);
 991     // so we do this
 992     ldr(rscratch1, addr);
 993     adds(rscratch1, rscratch1, DataLayout::counter_increment);
 994     Label L;
 995     br(Assembler::CS, L);       // skip store if counter overflow
 996     str(rscratch1, addr);
 997     bind(L);
 998   }
 999 }
1000 
1001 void InterpreterMacroAssembler::set_mdp_flag_at(Register mdp_in,
1002                                                 int flag_byte_constant) {
1003   assert(ProfileInterpreter, "must be profiling interpreter");
1004   int flags_offset = in_bytes(DataLayout::flags_offset());
1005   // Set the flag
1006   ldrb(rscratch1, Address(mdp_in, flags_offset));
1007   orr(rscratch1, rscratch1, flag_byte_constant);
1008   strb(rscratch1, Address(mdp_in, flags_offset));
1009 }
1010 
1011 
1012 void InterpreterMacroAssembler::test_mdp_data_at(Register mdp_in,
1013                                                  int offset,
1014                                                  Register value,
1015                                                  Register test_value_out,
1016                                                  Label& not_equal_continue) {
1017   assert(ProfileInterpreter, "must be profiling interpreter");
1018   if (test_value_out == noreg) {
1019     ldr(rscratch1, Address(mdp_in, offset));
1020     cmp(value, rscratch1);
1021   } else {
1022     // Put the test value into a register, so caller can use it:
1023     ldr(test_value_out, Address(mdp_in, offset));
1024     cmp(value, test_value_out);
1025   }
1026   br(Assembler::NE, not_equal_continue);
1027 }
1028 
1029 
1030 void InterpreterMacroAssembler::update_mdp_by_offset(Register mdp_in,
1031                                                      int offset_of_disp) {
1032   assert(ProfileInterpreter, "must be profiling interpreter");
1033   ldr(rscratch1, Address(mdp_in, offset_of_disp));
1034   add(mdp_in, mdp_in, rscratch1, LSL);
1035   str(mdp_in, Address(rfp, frame::interpreter_frame_mdp_offset * wordSize));
1036 }
1037 
1038 
1039 void InterpreterMacroAssembler::update_mdp_by_offset(Register mdp_in,
1040                                                      Register reg,
1041                                                      int offset_of_disp) {
1042   assert(ProfileInterpreter, "must be profiling interpreter");
1043   lea(rscratch1, Address(mdp_in, offset_of_disp));
1044   ldr(rscratch1, Address(rscratch1, reg, Address::lsl(0)));
1045   add(mdp_in, mdp_in, rscratch1, LSL);
1046   str(mdp_in, Address(rfp, frame::interpreter_frame_mdp_offset * wordSize));
1047 }
1048 
1049 
1050 void InterpreterMacroAssembler::update_mdp_by_constant(Register mdp_in,
1051                                                        int constant) {
1052   assert(ProfileInterpreter, "must be profiling interpreter");
1053   add(mdp_in, mdp_in, (unsigned)constant);
1054   str(mdp_in, Address(rfp, frame::interpreter_frame_mdp_offset * wordSize));
1055 }
1056 
1057 
1058 void InterpreterMacroAssembler::update_mdp_for_ret(Register return_bci) {
1059   assert(ProfileInterpreter, "must be profiling interpreter");
1060   // save/restore across call_VM
1061   stp(zr, return_bci, Address(pre(sp, -2 * wordSize)));
1062   call_VM(noreg,
1063           CAST_FROM_FN_PTR(address, InterpreterRuntime::update_mdp_for_ret),
1064           return_bci);
1065   ldp(zr, return_bci, Address(post(sp, 2 * wordSize)));
1066 }
1067 
1068 
1069 void InterpreterMacroAssembler::profile_taken_branch(Register mdp,
1070                                                      Register bumped_count) {
1071   if (ProfileInterpreter) {
1072     Label profile_continue;
1073 
1074     // If no method data exists, go to profile_continue.
1075     // Otherwise, assign to mdp
1076     test_method_data_pointer(mdp, profile_continue);
1077 
1078     // We are taking a branch.  Increment the taken count.
1079     // We inline increment_mdp_data_at to return bumped_count in a register
1080     //increment_mdp_data_at(mdp, in_bytes(JumpData::taken_offset()));
1081     Address data(mdp, in_bytes(JumpData::taken_offset()));
1082     ldr(bumped_count, data);
1083     assert(DataLayout::counter_increment == 1,
1084             "flow-free idiom only works with 1");
1085     // Intel does this to catch overflow
1086     // addptr(bumped_count, DataLayout::counter_increment);
1087     // sbbptr(bumped_count, 0);
1088     // so we do this
1089     adds(bumped_count, bumped_count, DataLayout::counter_increment);
1090     Label L;
1091     br(Assembler::CS, L);       // skip store if counter overflow
1092     str(bumped_count, data);
1093     bind(L);
1094     // The method data pointer needs to be updated to reflect the new target.
1095     update_mdp_by_offset(mdp, in_bytes(JumpData::displacement_offset()));
1096     bind(profile_continue);
1097   }
1098 }
1099 
1100 
1101 void InterpreterMacroAssembler::profile_not_taken_branch(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 taking a branch.  Increment the not taken count.
1109     increment_mdp_data_at(mdp, in_bytes(BranchData::not_taken_offset()));
1110 
1111     // The method data pointer needs to be updated to correspond to
1112     // the next bytecode
1113     update_mdp_by_constant(mdp, in_bytes(BranchData::branch_data_size()));
1114     bind(profile_continue);
1115   }
1116 }
1117 
1118 
1119 void InterpreterMacroAssembler::profile_call(Register mdp) {
1120   if (ProfileInterpreter) {
1121     Label profile_continue;
1122 
1123     // If no method data exists, go to profile_continue.
1124     test_method_data_pointer(mdp, profile_continue);
1125 
1126     // We are making a call.  Increment the count.
1127     increment_mdp_data_at(mdp, in_bytes(CounterData::count_offset()));
1128 
1129     // The method data pointer needs to be updated to reflect the new target.
1130     update_mdp_by_constant(mdp, in_bytes(CounterData::counter_data_size()));
1131     bind(profile_continue);
1132   }
1133 }
1134 
1135 void InterpreterMacroAssembler::profile_final_call(Register mdp) {
1136   if (ProfileInterpreter) {
1137     Label profile_continue;
1138 
1139     // If no method data exists, go to profile_continue.
1140     test_method_data_pointer(mdp, profile_continue);
1141 
1142     // We are making a call.  Increment the count.
1143     increment_mdp_data_at(mdp, in_bytes(CounterData::count_offset()));
1144 
1145     // The method data pointer needs to be updated to reflect the new target.
1146     update_mdp_by_constant(mdp,
1147                            in_bytes(VirtualCallData::
1148                                     virtual_call_data_size()));
1149     bind(profile_continue);
1150   }
1151 }
1152 
1153 
1154 void InterpreterMacroAssembler::profile_virtual_call(Register receiver,
1155                                                      Register mdp,
1156                                                      Register reg2,
1157                                                      bool receiver_can_be_null) {
1158   if (ProfileInterpreter) {
1159     Label profile_continue;
1160 
1161     // If no method data exists, go to profile_continue.
1162     test_method_data_pointer(mdp, profile_continue);
1163 
1164     Label skip_receiver_profile;
1165     if (receiver_can_be_null) {
1166       Label not_null;
1167       // We are making a call.  Increment the count for null receiver.
1168       increment_mdp_data_at(mdp, in_bytes(CounterData::count_offset()));
1169       b(skip_receiver_profile);
1170       bind(not_null);
1171     }
1172 
1173     // Record the receiver type.
1174     record_klass_in_profile(receiver, mdp, reg2, true);
1175     bind(skip_receiver_profile);
1176 
1177     // The method data pointer needs to be updated to reflect the new target.
1178 #if INCLUDE_JVMCI
1179     if (MethodProfileWidth == 0) {
1180       update_mdp_by_constant(mdp, in_bytes(VirtualCallData::virtual_call_data_size()));
1181     }
1182 #else // INCLUDE_JVMCI
1183     update_mdp_by_constant(mdp,
1184                            in_bytes(VirtualCallData::
1185                                     virtual_call_data_size()));
1186 #endif // INCLUDE_JVMCI
1187     bind(profile_continue);
1188   }
1189 }
1190 
1191 #if INCLUDE_JVMCI
1192 void InterpreterMacroAssembler::profile_called_method(Register method, Register mdp, Register reg2) {
1193   assert_different_registers(method, mdp, reg2);
1194   if (ProfileInterpreter && MethodProfileWidth > 0) {
1195     Label profile_continue;
1196 
1197     // If no method data exists, go to profile_continue.
1198     test_method_data_pointer(mdp, profile_continue);
1199 
1200     Label done;
1201     record_item_in_profile_helper(method, mdp, reg2, 0, done, MethodProfileWidth,
1202       &VirtualCallData::method_offset, &VirtualCallData::method_count_offset, in_bytes(VirtualCallData::nonprofiled_receiver_count_offset()));
1203     bind(done);
1204 
1205     update_mdp_by_constant(mdp, in_bytes(VirtualCallData::virtual_call_data_size()));
1206     bind(profile_continue);
1207   }
1208 }
1209 #endif // INCLUDE_JVMCI
1210 
1211 // This routine creates a state machine for updating the multi-row
1212 // type profile at a virtual call site (or other type-sensitive bytecode).
1213 // The machine visits each row (of receiver/count) until the receiver type
1214 // is found, or until it runs out of rows.  At the same time, it remembers
1215 // the location of the first empty row.  (An empty row records null for its
1216 // receiver, and can be allocated for a newly-observed receiver type.)
1217 // Because there are two degrees of freedom in the state, a simple linear
1218 // search will not work; it must be a decision tree.  Hence this helper
1219 // function is recursive, to generate the required tree structured code.
1220 // It's the interpreter, so we are trading off code space for speed.
1221 // See below for example code.
1222 void InterpreterMacroAssembler::record_klass_in_profile_helper(
1223                                         Register receiver, Register mdp,
1224                                         Register reg2, int start_row,
1225                                         Label& done, bool is_virtual_call) {
1226   if (TypeProfileWidth == 0) {
1227     if (is_virtual_call) {
1228       increment_mdp_data_at(mdp, in_bytes(CounterData::count_offset()));
1229     }
1230 #if INCLUDE_JVMCI
1231     else if (EnableJVMCI) {
1232       increment_mdp_data_at(mdp, in_bytes(ReceiverTypeData::nonprofiled_receiver_count_offset()));
1233     }
1234 #endif // INCLUDE_JVMCI
1235   } else {
1236     int non_profiled_offset = -1;
1237     if (is_virtual_call) {
1238       non_profiled_offset = in_bytes(CounterData::count_offset());
1239     }
1240 #if INCLUDE_JVMCI
1241     else if (EnableJVMCI) {
1242       non_profiled_offset = in_bytes(ReceiverTypeData::nonprofiled_receiver_count_offset());
1243     }
1244 #endif // INCLUDE_JVMCI
1245 
1246     record_item_in_profile_helper(receiver, mdp, reg2, 0, done, TypeProfileWidth,
1247         &VirtualCallData::receiver_offset, &VirtualCallData::receiver_count_offset, non_profiled_offset);
1248   }
1249 }
1250 
1251 void InterpreterMacroAssembler::record_item_in_profile_helper(Register item, Register mdp,
1252                                         Register reg2, int start_row, Label& done, int total_rows,
1253                                         OffsetFunction item_offset_fn, OffsetFunction item_count_offset_fn,
1254                                         int non_profiled_offset) {
1255   int last_row = total_rows - 1;
1256   assert(start_row <= last_row, "must be work left to do");
1257   // Test this row for both the item and for null.
1258   // Take any of three different outcomes:
1259   //   1. found item => increment count and goto done
1260   //   2. found null => keep looking for case 1, maybe allocate this cell
1261   //   3. found something else => keep looking for cases 1 and 2
1262   // Case 3 is handled by a recursive call.
1263   for (int row = start_row; row <= last_row; row++) {
1264     Label next_test;
1265     bool test_for_null_also = (row == start_row);
1266 
1267     // See if the item is item[n].
1268     int item_offset = in_bytes(item_offset_fn(row));
1269     test_mdp_data_at(mdp, item_offset, item,
1270                      (test_for_null_also ? reg2 : noreg),
1271                      next_test);
1272     // (Reg2 now contains the item from the CallData.)
1273 
1274     // The item is item[n].  Increment count[n].
1275     int count_offset = in_bytes(item_count_offset_fn(row));
1276     increment_mdp_data_at(mdp, count_offset);
1277     b(done);
1278     bind(next_test);
1279 
1280     if (test_for_null_also) {
1281       Label found_null;
1282       // Failed the equality check on item[n]...  Test for null.
1283       if (start_row == last_row) {
1284         // The only thing left to do is handle the null case.
1285         if (non_profiled_offset >= 0) {
1286           cbz(reg2, found_null);
1287           // Item did not match any saved item and there is no empty row for it.
1288           // Increment total counter to indicate polymorphic case.
1289           increment_mdp_data_at(mdp, non_profiled_offset);
1290           b(done);
1291           bind(found_null);
1292         } else {
1293           cbnz(reg2, done);
1294         }
1295         break;
1296       }
1297       // Since null is rare, make it be the branch-taken case.
1298       cbz(reg2, found_null);
1299 
1300       // Put all the "Case 3" tests here.
1301       record_item_in_profile_helper(item, mdp, reg2, start_row + 1, done, total_rows,
1302         item_offset_fn, item_count_offset_fn, non_profiled_offset);
1303 
1304       // Found a null.  Keep searching for a matching item,
1305       // but remember that this is an empty (unused) slot.
1306       bind(found_null);
1307     }
1308   }
1309 
1310   // In the fall-through case, we found no matching item, but we
1311   // observed the item[start_row] is NULL.
1312 
1313   // Fill in the item field and increment the count.
1314   int item_offset = in_bytes(item_offset_fn(start_row));
1315   set_mdp_data_at(mdp, item_offset, item);
1316   int count_offset = in_bytes(item_count_offset_fn(start_row));
1317   mov(reg2, DataLayout::counter_increment);
1318   set_mdp_data_at(mdp, count_offset, reg2);
1319   if (start_row > 0) {
1320     b(done);
1321   }
1322 }
1323 
1324 // Example state machine code for three profile rows:
1325 //   // main copy of decision tree, rooted at row[1]
1326 //   if (row[0].rec == rec) { row[0].incr(); goto done; }
1327 //   if (row[0].rec != NULL) {
1328 //     // inner copy of decision tree, rooted at row[1]
1329 //     if (row[1].rec == rec) { row[1].incr(); goto done; }
1330 //     if (row[1].rec != NULL) {
1331 //       // degenerate decision tree, rooted at row[2]
1332 //       if (row[2].rec == rec) { row[2].incr(); goto done; }
1333 //       if (row[2].rec != NULL) { count.incr(); goto done; } // overflow
1334 //       row[2].init(rec); goto done;
1335 //     } else {
1336 //       // remember row[1] is empty
1337 //       if (row[2].rec == rec) { row[2].incr(); goto done; }
1338 //       row[1].init(rec); goto done;
1339 //     }
1340 //   } else {
1341 //     // remember row[0] is empty
1342 //     if (row[1].rec == rec) { row[1].incr(); goto done; }
1343 //     if (row[2].rec == rec) { row[2].incr(); goto done; }
1344 //     row[0].init(rec); goto done;
1345 //   }
1346 //   done:
1347 
1348 void InterpreterMacroAssembler::record_klass_in_profile(Register receiver,
1349                                                         Register mdp, Register reg2,
1350                                                         bool is_virtual_call) {
1351   assert(ProfileInterpreter, "must be profiling");
1352   Label done;
1353 
1354   record_klass_in_profile_helper(receiver, mdp, reg2, 0, done, is_virtual_call);
1355 
1356   bind (done);
1357 }
1358 
1359 void InterpreterMacroAssembler::profile_ret(Register return_bci,
1360                                             Register mdp) {
1361   if (ProfileInterpreter) {
1362     Label profile_continue;
1363     uint row;
1364 
1365     // If no method data exists, go to profile_continue.
1366     test_method_data_pointer(mdp, profile_continue);
1367 
1368     // Update the total ret count.
1369     increment_mdp_data_at(mdp, in_bytes(CounterData::count_offset()));
1370 
1371     for (row = 0; row < RetData::row_limit(); row++) {
1372       Label next_test;
1373 
1374       // See if return_bci is equal to bci[n]:
1375       test_mdp_data_at(mdp,
1376                        in_bytes(RetData::bci_offset(row)),
1377                        return_bci, noreg,
1378                        next_test);
1379 
1380       // return_bci is equal to bci[n].  Increment the count.
1381       increment_mdp_data_at(mdp, in_bytes(RetData::bci_count_offset(row)));
1382 
1383       // The method data pointer needs to be updated to reflect the new target.
1384       update_mdp_by_offset(mdp,
1385                            in_bytes(RetData::bci_displacement_offset(row)));
1386       b(profile_continue);
1387       bind(next_test);
1388     }
1389 
1390     update_mdp_for_ret(return_bci);
1391 
1392     bind(profile_continue);
1393   }
1394 }
1395 
1396 void InterpreterMacroAssembler::profile_null_seen(Register mdp) {
1397   if (ProfileInterpreter) {
1398     Label profile_continue;
1399 
1400     // If no method data exists, go to profile_continue.
1401     test_method_data_pointer(mdp, profile_continue);
1402 
1403     set_mdp_flag_at(mdp, BitData::null_seen_byte_constant());
1404 
1405     // The method data pointer needs to be updated.
1406     int mdp_delta = in_bytes(BitData::bit_data_size());
1407     if (TypeProfileCasts) {
1408       mdp_delta = in_bytes(VirtualCallData::virtual_call_data_size());
1409     }
1410     update_mdp_by_constant(mdp, mdp_delta);
1411 
1412     bind(profile_continue);
1413   }
1414 }
1415 
1416 void InterpreterMacroAssembler::profile_typecheck_failed(Register mdp) {
1417   if (ProfileInterpreter && TypeProfileCasts) {
1418     Label profile_continue;
1419 
1420     // If no method data exists, go to profile_continue.
1421     test_method_data_pointer(mdp, profile_continue);
1422 
1423     int count_offset = in_bytes(CounterData::count_offset());
1424     // Back up the address, since we have already bumped the mdp.
1425     count_offset -= in_bytes(VirtualCallData::virtual_call_data_size());
1426 
1427     // *Decrement* the counter.  We expect to see zero or small negatives.
1428     increment_mdp_data_at(mdp, count_offset, true);
1429 
1430     bind (profile_continue);
1431   }
1432 }
1433 
1434 void InterpreterMacroAssembler::profile_typecheck(Register mdp, Register klass, Register reg2) {
1435   if (ProfileInterpreter) {
1436     Label profile_continue;
1437 
1438     // If no method data exists, go to profile_continue.
1439     test_method_data_pointer(mdp, profile_continue);
1440 
1441     // The method data pointer needs to be updated.
1442     int mdp_delta = in_bytes(BitData::bit_data_size());
1443     if (TypeProfileCasts) {
1444       mdp_delta = in_bytes(VirtualCallData::virtual_call_data_size());
1445 
1446       // Record the object type.
1447       record_klass_in_profile(klass, mdp, reg2, false);
1448     }
1449     update_mdp_by_constant(mdp, mdp_delta);
1450 
1451     bind(profile_continue);
1452   }
1453 }
1454 
1455 void InterpreterMacroAssembler::profile_switch_default(Register mdp) {
1456   if (ProfileInterpreter) {
1457     Label profile_continue;
1458 
1459     // If no method data exists, go to profile_continue.
1460     test_method_data_pointer(mdp, profile_continue);
1461 
1462     // Update the default case count
1463     increment_mdp_data_at(mdp,
1464                           in_bytes(MultiBranchData::default_count_offset()));
1465 
1466     // The method data pointer needs to be updated.
1467     update_mdp_by_offset(mdp,
1468                          in_bytes(MultiBranchData::
1469                                   default_displacement_offset()));
1470 
1471     bind(profile_continue);
1472   }
1473 }
1474 
1475 void InterpreterMacroAssembler::profile_switch_case(Register index,
1476                                                     Register mdp,
1477                                                     Register reg2) {
1478   if (ProfileInterpreter) {
1479     Label profile_continue;
1480 
1481     // If no method data exists, go to profile_continue.
1482     test_method_data_pointer(mdp, profile_continue);
1483 
1484     // Build the base (index * per_case_size_in_bytes()) +
1485     // case_array_offset_in_bytes()
1486     movw(reg2, in_bytes(MultiBranchData::per_case_size()));
1487     movw(rscratch1, in_bytes(MultiBranchData::case_array_offset()));
1488     Assembler::maddw(index, index, reg2, rscratch1);
1489 
1490     // Update the case count
1491     increment_mdp_data_at(mdp,
1492                           index,
1493                           in_bytes(MultiBranchData::relative_count_offset()));
1494 
1495     // The method data pointer needs to be updated.
1496     update_mdp_by_offset(mdp,
1497                          index,
1498                          in_bytes(MultiBranchData::
1499                                   relative_displacement_offset()));
1500 
1501     bind(profile_continue);
1502   }
1503 }
1504 
1505 void InterpreterMacroAssembler::verify_oop(Register reg, TosState state) {
1506   if (state == atos) {
1507     MacroAssembler::verify_oop(reg);
1508   }
1509 }
1510 
1511 void InterpreterMacroAssembler::verify_FPU(int stack_depth, TosState state) { ; }
1512 
1513 
1514 void InterpreterMacroAssembler::notify_method_entry() {
1515   // Whenever JVMTI is interp_only_mode, method entry/exit events are sent to
1516   // track stack depth.  If it is possible to enter interp_only_mode we add
1517   // the code to check if the event should be sent.
1518   if (JvmtiExport::can_post_interpreter_events()) {
1519     Label L;
1520     ldrw(r3, Address(rthread, JavaThread::interp_only_mode_offset()));
1521     cbzw(r3, L);
1522     call_VM(noreg, CAST_FROM_FN_PTR(address,
1523                                     InterpreterRuntime::post_method_entry));
1524     bind(L);
1525   }
1526 
1527   {
1528     SkipIfEqual skip(this, &DTraceMethodProbes, false);
1529     get_method(c_rarg1);
1530     call_VM_leaf(CAST_FROM_FN_PTR(address, SharedRuntime::dtrace_method_entry),
1531                  rthread, c_rarg1);
1532   }
1533 
1534   // RedefineClasses() tracing support for obsolete method entry
1535   if (log_is_enabled(Trace, redefine, class, obsolete)) {
1536     get_method(c_rarg1);
1537     call_VM_leaf(
1538       CAST_FROM_FN_PTR(address, SharedRuntime::rc_trace_method_entry),
1539       rthread, c_rarg1);
1540   }
1541 
1542  }
1543 
1544 
1545 void InterpreterMacroAssembler::notify_method_exit(
1546     TosState state, NotifyMethodExitMode mode) {
1547   // Whenever JVMTI is interp_only_mode, method entry/exit events are sent to
1548   // track stack depth.  If it is possible to enter interp_only_mode we add
1549   // the code to check if the event should be sent.
1550   if (mode == NotifyJVMTI && JvmtiExport::can_post_interpreter_events()) {
1551     Label L;
1552     // Note: frame::interpreter_frame_result has a dependency on how the
1553     // method result is saved across the call to post_method_exit. If this
1554     // is changed then the interpreter_frame_result implementation will
1555     // need to be updated too.
1556 
1557     // template interpreter will leave the result on the top of the stack.
1558     push(state);
1559     ldrw(r3, Address(rthread, JavaThread::interp_only_mode_offset()));
1560     cbz(r3, L);
1561     call_VM(noreg,
1562             CAST_FROM_FN_PTR(address, InterpreterRuntime::post_method_exit));
1563     bind(L);
1564     pop(state);
1565   }
1566 
1567   {
1568     SkipIfEqual skip(this, &DTraceMethodProbes, false);
1569     push(state);
1570     get_method(c_rarg1);
1571     call_VM_leaf(CAST_FROM_FN_PTR(address, SharedRuntime::dtrace_method_exit),
1572                  rthread, c_rarg1);
1573     pop(state);
1574   }
1575 }
1576 
1577 
1578 // Jump if ((*counter_addr += increment) & mask) satisfies the condition.
1579 void InterpreterMacroAssembler::increment_mask_and_jump(Address counter_addr,
1580                                                         int increment, Address mask,
1581                                                         Register scratch, Register scratch2,
1582                                                         bool preloaded, Condition cond,
1583                                                         Label* where) {
1584   if (!preloaded) {
1585     ldrw(scratch, counter_addr);
1586   }
1587   add(scratch, scratch, increment);
1588   strw(scratch, counter_addr);
1589   ldrw(scratch2, mask);
1590   ands(scratch, scratch, scratch2);
1591   br(cond, *where);
1592 }
1593 
1594 void InterpreterMacroAssembler::call_VM_leaf_base(address entry_point,
1595                                                   int number_of_arguments) {
1596   // interpreter specific
1597   //
1598   // Note: No need to save/restore rbcp & rlocals pointer since these
1599   //       are callee saved registers and no blocking/ GC can happen
1600   //       in leaf calls.
1601 #ifdef ASSERT
1602   {
1603     Label L;
1604     ldr(rscratch1, Address(rfp, frame::interpreter_frame_last_sp_offset * wordSize));
1605     cbz(rscratch1, L);
1606     stop("InterpreterMacroAssembler::call_VM_leaf_base:"
1607          " last_sp != NULL");
1608     bind(L);
1609   }
1610 #endif /* ASSERT */
1611   // super call
1612   MacroAssembler::call_VM_leaf_base(entry_point, number_of_arguments);
1613 }
1614 
1615 void InterpreterMacroAssembler::call_VM_base(Register oop_result,
1616                                              Register java_thread,
1617                                              Register last_java_sp,
1618                                              address  entry_point,
1619                                              int      number_of_arguments,
1620                                              bool     check_exceptions) {
1621   // interpreter specific
1622   //
1623   // Note: Could avoid restoring locals ptr (callee saved) - however doesn't
1624   //       really make a difference for these runtime calls, since they are
1625   //       slow anyway. Btw., bcp must be saved/restored since it may change
1626   //       due to GC.
1627   // assert(java_thread == noreg , "not expecting a precomputed java thread");
1628   save_bcp();
1629 #ifdef ASSERT
1630   {
1631     Label L;
1632     ldr(rscratch1, Address(rfp, frame::interpreter_frame_last_sp_offset * wordSize));
1633     cbz(rscratch1, L);
1634     stop("InterpreterMacroAssembler::call_VM_leaf_base:"
1635          " last_sp != NULL");
1636     bind(L);
1637   }
1638 #endif /* ASSERT */
1639   // super call
1640   MacroAssembler::call_VM_base(oop_result, noreg, last_java_sp,
1641                                entry_point, number_of_arguments,
1642                      check_exceptions);
1643 // interpreter specific
1644   restore_bcp();
1645   restore_locals();
1646 }
1647 
1648 void InterpreterMacroAssembler::profile_obj_type(Register obj, const Address& mdo_addr) {
1649   assert_different_registers(obj, rscratch1);
1650   Label update, next, none;
1651 
1652   verify_oop(obj);
1653 
1654   cbnz(obj, update);
1655   orptr(mdo_addr, TypeEntries::null_seen);
1656   b(next);
1657 
1658   bind(update);
1659   load_klass(obj, obj);
1660 
1661   ldr(rscratch1, mdo_addr);
1662   eor(obj, obj, rscratch1);
1663   tst(obj, TypeEntries::type_klass_mask);
1664   br(Assembler::EQ, next); // klass seen before, nothing to
1665                            // do. The unknown bit may have been
1666                            // set already but no need to check.
1667 
1668   tbnz(obj, exact_log2(TypeEntries::type_unknown), next);
1669   // already unknown. Nothing to do anymore.
1670 
1671   ldr(rscratch1, mdo_addr);
1672   cbz(rscratch1, none);
1673   cmp(rscratch1, (u1)TypeEntries::null_seen);
1674   br(Assembler::EQ, none);
1675   // There is a chance that the checks above (re-reading profiling
1676   // data from memory) fail if another thread has just set the
1677   // profiling to this obj's klass
1678   ldr(rscratch1, mdo_addr);
1679   eor(obj, obj, rscratch1);
1680   tst(obj, TypeEntries::type_klass_mask);
1681   br(Assembler::EQ, next);
1682 
1683   // different than before. Cannot keep accurate profile.
1684   orptr(mdo_addr, TypeEntries::type_unknown);
1685   b(next);
1686 
1687   bind(none);
1688   // first time here. Set profile type.
1689   str(obj, mdo_addr);
1690 
1691   bind(next);
1692 }
1693 
1694 void InterpreterMacroAssembler::profile_arguments_type(Register mdp, Register callee, Register tmp, bool is_virtual) {
1695   if (!ProfileInterpreter) {
1696     return;
1697   }
1698 
1699   if (MethodData::profile_arguments() || MethodData::profile_return()) {
1700     Label profile_continue;
1701 
1702     test_method_data_pointer(mdp, profile_continue);
1703 
1704     int off_to_start = is_virtual ? in_bytes(VirtualCallData::virtual_call_data_size()) : in_bytes(CounterData::counter_data_size());
1705 
1706     ldrb(rscratch1, Address(mdp, in_bytes(DataLayout::tag_offset()) - off_to_start));
1707     cmp(rscratch1, u1(is_virtual ? DataLayout::virtual_call_type_data_tag : DataLayout::call_type_data_tag));
1708     br(Assembler::NE, profile_continue);
1709 
1710     if (MethodData::profile_arguments()) {
1711       Label done;
1712       int off_to_args = in_bytes(TypeEntriesAtCall::args_data_offset());
1713 
1714       for (int i = 0; i < TypeProfileArgsLimit; i++) {
1715         if (i > 0 || MethodData::profile_return()) {
1716           // If return value type is profiled we may have no argument to profile
1717           ldr(tmp, Address(mdp, in_bytes(TypeEntriesAtCall::cell_count_offset())));
1718           sub(tmp, tmp, i*TypeStackSlotEntries::per_arg_count());
1719           cmp(tmp, (u1)TypeStackSlotEntries::per_arg_count());
1720           add(rscratch1, mdp, off_to_args);
1721           br(Assembler::LT, done);
1722         }
1723         ldr(tmp, Address(callee, Method::const_offset()));
1724         load_unsigned_short(tmp, Address(tmp, ConstMethod::size_of_parameters_offset()));
1725         // stack offset o (zero based) from the start of the argument
1726         // list, for n arguments translates into offset n - o - 1 from
1727         // the end of the argument list
1728         ldr(rscratch1, Address(mdp, in_bytes(TypeEntriesAtCall::stack_slot_offset(i))));
1729         sub(tmp, tmp, rscratch1);
1730         sub(tmp, tmp, 1);
1731         Address arg_addr = argument_address(tmp);
1732         ldr(tmp, arg_addr);
1733 
1734         Address mdo_arg_addr(mdp, in_bytes(TypeEntriesAtCall::argument_type_offset(i)));
1735         profile_obj_type(tmp, mdo_arg_addr);
1736 
1737         int to_add = in_bytes(TypeStackSlotEntries::per_arg_size());
1738         off_to_args += to_add;
1739       }
1740 
1741       if (MethodData::profile_return()) {
1742         ldr(tmp, Address(mdp, in_bytes(TypeEntriesAtCall::cell_count_offset())));
1743         sub(tmp, tmp, TypeProfileArgsLimit*TypeStackSlotEntries::per_arg_count());
1744       }
1745 
1746       add(rscratch1, mdp, off_to_args);
1747       bind(done);
1748       mov(mdp, rscratch1);
1749 
1750       if (MethodData::profile_return()) {
1751         // We're right after the type profile for the last
1752         // argument. tmp is the number of cells left in the
1753         // CallTypeData/VirtualCallTypeData to reach its end. Non null
1754         // if there's a return to profile.
1755         assert(ReturnTypeEntry::static_cell_count() < TypeStackSlotEntries::per_arg_count(), "can't move past ret type");
1756         add(mdp, mdp, tmp, LSL, exact_log2(DataLayout::cell_size));
1757       }
1758       str(mdp, Address(rfp, frame::interpreter_frame_mdp_offset * wordSize));
1759     } else {
1760       assert(MethodData::profile_return(), "either profile call args or call ret");
1761       update_mdp_by_constant(mdp, in_bytes(TypeEntriesAtCall::return_only_size()));
1762     }
1763 
1764     // mdp points right after the end of the
1765     // CallTypeData/VirtualCallTypeData, right after the cells for the
1766     // return value type if there's one
1767 
1768     bind(profile_continue);
1769   }
1770 }
1771 
1772 void InterpreterMacroAssembler::profile_return_type(Register mdp, Register ret, Register tmp) {
1773   assert_different_registers(mdp, ret, tmp, rbcp);
1774   if (ProfileInterpreter && MethodData::profile_return()) {
1775     Label profile_continue, done;
1776 
1777     test_method_data_pointer(mdp, profile_continue);
1778 
1779     if (MethodData::profile_return_jsr292_only()) {
1780       assert(Method::intrinsic_id_size_in_bytes() == 2, "assuming Method::_intrinsic_id is u2");
1781 
1782       // If we don't profile all invoke bytecodes we must make sure
1783       // it's a bytecode we indeed profile. We can't go back to the
1784       // begining of the ProfileData we intend to update to check its
1785       // type because we're right after it and we don't known its
1786       // length
1787       Label do_profile;
1788       ldrb(rscratch1, Address(rbcp, 0));
1789       cmp(rscratch1, (u1)Bytecodes::_invokedynamic);
1790       br(Assembler::EQ, do_profile);
1791       cmp(rscratch1, (u1)Bytecodes::_invokehandle);
1792       br(Assembler::EQ, do_profile);
1793       get_method(tmp);
1794       ldrh(rscratch1, Address(tmp, Method::intrinsic_id_offset_in_bytes()));
1795       subs(zr, rscratch1, vmIntrinsics::_compiledLambdaForm);
1796       br(Assembler::NE, profile_continue);
1797 
1798       bind(do_profile);
1799     }
1800 
1801     Address mdo_ret_addr(mdp, -in_bytes(ReturnTypeEntry::size()));
1802     mov(tmp, ret);
1803     profile_obj_type(tmp, mdo_ret_addr);
1804 
1805     bind(profile_continue);
1806   }
1807 }
1808 
1809 void InterpreterMacroAssembler::profile_parameters_type(Register mdp, Register tmp1, Register tmp2) {
1810   assert_different_registers(rscratch1, rscratch2, mdp, tmp1, tmp2);
1811   if (ProfileInterpreter && MethodData::profile_parameters()) {
1812     Label profile_continue, done;
1813 
1814     test_method_data_pointer(mdp, profile_continue);
1815 
1816     // Load the offset of the area within the MDO used for
1817     // parameters. If it's negative we're not profiling any parameters
1818     ldrw(tmp1, Address(mdp, in_bytes(MethodData::parameters_type_data_di_offset()) - in_bytes(MethodData::data_offset())));
1819     tbnz(tmp1, 31, profile_continue);  // i.e. sign bit set
1820 
1821     // Compute a pointer to the area for parameters from the offset
1822     // and move the pointer to the slot for the last
1823     // parameters. Collect profiling from last parameter down.
1824     // mdo start + parameters offset + array length - 1
1825     add(mdp, mdp, tmp1);
1826     ldr(tmp1, Address(mdp, ArrayData::array_len_offset()));
1827     sub(tmp1, tmp1, TypeStackSlotEntries::per_arg_count());
1828 
1829     Label loop;
1830     bind(loop);
1831 
1832     int off_base = in_bytes(ParametersTypeData::stack_slot_offset(0));
1833     int type_base = in_bytes(ParametersTypeData::type_offset(0));
1834     int per_arg_scale = exact_log2(DataLayout::cell_size);
1835     add(rscratch1, mdp, off_base);
1836     add(rscratch2, mdp, type_base);
1837 
1838     Address arg_off(rscratch1, tmp1, Address::lsl(per_arg_scale));
1839     Address arg_type(rscratch2, tmp1, Address::lsl(per_arg_scale));
1840 
1841     // load offset on the stack from the slot for this parameter
1842     ldr(tmp2, arg_off);
1843     neg(tmp2, tmp2);
1844     // read the parameter from the local area
1845     ldr(tmp2, Address(rlocals, tmp2, Address::lsl(Interpreter::logStackElementSize)));
1846 
1847     // profile the parameter
1848     profile_obj_type(tmp2, arg_type);
1849 
1850     // go to next parameter
1851     subs(tmp1, tmp1, TypeStackSlotEntries::per_arg_count());
1852     br(Assembler::GE, loop);
1853 
1854     bind(profile_continue);
1855   }
1856 }