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