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 taken, not_taken;
2515   __ pop_ptr(rdx);
2516 
2517   const int is_value_mask = markOopDesc::always_locked_pattern;
2518   if (EnableValhalla && UsePointerPerturbation) {
2519     Label is_null;
2520     __ testptr(rdx, rdx);
2521     __ jcc(Assembler::zero, is_null);
2522     __ movptr(rbx, Address(rdx, oopDesc::mark_offset_in_bytes()));
2523     __ andptr(rbx, is_value_mask);
2524     __ cmpl(rbx, is_value_mask);
2525     __ setb(Assembler::equal, rbx);
2526     __ movzbl(rbx, rbx);
2527     __ orptr(rdx, rbx);
2528     __ bind(is_null);
2529   }
2530 
2531   __ cmpoop(rdx, rax);
2532 
2533   if (EnableValhalla && !UsePointerPerturbation) {
2534     __ jcc(Assembler::notEqual, (cc == not_equal) ? taken : not_taken);
2535     __ testptr(rdx, rdx);
2536     __ jcc(Assembler::zero, (cc == equal) ? taken : not_taken);
2537     __ movptr(rbx, Address(rdx, oopDesc::mark_offset_in_bytes()));
2538     __ andptr(rbx, is_value_mask);
2539     __ cmpl(rbx, is_value_mask);
2540     cc = (cc == equal) ? not_equal : equal;
2541   }
2542 
2543   __ jcc(j_not(cc), not_taken);
2544   __ bind(taken);
2545   branch(false, false);
2546   __ bind(not_taken);
2547   __ profile_not_taken_branch(rax);
2548 }
2549 
2550 void TemplateTable::ret() {
2551   transition(vtos, vtos);
2552   locals_index(rbx);
2553   LP64_ONLY(__ movslq(rbx, iaddress(rbx))); // get return bci, compute return bcp
2554   NOT_LP64(__ movptr(rbx, iaddress(rbx)));
2555   __ profile_ret(rbx, rcx);
2556   __ get_method(rax);
2557   __ movptr(rbcp, Address(rax, Method::const_offset()));
2558   __ lea(rbcp, Address(rbcp, rbx, Address::times_1,
2559                       ConstMethod::codes_offset()));
2560   __ dispatch_next(vtos, 0, true);
2561 }
2562 
2563 void TemplateTable::wide_ret() {
2564   transition(vtos, vtos);
2565   locals_index_wide(rbx);
2566   __ movptr(rbx, aaddress(rbx)); // get return bci, compute return bcp
2567   __ profile_ret(rbx, rcx);
2568   __ get_method(rax);
2569   __ movptr(rbcp, Address(rax, Method::const_offset()));
2570   __ lea(rbcp, Address(rbcp, rbx, Address::times_1, ConstMethod::codes_offset()));
2571   __ dispatch_next(vtos, 0, true);
2572 }
2573 
2574 void TemplateTable::tableswitch() {
2575   Label default_case, continue_execution;
2576   transition(itos, vtos);
2577 
2578   // align r13/rsi
2579   __ lea(rbx, at_bcp(BytesPerInt));
2580   __ andptr(rbx, -BytesPerInt);
2581   // load lo & hi
2582   __ movl(rcx, Address(rbx, BytesPerInt));
2583   __ movl(rdx, Address(rbx, 2 * BytesPerInt));
2584   __ bswapl(rcx);
2585   __ bswapl(rdx);
2586   // check against lo & hi
2587   __ cmpl(rax, rcx);
2588   __ jcc(Assembler::less, default_case);
2589   __ cmpl(rax, rdx);
2590   __ jcc(Assembler::greater, default_case);
2591   // lookup dispatch offset
2592   __ subl(rax, rcx);
2593   __ movl(rdx, Address(rbx, rax, Address::times_4, 3 * BytesPerInt));
2594   __ profile_switch_case(rax, rbx, rcx);
2595   // continue execution
2596   __ bind(continue_execution);
2597   __ bswapl(rdx);
2598   LP64_ONLY(__ movl2ptr(rdx, rdx));
2599   __ load_unsigned_byte(rbx, Address(rbcp, rdx, Address::times_1));
2600   __ addptr(rbcp, rdx);
2601   __ dispatch_only(vtos, true);
2602   // handle default
2603   __ bind(default_case);
2604   __ profile_switch_default(rax);
2605   __ movl(rdx, Address(rbx, 0));
2606   __ jmp(continue_execution);
2607 }
2608 
2609 void TemplateTable::lookupswitch() {
2610   transition(itos, itos);
2611   __ stop("lookupswitch bytecode should have been rewritten");
2612 }
2613 
2614 void TemplateTable::fast_linearswitch() {
2615   transition(itos, vtos);
2616   Label loop_entry, loop, found, continue_execution;
2617   // bswap rax so we can avoid bswapping the table entries
2618   __ bswapl(rax);
2619   // align r13
2620   __ lea(rbx, at_bcp(BytesPerInt)); // btw: should be able to get rid of
2621                                     // this instruction (change offsets
2622                                     // below)
2623   __ andptr(rbx, -BytesPerInt);
2624   // set counter
2625   __ movl(rcx, Address(rbx, BytesPerInt));
2626   __ bswapl(rcx);
2627   __ jmpb(loop_entry);
2628   // table search
2629   __ bind(loop);
2630   __ cmpl(rax, Address(rbx, rcx, Address::times_8, 2 * BytesPerInt));
2631   __ jcc(Assembler::equal, found);
2632   __ bind(loop_entry);
2633   __ decrementl(rcx);
2634   __ jcc(Assembler::greaterEqual, loop);
2635   // default case
2636   __ profile_switch_default(rax);
2637   __ movl(rdx, Address(rbx, 0));
2638   __ jmp(continue_execution);
2639   // entry found -> get offset
2640   __ bind(found);
2641   __ movl(rdx, Address(rbx, rcx, Address::times_8, 3 * BytesPerInt));
2642   __ profile_switch_case(rcx, rax, rbx);
2643   // continue execution
2644   __ bind(continue_execution);
2645   __ bswapl(rdx);
2646   __ movl2ptr(rdx, rdx);
2647   __ load_unsigned_byte(rbx, Address(rbcp, rdx, Address::times_1));
2648   __ addptr(rbcp, rdx);
2649   __ dispatch_only(vtos, true);
2650 }
2651 
2652 void TemplateTable::fast_binaryswitch() {
2653   transition(itos, vtos);
2654   // Implementation using the following core algorithm:
2655   //
2656   // int binary_search(int key, LookupswitchPair* array, int n) {
2657   //   // Binary search according to "Methodik des Programmierens" by
2658   //   // Edsger W. Dijkstra and W.H.J. Feijen, Addison Wesley Germany 1985.
2659   //   int i = 0;
2660   //   int j = n;
2661   //   while (i+1 < j) {
2662   //     // invariant P: 0 <= i < j <= n and (a[i] <= key < a[j] or Q)
2663   //     // with      Q: for all i: 0 <= i < n: key < a[i]
2664   //     // where a stands for the array and assuming that the (inexisting)
2665   //     // element a[n] is infinitely big.
2666   //     int h = (i + j) >> 1;
2667   //     // i < h < j
2668   //     if (key < array[h].fast_match()) {
2669   //       j = h;
2670   //     } else {
2671   //       i = h;
2672   //     }
2673   //   }
2674   //   // R: a[i] <= key < a[i+1] or Q
2675   //   // (i.e., if key is within array, i is the correct index)
2676   //   return i;
2677   // }
2678 
2679   // Register allocation
2680   const Register key   = rax; // already set (tosca)
2681   const Register array = rbx;
2682   const Register i     = rcx;
2683   const Register j     = rdx;
2684   const Register h     = rdi;
2685   const Register temp  = rsi;
2686 
2687   // Find array start
2688   NOT_LP64(__ save_bcp());
2689 
2690   __ lea(array, at_bcp(3 * BytesPerInt)); // btw: should be able to
2691                                           // get rid of this
2692                                           // instruction (change
2693                                           // offsets below)
2694   __ andptr(array, -BytesPerInt);
2695 
2696   // Initialize i & j
2697   __ xorl(i, i);                            // i = 0;
2698   __ movl(j, Address(array, -BytesPerInt)); // j = length(array);
2699 
2700   // Convert j into native byteordering
2701   __ bswapl(j);
2702 
2703   // And start
2704   Label entry;
2705   __ jmp(entry);
2706 
2707   // binary search loop
2708   {
2709     Label loop;
2710     __ bind(loop);
2711     // int h = (i + j) >> 1;
2712     __ leal(h, Address(i, j, Address::times_1)); // h = i + j;
2713     __ sarl(h, 1);                               // h = (i + j) >> 1;
2714     // if (key < array[h].fast_match()) {
2715     //   j = h;
2716     // } else {
2717     //   i = h;
2718     // }
2719     // Convert array[h].match to native byte-ordering before compare
2720     __ movl(temp, Address(array, h, Address::times_8));
2721     __ bswapl(temp);
2722     __ cmpl(key, temp);
2723     // j = h if (key <  array[h].fast_match())
2724     __ cmov32(Assembler::less, j, h);
2725     // i = h if (key >= array[h].fast_match())
2726     __ cmov32(Assembler::greaterEqual, i, h);
2727     // while (i+1 < j)
2728     __ bind(entry);
2729     __ leal(h, Address(i, 1)); // i+1
2730     __ cmpl(h, j);             // i+1 < j
2731     __ jcc(Assembler::less, loop);
2732   }
2733 
2734   // end of binary search, result index is i (must check again!)
2735   Label default_case;
2736   // Convert array[i].match to native byte-ordering before compare
2737   __ movl(temp, Address(array, i, Address::times_8));
2738   __ bswapl(temp);
2739   __ cmpl(key, temp);
2740   __ jcc(Assembler::notEqual, default_case);
2741 
2742   // entry found -> j = offset
2743   __ movl(j , Address(array, i, Address::times_8, BytesPerInt));
2744   __ profile_switch_case(i, key, array);
2745   __ bswapl(j);
2746   LP64_ONLY(__ movslq(j, j));
2747 
2748   NOT_LP64(__ restore_bcp());
2749   NOT_LP64(__ restore_locals());                           // restore rdi
2750 
2751   __ load_unsigned_byte(rbx, Address(rbcp, j, Address::times_1));
2752   __ addptr(rbcp, j);
2753   __ dispatch_only(vtos, true);
2754 
2755   // default case -> j = default offset
2756   __ bind(default_case);
2757   __ profile_switch_default(i);
2758   __ movl(j, Address(array, -2 * BytesPerInt));
2759   __ bswapl(j);
2760   LP64_ONLY(__ movslq(j, j));
2761 
2762   NOT_LP64(__ restore_bcp());
2763   NOT_LP64(__ restore_locals());
2764 
2765   __ load_unsigned_byte(rbx, Address(rbcp, j, Address::times_1));
2766   __ addptr(rbcp, j);
2767   __ dispatch_only(vtos, true);
2768 }
2769 
2770 void TemplateTable::_return(TosState state) {
2771   transition(state, state);
2772 
2773   assert(_desc->calls_vm(),
2774          "inconsistent calls_vm information"); // call in remove_activation
2775 
2776   if (_desc->bytecode() == Bytecodes::_return_register_finalizer) {
2777     assert(state == vtos, "only valid state");
2778     Register robj = LP64_ONLY(c_rarg1) NOT_LP64(rax);
2779     __ movptr(robj, aaddress(0));
2780     __ load_klass(rdi, robj);
2781     __ movl(rdi, Address(rdi, Klass::access_flags_offset()));
2782     __ testl(rdi, JVM_ACC_HAS_FINALIZER);
2783     Label skip_register_finalizer;
2784     __ jcc(Assembler::zero, skip_register_finalizer);
2785 
2786     __ call_VM(noreg, CAST_FROM_FN_PTR(address, InterpreterRuntime::register_finalizer), robj);
2787 
2788     __ bind(skip_register_finalizer);
2789   }
2790 
2791   if (SafepointMechanism::uses_thread_local_poll() && _desc->bytecode() != Bytecodes::_return_register_finalizer) {
2792     Label no_safepoint;
2793     NOT_PRODUCT(__ block_comment("Thread-local Safepoint poll"));
2794 #ifdef _LP64
2795     __ testb(Address(r15_thread, Thread::polling_page_offset()), SafepointMechanism::poll_bit());
2796 #else
2797     const Register thread = rdi;
2798     __ get_thread(thread);
2799     __ testb(Address(thread, Thread::polling_page_offset()), SafepointMechanism::poll_bit());
2800 #endif
2801     __ jcc(Assembler::zero, no_safepoint);
2802     __ push(state);
2803     __ call_VM(noreg, CAST_FROM_FN_PTR(address,
2804                                     InterpreterRuntime::at_safepoint));
2805     __ pop(state);
2806     __ bind(no_safepoint);
2807   }
2808 
2809   if (EnableValhalla && state == atos) {
2810     Label not_returning_null_vt;
2811     const Register method = rbx, tmp = rdx;
2812 
2813     __ testl(rax, rax);
2814     __ jcc(Assembler::notZero, not_returning_null_vt);
2815     __ get_method(method);
2816     __ load_unsigned_short(tmp, Address(rbx, Method::flags_offset()));
2817     __ testl(tmp, Method::is_returning_vt_mask());
2818     __ jcc(Assembler::zero, not_returning_null_vt);
2819     __ call_VM(noreg, CAST_FROM_FN_PTR(address, InterpreterRuntime::deoptimize_caller_frame_for_vt), method);
2820     __ bind(not_returning_null_vt);
2821 
2822     if (ValueTypesBufferMaxMemory > 0) {
2823       Label notBuffered;
2824 
2825       __ test_value_is_not_buffered(rax, rbx, notBuffered);
2826       const Register thread1 = NOT_LP64(rcx) LP64_ONLY(r15_thread);
2827       __ call_VM(noreg, CAST_FROM_FN_PTR(address, InterpreterRuntime::return_value), rax);
2828       NOT_LP64(__ get_thread(thread1));
2829       __ get_vm_result(rax, thread1);
2830       __ bind(notBuffered);
2831     }
2832   }
2833 
2834   // Narrow result if state is itos but result type is smaller.
2835   // Need to narrow in the return bytecode rather than in generate_return_entry
2836   // since compiled code callers expect the result to already be narrowed.
2837   if (state == itos) {
2838     __ narrow(rax);
2839   }
2840 
2841   __ remove_activation(state, rbcp, true, true, true, /*state == qtos*/ false && ValueTypeReturnedAsFields);
2842 
2843   __ jmp(rbcp);
2844 }
2845 
2846 // ----------------------------------------------------------------------------
2847 // Volatile variables demand their effects be made known to all CPU's
2848 // in order.  Store buffers on most chips allow reads & writes to
2849 // reorder; the JMM's ReadAfterWrite.java test fails in -Xint mode
2850 // without some kind of memory barrier (i.e., it's not sufficient that
2851 // the interpreter does not reorder volatile references, the hardware
2852 // also must not reorder them).
2853 //
2854 // According to the new Java Memory Model (JMM):
2855 // (1) All volatiles are serialized wrt to each other.  ALSO reads &
2856 //     writes act as aquire & release, so:
2857 // (2) A read cannot let unrelated NON-volatile memory refs that
2858 //     happen after the read float up to before the read.  It's OK for
2859 //     non-volatile memory refs that happen before the volatile read to
2860 //     float down below it.
2861 // (3) Similar a volatile write cannot let unrelated NON-volatile
2862 //     memory refs that happen BEFORE the write float down to after the
2863 //     write.  It's OK for non-volatile memory refs that happen after the
2864 //     volatile write to float up before it.
2865 //
2866 // We only put in barriers around volatile refs (they are expensive),
2867 // not _between_ memory refs (that would require us to track the
2868 // flavor of the previous memory refs).  Requirements (2) and (3)
2869 // require some barriers before volatile stores and after volatile
2870 // loads.  These nearly cover requirement (1) but miss the
2871 // volatile-store-volatile-load case.  This final case is placed after
2872 // volatile-stores although it could just as well go before
2873 // volatile-loads.
2874 
2875 void TemplateTable::volatile_barrier(Assembler::Membar_mask_bits order_constraint ) {
2876   // Helper function to insert a is-volatile test and memory barrier
2877   if(!os::is_MP()) return;    // Not needed on single CPU
2878   __ membar(order_constraint);
2879 }
2880 
2881 void TemplateTable::resolve_cache_and_index(int byte_no,
2882                                             Register Rcache,
2883                                             Register index,
2884                                             size_t index_size) {
2885   const Register temp = rbx;
2886   assert_different_registers(Rcache, index, temp);
2887 
2888   Label resolved;
2889 
2890   Bytecodes::Code code = bytecode();
2891   switch (code) {
2892   case Bytecodes::_nofast_getfield: code = Bytecodes::_getfield; break;
2893   case Bytecodes::_nofast_putfield: code = Bytecodes::_putfield; break;
2894   default: break;
2895   }
2896 
2897   assert(byte_no == f1_byte || byte_no == f2_byte, "byte_no out of range");
2898   __ get_cache_and_index_and_bytecode_at_bcp(Rcache, index, temp, byte_no, 1, index_size);
2899   __ cmpl(temp, code);  // have we resolved this bytecode?
2900   __ jcc(Assembler::equal, resolved);
2901 
2902   // resolve first time through
2903   address entry = CAST_FROM_FN_PTR(address, InterpreterRuntime::resolve_from_cache);
2904   __ movl(temp, code);
2905   __ call_VM(noreg, entry, temp);
2906   // Update registers with resolved info
2907   __ get_cache_and_index_at_bcp(Rcache, index, 1, index_size);
2908   __ bind(resolved);
2909 }
2910 
2911 // The cache and index registers must be set before call
2912 void TemplateTable::load_field_cp_cache_entry(Register obj,
2913                                               Register cache,
2914                                               Register index,
2915                                               Register off,
2916                                               Register flags,
2917                                               bool is_static = false) {
2918   assert_different_registers(cache, index, flags, off);
2919 
2920   ByteSize cp_base_offset = ConstantPoolCache::base_offset();
2921   // Field offset
2922   __ movptr(off, Address(cache, index, Address::times_ptr,
2923                          in_bytes(cp_base_offset +
2924                                   ConstantPoolCacheEntry::f2_offset())));
2925   // Flags
2926   __ movl(flags, Address(cache, index, Address::times_ptr,
2927                          in_bytes(cp_base_offset +
2928                                   ConstantPoolCacheEntry::flags_offset())));
2929 
2930   // klass overwrite register
2931   if (is_static) {
2932     __ movptr(obj, Address(cache, index, Address::times_ptr,
2933                            in_bytes(cp_base_offset +
2934                                     ConstantPoolCacheEntry::f1_offset())));
2935     const int mirror_offset = in_bytes(Klass::java_mirror_offset());
2936     __ movptr(obj, Address(obj, mirror_offset));
2937     __ resolve_oop_handle(obj);
2938   }
2939 }
2940 
2941 void TemplateTable::load_invoke_cp_cache_entry(int byte_no,
2942                                                Register method,
2943                                                Register itable_index,
2944                                                Register flags,
2945                                                bool is_invokevirtual,
2946                                                bool is_invokevfinal, /*unused*/
2947                                                bool is_invokedynamic) {
2948   // setup registers
2949   const Register cache = rcx;
2950   const Register index = rdx;
2951   assert_different_registers(method, flags);
2952   assert_different_registers(method, cache, index);
2953   assert_different_registers(itable_index, flags);
2954   assert_different_registers(itable_index, cache, index);
2955   // determine constant pool cache field offsets
2956   assert(is_invokevirtual == (byte_no == f2_byte), "is_invokevirtual flag redundant");
2957   const int method_offset = in_bytes(
2958     ConstantPoolCache::base_offset() +
2959       ((byte_no == f2_byte)
2960        ? ConstantPoolCacheEntry::f2_offset()
2961        : ConstantPoolCacheEntry::f1_offset()));
2962   const int flags_offset = in_bytes(ConstantPoolCache::base_offset() +
2963                                     ConstantPoolCacheEntry::flags_offset());
2964   // access constant pool cache fields
2965   const int index_offset = in_bytes(ConstantPoolCache::base_offset() +
2966                                     ConstantPoolCacheEntry::f2_offset());
2967 
2968   size_t index_size = (is_invokedynamic ? sizeof(u4) : sizeof(u2));
2969   resolve_cache_and_index(byte_no, cache, index, index_size);
2970     __ movptr(method, Address(cache, index, Address::times_ptr, method_offset));
2971 
2972   if (itable_index != noreg) {
2973     // pick up itable or appendix index from f2 also:
2974     __ movptr(itable_index, Address(cache, index, Address::times_ptr, index_offset));
2975   }
2976   __ movl(flags, Address(cache, index, Address::times_ptr, flags_offset));
2977 }
2978 
2979 // The registers cache and index expected to be set before call.
2980 // Correct values of the cache and index registers are preserved.
2981 void TemplateTable::jvmti_post_field_access(Register cache,
2982                                             Register index,
2983                                             bool is_static,
2984                                             bool has_tos) {
2985   if (JvmtiExport::can_post_field_access()) {
2986     // Check to see if a field access watch has been set before we take
2987     // the time to call into the VM.
2988     Label L1;
2989     assert_different_registers(cache, index, rax);
2990     __ mov32(rax, ExternalAddress((address) JvmtiExport::get_field_access_count_addr()));
2991     __ testl(rax,rax);
2992     __ jcc(Assembler::zero, L1);
2993 
2994     // cache entry pointer
2995     __ addptr(cache, in_bytes(ConstantPoolCache::base_offset()));
2996     __ shll(index, LogBytesPerWord);
2997     __ addptr(cache, index);
2998     if (is_static) {
2999       __ xorptr(rax, rax);      // NULL object reference
3000     } else {
3001       __ pop(atos);         // Get the object
3002       __ verify_oop(rax);
3003       __ push(atos);        // Restore stack state
3004     }
3005     // rax,:   object pointer or NULL
3006     // cache: cache entry pointer
3007     __ call_VM(noreg, CAST_FROM_FN_PTR(address, InterpreterRuntime::post_field_access),
3008                rax, cache);
3009     __ get_cache_and_index_at_bcp(cache, index, 1);
3010     __ bind(L1);
3011   }
3012 }
3013 
3014 void TemplateTable::pop_and_check_object(Register r) {
3015   __ pop_ptr(r);
3016   __ null_check(r);  // for field access must check obj.
3017   __ verify_oop(r);
3018 }
3019 
3020 void TemplateTable::getfield_or_static(int byte_no, bool is_static, RewriteControl rc) {
3021   transition(vtos, vtos);
3022 
3023   const Register cache = rcx;
3024   const Register index = rdx;
3025   const Register obj   = LP64_ONLY(c_rarg3) NOT_LP64(rcx);
3026   const Register off   = rbx;
3027   const Register flags = rax;
3028   const Register bc    = LP64_ONLY(c_rarg3) NOT_LP64(rcx); // uses same reg as obj, so don't mix them
3029   const Register flags2 = rdx;
3030 
3031   resolve_cache_and_index(byte_no, cache, index, sizeof(u2));
3032   jvmti_post_field_access(cache, index, is_static, false);
3033   load_field_cp_cache_entry(obj, cache, index, off, flags, is_static);
3034 
3035   const Address field(obj, off, Address::times_1, 0*wordSize);
3036 
3037   Label Done, notByte, notBool, notInt, notShort, notChar, notLong, notFloat, notObj, notValueType, notDouble;
3038 
3039   if (!is_static) {
3040     __ movptr(rcx, Address(cache, index, Address::times_ptr,
3041                            in_bytes(ConstantPoolCache::base_offset() +
3042                                     ConstantPoolCacheEntry::f1_offset())));
3043   }
3044 
3045   __ movl(flags2, flags);
3046 
3047   __ shrl(flags, ConstantPoolCacheEntry::tos_state_shift);
3048   // Make sure we don't need to mask edx after the above shift
3049   assert(btos == 0, "change code, btos != 0");
3050 
3051   __ andl(flags, ConstantPoolCacheEntry::tos_state_mask);
3052 
3053   __ jcc(Assembler::notZero, notByte);
3054   // btos
3055   if (!is_static) pop_and_check_object(obj);
3056   __ access_load_at(T_BYTE, IN_HEAP, rax, field, noreg, noreg);
3057   __ push(btos);
3058   // Rewrite bytecode to be faster
3059   if (!is_static && rc == may_rewrite) {
3060     patch_bytecode(Bytecodes::_fast_bgetfield, bc, rbx);
3061   }
3062   __ jmp(Done);
3063 
3064   __ bind(notByte);
3065 
3066   __ cmpl(flags, ztos);
3067   __ jcc(Assembler::notEqual, notBool);
3068    if (!is_static) pop_and_check_object(obj);
3069   // ztos (same code as btos)
3070   __ access_load_at(T_BOOLEAN, IN_HEAP, rax, field, noreg, noreg);
3071   __ push(ztos);
3072   // Rewrite bytecode to be faster
3073   if (!is_static && rc == may_rewrite) {
3074     // use btos rewriting, no truncating to t/f bit is needed for getfield.
3075     patch_bytecode(Bytecodes::_fast_bgetfield, bc, rbx);
3076   }
3077   __ jmp(Done);
3078 
3079   __ bind(notBool);
3080   __ cmpl(flags, atos);
3081   __ jcc(Assembler::notEqual, notObj);
3082   // atos
3083   if (!EnableValhalla) {
3084     if (!is_static) pop_and_check_object(obj);
3085     do_oop_load(_masm, field, rax);
3086     __ push(atos);
3087     if (!is_static && rc == may_rewrite) {
3088       patch_bytecode(Bytecodes::_fast_agetfield, bc, rbx);
3089     }
3090     __ jmp(Done);
3091   } else {
3092     if (is_static) {
3093       __ load_heap_oop(rax, field);
3094       Label isFlattenable, uninitialized;
3095       // Issue below if the static field has not been initialized yet
3096       __ test_field_is_flattenable(flags2, rscratch1, isFlattenable);
3097         // Not flattenable case
3098         __ push(atos);
3099         __ jmp(Done);
3100       // Flattenable case, must not return null even if uninitialized
3101       __ bind(isFlattenable);
3102         __ testptr(rax, rax);
3103         __ jcc(Assembler::zero, uninitialized);
3104           __ push(atos);
3105           __ jmp(Done);
3106         __ bind(uninitialized);
3107           __ andl(flags2, ConstantPoolCacheEntry::field_index_mask);
3108           __ call_VM(rax, CAST_FROM_FN_PTR(address, InterpreterRuntime::uninitialized_static_value_field),
3109                  obj, flags2);
3110           __ verify_oop(rax);
3111           __ push(atos);
3112           __ jmp(Done);
3113     } else {
3114       Label isFlattened, nonnull, isFlattenable, rewriteFlattenable;
3115       __ test_field_is_flattenable(flags2, rscratch1, isFlattenable);
3116         // Non-flattenable field case, also covers the object case
3117         pop_and_check_object(obj);
3118         __ load_heap_oop(rax, field);
3119         __ push(atos);
3120         if (rc == may_rewrite) {
3121           patch_bytecode(Bytecodes::_fast_agetfield, bc, rbx);
3122         }
3123         __ jmp(Done);
3124       __ bind(isFlattenable);
3125         __ test_field_is_flattened(flags2, rscratch1, isFlattened);
3126           // Non-flattened field case
3127           pop_and_check_object(obj);
3128           __ load_heap_oop(rax, field);
3129           __ testptr(rax, rax);
3130           __ jcc(Assembler::notZero, nonnull);
3131             __ andl(flags2, ConstantPoolCacheEntry::field_index_mask);
3132             __ call_VM(rax, CAST_FROM_FN_PTR(address, InterpreterRuntime::uninitialized_instance_value_field),
3133                        obj, flags2);
3134           __ bind(nonnull);
3135           __ verify_oop(rax);
3136           __ push(atos);
3137           __ jmp(rewriteFlattenable);
3138         __ bind(isFlattened);
3139           __ andl(flags2, ConstantPoolCacheEntry::field_index_mask);
3140           pop_and_check_object(rbx);
3141           call_VM(rax, CAST_FROM_FN_PTR(address, InterpreterRuntime::read_flattened_field),
3142                   rbx, flags2, rcx);
3143           __ verify_oop(rax);
3144           __ push(atos);
3145       __ bind(rewriteFlattenable);
3146       if (rc == may_rewrite) {
3147         patch_bytecode(Bytecodes::_fast_qgetfield, bc, rbx);
3148       }
3149       __ jmp(Done);
3150     }
3151   }
3152 
3153   __ bind(notObj);
3154 
3155   if (!is_static) pop_and_check_object(obj);
3156 
3157   __ cmpl(flags, itos);
3158   __ jcc(Assembler::notEqual, notInt);
3159   // itos
3160   __ access_load_at(T_INT, IN_HEAP, rax, field, noreg, noreg);
3161   __ push(itos);
3162   // Rewrite bytecode to be faster
3163   if (!is_static && rc == may_rewrite) {
3164     patch_bytecode(Bytecodes::_fast_igetfield, bc, rbx);
3165   }
3166   __ jmp(Done);
3167 
3168   __ bind(notInt);
3169   __ cmpl(flags, ctos);
3170   __ jcc(Assembler::notEqual, notChar);
3171   // ctos
3172   __ access_load_at(T_CHAR, IN_HEAP, rax, field, noreg, noreg);
3173   __ push(ctos);
3174   // Rewrite bytecode to be faster
3175   if (!is_static && rc == may_rewrite) {
3176     patch_bytecode(Bytecodes::_fast_cgetfield, bc, rbx);
3177   }
3178   __ jmp(Done);
3179 
3180   __ bind(notChar);
3181   __ cmpl(flags, stos);
3182   __ jcc(Assembler::notEqual, notShort);
3183   // stos
3184   __ access_load_at(T_SHORT, IN_HEAP, rax, field, noreg, noreg);
3185   __ push(stos);
3186   // Rewrite bytecode to be faster
3187   if (!is_static && rc == may_rewrite) {
3188     patch_bytecode(Bytecodes::_fast_sgetfield, bc, rbx);
3189   }
3190   __ jmp(Done);
3191 
3192   __ bind(notShort);
3193   __ cmpl(flags, ltos);
3194   __ jcc(Assembler::notEqual, notLong);
3195   // ltos
3196     // Generate code as if volatile (x86_32).  There just aren't enough registers to
3197     // save that information and this code is faster than the test.
3198   __ access_load_at(T_LONG, IN_HEAP | MO_RELAXED, noreg /* ltos */, field, noreg, noreg);
3199   __ push(ltos);
3200   // Rewrite bytecode to be faster
3201   LP64_ONLY(if (!is_static && rc == may_rewrite) patch_bytecode(Bytecodes::_fast_lgetfield, bc, rbx));
3202   __ jmp(Done);
3203 
3204   __ bind(notLong);
3205   __ cmpl(flags, ftos);
3206   __ jcc(Assembler::notEqual, notFloat);
3207   // ftos
3208 
3209   __ access_load_at(T_FLOAT, IN_HEAP, noreg /* ftos */, field, noreg, noreg);
3210   __ push(ftos);
3211   // Rewrite bytecode to be faster
3212   if (!is_static && rc == may_rewrite) {
3213     patch_bytecode(Bytecodes::_fast_fgetfield, bc, rbx);
3214   }
3215   __ jmp(Done);
3216 
3217   __ bind(notFloat);
3218 #ifdef ASSERT
3219   __ cmpl(flags, dtos);
3220   __ jcc(Assembler::notEqual, notDouble);
3221 #endif
3222   // dtos
3223   __ access_load_at(T_DOUBLE, IN_HEAP, noreg /* dtos */, field, noreg, noreg);
3224   __ push(dtos);
3225   // Rewrite bytecode to be faster
3226   if (!is_static && rc == may_rewrite) {
3227     patch_bytecode(Bytecodes::_fast_dgetfield, bc, rbx);
3228   }
3229 #ifdef ASSERT
3230   __ jmp(Done);
3231 
3232 
3233   __ bind(notDouble);
3234   __ stop("Bad state");
3235 #endif
3236 
3237   __ bind(Done);
3238   // [jk] not needed currently
3239   // volatile_barrier(Assembler::Membar_mask_bits(Assembler::LoadLoad |
3240   //                                              Assembler::LoadStore));
3241 }
3242 
3243 void TemplateTable::getfield(int byte_no) {
3244   getfield_or_static(byte_no, false);
3245 }
3246 
3247 void TemplateTable::nofast_getfield(int byte_no) {
3248   getfield_or_static(byte_no, false, may_not_rewrite);
3249 }
3250 
3251 void TemplateTable::getstatic(int byte_no) {
3252   getfield_or_static(byte_no, true);
3253 }
3254 
3255 void TemplateTable::withfield() {
3256   transition(vtos, atos);
3257 
3258   Register cache = LP64_ONLY(c_rarg1) NOT_LP64(rcx);
3259   Register index = LP64_ONLY(c_rarg2) NOT_LP64(rdx);
3260 
3261   resolve_cache_and_index(f2_byte, cache, index, sizeof(u2));
3262 
3263   call_VM(rbx, CAST_FROM_FN_PTR(address, InterpreterRuntime::withfield), cache);
3264   // new value type is returned in rbx
3265   // stack adjustement is returned in rax
3266   __ verify_oop(rbx);
3267   __ addptr(rsp, rax);
3268   __ movptr(rax, rbx);
3269 }
3270 
3271 // The registers cache and index expected to be set before call.
3272 // The function may destroy various registers, just not the cache and index registers.
3273 void TemplateTable::jvmti_post_field_mod(Register cache, Register index, bool is_static) {
3274 
3275   const Register robj = LP64_ONLY(c_rarg2)   NOT_LP64(rax);
3276   const Register RBX  = LP64_ONLY(c_rarg1)   NOT_LP64(rbx);
3277   const Register RCX  = LP64_ONLY(c_rarg3)   NOT_LP64(rcx);
3278   const Register RDX  = LP64_ONLY(rscratch1) NOT_LP64(rdx);
3279 
3280   ByteSize cp_base_offset = ConstantPoolCache::base_offset();
3281 
3282   if (JvmtiExport::can_post_field_modification()) {
3283     // Check to see if a field modification watch has been set before
3284     // we take the time to call into the VM.
3285     Label L1;
3286     assert_different_registers(cache, index, rax);
3287     __ mov32(rax, ExternalAddress((address)JvmtiExport::get_field_modification_count_addr()));
3288     __ testl(rax, rax);
3289     __ jcc(Assembler::zero, L1);
3290 
3291     __ get_cache_and_index_at_bcp(robj, RDX, 1);
3292 
3293 
3294     if (is_static) {
3295       // Life is simple.  Null out the object pointer.
3296       __ xorl(RBX, RBX);
3297 
3298     } else {
3299       // Life is harder. The stack holds the value on top, followed by
3300       // the object.  We don't know the size of the value, though; it
3301       // could be one or two words depending on its type. As a result,
3302       // we must find the type to determine where the object is.
3303 #ifndef _LP64
3304       Label two_word, valsize_known;
3305 #endif
3306       __ movl(RCX, Address(robj, RDX,
3307                            Address::times_ptr,
3308                            in_bytes(cp_base_offset +
3309                                      ConstantPoolCacheEntry::flags_offset())));
3310       NOT_LP64(__ mov(rbx, rsp));
3311       __ shrl(RCX, ConstantPoolCacheEntry::tos_state_shift);
3312 
3313       // Make sure we don't need to mask rcx after the above shift
3314       ConstantPoolCacheEntry::verify_tos_state_shift();
3315 #ifdef _LP64
3316       __ movptr(c_rarg1, at_tos_p1());  // initially assume a one word jvalue
3317       __ cmpl(c_rarg3, ltos);
3318       __ cmovptr(Assembler::equal,
3319                  c_rarg1, at_tos_p2()); // ltos (two word jvalue)
3320       __ cmpl(c_rarg3, dtos);
3321       __ cmovptr(Assembler::equal,
3322                  c_rarg1, at_tos_p2()); // dtos (two word jvalue)
3323 #else
3324       __ cmpl(rcx, ltos);
3325       __ jccb(Assembler::equal, two_word);
3326       __ cmpl(rcx, dtos);
3327       __ jccb(Assembler::equal, two_word);
3328       __ addptr(rbx, Interpreter::expr_offset_in_bytes(1)); // one word jvalue (not ltos, dtos)
3329       __ jmpb(valsize_known);
3330 
3331       __ bind(two_word);
3332       __ addptr(rbx, Interpreter::expr_offset_in_bytes(2)); // two words jvalue
3333 
3334       __ bind(valsize_known);
3335       // setup object pointer
3336       __ movptr(rbx, Address(rbx, 0));
3337 #endif
3338     }
3339     // cache entry pointer
3340     __ addptr(robj, in_bytes(cp_base_offset));
3341     __ shll(RDX, LogBytesPerWord);
3342     __ addptr(robj, RDX);
3343     // object (tos)
3344     __ mov(RCX, rsp);
3345     // c_rarg1: object pointer set up above (NULL if static)
3346     // c_rarg2: cache entry pointer
3347     // c_rarg3: jvalue object on the stack
3348     __ call_VM(noreg,
3349                CAST_FROM_FN_PTR(address,
3350                                 InterpreterRuntime::post_field_modification),
3351                RBX, robj, RCX);
3352     __ get_cache_and_index_at_bcp(cache, index, 1);
3353     __ bind(L1);
3354   }
3355 }
3356 
3357 void TemplateTable::putfield_or_static(int byte_no, bool is_static, RewriteControl rc) {
3358   transition(vtos, vtos);
3359 
3360   const Register cache = rcx;
3361   const Register index = rdx;
3362   const Register obj   = rcx;
3363   const Register off   = rbx;
3364   const Register flags = rax;
3365   const Register bc    = LP64_ONLY(c_rarg3) NOT_LP64(rcx);
3366   const Register flags2 = rdx;
3367 
3368   resolve_cache_and_index(byte_no, cache, index, sizeof(u2));
3369   jvmti_post_field_mod(cache, index, is_static);
3370   load_field_cp_cache_entry(obj, cache, index, off, flags, is_static);
3371 
3372   // [jk] not needed currently
3373   // volatile_barrier(Assembler::Membar_mask_bits(Assembler::LoadStore |
3374   //                                              Assembler::StoreStore));
3375 
3376   Label notVolatile, Done;
3377 
3378   __ movl(flags2, flags);
3379 
3380   // field addresses
3381   const Address field(obj, off, Address::times_1, 0*wordSize);
3382   NOT_LP64( const Address hi(obj, off, Address::times_1, 1*wordSize);)
3383 
3384   Label notByte, notBool, notInt, notShort, notChar,
3385         notLong, notFloat, notObj, notValueType, notDouble;
3386 
3387   __ shrl(flags, ConstantPoolCacheEntry::tos_state_shift);
3388 
3389   assert(btos == 0, "change code, btos != 0");
3390   __ andl(flags, ConstantPoolCacheEntry::tos_state_mask);
3391   __ jcc(Assembler::notZero, notByte);
3392 
3393   // btos
3394   {
3395     __ pop(btos);
3396     if (!is_static) pop_and_check_object(obj);
3397     __ access_store_at(T_BYTE, IN_HEAP, field, rax, noreg, noreg);
3398     if (!is_static && rc == may_rewrite) {
3399       patch_bytecode(Bytecodes::_fast_bputfield, bc, rbx, true, byte_no);
3400     }
3401     __ jmp(Done);
3402   }
3403 
3404   __ bind(notByte);
3405   __ cmpl(flags, ztos);
3406   __ jcc(Assembler::notEqual, notBool);
3407 
3408   // ztos
3409   {
3410     __ pop(ztos);
3411     if (!is_static) pop_and_check_object(obj);
3412     __ access_store_at(T_BOOLEAN, IN_HEAP, field, rax, noreg, noreg);
3413     if (!is_static && rc == may_rewrite) {
3414       patch_bytecode(Bytecodes::_fast_zputfield, bc, rbx, true, byte_no);
3415     }
3416     __ jmp(Done);
3417   }
3418 
3419   __ bind(notBool);
3420   __ cmpl(flags, atos);
3421   __ jcc(Assembler::notEqual, notObj);
3422 
3423   // atos
3424   {
3425     if (!EnableValhalla) {
3426       __ pop(atos);
3427       if (!is_static) pop_and_check_object(obj);
3428       // Store into the field
3429       do_oop_store(_masm, field, rax);
3430       if (!is_static && rc == may_rewrite) {
3431         patch_bytecode(Bytecodes::_fast_aputfield, bc, rbx, true, byte_no);
3432       }
3433       __ jmp(Done);
3434     } else {
3435       __ pop(atos);
3436       if (is_static) {
3437         Label notFlattenable, notBuffered;
3438         __ test_field_is_not_flattenable(flags2, rscratch1, notFlattenable);
3439         __ null_check(rax);
3440         __ bind(notFlattenable);
3441         if (ValueTypesBufferMaxMemory > 0) {
3442           __ test_value_is_not_buffered(rax, rscratch1, notBuffered);
3443           call_VM(noreg, CAST_FROM_FN_PTR(address, InterpreterRuntime::write_heap_copy),
3444                   rax, off, obj);
3445           __ jmp(Done);
3446           __ bind(notBuffered);
3447         }
3448         do_oop_store(_masm, field, rax);
3449         __ jmp(Done);
3450       } else {
3451         Label isFlattenable, isFlattened, notBuffered, notBuffered2, rewriteNotFlattenable, rewriteFlattenable;
3452         __ test_field_is_flattenable(flags2, rscratch1, isFlattenable);
3453         // Not flattenable case, covers not flattenable values and objects
3454         pop_and_check_object(obj);
3455         // Store into the field
3456         if (ValueTypesBufferMaxMemory > 0) {
3457           __ test_value_is_not_buffered(rax, rscratch1, notBuffered);
3458           call_VM(noreg, CAST_FROM_FN_PTR(address, InterpreterRuntime::write_heap_copy),
3459                   rax, off, obj);
3460           __ jmp(rewriteNotFlattenable);
3461           __ bind(notBuffered);
3462         }
3463         do_oop_store(_masm, field, rax);
3464         __ bind(rewriteNotFlattenable);
3465         if (rc == may_rewrite) {
3466           patch_bytecode(Bytecodes::_fast_aputfield, bc, rbx, true, byte_no);
3467         }
3468         __ jmp(Done);
3469         // Implementation of the flattenable semantic
3470         __ bind(isFlattenable);
3471         __ null_check(rax);
3472         __ test_field_is_flattened(flags2, rscratch1, isFlattened);
3473         // Not flattened case
3474         if (ValueTypesBufferMaxMemory > 0) {
3475           __ test_value_is_not_buffered(rax, rscratch1, notBuffered2);
3476           pop_and_check_object(obj);
3477           call_VM(noreg, CAST_FROM_FN_PTR(address, InterpreterRuntime::write_heap_copy),
3478                   rax, off, obj);
3479           __ jmp(rewriteFlattenable);
3480           __ bind(notBuffered2);
3481         }
3482         pop_and_check_object(obj);
3483         // Store into the field
3484         do_oop_store(_masm, field, rax);
3485         __ jmp(rewriteFlattenable);
3486         __ bind(isFlattened);
3487         pop_and_check_object(obj);
3488         call_VM(noreg, CAST_FROM_FN_PTR(address, InterpreterRuntime::write_flattened_value),
3489                 rax, off, obj);
3490         __ bind(rewriteFlattenable);
3491         if (rc == may_rewrite) {
3492           patch_bytecode(Bytecodes::_fast_qputfield, bc, rbx, true, byte_no);
3493         }
3494         __ jmp(Done);
3495       }
3496     }
3497   }
3498 
3499   __ bind(notObj);
3500   __ cmpl(flags, itos);
3501   __ jcc(Assembler::notEqual, notInt);
3502 
3503   // itos
3504   {
3505     __ pop(itos);
3506     if (!is_static) pop_and_check_object(obj);
3507     __ access_store_at(T_INT, IN_HEAP, field, rax, noreg, noreg);
3508     if (!is_static && rc == may_rewrite) {
3509       patch_bytecode(Bytecodes::_fast_iputfield, bc, rbx, true, byte_no);
3510     }
3511     __ jmp(Done);
3512   }
3513 
3514   __ bind(notInt);
3515   __ cmpl(flags, ctos);
3516   __ jcc(Assembler::notEqual, notChar);
3517 
3518   // ctos
3519   {
3520     __ pop(ctos);
3521     if (!is_static) pop_and_check_object(obj);
3522     __ access_store_at(T_CHAR, IN_HEAP, field, rax, noreg, noreg);
3523     if (!is_static && rc == may_rewrite) {
3524       patch_bytecode(Bytecodes::_fast_cputfield, bc, rbx, true, byte_no);
3525     }
3526     __ jmp(Done);
3527   }
3528 
3529   __ bind(notChar);
3530   __ cmpl(flags, stos);
3531   __ jcc(Assembler::notEqual, notShort);
3532 
3533   // stos
3534   {
3535     __ pop(stos);
3536     if (!is_static) pop_and_check_object(obj);
3537     __ access_store_at(T_SHORT, IN_HEAP, field, rax, noreg, noreg);
3538     if (!is_static && rc == may_rewrite) {
3539       patch_bytecode(Bytecodes::_fast_sputfield, bc, rbx, true, byte_no);
3540     }
3541     __ jmp(Done);
3542   }
3543 
3544   __ bind(notShort);
3545   __ cmpl(flags, ltos);
3546   __ jcc(Assembler::notEqual, notLong);
3547 
3548   // ltos
3549 #ifdef _LP64
3550   {
3551     __ pop(ltos);
3552     if (!is_static) pop_and_check_object(obj);
3553     __ access_store_at(T_LONG, IN_HEAP, field, noreg /* ltos*/, noreg, noreg);
3554     if (!is_static && rc == may_rewrite) {
3555       patch_bytecode(Bytecodes::_fast_lputfield, bc, rbx, true, byte_no);
3556     }
3557     __ jmp(Done);
3558   }
3559 #else
3560   {
3561     Label notVolatileLong;
3562     __ testl(rdx, rdx);
3563     __ jcc(Assembler::zero, notVolatileLong);
3564 
3565     __ pop(ltos);  // overwrites rdx, do this after testing volatile.
3566     if (!is_static) pop_and_check_object(obj);
3567 
3568     // Replace with real volatile test
3569     __ access_store_at(T_LONG, IN_HEAP | MO_RELAXED, field, noreg /* ltos */, noreg, noreg);
3570     // volatile_barrier();
3571     volatile_barrier(Assembler::Membar_mask_bits(Assembler::StoreLoad |
3572                                                  Assembler::StoreStore));
3573     // Don't rewrite volatile version
3574     __ jmp(notVolatile);
3575 
3576     __ bind(notVolatileLong);
3577 
3578     __ pop(ltos);  // overwrites rdx
3579     if (!is_static) pop_and_check_object(obj);
3580     __ access_store_at(T_LONG, IN_HEAP, field, noreg /* ltos */, noreg, noreg);
3581     // Don't rewrite to _fast_lputfield for potential volatile case.
3582     __ jmp(notVolatile);
3583   }
3584 #endif // _LP64
3585 
3586   __ bind(notLong);
3587   __ cmpl(flags, ftos);
3588   __ jcc(Assembler::notEqual, notFloat);
3589 
3590   // ftos
3591   {
3592     __ pop(ftos);
3593     if (!is_static) pop_and_check_object(obj);
3594     __ access_store_at(T_FLOAT, IN_HEAP, field, noreg /* ftos */, noreg, noreg);
3595     if (!is_static && rc == may_rewrite) {
3596       patch_bytecode(Bytecodes::_fast_fputfield, bc, rbx, true, byte_no);
3597     }
3598     __ jmp(Done);
3599   }
3600 
3601   __ bind(notFloat);
3602 #ifdef ASSERT
3603   __ cmpl(flags, dtos);
3604   __ jcc(Assembler::notEqual, notDouble);
3605 #endif
3606 
3607   // dtos
3608   {
3609     __ pop(dtos);
3610     if (!is_static) pop_and_check_object(obj);
3611     __ access_store_at(T_DOUBLE, IN_HEAP, field, noreg /* dtos */, noreg, noreg);
3612     if (!is_static && rc == may_rewrite) {
3613       patch_bytecode(Bytecodes::_fast_dputfield, bc, rbx, true, byte_no);
3614     }
3615   }
3616 
3617 #ifdef ASSERT
3618   __ jmp(Done);
3619 
3620   __ bind(notDouble);
3621   __ stop("Bad state");
3622 #endif
3623 
3624   __ bind(Done);
3625 
3626   __ shrl(flags2, ConstantPoolCacheEntry::is_volatile_shift);
3627   __ andl(flags2, 0x1);
3628 
3629   // Check for volatile store
3630   __ testl(flags2, flags2);
3631   __ jcc(Assembler::zero, notVolatile);
3632   volatile_barrier(Assembler::Membar_mask_bits(Assembler::StoreLoad |
3633                                                Assembler::StoreStore));
3634   __ bind(notVolatile);
3635 }
3636 
3637 void TemplateTable::putfield(int byte_no) {
3638   putfield_or_static(byte_no, false);
3639 }
3640 
3641 void TemplateTable::nofast_putfield(int byte_no) {
3642   putfield_or_static(byte_no, false, may_not_rewrite);
3643 }
3644 
3645 void TemplateTable::putstatic(int byte_no) {
3646   putfield_or_static(byte_no, true);
3647 }
3648 
3649 void TemplateTable::jvmti_post_fast_field_mod() {
3650 
3651   const Register scratch = LP64_ONLY(c_rarg3) NOT_LP64(rcx);
3652 
3653   if (JvmtiExport::can_post_field_modification()) {
3654     // Check to see if a field modification watch has been set before
3655     // we take the time to call into the VM.
3656     Label L2;
3657     __ mov32(scratch, ExternalAddress((address)JvmtiExport::get_field_modification_count_addr()));
3658     __ testl(scratch, scratch);
3659     __ jcc(Assembler::zero, L2);
3660     __ pop_ptr(rbx);                  // copy the object pointer from tos
3661     __ verify_oop(rbx);
3662     __ push_ptr(rbx);                 // put the object pointer back on tos
3663     // Save tos values before call_VM() clobbers them. Since we have
3664     // to do it for every data type, we use the saved values as the
3665     // jvalue object.
3666     switch (bytecode()) {          // load values into the jvalue object
3667     case Bytecodes::_fast_qputfield: //fall through
3668     case Bytecodes::_fast_aputfield: __ push_ptr(rax); break;
3669     case Bytecodes::_fast_bputfield: // fall through
3670     case Bytecodes::_fast_zputfield: // fall through
3671     case Bytecodes::_fast_sputfield: // fall through
3672     case Bytecodes::_fast_cputfield: // fall through
3673     case Bytecodes::_fast_iputfield: __ push_i(rax); break;
3674     case Bytecodes::_fast_dputfield: __ push(dtos); break;
3675     case Bytecodes::_fast_fputfield: __ push(ftos); break;
3676     case Bytecodes::_fast_lputfield: __ push_l(rax); break;
3677 
3678     default:
3679       ShouldNotReachHere();
3680     }
3681     __ mov(scratch, rsp);             // points to jvalue on the stack
3682     // access constant pool cache entry
3683     LP64_ONLY(__ get_cache_entry_pointer_at_bcp(c_rarg2, rax, 1));
3684     NOT_LP64(__ get_cache_entry_pointer_at_bcp(rax, rdx, 1));
3685     __ verify_oop(rbx);
3686     // rbx: object pointer copied above
3687     // c_rarg2: cache entry pointer
3688     // c_rarg3: jvalue object on the stack
3689     LP64_ONLY(__ call_VM(noreg, CAST_FROM_FN_PTR(address, InterpreterRuntime::post_field_modification), rbx, c_rarg2, c_rarg3));
3690     NOT_LP64(__ call_VM(noreg, CAST_FROM_FN_PTR(address, InterpreterRuntime::post_field_modification), rbx, rax, rcx));
3691 
3692     switch (bytecode()) {             // restore tos values
3693     case Bytecodes::_fast_qputfield: // fall through
3694     case Bytecodes::_fast_aputfield: __ pop_ptr(rax); break;
3695     case Bytecodes::_fast_bputfield: // fall through
3696     case Bytecodes::_fast_zputfield: // fall through
3697     case Bytecodes::_fast_sputfield: // fall through
3698     case Bytecodes::_fast_cputfield: // fall through
3699     case Bytecodes::_fast_iputfield: __ pop_i(rax); break;
3700     case Bytecodes::_fast_dputfield: __ pop(dtos); break;
3701     case Bytecodes::_fast_fputfield: __ pop(ftos); break;
3702     case Bytecodes::_fast_lputfield: __ pop_l(rax); break;
3703     default: break;
3704     }
3705     __ bind(L2);
3706   }
3707 }
3708 
3709 void TemplateTable::fast_storefield(TosState state) {
3710   transition(state, vtos);
3711 
3712   ByteSize base = ConstantPoolCache::base_offset();
3713 
3714   jvmti_post_fast_field_mod();
3715 
3716   // access constant pool cache
3717   __ get_cache_and_index_at_bcp(rcx, rbx, 1);
3718 
3719   // test for volatile with rdx but rdx is tos register for lputfield.
3720   __ movl(rdx, Address(rcx, rbx, Address::times_ptr,
3721                        in_bytes(base +
3722                                 ConstantPoolCacheEntry::flags_offset())));
3723 
3724   // replace index with field offset from cache entry
3725   __ movptr(rbx, Address(rcx, rbx, Address::times_ptr,
3726                          in_bytes(base + ConstantPoolCacheEntry::f2_offset())));
3727 
3728   // [jk] not needed currently
3729   // volatile_barrier(Assembler::Membar_mask_bits(Assembler::LoadStore |
3730   //                                              Assembler::StoreStore));
3731 
3732   if (bytecode() == Bytecodes::_fast_qputfield) {
3733     __ movl(rscratch2, rdx);
3734   }
3735 
3736   Label notVolatile;
3737   __ shrl(rdx, ConstantPoolCacheEntry::is_volatile_shift);
3738   __ andl(rdx, 0x1);
3739 
3740   // Get object from stack
3741   pop_and_check_object(rcx);
3742 
3743   // field address
3744   const Address field(rcx, rbx, Address::times_1);
3745 
3746   // access field
3747   switch (bytecode()) {
3748   case Bytecodes::_fast_qputfield:
3749     {
3750       Label isFlattened, notBuffered, done;
3751       __ null_check(rax);
3752       __ test_field_is_flattened(rscratch2, rscratch1, isFlattened);
3753       // No Flattened case
3754       if (ValueTypesBufferMaxMemory > 0) {
3755         __ test_value_is_not_buffered(rax, rscratch1, notBuffered);
3756         call_VM(noreg, CAST_FROM_FN_PTR(address, InterpreterRuntime::write_heap_copy),
3757                     rax, rbx, rcx);
3758         __ jmp(done);
3759         __ bind(notBuffered);
3760       }
3761       do_oop_store(_masm, field, rax);
3762       __ jmp(done);
3763       __ bind(isFlattened);
3764       call_VM(noreg, CAST_FROM_FN_PTR(address, InterpreterRuntime::write_flattened_value),
3765           rax, rbx, rcx);
3766       __ bind(done);
3767     }
3768     break;
3769   case Bytecodes::_fast_aputfield:
3770     {
3771       Label notBuffered, done;
3772       if (ValueTypesBufferMaxMemory > 0) {
3773         __ test_value_is_not_buffered(rax, rscratch1, notBuffered);
3774         call_VM(noreg, CAST_FROM_FN_PTR(address, InterpreterRuntime::write_heap_copy),
3775             rax, rbx, rcx);
3776         __ jmp(done);
3777         __ bind(notBuffered);
3778       }
3779       do_oop_store(_masm, field, rax);
3780       __ bind(done);
3781     }
3782     break;
3783   case Bytecodes::_fast_lputfield:
3784 #ifdef _LP64
3785     __ access_store_at(T_LONG, IN_HEAP, field, noreg /* ltos */, noreg, noreg);
3786 #else
3787   __ stop("should not be rewritten");
3788 #endif
3789     break;
3790   case Bytecodes::_fast_iputfield:
3791     __ access_store_at(T_INT, IN_HEAP, field, rax, noreg, noreg);
3792     break;
3793   case Bytecodes::_fast_zputfield:
3794     __ access_store_at(T_BOOLEAN, IN_HEAP, field, rax, noreg, noreg);
3795     break;
3796   case Bytecodes::_fast_bputfield:
3797     __ access_store_at(T_BYTE, IN_HEAP, field, rax, noreg, noreg);
3798     break;
3799   case Bytecodes::_fast_sputfield:
3800     __ access_store_at(T_SHORT, IN_HEAP, field, rax, noreg, noreg);
3801     break;
3802   case Bytecodes::_fast_cputfield:
3803     __ access_store_at(T_CHAR, IN_HEAP, field, rax, noreg, noreg);
3804     break;
3805   case Bytecodes::_fast_fputfield:
3806     __ access_store_at(T_FLOAT, IN_HEAP, field, noreg /* ftos*/, noreg, noreg);
3807     break;
3808   case Bytecodes::_fast_dputfield:
3809     __ access_store_at(T_DOUBLE, IN_HEAP, field, noreg /* dtos*/, noreg, noreg);
3810     break;
3811   default:
3812     ShouldNotReachHere();
3813   }
3814 
3815   // Check for volatile store
3816   __ testl(rdx, rdx);
3817   __ jcc(Assembler::zero, notVolatile);
3818   volatile_barrier(Assembler::Membar_mask_bits(Assembler::StoreLoad |
3819                                                Assembler::StoreStore));
3820   __ bind(notVolatile);
3821 }
3822 
3823 void TemplateTable::fast_accessfield(TosState state) {
3824   transition(atos, state);
3825 
3826   // Do the JVMTI work here to avoid disturbing the register state below
3827   if (JvmtiExport::can_post_field_access()) {
3828     // Check to see if a field access watch has been set before we
3829     // take the time to call into the VM.
3830     Label L1;
3831     __ mov32(rcx, ExternalAddress((address) JvmtiExport::get_field_access_count_addr()));
3832     __ testl(rcx, rcx);
3833     __ jcc(Assembler::zero, L1);
3834     // access constant pool cache entry
3835     LP64_ONLY(__ get_cache_entry_pointer_at_bcp(c_rarg2, rcx, 1));
3836     NOT_LP64(__ get_cache_entry_pointer_at_bcp(rcx, rdx, 1));
3837     __ verify_oop(rax);
3838     __ push_ptr(rax);  // save object pointer before call_VM() clobbers it
3839     LP64_ONLY(__ mov(c_rarg1, rax));
3840     // c_rarg1: object pointer copied above
3841     // c_rarg2: cache entry pointer
3842     LP64_ONLY(__ call_VM(noreg, CAST_FROM_FN_PTR(address, InterpreterRuntime::post_field_access), c_rarg1, c_rarg2));
3843     NOT_LP64(__ call_VM(noreg, CAST_FROM_FN_PTR(address, InterpreterRuntime::post_field_access), rax, rcx));
3844     __ pop_ptr(rax); // restore object pointer
3845     __ bind(L1);
3846   }
3847 
3848   // access constant pool cache
3849   __ get_cache_and_index_at_bcp(rcx, rbx, 1);
3850   // replace index with field offset from cache entry
3851   // [jk] not needed currently
3852   // if (os::is_MP()) {
3853   //   __ movl(rdx, Address(rcx, rbx, Address::times_8,
3854   //                        in_bytes(ConstantPoolCache::base_offset() +
3855   //                                 ConstantPoolCacheEntry::flags_offset())));
3856   //   __ shrl(rdx, ConstantPoolCacheEntry::is_volatile_shift);
3857   //   __ andl(rdx, 0x1);
3858   // }
3859   __ movptr(rdx, Address(rcx, rbx, Address::times_ptr,
3860                          in_bytes(ConstantPoolCache::base_offset() +
3861                                   ConstantPoolCacheEntry::f2_offset())));
3862 
3863   // rax: object
3864   __ verify_oop(rax);
3865   __ null_check(rax);
3866   Address field(rax, rdx, Address::times_1);
3867 
3868   // access field
3869   switch (bytecode()) {
3870   case Bytecodes::_fast_qgetfield:
3871     {
3872       Label isFlattened, nonnull, Done;
3873       __ movptr(rscratch1, Address(rcx, rbx, Address::times_ptr,
3874                                    in_bytes(ConstantPoolCache::base_offset() +
3875                                             ConstantPoolCacheEntry::flags_offset())));
3876       __ test_field_is_flattened(rscratch1, rscratch2, isFlattened);
3877         // Non-flattened field case
3878         __ movptr(rscratch1, rax);
3879         __ load_heap_oop(rax, field);
3880         __ testptr(rax, rax);
3881         __ jcc(Assembler::notZero, nonnull);
3882           __ movptr(rax, rscratch1);
3883           __ movl(rcx, Address(rcx, rbx, Address::times_ptr,
3884                              in_bytes(ConstantPoolCache::base_offset() +
3885                                       ConstantPoolCacheEntry::flags_offset())));
3886           __ andl(rcx, ConstantPoolCacheEntry::field_index_mask);
3887           __ call_VM(rax, CAST_FROM_FN_PTR(address, InterpreterRuntime::uninitialized_instance_value_field),
3888                      rax, rcx);
3889         __ bind(nonnull);
3890         __ verify_oop(rax);
3891         __ jmp(Done);
3892       __ bind(isFlattened);
3893         __ movl(rdx, Address(rcx, rbx, Address::times_ptr,
3894                            in_bytes(ConstantPoolCache::base_offset() +
3895                                     ConstantPoolCacheEntry::flags_offset())));
3896         __ andl(rdx, ConstantPoolCacheEntry::field_index_mask);
3897         __ movptr(rcx, Address(rcx, rbx, Address::times_ptr,
3898                                      in_bytes(ConstantPoolCache::base_offset() +
3899                                               ConstantPoolCacheEntry::f1_offset())));
3900         call_VM(rax, CAST_FROM_FN_PTR(address, InterpreterRuntime::read_flattened_field),
3901                 rax, rdx, rcx);
3902         __ verify_oop(rax);
3903       __ bind(Done);
3904     }
3905     break;
3906   case Bytecodes::_fast_agetfield:
3907     do_oop_load(_masm, field, rax);
3908     __ verify_oop(rax);
3909     break;
3910   case Bytecodes::_fast_lgetfield:
3911 #ifdef _LP64
3912     __ access_load_at(T_LONG, IN_HEAP, noreg /* ltos */, field, noreg, noreg);
3913 #else
3914   __ stop("should not be rewritten");
3915 #endif
3916     break;
3917   case Bytecodes::_fast_igetfield:
3918     __ access_load_at(T_INT, IN_HEAP, rax, field, noreg, noreg);
3919     break;
3920   case Bytecodes::_fast_bgetfield:
3921     __ access_load_at(T_BYTE, IN_HEAP, rax, field, noreg, noreg);
3922     break;
3923   case Bytecodes::_fast_sgetfield:
3924     __ access_load_at(T_SHORT, IN_HEAP, rax, field, noreg, noreg);
3925     break;
3926   case Bytecodes::_fast_cgetfield:
3927     __ access_load_at(T_CHAR, IN_HEAP, rax, field, noreg, noreg);
3928     break;
3929   case Bytecodes::_fast_fgetfield:
3930     __ access_load_at(T_FLOAT, IN_HEAP, noreg /* ftos */, field, noreg, noreg);
3931     break;
3932   case Bytecodes::_fast_dgetfield:
3933     __ access_load_at(T_DOUBLE, IN_HEAP, noreg /* dtos */, field, noreg, noreg);
3934     break;
3935   default:
3936     ShouldNotReachHere();
3937   }
3938   // [jk] not needed currently
3939   // if (os::is_MP()) {
3940   //   Label notVolatile;
3941   //   __ testl(rdx, rdx);
3942   //   __ jcc(Assembler::zero, notVolatile);
3943   //   __ membar(Assembler::LoadLoad);
3944   //   __ bind(notVolatile);
3945   //};
3946 }
3947 
3948 void TemplateTable::fast_xaccess(TosState state) {
3949   transition(vtos, state);
3950 
3951   // get receiver
3952   __ movptr(rax, aaddress(0));
3953   // access constant pool cache
3954   __ get_cache_and_index_at_bcp(rcx, rdx, 2);
3955   __ movptr(rbx,
3956             Address(rcx, rdx, Address::times_ptr,
3957                     in_bytes(ConstantPoolCache::base_offset() +
3958                              ConstantPoolCacheEntry::f2_offset())));
3959   // make sure exception is reported in correct bcp range (getfield is
3960   // next instruction)
3961   __ increment(rbcp);
3962   __ null_check(rax);
3963   const Address field = Address(rax, rbx, Address::times_1, 0*wordSize);
3964   switch (state) {
3965   case itos:
3966     __ access_load_at(T_INT, IN_HEAP, rax, field, noreg, noreg);
3967     break;
3968   case atos:
3969     do_oop_load(_masm, field, rax);
3970     __ verify_oop(rax);
3971     break;
3972   case ftos:
3973     __ access_load_at(T_FLOAT, IN_HEAP, noreg /* ftos */, field, noreg, noreg);
3974     break;
3975   default:
3976     ShouldNotReachHere();
3977   }
3978 
3979   // [jk] not needed currently
3980   // if (os::is_MP()) {
3981   //   Label notVolatile;
3982   //   __ movl(rdx, Address(rcx, rdx, Address::times_8,
3983   //                        in_bytes(ConstantPoolCache::base_offset() +
3984   //                                 ConstantPoolCacheEntry::flags_offset())));
3985   //   __ shrl(rdx, ConstantPoolCacheEntry::is_volatile_shift);
3986   //   __ testl(rdx, 0x1);
3987   //   __ jcc(Assembler::zero, notVolatile);
3988   //   __ membar(Assembler::LoadLoad);
3989   //   __ bind(notVolatile);
3990   // }
3991 
3992   __ decrement(rbcp);
3993 }
3994 
3995 //-----------------------------------------------------------------------------
3996 // Calls
3997 
3998 void TemplateTable::count_calls(Register method, Register temp) {
3999   // implemented elsewhere
4000   ShouldNotReachHere();
4001 }
4002 
4003 void TemplateTable::prepare_invoke(int byte_no,
4004                                    Register method,  // linked method (or i-klass)
4005                                    Register index,   // itable index, MethodType, etc.
4006                                    Register recv,    // if caller wants to see it
4007                                    Register flags    // if caller wants to test it
4008                                    ) {
4009   // determine flags
4010   const Bytecodes::Code code = bytecode();
4011   const bool is_invokeinterface  = code == Bytecodes::_invokeinterface;
4012   const bool is_invokedynamic    = code == Bytecodes::_invokedynamic;
4013   const bool is_invokehandle     = code == Bytecodes::_invokehandle;
4014   const bool is_invokevirtual    = code == Bytecodes::_invokevirtual;
4015   const bool is_invokespecial    = code == Bytecodes::_invokespecial;
4016   const bool load_receiver       = (recv  != noreg);
4017   const bool save_flags          = (flags != noreg);
4018   assert(load_receiver == (code != Bytecodes::_invokestatic && code != Bytecodes::_invokedynamic), "");
4019   assert(save_flags    == (is_invokeinterface || is_invokevirtual), "need flags for vfinal");
4020   assert(flags == noreg || flags == rdx, "");
4021   assert(recv  == noreg || recv  == rcx, "");
4022 
4023   // setup registers & access constant pool cache
4024   if (recv  == noreg)  recv  = rcx;
4025   if (flags == noreg)  flags = rdx;
4026   assert_different_registers(method, index, recv, flags);
4027 
4028   // save 'interpreter return address'
4029   __ save_bcp();
4030 
4031   load_invoke_cp_cache_entry(byte_no, method, index, flags, is_invokevirtual, false, is_invokedynamic);
4032 
4033   // maybe push appendix to arguments (just before return address)
4034   if (is_invokedynamic || is_invokehandle) {
4035     Label L_no_push;
4036     __ testl(flags, (1 << ConstantPoolCacheEntry::has_appendix_shift));
4037     __ jcc(Assembler::zero, L_no_push);
4038     // Push the appendix as a trailing parameter.
4039     // This must be done before we get the receiver,
4040     // since the parameter_size includes it.
4041     __ push(rbx);
4042     __ mov(rbx, index);
4043     assert(ConstantPoolCacheEntry::_indy_resolved_references_appendix_offset == 0, "appendix expected at index+0");
4044     __ load_resolved_reference_at_index(index, rbx);
4045     __ pop(rbx);
4046     __ push(index);  // push appendix (MethodType, CallSite, etc.)
4047     __ bind(L_no_push);
4048   }
4049 
4050   // load receiver if needed (after appendix is pushed so parameter size is correct)
4051   // Note: no return address pushed yet
4052   if (load_receiver) {
4053     __ movl(recv, flags);
4054     __ andl(recv, ConstantPoolCacheEntry::parameter_size_mask);
4055     const int no_return_pc_pushed_yet = -1;  // argument slot correction before we push return address
4056     const int receiver_is_at_end      = -1;  // back off one slot to get receiver
4057     Address recv_addr = __ argument_address(recv, no_return_pc_pushed_yet + receiver_is_at_end);
4058     __ movptr(recv, recv_addr);
4059     __ verify_oop(recv);
4060   }
4061 
4062   if (save_flags) {
4063     __ movl(rbcp, flags);
4064   }
4065 
4066   // compute return type
4067   __ shrl(flags, ConstantPoolCacheEntry::tos_state_shift);
4068   // Make sure we don't need to mask flags after the above shift
4069   ConstantPoolCacheEntry::verify_tos_state_shift();
4070   // load return address
4071   {
4072     const address table_addr = (address) Interpreter::invoke_return_entry_table_for(code);
4073     ExternalAddress table(table_addr);
4074     LP64_ONLY(__ lea(rscratch1, table));
4075     LP64_ONLY(__ movptr(flags, Address(rscratch1, flags, Address::times_ptr)));
4076     NOT_LP64(__ movptr(flags, ArrayAddress(table, Address(noreg, flags, Address::times_ptr))));
4077   }
4078 
4079   // push return address
4080   __ push(flags);
4081 
4082   // Restore flags value from the constant pool cache, and restore rsi
4083   // for later null checks.  r13 is the bytecode pointer
4084   if (save_flags) {
4085     __ movl(flags, rbcp);
4086     __ restore_bcp();
4087   }
4088 }
4089 
4090 void TemplateTable::invokevirtual_helper(Register index,
4091                                          Register recv,
4092                                          Register flags) {
4093   // Uses temporary registers rax, rdx
4094   assert_different_registers(index, recv, rax, rdx);
4095   assert(index == rbx, "");
4096   assert(recv  == rcx, "");
4097 
4098   // Test for an invoke of a final method
4099   Label notFinal;
4100   __ movl(rax, flags);
4101   __ andl(rax, (1 << ConstantPoolCacheEntry::is_vfinal_shift));
4102   __ jcc(Assembler::zero, notFinal);
4103 
4104   const Register method = index;  // method must be rbx
4105   assert(method == rbx,
4106          "Method* must be rbx for interpreter calling convention");
4107 
4108   // do the call - the index is actually the method to call
4109   // that is, f2 is a vtable index if !is_vfinal, else f2 is a Method*
4110 
4111   // It's final, need a null check here!
4112   __ null_check(recv);
4113 
4114   // profile this call
4115   __ profile_final_call(rax);
4116   __ profile_arguments_type(rax, method, rbcp, true);
4117 
4118   __ jump_from_interpreted(method, rax);
4119 
4120   __ bind(notFinal);
4121 
4122   // get receiver klass
4123   __ null_check(recv, oopDesc::klass_offset_in_bytes());
4124   __ load_klass(rax, recv);
4125 
4126   // profile this call
4127   __ profile_virtual_call(rax, rlocals, rdx);
4128   // get target Method* & entry point
4129   __ lookup_virtual_method(rax, index, method);
4130   __ profile_called_method(method, rdx, rbcp);
4131 
4132   __ profile_arguments_type(rdx, method, rbcp, true);
4133   __ jump_from_interpreted(method, rdx);
4134 }
4135 
4136 void TemplateTable::invokevirtual(int byte_no) {
4137   transition(vtos, vtos);
4138   assert(byte_no == f2_byte, "use this argument");
4139   prepare_invoke(byte_no,
4140                  rbx,    // method or vtable index
4141                  noreg,  // unused itable index
4142                  rcx, rdx); // recv, flags
4143 
4144   // rbx: index
4145   // rcx: receiver
4146   // rdx: flags
4147 
4148   invokevirtual_helper(rbx, rcx, rdx);
4149 }
4150 
4151 void TemplateTable::invokespecial(int byte_no) {
4152   transition(vtos, vtos);
4153   assert(byte_no == f1_byte, "use this argument");
4154   prepare_invoke(byte_no, rbx, noreg,  // get f1 Method*
4155                  rcx);  // get receiver also for null check
4156   __ verify_oop(rcx);
4157   __ null_check(rcx);
4158   // do the call
4159   __ profile_call(rax);
4160   __ profile_arguments_type(rax, rbx, rbcp, false);
4161   __ jump_from_interpreted(rbx, rax);
4162 }
4163 
4164 void TemplateTable::invokestatic(int byte_no) {
4165   transition(vtos, vtos);
4166   assert(byte_no == f1_byte, "use this argument");
4167   prepare_invoke(byte_no, rbx);  // get f1 Method*
4168   // do the call
4169   __ profile_call(rax);
4170   __ profile_arguments_type(rax, rbx, rbcp, false);
4171   __ jump_from_interpreted(rbx, rax);
4172 }
4173 
4174 
4175 void TemplateTable::fast_invokevfinal(int byte_no) {
4176   transition(vtos, vtos);
4177   assert(byte_no == f2_byte, "use this argument");
4178   __ stop("fast_invokevfinal not used on x86");
4179 }
4180 
4181 
4182 void TemplateTable::invokeinterface(int byte_no) {
4183   transition(vtos, vtos);
4184   assert(byte_no == f1_byte, "use this argument");
4185   prepare_invoke(byte_no, rax, rbx,  // get f1 Klass*, f2 Method*
4186                  rcx, rdx); // recv, flags
4187 
4188   // rax: reference klass (from f1) if interface method
4189   // rbx: method (from f2)
4190   // rcx: receiver
4191   // rdx: flags
4192 
4193   // First check for Object case, then private interface method,
4194   // then regular interface method.
4195 
4196   // Special case of invokeinterface called for virtual method of
4197   // java.lang.Object.  See cpCache.cpp for details.
4198   Label notObjectMethod;
4199   __ movl(rlocals, rdx);
4200   __ andl(rlocals, (1 << ConstantPoolCacheEntry::is_forced_virtual_shift));
4201   __ jcc(Assembler::zero, notObjectMethod);
4202   invokevirtual_helper(rbx, rcx, rdx);
4203   // no return from above
4204   __ bind(notObjectMethod);
4205 
4206   Label no_such_interface; // for receiver subtype check
4207   Register recvKlass; // used for exception processing
4208 
4209   // Check for private method invocation - indicated by vfinal
4210   Label notVFinal;
4211   __ movl(rlocals, rdx);
4212   __ andl(rlocals, (1 << ConstantPoolCacheEntry::is_vfinal_shift));
4213   __ jcc(Assembler::zero, notVFinal);
4214 
4215   // Get receiver klass into rlocals - also a null check
4216   __ null_check(rcx, oopDesc::klass_offset_in_bytes());
4217   __ load_klass(rlocals, rcx);
4218 
4219   Label subtype;
4220   __ check_klass_subtype(rlocals, rax, rbcp, subtype);
4221   // If we get here the typecheck failed
4222   recvKlass = rdx;
4223   __ mov(recvKlass, rlocals); // shuffle receiver class for exception use
4224   __ jmp(no_such_interface);
4225 
4226   __ bind(subtype);
4227 
4228   // do the call - rbx is actually the method to call
4229 
4230   __ profile_final_call(rdx);
4231   __ profile_arguments_type(rdx, rbx, rbcp, true);
4232 
4233   __ jump_from_interpreted(rbx, rdx);
4234   // no return from above
4235   __ bind(notVFinal);
4236 
4237   // Get receiver klass into rdx - also a null check
4238   __ restore_locals();  // restore r14
4239   __ null_check(rcx, oopDesc::klass_offset_in_bytes());
4240   __ load_klass(rdx, rcx);
4241 
4242   Label no_such_method;
4243 
4244   // Preserve method for throw_AbstractMethodErrorVerbose.
4245   __ mov(rcx, rbx);
4246   // Receiver subtype check against REFC.
4247   // Superklass in rax. Subklass in rdx. Blows rcx, rdi.
4248   __ lookup_interface_method(// inputs: rec. class, interface, itable index
4249                              rdx, rax, noreg,
4250                              // outputs: scan temp. reg, scan temp. reg
4251                              rbcp, rlocals,
4252                              no_such_interface,
4253                              /*return_method=*/false);
4254 
4255   // profile this call
4256   __ restore_bcp(); // rbcp was destroyed by receiver type check
4257   __ profile_virtual_call(rdx, rbcp, rlocals);
4258 
4259   // Get declaring interface class from method, and itable index
4260   __ movptr(rax, Address(rbx, Method::const_offset()));
4261   __ movptr(rax, Address(rax, ConstMethod::constants_offset()));
4262   __ movptr(rax, Address(rax, ConstantPool::pool_holder_offset_in_bytes()));
4263   __ movl(rbx, Address(rbx, Method::itable_index_offset()));
4264   __ subl(rbx, Method::itable_index_max);
4265   __ negl(rbx);
4266 
4267   // Preserve recvKlass for throw_AbstractMethodErrorVerbose.
4268   __ mov(rlocals, rdx);
4269   __ lookup_interface_method(// inputs: rec. class, interface, itable index
4270                              rlocals, rax, rbx,
4271                              // outputs: method, scan temp. reg
4272                              rbx, rbcp,
4273                              no_such_interface);
4274 
4275   // rbx: Method* to call
4276   // rcx: receiver
4277   // Check for abstract method error
4278   // Note: This should be done more efficiently via a throw_abstract_method_error
4279   //       interpreter entry point and a conditional jump to it in case of a null
4280   //       method.
4281   __ testptr(rbx, rbx);
4282   __ jcc(Assembler::zero, no_such_method);
4283 
4284   __ profile_called_method(rbx, rbcp, rdx);
4285   __ profile_arguments_type(rdx, rbx, rbcp, true);
4286 
4287   // do the call
4288   // rcx: receiver
4289   // rbx,: Method*
4290   __ jump_from_interpreted(rbx, rdx);
4291   __ should_not_reach_here();
4292 
4293   // exception handling code follows...
4294   // note: must restore interpreter registers to canonical
4295   //       state for exception handling to work correctly!
4296 
4297   __ bind(no_such_method);
4298   // throw exception
4299   __ pop(rbx);           // pop return address (pushed by prepare_invoke)
4300   __ restore_bcp();      // rbcp must be correct for exception handler   (was destroyed)
4301   __ restore_locals();   // make sure locals pointer is correct as well (was destroyed)
4302   // Pass arguments for generating a verbose error message.
4303 #ifdef _LP64
4304   recvKlass = c_rarg1;
4305   Register method    = c_rarg2;
4306   if (recvKlass != rdx) { __ movq(recvKlass, rdx); }
4307   if (method != rcx)    { __ movq(method, rcx);    }
4308 #else
4309   recvKlass = rdx;
4310   Register method    = rcx;
4311 #endif
4312   __ call_VM(noreg, CAST_FROM_FN_PTR(address, InterpreterRuntime::throw_AbstractMethodErrorVerbose),
4313              recvKlass, method);
4314   // The call_VM checks for exception, so we should never return here.
4315   __ should_not_reach_here();
4316 
4317   __ bind(no_such_interface);
4318   // throw exception
4319   __ pop(rbx);           // pop return address (pushed by prepare_invoke)
4320   __ restore_bcp();      // rbcp must be correct for exception handler   (was destroyed)
4321   __ restore_locals();   // make sure locals pointer is correct as well (was destroyed)
4322   // Pass arguments for generating a verbose error message.
4323   LP64_ONLY( if (recvKlass != rdx) { __ movq(recvKlass, rdx); } )
4324   __ call_VM(noreg, CAST_FROM_FN_PTR(address, InterpreterRuntime::throw_IncompatibleClassChangeErrorVerbose),
4325              recvKlass, rax);
4326   // the call_VM checks for exception, so we should never return here.
4327   __ should_not_reach_here();
4328 }
4329 
4330 void TemplateTable::invokehandle(int byte_no) {
4331   transition(vtos, vtos);
4332   assert(byte_no == f1_byte, "use this argument");
4333   const Register rbx_method = rbx;
4334   const Register rax_mtype  = rax;
4335   const Register rcx_recv   = rcx;
4336   const Register rdx_flags  = rdx;
4337 
4338   prepare_invoke(byte_no, rbx_method, rax_mtype, rcx_recv);
4339   __ verify_method_ptr(rbx_method);
4340   __ verify_oop(rcx_recv);
4341   __ null_check(rcx_recv);
4342 
4343   // rax: MethodType object (from cpool->resolved_references[f1], if necessary)
4344   // rbx: MH.invokeExact_MT method (from f2)
4345 
4346   // Note:  rax_mtype is already pushed (if necessary) by prepare_invoke
4347 
4348   // FIXME: profile the LambdaForm also
4349   __ profile_final_call(rax);
4350   __ profile_arguments_type(rdx, rbx_method, rbcp, true);
4351 
4352   __ jump_from_interpreted(rbx_method, rdx);
4353 }
4354 
4355 void TemplateTable::invokedynamic(int byte_no) {
4356   transition(vtos, vtos);
4357   assert(byte_no == f1_byte, "use this argument");
4358 
4359   const Register rbx_method   = rbx;
4360   const Register rax_callsite = rax;
4361 
4362   prepare_invoke(byte_no, rbx_method, rax_callsite);
4363 
4364   // rax: CallSite object (from cpool->resolved_references[f1])
4365   // rbx: MH.linkToCallSite method (from f2)
4366 
4367   // Note:  rax_callsite is already pushed by prepare_invoke
4368 
4369   // %%% should make a type profile for any invokedynamic that takes a ref argument
4370   // profile this call
4371   __ profile_call(rbcp);
4372   __ profile_arguments_type(rdx, rbx_method, rbcp, false);
4373 
4374   __ verify_oop(rax_callsite);
4375 
4376   __ jump_from_interpreted(rbx_method, rdx);
4377 }
4378 
4379 //-----------------------------------------------------------------------------
4380 // Allocation
4381 
4382 void TemplateTable::_new() {
4383   transition(vtos, atos);
4384   __ get_unsigned_2_byte_index_at_bcp(rdx, 1);
4385   Label slow_case;
4386   Label slow_case_no_pop;
4387   Label done;
4388   Label initialize_header;
4389   Label initialize_object;  // including clearing the fields
4390 
4391   __ get_cpool_and_tags(rcx, rax);
4392 
4393   // Make sure the class we're about to instantiate has been resolved.
4394   // This is done before loading InstanceKlass to be consistent with the order
4395   // how Constant Pool is updated (see ConstantPool::klass_at_put)
4396   const int tags_offset = Array<u1>::base_offset_in_bytes();
4397   __ cmpb(Address(rax, rdx, Address::times_1, tags_offset), JVM_CONSTANT_Class);
4398   __ jcc(Assembler::notEqual, slow_case_no_pop);
4399 
4400   // get InstanceKlass
4401   __ load_resolved_klass_at_index(rcx, rdx, rcx);
4402   __ push(rcx);  // save the contexts of klass for initializing the header
4403 
4404   // make sure klass is initialized & doesn't have finalizer
4405   // make sure klass is fully initialized
4406   __ cmpb(Address(rcx, InstanceKlass::init_state_offset()), InstanceKlass::fully_initialized);
4407   __ jcc(Assembler::notEqual, slow_case);
4408 
4409   // get instance_size in InstanceKlass (scaled to a count of bytes)
4410   __ movl(rdx, Address(rcx, Klass::layout_helper_offset()));
4411   // test to see if it has a finalizer or is malformed in some way
4412   __ testl(rdx, Klass::_lh_instance_slow_path_bit);
4413   __ jcc(Assembler::notZero, slow_case);
4414 
4415   // Allocate the instance:
4416   //  If TLAB is enabled:
4417   //    Try to allocate in the TLAB.
4418   //    If fails, go to the slow path.
4419   //  Else If inline contiguous allocations are enabled:
4420   //    Try to allocate in eden.
4421   //    If fails due to heap end, go to slow path.
4422   //
4423   //  If TLAB is enabled OR inline contiguous is enabled:
4424   //    Initialize the allocation.
4425   //    Exit.
4426   //
4427   //  Go to slow path.
4428 
4429   const bool allow_shared_alloc =
4430     Universe::heap()->supports_inline_contig_alloc();
4431 
4432   const Register thread = LP64_ONLY(r15_thread) NOT_LP64(rcx);
4433 #ifndef _LP64
4434   if (UseTLAB || allow_shared_alloc) {
4435     __ get_thread(thread);
4436   }
4437 #endif // _LP64
4438 
4439   if (UseTLAB) {
4440     __ tlab_allocate(thread, rax, rdx, 0, rcx, rbx, slow_case);
4441     if (ZeroTLAB) {
4442       // the fields have been already cleared
4443       __ jmp(initialize_header);
4444     } else {
4445       // initialize both the header and fields
4446       __ jmp(initialize_object);
4447     }
4448   } else {
4449     // Allocation in the shared Eden, if allowed.
4450     //
4451     // rdx: instance size in bytes
4452     __ eden_allocate(thread, rax, rdx, 0, rbx, slow_case);
4453   }
4454 
4455   // If UseTLAB or allow_shared_alloc are true, the object is created above and
4456   // there is an initialize need. Otherwise, skip and go to the slow path.
4457   if (UseTLAB || allow_shared_alloc) {
4458     // The object is initialized before the header.  If the object size is
4459     // zero, go directly to the header initialization.
4460     __ bind(initialize_object);
4461     __ decrement(rdx, sizeof(oopDesc));
4462     __ jcc(Assembler::zero, initialize_header);
4463 
4464     // Initialize topmost object field, divide rdx by 8, check if odd and
4465     // test if zero.
4466     __ xorl(rcx, rcx);    // use zero reg to clear memory (shorter code)
4467     __ shrl(rdx, LogBytesPerLong); // divide by 2*oopSize and set carry flag if odd
4468 
4469     // rdx must have been multiple of 8
4470 #ifdef ASSERT
4471     // make sure rdx was multiple of 8
4472     Label L;
4473     // Ignore partial flag stall after shrl() since it is debug VM
4474     __ jcc(Assembler::carryClear, L);
4475     __ stop("object size is not multiple of 2 - adjust this code");
4476     __ bind(L);
4477     // rdx must be > 0, no extra check needed here
4478 #endif
4479 
4480     // initialize remaining object fields: rdx was a multiple of 8
4481     { Label loop;
4482     __ bind(loop);
4483     __ movptr(Address(rax, rdx, Address::times_8, sizeof(oopDesc) - 1*oopSize), rcx);
4484     NOT_LP64(__ movptr(Address(rax, rdx, Address::times_8, sizeof(oopDesc) - 2*oopSize), rcx));
4485     __ decrement(rdx);
4486     __ jcc(Assembler::notZero, loop);
4487     }
4488 
4489     // initialize object header only.
4490     __ bind(initialize_header);
4491     if (UseBiasedLocking) {
4492       __ pop(rcx);   // get saved klass back in the register.
4493       __ movptr(rbx, Address(rcx, Klass::prototype_header_offset()));
4494       __ movptr(Address(rax, oopDesc::mark_offset_in_bytes ()), rbx);
4495     } else {
4496       __ movptr(Address(rax, oopDesc::mark_offset_in_bytes ()),
4497                 (intptr_t)markOopDesc::prototype()); // header
4498       __ pop(rcx);   // get saved klass back in the register.
4499     }
4500 #ifdef _LP64
4501     __ xorl(rsi, rsi); // use zero reg to clear memory (shorter code)
4502     __ store_klass_gap(rax, rsi);  // zero klass gap for compressed oops
4503 #endif
4504     __ store_klass(rax, rcx);  // klass
4505 
4506     {
4507       SkipIfEqual skip_if(_masm, &DTraceAllocProbes, 0);
4508       // Trigger dtrace event for fastpath
4509       __ push(atos);
4510       __ call_VM_leaf(
4511            CAST_FROM_FN_PTR(address, SharedRuntime::dtrace_object_alloc), rax);
4512       __ pop(atos);
4513     }
4514 
4515     __ jmp(done);
4516   }
4517 
4518   // slow case
4519   __ bind(slow_case);
4520   __ pop(rcx);   // restore stack pointer to what it was when we came in.
4521   __ bind(slow_case_no_pop);
4522 
4523   Register rarg1 = LP64_ONLY(c_rarg1) NOT_LP64(rax);
4524   Register rarg2 = LP64_ONLY(c_rarg2) NOT_LP64(rdx);
4525 
4526   __ get_constant_pool(rarg1);
4527   __ get_unsigned_2_byte_index_at_bcp(rarg2, 1);
4528   call_VM(rax, CAST_FROM_FN_PTR(address, InterpreterRuntime::_new), rarg1, rarg2);
4529    __ verify_oop(rax);
4530 
4531   // continue
4532   __ bind(done);
4533 }
4534 
4535 void TemplateTable::defaultvalue() {
4536   transition(vtos, atos);
4537 
4538   Register rarg1 = LP64_ONLY(c_rarg1) NOT_LP64(rcx);
4539   Register rarg2 = LP64_ONLY(c_rarg2) NOT_LP64(rdx);
4540 
4541   __ get_unsigned_2_byte_index_at_bcp(rarg2, 1);
4542   __ get_constant_pool(rarg1);
4543 
4544   call_VM(rax, CAST_FROM_FN_PTR(address, InterpreterRuntime::defaultvalue),
4545       rarg1, rarg2);
4546   __ verify_oop(rax);
4547 }
4548 
4549 void TemplateTable::newarray() {
4550   transition(itos, atos);
4551   Register rarg1 = LP64_ONLY(c_rarg1) NOT_LP64(rdx);
4552   __ load_unsigned_byte(rarg1, at_bcp(1));
4553   call_VM(rax, CAST_FROM_FN_PTR(address, InterpreterRuntime::newarray),
4554           rarg1, rax);
4555 }
4556 
4557 void TemplateTable::anewarray() {
4558   transition(itos, atos);
4559 
4560   Register rarg1 = LP64_ONLY(c_rarg1) NOT_LP64(rcx);
4561   Register rarg2 = LP64_ONLY(c_rarg2) NOT_LP64(rdx);
4562 
4563   __ get_unsigned_2_byte_index_at_bcp(rarg2, 1);
4564   __ get_constant_pool(rarg1);
4565   call_VM(rax, CAST_FROM_FN_PTR(address, InterpreterRuntime::anewarray),
4566           rarg1, rarg2, rax);
4567 }
4568 
4569 void TemplateTable::arraylength() {
4570   transition(atos, itos);
4571   __ null_check(rax, arrayOopDesc::length_offset_in_bytes());
4572   __ movl(rax, Address(rax, arrayOopDesc::length_offset_in_bytes()));
4573 }
4574 
4575 void TemplateTable::checkcast() {
4576   transition(atos, atos);
4577   Label done, is_null, ok_is_subtype, quicked, resolved;
4578   __ testptr(rax, rax); // object is in rax
4579   __ jcc(Assembler::zero, is_null);
4580 
4581   // Get cpool & tags index
4582   __ get_cpool_and_tags(rcx, rdx); // rcx=cpool, rdx=tags array
4583   __ get_unsigned_2_byte_index_at_bcp(rbx, 1); // rbx=index
4584   // See if bytecode has already been quicked
4585   __ cmpb(Address(rdx, rbx,
4586                   Address::times_1,
4587                   Array<u1>::base_offset_in_bytes()),
4588           JVM_CONSTANT_Class);
4589   __ jcc(Assembler::equal, quicked);
4590   __ push(atos); // save receiver for result, and for GC
4591   call_VM(noreg, CAST_FROM_FN_PTR(address, InterpreterRuntime::quicken_io_cc));
4592 
4593   // vm_result_2 has metadata result
4594 #ifndef _LP64
4595   // borrow rdi from locals
4596   __ get_thread(rdi);
4597   __ get_vm_result_2(rax, rdi);
4598   __ restore_locals();
4599 #else
4600   __ get_vm_result_2(rax, r15_thread);
4601 #endif
4602 
4603   __ pop_ptr(rdx); // restore receiver
4604   __ jmpb(resolved);
4605 
4606   // Get superklass in rax and subklass in rbx
4607   __ bind(quicked);
4608   __ mov(rdx, rax); // Save object in rdx; rax needed for subtype check
4609   __ load_resolved_klass_at_index(rcx, rbx, rax);
4610 
4611   __ bind(resolved);
4612   __ load_klass(rbx, rdx);
4613 
4614   // Generate subtype check.  Blows rcx, rdi.  Object in rdx.
4615   // Superklass in rax.  Subklass in rbx.
4616   __ gen_subtype_check(rbx, ok_is_subtype);
4617 
4618   // Come here on failure
4619   __ push_ptr(rdx);
4620   // object is at TOS
4621   __ jump(ExternalAddress(Interpreter::_throw_ClassCastException_entry));
4622 
4623   // Come here on success
4624   __ bind(ok_is_subtype);
4625   __ mov(rax, rdx); // Restore object in rdx
4626 
4627   // Collect counts on whether this check-cast sees NULLs a lot or not.
4628   if (ProfileInterpreter) {
4629     __ jmp(done);
4630     __ bind(is_null);
4631     __ profile_null_seen(rcx);
4632   } else {
4633     __ bind(is_null);   // same as 'done'
4634   }
4635   __ bind(done);
4636 }
4637 
4638 void TemplateTable::instanceof() {
4639   transition(atos, itos);
4640   Label done, is_null, ok_is_subtype, quicked, resolved;
4641   __ testptr(rax, rax);
4642   __ jcc(Assembler::zero, is_null);
4643 
4644   // Get cpool & tags index
4645   __ get_cpool_and_tags(rcx, rdx); // rcx=cpool, rdx=tags array
4646   __ get_unsigned_2_byte_index_at_bcp(rbx, 1); // rbx=index
4647   // See if bytecode has already been quicked
4648   __ cmpb(Address(rdx, rbx,
4649                   Address::times_1,
4650                   Array<u1>::base_offset_in_bytes()),
4651           JVM_CONSTANT_Class);
4652   __ jcc(Assembler::equal, quicked);
4653 
4654   __ push(atos); // save receiver for result, and for GC
4655   call_VM(noreg, CAST_FROM_FN_PTR(address, InterpreterRuntime::quicken_io_cc));
4656   // vm_result_2 has metadata result
4657 
4658 #ifndef _LP64
4659   // borrow rdi from locals
4660   __ get_thread(rdi);
4661   __ get_vm_result_2(rax, rdi);
4662   __ restore_locals();
4663 #else
4664   __ get_vm_result_2(rax, r15_thread);
4665 #endif
4666 
4667   __ pop_ptr(rdx); // restore receiver
4668   __ verify_oop(rdx);
4669   __ load_klass(rdx, rdx);
4670   __ jmpb(resolved);
4671 
4672   // Get superklass in rax and subklass in rdx
4673   __ bind(quicked);
4674   __ load_klass(rdx, rax);
4675   __ load_resolved_klass_at_index(rcx, rbx, rax);
4676 
4677   __ bind(resolved);
4678 
4679   // Generate subtype check.  Blows rcx, rdi
4680   // Superklass in rax.  Subklass in rdx.
4681   __ gen_subtype_check(rdx, ok_is_subtype);
4682 
4683   // Come here on failure
4684   __ xorl(rax, rax);
4685   __ jmpb(done);
4686   // Come here on success
4687   __ bind(ok_is_subtype);
4688   __ movl(rax, 1);
4689 
4690   // Collect counts on whether this test sees NULLs a lot or not.
4691   if (ProfileInterpreter) {
4692     __ jmp(done);
4693     __ bind(is_null);
4694     __ profile_null_seen(rcx);
4695   } else {
4696     __ bind(is_null);   // same as 'done'
4697   }
4698   __ bind(done);
4699   // rax = 0: obj == NULL or  obj is not an instanceof the specified klass
4700   // rax = 1: obj != NULL and obj is     an instanceof the specified klass
4701 }
4702 
4703 //----------------------------------------------------------------------------------------------------
4704 // Breakpoints
4705 void TemplateTable::_breakpoint() {
4706   // Note: We get here even if we are single stepping..
4707   // jbug insists on setting breakpoints at every bytecode
4708   // even if we are in single step mode.
4709 
4710   transition(vtos, vtos);
4711 
4712   Register rarg = LP64_ONLY(c_rarg1) NOT_LP64(rcx);
4713 
4714   // get the unpatched byte code
4715   __ get_method(rarg);
4716   __ call_VM(noreg,
4717              CAST_FROM_FN_PTR(address,
4718                               InterpreterRuntime::get_original_bytecode_at),
4719              rarg, rbcp);
4720   __ mov(rbx, rax);  // why?
4721 
4722   // post the breakpoint event
4723   __ get_method(rarg);
4724   __ call_VM(noreg,
4725              CAST_FROM_FN_PTR(address, InterpreterRuntime::_breakpoint),
4726              rarg, rbcp);
4727 
4728   // complete the execution of original bytecode
4729   __ dispatch_only_normal(vtos);
4730 }
4731 
4732 //-----------------------------------------------------------------------------
4733 // Exceptions
4734 
4735 void TemplateTable::athrow() {
4736   transition(atos, vtos);
4737   __ null_check(rax);
4738   __ jump(ExternalAddress(Interpreter::throw_exception_entry()));
4739 }
4740 
4741 //-----------------------------------------------------------------------------
4742 // Synchronization
4743 //
4744 // Note: monitorenter & exit are symmetric routines; which is reflected
4745 //       in the assembly code structure as well
4746 //
4747 // Stack layout:
4748 //
4749 // [expressions  ] <--- rsp               = expression stack top
4750 // ..
4751 // [expressions  ]
4752 // [monitor entry] <--- monitor block top = expression stack bot
4753 // ..
4754 // [monitor entry]
4755 // [frame data   ] <--- monitor block bot
4756 // ...
4757 // [saved rbp    ] <--- rbp
4758 void TemplateTable::monitorenter() {
4759   transition(atos, vtos);
4760 
4761   // check for NULL object
4762   __ null_check(rax);
4763 
4764   __ resolve(IS_NOT_NULL, rax);
4765 
4766   const Address monitor_block_top(
4767         rbp, frame::interpreter_frame_monitor_block_top_offset * wordSize);
4768   const Address monitor_block_bot(
4769         rbp, frame::interpreter_frame_initial_sp_offset * wordSize);
4770   const int entry_size = frame::interpreter_frame_monitor_size() * wordSize;
4771 
4772   Label allocated;
4773 
4774   Register rtop = LP64_ONLY(c_rarg3) NOT_LP64(rcx);
4775   Register rbot = LP64_ONLY(c_rarg2) NOT_LP64(rbx);
4776   Register rmon = LP64_ONLY(c_rarg1) NOT_LP64(rdx);
4777 
4778   // initialize entry pointer
4779   __ xorl(rmon, rmon); // points to free slot or NULL
4780 
4781   // find a free slot in the monitor block (result in rmon)
4782   {
4783     Label entry, loop, exit;
4784     __ movptr(rtop, monitor_block_top); // points to current entry,
4785                                         // starting with top-most entry
4786     __ lea(rbot, monitor_block_bot);    // points to word before bottom
4787                                         // of monitor block
4788     __ jmpb(entry);
4789 
4790     __ bind(loop);
4791     // check if current entry is used
4792     __ cmpptr(Address(rtop, BasicObjectLock::obj_offset_in_bytes()), (int32_t) NULL_WORD);
4793     // if not used then remember entry in rmon
4794     __ cmovptr(Assembler::equal, rmon, rtop);   // cmov => cmovptr
4795     // check if current entry is for same object
4796     __ cmpptr(rax, Address(rtop, BasicObjectLock::obj_offset_in_bytes()));
4797     // if same object then stop searching
4798     __ jccb(Assembler::equal, exit);
4799     // otherwise advance to next entry
4800     __ addptr(rtop, entry_size);
4801     __ bind(entry);
4802     // check if bottom reached
4803     __ cmpptr(rtop, rbot);
4804     // if not at bottom then check this entry
4805     __ jcc(Assembler::notEqual, loop);
4806     __ bind(exit);
4807   }
4808 
4809   __ testptr(rmon, rmon); // check if a slot has been found
4810   __ jcc(Assembler::notZero, allocated); // if found, continue with that one
4811 
4812   // allocate one if there's no free slot
4813   {
4814     Label entry, loop;
4815     // 1. compute new pointers          // rsp: old expression stack top
4816     __ movptr(rmon, monitor_block_bot); // rmon: old expression stack bottom
4817     __ subptr(rsp, entry_size);         // move expression stack top
4818     __ subptr(rmon, entry_size);        // move expression stack bottom
4819     __ mov(rtop, rsp);                  // set start value for copy loop
4820     __ movptr(monitor_block_bot, rmon); // set new monitor block bottom
4821     __ jmp(entry);
4822     // 2. move expression stack contents
4823     __ bind(loop);
4824     __ movptr(rbot, Address(rtop, entry_size)); // load expression stack
4825                                                 // word from old location
4826     __ movptr(Address(rtop, 0), rbot);          // and store it at new location
4827     __ addptr(rtop, wordSize);                  // advance to next word
4828     __ bind(entry);
4829     __ cmpptr(rtop, rmon);                      // check if bottom reached
4830     __ jcc(Assembler::notEqual, loop);          // if not at bottom then
4831                                                 // copy next word
4832   }
4833 
4834   // call run-time routine
4835   // rmon: points to monitor entry
4836   __ bind(allocated);
4837 
4838   // Increment bcp to point to the next bytecode, so exception
4839   // handling for async. exceptions work correctly.
4840   // The object has already been poped from the stack, so the
4841   // expression stack looks correct.
4842   __ increment(rbcp);
4843 
4844   // store object
4845   __ movptr(Address(rmon, BasicObjectLock::obj_offset_in_bytes()), rax);
4846   __ lock_object(rmon);
4847 
4848   // check to make sure this monitor doesn't cause stack overflow after locking
4849   __ save_bcp();  // in case of exception
4850   __ generate_stack_overflow_check(0);
4851 
4852   // The bcp has already been incremented. Just need to dispatch to
4853   // next instruction.
4854   __ dispatch_next(vtos);
4855 }
4856 
4857 void TemplateTable::monitorexit() {
4858   transition(atos, vtos);
4859 
4860   // check for NULL object
4861   __ null_check(rax);
4862 
4863   __ resolve(IS_NOT_NULL, rax);
4864 
4865   const Address monitor_block_top(
4866         rbp, frame::interpreter_frame_monitor_block_top_offset * wordSize);
4867   const Address monitor_block_bot(
4868         rbp, frame::interpreter_frame_initial_sp_offset * wordSize);
4869   const int entry_size = frame::interpreter_frame_monitor_size() * wordSize;
4870 
4871   Register rtop = LP64_ONLY(c_rarg1) NOT_LP64(rdx);
4872   Register rbot = LP64_ONLY(c_rarg2) NOT_LP64(rbx);
4873 
4874   Label found;
4875 
4876   // find matching slot
4877   {
4878     Label entry, loop;
4879     __ movptr(rtop, monitor_block_top); // points to current entry,
4880                                         // starting with top-most entry
4881     __ lea(rbot, monitor_block_bot);    // points to word before bottom
4882                                         // of monitor block
4883     __ jmpb(entry);
4884 
4885     __ bind(loop);
4886     // check if current entry is for same object
4887     __ cmpptr(rax, Address(rtop, BasicObjectLock::obj_offset_in_bytes()));
4888     // if same object then stop searching
4889     __ jcc(Assembler::equal, found);
4890     // otherwise advance to next entry
4891     __ addptr(rtop, entry_size);
4892     __ bind(entry);
4893     // check if bottom reached
4894     __ cmpptr(rtop, rbot);
4895     // if not at bottom then check this entry
4896     __ jcc(Assembler::notEqual, loop);
4897   }
4898 
4899   // error handling. Unlocking was not block-structured
4900   __ call_VM(noreg, CAST_FROM_FN_PTR(address,
4901                    InterpreterRuntime::throw_illegal_monitor_state_exception));
4902   __ should_not_reach_here();
4903 
4904   // call run-time routine
4905   __ bind(found);
4906   __ push_ptr(rax); // make sure object is on stack (contract with oopMaps)
4907   __ unlock_object(rtop);
4908   __ pop_ptr(rax); // discard object
4909 }
4910 
4911 // Wide instructions
4912 void TemplateTable::wide() {
4913   transition(vtos, vtos);
4914   __ load_unsigned_byte(rbx, at_bcp(1));
4915   ExternalAddress wtable((address)Interpreter::_wentry_point);
4916   __ jump(ArrayAddress(wtable, Address(noreg, rbx, Address::times_ptr)));
4917   // Note: the rbcp increment step is part of the individual wide bytecode implementations
4918 }
4919 
4920 // Multi arrays
4921 void TemplateTable::multianewarray() {
4922   transition(vtos, atos);
4923 
4924   Register rarg = LP64_ONLY(c_rarg1) NOT_LP64(rax);
4925   __ load_unsigned_byte(rax, at_bcp(3)); // get number of dimensions
4926   // last dim is on top of stack; we want address of first one:
4927   // first_addr = last_addr + (ndims - 1) * stackElementSize - 1*wordsize
4928   // the latter wordSize to point to the beginning of the array.
4929   __ lea(rarg, Address(rsp, rax, Interpreter::stackElementScale(), -wordSize));
4930   call_VM(rax, CAST_FROM_FN_PTR(address, InterpreterRuntime::multianewarray), rarg);
4931   __ load_unsigned_byte(rbx, at_bcp(3));
4932   __ lea(rsp, Address(rsp, rbx, Interpreter::stackElementScale()));  // get rid of counts
4933 }