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