rev 55858 : 8228649: [PPC64] SA reads wrong slots from interpreter frames
Summary: Make frame layout consistent between dbg and product build and implement offsets accordingly.
Reviewed-by: goetz, gromero

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