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