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