1 /*
   2  * Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved.
   3  * Copyright 2013, 2015 SAP AG. 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 #include "precompiled.hpp"
  27 #include "asm/macroAssembler.inline.hpp"
  28 #include "interpreter/interpreter.hpp"
  29 #include "interpreter/interpreterRuntime.hpp"
  30 #include "interpreter/interp_masm.hpp"
  31 #include "interpreter/templateInterpreter.hpp"
  32 #include "interpreter/templateTable.hpp"
  33 #include "memory/universe.inline.hpp"
  34 #include "oops/objArrayKlass.hpp"
  35 #include "oops/oop.inline.hpp"
  36 #include "prims/methodHandles.hpp"
  37 #include "runtime/sharedRuntime.hpp"
  38 #include "runtime/stubRoutines.hpp"
  39 #include "runtime/synchronizer.hpp"
  40 #include "utilities/macros.hpp"
  41 
  42 #ifndef CC_INTERP
  43 
  44 #undef __
  45 #define __ _masm->
  46 
  47 // ============================================================================
  48 // Misc helpers
  49 
  50 // Do an oop store like *(base + index) = val OR *(base + offset) = val
  51 // (only one of both variants is possible at the same time).
  52 // Index can be noreg.
  53 // Kills:
  54 //   Rbase, Rtmp
  55 static void do_oop_store(InterpreterMacroAssembler* _masm,
  56                          Register           Rbase,
  57                          RegisterOrConstant offset,
  58                          Register           Rval,         // Noreg means always null.
  59                          Register           Rtmp1,
  60                          Register           Rtmp2,
  61                          Register           Rtmp3,
  62                          BarrierSet::Name   barrier,
  63                          bool               precise,
  64                          bool               check_null) {
  65   assert_different_registers(Rtmp1, Rtmp2, Rtmp3, Rval, Rbase);
  66 
  67   switch (barrier) {
  68 #if INCLUDE_ALL_GCS
  69     case BarrierSet::G1SATBCT:
  70     case BarrierSet::G1SATBCTLogging:
  71       {
  72         // Load and record the previous value.
  73         __ g1_write_barrier_pre(Rbase, offset,
  74                                 Rtmp3, /* holder of pre_val ? */
  75                                 Rtmp1, Rtmp2, false /* frame */);
  76 
  77         Label Lnull, Ldone;
  78         if (Rval != noreg) {
  79           if (check_null) {
  80             __ cmpdi(CCR0, Rval, 0);
  81             __ beq(CCR0, Lnull);
  82           }
  83           __ store_heap_oop_not_null(Rval, offset, Rbase, /*Rval must stay uncompressed.*/ Rtmp1);
  84           // Mark the card.
  85           if (!(offset.is_constant() && offset.as_constant() == 0) && precise) {
  86             __ add(Rbase, offset, Rbase);
  87           }
  88           __ g1_write_barrier_post(Rbase, Rval, Rtmp1, Rtmp2, Rtmp3, /*filtered (fast path)*/ &Ldone);
  89           if (check_null) { __ b(Ldone); }
  90         }
  91 
  92         if (Rval == noreg || check_null) { // Store null oop.
  93           Register Rnull = Rval;
  94           __ bind(Lnull);
  95           if (Rval == noreg) {
  96             Rnull = Rtmp1;
  97             __ li(Rnull, 0);
  98           }
  99           if (UseCompressedOops) {
 100             __ stw(Rnull, offset, Rbase);
 101           } else {
 102             __ std(Rnull, offset, Rbase);
 103           }
 104         }
 105         __ bind(Ldone);
 106       }
 107       break;
 108 #endif // INCLUDE_ALL_GCS
 109     case BarrierSet::CardTableModRef:
 110     case BarrierSet::CardTableExtension:
 111       {
 112         Label Lnull, Ldone;
 113         if (Rval != noreg) {
 114           if (check_null) {
 115             __ cmpdi(CCR0, Rval, 0);
 116             __ beq(CCR0, Lnull);
 117           }
 118           __ store_heap_oop_not_null(Rval, offset, Rbase, /*Rval should better stay uncompressed.*/ Rtmp1);
 119           // Mark the card.
 120           if (!(offset.is_constant() && offset.as_constant() == 0) && precise) {
 121             __ add(Rbase, offset, Rbase);
 122           }
 123           __ card_write_barrier_post(Rbase, Rval, Rtmp1);
 124           if (check_null) {
 125             __ b(Ldone);
 126           }
 127         }
 128 
 129         if (Rval == noreg || check_null) { // Store null oop.
 130           Register Rnull = Rval;
 131           __ bind(Lnull);
 132           if (Rval == noreg) {
 133             Rnull = Rtmp1;
 134             __ li(Rnull, 0);
 135           }
 136           if (UseCompressedOops) {
 137             __ stw(Rnull, offset, Rbase);
 138           } else {
 139             __ std(Rnull, offset, Rbase);
 140           }
 141         }
 142         __ bind(Ldone);
 143       }
 144       break;
 145     case BarrierSet::ModRef:
 146       ShouldNotReachHere();
 147       break;
 148     default:
 149       ShouldNotReachHere();
 150   }
 151 }
 152 
 153 // ============================================================================
 154 // Platform-dependent initialization
 155 
 156 void TemplateTable::pd_initialize() {
 157   // No ppc64 specific initialization.
 158 }
 159 
 160 Address TemplateTable::at_bcp(int offset) {
 161   // Not used on ppc.
 162   ShouldNotReachHere();
 163   return Address();
 164 }
 165 
 166 // Patches the current bytecode (ptr to it located in bcp)
 167 // in the bytecode stream with a new one.
 168 void TemplateTable::patch_bytecode(Bytecodes::Code new_bc, Register Rnew_bc, Register Rtemp, bool load_bc_into_bc_reg /*=true*/, int byte_no) {
 169   // With sharing on, may need to test method flag.
 170   if (!RewriteBytecodes) return;
 171   Label L_patch_done;
 172 
 173   switch (new_bc) {
 174     case Bytecodes::_fast_aputfield:
 175     case Bytecodes::_fast_bputfield:
 176     case Bytecodes::_fast_cputfield:
 177     case Bytecodes::_fast_dputfield:
 178     case Bytecodes::_fast_fputfield:
 179     case Bytecodes::_fast_iputfield:
 180     case Bytecodes::_fast_lputfield:
 181     case Bytecodes::_fast_sputfield:
 182     {
 183       // We skip bytecode quickening for putfield instructions when
 184       // the put_code written to the constant pool cache is zero.
 185       // This is required so that every execution of this instruction
 186       // calls out to InterpreterRuntime::resolve_get_put to do
 187       // additional, required work.
 188       assert(byte_no == f1_byte || byte_no == f2_byte, "byte_no out of range");
 189       assert(load_bc_into_bc_reg, "we use bc_reg as temp");
 190       __ get_cache_and_index_at_bcp(Rtemp /* dst = cache */, 1);
 191       // ((*(cache+indices))>>((1+byte_no)*8))&0xFF:
 192 #if defined(VM_LITTLE_ENDIAN)
 193       __ lbz(Rnew_bc, in_bytes(ConstantPoolCache::base_offset() + ConstantPoolCacheEntry::indices_offset()) + 1 + byte_no, Rtemp);
 194 #else
 195       __ lbz(Rnew_bc, in_bytes(ConstantPoolCache::base_offset() + ConstantPoolCacheEntry::indices_offset()) + 7 - (1 + byte_no), Rtemp);
 196 #endif
 197       __ cmpwi(CCR0, Rnew_bc, 0);
 198       __ li(Rnew_bc, (unsigned int)(unsigned char)new_bc);
 199       __ beq(CCR0, L_patch_done);
 200       // __ isync(); // acquire not needed
 201       break;
 202     }
 203 
 204     default:
 205       assert(byte_no == -1, "sanity");
 206       if (load_bc_into_bc_reg) {
 207         __ li(Rnew_bc, (unsigned int)(unsigned char)new_bc);
 208       }
 209   }
 210 
 211   if (JvmtiExport::can_post_breakpoint()) {
 212     Label L_fast_patch;
 213     __ lbz(Rtemp, 0, R14_bcp);
 214     __ cmpwi(CCR0, Rtemp, (unsigned int)(unsigned char)Bytecodes::_breakpoint);
 215     __ bne(CCR0, L_fast_patch);
 216     // Perform the quickening, slowly, in the bowels of the breakpoint table.
 217     __ call_VM(noreg, CAST_FROM_FN_PTR(address, InterpreterRuntime::set_original_bytecode_at), R19_method, R14_bcp, Rnew_bc);
 218     __ b(L_patch_done);
 219     __ bind(L_fast_patch);
 220   }
 221 
 222   // Patch bytecode.
 223   __ stb(Rnew_bc, 0, R14_bcp);
 224 
 225   __ bind(L_patch_done);
 226 }
 227 
 228 // ============================================================================
 229 // Individual instructions
 230 
 231 void TemplateTable::nop() {
 232   transition(vtos, vtos);
 233   // Nothing to do.
 234 }
 235 
 236 void TemplateTable::shouldnotreachhere() {
 237   transition(vtos, vtos);
 238   __ stop("shouldnotreachhere bytecode");
 239 }
 240 
 241 void TemplateTable::aconst_null() {
 242   transition(vtos, atos);
 243   __ li(R17_tos, 0);
 244 }
 245 
 246 void TemplateTable::iconst(int value) {
 247   transition(vtos, itos);
 248   assert(value >= -1 && value <= 5, "");
 249   __ li(R17_tos, value);
 250 }
 251 
 252 void TemplateTable::lconst(int value) {
 253   transition(vtos, ltos);
 254   assert(value >= -1 && value <= 5, "");
 255   __ li(R17_tos, value);
 256 }
 257 
 258 void TemplateTable::fconst(int value) {
 259   transition(vtos, ftos);
 260   static float zero = 0.0;
 261   static float one  = 1.0;
 262   static float two  = 2.0;
 263   switch (value) {
 264     default: ShouldNotReachHere();
 265     case 0: {
 266       int simm16_offset = __ load_const_optimized(R11_scratch1, (address*)&zero, R0, true);
 267       __ lfs(F15_ftos, simm16_offset, R11_scratch1);
 268       break;
 269     }
 270     case 1: {
 271       int simm16_offset = __ load_const_optimized(R11_scratch1, (address*)&one, R0, true);
 272       __ lfs(F15_ftos, simm16_offset, R11_scratch1);
 273       break;
 274     }
 275     case 2: {
 276       int simm16_offset = __ load_const_optimized(R11_scratch1, (address*)&two, R0, true);
 277       __ lfs(F15_ftos, simm16_offset, R11_scratch1);
 278       break;
 279     }
 280   }
 281 }
 282 
 283 void TemplateTable::dconst(int value) {
 284   transition(vtos, dtos);
 285   static double zero = 0.0;
 286   static double one  = 1.0;
 287   switch (value) {
 288     case 0: {
 289       int simm16_offset = __ load_const_optimized(R11_scratch1, (address*)&zero, R0, true);
 290       __ lfd(F15_ftos, simm16_offset, R11_scratch1);
 291       break;
 292     }
 293     case 1: {
 294       int simm16_offset = __ load_const_optimized(R11_scratch1, (address*)&one, R0, true);
 295       __ lfd(F15_ftos, simm16_offset, R11_scratch1);
 296       break;
 297     }
 298     default: ShouldNotReachHere();
 299   }
 300 }
 301 
 302 void TemplateTable::bipush() {
 303   transition(vtos, itos);
 304   __ lbz(R17_tos, 1, R14_bcp);
 305   __ extsb(R17_tos, R17_tos);
 306 }
 307 
 308 void TemplateTable::sipush() {
 309   transition(vtos, itos);
 310   __ get_2_byte_integer_at_bcp(1, R17_tos, InterpreterMacroAssembler::Signed);
 311 }
 312 
 313 void TemplateTable::ldc(bool wide) {
 314   Register Rscratch1 = R11_scratch1,
 315            Rscratch2 = R12_scratch2,
 316            Rcpool    = R3_ARG1;
 317 
 318   transition(vtos, vtos);
 319   Label notInt, notClass, exit;
 320 
 321   __ get_cpool_and_tags(Rcpool, Rscratch2); // Set Rscratch2 = &tags.
 322   if (wide) { // Read index.
 323     __ get_2_byte_integer_at_bcp(1, Rscratch1, InterpreterMacroAssembler::Unsigned);
 324   } else {
 325     __ lbz(Rscratch1, 1, R14_bcp);
 326   }
 327 
 328   const int base_offset = ConstantPool::header_size() * wordSize;
 329   const int tags_offset = Array<u1>::base_offset_in_bytes();
 330 
 331   // Get type from tags.
 332   __ addi(Rscratch2, Rscratch2, tags_offset);
 333   __ lbzx(Rscratch2, Rscratch2, Rscratch1);
 334 
 335   __ cmpwi(CCR0, Rscratch2, JVM_CONSTANT_UnresolvedClass); // Unresolved class?
 336   __ cmpwi(CCR1, Rscratch2, JVM_CONSTANT_UnresolvedClassInError); // Unresolved class in error state?
 337   __ cror(CCR0, Assembler::equal, CCR1, Assembler::equal);
 338 
 339   // Resolved class - need to call vm to get java mirror of the class.
 340   __ cmpwi(CCR1, Rscratch2, JVM_CONSTANT_Class);
 341   __ crnor(CCR0, Assembler::equal, CCR1, Assembler::equal); // Neither resolved class nor unresolved case from above?
 342   __ beq(CCR0, notClass);
 343 
 344   __ li(R4, wide ? 1 : 0);
 345   call_VM(R17_tos, CAST_FROM_FN_PTR(address, InterpreterRuntime::ldc), R4);
 346   __ push(atos);
 347   __ b(exit);
 348 
 349   __ align(32, 12);
 350   __ bind(notClass);
 351   __ addi(Rcpool, Rcpool, base_offset);
 352   __ sldi(Rscratch1, Rscratch1, LogBytesPerWord);
 353   __ cmpdi(CCR0, Rscratch2, JVM_CONSTANT_Integer);
 354   __ bne(CCR0, notInt);
 355   __ lwax(R17_tos, Rcpool, Rscratch1);
 356   __ push(itos);
 357   __ b(exit);
 358 
 359   __ align(32, 12);
 360   __ bind(notInt);
 361 #ifdef ASSERT
 362   // String and Object are rewritten to fast_aldc
 363   __ cmpdi(CCR0, Rscratch2, JVM_CONSTANT_Float);
 364   __ asm_assert_eq("unexpected type", 0x8765);
 365 #endif
 366   __ lfsx(F15_ftos, Rcpool, Rscratch1);
 367   __ push(ftos);
 368 
 369   __ align(32, 12);
 370   __ bind(exit);
 371 }
 372 
 373 // Fast path for caching oop constants.
 374 void TemplateTable::fast_aldc(bool wide) {
 375   transition(vtos, atos);
 376 
 377   int index_size = wide ? sizeof(u2) : sizeof(u1);
 378   const Register Rscratch = R11_scratch1;
 379   Label resolved;
 380 
 381   // We are resolved if the resolved reference cache entry contains a
 382   // non-null object (CallSite, etc.)
 383   __ get_cache_index_at_bcp(Rscratch, 1, index_size);  // Load index.
 384   __ load_resolved_reference_at_index(R17_tos, Rscratch);
 385   __ cmpdi(CCR0, R17_tos, 0);
 386   __ bne(CCR0, resolved);
 387   __ load_const_optimized(R3_ARG1, (int)bytecode());
 388 
 389   address entry = CAST_FROM_FN_PTR(address, InterpreterRuntime::resolve_ldc);
 390 
 391   // First time invocation - must resolve first.
 392   __ call_VM(R17_tos, entry, R3_ARG1);
 393 
 394   __ align(32, 12);
 395   __ bind(resolved);
 396   __ verify_oop(R17_tos);
 397 }
 398 
 399 void TemplateTable::ldc2_w() {
 400   transition(vtos, vtos);
 401   Label Llong, Lexit;
 402 
 403   Register Rindex = R11_scratch1,
 404            Rcpool = R12_scratch2,
 405            Rtag   = R3_ARG1;
 406   __ get_cpool_and_tags(Rcpool, Rtag);
 407   __ get_2_byte_integer_at_bcp(1, Rindex, InterpreterMacroAssembler::Unsigned);
 408 
 409   const int base_offset = ConstantPool::header_size() * wordSize;
 410   const int tags_offset = Array<u1>::base_offset_in_bytes();
 411   // Get type from tags.
 412   __ addi(Rcpool, Rcpool, base_offset);
 413   __ addi(Rtag, Rtag, tags_offset);
 414 
 415   __ lbzx(Rtag, Rtag, Rindex);
 416 
 417   __ sldi(Rindex, Rindex, LogBytesPerWord);
 418   __ cmpdi(CCR0, Rtag, JVM_CONSTANT_Double);
 419   __ bne(CCR0, Llong);
 420   // A double can be placed at word-aligned locations in the constant pool.
 421   // Check out Conversions.java for an example.
 422   // Also ConstantPool::header_size() is 20, which makes it very difficult
 423   // to double-align double on the constant pool. SG, 11/7/97
 424   __ lfdx(F15_ftos, Rcpool, Rindex);
 425   __ push(dtos);
 426   __ b(Lexit);
 427 
 428   __ bind(Llong);
 429   __ ldx(R17_tos, Rcpool, Rindex);
 430   __ push(ltos);
 431 
 432   __ bind(Lexit);
 433 }
 434 
 435 // Get the locals index located in the bytecode stream at bcp + offset.
 436 void TemplateTable::locals_index(Register Rdst, int offset) {
 437   __ lbz(Rdst, offset, R14_bcp);
 438 }
 439 
 440 void TemplateTable::iload() {
 441   iload_internal();
 442 }
 443 
 444 void TemplateTable::nofast_iload() {
 445   iload_internal(may_not_rewrite);
 446 }
 447 
 448 void TemplateTable::iload_internal(RewriteControl rc) {
 449   transition(vtos, itos);
 450 
 451   // Get the local value into tos
 452   const Register Rindex = R22_tmp2;
 453   locals_index(Rindex);
 454 
 455   // Rewrite iload,iload  pair into fast_iload2
 456   //         iload,caload pair into fast_icaload
 457   if (RewriteFrequentPairs && rc == may_rewrite) {
 458     Label Lrewrite, Ldone;
 459     Register Rnext_byte  = R3_ARG1,
 460              Rrewrite_to = R6_ARG4,
 461              Rscratch    = R11_scratch1;
 462 
 463     // get next byte
 464     __ lbz(Rnext_byte, Bytecodes::length_for(Bytecodes::_iload), R14_bcp);
 465 
 466     // if _iload, wait to rewrite to iload2. We only want to rewrite the
 467     // last two iloads in a pair. Comparing against fast_iload means that
 468     // the next bytecode is neither an iload or a caload, and therefore
 469     // an iload pair.
 470     __ cmpwi(CCR0, Rnext_byte, (unsigned int)(unsigned char)Bytecodes::_iload);
 471     __ beq(CCR0, Ldone);
 472 
 473     __ cmpwi(CCR1, Rnext_byte, (unsigned int)(unsigned char)Bytecodes::_fast_iload);
 474     __ li(Rrewrite_to, (unsigned int)(unsigned char)Bytecodes::_fast_iload2);
 475     __ beq(CCR1, Lrewrite);
 476 
 477     __ cmpwi(CCR0, Rnext_byte, (unsigned int)(unsigned char)Bytecodes::_caload);
 478     __ li(Rrewrite_to, (unsigned int)(unsigned char)Bytecodes::_fast_icaload);
 479     __ beq(CCR0, Lrewrite);
 480 
 481     __ li(Rrewrite_to, (unsigned int)(unsigned char)Bytecodes::_fast_iload);
 482 
 483     __ bind(Lrewrite);
 484     patch_bytecode(Bytecodes::_iload, Rrewrite_to, Rscratch, false);
 485     __ bind(Ldone);
 486   }
 487 
 488   __ load_local_int(R17_tos, Rindex, Rindex);
 489 }
 490 
 491 // Load 2 integers in a row without dispatching
 492 void TemplateTable::fast_iload2() {
 493   transition(vtos, itos);
 494 
 495   __ lbz(R3_ARG1, 1, R14_bcp);
 496   __ lbz(R17_tos, Bytecodes::length_for(Bytecodes::_iload) + 1, R14_bcp);
 497 
 498   __ load_local_int(R3_ARG1, R11_scratch1, R3_ARG1);
 499   __ load_local_int(R17_tos, R12_scratch2, R17_tos);
 500   __ push_i(R3_ARG1);
 501 }
 502 
 503 void TemplateTable::fast_iload() {
 504   transition(vtos, itos);
 505   // Get the local value into tos
 506 
 507   const Register Rindex = R11_scratch1;
 508   locals_index(Rindex);
 509   __ load_local_int(R17_tos, Rindex, Rindex);
 510 }
 511 
 512 // Load a local variable type long from locals area to TOS cache register.
 513 // Local index resides in bytecodestream.
 514 void TemplateTable::lload() {
 515   transition(vtos, ltos);
 516 
 517   const Register Rindex = R11_scratch1;
 518   locals_index(Rindex);
 519   __ load_local_long(R17_tos, Rindex, Rindex);
 520 }
 521 
 522 void TemplateTable::fload() {
 523   transition(vtos, ftos);
 524 
 525   const Register Rindex = R11_scratch1;
 526   locals_index(Rindex);
 527   __ load_local_float(F15_ftos, Rindex, Rindex);
 528 }
 529 
 530 void TemplateTable::dload() {
 531   transition(vtos, dtos);
 532 
 533   const Register Rindex = R11_scratch1;
 534   locals_index(Rindex);
 535   __ load_local_double(F15_ftos, Rindex, Rindex);
 536 }
 537 
 538 void TemplateTable::aload() {
 539   transition(vtos, atos);
 540 
 541   const Register Rindex = R11_scratch1;
 542   locals_index(Rindex);
 543   __ load_local_ptr(R17_tos, Rindex, Rindex);
 544 }
 545 
 546 void TemplateTable::locals_index_wide(Register Rdst) {
 547   // Offset is 2, not 1, because Lbcp points to wide prefix code.
 548   __ get_2_byte_integer_at_bcp(2, Rdst, InterpreterMacroAssembler::Unsigned);
 549 }
 550 
 551 void TemplateTable::wide_iload() {
 552   // Get the local value into tos.
 553 
 554   const Register Rindex = R11_scratch1;
 555   locals_index_wide(Rindex);
 556   __ load_local_int(R17_tos, Rindex, Rindex);
 557 }
 558 
 559 void TemplateTable::wide_lload() {
 560   transition(vtos, ltos);
 561 
 562   const Register Rindex = R11_scratch1;
 563   locals_index_wide(Rindex);
 564   __ load_local_long(R17_tos, Rindex, Rindex);
 565 }
 566 
 567 void TemplateTable::wide_fload() {
 568   transition(vtos, ftos);
 569 
 570   const Register Rindex = R11_scratch1;
 571   locals_index_wide(Rindex);
 572   __ load_local_float(F15_ftos, Rindex, Rindex);
 573 }
 574 
 575 void TemplateTable::wide_dload() {
 576   transition(vtos, dtos);
 577 
 578   const Register Rindex = R11_scratch1;
 579   locals_index_wide(Rindex);
 580   __ load_local_double(F15_ftos, Rindex, Rindex);
 581 }
 582 
 583 void TemplateTable::wide_aload() {
 584   transition(vtos, atos);
 585 
 586   const Register Rindex = R11_scratch1;
 587   locals_index_wide(Rindex);
 588   __ load_local_ptr(R17_tos, Rindex, Rindex);
 589 }
 590 
 591 void TemplateTable::iaload() {
 592   transition(itos, itos);
 593 
 594   const Register Rload_addr = R3_ARG1,
 595                  Rarray     = R4_ARG2,
 596                  Rtemp      = R5_ARG3;
 597   __ index_check(Rarray, R17_tos /* index */, LogBytesPerInt, Rtemp, Rload_addr);
 598   __ lwa(R17_tos, arrayOopDesc::base_offset_in_bytes(T_INT), Rload_addr);
 599 }
 600 
 601 void TemplateTable::laload() {
 602   transition(itos, ltos);
 603 
 604   const Register Rload_addr = R3_ARG1,
 605                  Rarray     = R4_ARG2,
 606                  Rtemp      = R5_ARG3;
 607   __ index_check(Rarray, R17_tos /* index */, LogBytesPerLong, Rtemp, Rload_addr);
 608   __ ld(R17_tos, arrayOopDesc::base_offset_in_bytes(T_LONG), Rload_addr);
 609 }
 610 
 611 void TemplateTable::faload() {
 612   transition(itos, ftos);
 613 
 614   const Register Rload_addr = R3_ARG1,
 615                  Rarray     = R4_ARG2,
 616                  Rtemp      = R5_ARG3;
 617   __ index_check(Rarray, R17_tos /* index */, LogBytesPerInt, Rtemp, Rload_addr);
 618   __ lfs(F15_ftos, arrayOopDesc::base_offset_in_bytes(T_FLOAT), Rload_addr);
 619 }
 620 
 621 void TemplateTable::daload() {
 622   transition(itos, dtos);
 623 
 624   const Register Rload_addr = R3_ARG1,
 625                  Rarray     = R4_ARG2,
 626                  Rtemp      = R5_ARG3;
 627   __ index_check(Rarray, R17_tos /* index */, LogBytesPerLong, Rtemp, Rload_addr);
 628   __ lfd(F15_ftos, arrayOopDesc::base_offset_in_bytes(T_DOUBLE), Rload_addr);
 629 }
 630 
 631 void TemplateTable::aaload() {
 632   transition(itos, atos);
 633 
 634   // tos: index
 635   // result tos: array
 636   const Register Rload_addr = R3_ARG1,
 637                  Rarray     = R4_ARG2,
 638                  Rtemp      = R5_ARG3;
 639   __ index_check(Rarray, R17_tos /* index */, UseCompressedOops ? 2 : LogBytesPerWord, Rtemp, Rload_addr);
 640   __ load_heap_oop(R17_tos, arrayOopDesc::base_offset_in_bytes(T_OBJECT), Rload_addr);
 641   __ verify_oop(R17_tos);
 642   //__ dcbt(R17_tos); // prefetch
 643 }
 644 
 645 void TemplateTable::baload() {
 646   transition(itos, itos);
 647 
 648   const Register Rload_addr = R3_ARG1,
 649                  Rarray     = R4_ARG2,
 650                  Rtemp      = R5_ARG3;
 651   __ index_check(Rarray, R17_tos /* index */, 0, Rtemp, Rload_addr);
 652   __ lbz(R17_tos, arrayOopDesc::base_offset_in_bytes(T_BYTE), Rload_addr);
 653   __ extsb(R17_tos, R17_tos);
 654 }
 655 
 656 void TemplateTable::caload() {
 657   transition(itos, itos);
 658 
 659   const Register Rload_addr = R3_ARG1,
 660                  Rarray     = R4_ARG2,
 661                  Rtemp      = R5_ARG3;
 662   __ index_check(Rarray, R17_tos /* index */, LogBytesPerShort, Rtemp, Rload_addr);
 663   __ lhz(R17_tos, arrayOopDesc::base_offset_in_bytes(T_CHAR), Rload_addr);
 664 }
 665 
 666 // Iload followed by caload frequent pair.
 667 void TemplateTable::fast_icaload() {
 668   transition(vtos, itos);
 669 
 670   const Register Rload_addr = R3_ARG1,
 671                  Rarray     = R4_ARG2,
 672                  Rtemp      = R11_scratch1;
 673 
 674   locals_index(R17_tos);
 675   __ load_local_int(R17_tos, Rtemp, R17_tos);
 676   __ index_check(Rarray, R17_tos /* index */, LogBytesPerShort, Rtemp, Rload_addr);
 677   __ lhz(R17_tos, arrayOopDesc::base_offset_in_bytes(T_CHAR), Rload_addr);
 678 }
 679 
 680 void TemplateTable::saload() {
 681   transition(itos, itos);
 682 
 683   const Register Rload_addr = R11_scratch1,
 684                  Rarray     = R12_scratch2,
 685                  Rtemp      = R3_ARG1;
 686   __ index_check(Rarray, R17_tos /* index */, LogBytesPerShort, Rtemp, Rload_addr);
 687   __ lha(R17_tos, arrayOopDesc::base_offset_in_bytes(T_SHORT), Rload_addr);
 688 }
 689 
 690 void TemplateTable::iload(int n) {
 691   transition(vtos, itos);
 692 
 693   __ lwz(R17_tos, Interpreter::local_offset_in_bytes(n), R18_locals);
 694 }
 695 
 696 void TemplateTable::lload(int n) {
 697   transition(vtos, ltos);
 698 
 699   __ ld(R17_tos, Interpreter::local_offset_in_bytes(n + 1), R18_locals);
 700 }
 701 
 702 void TemplateTable::fload(int n) {
 703   transition(vtos, ftos);
 704 
 705   __ lfs(F15_ftos, Interpreter::local_offset_in_bytes(n), R18_locals);
 706 }
 707 
 708 void TemplateTable::dload(int n) {
 709   transition(vtos, dtos);
 710 
 711   __ lfd(F15_ftos, Interpreter::local_offset_in_bytes(n + 1), R18_locals);
 712 }
 713 
 714 void TemplateTable::aload(int n) {
 715   transition(vtos, atos);
 716 
 717   __ ld(R17_tos, Interpreter::local_offset_in_bytes(n), R18_locals);
 718 }
 719 
 720 void TemplateTable::aload_0() {
 721   aload_0_internal();
 722 }
 723 
 724 void TemplateTable::nofast_aload_0() {
 725   aload_0_internal(may_not_rewrite);
 726 }
 727 
 728 void TemplateTable::aload_0_internal(RewriteControl rc) {
 729   transition(vtos, atos);
 730   // According to bytecode histograms, the pairs:
 731   //
 732   // _aload_0, _fast_igetfield
 733   // _aload_0, _fast_agetfield
 734   // _aload_0, _fast_fgetfield
 735   //
 736   // occur frequently. If RewriteFrequentPairs is set, the (slow)
 737   // _aload_0 bytecode checks if the next bytecode is either
 738   // _fast_igetfield, _fast_agetfield or _fast_fgetfield and then
 739   // rewrites the current bytecode into a pair bytecode; otherwise it
 740   // rewrites the current bytecode into _0 that doesn't do
 741   // the pair check anymore.
 742   //
 743   // Note: If the next bytecode is _getfield, the rewrite must be
 744   //       delayed, otherwise we may miss an opportunity for a pair.
 745   //
 746   // Also rewrite frequent pairs
 747   //   aload_0, aload_1
 748   //   aload_0, iload_1
 749   // These bytecodes with a small amount of code are most profitable
 750   // to rewrite.
 751 
 752   if (RewriteFrequentPairs && rc == may_rewrite) {
 753 
 754     Label Lrewrite, Ldont_rewrite;
 755     Register Rnext_byte  = R3_ARG1,
 756              Rrewrite_to = R6_ARG4,
 757              Rscratch    = R11_scratch1;
 758 
 759     // Get next byte.
 760     __ lbz(Rnext_byte, Bytecodes::length_for(Bytecodes::_aload_0), R14_bcp);
 761 
 762     // If _getfield, wait to rewrite. We only want to rewrite the last two bytecodes in a pair.
 763     __ cmpwi(CCR0, Rnext_byte, (unsigned int)(unsigned char)Bytecodes::_getfield);
 764     __ beq(CCR0, Ldont_rewrite);
 765 
 766     __ cmpwi(CCR1, Rnext_byte, (unsigned int)(unsigned char)Bytecodes::_fast_igetfield);
 767     __ li(Rrewrite_to, (unsigned int)(unsigned char)Bytecodes::_fast_iaccess_0);
 768     __ beq(CCR1, Lrewrite);
 769 
 770     __ cmpwi(CCR0, Rnext_byte, (unsigned int)(unsigned char)Bytecodes::_fast_agetfield);
 771     __ li(Rrewrite_to, (unsigned int)(unsigned char)Bytecodes::_fast_aaccess_0);
 772     __ beq(CCR0, Lrewrite);
 773 
 774     __ cmpwi(CCR1, Rnext_byte, (unsigned int)(unsigned char)Bytecodes::_fast_fgetfield);
 775     __ li(Rrewrite_to, (unsigned int)(unsigned char)Bytecodes::_fast_faccess_0);
 776     __ beq(CCR1, Lrewrite);
 777 
 778     __ li(Rrewrite_to, (unsigned int)(unsigned char)Bytecodes::_fast_aload_0);
 779 
 780     __ bind(Lrewrite);
 781     patch_bytecode(Bytecodes::_aload_0, Rrewrite_to, Rscratch, false);
 782     __ bind(Ldont_rewrite);
 783   }
 784 
 785   // Do actual aload_0 (must do this after patch_bytecode which might call VM and GC might change oop).
 786   aload(0);
 787 }
 788 
 789 void TemplateTable::istore() {
 790   transition(itos, vtos);
 791 
 792   const Register Rindex = R11_scratch1;
 793   locals_index(Rindex);
 794   __ store_local_int(R17_tos, Rindex);
 795 }
 796 
 797 void TemplateTable::lstore() {
 798   transition(ltos, vtos);
 799   const Register Rindex = R11_scratch1;
 800   locals_index(Rindex);
 801   __ store_local_long(R17_tos, Rindex);
 802 }
 803 
 804 void TemplateTable::fstore() {
 805   transition(ftos, vtos);
 806 
 807   const Register Rindex = R11_scratch1;
 808   locals_index(Rindex);
 809   __ store_local_float(F15_ftos, Rindex);
 810 }
 811 
 812 void TemplateTable::dstore() {
 813   transition(dtos, vtos);
 814 
 815   const Register Rindex = R11_scratch1;
 816   locals_index(Rindex);
 817   __ store_local_double(F15_ftos, Rindex);
 818 }
 819 
 820 void TemplateTable::astore() {
 821   transition(vtos, vtos);
 822 
 823   const Register Rindex = R11_scratch1;
 824   __ pop_ptr();
 825   __ verify_oop_or_return_address(R17_tos, Rindex);
 826   locals_index(Rindex);
 827   __ store_local_ptr(R17_tos, Rindex);
 828 }
 829 
 830 void TemplateTable::wide_istore() {
 831   transition(vtos, vtos);
 832 
 833   const Register Rindex = R11_scratch1;
 834   __ pop_i();
 835   locals_index_wide(Rindex);
 836   __ store_local_int(R17_tos, Rindex);
 837 }
 838 
 839 void TemplateTable::wide_lstore() {
 840   transition(vtos, vtos);
 841 
 842   const Register Rindex = R11_scratch1;
 843   __ pop_l();
 844   locals_index_wide(Rindex);
 845   __ store_local_long(R17_tos, Rindex);
 846 }
 847 
 848 void TemplateTable::wide_fstore() {
 849   transition(vtos, vtos);
 850 
 851   const Register Rindex = R11_scratch1;
 852   __ pop_f();
 853   locals_index_wide(Rindex);
 854   __ store_local_float(F15_ftos, Rindex);
 855 }
 856 
 857 void TemplateTable::wide_dstore() {
 858   transition(vtos, vtos);
 859 
 860   const Register Rindex = R11_scratch1;
 861   __ pop_d();
 862   locals_index_wide(Rindex);
 863   __ store_local_double(F15_ftos, Rindex);
 864 }
 865 
 866 void TemplateTable::wide_astore() {
 867   transition(vtos, vtos);
 868 
 869   const Register Rindex = R11_scratch1;
 870   __ pop_ptr();
 871   __ verify_oop_or_return_address(R17_tos, Rindex);
 872   locals_index_wide(Rindex);
 873   __ store_local_ptr(R17_tos, Rindex);
 874 }
 875 
 876 void TemplateTable::iastore() {
 877   transition(itos, vtos);
 878 
 879   const Register Rindex      = R3_ARG1,
 880                  Rstore_addr = R4_ARG2,
 881                  Rarray      = R5_ARG3,
 882                  Rtemp       = R6_ARG4;
 883   __ pop_i(Rindex);
 884   __ index_check(Rarray, Rindex, LogBytesPerInt, Rtemp, Rstore_addr);
 885   __ stw(R17_tos, arrayOopDesc::base_offset_in_bytes(T_INT), Rstore_addr);
 886   }
 887 
 888 void TemplateTable::lastore() {
 889   transition(ltos, vtos);
 890 
 891   const Register Rindex      = R3_ARG1,
 892                  Rstore_addr = R4_ARG2,
 893                  Rarray      = R5_ARG3,
 894                  Rtemp       = R6_ARG4;
 895   __ pop_i(Rindex);
 896   __ index_check(Rarray, Rindex, LogBytesPerLong, Rtemp, Rstore_addr);
 897   __ std(R17_tos, arrayOopDesc::base_offset_in_bytes(T_LONG), Rstore_addr);
 898   }
 899 
 900 void TemplateTable::fastore() {
 901   transition(ftos, vtos);
 902 
 903   const Register Rindex      = R3_ARG1,
 904                  Rstore_addr = R4_ARG2,
 905                  Rarray      = R5_ARG3,
 906                  Rtemp       = R6_ARG4;
 907   __ pop_i(Rindex);
 908   __ index_check(Rarray, Rindex, LogBytesPerInt, Rtemp, Rstore_addr);
 909   __ stfs(F15_ftos, arrayOopDesc::base_offset_in_bytes(T_FLOAT), Rstore_addr);
 910   }
 911 
 912 void TemplateTable::dastore() {
 913   transition(dtos, vtos);
 914 
 915   const Register Rindex      = R3_ARG1,
 916                  Rstore_addr = R4_ARG2,
 917                  Rarray      = R5_ARG3,
 918                  Rtemp       = R6_ARG4;
 919   __ pop_i(Rindex);
 920   __ index_check(Rarray, Rindex, LogBytesPerLong, Rtemp, Rstore_addr);
 921   __ stfd(F15_ftos, arrayOopDesc::base_offset_in_bytes(T_DOUBLE), Rstore_addr);
 922   }
 923 
 924 // Pop 3 values from the stack and...
 925 void TemplateTable::aastore() {
 926   transition(vtos, vtos);
 927 
 928   Label Lstore_ok, Lis_null, Ldone;
 929   const Register Rindex    = R3_ARG1,
 930                  Rarray    = R4_ARG2,
 931                  Rscratch  = R11_scratch1,
 932                  Rscratch2 = R12_scratch2,
 933                  Rarray_klass = R5_ARG3,
 934                  Rarray_element_klass = Rarray_klass,
 935                  Rvalue_klass = R6_ARG4,
 936                  Rstore_addr = R31;    // Use register which survives VM call.
 937 
 938   __ ld(R17_tos, Interpreter::expr_offset_in_bytes(0), R15_esp); // Get value to store.
 939   __ lwz(Rindex, Interpreter::expr_offset_in_bytes(1), R15_esp); // Get index.
 940   __ ld(Rarray, Interpreter::expr_offset_in_bytes(2), R15_esp);  // Get array.
 941 
 942   __ verify_oop(R17_tos);
 943   __ index_check_without_pop(Rarray, Rindex, UseCompressedOops ? 2 : LogBytesPerWord, Rscratch, Rstore_addr);
 944   // Rindex is dead!
 945   Register Rscratch3 = Rindex;
 946 
 947   // Do array store check - check for NULL value first.
 948   __ cmpdi(CCR0, R17_tos, 0);
 949   __ beq(CCR0, Lis_null);
 950 
 951   __ load_klass(Rarray_klass, Rarray);
 952   __ load_klass(Rvalue_klass, R17_tos);
 953 
 954   // Do fast instanceof cache test.
 955   __ ld(Rarray_element_klass, in_bytes(ObjArrayKlass::element_klass_offset()), Rarray_klass);
 956 
 957   // Generate a fast subtype check. Branch to store_ok if no failure. Throw if failure.
 958   __ gen_subtype_check(Rvalue_klass /*subklass*/, Rarray_element_klass /*superklass*/, Rscratch, Rscratch2, Rscratch3, Lstore_ok);
 959 
 960   // Fell through: subtype check failed => throw an exception.
 961   __ load_dispatch_table(R11_scratch1, (address*)Interpreter::_throw_ArrayStoreException_entry);
 962   __ mtctr(R11_scratch1);
 963   __ bctr();
 964 
 965   __ bind(Lis_null);
 966   do_oop_store(_masm, Rstore_addr, arrayOopDesc::base_offset_in_bytes(T_OBJECT), noreg /* 0 */,
 967                Rscratch, Rscratch2, Rscratch3, _bs->kind(), true /* precise */, false /* check_null */);
 968   __ profile_null_seen(Rscratch, Rscratch2);
 969   __ b(Ldone);
 970 
 971   // Store is OK.
 972   __ bind(Lstore_ok);
 973   do_oop_store(_masm, Rstore_addr, arrayOopDesc::base_offset_in_bytes(T_OBJECT), R17_tos /* value */,
 974                Rscratch, Rscratch2, Rscratch3, _bs->kind(), true /* precise */, false /* check_null */);
 975 
 976   __ bind(Ldone);
 977   // Adjust sp (pops array, index and value).
 978   __ addi(R15_esp, R15_esp, 3 * Interpreter::stackElementSize);
 979 }
 980 
 981 void TemplateTable::bastore() {
 982   transition(itos, vtos);
 983 
 984   const Register Rindex   = R11_scratch1,
 985                  Rarray   = R12_scratch2,
 986                  Rscratch = R3_ARG1;
 987   __ pop_i(Rindex);
 988   // tos: val
 989   // Rarray: array ptr (popped by index_check)
 990   __ index_check(Rarray, Rindex, 0, Rscratch, Rarray);
 991   __ stb(R17_tos, arrayOopDesc::base_offset_in_bytes(T_BYTE), Rarray);
 992 }
 993 
 994 void TemplateTable::castore() {
 995   transition(itos, vtos);
 996 
 997   const Register Rindex   = R11_scratch1,
 998                  Rarray   = R12_scratch2,
 999                  Rscratch = R3_ARG1;
1000   __ pop_i(Rindex);
1001   // tos: val
1002   // Rarray: array ptr (popped by index_check)
1003   __ index_check(Rarray, Rindex, LogBytesPerShort, Rscratch, Rarray);
1004   __ sth(R17_tos, arrayOopDesc::base_offset_in_bytes(T_CHAR), Rarray);
1005 }
1006 
1007 void TemplateTable::sastore() {
1008   castore();
1009 }
1010 
1011 void TemplateTable::istore(int n) {
1012   transition(itos, vtos);
1013   __ stw(R17_tos, Interpreter::local_offset_in_bytes(n), R18_locals);
1014 }
1015 
1016 void TemplateTable::lstore(int n) {
1017   transition(ltos, vtos);
1018   __ std(R17_tos, Interpreter::local_offset_in_bytes(n + 1), R18_locals);
1019 }
1020 
1021 void TemplateTable::fstore(int n) {
1022   transition(ftos, vtos);
1023   __ stfs(F15_ftos, Interpreter::local_offset_in_bytes(n), R18_locals);
1024 }
1025 
1026 void TemplateTable::dstore(int n) {
1027   transition(dtos, vtos);
1028   __ stfd(F15_ftos, Interpreter::local_offset_in_bytes(n + 1), R18_locals);
1029 }
1030 
1031 void TemplateTable::astore(int n) {
1032   transition(vtos, vtos);
1033 
1034   __ pop_ptr();
1035   __ verify_oop_or_return_address(R17_tos, R11_scratch1);
1036   __ std(R17_tos, Interpreter::local_offset_in_bytes(n), R18_locals);
1037 }
1038 
1039 void TemplateTable::pop() {
1040   transition(vtos, vtos);
1041 
1042   __ addi(R15_esp, R15_esp, Interpreter::stackElementSize);
1043 }
1044 
1045 void TemplateTable::pop2() {
1046   transition(vtos, vtos);
1047 
1048   __ addi(R15_esp, R15_esp, Interpreter::stackElementSize * 2);
1049 }
1050 
1051 void TemplateTable::dup() {
1052   transition(vtos, vtos);
1053 
1054   __ ld(R11_scratch1, Interpreter::stackElementSize, R15_esp);
1055   __ push_ptr(R11_scratch1);
1056 }
1057 
1058 void TemplateTable::dup_x1() {
1059   transition(vtos, vtos);
1060 
1061   Register Ra = R11_scratch1,
1062            Rb = R12_scratch2;
1063   // stack: ..., a, b
1064   __ ld(Rb, Interpreter::stackElementSize,     R15_esp);
1065   __ ld(Ra, Interpreter::stackElementSize * 2, R15_esp);
1066   __ std(Rb, Interpreter::stackElementSize * 2, R15_esp);
1067   __ std(Ra, Interpreter::stackElementSize,     R15_esp);
1068   __ push_ptr(Rb);
1069   // stack: ..., b, a, b
1070 }
1071 
1072 void TemplateTable::dup_x2() {
1073   transition(vtos, vtos);
1074 
1075   Register Ra = R11_scratch1,
1076            Rb = R12_scratch2,
1077            Rc = R3_ARG1;
1078 
1079   // stack: ..., a, b, c
1080   __ ld(Rc, Interpreter::stackElementSize,     R15_esp);  // load c
1081   __ ld(Ra, Interpreter::stackElementSize * 3, R15_esp);  // load a
1082   __ std(Rc, Interpreter::stackElementSize * 3, R15_esp); // store c in a
1083   __ ld(Rb, Interpreter::stackElementSize * 2, R15_esp);  // load b
1084   // stack: ..., c, b, c
1085   __ std(Ra, Interpreter::stackElementSize * 2, R15_esp); // store a in b
1086   // stack: ..., c, a, c
1087   __ std(Rb, Interpreter::stackElementSize,     R15_esp); // store b in c
1088   __ push_ptr(Rc);                                        // push c
1089   // stack: ..., c, a, b, c
1090 }
1091 
1092 void TemplateTable::dup2() {
1093   transition(vtos, vtos);
1094 
1095   Register Ra = R11_scratch1,
1096            Rb = R12_scratch2;
1097   // stack: ..., a, b
1098   __ ld(Rb, Interpreter::stackElementSize,     R15_esp);
1099   __ ld(Ra, Interpreter::stackElementSize * 2, R15_esp);
1100   __ push_2ptrs(Ra, Rb);
1101   // stack: ..., a, b, a, b
1102 }
1103 
1104 void TemplateTable::dup2_x1() {
1105   transition(vtos, vtos);
1106 
1107   Register Ra = R11_scratch1,
1108            Rb = R12_scratch2,
1109            Rc = R3_ARG1;
1110   // stack: ..., a, b, c
1111   __ ld(Rc, Interpreter::stackElementSize,     R15_esp);
1112   __ ld(Rb, Interpreter::stackElementSize * 2, R15_esp);
1113   __ std(Rc, Interpreter::stackElementSize * 2, R15_esp);
1114   __ ld(Ra, Interpreter::stackElementSize * 3, R15_esp);
1115   __ std(Ra, Interpreter::stackElementSize,     R15_esp);
1116   __ std(Rb, Interpreter::stackElementSize * 3, R15_esp);
1117   // stack: ..., b, c, a
1118   __ push_2ptrs(Rb, Rc);
1119   // stack: ..., b, c, a, b, c
1120 }
1121 
1122 void TemplateTable::dup2_x2() {
1123   transition(vtos, vtos);
1124 
1125   Register Ra = R11_scratch1,
1126            Rb = R12_scratch2,
1127            Rc = R3_ARG1,
1128            Rd = R4_ARG2;
1129   // stack: ..., a, b, c, d
1130   __ ld(Rb, Interpreter::stackElementSize * 3, R15_esp);
1131   __ ld(Rd, Interpreter::stackElementSize,     R15_esp);
1132   __ std(Rb, Interpreter::stackElementSize,     R15_esp);  // store b in d
1133   __ std(Rd, Interpreter::stackElementSize * 3, R15_esp);  // store d in b
1134   __ ld(Ra, Interpreter::stackElementSize * 4, R15_esp);
1135   __ ld(Rc, Interpreter::stackElementSize * 2, R15_esp);
1136   __ std(Ra, Interpreter::stackElementSize * 2, R15_esp);  // store a in c
1137   __ std(Rc, Interpreter::stackElementSize * 4, R15_esp);  // store c in a
1138   // stack: ..., c, d, a, b
1139   __ push_2ptrs(Rc, Rd);
1140   // stack: ..., c, d, a, b, c, d
1141 }
1142 
1143 void TemplateTable::swap() {
1144   transition(vtos, vtos);
1145   // stack: ..., a, b
1146 
1147   Register Ra = R11_scratch1,
1148            Rb = R12_scratch2;
1149   // stack: ..., a, b
1150   __ ld(Rb, Interpreter::stackElementSize,     R15_esp);
1151   __ ld(Ra, Interpreter::stackElementSize * 2, R15_esp);
1152   __ std(Rb, Interpreter::stackElementSize * 2, R15_esp);
1153   __ std(Ra, Interpreter::stackElementSize,     R15_esp);
1154   // stack: ..., b, a
1155 }
1156 
1157 void TemplateTable::iop2(Operation op) {
1158   transition(itos, itos);
1159 
1160   Register Rscratch = R11_scratch1;
1161 
1162   __ pop_i(Rscratch);
1163   // tos  = number of bits to shift
1164   // Rscratch = value to shift
1165   switch (op) {
1166     case  add:   __ add(R17_tos, Rscratch, R17_tos); break;
1167     case  sub:   __ sub(R17_tos, Rscratch, R17_tos); break;
1168     case  mul:   __ mullw(R17_tos, Rscratch, R17_tos); break;
1169     case  _and:  __ andr(R17_tos, Rscratch, R17_tos); break;
1170     case  _or:   __ orr(R17_tos, Rscratch, R17_tos); break;
1171     case  _xor:  __ xorr(R17_tos, Rscratch, R17_tos); break;
1172     case  shl:   __ rldicl(R17_tos, R17_tos, 0, 64-5); __ slw(R17_tos, Rscratch, R17_tos); break;
1173     case  shr:   __ rldicl(R17_tos, R17_tos, 0, 64-5); __ sraw(R17_tos, Rscratch, R17_tos); break;
1174     case  ushr:  __ rldicl(R17_tos, R17_tos, 0, 64-5); __ srw(R17_tos, Rscratch, R17_tos); break;
1175     default:     ShouldNotReachHere();
1176   }
1177 }
1178 
1179 void TemplateTable::lop2(Operation op) {
1180   transition(ltos, ltos);
1181 
1182   Register Rscratch = R11_scratch1;
1183   __ pop_l(Rscratch);
1184   switch (op) {
1185     case  add:   __ add(R17_tos, Rscratch, R17_tos); break;
1186     case  sub:   __ sub(R17_tos, Rscratch, R17_tos); break;
1187     case  _and:  __ andr(R17_tos, Rscratch, R17_tos); break;
1188     case  _or:   __ orr(R17_tos, Rscratch, R17_tos); break;
1189     case  _xor:  __ xorr(R17_tos, Rscratch, R17_tos); break;
1190     default:     ShouldNotReachHere();
1191   }
1192 }
1193 
1194 void TemplateTable::idiv() {
1195   transition(itos, itos);
1196 
1197   Label Lnormal, Lexception, Ldone;
1198   Register Rdividend = R11_scratch1; // Used by irem.
1199 
1200   __ addi(R0, R17_tos, 1);
1201   __ cmplwi(CCR0, R0, 2);
1202   __ bgt(CCR0, Lnormal); // divisor <-1 or >1
1203 
1204   __ cmpwi(CCR1, R17_tos, 0);
1205   __ beq(CCR1, Lexception); // divisor == 0
1206 
1207   __ pop_i(Rdividend);
1208   __ mullw(R17_tos, Rdividend, R17_tos); // div by +/-1
1209   __ b(Ldone);
1210 
1211   __ bind(Lexception);
1212   __ load_dispatch_table(R11_scratch1, (address*)Interpreter::_throw_ArithmeticException_entry);
1213   __ mtctr(R11_scratch1);
1214   __ bctr();
1215 
1216   __ align(32, 12);
1217   __ bind(Lnormal);
1218   __ pop_i(Rdividend);
1219   __ divw(R17_tos, Rdividend, R17_tos); // Can't divide minint/-1.
1220   __ bind(Ldone);
1221 }
1222 
1223 void TemplateTable::irem() {
1224   transition(itos, itos);
1225 
1226   __ mr(R12_scratch2, R17_tos);
1227   idiv();
1228   __ mullw(R17_tos, R17_tos, R12_scratch2);
1229   __ subf(R17_tos, R17_tos, R11_scratch1); // Dividend set by idiv.
1230 }
1231 
1232 void TemplateTable::lmul() {
1233   transition(ltos, ltos);
1234 
1235   __ pop_l(R11_scratch1);
1236   __ mulld(R17_tos, R11_scratch1, R17_tos);
1237 }
1238 
1239 void TemplateTable::ldiv() {
1240   transition(ltos, ltos);
1241 
1242   Label Lnormal, Lexception, Ldone;
1243   Register Rdividend = R11_scratch1; // Used by lrem.
1244 
1245   __ addi(R0, R17_tos, 1);
1246   __ cmpldi(CCR0, R0, 2);
1247   __ bgt(CCR0, Lnormal); // divisor <-1 or >1
1248 
1249   __ cmpdi(CCR1, R17_tos, 0);
1250   __ beq(CCR1, Lexception); // divisor == 0
1251 
1252   __ pop_l(Rdividend);
1253   __ mulld(R17_tos, Rdividend, R17_tos); // div by +/-1
1254   __ b(Ldone);
1255 
1256   __ bind(Lexception);
1257   __ load_dispatch_table(R11_scratch1, (address*)Interpreter::_throw_ArithmeticException_entry);
1258   __ mtctr(R11_scratch1);
1259   __ bctr();
1260 
1261   __ align(32, 12);
1262   __ bind(Lnormal);
1263   __ pop_l(Rdividend);
1264   __ divd(R17_tos, Rdividend, R17_tos); // Can't divide minint/-1.
1265   __ bind(Ldone);
1266 }
1267 
1268 void TemplateTable::lrem() {
1269   transition(ltos, ltos);
1270 
1271   __ mr(R12_scratch2, R17_tos);
1272   ldiv();
1273   __ mulld(R17_tos, R17_tos, R12_scratch2);
1274   __ subf(R17_tos, R17_tos, R11_scratch1); // Dividend set by ldiv.
1275 }
1276 
1277 void TemplateTable::lshl() {
1278   transition(itos, ltos);
1279 
1280   __ rldicl(R17_tos, R17_tos, 0, 64-6); // Extract least significant bits.
1281   __ pop_l(R11_scratch1);
1282   __ sld(R17_tos, R11_scratch1, R17_tos);
1283 }
1284 
1285 void TemplateTable::lshr() {
1286   transition(itos, ltos);
1287 
1288   __ rldicl(R17_tos, R17_tos, 0, 64-6); // Extract least significant bits.
1289   __ pop_l(R11_scratch1);
1290   __ srad(R17_tos, R11_scratch1, R17_tos);
1291 }
1292 
1293 void TemplateTable::lushr() {
1294   transition(itos, ltos);
1295 
1296   __ rldicl(R17_tos, R17_tos, 0, 64-6); // Extract least significant bits.
1297   __ pop_l(R11_scratch1);
1298   __ srd(R17_tos, R11_scratch1, R17_tos);
1299 }
1300 
1301 void TemplateTable::fop2(Operation op) {
1302   transition(ftos, ftos);
1303 
1304   switch (op) {
1305     case add: __ pop_f(F0_SCRATCH); __ fadds(F15_ftos, F0_SCRATCH, F15_ftos); break;
1306     case sub: __ pop_f(F0_SCRATCH); __ fsubs(F15_ftos, F0_SCRATCH, F15_ftos); break;
1307     case mul: __ pop_f(F0_SCRATCH); __ fmuls(F15_ftos, F0_SCRATCH, F15_ftos); break;
1308     case div: __ pop_f(F0_SCRATCH); __ fdivs(F15_ftos, F0_SCRATCH, F15_ftos); break;
1309     case rem:
1310       __ pop_f(F1_ARG1);
1311       __ fmr(F2_ARG2, F15_ftos);
1312       __ call_VM_leaf(CAST_FROM_FN_PTR(address, SharedRuntime::frem));
1313       __ fmr(F15_ftos, F1_RET);
1314       break;
1315 
1316     default: ShouldNotReachHere();
1317   }
1318 }
1319 
1320 void TemplateTable::dop2(Operation op) {
1321   transition(dtos, dtos);
1322 
1323   switch (op) {
1324     case add: __ pop_d(F0_SCRATCH); __ fadd(F15_ftos, F0_SCRATCH, F15_ftos); break;
1325     case sub: __ pop_d(F0_SCRATCH); __ fsub(F15_ftos, F0_SCRATCH, F15_ftos); break;
1326     case mul: __ pop_d(F0_SCRATCH); __ fmul(F15_ftos, F0_SCRATCH, F15_ftos); break;
1327     case div: __ pop_d(F0_SCRATCH); __ fdiv(F15_ftos, F0_SCRATCH, F15_ftos); break;
1328     case rem:
1329       __ pop_d(F1_ARG1);
1330       __ fmr(F2_ARG2, F15_ftos);
1331       __ call_VM_leaf(CAST_FROM_FN_PTR(address, SharedRuntime::drem));
1332       __ fmr(F15_ftos, F1_RET);
1333       break;
1334 
1335     default: ShouldNotReachHere();
1336   }
1337 }
1338 
1339 // Negate the value in the TOS cache.
1340 void TemplateTable::ineg() {
1341   transition(itos, itos);
1342 
1343   __ neg(R17_tos, R17_tos);
1344 }
1345 
1346 // Negate the value in the TOS cache.
1347 void TemplateTable::lneg() {
1348   transition(ltos, ltos);
1349 
1350   __ neg(R17_tos, R17_tos);
1351 }
1352 
1353 void TemplateTable::fneg() {
1354   transition(ftos, ftos);
1355 
1356   __ fneg(F15_ftos, F15_ftos);
1357 }
1358 
1359 void TemplateTable::dneg() {
1360   transition(dtos, dtos);
1361 
1362   __ fneg(F15_ftos, F15_ftos);
1363 }
1364 
1365 // Increments a local variable in place.
1366 void TemplateTable::iinc() {
1367   transition(vtos, vtos);
1368 
1369   const Register Rindex     = R11_scratch1,
1370                  Rincrement = R0,
1371                  Rvalue     = R12_scratch2;
1372 
1373   locals_index(Rindex);              // Load locals index from bytecode stream.
1374   __ lbz(Rincrement, 2, R14_bcp);    // Load increment from the bytecode stream.
1375   __ extsb(Rincrement, Rincrement);
1376 
1377   __ load_local_int(Rvalue, Rindex, Rindex); // Puts address of local into Rindex.
1378 
1379   __ add(Rvalue, Rincrement, Rvalue);
1380   __ stw(Rvalue, 0, Rindex);
1381 }
1382 
1383 void TemplateTable::wide_iinc() {
1384   transition(vtos, vtos);
1385 
1386   Register Rindex       = R11_scratch1,
1387            Rlocals_addr = Rindex,
1388            Rincr        = R12_scratch2;
1389   locals_index_wide(Rindex);
1390   __ get_2_byte_integer_at_bcp(4, Rincr, InterpreterMacroAssembler::Signed);
1391   __ load_local_int(R17_tos, Rlocals_addr, Rindex);
1392   __ add(R17_tos, Rincr, R17_tos);
1393   __ stw(R17_tos, 0, Rlocals_addr);
1394 }
1395 
1396 void TemplateTable::convert() {
1397   // %%%%% Factor this first part accross platforms
1398 #ifdef ASSERT
1399   TosState tos_in  = ilgl;
1400   TosState tos_out = ilgl;
1401   switch (bytecode()) {
1402     case Bytecodes::_i2l: // fall through
1403     case Bytecodes::_i2f: // fall through
1404     case Bytecodes::_i2d: // fall through
1405     case Bytecodes::_i2b: // fall through
1406     case Bytecodes::_i2c: // fall through
1407     case Bytecodes::_i2s: tos_in = itos; break;
1408     case Bytecodes::_l2i: // fall through
1409     case Bytecodes::_l2f: // fall through
1410     case Bytecodes::_l2d: tos_in = ltos; break;
1411     case Bytecodes::_f2i: // fall through
1412     case Bytecodes::_f2l: // fall through
1413     case Bytecodes::_f2d: tos_in = ftos; break;
1414     case Bytecodes::_d2i: // fall through
1415     case Bytecodes::_d2l: // fall through
1416     case Bytecodes::_d2f: tos_in = dtos; break;
1417     default             : ShouldNotReachHere();
1418   }
1419   switch (bytecode()) {
1420     case Bytecodes::_l2i: // fall through
1421     case Bytecodes::_f2i: // fall through
1422     case Bytecodes::_d2i: // fall through
1423     case Bytecodes::_i2b: // fall through
1424     case Bytecodes::_i2c: // fall through
1425     case Bytecodes::_i2s: tos_out = itos; break;
1426     case Bytecodes::_i2l: // fall through
1427     case Bytecodes::_f2l: // fall through
1428     case Bytecodes::_d2l: tos_out = ltos; break;
1429     case Bytecodes::_i2f: // fall through
1430     case Bytecodes::_l2f: // fall through
1431     case Bytecodes::_d2f: tos_out = ftos; break;
1432     case Bytecodes::_i2d: // fall through
1433     case Bytecodes::_l2d: // fall through
1434     case Bytecodes::_f2d: tos_out = dtos; break;
1435     default             : ShouldNotReachHere();
1436   }
1437   transition(tos_in, tos_out);
1438 #endif
1439 
1440   // Conversion
1441   Label done;
1442   switch (bytecode()) {
1443     case Bytecodes::_i2l:
1444       __ extsw(R17_tos, R17_tos);
1445       break;
1446 
1447     case Bytecodes::_l2i:
1448       // Nothing to do, we'll continue to work with the lower bits.
1449       break;
1450 
1451     case Bytecodes::_i2b:
1452       __ extsb(R17_tos, R17_tos);
1453       break;
1454 
1455     case Bytecodes::_i2c:
1456       __ rldicl(R17_tos, R17_tos, 0, 64-2*8);
1457       break;
1458 
1459     case Bytecodes::_i2s:
1460       __ extsh(R17_tos, R17_tos);
1461       break;
1462 
1463     case Bytecodes::_i2d:
1464       __ extsw(R17_tos, R17_tos);
1465     case Bytecodes::_l2d:
1466       __ push_l_pop_d();
1467       __ fcfid(F15_ftos, F15_ftos);
1468       break;
1469 
1470     case Bytecodes::_i2f:
1471       __ extsw(R17_tos, R17_tos);
1472       __ push_l_pop_d();
1473       if (VM_Version::has_fcfids()) { // fcfids is >= Power7 only
1474         // Comment: alternatively, load with sign extend could be done by lfiwax.
1475         __ fcfids(F15_ftos, F15_ftos);
1476       } else {
1477         __ fcfid(F15_ftos, F15_ftos);
1478         __ frsp(F15_ftos, F15_ftos);
1479       }
1480       break;
1481 
1482     case Bytecodes::_l2f:
1483       if (VM_Version::has_fcfids()) { // fcfids is >= Power7 only
1484         __ push_l_pop_d();
1485         __ fcfids(F15_ftos, F15_ftos);
1486       } else {
1487         // Avoid rounding problem when result should be 0x3f800001: need fixup code before fcfid+frsp.
1488         __ mr(R3_ARG1, R17_tos);
1489         __ call_VM_leaf(CAST_FROM_FN_PTR(address, SharedRuntime::l2f));
1490         __ fmr(F15_ftos, F1_RET);
1491       }
1492       break;
1493 
1494     case Bytecodes::_f2d:
1495       // empty
1496       break;
1497 
1498     case Bytecodes::_d2f:
1499       __ frsp(F15_ftos, F15_ftos);
1500       break;
1501 
1502     case Bytecodes::_d2i:
1503     case Bytecodes::_f2i:
1504       __ fcmpu(CCR0, F15_ftos, F15_ftos);
1505       __ li(R17_tos, 0); // 0 in case of NAN
1506       __ bso(CCR0, done);
1507       __ fctiwz(F15_ftos, F15_ftos);
1508       __ push_d_pop_l();
1509       break;
1510 
1511     case Bytecodes::_d2l:
1512     case Bytecodes::_f2l:
1513       __ fcmpu(CCR0, F15_ftos, F15_ftos);
1514       __ li(R17_tos, 0); // 0 in case of NAN
1515       __ bso(CCR0, done);
1516       __ fctidz(F15_ftos, F15_ftos);
1517       __ push_d_pop_l();
1518       break;
1519 
1520     default: ShouldNotReachHere();
1521   }
1522   __ bind(done);
1523 }
1524 
1525 // Long compare
1526 void TemplateTable::lcmp() {
1527   transition(ltos, itos);
1528 
1529   const Register Rscratch = R11_scratch1;
1530   __ pop_l(Rscratch); // first operand, deeper in stack
1531 
1532   __ cmpd(CCR0, Rscratch, R17_tos); // compare
1533   __ mfcr(R17_tos); // set bit 32..33 as follows: <: 0b10, =: 0b00, >: 0b01
1534   __ srwi(Rscratch, R17_tos, 30);
1535   __ srawi(R17_tos, R17_tos, 31);
1536   __ orr(R17_tos, Rscratch, R17_tos); // set result as follows: <: -1, =: 0, >: 1
1537 }
1538 
1539 // fcmpl/fcmpg and dcmpl/dcmpg bytecodes
1540 // unordered_result == -1 => fcmpl or dcmpl
1541 // unordered_result ==  1 => fcmpg or dcmpg
1542 void TemplateTable::float_cmp(bool is_float, int unordered_result) {
1543   const FloatRegister Rfirst  = F0_SCRATCH,
1544                       Rsecond = F15_ftos;
1545   const Register Rscratch = R11_scratch1;
1546 
1547   if (is_float) {
1548     __ pop_f(Rfirst);
1549   } else {
1550     __ pop_d(Rfirst);
1551   }
1552 
1553   Label Lunordered, Ldone;
1554   __ fcmpu(CCR0, Rfirst, Rsecond); // compare
1555   if (unordered_result) {
1556     __ bso(CCR0, Lunordered);
1557   }
1558   __ mfcr(R17_tos); // set bit 32..33 as follows: <: 0b10, =: 0b00, >: 0b01
1559   __ srwi(Rscratch, R17_tos, 30);
1560   __ srawi(R17_tos, R17_tos, 31);
1561   __ orr(R17_tos, Rscratch, R17_tos); // set result as follows: <: -1, =: 0, >: 1
1562   if (unordered_result) {
1563     __ b(Ldone);
1564     __ bind(Lunordered);
1565     __ load_const_optimized(R17_tos, unordered_result);
1566   }
1567   __ bind(Ldone);
1568 }
1569 
1570 // Branch_conditional which takes TemplateTable::Condition.
1571 void TemplateTable::branch_conditional(ConditionRegister crx, TemplateTable::Condition cc, Label& L, bool invert) {
1572   bool positive = false;
1573   Assembler::Condition cond = Assembler::equal;
1574   switch (cc) {
1575     case TemplateTable::equal:         positive = true ; cond = Assembler::equal  ; break;
1576     case TemplateTable::not_equal:     positive = false; cond = Assembler::equal  ; break;
1577     case TemplateTable::less:          positive = true ; cond = Assembler::less   ; break;
1578     case TemplateTable::less_equal:    positive = false; cond = Assembler::greater; break;
1579     case TemplateTable::greater:       positive = true ; cond = Assembler::greater; break;
1580     case TemplateTable::greater_equal: positive = false; cond = Assembler::less   ; break;
1581     default: ShouldNotReachHere();
1582   }
1583   int bo = (positive != invert) ? Assembler::bcondCRbiIs1 : Assembler::bcondCRbiIs0;
1584   int bi = Assembler::bi0(crx, cond);
1585   __ bc(bo, bi, L);
1586 }
1587 
1588 void TemplateTable::branch(bool is_jsr, bool is_wide) {
1589 
1590   // Note: on SPARC, we use InterpreterMacroAssembler::if_cmp also.
1591   __ verify_thread();
1592 
1593   const Register Rscratch1    = R11_scratch1,
1594                  Rscratch2    = R12_scratch2,
1595                  Rscratch3    = R3_ARG1,
1596                  R4_counters  = R4_ARG2,
1597                  bumped_count = R31,
1598                  Rdisp        = R22_tmp2;
1599 
1600   __ profile_taken_branch(Rscratch1, bumped_count);
1601 
1602   // Get (wide) offset.
1603   if (is_wide) {
1604     __ get_4_byte_integer_at_bcp(1, Rdisp, InterpreterMacroAssembler::Signed);
1605   } else {
1606     __ get_2_byte_integer_at_bcp(1, Rdisp, InterpreterMacroAssembler::Signed);
1607   }
1608 
1609   // --------------------------------------------------------------------------
1610   // Handle all the JSR stuff here, then exit.
1611   // It's much shorter and cleaner than intermingling with the
1612   // non-JSR normal-branch stuff occurring below.
1613   if (is_jsr) {
1614     // Compute return address as bci in Otos_i.
1615     __ ld(Rscratch1, in_bytes(Method::const_offset()), R19_method);
1616     __ addi(Rscratch2, R14_bcp, -in_bytes(ConstMethod::codes_offset()) + (is_wide ? 5 : 3));
1617     __ subf(R17_tos, Rscratch1, Rscratch2);
1618 
1619     // Bump bcp to target of JSR.
1620     __ add(R14_bcp, Rdisp, R14_bcp);
1621     // Push returnAddress for "ret" on stack.
1622     __ push_ptr(R17_tos);
1623     // And away we go!
1624     __ dispatch_next(vtos);
1625     return;
1626   }
1627 
1628   // --------------------------------------------------------------------------
1629   // Normal (non-jsr) branch handling
1630 
1631   const bool increment_invocation_counter_for_backward_branches = UseCompiler && UseLoopCounter;
1632   if (increment_invocation_counter_for_backward_branches) {
1633     //__ unimplemented("branch invocation counter");
1634 
1635     Label Lforward;
1636     __ add(R14_bcp, Rdisp, R14_bcp); // Add to bc addr.
1637 
1638     // Check branch direction.
1639     __ cmpdi(CCR0, Rdisp, 0);
1640     __ bgt(CCR0, Lforward);
1641 
1642     __ get_method_counters(R19_method, R4_counters, Lforward);
1643 
1644     if (TieredCompilation) {
1645       Label Lno_mdo, Loverflow;
1646       const int increment = InvocationCounter::count_increment;
1647       const int mask = ((1 << Tier0BackedgeNotifyFreqLog) - 1) << InvocationCounter::count_shift;
1648       if (ProfileInterpreter) {
1649         Register Rmdo = Rscratch1;
1650 
1651         // If no method data exists, go to profile_continue.
1652         __ ld(Rmdo, in_bytes(Method::method_data_offset()), R19_method);
1653         __ cmpdi(CCR0, Rmdo, 0);
1654         __ beq(CCR0, Lno_mdo);
1655 
1656         // Increment backedge counter in the MDO.
1657         const int mdo_bc_offs = in_bytes(MethodData::backedge_counter_offset()) + in_bytes(InvocationCounter::counter_offset());
1658         __ lwz(Rscratch2, mdo_bc_offs, Rmdo);
1659         __ load_const_optimized(Rscratch3, mask, R0);
1660         __ addi(Rscratch2, Rscratch2, increment);
1661         __ stw(Rscratch2, mdo_bc_offs, Rmdo);
1662         __ and_(Rscratch3, Rscratch2, Rscratch3);
1663         __ bne(CCR0, Lforward);
1664         __ b(Loverflow);
1665       }
1666 
1667       // If there's no MDO, increment counter in method.
1668       const int mo_bc_offs = in_bytes(MethodCounters::backedge_counter_offset()) + in_bytes(InvocationCounter::counter_offset());
1669       __ bind(Lno_mdo);
1670       __ lwz(Rscratch2, mo_bc_offs, R4_counters);
1671       __ load_const_optimized(Rscratch3, mask, R0);
1672       __ addi(Rscratch2, Rscratch2, increment);
1673       __ stw(Rscratch2, mo_bc_offs, R19_method);
1674       __ and_(Rscratch3, Rscratch2, Rscratch3);
1675       __ bne(CCR0, Lforward);
1676 
1677       __ bind(Loverflow);
1678 
1679       // Notify point for loop, pass branch bytecode.
1680       __ call_VM(noreg, CAST_FROM_FN_PTR(address, InterpreterRuntime::frequency_counter_overflow), R14_bcp, true);
1681 
1682       // Was an OSR adapter generated?
1683       // O0 = osr nmethod
1684       __ cmpdi(CCR0, R3_RET, 0);
1685       __ beq(CCR0, Lforward);
1686 
1687       // Has the nmethod been invalidated already?
1688       __ lbz(R0, nmethod::state_offset(), R3_RET);
1689       __ cmpwi(CCR0, R0, nmethod::in_use);
1690       __ bne(CCR0, Lforward);
1691 
1692       // Migrate the interpreter frame off of the stack.
1693       // We can use all registers because we will not return to interpreter from this point.
1694 
1695       // Save nmethod.
1696       const Register osr_nmethod = R31;
1697       __ mr(osr_nmethod, R3_RET);
1698       __ set_top_ijava_frame_at_SP_as_last_Java_frame(R1_SP, R11_scratch1);
1699       __ call_VM_leaf(CAST_FROM_FN_PTR(address, SharedRuntime::OSR_migration_begin), R16_thread);
1700       __ reset_last_Java_frame();
1701       // OSR buffer is in ARG1.
1702 
1703       // Remove the interpreter frame.
1704       __ merge_frames(/*top_frame_sp*/ R21_sender_SP, /*return_pc*/ R0, R11_scratch1, R12_scratch2);
1705 
1706       // Jump to the osr code.
1707       __ ld(R11_scratch1, nmethod::osr_entry_point_offset(), osr_nmethod);
1708       __ mtlr(R0);
1709       __ mtctr(R11_scratch1);
1710       __ bctr();
1711 
1712     } else {
1713 
1714       const Register invoke_ctr = Rscratch1;
1715       // Update Backedge branch separately from invocations.
1716       __ increment_backedge_counter(R4_counters, invoke_ctr, Rscratch2, Rscratch3);
1717 
1718       if (ProfileInterpreter) {
1719         __ test_invocation_counter_for_mdp(invoke_ctr, Rscratch2, Lforward);
1720         if (UseOnStackReplacement) {
1721           __ test_backedge_count_for_osr(bumped_count, R14_bcp, Rscratch2);
1722         }
1723       } else {
1724         if (UseOnStackReplacement) {
1725           __ test_backedge_count_for_osr(invoke_ctr, R14_bcp, Rscratch2);
1726         }
1727       }
1728     }
1729 
1730     __ bind(Lforward);
1731 
1732   } else {
1733     // Bump bytecode pointer by displacement (take the branch).
1734     __ add(R14_bcp, Rdisp, R14_bcp); // Add to bc addr.
1735   }
1736   // Continue with bytecode @ target.
1737   // %%%%% Like Intel, could speed things up by moving bytecode fetch to code above,
1738   // %%%%% and changing dispatch_next to dispatch_only.
1739   __ dispatch_next(vtos);
1740 }
1741 
1742 // Helper function for if_cmp* methods below.
1743 // Factored out common compare and branch code.
1744 void TemplateTable::if_cmp_common(Register Rfirst, Register Rsecond, Register Rscratch1, Register Rscratch2, Condition cc, bool is_jint, bool cmp0) {
1745   Label Lnot_taken;
1746   // Note: The condition code we get is the condition under which we
1747   // *fall through*! So we have to inverse the CC here.
1748 
1749   if (is_jint) {
1750     if (cmp0) {
1751       __ cmpwi(CCR0, Rfirst, 0);
1752     } else {
1753       __ cmpw(CCR0, Rfirst, Rsecond);
1754     }
1755   } else {
1756     if (cmp0) {
1757       __ cmpdi(CCR0, Rfirst, 0);
1758     } else {
1759       __ cmpd(CCR0, Rfirst, Rsecond);
1760     }
1761   }
1762   branch_conditional(CCR0, cc, Lnot_taken, /*invert*/ true);
1763 
1764   // Conition is false => Jump!
1765   branch(false, false);
1766 
1767   // Condition is not true => Continue.
1768   __ align(32, 12);
1769   __ bind(Lnot_taken);
1770   __ profile_not_taken_branch(Rscratch1, Rscratch2);
1771 }
1772 
1773 // Compare integer values with zero and fall through if CC holds, branch away otherwise.
1774 void TemplateTable::if_0cmp(Condition cc) {
1775   transition(itos, vtos);
1776 
1777   if_cmp_common(R17_tos, noreg, R11_scratch1, R12_scratch2, cc, true, true);
1778 }
1779 
1780 // Compare integer values and fall through if CC holds, branch away otherwise.
1781 //
1782 // Interface:
1783 //  - Rfirst: First operand  (older stack value)
1784 //  - tos:    Second operand (younger stack value)
1785 void TemplateTable::if_icmp(Condition cc) {
1786   transition(itos, vtos);
1787 
1788   const Register Rfirst  = R0,
1789                  Rsecond = R17_tos;
1790 
1791   __ pop_i(Rfirst);
1792   if_cmp_common(Rfirst, Rsecond, R11_scratch1, R12_scratch2, cc, true, false);
1793 }
1794 
1795 void TemplateTable::if_nullcmp(Condition cc) {
1796   transition(atos, vtos);
1797 
1798   if_cmp_common(R17_tos, noreg, R11_scratch1, R12_scratch2, cc, false, true);
1799 }
1800 
1801 void TemplateTable::if_acmp(Condition cc) {
1802   transition(atos, vtos);
1803 
1804   const Register Rfirst  = R0,
1805                  Rsecond = R17_tos;
1806 
1807   __ pop_ptr(Rfirst);
1808   if_cmp_common(Rfirst, Rsecond, R11_scratch1, R12_scratch2, cc, false, false);
1809 }
1810 
1811 void TemplateTable::ret() {
1812   locals_index(R11_scratch1);
1813   __ load_local_ptr(R17_tos, R11_scratch1, R11_scratch1);
1814 
1815   __ profile_ret(vtos, R17_tos, R11_scratch1, R12_scratch2);
1816 
1817   __ ld(R11_scratch1, in_bytes(Method::const_offset()), R19_method);
1818   __ add(R11_scratch1, R17_tos, R11_scratch1);
1819   __ addi(R14_bcp, R11_scratch1, in_bytes(ConstMethod::codes_offset()));
1820   __ dispatch_next(vtos);
1821 }
1822 
1823 void TemplateTable::wide_ret() {
1824   transition(vtos, vtos);
1825 
1826   const Register Rindex = R3_ARG1,
1827                  Rscratch1 = R11_scratch1,
1828                  Rscratch2 = R12_scratch2;
1829 
1830   locals_index_wide(Rindex);
1831   __ load_local_ptr(R17_tos, R17_tos, Rindex);
1832   __ profile_ret(vtos, R17_tos, Rscratch1, R12_scratch2);
1833   // Tos now contains the bci, compute the bcp from that.
1834   __ ld(Rscratch1, in_bytes(Method::const_offset()), R19_method);
1835   __ addi(Rscratch2, R17_tos, in_bytes(ConstMethod::codes_offset()));
1836   __ add(R14_bcp, Rscratch1, Rscratch2);
1837   __ dispatch_next(vtos);
1838 }
1839 
1840 void TemplateTable::tableswitch() {
1841   transition(itos, vtos);
1842 
1843   Label Ldispatch, Ldefault_case;
1844   Register Rlow_byte         = R3_ARG1,
1845            Rindex            = Rlow_byte,
1846            Rhigh_byte        = R4_ARG2,
1847            Rdef_offset_addr  = R5_ARG3, // is going to contain address of default offset
1848            Rscratch1         = R11_scratch1,
1849            Rscratch2         = R12_scratch2,
1850            Roffset           = R6_ARG4;
1851 
1852   // Align bcp.
1853   __ addi(Rdef_offset_addr, R14_bcp, BytesPerInt);
1854   __ clrrdi(Rdef_offset_addr, Rdef_offset_addr, log2_long((jlong)BytesPerInt));
1855 
1856   // Load lo & hi.
1857   __ get_u4(Rlow_byte, Rdef_offset_addr, BytesPerInt, InterpreterMacroAssembler::Unsigned);
1858   __ get_u4(Rhigh_byte, Rdef_offset_addr, 2 *BytesPerInt, InterpreterMacroAssembler::Unsigned);
1859 
1860   // Check for default case (=index outside [low,high]).
1861   __ cmpw(CCR0, R17_tos, Rlow_byte);
1862   __ cmpw(CCR1, R17_tos, Rhigh_byte);
1863   __ blt(CCR0, Ldefault_case);
1864   __ bgt(CCR1, Ldefault_case);
1865 
1866   // Lookup dispatch offset.
1867   __ sub(Rindex, R17_tos, Rlow_byte);
1868   __ extsw(Rindex, Rindex);
1869   __ profile_switch_case(Rindex, Rhigh_byte /* scratch */, Rscratch1, Rscratch2);
1870   __ sldi(Rindex, Rindex, LogBytesPerInt);
1871   __ addi(Rindex, Rindex, 3 * BytesPerInt);
1872 #if defined(VM_LITTLE_ENDIAN)
1873   __ lwbrx(Roffset, Rdef_offset_addr, Rindex);
1874   __ extsw(Roffset, Roffset);
1875 #else
1876   __ lwax(Roffset, Rdef_offset_addr, Rindex);
1877 #endif
1878   __ b(Ldispatch);
1879 
1880   __ bind(Ldefault_case);
1881   __ profile_switch_default(Rhigh_byte, Rscratch1);
1882   __ get_u4(Roffset, Rdef_offset_addr, 0, InterpreterMacroAssembler::Signed);
1883 
1884   __ bind(Ldispatch);
1885 
1886   __ add(R14_bcp, Roffset, R14_bcp);
1887   __ dispatch_next(vtos);
1888 }
1889 
1890 void TemplateTable::lookupswitch() {
1891   transition(itos, itos);
1892   __ stop("lookupswitch bytecode should have been rewritten");
1893 }
1894 
1895 // Table switch using linear search through cases.
1896 // Bytecode stream format:
1897 // Bytecode (1) | 4-byte padding | default offset (4) | count (4) | value/offset pair1 (8) | value/offset pair2 (8) | ...
1898 // Note: Everything is big-endian format here.
1899 void TemplateTable::fast_linearswitch() {
1900   transition(itos, vtos);
1901 
1902   Label Lloop_entry, Lsearch_loop, Lcontinue_execution, Ldefault_case;
1903   Register Rcount           = R3_ARG1,
1904            Rcurrent_pair    = R4_ARG2,
1905            Rdef_offset_addr = R5_ARG3, // Is going to contain address of default offset.
1906            Roffset          = R31,     // Might need to survive C call.
1907            Rvalue           = R12_scratch2,
1908            Rscratch         = R11_scratch1,
1909            Rcmp_value       = R17_tos;
1910 
1911   // Align bcp.
1912   __ addi(Rdef_offset_addr, R14_bcp, BytesPerInt);
1913   __ clrrdi(Rdef_offset_addr, Rdef_offset_addr, log2_long((jlong)BytesPerInt));
1914 
1915   // Setup loop counter and limit.
1916   __ get_u4(Rcount, Rdef_offset_addr, BytesPerInt, InterpreterMacroAssembler::Unsigned);
1917   __ addi(Rcurrent_pair, Rdef_offset_addr, 2 * BytesPerInt); // Rcurrent_pair now points to first pair.
1918 
1919   __ mtctr(Rcount);
1920   __ cmpwi(CCR0, Rcount, 0);
1921   __ bne(CCR0, Lloop_entry);
1922 
1923   // Default case
1924   __ bind(Ldefault_case);
1925   __ get_u4(Roffset, Rdef_offset_addr, 0, InterpreterMacroAssembler::Signed);
1926   if (ProfileInterpreter) {
1927     __ profile_switch_default(Rdef_offset_addr, Rcount/* scratch */);
1928   }
1929   __ b(Lcontinue_execution);
1930 
1931   // Next iteration
1932   __ bind(Lsearch_loop);
1933   __ bdz(Ldefault_case);
1934   __ addi(Rcurrent_pair, Rcurrent_pair, 2 * BytesPerInt);
1935   __ bind(Lloop_entry);
1936   __ get_u4(Rvalue, Rcurrent_pair, 0, InterpreterMacroAssembler::Unsigned);
1937   __ cmpw(CCR0, Rvalue, Rcmp_value);
1938   __ bne(CCR0, Lsearch_loop);
1939 
1940   // Found, load offset.
1941   __ get_u4(Roffset, Rcurrent_pair, BytesPerInt, InterpreterMacroAssembler::Signed);
1942   // Calculate case index and profile
1943   __ mfctr(Rcurrent_pair);
1944   if (ProfileInterpreter) {
1945     __ sub(Rcurrent_pair, Rcount, Rcurrent_pair);
1946     __ profile_switch_case(Rcurrent_pair, Rcount /*scratch*/, Rdef_offset_addr/*scratch*/, Rscratch);
1947   }
1948 
1949   __ bind(Lcontinue_execution);
1950   __ add(R14_bcp, Roffset, R14_bcp);
1951   __ dispatch_next(vtos);
1952 }
1953 
1954 // Table switch using binary search (value/offset pairs are ordered).
1955 // Bytecode stream format:
1956 // Bytecode (1) | 4-byte padding | default offset (4) | count (4) | value/offset pair1 (8) | value/offset pair2 (8) | ...
1957 // Note: Everything is big-endian format here. So on little endian machines, we have to revers offset and count and cmp value.
1958 void TemplateTable::fast_binaryswitch() {
1959 
1960   transition(itos, vtos);
1961   // Implementation using the following core algorithm: (copied from Intel)
1962   //
1963   // int binary_search(int key, LookupswitchPair* array, int n) {
1964   //   // Binary search according to "Methodik des Programmierens" by
1965   //   // Edsger W. Dijkstra and W.H.J. Feijen, Addison Wesley Germany 1985.
1966   //   int i = 0;
1967   //   int j = n;
1968   //   while (i+1 < j) {
1969   //     // invariant P: 0 <= i < j <= n and (a[i] <= key < a[j] or Q)
1970   //     // with      Q: for all i: 0 <= i < n: key < a[i]
1971   //     // where a stands for the array and assuming that the (inexisting)
1972   //     // element a[n] is infinitely big.
1973   //     int h = (i + j) >> 1;
1974   //     // i < h < j
1975   //     if (key < array[h].fast_match()) {
1976   //       j = h;
1977   //     } else {
1978   //       i = h;
1979   //     }
1980   //   }
1981   //   // R: a[i] <= key < a[i+1] or Q
1982   //   // (i.e., if key is within array, i is the correct index)
1983   //   return i;
1984   // }
1985 
1986   // register allocation
1987   const Register Rkey     = R17_tos;          // already set (tosca)
1988   const Register Rarray   = R3_ARG1;
1989   const Register Ri       = R4_ARG2;
1990   const Register Rj       = R5_ARG3;
1991   const Register Rh       = R6_ARG4;
1992   const Register Rscratch = R11_scratch1;
1993 
1994   const int log_entry_size = 3;
1995   const int entry_size = 1 << log_entry_size;
1996 
1997   Label found;
1998 
1999   // Find Array start,
2000   __ addi(Rarray, R14_bcp, 3 * BytesPerInt);
2001   __ clrrdi(Rarray, Rarray, log2_long((jlong)BytesPerInt));
2002 
2003   // initialize i & j
2004   __ li(Ri,0);
2005   __ get_u4(Rj, Rarray, -BytesPerInt, InterpreterMacroAssembler::Unsigned);
2006 
2007   // and start.
2008   Label entry;
2009   __ b(entry);
2010 
2011   // binary search loop
2012   { Label loop;
2013     __ bind(loop);
2014     // int h = (i + j) >> 1;
2015     __ srdi(Rh, Rh, 1);
2016     // if (key < array[h].fast_match()) {
2017     //   j = h;
2018     // } else {
2019     //   i = h;
2020     // }
2021     __ sldi(Rscratch, Rh, log_entry_size);
2022 #if defined(VM_LITTLE_ENDIAN)
2023     __ lwbrx(Rscratch, Rscratch, Rarray);
2024 #else
2025     __ lwzx(Rscratch, Rscratch, Rarray);
2026 #endif
2027 
2028     // if (key < current value)
2029     //   Rh = Rj
2030     // else
2031     //   Rh = Ri
2032     Label Lgreater;
2033     __ cmpw(CCR0, Rkey, Rscratch);
2034     __ bge(CCR0, Lgreater);
2035     __ mr(Rj, Rh);
2036     __ b(entry);
2037     __ bind(Lgreater);
2038     __ mr(Ri, Rh);
2039 
2040     // while (i+1 < j)
2041     __ bind(entry);
2042     __ addi(Rscratch, Ri, 1);
2043     __ cmpw(CCR0, Rscratch, Rj);
2044     __ add(Rh, Ri, Rj); // start h = i + j >> 1;
2045 
2046     __ blt(CCR0, loop);
2047   }
2048 
2049   // End of binary search, result index is i (must check again!).
2050   Label default_case;
2051   Label continue_execution;
2052   if (ProfileInterpreter) {
2053     __ mr(Rh, Ri);              // Save index in i for profiling.
2054   }
2055   // Ri = value offset
2056   __ sldi(Ri, Ri, log_entry_size);
2057   __ add(Ri, Ri, Rarray);
2058   __ get_u4(Rscratch, Ri, 0, InterpreterMacroAssembler::Unsigned);
2059 
2060   Label not_found;
2061   // Ri = offset offset
2062   __ cmpw(CCR0, Rkey, Rscratch);
2063   __ beq(CCR0, not_found);
2064   // entry not found -> j = default offset
2065   __ get_u4(Rj, Rarray, -2 * BytesPerInt, InterpreterMacroAssembler::Unsigned);
2066   __ b(default_case);
2067 
2068   __ bind(not_found);
2069   // entry found -> j = offset
2070   __ profile_switch_case(Rh, Rj, Rscratch, Rkey);
2071   __ get_u4(Rj, Ri, BytesPerInt, InterpreterMacroAssembler::Unsigned);
2072 
2073   if (ProfileInterpreter) {
2074     __ b(continue_execution);
2075   }
2076 
2077   __ bind(default_case); // fall through (if not profiling)
2078   __ profile_switch_default(Ri, Rscratch);
2079 
2080   __ bind(continue_execution);
2081 
2082   __ extsw(Rj, Rj);
2083   __ add(R14_bcp, Rj, R14_bcp);
2084   __ dispatch_next(vtos);
2085 }
2086 
2087 void TemplateTable::_return(TosState state) {
2088   transition(state, state);
2089   assert(_desc->calls_vm(),
2090          "inconsistent calls_vm information"); // call in remove_activation
2091 
2092   if (_desc->bytecode() == Bytecodes::_return_register_finalizer) {
2093 
2094     Register Rscratch     = R11_scratch1,
2095              Rklass       = R12_scratch2,
2096              Rklass_flags = Rklass;
2097     Label Lskip_register_finalizer;
2098 
2099     // Check if the method has the FINALIZER flag set and call into the VM to finalize in this case.
2100     assert(state == vtos, "only valid state");
2101     __ ld(R17_tos, 0, R18_locals);
2102 
2103     // Load klass of this obj.
2104     __ load_klass(Rklass, R17_tos);
2105     __ lwz(Rklass_flags, in_bytes(Klass::access_flags_offset()), Rklass);
2106     __ testbitdi(CCR0, R0, Rklass_flags, exact_log2(JVM_ACC_HAS_FINALIZER));
2107     __ bfalse(CCR0, Lskip_register_finalizer);
2108 
2109     __ call_VM(noreg, CAST_FROM_FN_PTR(address, InterpreterRuntime::register_finalizer), R17_tos /* obj */);
2110 
2111     __ align(32, 12);
2112     __ bind(Lskip_register_finalizer);
2113   }
2114 
2115   // Move the result value into the correct register and remove memory stack frame.
2116   __ remove_activation(state, /* throw_monitor_exception */ true);
2117   // Restoration of lr done by remove_activation.
2118   switch (state) {
2119     case ltos:
2120     case btos:
2121     case ctos:
2122     case stos:
2123     case atos:
2124     case itos: __ mr(R3_RET, R17_tos); break;
2125     case ftos:
2126     case dtos: __ fmr(F1_RET, F15_ftos); break;
2127     case vtos: // This might be a constructor. Final fields (and volatile fields on PPC64) need
2128                // to get visible before the reference to the object gets stored anywhere.
2129                __ membar(Assembler::StoreStore); break;
2130     default  : ShouldNotReachHere();
2131   }
2132   __ blr();
2133 }
2134 
2135 // ============================================================================
2136 // Constant pool cache access
2137 //
2138 // Memory ordering:
2139 //
2140 // Like done in C++ interpreter, we load the fields
2141 //   - _indices
2142 //   - _f12_oop
2143 // acquired, because these are asked if the cache is already resolved. We don't
2144 // want to float loads above this check.
2145 // See also comments in ConstantPoolCacheEntry::bytecode_1(),
2146 // ConstantPoolCacheEntry::bytecode_2() and ConstantPoolCacheEntry::f1();
2147 
2148 // Call into the VM if call site is not yet resolved
2149 //
2150 // Input regs:
2151 //   - None, all passed regs are outputs.
2152 //
2153 // Returns:
2154 //   - Rcache:  The const pool cache entry that contains the resolved result.
2155 //   - Rresult: Either noreg or output for f1/f2.
2156 //
2157 // Kills:
2158 //   - Rscratch
2159 void TemplateTable::resolve_cache_and_index(int byte_no, Register Rcache, Register Rscratch, size_t index_size) {
2160 
2161   __ get_cache_and_index_at_bcp(Rcache, 1, index_size);
2162   Label Lresolved, Ldone;
2163 
2164   Bytecodes::Code code = bytecode();
2165   switch (code) {
2166   case Bytecodes::_nofast_getfield: code = Bytecodes::_getfield; break;
2167   case Bytecodes::_nofast_putfield: code = Bytecodes::_putfield; break;
2168   }
2169 
2170   assert(byte_no == f1_byte || byte_no == f2_byte, "byte_no out of range");
2171   // We are resolved if the indices offset contains the current bytecode.
2172 #if defined(VM_LITTLE_ENDIAN)
2173   __ lbz(Rscratch, in_bytes(ConstantPoolCache::base_offset() + ConstantPoolCacheEntry::indices_offset()) + byte_no + 1, Rcache);
2174 #else
2175   __ lbz(Rscratch, in_bytes(ConstantPoolCache::base_offset() + ConstantPoolCacheEntry::indices_offset()) + 7 - (byte_no + 1), Rcache);
2176 #endif
2177   // Acquire by cmp-br-isync (see below).
2178   __ cmpdi(CCR0, Rscratch, (int)code);
2179   __ beq(CCR0, Lresolved);
2180 
2181   address entry = NULL;
2182   switch (code) {
2183     case Bytecodes::_getstatic      : // fall through
2184     case Bytecodes::_putstatic      : // fall through
2185     case Bytecodes::_getfield       : // fall through
2186     case Bytecodes::_putfield       : entry = CAST_FROM_FN_PTR(address, InterpreterRuntime::resolve_get_put); break;
2187     case Bytecodes::_invokevirtual  : // fall through
2188     case Bytecodes::_invokespecial  : // fall through
2189     case Bytecodes::_invokestatic   : // fall through
2190     case Bytecodes::_invokeinterface: entry = CAST_FROM_FN_PTR(address, InterpreterRuntime::resolve_invoke); break;
2191     case Bytecodes::_invokehandle   : entry = CAST_FROM_FN_PTR(address, InterpreterRuntime::resolve_invokehandle); break;
2192     case Bytecodes::_invokedynamic  : entry = CAST_FROM_FN_PTR(address, InterpreterRuntime::resolve_invokedynamic); break;
2193     default                         :
2194       fatal(err_msg("unexpected bytecode: %s", Bytecodes::name(code)));
2195       break;
2196   }
2197   __ li(R4_ARG2, code);
2198   __ call_VM(noreg, entry, R4_ARG2, true);
2199 
2200   // Update registers with resolved info.
2201   __ get_cache_and_index_at_bcp(Rcache, 1, index_size);
2202   __ b(Ldone);
2203 
2204   __ bind(Lresolved);
2205   __ isync(); // Order load wrt. succeeding loads.
2206   __ bind(Ldone);
2207 }
2208 
2209 // Load the constant pool cache entry at field accesses into registers.
2210 // The Rcache and Rindex registers must be set before call.
2211 // Input:
2212 //   - Rcache, Rindex
2213 // Output:
2214 //   - Robj, Roffset, Rflags
2215 void TemplateTable::load_field_cp_cache_entry(Register Robj,
2216                                               Register Rcache,
2217                                               Register Rindex /* unused on PPC64 */,
2218                                               Register Roffset,
2219                                               Register Rflags,
2220                                               bool is_static = false) {
2221   assert_different_registers(Rcache, Rflags, Roffset);
2222   // assert(Rindex == noreg, "parameter not used on PPC64");
2223 
2224   ByteSize cp_base_offset = ConstantPoolCache::base_offset();
2225   __ ld(Rflags, in_bytes(cp_base_offset) + in_bytes(ConstantPoolCacheEntry::flags_offset()), Rcache);
2226   __ ld(Roffset, in_bytes(cp_base_offset) + in_bytes(ConstantPoolCacheEntry::f2_offset()), Rcache);
2227   if (is_static) {
2228     __ ld(Robj, in_bytes(cp_base_offset) + in_bytes(ConstantPoolCacheEntry::f1_offset()), Rcache);
2229     __ ld(Robj, in_bytes(Klass::java_mirror_offset()), Robj);
2230     // Acquire not needed here. Following access has an address dependency on this value.
2231   }
2232 }
2233 
2234 // Load the constant pool cache entry at invokes into registers.
2235 // Resolve if necessary.
2236 
2237 // Input Registers:
2238 //   - None, bcp is used, though
2239 //
2240 // Return registers:
2241 //   - Rmethod       (f1 field or f2 if invokevirtual)
2242 //   - Ritable_index (f2 field)
2243 //   - Rflags        (flags field)
2244 //
2245 // Kills:
2246 //   - R21
2247 //
2248 void TemplateTable::load_invoke_cp_cache_entry(int byte_no,
2249                                                Register Rmethod,
2250                                                Register Ritable_index,
2251                                                Register Rflags,
2252                                                bool is_invokevirtual,
2253                                                bool is_invokevfinal,
2254                                                bool is_invokedynamic) {
2255 
2256   ByteSize cp_base_offset = ConstantPoolCache::base_offset();
2257   // Determine constant pool cache field offsets.
2258   assert(is_invokevirtual == (byte_no == f2_byte), "is_invokevirtual flag redundant");
2259   const int method_offset = in_bytes(cp_base_offset + (is_invokevirtual ? ConstantPoolCacheEntry::f2_offset() : ConstantPoolCacheEntry::f1_offset()));
2260   const int flags_offset  = in_bytes(cp_base_offset + ConstantPoolCacheEntry::flags_offset());
2261   // Access constant pool cache fields.
2262   const int index_offset  = in_bytes(cp_base_offset + ConstantPoolCacheEntry::f2_offset());
2263 
2264   Register Rcache = R21_tmp1; // Note: same register as R21_sender_SP.
2265 
2266   if (is_invokevfinal) {
2267     assert(Ritable_index == noreg, "register not used");
2268     // Already resolved.
2269     __ get_cache_and_index_at_bcp(Rcache, 1);
2270   } else {
2271     resolve_cache_and_index(byte_no, Rcache, R0, is_invokedynamic ? sizeof(u4) : sizeof(u2));
2272   }
2273 
2274   __ ld(Rmethod, method_offset, Rcache);
2275   __ ld(Rflags, flags_offset, Rcache);
2276 
2277   if (Ritable_index != noreg) {
2278     __ ld(Ritable_index, index_offset, Rcache);
2279   }
2280 }
2281 
2282 // ============================================================================
2283 // Field access
2284 
2285 // Volatile variables demand their effects be made known to all CPU's
2286 // in order. Store buffers on most chips allow reads & writes to
2287 // reorder; the JMM's ReadAfterWrite.java test fails in -Xint mode
2288 // without some kind of memory barrier (i.e., it's not sufficient that
2289 // the interpreter does not reorder volatile references, the hardware
2290 // also must not reorder them).
2291 //
2292 // According to the new Java Memory Model (JMM):
2293 // (1) All volatiles are serialized wrt to each other. ALSO reads &
2294 //     writes act as aquire & release, so:
2295 // (2) A read cannot let unrelated NON-volatile memory refs that
2296 //     happen after the read float up to before the read. It's OK for
2297 //     non-volatile memory refs that happen before the volatile read to
2298 //     float down below it.
2299 // (3) Similar a volatile write cannot let unrelated NON-volatile
2300 //     memory refs that happen BEFORE the write float down to after the
2301 //     write. It's OK for non-volatile memory refs that happen after the
2302 //     volatile write to float up before it.
2303 //
2304 // We only put in barriers around volatile refs (they are expensive),
2305 // not _between_ memory refs (that would require us to track the
2306 // flavor of the previous memory refs). Requirements (2) and (3)
2307 // require some barriers before volatile stores and after volatile
2308 // loads. These nearly cover requirement (1) but miss the
2309 // volatile-store-volatile-load case.  This final case is placed after
2310 // volatile-stores although it could just as well go before
2311 // volatile-loads.
2312 
2313 // The registers cache and index expected to be set before call.
2314 // Correct values of the cache and index registers are preserved.
2315 // Kills:
2316 //   Rcache (if has_tos)
2317 //   Rscratch
2318 void TemplateTable::jvmti_post_field_access(Register Rcache, Register Rscratch, bool is_static, bool has_tos) {
2319 
2320   assert_different_registers(Rcache, Rscratch);
2321 
2322   if (JvmtiExport::can_post_field_access()) {
2323     ByteSize cp_base_offset = ConstantPoolCache::base_offset();
2324     Label Lno_field_access_post;
2325 
2326     // Check if post field access in enabled.
2327     int offs = __ load_const_optimized(Rscratch, JvmtiExport::get_field_access_count_addr(), R0, true);
2328     __ lwz(Rscratch, offs, Rscratch);
2329 
2330     __ cmpwi(CCR0, Rscratch, 0);
2331     __ beq(CCR0, Lno_field_access_post);
2332 
2333     // Post access enabled - do it!
2334     __ addi(Rcache, Rcache, in_bytes(cp_base_offset));
2335     if (is_static) {
2336       __ li(R17_tos, 0);
2337     } else {
2338       if (has_tos) {
2339         // The fast bytecode versions have obj ptr in register.
2340         // Thus, save object pointer before call_VM() clobbers it
2341         // put object on tos where GC wants it.
2342         __ push_ptr(R17_tos);
2343       } else {
2344         // Load top of stack (do not pop the value off the stack).
2345         __ ld(R17_tos, Interpreter::expr_offset_in_bytes(0), R15_esp);
2346       }
2347       __ verify_oop(R17_tos);
2348     }
2349     // tos:   object pointer or NULL if static
2350     // cache: cache entry pointer
2351     __ call_VM(noreg, CAST_FROM_FN_PTR(address, InterpreterRuntime::post_field_access), R17_tos, Rcache);
2352     if (!is_static && has_tos) {
2353       // Restore object pointer.
2354       __ pop_ptr(R17_tos);
2355       __ verify_oop(R17_tos);
2356     } else {
2357       // Cache is still needed to get class or obj.
2358       __ get_cache_and_index_at_bcp(Rcache, 1);
2359     }
2360 
2361     __ align(32, 12);
2362     __ bind(Lno_field_access_post);
2363   }
2364 }
2365 
2366 // kills R11_scratch1
2367 void TemplateTable::pop_and_check_object(Register Roop) {
2368   Register Rtmp = R11_scratch1;
2369 
2370   assert_different_registers(Rtmp, Roop);
2371   __ pop_ptr(Roop);
2372   // For field access must check obj.
2373   __ null_check_throw(Roop, -1, Rtmp);
2374   __ verify_oop(Roop);
2375 }
2376 
2377 // PPC64: implement volatile loads as fence-store-acquire.
2378 void TemplateTable::getfield_or_static(int byte_no, bool is_static, RewriteControl rc) {
2379   transition(vtos, vtos);
2380 
2381   Label Lacquire, Lisync;
2382 
2383   const Register Rcache        = R3_ARG1,
2384                  Rclass_or_obj = R22_tmp2,
2385                  Roffset       = R23_tmp3,
2386                  Rflags        = R31,
2387                  Rbtable       = R5_ARG3,
2388                  Rbc           = R6_ARG4,
2389                  Rscratch      = R12_scratch2;
2390 
2391   static address field_branch_table[number_of_states],
2392                  static_branch_table[number_of_states];
2393 
2394   address* branch_table = (is_static || rc == may_not_rewrite) ? static_branch_table : field_branch_table;
2395 
2396   // Get field offset.
2397   resolve_cache_and_index(byte_no, Rcache, Rscratch, sizeof(u2));
2398 
2399   // JVMTI support
2400   jvmti_post_field_access(Rcache, Rscratch, is_static, false);
2401 
2402   // Load after possible GC.
2403   load_field_cp_cache_entry(Rclass_or_obj, Rcache, noreg, Roffset, Rflags, is_static);
2404 
2405   // Load pointer to branch table.
2406   __ load_const_optimized(Rbtable, (address)branch_table, Rscratch);
2407 
2408   // Get volatile flag.
2409   __ rldicl(Rscratch, Rflags, 64-ConstantPoolCacheEntry::is_volatile_shift, 63); // Extract volatile bit.
2410   // Note: sync is needed before volatile load on PPC64.
2411 
2412   // Check field type.
2413   __ rldicl(Rflags, Rflags, 64-ConstantPoolCacheEntry::tos_state_shift, 64-ConstantPoolCacheEntry::tos_state_bits);
2414 
2415 #ifdef ASSERT
2416   Label LFlagInvalid;
2417   __ cmpldi(CCR0, Rflags, number_of_states);
2418   __ bge(CCR0, LFlagInvalid);
2419 #endif
2420 
2421   // Load from branch table and dispatch (volatile case: one instruction ahead).
2422   __ sldi(Rflags, Rflags, LogBytesPerWord);
2423   __ cmpwi(CCR6, Rscratch, 1); // Volatile?
2424   if (support_IRIW_for_not_multiple_copy_atomic_cpu) {
2425     __ sldi(Rscratch, Rscratch, exact_log2(BytesPerInstWord)); // Volatile ? size of 1 instruction : 0.
2426   }
2427   __ ldx(Rbtable, Rbtable, Rflags);
2428 
2429   // Get the obj from stack.
2430   if (!is_static) {
2431     pop_and_check_object(Rclass_or_obj); // Kills R11_scratch1.
2432   } else {
2433     __ verify_oop(Rclass_or_obj);
2434   }
2435 
2436   if (support_IRIW_for_not_multiple_copy_atomic_cpu) {
2437     __ subf(Rbtable, Rscratch, Rbtable); // Point to volatile/non-volatile entry point.
2438   }
2439   __ mtctr(Rbtable);
2440   __ bctr();
2441 
2442 #ifdef ASSERT
2443   __ bind(LFlagInvalid);
2444   __ stop("got invalid flag", 0x654);
2445 #endif
2446 
2447   if (!is_static && rc == may_not_rewrite) {
2448     // We reuse the code from is_static.  It's jumped to via the table above.
2449     return;
2450   }
2451 
2452 #ifdef ASSERT
2453   // __ bind(Lvtos);
2454   address pc_before_fence = __ pc();
2455   __ fence(); // Volatile entry point (one instruction before non-volatile_entry point).
2456   assert(__ pc() - pc_before_fence == (ptrdiff_t)BytesPerInstWord, "must be single instruction");
2457   assert(branch_table[vtos] == 0, "can't compute twice");
2458   branch_table[vtos] = __ pc(); // non-volatile_entry point
2459   __ stop("vtos unexpected", 0x655);
2460 #endif
2461 
2462   __ align(32, 28, 28); // Align load.
2463   // __ bind(Ldtos);
2464   __ fence(); // Volatile entry point (one instruction before non-volatile_entry point).
2465   assert(branch_table[dtos] == 0, "can't compute twice");
2466   branch_table[dtos] = __ pc(); // non-volatile_entry point
2467   __ lfdx(F15_ftos, Rclass_or_obj, Roffset);
2468   __ push(dtos);
2469   if (!is_static && rc == may_rewrite) {
2470     patch_bytecode(Bytecodes::_fast_dgetfield, Rbc, Rscratch);
2471   }
2472   {
2473     Label acquire_double;
2474     __ beq(CCR6, acquire_double); // Volatile?
2475     __ dispatch_epilog(vtos, Bytecodes::length_for(bytecode()));
2476 
2477     __ bind(acquire_double);
2478     __ fcmpu(CCR0, F15_ftos, F15_ftos); // Acquire by cmp-br-isync.
2479     __ beq_predict_taken(CCR0, Lisync);
2480     __ b(Lisync); // In case of NAN.
2481   }
2482 
2483   __ align(32, 28, 28); // Align load.
2484   // __ bind(Lftos);
2485   __ fence(); // Volatile entry point (one instruction before non-volatile_entry point).
2486   assert(branch_table[ftos] == 0, "can't compute twice");
2487   branch_table[ftos] = __ pc(); // non-volatile_entry point
2488   __ lfsx(F15_ftos, Rclass_or_obj, Roffset);
2489   __ push(ftos);
2490   if (!is_static && rc == may_rewrite) {
2491     patch_bytecode(Bytecodes::_fast_fgetfield, Rbc, Rscratch);
2492   }
2493   {
2494     Label acquire_float;
2495     __ beq(CCR6, acquire_float); // Volatile?
2496     __ dispatch_epilog(vtos, Bytecodes::length_for(bytecode()));
2497 
2498     __ bind(acquire_float);
2499     __ fcmpu(CCR0, F15_ftos, F15_ftos); // Acquire by cmp-br-isync.
2500     __ beq_predict_taken(CCR0, Lisync);
2501     __ b(Lisync); // In case of NAN.
2502   }
2503 
2504   __ align(32, 28, 28); // Align load.
2505   // __ bind(Litos);
2506   __ fence(); // Volatile entry point (one instruction before non-volatile_entry point).
2507   assert(branch_table[itos] == 0, "can't compute twice");
2508   branch_table[itos] = __ pc(); // non-volatile_entry point
2509   __ lwax(R17_tos, Rclass_or_obj, Roffset);
2510   __ push(itos);
2511   if (!is_static && rc == may_rewrite) {
2512     patch_bytecode(Bytecodes::_fast_igetfield, Rbc, Rscratch);
2513   }
2514   __ beq(CCR6, Lacquire); // Volatile?
2515   __ dispatch_epilog(vtos, Bytecodes::length_for(bytecode()));
2516 
2517   __ align(32, 28, 28); // Align load.
2518   // __ bind(Lltos);
2519   __ fence(); // Volatile entry point (one instruction before non-volatile_entry point).
2520   assert(branch_table[ltos] == 0, "can't compute twice");
2521   branch_table[ltos] = __ pc(); // non-volatile_entry point
2522   __ ldx(R17_tos, Rclass_or_obj, Roffset);
2523   __ push(ltos);
2524   if (!is_static && rc == may_rewrite) {
2525     patch_bytecode(Bytecodes::_fast_lgetfield, Rbc, Rscratch);
2526   }
2527   __ beq(CCR6, Lacquire); // Volatile?
2528   __ dispatch_epilog(vtos, Bytecodes::length_for(bytecode()));
2529 
2530   __ align(32, 28, 28); // Align load.
2531   // __ bind(Lbtos);
2532   __ fence(); // Volatile entry point (one instruction before non-volatile_entry point).
2533   assert(branch_table[btos] == 0, "can't compute twice");
2534   branch_table[btos] = __ pc(); // non-volatile_entry point
2535   __ lbzx(R17_tos, Rclass_or_obj, Roffset);
2536   __ extsb(R17_tos, R17_tos);
2537   __ push(btos);
2538   if (!is_static && rc == may_rewrite) {
2539     patch_bytecode(Bytecodes::_fast_bgetfield, Rbc, Rscratch);
2540   }
2541   __ beq(CCR6, Lacquire); // Volatile?
2542   __ dispatch_epilog(vtos, Bytecodes::length_for(bytecode()));
2543 
2544   __ align(32, 28, 28); // Align load.
2545   // __ bind(Lctos);
2546   __ fence(); // Volatile entry point (one instruction before non-volatile_entry point).
2547   assert(branch_table[ctos] == 0, "can't compute twice");
2548   branch_table[ctos] = __ pc(); // non-volatile_entry point
2549   __ lhzx(R17_tos, Rclass_or_obj, Roffset);
2550   __ push(ctos);
2551   if (!is_static && rc == may_rewrite) {
2552     patch_bytecode(Bytecodes::_fast_cgetfield, Rbc, Rscratch);
2553   }
2554   __ beq(CCR6, Lacquire); // Volatile?
2555   __ dispatch_epilog(vtos, Bytecodes::length_for(bytecode()));
2556 
2557   __ align(32, 28, 28); // Align load.
2558   // __ bind(Lstos);
2559   __ fence(); // Volatile entry point (one instruction before non-volatile_entry point).
2560   assert(branch_table[stos] == 0, "can't compute twice");
2561   branch_table[stos] = __ pc(); // non-volatile_entry point
2562   __ lhax(R17_tos, Rclass_or_obj, Roffset);
2563   __ push(stos);
2564   if (!is_static && rc == may_rewrite) {
2565     patch_bytecode(Bytecodes::_fast_sgetfield, Rbc, Rscratch);
2566   }
2567   __ beq(CCR6, Lacquire); // Volatile?
2568   __ dispatch_epilog(vtos, Bytecodes::length_for(bytecode()));
2569 
2570   __ align(32, 28, 28); // Align load.
2571   // __ bind(Latos);
2572   __ fence(); // Volatile entry point (one instruction before non-volatile_entry point).
2573   assert(branch_table[atos] == 0, "can't compute twice");
2574   branch_table[atos] = __ pc(); // non-volatile_entry point
2575   __ load_heap_oop(R17_tos, (RegisterOrConstant)Roffset, Rclass_or_obj);
2576   __ verify_oop(R17_tos);
2577   __ push(atos);
2578   //__ dcbt(R17_tos); // prefetch
2579   if (!is_static && rc == may_rewrite) {
2580     patch_bytecode(Bytecodes::_fast_agetfield, Rbc, Rscratch);
2581   }
2582   __ beq(CCR6, Lacquire); // Volatile?
2583   __ dispatch_epilog(vtos, Bytecodes::length_for(bytecode()));
2584 
2585   __ align(32, 12);
2586   __ bind(Lacquire);
2587   __ twi_0(R17_tos);
2588   __ bind(Lisync);
2589   __ isync(); // acquire
2590 
2591 #ifdef ASSERT
2592   for (int i = 0; i<number_of_states; ++i) {
2593     assert(branch_table[i], "get initialization");
2594     //tty->print_cr("get: %s_branch_table[%d] = 0x%llx (opcode 0x%llx)",
2595     //              is_static ? "static" : "field", i, branch_table[i], *((unsigned int*)branch_table[i]));
2596   }
2597 #endif
2598 }
2599 
2600 void TemplateTable::getfield(int byte_no) {
2601   getfield_or_static(byte_no, false);
2602 }
2603 
2604 void TemplateTable::nofast_getfield(int byte_no) {
2605   getfield_or_static(byte_no, false, may_not_rewrite);
2606 }
2607 
2608 void TemplateTable::getstatic(int byte_no) {
2609   getfield_or_static(byte_no, true);
2610 }
2611 
2612 // The registers cache and index expected to be set before call.
2613 // The function may destroy various registers, just not the cache and index registers.
2614 void TemplateTable::jvmti_post_field_mod(Register Rcache, Register Rscratch, bool is_static) {
2615 
2616   assert_different_registers(Rcache, Rscratch, R6_ARG4);
2617 
2618   if (JvmtiExport::can_post_field_modification()) {
2619     Label Lno_field_mod_post;
2620 
2621     // Check if post field access in enabled.
2622     int offs = __ load_const_optimized(Rscratch, JvmtiExport::get_field_modification_count_addr(), R0, true);
2623     __ lwz(Rscratch, offs, Rscratch);
2624 
2625     __ cmpwi(CCR0, Rscratch, 0);
2626     __ beq(CCR0, Lno_field_mod_post);
2627 
2628     // Do the post
2629     ByteSize cp_base_offset = ConstantPoolCache::base_offset();
2630     const Register Robj = Rscratch;
2631 
2632     __ addi(Rcache, Rcache, in_bytes(cp_base_offset));
2633     if (is_static) {
2634       // Life is simple. Null out the object pointer.
2635       __ li(Robj, 0);
2636     } else {
2637       // In case of the fast versions, value lives in registers => put it back on tos.
2638       int offs = Interpreter::expr_offset_in_bytes(0);
2639       Register base = R15_esp;
2640       switch(bytecode()) {
2641         case Bytecodes::_fast_aputfield: __ push_ptr(); offs+= Interpreter::stackElementSize; break;
2642         case Bytecodes::_fast_iputfield: // Fall through
2643         case Bytecodes::_fast_bputfield: // Fall through
2644         case Bytecodes::_fast_cputfield: // Fall through
2645         case Bytecodes::_fast_sputfield: __ push_i(); offs+=  Interpreter::stackElementSize; break;
2646         case Bytecodes::_fast_lputfield: __ push_l(); offs+=2*Interpreter::stackElementSize; break;
2647         case Bytecodes::_fast_fputfield: __ push_f(); offs+=  Interpreter::stackElementSize; break;
2648         case Bytecodes::_fast_dputfield: __ push_d(); offs+=2*Interpreter::stackElementSize; break;
2649         default: {
2650           offs = 0;
2651           base = Robj;
2652           const Register Rflags = Robj;
2653           Label is_one_slot;
2654           // Life is harder. The stack holds the value on top, followed by the
2655           // object. We don't know the size of the value, though; it could be
2656           // one or two words depending on its type. As a result, we must find
2657           // the type to determine where the object is.
2658           __ ld(Rflags, in_bytes(ConstantPoolCacheEntry::flags_offset()), Rcache); // Big Endian
2659           __ rldicl(Rflags, Rflags, 64-ConstantPoolCacheEntry::tos_state_shift, 64-ConstantPoolCacheEntry::tos_state_bits);
2660 
2661           __ cmpwi(CCR0, Rflags, ltos);
2662           __ cmpwi(CCR1, Rflags, dtos);
2663           __ addi(base, R15_esp, Interpreter::expr_offset_in_bytes(1));
2664           __ crnor(CCR0, Assembler::equal, CCR1, Assembler::equal);
2665           __ beq(CCR0, is_one_slot);
2666           __ addi(base, R15_esp, Interpreter::expr_offset_in_bytes(2));
2667           __ bind(is_one_slot);
2668           break;
2669         }
2670       }
2671       __ ld(Robj, offs, base);
2672       __ verify_oop(Robj);
2673     }
2674 
2675     __ addi(R6_ARG4, R15_esp, Interpreter::expr_offset_in_bytes(0));
2676     __ call_VM(noreg, CAST_FROM_FN_PTR(address, InterpreterRuntime::post_field_modification), Robj, Rcache, R6_ARG4);
2677     __ get_cache_and_index_at_bcp(Rcache, 1);
2678 
2679     // In case of the fast versions, value lives in registers => put it back on tos.
2680     switch(bytecode()) {
2681       case Bytecodes::_fast_aputfield: __ pop_ptr(); break;
2682       case Bytecodes::_fast_iputfield: // Fall through
2683       case Bytecodes::_fast_bputfield: // Fall through
2684       case Bytecodes::_fast_cputfield: // Fall through
2685       case Bytecodes::_fast_sputfield: __ pop_i(); break;
2686       case Bytecodes::_fast_lputfield: __ pop_l(); break;
2687       case Bytecodes::_fast_fputfield: __ pop_f(); break;
2688       case Bytecodes::_fast_dputfield: __ pop_d(); break;
2689       default: break; // Nothin' to do.
2690     }
2691 
2692     __ align(32, 12);
2693     __ bind(Lno_field_mod_post);
2694   }
2695 }
2696 
2697 // PPC64: implement volatile stores as release-store (return bytecode contains an additional release).
2698 void TemplateTable::putfield_or_static(int byte_no, bool is_static, RewriteControl rc) {
2699   Label Lvolatile;
2700 
2701   const Register Rcache        = R5_ARG3,  // Do not use ARG1/2 (causes trouble in jvmti_post_field_mod).
2702                  Rclass_or_obj = R31,      // Needs to survive C call.
2703                  Roffset       = R22_tmp2, // Needs to survive C call.
2704                  Rflags        = R3_ARG1,
2705                  Rbtable       = R4_ARG2,
2706                  Rscratch      = R11_scratch1,
2707                  Rscratch2     = R12_scratch2,
2708                  Rscratch3     = R6_ARG4,
2709                  Rbc           = Rscratch3;
2710   const ConditionRegister CR_is_vol = CCR2; // Non-volatile condition register (survives runtime call in do_oop_store).
2711 
2712   static address field_rw_branch_table[number_of_states],
2713                  field_norw_branch_table[number_of_states],
2714                  static_branch_table[number_of_states];
2715 
2716   address* branch_table = is_static ? static_branch_table :
2717     (rc == may_rewrite ? field_rw_branch_table : field_norw_branch_table);
2718 
2719   // Stack (grows up):
2720   //  value
2721   //  obj
2722 
2723   // Load the field offset.
2724   resolve_cache_and_index(byte_no, Rcache, Rscratch, sizeof(u2));
2725   jvmti_post_field_mod(Rcache, Rscratch, is_static);
2726   load_field_cp_cache_entry(Rclass_or_obj, Rcache, noreg, Roffset, Rflags, is_static);
2727 
2728   // Load pointer to branch table.
2729   __ load_const_optimized(Rbtable, (address)branch_table, Rscratch);
2730 
2731   // Get volatile flag.
2732   __ rldicl(Rscratch, Rflags, 64-ConstantPoolCacheEntry::is_volatile_shift, 63); // Extract volatile bit.
2733 
2734   // Check the field type.
2735   __ rldicl(Rflags, Rflags, 64-ConstantPoolCacheEntry::tos_state_shift, 64-ConstantPoolCacheEntry::tos_state_bits);
2736 
2737 #ifdef ASSERT
2738   Label LFlagInvalid;
2739   __ cmpldi(CCR0, Rflags, number_of_states);
2740   __ bge(CCR0, LFlagInvalid);
2741 #endif
2742 
2743   // Load from branch table and dispatch (volatile case: one instruction ahead).
2744   __ sldi(Rflags, Rflags, LogBytesPerWord);
2745   if (!support_IRIW_for_not_multiple_copy_atomic_cpu) {
2746     __ cmpwi(CR_is_vol, Rscratch, 1);  // Volatile?
2747   }
2748   __ sldi(Rscratch, Rscratch, exact_log2(BytesPerInstWord)); // Volatile? size of instruction 1 : 0.
2749   __ ldx(Rbtable, Rbtable, Rflags);
2750 
2751   __ subf(Rbtable, Rscratch, Rbtable); // Point to volatile/non-volatile entry point.
2752   __ mtctr(Rbtable);
2753   __ bctr();
2754 
2755 #ifdef ASSERT
2756   __ bind(LFlagInvalid);
2757   __ stop("got invalid flag", 0x656);
2758 
2759   // __ bind(Lvtos);
2760   address pc_before_release = __ pc();
2761   __ release(); // Volatile entry point (one instruction before non-volatile_entry point).
2762   assert(__ pc() - pc_before_release == (ptrdiff_t)BytesPerInstWord, "must be single instruction");
2763   assert(branch_table[vtos] == 0, "can't compute twice");
2764   branch_table[vtos] = __ pc(); // non-volatile_entry point
2765   __ stop("vtos unexpected", 0x657);
2766 #endif
2767 
2768   __ align(32, 28, 28); // Align pop.
2769   // __ bind(Ldtos);
2770   __ release(); // Volatile entry point (one instruction before non-volatile_entry point).
2771   assert(branch_table[dtos] == 0, "can't compute twice");
2772   branch_table[dtos] = __ pc(); // non-volatile_entry point
2773   __ pop(dtos);
2774   if (!is_static) {
2775     pop_and_check_object(Rclass_or_obj);  // Kills R11_scratch1.
2776   }
2777   __ stfdx(F15_ftos, Rclass_or_obj, Roffset);
2778   if (!is_static && rc == may_rewrite) {
2779     patch_bytecode(Bytecodes::_fast_dputfield, Rbc, Rscratch, true, byte_no);
2780   }
2781   if (!support_IRIW_for_not_multiple_copy_atomic_cpu) {
2782     __ beq(CR_is_vol, Lvolatile); // Volatile?
2783   }
2784   __ dispatch_epilog(vtos, Bytecodes::length_for(bytecode()));
2785 
2786   __ align(32, 28, 28); // Align pop.
2787   // __ bind(Lftos);
2788   __ release(); // Volatile entry point (one instruction before non-volatile_entry point).
2789   assert(branch_table[ftos] == 0, "can't compute twice");
2790   branch_table[ftos] = __ pc(); // non-volatile_entry point
2791   __ pop(ftos);
2792   if (!is_static) { pop_and_check_object(Rclass_or_obj); } // Kills R11_scratch1.
2793   __ stfsx(F15_ftos, Rclass_or_obj, Roffset);
2794   if (!is_static && rc == may_rewrite) {
2795     patch_bytecode(Bytecodes::_fast_fputfield, Rbc, Rscratch, true, byte_no);
2796   }
2797   if (!support_IRIW_for_not_multiple_copy_atomic_cpu) {
2798     __ beq(CR_is_vol, Lvolatile); // Volatile?
2799   }
2800   __ dispatch_epilog(vtos, Bytecodes::length_for(bytecode()));
2801 
2802   __ align(32, 28, 28); // Align pop.
2803   // __ bind(Litos);
2804   __ release(); // Volatile entry point (one instruction before non-volatile_entry point).
2805   assert(branch_table[itos] == 0, "can't compute twice");
2806   branch_table[itos] = __ pc(); // non-volatile_entry point
2807   __ pop(itos);
2808   if (!is_static) { pop_and_check_object(Rclass_or_obj); } // Kills R11_scratch1.
2809   __ stwx(R17_tos, Rclass_or_obj, Roffset);
2810   if (!is_static && rc == may_rewrite) {
2811     patch_bytecode(Bytecodes::_fast_iputfield, Rbc, Rscratch, true, byte_no);
2812   }
2813   if (!support_IRIW_for_not_multiple_copy_atomic_cpu) {
2814     __ beq(CR_is_vol, Lvolatile); // Volatile?
2815   }
2816   __ dispatch_epilog(vtos, Bytecodes::length_for(bytecode()));
2817 
2818   __ align(32, 28, 28); // Align pop.
2819   // __ bind(Lltos);
2820   __ release(); // Volatile entry point (one instruction before non-volatile_entry point).
2821   assert(branch_table[ltos] == 0, "can't compute twice");
2822   branch_table[ltos] = __ pc(); // non-volatile_entry point
2823   __ pop(ltos);
2824   if (!is_static) { pop_and_check_object(Rclass_or_obj); } // Kills R11_scratch1.
2825   __ stdx(R17_tos, Rclass_or_obj, Roffset);
2826   if (!is_static && rc == may_rewrite) {
2827     patch_bytecode(Bytecodes::_fast_lputfield, Rbc, Rscratch, true, byte_no);
2828   }
2829   if (!support_IRIW_for_not_multiple_copy_atomic_cpu) {
2830     __ beq(CR_is_vol, Lvolatile); // Volatile?
2831   }
2832   __ dispatch_epilog(vtos, Bytecodes::length_for(bytecode()));
2833 
2834   __ align(32, 28, 28); // Align pop.
2835   // __ bind(Lbtos);
2836   __ release(); // Volatile entry point (one instruction before non-volatile_entry point).
2837   assert(branch_table[btos] == 0, "can't compute twice");
2838   branch_table[btos] = __ pc(); // non-volatile_entry point
2839   __ pop(btos);
2840   if (!is_static) { pop_and_check_object(Rclass_or_obj); } // Kills R11_scratch1.
2841   __ stbx(R17_tos, Rclass_or_obj, Roffset);
2842   if (!is_static && rc == may_rewrite) {
2843     patch_bytecode(Bytecodes::_fast_bputfield, Rbc, Rscratch, true, byte_no);
2844   }
2845   if (!support_IRIW_for_not_multiple_copy_atomic_cpu) {
2846     __ beq(CR_is_vol, Lvolatile); // Volatile?
2847   }
2848   __ dispatch_epilog(vtos, Bytecodes::length_for(bytecode()));
2849 
2850   __ align(32, 28, 28); // Align pop.
2851   // __ bind(Lctos);
2852   __ release(); // Volatile entry point (one instruction before non-volatile_entry point).
2853   assert(branch_table[ctos] == 0, "can't compute twice");
2854   branch_table[ctos] = __ pc(); // non-volatile_entry point
2855   __ pop(ctos);
2856   if (!is_static) { pop_and_check_object(Rclass_or_obj); } // Kills R11_scratch1..
2857   __ sthx(R17_tos, Rclass_or_obj, Roffset);
2858   if (!is_static && rc == may_rewrite) {
2859     patch_bytecode(Bytecodes::_fast_cputfield, Rbc, Rscratch, true, byte_no);
2860   }
2861   if (!support_IRIW_for_not_multiple_copy_atomic_cpu) {
2862     __ beq(CR_is_vol, Lvolatile); // Volatile?
2863   }
2864   __ dispatch_epilog(vtos, Bytecodes::length_for(bytecode()));
2865 
2866   __ align(32, 28, 28); // Align pop.
2867   // __ bind(Lstos);
2868   __ release(); // Volatile entry point (one instruction before non-volatile_entry point).
2869   assert(branch_table[stos] == 0, "can't compute twice");
2870   branch_table[stos] = __ pc(); // non-volatile_entry point
2871   __ pop(stos);
2872   if (!is_static) { pop_and_check_object(Rclass_or_obj); } // Kills R11_scratch1.
2873   __ sthx(R17_tos, Rclass_or_obj, Roffset);
2874   if (!is_static && rc == may_rewrite) {
2875     patch_bytecode(Bytecodes::_fast_sputfield, Rbc, Rscratch, true, byte_no);
2876   }
2877   if (!support_IRIW_for_not_multiple_copy_atomic_cpu) {
2878     __ beq(CR_is_vol, Lvolatile); // Volatile?
2879   }
2880   __ dispatch_epilog(vtos, Bytecodes::length_for(bytecode()));
2881 
2882   __ align(32, 28, 28); // Align pop.
2883   // __ bind(Latos);
2884   __ release(); // Volatile entry point (one instruction before non-volatile_entry point).
2885   assert(branch_table[atos] == 0, "can't compute twice");
2886   branch_table[atos] = __ pc(); // non-volatile_entry point
2887   __ pop(atos);
2888   if (!is_static) { pop_and_check_object(Rclass_or_obj); } // kills R11_scratch1
2889   do_oop_store(_masm, Rclass_or_obj, Roffset, R17_tos, Rscratch, Rscratch2, Rscratch3, _bs->kind(), false /* precise */, true /* check null */);
2890   if (!is_static && rc == may_rewrite) {
2891     patch_bytecode(Bytecodes::_fast_aputfield, Rbc, Rscratch, true, byte_no);
2892   }
2893   if (!support_IRIW_for_not_multiple_copy_atomic_cpu) {
2894     __ beq(CR_is_vol, Lvolatile); // Volatile?
2895     __ dispatch_epilog(vtos, Bytecodes::length_for(bytecode()));
2896 
2897     __ align(32, 12);
2898     __ bind(Lvolatile);
2899     __ fence();
2900   }
2901   // fallthru: __ b(Lexit);
2902 
2903 #ifdef ASSERT
2904   for (int i = 0; i<number_of_states; ++i) {
2905     assert(branch_table[i], "put initialization");
2906     //tty->print_cr("put: %s_branch_table[%d] = 0x%llx (opcode 0x%llx)",
2907     //              is_static ? "static" : "field", i, branch_table[i], *((unsigned int*)branch_table[i]));
2908   }
2909 #endif
2910 }
2911 
2912 void TemplateTable::putfield(int byte_no) {
2913   putfield_or_static(byte_no, false);
2914 }
2915 
2916 void TemplateTable::nofast_putfield(int byte_no) {
2917   putfield_or_static(byte_no, false, may_not_rewrite);
2918 }
2919 
2920 void TemplateTable::putstatic(int byte_no) {
2921   putfield_or_static(byte_no, true);
2922 }
2923 
2924 // See SPARC. On PPC64, we have a different jvmti_post_field_mod which does the job.
2925 void TemplateTable::jvmti_post_fast_field_mod() {
2926   __ should_not_reach_here();
2927 }
2928 
2929 void TemplateTable::fast_storefield(TosState state) {
2930   transition(state, vtos);
2931 
2932   const Register Rcache        = R5_ARG3,  // Do not use ARG1/2 (causes trouble in jvmti_post_field_mod).
2933                  Rclass_or_obj = R31,      // Needs to survive C call.
2934                  Roffset       = R22_tmp2, // Needs to survive C call.
2935                  Rflags        = R3_ARG1,
2936                  Rscratch      = R11_scratch1,
2937                  Rscratch2     = R12_scratch2,
2938                  Rscratch3     = R4_ARG2;
2939   const ConditionRegister CR_is_vol = CCR2; // Non-volatile condition register (survives runtime call in do_oop_store).
2940 
2941   // Constant pool already resolved => Load flags and offset of field.
2942   __ get_cache_and_index_at_bcp(Rcache, 1);
2943   jvmti_post_field_mod(Rcache, Rscratch, false /* not static */);
2944   load_field_cp_cache_entry(noreg, Rcache, noreg, Roffset, Rflags, false);
2945 
2946   // Get the obj and the final store addr.
2947   pop_and_check_object(Rclass_or_obj); // Kills R11_scratch1.
2948 
2949   // Get volatile flag.
2950   __ rldicl_(Rscratch, Rflags, 64-ConstantPoolCacheEntry::is_volatile_shift, 63); // Extract volatile bit.
2951   if (!support_IRIW_for_not_multiple_copy_atomic_cpu) { __ cmpdi(CR_is_vol, Rscratch, 1); }
2952   {
2953     Label LnotVolatile;
2954     __ beq(CCR0, LnotVolatile);
2955     __ release();
2956     __ align(32, 12);
2957     __ bind(LnotVolatile);
2958   }
2959 
2960   // Do the store and fencing.
2961   switch(bytecode()) {
2962     case Bytecodes::_fast_aputfield:
2963       // Store into the field.
2964       do_oop_store(_masm, Rclass_or_obj, Roffset, R17_tos, Rscratch, Rscratch2, Rscratch3, _bs->kind(), false /* precise */, true /* check null */);
2965       break;
2966 
2967     case Bytecodes::_fast_iputfield:
2968       __ stwx(R17_tos, Rclass_or_obj, Roffset);
2969       break;
2970 
2971     case Bytecodes::_fast_lputfield:
2972       __ stdx(R17_tos, Rclass_or_obj, Roffset);
2973       break;
2974 
2975     case Bytecodes::_fast_bputfield:
2976       __ stbx(R17_tos, Rclass_or_obj, Roffset);
2977       break;
2978 
2979     case Bytecodes::_fast_cputfield:
2980     case Bytecodes::_fast_sputfield:
2981       __ sthx(R17_tos, Rclass_or_obj, Roffset);
2982       break;
2983 
2984     case Bytecodes::_fast_fputfield:
2985       __ stfsx(F15_ftos, Rclass_or_obj, Roffset);
2986       break;
2987 
2988     case Bytecodes::_fast_dputfield:
2989       __ stfdx(F15_ftos, Rclass_or_obj, Roffset);
2990       break;
2991 
2992     default: ShouldNotReachHere();
2993   }
2994 
2995   if (!support_IRIW_for_not_multiple_copy_atomic_cpu) {
2996     Label LVolatile;
2997     __ beq(CR_is_vol, LVolatile);
2998     __ dispatch_epilog(vtos, Bytecodes::length_for(bytecode()));
2999 
3000     __ align(32, 12);
3001     __ bind(LVolatile);
3002     __ fence();
3003   }
3004 }
3005 
3006 void TemplateTable::fast_accessfield(TosState state) {
3007   transition(atos, state);
3008 
3009   Label LisVolatile;
3010   ByteSize cp_base_offset = ConstantPoolCache::base_offset();
3011 
3012   const Register Rcache        = R3_ARG1,
3013                  Rclass_or_obj = R17_tos,
3014                  Roffset       = R22_tmp2,
3015                  Rflags        = R23_tmp3,
3016                  Rscratch      = R12_scratch2;
3017 
3018   // Constant pool already resolved. Get the field offset.
3019   __ get_cache_and_index_at_bcp(Rcache, 1);
3020   load_field_cp_cache_entry(noreg, Rcache, noreg, Roffset, Rflags, false);
3021 
3022   // JVMTI support
3023   jvmti_post_field_access(Rcache, Rscratch, false, true);
3024 
3025   // Get the load address.
3026   __ null_check_throw(Rclass_or_obj, -1, Rscratch);
3027 
3028   // Get volatile flag.
3029   __ rldicl_(Rscratch, Rflags, 64-ConstantPoolCacheEntry::is_volatile_shift, 63); // Extract volatile bit.
3030   __ bne(CCR0, LisVolatile);
3031 
3032   switch(bytecode()) {
3033     case Bytecodes::_fast_agetfield:
3034     {
3035       __ load_heap_oop(R17_tos, (RegisterOrConstant)Roffset, Rclass_or_obj);
3036       __ verify_oop(R17_tos);
3037       __ dispatch_epilog(state, Bytecodes::length_for(bytecode()));
3038 
3039       __ bind(LisVolatile);
3040       if (support_IRIW_for_not_multiple_copy_atomic_cpu) { __ fence(); }
3041       __ load_heap_oop(R17_tos, (RegisterOrConstant)Roffset, Rclass_or_obj);
3042       __ verify_oop(R17_tos);
3043       __ twi_0(R17_tos);
3044       __ isync();
3045       break;
3046     }
3047     case Bytecodes::_fast_igetfield:
3048     {
3049       __ lwax(R17_tos, Rclass_or_obj, Roffset);
3050       __ dispatch_epilog(state, Bytecodes::length_for(bytecode()));
3051 
3052       __ bind(LisVolatile);
3053       if (support_IRIW_for_not_multiple_copy_atomic_cpu) { __ fence(); }
3054       __ lwax(R17_tos, Rclass_or_obj, Roffset);
3055       __ twi_0(R17_tos);
3056       __ isync();
3057       break;
3058     }
3059     case Bytecodes::_fast_lgetfield:
3060     {
3061       __ ldx(R17_tos, Rclass_or_obj, Roffset);
3062       __ dispatch_epilog(state, Bytecodes::length_for(bytecode()));
3063 
3064       __ bind(LisVolatile);
3065       if (support_IRIW_for_not_multiple_copy_atomic_cpu) { __ fence(); }
3066       __ ldx(R17_tos, Rclass_or_obj, Roffset);
3067       __ twi_0(R17_tos);
3068       __ isync();
3069       break;
3070     }
3071     case Bytecodes::_fast_bgetfield:
3072     {
3073       __ lbzx(R17_tos, Rclass_or_obj, Roffset);
3074       __ extsb(R17_tos, R17_tos);
3075       __ dispatch_epilog(state, Bytecodes::length_for(bytecode()));
3076 
3077       __ bind(LisVolatile);
3078       if (support_IRIW_for_not_multiple_copy_atomic_cpu) { __ fence(); }
3079       __ lbzx(R17_tos, Rclass_or_obj, Roffset);
3080       __ twi_0(R17_tos);
3081       __ extsb(R17_tos, R17_tos);
3082       __ isync();
3083       break;
3084     }
3085     case Bytecodes::_fast_cgetfield:
3086     {
3087       __ lhzx(R17_tos, Rclass_or_obj, Roffset);
3088       __ dispatch_epilog(state, Bytecodes::length_for(bytecode()));
3089 
3090       __ bind(LisVolatile);
3091       if (support_IRIW_for_not_multiple_copy_atomic_cpu) { __ fence(); }
3092       __ lhzx(R17_tos, Rclass_or_obj, Roffset);
3093       __ twi_0(R17_tos);
3094       __ isync();
3095       break;
3096     }
3097     case Bytecodes::_fast_sgetfield:
3098     {
3099       __ lhax(R17_tos, Rclass_or_obj, Roffset);
3100       __ dispatch_epilog(state, Bytecodes::length_for(bytecode()));
3101 
3102       __ bind(LisVolatile);
3103       if (support_IRIW_for_not_multiple_copy_atomic_cpu) { __ fence(); }
3104       __ lhax(R17_tos, Rclass_or_obj, Roffset);
3105       __ twi_0(R17_tos);
3106       __ isync();
3107       break;
3108     }
3109     case Bytecodes::_fast_fgetfield:
3110     {
3111       __ lfsx(F15_ftos, Rclass_or_obj, Roffset);
3112       __ dispatch_epilog(state, Bytecodes::length_for(bytecode()));
3113 
3114       __ bind(LisVolatile);
3115       Label Ldummy;
3116       if (support_IRIW_for_not_multiple_copy_atomic_cpu) { __ fence(); }
3117       __ lfsx(F15_ftos, Rclass_or_obj, Roffset);
3118       __ fcmpu(CCR0, F15_ftos, F15_ftos); // Acquire by cmp-br-isync.
3119       __ bne_predict_not_taken(CCR0, Ldummy);
3120       __ bind(Ldummy);
3121       __ isync();
3122       break;
3123     }
3124     case Bytecodes::_fast_dgetfield:
3125     {
3126       __ lfdx(F15_ftos, Rclass_or_obj, Roffset);
3127       __ dispatch_epilog(state, Bytecodes::length_for(bytecode()));
3128 
3129       __ bind(LisVolatile);
3130       Label Ldummy;
3131       if (support_IRIW_for_not_multiple_copy_atomic_cpu) { __ fence(); }
3132       __ lfdx(F15_ftos, Rclass_or_obj, Roffset);
3133       __ fcmpu(CCR0, F15_ftos, F15_ftos); // Acquire by cmp-br-isync.
3134       __ bne_predict_not_taken(CCR0, Ldummy);
3135       __ bind(Ldummy);
3136       __ isync();
3137       break;
3138     }
3139     default: ShouldNotReachHere();
3140   }
3141 }
3142 
3143 void TemplateTable::fast_xaccess(TosState state) {
3144   transition(vtos, state);
3145 
3146   Label LisVolatile;
3147   ByteSize cp_base_offset = ConstantPoolCache::base_offset();
3148   const Register Rcache        = R3_ARG1,
3149                  Rclass_or_obj = R17_tos,
3150                  Roffset       = R22_tmp2,
3151                  Rflags        = R23_tmp3,
3152                  Rscratch      = R12_scratch2;
3153 
3154   __ ld(Rclass_or_obj, 0, R18_locals);
3155 
3156   // Constant pool already resolved. Get the field offset.
3157   __ get_cache_and_index_at_bcp(Rcache, 2);
3158   load_field_cp_cache_entry(noreg, Rcache, noreg, Roffset, Rflags, false);
3159 
3160   // JVMTI support not needed, since we switch back to single bytecode as soon as debugger attaches.
3161 
3162   // Needed to report exception at the correct bcp.
3163   __ addi(R14_bcp, R14_bcp, 1);
3164 
3165   // Get the load address.
3166   __ null_check_throw(Rclass_or_obj, -1, Rscratch);
3167 
3168   // Get volatile flag.
3169   __ rldicl_(Rscratch, Rflags, 64-ConstantPoolCacheEntry::is_volatile_shift, 63); // Extract volatile bit.
3170   __ bne(CCR0, LisVolatile);
3171 
3172   switch(state) {
3173   case atos:
3174     {
3175       __ load_heap_oop(R17_tos, (RegisterOrConstant)Roffset, Rclass_or_obj);
3176       __ verify_oop(R17_tos);
3177       __ dispatch_epilog(state, Bytecodes::length_for(bytecode()) - 1); // Undo bcp increment.
3178 
3179       __ bind(LisVolatile);
3180       if (support_IRIW_for_not_multiple_copy_atomic_cpu) { __ fence(); }
3181       __ load_heap_oop(R17_tos, (RegisterOrConstant)Roffset, Rclass_or_obj);
3182       __ verify_oop(R17_tos);
3183       __ twi_0(R17_tos);
3184       __ isync();
3185       break;
3186     }
3187   case itos:
3188     {
3189       __ lwax(R17_tos, Rclass_or_obj, Roffset);
3190       __ dispatch_epilog(state, Bytecodes::length_for(bytecode()) - 1); // Undo bcp increment.
3191 
3192       __ bind(LisVolatile);
3193       if (support_IRIW_for_not_multiple_copy_atomic_cpu) { __ fence(); }
3194       __ lwax(R17_tos, Rclass_or_obj, Roffset);
3195       __ twi_0(R17_tos);
3196       __ isync();
3197       break;
3198     }
3199   case ftos:
3200     {
3201       __ lfsx(F15_ftos, Rclass_or_obj, Roffset);
3202       __ dispatch_epilog(state, Bytecodes::length_for(bytecode()) - 1); // Undo bcp increment.
3203 
3204       __ bind(LisVolatile);
3205       Label Ldummy;
3206       if (support_IRIW_for_not_multiple_copy_atomic_cpu) { __ fence(); }
3207       __ lfsx(F15_ftos, Rclass_or_obj, Roffset);
3208       __ fcmpu(CCR0, F15_ftos, F15_ftos); // Acquire by cmp-br-isync.
3209       __ bne_predict_not_taken(CCR0, Ldummy);
3210       __ bind(Ldummy);
3211       __ isync();
3212       break;
3213     }
3214   default: ShouldNotReachHere();
3215   }
3216   __ addi(R14_bcp, R14_bcp, -1);
3217 }
3218 
3219 // ============================================================================
3220 // Calls
3221 
3222 // Common code for invoke
3223 //
3224 // Input:
3225 //   - byte_no
3226 //
3227 // Output:
3228 //   - Rmethod:        The method to invoke next.
3229 //   - Rret_addr:      The return address to return to.
3230 //   - Rindex:         MethodType (invokehandle) or CallSite obj (invokedynamic)
3231 //   - Rrecv:          Cache for "this" pointer, might be noreg if static call.
3232 //   - Rflags:         Method flags from const pool cache.
3233 //
3234 //  Kills:
3235 //   - Rscratch1
3236 //
3237 void TemplateTable::prepare_invoke(int byte_no,
3238                                    Register Rmethod,  // linked method (or i-klass)
3239                                    Register Rret_addr,// return address
3240                                    Register Rindex,   // itable index, MethodType, etc.
3241                                    Register Rrecv,    // If caller wants to see it.
3242                                    Register Rflags,   // If caller wants to test it.
3243                                    Register Rscratch
3244                                    ) {
3245   // Determine flags.
3246   const Bytecodes::Code code = bytecode();
3247   const bool is_invokeinterface  = code == Bytecodes::_invokeinterface;
3248   const bool is_invokedynamic    = code == Bytecodes::_invokedynamic;
3249   const bool is_invokehandle     = code == Bytecodes::_invokehandle;
3250   const bool is_invokevirtual    = code == Bytecodes::_invokevirtual;
3251   const bool is_invokespecial    = code == Bytecodes::_invokespecial;
3252   const bool load_receiver       = (Rrecv != noreg);
3253   assert(load_receiver == (code != Bytecodes::_invokestatic && code != Bytecodes::_invokedynamic), "");
3254 
3255   assert_different_registers(Rmethod, Rindex, Rflags, Rscratch);
3256   assert_different_registers(Rmethod, Rrecv, Rflags, Rscratch);
3257   assert_different_registers(Rret_addr, Rscratch);
3258 
3259   load_invoke_cp_cache_entry(byte_no, Rmethod, Rindex, Rflags, is_invokevirtual, false, is_invokedynamic);
3260 
3261   // Saving of SP done in call_from_interpreter.
3262 
3263   // Maybe push "appendix" to arguments.
3264   if (is_invokedynamic || is_invokehandle) {
3265     Label Ldone;
3266     __ rldicl_(R0, Rflags, 64-ConstantPoolCacheEntry::has_appendix_shift, 63);
3267     __ beq(CCR0, Ldone);
3268     // Push "appendix" (MethodType, CallSite, etc.).
3269     // This must be done before we get the receiver,
3270     // since the parameter_size includes it.
3271     __ load_resolved_reference_at_index(Rscratch, Rindex);
3272     __ verify_oop(Rscratch);
3273     __ push_ptr(Rscratch);
3274     __ bind(Ldone);
3275   }
3276 
3277   // Load receiver if needed (after appendix is pushed so parameter size is correct).
3278   if (load_receiver) {
3279     const Register Rparam_count = Rscratch;
3280     __ andi(Rparam_count, Rflags, ConstantPoolCacheEntry::parameter_size_mask);
3281     __ load_receiver(Rparam_count, Rrecv);
3282     __ verify_oop(Rrecv);
3283   }
3284 
3285   // Get return address.
3286   {
3287     Register Rtable_addr = Rscratch;
3288     Register Rret_type = Rret_addr;
3289     address table_addr = (address) Interpreter::invoke_return_entry_table_for(code);
3290 
3291     // Get return type. It's coded into the upper 4 bits of the lower half of the 64 bit value.
3292     __ rldicl(Rret_type, Rflags, 64-ConstantPoolCacheEntry::tos_state_shift, 64-ConstantPoolCacheEntry::tos_state_bits);
3293     __ load_dispatch_table(Rtable_addr, (address*)table_addr);
3294     __ sldi(Rret_type, Rret_type, LogBytesPerWord);
3295     // Get return address.
3296     __ ldx(Rret_addr, Rtable_addr, Rret_type);
3297   }
3298 }
3299 
3300 // Helper for virtual calls. Load target out of vtable and jump off!
3301 // Kills all passed registers.
3302 void TemplateTable::generate_vtable_call(Register Rrecv_klass, Register Rindex, Register Rret, Register Rtemp) {
3303 
3304   assert_different_registers(Rrecv_klass, Rtemp, Rret);
3305   const Register Rtarget_method = Rindex;
3306 
3307   // Get target method & entry point.
3308   const int base = InstanceKlass::vtable_start_offset() * wordSize;
3309   // Calc vtable addr scale the vtable index by 8.
3310   __ sldi(Rindex, Rindex, exact_log2(vtableEntry::size() * wordSize));
3311   // Load target.
3312   __ addi(Rrecv_klass, Rrecv_klass, base + vtableEntry::method_offset_in_bytes());
3313   __ ldx(Rtarget_method, Rindex, Rrecv_klass);
3314   // Argument and return type profiling.
3315   __ profile_arguments_type(Rtarget_method, Rrecv_klass /* scratch1 */, Rtemp /* scratch2 */, true);
3316   __ call_from_interpreter(Rtarget_method, Rret, Rrecv_klass /* scratch1 */, Rtemp /* scratch2 */);
3317 }
3318 
3319 // Virtual or final call. Final calls are rewritten on the fly to run through "fast_finalcall" next time.
3320 void TemplateTable::invokevirtual(int byte_no) {
3321   transition(vtos, vtos);
3322 
3323   Register Rtable_addr = R11_scratch1,
3324            Rret_type = R12_scratch2,
3325            Rret_addr = R5_ARG3,
3326            Rflags = R22_tmp2, // Should survive C call.
3327            Rrecv = R3_ARG1,
3328            Rrecv_klass = Rrecv,
3329            Rvtableindex_or_method = R31, // Should survive C call.
3330            Rnum_params = R4_ARG2,
3331            Rnew_bc = R6_ARG4;
3332 
3333   Label LnotFinal;
3334 
3335   load_invoke_cp_cache_entry(byte_no, Rvtableindex_or_method, noreg, Rflags, /*virtual*/ true, false, false);
3336 
3337   __ testbitdi(CCR0, R0, Rflags, ConstantPoolCacheEntry::is_vfinal_shift);
3338   __ bfalse(CCR0, LnotFinal);
3339 
3340   if (RewriteBytecodes && !UseSharedSpaces) {
3341     patch_bytecode(Bytecodes::_fast_invokevfinal, Rnew_bc, R12_scratch2);
3342   }
3343   invokevfinal_helper(Rvtableindex_or_method, Rflags, R11_scratch1, R12_scratch2);
3344 
3345   __ align(32, 12);
3346   __ bind(LnotFinal);
3347   // Load "this" pointer (receiver).
3348   __ rldicl(Rnum_params, Rflags, 64, 48);
3349   __ load_receiver(Rnum_params, Rrecv);
3350   __ verify_oop(Rrecv);
3351 
3352   // Get return type. It's coded into the upper 4 bits of the lower half of the 64 bit value.
3353   __ rldicl(Rret_type, Rflags, 64-ConstantPoolCacheEntry::tos_state_shift, 64-ConstantPoolCacheEntry::tos_state_bits);
3354   __ load_dispatch_table(Rtable_addr, Interpreter::invoke_return_entry_table());
3355   __ sldi(Rret_type, Rret_type, LogBytesPerWord);
3356   __ ldx(Rret_addr, Rret_type, Rtable_addr);
3357   __ null_check_throw(Rrecv, oopDesc::klass_offset_in_bytes(), R11_scratch1);
3358   __ load_klass(Rrecv_klass, Rrecv);
3359   __ verify_klass_ptr(Rrecv_klass);
3360   __ profile_virtual_call(Rrecv_klass, R11_scratch1, R12_scratch2, false);
3361 
3362   generate_vtable_call(Rrecv_klass, Rvtableindex_or_method, Rret_addr, R11_scratch1);
3363 }
3364 
3365 void TemplateTable::fast_invokevfinal(int byte_no) {
3366   transition(vtos, vtos);
3367 
3368   assert(byte_no == f2_byte, "use this argument");
3369   Register Rflags  = R22_tmp2,
3370            Rmethod = R31;
3371   load_invoke_cp_cache_entry(byte_no, Rmethod, noreg, Rflags, /*virtual*/ true, /*is_invokevfinal*/ true, false);
3372   invokevfinal_helper(Rmethod, Rflags, R11_scratch1, R12_scratch2);
3373 }
3374 
3375 void TemplateTable::invokevfinal_helper(Register Rmethod, Register Rflags, Register Rscratch1, Register Rscratch2) {
3376 
3377   assert_different_registers(Rmethod, Rflags, Rscratch1, Rscratch2);
3378 
3379   // Load receiver from stack slot.
3380   Register Rrecv = Rscratch2;
3381   Register Rnum_params = Rrecv;
3382 
3383   __ ld(Rnum_params, in_bytes(Method::const_offset()), Rmethod);
3384   __ lhz(Rnum_params /* number of params */, in_bytes(ConstMethod::size_of_parameters_offset()), Rnum_params);
3385 
3386   // Get return address.
3387   Register Rtable_addr = Rscratch1,
3388            Rret_addr   = Rflags,
3389            Rret_type   = Rret_addr;
3390   // Get return type. It's coded into the upper 4 bits of the lower half of the 64 bit value.
3391   __ rldicl(Rret_type, Rflags, 64-ConstantPoolCacheEntry::tos_state_shift, 64-ConstantPoolCacheEntry::tos_state_bits);
3392   __ load_dispatch_table(Rtable_addr, Interpreter::invoke_return_entry_table());
3393   __ sldi(Rret_type, Rret_type, LogBytesPerWord);
3394   __ ldx(Rret_addr, Rret_type, Rtable_addr);
3395 
3396   // Load receiver and receiver NULL check.
3397   __ load_receiver(Rnum_params, Rrecv);
3398   __ null_check_throw(Rrecv, -1, Rscratch1);
3399 
3400   __ profile_final_call(Rrecv, Rscratch1);
3401   // Argument and return type profiling.
3402   __ profile_arguments_type(Rmethod, Rscratch1, Rscratch2, true);
3403 
3404   // Do the call.
3405   __ call_from_interpreter(Rmethod, Rret_addr, Rscratch1, Rscratch2);
3406 }
3407 
3408 void TemplateTable::invokespecial(int byte_no) {
3409   assert(byte_no == f1_byte, "use this argument");
3410   transition(vtos, vtos);
3411 
3412   Register Rtable_addr = R3_ARG1,
3413            Rret_addr   = R4_ARG2,
3414            Rflags      = R5_ARG3,
3415            Rreceiver   = R6_ARG4,
3416            Rmethod     = R31;
3417 
3418   prepare_invoke(byte_no, Rmethod, Rret_addr, noreg, Rreceiver, Rflags, R11_scratch1);
3419 
3420   // Receiver NULL check.
3421   __ null_check_throw(Rreceiver, -1, R11_scratch1);
3422 
3423   __ profile_call(R11_scratch1, R12_scratch2);
3424   // Argument and return type profiling.
3425   __ profile_arguments_type(Rmethod, R11_scratch1, R12_scratch2, false);
3426   __ call_from_interpreter(Rmethod, Rret_addr, R11_scratch1, R12_scratch2);
3427 }
3428 
3429 void TemplateTable::invokestatic(int byte_no) {
3430   assert(byte_no == f1_byte, "use this argument");
3431   transition(vtos, vtos);
3432 
3433   Register Rtable_addr = R3_ARG1,
3434            Rret_addr   = R4_ARG2,
3435            Rflags      = R5_ARG3;
3436 
3437   prepare_invoke(byte_no, R19_method, Rret_addr, noreg, noreg, Rflags, R11_scratch1);
3438 
3439   __ profile_call(R11_scratch1, R12_scratch2);
3440   // Argument and return type profiling.
3441   __ profile_arguments_type(R19_method, R11_scratch1, R12_scratch2, false);
3442   __ call_from_interpreter(R19_method, Rret_addr, R11_scratch1, R12_scratch2);
3443 }
3444 
3445 void TemplateTable::invokeinterface_object_method(Register Rrecv_klass,
3446                                                   Register Rret,
3447                                                   Register Rflags,
3448                                                   Register Rindex,
3449                                                   Register Rtemp1,
3450                                                   Register Rtemp2) {
3451 
3452   assert_different_registers(Rindex, Rret, Rrecv_klass, Rflags, Rtemp1, Rtemp2);
3453   Label LnotFinal;
3454 
3455   // Check for vfinal.
3456   __ testbitdi(CCR0, R0, Rflags, ConstantPoolCacheEntry::is_vfinal_shift);
3457   __ bfalse(CCR0, LnotFinal);
3458 
3459   Register Rscratch = Rflags; // Rflags is dead now.
3460 
3461   // Final call case.
3462   __ profile_final_call(Rtemp1, Rscratch);
3463   // Argument and return type profiling.
3464   __ profile_arguments_type(Rindex, Rscratch, Rrecv_klass /* scratch */, true);
3465   // Do the final call - the index (f2) contains the method.
3466   __ call_from_interpreter(Rindex, Rret, Rscratch, Rrecv_klass /* scratch */);
3467 
3468   // Non-final callc case.
3469   __ bind(LnotFinal);
3470   __ profile_virtual_call(Rrecv_klass, Rtemp1, Rscratch, false);
3471   generate_vtable_call(Rrecv_klass, Rindex, Rret, Rscratch);
3472 }
3473 
3474 void TemplateTable::invokeinterface(int byte_no) {
3475   assert(byte_no == f1_byte, "use this argument");
3476   transition(vtos, vtos);
3477 
3478   const Register Rscratch1        = R11_scratch1,
3479                  Rscratch2        = R12_scratch2,
3480                  Rscratch3        = R9_ARG7,
3481                  Rscratch4        = R10_ARG8,
3482                  Rtable_addr      = Rscratch2,
3483                  Rinterface_klass = R5_ARG3,
3484                  Rret_type        = R8_ARG6,
3485                  Rret_addr        = Rret_type,
3486                  Rindex           = R6_ARG4,
3487                  Rreceiver        = R4_ARG2,
3488                  Rrecv_klass      = Rreceiver,
3489                  Rflags           = R7_ARG5;
3490 
3491   prepare_invoke(byte_no, Rinterface_klass, Rret_addr, Rindex, Rreceiver, Rflags, Rscratch1);
3492 
3493   // Get receiver klass.
3494   __ null_check_throw(Rreceiver, oopDesc::klass_offset_in_bytes(), Rscratch3);
3495   __ load_klass(Rrecv_klass, Rreceiver);
3496 
3497   // Check corner case object method.
3498   Label LobjectMethod;
3499 
3500   __ testbitdi(CCR0, R0, Rflags, ConstantPoolCacheEntry::is_forced_virtual_shift);
3501   __ btrue(CCR0, LobjectMethod);
3502 
3503   // Fallthrough: The normal invokeinterface case.
3504   __ profile_virtual_call(Rrecv_klass, Rscratch1, Rscratch2, false);
3505 
3506   // Find entry point to call.
3507   Label Lthrow_icc, Lthrow_ame;
3508   // Result will be returned in Rindex.
3509   __ mr(Rscratch4, Rrecv_klass);
3510   __ mr(Rscratch3, Rindex);
3511   __ lookup_interface_method(Rrecv_klass, Rinterface_klass, Rindex, Rindex, Rscratch1, Rscratch2, Lthrow_icc);
3512 
3513   __ cmpdi(CCR0, Rindex, 0);
3514   __ beq(CCR0, Lthrow_ame);
3515   // Found entry. Jump off!
3516   // Argument and return type profiling.
3517   __ profile_arguments_type(Rindex, Rscratch1, Rscratch2, true);
3518   __ call_from_interpreter(Rindex, Rret_addr, Rscratch1, Rscratch2);
3519 
3520   // Vtable entry was NULL => Throw abstract method error.
3521   __ bind(Lthrow_ame);
3522   __ mr(Rrecv_klass, Rscratch4);
3523   __ mr(Rindex, Rscratch3);
3524   call_VM(noreg, CAST_FROM_FN_PTR(address, InterpreterRuntime::throw_AbstractMethodError));
3525 
3526   // Interface was not found => Throw incompatible class change error.
3527   __ bind(Lthrow_icc);
3528   __ mr(Rrecv_klass, Rscratch4);
3529   call_VM(noreg, CAST_FROM_FN_PTR(address, InterpreterRuntime::throw_IncompatibleClassChangeError));
3530 
3531   __ should_not_reach_here();
3532 
3533   // Special case of invokeinterface called for virtual method of
3534   // java.lang.Object. See ConstantPoolCacheEntry::set_method() for details:
3535   // The invokeinterface was rewritten to a invokevirtual, hence we have
3536   // to handle this corner case. This code isn't produced by javac, but could
3537   // be produced by another compliant java compiler.
3538   __ bind(LobjectMethod);
3539   invokeinterface_object_method(Rrecv_klass, Rret_addr, Rflags, Rindex, Rscratch1, Rscratch2);
3540 }
3541 
3542 void TemplateTable::invokedynamic(int byte_no) {
3543   transition(vtos, vtos);
3544 
3545   const Register Rret_addr = R3_ARG1,
3546                  Rflags    = R4_ARG2,
3547                  Rmethod   = R22_tmp2,
3548                  Rscratch1 = R11_scratch1,
3549                  Rscratch2 = R12_scratch2;
3550 
3551   prepare_invoke(byte_no, Rmethod, Rret_addr, Rscratch1, noreg, Rflags, Rscratch2);
3552 
3553   // Profile this call.
3554   __ profile_call(Rscratch1, Rscratch2);
3555 
3556   // Off we go. With the new method handles, we don't jump to a method handle
3557   // entry any more. Instead, we pushed an "appendix" in prepare invoke, which happens
3558   // to be the callsite object the bootstrap method returned. This is passed to a
3559   // "link" method which does the dispatch (Most likely just grabs the MH stored
3560   // inside the callsite and does an invokehandle).
3561   // Argument and return type profiling.
3562   __ profile_arguments_type(Rmethod, Rscratch1, Rscratch2, false);
3563   __ call_from_interpreter(Rmethod, Rret_addr, Rscratch1 /* scratch1 */, Rscratch2 /* scratch2 */);
3564 }
3565 
3566 void TemplateTable::invokehandle(int byte_no) {
3567   transition(vtos, vtos);
3568 
3569   const Register Rret_addr = R3_ARG1,
3570                  Rflags    = R4_ARG2,
3571                  Rrecv     = R5_ARG3,
3572                  Rmethod   = R22_tmp2,
3573                  Rscratch1 = R11_scratch1,
3574                  Rscratch2 = R12_scratch2;
3575 
3576   prepare_invoke(byte_no, Rmethod, Rret_addr, Rscratch1, Rrecv, Rflags, Rscratch2);
3577   __ verify_method_ptr(Rmethod);
3578   __ null_check_throw(Rrecv, -1, Rscratch2);
3579 
3580   __ profile_final_call(Rrecv, Rscratch1);
3581 
3582   // Still no call from handle => We call the method handle interpreter here.
3583   // Argument and return type profiling.
3584   __ profile_arguments_type(Rmethod, Rscratch1, Rscratch2, true);
3585   __ call_from_interpreter(Rmethod, Rret_addr, Rscratch1 /* scratch1 */, Rscratch2 /* scratch2 */);
3586 }
3587 
3588 // =============================================================================
3589 // Allocation
3590 
3591 // Puts allocated obj ref onto the expression stack.
3592 void TemplateTable::_new() {
3593   transition(vtos, atos);
3594 
3595   Label Lslow_case,
3596         Ldone,
3597         Linitialize_header,
3598         Lallocate_shared,
3599         Linitialize_object;  // Including clearing the fields.
3600 
3601   const Register RallocatedObject = R17_tos,
3602                  RinstanceKlass   = R9_ARG7,
3603                  Rscratch         = R11_scratch1,
3604                  Roffset          = R8_ARG6,
3605                  Rinstance_size   = Roffset,
3606                  Rcpool           = R4_ARG2,
3607                  Rtags            = R3_ARG1,
3608                  Rindex           = R5_ARG3;
3609 
3610   const bool allow_shared_alloc = Universe::heap()->supports_inline_contig_alloc();
3611 
3612   // --------------------------------------------------------------------------
3613   // Check if fast case is possible.
3614 
3615   // Load pointers to const pool and const pool's tags array.
3616   __ get_cpool_and_tags(Rcpool, Rtags);
3617   // Load index of constant pool entry.
3618   __ get_2_byte_integer_at_bcp(1, Rindex, InterpreterMacroAssembler::Unsigned);
3619 
3620   if (UseTLAB) {
3621     // Make sure the class we're about to instantiate has been resolved
3622     // This is done before loading instanceKlass to be consistent with the order
3623     // how Constant Pool is updated (see ConstantPoolCache::klass_at_put).
3624     __ addi(Rtags, Rtags, Array<u1>::base_offset_in_bytes());
3625     __ lbzx(Rtags, Rindex, Rtags);
3626 
3627     __ cmpdi(CCR0, Rtags, JVM_CONSTANT_Class);
3628     __ bne(CCR0, Lslow_case);
3629 
3630     // Get instanceKlass (load from Rcpool + sizeof(ConstantPool) + Rindex*BytesPerWord).
3631     __ sldi(Roffset, Rindex, LogBytesPerWord);
3632     __ addi(Rscratch, Rcpool, sizeof(ConstantPool));
3633     __ isync(); // Order load of instance Klass wrt. tags.
3634     __ ldx(RinstanceKlass, Roffset, Rscratch);
3635 
3636     // Make sure klass is fully initialized and get instance_size.
3637     __ lbz(Rscratch, in_bytes(InstanceKlass::init_state_offset()), RinstanceKlass);
3638     __ lwz(Rinstance_size, in_bytes(Klass::layout_helper_offset()), RinstanceKlass);
3639 
3640     __ cmpdi(CCR1, Rscratch, InstanceKlass::fully_initialized);
3641     // Make sure klass does not have has_finalizer, or is abstract, or interface or java/lang/Class.
3642     __ andi_(R0, Rinstance_size, Klass::_lh_instance_slow_path_bit); // slow path bit equals 0?
3643 
3644     __ crnand(CCR0, Assembler::equal, CCR1, Assembler::equal); // slow path bit set or not fully initialized?
3645     __ beq(CCR0, Lslow_case);
3646 
3647     // --------------------------------------------------------------------------
3648     // Fast case:
3649     // Allocate the instance.
3650     // 1) Try to allocate in the TLAB.
3651     // 2) If fail, and the TLAB is not full enough to discard, allocate in the shared Eden.
3652     // 3) If the above fails (or is not applicable), go to a slow case (creates a new TLAB, etc.).
3653 
3654     Register RoldTopValue = RallocatedObject; // Object will be allocated here if it fits.
3655     Register RnewTopValue = R6_ARG4;
3656     Register RendValue    = R7_ARG5;
3657 
3658     // Check if we can allocate in the TLAB.
3659     __ ld(RoldTopValue, in_bytes(JavaThread::tlab_top_offset()), R16_thread);
3660     __ ld(RendValue,    in_bytes(JavaThread::tlab_end_offset()), R16_thread);
3661 
3662     __ add(RnewTopValue, Rinstance_size, RoldTopValue);
3663 
3664     // If there is enough space, we do not CAS and do not clear.
3665     __ cmpld(CCR0, RnewTopValue, RendValue);
3666     __ bgt(CCR0, allow_shared_alloc ? Lallocate_shared : Lslow_case);
3667 
3668     __ std(RnewTopValue, in_bytes(JavaThread::tlab_top_offset()), R16_thread);
3669 
3670     if (ZeroTLAB) {
3671       // The fields have already been cleared.
3672       __ b(Linitialize_header);
3673     } else {
3674       // Initialize both the header and fields.
3675       __ b(Linitialize_object);
3676     }
3677 
3678     // Fall through: TLAB was too small.
3679     if (allow_shared_alloc) {
3680       Register RtlabWasteLimitValue = R10_ARG8;
3681       Register RfreeValue = RnewTopValue;
3682 
3683       __ bind(Lallocate_shared);
3684       // Check if tlab should be discarded (refill_waste_limit >= free).
3685       __ ld(RtlabWasteLimitValue, in_bytes(JavaThread::tlab_refill_waste_limit_offset()), R16_thread);
3686       __ subf(RfreeValue, RoldTopValue, RendValue);
3687       __ srdi(RfreeValue, RfreeValue, LogHeapWordSize); // in dwords
3688       __ cmpld(CCR0, RtlabWasteLimitValue, RfreeValue);
3689       __ bge(CCR0, Lslow_case);
3690 
3691       // Increment waste limit to prevent getting stuck on this slow path.
3692       __ addi(RtlabWasteLimitValue, RtlabWasteLimitValue, (int)ThreadLocalAllocBuffer::refill_waste_limit_increment());
3693       __ std(RtlabWasteLimitValue, in_bytes(JavaThread::tlab_refill_waste_limit_offset()), R16_thread);
3694     }
3695     // else: No allocation in the shared eden. // fallthru: __ b(Lslow_case);
3696   }
3697   // else: Always go the slow path.
3698 
3699   // --------------------------------------------------------------------------
3700   // slow case
3701   __ bind(Lslow_case);
3702   call_VM(R17_tos, CAST_FROM_FN_PTR(address, InterpreterRuntime::_new), Rcpool, Rindex);
3703 
3704   if (UseTLAB) {
3705     __ b(Ldone);
3706     // --------------------------------------------------------------------------
3707     // Init1: Zero out newly allocated memory.
3708 
3709     if (!ZeroTLAB || allow_shared_alloc) {
3710       // Clear object fields.
3711       __ bind(Linitialize_object);
3712 
3713       // Initialize remaining object fields.
3714       Register Rbase = Rtags;
3715       __ addi(Rinstance_size, Rinstance_size, 7 - (int)sizeof(oopDesc));
3716       __ addi(Rbase, RallocatedObject, sizeof(oopDesc));
3717       __ srdi(Rinstance_size, Rinstance_size, 3);
3718 
3719       // Clear out object skipping header. Takes also care of the zero length case.
3720       __ clear_memory_doubleword(Rbase, Rinstance_size);
3721       // fallthru: __ b(Linitialize_header);
3722     }
3723 
3724     // --------------------------------------------------------------------------
3725     // Init2: Initialize the header: mark, klass
3726     __ bind(Linitialize_header);
3727 
3728     // Init mark.
3729     if (UseBiasedLocking) {
3730       __ ld(Rscratch, in_bytes(Klass::prototype_header_offset()), RinstanceKlass);
3731     } else {
3732       __ load_const_optimized(Rscratch, markOopDesc::prototype(), R0);
3733     }
3734     __ std(Rscratch, oopDesc::mark_offset_in_bytes(), RallocatedObject);
3735 
3736     // Init klass.
3737     __ store_klass_gap(RallocatedObject);
3738     __ store_klass(RallocatedObject, RinstanceKlass, Rscratch); // klass (last for cms)
3739 
3740     // Check and trigger dtrace event.
3741     {
3742       SkipIfEqualZero skip_if(_masm, Rscratch, &DTraceAllocProbes);
3743       __ push(atos);
3744       __ call_VM_leaf(CAST_FROM_FN_PTR(address, SharedRuntime::dtrace_object_alloc));
3745       __ pop(atos);
3746     }
3747   }
3748 
3749   // continue
3750   __ bind(Ldone);
3751 
3752   // Must prevent reordering of stores for object initialization with stores that publish the new object.
3753   __ membar(Assembler::StoreStore);
3754 }
3755 
3756 void TemplateTable::newarray() {
3757   transition(itos, atos);
3758 
3759   __ lbz(R4, 1, R14_bcp);
3760   __ extsw(R5, R17_tos);
3761   call_VM(R17_tos, CAST_FROM_FN_PTR(address, InterpreterRuntime::newarray), R4, R5 /* size */);
3762 
3763   // Must prevent reordering of stores for object initialization with stores that publish the new object.
3764   __ membar(Assembler::StoreStore);
3765 }
3766 
3767 void TemplateTable::anewarray() {
3768   transition(itos, atos);
3769 
3770   __ get_constant_pool(R4);
3771   __ get_2_byte_integer_at_bcp(1, R5, InterpreterMacroAssembler::Unsigned);
3772   __ extsw(R6, R17_tos); // size
3773   call_VM(R17_tos, CAST_FROM_FN_PTR(address, InterpreterRuntime::anewarray), R4 /* pool */, R5 /* index */, R6 /* size */);
3774 
3775   // Must prevent reordering of stores for object initialization with stores that publish the new object.
3776   __ membar(Assembler::StoreStore);
3777 }
3778 
3779 // Allocate a multi dimensional array
3780 void TemplateTable::multianewarray() {
3781   transition(vtos, atos);
3782 
3783   Register Rptr = R31; // Needs to survive C call.
3784 
3785   // Put ndims * wordSize into frame temp slot
3786   __ lbz(Rptr, 3, R14_bcp);
3787   __ sldi(Rptr, Rptr, Interpreter::logStackElementSize);
3788   // Esp points past last_dim, so set to R4 to first_dim address.
3789   __ add(R4, Rptr, R15_esp);
3790   call_VM(R17_tos, CAST_FROM_FN_PTR(address, InterpreterRuntime::multianewarray), R4 /* first_size_address */);
3791   // Pop all dimensions off the stack.
3792   __ add(R15_esp, Rptr, R15_esp);
3793 
3794   // Must prevent reordering of stores for object initialization with stores that publish the new object.
3795   __ membar(Assembler::StoreStore);
3796 }
3797 
3798 void TemplateTable::arraylength() {
3799   transition(atos, itos);
3800 
3801   Label LnoException;
3802   __ verify_oop(R17_tos);
3803   __ null_check_throw(R17_tos, arrayOopDesc::length_offset_in_bytes(), R11_scratch1);
3804   __ lwa(R17_tos, arrayOopDesc::length_offset_in_bytes(), R17_tos);
3805 }
3806 
3807 // ============================================================================
3808 // Typechecks
3809 
3810 void TemplateTable::checkcast() {
3811   transition(atos, atos);
3812 
3813   Label Ldone, Lis_null, Lquicked, Lresolved;
3814   Register Roffset         = R6_ARG4,
3815            RobjKlass       = R4_ARG2,
3816            RspecifiedKlass = R5_ARG3, // Generate_ClassCastException_verbose_handler will read value from this register.
3817            Rcpool          = R11_scratch1,
3818            Rtags           = R12_scratch2;
3819 
3820   // Null does not pass.
3821   __ cmpdi(CCR0, R17_tos, 0);
3822   __ beq(CCR0, Lis_null);
3823 
3824   // Get constant pool tag to find out if the bytecode has already been "quickened".
3825   __ get_cpool_and_tags(Rcpool, Rtags);
3826 
3827   __ get_2_byte_integer_at_bcp(1, Roffset, InterpreterMacroAssembler::Unsigned);
3828 
3829   __ addi(Rtags, Rtags, Array<u1>::base_offset_in_bytes());
3830   __ lbzx(Rtags, Rtags, Roffset);
3831 
3832   __ cmpdi(CCR0, Rtags, JVM_CONSTANT_Class);
3833   __ beq(CCR0, Lquicked);
3834 
3835   // Call into the VM to "quicken" instanceof.
3836   __ push_ptr();  // for GC
3837   call_VM(noreg, CAST_FROM_FN_PTR(address, InterpreterRuntime::quicken_io_cc));
3838   __ get_vm_result_2(RspecifiedKlass);
3839   __ pop_ptr();   // Restore receiver.
3840   __ b(Lresolved);
3841 
3842   // Extract target class from constant pool.
3843   __ bind(Lquicked);
3844   __ sldi(Roffset, Roffset, LogBytesPerWord);
3845   __ addi(Rcpool, Rcpool, sizeof(ConstantPool));
3846   __ isync(); // Order load of specified Klass wrt. tags.
3847   __ ldx(RspecifiedKlass, Rcpool, Roffset);
3848 
3849   // Do the checkcast.
3850   __ bind(Lresolved);
3851   // Get value klass in RobjKlass.
3852   __ load_klass(RobjKlass, R17_tos);
3853   // Generate a fast subtype check. Branch to cast_ok if no failure. Return 0 if failure.
3854   __ gen_subtype_check(RobjKlass, RspecifiedKlass, /*3 temp regs*/ Roffset, Rcpool, Rtags, /*target if subtype*/ Ldone);
3855 
3856   // Not a subtype; so must throw exception
3857   // Target class oop is in register R6_ARG4 == RspecifiedKlass by convention.
3858   __ load_dispatch_table(R11_scratch1, (address*)Interpreter::_throw_ClassCastException_entry);
3859   __ mtctr(R11_scratch1);
3860   __ bctr();
3861 
3862   // Profile the null case.
3863   __ align(32, 12);
3864   __ bind(Lis_null);
3865   __ profile_null_seen(R11_scratch1, Rtags); // Rtags used as scratch.
3866 
3867   __ align(32, 12);
3868   __ bind(Ldone);
3869 }
3870 
3871 // Output:
3872 //   - tos == 0: Obj was null or not an instance of class.
3873 //   - tos == 1: Obj was an instance of class.
3874 void TemplateTable::instanceof() {
3875   transition(atos, itos);
3876 
3877   Label Ldone, Lis_null, Lquicked, Lresolved;
3878   Register Roffset         = R5_ARG3,
3879            RobjKlass       = R4_ARG2,
3880            RspecifiedKlass = R6_ARG4, // Generate_ClassCastException_verbose_handler will expect the value in this register.
3881            Rcpool          = R11_scratch1,
3882            Rtags           = R12_scratch2;
3883 
3884   // Null does not pass.
3885   __ cmpdi(CCR0, R17_tos, 0);
3886   __ beq(CCR0, Lis_null);
3887 
3888   // Get constant pool tag to find out if the bytecode has already been "quickened".
3889   __ get_cpool_and_tags(Rcpool, Rtags);
3890 
3891   __ get_2_byte_integer_at_bcp(1, Roffset, InterpreterMacroAssembler::Unsigned);
3892 
3893   __ addi(Rtags, Rtags, Array<u1>::base_offset_in_bytes());
3894   __ lbzx(Rtags, Rtags, Roffset);
3895 
3896   __ cmpdi(CCR0, Rtags, JVM_CONSTANT_Class);
3897   __ beq(CCR0, Lquicked);
3898 
3899   // Call into the VM to "quicken" instanceof.
3900   __ push_ptr();  // for GC
3901   call_VM(noreg, CAST_FROM_FN_PTR(address, InterpreterRuntime::quicken_io_cc));
3902   __ get_vm_result_2(RspecifiedKlass);
3903   __ pop_ptr();   // Restore receiver.
3904   __ b(Lresolved);
3905 
3906   // Extract target class from constant pool.
3907   __ bind(Lquicked);
3908   __ sldi(Roffset, Roffset, LogBytesPerWord);
3909   __ addi(Rcpool, Rcpool, sizeof(ConstantPool));
3910   __ isync(); // Order load of specified Klass wrt. tags.
3911   __ ldx(RspecifiedKlass, Rcpool, Roffset);
3912 
3913   // Do the checkcast.
3914   __ bind(Lresolved);
3915   // Get value klass in RobjKlass.
3916   __ load_klass(RobjKlass, R17_tos);
3917   // Generate a fast subtype check. Branch to cast_ok if no failure. Return 0 if failure.
3918   __ li(R17_tos, 1);
3919   __ gen_subtype_check(RobjKlass, RspecifiedKlass, /*3 temp regs*/ Roffset, Rcpool, Rtags, /*target if subtype*/ Ldone);
3920   __ li(R17_tos, 0);
3921 
3922   if (ProfileInterpreter) {
3923     __ b(Ldone);
3924   }
3925 
3926   // Profile the null case.
3927   __ align(32, 12);
3928   __ bind(Lis_null);
3929   __ profile_null_seen(Rcpool, Rtags); // Rcpool and Rtags used as scratch.
3930 
3931   __ align(32, 12);
3932   __ bind(Ldone);
3933 }
3934 
3935 // =============================================================================
3936 // Breakpoints
3937 
3938 void TemplateTable::_breakpoint() {
3939   transition(vtos, vtos);
3940 
3941   // Get the unpatched byte code.
3942   __ call_VM(noreg, CAST_FROM_FN_PTR(address, InterpreterRuntime::get_original_bytecode_at), R19_method, R14_bcp);
3943   __ mr(R31, R3_RET);
3944 
3945   // Post the breakpoint event.
3946   __ call_VM(noreg, CAST_FROM_FN_PTR(address, InterpreterRuntime::_breakpoint), R19_method, R14_bcp);
3947 
3948   // Complete the execution of original bytecode.
3949   __ dispatch_Lbyte_code(vtos, R31, Interpreter::normal_table(vtos));
3950 }
3951 
3952 // =============================================================================
3953 // Exceptions
3954 
3955 void TemplateTable::athrow() {
3956   transition(atos, vtos);
3957 
3958   // Exception oop is in tos
3959   __ verify_oop(R17_tos);
3960 
3961   __ null_check_throw(R17_tos, -1, R11_scratch1);
3962 
3963   // Throw exception interpreter entry expects exception oop to be in R3.
3964   __ mr(R3_RET, R17_tos);
3965   __ load_dispatch_table(R11_scratch1, (address*)Interpreter::throw_exception_entry());
3966   __ mtctr(R11_scratch1);
3967   __ bctr();
3968 }
3969 
3970 // =============================================================================
3971 // Synchronization
3972 // Searches the basic object lock list on the stack for a free slot
3973 // and uses it to lock the obect in tos.
3974 //
3975 // Recursive locking is enabled by exiting the search if the same
3976 // object is already found in the list. Thus, a new basic lock obj lock
3977 // is allocated "higher up" in the stack and thus is found first
3978 // at next monitor exit.
3979 void TemplateTable::monitorenter() {
3980   transition(atos, vtos);
3981 
3982   __ verify_oop(R17_tos);
3983 
3984   Register Rcurrent_monitor  = R11_scratch1,
3985            Rcurrent_obj      = R12_scratch2,
3986            Robj_to_lock      = R17_tos,
3987            Rscratch1         = R3_ARG1,
3988            Rscratch2         = R4_ARG2,
3989            Rscratch3         = R5_ARG3,
3990            Rcurrent_obj_addr = R6_ARG4;
3991 
3992   // ------------------------------------------------------------------------------
3993   // Null pointer exception.
3994   __ null_check_throw(Robj_to_lock, -1, R11_scratch1);
3995 
3996   // Try to acquire a lock on the object.
3997   // Repeat until succeeded (i.e., until monitorenter returns true).
3998 
3999   // ------------------------------------------------------------------------------
4000   // Find a free slot in the monitor block.
4001   Label Lfound, Lexit, Lallocate_new;
4002   ConditionRegister found_free_slot = CCR0,
4003                     found_same_obj  = CCR1,
4004                     reached_limit   = CCR6;
4005   {
4006     Label Lloop, Lentry;
4007     Register Rlimit = Rcurrent_monitor;
4008 
4009     // Set up search loop - start with topmost monitor.
4010     __ add(Rcurrent_obj_addr, BasicObjectLock::obj_offset_in_bytes(), R26_monitor);
4011 
4012     __ ld(Rlimit, 0, R1_SP);
4013     __ addi(Rlimit, Rlimit, - (frame::ijava_state_size + frame::interpreter_frame_monitor_size_in_bytes() - BasicObjectLock::obj_offset_in_bytes())); // Monitor base
4014 
4015     // Check if any slot is present => short cut to allocation if not.
4016     __ cmpld(reached_limit, Rcurrent_obj_addr, Rlimit);
4017     __ bgt(reached_limit, Lallocate_new);
4018 
4019     // Pre-load topmost slot.
4020     __ ld(Rcurrent_obj, 0, Rcurrent_obj_addr);
4021     __ addi(Rcurrent_obj_addr, Rcurrent_obj_addr, frame::interpreter_frame_monitor_size() * wordSize);
4022     // The search loop.
4023     __ bind(Lloop);
4024     // Found free slot?
4025     __ cmpdi(found_free_slot, Rcurrent_obj, 0);
4026     // Is this entry for same obj? If so, stop the search and take the found
4027     // free slot or allocate a new one to enable recursive locking.
4028     __ cmpd(found_same_obj, Rcurrent_obj, Robj_to_lock);
4029     __ cmpld(reached_limit, Rcurrent_obj_addr, Rlimit);
4030     __ beq(found_free_slot, Lexit);
4031     __ beq(found_same_obj, Lallocate_new);
4032     __ bgt(reached_limit, Lallocate_new);
4033     // Check if last allocated BasicLockObj reached.
4034     __ ld(Rcurrent_obj, 0, Rcurrent_obj_addr);
4035     __ addi(Rcurrent_obj_addr, Rcurrent_obj_addr, frame::interpreter_frame_monitor_size() * wordSize);
4036     // Next iteration if unchecked BasicObjectLocks exist on the stack.
4037     __ b(Lloop);
4038   }
4039 
4040   // ------------------------------------------------------------------------------
4041   // Check if we found a free slot.
4042   __ bind(Lexit);
4043 
4044   __ addi(Rcurrent_monitor, Rcurrent_obj_addr, -(frame::interpreter_frame_monitor_size() * wordSize) - BasicObjectLock::obj_offset_in_bytes());
4045   __ addi(Rcurrent_obj_addr, Rcurrent_obj_addr, - frame::interpreter_frame_monitor_size() * wordSize);
4046   __ b(Lfound);
4047 
4048   // We didn't find a free BasicObjLock => allocate one.
4049   __ align(32, 12);
4050   __ bind(Lallocate_new);
4051   __ add_monitor_to_stack(false, Rscratch1, Rscratch2);
4052   __ mr(Rcurrent_monitor, R26_monitor);
4053   __ addi(Rcurrent_obj_addr, R26_monitor, BasicObjectLock::obj_offset_in_bytes());
4054 
4055   // ------------------------------------------------------------------------------
4056   // We now have a slot to lock.
4057   __ bind(Lfound);
4058 
4059   // Increment bcp to point to the next bytecode, so exception handling for async. exceptions work correctly.
4060   // The object has already been poped from the stack, so the expression stack looks correct.
4061   __ addi(R14_bcp, R14_bcp, 1);
4062 
4063   __ std(Robj_to_lock, 0, Rcurrent_obj_addr);
4064   __ lock_object(Rcurrent_monitor, Robj_to_lock);
4065 
4066   // Check if there's enough space on the stack for the monitors after locking.
4067   Label Lskip_stack_check;
4068   // Optimization: If the monitors stack section is less then a std page size (4K) don't run
4069   // the stack check. There should be enough shadow pages to fit that in.
4070   __ ld(Rscratch3, 0, R1_SP);
4071   __ sub(Rscratch3, Rscratch3, R26_monitor);
4072   __ cmpdi(CCR0, Rscratch3, 4*K);
4073   __ blt(CCR0, Lskip_stack_check);
4074 
4075   DEBUG_ONLY(__ untested("stack overflow check during monitor enter");)
4076   __ li(Rscratch1, 0);
4077   __ generate_stack_overflow_check_with_compare_and_throw(Rscratch1, Rscratch2);
4078 
4079   __ align(32, 12);
4080   __ bind(Lskip_stack_check);
4081 
4082   // The bcp has already been incremented. Just need to dispatch to next instruction.
4083   __ dispatch_next(vtos);
4084 }
4085 
4086 void TemplateTable::monitorexit() {
4087   transition(atos, vtos);
4088   __ verify_oop(R17_tos);
4089 
4090   Register Rcurrent_monitor  = R11_scratch1,
4091            Rcurrent_obj      = R12_scratch2,
4092            Robj_to_lock      = R17_tos,
4093            Rcurrent_obj_addr = R3_ARG1,
4094            Rlimit            = R4_ARG2;
4095   Label Lfound, Lillegal_monitor_state;
4096 
4097   // Check corner case: unbalanced monitorEnter / Exit.
4098   __ ld(Rlimit, 0, R1_SP);
4099   __ addi(Rlimit, Rlimit, - (frame::ijava_state_size + frame::interpreter_frame_monitor_size_in_bytes())); // Monitor base
4100 
4101   // Null pointer check.
4102   __ null_check_throw(Robj_to_lock, -1, R11_scratch1);
4103 
4104   __ cmpld(CCR0, R26_monitor, Rlimit);
4105   __ bgt(CCR0, Lillegal_monitor_state);
4106 
4107   // Find the corresponding slot in the monitors stack section.
4108   {
4109     Label Lloop;
4110 
4111     // Start with topmost monitor.
4112     __ addi(Rcurrent_obj_addr, R26_monitor, BasicObjectLock::obj_offset_in_bytes());
4113     __ addi(Rlimit, Rlimit, BasicObjectLock::obj_offset_in_bytes());
4114     __ ld(Rcurrent_obj, 0, Rcurrent_obj_addr);
4115     __ addi(Rcurrent_obj_addr, Rcurrent_obj_addr, frame::interpreter_frame_monitor_size() * wordSize);
4116 
4117     __ bind(Lloop);
4118     // Is this entry for same obj?
4119     __ cmpd(CCR0, Rcurrent_obj, Robj_to_lock);
4120     __ beq(CCR0, Lfound);
4121 
4122     // Check if last allocated BasicLockObj reached.
4123 
4124     __ ld(Rcurrent_obj, 0, Rcurrent_obj_addr);
4125     __ cmpld(CCR0, Rcurrent_obj_addr, Rlimit);
4126     __ addi(Rcurrent_obj_addr, Rcurrent_obj_addr, frame::interpreter_frame_monitor_size() * wordSize);
4127 
4128     // Next iteration if unchecked BasicObjectLocks exist on the stack.
4129     __ ble(CCR0, Lloop);
4130   }
4131 
4132   // Fell through without finding the basic obj lock => throw up!
4133   __ bind(Lillegal_monitor_state);
4134   call_VM(noreg, CAST_FROM_FN_PTR(address, InterpreterRuntime::throw_illegal_monitor_state_exception));
4135   __ should_not_reach_here();
4136 
4137   __ align(32, 12);
4138   __ bind(Lfound);
4139   __ addi(Rcurrent_monitor, Rcurrent_obj_addr,
4140           -(frame::interpreter_frame_monitor_size() * wordSize) - BasicObjectLock::obj_offset_in_bytes());
4141   __ unlock_object(Rcurrent_monitor);
4142 }
4143 
4144 // ============================================================================
4145 // Wide bytecodes
4146 
4147 // Wide instructions. Simply redirects to the wide entry point for that instruction.
4148 void TemplateTable::wide() {
4149   transition(vtos, vtos);
4150 
4151   const Register Rtable = R11_scratch1,
4152                  Rindex = R12_scratch2,
4153                  Rtmp   = R0;
4154 
4155   __ lbz(Rindex, 1, R14_bcp);
4156 
4157   __ load_dispatch_table(Rtable, Interpreter::_wentry_point);
4158 
4159   __ slwi(Rindex, Rindex, LogBytesPerWord);
4160   __ ldx(Rtmp, Rtable, Rindex);
4161   __ mtctr(Rtmp);
4162   __ bctr();
4163   // Note: the bcp increment step is part of the individual wide bytecode implementations.
4164 }
4165 #endif // !CC_INTERP