1 /*
   2  * Copyright (c) 1997, 2021, 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 "gc/shared/collectedHeap.hpp"
  29 #include "gc/shared/tlab_globals.hpp"
  30 #include "interpreter/interpreter.hpp"
  31 #include "interpreter/interpreterRuntime.hpp"
  32 #include "interpreter/interp_masm.hpp"
  33 #include "interpreter/templateTable.hpp"
  34 #include "memory/universe.hpp"
  35 #include "oops/methodData.hpp"
  36 #include "oops/objArrayKlass.hpp"
  37 #include "oops/oop.inline.hpp"
  38 #include "oops/inlineKlass.hpp"
  39 #include "prims/jvmtiExport.hpp"
  40 #include "prims/methodHandles.hpp"
  41 #include "runtime/frame.inline.hpp"
  42 #include "runtime/safepointMechanism.hpp"
  43 #include "runtime/sharedRuntime.hpp"
  44 #include "runtime/stubRoutines.hpp"
  45 #include "runtime/synchronizer.hpp"
  46 #include "utilities/macros.hpp"
  47 
  48 #define __ Disassembler::hook<InterpreterMacroAssembler>(__FILE__, __LINE__, _masm)->
  49 
  50 // Global Register Names
  51 static const Register rbcp     = LP64_ONLY(r13) NOT_LP64(rsi);
  52 static const Register rlocals  = LP64_ONLY(r14) NOT_LP64(rdi);
  53 
  54 // Address Computation: local variables
  55 static inline Address iaddress(int n) {
  56   return Address(rlocals, Interpreter::local_offset_in_bytes(n));
  57 }
  58 
  59 static inline Address laddress(int n) {
  60   return iaddress(n + 1);
  61 }
  62 
  63 #ifndef _LP64
  64 static inline Address haddress(int n) {
  65   return iaddress(n + 0);
  66 }
  67 #endif
  68 
  69 static inline Address faddress(int n) {
  70   return iaddress(n);
  71 }
  72 
  73 static inline Address daddress(int n) {
  74   return laddress(n);
  75 }
  76 
  77 static inline Address aaddress(int n) {
  78   return iaddress(n);
  79 }
  80 
  81 static inline Address iaddress(Register r) {
  82   return Address(rlocals, r, Address::times_ptr);
  83 }
  84 
  85 static inline Address laddress(Register r) {
  86   return Address(rlocals, r, Address::times_ptr, Interpreter::local_offset_in_bytes(1));
  87 }
  88 
  89 #ifndef _LP64
  90 static inline Address haddress(Register r)       {
  91   return Address(rlocals, r, Interpreter::stackElementScale(), Interpreter::local_offset_in_bytes(0));
  92 }
  93 #endif
  94 
  95 static inline Address faddress(Register r) {
  96   return iaddress(r);
  97 }
  98 
  99 static inline Address daddress(Register r) {
 100   return laddress(r);
 101 }
 102 
 103 static inline Address aaddress(Register r) {
 104   return iaddress(r);
 105 }
 106 
 107 
 108 // expression stack
 109 // (Note: Must not use symmetric equivalents at_rsp_m1/2 since they store
 110 // data beyond the rsp which is potentially unsafe in an MT environment;
 111 // an interrupt may overwrite that data.)
 112 static inline Address at_rsp   () {
 113   return Address(rsp, 0);
 114 }
 115 
 116 // At top of Java expression stack which may be different than esp().  It
 117 // isn't for category 1 objects.
 118 static inline Address at_tos   () {
 119   return Address(rsp,  Interpreter::expr_offset_in_bytes(0));
 120 }
 121 
 122 static inline Address at_tos_p1() {
 123   return Address(rsp,  Interpreter::expr_offset_in_bytes(1));
 124 }
 125 
 126 static inline Address at_tos_p2() {
 127   return Address(rsp,  Interpreter::expr_offset_in_bytes(2));
 128 }
 129 
 130 // Condition conversion
 131 static Assembler::Condition j_not(TemplateTable::Condition cc) {
 132   switch (cc) {
 133   case TemplateTable::equal        : return Assembler::notEqual;
 134   case TemplateTable::not_equal    : return Assembler::equal;
 135   case TemplateTable::less         : return Assembler::greaterEqual;
 136   case TemplateTable::less_equal   : return Assembler::greater;
 137   case TemplateTable::greater      : return Assembler::lessEqual;
 138   case TemplateTable::greater_equal: return Assembler::less;
 139   }
 140   ShouldNotReachHere();
 141   return Assembler::zero;
 142 }
 143 
 144 
 145 
 146 // Miscelaneous helper routines
 147 // Store an oop (or NULL) at the address described by obj.
 148 // If val == noreg this means store a NULL
 149 
 150 
 151 static void do_oop_store(InterpreterMacroAssembler* _masm,
 152                          Address dst,
 153                          Register val,
 154                          DecoratorSet decorators = 0) {
 155   assert(val == noreg || val == rax, "parameter is just for looks");
 156   __ store_heap_oop(dst, val, rdx, rbx, noreg, decorators);
 157 }
 158 
 159 static void do_oop_load(InterpreterMacroAssembler* _masm,
 160                         Address src,
 161                         Register dst,
 162                         DecoratorSet decorators = 0) {
 163   __ load_heap_oop(dst, src, rdx, rbx, decorators);
 164 }
 165 
 166 Address TemplateTable::at_bcp(int offset) {
 167   assert(_desc->uses_bcp(), "inconsistent uses_bcp information");
 168   return Address(rbcp, offset);
 169 }
 170 
 171 
 172 void TemplateTable::patch_bytecode(Bytecodes::Code bc, Register bc_reg,
 173                                    Register temp_reg, bool load_bc_into_bc_reg/*=true*/,
 174                                    int byte_no) {
 175   if (!RewriteBytecodes)  return;
 176   Label L_patch_done;
 177 
 178   switch (bc) {
 179   case Bytecodes::_fast_qputfield:
 180   case Bytecodes::_fast_aputfield:
 181   case Bytecodes::_fast_bputfield:
 182   case Bytecodes::_fast_zputfield:
 183   case Bytecodes::_fast_cputfield:
 184   case Bytecodes::_fast_dputfield:
 185   case Bytecodes::_fast_fputfield:
 186   case Bytecodes::_fast_iputfield:
 187   case Bytecodes::_fast_lputfield:
 188   case Bytecodes::_fast_sputfield:
 189     {
 190       // We skip bytecode quickening for putfield instructions when
 191       // the put_code written to the constant pool cache is zero.
 192       // This is required so that every execution of this instruction
 193       // calls out to InterpreterRuntime::resolve_get_put to do
 194       // additional, required work.
 195       assert(byte_no == f1_byte || byte_no == f2_byte, "byte_no out of range");
 196       assert(load_bc_into_bc_reg, "we use bc_reg as temp");
 197       __ get_cache_and_index_and_bytecode_at_bcp(temp_reg, bc_reg, temp_reg, byte_no, 1);
 198       __ movl(bc_reg, bc);
 199       __ cmpl(temp_reg, (int) 0);
 200       __ jcc(Assembler::zero, L_patch_done);  // don't patch
 201     }
 202     break;
 203   default:
 204     assert(byte_no == -1, "sanity");
 205     // the pair bytecodes have already done the load.
 206     if (load_bc_into_bc_reg) {
 207       __ movl(bc_reg, bc);
 208     }
 209   }
 210 
 211   if (JvmtiExport::can_post_breakpoint()) {
 212     Label L_fast_patch;
 213     // if a breakpoint is present we can't rewrite the stream directly
 214     __ movzbl(temp_reg, at_bcp(0));
 215     __ cmpl(temp_reg, Bytecodes::_breakpoint);
 216     __ jcc(Assembler::notEqual, L_fast_patch);
 217     __ get_method(temp_reg);
 218     // Let breakpoint table handling rewrite to quicker bytecode
 219     __ call_VM(noreg, CAST_FROM_FN_PTR(address, InterpreterRuntime::set_original_bytecode_at), temp_reg, rbcp, bc_reg);
 220 #ifndef ASSERT
 221     __ jmpb(L_patch_done);
 222 #else
 223     __ jmp(L_patch_done);
 224 #endif
 225     __ bind(L_fast_patch);
 226   }
 227 
 228 #ifdef ASSERT
 229   Label L_okay;
 230   __ load_unsigned_byte(temp_reg, at_bcp(0));
 231   __ cmpl(temp_reg, (int) Bytecodes::java_code(bc));
 232   __ jcc(Assembler::equal, L_okay);
 233   __ cmpl(temp_reg, bc_reg);
 234   __ jcc(Assembler::equal, L_okay);
 235   __ stop("patching the wrong bytecode");
 236   __ bind(L_okay);
 237 #endif
 238 
 239   // patch bytecode
 240   __ movb(at_bcp(0), bc_reg);
 241   __ bind(L_patch_done);
 242 }
 243 // Individual instructions
 244 
 245 
 246 void TemplateTable::nop() {
 247   transition(vtos, vtos);
 248   // nothing to do
 249 }
 250 
 251 void TemplateTable::shouldnotreachhere() {
 252   transition(vtos, vtos);
 253   __ stop("shouldnotreachhere bytecode");
 254 }
 255 
 256 void TemplateTable::aconst_null() {
 257   transition(vtos, atos);
 258   __ xorl(rax, rax);
 259 }
 260 
 261 void TemplateTable::iconst(int value) {
 262   transition(vtos, itos);
 263   if (value == 0) {
 264     __ xorl(rax, rax);
 265   } else {
 266     __ movl(rax, value);
 267   }
 268 }
 269 
 270 void TemplateTable::lconst(int value) {
 271   transition(vtos, ltos);
 272   if (value == 0) {
 273     __ xorl(rax, rax);
 274   } else {
 275     __ movl(rax, value);
 276   }
 277 #ifndef _LP64
 278   assert(value >= 0, "check this code");
 279   __ xorptr(rdx, rdx);
 280 #endif
 281 }
 282 
 283 
 284 
 285 void TemplateTable::fconst(int value) {
 286   transition(vtos, ftos);
 287   if (UseSSE >= 1) {
 288     static float one = 1.0f, two = 2.0f;
 289     switch (value) {
 290     case 0:
 291       __ xorps(xmm0, xmm0);
 292       break;
 293     case 1:
 294       __ movflt(xmm0, ExternalAddress((address) &one));
 295       break;
 296     case 2:
 297       __ movflt(xmm0, ExternalAddress((address) &two));
 298       break;
 299     default:
 300       ShouldNotReachHere();
 301       break;
 302     }
 303   } else {
 304 #ifdef _LP64
 305     ShouldNotReachHere();
 306 #else
 307            if (value == 0) { __ fldz();
 308     } else if (value == 1) { __ fld1();
 309     } else if (value == 2) { __ fld1(); __ fld1(); __ faddp(); // should do a better solution here
 310     } else                 { ShouldNotReachHere();
 311     }
 312 #endif // _LP64
 313   }
 314 }
 315 
 316 void TemplateTable::dconst(int value) {
 317   transition(vtos, dtos);
 318   if (UseSSE >= 2) {
 319     static double one = 1.0;
 320     switch (value) {
 321     case 0:
 322       __ xorpd(xmm0, xmm0);
 323       break;
 324     case 1:
 325       __ movdbl(xmm0, ExternalAddress((address) &one));
 326       break;
 327     default:
 328       ShouldNotReachHere();
 329       break;
 330     }
 331   } else {
 332 #ifdef _LP64
 333     ShouldNotReachHere();
 334 #else
 335            if (value == 0) { __ fldz();
 336     } else if (value == 1) { __ fld1();
 337     } else                 { ShouldNotReachHere();
 338     }
 339 #endif
 340   }
 341 }
 342 
 343 void TemplateTable::bipush() {
 344   transition(vtos, itos);
 345   __ load_signed_byte(rax, at_bcp(1));
 346 }
 347 
 348 void TemplateTable::sipush() {
 349   transition(vtos, itos);
 350   __ load_unsigned_short(rax, at_bcp(1));
 351   __ bswapl(rax);
 352   __ sarl(rax, 16);
 353 }
 354 
 355 void TemplateTable::ldc(bool wide) {
 356   transition(vtos, vtos);
 357   Register rarg = NOT_LP64(rcx) LP64_ONLY(c_rarg1);
 358   Label call_ldc, notFloat, notClass, notInt, Done;
 359 
 360   if (wide) {
 361     __ get_unsigned_2_byte_index_at_bcp(rbx, 1);
 362   } else {
 363     __ load_unsigned_byte(rbx, at_bcp(1));
 364   }
 365 
 366   __ get_cpool_and_tags(rcx, rax);
 367   const int base_offset = ConstantPool::header_size() * wordSize;
 368   const int tags_offset = Array<u1>::base_offset_in_bytes();
 369 
 370   // get type
 371   __ movzbl(rdx, Address(rax, rbx, Address::times_1, tags_offset));
 372   __ andl(rdx, ~JVM_CONSTANT_QDescBit);
 373 
 374   // unresolved class - get the resolved class
 375   __ cmpl(rdx, JVM_CONSTANT_UnresolvedClass);
 376   __ jccb(Assembler::equal, call_ldc);
 377 
 378   // unresolved class in error state - call into runtime to throw the error
 379   // from the first resolution attempt
 380   __ cmpl(rdx, JVM_CONSTANT_UnresolvedClassInError);
 381   __ jccb(Assembler::equal, call_ldc);
 382 
 383   // resolved class - need to call vm to get java mirror of the class
 384   __ cmpl(rdx, JVM_CONSTANT_Class);
 385   __ jcc(Assembler::notEqual, notClass);
 386 
 387   __ bind(call_ldc);
 388 
 389   __ movl(rarg, wide);
 390   call_VM(rax, CAST_FROM_FN_PTR(address, InterpreterRuntime::ldc), rarg);
 391 
 392   __ push(atos);
 393   __ jmp(Done);
 394 
 395   __ bind(notClass);
 396   __ cmpl(rdx, JVM_CONSTANT_Float);
 397   __ jccb(Assembler::notEqual, notFloat);
 398 
 399   // ftos
 400   __ load_float(Address(rcx, rbx, Address::times_ptr, base_offset));
 401   __ push(ftos);
 402   __ jmp(Done);
 403 
 404   __ bind(notFloat);
 405   __ cmpl(rdx, JVM_CONSTANT_Integer);
 406   __ jccb(Assembler::notEqual, notInt);
 407 
 408   // itos
 409   __ movl(rax, Address(rcx, rbx, Address::times_ptr, base_offset));
 410   __ push(itos);
 411   __ jmp(Done);
 412 
 413   // assume the tag is for condy; if not, the VM runtime will tell us
 414   __ bind(notInt);
 415   condy_helper(Done);
 416 
 417   __ bind(Done);
 418 }
 419 
 420 // Fast path for caching oop constants.
 421 void TemplateTable::fast_aldc(bool wide) {
 422   transition(vtos, atos);
 423 
 424   Register result = rax;
 425   Register tmp = rdx;
 426   Register rarg = NOT_LP64(rcx) LP64_ONLY(c_rarg1);
 427   int index_size = wide ? sizeof(u2) : sizeof(u1);
 428 
 429   Label resolved;
 430 
 431   // We are resolved if the resolved reference cache entry contains a
 432   // non-null object (String, MethodType, etc.)
 433   assert_different_registers(result, tmp);
 434   __ get_cache_index_at_bcp(tmp, 1, index_size);
 435   __ load_resolved_reference_at_index(result, tmp);
 436   __ testptr(result, result);
 437   __ jcc(Assembler::notZero, resolved);
 438 
 439   address entry = CAST_FROM_FN_PTR(address, InterpreterRuntime::resolve_ldc);
 440 
 441   // first time invocation - must resolve first
 442   __ movl(rarg, (int)bytecode());
 443   __ call_VM(result, entry, rarg);
 444   __ bind(resolved);
 445 
 446   { // Check for the null sentinel.
 447     // If we just called the VM, it already did the mapping for us,
 448     // but it's harmless to retry.
 449     Label notNull;
 450     ExternalAddress null_sentinel((address)Universe::the_null_sentinel_addr());
 451     __ movptr(tmp, null_sentinel);
 452     __ resolve_oop_handle(tmp);
 453     __ cmpoop(tmp, result);
 454     __ jccb(Assembler::notEqual, notNull);
 455     __ xorptr(result, result);  // NULL object reference
 456     __ bind(notNull);
 457   }
 458 
 459   if (VerifyOops) {
 460     __ verify_oop(result);
 461   }
 462 }
 463 
 464 void TemplateTable::ldc2_w() {
 465   transition(vtos, vtos);
 466   Label notDouble, notLong, Done;
 467   __ get_unsigned_2_byte_index_at_bcp(rbx, 1);
 468 
 469   __ get_cpool_and_tags(rcx, rax);
 470   const int base_offset = ConstantPool::header_size() * wordSize;
 471   const int tags_offset = Array<u1>::base_offset_in_bytes();
 472 
 473   // get type
 474   __ movzbl(rdx, Address(rax, rbx, Address::times_1, tags_offset));
 475   __ cmpl(rdx, JVM_CONSTANT_Double);
 476   __ jccb(Assembler::notEqual, notDouble);
 477 
 478   // dtos
 479   __ load_double(Address(rcx, rbx, Address::times_ptr, base_offset));
 480   __ push(dtos);
 481 
 482   __ jmp(Done);
 483   __ bind(notDouble);
 484   __ cmpl(rdx, JVM_CONSTANT_Long);
 485   __ jccb(Assembler::notEqual, notLong);
 486 
 487   // ltos
 488   __ movptr(rax, Address(rcx, rbx, Address::times_ptr, base_offset + 0 * wordSize));
 489   NOT_LP64(__ movptr(rdx, Address(rcx, rbx, Address::times_ptr, base_offset + 1 * wordSize)));
 490   __ push(ltos);
 491   __ jmp(Done);
 492 
 493   __ bind(notLong);
 494   condy_helper(Done);
 495 
 496   __ bind(Done);
 497 }
 498 
 499 void TemplateTable::condy_helper(Label& Done) {
 500   const Register obj = rax;
 501   const Register off = rbx;
 502   const Register flags = rcx;
 503   const Register rarg = NOT_LP64(rcx) LP64_ONLY(c_rarg1);
 504   __ movl(rarg, (int)bytecode());
 505   call_VM(obj, CAST_FROM_FN_PTR(address, InterpreterRuntime::resolve_ldc), rarg);
 506 #ifndef _LP64
 507   // borrow rdi from locals
 508   __ get_thread(rdi);
 509   __ get_vm_result_2(flags, rdi);
 510   __ restore_locals();
 511 #else
 512   __ get_vm_result_2(flags, r15_thread);
 513 #endif
 514   // VMr = obj = base address to find primitive value to push
 515   // VMr2 = flags = (tos, off) using format of CPCE::_flags
 516   __ movl(off, flags);
 517   __ andl(off, ConstantPoolCacheEntry::field_index_mask);
 518   const Address field(obj, off, Address::times_1, 0*wordSize);
 519 
 520   // What sort of thing are we loading?
 521   __ shrl(flags, ConstantPoolCacheEntry::tos_state_shift);
 522   __ andl(flags, ConstantPoolCacheEntry::tos_state_mask);
 523 
 524   switch (bytecode()) {
 525   case Bytecodes::_ldc:
 526   case Bytecodes::_ldc_w:
 527     {
 528       // tos in (itos, ftos, stos, btos, ctos, ztos)
 529       Label notInt, notFloat, notShort, notByte, notChar, notBool;
 530       __ cmpl(flags, itos);
 531       __ jcc(Assembler::notEqual, notInt);
 532       // itos
 533       __ movl(rax, field);
 534       __ push(itos);
 535       __ jmp(Done);
 536 
 537       __ bind(notInt);
 538       __ cmpl(flags, ftos);
 539       __ jcc(Assembler::notEqual, notFloat);
 540       // ftos
 541       __ load_float(field);
 542       __ push(ftos);
 543       __ jmp(Done);
 544 
 545       __ bind(notFloat);
 546       __ cmpl(flags, stos);
 547       __ jcc(Assembler::notEqual, notShort);
 548       // stos
 549       __ load_signed_short(rax, field);
 550       __ push(stos);
 551       __ jmp(Done);
 552 
 553       __ bind(notShort);
 554       __ cmpl(flags, btos);
 555       __ jcc(Assembler::notEqual, notByte);
 556       // btos
 557       __ load_signed_byte(rax, field);
 558       __ push(btos);
 559       __ jmp(Done);
 560 
 561       __ bind(notByte);
 562       __ cmpl(flags, ctos);
 563       __ jcc(Assembler::notEqual, notChar);
 564       // ctos
 565       __ load_unsigned_short(rax, field);
 566       __ push(ctos);
 567       __ jmp(Done);
 568 
 569       __ bind(notChar);
 570       __ cmpl(flags, ztos);
 571       __ jcc(Assembler::notEqual, notBool);
 572       // ztos
 573       __ load_signed_byte(rax, field);
 574       __ push(ztos);
 575       __ jmp(Done);
 576 
 577       __ bind(notBool);
 578       break;
 579     }
 580 
 581   case Bytecodes::_ldc2_w:
 582     {
 583       Label notLong, notDouble;
 584       __ cmpl(flags, ltos);
 585       __ jcc(Assembler::notEqual, notLong);
 586       // ltos
 587       // Loading high word first because movptr clobbers rax
 588       NOT_LP64(__ movptr(rdx, field.plus_disp(4)));
 589       __ movptr(rax, field);
 590       __ push(ltos);
 591       __ jmp(Done);
 592 
 593       __ bind(notLong);
 594       __ cmpl(flags, dtos);
 595       __ jcc(Assembler::notEqual, notDouble);
 596       // dtos
 597       __ load_double(field);
 598       __ push(dtos);
 599       __ jmp(Done);
 600 
 601       __ bind(notDouble);
 602       break;
 603     }
 604 
 605   default:
 606     ShouldNotReachHere();
 607   }
 608 
 609   __ stop("bad ldc/condy");
 610 }
 611 
 612 void TemplateTable::locals_index(Register reg, int offset) {
 613   __ load_unsigned_byte(reg, at_bcp(offset));
 614   __ negptr(reg);
 615 }
 616 
 617 void TemplateTable::iload() {
 618   iload_internal();
 619 }
 620 
 621 void TemplateTable::nofast_iload() {
 622   iload_internal(may_not_rewrite);
 623 }
 624 
 625 void TemplateTable::iload_internal(RewriteControl rc) {
 626   transition(vtos, itos);
 627   if (RewriteFrequentPairs && rc == may_rewrite) {
 628     Label rewrite, done;
 629     const Register bc = LP64_ONLY(c_rarg3) NOT_LP64(rcx);
 630     LP64_ONLY(assert(rbx != bc, "register damaged"));
 631 
 632     // get next byte
 633     __ load_unsigned_byte(rbx,
 634                           at_bcp(Bytecodes::length_for(Bytecodes::_iload)));
 635     // if _iload, wait to rewrite to iload2.  We only want to rewrite the
 636     // last two iloads in a pair.  Comparing against fast_iload means that
 637     // the next bytecode is neither an iload or a caload, and therefore
 638     // an iload pair.
 639     __ cmpl(rbx, Bytecodes::_iload);
 640     __ jcc(Assembler::equal, done);
 641 
 642     __ cmpl(rbx, Bytecodes::_fast_iload);
 643     __ movl(bc, Bytecodes::_fast_iload2);
 644 
 645     __ jccb(Assembler::equal, rewrite);
 646 
 647     // if _caload, rewrite to fast_icaload
 648     __ cmpl(rbx, Bytecodes::_caload);
 649     __ movl(bc, Bytecodes::_fast_icaload);
 650     __ jccb(Assembler::equal, rewrite);
 651 
 652     // rewrite so iload doesn't check again.
 653     __ movl(bc, Bytecodes::_fast_iload);
 654 
 655     // rewrite
 656     // bc: fast bytecode
 657     __ bind(rewrite);
 658     patch_bytecode(Bytecodes::_iload, bc, rbx, false);
 659     __ bind(done);
 660   }
 661 
 662   // Get the local value into tos
 663   locals_index(rbx);
 664   __ movl(rax, iaddress(rbx));
 665 }
 666 
 667 void TemplateTable::fast_iload2() {
 668   transition(vtos, itos);
 669   locals_index(rbx);
 670   __ movl(rax, iaddress(rbx));
 671   __ push(itos);
 672   locals_index(rbx, 3);
 673   __ movl(rax, iaddress(rbx));
 674 }
 675 
 676 void TemplateTable::fast_iload() {
 677   transition(vtos, itos);
 678   locals_index(rbx);
 679   __ movl(rax, iaddress(rbx));
 680 }
 681 
 682 void TemplateTable::lload() {
 683   transition(vtos, ltos);
 684   locals_index(rbx);
 685   __ movptr(rax, laddress(rbx));
 686   NOT_LP64(__ movl(rdx, haddress(rbx)));
 687 }
 688 
 689 void TemplateTable::fload() {
 690   transition(vtos, ftos);
 691   locals_index(rbx);
 692   __ load_float(faddress(rbx));
 693 }
 694 
 695 void TemplateTable::dload() {
 696   transition(vtos, dtos);
 697   locals_index(rbx);
 698   __ load_double(daddress(rbx));
 699 }
 700 
 701 void TemplateTable::aload() {
 702   transition(vtos, atos);
 703   locals_index(rbx);
 704   __ movptr(rax, aaddress(rbx));
 705 }
 706 
 707 void TemplateTable::locals_index_wide(Register reg) {
 708   __ load_unsigned_short(reg, at_bcp(2));
 709   __ bswapl(reg);
 710   __ shrl(reg, 16);
 711   __ negptr(reg);
 712 }
 713 
 714 void TemplateTable::wide_iload() {
 715   transition(vtos, itos);
 716   locals_index_wide(rbx);
 717   __ movl(rax, iaddress(rbx));
 718 }
 719 
 720 void TemplateTable::wide_lload() {
 721   transition(vtos, ltos);
 722   locals_index_wide(rbx);
 723   __ movptr(rax, laddress(rbx));
 724   NOT_LP64(__ movl(rdx, haddress(rbx)));
 725 }
 726 
 727 void TemplateTable::wide_fload() {
 728   transition(vtos, ftos);
 729   locals_index_wide(rbx);
 730   __ load_float(faddress(rbx));
 731 }
 732 
 733 void TemplateTable::wide_dload() {
 734   transition(vtos, dtos);
 735   locals_index_wide(rbx);
 736   __ load_double(daddress(rbx));
 737 }
 738 
 739 void TemplateTable::wide_aload() {
 740   transition(vtos, atos);
 741   locals_index_wide(rbx);
 742   __ movptr(rax, aaddress(rbx));
 743 }
 744 
 745 void TemplateTable::index_check(Register array, Register index) {
 746   // Pop ptr into array
 747   __ pop_ptr(array);
 748   index_check_without_pop(array, index);
 749 }
 750 
 751 void TemplateTable::index_check_without_pop(Register array, Register index) {
 752   // destroys rbx
 753   // check array
 754   __ null_check(array, arrayOopDesc::length_offset_in_bytes());
 755   // sign extend index for use by indexed load
 756   __ movl2ptr(index, index);
 757   // check index
 758   __ cmpl(index, Address(array, arrayOopDesc::length_offset_in_bytes()));
 759   if (index != rbx) {
 760     // ??? convention: move aberrant index into rbx for exception message
 761     assert(rbx != array, "different registers");
 762     __ movl(rbx, index);
 763   }
 764   Label skip;
 765   __ jccb(Assembler::below, skip);
 766   // Pass array to create more detailed exceptions.
 767   __ mov(NOT_LP64(rax) LP64_ONLY(c_rarg1), array);
 768   __ jump(ExternalAddress(Interpreter::_throw_ArrayIndexOutOfBoundsException_entry));
 769   __ bind(skip);
 770 }
 771 
 772 void TemplateTable::iaload() {
 773   transition(itos, itos);
 774   // rax: index
 775   // rdx: array
 776   index_check(rdx, rax); // kills rbx
 777   __ access_load_at(T_INT, IN_HEAP | IS_ARRAY, rax,
 778                     Address(rdx, rax, Address::times_4,
 779                             arrayOopDesc::base_offset_in_bytes(T_INT)),
 780                     noreg, noreg);
 781 }
 782 
 783 void TemplateTable::laload() {
 784   transition(itos, ltos);
 785   // rax: index
 786   // rdx: array
 787   index_check(rdx, rax); // kills rbx
 788   NOT_LP64(__ mov(rbx, rax));
 789   // rbx,: index
 790   __ access_load_at(T_LONG, IN_HEAP | IS_ARRAY, noreg /* ltos */,
 791                     Address(rdx, rbx, Address::times_8,
 792                             arrayOopDesc::base_offset_in_bytes(T_LONG)),
 793                     noreg, noreg);
 794 }
 795 
 796 
 797 
 798 void TemplateTable::faload() {
 799   transition(itos, ftos);
 800   // rax: index
 801   // rdx: array
 802   index_check(rdx, rax); // kills rbx
 803   __ access_load_at(T_FLOAT, IN_HEAP | IS_ARRAY, noreg /* ftos */,
 804                     Address(rdx, rax,
 805                             Address::times_4,
 806                             arrayOopDesc::base_offset_in_bytes(T_FLOAT)),
 807                     noreg, noreg);
 808 }
 809 
 810 void TemplateTable::daload() {
 811   transition(itos, dtos);
 812   // rax: index
 813   // rdx: array
 814   index_check(rdx, rax); // kills rbx
 815   __ access_load_at(T_DOUBLE, IN_HEAP | IS_ARRAY, noreg /* dtos */,
 816                     Address(rdx, rax,
 817                             Address::times_8,
 818                             arrayOopDesc::base_offset_in_bytes(T_DOUBLE)),
 819                     noreg, noreg);
 820 }
 821 
 822 void TemplateTable::aaload() {
 823   transition(itos, atos);
 824   Register array = rdx;
 825   Register index = rax;
 826 
 827   index_check(array, index); // kills rbx
 828   __ profile_array(rbx, array, rcx);
 829   if (UseFlatArray) {
 830     Label is_flat_array, done;
 831     __ test_flattened_array_oop(array, rbx, is_flat_array);
 832     do_oop_load(_masm,
 833                 Address(array, index,
 834                         UseCompressedOops ? Address::times_4 : Address::times_ptr,
 835                         arrayOopDesc::base_offset_in_bytes(T_OBJECT)),
 836                 rax,
 837                 IS_ARRAY);
 838     __ jmp(done);
 839     __ bind(is_flat_array);
 840     __ read_flattened_element(array, index, rbx, rcx, rax);
 841     __ bind(done);
 842   } else {
 843     do_oop_load(_masm,
 844                 Address(array, index,
 845                         UseCompressedOops ? Address::times_4 : Address::times_ptr,
 846                         arrayOopDesc::base_offset_in_bytes(T_OBJECT)),
 847                 rax,
 848                 IS_ARRAY);
 849   }
 850   __ profile_element(rbx, rax, rcx);
 851 }
 852 
 853 void TemplateTable::baload() {
 854   transition(itos, itos);
 855   // rax: index
 856   // rdx: array
 857   index_check(rdx, rax); // kills rbx
 858   __ access_load_at(T_BYTE, IN_HEAP | IS_ARRAY, rax,
 859                     Address(rdx, rax, Address::times_1, arrayOopDesc::base_offset_in_bytes(T_BYTE)),
 860                     noreg, noreg);
 861 }
 862 
 863 void TemplateTable::caload() {
 864   transition(itos, itos);
 865   // rax: index
 866   // rdx: array
 867   index_check(rdx, rax); // kills rbx
 868   __ access_load_at(T_CHAR, IN_HEAP | IS_ARRAY, rax,
 869                     Address(rdx, rax, Address::times_2, arrayOopDesc::base_offset_in_bytes(T_CHAR)),
 870                     noreg, noreg);
 871 }
 872 
 873 // iload followed by caload frequent pair
 874 void TemplateTable::fast_icaload() {
 875   transition(vtos, itos);
 876   // load index out of locals
 877   locals_index(rbx);
 878   __ movl(rax, iaddress(rbx));
 879 
 880   // rax: index
 881   // rdx: array
 882   index_check(rdx, rax); // kills rbx
 883   __ access_load_at(T_CHAR, IN_HEAP | IS_ARRAY, rax,
 884                     Address(rdx, rax, Address::times_2, arrayOopDesc::base_offset_in_bytes(T_CHAR)),
 885                     noreg, noreg);
 886 }
 887 
 888 
 889 void TemplateTable::saload() {
 890   transition(itos, itos);
 891   // rax: index
 892   // rdx: array
 893   index_check(rdx, rax); // kills rbx
 894   __ access_load_at(T_SHORT, IN_HEAP | IS_ARRAY, rax,
 895                     Address(rdx, rax, Address::times_2, arrayOopDesc::base_offset_in_bytes(T_SHORT)),
 896                     noreg, noreg);
 897 }
 898 
 899 void TemplateTable::iload(int n) {
 900   transition(vtos, itos);
 901   __ movl(rax, iaddress(n));
 902 }
 903 
 904 void TemplateTable::lload(int n) {
 905   transition(vtos, ltos);
 906   __ movptr(rax, laddress(n));
 907   NOT_LP64(__ movptr(rdx, haddress(n)));
 908 }
 909 
 910 void TemplateTable::fload(int n) {
 911   transition(vtos, ftos);
 912   __ load_float(faddress(n));
 913 }
 914 
 915 void TemplateTable::dload(int n) {
 916   transition(vtos, dtos);
 917   __ load_double(daddress(n));
 918 }
 919 
 920 void TemplateTable::aload(int n) {
 921   transition(vtos, atos);
 922   __ movptr(rax, aaddress(n));
 923 }
 924 
 925 void TemplateTable::aload_0() {
 926   aload_0_internal();
 927 }
 928 
 929 void TemplateTable::nofast_aload_0() {
 930   aload_0_internal(may_not_rewrite);
 931 }
 932 
 933 void TemplateTable::aload_0_internal(RewriteControl rc) {
 934   transition(vtos, atos);
 935   // According to bytecode histograms, the pairs:
 936   //
 937   // _aload_0, _fast_igetfield
 938   // _aload_0, _fast_agetfield
 939   // _aload_0, _fast_fgetfield
 940   //
 941   // occur frequently. If RewriteFrequentPairs is set, the (slow)
 942   // _aload_0 bytecode checks if the next bytecode is either
 943   // _fast_igetfield, _fast_agetfield or _fast_fgetfield and then
 944   // rewrites the current bytecode into a pair bytecode; otherwise it
 945   // rewrites the current bytecode into _fast_aload_0 that doesn't do
 946   // the pair check anymore.
 947   //
 948   // Note: If the next bytecode is _getfield, the rewrite must be
 949   //       delayed, otherwise we may miss an opportunity for a pair.
 950   //
 951   // Also rewrite frequent pairs
 952   //   aload_0, aload_1
 953   //   aload_0, iload_1
 954   // These bytecodes with a small amount of code are most profitable
 955   // to rewrite
 956   if (RewriteFrequentPairs && rc == may_rewrite) {
 957     Label rewrite, done;
 958 
 959     const Register bc = LP64_ONLY(c_rarg3) NOT_LP64(rcx);
 960     LP64_ONLY(assert(rbx != bc, "register damaged"));
 961 
 962     // get next byte
 963     __ load_unsigned_byte(rbx, at_bcp(Bytecodes::length_for(Bytecodes::_aload_0)));
 964 
 965     // if _getfield then wait with rewrite
 966     __ cmpl(rbx, Bytecodes::_getfield);
 967     __ jcc(Assembler::equal, done);
 968 
 969     // if _igetfield then rewrite to _fast_iaccess_0
 970     assert(Bytecodes::java_code(Bytecodes::_fast_iaccess_0) == Bytecodes::_aload_0, "fix bytecode definition");
 971     __ cmpl(rbx, Bytecodes::_fast_igetfield);
 972     __ movl(bc, Bytecodes::_fast_iaccess_0);
 973     __ jccb(Assembler::equal, rewrite);
 974 
 975     // if _agetfield then rewrite to _fast_aaccess_0
 976     assert(Bytecodes::java_code(Bytecodes::_fast_aaccess_0) == Bytecodes::_aload_0, "fix bytecode definition");
 977     __ cmpl(rbx, Bytecodes::_fast_agetfield);
 978     __ movl(bc, Bytecodes::_fast_aaccess_0);
 979     __ jccb(Assembler::equal, rewrite);
 980 
 981     // if _fgetfield then rewrite to _fast_faccess_0
 982     assert(Bytecodes::java_code(Bytecodes::_fast_faccess_0) == Bytecodes::_aload_0, "fix bytecode definition");
 983     __ cmpl(rbx, Bytecodes::_fast_fgetfield);
 984     __ movl(bc, Bytecodes::_fast_faccess_0);
 985     __ jccb(Assembler::equal, rewrite);
 986 
 987     // else rewrite to _fast_aload0
 988     assert(Bytecodes::java_code(Bytecodes::_fast_aload_0) == Bytecodes::_aload_0, "fix bytecode definition");
 989     __ movl(bc, Bytecodes::_fast_aload_0);
 990 
 991     // rewrite
 992     // bc: fast bytecode
 993     __ bind(rewrite);
 994     patch_bytecode(Bytecodes::_aload_0, bc, rbx, false);
 995 
 996     __ bind(done);
 997   }
 998 
 999   // Do actual aload_0 (must do this after patch_bytecode which might call VM and GC might change oop).
1000   aload(0);
1001 }
1002 
1003 void TemplateTable::istore() {
1004   transition(itos, vtos);
1005   locals_index(rbx);
1006   __ movl(iaddress(rbx), rax);
1007 }
1008 
1009 
1010 void TemplateTable::lstore() {
1011   transition(ltos, vtos);
1012   locals_index(rbx);
1013   __ movptr(laddress(rbx), rax);
1014   NOT_LP64(__ movptr(haddress(rbx), rdx));
1015 }
1016 
1017 void TemplateTable::fstore() {
1018   transition(ftos, vtos);
1019   locals_index(rbx);
1020   __ store_float(faddress(rbx));
1021 }
1022 
1023 void TemplateTable::dstore() {
1024   transition(dtos, vtos);
1025   locals_index(rbx);
1026   __ store_double(daddress(rbx));
1027 }
1028 
1029 void TemplateTable::astore() {
1030   transition(vtos, vtos);
1031   __ pop_ptr(rax);
1032   locals_index(rbx);
1033   __ movptr(aaddress(rbx), rax);
1034 }
1035 
1036 void TemplateTable::wide_istore() {
1037   transition(vtos, vtos);
1038   __ pop_i();
1039   locals_index_wide(rbx);
1040   __ movl(iaddress(rbx), rax);
1041 }
1042 
1043 void TemplateTable::wide_lstore() {
1044   transition(vtos, vtos);
1045   NOT_LP64(__ pop_l(rax, rdx));
1046   LP64_ONLY(__ pop_l());
1047   locals_index_wide(rbx);
1048   __ movptr(laddress(rbx), rax);
1049   NOT_LP64(__ movl(haddress(rbx), rdx));
1050 }
1051 
1052 void TemplateTable::wide_fstore() {
1053 #ifdef _LP64
1054   transition(vtos, vtos);
1055   __ pop_f(xmm0);
1056   locals_index_wide(rbx);
1057   __ movflt(faddress(rbx), xmm0);
1058 #else
1059   wide_istore();
1060 #endif
1061 }
1062 
1063 void TemplateTable::wide_dstore() {
1064 #ifdef _LP64
1065   transition(vtos, vtos);
1066   __ pop_d(xmm0);
1067   locals_index_wide(rbx);
1068   __ movdbl(daddress(rbx), xmm0);
1069 #else
1070   wide_lstore();
1071 #endif
1072 }
1073 
1074 void TemplateTable::wide_astore() {
1075   transition(vtos, vtos);
1076   __ pop_ptr(rax);
1077   locals_index_wide(rbx);
1078   __ movptr(aaddress(rbx), rax);
1079 }
1080 
1081 void TemplateTable::iastore() {
1082   transition(itos, vtos);
1083   __ pop_i(rbx);
1084   // rax: value
1085   // rbx: index
1086   // rdx: array
1087   index_check(rdx, rbx); // prefer index in rbx
1088   __ access_store_at(T_INT, IN_HEAP | IS_ARRAY,
1089                      Address(rdx, rbx, Address::times_4,
1090                              arrayOopDesc::base_offset_in_bytes(T_INT)),
1091                      rax, noreg, noreg);
1092 }
1093 
1094 void TemplateTable::lastore() {
1095   transition(ltos, vtos);
1096   __ pop_i(rbx);
1097   // rax,: low(value)
1098   // rcx: array
1099   // rdx: high(value)
1100   index_check(rcx, rbx);  // prefer index in rbx,
1101   // rbx,: index
1102   __ access_store_at(T_LONG, IN_HEAP | IS_ARRAY,
1103                      Address(rcx, rbx, Address::times_8,
1104                              arrayOopDesc::base_offset_in_bytes(T_LONG)),
1105                      noreg /* ltos */, noreg, noreg);
1106 }
1107 
1108 
1109 void TemplateTable::fastore() {
1110   transition(ftos, vtos);
1111   __ pop_i(rbx);
1112   // value is in UseSSE >= 1 ? xmm0 : ST(0)
1113   // rbx:  index
1114   // rdx:  array
1115   index_check(rdx, rbx); // prefer index in rbx
1116   __ access_store_at(T_FLOAT, IN_HEAP | IS_ARRAY,
1117                      Address(rdx, rbx, Address::times_4,
1118                              arrayOopDesc::base_offset_in_bytes(T_FLOAT)),
1119                      noreg /* ftos */, noreg, noreg);
1120 }
1121 
1122 void TemplateTable::dastore() {
1123   transition(dtos, vtos);
1124   __ pop_i(rbx);
1125   // value is in UseSSE >= 2 ? xmm0 : ST(0)
1126   // rbx:  index
1127   // rdx:  array
1128   index_check(rdx, rbx); // prefer index in rbx
1129   __ access_store_at(T_DOUBLE, IN_HEAP | IS_ARRAY,
1130                      Address(rdx, rbx, Address::times_8,
1131                              arrayOopDesc::base_offset_in_bytes(T_DOUBLE)),
1132                      noreg /* dtos */, noreg, noreg);
1133 }
1134 
1135 void TemplateTable::aastore() {
1136   Label is_null, is_flat_array, ok_is_subtype, done;
1137   transition(vtos, vtos);
1138   // stack: ..., array, index, value
1139   __ movptr(rax, at_tos());    // value
1140   __ movl(rcx, at_tos_p1()); // index
1141   __ movptr(rdx, at_tos_p2()); // array
1142 
1143   Address element_address(rdx, rcx,
1144                           UseCompressedOops? Address::times_4 : Address::times_ptr,
1145                           arrayOopDesc::base_offset_in_bytes(T_OBJECT));
1146 
1147   index_check_without_pop(rdx, rcx);     // kills rbx
1148 
1149   __ profile_array(rdi, rdx, rbx);
1150   __ profile_element(rdi, rax, rbx);
1151 
1152   __ testptr(rax, rax);
1153   __ jcc(Assembler::zero, is_null);
1154 
1155   // Move array class to rdi
1156   Register tmp_load_klass = LP64_ONLY(rscratch1) NOT_LP64(noreg);
1157   __ load_klass(rdi, rdx, tmp_load_klass);
1158   if (UseFlatArray) {
1159     __ movl(rbx, Address(rdi, Klass::layout_helper_offset()));
1160     __ test_flattened_array_layout(rbx, is_flat_array);
1161   }
1162 
1163   // Move subklass into rbx
1164   __ load_klass(rbx, rax, tmp_load_klass);
1165   // Move array element superklass into rax
1166   __ movptr(rax, Address(rdi,

1167                          ObjArrayKlass::element_klass_offset()));
1168 
1169   // Generate subtype check.  Blows rcx, rdi
1170   // Superklass in rax.  Subklass in rbx.
1171   // is "rbx <: rax" ? (value subclass <: array element superclass)
1172   __ gen_subtype_check(rbx, ok_is_subtype, false);
1173 
1174   // Come here on failure
1175   // object is at TOS
1176   __ jump(ExternalAddress(Interpreter::_throw_ArrayStoreException_entry));
1177 
1178   // Come here on success
1179   __ bind(ok_is_subtype);
1180 
1181   // Get the value we will store
1182   __ movptr(rax, at_tos());
1183   __ movl(rcx, at_tos_p1()); // index
1184   // Now store using the appropriate barrier
1185   do_oop_store(_masm, element_address, rax, IS_ARRAY);
1186   __ jmp(done);
1187 
1188   // Have a NULL in rax, rdx=array, ecx=index.  Store NULL at ary[idx]
1189   __ bind(is_null);
1190   if (EnableValhalla) {
1191     Label is_null_into_value_array_npe, store_null;
1192 
1193     // No way to store null in null-free array
1194     __ test_null_free_array_oop(rdx, rbx, is_null_into_value_array_npe);
1195     __ jmp(store_null);
1196 
1197     __ bind(is_null_into_value_array_npe);
1198     __ jump(ExternalAddress(Interpreter::_throw_NullPointerException_entry));
1199 
1200     __ bind(store_null);
1201   }
1202   // Store a NULL
1203   do_oop_store(_masm, element_address, noreg, IS_ARRAY);
1204   __ jmp(done);
1205 
1206   if (EnableValhalla) {
1207     Label is_type_ok;
1208     __ bind(is_flat_array); // Store non-null value to flat
1209 
1210     // Simplistic type check...
1211 
1212     // Profile the not-null value's klass.
1213     __ load_klass(rbx, rax, tmp_load_klass);
1214     // Move element klass into rax
1215     __ movptr(rax, Address(rdi, ArrayKlass::element_klass_offset()));
1216     // flat value array needs exact type match
1217     // is "rax == rbx" (value subclass == array element superclass)
1218     __ cmpptr(rax, rbx);
1219     __ jccb(Assembler::equal, is_type_ok);
1220 
1221     __ jump(ExternalAddress(Interpreter::_throw_ArrayStoreException_entry));
1222 
1223     __ bind(is_type_ok);
1224     // rbx: value's klass
1225     // rdx: array
1226     // rdi: array klass
1227     __ test_klass_is_empty_inline_type(rbx, rax, done);
1228 
1229     // calc dst for copy
1230     __ movl(rax, at_tos_p1()); // index
1231     __ data_for_value_array_index(rdx, rdi, rax, rax);
1232 
1233     // ...and src for copy
1234     __ movptr(rcx, at_tos());  // value
1235     __ data_for_oop(rcx, rcx, rbx);
1236 
1237     __ access_value_copy(IN_HEAP, rcx, rax, rbx);
1238   }
1239   // Pop stack arguments
1240   __ bind(done);
1241   __ addptr(rsp, 3 * Interpreter::stackElementSize);
1242 }
1243 
1244 void TemplateTable::bastore() {
1245   transition(itos, vtos);
1246   __ pop_i(rbx);
1247   // rax: value
1248   // rbx: index
1249   // rdx: array
1250   index_check(rdx, rbx); // prefer index in rbx
1251   // Need to check whether array is boolean or byte
1252   // since both types share the bastore bytecode.
1253   Register tmp_load_klass = LP64_ONLY(rscratch1) NOT_LP64(noreg);
1254   __ load_klass(rcx, rdx, tmp_load_klass);
1255   __ movl(rcx, Address(rcx, Klass::layout_helper_offset()));
1256   int diffbit = Klass::layout_helper_boolean_diffbit();
1257   __ testl(rcx, diffbit);
1258   Label L_skip;
1259   __ jccb(Assembler::zero, L_skip);
1260   __ andl(rax, 1);  // if it is a T_BOOLEAN array, mask the stored value to 0/1
1261   __ bind(L_skip);
1262   __ access_store_at(T_BYTE, IN_HEAP | IS_ARRAY,
1263                      Address(rdx, rbx,Address::times_1,
1264                              arrayOopDesc::base_offset_in_bytes(T_BYTE)),
1265                      rax, noreg, noreg);
1266 }
1267 
1268 void TemplateTable::castore() {
1269   transition(itos, vtos);
1270   __ pop_i(rbx);
1271   // rax: value
1272   // rbx: index
1273   // rdx: array
1274   index_check(rdx, rbx);  // prefer index in rbx
1275   __ access_store_at(T_CHAR, IN_HEAP | IS_ARRAY,
1276                      Address(rdx, rbx, Address::times_2,
1277                              arrayOopDesc::base_offset_in_bytes(T_CHAR)),
1278                      rax, noreg, noreg);
1279 }
1280 
1281 
1282 void TemplateTable::sastore() {
1283   castore();
1284 }
1285 
1286 void TemplateTable::istore(int n) {
1287   transition(itos, vtos);
1288   __ movl(iaddress(n), rax);
1289 }
1290 
1291 void TemplateTable::lstore(int n) {
1292   transition(ltos, vtos);
1293   __ movptr(laddress(n), rax);
1294   NOT_LP64(__ movptr(haddress(n), rdx));
1295 }
1296 
1297 void TemplateTable::fstore(int n) {
1298   transition(ftos, vtos);
1299   __ store_float(faddress(n));
1300 }
1301 
1302 void TemplateTable::dstore(int n) {
1303   transition(dtos, vtos);
1304   __ store_double(daddress(n));
1305 }
1306 
1307 
1308 void TemplateTable::astore(int n) {
1309   transition(vtos, vtos);
1310   __ pop_ptr(rax);
1311   __ movptr(aaddress(n), rax);
1312 }
1313 
1314 void TemplateTable::pop() {
1315   transition(vtos, vtos);
1316   __ addptr(rsp, Interpreter::stackElementSize);
1317 }
1318 
1319 void TemplateTable::pop2() {
1320   transition(vtos, vtos);
1321   __ addptr(rsp, 2 * Interpreter::stackElementSize);
1322 }
1323 
1324 
1325 void TemplateTable::dup() {
1326   transition(vtos, vtos);
1327   __ load_ptr(0, rax);
1328   __ push_ptr(rax);
1329   // stack: ..., a, a
1330 }
1331 
1332 void TemplateTable::dup_x1() {
1333   transition(vtos, vtos);
1334   // stack: ..., a, b
1335   __ load_ptr( 0, rax);  // load b
1336   __ load_ptr( 1, rcx);  // load a
1337   __ store_ptr(1, rax);  // store b
1338   __ store_ptr(0, rcx);  // store a
1339   __ push_ptr(rax);      // push b
1340   // stack: ..., b, a, b
1341 }
1342 
1343 void TemplateTable::dup_x2() {
1344   transition(vtos, vtos);
1345   // stack: ..., a, b, c
1346   __ load_ptr( 0, rax);  // load c
1347   __ load_ptr( 2, rcx);  // load a
1348   __ store_ptr(2, rax);  // store c in a
1349   __ push_ptr(rax);      // push c
1350   // stack: ..., c, b, c, c
1351   __ load_ptr( 2, rax);  // load b
1352   __ store_ptr(2, rcx);  // store a in b
1353   // stack: ..., c, a, c, c
1354   __ store_ptr(1, rax);  // store b in c
1355   // stack: ..., c, a, b, c
1356 }
1357 
1358 void TemplateTable::dup2() {
1359   transition(vtos, vtos);
1360   // stack: ..., a, b
1361   __ load_ptr(1, rax);  // load a
1362   __ push_ptr(rax);     // push a
1363   __ load_ptr(1, rax);  // load b
1364   __ push_ptr(rax);     // push b
1365   // stack: ..., a, b, a, b
1366 }
1367 
1368 
1369 void TemplateTable::dup2_x1() {
1370   transition(vtos, vtos);
1371   // stack: ..., a, b, c
1372   __ load_ptr( 0, rcx);  // load c
1373   __ load_ptr( 1, rax);  // load b
1374   __ push_ptr(rax);      // push b
1375   __ push_ptr(rcx);      // push c
1376   // stack: ..., a, b, c, b, c
1377   __ store_ptr(3, rcx);  // store c in b
1378   // stack: ..., a, c, c, b, c
1379   __ load_ptr( 4, rcx);  // load a
1380   __ store_ptr(2, rcx);  // store a in 2nd c
1381   // stack: ..., a, c, a, b, c
1382   __ store_ptr(4, rax);  // store b in a
1383   // stack: ..., b, c, a, b, c
1384 }
1385 
1386 void TemplateTable::dup2_x2() {
1387   transition(vtos, vtos);
1388   // stack: ..., a, b, c, d
1389   __ load_ptr( 0, rcx);  // load d
1390   __ load_ptr( 1, rax);  // load c
1391   __ push_ptr(rax);      // push c
1392   __ push_ptr(rcx);      // push d
1393   // stack: ..., a, b, c, d, c, d
1394   __ load_ptr( 4, rax);  // load b
1395   __ store_ptr(2, rax);  // store b in d
1396   __ store_ptr(4, rcx);  // store d in b
1397   // stack: ..., a, d, c, b, c, d
1398   __ load_ptr( 5, rcx);  // load a
1399   __ load_ptr( 3, rax);  // load c
1400   __ store_ptr(3, rcx);  // store a in c
1401   __ store_ptr(5, rax);  // store c in a
1402   // stack: ..., c, d, a, b, c, d
1403 }
1404 
1405 void TemplateTable::swap() {
1406   transition(vtos, vtos);
1407   // stack: ..., a, b
1408   __ load_ptr( 1, rcx);  // load a
1409   __ load_ptr( 0, rax);  // load b
1410   __ store_ptr(0, rcx);  // store a in b
1411   __ store_ptr(1, rax);  // store b in a
1412   // stack: ..., b, a
1413 }
1414 
1415 void TemplateTable::iop2(Operation op) {
1416   transition(itos, itos);
1417   switch (op) {
1418   case add  :                    __ pop_i(rdx); __ addl (rax, rdx); break;
1419   case sub  : __ movl(rdx, rax); __ pop_i(rax); __ subl (rax, rdx); break;
1420   case mul  :                    __ pop_i(rdx); __ imull(rax, rdx); break;
1421   case _and :                    __ pop_i(rdx); __ andl (rax, rdx); break;
1422   case _or  :                    __ pop_i(rdx); __ orl  (rax, rdx); break;
1423   case _xor :                    __ pop_i(rdx); __ xorl (rax, rdx); break;
1424   case shl  : __ movl(rcx, rax); __ pop_i(rax); __ shll (rax);      break;
1425   case shr  : __ movl(rcx, rax); __ pop_i(rax); __ sarl (rax);      break;
1426   case ushr : __ movl(rcx, rax); __ pop_i(rax); __ shrl (rax);      break;
1427   default   : ShouldNotReachHere();
1428   }
1429 }
1430 
1431 void TemplateTable::lop2(Operation op) {
1432   transition(ltos, ltos);
1433 #ifdef _LP64
1434   switch (op) {
1435   case add  :                    __ pop_l(rdx); __ addptr(rax, rdx); break;
1436   case sub  : __ mov(rdx, rax);  __ pop_l(rax); __ subptr(rax, rdx); break;
1437   case _and :                    __ pop_l(rdx); __ andptr(rax, rdx); break;
1438   case _or  :                    __ pop_l(rdx); __ orptr (rax, rdx); break;
1439   case _xor :                    __ pop_l(rdx); __ xorptr(rax, rdx); break;
1440   default   : ShouldNotReachHere();
1441   }
1442 #else
1443   __ pop_l(rbx, rcx);
1444   switch (op) {
1445     case add  : __ addl(rax, rbx); __ adcl(rdx, rcx); break;
1446     case sub  : __ subl(rbx, rax); __ sbbl(rcx, rdx);
1447                 __ mov (rax, rbx); __ mov (rdx, rcx); break;
1448     case _and : __ andl(rax, rbx); __ andl(rdx, rcx); break;
1449     case _or  : __ orl (rax, rbx); __ orl (rdx, rcx); break;
1450     case _xor : __ xorl(rax, rbx); __ xorl(rdx, rcx); break;
1451     default   : ShouldNotReachHere();
1452   }
1453 #endif
1454 }
1455 
1456 void TemplateTable::idiv() {
1457   transition(itos, itos);
1458   __ movl(rcx, rax);
1459   __ pop_i(rax);
1460   // Note: could xor rax and ecx and compare with (-1 ^ min_int). If
1461   //       they are not equal, one could do a normal division (no correction
1462   //       needed), which may speed up this implementation for the common case.
1463   //       (see also JVM spec., p.243 & p.271)
1464   __ corrected_idivl(rcx);
1465 }
1466 
1467 void TemplateTable::irem() {
1468   transition(itos, itos);
1469   __ movl(rcx, rax);
1470   __ pop_i(rax);
1471   // Note: could xor rax and ecx and compare with (-1 ^ min_int). If
1472   //       they are not equal, one could do a normal division (no correction
1473   //       needed), which may speed up this implementation for the common case.
1474   //       (see also JVM spec., p.243 & p.271)
1475   __ corrected_idivl(rcx);
1476   __ movl(rax, rdx);
1477 }
1478 
1479 void TemplateTable::lmul() {
1480   transition(ltos, ltos);
1481 #ifdef _LP64
1482   __ pop_l(rdx);
1483   __ imulq(rax, rdx);
1484 #else
1485   __ pop_l(rbx, rcx);
1486   __ push(rcx); __ push(rbx);
1487   __ push(rdx); __ push(rax);
1488   __ lmul(2 * wordSize, 0);
1489   __ addptr(rsp, 4 * wordSize);  // take off temporaries
1490 #endif
1491 }
1492 
1493 void TemplateTable::ldiv() {
1494   transition(ltos, ltos);
1495 #ifdef _LP64
1496   __ mov(rcx, rax);
1497   __ pop_l(rax);
1498   // generate explicit div0 check
1499   __ testq(rcx, rcx);
1500   __ jump_cc(Assembler::zero,
1501              ExternalAddress(Interpreter::_throw_ArithmeticException_entry));
1502   // Note: could xor rax and rcx and compare with (-1 ^ min_int). If
1503   //       they are not equal, one could do a normal division (no correction
1504   //       needed), which may speed up this implementation for the common case.
1505   //       (see also JVM spec., p.243 & p.271)
1506   __ corrected_idivq(rcx); // kills rbx
1507 #else
1508   __ pop_l(rbx, rcx);
1509   __ push(rcx); __ push(rbx);
1510   __ push(rdx); __ push(rax);
1511   // check if y = 0
1512   __ orl(rax, rdx);
1513   __ jump_cc(Assembler::zero,
1514              ExternalAddress(Interpreter::_throw_ArithmeticException_entry));
1515   __ call_VM_leaf(CAST_FROM_FN_PTR(address, SharedRuntime::ldiv));
1516   __ addptr(rsp, 4 * wordSize);  // take off temporaries
1517 #endif
1518 }
1519 
1520 void TemplateTable::lrem() {
1521   transition(ltos, ltos);
1522 #ifdef _LP64
1523   __ mov(rcx, rax);
1524   __ pop_l(rax);
1525   __ testq(rcx, rcx);
1526   __ jump_cc(Assembler::zero,
1527              ExternalAddress(Interpreter::_throw_ArithmeticException_entry));
1528   // Note: could xor rax and rcx and compare with (-1 ^ min_int). If
1529   //       they are not equal, one could do a normal division (no correction
1530   //       needed), which may speed up this implementation for the common case.
1531   //       (see also JVM spec., p.243 & p.271)
1532   __ corrected_idivq(rcx); // kills rbx
1533   __ mov(rax, rdx);
1534 #else
1535   __ pop_l(rbx, rcx);
1536   __ push(rcx); __ push(rbx);
1537   __ push(rdx); __ push(rax);
1538   // check if y = 0
1539   __ orl(rax, rdx);
1540   __ jump_cc(Assembler::zero,
1541              ExternalAddress(Interpreter::_throw_ArithmeticException_entry));
1542   __ call_VM_leaf(CAST_FROM_FN_PTR(address, SharedRuntime::lrem));
1543   __ addptr(rsp, 4 * wordSize);
1544 #endif
1545 }
1546 
1547 void TemplateTable::lshl() {
1548   transition(itos, ltos);
1549   __ movl(rcx, rax);                             // get shift count
1550   #ifdef _LP64
1551   __ pop_l(rax);                                 // get shift value
1552   __ shlq(rax);
1553 #else
1554   __ pop_l(rax, rdx);                            // get shift value
1555   __ lshl(rdx, rax);
1556 #endif
1557 }
1558 
1559 void TemplateTable::lshr() {
1560 #ifdef _LP64
1561   transition(itos, ltos);
1562   __ movl(rcx, rax);                             // get shift count
1563   __ pop_l(rax);                                 // get shift value
1564   __ sarq(rax);
1565 #else
1566   transition(itos, ltos);
1567   __ mov(rcx, rax);                              // get shift count
1568   __ pop_l(rax, rdx);                            // get shift value
1569   __ lshr(rdx, rax, true);
1570 #endif
1571 }
1572 
1573 void TemplateTable::lushr() {
1574   transition(itos, ltos);
1575 #ifdef _LP64
1576   __ movl(rcx, rax);                             // get shift count
1577   __ pop_l(rax);                                 // get shift value
1578   __ shrq(rax);
1579 #else
1580   __ mov(rcx, rax);                              // get shift count
1581   __ pop_l(rax, rdx);                            // get shift value
1582   __ lshr(rdx, rax);
1583 #endif
1584 }
1585 
1586 void TemplateTable::fop2(Operation op) {
1587   transition(ftos, ftos);
1588 
1589   if (UseSSE >= 1) {
1590     switch (op) {
1591     case add:
1592       __ addss(xmm0, at_rsp());
1593       __ addptr(rsp, Interpreter::stackElementSize);
1594       break;
1595     case sub:
1596       __ movflt(xmm1, xmm0);
1597       __ pop_f(xmm0);
1598       __ subss(xmm0, xmm1);
1599       break;
1600     case mul:
1601       __ mulss(xmm0, at_rsp());
1602       __ addptr(rsp, Interpreter::stackElementSize);
1603       break;
1604     case div:
1605       __ movflt(xmm1, xmm0);
1606       __ pop_f(xmm0);
1607       __ divss(xmm0, xmm1);
1608       break;
1609     case rem:
1610       // On x86_64 platforms the SharedRuntime::frem method is called to perform the
1611       // modulo operation. The frem method calls the function
1612       // double fmod(double x, double y) in math.h. The documentation of fmod states:
1613       // "If x or y is a NaN, a NaN is returned." without specifying what type of NaN
1614       // (signalling or quiet) is returned.
1615       //
1616       // On x86_32 platforms the FPU is used to perform the modulo operation. The
1617       // reason is that on 32-bit Windows the sign of modulo operations diverges from
1618       // what is considered the standard (e.g., -0.0f % -3.14f is 0.0f (and not -0.0f).
1619       // The fprem instruction used on x86_32 is functionally equivalent to
1620       // SharedRuntime::frem in that it returns a NaN.
1621 #ifdef _LP64
1622       __ movflt(xmm1, xmm0);
1623       __ pop_f(xmm0);
1624       __ call_VM_leaf(CAST_FROM_FN_PTR(address, SharedRuntime::frem), 2);
1625 #else
1626       __ push_f(xmm0);
1627       __ pop_f();
1628       __ fld_s(at_rsp());
1629       __ fremr(rax);
1630       __ f2ieee();
1631       __ pop(rax);  // pop second operand off the stack
1632       __ push_f();
1633       __ pop_f(xmm0);
1634 #endif
1635       break;
1636     default:
1637       ShouldNotReachHere();
1638       break;
1639     }
1640   } else {
1641 #ifdef _LP64
1642     ShouldNotReachHere();
1643 #else
1644     switch (op) {
1645     case add: __ fadd_s (at_rsp());                break;
1646     case sub: __ fsubr_s(at_rsp());                break;
1647     case mul: __ fmul_s (at_rsp());                break;
1648     case div: __ fdivr_s(at_rsp());                break;
1649     case rem: __ fld_s  (at_rsp()); __ fremr(rax); break;
1650     default : ShouldNotReachHere();
1651     }
1652     __ f2ieee();
1653     __ pop(rax);  // pop second operand off the stack
1654 #endif // _LP64
1655   }
1656 }
1657 
1658 void TemplateTable::dop2(Operation op) {
1659   transition(dtos, dtos);
1660   if (UseSSE >= 2) {
1661     switch (op) {
1662     case add:
1663       __ addsd(xmm0, at_rsp());
1664       __ addptr(rsp, 2 * Interpreter::stackElementSize);
1665       break;
1666     case sub:
1667       __ movdbl(xmm1, xmm0);
1668       __ pop_d(xmm0);
1669       __ subsd(xmm0, xmm1);
1670       break;
1671     case mul:
1672       __ mulsd(xmm0, at_rsp());
1673       __ addptr(rsp, 2 * Interpreter::stackElementSize);
1674       break;
1675     case div:
1676       __ movdbl(xmm1, xmm0);
1677       __ pop_d(xmm0);
1678       __ divsd(xmm0, xmm1);
1679       break;
1680     case rem:
1681       // Similar to fop2(), the modulo operation is performed using the
1682       // SharedRuntime::drem method (on x86_64 platforms) or using the
1683       // FPU (on x86_32 platforms) for the same reasons as mentioned in fop2().
1684 #ifdef _LP64
1685       __ movdbl(xmm1, xmm0);
1686       __ pop_d(xmm0);
1687       __ call_VM_leaf(CAST_FROM_FN_PTR(address, SharedRuntime::drem), 2);
1688 #else
1689       __ push_d(xmm0);
1690       __ pop_d();
1691       __ fld_d(at_rsp());
1692       __ fremr(rax);
1693       __ d2ieee();
1694       __ pop(rax);
1695       __ pop(rdx);
1696       __ push_d();
1697       __ pop_d(xmm0);
1698 #endif
1699       break;
1700     default:
1701       ShouldNotReachHere();
1702       break;
1703     }
1704   } else {
1705 #ifdef _LP64
1706     ShouldNotReachHere();
1707 #else
1708     switch (op) {
1709     case add: __ fadd_d (at_rsp());                break;
1710     case sub: __ fsubr_d(at_rsp());                break;
1711     case mul: {
1712       // strict semantics
1713       __ fld_x(ExternalAddress(StubRoutines::x86::addr_fpu_subnormal_bias1()));
1714       __ fmulp();
1715       __ fmul_d (at_rsp());
1716       __ fld_x(ExternalAddress(StubRoutines::x86::addr_fpu_subnormal_bias2()));
1717       __ fmulp();
1718       break;
1719     }
1720     case div: {
1721       // strict semantics
1722       __ fld_x(ExternalAddress(StubRoutines::x86::addr_fpu_subnormal_bias1()));
1723       __ fmul_d (at_rsp());
1724       __ fdivrp();
1725       __ fld_x(ExternalAddress(StubRoutines::x86::addr_fpu_subnormal_bias2()));
1726       __ fmulp();
1727       break;
1728     }
1729     case rem: __ fld_d  (at_rsp()); __ fremr(rax); break;
1730     default : ShouldNotReachHere();
1731     }
1732     __ d2ieee();
1733     // Pop double precision number from rsp.
1734     __ pop(rax);
1735     __ pop(rdx);
1736 #endif
1737   }
1738 }
1739 
1740 void TemplateTable::ineg() {
1741   transition(itos, itos);
1742   __ negl(rax);
1743 }
1744 
1745 void TemplateTable::lneg() {
1746   transition(ltos, ltos);
1747   LP64_ONLY(__ negq(rax));
1748   NOT_LP64(__ lneg(rdx, rax));
1749 }
1750 
1751 // Note: 'double' and 'long long' have 32-bits alignment on x86.
1752 static jlong* double_quadword(jlong *adr, jlong lo, jlong hi) {
1753   // Use the expression (adr)&(~0xF) to provide 128-bits aligned address
1754   // of 128-bits operands for SSE instructions.
1755   jlong *operand = (jlong*)(((intptr_t)adr)&((intptr_t)(~0xF)));
1756   // Store the value to a 128-bits operand.
1757   operand[0] = lo;
1758   operand[1] = hi;
1759   return operand;
1760 }
1761 
1762 // Buffer for 128-bits masks used by SSE instructions.
1763 static jlong float_signflip_pool[2*2];
1764 static jlong double_signflip_pool[2*2];
1765 
1766 void TemplateTable::fneg() {
1767   transition(ftos, ftos);
1768   if (UseSSE >= 1) {
1769     static jlong *float_signflip  = double_quadword(&float_signflip_pool[1],  CONST64(0x8000000080000000),  CONST64(0x8000000080000000));
1770     __ xorps(xmm0, ExternalAddress((address) float_signflip));
1771   } else {
1772     LP64_ONLY(ShouldNotReachHere());
1773     NOT_LP64(__ fchs());
1774   }
1775 }
1776 
1777 void TemplateTable::dneg() {
1778   transition(dtos, dtos);
1779   if (UseSSE >= 2) {
1780     static jlong *double_signflip =
1781       double_quadword(&double_signflip_pool[1], CONST64(0x8000000000000000), CONST64(0x8000000000000000));
1782     __ xorpd(xmm0, ExternalAddress((address) double_signflip));
1783   } else {
1784 #ifdef _LP64
1785     ShouldNotReachHere();
1786 #else
1787     __ fchs();
1788 #endif
1789   }
1790 }
1791 
1792 void TemplateTable::iinc() {
1793   transition(vtos, vtos);
1794   __ load_signed_byte(rdx, at_bcp(2)); // get constant
1795   locals_index(rbx);
1796   __ addl(iaddress(rbx), rdx);
1797 }
1798 
1799 void TemplateTable::wide_iinc() {
1800   transition(vtos, vtos);
1801   __ movl(rdx, at_bcp(4)); // get constant
1802   locals_index_wide(rbx);
1803   __ bswapl(rdx); // swap bytes & sign-extend constant
1804   __ sarl(rdx, 16);
1805   __ addl(iaddress(rbx), rdx);
1806   // Note: should probably use only one movl to get both
1807   //       the index and the constant -> fix this
1808 }
1809 
1810 void TemplateTable::convert() {
1811 #ifdef _LP64
1812   // Checking
1813 #ifdef ASSERT
1814   {
1815     TosState tos_in  = ilgl;
1816     TosState tos_out = ilgl;
1817     switch (bytecode()) {
1818     case Bytecodes::_i2l: // fall through
1819     case Bytecodes::_i2f: // fall through
1820     case Bytecodes::_i2d: // fall through
1821     case Bytecodes::_i2b: // fall through
1822     case Bytecodes::_i2c: // fall through
1823     case Bytecodes::_i2s: tos_in = itos; break;
1824     case Bytecodes::_l2i: // fall through
1825     case Bytecodes::_l2f: // fall through
1826     case Bytecodes::_l2d: tos_in = ltos; break;
1827     case Bytecodes::_f2i: // fall through
1828     case Bytecodes::_f2l: // fall through
1829     case Bytecodes::_f2d: tos_in = ftos; break;
1830     case Bytecodes::_d2i: // fall through
1831     case Bytecodes::_d2l: // fall through
1832     case Bytecodes::_d2f: tos_in = dtos; break;
1833     default             : ShouldNotReachHere();
1834     }
1835     switch (bytecode()) {
1836     case Bytecodes::_l2i: // fall through
1837     case Bytecodes::_f2i: // fall through
1838     case Bytecodes::_d2i: // fall through
1839     case Bytecodes::_i2b: // fall through
1840     case Bytecodes::_i2c: // fall through
1841     case Bytecodes::_i2s: tos_out = itos; break;
1842     case Bytecodes::_i2l: // fall through
1843     case Bytecodes::_f2l: // fall through
1844     case Bytecodes::_d2l: tos_out = ltos; break;
1845     case Bytecodes::_i2f: // fall through
1846     case Bytecodes::_l2f: // fall through
1847     case Bytecodes::_d2f: tos_out = ftos; break;
1848     case Bytecodes::_i2d: // fall through
1849     case Bytecodes::_l2d: // fall through
1850     case Bytecodes::_f2d: tos_out = dtos; break;
1851     default             : ShouldNotReachHere();
1852     }
1853     transition(tos_in, tos_out);
1854   }
1855 #endif // ASSERT
1856 
1857   static const int64_t is_nan = 0x8000000000000000L;
1858 
1859   // Conversion
1860   switch (bytecode()) {
1861   case Bytecodes::_i2l:
1862     __ movslq(rax, rax);
1863     break;
1864   case Bytecodes::_i2f:
1865     __ cvtsi2ssl(xmm0, rax);
1866     break;
1867   case Bytecodes::_i2d:
1868     __ cvtsi2sdl(xmm0, rax);
1869     break;
1870   case Bytecodes::_i2b:
1871     __ movsbl(rax, rax);
1872     break;
1873   case Bytecodes::_i2c:
1874     __ movzwl(rax, rax);
1875     break;
1876   case Bytecodes::_i2s:
1877     __ movswl(rax, rax);
1878     break;
1879   case Bytecodes::_l2i:
1880     __ movl(rax, rax);
1881     break;
1882   case Bytecodes::_l2f:
1883     __ cvtsi2ssq(xmm0, rax);
1884     break;
1885   case Bytecodes::_l2d:
1886     __ cvtsi2sdq(xmm0, rax);
1887     break;
1888   case Bytecodes::_f2i:
1889   {
1890     Label L;
1891     __ cvttss2sil(rax, xmm0);
1892     __ cmpl(rax, 0x80000000); // NaN or overflow/underflow?
1893     __ jcc(Assembler::notEqual, L);
1894     __ call_VM_leaf(CAST_FROM_FN_PTR(address, SharedRuntime::f2i), 1);
1895     __ bind(L);
1896   }
1897     break;
1898   case Bytecodes::_f2l:
1899   {
1900     Label L;
1901     __ cvttss2siq(rax, xmm0);
1902     // NaN or overflow/underflow?
1903     __ cmp64(rax, ExternalAddress((address) &is_nan));
1904     __ jcc(Assembler::notEqual, L);
1905     __ call_VM_leaf(CAST_FROM_FN_PTR(address, SharedRuntime::f2l), 1);
1906     __ bind(L);
1907   }
1908     break;
1909   case Bytecodes::_f2d:
1910     __ cvtss2sd(xmm0, xmm0);
1911     break;
1912   case Bytecodes::_d2i:
1913   {
1914     Label L;
1915     __ cvttsd2sil(rax, xmm0);
1916     __ cmpl(rax, 0x80000000); // NaN or overflow/underflow?
1917     __ jcc(Assembler::notEqual, L);
1918     __ call_VM_leaf(CAST_FROM_FN_PTR(address, SharedRuntime::d2i), 1);
1919     __ bind(L);
1920   }
1921     break;
1922   case Bytecodes::_d2l:
1923   {
1924     Label L;
1925     __ cvttsd2siq(rax, xmm0);
1926     // NaN or overflow/underflow?
1927     __ cmp64(rax, ExternalAddress((address) &is_nan));
1928     __ jcc(Assembler::notEqual, L);
1929     __ call_VM_leaf(CAST_FROM_FN_PTR(address, SharedRuntime::d2l), 1);
1930     __ bind(L);
1931   }
1932     break;
1933   case Bytecodes::_d2f:
1934     __ cvtsd2ss(xmm0, xmm0);
1935     break;
1936   default:
1937     ShouldNotReachHere();
1938   }
1939 #else
1940   // Checking
1941 #ifdef ASSERT
1942   { TosState tos_in  = ilgl;
1943     TosState tos_out = ilgl;
1944     switch (bytecode()) {
1945       case Bytecodes::_i2l: // fall through
1946       case Bytecodes::_i2f: // fall through
1947       case Bytecodes::_i2d: // fall through
1948       case Bytecodes::_i2b: // fall through
1949       case Bytecodes::_i2c: // fall through
1950       case Bytecodes::_i2s: tos_in = itos; break;
1951       case Bytecodes::_l2i: // fall through
1952       case Bytecodes::_l2f: // fall through
1953       case Bytecodes::_l2d: tos_in = ltos; break;
1954       case Bytecodes::_f2i: // fall through
1955       case Bytecodes::_f2l: // fall through
1956       case Bytecodes::_f2d: tos_in = ftos; break;
1957       case Bytecodes::_d2i: // fall through
1958       case Bytecodes::_d2l: // fall through
1959       case Bytecodes::_d2f: tos_in = dtos; break;
1960       default             : ShouldNotReachHere();
1961     }
1962     switch (bytecode()) {
1963       case Bytecodes::_l2i: // fall through
1964       case Bytecodes::_f2i: // fall through
1965       case Bytecodes::_d2i: // fall through
1966       case Bytecodes::_i2b: // fall through
1967       case Bytecodes::_i2c: // fall through
1968       case Bytecodes::_i2s: tos_out = itos; break;
1969       case Bytecodes::_i2l: // fall through
1970       case Bytecodes::_f2l: // fall through
1971       case Bytecodes::_d2l: tos_out = ltos; break;
1972       case Bytecodes::_i2f: // fall through
1973       case Bytecodes::_l2f: // fall through
1974       case Bytecodes::_d2f: tos_out = ftos; break;
1975       case Bytecodes::_i2d: // fall through
1976       case Bytecodes::_l2d: // fall through
1977       case Bytecodes::_f2d: tos_out = dtos; break;
1978       default             : ShouldNotReachHere();
1979     }
1980     transition(tos_in, tos_out);
1981   }
1982 #endif // ASSERT
1983 
1984   // Conversion
1985   // (Note: use push(rcx)/pop(rcx) for 1/2-word stack-ptr manipulation)
1986   switch (bytecode()) {
1987     case Bytecodes::_i2l:
1988       __ extend_sign(rdx, rax);
1989       break;
1990     case Bytecodes::_i2f:
1991       if (UseSSE >= 1) {
1992         __ cvtsi2ssl(xmm0, rax);
1993       } else {
1994         __ push(rax);          // store int on tos
1995         __ fild_s(at_rsp());   // load int to ST0
1996         __ f2ieee();           // truncate to float size
1997         __ pop(rcx);           // adjust rsp
1998       }
1999       break;
2000     case Bytecodes::_i2d:
2001       if (UseSSE >= 2) {
2002         __ cvtsi2sdl(xmm0, rax);
2003       } else {
2004       __ push(rax);          // add one slot for d2ieee()
2005       __ push(rax);          // store int on tos
2006       __ fild_s(at_rsp());   // load int to ST0
2007       __ d2ieee();           // truncate to double size
2008       __ pop(rcx);           // adjust rsp
2009       __ pop(rcx);
2010       }
2011       break;
2012     case Bytecodes::_i2b:
2013       __ shll(rax, 24);      // truncate upper 24 bits
2014       __ sarl(rax, 24);      // and sign-extend byte
2015       LP64_ONLY(__ movsbl(rax, rax));
2016       break;
2017     case Bytecodes::_i2c:
2018       __ andl(rax, 0xFFFF);  // truncate upper 16 bits
2019       LP64_ONLY(__ movzwl(rax, rax));
2020       break;
2021     case Bytecodes::_i2s:
2022       __ shll(rax, 16);      // truncate upper 16 bits
2023       __ sarl(rax, 16);      // and sign-extend short
2024       LP64_ONLY(__ movswl(rax, rax));
2025       break;
2026     case Bytecodes::_l2i:
2027       /* nothing to do */
2028       break;
2029     case Bytecodes::_l2f:
2030       // On 64-bit platforms, the cvtsi2ssq instruction is used to convert
2031       // 64-bit long values to floats. On 32-bit platforms it is not possible
2032       // to use that instruction with 64-bit operands, therefore the FPU is
2033       // used to perform the conversion.
2034       __ push(rdx);          // store long on tos
2035       __ push(rax);
2036       __ fild_d(at_rsp());   // load long to ST0
2037       __ f2ieee();           // truncate to float size
2038       __ pop(rcx);           // adjust rsp
2039       __ pop(rcx);
2040       if (UseSSE >= 1) {
2041         __ push_f();
2042         __ pop_f(xmm0);
2043       }
2044       break;
2045     case Bytecodes::_l2d:
2046       // On 32-bit platforms the FPU is used for conversion because on
2047       // 32-bit platforms it is not not possible to use the cvtsi2sdq
2048       // instruction with 64-bit operands.
2049       __ push(rdx);          // store long on tos
2050       __ push(rax);
2051       __ fild_d(at_rsp());   // load long to ST0
2052       __ d2ieee();           // truncate to double size
2053       __ pop(rcx);           // adjust rsp
2054       __ pop(rcx);
2055       if (UseSSE >= 2) {
2056         __ push_d();
2057         __ pop_d(xmm0);
2058       }
2059       break;
2060     case Bytecodes::_f2i:
2061       // SharedRuntime::f2i does not differentiate between sNaNs and qNaNs
2062       // as it returns 0 for any NaN.
2063       if (UseSSE >= 1) {
2064         __ push_f(xmm0);
2065       } else {
2066         __ push(rcx);          // reserve space for argument
2067         __ fstp_s(at_rsp());   // pass float argument on stack
2068       }
2069       __ call_VM_leaf(CAST_FROM_FN_PTR(address, SharedRuntime::f2i), 1);
2070       break;
2071     case Bytecodes::_f2l:
2072       // SharedRuntime::f2l 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::f2l), 1);
2081       break;
2082     case Bytecodes::_f2d:
2083       if (UseSSE < 1) {
2084         /* nothing to do */
2085       } else if (UseSSE == 1) {
2086         __ push_f(xmm0);
2087         __ pop_f();
2088       } else { // UseSSE >= 2
2089         __ cvtss2sd(xmm0, xmm0);
2090       }
2091       break;
2092     case Bytecodes::_d2i:
2093       if (UseSSE >= 2) {
2094         __ push_d(xmm0);
2095       } else {
2096         __ push(rcx);          // reserve space for argument
2097         __ push(rcx);
2098         __ fstp_d(at_rsp());   // pass double argument on stack
2099       }
2100       __ call_VM_leaf(CAST_FROM_FN_PTR(address, SharedRuntime::d2i), 2);
2101       break;
2102     case Bytecodes::_d2l:
2103       if (UseSSE >= 2) {
2104         __ push_d(xmm0);
2105       } else {
2106         __ push(rcx);          // reserve space for argument
2107         __ push(rcx);
2108         __ fstp_d(at_rsp());   // pass double argument on stack
2109       }
2110       __ call_VM_leaf(CAST_FROM_FN_PTR(address, SharedRuntime::d2l), 2);
2111       break;
2112     case Bytecodes::_d2f:
2113       if (UseSSE <= 1) {
2114         __ push(rcx);          // reserve space for f2ieee()
2115         __ f2ieee();           // truncate to float size
2116         __ pop(rcx);           // adjust rsp
2117         if (UseSSE == 1) {
2118           // The cvtsd2ss instruction is not available if UseSSE==1, therefore
2119           // the conversion is performed using the FPU in this case.
2120           __ push_f();
2121           __ pop_f(xmm0);
2122         }
2123       } else { // UseSSE >= 2
2124         __ cvtsd2ss(xmm0, xmm0);
2125       }
2126       break;
2127     default             :
2128       ShouldNotReachHere();
2129   }
2130 #endif
2131 }
2132 
2133 void TemplateTable::lcmp() {
2134   transition(ltos, itos);
2135 #ifdef _LP64
2136   Label done;
2137   __ pop_l(rdx);
2138   __ cmpq(rdx, rax);
2139   __ movl(rax, -1);
2140   __ jccb(Assembler::less, done);
2141   __ setb(Assembler::notEqual, rax);
2142   __ movzbl(rax, rax);
2143   __ bind(done);
2144 #else
2145 
2146   // y = rdx:rax
2147   __ pop_l(rbx, rcx);             // get x = rcx:rbx
2148   __ lcmp2int(rcx, rbx, rdx, rax);// rcx := cmp(x, y)
2149   __ mov(rax, rcx);
2150 #endif
2151 }
2152 
2153 void TemplateTable::float_cmp(bool is_float, int unordered_result) {
2154   if ((is_float && UseSSE >= 1) ||
2155       (!is_float && UseSSE >= 2)) {
2156     Label done;
2157     if (is_float) {
2158       // XXX get rid of pop here, use ... reg, mem32
2159       __ pop_f(xmm1);
2160       __ ucomiss(xmm1, xmm0);
2161     } else {
2162       // XXX get rid of pop here, use ... reg, mem64
2163       __ pop_d(xmm1);
2164       __ ucomisd(xmm1, xmm0);
2165     }
2166     if (unordered_result < 0) {
2167       __ movl(rax, -1);
2168       __ jccb(Assembler::parity, done);
2169       __ jccb(Assembler::below, done);
2170       __ setb(Assembler::notEqual, rdx);
2171       __ movzbl(rax, rdx);
2172     } else {
2173       __ movl(rax, 1);
2174       __ jccb(Assembler::parity, done);
2175       __ jccb(Assembler::above, done);
2176       __ movl(rax, 0);
2177       __ jccb(Assembler::equal, done);
2178       __ decrementl(rax);
2179     }
2180     __ bind(done);
2181   } else {
2182 #ifdef _LP64
2183     ShouldNotReachHere();
2184 #else
2185     if (is_float) {
2186       __ fld_s(at_rsp());
2187     } else {
2188       __ fld_d(at_rsp());
2189       __ pop(rdx);
2190     }
2191     __ pop(rcx);
2192     __ fcmp2int(rax, unordered_result < 0);
2193 #endif // _LP64
2194   }
2195 }
2196 
2197 void TemplateTable::branch(bool is_jsr, bool is_wide) {
2198   __ get_method(rcx); // rcx holds method
2199   __ profile_taken_branch(rax, rbx); // rax holds updated MDP, rbx
2200                                      // holds bumped taken count
2201 
2202   const ByteSize be_offset = MethodCounters::backedge_counter_offset() +
2203                              InvocationCounter::counter_offset();
2204   const ByteSize inv_offset = MethodCounters::invocation_counter_offset() +
2205                               InvocationCounter::counter_offset();
2206 
2207   // Load up edx with the branch displacement
2208   if (is_wide) {
2209     __ movl(rdx, at_bcp(1));
2210   } else {
2211     __ load_signed_short(rdx, at_bcp(1));
2212   }
2213   __ bswapl(rdx);
2214 
2215   if (!is_wide) {
2216     __ sarl(rdx, 16);
2217   }
2218   LP64_ONLY(__ movl2ptr(rdx, rdx));
2219 
2220   // Handle all the JSR stuff here, then exit.
2221   // It's much shorter and cleaner than intermingling with the non-JSR
2222   // normal-branch stuff occurring below.
2223   if (is_jsr) {
2224     // Pre-load the next target bytecode into rbx
2225     __ load_unsigned_byte(rbx, Address(rbcp, rdx, Address::times_1, 0));
2226 
2227     // compute return address as bci in rax
2228     __ lea(rax, at_bcp((is_wide ? 5 : 3) -
2229                         in_bytes(ConstMethod::codes_offset())));
2230     __ subptr(rax, Address(rcx, Method::const_offset()));
2231     // Adjust the bcp in r13 by the displacement in rdx
2232     __ addptr(rbcp, rdx);
2233     // jsr returns atos that is not an oop
2234     __ push_i(rax);
2235     __ dispatch_only(vtos, true);
2236     return;
2237   }
2238 
2239   // Normal (non-jsr) branch handling
2240 
2241   // Adjust the bcp in r13 by the displacement in rdx
2242   __ addptr(rbcp, rdx);
2243 
2244   assert(UseLoopCounter || !UseOnStackReplacement,
2245          "on-stack-replacement requires loop counters");
2246   Label backedge_counter_overflow;
2247   Label dispatch;
2248   if (UseLoopCounter) {
2249     // increment backedge counter for backward branches
2250     // rax: MDO
2251     // rbx: MDO bumped taken-count
2252     // rcx: method
2253     // rdx: target offset
2254     // r13: target bcp
2255     // r14: locals pointer
2256     __ testl(rdx, rdx);             // check if forward or backward branch
2257     __ jcc(Assembler::positive, dispatch); // count only if backward branch
2258 
2259     // check if MethodCounters exists
2260     Label has_counters;
2261     __ movptr(rax, Address(rcx, Method::method_counters_offset()));
2262     __ testptr(rax, rax);
2263     __ jcc(Assembler::notZero, has_counters);
2264     __ push(rdx);
2265     __ push(rcx);
2266     __ call_VM(noreg, CAST_FROM_FN_PTR(address, InterpreterRuntime::build_method_counters),
2267                rcx);
2268     __ pop(rcx);
2269     __ pop(rdx);
2270     __ movptr(rax, Address(rcx, Method::method_counters_offset()));
2271     __ testptr(rax, rax);
2272     __ jcc(Assembler::zero, dispatch);
2273     __ bind(has_counters);
2274 
2275     Label no_mdo;
2276     int increment = InvocationCounter::count_increment;
2277     if (ProfileInterpreter) {
2278       // Are we profiling?
2279       __ movptr(rbx, Address(rcx, in_bytes(Method::method_data_offset())));
2280       __ testptr(rbx, rbx);
2281       __ jccb(Assembler::zero, no_mdo);
2282       // Increment the MDO backedge counter
2283       const Address mdo_backedge_counter(rbx, in_bytes(MethodData::backedge_counter_offset()) +
2284           in_bytes(InvocationCounter::counter_offset()));
2285       const Address mask(rbx, in_bytes(MethodData::backedge_mask_offset()));
2286       __ increment_mask_and_jump(mdo_backedge_counter, increment, mask, rax, false, Assembler::zero,
2287           UseOnStackReplacement ? &backedge_counter_overflow : NULL);
2288       __ jmp(dispatch);
2289     }
2290     __ bind(no_mdo);
2291     // Increment backedge counter in MethodCounters*
2292     __ movptr(rcx, Address(rcx, Method::method_counters_offset()));
2293     const Address mask(rcx, in_bytes(MethodCounters::backedge_mask_offset()));
2294     __ increment_mask_and_jump(Address(rcx, be_offset), increment, mask,
2295         rax, false, Assembler::zero, UseOnStackReplacement ? &backedge_counter_overflow : NULL);
2296     __ bind(dispatch);
2297   }
2298 
2299   // Pre-load the next target bytecode into rbx
2300   __ load_unsigned_byte(rbx, Address(rbcp, 0));
2301 
2302   // continue with the bytecode @ target
2303   // rax: return bci for jsr's, unused otherwise
2304   // rbx: target bytecode
2305   // r13: target bcp
2306   __ dispatch_only(vtos, true);
2307 
2308   if (UseLoopCounter) {
2309     if (UseOnStackReplacement) {
2310       Label set_mdp;
2311       // invocation counter overflow
2312       __ bind(backedge_counter_overflow);
2313       __ negptr(rdx);
2314       __ addptr(rdx, rbcp); // branch bcp
2315       // IcoResult frequency_counter_overflow([JavaThread*], address branch_bcp)
2316       __ call_VM(noreg,
2317                  CAST_FROM_FN_PTR(address,
2318                                   InterpreterRuntime::frequency_counter_overflow),
2319                  rdx);
2320 
2321       // rax: osr nmethod (osr ok) or NULL (osr not possible)
2322       // rdx: scratch
2323       // r14: locals pointer
2324       // r13: bcp
2325       __ testptr(rax, rax);                        // test result
2326       __ jcc(Assembler::zero, dispatch);         // no osr if null
2327       // nmethod may have been invalidated (VM may block upon call_VM return)
2328       __ cmpb(Address(rax, nmethod::state_offset()), nmethod::in_use);
2329       __ jcc(Assembler::notEqual, dispatch);
2330 
2331       // We have the address of an on stack replacement routine in rax.
2332       // In preparation of invoking it, first we must migrate the locals
2333       // and monitors from off the interpreter frame on the stack.
2334       // Ensure to save the osr nmethod over the migration call,
2335       // it will be preserved in rbx.
2336       __ mov(rbx, rax);
2337 
2338       NOT_LP64(__ get_thread(rcx));
2339 
2340       call_VM(noreg, CAST_FROM_FN_PTR(address, SharedRuntime::OSR_migration_begin));
2341 
2342       // rax is OSR buffer, move it to expected parameter location
2343       LP64_ONLY(__ mov(j_rarg0, rax));
2344       NOT_LP64(__ mov(rcx, rax));
2345       // We use j_rarg definitions here so that registers don't conflict as parameter
2346       // registers change across platforms as we are in the midst of a calling
2347       // sequence to the OSR nmethod and we don't want collision. These are NOT parameters.
2348 
2349       const Register retaddr   = LP64_ONLY(j_rarg2) NOT_LP64(rdi);
2350       const Register sender_sp = LP64_ONLY(j_rarg1) NOT_LP64(rdx);
2351 
2352       // pop the interpreter frame
2353       __ movptr(sender_sp, Address(rbp, frame::interpreter_frame_sender_sp_offset * wordSize)); // get sender sp
2354       __ leave();                                // remove frame anchor
2355       __ pop(retaddr);                           // get return address
2356       __ mov(rsp, sender_sp);                   // set sp to sender sp
2357       // Ensure compiled code always sees stack at proper alignment
2358       __ andptr(rsp, -(StackAlignmentInBytes));
2359 
2360       // unlike x86 we need no specialized return from compiled code
2361       // to the interpreter or the call stub.
2362 
2363       // push the return address
2364       __ push(retaddr);
2365 
2366       // and begin the OSR nmethod
2367       __ jmp(Address(rbx, nmethod::osr_entry_point_offset()));
2368     }
2369   }
2370 }
2371 
2372 void TemplateTable::if_0cmp(Condition cc) {
2373   transition(itos, vtos);
2374   // assume branch is more often taken than not (loops use backward branches)
2375   Label not_taken;
2376   __ testl(rax, rax);
2377   __ jcc(j_not(cc), not_taken);
2378   branch(false, false);
2379   __ bind(not_taken);
2380   __ profile_not_taken_branch(rax);
2381 }
2382 
2383 void TemplateTable::if_icmp(Condition cc) {
2384   transition(itos, vtos);
2385   // assume branch is more often taken than not (loops use backward branches)
2386   Label not_taken;
2387   __ pop_i(rdx);
2388   __ cmpl(rdx, rax);
2389   __ jcc(j_not(cc), not_taken);
2390   branch(false, false);
2391   __ bind(not_taken);
2392   __ profile_not_taken_branch(rax);
2393 }
2394 
2395 void TemplateTable::if_nullcmp(Condition cc) {
2396   transition(atos, vtos);
2397   // assume branch is more often taken than not (loops use backward branches)
2398   Label not_taken;
2399   __ testptr(rax, rax);
2400   __ jcc(j_not(cc), not_taken);
2401   branch(false, false);
2402   __ bind(not_taken);
2403   __ profile_not_taken_branch(rax);
2404 }
2405 
2406 void TemplateTable::if_acmp(Condition cc) {
2407   transition(atos, vtos);
2408   // assume branch is more often taken than not (loops use backward branches)
2409   Label taken, not_taken;
2410   __ pop_ptr(rdx);
2411 
2412   __ profile_acmp(rbx, rdx, rax, rcx);
2413 
2414   const int is_inline_type_mask = markWord::inline_type_pattern;
2415   if (EnableValhalla) {
2416     __ cmpoop(rdx, rax);
2417     __ jcc(Assembler::equal, (cc == equal) ? taken : not_taken);
2418 
2419     // might be substitutable, test if either rax or rdx is null
2420     __ testptr(rax, rax);
2421     __ jcc(Assembler::zero, (cc == equal) ? not_taken : taken);
2422     __ testptr(rdx, rdx);
2423     __ jcc(Assembler::zero, (cc == equal) ? not_taken : taken);
2424 
2425     // and both are values ?
2426     __ movptr(rbx, Address(rdx, oopDesc::mark_offset_in_bytes()));
2427     __ andptr(rbx, Address(rax, oopDesc::mark_offset_in_bytes()));
2428     __ andptr(rbx, is_inline_type_mask);
2429     __ cmpptr(rbx, is_inline_type_mask);
2430     __ jcc(Assembler::notEqual, (cc == equal) ? not_taken : taken);
2431 
2432     // same value klass ?
2433     __ load_metadata(rbx, rdx);
2434     __ load_metadata(rcx, rax);
2435     __ cmpptr(rbx, rcx);
2436     __ jcc(Assembler::notEqual, (cc == equal) ? not_taken : taken);
2437 
2438     // Know both are the same type, let's test for substitutability...
2439     if (cc == equal) {
2440       invoke_is_substitutable(rax, rdx, taken, not_taken);
2441     } else {
2442       invoke_is_substitutable(rax, rdx, not_taken, taken);
2443     }
2444     __ stop("Not reachable");
2445   }
2446 
2447   __ cmpoop(rdx, rax);
2448   __ jcc(j_not(cc), not_taken);
2449   __ bind(taken);
2450   branch(false, false);
2451   __ bind(not_taken);
2452   __ profile_not_taken_branch(rax, true);
2453 }
2454 
2455 void TemplateTable::invoke_is_substitutable(Register aobj, Register bobj,
2456                                             Label& is_subst, Label& not_subst) {
2457   __ call_VM(noreg, CAST_FROM_FN_PTR(address, InterpreterRuntime::is_substitutable), aobj, bobj);
2458   // Restored...rax answer, jmp to outcome...
2459   __ testl(rax, rax);
2460   __ jcc(Assembler::zero, not_subst);
2461   __ jmp(is_subst);
2462 }
2463 
2464 void TemplateTable::ret() {
2465   transition(vtos, vtos);
2466   locals_index(rbx);
2467   LP64_ONLY(__ movslq(rbx, iaddress(rbx))); // get return bci, compute return bcp
2468   NOT_LP64(__ movptr(rbx, iaddress(rbx)));
2469   __ profile_ret(rbx, rcx);
2470   __ get_method(rax);
2471   __ movptr(rbcp, Address(rax, Method::const_offset()));
2472   __ lea(rbcp, Address(rbcp, rbx, Address::times_1,
2473                       ConstMethod::codes_offset()));
2474   __ dispatch_next(vtos, 0, true);
2475 }
2476 
2477 void TemplateTable::wide_ret() {
2478   transition(vtos, vtos);
2479   locals_index_wide(rbx);
2480   __ movptr(rbx, aaddress(rbx)); // get return bci, compute return bcp
2481   __ profile_ret(rbx, rcx);
2482   __ get_method(rax);
2483   __ movptr(rbcp, Address(rax, Method::const_offset()));
2484   __ lea(rbcp, Address(rbcp, rbx, Address::times_1, ConstMethod::codes_offset()));
2485   __ dispatch_next(vtos, 0, true);
2486 }
2487 
2488 void TemplateTable::tableswitch() {
2489   Label default_case, continue_execution;
2490   transition(itos, vtos);
2491 
2492   // align r13/rsi
2493   __ lea(rbx, at_bcp(BytesPerInt));
2494   __ andptr(rbx, -BytesPerInt);
2495   // load lo & hi
2496   __ movl(rcx, Address(rbx, BytesPerInt));
2497   __ movl(rdx, Address(rbx, 2 * BytesPerInt));
2498   __ bswapl(rcx);
2499   __ bswapl(rdx);
2500   // check against lo & hi
2501   __ cmpl(rax, rcx);
2502   __ jcc(Assembler::less, default_case);
2503   __ cmpl(rax, rdx);
2504   __ jcc(Assembler::greater, default_case);
2505   // lookup dispatch offset
2506   __ subl(rax, rcx);
2507   __ movl(rdx, Address(rbx, rax, Address::times_4, 3 * BytesPerInt));
2508   __ profile_switch_case(rax, rbx, rcx);
2509   // continue execution
2510   __ bind(continue_execution);
2511   __ bswapl(rdx);
2512   LP64_ONLY(__ movl2ptr(rdx, rdx));
2513   __ load_unsigned_byte(rbx, Address(rbcp, rdx, Address::times_1));
2514   __ addptr(rbcp, rdx);
2515   __ dispatch_only(vtos, true);
2516   // handle default
2517   __ bind(default_case);
2518   __ profile_switch_default(rax);
2519   __ movl(rdx, Address(rbx, 0));
2520   __ jmp(continue_execution);
2521 }
2522 
2523 void TemplateTable::lookupswitch() {
2524   transition(itos, itos);
2525   __ stop("lookupswitch bytecode should have been rewritten");
2526 }
2527 
2528 void TemplateTable::fast_linearswitch() {
2529   transition(itos, vtos);
2530   Label loop_entry, loop, found, continue_execution;
2531   // bswap rax so we can avoid bswapping the table entries
2532   __ bswapl(rax);
2533   // align r13
2534   __ lea(rbx, at_bcp(BytesPerInt)); // btw: should be able to get rid of
2535                                     // this instruction (change offsets
2536                                     // below)
2537   __ andptr(rbx, -BytesPerInt);
2538   // set counter
2539   __ movl(rcx, Address(rbx, BytesPerInt));
2540   __ bswapl(rcx);
2541   __ jmpb(loop_entry);
2542   // table search
2543   __ bind(loop);
2544   __ cmpl(rax, Address(rbx, rcx, Address::times_8, 2 * BytesPerInt));
2545   __ jcc(Assembler::equal, found);
2546   __ bind(loop_entry);
2547   __ decrementl(rcx);
2548   __ jcc(Assembler::greaterEqual, loop);
2549   // default case
2550   __ profile_switch_default(rax);
2551   __ movl(rdx, Address(rbx, 0));
2552   __ jmp(continue_execution);
2553   // entry found -> get offset
2554   __ bind(found);
2555   __ movl(rdx, Address(rbx, rcx, Address::times_8, 3 * BytesPerInt));
2556   __ profile_switch_case(rcx, rax, rbx);
2557   // continue execution
2558   __ bind(continue_execution);
2559   __ bswapl(rdx);
2560   __ movl2ptr(rdx, rdx);
2561   __ load_unsigned_byte(rbx, Address(rbcp, rdx, Address::times_1));
2562   __ addptr(rbcp, rdx);
2563   __ dispatch_only(vtos, true);
2564 }
2565 
2566 void TemplateTable::fast_binaryswitch() {
2567   transition(itos, vtos);
2568   // Implementation using the following core algorithm:
2569   //
2570   // int binary_search(int key, LookupswitchPair* array, int n) {
2571   //   // Binary search according to "Methodik des Programmierens" by
2572   //   // Edsger W. Dijkstra and W.H.J. Feijen, Addison Wesley Germany 1985.
2573   //   int i = 0;
2574   //   int j = n;
2575   //   while (i+1 < j) {
2576   //     // invariant P: 0 <= i < j <= n and (a[i] <= key < a[j] or Q)
2577   //     // with      Q: for all i: 0 <= i < n: key < a[i]
2578   //     // where a stands for the array and assuming that the (inexisting)
2579   //     // element a[n] is infinitely big.
2580   //     int h = (i + j) >> 1;
2581   //     // i < h < j
2582   //     if (key < array[h].fast_match()) {
2583   //       j = h;
2584   //     } else {
2585   //       i = h;
2586   //     }
2587   //   }
2588   //   // R: a[i] <= key < a[i+1] or Q
2589   //   // (i.e., if key is within array, i is the correct index)
2590   //   return i;
2591   // }
2592 
2593   // Register allocation
2594   const Register key   = rax; // already set (tosca)
2595   const Register array = rbx;
2596   const Register i     = rcx;
2597   const Register j     = rdx;
2598   const Register h     = rdi;
2599   const Register temp  = rsi;
2600 
2601   // Find array start
2602   NOT_LP64(__ save_bcp());
2603 
2604   __ lea(array, at_bcp(3 * BytesPerInt)); // btw: should be able to
2605                                           // get rid of this
2606                                           // instruction (change
2607                                           // offsets below)
2608   __ andptr(array, -BytesPerInt);
2609 
2610   // Initialize i & j
2611   __ xorl(i, i);                            // i = 0;
2612   __ movl(j, Address(array, -BytesPerInt)); // j = length(array);
2613 
2614   // Convert j into native byteordering
2615   __ bswapl(j);
2616 
2617   // And start
2618   Label entry;
2619   __ jmp(entry);
2620 
2621   // binary search loop
2622   {
2623     Label loop;
2624     __ bind(loop);
2625     // int h = (i + j) >> 1;
2626     __ leal(h, Address(i, j, Address::times_1)); // h = i + j;
2627     __ sarl(h, 1);                               // h = (i + j) >> 1;
2628     // if (key < array[h].fast_match()) {
2629     //   j = h;
2630     // } else {
2631     //   i = h;
2632     // }
2633     // Convert array[h].match to native byte-ordering before compare
2634     __ movl(temp, Address(array, h, Address::times_8));
2635     __ bswapl(temp);
2636     __ cmpl(key, temp);
2637     // j = h if (key <  array[h].fast_match())
2638     __ cmov32(Assembler::less, j, h);
2639     // i = h if (key >= array[h].fast_match())
2640     __ cmov32(Assembler::greaterEqual, i, h);
2641     // while (i+1 < j)
2642     __ bind(entry);
2643     __ leal(h, Address(i, 1)); // i+1
2644     __ cmpl(h, j);             // i+1 < j
2645     __ jcc(Assembler::less, loop);
2646   }
2647 
2648   // end of binary search, result index is i (must check again!)
2649   Label default_case;
2650   // Convert array[i].match to native byte-ordering before compare
2651   __ movl(temp, Address(array, i, Address::times_8));
2652   __ bswapl(temp);
2653   __ cmpl(key, temp);
2654   __ jcc(Assembler::notEqual, default_case);
2655 
2656   // entry found -> j = offset
2657   __ movl(j , Address(array, i, Address::times_8, BytesPerInt));
2658   __ profile_switch_case(i, key, array);
2659   __ bswapl(j);
2660   LP64_ONLY(__ movslq(j, j));
2661 
2662   NOT_LP64(__ restore_bcp());
2663   NOT_LP64(__ restore_locals());                           // restore rdi
2664 
2665   __ load_unsigned_byte(rbx, Address(rbcp, j, Address::times_1));
2666   __ addptr(rbcp, j);
2667   __ dispatch_only(vtos, true);
2668 
2669   // default case -> j = default offset
2670   __ bind(default_case);
2671   __ profile_switch_default(i);
2672   __ movl(j, Address(array, -2 * BytesPerInt));
2673   __ bswapl(j);
2674   LP64_ONLY(__ movslq(j, j));
2675 
2676   NOT_LP64(__ restore_bcp());
2677   NOT_LP64(__ restore_locals());
2678 
2679   __ load_unsigned_byte(rbx, Address(rbcp, j, Address::times_1));
2680   __ addptr(rbcp, j);
2681   __ dispatch_only(vtos, true);
2682 }
2683 
2684 void TemplateTable::_return(TosState state) {
2685   transition(state, state);
2686 
2687   assert(_desc->calls_vm(),
2688          "inconsistent calls_vm information"); // call in remove_activation
2689 
2690   if (_desc->bytecode() == Bytecodes::_return_register_finalizer) {
2691     assert(state == vtos, "only valid state");
2692     Register robj = LP64_ONLY(c_rarg1) NOT_LP64(rax);
2693     __ movptr(robj, aaddress(0));
2694     Register tmp_load_klass = LP64_ONLY(rscratch1) NOT_LP64(noreg);
2695     __ load_klass(rdi, robj, tmp_load_klass);
2696     __ movl(rdi, Address(rdi, Klass::access_flags_offset()));
2697     __ testl(rdi, JVM_ACC_HAS_FINALIZER);
2698     Label skip_register_finalizer;
2699     __ jcc(Assembler::zero, skip_register_finalizer);
2700 
2701     __ call_VM(noreg, CAST_FROM_FN_PTR(address, InterpreterRuntime::register_finalizer), robj);
2702 
2703     __ bind(skip_register_finalizer);
2704   }
2705 
2706   if (_desc->bytecode() != Bytecodes::_return_register_finalizer) {
2707     Label no_safepoint;
2708     NOT_PRODUCT(__ block_comment("Thread-local Safepoint poll"));
2709 #ifdef _LP64
2710     __ testb(Address(r15_thread, JavaThread::polling_word_offset()), SafepointMechanism::poll_bit());
2711 #else
2712     const Register thread = rdi;
2713     __ get_thread(thread);
2714     __ testb(Address(thread, JavaThread::polling_word_offset()), SafepointMechanism::poll_bit());
2715 #endif
2716     __ jcc(Assembler::zero, no_safepoint);
2717     __ push(state);
2718     __ call_VM(noreg, CAST_FROM_FN_PTR(address,
2719                                        InterpreterRuntime::at_safepoint));
2720     __ pop(state);
2721     __ bind(no_safepoint);
2722   }
2723 
2724   // Narrow result if state is itos but result type is smaller.
2725   // Need to narrow in the return bytecode rather than in generate_return_entry
2726   // since compiled code callers expect the result to already be narrowed.
2727   if (state == itos) {
2728     __ narrow(rax);
2729   }
2730 
2731   __ remove_activation(state, rbcp, true, true, true);
2732 
2733   __ jmp(rbcp);
2734 }
2735 
2736 // ----------------------------------------------------------------------------
2737 // Volatile variables demand their effects be made known to all CPU's
2738 // in order.  Store buffers on most chips allow reads & writes to
2739 // reorder; the JMM's ReadAfterWrite.java test fails in -Xint mode
2740 // without some kind of memory barrier (i.e., it's not sufficient that
2741 // the interpreter does not reorder volatile references, the hardware
2742 // also must not reorder them).
2743 //
2744 // According to the new Java Memory Model (JMM):
2745 // (1) All volatiles are serialized wrt to each other.  ALSO reads &
2746 //     writes act as aquire & release, so:
2747 // (2) A read cannot let unrelated NON-volatile memory refs that
2748 //     happen after the read float up to before the read.  It's OK for
2749 //     non-volatile memory refs that happen before the volatile read to
2750 //     float down below it.
2751 // (3) Similar a volatile write cannot let unrelated NON-volatile
2752 //     memory refs that happen BEFORE the write float down to after the
2753 //     write.  It's OK for non-volatile memory refs that happen after the
2754 //     volatile write to float up before it.
2755 //
2756 // We only put in barriers around volatile refs (they are expensive),
2757 // not _between_ memory refs (that would require us to track the
2758 // flavor of the previous memory refs).  Requirements (2) and (3)
2759 // require some barriers before volatile stores and after volatile
2760 // loads.  These nearly cover requirement (1) but miss the
2761 // volatile-store-volatile-load case.  This final case is placed after
2762 // volatile-stores although it could just as well go before
2763 // volatile-loads.
2764 
2765 void TemplateTable::volatile_barrier(Assembler::Membar_mask_bits order_constraint ) {
2766   // Helper function to insert a is-volatile test and memory barrier
2767   __ membar(order_constraint);
2768 }
2769 
2770 void TemplateTable::resolve_cache_and_index(int byte_no,
2771                                             Register cache,
2772                                             Register index,
2773                                             size_t index_size) {
2774   const Register temp = rbx;
2775   assert_different_registers(cache, index, temp);
2776 
2777   Label L_clinit_barrier_slow;
2778   Label resolved;
2779 
2780   Bytecodes::Code code = bytecode();
2781   switch (code) {
2782   case Bytecodes::_nofast_getfield: code = Bytecodes::_getfield; break;
2783   case Bytecodes::_nofast_putfield: code = Bytecodes::_putfield; break;
2784   default: break;
2785   }
2786 
2787   assert(byte_no == f1_byte || byte_no == f2_byte, "byte_no out of range");
2788   __ get_cache_and_index_and_bytecode_at_bcp(cache, index, temp, byte_no, 1, index_size);
2789   __ cmpl(temp, code);  // have we resolved this bytecode?
2790   __ jcc(Assembler::equal, resolved);
2791 
2792   // resolve first time through
2793   // Class initialization barrier slow path lands here as well.
2794   __ bind(L_clinit_barrier_slow);
2795   address entry = CAST_FROM_FN_PTR(address, InterpreterRuntime::resolve_from_cache);
2796   __ movl(temp, code);
2797   __ call_VM(noreg, entry, temp);
2798   // Update registers with resolved info
2799   __ get_cache_and_index_at_bcp(cache, index, 1, index_size);
2800 
2801   __ bind(resolved);
2802 
2803   // Class initialization barrier for static methods
2804   if (VM_Version::supports_fast_class_init_checks() && bytecode() == Bytecodes::_invokestatic) {
2805     const Register method = temp;
2806     const Register klass  = temp;
2807     const Register thread = LP64_ONLY(r15_thread) NOT_LP64(noreg);
2808     assert(thread != noreg, "x86_32 not supported");
2809 
2810     __ load_resolved_method_at_index(byte_no, method, cache, index);
2811     __ load_method_holder(klass, method);
2812     __ clinit_barrier(klass, thread, NULL /*L_fast_path*/, &L_clinit_barrier_slow);
2813   }
2814 }
2815 
2816 // The cache and index registers must be set before call
2817 void TemplateTable::load_field_cp_cache_entry(Register obj,
2818                                               Register cache,
2819                                               Register index,
2820                                               Register off,
2821                                               Register flags,
2822                                               bool is_static = false) {
2823   assert_different_registers(cache, index, flags, off);
2824 
2825   ByteSize cp_base_offset = ConstantPoolCache::base_offset();
2826   // Field offset
2827   __ movptr(off, Address(cache, index, Address::times_ptr,
2828                          in_bytes(cp_base_offset +
2829                                   ConstantPoolCacheEntry::f2_offset())));
2830   // Flags
2831   __ movl(flags, Address(cache, index, Address::times_ptr,
2832                          in_bytes(cp_base_offset +
2833                                   ConstantPoolCacheEntry::flags_offset())));
2834 
2835   // klass overwrite register
2836   if (is_static) {
2837     __ movptr(obj, Address(cache, index, Address::times_ptr,
2838                            in_bytes(cp_base_offset +
2839                                     ConstantPoolCacheEntry::f1_offset())));
2840     const int mirror_offset = in_bytes(Klass::java_mirror_offset());
2841     __ movptr(obj, Address(obj, mirror_offset));
2842     __ resolve_oop_handle(obj);
2843   }
2844 }
2845 
2846 void TemplateTable::load_invoke_cp_cache_entry(int byte_no,
2847                                                Register method,
2848                                                Register itable_index,
2849                                                Register flags,
2850                                                bool is_invokevirtual,
2851                                                bool is_invokevfinal, /*unused*/
2852                                                bool is_invokedynamic) {
2853   // setup registers
2854   const Register cache = rcx;
2855   const Register index = rdx;
2856   assert_different_registers(method, flags);
2857   assert_different_registers(method, cache, index);
2858   assert_different_registers(itable_index, flags);
2859   assert_different_registers(itable_index, cache, index);
2860   // determine constant pool cache field offsets
2861   assert(is_invokevirtual == (byte_no == f2_byte), "is_invokevirtual flag redundant");
2862   const int flags_offset = in_bytes(ConstantPoolCache::base_offset() +
2863                                     ConstantPoolCacheEntry::flags_offset());
2864   // access constant pool cache fields
2865   const int index_offset = in_bytes(ConstantPoolCache::base_offset() +
2866                                     ConstantPoolCacheEntry::f2_offset());
2867 
2868   size_t index_size = (is_invokedynamic ? sizeof(u4) : sizeof(u2));
2869   resolve_cache_and_index(byte_no, cache, index, index_size);
2870   __ load_resolved_method_at_index(byte_no, method, cache, index);
2871 
2872   if (itable_index != noreg) {
2873     // pick up itable or appendix index from f2 also:
2874     __ movptr(itable_index, Address(cache, index, Address::times_ptr, index_offset));
2875   }
2876   __ movl(flags, Address(cache, index, Address::times_ptr, flags_offset));
2877 }
2878 
2879 // The registers cache and index expected to be set before call.
2880 // Correct values of the cache and index registers are preserved.
2881 void TemplateTable::jvmti_post_field_access(Register cache,
2882                                             Register index,
2883                                             bool is_static,
2884                                             bool has_tos) {
2885   if (JvmtiExport::can_post_field_access()) {
2886     // Check to see if a field access watch has been set before we take
2887     // the time to call into the VM.
2888     Label L1;
2889     assert_different_registers(cache, index, rax);
2890     __ mov32(rax, ExternalAddress((address) JvmtiExport::get_field_access_count_addr()));
2891     __ testl(rax,rax);
2892     __ jcc(Assembler::zero, L1);
2893 
2894     // cache entry pointer
2895     __ addptr(cache, in_bytes(ConstantPoolCache::base_offset()));
2896     __ shll(index, LogBytesPerWord);
2897     __ addptr(cache, index);
2898     if (is_static) {
2899       __ xorptr(rax, rax);      // NULL object reference
2900     } else {
2901       __ pop(atos);         // Get the object
2902       __ verify_oop(rax);
2903       __ push(atos);        // Restore stack state
2904     }
2905     // rax,:   object pointer or NULL
2906     // cache: cache entry pointer
2907     __ call_VM(noreg, CAST_FROM_FN_PTR(address, InterpreterRuntime::post_field_access),
2908                rax, cache);
2909     __ get_cache_and_index_at_bcp(cache, index, 1);
2910     __ bind(L1);
2911   }
2912 }
2913 
2914 void TemplateTable::pop_and_check_object(Register r) {
2915   __ pop_ptr(r);
2916   __ null_check(r);  // for field access must check obj.
2917   __ verify_oop(r);
2918 }
2919 
2920 void TemplateTable::getfield_or_static(int byte_no, bool is_static, RewriteControl rc) {
2921   transition(vtos, vtos);
2922 
2923   const Register cache = rcx;
2924   const Register index = rdx;
2925   const Register obj   = LP64_ONLY(c_rarg3) NOT_LP64(rcx);
2926   const Register off   = rbx;
2927   const Register flags = rax;
2928   const Register bc    = LP64_ONLY(c_rarg3) NOT_LP64(rcx); // uses same reg as obj, so don't mix them
2929   const Register flags2 = rdx;
2930 
2931   resolve_cache_and_index(byte_no, cache, index, sizeof(u2));
2932   jvmti_post_field_access(cache, index, is_static, false);
2933   load_field_cp_cache_entry(obj, cache, index, off, flags, is_static);
2934 


2935   const Address field(obj, off, Address::times_1, 0*wordSize);
2936 
2937   Label Done, notByte, notBool, notInt, notShort, notChar, notLong, notFloat, notObj, notInlineType;
2938 
2939   if (!is_static) {
2940     __ movptr(rcx, Address(cache, index, Address::times_ptr,
2941                            in_bytes(ConstantPoolCache::base_offset() +
2942                                     ConstantPoolCacheEntry::f1_offset())));
2943   }
2944 
2945   __ movl(flags2, flags);
2946 
2947   __ shrl(flags, ConstantPoolCacheEntry::tos_state_shift);
2948   // Make sure we don't need to mask edx after the above shift
2949   assert(btos == 0, "change code, btos != 0");
2950 
2951   __ andl(flags, ConstantPoolCacheEntry::tos_state_mask);
2952 
2953   __ jcc(Assembler::notZero, notByte);
2954   // btos
2955   if (!is_static) pop_and_check_object(obj);
2956   __ access_load_at(T_BYTE, IN_HEAP, rax, field, noreg, noreg);
2957   __ push(btos);
2958   // Rewrite bytecode to be faster
2959   if (!is_static && rc == may_rewrite) {
2960     patch_bytecode(Bytecodes::_fast_bgetfield, bc, rbx);
2961   }
2962   __ jmp(Done);
2963 
2964   __ bind(notByte);
2965 
2966   __ cmpl(flags, ztos);
2967   __ jcc(Assembler::notEqual, notBool);
2968    if (!is_static) pop_and_check_object(obj);
2969   // ztos (same code as btos)
2970   __ access_load_at(T_BOOLEAN, IN_HEAP, rax, field, noreg, noreg);
2971   __ push(ztos);
2972   // Rewrite bytecode to be faster
2973   if (!is_static && rc == may_rewrite) {
2974     // use btos rewriting, no truncating to t/f bit is needed for getfield.
2975     patch_bytecode(Bytecodes::_fast_bgetfield, bc, rbx);
2976   }
2977   __ jmp(Done);
2978 
2979   __ bind(notBool);
2980   __ cmpl(flags, atos);
2981   __ jcc(Assembler::notEqual, notObj);
2982   // atos
2983   if (!EnableValhalla) {
2984     if (!is_static) pop_and_check_object(obj);
2985     do_oop_load(_masm, field, rax);
2986     __ push(atos);
2987     if (!is_static && rc == may_rewrite) {
2988       patch_bytecode(Bytecodes::_fast_agetfield, bc, rbx);
2989     }
2990     __ jmp(Done);
2991   } else {
2992     if (is_static) {
2993       __ load_heap_oop(rax, field);
2994       if (EnableValhalla) {
2995         Label is_null_free_inline_type, uninitialized;
2996         // Issue below if the static field has not been initialized yet
2997         __ test_field_is_null_free_inline_type(flags2, rscratch1, is_null_free_inline_type);
2998           // field is not a null free inline type
2999           __ push(atos);
3000           __ jmp(Done);
3001         // field is a null free inline type, must not return null even if uninitialized
3002         __ bind(is_null_free_inline_type);
3003            __ testptr(rax, rax);
3004           __ jcc(Assembler::zero, uninitialized);
3005             __ push(atos);
3006             __ jmp(Done);
3007           __ bind(uninitialized);
3008             __ andl(flags2, ConstantPoolCacheEntry::field_index_mask);
3009   #ifdef _LP64
3010             Label slow_case, finish;
3011             __ cmpb(Address(rcx, InstanceKlass::init_state_offset()), InstanceKlass::fully_initialized);
3012             __ jcc(Assembler::notEqual, slow_case);
3013           __ get_default_value_oop(rcx, off, rax);
3014           __ jmp(finish);
3015           __ bind(slow_case);
3016   #endif // LP64
3017             __ call_VM(rax, CAST_FROM_FN_PTR(address, InterpreterRuntime::uninitialized_static_inline_type_field),
3018                   obj, flags2);
3019   #ifdef _LP64
3020             __ bind(finish);
3021   #endif // _LP64
3022       }
3023         __ verify_oop(rax);
3024         __ push(atos);
3025         __ jmp(Done);
3026     } else {
3027       Label is_inlined, nonnull, is_inline_type, rewrite_inline;
3028       if (EnableValhalla) {
3029         __ test_field_is_null_free_inline_type(flags2, rscratch1, is_inline_type);
3030       }
3031       // field is not a null free inline type
3032       pop_and_check_object(obj);
3033       __ load_heap_oop(rax, field);
3034       __ push(atos);
3035       if (rc == may_rewrite) {
3036         patch_bytecode(Bytecodes::_fast_agetfield, bc, rbx);
3037       }
3038       __ jmp(Done);
3039       if (EnableValhalla) {
3040         __ bind(is_inline_type);
3041           __ test_field_is_inlined(flags2, rscratch1, is_inlined);
3042             // field is not inlined
3043             __ movptr(rax, rcx);  // small dance required to preserve the klass_holder somewhere
3044             pop_and_check_object(obj);
3045             __ push(rax);
3046             __ load_heap_oop(rax, field);
3047             __ pop(rcx);
3048             __ testptr(rax, rax);
3049             __ jcc(Assembler::notZero, nonnull);
3050               __ andl(flags2, ConstantPoolCacheEntry::field_index_mask);
3051               __ get_inline_type_field_klass(rcx, flags2, rbx);
3052               __ get_default_value_oop(rbx, rcx, rax);
3053             __ bind(nonnull);
3054             __ verify_oop(rax);
3055             __ push(atos);
3056             __ jmp(rewrite_inline);
3057           __ bind(is_inlined);
3058           // field is inlined
3059             __ andl(flags2, ConstantPoolCacheEntry::field_index_mask);
3060             pop_and_check_object(rax);
3061             __ read_inlined_field(rcx, flags2, rbx, rax);
3062             __ verify_oop(rax);
3063             __ push(atos);
3064         __ bind(rewrite_inline);
3065         if (rc == may_rewrite) {
3066           patch_bytecode(Bytecodes::_fast_qgetfield, bc, rbx);
3067         }
3068         __ jmp(Done);
3069       }
3070     }
3071   }

3072 
3073   __ bind(notObj);
3074 
3075   if (!is_static) pop_and_check_object(obj);
3076 
3077   __ cmpl(flags, itos);
3078   __ jcc(Assembler::notEqual, notInt);
3079   // itos
3080   __ access_load_at(T_INT, IN_HEAP, rax, field, noreg, noreg);
3081   __ push(itos);
3082   // Rewrite bytecode to be faster
3083   if (!is_static && rc == may_rewrite) {
3084     patch_bytecode(Bytecodes::_fast_igetfield, bc, rbx);
3085   }
3086   __ jmp(Done);
3087 
3088   __ bind(notInt);
3089   __ cmpl(flags, ctos);
3090   __ jcc(Assembler::notEqual, notChar);
3091   // ctos
3092   __ access_load_at(T_CHAR, IN_HEAP, rax, field, noreg, noreg);
3093   __ push(ctos);
3094   // Rewrite bytecode to be faster
3095   if (!is_static && rc == may_rewrite) {
3096     patch_bytecode(Bytecodes::_fast_cgetfield, bc, rbx);
3097   }
3098   __ jmp(Done);
3099 
3100   __ bind(notChar);
3101   __ cmpl(flags, stos);
3102   __ jcc(Assembler::notEqual, notShort);
3103   // stos
3104   __ access_load_at(T_SHORT, IN_HEAP, rax, field, noreg, noreg);
3105   __ push(stos);
3106   // Rewrite bytecode to be faster
3107   if (!is_static && rc == may_rewrite) {
3108     patch_bytecode(Bytecodes::_fast_sgetfield, bc, rbx);
3109   }
3110   __ jmp(Done);
3111 
3112   __ bind(notShort);
3113   __ cmpl(flags, ltos);
3114   __ jcc(Assembler::notEqual, notLong);
3115   // ltos
3116     // Generate code as if volatile (x86_32).  There just aren't enough registers to
3117     // save that information and this code is faster than the test.
3118   __ access_load_at(T_LONG, IN_HEAP | MO_RELAXED, noreg /* ltos */, field, noreg, noreg);
3119   __ push(ltos);
3120   // Rewrite bytecode to be faster
3121   LP64_ONLY(if (!is_static && rc == may_rewrite) patch_bytecode(Bytecodes::_fast_lgetfield, bc, rbx));
3122   __ jmp(Done);
3123 
3124   __ bind(notLong);
3125   __ cmpl(flags, ftos);
3126   __ jcc(Assembler::notEqual, notFloat);
3127   // ftos
3128 
3129   __ access_load_at(T_FLOAT, IN_HEAP, noreg /* ftos */, field, noreg, noreg);
3130   __ push(ftos);
3131   // Rewrite bytecode to be faster
3132   if (!is_static && rc == may_rewrite) {
3133     patch_bytecode(Bytecodes::_fast_fgetfield, bc, rbx);
3134   }
3135   __ jmp(Done);
3136 
3137   __ bind(notFloat);
3138 #ifdef ASSERT
3139   Label notDouble;
3140   __ cmpl(flags, dtos);
3141   __ jcc(Assembler::notEqual, notDouble);
3142 #endif
3143   // dtos
3144   // MO_RELAXED: for the case of volatile field, in fact it adds no extra work for the underlying implementation
3145   __ access_load_at(T_DOUBLE, IN_HEAP | MO_RELAXED, noreg /* dtos */, field, noreg, noreg);
3146   __ push(dtos);
3147   // Rewrite bytecode to be faster
3148   if (!is_static && rc == may_rewrite) {
3149     patch_bytecode(Bytecodes::_fast_dgetfield, bc, rbx);
3150   }
3151 #ifdef ASSERT
3152   __ jmp(Done);
3153 
3154   __ bind(notDouble);
3155   __ stop("Bad state");
3156 #endif
3157 
3158   __ bind(Done);
3159   // [jk] not needed currently
3160   // volatile_barrier(Assembler::Membar_mask_bits(Assembler::LoadLoad |
3161   //                                              Assembler::LoadStore));
3162 }
3163 
3164 void TemplateTable::getfield(int byte_no) {
3165   getfield_or_static(byte_no, false);
3166 }
3167 
3168 void TemplateTable::nofast_getfield(int byte_no) {
3169   getfield_or_static(byte_no, false, may_not_rewrite);
3170 }
3171 
3172 void TemplateTable::getstatic(int byte_no) {
3173   getfield_or_static(byte_no, true);
3174 }
3175 
3176 void TemplateTable::withfield() {
3177   transition(vtos, atos);
3178 
3179   Register cache = LP64_ONLY(c_rarg1) NOT_LP64(rcx);
3180   Register index = LP64_ONLY(c_rarg2) NOT_LP64(rdx);
3181 
3182   resolve_cache_and_index(f2_byte, cache, index, sizeof(u2));
3183 
3184   Register cpentry = rbx;
3185 
3186   ByteSize cp_base_offset = ConstantPoolCache::base_offset();
3187 
3188   __ lea(cpentry, Address(cache, index, Address::times_ptr,
3189                          in_bytes(cp_base_offset)));
3190   __ lea(rax, at_tos());
3191   __ call_VM(rbx, CAST_FROM_FN_PTR(address, InterpreterRuntime::withfield), cpentry, rax);
3192   // new value type is returned in rbx
3193   // stack adjustment is returned in rax
3194   __ verify_oop(rbx);
3195   __ addptr(rsp, rax);
3196   __ movptr(rax, rbx);
3197 }
3198 
3199 // The registers cache and index expected to be set before call.
3200 // The function may destroy various registers, just not the cache and index registers.
3201 void TemplateTable::jvmti_post_field_mod(Register cache, Register index, bool is_static) {
3202 
3203   const Register robj = LP64_ONLY(c_rarg2)   NOT_LP64(rax);
3204   const Register RBX  = LP64_ONLY(c_rarg1)   NOT_LP64(rbx);
3205   const Register RCX  = LP64_ONLY(c_rarg3)   NOT_LP64(rcx);
3206   const Register RDX  = LP64_ONLY(rscratch1) NOT_LP64(rdx);
3207 
3208   ByteSize cp_base_offset = ConstantPoolCache::base_offset();
3209 
3210   if (JvmtiExport::can_post_field_modification()) {
3211     // Check to see if a field modification watch has been set before
3212     // we take the time to call into the VM.
3213     Label L1;
3214     assert_different_registers(cache, index, rax);
3215     __ mov32(rax, ExternalAddress((address)JvmtiExport::get_field_modification_count_addr()));
3216     __ testl(rax, rax);
3217     __ jcc(Assembler::zero, L1);
3218 
3219     __ get_cache_and_index_at_bcp(robj, RDX, 1);
3220 
3221 
3222     if (is_static) {
3223       // Life is simple.  Null out the object pointer.
3224       __ xorl(RBX, RBX);
3225 
3226     } else {
3227       // Life is harder. The stack holds the value on top, followed by
3228       // the object.  We don't know the size of the value, though; it
3229       // could be one or two words depending on its type. As a result,
3230       // we must find the type to determine where the object is.
3231 #ifndef _LP64
3232       Label two_word, valsize_known;
3233 #endif
3234       __ movl(RCX, Address(robj, RDX,
3235                            Address::times_ptr,
3236                            in_bytes(cp_base_offset +
3237                                      ConstantPoolCacheEntry::flags_offset())));
3238       NOT_LP64(__ mov(rbx, rsp));
3239       __ shrl(RCX, ConstantPoolCacheEntry::tos_state_shift);
3240 
3241       // Make sure we don't need to mask rcx after the above shift
3242       ConstantPoolCacheEntry::verify_tos_state_shift();
3243 #ifdef _LP64
3244       __ movptr(c_rarg1, at_tos_p1());  // initially assume a one word jvalue
3245       __ cmpl(c_rarg3, ltos);
3246       __ cmovptr(Assembler::equal,
3247                  c_rarg1, at_tos_p2()); // ltos (two word jvalue)
3248       __ cmpl(c_rarg3, dtos);
3249       __ cmovptr(Assembler::equal,
3250                  c_rarg1, at_tos_p2()); // dtos (two word jvalue)
3251 #else
3252       __ cmpl(rcx, ltos);
3253       __ jccb(Assembler::equal, two_word);
3254       __ cmpl(rcx, dtos);
3255       __ jccb(Assembler::equal, two_word);
3256       __ addptr(rbx, Interpreter::expr_offset_in_bytes(1)); // one word jvalue (not ltos, dtos)
3257       __ jmpb(valsize_known);
3258 
3259       __ bind(two_word);
3260       __ addptr(rbx, Interpreter::expr_offset_in_bytes(2)); // two words jvalue
3261 
3262       __ bind(valsize_known);
3263       // setup object pointer
3264       __ movptr(rbx, Address(rbx, 0));
3265 #endif
3266     }
3267     // cache entry pointer
3268     __ addptr(robj, in_bytes(cp_base_offset));
3269     __ shll(RDX, LogBytesPerWord);
3270     __ addptr(robj, RDX);
3271     // object (tos)
3272     __ mov(RCX, rsp);
3273     // c_rarg1: object pointer set up above (NULL if static)
3274     // c_rarg2: cache entry pointer
3275     // c_rarg3: jvalue object on the stack
3276     __ call_VM(noreg,
3277                CAST_FROM_FN_PTR(address,
3278                                 InterpreterRuntime::post_field_modification),
3279                RBX, robj, RCX);
3280     __ get_cache_and_index_at_bcp(cache, index, 1);
3281     __ bind(L1);
3282   }
3283 }
3284 
3285 void TemplateTable::putfield_or_static(int byte_no, bool is_static, RewriteControl rc) {
3286   transition(vtos, vtos);
3287 
3288   const Register cache = rcx;
3289   const Register index = rdx;
3290   const Register obj   = rcx;
3291   const Register off   = rbx;
3292   const Register flags = rax;
3293   const Register flags2 = rdx;
3294 
3295   resolve_cache_and_index(byte_no, cache, index, sizeof(u2));
3296   jvmti_post_field_mod(cache, index, is_static);
3297   load_field_cp_cache_entry(obj, cache, index, off, flags, is_static);
3298 
3299   // [jk] not needed currently
3300   // volatile_barrier(Assembler::Membar_mask_bits(Assembler::LoadStore |
3301   //                                              Assembler::StoreStore));
3302 
3303   Label notVolatile, Done;
3304   __ movl(rdx, flags);
3305   __ shrl(rdx, ConstantPoolCacheEntry::is_volatile_shift);
3306   __ andl(rdx, 0x1);
3307 
3308   // Check for volatile store
3309   __ testl(rdx, rdx);
3310   __ movl(flags2, flags);
3311   __ jcc(Assembler::zero, notVolatile);
3312 
3313   putfield_or_static_helper(byte_no, is_static, rc, obj, off, flags, flags2);
3314   volatile_barrier(Assembler::Membar_mask_bits(Assembler::StoreLoad |
3315                                                Assembler::StoreStore));
3316   __ jmp(Done);
3317   __ bind(notVolatile);
3318 
3319   putfield_or_static_helper(byte_no, is_static, rc, obj, off, flags, flags2);
3320 
3321   __ bind(Done);
3322 }
3323 
3324 void TemplateTable::putfield_or_static_helper(int byte_no, bool is_static, RewriteControl rc,
3325                                               Register obj, Register off, Register flags, Register flags2) {
3326 
3327   // field addresses
3328   const Address field(obj, off, Address::times_1, 0*wordSize);
3329   NOT_LP64( const Address hi(obj, off, Address::times_1, 1*wordSize);)
3330 
3331   Label notByte, notBool, notInt, notShort, notChar,
3332         notLong, notFloat, notObj, notInlineType;
3333   Label Done;
3334 
3335   const Register bc    = LP64_ONLY(c_rarg3) NOT_LP64(rcx);
3336 
3337   __ shrl(flags, ConstantPoolCacheEntry::tos_state_shift);
3338 
3339   assert(btos == 0, "change code, btos != 0");
3340   __ andl(flags, ConstantPoolCacheEntry::tos_state_mask);
3341   __ jcc(Assembler::notZero, notByte);
3342 
3343   // btos
3344   {
3345     __ pop(btos);
3346     if (!is_static) pop_and_check_object(obj);
3347     __ access_store_at(T_BYTE, IN_HEAP, field, rax, noreg, noreg);
3348     if (!is_static && rc == may_rewrite) {
3349       patch_bytecode(Bytecodes::_fast_bputfield, bc, rbx, true, byte_no);
3350     }
3351     __ jmp(Done);
3352   }
3353 
3354   __ bind(notByte);
3355   __ cmpl(flags, ztos);
3356   __ jcc(Assembler::notEqual, notBool);
3357 
3358   // ztos
3359   {
3360     __ pop(ztos);
3361     if (!is_static) pop_and_check_object(obj);
3362     __ access_store_at(T_BOOLEAN, IN_HEAP, field, rax, noreg, noreg);
3363     if (!is_static && rc == may_rewrite) {
3364       patch_bytecode(Bytecodes::_fast_zputfield, bc, rbx, true, byte_no);
3365     }
3366     __ jmp(Done);
3367   }
3368 
3369   __ bind(notBool);
3370   __ cmpl(flags, atos);
3371   __ jcc(Assembler::notEqual, notObj);
3372 
3373   // atos
3374   {
3375     if (!EnableValhalla) {
3376       __ pop(atos);
3377       if (!is_static) pop_and_check_object(obj);
3378       // Store into the field
3379       do_oop_store(_masm, field, rax);
3380       if (!is_static && rc == may_rewrite) {
3381         patch_bytecode(Bytecodes::_fast_aputfield, bc, rbx, true, byte_no);
3382       }
3383       __ jmp(Done);
3384     } else {
3385       __ pop(atos);
3386       if (is_static) {
3387         Label is_inline_type;
3388         if (EnableValhalla) {
3389           __ test_field_is_not_null_free_inline_type(flags2, rscratch1, is_inline_type);
3390           __ null_check(rax);
3391           __ bind(is_inline_type);
3392         }
3393         do_oop_store(_masm, field, rax);
3394         __ jmp(Done);
3395       } else {
3396         Label is_inline_type, is_inlined, rewrite_not_inline, rewrite_inline;
3397         if (EnableValhalla) {
3398           __ test_field_is_null_free_inline_type(flags2, rscratch1, is_inline_type);
3399         }
3400         // Not an inline type
3401         pop_and_check_object(obj);
3402         // Store into the field
3403         do_oop_store(_masm, field, rax);
3404         __ bind(rewrite_not_inline);
3405         if (rc == may_rewrite) {
3406           patch_bytecode(Bytecodes::_fast_aputfield, bc, rbx, true, byte_no);
3407         }
3408         __ jmp(Done);
3409         if (EnableValhalla) {
3410           // Implementation of the inline type semantic
3411           __ bind(is_inline_type);
3412           __ null_check(rax);
3413           __ test_field_is_inlined(flags2, rscratch1, is_inlined);
3414           // field is not inlined
3415           pop_and_check_object(obj);
3416           // Store into the field
3417           do_oop_store(_masm, field, rax);
3418           __ jmp(rewrite_inline);
3419           __ bind(is_inlined);
3420           // field is inlined
3421           pop_and_check_object(obj);
3422           assert_different_registers(rax, rdx, obj, off);
3423           __ load_klass(rdx, rax, rscratch1);
3424           __ data_for_oop(rax, rax, rdx);
3425           __ addptr(obj, off);
3426           __ access_value_copy(IN_HEAP, rax, obj, rdx);
3427           __ bind(rewrite_inline);
3428           if (rc == may_rewrite) {
3429             patch_bytecode(Bytecodes::_fast_qputfield, bc, rbx, true, byte_no);
3430           }
3431           __ jmp(Done);
3432         }
3433       }
3434     }

3435   }
3436 
3437   __ bind(notObj);
3438   __ cmpl(flags, itos);
3439   __ jcc(Assembler::notEqual, notInt);
3440 
3441   // itos
3442   {
3443     __ pop(itos);
3444     if (!is_static) pop_and_check_object(obj);
3445     __ access_store_at(T_INT, IN_HEAP, field, rax, noreg, noreg);
3446     if (!is_static && rc == may_rewrite) {
3447       patch_bytecode(Bytecodes::_fast_iputfield, bc, rbx, true, byte_no);
3448     }
3449     __ jmp(Done);
3450   }
3451 
3452   __ bind(notInt);
3453   __ cmpl(flags, ctos);
3454   __ jcc(Assembler::notEqual, notChar);
3455 
3456   // ctos
3457   {
3458     __ pop(ctos);
3459     if (!is_static) pop_and_check_object(obj);
3460     __ access_store_at(T_CHAR, IN_HEAP, field, rax, noreg, noreg);
3461     if (!is_static && rc == may_rewrite) {
3462       patch_bytecode(Bytecodes::_fast_cputfield, bc, rbx, true, byte_no);
3463     }
3464     __ jmp(Done);
3465   }
3466 
3467   __ bind(notChar);
3468   __ cmpl(flags, stos);
3469   __ jcc(Assembler::notEqual, notShort);
3470 
3471   // stos
3472   {
3473     __ pop(stos);
3474     if (!is_static) pop_and_check_object(obj);
3475     __ access_store_at(T_SHORT, IN_HEAP, field, rax, noreg, noreg);
3476     if (!is_static && rc == may_rewrite) {
3477       patch_bytecode(Bytecodes::_fast_sputfield, bc, rbx, true, byte_no);
3478     }
3479     __ jmp(Done);
3480   }
3481 
3482   __ bind(notShort);
3483   __ cmpl(flags, ltos);
3484   __ jcc(Assembler::notEqual, notLong);
3485 
3486   // ltos
3487   {
3488     __ pop(ltos);
3489     if (!is_static) pop_and_check_object(obj);
3490     // MO_RELAXED: generate atomic store for the case of volatile field (important for x86_32)
3491     __ access_store_at(T_LONG, IN_HEAP | MO_RELAXED, field, noreg /* ltos*/, noreg, noreg);
3492 #ifdef _LP64
3493     if (!is_static && rc == may_rewrite) {
3494       patch_bytecode(Bytecodes::_fast_lputfield, bc, rbx, true, byte_no);
3495     }
3496 #endif // _LP64
3497     __ jmp(Done);
3498   }
3499 
3500   __ bind(notLong);
3501   __ cmpl(flags, ftos);
3502   __ jcc(Assembler::notEqual, notFloat);
3503 
3504   // ftos
3505   {
3506     __ pop(ftos);
3507     if (!is_static) pop_and_check_object(obj);
3508     __ access_store_at(T_FLOAT, IN_HEAP, field, noreg /* ftos */, noreg, noreg);
3509     if (!is_static && rc == may_rewrite) {
3510       patch_bytecode(Bytecodes::_fast_fputfield, bc, rbx, true, byte_no);
3511     }
3512     __ jmp(Done);
3513   }
3514 
3515   __ bind(notFloat);
3516 #ifdef ASSERT
3517   Label notDouble;
3518   __ cmpl(flags, dtos);
3519   __ jcc(Assembler::notEqual, notDouble);
3520 #endif
3521 
3522   // dtos
3523   {
3524     __ pop(dtos);
3525     if (!is_static) pop_and_check_object(obj);
3526     // MO_RELAXED: for the case of volatile field, in fact it adds no extra work for the underlying implementation
3527     __ access_store_at(T_DOUBLE, IN_HEAP | MO_RELAXED, field, noreg /* dtos */, noreg, noreg);
3528     if (!is_static && rc == may_rewrite) {
3529       patch_bytecode(Bytecodes::_fast_dputfield, bc, rbx, true, byte_no);
3530     }
3531   }
3532 
3533 #ifdef ASSERT
3534   __ jmp(Done);
3535 
3536   __ bind(notDouble);
3537   __ stop("Bad state");
3538 #endif
3539 
3540   __ bind(Done);
3541 }
3542 
3543 void TemplateTable::putfield(int byte_no) {
3544   putfield_or_static(byte_no, false);
3545 }
3546 
3547 void TemplateTable::nofast_putfield(int byte_no) {
3548   putfield_or_static(byte_no, false, may_not_rewrite);
3549 }
3550 
3551 void TemplateTable::putstatic(int byte_no) {
3552   putfield_or_static(byte_no, true);
3553 }
3554 
3555 void TemplateTable::jvmti_post_fast_field_mod() {
3556 
3557   const Register scratch = LP64_ONLY(c_rarg3) NOT_LP64(rcx);
3558 
3559   if (JvmtiExport::can_post_field_modification()) {
3560     // Check to see if a field modification watch has been set before
3561     // we take the time to call into the VM.
3562     Label L2;
3563     __ mov32(scratch, ExternalAddress((address)JvmtiExport::get_field_modification_count_addr()));
3564     __ testl(scratch, scratch);
3565     __ jcc(Assembler::zero, L2);
3566     __ pop_ptr(rbx);                  // copy the object pointer from tos
3567     __ verify_oop(rbx);
3568     __ push_ptr(rbx);                 // put the object pointer back on tos
3569     // Save tos values before call_VM() clobbers them. Since we have
3570     // to do it for every data type, we use the saved values as the
3571     // jvalue object.
3572     switch (bytecode()) {          // load values into the jvalue object
3573     case Bytecodes::_fast_qputfield: //fall through
3574     case Bytecodes::_fast_aputfield: __ push_ptr(rax); break;
3575     case Bytecodes::_fast_bputfield: // fall through
3576     case Bytecodes::_fast_zputfield: // fall through
3577     case Bytecodes::_fast_sputfield: // fall through
3578     case Bytecodes::_fast_cputfield: // fall through
3579     case Bytecodes::_fast_iputfield: __ push_i(rax); break;
3580     case Bytecodes::_fast_dputfield: __ push(dtos); break;
3581     case Bytecodes::_fast_fputfield: __ push(ftos); break;
3582     case Bytecodes::_fast_lputfield: __ push_l(rax); break;
3583 
3584     default:
3585       ShouldNotReachHere();
3586     }
3587     __ mov(scratch, rsp);             // points to jvalue on the stack
3588     // access constant pool cache entry
3589     LP64_ONLY(__ get_cache_entry_pointer_at_bcp(c_rarg2, rax, 1));
3590     NOT_LP64(__ get_cache_entry_pointer_at_bcp(rax, rdx, 1));
3591     __ verify_oop(rbx);
3592     // rbx: object pointer copied above
3593     // c_rarg2: cache entry pointer
3594     // c_rarg3: jvalue object on the stack
3595     LP64_ONLY(__ call_VM(noreg, CAST_FROM_FN_PTR(address, InterpreterRuntime::post_field_modification), rbx, c_rarg2, c_rarg3));
3596     NOT_LP64(__ call_VM(noreg, CAST_FROM_FN_PTR(address, InterpreterRuntime::post_field_modification), rbx, rax, rcx));
3597 
3598     switch (bytecode()) {             // restore tos values
3599     case Bytecodes::_fast_qputfield: // fall through
3600     case Bytecodes::_fast_aputfield: __ pop_ptr(rax); break;
3601     case Bytecodes::_fast_bputfield: // fall through
3602     case Bytecodes::_fast_zputfield: // fall through
3603     case Bytecodes::_fast_sputfield: // fall through
3604     case Bytecodes::_fast_cputfield: // fall through
3605     case Bytecodes::_fast_iputfield: __ pop_i(rax); break;
3606     case Bytecodes::_fast_dputfield: __ pop(dtos); break;
3607     case Bytecodes::_fast_fputfield: __ pop(ftos); break;
3608     case Bytecodes::_fast_lputfield: __ pop_l(rax); break;
3609     default: break;
3610     }
3611     __ bind(L2);
3612   }
3613 }
3614 
3615 void TemplateTable::fast_storefield(TosState state) {
3616   transition(state, vtos);
3617 
3618   ByteSize base = ConstantPoolCache::base_offset();
3619 
3620   jvmti_post_fast_field_mod();
3621 
3622   // access constant pool cache
3623   __ get_cache_and_index_at_bcp(rcx, rbx, 1);
3624 
3625   // test for volatile with rdx but rdx is tos register for lputfield.
3626   __ movl(rdx, Address(rcx, rbx, Address::times_ptr,
3627                        in_bytes(base +
3628                                 ConstantPoolCacheEntry::flags_offset())));
3629 
3630   // replace index with field offset from cache entry
3631   __ movptr(rbx, Address(rcx, rbx, Address::times_ptr,
3632                          in_bytes(base + ConstantPoolCacheEntry::f2_offset())));
3633 
3634   // [jk] not needed currently
3635   // volatile_barrier(Assembler::Membar_mask_bits(Assembler::LoadStore |
3636   //                                              Assembler::StoreStore));
3637 
3638   Label notVolatile, Done;
3639   if (bytecode() == Bytecodes::_fast_qputfield) {
3640     __ movl(rscratch2, rdx);  // saving flags for is_inlined test
3641   }
3642 
3643   __ shrl(rdx, ConstantPoolCacheEntry::is_volatile_shift);
3644   __ andl(rdx, 0x1);
3645 
3646   // Get object from stack
3647   pop_and_check_object(rcx);
3648 
3649   // field address
3650   const Address field(rcx, rbx, Address::times_1);
3651 
3652   // Check for volatile store
3653   __ testl(rdx, rdx);
3654   __ jcc(Assembler::zero, notVolatile);
3655 
3656   if (bytecode() == Bytecodes::_fast_qputfield) {
3657     __ movl(rdx, rscratch2);  // restoring flags for is_inlined test
3658   }
3659   fast_storefield_helper(field, rax, rdx);
3660   volatile_barrier(Assembler::Membar_mask_bits(Assembler::StoreLoad |
3661                                                Assembler::StoreStore));
3662   __ jmp(Done);
3663   __ bind(notVolatile);
3664 
3665   if (bytecode() == Bytecodes::_fast_qputfield) {
3666     __ movl(rdx, rscratch2);  // restoring flags for is_inlined test
3667   }
3668   fast_storefield_helper(field, rax, rdx);
3669 
3670   __ bind(Done);
3671 }
3672 
3673 void TemplateTable::fast_storefield_helper(Address field, Register rax, Register flags) {
3674 
3675   // access field
3676   switch (bytecode()) {
3677   case Bytecodes::_fast_qputfield:
3678     {
3679       Label is_inlined, done;
3680       __ null_check(rax);
3681       __ test_field_is_inlined(flags, rscratch1, is_inlined);
3682       // field is not inlined
3683       do_oop_store(_masm, field, rax);
3684       __ jmp(done);
3685       __ bind(is_inlined);
3686       // field is inlined
3687       __ load_klass(rdx, rax, rscratch1);
3688       __ data_for_oop(rax, rax, rdx);
3689       __ lea(rcx, field);
3690       __ access_value_copy(IN_HEAP, rax, rcx, rdx);
3691       __ bind(done);
3692     }
3693     break;
3694   case Bytecodes::_fast_aputfield:
3695     {
3696       do_oop_store(_masm, field, rax);
3697     }
3698     break;
3699   case Bytecodes::_fast_lputfield:
3700 #ifdef _LP64
3701     __ access_store_at(T_LONG, IN_HEAP, field, noreg /* ltos */, noreg, noreg);
3702 #else
3703   __ stop("should not be rewritten");
3704 #endif
3705     break;
3706   case Bytecodes::_fast_iputfield:
3707     __ access_store_at(T_INT, IN_HEAP, field, rax, noreg, noreg);
3708     break;
3709   case Bytecodes::_fast_zputfield:
3710     __ access_store_at(T_BOOLEAN, IN_HEAP, field, rax, noreg, noreg);
3711     break;
3712   case Bytecodes::_fast_bputfield:
3713     __ access_store_at(T_BYTE, IN_HEAP, field, rax, noreg, noreg);
3714     break;
3715   case Bytecodes::_fast_sputfield:
3716     __ access_store_at(T_SHORT, IN_HEAP, field, rax, noreg, noreg);
3717     break;
3718   case Bytecodes::_fast_cputfield:
3719     __ access_store_at(T_CHAR, IN_HEAP, field, rax, noreg, noreg);
3720     break;
3721   case Bytecodes::_fast_fputfield:
3722     __ access_store_at(T_FLOAT, IN_HEAP, field, noreg /* ftos*/, noreg, noreg);
3723     break;
3724   case Bytecodes::_fast_dputfield:
3725     __ access_store_at(T_DOUBLE, IN_HEAP, field, noreg /* dtos*/, noreg, noreg);
3726     break;
3727   default:
3728     ShouldNotReachHere();
3729   }
3730 }
3731 
3732 void TemplateTable::fast_accessfield(TosState state) {
3733   transition(atos, state);
3734 
3735   // Do the JVMTI work here to avoid disturbing the register state below
3736   if (JvmtiExport::can_post_field_access()) {
3737     // Check to see if a field access watch has been set before we
3738     // take the time to call into the VM.
3739     Label L1;
3740     __ mov32(rcx, ExternalAddress((address) JvmtiExport::get_field_access_count_addr()));
3741     __ testl(rcx, rcx);
3742     __ jcc(Assembler::zero, L1);
3743     // access constant pool cache entry
3744     LP64_ONLY(__ get_cache_entry_pointer_at_bcp(c_rarg2, rcx, 1));
3745     NOT_LP64(__ get_cache_entry_pointer_at_bcp(rcx, rdx, 1));
3746     __ verify_oop(rax);
3747     __ push_ptr(rax);  // save object pointer before call_VM() clobbers it
3748     LP64_ONLY(__ mov(c_rarg1, rax));
3749     // c_rarg1: object pointer copied above
3750     // c_rarg2: cache entry pointer
3751     LP64_ONLY(__ call_VM(noreg, CAST_FROM_FN_PTR(address, InterpreterRuntime::post_field_access), c_rarg1, c_rarg2));
3752     NOT_LP64(__ call_VM(noreg, CAST_FROM_FN_PTR(address, InterpreterRuntime::post_field_access), rax, rcx));
3753     __ pop_ptr(rax); // restore object pointer
3754     __ bind(L1);
3755   }
3756 
3757   // access constant pool cache
3758   __ get_cache_and_index_at_bcp(rcx, rbx, 1);
3759   // replace index with field offset from cache entry
3760   // [jk] not needed currently
3761   // __ movl(rdx, Address(rcx, rbx, Address::times_8,
3762   //                      in_bytes(ConstantPoolCache::base_offset() +
3763   //                               ConstantPoolCacheEntry::flags_offset())));
3764   // __ shrl(rdx, ConstantPoolCacheEntry::is_volatile_shift);
3765   // __ andl(rdx, 0x1);
3766   //
3767   __ movptr(rdx, Address(rcx, rbx, Address::times_ptr,
3768                          in_bytes(ConstantPoolCache::base_offset() +
3769                                   ConstantPoolCacheEntry::f2_offset())));
3770 
3771   // rax: object
3772   __ verify_oop(rax);
3773   __ null_check(rax);
3774   Address field(rax, rdx, Address::times_1);
3775 
3776   // access field
3777   switch (bytecode()) {
3778   case Bytecodes::_fast_qgetfield:
3779     {
3780       Label is_inlined, nonnull, Done;
3781       __ movptr(rscratch1, Address(rcx, rbx, Address::times_ptr,
3782                                    in_bytes(ConstantPoolCache::base_offset() +
3783                                             ConstantPoolCacheEntry::flags_offset())));
3784       __ test_field_is_inlined(rscratch1, rscratch2, is_inlined);
3785         // field is not inlined
3786         __ load_heap_oop(rax, field);
3787         __ testptr(rax, rax);
3788         __ jcc(Assembler::notZero, nonnull);
3789           __ movl(rdx, Address(rcx, rbx, Address::times_ptr,
3790                              in_bytes(ConstantPoolCache::base_offset() +
3791                                       ConstantPoolCacheEntry::flags_offset())));
3792           __ andl(rdx, ConstantPoolCacheEntry::field_index_mask);
3793           __ movptr(rcx, Address(rcx, rbx, Address::times_ptr,
3794                                        in_bytes(ConstantPoolCache::base_offset() +
3795                                                 ConstantPoolCacheEntry::f1_offset())));
3796           __ get_inline_type_field_klass(rcx, rdx, rbx);
3797           __ get_default_value_oop(rbx, rcx, rax);
3798         __ bind(nonnull);
3799         __ verify_oop(rax);
3800         __ jmp(Done);
3801       __ bind(is_inlined);
3802       // field is inlined
3803         __ push(rdx); // save offset
3804         __ movl(rdx, Address(rcx, rbx, Address::times_ptr,
3805                            in_bytes(ConstantPoolCache::base_offset() +
3806                                     ConstantPoolCacheEntry::flags_offset())));
3807         __ andl(rdx, ConstantPoolCacheEntry::field_index_mask);
3808         __ movptr(rcx, Address(rcx, rbx, Address::times_ptr,
3809                                      in_bytes(ConstantPoolCache::base_offset() +
3810                                               ConstantPoolCacheEntry::f1_offset())));
3811         __ pop(rbx); // restore offset
3812         __ read_inlined_field(rcx, rdx, rbx, rax);
3813       __ bind(Done);
3814       __ verify_oop(rax);
3815     }
3816     break;
3817   case Bytecodes::_fast_agetfield:
3818     do_oop_load(_masm, field, rax);
3819     __ verify_oop(rax);
3820     break;
3821   case Bytecodes::_fast_lgetfield:
3822 #ifdef _LP64
3823     __ access_load_at(T_LONG, IN_HEAP, noreg /* ltos */, field, noreg, noreg);
3824 #else
3825   __ stop("should not be rewritten");
3826 #endif
3827     break;
3828   case Bytecodes::_fast_igetfield:
3829     __ access_load_at(T_INT, IN_HEAP, rax, field, noreg, noreg);
3830     break;
3831   case Bytecodes::_fast_bgetfield:
3832     __ access_load_at(T_BYTE, IN_HEAP, rax, field, noreg, noreg);
3833     break;
3834   case Bytecodes::_fast_sgetfield:
3835     __ access_load_at(T_SHORT, IN_HEAP, rax, field, noreg, noreg);
3836     break;
3837   case Bytecodes::_fast_cgetfield:
3838     __ access_load_at(T_CHAR, IN_HEAP, rax, field, noreg, noreg);
3839     break;
3840   case Bytecodes::_fast_fgetfield:
3841     __ access_load_at(T_FLOAT, IN_HEAP, noreg /* ftos */, field, noreg, noreg);
3842     break;
3843   case Bytecodes::_fast_dgetfield:
3844     __ access_load_at(T_DOUBLE, IN_HEAP, noreg /* dtos */, field, noreg, noreg);
3845     break;
3846   default:
3847     ShouldNotReachHere();
3848   }
3849   // [jk] not needed currently
3850   //   Label notVolatile;
3851   //   __ testl(rdx, rdx);
3852   //   __ jcc(Assembler::zero, notVolatile);
3853   //   __ membar(Assembler::LoadLoad);
3854   //   __ bind(notVolatile);
3855 }
3856 
3857 void TemplateTable::fast_xaccess(TosState state) {
3858   transition(vtos, state);
3859 
3860   // get receiver
3861   __ movptr(rax, aaddress(0));
3862   // access constant pool cache
3863   __ get_cache_and_index_at_bcp(rcx, rdx, 2);
3864   __ movptr(rbx,
3865             Address(rcx, rdx, Address::times_ptr,
3866                     in_bytes(ConstantPoolCache::base_offset() +
3867                              ConstantPoolCacheEntry::f2_offset())));
3868   // make sure exception is reported in correct bcp range (getfield is
3869   // next instruction)
3870   __ increment(rbcp);
3871   __ null_check(rax);
3872   const Address field = Address(rax, rbx, Address::times_1, 0*wordSize);
3873   switch (state) {
3874   case itos:
3875     __ access_load_at(T_INT, IN_HEAP, rax, field, noreg, noreg);
3876     break;
3877   case atos:
3878     do_oop_load(_masm, field, rax);
3879     __ verify_oop(rax);
3880     break;
3881   case ftos:
3882     __ access_load_at(T_FLOAT, IN_HEAP, noreg /* ftos */, field, noreg, noreg);
3883     break;
3884   default:
3885     ShouldNotReachHere();
3886   }
3887 
3888   // [jk] not needed currently
3889   // Label notVolatile;
3890   // __ movl(rdx, Address(rcx, rdx, Address::times_8,
3891   //                      in_bytes(ConstantPoolCache::base_offset() +
3892   //                               ConstantPoolCacheEntry::flags_offset())));
3893   // __ shrl(rdx, ConstantPoolCacheEntry::is_volatile_shift);
3894   // __ testl(rdx, 0x1);
3895   // __ jcc(Assembler::zero, notVolatile);
3896   // __ membar(Assembler::LoadLoad);
3897   // __ bind(notVolatile);
3898 
3899   __ decrement(rbcp);
3900 }
3901 
3902 //-----------------------------------------------------------------------------
3903 // Calls
3904 
3905 void TemplateTable::prepare_invoke(int byte_no,
3906                                    Register method,  // linked method (or i-klass)
3907                                    Register index,   // itable index, MethodType, etc.
3908                                    Register recv,    // if caller wants to see it
3909                                    Register flags    // if caller wants to test it
3910                                    ) {
3911   // determine flags
3912   const Bytecodes::Code code = bytecode();
3913   const bool is_invokeinterface  = code == Bytecodes::_invokeinterface;
3914   const bool is_invokedynamic    = code == Bytecodes::_invokedynamic;
3915   const bool is_invokehandle     = code == Bytecodes::_invokehandle;
3916   const bool is_invokevirtual    = code == Bytecodes::_invokevirtual;
3917   const bool is_invokespecial    = code == Bytecodes::_invokespecial;
3918   const bool load_receiver       = (recv  != noreg);
3919   const bool save_flags          = (flags != noreg);
3920   assert(load_receiver == (code != Bytecodes::_invokestatic && code != Bytecodes::_invokedynamic), "");
3921   assert(save_flags    == (is_invokeinterface || is_invokevirtual), "need flags for vfinal");
3922   assert(flags == noreg || flags == rdx, "");
3923   assert(recv  == noreg || recv  == rcx, "");
3924 
3925   // setup registers & access constant pool cache
3926   if (recv  == noreg)  recv  = rcx;
3927   if (flags == noreg)  flags = rdx;
3928   assert_different_registers(method, index, recv, flags);
3929 
3930   // save 'interpreter return address'
3931   __ save_bcp();
3932 
3933   load_invoke_cp_cache_entry(byte_no, method, index, flags, is_invokevirtual, false, is_invokedynamic);
3934 
3935   // maybe push appendix to arguments (just before return address)
3936   if (is_invokedynamic || is_invokehandle) {
3937     Label L_no_push;
3938     __ testl(flags, (1 << ConstantPoolCacheEntry::has_appendix_shift));
3939     __ jcc(Assembler::zero, L_no_push);
3940     // Push the appendix as a trailing parameter.
3941     // This must be done before we get the receiver,
3942     // since the parameter_size includes it.
3943     __ push(rbx);
3944     __ mov(rbx, index);
3945     __ load_resolved_reference_at_index(index, rbx);
3946     __ pop(rbx);
3947     __ push(index);  // push appendix (MethodType, CallSite, etc.)
3948     __ bind(L_no_push);
3949   }
3950 
3951   // load receiver if needed (after appendix is pushed so parameter size is correct)
3952   // Note: no return address pushed yet
3953   if (load_receiver) {
3954     __ movl(recv, flags);
3955     __ andl(recv, ConstantPoolCacheEntry::parameter_size_mask);
3956     const int no_return_pc_pushed_yet = -1;  // argument slot correction before we push return address
3957     const int receiver_is_at_end      = -1;  // back off one slot to get receiver
3958     Address recv_addr = __ argument_address(recv, no_return_pc_pushed_yet + receiver_is_at_end);
3959     __ movptr(recv, recv_addr);
3960     __ verify_oop(recv);
3961   }
3962 
3963   if (save_flags) {
3964     __ movl(rbcp, flags);
3965   }
3966 
3967   // compute return type
3968   __ shrl(flags, ConstantPoolCacheEntry::tos_state_shift);
3969   // Make sure we don't need to mask flags after the above shift
3970   ConstantPoolCacheEntry::verify_tos_state_shift();
3971   // load return address
3972   {
3973     const address table_addr = (address) Interpreter::invoke_return_entry_table_for(code);
3974     ExternalAddress table(table_addr);
3975     LP64_ONLY(__ lea(rscratch1, table));
3976     LP64_ONLY(__ movptr(flags, Address(rscratch1, flags, Address::times_ptr)));
3977     NOT_LP64(__ movptr(flags, ArrayAddress(table, Address(noreg, flags, Address::times_ptr))));
3978   }
3979 
3980   // push return address
3981   __ push(flags);
3982 
3983   // Restore flags value from the constant pool cache, and restore rsi
3984   // for later null checks.  r13 is the bytecode pointer
3985   if (save_flags) {
3986     __ movl(flags, rbcp);
3987     __ restore_bcp();
3988   }
3989 }
3990 
3991 void TemplateTable::invokevirtual_helper(Register index,
3992                                          Register recv,
3993                                          Register flags) {
3994   // Uses temporary registers rax, rdx
3995   assert_different_registers(index, recv, rax, rdx);
3996   assert(index == rbx, "");
3997   assert(recv  == rcx, "");
3998 
3999   // Test for an invoke of a final method
4000   Label notFinal;
4001   __ movl(rax, flags);
4002   __ andl(rax, (1 << ConstantPoolCacheEntry::is_vfinal_shift));
4003   __ jcc(Assembler::zero, notFinal);
4004 
4005   const Register method = index;  // method must be rbx
4006   assert(method == rbx,
4007          "Method* must be rbx for interpreter calling convention");
4008 
4009   // do the call - the index is actually the method to call
4010   // that is, f2 is a vtable index if !is_vfinal, else f2 is a Method*
4011 
4012   // It's final, need a null check here!
4013   __ null_check(recv);
4014 
4015   // profile this call
4016   __ profile_final_call(rax);
4017   __ profile_arguments_type(rax, method, rbcp, true);
4018 
4019   __ jump_from_interpreted(method, rax);
4020 
4021   __ bind(notFinal);
4022 
4023   // get receiver klass
4024   __ null_check(recv, oopDesc::klass_offset_in_bytes());
4025   Register tmp_load_klass = LP64_ONLY(rscratch1) NOT_LP64(noreg);
4026   __ load_klass(rax, recv, tmp_load_klass);
4027 
4028   // profile this call
4029   __ profile_virtual_call(rax, rlocals, rdx);
4030   // get target Method* & entry point
4031   __ lookup_virtual_method(rax, index, method);
4032 
4033   __ profile_arguments_type(rdx, method, rbcp, true);
4034   __ jump_from_interpreted(method, rdx);
4035 }
4036 
4037 void TemplateTable::invokevirtual(int byte_no) {
4038   transition(vtos, vtos);
4039   assert(byte_no == f2_byte, "use this argument");
4040   prepare_invoke(byte_no,
4041                  rbx,    // method or vtable index
4042                  noreg,  // unused itable index
4043                  rcx, rdx); // recv, flags
4044 
4045   // rbx: index
4046   // rcx: receiver
4047   // rdx: flags
4048 
4049   invokevirtual_helper(rbx, rcx, rdx);
4050 }
4051 
4052 void TemplateTable::invokespecial(int byte_no) {
4053   transition(vtos, vtos);
4054   assert(byte_no == f1_byte, "use this argument");
4055   prepare_invoke(byte_no, rbx, noreg,  // get f1 Method*
4056                  rcx);  // get receiver also for null check
4057   __ verify_oop(rcx);
4058   __ null_check(rcx);
4059   // do the call
4060   __ profile_call(rax);
4061   __ profile_arguments_type(rax, rbx, rbcp, false);
4062   __ jump_from_interpreted(rbx, rax);
4063 }
4064 
4065 void TemplateTable::invokestatic(int byte_no) {
4066   transition(vtos, vtos);
4067   assert(byte_no == f1_byte, "use this argument");
4068   prepare_invoke(byte_no, rbx);  // get f1 Method*
4069   // do the call
4070   __ profile_call(rax);
4071   __ profile_arguments_type(rax, rbx, rbcp, false);
4072   __ jump_from_interpreted(rbx, rax);
4073 }
4074 
4075 
4076 void TemplateTable::fast_invokevfinal(int byte_no) {
4077   transition(vtos, vtos);
4078   assert(byte_no == f2_byte, "use this argument");
4079   __ stop("fast_invokevfinal not used on x86");
4080 }
4081 
4082 
4083 void TemplateTable::invokeinterface(int byte_no) {
4084   transition(vtos, vtos);
4085   assert(byte_no == f1_byte, "use this argument");
4086   prepare_invoke(byte_no, rax, rbx,  // get f1 Klass*, f2 Method*
4087                  rcx, rdx); // recv, flags
4088 
4089   // rax: reference klass (from f1) if interface method
4090   // rbx: method (from f2)
4091   // rcx: receiver
4092   // rdx: flags
4093 
4094   // First check for Object case, then private interface method,
4095   // then regular interface method.
4096 
4097   // Special case of invokeinterface called for virtual method of
4098   // java.lang.Object.  See cpCache.cpp for details.
4099   Label notObjectMethod;
4100   __ movl(rlocals, rdx);
4101   __ andl(rlocals, (1 << ConstantPoolCacheEntry::is_forced_virtual_shift));
4102   __ jcc(Assembler::zero, notObjectMethod);
4103   invokevirtual_helper(rbx, rcx, rdx);
4104   // no return from above
4105   __ bind(notObjectMethod);
4106 
4107   Label no_such_interface; // for receiver subtype check
4108   Register recvKlass; // used for exception processing
4109 
4110   // Check for private method invocation - indicated by vfinal
4111   Label notVFinal;
4112   __ movl(rlocals, rdx);
4113   __ andl(rlocals, (1 << ConstantPoolCacheEntry::is_vfinal_shift));
4114   __ jcc(Assembler::zero, notVFinal);
4115 
4116   // Get receiver klass into rlocals - also a null check
4117   __ null_check(rcx, oopDesc::klass_offset_in_bytes());
4118   Register tmp_load_klass = LP64_ONLY(rscratch1) NOT_LP64(noreg);
4119   __ load_klass(rlocals, rcx, tmp_load_klass);
4120 
4121   Label subtype;
4122   __ check_klass_subtype(rlocals, rax, rbcp, subtype);
4123   // If we get here the typecheck failed
4124   recvKlass = rdx;
4125   __ mov(recvKlass, rlocals); // shuffle receiver class for exception use
4126   __ jmp(no_such_interface);
4127 
4128   __ bind(subtype);
4129 
4130   // do the call - rbx is actually the method to call
4131 
4132   __ profile_final_call(rdx);
4133   __ profile_arguments_type(rdx, rbx, rbcp, true);
4134 
4135   __ jump_from_interpreted(rbx, rdx);
4136   // no return from above
4137   __ bind(notVFinal);
4138 
4139   // Get receiver klass into rdx - also a null check
4140   __ restore_locals();  // restore r14
4141   __ null_check(rcx, oopDesc::klass_offset_in_bytes());
4142   __ load_klass(rdx, rcx, tmp_load_klass);
4143 
4144   Label no_such_method;
4145 
4146   // Preserve method for throw_AbstractMethodErrorVerbose.
4147   __ mov(rcx, rbx);
4148   // Receiver subtype check against REFC.
4149   // Superklass in rax. Subklass in rdx. Blows rcx, rdi.
4150   __ lookup_interface_method(// inputs: rec. class, interface, itable index
4151                              rdx, rax, noreg,
4152                              // outputs: scan temp. reg, scan temp. reg
4153                              rbcp, rlocals,
4154                              no_such_interface,
4155                              /*return_method=*/false);
4156 
4157   // profile this call
4158   __ restore_bcp(); // rbcp was destroyed by receiver type check
4159   __ profile_virtual_call(rdx, rbcp, rlocals);
4160 
4161   // Get declaring interface class from method, and itable index
4162   __ load_method_holder(rax, rbx);
4163   __ movl(rbx, Address(rbx, Method::itable_index_offset()));
4164   __ subl(rbx, Method::itable_index_max);
4165   __ negl(rbx);
4166 
4167   // Preserve recvKlass for throw_AbstractMethodErrorVerbose.
4168   __ mov(rlocals, rdx);
4169   __ lookup_interface_method(// inputs: rec. class, interface, itable index
4170                              rlocals, rax, rbx,
4171                              // outputs: method, scan temp. reg
4172                              rbx, rbcp,
4173                              no_such_interface);
4174 
4175   // rbx: Method* to call
4176   // rcx: receiver
4177   // Check for abstract method error
4178   // Note: This should be done more efficiently via a throw_abstract_method_error
4179   //       interpreter entry point and a conditional jump to it in case of a null
4180   //       method.
4181   __ testptr(rbx, rbx);
4182   __ jcc(Assembler::zero, no_such_method);
4183 
4184   __ profile_arguments_type(rdx, rbx, rbcp, true);
4185 
4186   // do the call
4187   // rcx: receiver
4188   // rbx,: Method*
4189   __ jump_from_interpreted(rbx, rdx);
4190   __ should_not_reach_here();
4191 
4192   // exception handling code follows...
4193   // note: must restore interpreter registers to canonical
4194   //       state for exception handling to work correctly!
4195 
4196   __ bind(no_such_method);
4197   // throw exception
4198   __ pop(rbx);           // pop return address (pushed by prepare_invoke)
4199   __ restore_bcp();      // rbcp must be correct for exception handler   (was destroyed)
4200   __ restore_locals();   // make sure locals pointer is correct as well (was destroyed)
4201   // Pass arguments for generating a verbose error message.
4202 #ifdef _LP64
4203   recvKlass = c_rarg1;
4204   Register method    = c_rarg2;
4205   if (recvKlass != rdx) { __ movq(recvKlass, rdx); }
4206   if (method != rcx)    { __ movq(method, rcx);    }
4207 #else
4208   recvKlass = rdx;
4209   Register method    = rcx;
4210 #endif
4211   __ call_VM(noreg, CAST_FROM_FN_PTR(address, InterpreterRuntime::throw_AbstractMethodErrorVerbose),
4212              recvKlass, method);
4213   // The call_VM checks for exception, so we should never return here.
4214   __ should_not_reach_here();
4215 
4216   __ bind(no_such_interface);
4217   // throw exception
4218   __ pop(rbx);           // pop return address (pushed by prepare_invoke)
4219   __ restore_bcp();      // rbcp must be correct for exception handler   (was destroyed)
4220   __ restore_locals();   // make sure locals pointer is correct as well (was destroyed)
4221   // Pass arguments for generating a verbose error message.
4222   LP64_ONLY( if (recvKlass != rdx) { __ movq(recvKlass, rdx); } )
4223   __ call_VM(noreg, CAST_FROM_FN_PTR(address, InterpreterRuntime::throw_IncompatibleClassChangeErrorVerbose),
4224              recvKlass, rax);
4225   // the call_VM checks for exception, so we should never return here.
4226   __ should_not_reach_here();
4227 }
4228 
4229 void TemplateTable::invokehandle(int byte_no) {
4230   transition(vtos, vtos);
4231   assert(byte_no == f1_byte, "use this argument");
4232   const Register rbx_method = rbx;
4233   const Register rax_mtype  = rax;
4234   const Register rcx_recv   = rcx;
4235   const Register rdx_flags  = rdx;
4236 
4237   prepare_invoke(byte_no, rbx_method, rax_mtype, rcx_recv);
4238   __ verify_method_ptr(rbx_method);
4239   __ verify_oop(rcx_recv);
4240   __ null_check(rcx_recv);
4241 
4242   // rax: MethodType object (from cpool->resolved_references[f1], if necessary)
4243   // rbx: MH.invokeExact_MT method (from f2)
4244 
4245   // Note:  rax_mtype is already pushed (if necessary) by prepare_invoke
4246 
4247   // FIXME: profile the LambdaForm also
4248   __ profile_final_call(rax);
4249   __ profile_arguments_type(rdx, rbx_method, rbcp, true);
4250 
4251   __ jump_from_interpreted(rbx_method, rdx);
4252 }
4253 
4254 void TemplateTable::invokedynamic(int byte_no) {
4255   transition(vtos, vtos);
4256   assert(byte_no == f1_byte, "use this argument");
4257 
4258   const Register rbx_method   = rbx;
4259   const Register rax_callsite = rax;
4260 
4261   prepare_invoke(byte_no, rbx_method, rax_callsite);
4262 
4263   // rax: CallSite object (from cpool->resolved_references[f1])
4264   // rbx: MH.linkToCallSite method (from f2)
4265 
4266   // Note:  rax_callsite is already pushed by prepare_invoke
4267 
4268   // %%% should make a type profile for any invokedynamic that takes a ref argument
4269   // profile this call
4270   __ profile_call(rbcp);
4271   __ profile_arguments_type(rdx, rbx_method, rbcp, false);
4272 
4273   __ verify_oop(rax_callsite);
4274 
4275   __ jump_from_interpreted(rbx_method, rdx);
4276 }
4277 
4278 //-----------------------------------------------------------------------------
4279 // Allocation
4280 
4281 void TemplateTable::_new() {
4282   transition(vtos, atos);
4283   __ get_unsigned_2_byte_index_at_bcp(rdx, 1);
4284   Label slow_case;

4285   Label done;
4286   Label is_not_value;

4287 
4288   __ get_cpool_and_tags(rcx, rax);
4289 
4290   // Make sure the class we're about to instantiate has been resolved.
4291   // This is done before loading InstanceKlass to be consistent with the order
4292   // how Constant Pool is updated (see ConstantPool::klass_at_put)
4293   const int tags_offset = Array<u1>::base_offset_in_bytes();
4294   __ cmpb(Address(rax, rdx, Address::times_1, tags_offset), JVM_CONSTANT_Class);
4295   __ jcc(Assembler::notEqual, slow_case);
4296 
4297   // get InstanceKlass
4298   __ load_resolved_klass_at_index(rcx, rcx, rdx);
4299 
4300   __ cmpb(Address(rcx, InstanceKlass::kind_offset()), InstanceKlass::_kind_inline_type);
4301   __ jcc(Assembler::notEqual, is_not_value);
4302 
4303   __ call_VM(noreg, CAST_FROM_FN_PTR(address, InterpreterRuntime::throw_InstantiationError));
4304 
4305   __ bind(is_not_value);
4306 
4307   // make sure klass is initialized & doesn't have finalizer

4308   __ cmpb(Address(rcx, InstanceKlass::init_state_offset()), InstanceKlass::fully_initialized);
4309   __ jcc(Assembler::notEqual, slow_case);
4310 
4311   __ allocate_instance(rcx, rax, rdx, rbx, true, slow_case);
4312   __ jmp(done);

















4313 
4314   // slow case
4315   __ bind(slow_case);
4316 
4317   Register rarg1 = LP64_ONLY(c_rarg1) NOT_LP64(rax);
4318   Register rarg2 = LP64_ONLY(c_rarg2) NOT_LP64(rdx);




4319 
4320   __ get_constant_pool(rarg1);
4321   __ get_unsigned_2_byte_index_at_bcp(rarg2, 1);
4322   call_VM(rax, CAST_FROM_FN_PTR(address, InterpreterRuntime::_new), rarg1, rarg2);
4323    __ verify_oop(rax);











4324 
4325   // continue
4326   __ bind(done);
4327 }





















4328 
4329 void TemplateTable::defaultvalue() {
4330   transition(vtos, atos);






4331 
4332   Label slow_case;
4333   Label done;
4334   Label is_value;








4335 
4336   __ get_unsigned_2_byte_index_at_bcp(rdx, 1);
4337   __ get_cpool_and_tags(rcx, rax);






4338 
4339   // Make sure the class we're about to instantiate has been resolved.
4340   // This is done before loading InstanceKlass to be consistent with the order
4341   // how Constant Pool is updated (see ConstantPool::klass_at_put)
4342   const int tags_offset = Array<u1>::base_offset_in_bytes();
4343   __ cmpb(Address(rax, rdx, Address::times_1, tags_offset), JVM_CONSTANT_Class);
4344   __ jcc(Assembler::notEqual, slow_case);
4345 
4346   // get InstanceKlass
4347   __ load_resolved_klass_at_index(rcx, rcx, rdx);
4348 
4349   __ cmpb(Address(rcx, InstanceKlass::kind_offset()), InstanceKlass::_kind_inline_type);
4350   __ jcc(Assembler::equal, is_value);
4351 
4352   // in the future, defaultvalue will just return null instead of throwing an exception
4353   __ call_VM(noreg, CAST_FROM_FN_PTR(address, InterpreterRuntime::throw_IncompatibleClassChangeError));
4354 
4355   __ bind(is_value);
4356 
4357   // make sure klass is fully initialized
4358   __ cmpb(Address(rcx, InstanceKlass::init_state_offset()), InstanceKlass::fully_initialized);
4359   __ jcc(Assembler::notEqual, slow_case);
4360 
4361   // have a resolved InlineKlass in rcx, return the default value oop from it
4362   __ get_default_value_oop(rcx, rdx, rax);
4363   __ jmp(done);
4364 

4365   __ bind(slow_case);


4366 
4367   Register rarg1 = LP64_ONLY(c_rarg1) NOT_LP64(rcx);
4368   Register rarg2 = LP64_ONLY(c_rarg2) NOT_LP64(rdx);
4369 

4370   __ get_unsigned_2_byte_index_at_bcp(rarg2, 1);
4371   __ get_constant_pool(rarg1);
4372 
4373   call_VM(rax, CAST_FROM_FN_PTR(address, InterpreterRuntime::defaultvalue),
4374       rarg1, rarg2);
4375 

4376   __ bind(done);
4377   __ verify_oop(rax);
4378 }
4379 
4380 void TemplateTable::newarray() {
4381   transition(itos, atos);
4382   Register rarg1 = LP64_ONLY(c_rarg1) NOT_LP64(rdx);
4383   __ load_unsigned_byte(rarg1, at_bcp(1));
4384   call_VM(rax, CAST_FROM_FN_PTR(address, InterpreterRuntime::newarray),
4385           rarg1, rax);
4386 }
4387 
4388 void TemplateTable::anewarray() {
4389   transition(itos, atos);
4390 
4391   Register rarg1 = LP64_ONLY(c_rarg1) NOT_LP64(rcx);
4392   Register rarg2 = LP64_ONLY(c_rarg2) NOT_LP64(rdx);
4393 
4394   __ get_unsigned_2_byte_index_at_bcp(rarg2, 1);
4395   __ get_constant_pool(rarg1);
4396   call_VM(rax, CAST_FROM_FN_PTR(address, InterpreterRuntime::anewarray),
4397           rarg1, rarg2, rax);
4398 }
4399 
4400 void TemplateTable::arraylength() {
4401   transition(atos, itos);
4402   __ null_check(rax, arrayOopDesc::length_offset_in_bytes());
4403   __ movl(rax, Address(rax, arrayOopDesc::length_offset_in_bytes()));
4404 }
4405 
4406 void TemplateTable::checkcast() {
4407   transition(atos, atos);
4408   Label done, is_null, ok_is_subtype, quicked, resolved;
4409   __ testptr(rax, rax); // object is in rax
4410   __ jcc(Assembler::zero, is_null);
4411 
4412   // Get cpool & tags index
4413   __ get_cpool_and_tags(rcx, rdx); // rcx=cpool, rdx=tags array
4414   __ get_unsigned_2_byte_index_at_bcp(rbx, 1); // rbx=index
4415   // See if bytecode has already been quicked
4416   __ movzbl(rdx, Address(rdx, rbx,
4417       Address::times_1,
4418       Array<u1>::base_offset_in_bytes()));
4419   __ andl (rdx, ~JVM_CONSTANT_QDescBit);
4420   __ cmpl(rdx, JVM_CONSTANT_Class);
4421   __ jcc(Assembler::equal, quicked);
4422   __ push(atos); // save receiver for result, and for GC
4423   call_VM(noreg, CAST_FROM_FN_PTR(address, InterpreterRuntime::quicken_io_cc));
4424 
4425   // vm_result_2 has metadata result
4426 #ifndef _LP64
4427   // borrow rdi from locals
4428   __ get_thread(rdi);
4429   __ get_vm_result_2(rax, rdi);
4430   __ restore_locals();
4431 #else
4432   __ get_vm_result_2(rax, r15_thread);
4433 #endif
4434 
4435   __ pop_ptr(rdx); // restore receiver
4436   __ jmpb(resolved);
4437 
4438   // Get superklass in rax and subklass in rbx
4439   __ bind(quicked);
4440   __ mov(rdx, rax); // Save object in rdx; rax needed for subtype check
4441   __ load_resolved_klass_at_index(rax, rcx, rbx);
4442 
4443   __ bind(resolved);
4444   Register tmp_load_klass = LP64_ONLY(rscratch1) NOT_LP64(noreg);
4445   __ load_klass(rbx, rdx, tmp_load_klass);
4446 
4447   // Generate subtype check.  Blows rcx, rdi.  Object in rdx.
4448   // Superklass in rax.  Subklass in rbx.
4449   __ gen_subtype_check(rbx, ok_is_subtype);
4450 
4451   // Come here on failure
4452   __ push_ptr(rdx);
4453   // object is at TOS
4454   __ jump(ExternalAddress(Interpreter::_throw_ClassCastException_entry));
4455 
4456   // Come here on success
4457   __ bind(ok_is_subtype);
4458   __ mov(rax, rdx); // Restore object in rdx
4459   __ jmp(done);
4460 
4461   __ bind(is_null);
4462 
4463   // Collect counts on whether this check-cast sees NULLs a lot or not.
4464   if (ProfileInterpreter) {


4465     __ profile_null_seen(rcx);


4466   }
4467 
4468   if (EnableValhalla) {
4469     // Get cpool & tags index
4470     __ get_cpool_and_tags(rcx, rdx); // rcx=cpool, rdx=tags array
4471     __ get_unsigned_2_byte_index_at_bcp(rbx, 1); // rbx=index
4472     // See if CP entry is a Q-descriptor
4473     __ movzbl(rcx, Address(rdx, rbx,
4474         Address::times_1,
4475         Array<u1>::base_offset_in_bytes()));
4476     __ andl (rcx, JVM_CONSTANT_QDescBit);
4477     __ cmpl(rcx, JVM_CONSTANT_QDescBit);
4478     __ jcc(Assembler::notEqual, done);
4479     __ jump(ExternalAddress(Interpreter::_throw_NullPointerException_entry));
4480   }
4481 
4482   __ bind(done);
4483 }
4484 
4485 void TemplateTable::instanceof() {
4486   transition(atos, itos);
4487   Label done, is_null, ok_is_subtype, quicked, resolved;
4488   __ testptr(rax, rax);
4489   __ jcc(Assembler::zero, is_null);
4490 
4491   // Get cpool & tags index
4492   __ get_cpool_and_tags(rcx, rdx); // rcx=cpool, rdx=tags array
4493   __ get_unsigned_2_byte_index_at_bcp(rbx, 1); // rbx=index
4494   // See if bytecode has already been quicked
4495   __ movzbl(rdx, Address(rdx, rbx,
4496         Address::times_1,
4497         Array<u1>::base_offset_in_bytes()));
4498   __ andl (rdx, ~JVM_CONSTANT_QDescBit);
4499   __ cmpl(rdx, JVM_CONSTANT_Class);
4500   __ jcc(Assembler::equal, quicked);
4501 
4502   __ push(atos); // save receiver for result, and for GC
4503   call_VM(noreg, CAST_FROM_FN_PTR(address, InterpreterRuntime::quicken_io_cc));
4504   // vm_result_2 has metadata result
4505 
4506 #ifndef _LP64
4507   // borrow rdi from locals
4508   __ get_thread(rdi);
4509   __ get_vm_result_2(rax, rdi);
4510   __ restore_locals();
4511 #else
4512   __ get_vm_result_2(rax, r15_thread);
4513 #endif
4514 
4515   __ pop_ptr(rdx); // restore receiver
4516   __ verify_oop(rdx);
4517   Register tmp_load_klass = LP64_ONLY(rscratch1) NOT_LP64(noreg);
4518   __ load_klass(rdx, rdx, tmp_load_klass);
4519   __ jmpb(resolved);
4520 
4521   // Get superklass in rax and subklass in rdx
4522   __ bind(quicked);
4523   __ load_klass(rdx, rax, tmp_load_klass);
4524   __ load_resolved_klass_at_index(rax, rcx, rbx);
4525 
4526   __ bind(resolved);
4527 
4528   // Generate subtype check.  Blows rcx, rdi
4529   // Superklass in rax.  Subklass in rdx.
4530   __ gen_subtype_check(rdx, ok_is_subtype);
4531 
4532   // Come here on failure
4533   __ xorl(rax, rax);
4534   __ jmpb(done);
4535   // Come here on success
4536   __ bind(ok_is_subtype);
4537   __ movl(rax, 1);
4538 
4539   // Collect counts on whether this test sees NULLs a lot or not.
4540   if (ProfileInterpreter) {
4541     __ jmp(done);
4542     __ bind(is_null);
4543     __ profile_null_seen(rcx);
4544   } else {
4545     __ bind(is_null);   // same as 'done'
4546   }
4547   __ bind(done);
4548   // rax = 0: obj == NULL or  obj is not an instanceof the specified klass
4549   // rax = 1: obj != NULL and obj is     an instanceof the specified klass
4550 }
4551 

4552 //----------------------------------------------------------------------------------------------------
4553 // Breakpoints
4554 void TemplateTable::_breakpoint() {
4555   // Note: We get here even if we are single stepping..
4556   // jbug insists on setting breakpoints at every bytecode
4557   // even if we are in single step mode.
4558 
4559   transition(vtos, vtos);
4560 
4561   Register rarg = LP64_ONLY(c_rarg1) NOT_LP64(rcx);
4562 
4563   // get the unpatched byte code
4564   __ get_method(rarg);
4565   __ call_VM(noreg,
4566              CAST_FROM_FN_PTR(address,
4567                               InterpreterRuntime::get_original_bytecode_at),
4568              rarg, rbcp);
4569   __ mov(rbx, rax);  // why?
4570 
4571   // post the breakpoint event
4572   __ get_method(rarg);
4573   __ call_VM(noreg,
4574              CAST_FROM_FN_PTR(address, InterpreterRuntime::_breakpoint),
4575              rarg, rbcp);
4576 
4577   // complete the execution of original bytecode
4578   __ dispatch_only_normal(vtos);
4579 }
4580 
4581 //-----------------------------------------------------------------------------
4582 // Exceptions
4583 
4584 void TemplateTable::athrow() {
4585   transition(atos, vtos);
4586   __ null_check(rax);
4587   __ jump(ExternalAddress(Interpreter::throw_exception_entry()));
4588 }
4589 
4590 //-----------------------------------------------------------------------------
4591 // Synchronization
4592 //
4593 // Note: monitorenter & exit are symmetric routines; which is reflected
4594 //       in the assembly code structure as well
4595 //
4596 // Stack layout:
4597 //
4598 // [expressions  ] <--- rsp               = expression stack top
4599 // ..
4600 // [expressions  ]
4601 // [monitor entry] <--- monitor block top = expression stack bot
4602 // ..
4603 // [monitor entry]
4604 // [frame data   ] <--- monitor block bot
4605 // ...
4606 // [saved rbp    ] <--- rbp
4607 void TemplateTable::monitorenter() {
4608   transition(atos, vtos);
4609 
4610   // check for NULL object
4611   __ null_check(rax);
4612 
4613   Label is_inline_type;
4614   __ movptr(rbx, Address(rax, oopDesc::mark_offset_in_bytes()));
4615   __ test_markword_is_inline_type(rbx, is_inline_type);
4616 
4617   const Address monitor_block_top(
4618         rbp, frame::interpreter_frame_monitor_block_top_offset * wordSize);
4619   const Address monitor_block_bot(
4620         rbp, frame::interpreter_frame_initial_sp_offset * wordSize);
4621   const int entry_size = frame::interpreter_frame_monitor_size() * wordSize;
4622 
4623   Label allocated;
4624 
4625   Register rtop = LP64_ONLY(c_rarg3) NOT_LP64(rcx);
4626   Register rbot = LP64_ONLY(c_rarg2) NOT_LP64(rbx);
4627   Register rmon = LP64_ONLY(c_rarg1) NOT_LP64(rdx);
4628 
4629   // initialize entry pointer
4630   __ xorl(rmon, rmon); // points to free slot or NULL
4631 
4632   // find a free slot in the monitor block (result in rmon)
4633   {
4634     Label entry, loop, exit;
4635     __ movptr(rtop, monitor_block_top); // points to current entry,
4636                                         // starting with top-most entry
4637     __ lea(rbot, monitor_block_bot);    // points to word before bottom
4638                                         // of monitor block
4639     __ jmpb(entry);
4640 
4641     __ bind(loop);
4642     // check if current entry is used
4643     __ cmpptr(Address(rtop, BasicObjectLock::obj_offset_in_bytes()), (int32_t) NULL_WORD);
4644     // if not used then remember entry in rmon
4645     __ cmovptr(Assembler::equal, rmon, rtop);   // cmov => cmovptr
4646     // check if current entry is for same object
4647     __ cmpptr(rax, Address(rtop, BasicObjectLock::obj_offset_in_bytes()));
4648     // if same object then stop searching
4649     __ jccb(Assembler::equal, exit);
4650     // otherwise advance to next entry
4651     __ addptr(rtop, entry_size);
4652     __ bind(entry);
4653     // check if bottom reached
4654     __ cmpptr(rtop, rbot);
4655     // if not at bottom then check this entry
4656     __ jcc(Assembler::notEqual, loop);
4657     __ bind(exit);
4658   }
4659 
4660   __ testptr(rmon, rmon); // check if a slot has been found
4661   __ jcc(Assembler::notZero, allocated); // if found, continue with that one
4662 
4663   // allocate one if there's no free slot
4664   {
4665     Label entry, loop;
4666     // 1. compute new pointers          // rsp: old expression stack top
4667     __ movptr(rmon, monitor_block_bot); // rmon: old expression stack bottom
4668     __ subptr(rsp, entry_size);         // move expression stack top
4669     __ subptr(rmon, entry_size);        // move expression stack bottom
4670     __ mov(rtop, rsp);                  // set start value for copy loop
4671     __ movptr(monitor_block_bot, rmon); // set new monitor block bottom
4672     __ jmp(entry);
4673     // 2. move expression stack contents
4674     __ bind(loop);
4675     __ movptr(rbot, Address(rtop, entry_size)); // load expression stack
4676                                                 // word from old location
4677     __ movptr(Address(rtop, 0), rbot);          // and store it at new location
4678     __ addptr(rtop, wordSize);                  // advance to next word
4679     __ bind(entry);
4680     __ cmpptr(rtop, rmon);                      // check if bottom reached
4681     __ jcc(Assembler::notEqual, loop);          // if not at bottom then
4682                                                 // copy next word
4683   }
4684 
4685   // call run-time routine
4686   // rmon: points to monitor entry
4687   __ bind(allocated);
4688 
4689   // Increment bcp to point to the next bytecode, so exception
4690   // handling for async. exceptions work correctly.
4691   // The object has already been poped from the stack, so the
4692   // expression stack looks correct.
4693   __ increment(rbcp);
4694 
4695   // store object
4696   __ movptr(Address(rmon, BasicObjectLock::obj_offset_in_bytes()), rax);
4697   __ lock_object(rmon);
4698 
4699   // check to make sure this monitor doesn't cause stack overflow after locking
4700   __ save_bcp();  // in case of exception
4701   __ generate_stack_overflow_check(0);
4702 
4703   // The bcp has already been incremented. Just need to dispatch to
4704   // next instruction.
4705   __ dispatch_next(vtos);
4706 
4707   __ bind(is_inline_type);
4708   __ call_VM(noreg, CAST_FROM_FN_PTR(address,
4709                     InterpreterRuntime::throw_illegal_monitor_state_exception));
4710   __ should_not_reach_here();
4711 }
4712 
4713 void TemplateTable::monitorexit() {
4714   transition(atos, vtos);
4715 
4716   // check for NULL object
4717   __ null_check(rax);
4718 
4719   const int is_inline_type_mask = markWord::inline_type_pattern;
4720   Label has_identity;
4721   __ movptr(rbx, Address(rax, oopDesc::mark_offset_in_bytes()));
4722   __ andptr(rbx, is_inline_type_mask);
4723   __ cmpl(rbx, is_inline_type_mask);
4724   __ jcc(Assembler::notEqual, has_identity);
4725   __ call_VM(noreg, CAST_FROM_FN_PTR(address,
4726                      InterpreterRuntime::throw_illegal_monitor_state_exception));
4727   __ should_not_reach_here();
4728   __ bind(has_identity);
4729 
4730   const Address monitor_block_top(
4731         rbp, frame::interpreter_frame_monitor_block_top_offset * wordSize);
4732   const Address monitor_block_bot(
4733         rbp, frame::interpreter_frame_initial_sp_offset * wordSize);
4734   const int entry_size = frame::interpreter_frame_monitor_size() * wordSize;
4735 
4736   Register rtop = LP64_ONLY(c_rarg1) NOT_LP64(rdx);
4737   Register rbot = LP64_ONLY(c_rarg2) NOT_LP64(rbx);
4738 
4739   Label found;
4740 
4741   // find matching slot
4742   {
4743     Label entry, loop;
4744     __ movptr(rtop, monitor_block_top); // points to current entry,
4745                                         // starting with top-most entry
4746     __ lea(rbot, monitor_block_bot);    // points to word before bottom
4747                                         // of monitor block
4748     __ jmpb(entry);
4749 
4750     __ bind(loop);
4751     // check if current entry is for same object
4752     __ cmpptr(rax, Address(rtop, BasicObjectLock::obj_offset_in_bytes()));
4753     // if same object then stop searching
4754     __ jcc(Assembler::equal, found);
4755     // otherwise advance to next entry
4756     __ addptr(rtop, entry_size);
4757     __ bind(entry);
4758     // check if bottom reached
4759     __ cmpptr(rtop, rbot);
4760     // if not at bottom then check this entry
4761     __ jcc(Assembler::notEqual, loop);
4762   }
4763 
4764   // error handling. Unlocking was not block-structured
4765   __ call_VM(noreg, CAST_FROM_FN_PTR(address,
4766                    InterpreterRuntime::throw_illegal_monitor_state_exception));
4767   __ should_not_reach_here();
4768 
4769   // call run-time routine
4770   __ bind(found);
4771   __ push_ptr(rax); // make sure object is on stack (contract with oopMaps)
4772   __ unlock_object(rtop);
4773   __ pop_ptr(rax); // discard object
4774 }
4775 
4776 // Wide instructions
4777 void TemplateTable::wide() {
4778   transition(vtos, vtos);
4779   __ load_unsigned_byte(rbx, at_bcp(1));
4780   ExternalAddress wtable((address)Interpreter::_wentry_point);
4781   __ jump(ArrayAddress(wtable, Address(noreg, rbx, Address::times_ptr)));
4782   // Note: the rbcp increment step is part of the individual wide bytecode implementations
4783 }
4784 
4785 // Multi arrays
4786 void TemplateTable::multianewarray() {
4787   transition(vtos, atos);
4788 
4789   Register rarg = LP64_ONLY(c_rarg1) NOT_LP64(rax);
4790   __ load_unsigned_byte(rax, at_bcp(3)); // get number of dimensions
4791   // last dim is on top of stack; we want address of first one:
4792   // first_addr = last_addr + (ndims - 1) * stackElementSize - 1*wordsize
4793   // the latter wordSize to point to the beginning of the array.
4794   __ lea(rarg, Address(rsp, rax, Interpreter::stackElementScale(), -wordSize));
4795   call_VM(rax, CAST_FROM_FN_PTR(address, InterpreterRuntime::multianewarray), rarg);
4796   __ load_unsigned_byte(rbx, at_bcp(3));
4797   __ lea(rsp, Address(rsp, rbx, Interpreter::stackElementScale()));  // get rid of counts
4798 }
--- EOF ---