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