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       JFR_ONLY(__ enter_jfr_critical_section();)
1829 
1830       call_VM(noreg, CAST_FROM_FN_PTR(address, SharedRuntime::OSR_migration_begin));
1831 
1832       // rax is OSR buffer, move it to expected parameter location
1833       __ mov(j_rarg0, rax);
1834       // We use j_rarg definitions here so that registers don't conflict as parameter
1835       // registers change across platforms as we are in the midst of a calling
1836       // sequence to the OSR nmethod and we don't want collision. These are NOT parameters.
1837 
1838       const Register retaddr   = j_rarg2;
1839       const Register sender_sp = j_rarg1;
1840 
1841       // pop the interpreter frame
1842       __ movptr(sender_sp, Address(rbp, frame::interpreter_frame_sender_sp_offset * wordSize)); // get sender sp
1843       __ leave();                                // remove frame anchor
1844       JFR_ONLY(__ leave_jfr_critical_section();)
1845       __ pop(retaddr);                           // get return address
1846       __ mov(rsp, sender_sp);                    // set sp to sender sp
1847       // Ensure compiled code always sees stack at proper alignment
1848       __ andptr(rsp, -(StackAlignmentInBytes));
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 L_clinit_barrier_slow;
2251   Label resolved;
2252 
2253   Bytecodes::Code code = bytecode();
2254   switch (code) {
2255     case Bytecodes::_nofast_getfield: code = Bytecodes::_getfield; break;
2256     case Bytecodes::_nofast_putfield: code = Bytecodes::_putfield; break;
2257     default: break;
2258   }
2259 
2260   assert(byte_no == f1_byte || byte_no == f2_byte, "byte_no out of range");
2261   __ load_field_entry(cache, index);
2262   if (byte_no == f1_byte) {
2263     __ load_unsigned_byte(temp, Address(cache, in_bytes(ResolvedFieldEntry::get_code_offset())));
2264   } else {
2265     __ load_unsigned_byte(temp, Address(cache, in_bytes(ResolvedFieldEntry::put_code_offset())));
2266   }
2267   __ cmpl(temp, code);  // have we resolved this bytecode?
2268   __ jcc(Assembler::equal, resolved);
2269 
2270   // resolve first time through
2271   __ bind(L_clinit_barrier_slow);
2272   address entry = CAST_FROM_FN_PTR(address, InterpreterRuntime::resolve_from_cache);
2273   __ movl(temp, code);
2274   __ call_VM(noreg, entry, temp);
2275   // Update registers with resolved info
2276   __ load_field_entry(cache, index);
2277 
2278   __ bind(resolved);
2279 
2280   // Class initialization barrier for static fields
2281   if (VM_Version::supports_fast_class_init_checks() &&
2282       (bytecode() == Bytecodes::_getstatic || bytecode() == Bytecodes::_putstatic)) {
2283     const Register field_holder = temp;
2284     const Register thread = LP64_ONLY(r15_thread) NOT_LP64(noreg);
2285     assert(thread != noreg, "x86_32 not supported");
2286 
2287     __ movptr(field_holder, Address(cache, in_bytes(ResolvedFieldEntry::field_holder_offset())));
2288     __ clinit_barrier(field_holder, nullptr /*L_fast_path*/, &L_clinit_barrier_slow);
2289   }
2290 }
2291 
2292 void TemplateTable::load_resolved_field_entry(Register obj,
2293                                               Register cache,
2294                                               Register tos_state,
2295                                               Register offset,
2296                                               Register flags,
2297                                               bool is_static = false) {
2298   assert_different_registers(cache, tos_state, flags, offset);
2299 
2300   // Field offset
2301   __ load_sized_value(offset, Address(cache, in_bytes(ResolvedFieldEntry::field_offset_offset())), sizeof(int), true /*is_signed*/);
2302 
2303   // Flags
2304   __ load_unsigned_byte(flags, Address(cache, in_bytes(ResolvedFieldEntry::flags_offset())));
2305 
2306   // TOS state
2307   __ load_unsigned_byte(tos_state, Address(cache, in_bytes(ResolvedFieldEntry::type_offset())));
2308 
2309   // Klass overwrite register
2310   if (is_static) {
2311     __ movptr(obj, Address(cache, ResolvedFieldEntry::field_holder_offset()));
2312     const int mirror_offset = in_bytes(Klass::java_mirror_offset());
2313     __ movptr(obj, Address(obj, mirror_offset));
2314     __ resolve_oop_handle(obj, rscratch2);
2315   }
2316 
2317 }
2318 
2319 void TemplateTable::load_invokedynamic_entry(Register method) {
2320   // setup registers
2321   const Register appendix = rax;
2322   const Register cache = rcx;
2323   const Register index = rdx;
2324   assert_different_registers(method, appendix, cache, index);
2325 
2326   __ save_bcp();
2327 
2328   Label resolved;
2329 
2330   __ load_resolved_indy_entry(cache, index);
2331   __ movptr(method, Address(cache, in_bytes(ResolvedIndyEntry::method_offset())));
2332 
2333   // Compare the method to zero
2334   __ testptr(method, method);
2335   __ jcc(Assembler::notZero, resolved);
2336 
2337   Bytecodes::Code code = bytecode();
2338 
2339   // Call to the interpreter runtime to resolve invokedynamic
2340   address entry = CAST_FROM_FN_PTR(address, InterpreterRuntime::resolve_from_cache);
2341   __ movl(method, code); // this is essentially Bytecodes::_invokedynamic
2342   __ call_VM(noreg, entry, method);
2343   // Update registers with resolved info
2344   __ load_resolved_indy_entry(cache, index);
2345   __ movptr(method, Address(cache, in_bytes(ResolvedIndyEntry::method_offset())));
2346 
2347 #ifdef ASSERT
2348   __ testptr(method, method);
2349   __ jcc(Assembler::notZero, resolved);
2350   __ stop("Should be resolved by now");
2351 #endif // ASSERT
2352   __ bind(resolved);
2353 
2354   Label L_no_push;
2355   // Check if there is an appendix
2356   __ load_unsigned_byte(index, Address(cache, in_bytes(ResolvedIndyEntry::flags_offset())));
2357   __ testl(index, (1 << ResolvedIndyEntry::has_appendix_shift));
2358   __ jcc(Assembler::zero, L_no_push);
2359 
2360   // Get appendix
2361   __ load_unsigned_short(index, Address(cache, in_bytes(ResolvedIndyEntry::resolved_references_index_offset())));
2362   // Push the appendix as a trailing parameter
2363   // since the parameter_size includes it.
2364   __ load_resolved_reference_at_index(appendix, index);
2365   __ verify_oop(appendix);
2366   __ push(appendix);  // push appendix (MethodType, CallSite, etc.)
2367   __ bind(L_no_push);
2368 
2369   // compute return type
2370   __ load_unsigned_byte(index, Address(cache, in_bytes(ResolvedIndyEntry::result_type_offset())));
2371   // load return address
2372   {
2373     const address table_addr = (address) Interpreter::invoke_return_entry_table_for(code);
2374     ExternalAddress table(table_addr);
2375     __ lea(rscratch1, table);
2376     __ movptr(index, Address(rscratch1, index, Address::times_ptr));
2377   }
2378 
2379   // push return address
2380   __ push(index);
2381 }
2382 
2383 void TemplateTable::load_resolved_method_entry_special_or_static(Register cache,
2384                                                                  Register method,
2385                                                                  Register flags) {
2386   // setup registers
2387   const Register index = rdx;
2388   assert_different_registers(cache, index);
2389   assert_different_registers(method, cache, flags);
2390 
2391   // determine constant pool cache field offsets
2392   resolve_cache_and_index_for_method(f1_byte, cache, index);
2393   __ load_unsigned_byte(flags, Address(cache, in_bytes(ResolvedMethodEntry::flags_offset())));
2394   __ movptr(method, Address(cache, in_bytes(ResolvedMethodEntry::method_offset())));
2395 }
2396 
2397 void TemplateTable::load_resolved_method_entry_handle(Register cache,
2398                                                Register method,
2399                                                Register ref_index,
2400                                                Register flags) {
2401   // setup registers
2402   const Register index = rdx;
2403   assert_different_registers(cache, index);
2404   assert_different_registers(cache, method, ref_index, flags);
2405 
2406   // determine constant pool cache field offsets
2407   resolve_cache_and_index_for_method(f1_byte, cache, index);
2408   __ load_unsigned_byte(flags, Address(cache, in_bytes(ResolvedMethodEntry::flags_offset())));
2409 
2410   // Maybe push appendix
2411   Label L_no_push;
2412   __ testl(flags, (1 << ResolvedMethodEntry::has_appendix_shift));
2413   __ jcc(Assembler::zero, L_no_push);
2414   // invokehandle uses an index into the resolved references array
2415   __ load_unsigned_short(ref_index, Address(cache, in_bytes(ResolvedMethodEntry::resolved_references_index_offset())));
2416   // Push the appendix as a trailing parameter.
2417   // This must be done before we get the receiver,
2418   // since the parameter_size includes it.
2419   Register appendix = method;
2420   __ load_resolved_reference_at_index(appendix, ref_index);
2421   __ push(appendix);  // push appendix (MethodType, CallSite, etc.)
2422   __ bind(L_no_push);
2423 
2424   __ movptr(method, Address(cache, in_bytes(ResolvedMethodEntry::method_offset())));
2425 }
2426 
2427 void TemplateTable::load_resolved_method_entry_interface(Register cache,
2428                                                          Register klass,
2429                                                          Register method_or_table_index,
2430                                                          Register flags) {
2431   // setup registers
2432   const Register index = rdx;
2433   assert_different_registers(cache, klass, method_or_table_index, flags);
2434 
2435   // determine constant pool cache field offsets
2436   resolve_cache_and_index_for_method(f1_byte, cache, index);
2437   __ load_unsigned_byte(flags, Address(cache, in_bytes(ResolvedMethodEntry::flags_offset())));
2438 
2439   // Invokeinterface can behave in different ways:
2440   // If calling a method from java.lang.Object, the forced virtual flag is true so the invocation will
2441   // behave like an invokevirtual call. The state of the virtual final flag will determine whether a method or
2442   // vtable index is placed in the register.
2443   // Otherwise, the registers will be populated with the klass and method.
2444 
2445   Label NotVirtual; Label NotVFinal; Label Done;
2446   __ testl(flags, 1 << ResolvedMethodEntry::is_forced_virtual_shift);
2447   __ jcc(Assembler::zero, NotVirtual);
2448   __ testl(flags, (1 << ResolvedMethodEntry::is_vfinal_shift));
2449   __ jcc(Assembler::zero, NotVFinal);
2450   __ movptr(method_or_table_index, Address(cache, in_bytes(ResolvedMethodEntry::method_offset())));
2451   __ jmp(Done);
2452 
2453   __ bind(NotVFinal);
2454   __ load_unsigned_short(method_or_table_index, Address(cache, in_bytes(ResolvedMethodEntry::table_index_offset())));
2455   __ jmp(Done);
2456 
2457   __ bind(NotVirtual);
2458   __ movptr(method_or_table_index, Address(cache, in_bytes(ResolvedMethodEntry::method_offset())));
2459   __ movptr(klass, Address(cache, in_bytes(ResolvedMethodEntry::klass_offset())));
2460   __ bind(Done);
2461 }
2462 
2463 void TemplateTable::load_resolved_method_entry_virtual(Register cache,
2464                                                        Register method_or_table_index,
2465                                                        Register flags) {
2466   // setup registers
2467   const Register index = rdx;
2468   assert_different_registers(index, cache);
2469   assert_different_registers(method_or_table_index, cache, flags);
2470 
2471   // determine constant pool cache field offsets
2472   resolve_cache_and_index_for_method(f2_byte, cache, index);
2473   __ load_unsigned_byte(flags, Address(cache, in_bytes(ResolvedMethodEntry::flags_offset())));
2474 
2475   // method_or_table_index can either be an itable index or a method depending on the virtual final flag
2476   Label isVFinal; Label Done;
2477   __ testl(flags, (1 << ResolvedMethodEntry::is_vfinal_shift));
2478   __ jcc(Assembler::notZero, isVFinal);
2479   __ load_unsigned_short(method_or_table_index, Address(cache, in_bytes(ResolvedMethodEntry::table_index_offset())));
2480   __ jmp(Done);
2481   __ bind(isVFinal);
2482   __ movptr(method_or_table_index, Address(cache, in_bytes(ResolvedMethodEntry::method_offset())));
2483   __ bind(Done);
2484 }
2485 
2486 // The registers cache and index expected to be set before call.
2487 // Correct values of the cache and index registers are preserved.
2488 void TemplateTable::jvmti_post_field_access(Register cache,
2489                                             Register index,
2490                                             bool is_static,
2491                                             bool has_tos) {
2492   if (JvmtiExport::can_post_field_access()) {
2493     // Check to see if a field access watch has been set before we take
2494     // the time to call into the VM.
2495     Label L1;
2496     assert_different_registers(cache, index, rax);
2497     __ mov32(rax, ExternalAddress((address) JvmtiExport::get_field_access_count_addr()));
2498     __ testl(rax,rax);
2499     __ jcc(Assembler::zero, L1);
2500 
2501     // cache entry pointer
2502     __ load_field_entry(cache, index);
2503     if (is_static) {
2504       __ xorptr(rax, rax);      // null object reference
2505     } else {
2506       __ pop(atos);         // Get the object
2507       __ verify_oop(rax);
2508       __ push(atos);        // Restore stack state
2509     }
2510     // rax,:   object pointer or null
2511     // cache: cache entry pointer
2512     __ call_VM(noreg, CAST_FROM_FN_PTR(address, InterpreterRuntime::post_field_access),
2513               rax, cache);
2514 
2515     __ load_field_entry(cache, index);
2516     __ bind(L1);
2517   }
2518 }
2519 
2520 void TemplateTable::pop_and_check_object(Register r) {
2521   __ pop_ptr(r);
2522   __ null_check(r);  // for field access must check obj.
2523   __ verify_oop(r);
2524 }
2525 
2526 void TemplateTable::getfield_or_static(int byte_no, bool is_static, RewriteControl rc) {
2527   transition(vtos, vtos);
2528 
2529   const Register obj   = c_rarg3;
2530   const Register cache = rcx;
2531   const Register index = rdx;
2532   const Register off   = rbx;
2533   const Register tos_state   = rax;
2534   const Register flags = rdx;
2535   const Register bc    = c_rarg3; // uses same reg as obj, so don't mix them
2536 
2537   resolve_cache_and_index_for_field(byte_no, cache, index);
2538   jvmti_post_field_access(cache, index, is_static, false);
2539   load_resolved_field_entry(obj, cache, tos_state, off, flags, is_static);
2540 
2541   if (!is_static) pop_and_check_object(obj);
2542 
2543   const Address field(obj, off, Address::times_1, 0*wordSize);
2544 
2545   Label Done, notByte, notBool, notInt, notShort, notChar, notLong, notFloat, notObj;
2546 
2547   // Make sure we don't need to mask edx after the above shift
2548   assert(btos == 0, "change code, btos != 0");
2549   __ testl(tos_state, tos_state);
2550   __ jcc(Assembler::notZero, notByte);
2551 
2552   // btos
2553   __ access_load_at(T_BYTE, IN_HEAP, rax, field, noreg);
2554   __ push(btos);
2555   // Rewrite bytecode to be faster
2556   if (!is_static && rc == may_rewrite) {
2557     patch_bytecode(Bytecodes::_fast_bgetfield, bc, rbx);
2558   }
2559   __ jmp(Done);
2560 
2561   __ bind(notByte);
2562   __ cmpl(tos_state, ztos);
2563   __ jcc(Assembler::notEqual, notBool);
2564 
2565   // ztos (same code as btos)
2566   __ access_load_at(T_BOOLEAN, IN_HEAP, rax, field, noreg);
2567   __ push(ztos);
2568   // Rewrite bytecode to be faster
2569   if (!is_static && rc == may_rewrite) {
2570     // use btos rewriting, no truncating to t/f bit is needed for getfield.
2571     patch_bytecode(Bytecodes::_fast_bgetfield, bc, rbx);
2572   }
2573   __ jmp(Done);
2574 
2575   __ bind(notBool);
2576   __ cmpl(tos_state, atos);
2577   __ jcc(Assembler::notEqual, notObj);
2578   // atos
2579   do_oop_load(_masm, field, rax);
2580   __ push(atos);
2581   if (!is_static && rc == may_rewrite) {
2582     patch_bytecode(Bytecodes::_fast_agetfield, bc, rbx);
2583   }
2584   __ jmp(Done);
2585 
2586   __ bind(notObj);
2587   __ cmpl(tos_state, itos);
2588   __ jcc(Assembler::notEqual, notInt);
2589   // itos
2590   __ access_load_at(T_INT, IN_HEAP, rax, field, noreg);
2591   __ push(itos);
2592   // Rewrite bytecode to be faster
2593   if (!is_static && rc == may_rewrite) {
2594     patch_bytecode(Bytecodes::_fast_igetfield, bc, rbx);
2595   }
2596   __ jmp(Done);
2597 
2598   __ bind(notInt);
2599   __ cmpl(tos_state, ctos);
2600   __ jcc(Assembler::notEqual, notChar);
2601   // ctos
2602   __ access_load_at(T_CHAR, IN_HEAP, rax, field, noreg);
2603   __ push(ctos);
2604   // Rewrite bytecode to be faster
2605   if (!is_static && rc == may_rewrite) {
2606     patch_bytecode(Bytecodes::_fast_cgetfield, bc, rbx);
2607   }
2608   __ jmp(Done);
2609 
2610   __ bind(notChar);
2611   __ cmpl(tos_state, stos);
2612   __ jcc(Assembler::notEqual, notShort);
2613   // stos
2614   __ access_load_at(T_SHORT, IN_HEAP, rax, field, noreg);
2615   __ push(stos);
2616   // Rewrite bytecode to be faster
2617   if (!is_static && rc == may_rewrite) {
2618     patch_bytecode(Bytecodes::_fast_sgetfield, bc, rbx);
2619   }
2620   __ jmp(Done);
2621 
2622   __ bind(notShort);
2623   __ cmpl(tos_state, ltos);
2624   __ jcc(Assembler::notEqual, notLong);
2625   // ltos
2626     // Generate code as if volatile (x86_32).  There just aren't enough registers to
2627     // save that information and this code is faster than the test.
2628   __ access_load_at(T_LONG, IN_HEAP | MO_RELAXED, noreg /* ltos */, field, noreg);
2629   __ push(ltos);
2630   // Rewrite bytecode to be faster
2631   if (!is_static && rc == may_rewrite) patch_bytecode(Bytecodes::_fast_lgetfield, bc, rbx);
2632   __ jmp(Done);
2633 
2634   __ bind(notLong);
2635   __ cmpl(tos_state, ftos);
2636   __ jcc(Assembler::notEqual, notFloat);
2637   // ftos
2638 
2639   __ access_load_at(T_FLOAT, IN_HEAP, noreg /* ftos */, field, noreg);
2640   __ push(ftos);
2641   // Rewrite bytecode to be faster
2642   if (!is_static && rc == may_rewrite) {
2643     patch_bytecode(Bytecodes::_fast_fgetfield, bc, rbx);
2644   }
2645   __ jmp(Done);
2646 
2647   __ bind(notFloat);
2648 #ifdef ASSERT
2649   Label notDouble;
2650   __ cmpl(tos_state, dtos);
2651   __ jcc(Assembler::notEqual, notDouble);
2652 #endif
2653   // dtos
2654   // MO_RELAXED: for the case of volatile field, in fact it adds no extra work for the underlying implementation
2655   __ access_load_at(T_DOUBLE, IN_HEAP | MO_RELAXED, noreg /* dtos */, field, noreg);
2656   __ push(dtos);
2657   // Rewrite bytecode to be faster
2658   if (!is_static && rc == may_rewrite) {
2659     patch_bytecode(Bytecodes::_fast_dgetfield, bc, rbx);
2660   }
2661 #ifdef ASSERT
2662   __ jmp(Done);
2663 
2664   __ bind(notDouble);
2665   __ stop("Bad state");
2666 #endif
2667 
2668   __ bind(Done);
2669   // [jk] not needed currently
2670   // volatile_barrier(Assembler::Membar_mask_bits(Assembler::LoadLoad |
2671   //                                              Assembler::LoadStore));
2672 }
2673 
2674 void TemplateTable::getfield(int byte_no) {
2675   getfield_or_static(byte_no, false);
2676 }
2677 
2678 void TemplateTable::nofast_getfield(int byte_no) {
2679   getfield_or_static(byte_no, false, may_not_rewrite);
2680 }
2681 
2682 void TemplateTable::getstatic(int byte_no) {
2683   getfield_or_static(byte_no, true);
2684 }
2685 
2686 
2687 // The registers cache and index expected to be set before call.
2688 // The function may destroy various registers, just not the cache and index registers.
2689 void TemplateTable::jvmti_post_field_mod(Register cache, Register index, bool is_static) {
2690   // Cache is rcx and index is rdx
2691   const Register entry = c_rarg2; // ResolvedFieldEntry
2692   const Register obj = c_rarg1;   // Object pointer
2693   const Register value = c_rarg3; // JValue object
2694 
2695   if (JvmtiExport::can_post_field_modification()) {
2696     // Check to see if a field modification watch has been set before
2697     // we take the time to call into the VM.
2698     Label L1;
2699     assert_different_registers(cache, obj, rax);
2700     __ mov32(rax, ExternalAddress((address)JvmtiExport::get_field_modification_count_addr()));
2701     __ testl(rax, rax);
2702     __ jcc(Assembler::zero, L1);
2703 
2704     __ mov(entry, cache);
2705 
2706     if (is_static) {
2707       // Life is simple.  Null out the object pointer.
2708       __ xorl(obj, obj);
2709 
2710     } else {
2711       // Life is harder. The stack holds the value on top, followed by
2712       // the object.  We don't know the size of the value, though; it
2713       // could be one or two words depending on its type. As a result,
2714       // we must find the type to determine where the object is.
2715       __ load_unsigned_byte(value, Address(entry, in_bytes(ResolvedFieldEntry::type_offset())));
2716       __ movptr(obj, at_tos_p1());  // initially assume a one word jvalue
2717       __ cmpl(value, ltos);
2718       __ cmovptr(Assembler::equal,
2719                  obj, at_tos_p2()); // ltos (two word jvalue)
2720       __ cmpl(value, dtos);
2721       __ cmovptr(Assembler::equal,
2722                  obj, at_tos_p2()); // dtos (two word jvalue)
2723     }
2724 
2725     // object (tos)
2726     __ mov(value, rsp);
2727     // obj: object pointer set up above (null if static)
2728     // cache: field entry pointer
2729     // value: jvalue object on the stack
2730     __ call_VM(noreg,
2731               CAST_FROM_FN_PTR(address,
2732                               InterpreterRuntime::post_field_modification),
2733               obj, entry, value);
2734     // Reload field entry
2735     __ load_field_entry(cache, index);
2736     __ bind(L1);
2737   }
2738 }
2739 
2740 void TemplateTable::putfield_or_static(int byte_no, bool is_static, RewriteControl rc) {
2741   transition(vtos, vtos);
2742 
2743   const Register obj = rcx;
2744   const Register cache = rcx;
2745   const Register index = rdx;
2746   const Register tos_state   = rdx;
2747   const Register off   = rbx;
2748   const Register flags = rax;
2749 
2750   resolve_cache_and_index_for_field(byte_no, cache, index);
2751   jvmti_post_field_mod(cache, index, is_static);
2752   load_resolved_field_entry(obj, cache, tos_state, off, flags, is_static);
2753 
2754   // [jk] not needed currently
2755   // volatile_barrier(Assembler::Membar_mask_bits(Assembler::LoadStore |
2756   //                                              Assembler::StoreStore));
2757 
2758   Label notVolatile, Done;
2759 
2760   // Check for volatile store
2761   __ andl(flags, (1 << ResolvedFieldEntry::is_volatile_shift));
2762   __ testl(flags, flags);
2763   __ jcc(Assembler::zero, notVolatile);
2764 
2765   putfield_or_static_helper(byte_no, is_static, rc, obj, off, tos_state);
2766   volatile_barrier(Assembler::Membar_mask_bits(Assembler::StoreLoad |
2767                                                Assembler::StoreStore));
2768   __ jmp(Done);
2769   __ bind(notVolatile);
2770 
2771   putfield_or_static_helper(byte_no, is_static, rc, obj, off, tos_state);
2772 
2773   __ bind(Done);
2774 }
2775 
2776 void TemplateTable::putfield_or_static_helper(int byte_no, bool is_static, RewriteControl rc,
2777                                               Register obj, Register off, Register tos_state) {
2778 
2779   // field addresses
2780   const Address field(obj, off, Address::times_1, 0*wordSize);
2781 
2782   Label notByte, notBool, notInt, notShort, notChar,
2783         notLong, notFloat, notObj;
2784   Label Done;
2785 
2786   const Register bc    = c_rarg3;
2787 
2788   // Test TOS state
2789   __ testl(tos_state, tos_state);
2790   __ jcc(Assembler::notZero, notByte);
2791 
2792   // btos
2793   {
2794     __ pop(btos);
2795     if (!is_static) pop_and_check_object(obj);
2796     __ access_store_at(T_BYTE, IN_HEAP, field, rax, noreg, noreg, noreg);
2797     if (!is_static && rc == may_rewrite) {
2798       patch_bytecode(Bytecodes::_fast_bputfield, bc, rbx, true, byte_no);
2799     }
2800     __ jmp(Done);
2801   }
2802 
2803   __ bind(notByte);
2804   __ cmpl(tos_state, ztos);
2805   __ jcc(Assembler::notEqual, notBool);
2806 
2807   // ztos
2808   {
2809     __ pop(ztos);
2810     if (!is_static) pop_and_check_object(obj);
2811     __ access_store_at(T_BOOLEAN, IN_HEAP, field, rax, noreg, noreg, noreg);
2812     if (!is_static && rc == may_rewrite) {
2813       patch_bytecode(Bytecodes::_fast_zputfield, bc, rbx, true, byte_no);
2814     }
2815     __ jmp(Done);
2816   }
2817 
2818   __ bind(notBool);
2819   __ cmpl(tos_state, atos);
2820   __ jcc(Assembler::notEqual, notObj);
2821 
2822   // atos
2823   {
2824     __ pop(atos);
2825     if (!is_static) pop_and_check_object(obj);
2826     // Store into the field
2827     do_oop_store(_masm, field, rax);
2828     if (!is_static && rc == may_rewrite) {
2829       patch_bytecode(Bytecodes::_fast_aputfield, bc, rbx, true, byte_no);
2830     }
2831     __ jmp(Done);
2832   }
2833 
2834   __ bind(notObj);
2835   __ cmpl(tos_state, itos);
2836   __ jcc(Assembler::notEqual, notInt);
2837 
2838   // itos
2839   {
2840     __ pop(itos);
2841     if (!is_static) pop_and_check_object(obj);
2842     __ access_store_at(T_INT, IN_HEAP, field, rax, noreg, noreg, noreg);
2843     if (!is_static && rc == may_rewrite) {
2844       patch_bytecode(Bytecodes::_fast_iputfield, bc, rbx, true, byte_no);
2845     }
2846     __ jmp(Done);
2847   }
2848 
2849   __ bind(notInt);
2850   __ cmpl(tos_state, ctos);
2851   __ jcc(Assembler::notEqual, notChar);
2852 
2853   // ctos
2854   {
2855     __ pop(ctos);
2856     if (!is_static) pop_and_check_object(obj);
2857     __ access_store_at(T_CHAR, IN_HEAP, field, rax, noreg, noreg, noreg);
2858     if (!is_static && rc == may_rewrite) {
2859       patch_bytecode(Bytecodes::_fast_cputfield, bc, rbx, true, byte_no);
2860     }
2861     __ jmp(Done);
2862   }
2863 
2864   __ bind(notChar);
2865   __ cmpl(tos_state, stos);
2866   __ jcc(Assembler::notEqual, notShort);
2867 
2868   // stos
2869   {
2870     __ pop(stos);
2871     if (!is_static) pop_and_check_object(obj);
2872     __ access_store_at(T_SHORT, IN_HEAP, field, rax, noreg, noreg, noreg);
2873     if (!is_static && rc == may_rewrite) {
2874       patch_bytecode(Bytecodes::_fast_sputfield, bc, rbx, true, byte_no);
2875     }
2876     __ jmp(Done);
2877   }
2878 
2879   __ bind(notShort);
2880   __ cmpl(tos_state, ltos);
2881   __ jcc(Assembler::notEqual, notLong);
2882 
2883   // ltos
2884   {
2885     __ pop(ltos);
2886     if (!is_static) pop_and_check_object(obj);
2887     // MO_RELAXED: generate atomic store for the case of volatile field (important for x86_32)
2888     __ access_store_at(T_LONG, IN_HEAP | MO_RELAXED, field, noreg /* ltos*/, noreg, noreg, noreg);
2889     if (!is_static && rc == may_rewrite) {
2890       patch_bytecode(Bytecodes::_fast_lputfield, bc, rbx, true, byte_no);
2891     }
2892     __ jmp(Done);
2893   }
2894 
2895   __ bind(notLong);
2896   __ cmpl(tos_state, ftos);
2897   __ jcc(Assembler::notEqual, notFloat);
2898 
2899   // ftos
2900   {
2901     __ pop(ftos);
2902     if (!is_static) pop_and_check_object(obj);
2903     __ access_store_at(T_FLOAT, IN_HEAP, field, noreg /* ftos */, noreg, noreg, noreg);
2904     if (!is_static && rc == may_rewrite) {
2905       patch_bytecode(Bytecodes::_fast_fputfield, bc, rbx, true, byte_no);
2906     }
2907     __ jmp(Done);
2908   }
2909 
2910   __ bind(notFloat);
2911 #ifdef ASSERT
2912   Label notDouble;
2913   __ cmpl(tos_state, dtos);
2914   __ jcc(Assembler::notEqual, notDouble);
2915 #endif
2916 
2917   // dtos
2918   {
2919     __ pop(dtos);
2920     if (!is_static) pop_and_check_object(obj);
2921     // MO_RELAXED: for the case of volatile field, in fact it adds no extra work for the underlying implementation
2922     __ access_store_at(T_DOUBLE, IN_HEAP | MO_RELAXED, field, noreg /* dtos */, noreg, noreg, noreg);
2923     if (!is_static && rc == may_rewrite) {
2924       patch_bytecode(Bytecodes::_fast_dputfield, bc, rbx, true, byte_no);
2925     }
2926   }
2927 
2928 #ifdef ASSERT
2929   __ jmp(Done);
2930 
2931   __ bind(notDouble);
2932   __ stop("Bad state");
2933 #endif
2934 
2935   __ bind(Done);
2936 }
2937 
2938 void TemplateTable::putfield(int byte_no) {
2939   putfield_or_static(byte_no, false);
2940 }
2941 
2942 void TemplateTable::nofast_putfield(int byte_no) {
2943   putfield_or_static(byte_no, false, may_not_rewrite);
2944 }
2945 
2946 void TemplateTable::putstatic(int byte_no) {
2947   putfield_or_static(byte_no, true);
2948 }
2949 
2950 void TemplateTable::jvmti_post_fast_field_mod() {
2951 
2952   const Register scratch = c_rarg3;
2953 
2954   if (JvmtiExport::can_post_field_modification()) {
2955     // Check to see if a field modification watch has been set before
2956     // we take the time to call into the VM.
2957     Label L2;
2958     __ mov32(scratch, ExternalAddress((address)JvmtiExport::get_field_modification_count_addr()));
2959     __ testl(scratch, scratch);
2960     __ jcc(Assembler::zero, L2);
2961     __ pop_ptr(rbx);                  // copy the object pointer from tos
2962     __ verify_oop(rbx);
2963     __ push_ptr(rbx);                 // put the object pointer back on tos
2964     // Save tos values before call_VM() clobbers them. Since we have
2965     // to do it for every data type, we use the saved values as the
2966     // jvalue object.
2967     switch (bytecode()) {          // load values into the jvalue object
2968     case Bytecodes::_fast_aputfield: __ push_ptr(rax); break;
2969     case Bytecodes::_fast_bputfield: // fall through
2970     case Bytecodes::_fast_zputfield: // fall through
2971     case Bytecodes::_fast_sputfield: // fall through
2972     case Bytecodes::_fast_cputfield: // fall through
2973     case Bytecodes::_fast_iputfield: __ push_i(rax); break;
2974     case Bytecodes::_fast_dputfield: __ push(dtos); break;
2975     case Bytecodes::_fast_fputfield: __ push(ftos); break;
2976     case Bytecodes::_fast_lputfield: __ push_l(rax); break;
2977 
2978     default:
2979       ShouldNotReachHere();
2980     }
2981     __ mov(scratch, rsp);             // points to jvalue on the stack
2982     // access constant pool cache entry
2983     __ load_field_entry(c_rarg2, rax);
2984     __ verify_oop(rbx);
2985     // rbx: object pointer copied above
2986     // c_rarg2: cache entry pointer
2987     // c_rarg3: jvalue object on the stack
2988     __ call_VM(noreg, CAST_FROM_FN_PTR(address, InterpreterRuntime::post_field_modification), rbx, c_rarg2, c_rarg3);
2989 
2990     switch (bytecode()) {             // restore tos values
2991     case Bytecodes::_fast_aputfield: __ pop_ptr(rax); break;
2992     case Bytecodes::_fast_bputfield: // fall through
2993     case Bytecodes::_fast_zputfield: // fall through
2994     case Bytecodes::_fast_sputfield: // fall through
2995     case Bytecodes::_fast_cputfield: // fall through
2996     case Bytecodes::_fast_iputfield: __ pop_i(rax); break;
2997     case Bytecodes::_fast_dputfield: __ pop(dtos); break;
2998     case Bytecodes::_fast_fputfield: __ pop(ftos); break;
2999     case Bytecodes::_fast_lputfield: __ pop_l(rax); break;
3000     default: break;
3001     }
3002     __ bind(L2);
3003   }
3004 }
3005 
3006 void TemplateTable::fast_storefield(TosState state) {
3007   transition(state, vtos);
3008 
3009   Register cache = rcx;
3010 
3011   Label notVolatile, Done;
3012 
3013   jvmti_post_fast_field_mod();
3014 
3015   __ push(rax);
3016   __ load_field_entry(rcx, rax);
3017   load_resolved_field_entry(noreg, cache, rax, rbx, rdx);
3018   // RBX: field offset, RAX: TOS, RDX: flags
3019   __ andl(rdx, (1 << ResolvedFieldEntry::is_volatile_shift));
3020   __ pop(rax);
3021 
3022   // Get object from stack
3023   pop_and_check_object(rcx);
3024 
3025   // field address
3026   const Address field(rcx, rbx, Address::times_1);
3027 
3028   // Check for volatile store
3029   __ testl(rdx, rdx);
3030   __ jcc(Assembler::zero, notVolatile);
3031 
3032   fast_storefield_helper(field, rax);
3033   volatile_barrier(Assembler::Membar_mask_bits(Assembler::StoreLoad |
3034                                                Assembler::StoreStore));
3035   __ jmp(Done);
3036   __ bind(notVolatile);
3037 
3038   fast_storefield_helper(field, rax);
3039 
3040   __ bind(Done);
3041 }
3042 
3043 void TemplateTable::fast_storefield_helper(Address field, Register rax) {
3044 
3045   // access field
3046   switch (bytecode()) {
3047   case Bytecodes::_fast_aputfield:
3048     do_oop_store(_masm, field, rax);
3049     break;
3050   case Bytecodes::_fast_lputfield:
3051     __ access_store_at(T_LONG, IN_HEAP, field, noreg /* ltos */, noreg, noreg, noreg);
3052     break;
3053   case Bytecodes::_fast_iputfield:
3054     __ access_store_at(T_INT, IN_HEAP, field, rax, noreg, noreg, noreg);
3055     break;
3056   case Bytecodes::_fast_zputfield:
3057     __ access_store_at(T_BOOLEAN, IN_HEAP, field, rax, noreg, noreg, noreg);
3058     break;
3059   case Bytecodes::_fast_bputfield:
3060     __ access_store_at(T_BYTE, IN_HEAP, field, rax, noreg, noreg, noreg);
3061     break;
3062   case Bytecodes::_fast_sputfield:
3063     __ access_store_at(T_SHORT, IN_HEAP, field, rax, noreg, noreg, noreg);
3064     break;
3065   case Bytecodes::_fast_cputfield:
3066     __ access_store_at(T_CHAR, IN_HEAP, field, rax, noreg, noreg, noreg);
3067     break;
3068   case Bytecodes::_fast_fputfield:
3069     __ access_store_at(T_FLOAT, IN_HEAP, field, noreg /* ftos*/, noreg, noreg, noreg);
3070     break;
3071   case Bytecodes::_fast_dputfield:
3072     __ access_store_at(T_DOUBLE, IN_HEAP, field, noreg /* dtos*/, noreg, noreg, noreg);
3073     break;
3074   default:
3075     ShouldNotReachHere();
3076   }
3077 }
3078 
3079 void TemplateTable::fast_accessfield(TosState state) {
3080   transition(atos, state);
3081 
3082   // Do the JVMTI work here to avoid disturbing the register state below
3083   if (JvmtiExport::can_post_field_access()) {
3084     // Check to see if a field access watch has been set before we
3085     // take the time to call into the VM.
3086     Label L1;
3087     __ mov32(rcx, ExternalAddress((address) JvmtiExport::get_field_access_count_addr()));
3088     __ testl(rcx, rcx);
3089     __ jcc(Assembler::zero, L1);
3090     // access constant pool cache entry
3091     __ load_field_entry(c_rarg2, rcx);
3092     __ verify_oop(rax);
3093     __ push_ptr(rax);  // save object pointer before call_VM() clobbers it
3094     __ mov(c_rarg1, rax);
3095     // c_rarg1: object pointer copied above
3096     // c_rarg2: cache entry pointer
3097     __ call_VM(noreg, CAST_FROM_FN_PTR(address, InterpreterRuntime::post_field_access), c_rarg1, c_rarg2);
3098     __ pop_ptr(rax); // restore object pointer
3099     __ bind(L1);
3100   }
3101 
3102   // access constant pool cache
3103   __ load_field_entry(rcx, rbx);
3104   __ load_sized_value(rbx, Address(rcx, in_bytes(ResolvedFieldEntry::field_offset_offset())), sizeof(int), true /*is_signed*/);
3105 
3106   // rax: object
3107   __ verify_oop(rax);
3108   __ null_check(rax);
3109   Address field(rax, rbx, Address::times_1);
3110 
3111   // access field
3112   switch (bytecode()) {
3113   case Bytecodes::_fast_agetfield:
3114     do_oop_load(_masm, field, rax);
3115     __ verify_oop(rax);
3116     break;
3117   case Bytecodes::_fast_lgetfield:
3118     __ access_load_at(T_LONG, IN_HEAP, noreg /* ltos */, field, noreg);
3119     break;
3120   case Bytecodes::_fast_igetfield:
3121     __ access_load_at(T_INT, IN_HEAP, rax, field, noreg);
3122     break;
3123   case Bytecodes::_fast_bgetfield:
3124     __ access_load_at(T_BYTE, IN_HEAP, rax, field, noreg);
3125     break;
3126   case Bytecodes::_fast_sgetfield:
3127     __ access_load_at(T_SHORT, IN_HEAP, rax, field, noreg);
3128     break;
3129   case Bytecodes::_fast_cgetfield:
3130     __ access_load_at(T_CHAR, IN_HEAP, rax, field, noreg);
3131     break;
3132   case Bytecodes::_fast_fgetfield:
3133     __ access_load_at(T_FLOAT, IN_HEAP, noreg /* ftos */, field, noreg);
3134     break;
3135   case Bytecodes::_fast_dgetfield:
3136     __ access_load_at(T_DOUBLE, IN_HEAP, noreg /* dtos */, field, noreg);
3137     break;
3138   default:
3139     ShouldNotReachHere();
3140   }
3141   // [jk] not needed currently
3142   //   Label notVolatile;
3143   //   __ testl(rdx, rdx);
3144   //   __ jcc(Assembler::zero, notVolatile);
3145   //   __ membar(Assembler::LoadLoad);
3146   //   __ bind(notVolatile);
3147 }
3148 
3149 void TemplateTable::fast_xaccess(TosState state) {
3150   transition(vtos, state);
3151 
3152   // get receiver
3153   __ movptr(rax, aaddress(0));
3154   // access constant pool cache
3155   __ load_field_entry(rcx, rdx, 2);
3156   __ load_sized_value(rbx, Address(rcx, in_bytes(ResolvedFieldEntry::field_offset_offset())), sizeof(int), true /*is_signed*/);
3157 
3158   // make sure exception is reported in correct bcp range (getfield is
3159   // next instruction)
3160   __ increment(rbcp);
3161   __ null_check(rax);
3162   const Address field = Address(rax, rbx, Address::times_1, 0*wordSize);
3163   switch (state) {
3164   case itos:
3165     __ access_load_at(T_INT, IN_HEAP, rax, field, noreg);
3166     break;
3167   case atos:
3168     do_oop_load(_masm, field, rax);
3169     __ verify_oop(rax);
3170     break;
3171   case ftos:
3172     __ access_load_at(T_FLOAT, IN_HEAP, noreg /* ftos */, field, noreg);
3173     break;
3174   default:
3175     ShouldNotReachHere();
3176   }
3177 
3178   // [jk] not needed currently
3179   // Label notVolatile;
3180   // __ movl(rdx, Address(rcx, rdx, Address::times_8,
3181   //                      in_bytes(ConstantPoolCache::base_offset() +
3182   //                               ConstantPoolCacheEntry::flags_offset())));
3183   // __ shrl(rdx, ConstantPoolCacheEntry::is_volatile_shift);
3184   // __ testl(rdx, 0x1);
3185   // __ jcc(Assembler::zero, notVolatile);
3186   // __ membar(Assembler::LoadLoad);
3187   // __ bind(notVolatile);
3188 
3189   __ decrement(rbcp);
3190 }
3191 
3192 //-----------------------------------------------------------------------------
3193 // Calls
3194 
3195 void TemplateTable::prepare_invoke(Register cache, Register recv, Register flags) {
3196   // determine flags
3197   const Bytecodes::Code code = bytecode();
3198   const bool load_receiver       = (code != Bytecodes::_invokestatic) && (code != Bytecodes::_invokedynamic);
3199   assert_different_registers(recv, flags);
3200 
3201   // save 'interpreter return address'
3202   __ save_bcp();
3203 
3204   // Save flags and load TOS
3205   __ movl(rbcp, flags);
3206   __ load_unsigned_byte(flags, Address(cache, in_bytes(ResolvedMethodEntry::type_offset())));
3207 
3208   // load receiver if needed (after appendix is pushed so parameter size is correct)
3209   // Note: no return address pushed yet
3210   if (load_receiver) {
3211     __ load_unsigned_short(recv, Address(cache, in_bytes(ResolvedMethodEntry::num_parameters_offset())));
3212     const int no_return_pc_pushed_yet = -1;  // argument slot correction before we push return address
3213     const int receiver_is_at_end      = -1;  // back off one slot to get receiver
3214     Address recv_addr = __ argument_address(recv, no_return_pc_pushed_yet + receiver_is_at_end);
3215     __ movptr(recv, recv_addr);
3216     __ verify_oop(recv);
3217   }
3218 
3219   // load return address
3220   {
3221     const address table_addr = (address) Interpreter::invoke_return_entry_table_for(code);
3222     ExternalAddress table(table_addr);
3223     __ lea(rscratch1, table);
3224     __ movptr(flags, Address(rscratch1, flags, Address::times_ptr));
3225   }
3226 
3227   // push return address
3228   __ push(flags);
3229 
3230   // Restore flags value from the constant pool cache entry, and restore rsi
3231   // for later null checks.  r13 is the bytecode pointer
3232   __ movl(flags, rbcp);
3233   __ restore_bcp();
3234 }
3235 
3236 void TemplateTable::invokevirtual_helper(Register index,
3237                                          Register recv,
3238                                          Register flags) {
3239   // Uses temporary registers rax, rdx
3240   assert_different_registers(index, recv, rax, rdx);
3241   assert(index == rbx, "");
3242   assert(recv  == rcx, "");
3243 
3244   // Test for an invoke of a final method
3245   Label notFinal;
3246   __ movl(rax, flags);
3247   __ andl(rax, (1 << ResolvedMethodEntry::is_vfinal_shift));
3248   __ jcc(Assembler::zero, notFinal);
3249 
3250   const Register method = index;  // method must be rbx
3251   assert(method == rbx,
3252          "Method* must be rbx for interpreter calling convention");
3253 
3254   // do the call - the index is actually the method to call
3255   // that is, f2 is a vtable index if !is_vfinal, else f2 is a Method*
3256 
3257   // It's final, need a null check here!
3258   __ null_check(recv);
3259 
3260   // profile this call
3261   __ profile_final_call(rax);
3262   __ profile_arguments_type(rax, method, rbcp, true);
3263 
3264   __ jump_from_interpreted(method, rax);
3265 
3266   __ bind(notFinal);
3267 
3268   // get receiver klass
3269   __ load_klass(rax, recv, rscratch1);
3270 
3271   // profile this call
3272   __ profile_virtual_call(rax, rlocals, rdx);
3273   // get target Method* & entry point
3274   __ lookup_virtual_method(rax, index, method);
3275 
3276   __ profile_arguments_type(rdx, method, rbcp, true);
3277   __ jump_from_interpreted(method, rdx);
3278 }
3279 
3280 void TemplateTable::invokevirtual(int byte_no) {
3281   transition(vtos, vtos);
3282   assert(byte_no == f2_byte, "use this argument");
3283 
3284   load_resolved_method_entry_virtual(rcx,  // ResolvedMethodEntry*
3285                                      rbx,  // Method or itable index
3286                                      rdx); // Flags
3287   prepare_invoke(rcx,  // ResolvedMethodEntry*
3288                  rcx,  // Receiver
3289                  rdx); // flags
3290 
3291   // rbx: index
3292   // rcx: receiver
3293   // rdx: flags
3294   invokevirtual_helper(rbx, rcx, rdx);
3295 }
3296 
3297 void TemplateTable::invokespecial(int byte_no) {
3298   transition(vtos, vtos);
3299   assert(byte_no == f1_byte, "use this argument");
3300 
3301   load_resolved_method_entry_special_or_static(rcx,  // ResolvedMethodEntry*
3302                                                rbx,  // Method*
3303                                                rdx); // flags
3304   prepare_invoke(rcx,
3305                  rcx,  // get receiver also for null check
3306                  rdx); // flags
3307 
3308   __ verify_oop(rcx);
3309   __ null_check(rcx);
3310   // do the call
3311   __ profile_call(rax);
3312   __ profile_arguments_type(rax, rbx, rbcp, false);
3313   __ jump_from_interpreted(rbx, rax);
3314 }
3315 
3316 void TemplateTable::invokestatic(int byte_no) {
3317   transition(vtos, vtos);
3318   assert(byte_no == f1_byte, "use this argument");
3319 
3320   load_resolved_method_entry_special_or_static(rcx, // ResolvedMethodEntry*
3321                                                rbx, // Method*
3322                                                rdx  // flags
3323                                                );
3324   prepare_invoke(rcx, rcx, rdx);  // cache and flags
3325 
3326   // do the call
3327   __ profile_call(rax);
3328   __ profile_arguments_type(rax, rbx, rbcp, false);
3329   __ jump_from_interpreted(rbx, rax);
3330 }
3331 
3332 
3333 void TemplateTable::fast_invokevfinal(int byte_no) {
3334   transition(vtos, vtos);
3335   assert(byte_no == f2_byte, "use this argument");
3336   __ stop("fast_invokevfinal not used on x86");
3337 }
3338 
3339 
3340 void TemplateTable::invokeinterface(int byte_no) {
3341   transition(vtos, vtos);
3342   assert(byte_no == f1_byte, "use this argument");
3343 
3344   load_resolved_method_entry_interface(rcx,  // ResolvedMethodEntry*
3345                                        rax,  // Klass*
3346                                        rbx,  // Method* or itable/vtable index
3347                                        rdx); // flags
3348   prepare_invoke(rcx, rcx, rdx); // receiver, flags
3349 
3350   // First check for Object case, then private interface method,
3351   // then regular interface method.
3352 
3353   // Special case of invokeinterface called for virtual method of
3354   // java.lang.Object.  See cpCache.cpp for details.
3355   Label notObjectMethod;
3356   __ movl(rlocals, rdx);
3357   __ andl(rlocals, (1 << ResolvedMethodEntry::is_forced_virtual_shift));
3358   __ jcc(Assembler::zero, notObjectMethod);
3359 
3360   invokevirtual_helper(rbx, rcx, rdx);
3361   // no return from above
3362   __ bind(notObjectMethod);
3363 
3364   Label no_such_interface; // for receiver subtype check
3365   Register recvKlass; // used for exception processing
3366 
3367   // Check for private method invocation - indicated by vfinal
3368   Label notVFinal;
3369   __ movl(rlocals, rdx);
3370   __ andl(rlocals, (1 << ResolvedMethodEntry::is_vfinal_shift));
3371   __ jcc(Assembler::zero, notVFinal);
3372 
3373   // Get receiver klass into rlocals - also a null check
3374   __ load_klass(rlocals, rcx, rscratch1);
3375 
3376   Label subtype;
3377   __ check_klass_subtype(rlocals, rax, rbcp, subtype);
3378   // If we get here the typecheck failed
3379   recvKlass = rdx;
3380   __ mov(recvKlass, rlocals); // shuffle receiver class for exception use
3381   __ jmp(no_such_interface);
3382 
3383   __ bind(subtype);
3384 
3385   // do the call - rbx is actually the method to call
3386 
3387   __ profile_final_call(rdx);
3388   __ profile_arguments_type(rdx, rbx, rbcp, true);
3389 
3390   __ jump_from_interpreted(rbx, rdx);
3391   // no return from above
3392   __ bind(notVFinal);
3393 
3394   // Get receiver klass into rdx - also a null check
3395   __ restore_locals();  // restore r14
3396   __ load_klass(rdx, rcx, rscratch1);
3397 
3398   Label no_such_method;
3399 
3400   // Preserve method for throw_AbstractMethodErrorVerbose.
3401   __ mov(rcx, rbx);
3402   // Receiver subtype check against REFC.
3403   // Superklass in rax. Subklass in rdx. Blows rcx, rdi.
3404   __ lookup_interface_method(// inputs: rec. class, interface, itable index
3405                              rdx, rax, noreg,
3406                              // outputs: scan temp. reg, scan temp. reg
3407                              rbcp, rlocals,
3408                              no_such_interface,
3409                              /*return_method=*/false);
3410 
3411   // profile this call
3412   __ restore_bcp(); // rbcp was destroyed by receiver type check
3413   __ profile_virtual_call(rdx, rbcp, rlocals);
3414 
3415   // Get declaring interface class from method, and itable index
3416   __ load_method_holder(rax, rbx);
3417   __ movl(rbx, Address(rbx, Method::itable_index_offset()));
3418   __ subl(rbx, Method::itable_index_max);
3419   __ negl(rbx);
3420 
3421   // Preserve recvKlass for throw_AbstractMethodErrorVerbose.
3422   __ mov(rlocals, rdx);
3423   __ lookup_interface_method(// inputs: rec. class, interface, itable index
3424                              rlocals, rax, rbx,
3425                              // outputs: method, scan temp. reg
3426                              rbx, rbcp,
3427                              no_such_interface);
3428 
3429   // rbx: Method* to call
3430   // rcx: receiver
3431   // Check for abstract method error
3432   // Note: This should be done more efficiently via a throw_abstract_method_error
3433   //       interpreter entry point and a conditional jump to it in case of a null
3434   //       method.
3435   __ testptr(rbx, rbx);
3436   __ jcc(Assembler::zero, no_such_method);
3437 
3438   __ profile_arguments_type(rdx, rbx, rbcp, true);
3439 
3440   // do the call
3441   // rcx: receiver
3442   // rbx,: Method*
3443   __ jump_from_interpreted(rbx, rdx);
3444   __ should_not_reach_here();
3445 
3446   // exception handling code follows...
3447   // note: must restore interpreter registers to canonical
3448   //       state for exception handling to work correctly!
3449 
3450   __ bind(no_such_method);
3451   // throw exception
3452   __ pop(rbx);           // pop return address (pushed by prepare_invoke)
3453   __ restore_bcp();      // rbcp must be correct for exception handler   (was destroyed)
3454   __ restore_locals();   // make sure locals pointer is correct as well (was destroyed)
3455   // Pass arguments for generating a verbose error message.
3456   recvKlass = c_rarg1;
3457   Register method    = c_rarg2;
3458   if (recvKlass != rdx) { __ movq(recvKlass, rdx); }
3459   if (method != rcx)    { __ movq(method, rcx);    }
3460   __ call_VM(noreg, CAST_FROM_FN_PTR(address, InterpreterRuntime::throw_AbstractMethodErrorVerbose),
3461              recvKlass, method);
3462   // The call_VM checks for exception, so we should never return here.
3463   __ should_not_reach_here();
3464 
3465   __ bind(no_such_interface);
3466   // throw exception
3467   __ pop(rbx);           // pop return address (pushed by prepare_invoke)
3468   __ restore_bcp();      // rbcp must be correct for exception handler   (was destroyed)
3469   __ restore_locals();   // make sure locals pointer is correct as well (was destroyed)
3470   // Pass arguments for generating a verbose error message.
3471   if (recvKlass != rdx) {
3472     __ movq(recvKlass, rdx);
3473   }
3474   __ call_VM(noreg, CAST_FROM_FN_PTR(address, InterpreterRuntime::throw_IncompatibleClassChangeErrorVerbose),
3475              recvKlass, rax);
3476   // the call_VM checks for exception, so we should never return here.
3477   __ should_not_reach_here();
3478 }
3479 
3480 void TemplateTable::invokehandle(int byte_no) {
3481   transition(vtos, vtos);
3482   assert(byte_no == f1_byte, "use this argument");
3483   const Register rbx_method = rbx;
3484   const Register rax_mtype  = rax;
3485   const Register rcx_recv   = rcx;
3486   const Register rdx_flags  = rdx;
3487 
3488   load_resolved_method_entry_handle(rcx, rbx_method, rax_mtype, rdx_flags);
3489   prepare_invoke(rcx, rcx_recv, rdx_flags);
3490 
3491   __ verify_method_ptr(rbx_method);
3492   __ verify_oop(rcx_recv);
3493   __ null_check(rcx_recv);
3494 
3495   // rax: MethodType object (from cpool->resolved_references[f1], if necessary)
3496   // rbx: MH.invokeExact_MT method
3497 
3498   // Note:  rax_mtype is already pushed (if necessary)
3499 
3500   // FIXME: profile the LambdaForm also
3501   __ profile_final_call(rax);
3502   __ profile_arguments_type(rdx, rbx_method, rbcp, true);
3503 
3504   __ jump_from_interpreted(rbx_method, rdx);
3505 }
3506 
3507 void TemplateTable::invokedynamic(int byte_no) {
3508   transition(vtos, vtos);
3509   assert(byte_no == f1_byte, "use this argument");
3510 
3511   const Register rbx_method   = rbx;
3512   const Register rax_callsite = rax;
3513 
3514   load_invokedynamic_entry(rbx_method);
3515   // rax: CallSite object (from cpool->resolved_references[])
3516   // rbx: MH.linkToCallSite method
3517 
3518   // Note:  rax_callsite is already pushed
3519 
3520   // %%% should make a type profile for any invokedynamic that takes a ref argument
3521   // profile this call
3522   __ profile_call(rbcp);
3523   __ profile_arguments_type(rdx, rbx_method, rbcp, false);
3524 
3525   __ verify_oop(rax_callsite);
3526 
3527   __ jump_from_interpreted(rbx_method, rdx);
3528 }
3529 
3530 //-----------------------------------------------------------------------------
3531 // Allocation
3532 
3533 void TemplateTable::_new() {
3534   transition(vtos, atos);
3535   __ get_unsigned_2_byte_index_at_bcp(rdx, 1);
3536   Label slow_case;
3537   Label slow_case_no_pop;
3538   Label done;
3539   Label initialize_header;
3540 
3541   __ get_cpool_and_tags(rcx, rax);
3542 
3543   // Make sure the class we're about to instantiate has been resolved.
3544   // This is done before loading InstanceKlass to be consistent with the order
3545   // how Constant Pool is updated (see ConstantPool::klass_at_put)
3546   const int tags_offset = Array<u1>::base_offset_in_bytes();
3547   __ cmpb(Address(rax, rdx, Address::times_1, tags_offset), JVM_CONSTANT_Class);
3548   __ jcc(Assembler::notEqual, slow_case_no_pop);
3549 
3550   // get InstanceKlass
3551   __ load_resolved_klass_at_index(rcx, rcx, rdx);
3552   __ push(rcx);  // save the contexts of klass for initializing the header
3553 
3554   // make sure klass is initialized
3555   // init_state needs acquire, but x86 is TSO, and so we are already good.
3556   assert(VM_Version::supports_fast_class_init_checks(), "must support fast class initialization checks");
3557   __ clinit_barrier(rcx, nullptr /*L_fast_path*/, &slow_case);
3558 
3559   // get instance_size in InstanceKlass (scaled to a count of bytes)
3560   __ movl(rdx, Address(rcx, Klass::layout_helper_offset()));
3561   // test to see if it is malformed in some way
3562   __ testl(rdx, Klass::_lh_instance_slow_path_bit);
3563   __ jcc(Assembler::notZero, slow_case);
3564 
3565   // Allocate the instance:
3566   //  If TLAB is enabled:
3567   //    Try to allocate in the TLAB.
3568   //    If fails, go to the slow path.
3569   //    Initialize the allocation.
3570   //    Exit.
3571   //
3572   //  Go to slow path.
3573 
3574   if (UseTLAB) {
3575     __ tlab_allocate(rax, rdx, 0, rcx, rbx, slow_case);
3576     if (ZeroTLAB) {
3577       // the fields have been already cleared
3578       __ jmp(initialize_header);
3579     }
3580 
3581     // The object is initialized before the header.  If the object size is
3582     // zero, go directly to the header initialization.
3583     if (UseCompactObjectHeaders) {
3584       assert(is_aligned(oopDesc::base_offset_in_bytes(), BytesPerLong), "oop base offset must be 8-byte-aligned");
3585       __ decrement(rdx, oopDesc::base_offset_in_bytes());
3586     } else {
3587       __ decrement(rdx, sizeof(oopDesc));
3588     }
3589     __ jcc(Assembler::zero, initialize_header);
3590 
3591     // Initialize topmost object field, divide rdx by 8, check if odd and
3592     // test if zero.
3593     __ xorl(rcx, rcx);    // use zero reg to clear memory (shorter code)
3594     __ shrl(rdx, LogBytesPerLong); // divide by 2*oopSize and set carry flag if odd
3595 
3596     // rdx must have been multiple of 8
3597 #ifdef ASSERT
3598     // make sure rdx was multiple of 8
3599     Label L;
3600     // Ignore partial flag stall after shrl() since it is debug VM
3601     __ jcc(Assembler::carryClear, L);
3602     __ stop("object size is not multiple of 2 - adjust this code");
3603     __ bind(L);
3604     // rdx must be > 0, no extra check needed here
3605 #endif
3606 
3607     // initialize remaining object fields: rdx was a multiple of 8
3608     { Label loop;
3609     __ bind(loop);
3610     int header_size_bytes = oopDesc::header_size() * HeapWordSize;
3611     assert(is_aligned(header_size_bytes, BytesPerLong), "oop header size must be 8-byte-aligned");
3612     __ movptr(Address(rax, rdx, Address::times_8, header_size_bytes - 1*oopSize), rcx);
3613     __ decrement(rdx);
3614     __ jcc(Assembler::notZero, loop);
3615     }
3616 
3617     // initialize object header only.
3618     __ bind(initialize_header);
3619     if (UseCompactObjectHeaders) {
3620       __ pop(rcx);   // get saved klass back in the register.
3621       __ movptr(rbx, Address(rcx, Klass::prototype_header_offset()));
3622       __ movptr(Address(rax, oopDesc::mark_offset_in_bytes()), rbx);
3623     } else {
3624       __ movptr(Address(rax, oopDesc::mark_offset_in_bytes()),
3625                 (intptr_t)markWord::prototype().value()); // header
3626       __ pop(rcx);   // get saved klass back in the register.
3627       __ xorl(rsi, rsi); // use zero reg to clear memory (shorter code)
3628       __ store_klass_gap(rax, rsi);  // zero klass gap for compressed oops
3629       __ store_klass(rax, rcx, rscratch1);  // klass
3630     }
3631 
3632     if (DTraceAllocProbes) {
3633       // Trigger dtrace event for fastpath
3634       __ push(atos);
3635       __ call_VM_leaf(
3636            CAST_FROM_FN_PTR(address, static_cast<int (*)(oopDesc*)>(SharedRuntime::dtrace_object_alloc)), rax);
3637       __ pop(atos);
3638     }
3639 
3640     __ jmp(done);
3641   }
3642 
3643   // slow case
3644   __ bind(slow_case);
3645   __ pop(rcx);   // restore stack pointer to what it was when we came in.
3646   __ bind(slow_case_no_pop);
3647 
3648   __ get_constant_pool(c_rarg1);
3649   __ get_unsigned_2_byte_index_at_bcp(c_rarg2, 1);
3650   call_VM(rax, CAST_FROM_FN_PTR(address, InterpreterRuntime::_new), c_rarg1, c_rarg2);
3651    __ verify_oop(rax);
3652 
3653   // continue
3654   __ bind(done);
3655 }
3656 
3657 void TemplateTable::newarray() {
3658   transition(itos, atos);
3659   __ load_unsigned_byte(c_rarg1, at_bcp(1));
3660   call_VM(rax, CAST_FROM_FN_PTR(address, InterpreterRuntime::newarray),
3661           c_rarg1, rax);
3662 }
3663 
3664 void TemplateTable::anewarray() {
3665   transition(itos, atos);
3666 
3667   __ get_unsigned_2_byte_index_at_bcp(c_rarg2, 1);
3668   __ get_constant_pool(c_rarg1);
3669   call_VM(rax, CAST_FROM_FN_PTR(address, InterpreterRuntime::anewarray),
3670           c_rarg1, c_rarg2, rax);
3671 }
3672 
3673 void TemplateTable::arraylength() {
3674   transition(atos, itos);
3675   __ movl(rax, Address(rax, arrayOopDesc::length_offset_in_bytes()));
3676 }
3677 
3678 void TemplateTable::checkcast() {
3679   transition(atos, atos);
3680   Label done, is_null, ok_is_subtype, quicked, resolved;
3681   __ testptr(rax, rax); // object is in rax
3682   __ jcc(Assembler::zero, is_null);
3683 
3684   // Get cpool & tags index
3685   __ get_cpool_and_tags(rcx, rdx); // rcx=cpool, rdx=tags array
3686   __ get_unsigned_2_byte_index_at_bcp(rbx, 1); // rbx=index
3687   // See if bytecode has already been quicked
3688   __ cmpb(Address(rdx, rbx,
3689                   Address::times_1,
3690                   Array<u1>::base_offset_in_bytes()),
3691           JVM_CONSTANT_Class);
3692   __ jcc(Assembler::equal, quicked);
3693   __ push(atos); // save receiver for result, and for GC
3694   call_VM(noreg, CAST_FROM_FN_PTR(address, InterpreterRuntime::quicken_io_cc));
3695 
3696   __ get_vm_result_metadata(rax);
3697 
3698   __ pop_ptr(rdx); // restore receiver
3699   __ jmpb(resolved);
3700 
3701   // Get superklass in rax and subklass in rbx
3702   __ bind(quicked);
3703   __ mov(rdx, rax); // Save object in rdx; rax needed for subtype check
3704   __ load_resolved_klass_at_index(rax, rcx, rbx);
3705 
3706   __ bind(resolved);
3707   __ load_klass(rbx, rdx, rscratch1);
3708 
3709   // Generate subtype check.  Blows rcx, rdi.  Object in rdx.
3710   // Superklass in rax.  Subklass in rbx.
3711   __ gen_subtype_check(rbx, ok_is_subtype);
3712 
3713   // Come here on failure
3714   __ push_ptr(rdx);
3715   // object is at TOS
3716   __ jump(RuntimeAddress(Interpreter::_throw_ClassCastException_entry));
3717 
3718   // Come here on success
3719   __ bind(ok_is_subtype);
3720   __ mov(rax, rdx); // Restore object in rdx
3721 
3722   // Collect counts on whether this check-cast sees nulls a lot or not.
3723   if (ProfileInterpreter) {
3724     __ jmp(done);
3725     __ bind(is_null);
3726     __ profile_null_seen(rcx);
3727   } else {
3728     __ bind(is_null);   // same as 'done'
3729   }
3730   __ bind(done);
3731 }
3732 
3733 void TemplateTable::instanceof() {
3734   transition(atos, itos);
3735   Label done, is_null, ok_is_subtype, quicked, resolved;
3736   __ testptr(rax, rax);
3737   __ jcc(Assembler::zero, is_null);
3738 
3739   // Get cpool & tags index
3740   __ get_cpool_and_tags(rcx, rdx); // rcx=cpool, rdx=tags array
3741   __ get_unsigned_2_byte_index_at_bcp(rbx, 1); // rbx=index
3742   // See if bytecode has already been quicked
3743   __ cmpb(Address(rdx, rbx,
3744                   Address::times_1,
3745                   Array<u1>::base_offset_in_bytes()),
3746           JVM_CONSTANT_Class);
3747   __ jcc(Assembler::equal, quicked);
3748 
3749   __ push(atos); // save receiver for result, and for GC
3750   call_VM(noreg, CAST_FROM_FN_PTR(address, InterpreterRuntime::quicken_io_cc));
3751 
3752   __ get_vm_result_metadata(rax);
3753 
3754   __ pop_ptr(rdx); // restore receiver
3755   __ verify_oop(rdx);
3756   __ load_klass(rdx, rdx, rscratch1);
3757   __ jmpb(resolved);
3758 
3759   // Get superklass in rax and subklass in rdx
3760   __ bind(quicked);
3761   __ load_klass(rdx, rax, rscratch1);
3762   __ load_resolved_klass_at_index(rax, rcx, rbx);
3763 
3764   __ bind(resolved);
3765 
3766   // Generate subtype check.  Blows rcx, rdi
3767   // Superklass in rax.  Subklass in rdx.
3768   __ gen_subtype_check(rdx, ok_is_subtype);
3769 
3770   // Come here on failure
3771   __ xorl(rax, rax);
3772   __ jmpb(done);
3773   // Come here on success
3774   __ bind(ok_is_subtype);
3775   __ movl(rax, 1);
3776 
3777   // Collect counts on whether this test sees nulls a lot or not.
3778   if (ProfileInterpreter) {
3779     __ jmp(done);
3780     __ bind(is_null);
3781     __ profile_null_seen(rcx);
3782   } else {
3783     __ bind(is_null);   // same as 'done'
3784   }
3785   __ bind(done);
3786   // rax = 0: obj == nullptr or  obj is not an instanceof the specified klass
3787   // rax = 1: obj != nullptr and obj is     an instanceof the specified klass
3788 }
3789 
3790 
3791 //----------------------------------------------------------------------------------------------------
3792 // Breakpoints
3793 void TemplateTable::_breakpoint() {
3794   // Note: We get here even if we are single stepping..
3795   // jbug insists on setting breakpoints at every bytecode
3796   // even if we are in single step mode.
3797 
3798   transition(vtos, vtos);
3799 
3800   // get the unpatched byte code
3801   __ get_method(c_rarg1);
3802   __ call_VM(noreg,
3803              CAST_FROM_FN_PTR(address,
3804                               InterpreterRuntime::get_original_bytecode_at),
3805              c_rarg1, rbcp);
3806   __ mov(rbx, rax);  // why?
3807 
3808   // post the breakpoint event
3809   __ get_method(c_rarg1);
3810   __ call_VM(noreg,
3811              CAST_FROM_FN_PTR(address, InterpreterRuntime::_breakpoint),
3812              c_rarg1, rbcp);
3813 
3814   // complete the execution of original bytecode
3815   __ dispatch_only_normal(vtos);
3816 }
3817 
3818 //-----------------------------------------------------------------------------
3819 // Exceptions
3820 
3821 void TemplateTable::athrow() {
3822   transition(atos, vtos);
3823   __ null_check(rax);
3824   __ jump(RuntimeAddress(Interpreter::throw_exception_entry()));
3825 }
3826 
3827 //-----------------------------------------------------------------------------
3828 // Synchronization
3829 //
3830 // Note: monitorenter & exit are symmetric routines; which is reflected
3831 //       in the assembly code structure as well
3832 //
3833 // Stack layout:
3834 //
3835 // [expressions  ] <--- rsp               = expression stack top
3836 // ..
3837 // [expressions  ]
3838 // [monitor entry] <--- monitor block top = expression stack bot
3839 // ..
3840 // [monitor entry]
3841 // [frame data   ] <--- monitor block bot
3842 // ...
3843 // [saved rbp    ] <--- rbp
3844 void TemplateTable::monitorenter() {
3845   transition(atos, vtos);
3846 
3847   // check for null object
3848   __ null_check(rax);
3849 
3850   const Address monitor_block_top(
3851         rbp, frame::interpreter_frame_monitor_block_top_offset * wordSize);
3852   const Address monitor_block_bot(
3853         rbp, frame::interpreter_frame_initial_sp_offset * wordSize);
3854   const int entry_size = frame::interpreter_frame_monitor_size_in_bytes();
3855 
3856   Label allocated;
3857 
3858   Register rtop = c_rarg3;
3859   Register rbot = c_rarg2;
3860   Register rmon = c_rarg1;
3861 
3862   // initialize entry pointer
3863   __ xorl(rmon, rmon); // points to free slot or null
3864 
3865   // find a free slot in the monitor block (result in rmon)
3866   {
3867     Label entry, loop, exit;
3868     __ movptr(rtop, monitor_block_top); // derelativize pointer
3869     __ lea(rtop, Address(rbp, rtop, Address::times_ptr));
3870     // rtop points to current entry, starting with top-most entry
3871 
3872     __ lea(rbot, monitor_block_bot);    // points to word before bottom
3873                                         // of monitor block
3874     __ jmpb(entry);
3875 
3876     __ bind(loop);
3877     // check if current entry is used
3878     __ cmpptr(Address(rtop, BasicObjectLock::obj_offset()), NULL_WORD);
3879     // if not used then remember entry in rmon
3880     __ cmovptr(Assembler::equal, rmon, rtop);   // cmov => cmovptr
3881     // check if current entry is for same object
3882     __ cmpptr(rax, Address(rtop, BasicObjectLock::obj_offset()));
3883     // if same object then stop searching
3884     __ jccb(Assembler::equal, exit);
3885     // otherwise advance to next entry
3886     __ addptr(rtop, entry_size);
3887     __ bind(entry);
3888     // check if bottom reached
3889     __ cmpptr(rtop, rbot);
3890     // if not at bottom then check this entry
3891     __ jcc(Assembler::notEqual, loop);
3892     __ bind(exit);
3893   }
3894 
3895   __ testptr(rmon, rmon); // check if a slot has been found
3896   __ jcc(Assembler::notZero, allocated); // if found, continue with that one
3897 
3898   // allocate one if there's no free slot
3899   {
3900     Label entry, loop;
3901     // 1. compute new pointers          // rsp: old expression stack top
3902     __ movptr(rmon, monitor_block_bot); // rmon: old expression stack bottom
3903     __ lea(rmon, Address(rbp, rmon, Address::times_ptr));
3904     __ subptr(rsp, entry_size);         // move expression stack top
3905     __ subptr(rmon, entry_size);        // move expression stack bottom
3906     __ mov(rtop, rsp);                  // set start value for copy loop
3907     __ subptr(monitor_block_bot, entry_size / wordSize); // set new monitor block bottom
3908     __ jmp(entry);
3909     // 2. move expression stack contents
3910     __ bind(loop);
3911     __ movptr(rbot, Address(rtop, entry_size)); // load expression stack
3912                                                 // word from old location
3913     __ movptr(Address(rtop, 0), rbot);          // and store it at new location
3914     __ addptr(rtop, wordSize);                  // advance to next word
3915     __ bind(entry);
3916     __ cmpptr(rtop, rmon);                      // check if bottom reached
3917     __ jcc(Assembler::notEqual, loop);          // if not at bottom then
3918                                                 // copy next word
3919   }
3920 
3921   // call run-time routine
3922   // rmon: points to monitor entry
3923   __ bind(allocated);
3924 
3925   // Increment bcp to point to the next bytecode, so exception
3926   // handling for async. exceptions work correctly.
3927   // The object has already been popped from the stack, so the
3928   // expression stack looks correct.
3929   __ increment(rbcp);
3930 
3931   // store object
3932   __ movptr(Address(rmon, BasicObjectLock::obj_offset()), rax);
3933   __ lock_object(rmon);
3934 
3935   // check to make sure this monitor doesn't cause stack overflow after locking
3936   __ save_bcp();  // in case of exception
3937   __ generate_stack_overflow_check(0);
3938 
3939   // The bcp has already been incremented. Just need to dispatch to
3940   // next instruction.
3941   __ dispatch_next(vtos);
3942 }
3943 
3944 void TemplateTable::monitorexit() {
3945   transition(atos, vtos);
3946 
3947   // check for null object
3948   __ null_check(rax);
3949 
3950   const Address monitor_block_top(
3951         rbp, frame::interpreter_frame_monitor_block_top_offset * wordSize);
3952   const Address monitor_block_bot(
3953         rbp, frame::interpreter_frame_initial_sp_offset * wordSize);
3954   const int entry_size = frame::interpreter_frame_monitor_size_in_bytes();
3955 
3956   Register rtop = c_rarg1;
3957   Register rbot = c_rarg2;
3958 
3959   Label found;
3960 
3961   // find matching slot
3962   {
3963     Label entry, loop;
3964     __ movptr(rtop, monitor_block_top); // derelativize pointer
3965     __ lea(rtop, Address(rbp, rtop, Address::times_ptr));
3966     // rtop points to current entry, starting with top-most entry
3967 
3968     __ lea(rbot, monitor_block_bot);    // points to word before bottom
3969                                         // of monitor block
3970     __ jmpb(entry);
3971 
3972     __ bind(loop);
3973     // check if current entry is for same object
3974     __ cmpptr(rax, Address(rtop, BasicObjectLock::obj_offset()));
3975     // if same object then stop searching
3976     __ jcc(Assembler::equal, found);
3977     // otherwise advance to next entry
3978     __ addptr(rtop, entry_size);
3979     __ bind(entry);
3980     // check if bottom reached
3981     __ cmpptr(rtop, rbot);
3982     // if not at bottom then check this entry
3983     __ jcc(Assembler::notEqual, loop);
3984   }
3985 
3986   // error handling. Unlocking was not block-structured
3987   __ call_VM(noreg, CAST_FROM_FN_PTR(address,
3988                    InterpreterRuntime::throw_illegal_monitor_state_exception));
3989   __ should_not_reach_here();
3990 
3991   // call run-time routine
3992   __ bind(found);
3993   __ push_ptr(rax); // make sure object is on stack (contract with oopMaps)
3994   __ unlock_object(rtop);
3995   __ pop_ptr(rax); // discard object
3996 }
3997 
3998 // Wide instructions
3999 void TemplateTable::wide() {
4000   transition(vtos, vtos);
4001   __ load_unsigned_byte(rbx, at_bcp(1));
4002   ExternalAddress wtable((address)Interpreter::_wentry_point);
4003   __ jump(ArrayAddress(wtable, Address(noreg, rbx, Address::times_ptr)), rscratch1);
4004   // Note: the rbcp increment step is part of the individual wide bytecode implementations
4005 }
4006 
4007 // Multi arrays
4008 void TemplateTable::multianewarray() {
4009   transition(vtos, atos);
4010 
4011   __ load_unsigned_byte(rax, at_bcp(3)); // get number of dimensions
4012   // last dim is on top of stack; we want address of first one:
4013   // first_addr = last_addr + (ndims - 1) * stackElementSize - 1*wordsize
4014   // the latter wordSize to point to the beginning of the array.
4015   __ lea(c_rarg1, Address(rsp, rax, Interpreter::stackElementScale(), -wordSize));
4016   call_VM(rax, CAST_FROM_FN_PTR(address, InterpreterRuntime::multianewarray), c_rarg1);
4017   __ load_unsigned_byte(rbx, at_bcp(3));
4018   __ lea(rsp, Address(rsp, rbx, Interpreter::stackElementScale()));  // get rid of counts
4019 }