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(rbx);
3107           call_VM(rax, CAST_FROM_FN_PTR(address, InterpreterRuntime::read_flattened_field),
3108                   rbx, flags2, rcx);
3109           __ verify_oop(rax);
3110           __ push(atos);
3111       __ bind(rewriteFlattenable);
3112       if (rc == may_rewrite) {
3113         patch_bytecode(Bytecodes::_fast_qgetfield, bc, rbx);
3114       }
3115       __ jmp(Done);
3116     }
3117   }
3118 
3119   __ bind(notObj);
3120 
3121   if (!is_static) pop_and_check_object(obj);
3122 
3123   __ cmpl(flags, itos);
3124   __ jcc(Assembler::notEqual, notInt);
3125   // itos
3126   __ access_load_at(T_INT, IN_HEAP, rax, field, noreg, noreg);
3127   __ push(itos);
3128   // Rewrite bytecode to be faster
3129   if (!is_static && rc == may_rewrite) {
3130     patch_bytecode(Bytecodes::_fast_igetfield, bc, rbx);
3131   }
3132   __ jmp(Done);
3133 
3134   __ bind(notInt);
3135   __ cmpl(flags, ctos);
3136   __ jcc(Assembler::notEqual, notChar);
3137   // ctos
3138   __ access_load_at(T_CHAR, IN_HEAP, rax, field, noreg, noreg);
3139   __ push(ctos);
3140   // Rewrite bytecode to be faster
3141   if (!is_static && rc == may_rewrite) {
3142     patch_bytecode(Bytecodes::_fast_cgetfield, bc, rbx);
3143   }
3144   __ jmp(Done);
3145 
3146   __ bind(notChar);
3147   __ cmpl(flags, stos);
3148   __ jcc(Assembler::notEqual, notShort);
3149   // stos
3150   __ access_load_at(T_SHORT, IN_HEAP, rax, field, noreg, noreg);
3151   __ push(stos);
3152   // Rewrite bytecode to be faster
3153   if (!is_static && rc == may_rewrite) {
3154     patch_bytecode(Bytecodes::_fast_sgetfield, bc, rbx);
3155   }
3156   __ jmp(Done);
3157 
3158   __ bind(notShort);
3159   __ cmpl(flags, ltos);
3160   __ jcc(Assembler::notEqual, notLong);
3161   // ltos
3162     // Generate code as if volatile (x86_32).  There just aren't enough registers to
3163     // save that information and this code is faster than the test.
3164   __ access_load_at(T_LONG, IN_HEAP | MO_RELAXED, noreg /* ltos */, field, noreg, noreg);
3165   __ push(ltos);
3166   // Rewrite bytecode to be faster
3167   LP64_ONLY(if (!is_static && rc == may_rewrite) patch_bytecode(Bytecodes::_fast_lgetfield, bc, rbx));
3168   __ jmp(Done);
3169 
3170   __ bind(notLong);
3171   __ cmpl(flags, ftos);
3172   __ jcc(Assembler::notEqual, notFloat);
3173   // ftos
3174 
3175   __ access_load_at(T_FLOAT, IN_HEAP, noreg /* ftos */, field, noreg, noreg);
3176   __ push(ftos);
3177   // Rewrite bytecode to be faster
3178   if (!is_static && rc == may_rewrite) {
3179     patch_bytecode(Bytecodes::_fast_fgetfield, bc, rbx);
3180   }
3181   __ jmp(Done);
3182 
3183   __ bind(notFloat);
3184 #ifdef ASSERT
3185   Label notDouble;
3186   __ cmpl(flags, dtos);
3187   __ jcc(Assembler::notEqual, notDouble);
3188 #endif
3189   // dtos
3190   // MO_RELAXED: for the case of volatile field, in fact it adds no extra work for the underlying implementation
3191   __ access_load_at(T_DOUBLE, IN_HEAP | MO_RELAXED, noreg /* dtos */, field, noreg, noreg);
3192   __ push(dtos);
3193   // Rewrite bytecode to be faster
3194   if (!is_static && rc == may_rewrite) {
3195     patch_bytecode(Bytecodes::_fast_dgetfield, bc, rbx);
3196   }
3197 #ifdef ASSERT
3198   __ jmp(Done);
3199 
3200   __ bind(notDouble);
3201   __ stop("Bad state");
3202 #endif
3203 
3204   __ bind(Done);
3205   // [jk] not needed currently
3206   // volatile_barrier(Assembler::Membar_mask_bits(Assembler::LoadLoad |
3207   //                                              Assembler::LoadStore));
3208 }
3209 
3210 void TemplateTable::getfield(int byte_no) {
3211   getfield_or_static(byte_no, false);
3212 }
3213 
3214 void TemplateTable::nofast_getfield(int byte_no) {
3215   getfield_or_static(byte_no, false, may_not_rewrite);
3216 }
3217 
3218 void TemplateTable::getstatic(int byte_no) {
3219   getfield_or_static(byte_no, true);
3220 }
3221 
3222 void TemplateTable::withfield() {
3223   transition(vtos, atos);
3224 
3225   Register cache = LP64_ONLY(c_rarg1) NOT_LP64(rcx);
3226   Register index = LP64_ONLY(c_rarg2) NOT_LP64(rdx);
3227 
3228   resolve_cache_and_index(f2_byte, cache, index, sizeof(u2));
3229 
3230   call_VM(rbx, CAST_FROM_FN_PTR(address, InterpreterRuntime::withfield), cache);
3231   // new value type is returned in rbx
3232   // stack adjustement is returned in rax
3233   __ verify_oop(rbx);
3234   __ addptr(rsp, rax);
3235   __ movptr(rax, rbx);
3236 }
3237 
3238 // The registers cache and index expected to be set before call.
3239 // The function may destroy various registers, just not the cache and index registers.
3240 void TemplateTable::jvmti_post_field_mod(Register cache, Register index, bool is_static) {
3241 
3242   const Register robj = LP64_ONLY(c_rarg2)   NOT_LP64(rax);
3243   const Register RBX  = LP64_ONLY(c_rarg1)   NOT_LP64(rbx);
3244   const Register RCX  = LP64_ONLY(c_rarg3)   NOT_LP64(rcx);
3245   const Register RDX  = LP64_ONLY(rscratch1) NOT_LP64(rdx);
3246 
3247   ByteSize cp_base_offset = ConstantPoolCache::base_offset();
3248 
3249   if (JvmtiExport::can_post_field_modification()) {
3250     // Check to see if a field modification watch has been set before
3251     // we take the time to call into the VM.
3252     Label L1;
3253     assert_different_registers(cache, index, rax);
3254     __ mov32(rax, ExternalAddress((address)JvmtiExport::get_field_modification_count_addr()));
3255     __ testl(rax, rax);
3256     __ jcc(Assembler::zero, L1);
3257 
3258     __ get_cache_and_index_at_bcp(robj, RDX, 1);
3259 
3260 
3261     if (is_static) {
3262       // Life is simple.  Null out the object pointer.
3263       __ xorl(RBX, RBX);
3264 
3265     } else {
3266       // Life is harder. The stack holds the value on top, followed by
3267       // the object.  We don't know the size of the value, though; it
3268       // could be one or two words depending on its type. As a result,
3269       // we must find the type to determine where the object is.
3270 #ifndef _LP64
3271       Label two_word, valsize_known;
3272 #endif
3273       __ movl(RCX, Address(robj, RDX,
3274                            Address::times_ptr,
3275                            in_bytes(cp_base_offset +
3276                                      ConstantPoolCacheEntry::flags_offset())));
3277       NOT_LP64(__ mov(rbx, rsp));
3278       __ shrl(RCX, ConstantPoolCacheEntry::tos_state_shift);
3279 
3280       // Make sure we don't need to mask rcx after the above shift
3281       ConstantPoolCacheEntry::verify_tos_state_shift();
3282 #ifdef _LP64
3283       __ movptr(c_rarg1, at_tos_p1());  // initially assume a one word jvalue
3284       __ cmpl(c_rarg3, ltos);
3285       __ cmovptr(Assembler::equal,
3286                  c_rarg1, at_tos_p2()); // ltos (two word jvalue)
3287       __ cmpl(c_rarg3, dtos);
3288       __ cmovptr(Assembler::equal,
3289                  c_rarg1, at_tos_p2()); // dtos (two word jvalue)
3290 #else
3291       __ cmpl(rcx, ltos);
3292       __ jccb(Assembler::equal, two_word);
3293       __ cmpl(rcx, dtos);
3294       __ jccb(Assembler::equal, two_word);
3295       __ addptr(rbx, Interpreter::expr_offset_in_bytes(1)); // one word jvalue (not ltos, dtos)
3296       __ jmpb(valsize_known);
3297 
3298       __ bind(two_word);
3299       __ addptr(rbx, Interpreter::expr_offset_in_bytes(2)); // two words jvalue
3300 
3301       __ bind(valsize_known);
3302       // setup object pointer
3303       __ movptr(rbx, Address(rbx, 0));
3304 #endif
3305     }
3306     // cache entry pointer
3307     __ addptr(robj, in_bytes(cp_base_offset));
3308     __ shll(RDX, LogBytesPerWord);
3309     __ addptr(robj, RDX);
3310     // object (tos)
3311     __ mov(RCX, rsp);
3312     // c_rarg1: object pointer set up above (NULL if static)
3313     // c_rarg2: cache entry pointer
3314     // c_rarg3: jvalue object on the stack
3315     __ call_VM(noreg,
3316                CAST_FROM_FN_PTR(address,
3317                                 InterpreterRuntime::post_field_modification),
3318                RBX, robj, RCX);
3319     __ get_cache_and_index_at_bcp(cache, index, 1);
3320     __ bind(L1);
3321   }
3322 }
3323 
3324 void TemplateTable::putfield_or_static(int byte_no, bool is_static, RewriteControl rc) {
3325   transition(vtos, vtos);
3326 
3327   const Register cache = rcx;
3328   const Register index = rdx;
3329   const Register obj   = rcx;
3330   const Register off   = rbx;
3331   const Register flags = rax;
3332   const Register flags2 = rdx;
3333 
3334   resolve_cache_and_index(byte_no, cache, index, sizeof(u2));
3335   jvmti_post_field_mod(cache, index, is_static);
3336   load_field_cp_cache_entry(obj, cache, index, off, flags, is_static);
3337 
3338   // [jk] not needed currently
3339   // volatile_barrier(Assembler::Membar_mask_bits(Assembler::LoadStore |
3340   //                                              Assembler::StoreStore));
3341 
3342   Label notVolatile, Done;
3343   __ movl(rdx, flags);
3344   __ shrl(rdx, ConstantPoolCacheEntry::is_volatile_shift);
3345   __ andl(rdx, 0x1);
3346 
3347   // Check for volatile store
3348   __ testl(rdx, rdx);
3349   __ movl(flags2, flags);
3350   __ jcc(Assembler::zero, notVolatile);
3351 
3352   putfield_or_static_helper(byte_no, is_static, rc, obj, off, flags, flags2);
3353   volatile_barrier(Assembler::Membar_mask_bits(Assembler::StoreLoad |
3354                                                Assembler::StoreStore));
3355   __ jmp(Done);
3356   __ bind(notVolatile);
3357 
3358   putfield_or_static_helper(byte_no, is_static, rc, obj, off, flags, flags2);
3359 
3360   __ bind(Done);
3361 }
3362 
3363 void TemplateTable::putfield_or_static_helper(int byte_no, bool is_static, RewriteControl rc,
3364                                               Register obj, Register off, Register flags, Register flags2) {
3365 
3366   // field addresses
3367   const Address field(obj, off, Address::times_1, 0*wordSize);
3368   NOT_LP64( const Address hi(obj, off, Address::times_1, 1*wordSize);)
3369 
3370   Label notByte, notBool, notInt, notShort, notChar,
3371         notLong, notFloat, notObj, notValueType;
3372   Label Done;
3373 
3374   const Register bc    = LP64_ONLY(c_rarg3) NOT_LP64(rcx);
3375 
3376   __ shrl(flags, ConstantPoolCacheEntry::tos_state_shift);
3377 
3378   assert(btos == 0, "change code, btos != 0");
3379   __ andl(flags, ConstantPoolCacheEntry::tos_state_mask);
3380   __ jcc(Assembler::notZero, notByte);
3381 
3382   // btos
3383   {
3384     __ pop(btos);
3385     if (!is_static) pop_and_check_object(obj);
3386     __ access_store_at(T_BYTE, IN_HEAP, field, rax, noreg, noreg);
3387     if (!is_static && rc == may_rewrite) {
3388       patch_bytecode(Bytecodes::_fast_bputfield, bc, rbx, true, byte_no);
3389     }
3390     __ jmp(Done);
3391   }
3392 
3393   __ bind(notByte);
3394   __ cmpl(flags, ztos);
3395   __ jcc(Assembler::notEqual, notBool);
3396 
3397   // ztos
3398   {
3399     __ pop(ztos);
3400     if (!is_static) pop_and_check_object(obj);
3401     __ access_store_at(T_BOOLEAN, IN_HEAP, field, rax, noreg, noreg);
3402     if (!is_static && rc == may_rewrite) {
3403       patch_bytecode(Bytecodes::_fast_zputfield, bc, rbx, true, byte_no);
3404     }
3405     __ jmp(Done);
3406   }
3407 
3408   __ bind(notBool);
3409   __ cmpl(flags, atos);
3410   __ jcc(Assembler::notEqual, notObj);
3411 
3412   // atos
3413   {
3414     if (!EnableValhalla) {
3415       __ pop(atos);
3416       if (!is_static) pop_and_check_object(obj);
3417       // Store into the field
3418       do_oop_store(_masm, field, rax);
3419       if (!is_static && rc == may_rewrite) {
3420         patch_bytecode(Bytecodes::_fast_aputfield, bc, rbx, true, byte_no);
3421       }
3422       __ jmp(Done);
3423     } else {
3424       __ pop(atos);
3425       if (is_static) {
3426         Label notFlattenable, notBuffered;
3427         __ test_field_is_not_flattenable(flags2, rscratch1, notFlattenable);
3428         __ null_check(rax);
3429         __ bind(notFlattenable);
3430         do_oop_store(_masm, field, rax);
3431         __ jmp(Done);
3432       } else {
3433         Label isFlattenable, isFlattened, notBuffered, notBuffered2, rewriteNotFlattenable, rewriteFlattenable;
3434         __ test_field_is_flattenable(flags2, rscratch1, isFlattenable);
3435         // Not flattenable case, covers not flattenable values and objects
3436         pop_and_check_object(obj);
3437         // Store into the field
3438         do_oop_store(_masm, field, rax);
3439         __ bind(rewriteNotFlattenable);
3440         if (rc == may_rewrite) {
3441           patch_bytecode(Bytecodes::_fast_aputfield, bc, rbx, true, byte_no);
3442         }
3443         __ jmp(Done);
3444         // Implementation of the flattenable semantic
3445         __ bind(isFlattenable);
3446         __ null_check(rax);
3447         __ test_field_is_flattened(flags2, rscratch1, isFlattened);
3448         // Not flattened case
3449         pop_and_check_object(obj);
3450         // Store into the field
3451         do_oop_store(_masm, field, rax);
3452         __ jmp(rewriteFlattenable);
3453         __ bind(isFlattened);
3454         pop_and_check_object(obj);
3455         assert_different_registers(rax, rdx, obj, off);
3456         __ load_klass(rdx, rax);
3457         __ data_for_oop(rax, rax, rdx);
3458         __ addptr(obj, off);
3459         __ access_value_copy(IN_HEAP, rax, obj, rdx);
3460         __ bind(rewriteFlattenable);
3461         if (rc == may_rewrite) {
3462           patch_bytecode(Bytecodes::_fast_qputfield, bc, rbx, true, byte_no);
3463         }
3464         __ jmp(Done);
3465       }
3466     }
3467   }
3468 
3469   __ bind(notObj);
3470   __ cmpl(flags, itos);
3471   __ jcc(Assembler::notEqual, notInt);
3472 
3473   // itos
3474   {
3475     __ pop(itos);
3476     if (!is_static) pop_and_check_object(obj);
3477     __ access_store_at(T_INT, IN_HEAP, field, rax, noreg, noreg);
3478     if (!is_static && rc == may_rewrite) {
3479       patch_bytecode(Bytecodes::_fast_iputfield, bc, rbx, true, byte_no);
3480     }
3481     __ jmp(Done);
3482   }
3483 
3484   __ bind(notInt);
3485   __ cmpl(flags, ctos);
3486   __ jcc(Assembler::notEqual, notChar);
3487 
3488   // ctos
3489   {
3490     __ pop(ctos);
3491     if (!is_static) pop_and_check_object(obj);
3492     __ access_store_at(T_CHAR, IN_HEAP, field, rax, noreg, noreg);
3493     if (!is_static && rc == may_rewrite) {
3494       patch_bytecode(Bytecodes::_fast_cputfield, bc, rbx, true, byte_no);
3495     }
3496     __ jmp(Done);
3497   }
3498 
3499   __ bind(notChar);
3500   __ cmpl(flags, stos);
3501   __ jcc(Assembler::notEqual, notShort);
3502 
3503   // stos
3504   {
3505     __ pop(stos);
3506     if (!is_static) pop_and_check_object(obj);
3507     __ access_store_at(T_SHORT, IN_HEAP, field, rax, noreg, noreg);
3508     if (!is_static && rc == may_rewrite) {
3509       patch_bytecode(Bytecodes::_fast_sputfield, bc, rbx, true, byte_no);
3510     }
3511     __ jmp(Done);
3512   }
3513 
3514   __ bind(notShort);
3515   __ cmpl(flags, ltos);
3516   __ jcc(Assembler::notEqual, notLong);
3517 
3518   // ltos
3519   {
3520     __ pop(ltos);
3521     if (!is_static) pop_and_check_object(obj);
3522     // MO_RELAXED: generate atomic store for the case of volatile field (important for x86_32)
3523     __ access_store_at(T_LONG, IN_HEAP | MO_RELAXED, field, noreg /* ltos*/, noreg, noreg);
3524 #ifdef _LP64
3525     if (!is_static && rc == may_rewrite) {
3526       patch_bytecode(Bytecodes::_fast_lputfield, bc, rbx, true, byte_no);
3527     }
3528 #endif // _LP64
3529     __ jmp(Done);
3530   }
3531 
3532   __ bind(notLong);
3533   __ cmpl(flags, ftos);
3534   __ jcc(Assembler::notEqual, notFloat);
3535 
3536   // ftos
3537   {
3538     __ pop(ftos);
3539     if (!is_static) pop_and_check_object(obj);
3540     __ access_store_at(T_FLOAT, IN_HEAP, field, noreg /* ftos */, noreg, noreg);
3541     if (!is_static && rc == may_rewrite) {
3542       patch_bytecode(Bytecodes::_fast_fputfield, bc, rbx, true, byte_no);
3543     }
3544     __ jmp(Done);
3545   }
3546 
3547   __ bind(notFloat);
3548 #ifdef ASSERT
3549   Label notDouble;
3550   __ cmpl(flags, dtos);
3551   __ jcc(Assembler::notEqual, notDouble);
3552 #endif
3553 
3554   // dtos
3555   {
3556     __ pop(dtos);
3557     if (!is_static) pop_and_check_object(obj);
3558     // MO_RELAXED: for the case of volatile field, in fact it adds no extra work for the underlying implementation
3559     __ access_store_at(T_DOUBLE, IN_HEAP | MO_RELAXED, field, noreg /* dtos */, noreg, noreg);
3560     if (!is_static && rc == may_rewrite) {
3561       patch_bytecode(Bytecodes::_fast_dputfield, bc, rbx, true, byte_no);
3562     }
3563   }
3564 
3565 #ifdef ASSERT
3566   __ jmp(Done);
3567 
3568   __ bind(notDouble);
3569   __ stop("Bad state");
3570 #endif
3571 
3572   __ bind(Done);
3573 }
3574 
3575 void TemplateTable::putfield(int byte_no) {
3576   putfield_or_static(byte_no, false);
3577 }
3578 
3579 void TemplateTable::nofast_putfield(int byte_no) {
3580   putfield_or_static(byte_no, false, may_not_rewrite);
3581 }
3582 
3583 void TemplateTable::putstatic(int byte_no) {
3584   putfield_or_static(byte_no, true);
3585 }
3586 
3587 void TemplateTable::jvmti_post_fast_field_mod() {
3588 
3589   const Register scratch = LP64_ONLY(c_rarg3) NOT_LP64(rcx);
3590 
3591   if (JvmtiExport::can_post_field_modification()) {
3592     // Check to see if a field modification watch has been set before
3593     // we take the time to call into the VM.
3594     Label L2;
3595     __ mov32(scratch, ExternalAddress((address)JvmtiExport::get_field_modification_count_addr()));
3596     __ testl(scratch, scratch);
3597     __ jcc(Assembler::zero, L2);
3598     __ pop_ptr(rbx);                  // copy the object pointer from tos
3599     __ verify_oop(rbx);
3600     __ push_ptr(rbx);                 // put the object pointer back on tos
3601     // Save tos values before call_VM() clobbers them. Since we have
3602     // to do it for every data type, we use the saved values as the
3603     // jvalue object.
3604     switch (bytecode()) {          // load values into the jvalue object
3605     case Bytecodes::_fast_qputfield: //fall through
3606     case Bytecodes::_fast_aputfield: __ push_ptr(rax); break;
3607     case Bytecodes::_fast_bputfield: // fall through
3608     case Bytecodes::_fast_zputfield: // fall through
3609     case Bytecodes::_fast_sputfield: // fall through
3610     case Bytecodes::_fast_cputfield: // fall through
3611     case Bytecodes::_fast_iputfield: __ push_i(rax); break;
3612     case Bytecodes::_fast_dputfield: __ push(dtos); break;
3613     case Bytecodes::_fast_fputfield: __ push(ftos); break;
3614     case Bytecodes::_fast_lputfield: __ push_l(rax); break;
3615 
3616     default:
3617       ShouldNotReachHere();
3618     }
3619     __ mov(scratch, rsp);             // points to jvalue on the stack
3620     // access constant pool cache entry
3621     LP64_ONLY(__ get_cache_entry_pointer_at_bcp(c_rarg2, rax, 1));
3622     NOT_LP64(__ get_cache_entry_pointer_at_bcp(rax, rdx, 1));
3623     __ verify_oop(rbx);
3624     // rbx: object pointer copied above
3625     // c_rarg2: cache entry pointer
3626     // c_rarg3: jvalue object on the stack
3627     LP64_ONLY(__ call_VM(noreg, CAST_FROM_FN_PTR(address, InterpreterRuntime::post_field_modification), rbx, c_rarg2, c_rarg3));
3628     NOT_LP64(__ call_VM(noreg, CAST_FROM_FN_PTR(address, InterpreterRuntime::post_field_modification), rbx, rax, rcx));
3629 
3630     switch (bytecode()) {             // restore tos values
3631     case Bytecodes::_fast_qputfield: // fall through
3632     case Bytecodes::_fast_aputfield: __ pop_ptr(rax); break;
3633     case Bytecodes::_fast_bputfield: // fall through
3634     case Bytecodes::_fast_zputfield: // fall through
3635     case Bytecodes::_fast_sputfield: // fall through
3636     case Bytecodes::_fast_cputfield: // fall through
3637     case Bytecodes::_fast_iputfield: __ pop_i(rax); break;
3638     case Bytecodes::_fast_dputfield: __ pop(dtos); break;
3639     case Bytecodes::_fast_fputfield: __ pop(ftos); break;
3640     case Bytecodes::_fast_lputfield: __ pop_l(rax); break;
3641     default: break;
3642     }
3643     __ bind(L2);
3644   }
3645 }
3646 
3647 void TemplateTable::fast_storefield(TosState state) {
3648   transition(state, vtos);
3649 
3650   ByteSize base = ConstantPoolCache::base_offset();
3651 
3652   jvmti_post_fast_field_mod();
3653 
3654   // access constant pool cache
3655   __ get_cache_and_index_at_bcp(rcx, rbx, 1);
3656 
3657   // test for volatile with rdx but rdx is tos register for lputfield.
3658   __ movl(rdx, Address(rcx, rbx, Address::times_ptr,
3659                        in_bytes(base +
3660                                 ConstantPoolCacheEntry::flags_offset())));
3661 
3662   // replace index with field offset from cache entry
3663   __ movptr(rbx, Address(rcx, rbx, Address::times_ptr,
3664                          in_bytes(base + ConstantPoolCacheEntry::f2_offset())));
3665 
3666   // [jk] not needed currently
3667   // volatile_barrier(Assembler::Membar_mask_bits(Assembler::LoadStore |
3668   //                                              Assembler::StoreStore));
3669 
3670   Label notVolatile, Done;
3671   if (bytecode() == Bytecodes::_fast_qputfield) {
3672     __ movl(rscratch2, rdx);  // saving flags for isFlattened test
3673   }
3674 
3675   __ shrl(rdx, ConstantPoolCacheEntry::is_volatile_shift);
3676   __ andl(rdx, 0x1);
3677 
3678   // Get object from stack
3679   pop_and_check_object(rcx);
3680 
3681   // field address
3682   const Address field(rcx, rbx, Address::times_1);
3683 
3684   // Check for volatile store
3685   __ testl(rdx, rdx);
3686   __ jcc(Assembler::zero, notVolatile);
3687 
3688   if (bytecode() == Bytecodes::_fast_qputfield) {
3689     __ movl(rdx, rscratch2);  // restoring flags for isFlattened test
3690   }
3691   fast_storefield_helper(field, rax, rdx);
3692   volatile_barrier(Assembler::Membar_mask_bits(Assembler::StoreLoad |
3693                                                Assembler::StoreStore));
3694   __ jmp(Done);
3695   __ bind(notVolatile);
3696 
3697   if (bytecode() == Bytecodes::_fast_qputfield) {
3698     __ movl(rdx, rscratch2);  // restoring flags for isFlattened test
3699   }
3700   fast_storefield_helper(field, rax, rdx);
3701 
3702   __ bind(Done);
3703 }
3704 
3705 void TemplateTable::fast_storefield_helper(Address field, Register rax, Register flags) {
3706 
3707   // access field
3708   switch (bytecode()) {
3709   case Bytecodes::_fast_qputfield:
3710     {
3711       Label isFlattened, done;
3712       __ null_check(rax);
3713       __ test_field_is_flattened(flags, rscratch1, isFlattened);
3714       // No Flattened case
3715       do_oop_store(_masm, field, rax);
3716       __ jmp(done);
3717       __ bind(isFlattened);
3718       // Flattened case
3719       __ load_klass(rdx, rax);
3720       __ data_for_oop(rax, rax, rdx);
3721       __ lea(rcx, field);
3722       __ access_value_copy(IN_HEAP, rax, rcx, rdx);
3723       __ bind(done);
3724     }
3725     break;
3726   case Bytecodes::_fast_aputfield:
3727     {
3728       do_oop_store(_masm, field, rax);
3729     }
3730     break;
3731   case Bytecodes::_fast_lputfield:
3732 #ifdef _LP64
3733     __ access_store_at(T_LONG, IN_HEAP, field, noreg /* ltos */, noreg, noreg);
3734 #else
3735   __ stop("should not be rewritten");
3736 #endif
3737     break;
3738   case Bytecodes::_fast_iputfield:
3739     __ access_store_at(T_INT, IN_HEAP, field, rax, noreg, noreg);
3740     break;
3741   case Bytecodes::_fast_zputfield:
3742     __ access_store_at(T_BOOLEAN, IN_HEAP, field, rax, noreg, noreg);
3743     break;
3744   case Bytecodes::_fast_bputfield:
3745     __ access_store_at(T_BYTE, IN_HEAP, field, rax, noreg, noreg);
3746     break;
3747   case Bytecodes::_fast_sputfield:
3748     __ access_store_at(T_SHORT, IN_HEAP, field, rax, noreg, noreg);
3749     break;
3750   case Bytecodes::_fast_cputfield:
3751     __ access_store_at(T_CHAR, IN_HEAP, field, rax, noreg, noreg);
3752     break;
3753   case Bytecodes::_fast_fputfield:
3754     __ access_store_at(T_FLOAT, IN_HEAP, field, noreg /* ftos*/, noreg, noreg);
3755     break;
3756   case Bytecodes::_fast_dputfield:
3757     __ access_store_at(T_DOUBLE, IN_HEAP, field, noreg /* dtos*/, noreg, noreg);
3758     break;
3759   default:
3760     ShouldNotReachHere();
3761   }
3762 }
3763 
3764 void TemplateTable::fast_accessfield(TosState state) {
3765   transition(atos, state);
3766 
3767   // Do the JVMTI work here to avoid disturbing the register state below
3768   if (JvmtiExport::can_post_field_access()) {
3769     // Check to see if a field access watch has been set before we
3770     // take the time to call into the VM.
3771     Label L1;
3772     __ mov32(rcx, ExternalAddress((address) JvmtiExport::get_field_access_count_addr()));
3773     __ testl(rcx, rcx);
3774     __ jcc(Assembler::zero, L1);
3775     // access constant pool cache entry
3776     LP64_ONLY(__ get_cache_entry_pointer_at_bcp(c_rarg2, rcx, 1));
3777     NOT_LP64(__ get_cache_entry_pointer_at_bcp(rcx, rdx, 1));
3778     __ verify_oop(rax);
3779     __ push_ptr(rax);  // save object pointer before call_VM() clobbers it
3780     LP64_ONLY(__ mov(c_rarg1, rax));
3781     // c_rarg1: object pointer copied above
3782     // c_rarg2: cache entry pointer
3783     LP64_ONLY(__ call_VM(noreg, CAST_FROM_FN_PTR(address, InterpreterRuntime::post_field_access), c_rarg1, c_rarg2));
3784     NOT_LP64(__ call_VM(noreg, CAST_FROM_FN_PTR(address, InterpreterRuntime::post_field_access), rax, rcx));
3785     __ pop_ptr(rax); // restore object pointer
3786     __ bind(L1);
3787   }
3788 
3789   // access constant pool cache
3790   __ get_cache_and_index_at_bcp(rcx, rbx, 1);
3791   // replace index with field offset from cache entry
3792   // [jk] not needed currently
3793   // __ movl(rdx, Address(rcx, rbx, Address::times_8,
3794   //                      in_bytes(ConstantPoolCache::base_offset() +
3795   //                               ConstantPoolCacheEntry::flags_offset())));
3796   // __ shrl(rdx, ConstantPoolCacheEntry::is_volatile_shift);
3797   // __ andl(rdx, 0x1);
3798   //
3799   __ movptr(rdx, Address(rcx, rbx, Address::times_ptr,
3800                          in_bytes(ConstantPoolCache::base_offset() +
3801                                   ConstantPoolCacheEntry::f2_offset())));
3802 
3803   // rax: object
3804   __ verify_oop(rax);
3805   __ null_check(rax);
3806   Address field(rax, rdx, Address::times_1);
3807 
3808   // access field
3809   switch (bytecode()) {
3810   case Bytecodes::_fast_qgetfield:
3811     {
3812       Label isFlattened, nonnull, Done;
3813       __ movptr(rscratch1, Address(rcx, rbx, Address::times_ptr,
3814                                    in_bytes(ConstantPoolCache::base_offset() +
3815                                             ConstantPoolCacheEntry::flags_offset())));
3816       __ test_field_is_flattened(rscratch1, rscratch2, isFlattened);
3817         // Non-flattened field case
3818         __ movptr(rscratch1, rax);
3819         __ load_heap_oop(rax, field);
3820         __ testptr(rax, rax);
3821         __ jcc(Assembler::notZero, nonnull);
3822           __ movptr(rax, rscratch1);
3823           __ movl(rcx, Address(rcx, rbx, Address::times_ptr,
3824                              in_bytes(ConstantPoolCache::base_offset() +
3825                                       ConstantPoolCacheEntry::flags_offset())));
3826           __ andl(rcx, ConstantPoolCacheEntry::field_index_mask);
3827           __ call_VM(rax, CAST_FROM_FN_PTR(address, InterpreterRuntime::uninitialized_instance_value_field),
3828                      rax, rcx);
3829         __ bind(nonnull);
3830         __ verify_oop(rax);
3831         __ jmp(Done);
3832       __ bind(isFlattened);
3833         __ push(rdx); // save offset
3834         __ movl(rdx, Address(rcx, rbx, Address::times_ptr,
3835                            in_bytes(ConstantPoolCache::base_offset() +
3836                                     ConstantPoolCacheEntry::flags_offset())));
3837         __ andl(rdx, ConstantPoolCacheEntry::field_index_mask);
3838         __ movptr(rcx, Address(rcx, rbx, Address::times_ptr,
3839                                      in_bytes(ConstantPoolCache::base_offset() +
3840                                               ConstantPoolCacheEntry::f1_offset())));
3841         __ pop(rbx); // restore offset
3842         __ read_flattened_field(rcx, rdx, rbx, rax);
3843       __ bind(Done);
3844       __ verify_oop(rax);
3845     }
3846     break;
3847   case Bytecodes::_fast_agetfield:
3848     do_oop_load(_masm, field, rax);
3849     __ verify_oop(rax);
3850     break;
3851   case Bytecodes::_fast_lgetfield:
3852 #ifdef _LP64
3853     __ access_load_at(T_LONG, IN_HEAP, noreg /* ltos */, field, noreg, noreg);
3854 #else
3855   __ stop("should not be rewritten");
3856 #endif
3857     break;
3858   case Bytecodes::_fast_igetfield:
3859     __ access_load_at(T_INT, IN_HEAP, rax, field, noreg, noreg);
3860     break;
3861   case Bytecodes::_fast_bgetfield:
3862     __ access_load_at(T_BYTE, IN_HEAP, rax, field, noreg, noreg);
3863     break;
3864   case Bytecodes::_fast_sgetfield:
3865     __ access_load_at(T_SHORT, IN_HEAP, rax, field, noreg, noreg);
3866     break;
3867   case Bytecodes::_fast_cgetfield:
3868     __ access_load_at(T_CHAR, IN_HEAP, rax, field, noreg, noreg);
3869     break;
3870   case Bytecodes::_fast_fgetfield:
3871     __ access_load_at(T_FLOAT, IN_HEAP, noreg /* ftos */, field, noreg, noreg);
3872     break;
3873   case Bytecodes::_fast_dgetfield:
3874     __ access_load_at(T_DOUBLE, IN_HEAP, noreg /* dtos */, field, noreg, noreg);
3875     break;
3876   default:
3877     ShouldNotReachHere();
3878   }
3879   // [jk] not needed currently
3880   //   Label notVolatile;
3881   //   __ testl(rdx, rdx);
3882   //   __ jcc(Assembler::zero, notVolatile);
3883   //   __ membar(Assembler::LoadLoad);
3884   //   __ bind(notVolatile);
3885 }
3886 
3887 void TemplateTable::fast_xaccess(TosState state) {
3888   transition(vtos, state);
3889 
3890   // get receiver
3891   __ movptr(rax, aaddress(0));
3892   // access constant pool cache
3893   __ get_cache_and_index_at_bcp(rcx, rdx, 2);
3894   __ movptr(rbx,
3895             Address(rcx, rdx, Address::times_ptr,
3896                     in_bytes(ConstantPoolCache::base_offset() +
3897                              ConstantPoolCacheEntry::f2_offset())));
3898   // make sure exception is reported in correct bcp range (getfield is
3899   // next instruction)
3900   __ increment(rbcp);
3901   __ null_check(rax);
3902   const Address field = Address(rax, rbx, Address::times_1, 0*wordSize);
3903   switch (state) {
3904   case itos:
3905     __ access_load_at(T_INT, IN_HEAP, rax, field, noreg, noreg);
3906     break;
3907   case atos:
3908     do_oop_load(_masm, field, rax);
3909     __ verify_oop(rax);
3910     break;
3911   case ftos:
3912     __ access_load_at(T_FLOAT, IN_HEAP, noreg /* ftos */, field, noreg, noreg);
3913     break;
3914   default:
3915     ShouldNotReachHere();
3916   }
3917 
3918   // [jk] not needed currently
3919   // Label notVolatile;
3920   // __ movl(rdx, Address(rcx, rdx, Address::times_8,
3921   //                      in_bytes(ConstantPoolCache::base_offset() +
3922   //                               ConstantPoolCacheEntry::flags_offset())));
3923   // __ shrl(rdx, ConstantPoolCacheEntry::is_volatile_shift);
3924   // __ testl(rdx, 0x1);
3925   // __ jcc(Assembler::zero, notVolatile);
3926   // __ membar(Assembler::LoadLoad);
3927   // __ bind(notVolatile);
3928 
3929   __ decrement(rbcp);
3930 }
3931 
3932 //-----------------------------------------------------------------------------
3933 // Calls
3934 
3935 void TemplateTable::count_calls(Register method, Register temp) {
3936   // implemented elsewhere
3937   ShouldNotReachHere();
3938 }
3939 
3940 void TemplateTable::prepare_invoke(int byte_no,
3941                                    Register method,  // linked method (or i-klass)
3942                                    Register index,   // itable index, MethodType, etc.
3943                                    Register recv,    // if caller wants to see it
3944                                    Register flags    // if caller wants to test it
3945                                    ) {
3946   // determine flags
3947   const Bytecodes::Code code = bytecode();
3948   const bool is_invokeinterface  = code == Bytecodes::_invokeinterface;
3949   const bool is_invokedynamic    = code == Bytecodes::_invokedynamic;
3950   const bool is_invokehandle     = code == Bytecodes::_invokehandle;
3951   const bool is_invokevirtual    = code == Bytecodes::_invokevirtual;
3952   const bool is_invokespecial    = code == Bytecodes::_invokespecial;
3953   const bool load_receiver       = (recv  != noreg);
3954   const bool save_flags          = (flags != noreg);
3955   assert(load_receiver == (code != Bytecodes::_invokestatic && code != Bytecodes::_invokedynamic), "");
3956   assert(save_flags    == (is_invokeinterface || is_invokevirtual), "need flags for vfinal");
3957   assert(flags == noreg || flags == rdx, "");
3958   assert(recv  == noreg || recv  == rcx, "");
3959 
3960   // setup registers & access constant pool cache
3961   if (recv  == noreg)  recv  = rcx;
3962   if (flags == noreg)  flags = rdx;
3963   assert_different_registers(method, index, recv, flags);
3964 
3965   // save 'interpreter return address'
3966   __ save_bcp();
3967 
3968   load_invoke_cp_cache_entry(byte_no, method, index, flags, is_invokevirtual, false, is_invokedynamic);
3969 
3970   // maybe push appendix to arguments (just before return address)
3971   if (is_invokedynamic || is_invokehandle) {
3972     Label L_no_push;
3973     __ testl(flags, (1 << ConstantPoolCacheEntry::has_appendix_shift));
3974     __ jcc(Assembler::zero, L_no_push);
3975     // Push the appendix as a trailing parameter.
3976     // This must be done before we get the receiver,
3977     // since the parameter_size includes it.
3978     __ push(rbx);
3979     __ mov(rbx, index);
3980     __ load_resolved_reference_at_index(index, rbx);
3981     __ pop(rbx);
3982     __ push(index);  // push appendix (MethodType, CallSite, etc.)
3983     __ bind(L_no_push);
3984   }
3985 
3986   // load receiver if needed (after appendix is pushed so parameter size is correct)
3987   // Note: no return address pushed yet
3988   if (load_receiver) {
3989     __ movl(recv, flags);
3990     __ andl(recv, ConstantPoolCacheEntry::parameter_size_mask);
3991     const int no_return_pc_pushed_yet = -1;  // argument slot correction before we push return address
3992     const int receiver_is_at_end      = -1;  // back off one slot to get receiver
3993     Address recv_addr = __ argument_address(recv, no_return_pc_pushed_yet + receiver_is_at_end);
3994     __ movptr(recv, recv_addr);
3995     __ verify_oop(recv);
3996   }
3997 
3998   if (save_flags) {
3999     __ movl(rbcp, flags);
4000   }
4001 
4002   // compute return type
4003   __ shrl(flags, ConstantPoolCacheEntry::tos_state_shift);
4004   // Make sure we don't need to mask flags after the above shift
4005   ConstantPoolCacheEntry::verify_tos_state_shift();
4006   // load return address
4007   {
4008     const address table_addr = (address) Interpreter::invoke_return_entry_table_for(code);
4009     ExternalAddress table(table_addr);
4010     LP64_ONLY(__ lea(rscratch1, table));
4011     LP64_ONLY(__ movptr(flags, Address(rscratch1, flags, Address::times_ptr)));
4012     NOT_LP64(__ movptr(flags, ArrayAddress(table, Address(noreg, flags, Address::times_ptr))));
4013   }
4014 
4015   // push return address
4016   __ push(flags);
4017 
4018   // Restore flags value from the constant pool cache, and restore rsi
4019   // for later null checks.  r13 is the bytecode pointer
4020   if (save_flags) {
4021     __ movl(flags, rbcp);
4022     __ restore_bcp();
4023   }
4024 }
4025 
4026 void TemplateTable::invokevirtual_helper(Register index,
4027                                          Register recv,
4028                                          Register flags) {
4029   // Uses temporary registers rax, rdx
4030   assert_different_registers(index, recv, rax, rdx);
4031   assert(index == rbx, "");
4032   assert(recv  == rcx, "");
4033 
4034   // Test for an invoke of a final method
4035   Label notFinal;
4036   __ movl(rax, flags);
4037   __ andl(rax, (1 << ConstantPoolCacheEntry::is_vfinal_shift));
4038   __ jcc(Assembler::zero, notFinal);
4039 
4040   const Register method = index;  // method must be rbx
4041   assert(method == rbx,
4042          "Method* must be rbx for interpreter calling convention");
4043 
4044   // do the call - the index is actually the method to call
4045   // that is, f2 is a vtable index if !is_vfinal, else f2 is a Method*
4046 
4047   // It's final, need a null check here!
4048   __ null_check(recv);
4049 
4050   // profile this call
4051   __ profile_final_call(rax);
4052   __ profile_arguments_type(rax, method, rbcp, true);
4053 
4054   __ jump_from_interpreted(method, rax);
4055 
4056   __ bind(notFinal);
4057 
4058   // get receiver klass
4059   __ null_check(recv, oopDesc::klass_offset_in_bytes());
4060   __ load_klass(rax, recv);
4061 
4062   // profile this call
4063   __ profile_virtual_call(rax, rlocals, rdx);
4064   // get target Method* & entry point
4065   __ lookup_virtual_method(rax, index, method);
4066   __ profile_called_method(method, rdx, rbcp);
4067 
4068   __ profile_arguments_type(rdx, method, rbcp, true);
4069   __ jump_from_interpreted(method, rdx);
4070 }
4071 
4072 void TemplateTable::invokevirtual(int byte_no) {
4073   transition(vtos, vtos);
4074   assert(byte_no == f2_byte, "use this argument");
4075   prepare_invoke(byte_no,
4076                  rbx,    // method or vtable index
4077                  noreg,  // unused itable index
4078                  rcx, rdx); // recv, flags
4079 
4080   // rbx: index
4081   // rcx: receiver
4082   // rdx: flags
4083 
4084   invokevirtual_helper(rbx, rcx, rdx);
4085 }
4086 
4087 void TemplateTable::invokespecial(int byte_no) {
4088   transition(vtos, vtos);
4089   assert(byte_no == f1_byte, "use this argument");
4090   prepare_invoke(byte_no, rbx, noreg,  // get f1 Method*
4091                  rcx);  // get receiver also for null check
4092   __ verify_oop(rcx);
4093   __ null_check(rcx);
4094   // do the call
4095   __ profile_call(rax);
4096   __ profile_arguments_type(rax, rbx, rbcp, false);
4097   __ jump_from_interpreted(rbx, rax);
4098 }
4099 
4100 void TemplateTable::invokestatic(int byte_no) {
4101   transition(vtos, vtos);
4102   assert(byte_no == f1_byte, "use this argument");
4103   prepare_invoke(byte_no, rbx);  // get f1 Method*
4104   // do the call
4105   __ profile_call(rax);
4106   __ profile_arguments_type(rax, rbx, rbcp, false);
4107   __ jump_from_interpreted(rbx, rax);
4108 }
4109 
4110 
4111 void TemplateTable::fast_invokevfinal(int byte_no) {
4112   transition(vtos, vtos);
4113   assert(byte_no == f2_byte, "use this argument");
4114   __ stop("fast_invokevfinal not used on x86");
4115 }
4116 
4117 
4118 void TemplateTable::invokeinterface(int byte_no) {
4119   transition(vtos, vtos);
4120   assert(byte_no == f1_byte, "use this argument");
4121   prepare_invoke(byte_no, rax, rbx,  // get f1 Klass*, f2 Method*
4122                  rcx, rdx); // recv, flags
4123 
4124   // rax: reference klass (from f1) if interface method
4125   // rbx: method (from f2)
4126   // rcx: receiver
4127   // rdx: flags
4128 
4129   // First check for Object case, then private interface method,
4130   // then regular interface method.
4131 
4132   // Special case of invokeinterface called for virtual method of
4133   // java.lang.Object.  See cpCache.cpp for details.
4134   Label notObjectMethod;
4135   __ movl(rlocals, rdx);
4136   __ andl(rlocals, (1 << ConstantPoolCacheEntry::is_forced_virtual_shift));
4137   __ jcc(Assembler::zero, notObjectMethod);
4138   invokevirtual_helper(rbx, rcx, rdx);
4139   // no return from above
4140   __ bind(notObjectMethod);
4141 
4142   Label no_such_interface; // for receiver subtype check
4143   Register recvKlass; // used for exception processing
4144 
4145   // Check for private method invocation - indicated by vfinal
4146   Label notVFinal;
4147   __ movl(rlocals, rdx);
4148   __ andl(rlocals, (1 << ConstantPoolCacheEntry::is_vfinal_shift));
4149   __ jcc(Assembler::zero, notVFinal);
4150 
4151   // Get receiver klass into rlocals - also a null check
4152   __ null_check(rcx, oopDesc::klass_offset_in_bytes());
4153   __ load_klass(rlocals, rcx);
4154 
4155   Label subtype;
4156   __ check_klass_subtype(rlocals, rax, rbcp, subtype);
4157   // If we get here the typecheck failed
4158   recvKlass = rdx;
4159   __ mov(recvKlass, rlocals); // shuffle receiver class for exception use
4160   __ jmp(no_such_interface);
4161 
4162   __ bind(subtype);
4163 
4164   // do the call - rbx is actually the method to call
4165 
4166   __ profile_final_call(rdx);
4167   __ profile_arguments_type(rdx, rbx, rbcp, true);
4168 
4169   __ jump_from_interpreted(rbx, rdx);
4170   // no return from above
4171   __ bind(notVFinal);
4172 
4173   // Get receiver klass into rdx - also a null check
4174   __ restore_locals();  // restore r14
4175   __ null_check(rcx, oopDesc::klass_offset_in_bytes());
4176   __ load_klass(rdx, rcx);
4177 
4178   Label no_such_method;
4179 
4180   // Preserve method for throw_AbstractMethodErrorVerbose.
4181   __ mov(rcx, rbx);
4182   // Receiver subtype check against REFC.
4183   // Superklass in rax. Subklass in rdx. Blows rcx, rdi.
4184   __ lookup_interface_method(// inputs: rec. class, interface, itable index
4185                              rdx, rax, noreg,
4186                              // outputs: scan temp. reg, scan temp. reg
4187                              rbcp, rlocals,
4188                              no_such_interface,
4189                              /*return_method=*/false);
4190 
4191   // profile this call
4192   __ restore_bcp(); // rbcp was destroyed by receiver type check
4193   __ profile_virtual_call(rdx, rbcp, rlocals);
4194 
4195   // Get declaring interface class from method, and itable index
4196   __ load_method_holder(rax, rbx);
4197   __ movl(rbx, Address(rbx, Method::itable_index_offset()));
4198   __ subl(rbx, Method::itable_index_max);
4199   __ negl(rbx);
4200 
4201   // Preserve recvKlass for throw_AbstractMethodErrorVerbose.
4202   __ mov(rlocals, rdx);
4203   __ lookup_interface_method(// inputs: rec. class, interface, itable index
4204                              rlocals, rax, rbx,
4205                              // outputs: method, scan temp. reg
4206                              rbx, rbcp,
4207                              no_such_interface);
4208 
4209   // rbx: Method* to call
4210   // rcx: receiver
4211   // Check for abstract method error
4212   // Note: This should be done more efficiently via a throw_abstract_method_error
4213   //       interpreter entry point and a conditional jump to it in case of a null
4214   //       method.
4215   __ testptr(rbx, rbx);
4216   __ jcc(Assembler::zero, no_such_method);
4217 
4218   __ profile_called_method(rbx, rbcp, rdx);
4219   __ profile_arguments_type(rdx, rbx, rbcp, true);
4220 
4221   // do the call
4222   // rcx: receiver
4223   // rbx,: Method*
4224   __ jump_from_interpreted(rbx, rdx);
4225   __ should_not_reach_here();
4226 
4227   // exception handling code follows...
4228   // note: must restore interpreter registers to canonical
4229   //       state for exception handling to work correctly!
4230 
4231   __ bind(no_such_method);
4232   // throw exception
4233   __ pop(rbx);           // pop return address (pushed by prepare_invoke)
4234   __ restore_bcp();      // rbcp must be correct for exception handler   (was destroyed)
4235   __ restore_locals();   // make sure locals pointer is correct as well (was destroyed)
4236   // Pass arguments for generating a verbose error message.
4237 #ifdef _LP64
4238   recvKlass = c_rarg1;
4239   Register method    = c_rarg2;
4240   if (recvKlass != rdx) { __ movq(recvKlass, rdx); }
4241   if (method != rcx)    { __ movq(method, rcx);    }
4242 #else
4243   recvKlass = rdx;
4244   Register method    = rcx;
4245 #endif
4246   __ call_VM(noreg, CAST_FROM_FN_PTR(address, InterpreterRuntime::throw_AbstractMethodErrorVerbose),
4247              recvKlass, method);
4248   // The call_VM checks for exception, so we should never return here.
4249   __ should_not_reach_here();
4250 
4251   __ bind(no_such_interface);
4252   // throw exception
4253   __ pop(rbx);           // pop return address (pushed by prepare_invoke)
4254   __ restore_bcp();      // rbcp must be correct for exception handler   (was destroyed)
4255   __ restore_locals();   // make sure locals pointer is correct as well (was destroyed)
4256   // Pass arguments for generating a verbose error message.
4257   LP64_ONLY( if (recvKlass != rdx) { __ movq(recvKlass, rdx); } )
4258   __ call_VM(noreg, CAST_FROM_FN_PTR(address, InterpreterRuntime::throw_IncompatibleClassChangeErrorVerbose),
4259              recvKlass, rax);
4260   // the call_VM checks for exception, so we should never return here.
4261   __ should_not_reach_here();
4262 }
4263 
4264 void TemplateTable::invokehandle(int byte_no) {
4265   transition(vtos, vtos);
4266   assert(byte_no == f1_byte, "use this argument");
4267   const Register rbx_method = rbx;
4268   const Register rax_mtype  = rax;
4269   const Register rcx_recv   = rcx;
4270   const Register rdx_flags  = rdx;
4271 
4272   prepare_invoke(byte_no, rbx_method, rax_mtype, rcx_recv);
4273   __ verify_method_ptr(rbx_method);
4274   __ verify_oop(rcx_recv);
4275   __ null_check(rcx_recv);
4276 
4277   // rax: MethodType object (from cpool->resolved_references[f1], if necessary)
4278   // rbx: MH.invokeExact_MT method (from f2)
4279 
4280   // Note:  rax_mtype is already pushed (if necessary) by prepare_invoke
4281 
4282   // FIXME: profile the LambdaForm also
4283   __ profile_final_call(rax);
4284   __ profile_arguments_type(rdx, rbx_method, rbcp, true);
4285 
4286   __ jump_from_interpreted(rbx_method, rdx);
4287 }
4288 
4289 void TemplateTable::invokedynamic(int byte_no) {
4290   transition(vtos, vtos);
4291   assert(byte_no == f1_byte, "use this argument");
4292 
4293   const Register rbx_method   = rbx;
4294   const Register rax_callsite = rax;
4295 
4296   prepare_invoke(byte_no, rbx_method, rax_callsite);
4297 
4298   // rax: CallSite object (from cpool->resolved_references[f1])
4299   // rbx: MH.linkToCallSite method (from f2)
4300 
4301   // Note:  rax_callsite is already pushed by prepare_invoke
4302 
4303   // %%% should make a type profile for any invokedynamic that takes a ref argument
4304   // profile this call
4305   __ profile_call(rbcp);
4306   __ profile_arguments_type(rdx, rbx_method, rbcp, false);
4307 
4308   __ verify_oop(rax_callsite);
4309 
4310   __ jump_from_interpreted(rbx_method, rdx);
4311 }
4312 
4313 //-----------------------------------------------------------------------------
4314 // Allocation
4315 
4316 void TemplateTable::_new() {
4317   transition(vtos, atos);
4318   __ get_unsigned_2_byte_index_at_bcp(rdx, 1);
4319   Label slow_case;
4320   Label done;
4321 
4322   __ get_cpool_and_tags(rcx, rax);
4323 
4324   // Make sure the class we're about to instantiate has been resolved.
4325   // This is done before loading InstanceKlass to be consistent with the order
4326   // how Constant Pool is updated (see ConstantPool::klass_at_put)
4327   const int tags_offset = Array<u1>::base_offset_in_bytes();
4328   __ cmpb(Address(rax, rdx, Address::times_1, tags_offset), JVM_CONSTANT_Class);
4329   __ jcc(Assembler::notEqual, slow_case);
4330 
4331   // get InstanceKlass
4332   __ load_resolved_klass_at_index(rcx, rcx, rdx);
4333 
4334   // make sure klass is initialized & doesn't have finalizer
4335   __ cmpb(Address(rcx, InstanceKlass::init_state_offset()), InstanceKlass::fully_initialized);
4336   __ jcc(Assembler::notEqual, slow_case);
4337 
4338   __ allocate_instance(rcx, rax, rdx, rbx, true, slow_case);
4339   __ jmp(done);
4340 
4341   // slow case
4342   __ bind(slow_case);
4343 
4344   Register rarg1 = LP64_ONLY(c_rarg1) NOT_LP64(rax);
4345   Register rarg2 = LP64_ONLY(c_rarg2) NOT_LP64(rdx);
4346 
4347   __ get_constant_pool(rarg1);
4348   __ get_unsigned_2_byte_index_at_bcp(rarg2, 1);
4349   call_VM(rax, CAST_FROM_FN_PTR(address, InterpreterRuntime::_new), rarg1, rarg2);
4350    __ verify_oop(rax);
4351 
4352   // continue
4353   __ bind(done);
4354 }
4355 
4356 void TemplateTable::defaultvalue() {
4357   transition(vtos, atos);
4358 
4359   Label slow_case;
4360   Label done;
4361 
4362   __ get_unsigned_2_byte_index_at_bcp(rdx, 1);
4363   __ get_cpool_and_tags(rcx, rax);
4364 
4365   // Make sure the class we're about to instantiate has been resolved.
4366   // This is done before loading InstanceKlass to be consistent with the order
4367   // how Constant Pool is updated (see ConstantPool::klass_at_put)
4368   const int tags_offset = Array<u1>::base_offset_in_bytes();
4369   __ cmpb(Address(rax, rdx, Address::times_1, tags_offset), JVM_CONSTANT_Class);
4370   __ jcc(Assembler::notEqual, slow_case);
4371 
4372   // get InstanceKlass
4373   __ load_resolved_klass_at_index(rcx, rcx, rdx);
4374 
4375   // make sure klass is fully initialized
4376   __ cmpb(Address(rcx, InstanceKlass::init_state_offset()), InstanceKlass::fully_initialized);
4377   __ jcc(Assembler::notEqual, slow_case);
4378 
4379   // have a resolved ValueKlass in rcx, return the default value oop from it
4380   __ get_default_value_oop(rcx, rdx, rax);
4381   __ jmp(done);
4382 
4383   __ bind(slow_case);
4384 
4385   Register rarg1 = LP64_ONLY(c_rarg1) NOT_LP64(rcx);
4386   Register rarg2 = LP64_ONLY(c_rarg2) NOT_LP64(rdx);
4387 
4388   __ get_unsigned_2_byte_index_at_bcp(rarg2, 1);
4389   __ get_constant_pool(rarg1);
4390 
4391   call_VM(rax, CAST_FROM_FN_PTR(address, InterpreterRuntime::defaultvalue),
4392       rarg1, rarg2);
4393 
4394   __ bind(done);
4395   __ verify_oop(rax);
4396 }
4397 
4398 void TemplateTable::newarray() {
4399   transition(itos, atos);
4400   Register rarg1 = LP64_ONLY(c_rarg1) NOT_LP64(rdx);
4401   __ load_unsigned_byte(rarg1, at_bcp(1));
4402   call_VM(rax, CAST_FROM_FN_PTR(address, InterpreterRuntime::newarray),
4403           rarg1, rax);
4404 }
4405 
4406 void TemplateTable::anewarray() {
4407   transition(itos, atos);
4408 
4409   Register rarg1 = LP64_ONLY(c_rarg1) NOT_LP64(rcx);
4410   Register rarg2 = LP64_ONLY(c_rarg2) NOT_LP64(rdx);
4411 
4412   __ get_unsigned_2_byte_index_at_bcp(rarg2, 1);
4413   __ get_constant_pool(rarg1);
4414   call_VM(rax, CAST_FROM_FN_PTR(address, InterpreterRuntime::anewarray),
4415           rarg1, rarg2, rax);
4416 }
4417 
4418 void TemplateTable::arraylength() {
4419   transition(atos, itos);
4420   __ null_check(rax, arrayOopDesc::length_offset_in_bytes());
4421   __ movl(rax, Address(rax, arrayOopDesc::length_offset_in_bytes()));
4422 }
4423 
4424 void TemplateTable::checkcast() {
4425   transition(atos, atos);
4426   Label done, is_null, ok_is_subtype, quicked, resolved;
4427   __ testptr(rax, rax); // object is in rax
4428   __ jcc(Assembler::zero, is_null);
4429 
4430   // Get cpool & tags index
4431   __ get_cpool_and_tags(rcx, rdx); // rcx=cpool, rdx=tags array
4432   __ get_unsigned_2_byte_index_at_bcp(rbx, 1); // rbx=index
4433   // See if bytecode has already been quicked
4434   __ movzbl(rdx, Address(rdx, rbx,
4435       Address::times_1,
4436       Array<u1>::base_offset_in_bytes()));
4437   __ andl (rdx, ~JVM_CONSTANT_QDescBit);
4438   __ cmpl(rdx, JVM_CONSTANT_Class);
4439   __ jcc(Assembler::equal, quicked);
4440   __ push(atos); // save receiver for result, and for GC
4441   call_VM(noreg, CAST_FROM_FN_PTR(address, InterpreterRuntime::quicken_io_cc));
4442 
4443   // vm_result_2 has metadata result
4444 #ifndef _LP64
4445   // borrow rdi from locals
4446   __ get_thread(rdi);
4447   __ get_vm_result_2(rax, rdi);
4448   __ restore_locals();
4449 #else
4450   __ get_vm_result_2(rax, r15_thread);
4451 #endif
4452 
4453   __ pop_ptr(rdx); // restore receiver
4454   __ jmpb(resolved);
4455 
4456   // Get superklass in rax and subklass in rbx
4457   __ bind(quicked);
4458   __ mov(rdx, rax); // Save object in rdx; rax needed for subtype check
4459   __ load_resolved_klass_at_index(rax, rcx, rbx);
4460 
4461   __ bind(resolved);
4462   __ load_klass(rbx, rdx);
4463 
4464   // Generate subtype check.  Blows rcx, rdi.  Object in rdx.
4465   // Superklass in rax.  Subklass in rbx.
4466   __ gen_subtype_check(rbx, ok_is_subtype);
4467 
4468   // Come here on failure
4469   __ push_ptr(rdx);
4470   // object is at TOS
4471   __ jump(ExternalAddress(Interpreter::_throw_ClassCastException_entry));
4472 
4473   // Come here on success
4474   __ bind(ok_is_subtype);
4475   __ mov(rax, rdx); // Restore object in rdx
4476   __ jmp(done);
4477 
4478   __ bind(is_null);
4479 
4480   // Collect counts on whether this check-cast sees NULLs a lot or not.
4481   if (ProfileInterpreter) {
4482     __ profile_null_seen(rcx);
4483   }
4484 
4485   if (EnableValhalla) {
4486     // Get cpool & tags index
4487     __ get_cpool_and_tags(rcx, rdx); // rcx=cpool, rdx=tags array
4488     __ get_unsigned_2_byte_index_at_bcp(rbx, 1); // rbx=index
4489     // See if CP entry is a Q-descriptor
4490     __ movzbl(rcx, Address(rdx, rbx,
4491         Address::times_1,
4492         Array<u1>::base_offset_in_bytes()));
4493     __ andl (rcx, JVM_CONSTANT_QDescBit);
4494     __ cmpl(rcx, JVM_CONSTANT_QDescBit);
4495     __ jcc(Assembler::notEqual, done);
4496     __ jump(ExternalAddress(Interpreter::_throw_NullPointerException_entry));
4497   }
4498 
4499   __ bind(done);
4500 }
4501 
4502 void TemplateTable::instanceof() {
4503   transition(atos, itos);
4504   Label done, is_null, ok_is_subtype, quicked, resolved;
4505   __ testptr(rax, rax);
4506   __ jcc(Assembler::zero, is_null);
4507 
4508   // Get cpool & tags index
4509   __ get_cpool_and_tags(rcx, rdx); // rcx=cpool, rdx=tags array
4510   __ get_unsigned_2_byte_index_at_bcp(rbx, 1); // rbx=index
4511   // See if bytecode has already been quicked
4512   __ movzbl(rdx, Address(rdx, rbx,
4513         Address::times_1,
4514         Array<u1>::base_offset_in_bytes()));
4515   __ andl (rdx, ~JVM_CONSTANT_QDescBit);
4516   __ cmpl(rdx, JVM_CONSTANT_Class);
4517   __ jcc(Assembler::equal, quicked);
4518 
4519   __ push(atos); // save receiver for result, and for GC
4520   call_VM(noreg, CAST_FROM_FN_PTR(address, InterpreterRuntime::quicken_io_cc));
4521   // vm_result_2 has metadata result
4522 
4523 #ifndef _LP64
4524   // borrow rdi from locals
4525   __ get_thread(rdi);
4526   __ get_vm_result_2(rax, rdi);
4527   __ restore_locals();
4528 #else
4529   __ get_vm_result_2(rax, r15_thread);
4530 #endif
4531 
4532   __ pop_ptr(rdx); // restore receiver
4533   __ verify_oop(rdx);
4534   __ load_klass(rdx, rdx);
4535   __ jmpb(resolved);
4536 
4537   // Get superklass in rax and subklass in rdx
4538   __ bind(quicked);
4539   __ load_klass(rdx, rax);
4540   __ load_resolved_klass_at_index(rax, rcx, rbx);
4541 
4542   __ bind(resolved);
4543 
4544   // Generate subtype check.  Blows rcx, rdi
4545   // Superklass in rax.  Subklass in rdx.
4546   __ gen_subtype_check(rdx, ok_is_subtype);
4547 
4548   // Come here on failure
4549   __ xorl(rax, rax);
4550   __ jmpb(done);
4551   // Come here on success
4552   __ bind(ok_is_subtype);
4553   __ movl(rax, 1);
4554 
4555   // Collect counts on whether this test sees NULLs a lot or not.
4556   if (ProfileInterpreter) {
4557     __ jmp(done);
4558     __ bind(is_null);
4559     __ profile_null_seen(rcx);
4560   } else {
4561     __ bind(is_null);   // same as 'done'
4562   }
4563   __ bind(done);
4564   // rax = 0: obj == NULL or  obj is not an instanceof the specified klass
4565   // rax = 1: obj != NULL and obj is     an instanceof the specified klass
4566 }
4567 
4568 //----------------------------------------------------------------------------------------------------
4569 // Breakpoints
4570 void TemplateTable::_breakpoint() {
4571   // Note: We get here even if we are single stepping..
4572   // jbug insists on setting breakpoints at every bytecode
4573   // even if we are in single step mode.
4574 
4575   transition(vtos, vtos);
4576 
4577   Register rarg = LP64_ONLY(c_rarg1) NOT_LP64(rcx);
4578 
4579   // get the unpatched byte code
4580   __ get_method(rarg);
4581   __ call_VM(noreg,
4582              CAST_FROM_FN_PTR(address,
4583                               InterpreterRuntime::get_original_bytecode_at),
4584              rarg, rbcp);
4585   __ mov(rbx, rax);  // why?
4586 
4587   // post the breakpoint event
4588   __ get_method(rarg);
4589   __ call_VM(noreg,
4590              CAST_FROM_FN_PTR(address, InterpreterRuntime::_breakpoint),
4591              rarg, rbcp);
4592 
4593   // complete the execution of original bytecode
4594   __ dispatch_only_normal(vtos);
4595 }
4596 
4597 //-----------------------------------------------------------------------------
4598 // Exceptions
4599 
4600 void TemplateTable::athrow() {
4601   transition(atos, vtos);
4602   __ null_check(rax);
4603   __ jump(ExternalAddress(Interpreter::throw_exception_entry()));
4604 }
4605 
4606 //-----------------------------------------------------------------------------
4607 // Synchronization
4608 //
4609 // Note: monitorenter & exit are symmetric routines; which is reflected
4610 //       in the assembly code structure as well
4611 //
4612 // Stack layout:
4613 //
4614 // [expressions  ] <--- rsp               = expression stack top
4615 // ..
4616 // [expressions  ]
4617 // [monitor entry] <--- monitor block top = expression stack bot
4618 // ..
4619 // [monitor entry]
4620 // [frame data   ] <--- monitor block bot
4621 // ...
4622 // [saved rbp    ] <--- rbp
4623 void TemplateTable::monitorenter() {
4624   transition(atos, vtos);
4625 
4626   // check for NULL object
4627   __ null_check(rax);
4628 
4629   __ resolve(IS_NOT_NULL, rax);
4630 
4631   const int is_value_mask = markWord::always_locked_pattern;
4632   Label has_identity;
4633   __ movptr(rbx, Address(rax, oopDesc::mark_offset_in_bytes()));
4634   __ andptr(rbx, is_value_mask);
4635   __ cmpl(rbx, is_value_mask);
4636   __ jcc(Assembler::notEqual, has_identity);
4637   __ call_VM(noreg, CAST_FROM_FN_PTR(address,
4638                      InterpreterRuntime::throw_illegal_monitor_state_exception));
4639   __ should_not_reach_here();
4640   __ bind(has_identity);
4641 
4642   const Address monitor_block_top(
4643         rbp, frame::interpreter_frame_monitor_block_top_offset * wordSize);
4644   const Address monitor_block_bot(
4645         rbp, frame::interpreter_frame_initial_sp_offset * wordSize);
4646   const int entry_size = frame::interpreter_frame_monitor_size() * wordSize;
4647 
4648   Label allocated;
4649 
4650   Register rtop = LP64_ONLY(c_rarg3) NOT_LP64(rcx);
4651   Register rbot = LP64_ONLY(c_rarg2) NOT_LP64(rbx);
4652   Register rmon = LP64_ONLY(c_rarg1) NOT_LP64(rdx);
4653 
4654   // initialize entry pointer
4655   __ xorl(rmon, rmon); // points to free slot or NULL
4656 
4657   // find a free slot in the monitor block (result in rmon)
4658   {
4659     Label entry, loop, exit;
4660     __ movptr(rtop, monitor_block_top); // points to current entry,
4661                                         // starting with top-most entry
4662     __ lea(rbot, monitor_block_bot);    // points to word before bottom
4663                                         // of monitor block
4664     __ jmpb(entry);
4665 
4666     __ bind(loop);
4667     // check if current entry is used
4668     __ cmpptr(Address(rtop, BasicObjectLock::obj_offset_in_bytes()), (int32_t) NULL_WORD);
4669     // if not used then remember entry in rmon
4670     __ cmovptr(Assembler::equal, rmon, rtop);   // cmov => cmovptr
4671     // check if current entry is for same object
4672     __ cmpptr(rax, Address(rtop, BasicObjectLock::obj_offset_in_bytes()));
4673     // if same object then stop searching
4674     __ jccb(Assembler::equal, exit);
4675     // otherwise advance to next entry
4676     __ addptr(rtop, entry_size);
4677     __ bind(entry);
4678     // check if bottom reached
4679     __ cmpptr(rtop, rbot);
4680     // if not at bottom then check this entry
4681     __ jcc(Assembler::notEqual, loop);
4682     __ bind(exit);
4683   }
4684 
4685   __ testptr(rmon, rmon); // check if a slot has been found
4686   __ jcc(Assembler::notZero, allocated); // if found, continue with that one
4687 
4688   // allocate one if there's no free slot
4689   {
4690     Label entry, loop;
4691     // 1. compute new pointers          // rsp: old expression stack top
4692     __ movptr(rmon, monitor_block_bot); // rmon: old expression stack bottom
4693     __ subptr(rsp, entry_size);         // move expression stack top
4694     __ subptr(rmon, entry_size);        // move expression stack bottom
4695     __ mov(rtop, rsp);                  // set start value for copy loop
4696     __ movptr(monitor_block_bot, rmon); // set new monitor block bottom
4697     __ jmp(entry);
4698     // 2. move expression stack contents
4699     __ bind(loop);
4700     __ movptr(rbot, Address(rtop, entry_size)); // load expression stack
4701                                                 // word from old location
4702     __ movptr(Address(rtop, 0), rbot);          // and store it at new location
4703     __ addptr(rtop, wordSize);                  // advance to next word
4704     __ bind(entry);
4705     __ cmpptr(rtop, rmon);                      // check if bottom reached
4706     __ jcc(Assembler::notEqual, loop);          // if not at bottom then
4707                                                 // copy next word
4708   }
4709 
4710   // call run-time routine
4711   // rmon: points to monitor entry
4712   __ bind(allocated);
4713 
4714   // Increment bcp to point to the next bytecode, so exception
4715   // handling for async. exceptions work correctly.
4716   // The object has already been poped from the stack, so the
4717   // expression stack looks correct.
4718   __ increment(rbcp);
4719 
4720   // store object
4721   __ movptr(Address(rmon, BasicObjectLock::obj_offset_in_bytes()), rax);
4722   __ lock_object(rmon);
4723 
4724   // check to make sure this monitor doesn't cause stack overflow after locking
4725   __ save_bcp();  // in case of exception
4726   __ generate_stack_overflow_check(0);
4727 
4728   // The bcp has already been incremented. Just need to dispatch to
4729   // next instruction.
4730   __ dispatch_next(vtos);
4731 }
4732 
4733 void TemplateTable::monitorexit() {
4734   transition(atos, vtos);
4735 
4736   // check for NULL object
4737   __ null_check(rax);
4738 
4739   __ resolve(IS_NOT_NULL, rax);
4740 
4741   const int is_value_mask = markWord::always_locked_pattern;
4742   Label has_identity;
4743   __ movptr(rbx, Address(rax, oopDesc::mark_offset_in_bytes()));
4744   __ andptr(rbx, is_value_mask);
4745   __ cmpl(rbx, is_value_mask);
4746   __ jcc(Assembler::notEqual, has_identity);
4747   __ call_VM(noreg, CAST_FROM_FN_PTR(address,
4748                      InterpreterRuntime::throw_illegal_monitor_state_exception));
4749   __ should_not_reach_here();
4750   __ bind(has_identity);
4751 
4752   const Address monitor_block_top(
4753         rbp, frame::interpreter_frame_monitor_block_top_offset * wordSize);
4754   const Address monitor_block_bot(
4755         rbp, frame::interpreter_frame_initial_sp_offset * wordSize);
4756   const int entry_size = frame::interpreter_frame_monitor_size() * wordSize;
4757 
4758   Register rtop = LP64_ONLY(c_rarg1) NOT_LP64(rdx);
4759   Register rbot = LP64_ONLY(c_rarg2) NOT_LP64(rbx);
4760 
4761   Label found;
4762 
4763   // find matching slot
4764   {
4765     Label entry, loop;
4766     __ movptr(rtop, monitor_block_top); // points to current entry,
4767                                         // starting with top-most entry
4768     __ lea(rbot, monitor_block_bot);    // points to word before bottom
4769                                         // of monitor block
4770     __ jmpb(entry);
4771 
4772     __ bind(loop);
4773     // check if current entry is for same object
4774     __ cmpptr(rax, Address(rtop, BasicObjectLock::obj_offset_in_bytes()));
4775     // if same object then stop searching
4776     __ jcc(Assembler::equal, found);
4777     // otherwise advance to next entry
4778     __ addptr(rtop, entry_size);
4779     __ bind(entry);
4780     // check if bottom reached
4781     __ cmpptr(rtop, rbot);
4782     // if not at bottom then check this entry
4783     __ jcc(Assembler::notEqual, loop);
4784   }
4785 
4786   // error handling. Unlocking was not block-structured
4787   __ call_VM(noreg, CAST_FROM_FN_PTR(address,
4788                    InterpreterRuntime::throw_illegal_monitor_state_exception));
4789   __ should_not_reach_here();
4790 
4791   // call run-time routine
4792   __ bind(found);
4793   __ push_ptr(rax); // make sure object is on stack (contract with oopMaps)
4794   __ unlock_object(rtop);
4795   __ pop_ptr(rax); // discard object
4796 }
4797 
4798 // Wide instructions
4799 void TemplateTable::wide() {
4800   transition(vtos, vtos);
4801   __ load_unsigned_byte(rbx, at_bcp(1));
4802   ExternalAddress wtable((address)Interpreter::_wentry_point);
4803   __ jump(ArrayAddress(wtable, Address(noreg, rbx, Address::times_ptr)));
4804   // Note: the rbcp increment step is part of the individual wide bytecode implementations
4805 }
4806 
4807 // Multi arrays
4808 void TemplateTable::multianewarray() {
4809   transition(vtos, atos);
4810 
4811   Register rarg = LP64_ONLY(c_rarg1) NOT_LP64(rax);
4812   __ load_unsigned_byte(rax, at_bcp(3)); // get number of dimensions
4813   // last dim is on top of stack; we want address of first one:
4814   // first_addr = last_addr + (ndims - 1) * stackElementSize - 1*wordsize
4815   // the latter wordSize to point to the beginning of the array.
4816   __ lea(rarg, Address(rsp, rax, Interpreter::stackElementScale(), -wordSize));
4817   call_VM(rax, CAST_FROM_FN_PTR(address, InterpreterRuntime::multianewarray), rarg);
4818   __ load_unsigned_byte(rbx, at_bcp(3));
4819   __ lea(rsp, Address(rsp, rbx, Interpreter::stackElementScale()));  // get rid of counts
4820 }