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