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