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