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