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     if (DiagnoseSyncOnValueBasedClasses != 0) {
 960       load_klass(tmp, object);
 961       lwz(tmp, in_bytes(Klass::access_flags_offset()), tmp);
 962       testbitdi(CCR0, R0, tmp, exact_log2(JVM_ACC_IS_VALUE_BASED_CLASS));
 963       bne(CCR0, slow_case);
 964     }
 965 
 966     if (LockingMode == LM_LIGHTWEIGHT) {
 967       lightweight_lock(object, header, tmp, slow_case);
 968       b(count_locking);
 969     } else if (LockingMode == LM_LEGACY) {
 970       // Load markWord from object into header.
 971       ld(header, oopDesc::mark_offset_in_bytes(), object);
 972 
 973       // Set displaced_header to be (markWord of object | UNLOCK_VALUE).
 974       ori(header, header, markWord::unlocked_value);
 975 
 976       // monitor->lock()->set_displaced_header(displaced_header);
 977       const int lock_offset = in_bytes(BasicObjectLock::lock_offset());
 978       const int mark_offset = lock_offset +
 979                               BasicLock::displaced_header_offset_in_bytes();
 980 
 981       // Initialize the box (Must happen before we update the object mark!).
 982       std(header, mark_offset, monitor);
 983 
 984       // if (Atomic::cmpxchg(/*addr*/obj->mark_addr(), /*cmp*/displaced_header, /*ex=*/monitor) == displaced_header) {
 985 
 986       // Store stack address of the BasicObjectLock (this is monitor) into object.
 987       addi(object_mark_addr, object, oopDesc::mark_offset_in_bytes());
 988 
 989       // Must fence, otherwise, preceding store(s) may float below cmpxchg.
 990       // CmpxchgX sets CCR0 to cmpX(current, displaced).
 991       cmpxchgd(/*flag=*/CCR0,
 992                /*current_value=*/current_header,
 993                /*compare_value=*/header, /*exchange_value=*/monitor,
 994                /*where=*/object_mark_addr,
 995                MacroAssembler::MemBarRel | MacroAssembler::MemBarAcq,
 996                MacroAssembler::cmpxchgx_hint_acquire_lock(),
 997                noreg,
 998                &cas_failed,
 999                /*check without membar and ldarx first*/true);
1000 
1001       // If the compare-and-exchange succeeded, then we found an unlocked
1002       // object and we have now locked it.
1003       b(count_locking);
1004       bind(cas_failed);
1005 
1006       // } else if (THREAD->is_lock_owned((address)displaced_header))
1007       //   // Simple recursive case.
1008       //   monitor->lock()->set_displaced_header(nullptr);
1009 
1010       // We did not see an unlocked object so try the fast recursive case.
1011 
1012       // Check if owner is self by comparing the value in the markWord of object
1013       // (current_header) with the stack pointer.
1014       sub(current_header, current_header, R1_SP);
1015 
1016       assert(os::vm_page_size() > 0xfff, "page size too small - change the constant");
1017       load_const_optimized(tmp, ~(os::vm_page_size()-1) | markWord::lock_mask_in_place);
1018 
1019       and_(R0/*==0?*/, current_header, tmp);
1020       // If condition is true we are done and hence we can store 0 in the displaced
1021       // header indicating it is a recursive lock.
1022       bne(CCR0, slow_case);
1023       std(R0/*==0!*/, mark_offset, monitor);
1024       b(count_locking);
1025     }
1026 
1027     // } else {
1028     //   // Slow path.
1029     //   InterpreterRuntime::monitorenter(THREAD, monitor);
1030 
1031     // None of the above fast optimizations worked so we have to get into the
1032     // slow case of monitor enter.
1033     bind(slow_case);
1034     if (LockingMode == LM_LIGHTWEIGHT) {
1035       call_VM(noreg, CAST_FROM_FN_PTR(address, InterpreterRuntime::monitorenter_obj), object);
1036     } else {
1037       call_VM(noreg, CAST_FROM_FN_PTR(address, InterpreterRuntime::monitorenter), monitor);
1038     }
1039     b(done);
1040     // }
1041     align(32, 12);
1042     bind(count_locking);
1043     inc_held_monitor_count(current_header /*tmp*/);
1044     bind(done);
1045   }
1046 }
1047 
1048 // Unlocks an object. Used in monitorexit bytecode and remove_activation.
1049 //
1050 // Registers alive
1051 //   monitor - Address of the BasicObjectLock to be used for locking,
1052 //             which must be initialized with the object to lock.
1053 //
1054 // Throw IllegalMonitorException if object is not locked by current thread.
1055 void InterpreterMacroAssembler::unlock_object(Register monitor) {
1056   if (LockingMode == LM_MONITOR) {
1057     call_VM_leaf(CAST_FROM_FN_PTR(address, InterpreterRuntime::monitorexit), monitor);
1058   } else {
1059 
1060     // template code (for LM_LEGACY):
1061     //
1062     // if ((displaced_header = monitor->displaced_header()) == nullptr) {
1063     //   // Recursive unlock. Mark the monitor unlocked by setting the object field to null.
1064     //   monitor->set_obj(nullptr);
1065     // } else if (Atomic::cmpxchg(obj->mark_addr(), monitor, displaced_header) == monitor) {
1066     //   // We swapped the unlocked mark in displaced_header into the object's mark word.
1067     //   monitor->set_obj(nullptr);
1068     // } else {
1069     //   // Slow path.
1070     //   InterpreterRuntime::monitorexit(monitor);
1071     // }
1072 
1073     const Register object           = R7_ARG5;
1074     const Register header           = R8_ARG6;
1075     const Register object_mark_addr = R9_ARG7;
1076     const Register current_header   = R10_ARG8;
1077 
1078     Label free_slot;
1079     Label slow_case;
1080 
1081     assert_different_registers(object, header, object_mark_addr, current_header);
1082 
1083     if (LockingMode != LM_LIGHTWEIGHT) {
1084       // Test first if we are in the fast recursive case.
1085       ld(header, in_bytes(BasicObjectLock::lock_offset()) +
1086                  BasicLock::displaced_header_offset_in_bytes(), monitor);
1087 
1088       // If the displaced header is zero, we have a recursive unlock.
1089       cmpdi(CCR0, header, 0);
1090       beq(CCR0, free_slot); // recursive unlock
1091     }
1092 
1093     // } else if (Atomic::cmpxchg(obj->mark_addr(), monitor, displaced_header) == monitor) {
1094     //   // We swapped the unlocked mark in displaced_header into the object's mark word.
1095     //   monitor->set_obj(nullptr);
1096 
1097     // If we still have a lightweight lock, unlock the object and be done.
1098 
1099     // The object address from the monitor is in object.
1100     ld(object, in_bytes(BasicObjectLock::obj_offset()), monitor);
1101 
1102     if (LockingMode == LM_LIGHTWEIGHT) {
1103       lightweight_unlock(object, header, slow_case);
1104     } else {
1105       addi(object_mark_addr, object, oopDesc::mark_offset_in_bytes());
1106 
1107       // We have the displaced header in displaced_header. If the lock is still
1108       // lightweight, it will contain the monitor address and we'll store the
1109       // displaced header back into the object's mark word.
1110       // CmpxchgX sets CCR0 to cmpX(current, monitor).
1111       cmpxchgd(/*flag=*/CCR0,
1112                /*current_value=*/current_header,
1113                /*compare_value=*/monitor, /*exchange_value=*/header,
1114                /*where=*/object_mark_addr,
1115                MacroAssembler::MemBarRel,
1116                MacroAssembler::cmpxchgx_hint_release_lock(),
1117                noreg,
1118                &slow_case);
1119     }
1120     b(free_slot);
1121 
1122     // } else {
1123     //   // Slow path.
1124     //   InterpreterRuntime::monitorexit(monitor);
1125 
1126     // The lock has been converted into a heavy lock and hence
1127     // we need to get into the slow case.
1128     bind(slow_case);
1129     call_VM_leaf(CAST_FROM_FN_PTR(address, InterpreterRuntime::monitorexit), monitor);
1130     // }
1131 
1132     Label done;
1133     b(done); // Monitor register may be overwritten! Runtime has already freed the slot.
1134 
1135     // Exchange worked, do monitor->set_obj(nullptr);
1136     align(32, 12);
1137     bind(free_slot);
1138     li(R0, 0);
1139     std(R0, in_bytes(BasicObjectLock::obj_offset()), monitor);
1140     dec_held_monitor_count(current_header /*tmp*/);
1141     bind(done);
1142   }
1143 }
1144 
1145 // Load compiled (i2c) or interpreter entry when calling from interpreted and
1146 // do the call. Centralized so that all interpreter calls will do the same actions.
1147 // If jvmti single stepping is on for a thread we must not call compiled code.
1148 //
1149 // Input:
1150 //   - Rtarget_method: method to call
1151 //   - Rret_addr:      return address
1152 //   - 2 scratch regs
1153 //
1154 void InterpreterMacroAssembler::call_from_interpreter(Register Rtarget_method, Register Rret_addr,
1155                                                       Register Rscratch1, Register Rscratch2) {
1156   assert_different_registers(Rscratch1, Rscratch2, Rtarget_method, Rret_addr);
1157   // Assume we want to go compiled if available.
1158   const Register Rtarget_addr = Rscratch1;
1159   const Register Rinterp_only = Rscratch2;
1160 
1161   ld(Rtarget_addr, in_bytes(Method::from_interpreted_offset()), Rtarget_method);
1162 
1163   if (JvmtiExport::can_post_interpreter_events()) {
1164     lwz(Rinterp_only, in_bytes(JavaThread::interp_only_mode_offset()), R16_thread);
1165 
1166     // JVMTI events, such as single-stepping, are implemented partly by avoiding running
1167     // compiled code in threads for which the event is enabled. Check here for
1168     // interp_only_mode if these events CAN be enabled.
1169     Label done;
1170     cmpwi(CCR0, Rinterp_only, 0);
1171     beq(CCR0, done);
1172     ld(Rtarget_addr, in_bytes(Method::interpreter_entry_offset()), Rtarget_method);
1173     align(32, 12);
1174     bind(done);
1175   }
1176 
1177 #ifdef ASSERT
1178   {
1179     Label Lok;
1180     cmpdi(CCR0, Rtarget_addr, 0);
1181     bne(CCR0, Lok);
1182     stop("null entry point");
1183     bind(Lok);
1184   }
1185 #endif // ASSERT
1186 
1187   mr(R21_sender_SP, R1_SP);
1188 
1189   // Calc a precise SP for the call. The SP value we calculated in
1190   // generate_fixed_frame() is based on the max_stack() value, so we would waste stack space
1191   // if esp is not max. Also, the i2c adapter extends the stack space without restoring
1192   // our pre-calced value, so repeating calls via i2c would result in stack overflow.
1193   // Since esp already points to an empty slot, we just have to sub 1 additional slot
1194   // to meet the abi scratch requirements.
1195   // The max_stack pointer will get restored by means of the GR_Lmax_stack local in
1196   // the return entry of the interpreter.
1197   addi(Rscratch2, R15_esp, Interpreter::stackElementSize - frame::top_ijava_frame_abi_size);
1198   clrrdi(Rscratch2, Rscratch2, exact_log2(frame::alignment_in_bytes)); // round towards smaller address
1199   resize_frame_absolute(Rscratch2, Rscratch2, R0);
1200 
1201   mr_if_needed(R19_method, Rtarget_method);
1202   mtctr(Rtarget_addr);
1203   mtlr(Rret_addr);
1204 
1205   save_interpreter_state(Rscratch2);
1206 #ifdef ASSERT
1207   ld(Rscratch1, _ijava_state_neg(top_frame_sp), Rscratch2); // Rscratch2 contains fp
1208   cmpd(CCR0, R21_sender_SP, Rscratch1);
1209   asm_assert_eq("top_frame_sp incorrect");
1210 #endif
1211 
1212   bctr();
1213 }
1214 
1215 // Set the method data pointer for the current bcp.
1216 void InterpreterMacroAssembler::set_method_data_pointer_for_bcp() {
1217   assert(ProfileInterpreter, "must be profiling interpreter");
1218   Label get_continue;
1219   ld(R28_mdx, in_bytes(Method::method_data_offset()), R19_method);
1220   test_method_data_pointer(get_continue);
1221   call_VM_leaf(CAST_FROM_FN_PTR(address, InterpreterRuntime::bcp_to_di), R19_method, R14_bcp);
1222 
1223   addi(R28_mdx, R28_mdx, in_bytes(MethodData::data_offset()));
1224   add(R28_mdx, R28_mdx, R3_RET);
1225   bind(get_continue);
1226 }
1227 
1228 // Test ImethodDataPtr. If it is null, continue at the specified label.
1229 void InterpreterMacroAssembler::test_method_data_pointer(Label& zero_continue) {
1230   assert(ProfileInterpreter, "must be profiling interpreter");
1231   cmpdi(CCR0, R28_mdx, 0);
1232   beq(CCR0, zero_continue);
1233 }
1234 
1235 void InterpreterMacroAssembler::verify_method_data_pointer() {
1236   assert(ProfileInterpreter, "must be profiling interpreter");
1237 #ifdef ASSERT
1238   Label verify_continue;
1239   test_method_data_pointer(verify_continue);
1240 
1241   // If the mdp is valid, it will point to a DataLayout header which is
1242   // consistent with the bcp. The converse is highly probable also.
1243   lhz(R11_scratch1, in_bytes(DataLayout::bci_offset()), R28_mdx);
1244   ld(R12_scratch2, in_bytes(Method::const_offset()), R19_method);
1245   addi(R11_scratch1, R11_scratch1, in_bytes(ConstMethod::codes_offset()));
1246   add(R11_scratch1, R12_scratch2, R12_scratch2);
1247   cmpd(CCR0, R11_scratch1, R14_bcp);
1248   beq(CCR0, verify_continue);
1249 
1250   call_VM_leaf(CAST_FROM_FN_PTR(address, InterpreterRuntime::verify_mdp ), R19_method, R14_bcp, R28_mdx);
1251 
1252   bind(verify_continue);
1253 #endif
1254 }
1255 
1256 // Store a value at some constant offset from the method data pointer.
1257 void InterpreterMacroAssembler::set_mdp_data_at(int constant, Register value) {
1258   assert(ProfileInterpreter, "must be profiling interpreter");
1259 
1260   std(value, constant, R28_mdx);
1261 }
1262 
1263 // Increment the value at some constant offset from the method data pointer.
1264 void InterpreterMacroAssembler::increment_mdp_data_at(int constant,
1265                                                       Register counter_addr,
1266                                                       Register Rbumped_count,
1267                                                       bool decrement) {
1268   // Locate the counter at a fixed offset from the mdp:
1269   addi(counter_addr, R28_mdx, constant);
1270   increment_mdp_data_at(counter_addr, Rbumped_count, decrement);
1271 }
1272 
1273 // Increment the value at some non-fixed (reg + constant) offset from
1274 // the method data pointer.
1275 void InterpreterMacroAssembler::increment_mdp_data_at(Register reg,
1276                                                       int constant,
1277                                                       Register scratch,
1278                                                       Register Rbumped_count,
1279                                                       bool decrement) {
1280   // Add the constant to reg to get the offset.
1281   add(scratch, R28_mdx, reg);
1282   // Then calculate the counter address.
1283   addi(scratch, scratch, constant);
1284   increment_mdp_data_at(scratch, Rbumped_count, decrement);
1285 }
1286 
1287 void InterpreterMacroAssembler::increment_mdp_data_at(Register counter_addr,
1288                                                       Register Rbumped_count,
1289                                                       bool decrement) {
1290   assert(ProfileInterpreter, "must be profiling interpreter");
1291 
1292   // Load the counter.
1293   ld(Rbumped_count, 0, counter_addr);
1294 
1295   if (decrement) {
1296     // Decrement the register. Set condition codes.
1297     addi(Rbumped_count, Rbumped_count, - DataLayout::counter_increment);
1298     // Store the decremented counter, if it is still negative.
1299     std(Rbumped_count, 0, counter_addr);
1300     // Note: add/sub overflow check are not ported, since 64 bit
1301     // calculation should never overflow.
1302   } else {
1303     // Increment the register. Set carry flag.
1304     addi(Rbumped_count, Rbumped_count, DataLayout::counter_increment);
1305     // Store the incremented counter.
1306     std(Rbumped_count, 0, counter_addr);
1307   }
1308 }
1309 
1310 // Set a flag value at the current method data pointer position.
1311 void InterpreterMacroAssembler::set_mdp_flag_at(int flag_constant,
1312                                                 Register scratch) {
1313   assert(ProfileInterpreter, "must be profiling interpreter");
1314   // Load the data header.
1315   lbz(scratch, in_bytes(DataLayout::flags_offset()), R28_mdx);
1316   // Set the flag.
1317   ori(scratch, scratch, flag_constant);
1318   // Store the modified header.
1319   stb(scratch, in_bytes(DataLayout::flags_offset()), R28_mdx);
1320 }
1321 
1322 // Test the location at some offset from the method data pointer.
1323 // If it is not equal to value, branch to the not_equal_continue Label.
1324 void InterpreterMacroAssembler::test_mdp_data_at(int offset,
1325                                                  Register value,
1326                                                  Label& not_equal_continue,
1327                                                  Register test_out) {
1328   assert(ProfileInterpreter, "must be profiling interpreter");
1329 
1330   ld(test_out, offset, R28_mdx);
1331   cmpd(CCR0,  value, test_out);
1332   bne(CCR0, not_equal_continue);
1333 }
1334 
1335 // Update the method data pointer by the displacement located at some fixed
1336 // offset from the method data pointer.
1337 void InterpreterMacroAssembler::update_mdp_by_offset(int offset_of_disp,
1338                                                      Register scratch) {
1339   assert(ProfileInterpreter, "must be profiling interpreter");
1340 
1341   ld(scratch, offset_of_disp, R28_mdx);
1342   add(R28_mdx, scratch, R28_mdx);
1343 }
1344 
1345 // Update the method data pointer by the displacement located at the
1346 // offset (reg + offset_of_disp).
1347 void InterpreterMacroAssembler::update_mdp_by_offset(Register reg,
1348                                                      int offset_of_disp,
1349                                                      Register scratch) {
1350   assert(ProfileInterpreter, "must be profiling interpreter");
1351 
1352   add(scratch, reg, R28_mdx);
1353   ld(scratch, offset_of_disp, scratch);
1354   add(R28_mdx, scratch, R28_mdx);
1355 }
1356 
1357 // Update the method data pointer by a simple constant displacement.
1358 void InterpreterMacroAssembler::update_mdp_by_constant(int constant) {
1359   assert(ProfileInterpreter, "must be profiling interpreter");
1360   addi(R28_mdx, R28_mdx, constant);
1361 }
1362 
1363 // Update the method data pointer for a _ret bytecode whose target
1364 // was not among our cached targets.
1365 void InterpreterMacroAssembler::update_mdp_for_ret(TosState state,
1366                                                    Register return_bci) {
1367   assert(ProfileInterpreter, "must be profiling interpreter");
1368 
1369   push(state);
1370   assert(return_bci->is_nonvolatile(), "need to protect return_bci");
1371   call_VM(noreg, CAST_FROM_FN_PTR(address, InterpreterRuntime::update_mdp_for_ret), return_bci);
1372   pop(state);
1373 }
1374 
1375 // Increments the backedge counter.
1376 // Returns backedge counter + invocation counter in Rdst.
1377 void InterpreterMacroAssembler::increment_backedge_counter(const Register Rcounters, const Register Rdst,
1378                                                            const Register Rtmp1, Register Rscratch) {
1379   assert(UseCompiler, "incrementing must be useful");
1380   assert_different_registers(Rdst, Rtmp1);
1381   const Register invocation_counter = Rtmp1;
1382   const Register counter = Rdst;
1383   // TODO: PPC port: assert(4 == InvocationCounter::sz_counter(), "unexpected field size.");
1384 
1385   // Load backedge counter.
1386   lwz(counter, in_bytes(MethodCounters::backedge_counter_offset()) +
1387                in_bytes(InvocationCounter::counter_offset()), Rcounters);
1388   // Load invocation counter.
1389   lwz(invocation_counter, in_bytes(MethodCounters::invocation_counter_offset()) +
1390                           in_bytes(InvocationCounter::counter_offset()), Rcounters);
1391 
1392   // Add the delta to the backedge counter.
1393   addi(counter, counter, InvocationCounter::count_increment);
1394 
1395   // Mask the invocation counter.
1396   andi(invocation_counter, invocation_counter, InvocationCounter::count_mask_value);
1397 
1398   // Store new counter value.
1399   stw(counter, in_bytes(MethodCounters::backedge_counter_offset()) +
1400                in_bytes(InvocationCounter::counter_offset()), Rcounters);
1401   // Return invocation counter + backedge counter.
1402   add(counter, counter, invocation_counter);
1403 }
1404 
1405 // Count a taken branch in the bytecodes.
1406 void InterpreterMacroAssembler::profile_taken_branch(Register scratch, Register bumped_count) {
1407   if (ProfileInterpreter) {
1408     Label profile_continue;
1409 
1410     // If no method data exists, go to profile_continue.
1411     test_method_data_pointer(profile_continue);
1412 
1413     // We are taking a branch. Increment the taken count.
1414     increment_mdp_data_at(in_bytes(JumpData::taken_offset()), scratch, bumped_count);
1415 
1416     // The method data pointer needs to be updated to reflect the new target.
1417     update_mdp_by_offset(in_bytes(JumpData::displacement_offset()), scratch);
1418     bind (profile_continue);
1419   }
1420 }
1421 
1422 // Count a not-taken branch in the bytecodes.
1423 void InterpreterMacroAssembler::profile_not_taken_branch(Register scratch1, Register scratch2) {
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 not taken count.
1431     increment_mdp_data_at(in_bytes(BranchData::not_taken_offset()), scratch1, scratch2);
1432 
1433     // The method data pointer needs to be updated to correspond to the
1434     // next bytecode.
1435     update_mdp_by_constant(in_bytes(BranchData::branch_data_size()));
1436     bind (profile_continue);
1437   }
1438 }
1439 
1440 // Count a non-virtual call in the bytecodes.
1441 void InterpreterMacroAssembler::profile_call(Register scratch1, Register scratch2) {
1442   if (ProfileInterpreter) {
1443     Label profile_continue;
1444 
1445     // If no method data exists, go to profile_continue.
1446     test_method_data_pointer(profile_continue);
1447 
1448     // We are making a call. Increment the count.
1449     increment_mdp_data_at(in_bytes(CounterData::count_offset()), scratch1, scratch2);
1450 
1451     // The method data pointer needs to be updated to reflect the new target.
1452     update_mdp_by_constant(in_bytes(CounterData::counter_data_size()));
1453     bind (profile_continue);
1454   }
1455 }
1456 
1457 // Count a final call in the bytecodes.
1458 void InterpreterMacroAssembler::profile_final_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(VirtualCallData::virtual_call_data_size()));
1470     bind (profile_continue);
1471   }
1472 }
1473 
1474 // Count a virtual call in the bytecodes.
1475 void InterpreterMacroAssembler::profile_virtual_call(Register Rreceiver,
1476                                                      Register Rscratch1,
1477                                                      Register Rscratch2,
1478                                                      bool receiver_can_be_null) {
1479   if (!ProfileInterpreter) { return; }
1480   Label profile_continue;
1481 
1482   // If no method data exists, go to profile_continue.
1483   test_method_data_pointer(profile_continue);
1484 
1485   Label skip_receiver_profile;
1486   if (receiver_can_be_null) {
1487     Label not_null;
1488     cmpdi(CCR0, Rreceiver, 0);
1489     bne(CCR0, not_null);
1490     // We are making a call. Increment the count for null receiver.
1491     increment_mdp_data_at(in_bytes(CounterData::count_offset()), Rscratch1, Rscratch2);
1492     b(skip_receiver_profile);
1493     bind(not_null);
1494   }
1495 
1496   // Record the receiver type.
1497   record_klass_in_profile(Rreceiver, Rscratch1, Rscratch2, true);
1498   bind(skip_receiver_profile);
1499 
1500   // The method data pointer needs to be updated to reflect the new target.
1501   update_mdp_by_constant(in_bytes(VirtualCallData::virtual_call_data_size()));
1502   bind (profile_continue);
1503 }
1504 
1505 void InterpreterMacroAssembler::profile_typecheck(Register Rklass, Register Rscratch1, Register Rscratch2) {
1506   if (ProfileInterpreter) {
1507     Label profile_continue;
1508 
1509     // If no method data exists, go to profile_continue.
1510     test_method_data_pointer(profile_continue);
1511 
1512     int mdp_delta = in_bytes(BitData::bit_data_size());
1513     if (TypeProfileCasts) {
1514       mdp_delta = in_bytes(VirtualCallData::virtual_call_data_size());
1515 
1516       // Record the object type.
1517       record_klass_in_profile(Rklass, Rscratch1, Rscratch2, false);
1518     }
1519 
1520     // The method data pointer needs to be updated.
1521     update_mdp_by_constant(mdp_delta);
1522 
1523     bind (profile_continue);
1524   }
1525 }
1526 
1527 void InterpreterMacroAssembler::profile_typecheck_failed(Register Rscratch1, Register Rscratch2) {
1528   if (ProfileInterpreter && TypeProfileCasts) {
1529     Label profile_continue;
1530 
1531     // If no method data exists, go to profile_continue.
1532     test_method_data_pointer(profile_continue);
1533 
1534     int count_offset = in_bytes(CounterData::count_offset());
1535     // Back up the address, since we have already bumped the mdp.
1536     count_offset -= in_bytes(VirtualCallData::virtual_call_data_size());
1537 
1538     // *Decrement* the counter. We expect to see zero or small negatives.
1539     increment_mdp_data_at(count_offset, Rscratch1, Rscratch2, true);
1540 
1541     bind (profile_continue);
1542   }
1543 }
1544 
1545 // Count a ret in the bytecodes.
1546 void InterpreterMacroAssembler::profile_ret(TosState state, Register return_bci,
1547                                             Register scratch1, Register scratch2) {
1548   if (ProfileInterpreter) {
1549     Label profile_continue;
1550     uint row;
1551 
1552     // If no method data exists, go to profile_continue.
1553     test_method_data_pointer(profile_continue);
1554 
1555     // Update the total ret count.
1556     increment_mdp_data_at(in_bytes(CounterData::count_offset()), scratch1, scratch2 );
1557 
1558     for (row = 0; row < RetData::row_limit(); row++) {
1559       Label next_test;
1560 
1561       // See if return_bci is equal to bci[n]:
1562       test_mdp_data_at(in_bytes(RetData::bci_offset(row)), return_bci, next_test, scratch1);
1563 
1564       // return_bci is equal to bci[n]. Increment the count.
1565       increment_mdp_data_at(in_bytes(RetData::bci_count_offset(row)), scratch1, scratch2);
1566 
1567       // The method data pointer needs to be updated to reflect the new target.
1568       update_mdp_by_offset(in_bytes(RetData::bci_displacement_offset(row)), scratch1);
1569       b(profile_continue);
1570       bind(next_test);
1571     }
1572 
1573     update_mdp_for_ret(state, return_bci);
1574 
1575     bind (profile_continue);
1576   }
1577 }
1578 
1579 // Count the default case of a switch construct.
1580 void InterpreterMacroAssembler::profile_switch_default(Register scratch1,  Register scratch2) {
1581   if (ProfileInterpreter) {
1582     Label profile_continue;
1583 
1584     // If no method data exists, go to profile_continue.
1585     test_method_data_pointer(profile_continue);
1586 
1587     // Update the default case count
1588     increment_mdp_data_at(in_bytes(MultiBranchData::default_count_offset()),
1589                           scratch1, scratch2);
1590 
1591     // The method data pointer needs to be updated.
1592     update_mdp_by_offset(in_bytes(MultiBranchData::default_displacement_offset()),
1593                          scratch1);
1594 
1595     bind (profile_continue);
1596   }
1597 }
1598 
1599 // Count the index'th case of a switch construct.
1600 void InterpreterMacroAssembler::profile_switch_case(Register index,
1601                                                     Register scratch1,
1602                                                     Register scratch2,
1603                                                     Register scratch3) {
1604   if (ProfileInterpreter) {
1605     assert_different_registers(index, scratch1, scratch2, scratch3);
1606     Label profile_continue;
1607 
1608     // If no method data exists, go to profile_continue.
1609     test_method_data_pointer(profile_continue);
1610 
1611     // Build the base (index * per_case_size_in_bytes()) + case_array_offset_in_bytes().
1612     li(scratch3, in_bytes(MultiBranchData::case_array_offset()));
1613 
1614     assert (in_bytes(MultiBranchData::per_case_size()) == 16, "so that shladd works");
1615     sldi(scratch1, index, exact_log2(in_bytes(MultiBranchData::per_case_size())));
1616     add(scratch1, scratch1, scratch3);
1617 
1618     // Update the case count.
1619     increment_mdp_data_at(scratch1, in_bytes(MultiBranchData::relative_count_offset()), scratch2, scratch3);
1620 
1621     // The method data pointer needs to be updated.
1622     update_mdp_by_offset(scratch1, in_bytes(MultiBranchData::relative_displacement_offset()), scratch2);
1623 
1624     bind (profile_continue);
1625   }
1626 }
1627 
1628 void InterpreterMacroAssembler::profile_null_seen(Register Rscratch1, Register Rscratch2) {
1629   if (ProfileInterpreter) {
1630     assert_different_registers(Rscratch1, Rscratch2);
1631     Label profile_continue;
1632 
1633     // If no method data exists, go to profile_continue.
1634     test_method_data_pointer(profile_continue);
1635 
1636     set_mdp_flag_at(BitData::null_seen_byte_constant(), Rscratch1);
1637 
1638     // The method data pointer needs to be updated.
1639     int mdp_delta = in_bytes(BitData::bit_data_size());
1640     if (TypeProfileCasts) {
1641       mdp_delta = in_bytes(VirtualCallData::virtual_call_data_size());
1642     }
1643     update_mdp_by_constant(mdp_delta);
1644 
1645     bind (profile_continue);
1646   }
1647 }
1648 
1649 void InterpreterMacroAssembler::record_klass_in_profile(Register Rreceiver,
1650                                                         Register Rscratch1, Register Rscratch2,
1651                                                         bool is_virtual_call) {
1652   assert(ProfileInterpreter, "must be profiling");
1653   assert_different_registers(Rreceiver, Rscratch1, Rscratch2);
1654 
1655   Label done;
1656   record_klass_in_profile_helper(Rreceiver, Rscratch1, Rscratch2, 0, done, is_virtual_call);
1657   bind (done);
1658 }
1659 
1660 void InterpreterMacroAssembler::record_klass_in_profile_helper(
1661                                         Register receiver, Register scratch1, Register scratch2,
1662                                         int start_row, Label& done, bool is_virtual_call) {
1663   if (TypeProfileWidth == 0) {
1664     if (is_virtual_call) {
1665       increment_mdp_data_at(in_bytes(CounterData::count_offset()), scratch1, scratch2);
1666     }
1667     return;
1668   }
1669 
1670   int last_row = VirtualCallData::row_limit() - 1;
1671   assert(start_row <= last_row, "must be work left to do");
1672   // Test this row for both the receiver and for null.
1673   // Take any of three different outcomes:
1674   //   1. found receiver => increment count and goto done
1675   //   2. found null => keep looking for case 1, maybe allocate this cell
1676   //   3. found something else => keep looking for cases 1 and 2
1677   // Case 3 is handled by a recursive call.
1678   for (int row = start_row; row <= last_row; row++) {
1679     Label next_test;
1680     bool test_for_null_also = (row == start_row);
1681 
1682     // See if the receiver is receiver[n].
1683     int recvr_offset = in_bytes(VirtualCallData::receiver_offset(row));
1684     test_mdp_data_at(recvr_offset, receiver, next_test, scratch1);
1685     // delayed()->tst(scratch);
1686 
1687     // The receiver is receiver[n]. Increment count[n].
1688     int count_offset = in_bytes(VirtualCallData::receiver_count_offset(row));
1689     increment_mdp_data_at(count_offset, scratch1, scratch2);
1690     b(done);
1691     bind(next_test);
1692 
1693     if (test_for_null_also) {
1694       Label found_null;
1695       // Failed the equality check on receiver[n]... Test for null.
1696       if (start_row == last_row) {
1697         // The only thing left to do is handle the null case.
1698         if (is_virtual_call) {
1699           // Scratch1 contains test_out from test_mdp_data_at.
1700           cmpdi(CCR0, scratch1, 0);
1701           beq(CCR0, found_null);
1702           // Receiver did not match any saved receiver and there is no empty row for it.
1703           // Increment total counter to indicate polymorphic case.
1704           increment_mdp_data_at(in_bytes(CounterData::count_offset()), scratch1, scratch2);
1705           b(done);
1706           bind(found_null);
1707         } else {
1708           cmpdi(CCR0, scratch1, 0);
1709           bne(CCR0, done);
1710         }
1711         break;
1712       }
1713       // Since null is rare, make it be the branch-taken case.
1714       cmpdi(CCR0, scratch1, 0);
1715       beq(CCR0, found_null);
1716 
1717       // Put all the "Case 3" tests here.
1718       record_klass_in_profile_helper(receiver, scratch1, scratch2, start_row + 1, done, is_virtual_call);
1719 
1720       // Found a null. Keep searching for a matching receiver,
1721       // but remember that this is an empty (unused) slot.
1722       bind(found_null);
1723     }
1724   }
1725 
1726   // In the fall-through case, we found no matching receiver, but we
1727   // observed the receiver[start_row] is null.
1728 
1729   // Fill in the receiver field and increment the count.
1730   int recvr_offset = in_bytes(VirtualCallData::receiver_offset(start_row));
1731   set_mdp_data_at(recvr_offset, receiver);
1732   int count_offset = in_bytes(VirtualCallData::receiver_count_offset(start_row));
1733   li(scratch1, DataLayout::counter_increment);
1734   set_mdp_data_at(count_offset, scratch1);
1735   if (start_row > 0) {
1736     b(done);
1737   }
1738 }
1739 
1740 // Argument and return type profilig.
1741 // kills: tmp, tmp2, R0, CR0, CR1
1742 void InterpreterMacroAssembler::profile_obj_type(Register obj, Register mdo_addr_base,
1743                                                  RegisterOrConstant mdo_addr_offs,
1744                                                  Register tmp, Register tmp2) {
1745   Label do_nothing, do_update;
1746 
1747   // tmp2 = obj is allowed
1748   assert_different_registers(obj, mdo_addr_base, tmp, R0);
1749   assert_different_registers(tmp2, mdo_addr_base, tmp, R0);
1750   const Register klass = tmp2;
1751 
1752   verify_oop(obj);
1753 
1754   ld(tmp, mdo_addr_offs, mdo_addr_base);
1755 
1756   // Set null_seen if obj is 0.
1757   cmpdi(CCR0, obj, 0);
1758   ori(R0, tmp, TypeEntries::null_seen);
1759   beq(CCR0, do_update);
1760 
1761   load_klass(klass, obj);
1762 
1763   clrrdi(R0, tmp, exact_log2(-TypeEntries::type_klass_mask));
1764   // Basically same as andi(R0, tmp, TypeEntries::type_klass_mask);
1765   cmpd(CCR1, R0, klass);
1766   // Klass seen before, nothing to do (regardless of unknown bit).
1767   //beq(CCR1, do_nothing);
1768 
1769   andi_(R0, tmp, TypeEntries::type_unknown);
1770   // Already unknown. Nothing to do anymore.
1771   //bne(CCR0, do_nothing);
1772   crorc(CCR0, Assembler::equal, CCR1, Assembler::equal); // cr0 eq = cr1 eq or cr0 ne
1773   beq(CCR0, do_nothing);
1774 
1775   clrrdi_(R0, tmp, exact_log2(-TypeEntries::type_mask));
1776   orr(R0, klass, tmp); // Combine klass and null_seen bit (only used if (tmp & type_mask)==0).
1777   beq(CCR0, do_update); // First time here. Set profile type.
1778 
1779   // Different than before. Cannot keep accurate profile.
1780   ori(R0, tmp, TypeEntries::type_unknown);
1781 
1782   bind(do_update);
1783   // update profile
1784   std(R0, mdo_addr_offs, mdo_addr_base);
1785 
1786   align(32, 12);
1787   bind(do_nothing);
1788 }
1789 
1790 void InterpreterMacroAssembler::profile_arguments_type(Register callee,
1791                                                        Register tmp1, Register tmp2,
1792                                                        bool is_virtual) {
1793   if (!ProfileInterpreter) {
1794     return;
1795   }
1796 
1797   assert_different_registers(callee, tmp1, tmp2, R28_mdx);
1798 
1799   if (MethodData::profile_arguments() || MethodData::profile_return()) {
1800     Label profile_continue;
1801 
1802     test_method_data_pointer(profile_continue);
1803 
1804     int off_to_start = is_virtual ?
1805       in_bytes(VirtualCallData::virtual_call_data_size()) : in_bytes(CounterData::counter_data_size());
1806 
1807     lbz(tmp1, in_bytes(DataLayout::tag_offset()) - off_to_start, R28_mdx);
1808     cmpwi(CCR0, tmp1, is_virtual ? DataLayout::virtual_call_type_data_tag : DataLayout::call_type_data_tag);
1809     bne(CCR0, profile_continue);
1810 
1811     if (MethodData::profile_arguments()) {
1812       Label done;
1813       int off_to_args = in_bytes(TypeEntriesAtCall::args_data_offset());
1814       addi(R28_mdx, R28_mdx, off_to_args);
1815 
1816       for (int i = 0; i < TypeProfileArgsLimit; i++) {
1817         if (i > 0 || MethodData::profile_return()) {
1818           // If return value type is profiled we may have no argument to profile.
1819           ld(tmp1, in_bytes(TypeEntriesAtCall::cell_count_offset())-off_to_args, R28_mdx);
1820           cmpdi(CCR0, tmp1, (i+1)*TypeStackSlotEntries::per_arg_count());
1821           addi(tmp1, tmp1, -i*TypeStackSlotEntries::per_arg_count());
1822           blt(CCR0, done);
1823         }
1824         ld(tmp1, in_bytes(Method::const_offset()), callee);
1825         lhz(tmp1, in_bytes(ConstMethod::size_of_parameters_offset()), tmp1);
1826         // Stack offset o (zero based) from the start of the argument
1827         // list, for n arguments translates into offset n - o - 1 from
1828         // the end of the argument list. But there's an extra slot at
1829         // the top of the stack. So the offset is n - o from Lesp.
1830         ld(tmp2, in_bytes(TypeEntriesAtCall::stack_slot_offset(i))-off_to_args, R28_mdx);
1831         subf(tmp1, tmp2, tmp1);
1832 
1833         sldi(tmp1, tmp1, Interpreter::logStackElementSize);
1834         ldx(tmp1, tmp1, R15_esp);
1835 
1836         profile_obj_type(tmp1, R28_mdx, in_bytes(TypeEntriesAtCall::argument_type_offset(i))-off_to_args, tmp2, tmp1);
1837 
1838         int to_add = in_bytes(TypeStackSlotEntries::per_arg_size());
1839         addi(R28_mdx, R28_mdx, to_add);
1840         off_to_args += to_add;
1841       }
1842 
1843       if (MethodData::profile_return()) {
1844         ld(tmp1, in_bytes(TypeEntriesAtCall::cell_count_offset())-off_to_args, R28_mdx);
1845         addi(tmp1, tmp1, -TypeProfileArgsLimit*TypeStackSlotEntries::per_arg_count());
1846       }
1847 
1848       bind(done);
1849 
1850       if (MethodData::profile_return()) {
1851         // We're right after the type profile for the last
1852         // argument. tmp1 is the number of cells left in the
1853         // CallTypeData/VirtualCallTypeData to reach its end. Non null
1854         // if there's a return to profile.
1855         assert(ReturnTypeEntry::static_cell_count() < TypeStackSlotEntries::per_arg_count(),
1856                "can't move past ret type");
1857         sldi(tmp1, tmp1, exact_log2(DataLayout::cell_size));
1858         add(R28_mdx, tmp1, R28_mdx);
1859       }
1860     } else {
1861       assert(MethodData::profile_return(), "either profile call args or call ret");
1862       update_mdp_by_constant(in_bytes(TypeEntriesAtCall::return_only_size()));
1863     }
1864 
1865     // Mdp points right after the end of the
1866     // CallTypeData/VirtualCallTypeData, right after the cells for the
1867     // return value type if there's one.
1868     align(32, 12);
1869     bind(profile_continue);
1870   }
1871 }
1872 
1873 void InterpreterMacroAssembler::profile_return_type(Register ret, Register tmp1, Register tmp2) {
1874   assert_different_registers(ret, tmp1, tmp2);
1875   if (ProfileInterpreter && MethodData::profile_return()) {
1876     Label profile_continue;
1877 
1878     test_method_data_pointer(profile_continue);
1879 
1880     if (MethodData::profile_return_jsr292_only()) {
1881       // If we don't profile all invoke bytecodes we must make sure
1882       // it's a bytecode we indeed profile. We can't go back to the
1883       // beginning of the ProfileData we intend to update to check its
1884       // type because we're right after it and we don't known its
1885       // length.
1886       lbz(tmp1, 0, R14_bcp);
1887       lbz(tmp2, in_bytes(Method::intrinsic_id_offset()), R19_method);
1888       cmpwi(CCR0, tmp1, Bytecodes::_invokedynamic);
1889       cmpwi(CCR1, tmp1, Bytecodes::_invokehandle);
1890       cror(CCR0, Assembler::equal, CCR1, Assembler::equal);
1891       cmpwi(CCR1, tmp2, static_cast<int>(vmIntrinsics::_compiledLambdaForm));
1892       cror(CCR0, Assembler::equal, CCR1, Assembler::equal);
1893       bne(CCR0, profile_continue);
1894     }
1895 
1896     profile_obj_type(ret, R28_mdx, -in_bytes(ReturnTypeEntry::size()), tmp1, tmp2);
1897 
1898     align(32, 12);
1899     bind(profile_continue);
1900   }
1901 }
1902 
1903 void InterpreterMacroAssembler::profile_parameters_type(Register tmp1, Register tmp2,
1904                                                         Register tmp3, Register tmp4) {
1905   if (ProfileInterpreter && MethodData::profile_parameters()) {
1906     Label profile_continue, done;
1907 
1908     test_method_data_pointer(profile_continue);
1909 
1910     // Load the offset of the area within the MDO used for
1911     // parameters. If it's negative we're not profiling any parameters.
1912     lwz(tmp1, in_bytes(MethodData::parameters_type_data_di_offset()) - in_bytes(MethodData::data_offset()), R28_mdx);
1913     cmpwi(CCR0, tmp1, 0);
1914     blt(CCR0, profile_continue);
1915 
1916     // Compute a pointer to the area for parameters from the offset
1917     // and move the pointer to the slot for the last
1918     // parameters. Collect profiling from last parameter down.
1919     // mdo start + parameters offset + array length - 1
1920 
1921     // Pointer to the parameter area in the MDO.
1922     const Register mdp = tmp1;
1923     add(mdp, tmp1, R28_mdx);
1924 
1925     // Offset of the current profile entry to update.
1926     const Register entry_offset = tmp2;
1927     // entry_offset = array len in number of cells
1928     ld(entry_offset, in_bytes(ArrayData::array_len_offset()), mdp);
1929 
1930     int off_base = in_bytes(ParametersTypeData::stack_slot_offset(0));
1931     assert(off_base % DataLayout::cell_size == 0, "should be a number of cells");
1932 
1933     // entry_offset (number of cells)  = array len - size of 1 entry + offset of the stack slot field
1934     addi(entry_offset, entry_offset, -TypeStackSlotEntries::per_arg_count() + (off_base / DataLayout::cell_size));
1935     // entry_offset in bytes
1936     sldi(entry_offset, entry_offset, exact_log2(DataLayout::cell_size));
1937 
1938     Label loop;
1939     align(32, 12);
1940     bind(loop);
1941 
1942     // Load offset on the stack from the slot for this parameter.
1943     ld(tmp3, entry_offset, mdp);
1944     sldi(tmp3, tmp3, Interpreter::logStackElementSize);
1945     neg(tmp3, tmp3);
1946     // Read the parameter from the local area.
1947     ldx(tmp3, tmp3, R18_locals);
1948 
1949     // Make entry_offset now point to the type field for this parameter.
1950     int type_base = in_bytes(ParametersTypeData::type_offset(0));
1951     assert(type_base > off_base, "unexpected");
1952     addi(entry_offset, entry_offset, type_base - off_base);
1953 
1954     // Profile the parameter.
1955     profile_obj_type(tmp3, mdp, entry_offset, tmp4, tmp3);
1956 
1957     // Go to next parameter.
1958     int delta = TypeStackSlotEntries::per_arg_count() * DataLayout::cell_size + (type_base - off_base);
1959     cmpdi(CCR0, entry_offset, off_base + delta);
1960     addi(entry_offset, entry_offset, -delta);
1961     bge(CCR0, loop);
1962 
1963     align(32, 12);
1964     bind(profile_continue);
1965   }
1966 }
1967 
1968 // Add a monitor (see frame_ppc.hpp).
1969 void InterpreterMacroAssembler::add_monitor_to_stack(bool stack_is_empty, Register Rtemp1, Register Rtemp2) {
1970 
1971   // Very-local scratch registers.
1972   const Register esp  = Rtemp1;
1973   const Register slot = Rtemp2;
1974 
1975   // Extracted monitor_size.
1976   int monitor_size = frame::interpreter_frame_monitor_size_in_bytes();
1977   assert(Assembler::is_aligned((unsigned int)monitor_size,
1978                                (unsigned int)frame::alignment_in_bytes),
1979          "size of a monitor must respect alignment of SP");
1980 
1981   resize_frame(-monitor_size, /*temp*/esp); // Allocate space for new monitor
1982   std(R1_SP, _ijava_state_neg(top_frame_sp), esp); // esp contains fp
1983 
1984   // Shuffle expression stack down. Recall that stack_base points
1985   // just above the new expression stack bottom. Old_tos and new_tos
1986   // are used to scan thru the old and new expression stacks.
1987   if (!stack_is_empty) {
1988     Label copy_slot, copy_slot_finished;
1989     const Register n_slots = slot;
1990 
1991     addi(esp, R15_esp, Interpreter::stackElementSize); // Point to first element (pre-pushed stack).
1992     subf(n_slots, esp, R26_monitor);
1993     srdi_(n_slots, n_slots, LogBytesPerWord);          // Compute number of slots to copy.
1994     assert(LogBytesPerWord == 3, "conflicts assembler instructions");
1995     beq(CCR0, copy_slot_finished);                     // Nothing to copy.
1996 
1997     mtctr(n_slots);
1998 
1999     // loop
2000     bind(copy_slot);
2001     ld(slot, 0, esp);              // Move expression stack down.
2002     std(slot, -monitor_size, esp); // distance = monitor_size
2003     addi(esp, esp, BytesPerWord);
2004     bdnz(copy_slot);
2005 
2006     bind(copy_slot_finished);
2007   }
2008 
2009   addi(R15_esp, R15_esp, -monitor_size);
2010   addi(R26_monitor, R26_monitor, -monitor_size);
2011 
2012   // Restart interpreter
2013 }
2014 
2015 // ============================================================================
2016 // Java locals access
2017 
2018 // Load a local variable at index in Rindex into register Rdst_value.
2019 // Also puts address of local into Rdst_address as a service.
2020 // Kills:
2021 //   - Rdst_value
2022 //   - Rdst_address
2023 void InterpreterMacroAssembler::load_local_int(Register Rdst_value, Register Rdst_address, Register Rindex) {
2024   sldi(Rdst_address, Rindex, Interpreter::logStackElementSize);
2025   subf(Rdst_address, Rdst_address, R18_locals);
2026   lwz(Rdst_value, 0, Rdst_address);
2027 }
2028 
2029 // Load a local variable at index in Rindex into register Rdst_value.
2030 // Also puts address of local into Rdst_address as a service.
2031 // Kills:
2032 //   - Rdst_value
2033 //   - Rdst_address
2034 void InterpreterMacroAssembler::load_local_long(Register Rdst_value, Register Rdst_address, Register Rindex) {
2035   sldi(Rdst_address, Rindex, Interpreter::logStackElementSize);
2036   subf(Rdst_address, Rdst_address, R18_locals);
2037   ld(Rdst_value, -8, Rdst_address);
2038 }
2039 
2040 // Load a local variable at index in Rindex into register Rdst_value.
2041 // Also puts address of local into Rdst_address as a service.
2042 // Input:
2043 //   - Rindex:      slot nr of local variable
2044 // Kills:
2045 //   - Rdst_value
2046 //   - Rdst_address
2047 void InterpreterMacroAssembler::load_local_ptr(Register Rdst_value,
2048                                                Register Rdst_address,
2049                                                Register Rindex) {
2050   sldi(Rdst_address, Rindex, Interpreter::logStackElementSize);
2051   subf(Rdst_address, Rdst_address, R18_locals);
2052   ld(Rdst_value, 0, Rdst_address);
2053 }
2054 
2055 // Load a local variable at index in Rindex into register Rdst_value.
2056 // Also puts address of local into Rdst_address as a service.
2057 // Kills:
2058 //   - Rdst_value
2059 //   - Rdst_address
2060 void InterpreterMacroAssembler::load_local_float(FloatRegister Rdst_value,
2061                                                  Register Rdst_address,
2062                                                  Register Rindex) {
2063   sldi(Rdst_address, Rindex, Interpreter::logStackElementSize);
2064   subf(Rdst_address, Rdst_address, R18_locals);
2065   lfs(Rdst_value, 0, Rdst_address);
2066 }
2067 
2068 // Load a local variable at index in Rindex into register Rdst_value.
2069 // Also puts address of local into Rdst_address as a service.
2070 // Kills:
2071 //   - Rdst_value
2072 //   - Rdst_address
2073 void InterpreterMacroAssembler::load_local_double(FloatRegister Rdst_value,
2074                                                   Register Rdst_address,
2075                                                   Register Rindex) {
2076   sldi(Rdst_address, Rindex, Interpreter::logStackElementSize);
2077   subf(Rdst_address, Rdst_address, R18_locals);
2078   lfd(Rdst_value, -8, Rdst_address);
2079 }
2080 
2081 // Store an int value at local variable slot Rindex.
2082 // Kills:
2083 //   - Rindex
2084 void InterpreterMacroAssembler::store_local_int(Register Rvalue, Register Rindex) {
2085   sldi(Rindex, Rindex, Interpreter::logStackElementSize);
2086   subf(Rindex, Rindex, R18_locals);
2087   stw(Rvalue, 0, Rindex);
2088 }
2089 
2090 // Store a long value at local variable slot Rindex.
2091 // Kills:
2092 //   - Rindex
2093 void InterpreterMacroAssembler::store_local_long(Register Rvalue, Register Rindex) {
2094   sldi(Rindex, Rindex, Interpreter::logStackElementSize);
2095   subf(Rindex, Rindex, R18_locals);
2096   std(Rvalue, -8, Rindex);
2097 }
2098 
2099 // Store an oop value at local variable slot Rindex.
2100 // Kills:
2101 //   - Rindex
2102 void InterpreterMacroAssembler::store_local_ptr(Register Rvalue, Register Rindex) {
2103   sldi(Rindex, Rindex, Interpreter::logStackElementSize);
2104   subf(Rindex, Rindex, R18_locals);
2105   std(Rvalue, 0, Rindex);
2106 }
2107 
2108 // Store an int value at local variable slot Rindex.
2109 // Kills:
2110 //   - Rindex
2111 void InterpreterMacroAssembler::store_local_float(FloatRegister Rvalue, Register Rindex) {
2112   sldi(Rindex, Rindex, Interpreter::logStackElementSize);
2113   subf(Rindex, Rindex, R18_locals);
2114   stfs(Rvalue, 0, Rindex);
2115 }
2116 
2117 // Store an int value at local variable slot Rindex.
2118 // Kills:
2119 //   - Rindex
2120 void InterpreterMacroAssembler::store_local_double(FloatRegister Rvalue, Register Rindex) {
2121   sldi(Rindex, Rindex, Interpreter::logStackElementSize);
2122   subf(Rindex, Rindex, R18_locals);
2123   stfd(Rvalue, -8, Rindex);
2124 }
2125 
2126 // Read pending exception from thread and jump to interpreter.
2127 // Throw exception entry if one if pending. Fall through otherwise.
2128 void InterpreterMacroAssembler::check_and_forward_exception(Register Rscratch1, Register Rscratch2) {
2129   assert_different_registers(Rscratch1, Rscratch2, R3);
2130   Register Rexception = Rscratch1;
2131   Register Rtmp       = Rscratch2;
2132   Label Ldone;
2133   // Get pending exception oop.
2134   ld(Rexception, thread_(pending_exception));
2135   cmpdi(CCR0, Rexception, 0);
2136   beq(CCR0, Ldone);
2137   li(Rtmp, 0);
2138   mr_if_needed(R3, Rexception);
2139   std(Rtmp, thread_(pending_exception)); // Clear exception in thread
2140   if (Interpreter::rethrow_exception_entry() != nullptr) {
2141     // Already got entry address.
2142     load_dispatch_table(Rtmp, (address*)Interpreter::rethrow_exception_entry());
2143   } else {
2144     // Dynamically load entry address.
2145     int simm16_rest = load_const_optimized(Rtmp, &Interpreter::_rethrow_exception_entry, R0, true);
2146     ld(Rtmp, simm16_rest, Rtmp);
2147   }
2148   mtctr(Rtmp);
2149   save_interpreter_state(Rtmp);
2150   bctr();
2151 
2152   align(32, 12);
2153   bind(Ldone);
2154 }
2155 
2156 void InterpreterMacroAssembler::call_VM(Register oop_result, address entry_point, bool check_exceptions) {
2157   save_interpreter_state(R11_scratch1);
2158 
2159   MacroAssembler::call_VM(oop_result, entry_point, false);
2160 
2161   restore_interpreter_state(R11_scratch1, /*bcp_and_mdx_only*/ true);
2162 
2163   check_and_handle_popframe(R11_scratch1);
2164   check_and_handle_earlyret(R11_scratch1);
2165   // Now check exceptions manually.
2166   if (check_exceptions) {
2167     check_and_forward_exception(R11_scratch1, R12_scratch2);
2168   }
2169 }
2170 
2171 void InterpreterMacroAssembler::call_VM(Register oop_result, address entry_point,
2172                                         Register arg_1, bool check_exceptions) {
2173   // ARG1 is reserved for the thread.
2174   mr_if_needed(R4_ARG2, arg_1);
2175   call_VM(oop_result, entry_point, check_exceptions);
2176 }
2177 
2178 void InterpreterMacroAssembler::call_VM(Register oop_result, address entry_point,
2179                                         Register arg_1, Register arg_2,
2180                                         bool check_exceptions) {
2181   // ARG1 is reserved for the thread.
2182   mr_if_needed(R4_ARG2, arg_1);
2183   assert(arg_2 != R4_ARG2, "smashed argument");
2184   mr_if_needed(R5_ARG3, arg_2);
2185   call_VM(oop_result, entry_point, check_exceptions);
2186 }
2187 
2188 void InterpreterMacroAssembler::call_VM(Register oop_result, address entry_point,
2189                                         Register arg_1, Register arg_2, Register arg_3,
2190                                         bool check_exceptions) {
2191   // ARG1 is reserved for the thread.
2192   mr_if_needed(R4_ARG2, arg_1);
2193   assert(arg_2 != R4_ARG2, "smashed argument");
2194   mr_if_needed(R5_ARG3, arg_2);
2195   assert(arg_3 != R4_ARG2 && arg_3 != R5_ARG3, "smashed argument");
2196   mr_if_needed(R6_ARG4, arg_3);
2197   call_VM(oop_result, entry_point, check_exceptions);
2198 }
2199 
2200 void InterpreterMacroAssembler::save_interpreter_state(Register scratch) {
2201   ld(scratch, 0, R1_SP);
2202   std(R15_esp, _ijava_state_neg(esp), scratch);
2203   std(R14_bcp, _ijava_state_neg(bcp), scratch);
2204   std(R26_monitor, _ijava_state_neg(monitors), scratch);
2205   if (ProfileInterpreter) { std(R28_mdx, _ijava_state_neg(mdx), scratch); }
2206   // Other entries should be unchanged.
2207 }
2208 
2209 void InterpreterMacroAssembler::restore_interpreter_state(Register scratch, bool bcp_and_mdx_only, bool restore_top_frame_sp) {
2210   ld_ptr(scratch, _abi0(callers_sp), R1_SP);   // Load frame pointer.
2211   if (restore_top_frame_sp) {
2212     // After thawing the top frame of a continuation we reach here with frame::java_abi.
2213     // therefore we have to restore top_frame_sp before the assertion below.
2214     assert(!bcp_and_mdx_only, "chose other registers");
2215     Register tfsp = R18_locals;
2216     Register scratch2 = R26_monitor;
2217     ld(tfsp, _ijava_state_neg(top_frame_sp), scratch);
2218     resize_frame_absolute(tfsp, scratch2, R0);
2219   }
2220   ld(R14_bcp, _ijava_state_neg(bcp), scratch); // Changed by VM code (exception).
2221   if (ProfileInterpreter) { ld(R28_mdx, _ijava_state_neg(mdx), scratch); } // Changed by VM code.
2222   if (!bcp_and_mdx_only) {
2223     // Following ones are Metadata.
2224     ld(R19_method, _ijava_state_neg(method), scratch);
2225     ld(R27_constPoolCache, _ijava_state_neg(cpoolCache), scratch);
2226     // Following ones are stack addresses and don't require reload.
2227     ld(R15_esp, _ijava_state_neg(esp), scratch);
2228     ld(R18_locals, _ijava_state_neg(locals), scratch);
2229     sldi(R18_locals, R18_locals, Interpreter::logStackElementSize);
2230     add(R18_locals, R18_locals, scratch);
2231     ld(R26_monitor, _ijava_state_neg(monitors), scratch);
2232   }
2233 #ifdef ASSERT
2234   {
2235     Label Lok;
2236     subf(R0, R1_SP, scratch);
2237     cmpdi(CCR0, R0, frame::top_ijava_frame_abi_size + frame::ijava_state_size);
2238     bge(CCR0, Lok);
2239     stop("frame too small (restore istate)");
2240     bind(Lok);
2241   }
2242 #endif
2243 }
2244 
2245 void InterpreterMacroAssembler::get_method_counters(Register method,
2246                                                     Register Rcounters,
2247                                                     Label& skip) {
2248   BLOCK_COMMENT("Load and ev. allocate counter object {");
2249   Label has_counters;
2250   ld(Rcounters, in_bytes(Method::method_counters_offset()), method);
2251   cmpdi(CCR0, Rcounters, 0);
2252   bne(CCR0, has_counters);
2253   call_VM(noreg, CAST_FROM_FN_PTR(address,
2254                                   InterpreterRuntime::build_method_counters), method);
2255   ld(Rcounters, in_bytes(Method::method_counters_offset()), method);
2256   cmpdi(CCR0, Rcounters, 0);
2257   beq(CCR0, skip); // No MethodCounters, OutOfMemory.
2258   BLOCK_COMMENT("} Load and ev. allocate counter object");
2259 
2260   bind(has_counters);
2261 }
2262 
2263 void InterpreterMacroAssembler::increment_invocation_counter(Register Rcounters,
2264                                                              Register iv_be_count,
2265                                                              Register Rtmp_r0) {
2266   assert(UseCompiler, "incrementing must be useful");
2267   Register invocation_count = iv_be_count;
2268   Register backedge_count   = Rtmp_r0;
2269   int delta = InvocationCounter::count_increment;
2270 
2271   // Load each counter in a register.
2272   //  ld(inv_counter, Rtmp);
2273   //  ld(be_counter, Rtmp2);
2274   int inv_counter_offset = in_bytes(MethodCounters::invocation_counter_offset() +
2275                                     InvocationCounter::counter_offset());
2276   int be_counter_offset  = in_bytes(MethodCounters::backedge_counter_offset() +
2277                                     InvocationCounter::counter_offset());
2278 
2279   BLOCK_COMMENT("Increment profiling counters {");
2280 
2281   // Load the backedge counter.
2282   lwz(backedge_count, be_counter_offset, Rcounters); // is unsigned int
2283   // Mask the backedge counter.
2284   andi(backedge_count, backedge_count, InvocationCounter::count_mask_value);
2285 
2286   // Load the invocation counter.
2287   lwz(invocation_count, inv_counter_offset, Rcounters); // is unsigned int
2288   // Add the delta to the invocation counter and store the result.
2289   addi(invocation_count, invocation_count, delta);
2290   // Store value.
2291   stw(invocation_count, inv_counter_offset, Rcounters);
2292 
2293   // Add invocation counter + backedge counter.
2294   add(iv_be_count, backedge_count, invocation_count);
2295 
2296   // Note that this macro must leave the backedge_count + invocation_count in
2297   // register iv_be_count!
2298   BLOCK_COMMENT("} Increment profiling counters");
2299 }
2300 
2301 void InterpreterMacroAssembler::verify_oop(Register reg, TosState state) {
2302   if (state == atos) { MacroAssembler::verify_oop(reg, FILE_AND_LINE); }
2303 }
2304 
2305 // Local helper function for the verify_oop_or_return_address macro.
2306 static bool verify_return_address(Method* m, int bci) {
2307 #ifndef PRODUCT
2308   address pc = (address)(m->constMethod()) + in_bytes(ConstMethod::codes_offset()) + bci;
2309   // Assume it is a valid return address if it is inside m and is preceded by a jsr.
2310   if (!m->contains(pc))                                            return false;
2311   address jsr_pc;
2312   jsr_pc = pc - Bytecodes::length_for(Bytecodes::_jsr);
2313   if (*jsr_pc == Bytecodes::_jsr   && jsr_pc >= m->code_base())    return true;
2314   jsr_pc = pc - Bytecodes::length_for(Bytecodes::_jsr_w);
2315   if (*jsr_pc == Bytecodes::_jsr_w && jsr_pc >= m->code_base())    return true;
2316 #endif // PRODUCT
2317   return false;
2318 }
2319 
2320 void InterpreterMacroAssembler::verify_FPU(int stack_depth, TosState state) {
2321   if (VerifyFPU) {
2322     unimplemented("verfiyFPU");
2323   }
2324 }
2325 
2326 void InterpreterMacroAssembler::verify_oop_or_return_address(Register reg, Register Rtmp) {
2327   if (!VerifyOops) return;
2328 
2329   // The VM documentation for the astore[_wide] bytecode allows
2330   // the TOS to be not only an oop but also a return address.
2331   Label test;
2332   Label skip;
2333   // See if it is an address (in the current method):
2334 
2335   const int log2_bytecode_size_limit = 16;
2336   srdi_(Rtmp, reg, log2_bytecode_size_limit);
2337   bne(CCR0, test);
2338 
2339   address fd = CAST_FROM_FN_PTR(address, verify_return_address);
2340   const int nbytes_save = MacroAssembler::num_volatile_regs * 8;
2341   save_volatile_gprs(R1_SP, -nbytes_save); // except R0
2342   save_LR_CR(Rtmp); // Save in old frame.
2343   push_frame_reg_args(nbytes_save, Rtmp);
2344 
2345   load_const_optimized(Rtmp, fd, R0);
2346   mr_if_needed(R4_ARG2, reg);
2347   mr(R3_ARG1, R19_method);
2348   call_c(Rtmp); // call C
2349 
2350   pop_frame();
2351   restore_LR_CR(Rtmp);
2352   restore_volatile_gprs(R1_SP, -nbytes_save); // except R0
2353   b(skip);
2354 
2355   // Perform a more elaborate out-of-line call.
2356   // Not an address; verify it:
2357   bind(test);
2358   verify_oop(reg);
2359   bind(skip);
2360 }
2361 
2362 // Inline assembly for:
2363 //
2364 // if (thread is in interp_only_mode) {
2365 //   InterpreterRuntime::post_method_entry();
2366 // }
2367 // if (*jvmpi::event_flags_array_at_addr(JVMPI_EVENT_METHOD_ENTRY ) ||
2368 //     *jvmpi::event_flags_array_at_addr(JVMPI_EVENT_METHOD_ENTRY2)   ) {
2369 //   SharedRuntime::jvmpi_method_entry(method, receiver);
2370 // }
2371 void InterpreterMacroAssembler::notify_method_entry() {
2372   // JVMTI
2373   // Whenever JVMTI puts a thread in interp_only_mode, method
2374   // entry/exit events are sent for that thread to track stack
2375   // depth. If it is possible to enter interp_only_mode we add
2376   // the code to check if the event should be sent.
2377   if (JvmtiExport::can_post_interpreter_events()) {
2378     Label jvmti_post_done;
2379 
2380     lwz(R0, in_bytes(JavaThread::interp_only_mode_offset()), R16_thread);
2381     cmpwi(CCR0, R0, 0);
2382     beq(CCR0, jvmti_post_done);
2383     call_VM(noreg, CAST_FROM_FN_PTR(address, InterpreterRuntime::post_method_entry));
2384 
2385     bind(jvmti_post_done);
2386   }
2387 }
2388 
2389 // Inline assembly for:
2390 //
2391 // if (thread is in interp_only_mode) {
2392 //   // save result
2393 //   InterpreterRuntime::post_method_exit();
2394 //   // restore result
2395 // }
2396 // if (*jvmpi::event_flags_array_at_addr(JVMPI_EVENT_METHOD_EXIT)) {
2397 //   // save result
2398 //   SharedRuntime::jvmpi_method_exit();
2399 //   // restore result
2400 // }
2401 //
2402 // Native methods have their result stored in d_tmp and l_tmp.
2403 // Java methods have their result stored in the expression stack.
2404 void InterpreterMacroAssembler::notify_method_exit(bool is_native_method, TosState state,
2405                                                    NotifyMethodExitMode mode, bool check_exceptions) {
2406   // JVMTI
2407   // Whenever JVMTI puts a thread in interp_only_mode, method
2408   // entry/exit events are sent for that thread to track stack
2409   // depth. If it is possible to enter interp_only_mode we add
2410   // the code to check if the event should be sent.
2411   if (mode == NotifyJVMTI && JvmtiExport::can_post_interpreter_events()) {
2412     Label jvmti_post_done;
2413 
2414     lwz(R0, in_bytes(JavaThread::interp_only_mode_offset()), R16_thread);
2415     cmpwi(CCR0, R0, 0);
2416     beq(CCR0, jvmti_post_done);
2417     if (!is_native_method) { push(state); } // Expose tos to GC.
2418     call_VM(noreg, CAST_FROM_FN_PTR(address, InterpreterRuntime::post_method_exit), check_exceptions);
2419     if (!is_native_method) { pop(state); }
2420 
2421     align(32, 12);
2422     bind(jvmti_post_done);
2423   }
2424 
2425   // Dtrace support not implemented.
2426 }