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