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