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