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