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_and_br_short(Rscratch, Bytecodes::_breakpoint, Assembler::notEqual, Assembler::pt, fast_patch);


 163     // perform the quickening, slowly, in the bowels of the breakpoint table
 164     __ call_VM(noreg, CAST_FROM_FN_PTR(address, InterpreterRuntime::set_original_bytecode_at), Lmethod, Lbcp, Rbyte_code);
 165     __ ba_short(patch_done);

 166     __ bind(fast_patch);
 167   }
 168 #ifdef ASSERT
 169   Bytecodes::Code orig_bytecode =  Bytecodes::java_code(bc);
 170   Label okay;
 171   __ ldub(at_bcp(0), Rscratch);
 172   __ cmp(Rscratch, orig_bytecode);
 173   __ br(Assembler::equal, false, Assembler::pt, okay);
 174   __ delayed() ->cmp(Rscratch, Rbyte_code);
 175   __ br(Assembler::equal, false, Assembler::pt, okay);
 176   __ delayed()->nop();
 177   __ stop("Rewriting wrong bytecode location");
 178   __ bind(okay);
 179 #endif
 180   __ stb(Rbyte_code, at_bcp(0));
 181   __ bind(patch_done);
 182 }
 183 
 184 //----------------------------------------------------------------------------------------------------
 185 // Individual instructions
 186 
 187 void TemplateTable::nop() {
 188   transition(vtos, vtos);
 189   // nothing to do
 190 }
 191 
 192 void TemplateTable::shouldnotreachhere() {
 193   transition(vtos, vtos);
 194   __ stop("shouldnotreachhere bytecode");
 195 }
 196 
 197 void TemplateTable::aconst_null() {
 198   transition(vtos, atos);
 199   __ clr(Otos_i);
 200 }
 201 
 202 
 203 void TemplateTable::iconst(int value) {
 204   transition(vtos, itos);
 205   __ set(value, Otos_i);
 206 }
 207 
 208 
 209 void TemplateTable::lconst(int value) {
 210   transition(vtos, ltos);
 211   assert(value >= 0, "check this code");
 212 #ifdef _LP64
 213   __ set(value, Otos_l);
 214 #else
 215   __ set(value, Otos_l2);
 216   __ clr( Otos_l1);
 217 #endif
 218 }
 219 
 220 
 221 void TemplateTable::fconst(int value) {
 222   transition(vtos, ftos);
 223   static float zero = 0.0, one = 1.0, two = 2.0;
 224   float* p;
 225   switch( value ) {
 226    default: ShouldNotReachHere();
 227    case 0:  p = &zero;  break;
 228    case 1:  p = &one;   break;
 229    case 2:  p = &two;   break;
 230   }
 231   AddressLiteral a(p);
 232   __ sethi(a, G3_scratch);
 233   __ ldf(FloatRegisterImpl::S, G3_scratch, a.low10(), Ftos_f);
 234 }
 235 
 236 
 237 void TemplateTable::dconst(int value) {
 238   transition(vtos, dtos);
 239   static double zero = 0.0, one = 1.0;
 240   double* p;
 241   switch( value ) {
 242    default: ShouldNotReachHere();
 243    case 0:  p = &zero;  break;
 244    case 1:  p = &one;   break;
 245   }
 246   AddressLiteral a(p);
 247   __ sethi(a, G3_scratch);
 248   __ ldf(FloatRegisterImpl::D, G3_scratch, a.low10(), Ftos_d);
 249 }
 250 
 251 
 252 // %%%%% Should factore most snippet templates across platforms
 253 
 254 void TemplateTable::bipush() {
 255   transition(vtos, itos);
 256   __ ldsb( at_bcp(1), Otos_i );
 257 }
 258 
 259 void TemplateTable::sipush() {
 260   transition(vtos, itos);
 261   __ get_2_byte_integer_at_bcp(1, G3_scratch, Otos_i, InterpreterMacroAssembler::Signed);
 262 }
 263 
 264 void TemplateTable::ldc(bool wide) {
 265   transition(vtos, vtos);
 266   Label call_ldc, notInt, isString, notString, notClass, exit;
 267 
 268   if (wide) {
 269     __ get_2_byte_integer_at_bcp(1, G3_scratch, O1, InterpreterMacroAssembler::Unsigned);
 270   } else {
 271     __ ldub(Lbcp, 1, O1);
 272   }
 273   __ get_cpool_and_tags(O0, O2);
 274 
 275   const int base_offset = constantPoolOopDesc::header_size() * wordSize;
 276   const int tags_offset = typeArrayOopDesc::header_size(T_BYTE) * wordSize;
 277 
 278   // get type from tags
 279   __ add(O2, tags_offset, O2);
 280   __ ldub(O2, O1, O2);
 281   // unresolved string? If so, must resolve
 282   __ cmp_and_brx_short(O2, JVM_CONSTANT_UnresolvedString, Assembler::equal, Assembler::pt, call_ldc);

 283 
 284   // unresolved class? If so, must resolve
 285   __ cmp_and_brx_short(O2, JVM_CONSTANT_UnresolvedClass, Assembler::equal, Assembler::pt, call_ldc);

 286 
 287   // unresolved class in error state
 288   __ cmp_and_brx_short(O2, JVM_CONSTANT_UnresolvedClassInError, Assembler::equal, Assembler::pn, call_ldc);

 289 
 290   __ cmp(O2, JVM_CONSTANT_Class);      // need to call vm to get java mirror of the class
 291   __ brx(Assembler::notEqual, true, Assembler::pt, notClass);
 292   __ delayed()->add(O0, base_offset, O0);
 293 
 294   __ bind(call_ldc);
 295   __ set(wide, O1);
 296   call_VM(Otos_i, CAST_FROM_FN_PTR(address, InterpreterRuntime::ldc), O1);
 297   __ push(atos);
 298   __ ba_short(exit);

 299 
 300   __ bind(notClass);
 301  // __ add(O0, base_offset, O0);
 302   __ sll(O1, LogBytesPerWord, O1);
 303   __ cmp(O2, JVM_CONSTANT_Integer);
 304   __ brx(Assembler::notEqual, true, Assembler::pt, notInt);
 305   __ delayed()->cmp(O2, JVM_CONSTANT_String);
 306   __ ld(O0, O1, Otos_i);
 307   __ push(itos);
 308   __ ba_short(exit);

 309 
 310   __ bind(notInt);
 311  // __ cmp(O2, JVM_CONSTANT_String);
 312   __ brx(Assembler::equal, true, Assembler::pt, isString);
 313   __ delayed()->cmp(O2, JVM_CONSTANT_Object);
 314   __ brx(Assembler::notEqual, true, Assembler::pt, notString);
 315   __ delayed()->ldf(FloatRegisterImpl::S, O0, O1, Ftos_f);
 316   __ bind(isString);
 317   __ ld_ptr(O0, O1, Otos_i);
 318   __ verify_oop(Otos_i);
 319   __ push(atos);
 320   __ ba_short(exit);

 321 
 322   __ bind(notString);
 323  // __ ldf(FloatRegisterImpl::S, O0, O1, Ftos_f);
 324   __ push(ftos);
 325 
 326   __ bind(exit);
 327 }
 328 
 329 // Fast path for caching oop constants.
 330 // %%% We should use this to handle Class and String constants also.
 331 // %%% It will simplify the ldc/primitive path considerably.
 332 void TemplateTable::fast_aldc(bool wide) {
 333   transition(vtos, atos);
 334 
 335   if (!EnableInvokeDynamic) {
 336     // We should not encounter this bytecode if !EnableInvokeDynamic.
 337     // The verifier will stop it.  However, if we get past the verifier,
 338     // this will stop the thread in a reasonable way, without crashing the JVM.
 339     __ call_VM(noreg, CAST_FROM_FN_PTR(address,
 340                      InterpreterRuntime::throw_IncompatibleClassChangeError));
 341     // the call_VM checks for exception, so we should never return here.
 342     __ should_not_reach_here();
 343     return;
 344   }
 345 
 346   Register Rcache = G3_scratch;
 347   Register Rscratch = G4_scratch;
 348 
 349   resolve_cache_and_index(f1_oop, Otos_i, Rcache, Rscratch, wide ? sizeof(u2) : sizeof(u1));
 350 
 351   __ verify_oop(Otos_i);
 352 
 353   Label L_done;
 354   const Register Rcon_klass = G3_scratch;  // same as Rcache
 355   const Register Rarray_klass = G4_scratch;  // same as Rscratch
 356   __ load_klass(Otos_i, Rcon_klass);
 357   AddressLiteral array_klass_addr((address)Universe::systemObjArrayKlassObj_addr());
 358   __ load_contents(array_klass_addr, Rarray_klass);
 359   __ cmp_and_brx_short(Rarray_klass, Rcon_klass, Assembler::notEqual, Assembler::pt, L_done);


 360   __ ld(Address(Otos_i, arrayOopDesc::length_offset_in_bytes()), Rcon_klass);
 361   __ tst(Rcon_klass);
 362   __ brx(Assembler::zero, true, Assembler::pt, L_done);
 363   __ delayed()->clr(Otos_i);    // executed only if branch is taken
 364 
 365   // Load the exception from the system-array which wraps it:
 366   __ load_heap_oop(Otos_i, arrayOopDesc::base_offset_in_bytes(T_OBJECT), Otos_i);
 367   __ throw_if_not_x(Assembler::never, Interpreter::throw_exception_entry(), G3_scratch);
 368 
 369   __ bind(L_done);
 370 }
 371 
 372 void TemplateTable::ldc2_w() {
 373   transition(vtos, vtos);
 374   Label retry, resolved, Long, exit;
 375 
 376   __ bind(retry);
 377   __ get_2_byte_integer_at_bcp(1, G3_scratch, O1, InterpreterMacroAssembler::Unsigned);
 378   __ get_cpool_and_tags(O0, O2);
 379 
 380   const int base_offset = constantPoolOopDesc::header_size() * wordSize;
 381   const int tags_offset = typeArrayOopDesc::header_size(T_BYTE) * wordSize;
 382   // get type from tags
 383   __ add(O2, tags_offset, O2);
 384   __ ldub(O2, O1, O2);
 385 
 386   __ sll(O1, LogBytesPerWord, O1);
 387   __ add(O0, O1, G3_scratch);
 388 
 389   __ cmp_and_brx_short(O2, JVM_CONSTANT_Double, Assembler::notEqual, Assembler::pt, Long);


 390   // A double can be placed at word-aligned locations in the constant pool.
 391   // Check out Conversions.java for an example.
 392   // Also constantPoolOopDesc::header_size() is 20, which makes it very difficult
 393   // to double-align double on the constant pool.  SG, 11/7/97
 394 #ifdef _LP64
 395   __ ldf(FloatRegisterImpl::D, G3_scratch, base_offset, Ftos_d);
 396 #else
 397   FloatRegister f = Ftos_d;
 398   __ ldf(FloatRegisterImpl::S, G3_scratch, base_offset, f);
 399   __ ldf(FloatRegisterImpl::S, G3_scratch, base_offset + sizeof(jdouble)/2,
 400          f->successor());
 401 #endif
 402   __ push(dtos);
 403   __ ba_short(exit);

 404 
 405   __ bind(Long);
 406 #ifdef _LP64
 407   __ ldx(G3_scratch, base_offset, Otos_l);
 408 #else
 409   __ ld(G3_scratch, base_offset, Otos_l);
 410   __ ld(G3_scratch, base_offset + sizeof(jlong)/2, Otos_l->successor());
 411 #endif
 412   __ push(ltos);
 413 
 414   __ bind(exit);
 415 }
 416 
 417 
 418 void TemplateTable::locals_index(Register reg, int offset) {
 419   __ ldub( at_bcp(offset), reg );
 420 }
 421 
 422 
 423 void TemplateTable::locals_index_wide(Register reg) {
 424   // offset is 2, not 1, because Lbcp points to wide prefix code
 425   __ get_2_byte_integer_at_bcp(2, G4_scratch, reg, InterpreterMacroAssembler::Unsigned);
 426 }
 427 
 428 void TemplateTable::iload() {
 429   transition(vtos, itos);
 430   // Rewrite iload,iload  pair into fast_iload2
 431   //         iload,caload pair into fast_icaload
 432   if (RewriteFrequentPairs) {
 433     Label rewrite, done;
 434 
 435     // get next byte
 436     __ ldub(at_bcp(Bytecodes::length_for(Bytecodes::_iload)), G3_scratch);
 437 
 438     // if _iload, wait to rewrite to iload2.  We only want to rewrite the
 439     // last two iloads in a pair.  Comparing against fast_iload means that
 440     // the next bytecode is neither an iload or a caload, and therefore
 441     // an iload pair.
 442     __ cmp_and_br_short(G3_scratch, (int)Bytecodes::_iload, Assembler::equal, Assembler::pn, done);


 443 
 444     __ cmp(G3_scratch, (int)Bytecodes::_fast_iload);
 445     __ br(Assembler::equal, false, Assembler::pn, rewrite);
 446     __ delayed()->set(Bytecodes::_fast_iload2, G4_scratch);
 447 
 448     __ cmp(G3_scratch, (int)Bytecodes::_caload);
 449     __ br(Assembler::equal, false, Assembler::pn, rewrite);
 450     __ delayed()->set(Bytecodes::_fast_icaload, G4_scratch);
 451 
 452     __ set(Bytecodes::_fast_iload, G4_scratch);  // don't check again
 453     // rewrite
 454     // G4_scratch: fast bytecode
 455     __ bind(rewrite);
 456     patch_bytecode(Bytecodes::_iload, G4_scratch, G3_scratch, false);
 457     __ bind(done);
 458   }
 459 
 460   // Get the local value into tos
 461   locals_index(G3_scratch);
 462   __ access_local_int( G3_scratch, Otos_i );
 463 }
 464 
 465 void TemplateTable::fast_iload2() {
 466   transition(vtos, itos);
 467   locals_index(G3_scratch);
 468   __ access_local_int( G3_scratch, Otos_i );
 469   __ push_i();
 470   locals_index(G3_scratch, 3);  // get next bytecode's local index.
 471   __ access_local_int( G3_scratch, Otos_i );
 472 }
 473 
 474 void TemplateTable::fast_iload() {
 475   transition(vtos, itos);
 476   locals_index(G3_scratch);
 477   __ access_local_int( G3_scratch, Otos_i );
 478 }
 479 
 480 void TemplateTable::lload() {
 481   transition(vtos, ltos);
 482   locals_index(G3_scratch);
 483   __ access_local_long( G3_scratch, Otos_l );
 484 }
 485 
 486 
 487 void TemplateTable::fload() {
 488   transition(vtos, ftos);
 489   locals_index(G3_scratch);
 490   __ access_local_float( G3_scratch, Ftos_f );
 491 }
 492 
 493 
 494 void TemplateTable::dload() {
 495   transition(vtos, dtos);
 496   locals_index(G3_scratch);
 497   __ access_local_double( G3_scratch, Ftos_d );
 498 }
 499 
 500 
 501 void TemplateTable::aload() {
 502   transition(vtos, atos);
 503   locals_index(G3_scratch);
 504   __ access_local_ptr( G3_scratch, Otos_i);
 505 }
 506 
 507 
 508 void TemplateTable::wide_iload() {
 509   transition(vtos, itos);
 510   locals_index_wide(G3_scratch);
 511   __ access_local_int( G3_scratch, Otos_i );
 512 }
 513 
 514 
 515 void TemplateTable::wide_lload() {
 516   transition(vtos, ltos);
 517   locals_index_wide(G3_scratch);
 518   __ access_local_long( G3_scratch, Otos_l );
 519 }
 520 
 521 
 522 void TemplateTable::wide_fload() {
 523   transition(vtos, ftos);
 524   locals_index_wide(G3_scratch);
 525   __ access_local_float( G3_scratch, Ftos_f );
 526 }
 527 
 528 
 529 void TemplateTable::wide_dload() {
 530   transition(vtos, dtos);
 531   locals_index_wide(G3_scratch);
 532   __ access_local_double( G3_scratch, Ftos_d );
 533 }
 534 
 535 
 536 void TemplateTable::wide_aload() {
 537   transition(vtos, atos);
 538   locals_index_wide(G3_scratch);
 539   __ access_local_ptr( G3_scratch, Otos_i );
 540   __ verify_oop(Otos_i);
 541 }
 542 
 543 
 544 void TemplateTable::iaload() {
 545   transition(itos, itos);
 546   // Otos_i: index
 547   // tos: array
 548   __ index_check(O2, Otos_i, LogBytesPerInt, G3_scratch, O3);
 549   __ ld(O3, arrayOopDesc::base_offset_in_bytes(T_INT), Otos_i);
 550 }
 551 
 552 
 553 void TemplateTable::laload() {
 554   transition(itos, ltos);
 555   // Otos_i: index
 556   // O2: array
 557   __ index_check(O2, Otos_i, LogBytesPerLong, G3_scratch, O3);
 558   __ ld_long(O3, arrayOopDesc::base_offset_in_bytes(T_LONG), Otos_l);
 559 }
 560 
 561 
 562 void TemplateTable::faload() {
 563   transition(itos, ftos);
 564   // Otos_i: index
 565   // O2: array
 566   __ index_check(O2, Otos_i, LogBytesPerInt, G3_scratch, O3);
 567   __ ldf(FloatRegisterImpl::S, O3, arrayOopDesc::base_offset_in_bytes(T_FLOAT), Ftos_f);
 568 }
 569 
 570 
 571 void TemplateTable::daload() {
 572   transition(itos, dtos);
 573   // Otos_i: index
 574   // O2: array
 575   __ index_check(O2, Otos_i, LogBytesPerLong, G3_scratch, O3);
 576   __ ldf(FloatRegisterImpl::D, O3, arrayOopDesc::base_offset_in_bytes(T_DOUBLE), Ftos_d);
 577 }
 578 
 579 
 580 void TemplateTable::aaload() {
 581   transition(itos, atos);
 582   // Otos_i: index
 583   // tos: array
 584   __ index_check(O2, Otos_i, UseCompressedOops ? 2 : LogBytesPerWord, G3_scratch, O3);
 585   __ load_heap_oop(O3, arrayOopDesc::base_offset_in_bytes(T_OBJECT), Otos_i);
 586   __ verify_oop(Otos_i);
 587 }
 588 
 589 
 590 void TemplateTable::baload() {
 591   transition(itos, itos);
 592   // Otos_i: index
 593   // tos: array
 594   __ index_check(O2, Otos_i, 0, G3_scratch, O3);
 595   __ ldsb(O3, arrayOopDesc::base_offset_in_bytes(T_BYTE), Otos_i);
 596 }
 597 
 598 
 599 void TemplateTable::caload() {
 600   transition(itos, itos);
 601   // Otos_i: index
 602   // tos: array
 603   __ index_check(O2, Otos_i, LogBytesPerShort, G3_scratch, O3);
 604   __ lduh(O3, arrayOopDesc::base_offset_in_bytes(T_CHAR), Otos_i);
 605 }
 606 
 607 void TemplateTable::fast_icaload() {
 608   transition(vtos, itos);
 609   // Otos_i: index
 610   // tos: array
 611   locals_index(G3_scratch);
 612   __ access_local_int( G3_scratch, Otos_i );
 613   __ index_check(O2, Otos_i, LogBytesPerShort, G3_scratch, O3);
 614   __ lduh(O3, arrayOopDesc::base_offset_in_bytes(T_CHAR), Otos_i);
 615 }
 616 
 617 
 618 void TemplateTable::saload() {
 619   transition(itos, itos);
 620   // Otos_i: index
 621   // tos: array
 622   __ index_check(O2, Otos_i, LogBytesPerShort, G3_scratch, O3);
 623   __ ldsh(O3, arrayOopDesc::base_offset_in_bytes(T_SHORT), Otos_i);
 624 }
 625 
 626 
 627 void TemplateTable::iload(int n) {
 628   transition(vtos, itos);
 629   __ ld( Llocals, Interpreter::local_offset_in_bytes(n), Otos_i );
 630 }
 631 
 632 
 633 void TemplateTable::lload(int n) {
 634   transition(vtos, ltos);
 635   assert(n+1 < Argument::n_register_parameters, "would need more code");
 636   __ load_unaligned_long(Llocals, Interpreter::local_offset_in_bytes(n+1), Otos_l);
 637 }
 638 
 639 
 640 void TemplateTable::fload(int n) {
 641   transition(vtos, ftos);
 642   assert(n < Argument::n_register_parameters, "would need more code");
 643   __ ldf( FloatRegisterImpl::S, Llocals, Interpreter::local_offset_in_bytes(n),     Ftos_f );
 644 }
 645 
 646 
 647 void TemplateTable::dload(int n) {
 648   transition(vtos, dtos);
 649   FloatRegister dst = Ftos_d;
 650   __ load_unaligned_double(Llocals, Interpreter::local_offset_in_bytes(n+1), dst);
 651 }
 652 
 653 
 654 void TemplateTable::aload(int n) {
 655   transition(vtos, atos);
 656   __ ld_ptr( Llocals, Interpreter::local_offset_in_bytes(n), Otos_i );
 657 }
 658 
 659 
 660 void TemplateTable::aload_0() {
 661   transition(vtos, atos);
 662 
 663   // According to bytecode histograms, the pairs:
 664   //
 665   // _aload_0, _fast_igetfield (itos)
 666   // _aload_0, _fast_agetfield (atos)
 667   // _aload_0, _fast_fgetfield (ftos)
 668   //
 669   // occur frequently. If RewriteFrequentPairs is set, the (slow) _aload_0
 670   // bytecode checks the next bytecode and then rewrites the current
 671   // bytecode into a pair bytecode; otherwise it rewrites the current
 672   // bytecode into _fast_aload_0 that doesn't do the pair check anymore.
 673   //
 674   if (RewriteFrequentPairs) {
 675     Label rewrite, done;
 676 
 677     // get next byte
 678     __ ldub(at_bcp(Bytecodes::length_for(Bytecodes::_aload_0)), G3_scratch);
 679 
 680     // do actual aload_0
 681     aload(0);
 682 
 683     // if _getfield then wait with rewrite
 684     __ cmp_and_br_short(G3_scratch, (int)Bytecodes::_getfield, Assembler::equal, Assembler::pn, done);


 685 
 686     // if _igetfield then rewrite to _fast_iaccess_0
 687     assert(Bytecodes::java_code(Bytecodes::_fast_iaccess_0) == Bytecodes::_aload_0, "adjust fast bytecode def");
 688     __ cmp(G3_scratch, (int)Bytecodes::_fast_igetfield);
 689     __ br(Assembler::equal, false, Assembler::pn, rewrite);
 690     __ delayed()->set(Bytecodes::_fast_iaccess_0, G4_scratch);
 691 
 692     // if _agetfield then rewrite to _fast_aaccess_0
 693     assert(Bytecodes::java_code(Bytecodes::_fast_aaccess_0) == Bytecodes::_aload_0, "adjust fast bytecode def");
 694     __ cmp(G3_scratch, (int)Bytecodes::_fast_agetfield);
 695     __ br(Assembler::equal, false, Assembler::pn, rewrite);
 696     __ delayed()->set(Bytecodes::_fast_aaccess_0, G4_scratch);
 697 
 698     // if _fgetfield then rewrite to _fast_faccess_0
 699     assert(Bytecodes::java_code(Bytecodes::_fast_faccess_0) == Bytecodes::_aload_0, "adjust fast bytecode def");
 700     __ cmp(G3_scratch, (int)Bytecodes::_fast_fgetfield);
 701     __ br(Assembler::equal, false, Assembler::pn, rewrite);
 702     __ delayed()->set(Bytecodes::_fast_faccess_0, G4_scratch);
 703 
 704     // else rewrite to _fast_aload0
 705     assert(Bytecodes::java_code(Bytecodes::_fast_aload_0) == Bytecodes::_aload_0, "adjust fast bytecode def");
 706     __ set(Bytecodes::_fast_aload_0, G4_scratch);
 707 
 708     // rewrite
 709     // G4_scratch: fast bytecode
 710     __ bind(rewrite);
 711     patch_bytecode(Bytecodes::_aload_0, G4_scratch, G3_scratch, false);
 712     __ bind(done);
 713   } else {
 714     aload(0);
 715   }
 716 }
 717 
 718 
 719 void TemplateTable::istore() {
 720   transition(itos, vtos);
 721   locals_index(G3_scratch);
 722   __ store_local_int( G3_scratch, Otos_i );
 723 }
 724 
 725 
 726 void TemplateTable::lstore() {
 727   transition(ltos, vtos);
 728   locals_index(G3_scratch);
 729   __ store_local_long( G3_scratch, Otos_l );
 730 }
 731 
 732 
 733 void TemplateTable::fstore() {
 734   transition(ftos, vtos);
 735   locals_index(G3_scratch);
 736   __ store_local_float( G3_scratch, Ftos_f );
 737 }
 738 
 739 
 740 void TemplateTable::dstore() {
 741   transition(dtos, vtos);
 742   locals_index(G3_scratch);
 743   __ store_local_double( G3_scratch, Ftos_d );
 744 }
 745 
 746 
 747 void TemplateTable::astore() {
 748   transition(vtos, vtos);
 749   __ load_ptr(0, Otos_i);
 750   __ inc(Lesp, Interpreter::stackElementSize);
 751   __ verify_oop_or_return_address(Otos_i, G3_scratch);
 752   locals_index(G3_scratch);
 753   __ store_local_ptr(G3_scratch, Otos_i);
 754 }
 755 
 756 
 757 void TemplateTable::wide_istore() {
 758   transition(vtos, vtos);
 759   __ pop_i();
 760   locals_index_wide(G3_scratch);
 761   __ store_local_int( G3_scratch, Otos_i );
 762 }
 763 
 764 
 765 void TemplateTable::wide_lstore() {
 766   transition(vtos, vtos);
 767   __ pop_l();
 768   locals_index_wide(G3_scratch);
 769   __ store_local_long( G3_scratch, Otos_l );
 770 }
 771 
 772 
 773 void TemplateTable::wide_fstore() {
 774   transition(vtos, vtos);
 775   __ pop_f();
 776   locals_index_wide(G3_scratch);
 777   __ store_local_float( G3_scratch, Ftos_f );
 778 }
 779 
 780 
 781 void TemplateTable::wide_dstore() {
 782   transition(vtos, vtos);
 783   __ pop_d();
 784   locals_index_wide(G3_scratch);
 785   __ store_local_double( G3_scratch, Ftos_d );
 786 }
 787 
 788 
 789 void TemplateTable::wide_astore() {
 790   transition(vtos, vtos);
 791   __ load_ptr(0, Otos_i);
 792   __ inc(Lesp, Interpreter::stackElementSize);
 793   __ verify_oop_or_return_address(Otos_i, G3_scratch);
 794   locals_index_wide(G3_scratch);
 795   __ store_local_ptr(G3_scratch, Otos_i);
 796 }
 797 
 798 
 799 void TemplateTable::iastore() {
 800   transition(itos, vtos);
 801   __ pop_i(O2); // index
 802   // Otos_i: val
 803   // O3: array
 804   __ index_check(O3, O2, LogBytesPerInt, G3_scratch, O2);
 805   __ st(Otos_i, O2, arrayOopDesc::base_offset_in_bytes(T_INT));
 806 }
 807 
 808 
 809 void TemplateTable::lastore() {
 810   transition(ltos, vtos);
 811   __ pop_i(O2); // index
 812   // Otos_l: val
 813   // O3: array
 814   __ index_check(O3, O2, LogBytesPerLong, G3_scratch, O2);
 815   __ st_long(Otos_l, O2, arrayOopDesc::base_offset_in_bytes(T_LONG));
 816 }
 817 
 818 
 819 void TemplateTable::fastore() {
 820   transition(ftos, vtos);
 821   __ pop_i(O2); // index
 822   // Ftos_f: val
 823   // O3: array
 824   __ index_check(O3, O2, LogBytesPerInt, G3_scratch, O2);
 825   __ stf(FloatRegisterImpl::S, Ftos_f, O2, arrayOopDesc::base_offset_in_bytes(T_FLOAT));
 826 }
 827 
 828 
 829 void TemplateTable::dastore() {
 830   transition(dtos, vtos);
 831   __ pop_i(O2); // index
 832   // Fos_d: val
 833   // O3: array
 834   __ index_check(O3, O2, LogBytesPerLong, G3_scratch, O2);
 835   __ stf(FloatRegisterImpl::D, Ftos_d, O2, arrayOopDesc::base_offset_in_bytes(T_DOUBLE));
 836 }
 837 
 838 
 839 void TemplateTable::aastore() {
 840   Label store_ok, is_null, done;
 841   transition(vtos, vtos);
 842   __ ld_ptr(Lesp, Interpreter::expr_offset_in_bytes(0), Otos_i);
 843   __ ld(Lesp, Interpreter::expr_offset_in_bytes(1), O2);         // get index
 844   __ ld_ptr(Lesp, Interpreter::expr_offset_in_bytes(2), O3);     // get array
 845   // Otos_i: val
 846   // O2: index
 847   // O3: array
 848   __ verify_oop(Otos_i);
 849   __ index_check_without_pop(O3, O2, UseCompressedOops ? 2 : LogBytesPerWord, G3_scratch, O1);
 850 
 851   // do array store check - check for NULL value first
 852   __ br_null_short( Otos_i, Assembler::pn, is_null );

 853 
 854   __ load_klass(O3, O4); // get array klass
 855   __ load_klass(Otos_i, O5); // get value klass
 856 
 857   // do fast instanceof cache test
 858 
 859   __ ld_ptr(O4,     sizeof(oopDesc) + objArrayKlass::element_klass_offset_in_bytes(),  O4);
 860 
 861   assert(Otos_i == O0, "just checking");
 862 
 863   // Otos_i:    value
 864   // O1:        addr - offset
 865   // O2:        index
 866   // O3:        array
 867   // O4:        array element klass
 868   // O5:        value klass
 869 
 870   // Address element(O1, 0, arrayOopDesc::base_offset_in_bytes(T_OBJECT));
 871 
 872   // Generate a fast subtype check.  Branch to store_ok if no
 873   // failure.  Throw if failure.
 874   __ gen_subtype_check( O5, O4, G3_scratch, G4_scratch, G1_scratch, store_ok );
 875 
 876   // Not a subtype; so must throw exception
 877   __ throw_if_not_x( Assembler::never, Interpreter::_throw_ArrayStoreException_entry, G3_scratch );
 878 
 879   // Store is OK.
 880   __ bind(store_ok);
 881   do_oop_store(_masm, O1, noreg, arrayOopDesc::base_offset_in_bytes(T_OBJECT), Otos_i, G3_scratch, _bs->kind(), true);
 882 
 883   __ ba(done);
 884   __ delayed()->inc(Lesp, 3* Interpreter::stackElementSize); // adj sp (pops array, index and value)
 885 
 886   __ bind(is_null);
 887   do_oop_store(_masm, O1, noreg, arrayOopDesc::base_offset_in_bytes(T_OBJECT), G0, G4_scratch, _bs->kind(), true);
 888 
 889   __ profile_null_seen(G3_scratch);
 890   __ inc(Lesp, 3* Interpreter::stackElementSize);     // adj sp (pops array, index and value)
 891   __ bind(done);
 892 }
 893 
 894 
 895 void TemplateTable::bastore() {
 896   transition(itos, vtos);
 897   __ pop_i(O2); // index
 898   // Otos_i: val
 899   // O3: array
 900   __ index_check(O3, O2, 0, G3_scratch, O2);
 901   __ stb(Otos_i, O2, arrayOopDesc::base_offset_in_bytes(T_BYTE));
 902 }
 903 
 904 
 905 void TemplateTable::castore() {
 906   transition(itos, vtos);
 907   __ pop_i(O2); // index
 908   // Otos_i: val
 909   // O3: array
 910   __ index_check(O3, O2, LogBytesPerShort, G3_scratch, O2);
 911   __ sth(Otos_i, O2, arrayOopDesc::base_offset_in_bytes(T_CHAR));
 912 }
 913 
 914 
 915 void TemplateTable::sastore() {
 916   // %%%%% Factor across platform
 917   castore();
 918 }
 919 
 920 
 921 void TemplateTable::istore(int n) {
 922   transition(itos, vtos);
 923   __ st(Otos_i, Llocals, Interpreter::local_offset_in_bytes(n));
 924 }
 925 
 926 
 927 void TemplateTable::lstore(int n) {
 928   transition(ltos, vtos);
 929   assert(n+1 < Argument::n_register_parameters, "only handle register cases");
 930   __ store_unaligned_long(Otos_l, Llocals, Interpreter::local_offset_in_bytes(n+1));
 931 
 932 }
 933 
 934 
 935 void TemplateTable::fstore(int n) {
 936   transition(ftos, vtos);
 937   assert(n < Argument::n_register_parameters, "only handle register cases");
 938   __ stf(FloatRegisterImpl::S, Ftos_f, Llocals, Interpreter::local_offset_in_bytes(n));
 939 }
 940 
 941 
 942 void TemplateTable::dstore(int n) {
 943   transition(dtos, vtos);
 944   FloatRegister src = Ftos_d;
 945   __ store_unaligned_double(src, Llocals, Interpreter::local_offset_in_bytes(n+1));
 946 }
 947 
 948 
 949 void TemplateTable::astore(int n) {
 950   transition(vtos, vtos);
 951   __ load_ptr(0, Otos_i);
 952   __ inc(Lesp, Interpreter::stackElementSize);
 953   __ verify_oop_or_return_address(Otos_i, G3_scratch);
 954   __ store_local_ptr(n, Otos_i);
 955 }
 956 
 957 
 958 void TemplateTable::pop() {
 959   transition(vtos, vtos);
 960   __ inc(Lesp, Interpreter::stackElementSize);
 961 }
 962 
 963 
 964 void TemplateTable::pop2() {
 965   transition(vtos, vtos);
 966   __ inc(Lesp, 2 * Interpreter::stackElementSize);
 967 }
 968 
 969 
 970 void TemplateTable::dup() {
 971   transition(vtos, vtos);
 972   // stack: ..., a
 973   // load a and tag
 974   __ load_ptr(0, Otos_i);
 975   __ push_ptr(Otos_i);
 976   // stack: ..., a, a
 977 }
 978 
 979 
 980 void TemplateTable::dup_x1() {
 981   transition(vtos, vtos);
 982   // stack: ..., a, b
 983   __ load_ptr( 1, G3_scratch);  // get a
 984   __ load_ptr( 0, Otos_l1);     // get b
 985   __ store_ptr(1, Otos_l1);     // put b
 986   __ store_ptr(0, G3_scratch);  // put a - like swap
 987   __ push_ptr(Otos_l1);         // push b
 988   // stack: ..., b, a, b
 989 }
 990 
 991 
 992 void TemplateTable::dup_x2() {
 993   transition(vtos, vtos);
 994   // stack: ..., a, b, c
 995   // get c and push on stack, reuse registers
 996   __ load_ptr( 0, G3_scratch);  // get c
 997   __ push_ptr(G3_scratch);      // push c with tag
 998   // stack: ..., a, b, c, c  (c in reg)  (Lesp - 4)
 999   // (stack offsets n+1 now)
1000   __ load_ptr( 3, Otos_l1);     // get a
1001   __ store_ptr(3, G3_scratch);  // put c at 3
1002   // stack: ..., c, b, c, c  (a in reg)
1003   __ load_ptr( 2, G3_scratch);  // get b
1004   __ store_ptr(2, Otos_l1);     // put a at 2
1005   // stack: ..., c, a, c, c  (b in reg)
1006   __ store_ptr(1, G3_scratch);  // put b at 1
1007   // stack: ..., c, a, b, c
1008 }
1009 
1010 
1011 void TemplateTable::dup2() {
1012   transition(vtos, vtos);
1013   __ load_ptr(1, G3_scratch);  // get a
1014   __ load_ptr(0, Otos_l1);     // get b
1015   __ push_ptr(G3_scratch);     // push a
1016   __ push_ptr(Otos_l1);        // push b
1017   // stack: ..., a, b, a, b
1018 }
1019 
1020 
1021 void TemplateTable::dup2_x1() {
1022   transition(vtos, vtos);
1023   // stack: ..., a, b, c
1024   __ load_ptr( 1, Lscratch);    // get b
1025   __ load_ptr( 2, Otos_l1);     // get a
1026   __ store_ptr(2, Lscratch);    // put b at a
1027   // stack: ..., b, b, c
1028   __ load_ptr( 0, G3_scratch);  // get c
1029   __ store_ptr(1, G3_scratch);  // put c at b
1030   // stack: ..., b, c, c
1031   __ store_ptr(0, Otos_l1);     // put a at c
1032   // stack: ..., b, c, a
1033   __ push_ptr(Lscratch);        // push b
1034   __ push_ptr(G3_scratch);      // push c
1035   // stack: ..., b, c, a, b, c
1036 }
1037 
1038 
1039 // The spec says that these types can be a mixture of category 1 (1 word)
1040 // types and/or category 2 types (long and doubles)
1041 void TemplateTable::dup2_x2() {
1042   transition(vtos, vtos);
1043   // stack: ..., a, b, c, d
1044   __ load_ptr( 1, Lscratch);    // get c
1045   __ load_ptr( 3, Otos_l1);     // get a
1046   __ store_ptr(3, Lscratch);    // put c at 3
1047   __ store_ptr(1, Otos_l1);     // put a at 1
1048   // stack: ..., c, b, a, d
1049   __ load_ptr( 2, G3_scratch);  // get b
1050   __ load_ptr( 0, Otos_l1);     // get d
1051   __ store_ptr(0, G3_scratch);  // put b at 0
1052   __ store_ptr(2, Otos_l1);     // put d at 2
1053   // stack: ..., c, d, a, b
1054   __ push_ptr(Lscratch);        // push c
1055   __ push_ptr(Otos_l1);         // push d
1056   // stack: ..., c, d, a, b, c, d
1057 }
1058 
1059 
1060 void TemplateTable::swap() {
1061   transition(vtos, vtos);
1062   // stack: ..., a, b
1063   __ load_ptr( 1, G3_scratch);  // get a
1064   __ load_ptr( 0, Otos_l1);     // get b
1065   __ store_ptr(0, G3_scratch);  // put b
1066   __ store_ptr(1, Otos_l1);     // put a
1067   // stack: ..., b, a
1068 }
1069 
1070 
1071 void TemplateTable::iop2(Operation op) {
1072   transition(itos, itos);
1073   __ pop_i(O1);
1074   switch (op) {
1075    case  add:  __  add(O1, Otos_i, Otos_i);  break;
1076    case  sub:  __  sub(O1, Otos_i, Otos_i);  break;
1077      // %%%%% Mul may not exist: better to call .mul?
1078    case  mul:  __ smul(O1, Otos_i, Otos_i);  break;
1079    case _and:  __ and3(O1, Otos_i, Otos_i);  break;
1080    case  _or:  __  or3(O1, Otos_i, Otos_i);  break;
1081    case _xor:  __ xor3(O1, Otos_i, Otos_i);  break;
1082    case  shl:  __  sll(O1, Otos_i, Otos_i);  break;
1083    case  shr:  __  sra(O1, Otos_i, Otos_i);  break;
1084    case ushr:  __  srl(O1, Otos_i, Otos_i);  break;
1085    default: ShouldNotReachHere();
1086   }
1087 }
1088 
1089 
1090 void TemplateTable::lop2(Operation op) {
1091   transition(ltos, ltos);
1092   __ pop_l(O2);
1093   switch (op) {
1094 #ifdef _LP64
1095    case  add:  __  add(O2, Otos_l, Otos_l);  break;
1096    case  sub:  __  sub(O2, Otos_l, Otos_l);  break;
1097    case _and:  __ and3(O2, Otos_l, Otos_l);  break;
1098    case  _or:  __  or3(O2, Otos_l, Otos_l);  break;
1099    case _xor:  __ xor3(O2, Otos_l, Otos_l);  break;
1100 #else
1101    case  add:  __ addcc(O3, Otos_l2, Otos_l2);  __ addc(O2, Otos_l1, Otos_l1);  break;
1102    case  sub:  __ subcc(O3, Otos_l2, Otos_l2);  __ subc(O2, Otos_l1, Otos_l1);  break;
1103    case _and:  __  and3(O3, Otos_l2, Otos_l2);  __ and3(O2, Otos_l1, Otos_l1);  break;
1104    case  _or:  __   or3(O3, Otos_l2, Otos_l2);  __  or3(O2, Otos_l1, Otos_l1);  break;
1105    case _xor:  __  xor3(O3, Otos_l2, Otos_l2);  __ xor3(O2, Otos_l1, Otos_l1);  break;
1106 #endif
1107    default: ShouldNotReachHere();
1108   }
1109 }
1110 
1111 
1112 void TemplateTable::idiv() {
1113   // %%%%% Later: ForSPARC/V7 call .sdiv library routine,
1114   // %%%%% Use ldsw...sdivx on pure V9 ABI. 64 bit safe.
1115 
1116   transition(itos, itos);
1117   __ pop_i(O1); // get 1st op
1118 
1119   // Y contains upper 32 bits of result, set it to 0 or all ones
1120   __ wry(G0);
1121   __ mov(~0, G3_scratch);
1122 
1123   __ tst(O1);
1124      Label neg;
1125   __ br(Assembler::negative, true, Assembler::pn, neg);
1126   __ delayed()->wry(G3_scratch);
1127   __ bind(neg);
1128 
1129      Label ok;
1130   __ tst(Otos_i);
1131   __ throw_if_not_icc( Assembler::notZero, Interpreter::_throw_ArithmeticException_entry, G3_scratch );
1132 
1133   const int min_int = 0x80000000;
1134   Label regular;
1135   __ cmp(Otos_i, -1);
1136   __ br(Assembler::notEqual, false, Assembler::pt, regular);
1137 #ifdef _LP64
1138   // Don't put set in delay slot
1139   // Set will turn into multiple instructions in 64 bit mode
1140   __ delayed()->nop();
1141   __ set(min_int, G4_scratch);
1142 #else
1143   __ delayed()->set(min_int, G4_scratch);
1144 #endif
1145   Label done;
1146   __ cmp(O1, G4_scratch);
1147   __ br(Assembler::equal, true, Assembler::pt, done);
1148   __ delayed()->mov(O1, Otos_i);   // (mov only executed if branch taken)
1149 
1150   __ bind(regular);
1151   __ sdiv(O1, Otos_i, Otos_i); // note: irem uses O1 after this instruction!
1152   __ bind(done);
1153 }
1154 
1155 
1156 void TemplateTable::irem() {
1157   transition(itos, itos);
1158   __ mov(Otos_i, O2); // save divisor
1159   idiv();                               // %%%% Hack: exploits fact that idiv leaves dividend in O1
1160   __ smul(Otos_i, O2, Otos_i);
1161   __ sub(O1, Otos_i, Otos_i);
1162 }
1163 
1164 
1165 void TemplateTable::lmul() {
1166   transition(ltos, ltos);
1167   __ pop_l(O2);
1168 #ifdef _LP64
1169   __ mulx(Otos_l, O2, Otos_l);
1170 #else
1171   __ call_VM_leaf(Lscratch, CAST_FROM_FN_PTR(address, SharedRuntime::lmul));
1172 #endif
1173 
1174 }
1175 
1176 
1177 void TemplateTable::ldiv() {
1178   transition(ltos, ltos);
1179 
1180   // check for zero
1181   __ pop_l(O2);
1182 #ifdef _LP64
1183   __ tst(Otos_l);
1184   __ throw_if_not_xcc( Assembler::notZero, Interpreter::_throw_ArithmeticException_entry, G3_scratch);
1185   __ sdivx(O2, Otos_l, Otos_l);
1186 #else
1187   __ orcc(Otos_l1, Otos_l2, G0);
1188   __ throw_if_not_icc( Assembler::notZero, Interpreter::_throw_ArithmeticException_entry, G3_scratch);
1189   __ call_VM_leaf(Lscratch, CAST_FROM_FN_PTR(address, SharedRuntime::ldiv));
1190 #endif
1191 }
1192 
1193 
1194 void TemplateTable::lrem() {
1195   transition(ltos, ltos);
1196 
1197   // check for zero
1198   __ pop_l(O2);
1199 #ifdef _LP64
1200   __ tst(Otos_l);
1201   __ throw_if_not_xcc( Assembler::notZero, Interpreter::_throw_ArithmeticException_entry, G3_scratch);
1202   __ sdivx(O2, Otos_l, Otos_l2);
1203   __ mulx (Otos_l2, Otos_l, Otos_l2);
1204   __ sub  (O2, Otos_l2, 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::lrem));
1209 #endif
1210 }
1211 
1212 
1213 void TemplateTable::lshl() {
1214   transition(itos, ltos); // %%%% could optimize, fill delay slot or opt for ultra
1215 
1216   __ pop_l(O2);                          // shift value in O2, O3
1217 #ifdef _LP64
1218   __ sllx(O2, Otos_i, Otos_l);
1219 #else
1220   __ lshl(O2, O3, Otos_i, Otos_l1, Otos_l2, O4);
1221 #endif
1222 }
1223 
1224 
1225 void TemplateTable::lshr() {
1226   transition(itos, ltos); // %%%% see lshl comment
1227 
1228   __ pop_l(O2);                          // shift value in O2, O3
1229 #ifdef _LP64
1230   __ srax(O2, Otos_i, Otos_l);
1231 #else
1232   __ lshr(O2, O3, Otos_i, Otos_l1, Otos_l2, O4);
1233 #endif
1234 }
1235 
1236 
1237 
1238 void TemplateTable::lushr() {
1239   transition(itos, ltos); // %%%% see lshl comment
1240 
1241   __ pop_l(O2);                          // shift value in O2, O3
1242 #ifdef _LP64
1243   __ srlx(O2, Otos_i, Otos_l);
1244 #else
1245   __ lushr(O2, O3, Otos_i, Otos_l1, Otos_l2, O4);
1246 #endif
1247 }
1248 
1249 
1250 void TemplateTable::fop2(Operation op) {
1251   transition(ftos, ftos);
1252   switch (op) {
1253    case  add:  __  pop_f(F4); __ fadd(FloatRegisterImpl::S, F4, Ftos_f, Ftos_f);  break;
1254    case  sub:  __  pop_f(F4); __ fsub(FloatRegisterImpl::S, F4, Ftos_f, Ftos_f);  break;
1255    case  mul:  __  pop_f(F4); __ fmul(FloatRegisterImpl::S, F4, Ftos_f, Ftos_f);  break;
1256    case  div:  __  pop_f(F4); __ fdiv(FloatRegisterImpl::S, F4, Ftos_f, Ftos_f);  break;
1257    case  rem:
1258      assert(Ftos_f == F0, "just checking");
1259 #ifdef _LP64
1260      // LP64 calling conventions use F1, F3 for passing 2 floats
1261      __ pop_f(F1);
1262      __ fmov(FloatRegisterImpl::S, Ftos_f, F3);
1263 #else
1264      __ pop_i(O0);
1265      __ stf(FloatRegisterImpl::S, Ftos_f, __ d_tmp);
1266      __ ld( __ d_tmp, O1 );
1267 #endif
1268      __ call_VM_leaf(Lscratch, CAST_FROM_FN_PTR(address, SharedRuntime::frem));
1269      assert( Ftos_f == F0, "fix this code" );
1270      break;
1271 
1272    default: ShouldNotReachHere();
1273   }
1274 }
1275 
1276 
1277 void TemplateTable::dop2(Operation op) {
1278   transition(dtos, dtos);
1279   switch (op) {
1280    case  add:  __  pop_d(F4); __ fadd(FloatRegisterImpl::D, F4, Ftos_d, Ftos_d);  break;
1281    case  sub:  __  pop_d(F4); __ fsub(FloatRegisterImpl::D, F4, Ftos_d, Ftos_d);  break;
1282    case  mul:  __  pop_d(F4); __ fmul(FloatRegisterImpl::D, F4, Ftos_d, Ftos_d);  break;
1283    case  div:  __  pop_d(F4); __ fdiv(FloatRegisterImpl::D, F4, Ftos_d, Ftos_d);  break;
1284    case  rem:
1285 #ifdef _LP64
1286      // Pass arguments in D0, D2
1287      __ fmov(FloatRegisterImpl::D, Ftos_f, F2 );
1288      __ pop_d( F0 );
1289 #else
1290      // Pass arguments in O0O1, O2O3
1291      __ stf(FloatRegisterImpl::D, Ftos_f, __ d_tmp);
1292      __ ldd( __ d_tmp, O2 );
1293      __ pop_d(Ftos_f);
1294      __ stf(FloatRegisterImpl::D, Ftos_f, __ d_tmp);
1295      __ ldd( __ d_tmp, O0 );
1296 #endif
1297      __ call_VM_leaf(Lscratch, CAST_FROM_FN_PTR(address, SharedRuntime::drem));
1298      assert( Ftos_d == F0, "fix this code" );
1299      break;
1300 
1301    default: ShouldNotReachHere();
1302   }
1303 }
1304 
1305 
1306 void TemplateTable::ineg() {
1307   transition(itos, itos);
1308   __ neg(Otos_i);
1309 }
1310 
1311 
1312 void TemplateTable::lneg() {
1313   transition(ltos, ltos);
1314 #ifdef _LP64
1315   __ sub(G0, Otos_l, Otos_l);
1316 #else
1317   __ lneg(Otos_l1, Otos_l2);
1318 #endif
1319 }
1320 
1321 
1322 void TemplateTable::fneg() {
1323   transition(ftos, ftos);
1324   __ fneg(FloatRegisterImpl::S, Ftos_f);
1325 }
1326 
1327 
1328 void TemplateTable::dneg() {
1329   transition(dtos, dtos);
1330   // v8 has fnegd if source and dest are the same
1331   __ fneg(FloatRegisterImpl::D, Ftos_f);
1332 }
1333 
1334 
1335 void TemplateTable::iinc() {
1336   transition(vtos, vtos);
1337   locals_index(G3_scratch);
1338   __ ldsb(Lbcp, 2, O2);  // load constant
1339   __ access_local_int(G3_scratch, Otos_i);
1340   __ add(Otos_i, O2, Otos_i);
1341   __ st(Otos_i, G3_scratch, 0);    // access_local_int puts E.A. in G3_scratch
1342 }
1343 
1344 
1345 void TemplateTable::wide_iinc() {
1346   transition(vtos, vtos);
1347   locals_index_wide(G3_scratch);
1348   __ get_2_byte_integer_at_bcp( 4,  O2, O3, InterpreterMacroAssembler::Signed);
1349   __ access_local_int(G3_scratch, Otos_i);
1350   __ add(Otos_i, O3, Otos_i);
1351   __ st(Otos_i, G3_scratch, 0);    // access_local_int puts E.A. in G3_scratch
1352 }
1353 
1354 
1355 void TemplateTable::convert() {
1356 // %%%%% Factor this first part accross platforms
1357   #ifdef ASSERT
1358     TosState tos_in  = ilgl;
1359     TosState tos_out = ilgl;
1360     switch (bytecode()) {
1361       case Bytecodes::_i2l: // fall through
1362       case Bytecodes::_i2f: // fall through
1363       case Bytecodes::_i2d: // fall through
1364       case Bytecodes::_i2b: // fall through
1365       case Bytecodes::_i2c: // fall through
1366       case Bytecodes::_i2s: tos_in = itos; break;
1367       case Bytecodes::_l2i: // fall through
1368       case Bytecodes::_l2f: // fall through
1369       case Bytecodes::_l2d: tos_in = ltos; break;
1370       case Bytecodes::_f2i: // fall through
1371       case Bytecodes::_f2l: // fall through
1372       case Bytecodes::_f2d: tos_in = ftos; break;
1373       case Bytecodes::_d2i: // fall through
1374       case Bytecodes::_d2l: // fall through
1375       case Bytecodes::_d2f: tos_in = dtos; break;
1376       default             : ShouldNotReachHere();
1377     }
1378     switch (bytecode()) {
1379       case Bytecodes::_l2i: // fall through
1380       case Bytecodes::_f2i: // fall through
1381       case Bytecodes::_d2i: // fall through
1382       case Bytecodes::_i2b: // fall through
1383       case Bytecodes::_i2c: // fall through
1384       case Bytecodes::_i2s: tos_out = itos; break;
1385       case Bytecodes::_i2l: // fall through
1386       case Bytecodes::_f2l: // fall through
1387       case Bytecodes::_d2l: tos_out = ltos; break;
1388       case Bytecodes::_i2f: // fall through
1389       case Bytecodes::_l2f: // fall through
1390       case Bytecodes::_d2f: tos_out = ftos; break;
1391       case Bytecodes::_i2d: // fall through
1392       case Bytecodes::_l2d: // fall through
1393       case Bytecodes::_f2d: tos_out = dtos; break;
1394       default             : ShouldNotReachHere();
1395     }
1396     transition(tos_in, tos_out);
1397   #endif
1398 
1399 
1400   // Conversion
1401   Label done;
1402   switch (bytecode()) {
1403    case Bytecodes::_i2l:
1404 #ifdef _LP64
1405     // Sign extend the 32 bits
1406     __ sra ( Otos_i, 0, Otos_l );
1407 #else
1408     __ addcc(Otos_i, 0, Otos_l2);
1409     __ br(Assembler::greaterEqual, true, Assembler::pt, done);
1410     __ delayed()->clr(Otos_l1);
1411     __ set(~0, Otos_l1);
1412 #endif
1413     break;
1414 
1415    case Bytecodes::_i2f:
1416     __ st(Otos_i, __ d_tmp );
1417     __ ldf(FloatRegisterImpl::S,  __ d_tmp, F0);
1418     __ fitof(FloatRegisterImpl::S, F0, Ftos_f);
1419     break;
1420 
1421    case Bytecodes::_i2d:
1422     __ st(Otos_i, __ d_tmp);
1423     __ ldf(FloatRegisterImpl::S,  __ d_tmp, F0);
1424     __ fitof(FloatRegisterImpl::D, F0, Ftos_f);
1425     break;
1426 
1427    case Bytecodes::_i2b:
1428     __ sll(Otos_i, 24, Otos_i);
1429     __ sra(Otos_i, 24, Otos_i);
1430     break;
1431 
1432    case Bytecodes::_i2c:
1433     __ sll(Otos_i, 16, Otos_i);
1434     __ srl(Otos_i, 16, Otos_i);
1435     break;
1436 
1437    case Bytecodes::_i2s:
1438     __ sll(Otos_i, 16, Otos_i);
1439     __ sra(Otos_i, 16, Otos_i);
1440     break;
1441 
1442    case Bytecodes::_l2i:
1443 #ifndef _LP64
1444     __ mov(Otos_l2, Otos_i);
1445 #else
1446     // Sign-extend into the high 32 bits
1447     __ sra(Otos_l, 0, Otos_i);
1448 #endif
1449     break;
1450 
1451    case Bytecodes::_l2f:
1452    case Bytecodes::_l2d:
1453     __ st_long(Otos_l, __ d_tmp);
1454     __ ldf(FloatRegisterImpl::D, __ d_tmp, Ftos_d);
1455 
1456     if (VM_Version::v9_instructions_work()) {
1457       if (bytecode() == Bytecodes::_l2f) {
1458         __ fxtof(FloatRegisterImpl::S, Ftos_d, Ftos_f);
1459       } else {
1460         __ fxtof(FloatRegisterImpl::D, Ftos_d, Ftos_d);
1461       }
1462     } else {
1463       __ call_VM_leaf(
1464         Lscratch,
1465         bytecode() == Bytecodes::_l2f
1466           ? CAST_FROM_FN_PTR(address, SharedRuntime::l2f)
1467           : CAST_FROM_FN_PTR(address, SharedRuntime::l2d)
1468       );
1469     }
1470     break;
1471 
1472   case Bytecodes::_f2i:  {
1473       Label isNaN;
1474       // result must be 0 if value is NaN; test by comparing value to itself
1475       __ fcmp(FloatRegisterImpl::S, Assembler::fcc0, Ftos_f, Ftos_f);
1476       // According to the v8 manual, you have to have a non-fp instruction
1477       // between fcmp and fb.
1478       if (!VM_Version::v9_instructions_work()) {
1479         __ nop();
1480       }
1481       __ fb(Assembler::f_unordered, true, Assembler::pn, isNaN);
1482       __ delayed()->clr(Otos_i);                                     // NaN
1483       __ ftoi(FloatRegisterImpl::S, Ftos_f, F30);
1484       __ stf(FloatRegisterImpl::S, F30, __ d_tmp);
1485       __ ld(__ d_tmp, Otos_i);
1486       __ bind(isNaN);
1487     }
1488     break;
1489 
1490    case Bytecodes::_f2l:
1491     // must uncache tos
1492     __ push_f();
1493 #ifdef _LP64
1494     __ pop_f(F1);
1495 #else
1496     __ pop_i(O0);
1497 #endif
1498     __ call_VM_leaf(Lscratch, CAST_FROM_FN_PTR(address, SharedRuntime::f2l));
1499     break;
1500 
1501    case Bytecodes::_f2d:
1502     __ ftof( FloatRegisterImpl::S, FloatRegisterImpl::D, Ftos_f, Ftos_f);
1503     break;
1504 
1505    case Bytecodes::_d2i:
1506    case Bytecodes::_d2l:
1507     // must uncache tos
1508     __ push_d();
1509 #ifdef _LP64
1510     // LP64 calling conventions pass first double arg in D0
1511     __ pop_d( Ftos_d );
1512 #else
1513     __ pop_i( O0 );
1514     __ pop_i( O1 );
1515 #endif
1516     __ call_VM_leaf(Lscratch,
1517         bytecode() == Bytecodes::_d2i
1518           ? CAST_FROM_FN_PTR(address, SharedRuntime::d2i)
1519           : CAST_FROM_FN_PTR(address, SharedRuntime::d2l));
1520     break;
1521 
1522     case Bytecodes::_d2f:
1523     if (VM_Version::v9_instructions_work()) {
1524       __ ftof( FloatRegisterImpl::D, FloatRegisterImpl::S, Ftos_d, Ftos_f);
1525     }
1526     else {
1527       // must uncache tos
1528       __ push_d();
1529       __ pop_i(O0);
1530       __ pop_i(O1);
1531       __ call_VM_leaf(Lscratch, CAST_FROM_FN_PTR(address, SharedRuntime::d2f));
1532     }
1533     break;
1534 
1535     default: ShouldNotReachHere();
1536   }
1537   __ bind(done);
1538 }
1539 
1540 
1541 void TemplateTable::lcmp() {
1542   transition(ltos, itos);
1543 
1544 #ifdef _LP64
1545   __ pop_l(O1); // pop off value 1, value 2 is in O0
1546   __ lcmp( O1, Otos_l, Otos_i );
1547 #else
1548   __ pop_l(O2); // cmp O2,3 to O0,1
1549   __ lcmp( O2, O3, Otos_l1, Otos_l2, Otos_i );
1550 #endif
1551 }
1552 
1553 
1554 void TemplateTable::float_cmp(bool is_float, int unordered_result) {
1555 
1556   if (is_float) __ pop_f(F2);
1557   else          __ pop_d(F2);
1558 
1559   assert(Ftos_f == F0  &&  Ftos_d == F0,  "alias checking:");
1560 
1561   __ float_cmp( is_float, unordered_result, F2, F0, Otos_i );
1562 }
1563 
1564 void TemplateTable::branch(bool is_jsr, bool is_wide) {
1565   // Note: on SPARC, we use InterpreterMacroAssembler::if_cmp also.
1566   __ verify_oop(Lmethod);
1567   __ verify_thread();
1568 
1569   const Register O2_bumped_count = O2;
1570   __ profile_taken_branch(G3_scratch, O2_bumped_count);
1571 
1572   // get (wide) offset to O1_disp
1573   const Register O1_disp = O1;
1574   if (is_wide)  __ get_4_byte_integer_at_bcp( 1,  G4_scratch, O1_disp,                                    InterpreterMacroAssembler::set_CC);
1575   else          __ get_2_byte_integer_at_bcp( 1,  G4_scratch, O1_disp, InterpreterMacroAssembler::Signed, InterpreterMacroAssembler::set_CC);
1576 
1577   // Handle all the JSR stuff here, then exit.
1578   // It's much shorter and cleaner than intermingling with the
1579   // non-JSR normal-branch stuff occurring below.
1580   if( is_jsr ) {
1581     // compute return address as bci in Otos_i
1582     __ ld_ptr(Lmethod, methodOopDesc::const_offset(), G3_scratch);
1583     __ sub(Lbcp, G3_scratch, G3_scratch);
1584     __ sub(G3_scratch, in_bytes(constMethodOopDesc::codes_offset()) - (is_wide ? 5 : 3), Otos_i);
1585 
1586     // Bump Lbcp to target of JSR
1587     __ add(Lbcp, O1_disp, Lbcp);
1588     // Push returnAddress for "ret" on stack
1589     __ push_ptr(Otos_i);
1590     // And away we go!
1591     __ dispatch_next(vtos);
1592     return;
1593   }
1594 
1595   // Normal (non-jsr) branch handling
1596 
1597   // Save the current Lbcp
1598   const Register O0_cur_bcp = O0;
1599   __ mov( Lbcp, O0_cur_bcp );
1600 
1601 
1602   bool increment_invocation_counter_for_backward_branches = UseCompiler && UseLoopCounter;
1603   if ( increment_invocation_counter_for_backward_branches ) {
1604     Label Lforward;
1605     // check branch direction
1606     __ br( Assembler::positive, false,  Assembler::pn, Lforward );
1607     // Bump bytecode pointer by displacement (take the branch)
1608     __ delayed()->add( O1_disp, Lbcp, Lbcp );     // add to bc addr
1609 
1610     if (TieredCompilation) {
1611       Label Lno_mdo, Loverflow;
1612       int increment = InvocationCounter::count_increment;
1613       int mask = ((1 << Tier0BackedgeNotifyFreqLog) - 1) << InvocationCounter::count_shift;
1614       if (ProfileInterpreter) {
1615         // If no method data exists, go to profile_continue.
1616         __ ld_ptr(Lmethod, methodOopDesc::method_data_offset(), G4_scratch);
1617         __ br_null_short(G4_scratch, Assembler::pn, Lno_mdo);

1618 
1619         // Increment backedge counter in the MDO
1620         Address mdo_backedge_counter(G4_scratch, in_bytes(methodDataOopDesc::backedge_counter_offset()) +
1621                                                  in_bytes(InvocationCounter::counter_offset()));
1622         __ increment_mask_and_jump(mdo_backedge_counter, increment, mask, G3_scratch, Lscratch,
1623                                    Assembler::notZero, &Lforward);
1624         __ ba_short(Loverflow);

1625       }
1626 
1627       // If there's no MDO, increment counter in methodOop
1628       __ bind(Lno_mdo);
1629       Address backedge_counter(Lmethod, in_bytes(methodOopDesc::backedge_counter_offset()) +
1630                                         in_bytes(InvocationCounter::counter_offset()));
1631       __ increment_mask_and_jump(backedge_counter, increment, mask, G3_scratch, Lscratch,
1632                                  Assembler::notZero, &Lforward);
1633       __ bind(Loverflow);
1634 
1635       // notify point for loop, pass branch bytecode
1636       __ call_VM(noreg, CAST_FROM_FN_PTR(address, InterpreterRuntime::frequency_counter_overflow), O0_cur_bcp);
1637 
1638       // Was an OSR adapter generated?
1639       // O0 = osr nmethod
1640       __ br_null_short(O0, Assembler::pn, Lforward);

1641 
1642       // Has the nmethod been invalidated already?
1643       __ ld(O0, nmethod::entry_bci_offset(), O2);
1644       __ cmp_and_br_short(O2, InvalidOSREntryBci, Assembler::equal, Assembler::pn, Lforward);


1645 
1646       // migrate the interpreter frame off of the stack
1647 
1648       __ mov(G2_thread, L7);
1649       // save nmethod
1650       __ mov(O0, L6);
1651       __ set_last_Java_frame(SP, noreg);
1652       __ call_VM_leaf(noreg, CAST_FROM_FN_PTR(address, SharedRuntime::OSR_migration_begin), L7);
1653       __ reset_last_Java_frame();
1654       __ mov(L7, G2_thread);
1655 
1656       // move OSR nmethod to I1
1657       __ mov(L6, I1);
1658 
1659       // OSR buffer to I0
1660       __ mov(O0, I0);
1661 
1662       // remove the interpreter frame
1663       __ restore(I5_savedSP, 0, SP);
1664 
1665       // Jump to the osr code.
1666       __ ld_ptr(O1, nmethod::osr_entry_point_offset(), O2);
1667       __ jmp(O2, G0);
1668       __ delayed()->nop();
1669 
1670     } else {
1671       // Update Backedge branch separately from invocations
1672       const Register G4_invoke_ctr = G4;
1673       __ increment_backedge_counter(G4_invoke_ctr, G1_scratch);
1674       if (ProfileInterpreter) {
1675         __ test_invocation_counter_for_mdp(G4_invoke_ctr, G3_scratch, Lforward);
1676         if (UseOnStackReplacement) {
1677           __ test_backedge_count_for_osr(O2_bumped_count, O0_cur_bcp, G3_scratch);
1678         }
1679       } else {
1680         if (UseOnStackReplacement) {
1681           __ test_backedge_count_for_osr(G4_invoke_ctr, O0_cur_bcp, G3_scratch);
1682         }
1683       }
1684     }
1685 
1686     __ bind(Lforward);
1687   } else
1688     // Bump bytecode pointer by displacement (take the branch)
1689     __ add( O1_disp, Lbcp, Lbcp );// add to bc addr
1690 
1691   // continue with bytecode @ target
1692   // %%%%% Like Intel, could speed things up by moving bytecode fetch to code above,
1693   // %%%%% and changing dispatch_next to dispatch_only
1694   __ dispatch_next(vtos);
1695 }
1696 
1697 
1698 // Note Condition in argument is TemplateTable::Condition
1699 // arg scope is within class scope
1700 
1701 void TemplateTable::if_0cmp(Condition cc) {
1702   // no pointers, integer only!
1703   transition(itos, vtos);
1704   // assume branch is more often taken than not (loops use backward branches)
1705   __ cmp( Otos_i, 0);
1706   __ if_cmp(ccNot(cc), false);
1707 }
1708 
1709 
1710 void TemplateTable::if_icmp(Condition cc) {
1711   transition(itos, vtos);
1712   __ pop_i(O1);
1713   __ cmp(O1, Otos_i);
1714   __ if_cmp(ccNot(cc), false);
1715 }
1716 
1717 
1718 void TemplateTable::if_nullcmp(Condition cc) {
1719   transition(atos, vtos);
1720   __ tst(Otos_i);
1721   __ if_cmp(ccNot(cc), true);
1722 }
1723 
1724 
1725 void TemplateTable::if_acmp(Condition cc) {
1726   transition(atos, vtos);
1727   __ pop_ptr(O1);
1728   __ verify_oop(O1);
1729   __ verify_oop(Otos_i);
1730   __ cmp(O1, Otos_i);
1731   __ if_cmp(ccNot(cc), true);
1732 }
1733 
1734 
1735 
1736 void TemplateTable::ret() {
1737   transition(vtos, vtos);
1738   locals_index(G3_scratch);
1739   __ access_local_returnAddress(G3_scratch, Otos_i);
1740   // Otos_i contains the bci, compute the bcp from that
1741 
1742 #ifdef _LP64
1743 #ifdef ASSERT
1744   // jsr result was labeled as an 'itos' not an 'atos' because we cannot GC
1745   // the result.  The return address (really a BCI) was stored with an
1746   // 'astore' because JVM specs claim it's a pointer-sized thing.  Hence in
1747   // the 64-bit build the 32-bit BCI is actually in the low bits of a 64-bit
1748   // loaded value.
1749   { Label zzz ;
1750      __ set (65536, G3_scratch) ;
1751      __ cmp (Otos_i, G3_scratch) ;
1752      __ bp( Assembler::lessEqualUnsigned, false, Assembler::xcc, Assembler::pn, zzz);
1753      __ delayed()->nop();
1754      __ stop("BCI is in the wrong register half?");
1755      __ bind (zzz) ;
1756   }
1757 #endif
1758 #endif
1759 
1760   __ profile_ret(vtos, Otos_i, G4_scratch);
1761 
1762   __ ld_ptr(Lmethod, methodOopDesc::const_offset(), G3_scratch);
1763   __ add(G3_scratch, Otos_i, G3_scratch);
1764   __ add(G3_scratch, in_bytes(constMethodOopDesc::codes_offset()), Lbcp);
1765   __ dispatch_next(vtos);
1766 }
1767 
1768 
1769 void TemplateTable::wide_ret() {
1770   transition(vtos, vtos);
1771   locals_index_wide(G3_scratch);
1772   __ access_local_returnAddress(G3_scratch, Otos_i);
1773   // Otos_i contains the bci, compute the bcp from that
1774 
1775   __ profile_ret(vtos, Otos_i, G4_scratch);
1776 
1777   __ ld_ptr(Lmethod, methodOopDesc::const_offset(), G3_scratch);
1778   __ add(G3_scratch, Otos_i, G3_scratch);
1779   __ add(G3_scratch, in_bytes(constMethodOopDesc::codes_offset()), Lbcp);
1780   __ dispatch_next(vtos);
1781 }
1782 
1783 
1784 void TemplateTable::tableswitch() {
1785   transition(itos, vtos);
1786   Label default_case, continue_execution;
1787 
1788   // align bcp
1789   __ add(Lbcp, BytesPerInt, O1);
1790   __ and3(O1, -BytesPerInt, O1);
1791   // load lo, hi
1792   __ ld(O1, 1 * BytesPerInt, O2);       // Low Byte
1793   __ ld(O1, 2 * BytesPerInt, O3);       // High Byte
1794 #ifdef _LP64
1795   // Sign extend the 32 bits
1796   __ sra ( Otos_i, 0, Otos_i );
1797 #endif /* _LP64 */
1798 
1799   // check against lo & hi
1800   __ cmp( Otos_i, O2);
1801   __ br( Assembler::less, false, Assembler::pn, default_case);
1802   __ delayed()->cmp( Otos_i, O3 );
1803   __ br( Assembler::greater, false, Assembler::pn, default_case);
1804   // lookup dispatch offset
1805   __ delayed()->sub(Otos_i, O2, O2);
1806   __ profile_switch_case(O2, O3, G3_scratch, G4_scratch);
1807   __ sll(O2, LogBytesPerInt, O2);
1808   __ add(O2, 3 * BytesPerInt, O2);
1809   __ ba(continue_execution);
1810   __ delayed()->ld(O1, O2, O2);
1811   // handle default
1812   __ bind(default_case);
1813   __ profile_switch_default(O3);
1814   __ ld(O1, 0, O2); // get default offset
1815   // continue execution
1816   __ bind(continue_execution);
1817   __ add(Lbcp, O2, Lbcp);
1818   __ dispatch_next(vtos);
1819 }
1820 
1821 
1822 void TemplateTable::lookupswitch() {
1823   transition(itos, itos);
1824   __ stop("lookupswitch bytecode should have been rewritten");
1825 }
1826 
1827 void TemplateTable::fast_linearswitch() {
1828   transition(itos, vtos);
1829     Label loop_entry, loop, found, continue_execution;
1830   // align bcp
1831   __ add(Lbcp, BytesPerInt, O1);
1832   __ and3(O1, -BytesPerInt, O1);
1833  // set counter
1834   __ ld(O1, BytesPerInt, O2);
1835   __ sll(O2, LogBytesPerInt + 1, O2); // in word-pairs
1836   __ add(O1, 2 * BytesPerInt, O3); // set first pair addr
1837   __ ba(loop_entry);
1838   __ delayed()->add(O3, O2, O2); // counter now points past last pair
1839 
1840   // table search
1841   __ bind(loop);
1842   __ cmp(O4, Otos_i);
1843   __ br(Assembler::equal, true, Assembler::pn, found);
1844   __ delayed()->ld(O3, BytesPerInt, O4); // offset -> O4
1845   __ inc(O3, 2 * BytesPerInt);
1846 
1847   __ bind(loop_entry);
1848   __ cmp(O2, O3);
1849   __ brx(Assembler::greaterUnsigned, true, Assembler::pt, loop);
1850   __ delayed()->ld(O3, 0, O4);
1851 
1852   // default case
1853   __ ld(O1, 0, O4); // get default offset
1854   if (ProfileInterpreter) {
1855     __ profile_switch_default(O3);
1856     __ ba_short(continue_execution);

1857   }
1858 
1859   // entry found -> get offset
1860   __ bind(found);
1861   if (ProfileInterpreter) {
1862     __ sub(O3, O1, O3);
1863     __ sub(O3, 2*BytesPerInt, O3);
1864     __ srl(O3, LogBytesPerInt + 1, O3); // in word-pairs
1865     __ profile_switch_case(O3, O1, O2, G3_scratch);
1866 
1867     __ bind(continue_execution);
1868   }
1869   __ add(Lbcp, O4, Lbcp);
1870   __ dispatch_next(vtos);
1871 }
1872 
1873 
1874 void TemplateTable::fast_binaryswitch() {
1875   transition(itos, vtos);
1876   // Implementation using the following core algorithm: (copied from Intel)
1877   //
1878   // int binary_search(int key, LookupswitchPair* array, int n) {
1879   //   // Binary search according to "Methodik des Programmierens" by
1880   //   // Edsger W. Dijkstra and W.H.J. Feijen, Addison Wesley Germany 1985.
1881   //   int i = 0;
1882   //   int j = n;
1883   //   while (i+1 < j) {
1884   //     // invariant P: 0 <= i < j <= n and (a[i] <= key < a[j] or Q)
1885   //     // with      Q: for all i: 0 <= i < n: key < a[i]
1886   //     // where a stands for the array and assuming that the (inexisting)
1887   //     // element a[n] is infinitely big.
1888   //     int h = (i + j) >> 1;
1889   //     // i < h < j
1890   //     if (key < array[h].fast_match()) {
1891   //       j = h;
1892   //     } else {
1893   //       i = h;
1894   //     }
1895   //   }
1896   //   // R: a[i] <= key < a[i+1] or Q
1897   //   // (i.e., if key is within array, i is the correct index)
1898   //   return i;
1899   // }
1900 
1901   // register allocation
1902   assert(Otos_i == O0, "alias checking");
1903   const Register Rkey     = Otos_i;                    // already set (tosca)
1904   const Register Rarray   = O1;
1905   const Register Ri       = O2;
1906   const Register Rj       = O3;
1907   const Register Rh       = O4;
1908   const Register Rscratch = O5;
1909 
1910   const int log_entry_size = 3;
1911   const int entry_size = 1 << log_entry_size;
1912 
1913   Label found;
1914   // Find Array start
1915   __ add(Lbcp, 3 * BytesPerInt, Rarray);
1916   __ and3(Rarray, -BytesPerInt, Rarray);
1917   // initialize i & j (in delay slot)
1918   __ clr( Ri );
1919 
1920   // and start
1921   Label entry;
1922   __ ba(entry);
1923   __ delayed()->ld( Rarray, -BytesPerInt, Rj);
1924   // (Rj is already in the native byte-ordering.)
1925 
1926   // binary search loop
1927   { Label loop;
1928     __ bind( loop );
1929     // int h = (i + j) >> 1;
1930     __ sra( Rh, 1, Rh );
1931     // if (key < array[h].fast_match()) {
1932     //   j = h;
1933     // } else {
1934     //   i = h;
1935     // }
1936     __ sll( Rh, log_entry_size, Rscratch );
1937     __ ld( Rarray, Rscratch, Rscratch );
1938     // (Rscratch is already in the native byte-ordering.)
1939     __ cmp( Rkey, Rscratch );
1940     if ( VM_Version::v9_instructions_work() ) {
1941       __ movcc( Assembler::less,         false, Assembler::icc, Rh, Rj );  // j = h if (key <  array[h].fast_match())
1942       __ movcc( Assembler::greaterEqual, false, Assembler::icc, Rh, Ri );  // i = h if (key >= array[h].fast_match())
1943     }
1944     else {
1945       Label end_of_if;
1946       __ br( Assembler::less, true, Assembler::pt, end_of_if );
1947       __ delayed()->mov( Rh, Rj ); // if (<) Rj = Rh
1948       __ mov( Rh, Ri );            // else i = h
1949       __ bind(end_of_if);          // }
1950     }
1951 
1952     // while (i+1 < j)
1953     __ bind( entry );
1954     __ add( Ri, 1, Rscratch );
1955     __ cmp(Rscratch, Rj);
1956     __ br( Assembler::less, true, Assembler::pt, loop );
1957     __ delayed()->add( Ri, Rj, Rh ); // start h = i + j  >> 1;
1958   }
1959 
1960   // end of binary search, result index is i (must check again!)
1961   Label default_case;
1962   Label continue_execution;
1963   if (ProfileInterpreter) {
1964     __ mov( Ri, Rh );              // Save index in i for profiling
1965   }
1966   __ sll( Ri, log_entry_size, Ri );
1967   __ ld( Rarray, Ri, Rscratch );
1968   // (Rscratch is already in the native byte-ordering.)
1969   __ cmp( Rkey, Rscratch );
1970   __ br( Assembler::notEqual, true, Assembler::pn, default_case );
1971   __ delayed()->ld( Rarray, -2 * BytesPerInt, Rj ); // load default offset -> j
1972 
1973   // entry found -> j = offset
1974   __ inc( Ri, BytesPerInt );
1975   __ profile_switch_case(Rh, Rj, Rscratch, Rkey);
1976   __ ld( Rarray, Ri, Rj );
1977   // (Rj is already in the native byte-ordering.)
1978 
1979   if (ProfileInterpreter) {
1980     __ ba_short(continue_execution);

1981   }
1982 
1983   __ bind(default_case); // fall through (if not profiling)
1984   __ profile_switch_default(Ri);
1985 
1986   __ bind(continue_execution);
1987   __ add( Lbcp, Rj, Lbcp );
1988   __ dispatch_next( vtos );
1989 }
1990 
1991 
1992 void TemplateTable::_return(TosState state) {
1993   transition(state, state);
1994   assert(_desc->calls_vm(), "inconsistent calls_vm information");
1995 
1996   if (_desc->bytecode() == Bytecodes::_return_register_finalizer) {
1997     assert(state == vtos, "only valid state");
1998     __ mov(G0, G3_scratch);
1999     __ access_local_ptr(G3_scratch, Otos_i);
2000     __ load_klass(Otos_i, O2);
2001     __ set(JVM_ACC_HAS_FINALIZER, G3);
2002     __ ld(O2, Klass::access_flags_offset_in_bytes() + sizeof(oopDesc), O2);
2003     __ andcc(G3, O2, G0);
2004     Label skip_register_finalizer;
2005     __ br(Assembler::zero, false, Assembler::pn, skip_register_finalizer);
2006     __ delayed()->nop();
2007 
2008     // Call out to do finalizer registration
2009     __ call_VM(noreg, CAST_FROM_FN_PTR(address, InterpreterRuntime::register_finalizer), Otos_i);
2010 
2011     __ bind(skip_register_finalizer);
2012   }
2013 
2014   __ remove_activation(state, /* throw_monitor_exception */ true);
2015 
2016   // The caller's SP was adjusted upon method entry to accomodate
2017   // the callee's non-argument locals. Undo that adjustment.
2018   __ ret();                             // return to caller
2019   __ delayed()->restore(I5_savedSP, G0, SP);
2020 }
2021 
2022 
2023 // ----------------------------------------------------------------------------
2024 // Volatile variables demand their effects be made known to all CPU's in
2025 // order.  Store buffers on most chips allow reads & writes to reorder; the
2026 // JMM's ReadAfterWrite.java test fails in -Xint mode without some kind of
2027 // memory barrier (i.e., it's not sufficient that the interpreter does not
2028 // reorder volatile references, the hardware also must not reorder them).
2029 //
2030 // According to the new Java Memory Model (JMM):
2031 // (1) All volatiles are serialized wrt to each other.
2032 // ALSO reads & writes act as aquire & release, so:
2033 // (2) A read cannot let unrelated NON-volatile memory refs that happen after
2034 // the read float up to before the read.  It's OK for non-volatile memory refs
2035 // that happen before the volatile read to float down below it.
2036 // (3) Similar a volatile write cannot let unrelated NON-volatile memory refs
2037 // that happen BEFORE the write float down to after the write.  It's OK for
2038 // non-volatile memory refs that happen after the volatile write to float up
2039 // before it.
2040 //
2041 // We only put in barriers around volatile refs (they are expensive), not
2042 // _between_ memory refs (that would require us to track the flavor of the
2043 // previous memory refs).  Requirements (2) and (3) require some barriers
2044 // before volatile stores and after volatile loads.  These nearly cover
2045 // requirement (1) but miss the volatile-store-volatile-load case.  This final
2046 // case is placed after volatile-stores although it could just as well go
2047 // before volatile-loads.
2048 void TemplateTable::volatile_barrier(Assembler::Membar_mask_bits order_constraint) {
2049   // Helper function to insert a is-volatile test and memory barrier
2050   // All current sparc implementations run in TSO, needing only StoreLoad
2051   if ((order_constraint & Assembler::StoreLoad) == 0) return;
2052   __ membar( order_constraint );
2053 }
2054 
2055 // ----------------------------------------------------------------------------
2056 void TemplateTable::resolve_cache_and_index(int byte_no,
2057                                             Register result,
2058                                             Register Rcache,
2059                                             Register index,
2060                                             size_t index_size) {
2061   // Depends on cpCacheOop layout!
2062   Label resolved;
2063 
2064   __ get_cache_and_index_at_bcp(Rcache, index, 1, index_size);
2065   if (byte_no == f1_oop) {
2066     // We are resolved if the f1 field contains a non-null object (CallSite, etc.)
2067     // This kind of CP cache entry does not need to match the flags byte, because
2068     // there is a 1-1 relation between bytecode type and CP entry type.
2069     assert_different_registers(result, Rcache);
2070     __ ld_ptr(Rcache, constantPoolCacheOopDesc::base_offset() +
2071               ConstantPoolCacheEntry::f1_offset(), result);
2072     __ tst(result);
2073     __ br(Assembler::notEqual, false, Assembler::pt, resolved);
2074     __ delayed()->set((int)bytecode(), O1);
2075   } else {
2076     assert(byte_no == f1_byte || byte_no == f2_byte, "byte_no out of range");
2077     assert(result == noreg, "");  //else change code for setting result
2078     const int shift_count = (1 + byte_no)*BitsPerByte;
2079 
2080     __ ld_ptr(Rcache, constantPoolCacheOopDesc::base_offset() +
2081               ConstantPoolCacheEntry::indices_offset(), Lbyte_code);
2082 
2083     __ srl(  Lbyte_code, shift_count, Lbyte_code );
2084     __ and3( Lbyte_code,        0xFF, Lbyte_code );
2085     __ cmp(  Lbyte_code, (int)bytecode());
2086     __ br(   Assembler::equal, false, Assembler::pt, resolved);
2087     __ delayed()->set((int)bytecode(), O1);
2088   }
2089 
2090   address entry;
2091   switch (bytecode()) {
2092     case Bytecodes::_getstatic      : // fall through
2093     case Bytecodes::_putstatic      : // fall through
2094     case Bytecodes::_getfield       : // fall through
2095     case Bytecodes::_putfield       : entry = CAST_FROM_FN_PTR(address, InterpreterRuntime::resolve_get_put); break;
2096     case Bytecodes::_invokevirtual  : // fall through
2097     case Bytecodes::_invokespecial  : // fall through
2098     case Bytecodes::_invokestatic   : // fall through
2099     case Bytecodes::_invokeinterface: entry = CAST_FROM_FN_PTR(address, InterpreterRuntime::resolve_invoke);  break;
2100     case Bytecodes::_invokedynamic  : entry = CAST_FROM_FN_PTR(address, InterpreterRuntime::resolve_invokedynamic);  break;
2101     case Bytecodes::_fast_aldc      : entry = CAST_FROM_FN_PTR(address, InterpreterRuntime::resolve_ldc);     break;
2102     case Bytecodes::_fast_aldc_w    : entry = CAST_FROM_FN_PTR(address, InterpreterRuntime::resolve_ldc);     break;
2103     default                         : ShouldNotReachHere();                                 break;
2104   }
2105   // first time invocation - must resolve first
2106   __ call_VM(noreg, entry, O1);
2107   // Update registers with resolved info
2108   __ get_cache_and_index_at_bcp(Rcache, index, 1, index_size);
2109   if (result != noreg)
2110     __ ld_ptr(Rcache, constantPoolCacheOopDesc::base_offset() +
2111               ConstantPoolCacheEntry::f1_offset(), result);
2112   __ bind(resolved);
2113 }
2114 
2115 void TemplateTable::load_invoke_cp_cache_entry(int byte_no,
2116                                                Register Rmethod,
2117                                                Register Ritable_index,
2118                                                Register Rflags,
2119                                                bool is_invokevirtual,
2120                                                bool is_invokevfinal,
2121                                                bool is_invokedynamic) {
2122   // Uses both G3_scratch and G4_scratch
2123   Register Rcache = G3_scratch;
2124   Register Rscratch = G4_scratch;
2125   assert_different_registers(Rcache, Rmethod, Ritable_index);
2126 
2127   ByteSize cp_base_offset = constantPoolCacheOopDesc::base_offset();
2128 
2129   // determine constant pool cache field offsets
2130   const int method_offset = in_bytes(
2131     cp_base_offset +
2132       (is_invokevirtual
2133        ? ConstantPoolCacheEntry::f2_offset()
2134        : ConstantPoolCacheEntry::f1_offset()
2135       )
2136     );
2137   const int flags_offset = in_bytes(cp_base_offset +
2138                                     ConstantPoolCacheEntry::flags_offset());
2139   // access constant pool cache fields
2140   const int index_offset = in_bytes(cp_base_offset +
2141                                     ConstantPoolCacheEntry::f2_offset());
2142 
2143   if (is_invokevfinal) {
2144     __ get_cache_and_index_at_bcp(Rcache, Rscratch, 1);
2145     __ ld_ptr(Rcache, method_offset, Rmethod);
2146   } else if (byte_no == f1_oop) {
2147     // Resolved f1_oop goes directly into 'method' register.
2148     resolve_cache_and_index(byte_no, Rmethod, Rcache, Rscratch, sizeof(u4));
2149   } else {
2150     resolve_cache_and_index(byte_no, noreg, Rcache, Rscratch, sizeof(u2));
2151     __ ld_ptr(Rcache, method_offset, Rmethod);
2152   }
2153 
2154   if (Ritable_index != noreg) {
2155     __ ld_ptr(Rcache, index_offset, Ritable_index);
2156   }
2157   __ ld_ptr(Rcache, flags_offset, Rflags);
2158 }
2159 
2160 // The Rcache register must be set before call
2161 void TemplateTable::load_field_cp_cache_entry(Register Robj,
2162                                               Register Rcache,
2163                                               Register index,
2164                                               Register Roffset,
2165                                               Register Rflags,
2166                                               bool is_static) {
2167   assert_different_registers(Rcache, Rflags, Roffset);
2168 
2169   ByteSize cp_base_offset = constantPoolCacheOopDesc::base_offset();
2170 
2171   __ ld_ptr(Rcache, cp_base_offset + ConstantPoolCacheEntry::flags_offset(), Rflags);
2172   __ ld_ptr(Rcache, cp_base_offset + ConstantPoolCacheEntry::f2_offset(), Roffset);
2173   if (is_static) {
2174     __ ld_ptr(Rcache, cp_base_offset + ConstantPoolCacheEntry::f1_offset(), Robj);
2175   }
2176 }
2177 
2178 // The registers Rcache and index expected to be set before call.
2179 // Correct values of the Rcache and index registers are preserved.
2180 void TemplateTable::jvmti_post_field_access(Register Rcache,
2181                                             Register index,
2182                                             bool is_static,
2183                                             bool has_tos) {
2184   ByteSize cp_base_offset = constantPoolCacheOopDesc::base_offset();
2185 
2186   if (JvmtiExport::can_post_field_access()) {
2187     // Check to see if a field access watch has been set before we take
2188     // the time to call into the VM.
2189     Label Label1;
2190     assert_different_registers(Rcache, index, G1_scratch);
2191     AddressLiteral get_field_access_count_addr(JvmtiExport::get_field_access_count_addr());
2192     __ load_contents(get_field_access_count_addr, G1_scratch);
2193     __ cmp_and_br_short(G1_scratch, 0, Assembler::equal, Assembler::pt, Label1);


2194 
2195     __ add(Rcache, in_bytes(cp_base_offset), Rcache);
2196 
2197     if (is_static) {
2198       __ clr(Otos_i);
2199     } else {
2200       if (has_tos) {
2201       // save object pointer before call_VM() clobbers it
2202         __ push_ptr(Otos_i);  // put object on tos where GC wants it.
2203       } else {
2204         // Load top of stack (do not pop the value off the stack);
2205         __ ld_ptr(Lesp, Interpreter::expr_offset_in_bytes(0), Otos_i);
2206       }
2207       __ verify_oop(Otos_i);
2208     }
2209     // Otos_i: object pointer or NULL if static
2210     // Rcache: cache entry pointer
2211     __ call_VM(noreg, CAST_FROM_FN_PTR(address, InterpreterRuntime::post_field_access),
2212                Otos_i, Rcache);
2213     if (!is_static && has_tos) {
2214       __ pop_ptr(Otos_i);  // restore object pointer
2215       __ verify_oop(Otos_i);
2216     }
2217     __ get_cache_and_index_at_bcp(Rcache, index, 1);
2218     __ bind(Label1);
2219   }
2220 }
2221 
2222 void TemplateTable::getfield_or_static(int byte_no, bool is_static) {
2223   transition(vtos, vtos);
2224 
2225   Register Rcache = G3_scratch;
2226   Register index  = G4_scratch;
2227   Register Rclass = Rcache;
2228   Register Roffset= G4_scratch;
2229   Register Rflags = G1_scratch;
2230   ByteSize cp_base_offset = constantPoolCacheOopDesc::base_offset();
2231 
2232   resolve_cache_and_index(byte_no, noreg, Rcache, index, sizeof(u2));
2233   jvmti_post_field_access(Rcache, index, is_static, false);
2234   load_field_cp_cache_entry(Rclass, Rcache, index, Roffset, Rflags, is_static);
2235 
2236   if (!is_static) {
2237     pop_and_check_object(Rclass);
2238   } else {
2239     __ verify_oop(Rclass);
2240   }
2241 
2242   Label exit;
2243 
2244   Assembler::Membar_mask_bits membar_bits =
2245     Assembler::Membar_mask_bits(Assembler::LoadLoad | Assembler::LoadStore);
2246 
2247   if (__ membar_has_effect(membar_bits)) {
2248     // Get volatile flag
2249     __ set((1 << ConstantPoolCacheEntry::volatileField), Lscratch);
2250     __ and3(Rflags, Lscratch, Lscratch);
2251   }
2252 
2253   Label checkVolatile;
2254 
2255   // compute field type
2256   Label notByte, notInt, notShort, notChar, notLong, notFloat, notObj;
2257   __ srl(Rflags, ConstantPoolCacheEntry::tosBits, Rflags);
2258   // Make sure we don't need to mask Rflags for tosBits after the above shift
2259   ConstantPoolCacheEntry::verify_tosBits();
2260 
2261   // Check atos before itos for getstatic, more likely (in Queens at least)
2262   __ cmp(Rflags, atos);
2263   __ br(Assembler::notEqual, false, Assembler::pt, notObj);
2264   __ delayed() ->cmp(Rflags, itos);
2265 
2266   // atos
2267   __ load_heap_oop(Rclass, Roffset, Otos_i);
2268   __ verify_oop(Otos_i);
2269   __ push(atos);
2270   if (!is_static) {
2271     patch_bytecode(Bytecodes::_fast_agetfield, G3_scratch, G4_scratch);
2272   }
2273   __ ba(checkVolatile);
2274   __ delayed()->tst(Lscratch);
2275 
2276   __ bind(notObj);
2277 
2278   // cmp(Rflags, itos);
2279   __ br(Assembler::notEqual, false, Assembler::pt, notInt);
2280   __ delayed() ->cmp(Rflags, ltos);
2281 
2282   // itos
2283   __ ld(Rclass, Roffset, Otos_i);
2284   __ push(itos);
2285   if (!is_static) {
2286     patch_bytecode(Bytecodes::_fast_igetfield, G3_scratch, G4_scratch);
2287   }
2288   __ ba(checkVolatile);
2289   __ delayed()->tst(Lscratch);
2290 
2291   __ bind(notInt);
2292 
2293   // cmp(Rflags, ltos);
2294   __ br(Assembler::notEqual, false, Assembler::pt, notLong);
2295   __ delayed() ->cmp(Rflags, btos);
2296 
2297   // ltos
2298   // load must be atomic
2299   __ ld_long(Rclass, Roffset, Otos_l);
2300   __ push(ltos);
2301   if (!is_static) {
2302     patch_bytecode(Bytecodes::_fast_lgetfield, G3_scratch, G4_scratch);
2303   }
2304   __ ba(checkVolatile);
2305   __ delayed()->tst(Lscratch);
2306 
2307   __ bind(notLong);
2308 
2309   // cmp(Rflags, btos);
2310   __ br(Assembler::notEqual, false, Assembler::pt, notByte);
2311   __ delayed() ->cmp(Rflags, ctos);
2312 
2313   // btos
2314   __ ldsb(Rclass, Roffset, Otos_i);
2315   __ push(itos);
2316   if (!is_static) {
2317     patch_bytecode(Bytecodes::_fast_bgetfield, G3_scratch, G4_scratch);
2318   }
2319   __ ba(checkVolatile);
2320   __ delayed()->tst(Lscratch);
2321 
2322   __ bind(notByte);
2323 
2324   // cmp(Rflags, ctos);
2325   __ br(Assembler::notEqual, false, Assembler::pt, notChar);
2326   __ delayed() ->cmp(Rflags, stos);
2327 
2328   // ctos
2329   __ lduh(Rclass, Roffset, Otos_i);
2330   __ push(itos);
2331   if (!is_static) {
2332     patch_bytecode(Bytecodes::_fast_cgetfield, G3_scratch, G4_scratch);
2333   }
2334   __ ba(checkVolatile);
2335   __ delayed()->tst(Lscratch);
2336 
2337   __ bind(notChar);
2338 
2339   // cmp(Rflags, stos);
2340   __ br(Assembler::notEqual, false, Assembler::pt, notShort);
2341   __ delayed() ->cmp(Rflags, ftos);
2342 
2343   // stos
2344   __ ldsh(Rclass, Roffset, Otos_i);
2345   __ push(itos);
2346   if (!is_static) {
2347     patch_bytecode(Bytecodes::_fast_sgetfield, G3_scratch, G4_scratch);
2348   }
2349   __ ba(checkVolatile);
2350   __ delayed()->tst(Lscratch);
2351 
2352   __ bind(notShort);
2353 
2354 
2355   // cmp(Rflags, ftos);
2356   __ br(Assembler::notEqual, false, Assembler::pt, notFloat);
2357   __ delayed() ->tst(Lscratch);
2358 
2359   // ftos
2360   __ ldf(FloatRegisterImpl::S, Rclass, Roffset, Ftos_f);
2361   __ push(ftos);
2362   if (!is_static) {
2363     patch_bytecode(Bytecodes::_fast_fgetfield, G3_scratch, G4_scratch);
2364   }
2365   __ ba(checkVolatile);
2366   __ delayed()->tst(Lscratch);
2367 
2368   __ bind(notFloat);
2369 
2370 
2371   // dtos
2372   __ ldf(FloatRegisterImpl::D, Rclass, Roffset, Ftos_d);
2373   __ push(dtos);
2374   if (!is_static) {
2375     patch_bytecode(Bytecodes::_fast_dgetfield, G3_scratch, G4_scratch);
2376   }
2377 
2378   __ bind(checkVolatile);
2379   if (__ membar_has_effect(membar_bits)) {
2380     // __ tst(Lscratch); executed in delay slot
2381     __ br(Assembler::zero, false, Assembler::pt, exit);
2382     __ delayed()->nop();
2383     volatile_barrier(membar_bits);
2384   }
2385 
2386   __ bind(exit);
2387 }
2388 
2389 
2390 void TemplateTable::getfield(int byte_no) {
2391   getfield_or_static(byte_no, false);
2392 }
2393 
2394 void TemplateTable::getstatic(int byte_no) {
2395   getfield_or_static(byte_no, true);
2396 }
2397 
2398 
2399 void TemplateTable::fast_accessfield(TosState state) {
2400   transition(atos, state);
2401   Register Rcache  = G3_scratch;
2402   Register index   = G4_scratch;
2403   Register Roffset = G4_scratch;
2404   Register Rflags  = Rcache;
2405   ByteSize cp_base_offset = constantPoolCacheOopDesc::base_offset();
2406 
2407   __ get_cache_and_index_at_bcp(Rcache, index, 1);
2408   jvmti_post_field_access(Rcache, index, /*is_static*/false, /*has_tos*/true);
2409 
2410   __ ld_ptr(Rcache, cp_base_offset + ConstantPoolCacheEntry::f2_offset(), Roffset);
2411 
2412   __ null_check(Otos_i);
2413   __ verify_oop(Otos_i);
2414 
2415   Label exit;
2416 
2417   Assembler::Membar_mask_bits membar_bits =
2418     Assembler::Membar_mask_bits(Assembler::LoadLoad | Assembler::LoadStore);
2419   if (__ membar_has_effect(membar_bits)) {
2420     // Get volatile flag
2421     __ ld_ptr(Rcache, cp_base_offset + ConstantPoolCacheEntry::f2_offset(), Rflags);
2422     __ set((1 << ConstantPoolCacheEntry::volatileField), Lscratch);
2423   }
2424 
2425   switch (bytecode()) {
2426     case Bytecodes::_fast_bgetfield:
2427       __ ldsb(Otos_i, Roffset, Otos_i);
2428       break;
2429     case Bytecodes::_fast_cgetfield:
2430       __ lduh(Otos_i, Roffset, Otos_i);
2431       break;
2432     case Bytecodes::_fast_sgetfield:
2433       __ ldsh(Otos_i, Roffset, Otos_i);
2434       break;
2435     case Bytecodes::_fast_igetfield:
2436       __ ld(Otos_i, Roffset, Otos_i);
2437       break;
2438     case Bytecodes::_fast_lgetfield:
2439       __ ld_long(Otos_i, Roffset, Otos_l);
2440       break;
2441     case Bytecodes::_fast_fgetfield:
2442       __ ldf(FloatRegisterImpl::S, Otos_i, Roffset, Ftos_f);
2443       break;
2444     case Bytecodes::_fast_dgetfield:
2445       __ ldf(FloatRegisterImpl::D, Otos_i, Roffset, Ftos_d);
2446       break;
2447     case Bytecodes::_fast_agetfield:
2448       __ load_heap_oop(Otos_i, Roffset, Otos_i);
2449       break;
2450     default:
2451       ShouldNotReachHere();
2452   }
2453 
2454   if (__ membar_has_effect(membar_bits)) {
2455     __ btst(Lscratch, Rflags);
2456     __ br(Assembler::zero, false, Assembler::pt, exit);
2457     __ delayed()->nop();
2458     volatile_barrier(membar_bits);
2459     __ bind(exit);
2460   }
2461 
2462   if (state == atos) {
2463     __ verify_oop(Otos_i);    // does not blow flags!
2464   }
2465 }
2466 
2467 void TemplateTable::jvmti_post_fast_field_mod() {
2468   if (JvmtiExport::can_post_field_modification()) {
2469     // Check to see if a field modification watch has been set before we take
2470     // the time to call into the VM.
2471     Label done;
2472     AddressLiteral get_field_modification_count_addr(JvmtiExport::get_field_modification_count_addr());
2473     __ load_contents(get_field_modification_count_addr, G4_scratch);
2474     __ cmp_and_br_short(G4_scratch, 0, Assembler::equal, Assembler::pt, done);


2475     __ pop_ptr(G4_scratch);     // copy the object pointer from tos
2476     __ verify_oop(G4_scratch);
2477     __ push_ptr(G4_scratch);    // put the object pointer back on tos
2478     __ get_cache_entry_pointer_at_bcp(G1_scratch, G3_scratch, 1);
2479     // Save tos values before call_VM() clobbers them. Since we have
2480     // to do it for every data type, we use the saved values as the
2481     // jvalue object.
2482     switch (bytecode()) {  // save tos values before call_VM() clobbers them
2483     case Bytecodes::_fast_aputfield: __ push_ptr(Otos_i); break;
2484     case Bytecodes::_fast_bputfield: // fall through
2485     case Bytecodes::_fast_sputfield: // fall through
2486     case Bytecodes::_fast_cputfield: // fall through
2487     case Bytecodes::_fast_iputfield: __ push_i(Otos_i); break;
2488     case Bytecodes::_fast_dputfield: __ push_d(Ftos_d); break;
2489     case Bytecodes::_fast_fputfield: __ push_f(Ftos_f); break;
2490     // get words in right order for use as jvalue object
2491     case Bytecodes::_fast_lputfield: __ push_l(Otos_l); break;
2492     }
2493     // setup pointer to jvalue object
2494     __ mov(Lesp, G3_scratch);  __ inc(G3_scratch, wordSize);
2495     // G4_scratch:  object pointer
2496     // G1_scratch: cache entry pointer
2497     // G3_scratch: jvalue object on the stack
2498     __ call_VM(noreg, CAST_FROM_FN_PTR(address, InterpreterRuntime::post_field_modification), G4_scratch, G1_scratch, G3_scratch);
2499     switch (bytecode()) {             // restore tos values
2500     case Bytecodes::_fast_aputfield: __ pop_ptr(Otos_i); break;
2501     case Bytecodes::_fast_bputfield: // fall through
2502     case Bytecodes::_fast_sputfield: // fall through
2503     case Bytecodes::_fast_cputfield: // fall through
2504     case Bytecodes::_fast_iputfield: __ pop_i(Otos_i); break;
2505     case Bytecodes::_fast_dputfield: __ pop_d(Ftos_d); break;
2506     case Bytecodes::_fast_fputfield: __ pop_f(Ftos_f); break;
2507     case Bytecodes::_fast_lputfield: __ pop_l(Otos_l); break;
2508     }
2509     __ bind(done);
2510   }
2511 }
2512 
2513 // The registers Rcache and index expected to be set before call.
2514 // The function may destroy various registers, just not the Rcache and index registers.
2515 void TemplateTable::jvmti_post_field_mod(Register Rcache, Register index, bool is_static) {
2516   ByteSize cp_base_offset = constantPoolCacheOopDesc::base_offset();
2517 
2518   if (JvmtiExport::can_post_field_modification()) {
2519     // Check to see if a field modification watch has been set before we take
2520     // the time to call into the VM.
2521     Label Label1;
2522     assert_different_registers(Rcache, index, G1_scratch);
2523     AddressLiteral get_field_modification_count_addr(JvmtiExport::get_field_modification_count_addr());
2524     __ load_contents(get_field_modification_count_addr, G1_scratch);
2525     __ cmp_and_br_short(G1_scratch, 0, Assembler::zero, Assembler::pt, Label1);


2526 
2527     // The Rcache and index registers have been already set.
2528     // This allows to eliminate this call but the Rcache and index
2529     // registers must be correspondingly used after this line.
2530     __ get_cache_and_index_at_bcp(G1_scratch, G4_scratch, 1);
2531 
2532     __ add(G1_scratch, in_bytes(cp_base_offset), G3_scratch);
2533     if (is_static) {
2534       // Life is simple.  Null out the object pointer.
2535       __ clr(G4_scratch);
2536     } else {
2537       Register Rflags = G1_scratch;
2538       // Life is harder. The stack holds the value on top, followed by the
2539       // object.  We don't know the size of the value, though; it could be
2540       // one or two words depending on its type. As a result, we must find
2541       // the type to determine where the object is.
2542 
2543       Label two_word, valsizeknown;
2544       __ ld_ptr(G1_scratch, cp_base_offset + ConstantPoolCacheEntry::flags_offset(), Rflags);
2545       __ mov(Lesp, G4_scratch);
2546       __ srl(Rflags, ConstantPoolCacheEntry::tosBits, Rflags);
2547       // Make sure we don't need to mask Rflags for tosBits after the above shift
2548       ConstantPoolCacheEntry::verify_tosBits();
2549       __ cmp(Rflags, ltos);
2550       __ br(Assembler::equal, false, Assembler::pt, two_word);
2551       __ delayed()->cmp(Rflags, dtos);
2552       __ br(Assembler::equal, false, Assembler::pt, two_word);
2553       __ delayed()->nop();
2554       __ inc(G4_scratch, Interpreter::expr_offset_in_bytes(1));
2555       __ ba_short(valsizeknown);

2556       __ bind(two_word);
2557 
2558       __ inc(G4_scratch, Interpreter::expr_offset_in_bytes(2));
2559 
2560       __ bind(valsizeknown);
2561       // setup object pointer
2562       __ ld_ptr(G4_scratch, 0, G4_scratch);
2563       __ verify_oop(G4_scratch);
2564     }
2565     // setup pointer to jvalue object
2566     __ mov(Lesp, G1_scratch);  __ inc(G1_scratch, wordSize);
2567     // G4_scratch:  object pointer or NULL if static
2568     // G3_scratch: cache entry pointer
2569     // G1_scratch: jvalue object on the stack
2570     __ call_VM(noreg, CAST_FROM_FN_PTR(address, InterpreterRuntime::post_field_modification),
2571                G4_scratch, G3_scratch, G1_scratch);
2572     __ get_cache_and_index_at_bcp(Rcache, index, 1);
2573     __ bind(Label1);
2574   }
2575 }
2576 
2577 void TemplateTable::pop_and_check_object(Register r) {
2578   __ pop_ptr(r);
2579   __ null_check(r);  // for field access must check obj.
2580   __ verify_oop(r);
2581 }
2582 
2583 void TemplateTable::putfield_or_static(int byte_no, bool is_static) {
2584   transition(vtos, vtos);
2585   Register Rcache = G3_scratch;
2586   Register index  = G4_scratch;
2587   Register Rclass = Rcache;
2588   Register Roffset= G4_scratch;
2589   Register Rflags = G1_scratch;
2590   ByteSize cp_base_offset = constantPoolCacheOopDesc::base_offset();
2591 
2592   resolve_cache_and_index(byte_no, noreg, Rcache, index, sizeof(u2));
2593   jvmti_post_field_mod(Rcache, index, is_static);
2594   load_field_cp_cache_entry(Rclass, Rcache, index, Roffset, Rflags, is_static);
2595 
2596   Assembler::Membar_mask_bits read_bits =
2597     Assembler::Membar_mask_bits(Assembler::LoadStore | Assembler::StoreStore);
2598   Assembler::Membar_mask_bits write_bits = Assembler::StoreLoad;
2599 
2600   Label notVolatile, checkVolatile, exit;
2601   if (__ membar_has_effect(read_bits) || __ membar_has_effect(write_bits)) {
2602     __ set((1 << ConstantPoolCacheEntry::volatileField), Lscratch);
2603     __ and3(Rflags, Lscratch, Lscratch);
2604 
2605     if (__ membar_has_effect(read_bits)) {
2606       __ cmp_and_br_short(Lscratch, 0, Assembler::equal, Assembler::pt, notVolatile);


2607       volatile_barrier(read_bits);
2608       __ bind(notVolatile);
2609     }
2610   }
2611 
2612   __ srl(Rflags, ConstantPoolCacheEntry::tosBits, Rflags);
2613   // Make sure we don't need to mask Rflags for tosBits after the above shift
2614   ConstantPoolCacheEntry::verify_tosBits();
2615 
2616   // compute field type
2617   Label notInt, notShort, notChar, notObj, notByte, notLong, notFloat;
2618 
2619   if (is_static) {
2620     // putstatic with object type most likely, check that first
2621     __ cmp(Rflags, atos );
2622     __ br(Assembler::notEqual, false, Assembler::pt, notObj);
2623     __ delayed() ->cmp(Rflags, itos );
2624 
2625     // atos
2626     __ pop_ptr();
2627     __ verify_oop(Otos_i);
2628 
2629     do_oop_store(_masm, Rclass, Roffset, 0, Otos_i, G1_scratch, _bs->kind(), false);
2630 
2631     __ ba(checkVolatile);
2632     __ delayed()->tst(Lscratch);
2633 
2634     __ bind(notObj);
2635 
2636     // cmp(Rflags, itos );
2637     __ br(Assembler::notEqual, false, Assembler::pt, notInt);
2638     __ delayed() ->cmp(Rflags, btos );
2639 
2640     // itos
2641     __ pop_i();
2642     __ st(Otos_i, Rclass, Roffset);
2643     __ ba(checkVolatile);
2644     __ delayed()->tst(Lscratch);
2645 
2646     __ bind(notInt);
2647 
2648   } else {
2649     // putfield with int type most likely, check that first
2650     __ cmp(Rflags, itos );
2651     __ br(Assembler::notEqual, false, Assembler::pt, notInt);
2652     __ delayed() ->cmp(Rflags, atos );
2653 
2654     // itos
2655     __ pop_i();
2656     pop_and_check_object(Rclass);
2657     __ st(Otos_i, Rclass, Roffset);
2658     patch_bytecode(Bytecodes::_fast_iputfield, G3_scratch, G4_scratch);
2659     __ ba(checkVolatile);
2660     __ delayed()->tst(Lscratch);
2661 
2662     __ bind(notInt);
2663     // cmp(Rflags, atos );
2664     __ br(Assembler::notEqual, false, Assembler::pt, notObj);
2665     __ delayed() ->cmp(Rflags, btos );
2666 
2667     // atos
2668     __ pop_ptr();
2669     pop_and_check_object(Rclass);
2670     __ verify_oop(Otos_i);
2671 
2672     do_oop_store(_masm, Rclass, Roffset, 0, Otos_i, G1_scratch, _bs->kind(), false);
2673 
2674     patch_bytecode(Bytecodes::_fast_aputfield, G3_scratch, G4_scratch);
2675     __ ba(checkVolatile);
2676     __ delayed()->tst(Lscratch);
2677 
2678     __ bind(notObj);
2679   }
2680 
2681   // cmp(Rflags, btos );
2682   __ br(Assembler::notEqual, false, Assembler::pt, notByte);
2683   __ delayed() ->cmp(Rflags, ltos );
2684 
2685   // btos
2686   __ pop_i();
2687   if (!is_static) pop_and_check_object(Rclass);
2688   __ stb(Otos_i, Rclass, Roffset);
2689   if (!is_static) {
2690     patch_bytecode(Bytecodes::_fast_bputfield, G3_scratch, G4_scratch);
2691   }
2692   __ ba(checkVolatile);
2693   __ delayed()->tst(Lscratch);
2694 
2695   __ bind(notByte);
2696 
2697   // cmp(Rflags, ltos );
2698   __ br(Assembler::notEqual, false, Assembler::pt, notLong);
2699   __ delayed() ->cmp(Rflags, ctos );
2700 
2701   // ltos
2702   __ pop_l();
2703   if (!is_static) pop_and_check_object(Rclass);
2704   __ st_long(Otos_l, Rclass, Roffset);
2705   if (!is_static) {
2706     patch_bytecode(Bytecodes::_fast_lputfield, G3_scratch, G4_scratch);
2707   }
2708   __ ba(checkVolatile);
2709   __ delayed()->tst(Lscratch);
2710 
2711   __ bind(notLong);
2712 
2713   // cmp(Rflags, ctos );
2714   __ br(Assembler::notEqual, false, Assembler::pt, notChar);
2715   __ delayed() ->cmp(Rflags, stos );
2716 
2717   // ctos (char)
2718   __ pop_i();
2719   if (!is_static) pop_and_check_object(Rclass);
2720   __ sth(Otos_i, Rclass, Roffset);
2721   if (!is_static) {
2722     patch_bytecode(Bytecodes::_fast_cputfield, G3_scratch, G4_scratch);
2723   }
2724   __ ba(checkVolatile);
2725   __ delayed()->tst(Lscratch);
2726 
2727   __ bind(notChar);
2728   // cmp(Rflags, stos );
2729   __ br(Assembler::notEqual, false, Assembler::pt, notShort);
2730   __ delayed() ->cmp(Rflags, ftos );
2731 
2732   // stos (char)
2733   __ pop_i();
2734   if (!is_static) pop_and_check_object(Rclass);
2735   __ sth(Otos_i, Rclass, Roffset);
2736   if (!is_static) {
2737     patch_bytecode(Bytecodes::_fast_sputfield, G3_scratch, G4_scratch);
2738   }
2739   __ ba(checkVolatile);
2740   __ delayed()->tst(Lscratch);
2741 
2742   __ bind(notShort);
2743   // cmp(Rflags, ftos );
2744   __ br(Assembler::notZero, false, Assembler::pt, notFloat);
2745   __ delayed()->nop();
2746 
2747   // ftos
2748   __ pop_f();
2749   if (!is_static) pop_and_check_object(Rclass);
2750   __ stf(FloatRegisterImpl::S, Ftos_f, Rclass, Roffset);
2751   if (!is_static) {
2752     patch_bytecode(Bytecodes::_fast_fputfield, G3_scratch, G4_scratch);
2753   }
2754   __ ba(checkVolatile);
2755   __ delayed()->tst(Lscratch);
2756 
2757   __ bind(notFloat);
2758 
2759   // dtos
2760   __ pop_d();
2761   if (!is_static) pop_and_check_object(Rclass);
2762   __ stf(FloatRegisterImpl::D, Ftos_d, Rclass, Roffset);
2763   if (!is_static) {
2764     patch_bytecode(Bytecodes::_fast_dputfield, G3_scratch, G4_scratch);
2765   }
2766 
2767   __ bind(checkVolatile);
2768   __ tst(Lscratch);
2769 
2770   if (__ membar_has_effect(write_bits)) {
2771     // __ tst(Lscratch); in delay slot
2772     __ br(Assembler::zero, false, Assembler::pt, exit);
2773     __ delayed()->nop();
2774     volatile_barrier(Assembler::StoreLoad);
2775     __ bind(exit);
2776   }
2777 }
2778 
2779 void TemplateTable::fast_storefield(TosState state) {
2780   transition(state, vtos);
2781   Register Rcache = G3_scratch;
2782   Register Rclass = Rcache;
2783   Register Roffset= G4_scratch;
2784   Register Rflags = G1_scratch;
2785   ByteSize cp_base_offset = constantPoolCacheOopDesc::base_offset();
2786 
2787   jvmti_post_fast_field_mod();
2788 
2789   __ get_cache_and_index_at_bcp(Rcache, G4_scratch, 1);
2790 
2791   Assembler::Membar_mask_bits read_bits =
2792     Assembler::Membar_mask_bits(Assembler::LoadStore | Assembler::StoreStore);
2793   Assembler::Membar_mask_bits write_bits = Assembler::StoreLoad;
2794 
2795   Label notVolatile, checkVolatile, exit;
2796   if (__ membar_has_effect(read_bits) || __ membar_has_effect(write_bits)) {
2797     __ ld_ptr(Rcache, cp_base_offset + ConstantPoolCacheEntry::flags_offset(), Rflags);
2798     __ set((1 << ConstantPoolCacheEntry::volatileField), Lscratch);
2799     __ and3(Rflags, Lscratch, Lscratch);
2800     if (__ membar_has_effect(read_bits)) {
2801       __ cmp_and_br_short(Lscratch, 0, Assembler::equal, Assembler::pt, notVolatile);


2802       volatile_barrier(read_bits);
2803       __ bind(notVolatile);
2804     }
2805   }
2806 
2807   __ ld_ptr(Rcache, cp_base_offset + ConstantPoolCacheEntry::f2_offset(), Roffset);
2808   pop_and_check_object(Rclass);
2809 
2810   switch (bytecode()) {
2811     case Bytecodes::_fast_bputfield: __ stb(Otos_i, Rclass, Roffset); break;
2812     case Bytecodes::_fast_cputfield: /* fall through */
2813     case Bytecodes::_fast_sputfield: __ sth(Otos_i, Rclass, Roffset); break;
2814     case Bytecodes::_fast_iputfield: __ st(Otos_i, Rclass, Roffset);  break;
2815     case Bytecodes::_fast_lputfield: __ st_long(Otos_l, Rclass, Roffset); break;
2816     case Bytecodes::_fast_fputfield:
2817       __ stf(FloatRegisterImpl::S, Ftos_f, Rclass, Roffset);
2818       break;
2819     case Bytecodes::_fast_dputfield:
2820       __ stf(FloatRegisterImpl::D, Ftos_d, Rclass, Roffset);
2821       break;
2822     case Bytecodes::_fast_aputfield:
2823       do_oop_store(_masm, Rclass, Roffset, 0, Otos_i, G1_scratch, _bs->kind(), false);
2824       break;
2825     default:
2826       ShouldNotReachHere();
2827   }
2828 
2829   if (__ membar_has_effect(write_bits)) {
2830     __ cmp_and_br_short(Lscratch, 0, Assembler::equal, Assembler::pt, exit);


2831     volatile_barrier(Assembler::StoreLoad);
2832     __ bind(exit);
2833   }
2834 }
2835 
2836 
2837 void TemplateTable::putfield(int byte_no) {
2838   putfield_or_static(byte_no, false);
2839 }
2840 
2841 void TemplateTable::putstatic(int byte_no) {
2842   putfield_or_static(byte_no, true);
2843 }
2844 
2845 
2846 void TemplateTable::fast_xaccess(TosState state) {
2847   transition(vtos, state);
2848   Register Rcache = G3_scratch;
2849   Register Roffset = G4_scratch;
2850   Register Rflags  = G4_scratch;
2851   Register Rreceiver = Lscratch;
2852 
2853   __ ld_ptr(Llocals, 0, Rreceiver);
2854 
2855   // access constant pool cache  (is resolved)
2856   __ get_cache_and_index_at_bcp(Rcache, G4_scratch, 2);
2857   __ ld_ptr(Rcache, constantPoolCacheOopDesc::base_offset() + ConstantPoolCacheEntry::f2_offset(), Roffset);
2858   __ add(Lbcp, 1, Lbcp);       // needed to report exception at the correct bcp
2859 
2860   __ verify_oop(Rreceiver);
2861   __ null_check(Rreceiver);
2862   if (state == atos) {
2863     __ load_heap_oop(Rreceiver, Roffset, Otos_i);
2864   } else if (state == itos) {
2865     __ ld (Rreceiver, Roffset, Otos_i) ;
2866   } else if (state == ftos) {
2867     __ ldf(FloatRegisterImpl::S, Rreceiver, Roffset, Ftos_f);
2868   } else {
2869     ShouldNotReachHere();
2870   }
2871 
2872   Assembler::Membar_mask_bits membar_bits =
2873     Assembler::Membar_mask_bits(Assembler::LoadLoad | Assembler::LoadStore);
2874   if (__ membar_has_effect(membar_bits)) {
2875 
2876     // Get is_volatile value in Rflags and check if membar is needed
2877     __ ld_ptr(Rcache, constantPoolCacheOopDesc::base_offset() + ConstantPoolCacheEntry::flags_offset(), Rflags);
2878 
2879     // Test volatile
2880     Label notVolatile;
2881     __ set((1 << ConstantPoolCacheEntry::volatileField), Lscratch);
2882     __ btst(Rflags, Lscratch);
2883     __ br(Assembler::zero, false, Assembler::pt, notVolatile);
2884     __ delayed()->nop();
2885     volatile_barrier(membar_bits);
2886     __ bind(notVolatile);
2887   }
2888 
2889   __ interp_verify_oop(Otos_i, state, __FILE__, __LINE__);
2890   __ sub(Lbcp, 1, Lbcp);
2891 }
2892 
2893 //----------------------------------------------------------------------------------------------------
2894 // Calls
2895 
2896 void TemplateTable::count_calls(Register method, Register temp) {
2897   // implemented elsewhere
2898   ShouldNotReachHere();
2899 }
2900 
2901 void TemplateTable::generate_vtable_call(Register Rrecv, Register Rindex, Register Rret) {
2902   Register Rtemp = G4_scratch;
2903   Register Rcall = Rindex;
2904   assert_different_registers(Rcall, G5_method, Gargs, Rret);
2905 
2906   // get target methodOop & entry point
2907   const int base = instanceKlass::vtable_start_offset() * wordSize;
2908   if (vtableEntry::size() % 3 == 0) {
2909     // scale the vtable index by 12:
2910     int one_third = vtableEntry::size() / 3;
2911     __ sll(Rindex, exact_log2(one_third * 1 * wordSize), Rtemp);
2912     __ sll(Rindex, exact_log2(one_third * 2 * wordSize), Rindex);
2913     __ add(Rindex, Rtemp, Rindex);
2914   } else {
2915     // scale the vtable index by 8:
2916     __ sll(Rindex, exact_log2(vtableEntry::size() * wordSize), Rindex);
2917   }
2918 
2919   __ add(Rrecv, Rindex, Rrecv);
2920   __ ld_ptr(Rrecv, base + vtableEntry::method_offset_in_bytes(), G5_method);
2921 
2922   __ call_from_interpreter(Rcall, Gargs, Rret);
2923 }
2924 
2925 void TemplateTable::invokevirtual(int byte_no) {
2926   transition(vtos, vtos);
2927   assert(byte_no == f2_byte, "use this argument");
2928 
2929   Register Rscratch = G3_scratch;
2930   Register Rtemp = G4_scratch;
2931   Register Rret = Lscratch;
2932   Register Rrecv = G5_method;
2933   Label notFinal;
2934 
2935   load_invoke_cp_cache_entry(byte_no, G5_method, noreg, Rret, true, false, false);
2936   __ mov(SP, O5_savedSP); // record SP that we wanted the callee to restore
2937 
2938   // Check for vfinal
2939   __ set((1 << ConstantPoolCacheEntry::vfinalMethod), G4_scratch);
2940   __ btst(Rret, G4_scratch);
2941   __ br(Assembler::zero, false, Assembler::pt, notFinal);
2942   __ delayed()->and3(Rret, 0xFF, G4_scratch);      // gets number of parameters
2943 
2944   patch_bytecode(Bytecodes::_fast_invokevfinal, Rscratch, Rtemp);
2945 
2946   invokevfinal_helper(Rscratch, Rret);
2947 
2948   __ bind(notFinal);
2949 
2950   __ mov(G5_method, Rscratch);  // better scratch register
2951   __ load_receiver(G4_scratch, O0);  // gets receiverOop
2952   // receiver is in O0
2953   __ verify_oop(O0);
2954 
2955   // get return address
2956   AddressLiteral table(Interpreter::return_3_addrs_by_index_table());
2957   __ set(table, Rtemp);
2958   __ srl(Rret, ConstantPoolCacheEntry::tosBits, Rret);          // get return type
2959   // Make sure we don't need to mask Rret for tosBits after the above shift
2960   ConstantPoolCacheEntry::verify_tosBits();
2961   __ sll(Rret,  LogBytesPerWord, Rret);
2962   __ ld_ptr(Rtemp, Rret, Rret);         // get return address
2963 
2964   // get receiver klass
2965   __ null_check(O0, oopDesc::klass_offset_in_bytes());
2966   __ load_klass(O0, Rrecv);
2967   __ verify_oop(Rrecv);
2968 
2969   __ profile_virtual_call(Rrecv, O4);
2970 
2971   generate_vtable_call(Rrecv, Rscratch, Rret);
2972 }
2973 
2974 void TemplateTable::fast_invokevfinal(int byte_no) {
2975   transition(vtos, vtos);
2976   assert(byte_no == f2_byte, "use this argument");
2977 
2978   load_invoke_cp_cache_entry(byte_no, G5_method, noreg, Lscratch, true,
2979                              /*is_invokevfinal*/true, false);
2980   __ mov(SP, O5_savedSP); // record SP that we wanted the callee to restore
2981   invokevfinal_helper(G3_scratch, Lscratch);
2982 }
2983 
2984 void TemplateTable::invokevfinal_helper(Register Rscratch, Register Rret) {
2985   Register Rtemp = G4_scratch;
2986 
2987   __ verify_oop(G5_method);
2988 
2989   // Load receiver from stack slot
2990   __ lduh(G5_method, in_bytes(methodOopDesc::size_of_parameters_offset()), G4_scratch);
2991   __ load_receiver(G4_scratch, O0);
2992 
2993   // receiver NULL check
2994   __ null_check(O0);
2995 
2996   __ profile_final_call(O4);
2997 
2998   // get return address
2999   AddressLiteral table(Interpreter::return_3_addrs_by_index_table());
3000   __ set(table, Rtemp);
3001   __ srl(Rret, ConstantPoolCacheEntry::tosBits, Rret);          // get return type
3002   // Make sure we don't need to mask Rret for tosBits after the above shift
3003   ConstantPoolCacheEntry::verify_tosBits();
3004   __ sll(Rret,  LogBytesPerWord, Rret);
3005   __ ld_ptr(Rtemp, Rret, Rret);         // get return address
3006 
3007 
3008   // do the call
3009   __ call_from_interpreter(Rscratch, Gargs, Rret);
3010 }
3011 
3012 void TemplateTable::invokespecial(int byte_no) {
3013   transition(vtos, vtos);
3014   assert(byte_no == f1_byte, "use this argument");
3015 
3016   Register Rscratch = G3_scratch;
3017   Register Rtemp = G4_scratch;
3018   Register Rret = Lscratch;
3019 
3020   load_invoke_cp_cache_entry(byte_no, G5_method, noreg, Rret, /*virtual*/ false, false, false);
3021   __ mov(SP, O5_savedSP); // record SP that we wanted the callee to restore
3022 
3023   __ verify_oop(G5_method);
3024 
3025   __ lduh(G5_method, in_bytes(methodOopDesc::size_of_parameters_offset()), G4_scratch);
3026   __ load_receiver(G4_scratch, O0);
3027 
3028   // receiver NULL check
3029   __ null_check(O0);
3030 
3031   __ profile_call(O4);
3032 
3033   // get return address
3034   AddressLiteral table(Interpreter::return_3_addrs_by_index_table());
3035   __ set(table, Rtemp);
3036   __ srl(Rret, ConstantPoolCacheEntry::tosBits, Rret);          // get return type
3037   // Make sure we don't need to mask Rret for tosBits after the above shift
3038   ConstantPoolCacheEntry::verify_tosBits();
3039   __ sll(Rret,  LogBytesPerWord, Rret);
3040   __ ld_ptr(Rtemp, Rret, Rret);         // get return address
3041 
3042   // do the call
3043   __ call_from_interpreter(Rscratch, Gargs, Rret);
3044 }
3045 
3046 void TemplateTable::invokestatic(int byte_no) {
3047   transition(vtos, vtos);
3048   assert(byte_no == f1_byte, "use this argument");
3049 
3050   Register Rscratch = G3_scratch;
3051   Register Rtemp = G4_scratch;
3052   Register Rret = Lscratch;
3053 
3054   load_invoke_cp_cache_entry(byte_no, G5_method, noreg, Rret, /*virtual*/ false, false, false);
3055   __ mov(SP, O5_savedSP); // record SP that we wanted the callee to restore
3056 
3057   __ verify_oop(G5_method);
3058 
3059   __ profile_call(O4);
3060 
3061   // get return address
3062   AddressLiteral table(Interpreter::return_3_addrs_by_index_table());
3063   __ set(table, Rtemp);
3064   __ srl(Rret, ConstantPoolCacheEntry::tosBits, Rret);          // get return type
3065   // Make sure we don't need to mask Rret for tosBits after the above shift
3066   ConstantPoolCacheEntry::verify_tosBits();
3067   __ sll(Rret,  LogBytesPerWord, Rret);
3068   __ ld_ptr(Rtemp, Rret, Rret);         // get return address
3069 
3070   // do the call
3071   __ call_from_interpreter(Rscratch, Gargs, Rret);
3072 }
3073 
3074 
3075 void TemplateTable::invokeinterface_object_method(Register RklassOop,
3076                                                   Register Rcall,
3077                                                   Register Rret,
3078                                                   Register Rflags) {
3079   Register Rscratch = G4_scratch;
3080   Register Rindex = Lscratch;
3081 
3082   assert_different_registers(Rscratch, Rindex, Rret);
3083 
3084   Label notFinal;
3085 
3086   // Check for vfinal
3087   __ set((1 << ConstantPoolCacheEntry::vfinalMethod), Rscratch);
3088   __ btst(Rflags, Rscratch);
3089   __ br(Assembler::zero, false, Assembler::pt, notFinal);
3090   __ delayed()->nop();
3091 
3092   __ profile_final_call(O4);
3093 
3094   // do the call - the index (f2) contains the methodOop
3095   assert_different_registers(G5_method, Gargs, Rcall);
3096   __ mov(Rindex, G5_method);
3097   __ call_from_interpreter(Rcall, Gargs, Rret);
3098   __ bind(notFinal);
3099 
3100   __ profile_virtual_call(RklassOop, O4);
3101   generate_vtable_call(RklassOop, Rindex, Rret);
3102 }
3103 
3104 
3105 void TemplateTable::invokeinterface(int byte_no) {
3106   transition(vtos, vtos);
3107   assert(byte_no == f1_byte, "use this argument");
3108 
3109   Register Rscratch = G4_scratch;
3110   Register Rret = G3_scratch;
3111   Register Rindex = Lscratch;
3112   Register Rinterface = G1_scratch;
3113   Register RklassOop = G5_method;
3114   Register Rflags = O1;
3115   assert_different_registers(Rscratch, G5_method);
3116 
3117   load_invoke_cp_cache_entry(byte_no, Rinterface, Rindex, Rflags, /*virtual*/ false, false, false);
3118   __ mov(SP, O5_savedSP); // record SP that we wanted the callee to restore
3119 
3120   // get receiver
3121   __ and3(Rflags, 0xFF, Rscratch);       // gets number of parameters
3122   __ load_receiver(Rscratch, O0);
3123   __ verify_oop(O0);
3124 
3125   __ mov(Rflags, Rret);
3126 
3127   // get return address
3128   AddressLiteral table(Interpreter::return_5_addrs_by_index_table());
3129   __ set(table, Rscratch);
3130   __ srl(Rret, ConstantPoolCacheEntry::tosBits, Rret);          // get return type
3131   // Make sure we don't need to mask Rret for tosBits after the above shift
3132   ConstantPoolCacheEntry::verify_tosBits();
3133   __ sll(Rret,  LogBytesPerWord, Rret);
3134   __ ld_ptr(Rscratch, Rret, Rret);      // get return address
3135 
3136   // get receiver klass
3137   __ null_check(O0, oopDesc::klass_offset_in_bytes());
3138   __ load_klass(O0, RklassOop);
3139   __ verify_oop(RklassOop);
3140 
3141   // Special case of invokeinterface called for virtual method of
3142   // java.lang.Object.  See cpCacheOop.cpp for details.
3143   // This code isn't produced by javac, but could be produced by
3144   // another compliant java compiler.
3145   Label notMethod;
3146   __ set((1 << ConstantPoolCacheEntry::methodInterface), Rscratch);
3147   __ btst(Rflags, Rscratch);
3148   __ br(Assembler::zero, false, Assembler::pt, notMethod);
3149   __ delayed()->nop();
3150 
3151   invokeinterface_object_method(RklassOop, Rinterface, Rret, Rflags);
3152 
3153   __ bind(notMethod);
3154 
3155   __ profile_virtual_call(RklassOop, O4);
3156 
3157   //
3158   // find entry point to call
3159   //
3160 
3161   // compute start of first itableOffsetEntry (which is at end of vtable)
3162   const int base = instanceKlass::vtable_start_offset() * wordSize;
3163   Label search;
3164   Register Rtemp = Rflags;
3165 
3166   __ ld(RklassOop, instanceKlass::vtable_length_offset() * wordSize, Rtemp);
3167   if (align_object_offset(1) > 1) {
3168     __ round_to(Rtemp, align_object_offset(1));
3169   }
3170   __ sll(Rtemp, LogBytesPerWord, Rtemp);   // Rscratch *= 4;
3171   if (Assembler::is_simm13(base)) {
3172     __ add(Rtemp, base, Rtemp);
3173   } else {
3174     __ set(base, Rscratch);
3175     __ add(Rscratch, Rtemp, Rtemp);
3176   }
3177   __ add(RklassOop, Rtemp, Rscratch);
3178 
3179   __ bind(search);
3180 
3181   __ ld_ptr(Rscratch, itableOffsetEntry::interface_offset_in_bytes(), Rtemp);
3182   {
3183     Label ok;
3184 
3185     // Check that entry is non-null.  Null entries are probably a bytecode
3186     // problem.  If the interface isn't implemented by the receiver class,
3187     // the VM should throw IncompatibleClassChangeError.  linkResolver checks
3188     // this too but that's only if the entry isn't already resolved, so we
3189     // need to check again.
3190     __ br_notnull_short( Rtemp, Assembler::pt, ok);

3191     call_VM(noreg, CAST_FROM_FN_PTR(address, InterpreterRuntime::throw_IncompatibleClassChangeError));
3192     __ should_not_reach_here();
3193     __ bind(ok);
3194     __ verify_oop(Rtemp);
3195   }
3196 
3197   __ verify_oop(Rinterface);
3198 
3199   __ cmp(Rinterface, Rtemp);
3200   __ brx(Assembler::notEqual, true, Assembler::pn, search);
3201   __ delayed()->add(Rscratch, itableOffsetEntry::size() * wordSize, Rscratch);
3202 
3203   // entry found and Rscratch points to it
3204   __ ld(Rscratch, itableOffsetEntry::offset_offset_in_bytes(), Rscratch);
3205 
3206   assert(itableMethodEntry::method_offset_in_bytes() == 0, "adjust instruction below");
3207   __ sll(Rindex, exact_log2(itableMethodEntry::size() * wordSize), Rindex);       // Rindex *= 8;
3208   __ add(Rscratch, Rindex, Rscratch);
3209   __ ld_ptr(RklassOop, Rscratch, G5_method);
3210 
3211   // Check for abstract method error.
3212   {
3213     Label ok;
3214     __ br_notnull_short(G5_method, Assembler::pt, ok);


3215     call_VM(noreg, CAST_FROM_FN_PTR(address, InterpreterRuntime::throw_AbstractMethodError));
3216     __ should_not_reach_here();
3217     __ bind(ok);
3218   }
3219 
3220   Register Rcall = Rinterface;
3221   assert_different_registers(Rcall, G5_method, Gargs, Rret);
3222 
3223   __ verify_oop(G5_method);
3224   __ call_from_interpreter(Rcall, Gargs, Rret);
3225 
3226 }
3227 
3228 
3229 void TemplateTable::invokedynamic(int byte_no) {
3230   transition(vtos, vtos);
3231   assert(byte_no == f1_oop, "use this argument");
3232 
3233   if (!EnableInvokeDynamic) {
3234     // We should not encounter this bytecode if !EnableInvokeDynamic.
3235     // The verifier will stop it.  However, if we get past the verifier,
3236     // this will stop the thread in a reasonable way, without crashing the JVM.
3237     __ call_VM(noreg, CAST_FROM_FN_PTR(address,
3238                      InterpreterRuntime::throw_IncompatibleClassChangeError));
3239     // the call_VM checks for exception, so we should never return here.
3240     __ should_not_reach_here();
3241     return;
3242   }
3243 
3244   // G5: CallSite object (f1)
3245   // XX: unused (f2)
3246   // XX: flags (unused)
3247 
3248   Register G5_callsite = G5_method;
3249   Register Rscratch    = G3_scratch;
3250   Register Rtemp       = G1_scratch;
3251   Register Rret        = Lscratch;
3252 
3253   load_invoke_cp_cache_entry(byte_no, G5_callsite, noreg, Rret,
3254                              /*virtual*/ false, /*vfinal*/ false, /*indy*/ true);
3255   __ mov(SP, O5_savedSP);  // record SP that we wanted the callee to restore
3256 
3257   // profile this call
3258   __ profile_call(O4);
3259 
3260   // get return address
3261   AddressLiteral table(Interpreter::return_5_addrs_by_index_table());
3262   __ set(table, Rtemp);
3263   __ srl(Rret, ConstantPoolCacheEntry::tosBits, Rret);  // get return type
3264   // Make sure we don't need to mask Rret for tosBits after the above shift
3265   ConstantPoolCacheEntry::verify_tosBits();
3266   __ sll(Rret, LogBytesPerWord, Rret);
3267   __ ld_ptr(Rtemp, Rret, Rret);  // get return address
3268 
3269   __ verify_oop(G5_callsite);
3270   __ load_heap_oop(G5_callsite, __ delayed_value(java_lang_invoke_CallSite::target_offset_in_bytes, Rscratch), G3_method_handle);
3271   __ null_check(G3_method_handle);
3272   __ verify_oop(G3_method_handle);
3273 
3274   // Adjust Rret first so Llast_SP can be same as Rret
3275   __ add(Rret, -frame::pc_return_offset, O7);
3276   __ add(Lesp, BytesPerWord, Gargs);  // setup parameter pointer
3277   __ jump_to_method_handle_entry(G3_method_handle, Rtemp, /* emit_delayed_nop */ false);
3278   // Record SP so we can remove any stack space allocated by adapter transition
3279   __ delayed()->mov(SP, Llast_SP);
3280 }
3281 
3282 
3283 //----------------------------------------------------------------------------------------------------
3284 // Allocation
3285 
3286 void TemplateTable::_new() {
3287   transition(vtos, atos);
3288 
3289   Label slow_case;
3290   Label done;
3291   Label initialize_header;
3292   Label initialize_object;  // including clearing the fields
3293 
3294   Register RallocatedObject = Otos_i;
3295   Register RinstanceKlass = O1;
3296   Register Roffset = O3;
3297   Register Rscratch = O4;
3298 
3299   __ get_2_byte_integer_at_bcp(1, Rscratch, Roffset, InterpreterMacroAssembler::Unsigned);
3300   __ get_cpool_and_tags(Rscratch, G3_scratch);
3301   // make sure the class we're about to instantiate has been resolved
3302   // This is done before loading instanceKlass to be consistent with the order
3303   // how Constant Pool is updated (see constantPoolOopDesc::klass_at_put)
3304   __ add(G3_scratch, typeArrayOopDesc::header_size(T_BYTE) * wordSize, G3_scratch);
3305   __ ldub(G3_scratch, Roffset, G3_scratch);
3306   __ cmp(G3_scratch, JVM_CONSTANT_Class);
3307   __ br(Assembler::notEqual, false, Assembler::pn, slow_case);
3308   __ delayed()->sll(Roffset, LogBytesPerWord, Roffset);
3309   // get instanceKlass
3310   //__ sll(Roffset, LogBytesPerWord, Roffset);        // executed in delay slot
3311   __ add(Roffset, sizeof(constantPoolOopDesc), Roffset);
3312   __ ld_ptr(Rscratch, Roffset, RinstanceKlass);
3313 
3314   // make sure klass is fully initialized:
3315   __ ld(RinstanceKlass, instanceKlass::init_state_offset_in_bytes() + sizeof(oopDesc), G3_scratch);
3316   __ cmp(G3_scratch, instanceKlass::fully_initialized);
3317   __ br(Assembler::notEqual, false, Assembler::pn, slow_case);
3318   __ delayed()->ld(RinstanceKlass, Klass::layout_helper_offset_in_bytes() + sizeof(oopDesc), Roffset);
3319 
3320   // get instance_size in instanceKlass (already aligned)
3321   //__ ld(RinstanceKlass, Klass::layout_helper_offset_in_bytes() + sizeof(oopDesc), Roffset);
3322 
3323   // make sure klass does not have has_finalizer, or is abstract, or interface or java/lang/Class
3324   __ btst(Klass::_lh_instance_slow_path_bit, Roffset);
3325   __ br(Assembler::notZero, false, Assembler::pn, slow_case);
3326   __ delayed()->nop();
3327 
3328   // allocate the instance
3329   // 1) Try to allocate in the TLAB
3330   // 2) if fail, and the TLAB is not full enough to discard, allocate in the shared Eden
3331   // 3) if the above fails (or is not applicable), go to a slow case
3332   // (creates a new TLAB, etc.)
3333 
3334   const bool allow_shared_alloc =
3335     Universe::heap()->supports_inline_contig_alloc() && !CMSIncrementalMode;
3336 
3337   if(UseTLAB) {
3338     Register RoldTopValue = RallocatedObject;
3339     Register RtopAddr = G3_scratch, RtlabWasteLimitValue = G3_scratch;
3340     Register RnewTopValue = G1_scratch;
3341     Register RendValue = Rscratch;
3342     Register RfreeValue = RnewTopValue;
3343 
3344     // check if we can allocate in the TLAB
3345     __ ld_ptr(G2_thread, in_bytes(JavaThread::tlab_top_offset()), RoldTopValue); // sets up RalocatedObject
3346     __ ld_ptr(G2_thread, in_bytes(JavaThread::tlab_end_offset()), RendValue);
3347     __ add(RoldTopValue, Roffset, RnewTopValue);
3348 
3349     // if there is enough space, we do not CAS and do not clear
3350     __ cmp(RnewTopValue, RendValue);
3351     if(ZeroTLAB) {
3352       // the fields have already been cleared
3353       __ brx(Assembler::lessEqualUnsigned, true, Assembler::pt, initialize_header);
3354     } else {
3355       // initialize both the header and fields
3356       __ brx(Assembler::lessEqualUnsigned, true, Assembler::pt, initialize_object);
3357     }
3358     __ delayed()->st_ptr(RnewTopValue, G2_thread, in_bytes(JavaThread::tlab_top_offset()));
3359 
3360     if (allow_shared_alloc) {
3361       // Check if tlab should be discarded (refill_waste_limit >= free)
3362       __ ld_ptr(G2_thread, in_bytes(JavaThread::tlab_refill_waste_limit_offset()), RtlabWasteLimitValue);
3363       __ sub(RendValue, RoldTopValue, RfreeValue);
3364 #ifdef _LP64
3365       __ srlx(RfreeValue, LogHeapWordSize, RfreeValue);
3366 #else
3367       __ srl(RfreeValue, LogHeapWordSize, RfreeValue);
3368 #endif
3369       __ cmp_and_brx_short(RtlabWasteLimitValue, RfreeValue, Assembler::greaterEqualUnsigned, Assembler::pt, slow_case); // tlab waste is small


3370 
3371       // increment waste limit to prevent getting stuck on this slow path
3372       __ add(RtlabWasteLimitValue, ThreadLocalAllocBuffer::refill_waste_limit_increment(), RtlabWasteLimitValue);
3373       __ st_ptr(RtlabWasteLimitValue, G2_thread, in_bytes(JavaThread::tlab_refill_waste_limit_offset()));
3374     } else {
3375       // No allocation in the shared eden.
3376       __ ba_short(slow_case);

3377     }
3378   }
3379 
3380   // Allocation in the shared Eden
3381   if (allow_shared_alloc) {
3382     Register RoldTopValue = G1_scratch;
3383     Register RtopAddr = G3_scratch;
3384     Register RnewTopValue = RallocatedObject;
3385     Register RendValue = Rscratch;
3386 
3387     __ set((intptr_t)Universe::heap()->top_addr(), RtopAddr);
3388 
3389     Label retry;
3390     __ bind(retry);
3391     __ set((intptr_t)Universe::heap()->end_addr(), RendValue);
3392     __ ld_ptr(RendValue, 0, RendValue);
3393     __ ld_ptr(RtopAddr, 0, RoldTopValue);
3394     __ add(RoldTopValue, Roffset, RnewTopValue);
3395 
3396     // RnewTopValue contains the top address after the new object
3397     // has been allocated.
3398     __ cmp_and_brx_short(RnewTopValue, RendValue, Assembler::greaterUnsigned, Assembler::pn, slow_case);


3399 
3400     __ casx_under_lock(RtopAddr, RoldTopValue, RnewTopValue,
3401       VM_Version::v9_instructions_work() ? NULL :
3402       (address)StubRoutines::Sparc::atomic_memory_operation_lock_addr());
3403 
3404     // if someone beat us on the allocation, try again, otherwise continue
3405     __ cmp_and_brx_short(RoldTopValue, RnewTopValue, Assembler::notEqual, Assembler::pn, retry);


3406 
3407     // bump total bytes allocated by this thread
3408     // RoldTopValue and RtopAddr are dead, so can use G1 and G3
3409     __ incr_allocated_bytes(Roffset, G1_scratch, G3_scratch);
3410   }
3411 
3412   if (UseTLAB || Universe::heap()->supports_inline_contig_alloc()) {
3413     // clear object fields
3414     __ bind(initialize_object);
3415     __ deccc(Roffset, sizeof(oopDesc));
3416     __ br(Assembler::zero, false, Assembler::pt, initialize_header);
3417     __ delayed()->add(RallocatedObject, sizeof(oopDesc), G3_scratch);
3418 
3419     // initialize remaining object fields
3420     { Label loop;
3421       __ subcc(Roffset, wordSize, Roffset);
3422       __ bind(loop);
3423       //__ subcc(Roffset, wordSize, Roffset);      // executed above loop or in delay slot
3424       __ st_ptr(G0, G3_scratch, Roffset);
3425       __ br(Assembler::notEqual, false, Assembler::pt, loop);
3426       __ delayed()->subcc(Roffset, wordSize, Roffset);
3427     }
3428     __ ba_short(initialize_header);

3429   }
3430 
3431   // slow case
3432   __ bind(slow_case);
3433   __ get_2_byte_integer_at_bcp(1, G3_scratch, O2, InterpreterMacroAssembler::Unsigned);
3434   __ get_constant_pool(O1);
3435 
3436   call_VM(Otos_i, CAST_FROM_FN_PTR(address, InterpreterRuntime::_new), O1, O2);
3437 
3438   __ ba_short(done);

3439 
3440   // Initialize the header: mark, klass
3441   __ bind(initialize_header);
3442 
3443   if (UseBiasedLocking) {
3444     __ ld_ptr(RinstanceKlass, Klass::prototype_header_offset_in_bytes() + sizeof(oopDesc), G4_scratch);
3445   } else {
3446     __ set((intptr_t)markOopDesc::prototype(), G4_scratch);
3447   }
3448   __ st_ptr(G4_scratch, RallocatedObject, oopDesc::mark_offset_in_bytes());       // mark
3449   __ store_klass_gap(G0, RallocatedObject);         // klass gap if compressed
3450   __ store_klass(RinstanceKlass, RallocatedObject); // klass (last for cms)
3451 
3452   {
3453     SkipIfEqual skip_if(
3454       _masm, G4_scratch, &DTraceAllocProbes, Assembler::zero);
3455     // Trigger dtrace event
3456     __ push(atos);
3457     __ call_VM_leaf(noreg,
3458        CAST_FROM_FN_PTR(address, SharedRuntime::dtrace_object_alloc), O0);
3459     __ pop(atos);
3460   }
3461 
3462   // continue
3463   __ bind(done);
3464 }
3465 
3466 
3467 
3468 void TemplateTable::newarray() {
3469   transition(itos, atos);
3470   __ ldub(Lbcp, 1, O1);
3471      call_VM(Otos_i, CAST_FROM_FN_PTR(address, InterpreterRuntime::newarray), O1, Otos_i);
3472 }
3473 
3474 
3475 void TemplateTable::anewarray() {
3476   transition(itos, atos);
3477   __ get_constant_pool(O1);
3478   __ get_2_byte_integer_at_bcp(1, G4_scratch, O2, InterpreterMacroAssembler::Unsigned);
3479      call_VM(Otos_i, CAST_FROM_FN_PTR(address, InterpreterRuntime::anewarray), O1, O2, Otos_i);
3480 }
3481 
3482 
3483 void TemplateTable::arraylength() {
3484   transition(atos, itos);
3485   Label ok;
3486   __ verify_oop(Otos_i);
3487   __ tst(Otos_i);
3488   __ throw_if_not_1_x( Assembler::notZero, ok );
3489   __ delayed()->ld(Otos_i, arrayOopDesc::length_offset_in_bytes(), Otos_i);
3490   __ throw_if_not_2( Interpreter::_throw_NullPointerException_entry, G3_scratch, ok);
3491 }
3492 
3493 
3494 void TemplateTable::checkcast() {
3495   transition(atos, atos);
3496   Label done, is_null, quicked, cast_ok, resolved;
3497   Register Roffset = G1_scratch;
3498   Register RobjKlass = O5;
3499   Register RspecifiedKlass = O4;
3500 
3501   // Check for casting a NULL
3502   __ br_null_short(Otos_i, Assembler::pn, is_null);

3503 
3504   // Get value klass in RobjKlass
3505   __ load_klass(Otos_i, RobjKlass); // get value klass
3506 
3507   // Get constant pool tag
3508   __ get_2_byte_integer_at_bcp(1, Lscratch, Roffset, InterpreterMacroAssembler::Unsigned);
3509 
3510   // See if the checkcast has been quickened
3511   __ get_cpool_and_tags(Lscratch, G3_scratch);
3512   __ add(G3_scratch, typeArrayOopDesc::header_size(T_BYTE) * wordSize, G3_scratch);
3513   __ ldub(G3_scratch, Roffset, G3_scratch);
3514   __ cmp(G3_scratch, JVM_CONSTANT_Class);
3515   __ br(Assembler::equal, true, Assembler::pt, quicked);
3516   __ delayed()->sll(Roffset, LogBytesPerWord, Roffset);
3517 
3518   __ push_ptr(); // save receiver for result, and for GC
3519   call_VM(RspecifiedKlass, CAST_FROM_FN_PTR(address, InterpreterRuntime::quicken_io_cc) );
3520   __ pop_ptr(Otos_i, G3_scratch); // restore receiver
3521 
3522   __ ba_short(resolved);

3523 
3524   // Extract target class from constant pool
3525   __ bind(quicked);
3526   __ add(Roffset, sizeof(constantPoolOopDesc), Roffset);
3527   __ ld_ptr(Lscratch, Roffset, RspecifiedKlass);
3528   __ bind(resolved);
3529   __ load_klass(Otos_i, RobjKlass); // get value klass
3530 
3531   // Generate a fast subtype check.  Branch to cast_ok if no
3532   // failure.  Throw exception if failure.
3533   __ gen_subtype_check( RobjKlass, RspecifiedKlass, G3_scratch, G4_scratch, G1_scratch, cast_ok );
3534 
3535   // Not a subtype; so must throw exception
3536   __ throw_if_not_x( Assembler::never, Interpreter::_throw_ClassCastException_entry, G3_scratch );
3537 
3538   __ bind(cast_ok);
3539 
3540   if (ProfileInterpreter) {
3541     __ ba_short(done);

3542   }
3543   __ bind(is_null);
3544   __ profile_null_seen(G3_scratch);
3545   __ bind(done);
3546 }
3547 
3548 
3549 void TemplateTable::instanceof() {
3550   Label done, is_null, quicked, resolved;
3551   transition(atos, itos);
3552   Register Roffset = G1_scratch;
3553   Register RobjKlass = O5;
3554   Register RspecifiedKlass = O4;
3555 
3556   // Check for casting a NULL
3557   __ br_null_short(Otos_i, Assembler::pt, is_null);

3558 
3559   // Get value klass in RobjKlass
3560   __ load_klass(Otos_i, RobjKlass); // get value klass
3561 
3562   // Get constant pool tag
3563   __ get_2_byte_integer_at_bcp(1, Lscratch, Roffset, InterpreterMacroAssembler::Unsigned);
3564 
3565   // See if the checkcast has been quickened
3566   __ get_cpool_and_tags(Lscratch, G3_scratch);
3567   __ add(G3_scratch, typeArrayOopDesc::header_size(T_BYTE) * wordSize, G3_scratch);
3568   __ ldub(G3_scratch, Roffset, G3_scratch);
3569   __ cmp(G3_scratch, JVM_CONSTANT_Class);
3570   __ br(Assembler::equal, true, Assembler::pt, quicked);
3571   __ delayed()->sll(Roffset, LogBytesPerWord, Roffset);
3572 
3573   __ push_ptr(); // save receiver for result, and for GC
3574   call_VM(RspecifiedKlass, CAST_FROM_FN_PTR(address, InterpreterRuntime::quicken_io_cc) );
3575   __ pop_ptr(Otos_i, G3_scratch); // restore receiver
3576 
3577   __ ba_short(resolved);

3578 

3579   // Extract target class from constant pool
3580   __ bind(quicked);
3581   __ add(Roffset, sizeof(constantPoolOopDesc), Roffset);
3582   __ get_constant_pool(Lscratch);
3583   __ ld_ptr(Lscratch, Roffset, RspecifiedKlass);
3584   __ bind(resolved);
3585   __ load_klass(Otos_i, RobjKlass); // get value klass
3586 
3587   // Generate a fast subtype check.  Branch to cast_ok if no
3588   // failure.  Return 0 if failure.
3589   __ or3(G0, 1, Otos_i);      // set result assuming quick tests succeed
3590   __ gen_subtype_check( RobjKlass, RspecifiedKlass, G3_scratch, G4_scratch, G1_scratch, done );
3591   // Not a subtype; return 0;
3592   __ clr( Otos_i );
3593 
3594   if (ProfileInterpreter) {
3595     __ ba_short(done);

3596   }
3597   __ bind(is_null);
3598   __ profile_null_seen(G3_scratch);
3599   __ bind(done);
3600 }
3601 
3602 void TemplateTable::_breakpoint() {
3603 
3604    // Note: We get here even if we are single stepping..
3605    // jbug inists on setting breakpoints at every bytecode
3606    // even if we are in single step mode.
3607 
3608    transition(vtos, vtos);
3609    // get the unpatched byte code
3610    __ call_VM(noreg, CAST_FROM_FN_PTR(address, InterpreterRuntime::get_original_bytecode_at), Lmethod, Lbcp);
3611    __ mov(O0, Lbyte_code);
3612 
3613    // post the breakpoint event
3614    __ call_VM(noreg, CAST_FROM_FN_PTR(address, InterpreterRuntime::_breakpoint), Lmethod, Lbcp);
3615 
3616    // complete the execution of original bytecode
3617    __ dispatch_normal(vtos);
3618 }
3619 
3620 
3621 //----------------------------------------------------------------------------------------------------
3622 // Exceptions
3623 
3624 void TemplateTable::athrow() {
3625   transition(atos, vtos);
3626 
3627   // This works because exception is cached in Otos_i which is same as O0,
3628   // which is same as what throw_exception_entry_expects
3629   assert(Otos_i == Oexception, "see explanation above");
3630 
3631   __ verify_oop(Otos_i);
3632   __ null_check(Otos_i);
3633   __ throw_if_not_x(Assembler::never, Interpreter::throw_exception_entry(), G3_scratch);
3634 }
3635 
3636 
3637 //----------------------------------------------------------------------------------------------------
3638 // Synchronization
3639 
3640 
3641 // See frame_sparc.hpp for monitor block layout.
3642 // Monitor elements are dynamically allocated by growing stack as needed.
3643 
3644 void TemplateTable::monitorenter() {
3645   transition(atos, vtos);
3646   __ verify_oop(Otos_i);
3647   // Try to acquire a lock on the object
3648   // Repeat until succeeded (i.e., until
3649   // monitorenter returns true).
3650 
3651   {   Label ok;
3652     __ tst(Otos_i);
3653     __ throw_if_not_1_x( Assembler::notZero,  ok);
3654     __ delayed()->mov(Otos_i, Lscratch); // save obj
3655     __ throw_if_not_2( Interpreter::_throw_NullPointerException_entry, G3_scratch, ok);
3656   }
3657 
3658   assert(O0 == Otos_i, "Be sure where the object to lock is");
3659 
3660   // find a free slot in the monitor block
3661 
3662 
3663   // initialize entry pointer
3664   __ clr(O1); // points to free slot or NULL
3665 
3666   {
3667     Label entry, loop, exit;
3668     __ add( __ top_most_monitor(), O2 ); // last one to check
3669     __ ba( entry );
3670     __ delayed()->mov( Lmonitors, O3 ); // first one to check
3671 
3672 
3673     __ bind( loop );
3674 
3675     __ verify_oop(O4);          // verify each monitor's oop
3676     __ tst(O4); // is this entry unused?
3677     if (VM_Version::v9_instructions_work())
3678       __ movcc( Assembler::zero, false, Assembler::ptr_cc, O3, O1);
3679     else {
3680       Label L;
3681       __ br( Assembler::zero, true, Assembler::pn, L );
3682       __ delayed()->mov(O3, O1); // rememeber this one if match
3683       __ bind(L);
3684     }
3685 
3686     __ cmp(O4, O0); // check if current entry is for same object
3687     __ brx( Assembler::equal, false, Assembler::pn, exit );
3688     __ delayed()->inc( O3, frame::interpreter_frame_monitor_size() * wordSize ); // check next one
3689 
3690     __ bind( entry );
3691 
3692     __ cmp( O3, O2 );
3693     __ brx( Assembler::lessEqualUnsigned, true, Assembler::pt, loop );
3694     __ delayed()->ld_ptr(O3, BasicObjectLock::obj_offset_in_bytes(), O4);
3695 
3696     __ bind( exit );
3697   }
3698 
3699   { Label allocated;
3700 
3701     // found free slot?
3702     __ br_notnull_short(O1, Assembler::pn, allocated);

3703 
3704     __ add_monitor_to_stack( false, O2, O3 );
3705     __ mov(Lmonitors, O1);
3706 
3707     __ bind(allocated);
3708   }
3709 
3710   // Increment bcp to point to the next bytecode, so exception handling for async. exceptions work correctly.
3711   // The object has already been poped from the stack, so the expression stack looks correct.
3712   __ inc(Lbcp);
3713 
3714   __ st_ptr(O0, O1, BasicObjectLock::obj_offset_in_bytes()); // store object
3715   __ lock_object(O1, O0);
3716 
3717   // check if there's enough space on the stack for the monitors after locking
3718   __ generate_stack_overflow_check(0);
3719 
3720   // The bcp has already been incremented. Just need to dispatch to next instruction.
3721   __ dispatch_next(vtos);
3722 }
3723 
3724 
3725 void TemplateTable::monitorexit() {
3726   transition(atos, vtos);
3727   __ verify_oop(Otos_i);
3728   __ tst(Otos_i);
3729   __ throw_if_not_x( Assembler::notZero, Interpreter::_throw_NullPointerException_entry, G3_scratch );
3730 
3731   assert(O0 == Otos_i, "just checking");
3732 
3733   { Label entry, loop, found;
3734     __ add( __ top_most_monitor(), O2 ); // last one to check
3735     __ ba(entry);
3736     // use Lscratch to hold monitor elem to check, start with most recent monitor,
3737     // By using a local it survives the call to the C routine.
3738     __ delayed()->mov( Lmonitors, Lscratch );
3739 
3740     __ bind( loop );
3741 
3742     __ verify_oop(O4);          // verify each monitor's oop
3743     __ cmp(O4, O0); // check if current entry is for desired object
3744     __ brx( Assembler::equal, true, Assembler::pt, found );
3745     __ delayed()->mov(Lscratch, O1); // pass found entry as argument to monitorexit
3746 
3747     __ inc( Lscratch, frame::interpreter_frame_monitor_size() * wordSize ); // advance to next
3748 
3749     __ bind( entry );
3750 
3751     __ cmp( Lscratch, O2 );
3752     __ brx( Assembler::lessEqualUnsigned, true, Assembler::pt, loop );
3753     __ delayed()->ld_ptr(Lscratch, BasicObjectLock::obj_offset_in_bytes(), O4);
3754 
3755     call_VM(noreg, CAST_FROM_FN_PTR(address, InterpreterRuntime::throw_illegal_monitor_state_exception));
3756     __ should_not_reach_here();
3757 
3758     __ bind(found);
3759   }
3760   __ unlock_object(O1);
3761 }
3762 
3763 
3764 //----------------------------------------------------------------------------------------------------
3765 // Wide instructions
3766 
3767 void TemplateTable::wide() {
3768   transition(vtos, vtos);
3769   __ ldub(Lbcp, 1, G3_scratch);// get next bc
3770   __ sll(G3_scratch, LogBytesPerWord, G3_scratch);
3771   AddressLiteral ep(Interpreter::_wentry_point);
3772   __ set(ep, G4_scratch);
3773   __ ld_ptr(G4_scratch, G3_scratch, G3_scratch);
3774   __ jmp(G3_scratch, G0);
3775   __ delayed()->nop();
3776   // Note: the Lbcp increment step is part of the individual wide bytecode implementations
3777 }
3778 
3779 
3780 //----------------------------------------------------------------------------------------------------
3781 // Multi arrays
3782 
3783 void TemplateTable::multianewarray() {
3784   transition(vtos, atos);
3785      // put ndims * wordSize into Lscratch
3786   __ ldub( Lbcp,     3,               Lscratch);
3787   __ sll(  Lscratch, Interpreter::logStackElementSize, Lscratch);
3788      // Lesp points past last_dim, so set to O1 to first_dim address
3789   __ add(  Lesp,     Lscratch,        O1);
3790      call_VM(Otos_i, CAST_FROM_FN_PTR(address, InterpreterRuntime::multianewarray), O1);
3791   __ add(  Lesp,     Lscratch,        Lesp); // pop all dimensions off the stack
3792 }
3793 #endif /* !CC_INTERP */
--- EOF ---