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