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_flattened_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_flattened_array_oop(rdx, 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     // No way to store null in null-free array
1189     __ test_null_free_array_oop(rdx, rbx, is_null_into_value_array_npe);







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