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