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