1 /*
   2  * Copyright (c) 2003, 2021, Oracle and/or its affiliates. All rights reserved.
   3  * Copyright (c) 2012, 2021 SAP SE. 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 
  27 #include "precompiled.hpp"
  28 #include "asm/macroAssembler.inline.hpp"
  29 #include "gc/shared/barrierSet.hpp"
  30 #include "gc/shared/barrierSetAssembler.hpp"
  31 #include "interp_masm_ppc.hpp"
  32 #include "interpreter/interpreterRuntime.hpp"
  33 #include "oops/methodData.hpp"
  34 #include "prims/jvmtiExport.hpp"
  35 #include "prims/jvmtiThreadState.hpp"
  36 #include "runtime/frame.inline.hpp"
  37 #include "runtime/safepointMechanism.hpp"
  38 #include "runtime/sharedRuntime.hpp"
  39 #include "runtime/vm_version.hpp"
  40 #include "utilities/powerOfTwo.hpp"
  41 
  42 // Implementation of InterpreterMacroAssembler.
  43 
  44 // This file specializes the assembler with interpreter-specific macros.
  45 
  46 #ifdef PRODUCT
  47 #define BLOCK_COMMENT(str) // nothing
  48 #else
  49 #define BLOCK_COMMENT(str) block_comment(str)
  50 #endif
  51 
  52 void InterpreterMacroAssembler::null_check_throw(Register a, int offset, Register temp_reg) {
  53   address exception_entry = Interpreter::throw_NullPointerException_entry();
  54   MacroAssembler::null_check_throw(a, offset, temp_reg, exception_entry);
  55 }
  56 
  57 void InterpreterMacroAssembler::jump_to_entry(address entry, Register Rscratch) {
  58   assert(entry, "Entry must have been generated by now");
  59   if (is_within_range_of_b(entry, pc())) {
  60     b(entry);
  61   } else {
  62     load_const_optimized(Rscratch, entry, R0);
  63     mtctr(Rscratch);
  64     bctr();
  65   }
  66 }
  67 
  68 void InterpreterMacroAssembler::dispatch_next(TosState state, int bcp_incr, bool generate_poll) {
  69   Register bytecode = R12_scratch2;
  70   if (bcp_incr != 0) {
  71     lbzu(bytecode, bcp_incr, R14_bcp);
  72   } else {
  73     lbz(bytecode, 0, R14_bcp);
  74   }
  75 
  76   dispatch_Lbyte_code(state, bytecode, Interpreter::dispatch_table(state), generate_poll);
  77 }
  78 
  79 void InterpreterMacroAssembler::dispatch_via(TosState state, address* table) {
  80   // Load current bytecode.
  81   Register bytecode = R12_scratch2;
  82   lbz(bytecode, 0, R14_bcp);
  83   dispatch_Lbyte_code(state, bytecode, table);
  84 }
  85 
  86 // Dispatch code executed in the prolog of a bytecode which does not do it's
  87 // own dispatch. The dispatch address is computed and placed in R24_dispatch_addr.
  88 void InterpreterMacroAssembler::dispatch_prolog(TosState state, int bcp_incr) {
  89   Register bytecode = R12_scratch2;
  90   lbz(bytecode, bcp_incr, R14_bcp);
  91 
  92   load_dispatch_table(R24_dispatch_addr, Interpreter::dispatch_table(state));
  93 
  94   sldi(bytecode, bytecode, LogBytesPerWord);
  95   ldx(R24_dispatch_addr, R24_dispatch_addr, bytecode);
  96 }
  97 
  98 // Dispatch code executed in the epilog of a bytecode which does not do it's
  99 // own dispatch. The dispatch address in R24_dispatch_addr is used for the
 100 // dispatch.
 101 void InterpreterMacroAssembler::dispatch_epilog(TosState state, int bcp_incr) {
 102   if (bcp_incr) { addi(R14_bcp, R14_bcp, bcp_incr); }
 103   mtctr(R24_dispatch_addr);
 104   bcctr(bcondAlways, 0, bhintbhBCCTRisNotPredictable);
 105 }
 106 
 107 void InterpreterMacroAssembler::check_and_handle_popframe(Register scratch_reg) {
 108   assert(scratch_reg != R0, "can't use R0 as scratch_reg here");
 109   if (JvmtiExport::can_pop_frame()) {
 110     Label L;
 111 
 112     // Check the "pending popframe condition" flag in the current thread.
 113     lwz(scratch_reg, in_bytes(JavaThread::popframe_condition_offset()), R16_thread);
 114 
 115     // Initiate popframe handling only if it is not already being
 116     // processed. If the flag has the popframe_processing bit set, it
 117     // means that this code is called *during* popframe handling - we
 118     // don't want to reenter.
 119     andi_(R0, scratch_reg, JavaThread::popframe_pending_bit);
 120     beq(CCR0, L);
 121 
 122     andi_(R0, scratch_reg, JavaThread::popframe_processing_bit);
 123     bne(CCR0, L);
 124 
 125     // Call the Interpreter::remove_activation_preserving_args_entry()
 126     // func to get the address of the same-named entrypoint in the
 127     // generated interpreter code.
 128 #if defined(ABI_ELFv2)
 129     call_c(CAST_FROM_FN_PTR(address,
 130                             Interpreter::remove_activation_preserving_args_entry),
 131            relocInfo::none);
 132 #else
 133     call_c(CAST_FROM_FN_PTR(FunctionDescriptor*,
 134                             Interpreter::remove_activation_preserving_args_entry),
 135            relocInfo::none);
 136 #endif
 137 
 138     // Jump to Interpreter::_remove_activation_preserving_args_entry.
 139     mtctr(R3_RET);
 140     bctr();
 141 
 142     align(32, 12);
 143     bind(L);
 144   }
 145 }
 146 
 147 void InterpreterMacroAssembler::check_and_handle_earlyret(Register scratch_reg) {
 148   const Register Rthr_state_addr = scratch_reg;
 149   if (JvmtiExport::can_force_early_return()) {
 150     Label Lno_early_ret;
 151     ld(Rthr_state_addr, in_bytes(JavaThread::jvmti_thread_state_offset()), R16_thread);
 152     cmpdi(CCR0, Rthr_state_addr, 0);
 153     beq(CCR0, Lno_early_ret);
 154 
 155     lwz(R0, in_bytes(JvmtiThreadState::earlyret_state_offset()), Rthr_state_addr);
 156     cmpwi(CCR0, R0, JvmtiThreadState::earlyret_pending);
 157     bne(CCR0, Lno_early_ret);
 158 
 159     // Jump to Interpreter::_earlyret_entry.
 160     lwz(R3_ARG1, in_bytes(JvmtiThreadState::earlyret_tos_offset()), Rthr_state_addr);
 161     call_VM_leaf(CAST_FROM_FN_PTR(address, Interpreter::remove_activation_early_entry));
 162     mtlr(R3_RET);
 163     blr();
 164 
 165     align(32, 12);
 166     bind(Lno_early_ret);
 167   }
 168 }
 169 
 170 void InterpreterMacroAssembler::load_earlyret_value(TosState state, Register Rscratch1) {
 171   const Register RjvmtiState = Rscratch1;
 172   const Register Rscratch2   = R0;
 173 
 174   ld(RjvmtiState, in_bytes(JavaThread::jvmti_thread_state_offset()), R16_thread);
 175   li(Rscratch2, 0);
 176 
 177   switch (state) {
 178     case atos: ld(R17_tos, in_bytes(JvmtiThreadState::earlyret_oop_offset()), RjvmtiState);
 179                std(Rscratch2, in_bytes(JvmtiThreadState::earlyret_oop_offset()), RjvmtiState);
 180                break;
 181     case ltos: ld(R17_tos, in_bytes(JvmtiThreadState::earlyret_value_offset()), RjvmtiState);
 182                break;
 183     case btos: // fall through
 184     case ztos: // fall through
 185     case ctos: // fall through
 186     case stos: // fall through
 187     case itos: lwz(R17_tos, in_bytes(JvmtiThreadState::earlyret_value_offset()), RjvmtiState);
 188                break;
 189     case ftos: lfs(F15_ftos, in_bytes(JvmtiThreadState::earlyret_value_offset()), RjvmtiState);
 190                break;
 191     case dtos: lfd(F15_ftos, in_bytes(JvmtiThreadState::earlyret_value_offset()), RjvmtiState);
 192                break;
 193     case vtos: break;
 194     default  : ShouldNotReachHere();
 195   }
 196 
 197   // Clean up tos value in the jvmti thread state.
 198   std(Rscratch2, in_bytes(JvmtiThreadState::earlyret_value_offset()), RjvmtiState);
 199   // Set tos state field to illegal value.
 200   li(Rscratch2, ilgl);
 201   stw(Rscratch2, in_bytes(JvmtiThreadState::earlyret_tos_offset()), RjvmtiState);
 202 }
 203 
 204 // Common code to dispatch and dispatch_only.
 205 // Dispatch value in Lbyte_code and increment Lbcp.
 206 
 207 void InterpreterMacroAssembler::load_dispatch_table(Register dst, address* table) {
 208   address table_base = (address)Interpreter::dispatch_table((TosState)0);
 209   intptr_t table_offs = (intptr_t)table - (intptr_t)table_base;
 210   if (is_simm16(table_offs)) {
 211     addi(dst, R25_templateTableBase, (int)table_offs);
 212   } else {
 213     load_const_optimized(dst, table, R0);
 214   }
 215 }
 216 
 217 void InterpreterMacroAssembler::dispatch_Lbyte_code(TosState state, Register bytecode,
 218                                                     address* table, bool generate_poll) {
 219   assert_different_registers(bytecode, R11_scratch1);
 220 
 221   // Calc dispatch table address.
 222   load_dispatch_table(R11_scratch1, table);
 223 
 224   if (generate_poll) {
 225     address *sfpt_tbl = Interpreter::safept_table(state);
 226     if (table != sfpt_tbl) {
 227       Label dispatch;
 228       ld(R0, in_bytes(JavaThread::polling_word_offset()), R16_thread);
 229       // Armed page has poll_bit set, if poll bit is cleared just continue.
 230       andi_(R0, R0, SafepointMechanism::poll_bit());
 231       beq(CCR0, dispatch);
 232       load_dispatch_table(R11_scratch1, sfpt_tbl);
 233       align(32, 16);
 234       bind(dispatch);
 235     }
 236   }
 237 
 238   sldi(R12_scratch2, bytecode, LogBytesPerWord);
 239   ldx(R11_scratch1, R11_scratch1, R12_scratch2);
 240 
 241   // Jump off!
 242   mtctr(R11_scratch1);
 243   bcctr(bcondAlways, 0, bhintbhBCCTRisNotPredictable);
 244 }
 245 
 246 void InterpreterMacroAssembler::load_receiver(Register Rparam_count, Register Rrecv_dst) {
 247   sldi(Rrecv_dst, Rparam_count, Interpreter::logStackElementSize);
 248   ldx(Rrecv_dst, Rrecv_dst, R15_esp);
 249 }
 250 
 251 // helpers for expression stack
 252 
 253 void InterpreterMacroAssembler::pop_i(Register r) {
 254   lwzu(r, Interpreter::stackElementSize, R15_esp);
 255 }
 256 
 257 void InterpreterMacroAssembler::pop_ptr(Register r) {
 258   ldu(r, Interpreter::stackElementSize, R15_esp);
 259 }
 260 
 261 void InterpreterMacroAssembler::pop_l(Register r) {
 262   ld(r, Interpreter::stackElementSize, R15_esp);
 263   addi(R15_esp, R15_esp, 2 * Interpreter::stackElementSize);
 264 }
 265 
 266 void InterpreterMacroAssembler::pop_f(FloatRegister f) {
 267   lfsu(f, Interpreter::stackElementSize, R15_esp);
 268 }
 269 
 270 void InterpreterMacroAssembler::pop_d(FloatRegister f) {
 271   lfd(f, Interpreter::stackElementSize, R15_esp);
 272   addi(R15_esp, R15_esp, 2 * Interpreter::stackElementSize);
 273 }
 274 
 275 void InterpreterMacroAssembler::push_i(Register r) {
 276   stw(r, 0, R15_esp);
 277   addi(R15_esp, R15_esp, - Interpreter::stackElementSize );
 278 }
 279 
 280 void InterpreterMacroAssembler::push_ptr(Register r) {
 281   std(r, 0, R15_esp);
 282   addi(R15_esp, R15_esp, - Interpreter::stackElementSize );
 283 }
 284 
 285 void InterpreterMacroAssembler::push_l(Register r) {
 286   // Clear unused slot.
 287   load_const_optimized(R0, 0L);
 288   std(R0, 0, R15_esp);
 289   std(r, - Interpreter::stackElementSize, R15_esp);
 290   addi(R15_esp, R15_esp, - 2 * Interpreter::stackElementSize );
 291 }
 292 
 293 void InterpreterMacroAssembler::push_f(FloatRegister f) {
 294   stfs(f, 0, R15_esp);
 295   addi(R15_esp, R15_esp, - Interpreter::stackElementSize );
 296 }
 297 
 298 void InterpreterMacroAssembler::push_d(FloatRegister f)   {
 299   stfd(f, - Interpreter::stackElementSize, R15_esp);
 300   addi(R15_esp, R15_esp, - 2 * Interpreter::stackElementSize );
 301 }
 302 
 303 void InterpreterMacroAssembler::push_2ptrs(Register first, Register second) {
 304   std(first, 0, R15_esp);
 305   std(second, -Interpreter::stackElementSize, R15_esp);
 306   addi(R15_esp, R15_esp, - 2 * Interpreter::stackElementSize );
 307 }
 308 
 309 void InterpreterMacroAssembler::move_l_to_d(Register l, FloatRegister d) {
 310   if (VM_Version::has_mtfprd()) {
 311     mtfprd(d, l);
 312   } else {
 313     std(l, 0, R15_esp);
 314     lfd(d, 0, R15_esp);
 315   }
 316 }
 317 
 318 void InterpreterMacroAssembler::move_d_to_l(FloatRegister d, Register l) {
 319   if (VM_Version::has_mtfprd()) {
 320     mffprd(l, d);
 321   } else {
 322     stfd(d, 0, R15_esp);
 323     ld(l, 0, R15_esp);
 324   }
 325 }
 326 
 327 void InterpreterMacroAssembler::push(TosState state) {
 328   switch (state) {
 329     case atos: push_ptr();                break;
 330     case btos:
 331     case ztos:
 332     case ctos:
 333     case stos:
 334     case itos: push_i();                  break;
 335     case ltos: push_l();                  break;
 336     case ftos: push_f();                  break;
 337     case dtos: push_d();                  break;
 338     case vtos: /* nothing to do */        break;
 339     default  : ShouldNotReachHere();
 340   }
 341 }
 342 
 343 void InterpreterMacroAssembler::pop(TosState state) {
 344   switch (state) {
 345     case atos: pop_ptr();            break;
 346     case btos:
 347     case ztos:
 348     case ctos:
 349     case stos:
 350     case itos: pop_i();              break;
 351     case ltos: pop_l();              break;
 352     case ftos: pop_f();              break;
 353     case dtos: pop_d();              break;
 354     case vtos: /* nothing to do */   break;
 355     default  : ShouldNotReachHere();
 356   }
 357   verify_oop(R17_tos, state);
 358 }
 359 
 360 void InterpreterMacroAssembler::empty_expression_stack() {
 361   addi(R15_esp, R26_monitor, - Interpreter::stackElementSize);
 362 }
 363 
 364 void InterpreterMacroAssembler::get_2_byte_integer_at_bcp(int         bcp_offset,
 365                                                           Register    Rdst,
 366                                                           signedOrNot is_signed) {
 367 #if defined(VM_LITTLE_ENDIAN)
 368   if (bcp_offset) {
 369     load_const_optimized(Rdst, bcp_offset);
 370     lhbrx(Rdst, R14_bcp, Rdst);
 371   } else {
 372     lhbrx(Rdst, R14_bcp);
 373   }
 374   if (is_signed == Signed) {
 375     extsh(Rdst, Rdst);
 376   }
 377 #else
 378   // Read Java big endian format.
 379   if (is_signed == Signed) {
 380     lha(Rdst, bcp_offset, R14_bcp);
 381   } else {
 382     lhz(Rdst, bcp_offset, R14_bcp);
 383   }
 384 #endif
 385 }
 386 
 387 void InterpreterMacroAssembler::get_4_byte_integer_at_bcp(int         bcp_offset,
 388                                                           Register    Rdst,
 389                                                           signedOrNot is_signed) {
 390 #if defined(VM_LITTLE_ENDIAN)
 391   if (bcp_offset) {
 392     load_const_optimized(Rdst, bcp_offset);
 393     lwbrx(Rdst, R14_bcp, Rdst);
 394   } else {
 395     lwbrx(Rdst, R14_bcp);
 396   }
 397   if (is_signed == Signed) {
 398     extsw(Rdst, Rdst);
 399   }
 400 #else
 401   // Read Java big endian format.
 402   if (bcp_offset & 3) { // Offset unaligned?
 403     load_const_optimized(Rdst, bcp_offset);
 404     if (is_signed == Signed) {
 405       lwax(Rdst, R14_bcp, Rdst);
 406     } else {
 407       lwzx(Rdst, R14_bcp, Rdst);
 408     }
 409   } else {
 410     if (is_signed == Signed) {
 411       lwa(Rdst, bcp_offset, R14_bcp);
 412     } else {
 413       lwz(Rdst, bcp_offset, R14_bcp);
 414     }
 415   }
 416 #endif
 417 }
 418 
 419 
 420 // Load the constant pool cache index from the bytecode stream.
 421 //
 422 // Kills / writes:
 423 //   - Rdst, Rscratch
 424 void InterpreterMacroAssembler::get_cache_index_at_bcp(Register Rdst, int bcp_offset,
 425                                                        size_t index_size) {
 426   assert(bcp_offset > 0, "bcp is still pointing to start of bytecode");
 427   // Cache index is always in the native format, courtesy of Rewriter.
 428   if (index_size == sizeof(u2)) {
 429     lhz(Rdst, bcp_offset, R14_bcp);
 430   } else if (index_size == sizeof(u4)) {
 431     if (bcp_offset & 3) {
 432       load_const_optimized(Rdst, bcp_offset);
 433       lwax(Rdst, R14_bcp, Rdst);
 434     } else {
 435       lwa(Rdst, bcp_offset, R14_bcp);
 436     }
 437     assert(ConstantPool::decode_invokedynamic_index(~123) == 123, "else change next line");
 438     nand(Rdst, Rdst, Rdst); // convert to plain index
 439   } else if (index_size == sizeof(u1)) {
 440     lbz(Rdst, bcp_offset, R14_bcp);
 441   } else {
 442     ShouldNotReachHere();
 443   }
 444   // Rdst now contains cp cache index.
 445 }
 446 
 447 void InterpreterMacroAssembler::get_cache_and_index_at_bcp(Register cache, int bcp_offset,
 448                                                            size_t index_size) {
 449   get_cache_index_at_bcp(cache, bcp_offset, index_size);
 450   sldi(cache, cache, exact_log2(in_words(ConstantPoolCacheEntry::size()) * BytesPerWord));
 451   add(cache, R27_constPoolCache, cache);
 452 }
 453 
 454 // Load 4-byte signed or unsigned integer in Java format (that is, big-endian format)
 455 // from (Rsrc)+offset.
 456 void InterpreterMacroAssembler::get_u4(Register Rdst, Register Rsrc, int offset,
 457                                        signedOrNot is_signed) {
 458 #if defined(VM_LITTLE_ENDIAN)
 459   if (offset) {
 460     load_const_optimized(Rdst, offset);
 461     lwbrx(Rdst, Rdst, Rsrc);
 462   } else {
 463     lwbrx(Rdst, Rsrc);
 464   }
 465   if (is_signed == Signed) {
 466     extsw(Rdst, Rdst);
 467   }
 468 #else
 469   if (is_signed == Signed) {
 470     lwa(Rdst, offset, Rsrc);
 471   } else {
 472     lwz(Rdst, offset, Rsrc);
 473   }
 474 #endif
 475 }
 476 
 477 // Load object from cpool->resolved_references(index).
 478 // Kills:
 479 //   - index
 480 void InterpreterMacroAssembler::load_resolved_reference_at_index(Register result, Register index,
 481                                                                  Register tmp1, Register tmp2,
 482                                                                  Label *L_handle_null) {
 483   assert_different_registers(result, index, tmp1, tmp2);
 484   assert(index->is_nonvolatile(), "needs to survive C-call in resolve_oop_handle");
 485   get_constant_pool(result);
 486 
 487   // Convert from field index to resolved_references() index and from
 488   // word index to byte offset. Since this is a java object, it can be compressed.
 489   sldi(index, index, LogBytesPerHeapOop);
 490   // Load pointer for resolved_references[] objArray.
 491   ld(result, ConstantPool::cache_offset_in_bytes(), result);
 492   ld(result, ConstantPoolCache::resolved_references_offset_in_bytes(), result);
 493   resolve_oop_handle(result, tmp1, tmp2, MacroAssembler::PRESERVATION_NONE);
 494 #ifdef ASSERT
 495   Label index_ok;
 496   lwa(R0, arrayOopDesc::length_offset_in_bytes(), result);
 497   sldi(R0, R0, LogBytesPerHeapOop);
 498   cmpd(CCR0, index, R0);
 499   blt(CCR0, index_ok);
 500   stop("resolved reference index out of bounds");
 501   bind(index_ok);
 502 #endif
 503   // Add in the index.
 504   add(result, index, result);
 505   load_heap_oop(result, arrayOopDesc::base_offset_in_bytes(T_OBJECT), result,
 506                 tmp1, tmp2,
 507                 MacroAssembler::PRESERVATION_NONE,
 508                 0, L_handle_null);
 509 }
 510 
 511 // load cpool->resolved_klass_at(index)
 512 void InterpreterMacroAssembler::load_resolved_klass_at_offset(Register Rcpool, Register Roffset, Register Rklass) {
 513   // int value = *(Rcpool->int_at_addr(which));
 514   // int resolved_klass_index = extract_low_short_from_int(value);
 515   add(Roffset, Rcpool, Roffset);
 516 #if defined(VM_LITTLE_ENDIAN)
 517   lhz(Roffset, sizeof(ConstantPool), Roffset);     // Roffset = resolved_klass_index
 518 #else
 519   lhz(Roffset, sizeof(ConstantPool) + 2, Roffset); // Roffset = resolved_klass_index
 520 #endif
 521 
 522   ld(Rklass, ConstantPool::resolved_klasses_offset_in_bytes(), Rcpool); // Rklass = Rcpool->_resolved_klasses
 523 
 524   sldi(Roffset, Roffset, LogBytesPerWord);
 525   addi(Roffset, Roffset, Array<Klass*>::base_offset_in_bytes());
 526   isync(); // Order load of instance Klass wrt. tags.
 527   ldx(Rklass, Rklass, Roffset);
 528 }
 529 
 530 void InterpreterMacroAssembler::load_resolved_method_at_index(int byte_no,
 531                                                               Register cache,
 532                                                               Register method) {
 533   const int method_offset = in_bytes(
 534     ConstantPoolCache::base_offset() +
 535       ((byte_no == TemplateTable::f2_byte)
 536        ? ConstantPoolCacheEntry::f2_offset()
 537        : ConstantPoolCacheEntry::f1_offset()));
 538 
 539   ld(method, method_offset, cache); // get f1 Method*
 540 }
 541 
 542 // Generate a subtype check: branch to ok_is_subtype if sub_klass is
 543 // a subtype of super_klass. Blows registers Rsub_klass, tmp1, tmp2.
 544 void InterpreterMacroAssembler::gen_subtype_check(Register Rsub_klass, Register Rsuper_klass, Register Rtmp1,
 545                                                   Register Rtmp2, Register Rtmp3, Label &ok_is_subtype) {
 546   // Profile the not-null value's klass.
 547   profile_typecheck(Rsub_klass, Rtmp1, Rtmp2);
 548   check_klass_subtype(Rsub_klass, Rsuper_klass, Rtmp1, Rtmp2, ok_is_subtype);
 549   profile_typecheck_failed(Rtmp1, Rtmp2);
 550 }
 551 
 552 // Separate these two to allow for delay slot in middle.
 553 // These are used to do a test and full jump to exception-throwing code.
 554 
 555 // Check that index is in range for array, then shift index by index_shift,
 556 // and put arrayOop + shifted_index into res.
 557 // Note: res is still shy of address by array offset into object.
 558 
 559 void InterpreterMacroAssembler::index_check_without_pop(Register Rarray, Register Rindex,
 560                                                         int index_shift, Register Rtmp, Register Rres) {
 561   // Check that index is in range for array, then shift index by index_shift,
 562   // and put arrayOop + shifted_index into res.
 563   // Note: res is still shy of address by array offset into object.
 564   // Kills:
 565   //   - Rindex
 566   // Writes:
 567   //   - Rres: Address that corresponds to the array index if check was successful.
 568   verify_oop(Rarray);
 569   const Register Rlength   = R0;
 570   const Register RsxtIndex = Rtmp;
 571   Label LisNull, LnotOOR;
 572 
 573   // Array nullcheck
 574   if (!ImplicitNullChecks) {
 575     cmpdi(CCR0, Rarray, 0);
 576     beq(CCR0, LisNull);
 577   } else {
 578     null_check_throw(Rarray, arrayOopDesc::length_offset_in_bytes(), /*temp*/RsxtIndex);
 579   }
 580 
 581   // Rindex might contain garbage in upper bits (remember that we don't sign extend
 582   // during integer arithmetic operations). So kill them and put value into same register
 583   // where ArrayIndexOutOfBounds would expect the index in.
 584   rldicl(RsxtIndex, Rindex, 0, 32); // zero extend 32 bit -> 64 bit
 585 
 586   // Index check
 587   lwz(Rlength, arrayOopDesc::length_offset_in_bytes(), Rarray);
 588   cmplw(CCR0, Rindex, Rlength);
 589   sldi(RsxtIndex, RsxtIndex, index_shift);
 590   blt(CCR0, LnotOOR);
 591   // Index should be in R17_tos, array should be in R4_ARG2.
 592   mr_if_needed(R17_tos, Rindex);
 593   mr_if_needed(R4_ARG2, Rarray);
 594   load_dispatch_table(Rtmp, (address*)Interpreter::_throw_ArrayIndexOutOfBoundsException_entry);
 595   mtctr(Rtmp);
 596   bctr();
 597 
 598   if (!ImplicitNullChecks) {
 599     bind(LisNull);
 600     load_dispatch_table(Rtmp, (address*)Interpreter::_throw_NullPointerException_entry);
 601     mtctr(Rtmp);
 602     bctr();
 603   }
 604 
 605   align(32, 16);
 606   bind(LnotOOR);
 607 
 608   // Calc address
 609   add(Rres, RsxtIndex, Rarray);
 610 }
 611 
 612 void InterpreterMacroAssembler::index_check(Register array, Register index,
 613                                             int index_shift, Register tmp, Register res) {
 614   // pop array
 615   pop_ptr(array);
 616 
 617   // check array
 618   index_check_without_pop(array, index, index_shift, tmp, res);
 619 }
 620 
 621 void InterpreterMacroAssembler::get_const(Register Rdst) {
 622   ld(Rdst, in_bytes(Method::const_offset()), R19_method);
 623 }
 624 
 625 void InterpreterMacroAssembler::get_constant_pool(Register Rdst) {
 626   get_const(Rdst);
 627   ld(Rdst, in_bytes(ConstMethod::constants_offset()), Rdst);
 628 }
 629 
 630 void InterpreterMacroAssembler::get_constant_pool_cache(Register Rdst) {
 631   get_constant_pool(Rdst);
 632   ld(Rdst, ConstantPool::cache_offset_in_bytes(), Rdst);
 633 }
 634 
 635 void InterpreterMacroAssembler::get_cpool_and_tags(Register Rcpool, Register Rtags) {
 636   get_constant_pool(Rcpool);
 637   ld(Rtags, ConstantPool::tags_offset_in_bytes(), Rcpool);
 638 }
 639 
 640 // Unlock if synchronized method.
 641 //
 642 // Unlock the receiver if this is a synchronized method.
 643 // Unlock any Java monitors from synchronized blocks.
 644 //
 645 // If there are locked Java monitors
 646 //   If throw_monitor_exception
 647 //     throws IllegalMonitorStateException
 648 //   Else if install_monitor_exception
 649 //     installs IllegalMonitorStateException
 650 //   Else
 651 //     no error processing
 652 void InterpreterMacroAssembler::unlock_if_synchronized_method(TosState state,
 653                                                               bool throw_monitor_exception,
 654                                                               bool install_monitor_exception) {
 655   Label Lunlocked, Lno_unlock;
 656   {
 657     Register Rdo_not_unlock_flag = R11_scratch1;
 658     Register Raccess_flags       = R12_scratch2;
 659 
 660     // Check if synchronized method or unlocking prevented by
 661     // JavaThread::do_not_unlock_if_synchronized flag.
 662     lbz(Rdo_not_unlock_flag, in_bytes(JavaThread::do_not_unlock_if_synchronized_offset()), R16_thread);
 663     lwz(Raccess_flags, in_bytes(Method::access_flags_offset()), R19_method);
 664     li(R0, 0);
 665     stb(R0, in_bytes(JavaThread::do_not_unlock_if_synchronized_offset()), R16_thread); // reset flag
 666 
 667     push(state);
 668 
 669     // Skip if we don't have to unlock.
 670     rldicl_(R0, Raccess_flags, 64-JVM_ACC_SYNCHRONIZED_BIT, 63); // Extract bit and compare to 0.
 671     beq(CCR0, Lunlocked);
 672 
 673     cmpwi(CCR0, Rdo_not_unlock_flag, 0);
 674     bne(CCR0, Lno_unlock);
 675   }
 676 
 677   // Unlock
 678   {
 679     Register Rmonitor_base = R11_scratch1;
 680 
 681     Label Lunlock;
 682     // If it's still locked, everything is ok, unlock it.
 683     ld(Rmonitor_base, 0, R1_SP);
 684     addi(Rmonitor_base, Rmonitor_base,
 685          -(frame::ijava_state_size + frame::interpreter_frame_monitor_size_in_bytes())); // Monitor base
 686 
 687     ld(R0, BasicObjectLock::obj_offset_in_bytes(), Rmonitor_base);
 688     cmpdi(CCR0, R0, 0);
 689     bne(CCR0, Lunlock);
 690 
 691     // If it's already unlocked, throw exception.
 692     if (throw_monitor_exception) {
 693       call_VM(noreg, CAST_FROM_FN_PTR(address, InterpreterRuntime::throw_illegal_monitor_state_exception));
 694       should_not_reach_here();
 695     } else {
 696       if (install_monitor_exception) {
 697         call_VM(noreg, CAST_FROM_FN_PTR(address, InterpreterRuntime::new_illegal_monitor_state_exception));
 698         b(Lunlocked);
 699       }
 700     }
 701 
 702     bind(Lunlock);
 703     unlock_object(Rmonitor_base);
 704   }
 705 
 706   // Check that all other monitors are unlocked. Throw IllegelMonitorState exception if not.
 707   bind(Lunlocked);
 708   {
 709     Label Lexception, Lrestart;
 710     Register Rcurrent_obj_addr = R11_scratch1;
 711     const int delta = frame::interpreter_frame_monitor_size_in_bytes();
 712     assert((delta & LongAlignmentMask) == 0, "sizeof BasicObjectLock must be even number of doublewords");
 713 
 714     bind(Lrestart);
 715     // Set up search loop: Calc num of iterations.
 716     {
 717       Register Riterations = R12_scratch2;
 718       Register Rmonitor_base = Rcurrent_obj_addr;
 719       ld(Rmonitor_base, 0, R1_SP);
 720       addi(Rmonitor_base, Rmonitor_base, - frame::ijava_state_size);  // Monitor base
 721 
 722       subf_(Riterations, R26_monitor, Rmonitor_base);
 723       ble(CCR0, Lno_unlock);
 724 
 725       addi(Rcurrent_obj_addr, Rmonitor_base,
 726            BasicObjectLock::obj_offset_in_bytes() - frame::interpreter_frame_monitor_size_in_bytes());
 727       // Check if any monitor is on stack, bail out if not
 728       srdi(Riterations, Riterations, exact_log2(delta));
 729       mtctr(Riterations);
 730     }
 731 
 732     // The search loop: Look for locked monitors.
 733     {
 734       const Register Rcurrent_obj = R0;
 735       Label Lloop;
 736 
 737       ld(Rcurrent_obj, 0, Rcurrent_obj_addr);
 738       addi(Rcurrent_obj_addr, Rcurrent_obj_addr, -delta);
 739       bind(Lloop);
 740 
 741       // Check if current entry is used.
 742       cmpdi(CCR0, Rcurrent_obj, 0);
 743       bne(CCR0, Lexception);
 744       // Preload next iteration's compare value.
 745       ld(Rcurrent_obj, 0, Rcurrent_obj_addr);
 746       addi(Rcurrent_obj_addr, Rcurrent_obj_addr, -delta);
 747       bdnz(Lloop);
 748     }
 749     // Fell through: Everything's unlocked => finish.
 750     b(Lno_unlock);
 751 
 752     // An object is still locked => need to throw exception.
 753     bind(Lexception);
 754     if (throw_monitor_exception) {
 755       call_VM(noreg, CAST_FROM_FN_PTR(address, InterpreterRuntime::throw_illegal_monitor_state_exception));
 756       should_not_reach_here();
 757     } else {
 758       // Stack unrolling. Unlock object and if requested, install illegal_monitor_exception.
 759       // Unlock does not block, so don't have to worry about the frame.
 760       Register Rmonitor_addr = R11_scratch1;
 761       addi(Rmonitor_addr, Rcurrent_obj_addr, -BasicObjectLock::obj_offset_in_bytes() + delta);
 762       unlock_object(Rmonitor_addr);
 763       if (install_monitor_exception) {
 764         call_VM(noreg, CAST_FROM_FN_PTR(address, InterpreterRuntime::new_illegal_monitor_state_exception));
 765       }
 766       b(Lrestart);
 767     }
 768   }
 769 
 770   align(32, 12);
 771   bind(Lno_unlock);
 772   pop(state);
 773 }
 774 
 775 // Support function for remove_activation & Co.
 776 void InterpreterMacroAssembler::merge_frames(Register Rsender_sp, Register return_pc,
 777                                              Register Rscratch1, Register Rscratch2) {
 778   // Pop interpreter frame.
 779   ld(Rscratch1, 0, R1_SP); // *SP
 780   ld(Rsender_sp, _ijava_state_neg(sender_sp), Rscratch1); // top_frame_sp
 781   ld(Rscratch2, 0, Rscratch1); // **SP
 782   if (return_pc!=noreg) {
 783     ld(return_pc, _abi0(lr), Rscratch1); // LR
 784   }
 785 
 786   // Merge top frames.
 787   subf(Rscratch1, R1_SP, Rsender_sp); // top_frame_sp - SP
 788   stdux(Rscratch2, R1_SP, Rscratch1); // atomically set *(SP = top_frame_sp) = **SP
 789 }
 790 
 791 void InterpreterMacroAssembler::narrow(Register result) {
 792   Register ret_type = R11_scratch1;
 793   ld(R11_scratch1, in_bytes(Method::const_offset()), R19_method);
 794   lbz(ret_type, in_bytes(ConstMethod::result_type_offset()), R11_scratch1);
 795 
 796   Label notBool, notByte, notChar, done;
 797 
 798   // common case first
 799   cmpwi(CCR0, ret_type, T_INT);
 800   beq(CCR0, done);
 801 
 802   cmpwi(CCR0, ret_type, T_BOOLEAN);
 803   bne(CCR0, notBool);
 804   andi(result, result, 0x1);
 805   b(done);
 806 
 807   bind(notBool);
 808   cmpwi(CCR0, ret_type, T_BYTE);
 809   bne(CCR0, notByte);
 810   extsb(result, result);
 811   b(done);
 812 
 813   bind(notByte);
 814   cmpwi(CCR0, ret_type, T_CHAR);
 815   bne(CCR0, notChar);
 816   andi(result, result, 0xffff);
 817   b(done);
 818 
 819   bind(notChar);
 820   // cmpwi(CCR0, ret_type, T_SHORT);  // all that's left
 821   // bne(CCR0, done);
 822   extsh(result, result);
 823 
 824   // Nothing to do for T_INT
 825   bind(done);
 826 }
 827 
 828 // Remove activation.
 829 //
 830 // Apply stack watermark barrier.
 831 // Unlock the receiver if this is a synchronized method.
 832 // Unlock any Java monitors from synchronized blocks.
 833 // Remove the activation from the stack.
 834 //
 835 // If there are locked Java monitors
 836 //    If throw_monitor_exception
 837 //       throws IllegalMonitorStateException
 838 //    Else if install_monitor_exception
 839 //       installs IllegalMonitorStateException
 840 //    Else
 841 //       no error processing
 842 void InterpreterMacroAssembler::remove_activation(TosState state,
 843                                                   bool throw_monitor_exception,
 844                                                   bool install_monitor_exception) {
 845   BLOCK_COMMENT("remove_activation {");
 846 
 847   // The below poll is for the stack watermark barrier. It allows fixing up frames lazily,
 848   // that would normally not be safe to use. Such bad returns into unsafe territory of
 849   // the stack, will call InterpreterRuntime::at_unwind.
 850   Label slow_path;
 851   Label fast_path;
 852   safepoint_poll(slow_path, R11_scratch1, true /* at_return */, false /* in_nmethod */);
 853   b(fast_path);
 854   bind(slow_path);
 855   push(state);
 856   set_last_Java_frame(R1_SP, noreg);
 857   call_VM_leaf(CAST_FROM_FN_PTR(address, InterpreterRuntime::at_unwind), R16_thread);
 858   reset_last_Java_frame();
 859   pop(state);
 860   align(32);
 861   bind(fast_path);
 862 
 863   unlock_if_synchronized_method(state, throw_monitor_exception, install_monitor_exception);
 864 
 865   // Save result (push state before jvmti call and pop it afterwards) and notify jvmti.
 866   notify_method_exit(false, state, NotifyJVMTI, true);
 867 
 868   BLOCK_COMMENT("reserved_stack_check:");
 869   if (StackReservedPages > 0) {
 870     // Test if reserved zone needs to be enabled.
 871     Label no_reserved_zone_enabling;
 872 
 873     // Compare frame pointers. There is no good stack pointer, as with stack
 874     // frame compression we can get different SPs when we do calls. A subsequent
 875     // call could have a smaller SP, so that this compare succeeds for an
 876     // inner call of the method annotated with ReservedStack.
 877     ld_ptr(R0, JavaThread::reserved_stack_activation_offset(), R16_thread);
 878     ld_ptr(R11_scratch1, _abi0(callers_sp), R1_SP); // Load frame pointer.
 879     cmpld(CCR0, R11_scratch1, R0);
 880     blt_predict_taken(CCR0, no_reserved_zone_enabling);
 881 
 882     // Enable reserved zone again, throw stack overflow exception.
 883     call_VM_leaf(CAST_FROM_FN_PTR(address, SharedRuntime::enable_stack_reserved_zone), R16_thread);
 884     call_VM(noreg, CAST_FROM_FN_PTR(address, InterpreterRuntime::throw_delayed_StackOverflowError));
 885 
 886     should_not_reach_here();
 887 
 888     bind(no_reserved_zone_enabling);
 889   }
 890 
 891   verify_oop(R17_tos, state);
 892   verify_thread();
 893 
 894   merge_frames(/*top_frame_sp*/ R21_sender_SP, /*return_pc*/ R0, R11_scratch1, R12_scratch2);
 895   mtlr(R0);
 896   BLOCK_COMMENT("} remove_activation");
 897 }
 898 
 899 // Lock object
 900 //
 901 // Registers alive
 902 //   monitor - Address of the BasicObjectLock to be used for locking,
 903 //             which must be initialized with the object to lock.
 904 //   object  - Address of the object to be locked.
 905 //
 906 void InterpreterMacroAssembler::lock_object(Register monitor, Register object) {
 907   if (UseHeavyMonitors) {
 908     call_VM(noreg, CAST_FROM_FN_PTR(address, InterpreterRuntime::monitorenter), monitor);
 909   } else {
 910     // template code:
 911     //
 912     // markWord displaced_header = obj->mark().set_unlocked();
 913     // monitor->lock()->set_displaced_header(displaced_header);
 914     // if (Atomic::cmpxchg(/*addr*/obj->mark_addr(), /*cmp*/displaced_header, /*ex=*/monitor) == displaced_header) {
 915     //   // We stored the monitor address into the object's mark word.
 916     // } else if (THREAD->is_lock_owned((address)displaced_header))
 917     //   // Simple recursive case.
 918     //   monitor->lock()->set_displaced_header(NULL);
 919     // } else {
 920     //   // Slow path.
 921     //   InterpreterRuntime::monitorenter(THREAD, monitor);
 922     // }
 923 
 924     const Register displaced_header = R7_ARG5;
 925     const Register object_mark_addr = R8_ARG6;
 926     const Register current_header   = R9_ARG7;
 927     const Register tmp              = R10_ARG8;
 928 
 929     Label done;
 930     Label cas_failed, slow_case;
 931 
 932     assert_different_registers(displaced_header, object_mark_addr, current_header, tmp);
 933 
 934     // markWord displaced_header = obj->mark().set_unlocked();
 935 
 936     // Load markWord from object into displaced_header.
 937     ld(displaced_header, oopDesc::mark_offset_in_bytes(), object);
 938 
 939     if (DiagnoseSyncOnValueBasedClasses != 0) {
 940       load_klass(tmp, object);
 941       lwz(tmp, in_bytes(Klass::access_flags_offset()), tmp);
 942       testbitdi(CCR0, R0, tmp, exact_log2(JVM_ACC_IS_VALUE_BASED_CLASS));
 943       bne(CCR0, slow_case);
 944     }
 945 
 946     // Set displaced_header to be (markWord of object | UNLOCK_VALUE).
 947     ori(displaced_header, displaced_header, markWord::unlocked_value);
 948 
 949     // monitor->lock()->set_displaced_header(displaced_header);
 950 
 951     // Initialize the box (Must happen before we update the object mark!).
 952     std(displaced_header, BasicObjectLock::lock_offset_in_bytes() +
 953         BasicLock::displaced_header_offset_in_bytes(), monitor);
 954 
 955     // if (Atomic::cmpxchg(/*addr*/obj->mark_addr(), /*cmp*/displaced_header, /*ex=*/monitor) == displaced_header) {
 956 
 957     // Store stack address of the BasicObjectLock (this is monitor) into object.
 958     addi(object_mark_addr, object, oopDesc::mark_offset_in_bytes());
 959 
 960     // Must fence, otherwise, preceding store(s) may float below cmpxchg.
 961     // CmpxchgX sets CCR0 to cmpX(current, displaced).
 962     cmpxchgd(/*flag=*/CCR0,
 963              /*current_value=*/current_header,
 964              /*compare_value=*/displaced_header, /*exchange_value=*/monitor,
 965              /*where=*/object_mark_addr,
 966              MacroAssembler::MemBarRel | MacroAssembler::MemBarAcq,
 967              MacroAssembler::cmpxchgx_hint_acquire_lock(),
 968              noreg,
 969              &cas_failed,
 970              /*check without membar and ldarx first*/true);
 971 
 972     // If the compare-and-exchange succeeded, then we found an unlocked
 973     // object and we have now locked it.
 974     b(done);
 975     bind(cas_failed);
 976 
 977     // } else if (THREAD->is_lock_owned((address)displaced_header))
 978     //   // Simple recursive case.
 979     //   monitor->lock()->set_displaced_header(NULL);
 980 
 981     // We did not see an unlocked object so try the fast recursive case.
 982 
 983     // Check if owner is self by comparing the value in the markWord of object
 984     // (current_header) with the stack pointer.
 985     sub(current_header, current_header, R1_SP);
 986 
 987     assert(os::vm_page_size() > 0xfff, "page size too small - change the constant");
 988     load_const_optimized(tmp, ~(os::vm_page_size()-1) | markWord::lock_mask_in_place);
 989 
 990     and_(R0/*==0?*/, current_header, tmp);
 991     // If condition is true we are done and hence we can store 0 in the displaced
 992     // header indicating it is a recursive lock.
 993     bne(CCR0, slow_case);
 994     std(R0/*==0!*/, BasicObjectLock::lock_offset_in_bytes() +
 995         BasicLock::displaced_header_offset_in_bytes(), monitor);
 996     b(done);
 997 
 998     // } else {
 999     //   // Slow path.
1000     //   InterpreterRuntime::monitorenter(THREAD, monitor);
1001 
1002     // None of the above fast optimizations worked so we have to get into the
1003     // slow case of monitor enter.
1004     bind(slow_case);
1005     call_VM(noreg, CAST_FROM_FN_PTR(address, InterpreterRuntime::monitorenter), monitor);
1006     // }
1007     align(32, 12);
1008     bind(done);
1009   }
1010 }
1011 
1012 // Unlocks an object. Used in monitorexit bytecode and remove_activation.
1013 //
1014 // Registers alive
1015 //   monitor - Address of the BasicObjectLock to be used for locking,
1016 //             which must be initialized with the object to lock.
1017 //
1018 // Throw IllegalMonitorException if object is not locked by current thread.
1019 void InterpreterMacroAssembler::unlock_object(Register monitor) {
1020   if (UseHeavyMonitors) {
1021     call_VM_leaf(CAST_FROM_FN_PTR(address, InterpreterRuntime::monitorexit), monitor);
1022   } else {
1023 
1024     // template code:
1025     //
1026     // if ((displaced_header = monitor->displaced_header()) == NULL) {
1027     //   // Recursive unlock. Mark the monitor unlocked by setting the object field to NULL.
1028     //   monitor->set_obj(NULL);
1029     // } else if (Atomic::cmpxchg(obj->mark_addr(), monitor, displaced_header) == monitor) {
1030     //   // We swapped the unlocked mark in displaced_header into the object's mark word.
1031     //   monitor->set_obj(NULL);
1032     // } else {
1033     //   // Slow path.
1034     //   InterpreterRuntime::monitorexit(monitor);
1035     // }
1036 
1037     const Register object           = R7_ARG5;
1038     const Register displaced_header = R8_ARG6;
1039     const Register object_mark_addr = R9_ARG7;
1040     const Register current_header   = R10_ARG8;
1041 
1042     Label free_slot;
1043     Label slow_case;
1044 
1045     assert_different_registers(object, displaced_header, object_mark_addr, current_header);
1046 
1047     // Test first if we are in the fast recursive case.
1048     ld(displaced_header, BasicObjectLock::lock_offset_in_bytes() +
1049            BasicLock::displaced_header_offset_in_bytes(), monitor);
1050 
1051     // If the displaced header is zero, we have a recursive unlock.
1052     cmpdi(CCR0, displaced_header, 0);
1053     beq(CCR0, free_slot); // recursive unlock
1054 
1055     // } else if (Atomic::cmpxchg(obj->mark_addr(), monitor, displaced_header) == monitor) {
1056     //   // We swapped the unlocked mark in displaced_header into the object's mark word.
1057     //   monitor->set_obj(NULL);
1058 
1059     // If we still have a lightweight lock, unlock the object and be done.
1060 
1061     // The object address from the monitor is in object.
1062     ld(object, BasicObjectLock::obj_offset_in_bytes(), monitor);
1063     addi(object_mark_addr, object, oopDesc::mark_offset_in_bytes());
1064 
1065     // We have the displaced header in displaced_header. If the lock is still
1066     // lightweight, it will contain the monitor address and we'll store the
1067     // displaced header back into the object's mark word.
1068     // CmpxchgX sets CCR0 to cmpX(current, monitor).
1069     cmpxchgd(/*flag=*/CCR0,
1070              /*current_value=*/current_header,
1071              /*compare_value=*/monitor, /*exchange_value=*/displaced_header,
1072              /*where=*/object_mark_addr,
1073              MacroAssembler::MemBarRel,
1074              MacroAssembler::cmpxchgx_hint_release_lock(),
1075              noreg,
1076              &slow_case);
1077     b(free_slot);
1078 
1079     // } else {
1080     //   // Slow path.
1081     //   InterpreterRuntime::monitorexit(monitor);
1082 
1083     // The lock has been converted into a heavy lock and hence
1084     // we need to get into the slow case.
1085     bind(slow_case);
1086     call_VM_leaf(CAST_FROM_FN_PTR(address, InterpreterRuntime::monitorexit), monitor);
1087     // }
1088 
1089     Label done;
1090     b(done); // Monitor register may be overwritten! Runtime has already freed the slot.
1091 
1092     // Exchange worked, do monitor->set_obj(NULL);
1093     align(32, 12);
1094     bind(free_slot);
1095     li(R0, 0);
1096     std(R0, BasicObjectLock::obj_offset_in_bytes(), monitor);
1097     bind(done);
1098   }
1099 }
1100 
1101 // Load compiled (i2c) or interpreter entry when calling from interpreted and
1102 // do the call. Centralized so that all interpreter calls will do the same actions.
1103 // If jvmti single stepping is on for a thread we must not call compiled code.
1104 //
1105 // Input:
1106 //   - Rtarget_method: method to call
1107 //   - Rret_addr:      return address
1108 //   - 2 scratch regs
1109 //
1110 void InterpreterMacroAssembler::call_from_interpreter(Register Rtarget_method, Register Rret_addr,
1111                                                       Register Rscratch1, Register Rscratch2) {
1112   assert_different_registers(Rscratch1, Rscratch2, Rtarget_method, Rret_addr);
1113   // Assume we want to go compiled if available.
1114   const Register Rtarget_addr = Rscratch1;
1115   const Register Rinterp_only = Rscratch2;
1116 
1117   ld(Rtarget_addr, in_bytes(Method::from_interpreted_offset()), Rtarget_method);
1118 
1119   if (JvmtiExport::can_post_interpreter_events()) {
1120     lwz(Rinterp_only, in_bytes(JavaThread::interp_only_mode_offset()), R16_thread);
1121 
1122     // JVMTI events, such as single-stepping, are implemented partly by avoiding running
1123     // compiled code in threads for which the event is enabled. Check here for
1124     // interp_only_mode if these events CAN be enabled.
1125     Label done;
1126     verify_thread();
1127     cmpwi(CCR0, Rinterp_only, 0);
1128     beq(CCR0, done);
1129     ld(Rtarget_addr, in_bytes(Method::interpreter_entry_offset()), Rtarget_method);
1130     align(32, 12);
1131     bind(done);
1132   }
1133 
1134 #ifdef ASSERT
1135   {
1136     Label Lok;
1137     cmpdi(CCR0, Rtarget_addr, 0);
1138     bne(CCR0, Lok);
1139     stop("null entry point");
1140     bind(Lok);
1141   }
1142 #endif // ASSERT
1143 
1144   mr(R21_sender_SP, R1_SP);
1145 
1146   // Calc a precise SP for the call. The SP value we calculated in
1147   // generate_fixed_frame() is based on the max_stack() value, so we would waste stack space
1148   // if esp is not max. Also, the i2c adapter extends the stack space without restoring
1149   // our pre-calced value, so repeating calls via i2c would result in stack overflow.
1150   // Since esp already points to an empty slot, we just have to sub 1 additional slot
1151   // to meet the abi scratch requirements.
1152   // The max_stack pointer will get restored by means of the GR_Lmax_stack local in
1153   // the return entry of the interpreter.
1154   addi(Rscratch2, R15_esp, Interpreter::stackElementSize - frame::abi_reg_args_size);
1155   clrrdi(Rscratch2, Rscratch2, exact_log2(frame::alignment_in_bytes)); // round towards smaller address
1156   resize_frame_absolute(Rscratch2, Rscratch2, R0);
1157 
1158   mr_if_needed(R19_method, Rtarget_method);
1159   mtctr(Rtarget_addr);
1160   mtlr(Rret_addr);
1161 
1162   save_interpreter_state(Rscratch2);
1163 #ifdef ASSERT
1164   ld(Rscratch1, _ijava_state_neg(top_frame_sp), Rscratch2); // Rscratch2 contains fp
1165   cmpd(CCR0, R21_sender_SP, Rscratch1);
1166   asm_assert_eq("top_frame_sp incorrect");
1167 #endif
1168 
1169   bctr();
1170 }
1171 
1172 // Set the method data pointer for the current bcp.
1173 void InterpreterMacroAssembler::set_method_data_pointer_for_bcp() {
1174   assert(ProfileInterpreter, "must be profiling interpreter");
1175   Label get_continue;
1176   ld(R28_mdx, in_bytes(Method::method_data_offset()), R19_method);
1177   test_method_data_pointer(get_continue);
1178   call_VM_leaf(CAST_FROM_FN_PTR(address, InterpreterRuntime::bcp_to_di), R19_method, R14_bcp);
1179 
1180   addi(R28_mdx, R28_mdx, in_bytes(MethodData::data_offset()));
1181   add(R28_mdx, R28_mdx, R3_RET);
1182   bind(get_continue);
1183 }
1184 
1185 // Test ImethodDataPtr. If it is null, continue at the specified label.
1186 void InterpreterMacroAssembler::test_method_data_pointer(Label& zero_continue) {
1187   assert(ProfileInterpreter, "must be profiling interpreter");
1188   cmpdi(CCR0, R28_mdx, 0);
1189   beq(CCR0, zero_continue);
1190 }
1191 
1192 void InterpreterMacroAssembler::verify_method_data_pointer() {
1193   assert(ProfileInterpreter, "must be profiling interpreter");
1194 #ifdef ASSERT
1195   Label verify_continue;
1196   test_method_data_pointer(verify_continue);
1197 
1198   // If the mdp is valid, it will point to a DataLayout header which is
1199   // consistent with the bcp. The converse is highly probable also.
1200   lhz(R11_scratch1, in_bytes(DataLayout::bci_offset()), R28_mdx);
1201   ld(R12_scratch2, in_bytes(Method::const_offset()), R19_method);
1202   addi(R11_scratch1, R11_scratch1, in_bytes(ConstMethod::codes_offset()));
1203   add(R11_scratch1, R12_scratch2, R12_scratch2);
1204   cmpd(CCR0, R11_scratch1, R14_bcp);
1205   beq(CCR0, verify_continue);
1206 
1207   call_VM_leaf(CAST_FROM_FN_PTR(address, InterpreterRuntime::verify_mdp ), R19_method, R14_bcp, R28_mdx);
1208 
1209   bind(verify_continue);
1210 #endif
1211 }
1212 
1213 // Store a value at some constant offset from the method data pointer.
1214 void InterpreterMacroAssembler::set_mdp_data_at(int constant, Register value) {
1215   assert(ProfileInterpreter, "must be profiling interpreter");
1216 
1217   std(value, constant, R28_mdx);
1218 }
1219 
1220 // Increment the value at some constant offset from the method data pointer.
1221 void InterpreterMacroAssembler::increment_mdp_data_at(int constant,
1222                                                       Register counter_addr,
1223                                                       Register Rbumped_count,
1224                                                       bool decrement) {
1225   // Locate the counter at a fixed offset from the mdp:
1226   addi(counter_addr, R28_mdx, constant);
1227   increment_mdp_data_at(counter_addr, Rbumped_count, decrement);
1228 }
1229 
1230 // Increment the value at some non-fixed (reg + constant) offset from
1231 // the method data pointer.
1232 void InterpreterMacroAssembler::increment_mdp_data_at(Register reg,
1233                                                       int constant,
1234                                                       Register scratch,
1235                                                       Register Rbumped_count,
1236                                                       bool decrement) {
1237   // Add the constant to reg to get the offset.
1238   add(scratch, R28_mdx, reg);
1239   // Then calculate the counter address.
1240   addi(scratch, scratch, constant);
1241   increment_mdp_data_at(scratch, Rbumped_count, decrement);
1242 }
1243 
1244 void InterpreterMacroAssembler::increment_mdp_data_at(Register counter_addr,
1245                                                       Register Rbumped_count,
1246                                                       bool decrement) {
1247   assert(ProfileInterpreter, "must be profiling interpreter");
1248 
1249   // Load the counter.
1250   ld(Rbumped_count, 0, counter_addr);
1251 
1252   if (decrement) {
1253     // Decrement the register. Set condition codes.
1254     addi(Rbumped_count, Rbumped_count, - DataLayout::counter_increment);
1255     // Store the decremented counter, if it is still negative.
1256     std(Rbumped_count, 0, counter_addr);
1257     // Note: add/sub overflow check are not ported, since 64 bit
1258     // calculation should never overflow.
1259   } else {
1260     // Increment the register. Set carry flag.
1261     addi(Rbumped_count, Rbumped_count, DataLayout::counter_increment);
1262     // Store the incremented counter.
1263     std(Rbumped_count, 0, counter_addr);
1264   }
1265 }
1266 
1267 // Set a flag value at the current method data pointer position.
1268 void InterpreterMacroAssembler::set_mdp_flag_at(int flag_constant,
1269                                                 Register scratch) {
1270   assert(ProfileInterpreter, "must be profiling interpreter");
1271   // Load the data header.
1272   lbz(scratch, in_bytes(DataLayout::flags_offset()), R28_mdx);
1273   // Set the flag.
1274   ori(scratch, scratch, flag_constant);
1275   // Store the modified header.
1276   stb(scratch, in_bytes(DataLayout::flags_offset()), R28_mdx);
1277 }
1278 
1279 // Test the location at some offset from the method data pointer.
1280 // If it is not equal to value, branch to the not_equal_continue Label.
1281 void InterpreterMacroAssembler::test_mdp_data_at(int offset,
1282                                                  Register value,
1283                                                  Label& not_equal_continue,
1284                                                  Register test_out) {
1285   assert(ProfileInterpreter, "must be profiling interpreter");
1286 
1287   ld(test_out, offset, R28_mdx);
1288   cmpd(CCR0,  value, test_out);
1289   bne(CCR0, not_equal_continue);
1290 }
1291 
1292 // Update the method data pointer by the displacement located at some fixed
1293 // offset from the method data pointer.
1294 void InterpreterMacroAssembler::update_mdp_by_offset(int offset_of_disp,
1295                                                      Register scratch) {
1296   assert(ProfileInterpreter, "must be profiling interpreter");
1297 
1298   ld(scratch, offset_of_disp, R28_mdx);
1299   add(R28_mdx, scratch, R28_mdx);
1300 }
1301 
1302 // Update the method data pointer by the displacement located at the
1303 // offset (reg + offset_of_disp).
1304 void InterpreterMacroAssembler::update_mdp_by_offset(Register reg,
1305                                                      int offset_of_disp,
1306                                                      Register scratch) {
1307   assert(ProfileInterpreter, "must be profiling interpreter");
1308 
1309   add(scratch, reg, R28_mdx);
1310   ld(scratch, offset_of_disp, scratch);
1311   add(R28_mdx, scratch, R28_mdx);
1312 }
1313 
1314 // Update the method data pointer by a simple constant displacement.
1315 void InterpreterMacroAssembler::update_mdp_by_constant(int constant) {
1316   assert(ProfileInterpreter, "must be profiling interpreter");
1317   addi(R28_mdx, R28_mdx, constant);
1318 }
1319 
1320 // Update the method data pointer for a _ret bytecode whose target
1321 // was not among our cached targets.
1322 void InterpreterMacroAssembler::update_mdp_for_ret(TosState state,
1323                                                    Register return_bci) {
1324   assert(ProfileInterpreter, "must be profiling interpreter");
1325 
1326   push(state);
1327   assert(return_bci->is_nonvolatile(), "need to protect return_bci");
1328   call_VM(noreg, CAST_FROM_FN_PTR(address, InterpreterRuntime::update_mdp_for_ret), return_bci);
1329   pop(state);
1330 }
1331 
1332 // Increments the backedge counter.
1333 // Returns backedge counter + invocation counter in Rdst.
1334 void InterpreterMacroAssembler::increment_backedge_counter(const Register Rcounters, const Register Rdst,
1335                                                            const Register Rtmp1, Register Rscratch) {
1336   assert(UseCompiler, "incrementing must be useful");
1337   assert_different_registers(Rdst, Rtmp1);
1338   const Register invocation_counter = Rtmp1;
1339   const Register counter = Rdst;
1340   // TODO: PPC port: assert(4 == InvocationCounter::sz_counter(), "unexpected field size.");
1341 
1342   // Load backedge counter.
1343   lwz(counter, in_bytes(MethodCounters::backedge_counter_offset()) +
1344                in_bytes(InvocationCounter::counter_offset()), Rcounters);
1345   // Load invocation counter.
1346   lwz(invocation_counter, in_bytes(MethodCounters::invocation_counter_offset()) +
1347                           in_bytes(InvocationCounter::counter_offset()), Rcounters);
1348 
1349   // Add the delta to the backedge counter.
1350   addi(counter, counter, InvocationCounter::count_increment);
1351 
1352   // Mask the invocation counter.
1353   andi(invocation_counter, invocation_counter, InvocationCounter::count_mask_value);
1354 
1355   // Store new counter value.
1356   stw(counter, in_bytes(MethodCounters::backedge_counter_offset()) +
1357                in_bytes(InvocationCounter::counter_offset()), Rcounters);
1358   // Return invocation counter + backedge counter.
1359   add(counter, counter, invocation_counter);
1360 }
1361 
1362 // Count a taken branch in the bytecodes.
1363 void InterpreterMacroAssembler::profile_taken_branch(Register scratch, Register bumped_count) {
1364   if (ProfileInterpreter) {
1365     Label profile_continue;
1366 
1367     // If no method data exists, go to profile_continue.
1368     test_method_data_pointer(profile_continue);
1369 
1370     // We are taking a branch. Increment the taken count.
1371     increment_mdp_data_at(in_bytes(JumpData::taken_offset()), scratch, bumped_count);
1372 
1373     // The method data pointer needs to be updated to reflect the new target.
1374     update_mdp_by_offset(in_bytes(JumpData::displacement_offset()), scratch);
1375     bind (profile_continue);
1376   }
1377 }
1378 
1379 // Count a not-taken branch in the bytecodes.
1380 void InterpreterMacroAssembler::profile_not_taken_branch(Register scratch1, Register scratch2) {
1381   if (ProfileInterpreter) {
1382     Label profile_continue;
1383 
1384     // If no method data exists, go to profile_continue.
1385     test_method_data_pointer(profile_continue);
1386 
1387     // We are taking a branch. Increment the not taken count.
1388     increment_mdp_data_at(in_bytes(BranchData::not_taken_offset()), scratch1, scratch2);
1389 
1390     // The method data pointer needs to be updated to correspond to the
1391     // next bytecode.
1392     update_mdp_by_constant(in_bytes(BranchData::branch_data_size()));
1393     bind (profile_continue);
1394   }
1395 }
1396 
1397 // Count a non-virtual call in the bytecodes.
1398 void InterpreterMacroAssembler::profile_call(Register scratch1, Register scratch2) {
1399   if (ProfileInterpreter) {
1400     Label profile_continue;
1401 
1402     // If no method data exists, go to profile_continue.
1403     test_method_data_pointer(profile_continue);
1404 
1405     // We are making a call. Increment the count.
1406     increment_mdp_data_at(in_bytes(CounterData::count_offset()), scratch1, scratch2);
1407 
1408     // The method data pointer needs to be updated to reflect the new target.
1409     update_mdp_by_constant(in_bytes(CounterData::counter_data_size()));
1410     bind (profile_continue);
1411   }
1412 }
1413 
1414 // Count a final call in the bytecodes.
1415 void InterpreterMacroAssembler::profile_final_call(Register scratch1, Register scratch2) {
1416   if (ProfileInterpreter) {
1417     Label profile_continue;
1418 
1419     // If no method data exists, go to profile_continue.
1420     test_method_data_pointer(profile_continue);
1421 
1422     // We are making a call. Increment the count.
1423     increment_mdp_data_at(in_bytes(CounterData::count_offset()), scratch1, scratch2);
1424 
1425     // The method data pointer needs to be updated to reflect the new target.
1426     update_mdp_by_constant(in_bytes(VirtualCallData::virtual_call_data_size()));
1427     bind (profile_continue);
1428   }
1429 }
1430 
1431 // Count a virtual call in the bytecodes.
1432 void InterpreterMacroAssembler::profile_virtual_call(Register Rreceiver,
1433                                                      Register Rscratch1,
1434                                                      Register Rscratch2,
1435                                                      bool receiver_can_be_null) {
1436   if (!ProfileInterpreter) { return; }
1437   Label profile_continue;
1438 
1439   // If no method data exists, go to profile_continue.
1440   test_method_data_pointer(profile_continue);
1441 
1442   Label skip_receiver_profile;
1443   if (receiver_can_be_null) {
1444     Label not_null;
1445     cmpdi(CCR0, Rreceiver, 0);
1446     bne(CCR0, not_null);
1447     // We are making a call. Increment the count for null receiver.
1448     increment_mdp_data_at(in_bytes(CounterData::count_offset()), Rscratch1, Rscratch2);
1449     b(skip_receiver_profile);
1450     bind(not_null);
1451   }
1452 
1453   // Record the receiver type.
1454   record_klass_in_profile(Rreceiver, Rscratch1, Rscratch2, true);
1455   bind(skip_receiver_profile);
1456 
1457   // The method data pointer needs to be updated to reflect the new target.
1458   update_mdp_by_constant(in_bytes(VirtualCallData::virtual_call_data_size()));
1459   bind (profile_continue);
1460 }
1461 
1462 void InterpreterMacroAssembler::profile_typecheck(Register Rklass, Register Rscratch1, Register Rscratch2) {
1463   if (ProfileInterpreter) {
1464     Label profile_continue;
1465 
1466     // If no method data exists, go to profile_continue.
1467     test_method_data_pointer(profile_continue);
1468 
1469     int mdp_delta = in_bytes(BitData::bit_data_size());
1470     if (TypeProfileCasts) {
1471       mdp_delta = in_bytes(VirtualCallData::virtual_call_data_size());
1472 
1473       // Record the object type.
1474       record_klass_in_profile(Rklass, Rscratch1, Rscratch2, false);
1475     }
1476 
1477     // The method data pointer needs to be updated.
1478     update_mdp_by_constant(mdp_delta);
1479 
1480     bind (profile_continue);
1481   }
1482 }
1483 
1484 void InterpreterMacroAssembler::profile_typecheck_failed(Register Rscratch1, Register Rscratch2) {
1485   if (ProfileInterpreter && TypeProfileCasts) {
1486     Label profile_continue;
1487 
1488     // If no method data exists, go to profile_continue.
1489     test_method_data_pointer(profile_continue);
1490 
1491     int count_offset = in_bytes(CounterData::count_offset());
1492     // Back up the address, since we have already bumped the mdp.
1493     count_offset -= in_bytes(VirtualCallData::virtual_call_data_size());
1494 
1495     // *Decrement* the counter. We expect to see zero or small negatives.
1496     increment_mdp_data_at(count_offset, Rscratch1, Rscratch2, true);
1497 
1498     bind (profile_continue);
1499   }
1500 }
1501 
1502 // Count a ret in the bytecodes.
1503 void InterpreterMacroAssembler::profile_ret(TosState state, Register return_bci,
1504                                             Register scratch1, Register scratch2) {
1505   if (ProfileInterpreter) {
1506     Label profile_continue;
1507     uint row;
1508 
1509     // If no method data exists, go to profile_continue.
1510     test_method_data_pointer(profile_continue);
1511 
1512     // Update the total ret count.
1513     increment_mdp_data_at(in_bytes(CounterData::count_offset()), scratch1, scratch2 );
1514 
1515     for (row = 0; row < RetData::row_limit(); row++) {
1516       Label next_test;
1517 
1518       // See if return_bci is equal to bci[n]:
1519       test_mdp_data_at(in_bytes(RetData::bci_offset(row)), return_bci, next_test, scratch1);
1520 
1521       // return_bci is equal to bci[n]. Increment the count.
1522       increment_mdp_data_at(in_bytes(RetData::bci_count_offset(row)), scratch1, scratch2);
1523 
1524       // The method data pointer needs to be updated to reflect the new target.
1525       update_mdp_by_offset(in_bytes(RetData::bci_displacement_offset(row)), scratch1);
1526       b(profile_continue);
1527       bind(next_test);
1528     }
1529 
1530     update_mdp_for_ret(state, return_bci);
1531 
1532     bind (profile_continue);
1533   }
1534 }
1535 
1536 // Count the default case of a switch construct.
1537 void InterpreterMacroAssembler::profile_switch_default(Register scratch1,  Register scratch2) {
1538   if (ProfileInterpreter) {
1539     Label profile_continue;
1540 
1541     // If no method data exists, go to profile_continue.
1542     test_method_data_pointer(profile_continue);
1543 
1544     // Update the default case count
1545     increment_mdp_data_at(in_bytes(MultiBranchData::default_count_offset()),
1546                           scratch1, scratch2);
1547 
1548     // The method data pointer needs to be updated.
1549     update_mdp_by_offset(in_bytes(MultiBranchData::default_displacement_offset()),
1550                          scratch1);
1551 
1552     bind (profile_continue);
1553   }
1554 }
1555 
1556 // Count the index'th case of a switch construct.
1557 void InterpreterMacroAssembler::profile_switch_case(Register index,
1558                                                     Register scratch1,
1559                                                     Register scratch2,
1560                                                     Register scratch3) {
1561   if (ProfileInterpreter) {
1562     assert_different_registers(index, scratch1, scratch2, scratch3);
1563     Label profile_continue;
1564 
1565     // If no method data exists, go to profile_continue.
1566     test_method_data_pointer(profile_continue);
1567 
1568     // Build the base (index * per_case_size_in_bytes()) + case_array_offset_in_bytes().
1569     li(scratch3, in_bytes(MultiBranchData::case_array_offset()));
1570 
1571     assert (in_bytes(MultiBranchData::per_case_size()) == 16, "so that shladd works");
1572     sldi(scratch1, index, exact_log2(in_bytes(MultiBranchData::per_case_size())));
1573     add(scratch1, scratch1, scratch3);
1574 
1575     // Update the case count.
1576     increment_mdp_data_at(scratch1, in_bytes(MultiBranchData::relative_count_offset()), scratch2, scratch3);
1577 
1578     // The method data pointer needs to be updated.
1579     update_mdp_by_offset(scratch1, in_bytes(MultiBranchData::relative_displacement_offset()), scratch2);
1580 
1581     bind (profile_continue);
1582   }
1583 }
1584 
1585 void InterpreterMacroAssembler::profile_null_seen(Register Rscratch1, Register Rscratch2) {
1586   if (ProfileInterpreter) {
1587     assert_different_registers(Rscratch1, Rscratch2);
1588     Label profile_continue;
1589 
1590     // If no method data exists, go to profile_continue.
1591     test_method_data_pointer(profile_continue);
1592 
1593     set_mdp_flag_at(BitData::null_seen_byte_constant(), Rscratch1);
1594 
1595     // The method data pointer needs to be updated.
1596     int mdp_delta = in_bytes(BitData::bit_data_size());
1597     if (TypeProfileCasts) {
1598       mdp_delta = in_bytes(VirtualCallData::virtual_call_data_size());
1599     }
1600     update_mdp_by_constant(mdp_delta);
1601 
1602     bind (profile_continue);
1603   }
1604 }
1605 
1606 void InterpreterMacroAssembler::record_klass_in_profile(Register Rreceiver,
1607                                                         Register Rscratch1, Register Rscratch2,
1608                                                         bool is_virtual_call) {
1609   assert(ProfileInterpreter, "must be profiling");
1610   assert_different_registers(Rreceiver, Rscratch1, Rscratch2);
1611 
1612   Label done;
1613   record_klass_in_profile_helper(Rreceiver, Rscratch1, Rscratch2, 0, done, is_virtual_call);
1614   bind (done);
1615 }
1616 
1617 void InterpreterMacroAssembler::record_klass_in_profile_helper(
1618                                         Register receiver, Register scratch1, Register scratch2,
1619                                         int start_row, Label& done, bool is_virtual_call) {
1620   if (TypeProfileWidth == 0) {
1621     if (is_virtual_call) {
1622       increment_mdp_data_at(in_bytes(CounterData::count_offset()), scratch1, scratch2);
1623     }
1624     return;
1625   }
1626 
1627   int last_row = VirtualCallData::row_limit() - 1;
1628   assert(start_row <= last_row, "must be work left to do");
1629   // Test this row for both the receiver and for null.
1630   // Take any of three different outcomes:
1631   //   1. found receiver => increment count and goto done
1632   //   2. found null => keep looking for case 1, maybe allocate this cell
1633   //   3. found something else => keep looking for cases 1 and 2
1634   // Case 3 is handled by a recursive call.
1635   for (int row = start_row; row <= last_row; row++) {
1636     Label next_test;
1637     bool test_for_null_also = (row == start_row);
1638 
1639     // See if the receiver is receiver[n].
1640     int recvr_offset = in_bytes(VirtualCallData::receiver_offset(row));
1641     test_mdp_data_at(recvr_offset, receiver, next_test, scratch1);
1642     // delayed()->tst(scratch);
1643 
1644     // The receiver is receiver[n]. Increment count[n].
1645     int count_offset = in_bytes(VirtualCallData::receiver_count_offset(row));
1646     increment_mdp_data_at(count_offset, scratch1, scratch2);
1647     b(done);
1648     bind(next_test);
1649 
1650     if (test_for_null_also) {
1651       Label found_null;
1652       // Failed the equality check on receiver[n]... Test for null.
1653       if (start_row == last_row) {
1654         // The only thing left to do is handle the null case.
1655         if (is_virtual_call) {
1656           // Scratch1 contains test_out from test_mdp_data_at.
1657           cmpdi(CCR0, scratch1, 0);
1658           beq(CCR0, found_null);
1659           // Receiver did not match any saved receiver and there is no empty row for it.
1660           // Increment total counter to indicate polymorphic case.
1661           increment_mdp_data_at(in_bytes(CounterData::count_offset()), scratch1, scratch2);
1662           b(done);
1663           bind(found_null);
1664         } else {
1665           cmpdi(CCR0, scratch1, 0);
1666           bne(CCR0, done);
1667         }
1668         break;
1669       }
1670       // Since null is rare, make it be the branch-taken case.
1671       cmpdi(CCR0, scratch1, 0);
1672       beq(CCR0, found_null);
1673 
1674       // Put all the "Case 3" tests here.
1675       record_klass_in_profile_helper(receiver, scratch1, scratch2, start_row + 1, done, is_virtual_call);
1676 
1677       // Found a null. Keep searching for a matching receiver,
1678       // but remember that this is an empty (unused) slot.
1679       bind(found_null);
1680     }
1681   }
1682 
1683   // In the fall-through case, we found no matching receiver, but we
1684   // observed the receiver[start_row] is NULL.
1685 
1686   // Fill in the receiver field and increment the count.
1687   int recvr_offset = in_bytes(VirtualCallData::receiver_offset(start_row));
1688   set_mdp_data_at(recvr_offset, receiver);
1689   int count_offset = in_bytes(VirtualCallData::receiver_count_offset(start_row));
1690   li(scratch1, DataLayout::counter_increment);
1691   set_mdp_data_at(count_offset, scratch1);
1692   if (start_row > 0) {
1693     b(done);
1694   }
1695 }
1696 
1697 // Argument and return type profilig.
1698 // kills: tmp, tmp2, R0, CR0, CR1
1699 void InterpreterMacroAssembler::profile_obj_type(Register obj, Register mdo_addr_base,
1700                                                  RegisterOrConstant mdo_addr_offs,
1701                                                  Register tmp, Register tmp2) {
1702   Label do_nothing, do_update;
1703 
1704   // tmp2 = obj is allowed
1705   assert_different_registers(obj, mdo_addr_base, tmp, R0);
1706   assert_different_registers(tmp2, mdo_addr_base, tmp, R0);
1707   const Register klass = tmp2;
1708 
1709   verify_oop(obj);
1710 
1711   ld(tmp, mdo_addr_offs, mdo_addr_base);
1712 
1713   // Set null_seen if obj is 0.
1714   cmpdi(CCR0, obj, 0);
1715   ori(R0, tmp, TypeEntries::null_seen);
1716   beq(CCR0, do_update);
1717 
1718   load_klass(klass, obj);
1719 
1720   clrrdi(R0, tmp, exact_log2(-TypeEntries::type_klass_mask));
1721   // Basically same as andi(R0, tmp, TypeEntries::type_klass_mask);
1722   cmpd(CCR1, R0, klass);
1723   // Klass seen before, nothing to do (regardless of unknown bit).
1724   //beq(CCR1, do_nothing);
1725 
1726   andi_(R0, klass, TypeEntries::type_unknown);
1727   // Already unknown. Nothing to do anymore.
1728   //bne(CCR0, do_nothing);
1729   crorc(CCR0, Assembler::equal, CCR1, Assembler::equal); // cr0 eq = cr1 eq or cr0 ne
1730   beq(CCR0, do_nothing);
1731 
1732   clrrdi_(R0, tmp, exact_log2(-TypeEntries::type_mask));
1733   orr(R0, klass, tmp); // Combine klass and null_seen bit (only used if (tmp & type_mask)==0).
1734   beq(CCR0, do_update); // First time here. Set profile type.
1735 
1736   // Different than before. Cannot keep accurate profile.
1737   ori(R0, tmp, TypeEntries::type_unknown);
1738 
1739   bind(do_update);
1740   // update profile
1741   std(R0, mdo_addr_offs, mdo_addr_base);
1742 
1743   align(32, 12);
1744   bind(do_nothing);
1745 }
1746 
1747 void InterpreterMacroAssembler::profile_arguments_type(Register callee,
1748                                                        Register tmp1, Register tmp2,
1749                                                        bool is_virtual) {
1750   if (!ProfileInterpreter) {
1751     return;
1752   }
1753 
1754   assert_different_registers(callee, tmp1, tmp2, R28_mdx);
1755 
1756   if (MethodData::profile_arguments() || MethodData::profile_return()) {
1757     Label profile_continue;
1758 
1759     test_method_data_pointer(profile_continue);
1760 
1761     int off_to_start = is_virtual ?
1762       in_bytes(VirtualCallData::virtual_call_data_size()) : in_bytes(CounterData::counter_data_size());
1763 
1764     lbz(tmp1, in_bytes(DataLayout::tag_offset()) - off_to_start, R28_mdx);
1765     cmpwi(CCR0, tmp1, is_virtual ? DataLayout::virtual_call_type_data_tag : DataLayout::call_type_data_tag);
1766     bne(CCR0, profile_continue);
1767 
1768     if (MethodData::profile_arguments()) {
1769       Label done;
1770       int off_to_args = in_bytes(TypeEntriesAtCall::args_data_offset());
1771       add(R28_mdx, off_to_args, R28_mdx);
1772 
1773       for (int i = 0; i < TypeProfileArgsLimit; i++) {
1774         if (i > 0 || MethodData::profile_return()) {
1775           // If return value type is profiled we may have no argument to profile.
1776           ld(tmp1, in_bytes(TypeEntriesAtCall::cell_count_offset())-off_to_args, R28_mdx);
1777           cmpdi(CCR0, tmp1, (i+1)*TypeStackSlotEntries::per_arg_count());
1778           addi(tmp1, tmp1, -i*TypeStackSlotEntries::per_arg_count());
1779           blt(CCR0, done);
1780         }
1781         ld(tmp1, in_bytes(Method::const_offset()), callee);
1782         lhz(tmp1, in_bytes(ConstMethod::size_of_parameters_offset()), tmp1);
1783         // Stack offset o (zero based) from the start of the argument
1784         // list, for n arguments translates into offset n - o - 1 from
1785         // the end of the argument list. But there's an extra slot at
1786         // the top of the stack. So the offset is n - o from Lesp.
1787         ld(tmp2, in_bytes(TypeEntriesAtCall::stack_slot_offset(i))-off_to_args, R28_mdx);
1788         subf(tmp1, tmp2, tmp1);
1789 
1790         sldi(tmp1, tmp1, Interpreter::logStackElementSize);
1791         ldx(tmp1, tmp1, R15_esp);
1792 
1793         profile_obj_type(tmp1, R28_mdx, in_bytes(TypeEntriesAtCall::argument_type_offset(i))-off_to_args, tmp2, tmp1);
1794 
1795         int to_add = in_bytes(TypeStackSlotEntries::per_arg_size());
1796         addi(R28_mdx, R28_mdx, to_add);
1797         off_to_args += to_add;
1798       }
1799 
1800       if (MethodData::profile_return()) {
1801         ld(tmp1, in_bytes(TypeEntriesAtCall::cell_count_offset())-off_to_args, R28_mdx);
1802         addi(tmp1, tmp1, -TypeProfileArgsLimit*TypeStackSlotEntries::per_arg_count());
1803       }
1804 
1805       bind(done);
1806 
1807       if (MethodData::profile_return()) {
1808         // We're right after the type profile for the last
1809         // argument. tmp1 is the number of cells left in the
1810         // CallTypeData/VirtualCallTypeData to reach its end. Non null
1811         // if there's a return to profile.
1812         assert(SingleTypeEntry::static_cell_count() < TypeStackSlotEntries::per_arg_count(),
1813                "can't move past ret type");
1814         sldi(tmp1, tmp1, exact_log2(DataLayout::cell_size));
1815         add(R28_mdx, tmp1, R28_mdx);
1816       }
1817     } else {
1818       assert(MethodData::profile_return(), "either profile call args or call ret");
1819       update_mdp_by_constant(in_bytes(TypeEntriesAtCall::return_only_size()));
1820     }
1821 
1822     // Mdp points right after the end of the
1823     // CallTypeData/VirtualCallTypeData, right after the cells for the
1824     // return value type if there's one.
1825     align(32, 12);
1826     bind(profile_continue);
1827   }
1828 }
1829 
1830 void InterpreterMacroAssembler::profile_return_type(Register ret, Register tmp1, Register tmp2) {
1831   assert_different_registers(ret, tmp1, tmp2);
1832   if (ProfileInterpreter && MethodData::profile_return()) {
1833     Label profile_continue;
1834 
1835     test_method_data_pointer(profile_continue);
1836 
1837     if (MethodData::profile_return_jsr292_only()) {
1838       // If we don't profile all invoke bytecodes we must make sure
1839       // it's a bytecode we indeed profile. We can't go back to the
1840       // begining of the ProfileData we intend to update to check its
1841       // type because we're right after it and we don't known its
1842       // length.
1843       lbz(tmp1, 0, R14_bcp);
1844       lbz(tmp2, Method::intrinsic_id_offset_in_bytes(), R19_method);
1845       cmpwi(CCR0, tmp1, Bytecodes::_invokedynamic);
1846       cmpwi(CCR1, tmp1, Bytecodes::_invokehandle);
1847       cror(CCR0, Assembler::equal, CCR1, Assembler::equal);
1848       cmpwi(CCR1, tmp2, static_cast<int>(vmIntrinsics::_compiledLambdaForm));
1849       cror(CCR0, Assembler::equal, CCR1, Assembler::equal);
1850       bne(CCR0, profile_continue);
1851     }
1852 
1853     profile_obj_type(ret, R28_mdx, -in_bytes(SingleTypeEntry::size()), tmp1, tmp2);
1854 
1855     align(32, 12);
1856     bind(profile_continue);
1857   }
1858 }
1859 
1860 void InterpreterMacroAssembler::profile_parameters_type(Register tmp1, Register tmp2,
1861                                                         Register tmp3, Register tmp4) {
1862   if (ProfileInterpreter && MethodData::profile_parameters()) {
1863     Label profile_continue, done;
1864 
1865     test_method_data_pointer(profile_continue);
1866 
1867     // Load the offset of the area within the MDO used for
1868     // parameters. If it's negative we're not profiling any parameters.
1869     lwz(tmp1, in_bytes(MethodData::parameters_type_data_di_offset()) - in_bytes(MethodData::data_offset()), R28_mdx);
1870     cmpwi(CCR0, tmp1, 0);
1871     blt(CCR0, profile_continue);
1872 
1873     // Compute a pointer to the area for parameters from the offset
1874     // and move the pointer to the slot for the last
1875     // parameters. Collect profiling from last parameter down.
1876     // mdo start + parameters offset + array length - 1
1877 
1878     // Pointer to the parameter area in the MDO.
1879     const Register mdp = tmp1;
1880     add(mdp, tmp1, R28_mdx);
1881 
1882     // Offset of the current profile entry to update.
1883     const Register entry_offset = tmp2;
1884     // entry_offset = array len in number of cells
1885     ld(entry_offset, in_bytes(ArrayData::array_len_offset()), mdp);
1886 
1887     int off_base = in_bytes(ParametersTypeData::stack_slot_offset(0));
1888     assert(off_base % DataLayout::cell_size == 0, "should be a number of cells");
1889 
1890     // entry_offset (number of cells)  = array len - size of 1 entry + offset of the stack slot field
1891     addi(entry_offset, entry_offset, -TypeStackSlotEntries::per_arg_count() + (off_base / DataLayout::cell_size));
1892     // entry_offset in bytes
1893     sldi(entry_offset, entry_offset, exact_log2(DataLayout::cell_size));
1894 
1895     Label loop;
1896     align(32, 12);
1897     bind(loop);
1898 
1899     // Load offset on the stack from the slot for this parameter.
1900     ld(tmp3, entry_offset, mdp);
1901     sldi(tmp3, tmp3, Interpreter::logStackElementSize);
1902     neg(tmp3, tmp3);
1903     // Read the parameter from the local area.
1904     ldx(tmp3, tmp3, R18_locals);
1905 
1906     // Make entry_offset now point to the type field for this parameter.
1907     int type_base = in_bytes(ParametersTypeData::type_offset(0));
1908     assert(type_base > off_base, "unexpected");
1909     addi(entry_offset, entry_offset, type_base - off_base);
1910 
1911     // Profile the parameter.
1912     profile_obj_type(tmp3, mdp, entry_offset, tmp4, tmp3);
1913 
1914     // Go to next parameter.
1915     int delta = TypeStackSlotEntries::per_arg_count() * DataLayout::cell_size + (type_base - off_base);
1916     cmpdi(CCR0, entry_offset, off_base + delta);
1917     addi(entry_offset, entry_offset, -delta);
1918     bge(CCR0, loop);
1919 
1920     align(32, 12);
1921     bind(profile_continue);
1922   }
1923 }
1924 
1925 // Add a InterpMonitorElem to stack (see frame_sparc.hpp).
1926 void InterpreterMacroAssembler::add_monitor_to_stack(bool stack_is_empty, Register Rtemp1, Register Rtemp2) {
1927 
1928   // Very-local scratch registers.
1929   const Register esp  = Rtemp1;
1930   const Register slot = Rtemp2;
1931 
1932   // Extracted monitor_size.
1933   int monitor_size = frame::interpreter_frame_monitor_size_in_bytes();
1934   assert(Assembler::is_aligned((unsigned int)monitor_size,
1935                                (unsigned int)frame::alignment_in_bytes),
1936          "size of a monitor must respect alignment of SP");
1937 
1938   resize_frame(-monitor_size, /*temp*/esp); // Allocate space for new monitor
1939   std(R1_SP, _ijava_state_neg(top_frame_sp), esp); // esp contains fp
1940 
1941   // Shuffle expression stack down. Recall that stack_base points
1942   // just above the new expression stack bottom. Old_tos and new_tos
1943   // are used to scan thru the old and new expression stacks.
1944   if (!stack_is_empty) {
1945     Label copy_slot, copy_slot_finished;
1946     const Register n_slots = slot;
1947 
1948     addi(esp, R15_esp, Interpreter::stackElementSize); // Point to first element (pre-pushed stack).
1949     subf(n_slots, esp, R26_monitor);
1950     srdi_(n_slots, n_slots, LogBytesPerWord);          // Compute number of slots to copy.
1951     assert(LogBytesPerWord == 3, "conflicts assembler instructions");
1952     beq(CCR0, copy_slot_finished);                     // Nothing to copy.
1953 
1954     mtctr(n_slots);
1955 
1956     // loop
1957     bind(copy_slot);
1958     ld(slot, 0, esp);              // Move expression stack down.
1959     std(slot, -monitor_size, esp); // distance = monitor_size
1960     addi(esp, esp, BytesPerWord);
1961     bdnz(copy_slot);
1962 
1963     bind(copy_slot_finished);
1964   }
1965 
1966   addi(R15_esp, R15_esp, -monitor_size);
1967   addi(R26_monitor, R26_monitor, -monitor_size);
1968 
1969   // Restart interpreter
1970 }
1971 
1972 // ============================================================================
1973 // Java locals access
1974 
1975 // Load a local variable at index in Rindex into register Rdst_value.
1976 // Also puts address of local into Rdst_address as a service.
1977 // Kills:
1978 //   - Rdst_value
1979 //   - Rdst_address
1980 void InterpreterMacroAssembler::load_local_int(Register Rdst_value, Register Rdst_address, Register Rindex) {
1981   sldi(Rdst_address, Rindex, Interpreter::logStackElementSize);
1982   subf(Rdst_address, Rdst_address, R18_locals);
1983   lwz(Rdst_value, 0, Rdst_address);
1984 }
1985 
1986 // Load a local variable at index in Rindex into register Rdst_value.
1987 // Also puts address of local into Rdst_address as a service.
1988 // Kills:
1989 //   - Rdst_value
1990 //   - Rdst_address
1991 void InterpreterMacroAssembler::load_local_long(Register Rdst_value, Register Rdst_address, Register Rindex) {
1992   sldi(Rdst_address, Rindex, Interpreter::logStackElementSize);
1993   subf(Rdst_address, Rdst_address, R18_locals);
1994   ld(Rdst_value, -8, Rdst_address);
1995 }
1996 
1997 // Load a local variable at index in Rindex into register Rdst_value.
1998 // Also puts address of local into Rdst_address as a service.
1999 // Input:
2000 //   - Rindex:      slot nr of local variable
2001 // Kills:
2002 //   - Rdst_value
2003 //   - Rdst_address
2004 void InterpreterMacroAssembler::load_local_ptr(Register Rdst_value,
2005                                                Register Rdst_address,
2006                                                Register Rindex) {
2007   sldi(Rdst_address, Rindex, Interpreter::logStackElementSize);
2008   subf(Rdst_address, Rdst_address, R18_locals);
2009   ld(Rdst_value, 0, Rdst_address);
2010 }
2011 
2012 // Load a local variable at index in Rindex into register Rdst_value.
2013 // Also puts address of local into Rdst_address as a service.
2014 // Kills:
2015 //   - Rdst_value
2016 //   - Rdst_address
2017 void InterpreterMacroAssembler::load_local_float(FloatRegister Rdst_value,
2018                                                  Register Rdst_address,
2019                                                  Register Rindex) {
2020   sldi(Rdst_address, Rindex, Interpreter::logStackElementSize);
2021   subf(Rdst_address, Rdst_address, R18_locals);
2022   lfs(Rdst_value, 0, Rdst_address);
2023 }
2024 
2025 // Load a local variable at index in Rindex into register Rdst_value.
2026 // Also puts address of local into Rdst_address as a service.
2027 // Kills:
2028 //   - Rdst_value
2029 //   - Rdst_address
2030 void InterpreterMacroAssembler::load_local_double(FloatRegister Rdst_value,
2031                                                   Register Rdst_address,
2032                                                   Register Rindex) {
2033   sldi(Rdst_address, Rindex, Interpreter::logStackElementSize);
2034   subf(Rdst_address, Rdst_address, R18_locals);
2035   lfd(Rdst_value, -8, Rdst_address);
2036 }
2037 
2038 // Store an int value at local variable slot Rindex.
2039 // Kills:
2040 //   - Rindex
2041 void InterpreterMacroAssembler::store_local_int(Register Rvalue, Register Rindex) {
2042   sldi(Rindex, Rindex, Interpreter::logStackElementSize);
2043   subf(Rindex, Rindex, R18_locals);
2044   stw(Rvalue, 0, Rindex);
2045 }
2046 
2047 // Store a long value at local variable slot Rindex.
2048 // Kills:
2049 //   - Rindex
2050 void InterpreterMacroAssembler::store_local_long(Register Rvalue, Register Rindex) {
2051   sldi(Rindex, Rindex, Interpreter::logStackElementSize);
2052   subf(Rindex, Rindex, R18_locals);
2053   std(Rvalue, -8, Rindex);
2054 }
2055 
2056 // Store an oop value at local variable slot Rindex.
2057 // Kills:
2058 //   - Rindex
2059 void InterpreterMacroAssembler::store_local_ptr(Register Rvalue, Register Rindex) {
2060   sldi(Rindex, Rindex, Interpreter::logStackElementSize);
2061   subf(Rindex, Rindex, R18_locals);
2062   std(Rvalue, 0, Rindex);
2063 }
2064 
2065 // Store an int value at local variable slot Rindex.
2066 // Kills:
2067 //   - Rindex
2068 void InterpreterMacroAssembler::store_local_float(FloatRegister Rvalue, Register Rindex) {
2069   sldi(Rindex, Rindex, Interpreter::logStackElementSize);
2070   subf(Rindex, Rindex, R18_locals);
2071   stfs(Rvalue, 0, Rindex);
2072 }
2073 
2074 // Store an int value at local variable slot Rindex.
2075 // Kills:
2076 //   - Rindex
2077 void InterpreterMacroAssembler::store_local_double(FloatRegister Rvalue, Register Rindex) {
2078   sldi(Rindex, Rindex, Interpreter::logStackElementSize);
2079   subf(Rindex, Rindex, R18_locals);
2080   stfd(Rvalue, -8, Rindex);
2081 }
2082 
2083 // Read pending exception from thread and jump to interpreter.
2084 // Throw exception entry if one if pending. Fall through otherwise.
2085 void InterpreterMacroAssembler::check_and_forward_exception(Register Rscratch1, Register Rscratch2) {
2086   assert_different_registers(Rscratch1, Rscratch2, R3);
2087   Register Rexception = Rscratch1;
2088   Register Rtmp       = Rscratch2;
2089   Label Ldone;
2090   // Get pending exception oop.
2091   ld(Rexception, thread_(pending_exception));
2092   cmpdi(CCR0, Rexception, 0);
2093   beq(CCR0, Ldone);
2094   li(Rtmp, 0);
2095   mr_if_needed(R3, Rexception);
2096   std(Rtmp, thread_(pending_exception)); // Clear exception in thread
2097   if (Interpreter::rethrow_exception_entry() != NULL) {
2098     // Already got entry address.
2099     load_dispatch_table(Rtmp, (address*)Interpreter::rethrow_exception_entry());
2100   } else {
2101     // Dynamically load entry address.
2102     int simm16_rest = load_const_optimized(Rtmp, &Interpreter::_rethrow_exception_entry, R0, true);
2103     ld(Rtmp, simm16_rest, Rtmp);
2104   }
2105   mtctr(Rtmp);
2106   save_interpreter_state(Rtmp);
2107   bctr();
2108 
2109   align(32, 12);
2110   bind(Ldone);
2111 }
2112 
2113 void InterpreterMacroAssembler::call_VM(Register oop_result, address entry_point, bool check_exceptions) {
2114   save_interpreter_state(R11_scratch1);
2115 
2116   MacroAssembler::call_VM(oop_result, entry_point, false);
2117 
2118   restore_interpreter_state(R11_scratch1, /*bcp_and_mdx_only*/ true);
2119 
2120   check_and_handle_popframe(R11_scratch1);
2121   check_and_handle_earlyret(R11_scratch1);
2122   // Now check exceptions manually.
2123   if (check_exceptions) {
2124     check_and_forward_exception(R11_scratch1, R12_scratch2);
2125   }
2126 }
2127 
2128 void InterpreterMacroAssembler::call_VM(Register oop_result, address entry_point,
2129                                         Register arg_1, bool check_exceptions) {
2130   // ARG1 is reserved for the thread.
2131   mr_if_needed(R4_ARG2, arg_1);
2132   call_VM(oop_result, entry_point, check_exceptions);
2133 }
2134 
2135 void InterpreterMacroAssembler::call_VM(Register oop_result, address entry_point,
2136                                         Register arg_1, Register arg_2,
2137                                         bool check_exceptions) {
2138   // ARG1 is reserved for the thread.
2139   mr_if_needed(R4_ARG2, arg_1);
2140   assert(arg_2 != R4_ARG2, "smashed argument");
2141   mr_if_needed(R5_ARG3, arg_2);
2142   call_VM(oop_result, entry_point, check_exceptions);
2143 }
2144 
2145 void InterpreterMacroAssembler::call_VM(Register oop_result, address entry_point,
2146                                         Register arg_1, Register arg_2, Register arg_3,
2147                                         bool check_exceptions) {
2148   // ARG1 is reserved for the thread.
2149   mr_if_needed(R4_ARG2, arg_1);
2150   assert(arg_2 != R4_ARG2, "smashed argument");
2151   mr_if_needed(R5_ARG3, arg_2);
2152   assert(arg_3 != R4_ARG2 && arg_3 != R5_ARG3, "smashed argument");
2153   mr_if_needed(R6_ARG4, arg_3);
2154   call_VM(oop_result, entry_point, check_exceptions);
2155 }
2156 
2157 void InterpreterMacroAssembler::save_interpreter_state(Register scratch) {
2158   ld(scratch, 0, R1_SP);
2159   std(R15_esp, _ijava_state_neg(esp), scratch);
2160   std(R14_bcp, _ijava_state_neg(bcp), scratch);
2161   std(R26_monitor, _ijava_state_neg(monitors), scratch);
2162   if (ProfileInterpreter) { std(R28_mdx, _ijava_state_neg(mdx), scratch); }
2163   // Other entries should be unchanged.
2164 }
2165 
2166 void InterpreterMacroAssembler::restore_interpreter_state(Register scratch, bool bcp_and_mdx_only) {
2167   ld(scratch, 0, R1_SP);
2168   ld(R14_bcp, _ijava_state_neg(bcp), scratch); // Changed by VM code (exception).
2169   if (ProfileInterpreter) { ld(R28_mdx, _ijava_state_neg(mdx), scratch); } // Changed by VM code.
2170   if (!bcp_and_mdx_only) {
2171     // Following ones are Metadata.
2172     ld(R19_method, _ijava_state_neg(method), scratch);
2173     ld(R27_constPoolCache, _ijava_state_neg(cpoolCache), scratch);
2174     // Following ones are stack addresses and don't require reload.
2175     ld(R15_esp, _ijava_state_neg(esp), scratch);
2176     ld(R18_locals, _ijava_state_neg(locals), scratch);
2177     ld(R26_monitor, _ijava_state_neg(monitors), scratch);
2178   }
2179 #ifdef ASSERT
2180   {
2181     Label Lok;
2182     subf(R0, R1_SP, scratch);
2183     cmpdi(CCR0, R0, frame::abi_reg_args_size + frame::ijava_state_size);
2184     bge(CCR0, Lok);
2185     stop("frame too small (restore istate)");
2186     bind(Lok);
2187   }
2188 #endif
2189 }
2190 
2191 void InterpreterMacroAssembler::get_method_counters(Register method,
2192                                                     Register Rcounters,
2193                                                     Label& skip) {
2194   BLOCK_COMMENT("Load and ev. allocate counter object {");
2195   Label has_counters;
2196   ld(Rcounters, in_bytes(Method::method_counters_offset()), method);
2197   cmpdi(CCR0, Rcounters, 0);
2198   bne(CCR0, has_counters);
2199   call_VM(noreg, CAST_FROM_FN_PTR(address,
2200                                   InterpreterRuntime::build_method_counters), method);
2201   ld(Rcounters, in_bytes(Method::method_counters_offset()), method);
2202   cmpdi(CCR0, Rcounters, 0);
2203   beq(CCR0, skip); // No MethodCounters, OutOfMemory.
2204   BLOCK_COMMENT("} Load and ev. allocate counter object");
2205 
2206   bind(has_counters);
2207 }
2208 
2209 void InterpreterMacroAssembler::increment_invocation_counter(Register Rcounters,
2210                                                              Register iv_be_count,
2211                                                              Register Rtmp_r0) {
2212   assert(UseCompiler || LogTouchedMethods, "incrementing must be useful");
2213   Register invocation_count = iv_be_count;
2214   Register backedge_count   = Rtmp_r0;
2215   int delta = InvocationCounter::count_increment;
2216 
2217   // Load each counter in a register.
2218   //  ld(inv_counter, Rtmp);
2219   //  ld(be_counter, Rtmp2);
2220   int inv_counter_offset = in_bytes(MethodCounters::invocation_counter_offset() +
2221                                     InvocationCounter::counter_offset());
2222   int be_counter_offset  = in_bytes(MethodCounters::backedge_counter_offset() +
2223                                     InvocationCounter::counter_offset());
2224 
2225   BLOCK_COMMENT("Increment profiling counters {");
2226 
2227   // Load the backedge counter.
2228   lwz(backedge_count, be_counter_offset, Rcounters); // is unsigned int
2229   // Mask the backedge counter.
2230   andi(backedge_count, backedge_count, InvocationCounter::count_mask_value);
2231 
2232   // Load the invocation counter.
2233   lwz(invocation_count, inv_counter_offset, Rcounters); // is unsigned int
2234   // Add the delta to the invocation counter and store the result.
2235   addi(invocation_count, invocation_count, delta);
2236   // Store value.
2237   stw(invocation_count, inv_counter_offset, Rcounters);
2238 
2239   // Add invocation counter + backedge counter.
2240   add(iv_be_count, backedge_count, invocation_count);
2241 
2242   // Note that this macro must leave the backedge_count + invocation_count in
2243   // register iv_be_count!
2244   BLOCK_COMMENT("} Increment profiling counters");
2245 }
2246 
2247 void InterpreterMacroAssembler::verify_oop(Register reg, TosState state) {
2248   if (state == atos) { MacroAssembler::verify_oop(reg, FILE_AND_LINE); }
2249 }
2250 
2251 // Local helper function for the verify_oop_or_return_address macro.
2252 static bool verify_return_address(Method* m, int bci) {
2253 #ifndef PRODUCT
2254   address pc = (address)(m->constMethod()) + in_bytes(ConstMethod::codes_offset()) + bci;
2255   // Assume it is a valid return address if it is inside m and is preceded by a jsr.
2256   if (!m->contains(pc))                                            return false;
2257   address jsr_pc;
2258   jsr_pc = pc - Bytecodes::length_for(Bytecodes::_jsr);
2259   if (*jsr_pc == Bytecodes::_jsr   && jsr_pc >= m->code_base())    return true;
2260   jsr_pc = pc - Bytecodes::length_for(Bytecodes::_jsr_w);
2261   if (*jsr_pc == Bytecodes::_jsr_w && jsr_pc >= m->code_base())    return true;
2262 #endif // PRODUCT
2263   return false;
2264 }
2265 
2266 void InterpreterMacroAssembler::verify_FPU(int stack_depth, TosState state) {
2267   if (VerifyFPU) {
2268     unimplemented("verfiyFPU");
2269   }
2270 }
2271 
2272 void InterpreterMacroAssembler::verify_oop_or_return_address(Register reg, Register Rtmp) {
2273   if (!VerifyOops) return;
2274 
2275   // The VM documentation for the astore[_wide] bytecode allows
2276   // the TOS to be not only an oop but also a return address.
2277   Label test;
2278   Label skip;
2279   // See if it is an address (in the current method):
2280 
2281   const int log2_bytecode_size_limit = 16;
2282   srdi_(Rtmp, reg, log2_bytecode_size_limit);
2283   bne(CCR0, test);
2284 
2285   address fd = CAST_FROM_FN_PTR(address, verify_return_address);
2286   const int nbytes_save = MacroAssembler::num_volatile_regs * 8;
2287   save_volatile_gprs(R1_SP, -nbytes_save); // except R0
2288   save_LR_CR(Rtmp); // Save in old frame.
2289   push_frame_reg_args(nbytes_save, Rtmp);
2290 
2291   load_const_optimized(Rtmp, fd, R0);
2292   mr_if_needed(R4_ARG2, reg);
2293   mr(R3_ARG1, R19_method);
2294   call_c(Rtmp); // call C
2295 
2296   pop_frame();
2297   restore_LR_CR(Rtmp);
2298   restore_volatile_gprs(R1_SP, -nbytes_save); // except R0
2299   b(skip);
2300 
2301   // Perform a more elaborate out-of-line call.
2302   // Not an address; verify it:
2303   bind(test);
2304   verify_oop(reg);
2305   bind(skip);
2306 }
2307 
2308 // Inline assembly for:
2309 //
2310 // if (thread is in interp_only_mode) {
2311 //   InterpreterRuntime::post_method_entry();
2312 // }
2313 // if (*jvmpi::event_flags_array_at_addr(JVMPI_EVENT_METHOD_ENTRY ) ||
2314 //     *jvmpi::event_flags_array_at_addr(JVMPI_EVENT_METHOD_ENTRY2)   ) {
2315 //   SharedRuntime::jvmpi_method_entry(method, receiver);
2316 // }
2317 void InterpreterMacroAssembler::notify_method_entry() {
2318   // JVMTI
2319   // Whenever JVMTI puts a thread in interp_only_mode, method
2320   // entry/exit events are sent for that thread to track stack
2321   // depth. If it is possible to enter interp_only_mode we add
2322   // the code to check if the event should be sent.
2323   if (JvmtiExport::can_post_interpreter_events()) {
2324     Label jvmti_post_done;
2325 
2326     lwz(R0, in_bytes(JavaThread::interp_only_mode_offset()), R16_thread);
2327     cmpwi(CCR0, R0, 0);
2328     beq(CCR0, jvmti_post_done);
2329     call_VM(noreg, CAST_FROM_FN_PTR(address, InterpreterRuntime::post_method_entry));
2330 
2331     bind(jvmti_post_done);
2332   }
2333 }
2334 
2335 // Inline assembly for:
2336 //
2337 // if (thread is in interp_only_mode) {
2338 //   // save result
2339 //   InterpreterRuntime::post_method_exit();
2340 //   // restore result
2341 // }
2342 // if (*jvmpi::event_flags_array_at_addr(JVMPI_EVENT_METHOD_EXIT)) {
2343 //   // save result
2344 //   SharedRuntime::jvmpi_method_exit();
2345 //   // restore result
2346 // }
2347 //
2348 // Native methods have their result stored in d_tmp and l_tmp.
2349 // Java methods have their result stored in the expression stack.
2350 void InterpreterMacroAssembler::notify_method_exit(bool is_native_method, TosState state,
2351                                                    NotifyMethodExitMode mode, bool check_exceptions) {
2352   // JVMTI
2353   // Whenever JVMTI puts a thread in interp_only_mode, method
2354   // entry/exit events are sent for that thread to track stack
2355   // depth. If it is possible to enter interp_only_mode we add
2356   // the code to check if the event should be sent.
2357   if (mode == NotifyJVMTI && JvmtiExport::can_post_interpreter_events()) {
2358     Label jvmti_post_done;
2359 
2360     lwz(R0, in_bytes(JavaThread::interp_only_mode_offset()), R16_thread);
2361     cmpwi(CCR0, R0, 0);
2362     beq(CCR0, jvmti_post_done);
2363     if (!is_native_method) { push(state); } // Expose tos to GC.
2364     call_VM(noreg, CAST_FROM_FN_PTR(address, InterpreterRuntime::post_method_exit), check_exceptions);
2365     if (!is_native_method) { pop(state); }
2366 
2367     align(32, 12);
2368     bind(jvmti_post_done);
2369   }
2370 
2371   // Dtrace support not implemented.
2372 }