1 /*
   2  * Copyright (c) 2000, 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 "asm/macroAssembler.inline.hpp"
  28 #include "c1/c1_Compilation.hpp"
  29 #include "c1/c1_LIRAssembler.hpp"
  30 #include "c1/c1_MacroAssembler.hpp"
  31 #include "c1/c1_Runtime1.hpp"
  32 #include "c1/c1_ValueStack.hpp"
  33 #include "ci/ciArrayKlass.hpp"
  34 #include "ci/ciInstance.hpp"
  35 #include "gc/shared/barrierSet.hpp"
  36 #include "gc/shared/cardTableModRefBS.hpp"
  37 #include "gc/shared/collectedHeap.hpp"
  38 #include "nativeInst_x86.hpp"
  39 #include "oops/objArrayKlass.hpp"
  40 #include "runtime/sharedRuntime.hpp"
  41 #include "vmreg_x86.inline.hpp"
  42 
  43 
  44 // These masks are used to provide 128-bit aligned bitmasks to the XMM
  45 // instructions, to allow sign-masking or sign-bit flipping.  They allow
  46 // fast versions of NegF/NegD and AbsF/AbsD.
  47 
  48 // Note: 'double' and 'long long' have 32-bits alignment on x86.
  49 static jlong* double_quadword(jlong *adr, jlong lo, jlong hi) {
  50   // Use the expression (adr)&(~0xF) to provide 128-bits aligned address
  51   // of 128-bits operands for SSE instructions.
  52   jlong *operand = (jlong*)(((intptr_t)adr) & ((intptr_t)(~0xF)));
  53   // Store the value to a 128-bits operand.
  54   operand[0] = lo;
  55   operand[1] = hi;
  56   return operand;
  57 }
  58 
  59 // Buffer for 128-bits masks used by SSE instructions.
  60 static jlong fp_signmask_pool[(4+1)*2]; // 4*128bits(data) + 128bits(alignment)
  61 
  62 // Static initialization during VM startup.
  63 static jlong *float_signmask_pool  = double_quadword(&fp_signmask_pool[1*2],         CONST64(0x7FFFFFFF7FFFFFFF),         CONST64(0x7FFFFFFF7FFFFFFF));
  64 static jlong *double_signmask_pool = double_quadword(&fp_signmask_pool[2*2],         CONST64(0x7FFFFFFFFFFFFFFF),         CONST64(0x7FFFFFFFFFFFFFFF));
  65 static jlong *float_signflip_pool  = double_quadword(&fp_signmask_pool[3*2], (jlong)UCONST64(0x8000000080000000), (jlong)UCONST64(0x8000000080000000));
  66 static jlong *double_signflip_pool = double_quadword(&fp_signmask_pool[4*2], (jlong)UCONST64(0x8000000000000000), (jlong)UCONST64(0x8000000000000000));
  67 
  68 
  69 
  70 NEEDS_CLEANUP // remove this definitions ?
  71 const Register IC_Klass    = rax;   // where the IC klass is cached
  72 const Register SYNC_header = rax;   // synchronization header
  73 const Register SHIFT_count = rcx;   // where count for shift operations must be
  74 
  75 #define __ _masm->
  76 
  77 
  78 static void select_different_registers(Register preserve,
  79                                        Register extra,
  80                                        Register &tmp1,
  81                                        Register &tmp2) {
  82   if (tmp1 == preserve) {
  83     assert_different_registers(tmp1, tmp2, extra);
  84     tmp1 = extra;
  85   } else if (tmp2 == preserve) {
  86     assert_different_registers(tmp1, tmp2, extra);
  87     tmp2 = extra;
  88   }
  89   assert_different_registers(preserve, tmp1, tmp2);
  90 }
  91 
  92 
  93 
  94 static void select_different_registers(Register preserve,
  95                                        Register extra,
  96                                        Register &tmp1,
  97                                        Register &tmp2,
  98                                        Register &tmp3) {
  99   if (tmp1 == preserve) {
 100     assert_different_registers(tmp1, tmp2, tmp3, extra);
 101     tmp1 = extra;
 102   } else if (tmp2 == preserve) {
 103     assert_different_registers(tmp1, tmp2, tmp3, extra);
 104     tmp2 = extra;
 105   } else if (tmp3 == preserve) {
 106     assert_different_registers(tmp1, tmp2, tmp3, extra);
 107     tmp3 = extra;
 108   }
 109   assert_different_registers(preserve, tmp1, tmp2, tmp3);
 110 }
 111 
 112 
 113 
 114 bool LIR_Assembler::is_small_constant(LIR_Opr opr) {
 115   if (opr->is_constant()) {
 116     LIR_Const* constant = opr->as_constant_ptr();
 117     switch (constant->type()) {
 118       case T_INT: {
 119         return true;
 120       }
 121 
 122       default:
 123         return false;
 124     }
 125   }
 126   return false;
 127 }
 128 
 129 
 130 LIR_Opr LIR_Assembler::receiverOpr() {
 131   return FrameMap::receiver_opr;
 132 }
 133 
 134 LIR_Opr LIR_Assembler::osrBufferPointer() {
 135   return FrameMap::as_pointer_opr(receiverOpr()->as_register());
 136 }
 137 
 138 //--------------fpu register translations-----------------------
 139 
 140 
 141 address LIR_Assembler::float_constant(float f) {
 142   address const_addr = __ float_constant(f);
 143   if (const_addr == NULL) {
 144     bailout("const section overflow");
 145     return __ code()->consts()->start();
 146   } else {
 147     return const_addr;
 148   }
 149 }
 150 
 151 
 152 address LIR_Assembler::double_constant(double d) {
 153   address const_addr = __ double_constant(d);
 154   if (const_addr == NULL) {
 155     bailout("const section overflow");
 156     return __ code()->consts()->start();
 157   } else {
 158     return const_addr;
 159   }
 160 }
 161 
 162 
 163 void LIR_Assembler::set_24bit_FPU() {
 164   __ fldcw(ExternalAddress(StubRoutines::addr_fpu_cntrl_wrd_24()));
 165 }
 166 
 167 void LIR_Assembler::reset_FPU() {
 168   __ fldcw(ExternalAddress(StubRoutines::addr_fpu_cntrl_wrd_std()));
 169 }
 170 
 171 void LIR_Assembler::fpop() {
 172   __ fpop();
 173 }
 174 
 175 void LIR_Assembler::fxch(int i) {
 176   __ fxch(i);
 177 }
 178 
 179 void LIR_Assembler::fld(int i) {
 180   __ fld_s(i);
 181 }
 182 
 183 void LIR_Assembler::ffree(int i) {
 184   __ ffree(i);
 185 }
 186 
 187 void LIR_Assembler::breakpoint() {
 188   __ int3();
 189 }
 190 
 191 void LIR_Assembler::push(LIR_Opr opr) {
 192   if (opr->is_single_cpu()) {
 193     __ push_reg(opr->as_register());
 194   } else if (opr->is_double_cpu()) {
 195     NOT_LP64(__ push_reg(opr->as_register_hi()));
 196     __ push_reg(opr->as_register_lo());
 197   } else if (opr->is_stack()) {
 198     __ push_addr(frame_map()->address_for_slot(opr->single_stack_ix()));
 199   } else if (opr->is_constant()) {
 200     LIR_Const* const_opr = opr->as_constant_ptr();
 201     if (const_opr->type() == T_OBJECT) {
 202       __ push_oop(const_opr->as_jobject());
 203     } else if (const_opr->type() == T_INT) {
 204       __ push_jint(const_opr->as_jint());
 205     } else {
 206       ShouldNotReachHere();
 207     }
 208 
 209   } else {
 210     ShouldNotReachHere();
 211   }
 212 }
 213 
 214 void LIR_Assembler::pop(LIR_Opr opr) {
 215   if (opr->is_single_cpu()) {
 216     __ pop_reg(opr->as_register());
 217   } else {
 218     ShouldNotReachHere();
 219   }
 220 }
 221 
 222 bool LIR_Assembler::is_literal_address(LIR_Address* addr) {
 223   return addr->base()->is_illegal() && addr->index()->is_illegal();
 224 }
 225 
 226 //-------------------------------------------
 227 
 228 Address LIR_Assembler::as_Address(LIR_Address* addr) {
 229   return as_Address(addr, rscratch1);
 230 }
 231 
 232 Address LIR_Assembler::as_Address(LIR_Address* addr, Register tmp) {
 233   if (addr->base()->is_illegal()) {
 234     assert(addr->index()->is_illegal(), "must be illegal too");
 235     AddressLiteral laddr((address)addr->disp(), relocInfo::none);
 236     if (! __ reachable(laddr)) {
 237       __ movptr(tmp, laddr.addr());
 238       Address res(tmp, 0);
 239       return res;
 240     } else {
 241       return __ as_Address(laddr);
 242     }
 243   }
 244 
 245   Register base = addr->base()->as_pointer_register();
 246 
 247   if (addr->index()->is_illegal()) {
 248     return Address( base, addr->disp());
 249   } else if (addr->index()->is_cpu_register()) {
 250     Register index = addr->index()->as_pointer_register();
 251     return Address(base, index, (Address::ScaleFactor) addr->scale(), addr->disp());
 252   } else if (addr->index()->is_constant()) {
 253     intptr_t addr_offset = (addr->index()->as_constant_ptr()->as_jint() << addr->scale()) + addr->disp();
 254     assert(Assembler::is_simm32(addr_offset), "must be");
 255 
 256     return Address(base, addr_offset);
 257   } else {
 258     Unimplemented();
 259     return Address();
 260   }
 261 }
 262 
 263 
 264 Address LIR_Assembler::as_Address_hi(LIR_Address* addr) {
 265   Address base = as_Address(addr);
 266   return Address(base._base, base._index, base._scale, base._disp + BytesPerWord);
 267 }
 268 
 269 
 270 Address LIR_Assembler::as_Address_lo(LIR_Address* addr) {
 271   return as_Address(addr);
 272 }
 273 
 274 
 275 void LIR_Assembler::osr_entry() {
 276   offsets()->set_value(CodeOffsets::OSR_Entry, code_offset());
 277   BlockBegin* osr_entry = compilation()->hir()->osr_entry();
 278   ValueStack* entry_state = osr_entry->state();
 279   int number_of_locks = entry_state->locks_size();
 280 
 281   // we jump here if osr happens with the interpreter
 282   // state set up to continue at the beginning of the
 283   // loop that triggered osr - in particular, we have
 284   // the following registers setup:
 285   //
 286   // rcx: osr buffer
 287   //
 288 
 289   // build frame
 290   ciMethod* m = compilation()->method();
 291   __ build_frame(initial_frame_size_in_bytes(), bang_size_in_bytes());
 292 
 293   // OSR buffer is
 294   //
 295   // locals[nlocals-1..0]
 296   // monitors[0..number_of_locks]
 297   //
 298   // locals is a direct copy of the interpreter frame so in the osr buffer
 299   // so first slot in the local array is the last local from the interpreter
 300   // and last slot is local[0] (receiver) from the interpreter
 301   //
 302   // Similarly with locks. The first lock slot in the osr buffer is the nth lock
 303   // from the interpreter frame, the nth lock slot in the osr buffer is 0th lock
 304   // in the interpreter frame (the method lock if a sync method)
 305 
 306   // Initialize monitors in the compiled activation.
 307   //   rcx: pointer to osr buffer
 308   //
 309   // All other registers are dead at this point and the locals will be
 310   // copied into place by code emitted in the IR.
 311 
 312   Register OSR_buf = osrBufferPointer()->as_pointer_register();
 313   { assert(frame::interpreter_frame_monitor_size() == BasicObjectLock::size(), "adjust code below");
 314     int monitor_offset = BytesPerWord * method()->max_locals() +
 315       (2 * BytesPerWord) * (number_of_locks - 1);
 316     // SharedRuntime::OSR_migration_begin() packs BasicObjectLocks in
 317     // the OSR buffer using 2 word entries: first the lock and then
 318     // the oop.
 319     for (int i = 0; i < number_of_locks; i++) {
 320       int slot_offset = monitor_offset - ((i * 2) * BytesPerWord);
 321 #ifdef ASSERT
 322       // verify the interpreter's monitor has a non-null object
 323       {
 324         Label L;
 325         __ cmpptr(Address(OSR_buf, slot_offset + 1*BytesPerWord), (int32_t)NULL_WORD);
 326         __ jcc(Assembler::notZero, L);
 327         __ stop("locked object is NULL");
 328         __ bind(L);
 329       }
 330 #endif
 331       __ movptr(rbx, Address(OSR_buf, slot_offset + 0));
 332       __ movptr(frame_map()->address_for_monitor_lock(i), rbx);
 333       __ movptr(rbx, Address(OSR_buf, slot_offset + 1*BytesPerWord));
 334       __ movptr(frame_map()->address_for_monitor_object(i), rbx);
 335     }
 336   }
 337 }
 338 
 339 
 340 // inline cache check; done before the frame is built.
 341 int LIR_Assembler::check_icache() {
 342   Register receiver = FrameMap::receiver_opr->as_register();
 343   Register ic_klass = IC_Klass;
 344   const int ic_cmp_size = LP64_ONLY(10) NOT_LP64(9);
 345   const bool do_post_padding = VerifyOops || UseCompressedClassPointers;
 346   if (!do_post_padding) {
 347     // insert some nops so that the verified entry point is aligned on CodeEntryAlignment
 348     __ align(CodeEntryAlignment, __ offset() + ic_cmp_size);
 349   }
 350   int offset = __ offset();
 351   __ inline_cache_check(receiver, IC_Klass);
 352   assert(__ offset() % CodeEntryAlignment == 0 || do_post_padding, "alignment must be correct");
 353   if (do_post_padding) {
 354     // force alignment after the cache check.
 355     // It's been verified to be aligned if !VerifyOops
 356     __ align(CodeEntryAlignment);
 357   }
 358   return offset;
 359 }
 360 
 361 
 362 void LIR_Assembler::jobject2reg_with_patching(Register reg, CodeEmitInfo* info) {
 363   jobject o = NULL;
 364   PatchingStub* patch = new PatchingStub(_masm, patching_id(info));
 365   __ movoop(reg, o);
 366   patching_epilog(patch, lir_patch_normal, reg, info);
 367 }
 368 
 369 void LIR_Assembler::klass2reg_with_patching(Register reg, CodeEmitInfo* info) {
 370   Metadata* o = NULL;
 371   PatchingStub* patch = new PatchingStub(_masm, PatchingStub::load_klass_id);
 372   __ mov_metadata(reg, o);
 373   patching_epilog(patch, lir_patch_normal, reg, info);
 374 }
 375 
 376 // This specifies the rsp decrement needed to build the frame
 377 int LIR_Assembler::initial_frame_size_in_bytes() const {
 378   // if rounding, must let FrameMap know!
 379 
 380   // The frame_map records size in slots (32bit word)
 381 
 382   // subtract two words to account for return address and link
 383   return (frame_map()->framesize() - (2*VMRegImpl::slots_per_word))  * VMRegImpl::stack_slot_size;
 384 }
 385 
 386 
 387 int LIR_Assembler::emit_exception_handler() {
 388   // if the last instruction is a call (typically to do a throw which
 389   // is coming at the end after block reordering) the return address
 390   // must still point into the code area in order to avoid assertion
 391   // failures when searching for the corresponding bci => add a nop
 392   // (was bug 5/14/1999 - gri)
 393   __ nop();
 394 
 395   // generate code for exception handler
 396   address handler_base = __ start_a_stub(exception_handler_size);
 397   if (handler_base == NULL) {
 398     // not enough space left for the handler
 399     bailout("exception handler overflow");
 400     return -1;
 401   }
 402 
 403   int offset = code_offset();
 404 
 405   // the exception oop and pc are in rax, and rdx
 406   // no other registers need to be preserved, so invalidate them
 407   __ invalidate_registers(false, true, true, false, true, true);
 408 
 409   // check that there is really an exception
 410   __ verify_not_null_oop(rax);
 411 
 412   // search an exception handler (rax: exception oop, rdx: throwing pc)
 413   __ call(RuntimeAddress(Runtime1::entry_for(Runtime1::handle_exception_from_callee_id)));
 414   __ should_not_reach_here();
 415   guarantee(code_offset() - offset <= exception_handler_size, "overflow");
 416   __ end_a_stub();
 417 
 418   return offset;
 419 }
 420 
 421 
 422 // Emit the code to remove the frame from the stack in the exception
 423 // unwind path.
 424 int LIR_Assembler::emit_unwind_handler() {
 425 #ifndef PRODUCT
 426   if (CommentedAssembly) {
 427     _masm->block_comment("Unwind handler");
 428   }
 429 #endif
 430 
 431   int offset = code_offset();
 432 
 433   // Fetch the exception from TLS and clear out exception related thread state
 434   Register thread = NOT_LP64(rsi) LP64_ONLY(r15_thread);
 435   NOT_LP64(__ get_thread(rsi));
 436   __ movptr(rax, Address(thread, JavaThread::exception_oop_offset()));
 437   __ movptr(Address(thread, JavaThread::exception_oop_offset()), (intptr_t)NULL_WORD);
 438   __ movptr(Address(thread, JavaThread::exception_pc_offset()), (intptr_t)NULL_WORD);
 439 
 440   __ bind(_unwind_handler_entry);
 441   __ verify_not_null_oop(rax);
 442   if (method()->is_synchronized() || compilation()->env()->dtrace_method_probes()) {
 443     __ mov(rbx, rax);  // Preserve the exception (rbx is always callee-saved)
 444   }
 445 
 446   // Preform needed unlocking
 447   MonitorExitStub* stub = NULL;
 448   if (method()->is_synchronized()) {
 449     monitor_address(0, FrameMap::rax_opr);
 450     stub = new MonitorExitStub(FrameMap::rax_opr, true, 0);
 451     __ unlock_object(rdi, rsi, rax, *stub->entry());
 452     __ bind(*stub->continuation());
 453   }
 454 
 455   if (compilation()->env()->dtrace_method_probes()) {
 456 #ifdef _LP64
 457     __ mov(rdi, r15_thread);
 458     __ mov_metadata(rsi, method()->constant_encoding());
 459 #else
 460     __ get_thread(rax);
 461     __ movptr(Address(rsp, 0), rax);
 462     __ mov_metadata(Address(rsp, sizeof(void*)), method()->constant_encoding());
 463 #endif
 464     __ call(RuntimeAddress(CAST_FROM_FN_PTR(address, SharedRuntime::dtrace_method_exit)));
 465   }
 466 
 467   if (method()->is_synchronized() || compilation()->env()->dtrace_method_probes()) {
 468     __ mov(rax, rbx);  // Restore the exception
 469   }
 470 
 471   // remove the activation and dispatch to the unwind handler
 472   __ remove_frame(initial_frame_size_in_bytes());
 473   __ jump(RuntimeAddress(Runtime1::entry_for(Runtime1::unwind_exception_id)));
 474 
 475   // Emit the slow path assembly
 476   if (stub != NULL) {
 477     stub->emit_code(this);
 478   }
 479 
 480   return offset;
 481 }
 482 
 483 
 484 int LIR_Assembler::emit_deopt_handler() {
 485   // if the last instruction is a call (typically to do a throw which
 486   // is coming at the end after block reordering) the return address
 487   // must still point into the code area in order to avoid assertion
 488   // failures when searching for the corresponding bci => add a nop
 489   // (was bug 5/14/1999 - gri)
 490   __ nop();
 491 
 492   // generate code for exception handler
 493   address handler_base = __ start_a_stub(deopt_handler_size);
 494   if (handler_base == NULL) {
 495     // not enough space left for the handler
 496     bailout("deopt handler overflow");
 497     return -1;
 498   }
 499 
 500   int offset = code_offset();
 501   InternalAddress here(__ pc());
 502 
 503   __ pushptr(here.addr());
 504   __ jump(RuntimeAddress(SharedRuntime::deopt_blob()->unpack()));
 505   guarantee(code_offset() - offset <= deopt_handler_size, "overflow");
 506   __ end_a_stub();
 507 
 508   return offset;
 509 }
 510 
 511 
 512 void LIR_Assembler::return_op(LIR_Opr result) {
 513   assert(result->is_illegal() || !result->is_single_cpu() || result->as_register() == rax, "word returns are in rax,");
 514   if (!result->is_illegal() && result->is_float_kind() && !result->is_xmm_register()) {
 515     assert(result->fpu() == 0, "result must already be on TOS");
 516   }
 517 
 518   // Pop the stack before the safepoint code
 519   __ remove_frame(initial_frame_size_in_bytes());
 520 
 521   bool result_is_oop = result->is_valid() ? result->is_oop() : false;
 522 
 523   // Note: we do not need to round double result; float result has the right precision
 524   // the poll sets the condition code, but no data registers
 525   AddressLiteral polling_page(os::get_polling_page(), relocInfo::poll_return_type);
 526 
 527   if (Assembler::is_polling_page_far()) {
 528     __ lea(rscratch1, polling_page);
 529     __ relocate(relocInfo::poll_return_type);
 530     __ testl(rax, Address(rscratch1, 0));
 531   } else {
 532     __ testl(rax, polling_page);
 533   }
 534   __ ret(0);
 535 }
 536 
 537 
 538 int LIR_Assembler::safepoint_poll(LIR_Opr tmp, CodeEmitInfo* info) {
 539   AddressLiteral polling_page(os::get_polling_page(), relocInfo::poll_type);
 540   guarantee(info != NULL, "Shouldn't be NULL");
 541   int offset = __ offset();
 542   if (Assembler::is_polling_page_far()) {
 543     __ lea(rscratch1, polling_page);
 544     offset = __ offset();
 545     add_debug_info_for_branch(info);
 546     __ relocate(relocInfo::poll_type);
 547     __ testl(rax, Address(rscratch1, 0));
 548   } else {
 549     add_debug_info_for_branch(info);
 550     __ testl(rax, polling_page);
 551   }
 552   return offset;
 553 }
 554 
 555 
 556 void LIR_Assembler::move_regs(Register from_reg, Register to_reg) {
 557   if (from_reg != to_reg) __ mov(to_reg, from_reg);
 558 }
 559 
 560 void LIR_Assembler::swap_reg(Register a, Register b) {
 561   __ xchgptr(a, b);
 562 }
 563 
 564 
 565 void LIR_Assembler::const2reg(LIR_Opr src, LIR_Opr dest, LIR_PatchCode patch_code, CodeEmitInfo* info) {
 566   assert(src->is_constant(), "should not call otherwise");
 567   assert(dest->is_register(), "should not call otherwise");
 568   LIR_Const* c = src->as_constant_ptr();
 569 
 570   switch (c->type()) {
 571     case T_INT: {
 572       assert(patch_code == lir_patch_none, "no patching handled here");
 573       __ movl(dest->as_register(), c->as_jint());
 574       break;
 575     }
 576 
 577     case T_ADDRESS: {
 578       assert(patch_code == lir_patch_none, "no patching handled here");
 579       __ movptr(dest->as_register(), c->as_jint());
 580       break;
 581     }
 582 
 583     case T_LONG: {
 584       assert(patch_code == lir_patch_none, "no patching handled here");
 585 #ifdef _LP64
 586       __ movptr(dest->as_register_lo(), (intptr_t)c->as_jlong());
 587 #else
 588       __ movptr(dest->as_register_lo(), c->as_jint_lo());
 589       __ movptr(dest->as_register_hi(), c->as_jint_hi());
 590 #endif // _LP64
 591       break;
 592     }
 593 
 594     case T_OBJECT: {
 595       if (patch_code != lir_patch_none) {
 596         jobject2reg_with_patching(dest->as_register(), info);
 597       } else {
 598         __ movoop(dest->as_register(), c->as_jobject());
 599       }
 600       break;
 601     }
 602 
 603     case T_METADATA: {
 604       if (patch_code != lir_patch_none) {
 605         klass2reg_with_patching(dest->as_register(), info);
 606       } else {
 607         __ mov_metadata(dest->as_register(), c->as_metadata());
 608       }
 609       break;
 610     }
 611 
 612     case T_FLOAT: {
 613       if (dest->is_single_xmm()) {
 614         if (c->is_zero_float()) {
 615           __ xorps(dest->as_xmm_float_reg(), dest->as_xmm_float_reg());
 616         } else {
 617           __ movflt(dest->as_xmm_float_reg(),
 618                    InternalAddress(float_constant(c->as_jfloat())));
 619         }
 620       } else {
 621         assert(dest->is_single_fpu(), "must be");
 622         assert(dest->fpu_regnr() == 0, "dest must be TOS");
 623         if (c->is_zero_float()) {
 624           __ fldz();
 625         } else if (c->is_one_float()) {
 626           __ fld1();
 627         } else {
 628           __ fld_s (InternalAddress(float_constant(c->as_jfloat())));
 629         }
 630       }
 631       break;
 632     }
 633 
 634     case T_DOUBLE: {
 635       if (dest->is_double_xmm()) {
 636         if (c->is_zero_double()) {
 637           __ xorpd(dest->as_xmm_double_reg(), dest->as_xmm_double_reg());
 638         } else {
 639           __ movdbl(dest->as_xmm_double_reg(),
 640                     InternalAddress(double_constant(c->as_jdouble())));
 641         }
 642       } else {
 643         assert(dest->is_double_fpu(), "must be");
 644         assert(dest->fpu_regnrLo() == 0, "dest must be TOS");
 645         if (c->is_zero_double()) {
 646           __ fldz();
 647         } else if (c->is_one_double()) {
 648           __ fld1();
 649         } else {
 650           __ fld_d (InternalAddress(double_constant(c->as_jdouble())));
 651         }
 652       }
 653       break;
 654     }
 655 
 656     default:
 657       ShouldNotReachHere();
 658   }
 659 }
 660 
 661 void LIR_Assembler::const2stack(LIR_Opr src, LIR_Opr dest) {
 662   assert(src->is_constant(), "should not call otherwise");
 663   assert(dest->is_stack(), "should not call otherwise");
 664   LIR_Const* c = src->as_constant_ptr();
 665 
 666   switch (c->type()) {
 667     case T_INT:  // fall through
 668     case T_FLOAT:
 669       __ movl(frame_map()->address_for_slot(dest->single_stack_ix()), c->as_jint_bits());
 670       break;
 671 
 672     case T_ADDRESS:
 673       __ movptr(frame_map()->address_for_slot(dest->single_stack_ix()), c->as_jint_bits());
 674       break;
 675 
 676     case T_OBJECT:
 677       __ movoop(frame_map()->address_for_slot(dest->single_stack_ix()), c->as_jobject());
 678       break;
 679 
 680     case T_LONG:  // fall through
 681     case T_DOUBLE:
 682 #ifdef _LP64
 683       __ movptr(frame_map()->address_for_slot(dest->double_stack_ix(),
 684                                             lo_word_offset_in_bytes), (intptr_t)c->as_jlong_bits());
 685 #else
 686       __ movptr(frame_map()->address_for_slot(dest->double_stack_ix(),
 687                                               lo_word_offset_in_bytes), c->as_jint_lo_bits());
 688       __ movptr(frame_map()->address_for_slot(dest->double_stack_ix(),
 689                                               hi_word_offset_in_bytes), c->as_jint_hi_bits());
 690 #endif // _LP64
 691       break;
 692 
 693     default:
 694       ShouldNotReachHere();
 695   }
 696 }
 697 
 698 void LIR_Assembler::const2mem(LIR_Opr src, LIR_Opr dest, BasicType type, CodeEmitInfo* info, bool wide) {
 699   assert(src->is_constant(), "should not call otherwise");
 700   assert(dest->is_address(), "should not call otherwise");
 701   LIR_Const* c = src->as_constant_ptr();
 702   LIR_Address* addr = dest->as_address_ptr();
 703 
 704   int null_check_here = code_offset();
 705   switch (type) {
 706     case T_INT:    // fall through
 707     case T_FLOAT:
 708       __ movl(as_Address(addr), c->as_jint_bits());
 709       break;
 710 
 711     case T_ADDRESS:
 712       __ movptr(as_Address(addr), c->as_jint_bits());
 713       break;
 714 
 715     case T_OBJECT:  // fall through
 716     case T_ARRAY:
 717       if (c->as_jobject() == NULL) {
 718         if (UseCompressedOops && !wide) {
 719           __ movl(as_Address(addr), (int32_t)NULL_WORD);
 720         } else {
 721 #ifdef _LP64
 722           __ xorptr(rscratch1, rscratch1);
 723           null_check_here = code_offset();
 724           __ movptr(as_Address(addr), rscratch1);
 725 #else
 726           __ movptr(as_Address(addr), NULL_WORD);
 727 #endif
 728         }
 729       } else {
 730         if (is_literal_address(addr)) {
 731           ShouldNotReachHere();
 732           __ movoop(as_Address(addr, noreg), c->as_jobject());
 733         } else {
 734 #ifdef _LP64
 735           __ movoop(rscratch1, c->as_jobject());
 736           if (UseCompressedOops && !wide) {
 737             __ encode_heap_oop(rscratch1);
 738             null_check_here = code_offset();
 739             __ movl(as_Address_lo(addr), rscratch1);
 740           } else {
 741             null_check_here = code_offset();
 742             __ movptr(as_Address_lo(addr), rscratch1);
 743           }
 744 #else
 745           __ movoop(as_Address(addr), c->as_jobject());
 746 #endif
 747         }
 748       }
 749       break;
 750 
 751     case T_LONG:    // fall through
 752     case T_DOUBLE:
 753 #ifdef _LP64
 754       if (is_literal_address(addr)) {
 755         ShouldNotReachHere();
 756         __ movptr(as_Address(addr, r15_thread), (intptr_t)c->as_jlong_bits());
 757       } else {
 758         __ movptr(r10, (intptr_t)c->as_jlong_bits());
 759         null_check_here = code_offset();
 760         __ movptr(as_Address_lo(addr), r10);
 761       }
 762 #else
 763       // Always reachable in 32bit so this doesn't produce useless move literal
 764       __ movptr(as_Address_hi(addr), c->as_jint_hi_bits());
 765       __ movptr(as_Address_lo(addr), c->as_jint_lo_bits());
 766 #endif // _LP64
 767       break;
 768 
 769     case T_BOOLEAN: // fall through
 770     case T_BYTE:
 771       __ movb(as_Address(addr), c->as_jint() & 0xFF);
 772       break;
 773 
 774     case T_CHAR:    // fall through
 775     case T_SHORT:
 776       __ movw(as_Address(addr), c->as_jint() & 0xFFFF);
 777       break;
 778 
 779     default:
 780       ShouldNotReachHere();
 781   };
 782 
 783   if (info != NULL) {
 784     add_debug_info_for_null_check(null_check_here, info);
 785   }
 786 }
 787 
 788 
 789 void LIR_Assembler::reg2reg(LIR_Opr src, LIR_Opr dest) {
 790   assert(src->is_register(), "should not call otherwise");
 791   assert(dest->is_register(), "should not call otherwise");
 792 
 793   // move between cpu-registers
 794   if (dest->is_single_cpu()) {
 795 #ifdef _LP64
 796     if (src->type() == T_LONG) {
 797       // Can do LONG -> OBJECT
 798       move_regs(src->as_register_lo(), dest->as_register());
 799       return;
 800     }
 801 #endif
 802     assert(src->is_single_cpu(), "must match");
 803     if (src->type() == T_OBJECT) {
 804       __ verify_oop(src->as_register());
 805     }
 806     move_regs(src->as_register(), dest->as_register());
 807 
 808   } else if (dest->is_double_cpu()) {
 809 #ifdef _LP64
 810     if (src->type() == T_OBJECT || src->type() == T_ARRAY) {
 811       // Surprising to me but we can see move of a long to t_object
 812       __ verify_oop(src->as_register());
 813       move_regs(src->as_register(), dest->as_register_lo());
 814       return;
 815     }
 816 #endif
 817     assert(src->is_double_cpu(), "must match");
 818     Register f_lo = src->as_register_lo();
 819     Register f_hi = src->as_register_hi();
 820     Register t_lo = dest->as_register_lo();
 821     Register t_hi = dest->as_register_hi();
 822 #ifdef _LP64
 823     assert(f_hi == f_lo, "must be same");
 824     assert(t_hi == t_lo, "must be same");
 825     move_regs(f_lo, t_lo);
 826 #else
 827     assert(f_lo != f_hi && t_lo != t_hi, "invalid register allocation");
 828 
 829 
 830     if (f_lo == t_hi && f_hi == t_lo) {
 831       swap_reg(f_lo, f_hi);
 832     } else if (f_hi == t_lo) {
 833       assert(f_lo != t_hi, "overwriting register");
 834       move_regs(f_hi, t_hi);
 835       move_regs(f_lo, t_lo);
 836     } else {
 837       assert(f_hi != t_lo, "overwriting register");
 838       move_regs(f_lo, t_lo);
 839       move_regs(f_hi, t_hi);
 840     }
 841 #endif // LP64
 842 
 843     // special moves from fpu-register to xmm-register
 844     // necessary for method results
 845   } else if (src->is_single_xmm() && !dest->is_single_xmm()) {
 846     __ movflt(Address(rsp, 0), src->as_xmm_float_reg());
 847     __ fld_s(Address(rsp, 0));
 848   } else if (src->is_double_xmm() && !dest->is_double_xmm()) {
 849     __ movdbl(Address(rsp, 0), src->as_xmm_double_reg());
 850     __ fld_d(Address(rsp, 0));
 851   } else if (dest->is_single_xmm() && !src->is_single_xmm()) {
 852     __ fstp_s(Address(rsp, 0));
 853     __ movflt(dest->as_xmm_float_reg(), Address(rsp, 0));
 854   } else if (dest->is_double_xmm() && !src->is_double_xmm()) {
 855     __ fstp_d(Address(rsp, 0));
 856     __ movdbl(dest->as_xmm_double_reg(), Address(rsp, 0));
 857 
 858     // move between xmm-registers
 859   } else if (dest->is_single_xmm()) {
 860     assert(src->is_single_xmm(), "must match");
 861     __ movflt(dest->as_xmm_float_reg(), src->as_xmm_float_reg());
 862   } else if (dest->is_double_xmm()) {
 863     assert(src->is_double_xmm(), "must match");
 864     __ movdbl(dest->as_xmm_double_reg(), src->as_xmm_double_reg());
 865 
 866     // move between fpu-registers (no instruction necessary because of fpu-stack)
 867   } else if (dest->is_single_fpu() || dest->is_double_fpu()) {
 868     assert(src->is_single_fpu() || src->is_double_fpu(), "must match");
 869     assert(src->fpu() == dest->fpu(), "currently should be nothing to do");
 870   } else {
 871     ShouldNotReachHere();
 872   }
 873 }
 874 
 875 void LIR_Assembler::reg2stack(LIR_Opr src, LIR_Opr dest, BasicType type, bool pop_fpu_stack) {
 876   assert(src->is_register(), "should not call otherwise");
 877   assert(dest->is_stack(), "should not call otherwise");
 878 
 879   if (src->is_single_cpu()) {
 880     Address dst = frame_map()->address_for_slot(dest->single_stack_ix());
 881     if (type == T_OBJECT || type == T_ARRAY) {
 882       __ verify_oop(src->as_register());
 883       __ movptr (dst, src->as_register());
 884     } else if (type == T_METADATA) {
 885       __ movptr (dst, src->as_register());
 886     } else {
 887       __ movl (dst, src->as_register());
 888     }
 889 
 890   } else if (src->is_double_cpu()) {
 891     Address dstLO = frame_map()->address_for_slot(dest->double_stack_ix(), lo_word_offset_in_bytes);
 892     Address dstHI = frame_map()->address_for_slot(dest->double_stack_ix(), hi_word_offset_in_bytes);
 893     __ movptr (dstLO, src->as_register_lo());
 894     NOT_LP64(__ movptr (dstHI, src->as_register_hi()));
 895 
 896   } else if (src->is_single_xmm()) {
 897     Address dst_addr = frame_map()->address_for_slot(dest->single_stack_ix());
 898     __ movflt(dst_addr, src->as_xmm_float_reg());
 899 
 900   } else if (src->is_double_xmm()) {
 901     Address dst_addr = frame_map()->address_for_slot(dest->double_stack_ix());
 902     __ movdbl(dst_addr, src->as_xmm_double_reg());
 903 
 904   } else if (src->is_single_fpu()) {
 905     assert(src->fpu_regnr() == 0, "argument must be on TOS");
 906     Address dst_addr = frame_map()->address_for_slot(dest->single_stack_ix());
 907     if (pop_fpu_stack)     __ fstp_s (dst_addr);
 908     else                   __ fst_s  (dst_addr);
 909 
 910   } else if (src->is_double_fpu()) {
 911     assert(src->fpu_regnrLo() == 0, "argument must be on TOS");
 912     Address dst_addr = frame_map()->address_for_slot(dest->double_stack_ix());
 913     if (pop_fpu_stack)     __ fstp_d (dst_addr);
 914     else                   __ fst_d  (dst_addr);
 915 
 916   } else {
 917     ShouldNotReachHere();
 918   }
 919 }
 920 
 921 
 922 void LIR_Assembler::reg2mem(LIR_Opr src, LIR_Opr dest, BasicType type, LIR_PatchCode patch_code, CodeEmitInfo* info, bool pop_fpu_stack, bool wide, bool /* unaligned */) {
 923   LIR_Address* to_addr = dest->as_address_ptr();
 924   PatchingStub* patch = NULL;
 925   Register compressed_src = rscratch1;
 926 
 927   if (type == T_ARRAY || type == T_OBJECT) {
 928     __ verify_oop(src->as_register());
 929 #ifdef _LP64
 930     if (UseCompressedOops && !wide) {
 931       __ movptr(compressed_src, src->as_register());
 932       __ encode_heap_oop(compressed_src);
 933       if (patch_code != lir_patch_none) {
 934         info->oop_map()->set_narrowoop(compressed_src->as_VMReg());
 935       }
 936     }
 937 #endif
 938   }
 939 
 940   if (patch_code != lir_patch_none) {
 941     patch = new PatchingStub(_masm, PatchingStub::access_field_id);
 942     Address toa = as_Address(to_addr);
 943     assert(toa.disp() != 0, "must have");
 944   }
 945 
 946   int null_check_here = code_offset();
 947   switch (type) {
 948     case T_FLOAT: {
 949       if (src->is_single_xmm()) {
 950         __ movflt(as_Address(to_addr), src->as_xmm_float_reg());
 951       } else {
 952         assert(src->is_single_fpu(), "must be");
 953         assert(src->fpu_regnr() == 0, "argument must be on TOS");
 954         if (pop_fpu_stack)      __ fstp_s(as_Address(to_addr));
 955         else                    __ fst_s (as_Address(to_addr));
 956       }
 957       break;
 958     }
 959 
 960     case T_DOUBLE: {
 961       if (src->is_double_xmm()) {
 962         __ movdbl(as_Address(to_addr), src->as_xmm_double_reg());
 963       } else {
 964         assert(src->is_double_fpu(), "must be");
 965         assert(src->fpu_regnrLo() == 0, "argument must be on TOS");
 966         if (pop_fpu_stack)      __ fstp_d(as_Address(to_addr));
 967         else                    __ fst_d (as_Address(to_addr));
 968       }
 969       break;
 970     }
 971 
 972     case T_ARRAY:   // fall through
 973     case T_OBJECT:  // fall through
 974       if (UseCompressedOops && !wide) {
 975         __ movl(as_Address(to_addr), compressed_src);
 976       } else {
 977         __ movptr(as_Address(to_addr), src->as_register());
 978       }
 979       break;
 980     case T_METADATA:
 981       // We get here to store a method pointer to the stack to pass to
 982       // a dtrace runtime call. This can't work on 64 bit with
 983       // compressed klass ptrs: T_METADATA can be a compressed klass
 984       // ptr or a 64 bit method pointer.
 985       LP64_ONLY(ShouldNotReachHere());
 986       __ movptr(as_Address(to_addr), src->as_register());
 987       break;
 988     case T_ADDRESS:
 989       __ movptr(as_Address(to_addr), src->as_register());
 990       break;
 991     case T_INT:
 992       __ movl(as_Address(to_addr), src->as_register());
 993       break;
 994 
 995     case T_LONG: {
 996       Register from_lo = src->as_register_lo();
 997       Register from_hi = src->as_register_hi();
 998 #ifdef _LP64
 999       __ movptr(as_Address_lo(to_addr), from_lo);
1000 #else
1001       Register base = to_addr->base()->as_register();
1002       Register index = noreg;
1003       if (to_addr->index()->is_register()) {
1004         index = to_addr->index()->as_register();
1005       }
1006       if (base == from_lo || index == from_lo) {
1007         assert(base != from_hi, "can't be");
1008         assert(index == noreg || (index != base && index != from_hi), "can't handle this");
1009         __ movl(as_Address_hi(to_addr), from_hi);
1010         if (patch != NULL) {
1011           patching_epilog(patch, lir_patch_high, base, info);
1012           patch = new PatchingStub(_masm, PatchingStub::access_field_id);
1013           patch_code = lir_patch_low;
1014         }
1015         __ movl(as_Address_lo(to_addr), from_lo);
1016       } else {
1017         assert(index == noreg || (index != base && index != from_lo), "can't handle this");
1018         __ movl(as_Address_lo(to_addr), from_lo);
1019         if (patch != NULL) {
1020           patching_epilog(patch, lir_patch_low, base, info);
1021           patch = new PatchingStub(_masm, PatchingStub::access_field_id);
1022           patch_code = lir_patch_high;
1023         }
1024         __ movl(as_Address_hi(to_addr), from_hi);
1025       }
1026 #endif // _LP64
1027       break;
1028     }
1029 
1030     case T_BYTE:    // fall through
1031     case T_BOOLEAN: {
1032       Register src_reg = src->as_register();
1033       Address dst_addr = as_Address(to_addr);
1034       assert(VM_Version::is_P6() || src_reg->has_byte_register(), "must use byte registers if not P6");
1035       __ movb(dst_addr, src_reg);
1036       break;
1037     }
1038 
1039     case T_CHAR:    // fall through
1040     case T_SHORT:
1041       __ movw(as_Address(to_addr), src->as_register());
1042       break;
1043 
1044     default:
1045       ShouldNotReachHere();
1046   }
1047   if (info != NULL) {
1048     add_debug_info_for_null_check(null_check_here, info);
1049   }
1050 
1051   if (patch_code != lir_patch_none) {
1052     patching_epilog(patch, patch_code, to_addr->base()->as_register(), info);
1053   }
1054 }
1055 
1056 
1057 void LIR_Assembler::stack2reg(LIR_Opr src, LIR_Opr dest, BasicType type) {
1058   assert(src->is_stack(), "should not call otherwise");
1059   assert(dest->is_register(), "should not call otherwise");
1060 
1061   if (dest->is_single_cpu()) {
1062     if (type == T_ARRAY || type == T_OBJECT) {
1063       __ movptr(dest->as_register(), frame_map()->address_for_slot(src->single_stack_ix()));
1064       __ verify_oop(dest->as_register());
1065     } else if (type == T_METADATA) {
1066       __ movptr(dest->as_register(), frame_map()->address_for_slot(src->single_stack_ix()));
1067     } else {
1068       __ movl(dest->as_register(), frame_map()->address_for_slot(src->single_stack_ix()));
1069     }
1070 
1071   } else if (dest->is_double_cpu()) {
1072     Address src_addr_LO = frame_map()->address_for_slot(src->double_stack_ix(), lo_word_offset_in_bytes);
1073     Address src_addr_HI = frame_map()->address_for_slot(src->double_stack_ix(), hi_word_offset_in_bytes);
1074     __ movptr(dest->as_register_lo(), src_addr_LO);
1075     NOT_LP64(__ movptr(dest->as_register_hi(), src_addr_HI));
1076 
1077   } else if (dest->is_single_xmm()) {
1078     Address src_addr = frame_map()->address_for_slot(src->single_stack_ix());
1079     __ movflt(dest->as_xmm_float_reg(), src_addr);
1080 
1081   } else if (dest->is_double_xmm()) {
1082     Address src_addr = frame_map()->address_for_slot(src->double_stack_ix());
1083     __ movdbl(dest->as_xmm_double_reg(), src_addr);
1084 
1085   } else if (dest->is_single_fpu()) {
1086     assert(dest->fpu_regnr() == 0, "dest must be TOS");
1087     Address src_addr = frame_map()->address_for_slot(src->single_stack_ix());
1088     __ fld_s(src_addr);
1089 
1090   } else if (dest->is_double_fpu()) {
1091     assert(dest->fpu_regnrLo() == 0, "dest must be TOS");
1092     Address src_addr = frame_map()->address_for_slot(src->double_stack_ix());
1093     __ fld_d(src_addr);
1094 
1095   } else {
1096     ShouldNotReachHere();
1097   }
1098 }
1099 
1100 
1101 void LIR_Assembler::stack2stack(LIR_Opr src, LIR_Opr dest, BasicType type) {
1102   if (src->is_single_stack()) {
1103     if (type == T_OBJECT || type == T_ARRAY) {
1104       __ pushptr(frame_map()->address_for_slot(src ->single_stack_ix()));
1105       __ popptr (frame_map()->address_for_slot(dest->single_stack_ix()));
1106     } else {
1107 #ifndef _LP64
1108       __ pushl(frame_map()->address_for_slot(src ->single_stack_ix()));
1109       __ popl (frame_map()->address_for_slot(dest->single_stack_ix()));
1110 #else
1111       //no pushl on 64bits
1112       __ movl(rscratch1, frame_map()->address_for_slot(src ->single_stack_ix()));
1113       __ movl(frame_map()->address_for_slot(dest->single_stack_ix()), rscratch1);
1114 #endif
1115     }
1116 
1117   } else if (src->is_double_stack()) {
1118 #ifdef _LP64
1119     __ pushptr(frame_map()->address_for_slot(src ->double_stack_ix()));
1120     __ popptr (frame_map()->address_for_slot(dest->double_stack_ix()));
1121 #else
1122     __ pushl(frame_map()->address_for_slot(src ->double_stack_ix(), 0));
1123     // push and pop the part at src + wordSize, adding wordSize for the previous push
1124     __ pushl(frame_map()->address_for_slot(src ->double_stack_ix(), 2 * wordSize));
1125     __ popl (frame_map()->address_for_slot(dest->double_stack_ix(), 2 * wordSize));
1126     __ popl (frame_map()->address_for_slot(dest->double_stack_ix(), 0));
1127 #endif // _LP64
1128 
1129   } else {
1130     ShouldNotReachHere();
1131   }
1132 }
1133 
1134 
1135 void LIR_Assembler::mem2reg(LIR_Opr src, LIR_Opr dest, BasicType type, LIR_PatchCode patch_code, CodeEmitInfo* info, bool wide, bool /* unaligned */) {
1136   assert(src->is_address(), "should not call otherwise");
1137   assert(dest->is_register(), "should not call otherwise");
1138 
1139   LIR_Address* addr = src->as_address_ptr();
1140   Address from_addr = as_Address(addr);
1141 
1142   if (addr->base()->type() == T_OBJECT) {
1143     __ verify_oop(addr->base()->as_pointer_register());
1144   }
1145 
1146   switch (type) {
1147     case T_BOOLEAN: // fall through
1148     case T_BYTE:    // fall through
1149     case T_CHAR:    // fall through
1150     case T_SHORT:
1151       if (!VM_Version::is_P6() && !from_addr.uses(dest->as_register())) {
1152         // on pre P6 processors we may get partial register stalls
1153         // so blow away the value of to_rinfo before loading a
1154         // partial word into it.  Do it here so that it precedes
1155         // the potential patch point below.
1156         __ xorptr(dest->as_register(), dest->as_register());
1157       }
1158       break;
1159   }
1160 
1161   PatchingStub* patch = NULL;
1162   if (patch_code != lir_patch_none) {
1163     patch = new PatchingStub(_masm, PatchingStub::access_field_id);
1164     assert(from_addr.disp() != 0, "must have");
1165   }
1166   if (info != NULL) {
1167     add_debug_info_for_null_check_here(info);
1168   }
1169 
1170   switch (type) {
1171     case T_FLOAT: {
1172       if (dest->is_single_xmm()) {
1173         __ movflt(dest->as_xmm_float_reg(), from_addr);
1174       } else {
1175         assert(dest->is_single_fpu(), "must be");
1176         assert(dest->fpu_regnr() == 0, "dest must be TOS");
1177         __ fld_s(from_addr);
1178       }
1179       break;
1180     }
1181 
1182     case T_DOUBLE: {
1183       if (dest->is_double_xmm()) {
1184         __ movdbl(dest->as_xmm_double_reg(), from_addr);
1185       } else {
1186         assert(dest->is_double_fpu(), "must be");
1187         assert(dest->fpu_regnrLo() == 0, "dest must be TOS");
1188         __ fld_d(from_addr);
1189       }
1190       break;
1191     }
1192 
1193     case T_OBJECT:  // fall through
1194     case T_ARRAY:   // fall through
1195       if (UseCompressedOops && !wide) {
1196         __ movl(dest->as_register(), from_addr);
1197       } else {
1198         __ movptr(dest->as_register(), from_addr);
1199       }
1200       break;
1201 
1202     case T_ADDRESS:
1203       if (UseCompressedClassPointers && addr->disp() == oopDesc::klass_offset_in_bytes()) {
1204         __ movl(dest->as_register(), from_addr);
1205       } else {
1206         __ movptr(dest->as_register(), from_addr);
1207       }
1208       break;
1209     case T_INT:
1210       __ movl(dest->as_register(), from_addr);
1211       break;
1212 
1213     case T_LONG: {
1214       Register to_lo = dest->as_register_lo();
1215       Register to_hi = dest->as_register_hi();
1216 #ifdef _LP64
1217       __ movptr(to_lo, as_Address_lo(addr));
1218 #else
1219       Register base = addr->base()->as_register();
1220       Register index = noreg;
1221       if (addr->index()->is_register()) {
1222         index = addr->index()->as_register();
1223       }
1224       if ((base == to_lo && index == to_hi) ||
1225           (base == to_hi && index == to_lo)) {
1226         // addresses with 2 registers are only formed as a result of
1227         // array access so this code will never have to deal with
1228         // patches or null checks.
1229         assert(info == NULL && patch == NULL, "must be");
1230         __ lea(to_hi, as_Address(addr));
1231         __ movl(to_lo, Address(to_hi, 0));
1232         __ movl(to_hi, Address(to_hi, BytesPerWord));
1233       } else if (base == to_lo || index == to_lo) {
1234         assert(base != to_hi, "can't be");
1235         assert(index == noreg || (index != base && index != to_hi), "can't handle this");
1236         __ movl(to_hi, as_Address_hi(addr));
1237         if (patch != NULL) {
1238           patching_epilog(patch, lir_patch_high, base, info);
1239           patch = new PatchingStub(_masm, PatchingStub::access_field_id);
1240           patch_code = lir_patch_low;
1241         }
1242         __ movl(to_lo, as_Address_lo(addr));
1243       } else {
1244         assert(index == noreg || (index != base && index != to_lo), "can't handle this");
1245         __ movl(to_lo, as_Address_lo(addr));
1246         if (patch != NULL) {
1247           patching_epilog(patch, lir_patch_low, base, info);
1248           patch = new PatchingStub(_masm, PatchingStub::access_field_id);
1249           patch_code = lir_patch_high;
1250         }
1251         __ movl(to_hi, as_Address_hi(addr));
1252       }
1253 #endif // _LP64
1254       break;
1255     }
1256 
1257     case T_BOOLEAN: // fall through
1258     case T_BYTE: {
1259       Register dest_reg = dest->as_register();
1260       assert(VM_Version::is_P6() || dest_reg->has_byte_register(), "must use byte registers if not P6");
1261       if (VM_Version::is_P6() || from_addr.uses(dest_reg)) {
1262         __ movsbl(dest_reg, from_addr);
1263       } else {
1264         __ movb(dest_reg, from_addr);
1265         __ shll(dest_reg, 24);
1266         __ sarl(dest_reg, 24);
1267       }
1268       break;
1269     }
1270 
1271     case T_CHAR: {
1272       Register dest_reg = dest->as_register();
1273       assert(VM_Version::is_P6() || dest_reg->has_byte_register(), "must use byte registers if not P6");
1274       if (VM_Version::is_P6() || from_addr.uses(dest_reg)) {
1275         __ movzwl(dest_reg, from_addr);
1276       } else {
1277         __ movw(dest_reg, from_addr);
1278       }
1279       break;
1280     }
1281 
1282     case T_SHORT: {
1283       Register dest_reg = dest->as_register();
1284       if (VM_Version::is_P6() || from_addr.uses(dest_reg)) {
1285         __ movswl(dest_reg, from_addr);
1286       } else {
1287         __ movw(dest_reg, from_addr);
1288         __ shll(dest_reg, 16);
1289         __ sarl(dest_reg, 16);
1290       }
1291       break;
1292     }
1293 
1294     default:
1295       ShouldNotReachHere();
1296   }
1297 
1298   if (patch != NULL) {
1299     patching_epilog(patch, patch_code, addr->base()->as_register(), info);
1300   }
1301 
1302   if (type == T_ARRAY || type == T_OBJECT) {
1303 #ifdef _LP64
1304     if (UseCompressedOops && !wide) {
1305       __ decode_heap_oop(dest->as_register());
1306     }
1307 #endif
1308     __ verify_oop(dest->as_register());
1309   } else if (type == T_ADDRESS && addr->disp() == oopDesc::klass_offset_in_bytes()) {
1310 #ifdef _LP64
1311     if (UseCompressedClassPointers) {
1312       __ decode_klass_not_null(dest->as_register());
1313     }
1314 #endif
1315   }
1316 }
1317 
1318 
1319 NEEDS_CLEANUP; // This could be static?
1320 Address::ScaleFactor LIR_Assembler::array_element_size(BasicType type) const {
1321   int elem_size = type2aelembytes(type);
1322   switch (elem_size) {
1323     case 1: return Address::times_1;
1324     case 2: return Address::times_2;
1325     case 4: return Address::times_4;
1326     case 8: return Address::times_8;
1327   }
1328   ShouldNotReachHere();
1329   return Address::no_scale;
1330 }
1331 
1332 
1333 void LIR_Assembler::emit_op3(LIR_Op3* op) {
1334   switch (op->code()) {
1335     case lir_idiv:
1336     case lir_irem:
1337       arithmetic_idiv(op->code(),
1338                       op->in_opr1(),
1339                       op->in_opr2(),
1340                       op->in_opr3(),
1341                       op->result_opr(),
1342                       op->info());
1343       break;
1344     default:      ShouldNotReachHere(); break;
1345   }
1346 }
1347 
1348 void LIR_Assembler::emit_opBranch(LIR_OpBranch* op) {
1349 #ifdef ASSERT
1350   assert(op->block() == NULL || op->block()->label() == op->label(), "wrong label");
1351   if (op->block() != NULL)  _branch_target_blocks.append(op->block());
1352   if (op->ublock() != NULL) _branch_target_blocks.append(op->ublock());
1353 #endif
1354 
1355   if (op->cond() == lir_cond_always) {
1356     if (op->info() != NULL) add_debug_info_for_branch(op->info());
1357     __ jmp (*(op->label()));
1358   } else {
1359     Assembler::Condition acond = Assembler::zero;
1360     if (op->code() == lir_cond_float_branch) {
1361       assert(op->ublock() != NULL, "must have unordered successor");
1362       __ jcc(Assembler::parity, *(op->ublock()->label()));
1363       switch(op->cond()) {
1364         case lir_cond_equal:        acond = Assembler::equal;      break;
1365         case lir_cond_notEqual:     acond = Assembler::notEqual;   break;
1366         case lir_cond_less:         acond = Assembler::below;      break;
1367         case lir_cond_lessEqual:    acond = Assembler::belowEqual; break;
1368         case lir_cond_greaterEqual: acond = Assembler::aboveEqual; break;
1369         case lir_cond_greater:      acond = Assembler::above;      break;
1370         default:                         ShouldNotReachHere();
1371       }
1372     } else {
1373       switch (op->cond()) {
1374         case lir_cond_equal:        acond = Assembler::equal;       break;
1375         case lir_cond_notEqual:     acond = Assembler::notEqual;    break;
1376         case lir_cond_less:         acond = Assembler::less;        break;
1377         case lir_cond_lessEqual:    acond = Assembler::lessEqual;   break;
1378         case lir_cond_greaterEqual: acond = Assembler::greaterEqual;break;
1379         case lir_cond_greater:      acond = Assembler::greater;     break;
1380         case lir_cond_belowEqual:   acond = Assembler::belowEqual;  break;
1381         case lir_cond_aboveEqual:   acond = Assembler::aboveEqual;  break;
1382         default:                         ShouldNotReachHere();
1383       }
1384     }
1385     __ jcc(acond,*(op->label()));
1386   }
1387 }
1388 
1389 void LIR_Assembler::emit_opConvert(LIR_OpConvert* op) {
1390   LIR_Opr src  = op->in_opr();
1391   LIR_Opr dest = op->result_opr();
1392 
1393   switch (op->bytecode()) {
1394     case Bytecodes::_i2l:
1395 #ifdef _LP64
1396       __ movl2ptr(dest->as_register_lo(), src->as_register());
1397 #else
1398       move_regs(src->as_register(), dest->as_register_lo());
1399       move_regs(src->as_register(), dest->as_register_hi());
1400       __ sarl(dest->as_register_hi(), 31);
1401 #endif // LP64
1402       break;
1403 
1404     case Bytecodes::_l2i:
1405 #ifdef _LP64
1406       __ movl(dest->as_register(), src->as_register_lo());
1407 #else
1408       move_regs(src->as_register_lo(), dest->as_register());
1409 #endif
1410       break;
1411 
1412     case Bytecodes::_i2b:
1413       move_regs(src->as_register(), dest->as_register());
1414       __ sign_extend_byte(dest->as_register());
1415       break;
1416 
1417     case Bytecodes::_i2c:
1418       move_regs(src->as_register(), dest->as_register());
1419       __ andl(dest->as_register(), 0xFFFF);
1420       break;
1421 
1422     case Bytecodes::_i2s:
1423       move_regs(src->as_register(), dest->as_register());
1424       __ sign_extend_short(dest->as_register());
1425       break;
1426 
1427 
1428     case Bytecodes::_f2d:
1429     case Bytecodes::_d2f:
1430       if (dest->is_single_xmm()) {
1431         __ cvtsd2ss(dest->as_xmm_float_reg(), src->as_xmm_double_reg());
1432       } else if (dest->is_double_xmm()) {
1433         __ cvtss2sd(dest->as_xmm_double_reg(), src->as_xmm_float_reg());
1434       } else {
1435         assert(src->fpu() == dest->fpu(), "register must be equal");
1436         // do nothing (float result is rounded later through spilling)
1437       }
1438       break;
1439 
1440     case Bytecodes::_i2f:
1441     case Bytecodes::_i2d:
1442       if (dest->is_single_xmm()) {
1443         __ cvtsi2ssl(dest->as_xmm_float_reg(), src->as_register());
1444       } else if (dest->is_double_xmm()) {
1445         __ cvtsi2sdl(dest->as_xmm_double_reg(), src->as_register());
1446       } else {
1447         assert(dest->fpu() == 0, "result must be on TOS");
1448         __ movl(Address(rsp, 0), src->as_register());
1449         __ fild_s(Address(rsp, 0));
1450       }
1451       break;
1452 
1453     case Bytecodes::_f2i:
1454     case Bytecodes::_d2i:
1455       if (src->is_single_xmm()) {
1456         __ cvttss2sil(dest->as_register(), src->as_xmm_float_reg());
1457       } else if (src->is_double_xmm()) {
1458         __ cvttsd2sil(dest->as_register(), src->as_xmm_double_reg());
1459       } else {
1460         assert(src->fpu() == 0, "input must be on TOS");
1461         __ fldcw(ExternalAddress(StubRoutines::addr_fpu_cntrl_wrd_trunc()));
1462         __ fist_s(Address(rsp, 0));
1463         __ movl(dest->as_register(), Address(rsp, 0));
1464         __ fldcw(ExternalAddress(StubRoutines::addr_fpu_cntrl_wrd_std()));
1465       }
1466 
1467       // IA32 conversion instructions do not match JLS for overflow, underflow and NaN -> fixup in stub
1468       assert(op->stub() != NULL, "stub required");
1469       __ cmpl(dest->as_register(), 0x80000000);
1470       __ jcc(Assembler::equal, *op->stub()->entry());
1471       __ bind(*op->stub()->continuation());
1472       break;
1473 
1474     case Bytecodes::_l2f:
1475     case Bytecodes::_l2d:
1476       assert(!dest->is_xmm_register(), "result in xmm register not supported (no SSE instruction present)");
1477       assert(dest->fpu() == 0, "result must be on TOS");
1478 
1479       __ movptr(Address(rsp, 0),            src->as_register_lo());
1480       NOT_LP64(__ movl(Address(rsp, BytesPerWord), src->as_register_hi()));
1481       __ fild_d(Address(rsp, 0));
1482       // float result is rounded later through spilling
1483       break;
1484 
1485     case Bytecodes::_f2l:
1486     case Bytecodes::_d2l:
1487       assert(!src->is_xmm_register(), "input in xmm register not supported (no SSE instruction present)");
1488       assert(src->fpu() == 0, "input must be on TOS");
1489       assert(dest == FrameMap::long0_opr, "runtime stub places result in these registers");
1490 
1491       // instruction sequence too long to inline it here
1492       {
1493         __ call(RuntimeAddress(Runtime1::entry_for(Runtime1::fpu2long_stub_id)));
1494       }
1495       break;
1496 
1497     default: ShouldNotReachHere();
1498   }
1499 }
1500 
1501 void LIR_Assembler::emit_alloc_obj(LIR_OpAllocObj* op) {
1502   if (op->init_check()) {
1503     __ cmpb(Address(op->klass()->as_register(),
1504                     InstanceKlass::init_state_offset()),
1505                     InstanceKlass::fully_initialized);
1506     add_debug_info_for_null_check_here(op->stub()->info());
1507     __ jcc(Assembler::notEqual, *op->stub()->entry());
1508   }
1509   __ allocate_object(op->obj()->as_register(),
1510                      op->tmp1()->as_register(),
1511                      op->tmp2()->as_register(),
1512                      op->header_size(),
1513                      op->object_size(),
1514                      op->klass()->as_register(),
1515                      *op->stub()->entry());
1516   __ bind(*op->stub()->continuation());
1517 }
1518 
1519 void LIR_Assembler::emit_alloc_array(LIR_OpAllocArray* op) {
1520   Register len =  op->len()->as_register();
1521   LP64_ONLY( __ movslq(len, len); )
1522 
1523   if (UseSlowPath ||
1524       (!UseFastNewObjectArray && (op->type() == T_OBJECT || op->type() == T_ARRAY)) ||
1525       (!UseFastNewTypeArray   && (op->type() != T_OBJECT && op->type() != T_ARRAY))) {
1526     __ jmp(*op->stub()->entry());
1527   } else {
1528     Register tmp1 = op->tmp1()->as_register();
1529     Register tmp2 = op->tmp2()->as_register();
1530     Register tmp3 = op->tmp3()->as_register();
1531     if (len == tmp1) {
1532       tmp1 = tmp3;
1533     } else if (len == tmp2) {
1534       tmp2 = tmp3;
1535     } else if (len == tmp3) {
1536       // everything is ok
1537     } else {
1538       __ mov(tmp3, len);
1539     }
1540     __ allocate_array(op->obj()->as_register(),
1541                       len,
1542                       tmp1,
1543                       tmp2,
1544                       arrayOopDesc::header_size(op->type()),
1545                       array_element_size(op->type()),
1546                       op->klass()->as_register(),
1547                       *op->stub()->entry());
1548   }
1549   __ bind(*op->stub()->continuation());
1550 }
1551 
1552 void LIR_Assembler::type_profile_helper(Register mdo,
1553                                         ciMethodData *md, ciProfileData *data,
1554                                         Register recv, Label* update_done) {
1555   for (uint i = 0; i < ReceiverTypeData::row_limit(); i++) {
1556     Label next_test;
1557     // See if the receiver is receiver[n].
1558     __ cmpptr(recv, Address(mdo, md->byte_offset_of_slot(data, ReceiverTypeData::receiver_offset(i))));
1559     __ jccb(Assembler::notEqual, next_test);
1560     Address data_addr(mdo, md->byte_offset_of_slot(data, ReceiverTypeData::receiver_count_offset(i)));
1561     __ addptr(data_addr, DataLayout::counter_increment);
1562     __ jmp(*update_done);
1563     __ bind(next_test);
1564   }
1565 
1566   // Didn't find receiver; find next empty slot and fill it in
1567   for (uint i = 0; i < ReceiverTypeData::row_limit(); i++) {
1568     Label next_test;
1569     Address recv_addr(mdo, md->byte_offset_of_slot(data, ReceiverTypeData::receiver_offset(i)));
1570     __ cmpptr(recv_addr, (intptr_t)NULL_WORD);
1571     __ jccb(Assembler::notEqual, next_test);
1572     __ movptr(recv_addr, recv);
1573     __ movptr(Address(mdo, md->byte_offset_of_slot(data, ReceiverTypeData::receiver_count_offset(i))), DataLayout::counter_increment);
1574     __ jmp(*update_done);
1575     __ bind(next_test);
1576   }
1577 }
1578 
1579 void LIR_Assembler::emit_typecheck_helper(LIR_OpTypeCheck *op, Label* success, Label* failure, Label* obj_is_null) {
1580   // we always need a stub for the failure case.
1581   CodeStub* stub = op->stub();
1582   Register obj = op->object()->as_register();
1583   Register k_RInfo = op->tmp1()->as_register();
1584   Register klass_RInfo = op->tmp2()->as_register();
1585   Register dst = op->result_opr()->as_register();
1586   ciKlass* k = op->klass();
1587   Register Rtmp1 = noreg;
1588 
1589   // check if it needs to be profiled
1590   ciMethodData* md = NULL;
1591   ciProfileData* data = NULL;
1592 
1593   if (op->should_profile()) {
1594     ciMethod* method = op->profiled_method();
1595     assert(method != NULL, "Should have method");
1596     int bci = op->profiled_bci();
1597     md = method->method_data_or_null();
1598     assert(md != NULL, "Sanity");
1599     data = md->bci_to_data(bci);
1600     assert(data != NULL,                "need data for type check");
1601     assert(data->is_ReceiverTypeData(), "need ReceiverTypeData for type check");
1602   }
1603   Label profile_cast_success, profile_cast_failure;
1604   Label *success_target = op->should_profile() ? &profile_cast_success : success;
1605   Label *failure_target = op->should_profile() ? &profile_cast_failure : failure;
1606 
1607   if (obj == k_RInfo) {
1608     k_RInfo = dst;
1609   } else if (obj == klass_RInfo) {
1610     klass_RInfo = dst;
1611   }
1612   if (k->is_loaded() && !UseCompressedClassPointers) {
1613     select_different_registers(obj, dst, k_RInfo, klass_RInfo);
1614   } else {
1615     Rtmp1 = op->tmp3()->as_register();
1616     select_different_registers(obj, dst, k_RInfo, klass_RInfo, Rtmp1);
1617   }
1618 
1619   assert_different_registers(obj, k_RInfo, klass_RInfo);
1620 
1621   __ cmpptr(obj, (int32_t)NULL_WORD);
1622   if (op->should_profile()) {
1623     Label not_null;
1624     __ jccb(Assembler::notEqual, not_null);
1625     // Object is null; update MDO and exit
1626     Register mdo  = klass_RInfo;
1627     __ mov_metadata(mdo, md->constant_encoding());
1628     Address data_addr(mdo, md->byte_offset_of_slot(data, DataLayout::header_offset()));
1629     int header_bits = DataLayout::flag_mask_to_header_mask(BitData::null_seen_byte_constant());
1630     __ orl(data_addr, header_bits);
1631     __ jmp(*obj_is_null);
1632     __ bind(not_null);
1633   } else {
1634     __ jcc(Assembler::equal, *obj_is_null);
1635   }
1636 
1637   if (!k->is_loaded()) {
1638     klass2reg_with_patching(k_RInfo, op->info_for_patch());
1639   } else {
1640 #ifdef _LP64
1641     __ mov_metadata(k_RInfo, k->constant_encoding());
1642 #endif // _LP64
1643   }
1644   __ verify_oop(obj);
1645 
1646   if (op->fast_check()) {
1647     // get object class
1648     // not a safepoint as obj null check happens earlier
1649 #ifdef _LP64
1650     if (UseCompressedClassPointers) {
1651       __ load_klass(Rtmp1, obj);
1652       __ cmpptr(k_RInfo, Rtmp1);
1653     } else {
1654       __ cmpptr(k_RInfo, Address(obj, oopDesc::klass_offset_in_bytes()));
1655     }
1656 #else
1657     if (k->is_loaded()) {
1658       __ cmpklass(Address(obj, oopDesc::klass_offset_in_bytes()), k->constant_encoding());
1659     } else {
1660       __ cmpptr(k_RInfo, Address(obj, oopDesc::klass_offset_in_bytes()));
1661     }
1662 #endif
1663     __ jcc(Assembler::notEqual, *failure_target);
1664     // successful cast, fall through to profile or jump
1665   } else {
1666     // get object class
1667     // not a safepoint as obj null check happens earlier
1668     __ load_klass(klass_RInfo, obj);
1669     if (k->is_loaded()) {
1670       // See if we get an immediate positive hit
1671 #ifdef _LP64
1672       __ cmpptr(k_RInfo, Address(klass_RInfo, k->super_check_offset()));
1673 #else
1674       __ cmpklass(Address(klass_RInfo, k->super_check_offset()), k->constant_encoding());
1675 #endif // _LP64
1676       if ((juint)in_bytes(Klass::secondary_super_cache_offset()) != k->super_check_offset()) {
1677         __ jcc(Assembler::notEqual, *failure_target);
1678         // successful cast, fall through to profile or jump
1679       } else {
1680         // See if we get an immediate positive hit
1681         __ jcc(Assembler::equal, *success_target);
1682         // check for self
1683 #ifdef _LP64
1684         __ cmpptr(klass_RInfo, k_RInfo);
1685 #else
1686         __ cmpklass(klass_RInfo, k->constant_encoding());
1687 #endif // _LP64
1688         __ jcc(Assembler::equal, *success_target);
1689 
1690         __ push(klass_RInfo);
1691 #ifdef _LP64
1692         __ push(k_RInfo);
1693 #else
1694         __ pushklass(k->constant_encoding());
1695 #endif // _LP64
1696         __ call(RuntimeAddress(Runtime1::entry_for(Runtime1::slow_subtype_check_id)));
1697         __ pop(klass_RInfo);
1698         __ pop(klass_RInfo);
1699         // result is a boolean
1700         __ cmpl(klass_RInfo, 0);
1701         __ jcc(Assembler::equal, *failure_target);
1702         // successful cast, fall through to profile or jump
1703       }
1704     } else {
1705       // perform the fast part of the checking logic
1706       __ check_klass_subtype_fast_path(klass_RInfo, k_RInfo, Rtmp1, success_target, failure_target, NULL);
1707       // call out-of-line instance of __ check_klass_subtype_slow_path(...):
1708       __ push(klass_RInfo);
1709       __ push(k_RInfo);
1710       __ call(RuntimeAddress(Runtime1::entry_for(Runtime1::slow_subtype_check_id)));
1711       __ pop(klass_RInfo);
1712       __ pop(k_RInfo);
1713       // result is a boolean
1714       __ cmpl(k_RInfo, 0);
1715       __ jcc(Assembler::equal, *failure_target);
1716       // successful cast, fall through to profile or jump
1717     }
1718   }
1719   if (op->should_profile()) {
1720     Register mdo  = klass_RInfo, recv = k_RInfo;
1721     __ bind(profile_cast_success);
1722     __ mov_metadata(mdo, md->constant_encoding());
1723     __ load_klass(recv, obj);
1724     Label update_done;
1725     type_profile_helper(mdo, md, data, recv, success);
1726     __ jmp(*success);
1727 
1728     __ bind(profile_cast_failure);
1729     __ mov_metadata(mdo, md->constant_encoding());
1730     Address counter_addr(mdo, md->byte_offset_of_slot(data, CounterData::count_offset()));
1731     __ subptr(counter_addr, DataLayout::counter_increment);
1732     __ jmp(*failure);
1733   }
1734   __ jmp(*success);
1735 }
1736 
1737 
1738 void LIR_Assembler::emit_opTypeCheck(LIR_OpTypeCheck* op) {
1739   LIR_Code code = op->code();
1740   if (code == lir_store_check) {
1741     Register value = op->object()->as_register();
1742     Register array = op->array()->as_register();
1743     Register k_RInfo = op->tmp1()->as_register();
1744     Register klass_RInfo = op->tmp2()->as_register();
1745     Register Rtmp1 = op->tmp3()->as_register();
1746 
1747     CodeStub* stub = op->stub();
1748 
1749     // check if it needs to be profiled
1750     ciMethodData* md = NULL;
1751     ciProfileData* data = NULL;
1752 
1753     if (op->should_profile()) {
1754       ciMethod* method = op->profiled_method();
1755       assert(method != NULL, "Should have method");
1756       int bci = op->profiled_bci();
1757       md = method->method_data_or_null();
1758       assert(md != NULL, "Sanity");
1759       data = md->bci_to_data(bci);
1760       assert(data != NULL,                "need data for type check");
1761       assert(data->is_ReceiverTypeData(), "need ReceiverTypeData for type check");
1762     }
1763     Label profile_cast_success, profile_cast_failure, done;
1764     Label *success_target = op->should_profile() ? &profile_cast_success : &done;
1765     Label *failure_target = op->should_profile() ? &profile_cast_failure : stub->entry();
1766 
1767     __ cmpptr(value, (int32_t)NULL_WORD);
1768     if (op->should_profile()) {
1769       Label not_null;
1770       __ jccb(Assembler::notEqual, not_null);
1771       // Object is null; update MDO and exit
1772       Register mdo  = klass_RInfo;
1773       __ mov_metadata(mdo, md->constant_encoding());
1774       Address data_addr(mdo, md->byte_offset_of_slot(data, DataLayout::header_offset()));
1775       int header_bits = DataLayout::flag_mask_to_header_mask(BitData::null_seen_byte_constant());
1776       __ orl(data_addr, header_bits);
1777       __ jmp(done);
1778       __ bind(not_null);
1779     } else {
1780       __ jcc(Assembler::equal, done);
1781     }
1782 
1783     add_debug_info_for_null_check_here(op->info_for_exception());
1784     __ load_klass(k_RInfo, array);
1785     __ load_klass(klass_RInfo, value);
1786 
1787     // get instance klass (it's already uncompressed)
1788     __ movptr(k_RInfo, Address(k_RInfo, ObjArrayKlass::element_klass_offset()));
1789     // perform the fast part of the checking logic
1790     __ check_klass_subtype_fast_path(klass_RInfo, k_RInfo, Rtmp1, success_target, failure_target, NULL);
1791     // call out-of-line instance of __ check_klass_subtype_slow_path(...):
1792     __ push(klass_RInfo);
1793     __ push(k_RInfo);
1794     __ call(RuntimeAddress(Runtime1::entry_for(Runtime1::slow_subtype_check_id)));
1795     __ pop(klass_RInfo);
1796     __ pop(k_RInfo);
1797     // result is a boolean
1798     __ cmpl(k_RInfo, 0);
1799     __ jcc(Assembler::equal, *failure_target);
1800     // fall through to the success case
1801 
1802     if (op->should_profile()) {
1803       Register mdo  = klass_RInfo, recv = k_RInfo;
1804       __ bind(profile_cast_success);
1805       __ mov_metadata(mdo, md->constant_encoding());
1806       __ load_klass(recv, value);
1807       Label update_done;
1808       type_profile_helper(mdo, md, data, recv, &done);
1809       __ jmpb(done);
1810 
1811       __ bind(profile_cast_failure);
1812       __ mov_metadata(mdo, md->constant_encoding());
1813       Address counter_addr(mdo, md->byte_offset_of_slot(data, CounterData::count_offset()));
1814       __ subptr(counter_addr, DataLayout::counter_increment);
1815       __ jmp(*stub->entry());
1816     }
1817 
1818     __ bind(done);
1819   } else
1820     if (code == lir_checkcast) {
1821       Register obj = op->object()->as_register();
1822       Register dst = op->result_opr()->as_register();
1823       Label success;
1824       emit_typecheck_helper(op, &success, op->stub()->entry(), &success);
1825       __ bind(success);
1826       if (dst != obj) {
1827         __ mov(dst, obj);
1828       }
1829     } else
1830       if (code == lir_instanceof) {
1831         Register obj = op->object()->as_register();
1832         Register dst = op->result_opr()->as_register();
1833         Label success, failure, done;
1834         emit_typecheck_helper(op, &success, &failure, &failure);
1835         __ bind(failure);
1836         __ xorptr(dst, dst);
1837         __ jmpb(done);
1838         __ bind(success);
1839         __ movptr(dst, 1);
1840         __ bind(done);
1841       } else {
1842         ShouldNotReachHere();
1843       }
1844 
1845 }
1846 
1847 
1848 void LIR_Assembler::emit_compare_and_swap(LIR_OpCompareAndSwap* op) {
1849   if (LP64_ONLY(false &&) op->code() == lir_cas_long && VM_Version::supports_cx8()) {
1850     assert(op->cmp_value()->as_register_lo() == rax, "wrong register");
1851     assert(op->cmp_value()->as_register_hi() == rdx, "wrong register");
1852     assert(op->new_value()->as_register_lo() == rbx, "wrong register");
1853     assert(op->new_value()->as_register_hi() == rcx, "wrong register");
1854     Register addr = op->addr()->as_register();
1855     if (os::is_MP()) {
1856       __ lock();
1857     }
1858     NOT_LP64(__ cmpxchg8(Address(addr, 0)));
1859 
1860   } else if (op->code() == lir_cas_int || op->code() == lir_cas_obj ) {
1861     NOT_LP64(assert(op->addr()->is_single_cpu(), "must be single");)
1862     Register addr = (op->addr()->is_single_cpu() ? op->addr()->as_register() : op->addr()->as_register_lo());
1863     Register newval = op->new_value()->as_register();
1864     Register cmpval = op->cmp_value()->as_register();
1865     assert(cmpval == rax, "wrong register");
1866     assert(newval != NULL, "new val must be register");
1867     assert(cmpval != newval, "cmp and new values must be in different registers");
1868     assert(cmpval != addr, "cmp and addr must be in different registers");
1869     assert(newval != addr, "new value and addr must be in different registers");
1870 
1871     if ( op->code() == lir_cas_obj) {
1872 #ifdef _LP64
1873       if (UseCompressedOops) {
1874         __ encode_heap_oop(cmpval);
1875         __ mov(rscratch1, newval);
1876         __ encode_heap_oop(rscratch1);
1877         if (os::is_MP()) {
1878           __ lock();
1879         }
1880         // cmpval (rax) is implicitly used by this instruction
1881         __ cmpxchgl(rscratch1, Address(addr, 0));
1882       } else
1883 #endif
1884       {
1885         if (os::is_MP()) {
1886           __ lock();
1887         }
1888         __ cmpxchgptr(newval, Address(addr, 0));
1889       }
1890     } else {
1891       assert(op->code() == lir_cas_int, "lir_cas_int expected");
1892       if (os::is_MP()) {
1893         __ lock();
1894       }
1895       __ cmpxchgl(newval, Address(addr, 0));
1896     }
1897 #ifdef _LP64
1898   } else if (op->code() == lir_cas_long) {
1899     Register addr = (op->addr()->is_single_cpu() ? op->addr()->as_register() : op->addr()->as_register_lo());
1900     Register newval = op->new_value()->as_register_lo();
1901     Register cmpval = op->cmp_value()->as_register_lo();
1902     assert(cmpval == rax, "wrong register");
1903     assert(newval != NULL, "new val must be register");
1904     assert(cmpval != newval, "cmp and new values must be in different registers");
1905     assert(cmpval != addr, "cmp and addr must be in different registers");
1906     assert(newval != addr, "new value and addr must be in different registers");
1907     if (os::is_MP()) {
1908       __ lock();
1909     }
1910     __ cmpxchgq(newval, Address(addr, 0));
1911 #endif // _LP64
1912   } else {
1913     Unimplemented();
1914   }
1915 }
1916 
1917 void LIR_Assembler::cmove(LIR_Condition condition, LIR_Opr opr1, LIR_Opr opr2, LIR_Opr result, BasicType type) {
1918   Assembler::Condition acond, ncond;
1919   switch (condition) {
1920     case lir_cond_equal:        acond = Assembler::equal;        ncond = Assembler::notEqual;     break;
1921     case lir_cond_notEqual:     acond = Assembler::notEqual;     ncond = Assembler::equal;        break;
1922     case lir_cond_less:         acond = Assembler::less;         ncond = Assembler::greaterEqual; break;
1923     case lir_cond_lessEqual:    acond = Assembler::lessEqual;    ncond = Assembler::greater;      break;
1924     case lir_cond_greaterEqual: acond = Assembler::greaterEqual; ncond = Assembler::less;         break;
1925     case lir_cond_greater:      acond = Assembler::greater;      ncond = Assembler::lessEqual;    break;
1926     case lir_cond_belowEqual:   acond = Assembler::belowEqual;   ncond = Assembler::above;        break;
1927     case lir_cond_aboveEqual:   acond = Assembler::aboveEqual;   ncond = Assembler::below;        break;
1928     default:                    acond = Assembler::equal;        ncond = Assembler::notEqual;
1929                                 ShouldNotReachHere();
1930   }
1931 
1932   if (opr1->is_cpu_register()) {
1933     reg2reg(opr1, result);
1934   } else if (opr1->is_stack()) {
1935     stack2reg(opr1, result, result->type());
1936   } else if (opr1->is_constant()) {
1937     const2reg(opr1, result, lir_patch_none, NULL);
1938   } else {
1939     ShouldNotReachHere();
1940   }
1941 
1942   if (VM_Version::supports_cmov() && !opr2->is_constant()) {
1943     // optimized version that does not require a branch
1944     if (opr2->is_single_cpu()) {
1945       assert(opr2->cpu_regnr() != result->cpu_regnr(), "opr2 already overwritten by previous move");
1946       __ cmov(ncond, result->as_register(), opr2->as_register());
1947     } else if (opr2->is_double_cpu()) {
1948       assert(opr2->cpu_regnrLo() != result->cpu_regnrLo() && opr2->cpu_regnrLo() != result->cpu_regnrHi(), "opr2 already overwritten by previous move");
1949       assert(opr2->cpu_regnrHi() != result->cpu_regnrLo() && opr2->cpu_regnrHi() != result->cpu_regnrHi(), "opr2 already overwritten by previous move");
1950       __ cmovptr(ncond, result->as_register_lo(), opr2->as_register_lo());
1951       NOT_LP64(__ cmovptr(ncond, result->as_register_hi(), opr2->as_register_hi());)
1952     } else if (opr2->is_single_stack()) {
1953       __ cmovl(ncond, result->as_register(), frame_map()->address_for_slot(opr2->single_stack_ix()));
1954     } else if (opr2->is_double_stack()) {
1955       __ cmovptr(ncond, result->as_register_lo(), frame_map()->address_for_slot(opr2->double_stack_ix(), lo_word_offset_in_bytes));
1956       NOT_LP64(__ cmovptr(ncond, result->as_register_hi(), frame_map()->address_for_slot(opr2->double_stack_ix(), hi_word_offset_in_bytes));)
1957     } else {
1958       ShouldNotReachHere();
1959     }
1960 
1961   } else {
1962     Label skip;
1963     __ jcc (acond, skip);
1964     if (opr2->is_cpu_register()) {
1965       reg2reg(opr2, result);
1966     } else if (opr2->is_stack()) {
1967       stack2reg(opr2, result, result->type());
1968     } else if (opr2->is_constant()) {
1969       const2reg(opr2, result, lir_patch_none, NULL);
1970     } else {
1971       ShouldNotReachHere();
1972     }
1973     __ bind(skip);
1974   }
1975 }
1976 
1977 
1978 void LIR_Assembler::arith_op(LIR_Code code, LIR_Opr left, LIR_Opr right, LIR_Opr dest, CodeEmitInfo* info, bool pop_fpu_stack) {
1979   assert(info == NULL, "should never be used, idiv/irem and ldiv/lrem not handled by this method");
1980 
1981   if (left->is_single_cpu()) {
1982     assert(left == dest, "left and dest must be equal");
1983     Register lreg = left->as_register();
1984 
1985     if (right->is_single_cpu()) {
1986       // cpu register - cpu register
1987       Register rreg = right->as_register();
1988       switch (code) {
1989         case lir_add: __ addl (lreg, rreg); break;
1990         case lir_sub: __ subl (lreg, rreg); break;
1991         case lir_mul: __ imull(lreg, rreg); break;
1992         default:      ShouldNotReachHere();
1993       }
1994 
1995     } else if (right->is_stack()) {
1996       // cpu register - stack
1997       Address raddr = frame_map()->address_for_slot(right->single_stack_ix());
1998       switch (code) {
1999         case lir_add: __ addl(lreg, raddr); break;
2000         case lir_sub: __ subl(lreg, raddr); break;
2001         default:      ShouldNotReachHere();
2002       }
2003 
2004     } else if (right->is_constant()) {
2005       // cpu register - constant
2006       jint c = right->as_constant_ptr()->as_jint();
2007       switch (code) {
2008         case lir_add: {
2009           __ incrementl(lreg, c);
2010           break;
2011         }
2012         case lir_sub: {
2013           __ decrementl(lreg, c);
2014           break;
2015         }
2016         default: ShouldNotReachHere();
2017       }
2018 
2019     } else {
2020       ShouldNotReachHere();
2021     }
2022 
2023   } else if (left->is_double_cpu()) {
2024     assert(left == dest, "left and dest must be equal");
2025     Register lreg_lo = left->as_register_lo();
2026     Register lreg_hi = left->as_register_hi();
2027 
2028     if (right->is_double_cpu()) {
2029       // cpu register - cpu register
2030       Register rreg_lo = right->as_register_lo();
2031       Register rreg_hi = right->as_register_hi();
2032       NOT_LP64(assert_different_registers(lreg_lo, lreg_hi, rreg_lo, rreg_hi));
2033       LP64_ONLY(assert_different_registers(lreg_lo, rreg_lo));
2034       switch (code) {
2035         case lir_add:
2036           __ addptr(lreg_lo, rreg_lo);
2037           NOT_LP64(__ adcl(lreg_hi, rreg_hi));
2038           break;
2039         case lir_sub:
2040           __ subptr(lreg_lo, rreg_lo);
2041           NOT_LP64(__ sbbl(lreg_hi, rreg_hi));
2042           break;
2043         case lir_mul:
2044 #ifdef _LP64
2045           __ imulq(lreg_lo, rreg_lo);
2046 #else
2047           assert(lreg_lo == rax && lreg_hi == rdx, "must be");
2048           __ imull(lreg_hi, rreg_lo);
2049           __ imull(rreg_hi, lreg_lo);
2050           __ addl (rreg_hi, lreg_hi);
2051           __ mull (rreg_lo);
2052           __ addl (lreg_hi, rreg_hi);
2053 #endif // _LP64
2054           break;
2055         default:
2056           ShouldNotReachHere();
2057       }
2058 
2059     } else if (right->is_constant()) {
2060       // cpu register - constant
2061 #ifdef _LP64
2062       jlong c = right->as_constant_ptr()->as_jlong_bits();
2063       __ movptr(r10, (intptr_t) c);
2064       switch (code) {
2065         case lir_add:
2066           __ addptr(lreg_lo, r10);
2067           break;
2068         case lir_sub:
2069           __ subptr(lreg_lo, r10);
2070           break;
2071         default:
2072           ShouldNotReachHere();
2073       }
2074 #else
2075       jint c_lo = right->as_constant_ptr()->as_jint_lo();
2076       jint c_hi = right->as_constant_ptr()->as_jint_hi();
2077       switch (code) {
2078         case lir_add:
2079           __ addptr(lreg_lo, c_lo);
2080           __ adcl(lreg_hi, c_hi);
2081           break;
2082         case lir_sub:
2083           __ subptr(lreg_lo, c_lo);
2084           __ sbbl(lreg_hi, c_hi);
2085           break;
2086         default:
2087           ShouldNotReachHere();
2088       }
2089 #endif // _LP64
2090 
2091     } else {
2092       ShouldNotReachHere();
2093     }
2094 
2095   } else if (left->is_single_xmm()) {
2096     assert(left == dest, "left and dest must be equal");
2097     XMMRegister lreg = left->as_xmm_float_reg();
2098 
2099     if (right->is_single_xmm()) {
2100       XMMRegister rreg = right->as_xmm_float_reg();
2101       switch (code) {
2102         case lir_add: __ addss(lreg, rreg);  break;
2103         case lir_sub: __ subss(lreg, rreg);  break;
2104         case lir_mul_strictfp: // fall through
2105         case lir_mul: __ mulss(lreg, rreg);  break;
2106         case lir_div_strictfp: // fall through
2107         case lir_div: __ divss(lreg, rreg);  break;
2108         default: ShouldNotReachHere();
2109       }
2110     } else {
2111       Address raddr;
2112       if (right->is_single_stack()) {
2113         raddr = frame_map()->address_for_slot(right->single_stack_ix());
2114       } else if (right->is_constant()) {
2115         // hack for now
2116         raddr = __ as_Address(InternalAddress(float_constant(right->as_jfloat())));
2117       } else {
2118         ShouldNotReachHere();
2119       }
2120       switch (code) {
2121         case lir_add: __ addss(lreg, raddr);  break;
2122         case lir_sub: __ subss(lreg, raddr);  break;
2123         case lir_mul_strictfp: // fall through
2124         case lir_mul: __ mulss(lreg, raddr);  break;
2125         case lir_div_strictfp: // fall through
2126         case lir_div: __ divss(lreg, raddr);  break;
2127         default: ShouldNotReachHere();
2128       }
2129     }
2130 
2131   } else if (left->is_double_xmm()) {
2132     assert(left == dest, "left and dest must be equal");
2133 
2134     XMMRegister lreg = left->as_xmm_double_reg();
2135     if (right->is_double_xmm()) {
2136       XMMRegister rreg = right->as_xmm_double_reg();
2137       switch (code) {
2138         case lir_add: __ addsd(lreg, rreg);  break;
2139         case lir_sub: __ subsd(lreg, rreg);  break;
2140         case lir_mul_strictfp: // fall through
2141         case lir_mul: __ mulsd(lreg, rreg);  break;
2142         case lir_div_strictfp: // fall through
2143         case lir_div: __ divsd(lreg, rreg);  break;
2144         default: ShouldNotReachHere();
2145       }
2146     } else {
2147       Address raddr;
2148       if (right->is_double_stack()) {
2149         raddr = frame_map()->address_for_slot(right->double_stack_ix());
2150       } else if (right->is_constant()) {
2151         // hack for now
2152         raddr = __ as_Address(InternalAddress(double_constant(right->as_jdouble())));
2153       } else {
2154         ShouldNotReachHere();
2155       }
2156       switch (code) {
2157         case lir_add: __ addsd(lreg, raddr);  break;
2158         case lir_sub: __ subsd(lreg, raddr);  break;
2159         case lir_mul_strictfp: // fall through
2160         case lir_mul: __ mulsd(lreg, raddr);  break;
2161         case lir_div_strictfp: // fall through
2162         case lir_div: __ divsd(lreg, raddr);  break;
2163         default: ShouldNotReachHere();
2164       }
2165     }
2166 
2167   } else if (left->is_single_fpu()) {
2168     assert(dest->is_single_fpu(),  "fpu stack allocation required");
2169 
2170     if (right->is_single_fpu()) {
2171       arith_fpu_implementation(code, left->fpu_regnr(), right->fpu_regnr(), dest->fpu_regnr(), pop_fpu_stack);
2172 
2173     } else {
2174       assert(left->fpu_regnr() == 0, "left must be on TOS");
2175       assert(dest->fpu_regnr() == 0, "dest must be on TOS");
2176 
2177       Address raddr;
2178       if (right->is_single_stack()) {
2179         raddr = frame_map()->address_for_slot(right->single_stack_ix());
2180       } else if (right->is_constant()) {
2181         address const_addr = float_constant(right->as_jfloat());
2182         assert(const_addr != NULL, "incorrect float/double constant maintainance");
2183         // hack for now
2184         raddr = __ as_Address(InternalAddress(const_addr));
2185       } else {
2186         ShouldNotReachHere();
2187       }
2188 
2189       switch (code) {
2190         case lir_add: __ fadd_s(raddr); break;
2191         case lir_sub: __ fsub_s(raddr); break;
2192         case lir_mul_strictfp: // fall through
2193         case lir_mul: __ fmul_s(raddr); break;
2194         case lir_div_strictfp: // fall through
2195         case lir_div: __ fdiv_s(raddr); break;
2196         default:      ShouldNotReachHere();
2197       }
2198     }
2199 
2200   } else if (left->is_double_fpu()) {
2201     assert(dest->is_double_fpu(),  "fpu stack allocation required");
2202 
2203     if (code == lir_mul_strictfp || code == lir_div_strictfp) {
2204       // Double values require special handling for strictfp mul/div on x86
2205       __ fld_x(ExternalAddress(StubRoutines::addr_fpu_subnormal_bias1()));
2206       __ fmulp(left->fpu_regnrLo() + 1);
2207     }
2208 
2209     if (right->is_double_fpu()) {
2210       arith_fpu_implementation(code, left->fpu_regnrLo(), right->fpu_regnrLo(), dest->fpu_regnrLo(), pop_fpu_stack);
2211 
2212     } else {
2213       assert(left->fpu_regnrLo() == 0, "left must be on TOS");
2214       assert(dest->fpu_regnrLo() == 0, "dest must be on TOS");
2215 
2216       Address raddr;
2217       if (right->is_double_stack()) {
2218         raddr = frame_map()->address_for_slot(right->double_stack_ix());
2219       } else if (right->is_constant()) {
2220         // hack for now
2221         raddr = __ as_Address(InternalAddress(double_constant(right->as_jdouble())));
2222       } else {
2223         ShouldNotReachHere();
2224       }
2225 
2226       switch (code) {
2227         case lir_add: __ fadd_d(raddr); break;
2228         case lir_sub: __ fsub_d(raddr); break;
2229         case lir_mul_strictfp: // fall through
2230         case lir_mul: __ fmul_d(raddr); break;
2231         case lir_div_strictfp: // fall through
2232         case lir_div: __ fdiv_d(raddr); break;
2233         default: ShouldNotReachHere();
2234       }
2235     }
2236 
2237     if (code == lir_mul_strictfp || code == lir_div_strictfp) {
2238       // Double values require special handling for strictfp mul/div on x86
2239       __ fld_x(ExternalAddress(StubRoutines::addr_fpu_subnormal_bias2()));
2240       __ fmulp(dest->fpu_regnrLo() + 1);
2241     }
2242 
2243   } else if (left->is_single_stack() || left->is_address()) {
2244     assert(left == dest, "left and dest must be equal");
2245 
2246     Address laddr;
2247     if (left->is_single_stack()) {
2248       laddr = frame_map()->address_for_slot(left->single_stack_ix());
2249     } else if (left->is_address()) {
2250       laddr = as_Address(left->as_address_ptr());
2251     } else {
2252       ShouldNotReachHere();
2253     }
2254 
2255     if (right->is_single_cpu()) {
2256       Register rreg = right->as_register();
2257       switch (code) {
2258         case lir_add: __ addl(laddr, rreg); break;
2259         case lir_sub: __ subl(laddr, rreg); break;
2260         default:      ShouldNotReachHere();
2261       }
2262     } else if (right->is_constant()) {
2263       jint c = right->as_constant_ptr()->as_jint();
2264       switch (code) {
2265         case lir_add: {
2266           __ incrementl(laddr, c);
2267           break;
2268         }
2269         case lir_sub: {
2270           __ decrementl(laddr, c);
2271           break;
2272         }
2273         default: ShouldNotReachHere();
2274       }
2275     } else {
2276       ShouldNotReachHere();
2277     }
2278 
2279   } else {
2280     ShouldNotReachHere();
2281   }
2282 }
2283 
2284 void LIR_Assembler::arith_fpu_implementation(LIR_Code code, int left_index, int right_index, int dest_index, bool pop_fpu_stack) {
2285   assert(pop_fpu_stack  || (left_index     == dest_index || right_index     == dest_index), "invalid LIR");
2286   assert(!pop_fpu_stack || (left_index - 1 == dest_index || right_index - 1 == dest_index), "invalid LIR");
2287   assert(left_index == 0 || right_index == 0, "either must be on top of stack");
2288 
2289   bool left_is_tos = (left_index == 0);
2290   bool dest_is_tos = (dest_index == 0);
2291   int non_tos_index = (left_is_tos ? right_index : left_index);
2292 
2293   switch (code) {
2294     case lir_add:
2295       if (pop_fpu_stack)       __ faddp(non_tos_index);
2296       else if (dest_is_tos)    __ fadd (non_tos_index);
2297       else                     __ fadda(non_tos_index);
2298       break;
2299 
2300     case lir_sub:
2301       if (left_is_tos) {
2302         if (pop_fpu_stack)     __ fsubrp(non_tos_index);
2303         else if (dest_is_tos)  __ fsub  (non_tos_index);
2304         else                   __ fsubra(non_tos_index);
2305       } else {
2306         if (pop_fpu_stack)     __ fsubp (non_tos_index);
2307         else if (dest_is_tos)  __ fsubr (non_tos_index);
2308         else                   __ fsuba (non_tos_index);
2309       }
2310       break;
2311 
2312     case lir_mul_strictfp: // fall through
2313     case lir_mul:
2314       if (pop_fpu_stack)       __ fmulp(non_tos_index);
2315       else if (dest_is_tos)    __ fmul (non_tos_index);
2316       else                     __ fmula(non_tos_index);
2317       break;
2318 
2319     case lir_div_strictfp: // fall through
2320     case lir_div:
2321       if (left_is_tos) {
2322         if (pop_fpu_stack)     __ fdivrp(non_tos_index);
2323         else if (dest_is_tos)  __ fdiv  (non_tos_index);
2324         else                   __ fdivra(non_tos_index);
2325       } else {
2326         if (pop_fpu_stack)     __ fdivp (non_tos_index);
2327         else if (dest_is_tos)  __ fdivr (non_tos_index);
2328         else                   __ fdiva (non_tos_index);
2329       }
2330       break;
2331 
2332     case lir_rem:
2333       assert(left_is_tos && dest_is_tos && right_index == 1, "must be guaranteed by FPU stack allocation");
2334       __ fremr(noreg);
2335       break;
2336 
2337     default:
2338       ShouldNotReachHere();
2339   }
2340 }
2341 
2342 
2343 void LIR_Assembler::intrinsic_op(LIR_Code code, LIR_Opr value, LIR_Opr unused, LIR_Opr dest, LIR_Op* op) {
2344   if (value->is_double_xmm()) {
2345     switch(code) {
2346       case lir_abs :
2347         {
2348           if (dest->as_xmm_double_reg() != value->as_xmm_double_reg()) {
2349             __ movdbl(dest->as_xmm_double_reg(), value->as_xmm_double_reg());
2350           }
2351           __ andpd(dest->as_xmm_double_reg(),
2352                     ExternalAddress((address)double_signmask_pool));
2353         }
2354         break;
2355 
2356       case lir_sqrt: __ sqrtsd(dest->as_xmm_double_reg(), value->as_xmm_double_reg()); break;
2357       // all other intrinsics are not available in the SSE instruction set, so FPU is used
2358       default      : ShouldNotReachHere();
2359     }
2360 
2361   } else if (value->is_double_fpu()) {
2362     assert(value->fpu_regnrLo() == 0 && dest->fpu_regnrLo() == 0, "both must be on TOS");
2363     switch(code) {
2364       case lir_log10 : __ flog10() ; break;
2365       case lir_abs   : __ fabs() ; break;
2366       case lir_sqrt  : __ fsqrt(); break;
2367       case lir_tan :
2368         // Should consider not saving rbx, if not necessary
2369         __ trigfunc('t', op->as_Op2()->fpu_stack_size());
2370         break;
2371       case lir_pow :
2372         __ pow_with_fallback(op->as_Op2()->fpu_stack_size());
2373         break;
2374       default      : ShouldNotReachHere();
2375     }
2376   } else {
2377     Unimplemented();
2378   }
2379 }
2380 
2381 void LIR_Assembler::logic_op(LIR_Code code, LIR_Opr left, LIR_Opr right, LIR_Opr dst) {
2382   // assert(left->destroys_register(), "check");
2383   if (left->is_single_cpu()) {
2384     Register reg = left->as_register();
2385     if (right->is_constant()) {
2386       int val = right->as_constant_ptr()->as_jint();
2387       switch (code) {
2388         case lir_logic_and: __ andl (reg, val); break;
2389         case lir_logic_or:  __ orl  (reg, val); break;
2390         case lir_logic_xor: __ xorl (reg, val); break;
2391         default: ShouldNotReachHere();
2392       }
2393     } else if (right->is_stack()) {
2394       // added support for stack operands
2395       Address raddr = frame_map()->address_for_slot(right->single_stack_ix());
2396       switch (code) {
2397         case lir_logic_and: __ andl (reg, raddr); break;
2398         case lir_logic_or:  __ orl  (reg, raddr); break;
2399         case lir_logic_xor: __ xorl (reg, raddr); break;
2400         default: ShouldNotReachHere();
2401       }
2402     } else {
2403       Register rright = right->as_register();
2404       switch (code) {
2405         case lir_logic_and: __ andptr (reg, rright); break;
2406         case lir_logic_or : __ orptr  (reg, rright); break;
2407         case lir_logic_xor: __ xorptr (reg, rright); break;
2408         default: ShouldNotReachHere();
2409       }
2410     }
2411     move_regs(reg, dst->as_register());
2412   } else {
2413     Register l_lo = left->as_register_lo();
2414     Register l_hi = left->as_register_hi();
2415     if (right->is_constant()) {
2416 #ifdef _LP64
2417       __ mov64(rscratch1, right->as_constant_ptr()->as_jlong());
2418       switch (code) {
2419         case lir_logic_and:
2420           __ andq(l_lo, rscratch1);
2421           break;
2422         case lir_logic_or:
2423           __ orq(l_lo, rscratch1);
2424           break;
2425         case lir_logic_xor:
2426           __ xorq(l_lo, rscratch1);
2427           break;
2428         default: ShouldNotReachHere();
2429       }
2430 #else
2431       int r_lo = right->as_constant_ptr()->as_jint_lo();
2432       int r_hi = right->as_constant_ptr()->as_jint_hi();
2433       switch (code) {
2434         case lir_logic_and:
2435           __ andl(l_lo, r_lo);
2436           __ andl(l_hi, r_hi);
2437           break;
2438         case lir_logic_or:
2439           __ orl(l_lo, r_lo);
2440           __ orl(l_hi, r_hi);
2441           break;
2442         case lir_logic_xor:
2443           __ xorl(l_lo, r_lo);
2444           __ xorl(l_hi, r_hi);
2445           break;
2446         default: ShouldNotReachHere();
2447       }
2448 #endif // _LP64
2449     } else {
2450 #ifdef _LP64
2451       Register r_lo;
2452       if (right->type() == T_OBJECT || right->type() == T_ARRAY) {
2453         r_lo = right->as_register();
2454       } else {
2455         r_lo = right->as_register_lo();
2456       }
2457 #else
2458       Register r_lo = right->as_register_lo();
2459       Register r_hi = right->as_register_hi();
2460       assert(l_lo != r_hi, "overwriting registers");
2461 #endif
2462       switch (code) {
2463         case lir_logic_and:
2464           __ andptr(l_lo, r_lo);
2465           NOT_LP64(__ andptr(l_hi, r_hi);)
2466           break;
2467         case lir_logic_or:
2468           __ orptr(l_lo, r_lo);
2469           NOT_LP64(__ orptr(l_hi, r_hi);)
2470           break;
2471         case lir_logic_xor:
2472           __ xorptr(l_lo, r_lo);
2473           NOT_LP64(__ xorptr(l_hi, r_hi);)
2474           break;
2475         default: ShouldNotReachHere();
2476       }
2477     }
2478 
2479     Register dst_lo = dst->as_register_lo();
2480     Register dst_hi = dst->as_register_hi();
2481 
2482 #ifdef _LP64
2483     move_regs(l_lo, dst_lo);
2484 #else
2485     if (dst_lo == l_hi) {
2486       assert(dst_hi != l_lo, "overwriting registers");
2487       move_regs(l_hi, dst_hi);
2488       move_regs(l_lo, dst_lo);
2489     } else {
2490       assert(dst_lo != l_hi, "overwriting registers");
2491       move_regs(l_lo, dst_lo);
2492       move_regs(l_hi, dst_hi);
2493     }
2494 #endif // _LP64
2495   }
2496 }
2497 
2498 
2499 // we assume that rax, and rdx can be overwritten
2500 void LIR_Assembler::arithmetic_idiv(LIR_Code code, LIR_Opr left, LIR_Opr right, LIR_Opr temp, LIR_Opr result, CodeEmitInfo* info) {
2501 
2502   assert(left->is_single_cpu(),   "left must be register");
2503   assert(right->is_single_cpu() || right->is_constant(),  "right must be register or constant");
2504   assert(result->is_single_cpu(), "result must be register");
2505 
2506   //  assert(left->destroys_register(), "check");
2507   //  assert(right->destroys_register(), "check");
2508 
2509   Register lreg = left->as_register();
2510   Register dreg = result->as_register();
2511 
2512   if (right->is_constant()) {
2513     int divisor = right->as_constant_ptr()->as_jint();
2514     assert(divisor > 0 && is_power_of_2(divisor), "must be");
2515     if (code == lir_idiv) {
2516       assert(lreg == rax, "must be rax,");
2517       assert(temp->as_register() == rdx, "tmp register must be rdx");
2518       __ cdql(); // sign extend into rdx:rax
2519       if (divisor == 2) {
2520         __ subl(lreg, rdx);
2521       } else {
2522         __ andl(rdx, divisor - 1);
2523         __ addl(lreg, rdx);
2524       }
2525       __ sarl(lreg, log2_intptr(divisor));
2526       move_regs(lreg, dreg);
2527     } else if (code == lir_irem) {
2528       Label done;
2529       __ mov(dreg, lreg);
2530       __ andl(dreg, 0x80000000 | (divisor - 1));
2531       __ jcc(Assembler::positive, done);
2532       __ decrement(dreg);
2533       __ orl(dreg, ~(divisor - 1));
2534       __ increment(dreg);
2535       __ bind(done);
2536     } else {
2537       ShouldNotReachHere();
2538     }
2539   } else {
2540     Register rreg = right->as_register();
2541     assert(lreg == rax, "left register must be rax,");
2542     assert(rreg != rdx, "right register must not be rdx");
2543     assert(temp->as_register() == rdx, "tmp register must be rdx");
2544 
2545     move_regs(lreg, rax);
2546 
2547     int idivl_offset = __ corrected_idivl(rreg);
2548     add_debug_info_for_div0(idivl_offset, info);
2549     if (code == lir_irem) {
2550       move_regs(rdx, dreg); // result is in rdx
2551     } else {
2552       move_regs(rax, dreg);
2553     }
2554   }
2555 }
2556 
2557 
2558 void LIR_Assembler::comp_op(LIR_Condition condition, LIR_Opr opr1, LIR_Opr opr2, LIR_Op2* op) {
2559   if (opr1->is_single_cpu()) {
2560     Register reg1 = opr1->as_register();
2561     if (opr2->is_single_cpu()) {
2562       // cpu register - cpu register
2563       if (opr1->type() == T_OBJECT || opr1->type() == T_ARRAY) {
2564         __ cmpptr(reg1, opr2->as_register());
2565       } else {
2566         assert(opr2->type() != T_OBJECT && opr2->type() != T_ARRAY, "cmp int, oop?");
2567         __ cmpl(reg1, opr2->as_register());
2568       }
2569     } else if (opr2->is_stack()) {
2570       // cpu register - stack
2571       if (opr1->type() == T_OBJECT || opr1->type() == T_ARRAY) {
2572         __ cmpptr(reg1, frame_map()->address_for_slot(opr2->single_stack_ix()));
2573       } else {
2574         __ cmpl(reg1, frame_map()->address_for_slot(opr2->single_stack_ix()));
2575       }
2576     } else if (opr2->is_constant()) {
2577       // cpu register - constant
2578       LIR_Const* c = opr2->as_constant_ptr();
2579       if (c->type() == T_INT) {
2580         __ cmpl(reg1, c->as_jint());
2581       } else if (c->type() == T_OBJECT || c->type() == T_ARRAY) {
2582         // In 64bit oops are single register
2583         jobject o = c->as_jobject();
2584         if (o == NULL) {
2585           __ cmpptr(reg1, (int32_t)NULL_WORD);
2586         } else {
2587 #ifdef _LP64
2588           __ movoop(rscratch1, o);
2589           __ cmpptr(reg1, rscratch1);
2590 #else
2591           __ cmpoop(reg1, c->as_jobject());
2592 #endif // _LP64
2593         }
2594       } else {
2595         fatal("unexpected type: %s", basictype_to_str(c->type()));
2596       }
2597       // cpu register - address
2598     } else if (opr2->is_address()) {
2599       if (op->info() != NULL) {
2600         add_debug_info_for_null_check_here(op->info());
2601       }
2602       __ cmpl(reg1, as_Address(opr2->as_address_ptr()));
2603     } else {
2604       ShouldNotReachHere();
2605     }
2606 
2607   } else if(opr1->is_double_cpu()) {
2608     Register xlo = opr1->as_register_lo();
2609     Register xhi = opr1->as_register_hi();
2610     if (opr2->is_double_cpu()) {
2611 #ifdef _LP64
2612       __ cmpptr(xlo, opr2->as_register_lo());
2613 #else
2614       // cpu register - cpu register
2615       Register ylo = opr2->as_register_lo();
2616       Register yhi = opr2->as_register_hi();
2617       __ subl(xlo, ylo);
2618       __ sbbl(xhi, yhi);
2619       if (condition == lir_cond_equal || condition == lir_cond_notEqual) {
2620         __ orl(xhi, xlo);
2621       }
2622 #endif // _LP64
2623     } else if (opr2->is_constant()) {
2624       // cpu register - constant 0
2625       assert(opr2->as_jlong() == (jlong)0, "only handles zero");
2626 #ifdef _LP64
2627       __ cmpptr(xlo, (int32_t)opr2->as_jlong());
2628 #else
2629       assert(condition == lir_cond_equal || condition == lir_cond_notEqual, "only handles equals case");
2630       __ orl(xhi, xlo);
2631 #endif // _LP64
2632     } else {
2633       ShouldNotReachHere();
2634     }
2635 
2636   } else if (opr1->is_single_xmm()) {
2637     XMMRegister reg1 = opr1->as_xmm_float_reg();
2638     if (opr2->is_single_xmm()) {
2639       // xmm register - xmm register
2640       __ ucomiss(reg1, opr2->as_xmm_float_reg());
2641     } else if (opr2->is_stack()) {
2642       // xmm register - stack
2643       __ ucomiss(reg1, frame_map()->address_for_slot(opr2->single_stack_ix()));
2644     } else if (opr2->is_constant()) {
2645       // xmm register - constant
2646       __ ucomiss(reg1, InternalAddress(float_constant(opr2->as_jfloat())));
2647     } else if (opr2->is_address()) {
2648       // xmm register - address
2649       if (op->info() != NULL) {
2650         add_debug_info_for_null_check_here(op->info());
2651       }
2652       __ ucomiss(reg1, as_Address(opr2->as_address_ptr()));
2653     } else {
2654       ShouldNotReachHere();
2655     }
2656 
2657   } else if (opr1->is_double_xmm()) {
2658     XMMRegister reg1 = opr1->as_xmm_double_reg();
2659     if (opr2->is_double_xmm()) {
2660       // xmm register - xmm register
2661       __ ucomisd(reg1, opr2->as_xmm_double_reg());
2662     } else if (opr2->is_stack()) {
2663       // xmm register - stack
2664       __ ucomisd(reg1, frame_map()->address_for_slot(opr2->double_stack_ix()));
2665     } else if (opr2->is_constant()) {
2666       // xmm register - constant
2667       __ ucomisd(reg1, InternalAddress(double_constant(opr2->as_jdouble())));
2668     } else if (opr2->is_address()) {
2669       // xmm register - address
2670       if (op->info() != NULL) {
2671         add_debug_info_for_null_check_here(op->info());
2672       }
2673       __ ucomisd(reg1, as_Address(opr2->pointer()->as_address()));
2674     } else {
2675       ShouldNotReachHere();
2676     }
2677 
2678   } else if(opr1->is_single_fpu() || opr1->is_double_fpu()) {
2679     assert(opr1->is_fpu_register() && opr1->fpu() == 0, "currently left-hand side must be on TOS (relax this restriction)");
2680     assert(opr2->is_fpu_register(), "both must be registers");
2681     __ fcmp(noreg, opr2->fpu(), op->fpu_pop_count() > 0, op->fpu_pop_count() > 1);
2682 
2683   } else if (opr1->is_address() && opr2->is_constant()) {
2684     LIR_Const* c = opr2->as_constant_ptr();
2685 #ifdef _LP64
2686     if (c->type() == T_OBJECT || c->type() == T_ARRAY) {
2687       assert(condition == lir_cond_equal || condition == lir_cond_notEqual, "need to reverse");
2688       __ movoop(rscratch1, c->as_jobject());
2689     }
2690 #endif // LP64
2691     if (op->info() != NULL) {
2692       add_debug_info_for_null_check_here(op->info());
2693     }
2694     // special case: address - constant
2695     LIR_Address* addr = opr1->as_address_ptr();
2696     if (c->type() == T_INT) {
2697       __ cmpl(as_Address(addr), c->as_jint());
2698     } else if (c->type() == T_OBJECT || c->type() == T_ARRAY) {
2699 #ifdef _LP64
2700       // %%% Make this explode if addr isn't reachable until we figure out a
2701       // better strategy by giving noreg as the temp for as_Address
2702       __ cmpptr(rscratch1, as_Address(addr, noreg));
2703 #else
2704       __ cmpoop(as_Address(addr), c->as_jobject());
2705 #endif // _LP64
2706     } else {
2707       ShouldNotReachHere();
2708     }
2709 
2710   } else {
2711     ShouldNotReachHere();
2712   }
2713 }
2714 
2715 void LIR_Assembler::comp_fl2i(LIR_Code code, LIR_Opr left, LIR_Opr right, LIR_Opr dst, LIR_Op2* op) {
2716   if (code == lir_cmp_fd2i || code == lir_ucmp_fd2i) {
2717     if (left->is_single_xmm()) {
2718       assert(right->is_single_xmm(), "must match");
2719       __ cmpss2int(left->as_xmm_float_reg(), right->as_xmm_float_reg(), dst->as_register(), code == lir_ucmp_fd2i);
2720     } else if (left->is_double_xmm()) {
2721       assert(right->is_double_xmm(), "must match");
2722       __ cmpsd2int(left->as_xmm_double_reg(), right->as_xmm_double_reg(), dst->as_register(), code == lir_ucmp_fd2i);
2723 
2724     } else {
2725       assert(left->is_single_fpu() || left->is_double_fpu(), "must be");
2726       assert(right->is_single_fpu() || right->is_double_fpu(), "must match");
2727 
2728       assert(left->fpu() == 0, "left must be on TOS");
2729       __ fcmp2int(dst->as_register(), code == lir_ucmp_fd2i, right->fpu(),
2730                   op->fpu_pop_count() > 0, op->fpu_pop_count() > 1);
2731     }
2732   } else {
2733     assert(code == lir_cmp_l2i, "check");
2734 #ifdef _LP64
2735     Label done;
2736     Register dest = dst->as_register();
2737     __ cmpptr(left->as_register_lo(), right->as_register_lo());
2738     __ movl(dest, -1);
2739     __ jccb(Assembler::less, done);
2740     __ set_byte_if_not_zero(dest);
2741     __ movzbl(dest, dest);
2742     __ bind(done);
2743 #else
2744     __ lcmp2int(left->as_register_hi(),
2745                 left->as_register_lo(),
2746                 right->as_register_hi(),
2747                 right->as_register_lo());
2748     move_regs(left->as_register_hi(), dst->as_register());
2749 #endif // _LP64
2750   }
2751 }
2752 
2753 
2754 void LIR_Assembler::align_call(LIR_Code code) {
2755   if (os::is_MP()) {
2756     // make sure that the displacement word of the call ends up word aligned
2757     int offset = __ offset();
2758     switch (code) {
2759       case lir_static_call:
2760       case lir_optvirtual_call:
2761       case lir_dynamic_call:
2762         offset += NativeCall::displacement_offset;
2763         break;
2764       case lir_icvirtual_call:
2765         offset += NativeCall::displacement_offset + NativeMovConstReg::instruction_size;
2766       break;
2767       case lir_virtual_call:  // currently, sparc-specific for niagara
2768       default: ShouldNotReachHere();
2769     }
2770     __ align(BytesPerWord, offset);
2771   }
2772 }
2773 
2774 
2775 void LIR_Assembler::call(LIR_OpJavaCall* op, relocInfo::relocType rtype) {
2776   assert(!os::is_MP() || (__ offset() + NativeCall::displacement_offset) % BytesPerWord == 0,
2777          "must be aligned");
2778   __ call(AddressLiteral(op->addr(), rtype));
2779   add_call_info(code_offset(), op->info());
2780 }
2781 
2782 
2783 void LIR_Assembler::ic_call(LIR_OpJavaCall* op) {
2784   __ ic_call(op->addr());
2785   add_call_info(code_offset(), op->info());
2786   assert(!os::is_MP() ||
2787          (__ offset() - NativeCall::instruction_size + NativeCall::displacement_offset) % BytesPerWord == 0,
2788          "must be aligned");
2789 }
2790 
2791 
2792 /* Currently, vtable-dispatch is only enabled for sparc platforms */
2793 void LIR_Assembler::vtable_call(LIR_OpJavaCall* op) {
2794   ShouldNotReachHere();
2795 }
2796 
2797 
2798 void LIR_Assembler::emit_static_call_stub() {
2799   address call_pc = __ pc();
2800   address stub = __ start_a_stub(call_stub_size);
2801   if (stub == NULL) {
2802     bailout("static call stub overflow");
2803     return;
2804   }
2805 
2806   int start = __ offset();
2807   if (os::is_MP()) {
2808     // make sure that the displacement word of the call ends up word aligned
2809     __ align(BytesPerWord, __ offset() + NativeMovConstReg::instruction_size + NativeCall::displacement_offset);
2810   }
2811   __ relocate(static_stub_Relocation::spec(call_pc));
2812   __ mov_metadata(rbx, (Metadata*)NULL);
2813   // must be set to -1 at code generation time
2814   assert(!os::is_MP() || ((__ offset() + 1) % BytesPerWord) == 0, "must be aligned on MP");
2815   // On 64bit this will die since it will take a movq & jmp, must be only a jmp
2816   __ jump(RuntimeAddress(__ pc()));
2817 
2818   assert(__ offset() - start <= call_stub_size, "stub too big");
2819   __ end_a_stub();
2820 }
2821 
2822 
2823 void LIR_Assembler::throw_op(LIR_Opr exceptionPC, LIR_Opr exceptionOop, CodeEmitInfo* info) {
2824   assert(exceptionOop->as_register() == rax, "must match");
2825   assert(exceptionPC->as_register() == rdx, "must match");
2826 
2827   // exception object is not added to oop map by LinearScan
2828   // (LinearScan assumes that no oops are in fixed registers)
2829   info->add_register_oop(exceptionOop);
2830   Runtime1::StubID unwind_id;
2831 
2832   // get current pc information
2833   // pc is only needed if the method has an exception handler, the unwind code does not need it.
2834   int pc_for_athrow_offset = __ offset();
2835   InternalAddress pc_for_athrow(__ pc());
2836   __ lea(exceptionPC->as_register(), pc_for_athrow);
2837   add_call_info(pc_for_athrow_offset, info); // for exception handler
2838 
2839   __ verify_not_null_oop(rax);
2840   // search an exception handler (rax: exception oop, rdx: throwing pc)
2841   if (compilation()->has_fpu_code()) {
2842     unwind_id = Runtime1::handle_exception_id;
2843   } else {
2844     unwind_id = Runtime1::handle_exception_nofpu_id;
2845   }
2846   __ call(RuntimeAddress(Runtime1::entry_for(unwind_id)));
2847 
2848   // enough room for two byte trap
2849   __ nop();
2850 }
2851 
2852 
2853 void LIR_Assembler::unwind_op(LIR_Opr exceptionOop) {
2854   assert(exceptionOop->as_register() == rax, "must match");
2855 
2856   __ jmp(_unwind_handler_entry);
2857 }
2858 
2859 
2860 void LIR_Assembler::shift_op(LIR_Code code, LIR_Opr left, LIR_Opr count, LIR_Opr dest, LIR_Opr tmp) {
2861 
2862   // optimized version for linear scan:
2863   // * count must be already in ECX (guaranteed by LinearScan)
2864   // * left and dest must be equal
2865   // * tmp must be unused
2866   assert(count->as_register() == SHIFT_count, "count must be in ECX");
2867   assert(left == dest, "left and dest must be equal");
2868   assert(tmp->is_illegal(), "wasting a register if tmp is allocated");
2869 
2870   if (left->is_single_cpu()) {
2871     Register value = left->as_register();
2872     assert(value != SHIFT_count, "left cannot be ECX");
2873 
2874     switch (code) {
2875       case lir_shl:  __ shll(value); break;
2876       case lir_shr:  __ sarl(value); break;
2877       case lir_ushr: __ shrl(value); break;
2878       default: ShouldNotReachHere();
2879     }
2880   } else if (left->is_double_cpu()) {
2881     Register lo = left->as_register_lo();
2882     Register hi = left->as_register_hi();
2883     assert(lo != SHIFT_count && hi != SHIFT_count, "left cannot be ECX");
2884 #ifdef _LP64
2885     switch (code) {
2886       case lir_shl:  __ shlptr(lo);        break;
2887       case lir_shr:  __ sarptr(lo);        break;
2888       case lir_ushr: __ shrptr(lo);        break;
2889       default: ShouldNotReachHere();
2890     }
2891 #else
2892 
2893     switch (code) {
2894       case lir_shl:  __ lshl(hi, lo);        break;
2895       case lir_shr:  __ lshr(hi, lo, true);  break;
2896       case lir_ushr: __ lshr(hi, lo, false); break;
2897       default: ShouldNotReachHere();
2898     }
2899 #endif // LP64
2900   } else {
2901     ShouldNotReachHere();
2902   }
2903 }
2904 
2905 
2906 void LIR_Assembler::shift_op(LIR_Code code, LIR_Opr left, jint count, LIR_Opr dest) {
2907   if (dest->is_single_cpu()) {
2908     // first move left into dest so that left is not destroyed by the shift
2909     Register value = dest->as_register();
2910     count = count & 0x1F; // Java spec
2911 
2912     move_regs(left->as_register(), value);
2913     switch (code) {
2914       case lir_shl:  __ shll(value, count); break;
2915       case lir_shr:  __ sarl(value, count); break;
2916       case lir_ushr: __ shrl(value, count); break;
2917       default: ShouldNotReachHere();
2918     }
2919   } else if (dest->is_double_cpu()) {
2920 #ifndef _LP64
2921     Unimplemented();
2922 #else
2923     // first move left into dest so that left is not destroyed by the shift
2924     Register value = dest->as_register_lo();
2925     count = count & 0x1F; // Java spec
2926 
2927     move_regs(left->as_register_lo(), value);
2928     switch (code) {
2929       case lir_shl:  __ shlptr(value, count); break;
2930       case lir_shr:  __ sarptr(value, count); break;
2931       case lir_ushr: __ shrptr(value, count); break;
2932       default: ShouldNotReachHere();
2933     }
2934 #endif // _LP64
2935   } else {
2936     ShouldNotReachHere();
2937   }
2938 }
2939 
2940 
2941 void LIR_Assembler::store_parameter(Register r, int offset_from_rsp_in_words) {
2942   assert(offset_from_rsp_in_words >= 0, "invalid offset from rsp");
2943   int offset_from_rsp_in_bytes = offset_from_rsp_in_words * BytesPerWord;
2944   assert(offset_from_rsp_in_bytes < frame_map()->reserved_argument_area_size(), "invalid offset");
2945   __ movptr (Address(rsp, offset_from_rsp_in_bytes), r);
2946 }
2947 
2948 
2949 void LIR_Assembler::store_parameter(jint c,     int offset_from_rsp_in_words) {
2950   assert(offset_from_rsp_in_words >= 0, "invalid offset from rsp");
2951   int offset_from_rsp_in_bytes = offset_from_rsp_in_words * BytesPerWord;
2952   assert(offset_from_rsp_in_bytes < frame_map()->reserved_argument_area_size(), "invalid offset");
2953   __ movptr (Address(rsp, offset_from_rsp_in_bytes), c);
2954 }
2955 
2956 
2957 void LIR_Assembler::store_parameter(jobject o,  int offset_from_rsp_in_words) {
2958   assert(offset_from_rsp_in_words >= 0, "invalid offset from rsp");
2959   int offset_from_rsp_in_bytes = offset_from_rsp_in_words * BytesPerWord;
2960   assert(offset_from_rsp_in_bytes < frame_map()->reserved_argument_area_size(), "invalid offset");
2961   __ movoop (Address(rsp, offset_from_rsp_in_bytes), o);
2962 }
2963 
2964 
2965 // This code replaces a call to arraycopy; no exception may
2966 // be thrown in this code, they must be thrown in the System.arraycopy
2967 // activation frame; we could save some checks if this would not be the case
2968 void LIR_Assembler::emit_arraycopy(LIR_OpArrayCopy* op) {
2969   ciArrayKlass* default_type = op->expected_type();
2970   Register src = op->src()->as_register();
2971   Register dst = op->dst()->as_register();
2972   Register src_pos = op->src_pos()->as_register();
2973   Register dst_pos = op->dst_pos()->as_register();
2974   Register length  = op->length()->as_register();
2975   Register tmp = op->tmp()->as_register();
2976 
2977   CodeStub* stub = op->stub();
2978   int flags = op->flags();
2979   BasicType basic_type = default_type != NULL ? default_type->element_type()->basic_type() : T_ILLEGAL;
2980   if (basic_type == T_ARRAY) basic_type = T_OBJECT;
2981 
2982   // if we don't know anything, just go through the generic arraycopy
2983   if (default_type == NULL) {
2984     Label done;
2985     // save outgoing arguments on stack in case call to System.arraycopy is needed
2986     // HACK ALERT. This code used to push the parameters in a hardwired fashion
2987     // for interpreter calling conventions. Now we have to do it in new style conventions.
2988     // For the moment until C1 gets the new register allocator I just force all the
2989     // args to the right place (except the register args) and then on the back side
2990     // reload the register args properly if we go slow path. Yuck
2991 
2992     // These are proper for the calling convention
2993     store_parameter(length, 2);
2994     store_parameter(dst_pos, 1);
2995     store_parameter(dst, 0);
2996 
2997     // these are just temporary placements until we need to reload
2998     store_parameter(src_pos, 3);
2999     store_parameter(src, 4);
3000     NOT_LP64(assert(src == rcx && src_pos == rdx, "mismatch in calling convention");)
3001 
3002     address C_entry = CAST_FROM_FN_PTR(address, Runtime1::arraycopy);
3003 
3004     address copyfunc_addr = StubRoutines::generic_arraycopy();
3005 
3006     // pass arguments: may push as this is not a safepoint; SP must be fix at each safepoint
3007 #ifdef _LP64
3008     // The arguments are in java calling convention so we can trivially shift them to C
3009     // convention
3010     assert_different_registers(c_rarg0, j_rarg1, j_rarg2, j_rarg3, j_rarg4);
3011     __ mov(c_rarg0, j_rarg0);
3012     assert_different_registers(c_rarg1, j_rarg2, j_rarg3, j_rarg4);
3013     __ mov(c_rarg1, j_rarg1);
3014     assert_different_registers(c_rarg2, j_rarg3, j_rarg4);
3015     __ mov(c_rarg2, j_rarg2);
3016     assert_different_registers(c_rarg3, j_rarg4);
3017     __ mov(c_rarg3, j_rarg3);
3018 #ifdef _WIN64
3019     // Allocate abi space for args but be sure to keep stack aligned
3020     __ subptr(rsp, 6*wordSize);
3021     store_parameter(j_rarg4, 4);
3022     if (copyfunc_addr == NULL) { // Use C version if stub was not generated
3023       __ call(RuntimeAddress(C_entry));
3024     } else {
3025 #ifndef PRODUCT
3026       if (PrintC1Statistics) {
3027         __ incrementl(ExternalAddress((address)&Runtime1::_generic_arraycopystub_cnt));
3028       }
3029 #endif
3030       __ call(RuntimeAddress(copyfunc_addr));
3031     }
3032     __ addptr(rsp, 6*wordSize);
3033 #else
3034     __ mov(c_rarg4, j_rarg4);
3035     if (copyfunc_addr == NULL) { // Use C version if stub was not generated
3036       __ call(RuntimeAddress(C_entry));
3037     } else {
3038 #ifndef PRODUCT
3039       if (PrintC1Statistics) {
3040         __ incrementl(ExternalAddress((address)&Runtime1::_generic_arraycopystub_cnt));
3041       }
3042 #endif
3043       __ call(RuntimeAddress(copyfunc_addr));
3044     }
3045 #endif // _WIN64
3046 #else
3047     __ push(length);
3048     __ push(dst_pos);
3049     __ push(dst);
3050     __ push(src_pos);
3051     __ push(src);
3052 
3053     if (copyfunc_addr == NULL) { // Use C version if stub was not generated
3054       __ call_VM_leaf(C_entry, 5); // removes pushed parameter from the stack
3055     } else {
3056 #ifndef PRODUCT
3057       if (PrintC1Statistics) {
3058         __ incrementl(ExternalAddress((address)&Runtime1::_generic_arraycopystub_cnt));
3059       }
3060 #endif
3061       __ call_VM_leaf(copyfunc_addr, 5); // removes pushed parameter from the stack
3062     }
3063 
3064 #endif // _LP64
3065 
3066     __ cmpl(rax, 0);
3067     __ jcc(Assembler::equal, *stub->continuation());
3068 
3069     if (copyfunc_addr != NULL) {
3070       __ mov(tmp, rax);
3071       __ xorl(tmp, -1);
3072     }
3073 
3074     // Reload values from the stack so they are where the stub
3075     // expects them.
3076     __ movptr   (dst,     Address(rsp, 0*BytesPerWord));
3077     __ movptr   (dst_pos, Address(rsp, 1*BytesPerWord));
3078     __ movptr   (length,  Address(rsp, 2*BytesPerWord));
3079     __ movptr   (src_pos, Address(rsp, 3*BytesPerWord));
3080     __ movptr   (src,     Address(rsp, 4*BytesPerWord));
3081 
3082     if (copyfunc_addr != NULL) {
3083       __ subl(length, tmp);
3084       __ addl(src_pos, tmp);
3085       __ addl(dst_pos, tmp);
3086     }
3087     __ jmp(*stub->entry());
3088 
3089     __ bind(*stub->continuation());
3090     return;
3091   }
3092 
3093   assert(default_type != NULL && default_type->is_array_klass() && default_type->is_loaded(), "must be true at this point");
3094 
3095   int elem_size = type2aelembytes(basic_type);
3096   Address::ScaleFactor scale;
3097 
3098   switch (elem_size) {
3099     case 1 :
3100       scale = Address::times_1;
3101       break;
3102     case 2 :
3103       scale = Address::times_2;
3104       break;
3105     case 4 :
3106       scale = Address::times_4;
3107       break;
3108     case 8 :
3109       scale = Address::times_8;
3110       break;
3111     default:
3112       scale = Address::no_scale;
3113       ShouldNotReachHere();
3114   }
3115 
3116   Address src_length_addr = Address(src, arrayOopDesc::length_offset_in_bytes());
3117   Address dst_length_addr = Address(dst, arrayOopDesc::length_offset_in_bytes());
3118   Address src_klass_addr = Address(src, oopDesc::klass_offset_in_bytes());
3119   Address dst_klass_addr = Address(dst, oopDesc::klass_offset_in_bytes());
3120 
3121   // length and pos's are all sign extended at this point on 64bit
3122 
3123   // test for NULL
3124   if (flags & LIR_OpArrayCopy::src_null_check) {
3125     __ testptr(src, src);
3126     __ jcc(Assembler::zero, *stub->entry());
3127   }
3128   if (flags & LIR_OpArrayCopy::dst_null_check) {
3129     __ testptr(dst, dst);
3130     __ jcc(Assembler::zero, *stub->entry());
3131   }
3132 
3133   // check if negative
3134   if (flags & LIR_OpArrayCopy::src_pos_positive_check) {
3135     __ testl(src_pos, src_pos);
3136     __ jcc(Assembler::less, *stub->entry());
3137   }
3138   if (flags & LIR_OpArrayCopy::dst_pos_positive_check) {
3139     __ testl(dst_pos, dst_pos);
3140     __ jcc(Assembler::less, *stub->entry());
3141   }
3142 
3143   if (flags & LIR_OpArrayCopy::src_range_check) {
3144     __ lea(tmp, Address(src_pos, length, Address::times_1, 0));
3145     __ cmpl(tmp, src_length_addr);
3146     __ jcc(Assembler::above, *stub->entry());
3147   }
3148   if (flags & LIR_OpArrayCopy::dst_range_check) {
3149     __ lea(tmp, Address(dst_pos, length, Address::times_1, 0));
3150     __ cmpl(tmp, dst_length_addr);
3151     __ jcc(Assembler::above, *stub->entry());
3152   }
3153 
3154   if (flags & LIR_OpArrayCopy::length_positive_check) {
3155     __ testl(length, length);
3156     __ jcc(Assembler::less, *stub->entry());
3157     __ jcc(Assembler::zero, *stub->continuation());
3158   }
3159 
3160 #ifdef _LP64
3161   __ movl2ptr(src_pos, src_pos); //higher 32bits must be null
3162   __ movl2ptr(dst_pos, dst_pos); //higher 32bits must be null
3163 #endif
3164 
3165   if (flags & LIR_OpArrayCopy::type_check) {
3166     // We don't know the array types are compatible
3167     if (basic_type != T_OBJECT) {
3168       // Simple test for basic type arrays
3169       if (UseCompressedClassPointers) {
3170         __ movl(tmp, src_klass_addr);
3171         __ cmpl(tmp, dst_klass_addr);
3172       } else {
3173         __ movptr(tmp, src_klass_addr);
3174         __ cmpptr(tmp, dst_klass_addr);
3175       }
3176       __ jcc(Assembler::notEqual, *stub->entry());
3177     } else {
3178       // For object arrays, if src is a sub class of dst then we can
3179       // safely do the copy.
3180       Label cont, slow;
3181 
3182       __ push(src);
3183       __ push(dst);
3184 
3185       __ load_klass(src, src);
3186       __ load_klass(dst, dst);
3187 
3188       __ check_klass_subtype_fast_path(src, dst, tmp, &cont, &slow, NULL);
3189 
3190       __ push(src);
3191       __ push(dst);
3192       __ call(RuntimeAddress(Runtime1::entry_for(Runtime1::slow_subtype_check_id)));
3193       __ pop(dst);
3194       __ pop(src);
3195 
3196       __ cmpl(src, 0);
3197       __ jcc(Assembler::notEqual, cont);
3198 
3199       __ bind(slow);
3200       __ pop(dst);
3201       __ pop(src);
3202 
3203       address copyfunc_addr = StubRoutines::checkcast_arraycopy();
3204       if (copyfunc_addr != NULL) { // use stub if available
3205         // src is not a sub class of dst so we have to do a
3206         // per-element check.
3207 
3208         int mask = LIR_OpArrayCopy::src_objarray|LIR_OpArrayCopy::dst_objarray;
3209         if ((flags & mask) != mask) {
3210           // Check that at least both of them object arrays.
3211           assert(flags & mask, "one of the two should be known to be an object array");
3212 
3213           if (!(flags & LIR_OpArrayCopy::src_objarray)) {
3214             __ load_klass(tmp, src);
3215           } else if (!(flags & LIR_OpArrayCopy::dst_objarray)) {
3216             __ load_klass(tmp, dst);
3217           }
3218           int lh_offset = in_bytes(Klass::layout_helper_offset());
3219           Address klass_lh_addr(tmp, lh_offset);
3220           jint objArray_lh = Klass::array_layout_helper(T_OBJECT);
3221           __ cmpl(klass_lh_addr, objArray_lh);
3222           __ jcc(Assembler::notEqual, *stub->entry());
3223         }
3224 
3225        // Spill because stubs can use any register they like and it's
3226        // easier to restore just those that we care about.
3227        store_parameter(dst, 0);
3228        store_parameter(dst_pos, 1);
3229        store_parameter(length, 2);
3230        store_parameter(src_pos, 3);
3231        store_parameter(src, 4);
3232 
3233 #ifndef _LP64
3234         __ movptr(tmp, dst_klass_addr);
3235         __ movptr(tmp, Address(tmp, ObjArrayKlass::element_klass_offset()));
3236         __ push(tmp);
3237         __ movl(tmp, Address(tmp, Klass::super_check_offset_offset()));
3238         __ push(tmp);
3239         __ push(length);
3240         __ lea(tmp, Address(dst, dst_pos, scale, arrayOopDesc::base_offset_in_bytes(basic_type)));
3241         __ push(tmp);
3242         __ lea(tmp, Address(src, src_pos, scale, arrayOopDesc::base_offset_in_bytes(basic_type)));
3243         __ push(tmp);
3244 
3245         __ call_VM_leaf(copyfunc_addr, 5);
3246 #else
3247         __ movl2ptr(length, length); //higher 32bits must be null
3248 
3249         __ lea(c_rarg0, Address(src, src_pos, scale, arrayOopDesc::base_offset_in_bytes(basic_type)));
3250         assert_different_registers(c_rarg0, dst, dst_pos, length);
3251         __ lea(c_rarg1, Address(dst, dst_pos, scale, arrayOopDesc::base_offset_in_bytes(basic_type)));
3252         assert_different_registers(c_rarg1, dst, length);
3253 
3254         __ mov(c_rarg2, length);
3255         assert_different_registers(c_rarg2, dst);
3256 
3257 #ifdef _WIN64
3258         // Allocate abi space for args but be sure to keep stack aligned
3259         __ subptr(rsp, 6*wordSize);
3260         __ load_klass(c_rarg3, dst);
3261         __ movptr(c_rarg3, Address(c_rarg3, ObjArrayKlass::element_klass_offset()));
3262         store_parameter(c_rarg3, 4);
3263         __ movl(c_rarg3, Address(c_rarg3, Klass::super_check_offset_offset()));
3264         __ call(RuntimeAddress(copyfunc_addr));
3265         __ addptr(rsp, 6*wordSize);
3266 #else
3267         __ load_klass(c_rarg4, dst);
3268         __ movptr(c_rarg4, Address(c_rarg4, ObjArrayKlass::element_klass_offset()));
3269         __ movl(c_rarg3, Address(c_rarg4, Klass::super_check_offset_offset()));
3270         __ call(RuntimeAddress(copyfunc_addr));
3271 #endif
3272 
3273 #endif
3274 
3275 #ifndef PRODUCT
3276         if (PrintC1Statistics) {
3277           Label failed;
3278           __ testl(rax, rax);
3279           __ jcc(Assembler::notZero, failed);
3280           __ incrementl(ExternalAddress((address)&Runtime1::_arraycopy_checkcast_cnt));
3281           __ bind(failed);
3282         }
3283 #endif
3284 
3285         __ testl(rax, rax);
3286         __ jcc(Assembler::zero, *stub->continuation());
3287 
3288 #ifndef PRODUCT
3289         if (PrintC1Statistics) {
3290           __ incrementl(ExternalAddress((address)&Runtime1::_arraycopy_checkcast_attempt_cnt));
3291         }
3292 #endif
3293 
3294         __ mov(tmp, rax);
3295 
3296         __ xorl(tmp, -1);
3297 
3298         // Restore previously spilled arguments
3299         __ movptr   (dst,     Address(rsp, 0*BytesPerWord));
3300         __ movptr   (dst_pos, Address(rsp, 1*BytesPerWord));
3301         __ movptr   (length,  Address(rsp, 2*BytesPerWord));
3302         __ movptr   (src_pos, Address(rsp, 3*BytesPerWord));
3303         __ movptr   (src,     Address(rsp, 4*BytesPerWord));
3304 
3305 
3306         __ subl(length, tmp);
3307         __ addl(src_pos, tmp);
3308         __ addl(dst_pos, tmp);
3309       }
3310 
3311       __ jmp(*stub->entry());
3312 
3313       __ bind(cont);
3314       __ pop(dst);
3315       __ pop(src);
3316     }
3317   }
3318 
3319 #ifdef ASSERT
3320   if (basic_type != T_OBJECT || !(flags & LIR_OpArrayCopy::type_check)) {
3321     // Sanity check the known type with the incoming class.  For the
3322     // primitive case the types must match exactly with src.klass and
3323     // dst.klass each exactly matching the default type.  For the
3324     // object array case, if no type check is needed then either the
3325     // dst type is exactly the expected type and the src type is a
3326     // subtype which we can't check or src is the same array as dst
3327     // but not necessarily exactly of type default_type.
3328     Label known_ok, halt;
3329     __ mov_metadata(tmp, default_type->constant_encoding());
3330 #ifdef _LP64
3331     if (UseCompressedClassPointers) {
3332       __ encode_klass_not_null(tmp);
3333     }
3334 #endif
3335 
3336     if (basic_type != T_OBJECT) {
3337 
3338       if (UseCompressedClassPointers)          __ cmpl(tmp, dst_klass_addr);
3339       else                   __ cmpptr(tmp, dst_klass_addr);
3340       __ jcc(Assembler::notEqual, halt);
3341       if (UseCompressedClassPointers)          __ cmpl(tmp, src_klass_addr);
3342       else                   __ cmpptr(tmp, src_klass_addr);
3343       __ jcc(Assembler::equal, known_ok);
3344     } else {
3345       if (UseCompressedClassPointers)          __ cmpl(tmp, dst_klass_addr);
3346       else                   __ cmpptr(tmp, dst_klass_addr);
3347       __ jcc(Assembler::equal, known_ok);
3348       __ cmpptr(src, dst);
3349       __ jcc(Assembler::equal, known_ok);
3350     }
3351     __ bind(halt);
3352     __ stop("incorrect type information in arraycopy");
3353     __ bind(known_ok);
3354   }
3355 #endif
3356 
3357 #ifndef PRODUCT
3358   if (PrintC1Statistics) {
3359     __ incrementl(ExternalAddress(Runtime1::arraycopy_count_address(basic_type)));
3360   }
3361 #endif
3362 
3363 #ifdef _LP64
3364   assert_different_registers(c_rarg0, dst, dst_pos, length);
3365   __ lea(c_rarg0, Address(src, src_pos, scale, arrayOopDesc::base_offset_in_bytes(basic_type)));
3366   assert_different_registers(c_rarg1, length);
3367   __ lea(c_rarg1, Address(dst, dst_pos, scale, arrayOopDesc::base_offset_in_bytes(basic_type)));
3368   __ mov(c_rarg2, length);
3369 
3370 #else
3371   __ lea(tmp, Address(src, src_pos, scale, arrayOopDesc::base_offset_in_bytes(basic_type)));
3372   store_parameter(tmp, 0);
3373   __ lea(tmp, Address(dst, dst_pos, scale, arrayOopDesc::base_offset_in_bytes(basic_type)));
3374   store_parameter(tmp, 1);
3375   store_parameter(length, 2);
3376 #endif // _LP64
3377 
3378   bool disjoint = (flags & LIR_OpArrayCopy::overlapping) == 0;
3379   bool aligned = (flags & LIR_OpArrayCopy::unaligned) == 0;
3380   const char *name;
3381   address entry = StubRoutines::select_arraycopy_function(basic_type, aligned, disjoint, name, false);
3382   __ call_VM_leaf(entry, 0);
3383 
3384   __ bind(*stub->continuation());
3385 }
3386 
3387 void LIR_Assembler::emit_updatecrc32(LIR_OpUpdateCRC32* op) {
3388   assert(op->crc()->is_single_cpu(),  "crc must be register");
3389   assert(op->val()->is_single_cpu(),  "byte value must be register");
3390   assert(op->result_opr()->is_single_cpu(), "result must be register");
3391   Register crc = op->crc()->as_register();
3392   Register val = op->val()->as_register();
3393   Register res = op->result_opr()->as_register();
3394 
3395   assert_different_registers(val, crc, res);
3396 
3397   __ lea(res, ExternalAddress(StubRoutines::crc_table_addr()));
3398   __ notl(crc); // ~crc
3399   __ update_byte_crc32(crc, val, res);
3400   __ notl(crc); // ~crc
3401   __ mov(res, crc);
3402 }
3403 
3404 void LIR_Assembler::emit_lock(LIR_OpLock* op) {
3405   Register obj = op->obj_opr()->as_register();  // may not be an oop
3406   Register hdr = op->hdr_opr()->as_register();
3407   Register lock = op->lock_opr()->as_register();
3408   if (!UseFastLocking) {
3409     __ jmp(*op->stub()->entry());
3410   } else if (op->code() == lir_lock) {
3411     Register scratch = noreg;
3412     if (UseBiasedLocking) {
3413       scratch = op->scratch_opr()->as_register();
3414     }
3415     assert(BasicLock::displaced_header_offset_in_bytes() == 0, "lock_reg must point to the displaced header");
3416     // add debug info for NullPointerException only if one is possible
3417     int null_check_offset = __ lock_object(hdr, obj, lock, scratch, *op->stub()->entry());
3418     if (op->info() != NULL) {
3419       add_debug_info_for_null_check(null_check_offset, op->info());
3420     }
3421     // done
3422   } else if (op->code() == lir_unlock) {
3423     assert(BasicLock::displaced_header_offset_in_bytes() == 0, "lock_reg must point to the displaced header");
3424     __ unlock_object(hdr, obj, lock, *op->stub()->entry());
3425   } else {
3426     Unimplemented();
3427   }
3428   __ bind(*op->stub()->continuation());
3429 }
3430 
3431 
3432 void LIR_Assembler::emit_profile_call(LIR_OpProfileCall* op) {
3433   ciMethod* method = op->profiled_method();
3434   int bci          = op->profiled_bci();
3435   ciMethod* callee = op->profiled_callee();
3436 
3437   // Update counter for all call types
3438   ciMethodData* md = method->method_data_or_null();
3439   assert(md != NULL, "Sanity");
3440   ciProfileData* data = md->bci_to_data(bci);
3441   assert(data->is_CounterData(), "need CounterData for calls");
3442   assert(op->mdo()->is_single_cpu(),  "mdo must be allocated");
3443   Register mdo  = op->mdo()->as_register();
3444   __ mov_metadata(mdo, md->constant_encoding());
3445   Address counter_addr(mdo, md->byte_offset_of_slot(data, CounterData::count_offset()));
3446   Bytecodes::Code bc = method->java_code_at_bci(bci);
3447   const bool callee_is_static = callee->is_loaded() && callee->is_static();
3448   // Perform additional virtual call profiling for invokevirtual and
3449   // invokeinterface bytecodes
3450   if ((bc == Bytecodes::_invokevirtual || bc == Bytecodes::_invokeinterface) &&
3451       !callee_is_static &&  // required for optimized MH invokes
3452       C1ProfileVirtualCalls) {
3453     assert(op->recv()->is_single_cpu(), "recv must be allocated");
3454     Register recv = op->recv()->as_register();
3455     assert_different_registers(mdo, recv);
3456     assert(data->is_VirtualCallData(), "need VirtualCallData for virtual calls");
3457     ciKlass* known_klass = op->known_holder();
3458     if (C1OptimizeVirtualCallProfiling && known_klass != NULL) {
3459       // We know the type that will be seen at this call site; we can
3460       // statically update the MethodData* rather than needing to do
3461       // dynamic tests on the receiver type
3462 
3463       // NOTE: we should probably put a lock around this search to
3464       // avoid collisions by concurrent compilations
3465       ciVirtualCallData* vc_data = (ciVirtualCallData*) data;
3466       uint i;
3467       for (i = 0; i < VirtualCallData::row_limit(); i++) {
3468         ciKlass* receiver = vc_data->receiver(i);
3469         if (known_klass->equals(receiver)) {
3470           Address data_addr(mdo, md->byte_offset_of_slot(data, VirtualCallData::receiver_count_offset(i)));
3471           __ addptr(data_addr, DataLayout::counter_increment);
3472           return;
3473         }
3474       }
3475 
3476       // Receiver type not found in profile data; select an empty slot
3477 
3478       // Note that this is less efficient than it should be because it
3479       // always does a write to the receiver part of the
3480       // VirtualCallData rather than just the first time
3481       for (i = 0; i < VirtualCallData::row_limit(); i++) {
3482         ciKlass* receiver = vc_data->receiver(i);
3483         if (receiver == NULL) {
3484           Address recv_addr(mdo, md->byte_offset_of_slot(data, VirtualCallData::receiver_offset(i)));
3485           __ mov_metadata(recv_addr, known_klass->constant_encoding());
3486           Address data_addr(mdo, md->byte_offset_of_slot(data, VirtualCallData::receiver_count_offset(i)));
3487           __ addptr(data_addr, DataLayout::counter_increment);
3488           return;
3489         }
3490       }
3491     } else {
3492       __ load_klass(recv, recv);
3493       Label update_done;
3494       type_profile_helper(mdo, md, data, recv, &update_done);
3495       // Receiver did not match any saved receiver and there is no empty row for it.
3496       // Increment total counter to indicate polymorphic case.
3497       __ addptr(counter_addr, DataLayout::counter_increment);
3498 
3499       __ bind(update_done);
3500     }
3501   } else {
3502     // Static call
3503     __ addptr(counter_addr, DataLayout::counter_increment);
3504   }
3505 }
3506 
3507 void LIR_Assembler::emit_profile_type(LIR_OpProfileType* op) {
3508   Register obj = op->obj()->as_register();
3509   Register tmp = op->tmp()->as_pointer_register();
3510   Address mdo_addr = as_Address(op->mdp()->as_address_ptr());
3511   ciKlass* exact_klass = op->exact_klass();
3512   intptr_t current_klass = op->current_klass();
3513   bool not_null = op->not_null();
3514   bool no_conflict = op->no_conflict();
3515 
3516   Label update, next, none;
3517 
3518   bool do_null = !not_null;
3519   bool exact_klass_set = exact_klass != NULL && ciTypeEntries::valid_ciklass(current_klass) == exact_klass;
3520   bool do_update = !TypeEntries::is_type_unknown(current_klass) && !exact_klass_set;
3521 
3522   assert(do_null || do_update, "why are we here?");
3523   assert(!TypeEntries::was_null_seen(current_klass) || do_update, "why are we here?");
3524 
3525   __ verify_oop(obj);
3526 
3527   if (tmp != obj) {
3528     __ mov(tmp, obj);
3529   }
3530   if (do_null) {
3531     __ testptr(tmp, tmp);
3532     __ jccb(Assembler::notZero, update);
3533     if (!TypeEntries::was_null_seen(current_klass)) {
3534       __ orptr(mdo_addr, TypeEntries::null_seen);
3535     }
3536     if (do_update) {
3537 #ifndef ASSERT
3538       __ jmpb(next);
3539     }
3540 #else
3541       __ jmp(next);
3542     }
3543   } else {
3544     __ testptr(tmp, tmp);
3545     __ jccb(Assembler::notZero, update);
3546     __ stop("unexpect null obj");
3547 #endif
3548   }
3549 
3550   __ bind(update);
3551 
3552   if (do_update) {
3553 #ifdef ASSERT
3554     if (exact_klass != NULL) {
3555       Label ok;
3556       __ load_klass(tmp, tmp);
3557       __ push(tmp);
3558       __ mov_metadata(tmp, exact_klass->constant_encoding());
3559       __ cmpptr(tmp, Address(rsp, 0));
3560       __ jccb(Assembler::equal, ok);
3561       __ stop("exact klass and actual klass differ");
3562       __ bind(ok);
3563       __ pop(tmp);
3564     }
3565 #endif
3566     if (!no_conflict) {
3567       if (exact_klass == NULL || TypeEntries::is_type_none(current_klass)) {
3568         if (exact_klass != NULL) {
3569           __ mov_metadata(tmp, exact_klass->constant_encoding());
3570         } else {
3571           __ load_klass(tmp, tmp);
3572         }
3573 
3574         __ xorptr(tmp, mdo_addr);
3575         __ testptr(tmp, TypeEntries::type_klass_mask);
3576         // klass seen before, nothing to do. The unknown bit may have been
3577         // set already but no need to check.
3578         __ jccb(Assembler::zero, next);
3579 
3580         __ testptr(tmp, TypeEntries::type_unknown);
3581         __ jccb(Assembler::notZero, next); // already unknown. Nothing to do anymore.
3582 
3583         if (TypeEntries::is_type_none(current_klass)) {
3584           __ cmpptr(mdo_addr, 0);
3585           __ jccb(Assembler::equal, none);
3586           __ cmpptr(mdo_addr, TypeEntries::null_seen);
3587           __ jccb(Assembler::equal, none);
3588           // There is a chance that the checks above (re-reading profiling
3589           // data from memory) fail if another thread has just set the
3590           // profiling to this obj's klass
3591           __ xorptr(tmp, mdo_addr);
3592           __ testptr(tmp, TypeEntries::type_klass_mask);
3593           __ jccb(Assembler::zero, next);
3594         }
3595       } else {
3596         assert(ciTypeEntries::valid_ciklass(current_klass) != NULL &&
3597                ciTypeEntries::valid_ciklass(current_klass) != exact_klass, "conflict only");
3598 
3599         __ movptr(tmp, mdo_addr);
3600         __ testptr(tmp, TypeEntries::type_unknown);
3601         __ jccb(Assembler::notZero, next); // already unknown. Nothing to do anymore.
3602       }
3603 
3604       // different than before. Cannot keep accurate profile.
3605       __ orptr(mdo_addr, TypeEntries::type_unknown);
3606 
3607       if (TypeEntries::is_type_none(current_klass)) {
3608         __ jmpb(next);
3609 
3610         __ bind(none);
3611         // first time here. Set profile type.
3612         __ movptr(mdo_addr, tmp);
3613       }
3614     } else {
3615       // There's a single possible klass at this profile point
3616       assert(exact_klass != NULL, "should be");
3617       if (TypeEntries::is_type_none(current_klass)) {
3618         __ mov_metadata(tmp, exact_klass->constant_encoding());
3619         __ xorptr(tmp, mdo_addr);
3620         __ testptr(tmp, TypeEntries::type_klass_mask);
3621 #ifdef ASSERT
3622         __ jcc(Assembler::zero, next);
3623 
3624         {
3625           Label ok;
3626           __ push(tmp);
3627           __ cmpptr(mdo_addr, 0);
3628           __ jcc(Assembler::equal, ok);
3629           __ cmpptr(mdo_addr, TypeEntries::null_seen);
3630           __ jcc(Assembler::equal, ok);
3631           // may have been set by another thread
3632           __ mov_metadata(tmp, exact_klass->constant_encoding());
3633           __ xorptr(tmp, mdo_addr);
3634           __ testptr(tmp, TypeEntries::type_mask);
3635           __ jcc(Assembler::zero, ok);
3636 
3637           __ stop("unexpected profiling mismatch");
3638           __ bind(ok);
3639           __ pop(tmp);
3640         }
3641 #else
3642         __ jccb(Assembler::zero, next);
3643 #endif
3644         // first time here. Set profile type.
3645         __ movptr(mdo_addr, tmp);
3646       } else {
3647         assert(ciTypeEntries::valid_ciklass(current_klass) != NULL &&
3648                ciTypeEntries::valid_ciklass(current_klass) != exact_klass, "inconsistent");
3649 
3650         __ movptr(tmp, mdo_addr);
3651         __ testptr(tmp, TypeEntries::type_unknown);
3652         __ jccb(Assembler::notZero, next); // already unknown. Nothing to do anymore.
3653 
3654         __ orptr(mdo_addr, TypeEntries::type_unknown);
3655       }
3656     }
3657 
3658     __ bind(next);
3659   }
3660 }
3661 
3662 void LIR_Assembler::emit_delay(LIR_OpDelay*) {
3663   Unimplemented();
3664 }
3665 
3666 
3667 void LIR_Assembler::monitor_address(int monitor_no, LIR_Opr dst) {
3668   __ lea(dst->as_register(), frame_map()->address_for_monitor_lock(monitor_no));
3669 }
3670 
3671 
3672 void LIR_Assembler::align_backward_branch_target() {
3673   __ align(BytesPerWord);
3674 }
3675 
3676 
3677 void LIR_Assembler::negate(LIR_Opr left, LIR_Opr dest) {
3678   if (left->is_single_cpu()) {
3679     __ negl(left->as_register());
3680     move_regs(left->as_register(), dest->as_register());
3681 
3682   } else if (left->is_double_cpu()) {
3683     Register lo = left->as_register_lo();
3684 #ifdef _LP64
3685     Register dst = dest->as_register_lo();
3686     __ movptr(dst, lo);
3687     __ negptr(dst);
3688 #else
3689     Register hi = left->as_register_hi();
3690     __ lneg(hi, lo);
3691     if (dest->as_register_lo() == hi) {
3692       assert(dest->as_register_hi() != lo, "destroying register");
3693       move_regs(hi, dest->as_register_hi());
3694       move_regs(lo, dest->as_register_lo());
3695     } else {
3696       move_regs(lo, dest->as_register_lo());
3697       move_regs(hi, dest->as_register_hi());
3698     }
3699 #endif // _LP64
3700 
3701   } else if (dest->is_single_xmm()) {
3702     if (left->as_xmm_float_reg() != dest->as_xmm_float_reg()) {
3703       __ movflt(dest->as_xmm_float_reg(), left->as_xmm_float_reg());
3704     }
3705     if (UseAVX > 0) {
3706       __ vnegatess(dest->as_xmm_float_reg(), dest->as_xmm_float_reg(),
3707                    ExternalAddress((address)float_signflip_pool));
3708     } else {
3709       __ xorps(dest->as_xmm_float_reg(),
3710                ExternalAddress((address)float_signflip_pool));
3711     }
3712   } else if (dest->is_double_xmm()) {
3713     if (left->as_xmm_double_reg() != dest->as_xmm_double_reg()) {
3714       __ movdbl(dest->as_xmm_double_reg(), left->as_xmm_double_reg());
3715     }
3716     if (UseAVX > 0) {
3717       __ vnegatesd(dest->as_xmm_double_reg(), dest->as_xmm_double_reg(),
3718                    ExternalAddress((address)double_signflip_pool));
3719     } else {
3720       __ xorpd(dest->as_xmm_double_reg(),
3721                ExternalAddress((address)double_signflip_pool));
3722     }
3723   } else if (left->is_single_fpu() || left->is_double_fpu()) {
3724     assert(left->fpu() == 0, "arg must be on TOS");
3725     assert(dest->fpu() == 0, "dest must be TOS");
3726     __ fchs();
3727 
3728   } else {
3729     ShouldNotReachHere();
3730   }
3731 }
3732 
3733 
3734 void LIR_Assembler::leal(LIR_Opr addr, LIR_Opr dest) {
3735   assert(addr->is_address() && dest->is_register(), "check");
3736   Register reg;
3737   reg = dest->as_pointer_register();
3738   __ lea(reg, as_Address(addr->as_address_ptr()));
3739 }
3740 
3741 
3742 
3743 void LIR_Assembler::rt_call(LIR_Opr result, address dest, const LIR_OprList* args, LIR_Opr tmp, CodeEmitInfo* info) {
3744   assert(!tmp->is_valid(), "don't need temporary");
3745   __ call(RuntimeAddress(dest));
3746   if (info != NULL) {
3747     add_call_info_here(info);
3748   }
3749 }
3750 
3751 
3752 void LIR_Assembler::volatile_move_op(LIR_Opr src, LIR_Opr dest, BasicType type, CodeEmitInfo* info) {
3753   assert(type == T_LONG, "only for volatile long fields");
3754 
3755   if (info != NULL) {
3756     add_debug_info_for_null_check_here(info);
3757   }
3758 
3759   if (src->is_double_xmm()) {
3760     if (dest->is_double_cpu()) {
3761 #ifdef _LP64
3762       __ movdq(dest->as_register_lo(), src->as_xmm_double_reg());
3763 #else
3764       __ movdl(dest->as_register_lo(), src->as_xmm_double_reg());
3765       __ psrlq(src->as_xmm_double_reg(), 32);
3766       __ movdl(dest->as_register_hi(), src->as_xmm_double_reg());
3767 #endif // _LP64
3768     } else if (dest->is_double_stack()) {
3769       __ movdbl(frame_map()->address_for_slot(dest->double_stack_ix()), src->as_xmm_double_reg());
3770     } else if (dest->is_address()) {
3771       __ movdbl(as_Address(dest->as_address_ptr()), src->as_xmm_double_reg());
3772     } else {
3773       ShouldNotReachHere();
3774     }
3775 
3776   } else if (dest->is_double_xmm()) {
3777     if (src->is_double_stack()) {
3778       __ movdbl(dest->as_xmm_double_reg(), frame_map()->address_for_slot(src->double_stack_ix()));
3779     } else if (src->is_address()) {
3780       __ movdbl(dest->as_xmm_double_reg(), as_Address(src->as_address_ptr()));
3781     } else {
3782       ShouldNotReachHere();
3783     }
3784 
3785   } else if (src->is_double_fpu()) {
3786     assert(src->fpu_regnrLo() == 0, "must be TOS");
3787     if (dest->is_double_stack()) {
3788       __ fistp_d(frame_map()->address_for_slot(dest->double_stack_ix()));
3789     } else if (dest->is_address()) {
3790       __ fistp_d(as_Address(dest->as_address_ptr()));
3791     } else {
3792       ShouldNotReachHere();
3793     }
3794 
3795   } else if (dest->is_double_fpu()) {
3796     assert(dest->fpu_regnrLo() == 0, "must be TOS");
3797     if (src->is_double_stack()) {
3798       __ fild_d(frame_map()->address_for_slot(src->double_stack_ix()));
3799     } else if (src->is_address()) {
3800       __ fild_d(as_Address(src->as_address_ptr()));
3801     } else {
3802       ShouldNotReachHere();
3803     }
3804   } else {
3805     ShouldNotReachHere();
3806   }
3807 }
3808 
3809 #ifdef ASSERT
3810 // emit run-time assertion
3811 void LIR_Assembler::emit_assert(LIR_OpAssert* op) {
3812   assert(op->code() == lir_assert, "must be");
3813 
3814   if (op->in_opr1()->is_valid()) {
3815     assert(op->in_opr2()->is_valid(), "both operands must be valid");
3816     comp_op(op->condition(), op->in_opr1(), op->in_opr2(), op);
3817   } else {
3818     assert(op->in_opr2()->is_illegal(), "both operands must be illegal");
3819     assert(op->condition() == lir_cond_always, "no other conditions allowed");
3820   }
3821 
3822   Label ok;
3823   if (op->condition() != lir_cond_always) {
3824     Assembler::Condition acond = Assembler::zero;
3825     switch (op->condition()) {
3826       case lir_cond_equal:        acond = Assembler::equal;       break;
3827       case lir_cond_notEqual:     acond = Assembler::notEqual;    break;
3828       case lir_cond_less:         acond = Assembler::less;        break;
3829       case lir_cond_lessEqual:    acond = Assembler::lessEqual;   break;
3830       case lir_cond_greaterEqual: acond = Assembler::greaterEqual;break;
3831       case lir_cond_greater:      acond = Assembler::greater;     break;
3832       case lir_cond_belowEqual:   acond = Assembler::belowEqual;  break;
3833       case lir_cond_aboveEqual:   acond = Assembler::aboveEqual;  break;
3834       default:                    ShouldNotReachHere();
3835     }
3836     __ jcc(acond, ok);
3837   }
3838   if (op->halt()) {
3839     const char* str = __ code_string(op->msg());
3840     __ stop(str);
3841   } else {
3842     breakpoint();
3843   }
3844   __ bind(ok);
3845 }
3846 #endif
3847 
3848 void LIR_Assembler::membar() {
3849   // QQQ sparc TSO uses this,
3850   __ membar( Assembler::Membar_mask_bits(Assembler::StoreLoad));
3851 }
3852 
3853 void LIR_Assembler::membar_acquire() {
3854   // No x86 machines currently require load fences
3855 }
3856 
3857 void LIR_Assembler::membar_release() {
3858   // No x86 machines currently require store fences
3859 }
3860 
3861 void LIR_Assembler::membar_loadload() {
3862   // no-op
3863   //__ membar(Assembler::Membar_mask_bits(Assembler::loadload));
3864 }
3865 
3866 void LIR_Assembler::membar_storestore() {
3867   // no-op
3868   //__ membar(Assembler::Membar_mask_bits(Assembler::storestore));
3869 }
3870 
3871 void LIR_Assembler::membar_loadstore() {
3872   // no-op
3873   //__ membar(Assembler::Membar_mask_bits(Assembler::loadstore));
3874 }
3875 
3876 void LIR_Assembler::membar_storeload() {
3877   __ membar(Assembler::Membar_mask_bits(Assembler::StoreLoad));
3878 }
3879 
3880 void LIR_Assembler::get_thread(LIR_Opr result_reg) {
3881   assert(result_reg->is_register(), "check");
3882 #ifdef _LP64
3883   // __ get_thread(result_reg->as_register_lo());
3884   __ mov(result_reg->as_register(), r15_thread);
3885 #else
3886   __ get_thread(result_reg->as_register());
3887 #endif // _LP64
3888 }
3889 
3890 
3891 void LIR_Assembler::peephole(LIR_List*) {
3892   // do nothing for now
3893 }
3894 
3895 void LIR_Assembler::atomic_op(LIR_Code code, LIR_Opr src, LIR_Opr data, LIR_Opr dest, LIR_Opr tmp) {
3896   assert(data == dest, "xchg/xadd uses only 2 operands");
3897 
3898   if (data->type() == T_INT) {
3899     if (code == lir_xadd) {
3900       if (os::is_MP()) {
3901         __ lock();
3902       }
3903       __ xaddl(as_Address(src->as_address_ptr()), data->as_register());
3904     } else {
3905       __ xchgl(data->as_register(), as_Address(src->as_address_ptr()));
3906     }
3907   } else if (data->is_oop()) {
3908     assert (code == lir_xchg, "xadd for oops");
3909     Register obj = data->as_register();
3910 #ifdef _LP64
3911     if (UseCompressedOops) {
3912       __ encode_heap_oop(obj);
3913       __ xchgl(obj, as_Address(src->as_address_ptr()));
3914       __ decode_heap_oop(obj);
3915     } else {
3916       __ xchgptr(obj, as_Address(src->as_address_ptr()));
3917     }
3918 #else
3919     __ xchgl(obj, as_Address(src->as_address_ptr()));
3920 #endif
3921   } else if (data->type() == T_LONG) {
3922 #ifdef _LP64
3923     assert(data->as_register_lo() == data->as_register_hi(), "should be a single register");
3924     if (code == lir_xadd) {
3925       if (os::is_MP()) {
3926         __ lock();
3927       }
3928       __ xaddq(as_Address(src->as_address_ptr()), data->as_register_lo());
3929     } else {
3930       __ xchgq(data->as_register_lo(), as_Address(src->as_address_ptr()));
3931     }
3932 #else
3933     ShouldNotReachHere();
3934 #endif
3935   } else {
3936     ShouldNotReachHere();
3937   }
3938 }
3939 
3940 #undef __