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