1 /*
   2  * Copyright (c) 1997, 2023, 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(ExternalAddress(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(ExternalAddress(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              ExternalAddress(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              ExternalAddress(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              ExternalAddress(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              ExternalAddress(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     __ movl(rdi, Address(rdi, Klass::access_flags_offset()));
2583     __ testl(rdi, JVM_ACC_HAS_FINALIZER);
2584     Label skip_register_finalizer;
2585     __ jcc(Assembler::zero, skip_register_finalizer);
2586 
2587     __ call_VM(noreg, CAST_FROM_FN_PTR(address, InterpreterRuntime::register_finalizer), robj);
2588 
2589     __ bind(skip_register_finalizer);
2590   }
2591 
2592   if (_desc->bytecode() != Bytecodes::_return_register_finalizer) {
2593     Label no_safepoint;
2594     NOT_PRODUCT(__ block_comment("Thread-local Safepoint poll"));
2595 #ifdef _LP64
2596     __ testb(Address(r15_thread, JavaThread::polling_word_offset()), SafepointMechanism::poll_bit());
2597 #else
2598     const Register thread = rdi;
2599     __ get_thread(thread);
2600     __ testb(Address(thread, JavaThread::polling_word_offset()), SafepointMechanism::poll_bit());
2601 #endif
2602     __ jcc(Assembler::zero, no_safepoint);
2603     __ push(state);
2604     __ push_cont_fastpath();
2605     __ call_VM(noreg, CAST_FROM_FN_PTR(address,
2606                                        InterpreterRuntime::at_safepoint));
2607     __ pop_cont_fastpath();
2608     __ pop(state);
2609     __ bind(no_safepoint);
2610   }
2611 
2612   // Narrow result if state is itos but result type is smaller.
2613   // Need to narrow in the return bytecode rather than in generate_return_entry
2614   // since compiled code callers expect the result to already be narrowed.
2615   if (state == itos) {
2616     __ narrow(rax);
2617   }
2618   __ remove_activation(state, rbcp);
2619 
2620   __ jmp(rbcp);
2621 }
2622 
2623 // ----------------------------------------------------------------------------
2624 // Volatile variables demand their effects be made known to all CPU's
2625 // in order.  Store buffers on most chips allow reads & writes to
2626 // reorder; the JMM's ReadAfterWrite.java test fails in -Xint mode
2627 // without some kind of memory barrier (i.e., it's not sufficient that
2628 // the interpreter does not reorder volatile references, the hardware
2629 // also must not reorder them).
2630 //
2631 // According to the new Java Memory Model (JMM):
2632 // (1) All volatiles are serialized wrt to each other.  ALSO reads &
2633 //     writes act as acquire & release, so:
2634 // (2) A read cannot let unrelated NON-volatile memory refs that
2635 //     happen after the read float up to before the read.  It's OK for
2636 //     non-volatile memory refs that happen before the volatile read to
2637 //     float down below it.
2638 // (3) Similar a volatile write cannot let unrelated NON-volatile
2639 //     memory refs that happen BEFORE the write float down to after the
2640 //     write.  It's OK for non-volatile memory refs that happen after the
2641 //     volatile write to float up before it.
2642 //
2643 // We only put in barriers around volatile refs (they are expensive),
2644 // not _between_ memory refs (that would require us to track the
2645 // flavor of the previous memory refs).  Requirements (2) and (3)
2646 // require some barriers before volatile stores and after volatile
2647 // loads.  These nearly cover requirement (1) but miss the
2648 // volatile-store-volatile-load case.  This final case is placed after
2649 // volatile-stores although it could just as well go before
2650 // volatile-loads.
2651 
2652 void TemplateTable::volatile_barrier(Assembler::Membar_mask_bits order_constraint ) {
2653   // Helper function to insert a is-volatile test and memory barrier
2654   __ membar(order_constraint);
2655 }
2656 
2657 void TemplateTable::resolve_cache_and_index_for_method(int byte_no,
2658                                                        Register cache,
2659                                                        Register index) {
2660   const Register temp = rbx;
2661   assert_different_registers(cache, index, temp);
2662 
2663   Label L_clinit_barrier_slow;
2664   Label resolved;
2665 
2666   Bytecodes::Code code = bytecode();
2667 
2668   assert(byte_no == f1_byte || byte_no == f2_byte, "byte_no out of range");
2669 
2670   __ load_method_entry(cache, index);
2671   switch(byte_no) {
2672     case f1_byte:
2673       __ load_unsigned_byte(temp, Address(cache, in_bytes(ResolvedMethodEntry::bytecode1_offset())));
2674       break;
2675     case f2_byte:
2676       __ load_unsigned_byte(temp, Address(cache, in_bytes(ResolvedMethodEntry::bytecode2_offset())));
2677       break;
2678     default:
2679       ShouldNotReachHere();
2680   }
2681   __ cmpl(temp, code);  // have we resolved this bytecode?
2682   __ jcc(Assembler::equal, resolved);
2683 
2684   // resolve first time through
2685   // Class initialization barrier slow path lands here as well.
2686   __ bind(L_clinit_barrier_slow);
2687   address entry = CAST_FROM_FN_PTR(address, InterpreterRuntime::resolve_from_cache);
2688   __ movl(temp, code);
2689   __ call_VM(noreg, entry, temp);
2690   // Update registers with resolved info
2691   __ load_method_entry(cache, index);
2692 
2693   __ bind(resolved);
2694 
2695   // Class initialization barrier for static methods
2696   if (VM_Version::supports_fast_class_init_checks() && bytecode() == Bytecodes::_invokestatic) {
2697     const Register method = temp;
2698     const Register klass  = temp;
2699     const Register thread = LP64_ONLY(r15_thread) NOT_LP64(noreg);
2700     assert(thread != noreg, "x86_32 not supported");
2701 
2702     __ movptr(method, Address(cache, in_bytes(ResolvedMethodEntry::method_offset())));
2703     __ load_method_holder(klass, method);
2704     __ clinit_barrier(klass, thread, nullptr /*L_fast_path*/, &L_clinit_barrier_slow);
2705   }
2706 }
2707 
2708 void TemplateTable::resolve_cache_and_index_for_field(int byte_no,
2709                                             Register cache,
2710                                             Register index) {
2711   const Register temp = rbx;
2712   assert_different_registers(cache, index, temp);
2713 
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   address entry = CAST_FROM_FN_PTR(address, InterpreterRuntime::resolve_from_cache);
2735   __ movl(temp, code);
2736   __ call_VM(noreg, entry, temp);
2737   // Update registers with resolved info
2738   __ load_field_entry(cache, index);
2739 
2740   __ bind(resolved);
2741 }
2742 
2743 void TemplateTable::load_resolved_field_entry(Register obj,
2744                                               Register cache,
2745                                               Register tos_state,
2746                                               Register offset,
2747                                               Register flags,
2748                                               bool is_static = false) {
2749   assert_different_registers(cache, tos_state, flags, offset);
2750 
2751   // Field offset
2752   __ load_sized_value(offset, Address(cache, in_bytes(ResolvedFieldEntry::field_offset_offset())), sizeof(int), true /*is_signed*/);
2753 
2754   // Flags
2755   __ load_unsigned_byte(flags, Address(cache, in_bytes(ResolvedFieldEntry::flags_offset())));
2756 
2757   // TOS state
2758   __ load_unsigned_byte(tos_state, Address(cache, in_bytes(ResolvedFieldEntry::type_offset())));
2759 
2760   // Klass overwrite register
2761   if (is_static) {
2762     __ movptr(obj, Address(cache, ResolvedFieldEntry::field_holder_offset()));
2763     const int mirror_offset = in_bytes(Klass::java_mirror_offset());
2764     __ movptr(obj, Address(obj, mirror_offset));
2765     __ resolve_oop_handle(obj, rscratch2);
2766   }
2767 
2768 }
2769 
2770 void TemplateTable::load_invokedynamic_entry(Register method) {
2771   // setup registers
2772   const Register appendix = rax;
2773   const Register cache = rcx;
2774   const Register index = rdx;
2775   assert_different_registers(method, appendix, cache, index);
2776 
2777   __ save_bcp();
2778 
2779   Label resolved;
2780 
2781   __ load_resolved_indy_entry(cache, index);
2782   __ movptr(method, Address(cache, in_bytes(ResolvedIndyEntry::method_offset())));
2783 
2784   // Compare the method to zero
2785   __ testptr(method, method);
2786   __ jcc(Assembler::notZero, resolved);
2787 
2788   Bytecodes::Code code = bytecode();
2789 
2790   // Call to the interpreter runtime to resolve invokedynamic
2791   address entry = CAST_FROM_FN_PTR(address, InterpreterRuntime::resolve_from_cache);
2792   __ movl(method, code); // this is essentially Bytecodes::_invokedynamic
2793   __ call_VM(noreg, entry, method);
2794   // Update registers with resolved info
2795   __ load_resolved_indy_entry(cache, index);
2796   __ movptr(method, Address(cache, in_bytes(ResolvedIndyEntry::method_offset())));
2797 
2798 #ifdef ASSERT
2799   __ testptr(method, method);
2800   __ jcc(Assembler::notZero, resolved);
2801   __ stop("Should be resolved by now");
2802 #endif // ASSERT
2803   __ bind(resolved);
2804 
2805   Label L_no_push;
2806   // Check if there is an appendix
2807   __ load_unsigned_byte(index, Address(cache, in_bytes(ResolvedIndyEntry::flags_offset())));
2808   __ testl(index, (1 << ResolvedIndyEntry::has_appendix_shift));
2809   __ jcc(Assembler::zero, L_no_push);
2810 
2811   // Get appendix
2812   __ load_unsigned_short(index, Address(cache, in_bytes(ResolvedIndyEntry::resolved_references_index_offset())));
2813   // Push the appendix as a trailing parameter
2814   // since the parameter_size includes it.
2815   __ load_resolved_reference_at_index(appendix, index);
2816   __ verify_oop(appendix);
2817   __ push(appendix);  // push appendix (MethodType, CallSite, etc.)
2818   __ bind(L_no_push);
2819 
2820   // compute return type
2821   __ load_unsigned_byte(index, Address(cache, in_bytes(ResolvedIndyEntry::result_type_offset())));
2822   // load return address
2823   {
2824     const address table_addr = (address) Interpreter::invoke_return_entry_table_for(code);
2825     ExternalAddress table(table_addr);
2826 #ifdef _LP64
2827     __ lea(rscratch1, table);
2828     __ movptr(index, Address(rscratch1, index, Address::times_ptr));
2829 #else
2830     __ movptr(index, ArrayAddress(table, Address(noreg, index, Address::times_ptr)));
2831 #endif // _LP64
2832   }
2833 
2834   // push return address
2835   __ push(index);
2836 }
2837 
2838 void TemplateTable::load_resolved_method_entry_special_or_static(Register cache,
2839                                                                  Register method,
2840                                                                  Register flags) {
2841   // setup registers
2842   const Register index = rdx;
2843   assert_different_registers(cache, index);
2844   assert_different_registers(method, cache, flags);
2845 
2846   // determine constant pool cache field offsets
2847   resolve_cache_and_index_for_method(f1_byte, cache, index);
2848   __ load_unsigned_byte(flags, Address(cache, in_bytes(ResolvedMethodEntry::flags_offset())));
2849   __ movptr(method, Address(cache, in_bytes(ResolvedMethodEntry::method_offset())));
2850 }
2851 
2852 void TemplateTable::load_resolved_method_entry_handle(Register cache,
2853                                                Register method,
2854                                                Register ref_index,
2855                                                Register flags) {
2856   // setup registers
2857   const Register index = rdx;
2858   assert_different_registers(cache, index);
2859   assert_different_registers(cache, method, ref_index, flags);
2860 
2861   // determine constant pool cache field offsets
2862   resolve_cache_and_index_for_method(f1_byte, cache, index);
2863   __ load_unsigned_byte(flags, Address(cache, in_bytes(ResolvedMethodEntry::flags_offset())));
2864 
2865   // Maybe push appendix
2866   Label L_no_push;
2867   __ testl(flags, (1 << ResolvedMethodEntry::has_appendix_shift));
2868   __ jcc(Assembler::zero, L_no_push);
2869   // invokehandle uses an index into the resolved references array
2870   __ load_unsigned_short(ref_index, Address(cache, in_bytes(ResolvedMethodEntry::resolved_references_index_offset())));
2871   // Push the appendix as a trailing parameter.
2872   // This must be done before we get the receiver,
2873   // since the parameter_size includes it.
2874   Register appendix = method;
2875   __ load_resolved_reference_at_index(appendix, ref_index);
2876   __ push(appendix);  // push appendix (MethodType, CallSite, etc.)
2877   __ bind(L_no_push);
2878 
2879   __ movptr(method, Address(cache, in_bytes(ResolvedMethodEntry::method_offset())));
2880 }
2881 
2882 void TemplateTable::load_resolved_method_entry_interface(Register cache,
2883                                                          Register klass,
2884                                                          Register method_or_table_index,
2885                                                          Register flags) {
2886   // setup registers
2887   const Register index = rdx;
2888   assert_different_registers(cache, klass, method_or_table_index, flags);
2889 
2890   // determine constant pool cache field offsets
2891   resolve_cache_and_index_for_method(f1_byte, cache, index);
2892   __ load_unsigned_byte(flags, Address(cache, in_bytes(ResolvedMethodEntry::flags_offset())));
2893 
2894   // Invokeinterface can behave in different ways:
2895   // If calling a method from java.lang.Object, the forced virtual flag is true so the invocation will
2896   // behave like an invokevirtual call. The state of the virtual final flag will determine whether a method or
2897   // vtable index is placed in the register.
2898   // Otherwise, the registers will be populated with the klass and method.
2899 
2900   Label NotVirtual; Label NotVFinal; Label Done;
2901   __ testl(flags, 1 << ResolvedMethodEntry::is_forced_virtual_shift);
2902   __ jcc(Assembler::zero, NotVirtual);
2903   __ testl(flags, (1 << ResolvedMethodEntry::is_vfinal_shift));
2904   __ jcc(Assembler::zero, NotVFinal);
2905   __ movptr(method_or_table_index, Address(cache, in_bytes(ResolvedMethodEntry::method_offset())));
2906   __ jmp(Done);
2907 
2908   __ bind(NotVFinal);
2909   __ load_unsigned_short(method_or_table_index, Address(cache, in_bytes(ResolvedMethodEntry::table_index_offset())));
2910   __ jmp(Done);
2911 
2912   __ bind(NotVirtual);
2913   __ movptr(method_or_table_index, Address(cache, in_bytes(ResolvedMethodEntry::method_offset())));
2914   __ movptr(klass, Address(cache, in_bytes(ResolvedMethodEntry::klass_offset())));
2915   __ bind(Done);
2916 }
2917 
2918 void TemplateTable::load_resolved_method_entry_virtual(Register cache,
2919                                                        Register method_or_table_index,
2920                                                        Register flags) {
2921   // setup registers
2922   const Register index = rdx;
2923   assert_different_registers(index, cache);
2924   assert_different_registers(method_or_table_index, cache, flags);
2925 
2926   // determine constant pool cache field offsets
2927   resolve_cache_and_index_for_method(f2_byte, cache, index);
2928   __ load_unsigned_byte(flags, Address(cache, in_bytes(ResolvedMethodEntry::flags_offset())));
2929 
2930   // method_or_table_index can either be an itable index or a method depending on the virtual final flag
2931   Label isVFinal; Label Done;
2932   __ testl(flags, (1 << ResolvedMethodEntry::is_vfinal_shift));
2933   __ jcc(Assembler::notZero, isVFinal);
2934   __ load_unsigned_short(method_or_table_index, Address(cache, in_bytes(ResolvedMethodEntry::table_index_offset())));
2935   __ jmp(Done);
2936   __ bind(isVFinal);
2937   __ movptr(method_or_table_index, Address(cache, in_bytes(ResolvedMethodEntry::method_offset())));
2938   __ bind(Done);
2939 }
2940 
2941 // The registers cache and index expected to be set before call.
2942 // Correct values of the cache and index registers are preserved.
2943 void TemplateTable::jvmti_post_field_access(Register cache,
2944                                             Register index,
2945                                             bool is_static,
2946                                             bool has_tos) {
2947   if (JvmtiExport::can_post_field_access()) {
2948     // Check to see if a field access watch has been set before we take
2949     // the time to call into the VM.
2950     Label L1;
2951     assert_different_registers(cache, index, rax);
2952     __ mov32(rax, ExternalAddress((address) JvmtiExport::get_field_access_count_addr()));
2953     __ testl(rax,rax);
2954     __ jcc(Assembler::zero, L1);
2955 
2956     // cache entry pointer
2957     __ load_field_entry(cache, index);
2958     if (is_static) {
2959       __ xorptr(rax, rax);      // null object reference
2960     } else {
2961       __ pop(atos);         // Get the object
2962       __ verify_oop(rax);
2963       __ push(atos);        // Restore stack state
2964     }
2965     // rax,:   object pointer or null
2966     // cache: cache entry pointer
2967     __ call_VM(noreg, CAST_FROM_FN_PTR(address, InterpreterRuntime::post_field_access),
2968               rax, cache);
2969 
2970     __ load_field_entry(cache, index);
2971     __ bind(L1);
2972   }
2973 }
2974 
2975 void TemplateTable::pop_and_check_object(Register r) {
2976   __ pop_ptr(r);
2977   __ null_check(r);  // for field access must check obj.
2978   __ verify_oop(r);
2979 }
2980 
2981 void TemplateTable::getfield_or_static(int byte_no, bool is_static, RewriteControl rc) {
2982   transition(vtos, vtos);
2983 
2984   const Register obj   = LP64_ONLY(c_rarg3) NOT_LP64(rcx);
2985   const Register cache = rcx;
2986   const Register index = rdx;
2987   const Register off   = rbx;
2988   const Register tos_state   = rax;
2989   const Register flags = rdx;
2990   const Register bc    = LP64_ONLY(c_rarg3) NOT_LP64(rcx); // uses same reg as obj, so don't mix them
2991 
2992   resolve_cache_and_index_for_field(byte_no, cache, index);
2993   jvmti_post_field_access(cache, index, is_static, false);
2994   load_resolved_field_entry(obj, cache, tos_state, off, flags, is_static);
2995 
2996   if (!is_static) pop_and_check_object(obj);
2997 
2998   const Address field(obj, off, Address::times_1, 0*wordSize);
2999 
3000   Label Done, notByte, notBool, notInt, notShort, notChar, notLong, notFloat, notObj;
3001 
3002   // Make sure we don't need to mask edx after the above shift
3003   assert(btos == 0, "change code, btos != 0");
3004   __ testl(tos_state, tos_state);
3005   __ jcc(Assembler::notZero, notByte);
3006 
3007   // btos
3008   __ access_load_at(T_BYTE, IN_HEAP, rax, field, noreg, noreg);
3009   __ push(btos);
3010   // Rewrite bytecode to be faster
3011   if (!is_static && rc == may_rewrite) {
3012     patch_bytecode(Bytecodes::_fast_bgetfield, bc, rbx);
3013   }
3014   __ jmp(Done);
3015 
3016   __ bind(notByte);
3017   __ cmpl(tos_state, ztos);
3018   __ jcc(Assembler::notEqual, notBool);
3019 
3020   // ztos (same code as btos)
3021   __ access_load_at(T_BOOLEAN, IN_HEAP, rax, field, noreg, noreg);
3022   __ push(ztos);
3023   // Rewrite bytecode to be faster
3024   if (!is_static && rc == may_rewrite) {
3025     // use btos rewriting, no truncating to t/f bit is needed for getfield.
3026     patch_bytecode(Bytecodes::_fast_bgetfield, bc, rbx);
3027   }
3028   __ jmp(Done);
3029 
3030   __ bind(notBool);
3031   __ cmpl(tos_state, atos);
3032   __ jcc(Assembler::notEqual, notObj);
3033   // atos
3034   do_oop_load(_masm, field, rax);
3035   __ push(atos);
3036   if (!is_static && rc == may_rewrite) {
3037     patch_bytecode(Bytecodes::_fast_agetfield, bc, rbx);
3038   }
3039   __ jmp(Done);
3040 
3041   __ bind(notObj);
3042   __ cmpl(tos_state, itos);
3043   __ jcc(Assembler::notEqual, notInt);
3044   // itos
3045   __ access_load_at(T_INT, IN_HEAP, rax, field, noreg, noreg);
3046   __ push(itos);
3047   // Rewrite bytecode to be faster
3048   if (!is_static && rc == may_rewrite) {
3049     patch_bytecode(Bytecodes::_fast_igetfield, bc, rbx);
3050   }
3051   __ jmp(Done);
3052 
3053   __ bind(notInt);
3054   __ cmpl(tos_state, ctos);
3055   __ jcc(Assembler::notEqual, notChar);
3056   // ctos
3057   __ access_load_at(T_CHAR, IN_HEAP, rax, field, noreg, noreg);
3058   __ push(ctos);
3059   // Rewrite bytecode to be faster
3060   if (!is_static && rc == may_rewrite) {
3061     patch_bytecode(Bytecodes::_fast_cgetfield, bc, rbx);
3062   }
3063   __ jmp(Done);
3064 
3065   __ bind(notChar);
3066   __ cmpl(tos_state, stos);
3067   __ jcc(Assembler::notEqual, notShort);
3068   // stos
3069   __ access_load_at(T_SHORT, IN_HEAP, rax, field, noreg, noreg);
3070   __ push(stos);
3071   // Rewrite bytecode to be faster
3072   if (!is_static && rc == may_rewrite) {
3073     patch_bytecode(Bytecodes::_fast_sgetfield, bc, rbx);
3074   }
3075   __ jmp(Done);
3076 
3077   __ bind(notShort);
3078   __ cmpl(tos_state, ltos);
3079   __ jcc(Assembler::notEqual, notLong);
3080   // ltos
3081     // Generate code as if volatile (x86_32).  There just aren't enough registers to
3082     // save that information and this code is faster than the test.
3083   __ access_load_at(T_LONG, IN_HEAP | MO_RELAXED, noreg /* ltos */, field, noreg, noreg);
3084   __ push(ltos);
3085   // Rewrite bytecode to be faster
3086   LP64_ONLY(if (!is_static && rc == may_rewrite) patch_bytecode(Bytecodes::_fast_lgetfield, bc, rbx));
3087   __ jmp(Done);
3088 
3089   __ bind(notLong);
3090   __ cmpl(tos_state, ftos);
3091   __ jcc(Assembler::notEqual, notFloat);
3092   // ftos
3093 
3094   __ access_load_at(T_FLOAT, IN_HEAP, noreg /* ftos */, field, noreg, noreg);
3095   __ push(ftos);
3096   // Rewrite bytecode to be faster
3097   if (!is_static && rc == may_rewrite) {
3098     patch_bytecode(Bytecodes::_fast_fgetfield, bc, rbx);
3099   }
3100   __ jmp(Done);
3101 
3102   __ bind(notFloat);
3103 #ifdef ASSERT
3104   Label notDouble;
3105   __ cmpl(tos_state, dtos);
3106   __ jcc(Assembler::notEqual, notDouble);
3107 #endif
3108   // dtos
3109   // MO_RELAXED: for the case of volatile field, in fact it adds no extra work for the underlying implementation
3110   __ access_load_at(T_DOUBLE, IN_HEAP | MO_RELAXED, noreg /* dtos */, field, noreg, noreg);
3111   __ push(dtos);
3112   // Rewrite bytecode to be faster
3113   if (!is_static && rc == may_rewrite) {
3114     patch_bytecode(Bytecodes::_fast_dgetfield, bc, rbx);
3115   }
3116 #ifdef ASSERT
3117   __ jmp(Done);
3118 
3119   __ bind(notDouble);
3120   __ stop("Bad state");
3121 #endif
3122 
3123   __ bind(Done);
3124   // [jk] not needed currently
3125   // volatile_barrier(Assembler::Membar_mask_bits(Assembler::LoadLoad |
3126   //                                              Assembler::LoadStore));
3127 }
3128 
3129 void TemplateTable::getfield(int byte_no) {
3130   getfield_or_static(byte_no, false);
3131 }
3132 
3133 void TemplateTable::nofast_getfield(int byte_no) {
3134   getfield_or_static(byte_no, false, may_not_rewrite);
3135 }
3136 
3137 void TemplateTable::getstatic(int byte_no) {
3138   getfield_or_static(byte_no, true);
3139 }
3140 
3141 
3142 // The registers cache and index expected to be set before call.
3143 // The function may destroy various registers, just not the cache and index registers.
3144 void TemplateTable::jvmti_post_field_mod(Register cache, Register index, bool is_static) {
3145   // Cache is rcx and index is rdx
3146   const Register entry = LP64_ONLY(c_rarg2) NOT_LP64(rax); // ResolvedFieldEntry
3147   const Register obj = LP64_ONLY(c_rarg1) NOT_LP64(rbx);   // Object pointer
3148   const Register value = LP64_ONLY(c_rarg3) NOT_LP64(rcx); // JValue object
3149 
3150   if (JvmtiExport::can_post_field_modification()) {
3151     // Check to see if a field modification watch has been set before
3152     // we take the time to call into the VM.
3153     Label L1;
3154     assert_different_registers(cache, obj, rax);
3155     __ mov32(rax, ExternalAddress((address)JvmtiExport::get_field_modification_count_addr()));
3156     __ testl(rax, rax);
3157     __ jcc(Assembler::zero, L1);
3158 
3159     __ mov(entry, cache);
3160 
3161     if (is_static) {
3162       // Life is simple.  Null out the object pointer.
3163       __ xorl(obj, obj);
3164 
3165     } else {
3166       // Life is harder. The stack holds the value on top, followed by
3167       // the object.  We don't know the size of the value, though; it
3168       // could be one or two words depending on its type. As a result,
3169       // we must find the type to determine where the object is.
3170 #ifndef _LP64
3171       Label two_word, valsize_known;
3172 #endif
3173       __ load_unsigned_byte(value, Address(entry, in_bytes(ResolvedFieldEntry::type_offset())));
3174 #ifdef _LP64
3175       __ movptr(obj, at_tos_p1());  // initially assume a one word jvalue
3176       __ cmpl(value, ltos);
3177       __ cmovptr(Assembler::equal,
3178                  obj, at_tos_p2()); // ltos (two word jvalue)
3179       __ cmpl(value, dtos);
3180       __ cmovptr(Assembler::equal,
3181                  obj, at_tos_p2()); // dtos (two word jvalue)
3182 #else
3183       __ mov(obj, rsp);
3184       __ cmpl(value, ltos);
3185       __ jccb(Assembler::equal, two_word);
3186       __ cmpl(value, dtos);
3187       __ jccb(Assembler::equal, two_word);
3188       __ addptr(obj, Interpreter::expr_offset_in_bytes(1)); // one word jvalue (not ltos, dtos)
3189       __ jmpb(valsize_known);
3190 
3191       __ bind(two_word);
3192       __ addptr(obj, Interpreter::expr_offset_in_bytes(2)); // two words jvalue
3193 
3194       __ bind(valsize_known);
3195       // setup object pointer
3196       __ movptr(obj, Address(obj, 0));
3197 #endif
3198     }
3199 
3200     // object (tos)
3201     __ mov(value, rsp);
3202     // obj: object pointer set up above (null if static)
3203     // cache: field entry pointer
3204     // value: jvalue object on the stack
3205     __ call_VM(noreg,
3206               CAST_FROM_FN_PTR(address,
3207                               InterpreterRuntime::post_field_modification),
3208               obj, entry, value);
3209     // Reload field entry
3210     __ load_field_entry(cache, index);
3211     __ bind(L1);
3212   }
3213 }
3214 
3215 void TemplateTable::putfield_or_static(int byte_no, bool is_static, RewriteControl rc) {
3216   transition(vtos, vtos);
3217 
3218   const Register obj = rcx;
3219   const Register cache = rcx;
3220   const Register index = rdx;
3221   const Register tos_state   = rdx;
3222   const Register off   = rbx;
3223   const Register flags = rax;
3224 
3225   resolve_cache_and_index_for_field(byte_no, cache, index);
3226   jvmti_post_field_mod(cache, index, is_static);
3227   load_resolved_field_entry(obj, cache, tos_state, off, flags, is_static);
3228 
3229   // [jk] not needed currently
3230   // volatile_barrier(Assembler::Membar_mask_bits(Assembler::LoadStore |
3231   //                                              Assembler::StoreStore));
3232 
3233   Label notVolatile, Done;
3234 
3235   // Check for volatile store
3236   __ andl(flags, (1 << ResolvedFieldEntry::is_volatile_shift));
3237   __ testl(flags, flags);
3238   __ jcc(Assembler::zero, notVolatile);
3239 
3240   putfield_or_static_helper(byte_no, is_static, rc, obj, off, tos_state);
3241   volatile_barrier(Assembler::Membar_mask_bits(Assembler::StoreLoad |
3242                                                Assembler::StoreStore));
3243   __ jmp(Done);
3244   __ bind(notVolatile);
3245 
3246   putfield_or_static_helper(byte_no, is_static, rc, obj, off, tos_state);
3247 
3248   __ bind(Done);
3249 }
3250 
3251 void TemplateTable::putfield_or_static_helper(int byte_no, bool is_static, RewriteControl rc,
3252                                               Register obj, Register off, Register tos_state) {
3253 
3254   // field addresses
3255   const Address field(obj, off, Address::times_1, 0*wordSize);
3256   NOT_LP64( const Address hi(obj, off, Address::times_1, 1*wordSize);)
3257 
3258   Label notByte, notBool, notInt, notShort, notChar,
3259         notLong, notFloat, notObj;
3260   Label Done;
3261 
3262   const Register bc    = LP64_ONLY(c_rarg3) NOT_LP64(rcx);
3263 
3264   // Test TOS state
3265   __ testl(tos_state, tos_state);
3266   __ jcc(Assembler::notZero, notByte);
3267 
3268   // btos
3269   {
3270     __ pop(btos);
3271     if (!is_static) pop_and_check_object(obj);
3272     __ access_store_at(T_BYTE, IN_HEAP, field, rax, noreg, noreg, noreg);
3273     if (!is_static && rc == may_rewrite) {
3274       patch_bytecode(Bytecodes::_fast_bputfield, bc, rbx, true, byte_no);
3275     }
3276     __ jmp(Done);
3277   }
3278 
3279   __ bind(notByte);
3280   __ cmpl(tos_state, ztos);
3281   __ jcc(Assembler::notEqual, notBool);
3282 
3283   // ztos
3284   {
3285     __ pop(ztos);
3286     if (!is_static) pop_and_check_object(obj);
3287     __ access_store_at(T_BOOLEAN, IN_HEAP, field, rax, noreg, noreg, noreg);
3288     if (!is_static && rc == may_rewrite) {
3289       patch_bytecode(Bytecodes::_fast_zputfield, bc, rbx, true, byte_no);
3290     }
3291     __ jmp(Done);
3292   }
3293 
3294   __ bind(notBool);
3295   __ cmpl(tos_state, atos);
3296   __ jcc(Assembler::notEqual, notObj);
3297 
3298   // atos
3299   {
3300     __ pop(atos);
3301     if (!is_static) pop_and_check_object(obj);
3302     // Store into the field
3303     do_oop_store(_masm, field, rax);
3304     if (!is_static && rc == may_rewrite) {
3305       patch_bytecode(Bytecodes::_fast_aputfield, bc, rbx, true, byte_no);
3306     }
3307     __ jmp(Done);
3308   }
3309 
3310   __ bind(notObj);
3311   __ cmpl(tos_state, itos);
3312   __ jcc(Assembler::notEqual, notInt);
3313 
3314   // itos
3315   {
3316     __ pop(itos);
3317     if (!is_static) pop_and_check_object(obj);
3318     __ access_store_at(T_INT, IN_HEAP, field, rax, noreg, noreg, noreg);
3319     if (!is_static && rc == may_rewrite) {
3320       patch_bytecode(Bytecodes::_fast_iputfield, bc, rbx, true, byte_no);
3321     }
3322     __ jmp(Done);
3323   }
3324 
3325   __ bind(notInt);
3326   __ cmpl(tos_state, ctos);
3327   __ jcc(Assembler::notEqual, notChar);
3328 
3329   // ctos
3330   {
3331     __ pop(ctos);
3332     if (!is_static) pop_and_check_object(obj);
3333     __ access_store_at(T_CHAR, IN_HEAP, field, rax, noreg, noreg, noreg);
3334     if (!is_static && rc == may_rewrite) {
3335       patch_bytecode(Bytecodes::_fast_cputfield, bc, rbx, true, byte_no);
3336     }
3337     __ jmp(Done);
3338   }
3339 
3340   __ bind(notChar);
3341   __ cmpl(tos_state, stos);
3342   __ jcc(Assembler::notEqual, notShort);
3343 
3344   // stos
3345   {
3346     __ pop(stos);
3347     if (!is_static) pop_and_check_object(obj);
3348     __ access_store_at(T_SHORT, IN_HEAP, field, rax, noreg, noreg, noreg);
3349     if (!is_static && rc == may_rewrite) {
3350       patch_bytecode(Bytecodes::_fast_sputfield, bc, rbx, true, byte_no);
3351     }
3352     __ jmp(Done);
3353   }
3354 
3355   __ bind(notShort);
3356   __ cmpl(tos_state, ltos);
3357   __ jcc(Assembler::notEqual, notLong);
3358 
3359   // ltos
3360   {
3361     __ pop(ltos);
3362     if (!is_static) pop_and_check_object(obj);
3363     // MO_RELAXED: generate atomic store for the case of volatile field (important for x86_32)
3364     __ access_store_at(T_LONG, IN_HEAP | MO_RELAXED, field, noreg /* ltos*/, noreg, noreg, noreg);
3365 #ifdef _LP64
3366     if (!is_static && rc == may_rewrite) {
3367       patch_bytecode(Bytecodes::_fast_lputfield, bc, rbx, true, byte_no);
3368     }
3369 #endif // _LP64
3370     __ jmp(Done);
3371   }
3372 
3373   __ bind(notLong);
3374   __ cmpl(tos_state, ftos);
3375   __ jcc(Assembler::notEqual, notFloat);
3376 
3377   // ftos
3378   {
3379     __ pop(ftos);
3380     if (!is_static) pop_and_check_object(obj);
3381     __ access_store_at(T_FLOAT, IN_HEAP, field, noreg /* ftos */, noreg, noreg, noreg);
3382     if (!is_static && rc == may_rewrite) {
3383       patch_bytecode(Bytecodes::_fast_fputfield, bc, rbx, true, byte_no);
3384     }
3385     __ jmp(Done);
3386   }
3387 
3388   __ bind(notFloat);
3389 #ifdef ASSERT
3390   Label notDouble;
3391   __ cmpl(tos_state, dtos);
3392   __ jcc(Assembler::notEqual, notDouble);
3393 #endif
3394 
3395   // dtos
3396   {
3397     __ pop(dtos);
3398     if (!is_static) pop_and_check_object(obj);
3399     // MO_RELAXED: for the case of volatile field, in fact it adds no extra work for the underlying implementation
3400     __ access_store_at(T_DOUBLE, IN_HEAP | MO_RELAXED, field, noreg /* dtos */, noreg, noreg, noreg);
3401     if (!is_static && rc == may_rewrite) {
3402       patch_bytecode(Bytecodes::_fast_dputfield, bc, rbx, true, byte_no);
3403     }
3404   }
3405 
3406 #ifdef ASSERT
3407   __ jmp(Done);
3408 
3409   __ bind(notDouble);
3410   __ stop("Bad state");
3411 #endif
3412 
3413   __ bind(Done);
3414 }
3415 
3416 void TemplateTable::putfield(int byte_no) {
3417   putfield_or_static(byte_no, false);
3418 }
3419 
3420 void TemplateTable::nofast_putfield(int byte_no) {
3421   putfield_or_static(byte_no, false, may_not_rewrite);
3422 }
3423 
3424 void TemplateTable::putstatic(int byte_no) {
3425   putfield_or_static(byte_no, true);
3426 }
3427 
3428 void TemplateTable::jvmti_post_fast_field_mod() {
3429 
3430   const Register scratch = LP64_ONLY(c_rarg3) NOT_LP64(rcx);
3431 
3432   if (JvmtiExport::can_post_field_modification()) {
3433     // Check to see if a field modification watch has been set before
3434     // we take the time to call into the VM.
3435     Label L2;
3436     __ mov32(scratch, ExternalAddress((address)JvmtiExport::get_field_modification_count_addr()));
3437     __ testl(scratch, scratch);
3438     __ jcc(Assembler::zero, L2);
3439     __ pop_ptr(rbx);                  // copy the object pointer from tos
3440     __ verify_oop(rbx);
3441     __ push_ptr(rbx);                 // put the object pointer back on tos
3442     // Save tos values before call_VM() clobbers them. Since we have
3443     // to do it for every data type, we use the saved values as the
3444     // jvalue object.
3445     switch (bytecode()) {          // load values into the jvalue object
3446     case Bytecodes::_fast_aputfield: __ push_ptr(rax); break;
3447     case Bytecodes::_fast_bputfield: // fall through
3448     case Bytecodes::_fast_zputfield: // fall through
3449     case Bytecodes::_fast_sputfield: // fall through
3450     case Bytecodes::_fast_cputfield: // fall through
3451     case Bytecodes::_fast_iputfield: __ push_i(rax); break;
3452     case Bytecodes::_fast_dputfield: __ push(dtos); break;
3453     case Bytecodes::_fast_fputfield: __ push(ftos); break;
3454     case Bytecodes::_fast_lputfield: __ push_l(rax); break;
3455 
3456     default:
3457       ShouldNotReachHere();
3458     }
3459     __ mov(scratch, rsp);             // points to jvalue on the stack
3460     // access constant pool cache entry
3461     LP64_ONLY(__ load_field_entry(c_rarg2, rax));
3462     NOT_LP64(__ load_field_entry(rax, rdx));
3463     __ verify_oop(rbx);
3464     // rbx: object pointer copied above
3465     // c_rarg2: cache entry pointer
3466     // c_rarg3: jvalue object on the stack
3467     LP64_ONLY(__ call_VM(noreg, CAST_FROM_FN_PTR(address, InterpreterRuntime::post_field_modification), rbx, c_rarg2, c_rarg3));
3468     NOT_LP64(__ call_VM(noreg, CAST_FROM_FN_PTR(address, InterpreterRuntime::post_field_modification), rbx, rax, rcx));
3469 
3470     switch (bytecode()) {             // restore tos values
3471     case Bytecodes::_fast_aputfield: __ pop_ptr(rax); break;
3472     case Bytecodes::_fast_bputfield: // fall through
3473     case Bytecodes::_fast_zputfield: // fall through
3474     case Bytecodes::_fast_sputfield: // fall through
3475     case Bytecodes::_fast_cputfield: // fall through
3476     case Bytecodes::_fast_iputfield: __ pop_i(rax); break;
3477     case Bytecodes::_fast_dputfield: __ pop(dtos); break;
3478     case Bytecodes::_fast_fputfield: __ pop(ftos); break;
3479     case Bytecodes::_fast_lputfield: __ pop_l(rax); break;
3480     default: break;
3481     }
3482     __ bind(L2);
3483   }
3484 }
3485 
3486 void TemplateTable::fast_storefield(TosState state) {
3487   transition(state, vtos);
3488 
3489   Register cache = rcx;
3490 
3491   Label notVolatile, Done;
3492 
3493   jvmti_post_fast_field_mod();
3494 
3495   __ push(rax);
3496   __ load_field_entry(rcx, rax);
3497   load_resolved_field_entry(noreg, cache, rax, rbx, rdx);
3498   // RBX: field offset, RCX: RAX: TOS, RDX: flags
3499   __ andl(rdx, (1 << ResolvedFieldEntry::is_volatile_shift));
3500   __ pop(rax);
3501 
3502   // Get object from stack
3503   pop_and_check_object(rcx);
3504 
3505   // field address
3506   const Address field(rcx, rbx, Address::times_1);
3507 
3508   // Check for volatile store
3509   __ testl(rdx, rdx);
3510   __ jcc(Assembler::zero, notVolatile);
3511 
3512   fast_storefield_helper(field, rax);
3513   volatile_barrier(Assembler::Membar_mask_bits(Assembler::StoreLoad |
3514                                                Assembler::StoreStore));
3515   __ jmp(Done);
3516   __ bind(notVolatile);
3517 
3518   fast_storefield_helper(field, rax);
3519 
3520   __ bind(Done);
3521 }
3522 
3523 void TemplateTable::fast_storefield_helper(Address field, Register rax) {
3524 
3525   // access field
3526   switch (bytecode()) {
3527   case Bytecodes::_fast_aputfield:
3528     do_oop_store(_masm, field, rax);
3529     break;
3530   case Bytecodes::_fast_lputfield:
3531 #ifdef _LP64
3532     __ access_store_at(T_LONG, IN_HEAP, field, noreg /* ltos */, noreg, noreg, noreg);
3533 #else
3534   __ stop("should not be rewritten");
3535 #endif
3536     break;
3537   case Bytecodes::_fast_iputfield:
3538     __ access_store_at(T_INT, IN_HEAP, field, rax, noreg, noreg, noreg);
3539     break;
3540   case Bytecodes::_fast_zputfield:
3541     __ access_store_at(T_BOOLEAN, IN_HEAP, field, rax, noreg, noreg, noreg);
3542     break;
3543   case Bytecodes::_fast_bputfield:
3544     __ access_store_at(T_BYTE, IN_HEAP, field, rax, noreg, noreg, noreg);
3545     break;
3546   case Bytecodes::_fast_sputfield:
3547     __ access_store_at(T_SHORT, IN_HEAP, field, rax, noreg, noreg, noreg);
3548     break;
3549   case Bytecodes::_fast_cputfield:
3550     __ access_store_at(T_CHAR, IN_HEAP, field, rax, noreg, noreg, noreg);
3551     break;
3552   case Bytecodes::_fast_fputfield:
3553     __ access_store_at(T_FLOAT, IN_HEAP, field, noreg /* ftos*/, noreg, noreg, noreg);
3554     break;
3555   case Bytecodes::_fast_dputfield:
3556     __ access_store_at(T_DOUBLE, IN_HEAP, field, noreg /* dtos*/, noreg, noreg, noreg);
3557     break;
3558   default:
3559     ShouldNotReachHere();
3560   }
3561 }
3562 
3563 void TemplateTable::fast_accessfield(TosState state) {
3564   transition(atos, state);
3565 
3566   // Do the JVMTI work here to avoid disturbing the register state below
3567   if (JvmtiExport::can_post_field_access()) {
3568     // Check to see if a field access watch has been set before we
3569     // take the time to call into the VM.
3570     Label L1;
3571     __ mov32(rcx, ExternalAddress((address) JvmtiExport::get_field_access_count_addr()));
3572     __ testl(rcx, rcx);
3573     __ jcc(Assembler::zero, L1);
3574     // access constant pool cache entry
3575     LP64_ONLY(__ load_field_entry(c_rarg2, rcx));
3576     NOT_LP64(__ load_field_entry(rcx, rdx));
3577     __ verify_oop(rax);
3578     __ push_ptr(rax);  // save object pointer before call_VM() clobbers it
3579     LP64_ONLY(__ mov(c_rarg1, rax));
3580     // c_rarg1: object pointer copied above
3581     // c_rarg2: cache entry pointer
3582     LP64_ONLY(__ call_VM(noreg, CAST_FROM_FN_PTR(address, InterpreterRuntime::post_field_access), c_rarg1, c_rarg2));
3583     NOT_LP64(__ call_VM(noreg, CAST_FROM_FN_PTR(address, InterpreterRuntime::post_field_access), rax, rcx));
3584     __ pop_ptr(rax); // restore object pointer
3585     __ bind(L1);
3586   }
3587 
3588   // access constant pool cache
3589   __ load_field_entry(rcx, rbx);
3590   __ load_sized_value(rbx, Address(rcx, in_bytes(ResolvedFieldEntry::field_offset_offset())), sizeof(int), true /*is_signed*/);
3591 
3592   // rax: object
3593   __ verify_oop(rax);
3594   __ null_check(rax);
3595   Address field(rax, rbx, Address::times_1);
3596 
3597   // access field
3598   switch (bytecode()) {
3599   case Bytecodes::_fast_agetfield:
3600     do_oop_load(_masm, field, rax);
3601     __ verify_oop(rax);
3602     break;
3603   case Bytecodes::_fast_lgetfield:
3604 #ifdef _LP64
3605     __ access_load_at(T_LONG, IN_HEAP, noreg /* ltos */, field, noreg, noreg);
3606 #else
3607   __ stop("should not be rewritten");
3608 #endif
3609     break;
3610   case Bytecodes::_fast_igetfield:
3611     __ access_load_at(T_INT, IN_HEAP, rax, field, noreg, noreg);
3612     break;
3613   case Bytecodes::_fast_bgetfield:
3614     __ access_load_at(T_BYTE, IN_HEAP, rax, field, noreg, noreg);
3615     break;
3616   case Bytecodes::_fast_sgetfield:
3617     __ access_load_at(T_SHORT, IN_HEAP, rax, field, noreg, noreg);
3618     break;
3619   case Bytecodes::_fast_cgetfield:
3620     __ access_load_at(T_CHAR, IN_HEAP, rax, field, noreg, noreg);
3621     break;
3622   case Bytecodes::_fast_fgetfield:
3623     __ access_load_at(T_FLOAT, IN_HEAP, noreg /* ftos */, field, noreg, noreg);
3624     break;
3625   case Bytecodes::_fast_dgetfield:
3626     __ access_load_at(T_DOUBLE, IN_HEAP, noreg /* dtos */, field, noreg, noreg);
3627     break;
3628   default:
3629     ShouldNotReachHere();
3630   }
3631   // [jk] not needed currently
3632   //   Label notVolatile;
3633   //   __ testl(rdx, rdx);
3634   //   __ jcc(Assembler::zero, notVolatile);
3635   //   __ membar(Assembler::LoadLoad);
3636   //   __ bind(notVolatile);
3637 }
3638 
3639 void TemplateTable::fast_xaccess(TosState state) {
3640   transition(vtos, state);
3641 
3642   // get receiver
3643   __ movptr(rax, aaddress(0));
3644   // access constant pool cache
3645   __ load_field_entry(rcx, rdx, 2);
3646   __ load_sized_value(rbx, Address(rcx, in_bytes(ResolvedFieldEntry::field_offset_offset())), sizeof(int), true /*is_signed*/);
3647 
3648   // make sure exception is reported in correct bcp range (getfield is
3649   // next instruction)
3650   __ increment(rbcp);
3651   __ null_check(rax);
3652   const Address field = Address(rax, rbx, Address::times_1, 0*wordSize);
3653   switch (state) {
3654   case itos:
3655     __ access_load_at(T_INT, IN_HEAP, rax, field, noreg, noreg);
3656     break;
3657   case atos:
3658     do_oop_load(_masm, field, rax);
3659     __ verify_oop(rax);
3660     break;
3661   case ftos:
3662     __ access_load_at(T_FLOAT, IN_HEAP, noreg /* ftos */, field, noreg, noreg);
3663     break;
3664   default:
3665     ShouldNotReachHere();
3666   }
3667 
3668   // [jk] not needed currently
3669   // Label notVolatile;
3670   // __ movl(rdx, Address(rcx, rdx, Address::times_8,
3671   //                      in_bytes(ConstantPoolCache::base_offset() +
3672   //                               ConstantPoolCacheEntry::flags_offset())));
3673   // __ shrl(rdx, ConstantPoolCacheEntry::is_volatile_shift);
3674   // __ testl(rdx, 0x1);
3675   // __ jcc(Assembler::zero, notVolatile);
3676   // __ membar(Assembler::LoadLoad);
3677   // __ bind(notVolatile);
3678 
3679   __ decrement(rbcp);
3680 }
3681 
3682 //-----------------------------------------------------------------------------
3683 // Calls
3684 
3685 void TemplateTable::prepare_invoke(Register cache, Register recv, Register flags) {
3686   // determine flags
3687   const Bytecodes::Code code = bytecode();
3688   const bool load_receiver       = (code != Bytecodes::_invokestatic) && (code != Bytecodes::_invokedynamic);
3689   assert_different_registers(recv, flags);
3690 
3691   // save 'interpreter return address'
3692   __ save_bcp();
3693 
3694   // Save flags and load TOS
3695   __ movl(rbcp, flags);
3696   __ load_unsigned_byte(flags, Address(cache, in_bytes(ResolvedMethodEntry::type_offset())));
3697 
3698   // load receiver if needed (after appendix is pushed so parameter size is correct)
3699   // Note: no return address pushed yet
3700   if (load_receiver) {
3701     __ load_unsigned_short(recv, Address(cache, in_bytes(ResolvedMethodEntry::num_parameters_offset())));
3702     const int no_return_pc_pushed_yet = -1;  // argument slot correction before we push return address
3703     const int receiver_is_at_end      = -1;  // back off one slot to get receiver
3704     Address recv_addr = __ argument_address(recv, no_return_pc_pushed_yet + receiver_is_at_end);
3705     __ movptr(recv, recv_addr);
3706     __ verify_oop(recv);
3707   }
3708 
3709   // load return address
3710   {
3711     const address table_addr = (address) Interpreter::invoke_return_entry_table_for(code);
3712     ExternalAddress table(table_addr);
3713 #ifdef _LP64
3714     __ lea(rscratch1, table);
3715     __ movptr(flags, Address(rscratch1, flags, Address::times_ptr));
3716 #else
3717     __ movptr(flags, ArrayAddress(table, Address(noreg, flags, Address::times_ptr)));
3718 #endif // _LP64
3719   }
3720 
3721   // push return address
3722   __ push(flags);
3723 
3724   // Restore flags value from the constant pool cache entry, and restore rsi
3725   // for later null checks.  r13 is the bytecode pointer
3726   __ movl(flags, rbcp);
3727   __ restore_bcp();
3728 }
3729 
3730 void TemplateTable::invokevirtual_helper(Register index,
3731                                          Register recv,
3732                                          Register flags) {
3733   // Uses temporary registers rax, rdx
3734   assert_different_registers(index, recv, rax, rdx);
3735   assert(index == rbx, "");
3736   assert(recv  == rcx, "");
3737 
3738   // Test for an invoke of a final method
3739   Label notFinal;
3740   __ movl(rax, flags);
3741   __ andl(rax, (1 << ResolvedMethodEntry::is_vfinal_shift));
3742   __ jcc(Assembler::zero, notFinal);
3743 
3744   const Register method = index;  // method must be rbx
3745   assert(method == rbx,
3746          "Method* must be rbx for interpreter calling convention");
3747 
3748   // do the call - the index is actually the method to call
3749   // that is, f2 is a vtable index if !is_vfinal, else f2 is a Method*
3750 
3751   // It's final, need a null check here!
3752   __ null_check(recv);
3753 
3754   // profile this call
3755   __ profile_final_call(rax);
3756   __ profile_arguments_type(rax, method, rbcp, true);
3757 
3758   __ jump_from_interpreted(method, rax);
3759 
3760   __ bind(notFinal);
3761 
3762   // get receiver klass
3763   __ load_klass(rax, recv, rscratch1);
3764 
3765   // profile this call
3766   __ profile_virtual_call(rax, rlocals, rdx);
3767   // get target Method* & entry point
3768   __ lookup_virtual_method(rax, index, method);
3769 
3770   __ profile_arguments_type(rdx, method, rbcp, true);
3771   __ jump_from_interpreted(method, rdx);
3772 }
3773 
3774 void TemplateTable::invokevirtual(int byte_no) {
3775   transition(vtos, vtos);
3776   assert(byte_no == f2_byte, "use this argument");
3777 
3778   load_resolved_method_entry_virtual(rcx,  // ResolvedMethodEntry*
3779                                      rbx,  // Method or itable index
3780                                      rdx); // Flags
3781   prepare_invoke(rcx,  // ResolvedMethodEntry*
3782                  rcx,  // Receiver
3783                  rdx); // flags
3784 
3785   // rbx: index
3786   // rcx: receiver
3787   // rdx: flags
3788   invokevirtual_helper(rbx, rcx, rdx);
3789 }
3790 
3791 void TemplateTable::invokespecial(int byte_no) {
3792   transition(vtos, vtos);
3793   assert(byte_no == f1_byte, "use this argument");
3794 
3795   load_resolved_method_entry_special_or_static(rcx,  // ResolvedMethodEntry*
3796                                                rbx,  // Method*
3797                                                rdx); // flags
3798   prepare_invoke(rcx,
3799                  rcx,  // get receiver also for null check
3800                  rdx); // flags
3801 
3802   __ verify_oop(rcx);
3803   __ null_check(rcx);
3804   // do the call
3805   __ profile_call(rax);
3806   __ profile_arguments_type(rax, rbx, rbcp, false);
3807   __ jump_from_interpreted(rbx, rax);
3808 }
3809 
3810 void TemplateTable::invokestatic(int byte_no) {
3811   transition(vtos, vtos);
3812   assert(byte_no == f1_byte, "use this argument");
3813 
3814   load_resolved_method_entry_special_or_static(rcx, // ResolvedMethodEntry*
3815                                                rbx, // Method*
3816                                                rdx  // flags
3817                                                );
3818   prepare_invoke(rcx, rcx, rdx);  // cache and flags
3819 
3820   // do the call
3821   __ profile_call(rax);
3822   __ profile_arguments_type(rax, rbx, rbcp, false);
3823   __ jump_from_interpreted(rbx, rax);
3824 }
3825 
3826 
3827 void TemplateTable::fast_invokevfinal(int byte_no) {
3828   transition(vtos, vtos);
3829   assert(byte_no == f2_byte, "use this argument");
3830   __ stop("fast_invokevfinal not used on x86");
3831 }
3832 
3833 
3834 void TemplateTable::invokeinterface(int byte_no) {
3835   transition(vtos, vtos);
3836   assert(byte_no == f1_byte, "use this argument");
3837 
3838   load_resolved_method_entry_interface(rcx,  // ResolvedMethodEntry*
3839                                        rax,  // Klass*
3840                                        rbx,  // Method* or itable/vtable index
3841                                        rdx); // flags
3842   prepare_invoke(rcx, rcx, rdx); // receiver, flags
3843 
3844   // First check for Object case, then private interface method,
3845   // then regular interface method.
3846 
3847   // Special case of invokeinterface called for virtual method of
3848   // java.lang.Object.  See cpCache.cpp for details.
3849   Label notObjectMethod;
3850   __ movl(rlocals, rdx);
3851   __ andl(rlocals, (1 << ResolvedMethodEntry::is_forced_virtual_shift));
3852   __ jcc(Assembler::zero, notObjectMethod);
3853 
3854   invokevirtual_helper(rbx, rcx, rdx);
3855   // no return from above
3856   __ bind(notObjectMethod);
3857 
3858   Label no_such_interface; // for receiver subtype check
3859   Register recvKlass; // used for exception processing
3860 
3861   // Check for private method invocation - indicated by vfinal
3862   Label notVFinal;
3863   __ movl(rlocals, rdx);
3864   __ andl(rlocals, (1 << ResolvedMethodEntry::is_vfinal_shift));
3865   __ jcc(Assembler::zero, notVFinal);
3866 
3867   // Get receiver klass into rlocals - also a null check
3868   __ load_klass(rlocals, rcx, rscratch1);
3869 
3870   Label subtype;
3871   __ check_klass_subtype(rlocals, rax, rbcp, subtype);
3872   // If we get here the typecheck failed
3873   recvKlass = rdx;
3874   __ mov(recvKlass, rlocals); // shuffle receiver class for exception use
3875   __ jmp(no_such_interface);
3876 
3877   __ bind(subtype);
3878 
3879   // do the call - rbx is actually the method to call
3880 
3881   __ profile_final_call(rdx);
3882   __ profile_arguments_type(rdx, rbx, rbcp, true);
3883 
3884   __ jump_from_interpreted(rbx, rdx);
3885   // no return from above
3886   __ bind(notVFinal);
3887 
3888   // Get receiver klass into rdx - also a null check
3889   __ restore_locals();  // restore r14
3890   __ load_klass(rdx, rcx, rscratch1);
3891 
3892   Label no_such_method;
3893 
3894   // Preserve method for throw_AbstractMethodErrorVerbose.
3895   __ mov(rcx, rbx);
3896   // Receiver subtype check against REFC.
3897   // Superklass in rax. Subklass in rdx. Blows rcx, rdi.
3898   __ lookup_interface_method(// inputs: rec. class, interface, itable index
3899                              rdx, rax, noreg,
3900                              // outputs: scan temp. reg, scan temp. reg
3901                              rbcp, rlocals,
3902                              no_such_interface,
3903                              /*return_method=*/false);
3904 
3905   // profile this call
3906   __ restore_bcp(); // rbcp was destroyed by receiver type check
3907   __ profile_virtual_call(rdx, rbcp, rlocals);
3908 
3909   // Get declaring interface class from method, and itable index
3910   __ load_method_holder(rax, rbx);
3911   __ movl(rbx, Address(rbx, Method::itable_index_offset()));
3912   __ subl(rbx, Method::itable_index_max);
3913   __ negl(rbx);
3914 
3915   // Preserve recvKlass for throw_AbstractMethodErrorVerbose.
3916   __ mov(rlocals, rdx);
3917   __ lookup_interface_method(// inputs: rec. class, interface, itable index
3918                              rlocals, rax, rbx,
3919                              // outputs: method, scan temp. reg
3920                              rbx, rbcp,
3921                              no_such_interface);
3922 
3923   // rbx: Method* to call
3924   // rcx: receiver
3925   // Check for abstract method error
3926   // Note: This should be done more efficiently via a throw_abstract_method_error
3927   //       interpreter entry point and a conditional jump to it in case of a null
3928   //       method.
3929   __ testptr(rbx, rbx);
3930   __ jcc(Assembler::zero, no_such_method);
3931 
3932   __ profile_arguments_type(rdx, rbx, rbcp, true);
3933 
3934   // do the call
3935   // rcx: receiver
3936   // rbx,: Method*
3937   __ jump_from_interpreted(rbx, rdx);
3938   __ should_not_reach_here();
3939 
3940   // exception handling code follows...
3941   // note: must restore interpreter registers to canonical
3942   //       state for exception handling to work correctly!
3943 
3944   __ bind(no_such_method);
3945   // throw exception
3946   __ pop(rbx);           // pop return address (pushed by prepare_invoke)
3947   __ restore_bcp();      // rbcp must be correct for exception handler   (was destroyed)
3948   __ restore_locals();   // make sure locals pointer is correct as well (was destroyed)
3949   // Pass arguments for generating a verbose error message.
3950 #ifdef _LP64
3951   recvKlass = c_rarg1;
3952   Register method    = c_rarg2;
3953   if (recvKlass != rdx) { __ movq(recvKlass, rdx); }
3954   if (method != rcx)    { __ movq(method, rcx);    }
3955 #else
3956   recvKlass = rdx;
3957   Register method    = rcx;
3958 #endif
3959   __ call_VM(noreg, CAST_FROM_FN_PTR(address, InterpreterRuntime::throw_AbstractMethodErrorVerbose),
3960              recvKlass, method);
3961   // The call_VM checks for exception, so we should never return here.
3962   __ should_not_reach_here();
3963 
3964   __ bind(no_such_interface);
3965   // throw exception
3966   __ pop(rbx);           // pop return address (pushed by prepare_invoke)
3967   __ restore_bcp();      // rbcp must be correct for exception handler   (was destroyed)
3968   __ restore_locals();   // make sure locals pointer is correct as well (was destroyed)
3969   // Pass arguments for generating a verbose error message.
3970   LP64_ONLY( if (recvKlass != rdx) { __ movq(recvKlass, rdx); } )
3971   __ call_VM(noreg, CAST_FROM_FN_PTR(address, InterpreterRuntime::throw_IncompatibleClassChangeErrorVerbose),
3972              recvKlass, rax);
3973   // the call_VM checks for exception, so we should never return here.
3974   __ should_not_reach_here();
3975 }
3976 
3977 void TemplateTable::invokehandle(int byte_no) {
3978   transition(vtos, vtos);
3979   assert(byte_no == f1_byte, "use this argument");
3980   const Register rbx_method = rbx;
3981   const Register rax_mtype  = rax;
3982   const Register rcx_recv   = rcx;
3983   const Register rdx_flags  = rdx;
3984 
3985   load_resolved_method_entry_handle(rcx, rbx_method, rax_mtype, rdx_flags);
3986   prepare_invoke(rcx, rcx_recv, rdx_flags);
3987 
3988   __ verify_method_ptr(rbx_method);
3989   __ verify_oop(rcx_recv);
3990   __ null_check(rcx_recv);
3991 
3992   // rax: MethodType object (from cpool->resolved_references[f1], if necessary)
3993   // rbx: MH.invokeExact_MT method
3994 
3995   // Note:  rax_mtype is already pushed (if necessary)
3996 
3997   // FIXME: profile the LambdaForm also
3998   __ profile_final_call(rax);
3999   __ profile_arguments_type(rdx, rbx_method, rbcp, true);
4000 
4001   __ jump_from_interpreted(rbx_method, rdx);
4002 }
4003 
4004 void TemplateTable::invokedynamic(int byte_no) {
4005   transition(vtos, vtos);
4006   assert(byte_no == f1_byte, "use this argument");
4007 
4008   const Register rbx_method   = rbx;
4009   const Register rax_callsite = rax;
4010 
4011   load_invokedynamic_entry(rbx_method);
4012   // rax: CallSite object (from cpool->resolved_references[])
4013   // rbx: MH.linkToCallSite method
4014 
4015   // Note:  rax_callsite is already pushed
4016 
4017   // %%% should make a type profile for any invokedynamic that takes a ref argument
4018   // profile this call
4019   __ profile_call(rbcp);
4020   __ profile_arguments_type(rdx, rbx_method, rbcp, false);
4021 
4022   __ verify_oop(rax_callsite);
4023 
4024   __ jump_from_interpreted(rbx_method, rdx);
4025 }
4026 
4027 //-----------------------------------------------------------------------------
4028 // Allocation
4029 
4030 void TemplateTable::_new() {
4031   transition(vtos, atos);
4032   __ get_unsigned_2_byte_index_at_bcp(rdx, 1);
4033   Label slow_case;
4034   Label slow_case_no_pop;
4035   Label done;
4036   Label initialize_header;
4037 
4038   __ get_cpool_and_tags(rcx, rax);
4039 
4040   // Make sure the class we're about to instantiate has been resolved.
4041   // This is done before loading InstanceKlass to be consistent with the order
4042   // how Constant Pool is updated (see ConstantPool::klass_at_put)
4043   const int tags_offset = Array<u1>::base_offset_in_bytes();
4044   __ cmpb(Address(rax, rdx, Address::times_1, tags_offset), JVM_CONSTANT_Class);
4045   __ jcc(Assembler::notEqual, slow_case_no_pop);
4046 
4047   // get InstanceKlass
4048   __ load_resolved_klass_at_index(rcx, rcx, rdx);
4049   __ push(rcx);  // save the contexts of klass for initializing the header
4050 
4051   // make sure klass is initialized
4052 #ifdef _LP64
4053   assert(VM_Version::supports_fast_class_init_checks(), "must support fast class initialization checks");
4054   __ clinit_barrier(rcx, r15_thread, nullptr /*L_fast_path*/, &slow_case);
4055 #else
4056   __ cmpb(Address(rcx, InstanceKlass::init_state_offset()), InstanceKlass::fully_initialized);
4057   __ jcc(Assembler::notEqual, slow_case);
4058 #endif
4059 
4060   // make sure klass doesn't have finalizer
4061   // get instance_size in InstanceKlass (scaled to a count of bytes)
4062   __ movl(rdx, Address(rcx, Klass::layout_helper_offset()));
4063   // test to see if it has a finalizer or is malformed in some way
4064   __ testl(rdx, Klass::_lh_instance_slow_path_bit);
4065   __ jcc(Assembler::notZero, slow_case);
4066 
4067   // Allocate the instance:
4068   //  If TLAB is enabled:
4069   //    Try to allocate in the TLAB.
4070   //    If fails, go to the slow path.
4071   //    Initialize the allocation.
4072   //    Exit.
4073   //
4074   //  Go to slow path.
4075 
4076   const Register thread = LP64_ONLY(r15_thread) NOT_LP64(rcx);
4077 
4078   if (UseTLAB) {
4079     NOT_LP64(__ get_thread(thread);)
4080     __ tlab_allocate(thread, rax, rdx, 0, rcx, rbx, slow_case);
4081     if (ZeroTLAB) {
4082       // the fields have been already cleared
4083       __ jmp(initialize_header);
4084     }
4085 
4086     // The object is initialized before the header.  If the object size is
4087     // zero, go directly to the header initialization.
4088     if (UseCompactObjectHeaders) {
4089       assert(is_aligned(oopDesc::base_offset_in_bytes(), BytesPerLong), "oop base offset must be 8-byte-aligned");
4090       __ decrement(rdx, oopDesc::base_offset_in_bytes());
4091     } else {
4092       __ decrement(rdx, sizeof(oopDesc));
4093     }
4094     __ jcc(Assembler::zero, initialize_header);
4095 
4096     // Initialize topmost object field, divide rdx by 8, check if odd and
4097     // test if zero.
4098     __ xorl(rcx, rcx);    // use zero reg to clear memory (shorter code)
4099     __ shrl(rdx, LogBytesPerLong); // divide by 2*oopSize and set carry flag if odd
4100 
4101     // rdx must have been multiple of 8
4102 #ifdef ASSERT
4103     // make sure rdx was multiple of 8
4104     Label L;
4105     // Ignore partial flag stall after shrl() since it is debug VM
4106     __ jcc(Assembler::carryClear, L);
4107     __ stop("object size is not multiple of 2 - adjust this code");
4108     __ bind(L);
4109     // rdx must be > 0, no extra check needed here
4110 #endif
4111 
4112     // initialize remaining object fields: rdx was a multiple of 8
4113     { Label loop;
4114     __ bind(loop);
4115     if (UseCompactObjectHeaders) {
4116       assert(is_aligned(oopDesc::base_offset_in_bytes(), BytesPerLong), "oop base offset must be 8-byte-aligned");
4117       int header_size = oopDesc::base_offset_in_bytes();
4118       __ movptr(Address(rax, rdx, Address::times_8, header_size - 1*oopSize), rcx);
4119       NOT_LP64(__ movptr(Address(rax, rdx, Address::times_8, header_size - 2*oopSize), rcx));
4120     } else {
4121       __ movptr(Address(rax, rdx, Address::times_8, sizeof(oopDesc) - 1*oopSize), rcx);
4122       NOT_LP64(__ movptr(Address(rax, rdx, Address::times_8, sizeof(oopDesc) - 2*oopSize), rcx));
4123     }
4124     __ decrement(rdx);
4125     __ jcc(Assembler::notZero, loop);
4126     }
4127 
4128     // initialize object header only.
4129     __ bind(initialize_header);
4130     if (UseCompactObjectHeaders) {
4131       __ pop(rcx);   // get saved klass back in the register.
4132       __ movptr(rbx, Address(rcx, Klass::prototype_header_offset()));
4133       __ movptr(Address(rax, oopDesc::mark_offset_in_bytes ()), rbx);
4134     } else {
4135       __ movptr(Address(rax, oopDesc::mark_offset_in_bytes()),
4136                 (intptr_t)markWord::prototype().value()); // header
4137       __ pop(rcx);   // get saved klass back in the register.
4138 #ifdef _LP64
4139       __ xorl(rsi, rsi); // use zero reg to clear memory (shorter code)
4140       __ store_klass_gap(rax, rsi);  // zero klass gap for compressed oops
4141 #endif
4142       __ store_klass(rax, rcx, rscratch1);  // klass
4143     }
4144 
4145     {
4146       SkipIfEqual skip_if(_masm, &DTraceAllocProbes, 0, rscratch1);
4147       // Trigger dtrace event for fastpath
4148       __ push(atos);
4149       __ call_VM_leaf(
4150            CAST_FROM_FN_PTR(address, static_cast<int (*)(oopDesc*)>(SharedRuntime::dtrace_object_alloc)), rax);
4151       __ pop(atos);
4152     }
4153 
4154     __ jmp(done);
4155   }
4156 
4157   // slow case
4158   __ bind(slow_case);
4159   __ pop(rcx);   // restore stack pointer to what it was when we came in.
4160   __ bind(slow_case_no_pop);
4161 
4162   Register rarg1 = LP64_ONLY(c_rarg1) NOT_LP64(rax);
4163   Register rarg2 = LP64_ONLY(c_rarg2) NOT_LP64(rdx);
4164 
4165   __ get_constant_pool(rarg1);
4166   __ get_unsigned_2_byte_index_at_bcp(rarg2, 1);
4167   call_VM(rax, CAST_FROM_FN_PTR(address, InterpreterRuntime::_new), rarg1, rarg2);
4168    __ verify_oop(rax);
4169 
4170   // continue
4171   __ bind(done);
4172 }
4173 
4174 void TemplateTable::newarray() {
4175   transition(itos, atos);
4176   Register rarg1 = LP64_ONLY(c_rarg1) NOT_LP64(rdx);
4177   __ load_unsigned_byte(rarg1, at_bcp(1));
4178   call_VM(rax, CAST_FROM_FN_PTR(address, InterpreterRuntime::newarray),
4179           rarg1, rax);
4180 }
4181 
4182 void TemplateTable::anewarray() {
4183   transition(itos, atos);
4184 
4185   Register rarg1 = LP64_ONLY(c_rarg1) NOT_LP64(rcx);
4186   Register rarg2 = LP64_ONLY(c_rarg2) NOT_LP64(rdx);
4187 
4188   __ get_unsigned_2_byte_index_at_bcp(rarg2, 1);
4189   __ get_constant_pool(rarg1);
4190   call_VM(rax, CAST_FROM_FN_PTR(address, InterpreterRuntime::anewarray),
4191           rarg1, rarg2, rax);
4192 }
4193 
4194 void TemplateTable::arraylength() {
4195   transition(atos, itos);
4196   __ movl(rax, Address(rax, arrayOopDesc::length_offset_in_bytes()));
4197 }
4198 
4199 void TemplateTable::checkcast() {
4200   transition(atos, atos);
4201   Label done, is_null, ok_is_subtype, quicked, resolved;
4202   __ testptr(rax, rax); // object is in rax
4203   __ jcc(Assembler::zero, is_null);
4204 
4205   // Get cpool & tags index
4206   __ get_cpool_and_tags(rcx, rdx); // rcx=cpool, rdx=tags array
4207   __ get_unsigned_2_byte_index_at_bcp(rbx, 1); // rbx=index
4208   // See if bytecode has already been quicked
4209   __ cmpb(Address(rdx, rbx,
4210                   Address::times_1,
4211                   Array<u1>::base_offset_in_bytes()),
4212           JVM_CONSTANT_Class);
4213   __ jcc(Assembler::equal, quicked);
4214   __ push(atos); // save receiver for result, and for GC
4215   call_VM(noreg, CAST_FROM_FN_PTR(address, InterpreterRuntime::quicken_io_cc));
4216 
4217   // vm_result_2 has metadata result
4218 #ifndef _LP64
4219   // borrow rdi from locals
4220   __ get_thread(rdi);
4221   __ get_vm_result_2(rax, rdi);
4222   __ restore_locals();
4223 #else
4224   __ get_vm_result_2(rax, r15_thread);
4225 #endif
4226 
4227   __ pop_ptr(rdx); // restore receiver
4228   __ jmpb(resolved);
4229 
4230   // Get superklass in rax and subklass in rbx
4231   __ bind(quicked);
4232   __ mov(rdx, rax); // Save object in rdx; rax needed for subtype check
4233   __ load_resolved_klass_at_index(rax, rcx, rbx);
4234 
4235   __ bind(resolved);
4236   __ load_klass(rbx, rdx, rscratch1);
4237 
4238   // Generate subtype check.  Blows rcx, rdi.  Object in rdx.
4239   // Superklass in rax.  Subklass in rbx.
4240   __ gen_subtype_check(rbx, ok_is_subtype);
4241 
4242   // Come here on failure
4243   __ push_ptr(rdx);
4244   // object is at TOS
4245   __ jump(ExternalAddress(Interpreter::_throw_ClassCastException_entry));
4246 
4247   // Come here on success
4248   __ bind(ok_is_subtype);
4249   __ mov(rax, rdx); // Restore object in rdx
4250 
4251   // Collect counts on whether this check-cast sees nulls a lot or not.
4252   if (ProfileInterpreter) {
4253     __ jmp(done);
4254     __ bind(is_null);
4255     __ profile_null_seen(rcx);
4256   } else {
4257     __ bind(is_null);   // same as 'done'
4258   }
4259   __ bind(done);
4260 }
4261 
4262 void TemplateTable::instanceof() {
4263   transition(atos, itos);
4264   Label done, is_null, ok_is_subtype, quicked, resolved;
4265   __ testptr(rax, rax);
4266   __ jcc(Assembler::zero, is_null);
4267 
4268   // Get cpool & tags index
4269   __ get_cpool_and_tags(rcx, rdx); // rcx=cpool, rdx=tags array
4270   __ get_unsigned_2_byte_index_at_bcp(rbx, 1); // rbx=index
4271   // See if bytecode has already been quicked
4272   __ cmpb(Address(rdx, rbx,
4273                   Address::times_1,
4274                   Array<u1>::base_offset_in_bytes()),
4275           JVM_CONSTANT_Class);
4276   __ jcc(Assembler::equal, quicked);
4277 
4278   __ push(atos); // save receiver for result, and for GC
4279   call_VM(noreg, CAST_FROM_FN_PTR(address, InterpreterRuntime::quicken_io_cc));
4280   // vm_result_2 has metadata result
4281 
4282 #ifndef _LP64
4283   // borrow rdi from locals
4284   __ get_thread(rdi);
4285   __ get_vm_result_2(rax, rdi);
4286   __ restore_locals();
4287 #else
4288   __ get_vm_result_2(rax, r15_thread);
4289 #endif
4290 
4291   __ pop_ptr(rdx); // restore receiver
4292   __ verify_oop(rdx);
4293   __ load_klass(rdx, rdx, rscratch1);
4294   __ jmpb(resolved);
4295 
4296   // Get superklass in rax and subklass in rdx
4297   __ bind(quicked);
4298   __ load_klass(rdx, rax, rscratch1);
4299   __ load_resolved_klass_at_index(rax, rcx, rbx);
4300 
4301   __ bind(resolved);
4302 
4303   // Generate subtype check.  Blows rcx, rdi
4304   // Superklass in rax.  Subklass in rdx.
4305   __ gen_subtype_check(rdx, ok_is_subtype);
4306 
4307   // Come here on failure
4308   __ xorl(rax, rax);
4309   __ jmpb(done);
4310   // Come here on success
4311   __ bind(ok_is_subtype);
4312   __ movl(rax, 1);
4313 
4314   // Collect counts on whether this test sees nulls a lot or not.
4315   if (ProfileInterpreter) {
4316     __ jmp(done);
4317     __ bind(is_null);
4318     __ profile_null_seen(rcx);
4319   } else {
4320     __ bind(is_null);   // same as 'done'
4321   }
4322   __ bind(done);
4323   // rax = 0: obj == nullptr or  obj is not an instanceof the specified klass
4324   // rax = 1: obj != nullptr and obj is     an instanceof the specified klass
4325 }
4326 
4327 
4328 //----------------------------------------------------------------------------------------------------
4329 // Breakpoints
4330 void TemplateTable::_breakpoint() {
4331   // Note: We get here even if we are single stepping..
4332   // jbug insists on setting breakpoints at every bytecode
4333   // even if we are in single step mode.
4334 
4335   transition(vtos, vtos);
4336 
4337   Register rarg = LP64_ONLY(c_rarg1) NOT_LP64(rcx);
4338 
4339   // get the unpatched byte code
4340   __ get_method(rarg);
4341   __ call_VM(noreg,
4342              CAST_FROM_FN_PTR(address,
4343                               InterpreterRuntime::get_original_bytecode_at),
4344              rarg, rbcp);
4345   __ mov(rbx, rax);  // why?
4346 
4347   // post the breakpoint event
4348   __ get_method(rarg);
4349   __ call_VM(noreg,
4350              CAST_FROM_FN_PTR(address, InterpreterRuntime::_breakpoint),
4351              rarg, rbcp);
4352 
4353   // complete the execution of original bytecode
4354   __ dispatch_only_normal(vtos);
4355 }
4356 
4357 //-----------------------------------------------------------------------------
4358 // Exceptions
4359 
4360 void TemplateTable::athrow() {
4361   transition(atos, vtos);
4362   __ null_check(rax);
4363   __ jump(ExternalAddress(Interpreter::throw_exception_entry()));
4364 }
4365 
4366 //-----------------------------------------------------------------------------
4367 // Synchronization
4368 //
4369 // Note: monitorenter & exit are symmetric routines; which is reflected
4370 //       in the assembly code structure as well
4371 //
4372 // Stack layout:
4373 //
4374 // [expressions  ] <--- rsp               = expression stack top
4375 // ..
4376 // [expressions  ]
4377 // [monitor entry] <--- monitor block top = expression stack bot
4378 // ..
4379 // [monitor entry]
4380 // [frame data   ] <--- monitor block bot
4381 // ...
4382 // [saved rbp    ] <--- rbp
4383 void TemplateTable::monitorenter() {
4384   transition(atos, vtos);
4385 
4386   // check for null object
4387   __ null_check(rax);
4388 
4389   const Address monitor_block_top(
4390         rbp, frame::interpreter_frame_monitor_block_top_offset * wordSize);
4391   const Address monitor_block_bot(
4392         rbp, frame::interpreter_frame_initial_sp_offset * wordSize);
4393   const int entry_size = frame::interpreter_frame_monitor_size_in_bytes();
4394 
4395   Label allocated;
4396 
4397   Register rtop = LP64_ONLY(c_rarg3) NOT_LP64(rcx);
4398   Register rbot = LP64_ONLY(c_rarg2) NOT_LP64(rbx);
4399   Register rmon = LP64_ONLY(c_rarg1) NOT_LP64(rdx);
4400 
4401   // initialize entry pointer
4402   __ xorl(rmon, rmon); // points to free slot or null
4403 
4404   // find a free slot in the monitor block (result in rmon)
4405   {
4406     Label entry, loop, exit;
4407     __ movptr(rtop, monitor_block_top); // derelativize pointer
4408     __ lea(rtop, Address(rbp, rtop, Address::times_ptr));
4409     // rtop points to current entry, starting with top-most entry
4410 
4411     __ lea(rbot, monitor_block_bot);    // points to word before bottom
4412                                         // of monitor block
4413     __ jmpb(entry);
4414 
4415     __ bind(loop);
4416     // check if current entry is used
4417     __ cmpptr(Address(rtop, BasicObjectLock::obj_offset()), NULL_WORD);
4418     // if not used then remember entry in rmon
4419     __ cmovptr(Assembler::equal, rmon, rtop);   // cmov => cmovptr
4420     // check if current entry is for same object
4421     __ cmpptr(rax, Address(rtop, BasicObjectLock::obj_offset()));
4422     // if same object then stop searching
4423     __ jccb(Assembler::equal, exit);
4424     // otherwise advance to next entry
4425     __ addptr(rtop, entry_size);
4426     __ bind(entry);
4427     // check if bottom reached
4428     __ cmpptr(rtop, rbot);
4429     // if not at bottom then check this entry
4430     __ jcc(Assembler::notEqual, loop);
4431     __ bind(exit);
4432   }
4433 
4434   __ testptr(rmon, rmon); // check if a slot has been found
4435   __ jcc(Assembler::notZero, allocated); // if found, continue with that one
4436 
4437   // allocate one if there's no free slot
4438   {
4439     Label entry, loop;
4440     // 1. compute new pointers          // rsp: old expression stack top
4441     __ movptr(rmon, monitor_block_bot); // rmon: old expression stack bottom
4442     __ lea(rmon, Address(rbp, rmon, Address::times_ptr));
4443     __ subptr(rsp, entry_size);         // move expression stack top
4444     __ subptr(rmon, entry_size);        // move expression stack bottom
4445     __ mov(rtop, rsp);                  // set start value for copy loop
4446     __ subptr(monitor_block_bot, entry_size / wordSize); // set new monitor block bottom
4447     __ jmp(entry);
4448     // 2. move expression stack contents
4449     __ bind(loop);
4450     __ movptr(rbot, Address(rtop, entry_size)); // load expression stack
4451                                                 // word from old location
4452     __ movptr(Address(rtop, 0), rbot);          // and store it at new location
4453     __ addptr(rtop, wordSize);                  // advance to next word
4454     __ bind(entry);
4455     __ cmpptr(rtop, rmon);                      // check if bottom reached
4456     __ jcc(Assembler::notEqual, loop);          // if not at bottom then
4457                                                 // copy next word
4458   }
4459 
4460   // call run-time routine
4461   // rmon: points to monitor entry
4462   __ bind(allocated);
4463 
4464   // Increment bcp to point to the next bytecode, so exception
4465   // handling for async. exceptions work correctly.
4466   // The object has already been popped from the stack, so the
4467   // expression stack looks correct.
4468   __ increment(rbcp);
4469 
4470   // store object
4471   __ movptr(Address(rmon, BasicObjectLock::obj_offset()), rax);
4472   __ lock_object(rmon);
4473 
4474   // check to make sure this monitor doesn't cause stack overflow after locking
4475   __ save_bcp();  // in case of exception
4476   __ generate_stack_overflow_check(0);
4477 
4478   // The bcp has already been incremented. Just need to dispatch to
4479   // next instruction.
4480   __ dispatch_next(vtos);
4481 }
4482 
4483 void TemplateTable::monitorexit() {
4484   transition(atos, vtos);
4485 
4486   // check for null object
4487   __ null_check(rax);
4488 
4489   const Address monitor_block_top(
4490         rbp, frame::interpreter_frame_monitor_block_top_offset * wordSize);
4491   const Address monitor_block_bot(
4492         rbp, frame::interpreter_frame_initial_sp_offset * wordSize);
4493   const int entry_size = frame::interpreter_frame_monitor_size_in_bytes();
4494 
4495   Register rtop = LP64_ONLY(c_rarg1) NOT_LP64(rdx);
4496   Register rbot = LP64_ONLY(c_rarg2) NOT_LP64(rbx);
4497 
4498   Label found;
4499 
4500   // find matching slot
4501   {
4502     Label entry, loop;
4503     __ movptr(rtop, monitor_block_top); // derelativize pointer
4504     __ lea(rtop, Address(rbp, rtop, Address::times_ptr));
4505     // rtop points to current entry, starting with top-most entry
4506 
4507     __ lea(rbot, monitor_block_bot);    // points to word before bottom
4508                                         // of monitor block
4509     __ jmpb(entry);
4510 
4511     __ bind(loop);
4512     // check if current entry is for same object
4513     __ cmpptr(rax, Address(rtop, BasicObjectLock::obj_offset()));
4514     // if same object then stop searching
4515     __ jcc(Assembler::equal, found);
4516     // otherwise advance to next entry
4517     __ addptr(rtop, entry_size);
4518     __ bind(entry);
4519     // check if bottom reached
4520     __ cmpptr(rtop, rbot);
4521     // if not at bottom then check this entry
4522     __ jcc(Assembler::notEqual, loop);
4523   }
4524 
4525   // error handling. Unlocking was not block-structured
4526   __ call_VM(noreg, CAST_FROM_FN_PTR(address,
4527                    InterpreterRuntime::throw_illegal_monitor_state_exception));
4528   __ should_not_reach_here();
4529 
4530   // call run-time routine
4531   __ bind(found);
4532   __ push_ptr(rax); // make sure object is on stack (contract with oopMaps)
4533   __ unlock_object(rtop);
4534   __ pop_ptr(rax); // discard object
4535 }
4536 
4537 // Wide instructions
4538 void TemplateTable::wide() {
4539   transition(vtos, vtos);
4540   __ load_unsigned_byte(rbx, at_bcp(1));
4541   ExternalAddress wtable((address)Interpreter::_wentry_point);
4542   __ jump(ArrayAddress(wtable, Address(noreg, rbx, Address::times_ptr)), rscratch1);
4543   // Note: the rbcp increment step is part of the individual wide bytecode implementations
4544 }
4545 
4546 // Multi arrays
4547 void TemplateTable::multianewarray() {
4548   transition(vtos, atos);
4549 
4550   Register rarg = LP64_ONLY(c_rarg1) NOT_LP64(rax);
4551   __ load_unsigned_byte(rax, at_bcp(3)); // get number of dimensions
4552   // last dim is on top of stack; we want address of first one:
4553   // first_addr = last_addr + (ndims - 1) * stackElementSize - 1*wordsize
4554   // the latter wordSize to point to the beginning of the array.
4555   __ lea(rarg, Address(rsp, rax, Interpreter::stackElementScale(), -wordSize));
4556   call_VM(rax, CAST_FROM_FN_PTR(address, InterpreterRuntime::multianewarray), rarg);
4557   __ load_unsigned_byte(rbx, at_bcp(3));
4558   __ lea(rsp, Address(rsp, rbx, Interpreter::stackElementScale()));  // get rid of counts
4559 }