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