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