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::CardTableModRef:
 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], 0x8000000080000000, 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  = double_quadword(&double_signflip_pool[1], 0x8000000000000000, 0x8000000000000000);
1626     __ xorpd(xmm0, ExternalAddress((address) double_signflip));
1627   } else {
1628 #ifdef _LP64
1629     ShouldNotReachHere();
1630 #else
1631     __ fchs();
1632 #endif
1633   }
1634 }
1635 
1636 void TemplateTable::iinc() {
1637   transition(vtos, vtos);
1638   __ load_signed_byte(rdx, at_bcp(2)); // get constant
1639   locals_index(rbx);
1640   __ addl(iaddress(rbx), rdx);
1641 }
1642 
1643 void TemplateTable::wide_iinc() {
1644   transition(vtos, vtos);
1645   __ movl(rdx, at_bcp(4)); // get constant
1646   locals_index_wide(rbx);
1647   __ bswapl(rdx); // swap bytes & sign-extend constant
1648   __ sarl(rdx, 16);
1649   __ addl(iaddress(rbx), rdx);
1650   // Note: should probably use only one movl to get both
1651   //       the index and the constant -> fix this
1652 }
1653 
1654 void TemplateTable::convert() {
1655 #ifdef _LP64
1656   // Checking
1657 #ifdef ASSERT
1658   {
1659     TosState tos_in  = ilgl;
1660     TosState tos_out = ilgl;
1661     switch (bytecode()) {
1662     case Bytecodes::_i2l: // fall through
1663     case Bytecodes::_i2f: // fall through
1664     case Bytecodes::_i2d: // fall through
1665     case Bytecodes::_i2b: // fall through
1666     case Bytecodes::_i2c: // fall through
1667     case Bytecodes::_i2s: tos_in = itos; break;
1668     case Bytecodes::_l2i: // fall through
1669     case Bytecodes::_l2f: // fall through
1670     case Bytecodes::_l2d: tos_in = ltos; break;
1671     case Bytecodes::_f2i: // fall through
1672     case Bytecodes::_f2l: // fall through
1673     case Bytecodes::_f2d: tos_in = ftos; break;
1674     case Bytecodes::_d2i: // fall through
1675     case Bytecodes::_d2l: // fall through
1676     case Bytecodes::_d2f: tos_in = dtos; break;
1677     default             : ShouldNotReachHere();
1678     }
1679     switch (bytecode()) {
1680     case Bytecodes::_l2i: // fall through
1681     case Bytecodes::_f2i: // fall through
1682     case Bytecodes::_d2i: // fall through
1683     case Bytecodes::_i2b: // fall through
1684     case Bytecodes::_i2c: // fall through
1685     case Bytecodes::_i2s: tos_out = itos; break;
1686     case Bytecodes::_i2l: // fall through
1687     case Bytecodes::_f2l: // fall through
1688     case Bytecodes::_d2l: tos_out = ltos; break;
1689     case Bytecodes::_i2f: // fall through
1690     case Bytecodes::_l2f: // fall through
1691     case Bytecodes::_d2f: tos_out = ftos; break;
1692     case Bytecodes::_i2d: // fall through
1693     case Bytecodes::_l2d: // fall through
1694     case Bytecodes::_f2d: tos_out = dtos; break;
1695     default             : ShouldNotReachHere();
1696     }
1697     transition(tos_in, tos_out);
1698   }
1699 #endif // ASSERT
1700 
1701   static const int64_t is_nan = 0x8000000000000000L;
1702 
1703   // Conversion
1704   switch (bytecode()) {
1705   case Bytecodes::_i2l:
1706     __ movslq(rax, rax);
1707     break;
1708   case Bytecodes::_i2f:
1709     __ cvtsi2ssl(xmm0, rax);
1710     break;
1711   case Bytecodes::_i2d:
1712     __ cvtsi2sdl(xmm0, rax);
1713     break;
1714   case Bytecodes::_i2b:
1715     __ movsbl(rax, rax);
1716     break;
1717   case Bytecodes::_i2c:
1718     __ movzwl(rax, rax);
1719     break;
1720   case Bytecodes::_i2s:
1721     __ movswl(rax, rax);
1722     break;
1723   case Bytecodes::_l2i:
1724     __ movl(rax, rax);
1725     break;
1726   case Bytecodes::_l2f:
1727     __ cvtsi2ssq(xmm0, rax);
1728     break;
1729   case Bytecodes::_l2d:
1730     __ cvtsi2sdq(xmm0, rax);
1731     break;
1732   case Bytecodes::_f2i:
1733   {
1734     Label L;
1735     __ cvttss2sil(rax, xmm0);
1736     __ cmpl(rax, 0x80000000); // NaN or overflow/underflow?
1737     __ jcc(Assembler::notEqual, L);
1738     __ call_VM_leaf(CAST_FROM_FN_PTR(address, SharedRuntime::f2i), 1);
1739     __ bind(L);
1740   }
1741     break;
1742   case Bytecodes::_f2l:
1743   {
1744     Label L;
1745     __ cvttss2siq(rax, xmm0);
1746     // NaN or overflow/underflow?
1747     __ cmp64(rax, ExternalAddress((address) &is_nan));
1748     __ jcc(Assembler::notEqual, L);
1749     __ call_VM_leaf(CAST_FROM_FN_PTR(address, SharedRuntime::f2l), 1);
1750     __ bind(L);
1751   }
1752     break;
1753   case Bytecodes::_f2d:
1754     __ cvtss2sd(xmm0, xmm0);
1755     break;
1756   case Bytecodes::_d2i:
1757   {
1758     Label L;
1759     __ cvttsd2sil(rax, xmm0);
1760     __ cmpl(rax, 0x80000000); // NaN or overflow/underflow?
1761     __ jcc(Assembler::notEqual, L);
1762     __ call_VM_leaf(CAST_FROM_FN_PTR(address, SharedRuntime::d2i), 1);
1763     __ bind(L);
1764   }
1765     break;
1766   case Bytecodes::_d2l:
1767   {
1768     Label L;
1769     __ cvttsd2siq(rax, xmm0);
1770     // NaN or overflow/underflow?
1771     __ cmp64(rax, ExternalAddress((address) &is_nan));
1772     __ jcc(Assembler::notEqual, L);
1773     __ call_VM_leaf(CAST_FROM_FN_PTR(address, SharedRuntime::d2l), 1);
1774     __ bind(L);
1775   }
1776     break;
1777   case Bytecodes::_d2f:
1778     __ cvtsd2ss(xmm0, xmm0);
1779     break;
1780   default:
1781     ShouldNotReachHere();
1782   }
1783 #else
1784   // Checking
1785 #ifdef ASSERT
1786   { TosState tos_in  = ilgl;
1787     TosState tos_out = ilgl;
1788     switch (bytecode()) {
1789       case Bytecodes::_i2l: // fall through
1790       case Bytecodes::_i2f: // fall through
1791       case Bytecodes::_i2d: // fall through
1792       case Bytecodes::_i2b: // fall through
1793       case Bytecodes::_i2c: // fall through
1794       case Bytecodes::_i2s: tos_in = itos; break;
1795       case Bytecodes::_l2i: // fall through
1796       case Bytecodes::_l2f: // fall through
1797       case Bytecodes::_l2d: tos_in = ltos; break;
1798       case Bytecodes::_f2i: // fall through
1799       case Bytecodes::_f2l: // fall through
1800       case Bytecodes::_f2d: tos_in = ftos; break;
1801       case Bytecodes::_d2i: // fall through
1802       case Bytecodes::_d2l: // fall through
1803       case Bytecodes::_d2f: tos_in = dtos; break;
1804       default             : ShouldNotReachHere();
1805     }
1806     switch (bytecode()) {
1807       case Bytecodes::_l2i: // fall through
1808       case Bytecodes::_f2i: // fall through
1809       case Bytecodes::_d2i: // fall through
1810       case Bytecodes::_i2b: // fall through
1811       case Bytecodes::_i2c: // fall through
1812       case Bytecodes::_i2s: tos_out = itos; break;
1813       case Bytecodes::_i2l: // fall through
1814       case Bytecodes::_f2l: // fall through
1815       case Bytecodes::_d2l: tos_out = ltos; break;
1816       case Bytecodes::_i2f: // fall through
1817       case Bytecodes::_l2f: // fall through
1818       case Bytecodes::_d2f: tos_out = ftos; break;
1819       case Bytecodes::_i2d: // fall through
1820       case Bytecodes::_l2d: // fall through
1821       case Bytecodes::_f2d: tos_out = dtos; break;
1822       default             : ShouldNotReachHere();
1823     }
1824     transition(tos_in, tos_out);
1825   }
1826 #endif // ASSERT
1827 
1828   // Conversion
1829   // (Note: use push(rcx)/pop(rcx) for 1/2-word stack-ptr manipulation)
1830   switch (bytecode()) {
1831     case Bytecodes::_i2l:
1832       __ extend_sign(rdx, rax);
1833       break;
1834     case Bytecodes::_i2f:
1835       if (UseSSE >= 1) {
1836         __ cvtsi2ssl(xmm0, rax);
1837       } else {
1838         __ push(rax);          // store int on tos
1839         __ fild_s(at_rsp());   // load int to ST0
1840         __ f2ieee();           // truncate to float size
1841         __ pop(rcx);           // adjust rsp
1842       }
1843       break;
1844     case Bytecodes::_i2d:
1845       if (UseSSE >= 2) {
1846         __ cvtsi2sdl(xmm0, rax);
1847       } else {
1848       __ push(rax);          // add one slot for d2ieee()
1849       __ push(rax);          // store int on tos
1850       __ fild_s(at_rsp());   // load int to ST0
1851       __ d2ieee();           // truncate to double size
1852       __ pop(rcx);           // adjust rsp
1853       __ pop(rcx);
1854       }
1855       break;
1856     case Bytecodes::_i2b:
1857       __ shll(rax, 24);      // truncate upper 24 bits
1858       __ sarl(rax, 24);      // and sign-extend byte
1859       LP64_ONLY(__ movsbl(rax, rax));
1860       break;
1861     case Bytecodes::_i2c:
1862       __ andl(rax, 0xFFFF);  // truncate upper 16 bits
1863       LP64_ONLY(__ movzwl(rax, rax));
1864       break;
1865     case Bytecodes::_i2s:
1866       __ shll(rax, 16);      // truncate upper 16 bits
1867       __ sarl(rax, 16);      // and sign-extend short
1868       LP64_ONLY(__ movswl(rax, rax));
1869       break;
1870     case Bytecodes::_l2i:
1871       /* nothing to do */
1872       break;
1873     case Bytecodes::_l2f:
1874       // On 64-bit platforms, the cvtsi2ssq instruction is used to convert
1875       // 64-bit long values to floats. On 32-bit platforms it is not possible
1876       // to use that instruction with 64-bit operands, therefore the FPU is
1877       // used to perform the conversion.
1878       __ push(rdx);          // store long on tos
1879       __ push(rax);
1880       __ fild_d(at_rsp());   // load long to ST0
1881       __ f2ieee();           // truncate to float size
1882       __ pop(rcx);           // adjust rsp
1883       __ pop(rcx);
1884       if (UseSSE >= 1) {
1885         __ push_f();
1886         __ pop_f(xmm0);
1887       }
1888       break;
1889     case Bytecodes::_l2d:
1890       // On 32-bit platforms the FPU is used for conversion because on
1891       // 32-bit platforms it is not not possible to use the cvtsi2sdq
1892       // instruction with 64-bit operands.
1893       __ push(rdx);          // store long on tos
1894       __ push(rax);
1895       __ fild_d(at_rsp());   // load long to ST0
1896       __ d2ieee();           // truncate to double size
1897       __ pop(rcx);           // adjust rsp
1898       __ pop(rcx);
1899       if (UseSSE >= 2) {
1900         __ push_d();
1901         __ pop_d(xmm0);
1902       }
1903       break;
1904     case Bytecodes::_f2i:
1905       // SharedRuntime::f2i does not differentiate between sNaNs and qNaNs
1906       // as it returns 0 for any NaN.
1907       if (UseSSE >= 1) {
1908         __ push_f(xmm0);
1909       } else {
1910         __ push(rcx);          // reserve space for argument
1911         __ fstp_s(at_rsp());   // pass float argument on stack
1912       }
1913       __ call_VM_leaf(CAST_FROM_FN_PTR(address, SharedRuntime::f2i), 1);
1914       break;
1915     case Bytecodes::_f2l:
1916       // SharedRuntime::f2l does not differentiate between sNaNs and qNaNs
1917       // as it returns 0 for any NaN.
1918       if (UseSSE >= 1) {
1919        __ push_f(xmm0);
1920       } else {
1921         __ push(rcx);          // reserve space for argument
1922         __ fstp_s(at_rsp());   // pass float argument on stack
1923       }
1924       __ call_VM_leaf(CAST_FROM_FN_PTR(address, SharedRuntime::f2l), 1);
1925       break;
1926     case Bytecodes::_f2d:
1927       if (UseSSE < 1) {
1928         /* nothing to do */
1929       } else if (UseSSE == 1) {
1930         __ push_f(xmm0);
1931         __ pop_f();
1932       } else { // UseSSE >= 2
1933         __ cvtss2sd(xmm0, xmm0);
1934       }
1935       break;
1936     case Bytecodes::_d2i:
1937       if (UseSSE >= 2) {
1938         __ push_d(xmm0);
1939       } else {
1940         __ push(rcx);          // reserve space for argument
1941         __ push(rcx);
1942         __ fstp_d(at_rsp());   // pass double argument on stack
1943       }
1944       __ call_VM_leaf(CAST_FROM_FN_PTR(address, SharedRuntime::d2i), 2);
1945       break;
1946     case Bytecodes::_d2l:
1947       if (UseSSE >= 2) {
1948         __ push_d(xmm0);
1949       } else {
1950         __ push(rcx);          // reserve space for argument
1951         __ push(rcx);
1952         __ fstp_d(at_rsp());   // pass double argument on stack
1953       }
1954       __ call_VM_leaf(CAST_FROM_FN_PTR(address, SharedRuntime::d2l), 2);
1955       break;
1956     case Bytecodes::_d2f:
1957       if (UseSSE <= 1) {
1958         __ push(rcx);          // reserve space for f2ieee()
1959         __ f2ieee();           // truncate to float size
1960         __ pop(rcx);           // adjust rsp
1961         if (UseSSE == 1) {
1962           // The cvtsd2ss instruction is not available if UseSSE==1, therefore
1963           // the conversion is performed using the FPU in this case.
1964           __ push_f();
1965           __ pop_f(xmm0);
1966         }
1967       } else { // UseSSE >= 2
1968         __ cvtsd2ss(xmm0, xmm0);
1969       }
1970       break;
1971     default             :
1972       ShouldNotReachHere();
1973   }
1974 #endif
1975 }
1976 
1977 void TemplateTable::lcmp() {
1978   transition(ltos, itos);
1979 #ifdef _LP64
1980   Label done;
1981   __ pop_l(rdx);
1982   __ cmpq(rdx, rax);
1983   __ movl(rax, -1);
1984   __ jccb(Assembler::less, done);
1985   __ setb(Assembler::notEqual, rax);
1986   __ movzbl(rax, rax);
1987   __ bind(done);
1988 #else
1989 
1990   // y = rdx:rax
1991   __ pop_l(rbx, rcx);             // get x = rcx:rbx
1992   __ lcmp2int(rcx, rbx, rdx, rax);// rcx := cmp(x, y)
1993   __ mov(rax, rcx);
1994 #endif
1995 }
1996 
1997 void TemplateTable::float_cmp(bool is_float, int unordered_result) {
1998   if ((is_float && UseSSE >= 1) ||
1999       (!is_float && UseSSE >= 2)) {
2000     Label done;
2001     if (is_float) {
2002       // XXX get rid of pop here, use ... reg, mem32
2003       __ pop_f(xmm1);
2004       __ ucomiss(xmm1, xmm0);
2005     } else {
2006       // XXX get rid of pop here, use ... reg, mem64
2007       __ pop_d(xmm1);
2008       __ ucomisd(xmm1, xmm0);
2009     }
2010     if (unordered_result < 0) {
2011       __ movl(rax, -1);
2012       __ jccb(Assembler::parity, done);
2013       __ jccb(Assembler::below, done);
2014       __ setb(Assembler::notEqual, rdx);
2015       __ movzbl(rax, rdx);
2016     } else {
2017       __ movl(rax, 1);
2018       __ jccb(Assembler::parity, done);
2019       __ jccb(Assembler::above, done);
2020       __ movl(rax, 0);
2021       __ jccb(Assembler::equal, done);
2022       __ decrementl(rax);
2023     }
2024     __ bind(done);
2025   } else {
2026 #ifdef _LP64
2027     ShouldNotReachHere();
2028 #else
2029     if (is_float) {
2030       __ fld_s(at_rsp());
2031     } else {
2032       __ fld_d(at_rsp());
2033       __ pop(rdx);
2034     }
2035     __ pop(rcx);
2036     __ fcmp2int(rax, unordered_result < 0);
2037 #endif // _LP64
2038   }
2039 }
2040 
2041 void TemplateTable::branch(bool is_jsr, bool is_wide) {
2042   __ get_method(rcx); // rcx holds method
2043   __ profile_taken_branch(rax, rbx); // rax holds updated MDP, rbx
2044                                      // holds bumped taken count
2045 
2046   const ByteSize be_offset = MethodCounters::backedge_counter_offset() +
2047                              InvocationCounter::counter_offset();
2048   const ByteSize inv_offset = MethodCounters::invocation_counter_offset() +
2049                               InvocationCounter::counter_offset();
2050 
2051   // Load up edx with the branch displacement
2052   if (is_wide) {
2053     __ movl(rdx, at_bcp(1));
2054   } else {
2055     __ load_signed_short(rdx, at_bcp(1));
2056   }
2057   __ bswapl(rdx);
2058 
2059   if (!is_wide) {
2060     __ sarl(rdx, 16);
2061   }
2062   LP64_ONLY(__ movl2ptr(rdx, rdx));
2063 
2064   // Handle all the JSR stuff here, then exit.
2065   // It's much shorter and cleaner than intermingling with the non-JSR
2066   // normal-branch stuff occurring below.
2067   if (is_jsr) {
2068     // Pre-load the next target bytecode into rbx
2069     __ load_unsigned_byte(rbx, Address(rbcp, rdx, Address::times_1, 0));
2070 
2071     // compute return address as bci in rax
2072     __ lea(rax, at_bcp((is_wide ? 5 : 3) -
2073                         in_bytes(ConstMethod::codes_offset())));
2074     __ subptr(rax, Address(rcx, Method::const_offset()));
2075     // Adjust the bcp in r13 by the displacement in rdx
2076     __ addptr(rbcp, rdx);
2077     // jsr returns atos that is not an oop
2078     __ push_i(rax);
2079     __ dispatch_only(vtos);
2080     return;
2081   }
2082 
2083   // Normal (non-jsr) branch handling
2084 
2085   // Adjust the bcp in r13 by the displacement in rdx
2086   __ addptr(rbcp, rdx);
2087 
2088   assert(UseLoopCounter || !UseOnStackReplacement,
2089          "on-stack-replacement requires loop counters");
2090   Label backedge_counter_overflow;
2091   Label profile_method;
2092   Label dispatch;
2093   if (UseLoopCounter) {
2094     // increment backedge counter for backward branches
2095     // rax: MDO
2096     // rbx: MDO bumped taken-count
2097     // rcx: method
2098     // rdx: target offset
2099     // r13: target bcp
2100     // r14: locals pointer
2101     __ testl(rdx, rdx);             // check if forward or backward branch
2102     __ jcc(Assembler::positive, dispatch); // count only if backward branch
2103 
2104     // check if MethodCounters exists
2105     Label has_counters;
2106     __ movptr(rax, Address(rcx, Method::method_counters_offset()));
2107     __ testptr(rax, rax);
2108     __ jcc(Assembler::notZero, has_counters);
2109     __ push(rdx);
2110     __ push(rcx);
2111     __ call_VM(noreg, CAST_FROM_FN_PTR(address, InterpreterRuntime::build_method_counters),
2112                rcx);
2113     __ pop(rcx);
2114     __ pop(rdx);
2115     __ movptr(rax, Address(rcx, Method::method_counters_offset()));
2116     __ jcc(Assembler::zero, dispatch);
2117     __ bind(has_counters);
2118 
2119     if (TieredCompilation) {
2120       Label no_mdo;
2121       int increment = InvocationCounter::count_increment;
2122       if (ProfileInterpreter) {
2123         // Are we profiling?
2124         __ movptr(rbx, Address(rcx, in_bytes(Method::method_data_offset())));
2125         __ testptr(rbx, rbx);
2126         __ jccb(Assembler::zero, no_mdo);
2127         // Increment the MDO backedge counter
2128         const Address mdo_backedge_counter(rbx, in_bytes(MethodData::backedge_counter_offset()) +
2129                                            in_bytes(InvocationCounter::counter_offset()));
2130         const Address mask(rbx, in_bytes(MethodData::backedge_mask_offset()));
2131         __ increment_mask_and_jump(mdo_backedge_counter, increment, mask,
2132                                    rax, false, Assembler::zero, &backedge_counter_overflow);
2133         __ jmp(dispatch);
2134       }
2135       __ bind(no_mdo);
2136       // Increment backedge counter in MethodCounters*
2137       __ movptr(rcx, Address(rcx, Method::method_counters_offset()));
2138       const Address mask(rcx, in_bytes(MethodCounters::backedge_mask_offset()));
2139       __ increment_mask_and_jump(Address(rcx, be_offset), increment, mask,
2140                                  rax, false, Assembler::zero, &backedge_counter_overflow);
2141     } else { // not TieredCompilation
2142       // increment counter
2143       __ movptr(rcx, Address(rcx, Method::method_counters_offset()));
2144       __ movl(rax, Address(rcx, be_offset));        // load backedge counter
2145       __ incrementl(rax, InvocationCounter::count_increment); // increment counter
2146       __ movl(Address(rcx, be_offset), rax);        // store counter
2147 
2148       __ movl(rax, Address(rcx, inv_offset));    // load invocation counter
2149 
2150       __ andl(rax, InvocationCounter::count_mask_value); // and the status bits
2151       __ addl(rax, Address(rcx, be_offset));        // add both counters
2152 
2153       if (ProfileInterpreter) {
2154         // Test to see if we should create a method data oop
2155         __ cmp32(rax, Address(rcx, in_bytes(MethodCounters::interpreter_profile_limit_offset())));
2156         __ jcc(Assembler::less, dispatch);
2157 
2158         // if no method data exists, go to profile method
2159         __ test_method_data_pointer(rax, profile_method);
2160 
2161         if (UseOnStackReplacement) {
2162           // check for overflow against rbx which is the MDO taken count
2163           __ cmp32(rbx, Address(rcx, in_bytes(MethodCounters::interpreter_backward_branch_limit_offset())));
2164           __ jcc(Assembler::below, dispatch);
2165 
2166           // When ProfileInterpreter is on, the backedge_count comes
2167           // from the MethodData*, which value does not get reset on
2168           // the call to frequency_counter_overflow().  To avoid
2169           // excessive calls to the overflow routine while the method is
2170           // being compiled, add a second test to make sure the overflow
2171           // function is called only once every overflow_frequency.
2172           const int overflow_frequency = 1024;
2173           __ andl(rbx, overflow_frequency - 1);
2174           __ jcc(Assembler::zero, backedge_counter_overflow);
2175 
2176         }
2177       } else {
2178         if (UseOnStackReplacement) {
2179           // check for overflow against rax, which is the sum of the
2180           // counters
2181           __ cmp32(rax, Address(rcx, in_bytes(MethodCounters::interpreter_backward_branch_limit_offset())));
2182           __ jcc(Assembler::aboveEqual, backedge_counter_overflow);
2183 
2184         }
2185       }
2186     }
2187     __ bind(dispatch);
2188   }
2189 
2190   // Pre-load the next target bytecode into rbx
2191   __ load_unsigned_byte(rbx, Address(rbcp, 0));
2192 
2193   // continue with the bytecode @ target
2194   // rax: return bci for jsr's, unused otherwise
2195   // rbx: target bytecode
2196   // r13: target bcp
2197   __ dispatch_only(vtos);
2198 
2199   if (UseLoopCounter) {
2200     if (ProfileInterpreter) {
2201       // Out-of-line code to allocate method data oop.
2202       __ bind(profile_method);
2203       __ call_VM(noreg, CAST_FROM_FN_PTR(address, InterpreterRuntime::profile_method));
2204       __ load_unsigned_byte(rbx, Address(rbcp, 0));  // restore target bytecode
2205       __ set_method_data_pointer_for_bcp();
2206       __ jmp(dispatch);
2207     }
2208 
2209     if (UseOnStackReplacement) {
2210       // invocation counter overflow
2211       __ bind(backedge_counter_overflow);
2212       __ negptr(rdx);
2213       __ addptr(rdx, rbcp); // branch bcp
2214       // IcoResult frequency_counter_overflow([JavaThread*], address branch_bcp)
2215       __ call_VM(noreg,
2216                  CAST_FROM_FN_PTR(address,
2217                                   InterpreterRuntime::frequency_counter_overflow),
2218                  rdx);
2219       __ load_unsigned_byte(rbx, Address(rbcp, 0));  // restore target bytecode
2220 
2221       // rax: osr nmethod (osr ok) or NULL (osr not possible)
2222       // rbx: target bytecode
2223       // rdx: scratch
2224       // r14: locals pointer
2225       // r13: bcp
2226       __ testptr(rax, rax);                        // test result
2227       __ jcc(Assembler::zero, dispatch);         // no osr if null
2228       // nmethod may have been invalidated (VM may block upon call_VM return)
2229       __ cmpb(Address(rax, nmethod::state_offset()), nmethod::in_use);
2230       __ jcc(Assembler::notEqual, dispatch);
2231 
2232       // We have the address of an on stack replacement routine in rax
2233       // We need to prepare to execute the OSR method. First we must
2234       // migrate the locals and monitors off of the stack.
2235 
2236       LP64_ONLY(__ mov(r13, rax));                             // save the nmethod
2237       NOT_LP64(__ mov(rbx, rax));                             // save the nmethod
2238       NOT_LP64(__ get_thread(rcx));
2239 
2240       call_VM(noreg, CAST_FROM_FN_PTR(address, SharedRuntime::OSR_migration_begin));
2241 
2242       // rax is OSR buffer, move it to expected parameter location
2243       LP64_ONLY(__ mov(j_rarg0, rax));
2244       NOT_LP64(__ mov(rcx, rax));
2245       // We use j_rarg definitions here so that registers don't conflict as parameter
2246       // registers change across platforms as we are in the midst of a calling
2247       // sequence to the OSR nmethod and we don't want collision. These are NOT parameters.
2248 
2249       const Register retaddr   = LP64_ONLY(j_rarg2) NOT_LP64(rdi);
2250       const Register sender_sp = LP64_ONLY(j_rarg1) NOT_LP64(rdx);
2251 
2252 
2253       // pop the interpreter frame
2254       __ movptr(sender_sp, Address(rbp, frame::interpreter_frame_sender_sp_offset * wordSize)); // get sender sp
2255       __ leave();                                // remove frame anchor
2256       __ pop(retaddr);                           // get return address
2257       __ mov(rsp, sender_sp);                   // set sp to sender sp
2258       // Ensure compiled code always sees stack at proper alignment
2259       __ andptr(rsp, -(StackAlignmentInBytes));
2260 
2261       // unlike x86 we need no specialized return from compiled code
2262       // to the interpreter or the call stub.
2263 
2264       // push the return address
2265       __ push(retaddr);
2266 
2267       // and begin the OSR nmethod
2268       LP64_ONLY(__ jmp(Address(r13, nmethod::osr_entry_point_offset())));
2269       NOT_LP64(__ jmp(Address(rbx, nmethod::osr_entry_point_offset())));
2270     }
2271   }
2272 }
2273 
2274 void TemplateTable::if_0cmp(Condition cc) {
2275   transition(itos, vtos);
2276   // assume branch is more often taken than not (loops use backward branches)
2277   Label not_taken;
2278   __ testl(rax, rax);
2279   __ jcc(j_not(cc), not_taken);
2280   branch(false, false);
2281   __ bind(not_taken);
2282   __ profile_not_taken_branch(rax);
2283 }
2284 
2285 void TemplateTable::if_icmp(Condition cc) {
2286   transition(itos, vtos);
2287   // assume branch is more often taken than not (loops use backward branches)
2288   Label not_taken;
2289   __ pop_i(rdx);
2290   __ cmpl(rdx, rax);
2291   __ jcc(j_not(cc), not_taken);
2292   branch(false, false);
2293   __ bind(not_taken);
2294   __ profile_not_taken_branch(rax);
2295 }
2296 
2297 void TemplateTable::if_nullcmp(Condition cc) {
2298   transition(atos, vtos);
2299   // assume branch is more often taken than not (loops use backward branches)
2300   Label not_taken;
2301   __ testptr(rax, rax);
2302   __ jcc(j_not(cc), not_taken);
2303   branch(false, false);
2304   __ bind(not_taken);
2305   __ profile_not_taken_branch(rax);
2306 }
2307 
2308 void TemplateTable::if_acmp(Condition cc) {
2309   transition(atos, vtos);
2310   // assume branch is more often taken than not (loops use backward branches)
2311   Label not_taken;
2312   __ pop_ptr(rdx);
2313   __ cmpptr(rdx, rax);
2314   __ jcc(j_not(cc), not_taken);
2315   branch(false, false);
2316   __ bind(not_taken);
2317   __ profile_not_taken_branch(rax);
2318 }
2319 
2320 void TemplateTable::ret() {
2321   transition(vtos, vtos);
2322   locals_index(rbx);
2323   LP64_ONLY(__ movslq(rbx, iaddress(rbx))); // get return bci, compute return bcp
2324   NOT_LP64(__ movptr(rbx, iaddress(rbx)));
2325   __ profile_ret(rbx, rcx);
2326   __ get_method(rax);
2327   __ movptr(rbcp, Address(rax, Method::const_offset()));
2328   __ lea(rbcp, Address(rbcp, rbx, Address::times_1,
2329                       ConstMethod::codes_offset()));
2330   __ dispatch_next(vtos);
2331 }
2332 
2333 void TemplateTable::wide_ret() {
2334   transition(vtos, vtos);
2335   locals_index_wide(rbx);
2336   __ movptr(rbx, aaddress(rbx)); // get return bci, compute return bcp
2337   __ profile_ret(rbx, rcx);
2338   __ get_method(rax);
2339   __ movptr(rbcp, Address(rax, Method::const_offset()));
2340   __ lea(rbcp, Address(rbcp, rbx, Address::times_1, ConstMethod::codes_offset()));
2341   __ dispatch_next(vtos);
2342 }
2343 
2344 void TemplateTable::tableswitch() {
2345   Label default_case, continue_execution;
2346   transition(itos, vtos);
2347 
2348   // align r13/rsi
2349   __ lea(rbx, at_bcp(BytesPerInt));
2350   __ andptr(rbx, -BytesPerInt);
2351   // load lo & hi
2352   __ movl(rcx, Address(rbx, BytesPerInt));
2353   __ movl(rdx, Address(rbx, 2 * BytesPerInt));
2354   __ bswapl(rcx);
2355   __ bswapl(rdx);
2356   // check against lo & hi
2357   __ cmpl(rax, rcx);
2358   __ jcc(Assembler::less, default_case);
2359   __ cmpl(rax, rdx);
2360   __ jcc(Assembler::greater, default_case);
2361   // lookup dispatch offset
2362   __ subl(rax, rcx);
2363   __ movl(rdx, Address(rbx, rax, Address::times_4, 3 * BytesPerInt));
2364   __ profile_switch_case(rax, rbx, rcx);
2365   // continue execution
2366   __ bind(continue_execution);
2367   __ bswapl(rdx);
2368   LP64_ONLY(__ movl2ptr(rdx, rdx));
2369   __ load_unsigned_byte(rbx, Address(rbcp, rdx, Address::times_1));
2370   __ addptr(rbcp, rdx);
2371   __ dispatch_only(vtos);
2372   // handle default
2373   __ bind(default_case);
2374   __ profile_switch_default(rax);
2375   __ movl(rdx, Address(rbx, 0));
2376   __ jmp(continue_execution);
2377 }
2378 
2379 void TemplateTable::lookupswitch() {
2380   transition(itos, itos);
2381   __ stop("lookupswitch bytecode should have been rewritten");
2382 }
2383 
2384 void TemplateTable::fast_linearswitch() {
2385   transition(itos, vtos);
2386   Label loop_entry, loop, found, continue_execution;
2387   // bswap rax so we can avoid bswapping the table entries
2388   __ bswapl(rax);
2389   // align r13
2390   __ lea(rbx, at_bcp(BytesPerInt)); // btw: should be able to get rid of
2391                                     // this instruction (change offsets
2392                                     // below)
2393   __ andptr(rbx, -BytesPerInt);
2394   // set counter
2395   __ movl(rcx, Address(rbx, BytesPerInt));
2396   __ bswapl(rcx);
2397   __ jmpb(loop_entry);
2398   // table search
2399   __ bind(loop);
2400   __ cmpl(rax, Address(rbx, rcx, Address::times_8, 2 * BytesPerInt));
2401   __ jcc(Assembler::equal, found);
2402   __ bind(loop_entry);
2403   __ decrementl(rcx);
2404   __ jcc(Assembler::greaterEqual, loop);
2405   // default case
2406   __ profile_switch_default(rax);
2407   __ movl(rdx, Address(rbx, 0));
2408   __ jmp(continue_execution);
2409   // entry found -> get offset
2410   __ bind(found);
2411   __ movl(rdx, Address(rbx, rcx, Address::times_8, 3 * BytesPerInt));
2412   __ profile_switch_case(rcx, rax, rbx);
2413   // continue execution
2414   __ bind(continue_execution);
2415   __ bswapl(rdx);
2416   __ movl2ptr(rdx, rdx);
2417   __ load_unsigned_byte(rbx, Address(rbcp, rdx, Address::times_1));
2418   __ addptr(rbcp, rdx);
2419   __ dispatch_only(vtos);
2420 }
2421 
2422 void TemplateTable::fast_binaryswitch() {
2423   transition(itos, vtos);
2424   // Implementation using the following core algorithm:
2425   //
2426   // int binary_search(int key, LookupswitchPair* array, int n) {
2427   //   // Binary search according to "Methodik des Programmierens" by
2428   //   // Edsger W. Dijkstra and W.H.J. Feijen, Addison Wesley Germany 1985.
2429   //   int i = 0;
2430   //   int j = n;
2431   //   while (i+1 < j) {
2432   //     // invariant P: 0 <= i < j <= n and (a[i] <= key < a[j] or Q)
2433   //     // with      Q: for all i: 0 <= i < n: key < a[i]
2434   //     // where a stands for the array and assuming that the (inexisting)
2435   //     // element a[n] is infinitely big.
2436   //     int h = (i + j) >> 1;
2437   //     // i < h < j
2438   //     if (key < array[h].fast_match()) {
2439   //       j = h;
2440   //     } else {
2441   //       i = h;
2442   //     }
2443   //   }
2444   //   // R: a[i] <= key < a[i+1] or Q
2445   //   // (i.e., if key is within array, i is the correct index)
2446   //   return i;
2447   // }
2448 
2449   // Register allocation
2450   const Register key   = rax; // already set (tosca)
2451   const Register array = rbx;
2452   const Register i     = rcx;
2453   const Register j     = rdx;
2454   const Register h     = rdi;
2455   const Register temp  = rsi;
2456 
2457   // Find array start
2458   NOT_LP64(__ save_bcp());
2459 
2460   __ lea(array, at_bcp(3 * BytesPerInt)); // btw: should be able to
2461                                           // get rid of this
2462                                           // instruction (change
2463                                           // offsets below)
2464   __ andptr(array, -BytesPerInt);
2465 
2466   // Initialize i & j
2467   __ xorl(i, i);                            // i = 0;
2468   __ movl(j, Address(array, -BytesPerInt)); // j = length(array);
2469 
2470   // Convert j into native byteordering
2471   __ bswapl(j);
2472 
2473   // And start
2474   Label entry;
2475   __ jmp(entry);
2476 
2477   // binary search loop
2478   {
2479     Label loop;
2480     __ bind(loop);
2481     // int h = (i + j) >> 1;
2482     __ leal(h, Address(i, j, Address::times_1)); // h = i + j;
2483     __ sarl(h, 1);                               // h = (i + j) >> 1;
2484     // if (key < array[h].fast_match()) {
2485     //   j = h;
2486     // } else {
2487     //   i = h;
2488     // }
2489     // Convert array[h].match to native byte-ordering before compare
2490     __ movl(temp, Address(array, h, Address::times_8));
2491     __ bswapl(temp);
2492     __ cmpl(key, temp);
2493     // j = h if (key <  array[h].fast_match())
2494     __ cmov32(Assembler::less, j, h);
2495     // i = h if (key >= array[h].fast_match())
2496     __ cmov32(Assembler::greaterEqual, i, h);
2497     // while (i+1 < j)
2498     __ bind(entry);
2499     __ leal(h, Address(i, 1)); // i+1
2500     __ cmpl(h, j);             // i+1 < j
2501     __ jcc(Assembler::less, loop);
2502   }
2503 
2504   // end of binary search, result index is i (must check again!)
2505   Label default_case;
2506   // Convert array[i].match to native byte-ordering before compare
2507   __ movl(temp, Address(array, i, Address::times_8));
2508   __ bswapl(temp);
2509   __ cmpl(key, temp);
2510   __ jcc(Assembler::notEqual, default_case);
2511 
2512   // entry found -> j = offset
2513   __ movl(j , Address(array, i, Address::times_8, BytesPerInt));
2514   __ profile_switch_case(i, key, array);
2515   __ bswapl(j);
2516   LP64_ONLY(__ movslq(j, j));
2517 
2518   NOT_LP64(__ restore_bcp());
2519   NOT_LP64(__ restore_locals());                           // restore rdi
2520 
2521   __ load_unsigned_byte(rbx, Address(rbcp, j, Address::times_1));
2522   __ addptr(rbcp, j);
2523   __ dispatch_only(vtos);
2524 
2525   // default case -> j = default offset
2526   __ bind(default_case);
2527   __ profile_switch_default(i);
2528   __ movl(j, Address(array, -2 * BytesPerInt));
2529   __ bswapl(j);
2530   LP64_ONLY(__ movslq(j, j));
2531 
2532   NOT_LP64(__ restore_bcp());
2533   NOT_LP64(__ restore_locals());
2534 
2535   __ load_unsigned_byte(rbx, Address(rbcp, j, Address::times_1));
2536   __ addptr(rbcp, j);
2537   __ dispatch_only(vtos);
2538 }
2539 
2540 void TemplateTable::_return(TosState state) {
2541   transition(state, state);
2542 
2543   Register robj = LP64_ONLY(c_rarg1) NOT_LP64(rax);
2544 
2545   assert(_desc->calls_vm(),
2546          "inconsistent calls_vm information"); // call in remove_activation
2547 
2548   if (_desc->bytecode() == Bytecodes::_return_register_finalizer) {
2549     assert(state == vtos, "only valid state");
2550     __ movptr(robj, aaddress(0));
2551     __ load_klass(rdi, robj);
2552     __ movl(rdi, Address(rdi, Klass::access_flags_offset()));
2553     __ testl(rdi, JVM_ACC_HAS_FINALIZER);
2554     Label skip_register_finalizer;
2555     __ jcc(Assembler::zero, skip_register_finalizer);
2556 
2557     __ call_VM(noreg, CAST_FROM_FN_PTR(address, InterpreterRuntime::register_finalizer), robj);
2558 
2559     __ bind(skip_register_finalizer);
2560   }
2561 
2562   __ remove_activation(state, rbcp);
2563   __ jmp(rbcp);
2564 }
2565 
2566 // ----------------------------------------------------------------------------
2567 // Volatile variables demand their effects be made known to all CPU's
2568 // in order.  Store buffers on most chips allow reads & writes to
2569 // reorder; the JMM's ReadAfterWrite.java test fails in -Xint mode
2570 // without some kind of memory barrier (i.e., it's not sufficient that
2571 // the interpreter does not reorder volatile references, the hardware
2572 // also must not reorder them).
2573 //
2574 // According to the new Java Memory Model (JMM):
2575 // (1) All volatiles are serialized wrt to each other.  ALSO reads &
2576 //     writes act as aquire & release, so:
2577 // (2) A read cannot let unrelated NON-volatile memory refs that
2578 //     happen after the read float up to before the read.  It's OK for
2579 //     non-volatile memory refs that happen before the volatile read to
2580 //     float down below it.
2581 // (3) Similar a volatile write cannot let unrelated NON-volatile
2582 //     memory refs that happen BEFORE the write float down to after the
2583 //     write.  It's OK for non-volatile memory refs that happen after the
2584 //     volatile write to float up before it.
2585 //
2586 // We only put in barriers around volatile refs (they are expensive),
2587 // not _between_ memory refs (that would require us to track the
2588 // flavor of the previous memory refs).  Requirements (2) and (3)
2589 // require some barriers before volatile stores and after volatile
2590 // loads.  These nearly cover requirement (1) but miss the
2591 // volatile-store-volatile-load case.  This final case is placed after
2592 // volatile-stores although it could just as well go before
2593 // volatile-loads.
2594 
2595 void TemplateTable::volatile_barrier(Assembler::Membar_mask_bits order_constraint ) {
2596   // Helper function to insert a is-volatile test and memory barrier
2597   if(!os::is_MP()) return;    // Not needed on single CPU
2598   __ membar(order_constraint);
2599 }
2600 
2601 void TemplateTable::resolve_cache_and_index(int byte_no,
2602                                             Register Rcache,
2603                                             Register index,
2604                                             size_t index_size) {
2605   const Register temp = rbx;
2606   assert_different_registers(Rcache, index, temp);
2607 
2608   Label resolved;
2609 
2610   Bytecodes::Code code = bytecode();
2611   switch (code) {
2612   case Bytecodes::_nofast_getfield: code = Bytecodes::_getfield; break;
2613   case Bytecodes::_nofast_putfield: code = Bytecodes::_putfield; break;
2614   }
2615 
2616   assert(byte_no == f1_byte || byte_no == f2_byte, "byte_no out of range");
2617   __ get_cache_and_index_and_bytecode_at_bcp(Rcache, index, temp, byte_no, 1, index_size);
2618   __ cmpl(temp, code);  // have we resolved this bytecode?
2619   __ jcc(Assembler::equal, resolved);
2620 
2621   // resolve first time through
2622   address entry = CAST_FROM_FN_PTR(address, InterpreterRuntime::resolve_from_cache);
2623   __ movl(temp, code);
2624   __ call_VM(noreg, entry, temp);
2625   // Update registers with resolved info
2626   __ get_cache_and_index_at_bcp(Rcache, index, 1, index_size);
2627   __ bind(resolved);
2628 }
2629 
2630 // The cache and index registers must be set before call
2631 void TemplateTable::load_field_cp_cache_entry(Register obj,
2632                                               Register cache,
2633                                               Register index,
2634                                               Register off,
2635                                               Register flags,
2636                                               bool is_static = false) {
2637   assert_different_registers(cache, index, flags, off);
2638 
2639   ByteSize cp_base_offset = ConstantPoolCache::base_offset();
2640   // Field offset
2641   __ movptr(off, Address(cache, index, Address::times_ptr,
2642                          in_bytes(cp_base_offset +
2643                                   ConstantPoolCacheEntry::f2_offset())));
2644   // Flags
2645   __ movl(flags, Address(cache, index, Address::times_ptr,
2646                          in_bytes(cp_base_offset +
2647                                   ConstantPoolCacheEntry::flags_offset())));
2648 
2649   // klass overwrite register
2650   if (is_static) {
2651     __ movptr(obj, Address(cache, index, Address::times_ptr,
2652                            in_bytes(cp_base_offset +
2653                                     ConstantPoolCacheEntry::f1_offset())));
2654     const int mirror_offset = in_bytes(Klass::java_mirror_offset());
2655     __ movptr(obj, Address(obj, mirror_offset));
2656   }
2657 }
2658 
2659 void TemplateTable::load_invoke_cp_cache_entry(int byte_no,
2660                                                Register method,
2661                                                Register itable_index,
2662                                                Register flags,
2663                                                bool is_invokevirtual,
2664                                                bool is_invokevfinal, /*unused*/
2665                                                bool is_invokedynamic) {
2666   // setup registers
2667   const Register cache = rcx;
2668   const Register index = rdx;
2669   assert_different_registers(method, flags);
2670   assert_different_registers(method, cache, index);
2671   assert_different_registers(itable_index, flags);
2672   assert_different_registers(itable_index, cache, index);
2673   // determine constant pool cache field offsets
2674   assert(is_invokevirtual == (byte_no == f2_byte), "is_invokevirtual flag redundant");
2675   const int method_offset = in_bytes(
2676     ConstantPoolCache::base_offset() +
2677       ((byte_no == f2_byte)
2678        ? ConstantPoolCacheEntry::f2_offset()
2679        : ConstantPoolCacheEntry::f1_offset()));
2680   const int flags_offset = in_bytes(ConstantPoolCache::base_offset() +
2681                                     ConstantPoolCacheEntry::flags_offset());
2682   // access constant pool cache fields
2683   const int index_offset = in_bytes(ConstantPoolCache::base_offset() +
2684                                     ConstantPoolCacheEntry::f2_offset());
2685 
2686   size_t index_size = (is_invokedynamic ? sizeof(u4) : sizeof(u2));
2687   resolve_cache_and_index(byte_no, cache, index, index_size);
2688     __ movptr(method, Address(cache, index, Address::times_ptr, method_offset));
2689 
2690   if (itable_index != noreg) {
2691     // pick up itable or appendix index from f2 also:
2692     __ movptr(itable_index, Address(cache, index, Address::times_ptr, index_offset));
2693   }
2694   __ movl(flags, Address(cache, index, Address::times_ptr, flags_offset));
2695 }
2696 
2697 // The registers cache and index expected to be set before call.
2698 // Correct values of the cache and index registers are preserved.
2699 void TemplateTable::jvmti_post_field_access(Register cache,
2700                                             Register index,
2701                                             bool is_static,
2702                                             bool has_tos) {
2703   if (JvmtiExport::can_post_field_access()) {
2704     // Check to see if a field access watch has been set before we take
2705     // the time to call into the VM.
2706     Label L1;
2707     assert_different_registers(cache, index, rax);
2708     __ mov32(rax, ExternalAddress((address) JvmtiExport::get_field_access_count_addr()));
2709     __ testl(rax,rax);
2710     __ jcc(Assembler::zero, L1);
2711 
2712     // cache entry pointer
2713     __ addptr(cache, in_bytes(ConstantPoolCache::base_offset()));
2714     __ shll(index, LogBytesPerWord);
2715     __ addptr(cache, index);
2716     if (is_static) {
2717       __ xorptr(rax, rax);      // NULL object reference
2718     } else {
2719       __ pop(atos);         // Get the object
2720       __ verify_oop(rax);
2721       __ push(atos);        // Restore stack state
2722     }
2723     // rax,:   object pointer or NULL
2724     // cache: cache entry pointer
2725     __ call_VM(noreg, CAST_FROM_FN_PTR(address, InterpreterRuntime::post_field_access),
2726                rax, cache);
2727     __ get_cache_and_index_at_bcp(cache, index, 1);
2728     __ bind(L1);
2729   }
2730 }
2731 
2732 void TemplateTable::pop_and_check_object(Register r) {
2733   __ pop_ptr(r);
2734   __ null_check(r);  // for field access must check obj.
2735   __ verify_oop(r);
2736 }
2737 
2738 void TemplateTable::getfield_or_static(int byte_no, bool is_static, RewriteControl rc) {
2739   transition(vtos, vtos);
2740 
2741   const Register cache = rcx;
2742   const Register index = rdx;
2743   const Register obj   = LP64_ONLY(c_rarg3) NOT_LP64(rcx);
2744   const Register off   = rbx;
2745   const Register flags = rax;
2746   const Register bc    = LP64_ONLY(c_rarg3) NOT_LP64(rcx); // uses same reg as obj, so don't mix them
2747 
2748   resolve_cache_and_index(byte_no, cache, index, sizeof(u2));
2749   jvmti_post_field_access(cache, index, is_static, false);
2750   load_field_cp_cache_entry(obj, cache, index, off, flags, is_static);
2751 
2752   if (!is_static) pop_and_check_object(obj);
2753 
2754   const Address field(obj, off, Address::times_1, 0*wordSize);
2755   NOT_LP64(const Address hi(obj, off, Address::times_1, 1*wordSize));
2756 
2757   Label Done, notByte, notInt, notShort, notChar, notLong, notFloat, notObj, notDouble;
2758 
2759   __ shrl(flags, ConstantPoolCacheEntry::tos_state_shift);
2760   // Make sure we don't need to mask edx after the above shift
2761   assert(btos == 0, "change code, btos != 0");
2762 
2763   __ andl(flags, ConstantPoolCacheEntry::tos_state_mask);
2764 
2765   __ jcc(Assembler::notZero, notByte);
2766   // btos
2767   __ load_signed_byte(rax, field);
2768   __ push(btos);
2769   // Rewrite bytecode to be faster
2770   if (!is_static && rc == may_rewrite) {
2771     patch_bytecode(Bytecodes::_fast_bgetfield, bc, rbx);
2772   }
2773   __ jmp(Done);
2774 
2775   __ bind(notByte);
2776   __ cmpl(flags, atos);
2777   __ jcc(Assembler::notEqual, notObj);
2778   // atos
2779   __ load_heap_oop(rax, field);
2780   __ push(atos);
2781   if (!is_static && rc == may_rewrite) {
2782     patch_bytecode(Bytecodes::_fast_agetfield, bc, rbx);
2783   }
2784   __ jmp(Done);
2785 
2786   __ bind(notObj);
2787   __ cmpl(flags, itos);
2788   __ jcc(Assembler::notEqual, notInt);
2789   // itos
2790   __ movl(rax, field);
2791   __ push(itos);
2792   // Rewrite bytecode to be faster
2793   if (!is_static && rc == may_rewrite) {
2794     patch_bytecode(Bytecodes::_fast_igetfield, bc, rbx);
2795   }
2796   __ jmp(Done);
2797 
2798   __ bind(notInt);
2799   __ cmpl(flags, ctos);
2800   __ jcc(Assembler::notEqual, notChar);
2801   // ctos
2802   __ load_unsigned_short(rax, field);
2803   __ push(ctos);
2804   // Rewrite bytecode to be faster
2805   if (!is_static && rc == may_rewrite) {
2806     patch_bytecode(Bytecodes::_fast_cgetfield, bc, rbx);
2807   }
2808   __ jmp(Done);
2809 
2810   __ bind(notChar);
2811   __ cmpl(flags, stos);
2812   __ jcc(Assembler::notEqual, notShort);
2813   // stos
2814   __ load_signed_short(rax, field);
2815   __ push(stos);
2816   // Rewrite bytecode to be faster
2817   if (!is_static && rc == may_rewrite) {
2818     patch_bytecode(Bytecodes::_fast_sgetfield, bc, rbx);
2819   }
2820   __ jmp(Done);
2821 
2822   __ bind(notShort);
2823   __ cmpl(flags, ltos);
2824   __ jcc(Assembler::notEqual, notLong);
2825   // ltos
2826 
2827 #ifndef _LP64
2828   // Generate code as if volatile.  There just aren't enough registers to
2829   // save that information and this code is faster than the test.
2830   __ fild_d(field);                // Must load atomically
2831   __ subptr(rsp,2*wordSize);    // Make space for store
2832   __ fistp_d(Address(rsp,0));
2833   __ pop(rax);
2834   __ pop(rdx);
2835 #else
2836   __ movq(rax, field);
2837 #endif
2838 
2839   __ push(ltos);
2840   // Rewrite bytecode to be faster
2841   LP64_ONLY(if (!is_static && rc == may_rewrite) patch_bytecode(Bytecodes::_fast_lgetfield, bc, rbx));
2842   __ jmp(Done);
2843 
2844   __ bind(notLong);
2845   __ cmpl(flags, ftos);
2846   __ jcc(Assembler::notEqual, notFloat);
2847   // ftos
2848 
2849   __ load_float(field);
2850   __ push(ftos);
2851   // Rewrite bytecode to be faster
2852   if (!is_static && rc == may_rewrite) {
2853     patch_bytecode(Bytecodes::_fast_fgetfield, bc, rbx);
2854   }
2855   __ jmp(Done);
2856 
2857   __ bind(notFloat);
2858 #ifdef ASSERT
2859   __ cmpl(flags, dtos);
2860   __ jcc(Assembler::notEqual, notDouble);
2861 #endif
2862   // dtos
2863   __ load_double(field);
2864   __ push(dtos);
2865   // Rewrite bytecode to be faster
2866   if (!is_static && rc == may_rewrite) {
2867     patch_bytecode(Bytecodes::_fast_dgetfield, bc, rbx);
2868   }
2869 #ifdef ASSERT
2870   __ jmp(Done);
2871 
2872 
2873   __ bind(notDouble);
2874   __ stop("Bad state");
2875 #endif
2876 
2877   __ bind(Done);
2878   // [jk] not needed currently
2879   // volatile_barrier(Assembler::Membar_mask_bits(Assembler::LoadLoad |
2880   //                                              Assembler::LoadStore));
2881 }
2882 
2883 void TemplateTable::getfield(int byte_no) {
2884   getfield_or_static(byte_no, false);
2885 }
2886 
2887 void TemplateTable::nofast_getfield(int byte_no) {
2888   getfield_or_static(byte_no, false, may_not_rewrite);
2889 }
2890 
2891 void TemplateTable::getstatic(int byte_no) {
2892   getfield_or_static(byte_no, true);
2893 }
2894 
2895 
2896 // The registers cache and index expected to be set before call.
2897 // The function may destroy various registers, just not the cache and index registers.
2898 void TemplateTable::jvmti_post_field_mod(Register cache, Register index, bool is_static) {
2899 
2900   const Register robj = LP64_ONLY(c_rarg2)   NOT_LP64(rax);
2901   const Register RBX  = LP64_ONLY(c_rarg1)   NOT_LP64(rbx);
2902   const Register RCX  = LP64_ONLY(c_rarg3)   NOT_LP64(rcx);
2903   const Register RDX  = LP64_ONLY(rscratch1) NOT_LP64(rdx);
2904 
2905   ByteSize cp_base_offset = ConstantPoolCache::base_offset();
2906 
2907   if (JvmtiExport::can_post_field_modification()) {
2908     // Check to see if a field modification watch has been set before
2909     // we take the time to call into the VM.
2910     Label L1;
2911     assert_different_registers(cache, index, rax);
2912     __ mov32(rax, ExternalAddress((address)JvmtiExport::get_field_modification_count_addr()));
2913     __ testl(rax, rax);
2914     __ jcc(Assembler::zero, L1);
2915 
2916     __ get_cache_and_index_at_bcp(robj, RDX, 1);
2917 
2918 
2919     if (is_static) {
2920       // Life is simple.  Null out the object pointer.
2921       __ xorl(RBX, RBX);
2922 
2923     } else {
2924       // Life is harder. The stack holds the value on top, followed by
2925       // the object.  We don't know the size of the value, though; it
2926       // could be one or two words depending on its type. As a result,
2927       // we must find the type to determine where the object is.
2928 #ifndef _LP64
2929       Label two_word, valsize_known;
2930 #endif
2931       __ movl(RCX, Address(robj, RDX,
2932                            Address::times_ptr,
2933                            in_bytes(cp_base_offset +
2934                                      ConstantPoolCacheEntry::flags_offset())));
2935       NOT_LP64(__ mov(rbx, rsp));
2936       __ shrl(RCX, ConstantPoolCacheEntry::tos_state_shift);
2937 
2938       // Make sure we don't need to mask rcx after the above shift
2939       ConstantPoolCacheEntry::verify_tos_state_shift();
2940 #ifdef _LP64
2941       __ movptr(c_rarg1, at_tos_p1());  // initially assume a one word jvalue
2942       __ cmpl(c_rarg3, ltos);
2943       __ cmovptr(Assembler::equal,
2944                  c_rarg1, at_tos_p2()); // ltos (two word jvalue)
2945       __ cmpl(c_rarg3, dtos);
2946       __ cmovptr(Assembler::equal,
2947                  c_rarg1, at_tos_p2()); // dtos (two word jvalue)
2948 #else
2949       __ cmpl(rcx, ltos);
2950       __ jccb(Assembler::equal, two_word);
2951       __ cmpl(rcx, dtos);
2952       __ jccb(Assembler::equal, two_word);
2953       __ addptr(rbx, Interpreter::expr_offset_in_bytes(1)); // one word jvalue (not ltos, dtos)
2954       __ jmpb(valsize_known);
2955 
2956       __ bind(two_word);
2957       __ addptr(rbx, Interpreter::expr_offset_in_bytes(2)); // two words jvalue
2958 
2959       __ bind(valsize_known);
2960       // setup object pointer
2961       __ movptr(rbx, Address(rbx, 0));
2962 #endif
2963     }
2964     // cache entry pointer
2965     __ addptr(robj, in_bytes(cp_base_offset));
2966     __ shll(RDX, LogBytesPerWord);
2967     __ addptr(robj, RDX);
2968     // object (tos)
2969     __ mov(RCX, rsp);
2970     // c_rarg1: object pointer set up above (NULL if static)
2971     // c_rarg2: cache entry pointer
2972     // c_rarg3: jvalue object on the stack
2973     __ call_VM(noreg,
2974                CAST_FROM_FN_PTR(address,
2975                                 InterpreterRuntime::post_field_modification),
2976                RBX, robj, RCX);
2977     __ get_cache_and_index_at_bcp(cache, index, 1);
2978     __ bind(L1);
2979   }
2980 }
2981 
2982 void TemplateTable::putfield_or_static(int byte_no, bool is_static, RewriteControl rc) {
2983   transition(vtos, vtos);
2984 
2985   const Register cache = rcx;
2986   const Register index = rdx;
2987   const Register obj   = rcx;
2988   const Register off   = rbx;
2989   const Register flags = rax;
2990   const Register bc    = LP64_ONLY(c_rarg3) NOT_LP64(rcx);
2991 
2992   resolve_cache_and_index(byte_no, cache, index, sizeof(u2));
2993   jvmti_post_field_mod(cache, index, is_static);
2994   load_field_cp_cache_entry(obj, cache, index, off, flags, is_static);
2995 
2996   // [jk] not needed currently
2997   // volatile_barrier(Assembler::Membar_mask_bits(Assembler::LoadStore |
2998   //                                              Assembler::StoreStore));
2999 
3000   Label notVolatile, Done;
3001   __ movl(rdx, flags);
3002   __ shrl(rdx, ConstantPoolCacheEntry::is_volatile_shift);
3003   __ andl(rdx, 0x1);
3004 
3005   // field addresses
3006   const Address field(obj, off, Address::times_1, 0*wordSize);
3007   NOT_LP64( const Address hi(obj, off, Address::times_1, 1*wordSize);)
3008 
3009   Label notByte, notInt, notShort, notChar,
3010         notLong, notFloat, notObj, notDouble;
3011 
3012   __ shrl(flags, ConstantPoolCacheEntry::tos_state_shift);
3013 
3014   assert(btos == 0, "change code, btos != 0");
3015   __ andl(flags, ConstantPoolCacheEntry::tos_state_mask);
3016   __ jcc(Assembler::notZero, notByte);
3017 
3018   // btos
3019   {
3020     __ pop(btos);
3021     if (!is_static) pop_and_check_object(obj);
3022     __ movb(field, rax);
3023     if (!is_static && rc == may_rewrite) {
3024       patch_bytecode(Bytecodes::_fast_bputfield, bc, rbx, true, byte_no);
3025     }
3026     __ jmp(Done);
3027   }
3028 
3029   __ bind(notByte);
3030   __ cmpl(flags, atos);
3031   __ jcc(Assembler::notEqual, notObj);
3032 
3033   // atos
3034   {
3035     __ pop(atos);
3036     if (!is_static) pop_and_check_object(obj);
3037     // Store into the field
3038     do_oop_store(_masm, field, rax, _bs->kind(), false);
3039     if (!is_static && rc == may_rewrite) {
3040       patch_bytecode(Bytecodes::_fast_aputfield, bc, rbx, true, byte_no);
3041     }
3042     __ jmp(Done);
3043   }
3044 
3045   __ bind(notObj);
3046   __ cmpl(flags, itos);
3047   __ jcc(Assembler::notEqual, notInt);
3048 
3049   // itos
3050   {
3051     __ pop(itos);
3052     if (!is_static) pop_and_check_object(obj);
3053     __ movl(field, rax);
3054     if (!is_static && rc == may_rewrite) {
3055       patch_bytecode(Bytecodes::_fast_iputfield, bc, rbx, true, byte_no);
3056     }
3057     __ jmp(Done);
3058   }
3059 
3060   __ bind(notInt);
3061   __ cmpl(flags, ctos);
3062   __ jcc(Assembler::notEqual, notChar);
3063 
3064   // ctos
3065   {
3066     __ pop(ctos);
3067     if (!is_static) pop_and_check_object(obj);
3068     __ movw(field, rax);
3069     if (!is_static && rc == may_rewrite) {
3070       patch_bytecode(Bytecodes::_fast_cputfield, bc, rbx, true, byte_no);
3071     }
3072     __ jmp(Done);
3073   }
3074 
3075   __ bind(notChar);
3076   __ cmpl(flags, stos);
3077   __ jcc(Assembler::notEqual, notShort);
3078 
3079   // stos
3080   {
3081     __ pop(stos);
3082     if (!is_static) pop_and_check_object(obj);
3083     __ movw(field, rax);
3084     if (!is_static && rc == may_rewrite) {
3085       patch_bytecode(Bytecodes::_fast_sputfield, bc, rbx, true, byte_no);
3086     }
3087     __ jmp(Done);
3088   }
3089 
3090   __ bind(notShort);
3091   __ cmpl(flags, ltos);
3092   __ jcc(Assembler::notEqual, notLong);
3093 
3094   // ltos
3095 #ifdef _LP64
3096   {
3097     __ pop(ltos);
3098     if (!is_static) pop_and_check_object(obj);
3099     __ movq(field, rax);
3100     if (!is_static && rc == may_rewrite) {
3101       patch_bytecode(Bytecodes::_fast_lputfield, bc, rbx, true, byte_no);
3102     }
3103     __ jmp(Done);
3104   }
3105 #else
3106   {
3107     Label notVolatileLong;
3108     __ testl(rdx, rdx);
3109     __ jcc(Assembler::zero, notVolatileLong);
3110 
3111     __ pop(ltos);  // overwrites rdx, do this after testing volatile.
3112     if (!is_static) pop_and_check_object(obj);
3113 
3114     // Replace with real volatile test
3115     __ push(rdx);
3116     __ push(rax);                 // Must update atomically with FIST
3117     __ fild_d(Address(rsp,0));    // So load into FPU register
3118     __ fistp_d(field);            // and put into memory atomically
3119     __ addptr(rsp, 2*wordSize);
3120     // volatile_barrier();
3121     volatile_barrier(Assembler::Membar_mask_bits(Assembler::StoreLoad |
3122                                                  Assembler::StoreStore));
3123     // Don't rewrite volatile version
3124     __ jmp(notVolatile);
3125 
3126     __ bind(notVolatileLong);
3127 
3128     __ pop(ltos);  // overwrites rdx
3129     if (!is_static) pop_and_check_object(obj);
3130     __ movptr(hi, rdx);
3131     __ movptr(field, rax);
3132     // Don't rewrite to _fast_lputfield for potential volatile case.
3133     __ jmp(notVolatile);
3134   }
3135 #endif // _LP64
3136 
3137   __ bind(notLong);
3138   __ cmpl(flags, ftos);
3139   __ jcc(Assembler::notEqual, notFloat);
3140 
3141   // ftos
3142   {
3143     __ pop(ftos);
3144     if (!is_static) pop_and_check_object(obj);
3145     __ store_float(field);
3146     if (!is_static && rc == may_rewrite) {
3147       patch_bytecode(Bytecodes::_fast_fputfield, bc, rbx, true, byte_no);
3148     }
3149     __ jmp(Done);
3150   }
3151 
3152   __ bind(notFloat);
3153 #ifdef ASSERT
3154   __ cmpl(flags, dtos);
3155   __ jcc(Assembler::notEqual, notDouble);
3156 #endif
3157 
3158   // dtos
3159   {
3160     __ pop(dtos);
3161     if (!is_static) pop_and_check_object(obj);
3162     __ store_double(field);
3163     if (!is_static && rc == may_rewrite) {
3164       patch_bytecode(Bytecodes::_fast_dputfield, bc, rbx, true, byte_no);
3165     }
3166   }
3167 
3168 #ifdef ASSERT
3169   __ jmp(Done);
3170 
3171   __ bind(notDouble);
3172   __ stop("Bad state");
3173 #endif
3174 
3175   __ bind(Done);
3176 
3177   // Check for volatile store
3178   __ testl(rdx, rdx);
3179   __ jcc(Assembler::zero, notVolatile);
3180   volatile_barrier(Assembler::Membar_mask_bits(Assembler::StoreLoad |
3181                                                Assembler::StoreStore));
3182   __ bind(notVolatile);
3183 }
3184 
3185 void TemplateTable::putfield(int byte_no) {
3186   putfield_or_static(byte_no, false);
3187 }
3188 
3189 void TemplateTable::nofast_putfield(int byte_no) {
3190   putfield_or_static(byte_no, false, may_not_rewrite);
3191 }
3192 
3193 void TemplateTable::putstatic(int byte_no) {
3194   putfield_or_static(byte_no, true);
3195 }
3196 
3197 void TemplateTable::jvmti_post_fast_field_mod() {
3198 
3199   const Register scratch = LP64_ONLY(c_rarg3) NOT_LP64(rcx);
3200 
3201   if (JvmtiExport::can_post_field_modification()) {
3202     // Check to see if a field modification watch has been set before
3203     // we take the time to call into the VM.
3204     Label L2;
3205     __ mov32(scratch, ExternalAddress((address)JvmtiExport::get_field_modification_count_addr()));
3206     __ testl(scratch, scratch);
3207     __ jcc(Assembler::zero, L2);
3208     __ pop_ptr(rbx);                  // copy the object pointer from tos
3209     __ verify_oop(rbx);
3210     __ push_ptr(rbx);                 // put the object pointer back on tos
3211     // Save tos values before call_VM() clobbers them. Since we have
3212     // to do it for every data type, we use the saved values as the
3213     // jvalue object.
3214     switch (bytecode()) {          // load values into the jvalue object
3215     case Bytecodes::_fast_aputfield: __ push_ptr(rax); break;
3216     case Bytecodes::_fast_bputfield: // fall through
3217     case Bytecodes::_fast_sputfield: // fall through
3218     case Bytecodes::_fast_cputfield: // fall through
3219     case Bytecodes::_fast_iputfield: __ push_i(rax); break;
3220     case Bytecodes::_fast_dputfield: __ push(dtos); break;
3221     case Bytecodes::_fast_fputfield: __ push(ftos); break;
3222     case Bytecodes::_fast_lputfield: __ push_l(rax); break;
3223 
3224     default:
3225       ShouldNotReachHere();
3226     }
3227     __ mov(scratch, rsp);             // points to jvalue on the stack
3228     // access constant pool cache entry
3229     LP64_ONLY(__ get_cache_entry_pointer_at_bcp(c_rarg2, rax, 1));
3230     NOT_LP64(__ get_cache_entry_pointer_at_bcp(rax, rdx, 1));
3231     __ verify_oop(rbx);
3232     // rbx: object pointer copied above
3233     // c_rarg2: cache entry pointer
3234     // c_rarg3: jvalue object on the stack
3235     LP64_ONLY(__ call_VM(noreg, CAST_FROM_FN_PTR(address, InterpreterRuntime::post_field_modification), rbx, c_rarg2, c_rarg3));
3236     NOT_LP64(__ call_VM(noreg, CAST_FROM_FN_PTR(address, InterpreterRuntime::post_field_modification), rbx, rax, rcx));
3237 
3238     switch (bytecode()) {             // restore tos values
3239     case Bytecodes::_fast_aputfield: __ pop_ptr(rax); break;
3240     case Bytecodes::_fast_bputfield: // fall through
3241     case Bytecodes::_fast_sputfield: // fall through
3242     case Bytecodes::_fast_cputfield: // fall through
3243     case Bytecodes::_fast_iputfield: __ pop_i(rax); break;
3244     case Bytecodes::_fast_dputfield: __ pop(dtos); break;
3245     case Bytecodes::_fast_fputfield: __ pop(ftos); break;
3246     case Bytecodes::_fast_lputfield: __ pop_l(rax); break;
3247     }
3248     __ bind(L2);
3249   }
3250 }
3251 
3252 void TemplateTable::fast_storefield(TosState state) {
3253   transition(state, vtos);
3254 
3255   ByteSize base = ConstantPoolCache::base_offset();
3256 
3257   jvmti_post_fast_field_mod();
3258 
3259   // access constant pool cache
3260   __ get_cache_and_index_at_bcp(rcx, rbx, 1);
3261 
3262   // test for volatile with rdx but rdx is tos register for lputfield.
3263   __ movl(rdx, Address(rcx, rbx, Address::times_ptr,
3264                        in_bytes(base +
3265                                 ConstantPoolCacheEntry::flags_offset())));
3266 
3267   // replace index with field offset from cache entry
3268   __ movptr(rbx, Address(rcx, rbx, Address::times_ptr,
3269                          in_bytes(base + ConstantPoolCacheEntry::f2_offset())));
3270 
3271   // [jk] not needed currently
3272   // volatile_barrier(Assembler::Membar_mask_bits(Assembler::LoadStore |
3273   //                                              Assembler::StoreStore));
3274 
3275   Label notVolatile;
3276   __ shrl(rdx, ConstantPoolCacheEntry::is_volatile_shift);
3277   __ andl(rdx, 0x1);
3278 
3279   // Get object from stack
3280   pop_and_check_object(rcx);
3281 
3282   // field address
3283   const Address field(rcx, rbx, Address::times_1);
3284 
3285   // access field
3286   switch (bytecode()) {
3287   case Bytecodes::_fast_aputfield:
3288     do_oop_store(_masm, field, rax, _bs->kind(), false);
3289     break;
3290   case Bytecodes::_fast_lputfield:
3291 #ifdef _LP64
3292   __ movq(field, rax);
3293 #else
3294   __ stop("should not be rewritten");
3295 #endif
3296     break;
3297   case Bytecodes::_fast_iputfield:
3298     __ movl(field, rax);
3299     break;
3300   case Bytecodes::_fast_bputfield:
3301     __ movb(field, rax);
3302     break;
3303   case Bytecodes::_fast_sputfield:
3304     // fall through
3305   case Bytecodes::_fast_cputfield:
3306     __ movw(field, rax);
3307     break;
3308   case Bytecodes::_fast_fputfield:
3309     __ store_float(field);
3310     break;
3311   case Bytecodes::_fast_dputfield:
3312     __ store_double(field);
3313     break;
3314   default:
3315     ShouldNotReachHere();
3316   }
3317 
3318   // Check for volatile store
3319   __ testl(rdx, rdx);
3320   __ jcc(Assembler::zero, notVolatile);
3321   volatile_barrier(Assembler::Membar_mask_bits(Assembler::StoreLoad |
3322                                                Assembler::StoreStore));
3323   __ bind(notVolatile);
3324 }
3325 
3326 void TemplateTable::fast_accessfield(TosState state) {
3327   transition(atos, state);
3328 
3329   // Do the JVMTI work here to avoid disturbing the register state below
3330   if (JvmtiExport::can_post_field_access()) {
3331     // Check to see if a field access watch has been set before we
3332     // take the time to call into the VM.
3333     Label L1;
3334     __ mov32(rcx, ExternalAddress((address) JvmtiExport::get_field_access_count_addr()));
3335     __ testl(rcx, rcx);
3336     __ jcc(Assembler::zero, L1);
3337     // access constant pool cache entry
3338     LP64_ONLY(__ get_cache_entry_pointer_at_bcp(c_rarg2, rcx, 1));
3339     NOT_LP64(__ get_cache_entry_pointer_at_bcp(rcx, rdx, 1));
3340     __ verify_oop(rax);
3341     __ push_ptr(rax);  // save object pointer before call_VM() clobbers it
3342     LP64_ONLY(__ mov(c_rarg1, rax));
3343     // c_rarg1: object pointer copied above
3344     // c_rarg2: cache entry pointer
3345     LP64_ONLY(__ call_VM(noreg, CAST_FROM_FN_PTR(address, InterpreterRuntime::post_field_access), c_rarg1, c_rarg2));
3346     NOT_LP64(__ call_VM(noreg, CAST_FROM_FN_PTR(address, InterpreterRuntime::post_field_access), rax, rcx));
3347     __ pop_ptr(rax); // restore object pointer
3348     __ bind(L1);
3349   }
3350 
3351   // access constant pool cache
3352   __ get_cache_and_index_at_bcp(rcx, rbx, 1);
3353   // replace index with field offset from cache entry
3354   // [jk] not needed currently
3355   // if (os::is_MP()) {
3356   //   __ movl(rdx, Address(rcx, rbx, Address::times_8,
3357   //                        in_bytes(ConstantPoolCache::base_offset() +
3358   //                                 ConstantPoolCacheEntry::flags_offset())));
3359   //   __ shrl(rdx, ConstantPoolCacheEntry::is_volatile_shift);
3360   //   __ andl(rdx, 0x1);
3361   // }
3362   __ movptr(rbx, Address(rcx, rbx, Address::times_ptr,
3363                          in_bytes(ConstantPoolCache::base_offset() +
3364                                   ConstantPoolCacheEntry::f2_offset())));
3365 
3366   // rax: object
3367   __ verify_oop(rax);
3368   __ null_check(rax);
3369   Address field(rax, rbx, Address::times_1);
3370 
3371   // access field
3372   switch (bytecode()) {
3373   case Bytecodes::_fast_agetfield:
3374     __ load_heap_oop(rax, field);
3375     __ verify_oop(rax);
3376     break;
3377   case Bytecodes::_fast_lgetfield:
3378 #ifdef _LP64
3379   __ movq(rax, field);
3380 #else
3381   __ stop("should not be rewritten");
3382 #endif
3383     break;
3384   case Bytecodes::_fast_igetfield:
3385     __ movl(rax, field);
3386     break;
3387   case Bytecodes::_fast_bgetfield:
3388     __ movsbl(rax, field);
3389     break;
3390   case Bytecodes::_fast_sgetfield:
3391     __ load_signed_short(rax, field);
3392     break;
3393   case Bytecodes::_fast_cgetfield:
3394     __ load_unsigned_short(rax, field);
3395     break;
3396   case Bytecodes::_fast_fgetfield:
3397     __ load_float(field);
3398     break;
3399   case Bytecodes::_fast_dgetfield:
3400     __ load_double(field);
3401     break;
3402   default:
3403     ShouldNotReachHere();
3404   }
3405   // [jk] not needed currently
3406   // if (os::is_MP()) {
3407   //   Label notVolatile;
3408   //   __ testl(rdx, rdx);
3409   //   __ jcc(Assembler::zero, notVolatile);
3410   //   __ membar(Assembler::LoadLoad);
3411   //   __ bind(notVolatile);
3412   //};
3413 }
3414 
3415 void TemplateTable::fast_xaccess(TosState state) {
3416   transition(vtos, state);
3417 
3418   // get receiver
3419   __ movptr(rax, aaddress(0));
3420   // access constant pool cache
3421   __ get_cache_and_index_at_bcp(rcx, rdx, 2);
3422   __ movptr(rbx,
3423             Address(rcx, rdx, Address::times_ptr,
3424                     in_bytes(ConstantPoolCache::base_offset() +
3425                              ConstantPoolCacheEntry::f2_offset())));
3426   // make sure exception is reported in correct bcp range (getfield is
3427   // next instruction)
3428   __ increment(rbcp);
3429   __ null_check(rax);
3430   const Address field = Address(rax, rbx, Address::times_1, 0*wordSize);
3431   switch (state) {
3432   case itos:
3433     __ movl(rax, field);
3434     break;
3435   case atos:
3436     __ load_heap_oop(rax, field);
3437     __ verify_oop(rax);
3438     break;
3439   case ftos:
3440     __ load_float(field);
3441     break;
3442   default:
3443     ShouldNotReachHere();
3444   }
3445 
3446   // [jk] not needed currently
3447   // if (os::is_MP()) {
3448   //   Label notVolatile;
3449   //   __ movl(rdx, Address(rcx, rdx, Address::times_8,
3450   //                        in_bytes(ConstantPoolCache::base_offset() +
3451   //                                 ConstantPoolCacheEntry::flags_offset())));
3452   //   __ shrl(rdx, ConstantPoolCacheEntry::is_volatile_shift);
3453   //   __ testl(rdx, 0x1);
3454   //   __ jcc(Assembler::zero, notVolatile);
3455   //   __ membar(Assembler::LoadLoad);
3456   //   __ bind(notVolatile);
3457   // }
3458 
3459   __ decrement(rbcp);
3460 }
3461 
3462 //-----------------------------------------------------------------------------
3463 // Calls
3464 
3465 void TemplateTable::count_calls(Register method, Register temp) {
3466   // implemented elsewhere
3467   ShouldNotReachHere();
3468 }
3469 
3470 void TemplateTable::prepare_invoke(int byte_no,
3471                                    Register method,  // linked method (or i-klass)
3472                                    Register index,   // itable index, MethodType, etc.
3473                                    Register recv,    // if caller wants to see it
3474                                    Register flags    // if caller wants to test it
3475                                    ) {
3476   // determine flags
3477   const Bytecodes::Code code = bytecode();
3478   const bool is_invokeinterface  = code == Bytecodes::_invokeinterface;
3479   const bool is_invokedynamic    = code == Bytecodes::_invokedynamic;
3480   const bool is_invokehandle     = code == Bytecodes::_invokehandle;
3481   const bool is_invokevirtual    = code == Bytecodes::_invokevirtual;
3482   const bool is_invokespecial    = code == Bytecodes::_invokespecial;
3483   const bool load_receiver       = (recv  != noreg);
3484   const bool save_flags          = (flags != noreg);
3485   assert(load_receiver == (code != Bytecodes::_invokestatic && code != Bytecodes::_invokedynamic), "");
3486   assert(save_flags    == (is_invokeinterface || is_invokevirtual), "need flags for vfinal");
3487   assert(flags == noreg || flags == rdx, "");
3488   assert(recv  == noreg || recv  == rcx, "");
3489 
3490   // setup registers & access constant pool cache
3491   if (recv  == noreg)  recv  = rcx;
3492   if (flags == noreg)  flags = rdx;
3493   assert_different_registers(method, index, recv, flags);
3494 
3495   // save 'interpreter return address'
3496   __ save_bcp();
3497 
3498   load_invoke_cp_cache_entry(byte_no, method, index, flags, is_invokevirtual, false, is_invokedynamic);
3499 
3500   // maybe push appendix to arguments (just before return address)
3501   if (is_invokedynamic || is_invokehandle) {
3502     Label L_no_push;
3503     __ testl(flags, (1 << ConstantPoolCacheEntry::has_appendix_shift));
3504     __ jcc(Assembler::zero, L_no_push);
3505     // Push the appendix as a trailing parameter.
3506     // This must be done before we get the receiver,
3507     // since the parameter_size includes it.
3508     __ push(rbx);
3509     __ mov(rbx, index);
3510     assert(ConstantPoolCacheEntry::_indy_resolved_references_appendix_offset == 0, "appendix expected at index+0");
3511     __ load_resolved_reference_at_index(index, rbx);
3512     __ pop(rbx);
3513     __ push(index);  // push appendix (MethodType, CallSite, etc.)
3514     __ bind(L_no_push);
3515   }
3516 
3517   // load receiver if needed (after appendix is pushed so parameter size is correct)
3518   // Note: no return address pushed yet
3519   if (load_receiver) {
3520     __ movl(recv, flags);
3521     __ andl(recv, ConstantPoolCacheEntry::parameter_size_mask);
3522     const int no_return_pc_pushed_yet = -1;  // argument slot correction before we push return address
3523     const int receiver_is_at_end      = -1;  // back off one slot to get receiver
3524     Address recv_addr = __ argument_address(recv, no_return_pc_pushed_yet + receiver_is_at_end);
3525     __ movptr(recv, recv_addr);
3526     __ verify_oop(recv);
3527   }
3528 
3529   if (save_flags) {
3530     __ movl(rbcp, flags);
3531   }
3532 
3533   // compute return type
3534   __ shrl(flags, ConstantPoolCacheEntry::tos_state_shift);
3535   // Make sure we don't need to mask flags after the above shift
3536   ConstantPoolCacheEntry::verify_tos_state_shift();
3537   // load return address
3538   {
3539     const address table_addr = (address) Interpreter::invoke_return_entry_table_for(code);
3540     ExternalAddress table(table_addr);
3541     LP64_ONLY(__ lea(rscratch1, table));
3542     LP64_ONLY(__ movptr(flags, Address(rscratch1, flags, Address::times_ptr)));
3543     NOT_LP64(__ movptr(flags, ArrayAddress(table, Address(noreg, flags, Address::times_ptr))));
3544   }
3545 
3546   // push return address
3547   __ push(flags);
3548 
3549   // Restore flags value from the constant pool cache, and restore rsi
3550   // for later null checks.  r13 is the bytecode pointer
3551   if (save_flags) {
3552     __ movl(flags, rbcp);
3553     __ restore_bcp();
3554   }
3555 }
3556 
3557 void TemplateTable::invokevirtual_helper(Register index,
3558                                          Register recv,
3559                                          Register flags) {
3560   // Uses temporary registers rax, rdx
3561   assert_different_registers(index, recv, rax, rdx);
3562   assert(index == rbx, "");
3563   assert(recv  == rcx, "");
3564 
3565   // Test for an invoke of a final method
3566   Label notFinal;
3567   __ movl(rax, flags);
3568   __ andl(rax, (1 << ConstantPoolCacheEntry::is_vfinal_shift));
3569   __ jcc(Assembler::zero, notFinal);
3570 
3571   const Register method = index;  // method must be rbx
3572   assert(method == rbx,
3573          "Method* must be rbx for interpreter calling convention");
3574 
3575   // do the call - the index is actually the method to call
3576   // that is, f2 is a vtable index if !is_vfinal, else f2 is a Method*
3577 
3578   // It's final, need a null check here!
3579   __ null_check(recv);
3580 
3581   // profile this call
3582   __ profile_final_call(rax);
3583   __ profile_arguments_type(rax, method, rbcp, true);
3584 
3585   __ jump_from_interpreted(method, rax);
3586 
3587   __ bind(notFinal);
3588 
3589   // get receiver klass
3590   __ null_check(recv, oopDesc::klass_offset_in_bytes());
3591   __ load_klass(rax, recv);
3592 
3593   // profile this call
3594   __ profile_virtual_call(rax, rlocals, rdx);
3595   // get target Method* & entry point
3596   __ lookup_virtual_method(rax, index, method);
3597   __ profile_arguments_type(rdx, method, rbcp, true);
3598   __ jump_from_interpreted(method, rdx);
3599 }
3600 
3601 void TemplateTable::invokevirtual(int byte_no) {
3602   transition(vtos, vtos);
3603   assert(byte_no == f2_byte, "use this argument");
3604   prepare_invoke(byte_no,
3605                  rbx,    // method or vtable index
3606                  noreg,  // unused itable index
3607                  rcx, rdx); // recv, flags
3608 
3609   // rbx: index
3610   // rcx: receiver
3611   // rdx: flags
3612 
3613   invokevirtual_helper(rbx, rcx, rdx);
3614 }
3615 
3616 void TemplateTable::invokespecial(int byte_no) {
3617   transition(vtos, vtos);
3618   assert(byte_no == f1_byte, "use this argument");
3619   prepare_invoke(byte_no, rbx, noreg,  // get f1 Method*
3620                  rcx);  // get receiver also for null check
3621   __ verify_oop(rcx);
3622   __ null_check(rcx);
3623   // do the call
3624   __ profile_call(rax);
3625   __ profile_arguments_type(rax, rbx, rbcp, false);
3626   __ jump_from_interpreted(rbx, rax);
3627 }
3628 
3629 void TemplateTable::invokestatic(int byte_no) {
3630   transition(vtos, vtos);
3631   assert(byte_no == f1_byte, "use this argument");
3632   prepare_invoke(byte_no, rbx);  // get f1 Method*
3633   // do the call
3634   __ profile_call(rax);
3635   __ profile_arguments_type(rax, rbx, rbcp, false);
3636   __ jump_from_interpreted(rbx, rax);
3637 }
3638 
3639 
3640 void TemplateTable::fast_invokevfinal(int byte_no) {
3641   transition(vtos, vtos);
3642   assert(byte_no == f2_byte, "use this argument");
3643   __ stop("fast_invokevfinal not used on x86");
3644 }
3645 
3646 
3647 void TemplateTable::invokeinterface(int byte_no) {
3648   transition(vtos, vtos);
3649   assert(byte_no == f1_byte, "use this argument");
3650   prepare_invoke(byte_no, rax, rbx,  // get f1 Klass*, f2 itable index
3651                  rcx, rdx); // recv, flags
3652 
3653   // rax: interface klass (from f1)
3654   // rbx: itable index (from f2)
3655   // rcx: receiver
3656   // rdx: flags
3657 
3658   // Special case of invokeinterface called for virtual method of
3659   // java.lang.Object.  See cpCacheOop.cpp for details.
3660   // This code isn't produced by javac, but could be produced by
3661   // another compliant java compiler.
3662   Label notMethod;
3663   __ movl(rlocals, rdx);
3664   __ andl(rlocals, (1 << ConstantPoolCacheEntry::is_forced_virtual_shift));
3665 
3666   __ jcc(Assembler::zero, notMethod);
3667 
3668   invokevirtual_helper(rbx, rcx, rdx);
3669   __ bind(notMethod);
3670 
3671   // Get receiver klass into rdx - also a null check
3672   __ restore_locals();  // restore r14
3673   __ null_check(rcx, oopDesc::klass_offset_in_bytes());
3674   __ load_klass(rdx, rcx);
3675 
3676   // profile this call
3677   __ profile_virtual_call(rdx, rbcp, rlocals);
3678 
3679   Label no_such_interface, no_such_method;
3680 
3681   __ lookup_interface_method(// inputs: rec. class, interface, itable index
3682                              rdx, rax, rbx,
3683                              // outputs: method, scan temp. reg
3684                              rbx, rbcp,
3685                              no_such_interface);
3686 
3687   // rbx: Method* to call
3688   // rcx: receiver
3689   // Check for abstract method error
3690   // Note: This should be done more efficiently via a throw_abstract_method_error
3691   //       interpreter entry point and a conditional jump to it in case of a null
3692   //       method.
3693   __ testptr(rbx, rbx);
3694   __ jcc(Assembler::zero, no_such_method);
3695 
3696   __ profile_arguments_type(rdx, rbx, rbcp, true);
3697 
3698   // do the call
3699   // rcx: receiver
3700   // rbx,: Method*
3701   __ jump_from_interpreted(rbx, rdx);
3702   __ should_not_reach_here();
3703 
3704   // exception handling code follows...
3705   // note: must restore interpreter registers to canonical
3706   //       state for exception handling to work correctly!
3707 
3708   __ bind(no_such_method);
3709   // throw exception
3710   __ pop(rbx);           // pop return address (pushed by prepare_invoke)
3711   __ restore_bcp();      // rbcp must be correct for exception handler   (was destroyed)
3712   __ restore_locals();   // make sure locals pointer is correct as well (was destroyed)
3713   __ call_VM(noreg, CAST_FROM_FN_PTR(address, InterpreterRuntime::throw_AbstractMethodError));
3714   // the call_VM checks for exception, so we should never return here.
3715   __ should_not_reach_here();
3716 
3717   __ bind(no_such_interface);
3718   // throw exception
3719   __ pop(rbx);           // pop return address (pushed by prepare_invoke)
3720   __ restore_bcp();      // rbcp must be correct for exception handler   (was destroyed)
3721   __ restore_locals();   // make sure locals pointer is correct as well (was destroyed)
3722   __ call_VM(noreg, CAST_FROM_FN_PTR(address,
3723                    InterpreterRuntime::throw_IncompatibleClassChangeError));
3724   // the call_VM checks for exception, so we should never return here.
3725   __ should_not_reach_here();
3726 }
3727 
3728 void TemplateTable::invokehandle(int byte_no) {
3729   transition(vtos, vtos);
3730   assert(byte_no == f1_byte, "use this argument");
3731   const Register rbx_method = rbx;
3732   const Register rax_mtype  = rax;
3733   const Register rcx_recv   = rcx;
3734   const Register rdx_flags  = rdx;
3735 
3736   prepare_invoke(byte_no, rbx_method, rax_mtype, rcx_recv);
3737   __ verify_method_ptr(rbx_method);
3738   __ verify_oop(rcx_recv);
3739   __ null_check(rcx_recv);
3740 
3741   // rax: MethodType object (from cpool->resolved_references[f1], if necessary)
3742   // rbx: MH.invokeExact_MT method (from f2)
3743 
3744   // Note:  rax_mtype is already pushed (if necessary) by prepare_invoke
3745 
3746   // FIXME: profile the LambdaForm also
3747   __ profile_final_call(rax);
3748   __ profile_arguments_type(rdx, rbx_method, rbcp, true);
3749 
3750   __ jump_from_interpreted(rbx_method, rdx);
3751 }
3752 
3753 void TemplateTable::invokedynamic(int byte_no) {
3754   transition(vtos, vtos);
3755   assert(byte_no == f1_byte, "use this argument");
3756 
3757   const Register rbx_method   = rbx;
3758   const Register rax_callsite = rax;
3759 
3760   prepare_invoke(byte_no, rbx_method, rax_callsite);
3761 
3762   // rax: CallSite object (from cpool->resolved_references[f1])
3763   // rbx: MH.linkToCallSite method (from f2)
3764 
3765   // Note:  rax_callsite is already pushed by prepare_invoke
3766 
3767   // %%% should make a type profile for any invokedynamic that takes a ref argument
3768   // profile this call
3769   __ profile_call(rbcp);
3770   __ profile_arguments_type(rdx, rbx_method, rbcp, false);
3771 
3772   __ verify_oop(rax_callsite);
3773 
3774   __ jump_from_interpreted(rbx_method, rdx);
3775 }
3776 
3777 //-----------------------------------------------------------------------------
3778 // Allocation
3779 
3780 void TemplateTable::_new() {
3781   transition(vtos, atos);
3782   __ get_unsigned_2_byte_index_at_bcp(rdx, 1);
3783   Label slow_case;
3784   Label slow_case_no_pop;
3785   Label done;
3786   Label initialize_header;
3787   Label initialize_object;  // including clearing the fields
3788   Label allocate_shared;
3789 
3790   __ get_cpool_and_tags(rcx, rax);
3791 
3792   // Make sure the class we're about to instantiate has been resolved.
3793   // This is done before loading InstanceKlass to be consistent with the order
3794   // how Constant Pool is updated (see ConstantPool::klass_at_put)
3795   const int tags_offset = Array<u1>::base_offset_in_bytes();
3796   __ cmpb(Address(rax, rdx, Address::times_1, tags_offset), JVM_CONSTANT_Class);
3797   __ jcc(Assembler::notEqual, slow_case_no_pop);
3798 
3799   // get InstanceKlass
3800   __ movptr(rcx, Address(rcx, rdx, Address::times_ptr, sizeof(ConstantPool)));
3801   __ push(rcx);  // save the contexts of klass for initializing the header
3802 
3803   // make sure klass is initialized & doesn't have finalizer
3804   // make sure klass is fully initialized
3805   __ cmpb(Address(rcx, InstanceKlass::init_state_offset()), InstanceKlass::fully_initialized);
3806   __ jcc(Assembler::notEqual, slow_case);
3807 
3808   // get instance_size in InstanceKlass (scaled to a count of bytes)
3809   __ movl(rdx, Address(rcx, Klass::layout_helper_offset()));
3810   // test to see if it has a finalizer or is malformed in some way
3811   __ testl(rdx, Klass::_lh_instance_slow_path_bit);
3812   __ jcc(Assembler::notZero, slow_case);
3813 
3814   //
3815   // Allocate the instance
3816   // 1) Try to allocate in the TLAB
3817   // 2) if fail and the object is large allocate in the shared Eden
3818   // 3) if the above fails (or is not applicable), go to a slow case
3819   // (creates a new TLAB, etc.)
3820 
3821   const bool allow_shared_alloc =
3822     Universe::heap()->supports_inline_contig_alloc();
3823 
3824   const Register thread = LP64_ONLY(r15_thread) NOT_LP64(rcx);
3825 #ifndef _LP64
3826   if (UseTLAB || allow_shared_alloc) {
3827     __ get_thread(thread);
3828   }
3829 #endif // _LP64
3830 
3831   if (UseTLAB) {
3832     __ movptr(rax, Address(thread, in_bytes(JavaThread::tlab_top_offset())));
3833     __ lea(rbx, Address(rax, rdx, Address::times_1));
3834     __ cmpptr(rbx, Address(thread, in_bytes(JavaThread::tlab_end_offset())));
3835     __ jcc(Assembler::above, allow_shared_alloc ? allocate_shared : slow_case);
3836     __ movptr(Address(thread, in_bytes(JavaThread::tlab_top_offset())), rbx);
3837     if (ZeroTLAB) {
3838       // the fields have been already cleared
3839       __ jmp(initialize_header);
3840     } else {
3841       // initialize both the header and fields
3842       __ jmp(initialize_object);
3843     }
3844   }
3845 
3846   // Allocation in the shared Eden, if allowed.
3847   //
3848   // rdx: instance size in bytes
3849   if (allow_shared_alloc) {
3850     __ bind(allocate_shared);
3851 
3852     ExternalAddress heap_top((address)Universe::heap()->top_addr());
3853     ExternalAddress heap_end((address)Universe::heap()->end_addr());
3854 
3855     Label retry;
3856     __ bind(retry);
3857     __ movptr(rax, heap_top);
3858     __ lea(rbx, Address(rax, rdx, Address::times_1));
3859     __ cmpptr(rbx, heap_end);
3860     __ jcc(Assembler::above, slow_case);
3861 
3862     // Compare rax, with the top addr, and if still equal, store the new
3863     // top addr in rbx, at the address of the top addr pointer. Sets ZF if was
3864     // equal, and clears it otherwise. Use lock prefix for atomicity on MPs.
3865     //
3866     // rax,: object begin
3867     // rbx,: object end
3868     // rdx: instance size in bytes
3869     __ locked_cmpxchgptr(rbx, heap_top);
3870 
3871     // if someone beat us on the allocation, try again, otherwise continue
3872     __ jcc(Assembler::notEqual, retry);
3873 
3874     __ incr_allocated_bytes(thread, rdx, 0);
3875   }
3876 
3877   if (UseTLAB || Universe::heap()->supports_inline_contig_alloc()) {
3878     // The object is initialized before the header.  If the object size is
3879     // zero, go directly to the header initialization.
3880     __ bind(initialize_object);
3881     __ decrement(rdx, sizeof(oopDesc));
3882     __ jcc(Assembler::zero, initialize_header);
3883 
3884     // Initialize topmost object field, divide rdx by 8, check if odd and
3885     // test if zero.
3886     __ xorl(rcx, rcx);    // use zero reg to clear memory (shorter code)
3887     __ shrl(rdx, LogBytesPerLong); // divide by 2*oopSize and set carry flag if odd
3888 
3889     // rdx must have been multiple of 8
3890 #ifdef ASSERT
3891     // make sure rdx was multiple of 8
3892     Label L;
3893     // Ignore partial flag stall after shrl() since it is debug VM
3894     __ jccb(Assembler::carryClear, L);
3895     __ stop("object size is not multiple of 2 - adjust this code");
3896     __ bind(L);
3897     // rdx must be > 0, no extra check needed here
3898 #endif
3899 
3900     // initialize remaining object fields: rdx was a multiple of 8
3901     { Label loop;
3902     __ bind(loop);
3903     __ movptr(Address(rax, rdx, Address::times_8, sizeof(oopDesc) - 1*oopSize), rcx);
3904     NOT_LP64(__ movptr(Address(rax, rdx, Address::times_8, sizeof(oopDesc) - 2*oopSize), rcx));
3905     __ decrement(rdx);
3906     __ jcc(Assembler::notZero, loop);
3907     }
3908 
3909     // initialize object header only.
3910     __ bind(initialize_header);
3911     if (UseBiasedLocking) {
3912       __ pop(rcx);   // get saved klass back in the register.
3913       __ movptr(rbx, Address(rcx, Klass::prototype_header_offset()));
3914       __ movptr(Address(rax, oopDesc::mark_offset_in_bytes ()), rbx);
3915     } else {
3916       __ movptr(Address(rax, oopDesc::mark_offset_in_bytes ()),
3917                 (intptr_t)markOopDesc::prototype()); // header
3918       __ pop(rcx);   // get saved klass back in the register.
3919     }
3920 #ifdef _LP64
3921     __ xorl(rsi, rsi); // use zero reg to clear memory (shorter code)
3922     __ store_klass_gap(rax, rsi);  // zero klass gap for compressed oops
3923 #endif
3924     __ store_klass(rax, rcx);  // klass
3925 
3926     {
3927       SkipIfEqual skip_if(_masm, &DTraceAllocProbes, 0);
3928       // Trigger dtrace event for fastpath
3929       __ push(atos);
3930       __ call_VM_leaf(
3931            CAST_FROM_FN_PTR(address, SharedRuntime::dtrace_object_alloc), rax);
3932       __ pop(atos);
3933     }
3934 
3935     __ jmp(done);
3936   }
3937 
3938   // slow case
3939   __ bind(slow_case);
3940   __ pop(rcx);   // restore stack pointer to what it was when we came in.
3941   __ bind(slow_case_no_pop);
3942 
3943   Register rarg1 = LP64_ONLY(c_rarg1) NOT_LP64(rax);
3944   Register rarg2 = LP64_ONLY(c_rarg2) NOT_LP64(rdx);
3945 
3946   __ get_constant_pool(rarg1);
3947   __ get_unsigned_2_byte_index_at_bcp(rarg2, 1);
3948   call_VM(rax, CAST_FROM_FN_PTR(address, InterpreterRuntime::_new), rarg1, rarg2);
3949    __ verify_oop(rax);
3950 
3951   // continue
3952   __ bind(done);
3953 }
3954 
3955 void TemplateTable::newarray() {
3956   transition(itos, atos);
3957   Register rarg1 = LP64_ONLY(c_rarg1) NOT_LP64(rdx);
3958   __ load_unsigned_byte(rarg1, at_bcp(1));
3959   call_VM(rax, CAST_FROM_FN_PTR(address, InterpreterRuntime::newarray),
3960           rarg1, rax);
3961 }
3962 
3963 void TemplateTable::anewarray() {
3964   transition(itos, atos);
3965 
3966   Register rarg1 = LP64_ONLY(c_rarg1) NOT_LP64(rcx);
3967   Register rarg2 = LP64_ONLY(c_rarg2) NOT_LP64(rdx);
3968 
3969   __ get_unsigned_2_byte_index_at_bcp(rarg2, 1);
3970   __ get_constant_pool(rarg1);
3971   call_VM(rax, CAST_FROM_FN_PTR(address, InterpreterRuntime::anewarray),
3972           rarg1, rarg2, rax);
3973 }
3974 
3975 void TemplateTable::arraylength() {
3976   transition(atos, itos);
3977   __ null_check(rax, arrayOopDesc::length_offset_in_bytes());
3978   __ movl(rax, Address(rax, arrayOopDesc::length_offset_in_bytes()));
3979 }
3980 
3981 void TemplateTable::checkcast() {
3982   transition(atos, atos);
3983   Label done, is_null, ok_is_subtype, quicked, resolved;
3984   __ testptr(rax, rax); // object is in rax
3985   __ jcc(Assembler::zero, is_null);
3986 
3987   // Get cpool & tags index
3988   __ get_cpool_and_tags(rcx, rdx); // rcx=cpool, rdx=tags array
3989   __ get_unsigned_2_byte_index_at_bcp(rbx, 1); // rbx=index
3990   // See if bytecode has already been quicked
3991   __ cmpb(Address(rdx, rbx,
3992                   Address::times_1,
3993                   Array<u1>::base_offset_in_bytes()),
3994           JVM_CONSTANT_Class);
3995   __ jcc(Assembler::equal, quicked);
3996   __ push(atos); // save receiver for result, and for GC
3997   call_VM(noreg, CAST_FROM_FN_PTR(address, InterpreterRuntime::quicken_io_cc));
3998 
3999   // vm_result_2 has metadata result
4000 #ifndef _LP64
4001   // borrow rdi from locals
4002   __ get_thread(rdi);
4003   __ get_vm_result_2(rax, rdi);
4004   __ restore_locals();
4005 #else
4006   __ get_vm_result_2(rax, r15_thread);
4007 #endif
4008 
4009   __ pop_ptr(rdx); // restore receiver
4010   __ jmpb(resolved);
4011 
4012   // Get superklass in rax and subklass in rbx
4013   __ bind(quicked);
4014   __ mov(rdx, rax); // Save object in rdx; rax needed for subtype check
4015   __ movptr(rax, Address(rcx, rbx,
4016                        Address::times_ptr, sizeof(ConstantPool)));
4017 
4018   __ bind(resolved);
4019   __ load_klass(rbx, rdx);
4020 
4021   // Generate subtype check.  Blows rcx, rdi.  Object in rdx.
4022   // Superklass in rax.  Subklass in rbx.
4023   __ gen_subtype_check(rbx, ok_is_subtype);
4024 
4025   // Come here on failure
4026   __ push_ptr(rdx);
4027   // object is at TOS
4028   __ jump(ExternalAddress(Interpreter::_throw_ClassCastException_entry));
4029 
4030   // Come here on success
4031   __ bind(ok_is_subtype);
4032   __ mov(rax, rdx); // Restore object in rdx
4033 
4034   // Collect counts on whether this check-cast sees NULLs a lot or not.
4035   if (ProfileInterpreter) {
4036     __ jmp(done);
4037     __ bind(is_null);
4038     __ profile_null_seen(rcx);
4039   } else {
4040     __ bind(is_null);   // same as 'done'
4041   }
4042   __ bind(done);
4043 }
4044 
4045 void TemplateTable::instanceof() {
4046   transition(atos, itos);
4047   Label done, is_null, ok_is_subtype, quicked, resolved;
4048   __ testptr(rax, rax);
4049   __ jcc(Assembler::zero, is_null);
4050 
4051   // Get cpool & tags index
4052   __ get_cpool_and_tags(rcx, rdx); // rcx=cpool, rdx=tags array
4053   __ get_unsigned_2_byte_index_at_bcp(rbx, 1); // rbx=index
4054   // See if bytecode has already been quicked
4055   __ cmpb(Address(rdx, rbx,
4056                   Address::times_1,
4057                   Array<u1>::base_offset_in_bytes()),
4058           JVM_CONSTANT_Class);
4059   __ jcc(Assembler::equal, quicked);
4060 
4061   __ push(atos); // save receiver for result, and for GC
4062   call_VM(noreg, CAST_FROM_FN_PTR(address, InterpreterRuntime::quicken_io_cc));
4063   // vm_result_2 has metadata result
4064 
4065 #ifndef _LP64
4066   // borrow rdi from locals
4067   __ get_thread(rdi);
4068   __ get_vm_result_2(rax, rdi);
4069   __ restore_locals();
4070 #else
4071   __ get_vm_result_2(rax, r15_thread);
4072 #endif
4073 
4074   __ pop_ptr(rdx); // restore receiver
4075   __ verify_oop(rdx);
4076   __ load_klass(rdx, rdx);
4077   __ jmpb(resolved);
4078 
4079   // Get superklass in rax and subklass in rdx
4080   __ bind(quicked);
4081   __ load_klass(rdx, rax);
4082   __ movptr(rax, Address(rcx, rbx,
4083                          Address::times_ptr, sizeof(ConstantPool)));
4084 
4085   __ bind(resolved);
4086 
4087   // Generate subtype check.  Blows rcx, rdi
4088   // Superklass in rax.  Subklass in rdx.
4089   __ gen_subtype_check(rdx, ok_is_subtype);
4090 
4091   // Come here on failure
4092   __ xorl(rax, rax);
4093   __ jmpb(done);
4094   // Come here on success
4095   __ bind(ok_is_subtype);
4096   __ movl(rax, 1);
4097 
4098   // Collect counts on whether this test sees NULLs a lot or not.
4099   if (ProfileInterpreter) {
4100     __ jmp(done);
4101     __ bind(is_null);
4102     __ profile_null_seen(rcx);
4103   } else {
4104     __ bind(is_null);   // same as 'done'
4105   }
4106   __ bind(done);
4107   // rax = 0: obj == NULL or  obj is not an instanceof the specified klass
4108   // rax = 1: obj != NULL and obj is     an instanceof the specified klass
4109 }
4110 
4111 
4112 //----------------------------------------------------------------------------------------------------
4113 // Breakpoints
4114 void TemplateTable::_breakpoint() {
4115   // Note: We get here even if we are single stepping..
4116   // jbug insists on setting breakpoints at every bytecode
4117   // even if we are in single step mode.
4118 
4119   transition(vtos, vtos);
4120 
4121   Register rarg = LP64_ONLY(c_rarg1) NOT_LP64(rcx);
4122 
4123   // get the unpatched byte code
4124   __ get_method(rarg);
4125   __ call_VM(noreg,
4126              CAST_FROM_FN_PTR(address,
4127                               InterpreterRuntime::get_original_bytecode_at),
4128              rarg, rbcp);
4129   __ mov(rbx, rax);  // why?
4130 
4131   // post the breakpoint event
4132   __ get_method(rarg);
4133   __ call_VM(noreg,
4134              CAST_FROM_FN_PTR(address, InterpreterRuntime::_breakpoint),
4135              rarg, rbcp);
4136 
4137   // complete the execution of original bytecode
4138   __ dispatch_only_normal(vtos);
4139 }
4140 
4141 //-----------------------------------------------------------------------------
4142 // Exceptions
4143 
4144 void TemplateTable::athrow() {
4145   transition(atos, vtos);
4146   __ null_check(rax);
4147   __ jump(ExternalAddress(Interpreter::throw_exception_entry()));
4148 }
4149 
4150 //-----------------------------------------------------------------------------
4151 // Synchronization
4152 //
4153 // Note: monitorenter & exit are symmetric routines; which is reflected
4154 //       in the assembly code structure as well
4155 //
4156 // Stack layout:
4157 //
4158 // [expressions  ] <--- rsp               = expression stack top
4159 // ..
4160 // [expressions  ]
4161 // [monitor entry] <--- monitor block top = expression stack bot
4162 // ..
4163 // [monitor entry]
4164 // [frame data   ] <--- monitor block bot
4165 // ...
4166 // [saved rbp    ] <--- rbp
4167 void TemplateTable::monitorenter() {
4168   transition(atos, vtos);
4169 
4170   // check for NULL object
4171   __ null_check(rax);
4172 
4173   const Address monitor_block_top(
4174         rbp, frame::interpreter_frame_monitor_block_top_offset * wordSize);
4175   const Address monitor_block_bot(
4176         rbp, frame::interpreter_frame_initial_sp_offset * wordSize);
4177   const int entry_size = frame::interpreter_frame_monitor_size() * wordSize;
4178 
4179   Label allocated;
4180 
4181   Register rtop = LP64_ONLY(c_rarg3) NOT_LP64(rcx);
4182   Register rbot = LP64_ONLY(c_rarg2) NOT_LP64(rbx);
4183   Register rmon = LP64_ONLY(c_rarg1) NOT_LP64(rdx);
4184 
4185   // initialize entry pointer
4186   __ xorl(rmon, rmon); // points to free slot or NULL
4187 
4188   // find a free slot in the monitor block (result in rmon)
4189   {
4190     Label entry, loop, exit;
4191     __ movptr(rtop, monitor_block_top); // points to current entry,
4192                                         // starting with top-most entry
4193     __ lea(rbot, monitor_block_bot);    // points to word before bottom
4194                                         // of monitor block
4195     __ jmpb(entry);
4196 
4197     __ bind(loop);
4198     // check if current entry is used
4199     __ cmpptr(Address(rtop, BasicObjectLock::obj_offset_in_bytes()), (int32_t) NULL_WORD);
4200     // if not used then remember entry in rmon
4201     __ cmovptr(Assembler::equal, rmon, rtop);   // cmov => cmovptr
4202     // check if current entry is for same object
4203     __ cmpptr(rax, Address(rtop, BasicObjectLock::obj_offset_in_bytes()));
4204     // if same object then stop searching
4205     __ jccb(Assembler::equal, exit);
4206     // otherwise advance to next entry
4207     __ addptr(rtop, entry_size);
4208     __ bind(entry);
4209     // check if bottom reached
4210     __ cmpptr(rtop, rbot);
4211     // if not at bottom then check this entry
4212     __ jcc(Assembler::notEqual, loop);
4213     __ bind(exit);
4214   }
4215 
4216   __ testptr(rmon, rmon); // check if a slot has been found
4217   __ jcc(Assembler::notZero, allocated); // if found, continue with that one
4218 
4219   // allocate one if there's no free slot
4220   {
4221     Label entry, loop;
4222     // 1. compute new pointers          // rsp: old expression stack top
4223     __ movptr(rmon, monitor_block_bot); // rmon: old expression stack bottom
4224     __ subptr(rsp, entry_size);         // move expression stack top
4225     __ subptr(rmon, entry_size);        // move expression stack bottom
4226     __ mov(rtop, rsp);                  // set start value for copy loop
4227     __ movptr(monitor_block_bot, rmon); // set new monitor block bottom
4228     __ jmp(entry);
4229     // 2. move expression stack contents
4230     __ bind(loop);
4231     __ movptr(rbot, Address(rtop, entry_size)); // load expression stack
4232                                                 // word from old location
4233     __ movptr(Address(rtop, 0), rbot);          // and store it at new location
4234     __ addptr(rtop, wordSize);                  // advance to next word
4235     __ bind(entry);
4236     __ cmpptr(rtop, rmon);                      // check if bottom reached
4237     __ jcc(Assembler::notEqual, loop);          // if not at bottom then
4238                                                 // copy next word
4239   }
4240 
4241   // call run-time routine
4242   // rmon: points to monitor entry
4243   __ bind(allocated);
4244 
4245   // Increment bcp to point to the next bytecode, so exception
4246   // handling for async. exceptions work correctly.
4247   // The object has already been poped from the stack, so the
4248   // expression stack looks correct.
4249   __ increment(rbcp);
4250 
4251   // store object
4252   __ movptr(Address(rmon, BasicObjectLock::obj_offset_in_bytes()), rax);
4253   __ lock_object(rmon);
4254 
4255   // check to make sure this monitor doesn't cause stack overflow after locking
4256   __ save_bcp();  // in case of exception
4257   __ generate_stack_overflow_check(0);
4258 
4259   // The bcp has already been incremented. Just need to dispatch to
4260   // next instruction.
4261   __ dispatch_next(vtos);
4262 }
4263 
4264 void TemplateTable::monitorexit() {
4265   transition(atos, vtos);
4266 
4267   // check for NULL object
4268   __ null_check(rax);
4269 
4270   const Address monitor_block_top(
4271         rbp, frame::interpreter_frame_monitor_block_top_offset * wordSize);
4272   const Address monitor_block_bot(
4273         rbp, frame::interpreter_frame_initial_sp_offset * wordSize);
4274   const int entry_size = frame::interpreter_frame_monitor_size() * wordSize;
4275 
4276   Register rtop = LP64_ONLY(c_rarg1) NOT_LP64(rdx);
4277   Register rbot = LP64_ONLY(c_rarg2) NOT_LP64(rbx);
4278 
4279   Label found;
4280 
4281   // find matching slot
4282   {
4283     Label entry, loop;
4284     __ movptr(rtop, monitor_block_top); // points to current entry,
4285                                         // starting with top-most entry
4286     __ lea(rbot, monitor_block_bot);    // points to word before bottom
4287                                         // of monitor block
4288     __ jmpb(entry);
4289 
4290     __ bind(loop);
4291     // check if current entry is for same object
4292     __ cmpptr(rax, Address(rtop, BasicObjectLock::obj_offset_in_bytes()));
4293     // if same object then stop searching
4294     __ jcc(Assembler::equal, found);
4295     // otherwise advance to next entry
4296     __ addptr(rtop, entry_size);
4297     __ bind(entry);
4298     // check if bottom reached
4299     __ cmpptr(rtop, rbot);
4300     // if not at bottom then check this entry
4301     __ jcc(Assembler::notEqual, loop);
4302   }
4303 
4304   // error handling. Unlocking was not block-structured
4305   __ call_VM(noreg, CAST_FROM_FN_PTR(address,
4306                    InterpreterRuntime::throw_illegal_monitor_state_exception));
4307   __ should_not_reach_here();
4308 
4309   // call run-time routine
4310   __ bind(found);
4311   __ push_ptr(rax); // make sure object is on stack (contract with oopMaps)
4312   __ unlock_object(rtop);
4313   __ pop_ptr(rax); // discard object
4314 }
4315 
4316 // Wide instructions
4317 void TemplateTable::wide() {
4318   transition(vtos, vtos);
4319   __ load_unsigned_byte(rbx, at_bcp(1));
4320   ExternalAddress wtable((address)Interpreter::_wentry_point);
4321   __ jump(ArrayAddress(wtable, Address(noreg, rbx, Address::times_ptr)));
4322   // Note: the rbcp increment step is part of the individual wide bytecode implementations
4323 }
4324 
4325 // Multi arrays
4326 void TemplateTable::multianewarray() {
4327   transition(vtos, atos);
4328 
4329   Register rarg = LP64_ONLY(c_rarg1) NOT_LP64(rax);
4330   __ load_unsigned_byte(rax, at_bcp(3)); // get number of dimensions
4331   // last dim is on top of stack; we want address of first one:
4332   // first_addr = last_addr + (ndims - 1) * stackElementSize - 1*wordsize
4333   // the latter wordSize to point to the beginning of the array.
4334   __ lea(rarg, Address(rsp, rax, Interpreter::stackElementScale(), -wordSize));
4335   call_VM(rax, CAST_FROM_FN_PTR(address, InterpreterRuntime::multianewarray), rarg);
4336   __ load_unsigned_byte(rbx, at_bcp(3));
4337   __ lea(rsp, Address(rsp, rbx, Interpreter::stackElementScale()));  // get rid of counts
4338 }
4339 #endif /* !CC_INTERP */
4340