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