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