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