1 /*
   2  * Copyright (c) 1997, 2017, 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.inline.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/sharedRuntime.hpp"
  37 #include "runtime/stubRoutines.hpp"
  38 #include "runtime/synchronizer.hpp"
  39 #include "utilities/macros.hpp"
  40 
  41 #define __ _masm->
  42 
  43 // Global Register Names
  44 static const Register rbcp     = LP64_ONLY(r13) NOT_LP64(rsi);
  45 static const Register rlocals  = LP64_ONLY(r14) NOT_LP64(rdi);
  46 
  47 // Platform-dependent initialization
  48 void TemplateTable::pd_initialize() {
  49   // No x86 specific initialization
  50 }
  51 
  52 // Address Computation: local variables
  53 static inline Address iaddress(int n) {
  54   return Address(rlocals, Interpreter::local_offset_in_bytes(n));
  55 }
  56 
  57 static inline Address laddress(int n) {
  58   return iaddress(n + 1);
  59 }
  60 
  61 #ifndef _LP64
  62 static inline Address haddress(int n) {
  63   return iaddress(n + 0);
  64 }
  65 #endif
  66 
  67 static inline Address faddress(int n) {
  68   return iaddress(n);
  69 }
  70 
  71 static inline Address daddress(int n) {
  72   return laddress(n);
  73 }
  74 
  75 static inline Address aaddress(int n) {
  76   return iaddress(n);
  77 }
  78 
  79 static inline Address iaddress(Register r) {
  80   return Address(rlocals, r, Address::times_ptr);
  81 }
  82 
  83 static inline Address laddress(Register r) {
  84   return Address(rlocals, r, Address::times_ptr, Interpreter::local_offset_in_bytes(1));
  85 }
  86 
  87 #ifndef _LP64
  88 static inline Address haddress(Register r)       {
  89   return Address(rlocals, r, Interpreter::stackElementScale(), Interpreter::local_offset_in_bytes(0));
  90 }
  91 #endif
  92 
  93 static inline Address faddress(Register r) {
  94   return iaddress(r);
  95 }
  96 
  97 static inline Address daddress(Register r) {
  98   return laddress(r);
  99 }
 100 
 101 static inline Address aaddress(Register r) {
 102   return iaddress(r);
 103 }
 104 
 105 
 106 // expression stack
 107 // (Note: Must not use symmetric equivalents at_rsp_m1/2 since they store
 108 // data beyond the rsp which is potentially unsafe in an MT environment;
 109 // an interrupt may overwrite that data.)
 110 static inline Address at_rsp   () {
 111   return Address(rsp, 0);
 112 }
 113 
 114 // At top of Java expression stack which may be different than esp().  It
 115 // isn't for category 1 objects.
 116 static inline Address at_tos   () {
 117   return Address(rsp,  Interpreter::expr_offset_in_bytes(0));
 118 }
 119 
 120 static inline Address at_tos_p1() {
 121   return Address(rsp,  Interpreter::expr_offset_in_bytes(1));
 122 }
 123 
 124 static inline Address at_tos_p2() {
 125   return Address(rsp,  Interpreter::expr_offset_in_bytes(2));
 126 }
 127 
 128 // Condition conversion
 129 static Assembler::Condition j_not(TemplateTable::Condition cc) {
 130   switch (cc) {
 131   case TemplateTable::equal        : return Assembler::notEqual;
 132   case TemplateTable::not_equal    : return Assembler::equal;
 133   case TemplateTable::less         : return Assembler::greaterEqual;
 134   case TemplateTable::less_equal   : return Assembler::greater;
 135   case TemplateTable::greater      : return Assembler::lessEqual;
 136   case TemplateTable::greater_equal: return Assembler::less;
 137   }
 138   ShouldNotReachHere();
 139   return Assembler::zero;
 140 }
 141 
 142 
 143 
 144 // Miscelaneous helper routines
 145 // Store an oop (or NULL) at the address described by obj.
 146 // If val == noreg this means store a NULL
 147 
 148 
 149 static void do_oop_store(InterpreterMacroAssembler* _masm,
 150                          Address obj,
 151                          Register val,
 152                          BarrierSet::Name barrier,
 153                          bool precise) {
 154   assert(val == noreg || val == rax, "parameter is just for looks");
 155   switch (barrier) {
 156 #if INCLUDE_ALL_GCS
 157     case BarrierSet::G1SATBCTLogging:
 158       {
 159         // flatten object address if needed
 160         // We do it regardless of precise because we need the registers
 161         if (obj.index() == noreg && obj.disp() == 0) {
 162           if (obj.base() != rdx) {
 163             __ movptr(rdx, obj.base());
 164           }
 165         } else {
 166           __ lea(rdx, obj);
 167         }
 168 
 169         Register rtmp    = LP64_ONLY(r8)         NOT_LP64(rsi);
 170         Register rthread = LP64_ONLY(r15_thread) NOT_LP64(rcx);
 171 
 172         NOT_LP64(__ get_thread(rcx));
 173         NOT_LP64(__ save_bcp());
 174 
 175         __ g1_write_barrier_pre(rdx /* obj */,
 176                                 rbx /* pre_val */,
 177                                 rthread /* thread */,
 178                                 rtmp  /* tmp */,
 179                                 val != noreg /* tosca_live */,
 180                                 false /* expand_call */);
 181         if (val == noreg) {
 182           __ store_heap_oop_null(Address(rdx, 0));
 183         } else {
 184           // G1 barrier needs uncompressed oop for region cross check.
 185           Register new_val = val;
 186           if (UseCompressedOops) {
 187             new_val = rbx;
 188             __ movptr(new_val, val);
 189           }
 190           __ store_heap_oop(Address(rdx, 0), val);
 191           __ g1_write_barrier_post(rdx /* store_adr */,
 192                                    new_val /* new_val */,
 193                                    rthread /* thread */,
 194                                    rtmp /* tmp */,
 195                                    rbx /* tmp2 */);
 196         }
 197         NOT_LP64( __ restore_bcp());
 198       }
 199       break;
 200 #endif // INCLUDE_ALL_GCS
 201     case BarrierSet::CardTableForRS:
 202     case BarrierSet::CardTableExtension:
 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 #ifdef _LP64
2696   if (SafepointMechanism::uses_thread_local_poll() && _desc->bytecode() != Bytecodes::_return_register_finalizer) {
2697     Label no_safepoint;
2698     NOT_PRODUCT(__ block_comment("Thread-local Safepoint poll"));
2699     __ testb(Address(r15_thread, Thread::polling_page_offset()), SafepointMechanism::poll_bit());
2700     __ jcc(Assembler::zero, no_safepoint);
2701     __ push(state);
2702     __ call_VM(noreg, CAST_FROM_FN_PTR(address,
2703                                     InterpreterRuntime::at_safepoint));
2704     __ pop(state);
2705     __ bind(no_safepoint);
2706   }
2707 #endif
2708 
2709   // Narrow result if state is itos but result type is smaller.
2710   // Need to narrow in the return bytecode rather than in generate_return_entry
2711   // since compiled code callers expect the result to already be narrowed.
2712   if (state == itos) {
2713     __ narrow(rax);
2714   }
2715   __ remove_activation(state, rbcp);
2716 
2717   __ jmp(rbcp);
2718 }
2719 
2720 // ----------------------------------------------------------------------------
2721 // Volatile variables demand their effects be made known to all CPU's
2722 // in order.  Store buffers on most chips allow reads & writes to
2723 // reorder; the JMM's ReadAfterWrite.java test fails in -Xint mode
2724 // without some kind of memory barrier (i.e., it's not sufficient that
2725 // the interpreter does not reorder volatile references, the hardware
2726 // also must not reorder them).
2727 //
2728 // According to the new Java Memory Model (JMM):
2729 // (1) All volatiles are serialized wrt to each other.  ALSO reads &
2730 //     writes act as aquire & release, so:
2731 // (2) A read cannot let unrelated NON-volatile memory refs that
2732 //     happen after the read float up to before the read.  It's OK for
2733 //     non-volatile memory refs that happen before the volatile read to
2734 //     float down below it.
2735 // (3) Similar a volatile write cannot let unrelated NON-volatile
2736 //     memory refs that happen BEFORE the write float down to after the
2737 //     write.  It's OK for non-volatile memory refs that happen after the
2738 //     volatile write to float up before it.
2739 //
2740 // We only put in barriers around volatile refs (they are expensive),
2741 // not _between_ memory refs (that would require us to track the
2742 // flavor of the previous memory refs).  Requirements (2) and (3)
2743 // require some barriers before volatile stores and after volatile
2744 // loads.  These nearly cover requirement (1) but miss the
2745 // volatile-store-volatile-load case.  This final case is placed after
2746 // volatile-stores although it could just as well go before
2747 // volatile-loads.
2748 
2749 void TemplateTable::volatile_barrier(Assembler::Membar_mask_bits order_constraint ) {
2750   // Helper function to insert a is-volatile test and memory barrier
2751   if(!os::is_MP()) return;    // Not needed on single CPU
2752   __ membar(order_constraint);
2753 }
2754 
2755 void TemplateTable::resolve_cache_and_index(int byte_no,
2756                                             Register Rcache,
2757                                             Register index,
2758                                             size_t index_size) {
2759   const Register temp = rbx;
2760   assert_different_registers(Rcache, index, temp);
2761 
2762   Label resolved;
2763 
2764   Bytecodes::Code code = bytecode();
2765   switch (code) {
2766   case Bytecodes::_nofast_getfield: code = Bytecodes::_getfield; break;
2767   case Bytecodes::_nofast_putfield: code = Bytecodes::_putfield; break;
2768   default: break;
2769   }
2770 
2771   assert(byte_no == f1_byte || byte_no == f2_byte, "byte_no out of range");
2772   __ get_cache_and_index_and_bytecode_at_bcp(Rcache, index, temp, byte_no, 1, index_size);
2773   __ cmpl(temp, code);  // have we resolved this bytecode?
2774   __ jcc(Assembler::equal, resolved);
2775 
2776   // resolve first time through
2777   address entry = CAST_FROM_FN_PTR(address, InterpreterRuntime::resolve_from_cache);
2778   __ movl(temp, code);
2779   __ call_VM(noreg, entry, temp);
2780   // Update registers with resolved info
2781   __ get_cache_and_index_at_bcp(Rcache, index, 1, index_size);
2782   __ bind(resolved);
2783 }
2784 
2785 // The cache and index registers must be set before call
2786 void TemplateTable::load_field_cp_cache_entry(Register obj,
2787                                               Register cache,
2788                                               Register index,
2789                                               Register off,
2790                                               Register flags,
2791                                               bool is_static = false) {
2792   assert_different_registers(cache, index, flags, off);
2793 
2794   ByteSize cp_base_offset = ConstantPoolCache::base_offset();
2795   // Field offset
2796   __ movptr(off, Address(cache, index, Address::times_ptr,
2797                          in_bytes(cp_base_offset +
2798                                   ConstantPoolCacheEntry::f2_offset())));
2799   // Flags
2800   __ movl(flags, Address(cache, index, Address::times_ptr,
2801                          in_bytes(cp_base_offset +
2802                                   ConstantPoolCacheEntry::flags_offset())));
2803 
2804   // klass overwrite register
2805   if (is_static) {
2806     __ movptr(obj, Address(cache, index, Address::times_ptr,
2807                            in_bytes(cp_base_offset +
2808                                     ConstantPoolCacheEntry::f1_offset())));
2809     const int mirror_offset = in_bytes(Klass::java_mirror_offset());
2810     __ movptr(obj, Address(obj, mirror_offset));
2811     __ resolve_oop_handle(obj);
2812   }
2813 }
2814 
2815 void TemplateTable::load_invoke_cp_cache_entry(int byte_no,
2816                                                Register method,
2817                                                Register itable_index,
2818                                                Register flags,
2819                                                bool is_invokevirtual,
2820                                                bool is_invokevfinal, /*unused*/
2821                                                bool is_invokedynamic) {
2822   // setup registers
2823   const Register cache = rcx;
2824   const Register index = rdx;
2825   assert_different_registers(method, flags);
2826   assert_different_registers(method, cache, index);
2827   assert_different_registers(itable_index, flags);
2828   assert_different_registers(itable_index, cache, index);
2829   // determine constant pool cache field offsets
2830   assert(is_invokevirtual == (byte_no == f2_byte), "is_invokevirtual flag redundant");
2831   const int method_offset = in_bytes(
2832     ConstantPoolCache::base_offset() +
2833       ((byte_no == f2_byte)
2834        ? ConstantPoolCacheEntry::f2_offset()
2835        : ConstantPoolCacheEntry::f1_offset()));
2836   const int flags_offset = in_bytes(ConstantPoolCache::base_offset() +
2837                                     ConstantPoolCacheEntry::flags_offset());
2838   // access constant pool cache fields
2839   const int index_offset = in_bytes(ConstantPoolCache::base_offset() +
2840                                     ConstantPoolCacheEntry::f2_offset());
2841 
2842   size_t index_size = (is_invokedynamic ? sizeof(u4) : sizeof(u2));
2843   resolve_cache_and_index(byte_no, cache, index, index_size);
2844     __ movptr(method, Address(cache, index, Address::times_ptr, method_offset));
2845 
2846   if (itable_index != noreg) {
2847     // pick up itable or appendix index from f2 also:
2848     __ movptr(itable_index, Address(cache, index, Address::times_ptr, index_offset));
2849   }
2850   __ movl(flags, Address(cache, index, Address::times_ptr, flags_offset));
2851 }
2852 
2853 // The registers cache and index expected to be set before call.
2854 // Correct values of the cache and index registers are preserved.
2855 void TemplateTable::jvmti_post_field_access(Register cache,
2856                                             Register index,
2857                                             bool is_static,
2858                                             bool has_tos) {
2859   if (JvmtiExport::can_post_field_access()) {
2860     // Check to see if a field access watch has been set before we take
2861     // the time to call into the VM.
2862     Label L1;
2863     assert_different_registers(cache, index, rax);
2864     __ mov32(rax, ExternalAddress((address) JvmtiExport::get_field_access_count_addr()));
2865     __ testl(rax,rax);
2866     __ jcc(Assembler::zero, L1);
2867 
2868     // cache entry pointer
2869     __ addptr(cache, in_bytes(ConstantPoolCache::base_offset()));
2870     __ shll(index, LogBytesPerWord);
2871     __ addptr(cache, index);
2872     if (is_static) {
2873       __ xorptr(rax, rax);      // NULL object reference
2874     } else {
2875       __ pop(atos);         // Get the object
2876       __ verify_oop(rax);
2877       __ push(atos);        // Restore stack state
2878     }
2879     // rax,:   object pointer or NULL
2880     // cache: cache entry pointer
2881     __ call_VM(noreg, CAST_FROM_FN_PTR(address, InterpreterRuntime::post_field_access),
2882                rax, cache);
2883     __ get_cache_and_index_at_bcp(cache, index, 1);
2884     __ bind(L1);
2885   }
2886 }
2887 
2888 void TemplateTable::pop_and_check_object(Register r) {
2889   __ pop_ptr(r);
2890   __ null_check(r);  // for field access must check obj.
2891   __ verify_oop(r);
2892 }
2893 
2894 void TemplateTable::getfield_or_static(int byte_no, bool is_static, RewriteControl rc) {
2895   transition(vtos, vtos);
2896 
2897   const Register cache = rcx;
2898   const Register index = rdx;
2899   const Register obj   = LP64_ONLY(c_rarg3) NOT_LP64(rcx);
2900   const Register off   = rbx;
2901   const Register flags = rax;
2902   const Register bc    = LP64_ONLY(c_rarg3) NOT_LP64(rcx); // uses same reg as obj, so don't mix them
2903 
2904   resolve_cache_and_index(byte_no, cache, index, sizeof(u2));
2905   jvmti_post_field_access(cache, index, is_static, false);
2906   load_field_cp_cache_entry(obj, cache, index, off, flags, is_static);
2907 
2908   if (!is_static) pop_and_check_object(obj);
2909 
2910   const Address field(obj, off, Address::times_1, 0*wordSize);
2911   NOT_LP64(const Address hi(obj, off, Address::times_1, 1*wordSize));
2912 
2913   Label Done, notByte, notBool, notInt, notShort, notChar, notLong, notFloat, notObj, notDouble;
2914 
2915   __ shrl(flags, ConstantPoolCacheEntry::tos_state_shift);
2916   // Make sure we don't need to mask edx after the above shift
2917   assert(btos == 0, "change code, btos != 0");
2918 
2919   __ andl(flags, ConstantPoolCacheEntry::tos_state_mask);
2920 
2921   __ jcc(Assembler::notZero, notByte);
2922   // btos
2923   __ load_signed_byte(rax, field);
2924   __ push(btos);
2925   // Rewrite bytecode to be faster
2926   if (!is_static && rc == may_rewrite) {
2927     patch_bytecode(Bytecodes::_fast_bgetfield, bc, rbx);
2928   }
2929   __ jmp(Done);
2930 
2931   __ bind(notByte);
2932   __ cmpl(flags, ztos);
2933   __ jcc(Assembler::notEqual, notBool);
2934 
2935   // ztos (same code as btos)
2936   __ load_signed_byte(rax, field);
2937   __ push(ztos);
2938   // Rewrite bytecode to be faster
2939   if (!is_static && rc == may_rewrite) {
2940     // use btos rewriting, no truncating to t/f bit is needed for getfield.
2941     patch_bytecode(Bytecodes::_fast_bgetfield, bc, rbx);
2942   }
2943   __ jmp(Done);
2944 
2945   __ bind(notBool);
2946   __ cmpl(flags, atos);
2947   __ jcc(Assembler::notEqual, notObj);
2948   // atos
2949   __ load_heap_oop(rax, field);
2950   __ push(atos);
2951   if (!is_static && rc == may_rewrite) {
2952     patch_bytecode(Bytecodes::_fast_agetfield, bc, rbx);
2953   }
2954   __ jmp(Done);
2955 
2956   __ bind(notObj);
2957   __ cmpl(flags, itos);
2958   __ jcc(Assembler::notEqual, notInt);
2959   // itos
2960   __ movl(rax, field);
2961   __ push(itos);
2962   // Rewrite bytecode to be faster
2963   if (!is_static && rc == may_rewrite) {
2964     patch_bytecode(Bytecodes::_fast_igetfield, bc, rbx);
2965   }
2966   __ jmp(Done);
2967 
2968   __ bind(notInt);
2969   __ cmpl(flags, ctos);
2970   __ jcc(Assembler::notEqual, notChar);
2971   // ctos
2972   __ load_unsigned_short(rax, field);
2973   __ push(ctos);
2974   // Rewrite bytecode to be faster
2975   if (!is_static && rc == may_rewrite) {
2976     patch_bytecode(Bytecodes::_fast_cgetfield, bc, rbx);
2977   }
2978   __ jmp(Done);
2979 
2980   __ bind(notChar);
2981   __ cmpl(flags, stos);
2982   __ jcc(Assembler::notEqual, notShort);
2983   // stos
2984   __ load_signed_short(rax, field);
2985   __ push(stos);
2986   // Rewrite bytecode to be faster
2987   if (!is_static && rc == may_rewrite) {
2988     patch_bytecode(Bytecodes::_fast_sgetfield, bc, rbx);
2989   }
2990   __ jmp(Done);
2991 
2992   __ bind(notShort);
2993   __ cmpl(flags, ltos);
2994   __ jcc(Assembler::notEqual, notLong);
2995   // ltos
2996 
2997 #ifndef _LP64
2998   // Generate code as if volatile.  There just aren't enough registers to
2999   // save that information and this code is faster than the test.
3000   __ fild_d(field);                // Must load atomically
3001   __ subptr(rsp,2*wordSize);    // Make space for store
3002   __ fistp_d(Address(rsp,0));
3003   __ pop(rax);
3004   __ pop(rdx);
3005 #else
3006   __ movq(rax, field);
3007 #endif
3008 
3009   __ push(ltos);
3010   // Rewrite bytecode to be faster
3011   LP64_ONLY(if (!is_static && rc == may_rewrite) patch_bytecode(Bytecodes::_fast_lgetfield, bc, rbx));
3012   __ jmp(Done);
3013 
3014   __ bind(notLong);
3015   __ cmpl(flags, ftos);
3016   __ jcc(Assembler::notEqual, notFloat);
3017   // ftos
3018 
3019   __ load_float(field);
3020   __ push(ftos);
3021   // Rewrite bytecode to be faster
3022   if (!is_static && rc == may_rewrite) {
3023     patch_bytecode(Bytecodes::_fast_fgetfield, bc, rbx);
3024   }
3025   __ jmp(Done);
3026 
3027   __ bind(notFloat);
3028 #ifdef ASSERT
3029   __ cmpl(flags, dtos);
3030   __ jcc(Assembler::notEqual, notDouble);
3031 #endif
3032   // dtos
3033   __ load_double(field);
3034   __ push(dtos);
3035   // Rewrite bytecode to be faster
3036   if (!is_static && rc == may_rewrite) {
3037     patch_bytecode(Bytecodes::_fast_dgetfield, bc, rbx);
3038   }
3039 #ifdef ASSERT
3040   __ jmp(Done);
3041 
3042 
3043   __ bind(notDouble);
3044   __ stop("Bad state");
3045 #endif
3046 
3047   __ bind(Done);
3048   // [jk] not needed currently
3049   // volatile_barrier(Assembler::Membar_mask_bits(Assembler::LoadLoad |
3050   //                                              Assembler::LoadStore));
3051 }
3052 
3053 void TemplateTable::getfield(int byte_no) {
3054   getfield_or_static(byte_no, false);
3055 }
3056 
3057 void TemplateTable::nofast_getfield(int byte_no) {
3058   getfield_or_static(byte_no, false, may_not_rewrite);
3059 }
3060 
3061 void TemplateTable::getstatic(int byte_no) {
3062   getfield_or_static(byte_no, true);
3063 }
3064 
3065 
3066 // The registers cache and index expected to be set before call.
3067 // The function may destroy various registers, just not the cache and index registers.
3068 void TemplateTable::jvmti_post_field_mod(Register cache, Register index, bool is_static) {
3069 
3070   const Register robj = LP64_ONLY(c_rarg2)   NOT_LP64(rax);
3071   const Register RBX  = LP64_ONLY(c_rarg1)   NOT_LP64(rbx);
3072   const Register RCX  = LP64_ONLY(c_rarg3)   NOT_LP64(rcx);
3073   const Register RDX  = LP64_ONLY(rscratch1) NOT_LP64(rdx);
3074 
3075   ByteSize cp_base_offset = ConstantPoolCache::base_offset();
3076 
3077   if (JvmtiExport::can_post_field_modification()) {
3078     // Check to see if a field modification watch has been set before
3079     // we take the time to call into the VM.
3080     Label L1;
3081     assert_different_registers(cache, index, rax);
3082     __ mov32(rax, ExternalAddress((address)JvmtiExport::get_field_modification_count_addr()));
3083     __ testl(rax, rax);
3084     __ jcc(Assembler::zero, L1);
3085 
3086     __ get_cache_and_index_at_bcp(robj, RDX, 1);
3087 
3088 
3089     if (is_static) {
3090       // Life is simple.  Null out the object pointer.
3091       __ xorl(RBX, RBX);
3092 
3093     } else {
3094       // Life is harder. The stack holds the value on top, followed by
3095       // the object.  We don't know the size of the value, though; it
3096       // could be one or two words depending on its type. As a result,
3097       // we must find the type to determine where the object is.
3098 #ifndef _LP64
3099       Label two_word, valsize_known;
3100 #endif
3101       __ movl(RCX, Address(robj, RDX,
3102                            Address::times_ptr,
3103                            in_bytes(cp_base_offset +
3104                                      ConstantPoolCacheEntry::flags_offset())));
3105       NOT_LP64(__ mov(rbx, rsp));
3106       __ shrl(RCX, ConstantPoolCacheEntry::tos_state_shift);
3107 
3108       // Make sure we don't need to mask rcx after the above shift
3109       ConstantPoolCacheEntry::verify_tos_state_shift();
3110 #ifdef _LP64
3111       __ movptr(c_rarg1, at_tos_p1());  // initially assume a one word jvalue
3112       __ cmpl(c_rarg3, ltos);
3113       __ cmovptr(Assembler::equal,
3114                  c_rarg1, at_tos_p2()); // ltos (two word jvalue)
3115       __ cmpl(c_rarg3, dtos);
3116       __ cmovptr(Assembler::equal,
3117                  c_rarg1, at_tos_p2()); // dtos (two word jvalue)
3118 #else
3119       __ cmpl(rcx, ltos);
3120       __ jccb(Assembler::equal, two_word);
3121       __ cmpl(rcx, dtos);
3122       __ jccb(Assembler::equal, two_word);
3123       __ addptr(rbx, Interpreter::expr_offset_in_bytes(1)); // one word jvalue (not ltos, dtos)
3124       __ jmpb(valsize_known);
3125 
3126       __ bind(two_word);
3127       __ addptr(rbx, Interpreter::expr_offset_in_bytes(2)); // two words jvalue
3128 
3129       __ bind(valsize_known);
3130       // setup object pointer
3131       __ movptr(rbx, Address(rbx, 0));
3132 #endif
3133     }
3134     // cache entry pointer
3135     __ addptr(robj, in_bytes(cp_base_offset));
3136     __ shll(RDX, LogBytesPerWord);
3137     __ addptr(robj, RDX);
3138     // object (tos)
3139     __ mov(RCX, rsp);
3140     // c_rarg1: object pointer set up above (NULL if static)
3141     // c_rarg2: cache entry pointer
3142     // c_rarg3: jvalue object on the stack
3143     __ call_VM(noreg,
3144                CAST_FROM_FN_PTR(address,
3145                                 InterpreterRuntime::post_field_modification),
3146                RBX, robj, RCX);
3147     __ get_cache_and_index_at_bcp(cache, index, 1);
3148     __ bind(L1);
3149   }
3150 }
3151 
3152 void TemplateTable::putfield_or_static(int byte_no, bool is_static, RewriteControl rc) {
3153   transition(vtos, vtos);
3154 
3155   const Register cache = rcx;
3156   const Register index = rdx;
3157   const Register obj   = rcx;
3158   const Register off   = rbx;
3159   const Register flags = rax;
3160   const Register bc    = LP64_ONLY(c_rarg3) NOT_LP64(rcx);
3161 
3162   resolve_cache_and_index(byte_no, cache, index, sizeof(u2));
3163   jvmti_post_field_mod(cache, index, is_static);
3164   load_field_cp_cache_entry(obj, cache, index, off, flags, is_static);
3165 
3166   // [jk] not needed currently
3167   // volatile_barrier(Assembler::Membar_mask_bits(Assembler::LoadStore |
3168   //                                              Assembler::StoreStore));
3169 
3170   Label notVolatile, Done;
3171   __ movl(rdx, flags);
3172   __ shrl(rdx, ConstantPoolCacheEntry::is_volatile_shift);
3173   __ andl(rdx, 0x1);
3174 
3175   // field addresses
3176   const Address field(obj, off, Address::times_1, 0*wordSize);
3177   NOT_LP64( const Address hi(obj, off, Address::times_1, 1*wordSize);)
3178 
3179   Label notByte, notBool, notInt, notShort, notChar,
3180         notLong, notFloat, notObj, notDouble;
3181 
3182   __ shrl(flags, ConstantPoolCacheEntry::tos_state_shift);
3183 
3184   assert(btos == 0, "change code, btos != 0");
3185   __ andl(flags, ConstantPoolCacheEntry::tos_state_mask);
3186   __ jcc(Assembler::notZero, notByte);
3187 
3188   // btos
3189   {
3190     __ pop(btos);
3191     if (!is_static) pop_and_check_object(obj);
3192     __ movb(field, rax);
3193     if (!is_static && rc == may_rewrite) {
3194       patch_bytecode(Bytecodes::_fast_bputfield, bc, rbx, true, byte_no);
3195     }
3196     __ jmp(Done);
3197   }
3198 
3199   __ bind(notByte);
3200   __ cmpl(flags, ztos);
3201   __ jcc(Assembler::notEqual, notBool);
3202 
3203   // ztos
3204   {
3205     __ pop(ztos);
3206     if (!is_static) pop_and_check_object(obj);
3207     __ andl(rax, 0x1);
3208     __ movb(field, rax);
3209     if (!is_static && rc == may_rewrite) {
3210       patch_bytecode(Bytecodes::_fast_zputfield, bc, rbx, true, byte_no);
3211     }
3212     __ jmp(Done);
3213   }
3214 
3215   __ bind(notBool);
3216   __ cmpl(flags, atos);
3217   __ jcc(Assembler::notEqual, notObj);
3218 
3219   // atos
3220   {
3221     __ pop(atos);
3222     if (!is_static) pop_and_check_object(obj);
3223     // Store into the field
3224     do_oop_store(_masm, field, rax, _bs->kind(), false);
3225     if (!is_static && rc == may_rewrite) {
3226       patch_bytecode(Bytecodes::_fast_aputfield, bc, rbx, true, byte_no);
3227     }
3228     __ jmp(Done);
3229   }
3230 
3231   __ bind(notObj);
3232   __ cmpl(flags, itos);
3233   __ jcc(Assembler::notEqual, notInt);
3234 
3235   // itos
3236   {
3237     __ pop(itos);
3238     if (!is_static) pop_and_check_object(obj);
3239     __ movl(field, rax);
3240     if (!is_static && rc == may_rewrite) {
3241       patch_bytecode(Bytecodes::_fast_iputfield, bc, rbx, true, byte_no);
3242     }
3243     __ jmp(Done);
3244   }
3245 
3246   __ bind(notInt);
3247   __ cmpl(flags, ctos);
3248   __ jcc(Assembler::notEqual, notChar);
3249 
3250   // ctos
3251   {
3252     __ pop(ctos);
3253     if (!is_static) pop_and_check_object(obj);
3254     __ movw(field, rax);
3255     if (!is_static && rc == may_rewrite) {
3256       patch_bytecode(Bytecodes::_fast_cputfield, bc, rbx, true, byte_no);
3257     }
3258     __ jmp(Done);
3259   }
3260 
3261   __ bind(notChar);
3262   __ cmpl(flags, stos);
3263   __ jcc(Assembler::notEqual, notShort);
3264 
3265   // stos
3266   {
3267     __ pop(stos);
3268     if (!is_static) pop_and_check_object(obj);
3269     __ movw(field, rax);
3270     if (!is_static && rc == may_rewrite) {
3271       patch_bytecode(Bytecodes::_fast_sputfield, bc, rbx, true, byte_no);
3272     }
3273     __ jmp(Done);
3274   }
3275 
3276   __ bind(notShort);
3277   __ cmpl(flags, ltos);
3278   __ jcc(Assembler::notEqual, notLong);
3279 
3280   // ltos
3281 #ifdef _LP64
3282   {
3283     __ pop(ltos);
3284     if (!is_static) pop_and_check_object(obj);
3285     __ movq(field, rax);
3286     if (!is_static && rc == may_rewrite) {
3287       patch_bytecode(Bytecodes::_fast_lputfield, bc, rbx, true, byte_no);
3288     }
3289     __ jmp(Done);
3290   }
3291 #else
3292   {
3293     Label notVolatileLong;
3294     __ testl(rdx, rdx);
3295     __ jcc(Assembler::zero, notVolatileLong);
3296 
3297     __ pop(ltos);  // overwrites rdx, do this after testing volatile.
3298     if (!is_static) pop_and_check_object(obj);
3299 
3300     // Replace with real volatile test
3301     __ push(rdx);
3302     __ push(rax);                 // Must update atomically with FIST
3303     __ fild_d(Address(rsp,0));    // So load into FPU register
3304     __ fistp_d(field);            // and put into memory atomically
3305     __ addptr(rsp, 2*wordSize);
3306     // volatile_barrier();
3307     volatile_barrier(Assembler::Membar_mask_bits(Assembler::StoreLoad |
3308                                                  Assembler::StoreStore));
3309     // Don't rewrite volatile version
3310     __ jmp(notVolatile);
3311 
3312     __ bind(notVolatileLong);
3313 
3314     __ pop(ltos);  // overwrites rdx
3315     if (!is_static) pop_and_check_object(obj);
3316     __ movptr(hi, rdx);
3317     __ movptr(field, rax);
3318     // Don't rewrite to _fast_lputfield for potential volatile case.
3319     __ jmp(notVolatile);
3320   }
3321 #endif // _LP64
3322 
3323   __ bind(notLong);
3324   __ cmpl(flags, ftos);
3325   __ jcc(Assembler::notEqual, notFloat);
3326 
3327   // ftos
3328   {
3329     __ pop(ftos);
3330     if (!is_static) pop_and_check_object(obj);
3331     __ store_float(field);
3332     if (!is_static && rc == may_rewrite) {
3333       patch_bytecode(Bytecodes::_fast_fputfield, bc, rbx, true, byte_no);
3334     }
3335     __ jmp(Done);
3336   }
3337 
3338   __ bind(notFloat);
3339 #ifdef ASSERT
3340   __ cmpl(flags, dtos);
3341   __ jcc(Assembler::notEqual, notDouble);
3342 #endif
3343 
3344   // dtos
3345   {
3346     __ pop(dtos);
3347     if (!is_static) pop_and_check_object(obj);
3348     __ store_double(field);
3349     if (!is_static && rc == may_rewrite) {
3350       patch_bytecode(Bytecodes::_fast_dputfield, bc, rbx, true, byte_no);
3351     }
3352   }
3353 
3354 #ifdef ASSERT
3355   __ jmp(Done);
3356 
3357   __ bind(notDouble);
3358   __ stop("Bad state");
3359 #endif
3360 
3361   __ bind(Done);
3362 
3363   // Check for volatile store
3364   __ testl(rdx, rdx);
3365   __ jcc(Assembler::zero, notVolatile);
3366   volatile_barrier(Assembler::Membar_mask_bits(Assembler::StoreLoad |
3367                                                Assembler::StoreStore));
3368   __ bind(notVolatile);
3369 }
3370 
3371 void TemplateTable::putfield(int byte_no) {
3372   putfield_or_static(byte_no, false);
3373 }
3374 
3375 void TemplateTable::nofast_putfield(int byte_no) {
3376   putfield_or_static(byte_no, false, may_not_rewrite);
3377 }
3378 
3379 void TemplateTable::putstatic(int byte_no) {
3380   putfield_or_static(byte_no, true);
3381 }
3382 
3383 void TemplateTable::jvmti_post_fast_field_mod() {
3384 
3385   const Register scratch = LP64_ONLY(c_rarg3) NOT_LP64(rcx);
3386 
3387   if (JvmtiExport::can_post_field_modification()) {
3388     // Check to see if a field modification watch has been set before
3389     // we take the time to call into the VM.
3390     Label L2;
3391     __ mov32(scratch, ExternalAddress((address)JvmtiExport::get_field_modification_count_addr()));
3392     __ testl(scratch, scratch);
3393     __ jcc(Assembler::zero, L2);
3394     __ pop_ptr(rbx);                  // copy the object pointer from tos
3395     __ verify_oop(rbx);
3396     __ push_ptr(rbx);                 // put the object pointer back on tos
3397     // Save tos values before call_VM() clobbers them. Since we have
3398     // to do it for every data type, we use the saved values as the
3399     // jvalue object.
3400     switch (bytecode()) {          // load values into the jvalue object
3401     case Bytecodes::_fast_aputfield: __ push_ptr(rax); break;
3402     case Bytecodes::_fast_bputfield: // fall through
3403     case Bytecodes::_fast_zputfield: // fall through
3404     case Bytecodes::_fast_sputfield: // fall through
3405     case Bytecodes::_fast_cputfield: // fall through
3406     case Bytecodes::_fast_iputfield: __ push_i(rax); break;
3407     case Bytecodes::_fast_dputfield: __ push(dtos); break;
3408     case Bytecodes::_fast_fputfield: __ push(ftos); break;
3409     case Bytecodes::_fast_lputfield: __ push_l(rax); break;
3410 
3411     default:
3412       ShouldNotReachHere();
3413     }
3414     __ mov(scratch, rsp);             // points to jvalue on the stack
3415     // access constant pool cache entry
3416     LP64_ONLY(__ get_cache_entry_pointer_at_bcp(c_rarg2, rax, 1));
3417     NOT_LP64(__ get_cache_entry_pointer_at_bcp(rax, rdx, 1));
3418     __ verify_oop(rbx);
3419     // rbx: object pointer copied above
3420     // c_rarg2: cache entry pointer
3421     // c_rarg3: jvalue object on the stack
3422     LP64_ONLY(__ call_VM(noreg, CAST_FROM_FN_PTR(address, InterpreterRuntime::post_field_modification), rbx, c_rarg2, c_rarg3));
3423     NOT_LP64(__ call_VM(noreg, CAST_FROM_FN_PTR(address, InterpreterRuntime::post_field_modification), rbx, rax, rcx));
3424 
3425     switch (bytecode()) {             // restore tos values
3426     case Bytecodes::_fast_aputfield: __ pop_ptr(rax); break;
3427     case Bytecodes::_fast_bputfield: // fall through
3428     case Bytecodes::_fast_zputfield: // fall through
3429     case Bytecodes::_fast_sputfield: // fall through
3430     case Bytecodes::_fast_cputfield: // fall through
3431     case Bytecodes::_fast_iputfield: __ pop_i(rax); break;
3432     case Bytecodes::_fast_dputfield: __ pop(dtos); break;
3433     case Bytecodes::_fast_fputfield: __ pop(ftos); break;
3434     case Bytecodes::_fast_lputfield: __ pop_l(rax); break;
3435     default: break;
3436     }
3437     __ bind(L2);
3438   }
3439 }
3440 
3441 void TemplateTable::fast_storefield(TosState state) {
3442   transition(state, vtos);
3443 
3444   ByteSize base = ConstantPoolCache::base_offset();
3445 
3446   jvmti_post_fast_field_mod();
3447 
3448   // access constant pool cache
3449   __ get_cache_and_index_at_bcp(rcx, rbx, 1);
3450 
3451   // test for volatile with rdx but rdx is tos register for lputfield.
3452   __ movl(rdx, Address(rcx, rbx, Address::times_ptr,
3453                        in_bytes(base +
3454                                 ConstantPoolCacheEntry::flags_offset())));
3455 
3456   // replace index with field offset from cache entry
3457   __ movptr(rbx, Address(rcx, rbx, Address::times_ptr,
3458                          in_bytes(base + ConstantPoolCacheEntry::f2_offset())));
3459 
3460   // [jk] not needed currently
3461   // volatile_barrier(Assembler::Membar_mask_bits(Assembler::LoadStore |
3462   //                                              Assembler::StoreStore));
3463 
3464   Label notVolatile;
3465   __ shrl(rdx, ConstantPoolCacheEntry::is_volatile_shift);
3466   __ andl(rdx, 0x1);
3467 
3468   // Get object from stack
3469   pop_and_check_object(rcx);
3470 
3471   // field address
3472   const Address field(rcx, rbx, Address::times_1);
3473 
3474   // access field
3475   switch (bytecode()) {
3476   case Bytecodes::_fast_aputfield:
3477     do_oop_store(_masm, field, rax, _bs->kind(), false);
3478     break;
3479   case Bytecodes::_fast_lputfield:
3480 #ifdef _LP64
3481   __ movq(field, rax);
3482 #else
3483   __ stop("should not be rewritten");
3484 #endif
3485     break;
3486   case Bytecodes::_fast_iputfield:
3487     __ movl(field, rax);
3488     break;
3489   case Bytecodes::_fast_zputfield:
3490     __ andl(rax, 0x1);  // boolean is true if LSB is 1
3491     // fall through to bputfield
3492   case Bytecodes::_fast_bputfield:
3493     __ movb(field, rax);
3494     break;
3495   case Bytecodes::_fast_sputfield:
3496     // fall through
3497   case Bytecodes::_fast_cputfield:
3498     __ movw(field, rax);
3499     break;
3500   case Bytecodes::_fast_fputfield:
3501     __ store_float(field);
3502     break;
3503   case Bytecodes::_fast_dputfield:
3504     __ store_double(field);
3505     break;
3506   default:
3507     ShouldNotReachHere();
3508   }
3509 
3510   // Check for volatile store
3511   __ testl(rdx, rdx);
3512   __ jcc(Assembler::zero, notVolatile);
3513   volatile_barrier(Assembler::Membar_mask_bits(Assembler::StoreLoad |
3514                                                Assembler::StoreStore));
3515   __ bind(notVolatile);
3516 }
3517 
3518 void TemplateTable::fast_accessfield(TosState state) {
3519   transition(atos, state);
3520 
3521   // Do the JVMTI work here to avoid disturbing the register state below
3522   if (JvmtiExport::can_post_field_access()) {
3523     // Check to see if a field access watch has been set before we
3524     // take the time to call into the VM.
3525     Label L1;
3526     __ mov32(rcx, ExternalAddress((address) JvmtiExport::get_field_access_count_addr()));
3527     __ testl(rcx, rcx);
3528     __ jcc(Assembler::zero, L1);
3529     // access constant pool cache entry
3530     LP64_ONLY(__ get_cache_entry_pointer_at_bcp(c_rarg2, rcx, 1));
3531     NOT_LP64(__ get_cache_entry_pointer_at_bcp(rcx, rdx, 1));
3532     __ verify_oop(rax);
3533     __ push_ptr(rax);  // save object pointer before call_VM() clobbers it
3534     LP64_ONLY(__ mov(c_rarg1, rax));
3535     // c_rarg1: object pointer copied above
3536     // c_rarg2: cache entry pointer
3537     LP64_ONLY(__ call_VM(noreg, CAST_FROM_FN_PTR(address, InterpreterRuntime::post_field_access), c_rarg1, c_rarg2));
3538     NOT_LP64(__ call_VM(noreg, CAST_FROM_FN_PTR(address, InterpreterRuntime::post_field_access), rax, rcx));
3539     __ pop_ptr(rax); // restore object pointer
3540     __ bind(L1);
3541   }
3542 
3543   // access constant pool cache
3544   __ get_cache_and_index_at_bcp(rcx, rbx, 1);
3545   // replace index with field offset from cache entry
3546   // [jk] not needed currently
3547   // if (os::is_MP()) {
3548   //   __ movl(rdx, Address(rcx, rbx, Address::times_8,
3549   //                        in_bytes(ConstantPoolCache::base_offset() +
3550   //                                 ConstantPoolCacheEntry::flags_offset())));
3551   //   __ shrl(rdx, ConstantPoolCacheEntry::is_volatile_shift);
3552   //   __ andl(rdx, 0x1);
3553   // }
3554   __ movptr(rbx, Address(rcx, rbx, Address::times_ptr,
3555                          in_bytes(ConstantPoolCache::base_offset() +
3556                                   ConstantPoolCacheEntry::f2_offset())));
3557 
3558   // rax: object
3559   __ verify_oop(rax);
3560   __ null_check(rax);
3561   Address field(rax, rbx, Address::times_1);
3562 
3563   // access field
3564   switch (bytecode()) {
3565   case Bytecodes::_fast_agetfield:
3566     __ load_heap_oop(rax, field);
3567     __ verify_oop(rax);
3568     break;
3569   case Bytecodes::_fast_lgetfield:
3570 #ifdef _LP64
3571   __ movq(rax, field);
3572 #else
3573   __ stop("should not be rewritten");
3574 #endif
3575     break;
3576   case Bytecodes::_fast_igetfield:
3577     __ movl(rax, field);
3578     break;
3579   case Bytecodes::_fast_bgetfield:
3580     __ movsbl(rax, field);
3581     break;
3582   case Bytecodes::_fast_sgetfield:
3583     __ load_signed_short(rax, field);
3584     break;
3585   case Bytecodes::_fast_cgetfield:
3586     __ load_unsigned_short(rax, field);
3587     break;
3588   case Bytecodes::_fast_fgetfield:
3589     __ load_float(field);
3590     break;
3591   case Bytecodes::_fast_dgetfield:
3592     __ load_double(field);
3593     break;
3594   default:
3595     ShouldNotReachHere();
3596   }
3597   // [jk] not needed currently
3598   // if (os::is_MP()) {
3599   //   Label notVolatile;
3600   //   __ testl(rdx, rdx);
3601   //   __ jcc(Assembler::zero, notVolatile);
3602   //   __ membar(Assembler::LoadLoad);
3603   //   __ bind(notVolatile);
3604   //};
3605 }
3606 
3607 void TemplateTable::fast_xaccess(TosState state) {
3608   transition(vtos, state);
3609 
3610   // get receiver
3611   __ movptr(rax, aaddress(0));
3612   // access constant pool cache
3613   __ get_cache_and_index_at_bcp(rcx, rdx, 2);
3614   __ movptr(rbx,
3615             Address(rcx, rdx, Address::times_ptr,
3616                     in_bytes(ConstantPoolCache::base_offset() +
3617                              ConstantPoolCacheEntry::f2_offset())));
3618   // make sure exception is reported in correct bcp range (getfield is
3619   // next instruction)
3620   __ increment(rbcp);
3621   __ null_check(rax);
3622   const Address field = Address(rax, rbx, Address::times_1, 0*wordSize);
3623   switch (state) {
3624   case itos:
3625     __ movl(rax, field);
3626     break;
3627   case atos:
3628     __ load_heap_oop(rax, field);
3629     __ verify_oop(rax);
3630     break;
3631   case ftos:
3632     __ load_float(field);
3633     break;
3634   default:
3635     ShouldNotReachHere();
3636   }
3637 
3638   // [jk] not needed currently
3639   // if (os::is_MP()) {
3640   //   Label notVolatile;
3641   //   __ movl(rdx, Address(rcx, rdx, Address::times_8,
3642   //                        in_bytes(ConstantPoolCache::base_offset() +
3643   //                                 ConstantPoolCacheEntry::flags_offset())));
3644   //   __ shrl(rdx, ConstantPoolCacheEntry::is_volatile_shift);
3645   //   __ testl(rdx, 0x1);
3646   //   __ jcc(Assembler::zero, notVolatile);
3647   //   __ membar(Assembler::LoadLoad);
3648   //   __ bind(notVolatile);
3649   // }
3650 
3651   __ decrement(rbcp);
3652 }
3653 
3654 //-----------------------------------------------------------------------------
3655 // Calls
3656 
3657 void TemplateTable::count_calls(Register method, Register temp) {
3658   // implemented elsewhere
3659   ShouldNotReachHere();
3660 }
3661 
3662 void TemplateTable::prepare_invoke(int byte_no,
3663                                    Register method,  // linked method (or i-klass)
3664                                    Register index,   // itable index, MethodType, etc.
3665                                    Register recv,    // if caller wants to see it
3666                                    Register flags    // if caller wants to test it
3667                                    ) {
3668   // determine flags
3669   const Bytecodes::Code code = bytecode();
3670   const bool is_invokeinterface  = code == Bytecodes::_invokeinterface;
3671   const bool is_invokedynamic    = code == Bytecodes::_invokedynamic;
3672   const bool is_invokehandle     = code == Bytecodes::_invokehandle;
3673   const bool is_invokevirtual    = code == Bytecodes::_invokevirtual;
3674   const bool is_invokespecial    = code == Bytecodes::_invokespecial;
3675   const bool load_receiver       = (recv  != noreg);
3676   const bool save_flags          = (flags != noreg);
3677   assert(load_receiver == (code != Bytecodes::_invokestatic && code != Bytecodes::_invokedynamic), "");
3678   assert(save_flags    == (is_invokeinterface || is_invokevirtual), "need flags for vfinal");
3679   assert(flags == noreg || flags == rdx, "");
3680   assert(recv  == noreg || recv  == rcx, "");
3681 
3682   // setup registers & access constant pool cache
3683   if (recv  == noreg)  recv  = rcx;
3684   if (flags == noreg)  flags = rdx;
3685   assert_different_registers(method, index, recv, flags);
3686 
3687   // save 'interpreter return address'
3688   __ save_bcp();
3689 
3690   load_invoke_cp_cache_entry(byte_no, method, index, flags, is_invokevirtual, false, is_invokedynamic);
3691 
3692   // maybe push appendix to arguments (just before return address)
3693   if (is_invokedynamic || is_invokehandle) {
3694     Label L_no_push;
3695     __ testl(flags, (1 << ConstantPoolCacheEntry::has_appendix_shift));
3696     __ jcc(Assembler::zero, L_no_push);
3697     // Push the appendix as a trailing parameter.
3698     // This must be done before we get the receiver,
3699     // since the parameter_size includes it.
3700     __ push(rbx);
3701     __ mov(rbx, index);
3702     assert(ConstantPoolCacheEntry::_indy_resolved_references_appendix_offset == 0, "appendix expected at index+0");
3703     __ load_resolved_reference_at_index(index, rbx);
3704     __ pop(rbx);
3705     __ push(index);  // push appendix (MethodType, CallSite, etc.)
3706     __ bind(L_no_push);
3707   }
3708 
3709   // load receiver if needed (after appendix is pushed so parameter size is correct)
3710   // Note: no return address pushed yet
3711   if (load_receiver) {
3712     __ movl(recv, flags);
3713     __ andl(recv, ConstantPoolCacheEntry::parameter_size_mask);
3714     const int no_return_pc_pushed_yet = -1;  // argument slot correction before we push return address
3715     const int receiver_is_at_end      = -1;  // back off one slot to get receiver
3716     Address recv_addr = __ argument_address(recv, no_return_pc_pushed_yet + receiver_is_at_end);
3717     __ movptr(recv, recv_addr);
3718     __ verify_oop(recv);
3719   }
3720 
3721   if (save_flags) {
3722     __ movl(rbcp, flags);
3723   }
3724 
3725   // compute return type
3726   __ shrl(flags, ConstantPoolCacheEntry::tos_state_shift);
3727   // Make sure we don't need to mask flags after the above shift
3728   ConstantPoolCacheEntry::verify_tos_state_shift();
3729   // load return address
3730   {
3731     const address table_addr = (address) Interpreter::invoke_return_entry_table_for(code);
3732     ExternalAddress table(table_addr);
3733     LP64_ONLY(__ lea(rscratch1, table));
3734     LP64_ONLY(__ movptr(flags, Address(rscratch1, flags, Address::times_ptr)));
3735     NOT_LP64(__ movptr(flags, ArrayAddress(table, Address(noreg, flags, Address::times_ptr))));
3736   }
3737 
3738   // push return address
3739   __ push(flags);
3740 
3741   // Restore flags value from the constant pool cache, and restore rsi
3742   // for later null checks.  r13 is the bytecode pointer
3743   if (save_flags) {
3744     __ movl(flags, rbcp);
3745     __ restore_bcp();
3746   }
3747 }
3748 
3749 void TemplateTable::invokevirtual_helper(Register index,
3750                                          Register recv,
3751                                          Register flags) {
3752   // Uses temporary registers rax, rdx
3753   assert_different_registers(index, recv, rax, rdx);
3754   assert(index == rbx, "");
3755   assert(recv  == rcx, "");
3756 
3757   // Test for an invoke of a final method
3758   Label notFinal;
3759   __ movl(rax, flags);
3760   __ andl(rax, (1 << ConstantPoolCacheEntry::is_vfinal_shift));
3761   __ jcc(Assembler::zero, notFinal);
3762 
3763   const Register method = index;  // method must be rbx
3764   assert(method == rbx,
3765          "Method* must be rbx for interpreter calling convention");
3766 
3767   // do the call - the index is actually the method to call
3768   // that is, f2 is a vtable index if !is_vfinal, else f2 is a Method*
3769 
3770   // It's final, need a null check here!
3771   __ null_check(recv);
3772 
3773   // profile this call
3774   __ profile_final_call(rax);
3775   __ profile_arguments_type(rax, method, rbcp, true);
3776 
3777   __ jump_from_interpreted(method, rax);
3778 
3779   __ bind(notFinal);
3780 
3781   // get receiver klass
3782   __ null_check(recv, oopDesc::klass_offset_in_bytes());
3783   __ load_klass(rax, recv);
3784 
3785   // profile this call
3786   __ profile_virtual_call(rax, rlocals, rdx);
3787   // get target Method* & entry point
3788   __ lookup_virtual_method(rax, index, method);
3789   __ profile_called_method(method, rdx, rbcp);
3790 
3791   __ profile_arguments_type(rdx, method, rbcp, true);
3792   __ jump_from_interpreted(method, rdx);
3793 }
3794 
3795 void TemplateTable::invokevirtual(int byte_no) {
3796   transition(vtos, vtos);
3797   assert(byte_no == f2_byte, "use this argument");
3798   prepare_invoke(byte_no,
3799                  rbx,    // method or vtable index
3800                  noreg,  // unused itable index
3801                  rcx, rdx); // recv, flags
3802 
3803   // rbx: index
3804   // rcx: receiver
3805   // rdx: flags
3806 
3807   invokevirtual_helper(rbx, rcx, rdx);
3808 }
3809 
3810 void TemplateTable::invokespecial(int byte_no) {
3811   transition(vtos, vtos);
3812   assert(byte_no == f1_byte, "use this argument");
3813   prepare_invoke(byte_no, rbx, noreg,  // get f1 Method*
3814                  rcx);  // get receiver also for null check
3815   __ verify_oop(rcx);
3816   __ null_check(rcx);
3817   // do the call
3818   __ profile_call(rax);
3819   __ profile_arguments_type(rax, rbx, rbcp, false);
3820   __ jump_from_interpreted(rbx, rax);
3821 }
3822 
3823 void TemplateTable::invokestatic(int byte_no) {
3824   transition(vtos, vtos);
3825   assert(byte_no == f1_byte, "use this argument");
3826   prepare_invoke(byte_no, rbx);  // get f1 Method*
3827   // do the call
3828   __ profile_call(rax);
3829   __ profile_arguments_type(rax, rbx, rbcp, false);
3830   __ jump_from_interpreted(rbx, rax);
3831 }
3832 
3833 
3834 void TemplateTable::fast_invokevfinal(int byte_no) {
3835   transition(vtos, vtos);
3836   assert(byte_no == f2_byte, "use this argument");
3837   __ stop("fast_invokevfinal not used on x86");
3838 }
3839 
3840 
3841 void TemplateTable::invokeinterface(int byte_no) {
3842   transition(vtos, vtos);
3843   assert(byte_no == f1_byte, "use this argument");
3844   prepare_invoke(byte_no, rax, rbx,  // get f1 Klass*, f2 Method*
3845                  rcx, rdx); // recv, flags
3846 
3847   // rax: reference klass (from f1)
3848   // rbx: method (from f2)
3849   // rcx: receiver
3850   // rdx: flags
3851 
3852   // Special case of invokeinterface called for virtual method of
3853   // java.lang.Object.  See cpCacheOop.cpp for details.
3854   // This code isn't produced by javac, but could be produced by
3855   // another compliant java compiler.
3856   Label notMethod;
3857   __ movl(rlocals, rdx);
3858   __ andl(rlocals, (1 << ConstantPoolCacheEntry::is_forced_virtual_shift));
3859 
3860   __ jcc(Assembler::zero, notMethod);
3861 
3862   invokevirtual_helper(rbx, rcx, rdx);
3863   __ bind(notMethod);
3864 
3865   // Get receiver klass into rdx - also a null check
3866   __ restore_locals();  // restore r14
3867   __ null_check(rcx, oopDesc::klass_offset_in_bytes());
3868   __ load_klass(rdx, rcx);
3869 
3870   Label no_such_interface, no_such_method;
3871 
3872   // Receiver subtype check against REFC.
3873   // Superklass in rax. Subklass in rdx. Blows rcx, rdi.
3874   __ lookup_interface_method(// inputs: rec. class, interface, itable index
3875                              rdx, rax, noreg,
3876                              // outputs: scan temp. reg, scan temp. reg
3877                              rbcp, rlocals,
3878                              no_such_interface,
3879                              /*return_method=*/false);
3880 
3881   // profile this call
3882   __ restore_bcp(); // rbcp was destroyed by receiver type check
3883   __ profile_virtual_call(rdx, rbcp, rlocals);
3884 
3885   // Get declaring interface class from method, and itable index
3886   __ movptr(rax, Address(rbx, Method::const_offset()));
3887   __ movptr(rax, Address(rax, ConstMethod::constants_offset()));
3888   __ movptr(rax, Address(rax, ConstantPool::pool_holder_offset_in_bytes()));
3889   __ movl(rbx, Address(rbx, Method::itable_index_offset()));
3890   __ subl(rbx, Method::itable_index_max);
3891   __ negl(rbx);
3892 
3893   __ lookup_interface_method(// inputs: rec. class, interface, itable index
3894                              rdx, rax, rbx,
3895                              // outputs: method, scan temp. reg
3896                              rbx, rbcp,
3897                              no_such_interface);
3898 
3899   // rbx: Method* to call
3900   // rcx: receiver
3901   // Check for abstract method error
3902   // Note: This should be done more efficiently via a throw_abstract_method_error
3903   //       interpreter entry point and a conditional jump to it in case of a null
3904   //       method.
3905   __ testptr(rbx, rbx);
3906   __ jcc(Assembler::zero, no_such_method);
3907 
3908   __ profile_called_method(rbx, rbcp, rdx);
3909   __ profile_arguments_type(rdx, rbx, rbcp, true);
3910 
3911   // do the call
3912   // rcx: receiver
3913   // rbx,: Method*
3914   __ jump_from_interpreted(rbx, rdx);
3915   __ should_not_reach_here();
3916 
3917   // exception handling code follows...
3918   // note: must restore interpreter registers to canonical
3919   //       state for exception handling to work correctly!
3920 
3921   __ bind(no_such_method);
3922   // throw exception
3923   __ pop(rbx);           // pop return address (pushed by prepare_invoke)
3924   __ restore_bcp();      // rbcp must be correct for exception handler   (was destroyed)
3925   __ restore_locals();   // make sure locals pointer is correct as well (was destroyed)
3926   __ call_VM(noreg, CAST_FROM_FN_PTR(address, InterpreterRuntime::throw_AbstractMethodError));
3927   // the call_VM checks for exception, so we should never return here.
3928   __ should_not_reach_here();
3929 
3930   __ bind(no_such_interface);
3931   // throw exception
3932   __ pop(rbx);           // pop return address (pushed by prepare_invoke)
3933   __ restore_bcp();      // rbcp must be correct for exception handler   (was destroyed)
3934   __ restore_locals();   // make sure locals pointer is correct as well (was destroyed)
3935   __ call_VM(noreg, CAST_FROM_FN_PTR(address,
3936                    InterpreterRuntime::throw_IncompatibleClassChangeError));
3937   // the call_VM checks for exception, so we should never return here.
3938   __ should_not_reach_here();
3939 }
3940 
3941 void TemplateTable::invokehandle(int byte_no) {
3942   transition(vtos, vtos);
3943   assert(byte_no == f1_byte, "use this argument");
3944   const Register rbx_method = rbx;
3945   const Register rax_mtype  = rax;
3946   const Register rcx_recv   = rcx;
3947   const Register rdx_flags  = rdx;
3948 
3949   prepare_invoke(byte_no, rbx_method, rax_mtype, rcx_recv);
3950   __ verify_method_ptr(rbx_method);
3951   __ verify_oop(rcx_recv);
3952   __ null_check(rcx_recv);
3953 
3954   // rax: MethodType object (from cpool->resolved_references[f1], if necessary)
3955   // rbx: MH.invokeExact_MT method (from f2)
3956 
3957   // Note:  rax_mtype is already pushed (if necessary) by prepare_invoke
3958 
3959   // FIXME: profile the LambdaForm also
3960   __ profile_final_call(rax);
3961   __ profile_arguments_type(rdx, rbx_method, rbcp, true);
3962 
3963   __ jump_from_interpreted(rbx_method, rdx);
3964 }
3965 
3966 void TemplateTable::invokedynamic(int byte_no) {
3967   transition(vtos, vtos);
3968   assert(byte_no == f1_byte, "use this argument");
3969 
3970   const Register rbx_method   = rbx;
3971   const Register rax_callsite = rax;
3972 
3973   prepare_invoke(byte_no, rbx_method, rax_callsite);
3974 
3975   // rax: CallSite object (from cpool->resolved_references[f1])
3976   // rbx: MH.linkToCallSite method (from f2)
3977 
3978   // Note:  rax_callsite is already pushed by prepare_invoke
3979 
3980   // %%% should make a type profile for any invokedynamic that takes a ref argument
3981   // profile this call
3982   __ profile_call(rbcp);
3983   __ profile_arguments_type(rdx, rbx_method, rbcp, false);
3984 
3985   __ verify_oop(rax_callsite);
3986 
3987   __ jump_from_interpreted(rbx_method, rdx);
3988 }
3989 
3990 //-----------------------------------------------------------------------------
3991 // Allocation
3992 
3993 void TemplateTable::_new() {
3994   transition(vtos, atos);
3995   __ get_unsigned_2_byte_index_at_bcp(rdx, 1);
3996   Label slow_case;
3997   Label slow_case_no_pop;
3998   Label done;
3999   Label initialize_header;
4000   Label initialize_object;  // including clearing the fields
4001 
4002   __ get_cpool_and_tags(rcx, rax);
4003 
4004   // Make sure the class we're about to instantiate has been resolved.
4005   // This is done before loading InstanceKlass to be consistent with the order
4006   // how Constant Pool is updated (see ConstantPool::klass_at_put)
4007   const int tags_offset = Array<u1>::base_offset_in_bytes();
4008   __ cmpb(Address(rax, rdx, Address::times_1, tags_offset), JVM_CONSTANT_Class);
4009   __ jcc(Assembler::notEqual, slow_case_no_pop);
4010 
4011   // get InstanceKlass
4012   __ load_resolved_klass_at_index(rcx, rdx, rcx);
4013   __ push(rcx);  // save the contexts of klass for initializing the header
4014 
4015   // make sure klass is initialized & doesn't have finalizer
4016   // make sure klass is fully initialized
4017   __ cmpb(Address(rcx, InstanceKlass::init_state_offset()), InstanceKlass::fully_initialized);
4018   __ jcc(Assembler::notEqual, slow_case);
4019 
4020   // get instance_size in InstanceKlass (scaled to a count of bytes)
4021   __ movl(rdx, Address(rcx, Klass::layout_helper_offset()));
4022   // test to see if it has a finalizer or is malformed in some way
4023   __ testl(rdx, Klass::_lh_instance_slow_path_bit);
4024   __ jcc(Assembler::notZero, slow_case);
4025 
4026   // Allocate the instance:
4027   //  If TLAB is enabled:
4028   //    Try to allocate in the TLAB.
4029   //    If fails, go to the slow path.
4030   //  Else If inline contiguous allocations are enabled:
4031   //    Try to allocate in eden.
4032   //    If fails due to heap end, go to slow path.
4033   //
4034   //  If TLAB is enabled OR inline contiguous is enabled:
4035   //    Initialize the allocation.
4036   //    Exit.
4037   //
4038   //  Go to slow path.
4039 
4040   const bool allow_shared_alloc =
4041     Universe::heap()->supports_inline_contig_alloc();
4042 
4043   const Register thread = LP64_ONLY(r15_thread) NOT_LP64(rcx);
4044 #ifndef _LP64
4045   if (UseTLAB || allow_shared_alloc) {
4046     __ get_thread(thread);
4047   }
4048 #endif // _LP64
4049 
4050   if (UseTLAB) {
4051     __ movptr(rax, Address(thread, in_bytes(JavaThread::tlab_top_offset())));
4052     __ lea(rbx, Address(rax, rdx, Address::times_1));
4053     __ cmpptr(rbx, Address(thread, in_bytes(JavaThread::tlab_end_offset())));
4054     __ jcc(Assembler::above, slow_case);
4055     __ movptr(Address(thread, in_bytes(JavaThread::tlab_top_offset())), rbx);
4056     if (ZeroTLAB) {
4057       // the fields have been already cleared
4058       __ jmp(initialize_header);
4059     } else {
4060       // initialize both the header and fields
4061       __ jmp(initialize_object);
4062     }
4063   } else {
4064     // Allocation in the shared Eden, if allowed.
4065     //
4066     // rdx: instance size in bytes
4067     if (allow_shared_alloc) {
4068       ExternalAddress heap_top((address)Universe::heap()->top_addr());
4069       ExternalAddress heap_end((address)Universe::heap()->end_addr());
4070 
4071       Label retry;
4072       __ bind(retry);
4073       __ movptr(rax, heap_top);
4074       __ lea(rbx, Address(rax, rdx, Address::times_1));
4075       __ cmpptr(rbx, heap_end);
4076       __ jcc(Assembler::above, slow_case);
4077 
4078       // Compare rax, with the top addr, and if still equal, store the new
4079       // top addr in rbx, at the address of the top addr pointer. Sets ZF if was
4080       // equal, and clears it otherwise. Use lock prefix for atomicity on MPs.
4081       //
4082       // rax,: object begin
4083       // rbx,: object end
4084       // rdx: instance size in bytes
4085       __ locked_cmpxchgptr(rbx, heap_top);
4086 
4087       // if someone beat us on the allocation, try again, otherwise continue
4088       __ jcc(Assembler::notEqual, retry);
4089 
4090       __ incr_allocated_bytes(thread, rdx, 0);
4091     }
4092   }
4093 
4094   // If UseTLAB or allow_shared_alloc are true, the object is created above and
4095   // there is an initialize need. Otherwise, skip and go to the slow path.
4096   if (UseTLAB || allow_shared_alloc) {
4097     // The object is initialized before the header.  If the object size is
4098     // zero, go directly to the header initialization.
4099     __ bind(initialize_object);
4100     __ decrement(rdx, sizeof(oopDesc));
4101     __ jcc(Assembler::zero, initialize_header);
4102 
4103     // Initialize topmost object field, divide rdx by 8, check if odd and
4104     // test if zero.
4105     __ xorl(rcx, rcx);    // use zero reg to clear memory (shorter code)
4106     __ shrl(rdx, LogBytesPerLong); // divide by 2*oopSize and set carry flag if odd
4107 
4108     // rdx must have been multiple of 8
4109 #ifdef ASSERT
4110     // make sure rdx was multiple of 8
4111     Label L;
4112     // Ignore partial flag stall after shrl() since it is debug VM
4113     __ jccb(Assembler::carryClear, L);
4114     __ stop("object size is not multiple of 2 - adjust this code");
4115     __ bind(L);
4116     // rdx must be > 0, no extra check needed here
4117 #endif
4118 
4119     // initialize remaining object fields: rdx was a multiple of 8
4120     { Label loop;
4121     __ bind(loop);
4122     __ movptr(Address(rax, rdx, Address::times_8, sizeof(oopDesc) - 1*oopSize), rcx);
4123     NOT_LP64(__ movptr(Address(rax, rdx, Address::times_8, sizeof(oopDesc) - 2*oopSize), rcx));
4124     __ decrement(rdx);
4125     __ jcc(Assembler::notZero, loop);
4126     }
4127 
4128     // initialize object header only.
4129     __ bind(initialize_header);
4130     if (UseBiasedLocking) {
4131       __ pop(rcx);   // get saved klass back in the register.
4132       __ movptr(rbx, Address(rcx, Klass::prototype_header_offset()));
4133       __ movptr(Address(rax, oopDesc::mark_offset_in_bytes ()), rbx);
4134     } else {
4135       __ movptr(Address(rax, oopDesc::mark_offset_in_bytes ()),
4136                 (intptr_t)markOopDesc::prototype()); // header
4137       __ pop(rcx);   // get saved klass back in the register.
4138     }
4139 #ifdef _LP64
4140     __ xorl(rsi, rsi); // use zero reg to clear memory (shorter code)
4141     __ store_klass_gap(rax, rsi);  // zero klass gap for compressed oops
4142 #endif
4143     __ store_klass(rax, rcx);  // klass
4144 
4145     {
4146       SkipIfEqual skip_if(_masm, &DTraceAllocProbes, 0);
4147       // Trigger dtrace event for fastpath
4148       __ push(atos);
4149       __ call_VM_leaf(
4150            CAST_FROM_FN_PTR(address, SharedRuntime::dtrace_object_alloc), rax);
4151       __ pop(atos);
4152     }
4153 
4154     __ jmp(done);
4155   }
4156 
4157   // slow case
4158   __ bind(slow_case);
4159   __ pop(rcx);   // restore stack pointer to what it was when we came in.
4160   __ bind(slow_case_no_pop);
4161 
4162   Register rarg1 = LP64_ONLY(c_rarg1) NOT_LP64(rax);
4163   Register rarg2 = LP64_ONLY(c_rarg2) NOT_LP64(rdx);
4164 
4165   __ get_constant_pool(rarg1);
4166   __ get_unsigned_2_byte_index_at_bcp(rarg2, 1);
4167   call_VM(rax, CAST_FROM_FN_PTR(address, InterpreterRuntime::_new), rarg1, rarg2);
4168    __ verify_oop(rax);
4169 
4170   // continue
4171   __ bind(done);
4172 }
4173 
4174 void TemplateTable::newarray() {
4175   transition(itos, atos);
4176   Register rarg1 = LP64_ONLY(c_rarg1) NOT_LP64(rdx);
4177   __ load_unsigned_byte(rarg1, at_bcp(1));
4178   call_VM(rax, CAST_FROM_FN_PTR(address, InterpreterRuntime::newarray),
4179           rarg1, rax);
4180 }
4181 
4182 void TemplateTable::anewarray() {
4183   transition(itos, atos);
4184 
4185   Register rarg1 = LP64_ONLY(c_rarg1) NOT_LP64(rcx);
4186   Register rarg2 = LP64_ONLY(c_rarg2) NOT_LP64(rdx);
4187 
4188   __ get_unsigned_2_byte_index_at_bcp(rarg2, 1);
4189   __ get_constant_pool(rarg1);
4190   call_VM(rax, CAST_FROM_FN_PTR(address, InterpreterRuntime::anewarray),
4191           rarg1, rarg2, rax);
4192 }
4193 
4194 void TemplateTable::arraylength() {
4195   transition(atos, itos);
4196   __ null_check(rax, arrayOopDesc::length_offset_in_bytes());
4197   __ movl(rax, Address(rax, arrayOopDesc::length_offset_in_bytes()));
4198 }
4199 
4200 void TemplateTable::checkcast() {
4201   transition(atos, atos);
4202   Label done, is_null, ok_is_subtype, quicked, resolved;
4203   __ testptr(rax, rax); // object is in rax
4204   __ jcc(Assembler::zero, is_null);
4205 
4206   // Get cpool & tags index
4207   __ get_cpool_and_tags(rcx, rdx); // rcx=cpool, rdx=tags array
4208   __ get_unsigned_2_byte_index_at_bcp(rbx, 1); // rbx=index
4209   // See if bytecode has already been quicked
4210   __ cmpb(Address(rdx, rbx,
4211                   Address::times_1,
4212                   Array<u1>::base_offset_in_bytes()),
4213           JVM_CONSTANT_Class);
4214   __ jcc(Assembler::equal, quicked);
4215   __ push(atos); // save receiver for result, and for GC
4216   call_VM(noreg, CAST_FROM_FN_PTR(address, InterpreterRuntime::quicken_io_cc));
4217 
4218   // vm_result_2 has metadata result
4219 #ifndef _LP64
4220   // borrow rdi from locals
4221   __ get_thread(rdi);
4222   __ get_vm_result_2(rax, rdi);
4223   __ restore_locals();
4224 #else
4225   __ get_vm_result_2(rax, r15_thread);
4226 #endif
4227 
4228   __ pop_ptr(rdx); // restore receiver
4229   __ jmpb(resolved);
4230 
4231   // Get superklass in rax and subklass in rbx
4232   __ bind(quicked);
4233   __ mov(rdx, rax); // Save object in rdx; rax needed for subtype check
4234   __ load_resolved_klass_at_index(rcx, rbx, rax);
4235 
4236   __ bind(resolved);
4237   __ load_klass(rbx, rdx);
4238 
4239   // Generate subtype check.  Blows rcx, rdi.  Object in rdx.
4240   // Superklass in rax.  Subklass in rbx.
4241   __ gen_subtype_check(rbx, ok_is_subtype);
4242 
4243   // Come here on failure
4244   __ push_ptr(rdx);
4245   // object is at TOS
4246   __ jump(ExternalAddress(Interpreter::_throw_ClassCastException_entry));
4247 
4248   // Come here on success
4249   __ bind(ok_is_subtype);
4250   __ mov(rax, rdx); // Restore object in rdx
4251 
4252   // Collect counts on whether this check-cast sees NULLs a lot or not.
4253   if (ProfileInterpreter) {
4254     __ jmp(done);
4255     __ bind(is_null);
4256     __ profile_null_seen(rcx);
4257   } else {
4258     __ bind(is_null);   // same as 'done'
4259   }
4260   __ bind(done);
4261 }
4262 
4263 void TemplateTable::instanceof() {
4264   transition(atos, itos);
4265   Label done, is_null, ok_is_subtype, quicked, resolved;
4266   __ testptr(rax, rax);
4267   __ jcc(Assembler::zero, is_null);
4268 
4269   // Get cpool & tags index
4270   __ get_cpool_and_tags(rcx, rdx); // rcx=cpool, rdx=tags array
4271   __ get_unsigned_2_byte_index_at_bcp(rbx, 1); // rbx=index
4272   // See if bytecode has already been quicked
4273   __ cmpb(Address(rdx, rbx,
4274                   Address::times_1,
4275                   Array<u1>::base_offset_in_bytes()),
4276           JVM_CONSTANT_Class);
4277   __ jcc(Assembler::equal, quicked);
4278 
4279   __ push(atos); // save receiver for result, and for GC
4280   call_VM(noreg, CAST_FROM_FN_PTR(address, InterpreterRuntime::quicken_io_cc));
4281   // vm_result_2 has metadata result
4282 
4283 #ifndef _LP64
4284   // borrow rdi from locals
4285   __ get_thread(rdi);
4286   __ get_vm_result_2(rax, rdi);
4287   __ restore_locals();
4288 #else
4289   __ get_vm_result_2(rax, r15_thread);
4290 #endif
4291 
4292   __ pop_ptr(rdx); // restore receiver
4293   __ verify_oop(rdx);
4294   __ load_klass(rdx, rdx);
4295   __ jmpb(resolved);
4296 
4297   // Get superklass in rax and subklass in rdx
4298   __ bind(quicked);
4299   __ load_klass(rdx, rax);
4300   __ load_resolved_klass_at_index(rcx, rbx, rax);
4301 
4302   __ bind(resolved);
4303 
4304   // Generate subtype check.  Blows rcx, rdi
4305   // Superklass in rax.  Subklass in rdx.
4306   __ gen_subtype_check(rdx, ok_is_subtype);
4307 
4308   // Come here on failure
4309   __ xorl(rax, rax);
4310   __ jmpb(done);
4311   // Come here on success
4312   __ bind(ok_is_subtype);
4313   __ movl(rax, 1);
4314 
4315   // Collect counts on whether this test sees NULLs a lot or not.
4316   if (ProfileInterpreter) {
4317     __ jmp(done);
4318     __ bind(is_null);
4319     __ profile_null_seen(rcx);
4320   } else {
4321     __ bind(is_null);   // same as 'done'
4322   }
4323   __ bind(done);
4324   // rax = 0: obj == NULL or  obj is not an instanceof the specified klass
4325   // rax = 1: obj != NULL and obj is     an instanceof the specified klass
4326 }
4327 
4328 
4329 //----------------------------------------------------------------------------------------------------
4330 // Breakpoints
4331 void TemplateTable::_breakpoint() {
4332   // Note: We get here even if we are single stepping..
4333   // jbug insists on setting breakpoints at every bytecode
4334   // even if we are in single step mode.
4335 
4336   transition(vtos, vtos);
4337 
4338   Register rarg = LP64_ONLY(c_rarg1) NOT_LP64(rcx);
4339 
4340   // get the unpatched byte code
4341   __ get_method(rarg);
4342   __ call_VM(noreg,
4343              CAST_FROM_FN_PTR(address,
4344                               InterpreterRuntime::get_original_bytecode_at),
4345              rarg, rbcp);
4346   __ mov(rbx, rax);  // why?
4347 
4348   // post the breakpoint event
4349   __ get_method(rarg);
4350   __ call_VM(noreg,
4351              CAST_FROM_FN_PTR(address, InterpreterRuntime::_breakpoint),
4352              rarg, rbcp);
4353 
4354   // complete the execution of original bytecode
4355   __ dispatch_only_normal(vtos);
4356 }
4357 
4358 //-----------------------------------------------------------------------------
4359 // Exceptions
4360 
4361 void TemplateTable::athrow() {
4362   transition(atos, vtos);
4363   __ null_check(rax);
4364   __ jump(ExternalAddress(Interpreter::throw_exception_entry()));
4365 }
4366 
4367 //-----------------------------------------------------------------------------
4368 // Synchronization
4369 //
4370 // Note: monitorenter & exit are symmetric routines; which is reflected
4371 //       in the assembly code structure as well
4372 //
4373 // Stack layout:
4374 //
4375 // [expressions  ] <--- rsp               = expression stack top
4376 // ..
4377 // [expressions  ]
4378 // [monitor entry] <--- monitor block top = expression stack bot
4379 // ..
4380 // [monitor entry]
4381 // [frame data   ] <--- monitor block bot
4382 // ...
4383 // [saved rbp    ] <--- rbp
4384 void TemplateTable::monitorenter() {
4385   transition(atos, vtos);
4386 
4387   // check for NULL object
4388   __ null_check(rax);
4389 
4390   const Address monitor_block_top(
4391         rbp, frame::interpreter_frame_monitor_block_top_offset * wordSize);
4392   const Address monitor_block_bot(
4393         rbp, frame::interpreter_frame_initial_sp_offset * wordSize);
4394   const int entry_size = frame::interpreter_frame_monitor_size() * wordSize;
4395 
4396   Label allocated;
4397 
4398   Register rtop = LP64_ONLY(c_rarg3) NOT_LP64(rcx);
4399   Register rbot = LP64_ONLY(c_rarg2) NOT_LP64(rbx);
4400   Register rmon = LP64_ONLY(c_rarg1) NOT_LP64(rdx);
4401 
4402   // initialize entry pointer
4403   __ xorl(rmon, rmon); // points to free slot or NULL
4404 
4405   // find a free slot in the monitor block (result in rmon)
4406   {
4407     Label entry, loop, exit;
4408     __ movptr(rtop, monitor_block_top); // points to current entry,
4409                                         // starting with top-most entry
4410     __ lea(rbot, monitor_block_bot);    // points to word before bottom
4411                                         // of monitor block
4412     __ jmpb(entry);
4413 
4414     __ bind(loop);
4415     // check if current entry is used
4416     __ cmpptr(Address(rtop, BasicObjectLock::obj_offset_in_bytes()), (int32_t) NULL_WORD);
4417     // if not used then remember entry in rmon
4418     __ cmovptr(Assembler::equal, rmon, rtop);   // cmov => cmovptr
4419     // check if current entry is for same object
4420     __ cmpptr(rax, Address(rtop, BasicObjectLock::obj_offset_in_bytes()));
4421     // if same object then stop searching
4422     __ jccb(Assembler::equal, exit);
4423     // otherwise advance to next entry
4424     __ addptr(rtop, entry_size);
4425     __ bind(entry);
4426     // check if bottom reached
4427     __ cmpptr(rtop, rbot);
4428     // if not at bottom then check this entry
4429     __ jcc(Assembler::notEqual, loop);
4430     __ bind(exit);
4431   }
4432 
4433   __ testptr(rmon, rmon); // check if a slot has been found
4434   __ jcc(Assembler::notZero, allocated); // if found, continue with that one
4435 
4436   // allocate one if there's no free slot
4437   {
4438     Label entry, loop;
4439     // 1. compute new pointers          // rsp: old expression stack top
4440     __ movptr(rmon, monitor_block_bot); // rmon: old expression stack bottom
4441     __ subptr(rsp, entry_size);         // move expression stack top
4442     __ subptr(rmon, entry_size);        // move expression stack bottom
4443     __ mov(rtop, rsp);                  // set start value for copy loop
4444     __ movptr(monitor_block_bot, rmon); // set new monitor block bottom
4445     __ jmp(entry);
4446     // 2. move expression stack contents
4447     __ bind(loop);
4448     __ movptr(rbot, Address(rtop, entry_size)); // load expression stack
4449                                                 // word from old location
4450     __ movptr(Address(rtop, 0), rbot);          // and store it at new location
4451     __ addptr(rtop, wordSize);                  // advance to next word
4452     __ bind(entry);
4453     __ cmpptr(rtop, rmon);                      // check if bottom reached
4454     __ jcc(Assembler::notEqual, loop);          // if not at bottom then
4455                                                 // copy next word
4456   }
4457 
4458   // call run-time routine
4459   // rmon: points to monitor entry
4460   __ bind(allocated);
4461 
4462   // Increment bcp to point to the next bytecode, so exception
4463   // handling for async. exceptions work correctly.
4464   // The object has already been poped from the stack, so the
4465   // expression stack looks correct.
4466   __ increment(rbcp);
4467 
4468   // store object
4469   __ movptr(Address(rmon, BasicObjectLock::obj_offset_in_bytes()), rax);
4470   __ lock_object(rmon);
4471 
4472   // check to make sure this monitor doesn't cause stack overflow after locking
4473   __ save_bcp();  // in case of exception
4474   __ generate_stack_overflow_check(0);
4475 
4476   // The bcp has already been incremented. Just need to dispatch to
4477   // next instruction.
4478   __ dispatch_next(vtos);
4479 }
4480 
4481 void TemplateTable::monitorexit() {
4482   transition(atos, vtos);
4483 
4484   // check for NULL object
4485   __ null_check(rax);
4486 
4487   const Address monitor_block_top(
4488         rbp, frame::interpreter_frame_monitor_block_top_offset * wordSize);
4489   const Address monitor_block_bot(
4490         rbp, frame::interpreter_frame_initial_sp_offset * wordSize);
4491   const int entry_size = frame::interpreter_frame_monitor_size() * wordSize;
4492 
4493   Register rtop = LP64_ONLY(c_rarg1) NOT_LP64(rdx);
4494   Register rbot = LP64_ONLY(c_rarg2) NOT_LP64(rbx);
4495 
4496   Label found;
4497 
4498   // find matching slot
4499   {
4500     Label entry, loop;
4501     __ movptr(rtop, monitor_block_top); // points to current entry,
4502                                         // starting with top-most entry
4503     __ lea(rbot, monitor_block_bot);    // points to word before bottom
4504                                         // of monitor block
4505     __ jmpb(entry);
4506 
4507     __ bind(loop);
4508     // check if current entry is for same object
4509     __ cmpptr(rax, Address(rtop, BasicObjectLock::obj_offset_in_bytes()));
4510     // if same object then stop searching
4511     __ jcc(Assembler::equal, found);
4512     // otherwise advance to next entry
4513     __ addptr(rtop, entry_size);
4514     __ bind(entry);
4515     // check if bottom reached
4516     __ cmpptr(rtop, rbot);
4517     // if not at bottom then check this entry
4518     __ jcc(Assembler::notEqual, loop);
4519   }
4520 
4521   // error handling. Unlocking was not block-structured
4522   __ call_VM(noreg, CAST_FROM_FN_PTR(address,
4523                    InterpreterRuntime::throw_illegal_monitor_state_exception));
4524   __ should_not_reach_here();
4525 
4526   // call run-time routine
4527   __ bind(found);
4528   __ push_ptr(rax); // make sure object is on stack (contract with oopMaps)
4529   __ unlock_object(rtop);
4530   __ pop_ptr(rax); // discard object
4531 }
4532 
4533 // Wide instructions
4534 void TemplateTable::wide() {
4535   transition(vtos, vtos);
4536   __ load_unsigned_byte(rbx, at_bcp(1));
4537   ExternalAddress wtable((address)Interpreter::_wentry_point);
4538   __ jump(ArrayAddress(wtable, Address(noreg, rbx, Address::times_ptr)));
4539   // Note: the rbcp increment step is part of the individual wide bytecode implementations
4540 }
4541 
4542 // Multi arrays
4543 void TemplateTable::multianewarray() {
4544   transition(vtos, atos);
4545 
4546   Register rarg = LP64_ONLY(c_rarg1) NOT_LP64(rax);
4547   __ load_unsigned_byte(rax, at_bcp(3)); // get number of dimensions
4548   // last dim is on top of stack; we want address of first one:
4549   // first_addr = last_addr + (ndims - 1) * stackElementSize - 1*wordsize
4550   // the latter wordSize to point to the beginning of the array.
4551   __ lea(rarg, Address(rsp, rax, Interpreter::stackElementScale(), -wordSize));
4552   call_VM(rax, CAST_FROM_FN_PTR(address, InterpreterRuntime::multianewarray), rarg);
4553   __ load_unsigned_byte(rbx, at_bcp(3));
4554   __ lea(rsp, Address(rsp, rbx, Interpreter::stackElementScale()));  // get rid of counts
4555 }