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