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