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