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