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