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