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