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