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