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