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