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