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