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