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