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