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