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