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