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