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