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