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