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