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