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