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