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