1 /*
   2  * Copyright (c) 1997, 2016, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  *
  23  */
  24 
  25 #include "precompiled.hpp"
  26 #include "interpreter/interpreter.hpp"
  27 #include "interpreter/interpreterRuntime.hpp"
  28 #include "interpreter/interp_masm.hpp"
  29 #include "interpreter/templateTable.hpp"
  30 #include "memory/universe.inline.hpp"
  31 #include "oops/methodData.hpp"
  32 #include "oops/objArrayKlass.hpp"
  33 #include "oops/oop.inline.hpp"
  34 #include "prims/methodHandles.hpp"
  35 #include "runtime/sharedRuntime.hpp"
  36 #include "runtime/stubRoutines.hpp"
  37 #include "runtime/synchronizer.hpp"
  38 #include "utilities/macros.hpp"
  39 
  40 #define __ _masm->
  41 
  42 // Misc helpers
  43 
  44 // Do an oop store like *(base + index + offset) = val
  45 // index can be noreg,
  46 static void do_oop_store(InterpreterMacroAssembler* _masm,
  47                          Register base,
  48                          Register index,
  49                          int offset,
  50                          Register val,
  51                          Register tmp,
  52                          BarrierSet::Name barrier,
  53                          bool precise) {
  54   assert(tmp != val && tmp != base && tmp != index, "register collision");
  55   assert(index == noreg || offset == 0, "only one offset");
  56   switch (barrier) {
  57 #if INCLUDE_ALL_GCS
  58     case BarrierSet::G1SATBCTLogging:
  59       {
  60         // Load and record the previous value.
  61         __ g1_write_barrier_pre(base, index, offset,
  62                                 noreg /* pre_val */,
  63                                 tmp, true /*preserve_o_regs*/);
  64 
  65         // G1 barrier needs uncompressed oop for region cross check.
  66         Register new_val = val;
  67         if (UseCompressedOops && val != G0) {
  68           new_val = tmp;
  69           __ mov(val, new_val);
  70         }
  71 
  72         if (index == noreg ) {
  73           assert(Assembler::is_simm13(offset), "fix this code");
  74           __ store_heap_oop(val, base, offset);
  75         } else {
  76           __ store_heap_oop(val, base, index);
  77         }
  78 
  79         // No need for post barrier if storing NULL
  80         if (val != G0) {
  81           if (precise) {
  82             if (index == noreg) {
  83               __ add(base, offset, base);
  84             } else {
  85               __ add(base, index, base);
  86             }
  87           }
  88           __ g1_write_barrier_post(base, new_val, tmp);
  89         }
  90       }
  91       break;
  92 #endif // INCLUDE_ALL_GCS
  93     case BarrierSet::CardTableForRS:
  94     case BarrierSet::CardTableExtension:
  95       {
  96         if (index == noreg ) {
  97           assert(Assembler::is_simm13(offset), "fix this code");
  98           __ store_heap_oop(val, base, offset);
  99         } else {
 100           __ store_heap_oop(val, base, index);
 101         }
 102         // No need for post barrier if storing NULL
 103         if (val != G0) {
 104           if (precise) {
 105             if (index == noreg) {
 106               __ add(base, offset, base);
 107             } else {
 108               __ add(base, index, base);
 109             }
 110           }
 111           __ card_write_barrier_post(base, val, tmp);
 112         }
 113       }
 114       break;
 115     case BarrierSet::ModRef:
 116       ShouldNotReachHere();
 117       break;
 118     default      :
 119       ShouldNotReachHere();
 120 
 121   }
 122 }
 123 
 124 
 125 //----------------------------------------------------------------------------------------------------
 126 // Platform-dependent initialization
 127 
 128 void TemplateTable::pd_initialize() {
 129   // (none)
 130 }
 131 
 132 
 133 //----------------------------------------------------------------------------------------------------
 134 // Condition conversion
 135 Assembler::Condition ccNot(TemplateTable::Condition cc) {
 136   switch (cc) {
 137     case TemplateTable::equal        : return Assembler::notEqual;
 138     case TemplateTable::not_equal    : return Assembler::equal;
 139     case TemplateTable::less         : return Assembler::greaterEqual;
 140     case TemplateTable::less_equal   : return Assembler::greater;
 141     case TemplateTable::greater      : return Assembler::lessEqual;
 142     case TemplateTable::greater_equal: return Assembler::less;
 143   }
 144   ShouldNotReachHere();
 145   return Assembler::zero;
 146 }
 147 
 148 //----------------------------------------------------------------------------------------------------
 149 // Miscelaneous helper routines
 150 
 151 
 152 Address TemplateTable::at_bcp(int offset) {
 153   assert(_desc->uses_bcp(), "inconsistent uses_bcp information");
 154   return Address(Lbcp, offset);
 155 }
 156 
 157 
 158 void TemplateTable::patch_bytecode(Bytecodes::Code bc, Register bc_reg,
 159                                    Register temp_reg, bool load_bc_into_bc_reg/*=true*/,
 160                                    int byte_no) {
 161   // With sharing on, may need to test Method* flag.
 162   if (!RewriteBytecodes)  return;
 163   Label L_patch_done;
 164 
 165   switch (bc) {
 166   case Bytecodes::_fast_aputfield:
 167   case Bytecodes::_fast_bputfield:
 168   case Bytecodes::_fast_zputfield:
 169   case Bytecodes::_fast_cputfield:
 170   case Bytecodes::_fast_dputfield:
 171   case Bytecodes::_fast_fputfield:
 172   case Bytecodes::_fast_iputfield:
 173   case Bytecodes::_fast_lputfield:
 174   case Bytecodes::_fast_sputfield:
 175     {
 176       // We skip bytecode quickening for putfield instructions when
 177       // the put_code written to the constant pool cache is zero.
 178       // This is required so that every execution of this instruction
 179       // calls out to InterpreterRuntime::resolve_get_put to do
 180       // additional, required work.
 181       assert(byte_no == f1_byte || byte_no == f2_byte, "byte_no out of range");
 182       assert(load_bc_into_bc_reg, "we use bc_reg as temp");
 183       __ get_cache_and_index_and_bytecode_at_bcp(bc_reg, temp_reg, temp_reg, byte_no, 1);
 184       __ set(bc, bc_reg);
 185       __ cmp_and_br_short(temp_reg, 0, Assembler::equal, Assembler::pn, L_patch_done);  // don't patch
 186     }
 187     break;
 188   default:
 189     assert(byte_no == -1, "sanity");
 190     if (load_bc_into_bc_reg) {
 191       __ set(bc, bc_reg);
 192     }
 193   }
 194 
 195   if (JvmtiExport::can_post_breakpoint()) {
 196     Label L_fast_patch;
 197     __ ldub(at_bcp(0), temp_reg);
 198     __ cmp_and_br_short(temp_reg, Bytecodes::_breakpoint, Assembler::notEqual, Assembler::pt, L_fast_patch);
 199     // perform the quickening, slowly, in the bowels of the breakpoint table
 200     __ call_VM(noreg, CAST_FROM_FN_PTR(address, InterpreterRuntime::set_original_bytecode_at), Lmethod, Lbcp, bc_reg);
 201     __ ba_short(L_patch_done);
 202     __ bind(L_fast_patch);
 203   }
 204 
 205 #ifdef ASSERT
 206   Bytecodes::Code orig_bytecode =  Bytecodes::java_code(bc);
 207   Label L_okay;
 208   __ ldub(at_bcp(0), temp_reg);
 209   __ cmp(temp_reg, orig_bytecode);
 210   __ br(Assembler::equal, false, Assembler::pt, L_okay);
 211   __ delayed()->cmp(temp_reg, bc_reg);
 212   __ br(Assembler::equal, false, Assembler::pt, L_okay);
 213   __ delayed()->nop();
 214   __ stop("patching the wrong bytecode");
 215   __ bind(L_okay);
 216 #endif
 217 
 218   // patch bytecode
 219   __ stb(bc_reg, at_bcp(0));
 220   __ bind(L_patch_done);
 221 }
 222 
 223 //----------------------------------------------------------------------------------------------------
 224 // Individual instructions
 225 
 226 void TemplateTable::nop() {
 227   transition(vtos, vtos);
 228   // nothing to do
 229 }
 230 
 231 void TemplateTable::shouldnotreachhere() {
 232   transition(vtos, vtos);
 233   __ stop("shouldnotreachhere bytecode");
 234 }
 235 
 236 void TemplateTable::aconst_null() {
 237   transition(vtos, atos);
 238   __ clr(Otos_i);
 239 }
 240 
 241 
 242 void TemplateTable::iconst(int value) {
 243   transition(vtos, itos);
 244   __ set(value, Otos_i);
 245 }
 246 
 247 
 248 void TemplateTable::lconst(int value) {
 249   transition(vtos, ltos);
 250   assert(value >= 0, "check this code");
 251 #ifdef _LP64
 252   __ set(value, Otos_l);
 253 #else
 254   __ set(value, Otos_l2);
 255   __ clr( Otos_l1);
 256 #endif
 257 }
 258 
 259 
 260 void TemplateTable::fconst(int value) {
 261   transition(vtos, ftos);
 262   static float zero = 0.0, one = 1.0, two = 2.0;
 263   float* p;
 264   switch( value ) {
 265    default: ShouldNotReachHere();
 266    case 0:  p = &zero;  break;
 267    case 1:  p = &one;   break;
 268    case 2:  p = &two;   break;
 269   }
 270   AddressLiteral a(p);
 271   __ sethi(a, G3_scratch);
 272   __ ldf(FloatRegisterImpl::S, G3_scratch, a.low10(), Ftos_f);
 273 }
 274 
 275 
 276 void TemplateTable::dconst(int value) {
 277   transition(vtos, dtos);
 278   static double zero = 0.0, one = 1.0;
 279   double* p;
 280   switch( value ) {
 281    default: ShouldNotReachHere();
 282    case 0:  p = &zero;  break;
 283    case 1:  p = &one;   break;
 284   }
 285   AddressLiteral a(p);
 286   __ sethi(a, G3_scratch);
 287   __ ldf(FloatRegisterImpl::D, G3_scratch, a.low10(), Ftos_d);
 288 }
 289 
 290 
 291 // %%%%% Should factore most snippet templates across platforms
 292 
 293 void TemplateTable::bipush() {
 294   transition(vtos, itos);
 295   __ ldsb( at_bcp(1), Otos_i );
 296 }
 297 
 298 void TemplateTable::sipush() {
 299   transition(vtos, itos);
 300   __ get_2_byte_integer_at_bcp(1, G3_scratch, Otos_i, InterpreterMacroAssembler::Signed);
 301 }
 302 
 303 void TemplateTable::ldc(bool wide) {
 304   transition(vtos, vtos);
 305   Label call_ldc, notInt, isString, notString, notClass, exit;
 306 
 307   if (wide) {
 308     __ get_2_byte_integer_at_bcp(1, G3_scratch, O1, InterpreterMacroAssembler::Unsigned);
 309   } else {
 310     __ ldub(Lbcp, 1, O1);
 311   }
 312   __ get_cpool_and_tags(O0, O2);
 313 
 314   const int base_offset = ConstantPool::header_size() * wordSize;
 315   const int tags_offset = Array<u1>::base_offset_in_bytes();
 316 
 317   // get type from tags
 318   __ add(O2, tags_offset, O2);
 319   __ ldub(O2, O1, O2);
 320 
 321   // unresolved class? If so, must resolve
 322   __ cmp_and_brx_short(O2, JVM_CONSTANT_UnresolvedClass, Assembler::equal, Assembler::pt, call_ldc);
 323 
 324   // unresolved class in error state
 325   __ cmp_and_brx_short(O2, JVM_CONSTANT_UnresolvedClassInError, Assembler::equal, Assembler::pn, call_ldc);
 326 
 327   __ cmp(O2, JVM_CONSTANT_Class);      // need to call vm to get java mirror of the class
 328   __ brx(Assembler::notEqual, true, Assembler::pt, notClass);
 329   __ delayed()->add(O0, base_offset, O0);
 330 
 331   __ bind(call_ldc);
 332   __ set(wide, O1);
 333   call_VM(Otos_i, CAST_FROM_FN_PTR(address, InterpreterRuntime::ldc), O1);
 334   __ push(atos);
 335   __ ba_short(exit);
 336 
 337   __ bind(notClass);
 338  // __ add(O0, base_offset, O0);
 339   __ sll(O1, LogBytesPerWord, O1);
 340   __ cmp(O2, JVM_CONSTANT_Integer);
 341   __ brx(Assembler::notEqual, true, Assembler::pt, notInt);
 342   __ delayed()->cmp(O2, JVM_CONSTANT_String);
 343   __ ld(O0, O1, Otos_i);
 344   __ push(itos);
 345   __ ba_short(exit);
 346 
 347   __ bind(notInt);
 348  // __ cmp(O2, JVM_CONSTANT_String);
 349   __ brx(Assembler::notEqual, true, Assembler::pt, notString);
 350   __ delayed()->ldf(FloatRegisterImpl::S, O0, O1, Ftos_f);
 351   __ bind(isString);
 352   __ stop("string should be rewritten to fast_aldc");
 353   __ ba_short(exit);
 354 
 355   __ bind(notString);
 356  // __ ldf(FloatRegisterImpl::S, O0, O1, Ftos_f);
 357   __ push(ftos);
 358 
 359   __ bind(exit);
 360 }
 361 
 362 // Fast path for caching oop constants.
 363 // %%% We should use this to handle Class and String constants also.
 364 // %%% It will simplify the ldc/primitive path considerably.
 365 void TemplateTable::fast_aldc(bool wide) {
 366   transition(vtos, atos);
 367 
 368   int index_size = wide ? sizeof(u2) : sizeof(u1);
 369   Label resolved;
 370 
 371   // We are resolved if the resolved reference cache entry contains a
 372   // non-null object (CallSite, etc.)
 373   assert_different_registers(Otos_i, G3_scratch);
 374   __ get_cache_index_at_bcp(Otos_i, G3_scratch, 1, index_size);  // load index => G3_scratch
 375   __ load_resolved_reference_at_index(Otos_i, G3_scratch);
 376   __ tst(Otos_i);
 377   __ br(Assembler::notEqual, false, Assembler::pt, resolved);
 378   __ delayed()->set((int)bytecode(), O1);
 379 
 380   address entry = CAST_FROM_FN_PTR(address, InterpreterRuntime::resolve_ldc);
 381 
 382   // first time invocation - must resolve first
 383   __ call_VM(Otos_i, entry, O1);
 384   __ bind(resolved);
 385   __ verify_oop(Otos_i);
 386 }
 387 
 388 void TemplateTable::ldc2_w() {
 389   transition(vtos, vtos);
 390   Label Long, exit;
 391 
 392   __ get_2_byte_integer_at_bcp(1, G3_scratch, O1, InterpreterMacroAssembler::Unsigned);
 393   __ get_cpool_and_tags(O0, O2);
 394 
 395   const int base_offset = ConstantPool::header_size() * wordSize;
 396   const int tags_offset = Array<u1>::base_offset_in_bytes();
 397   // get type from tags
 398   __ add(O2, tags_offset, O2);
 399   __ ldub(O2, O1, O2);
 400 
 401   __ sll(O1, LogBytesPerWord, O1);
 402   __ add(O0, O1, G3_scratch);
 403 
 404   __ cmp_and_brx_short(O2, JVM_CONSTANT_Double, Assembler::notEqual, Assembler::pt, Long);
 405   // A double can be placed at word-aligned locations in the constant pool.
 406   // Check out Conversions.java for an example.
 407   // Also ConstantPool::header_size() is 20, which makes it very difficult
 408   // to double-align double on the constant pool.  SG, 11/7/97
 409 #ifdef _LP64
 410   __ ldf(FloatRegisterImpl::D, G3_scratch, base_offset, Ftos_d);
 411 #else
 412   FloatRegister f = Ftos_d;
 413   __ ldf(FloatRegisterImpl::S, G3_scratch, base_offset, f);
 414   __ ldf(FloatRegisterImpl::S, G3_scratch, base_offset + sizeof(jdouble)/2,
 415          f->successor());
 416 #endif
 417   __ push(dtos);
 418   __ ba_short(exit);
 419 
 420   __ bind(Long);
 421 #ifdef _LP64
 422   __ ldx(G3_scratch, base_offset, Otos_l);
 423 #else
 424   __ ld(G3_scratch, base_offset, Otos_l);
 425   __ ld(G3_scratch, base_offset + sizeof(jlong)/2, Otos_l->successor());
 426 #endif
 427   __ push(ltos);
 428 
 429   __ bind(exit);
 430 }
 431 
 432 void TemplateTable::locals_index(Register reg, int offset) {
 433   __ ldub( at_bcp(offset), reg );
 434 }
 435 
 436 void TemplateTable::locals_index_wide(Register reg) {
 437   // offset is 2, not 1, because Lbcp points to wide prefix code
 438   __ get_2_byte_integer_at_bcp(2, G4_scratch, reg, InterpreterMacroAssembler::Unsigned);
 439 }
 440 
 441 void TemplateTable::iload() {
 442   iload_internal();
 443 }
 444 
 445 void TemplateTable::nofast_iload() {
 446   iload_internal(may_not_rewrite);
 447 }
 448 
 449 void TemplateTable::iload_internal(RewriteControl rc) {
 450   transition(vtos, itos);
 451   // Rewrite iload,iload  pair into fast_iload2
 452   //         iload,caload pair into fast_icaload
 453   if (RewriteFrequentPairs && rc == may_rewrite) {
 454     Label rewrite, done;
 455 
 456     // get next byte
 457     __ ldub(at_bcp(Bytecodes::length_for(Bytecodes::_iload)), G3_scratch);
 458 
 459     // if _iload, wait to rewrite to iload2.  We only want to rewrite the
 460     // last two iloads in a pair.  Comparing against fast_iload means that
 461     // the next bytecode is neither an iload or a caload, and therefore
 462     // an iload pair.
 463     __ cmp_and_br_short(G3_scratch, (int)Bytecodes::_iload, Assembler::equal, Assembler::pn, done);
 464 
 465     __ cmp(G3_scratch, (int)Bytecodes::_fast_iload);
 466     __ br(Assembler::equal, false, Assembler::pn, rewrite);
 467     __ delayed()->set(Bytecodes::_fast_iload2, G4_scratch);
 468 
 469     __ cmp(G3_scratch, (int)Bytecodes::_caload);
 470     __ br(Assembler::equal, false, Assembler::pn, rewrite);
 471     __ delayed()->set(Bytecodes::_fast_icaload, G4_scratch);
 472 
 473     __ set(Bytecodes::_fast_iload, G4_scratch);  // don't check again
 474     // rewrite
 475     // G4_scratch: fast bytecode
 476     __ bind(rewrite);
 477     patch_bytecode(Bytecodes::_iload, G4_scratch, G3_scratch, false);
 478     __ bind(done);
 479   }
 480 
 481   // Get the local value into tos
 482   locals_index(G3_scratch);
 483   __ access_local_int( G3_scratch, Otos_i );
 484 }
 485 
 486 void TemplateTable::fast_iload2() {
 487   transition(vtos, itos);
 488   locals_index(G3_scratch);
 489   __ access_local_int( G3_scratch, Otos_i );
 490   __ push_i();
 491   locals_index(G3_scratch, 3);  // get next bytecode's local index.
 492   __ access_local_int( G3_scratch, Otos_i );
 493 }
 494 
 495 void TemplateTable::fast_iload() {
 496   transition(vtos, itos);
 497   locals_index(G3_scratch);
 498   __ access_local_int( G3_scratch, Otos_i );
 499 }
 500 
 501 void TemplateTable::lload() {
 502   transition(vtos, ltos);
 503   locals_index(G3_scratch);
 504   __ access_local_long( G3_scratch, Otos_l );
 505 }
 506 
 507 
 508 void TemplateTable::fload() {
 509   transition(vtos, ftos);
 510   locals_index(G3_scratch);
 511   __ access_local_float( G3_scratch, Ftos_f );
 512 }
 513 
 514 
 515 void TemplateTable::dload() {
 516   transition(vtos, dtos);
 517   locals_index(G3_scratch);
 518   __ access_local_double( G3_scratch, Ftos_d );
 519 }
 520 
 521 
 522 void TemplateTable::aload() {
 523   transition(vtos, atos);
 524   locals_index(G3_scratch);
 525   __ access_local_ptr( G3_scratch, Otos_i);
 526 }
 527 
 528 
 529 void TemplateTable::wide_iload() {
 530   transition(vtos, itos);
 531   locals_index_wide(G3_scratch);
 532   __ access_local_int( G3_scratch, Otos_i );
 533 }
 534 
 535 
 536 void TemplateTable::wide_lload() {
 537   transition(vtos, ltos);
 538   locals_index_wide(G3_scratch);
 539   __ access_local_long( G3_scratch, Otos_l );
 540 }
 541 
 542 
 543 void TemplateTable::wide_fload() {
 544   transition(vtos, ftos);
 545   locals_index_wide(G3_scratch);
 546   __ access_local_float( G3_scratch, Ftos_f );
 547 }
 548 
 549 
 550 void TemplateTable::wide_dload() {
 551   transition(vtos, dtos);
 552   locals_index_wide(G3_scratch);
 553   __ access_local_double( G3_scratch, Ftos_d );
 554 }
 555 
 556 
 557 void TemplateTable::wide_aload() {
 558   transition(vtos, atos);
 559   locals_index_wide(G3_scratch);
 560   __ access_local_ptr( G3_scratch, Otos_i );
 561   __ verify_oop(Otos_i);
 562 }
 563 
 564 
 565 void TemplateTable::iaload() {
 566   transition(itos, itos);
 567   // Otos_i: index
 568   // tos: array
 569   __ index_check(O2, Otos_i, LogBytesPerInt, G3_scratch, O3);
 570   __ ld(O3, arrayOopDesc::base_offset_in_bytes(T_INT), Otos_i);
 571 }
 572 
 573 
 574 void TemplateTable::laload() {
 575   transition(itos, ltos);
 576   // Otos_i: index
 577   // O2: array
 578   __ index_check(O2, Otos_i, LogBytesPerLong, G3_scratch, O3);
 579   __ ld_long(O3, arrayOopDesc::base_offset_in_bytes(T_LONG), Otos_l);
 580 }
 581 
 582 
 583 void TemplateTable::faload() {
 584   transition(itos, ftos);
 585   // Otos_i: index
 586   // O2: array
 587   __ index_check(O2, Otos_i, LogBytesPerInt, G3_scratch, O3);
 588   __ ldf(FloatRegisterImpl::S, O3, arrayOopDesc::base_offset_in_bytes(T_FLOAT), Ftos_f);
 589 }
 590 
 591 
 592 void TemplateTable::daload() {
 593   transition(itos, dtos);
 594   // Otos_i: index
 595   // O2: array
 596   __ index_check(O2, Otos_i, LogBytesPerLong, G3_scratch, O3);
 597   __ ldf(FloatRegisterImpl::D, O3, arrayOopDesc::base_offset_in_bytes(T_DOUBLE), Ftos_d);
 598 }
 599 
 600 
 601 void TemplateTable::aaload() {
 602   transition(itos, atos);
 603   // Otos_i: index
 604   // tos: array
 605   __ index_check(O2, Otos_i, UseCompressedOops ? 2 : LogBytesPerWord, G3_scratch, O3);
 606   __ load_heap_oop(O3, arrayOopDesc::base_offset_in_bytes(T_OBJECT), Otos_i);
 607   __ verify_oop(Otos_i);
 608 }
 609 
 610 
 611 void TemplateTable::baload() {
 612   transition(itos, itos);
 613   // Otos_i: index
 614   // tos: array
 615   __ index_check(O2, Otos_i, 0, G3_scratch, O3);
 616   __ ldsb(O3, arrayOopDesc::base_offset_in_bytes(T_BYTE), Otos_i);
 617 }
 618 
 619 
 620 void TemplateTable::caload() {
 621   transition(itos, itos);
 622   // Otos_i: index
 623   // tos: array
 624   __ index_check(O2, Otos_i, LogBytesPerShort, G3_scratch, O3);
 625   __ lduh(O3, arrayOopDesc::base_offset_in_bytes(T_CHAR), Otos_i);
 626 }
 627 
 628 void TemplateTable::fast_icaload() {
 629   transition(vtos, itos);
 630   // Otos_i: index
 631   // tos: array
 632   locals_index(G3_scratch);
 633   __ access_local_int( G3_scratch, Otos_i );
 634   __ index_check(O2, Otos_i, LogBytesPerShort, G3_scratch, O3);
 635   __ lduh(O3, arrayOopDesc::base_offset_in_bytes(T_CHAR), Otos_i);
 636 }
 637 
 638 
 639 void TemplateTable::saload() {
 640   transition(itos, itos);
 641   // Otos_i: index
 642   // tos: array
 643   __ index_check(O2, Otos_i, LogBytesPerShort, G3_scratch, O3);
 644   __ ldsh(O3, arrayOopDesc::base_offset_in_bytes(T_SHORT), Otos_i);
 645 }
 646 
 647 
 648 void TemplateTable::iload(int n) {
 649   transition(vtos, itos);
 650   __ ld( Llocals, Interpreter::local_offset_in_bytes(n), Otos_i );
 651 }
 652 
 653 
 654 void TemplateTable::lload(int n) {
 655   transition(vtos, ltos);
 656   assert(n+1 < Argument::n_register_parameters, "would need more code");
 657   __ load_unaligned_long(Llocals, Interpreter::local_offset_in_bytes(n+1), Otos_l);
 658 }
 659 
 660 
 661 void TemplateTable::fload(int n) {
 662   transition(vtos, ftos);
 663   assert(n < Argument::n_register_parameters, "would need more code");
 664   __ ldf( FloatRegisterImpl::S, Llocals, Interpreter::local_offset_in_bytes(n),     Ftos_f );
 665 }
 666 
 667 
 668 void TemplateTable::dload(int n) {
 669   transition(vtos, dtos);
 670   FloatRegister dst = Ftos_d;
 671   __ load_unaligned_double(Llocals, Interpreter::local_offset_in_bytes(n+1), dst);
 672 }
 673 
 674 
 675 void TemplateTable::aload(int n) {
 676   transition(vtos, atos);
 677   __ ld_ptr( Llocals, Interpreter::local_offset_in_bytes(n), Otos_i );
 678 }
 679 
 680 void TemplateTable::aload_0() {
 681   aload_0_internal();
 682 }
 683 
 684 void TemplateTable::nofast_aload_0() {
 685   aload_0_internal(may_not_rewrite);
 686 }
 687 
 688 void TemplateTable::aload_0_internal(RewriteControl rc) {
 689   transition(vtos, atos);
 690 
 691   // According to bytecode histograms, the pairs:
 692   //
 693   // _aload_0, _fast_igetfield (itos)
 694   // _aload_0, _fast_agetfield (atos)
 695   // _aload_0, _fast_fgetfield (ftos)
 696   //
 697   // occur frequently. If RewriteFrequentPairs is set, the (slow) _aload_0
 698   // bytecode checks the next bytecode and then rewrites the current
 699   // bytecode into a pair bytecode; otherwise it rewrites the current
 700   // bytecode into _fast_aload_0 that doesn't do the pair check anymore.
 701   //
 702   if (RewriteFrequentPairs && rc == may_rewrite) {
 703     Label rewrite, done;
 704 
 705     // get next byte
 706     __ ldub(at_bcp(Bytecodes::length_for(Bytecodes::_aload_0)), G3_scratch);
 707 
 708     // do actual aload_0
 709     aload(0);
 710 
 711     // if _getfield then wait with rewrite
 712     __ cmp_and_br_short(G3_scratch, (int)Bytecodes::_getfield, Assembler::equal, Assembler::pn, done);
 713 
 714     // if _igetfield then rewrite to _fast_iaccess_0
 715     assert(Bytecodes::java_code(Bytecodes::_fast_iaccess_0) == Bytecodes::_aload_0, "adjust fast bytecode def");
 716     __ cmp(G3_scratch, (int)Bytecodes::_fast_igetfield);
 717     __ br(Assembler::equal, false, Assembler::pn, rewrite);
 718     __ delayed()->set(Bytecodes::_fast_iaccess_0, G4_scratch);
 719 
 720     // if _agetfield then rewrite to _fast_aaccess_0
 721     assert(Bytecodes::java_code(Bytecodes::_fast_aaccess_0) == Bytecodes::_aload_0, "adjust fast bytecode def");
 722     __ cmp(G3_scratch, (int)Bytecodes::_fast_agetfield);
 723     __ br(Assembler::equal, false, Assembler::pn, rewrite);
 724     __ delayed()->set(Bytecodes::_fast_aaccess_0, G4_scratch);
 725 
 726     // if _fgetfield then rewrite to _fast_faccess_0
 727     assert(Bytecodes::java_code(Bytecodes::_fast_faccess_0) == Bytecodes::_aload_0, "adjust fast bytecode def");
 728     __ cmp(G3_scratch, (int)Bytecodes::_fast_fgetfield);
 729     __ br(Assembler::equal, false, Assembler::pn, rewrite);
 730     __ delayed()->set(Bytecodes::_fast_faccess_0, G4_scratch);
 731 
 732     // else rewrite to _fast_aload0
 733     assert(Bytecodes::java_code(Bytecodes::_fast_aload_0) == Bytecodes::_aload_0, "adjust fast bytecode def");
 734     __ set(Bytecodes::_fast_aload_0, G4_scratch);
 735 
 736     // rewrite
 737     // G4_scratch: fast bytecode
 738     __ bind(rewrite);
 739     patch_bytecode(Bytecodes::_aload_0, G4_scratch, G3_scratch, false);
 740     __ bind(done);
 741   } else {
 742     aload(0);
 743   }
 744 }
 745 
 746 void TemplateTable::istore() {
 747   transition(itos, vtos);
 748   locals_index(G3_scratch);
 749   __ store_local_int( G3_scratch, Otos_i );
 750 }
 751 
 752 
 753 void TemplateTable::lstore() {
 754   transition(ltos, vtos);
 755   locals_index(G3_scratch);
 756   __ store_local_long( G3_scratch, Otos_l );
 757 }
 758 
 759 
 760 void TemplateTable::fstore() {
 761   transition(ftos, vtos);
 762   locals_index(G3_scratch);
 763   __ store_local_float( G3_scratch, Ftos_f );
 764 }
 765 
 766 
 767 void TemplateTable::dstore() {
 768   transition(dtos, vtos);
 769   locals_index(G3_scratch);
 770   __ store_local_double( G3_scratch, Ftos_d );
 771 }
 772 
 773 
 774 void TemplateTable::astore() {
 775   transition(vtos, vtos);
 776   __ load_ptr(0, Otos_i);
 777   __ inc(Lesp, Interpreter::stackElementSize);
 778   __ verify_oop_or_return_address(Otos_i, G3_scratch);
 779   locals_index(G3_scratch);
 780   __ store_local_ptr(G3_scratch, Otos_i);
 781 }
 782 
 783 
 784 void TemplateTable::wide_istore() {
 785   transition(vtos, vtos);
 786   __ pop_i();
 787   locals_index_wide(G3_scratch);
 788   __ store_local_int( G3_scratch, Otos_i );
 789 }
 790 
 791 
 792 void TemplateTable::wide_lstore() {
 793   transition(vtos, vtos);
 794   __ pop_l();
 795   locals_index_wide(G3_scratch);
 796   __ store_local_long( G3_scratch, Otos_l );
 797 }
 798 
 799 
 800 void TemplateTable::wide_fstore() {
 801   transition(vtos, vtos);
 802   __ pop_f();
 803   locals_index_wide(G3_scratch);
 804   __ store_local_float( G3_scratch, Ftos_f );
 805 }
 806 
 807 
 808 void TemplateTable::wide_dstore() {
 809   transition(vtos, vtos);
 810   __ pop_d();
 811   locals_index_wide(G3_scratch);
 812   __ store_local_double( G3_scratch, Ftos_d );
 813 }
 814 
 815 
 816 void TemplateTable::wide_astore() {
 817   transition(vtos, vtos);
 818   __ load_ptr(0, Otos_i);
 819   __ inc(Lesp, Interpreter::stackElementSize);
 820   __ verify_oop_or_return_address(Otos_i, G3_scratch);
 821   locals_index_wide(G3_scratch);
 822   __ store_local_ptr(G3_scratch, Otos_i);
 823 }
 824 
 825 
 826 void TemplateTable::iastore() {
 827   transition(itos, vtos);
 828   __ pop_i(O2); // index
 829   // Otos_i: val
 830   // O3: array
 831   __ index_check(O3, O2, LogBytesPerInt, G3_scratch, O2);
 832   __ st(Otos_i, O2, arrayOopDesc::base_offset_in_bytes(T_INT));
 833 }
 834 
 835 
 836 void TemplateTable::lastore() {
 837   transition(ltos, vtos);
 838   __ pop_i(O2); // index
 839   // Otos_l: val
 840   // O3: array
 841   __ index_check(O3, O2, LogBytesPerLong, G3_scratch, O2);
 842   __ st_long(Otos_l, O2, arrayOopDesc::base_offset_in_bytes(T_LONG));
 843 }
 844 
 845 
 846 void TemplateTable::fastore() {
 847   transition(ftos, vtos);
 848   __ pop_i(O2); // index
 849   // Ftos_f: val
 850   // O3: array
 851   __ index_check(O3, O2, LogBytesPerInt, G3_scratch, O2);
 852   __ stf(FloatRegisterImpl::S, Ftos_f, O2, arrayOopDesc::base_offset_in_bytes(T_FLOAT));
 853 }
 854 
 855 
 856 void TemplateTable::dastore() {
 857   transition(dtos, vtos);
 858   __ pop_i(O2); // index
 859   // Fos_d: val
 860   // O3: array
 861   __ index_check(O3, O2, LogBytesPerLong, G3_scratch, O2);
 862   __ stf(FloatRegisterImpl::D, Ftos_d, O2, arrayOopDesc::base_offset_in_bytes(T_DOUBLE));
 863 }
 864 
 865 
 866 void TemplateTable::aastore() {
 867   Label store_ok, is_null, done;
 868   transition(vtos, vtos);
 869   __ ld_ptr(Lesp, Interpreter::expr_offset_in_bytes(0), Otos_i);
 870   __ ld(Lesp, Interpreter::expr_offset_in_bytes(1), O2);         // get index
 871   __ ld_ptr(Lesp, Interpreter::expr_offset_in_bytes(2), O3);     // get array
 872   // Otos_i: val
 873   // O2: index
 874   // O3: array
 875   __ verify_oop(Otos_i);
 876   __ index_check_without_pop(O3, O2, UseCompressedOops ? 2 : LogBytesPerWord, G3_scratch, O1);
 877 
 878   // do array store check - check for NULL value first
 879   __ br_null_short( Otos_i, Assembler::pn, is_null );
 880 
 881   __ load_klass(O3, O4); // get array klass
 882   __ load_klass(Otos_i, O5); // get value klass
 883 
 884   // do fast instanceof cache test
 885 
 886   __ ld_ptr(O4,     in_bytes(ObjArrayKlass::element_klass_offset()),  O4);
 887 
 888   assert(Otos_i == O0, "just checking");
 889 
 890   // Otos_i:    value
 891   // O1:        addr - offset
 892   // O2:        index
 893   // O3:        array
 894   // O4:        array element klass
 895   // O5:        value klass
 896 
 897   // Address element(O1, 0, arrayOopDesc::base_offset_in_bytes(T_OBJECT));
 898 
 899   // Generate a fast subtype check.  Branch to store_ok if no
 900   // failure.  Throw if failure.
 901   __ gen_subtype_check( O5, O4, G3_scratch, G4_scratch, G1_scratch, store_ok );
 902 
 903   // Not a subtype; so must throw exception
 904   __ throw_if_not_x( Assembler::never, Interpreter::_throw_ArrayStoreException_entry, G3_scratch );
 905 
 906   // Store is OK.
 907   __ bind(store_ok);
 908   do_oop_store(_masm, O1, noreg, arrayOopDesc::base_offset_in_bytes(T_OBJECT), Otos_i, G3_scratch, _bs->kind(), true);
 909 
 910   __ ba(done);
 911   __ delayed()->inc(Lesp, 3* Interpreter::stackElementSize); // adj sp (pops array, index and value)
 912 
 913   __ bind(is_null);
 914   do_oop_store(_masm, O1, noreg, arrayOopDesc::base_offset_in_bytes(T_OBJECT), G0, G4_scratch, _bs->kind(), true);
 915 
 916   __ profile_null_seen(G3_scratch);
 917   __ inc(Lesp, 3* Interpreter::stackElementSize);     // adj sp (pops array, index and value)
 918   __ bind(done);
 919 }
 920 
 921 
 922 void TemplateTable::bastore() {
 923   transition(itos, vtos);
 924   __ pop_i(O2); // index
 925   // Otos_i: val
 926   // O2: index
 927   // O3: array
 928   __ index_check(O3, O2, 0, G3_scratch, O2);
 929   // Need to check whether array is boolean or byte
 930   // since both types share the bastore bytecode.
 931   __ load_klass(O3, G4_scratch);
 932   __ ld(G4_scratch, in_bytes(Klass::layout_helper_offset()), G4_scratch);
 933   __ set(Klass::layout_helper_boolean_diffbit(), G3_scratch);
 934   __ andcc(G3_scratch, G4_scratch, G0);
 935   Label L_skip;
 936   __ br(Assembler::zero, false, Assembler::pn, L_skip);
 937   __ delayed()->nop();
 938   __ and3(Otos_i, 1, Otos_i);  // if it is a T_BOOLEAN array, mask the stored value to 0/1
 939   __ bind(L_skip);
 940   __ stb(Otos_i, O2, arrayOopDesc::base_offset_in_bytes(T_BYTE));
 941 }
 942 
 943 
 944 void TemplateTable::castore() {
 945   transition(itos, vtos);
 946   __ pop_i(O2); // index
 947   // Otos_i: val
 948   // O3: array
 949   __ index_check(O3, O2, LogBytesPerShort, G3_scratch, O2);
 950   __ sth(Otos_i, O2, arrayOopDesc::base_offset_in_bytes(T_CHAR));
 951 }
 952 
 953 
 954 void TemplateTable::sastore() {
 955   // %%%%% Factor across platform
 956   castore();
 957 }
 958 
 959 
 960 void TemplateTable::istore(int n) {
 961   transition(itos, vtos);
 962   __ st(Otos_i, Llocals, Interpreter::local_offset_in_bytes(n));
 963 }
 964 
 965 
 966 void TemplateTable::lstore(int n) {
 967   transition(ltos, vtos);
 968   assert(n+1 < Argument::n_register_parameters, "only handle register cases");
 969   __ store_unaligned_long(Otos_l, Llocals, Interpreter::local_offset_in_bytes(n+1));
 970 
 971 }
 972 
 973 
 974 void TemplateTable::fstore(int n) {
 975   transition(ftos, vtos);
 976   assert(n < Argument::n_register_parameters, "only handle register cases");
 977   __ stf(FloatRegisterImpl::S, Ftos_f, Llocals, Interpreter::local_offset_in_bytes(n));
 978 }
 979 
 980 
 981 void TemplateTable::dstore(int n) {
 982   transition(dtos, vtos);
 983   FloatRegister src = Ftos_d;
 984   __ store_unaligned_double(src, Llocals, Interpreter::local_offset_in_bytes(n+1));
 985 }
 986 
 987 
 988 void TemplateTable::astore(int n) {
 989   transition(vtos, vtos);
 990   __ load_ptr(0, Otos_i);
 991   __ inc(Lesp, Interpreter::stackElementSize);
 992   __ verify_oop_or_return_address(Otos_i, G3_scratch);
 993   __ store_local_ptr(n, Otos_i);
 994 }
 995 
 996 
 997 void TemplateTable::pop() {
 998   transition(vtos, vtos);
 999   __ inc(Lesp, Interpreter::stackElementSize);
1000 }
1001 
1002 
1003 void TemplateTable::pop2() {
1004   transition(vtos, vtos);
1005   __ inc(Lesp, 2 * Interpreter::stackElementSize);
1006 }
1007 
1008 
1009 void TemplateTable::dup() {
1010   transition(vtos, vtos);
1011   // stack: ..., a
1012   // load a and tag
1013   __ load_ptr(0, Otos_i);
1014   __ push_ptr(Otos_i);
1015   // stack: ..., a, a
1016 }
1017 
1018 
1019 void TemplateTable::dup_x1() {
1020   transition(vtos, vtos);
1021   // stack: ..., a, b
1022   __ load_ptr( 1, G3_scratch);  // get a
1023   __ load_ptr( 0, Otos_l1);     // get b
1024   __ store_ptr(1, Otos_l1);     // put b
1025   __ store_ptr(0, G3_scratch);  // put a - like swap
1026   __ push_ptr(Otos_l1);         // push b
1027   // stack: ..., b, a, b
1028 }
1029 
1030 
1031 void TemplateTable::dup_x2() {
1032   transition(vtos, vtos);
1033   // stack: ..., a, b, c
1034   // get c and push on stack, reuse registers
1035   __ load_ptr( 0, G3_scratch);  // get c
1036   __ push_ptr(G3_scratch);      // push c with tag
1037   // stack: ..., a, b, c, c  (c in reg)  (Lesp - 4)
1038   // (stack offsets n+1 now)
1039   __ load_ptr( 3, Otos_l1);     // get a
1040   __ store_ptr(3, G3_scratch);  // put c at 3
1041   // stack: ..., c, b, c, c  (a in reg)
1042   __ load_ptr( 2, G3_scratch);  // get b
1043   __ store_ptr(2, Otos_l1);     // put a at 2
1044   // stack: ..., c, a, c, c  (b in reg)
1045   __ store_ptr(1, G3_scratch);  // put b at 1
1046   // stack: ..., c, a, b, c
1047 }
1048 
1049 
1050 void TemplateTable::dup2() {
1051   transition(vtos, vtos);
1052   __ load_ptr(1, G3_scratch);  // get a
1053   __ load_ptr(0, Otos_l1);     // get b
1054   __ push_ptr(G3_scratch);     // push a
1055   __ push_ptr(Otos_l1);        // push b
1056   // stack: ..., a, b, a, b
1057 }
1058 
1059 
1060 void TemplateTable::dup2_x1() {
1061   transition(vtos, vtos);
1062   // stack: ..., a, b, c
1063   __ load_ptr( 1, Lscratch);    // get b
1064   __ load_ptr( 2, Otos_l1);     // get a
1065   __ store_ptr(2, Lscratch);    // put b at a
1066   // stack: ..., b, b, c
1067   __ load_ptr( 0, G3_scratch);  // get c
1068   __ store_ptr(1, G3_scratch);  // put c at b
1069   // stack: ..., b, c, c
1070   __ store_ptr(0, Otos_l1);     // put a at c
1071   // stack: ..., b, c, a
1072   __ push_ptr(Lscratch);        // push b
1073   __ push_ptr(G3_scratch);      // push c
1074   // stack: ..., b, c, a, b, c
1075 }
1076 
1077 
1078 // The spec says that these types can be a mixture of category 1 (1 word)
1079 // types and/or category 2 types (long and doubles)
1080 void TemplateTable::dup2_x2() {
1081   transition(vtos, vtos);
1082   // stack: ..., a, b, c, d
1083   __ load_ptr( 1, Lscratch);    // get c
1084   __ load_ptr( 3, Otos_l1);     // get a
1085   __ store_ptr(3, Lscratch);    // put c at 3
1086   __ store_ptr(1, Otos_l1);     // put a at 1
1087   // stack: ..., c, b, a, d
1088   __ load_ptr( 2, G3_scratch);  // get b
1089   __ load_ptr( 0, Otos_l1);     // get d
1090   __ store_ptr(0, G3_scratch);  // put b at 0
1091   __ store_ptr(2, Otos_l1);     // put d at 2
1092   // stack: ..., c, d, a, b
1093   __ push_ptr(Lscratch);        // push c
1094   __ push_ptr(Otos_l1);         // push d
1095   // stack: ..., c, d, a, b, c, d
1096 }
1097 
1098 
1099 void TemplateTable::swap() {
1100   transition(vtos, vtos);
1101   // stack: ..., a, b
1102   __ load_ptr( 1, G3_scratch);  // get a
1103   __ load_ptr( 0, Otos_l1);     // get b
1104   __ store_ptr(0, G3_scratch);  // put b
1105   __ store_ptr(1, Otos_l1);     // put a
1106   // stack: ..., b, a
1107 }
1108 
1109 
1110 void TemplateTable::iop2(Operation op) {
1111   transition(itos, itos);
1112   __ pop_i(O1);
1113   switch (op) {
1114    case  add:  __  add(O1, Otos_i, Otos_i);  break;
1115    case  sub:  __  sub(O1, Otos_i, Otos_i);  break;
1116      // %%%%% Mul may not exist: better to call .mul?
1117    case  mul:  __ smul(O1, Otos_i, Otos_i);  break;
1118    case _and:  __ and3(O1, Otos_i, Otos_i);  break;
1119    case  _or:  __  or3(O1, Otos_i, Otos_i);  break;
1120    case _xor:  __ xor3(O1, Otos_i, Otos_i);  break;
1121    case  shl:  __  sll(O1, Otos_i, Otos_i);  break;
1122    case  shr:  __  sra(O1, Otos_i, Otos_i);  break;
1123    case ushr:  __  srl(O1, Otos_i, Otos_i);  break;
1124    default: ShouldNotReachHere();
1125   }
1126 }
1127 
1128 
1129 void TemplateTable::lop2(Operation op) {
1130   transition(ltos, ltos);
1131   __ pop_l(O2);
1132   switch (op) {
1133 #ifdef _LP64
1134    case  add:  __  add(O2, Otos_l, Otos_l);  break;
1135    case  sub:  __  sub(O2, Otos_l, Otos_l);  break;
1136    case _and:  __ and3(O2, Otos_l, Otos_l);  break;
1137    case  _or:  __  or3(O2, Otos_l, Otos_l);  break;
1138    case _xor:  __ xor3(O2, Otos_l, Otos_l);  break;
1139 #else
1140    case  add:  __ addcc(O3, Otos_l2, Otos_l2);  __ addc(O2, Otos_l1, Otos_l1);  break;
1141    case  sub:  __ subcc(O3, Otos_l2, Otos_l2);  __ subc(O2, Otos_l1, Otos_l1);  break;
1142    case _and:  __  and3(O3, Otos_l2, Otos_l2);  __ and3(O2, Otos_l1, Otos_l1);  break;
1143    case  _or:  __   or3(O3, Otos_l2, Otos_l2);  __  or3(O2, Otos_l1, Otos_l1);  break;
1144    case _xor:  __  xor3(O3, Otos_l2, Otos_l2);  __ xor3(O2, Otos_l1, Otos_l1);  break;
1145 #endif
1146    default: ShouldNotReachHere();
1147   }
1148 }
1149 
1150 
1151 void TemplateTable::idiv() {
1152   // %%%%% Later: ForSPARC/V7 call .sdiv library routine,
1153   // %%%%% Use ldsw...sdivx on pure V9 ABI. 64 bit safe.
1154 
1155   transition(itos, itos);
1156   __ pop_i(O1); // get 1st op
1157 
1158   // Y contains upper 32 bits of result, set it to 0 or all ones
1159   __ wry(G0);
1160   __ mov(~0, G3_scratch);
1161 
1162   __ tst(O1);
1163      Label neg;
1164   __ br(Assembler::negative, true, Assembler::pn, neg);
1165   __ delayed()->wry(G3_scratch);
1166   __ bind(neg);
1167 
1168      Label ok;
1169   __ tst(Otos_i);
1170   __ throw_if_not_icc( Assembler::notZero, Interpreter::_throw_ArithmeticException_entry, G3_scratch );
1171 
1172   const int min_int = 0x80000000;
1173   Label regular;
1174   __ cmp(Otos_i, -1);
1175   __ br(Assembler::notEqual, false, Assembler::pt, regular);
1176 #ifdef _LP64
1177   // Don't put set in delay slot
1178   // Set will turn into multiple instructions in 64 bit mode
1179   __ delayed()->nop();
1180   __ set(min_int, G4_scratch);
1181 #else
1182   __ delayed()->set(min_int, G4_scratch);
1183 #endif
1184   Label done;
1185   __ cmp(O1, G4_scratch);
1186   __ br(Assembler::equal, true, Assembler::pt, done);
1187   __ delayed()->mov(O1, Otos_i);   // (mov only executed if branch taken)
1188 
1189   __ bind(regular);
1190   __ sdiv(O1, Otos_i, Otos_i); // note: irem uses O1 after this instruction!
1191   __ bind(done);
1192 }
1193 
1194 
1195 void TemplateTable::irem() {
1196   transition(itos, itos);
1197   __ mov(Otos_i, O2); // save divisor
1198   idiv();                               // %%%% Hack: exploits fact that idiv leaves dividend in O1
1199   __ smul(Otos_i, O2, Otos_i);
1200   __ sub(O1, Otos_i, Otos_i);
1201 }
1202 
1203 
1204 void TemplateTable::lmul() {
1205   transition(ltos, ltos);
1206   __ pop_l(O2);
1207 #ifdef _LP64
1208   __ mulx(Otos_l, O2, Otos_l);
1209 #else
1210   __ call_VM_leaf(Lscratch, CAST_FROM_FN_PTR(address, SharedRuntime::lmul));
1211 #endif
1212 
1213 }
1214 
1215 
1216 void TemplateTable::ldiv() {
1217   transition(ltos, ltos);
1218 
1219   // check for zero
1220   __ pop_l(O2);
1221 #ifdef _LP64
1222   __ tst(Otos_l);
1223   __ throw_if_not_xcc( Assembler::notZero, Interpreter::_throw_ArithmeticException_entry, G3_scratch);
1224   __ sdivx(O2, Otos_l, Otos_l);
1225 #else
1226   __ orcc(Otos_l1, Otos_l2, G0);
1227   __ throw_if_not_icc( Assembler::notZero, Interpreter::_throw_ArithmeticException_entry, G3_scratch);
1228   __ call_VM_leaf(Lscratch, CAST_FROM_FN_PTR(address, SharedRuntime::ldiv));
1229 #endif
1230 }
1231 
1232 
1233 void TemplateTable::lrem() {
1234   transition(ltos, ltos);
1235 
1236   // check for zero
1237   __ pop_l(O2);
1238 #ifdef _LP64
1239   __ tst(Otos_l);
1240   __ throw_if_not_xcc( Assembler::notZero, Interpreter::_throw_ArithmeticException_entry, G3_scratch);
1241   __ sdivx(O2, Otos_l, Otos_l2);
1242   __ mulx (Otos_l2, Otos_l, Otos_l2);
1243   __ sub  (O2, Otos_l2, Otos_l);
1244 #else
1245   __ orcc(Otos_l1, Otos_l2, G0);
1246   __ throw_if_not_icc(Assembler::notZero, Interpreter::_throw_ArithmeticException_entry, G3_scratch);
1247   __ call_VM_leaf(Lscratch, CAST_FROM_FN_PTR(address, SharedRuntime::lrem));
1248 #endif
1249 }
1250 
1251 
1252 void TemplateTable::lshl() {
1253   transition(itos, ltos); // %%%% could optimize, fill delay slot or opt for ultra
1254 
1255   __ pop_l(O2);                          // shift value in O2, O3
1256 #ifdef _LP64
1257   __ sllx(O2, Otos_i, Otos_l);
1258 #else
1259   __ lshl(O2, O3, Otos_i, Otos_l1, Otos_l2, O4);
1260 #endif
1261 }
1262 
1263 
1264 void TemplateTable::lshr() {
1265   transition(itos, ltos); // %%%% see lshl comment
1266 
1267   __ pop_l(O2);                          // shift value in O2, O3
1268 #ifdef _LP64
1269   __ srax(O2, Otos_i, Otos_l);
1270 #else
1271   __ lshr(O2, O3, Otos_i, Otos_l1, Otos_l2, O4);
1272 #endif
1273 }
1274 
1275 
1276 
1277 void TemplateTable::lushr() {
1278   transition(itos, ltos); // %%%% see lshl comment
1279 
1280   __ pop_l(O2);                          // shift value in O2, O3
1281 #ifdef _LP64
1282   __ srlx(O2, Otos_i, Otos_l);
1283 #else
1284   __ lushr(O2, O3, Otos_i, Otos_l1, Otos_l2, O4);
1285 #endif
1286 }
1287 
1288 
1289 void TemplateTable::fop2(Operation op) {
1290   transition(ftos, ftos);
1291   switch (op) {
1292    case  add:  __  pop_f(F4); __ fadd(FloatRegisterImpl::S, F4, Ftos_f, Ftos_f);  break;
1293    case  sub:  __  pop_f(F4); __ fsub(FloatRegisterImpl::S, F4, Ftos_f, Ftos_f);  break;
1294    case  mul:  __  pop_f(F4); __ fmul(FloatRegisterImpl::S, F4, Ftos_f, Ftos_f);  break;
1295    case  div:  __  pop_f(F4); __ fdiv(FloatRegisterImpl::S, F4, Ftos_f, Ftos_f);  break;
1296    case  rem:
1297      assert(Ftos_f == F0, "just checking");
1298 #ifdef _LP64
1299      // LP64 calling conventions use F1, F3 for passing 2 floats
1300      __ pop_f(F1);
1301      __ fmov(FloatRegisterImpl::S, Ftos_f, F3);
1302 #else
1303      __ pop_i(O0);
1304      __ stf(FloatRegisterImpl::S, Ftos_f, __ d_tmp);
1305      __ ld( __ d_tmp, O1 );
1306 #endif
1307      __ call_VM_leaf(Lscratch, CAST_FROM_FN_PTR(address, SharedRuntime::frem));
1308      assert( Ftos_f == F0, "fix this code" );
1309      break;
1310 
1311    default: ShouldNotReachHere();
1312   }
1313 }
1314 
1315 
1316 void TemplateTable::dop2(Operation op) {
1317   transition(dtos, dtos);
1318   switch (op) {
1319    case  add:  __  pop_d(F4); __ fadd(FloatRegisterImpl::D, F4, Ftos_d, Ftos_d);  break;
1320    case  sub:  __  pop_d(F4); __ fsub(FloatRegisterImpl::D, F4, Ftos_d, Ftos_d);  break;
1321    case  mul:  __  pop_d(F4); __ fmul(FloatRegisterImpl::D, F4, Ftos_d, Ftos_d);  break;
1322    case  div:  __  pop_d(F4); __ fdiv(FloatRegisterImpl::D, F4, Ftos_d, Ftos_d);  break;
1323    case  rem:
1324 #ifdef _LP64
1325      // Pass arguments in D0, D2
1326      __ fmov(FloatRegisterImpl::D, Ftos_f, F2 );
1327      __ pop_d( F0 );
1328 #else
1329      // Pass arguments in O0O1, O2O3
1330      __ stf(FloatRegisterImpl::D, Ftos_f, __ d_tmp);
1331      __ ldd( __ d_tmp, O2 );
1332      __ pop_d(Ftos_f);
1333      __ stf(FloatRegisterImpl::D, Ftos_f, __ d_tmp);
1334      __ ldd( __ d_tmp, O0 );
1335 #endif
1336      __ call_VM_leaf(Lscratch, CAST_FROM_FN_PTR(address, SharedRuntime::drem));
1337      assert( Ftos_d == F0, "fix this code" );
1338      break;
1339 
1340    default: ShouldNotReachHere();
1341   }
1342 }
1343 
1344 
1345 void TemplateTable::ineg() {
1346   transition(itos, itos);
1347   __ neg(Otos_i);
1348 }
1349 
1350 
1351 void TemplateTable::lneg() {
1352   transition(ltos, ltos);
1353 #ifdef _LP64
1354   __ sub(G0, Otos_l, Otos_l);
1355 #else
1356   __ lneg(Otos_l1, Otos_l2);
1357 #endif
1358 }
1359 
1360 
1361 void TemplateTable::fneg() {
1362   transition(ftos, ftos);
1363   __ fneg(FloatRegisterImpl::S, Ftos_f, Ftos_f);
1364 }
1365 
1366 
1367 void TemplateTable::dneg() {
1368   transition(dtos, dtos);
1369   __ fneg(FloatRegisterImpl::D, Ftos_f, Ftos_f);
1370 }
1371 
1372 
1373 void TemplateTable::iinc() {
1374   transition(vtos, vtos);
1375   locals_index(G3_scratch);
1376   __ ldsb(Lbcp, 2, O2);  // load constant
1377   __ access_local_int(G3_scratch, Otos_i);
1378   __ add(Otos_i, O2, Otos_i);
1379   __ st(Otos_i, G3_scratch, 0);    // access_local_int puts E.A. in G3_scratch
1380 }
1381 
1382 
1383 void TemplateTable::wide_iinc() {
1384   transition(vtos, vtos);
1385   locals_index_wide(G3_scratch);
1386   __ get_2_byte_integer_at_bcp( 4,  O2, O3, InterpreterMacroAssembler::Signed);
1387   __ access_local_int(G3_scratch, Otos_i);
1388   __ add(Otos_i, O3, Otos_i);
1389   __ st(Otos_i, G3_scratch, 0);    // access_local_int puts E.A. in G3_scratch
1390 }
1391 
1392 
1393 void TemplateTable::convert() {
1394 // %%%%% Factor this first part accross platforms
1395   #ifdef ASSERT
1396     TosState tos_in  = ilgl;
1397     TosState tos_out = ilgl;
1398     switch (bytecode()) {
1399       case Bytecodes::_i2l: // fall through
1400       case Bytecodes::_i2f: // fall through
1401       case Bytecodes::_i2d: // fall through
1402       case Bytecodes::_i2b: // fall through
1403       case Bytecodes::_i2c: // fall through
1404       case Bytecodes::_i2s: tos_in = itos; break;
1405       case Bytecodes::_l2i: // fall through
1406       case Bytecodes::_l2f: // fall through
1407       case Bytecodes::_l2d: tos_in = ltos; break;
1408       case Bytecodes::_f2i: // fall through
1409       case Bytecodes::_f2l: // fall through
1410       case Bytecodes::_f2d: tos_in = ftos; break;
1411       case Bytecodes::_d2i: // fall through
1412       case Bytecodes::_d2l: // fall through
1413       case Bytecodes::_d2f: tos_in = dtos; break;
1414       default             : ShouldNotReachHere();
1415     }
1416     switch (bytecode()) {
1417       case Bytecodes::_l2i: // fall through
1418       case Bytecodes::_f2i: // fall through
1419       case Bytecodes::_d2i: // fall through
1420       case Bytecodes::_i2b: // fall through
1421       case Bytecodes::_i2c: // fall through
1422       case Bytecodes::_i2s: tos_out = itos; break;
1423       case Bytecodes::_i2l: // fall through
1424       case Bytecodes::_f2l: // fall through
1425       case Bytecodes::_d2l: tos_out = ltos; break;
1426       case Bytecodes::_i2f: // fall through
1427       case Bytecodes::_l2f: // fall through
1428       case Bytecodes::_d2f: tos_out = ftos; break;
1429       case Bytecodes::_i2d: // fall through
1430       case Bytecodes::_l2d: // fall through
1431       case Bytecodes::_f2d: tos_out = dtos; break;
1432       default             : ShouldNotReachHere();
1433     }
1434     transition(tos_in, tos_out);
1435   #endif
1436 
1437 
1438   // Conversion
1439   Label done;
1440   switch (bytecode()) {
1441    case Bytecodes::_i2l:
1442 #ifdef _LP64
1443     // Sign extend the 32 bits
1444     __ sra ( Otos_i, 0, Otos_l );
1445 #else
1446     __ addcc(Otos_i, 0, Otos_l2);
1447     __ br(Assembler::greaterEqual, true, Assembler::pt, done);
1448     __ delayed()->clr(Otos_l1);
1449     __ set(~0, Otos_l1);
1450 #endif
1451     break;
1452 
1453    case Bytecodes::_i2f:
1454     __ st(Otos_i, __ d_tmp );
1455     __ ldf(FloatRegisterImpl::S,  __ d_tmp, F0);
1456     __ fitof(FloatRegisterImpl::S, F0, Ftos_f);
1457     break;
1458 
1459    case Bytecodes::_i2d:
1460     __ st(Otos_i, __ d_tmp);
1461     __ ldf(FloatRegisterImpl::S,  __ d_tmp, F0);
1462     __ fitof(FloatRegisterImpl::D, F0, Ftos_f);
1463     break;
1464 
1465    case Bytecodes::_i2b:
1466     __ sll(Otos_i, 24, Otos_i);
1467     __ sra(Otos_i, 24, Otos_i);
1468     break;
1469 
1470    case Bytecodes::_i2c:
1471     __ sll(Otos_i, 16, Otos_i);
1472     __ srl(Otos_i, 16, Otos_i);
1473     break;
1474 
1475    case Bytecodes::_i2s:
1476     __ sll(Otos_i, 16, Otos_i);
1477     __ sra(Otos_i, 16, Otos_i);
1478     break;
1479 
1480    case Bytecodes::_l2i:
1481 #ifndef _LP64
1482     __ mov(Otos_l2, Otos_i);
1483 #else
1484     // Sign-extend into the high 32 bits
1485     __ sra(Otos_l, 0, Otos_i);
1486 #endif
1487     break;
1488 
1489    case Bytecodes::_l2f:
1490    case Bytecodes::_l2d:
1491     __ st_long(Otos_l, __ d_tmp);
1492     __ ldf(FloatRegisterImpl::D, __ d_tmp, Ftos_d);
1493 
1494     if (bytecode() == Bytecodes::_l2f) {
1495       __ fxtof(FloatRegisterImpl::S, Ftos_d, Ftos_f);
1496     } else {
1497       __ fxtof(FloatRegisterImpl::D, Ftos_d, Ftos_d);
1498     }
1499     break;
1500 
1501   case Bytecodes::_f2i:  {
1502       Label isNaN;
1503       // result must be 0 if value is NaN; test by comparing value to itself
1504       __ fcmp(FloatRegisterImpl::S, Assembler::fcc0, Ftos_f, Ftos_f);
1505       __ fb(Assembler::f_unordered, true, Assembler::pn, isNaN);
1506       __ delayed()->clr(Otos_i);                                     // NaN
1507       __ ftoi(FloatRegisterImpl::S, Ftos_f, F30);
1508       __ stf(FloatRegisterImpl::S, F30, __ d_tmp);
1509       __ ld(__ d_tmp, Otos_i);
1510       __ bind(isNaN);
1511     }
1512     break;
1513 
1514    case Bytecodes::_f2l:
1515     // must uncache tos
1516     __ push_f();
1517 #ifdef _LP64
1518     __ pop_f(F1);
1519 #else
1520     __ pop_i(O0);
1521 #endif
1522     __ call_VM_leaf(Lscratch, CAST_FROM_FN_PTR(address, SharedRuntime::f2l));
1523     break;
1524 
1525    case Bytecodes::_f2d:
1526     __ ftof( FloatRegisterImpl::S, FloatRegisterImpl::D, Ftos_f, Ftos_f);
1527     break;
1528 
1529    case Bytecodes::_d2i:
1530    case Bytecodes::_d2l:
1531     // must uncache tos
1532     __ push_d();
1533 #ifdef _LP64
1534     // LP64 calling conventions pass first double arg in D0
1535     __ pop_d( Ftos_d );
1536 #else
1537     __ pop_i( O0 );
1538     __ pop_i( O1 );
1539 #endif
1540     __ call_VM_leaf(Lscratch,
1541         bytecode() == Bytecodes::_d2i
1542           ? CAST_FROM_FN_PTR(address, SharedRuntime::d2i)
1543           : CAST_FROM_FN_PTR(address, SharedRuntime::d2l));
1544     break;
1545 
1546     case Bytecodes::_d2f:
1547       __ ftof( FloatRegisterImpl::D, FloatRegisterImpl::S, Ftos_d, Ftos_f);
1548     break;
1549 
1550     default: ShouldNotReachHere();
1551   }
1552   __ bind(done);
1553 }
1554 
1555 
1556 void TemplateTable::lcmp() {
1557   transition(ltos, itos);
1558 
1559 #ifdef _LP64
1560   __ pop_l(O1); // pop off value 1, value 2 is in O0
1561   __ lcmp( O1, Otos_l, Otos_i );
1562 #else
1563   __ pop_l(O2); // cmp O2,3 to O0,1
1564   __ lcmp( O2, O3, Otos_l1, Otos_l2, Otos_i );
1565 #endif
1566 }
1567 
1568 
1569 void TemplateTable::float_cmp(bool is_float, int unordered_result) {
1570 
1571   if (is_float) __ pop_f(F2);
1572   else          __ pop_d(F2);
1573 
1574   assert(Ftos_f == F0  &&  Ftos_d == F0,  "alias checking:");
1575 
1576   __ float_cmp( is_float, unordered_result, F2, F0, Otos_i );
1577 }
1578 
1579 void TemplateTable::branch(bool is_jsr, bool is_wide) {
1580   // Note: on SPARC, we use InterpreterMacroAssembler::if_cmp also.
1581   __ verify_thread();
1582 
1583   const Register O2_bumped_count = O2;
1584   __ profile_taken_branch(G3_scratch, O2_bumped_count);
1585 
1586   // get (wide) offset to O1_disp
1587   const Register O1_disp = O1;
1588   if (is_wide)  __ get_4_byte_integer_at_bcp( 1,  G4_scratch, O1_disp,                                    InterpreterMacroAssembler::set_CC);
1589   else          __ get_2_byte_integer_at_bcp( 1,  G4_scratch, O1_disp, InterpreterMacroAssembler::Signed, InterpreterMacroAssembler::set_CC);
1590 
1591   // Handle all the JSR stuff here, then exit.
1592   // It's much shorter and cleaner than intermingling with the
1593   // non-JSR normal-branch stuff occurring below.
1594   if( is_jsr ) {
1595     // compute return address as bci in Otos_i
1596     __ ld_ptr(Lmethod, Method::const_offset(), G3_scratch);
1597     __ sub(Lbcp, G3_scratch, G3_scratch);
1598     __ sub(G3_scratch, in_bytes(ConstMethod::codes_offset()) - (is_wide ? 5 : 3), Otos_i);
1599 
1600     // Bump Lbcp to target of JSR
1601     __ add(Lbcp, O1_disp, Lbcp);
1602     // Push returnAddress for "ret" on stack
1603     __ push_ptr(Otos_i);
1604     // And away we go!
1605     __ dispatch_next(vtos);
1606     return;
1607   }
1608 
1609   // Normal (non-jsr) branch handling
1610 
1611   // Save the current Lbcp
1612   const Register l_cur_bcp = Lscratch;
1613   __ mov( Lbcp, l_cur_bcp );
1614 
1615   bool increment_invocation_counter_for_backward_branches = UseCompiler && UseLoopCounter;
1616   if ( increment_invocation_counter_for_backward_branches ) {
1617     Label Lforward;
1618     // check branch direction
1619     __ br( Assembler::positive, false,  Assembler::pn, Lforward );
1620     // Bump bytecode pointer by displacement (take the branch)
1621     __ delayed()->add( O1_disp, Lbcp, Lbcp );     // add to bc addr
1622 
1623     const Register G3_method_counters = G3_scratch;
1624     __ get_method_counters(Lmethod, G3_method_counters, Lforward);
1625 
1626     if (TieredCompilation) {
1627       Label Lno_mdo, Loverflow;
1628       int increment = InvocationCounter::count_increment;
1629       if (ProfileInterpreter) {
1630         // If no method data exists, go to profile_continue.
1631         __ ld_ptr(Lmethod, Method::method_data_offset(), G4_scratch);
1632         __ br_null_short(G4_scratch, Assembler::pn, Lno_mdo);
1633 
1634         // Increment backedge counter in the MDO
1635         Address mdo_backedge_counter(G4_scratch, in_bytes(MethodData::backedge_counter_offset()) +
1636                                                  in_bytes(InvocationCounter::counter_offset()));
1637         Address mask(G4_scratch, in_bytes(MethodData::backedge_mask_offset()));
1638         __ increment_mask_and_jump(mdo_backedge_counter, increment, mask, G3_scratch, O0,
1639                                    (UseOnStackReplacement ? Assembler::notZero : Assembler::always), &Lforward);
1640         __ ba_short(Loverflow);
1641       }
1642 
1643       // If there's no MDO, increment counter in MethodCounters*
1644       __ bind(Lno_mdo);
1645       Address backedge_counter(G3_method_counters,
1646               in_bytes(MethodCounters::backedge_counter_offset()) +
1647               in_bytes(InvocationCounter::counter_offset()));
1648       Address mask(G3_method_counters, in_bytes(MethodCounters::backedge_mask_offset()));
1649       __ increment_mask_and_jump(backedge_counter, increment, mask, G4_scratch, O0,
1650                                  (UseOnStackReplacement ? Assembler::notZero : Assembler::always), &Lforward);
1651       __ bind(Loverflow);
1652 
1653       // notify point for loop, pass branch bytecode
1654       __ call_VM(noreg, CAST_FROM_FN_PTR(address, InterpreterRuntime::frequency_counter_overflow), l_cur_bcp);
1655 
1656       // Was an OSR adapter generated?
1657       // O0 = osr nmethod
1658       __ br_null_short(O0, Assembler::pn, Lforward);
1659 
1660       // Has the nmethod been invalidated already?
1661       __ ldub(O0, nmethod::state_offset(), O2);
1662       __ cmp_and_br_short(O2, nmethod::in_use, Assembler::notEqual, Assembler::pn, Lforward);
1663 
1664       // migrate the interpreter frame off of the stack
1665 
1666       __ mov(G2_thread, L7);
1667       // save nmethod
1668       __ mov(O0, L6);
1669       __ set_last_Java_frame(SP, noreg);
1670       __ call_VM_leaf(noreg, CAST_FROM_FN_PTR(address, SharedRuntime::OSR_migration_begin), L7);
1671       __ reset_last_Java_frame();
1672       __ mov(L7, G2_thread);
1673 
1674       // move OSR nmethod to I1
1675       __ mov(L6, I1);
1676 
1677       // OSR buffer to I0
1678       __ mov(O0, I0);
1679 
1680       // remove the interpreter frame
1681       __ restore(I5_savedSP, 0, SP);
1682 
1683       // Jump to the osr code.
1684       __ ld_ptr(O1, nmethod::osr_entry_point_offset(), O2);
1685       __ jmp(O2, G0);
1686       __ delayed()->nop();
1687 
1688     } else { // not TieredCompilation
1689       // Update Backedge branch separately from invocations
1690       const Register G4_invoke_ctr = G4;
1691       __ increment_backedge_counter(G3_method_counters, G4_invoke_ctr, G1_scratch);
1692       if (ProfileInterpreter) {
1693         __ test_invocation_counter_for_mdp(G4_invoke_ctr, G3_method_counters, G1_scratch, Lforward);
1694         if (UseOnStackReplacement) {
1695 
1696           __ test_backedge_count_for_osr(O2_bumped_count, G3_method_counters, l_cur_bcp, G1_scratch);
1697         }
1698       } else {
1699         if (UseOnStackReplacement) {
1700           __ test_backedge_count_for_osr(G4_invoke_ctr, G3_method_counters, l_cur_bcp, G1_scratch);
1701         }
1702       }
1703     }
1704 
1705     __ bind(Lforward);
1706   } else
1707     // Bump bytecode pointer by displacement (take the branch)
1708     __ add( O1_disp, Lbcp, Lbcp );// add to bc addr
1709 
1710   // continue with bytecode @ target
1711   // %%%%% Like Intel, could speed things up by moving bytecode fetch to code above,
1712   // %%%%% and changing dispatch_next to dispatch_only
1713   __ dispatch_next(vtos);
1714 }
1715 
1716 
1717 // Note Condition in argument is TemplateTable::Condition
1718 // arg scope is within class scope
1719 
1720 void TemplateTable::if_0cmp(Condition cc) {
1721   // no pointers, integer only!
1722   transition(itos, vtos);
1723   // assume branch is more often taken than not (loops use backward branches)
1724   __ cmp( Otos_i, 0);
1725   __ if_cmp(ccNot(cc), false);
1726 }
1727 
1728 
1729 void TemplateTable::if_icmp(Condition cc) {
1730   transition(itos, vtos);
1731   __ pop_i(O1);
1732   __ cmp(O1, Otos_i);
1733   __ if_cmp(ccNot(cc), false);
1734 }
1735 
1736 
1737 void TemplateTable::if_nullcmp(Condition cc) {
1738   transition(atos, vtos);
1739   __ tst(Otos_i);
1740   __ if_cmp(ccNot(cc), true);
1741 }
1742 
1743 
1744 void TemplateTable::if_acmp(Condition cc) {
1745   transition(atos, vtos);
1746   __ pop_ptr(O1);
1747   __ verify_oop(O1);
1748   __ verify_oop(Otos_i);
1749   __ cmp(O1, Otos_i);
1750   __ if_cmp(ccNot(cc), true);
1751 }
1752 
1753 
1754 
1755 void TemplateTable::ret() {
1756   transition(vtos, vtos);
1757   locals_index(G3_scratch);
1758   __ access_local_returnAddress(G3_scratch, Otos_i);
1759   // Otos_i contains the bci, compute the bcp from that
1760 
1761 #ifdef _LP64
1762 #ifdef ASSERT
1763   // jsr result was labeled as an 'itos' not an 'atos' because we cannot GC
1764   // the result.  The return address (really a BCI) was stored with an
1765   // 'astore' because JVM specs claim it's a pointer-sized thing.  Hence in
1766   // the 64-bit build the 32-bit BCI is actually in the low bits of a 64-bit
1767   // loaded value.
1768   { Label zzz ;
1769      __ set (65536, G3_scratch) ;
1770      __ cmp (Otos_i, G3_scratch) ;
1771      __ bp( Assembler::lessEqualUnsigned, false, Assembler::xcc, Assembler::pn, zzz);
1772      __ delayed()->nop();
1773      __ stop("BCI is in the wrong register half?");
1774      __ bind (zzz) ;
1775   }
1776 #endif
1777 #endif
1778 
1779   __ profile_ret(vtos, Otos_i, G4_scratch);
1780 
1781   __ ld_ptr(Lmethod, Method::const_offset(), G3_scratch);
1782   __ add(G3_scratch, Otos_i, G3_scratch);
1783   __ add(G3_scratch, in_bytes(ConstMethod::codes_offset()), Lbcp);
1784   __ dispatch_next(vtos);
1785 }
1786 
1787 
1788 void TemplateTable::wide_ret() {
1789   transition(vtos, vtos);
1790   locals_index_wide(G3_scratch);
1791   __ access_local_returnAddress(G3_scratch, Otos_i);
1792   // Otos_i contains the bci, compute the bcp from that
1793 
1794   __ profile_ret(vtos, Otos_i, G4_scratch);
1795 
1796   __ ld_ptr(Lmethod, Method::const_offset(), G3_scratch);
1797   __ add(G3_scratch, Otos_i, G3_scratch);
1798   __ add(G3_scratch, in_bytes(ConstMethod::codes_offset()), Lbcp);
1799   __ dispatch_next(vtos);
1800 }
1801 
1802 
1803 void TemplateTable::tableswitch() {
1804   transition(itos, vtos);
1805   Label default_case, continue_execution;
1806 
1807   // align bcp
1808   __ add(Lbcp, BytesPerInt, O1);
1809   __ and3(O1, -BytesPerInt, O1);
1810   // load lo, hi
1811   __ ld(O1, 1 * BytesPerInt, O2);       // Low Byte
1812   __ ld(O1, 2 * BytesPerInt, O3);       // High Byte
1813 #ifdef _LP64
1814   // Sign extend the 32 bits
1815   __ sra ( Otos_i, 0, Otos_i );
1816 #endif /* _LP64 */
1817 
1818   // check against lo & hi
1819   __ cmp( Otos_i, O2);
1820   __ br( Assembler::less, false, Assembler::pn, default_case);
1821   __ delayed()->cmp( Otos_i, O3 );
1822   __ br( Assembler::greater, false, Assembler::pn, default_case);
1823   // lookup dispatch offset
1824   __ delayed()->sub(Otos_i, O2, O2);
1825   __ profile_switch_case(O2, O3, G3_scratch, G4_scratch);
1826   __ sll(O2, LogBytesPerInt, O2);
1827   __ add(O2, 3 * BytesPerInt, O2);
1828   __ ba(continue_execution);
1829   __ delayed()->ld(O1, O2, O2);
1830   // handle default
1831   __ bind(default_case);
1832   __ profile_switch_default(O3);
1833   __ ld(O1, 0, O2); // get default offset
1834   // continue execution
1835   __ bind(continue_execution);
1836   __ add(Lbcp, O2, Lbcp);
1837   __ dispatch_next(vtos);
1838 }
1839 
1840 
1841 void TemplateTable::lookupswitch() {
1842   transition(itos, itos);
1843   __ stop("lookupswitch bytecode should have been rewritten");
1844 }
1845 
1846 void TemplateTable::fast_linearswitch() {
1847   transition(itos, vtos);
1848     Label loop_entry, loop, found, continue_execution;
1849   // align bcp
1850   __ add(Lbcp, BytesPerInt, O1);
1851   __ and3(O1, -BytesPerInt, O1);
1852  // set counter
1853   __ ld(O1, BytesPerInt, O2);
1854   __ sll(O2, LogBytesPerInt + 1, O2); // in word-pairs
1855   __ add(O1, 2 * BytesPerInt, O3); // set first pair addr
1856   __ ba(loop_entry);
1857   __ delayed()->add(O3, O2, O2); // counter now points past last pair
1858 
1859   // table search
1860   __ bind(loop);
1861   __ cmp(O4, Otos_i);
1862   __ br(Assembler::equal, true, Assembler::pn, found);
1863   __ delayed()->ld(O3, BytesPerInt, O4); // offset -> O4
1864   __ inc(O3, 2 * BytesPerInt);
1865 
1866   __ bind(loop_entry);
1867   __ cmp(O2, O3);
1868   __ brx(Assembler::greaterUnsigned, true, Assembler::pt, loop);
1869   __ delayed()->ld(O3, 0, O4);
1870 
1871   // default case
1872   __ ld(O1, 0, O4); // get default offset
1873   if (ProfileInterpreter) {
1874     __ profile_switch_default(O3);
1875     __ ba_short(continue_execution);
1876   }
1877 
1878   // entry found -> get offset
1879   __ bind(found);
1880   if (ProfileInterpreter) {
1881     __ sub(O3, O1, O3);
1882     __ sub(O3, 2*BytesPerInt, O3);
1883     __ srl(O3, LogBytesPerInt + 1, O3); // in word-pairs
1884     __ profile_switch_case(O3, O1, O2, G3_scratch);
1885 
1886     __ bind(continue_execution);
1887   }
1888   __ add(Lbcp, O4, Lbcp);
1889   __ dispatch_next(vtos);
1890 }
1891 
1892 
1893 void TemplateTable::fast_binaryswitch() {
1894   transition(itos, vtos);
1895   // Implementation using the following core algorithm: (copied from Intel)
1896   //
1897   // int binary_search(int key, LookupswitchPair* array, int n) {
1898   //   // Binary search according to "Methodik des Programmierens" by
1899   //   // Edsger W. Dijkstra and W.H.J. Feijen, Addison Wesley Germany 1985.
1900   //   int i = 0;
1901   //   int j = n;
1902   //   while (i+1 < j) {
1903   //     // invariant P: 0 <= i < j <= n and (a[i] <= key < a[j] or Q)
1904   //     // with      Q: for all i: 0 <= i < n: key < a[i]
1905   //     // where a stands for the array and assuming that the (inexisting)
1906   //     // element a[n] is infinitely big.
1907   //     int h = (i + j) >> 1;
1908   //     // i < h < j
1909   //     if (key < array[h].fast_match()) {
1910   //       j = h;
1911   //     } else {
1912   //       i = h;
1913   //     }
1914   //   }
1915   //   // R: a[i] <= key < a[i+1] or Q
1916   //   // (i.e., if key is within array, i is the correct index)
1917   //   return i;
1918   // }
1919 
1920   // register allocation
1921   assert(Otos_i == O0, "alias checking");
1922   const Register Rkey     = Otos_i;                    // already set (tosca)
1923   const Register Rarray   = O1;
1924   const Register Ri       = O2;
1925   const Register Rj       = O3;
1926   const Register Rh       = O4;
1927   const Register Rscratch = O5;
1928 
1929   const int log_entry_size = 3;
1930   const int entry_size = 1 << log_entry_size;
1931 
1932   Label found;
1933   // Find Array start
1934   __ add(Lbcp, 3 * BytesPerInt, Rarray);
1935   __ and3(Rarray, -BytesPerInt, Rarray);
1936   // initialize i & j (in delay slot)
1937   __ clr( Ri );
1938 
1939   // and start
1940   Label entry;
1941   __ ba(entry);
1942   __ delayed()->ld( Rarray, -BytesPerInt, Rj);
1943   // (Rj is already in the native byte-ordering.)
1944 
1945   // binary search loop
1946   { Label loop;
1947     __ bind( loop );
1948     // int h = (i + j) >> 1;
1949     __ sra( Rh, 1, Rh );
1950     // if (key < array[h].fast_match()) {
1951     //   j = h;
1952     // } else {
1953     //   i = h;
1954     // }
1955     __ sll( Rh, log_entry_size, Rscratch );
1956     __ ld( Rarray, Rscratch, Rscratch );
1957     // (Rscratch is already in the native byte-ordering.)
1958     __ cmp( Rkey, Rscratch );
1959     __ movcc( Assembler::less,         false, Assembler::icc, Rh, Rj );  // j = h if (key <  array[h].fast_match())
1960     __ movcc( Assembler::greaterEqual, false, Assembler::icc, Rh, Ri );  // i = h if (key >= array[h].fast_match())
1961 
1962     // while (i+1 < j)
1963     __ bind( entry );
1964     __ add( Ri, 1, Rscratch );
1965     __ cmp(Rscratch, Rj);
1966     __ br( Assembler::less, true, Assembler::pt, loop );
1967     __ delayed()->add( Ri, Rj, Rh ); // start h = i + j  >> 1;
1968   }
1969 
1970   // end of binary search, result index is i (must check again!)
1971   Label default_case;
1972   Label continue_execution;
1973   if (ProfileInterpreter) {
1974     __ mov( Ri, Rh );              // Save index in i for profiling
1975   }
1976   __ sll( Ri, log_entry_size, Ri );
1977   __ ld( Rarray, Ri, Rscratch );
1978   // (Rscratch is already in the native byte-ordering.)
1979   __ cmp( Rkey, Rscratch );
1980   __ br( Assembler::notEqual, true, Assembler::pn, default_case );
1981   __ delayed()->ld( Rarray, -2 * BytesPerInt, Rj ); // load default offset -> j
1982 
1983   // entry found -> j = offset
1984   __ inc( Ri, BytesPerInt );
1985   __ profile_switch_case(Rh, Rj, Rscratch, Rkey);
1986   __ ld( Rarray, Ri, Rj );
1987   // (Rj is already in the native byte-ordering.)
1988 
1989   if (ProfileInterpreter) {
1990     __ ba_short(continue_execution);
1991   }
1992 
1993   __ bind(default_case); // fall through (if not profiling)
1994   __ profile_switch_default(Ri);
1995 
1996   __ bind(continue_execution);
1997   __ add( Lbcp, Rj, Lbcp );
1998   __ dispatch_next( vtos );
1999 }
2000 
2001 
2002 void TemplateTable::_return(TosState state) {
2003   transition(state, state);
2004   assert(_desc->calls_vm(), "inconsistent calls_vm information");
2005 
2006   if (_desc->bytecode() == Bytecodes::_return_register_finalizer) {
2007     assert(state == vtos, "only valid state");
2008     __ mov(G0, G3_scratch);
2009     __ access_local_ptr(G3_scratch, Otos_i);
2010     __ load_klass(Otos_i, O2);
2011     __ set(JVM_ACC_HAS_FINALIZER, G3);
2012     __ ld(O2, in_bytes(Klass::access_flags_offset()), O2);
2013     __ andcc(G3, O2, G0);
2014     Label skip_register_finalizer;
2015     __ br(Assembler::zero, false, Assembler::pn, skip_register_finalizer);
2016     __ delayed()->nop();
2017 
2018     // Call out to do finalizer registration
2019     __ call_VM(noreg, CAST_FROM_FN_PTR(address, InterpreterRuntime::register_finalizer), Otos_i);
2020 
2021     __ bind(skip_register_finalizer);
2022   }
2023 
2024   // Narrow result if state is itos but result type is smaller.
2025   // Need to narrow in the return bytecode rather than in generate_return_entry
2026   // since compiled code callers expect the result to already be narrowed.
2027   if (state == itos) {
2028     __ narrow(Otos_i);
2029   }
2030   __ remove_activation(state, /* throw_monitor_exception */ true);
2031 
2032   // The caller's SP was adjusted upon method entry to accomodate
2033   // the callee's non-argument locals. Undo that adjustment.
2034   __ ret();                             // return to caller
2035   __ delayed()->restore(I5_savedSP, G0, SP);
2036 }
2037 
2038 
2039 // ----------------------------------------------------------------------------
2040 // Volatile variables demand their effects be made known to all CPU's in
2041 // order.  Store buffers on most chips allow reads & writes to reorder; the
2042 // JMM's ReadAfterWrite.java test fails in -Xint mode without some kind of
2043 // memory barrier (i.e., it's not sufficient that the interpreter does not
2044 // reorder volatile references, the hardware also must not reorder them).
2045 //
2046 // According to the new Java Memory Model (JMM):
2047 // (1) All volatiles are serialized wrt to each other.
2048 // ALSO reads & writes act as aquire & release, so:
2049 // (2) A read cannot let unrelated NON-volatile memory refs that happen after
2050 // the read float up to before the read.  It's OK for non-volatile memory refs
2051 // that happen before the volatile read to float down below it.
2052 // (3) Similar a volatile write cannot let unrelated NON-volatile memory refs
2053 // that happen BEFORE the write float down to after the write.  It's OK for
2054 // non-volatile memory refs that happen after the volatile write to float up
2055 // before it.
2056 //
2057 // We only put in barriers around volatile refs (they are expensive), not
2058 // _between_ memory refs (that would require us to track the flavor of the
2059 // previous memory refs).  Requirements (2) and (3) require some barriers
2060 // before volatile stores and after volatile loads.  These nearly cover
2061 // requirement (1) but miss the volatile-store-volatile-load case.  This final
2062 // case is placed after volatile-stores although it could just as well go
2063 // before volatile-loads.
2064 void TemplateTable::volatile_barrier(Assembler::Membar_mask_bits order_constraint) {
2065   // Helper function to insert a is-volatile test and memory barrier
2066   // All current sparc implementations run in TSO, needing only StoreLoad
2067   if ((order_constraint & Assembler::StoreLoad) == 0) return;
2068   __ membar( order_constraint );
2069 }
2070 
2071 // ----------------------------------------------------------------------------
2072 void TemplateTable::resolve_cache_and_index(int byte_no,
2073                                             Register Rcache,
2074                                             Register index,
2075                                             size_t index_size) {
2076   // Depends on cpCacheOop layout!
2077 
2078   Label resolved;
2079   Bytecodes::Code code = bytecode();
2080   switch (code) {
2081   case Bytecodes::_nofast_getfield: code = Bytecodes::_getfield; break;
2082   case Bytecodes::_nofast_putfield: code = Bytecodes::_putfield; break;
2083   }
2084 
2085   assert(byte_no == f1_byte || byte_no == f2_byte, "byte_no out of range");
2086   __ get_cache_and_index_and_bytecode_at_bcp(Rcache, index, Lbyte_code, byte_no, 1, index_size);
2087   __ cmp(Lbyte_code, code);  // have we resolved this bytecode?
2088   __ br(Assembler::equal, false, Assembler::pt, resolved);
2089   __ delayed()->set(code, O1);
2090 
2091   address entry = CAST_FROM_FN_PTR(address, InterpreterRuntime::resolve_from_cache);
2092   // first time invocation - must resolve first
2093   __ call_VM(noreg, entry, O1);
2094   // Update registers with resolved info
2095   __ get_cache_and_index_at_bcp(Rcache, index, 1, index_size);
2096   __ bind(resolved);
2097 }
2098 
2099 void TemplateTable::load_invoke_cp_cache_entry(int byte_no,
2100                                                Register method,
2101                                                Register itable_index,
2102                                                Register flags,
2103                                                bool is_invokevirtual,
2104                                                bool is_invokevfinal,
2105                                                bool is_invokedynamic) {
2106   // Uses both G3_scratch and G4_scratch
2107   Register cache = G3_scratch;
2108   Register index = G4_scratch;
2109   assert_different_registers(cache, method, itable_index);
2110 
2111   // determine constant pool cache field offsets
2112   assert(is_invokevirtual == (byte_no == f2_byte), "is_invokevirtual flag redundant");
2113   const int method_offset = in_bytes(
2114       ConstantPoolCache::base_offset() +
2115       ((byte_no == f2_byte)
2116        ? ConstantPoolCacheEntry::f2_offset()
2117        : ConstantPoolCacheEntry::f1_offset()
2118       )
2119     );
2120   const int flags_offset = in_bytes(ConstantPoolCache::base_offset() +
2121                                     ConstantPoolCacheEntry::flags_offset());
2122   // access constant pool cache fields
2123   const int index_offset = in_bytes(ConstantPoolCache::base_offset() +
2124                                     ConstantPoolCacheEntry::f2_offset());
2125 
2126   if (is_invokevfinal) {
2127     __ get_cache_and_index_at_bcp(cache, index, 1);
2128     __ ld_ptr(Address(cache, method_offset), method);
2129   } else {
2130     size_t index_size = (is_invokedynamic ? sizeof(u4) : sizeof(u2));
2131     resolve_cache_and_index(byte_no, cache, index, index_size);
2132     __ ld_ptr(Address(cache, method_offset), method);
2133   }
2134 
2135   if (itable_index != noreg) {
2136     // pick up itable or appendix index from f2 also:
2137     __ ld_ptr(Address(cache, index_offset), itable_index);
2138   }
2139   __ ld_ptr(Address(cache, flags_offset), flags);
2140 }
2141 
2142 // The Rcache register must be set before call
2143 void TemplateTable::load_field_cp_cache_entry(Register Robj,
2144                                               Register Rcache,
2145                                               Register index,
2146                                               Register Roffset,
2147                                               Register Rflags,
2148                                               bool is_static) {
2149   assert_different_registers(Rcache, Rflags, Roffset);
2150 
2151   ByteSize cp_base_offset = ConstantPoolCache::base_offset();
2152 
2153   __ ld_ptr(Rcache, cp_base_offset + ConstantPoolCacheEntry::flags_offset(), Rflags);
2154   __ ld_ptr(Rcache, cp_base_offset + ConstantPoolCacheEntry::f2_offset(), Roffset);
2155   if (is_static) {
2156     __ ld_ptr(Rcache, cp_base_offset + ConstantPoolCacheEntry::f1_offset(), Robj);
2157     const int mirror_offset = in_bytes(Klass::java_mirror_offset());
2158     __ ld_ptr( Robj, mirror_offset, Robj);
2159   }
2160 }
2161 
2162 // The registers Rcache and index expected to be set before call.
2163 // Correct values of the Rcache and index registers are preserved.
2164 void TemplateTable::jvmti_post_field_access(Register Rcache,
2165                                             Register index,
2166                                             bool is_static,
2167                                             bool has_tos) {
2168   ByteSize cp_base_offset = ConstantPoolCache::base_offset();
2169 
2170   if (JvmtiExport::can_post_field_access()) {
2171     // Check to see if a field access watch has been set before we take
2172     // the time to call into the VM.
2173     Label Label1;
2174     assert_different_registers(Rcache, index, G1_scratch);
2175     AddressLiteral get_field_access_count_addr(JvmtiExport::get_field_access_count_addr());
2176     __ load_contents(get_field_access_count_addr, G1_scratch);
2177     __ cmp_and_br_short(G1_scratch, 0, Assembler::equal, Assembler::pt, Label1);
2178 
2179     __ add(Rcache, in_bytes(cp_base_offset), Rcache);
2180 
2181     if (is_static) {
2182       __ clr(Otos_i);
2183     } else {
2184       if (has_tos) {
2185       // save object pointer before call_VM() clobbers it
2186         __ push_ptr(Otos_i);  // put object on tos where GC wants it.
2187       } else {
2188         // Load top of stack (do not pop the value off the stack);
2189         __ ld_ptr(Lesp, Interpreter::expr_offset_in_bytes(0), Otos_i);
2190       }
2191       __ verify_oop(Otos_i);
2192     }
2193     // Otos_i: object pointer or NULL if static
2194     // Rcache: cache entry pointer
2195     __ call_VM(noreg, CAST_FROM_FN_PTR(address, InterpreterRuntime::post_field_access),
2196                Otos_i, Rcache);
2197     if (!is_static && has_tos) {
2198       __ pop_ptr(Otos_i);  // restore object pointer
2199       __ verify_oop(Otos_i);
2200     }
2201     __ get_cache_and_index_at_bcp(Rcache, index, 1);
2202     __ bind(Label1);
2203   }
2204 }
2205 
2206 void TemplateTable::getfield_or_static(int byte_no, bool is_static, RewriteControl rc) {
2207   transition(vtos, vtos);
2208 
2209   Register Rcache = G3_scratch;
2210   Register index  = G4_scratch;
2211   Register Rclass = Rcache;
2212   Register Roffset= G4_scratch;
2213   Register Rflags = G1_scratch;
2214   ByteSize cp_base_offset = ConstantPoolCache::base_offset();
2215 
2216   resolve_cache_and_index(byte_no, Rcache, index, sizeof(u2));
2217   jvmti_post_field_access(Rcache, index, is_static, false);
2218   load_field_cp_cache_entry(Rclass, Rcache, index, Roffset, Rflags, is_static);
2219 
2220   if (!is_static) {
2221     pop_and_check_object(Rclass);
2222   } else {
2223     __ verify_oop(Rclass);
2224   }
2225 
2226   Label exit;
2227 
2228   Assembler::Membar_mask_bits membar_bits =
2229     Assembler::Membar_mask_bits(Assembler::LoadLoad | Assembler::LoadStore);
2230 
2231   if (__ membar_has_effect(membar_bits)) {
2232     // Get volatile flag
2233     __ set((1 << ConstantPoolCacheEntry::is_volatile_shift), Lscratch);
2234     __ and3(Rflags, Lscratch, Lscratch);
2235   }
2236 
2237   Label checkVolatile;
2238 
2239   // compute field type
2240   Label notByte, notBool, notInt, notShort, notChar, notLong, notFloat, notObj;
2241   __ srl(Rflags, ConstantPoolCacheEntry::tos_state_shift, Rflags);
2242   // Make sure we don't need to mask Rflags after the above shift
2243   ConstantPoolCacheEntry::verify_tos_state_shift();
2244 
2245   // Check atos before itos for getstatic, more likely (in Queens at least)
2246   __ cmp(Rflags, atos);
2247   __ br(Assembler::notEqual, false, Assembler::pt, notObj);
2248   __ delayed() ->cmp(Rflags, itos);
2249 
2250   // atos
2251   __ load_heap_oop(Rclass, Roffset, Otos_i);
2252   __ verify_oop(Otos_i);
2253   __ push(atos);
2254   if (!is_static && rc == may_rewrite) {
2255     patch_bytecode(Bytecodes::_fast_agetfield, G3_scratch, G4_scratch);
2256   }
2257   __ ba(checkVolatile);
2258   __ delayed()->tst(Lscratch);
2259 
2260   __ bind(notObj);
2261 
2262   // cmp(Rflags, itos);
2263   __ br(Assembler::notEqual, false, Assembler::pt, notInt);
2264   __ delayed() ->cmp(Rflags, ltos);
2265 
2266   // itos
2267   __ ld(Rclass, Roffset, Otos_i);
2268   __ push(itos);
2269   if (!is_static && rc == may_rewrite) {
2270     patch_bytecode(Bytecodes::_fast_igetfield, G3_scratch, G4_scratch);
2271   }
2272   __ ba(checkVolatile);
2273   __ delayed()->tst(Lscratch);
2274 
2275   __ bind(notInt);
2276 
2277   // cmp(Rflags, ltos);
2278   __ br(Assembler::notEqual, false, Assembler::pt, notLong);
2279   __ delayed() ->cmp(Rflags, btos);
2280 
2281   // ltos
2282   // load must be atomic
2283   __ ld_long(Rclass, Roffset, Otos_l);
2284   __ push(ltos);
2285   if (!is_static && rc == may_rewrite) {
2286     patch_bytecode(Bytecodes::_fast_lgetfield, G3_scratch, G4_scratch);
2287   }
2288   __ ba(checkVolatile);
2289   __ delayed()->tst(Lscratch);
2290 
2291   __ bind(notLong);
2292 
2293   // cmp(Rflags, btos);
2294   __ br(Assembler::notEqual, false, Assembler::pt, notByte);
2295   __ delayed() ->cmp(Rflags, ztos);
2296 
2297   // btos
2298   __ ldsb(Rclass, Roffset, Otos_i);
2299   __ push(itos);
2300   if (!is_static && rc == may_rewrite) {
2301     patch_bytecode(Bytecodes::_fast_bgetfield, G3_scratch, G4_scratch);
2302   }
2303   __ ba(checkVolatile);
2304   __ delayed()->tst(Lscratch);
2305 
2306   __ bind(notByte);
2307 
2308   // cmp(Rflags, ztos);
2309   __ br(Assembler::notEqual, false, Assembler::pt, notBool);
2310   __ delayed() ->cmp(Rflags, ctos);
2311 
2312   // ztos
2313   __ ldsb(Rclass, Roffset, Otos_i);
2314   __ push(itos);
2315   if (!is_static && rc == may_rewrite) {
2316     // use btos rewriting, no truncating to t/f bit is needed for getfield.
2317     patch_bytecode(Bytecodes::_fast_bgetfield, G3_scratch, G4_scratch);
2318   }
2319   __ ba(checkVolatile);
2320   __ delayed()->tst(Lscratch);
2321 
2322   __ bind(notBool);
2323 
2324   // cmp(Rflags, ctos);
2325   __ br(Assembler::notEqual, false, Assembler::pt, notChar);
2326   __ delayed() ->cmp(Rflags, stos);
2327 
2328   // ctos
2329   __ lduh(Rclass, Roffset, Otos_i);
2330   __ push(itos);
2331   if (!is_static && rc == may_rewrite) {
2332     patch_bytecode(Bytecodes::_fast_cgetfield, G3_scratch, G4_scratch);
2333   }
2334   __ ba(checkVolatile);
2335   __ delayed()->tst(Lscratch);
2336 
2337   __ bind(notChar);
2338 
2339   // cmp(Rflags, stos);
2340   __ br(Assembler::notEqual, false, Assembler::pt, notShort);
2341   __ delayed() ->cmp(Rflags, ftos);
2342 
2343   // stos
2344   __ ldsh(Rclass, Roffset, Otos_i);
2345   __ push(itos);
2346   if (!is_static && rc == may_rewrite) {
2347     patch_bytecode(Bytecodes::_fast_sgetfield, G3_scratch, G4_scratch);
2348   }
2349   __ ba(checkVolatile);
2350   __ delayed()->tst(Lscratch);
2351 
2352   __ bind(notShort);
2353 
2354 
2355   // cmp(Rflags, ftos);
2356   __ br(Assembler::notEqual, false, Assembler::pt, notFloat);
2357   __ delayed() ->tst(Lscratch);
2358 
2359   // ftos
2360   __ ldf(FloatRegisterImpl::S, Rclass, Roffset, Ftos_f);
2361   __ push(ftos);
2362   if (!is_static && rc == may_rewrite) {
2363     patch_bytecode(Bytecodes::_fast_fgetfield, G3_scratch, G4_scratch);
2364   }
2365   __ ba(checkVolatile);
2366   __ delayed()->tst(Lscratch);
2367 
2368   __ bind(notFloat);
2369 
2370 
2371   // dtos
2372   __ ldf(FloatRegisterImpl::D, Rclass, Roffset, Ftos_d);
2373   __ push(dtos);
2374   if (!is_static && rc == may_rewrite) {
2375     patch_bytecode(Bytecodes::_fast_dgetfield, G3_scratch, G4_scratch);
2376   }
2377 
2378   __ bind(checkVolatile);
2379   if (__ membar_has_effect(membar_bits)) {
2380     // __ tst(Lscratch); executed in delay slot
2381     __ br(Assembler::zero, false, Assembler::pt, exit);
2382     __ delayed()->nop();
2383     volatile_barrier(membar_bits);
2384   }
2385 
2386   __ bind(exit);
2387 }
2388 
2389 void TemplateTable::getfield(int byte_no) {
2390   getfield_or_static(byte_no, false);
2391 }
2392 
2393 void TemplateTable::nofast_getfield(int byte_no) {
2394   getfield_or_static(byte_no, false, may_not_rewrite);
2395 }
2396 
2397 void TemplateTable::getstatic(int byte_no) {
2398   getfield_or_static(byte_no, true);
2399 }
2400 
2401 void TemplateTable::fast_accessfield(TosState state) {
2402   transition(atos, state);
2403   Register Rcache  = G3_scratch;
2404   Register index   = G4_scratch;
2405   Register Roffset = G4_scratch;
2406   Register Rflags  = Rcache;
2407   ByteSize cp_base_offset = ConstantPoolCache::base_offset();
2408 
2409   __ get_cache_and_index_at_bcp(Rcache, index, 1);
2410   jvmti_post_field_access(Rcache, index, /*is_static*/false, /*has_tos*/true);
2411 
2412   __ ld_ptr(Rcache, cp_base_offset + ConstantPoolCacheEntry::f2_offset(), Roffset);
2413 
2414   __ null_check(Otos_i);
2415   __ verify_oop(Otos_i);
2416 
2417   Label exit;
2418 
2419   Assembler::Membar_mask_bits membar_bits =
2420     Assembler::Membar_mask_bits(Assembler::LoadLoad | Assembler::LoadStore);
2421   if (__ membar_has_effect(membar_bits)) {
2422     // Get volatile flag
2423     __ ld_ptr(Rcache, cp_base_offset + ConstantPoolCacheEntry::f2_offset(), Rflags);
2424     __ set((1 << ConstantPoolCacheEntry::is_volatile_shift), Lscratch);
2425   }
2426 
2427   switch (bytecode()) {
2428     case Bytecodes::_fast_bgetfield:
2429       __ ldsb(Otos_i, Roffset, Otos_i);
2430       break;
2431     case Bytecodes::_fast_cgetfield:
2432       __ lduh(Otos_i, Roffset, Otos_i);
2433       break;
2434     case Bytecodes::_fast_sgetfield:
2435       __ ldsh(Otos_i, Roffset, Otos_i);
2436       break;
2437     case Bytecodes::_fast_igetfield:
2438       __ ld(Otos_i, Roffset, Otos_i);
2439       break;
2440     case Bytecodes::_fast_lgetfield:
2441       __ ld_long(Otos_i, Roffset, Otos_l);
2442       break;
2443     case Bytecodes::_fast_fgetfield:
2444       __ ldf(FloatRegisterImpl::S, Otos_i, Roffset, Ftos_f);
2445       break;
2446     case Bytecodes::_fast_dgetfield:
2447       __ ldf(FloatRegisterImpl::D, Otos_i, Roffset, Ftos_d);
2448       break;
2449     case Bytecodes::_fast_agetfield:
2450       __ load_heap_oop(Otos_i, Roffset, Otos_i);
2451       break;
2452     default:
2453       ShouldNotReachHere();
2454   }
2455 
2456   if (__ membar_has_effect(membar_bits)) {
2457     __ btst(Lscratch, Rflags);
2458     __ br(Assembler::zero, false, Assembler::pt, exit);
2459     __ delayed()->nop();
2460     volatile_barrier(membar_bits);
2461     __ bind(exit);
2462   }
2463 
2464   if (state == atos) {
2465     __ verify_oop(Otos_i);    // does not blow flags!
2466   }
2467 }
2468 
2469 void TemplateTable::jvmti_post_fast_field_mod() {
2470   if (JvmtiExport::can_post_field_modification()) {
2471     // Check to see if a field modification watch has been set before we take
2472     // the time to call into the VM.
2473     Label done;
2474     AddressLiteral get_field_modification_count_addr(JvmtiExport::get_field_modification_count_addr());
2475     __ load_contents(get_field_modification_count_addr, G4_scratch);
2476     __ cmp_and_br_short(G4_scratch, 0, Assembler::equal, Assembler::pt, done);
2477     __ pop_ptr(G4_scratch);     // copy the object pointer from tos
2478     __ verify_oop(G4_scratch);
2479     __ push_ptr(G4_scratch);    // put the object pointer back on tos
2480     __ get_cache_entry_pointer_at_bcp(G1_scratch, G3_scratch, 1);
2481     // Save tos values before call_VM() clobbers them. Since we have
2482     // to do it for every data type, we use the saved values as the
2483     // jvalue object.
2484     switch (bytecode()) {  // save tos values before call_VM() clobbers them
2485     case Bytecodes::_fast_aputfield: __ push_ptr(Otos_i); break;
2486     case Bytecodes::_fast_bputfield: // fall through
2487     case Bytecodes::_fast_zputfield: // fall through
2488     case Bytecodes::_fast_sputfield: // fall through
2489     case Bytecodes::_fast_cputfield: // fall through
2490     case Bytecodes::_fast_iputfield: __ push_i(Otos_i); break;
2491     case Bytecodes::_fast_dputfield: __ push_d(Ftos_d); break;
2492     case Bytecodes::_fast_fputfield: __ push_f(Ftos_f); break;
2493     // get words in right order for use as jvalue object
2494     case Bytecodes::_fast_lputfield: __ push_l(Otos_l); break;
2495     }
2496     // setup pointer to jvalue object
2497     __ mov(Lesp, G3_scratch);  __ inc(G3_scratch, wordSize);
2498     // G4_scratch:  object pointer
2499     // G1_scratch: cache entry pointer
2500     // G3_scratch: jvalue object on the stack
2501     __ call_VM(noreg, CAST_FROM_FN_PTR(address, InterpreterRuntime::post_field_modification), G4_scratch, G1_scratch, G3_scratch);
2502     switch (bytecode()) {             // restore tos values
2503     case Bytecodes::_fast_aputfield: __ pop_ptr(Otos_i); break;
2504     case Bytecodes::_fast_bputfield: // fall through
2505     case Bytecodes::_fast_zputfield: // fall through
2506     case Bytecodes::_fast_sputfield: // fall through
2507     case Bytecodes::_fast_cputfield: // fall through
2508     case Bytecodes::_fast_iputfield: __ pop_i(Otos_i); break;
2509     case Bytecodes::_fast_dputfield: __ pop_d(Ftos_d); break;
2510     case Bytecodes::_fast_fputfield: __ pop_f(Ftos_f); break;
2511     case Bytecodes::_fast_lputfield: __ pop_l(Otos_l); break;
2512     }
2513     __ bind(done);
2514   }
2515 }
2516 
2517 // The registers Rcache and index expected to be set before call.
2518 // The function may destroy various registers, just not the Rcache and index registers.
2519 void TemplateTable::jvmti_post_field_mod(Register Rcache, Register index, bool is_static) {
2520   ByteSize cp_base_offset = ConstantPoolCache::base_offset();
2521 
2522   if (JvmtiExport::can_post_field_modification()) {
2523     // Check to see if a field modification watch has been set before we take
2524     // the time to call into the VM.
2525     Label Label1;
2526     assert_different_registers(Rcache, index, G1_scratch);
2527     AddressLiteral get_field_modification_count_addr(JvmtiExport::get_field_modification_count_addr());
2528     __ load_contents(get_field_modification_count_addr, G1_scratch);
2529     __ cmp_and_br_short(G1_scratch, 0, Assembler::zero, Assembler::pt, Label1);
2530 
2531     // The Rcache and index registers have been already set.
2532     // This allows to eliminate this call but the Rcache and index
2533     // registers must be correspondingly used after this line.
2534     __ get_cache_and_index_at_bcp(G1_scratch, G4_scratch, 1);
2535 
2536     __ add(G1_scratch, in_bytes(cp_base_offset), G3_scratch);
2537     if (is_static) {
2538       // Life is simple.  Null out the object pointer.
2539       __ clr(G4_scratch);
2540     } else {
2541       Register Rflags = G1_scratch;
2542       // Life is harder. The stack holds the value on top, followed by the
2543       // object.  We don't know the size of the value, though; it could be
2544       // one or two words depending on its type. As a result, we must find
2545       // the type to determine where the object is.
2546 
2547       Label two_word, valsizeknown;
2548       __ ld_ptr(G1_scratch, cp_base_offset + ConstantPoolCacheEntry::flags_offset(), Rflags);
2549       __ mov(Lesp, G4_scratch);
2550       __ srl(Rflags, ConstantPoolCacheEntry::tos_state_shift, Rflags);
2551       // Make sure we don't need to mask Rflags after the above shift
2552       ConstantPoolCacheEntry::verify_tos_state_shift();
2553       __ cmp(Rflags, ltos);
2554       __ br(Assembler::equal, false, Assembler::pt, two_word);
2555       __ delayed()->cmp(Rflags, dtos);
2556       __ br(Assembler::equal, false, Assembler::pt, two_word);
2557       __ delayed()->nop();
2558       __ inc(G4_scratch, Interpreter::expr_offset_in_bytes(1));
2559       __ ba_short(valsizeknown);
2560       __ bind(two_word);
2561 
2562       __ inc(G4_scratch, Interpreter::expr_offset_in_bytes(2));
2563 
2564       __ bind(valsizeknown);
2565       // setup object pointer
2566       __ ld_ptr(G4_scratch, 0, G4_scratch);
2567       __ verify_oop(G4_scratch);
2568     }
2569     // setup pointer to jvalue object
2570     __ mov(Lesp, G1_scratch);  __ inc(G1_scratch, wordSize);
2571     // G4_scratch:  object pointer or NULL if static
2572     // G3_scratch: cache entry pointer
2573     // G1_scratch: jvalue object on the stack
2574     __ call_VM(noreg, CAST_FROM_FN_PTR(address, InterpreterRuntime::post_field_modification),
2575                G4_scratch, G3_scratch, G1_scratch);
2576     __ get_cache_and_index_at_bcp(Rcache, index, 1);
2577     __ bind(Label1);
2578   }
2579 }
2580 
2581 void TemplateTable::pop_and_check_object(Register r) {
2582   __ pop_ptr(r);
2583   __ null_check(r);  // for field access must check obj.
2584   __ verify_oop(r);
2585 }
2586 
2587 void TemplateTable::putfield_or_static(int byte_no, bool is_static, RewriteControl rc) {
2588   transition(vtos, vtos);
2589   Register Rcache = G3_scratch;
2590   Register index  = G4_scratch;
2591   Register Rclass = Rcache;
2592   Register Roffset= G4_scratch;
2593   Register Rflags = G1_scratch;
2594   ByteSize cp_base_offset = ConstantPoolCache::base_offset();
2595 
2596   resolve_cache_and_index(byte_no, Rcache, index, sizeof(u2));
2597   jvmti_post_field_mod(Rcache, index, is_static);
2598   load_field_cp_cache_entry(Rclass, Rcache, index, Roffset, Rflags, is_static);
2599 
2600   Assembler::Membar_mask_bits read_bits =
2601     Assembler::Membar_mask_bits(Assembler::LoadStore | Assembler::StoreStore);
2602   Assembler::Membar_mask_bits write_bits = Assembler::StoreLoad;
2603 
2604   Label notVolatile, checkVolatile, exit;
2605   if (__ membar_has_effect(read_bits) || __ membar_has_effect(write_bits)) {
2606     __ set((1 << ConstantPoolCacheEntry::is_volatile_shift), Lscratch);
2607     __ and3(Rflags, Lscratch, Lscratch);
2608 
2609     if (__ membar_has_effect(read_bits)) {
2610       __ cmp_and_br_short(Lscratch, 0, Assembler::equal, Assembler::pt, notVolatile);
2611       volatile_barrier(read_bits);
2612       __ bind(notVolatile);
2613     }
2614   }
2615 
2616   __ srl(Rflags, ConstantPoolCacheEntry::tos_state_shift, Rflags);
2617   // Make sure we don't need to mask Rflags after the above shift
2618   ConstantPoolCacheEntry::verify_tos_state_shift();
2619 
2620   // compute field type
2621   Label notInt, notShort, notChar, notObj, notByte, notBool, notLong, notFloat;
2622 
2623   if (is_static) {
2624     // putstatic with object type most likely, check that first
2625     __ cmp(Rflags, atos);
2626     __ br(Assembler::notEqual, false, Assembler::pt, notObj);
2627     __ delayed()->cmp(Rflags, itos);
2628 
2629     // atos
2630     {
2631       __ pop_ptr();
2632       __ verify_oop(Otos_i);
2633       do_oop_store(_masm, Rclass, Roffset, 0, Otos_i, G1_scratch, _bs->kind(), false);
2634       __ ba(checkVolatile);
2635       __ delayed()->tst(Lscratch);
2636     }
2637 
2638     __ bind(notObj);
2639     // cmp(Rflags, itos);
2640     __ br(Assembler::notEqual, false, Assembler::pt, notInt);
2641     __ delayed()->cmp(Rflags, btos);
2642 
2643     // itos
2644     {
2645       __ pop_i();
2646       __ st(Otos_i, Rclass, Roffset);
2647       __ ba(checkVolatile);
2648       __ delayed()->tst(Lscratch);
2649     }
2650 
2651     __ bind(notInt);
2652   } else {
2653     // putfield with int type most likely, check that first
2654     __ cmp(Rflags, itos);
2655     __ br(Assembler::notEqual, false, Assembler::pt, notInt);
2656     __ delayed()->cmp(Rflags, atos);
2657 
2658     // itos
2659     {
2660       __ pop_i();
2661       pop_and_check_object(Rclass);
2662       __ st(Otos_i, Rclass, Roffset);
2663       if (rc == may_rewrite) patch_bytecode(Bytecodes::_fast_iputfield, G3_scratch, G4_scratch, true, byte_no);
2664       __ ba(checkVolatile);
2665       __ delayed()->tst(Lscratch);
2666     }
2667 
2668     __ bind(notInt);
2669     // cmp(Rflags, atos);
2670     __ br(Assembler::notEqual, false, Assembler::pt, notObj);
2671     __ delayed()->cmp(Rflags, btos);
2672 
2673     // atos
2674     {
2675       __ pop_ptr();
2676       pop_and_check_object(Rclass);
2677       __ verify_oop(Otos_i);
2678       do_oop_store(_masm, Rclass, Roffset, 0, Otos_i, G1_scratch, _bs->kind(), false);
2679       if (rc == may_rewrite) patch_bytecode(Bytecodes::_fast_aputfield, G3_scratch, G4_scratch, true, byte_no);
2680       __ ba(checkVolatile);
2681       __ delayed()->tst(Lscratch);
2682     }
2683 
2684     __ bind(notObj);
2685   }
2686 
2687   // cmp(Rflags, btos);
2688   __ br(Assembler::notEqual, false, Assembler::pt, notByte);
2689   __ delayed()->cmp(Rflags, ztos);
2690 
2691   // btos
2692   {
2693     __ pop_i();
2694     if (!is_static) pop_and_check_object(Rclass);
2695     __ stb(Otos_i, Rclass, Roffset);
2696     if (!is_static && rc == may_rewrite) {
2697       patch_bytecode(Bytecodes::_fast_bputfield, G3_scratch, G4_scratch, true, byte_no);
2698     }
2699     __ ba(checkVolatile);
2700     __ delayed()->tst(Lscratch);
2701   }
2702 
2703   __ bind(notByte);
2704 
2705   // cmp(Rflags, btos);
2706   __ br(Assembler::notEqual, false, Assembler::pt, notBool);
2707   __ delayed()->cmp(Rflags, ltos);
2708 
2709   // ztos
2710   {
2711     __ pop_i();
2712     if (!is_static) pop_and_check_object(Rclass);
2713     __ and3(Otos_i, 1, Otos_i);
2714     __ stb(Otos_i, Rclass, Roffset);
2715     if (!is_static && rc == may_rewrite) {
2716       patch_bytecode(Bytecodes::_fast_zputfield, G3_scratch, G4_scratch, true, byte_no);
2717     }
2718     __ ba(checkVolatile);
2719     __ delayed()->tst(Lscratch);
2720   }
2721 
2722   __ bind(notBool);
2723   // cmp(Rflags, ltos);
2724   __ br(Assembler::notEqual, false, Assembler::pt, notLong);
2725   __ delayed()->cmp(Rflags, ctos);
2726 
2727   // ltos
2728   {
2729     __ pop_l();
2730     if (!is_static) pop_and_check_object(Rclass);
2731     __ st_long(Otos_l, Rclass, Roffset);
2732     if (!is_static && rc == may_rewrite) {
2733       patch_bytecode(Bytecodes::_fast_lputfield, G3_scratch, G4_scratch, true, byte_no);
2734     }
2735     __ ba(checkVolatile);
2736     __ delayed()->tst(Lscratch);
2737   }
2738 
2739   __ bind(notLong);
2740   // cmp(Rflags, ctos);
2741   __ br(Assembler::notEqual, false, Assembler::pt, notChar);
2742   __ delayed()->cmp(Rflags, stos);
2743 
2744   // ctos (char)
2745   {
2746     __ pop_i();
2747     if (!is_static) pop_and_check_object(Rclass);
2748     __ sth(Otos_i, Rclass, Roffset);
2749     if (!is_static && rc == may_rewrite) {
2750       patch_bytecode(Bytecodes::_fast_cputfield, G3_scratch, G4_scratch, true, byte_no);
2751     }
2752     __ ba(checkVolatile);
2753     __ delayed()->tst(Lscratch);
2754   }
2755 
2756   __ bind(notChar);
2757   // cmp(Rflags, stos);
2758   __ br(Assembler::notEqual, false, Assembler::pt, notShort);
2759   __ delayed()->cmp(Rflags, ftos);
2760 
2761   // stos (short)
2762   {
2763     __ pop_i();
2764     if (!is_static) pop_and_check_object(Rclass);
2765     __ sth(Otos_i, Rclass, Roffset);
2766     if (!is_static && rc == may_rewrite) {
2767       patch_bytecode(Bytecodes::_fast_sputfield, G3_scratch, G4_scratch, true, byte_no);
2768     }
2769     __ ba(checkVolatile);
2770     __ delayed()->tst(Lscratch);
2771   }
2772 
2773   __ bind(notShort);
2774   // cmp(Rflags, ftos);
2775   __ br(Assembler::notZero, false, Assembler::pt, notFloat);
2776   __ delayed()->nop();
2777 
2778   // ftos
2779   {
2780     __ pop_f();
2781     if (!is_static) pop_and_check_object(Rclass);
2782     __ stf(FloatRegisterImpl::S, Ftos_f, Rclass, Roffset);
2783     if (!is_static && rc == may_rewrite) {
2784       patch_bytecode(Bytecodes::_fast_fputfield, G3_scratch, G4_scratch, true, byte_no);
2785     }
2786     __ ba(checkVolatile);
2787     __ delayed()->tst(Lscratch);
2788   }
2789 
2790   __ bind(notFloat);
2791 
2792   // dtos
2793   {
2794     __ pop_d();
2795     if (!is_static) pop_and_check_object(Rclass);
2796     __ stf(FloatRegisterImpl::D, Ftos_d, Rclass, Roffset);
2797     if (!is_static && rc == may_rewrite) {
2798       patch_bytecode(Bytecodes::_fast_dputfield, G3_scratch, G4_scratch, true, byte_no);
2799     }
2800   }
2801 
2802   __ bind(checkVolatile);
2803   __ tst(Lscratch);
2804 
2805   if (__ membar_has_effect(write_bits)) {
2806     // __ tst(Lscratch); in delay slot
2807     __ br(Assembler::zero, false, Assembler::pt, exit);
2808     __ delayed()->nop();
2809     volatile_barrier(Assembler::StoreLoad);
2810     __ bind(exit);
2811   }
2812 }
2813 
2814 void TemplateTable::fast_storefield(TosState state) {
2815   transition(state, vtos);
2816   Register Rcache = G3_scratch;
2817   Register Rclass = Rcache;
2818   Register Roffset= G4_scratch;
2819   Register Rflags = G1_scratch;
2820   ByteSize cp_base_offset = ConstantPoolCache::base_offset();
2821 
2822   jvmti_post_fast_field_mod();
2823 
2824   __ get_cache_and_index_at_bcp(Rcache, G4_scratch, 1);
2825 
2826   Assembler::Membar_mask_bits read_bits =
2827     Assembler::Membar_mask_bits(Assembler::LoadStore | Assembler::StoreStore);
2828   Assembler::Membar_mask_bits write_bits = Assembler::StoreLoad;
2829 
2830   Label notVolatile, checkVolatile, exit;
2831   if (__ membar_has_effect(read_bits) || __ membar_has_effect(write_bits)) {
2832     __ ld_ptr(Rcache, cp_base_offset + ConstantPoolCacheEntry::flags_offset(), Rflags);
2833     __ set((1 << ConstantPoolCacheEntry::is_volatile_shift), Lscratch);
2834     __ and3(Rflags, Lscratch, Lscratch);
2835     if (__ membar_has_effect(read_bits)) {
2836       __ cmp_and_br_short(Lscratch, 0, Assembler::equal, Assembler::pt, notVolatile);
2837       volatile_barrier(read_bits);
2838       __ bind(notVolatile);
2839     }
2840   }
2841 
2842   __ ld_ptr(Rcache, cp_base_offset + ConstantPoolCacheEntry::f2_offset(), Roffset);
2843   pop_and_check_object(Rclass);
2844 
2845   switch (bytecode()) {
2846     case Bytecodes::_fast_zputfield: __ and3(Otos_i, 1, Otos_i);  // fall through to bputfield
2847     case Bytecodes::_fast_bputfield: __ stb(Otos_i, Rclass, Roffset); break;
2848     case Bytecodes::_fast_cputfield: /* fall through */
2849     case Bytecodes::_fast_sputfield: __ sth(Otos_i, Rclass, Roffset); break;
2850     case Bytecodes::_fast_iputfield: __ st(Otos_i, Rclass, Roffset);  break;
2851     case Bytecodes::_fast_lputfield: __ st_long(Otos_l, Rclass, Roffset); break;
2852     case Bytecodes::_fast_fputfield:
2853       __ stf(FloatRegisterImpl::S, Ftos_f, Rclass, Roffset);
2854       break;
2855     case Bytecodes::_fast_dputfield:
2856       __ stf(FloatRegisterImpl::D, Ftos_d, Rclass, Roffset);
2857       break;
2858     case Bytecodes::_fast_aputfield:
2859       do_oop_store(_masm, Rclass, Roffset, 0, Otos_i, G1_scratch, _bs->kind(), false);
2860       break;
2861     default:
2862       ShouldNotReachHere();
2863   }
2864 
2865   if (__ membar_has_effect(write_bits)) {
2866     __ cmp_and_br_short(Lscratch, 0, Assembler::equal, Assembler::pt, exit);
2867     volatile_barrier(Assembler::StoreLoad);
2868     __ bind(exit);
2869   }
2870 }
2871 
2872 void TemplateTable::putfield(int byte_no) {
2873   putfield_or_static(byte_no, false);
2874 }
2875 
2876 void TemplateTable::nofast_putfield(int byte_no) {
2877   putfield_or_static(byte_no, false, may_not_rewrite);
2878 }
2879 
2880 void TemplateTable::putstatic(int byte_no) {
2881   putfield_or_static(byte_no, true);
2882 }
2883 
2884 void TemplateTable::fast_xaccess(TosState state) {
2885   transition(vtos, state);
2886   Register Rcache = G3_scratch;
2887   Register Roffset = G4_scratch;
2888   Register Rflags  = G4_scratch;
2889   Register Rreceiver = Lscratch;
2890 
2891   __ ld_ptr(Llocals, 0, Rreceiver);
2892 
2893   // access constant pool cache  (is resolved)
2894   __ get_cache_and_index_at_bcp(Rcache, G4_scratch, 2);
2895   __ ld_ptr(Rcache, ConstantPoolCache::base_offset() + ConstantPoolCacheEntry::f2_offset(), Roffset);
2896   __ add(Lbcp, 1, Lbcp);       // needed to report exception at the correct bcp
2897 
2898   __ verify_oop(Rreceiver);
2899   __ null_check(Rreceiver);
2900   if (state == atos) {
2901     __ load_heap_oop(Rreceiver, Roffset, Otos_i);
2902   } else if (state == itos) {
2903     __ ld (Rreceiver, Roffset, Otos_i) ;
2904   } else if (state == ftos) {
2905     __ ldf(FloatRegisterImpl::S, Rreceiver, Roffset, Ftos_f);
2906   } else {
2907     ShouldNotReachHere();
2908   }
2909 
2910   Assembler::Membar_mask_bits membar_bits =
2911     Assembler::Membar_mask_bits(Assembler::LoadLoad | Assembler::LoadStore);
2912   if (__ membar_has_effect(membar_bits)) {
2913 
2914     // Get is_volatile value in Rflags and check if membar is needed
2915     __ ld_ptr(Rcache, ConstantPoolCache::base_offset() + ConstantPoolCacheEntry::flags_offset(), Rflags);
2916 
2917     // Test volatile
2918     Label notVolatile;
2919     __ set((1 << ConstantPoolCacheEntry::is_volatile_shift), Lscratch);
2920     __ btst(Rflags, Lscratch);
2921     __ br(Assembler::zero, false, Assembler::pt, notVolatile);
2922     __ delayed()->nop();
2923     volatile_barrier(membar_bits);
2924     __ bind(notVolatile);
2925   }
2926 
2927   __ interp_verify_oop(Otos_i, state, __FILE__, __LINE__);
2928   __ sub(Lbcp, 1, Lbcp);
2929 }
2930 
2931 //----------------------------------------------------------------------------------------------------
2932 // Calls
2933 
2934 void TemplateTable::count_calls(Register method, Register temp) {
2935   // implemented elsewhere
2936   ShouldNotReachHere();
2937 }
2938 
2939 void TemplateTable::prepare_invoke(int byte_no,
2940                                    Register method,  // linked method (or i-klass)
2941                                    Register ra,      // return address
2942                                    Register index,   // itable index, MethodType, etc.
2943                                    Register recv,    // if caller wants to see it
2944                                    Register flags    // if caller wants to test it
2945                                    ) {
2946   // determine flags
2947   const Bytecodes::Code code = bytecode();
2948   const bool is_invokeinterface  = code == Bytecodes::_invokeinterface;
2949   const bool is_invokedynamic    = code == Bytecodes::_invokedynamic;
2950   const bool is_invokehandle     = code == Bytecodes::_invokehandle;
2951   const bool is_invokevirtual    = code == Bytecodes::_invokevirtual;
2952   const bool is_invokespecial    = code == Bytecodes::_invokespecial;
2953   const bool load_receiver       = (recv != noreg);
2954   assert(load_receiver == (code != Bytecodes::_invokestatic && code != Bytecodes::_invokedynamic), "");
2955   assert(recv  == noreg || recv  == O0, "");
2956   assert(flags == noreg || flags == O1, "");
2957 
2958   // setup registers & access constant pool cache
2959   if (recv  == noreg)  recv  = O0;
2960   if (flags == noreg)  flags = O1;
2961   const Register temp = O2;
2962   assert_different_registers(method, ra, index, recv, flags, temp);
2963 
2964   load_invoke_cp_cache_entry(byte_no, method, index, flags, is_invokevirtual, false, is_invokedynamic);
2965 
2966   __ mov(SP, O5_savedSP);  // record SP that we wanted the callee to restore
2967 
2968   // maybe push appendix to arguments
2969   if (is_invokedynamic || is_invokehandle) {
2970     Label L_no_push;
2971     __ set((1 << ConstantPoolCacheEntry::has_appendix_shift), temp);
2972     __ btst(flags, temp);
2973     __ br(Assembler::zero, false, Assembler::pt, L_no_push);
2974     __ delayed()->nop();
2975     // Push the appendix as a trailing parameter.
2976     // This must be done before we get the receiver,
2977     // since the parameter_size includes it.
2978     assert(ConstantPoolCacheEntry::_indy_resolved_references_appendix_offset == 0, "appendix expected at index+0");
2979     __ load_resolved_reference_at_index(temp, index);
2980     __ verify_oop(temp);
2981     __ push_ptr(temp);  // push appendix (MethodType, CallSite, etc.)
2982     __ bind(L_no_push);
2983   }
2984 
2985   // load receiver if needed (after appendix is pushed so parameter size is correct)
2986   if (load_receiver) {
2987     __ and3(flags, ConstantPoolCacheEntry::parameter_size_mask, temp);  // get parameter size
2988     __ load_receiver(temp, recv);  //  __ argument_address uses Gargs but we need Lesp
2989     __ verify_oop(recv);
2990   }
2991 
2992   // compute return type
2993   __ srl(flags, ConstantPoolCacheEntry::tos_state_shift, ra);
2994   // Make sure we don't need to mask flags after the above shift
2995   ConstantPoolCacheEntry::verify_tos_state_shift();
2996   // load return address
2997   {
2998     const address table_addr = (address) Interpreter::invoke_return_entry_table_for(code);
2999     AddressLiteral table(table_addr);
3000     __ set(table, temp);
3001     __ sll(ra, LogBytesPerWord, ra);
3002     __ ld_ptr(Address(temp, ra), ra);
3003   }
3004 }
3005 
3006 
3007 void TemplateTable::generate_vtable_call(Register Rrecv, Register Rindex, Register Rret) {
3008   Register Rtemp = G4_scratch;
3009   Register Rcall = Rindex;
3010   assert_different_registers(Rcall, G5_method, Gargs, Rret);
3011 
3012   // get target Method* & entry point
3013   __ lookup_virtual_method(Rrecv, Rindex, G5_method);
3014   __ profile_arguments_type(G5_method, Rcall, Gargs, true);
3015   __ profile_called_method(G5_method, Rtemp);
3016   __ call_from_interpreter(Rcall, Gargs, Rret);
3017 }
3018 
3019 void TemplateTable::invokevirtual(int byte_no) {
3020   transition(vtos, vtos);
3021   assert(byte_no == f2_byte, "use this argument");
3022 
3023   Register Rscratch = G3_scratch;
3024   Register Rtemp    = G4_scratch;
3025   Register Rret     = Lscratch;
3026   Register O0_recv  = O0;
3027   Label notFinal;
3028 
3029   load_invoke_cp_cache_entry(byte_no, G5_method, noreg, Rret, true, false, false);
3030   __ mov(SP, O5_savedSP); // record SP that we wanted the callee to restore
3031 
3032   // Check for vfinal
3033   __ set((1 << ConstantPoolCacheEntry::is_vfinal_shift), G4_scratch);
3034   __ btst(Rret, G4_scratch);
3035   __ br(Assembler::zero, false, Assembler::pt, notFinal);
3036   __ delayed()->and3(Rret, 0xFF, G4_scratch);      // gets number of parameters
3037 
3038   if (RewriteBytecodes && !UseSharedSpaces) {
3039     patch_bytecode(Bytecodes::_fast_invokevfinal, Rscratch, Rtemp);
3040   }
3041 
3042   invokevfinal_helper(Rscratch, Rret);
3043 
3044   __ bind(notFinal);
3045 
3046   __ mov(G5_method, Rscratch);  // better scratch register
3047   __ load_receiver(G4_scratch, O0_recv);  // gets receiverOop
3048   // receiver is in O0_recv
3049   __ verify_oop(O0_recv);
3050 
3051   // get return address
3052   AddressLiteral table(Interpreter::invoke_return_entry_table());
3053   __ set(table, Rtemp);
3054   __ srl(Rret, ConstantPoolCacheEntry::tos_state_shift, Rret);          // get return type
3055   // Make sure we don't need to mask Rret after the above shift
3056   ConstantPoolCacheEntry::verify_tos_state_shift();
3057   __ sll(Rret,  LogBytesPerWord, Rret);
3058   __ ld_ptr(Rtemp, Rret, Rret);         // get return address
3059 
3060   // get receiver klass
3061   __ null_check(O0_recv, oopDesc::klass_offset_in_bytes());
3062   __ load_klass(O0_recv, O0_recv);
3063   __ verify_klass_ptr(O0_recv);
3064 
3065   __ profile_virtual_call(O0_recv, O4);
3066 
3067   generate_vtable_call(O0_recv, Rscratch, Rret);
3068 }
3069 
3070 void TemplateTable::fast_invokevfinal(int byte_no) {
3071   transition(vtos, vtos);
3072   assert(byte_no == f2_byte, "use this argument");
3073 
3074   load_invoke_cp_cache_entry(byte_no, G5_method, noreg, Lscratch, true,
3075                              /*is_invokevfinal*/true, false);
3076   __ mov(SP, O5_savedSP); // record SP that we wanted the callee to restore
3077   invokevfinal_helper(G3_scratch, Lscratch);
3078 }
3079 
3080 void TemplateTable::invokevfinal_helper(Register Rscratch, Register Rret) {
3081   Register Rtemp = G4_scratch;
3082 
3083   // Load receiver from stack slot
3084   __ ld_ptr(G5_method, in_bytes(Method::const_offset()), G4_scratch);
3085   __ lduh(G4_scratch, in_bytes(ConstMethod::size_of_parameters_offset()), G4_scratch);
3086   __ load_receiver(G4_scratch, O0);
3087 
3088   // receiver NULL check
3089   __ null_check(O0);
3090 
3091   __ profile_final_call(O4);
3092   __ profile_arguments_type(G5_method, Rscratch, Gargs, true);
3093 
3094   // get return address
3095   AddressLiteral table(Interpreter::invoke_return_entry_table());
3096   __ set(table, Rtemp);
3097   __ srl(Rret, ConstantPoolCacheEntry::tos_state_shift, Rret);          // get return type
3098   // Make sure we don't need to mask Rret after the above shift
3099   ConstantPoolCacheEntry::verify_tos_state_shift();
3100   __ sll(Rret,  LogBytesPerWord, Rret);
3101   __ ld_ptr(Rtemp, Rret, Rret);         // get return address
3102 
3103 
3104   // do the call
3105   __ call_from_interpreter(Rscratch, Gargs, Rret);
3106 }
3107 
3108 
3109 void TemplateTable::invokespecial(int byte_no) {
3110   transition(vtos, vtos);
3111   assert(byte_no == f1_byte, "use this argument");
3112 
3113   const Register Rret     = Lscratch;
3114   const Register O0_recv  = O0;
3115   const Register Rscratch = G3_scratch;
3116 
3117   prepare_invoke(byte_no, G5_method, Rret, noreg, O0_recv);  // get receiver also for null check
3118   __ null_check(O0_recv);
3119 
3120   // do the call
3121   __ profile_call(O4);
3122   __ profile_arguments_type(G5_method, Rscratch, Gargs, false);
3123   __ call_from_interpreter(Rscratch, Gargs, Rret);
3124 }
3125 
3126 
3127 void TemplateTable::invokestatic(int byte_no) {
3128   transition(vtos, vtos);
3129   assert(byte_no == f1_byte, "use this argument");
3130 
3131   const Register Rret     = Lscratch;
3132   const Register Rscratch = G3_scratch;
3133 
3134   prepare_invoke(byte_no, G5_method, Rret);  // get f1 Method*
3135 
3136   // do the call
3137   __ profile_call(O4);
3138   __ profile_arguments_type(G5_method, Rscratch, Gargs, false);
3139   __ call_from_interpreter(Rscratch, Gargs, Rret);
3140 }
3141 
3142 void TemplateTable::invokeinterface_object_method(Register RKlass,
3143                                                   Register Rcall,
3144                                                   Register Rret,
3145                                                   Register Rflags) {
3146   Register Rscratch = G4_scratch;
3147   Register Rindex = Lscratch;
3148 
3149   assert_different_registers(Rscratch, Rindex, Rret);
3150 
3151   Label notFinal;
3152 
3153   // Check for vfinal
3154   __ set((1 << ConstantPoolCacheEntry::is_vfinal_shift), Rscratch);
3155   __ btst(Rflags, Rscratch);
3156   __ br(Assembler::zero, false, Assembler::pt, notFinal);
3157   __ delayed()->nop();
3158 
3159   __ profile_final_call(O4);
3160 
3161   // do the call - the index (f2) contains the Method*
3162   assert_different_registers(G5_method, Gargs, Rcall);
3163   __ mov(Rindex, G5_method);
3164   __ profile_arguments_type(G5_method, Rcall, Gargs, true);
3165   __ call_from_interpreter(Rcall, Gargs, Rret);
3166   __ bind(notFinal);
3167 
3168   __ profile_virtual_call(RKlass, O4);
3169   generate_vtable_call(RKlass, Rindex, Rret);
3170 }
3171 
3172 
3173 void TemplateTable::invokeinterface(int byte_no) {
3174   transition(vtos, vtos);
3175   assert(byte_no == f1_byte, "use this argument");
3176 
3177   const Register Rinterface  = G1_scratch;
3178   const Register Rret        = G3_scratch;
3179   const Register Rindex      = Lscratch;
3180   const Register O0_recv     = O0;
3181   const Register O1_flags    = O1;
3182   const Register O2_Klass    = O2;
3183   const Register Rscratch    = G4_scratch;
3184   assert_different_registers(Rscratch, G5_method);
3185 
3186   prepare_invoke(byte_no, Rinterface, Rret, Rindex, O0_recv, O1_flags);
3187 
3188   // get receiver klass
3189   __ null_check(O0_recv, oopDesc::klass_offset_in_bytes());
3190   __ load_klass(O0_recv, O2_Klass);
3191 
3192   // Special case of invokeinterface called for virtual method of
3193   // java.lang.Object.  See cpCacheOop.cpp for details.
3194   // This code isn't produced by javac, but could be produced by
3195   // another compliant java compiler.
3196   Label notMethod;
3197   __ set((1 << ConstantPoolCacheEntry::is_forced_virtual_shift), Rscratch);
3198   __ btst(O1_flags, Rscratch);
3199   __ br(Assembler::zero, false, Assembler::pt, notMethod);
3200   __ delayed()->nop();
3201 
3202   invokeinterface_object_method(O2_Klass, Rinterface, Rret, O1_flags);
3203 
3204   __ bind(notMethod);
3205 
3206   __ profile_virtual_call(O2_Klass, O4);
3207 
3208   //
3209   // find entry point to call
3210   //
3211 
3212   // compute start of first itableOffsetEntry (which is at end of vtable)
3213   const int base = in_bytes(Klass::vtable_start_offset());
3214   Label search;
3215   Register Rtemp = O1_flags;
3216 
3217   __ ld(O2_Klass, in_bytes(Klass::vtable_length_offset()), Rtemp);
3218   __ sll(Rtemp, LogBytesPerWord, Rtemp);   // Rscratch *= 4;
3219   if (Assembler::is_simm13(base)) {
3220     __ add(Rtemp, base, Rtemp);
3221   } else {
3222     __ set(base, Rscratch);
3223     __ add(Rscratch, Rtemp, Rtemp);
3224   }
3225   __ add(O2_Klass, Rtemp, Rscratch);
3226 
3227   __ bind(search);
3228 
3229   __ ld_ptr(Rscratch, itableOffsetEntry::interface_offset_in_bytes(), Rtemp);
3230   {
3231     Label ok;
3232 
3233     // Check that entry is non-null.  Null entries are probably a bytecode
3234     // problem.  If the interface isn't implemented by the receiver class,
3235     // the VM should throw IncompatibleClassChangeError.  linkResolver checks
3236     // this too but that's only if the entry isn't already resolved, so we
3237     // need to check again.
3238     __ br_notnull_short( Rtemp, Assembler::pt, ok);
3239     call_VM(noreg, CAST_FROM_FN_PTR(address, InterpreterRuntime::throw_IncompatibleClassChangeError));
3240     __ should_not_reach_here();
3241     __ bind(ok);
3242   }
3243 
3244   __ cmp(Rinterface, Rtemp);
3245   __ brx(Assembler::notEqual, true, Assembler::pn, search);
3246   __ delayed()->add(Rscratch, itableOffsetEntry::size() * wordSize, Rscratch);
3247 
3248   // entry found and Rscratch points to it
3249   __ ld(Rscratch, itableOffsetEntry::offset_offset_in_bytes(), Rscratch);
3250 
3251   assert(itableMethodEntry::method_offset_in_bytes() == 0, "adjust instruction below");
3252   __ sll(Rindex, exact_log2(itableMethodEntry::size() * wordSize), Rindex);       // Rindex *= 8;
3253   __ add(Rscratch, Rindex, Rscratch);
3254   __ ld_ptr(O2_Klass, Rscratch, G5_method);
3255 
3256   // Check for abstract method error.
3257   {
3258     Label ok;
3259     __ br_notnull_short(G5_method, Assembler::pt, ok);
3260     call_VM(noreg, CAST_FROM_FN_PTR(address, InterpreterRuntime::throw_AbstractMethodError));
3261     __ should_not_reach_here();
3262     __ bind(ok);
3263   }
3264 
3265   Register Rcall = Rinterface;
3266   assert_different_registers(Rcall, G5_method, Gargs, Rret);
3267 
3268   __ profile_arguments_type(G5_method, Rcall, Gargs, true);
3269   __ profile_called_method(G5_method, Rscratch);
3270   __ call_from_interpreter(Rcall, Gargs, Rret);
3271 }
3272 
3273 void TemplateTable::invokehandle(int byte_no) {
3274   transition(vtos, vtos);
3275   assert(byte_no == f1_byte, "use this argument");
3276 
3277   const Register Rret       = Lscratch;
3278   const Register G4_mtype   = G4_scratch;
3279   const Register O0_recv    = O0;
3280   const Register Rscratch   = G3_scratch;
3281 
3282   prepare_invoke(byte_no, G5_method, Rret, G4_mtype, O0_recv);
3283   __ null_check(O0_recv);
3284 
3285   // G4: MethodType object (from cpool->resolved_references[f1], if necessary)
3286   // G5: MH.invokeExact_MT method (from f2)
3287 
3288   // Note:  G4_mtype is already pushed (if necessary) by prepare_invoke
3289 
3290   // do the call
3291   __ verify_oop(G4_mtype);
3292   __ profile_final_call(O4);  // FIXME: profile the LambdaForm also
3293   __ profile_arguments_type(G5_method, Rscratch, Gargs, true);
3294   __ call_from_interpreter(Rscratch, Gargs, Rret);
3295 }
3296 
3297 
3298 void TemplateTable::invokedynamic(int byte_no) {
3299   transition(vtos, vtos);
3300   assert(byte_no == f1_byte, "use this argument");
3301 
3302   const Register Rret        = Lscratch;
3303   const Register G4_callsite = G4_scratch;
3304   const Register Rscratch    = G3_scratch;
3305 
3306   prepare_invoke(byte_no, G5_method, Rret, G4_callsite);
3307 
3308   // G4: CallSite object (from cpool->resolved_references[f1])
3309   // G5: MH.linkToCallSite method (from f2)
3310 
3311   // Note:  G4_callsite is already pushed by prepare_invoke
3312 
3313   // %%% should make a type profile for any invokedynamic that takes a ref argument
3314   // profile this call
3315   __ profile_call(O4);
3316 
3317   // do the call
3318   __ verify_oop(G4_callsite);
3319   __ profile_arguments_type(G5_method, Rscratch, Gargs, false);
3320   __ call_from_interpreter(Rscratch, Gargs, Rret);
3321 }
3322 
3323 
3324 //----------------------------------------------------------------------------------------------------
3325 // Allocation
3326 
3327 void TemplateTable::_new() {
3328   transition(vtos, atos);
3329 
3330   Label slow_case;
3331   Label done;
3332   Label initialize_header;
3333   Label initialize_object;  // including clearing the fields
3334 
3335   Register RallocatedObject = Otos_i;
3336   Register RinstanceKlass = O1;
3337   Register Roffset = O3;
3338   Register Rscratch = O4;
3339 
3340   __ get_2_byte_integer_at_bcp(1, Rscratch, Roffset, InterpreterMacroAssembler::Unsigned);
3341   __ get_cpool_and_tags(Rscratch, G3_scratch);
3342   // make sure the class we're about to instantiate has been resolved
3343   // This is done before loading InstanceKlass to be consistent with the order
3344   // how Constant Pool is updated (see ConstantPool::klass_at_put)
3345   __ add(G3_scratch, Array<u1>::base_offset_in_bytes(), G3_scratch);
3346   __ ldub(G3_scratch, Roffset, G3_scratch);
3347   __ cmp(G3_scratch, JVM_CONSTANT_Class);
3348   __ br(Assembler::notEqual, false, Assembler::pn, slow_case);
3349   __ delayed()->sll(Roffset, LogBytesPerWord, Roffset);
3350   // get InstanceKlass
3351   //__ sll(Roffset, LogBytesPerWord, Roffset);        // executed in delay slot
3352   __ add(Roffset, sizeof(ConstantPool), Roffset);
3353   __ ld_ptr(Rscratch, Roffset, RinstanceKlass);
3354 
3355   // make sure klass is fully initialized:
3356   __ ldub(RinstanceKlass, in_bytes(InstanceKlass::init_state_offset()), G3_scratch);
3357   __ cmp(G3_scratch, InstanceKlass::fully_initialized);
3358   __ br(Assembler::notEqual, false, Assembler::pn, slow_case);
3359   __ delayed()->ld(RinstanceKlass, in_bytes(Klass::layout_helper_offset()), Roffset);
3360 
3361   // get instance_size in InstanceKlass (already aligned)
3362   //__ ld(RinstanceKlass, in_bytes(Klass::layout_helper_offset()), Roffset);
3363 
3364   // make sure klass does not have has_finalizer, or is abstract, or interface or java/lang/Class
3365   __ btst(Klass::_lh_instance_slow_path_bit, Roffset);
3366   __ br(Assembler::notZero, false, Assembler::pn, slow_case);
3367   __ delayed()->nop();
3368 
3369   // allocate the instance
3370   // 1) Try to allocate in the TLAB
3371   // 2) if fail, and the TLAB is not full enough to discard, allocate in the shared Eden
3372   // 3) if the above fails (or is not applicable), go to a slow case
3373   // (creates a new TLAB, etc.)
3374 
3375   const bool allow_shared_alloc =
3376     Universe::heap()->supports_inline_contig_alloc();
3377 
3378   if(UseTLAB) {
3379     Register RoldTopValue = RallocatedObject;
3380     Register RtlabWasteLimitValue = G3_scratch;
3381     Register RnewTopValue = G1_scratch;
3382     Register RendValue = Rscratch;
3383     Register RfreeValue = RnewTopValue;
3384 
3385     // check if we can allocate in the TLAB
3386     __ ld_ptr(G2_thread, in_bytes(JavaThread::tlab_top_offset()), RoldTopValue); // sets up RalocatedObject
3387     __ ld_ptr(G2_thread, in_bytes(JavaThread::tlab_end_offset()), RendValue);
3388     __ add(RoldTopValue, Roffset, RnewTopValue);
3389 
3390     // if there is enough space, we do not CAS and do not clear
3391     __ cmp(RnewTopValue, RendValue);
3392     if(ZeroTLAB) {
3393       // the fields have already been cleared
3394       __ brx(Assembler::lessEqualUnsigned, true, Assembler::pt, initialize_header);
3395     } else {
3396       // initialize both the header and fields
3397       __ brx(Assembler::lessEqualUnsigned, true, Assembler::pt, initialize_object);
3398     }
3399     __ delayed()->st_ptr(RnewTopValue, G2_thread, in_bytes(JavaThread::tlab_top_offset()));
3400 
3401     if (allow_shared_alloc) {
3402       // Check if tlab should be discarded (refill_waste_limit >= free)
3403       __ ld_ptr(G2_thread, in_bytes(JavaThread::tlab_refill_waste_limit_offset()), RtlabWasteLimitValue);
3404       __ sub(RendValue, RoldTopValue, RfreeValue);
3405 #ifdef _LP64
3406       __ srlx(RfreeValue, LogHeapWordSize, RfreeValue);
3407 #else
3408       __ srl(RfreeValue, LogHeapWordSize, RfreeValue);
3409 #endif
3410       __ cmp_and_brx_short(RtlabWasteLimitValue, RfreeValue, Assembler::greaterEqualUnsigned, Assembler::pt, slow_case); // tlab waste is small
3411 
3412       // increment waste limit to prevent getting stuck on this slow path
3413       if (Assembler::is_simm13(ThreadLocalAllocBuffer::refill_waste_limit_increment())) {
3414         __ add(RtlabWasteLimitValue, ThreadLocalAllocBuffer::refill_waste_limit_increment(), RtlabWasteLimitValue);
3415       } else {
3416         // set64 does not use the temp register if the given constant is 32 bit. So
3417         // we can just use any register; using G0 results in ignoring of the upper 32 bit
3418         // of that value.
3419         __ set64(ThreadLocalAllocBuffer::refill_waste_limit_increment(), G4_scratch, G0);
3420         __ add(RtlabWasteLimitValue, G4_scratch, RtlabWasteLimitValue);
3421       }
3422       __ st_ptr(RtlabWasteLimitValue, G2_thread, in_bytes(JavaThread::tlab_refill_waste_limit_offset()));
3423     } else {
3424       // No allocation in the shared eden.
3425       __ ba_short(slow_case);
3426     }
3427   }
3428 
3429   // Allocation in the shared Eden
3430   if (allow_shared_alloc) {
3431     Register RoldTopValue = G1_scratch;
3432     Register RtopAddr = G3_scratch;
3433     Register RnewTopValue = RallocatedObject;
3434     Register RendValue = Rscratch;
3435 
3436     __ set((intptr_t)Universe::heap()->top_addr(), RtopAddr);
3437 
3438     Label retry;
3439     __ bind(retry);
3440     __ set((intptr_t)Universe::heap()->end_addr(), RendValue);
3441     __ ld_ptr(RendValue, 0, RendValue);
3442     __ ld_ptr(RtopAddr, 0, RoldTopValue);
3443     __ add(RoldTopValue, Roffset, RnewTopValue);
3444 
3445     // RnewTopValue contains the top address after the new object
3446     // has been allocated.
3447     __ cmp_and_brx_short(RnewTopValue, RendValue, Assembler::greaterUnsigned, Assembler::pn, slow_case);
3448 
3449     __ cas_ptr(RtopAddr, RoldTopValue, RnewTopValue);
3450 
3451     // if someone beat us on the allocation, try again, otherwise continue
3452     __ cmp_and_brx_short(RoldTopValue, RnewTopValue, Assembler::notEqual, Assembler::pn, retry);
3453 
3454     // bump total bytes allocated by this thread
3455     // RoldTopValue and RtopAddr are dead, so can use G1 and G3
3456     __ incr_allocated_bytes(Roffset, G1_scratch, G3_scratch);
3457   }
3458 
3459   if (UseTLAB || Universe::heap()->supports_inline_contig_alloc()) {
3460     // clear object fields
3461     __ bind(initialize_object);
3462     __ deccc(Roffset, sizeof(oopDesc));
3463     __ br(Assembler::zero, false, Assembler::pt, initialize_header);
3464     __ delayed()->add(RallocatedObject, sizeof(oopDesc), G3_scratch);
3465 
3466     // initialize remaining object fields
3467     if (UseBlockZeroing) {
3468       // Use BIS for zeroing
3469       __ bis_zeroing(G3_scratch, Roffset, G1_scratch, initialize_header);
3470     } else {
3471       Label loop;
3472       __ subcc(Roffset, wordSize, Roffset);
3473       __ bind(loop);
3474       //__ subcc(Roffset, wordSize, Roffset);      // executed above loop or in delay slot
3475       __ st_ptr(G0, G3_scratch, Roffset);
3476       __ br(Assembler::notEqual, false, Assembler::pt, loop);
3477       __ delayed()->subcc(Roffset, wordSize, Roffset);
3478     }
3479     __ ba_short(initialize_header);
3480   }
3481 
3482   // slow case
3483   __ bind(slow_case);
3484   __ get_2_byte_integer_at_bcp(1, G3_scratch, O2, InterpreterMacroAssembler::Unsigned);
3485   __ get_constant_pool(O1);
3486 
3487   call_VM(Otos_i, CAST_FROM_FN_PTR(address, InterpreterRuntime::_new), O1, O2);
3488 
3489   __ ba_short(done);
3490 
3491   // Initialize the header: mark, klass
3492   __ bind(initialize_header);
3493 
3494   if (UseBiasedLocking) {
3495     __ ld_ptr(RinstanceKlass, in_bytes(Klass::prototype_header_offset()), G4_scratch);
3496   } else {
3497     __ set((intptr_t)markOopDesc::prototype(), G4_scratch);
3498   }
3499   __ st_ptr(G4_scratch, RallocatedObject, oopDesc::mark_offset_in_bytes());       // mark
3500   __ store_klass_gap(G0, RallocatedObject);         // klass gap if compressed
3501   __ store_klass(RinstanceKlass, RallocatedObject); // klass (last for cms)
3502 
3503   {
3504     SkipIfEqual skip_if(
3505       _masm, G4_scratch, &DTraceAllocProbes, Assembler::zero);
3506     // Trigger dtrace event
3507     __ push(atos);
3508     __ call_VM_leaf(noreg,
3509        CAST_FROM_FN_PTR(address, SharedRuntime::dtrace_object_alloc), O0);
3510     __ pop(atos);
3511   }
3512 
3513   // continue
3514   __ bind(done);
3515 }
3516 
3517 
3518 
3519 void TemplateTable::newarray() {
3520   transition(itos, atos);
3521   __ ldub(Lbcp, 1, O1);
3522      call_VM(Otos_i, CAST_FROM_FN_PTR(address, InterpreterRuntime::newarray), O1, Otos_i);
3523 }
3524 
3525 
3526 void TemplateTable::anewarray() {
3527   transition(itos, atos);
3528   __ get_constant_pool(O1);
3529   __ get_2_byte_integer_at_bcp(1, G4_scratch, O2, InterpreterMacroAssembler::Unsigned);
3530      call_VM(Otos_i, CAST_FROM_FN_PTR(address, InterpreterRuntime::anewarray), O1, O2, Otos_i);
3531 }
3532 
3533 
3534 void TemplateTable::arraylength() {
3535   transition(atos, itos);
3536   Label ok;
3537   __ verify_oop(Otos_i);
3538   __ tst(Otos_i);
3539   __ throw_if_not_1_x( Assembler::notZero, ok );
3540   __ delayed()->ld(Otos_i, arrayOopDesc::length_offset_in_bytes(), Otos_i);
3541   __ throw_if_not_2( Interpreter::_throw_NullPointerException_entry, G3_scratch, ok);
3542 }
3543 
3544 
3545 void TemplateTable::checkcast() {
3546   transition(atos, atos);
3547   Label done, is_null, quicked, cast_ok, resolved;
3548   Register Roffset = G1_scratch;
3549   Register RobjKlass = O5;
3550   Register RspecifiedKlass = O4;
3551 
3552   // Check for casting a NULL
3553   __ br_null(Otos_i, false, Assembler::pn, is_null);
3554   __ delayed()->nop();
3555 
3556   // Get value klass in RobjKlass
3557   __ load_klass(Otos_i, RobjKlass); // get value klass
3558 
3559   // Get constant pool tag
3560   __ get_2_byte_integer_at_bcp(1, Lscratch, Roffset, InterpreterMacroAssembler::Unsigned);
3561 
3562   // See if the checkcast has been quickened
3563   __ get_cpool_and_tags(Lscratch, G3_scratch);
3564   __ add(G3_scratch, Array<u1>::base_offset_in_bytes(), G3_scratch);
3565   __ ldub(G3_scratch, Roffset, G3_scratch);
3566   __ cmp(G3_scratch, JVM_CONSTANT_Class);
3567   __ br(Assembler::equal, true, Assembler::pt, quicked);
3568   __ delayed()->sll(Roffset, LogBytesPerWord, Roffset);
3569 
3570   __ push_ptr(); // save receiver for result, and for GC
3571   call_VM(noreg, CAST_FROM_FN_PTR(address, InterpreterRuntime::quicken_io_cc) );
3572   __ get_vm_result_2(RspecifiedKlass);
3573   __ pop_ptr(Otos_i, G3_scratch); // restore receiver
3574 
3575   __ ba_short(resolved);
3576 
3577   // Extract target class from constant pool
3578   __ bind(quicked);
3579   __ add(Roffset, sizeof(ConstantPool), Roffset);
3580   __ ld_ptr(Lscratch, Roffset, RspecifiedKlass);
3581   __ bind(resolved);
3582   __ load_klass(Otos_i, RobjKlass); // get value klass
3583 
3584   // Generate a fast subtype check.  Branch to cast_ok if no
3585   // failure.  Throw exception if failure.
3586   __ gen_subtype_check( RobjKlass, RspecifiedKlass, G3_scratch, G4_scratch, G1_scratch, cast_ok );
3587 
3588   // Not a subtype; so must throw exception
3589   __ throw_if_not_x( Assembler::never, Interpreter::_throw_ClassCastException_entry, G3_scratch );
3590 
3591   __ bind(cast_ok);
3592 
3593   if (ProfileInterpreter) {
3594     __ ba_short(done);
3595   }
3596   __ bind(is_null);
3597   __ profile_null_seen(G3_scratch);
3598   __ bind(done);
3599 }
3600 
3601 
3602 void TemplateTable::instanceof() {
3603   Label done, is_null, quicked, resolved;
3604   transition(atos, itos);
3605   Register Roffset = G1_scratch;
3606   Register RobjKlass = O5;
3607   Register RspecifiedKlass = O4;
3608 
3609   // Check for casting a NULL
3610   __ br_null(Otos_i, false, Assembler::pt, is_null);
3611   __ delayed()->nop();
3612 
3613   // Get value klass in RobjKlass
3614   __ load_klass(Otos_i, RobjKlass); // get value klass
3615 
3616   // Get constant pool tag
3617   __ get_2_byte_integer_at_bcp(1, Lscratch, Roffset, InterpreterMacroAssembler::Unsigned);
3618 
3619   // See if the checkcast has been quickened
3620   __ get_cpool_and_tags(Lscratch, G3_scratch);
3621   __ add(G3_scratch, Array<u1>::base_offset_in_bytes(), G3_scratch);
3622   __ ldub(G3_scratch, Roffset, G3_scratch);
3623   __ cmp(G3_scratch, JVM_CONSTANT_Class);
3624   __ br(Assembler::equal, true, Assembler::pt, quicked);
3625   __ delayed()->sll(Roffset, LogBytesPerWord, Roffset);
3626 
3627   __ push_ptr(); // save receiver for result, and for GC
3628   call_VM(noreg, CAST_FROM_FN_PTR(address, InterpreterRuntime::quicken_io_cc) );
3629   __ get_vm_result_2(RspecifiedKlass);
3630   __ pop_ptr(Otos_i, G3_scratch); // restore receiver
3631 
3632   __ ba_short(resolved);
3633 
3634   // Extract target class from constant pool
3635   __ bind(quicked);
3636   __ add(Roffset, sizeof(ConstantPool), Roffset);
3637   __ get_constant_pool(Lscratch);
3638   __ ld_ptr(Lscratch, Roffset, RspecifiedKlass);
3639   __ bind(resolved);
3640   __ load_klass(Otos_i, RobjKlass); // get value klass
3641 
3642   // Generate a fast subtype check.  Branch to cast_ok if no
3643   // failure.  Return 0 if failure.
3644   __ or3(G0, 1, Otos_i);      // set result assuming quick tests succeed
3645   __ gen_subtype_check( RobjKlass, RspecifiedKlass, G3_scratch, G4_scratch, G1_scratch, done );
3646   // Not a subtype; return 0;
3647   __ clr( Otos_i );
3648 
3649   if (ProfileInterpreter) {
3650     __ ba_short(done);
3651   }
3652   __ bind(is_null);
3653   __ profile_null_seen(G3_scratch);
3654   __ bind(done);
3655 }
3656 
3657 void TemplateTable::_breakpoint() {
3658 
3659    // Note: We get here even if we are single stepping..
3660    // jbug inists on setting breakpoints at every bytecode
3661    // even if we are in single step mode.
3662 
3663    transition(vtos, vtos);
3664    // get the unpatched byte code
3665    __ call_VM(noreg, CAST_FROM_FN_PTR(address, InterpreterRuntime::get_original_bytecode_at), Lmethod, Lbcp);
3666    __ mov(O0, Lbyte_code);
3667 
3668    // post the breakpoint event
3669    __ call_VM(noreg, CAST_FROM_FN_PTR(address, InterpreterRuntime::_breakpoint), Lmethod, Lbcp);
3670 
3671    // complete the execution of original bytecode
3672    __ dispatch_normal(vtos);
3673 }
3674 
3675 
3676 //----------------------------------------------------------------------------------------------------
3677 // Exceptions
3678 
3679 void TemplateTable::athrow() {
3680   transition(atos, vtos);
3681 
3682   // This works because exception is cached in Otos_i which is same as O0,
3683   // which is same as what throw_exception_entry_expects
3684   assert(Otos_i == Oexception, "see explanation above");
3685 
3686   __ verify_oop(Otos_i);
3687   __ null_check(Otos_i);
3688   __ throw_if_not_x(Assembler::never, Interpreter::throw_exception_entry(), G3_scratch);
3689 }
3690 
3691 
3692 //----------------------------------------------------------------------------------------------------
3693 // Synchronization
3694 
3695 
3696 // See frame_sparc.hpp for monitor block layout.
3697 // Monitor elements are dynamically allocated by growing stack as needed.
3698 
3699 void TemplateTable::monitorenter() {
3700   transition(atos, vtos);
3701   __ verify_oop(Otos_i);
3702   // Try to acquire a lock on the object
3703   // Repeat until succeeded (i.e., until
3704   // monitorenter returns true).
3705 
3706   {   Label ok;
3707     __ tst(Otos_i);
3708     __ throw_if_not_1_x( Assembler::notZero,  ok);
3709     __ delayed()->mov(Otos_i, Lscratch); // save obj
3710     __ throw_if_not_2( Interpreter::_throw_NullPointerException_entry, G3_scratch, ok);
3711   }
3712 
3713   assert(O0 == Otos_i, "Be sure where the object to lock is");
3714 
3715   // find a free slot in the monitor block
3716 
3717 
3718   // initialize entry pointer
3719   __ clr(O1); // points to free slot or NULL
3720 
3721   {
3722     Label entry, loop, exit;
3723     __ add( __ top_most_monitor(), O2 ); // last one to check
3724     __ ba( entry );
3725     __ delayed()->mov( Lmonitors, O3 ); // first one to check
3726 
3727 
3728     __ bind( loop );
3729 
3730     __ verify_oop(O4);          // verify each monitor's oop
3731     __ tst(O4); // is this entry unused?
3732     __ movcc( Assembler::zero, false, Assembler::ptr_cc, O3, O1);
3733 
3734     __ cmp(O4, O0); // check if current entry is for same object
3735     __ brx( Assembler::equal, false, Assembler::pn, exit );
3736     __ delayed()->inc( O3, frame::interpreter_frame_monitor_size() * wordSize ); // check next one
3737 
3738     __ bind( entry );
3739 
3740     __ cmp( O3, O2 );
3741     __ brx( Assembler::lessEqualUnsigned, true, Assembler::pt, loop );
3742     __ delayed()->ld_ptr(O3, BasicObjectLock::obj_offset_in_bytes(), O4);
3743 
3744     __ bind( exit );
3745   }
3746 
3747   { Label allocated;
3748 
3749     // found free slot?
3750     __ br_notnull_short(O1, Assembler::pn, allocated);
3751 
3752     __ add_monitor_to_stack( false, O2, O3 );
3753     __ mov(Lmonitors, O1);
3754 
3755     __ bind(allocated);
3756   }
3757 
3758   // Increment bcp to point to the next bytecode, so exception handling for async. exceptions work correctly.
3759   // The object has already been poped from the stack, so the expression stack looks correct.
3760   __ inc(Lbcp);
3761 
3762   __ st_ptr(O0, O1, BasicObjectLock::obj_offset_in_bytes()); // store object
3763   __ lock_object(O1, O0);
3764 
3765   // check if there's enough space on the stack for the monitors after locking
3766   __ generate_stack_overflow_check(0);
3767 
3768   // The bcp has already been incremented. Just need to dispatch to next instruction.
3769   __ dispatch_next(vtos);
3770 }
3771 
3772 
3773 void TemplateTable::monitorexit() {
3774   transition(atos, vtos);
3775   __ verify_oop(Otos_i);
3776   __ tst(Otos_i);
3777   __ throw_if_not_x( Assembler::notZero, Interpreter::_throw_NullPointerException_entry, G3_scratch );
3778 
3779   assert(O0 == Otos_i, "just checking");
3780 
3781   { Label entry, loop, found;
3782     __ add( __ top_most_monitor(), O2 ); // last one to check
3783     __ ba(entry);
3784     // use Lscratch to hold monitor elem to check, start with most recent monitor,
3785     // By using a local it survives the call to the C routine.
3786     __ delayed()->mov( Lmonitors, Lscratch );
3787 
3788     __ bind( loop );
3789 
3790     __ verify_oop(O4);          // verify each monitor's oop
3791     __ cmp(O4, O0); // check if current entry is for desired object
3792     __ brx( Assembler::equal, true, Assembler::pt, found );
3793     __ delayed()->mov(Lscratch, O1); // pass found entry as argument to monitorexit
3794 
3795     __ inc( Lscratch, frame::interpreter_frame_monitor_size() * wordSize ); // advance to next
3796 
3797     __ bind( entry );
3798 
3799     __ cmp( Lscratch, O2 );
3800     __ brx( Assembler::lessEqualUnsigned, true, Assembler::pt, loop );
3801     __ delayed()->ld_ptr(Lscratch, BasicObjectLock::obj_offset_in_bytes(), O4);
3802 
3803     call_VM(noreg, CAST_FROM_FN_PTR(address, InterpreterRuntime::throw_illegal_monitor_state_exception));
3804     __ should_not_reach_here();
3805 
3806     __ bind(found);
3807   }
3808   __ unlock_object(O1);
3809 }
3810 
3811 
3812 //----------------------------------------------------------------------------------------------------
3813 // Wide instructions
3814 
3815 void TemplateTable::wide() {
3816   transition(vtos, vtos);
3817   __ ldub(Lbcp, 1, G3_scratch);// get next bc
3818   __ sll(G3_scratch, LogBytesPerWord, G3_scratch);
3819   AddressLiteral ep(Interpreter::_wentry_point);
3820   __ set(ep, G4_scratch);
3821   __ ld_ptr(G4_scratch, G3_scratch, G3_scratch);
3822   __ jmp(G3_scratch, G0);
3823   __ delayed()->nop();
3824   // Note: the Lbcp increment step is part of the individual wide bytecode implementations
3825 }
3826 
3827 
3828 //----------------------------------------------------------------------------------------------------
3829 // Multi arrays
3830 
3831 void TemplateTable::multianewarray() {
3832   transition(vtos, atos);
3833      // put ndims * wordSize into Lscratch
3834   __ ldub( Lbcp,     3,               Lscratch);
3835   __ sll(  Lscratch, Interpreter::logStackElementSize, Lscratch);
3836      // Lesp points past last_dim, so set to O1 to first_dim address
3837   __ add(  Lesp,     Lscratch,        O1);
3838      call_VM(Otos_i, CAST_FROM_FN_PTR(address, InterpreterRuntime::multianewarray), O1);
3839   __ add(  Lesp,     Lscratch,        Lesp); // pop all dimensions off the stack
3840 }