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