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