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