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