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