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