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