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