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