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