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