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