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, /*tmp2*/ 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_ON_HEAP | ACCESS_ON_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_ON_HEAP);
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_ON_HEAP);
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   }
2566 
2567   assert(byte_no == f1_byte || byte_no == f2_byte, "byte_no out of range");
2568   __ get_cache_and_index_and_bytecode_at_bcp(Rcache, index, temp, byte_no, 1, index_size);
2569   __ cmpl(temp, code);  // have we resolved this bytecode?
2570   __ jcc(Assembler::equal, resolved);
2571 
2572   // resolve first time through
2573   address entry = CAST_FROM_FN_PTR(address, InterpreterRuntime::resolve_from_cache);
2574   __ movl(temp, code);
2575   __ call_VM(noreg, entry, temp);
2576   // Update registers with resolved info
2577   __ get_cache_and_index_at_bcp(Rcache, index, 1, index_size);
2578   __ bind(resolved);
2579 }
2580 
2581 // The cache and index registers must be set before call
2582 void TemplateTable::load_field_cp_cache_entry(Register obj,
2583                                               Register cache,
2584                                               Register index,
2585                                               Register off,
2586                                               Register flags,
2587                                               bool is_static = false) {
2588   assert_different_registers(cache, index, flags, off);
2589 
2590   ByteSize cp_base_offset = ConstantPoolCache::base_offset();
2591   // Field offset
2592   __ movptr(off, Address(cache, index, Address::times_ptr,
2593                          in_bytes(cp_base_offset +
2594                                   ConstantPoolCacheEntry::f2_offset())));
2595   // Flags
2596   __ movl(flags, Address(cache, index, Address::times_ptr,
2597                          in_bytes(cp_base_offset +
2598                                   ConstantPoolCacheEntry::flags_offset())));
2599 
2600   // klass overwrite register
2601   if (is_static) {
2602     __ movptr(obj, Address(cache, index, Address::times_ptr,
2603                            in_bytes(cp_base_offset +
2604                                     ConstantPoolCacheEntry::f1_offset())));
2605     const int mirror_offset = in_bytes(Klass::java_mirror_offset());
2606     __ movptr(obj, Address(obj, mirror_offset));
2607   }
2608 }
2609 
2610 void TemplateTable::load_invoke_cp_cache_entry(int byte_no,
2611                                                Register method,
2612                                                Register itable_index,
2613                                                Register flags,
2614                                                bool is_invokevirtual,
2615                                                bool is_invokevfinal, /*unused*/
2616                                                bool is_invokedynamic) {
2617   // setup registers
2618   const Register cache = rcx;
2619   const Register index = rdx;
2620   assert_different_registers(method, flags);
2621   assert_different_registers(method, cache, index);
2622   assert_different_registers(itable_index, flags);
2623   assert_different_registers(itable_index, cache, index);
2624   // determine constant pool cache field offsets
2625   assert(is_invokevirtual == (byte_no == f2_byte), "is_invokevirtual flag redundant");
2626   const int method_offset = in_bytes(
2627     ConstantPoolCache::base_offset() +
2628       ((byte_no == f2_byte)
2629        ? ConstantPoolCacheEntry::f2_offset()
2630        : ConstantPoolCacheEntry::f1_offset()));
2631   const int flags_offset = in_bytes(ConstantPoolCache::base_offset() +
2632                                     ConstantPoolCacheEntry::flags_offset());
2633   // access constant pool cache fields
2634   const int index_offset = in_bytes(ConstantPoolCache::base_offset() +
2635                                     ConstantPoolCacheEntry::f2_offset());
2636 
2637   size_t index_size = (is_invokedynamic ? sizeof(u4) : sizeof(u2));
2638   resolve_cache_and_index(byte_no, cache, index, index_size);
2639     __ movptr(method, Address(cache, index, Address::times_ptr, method_offset));
2640 
2641   if (itable_index != noreg) {
2642     // pick up itable or appendix index from f2 also:
2643     __ movptr(itable_index, Address(cache, index, Address::times_ptr, index_offset));
2644   }
2645   __ movl(flags, Address(cache, index, Address::times_ptr, flags_offset));
2646 }
2647 
2648 // The registers cache and index expected to be set before call.
2649 // Correct values of the cache and index registers are preserved.
2650 void TemplateTable::jvmti_post_field_access(Register cache,
2651                                             Register index,
2652                                             bool is_static,
2653                                             bool has_tos) {
2654   if (JvmtiExport::can_post_field_access()) {
2655     // Check to see if a field access watch has been set before we take
2656     // the time to call into the VM.
2657     Label L1;
2658     assert_different_registers(cache, index, rax);
2659     __ mov32(rax, ExternalAddress((address) JvmtiExport::get_field_access_count_addr()));
2660     __ testl(rax,rax);
2661     __ jcc(Assembler::zero, L1);
2662 
2663     // cache entry pointer
2664     __ addptr(cache, in_bytes(ConstantPoolCache::base_offset()));
2665     __ shll(index, LogBytesPerWord);
2666     __ addptr(cache, index);
2667     if (is_static) {
2668       __ xorptr(rax, rax);      // NULL object reference
2669     } else {
2670       __ pop(atos);         // Get the object
2671       __ verify_oop(rax);
2672       __ push(atos);        // Restore stack state
2673     }
2674     // rax,:   object pointer or NULL
2675     // cache: cache entry pointer
2676     __ call_VM(noreg, CAST_FROM_FN_PTR(address, InterpreterRuntime::post_field_access),
2677                rax, cache);
2678     __ get_cache_and_index_at_bcp(cache, index, 1);
2679     __ bind(L1);
2680   }
2681 }
2682 
2683 void TemplateTable::pop_and_check_object(Register r) {
2684   __ pop_ptr(r);
2685   __ null_check(r);  // for field access must check obj.
2686   __ verify_oop(r);
2687 }
2688 
2689 void TemplateTable::getfield_or_static(int byte_no, bool is_static, RewriteControl rc) {
2690   transition(vtos, vtos);
2691 
2692   const Register cache = rcx;
2693   const Register index = rdx;
2694   const Register obj   = LP64_ONLY(c_rarg3) NOT_LP64(rcx);
2695   const Register off   = rbx;
2696   const Register flags = rax;
2697   const Register bc    = LP64_ONLY(c_rarg3) NOT_LP64(rcx); // uses same reg as obj, so don't mix them
2698 
2699   resolve_cache_and_index(byte_no, cache, index, sizeof(u2));
2700   jvmti_post_field_access(cache, index, is_static, false);
2701   load_field_cp_cache_entry(obj, cache, index, off, flags, is_static);
2702 
2703   if (!is_static) pop_and_check_object(obj);
2704 
2705   const Address field(obj, off, Address::times_1, 0*wordSize);
2706   NOT_LP64(const Address hi(obj, off, Address::times_1, 1*wordSize));
2707 
2708   Label Done, notByte, notBool, notInt, notShort, notChar, notLong, notFloat, notObj, notDouble;
2709 
2710   __ shrl(flags, ConstantPoolCacheEntry::tos_state_shift);
2711   // Make sure we don't need to mask edx after the above shift
2712   assert(btos == 0, "change code, btos != 0");
2713 
2714   __ andl(flags, ConstantPoolCacheEntry::tos_state_mask);
2715 
2716   __ jcc(Assembler::notZero, notByte);
2717   // btos
2718   __ load_signed_byte(rax, field);
2719   __ push(btos);
2720   // Rewrite bytecode to be faster
2721   if (!is_static && rc == may_rewrite) {
2722     patch_bytecode(Bytecodes::_fast_bgetfield, bc, rbx);
2723   }
2724   __ jmp(Done);
2725 
2726   __ bind(notByte);
2727   __ cmpl(flags, ztos);
2728   __ jcc(Assembler::notEqual, notBool);
2729 
2730   // ztos (same code as btos)
2731   __ load_signed_byte(rax, field);
2732   __ push(ztos);
2733   // Rewrite bytecode to be faster
2734   if (!is_static && rc == may_rewrite) {
2735     // use btos rewriting, no truncating to t/f bit is needed for getfield.
2736     patch_bytecode(Bytecodes::_fast_bgetfield, bc, rbx);
2737   }
2738   __ jmp(Done);
2739 
2740   __ bind(notBool);
2741   __ cmpl(flags, atos);
2742   __ jcc(Assembler::notEqual, notObj);
2743   // atos
2744   __ load_heap_oop(rax, field);
2745   __ push(atos);
2746   if (!is_static && rc == may_rewrite) {
2747     patch_bytecode(Bytecodes::_fast_agetfield, bc, rbx);
2748   }
2749   __ jmp(Done);
2750 
2751   __ bind(notObj);
2752   __ cmpl(flags, itos);
2753   __ jcc(Assembler::notEqual, notInt);
2754   // itos
2755   __ movl(rax, field);
2756   __ push(itos);
2757   // Rewrite bytecode to be faster
2758   if (!is_static && rc == may_rewrite) {
2759     patch_bytecode(Bytecodes::_fast_igetfield, bc, rbx);
2760   }
2761   __ jmp(Done);
2762 
2763   __ bind(notInt);
2764   __ cmpl(flags, ctos);
2765   __ jcc(Assembler::notEqual, notChar);
2766   // ctos
2767   __ load_unsigned_short(rax, field);
2768   __ push(ctos);
2769   // Rewrite bytecode to be faster
2770   if (!is_static && rc == may_rewrite) {
2771     patch_bytecode(Bytecodes::_fast_cgetfield, bc, rbx);
2772   }
2773   __ jmp(Done);
2774 
2775   __ bind(notChar);
2776   __ cmpl(flags, stos);
2777   __ jcc(Assembler::notEqual, notShort);
2778   // stos
2779   __ load_signed_short(rax, field);
2780   __ push(stos);
2781   // Rewrite bytecode to be faster
2782   if (!is_static && rc == may_rewrite) {
2783     patch_bytecode(Bytecodes::_fast_sgetfield, bc, rbx);
2784   }
2785   __ jmp(Done);
2786 
2787   __ bind(notShort);
2788   __ cmpl(flags, ltos);
2789   __ jcc(Assembler::notEqual, notLong);
2790   // ltos
2791 
2792 #ifndef _LP64
2793   // Generate code as if volatile.  There just aren't enough registers to
2794   // save that information and this code is faster than the test.
2795   __ fild_d(field);                // Must load atomically
2796   __ subptr(rsp,2*wordSize);    // Make space for store
2797   __ fistp_d(Address(rsp,0));
2798   __ pop(rax);
2799   __ pop(rdx);
2800 #else
2801   __ movq(rax, field);
2802 #endif
2803 
2804   __ push(ltos);
2805   // Rewrite bytecode to be faster
2806   LP64_ONLY(if (!is_static && rc == may_rewrite) patch_bytecode(Bytecodes::_fast_lgetfield, bc, rbx));
2807   __ jmp(Done);
2808 
2809   __ bind(notLong);
2810   __ cmpl(flags, ftos);
2811   __ jcc(Assembler::notEqual, notFloat);
2812   // ftos
2813 
2814   __ load_float(field);
2815   __ push(ftos);
2816   // Rewrite bytecode to be faster
2817   if (!is_static && rc == may_rewrite) {
2818     patch_bytecode(Bytecodes::_fast_fgetfield, bc, rbx);
2819   }
2820   __ jmp(Done);
2821 
2822   __ bind(notFloat);
2823 #ifdef ASSERT
2824   __ cmpl(flags, dtos);
2825   __ jcc(Assembler::notEqual, notDouble);
2826 #endif
2827   // dtos
2828   __ load_double(field);
2829   __ push(dtos);
2830   // Rewrite bytecode to be faster
2831   if (!is_static && rc == may_rewrite) {
2832     patch_bytecode(Bytecodes::_fast_dgetfield, bc, rbx);
2833   }
2834 #ifdef ASSERT
2835   __ jmp(Done);
2836 
2837 
2838   __ bind(notDouble);
2839   __ stop("Bad state");
2840 #endif
2841 
2842   __ bind(Done);
2843   // [jk] not needed currently
2844   // volatile_barrier(Assembler::Membar_mask_bits(Assembler::LoadLoad |
2845   //                                              Assembler::LoadStore));
2846 }
2847 
2848 void TemplateTable::getfield(int byte_no) {
2849   getfield_or_static(byte_no, false);
2850 }
2851 
2852 void TemplateTable::nofast_getfield(int byte_no) {
2853   getfield_or_static(byte_no, false, may_not_rewrite);
2854 }
2855 
2856 void TemplateTable::getstatic(int byte_no) {
2857   getfield_or_static(byte_no, true);
2858 }
2859 
2860 
2861 // The registers cache and index expected to be set before call.
2862 // The function may destroy various registers, just not the cache and index registers.
2863 void TemplateTable::jvmti_post_field_mod(Register cache, Register index, bool is_static) {
2864 
2865   const Register robj = LP64_ONLY(c_rarg2)   NOT_LP64(rax);
2866   const Register RBX  = LP64_ONLY(c_rarg1)   NOT_LP64(rbx);
2867   const Register RCX  = LP64_ONLY(c_rarg3)   NOT_LP64(rcx);
2868   const Register RDX  = LP64_ONLY(rscratch1) NOT_LP64(rdx);
2869 
2870   ByteSize cp_base_offset = ConstantPoolCache::base_offset();
2871 
2872   if (JvmtiExport::can_post_field_modification()) {
2873     // Check to see if a field modification watch has been set before
2874     // we take the time to call into the VM.
2875     Label L1;
2876     assert_different_registers(cache, index, rax);
2877     __ mov32(rax, ExternalAddress((address)JvmtiExport::get_field_modification_count_addr()));
2878     __ testl(rax, rax);
2879     __ jcc(Assembler::zero, L1);
2880 
2881     __ get_cache_and_index_at_bcp(robj, RDX, 1);
2882 
2883 
2884     if (is_static) {
2885       // Life is simple.  Null out the object pointer.
2886       __ xorl(RBX, RBX);
2887 
2888     } else {
2889       // Life is harder. The stack holds the value on top, followed by
2890       // the object.  We don't know the size of the value, though; it
2891       // could be one or two words depending on its type. As a result,
2892       // we must find the type to determine where the object is.
2893 #ifndef _LP64
2894       Label two_word, valsize_known;
2895 #endif
2896       __ movl(RCX, Address(robj, RDX,
2897                            Address::times_ptr,
2898                            in_bytes(cp_base_offset +
2899                                      ConstantPoolCacheEntry::flags_offset())));
2900       NOT_LP64(__ mov(rbx, rsp));
2901       __ shrl(RCX, ConstantPoolCacheEntry::tos_state_shift);
2902 
2903       // Make sure we don't need to mask rcx after the above shift
2904       ConstantPoolCacheEntry::verify_tos_state_shift();
2905 #ifdef _LP64
2906       __ movptr(c_rarg1, at_tos_p1());  // initially assume a one word jvalue
2907       __ cmpl(c_rarg3, ltos);
2908       __ cmovptr(Assembler::equal,
2909                  c_rarg1, at_tos_p2()); // ltos (two word jvalue)
2910       __ cmpl(c_rarg3, dtos);
2911       __ cmovptr(Assembler::equal,
2912                  c_rarg1, at_tos_p2()); // dtos (two word jvalue)
2913 #else
2914       __ cmpl(rcx, ltos);
2915       __ jccb(Assembler::equal, two_word);
2916       __ cmpl(rcx, dtos);
2917       __ jccb(Assembler::equal, two_word);
2918       __ addptr(rbx, Interpreter::expr_offset_in_bytes(1)); // one word jvalue (not ltos, dtos)
2919       __ jmpb(valsize_known);
2920 
2921       __ bind(two_word);
2922       __ addptr(rbx, Interpreter::expr_offset_in_bytes(2)); // two words jvalue
2923 
2924       __ bind(valsize_known);
2925       // setup object pointer
2926       __ movptr(rbx, Address(rbx, 0));
2927 #endif
2928     }
2929     // cache entry pointer
2930     __ addptr(robj, in_bytes(cp_base_offset));
2931     __ shll(RDX, LogBytesPerWord);
2932     __ addptr(robj, RDX);
2933     // object (tos)
2934     __ mov(RCX, rsp);
2935     // c_rarg1: object pointer set up above (NULL if static)
2936     // c_rarg2: cache entry pointer
2937     // c_rarg3: jvalue object on the stack
2938     __ call_VM(noreg,
2939                CAST_FROM_FN_PTR(address,
2940                                 InterpreterRuntime::post_field_modification),
2941                RBX, robj, RCX);
2942     __ get_cache_and_index_at_bcp(cache, index, 1);
2943     __ bind(L1);
2944   }
2945 }
2946 
2947 void TemplateTable::putfield_or_static(int byte_no, bool is_static, RewriteControl rc) {
2948   transition(vtos, vtos);
2949 
2950   const Register cache = rcx;
2951   const Register index = rdx;
2952   const Register obj   = rcx;
2953   const Register off   = rbx;
2954   const Register flags = rax;
2955   const Register bc    = LP64_ONLY(c_rarg3) NOT_LP64(rcx);
2956 
2957   resolve_cache_and_index(byte_no, cache, index, sizeof(u2));
2958   jvmti_post_field_mod(cache, index, is_static);
2959   load_field_cp_cache_entry(obj, cache, index, off, flags, is_static);
2960 
2961   // [jk] not needed currently
2962   // volatile_barrier(Assembler::Membar_mask_bits(Assembler::LoadStore |
2963   //                                              Assembler::StoreStore));
2964 
2965   Label notVolatile, Done;
2966   __ movl(rdx, flags);
2967   __ shrl(rdx, ConstantPoolCacheEntry::is_volatile_shift);
2968   __ andl(rdx, 0x1);
2969 
2970   // field addresses
2971   const Address field(obj, off, Address::times_1, 0*wordSize);
2972   NOT_LP64( const Address hi(obj, off, Address::times_1, 1*wordSize);)
2973 
2974   Label notByte, notBool, notInt, notShort, notChar,
2975         notLong, notFloat, notObj, notDouble;
2976 
2977   __ shrl(flags, ConstantPoolCacheEntry::tos_state_shift);
2978 
2979   assert(btos == 0, "change code, btos != 0");
2980   __ andl(flags, ConstantPoolCacheEntry::tos_state_mask);
2981   __ jcc(Assembler::notZero, notByte);
2982 
2983   // btos
2984   {
2985     __ pop(btos);
2986     if (!is_static) pop_and_check_object(obj);
2987     __ movb(field, rax);
2988     if (!is_static && rc == may_rewrite) {
2989       patch_bytecode(Bytecodes::_fast_bputfield, bc, rbx, true, byte_no);
2990     }
2991     __ jmp(Done);
2992   }
2993 
2994   __ bind(notByte);
2995   __ cmpl(flags, ztos);
2996   __ jcc(Assembler::notEqual, notBool);
2997 
2998   // ztos
2999   {
3000     __ pop(ztos);
3001     if (!is_static) pop_and_check_object(obj);
3002     __ andl(rax, 0x1);
3003     __ movb(field, rax);
3004     if (!is_static && rc == may_rewrite) {
3005       patch_bytecode(Bytecodes::_fast_zputfield, bc, rbx, true, byte_no);
3006     }
3007     __ jmp(Done);
3008   }
3009 
3010   __ bind(notBool);
3011   __ cmpl(flags, atos);
3012   __ jcc(Assembler::notEqual, notObj);
3013 
3014   // atos
3015   {
3016     __ pop(atos);
3017     if (!is_static) pop_and_check_object(obj);
3018     // Store into the field
3019     do_oop_store(_masm, field, rax, ACCESS_ON_HEAP | ACCESS_ON_ARRAY);
3020     if (!is_static && rc == may_rewrite) {
3021       patch_bytecode(Bytecodes::_fast_aputfield, bc, rbx, true, byte_no);
3022     }
3023     __ jmp(Done);
3024   }
3025 
3026   __ bind(notObj);
3027   __ cmpl(flags, itos);
3028   __ jcc(Assembler::notEqual, notInt);
3029 
3030   // itos
3031   {
3032     __ pop(itos);
3033     if (!is_static) pop_and_check_object(obj);
3034     __ movl(field, rax);
3035     if (!is_static && rc == may_rewrite) {
3036       patch_bytecode(Bytecodes::_fast_iputfield, bc, rbx, true, byte_no);
3037     }
3038     __ jmp(Done);
3039   }
3040 
3041   __ bind(notInt);
3042   __ cmpl(flags, ctos);
3043   __ jcc(Assembler::notEqual, notChar);
3044 
3045   // ctos
3046   {
3047     __ pop(ctos);
3048     if (!is_static) pop_and_check_object(obj);
3049     __ movw(field, rax);
3050     if (!is_static && rc == may_rewrite) {
3051       patch_bytecode(Bytecodes::_fast_cputfield, bc, rbx, true, byte_no);
3052     }
3053     __ jmp(Done);
3054   }
3055 
3056   __ bind(notChar);
3057   __ cmpl(flags, stos);
3058   __ jcc(Assembler::notEqual, notShort);
3059 
3060   // stos
3061   {
3062     __ pop(stos);
3063     if (!is_static) pop_and_check_object(obj);
3064     __ movw(field, rax);
3065     if (!is_static && rc == may_rewrite) {
3066       patch_bytecode(Bytecodes::_fast_sputfield, bc, rbx, true, byte_no);
3067     }
3068     __ jmp(Done);
3069   }
3070 
3071   __ bind(notShort);
3072   __ cmpl(flags, ltos);
3073   __ jcc(Assembler::notEqual, notLong);
3074 
3075   // ltos
3076 #ifdef _LP64
3077   {
3078     __ pop(ltos);
3079     if (!is_static) pop_and_check_object(obj);
3080     __ movq(field, rax);
3081     if (!is_static && rc == may_rewrite) {
3082       patch_bytecode(Bytecodes::_fast_lputfield, bc, rbx, true, byte_no);
3083     }
3084     __ jmp(Done);
3085   }
3086 #else
3087   {
3088     Label notVolatileLong;
3089     __ testl(rdx, rdx);
3090     __ jcc(Assembler::zero, notVolatileLong);
3091 
3092     __ pop(ltos);  // overwrites rdx, do this after testing volatile.
3093     if (!is_static) pop_and_check_object(obj);
3094 
3095     // Replace with real volatile test
3096     __ push(rdx);
3097     __ push(rax);                 // Must update atomically with FIST
3098     __ fild_d(Address(rsp,0));    // So load into FPU register
3099     __ fistp_d(field);            // and put into memory atomically
3100     __ addptr(rsp, 2*wordSize);
3101     // volatile_barrier();
3102     volatile_barrier(Assembler::Membar_mask_bits(Assembler::StoreLoad |
3103                                                  Assembler::StoreStore));
3104     // Don't rewrite volatile version
3105     __ jmp(notVolatile);
3106 
3107     __ bind(notVolatileLong);
3108 
3109     __ pop(ltos);  // overwrites rdx
3110     if (!is_static) pop_and_check_object(obj);
3111     __ movptr(hi, rdx);
3112     __ movptr(field, rax);
3113     // Don't rewrite to _fast_lputfield for potential volatile case.
3114     __ jmp(notVolatile);
3115   }
3116 #endif // _LP64
3117 
3118   __ bind(notLong);
3119   __ cmpl(flags, ftos);
3120   __ jcc(Assembler::notEqual, notFloat);
3121 
3122   // ftos
3123   {
3124     __ pop(ftos);
3125     if (!is_static) pop_and_check_object(obj);
3126     __ store_float(field);
3127     if (!is_static && rc == may_rewrite) {
3128       patch_bytecode(Bytecodes::_fast_fputfield, bc, rbx, true, byte_no);
3129     }
3130     __ jmp(Done);
3131   }
3132 
3133   __ bind(notFloat);
3134 #ifdef ASSERT
3135   __ cmpl(flags, dtos);
3136   __ jcc(Assembler::notEqual, notDouble);
3137 #endif
3138 
3139   // dtos
3140   {
3141     __ pop(dtos);
3142     if (!is_static) pop_and_check_object(obj);
3143     __ store_double(field);
3144     if (!is_static && rc == may_rewrite) {
3145       patch_bytecode(Bytecodes::_fast_dputfield, bc, rbx, true, byte_no);
3146     }
3147   }
3148 
3149 #ifdef ASSERT
3150   __ jmp(Done);
3151 
3152   __ bind(notDouble);
3153   __ stop("Bad state");
3154 #endif
3155 
3156   __ bind(Done);
3157 
3158   // Check for volatile store
3159   __ testl(rdx, rdx);
3160   __ jcc(Assembler::zero, notVolatile);
3161   volatile_barrier(Assembler::Membar_mask_bits(Assembler::StoreLoad |
3162                                                Assembler::StoreStore));
3163   __ bind(notVolatile);
3164 }
3165 
3166 void TemplateTable::putfield(int byte_no) {
3167   putfield_or_static(byte_no, false);
3168 }
3169 
3170 void TemplateTable::nofast_putfield(int byte_no) {
3171   putfield_or_static(byte_no, false, may_not_rewrite);
3172 }
3173 
3174 void TemplateTable::putstatic(int byte_no) {
3175   putfield_or_static(byte_no, true);
3176 }
3177 
3178 void TemplateTable::jvmti_post_fast_field_mod() {
3179 
3180   const Register scratch = LP64_ONLY(c_rarg3) NOT_LP64(rcx);
3181 
3182   if (JvmtiExport::can_post_field_modification()) {
3183     // Check to see if a field modification watch has been set before
3184     // we take the time to call into the VM.
3185     Label L2;
3186     __ mov32(scratch, ExternalAddress((address)JvmtiExport::get_field_modification_count_addr()));
3187     __ testl(scratch, scratch);
3188     __ jcc(Assembler::zero, L2);
3189     __ pop_ptr(rbx);                  // copy the object pointer from tos
3190     __ verify_oop(rbx);
3191     __ push_ptr(rbx);                 // put the object pointer back on tos
3192     // Save tos values before call_VM() clobbers them. Since we have
3193     // to do it for every data type, we use the saved values as the
3194     // jvalue object.
3195     switch (bytecode()) {          // load values into the jvalue object
3196     case Bytecodes::_fast_aputfield: __ push_ptr(rax); break;
3197     case Bytecodes::_fast_bputfield: // fall through
3198     case Bytecodes::_fast_zputfield: // fall through
3199     case Bytecodes::_fast_sputfield: // fall through
3200     case Bytecodes::_fast_cputfield: // fall through
3201     case Bytecodes::_fast_iputfield: __ push_i(rax); break;
3202     case Bytecodes::_fast_dputfield: __ push(dtos); break;
3203     case Bytecodes::_fast_fputfield: __ push(ftos); break;
3204     case Bytecodes::_fast_lputfield: __ push_l(rax); break;
3205 
3206     default:
3207       ShouldNotReachHere();
3208     }
3209     __ mov(scratch, rsp);             // points to jvalue on the stack
3210     // access constant pool cache entry
3211     LP64_ONLY(__ get_cache_entry_pointer_at_bcp(c_rarg2, rax, 1));
3212     NOT_LP64(__ get_cache_entry_pointer_at_bcp(rax, rdx, 1));
3213     __ verify_oop(rbx);
3214     // rbx: object pointer copied above
3215     // c_rarg2: cache entry pointer
3216     // c_rarg3: jvalue object on the stack
3217     LP64_ONLY(__ call_VM(noreg, CAST_FROM_FN_PTR(address, InterpreterRuntime::post_field_modification), rbx, c_rarg2, c_rarg3));
3218     NOT_LP64(__ call_VM(noreg, CAST_FROM_FN_PTR(address, InterpreterRuntime::post_field_modification), rbx, rax, rcx));
3219 
3220     switch (bytecode()) {             // restore tos values
3221     case Bytecodes::_fast_aputfield: __ pop_ptr(rax); break;
3222     case Bytecodes::_fast_bputfield: // fall through
3223     case Bytecodes::_fast_zputfield: // fall through
3224     case Bytecodes::_fast_sputfield: // fall through
3225     case Bytecodes::_fast_cputfield: // fall through
3226     case Bytecodes::_fast_iputfield: __ pop_i(rax); break;
3227     case Bytecodes::_fast_dputfield: __ pop(dtos); break;
3228     case Bytecodes::_fast_fputfield: __ pop(ftos); break;
3229     case Bytecodes::_fast_lputfield: __ pop_l(rax); break;
3230     }
3231     __ bind(L2);
3232   }
3233 }
3234 
3235 void TemplateTable::fast_storefield(TosState state) {
3236   transition(state, vtos);
3237 
3238   ByteSize base = ConstantPoolCache::base_offset();
3239 
3240   jvmti_post_fast_field_mod();
3241 
3242   // access constant pool cache
3243   __ get_cache_and_index_at_bcp(rcx, rbx, 1);
3244 
3245   // test for volatile with rdx but rdx is tos register for lputfield.
3246   __ movl(rdx, Address(rcx, rbx, Address::times_ptr,
3247                        in_bytes(base +
3248                                 ConstantPoolCacheEntry::flags_offset())));
3249 
3250   // replace index with field offset from cache entry
3251   __ movptr(rbx, Address(rcx, rbx, Address::times_ptr,
3252                          in_bytes(base + ConstantPoolCacheEntry::f2_offset())));
3253 
3254   // [jk] not needed currently
3255   // volatile_barrier(Assembler::Membar_mask_bits(Assembler::LoadStore |
3256   //                                              Assembler::StoreStore));
3257 
3258   Label notVolatile;
3259   __ shrl(rdx, ConstantPoolCacheEntry::is_volatile_shift);
3260   __ andl(rdx, 0x1);
3261 
3262   // Get object from stack
3263   pop_and_check_object(rcx);
3264 
3265   // field address
3266   const Address field(rcx, rbx, Address::times_1);
3267 
3268   // access field
3269   switch (bytecode()) {
3270   case Bytecodes::_fast_aputfield:
3271     do_oop_store(_masm, field, rax, ACCESS_ON_HEAP | ACCESS_ON_ARRAY);
3272     break;
3273   case Bytecodes::_fast_lputfield:
3274 #ifdef _LP64
3275   __ movq(field, rax);
3276 #else
3277   __ stop("should not be rewritten");
3278 #endif
3279     break;
3280   case Bytecodes::_fast_iputfield:
3281     __ movl(field, rax);
3282     break;
3283   case Bytecodes::_fast_zputfield:
3284     __ andl(rax, 0x1);  // boolean is true if LSB is 1
3285     // fall through to bputfield
3286   case Bytecodes::_fast_bputfield:
3287     __ movb(field, rax);
3288     break;
3289   case Bytecodes::_fast_sputfield:
3290     // fall through
3291   case Bytecodes::_fast_cputfield:
3292     __ movw(field, rax);
3293     break;
3294   case Bytecodes::_fast_fputfield:
3295     __ store_float(field);
3296     break;
3297   case Bytecodes::_fast_dputfield:
3298     __ store_double(field);
3299     break;
3300   default:
3301     ShouldNotReachHere();
3302   }
3303 
3304   // Check for volatile store
3305   __ testl(rdx, rdx);
3306   __ jcc(Assembler::zero, notVolatile);
3307   volatile_barrier(Assembler::Membar_mask_bits(Assembler::StoreLoad |
3308                                                Assembler::StoreStore));
3309   __ bind(notVolatile);
3310 }
3311 
3312 void TemplateTable::fast_accessfield(TosState state) {
3313   transition(atos, state);
3314 
3315   // Do the JVMTI work here to avoid disturbing the register state below
3316   if (JvmtiExport::can_post_field_access()) {
3317     // Check to see if a field access watch has been set before we
3318     // take the time to call into the VM.
3319     Label L1;
3320     __ mov32(rcx, ExternalAddress((address) JvmtiExport::get_field_access_count_addr()));
3321     __ testl(rcx, rcx);
3322     __ jcc(Assembler::zero, L1);
3323     // access constant pool cache entry
3324     LP64_ONLY(__ get_cache_entry_pointer_at_bcp(c_rarg2, rcx, 1));
3325     NOT_LP64(__ get_cache_entry_pointer_at_bcp(rcx, rdx, 1));
3326     __ verify_oop(rax);
3327     __ push_ptr(rax);  // save object pointer before call_VM() clobbers it
3328     LP64_ONLY(__ mov(c_rarg1, rax));
3329     // c_rarg1: object pointer copied above
3330     // c_rarg2: cache entry pointer
3331     LP64_ONLY(__ call_VM(noreg, CAST_FROM_FN_PTR(address, InterpreterRuntime::post_field_access), c_rarg1, c_rarg2));
3332     NOT_LP64(__ call_VM(noreg, CAST_FROM_FN_PTR(address, InterpreterRuntime::post_field_access), rax, rcx));
3333     __ pop_ptr(rax); // restore object pointer
3334     __ bind(L1);
3335   }
3336 
3337   // access constant pool cache
3338   __ get_cache_and_index_at_bcp(rcx, rbx, 1);
3339   // replace index with field offset from cache entry
3340   // [jk] not needed currently
3341   // if (os::is_MP()) {
3342   //   __ movl(rdx, Address(rcx, rbx, Address::times_8,
3343   //                        in_bytes(ConstantPoolCache::base_offset() +
3344   //                                 ConstantPoolCacheEntry::flags_offset())));
3345   //   __ shrl(rdx, ConstantPoolCacheEntry::is_volatile_shift);
3346   //   __ andl(rdx, 0x1);
3347   // }
3348   __ movptr(rbx, Address(rcx, rbx, Address::times_ptr,
3349                          in_bytes(ConstantPoolCache::base_offset() +
3350                                   ConstantPoolCacheEntry::f2_offset())));
3351 
3352   // rax: object
3353   __ verify_oop(rax);
3354   __ null_check(rax);
3355   Address field(rax, rbx, Address::times_1);
3356 
3357   // access field
3358   switch (bytecode()) {
3359   case Bytecodes::_fast_agetfield:
3360     __ load_heap_oop(rax, field);
3361     __ verify_oop(rax);
3362     break;
3363   case Bytecodes::_fast_lgetfield:
3364 #ifdef _LP64
3365   __ movq(rax, field);
3366 #else
3367   __ stop("should not be rewritten");
3368 #endif
3369     break;
3370   case Bytecodes::_fast_igetfield:
3371     __ movl(rax, field);
3372     break;
3373   case Bytecodes::_fast_bgetfield:
3374     __ movsbl(rax, field);
3375     break;
3376   case Bytecodes::_fast_sgetfield:
3377     __ load_signed_short(rax, field);
3378     break;
3379   case Bytecodes::_fast_cgetfield:
3380     __ load_unsigned_short(rax, field);
3381     break;
3382   case Bytecodes::_fast_fgetfield:
3383     __ load_float(field);
3384     break;
3385   case Bytecodes::_fast_dgetfield:
3386     __ load_double(field);
3387     break;
3388   default:
3389     ShouldNotReachHere();
3390   }
3391   // [jk] not needed currently
3392   // if (os::is_MP()) {
3393   //   Label notVolatile;
3394   //   __ testl(rdx, rdx);
3395   //   __ jcc(Assembler::zero, notVolatile);
3396   //   __ membar(Assembler::LoadLoad);
3397   //   __ bind(notVolatile);
3398   //};
3399 }
3400 
3401 void TemplateTable::fast_xaccess(TosState state) {
3402   transition(vtos, state);
3403 
3404   // get receiver
3405   __ movptr(rax, aaddress(0));
3406   // access constant pool cache
3407   __ get_cache_and_index_at_bcp(rcx, rdx, 2);
3408   __ movptr(rbx,
3409             Address(rcx, rdx, Address::times_ptr,
3410                     in_bytes(ConstantPoolCache::base_offset() +
3411                              ConstantPoolCacheEntry::f2_offset())));
3412   // make sure exception is reported in correct bcp range (getfield is
3413   // next instruction)
3414   __ increment(rbcp);
3415   __ null_check(rax);
3416   const Address field = Address(rax, rbx, Address::times_1, 0*wordSize);
3417   switch (state) {
3418   case itos:
3419     __ movl(rax, field);
3420     break;
3421   case atos:
3422     __ load_heap_oop(rax, field);
3423     __ verify_oop(rax);
3424     break;
3425   case ftos:
3426     __ load_float(field);
3427     break;
3428   default:
3429     ShouldNotReachHere();
3430   }
3431 
3432   // [jk] not needed currently
3433   // if (os::is_MP()) {
3434   //   Label notVolatile;
3435   //   __ movl(rdx, Address(rcx, rdx, Address::times_8,
3436   //                        in_bytes(ConstantPoolCache::base_offset() +
3437   //                                 ConstantPoolCacheEntry::flags_offset())));
3438   //   __ shrl(rdx, ConstantPoolCacheEntry::is_volatile_shift);
3439   //   __ testl(rdx, 0x1);
3440   //   __ jcc(Assembler::zero, notVolatile);
3441   //   __ membar(Assembler::LoadLoad);
3442   //   __ bind(notVolatile);
3443   // }
3444 
3445   __ decrement(rbcp);
3446 }
3447 
3448 //-----------------------------------------------------------------------------
3449 // Calls
3450 
3451 void TemplateTable::count_calls(Register method, Register temp) {
3452   // implemented elsewhere
3453   ShouldNotReachHere();
3454 }
3455 
3456 void TemplateTable::prepare_invoke(int byte_no,
3457                                    Register method,  // linked method (or i-klass)
3458                                    Register index,   // itable index, MethodType, etc.
3459                                    Register recv,    // if caller wants to see it
3460                                    Register flags    // if caller wants to test it
3461                                    ) {
3462   // determine flags
3463   const Bytecodes::Code code = bytecode();
3464   const bool is_invokeinterface  = code == Bytecodes::_invokeinterface;
3465   const bool is_invokedynamic    = code == Bytecodes::_invokedynamic;
3466   const bool is_invokehandle     = code == Bytecodes::_invokehandle;
3467   const bool is_invokevirtual    = code == Bytecodes::_invokevirtual;
3468   const bool is_invokespecial    = code == Bytecodes::_invokespecial;
3469   const bool load_receiver       = (recv  != noreg);
3470   const bool save_flags          = (flags != noreg);
3471   assert(load_receiver == (code != Bytecodes::_invokestatic && code != Bytecodes::_invokedynamic), "");
3472   assert(save_flags    == (is_invokeinterface || is_invokevirtual), "need flags for vfinal");
3473   assert(flags == noreg || flags == rdx, "");
3474   assert(recv  == noreg || recv  == rcx, "");
3475 
3476   // setup registers & access constant pool cache
3477   if (recv  == noreg)  recv  = rcx;
3478   if (flags == noreg)  flags = rdx;
3479   assert_different_registers(method, index, recv, flags);
3480 
3481   // save 'interpreter return address'
3482   __ save_bcp();
3483 
3484   load_invoke_cp_cache_entry(byte_no, method, index, flags, is_invokevirtual, false, is_invokedynamic);
3485 
3486   // maybe push appendix to arguments (just before return address)
3487   if (is_invokedynamic || is_invokehandle) {
3488     Label L_no_push;
3489     __ testl(flags, (1 << ConstantPoolCacheEntry::has_appendix_shift));
3490     __ jcc(Assembler::zero, L_no_push);
3491     // Push the appendix as a trailing parameter.
3492     // This must be done before we get the receiver,
3493     // since the parameter_size includes it.
3494     __ push(rbx);
3495     __ mov(rbx, index);
3496     assert(ConstantPoolCacheEntry::_indy_resolved_references_appendix_offset == 0, "appendix expected at index+0");
3497     __ load_resolved_reference_at_index(index, rbx);
3498     __ pop(rbx);
3499     __ push(index);  // push appendix (MethodType, CallSite, etc.)
3500     __ bind(L_no_push);
3501   }
3502 
3503   // load receiver if needed (after appendix is pushed so parameter size is correct)
3504   // Note: no return address pushed yet
3505   if (load_receiver) {
3506     __ movl(recv, flags);
3507     __ andl(recv, ConstantPoolCacheEntry::parameter_size_mask);
3508     const int no_return_pc_pushed_yet = -1;  // argument slot correction before we push return address
3509     const int receiver_is_at_end      = -1;  // back off one slot to get receiver
3510     Address recv_addr = __ argument_address(recv, no_return_pc_pushed_yet + receiver_is_at_end);
3511     __ movptr(recv, recv_addr);
3512     __ verify_oop(recv);
3513   }
3514 
3515   if (save_flags) {
3516     __ movl(rbcp, flags);
3517   }
3518 
3519   // compute return type
3520   __ shrl(flags, ConstantPoolCacheEntry::tos_state_shift);
3521   // Make sure we don't need to mask flags after the above shift
3522   ConstantPoolCacheEntry::verify_tos_state_shift();
3523   // load return address
3524   {
3525     const address table_addr = (address) Interpreter::invoke_return_entry_table_for(code);
3526     ExternalAddress table(table_addr);
3527     LP64_ONLY(__ lea(rscratch1, table));
3528     LP64_ONLY(__ movptr(flags, Address(rscratch1, flags, Address::times_ptr)));
3529     NOT_LP64(__ movptr(flags, ArrayAddress(table, Address(noreg, flags, Address::times_ptr))));
3530   }
3531 
3532   // push return address
3533   __ push(flags);
3534 
3535   // Restore flags value from the constant pool cache, and restore rsi
3536   // for later null checks.  r13 is the bytecode pointer
3537   if (save_flags) {
3538     __ movl(flags, rbcp);
3539     __ restore_bcp();
3540   }
3541 }
3542 
3543 void TemplateTable::invokevirtual_helper(Register index,
3544                                          Register recv,
3545                                          Register flags) {
3546   // Uses temporary registers rax, rdx
3547   assert_different_registers(index, recv, rax, rdx);
3548   assert(index == rbx, "");
3549   assert(recv  == rcx, "");
3550 
3551   // Test for an invoke of a final method
3552   Label notFinal;
3553   __ movl(rax, flags);
3554   __ andl(rax, (1 << ConstantPoolCacheEntry::is_vfinal_shift));
3555   __ jcc(Assembler::zero, notFinal);
3556 
3557   const Register method = index;  // method must be rbx
3558   assert(method == rbx,
3559          "Method* must be rbx for interpreter calling convention");
3560 
3561   // do the call - the index is actually the method to call
3562   // that is, f2 is a vtable index if !is_vfinal, else f2 is a Method*
3563 
3564   // It's final, need a null check here!
3565   __ null_check(recv);
3566 
3567   // profile this call
3568   __ profile_final_call(rax);
3569   __ profile_arguments_type(rax, method, rbcp, true);
3570 
3571   __ jump_from_interpreted(method, rax);
3572 
3573   __ bind(notFinal);
3574 
3575   // get receiver klass
3576   __ null_check(recv, oopDesc::klass_offset_in_bytes());
3577   __ load_klass(rax, recv);
3578 
3579   // profile this call
3580   __ profile_virtual_call(rax, rlocals, rdx);
3581   // get target Method* & entry point
3582   __ lookup_virtual_method(rax, index, method);
3583   __ profile_called_method(method, rdx, rbcp);
3584 
3585   __ profile_arguments_type(rdx, method, rbcp, true);
3586   __ jump_from_interpreted(method, rdx);
3587 }
3588 
3589 void TemplateTable::invokevirtual(int byte_no) {
3590   transition(vtos, vtos);
3591   assert(byte_no == f2_byte, "use this argument");
3592   prepare_invoke(byte_no,
3593                  rbx,    // method or vtable index
3594                  noreg,  // unused itable index
3595                  rcx, rdx); // recv, flags
3596 
3597   // rbx: index
3598   // rcx: receiver
3599   // rdx: flags
3600 
3601   invokevirtual_helper(rbx, rcx, rdx);
3602 }
3603 
3604 void TemplateTable::invokespecial(int byte_no) {
3605   transition(vtos, vtos);
3606   assert(byte_no == f1_byte, "use this argument");
3607   prepare_invoke(byte_no, rbx, noreg,  // get f1 Method*
3608                  rcx);  // get receiver also for null check
3609   __ verify_oop(rcx);
3610   __ null_check(rcx);
3611   // do the call
3612   __ profile_call(rax);
3613   __ profile_arguments_type(rax, rbx, rbcp, false);
3614   __ jump_from_interpreted(rbx, rax);
3615 }
3616 
3617 void TemplateTable::invokestatic(int byte_no) {
3618   transition(vtos, vtos);
3619   assert(byte_no == f1_byte, "use this argument");
3620   prepare_invoke(byte_no, rbx);  // get f1 Method*
3621   // do the call
3622   __ profile_call(rax);
3623   __ profile_arguments_type(rax, rbx, rbcp, false);
3624   __ jump_from_interpreted(rbx, rax);
3625 }
3626 
3627 
3628 void TemplateTable::fast_invokevfinal(int byte_no) {
3629   transition(vtos, vtos);
3630   assert(byte_no == f2_byte, "use this argument");
3631   __ stop("fast_invokevfinal not used on x86");
3632 }
3633 
3634 
3635 void TemplateTable::invokeinterface(int byte_no) {
3636   transition(vtos, vtos);
3637   assert(byte_no == f1_byte, "use this argument");
3638   prepare_invoke(byte_no, rax, rbx,  // get f1 Klass*, f2 itable index
3639                  rcx, rdx); // recv, flags
3640 
3641   // rax: interface klass (from f1)
3642   // rbx: itable index (from f2)
3643   // rcx: receiver
3644   // rdx: flags
3645 
3646   // Special case of invokeinterface called for virtual method of
3647   // java.lang.Object.  See cpCacheOop.cpp for details.
3648   // This code isn't produced by javac, but could be produced by
3649   // another compliant java compiler.
3650   Label notMethod;
3651   __ movl(rlocals, rdx);
3652   __ andl(rlocals, (1 << ConstantPoolCacheEntry::is_forced_virtual_shift));
3653 
3654   __ jcc(Assembler::zero, notMethod);
3655 
3656   invokevirtual_helper(rbx, rcx, rdx);
3657   __ bind(notMethod);
3658 
3659   // Get receiver klass into rdx - also a null check
3660   __ restore_locals();  // restore r14
3661   __ null_check(rcx, oopDesc::klass_offset_in_bytes());
3662   __ load_klass(rdx, rcx);
3663 
3664   // profile this call
3665   __ profile_virtual_call(rdx, rbcp, rlocals);
3666 
3667   Label no_such_interface, no_such_method;
3668 
3669   __ lookup_interface_method(// inputs: rec. class, interface, itable index
3670                              rdx, rax, rbx,
3671                              // outputs: method, scan temp. reg
3672                              rbx, rbcp,
3673                              no_such_interface);
3674 
3675   // rbx: Method* to call
3676   // rcx: receiver
3677   // Check for abstract method error
3678   // Note: This should be done more efficiently via a throw_abstract_method_error
3679   //       interpreter entry point and a conditional jump to it in case of a null
3680   //       method.
3681   __ testptr(rbx, rbx);
3682   __ jcc(Assembler::zero, no_such_method);
3683 
3684   __ profile_called_method(rbx, rbcp, rdx);
3685   __ profile_arguments_type(rdx, rbx, rbcp, true);
3686 
3687   // do the call
3688   // rcx: receiver
3689   // rbx,: Method*
3690   __ jump_from_interpreted(rbx, rdx);
3691   __ should_not_reach_here();
3692 
3693   // exception handling code follows...
3694   // note: must restore interpreter registers to canonical
3695   //       state for exception handling to work correctly!
3696 
3697   __ bind(no_such_method);
3698   // throw exception
3699   __ pop(rbx);           // pop return address (pushed by prepare_invoke)
3700   __ restore_bcp();      // rbcp must be correct for exception handler   (was destroyed)
3701   __ restore_locals();   // make sure locals pointer is correct as well (was destroyed)
3702   __ call_VM(noreg, CAST_FROM_FN_PTR(address, InterpreterRuntime::throw_AbstractMethodError));
3703   // the call_VM checks for exception, so we should never return here.
3704   __ should_not_reach_here();
3705 
3706   __ bind(no_such_interface);
3707   // throw exception
3708   __ pop(rbx);           // pop return address (pushed by prepare_invoke)
3709   __ restore_bcp();      // rbcp must be correct for exception handler   (was destroyed)
3710   __ restore_locals();   // make sure locals pointer is correct as well (was destroyed)
3711   __ call_VM(noreg, CAST_FROM_FN_PTR(address,
3712                    InterpreterRuntime::throw_IncompatibleClassChangeError));
3713   // the call_VM checks for exception, so we should never return here.
3714   __ should_not_reach_here();
3715 }
3716 
3717 void TemplateTable::invokehandle(int byte_no) {
3718   transition(vtos, vtos);
3719   assert(byte_no == f1_byte, "use this argument");
3720   const Register rbx_method = rbx;
3721   const Register rax_mtype  = rax;
3722   const Register rcx_recv   = rcx;
3723   const Register rdx_flags  = rdx;
3724 
3725   prepare_invoke(byte_no, rbx_method, rax_mtype, rcx_recv);
3726   __ verify_method_ptr(rbx_method);
3727   __ verify_oop(rcx_recv);
3728   __ null_check(rcx_recv);
3729 
3730   // rax: MethodType object (from cpool->resolved_references[f1], if necessary)
3731   // rbx: MH.invokeExact_MT method (from f2)
3732 
3733   // Note:  rax_mtype is already pushed (if necessary) by prepare_invoke
3734 
3735   // FIXME: profile the LambdaForm also
3736   __ profile_final_call(rax);
3737   __ profile_arguments_type(rdx, rbx_method, rbcp, true);
3738 
3739   __ jump_from_interpreted(rbx_method, rdx);
3740 }
3741 
3742 void TemplateTable::invokedynamic(int byte_no) {
3743   transition(vtos, vtos);
3744   assert(byte_no == f1_byte, "use this argument");
3745 
3746   const Register rbx_method   = rbx;
3747   const Register rax_callsite = rax;
3748 
3749   prepare_invoke(byte_no, rbx_method, rax_callsite);
3750 
3751   // rax: CallSite object (from cpool->resolved_references[f1])
3752   // rbx: MH.linkToCallSite method (from f2)
3753 
3754   // Note:  rax_callsite is already pushed by prepare_invoke
3755 
3756   // %%% should make a type profile for any invokedynamic that takes a ref argument
3757   // profile this call
3758   __ profile_call(rbcp);
3759   __ profile_arguments_type(rdx, rbx_method, rbcp, false);
3760 
3761   __ verify_oop(rax_callsite);
3762 
3763   __ jump_from_interpreted(rbx_method, rdx);
3764 }
3765 
3766 //-----------------------------------------------------------------------------
3767 // Allocation
3768 
3769 void TemplateTable::_new() {
3770   transition(vtos, atos);
3771   __ get_unsigned_2_byte_index_at_bcp(rdx, 1);
3772   Label slow_case;
3773   Label slow_case_no_pop;
3774   Label done;
3775   Label initialize_header;
3776   Label initialize_object;  // including clearing the fields
3777   Label allocate_shared;
3778 
3779   __ get_cpool_and_tags(rcx, rax);
3780 
3781   // Make sure the class we're about to instantiate has been resolved.
3782   // This is done before loading InstanceKlass to be consistent with the order
3783   // how Constant Pool is updated (see ConstantPool::klass_at_put)
3784   const int tags_offset = Array<u1>::base_offset_in_bytes();
3785   __ cmpb(Address(rax, rdx, Address::times_1, tags_offset), JVM_CONSTANT_Class);
3786   __ jcc(Assembler::notEqual, slow_case_no_pop);
3787 
3788   // get InstanceKlass
3789   __ movptr(rcx, Address(rcx, rdx, Address::times_ptr, sizeof(ConstantPool)));
3790   __ push(rcx);  // save the contexts of klass for initializing the header
3791 
3792   // make sure klass is initialized & doesn't have finalizer
3793   // make sure klass is fully initialized
3794   __ cmpb(Address(rcx, InstanceKlass::init_state_offset()), InstanceKlass::fully_initialized);
3795   __ jcc(Assembler::notEqual, slow_case);
3796 
3797   // get instance_size in InstanceKlass (scaled to a count of bytes)
3798   __ movl(rdx, Address(rcx, Klass::layout_helper_offset()));
3799   // test to see if it has a finalizer or is malformed in some way
3800   __ testl(rdx, Klass::_lh_instance_slow_path_bit);
3801   __ jcc(Assembler::notZero, slow_case);
3802 
3803   //
3804   // Allocate the instance
3805   // 1) Try to allocate in the TLAB
3806   // 2) if fail and the object is large allocate in the shared Eden
3807   // 3) if the above fails (or is not applicable), go to a slow case
3808   // (creates a new TLAB, etc.)
3809 
3810   const bool allow_shared_alloc =
3811     Universe::heap()->supports_inline_contig_alloc();
3812 
3813   const Register thread = LP64_ONLY(r15_thread) NOT_LP64(rcx);
3814 #ifndef _LP64
3815   if (UseTLAB || allow_shared_alloc) {
3816     __ get_thread(thread);
3817   }
3818 #endif // _LP64
3819 
3820   if (UseTLAB) {
3821     __ movptr(rax, Address(thread, in_bytes(JavaThread::tlab_top_offset())));
3822     __ lea(rbx, Address(rax, rdx, Address::times_1));
3823     __ cmpptr(rbx, Address(thread, in_bytes(JavaThread::tlab_end_offset())));
3824     __ jcc(Assembler::above, allow_shared_alloc ? allocate_shared : slow_case);
3825     __ movptr(Address(thread, in_bytes(JavaThread::tlab_top_offset())), rbx);
3826     if (ZeroTLAB) {
3827       // the fields have been already cleared
3828       __ jmp(initialize_header);
3829     } else {
3830       // initialize both the header and fields
3831       __ jmp(initialize_object);
3832     }
3833   }
3834 
3835   // Allocation in the shared Eden, if allowed.
3836   //
3837   // rdx: instance size in bytes
3838   if (allow_shared_alloc) {
3839     __ bind(allocate_shared);
3840 
3841     ExternalAddress heap_top((address)Universe::heap()->top_addr());
3842     ExternalAddress heap_end((address)Universe::heap()->end_addr());
3843 
3844     Label retry;
3845     __ bind(retry);
3846     __ movptr(rax, heap_top);
3847     __ lea(rbx, Address(rax, rdx, Address::times_1));
3848     __ cmpptr(rbx, heap_end);
3849     __ jcc(Assembler::above, slow_case);
3850 
3851     // Compare rax, with the top addr, and if still equal, store the new
3852     // top addr in rbx, at the address of the top addr pointer. Sets ZF if was
3853     // equal, and clears it otherwise. Use lock prefix for atomicity on MPs.
3854     //
3855     // rax,: object begin
3856     // rbx,: object end
3857     // rdx: instance size in bytes
3858     __ locked_cmpxchgptr(rbx, heap_top);
3859 
3860     // if someone beat us on the allocation, try again, otherwise continue
3861     __ jcc(Assembler::notEqual, retry);
3862 
3863     __ incr_allocated_bytes(thread, rdx, 0);
3864   }
3865 
3866   if (UseTLAB || Universe::heap()->supports_inline_contig_alloc()) {
3867     // The object is initialized before the header.  If the object size is
3868     // zero, go directly to the header initialization.
3869     __ bind(initialize_object);
3870     __ decrement(rdx, sizeof(oopDesc));
3871     __ jcc(Assembler::zero, initialize_header);
3872 
3873     // Initialize topmost object field, divide rdx by 8, check if odd and
3874     // test if zero.
3875     __ xorl(rcx, rcx);    // use zero reg to clear memory (shorter code)
3876     __ shrl(rdx, LogBytesPerLong); // divide by 2*oopSize and set carry flag if odd
3877 
3878     // rdx must have been multiple of 8
3879 #ifdef ASSERT
3880     // make sure rdx was multiple of 8
3881     Label L;
3882     // Ignore partial flag stall after shrl() since it is debug VM
3883     __ jccb(Assembler::carryClear, L);
3884     __ stop("object size is not multiple of 2 - adjust this code");
3885     __ bind(L);
3886     // rdx must be > 0, no extra check needed here
3887 #endif
3888 
3889     // initialize remaining object fields: rdx was a multiple of 8
3890     { Label loop;
3891     __ bind(loop);
3892     __ movptr(Address(rax, rdx, Address::times_8, sizeof(oopDesc) - 1*oopSize), rcx);
3893     NOT_LP64(__ movptr(Address(rax, rdx, Address::times_8, sizeof(oopDesc) - 2*oopSize), rcx));
3894     __ decrement(rdx);
3895     __ jcc(Assembler::notZero, loop);
3896     }
3897 
3898     // initialize object header only.
3899     __ bind(initialize_header);
3900     if (UseBiasedLocking) {
3901       __ pop(rcx);   // get saved klass back in the register.
3902       __ movptr(rbx, Address(rcx, Klass::prototype_header_offset()));
3903       __ movptr(Address(rax, oopDesc::mark_offset_in_bytes ()), rbx);
3904     } else {
3905       __ movptr(Address(rax, oopDesc::mark_offset_in_bytes ()),
3906                 (intptr_t)markOopDesc::prototype()); // header
3907       __ pop(rcx);   // get saved klass back in the register.
3908     }
3909 #ifdef _LP64
3910     __ xorl(rsi, rsi); // use zero reg to clear memory (shorter code)
3911     __ store_klass_gap(rax, rsi);  // zero klass gap for compressed oops
3912 #endif
3913     __ store_klass(rax, rcx);  // klass
3914 
3915     {
3916       SkipIfEqual skip_if(_masm, &DTraceAllocProbes, 0);
3917       // Trigger dtrace event for fastpath
3918       __ push(atos);
3919       __ call_VM_leaf(
3920            CAST_FROM_FN_PTR(address, SharedRuntime::dtrace_object_alloc), rax);
3921       __ pop(atos);
3922     }
3923 
3924     __ jmp(done);
3925   }
3926 
3927   // slow case
3928   __ bind(slow_case);
3929   __ pop(rcx);   // restore stack pointer to what it was when we came in.
3930   __ bind(slow_case_no_pop);
3931 
3932   Register rarg1 = LP64_ONLY(c_rarg1) NOT_LP64(rax);
3933   Register rarg2 = LP64_ONLY(c_rarg2) NOT_LP64(rdx);
3934 
3935   __ get_constant_pool(rarg1);
3936   __ get_unsigned_2_byte_index_at_bcp(rarg2, 1);
3937   call_VM(rax, CAST_FROM_FN_PTR(address, InterpreterRuntime::_new), rarg1, rarg2);
3938    __ verify_oop(rax);
3939 
3940   // continue
3941   __ bind(done);
3942 }
3943 
3944 void TemplateTable::newarray() {
3945   transition(itos, atos);
3946   Register rarg1 = LP64_ONLY(c_rarg1) NOT_LP64(rdx);
3947   __ load_unsigned_byte(rarg1, at_bcp(1));
3948   call_VM(rax, CAST_FROM_FN_PTR(address, InterpreterRuntime::newarray),
3949           rarg1, rax);
3950 }
3951 
3952 void TemplateTable::anewarray() {
3953   transition(itos, atos);
3954 
3955   Register rarg1 = LP64_ONLY(c_rarg1) NOT_LP64(rcx);
3956   Register rarg2 = LP64_ONLY(c_rarg2) NOT_LP64(rdx);
3957 
3958   __ get_unsigned_2_byte_index_at_bcp(rarg2, 1);
3959   __ get_constant_pool(rarg1);
3960   call_VM(rax, CAST_FROM_FN_PTR(address, InterpreterRuntime::anewarray),
3961           rarg1, rarg2, rax);
3962 }
3963 
3964 void TemplateTable::arraylength() {
3965   transition(atos, itos);
3966   __ null_check(rax, arrayOopDesc::length_offset_in_bytes());
3967   __ movl(rax, Address(rax, arrayOopDesc::length_offset_in_bytes()));
3968 }
3969 
3970 void TemplateTable::checkcast() {
3971   transition(atos, atos);
3972   Label done, is_null, ok_is_subtype, quicked, resolved;
3973   __ testptr(rax, rax); // object is in rax
3974   __ jcc(Assembler::zero, is_null);
3975 
3976   // Get cpool & tags index
3977   __ get_cpool_and_tags(rcx, rdx); // rcx=cpool, rdx=tags array
3978   __ get_unsigned_2_byte_index_at_bcp(rbx, 1); // rbx=index
3979   // See if bytecode has already been quicked
3980   __ cmpb(Address(rdx, rbx,
3981                   Address::times_1,
3982                   Array<u1>::base_offset_in_bytes()),
3983           JVM_CONSTANT_Class);
3984   __ jcc(Assembler::equal, quicked);
3985   __ push(atos); // save receiver for result, and for GC
3986   call_VM(noreg, CAST_FROM_FN_PTR(address, InterpreterRuntime::quicken_io_cc));
3987 
3988   // vm_result_2 has metadata result
3989 #ifndef _LP64
3990   // borrow rdi from locals
3991   __ get_thread(rdi);
3992   __ get_vm_result_2(rax, rdi);
3993   __ restore_locals();
3994 #else
3995   __ get_vm_result_2(rax, r15_thread);
3996 #endif
3997 
3998   __ pop_ptr(rdx); // restore receiver
3999   __ jmpb(resolved);
4000 
4001   // Get superklass in rax and subklass in rbx
4002   __ bind(quicked);
4003   __ mov(rdx, rax); // Save object in rdx; rax needed for subtype check
4004   __ movptr(rax, Address(rcx, rbx,
4005                        Address::times_ptr, sizeof(ConstantPool)));
4006 
4007   __ bind(resolved);
4008   __ load_klass(rbx, rdx);
4009 
4010   // Generate subtype check.  Blows rcx, rdi.  Object in rdx.
4011   // Superklass in rax.  Subklass in rbx.
4012   __ gen_subtype_check(rbx, ok_is_subtype);
4013 
4014   // Come here on failure
4015   __ push_ptr(rdx);
4016   // object is at TOS
4017   __ jump(ExternalAddress(Interpreter::_throw_ClassCastException_entry));
4018 
4019   // Come here on success
4020   __ bind(ok_is_subtype);
4021   __ mov(rax, rdx); // Restore object in rdx
4022 
4023   // Collect counts on whether this check-cast sees NULLs a lot or not.
4024   if (ProfileInterpreter) {
4025     __ jmp(done);
4026     __ bind(is_null);
4027     __ profile_null_seen(rcx);
4028   } else {
4029     __ bind(is_null);   // same as 'done'
4030   }
4031   __ bind(done);
4032 }
4033 
4034 void TemplateTable::instanceof() {
4035   transition(atos, itos);
4036   Label done, is_null, ok_is_subtype, quicked, resolved;
4037   __ testptr(rax, rax);
4038   __ jcc(Assembler::zero, is_null);
4039 
4040   // Get cpool & tags index
4041   __ get_cpool_and_tags(rcx, rdx); // rcx=cpool, rdx=tags array
4042   __ get_unsigned_2_byte_index_at_bcp(rbx, 1); // rbx=index
4043   // See if bytecode has already been quicked
4044   __ cmpb(Address(rdx, rbx,
4045                   Address::times_1,
4046                   Array<u1>::base_offset_in_bytes()),
4047           JVM_CONSTANT_Class);
4048   __ jcc(Assembler::equal, quicked);
4049 
4050   __ push(atos); // save receiver for result, and for GC
4051   call_VM(noreg, CAST_FROM_FN_PTR(address, InterpreterRuntime::quicken_io_cc));
4052   // vm_result_2 has metadata result
4053 
4054 #ifndef _LP64
4055   // borrow rdi from locals
4056   __ get_thread(rdi);
4057   __ get_vm_result_2(rax, rdi);
4058   __ restore_locals();
4059 #else
4060   __ get_vm_result_2(rax, r15_thread);
4061 #endif
4062 
4063   __ pop_ptr(rdx); // restore receiver
4064   __ verify_oop(rdx);
4065   __ load_klass(rdx, rdx);
4066   __ jmpb(resolved);
4067 
4068   // Get superklass in rax and subklass in rdx
4069   __ bind(quicked);
4070   __ load_klass(rdx, rax);
4071   __ movptr(rax, Address(rcx, rbx,
4072                          Address::times_ptr, sizeof(ConstantPool)));
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 }