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