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