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