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