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