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