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