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