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