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