1 /*
   2  * Copyright (c) 1997, 2018, 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 "asm/macroAssembler.hpp"
  27 #include "interpreter/interpreter.hpp"
  28 #include "interpreter/interpreterRuntime.hpp"
  29 #include "interpreter/interp_masm.hpp"
  30 #include "interpreter/templateTable.hpp"
  31 #include "memory/universe.hpp"
  32 #include "oops/methodData.hpp"
  33 #include "oops/objArrayKlass.hpp"
  34 #include "oops/oop.inline.hpp"
  35 #include "prims/methodHandles.hpp"
  36 #include "runtime/frame.inline.hpp"
  37 #include "runtime/safepointMechanism.hpp"
  38 #include "runtime/sharedRuntime.hpp"
  39 #include "runtime/stubRoutines.hpp"
  40 #include "runtime/synchronizer.hpp"
  41 #include "utilities/macros.hpp"
  42 
  43 #define __ _masm->
  44 
  45 // Global Register Names
  46 static const Register rbcp     = LP64_ONLY(r13) NOT_LP64(rsi);
  47 static const Register rlocals  = LP64_ONLY(r14) NOT_LP64(rdi);
  48 
  49 // Platform-dependent initialization
  50 void TemplateTable::pd_initialize() {
  51   // No x86 specific initialization
  52 }
  53 
  54 // Address Computation: local variables
  55 static inline Address iaddress(int n) {
  56   return Address(rlocals, Interpreter::local_offset_in_bytes(n));
  57 }
  58 
  59 static inline Address laddress(int n) {
  60   return iaddress(n + 1);
  61 }
  62 
  63 #ifndef _LP64
  64 static inline Address haddress(int n) {
  65   return iaddress(n + 0);
  66 }
  67 #endif
  68 
  69 static inline Address faddress(int n) {
  70   return iaddress(n);
  71 }
  72 
  73 static inline Address daddress(int n) {
  74   return laddress(n);
  75 }
  76 
  77 static inline Address aaddress(int n) {
  78   return iaddress(n);
  79 }
  80 
  81 static inline Address iaddress(Register r) {
  82   return Address(rlocals, r, Address::times_ptr);
  83 }
  84 
  85 static inline Address laddress(Register r) {
  86   return Address(rlocals, r, Address::times_ptr, Interpreter::local_offset_in_bytes(1));
  87 }
  88 
  89 #ifndef _LP64
  90 static inline Address haddress(Register r)       {
  91   return Address(rlocals, r, Interpreter::stackElementScale(), Interpreter::local_offset_in_bytes(0));
  92 }
  93 #endif
  94 
  95 static inline Address faddress(Register r) {
  96   return iaddress(r);
  97 }
  98 
  99 static inline Address daddress(Register r) {
 100   return laddress(r);
 101 }
 102 
 103 static inline Address aaddress(Register r) {
 104   return iaddress(r);
 105 }
 106 
 107 
 108 // expression stack
 109 // (Note: Must not use symmetric equivalents at_rsp_m1/2 since they store
 110 // data beyond the rsp which is potentially unsafe in an MT environment;
 111 // an interrupt may overwrite that data.)
 112 static inline Address at_rsp   () {
 113   return Address(rsp, 0);
 114 }
 115 
 116 // At top of Java expression stack which may be different than esp().  It
 117 // isn't for category 1 objects.
 118 static inline Address at_tos   () {
 119   return Address(rsp,  Interpreter::expr_offset_in_bytes(0));
 120 }
 121 
 122 static inline Address at_tos_p1() {
 123   return Address(rsp,  Interpreter::expr_offset_in_bytes(1));
 124 }
 125 
 126 static inline Address at_tos_p2() {
 127   return Address(rsp,  Interpreter::expr_offset_in_bytes(2));
 128 }
 129 
 130 // Condition conversion
 131 static Assembler::Condition j_not(TemplateTable::Condition cc) {
 132   switch (cc) {
 133   case TemplateTable::equal        : return Assembler::notEqual;
 134   case TemplateTable::not_equal    : return Assembler::equal;
 135   case TemplateTable::less         : return Assembler::greaterEqual;
 136   case TemplateTable::less_equal   : return Assembler::greater;
 137   case TemplateTable::greater      : return Assembler::lessEqual;
 138   case TemplateTable::greater_equal: return Assembler::less;
 139   }
 140   ShouldNotReachHere();
 141   return Assembler::zero;
 142 }
 143 
 144 
 145 
 146 // Miscelaneous helper routines
 147 // Store an oop (or NULL) at the address described by obj.
 148 // If val == noreg this means store a NULL
 149 
 150 
 151 static void do_oop_store(InterpreterMacroAssembler* _masm,
 152                          Address dst,
 153                          Register val,
 154                          DecoratorSet decorators = 0) {
 155   assert(val == noreg || val == rax, "parameter is just for looks");
 156   __ store_heap_oop(dst, val, rdx, rbx, decorators);
 157 }
 158 
 159 static void do_oop_load(InterpreterMacroAssembler* _masm,
 160                         Address src,
 161                         Register dst,
 162                         DecoratorSet decorators = 0) {
 163   __ load_heap_oop(dst, src, rdx, rbx, decorators);
 164 }
 165 
 166 Address TemplateTable::at_bcp(int offset) {
 167   assert(_desc->uses_bcp(), "inconsistent uses_bcp information");
 168   return Address(rbcp, offset);
 169 }
 170 
 171 
 172 void TemplateTable::patch_bytecode(Bytecodes::Code bc, Register bc_reg,
 173                                    Register temp_reg, bool load_bc_into_bc_reg/*=true*/,
 174                                    int byte_no) {
 175   if (!RewriteBytecodes)  return;
 176   Label L_patch_done;
 177 
 178   switch (bc) {
 179   case Bytecodes::_fast_aputfield:
 180   case Bytecodes::_fast_bputfield:
 181   case Bytecodes::_fast_zputfield:
 182   case Bytecodes::_fast_cputfield:
 183   case Bytecodes::_fast_dputfield:
 184   case Bytecodes::_fast_fputfield:
 185   case Bytecodes::_fast_iputfield:
 186   case Bytecodes::_fast_lputfield:
 187   case Bytecodes::_fast_sputfield:
 188     {
 189       // We skip bytecode quickening for putfield instructions when
 190       // the put_code written to the constant pool cache is zero.
 191       // This is required so that every execution of this instruction
 192       // calls out to InterpreterRuntime::resolve_get_put to do
 193       // additional, required work.
 194       assert(byte_no == f1_byte || byte_no == f2_byte, "byte_no out of range");
 195       assert(load_bc_into_bc_reg, "we use bc_reg as temp");
 196       __ get_cache_and_index_and_bytecode_at_bcp(temp_reg, bc_reg, temp_reg, byte_no, 1);
 197       __ movl(bc_reg, bc);
 198       __ cmpl(temp_reg, (int) 0);
 199       __ jcc(Assembler::zero, L_patch_done);  // don't patch
 200     }
 201     break;
 202   default:
 203     assert(byte_no == -1, "sanity");
 204     // the pair bytecodes have already done the load.
 205     if (load_bc_into_bc_reg) {
 206       __ movl(bc_reg, bc);
 207     }
 208   }
 209 
 210   if (JvmtiExport::can_post_breakpoint()) {
 211     Label L_fast_patch;
 212     // if a breakpoint is present we can't rewrite the stream directly
 213     __ movzbl(temp_reg, at_bcp(0));
 214     __ cmpl(temp_reg, Bytecodes::_breakpoint);
 215     __ jcc(Assembler::notEqual, L_fast_patch);
 216     __ get_method(temp_reg);
 217     // Let breakpoint table handling rewrite to quicker bytecode
 218     __ call_VM(noreg, CAST_FROM_FN_PTR(address, InterpreterRuntime::set_original_bytecode_at), temp_reg, rbcp, bc_reg);
 219 #ifndef ASSERT
 220     __ jmpb(L_patch_done);
 221 #else
 222     __ jmp(L_patch_done);
 223 #endif
 224     __ bind(L_fast_patch);
 225   }
 226 
 227 #ifdef ASSERT
 228   Label L_okay;
 229   __ load_unsigned_byte(temp_reg, at_bcp(0));
 230   __ cmpl(temp_reg, (int) Bytecodes::java_code(bc));
 231   __ jcc(Assembler::equal, L_okay);
 232   __ cmpl(temp_reg, bc_reg);
 233   __ jcc(Assembler::equal, L_okay);
 234   __ stop("patching the wrong bytecode");
 235   __ bind(L_okay);
 236 #endif
 237 
 238   // patch bytecode
 239   __ movb(at_bcp(0), bc_reg);
 240   __ bind(L_patch_done);
 241 }
 242 // Individual instructions
 243 
 244 
 245 void TemplateTable::nop() {
 246   transition(vtos, vtos);
 247   // nothing to do
 248 }
 249 
 250 void TemplateTable::shouldnotreachhere() {
 251   transition(vtos, vtos);
 252   __ stop("shouldnotreachhere bytecode");
 253 }
 254 
 255 void TemplateTable::aconst_null() {
 256   transition(vtos, atos);
 257   __ xorl(rax, rax);
 258 }
 259 
 260 void TemplateTable::iconst(int value) {
 261   transition(vtos, itos);
 262   if (value == 0) {
 263     __ xorl(rax, rax);
 264   } else {
 265     __ movl(rax, value);
 266   }
 267 }
 268 
 269 void TemplateTable::lconst(int value) {
 270   transition(vtos, ltos);
 271   if (value == 0) {
 272     __ xorl(rax, rax);
 273   } else {
 274     __ movl(rax, value);
 275   }
 276 #ifndef _LP64
 277   assert(value >= 0, "check this code");
 278   __ xorptr(rdx, rdx);
 279 #endif
 280 }
 281 
 282 
 283 
 284 void TemplateTable::fconst(int value) {
 285   transition(vtos, ftos);
 286   if (UseSSE >= 1) {
 287     static float one = 1.0f, two = 2.0f;
 288     switch (value) {
 289     case 0:
 290       __ xorps(xmm0, xmm0);
 291       break;
 292     case 1:
 293       __ movflt(xmm0, ExternalAddress((address) &one));
 294       break;
 295     case 2:
 296       __ movflt(xmm0, ExternalAddress((address) &two));
 297       break;
 298     default:
 299       ShouldNotReachHere();
 300       break;
 301     }
 302   } else {
 303 #ifdef _LP64
 304     ShouldNotReachHere();
 305 #else
 306            if (value == 0) { __ fldz();
 307     } else if (value == 1) { __ fld1();
 308     } else if (value == 2) { __ fld1(); __ fld1(); __ faddp(); // should do a better solution here
 309     } else                 { ShouldNotReachHere();
 310     }
 311 #endif // _LP64
 312   }
 313 }
 314 
 315 void TemplateTable::dconst(int value) {
 316   transition(vtos, dtos);
 317   if (UseSSE >= 2) {
 318     static double one = 1.0;
 319     switch (value) {
 320     case 0:
 321       __ xorpd(xmm0, xmm0);
 322       break;
 323     case 1:
 324       __ movdbl(xmm0, ExternalAddress((address) &one));
 325       break;
 326     default:
 327       ShouldNotReachHere();
 328       break;
 329     }
 330   } else {
 331 #ifdef _LP64
 332     ShouldNotReachHere();
 333 #else
 334            if (value == 0) { __ fldz();
 335     } else if (value == 1) { __ fld1();
 336     } else                 { ShouldNotReachHere();
 337     }
 338 #endif
 339   }
 340 }
 341 
 342 void TemplateTable::bipush() {
 343   transition(vtos, itos);
 344   __ load_signed_byte(rax, at_bcp(1));
 345 }
 346 
 347 void TemplateTable::sipush() {
 348   transition(vtos, itos);
 349   __ load_unsigned_short(rax, at_bcp(1));
 350   __ bswapl(rax);
 351   __ sarl(rax, 16);
 352 }
 353 
 354 void TemplateTable::ldc(bool wide) {
 355   transition(vtos, vtos);
 356   Register rarg = NOT_LP64(rcx) LP64_ONLY(c_rarg1);
 357   Label call_ldc, notFloat, notClass, notInt, Done;
 358 
 359   if (wide) {
 360     __ get_unsigned_2_byte_index_at_bcp(rbx, 1);
 361   } else {
 362     __ load_unsigned_byte(rbx, at_bcp(1));
 363   }
 364 
 365   __ get_cpool_and_tags(rcx, rax);
 366   const int base_offset = ConstantPool::header_size() * wordSize;
 367   const int tags_offset = Array<u1>::base_offset_in_bytes();
 368 
 369   // get type
 370   __ movzbl(rdx, Address(rax, rbx, Address::times_1, tags_offset));
 371 
 372   // unresolved class - get the resolved class
 373   __ cmpl(rdx, JVM_CONSTANT_UnresolvedClass);
 374   __ jccb(Assembler::equal, call_ldc);
 375 
 376   // unresolved class in error state - call into runtime to throw the error
 377   // from the first resolution attempt
 378   __ cmpl(rdx, JVM_CONSTANT_UnresolvedClassInError);
 379   __ jccb(Assembler::equal, call_ldc);
 380 
 381   // resolved class - need to call vm to get java mirror of the class
 382   __ cmpl(rdx, JVM_CONSTANT_Class);
 383   __ jcc(Assembler::notEqual, notClass);
 384 
 385   __ bind(call_ldc);
 386 
 387   __ movl(rarg, wide);
 388   call_VM(rax, CAST_FROM_FN_PTR(address, InterpreterRuntime::ldc), rarg);
 389 
 390   __ push(atos);
 391   __ jmp(Done);
 392 
 393   __ bind(notClass);
 394   __ cmpl(rdx, JVM_CONSTANT_Float);
 395   __ jccb(Assembler::notEqual, notFloat);
 396 
 397   // ftos
 398   __ load_float(Address(rcx, rbx, Address::times_ptr, base_offset));
 399   __ push(ftos);
 400   __ jmp(Done);
 401 
 402   __ bind(notFloat);
 403   __ cmpl(rdx, JVM_CONSTANT_Integer);
 404   __ jccb(Assembler::notEqual, notInt);
 405 
 406   // itos
 407   __ movl(rax, Address(rcx, rbx, Address::times_ptr, base_offset));
 408   __ push(itos);
 409   __ jmp(Done);
 410 
 411   // assume the tag is for condy; if not, the VM runtime will tell us
 412   __ bind(notInt);
 413   condy_helper(Done);
 414 
 415   __ bind(Done);
 416 }
 417 
 418 // Fast path for caching oop constants.
 419 void TemplateTable::fast_aldc(bool wide) {
 420   transition(vtos, atos);
 421 
 422   Register result = rax;
 423   Register tmp = rdx;
 424   Register rarg = NOT_LP64(rcx) LP64_ONLY(c_rarg1);
 425   int index_size = wide ? sizeof(u2) : sizeof(u1);
 426 
 427   Label resolved;
 428 
 429   // We are resolved if the resolved reference cache entry contains a
 430   // non-null object (String, MethodType, etc.)
 431   assert_different_registers(result, tmp);
 432   __ get_cache_index_at_bcp(tmp, 1, index_size);
 433   __ load_resolved_reference_at_index(result, tmp);
 434   __ testptr(result, result);
 435   __ jcc(Assembler::notZero, resolved);
 436 
 437   address entry = CAST_FROM_FN_PTR(address, InterpreterRuntime::resolve_ldc);
 438 
 439   // first time invocation - must resolve first
 440   __ movl(rarg, (int)bytecode());
 441   __ call_VM(result, entry, rarg);
 442   __ bind(resolved);
 443 
 444   { // Check for the null sentinel.
 445     // If we just called the VM, it already did the mapping for us,
 446     // but it's harmless to retry.
 447     Label notNull;
 448     ExternalAddress null_sentinel((address)Universe::the_null_sentinel_addr());
 449     __ movptr(tmp, null_sentinel);
 450     __ cmpptr(tmp, result);
 451     __ jccb(Assembler::notEqual, notNull);
 452     __ xorptr(result, result);  // NULL object reference
 453     __ bind(notNull);
 454   }
 455 
 456   if (VerifyOops) {
 457     __ verify_oop(result);
 458   }
 459 }
 460 
 461 void TemplateTable::ldc2_w() {
 462   transition(vtos, vtos);
 463   Label notDouble, notLong, Done;
 464   __ get_unsigned_2_byte_index_at_bcp(rbx, 1);
 465 
 466   __ get_cpool_and_tags(rcx, rax);
 467   const int base_offset = ConstantPool::header_size() * wordSize;
 468   const int tags_offset = Array<u1>::base_offset_in_bytes();
 469 
 470   // get type
 471   __ movzbl(rdx, Address(rax, rbx, Address::times_1, tags_offset));
 472   __ cmpl(rdx, JVM_CONSTANT_Double);
 473   __ jccb(Assembler::notEqual, notDouble);
 474 
 475   // dtos
 476   __ load_double(Address(rcx, rbx, Address::times_ptr, base_offset));
 477   __ push(dtos);
 478 
 479   __ jmp(Done);
 480   __ bind(notDouble);
 481   __ cmpl(rdx, JVM_CONSTANT_Long);
 482   __ jccb(Assembler::notEqual, notLong);
 483 
 484   // ltos
 485   __ movptr(rax, Address(rcx, rbx, Address::times_ptr, base_offset + 0 * wordSize));
 486   NOT_LP64(__ movptr(rdx, Address(rcx, rbx, Address::times_ptr, base_offset + 1 * wordSize)));
 487   __ push(ltos);
 488   __ jmp(Done);
 489 
 490   __ bind(notLong);
 491   condy_helper(Done);
 492 
 493   __ bind(Done);
 494 }
 495 
 496 void TemplateTable::condy_helper(Label& Done) {
 497   const Register obj = rax;
 498   const Register off = rbx;
 499   const Register flags = rcx;
 500   const Register rarg = NOT_LP64(rcx) LP64_ONLY(c_rarg1);
 501   __ movl(rarg, (int)bytecode());
 502   call_VM(obj, CAST_FROM_FN_PTR(address, InterpreterRuntime::resolve_ldc), rarg);
 503 #ifndef _LP64
 504   // borrow rdi from locals
 505   __ get_thread(rdi);
 506   __ get_vm_result_2(flags, rdi);
 507   __ restore_locals();
 508 #else
 509   __ get_vm_result_2(flags, r15_thread);
 510 #endif
 511   // VMr = obj = base address to find primitive value to push
 512   // VMr2 = flags = (tos, off) using format of CPCE::_flags
 513   __ movl(off, flags);
 514   __ andl(off, ConstantPoolCacheEntry::field_index_mask);
 515   const Address field(obj, off, Address::times_1, 0*wordSize);
 516 
 517   // What sort of thing are we loading?
 518   __ shrl(flags, ConstantPoolCacheEntry::tos_state_shift);
 519   __ andl(flags, ConstantPoolCacheEntry::tos_state_mask);
 520 
 521   switch (bytecode()) {
 522   case Bytecodes::_ldc:
 523   case Bytecodes::_ldc_w:
 524     {
 525       // tos in (itos, ftos, stos, btos, ctos, ztos)
 526       Label notInt, notFloat, notShort, notByte, notChar, notBool;
 527       __ cmpl(flags, itos);
 528       __ jcc(Assembler::notEqual, notInt);
 529       // itos
 530       __ movl(rax, field);
 531       __ push(itos);
 532       __ jmp(Done);
 533 
 534       __ bind(notInt);
 535       __ cmpl(flags, ftos);
 536       __ jcc(Assembler::notEqual, notFloat);
 537       // ftos
 538       __ load_float(field);
 539       __ push(ftos);
 540       __ jmp(Done);
 541 
 542       __ bind(notFloat);
 543       __ cmpl(flags, stos);
 544       __ jcc(Assembler::notEqual, notShort);
 545       // stos
 546       __ load_signed_short(rax, field);
 547       __ push(stos);
 548       __ jmp(Done);
 549 
 550       __ bind(notShort);
 551       __ cmpl(flags, btos);
 552       __ jcc(Assembler::notEqual, notByte);
 553       // btos
 554       __ load_signed_byte(rax, field);
 555       __ push(btos);
 556       __ jmp(Done);
 557 
 558       __ bind(notByte);
 559       __ cmpl(flags, ctos);
 560       __ jcc(Assembler::notEqual, notChar);
 561       // ctos
 562       __ load_unsigned_short(rax, field);
 563       __ push(ctos);
 564       __ jmp(Done);
 565 
 566       __ bind(notChar);
 567       __ cmpl(flags, ztos);
 568       __ jcc(Assembler::notEqual, notBool);
 569       // ztos
 570       __ load_signed_byte(rax, field);
 571       __ push(ztos);
 572       __ jmp(Done);
 573 
 574       __ bind(notBool);
 575       break;
 576     }
 577 
 578   case Bytecodes::_ldc2_w:
 579     {
 580       Label notLong, notDouble;
 581       __ cmpl(flags, ltos);
 582       __ jcc(Assembler::notEqual, notLong);
 583       // ltos
 584       __ movptr(rax, field);
 585       NOT_LP64(__ movptr(rdx, field.plus_disp(4)));
 586       __ push(ltos);
 587       __ jmp(Done);
 588 
 589       __ bind(notLong);
 590       __ cmpl(flags, dtos);
 591       __ jcc(Assembler::notEqual, notDouble);
 592       // dtos
 593       __ load_double(field);
 594       __ push(dtos);
 595       __ jmp(Done);
 596 
 597       __ bind(notDouble);
 598       break;
 599     }
 600 
 601   default:
 602     ShouldNotReachHere();
 603   }
 604 
 605   __ stop("bad ldc/condy");
 606 }
 607 
 608 void TemplateTable::locals_index(Register reg, int offset) {
 609   __ load_unsigned_byte(reg, at_bcp(offset));
 610   __ negptr(reg);
 611 }
 612 
 613 void TemplateTable::iload() {
 614   iload_internal();
 615 }
 616 
 617 void TemplateTable::nofast_iload() {
 618   iload_internal(may_not_rewrite);
 619 }
 620 
 621 void TemplateTable::iload_internal(RewriteControl rc) {
 622   transition(vtos, itos);
 623   if (RewriteFrequentPairs && rc == may_rewrite) {
 624     Label rewrite, done;
 625     const Register bc = LP64_ONLY(c_rarg3) NOT_LP64(rcx);
 626     LP64_ONLY(assert(rbx != bc, "register damaged"));
 627 
 628     // get next byte
 629     __ load_unsigned_byte(rbx,
 630                           at_bcp(Bytecodes::length_for(Bytecodes::_iload)));
 631     // if _iload, wait to rewrite to iload2.  We only want to rewrite the
 632     // last two iloads in a pair.  Comparing against fast_iload means that
 633     // the next bytecode is neither an iload or a caload, and therefore
 634     // an iload pair.
 635     __ cmpl(rbx, Bytecodes::_iload);
 636     __ jcc(Assembler::equal, done);
 637 
 638     __ cmpl(rbx, Bytecodes::_fast_iload);
 639     __ movl(bc, Bytecodes::_fast_iload2);
 640 
 641     __ jccb(Assembler::equal, rewrite);
 642 
 643     // if _caload, rewrite to fast_icaload
 644     __ cmpl(rbx, Bytecodes::_caload);
 645     __ movl(bc, Bytecodes::_fast_icaload);
 646     __ jccb(Assembler::equal, rewrite);
 647 
 648     // rewrite so iload doesn't check again.
 649     __ movl(bc, Bytecodes::_fast_iload);
 650 
 651     // rewrite
 652     // bc: fast bytecode
 653     __ bind(rewrite);
 654     patch_bytecode(Bytecodes::_iload, bc, rbx, false);
 655     __ bind(done);
 656   }
 657 
 658   // Get the local value into tos
 659   locals_index(rbx);
 660   __ movl(rax, iaddress(rbx));
 661 }
 662 
 663 void TemplateTable::fast_iload2() {
 664   transition(vtos, itos);
 665   locals_index(rbx);
 666   __ movl(rax, iaddress(rbx));
 667   __ push(itos);
 668   locals_index(rbx, 3);
 669   __ movl(rax, iaddress(rbx));
 670 }
 671 
 672 void TemplateTable::fast_iload() {
 673   transition(vtos, itos);
 674   locals_index(rbx);
 675   __ movl(rax, iaddress(rbx));
 676 }
 677 
 678 void TemplateTable::lload() {
 679   transition(vtos, ltos);
 680   locals_index(rbx);
 681   __ movptr(rax, laddress(rbx));
 682   NOT_LP64(__ movl(rdx, haddress(rbx)));
 683 }
 684 
 685 void TemplateTable::fload() {
 686   transition(vtos, ftos);
 687   locals_index(rbx);
 688   __ load_float(faddress(rbx));
 689 }
 690 
 691 void TemplateTable::dload() {
 692   transition(vtos, dtos);
 693   locals_index(rbx);
 694   __ load_double(daddress(rbx));
 695 }
 696 
 697 void TemplateTable::aload() {
 698   transition(vtos, atos);
 699   locals_index(rbx);
 700   __ movptr(rax, aaddress(rbx));
 701 }
 702 
 703 void TemplateTable::locals_index_wide(Register reg) {
 704   __ load_unsigned_short(reg, at_bcp(2));
 705   __ bswapl(reg);
 706   __ shrl(reg, 16);
 707   __ negptr(reg);
 708 }
 709 
 710 void TemplateTable::wide_iload() {
 711   transition(vtos, itos);
 712   locals_index_wide(rbx);
 713   __ movl(rax, iaddress(rbx));
 714 }
 715 
 716 void TemplateTable::wide_lload() {
 717   transition(vtos, ltos);
 718   locals_index_wide(rbx);
 719   __ movptr(rax, laddress(rbx));
 720   NOT_LP64(__ movl(rdx, haddress(rbx)));
 721 }
 722 
 723 void TemplateTable::wide_fload() {
 724   transition(vtos, ftos);
 725   locals_index_wide(rbx);
 726   __ load_float(faddress(rbx));
 727 }
 728 
 729 void TemplateTable::wide_dload() {
 730   transition(vtos, dtos);
 731   locals_index_wide(rbx);
 732   __ load_double(daddress(rbx));
 733 }
 734 
 735 void TemplateTable::wide_aload() {
 736   transition(vtos, atos);
 737   locals_index_wide(rbx);
 738   __ movptr(rax, aaddress(rbx));
 739 }
 740 
 741 void TemplateTable::index_check(Register array, Register index) {
 742   // Pop ptr into array
 743   __ pop_ptr(array);
 744   index_check_without_pop(array, index);
 745 }
 746 
 747 void TemplateTable::index_check_without_pop(Register array, Register index) {
 748   // destroys rbx
 749   // check array
 750   __ null_check(array, arrayOopDesc::length_offset_in_bytes());
 751   // sign extend index for use by indexed load
 752   __ movl2ptr(index, index);
 753   // check index
 754   __ cmpl(index, Address(array, arrayOopDesc::length_offset_in_bytes()));
 755   if (index != rbx) {
 756     // ??? convention: move aberrant index into rbx for exception message
 757     assert(rbx != array, "different registers");
 758     __ movl(rbx, index);
 759   }
 760   Label skip;
 761   __ jccb(Assembler::below, skip);
 762   // Pass array to create more detailed exceptions.
 763   __ mov(NOT_LP64(rax) LP64_ONLY(c_rarg1), array);
 764   __ jump(ExternalAddress(Interpreter::_throw_ArrayIndexOutOfBoundsException_entry));
 765   __ bind(skip);
 766 }
 767 
 768 void TemplateTable::iaload() {
 769   transition(itos, itos);
 770   // rax: index
 771   // rdx: array
 772   index_check(rdx, rax); // kills rbx
 773   __ access_load_at(T_INT, IN_HEAP | IN_HEAP_ARRAY, rax,
 774                     Address(rdx, rax, Address::times_4,
 775                             arrayOopDesc::base_offset_in_bytes(T_INT)),
 776                     noreg, noreg);
 777 }
 778 
 779 void TemplateTable::laload() {
 780   transition(itos, ltos);
 781   // rax: index
 782   // rdx: array
 783   index_check(rdx, rax); // kills rbx
 784   NOT_LP64(__ mov(rbx, rax));
 785   // rbx,: index
 786   __ access_load_at(T_LONG, IN_HEAP | IN_HEAP_ARRAY, noreg /* ltos */,
 787                     Address(rdx, rbx, Address::times_8,
 788                             arrayOopDesc::base_offset_in_bytes(T_LONG)),
 789                     noreg, noreg);
 790 }
 791 
 792 
 793 
 794 void TemplateTable::faload() {
 795   transition(itos, ftos);
 796   // rax: index
 797   // rdx: array
 798   index_check(rdx, rax); // kills rbx
 799   __ access_load_at(T_FLOAT, IN_HEAP | IN_HEAP_ARRAY, noreg /* ftos */,
 800                     Address(rdx, rax,
 801                             Address::times_4,
 802                             arrayOopDesc::base_offset_in_bytes(T_FLOAT)),
 803                     noreg, noreg);
 804 }
 805 
 806 void TemplateTable::daload() {
 807   transition(itos, dtos);
 808   // rax: index
 809   // rdx: array
 810   index_check(rdx, rax); // kills rbx
 811   __ access_load_at(T_DOUBLE, IN_HEAP | IN_HEAP_ARRAY, noreg /* dtos */,
 812                     Address(rdx, rax,
 813                             Address::times_8,
 814                             arrayOopDesc::base_offset_in_bytes(T_DOUBLE)),
 815                     noreg, noreg);
 816 }
 817 
 818 void TemplateTable::aaload() {
 819   transition(itos, atos);
 820   // rax: index
 821   // rdx: array
 822   index_check(rdx, rax); // kills rbx
 823   do_oop_load(_masm,
 824               Address(rdx, rax,
 825                       UseCompressedOops ? Address::times_4 : Address::times_ptr,
 826                       arrayOopDesc::base_offset_in_bytes(T_OBJECT)),
 827               rax,
 828               IN_HEAP_ARRAY);
 829 }
 830 
 831 void TemplateTable::baload() {
 832   transition(itos, itos);
 833   // rax: index
 834   // rdx: array
 835   index_check(rdx, rax); // kills rbx
 836   __ access_load_at(T_BYTE, IN_HEAP | IN_HEAP_ARRAY, rax,
 837                     Address(rdx, rax, Address::times_1, arrayOopDesc::base_offset_in_bytes(T_BYTE)),
 838                     noreg, noreg);
 839 }
 840 
 841 void TemplateTable::caload() {
 842   transition(itos, itos);
 843   // rax: index
 844   // rdx: array
 845   index_check(rdx, rax); // kills rbx
 846   __ access_load_at(T_CHAR, IN_HEAP | IN_HEAP_ARRAY, rax,
 847                     Address(rdx, rax, Address::times_2, arrayOopDesc::base_offset_in_bytes(T_CHAR)),
 848                     noreg, noreg);
 849 }
 850 
 851 // iload followed by caload frequent pair
 852 void TemplateTable::fast_icaload() {
 853   transition(vtos, itos);
 854   // load index out of locals
 855   locals_index(rbx);
 856   __ movl(rax, iaddress(rbx));
 857 
 858   // rax: index
 859   // rdx: array
 860   index_check(rdx, rax); // kills rbx
 861   __ access_load_at(T_CHAR, IN_HEAP | IN_HEAP_ARRAY, rax,
 862                     Address(rdx, rax, Address::times_2, arrayOopDesc::base_offset_in_bytes(T_CHAR)),
 863                     noreg, noreg);
 864 }
 865 
 866 
 867 void TemplateTable::saload() {
 868   transition(itos, itos);
 869   // rax: index
 870   // rdx: array
 871   index_check(rdx, rax); // kills rbx
 872   __ access_load_at(T_SHORT, IN_HEAP | IN_HEAP_ARRAY, rax,
 873                     Address(rdx, rax, Address::times_2, arrayOopDesc::base_offset_in_bytes(T_SHORT)),
 874                     noreg, noreg);
 875 }
 876 
 877 void TemplateTable::iload(int n) {
 878   transition(vtos, itos);
 879   __ movl(rax, iaddress(n));
 880 }
 881 
 882 void TemplateTable::lload(int n) {
 883   transition(vtos, ltos);
 884   __ movptr(rax, laddress(n));
 885   NOT_LP64(__ movptr(rdx, haddress(n)));
 886 }
 887 
 888 void TemplateTable::fload(int n) {
 889   transition(vtos, ftos);
 890   __ load_float(faddress(n));
 891 }
 892 
 893 void TemplateTable::dload(int n) {
 894   transition(vtos, dtos);
 895   __ load_double(daddress(n));
 896 }
 897 
 898 void TemplateTable::aload(int n) {
 899   transition(vtos, atos);
 900   __ movptr(rax, aaddress(n));
 901 }
 902 
 903 void TemplateTable::aload_0() {
 904   aload_0_internal();
 905 }
 906 
 907 void TemplateTable::nofast_aload_0() {
 908   aload_0_internal(may_not_rewrite);
 909 }
 910 
 911 void TemplateTable::aload_0_internal(RewriteControl rc) {
 912   transition(vtos, atos);
 913   // According to bytecode histograms, the pairs:
 914   //
 915   // _aload_0, _fast_igetfield
 916   // _aload_0, _fast_agetfield
 917   // _aload_0, _fast_fgetfield
 918   //
 919   // occur frequently. If RewriteFrequentPairs is set, the (slow)
 920   // _aload_0 bytecode checks if the next bytecode is either
 921   // _fast_igetfield, _fast_agetfield or _fast_fgetfield and then
 922   // rewrites the current bytecode into a pair bytecode; otherwise it
 923   // rewrites the current bytecode into _fast_aload_0 that doesn't do
 924   // the pair check anymore.
 925   //
 926   // Note: If the next bytecode is _getfield, the rewrite must be
 927   //       delayed, otherwise we may miss an opportunity for a pair.
 928   //
 929   // Also rewrite frequent pairs
 930   //   aload_0, aload_1
 931   //   aload_0, iload_1
 932   // These bytecodes with a small amount of code are most profitable
 933   // to rewrite
 934   if (RewriteFrequentPairs && rc == may_rewrite) {
 935     Label rewrite, done;
 936 
 937     const Register bc = LP64_ONLY(c_rarg3) NOT_LP64(rcx);
 938     LP64_ONLY(assert(rbx != bc, "register damaged"));
 939 
 940     // get next byte
 941     __ load_unsigned_byte(rbx, at_bcp(Bytecodes::length_for(Bytecodes::_aload_0)));
 942 
 943     // if _getfield then wait with rewrite
 944     __ cmpl(rbx, Bytecodes::_getfield);
 945     __ jcc(Assembler::equal, done);
 946 
 947     // if _igetfield then rewrite to _fast_iaccess_0
 948     assert(Bytecodes::java_code(Bytecodes::_fast_iaccess_0) == Bytecodes::_aload_0, "fix bytecode definition");
 949     __ cmpl(rbx, Bytecodes::_fast_igetfield);
 950     __ movl(bc, Bytecodes::_fast_iaccess_0);
 951     __ jccb(Assembler::equal, rewrite);
 952 
 953     // if _agetfield then rewrite to _fast_aaccess_0
 954     assert(Bytecodes::java_code(Bytecodes::_fast_aaccess_0) == Bytecodes::_aload_0, "fix bytecode definition");
 955     __ cmpl(rbx, Bytecodes::_fast_agetfield);
 956     __ movl(bc, Bytecodes::_fast_aaccess_0);
 957     __ jccb(Assembler::equal, rewrite);
 958 
 959     // if _fgetfield then rewrite to _fast_faccess_0
 960     assert(Bytecodes::java_code(Bytecodes::_fast_faccess_0) == Bytecodes::_aload_0, "fix bytecode definition");
 961     __ cmpl(rbx, Bytecodes::_fast_fgetfield);
 962     __ movl(bc, Bytecodes::_fast_faccess_0);
 963     __ jccb(Assembler::equal, rewrite);
 964 
 965     // else rewrite to _fast_aload0
 966     assert(Bytecodes::java_code(Bytecodes::_fast_aload_0) == Bytecodes::_aload_0, "fix bytecode definition");
 967     __ movl(bc, Bytecodes::_fast_aload_0);
 968 
 969     // rewrite
 970     // bc: fast bytecode
 971     __ bind(rewrite);
 972     patch_bytecode(Bytecodes::_aload_0, bc, rbx, false);
 973 
 974     __ bind(done);
 975   }
 976 
 977   // Do actual aload_0 (must do this after patch_bytecode which might call VM and GC might change oop).
 978   aload(0);
 979 }
 980 
 981 void TemplateTable::istore() {
 982   transition(itos, vtos);
 983   locals_index(rbx);
 984   __ movl(iaddress(rbx), rax);
 985 }
 986 
 987 
 988 void TemplateTable::lstore() {
 989   transition(ltos, vtos);
 990   locals_index(rbx);
 991   __ movptr(laddress(rbx), rax);
 992   NOT_LP64(__ movptr(haddress(rbx), rdx));
 993 }
 994 
 995 void TemplateTable::fstore() {
 996   transition(ftos, vtos);
 997   locals_index(rbx);
 998   __ store_float(faddress(rbx));
 999 }
1000 
1001 void TemplateTable::dstore() {
1002   transition(dtos, vtos);
1003   locals_index(rbx);
1004   __ store_double(daddress(rbx));
1005 }
1006 
1007 void TemplateTable::astore() {
1008   transition(vtos, vtos);
1009   __ pop_ptr(rax);
1010   locals_index(rbx);
1011   __ movptr(aaddress(rbx), rax);
1012 }
1013 
1014 void TemplateTable::wide_istore() {
1015   transition(vtos, vtos);
1016   __ pop_i();
1017   locals_index_wide(rbx);
1018   __ movl(iaddress(rbx), rax);
1019 }
1020 
1021 void TemplateTable::wide_lstore() {
1022   transition(vtos, vtos);
1023   NOT_LP64(__ pop_l(rax, rdx));
1024   LP64_ONLY(__ pop_l());
1025   locals_index_wide(rbx);
1026   __ movptr(laddress(rbx), rax);
1027   NOT_LP64(__ movl(haddress(rbx), rdx));
1028 }
1029 
1030 void TemplateTable::wide_fstore() {
1031 #ifdef _LP64
1032   transition(vtos, vtos);
1033   __ pop_f(xmm0);
1034   locals_index_wide(rbx);
1035   __ movflt(faddress(rbx), xmm0);
1036 #else
1037   wide_istore();
1038 #endif
1039 }
1040 
1041 void TemplateTable::wide_dstore() {
1042 #ifdef _LP64
1043   transition(vtos, vtos);
1044   __ pop_d(xmm0);
1045   locals_index_wide(rbx);
1046   __ movdbl(daddress(rbx), xmm0);
1047 #else
1048   wide_lstore();
1049 #endif
1050 }
1051 
1052 void TemplateTable::wide_astore() {
1053   transition(vtos, vtos);
1054   __ pop_ptr(rax);
1055   locals_index_wide(rbx);
1056   __ movptr(aaddress(rbx), rax);
1057 }
1058 
1059 void TemplateTable::iastore() {
1060   transition(itos, vtos);
1061   __ pop_i(rbx);
1062   // rax: value
1063   // rbx: index
1064   // rdx: array
1065   index_check(rdx, rbx); // prefer index in rbx
1066   __ access_store_at(T_INT, IN_HEAP | IN_HEAP_ARRAY,
1067                      Address(rdx, rbx, Address::times_4,
1068                              arrayOopDesc::base_offset_in_bytes(T_INT)),
1069                      rax, noreg, noreg);
1070 }
1071 
1072 void TemplateTable::lastore() {
1073   transition(ltos, vtos);
1074   __ pop_i(rbx);
1075   // rax,: low(value)
1076   // rcx: array
1077   // rdx: high(value)
1078   index_check(rcx, rbx);  // prefer index in rbx,
1079   // rbx,: index
1080   __ access_store_at(T_LONG, IN_HEAP | IN_HEAP_ARRAY,
1081                      Address(rcx, rbx, Address::times_8,
1082                              arrayOopDesc::base_offset_in_bytes(T_LONG)),
1083                      noreg /* ltos */, noreg, noreg);
1084 }
1085 
1086 
1087 void TemplateTable::fastore() {
1088   transition(ftos, vtos);
1089   __ pop_i(rbx);
1090   // value is in UseSSE >= 1 ? xmm0 : ST(0)
1091   // rbx:  index
1092   // rdx:  array
1093   index_check(rdx, rbx); // prefer index in rbx
1094   __ access_store_at(T_FLOAT, IN_HEAP | IN_HEAP_ARRAY,
1095                      Address(rdx, rbx, Address::times_4,
1096                              arrayOopDesc::base_offset_in_bytes(T_FLOAT)),
1097                      noreg /* ftos */, noreg, noreg);
1098 }
1099 
1100 void TemplateTable::dastore() {
1101   transition(dtos, vtos);
1102   __ pop_i(rbx);
1103   // value is in UseSSE >= 2 ? xmm0 : ST(0)
1104   // rbx:  index
1105   // rdx:  array
1106   index_check(rdx, rbx); // prefer index in rbx
1107   __ access_store_at(T_DOUBLE, IN_HEAP | IN_HEAP_ARRAY,
1108                      Address(rdx, rbx, Address::times_8,
1109                              arrayOopDesc::base_offset_in_bytes(T_DOUBLE)),
1110                      noreg /* dtos */, noreg, noreg);
1111 }
1112 
1113 void TemplateTable::aastore() {
1114   Label is_null, ok_is_subtype, done;
1115   transition(vtos, vtos);
1116   // stack: ..., array, index, value
1117   __ movptr(rax, at_tos());    // value
1118   __ movl(rcx, at_tos_p1()); // index
1119   __ movptr(rdx, at_tos_p2()); // array
1120 
1121   Address element_address(rdx, rcx,
1122                           UseCompressedOops? Address::times_4 : Address::times_ptr,
1123                           arrayOopDesc::base_offset_in_bytes(T_OBJECT));
1124 
1125   index_check_without_pop(rdx, rcx);     // kills rbx
1126   __ testptr(rax, rax);
1127   __ jcc(Assembler::zero, is_null);
1128 
1129   // Move subklass into rbx
1130   __ load_klass(rbx, rax);
1131   // Move superklass into rax
1132   __ load_klass(rax, rdx);
1133   __ movptr(rax, Address(rax,
1134                          ObjArrayKlass::element_klass_offset()));
1135 
1136   // Generate subtype check.  Blows rcx, rdi
1137   // Superklass in rax.  Subklass in rbx.
1138   __ gen_subtype_check(rbx, ok_is_subtype);
1139 
1140   // Come here on failure
1141   // object is at TOS
1142   __ jump(ExternalAddress(Interpreter::_throw_ArrayStoreException_entry));
1143 
1144   // Come here on success
1145   __ bind(ok_is_subtype);
1146 
1147   // Get the value we will store
1148   __ movptr(rax, at_tos());
1149   __ movl(rcx, at_tos_p1()); // index
1150   // Now store using the appropriate barrier
1151   do_oop_store(_masm, element_address, rax, IN_HEAP_ARRAY);
1152   __ jmp(done);
1153 
1154   // Have a NULL in rax, rdx=array, ecx=index.  Store NULL at ary[idx]
1155   __ bind(is_null);
1156   __ profile_null_seen(rbx);
1157 
1158   // Store a NULL
1159   do_oop_store(_masm, element_address, noreg, IN_HEAP_ARRAY);
1160 
1161   // Pop stack arguments
1162   __ bind(done);
1163   __ addptr(rsp, 3 * Interpreter::stackElementSize);
1164 }
1165 
1166 void TemplateTable::bastore() {
1167   transition(itos, vtos);
1168   __ pop_i(rbx);
1169   // rax: value
1170   // rbx: index
1171   // rdx: array
1172   index_check(rdx, rbx); // prefer index in rbx
1173   // Need to check whether array is boolean or byte
1174   // since both types share the bastore bytecode.
1175   __ load_klass(rcx, rdx);
1176   __ movl(rcx, Address(rcx, Klass::layout_helper_offset()));
1177   int diffbit = Klass::layout_helper_boolean_diffbit();
1178   __ testl(rcx, diffbit);
1179   Label L_skip;
1180   __ jccb(Assembler::zero, L_skip);
1181   __ andl(rax, 1);  // if it is a T_BOOLEAN array, mask the stored value to 0/1
1182   __ bind(L_skip);
1183   __ access_store_at(T_BYTE, IN_HEAP | IN_HEAP_ARRAY,
1184                      Address(rdx, rbx,Address::times_1,
1185                              arrayOopDesc::base_offset_in_bytes(T_BYTE)),
1186                      rax, noreg, noreg);
1187 }
1188 
1189 void TemplateTable::castore() {
1190   transition(itos, vtos);
1191   __ pop_i(rbx);
1192   // rax: value
1193   // rbx: index
1194   // rdx: array
1195   index_check(rdx, rbx);  // prefer index in rbx
1196   __ access_store_at(T_CHAR, IN_HEAP | IN_HEAP_ARRAY,
1197                      Address(rdx, rbx, Address::times_2,
1198                              arrayOopDesc::base_offset_in_bytes(T_CHAR)),
1199                      rax, noreg, noreg);
1200 }
1201 
1202 
1203 void TemplateTable::sastore() {
1204   castore();
1205 }
1206 
1207 void TemplateTable::istore(int n) {
1208   transition(itos, vtos);
1209   __ movl(iaddress(n), rax);
1210 }
1211 
1212 void TemplateTable::lstore(int n) {
1213   transition(ltos, vtos);
1214   __ movptr(laddress(n), rax);
1215   NOT_LP64(__ movptr(haddress(n), rdx));
1216 }
1217 
1218 void TemplateTable::fstore(int n) {
1219   transition(ftos, vtos);
1220   __ store_float(faddress(n));
1221 }
1222 
1223 void TemplateTable::dstore(int n) {
1224   transition(dtos, vtos);
1225   __ store_double(daddress(n));
1226 }
1227 
1228 
1229 void TemplateTable::astore(int n) {
1230   transition(vtos, vtos);
1231   __ pop_ptr(rax);
1232   __ movptr(aaddress(n), rax);
1233 }
1234 
1235 void TemplateTable::pop() {
1236   transition(vtos, vtos);
1237   __ addptr(rsp, Interpreter::stackElementSize);
1238 }
1239 
1240 void TemplateTable::pop2() {
1241   transition(vtos, vtos);
1242   __ addptr(rsp, 2 * Interpreter::stackElementSize);
1243 }
1244 
1245 
1246 void TemplateTable::dup() {
1247   transition(vtos, vtos);
1248   __ load_ptr(0, rax);
1249   __ push_ptr(rax);
1250   // stack: ..., a, a
1251 }
1252 
1253 void TemplateTable::dup_x1() {
1254   transition(vtos, vtos);
1255   // stack: ..., a, b
1256   __ load_ptr( 0, rax);  // load b
1257   __ load_ptr( 1, rcx);  // load a
1258   __ store_ptr(1, rax);  // store b
1259   __ store_ptr(0, rcx);  // store a
1260   __ push_ptr(rax);      // push b
1261   // stack: ..., b, a, b
1262 }
1263 
1264 void TemplateTable::dup_x2() {
1265   transition(vtos, vtos);
1266   // stack: ..., a, b, c
1267   __ load_ptr( 0, rax);  // load c
1268   __ load_ptr( 2, rcx);  // load a
1269   __ store_ptr(2, rax);  // store c in a
1270   __ push_ptr(rax);      // push c
1271   // stack: ..., c, b, c, c
1272   __ load_ptr( 2, rax);  // load b
1273   __ store_ptr(2, rcx);  // store a in b
1274   // stack: ..., c, a, c, c
1275   __ store_ptr(1, rax);  // store b in c
1276   // stack: ..., c, a, b, c
1277 }
1278 
1279 void TemplateTable::dup2() {
1280   transition(vtos, vtos);
1281   // stack: ..., a, b
1282   __ load_ptr(1, rax);  // load a
1283   __ push_ptr(rax);     // push a
1284   __ load_ptr(1, rax);  // load b
1285   __ push_ptr(rax);     // push b
1286   // stack: ..., a, b, a, b
1287 }
1288 
1289 
1290 void TemplateTable::dup2_x1() {
1291   transition(vtos, vtos);
1292   // stack: ..., a, b, c
1293   __ load_ptr( 0, rcx);  // load c
1294   __ load_ptr( 1, rax);  // load b
1295   __ push_ptr(rax);      // push b
1296   __ push_ptr(rcx);      // push c
1297   // stack: ..., a, b, c, b, c
1298   __ store_ptr(3, rcx);  // store c in b
1299   // stack: ..., a, c, c, b, c
1300   __ load_ptr( 4, rcx);  // load a
1301   __ store_ptr(2, rcx);  // store a in 2nd c
1302   // stack: ..., a, c, a, b, c
1303   __ store_ptr(4, rax);  // store b in a
1304   // stack: ..., b, c, a, b, c
1305 }
1306 
1307 void TemplateTable::dup2_x2() {
1308   transition(vtos, vtos);
1309   // stack: ..., a, b, c, d
1310   __ load_ptr( 0, rcx);  // load d
1311   __ load_ptr( 1, rax);  // load c
1312   __ push_ptr(rax);      // push c
1313   __ push_ptr(rcx);      // push d
1314   // stack: ..., a, b, c, d, c, d
1315   __ load_ptr( 4, rax);  // load b
1316   __ store_ptr(2, rax);  // store b in d
1317   __ store_ptr(4, rcx);  // store d in b
1318   // stack: ..., a, d, c, b, c, d
1319   __ load_ptr( 5, rcx);  // load a
1320   __ load_ptr( 3, rax);  // load c
1321   __ store_ptr(3, rcx);  // store a in c
1322   __ store_ptr(5, rax);  // store c in a
1323   // stack: ..., c, d, a, b, c, d
1324 }
1325 
1326 void TemplateTable::swap() {
1327   transition(vtos, vtos);
1328   // stack: ..., a, b
1329   __ load_ptr( 1, rcx);  // load a
1330   __ load_ptr( 0, rax);  // load b
1331   __ store_ptr(0, rcx);  // store a in b
1332   __ store_ptr(1, rax);  // store b in a
1333   // stack: ..., b, a
1334 }
1335 
1336 void TemplateTable::iop2(Operation op) {
1337   transition(itos, itos);
1338   switch (op) {
1339   case add  :                    __ pop_i(rdx); __ addl (rax, rdx); break;
1340   case sub  : __ movl(rdx, rax); __ pop_i(rax); __ subl (rax, rdx); break;
1341   case mul  :                    __ pop_i(rdx); __ imull(rax, rdx); break;
1342   case _and :                    __ pop_i(rdx); __ andl (rax, rdx); break;
1343   case _or  :                    __ pop_i(rdx); __ orl  (rax, rdx); break;
1344   case _xor :                    __ pop_i(rdx); __ xorl (rax, rdx); break;
1345   case shl  : __ movl(rcx, rax); __ pop_i(rax); __ shll (rax);      break;
1346   case shr  : __ movl(rcx, rax); __ pop_i(rax); __ sarl (rax);      break;
1347   case ushr : __ movl(rcx, rax); __ pop_i(rax); __ shrl (rax);      break;
1348   default   : ShouldNotReachHere();
1349   }
1350 }
1351 
1352 void TemplateTable::lop2(Operation op) {
1353   transition(ltos, ltos);
1354 #ifdef _LP64
1355   switch (op) {
1356   case add  :                    __ pop_l(rdx); __ addptr(rax, rdx); break;
1357   case sub  : __ mov(rdx, rax);  __ pop_l(rax); __ subptr(rax, rdx); break;
1358   case _and :                    __ pop_l(rdx); __ andptr(rax, rdx); break;
1359   case _or  :                    __ pop_l(rdx); __ orptr (rax, rdx); break;
1360   case _xor :                    __ pop_l(rdx); __ xorptr(rax, rdx); break;
1361   default   : ShouldNotReachHere();
1362   }
1363 #else
1364   __ pop_l(rbx, rcx);
1365   switch (op) {
1366     case add  : __ addl(rax, rbx); __ adcl(rdx, rcx); break;
1367     case sub  : __ subl(rbx, rax); __ sbbl(rcx, rdx);
1368                 __ mov (rax, rbx); __ mov (rdx, rcx); break;
1369     case _and : __ andl(rax, rbx); __ andl(rdx, rcx); break;
1370     case _or  : __ orl (rax, rbx); __ orl (rdx, rcx); break;
1371     case _xor : __ xorl(rax, rbx); __ xorl(rdx, rcx); break;
1372     default   : ShouldNotReachHere();
1373   }
1374 #endif
1375 }
1376 
1377 void TemplateTable::idiv() {
1378   transition(itos, itos);
1379   __ movl(rcx, rax);
1380   __ pop_i(rax);
1381   // Note: could xor rax and ecx and compare with (-1 ^ min_int). If
1382   //       they are not equal, one could do a normal division (no correction
1383   //       needed), which may speed up this implementation for the common case.
1384   //       (see also JVM spec., p.243 & p.271)
1385   __ corrected_idivl(rcx);
1386 }
1387 
1388 void TemplateTable::irem() {
1389   transition(itos, itos);
1390   __ movl(rcx, rax);
1391   __ pop_i(rax);
1392   // Note: could xor rax and ecx and compare with (-1 ^ min_int). If
1393   //       they are not equal, one could do a normal division (no correction
1394   //       needed), which may speed up this implementation for the common case.
1395   //       (see also JVM spec., p.243 & p.271)
1396   __ corrected_idivl(rcx);
1397   __ movl(rax, rdx);
1398 }
1399 
1400 void TemplateTable::lmul() {
1401   transition(ltos, ltos);
1402 #ifdef _LP64
1403   __ pop_l(rdx);
1404   __ imulq(rax, rdx);
1405 #else
1406   __ pop_l(rbx, rcx);
1407   __ push(rcx); __ push(rbx);
1408   __ push(rdx); __ push(rax);
1409   __ lmul(2 * wordSize, 0);
1410   __ addptr(rsp, 4 * wordSize);  // take off temporaries
1411 #endif
1412 }
1413 
1414 void TemplateTable::ldiv() {
1415   transition(ltos, ltos);
1416 #ifdef _LP64
1417   __ mov(rcx, rax);
1418   __ pop_l(rax);
1419   // generate explicit div0 check
1420   __ testq(rcx, rcx);
1421   __ jump_cc(Assembler::zero,
1422              ExternalAddress(Interpreter::_throw_ArithmeticException_entry));
1423   // Note: could xor rax and rcx and compare with (-1 ^ min_int). If
1424   //       they are not equal, one could do a normal division (no correction
1425   //       needed), which may speed up this implementation for the common case.
1426   //       (see also JVM spec., p.243 & p.271)
1427   __ corrected_idivq(rcx); // kills rbx
1428 #else
1429   __ pop_l(rbx, rcx);
1430   __ push(rcx); __ push(rbx);
1431   __ push(rdx); __ push(rax);
1432   // check if y = 0
1433   __ orl(rax, rdx);
1434   __ jump_cc(Assembler::zero,
1435              ExternalAddress(Interpreter::_throw_ArithmeticException_entry));
1436   __ call_VM_leaf(CAST_FROM_FN_PTR(address, SharedRuntime::ldiv));
1437   __ addptr(rsp, 4 * wordSize);  // take off temporaries
1438 #endif
1439 }
1440 
1441 void TemplateTable::lrem() {
1442   transition(ltos, ltos);
1443 #ifdef _LP64
1444   __ mov(rcx, rax);
1445   __ pop_l(rax);
1446   __ testq(rcx, rcx);
1447   __ jump_cc(Assembler::zero,
1448              ExternalAddress(Interpreter::_throw_ArithmeticException_entry));
1449   // Note: could xor rax and rcx and compare with (-1 ^ min_int). If
1450   //       they are not equal, one could do a normal division (no correction
1451   //       needed), which may speed up this implementation for the common case.
1452   //       (see also JVM spec., p.243 & p.271)
1453   __ corrected_idivq(rcx); // kills rbx
1454   __ mov(rax, rdx);
1455 #else
1456   __ pop_l(rbx, rcx);
1457   __ push(rcx); __ push(rbx);
1458   __ push(rdx); __ push(rax);
1459   // check if y = 0
1460   __ orl(rax, rdx);
1461   __ jump_cc(Assembler::zero,
1462              ExternalAddress(Interpreter::_throw_ArithmeticException_entry));
1463   __ call_VM_leaf(CAST_FROM_FN_PTR(address, SharedRuntime::lrem));
1464   __ addptr(rsp, 4 * wordSize);
1465 #endif
1466 }
1467 
1468 void TemplateTable::lshl() {
1469   transition(itos, ltos);
1470   __ movl(rcx, rax);                             // get shift count
1471   #ifdef _LP64
1472   __ pop_l(rax);                                 // get shift value
1473   __ shlq(rax);
1474 #else
1475   __ pop_l(rax, rdx);                            // get shift value
1476   __ lshl(rdx, rax);
1477 #endif
1478 }
1479 
1480 void TemplateTable::lshr() {
1481 #ifdef _LP64
1482   transition(itos, ltos);
1483   __ movl(rcx, rax);                             // get shift count
1484   __ pop_l(rax);                                 // get shift value
1485   __ sarq(rax);
1486 #else
1487   transition(itos, ltos);
1488   __ mov(rcx, rax);                              // get shift count
1489   __ pop_l(rax, rdx);                            // get shift value
1490   __ lshr(rdx, rax, true);
1491 #endif
1492 }
1493 
1494 void TemplateTable::lushr() {
1495   transition(itos, ltos);
1496 #ifdef _LP64
1497   __ movl(rcx, rax);                             // get shift count
1498   __ pop_l(rax);                                 // get shift value
1499   __ shrq(rax);
1500 #else
1501   __ mov(rcx, rax);                              // get shift count
1502   __ pop_l(rax, rdx);                            // get shift value
1503   __ lshr(rdx, rax);
1504 #endif
1505 }
1506 
1507 void TemplateTable::fop2(Operation op) {
1508   transition(ftos, ftos);
1509 
1510   if (UseSSE >= 1) {
1511     switch (op) {
1512     case add:
1513       __ addss(xmm0, at_rsp());
1514       __ addptr(rsp, Interpreter::stackElementSize);
1515       break;
1516     case sub:
1517       __ movflt(xmm1, xmm0);
1518       __ pop_f(xmm0);
1519       __ subss(xmm0, xmm1);
1520       break;
1521     case mul:
1522       __ mulss(xmm0, at_rsp());
1523       __ addptr(rsp, Interpreter::stackElementSize);
1524       break;
1525     case div:
1526       __ movflt(xmm1, xmm0);
1527       __ pop_f(xmm0);
1528       __ divss(xmm0, xmm1);
1529       break;
1530     case rem:
1531       // On x86_64 platforms the SharedRuntime::frem method is called to perform the
1532       // modulo operation. The frem method calls the function
1533       // double fmod(double x, double y) in math.h. The documentation of fmod states:
1534       // "If x or y is a NaN, a NaN is returned." without specifying what type of NaN
1535       // (signalling or quiet) is returned.
1536       //
1537       // On x86_32 platforms the FPU is used to perform the modulo operation. The
1538       // reason is that on 32-bit Windows the sign of modulo operations diverges from
1539       // what is considered the standard (e.g., -0.0f % -3.14f is 0.0f (and not -0.0f).
1540       // The fprem instruction used on x86_32 is functionally equivalent to
1541       // SharedRuntime::frem in that it returns a NaN.
1542 #ifdef _LP64
1543       __ movflt(xmm1, xmm0);
1544       __ pop_f(xmm0);
1545       __ call_VM_leaf(CAST_FROM_FN_PTR(address, SharedRuntime::frem), 2);
1546 #else
1547       __ push_f(xmm0);
1548       __ pop_f();
1549       __ fld_s(at_rsp());
1550       __ fremr(rax);
1551       __ f2ieee();
1552       __ pop(rax);  // pop second operand off the stack
1553       __ push_f();
1554       __ pop_f(xmm0);
1555 #endif
1556       break;
1557     default:
1558       ShouldNotReachHere();
1559       break;
1560     }
1561   } else {
1562 #ifdef _LP64
1563     ShouldNotReachHere();
1564 #else
1565     switch (op) {
1566     case add: __ fadd_s (at_rsp());                break;
1567     case sub: __ fsubr_s(at_rsp());                break;
1568     case mul: __ fmul_s (at_rsp());                break;
1569     case div: __ fdivr_s(at_rsp());                break;
1570     case rem: __ fld_s  (at_rsp()); __ fremr(rax); break;
1571     default : ShouldNotReachHere();
1572     }
1573     __ f2ieee();
1574     __ pop(rax);  // pop second operand off the stack
1575 #endif // _LP64
1576   }
1577 }
1578 
1579 void TemplateTable::dop2(Operation op) {
1580   transition(dtos, dtos);
1581   if (UseSSE >= 2) {
1582     switch (op) {
1583     case add:
1584       __ addsd(xmm0, at_rsp());
1585       __ addptr(rsp, 2 * Interpreter::stackElementSize);
1586       break;
1587     case sub:
1588       __ movdbl(xmm1, xmm0);
1589       __ pop_d(xmm0);
1590       __ subsd(xmm0, xmm1);
1591       break;
1592     case mul:
1593       __ mulsd(xmm0, at_rsp());
1594       __ addptr(rsp, 2 * Interpreter::stackElementSize);
1595       break;
1596     case div:
1597       __ movdbl(xmm1, xmm0);
1598       __ pop_d(xmm0);
1599       __ divsd(xmm0, xmm1);
1600       break;
1601     case rem:
1602       // Similar to fop2(), the modulo operation is performed using the
1603       // SharedRuntime::drem method (on x86_64 platforms) or using the
1604       // FPU (on x86_32 platforms) for the same reasons as mentioned in fop2().
1605 #ifdef _LP64
1606       __ movdbl(xmm1, xmm0);
1607       __ pop_d(xmm0);
1608       __ call_VM_leaf(CAST_FROM_FN_PTR(address, SharedRuntime::drem), 2);
1609 #else
1610       __ push_d(xmm0);
1611       __ pop_d();
1612       __ fld_d(at_rsp());
1613       __ fremr(rax);
1614       __ d2ieee();
1615       __ pop(rax);
1616       __ pop(rdx);
1617       __ push_d();
1618       __ pop_d(xmm0);
1619 #endif
1620       break;
1621     default:
1622       ShouldNotReachHere();
1623       break;
1624     }
1625   } else {
1626 #ifdef _LP64
1627     ShouldNotReachHere();
1628 #else
1629     switch (op) {
1630     case add: __ fadd_d (at_rsp());                break;
1631     case sub: __ fsubr_d(at_rsp());                break;
1632     case mul: {
1633       Label L_strict;
1634       Label L_join;
1635       const Address access_flags      (rcx, Method::access_flags_offset());
1636       __ get_method(rcx);
1637       __ movl(rcx, access_flags);
1638       __ testl(rcx, JVM_ACC_STRICT);
1639       __ jccb(Assembler::notZero, L_strict);
1640       __ fmul_d (at_rsp());
1641       __ jmpb(L_join);
1642       __ bind(L_strict);
1643       __ fld_x(ExternalAddress(StubRoutines::addr_fpu_subnormal_bias1()));
1644       __ fmulp();
1645       __ fmul_d (at_rsp());
1646       __ fld_x(ExternalAddress(StubRoutines::addr_fpu_subnormal_bias2()));
1647       __ fmulp();
1648       __ bind(L_join);
1649       break;
1650     }
1651     case div: {
1652       Label L_strict;
1653       Label L_join;
1654       const Address access_flags      (rcx, Method::access_flags_offset());
1655       __ get_method(rcx);
1656       __ movl(rcx, access_flags);
1657       __ testl(rcx, JVM_ACC_STRICT);
1658       __ jccb(Assembler::notZero, L_strict);
1659       __ fdivr_d(at_rsp());
1660       __ jmp(L_join);
1661       __ bind(L_strict);
1662       __ fld_x(ExternalAddress(StubRoutines::addr_fpu_subnormal_bias1()));
1663       __ fmul_d (at_rsp());
1664       __ fdivrp();
1665       __ fld_x(ExternalAddress(StubRoutines::addr_fpu_subnormal_bias2()));
1666       __ fmulp();
1667       __ bind(L_join);
1668       break;
1669     }
1670     case rem: __ fld_d  (at_rsp()); __ fremr(rax); break;
1671     default : ShouldNotReachHere();
1672     }
1673     __ d2ieee();
1674     // Pop double precision number from rsp.
1675     __ pop(rax);
1676     __ pop(rdx);
1677 #endif
1678   }
1679 }
1680 
1681 void TemplateTable::ineg() {
1682   transition(itos, itos);
1683   __ negl(rax);
1684 }
1685 
1686 void TemplateTable::lneg() {
1687   transition(ltos, ltos);
1688   LP64_ONLY(__ negq(rax));
1689   NOT_LP64(__ lneg(rdx, rax));
1690 }
1691 
1692 // Note: 'double' and 'long long' have 32-bits alignment on x86.
1693 static jlong* double_quadword(jlong *adr, jlong lo, jlong hi) {
1694   // Use the expression (adr)&(~0xF) to provide 128-bits aligned address
1695   // of 128-bits operands for SSE instructions.
1696   jlong *operand = (jlong*)(((intptr_t)adr)&((intptr_t)(~0xF)));
1697   // Store the value to a 128-bits operand.
1698   operand[0] = lo;
1699   operand[1] = hi;
1700   return operand;
1701 }
1702 
1703 // Buffer for 128-bits masks used by SSE instructions.
1704 static jlong float_signflip_pool[2*2];
1705 static jlong double_signflip_pool[2*2];
1706 
1707 void TemplateTable::fneg() {
1708   transition(ftos, ftos);
1709   if (UseSSE >= 1) {
1710     static jlong *float_signflip  = double_quadword(&float_signflip_pool[1],  CONST64(0x8000000080000000),  CONST64(0x8000000080000000));
1711     __ xorps(xmm0, ExternalAddress((address) float_signflip));
1712   } else {
1713     LP64_ONLY(ShouldNotReachHere());
1714     NOT_LP64(__ fchs());
1715   }
1716 }
1717 
1718 void TemplateTable::dneg() {
1719   transition(dtos, dtos);
1720   if (UseSSE >= 2) {
1721     static jlong *double_signflip =
1722       double_quadword(&double_signflip_pool[1], CONST64(0x8000000000000000), CONST64(0x8000000000000000));
1723     __ xorpd(xmm0, ExternalAddress((address) double_signflip));
1724   } else {
1725 #ifdef _LP64
1726     ShouldNotReachHere();
1727 #else
1728     __ fchs();
1729 #endif
1730   }
1731 }
1732 
1733 void TemplateTable::iinc() {
1734   transition(vtos, vtos);
1735   __ load_signed_byte(rdx, at_bcp(2)); // get constant
1736   locals_index(rbx);
1737   __ addl(iaddress(rbx), rdx);
1738 }
1739 
1740 void TemplateTable::wide_iinc() {
1741   transition(vtos, vtos);
1742   __ movl(rdx, at_bcp(4)); // get constant
1743   locals_index_wide(rbx);
1744   __ bswapl(rdx); // swap bytes & sign-extend constant
1745   __ sarl(rdx, 16);
1746   __ addl(iaddress(rbx), rdx);
1747   // Note: should probably use only one movl to get both
1748   //       the index and the constant -> fix this
1749 }
1750 
1751 void TemplateTable::convert() {
1752 #ifdef _LP64
1753   // Checking
1754 #ifdef ASSERT
1755   {
1756     TosState tos_in  = ilgl;
1757     TosState tos_out = ilgl;
1758     switch (bytecode()) {
1759     case Bytecodes::_i2l: // fall through
1760     case Bytecodes::_i2f: // fall through
1761     case Bytecodes::_i2d: // fall through
1762     case Bytecodes::_i2b: // fall through
1763     case Bytecodes::_i2c: // fall through
1764     case Bytecodes::_i2s: tos_in = itos; break;
1765     case Bytecodes::_l2i: // fall through
1766     case Bytecodes::_l2f: // fall through
1767     case Bytecodes::_l2d: tos_in = ltos; break;
1768     case Bytecodes::_f2i: // fall through
1769     case Bytecodes::_f2l: // fall through
1770     case Bytecodes::_f2d: tos_in = ftos; break;
1771     case Bytecodes::_d2i: // fall through
1772     case Bytecodes::_d2l: // fall through
1773     case Bytecodes::_d2f: tos_in = dtos; break;
1774     default             : ShouldNotReachHere();
1775     }
1776     switch (bytecode()) {
1777     case Bytecodes::_l2i: // fall through
1778     case Bytecodes::_f2i: // fall through
1779     case Bytecodes::_d2i: // fall through
1780     case Bytecodes::_i2b: // fall through
1781     case Bytecodes::_i2c: // fall through
1782     case Bytecodes::_i2s: tos_out = itos; break;
1783     case Bytecodes::_i2l: // fall through
1784     case Bytecodes::_f2l: // fall through
1785     case Bytecodes::_d2l: tos_out = ltos; break;
1786     case Bytecodes::_i2f: // fall through
1787     case Bytecodes::_l2f: // fall through
1788     case Bytecodes::_d2f: tos_out = ftos; break;
1789     case Bytecodes::_i2d: // fall through
1790     case Bytecodes::_l2d: // fall through
1791     case Bytecodes::_f2d: tos_out = dtos; break;
1792     default             : ShouldNotReachHere();
1793     }
1794     transition(tos_in, tos_out);
1795   }
1796 #endif // ASSERT
1797 
1798   static const int64_t is_nan = 0x8000000000000000L;
1799 
1800   // Conversion
1801   switch (bytecode()) {
1802   case Bytecodes::_i2l:
1803     __ movslq(rax, rax);
1804     break;
1805   case Bytecodes::_i2f:
1806     __ cvtsi2ssl(xmm0, rax);
1807     break;
1808   case Bytecodes::_i2d:
1809     __ cvtsi2sdl(xmm0, rax);
1810     break;
1811   case Bytecodes::_i2b:
1812     __ movsbl(rax, rax);
1813     break;
1814   case Bytecodes::_i2c:
1815     __ movzwl(rax, rax);
1816     break;
1817   case Bytecodes::_i2s:
1818     __ movswl(rax, rax);
1819     break;
1820   case Bytecodes::_l2i:
1821     __ movl(rax, rax);
1822     break;
1823   case Bytecodes::_l2f:
1824     __ cvtsi2ssq(xmm0, rax);
1825     break;
1826   case Bytecodes::_l2d:
1827     __ cvtsi2sdq(xmm0, rax);
1828     break;
1829   case Bytecodes::_f2i:
1830   {
1831     Label L;
1832     __ cvttss2sil(rax, xmm0);
1833     __ cmpl(rax, 0x80000000); // NaN or overflow/underflow?
1834     __ jcc(Assembler::notEqual, L);
1835     __ call_VM_leaf(CAST_FROM_FN_PTR(address, SharedRuntime::f2i), 1);
1836     __ bind(L);
1837   }
1838     break;
1839   case Bytecodes::_f2l:
1840   {
1841     Label L;
1842     __ cvttss2siq(rax, xmm0);
1843     // NaN or overflow/underflow?
1844     __ cmp64(rax, ExternalAddress((address) &is_nan));
1845     __ jcc(Assembler::notEqual, L);
1846     __ call_VM_leaf(CAST_FROM_FN_PTR(address, SharedRuntime::f2l), 1);
1847     __ bind(L);
1848   }
1849     break;
1850   case Bytecodes::_f2d:
1851     __ cvtss2sd(xmm0, xmm0);
1852     break;
1853   case Bytecodes::_d2i:
1854   {
1855     Label L;
1856     __ cvttsd2sil(rax, xmm0);
1857     __ cmpl(rax, 0x80000000); // NaN or overflow/underflow?
1858     __ jcc(Assembler::notEqual, L);
1859     __ call_VM_leaf(CAST_FROM_FN_PTR(address, SharedRuntime::d2i), 1);
1860     __ bind(L);
1861   }
1862     break;
1863   case Bytecodes::_d2l:
1864   {
1865     Label L;
1866     __ cvttsd2siq(rax, xmm0);
1867     // NaN or overflow/underflow?
1868     __ cmp64(rax, ExternalAddress((address) &is_nan));
1869     __ jcc(Assembler::notEqual, L);
1870     __ call_VM_leaf(CAST_FROM_FN_PTR(address, SharedRuntime::d2l), 1);
1871     __ bind(L);
1872   }
1873     break;
1874   case Bytecodes::_d2f:
1875     __ cvtsd2ss(xmm0, xmm0);
1876     break;
1877   default:
1878     ShouldNotReachHere();
1879   }
1880 #else
1881   // Checking
1882 #ifdef ASSERT
1883   { TosState tos_in  = ilgl;
1884     TosState tos_out = ilgl;
1885     switch (bytecode()) {
1886       case Bytecodes::_i2l: // fall through
1887       case Bytecodes::_i2f: // fall through
1888       case Bytecodes::_i2d: // fall through
1889       case Bytecodes::_i2b: // fall through
1890       case Bytecodes::_i2c: // fall through
1891       case Bytecodes::_i2s: tos_in = itos; break;
1892       case Bytecodes::_l2i: // fall through
1893       case Bytecodes::_l2f: // fall through
1894       case Bytecodes::_l2d: tos_in = ltos; break;
1895       case Bytecodes::_f2i: // fall through
1896       case Bytecodes::_f2l: // fall through
1897       case Bytecodes::_f2d: tos_in = ftos; break;
1898       case Bytecodes::_d2i: // fall through
1899       case Bytecodes::_d2l: // fall through
1900       case Bytecodes::_d2f: tos_in = dtos; break;
1901       default             : ShouldNotReachHere();
1902     }
1903     switch (bytecode()) {
1904       case Bytecodes::_l2i: // fall through
1905       case Bytecodes::_f2i: // fall through
1906       case Bytecodes::_d2i: // fall through
1907       case Bytecodes::_i2b: // fall through
1908       case Bytecodes::_i2c: // fall through
1909       case Bytecodes::_i2s: tos_out = itos; break;
1910       case Bytecodes::_i2l: // fall through
1911       case Bytecodes::_f2l: // fall through
1912       case Bytecodes::_d2l: tos_out = ltos; break;
1913       case Bytecodes::_i2f: // fall through
1914       case Bytecodes::_l2f: // fall through
1915       case Bytecodes::_d2f: tos_out = ftos; break;
1916       case Bytecodes::_i2d: // fall through
1917       case Bytecodes::_l2d: // fall through
1918       case Bytecodes::_f2d: tos_out = dtos; break;
1919       default             : ShouldNotReachHere();
1920     }
1921     transition(tos_in, tos_out);
1922   }
1923 #endif // ASSERT
1924 
1925   // Conversion
1926   // (Note: use push(rcx)/pop(rcx) for 1/2-word stack-ptr manipulation)
1927   switch (bytecode()) {
1928     case Bytecodes::_i2l:
1929       __ extend_sign(rdx, rax);
1930       break;
1931     case Bytecodes::_i2f:
1932       if (UseSSE >= 1) {
1933         __ cvtsi2ssl(xmm0, rax);
1934       } else {
1935         __ push(rax);          // store int on tos
1936         __ fild_s(at_rsp());   // load int to ST0
1937         __ f2ieee();           // truncate to float size
1938         __ pop(rcx);           // adjust rsp
1939       }
1940       break;
1941     case Bytecodes::_i2d:
1942       if (UseSSE >= 2) {
1943         __ cvtsi2sdl(xmm0, rax);
1944       } else {
1945       __ push(rax);          // add one slot for d2ieee()
1946       __ push(rax);          // store int on tos
1947       __ fild_s(at_rsp());   // load int to ST0
1948       __ d2ieee();           // truncate to double size
1949       __ pop(rcx);           // adjust rsp
1950       __ pop(rcx);
1951       }
1952       break;
1953     case Bytecodes::_i2b:
1954       __ shll(rax, 24);      // truncate upper 24 bits
1955       __ sarl(rax, 24);      // and sign-extend byte
1956       LP64_ONLY(__ movsbl(rax, rax));
1957       break;
1958     case Bytecodes::_i2c:
1959       __ andl(rax, 0xFFFF);  // truncate upper 16 bits
1960       LP64_ONLY(__ movzwl(rax, rax));
1961       break;
1962     case Bytecodes::_i2s:
1963       __ shll(rax, 16);      // truncate upper 16 bits
1964       __ sarl(rax, 16);      // and sign-extend short
1965       LP64_ONLY(__ movswl(rax, rax));
1966       break;
1967     case Bytecodes::_l2i:
1968       /* nothing to do */
1969       break;
1970     case Bytecodes::_l2f:
1971       // On 64-bit platforms, the cvtsi2ssq instruction is used to convert
1972       // 64-bit long values to floats. On 32-bit platforms it is not possible
1973       // to use that instruction with 64-bit operands, therefore the FPU is
1974       // used to perform the conversion.
1975       __ push(rdx);          // store long on tos
1976       __ push(rax);
1977       __ fild_d(at_rsp());   // load long to ST0
1978       __ f2ieee();           // truncate to float size
1979       __ pop(rcx);           // adjust rsp
1980       __ pop(rcx);
1981       if (UseSSE >= 1) {
1982         __ push_f();
1983         __ pop_f(xmm0);
1984       }
1985       break;
1986     case Bytecodes::_l2d:
1987       // On 32-bit platforms the FPU is used for conversion because on
1988       // 32-bit platforms it is not not possible to use the cvtsi2sdq
1989       // instruction with 64-bit operands.
1990       __ push(rdx);          // store long on tos
1991       __ push(rax);
1992       __ fild_d(at_rsp());   // load long to ST0
1993       __ d2ieee();           // truncate to double size
1994       __ pop(rcx);           // adjust rsp
1995       __ pop(rcx);
1996       if (UseSSE >= 2) {
1997         __ push_d();
1998         __ pop_d(xmm0);
1999       }
2000       break;
2001     case Bytecodes::_f2i:
2002       // SharedRuntime::f2i does not differentiate between sNaNs and qNaNs
2003       // as it returns 0 for any NaN.
2004       if (UseSSE >= 1) {
2005         __ push_f(xmm0);
2006       } else {
2007         __ push(rcx);          // reserve space for argument
2008         __ fstp_s(at_rsp());   // pass float argument on stack
2009       }
2010       __ call_VM_leaf(CAST_FROM_FN_PTR(address, SharedRuntime::f2i), 1);
2011       break;
2012     case Bytecodes::_f2l:
2013       // SharedRuntime::f2l does not differentiate between sNaNs and qNaNs
2014       // as it returns 0 for any NaN.
2015       if (UseSSE >= 1) {
2016        __ push_f(xmm0);
2017       } else {
2018         __ push(rcx);          // reserve space for argument
2019         __ fstp_s(at_rsp());   // pass float argument on stack
2020       }
2021       __ call_VM_leaf(CAST_FROM_FN_PTR(address, SharedRuntime::f2l), 1);
2022       break;
2023     case Bytecodes::_f2d:
2024       if (UseSSE < 1) {
2025         /* nothing to do */
2026       } else if (UseSSE == 1) {
2027         __ push_f(xmm0);
2028         __ pop_f();
2029       } else { // UseSSE >= 2
2030         __ cvtss2sd(xmm0, xmm0);
2031       }
2032       break;
2033     case Bytecodes::_d2i:
2034       if (UseSSE >= 2) {
2035         __ push_d(xmm0);
2036       } else {
2037         __ push(rcx);          // reserve space for argument
2038         __ push(rcx);
2039         __ fstp_d(at_rsp());   // pass double argument on stack
2040       }
2041       __ call_VM_leaf(CAST_FROM_FN_PTR(address, SharedRuntime::d2i), 2);
2042       break;
2043     case Bytecodes::_d2l:
2044       if (UseSSE >= 2) {
2045         __ push_d(xmm0);
2046       } else {
2047         __ push(rcx);          // reserve space for argument
2048         __ push(rcx);
2049         __ fstp_d(at_rsp());   // pass double argument on stack
2050       }
2051       __ call_VM_leaf(CAST_FROM_FN_PTR(address, SharedRuntime::d2l), 2);
2052       break;
2053     case Bytecodes::_d2f:
2054       if (UseSSE <= 1) {
2055         __ push(rcx);          // reserve space for f2ieee()
2056         __ f2ieee();           // truncate to float size
2057         __ pop(rcx);           // adjust rsp
2058         if (UseSSE == 1) {
2059           // The cvtsd2ss instruction is not available if UseSSE==1, therefore
2060           // the conversion is performed using the FPU in this case.
2061           __ push_f();
2062           __ pop_f(xmm0);
2063         }
2064       } else { // UseSSE >= 2
2065         __ cvtsd2ss(xmm0, xmm0);
2066       }
2067       break;
2068     default             :
2069       ShouldNotReachHere();
2070   }
2071 #endif
2072 }
2073 
2074 void TemplateTable::lcmp() {
2075   transition(ltos, itos);
2076 #ifdef _LP64
2077   Label done;
2078   __ pop_l(rdx);
2079   __ cmpq(rdx, rax);
2080   __ movl(rax, -1);
2081   __ jccb(Assembler::less, done);
2082   __ setb(Assembler::notEqual, rax);
2083   __ movzbl(rax, rax);
2084   __ bind(done);
2085 #else
2086 
2087   // y = rdx:rax
2088   __ pop_l(rbx, rcx);             // get x = rcx:rbx
2089   __ lcmp2int(rcx, rbx, rdx, rax);// rcx := cmp(x, y)
2090   __ mov(rax, rcx);
2091 #endif
2092 }
2093 
2094 void TemplateTable::float_cmp(bool is_float, int unordered_result) {
2095   if ((is_float && UseSSE >= 1) ||
2096       (!is_float && UseSSE >= 2)) {
2097     Label done;
2098     if (is_float) {
2099       // XXX get rid of pop here, use ... reg, mem32
2100       __ pop_f(xmm1);
2101       __ ucomiss(xmm1, xmm0);
2102     } else {
2103       // XXX get rid of pop here, use ... reg, mem64
2104       __ pop_d(xmm1);
2105       __ ucomisd(xmm1, xmm0);
2106     }
2107     if (unordered_result < 0) {
2108       __ movl(rax, -1);
2109       __ jccb(Assembler::parity, done);
2110       __ jccb(Assembler::below, done);
2111       __ setb(Assembler::notEqual, rdx);
2112       __ movzbl(rax, rdx);
2113     } else {
2114       __ movl(rax, 1);
2115       __ jccb(Assembler::parity, done);
2116       __ jccb(Assembler::above, done);
2117       __ movl(rax, 0);
2118       __ jccb(Assembler::equal, done);
2119       __ decrementl(rax);
2120     }
2121     __ bind(done);
2122   } else {
2123 #ifdef _LP64
2124     ShouldNotReachHere();
2125 #else
2126     if (is_float) {
2127       __ fld_s(at_rsp());
2128     } else {
2129       __ fld_d(at_rsp());
2130       __ pop(rdx);
2131     }
2132     __ pop(rcx);
2133     __ fcmp2int(rax, unordered_result < 0);
2134 #endif // _LP64
2135   }
2136 }
2137 
2138 void TemplateTable::branch(bool is_jsr, bool is_wide) {
2139   __ get_method(rcx); // rcx holds method
2140   __ profile_taken_branch(rax, rbx); // rax holds updated MDP, rbx
2141                                      // holds bumped taken count
2142 
2143   const ByteSize be_offset = MethodCounters::backedge_counter_offset() +
2144                              InvocationCounter::counter_offset();
2145   const ByteSize inv_offset = MethodCounters::invocation_counter_offset() +
2146                               InvocationCounter::counter_offset();
2147 
2148   // Load up edx with the branch displacement
2149   if (is_wide) {
2150     __ movl(rdx, at_bcp(1));
2151   } else {
2152     __ load_signed_short(rdx, at_bcp(1));
2153   }
2154   __ bswapl(rdx);
2155 
2156   if (!is_wide) {
2157     __ sarl(rdx, 16);
2158   }
2159   LP64_ONLY(__ movl2ptr(rdx, rdx));
2160 
2161   // Handle all the JSR stuff here, then exit.
2162   // It's much shorter and cleaner than intermingling with the non-JSR
2163   // normal-branch stuff occurring below.
2164   if (is_jsr) {
2165     // Pre-load the next target bytecode into rbx
2166     __ load_unsigned_byte(rbx, Address(rbcp, rdx, Address::times_1, 0));
2167 
2168     // compute return address as bci in rax
2169     __ lea(rax, at_bcp((is_wide ? 5 : 3) -
2170                         in_bytes(ConstMethod::codes_offset())));
2171     __ subptr(rax, Address(rcx, Method::const_offset()));
2172     // Adjust the bcp in r13 by the displacement in rdx
2173     __ addptr(rbcp, rdx);
2174     // jsr returns atos that is not an oop
2175     __ push_i(rax);
2176     __ dispatch_only(vtos, true);
2177     return;
2178   }
2179 
2180   // Normal (non-jsr) branch handling
2181 
2182   // Adjust the bcp in r13 by the displacement in rdx
2183   __ addptr(rbcp, rdx);
2184 
2185   assert(UseLoopCounter || !UseOnStackReplacement,
2186          "on-stack-replacement requires loop counters");
2187   Label backedge_counter_overflow;
2188   Label profile_method;
2189   Label dispatch;
2190   if (UseLoopCounter) {
2191     // increment backedge counter for backward branches
2192     // rax: MDO
2193     // rbx: MDO bumped taken-count
2194     // rcx: method
2195     // rdx: target offset
2196     // r13: target bcp
2197     // r14: locals pointer
2198     __ testl(rdx, rdx);             // check if forward or backward branch
2199     __ jcc(Assembler::positive, dispatch); // count only if backward branch
2200 
2201     // check if MethodCounters exists
2202     Label has_counters;
2203     __ movptr(rax, Address(rcx, Method::method_counters_offset()));
2204     __ testptr(rax, rax);
2205     __ jcc(Assembler::notZero, has_counters);
2206     __ push(rdx);
2207     __ push(rcx);
2208     __ call_VM(noreg, CAST_FROM_FN_PTR(address, InterpreterRuntime::build_method_counters),
2209                rcx);
2210     __ pop(rcx);
2211     __ pop(rdx);
2212     __ movptr(rax, Address(rcx, Method::method_counters_offset()));
2213     __ testptr(rax, rax);
2214     __ jcc(Assembler::zero, dispatch);
2215     __ bind(has_counters);
2216 
2217     if (TieredCompilation) {
2218       Label no_mdo;
2219       int increment = InvocationCounter::count_increment;
2220       if (ProfileInterpreter) {
2221         // Are we profiling?
2222         __ movptr(rbx, Address(rcx, in_bytes(Method::method_data_offset())));
2223         __ testptr(rbx, rbx);
2224         __ jccb(Assembler::zero, no_mdo);
2225         // Increment the MDO backedge counter
2226         const Address mdo_backedge_counter(rbx, in_bytes(MethodData::backedge_counter_offset()) +
2227                                            in_bytes(InvocationCounter::counter_offset()));
2228         const Address mask(rbx, in_bytes(MethodData::backedge_mask_offset()));
2229         __ increment_mask_and_jump(mdo_backedge_counter, increment, mask,
2230                                    rax, false, Assembler::zero, &backedge_counter_overflow);
2231         __ jmp(dispatch);
2232       }
2233       __ bind(no_mdo);
2234       // Increment backedge counter in MethodCounters*
2235       __ movptr(rcx, Address(rcx, Method::method_counters_offset()));
2236       const Address mask(rcx, in_bytes(MethodCounters::backedge_mask_offset()));
2237       __ increment_mask_and_jump(Address(rcx, be_offset), increment, mask,
2238                                  rax, false, Assembler::zero, &backedge_counter_overflow);
2239     } else { // not TieredCompilation
2240       // increment counter
2241       __ movptr(rcx, Address(rcx, Method::method_counters_offset()));
2242       __ movl(rax, Address(rcx, be_offset));        // load backedge counter
2243       __ incrementl(rax, InvocationCounter::count_increment); // increment counter
2244       __ movl(Address(rcx, be_offset), rax);        // store counter
2245 
2246       __ movl(rax, Address(rcx, inv_offset));    // load invocation counter
2247 
2248       __ andl(rax, InvocationCounter::count_mask_value); // and the status bits
2249       __ addl(rax, Address(rcx, be_offset));        // add both counters
2250 
2251       if (ProfileInterpreter) {
2252         // Test to see if we should create a method data oop
2253         __ cmp32(rax, Address(rcx, in_bytes(MethodCounters::interpreter_profile_limit_offset())));
2254         __ jcc(Assembler::less, dispatch);
2255 
2256         // if no method data exists, go to profile method
2257         __ test_method_data_pointer(rax, profile_method);
2258 
2259         if (UseOnStackReplacement) {
2260           // check for overflow against rbx which is the MDO taken count
2261           __ cmp32(rbx, Address(rcx, in_bytes(MethodCounters::interpreter_backward_branch_limit_offset())));
2262           __ jcc(Assembler::below, dispatch);
2263 
2264           // When ProfileInterpreter is on, the backedge_count comes
2265           // from the MethodData*, which value does not get reset on
2266           // the call to frequency_counter_overflow().  To avoid
2267           // excessive calls to the overflow routine while the method is
2268           // being compiled, add a second test to make sure the overflow
2269           // function is called only once every overflow_frequency.
2270           const int overflow_frequency = 1024;
2271           __ andl(rbx, overflow_frequency - 1);
2272           __ jcc(Assembler::zero, backedge_counter_overflow);
2273 
2274         }
2275       } else {
2276         if (UseOnStackReplacement) {
2277           // check for overflow against rax, which is the sum of the
2278           // counters
2279           __ cmp32(rax, Address(rcx, in_bytes(MethodCounters::interpreter_backward_branch_limit_offset())));
2280           __ jcc(Assembler::aboveEqual, backedge_counter_overflow);
2281 
2282         }
2283       }
2284     }
2285     __ bind(dispatch);
2286   }
2287 
2288   // Pre-load the next target bytecode into rbx
2289   __ load_unsigned_byte(rbx, Address(rbcp, 0));
2290 
2291   // continue with the bytecode @ target
2292   // rax: return bci for jsr's, unused otherwise
2293   // rbx: target bytecode
2294   // r13: target bcp
2295   __ dispatch_only(vtos, true);
2296 
2297   if (UseLoopCounter) {
2298     if (ProfileInterpreter) {
2299       // Out-of-line code to allocate method data oop.
2300       __ bind(profile_method);
2301       __ call_VM(noreg, CAST_FROM_FN_PTR(address, InterpreterRuntime::profile_method));
2302       __ set_method_data_pointer_for_bcp();
2303       __ jmp(dispatch);
2304     }
2305 
2306     if (UseOnStackReplacement) {
2307       // invocation counter overflow
2308       __ bind(backedge_counter_overflow);
2309       __ negptr(rdx);
2310       __ addptr(rdx, rbcp); // branch bcp
2311       // IcoResult frequency_counter_overflow([JavaThread*], address branch_bcp)
2312       __ call_VM(noreg,
2313                  CAST_FROM_FN_PTR(address,
2314                                   InterpreterRuntime::frequency_counter_overflow),
2315                  rdx);
2316 
2317       // rax: osr nmethod (osr ok) or NULL (osr not possible)
2318       // rdx: scratch
2319       // r14: locals pointer
2320       // r13: bcp
2321       __ testptr(rax, rax);                        // test result
2322       __ jcc(Assembler::zero, dispatch);         // no osr if null
2323       // nmethod may have been invalidated (VM may block upon call_VM return)
2324       __ cmpb(Address(rax, nmethod::state_offset()), nmethod::in_use);
2325       __ jcc(Assembler::notEqual, dispatch);
2326 
2327       // We have the address of an on stack replacement routine in rax.
2328       // In preparation of invoking it, first we must migrate the locals
2329       // and monitors from off the interpreter frame on the stack.
2330       // Ensure to save the osr nmethod over the migration call,
2331       // it will be preserved in rbx.
2332       __ mov(rbx, rax);
2333 
2334       NOT_LP64(__ get_thread(rcx));
2335 
2336       call_VM(noreg, CAST_FROM_FN_PTR(address, SharedRuntime::OSR_migration_begin));
2337 
2338       // rax is OSR buffer, move it to expected parameter location
2339       LP64_ONLY(__ mov(j_rarg0, rax));
2340       NOT_LP64(__ mov(rcx, rax));
2341       // We use j_rarg definitions here so that registers don't conflict as parameter
2342       // registers change across platforms as we are in the midst of a calling
2343       // sequence to the OSR nmethod and we don't want collision. These are NOT parameters.
2344 
2345       const Register retaddr   = LP64_ONLY(j_rarg2) NOT_LP64(rdi);
2346       const Register sender_sp = LP64_ONLY(j_rarg1) NOT_LP64(rdx);
2347 
2348       // pop the interpreter frame
2349       __ movptr(sender_sp, Address(rbp, frame::interpreter_frame_sender_sp_offset * wordSize)); // get sender sp
2350       __ leave();                                // remove frame anchor
2351       __ pop(retaddr);                           // get return address
2352       __ mov(rsp, sender_sp);                   // set sp to sender sp
2353       // Ensure compiled code always sees stack at proper alignment
2354       __ andptr(rsp, -(StackAlignmentInBytes));
2355 
2356       // unlike x86 we need no specialized return from compiled code
2357       // to the interpreter or the call stub.
2358 
2359       // push the return address
2360       __ push(retaddr);
2361 
2362       // and begin the OSR nmethod
2363       __ jmp(Address(rbx, nmethod::osr_entry_point_offset()));
2364     }
2365   }
2366 }
2367 
2368 void TemplateTable::if_0cmp(Condition cc) {
2369   transition(itos, vtos);
2370   // assume branch is more often taken than not (loops use backward branches)
2371   Label not_taken;
2372   __ testl(rax, rax);
2373   __ jcc(j_not(cc), not_taken);
2374   branch(false, false);
2375   __ bind(not_taken);
2376   __ profile_not_taken_branch(rax);
2377 }
2378 
2379 void TemplateTable::if_icmp(Condition cc) {
2380   transition(itos, vtos);
2381   // assume branch is more often taken than not (loops use backward branches)
2382   Label not_taken;
2383   __ pop_i(rdx);
2384   __ cmpl(rdx, rax);
2385   __ jcc(j_not(cc), not_taken);
2386   branch(false, false);
2387   __ bind(not_taken);
2388   __ profile_not_taken_branch(rax);
2389 }
2390 
2391 void TemplateTable::if_nullcmp(Condition cc) {
2392   transition(atos, vtos);
2393   // assume branch is more often taken than not (loops use backward branches)
2394   Label not_taken;
2395   __ testptr(rax, rax);
2396   __ jcc(j_not(cc), not_taken);
2397   branch(false, false);
2398   __ bind(not_taken);
2399   __ profile_not_taken_branch(rax);
2400 }
2401 
2402 void TemplateTable::if_acmp(Condition cc) {
2403   transition(atos, vtos);
2404   // assume branch is more often taken than not (loops use backward branches)
2405   Label not_taken;
2406   __ pop_ptr(rdx);
2407   __ cmpoop(rdx, rax);
2408   __ jcc(j_not(cc), not_taken);
2409   branch(false, false);
2410   __ bind(not_taken);
2411   __ profile_not_taken_branch(rax);
2412 }
2413 
2414 void TemplateTable::ret() {
2415   transition(vtos, vtos);
2416   locals_index(rbx);
2417   LP64_ONLY(__ movslq(rbx, iaddress(rbx))); // get return bci, compute return bcp
2418   NOT_LP64(__ movptr(rbx, iaddress(rbx)));
2419   __ profile_ret(rbx, rcx);
2420   __ get_method(rax);
2421   __ movptr(rbcp, Address(rax, Method::const_offset()));
2422   __ lea(rbcp, Address(rbcp, rbx, Address::times_1,
2423                       ConstMethod::codes_offset()));
2424   __ dispatch_next(vtos, 0, true);
2425 }
2426 
2427 void TemplateTable::wide_ret() {
2428   transition(vtos, vtos);
2429   locals_index_wide(rbx);
2430   __ movptr(rbx, aaddress(rbx)); // get return bci, compute return bcp
2431   __ profile_ret(rbx, rcx);
2432   __ get_method(rax);
2433   __ movptr(rbcp, Address(rax, Method::const_offset()));
2434   __ lea(rbcp, Address(rbcp, rbx, Address::times_1, ConstMethod::codes_offset()));
2435   __ dispatch_next(vtos, 0, true);
2436 }
2437 
2438 void TemplateTable::tableswitch() {
2439   Label default_case, continue_execution;
2440   transition(itos, vtos);
2441 
2442   // align r13/rsi
2443   __ lea(rbx, at_bcp(BytesPerInt));
2444   __ andptr(rbx, -BytesPerInt);
2445   // load lo & hi
2446   __ movl(rcx, Address(rbx, BytesPerInt));
2447   __ movl(rdx, Address(rbx, 2 * BytesPerInt));
2448   __ bswapl(rcx);
2449   __ bswapl(rdx);
2450   // check against lo & hi
2451   __ cmpl(rax, rcx);
2452   __ jcc(Assembler::less, default_case);
2453   __ cmpl(rax, rdx);
2454   __ jcc(Assembler::greater, default_case);
2455   // lookup dispatch offset
2456   __ subl(rax, rcx);
2457   __ movl(rdx, Address(rbx, rax, Address::times_4, 3 * BytesPerInt));
2458   __ profile_switch_case(rax, rbx, rcx);
2459   // continue execution
2460   __ bind(continue_execution);
2461   __ bswapl(rdx);
2462   LP64_ONLY(__ movl2ptr(rdx, rdx));
2463   __ load_unsigned_byte(rbx, Address(rbcp, rdx, Address::times_1));
2464   __ addptr(rbcp, rdx);
2465   __ dispatch_only(vtos, true);
2466   // handle default
2467   __ bind(default_case);
2468   __ profile_switch_default(rax);
2469   __ movl(rdx, Address(rbx, 0));
2470   __ jmp(continue_execution);
2471 }
2472 
2473 void TemplateTable::lookupswitch() {
2474   transition(itos, itos);
2475   __ stop("lookupswitch bytecode should have been rewritten");
2476 }
2477 
2478 void TemplateTable::fast_linearswitch() {
2479   transition(itos, vtos);
2480   Label loop_entry, loop, found, continue_execution;
2481   // bswap rax so we can avoid bswapping the table entries
2482   __ bswapl(rax);
2483   // align r13
2484   __ lea(rbx, at_bcp(BytesPerInt)); // btw: should be able to get rid of
2485                                     // this instruction (change offsets
2486                                     // below)
2487   __ andptr(rbx, -BytesPerInt);
2488   // set counter
2489   __ movl(rcx, Address(rbx, BytesPerInt));
2490   __ bswapl(rcx);
2491   __ jmpb(loop_entry);
2492   // table search
2493   __ bind(loop);
2494   __ cmpl(rax, Address(rbx, rcx, Address::times_8, 2 * BytesPerInt));
2495   __ jcc(Assembler::equal, found);
2496   __ bind(loop_entry);
2497   __ decrementl(rcx);
2498   __ jcc(Assembler::greaterEqual, loop);
2499   // default case
2500   __ profile_switch_default(rax);
2501   __ movl(rdx, Address(rbx, 0));
2502   __ jmp(continue_execution);
2503   // entry found -> get offset
2504   __ bind(found);
2505   __ movl(rdx, Address(rbx, rcx, Address::times_8, 3 * BytesPerInt));
2506   __ profile_switch_case(rcx, rax, rbx);
2507   // continue execution
2508   __ bind(continue_execution);
2509   __ bswapl(rdx);
2510   __ movl2ptr(rdx, rdx);
2511   __ load_unsigned_byte(rbx, Address(rbcp, rdx, Address::times_1));
2512   __ addptr(rbcp, rdx);
2513   __ dispatch_only(vtos, true);
2514 }
2515 
2516 void TemplateTable::fast_binaryswitch() {
2517   transition(itos, vtos);
2518   // Implementation using the following core algorithm:
2519   //
2520   // int binary_search(int key, LookupswitchPair* array, int n) {
2521   //   // Binary search according to "Methodik des Programmierens" by
2522   //   // Edsger W. Dijkstra and W.H.J. Feijen, Addison Wesley Germany 1985.
2523   //   int i = 0;
2524   //   int j = n;
2525   //   while (i+1 < j) {
2526   //     // invariant P: 0 <= i < j <= n and (a[i] <= key < a[j] or Q)
2527   //     // with      Q: for all i: 0 <= i < n: key < a[i]
2528   //     // where a stands for the array and assuming that the (inexisting)
2529   //     // element a[n] is infinitely big.
2530   //     int h = (i + j) >> 1;
2531   //     // i < h < j
2532   //     if (key < array[h].fast_match()) {
2533   //       j = h;
2534   //     } else {
2535   //       i = h;
2536   //     }
2537   //   }
2538   //   // R: a[i] <= key < a[i+1] or Q
2539   //   // (i.e., if key is within array, i is the correct index)
2540   //   return i;
2541   // }
2542 
2543   // Register allocation
2544   const Register key   = rax; // already set (tosca)
2545   const Register array = rbx;
2546   const Register i     = rcx;
2547   const Register j     = rdx;
2548   const Register h     = rdi;
2549   const Register temp  = rsi;
2550 
2551   // Find array start
2552   NOT_LP64(__ save_bcp());
2553 
2554   __ lea(array, at_bcp(3 * BytesPerInt)); // btw: should be able to
2555                                           // get rid of this
2556                                           // instruction (change
2557                                           // offsets below)
2558   __ andptr(array, -BytesPerInt);
2559 
2560   // Initialize i & j
2561   __ xorl(i, i);                            // i = 0;
2562   __ movl(j, Address(array, -BytesPerInt)); // j = length(array);
2563 
2564   // Convert j into native byteordering
2565   __ bswapl(j);
2566 
2567   // And start
2568   Label entry;
2569   __ jmp(entry);
2570 
2571   // binary search loop
2572   {
2573     Label loop;
2574     __ bind(loop);
2575     // int h = (i + j) >> 1;
2576     __ leal(h, Address(i, j, Address::times_1)); // h = i + j;
2577     __ sarl(h, 1);                               // h = (i + j) >> 1;
2578     // if (key < array[h].fast_match()) {
2579     //   j = h;
2580     // } else {
2581     //   i = h;
2582     // }
2583     // Convert array[h].match to native byte-ordering before compare
2584     __ movl(temp, Address(array, h, Address::times_8));
2585     __ bswapl(temp);
2586     __ cmpl(key, temp);
2587     // j = h if (key <  array[h].fast_match())
2588     __ cmov32(Assembler::less, j, h);
2589     // i = h if (key >= array[h].fast_match())
2590     __ cmov32(Assembler::greaterEqual, i, h);
2591     // while (i+1 < j)
2592     __ bind(entry);
2593     __ leal(h, Address(i, 1)); // i+1
2594     __ cmpl(h, j);             // i+1 < j
2595     __ jcc(Assembler::less, loop);
2596   }
2597 
2598   // end of binary search, result index is i (must check again!)
2599   Label default_case;
2600   // Convert array[i].match to native byte-ordering before compare
2601   __ movl(temp, Address(array, i, Address::times_8));
2602   __ bswapl(temp);
2603   __ cmpl(key, temp);
2604   __ jcc(Assembler::notEqual, default_case);
2605 
2606   // entry found -> j = offset
2607   __ movl(j , Address(array, i, Address::times_8, BytesPerInt));
2608   __ profile_switch_case(i, key, array);
2609   __ bswapl(j);
2610   LP64_ONLY(__ movslq(j, j));
2611 
2612   NOT_LP64(__ restore_bcp());
2613   NOT_LP64(__ restore_locals());                           // restore rdi
2614 
2615   __ load_unsigned_byte(rbx, Address(rbcp, j, Address::times_1));
2616   __ addptr(rbcp, j);
2617   __ dispatch_only(vtos, true);
2618 
2619   // default case -> j = default offset
2620   __ bind(default_case);
2621   __ profile_switch_default(i);
2622   __ movl(j, Address(array, -2 * BytesPerInt));
2623   __ bswapl(j);
2624   LP64_ONLY(__ movslq(j, j));
2625 
2626   NOT_LP64(__ restore_bcp());
2627   NOT_LP64(__ restore_locals());
2628 
2629   __ load_unsigned_byte(rbx, Address(rbcp, j, Address::times_1));
2630   __ addptr(rbcp, j);
2631   __ dispatch_only(vtos, true);
2632 }
2633 
2634 void TemplateTable::_return(TosState state) {
2635   transition(state, state);
2636 
2637   assert(_desc->calls_vm(),
2638          "inconsistent calls_vm information"); // call in remove_activation
2639 
2640   if (_desc->bytecode() == Bytecodes::_return_register_finalizer) {
2641     assert(state == vtos, "only valid state");
2642     Register robj = LP64_ONLY(c_rarg1) NOT_LP64(rax);
2643     __ movptr(robj, aaddress(0));
2644     __ load_klass(rdi, robj);
2645     __ movl(rdi, Address(rdi, Klass::access_flags_offset()));
2646     __ testl(rdi, JVM_ACC_HAS_FINALIZER);
2647     Label skip_register_finalizer;
2648     __ jcc(Assembler::zero, skip_register_finalizer);
2649 
2650     __ call_VM(noreg, CAST_FROM_FN_PTR(address, InterpreterRuntime::register_finalizer), robj);
2651 
2652     __ bind(skip_register_finalizer);
2653   }
2654 
2655   if (SafepointMechanism::uses_thread_local_poll() && _desc->bytecode() != Bytecodes::_return_register_finalizer) {
2656     Label no_safepoint;
2657     NOT_PRODUCT(__ block_comment("Thread-local Safepoint poll"));
2658 #ifdef _LP64
2659     __ testb(Address(r15_thread, Thread::polling_page_offset()), SafepointMechanism::poll_bit());
2660 #else
2661     const Register thread = rdi;
2662     __ get_thread(thread);
2663     __ testb(Address(thread, Thread::polling_page_offset()), SafepointMechanism::poll_bit());
2664 #endif
2665     __ jcc(Assembler::zero, no_safepoint);
2666     __ push(state);
2667     __ call_VM(noreg, CAST_FROM_FN_PTR(address,
2668                                     InterpreterRuntime::at_safepoint));
2669     __ pop(state);
2670     __ bind(no_safepoint);
2671   }
2672 
2673   // Narrow result if state is itos but result type is smaller.
2674   // Need to narrow in the return bytecode rather than in generate_return_entry
2675   // since compiled code callers expect the result to already be narrowed.
2676   if (state == itos) {
2677     __ narrow(rax);
2678   }
2679   __ remove_activation(state, rbcp);
2680 
2681   __ jmp(rbcp);
2682 }
2683 
2684 // ----------------------------------------------------------------------------
2685 // Volatile variables demand their effects be made known to all CPU's
2686 // in order.  Store buffers on most chips allow reads & writes to
2687 // reorder; the JMM's ReadAfterWrite.java test fails in -Xint mode
2688 // without some kind of memory barrier (i.e., it's not sufficient that
2689 // the interpreter does not reorder volatile references, the hardware
2690 // also must not reorder them).
2691 //
2692 // According to the new Java Memory Model (JMM):
2693 // (1) All volatiles are serialized wrt to each other.  ALSO reads &
2694 //     writes act as aquire & release, so:
2695 // (2) A read cannot let unrelated NON-volatile memory refs that
2696 //     happen after the read float up to before the read.  It's OK for
2697 //     non-volatile memory refs that happen before the volatile read to
2698 //     float down below it.
2699 // (3) Similar a volatile write cannot let unrelated NON-volatile
2700 //     memory refs that happen BEFORE the write float down to after the
2701 //     write.  It's OK for non-volatile memory refs that happen after the
2702 //     volatile write to float up before it.
2703 //
2704 // We only put in barriers around volatile refs (they are expensive),
2705 // not _between_ memory refs (that would require us to track the
2706 // flavor of the previous memory refs).  Requirements (2) and (3)
2707 // require some barriers before volatile stores and after volatile
2708 // loads.  These nearly cover requirement (1) but miss the
2709 // volatile-store-volatile-load case.  This final case is placed after
2710 // volatile-stores although it could just as well go before
2711 // volatile-loads.
2712 
2713 void TemplateTable::volatile_barrier(Assembler::Membar_mask_bits order_constraint ) {
2714   // Helper function to insert a is-volatile test and memory barrier
2715   if(!os::is_MP()) return;    // Not needed on single CPU
2716   __ membar(order_constraint);
2717 }
2718 
2719 void TemplateTable::resolve_cache_and_index(int byte_no,
2720                                             Register Rcache,
2721                                             Register index,
2722                                             size_t index_size) {
2723   const Register temp = rbx;
2724   assert_different_registers(Rcache, index, temp);
2725 
2726   Label resolved;
2727 
2728   Bytecodes::Code code = bytecode();
2729   switch (code) {
2730   case Bytecodes::_nofast_getfield: code = Bytecodes::_getfield; break;
2731   case Bytecodes::_nofast_putfield: code = Bytecodes::_putfield; break;
2732   default: break;
2733   }
2734 
2735   assert(byte_no == f1_byte || byte_no == f2_byte, "byte_no out of range");
2736   __ get_cache_and_index_and_bytecode_at_bcp(Rcache, index, temp, byte_no, 1, index_size);
2737   __ cmpl(temp, code);  // have we resolved this bytecode?
2738   __ jcc(Assembler::equal, resolved);
2739 
2740   // resolve first time through
2741   address entry = CAST_FROM_FN_PTR(address, InterpreterRuntime::resolve_from_cache);
2742   __ movl(temp, code);
2743   __ call_VM(noreg, entry, temp);
2744   // Update registers with resolved info
2745   __ get_cache_and_index_at_bcp(Rcache, index, 1, index_size);
2746   __ bind(resolved);
2747 }
2748 
2749 // The cache and index registers must be set before call
2750 void TemplateTable::load_field_cp_cache_entry(Register obj,
2751                                               Register cache,
2752                                               Register index,
2753                                               Register off,
2754                                               Register flags,
2755                                               bool is_static = false) {
2756   assert_different_registers(cache, index, flags, off);
2757 
2758   ByteSize cp_base_offset = ConstantPoolCache::base_offset();
2759   // Field offset
2760   __ movptr(off, Address(cache, index, Address::times_ptr,
2761                          in_bytes(cp_base_offset +
2762                                   ConstantPoolCacheEntry::f2_offset())));
2763   // Flags
2764   __ movl(flags, Address(cache, index, Address::times_ptr,
2765                          in_bytes(cp_base_offset +
2766                                   ConstantPoolCacheEntry::flags_offset())));
2767 
2768   // klass overwrite register
2769   if (is_static) {
2770     __ movptr(obj, Address(cache, index, Address::times_ptr,
2771                            in_bytes(cp_base_offset +
2772                                     ConstantPoolCacheEntry::f1_offset())));
2773     const int mirror_offset = in_bytes(Klass::java_mirror_offset());
2774     __ movptr(obj, Address(obj, mirror_offset));
2775     __ resolve_oop_handle(obj);
2776   }
2777 }
2778 
2779 void TemplateTable::load_invoke_cp_cache_entry(int byte_no,
2780                                                Register method,
2781                                                Register itable_index,
2782                                                Register flags,
2783                                                bool is_invokevirtual,
2784                                                bool is_invokevfinal, /*unused*/
2785                                                bool is_invokedynamic) {
2786   // setup registers
2787   const Register cache = rcx;
2788   const Register index = rdx;
2789   assert_different_registers(method, flags);
2790   assert_different_registers(method, cache, index);
2791   assert_different_registers(itable_index, flags);
2792   assert_different_registers(itable_index, cache, index);
2793   // determine constant pool cache field offsets
2794   assert(is_invokevirtual == (byte_no == f2_byte), "is_invokevirtual flag redundant");
2795   const int method_offset = in_bytes(
2796     ConstantPoolCache::base_offset() +
2797       ((byte_no == f2_byte)
2798        ? ConstantPoolCacheEntry::f2_offset()
2799        : ConstantPoolCacheEntry::f1_offset()));
2800   const int flags_offset = in_bytes(ConstantPoolCache::base_offset() +
2801                                     ConstantPoolCacheEntry::flags_offset());
2802   // access constant pool cache fields
2803   const int index_offset = in_bytes(ConstantPoolCache::base_offset() +
2804                                     ConstantPoolCacheEntry::f2_offset());
2805 
2806   size_t index_size = (is_invokedynamic ? sizeof(u4) : sizeof(u2));
2807   resolve_cache_and_index(byte_no, cache, index, index_size);
2808     __ movptr(method, Address(cache, index, Address::times_ptr, method_offset));
2809 
2810   if (itable_index != noreg) {
2811     // pick up itable or appendix index from f2 also:
2812     __ movptr(itable_index, Address(cache, index, Address::times_ptr, index_offset));
2813   }
2814   __ movl(flags, Address(cache, index, Address::times_ptr, flags_offset));
2815 }
2816 
2817 // The registers cache and index expected to be set before call.
2818 // Correct values of the cache and index registers are preserved.
2819 void TemplateTable::jvmti_post_field_access(Register cache,
2820                                             Register index,
2821                                             bool is_static,
2822                                             bool has_tos) {
2823   if (JvmtiExport::can_post_field_access()) {
2824     // Check to see if a field access watch has been set before we take
2825     // the time to call into the VM.
2826     Label L1;
2827     assert_different_registers(cache, index, rax);
2828     __ mov32(rax, ExternalAddress((address) JvmtiExport::get_field_access_count_addr()));
2829     __ testl(rax,rax);
2830     __ jcc(Assembler::zero, L1);
2831 
2832     // cache entry pointer
2833     __ addptr(cache, in_bytes(ConstantPoolCache::base_offset()));
2834     __ shll(index, LogBytesPerWord);
2835     __ addptr(cache, index);
2836     if (is_static) {
2837       __ xorptr(rax, rax);      // NULL object reference
2838     } else {
2839       __ pop(atos);         // Get the object
2840       __ verify_oop(rax);
2841       __ push(atos);        // Restore stack state
2842     }
2843     // rax,:   object pointer or NULL
2844     // cache: cache entry pointer
2845     __ call_VM(noreg, CAST_FROM_FN_PTR(address, InterpreterRuntime::post_field_access),
2846                rax, cache);
2847     __ get_cache_and_index_at_bcp(cache, index, 1);
2848     __ bind(L1);
2849   }
2850 }
2851 
2852 void TemplateTable::pop_and_check_object(Register r) {
2853   __ pop_ptr(r);
2854   __ null_check(r);  // for field access must check obj.
2855   __ verify_oop(r);
2856 }
2857 
2858 void TemplateTable::getfield_or_static(int byte_no, bool is_static, RewriteControl rc) {
2859   transition(vtos, vtos);
2860 
2861   const Register cache = rcx;
2862   const Register index = rdx;
2863   const Register obj   = LP64_ONLY(c_rarg3) NOT_LP64(rcx);
2864   const Register off   = rbx;
2865   const Register flags = rax;
2866   const Register bc    = LP64_ONLY(c_rarg3) NOT_LP64(rcx); // uses same reg as obj, so don't mix them
2867 
2868   resolve_cache_and_index(byte_no, cache, index, sizeof(u2));
2869   jvmti_post_field_access(cache, index, is_static, false);
2870   load_field_cp_cache_entry(obj, cache, index, off, flags, is_static);
2871 
2872   if (!is_static) pop_and_check_object(obj);
2873 
2874   const Address field(obj, off, Address::times_1, 0*wordSize);
2875 
2876   Label Done, notByte, notBool, notInt, notShort, notChar, notLong, notFloat, notObj, notDouble;
2877 
2878   __ shrl(flags, ConstantPoolCacheEntry::tos_state_shift);
2879   // Make sure we don't need to mask edx after the above shift
2880   assert(btos == 0, "change code, btos != 0");
2881 
2882   __ andl(flags, ConstantPoolCacheEntry::tos_state_mask);
2883 
2884   __ jcc(Assembler::notZero, notByte);
2885   // btos
2886   __ access_load_at(T_BYTE, IN_HEAP, rax, field, noreg, noreg);
2887   __ push(btos);
2888   // Rewrite bytecode to be faster
2889   if (!is_static && rc == may_rewrite) {
2890     patch_bytecode(Bytecodes::_fast_bgetfield, bc, rbx);
2891   }
2892   __ jmp(Done);
2893 
2894   __ bind(notByte);
2895   __ cmpl(flags, ztos);
2896   __ jcc(Assembler::notEqual, notBool);
2897 
2898   // ztos (same code as btos)
2899   __ access_load_at(T_BOOLEAN, IN_HEAP, rax, field, noreg, noreg);
2900   __ push(ztos);
2901   // Rewrite bytecode to be faster
2902   if (!is_static && rc == may_rewrite) {
2903     // use btos rewriting, no truncating to t/f bit is needed for getfield.
2904     patch_bytecode(Bytecodes::_fast_bgetfield, bc, rbx);
2905   }
2906   __ jmp(Done);
2907 
2908   __ bind(notBool);
2909   __ cmpl(flags, atos);
2910   __ jcc(Assembler::notEqual, notObj);
2911   // atos
2912   do_oop_load(_masm, field, rax);
2913   __ push(atos);
2914   if (!is_static && rc == may_rewrite) {
2915     patch_bytecode(Bytecodes::_fast_agetfield, bc, rbx);
2916   }
2917   __ jmp(Done);
2918 
2919   __ bind(notObj);
2920   __ cmpl(flags, itos);
2921   __ jcc(Assembler::notEqual, notInt);
2922   // itos
2923   __ access_load_at(T_INT, IN_HEAP, rax, field, noreg, noreg);
2924   __ push(itos);
2925   // Rewrite bytecode to be faster
2926   if (!is_static && rc == may_rewrite) {
2927     patch_bytecode(Bytecodes::_fast_igetfield, bc, rbx);
2928   }
2929   __ jmp(Done);
2930 
2931   __ bind(notInt);
2932   __ cmpl(flags, ctos);
2933   __ jcc(Assembler::notEqual, notChar);
2934   // ctos
2935   __ access_load_at(T_CHAR, IN_HEAP, rax, field, noreg, noreg);
2936   __ push(ctos);
2937   // Rewrite bytecode to be faster
2938   if (!is_static && rc == may_rewrite) {
2939     patch_bytecode(Bytecodes::_fast_cgetfield, bc, rbx);
2940   }
2941   __ jmp(Done);
2942 
2943   __ bind(notChar);
2944   __ cmpl(flags, stos);
2945   __ jcc(Assembler::notEqual, notShort);
2946   // stos
2947   __ access_load_at(T_SHORT, IN_HEAP, rax, field, noreg, noreg);
2948   __ push(stos);
2949   // Rewrite bytecode to be faster
2950   if (!is_static && rc == may_rewrite) {
2951     patch_bytecode(Bytecodes::_fast_sgetfield, bc, rbx);
2952   }
2953   __ jmp(Done);
2954 
2955   __ bind(notShort);
2956   __ cmpl(flags, ltos);
2957   __ jcc(Assembler::notEqual, notLong);
2958   // ltos
2959     // Generate code as if volatile (x86_32).  There just aren't enough registers to
2960     // save that information and this code is faster than the test.
2961   __ access_load_at(T_LONG, IN_HEAP | MO_RELAXED, noreg /* ltos */, field, noreg, noreg);
2962   __ push(ltos);
2963   // Rewrite bytecode to be faster
2964   LP64_ONLY(if (!is_static && rc == may_rewrite) patch_bytecode(Bytecodes::_fast_lgetfield, bc, rbx));
2965   __ jmp(Done);
2966 
2967   __ bind(notLong);
2968   __ cmpl(flags, ftos);
2969   __ jcc(Assembler::notEqual, notFloat);
2970   // ftos
2971 
2972   __ access_load_at(T_FLOAT, IN_HEAP, noreg /* ftos */, field, noreg, noreg);
2973   __ push(ftos);
2974   // Rewrite bytecode to be faster
2975   if (!is_static && rc == may_rewrite) {
2976     patch_bytecode(Bytecodes::_fast_fgetfield, bc, rbx);
2977   }
2978   __ jmp(Done);
2979 
2980   __ bind(notFloat);
2981 #ifdef ASSERT
2982   __ cmpl(flags, dtos);
2983   __ jcc(Assembler::notEqual, notDouble);
2984 #endif
2985   // dtos
2986   __ access_load_at(T_DOUBLE, IN_HEAP, noreg /* dtos */, field, noreg, noreg);
2987   __ push(dtos);
2988   // Rewrite bytecode to be faster
2989   if (!is_static && rc == may_rewrite) {
2990     patch_bytecode(Bytecodes::_fast_dgetfield, bc, rbx);
2991   }
2992 #ifdef ASSERT
2993   __ jmp(Done);
2994 
2995 
2996   __ bind(notDouble);
2997   __ stop("Bad state");
2998 #endif
2999 
3000   __ bind(Done);
3001   // [jk] not needed currently
3002   // volatile_barrier(Assembler::Membar_mask_bits(Assembler::LoadLoad |
3003   //                                              Assembler::LoadStore));
3004 }
3005 
3006 void TemplateTable::getfield(int byte_no) {
3007   getfield_or_static(byte_no, false);
3008 }
3009 
3010 void TemplateTable::nofast_getfield(int byte_no) {
3011   getfield_or_static(byte_no, false, may_not_rewrite);
3012 }
3013 
3014 void TemplateTable::getstatic(int byte_no) {
3015   getfield_or_static(byte_no, true);
3016 }
3017 
3018 
3019 // The registers cache and index expected to be set before call.
3020 // The function may destroy various registers, just not the cache and index registers.
3021 void TemplateTable::jvmti_post_field_mod(Register cache, Register index, bool is_static) {
3022 
3023   const Register robj = LP64_ONLY(c_rarg2)   NOT_LP64(rax);
3024   const Register RBX  = LP64_ONLY(c_rarg1)   NOT_LP64(rbx);
3025   const Register RCX  = LP64_ONLY(c_rarg3)   NOT_LP64(rcx);
3026   const Register RDX  = LP64_ONLY(rscratch1) NOT_LP64(rdx);
3027 
3028   ByteSize cp_base_offset = ConstantPoolCache::base_offset();
3029 
3030   if (JvmtiExport::can_post_field_modification()) {
3031     // Check to see if a field modification watch has been set before
3032     // we take the time to call into the VM.
3033     Label L1;
3034     assert_different_registers(cache, index, rax);
3035     __ mov32(rax, ExternalAddress((address)JvmtiExport::get_field_modification_count_addr()));
3036     __ testl(rax, rax);
3037     __ jcc(Assembler::zero, L1);
3038 
3039     __ get_cache_and_index_at_bcp(robj, RDX, 1);
3040 
3041 
3042     if (is_static) {
3043       // Life is simple.  Null out the object pointer.
3044       __ xorl(RBX, RBX);
3045 
3046     } else {
3047       // Life is harder. The stack holds the value on top, followed by
3048       // the object.  We don't know the size of the value, though; it
3049       // could be one or two words depending on its type. As a result,
3050       // we must find the type to determine where the object is.
3051 #ifndef _LP64
3052       Label two_word, valsize_known;
3053 #endif
3054       __ movl(RCX, Address(robj, RDX,
3055                            Address::times_ptr,
3056                            in_bytes(cp_base_offset +
3057                                      ConstantPoolCacheEntry::flags_offset())));
3058       NOT_LP64(__ mov(rbx, rsp));
3059       __ shrl(RCX, ConstantPoolCacheEntry::tos_state_shift);
3060 
3061       // Make sure we don't need to mask rcx after the above shift
3062       ConstantPoolCacheEntry::verify_tos_state_shift();
3063 #ifdef _LP64
3064       __ movptr(c_rarg1, at_tos_p1());  // initially assume a one word jvalue
3065       __ cmpl(c_rarg3, ltos);
3066       __ cmovptr(Assembler::equal,
3067                  c_rarg1, at_tos_p2()); // ltos (two word jvalue)
3068       __ cmpl(c_rarg3, dtos);
3069       __ cmovptr(Assembler::equal,
3070                  c_rarg1, at_tos_p2()); // dtos (two word jvalue)
3071 #else
3072       __ cmpl(rcx, ltos);
3073       __ jccb(Assembler::equal, two_word);
3074       __ cmpl(rcx, dtos);
3075       __ jccb(Assembler::equal, two_word);
3076       __ addptr(rbx, Interpreter::expr_offset_in_bytes(1)); // one word jvalue (not ltos, dtos)
3077       __ jmpb(valsize_known);
3078 
3079       __ bind(two_word);
3080       __ addptr(rbx, Interpreter::expr_offset_in_bytes(2)); // two words jvalue
3081 
3082       __ bind(valsize_known);
3083       // setup object pointer
3084       __ movptr(rbx, Address(rbx, 0));
3085 #endif
3086     }
3087     // cache entry pointer
3088     __ addptr(robj, in_bytes(cp_base_offset));
3089     __ shll(RDX, LogBytesPerWord);
3090     __ addptr(robj, RDX);
3091     // object (tos)
3092     __ mov(RCX, rsp);
3093     // c_rarg1: object pointer set up above (NULL if static)
3094     // c_rarg2: cache entry pointer
3095     // c_rarg3: jvalue object on the stack
3096     __ call_VM(noreg,
3097                CAST_FROM_FN_PTR(address,
3098                                 InterpreterRuntime::post_field_modification),
3099                RBX, robj, RCX);
3100     __ get_cache_and_index_at_bcp(cache, index, 1);
3101     __ bind(L1);
3102   }
3103 }
3104 
3105 void TemplateTable::putfield_or_static(int byte_no, bool is_static, RewriteControl rc) {
3106   transition(vtos, vtos);
3107 
3108   const Register cache = rcx;
3109   const Register index = rdx;
3110   const Register obj   = rcx;
3111   const Register off   = rbx;
3112   const Register flags = rax;
3113   const Register bc    = LP64_ONLY(c_rarg3) NOT_LP64(rcx);
3114 
3115   resolve_cache_and_index(byte_no, cache, index, sizeof(u2));
3116   jvmti_post_field_mod(cache, index, is_static);
3117   load_field_cp_cache_entry(obj, cache, index, off, flags, is_static);
3118 
3119   // [jk] not needed currently
3120   // volatile_barrier(Assembler::Membar_mask_bits(Assembler::LoadStore |
3121   //                                              Assembler::StoreStore));
3122 
3123   Label notVolatile, Done;
3124   __ movl(rdx, flags);
3125   __ shrl(rdx, ConstantPoolCacheEntry::is_volatile_shift);
3126   __ andl(rdx, 0x1);
3127 
3128   // field addresses
3129   const Address field(obj, off, Address::times_1, 0*wordSize);
3130   NOT_LP64( const Address hi(obj, off, Address::times_1, 1*wordSize);)
3131 
3132   Label notByte, notBool, notInt, notShort, notChar,
3133         notLong, notFloat, notObj, notDouble;
3134 
3135   __ shrl(flags, ConstantPoolCacheEntry::tos_state_shift);
3136 
3137   assert(btos == 0, "change code, btos != 0");
3138   __ andl(flags, ConstantPoolCacheEntry::tos_state_mask);
3139   __ jcc(Assembler::notZero, notByte);
3140 
3141   // btos
3142   {
3143     __ pop(btos);
3144     if (!is_static) pop_and_check_object(obj);
3145     __ access_store_at(T_BYTE, IN_HEAP, field, rax, noreg, noreg);
3146     if (!is_static && rc == may_rewrite) {
3147       patch_bytecode(Bytecodes::_fast_bputfield, bc, rbx, true, byte_no);
3148     }
3149     __ jmp(Done);
3150   }
3151 
3152   __ bind(notByte);
3153   __ cmpl(flags, ztos);
3154   __ jcc(Assembler::notEqual, notBool);
3155 
3156   // ztos
3157   {
3158     __ pop(ztos);
3159     if (!is_static) pop_and_check_object(obj);
3160     __ access_store_at(T_BOOLEAN, IN_HEAP, field, rax, noreg, noreg);
3161     if (!is_static && rc == may_rewrite) {
3162       patch_bytecode(Bytecodes::_fast_zputfield, bc, rbx, true, byte_no);
3163     }
3164     __ jmp(Done);
3165   }
3166 
3167   __ bind(notBool);
3168   __ cmpl(flags, atos);
3169   __ jcc(Assembler::notEqual, notObj);
3170 
3171   // atos
3172   {
3173     __ pop(atos);
3174     if (!is_static) pop_and_check_object(obj);
3175     // Store into the field
3176     do_oop_store(_masm, field, rax);
3177     if (!is_static && rc == may_rewrite) {
3178       patch_bytecode(Bytecodes::_fast_aputfield, bc, rbx, true, byte_no);
3179     }
3180     __ jmp(Done);
3181   }
3182 
3183   __ bind(notObj);
3184   __ cmpl(flags, itos);
3185   __ jcc(Assembler::notEqual, notInt);
3186 
3187   // itos
3188   {
3189     __ pop(itos);
3190     if (!is_static) pop_and_check_object(obj);
3191     __ access_store_at(T_INT, IN_HEAP, field, rax, noreg, noreg);
3192     if (!is_static && rc == may_rewrite) {
3193       patch_bytecode(Bytecodes::_fast_iputfield, bc, rbx, true, byte_no);
3194     }
3195     __ jmp(Done);
3196   }
3197 
3198   __ bind(notInt);
3199   __ cmpl(flags, ctos);
3200   __ jcc(Assembler::notEqual, notChar);
3201 
3202   // ctos
3203   {
3204     __ pop(ctos);
3205     if (!is_static) pop_and_check_object(obj);
3206     __ access_store_at(T_CHAR, IN_HEAP, field, rax, noreg, noreg);
3207     if (!is_static && rc == may_rewrite) {
3208       patch_bytecode(Bytecodes::_fast_cputfield, bc, rbx, true, byte_no);
3209     }
3210     __ jmp(Done);
3211   }
3212 
3213   __ bind(notChar);
3214   __ cmpl(flags, stos);
3215   __ jcc(Assembler::notEqual, notShort);
3216 
3217   // stos
3218   {
3219     __ pop(stos);
3220     if (!is_static) pop_and_check_object(obj);
3221     __ access_store_at(T_SHORT, IN_HEAP, field, rax, noreg, noreg);
3222     if (!is_static && rc == may_rewrite) {
3223       patch_bytecode(Bytecodes::_fast_sputfield, bc, rbx, true, byte_no);
3224     }
3225     __ jmp(Done);
3226   }
3227 
3228   __ bind(notShort);
3229   __ cmpl(flags, ltos);
3230   __ jcc(Assembler::notEqual, notLong);
3231 
3232   // ltos
3233 #ifdef _LP64
3234   {
3235     __ pop(ltos);
3236     if (!is_static) pop_and_check_object(obj);
3237     __ access_store_at(T_LONG, IN_HEAP, field, noreg /* ltos*/, noreg, noreg);
3238     if (!is_static && rc == may_rewrite) {
3239       patch_bytecode(Bytecodes::_fast_lputfield, bc, rbx, true, byte_no);
3240     }
3241     __ jmp(Done);
3242   }
3243 #else
3244   {
3245     Label notVolatileLong;
3246     __ testl(rdx, rdx);
3247     __ jcc(Assembler::zero, notVolatileLong);
3248 
3249     __ pop(ltos);  // overwrites rdx, do this after testing volatile.
3250     if (!is_static) pop_and_check_object(obj);
3251 
3252     // Replace with real volatile test
3253     __ access_store_at(T_LONG, IN_HEAP | MO_RELAXED, field, noreg /* ltos */, noreg, noreg);
3254     // volatile_barrier();
3255     volatile_barrier(Assembler::Membar_mask_bits(Assembler::StoreLoad |
3256                                                  Assembler::StoreStore));
3257     // Don't rewrite volatile version
3258     __ jmp(notVolatile);
3259 
3260     __ bind(notVolatileLong);
3261 
3262     __ pop(ltos);  // overwrites rdx
3263     if (!is_static) pop_and_check_object(obj);
3264     __ access_store_at(T_LONG, IN_HEAP, field, noreg /* ltos */, noreg, noreg);
3265     // Don't rewrite to _fast_lputfield for potential volatile case.
3266     __ jmp(notVolatile);
3267   }
3268 #endif // _LP64
3269 
3270   __ bind(notLong);
3271   __ cmpl(flags, ftos);
3272   __ jcc(Assembler::notEqual, notFloat);
3273 
3274   // ftos
3275   {
3276     __ pop(ftos);
3277     if (!is_static) pop_and_check_object(obj);
3278     __ access_store_at(T_FLOAT, IN_HEAP, field, noreg /* ftos */, noreg, noreg);
3279     if (!is_static && rc == may_rewrite) {
3280       patch_bytecode(Bytecodes::_fast_fputfield, bc, rbx, true, byte_no);
3281     }
3282     __ jmp(Done);
3283   }
3284 
3285   __ bind(notFloat);
3286 #ifdef ASSERT
3287   __ cmpl(flags, dtos);
3288   __ jcc(Assembler::notEqual, notDouble);
3289 #endif
3290 
3291   // dtos
3292   {
3293     __ pop(dtos);
3294     if (!is_static) pop_and_check_object(obj);
3295     __ access_store_at(T_DOUBLE, IN_HEAP, field, noreg /* dtos */, noreg, noreg);
3296     if (!is_static && rc == may_rewrite) {
3297       patch_bytecode(Bytecodes::_fast_dputfield, bc, rbx, true, byte_no);
3298     }
3299   }
3300 
3301 #ifdef ASSERT
3302   __ jmp(Done);
3303 
3304   __ bind(notDouble);
3305   __ stop("Bad state");
3306 #endif
3307 
3308   __ bind(Done);
3309 
3310   // Check for volatile store
3311   __ testl(rdx, rdx);
3312   __ jcc(Assembler::zero, notVolatile);
3313   volatile_barrier(Assembler::Membar_mask_bits(Assembler::StoreLoad |
3314                                                Assembler::StoreStore));
3315   __ bind(notVolatile);
3316 }
3317 
3318 void TemplateTable::putfield(int byte_no) {
3319   putfield_or_static(byte_no, false);
3320 }
3321 
3322 void TemplateTable::nofast_putfield(int byte_no) {
3323   putfield_or_static(byte_no, false, may_not_rewrite);
3324 }
3325 
3326 void TemplateTable::putstatic(int byte_no) {
3327   putfield_or_static(byte_no, true);
3328 }
3329 
3330 void TemplateTable::jvmti_post_fast_field_mod() {
3331 
3332   const Register scratch = LP64_ONLY(c_rarg3) NOT_LP64(rcx);
3333 
3334   if (JvmtiExport::can_post_field_modification()) {
3335     // Check to see if a field modification watch has been set before
3336     // we take the time to call into the VM.
3337     Label L2;
3338     __ mov32(scratch, ExternalAddress((address)JvmtiExport::get_field_modification_count_addr()));
3339     __ testl(scratch, scratch);
3340     __ jcc(Assembler::zero, L2);
3341     __ pop_ptr(rbx);                  // copy the object pointer from tos
3342     __ verify_oop(rbx);
3343     __ push_ptr(rbx);                 // put the object pointer back on tos
3344     // Save tos values before call_VM() clobbers them. Since we have
3345     // to do it for every data type, we use the saved values as the
3346     // jvalue object.
3347     switch (bytecode()) {          // load values into the jvalue object
3348     case Bytecodes::_fast_aputfield: __ push_ptr(rax); break;
3349     case Bytecodes::_fast_bputfield: // fall through
3350     case Bytecodes::_fast_zputfield: // fall through
3351     case Bytecodes::_fast_sputfield: // fall through
3352     case Bytecodes::_fast_cputfield: // fall through
3353     case Bytecodes::_fast_iputfield: __ push_i(rax); break;
3354     case Bytecodes::_fast_dputfield: __ push(dtos); break;
3355     case Bytecodes::_fast_fputfield: __ push(ftos); break;
3356     case Bytecodes::_fast_lputfield: __ push_l(rax); break;
3357 
3358     default:
3359       ShouldNotReachHere();
3360     }
3361     __ mov(scratch, rsp);             // points to jvalue on the stack
3362     // access constant pool cache entry
3363     LP64_ONLY(__ get_cache_entry_pointer_at_bcp(c_rarg2, rax, 1));
3364     NOT_LP64(__ get_cache_entry_pointer_at_bcp(rax, rdx, 1));
3365     __ verify_oop(rbx);
3366     // rbx: object pointer copied above
3367     // c_rarg2: cache entry pointer
3368     // c_rarg3: jvalue object on the stack
3369     LP64_ONLY(__ call_VM(noreg, CAST_FROM_FN_PTR(address, InterpreterRuntime::post_field_modification), rbx, c_rarg2, c_rarg3));
3370     NOT_LP64(__ call_VM(noreg, CAST_FROM_FN_PTR(address, InterpreterRuntime::post_field_modification), rbx, rax, rcx));
3371 
3372     switch (bytecode()) {             // restore tos values
3373     case Bytecodes::_fast_aputfield: __ pop_ptr(rax); break;
3374     case Bytecodes::_fast_bputfield: // fall through
3375     case Bytecodes::_fast_zputfield: // fall through
3376     case Bytecodes::_fast_sputfield: // fall through
3377     case Bytecodes::_fast_cputfield: // fall through
3378     case Bytecodes::_fast_iputfield: __ pop_i(rax); break;
3379     case Bytecodes::_fast_dputfield: __ pop(dtos); break;
3380     case Bytecodes::_fast_fputfield: __ pop(ftos); break;
3381     case Bytecodes::_fast_lputfield: __ pop_l(rax); break;
3382     default: break;
3383     }
3384     __ bind(L2);
3385   }
3386 }
3387 
3388 void TemplateTable::fast_storefield(TosState state) {
3389   transition(state, vtos);
3390 
3391   ByteSize base = ConstantPoolCache::base_offset();
3392 
3393   jvmti_post_fast_field_mod();
3394 
3395   // access constant pool cache
3396   __ get_cache_and_index_at_bcp(rcx, rbx, 1);
3397 
3398   // test for volatile with rdx but rdx is tos register for lputfield.
3399   __ movl(rdx, Address(rcx, rbx, Address::times_ptr,
3400                        in_bytes(base +
3401                                 ConstantPoolCacheEntry::flags_offset())));
3402 
3403   // replace index with field offset from cache entry
3404   __ movptr(rbx, Address(rcx, rbx, Address::times_ptr,
3405                          in_bytes(base + ConstantPoolCacheEntry::f2_offset())));
3406 
3407   // [jk] not needed currently
3408   // volatile_barrier(Assembler::Membar_mask_bits(Assembler::LoadStore |
3409   //                                              Assembler::StoreStore));
3410 
3411   Label notVolatile;
3412   __ shrl(rdx, ConstantPoolCacheEntry::is_volatile_shift);
3413   __ andl(rdx, 0x1);
3414 
3415   // Get object from stack
3416   pop_and_check_object(rcx);
3417 
3418   // field address
3419   const Address field(rcx, rbx, Address::times_1);
3420 
3421   // access field
3422   switch (bytecode()) {
3423   case Bytecodes::_fast_aputfield:
3424     do_oop_store(_masm, field, rax);
3425     break;
3426   case Bytecodes::_fast_lputfield:
3427 #ifdef _LP64
3428     __ access_store_at(T_LONG, IN_HEAP, field, noreg /* ltos */, noreg, noreg);
3429 #else
3430   __ stop("should not be rewritten");
3431 #endif
3432     break;
3433   case Bytecodes::_fast_iputfield:
3434     __ access_store_at(T_INT, IN_HEAP, field, rax, noreg, noreg);
3435     break;
3436   case Bytecodes::_fast_zputfield:
3437     __ access_store_at(T_BOOLEAN, IN_HEAP, field, rax, noreg, noreg);
3438     break;
3439   case Bytecodes::_fast_bputfield:
3440     __ access_store_at(T_BYTE, IN_HEAP, field, rax, noreg, noreg);
3441     break;
3442   case Bytecodes::_fast_sputfield:
3443     __ access_store_at(T_SHORT, IN_HEAP, field, rax, noreg, noreg);
3444     break;
3445   case Bytecodes::_fast_cputfield:
3446     __ access_store_at(T_CHAR, IN_HEAP, field, rax, noreg, noreg);
3447     break;
3448   case Bytecodes::_fast_fputfield:
3449     __ access_store_at(T_FLOAT, IN_HEAP, field, noreg /* ftos*/, noreg, noreg);
3450     break;
3451   case Bytecodes::_fast_dputfield:
3452     __ access_store_at(T_DOUBLE, IN_HEAP, field, noreg /* dtos*/, noreg, noreg);
3453     break;
3454   default:
3455     ShouldNotReachHere();
3456   }
3457 
3458   // Check for volatile store
3459   __ testl(rdx, rdx);
3460   __ jcc(Assembler::zero, notVolatile);
3461   volatile_barrier(Assembler::Membar_mask_bits(Assembler::StoreLoad |
3462                                                Assembler::StoreStore));
3463   __ bind(notVolatile);
3464 }
3465 
3466 void TemplateTable::fast_accessfield(TosState state) {
3467   transition(atos, state);
3468 
3469   // Do the JVMTI work here to avoid disturbing the register state below
3470   if (JvmtiExport::can_post_field_access()) {
3471     // Check to see if a field access watch has been set before we
3472     // take the time to call into the VM.
3473     Label L1;
3474     __ mov32(rcx, ExternalAddress((address) JvmtiExport::get_field_access_count_addr()));
3475     __ testl(rcx, rcx);
3476     __ jcc(Assembler::zero, L1);
3477     // access constant pool cache entry
3478     LP64_ONLY(__ get_cache_entry_pointer_at_bcp(c_rarg2, rcx, 1));
3479     NOT_LP64(__ get_cache_entry_pointer_at_bcp(rcx, rdx, 1));
3480     __ verify_oop(rax);
3481     __ push_ptr(rax);  // save object pointer before call_VM() clobbers it
3482     LP64_ONLY(__ mov(c_rarg1, rax));
3483     // c_rarg1: object pointer copied above
3484     // c_rarg2: cache entry pointer
3485     LP64_ONLY(__ call_VM(noreg, CAST_FROM_FN_PTR(address, InterpreterRuntime::post_field_access), c_rarg1, c_rarg2));
3486     NOT_LP64(__ call_VM(noreg, CAST_FROM_FN_PTR(address, InterpreterRuntime::post_field_access), rax, rcx));
3487     __ pop_ptr(rax); // restore object pointer
3488     __ bind(L1);
3489   }
3490 
3491   // access constant pool cache
3492   __ get_cache_and_index_at_bcp(rcx, rbx, 1);
3493   // replace index with field offset from cache entry
3494   // [jk] not needed currently
3495   // if (os::is_MP()) {
3496   //   __ movl(rdx, Address(rcx, rbx, Address::times_8,
3497   //                        in_bytes(ConstantPoolCache::base_offset() +
3498   //                                 ConstantPoolCacheEntry::flags_offset())));
3499   //   __ shrl(rdx, ConstantPoolCacheEntry::is_volatile_shift);
3500   //   __ andl(rdx, 0x1);
3501   // }
3502   __ movptr(rbx, Address(rcx, rbx, Address::times_ptr,
3503                          in_bytes(ConstantPoolCache::base_offset() +
3504                                   ConstantPoolCacheEntry::f2_offset())));
3505 
3506   // rax: object
3507   __ verify_oop(rax);
3508   __ null_check(rax);
3509   Address field(rax, rbx, Address::times_1);
3510 
3511   // access field
3512   switch (bytecode()) {
3513   case Bytecodes::_fast_agetfield:
3514     do_oop_load(_masm, field, rax);
3515     __ verify_oop(rax);
3516     break;
3517   case Bytecodes::_fast_lgetfield:
3518 #ifdef _LP64
3519     __ access_load_at(T_LONG, IN_HEAP, noreg /* ltos */, field, noreg, noreg);
3520 #else
3521   __ stop("should not be rewritten");
3522 #endif
3523     break;
3524   case Bytecodes::_fast_igetfield:
3525     __ access_load_at(T_INT, IN_HEAP, rax, field, noreg, noreg);
3526     break;
3527   case Bytecodes::_fast_bgetfield:
3528     __ access_load_at(T_BYTE, IN_HEAP, rax, field, noreg, noreg);
3529     break;
3530   case Bytecodes::_fast_sgetfield:
3531     __ access_load_at(T_SHORT, IN_HEAP, rax, field, noreg, noreg);
3532     break;
3533   case Bytecodes::_fast_cgetfield:
3534     __ access_load_at(T_CHAR, IN_HEAP, rax, field, noreg, noreg);
3535     break;
3536   case Bytecodes::_fast_fgetfield:
3537     __ access_load_at(T_FLOAT, IN_HEAP, noreg /* ftos */, field, noreg, noreg);
3538     break;
3539   case Bytecodes::_fast_dgetfield:
3540     __ access_load_at(T_DOUBLE, IN_HEAP, noreg /* dtos */, field, noreg, noreg);
3541     break;
3542   default:
3543     ShouldNotReachHere();
3544   }
3545   // [jk] not needed currently
3546   // if (os::is_MP()) {
3547   //   Label notVolatile;
3548   //   __ testl(rdx, rdx);
3549   //   __ jcc(Assembler::zero, notVolatile);
3550   //   __ membar(Assembler::LoadLoad);
3551   //   __ bind(notVolatile);
3552   //};
3553 }
3554 
3555 void TemplateTable::fast_xaccess(TosState state) {
3556   transition(vtos, state);
3557 
3558   // get receiver
3559   __ movptr(rax, aaddress(0));
3560   // access constant pool cache
3561   __ get_cache_and_index_at_bcp(rcx, rdx, 2);
3562   __ movptr(rbx,
3563             Address(rcx, rdx, Address::times_ptr,
3564                     in_bytes(ConstantPoolCache::base_offset() +
3565                              ConstantPoolCacheEntry::f2_offset())));
3566   // make sure exception is reported in correct bcp range (getfield is
3567   // next instruction)
3568   __ increment(rbcp);
3569   __ null_check(rax);
3570   const Address field = Address(rax, rbx, Address::times_1, 0*wordSize);
3571   switch (state) {
3572   case itos:
3573     __ access_load_at(T_INT, IN_HEAP, rax, field, noreg, noreg);
3574     break;
3575   case atos:
3576     do_oop_load(_masm, field, rax);
3577     __ verify_oop(rax);
3578     break;
3579   case ftos:
3580     __ access_load_at(T_FLOAT, IN_HEAP, noreg /* ftos */, field, noreg, noreg);
3581     break;
3582   default:
3583     ShouldNotReachHere();
3584   }
3585 
3586   // [jk] not needed currently
3587   // if (os::is_MP()) {
3588   //   Label notVolatile;
3589   //   __ movl(rdx, Address(rcx, rdx, Address::times_8,
3590   //                        in_bytes(ConstantPoolCache::base_offset() +
3591   //                                 ConstantPoolCacheEntry::flags_offset())));
3592   //   __ shrl(rdx, ConstantPoolCacheEntry::is_volatile_shift);
3593   //   __ testl(rdx, 0x1);
3594   //   __ jcc(Assembler::zero, notVolatile);
3595   //   __ membar(Assembler::LoadLoad);
3596   //   __ bind(notVolatile);
3597   // }
3598 
3599   __ decrement(rbcp);
3600 }
3601 
3602 //-----------------------------------------------------------------------------
3603 // Calls
3604 
3605 void TemplateTable::count_calls(Register method, Register temp) {
3606   // implemented elsewhere
3607   ShouldNotReachHere();
3608 }
3609 
3610 void TemplateTable::prepare_invoke(int byte_no,
3611                                    Register method,  // linked method (or i-klass)
3612                                    Register index,   // itable index, MethodType, etc.
3613                                    Register recv,    // if caller wants to see it
3614                                    Register flags    // if caller wants to test it
3615                                    ) {
3616   // determine flags
3617   const Bytecodes::Code code = bytecode();
3618   const bool is_invokeinterface  = code == Bytecodes::_invokeinterface;
3619   const bool is_invokedynamic    = code == Bytecodes::_invokedynamic;
3620   const bool is_invokehandle     = code == Bytecodes::_invokehandle;
3621   const bool is_invokevirtual    = code == Bytecodes::_invokevirtual;
3622   const bool is_invokespecial    = code == Bytecodes::_invokespecial;
3623   const bool load_receiver       = (recv  != noreg);
3624   const bool save_flags          = (flags != noreg);
3625   assert(load_receiver == (code != Bytecodes::_invokestatic && code != Bytecodes::_invokedynamic), "");
3626   assert(save_flags    == (is_invokeinterface || is_invokevirtual), "need flags for vfinal");
3627   assert(flags == noreg || flags == rdx, "");
3628   assert(recv  == noreg || recv  == rcx, "");
3629 
3630   // setup registers & access constant pool cache
3631   if (recv  == noreg)  recv  = rcx;
3632   if (flags == noreg)  flags = rdx;
3633   assert_different_registers(method, index, recv, flags);
3634 
3635   // save 'interpreter return address'
3636   __ save_bcp();
3637 
3638   load_invoke_cp_cache_entry(byte_no, method, index, flags, is_invokevirtual, false, is_invokedynamic);
3639 
3640   // maybe push appendix to arguments (just before return address)
3641   if (is_invokedynamic || is_invokehandle) {
3642     Label L_no_push;
3643     __ testl(flags, (1 << ConstantPoolCacheEntry::has_appendix_shift));
3644     __ jcc(Assembler::zero, L_no_push);
3645     // Push the appendix as a trailing parameter.
3646     // This must be done before we get the receiver,
3647     // since the parameter_size includes it.
3648     __ push(rbx);
3649     __ mov(rbx, index);
3650     assert(ConstantPoolCacheEntry::_indy_resolved_references_appendix_offset == 0, "appendix expected at index+0");
3651     __ load_resolved_reference_at_index(index, rbx);
3652     __ pop(rbx);
3653     __ push(index);  // push appendix (MethodType, CallSite, etc.)
3654     __ bind(L_no_push);
3655   }
3656 
3657   // load receiver if needed (after appendix is pushed so parameter size is correct)
3658   // Note: no return address pushed yet
3659   if (load_receiver) {
3660     __ movl(recv, flags);
3661     __ andl(recv, ConstantPoolCacheEntry::parameter_size_mask);
3662     const int no_return_pc_pushed_yet = -1;  // argument slot correction before we push return address
3663     const int receiver_is_at_end      = -1;  // back off one slot to get receiver
3664     Address recv_addr = __ argument_address(recv, no_return_pc_pushed_yet + receiver_is_at_end);
3665     __ movptr(recv, recv_addr);
3666     __ verify_oop(recv);
3667   }
3668 
3669   if (save_flags) {
3670     __ movl(rbcp, flags);
3671   }
3672 
3673   // compute return type
3674   __ shrl(flags, ConstantPoolCacheEntry::tos_state_shift);
3675   // Make sure we don't need to mask flags after the above shift
3676   ConstantPoolCacheEntry::verify_tos_state_shift();
3677   // load return address
3678   {
3679     const address table_addr = (address) Interpreter::invoke_return_entry_table_for(code);
3680     ExternalAddress table(table_addr);
3681     LP64_ONLY(__ lea(rscratch1, table));
3682     LP64_ONLY(__ movptr(flags, Address(rscratch1, flags, Address::times_ptr)));
3683     NOT_LP64(__ movptr(flags, ArrayAddress(table, Address(noreg, flags, Address::times_ptr))));
3684   }
3685 
3686   // push return address
3687   __ push(flags);
3688 
3689   // Restore flags value from the constant pool cache, and restore rsi
3690   // for later null checks.  r13 is the bytecode pointer
3691   if (save_flags) {
3692     __ movl(flags, rbcp);
3693     __ restore_bcp();
3694   }
3695 }
3696 
3697 void TemplateTable::invokevirtual_helper(Register index,
3698                                          Register recv,
3699                                          Register flags) {
3700   // Uses temporary registers rax, rdx
3701   assert_different_registers(index, recv, rax, rdx);
3702   assert(index == rbx, "");
3703   assert(recv  == rcx, "");
3704 
3705   // Test for an invoke of a final method
3706   Label notFinal;
3707   __ movl(rax, flags);
3708   __ andl(rax, (1 << ConstantPoolCacheEntry::is_vfinal_shift));
3709   __ jcc(Assembler::zero, notFinal);
3710 
3711   const Register method = index;  // method must be rbx
3712   assert(method == rbx,
3713          "Method* must be rbx for interpreter calling convention");
3714 
3715   // do the call - the index is actually the method to call
3716   // that is, f2 is a vtable index if !is_vfinal, else f2 is a Method*
3717 
3718   // It's final, need a null check here!
3719   __ null_check(recv);
3720 
3721   // profile this call
3722   __ profile_final_call(rax);
3723   __ profile_arguments_type(rax, method, rbcp, true);
3724 
3725   __ jump_from_interpreted(method, rax);
3726 
3727   __ bind(notFinal);
3728 
3729   // get receiver klass
3730   __ null_check(recv, oopDesc::klass_offset_in_bytes());
3731   __ load_klass(rax, recv);
3732 
3733   // profile this call
3734   __ profile_virtual_call(rax, rlocals, rdx);
3735   // get target Method* & entry point
3736   __ lookup_virtual_method(rax, index, method);
3737   __ profile_called_method(method, rdx, rbcp);
3738 
3739   __ profile_arguments_type(rdx, method, rbcp, true);
3740   __ jump_from_interpreted(method, rdx);
3741 }
3742 
3743 void TemplateTable::invokevirtual(int byte_no) {
3744   transition(vtos, vtos);
3745   assert(byte_no == f2_byte, "use this argument");
3746   prepare_invoke(byte_no,
3747                  rbx,    // method or vtable index
3748                  noreg,  // unused itable index
3749                  rcx, rdx); // recv, flags
3750 
3751   // rbx: index
3752   // rcx: receiver
3753   // rdx: flags
3754 
3755   invokevirtual_helper(rbx, rcx, rdx);
3756 }
3757 
3758 void TemplateTable::invokespecial(int byte_no) {
3759   transition(vtos, vtos);
3760   assert(byte_no == f1_byte, "use this argument");
3761   prepare_invoke(byte_no, rbx, noreg,  // get f1 Method*
3762                  rcx);  // get receiver also for null check
3763   __ verify_oop(rcx);
3764   __ null_check(rcx);
3765   // do the call
3766   __ profile_call(rax);
3767   __ profile_arguments_type(rax, rbx, rbcp, false);
3768   __ jump_from_interpreted(rbx, rax);
3769 }
3770 
3771 void TemplateTable::invokestatic(int byte_no) {
3772   transition(vtos, vtos);
3773   assert(byte_no == f1_byte, "use this argument");
3774   prepare_invoke(byte_no, rbx);  // get f1 Method*
3775   // do the call
3776   __ profile_call(rax);
3777   __ profile_arguments_type(rax, rbx, rbcp, false);
3778   __ jump_from_interpreted(rbx, rax);
3779 }
3780 
3781 
3782 void TemplateTable::fast_invokevfinal(int byte_no) {
3783   transition(vtos, vtos);
3784   assert(byte_no == f2_byte, "use this argument");
3785   __ stop("fast_invokevfinal not used on x86");
3786 }
3787 
3788 
3789 void TemplateTable::invokeinterface(int byte_no) {
3790   transition(vtos, vtos);
3791   assert(byte_no == f1_byte, "use this argument");
3792   prepare_invoke(byte_no, rax, rbx,  // get f1 Klass*, f2 Method*
3793                  rcx, rdx); // recv, flags
3794 
3795   // rax: reference klass (from f1)
3796   // rbx: method (from f2)
3797   // rcx: receiver
3798   // rdx: flags
3799 
3800   // Special case of invokeinterface called for virtual method of
3801   // java.lang.Object.  See cpCacheOop.cpp for details.
3802   // This code isn't produced by javac, but could be produced by
3803   // another compliant java compiler.
3804   Label notMethod;
3805   __ movl(rlocals, rdx);
3806   __ andl(rlocals, (1 << ConstantPoolCacheEntry::is_forced_virtual_shift));
3807 
3808   __ jcc(Assembler::zero, notMethod);
3809 
3810   invokevirtual_helper(rbx, rcx, rdx);
3811   __ bind(notMethod);
3812 
3813   // Get receiver klass into rdx - also a null check
3814   __ restore_locals();  // restore r14
3815   __ null_check(rcx, oopDesc::klass_offset_in_bytes());
3816   __ load_klass(rdx, rcx);
3817 
3818   Label no_such_interface, no_such_method;
3819 
3820   // Preserve method for throw_AbstractMethodErrorVerbose.
3821   __ mov(rcx, rbx);
3822   // Receiver subtype check against REFC.
3823   // Superklass in rax. Subklass in rdx. Blows rcx, rdi.
3824   __ lookup_interface_method(// inputs: rec. class, interface, itable index
3825                              rdx, rax, noreg,
3826                              // outputs: scan temp. reg, scan temp. reg
3827                              rbcp, rlocals,
3828                              no_such_interface,
3829                              /*return_method=*/false);
3830 
3831   // profile this call
3832   __ restore_bcp(); // rbcp was destroyed by receiver type check
3833   __ profile_virtual_call(rdx, rbcp, rlocals);
3834 
3835   // Get declaring interface class from method, and itable index
3836   __ movptr(rax, Address(rbx, Method::const_offset()));
3837   __ movptr(rax, Address(rax, ConstMethod::constants_offset()));
3838   __ movptr(rax, Address(rax, ConstantPool::pool_holder_offset_in_bytes()));
3839   __ movl(rbx, Address(rbx, Method::itable_index_offset()));
3840   __ subl(rbx, Method::itable_index_max);
3841   __ negl(rbx);
3842 
3843   // Preserve recvKlass for throw_AbstractMethodErrorVerbose.
3844   __ mov(rlocals, rdx);
3845   __ lookup_interface_method(// inputs: rec. class, interface, itable index
3846                              rlocals, rax, rbx,
3847                              // outputs: method, scan temp. reg
3848                              rbx, rbcp,
3849                              no_such_interface);
3850 
3851   // rbx: Method* to call
3852   // rcx: receiver
3853   // Check for abstract method error
3854   // Note: This should be done more efficiently via a throw_abstract_method_error
3855   //       interpreter entry point and a conditional jump to it in case of a null
3856   //       method.
3857   __ testptr(rbx, rbx);
3858   __ jcc(Assembler::zero, no_such_method);
3859 
3860   __ profile_called_method(rbx, rbcp, rdx);
3861   __ profile_arguments_type(rdx, rbx, rbcp, true);
3862 
3863   // do the call
3864   // rcx: receiver
3865   // rbx,: Method*
3866   __ jump_from_interpreted(rbx, rdx);
3867   __ should_not_reach_here();
3868 
3869   // exception handling code follows...
3870   // note: must restore interpreter registers to canonical
3871   //       state for exception handling to work correctly!
3872 
3873   __ bind(no_such_method);
3874   // throw exception
3875   __ pop(rbx);           // pop return address (pushed by prepare_invoke)
3876   __ restore_bcp();      // rbcp must be correct for exception handler   (was destroyed)
3877   __ restore_locals();   // make sure locals pointer is correct as well (was destroyed)
3878   // Pass arguments for generating a verbose error message.
3879 #ifdef _LP64
3880   Register recvKlass = c_rarg1;
3881   Register method    = c_rarg2;
3882   if (recvKlass != rdx) { __ movq(recvKlass, rdx); }
3883   if (method != rcx)    { __ movq(method, rcx);    }
3884 #else
3885   Register recvKlass = rdx;
3886   Register method    = rcx;
3887 #endif
3888   __ call_VM(noreg, CAST_FROM_FN_PTR(address, InterpreterRuntime::throw_AbstractMethodErrorVerbose),
3889              recvKlass, method);
3890   // The call_VM checks for exception, so we should never return here.
3891   __ should_not_reach_here();
3892 
3893   __ bind(no_such_interface);
3894   // throw exception
3895   __ pop(rbx);           // pop return address (pushed by prepare_invoke)
3896   __ restore_bcp();      // rbcp must be correct for exception handler   (was destroyed)
3897   __ restore_locals();   // make sure locals pointer is correct as well (was destroyed)
3898   // Pass arguments for generating a verbose error message.
3899   LP64_ONLY( if (recvKlass != rdx) { __ movq(recvKlass, rdx); } )
3900   __ call_VM(noreg, CAST_FROM_FN_PTR(address, InterpreterRuntime::throw_IncompatibleClassChangeErrorVerbose),
3901              recvKlass, rax);
3902   // the call_VM checks for exception, so we should never return here.
3903   __ should_not_reach_here();
3904 }
3905 
3906 void TemplateTable::invokehandle(int byte_no) {
3907   transition(vtos, vtos);
3908   assert(byte_no == f1_byte, "use this argument");
3909   const Register rbx_method = rbx;
3910   const Register rax_mtype  = rax;
3911   const Register rcx_recv   = rcx;
3912   const Register rdx_flags  = rdx;
3913 
3914   prepare_invoke(byte_no, rbx_method, rax_mtype, rcx_recv);
3915   __ verify_method_ptr(rbx_method);
3916   __ verify_oop(rcx_recv);
3917   __ null_check(rcx_recv);
3918 
3919   // rax: MethodType object (from cpool->resolved_references[f1], if necessary)
3920   // rbx: MH.invokeExact_MT method (from f2)
3921 
3922   // Note:  rax_mtype is already pushed (if necessary) by prepare_invoke
3923 
3924   // FIXME: profile the LambdaForm also
3925   __ profile_final_call(rax);
3926   __ profile_arguments_type(rdx, rbx_method, rbcp, true);
3927 
3928   __ jump_from_interpreted(rbx_method, rdx);
3929 }
3930 
3931 void TemplateTable::invokedynamic(int byte_no) {
3932   transition(vtos, vtos);
3933   assert(byte_no == f1_byte, "use this argument");
3934 
3935   const Register rbx_method   = rbx;
3936   const Register rax_callsite = rax;
3937 
3938   prepare_invoke(byte_no, rbx_method, rax_callsite);
3939 
3940   // rax: CallSite object (from cpool->resolved_references[f1])
3941   // rbx: MH.linkToCallSite method (from f2)
3942 
3943   // Note:  rax_callsite is already pushed by prepare_invoke
3944 
3945   // %%% should make a type profile for any invokedynamic that takes a ref argument
3946   // profile this call
3947   __ profile_call(rbcp);
3948   __ profile_arguments_type(rdx, rbx_method, rbcp, false);
3949 
3950   __ verify_oop(rax_callsite);
3951 
3952   __ jump_from_interpreted(rbx_method, rdx);
3953 }
3954 
3955 //-----------------------------------------------------------------------------
3956 // Allocation
3957 
3958 void TemplateTable::_new() {
3959   transition(vtos, atos);
3960   __ get_unsigned_2_byte_index_at_bcp(rdx, 1);
3961   Label slow_case;
3962   Label slow_case_no_pop;
3963   Label done;
3964   Label initialize_header;
3965   Label initialize_object;  // including clearing the fields
3966 
3967   __ get_cpool_and_tags(rcx, rax);
3968 
3969   // Make sure the class we're about to instantiate has been resolved.
3970   // This is done before loading InstanceKlass to be consistent with the order
3971   // how Constant Pool is updated (see ConstantPool::klass_at_put)
3972   const int tags_offset = Array<u1>::base_offset_in_bytes();
3973   __ cmpb(Address(rax, rdx, Address::times_1, tags_offset), JVM_CONSTANT_Class);
3974   __ jcc(Assembler::notEqual, slow_case_no_pop);
3975 
3976   // get InstanceKlass
3977   __ load_resolved_klass_at_index(rcx, rdx, rcx);
3978   __ push(rcx);  // save the contexts of klass for initializing the header
3979 
3980   // make sure klass is initialized & doesn't have finalizer
3981   // make sure klass is fully initialized
3982   __ cmpb(Address(rcx, InstanceKlass::init_state_offset()), InstanceKlass::fully_initialized);
3983   __ jcc(Assembler::notEqual, slow_case);
3984 
3985   // get instance_size in InstanceKlass (scaled to a count of bytes)
3986   __ movl(rdx, Address(rcx, Klass::layout_helper_offset()));
3987   // test to see if it has a finalizer or is malformed in some way
3988   __ testl(rdx, Klass::_lh_instance_slow_path_bit);
3989   __ jcc(Assembler::notZero, slow_case);
3990 
3991   // Allocate the instance:
3992   //  If TLAB is enabled:
3993   //    Try to allocate in the TLAB.
3994   //    If fails, go to the slow path.
3995   //  Else If inline contiguous allocations are enabled:
3996   //    Try to allocate in eden.
3997   //    If fails due to heap end, go to slow path.
3998   //
3999   //  If TLAB is enabled OR inline contiguous is enabled:
4000   //    Initialize the allocation.
4001   //    Exit.
4002   //
4003   //  Go to slow path.
4004 
4005   const bool allow_shared_alloc =
4006     Universe::heap()->supports_inline_contig_alloc();
4007 
4008   const Register thread = LP64_ONLY(r15_thread) NOT_LP64(rcx);
4009 #ifndef _LP64
4010   if (UseTLAB || allow_shared_alloc) {
4011     __ get_thread(thread);
4012   }
4013 #endif // _LP64
4014 
4015   if (UseTLAB) {
4016     __ tlab_allocate(thread, rax, rdx, 0, rcx, rbx, slow_case);
4017     if (ZeroTLAB) {
4018       // the fields have been already cleared
4019       __ jmp(initialize_header);
4020     } else {
4021       // initialize both the header and fields
4022       __ jmp(initialize_object);
4023     }
4024   } else {
4025     // Allocation in the shared Eden, if allowed.
4026     //
4027     // rdx: instance size in bytes
4028     __ eden_allocate(thread, rax, rdx, 0, rbx, slow_case);
4029   }
4030 
4031   // If UseTLAB or allow_shared_alloc are true, the object is created above and
4032   // there is an initialize need. Otherwise, skip and go to the slow path.
4033   if (UseTLAB || allow_shared_alloc) {
4034     // The object is initialized before the header.  If the object size is
4035     // zero, go directly to the header initialization.
4036     __ bind(initialize_object);
4037     __ decrement(rdx, sizeof(oopDesc));
4038     __ jcc(Assembler::zero, initialize_header);
4039 
4040     // Initialize topmost object field, divide rdx by 8, check if odd and
4041     // test if zero.
4042     __ xorl(rcx, rcx);    // use zero reg to clear memory (shorter code)
4043     __ shrl(rdx, LogBytesPerLong); // divide by 2*oopSize and set carry flag if odd
4044 
4045     // rdx must have been multiple of 8
4046 #ifdef ASSERT
4047     // make sure rdx was multiple of 8
4048     Label L;
4049     // Ignore partial flag stall after shrl() since it is debug VM
4050     __ jccb(Assembler::carryClear, L);
4051     __ stop("object size is not multiple of 2 - adjust this code");
4052     __ bind(L);
4053     // rdx must be > 0, no extra check needed here
4054 #endif
4055 
4056     // initialize remaining object fields: rdx was a multiple of 8
4057     { Label loop;
4058     __ bind(loop);
4059     __ movptr(Address(rax, rdx, Address::times_8, sizeof(oopDesc) - 1*oopSize), rcx);
4060     NOT_LP64(__ movptr(Address(rax, rdx, Address::times_8, sizeof(oopDesc) - 2*oopSize), rcx));
4061     __ decrement(rdx);
4062     __ jcc(Assembler::notZero, loop);
4063     }
4064 
4065     // initialize object header only.
4066     __ bind(initialize_header);
4067     if (UseBiasedLocking) {
4068       __ pop(rcx);   // get saved klass back in the register.
4069       __ movptr(rbx, Address(rcx, Klass::prototype_header_offset()));
4070       __ movptr(Address(rax, oopDesc::mark_offset_in_bytes ()), rbx);
4071     } else {
4072       __ movptr(Address(rax, oopDesc::mark_offset_in_bytes ()),
4073                 (intptr_t)markOopDesc::prototype()); // header
4074       __ pop(rcx);   // get saved klass back in the register.
4075     }
4076 #ifdef _LP64
4077     __ xorl(rsi, rsi); // use zero reg to clear memory (shorter code)
4078     __ store_klass_gap(rax, rsi);  // zero klass gap for compressed oops
4079 #endif
4080     __ store_klass(rax, rcx);  // klass
4081 
4082     {
4083       SkipIfEqual skip_if(_masm, &DTraceAllocProbes, 0);
4084       // Trigger dtrace event for fastpath
4085       __ push(atos);
4086       __ call_VM_leaf(
4087            CAST_FROM_FN_PTR(address, SharedRuntime::dtrace_object_alloc), rax);
4088       __ pop(atos);
4089     }
4090 
4091     __ jmp(done);
4092   }
4093 
4094   // slow case
4095   __ bind(slow_case);
4096   __ pop(rcx);   // restore stack pointer to what it was when we came in.
4097   __ bind(slow_case_no_pop);
4098 
4099   Register rarg1 = LP64_ONLY(c_rarg1) NOT_LP64(rax);
4100   Register rarg2 = LP64_ONLY(c_rarg2) NOT_LP64(rdx);
4101 
4102   __ get_constant_pool(rarg1);
4103   __ get_unsigned_2_byte_index_at_bcp(rarg2, 1);
4104   call_VM(rax, CAST_FROM_FN_PTR(address, InterpreterRuntime::_new), rarg1, rarg2);
4105    __ verify_oop(rax);
4106 
4107   // continue
4108   __ bind(done);
4109 }
4110 
4111 void TemplateTable::newarray() {
4112   transition(itos, atos);
4113   Register rarg1 = LP64_ONLY(c_rarg1) NOT_LP64(rdx);
4114   __ load_unsigned_byte(rarg1, at_bcp(1));
4115   call_VM(rax, CAST_FROM_FN_PTR(address, InterpreterRuntime::newarray),
4116           rarg1, rax);
4117 }
4118 
4119 void TemplateTable::anewarray() {
4120   transition(itos, atos);
4121 
4122   Register rarg1 = LP64_ONLY(c_rarg1) NOT_LP64(rcx);
4123   Register rarg2 = LP64_ONLY(c_rarg2) NOT_LP64(rdx);
4124 
4125   __ get_unsigned_2_byte_index_at_bcp(rarg2, 1);
4126   __ get_constant_pool(rarg1);
4127   call_VM(rax, CAST_FROM_FN_PTR(address, InterpreterRuntime::anewarray),
4128           rarg1, rarg2, rax);
4129 }
4130 
4131 void TemplateTable::arraylength() {
4132   transition(atos, itos);
4133   __ null_check(rax, arrayOopDesc::length_offset_in_bytes());
4134   __ movl(rax, Address(rax, arrayOopDesc::length_offset_in_bytes()));
4135 }
4136 
4137 void TemplateTable::checkcast() {
4138   transition(atos, atos);
4139   Label done, is_null, ok_is_subtype, quicked, resolved;
4140   __ testptr(rax, rax); // object is in rax
4141   __ jcc(Assembler::zero, is_null);
4142 
4143   // Get cpool & tags index
4144   __ get_cpool_and_tags(rcx, rdx); // rcx=cpool, rdx=tags array
4145   __ get_unsigned_2_byte_index_at_bcp(rbx, 1); // rbx=index
4146   // See if bytecode has already been quicked
4147   __ cmpb(Address(rdx, rbx,
4148                   Address::times_1,
4149                   Array<u1>::base_offset_in_bytes()),
4150           JVM_CONSTANT_Class);
4151   __ jcc(Assembler::equal, quicked);
4152   __ push(atos); // save receiver for result, and for GC
4153   call_VM(noreg, CAST_FROM_FN_PTR(address, InterpreterRuntime::quicken_io_cc));
4154 
4155   // vm_result_2 has metadata result
4156 #ifndef _LP64
4157   // borrow rdi from locals
4158   __ get_thread(rdi);
4159   __ get_vm_result_2(rax, rdi);
4160   __ restore_locals();
4161 #else
4162   __ get_vm_result_2(rax, r15_thread);
4163 #endif
4164 
4165   __ pop_ptr(rdx); // restore receiver
4166   __ jmpb(resolved);
4167 
4168   // Get superklass in rax and subklass in rbx
4169   __ bind(quicked);
4170   __ mov(rdx, rax); // Save object in rdx; rax needed for subtype check
4171   __ load_resolved_klass_at_index(rcx, rbx, rax);
4172 
4173   __ bind(resolved);
4174   __ load_klass(rbx, rdx);
4175 
4176   // Generate subtype check.  Blows rcx, rdi.  Object in rdx.
4177   // Superklass in rax.  Subklass in rbx.
4178   __ gen_subtype_check(rbx, ok_is_subtype);
4179 
4180   // Come here on failure
4181   __ push_ptr(rdx);
4182   // object is at TOS
4183   __ jump(ExternalAddress(Interpreter::_throw_ClassCastException_entry));
4184 
4185   // Come here on success
4186   __ bind(ok_is_subtype);
4187   __ mov(rax, rdx); // Restore object in rdx
4188 
4189   // Collect counts on whether this check-cast sees NULLs a lot or not.
4190   if (ProfileInterpreter) {
4191     __ jmp(done);
4192     __ bind(is_null);
4193     __ profile_null_seen(rcx);
4194   } else {
4195     __ bind(is_null);   // same as 'done'
4196   }
4197   __ bind(done);
4198 }
4199 
4200 void TemplateTable::instanceof() {
4201   transition(atos, itos);
4202   Label done, is_null, ok_is_subtype, quicked, resolved;
4203   __ testptr(rax, rax);
4204   __ jcc(Assembler::zero, is_null);
4205 
4206   // Get cpool & tags index
4207   __ get_cpool_and_tags(rcx, rdx); // rcx=cpool, rdx=tags array
4208   __ get_unsigned_2_byte_index_at_bcp(rbx, 1); // rbx=index
4209   // See if bytecode has already been quicked
4210   __ cmpb(Address(rdx, rbx,
4211                   Address::times_1,
4212                   Array<u1>::base_offset_in_bytes()),
4213           JVM_CONSTANT_Class);
4214   __ jcc(Assembler::equal, quicked);
4215 
4216   __ push(atos); // save receiver for result, and for GC
4217   call_VM(noreg, CAST_FROM_FN_PTR(address, InterpreterRuntime::quicken_io_cc));
4218   // vm_result_2 has metadata result
4219 
4220 #ifndef _LP64
4221   // borrow rdi from locals
4222   __ get_thread(rdi);
4223   __ get_vm_result_2(rax, rdi);
4224   __ restore_locals();
4225 #else
4226   __ get_vm_result_2(rax, r15_thread);
4227 #endif
4228 
4229   __ pop_ptr(rdx); // restore receiver
4230   __ verify_oop(rdx);
4231   __ load_klass(rdx, rdx);
4232   __ jmpb(resolved);
4233 
4234   // Get superklass in rax and subklass in rdx
4235   __ bind(quicked);
4236   __ load_klass(rdx, rax);
4237   __ load_resolved_klass_at_index(rcx, rbx, rax);
4238 
4239   __ bind(resolved);
4240 
4241   // Generate subtype check.  Blows rcx, rdi
4242   // Superklass in rax.  Subklass in rdx.
4243   __ gen_subtype_check(rdx, ok_is_subtype);
4244 
4245   // Come here on failure
4246   __ xorl(rax, rax);
4247   __ jmpb(done);
4248   // Come here on success
4249   __ bind(ok_is_subtype);
4250   __ movl(rax, 1);
4251 
4252   // Collect counts on whether this test sees NULLs a lot or not.
4253   if (ProfileInterpreter) {
4254     __ jmp(done);
4255     __ bind(is_null);
4256     __ profile_null_seen(rcx);
4257   } else {
4258     __ bind(is_null);   // same as 'done'
4259   }
4260   __ bind(done);
4261   // rax = 0: obj == NULL or  obj is not an instanceof the specified klass
4262   // rax = 1: obj != NULL and obj is     an instanceof the specified klass
4263 }
4264 
4265 
4266 //----------------------------------------------------------------------------------------------------
4267 // Breakpoints
4268 void TemplateTable::_breakpoint() {
4269   // Note: We get here even if we are single stepping..
4270   // jbug insists on setting breakpoints at every bytecode
4271   // even if we are in single step mode.
4272 
4273   transition(vtos, vtos);
4274 
4275   Register rarg = LP64_ONLY(c_rarg1) NOT_LP64(rcx);
4276 
4277   // get the unpatched byte code
4278   __ get_method(rarg);
4279   __ call_VM(noreg,
4280              CAST_FROM_FN_PTR(address,
4281                               InterpreterRuntime::get_original_bytecode_at),
4282              rarg, rbcp);
4283   __ mov(rbx, rax);  // why?
4284 
4285   // post the breakpoint event
4286   __ get_method(rarg);
4287   __ call_VM(noreg,
4288              CAST_FROM_FN_PTR(address, InterpreterRuntime::_breakpoint),
4289              rarg, rbcp);
4290 
4291   // complete the execution of original bytecode
4292   __ dispatch_only_normal(vtos);
4293 }
4294 
4295 //-----------------------------------------------------------------------------
4296 // Exceptions
4297 
4298 void TemplateTable::athrow() {
4299   transition(atos, vtos);
4300   __ null_check(rax);
4301   __ jump(ExternalAddress(Interpreter::throw_exception_entry()));
4302 }
4303 
4304 //-----------------------------------------------------------------------------
4305 // Synchronization
4306 //
4307 // Note: monitorenter & exit are symmetric routines; which is reflected
4308 //       in the assembly code structure as well
4309 //
4310 // Stack layout:
4311 //
4312 // [expressions  ] <--- rsp               = expression stack top
4313 // ..
4314 // [expressions  ]
4315 // [monitor entry] <--- monitor block top = expression stack bot
4316 // ..
4317 // [monitor entry]
4318 // [frame data   ] <--- monitor block bot
4319 // ...
4320 // [saved rbp    ] <--- rbp
4321 void TemplateTable::monitorenter() {
4322   transition(atos, vtos);
4323 
4324   // check for NULL object
4325   __ null_check(rax);
4326 
4327   __ resolve_for_write(OOP_NOT_NULL, rax);
4328 
4329   const Address monitor_block_top(
4330         rbp, frame::interpreter_frame_monitor_block_top_offset * wordSize);
4331   const Address monitor_block_bot(
4332         rbp, frame::interpreter_frame_initial_sp_offset * wordSize);
4333   const int entry_size = frame::interpreter_frame_monitor_size() * wordSize;
4334 
4335   Label allocated;
4336 
4337   Register rtop = LP64_ONLY(c_rarg3) NOT_LP64(rcx);
4338   Register rbot = LP64_ONLY(c_rarg2) NOT_LP64(rbx);
4339   Register rmon = LP64_ONLY(c_rarg1) NOT_LP64(rdx);
4340 
4341   // initialize entry pointer
4342   __ xorl(rmon, rmon); // points to free slot or NULL
4343 
4344   // find a free slot in the monitor block (result in rmon)
4345   {
4346     Label entry, loop, exit;
4347     __ movptr(rtop, monitor_block_top); // points to current entry,
4348                                         // starting with top-most entry
4349     __ lea(rbot, monitor_block_bot);    // points to word before bottom
4350                                         // of monitor block
4351     __ jmpb(entry);
4352 
4353     __ bind(loop);
4354     // check if current entry is used
4355     __ cmpptr(Address(rtop, BasicObjectLock::obj_offset_in_bytes()), (int32_t) NULL_WORD);
4356     // if not used then remember entry in rmon
4357     __ cmovptr(Assembler::equal, rmon, rtop);   // cmov => cmovptr
4358     // check if current entry is for same object
4359     __ cmpptr(rax, Address(rtop, BasicObjectLock::obj_offset_in_bytes()));
4360     // if same object then stop searching
4361     __ jccb(Assembler::equal, exit);
4362     // otherwise advance to next entry
4363     __ addptr(rtop, entry_size);
4364     __ bind(entry);
4365     // check if bottom reached
4366     __ cmpptr(rtop, rbot);
4367     // if not at bottom then check this entry
4368     __ jcc(Assembler::notEqual, loop);
4369     __ bind(exit);
4370   }
4371 
4372   __ testptr(rmon, rmon); // check if a slot has been found
4373   __ jcc(Assembler::notZero, allocated); // if found, continue with that one
4374 
4375   // allocate one if there's no free slot
4376   {
4377     Label entry, loop;
4378     // 1. compute new pointers          // rsp: old expression stack top
4379     __ movptr(rmon, monitor_block_bot); // rmon: old expression stack bottom
4380     __ subptr(rsp, entry_size);         // move expression stack top
4381     __ subptr(rmon, entry_size);        // move expression stack bottom
4382     __ mov(rtop, rsp);                  // set start value for copy loop
4383     __ movptr(monitor_block_bot, rmon); // set new monitor block bottom
4384     __ jmp(entry);
4385     // 2. move expression stack contents
4386     __ bind(loop);
4387     __ movptr(rbot, Address(rtop, entry_size)); // load expression stack
4388                                                 // word from old location
4389     __ movptr(Address(rtop, 0), rbot);          // and store it at new location
4390     __ addptr(rtop, wordSize);                  // advance to next word
4391     __ bind(entry);
4392     __ cmpptr(rtop, rmon);                      // check if bottom reached
4393     __ jcc(Assembler::notEqual, loop);          // if not at bottom then
4394                                                 // copy next word
4395   }
4396 
4397   // call run-time routine
4398   // rmon: points to monitor entry
4399   __ bind(allocated);
4400 
4401   // Increment bcp to point to the next bytecode, so exception
4402   // handling for async. exceptions work correctly.
4403   // The object has already been poped from the stack, so the
4404   // expression stack looks correct.
4405   __ increment(rbcp);
4406 
4407   // store object
4408   __ movptr(Address(rmon, BasicObjectLock::obj_offset_in_bytes()), rax);
4409   __ lock_object(rmon);
4410 
4411   // check to make sure this monitor doesn't cause stack overflow after locking
4412   __ save_bcp();  // in case of exception
4413   __ generate_stack_overflow_check(0);
4414 
4415   // The bcp has already been incremented. Just need to dispatch to
4416   // next instruction.
4417   __ dispatch_next(vtos);
4418 }
4419 
4420 void TemplateTable::monitorexit() {
4421   transition(atos, vtos);
4422 
4423   // check for NULL object
4424   __ null_check(rax);
4425 
4426   __ resolve_for_write(OOP_NOT_NULL, rax);
4427 
4428   const Address monitor_block_top(
4429         rbp, frame::interpreter_frame_monitor_block_top_offset * wordSize);
4430   const Address monitor_block_bot(
4431         rbp, frame::interpreter_frame_initial_sp_offset * wordSize);
4432   const int entry_size = frame::interpreter_frame_monitor_size() * wordSize;
4433 
4434   Register rtop = LP64_ONLY(c_rarg1) NOT_LP64(rdx);
4435   Register rbot = LP64_ONLY(c_rarg2) NOT_LP64(rbx);
4436 
4437   Label found;
4438 
4439   // find matching slot
4440   {
4441     Label entry, loop;
4442     __ movptr(rtop, monitor_block_top); // points to current entry,
4443                                         // starting with top-most entry
4444     __ lea(rbot, monitor_block_bot);    // points to word before bottom
4445                                         // of monitor block
4446     __ jmpb(entry);
4447 
4448     __ bind(loop);
4449     // check if current entry is for same object
4450     __ cmpptr(rax, Address(rtop, BasicObjectLock::obj_offset_in_bytes()));
4451     // if same object then stop searching
4452     __ jcc(Assembler::equal, found);
4453     // otherwise advance to next entry
4454     __ addptr(rtop, entry_size);
4455     __ bind(entry);
4456     // check if bottom reached
4457     __ cmpptr(rtop, rbot);
4458     // if not at bottom then check this entry
4459     __ jcc(Assembler::notEqual, loop);
4460   }
4461 
4462   // error handling. Unlocking was not block-structured
4463   __ call_VM(noreg, CAST_FROM_FN_PTR(address,
4464                    InterpreterRuntime::throw_illegal_monitor_state_exception));
4465   __ should_not_reach_here();
4466 
4467   // call run-time routine
4468   __ bind(found);
4469   __ push_ptr(rax); // make sure object is on stack (contract with oopMaps)
4470   __ unlock_object(rtop);
4471   __ pop_ptr(rax); // discard object
4472 }
4473 
4474 // Wide instructions
4475 void TemplateTable::wide() {
4476   transition(vtos, vtos);
4477   __ load_unsigned_byte(rbx, at_bcp(1));
4478   ExternalAddress wtable((address)Interpreter::_wentry_point);
4479   __ jump(ArrayAddress(wtable, Address(noreg, rbx, Address::times_ptr)));
4480   // Note: the rbcp increment step is part of the individual wide bytecode implementations
4481 }
4482 
4483 // Multi arrays
4484 void TemplateTable::multianewarray() {
4485   transition(vtos, atos);
4486 
4487   Register rarg = LP64_ONLY(c_rarg1) NOT_LP64(rax);
4488   __ load_unsigned_byte(rax, at_bcp(3)); // get number of dimensions
4489   // last dim is on top of stack; we want address of first one:
4490   // first_addr = last_addr + (ndims - 1) * stackElementSize - 1*wordsize
4491   // the latter wordSize to point to the beginning of the array.
4492   __ lea(rarg, Address(rsp, rax, Interpreter::stackElementScale(), -wordSize));
4493   call_VM(rax, CAST_FROM_FN_PTR(address, InterpreterRuntime::multianewarray), rarg);
4494   __ load_unsigned_byte(rbx, at_bcp(3));
4495   __ lea(rsp, Address(rsp, rbx, Interpreter::stackElementScale()));  // get rid of counts
4496 }