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