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