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