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 "c1/c1_Compilation.hpp"
  27 #include "c1/c1_LIRAssembler.hpp"
  28 #include "c1/c1_MacroAssembler.hpp"
  29 #include "c1/c1_Runtime1.hpp"
  30 #include "c1/c1_ValueStack.hpp"
  31 #include "ci/ciArrayKlass.hpp"
  32 #include "ci/ciInstance.hpp"
  33 #include "gc/shared/barrierSet.hpp"
  34 #include "gc/shared/cardTableModRefBS.hpp"
  35 #include "gc/shared/collectedHeap.hpp"
  36 #include "nativeInst_sparc.hpp"
  37 #include "oops/objArrayKlass.hpp"
  38 #include "runtime/sharedRuntime.hpp"
  39 
  40 #define __ _masm->
  41 
  42 
  43 //------------------------------------------------------------
  44 
  45 
  46 bool LIR_Assembler::is_small_constant(LIR_Opr opr) {
  47   if (opr->is_constant()) {
  48     LIR_Const* constant = opr->as_constant_ptr();
  49     switch (constant->type()) {
  50       case T_INT: {
  51         jint value = constant->as_jint();
  52         return Assembler::is_simm13(value);
  53       }
  54 
  55       default:
  56         return false;
  57     }
  58   }
  59   return false;
  60 }
  61 
  62 
  63 bool LIR_Assembler::is_single_instruction(LIR_Op* op) {
  64   switch (op->code()) {
  65     case lir_null_check:
  66     return true;
  67 
  68 
  69     case lir_add:
  70     case lir_ushr:
  71     case lir_shr:
  72     case lir_shl:
  73       // integer shifts and adds are always one instruction
  74       return op->result_opr()->is_single_cpu();
  75 
  76 
  77     case lir_move: {
  78       LIR_Op1* op1 = op->as_Op1();
  79       LIR_Opr src = op1->in_opr();
  80       LIR_Opr dst = op1->result_opr();
  81 
  82       if (src == dst) {
  83         NEEDS_CLEANUP;
  84         // this works around a problem where moves with the same src and dst
  85         // end up in the delay slot and then the assembler swallows the mov
  86         // since it has no effect and then it complains because the delay slot
  87         // is empty.  returning false stops the optimizer from putting this in
  88         // the delay slot
  89         return false;
  90       }
  91 
  92       // don't put moves involving oops into the delay slot since the VerifyOops code
  93       // will make it much larger than a single instruction.
  94       if (VerifyOops) {
  95         return false;
  96       }
  97 
  98       if (src->is_double_cpu() || dst->is_double_cpu() || op1->patch_code() != lir_patch_none ||
  99           ((src->is_double_fpu() || dst->is_double_fpu()) && op1->move_kind() != lir_move_normal)) {
 100         return false;
 101       }
 102 
 103       if (UseCompressedOops) {
 104         if (dst->is_address() && !dst->is_stack() && (dst->type() == T_OBJECT || dst->type() == T_ARRAY)) return false;
 105         if (src->is_address() && !src->is_stack() && (src->type() == T_OBJECT || src->type() == T_ARRAY)) return false;
 106       }
 107 
 108       if (UseCompressedClassPointers) {
 109         if (src->is_address() && !src->is_stack() && src->type() == T_ADDRESS &&
 110             src->as_address_ptr()->disp() == oopDesc::klass_offset_in_bytes()) return false;
 111       }
 112 
 113       if (dst->is_register()) {
 114         if (src->is_address() && Assembler::is_simm13(src->as_address_ptr()->disp())) {
 115           return !PatchALot;
 116         } else if (src->is_single_stack()) {
 117           return true;
 118         }
 119       }
 120 
 121       if (src->is_register()) {
 122         if (dst->is_address() && Assembler::is_simm13(dst->as_address_ptr()->disp())) {
 123           return !PatchALot;
 124         } else if (dst->is_single_stack()) {
 125           return true;
 126         }
 127       }
 128 
 129       if (dst->is_register() &&
 130           ((src->is_register() && src->is_single_word() && src->is_same_type(dst)) ||
 131            (src->is_constant() && LIR_Assembler::is_small_constant(op->as_Op1()->in_opr())))) {
 132         return true;
 133       }
 134 
 135       return false;
 136     }
 137 
 138     default:
 139       return false;
 140   }
 141   ShouldNotReachHere();
 142 }
 143 
 144 
 145 LIR_Opr LIR_Assembler::receiverOpr() {
 146   return FrameMap::O0_oop_opr;
 147 }
 148 
 149 
 150 LIR_Opr LIR_Assembler::osrBufferPointer() {
 151   return FrameMap::I0_opr;
 152 }
 153 
 154 
 155 int LIR_Assembler::initial_frame_size_in_bytes() const {
 156   return in_bytes(frame_map()->framesize_in_bytes());
 157 }
 158 
 159 
 160 // inline cache check: the inline cached class is in G5_inline_cache_reg(G5);
 161 // we fetch the class of the receiver (O0) and compare it with the cached class.
 162 // If they do not match we jump to slow case.
 163 int LIR_Assembler::check_icache() {
 164   int offset = __ offset();
 165   __ inline_cache_check(O0, G5_inline_cache_reg);
 166   return offset;
 167 }
 168 
 169 
 170 void LIR_Assembler::osr_entry() {
 171   // On-stack-replacement entry sequence (interpreter frame layout described in interpreter_sparc.cpp):
 172   //
 173   //   1. Create a new compiled activation.
 174   //   2. Initialize local variables in the compiled activation.  The expression stack must be empty
 175   //      at the osr_bci; it is not initialized.
 176   //   3. Jump to the continuation address in compiled code to resume execution.
 177 
 178   // OSR entry point
 179   offsets()->set_value(CodeOffsets::OSR_Entry, code_offset());
 180   BlockBegin* osr_entry = compilation()->hir()->osr_entry();
 181   ValueStack* entry_state = osr_entry->end()->state();
 182   int number_of_locks = entry_state->locks_size();
 183 
 184   // Create a frame for the compiled activation.
 185   __ build_frame(initial_frame_size_in_bytes(), bang_size_in_bytes());
 186 
 187   // OSR buffer is
 188   //
 189   // locals[nlocals-1..0]
 190   // monitors[number_of_locks-1..0]
 191   //
 192   // locals is a direct copy of the interpreter frame so in the osr buffer
 193   // so first slot in the local array is the last local from the interpreter
 194   // and last slot is local[0] (receiver) from the interpreter
 195   //
 196   // Similarly with locks. The first lock slot in the osr buffer is the nth lock
 197   // from the interpreter frame, the nth lock slot in the osr buffer is 0th lock
 198   // in the interpreter frame (the method lock if a sync method)
 199 
 200   // Initialize monitors in the compiled activation.
 201   //   I0: pointer to osr buffer
 202   //
 203   // All other registers are dead at this point and the locals will be
 204   // copied into place by code emitted in the IR.
 205 
 206   Register OSR_buf = osrBufferPointer()->as_register();
 207   { assert(frame::interpreter_frame_monitor_size() == BasicObjectLock::size(), "adjust code below");
 208     int monitor_offset = BytesPerWord * method()->max_locals() +
 209       (2 * BytesPerWord) * (number_of_locks - 1);
 210     // SharedRuntime::OSR_migration_begin() packs BasicObjectLocks in
 211     // the OSR buffer using 2 word entries: first the lock and then
 212     // the oop.
 213     for (int i = 0; i < number_of_locks; i++) {
 214       int slot_offset = monitor_offset - ((i * 2) * BytesPerWord);
 215 #ifdef ASSERT
 216       // verify the interpreter's monitor has a non-null object
 217       {
 218         Label L;
 219         __ ld_ptr(OSR_buf, slot_offset + 1*BytesPerWord, O7);
 220         __ cmp_and_br_short(O7, G0, Assembler::notEqual, Assembler::pt, L);
 221         __ stop("locked object is NULL");
 222         __ bind(L);
 223       }
 224 #endif // ASSERT
 225       // Copy the lock field into the compiled activation.
 226       __ ld_ptr(OSR_buf, slot_offset + 0, O7);
 227       __ st_ptr(O7, frame_map()->address_for_monitor_lock(i));
 228       __ ld_ptr(OSR_buf, slot_offset + 1*BytesPerWord, O7);
 229       __ st_ptr(O7, frame_map()->address_for_monitor_object(i));
 230     }
 231   }
 232 }
 233 
 234 
 235 // --------------------------------------------------------------------------------------------
 236 
 237 void LIR_Assembler::monitorexit(LIR_Opr obj_opr, LIR_Opr lock_opr, Register hdr, int monitor_no) {
 238   if (!GenerateSynchronizationCode) return;
 239 
 240   Register obj_reg = obj_opr->as_register();
 241   Register lock_reg = lock_opr->as_register();
 242 
 243   Address mon_addr = frame_map()->address_for_monitor_lock(monitor_no);
 244   Register reg = mon_addr.base();
 245   int offset = mon_addr.disp();
 246   // compute pointer to BasicLock
 247   if (mon_addr.is_simm13()) {
 248     __ add(reg, offset, lock_reg);
 249   }
 250   else {
 251     __ set(offset, lock_reg);
 252     __ add(reg, lock_reg, lock_reg);
 253   }
 254   // unlock object
 255   MonitorAccessStub* slow_case = new MonitorExitStub(lock_opr, UseFastLocking, monitor_no);
 256   // _slow_case_stubs->append(slow_case);
 257   // temporary fix: must be created after exceptionhandler, therefore as call stub
 258   _slow_case_stubs->append(slow_case);
 259   if (UseFastLocking) {
 260     // try inlined fast unlocking first, revert to slow locking if it fails
 261     // note: lock_reg points to the displaced header since the displaced header offset is 0!
 262     assert(BasicLock::displaced_header_offset_in_bytes() == 0, "lock_reg must point to the displaced header");
 263     __ unlock_object(hdr, obj_reg, lock_reg, *slow_case->entry());
 264   } else {
 265     // always do slow unlocking
 266     // note: the slow unlocking code could be inlined here, however if we use
 267     //       slow unlocking, speed doesn't matter anyway and this solution is
 268     //       simpler and requires less duplicated code - additionally, the
 269     //       slow unlocking code is the same in either case which simplifies
 270     //       debugging
 271     __ br(Assembler::always, false, Assembler::pt, *slow_case->entry());
 272     __ delayed()->nop();
 273   }
 274   // done
 275   __ bind(*slow_case->continuation());
 276 }
 277 
 278 
 279 int LIR_Assembler::emit_exception_handler() {
 280   // if the last instruction is a call (typically to do a throw which
 281   // is coming at the end after block reordering) the return address
 282   // must still point into the code area in order to avoid assertion
 283   // failures when searching for the corresponding bci => add a nop
 284   // (was bug 5/14/1999 - gri)
 285   __ nop();
 286 
 287   // generate code for exception handler
 288   ciMethod* method = compilation()->method();
 289 
 290   address handler_base = __ start_a_stub(exception_handler_size);
 291 
 292   if (handler_base == NULL) {
 293     // not enough space left for the handler
 294     bailout("exception handler overflow");
 295     return -1;
 296   }
 297 
 298   int offset = code_offset();
 299 
 300   __ call(Runtime1::entry_for(Runtime1::handle_exception_from_callee_id), relocInfo::runtime_call_type);
 301   __ delayed()->nop();
 302   __ should_not_reach_here();
 303   guarantee(code_offset() - offset <= exception_handler_size, "overflow");
 304   __ end_a_stub();
 305 
 306   return offset;
 307 }
 308 
 309 
 310 // Emit the code to remove the frame from the stack in the exception
 311 // unwind path.
 312 int LIR_Assembler::emit_unwind_handler() {
 313 #ifndef PRODUCT
 314   if (CommentedAssembly) {
 315     _masm->block_comment("Unwind handler");
 316   }
 317 #endif
 318 
 319   int offset = code_offset();
 320 
 321   // Fetch the exception from TLS and clear out exception related thread state
 322   __ ld_ptr(G2_thread, in_bytes(JavaThread::exception_oop_offset()), O0);
 323   __ st_ptr(G0, G2_thread, in_bytes(JavaThread::exception_oop_offset()));
 324   __ st_ptr(G0, G2_thread, in_bytes(JavaThread::exception_pc_offset()));
 325 
 326   __ bind(_unwind_handler_entry);
 327   __ verify_not_null_oop(O0);
 328   if (method()->is_synchronized() || compilation()->env()->dtrace_method_probes()) {
 329     __ mov(O0, I0);  // Preserve the exception
 330   }
 331 
 332   // Preform needed unlocking
 333   MonitorExitStub* stub = NULL;
 334   if (method()->is_synchronized()) {
 335     monitor_address(0, FrameMap::I1_opr);
 336     stub = new MonitorExitStub(FrameMap::I1_opr, true, 0);
 337     __ unlock_object(I3, I2, I1, *stub->entry());
 338     __ bind(*stub->continuation());
 339   }
 340 
 341   if (compilation()->env()->dtrace_method_probes()) {
 342     __ mov(G2_thread, O0);
 343     __ save_thread(I1); // need to preserve thread in G2 across
 344                         // runtime call
 345     metadata2reg(method()->constant_encoding(), O1);
 346     __ call(CAST_FROM_FN_PTR(address, SharedRuntime::dtrace_method_exit), relocInfo::runtime_call_type);
 347     __ delayed()->nop();
 348     __ restore_thread(I1);
 349   }
 350 
 351   if (method()->is_synchronized() || compilation()->env()->dtrace_method_probes()) {
 352     __ mov(I0, O0);  // Restore the exception
 353   }
 354 
 355   // dispatch to the unwind logic
 356   __ call(Runtime1::entry_for(Runtime1::unwind_exception_id), relocInfo::runtime_call_type);
 357   __ delayed()->nop();
 358 
 359   // Emit the slow path assembly
 360   if (stub != NULL) {
 361     stub->emit_code(this);
 362   }
 363 
 364   return offset;
 365 }
 366 
 367 
 368 int LIR_Assembler::emit_deopt_handler() {
 369   // if the last instruction is a call (typically to do a throw which
 370   // is coming at the end after block reordering) the return address
 371   // must still point into the code area in order to avoid assertion
 372   // failures when searching for the corresponding bci => add a nop
 373   // (was bug 5/14/1999 - gri)
 374   __ nop();
 375 
 376   // generate code for deopt handler
 377   ciMethod* method = compilation()->method();
 378   address handler_base = __ start_a_stub(deopt_handler_size);
 379   if (handler_base == NULL) {
 380     // not enough space left for the handler
 381     bailout("deopt handler overflow");
 382     return -1;
 383   }
 384 
 385   int offset = code_offset();
 386   AddressLiteral deopt_blob(SharedRuntime::deopt_blob()->unpack());
 387   __ JUMP(deopt_blob, G3_scratch, 0); // sethi;jmp
 388   __ delayed()->nop();
 389   guarantee(code_offset() - offset <= deopt_handler_size, "overflow");
 390   __ end_a_stub();
 391 
 392   return offset;
 393 }
 394 
 395 
 396 void LIR_Assembler::jobject2reg(jobject o, Register reg) {
 397   if (o == NULL) {
 398     __ set(NULL_WORD, reg);
 399   } else {
 400     int oop_index = __ oop_recorder()->find_index(o);
 401     assert(Universe::heap()->is_in_reserved(JNIHandles::resolve(o)), "should be real oop");
 402     RelocationHolder rspec = oop_Relocation::spec(oop_index);
 403     __ set(NULL_WORD, reg, rspec); // Will be set when the nmethod is created
 404   }
 405 }
 406 
 407 
 408 void LIR_Assembler::jobject2reg_with_patching(Register reg, CodeEmitInfo *info) {
 409   // Allocate a new index in table to hold the object once it's been patched
 410   int oop_index = __ oop_recorder()->allocate_oop_index(NULL);
 411   PatchingStub* patch = new PatchingStub(_masm, patching_id(info), oop_index);
 412 
 413   AddressLiteral addrlit(NULL, oop_Relocation::spec(oop_index));
 414   assert(addrlit.rspec().type() == relocInfo::oop_type, "must be an oop reloc");
 415   // It may not seem necessary to use a sethi/add pair to load a NULL into dest, but the
 416   // NULL will be dynamically patched later and the patched value may be large.  We must
 417   // therefore generate the sethi/add as a placeholders
 418   __ patchable_set(addrlit, reg);
 419 
 420   patching_epilog(patch, lir_patch_normal, reg, info);
 421 }
 422 
 423 
 424 void LIR_Assembler::metadata2reg(Metadata* o, Register reg) {
 425   __ set_metadata_constant(o, reg);
 426 }
 427 
 428 void LIR_Assembler::klass2reg_with_patching(Register reg, CodeEmitInfo *info) {
 429   // Allocate a new index in table to hold the klass once it's been patched
 430   int index = __ oop_recorder()->allocate_metadata_index(NULL);
 431   PatchingStub* patch = new PatchingStub(_masm, PatchingStub::load_klass_id, index);
 432   AddressLiteral addrlit(NULL, metadata_Relocation::spec(index));
 433   assert(addrlit.rspec().type() == relocInfo::metadata_type, "must be an metadata reloc");
 434   // It may not seem necessary to use a sethi/add pair to load a NULL into dest, but the
 435   // NULL will be dynamically patched later and the patched value may be large.  We must
 436   // therefore generate the sethi/add as a placeholders
 437   __ patchable_set(addrlit, reg);
 438 
 439   patching_epilog(patch, lir_patch_normal, reg, info);
 440 }
 441 
 442 void LIR_Assembler::emit_op3(LIR_Op3* op) {
 443   Register Rdividend = op->in_opr1()->as_register();
 444   Register Rdivisor  = noreg;
 445   Register Rscratch  = op->in_opr3()->as_register();
 446   Register Rresult   = op->result_opr()->as_register();
 447   int divisor = -1;
 448 
 449   if (op->in_opr2()->is_register()) {
 450     Rdivisor = op->in_opr2()->as_register();
 451   } else {
 452     divisor = op->in_opr2()->as_constant_ptr()->as_jint();
 453     assert(Assembler::is_simm13(divisor), "can only handle simm13");
 454   }
 455 
 456   assert(Rdividend != Rscratch, "");
 457   assert(Rdivisor  != Rscratch, "");
 458   assert(op->code() == lir_idiv || op->code() == lir_irem, "Must be irem or idiv");
 459 
 460   if (Rdivisor == noreg && is_power_of_2(divisor)) {
 461     // convert division by a power of two into some shifts and logical operations
 462     if (op->code() == lir_idiv) {
 463       if (divisor == 2) {
 464         __ srl(Rdividend, 31, Rscratch);
 465       } else {
 466         __ sra(Rdividend, 31, Rscratch);
 467         __ and3(Rscratch, divisor - 1, Rscratch);
 468       }
 469       __ add(Rdividend, Rscratch, Rscratch);
 470       __ sra(Rscratch, log2_intptr(divisor), Rresult);
 471       return;
 472     } else {
 473       if (divisor == 2) {
 474         __ srl(Rdividend, 31, Rscratch);
 475       } else {
 476         __ sra(Rdividend, 31, Rscratch);
 477         __ and3(Rscratch, divisor - 1,Rscratch);
 478       }
 479       __ add(Rdividend, Rscratch, Rscratch);
 480       __ andn(Rscratch, divisor - 1,Rscratch);
 481       __ sub(Rdividend, Rscratch, Rresult);
 482       return;
 483     }
 484   }
 485 
 486   __ sra(Rdividend, 31, Rscratch);
 487   __ wry(Rscratch);
 488 
 489   add_debug_info_for_div0_here(op->info());
 490 
 491   if (Rdivisor != noreg) {
 492     __ sdivcc(Rdividend, Rdivisor, (op->code() == lir_idiv ? Rresult : Rscratch));
 493   } else {
 494     assert(Assembler::is_simm13(divisor), "can only handle simm13");
 495     __ sdivcc(Rdividend, divisor, (op->code() == lir_idiv ? Rresult : Rscratch));
 496   }
 497 
 498   Label skip;
 499   __ br(Assembler::overflowSet, true, Assembler::pn, skip);
 500   __ delayed()->Assembler::sethi(0x80000000, (op->code() == lir_idiv ? Rresult : Rscratch));
 501   __ bind(skip);
 502 
 503   if (op->code() == lir_irem) {
 504     if (Rdivisor != noreg) {
 505       __ smul(Rscratch, Rdivisor, Rscratch);
 506     } else {
 507       __ smul(Rscratch, divisor, Rscratch);
 508     }
 509     __ sub(Rdividend, Rscratch, Rresult);
 510   }
 511 }
 512 
 513 
 514 void LIR_Assembler::emit_opBranch(LIR_OpBranch* op) {
 515 #ifdef ASSERT
 516   assert(op->block() == NULL || op->block()->label() == op->label(), "wrong label");
 517   if (op->block() != NULL)  _branch_target_blocks.append(op->block());
 518   if (op->ublock() != NULL) _branch_target_blocks.append(op->ublock());
 519 #endif
 520   assert(op->info() == NULL, "shouldn't have CodeEmitInfo");
 521 
 522   if (op->cond() == lir_cond_always) {
 523     __ br(Assembler::always, false, Assembler::pt, *(op->label()));
 524   } else if (op->code() == lir_cond_float_branch) {
 525     assert(op->ublock() != NULL, "must have unordered successor");
 526     bool is_unordered = (op->ublock() == op->block());
 527     Assembler::Condition acond;
 528     switch (op->cond()) {
 529       case lir_cond_equal:         acond = Assembler::f_equal;    break;
 530       case lir_cond_notEqual:      acond = Assembler::f_notEqual; break;
 531       case lir_cond_less:          acond = (is_unordered ? Assembler::f_unorderedOrLess          : Assembler::f_less);           break;
 532       case lir_cond_greater:       acond = (is_unordered ? Assembler::f_unorderedOrGreater       : Assembler::f_greater);        break;
 533       case lir_cond_lessEqual:     acond = (is_unordered ? Assembler::f_unorderedOrLessOrEqual   : Assembler::f_lessOrEqual);    break;
 534       case lir_cond_greaterEqual:  acond = (is_unordered ? Assembler::f_unorderedOrGreaterOrEqual: Assembler::f_greaterOrEqual); break;
 535       default :                         ShouldNotReachHere();
 536     }
 537     __ fb( acond, false, Assembler::pn, *(op->label()));
 538   } else {
 539     assert (op->code() == lir_branch, "just checking");
 540 
 541     Assembler::Condition acond;
 542     switch (op->cond()) {
 543       case lir_cond_equal:        acond = Assembler::equal;                break;
 544       case lir_cond_notEqual:     acond = Assembler::notEqual;             break;
 545       case lir_cond_less:         acond = Assembler::less;                 break;
 546       case lir_cond_lessEqual:    acond = Assembler::lessEqual;            break;
 547       case lir_cond_greaterEqual: acond = Assembler::greaterEqual;         break;
 548       case lir_cond_greater:      acond = Assembler::greater;              break;
 549       case lir_cond_aboveEqual:   acond = Assembler::greaterEqualUnsigned; break;
 550       case lir_cond_belowEqual:   acond = Assembler::lessEqualUnsigned;    break;
 551       default:                         ShouldNotReachHere();
 552     };
 553 
 554     // sparc has different condition codes for testing 32-bit
 555     // vs. 64-bit values.  We could always test xcc is we could
 556     // guarantee that 32-bit loads always sign extended but that isn't
 557     // true and since sign extension isn't free, it would impose a
 558     // slight cost.
 559 #ifdef _LP64
 560     if  (op->type() == T_INT) {
 561       __ br(acond, false, Assembler::pn, *(op->label()));
 562     } else
 563 #endif
 564       __ brx(acond, false, Assembler::pn, *(op->label()));
 565   }
 566   // The peephole pass fills the delay slot
 567 }
 568 
 569 
 570 void LIR_Assembler::emit_opConvert(LIR_OpConvert* op) {
 571   Bytecodes::Code code = op->bytecode();
 572   LIR_Opr dst = op->result_opr();
 573 
 574   switch(code) {
 575     case Bytecodes::_i2l: {
 576       Register rlo  = dst->as_register_lo();
 577       Register rhi  = dst->as_register_hi();
 578       Register rval = op->in_opr()->as_register();
 579 #ifdef _LP64
 580       __ sra(rval, 0, rlo);
 581 #else
 582       __ mov(rval, rlo);
 583       __ sra(rval, BitsPerInt-1, rhi);
 584 #endif
 585       break;
 586     }
 587     case Bytecodes::_i2d:
 588     case Bytecodes::_i2f: {
 589       bool is_double = (code == Bytecodes::_i2d);
 590       FloatRegister rdst = is_double ? dst->as_double_reg() : dst->as_float_reg();
 591       FloatRegisterImpl::Width w = is_double ? FloatRegisterImpl::D : FloatRegisterImpl::S;
 592       FloatRegister rsrc = op->in_opr()->as_float_reg();
 593       if (rsrc != rdst) {
 594         __ fmov(FloatRegisterImpl::S, rsrc, rdst);
 595       }
 596       __ fitof(w, rdst, rdst);
 597       break;
 598     }
 599     case Bytecodes::_f2i:{
 600       FloatRegister rsrc = op->in_opr()->as_float_reg();
 601       Address       addr = frame_map()->address_for_slot(dst->single_stack_ix());
 602       Label L;
 603       // result must be 0 if value is NaN; test by comparing value to itself
 604       __ fcmp(FloatRegisterImpl::S, Assembler::fcc0, rsrc, rsrc);
 605       __ fb(Assembler::f_unordered, true, Assembler::pn, L);
 606       __ delayed()->st(G0, addr); // annuled if contents of rsrc is not NaN
 607       __ ftoi(FloatRegisterImpl::S, rsrc, rsrc);
 608       // move integer result from float register to int register
 609       __ stf(FloatRegisterImpl::S, rsrc, addr.base(), addr.disp());
 610       __ bind (L);
 611       break;
 612     }
 613     case Bytecodes::_l2i: {
 614       Register rlo  = op->in_opr()->as_register_lo();
 615       Register rhi  = op->in_opr()->as_register_hi();
 616       Register rdst = dst->as_register();
 617 #ifdef _LP64
 618       __ sra(rlo, 0, rdst);
 619 #else
 620       __ mov(rlo, rdst);
 621 #endif
 622       break;
 623     }
 624     case Bytecodes::_d2f:
 625     case Bytecodes::_f2d: {
 626       bool is_double = (code == Bytecodes::_f2d);
 627       assert((!is_double && dst->is_single_fpu()) || (is_double && dst->is_double_fpu()), "check");
 628       LIR_Opr val = op->in_opr();
 629       FloatRegister rval = (code == Bytecodes::_d2f) ? val->as_double_reg() : val->as_float_reg();
 630       FloatRegister rdst = is_double ? dst->as_double_reg() : dst->as_float_reg();
 631       FloatRegisterImpl::Width vw = is_double ? FloatRegisterImpl::S : FloatRegisterImpl::D;
 632       FloatRegisterImpl::Width dw = is_double ? FloatRegisterImpl::D : FloatRegisterImpl::S;
 633       __ ftof(vw, dw, rval, rdst);
 634       break;
 635     }
 636     case Bytecodes::_i2s:
 637     case Bytecodes::_i2b: {
 638       Register rval = op->in_opr()->as_register();
 639       Register rdst = dst->as_register();
 640       int shift = (code == Bytecodes::_i2b) ? (BitsPerInt - T_BYTE_aelem_bytes * BitsPerByte) : (BitsPerInt - BitsPerShort);
 641       __ sll (rval, shift, rdst);
 642       __ sra (rdst, shift, rdst);
 643       break;
 644     }
 645     case Bytecodes::_i2c: {
 646       Register rval = op->in_opr()->as_register();
 647       Register rdst = dst->as_register();
 648       int shift = BitsPerInt - T_CHAR_aelem_bytes * BitsPerByte;
 649       __ sll (rval, shift, rdst);
 650       __ srl (rdst, shift, rdst);
 651       break;
 652     }
 653 
 654     default: ShouldNotReachHere();
 655   }
 656 }
 657 
 658 
 659 void LIR_Assembler::align_call(LIR_Code) {
 660   // do nothing since all instructions are word aligned on sparc
 661 }
 662 
 663 
 664 void LIR_Assembler::call(LIR_OpJavaCall* op, relocInfo::relocType rtype) {
 665   __ call(op->addr(), rtype);
 666   // The peephole pass fills the delay slot, add_call_info is done in
 667   // LIR_Assembler::emit_delay.
 668 }
 669 
 670 
 671 void LIR_Assembler::ic_call(LIR_OpJavaCall* op) {
 672   __ ic_call(op->addr(), false);
 673   // The peephole pass fills the delay slot, add_call_info is done in
 674   // LIR_Assembler::emit_delay.
 675 }
 676 
 677 
 678 void LIR_Assembler::vtable_call(LIR_OpJavaCall* op) {
 679   add_debug_info_for_null_check_here(op->info());
 680   __ load_klass(O0, G3_scratch);
 681   if (Assembler::is_simm13(op->vtable_offset())) {
 682     __ ld_ptr(G3_scratch, op->vtable_offset(), G5_method);
 683   } else {
 684     // This will generate 2 instructions
 685     __ set(op->vtable_offset(), G5_method);
 686     // ld_ptr, set_hi, set
 687     __ ld_ptr(G3_scratch, G5_method, G5_method);
 688   }
 689   __ ld_ptr(G5_method, Method::from_compiled_offset(), G3_scratch);
 690   __ callr(G3_scratch, G0);
 691   // the peephole pass fills the delay slot
 692 }
 693 
 694 int LIR_Assembler::store(LIR_Opr from_reg, Register base, int offset, BasicType type, bool wide, bool unaligned) {
 695   int store_offset;
 696   if (!Assembler::is_simm13(offset + (type == T_LONG) ? wordSize : 0)) {
 697     assert(!unaligned, "can't handle this");
 698     // for offsets larger than a simm13 we setup the offset in O7
 699     __ set(offset, O7);
 700     store_offset = store(from_reg, base, O7, type, wide);
 701   } else {
 702     if (type == T_ARRAY || type == T_OBJECT) {
 703       __ verify_oop(from_reg->as_register());
 704     }
 705     store_offset = code_offset();
 706     switch (type) {
 707       case T_BOOLEAN: // fall through
 708       case T_BYTE  : __ stb(from_reg->as_register(), base, offset); break;
 709       case T_CHAR  : __ sth(from_reg->as_register(), base, offset); break;
 710       case T_SHORT : __ sth(from_reg->as_register(), base, offset); break;
 711       case T_INT   : __ stw(from_reg->as_register(), base, offset); break;
 712       case T_LONG  :
 713 #ifdef _LP64
 714         if (unaligned || PatchALot) {
 715           __ srax(from_reg->as_register_lo(), 32, O7);
 716           __ stw(from_reg->as_register_lo(), base, offset + lo_word_offset_in_bytes);
 717           __ stw(O7,                         base, offset + hi_word_offset_in_bytes);
 718         } else {
 719           __ stx(from_reg->as_register_lo(), base, offset);
 720         }
 721 #else
 722         assert(Assembler::is_simm13(offset + 4), "must be");
 723         __ stw(from_reg->as_register_lo(), base, offset + lo_word_offset_in_bytes);
 724         __ stw(from_reg->as_register_hi(), base, offset + hi_word_offset_in_bytes);
 725 #endif
 726         break;
 727       case T_ADDRESS:
 728       case T_METADATA:
 729         __ st_ptr(from_reg->as_register(), base, offset);
 730         break;
 731       case T_ARRAY : // fall through
 732       case T_OBJECT:
 733         {
 734           if (UseCompressedOops && !wide) {
 735             __ encode_heap_oop(from_reg->as_register(), G3_scratch);
 736             store_offset = code_offset();
 737             __ stw(G3_scratch, base, offset);
 738           } else {
 739             __ st_ptr(from_reg->as_register(), base, offset);
 740           }
 741           break;
 742         }
 743 
 744       case T_FLOAT : __ stf(FloatRegisterImpl::S, from_reg->as_float_reg(), base, offset); break;
 745       case T_DOUBLE:
 746         {
 747           FloatRegister reg = from_reg->as_double_reg();
 748           // split unaligned stores
 749           if (unaligned || PatchALot) {
 750             assert(Assembler::is_simm13(offset + 4), "must be");
 751             __ stf(FloatRegisterImpl::S, reg->successor(), base, offset + 4);
 752             __ stf(FloatRegisterImpl::S, reg,              base, offset);
 753           } else {
 754             __ stf(FloatRegisterImpl::D, reg, base, offset);
 755           }
 756           break;
 757         }
 758       default      : ShouldNotReachHere();
 759     }
 760   }
 761   return store_offset;
 762 }
 763 
 764 
 765 int LIR_Assembler::store(LIR_Opr from_reg, Register base, Register disp, BasicType type, bool wide) {
 766   if (type == T_ARRAY || type == T_OBJECT) {
 767     __ verify_oop(from_reg->as_register());
 768   }
 769   int store_offset = code_offset();
 770   switch (type) {
 771     case T_BOOLEAN: // fall through
 772     case T_BYTE  : __ stb(from_reg->as_register(), base, disp); break;
 773     case T_CHAR  : __ sth(from_reg->as_register(), base, disp); break;
 774     case T_SHORT : __ sth(from_reg->as_register(), base, disp); break;
 775     case T_INT   : __ stw(from_reg->as_register(), base, disp); break;
 776     case T_LONG  :
 777 #ifdef _LP64
 778       __ stx(from_reg->as_register_lo(), base, disp);
 779 #else
 780       assert(from_reg->as_register_hi()->successor() == from_reg->as_register_lo(), "must match");
 781       __ std(from_reg->as_register_hi(), base, disp);
 782 #endif
 783       break;
 784     case T_ADDRESS:
 785       __ st_ptr(from_reg->as_register(), base, disp);
 786       break;
 787     case T_ARRAY : // fall through
 788     case T_OBJECT:
 789       {
 790         if (UseCompressedOops && !wide) {
 791           __ encode_heap_oop(from_reg->as_register(), G3_scratch);
 792           store_offset = code_offset();
 793           __ stw(G3_scratch, base, disp);
 794         } else {
 795           __ st_ptr(from_reg->as_register(), base, disp);
 796         }
 797         break;
 798       }
 799     case T_FLOAT : __ stf(FloatRegisterImpl::S, from_reg->as_float_reg(), base, disp); break;
 800     case T_DOUBLE: __ stf(FloatRegisterImpl::D, from_reg->as_double_reg(), base, disp); break;
 801     default      : ShouldNotReachHere();
 802   }
 803   return store_offset;
 804 }
 805 
 806 
 807 int LIR_Assembler::load(Register base, int offset, LIR_Opr to_reg, BasicType type, bool wide, bool unaligned) {
 808   int load_offset;
 809   if (!Assembler::is_simm13(offset + (type == T_LONG) ? wordSize : 0)) {
 810     assert(base != O7, "destroying register");
 811     assert(!unaligned, "can't handle this");
 812     // for offsets larger than a simm13 we setup the offset in O7
 813     __ set(offset, O7);
 814     load_offset = load(base, O7, to_reg, type, wide);
 815   } else {
 816     load_offset = code_offset();
 817     switch(type) {
 818       case T_BOOLEAN: // fall through
 819       case T_BYTE  : __ ldsb(base, offset, to_reg->as_register()); break;
 820       case T_CHAR  : __ lduh(base, offset, to_reg->as_register()); break;
 821       case T_SHORT : __ ldsh(base, offset, to_reg->as_register()); break;
 822       case T_INT   : __ ld(base, offset, to_reg->as_register()); break;
 823       case T_LONG  :
 824         if (!unaligned) {
 825 #ifdef _LP64
 826           __ ldx(base, offset, to_reg->as_register_lo());
 827 #else
 828           assert(to_reg->as_register_hi()->successor() == to_reg->as_register_lo(),
 829                  "must be sequential");
 830           __ ldd(base, offset, to_reg->as_register_hi());
 831 #endif
 832         } else {
 833 #ifdef _LP64
 834           assert(base != to_reg->as_register_lo(), "can't handle this");
 835           assert(O7 != to_reg->as_register_lo(), "can't handle this");
 836           __ ld(base, offset + hi_word_offset_in_bytes, to_reg->as_register_lo());
 837           __ lduw(base, offset + lo_word_offset_in_bytes, O7); // in case O7 is base or offset, use it last
 838           __ sllx(to_reg->as_register_lo(), 32, to_reg->as_register_lo());
 839           __ or3(to_reg->as_register_lo(), O7, to_reg->as_register_lo());
 840 #else
 841           if (base == to_reg->as_register_lo()) {
 842             __ ld(base, offset + hi_word_offset_in_bytes, to_reg->as_register_hi());
 843             __ ld(base, offset + lo_word_offset_in_bytes, to_reg->as_register_lo());
 844           } else {
 845             __ ld(base, offset + lo_word_offset_in_bytes, to_reg->as_register_lo());
 846             __ ld(base, offset + hi_word_offset_in_bytes, to_reg->as_register_hi());
 847           }
 848 #endif
 849         }
 850         break;
 851       case T_METADATA:  __ ld_ptr(base, offset, to_reg->as_register()); break;
 852       case T_ADDRESS:
 853 #ifdef _LP64
 854         if (offset == oopDesc::klass_offset_in_bytes() && UseCompressedClassPointers) {
 855           __ lduw(base, offset, to_reg->as_register());
 856           __ decode_klass_not_null(to_reg->as_register());
 857         } else
 858 #endif
 859         {
 860           __ ld_ptr(base, offset, to_reg->as_register());
 861         }
 862         break;
 863       case T_ARRAY : // fall through
 864       case T_OBJECT:
 865         {
 866           if (UseCompressedOops && !wide) {
 867             __ lduw(base, offset, to_reg->as_register());
 868             __ decode_heap_oop(to_reg->as_register());
 869           } else {
 870             __ ld_ptr(base, offset, to_reg->as_register());
 871           }
 872           break;
 873         }
 874       case T_FLOAT:  __ ldf(FloatRegisterImpl::S, base, offset, to_reg->as_float_reg()); break;
 875       case T_DOUBLE:
 876         {
 877           FloatRegister reg = to_reg->as_double_reg();
 878           // split unaligned loads
 879           if (unaligned || PatchALot) {
 880             __ ldf(FloatRegisterImpl::S, base, offset + 4, reg->successor());
 881             __ ldf(FloatRegisterImpl::S, base, offset,     reg);
 882           } else {
 883             __ ldf(FloatRegisterImpl::D, base, offset, to_reg->as_double_reg());
 884           }
 885           break;
 886         }
 887       default      : ShouldNotReachHere();
 888     }
 889     if (type == T_ARRAY || type == T_OBJECT) {
 890       __ verify_oop(to_reg->as_register());
 891     }
 892   }
 893   return load_offset;
 894 }
 895 
 896 
 897 int LIR_Assembler::load(Register base, Register disp, LIR_Opr to_reg, BasicType type, bool wide) {
 898   int load_offset = code_offset();
 899   switch(type) {
 900     case T_BOOLEAN: // fall through
 901     case T_BYTE  :  __ ldsb(base, disp, to_reg->as_register()); break;
 902     case T_CHAR  :  __ lduh(base, disp, to_reg->as_register()); break;
 903     case T_SHORT :  __ ldsh(base, disp, to_reg->as_register()); break;
 904     case T_INT   :  __ ld(base, disp, to_reg->as_register()); break;
 905     case T_ADDRESS: __ ld_ptr(base, disp, to_reg->as_register()); break;
 906     case T_ARRAY : // fall through
 907     case T_OBJECT:
 908       {
 909           if (UseCompressedOops && !wide) {
 910             __ lduw(base, disp, to_reg->as_register());
 911             __ decode_heap_oop(to_reg->as_register());
 912           } else {
 913             __ ld_ptr(base, disp, to_reg->as_register());
 914           }
 915           break;
 916       }
 917     case T_FLOAT:  __ ldf(FloatRegisterImpl::S, base, disp, to_reg->as_float_reg()); break;
 918     case T_DOUBLE: __ ldf(FloatRegisterImpl::D, base, disp, to_reg->as_double_reg()); break;
 919     case T_LONG  :
 920 #ifdef _LP64
 921       __ ldx(base, disp, to_reg->as_register_lo());
 922 #else
 923       assert(to_reg->as_register_hi()->successor() == to_reg->as_register_lo(),
 924              "must be sequential");
 925       __ ldd(base, disp, to_reg->as_register_hi());
 926 #endif
 927       break;
 928     default      : ShouldNotReachHere();
 929   }
 930   if (type == T_ARRAY || type == T_OBJECT) {
 931     __ verify_oop(to_reg->as_register());
 932   }
 933   return load_offset;
 934 }
 935 
 936 void LIR_Assembler::const2stack(LIR_Opr src, LIR_Opr dest) {
 937   LIR_Const* c = src->as_constant_ptr();
 938   switch (c->type()) {
 939     case T_INT:
 940     case T_FLOAT: {
 941       Register src_reg = O7;
 942       int value = c->as_jint_bits();
 943       if (value == 0) {
 944         src_reg = G0;
 945       } else {
 946         __ set(value, O7);
 947       }
 948       Address addr = frame_map()->address_for_slot(dest->single_stack_ix());
 949       __ stw(src_reg, addr.base(), addr.disp());
 950       break;
 951     }
 952     case T_ADDRESS: {
 953       Register src_reg = O7;
 954       int value = c->as_jint_bits();
 955       if (value == 0) {
 956         src_reg = G0;
 957       } else {
 958         __ set(value, O7);
 959       }
 960       Address addr = frame_map()->address_for_slot(dest->single_stack_ix());
 961       __ st_ptr(src_reg, addr.base(), addr.disp());
 962       break;
 963     }
 964     case T_OBJECT: {
 965       Register src_reg = O7;
 966       jobject2reg(c->as_jobject(), src_reg);
 967       Address addr = frame_map()->address_for_slot(dest->single_stack_ix());
 968       __ st_ptr(src_reg, addr.base(), addr.disp());
 969       break;
 970     }
 971     case T_LONG:
 972     case T_DOUBLE: {
 973       Address addr = frame_map()->address_for_double_slot(dest->double_stack_ix());
 974 
 975       Register tmp = O7;
 976       int value_lo = c->as_jint_lo_bits();
 977       if (value_lo == 0) {
 978         tmp = G0;
 979       } else {
 980         __ set(value_lo, O7);
 981       }
 982       __ stw(tmp, addr.base(), addr.disp() + lo_word_offset_in_bytes);
 983       int value_hi = c->as_jint_hi_bits();
 984       if (value_hi == 0) {
 985         tmp = G0;
 986       } else {
 987         __ set(value_hi, O7);
 988       }
 989       __ stw(tmp, addr.base(), addr.disp() + hi_word_offset_in_bytes);
 990       break;
 991     }
 992     default:
 993       Unimplemented();
 994   }
 995 }
 996 
 997 
 998 void LIR_Assembler::const2mem(LIR_Opr src, LIR_Opr dest, BasicType type, CodeEmitInfo* info, bool wide) {
 999   LIR_Const* c = src->as_constant_ptr();
1000   LIR_Address* addr     = dest->as_address_ptr();
1001   Register base = addr->base()->as_pointer_register();
1002   int offset = -1;
1003 
1004   switch (c->type()) {
1005     case T_INT:
1006     case T_FLOAT:
1007     case T_ADDRESS: {
1008       LIR_Opr tmp = FrameMap::O7_opr;
1009       int value = c->as_jint_bits();
1010       if (value == 0) {
1011         tmp = FrameMap::G0_opr;
1012       } else if (Assembler::is_simm13(value)) {
1013         __ set(value, O7);
1014       }
1015       if (addr->index()->is_valid()) {
1016         assert(addr->disp() == 0, "must be zero");
1017         offset = store(tmp, base, addr->index()->as_pointer_register(), type, wide);
1018       } else {
1019         assert(Assembler::is_simm13(addr->disp()), "can't handle larger addresses");
1020         offset = store(tmp, base, addr->disp(), type, wide, false);
1021       }
1022       break;
1023     }
1024     case T_LONG:
1025     case T_DOUBLE: {
1026       assert(!addr->index()->is_valid(), "can't handle reg reg address here");
1027       assert(Assembler::is_simm13(addr->disp()) &&
1028              Assembler::is_simm13(addr->disp() + 4), "can't handle larger addresses");
1029 
1030       LIR_Opr tmp = FrameMap::O7_opr;
1031       int value_lo = c->as_jint_lo_bits();
1032       if (value_lo == 0) {
1033         tmp = FrameMap::G0_opr;
1034       } else {
1035         __ set(value_lo, O7);
1036       }
1037       offset = store(tmp, base, addr->disp() + lo_word_offset_in_bytes, T_INT, wide, false);
1038       int value_hi = c->as_jint_hi_bits();
1039       if (value_hi == 0) {
1040         tmp = FrameMap::G0_opr;
1041       } else {
1042         __ set(value_hi, O7);
1043       }
1044       store(tmp, base, addr->disp() + hi_word_offset_in_bytes, T_INT, wide, false);
1045       break;
1046     }
1047     case T_OBJECT: {
1048       jobject obj = c->as_jobject();
1049       LIR_Opr tmp;
1050       if (obj == NULL) {
1051         tmp = FrameMap::G0_opr;
1052       } else {
1053         tmp = FrameMap::O7_opr;
1054         jobject2reg(c->as_jobject(), O7);
1055       }
1056       // handle either reg+reg or reg+disp address
1057       if (addr->index()->is_valid()) {
1058         assert(addr->disp() == 0, "must be zero");
1059         offset = store(tmp, base, addr->index()->as_pointer_register(), type, wide);
1060       } else {
1061         assert(Assembler::is_simm13(addr->disp()), "can't handle larger addresses");
1062         offset = store(tmp, base, addr->disp(), type, wide, false);
1063       }
1064 
1065       break;
1066     }
1067     default:
1068       Unimplemented();
1069   }
1070   if (info != NULL) {
1071     assert(offset != -1, "offset should've been set");
1072     add_debug_info_for_null_check(offset, info);
1073   }
1074 }
1075 
1076 
1077 void LIR_Assembler::const2reg(LIR_Opr src, LIR_Opr dest, LIR_PatchCode patch_code, CodeEmitInfo* info) {
1078   LIR_Const* c = src->as_constant_ptr();
1079   LIR_Opr to_reg = dest;
1080 
1081   switch (c->type()) {
1082     case T_INT:
1083     case T_ADDRESS:
1084       {
1085         jint con = c->as_jint();
1086         if (to_reg->is_single_cpu()) {
1087           assert(patch_code == lir_patch_none, "no patching handled here");
1088           __ set(con, to_reg->as_register());
1089         } else {
1090           ShouldNotReachHere();
1091           assert(to_reg->is_single_fpu(), "wrong register kind");
1092 
1093           __ set(con, O7);
1094           Address temp_slot(SP, (frame::register_save_words * wordSize) + STACK_BIAS);
1095           __ st(O7, temp_slot);
1096           __ ldf(FloatRegisterImpl::S, temp_slot, to_reg->as_float_reg());
1097         }
1098       }
1099       break;
1100 
1101     case T_LONG:
1102       {
1103         jlong con = c->as_jlong();
1104 
1105         if (to_reg->is_double_cpu()) {
1106 #ifdef _LP64
1107           __ set(con,  to_reg->as_register_lo());
1108 #else
1109           __ set(low(con),  to_reg->as_register_lo());
1110           __ set(high(con), to_reg->as_register_hi());
1111 #endif
1112 #ifdef _LP64
1113         } else if (to_reg->is_single_cpu()) {
1114           __ set(con, to_reg->as_register());
1115 #endif
1116         } else {
1117           ShouldNotReachHere();
1118           assert(to_reg->is_double_fpu(), "wrong register kind");
1119           Address temp_slot_lo(SP, ((frame::register_save_words  ) * wordSize) + STACK_BIAS);
1120           Address temp_slot_hi(SP, ((frame::register_save_words) * wordSize) + (longSize/2) + STACK_BIAS);
1121           __ set(low(con),  O7);
1122           __ st(O7, temp_slot_lo);
1123           __ set(high(con), O7);
1124           __ st(O7, temp_slot_hi);
1125           __ ldf(FloatRegisterImpl::D, temp_slot_lo, to_reg->as_double_reg());
1126         }
1127       }
1128       break;
1129 
1130     case T_OBJECT:
1131       {
1132         if (patch_code == lir_patch_none) {
1133           jobject2reg(c->as_jobject(), to_reg->as_register());
1134         } else {
1135           jobject2reg_with_patching(to_reg->as_register(), info);
1136         }
1137       }
1138       break;
1139 
1140     case T_METADATA:
1141       {
1142         if (patch_code == lir_patch_none) {
1143           metadata2reg(c->as_metadata(), to_reg->as_register());
1144         } else {
1145           klass2reg_with_patching(to_reg->as_register(), info);
1146         }
1147       }
1148       break;
1149 
1150     case T_FLOAT:
1151       {
1152         address const_addr = __ float_constant(c->as_jfloat());
1153         if (const_addr == NULL) {
1154           bailout("const section overflow");
1155           break;
1156         }
1157         RelocationHolder rspec = internal_word_Relocation::spec(const_addr);
1158         AddressLiteral const_addrlit(const_addr, rspec);
1159         if (to_reg->is_single_fpu()) {
1160           __ patchable_sethi(const_addrlit, O7);
1161           __ relocate(rspec);
1162           __ ldf(FloatRegisterImpl::S, O7, const_addrlit.low10(), to_reg->as_float_reg());
1163 
1164         } else {
1165           assert(to_reg->is_single_cpu(), "Must be a cpu register.");
1166 
1167           __ set(const_addrlit, O7);
1168           __ ld(O7, 0, to_reg->as_register());
1169         }
1170       }
1171       break;
1172 
1173     case T_DOUBLE:
1174       {
1175         address const_addr = __ double_constant(c->as_jdouble());
1176         if (const_addr == NULL) {
1177           bailout("const section overflow");
1178           break;
1179         }
1180         RelocationHolder rspec = internal_word_Relocation::spec(const_addr);
1181 
1182         if (to_reg->is_double_fpu()) {
1183           AddressLiteral const_addrlit(const_addr, rspec);
1184           __ patchable_sethi(const_addrlit, O7);
1185           __ relocate(rspec);
1186           __ ldf (FloatRegisterImpl::D, O7, const_addrlit.low10(), to_reg->as_double_reg());
1187         } else {
1188           assert(to_reg->is_double_cpu(), "Must be a long register.");
1189 #ifdef _LP64
1190           __ set(jlong_cast(c->as_jdouble()), to_reg->as_register_lo());
1191 #else
1192           __ set(low(jlong_cast(c->as_jdouble())), to_reg->as_register_lo());
1193           __ set(high(jlong_cast(c->as_jdouble())), to_reg->as_register_hi());
1194 #endif
1195         }
1196 
1197       }
1198       break;
1199 
1200     default:
1201       ShouldNotReachHere();
1202   }
1203 }
1204 
1205 Address LIR_Assembler::as_Address(LIR_Address* addr) {
1206   Register reg = addr->base()->as_pointer_register();
1207   LIR_Opr index = addr->index();
1208   if (index->is_illegal()) {
1209     return Address(reg, addr->disp());
1210   } else {
1211     assert (addr->disp() == 0, "unsupported address mode");
1212     return Address(reg, index->as_pointer_register());
1213   }
1214 }
1215 
1216 
1217 void LIR_Assembler::stack2stack(LIR_Opr src, LIR_Opr dest, BasicType type) {
1218   switch (type) {
1219     case T_INT:
1220     case T_FLOAT: {
1221       Register tmp = O7;
1222       Address from = frame_map()->address_for_slot(src->single_stack_ix());
1223       Address to   = frame_map()->address_for_slot(dest->single_stack_ix());
1224       __ lduw(from.base(), from.disp(), tmp);
1225       __ stw(tmp, to.base(), to.disp());
1226       break;
1227     }
1228     case T_OBJECT: {
1229       Register tmp = O7;
1230       Address from = frame_map()->address_for_slot(src->single_stack_ix());
1231       Address to   = frame_map()->address_for_slot(dest->single_stack_ix());
1232       __ ld_ptr(from.base(), from.disp(), tmp);
1233       __ st_ptr(tmp, to.base(), to.disp());
1234       break;
1235     }
1236     case T_LONG:
1237     case T_DOUBLE: {
1238       Register tmp = O7;
1239       Address from = frame_map()->address_for_double_slot(src->double_stack_ix());
1240       Address to   = frame_map()->address_for_double_slot(dest->double_stack_ix());
1241       __ lduw(from.base(), from.disp(), tmp);
1242       __ stw(tmp, to.base(), to.disp());
1243       __ lduw(from.base(), from.disp() + 4, tmp);
1244       __ stw(tmp, to.base(), to.disp() + 4);
1245       break;
1246     }
1247 
1248     default:
1249       ShouldNotReachHere();
1250   }
1251 }
1252 
1253 
1254 Address LIR_Assembler::as_Address_hi(LIR_Address* addr) {
1255   Address base = as_Address(addr);
1256   return Address(base.base(), base.disp() + hi_word_offset_in_bytes);
1257 }
1258 
1259 
1260 Address LIR_Assembler::as_Address_lo(LIR_Address* addr) {
1261   Address base = as_Address(addr);
1262   return Address(base.base(), base.disp() + lo_word_offset_in_bytes);
1263 }
1264 
1265 
1266 void LIR_Assembler::mem2reg(LIR_Opr src_opr, LIR_Opr dest, BasicType type,
1267                             LIR_PatchCode patch_code, CodeEmitInfo* info, bool wide, bool unaligned) {
1268 
1269   assert(type != T_METADATA, "load of metadata ptr not supported");
1270   LIR_Address* addr = src_opr->as_address_ptr();
1271   LIR_Opr to_reg = dest;
1272 
1273   Register src = addr->base()->as_pointer_register();
1274   Register disp_reg = noreg;
1275   int disp_value = addr->disp();
1276   bool needs_patching = (patch_code != lir_patch_none);
1277 
1278   if (addr->base()->type() == T_OBJECT) {
1279     __ verify_oop(src);
1280   }
1281 
1282   PatchingStub* patch = NULL;
1283   if (needs_patching) {
1284     patch = new PatchingStub(_masm, PatchingStub::access_field_id);
1285     assert(!to_reg->is_double_cpu() ||
1286            patch_code == lir_patch_none ||
1287            patch_code == lir_patch_normal, "patching doesn't match register");
1288   }
1289 
1290   if (addr->index()->is_illegal()) {
1291     if (!Assembler::is_simm13(disp_value) && (!unaligned || Assembler::is_simm13(disp_value + 4))) {
1292       if (needs_patching) {
1293         __ patchable_set(0, O7);
1294       } else {
1295         __ set(disp_value, O7);
1296       }
1297       disp_reg = O7;
1298     }
1299   } else if (unaligned || PatchALot) {
1300     __ add(src, addr->index()->as_register(), O7);
1301     src = O7;
1302   } else {
1303     disp_reg = addr->index()->as_pointer_register();
1304     assert(disp_value == 0, "can't handle 3 operand addresses");
1305   }
1306 
1307   // remember the offset of the load.  The patching_epilog must be done
1308   // before the call to add_debug_info, otherwise the PcDescs don't get
1309   // entered in increasing order.
1310   int offset = code_offset();
1311 
1312   assert(disp_reg != noreg || Assembler::is_simm13(disp_value), "should have set this up");
1313   if (disp_reg == noreg) {
1314     offset = load(src, disp_value, to_reg, type, wide, unaligned);
1315   } else {
1316     assert(!unaligned, "can't handle this");
1317     offset = load(src, disp_reg, to_reg, type, wide);
1318   }
1319 
1320   if (patch != NULL) {
1321     patching_epilog(patch, patch_code, src, info);
1322   }
1323   if (info != NULL) add_debug_info_for_null_check(offset, info);
1324 }
1325 
1326 
1327 void LIR_Assembler::stack2reg(LIR_Opr src, LIR_Opr dest, BasicType type) {
1328   Address addr;
1329   if (src->is_single_word()) {
1330     addr = frame_map()->address_for_slot(src->single_stack_ix());
1331   } else if (src->is_double_word())  {
1332     addr = frame_map()->address_for_double_slot(src->double_stack_ix());
1333   }
1334 
1335   bool unaligned = (addr.disp() - STACK_BIAS) % 8 != 0;
1336   load(addr.base(), addr.disp(), dest, dest->type(), true /*wide*/, unaligned);
1337 }
1338 
1339 
1340 void LIR_Assembler::reg2stack(LIR_Opr from_reg, LIR_Opr dest, BasicType type, bool pop_fpu_stack) {
1341   Address addr;
1342   if (dest->is_single_word()) {
1343     addr = frame_map()->address_for_slot(dest->single_stack_ix());
1344   } else if (dest->is_double_word())  {
1345     addr = frame_map()->address_for_slot(dest->double_stack_ix());
1346   }
1347   bool unaligned = (addr.disp() - STACK_BIAS) % 8 != 0;
1348   store(from_reg, addr.base(), addr.disp(), from_reg->type(), true /*wide*/, unaligned);
1349 }
1350 
1351 
1352 void LIR_Assembler::reg2reg(LIR_Opr from_reg, LIR_Opr to_reg) {
1353   if (from_reg->is_float_kind() && to_reg->is_float_kind()) {
1354     if (from_reg->is_double_fpu()) {
1355       // double to double moves
1356       assert(to_reg->is_double_fpu(), "should match");
1357       __ fmov(FloatRegisterImpl::D, from_reg->as_double_reg(), to_reg->as_double_reg());
1358     } else {
1359       // float to float moves
1360       assert(to_reg->is_single_fpu(), "should match");
1361       __ fmov(FloatRegisterImpl::S, from_reg->as_float_reg(), to_reg->as_float_reg());
1362     }
1363   } else if (!from_reg->is_float_kind() && !to_reg->is_float_kind()) {
1364     if (from_reg->is_double_cpu()) {
1365 #ifdef _LP64
1366       __ mov(from_reg->as_pointer_register(), to_reg->as_pointer_register());
1367 #else
1368       assert(to_reg->is_double_cpu() &&
1369              from_reg->as_register_hi() != to_reg->as_register_lo() &&
1370              from_reg->as_register_lo() != to_reg->as_register_hi(),
1371              "should both be long and not overlap");
1372       // long to long moves
1373       __ mov(from_reg->as_register_hi(), to_reg->as_register_hi());
1374       __ mov(from_reg->as_register_lo(), to_reg->as_register_lo());
1375 #endif
1376 #ifdef _LP64
1377     } else if (to_reg->is_double_cpu()) {
1378       // int to int moves
1379       __ mov(from_reg->as_register(), to_reg->as_register_lo());
1380 #endif
1381     } else {
1382       // int to int moves
1383       __ mov(from_reg->as_register(), to_reg->as_register());
1384     }
1385   } else {
1386     ShouldNotReachHere();
1387   }
1388   if (to_reg->type() == T_OBJECT || to_reg->type() == T_ARRAY) {
1389     __ verify_oop(to_reg->as_register());
1390   }
1391 }
1392 
1393 
1394 void LIR_Assembler::reg2mem(LIR_Opr from_reg, LIR_Opr dest, BasicType type,
1395                             LIR_PatchCode patch_code, CodeEmitInfo* info, bool pop_fpu_stack,
1396                             bool wide, bool unaligned) {
1397   assert(type != T_METADATA, "store of metadata ptr not supported");
1398   LIR_Address* addr = dest->as_address_ptr();
1399 
1400   Register src = addr->base()->as_pointer_register();
1401   Register disp_reg = noreg;
1402   int disp_value = addr->disp();
1403   bool needs_patching = (patch_code != lir_patch_none);
1404 
1405   if (addr->base()->is_oop_register()) {
1406     __ verify_oop(src);
1407   }
1408 
1409   PatchingStub* patch = NULL;
1410   if (needs_patching) {
1411     patch = new PatchingStub(_masm, PatchingStub::access_field_id);
1412     assert(!from_reg->is_double_cpu() ||
1413            patch_code == lir_patch_none ||
1414            patch_code == lir_patch_normal, "patching doesn't match register");
1415   }
1416 
1417   if (addr->index()->is_illegal()) {
1418     if (!Assembler::is_simm13(disp_value) && (!unaligned || Assembler::is_simm13(disp_value + 4))) {
1419       if (needs_patching) {
1420         __ patchable_set(0, O7);
1421       } else {
1422         __ set(disp_value, O7);
1423       }
1424       disp_reg = O7;
1425     }
1426   } else if (unaligned || PatchALot) {
1427     __ add(src, addr->index()->as_register(), O7);
1428     src = O7;
1429   } else {
1430     disp_reg = addr->index()->as_pointer_register();
1431     assert(disp_value == 0, "can't handle 3 operand addresses");
1432   }
1433 
1434   // remember the offset of the store.  The patching_epilog must be done
1435   // before the call to add_debug_info_for_null_check, otherwise the PcDescs don't get
1436   // entered in increasing order.
1437   int offset;
1438 
1439   assert(disp_reg != noreg || Assembler::is_simm13(disp_value), "should have set this up");
1440   if (disp_reg == noreg) {
1441     offset = store(from_reg, src, disp_value, type, wide, unaligned);
1442   } else {
1443     assert(!unaligned, "can't handle this");
1444     offset = store(from_reg, src, disp_reg, type, wide);
1445   }
1446 
1447   if (patch != NULL) {
1448     patching_epilog(patch, patch_code, src, info);
1449   }
1450 
1451   if (info != NULL) add_debug_info_for_null_check(offset, info);
1452 }
1453 
1454 
1455 void LIR_Assembler::return_op(LIR_Opr result) {
1456   if (StackReservedPages > 0 && compilation()->has_reserved_stack_access()) {
1457     __ reserved_stack_check();
1458   }
1459   // the poll may need a register so just pick one that isn't the return register
1460 #if defined(TIERED) && !defined(_LP64)
1461   if (result->type_field() == LIR_OprDesc::long_type) {
1462     // Must move the result to G1
1463     // Must leave proper result in O0,O1 and G1 (TIERED only)
1464     __ sllx(I0, 32, G1);          // Shift bits into high G1
1465     __ srl (I1, 0, I1);           // Zero extend O1 (harmless?)
1466     __ or3 (I1, G1, G1);          // OR 64 bits into G1
1467 #ifdef ASSERT
1468     // mangle it so any problems will show up
1469     __ set(0xdeadbeef, I0);
1470     __ set(0xdeadbeef, I1);
1471 #endif
1472   }
1473 #endif // TIERED
1474   __ set((intptr_t)os::get_polling_page(), L0);
1475   __ relocate(relocInfo::poll_return_type);
1476   __ ld_ptr(L0, 0, G0);
1477   __ ret();
1478   __ delayed()->restore();
1479 }
1480 
1481 
1482 int LIR_Assembler::safepoint_poll(LIR_Opr tmp, CodeEmitInfo* info) {
1483   __ set((intptr_t)os::get_polling_page(), tmp->as_register());
1484   if (info != NULL) {
1485     add_debug_info_for_branch(info);
1486   }
1487   int offset = __ offset();
1488   __ relocate(relocInfo::poll_type);
1489   __ ld_ptr(tmp->as_register(), 0, G0);
1490   return offset;
1491 }
1492 
1493 
1494 void LIR_Assembler::emit_static_call_stub() {
1495   address call_pc = __ pc();
1496   address stub = __ start_a_stub(call_stub_size);
1497   if (stub == NULL) {
1498     bailout("static call stub overflow");
1499     return;
1500   }
1501 
1502   int start = __ offset();
1503   __ relocate(static_stub_Relocation::spec(call_pc));
1504 
1505   __ set_metadata(NULL, G5);
1506   // must be set to -1 at code generation time
1507   AddressLiteral addrlit(-1);
1508   __ jump_to(addrlit, G3);
1509   __ delayed()->nop();
1510 
1511   assert(__ offset() - start <= call_stub_size, "stub too big");
1512   __ end_a_stub();
1513 }
1514 
1515 
1516 void LIR_Assembler::comp_op(LIR_Condition condition, LIR_Opr opr1, LIR_Opr opr2, LIR_Op2* op) {
1517   if (opr1->is_single_fpu()) {
1518     __ fcmp(FloatRegisterImpl::S, Assembler::fcc0, opr1->as_float_reg(), opr2->as_float_reg());
1519   } else if (opr1->is_double_fpu()) {
1520     __ fcmp(FloatRegisterImpl::D, Assembler::fcc0, opr1->as_double_reg(), opr2->as_double_reg());
1521   } else if (opr1->is_single_cpu()) {
1522     if (opr2->is_constant()) {
1523       switch (opr2->as_constant_ptr()->type()) {
1524         case T_INT:
1525           { jint con = opr2->as_constant_ptr()->as_jint();
1526             if (Assembler::is_simm13(con)) {
1527               __ cmp(opr1->as_register(), con);
1528             } else {
1529               __ set(con, O7);
1530               __ cmp(opr1->as_register(), O7);
1531             }
1532           }
1533           break;
1534 
1535         case T_OBJECT:
1536           // there are only equal/notequal comparisions on objects
1537           { jobject con = opr2->as_constant_ptr()->as_jobject();
1538             if (con == NULL) {
1539               __ cmp(opr1->as_register(), 0);
1540             } else {
1541               jobject2reg(con, O7);
1542               __ cmp(opr1->as_register(), O7);
1543             }
1544           }
1545           break;
1546 
1547         default:
1548           ShouldNotReachHere();
1549           break;
1550       }
1551     } else {
1552       if (opr2->is_address()) {
1553         LIR_Address * addr = opr2->as_address_ptr();
1554         BasicType type = addr->type();
1555         if ( type == T_OBJECT ) __ ld_ptr(as_Address(addr), O7);
1556         else                    __ ld(as_Address(addr), O7);
1557         __ cmp(opr1->as_register(), O7);
1558       } else {
1559         __ cmp(opr1->as_register(), opr2->as_register());
1560       }
1561     }
1562   } else if (opr1->is_double_cpu()) {
1563     Register xlo = opr1->as_register_lo();
1564     Register xhi = opr1->as_register_hi();
1565     if (opr2->is_constant() && opr2->as_jlong() == 0) {
1566       assert(condition == lir_cond_equal || condition == lir_cond_notEqual, "only handles these cases");
1567 #ifdef _LP64
1568       __ orcc(xhi, G0, G0);
1569 #else
1570       __ orcc(xhi, xlo, G0);
1571 #endif
1572     } else if (opr2->is_register()) {
1573       Register ylo = opr2->as_register_lo();
1574       Register yhi = opr2->as_register_hi();
1575 #ifdef _LP64
1576       __ cmp(xlo, ylo);
1577 #else
1578       __ subcc(xlo, ylo, xlo);
1579       __ subccc(xhi, yhi, xhi);
1580       if (condition == lir_cond_equal || condition == lir_cond_notEqual) {
1581         __ orcc(xhi, xlo, G0);
1582       }
1583 #endif
1584     } else {
1585       ShouldNotReachHere();
1586     }
1587   } else if (opr1->is_address()) {
1588     LIR_Address * addr = opr1->as_address_ptr();
1589     BasicType type = addr->type();
1590     assert (opr2->is_constant(), "Checking");
1591     if ( type == T_OBJECT ) __ ld_ptr(as_Address(addr), O7);
1592     else                    __ ld(as_Address(addr), O7);
1593     __ cmp(O7, opr2->as_constant_ptr()->as_jint());
1594   } else {
1595     ShouldNotReachHere();
1596   }
1597 }
1598 
1599 
1600 void LIR_Assembler::comp_fl2i(LIR_Code code, LIR_Opr left, LIR_Opr right, LIR_Opr dst, LIR_Op2* op){
1601   if (code == lir_cmp_fd2i || code == lir_ucmp_fd2i) {
1602     bool is_unordered_less = (code == lir_ucmp_fd2i);
1603     if (left->is_single_fpu()) {
1604       __ float_cmp(true, is_unordered_less ? -1 : 1, left->as_float_reg(), right->as_float_reg(), dst->as_register());
1605     } else if (left->is_double_fpu()) {
1606       __ float_cmp(false, is_unordered_less ? -1 : 1, left->as_double_reg(), right->as_double_reg(), dst->as_register());
1607     } else {
1608       ShouldNotReachHere();
1609     }
1610   } else if (code == lir_cmp_l2i) {
1611 #ifdef _LP64
1612     __ lcmp(left->as_register_lo(), right->as_register_lo(), dst->as_register());
1613 #else
1614     __ lcmp(left->as_register_hi(),  left->as_register_lo(),
1615             right->as_register_hi(), right->as_register_lo(),
1616             dst->as_register());
1617 #endif
1618   } else {
1619     ShouldNotReachHere();
1620   }
1621 }
1622 
1623 
1624 void LIR_Assembler::cmove(LIR_Condition condition, LIR_Opr opr1, LIR_Opr opr2, LIR_Opr result, BasicType type) {
1625   Assembler::Condition acond;
1626   switch (condition) {
1627     case lir_cond_equal:        acond = Assembler::equal;        break;
1628     case lir_cond_notEqual:     acond = Assembler::notEqual;     break;
1629     case lir_cond_less:         acond = Assembler::less;         break;
1630     case lir_cond_lessEqual:    acond = Assembler::lessEqual;    break;
1631     case lir_cond_greaterEqual: acond = Assembler::greaterEqual; break;
1632     case lir_cond_greater:      acond = Assembler::greater;      break;
1633     case lir_cond_aboveEqual:   acond = Assembler::greaterEqualUnsigned;      break;
1634     case lir_cond_belowEqual:   acond = Assembler::lessEqualUnsigned;      break;
1635     default:                         ShouldNotReachHere();
1636   };
1637 
1638   if (opr1->is_constant() && opr1->type() == T_INT) {
1639     Register dest = result->as_register();
1640     // load up first part of constant before branch
1641     // and do the rest in the delay slot.
1642     if (!Assembler::is_simm13(opr1->as_jint())) {
1643       __ sethi(opr1->as_jint(), dest);
1644     }
1645   } else if (opr1->is_constant()) {
1646     const2reg(opr1, result, lir_patch_none, NULL);
1647   } else if (opr1->is_register()) {
1648     reg2reg(opr1, result);
1649   } else if (opr1->is_stack()) {
1650     stack2reg(opr1, result, result->type());
1651   } else {
1652     ShouldNotReachHere();
1653   }
1654   Label skip;
1655 #ifdef _LP64
1656     if  (type == T_INT) {
1657       __ br(acond, false, Assembler::pt, skip);
1658     } else
1659 #endif
1660       __ brx(acond, false, Assembler::pt, skip); // checks icc on 32bit and xcc on 64bit
1661   if (opr1->is_constant() && opr1->type() == T_INT) {
1662     Register dest = result->as_register();
1663     if (Assembler::is_simm13(opr1->as_jint())) {
1664       __ delayed()->or3(G0, opr1->as_jint(), dest);
1665     } else {
1666       // the sethi has been done above, so just put in the low 10 bits
1667       __ delayed()->or3(dest, opr1->as_jint() & 0x3ff, dest);
1668     }
1669   } else {
1670     // can't do anything useful in the delay slot
1671     __ delayed()->nop();
1672   }
1673   if (opr2->is_constant()) {
1674     const2reg(opr2, result, lir_patch_none, NULL);
1675   } else if (opr2->is_register()) {
1676     reg2reg(opr2, result);
1677   } else if (opr2->is_stack()) {
1678     stack2reg(opr2, result, result->type());
1679   } else {
1680     ShouldNotReachHere();
1681   }
1682   __ bind(skip);
1683 }
1684 
1685 
1686 void LIR_Assembler::arith_op(LIR_Code code, LIR_Opr left, LIR_Opr right, LIR_Opr dest, CodeEmitInfo* info, bool pop_fpu_stack) {
1687   assert(info == NULL, "unused on this code path");
1688   assert(left->is_register(), "wrong items state");
1689   assert(dest->is_register(), "wrong items state");
1690 
1691   if (right->is_register()) {
1692     if (dest->is_float_kind()) {
1693 
1694       FloatRegister lreg, rreg, res;
1695       FloatRegisterImpl::Width w;
1696       if (right->is_single_fpu()) {
1697         w = FloatRegisterImpl::S;
1698         lreg = left->as_float_reg();
1699         rreg = right->as_float_reg();
1700         res  = dest->as_float_reg();
1701       } else {
1702         w = FloatRegisterImpl::D;
1703         lreg = left->as_double_reg();
1704         rreg = right->as_double_reg();
1705         res  = dest->as_double_reg();
1706       }
1707 
1708       switch (code) {
1709         case lir_add: __ fadd(w, lreg, rreg, res); break;
1710         case lir_sub: __ fsub(w, lreg, rreg, res); break;
1711         case lir_mul: // fall through
1712         case lir_mul_strictfp: __ fmul(w, lreg, rreg, res); break;
1713         case lir_div: // fall through
1714         case lir_div_strictfp: __ fdiv(w, lreg, rreg, res); break;
1715         default: ShouldNotReachHere();
1716       }
1717 
1718     } else if (dest->is_double_cpu()) {
1719 #ifdef _LP64
1720       Register dst_lo = dest->as_register_lo();
1721       Register op1_lo = left->as_pointer_register();
1722       Register op2_lo = right->as_pointer_register();
1723 
1724       switch (code) {
1725         case lir_add:
1726           __ add(op1_lo, op2_lo, dst_lo);
1727           break;
1728 
1729         case lir_sub:
1730           __ sub(op1_lo, op2_lo, dst_lo);
1731           break;
1732 
1733         default: ShouldNotReachHere();
1734       }
1735 #else
1736       Register op1_lo = left->as_register_lo();
1737       Register op1_hi = left->as_register_hi();
1738       Register op2_lo = right->as_register_lo();
1739       Register op2_hi = right->as_register_hi();
1740       Register dst_lo = dest->as_register_lo();
1741       Register dst_hi = dest->as_register_hi();
1742 
1743       switch (code) {
1744         case lir_add:
1745           __ addcc(op1_lo, op2_lo, dst_lo);
1746           __ addc (op1_hi, op2_hi, dst_hi);
1747           break;
1748 
1749         case lir_sub:
1750           __ subcc(op1_lo, op2_lo, dst_lo);
1751           __ subc (op1_hi, op2_hi, dst_hi);
1752           break;
1753 
1754         default: ShouldNotReachHere();
1755       }
1756 #endif
1757     } else {
1758       assert (right->is_single_cpu(), "Just Checking");
1759 
1760       Register lreg = left->as_register();
1761       Register res  = dest->as_register();
1762       Register rreg = right->as_register();
1763       switch (code) {
1764         case lir_add:  __ add  (lreg, rreg, res); break;
1765         case lir_sub:  __ sub  (lreg, rreg, res); break;
1766         case lir_mul:  __ mulx (lreg, rreg, res); break;
1767         default: ShouldNotReachHere();
1768       }
1769     }
1770   } else {
1771     assert (right->is_constant(), "must be constant");
1772 
1773     if (dest->is_single_cpu()) {
1774       Register lreg = left->as_register();
1775       Register res  = dest->as_register();
1776       int    simm13 = right->as_constant_ptr()->as_jint();
1777 
1778       switch (code) {
1779         case lir_add:  __ add  (lreg, simm13, res); break;
1780         case lir_sub:  __ sub  (lreg, simm13, res); break;
1781         case lir_mul:  __ mulx (lreg, simm13, res); break;
1782         default: ShouldNotReachHere();
1783       }
1784     } else {
1785       Register lreg = left->as_pointer_register();
1786       Register res  = dest->as_register_lo();
1787       long con = right->as_constant_ptr()->as_jlong();
1788       assert(Assembler::is_simm13(con), "must be simm13");
1789 
1790       switch (code) {
1791         case lir_add:  __ add  (lreg, (int)con, res); break;
1792         case lir_sub:  __ sub  (lreg, (int)con, res); break;
1793         case lir_mul:  __ mulx (lreg, (int)con, res); break;
1794         default: ShouldNotReachHere();
1795       }
1796     }
1797   }
1798 }
1799 
1800 
1801 void LIR_Assembler::fpop() {
1802   // do nothing
1803 }
1804 
1805 
1806 void LIR_Assembler::intrinsic_op(LIR_Code code, LIR_Opr value, LIR_Opr thread, LIR_Opr dest, LIR_Op* op) {
1807   switch (code) {
1808     case lir_tan: {
1809       assert(thread->is_valid(), "preserve the thread object for performance reasons");
1810       assert(dest->as_double_reg() == F0, "the result will be in f0/f1");
1811       break;
1812     }
1813     case lir_sqrt: {
1814       assert(!thread->is_valid(), "there is no need for a thread_reg for dsqrt");
1815       FloatRegister src_reg = value->as_double_reg();
1816       FloatRegister dst_reg = dest->as_double_reg();
1817       __ fsqrt(FloatRegisterImpl::D, src_reg, dst_reg);
1818       break;
1819     }
1820     case lir_abs: {
1821       assert(!thread->is_valid(), "there is no need for a thread_reg for fabs");
1822       FloatRegister src_reg = value->as_double_reg();
1823       FloatRegister dst_reg = dest->as_double_reg();
1824       __ fabs(FloatRegisterImpl::D, src_reg, dst_reg);
1825       break;
1826     }
1827     default: {
1828       ShouldNotReachHere();
1829       break;
1830     }
1831   }
1832 }
1833 
1834 
1835 void LIR_Assembler::logic_op(LIR_Code code, LIR_Opr left, LIR_Opr right, LIR_Opr dest) {
1836   if (right->is_constant()) {
1837     if (dest->is_single_cpu()) {
1838       int simm13 = right->as_constant_ptr()->as_jint();
1839       switch (code) {
1840         case lir_logic_and:   __ and3 (left->as_register(), simm13, dest->as_register()); break;
1841         case lir_logic_or:    __ or3  (left->as_register(), simm13, dest->as_register()); break;
1842         case lir_logic_xor:   __ xor3 (left->as_register(), simm13, dest->as_register()); break;
1843         default: ShouldNotReachHere();
1844       }
1845     } else {
1846       long c = right->as_constant_ptr()->as_jlong();
1847       assert(c == (int)c && Assembler::is_simm13(c), "out of range");
1848       int simm13 = (int)c;
1849       switch (code) {
1850         case lir_logic_and:
1851 #ifndef _LP64
1852           __ and3 (left->as_register_hi(), 0,      dest->as_register_hi());
1853 #endif
1854           __ and3 (left->as_register_lo(), simm13, dest->as_register_lo());
1855           break;
1856 
1857         case lir_logic_or:
1858 #ifndef _LP64
1859           __ or3 (left->as_register_hi(), 0,      dest->as_register_hi());
1860 #endif
1861           __ or3 (left->as_register_lo(), simm13, dest->as_register_lo());
1862           break;
1863 
1864         case lir_logic_xor:
1865 #ifndef _LP64
1866           __ xor3 (left->as_register_hi(), 0,      dest->as_register_hi());
1867 #endif
1868           __ xor3 (left->as_register_lo(), simm13, dest->as_register_lo());
1869           break;
1870 
1871         default: ShouldNotReachHere();
1872       }
1873     }
1874   } else {
1875     assert(right->is_register(), "right should be in register");
1876 
1877     if (dest->is_single_cpu()) {
1878       switch (code) {
1879         case lir_logic_and:   __ and3 (left->as_register(), right->as_register(), dest->as_register()); break;
1880         case lir_logic_or:    __ or3  (left->as_register(), right->as_register(), dest->as_register()); break;
1881         case lir_logic_xor:   __ xor3 (left->as_register(), right->as_register(), dest->as_register()); break;
1882         default: ShouldNotReachHere();
1883       }
1884     } else {
1885 #ifdef _LP64
1886       Register l = (left->is_single_cpu() && left->is_oop_register()) ? left->as_register() :
1887                                                                         left->as_register_lo();
1888       Register r = (right->is_single_cpu() && right->is_oop_register()) ? right->as_register() :
1889                                                                           right->as_register_lo();
1890 
1891       switch (code) {
1892         case lir_logic_and: __ and3 (l, r, dest->as_register_lo()); break;
1893         case lir_logic_or:  __ or3  (l, r, dest->as_register_lo()); break;
1894         case lir_logic_xor: __ xor3 (l, r, dest->as_register_lo()); break;
1895         default: ShouldNotReachHere();
1896       }
1897 #else
1898       switch (code) {
1899         case lir_logic_and:
1900           __ and3 (left->as_register_hi(), right->as_register_hi(), dest->as_register_hi());
1901           __ and3 (left->as_register_lo(), right->as_register_lo(), dest->as_register_lo());
1902           break;
1903 
1904         case lir_logic_or:
1905           __ or3 (left->as_register_hi(), right->as_register_hi(), dest->as_register_hi());
1906           __ or3 (left->as_register_lo(), right->as_register_lo(), dest->as_register_lo());
1907           break;
1908 
1909         case lir_logic_xor:
1910           __ xor3 (left->as_register_hi(), right->as_register_hi(), dest->as_register_hi());
1911           __ xor3 (left->as_register_lo(), right->as_register_lo(), dest->as_register_lo());
1912           break;
1913 
1914         default: ShouldNotReachHere();
1915       }
1916 #endif
1917     }
1918   }
1919 }
1920 
1921 
1922 int LIR_Assembler::shift_amount(BasicType t) {
1923   int elem_size = type2aelembytes(t);
1924   switch (elem_size) {
1925     case 1 : return 0;
1926     case 2 : return 1;
1927     case 4 : return 2;
1928     case 8 : return 3;
1929   }
1930   ShouldNotReachHere();
1931   return -1;
1932 }
1933 
1934 
1935 void LIR_Assembler::throw_op(LIR_Opr exceptionPC, LIR_Opr exceptionOop, CodeEmitInfo* info) {
1936   assert(exceptionOop->as_register() == Oexception, "should match");
1937   assert(exceptionPC->as_register() == Oissuing_pc, "should match");
1938 
1939   info->add_register_oop(exceptionOop);
1940 
1941   // reuse the debug info from the safepoint poll for the throw op itself
1942   address pc_for_athrow  = __ pc();
1943   int pc_for_athrow_offset = __ offset();
1944   RelocationHolder rspec = internal_word_Relocation::spec(pc_for_athrow);
1945   __ set(pc_for_athrow, Oissuing_pc, rspec);
1946   add_call_info(pc_for_athrow_offset, info); // for exception handler
1947 
1948   __ call(Runtime1::entry_for(Runtime1::handle_exception_id), relocInfo::runtime_call_type);
1949   __ delayed()->nop();
1950 }
1951 
1952 
1953 void LIR_Assembler::unwind_op(LIR_Opr exceptionOop) {
1954   assert(exceptionOop->as_register() == Oexception, "should match");
1955 
1956   __ br(Assembler::always, false, Assembler::pt, _unwind_handler_entry);
1957   __ delayed()->nop();
1958 }
1959 
1960 void LIR_Assembler::emit_arraycopy(LIR_OpArrayCopy* op) {
1961   Register src = op->src()->as_register();
1962   Register dst = op->dst()->as_register();
1963   Register src_pos = op->src_pos()->as_register();
1964   Register dst_pos = op->dst_pos()->as_register();
1965   Register length  = op->length()->as_register();
1966   Register tmp = op->tmp()->as_register();
1967   Register tmp2 = O7;
1968 
1969   int flags = op->flags();
1970   ciArrayKlass* default_type = op->expected_type();
1971   BasicType basic_type = default_type != NULL ? default_type->element_type()->basic_type() : T_ILLEGAL;
1972   if (basic_type == T_ARRAY) basic_type = T_OBJECT;
1973 
1974 #ifdef _LP64
1975   // higher 32bits must be null
1976   __ sra(dst_pos, 0, dst_pos);
1977   __ sra(src_pos, 0, src_pos);
1978   __ sra(length, 0, length);
1979 #endif
1980 
1981   // set up the arraycopy stub information
1982   ArrayCopyStub* stub = op->stub();
1983 
1984   // always do stub if no type information is available.  it's ok if
1985   // the known type isn't loaded since the code sanity checks
1986   // in debug mode and the type isn't required when we know the exact type
1987   // also check that the type is an array type.
1988   if (op->expected_type() == NULL) {
1989     __ mov(src,     O0);
1990     __ mov(src_pos, O1);
1991     __ mov(dst,     O2);
1992     __ mov(dst_pos, O3);
1993     __ mov(length,  O4);
1994     address copyfunc_addr = StubRoutines::generic_arraycopy();
1995 
1996     if (copyfunc_addr == NULL) { // Use C version if stub was not generated
1997       __ call_VM_leaf(tmp, CAST_FROM_FN_PTR(address, Runtime1::arraycopy));
1998     } else {
1999 #ifndef PRODUCT
2000       if (PrintC1Statistics) {
2001         address counter = (address)&Runtime1::_generic_arraycopystub_cnt;
2002         __ inc_counter(counter, G1, G3);
2003       }
2004 #endif
2005       __ call_VM_leaf(tmp, copyfunc_addr);
2006     }
2007 
2008     if (copyfunc_addr != NULL) {
2009       __ xor3(O0, -1, tmp);
2010       __ sub(length, tmp, length);
2011       __ add(src_pos, tmp, src_pos);
2012       __ cmp_zero_and_br(Assembler::less, O0, *stub->entry());
2013       __ delayed()->add(dst_pos, tmp, dst_pos);
2014     } else {
2015       __ cmp_zero_and_br(Assembler::less, O0, *stub->entry());
2016       __ delayed()->nop();
2017     }
2018     __ bind(*stub->continuation());
2019     return;
2020   }
2021 
2022   assert(default_type != NULL && default_type->is_array_klass(), "must be true at this point");
2023 
2024   // make sure src and dst are non-null and load array length
2025   if (flags & LIR_OpArrayCopy::src_null_check) {
2026     __ tst(src);
2027     __ brx(Assembler::equal, false, Assembler::pn, *stub->entry());
2028     __ delayed()->nop();
2029   }
2030 
2031   if (flags & LIR_OpArrayCopy::dst_null_check) {
2032     __ tst(dst);
2033     __ brx(Assembler::equal, false, Assembler::pn, *stub->entry());
2034     __ delayed()->nop();
2035   }
2036 
2037   if (flags & LIR_OpArrayCopy::src_pos_positive_check) {
2038     // test src_pos register
2039     __ cmp_zero_and_br(Assembler::less, src_pos, *stub->entry());
2040     __ delayed()->nop();
2041   }
2042 
2043   if (flags & LIR_OpArrayCopy::dst_pos_positive_check) {
2044     // test dst_pos register
2045     __ cmp_zero_and_br(Assembler::less, dst_pos, *stub->entry());
2046     __ delayed()->nop();
2047   }
2048 
2049   if (flags & LIR_OpArrayCopy::length_positive_check) {
2050     // make sure length isn't negative
2051     __ cmp_zero_and_br(Assembler::less, length, *stub->entry());
2052     __ delayed()->nop();
2053   }
2054 
2055   if (flags & LIR_OpArrayCopy::src_range_check) {
2056     __ ld(src, arrayOopDesc::length_offset_in_bytes(), tmp2);
2057     __ add(length, src_pos, tmp);
2058     __ cmp(tmp2, tmp);
2059     __ br(Assembler::carrySet, false, Assembler::pn, *stub->entry());
2060     __ delayed()->nop();
2061   }
2062 
2063   if (flags & LIR_OpArrayCopy::dst_range_check) {
2064     __ ld(dst, arrayOopDesc::length_offset_in_bytes(), tmp2);
2065     __ add(length, dst_pos, tmp);
2066     __ cmp(tmp2, tmp);
2067     __ br(Assembler::carrySet, false, Assembler::pn, *stub->entry());
2068     __ delayed()->nop();
2069   }
2070 
2071   int shift = shift_amount(basic_type);
2072 
2073   if (flags & LIR_OpArrayCopy::type_check) {
2074     // We don't know the array types are compatible
2075     if (basic_type != T_OBJECT) {
2076       // Simple test for basic type arrays
2077       if (UseCompressedClassPointers) {
2078         // We don't need decode because we just need to compare
2079         __ lduw(src, oopDesc::klass_offset_in_bytes(), tmp);
2080         __ lduw(dst, oopDesc::klass_offset_in_bytes(), tmp2);
2081         __ cmp(tmp, tmp2);
2082         __ br(Assembler::notEqual, false, Assembler::pt, *stub->entry());
2083       } else {
2084         __ ld_ptr(src, oopDesc::klass_offset_in_bytes(), tmp);
2085         __ ld_ptr(dst, oopDesc::klass_offset_in_bytes(), tmp2);
2086         __ cmp(tmp, tmp2);
2087         __ brx(Assembler::notEqual, false, Assembler::pt, *stub->entry());
2088       }
2089       __ delayed()->nop();
2090     } else {
2091       // For object arrays, if src is a sub class of dst then we can
2092       // safely do the copy.
2093       address copyfunc_addr = StubRoutines::checkcast_arraycopy();
2094 
2095       Label cont, slow;
2096       assert_different_registers(tmp, tmp2, G3, G1);
2097 
2098       __ load_klass(src, G3);
2099       __ load_klass(dst, G1);
2100 
2101       __ check_klass_subtype_fast_path(G3, G1, tmp, tmp2, &cont, copyfunc_addr == NULL ? stub->entry() : &slow, NULL);
2102 
2103       __ call(Runtime1::entry_for(Runtime1::slow_subtype_check_id), relocInfo::runtime_call_type);
2104       __ delayed()->nop();
2105 
2106       __ cmp(G3, 0);
2107       if (copyfunc_addr != NULL) { // use stub if available
2108         // src is not a sub class of dst so we have to do a
2109         // per-element check.
2110         __ br(Assembler::notEqual, false, Assembler::pt, cont);
2111         __ delayed()->nop();
2112 
2113         __ bind(slow);
2114 
2115         int mask = LIR_OpArrayCopy::src_objarray|LIR_OpArrayCopy::dst_objarray;
2116         if ((flags & mask) != mask) {
2117           // Check that at least both of them object arrays.
2118           assert(flags & mask, "one of the two should be known to be an object array");
2119 
2120           if (!(flags & LIR_OpArrayCopy::src_objarray)) {
2121             __ load_klass(src, tmp);
2122           } else if (!(flags & LIR_OpArrayCopy::dst_objarray)) {
2123             __ load_klass(dst, tmp);
2124           }
2125           int lh_offset = in_bytes(Klass::layout_helper_offset());
2126 
2127           __ lduw(tmp, lh_offset, tmp2);
2128 
2129           jint objArray_lh = Klass::array_layout_helper(T_OBJECT);
2130           __ set(objArray_lh, tmp);
2131           __ cmp(tmp, tmp2);
2132           __ br(Assembler::notEqual, false, Assembler::pt,  *stub->entry());
2133           __ delayed()->nop();
2134         }
2135 
2136         Register src_ptr = O0;
2137         Register dst_ptr = O1;
2138         Register len     = O2;
2139         Register chk_off = O3;
2140         Register super_k = O4;
2141 
2142         __ add(src, arrayOopDesc::base_offset_in_bytes(basic_type), src_ptr);
2143         if (shift == 0) {
2144           __ add(src_ptr, src_pos, src_ptr);
2145         } else {
2146           __ sll(src_pos, shift, tmp);
2147           __ add(src_ptr, tmp, src_ptr);
2148         }
2149 
2150         __ add(dst, arrayOopDesc::base_offset_in_bytes(basic_type), dst_ptr);
2151         if (shift == 0) {
2152           __ add(dst_ptr, dst_pos, dst_ptr);
2153         } else {
2154           __ sll(dst_pos, shift, tmp);
2155           __ add(dst_ptr, tmp, dst_ptr);
2156         }
2157         __ mov(length, len);
2158         __ load_klass(dst, tmp);
2159 
2160         int ek_offset = in_bytes(ObjArrayKlass::element_klass_offset());
2161         __ ld_ptr(tmp, ek_offset, super_k);
2162 
2163         int sco_offset = in_bytes(Klass::super_check_offset_offset());
2164         __ lduw(super_k, sco_offset, chk_off);
2165 
2166         __ call_VM_leaf(tmp, copyfunc_addr);
2167 
2168 #ifndef PRODUCT
2169         if (PrintC1Statistics) {
2170           Label failed;
2171           __ br_notnull_short(O0, Assembler::pn, failed);
2172           __ inc_counter((address)&Runtime1::_arraycopy_checkcast_cnt, G1, G3);
2173           __ bind(failed);
2174         }
2175 #endif
2176 
2177         __ br_null(O0, false, Assembler::pt,  *stub->continuation());
2178         __ delayed()->xor3(O0, -1, tmp);
2179 
2180 #ifndef PRODUCT
2181         if (PrintC1Statistics) {
2182           __ inc_counter((address)&Runtime1::_arraycopy_checkcast_attempt_cnt, G1, G3);
2183         }
2184 #endif
2185 
2186         __ sub(length, tmp, length);
2187         __ add(src_pos, tmp, src_pos);
2188         __ br(Assembler::always, false, Assembler::pt, *stub->entry());
2189         __ delayed()->add(dst_pos, tmp, dst_pos);
2190 
2191         __ bind(cont);
2192       } else {
2193         __ br(Assembler::equal, false, Assembler::pn, *stub->entry());
2194         __ delayed()->nop();
2195         __ bind(cont);
2196       }
2197     }
2198   }
2199 
2200 #ifdef ASSERT
2201   if (basic_type != T_OBJECT || !(flags & LIR_OpArrayCopy::type_check)) {
2202     // Sanity check the known type with the incoming class.  For the
2203     // primitive case the types must match exactly with src.klass and
2204     // dst.klass each exactly matching the default type.  For the
2205     // object array case, if no type check is needed then either the
2206     // dst type is exactly the expected type and the src type is a
2207     // subtype which we can't check or src is the same array as dst
2208     // but not necessarily exactly of type default_type.
2209     Label known_ok, halt;
2210     metadata2reg(op->expected_type()->constant_encoding(), tmp);
2211     if (UseCompressedClassPointers) {
2212       // tmp holds the default type. It currently comes uncompressed after the
2213       // load of a constant, so encode it.
2214       __ encode_klass_not_null(tmp);
2215       // load the raw value of the dst klass, since we will be comparing
2216       // uncompressed values directly.
2217       __ lduw(dst, oopDesc::klass_offset_in_bytes(), tmp2);
2218       if (basic_type != T_OBJECT) {
2219         __ cmp(tmp, tmp2);
2220         __ br(Assembler::notEqual, false, Assembler::pn, halt);
2221         // load the raw value of the src klass.
2222         __ delayed()->lduw(src, oopDesc::klass_offset_in_bytes(), tmp2);
2223         __ cmp_and_br_short(tmp, tmp2, Assembler::equal, Assembler::pn, known_ok);
2224       } else {
2225         __ cmp(tmp, tmp2);
2226         __ br(Assembler::equal, false, Assembler::pn, known_ok);
2227         __ delayed()->cmp(src, dst);
2228         __ brx(Assembler::equal, false, Assembler::pn, known_ok);
2229         __ delayed()->nop();
2230       }
2231     } else {
2232       __ ld_ptr(dst, oopDesc::klass_offset_in_bytes(), tmp2);
2233       if (basic_type != T_OBJECT) {
2234         __ cmp(tmp, tmp2);
2235         __ brx(Assembler::notEqual, false, Assembler::pn, halt);
2236         __ delayed()->ld_ptr(src, oopDesc::klass_offset_in_bytes(), tmp2);
2237         __ cmp_and_brx_short(tmp, tmp2, Assembler::equal, Assembler::pn, known_ok);
2238       } else {
2239         __ cmp(tmp, tmp2);
2240         __ brx(Assembler::equal, false, Assembler::pn, known_ok);
2241         __ delayed()->cmp(src, dst);
2242         __ brx(Assembler::equal, false, Assembler::pn, known_ok);
2243         __ delayed()->nop();
2244       }
2245     }
2246     __ bind(halt);
2247     __ stop("incorrect type information in arraycopy");
2248     __ bind(known_ok);
2249   }
2250 #endif
2251 
2252 #ifndef PRODUCT
2253   if (PrintC1Statistics) {
2254     address counter = Runtime1::arraycopy_count_address(basic_type);
2255     __ inc_counter(counter, G1, G3);
2256   }
2257 #endif
2258 
2259   Register src_ptr = O0;
2260   Register dst_ptr = O1;
2261   Register len     = O2;
2262 
2263   __ add(src, arrayOopDesc::base_offset_in_bytes(basic_type), src_ptr);
2264   if (shift == 0) {
2265     __ add(src_ptr, src_pos, src_ptr);
2266   } else {
2267     __ sll(src_pos, shift, tmp);
2268     __ add(src_ptr, tmp, src_ptr);
2269   }
2270 
2271   __ add(dst, arrayOopDesc::base_offset_in_bytes(basic_type), dst_ptr);
2272   if (shift == 0) {
2273     __ add(dst_ptr, dst_pos, dst_ptr);
2274   } else {
2275     __ sll(dst_pos, shift, tmp);
2276     __ add(dst_ptr, tmp, dst_ptr);
2277   }
2278 
2279   bool disjoint = (flags & LIR_OpArrayCopy::overlapping) == 0;
2280   bool aligned = (flags & LIR_OpArrayCopy::unaligned) == 0;
2281   const char *name;
2282   address entry = StubRoutines::select_arraycopy_function(basic_type, aligned, disjoint, name, false);
2283 
2284   // arraycopy stubs takes a length in number of elements, so don't scale it.
2285   __ mov(length, len);
2286   __ call_VM_leaf(tmp, entry);
2287 
2288   __ bind(*stub->continuation());
2289 }
2290 
2291 
2292 void LIR_Assembler::shift_op(LIR_Code code, LIR_Opr left, LIR_Opr count, LIR_Opr dest, LIR_Opr tmp) {
2293   if (dest->is_single_cpu()) {
2294 #ifdef _LP64
2295     if (left->type() == T_OBJECT) {
2296       switch (code) {
2297         case lir_shl:  __ sllx  (left->as_register(), count->as_register(), dest->as_register()); break;
2298         case lir_shr:  __ srax  (left->as_register(), count->as_register(), dest->as_register()); break;
2299         case lir_ushr: __ srl   (left->as_register(), count->as_register(), dest->as_register()); break;
2300         default: ShouldNotReachHere();
2301       }
2302     } else
2303 #endif
2304       switch (code) {
2305         case lir_shl:  __ sll   (left->as_register(), count->as_register(), dest->as_register()); break;
2306         case lir_shr:  __ sra   (left->as_register(), count->as_register(), dest->as_register()); break;
2307         case lir_ushr: __ srl   (left->as_register(), count->as_register(), dest->as_register()); break;
2308         default: ShouldNotReachHere();
2309       }
2310   } else {
2311 #ifdef _LP64
2312     switch (code) {
2313       case lir_shl:  __ sllx  (left->as_register_lo(), count->as_register(), dest->as_register_lo()); break;
2314       case lir_shr:  __ srax  (left->as_register_lo(), count->as_register(), dest->as_register_lo()); break;
2315       case lir_ushr: __ srlx  (left->as_register_lo(), count->as_register(), dest->as_register_lo()); break;
2316       default: ShouldNotReachHere();
2317     }
2318 #else
2319     switch (code) {
2320       case lir_shl:  __ lshl  (left->as_register_hi(), left->as_register_lo(), count->as_register(), dest->as_register_hi(), dest->as_register_lo(), G3_scratch); break;
2321       case lir_shr:  __ lshr  (left->as_register_hi(), left->as_register_lo(), count->as_register(), dest->as_register_hi(), dest->as_register_lo(), G3_scratch); break;
2322       case lir_ushr: __ lushr (left->as_register_hi(), left->as_register_lo(), count->as_register(), dest->as_register_hi(), dest->as_register_lo(), G3_scratch); break;
2323       default: ShouldNotReachHere();
2324     }
2325 #endif
2326   }
2327 }
2328 
2329 
2330 void LIR_Assembler::shift_op(LIR_Code code, LIR_Opr left, jint count, LIR_Opr dest) {
2331 #ifdef _LP64
2332   if (left->type() == T_OBJECT) {
2333     count = count & 63;  // shouldn't shift by more than sizeof(intptr_t)
2334     Register l = left->as_register();
2335     Register d = dest->as_register_lo();
2336     switch (code) {
2337       case lir_shl:  __ sllx  (l, count, d); break;
2338       case lir_shr:  __ srax  (l, count, d); break;
2339       case lir_ushr: __ srlx  (l, count, d); break;
2340       default: ShouldNotReachHere();
2341     }
2342     return;
2343   }
2344 #endif
2345 
2346   if (dest->is_single_cpu()) {
2347     count = count & 0x1F; // Java spec
2348     switch (code) {
2349       case lir_shl:  __ sll   (left->as_register(), count, dest->as_register()); break;
2350       case lir_shr:  __ sra   (left->as_register(), count, dest->as_register()); break;
2351       case lir_ushr: __ srl   (left->as_register(), count, dest->as_register()); break;
2352       default: ShouldNotReachHere();
2353     }
2354   } else if (dest->is_double_cpu()) {
2355     count = count & 63; // Java spec
2356     switch (code) {
2357       case lir_shl:  __ sllx  (left->as_pointer_register(), count, dest->as_pointer_register()); break;
2358       case lir_shr:  __ srax  (left->as_pointer_register(), count, dest->as_pointer_register()); break;
2359       case lir_ushr: __ srlx  (left->as_pointer_register(), count, dest->as_pointer_register()); break;
2360       default: ShouldNotReachHere();
2361     }
2362   } else {
2363     ShouldNotReachHere();
2364   }
2365 }
2366 
2367 
2368 void LIR_Assembler::emit_alloc_obj(LIR_OpAllocObj* op) {
2369   assert(op->tmp1()->as_register()  == G1 &&
2370          op->tmp2()->as_register()  == G3 &&
2371          op->tmp3()->as_register()  == G4 &&
2372          op->obj()->as_register()   == O0 &&
2373          op->klass()->as_register() == G5, "must be");
2374   if (op->init_check()) {
2375     __ ldub(op->klass()->as_register(),
2376           in_bytes(InstanceKlass::init_state_offset()),
2377           op->tmp1()->as_register());
2378     add_debug_info_for_null_check_here(op->stub()->info());
2379     __ cmp(op->tmp1()->as_register(), InstanceKlass::fully_initialized);
2380     __ br(Assembler::notEqual, false, Assembler::pn, *op->stub()->entry());
2381     __ delayed()->nop();
2382   }
2383   __ allocate_object(op->obj()->as_register(),
2384                      op->tmp1()->as_register(),
2385                      op->tmp2()->as_register(),
2386                      op->tmp3()->as_register(),
2387                      op->header_size(),
2388                      op->object_size(),
2389                      op->klass()->as_register(),
2390                      *op->stub()->entry());
2391   __ bind(*op->stub()->continuation());
2392   __ verify_oop(op->obj()->as_register());
2393 }
2394 
2395 
2396 void LIR_Assembler::emit_alloc_array(LIR_OpAllocArray* op) {
2397   assert(op->tmp1()->as_register()  == G1 &&
2398          op->tmp2()->as_register()  == G3 &&
2399          op->tmp3()->as_register()  == G4 &&
2400          op->tmp4()->as_register()  == O1 &&
2401          op->klass()->as_register() == G5, "must be");
2402 
2403   LP64_ONLY( __ signx(op->len()->as_register()); )
2404   if (UseSlowPath ||
2405       (!UseFastNewObjectArray && (op->type() == T_OBJECT || op->type() == T_ARRAY)) ||
2406       (!UseFastNewTypeArray   && (op->type() != T_OBJECT && op->type() != T_ARRAY))) {
2407     __ br(Assembler::always, false, Assembler::pt, *op->stub()->entry());
2408     __ delayed()->nop();
2409   } else {
2410     __ allocate_array(op->obj()->as_register(),
2411                       op->len()->as_register(),
2412                       op->tmp1()->as_register(),
2413                       op->tmp2()->as_register(),
2414                       op->tmp3()->as_register(),
2415                       arrayOopDesc::header_size(op->type()),
2416                       type2aelembytes(op->type()),
2417                       op->klass()->as_register(),
2418                       *op->stub()->entry());
2419   }
2420   __ bind(*op->stub()->continuation());
2421 }
2422 
2423 
2424 void LIR_Assembler::type_profile_helper(Register mdo, int mdo_offset_bias,
2425                                         ciMethodData *md, ciProfileData *data,
2426                                         Register recv, Register tmp1, Label* update_done) {
2427   uint i;
2428   for (i = 0; i < VirtualCallData::row_limit(); i++) {
2429     Label next_test;
2430     // See if the receiver is receiver[n].
2431     Address receiver_addr(mdo, md->byte_offset_of_slot(data, ReceiverTypeData::receiver_offset(i)) -
2432                           mdo_offset_bias);
2433     __ ld_ptr(receiver_addr, tmp1);
2434     __ verify_klass_ptr(tmp1);
2435     __ cmp_and_brx_short(recv, tmp1, Assembler::notEqual, Assembler::pt, next_test);
2436     Address data_addr(mdo, md->byte_offset_of_slot(data, ReceiverTypeData::receiver_count_offset(i)) -
2437                       mdo_offset_bias);
2438     __ ld_ptr(data_addr, tmp1);
2439     __ add(tmp1, DataLayout::counter_increment, tmp1);
2440     __ st_ptr(tmp1, data_addr);
2441     __ ba(*update_done);
2442     __ delayed()->nop();
2443     __ bind(next_test);
2444   }
2445 
2446   // Didn't find receiver; find next empty slot and fill it in
2447   for (i = 0; i < VirtualCallData::row_limit(); i++) {
2448     Label next_test;
2449     Address recv_addr(mdo, md->byte_offset_of_slot(data, ReceiverTypeData::receiver_offset(i)) -
2450                       mdo_offset_bias);
2451     __ ld_ptr(recv_addr, tmp1);
2452     __ br_notnull_short(tmp1, Assembler::pt, next_test);
2453     __ st_ptr(recv, recv_addr);
2454     __ set(DataLayout::counter_increment, tmp1);
2455     __ st_ptr(tmp1, mdo, md->byte_offset_of_slot(data, ReceiverTypeData::receiver_count_offset(i)) -
2456               mdo_offset_bias);
2457     __ ba(*update_done);
2458     __ delayed()->nop();
2459     __ bind(next_test);
2460   }
2461 }
2462 
2463 
2464 void LIR_Assembler::setup_md_access(ciMethod* method, int bci,
2465                                     ciMethodData*& md, ciProfileData*& data, int& mdo_offset_bias) {
2466   md = method->method_data_or_null();
2467   assert(md != NULL, "Sanity");
2468   data = md->bci_to_data(bci);
2469   assert(data != NULL,       "need data for checkcast");
2470   assert(data->is_ReceiverTypeData(), "need ReceiverTypeData for type check");
2471   if (!Assembler::is_simm13(md->byte_offset_of_slot(data, DataLayout::header_offset()) + data->size_in_bytes())) {
2472     // The offset is large so bias the mdo by the base of the slot so
2473     // that the ld can use simm13s to reference the slots of the data
2474     mdo_offset_bias = md->byte_offset_of_slot(data, DataLayout::header_offset());
2475   }
2476 }
2477 
2478 void LIR_Assembler::emit_typecheck_helper(LIR_OpTypeCheck *op, Label* success, Label* failure, Label* obj_is_null) {
2479   // we always need a stub for the failure case.
2480   CodeStub* stub = op->stub();
2481   Register obj = op->object()->as_register();
2482   Register k_RInfo = op->tmp1()->as_register();
2483   Register klass_RInfo = op->tmp2()->as_register();
2484   Register dst = op->result_opr()->as_register();
2485   Register Rtmp1 = op->tmp3()->as_register();
2486   ciKlass* k = op->klass();
2487 
2488 
2489   if (obj == k_RInfo) {
2490     k_RInfo = klass_RInfo;
2491     klass_RInfo = obj;
2492   }
2493 
2494   ciMethodData* md;
2495   ciProfileData* data;
2496   int mdo_offset_bias = 0;
2497   if (op->should_profile()) {
2498     ciMethod* method = op->profiled_method();
2499     assert(method != NULL, "Should have method");
2500     setup_md_access(method, op->profiled_bci(), md, data, mdo_offset_bias);
2501 
2502     Label not_null;
2503     __ br_notnull_short(obj, Assembler::pn, not_null);
2504     Register mdo      = k_RInfo;
2505     Register data_val = Rtmp1;
2506     metadata2reg(md->constant_encoding(), mdo);
2507     if (mdo_offset_bias > 0) {
2508       __ set(mdo_offset_bias, data_val);
2509       __ add(mdo, data_val, mdo);
2510     }
2511     Address flags_addr(mdo, md->byte_offset_of_slot(data, DataLayout::flags_offset()) - mdo_offset_bias);
2512     __ ldub(flags_addr, data_val);
2513     __ or3(data_val, BitData::null_seen_byte_constant(), data_val);
2514     __ stb(data_val, flags_addr);
2515     __ ba(*obj_is_null);
2516     __ delayed()->nop();
2517     __ bind(not_null);
2518   } else {
2519     __ br_null(obj, false, Assembler::pn, *obj_is_null);
2520     __ delayed()->nop();
2521   }
2522 
2523   Label profile_cast_failure, profile_cast_success;
2524   Label *failure_target = op->should_profile() ? &profile_cast_failure : failure;
2525   Label *success_target = op->should_profile() ? &profile_cast_success : success;
2526 
2527   // patching may screw with our temporaries on sparc,
2528   // so let's do it before loading the class
2529   if (k->is_loaded()) {
2530     metadata2reg(k->constant_encoding(), k_RInfo);
2531   } else {
2532     klass2reg_with_patching(k_RInfo, op->info_for_patch());
2533   }
2534   assert(obj != k_RInfo, "must be different");
2535 
2536   // get object class
2537   // not a safepoint as obj null check happens earlier
2538   __ load_klass(obj, klass_RInfo);
2539   if (op->fast_check()) {
2540     assert_different_registers(klass_RInfo, k_RInfo);
2541     __ cmp(k_RInfo, klass_RInfo);
2542     __ brx(Assembler::notEqual, false, Assembler::pt, *failure_target);
2543     __ delayed()->nop();
2544   } else {
2545     bool need_slow_path = true;
2546     if (k->is_loaded()) {
2547       if ((int) k->super_check_offset() != in_bytes(Klass::secondary_super_cache_offset()))
2548         need_slow_path = false;
2549       // perform the fast part of the checking logic
2550       __ check_klass_subtype_fast_path(klass_RInfo, k_RInfo, Rtmp1, noreg,
2551                                        (need_slow_path ? success_target : NULL),
2552                                        failure_target, NULL,
2553                                        RegisterOrConstant(k->super_check_offset()));
2554     } else {
2555       // perform the fast part of the checking logic
2556       __ check_klass_subtype_fast_path(klass_RInfo, k_RInfo, Rtmp1, O7, success_target,
2557                                        failure_target, NULL);
2558     }
2559     if (need_slow_path) {
2560       // call out-of-line instance of __ check_klass_subtype_slow_path(...):
2561       assert(klass_RInfo == G3 && k_RInfo == G1, "incorrect call setup");
2562       __ call(Runtime1::entry_for(Runtime1::slow_subtype_check_id), relocInfo::runtime_call_type);
2563       __ delayed()->nop();
2564       __ cmp(G3, 0);
2565       __ br(Assembler::equal, false, Assembler::pn, *failure_target);
2566       __ delayed()->nop();
2567       // Fall through to success case
2568     }
2569   }
2570 
2571   if (op->should_profile()) {
2572     Register mdo  = klass_RInfo, recv = k_RInfo, tmp1 = Rtmp1;
2573     assert_different_registers(obj, mdo, recv, tmp1);
2574     __ bind(profile_cast_success);
2575     metadata2reg(md->constant_encoding(), mdo);
2576     if (mdo_offset_bias > 0) {
2577       __ set(mdo_offset_bias, tmp1);
2578       __ add(mdo, tmp1, mdo);
2579     }
2580     __ load_klass(obj, recv);
2581     type_profile_helper(mdo, mdo_offset_bias, md, data, recv, tmp1, success);
2582     // Jump over the failure case
2583     __ ba(*success);
2584     __ delayed()->nop();
2585     // Cast failure case
2586     __ bind(profile_cast_failure);
2587     metadata2reg(md->constant_encoding(), mdo);
2588     if (mdo_offset_bias > 0) {
2589       __ set(mdo_offset_bias, tmp1);
2590       __ add(mdo, tmp1, mdo);
2591     }
2592     Address data_addr(mdo, md->byte_offset_of_slot(data, CounterData::count_offset()) - mdo_offset_bias);
2593     __ ld_ptr(data_addr, tmp1);
2594     __ sub(tmp1, DataLayout::counter_increment, tmp1);
2595     __ st_ptr(tmp1, data_addr);
2596     __ ba(*failure);
2597     __ delayed()->nop();
2598   }
2599   __ ba(*success);
2600   __ delayed()->nop();
2601 }
2602 
2603 void LIR_Assembler::emit_opTypeCheck(LIR_OpTypeCheck* op) {
2604   LIR_Code code = op->code();
2605   if (code == lir_store_check) {
2606     Register value = op->object()->as_register();
2607     Register array = op->array()->as_register();
2608     Register k_RInfo = op->tmp1()->as_register();
2609     Register klass_RInfo = op->tmp2()->as_register();
2610     Register Rtmp1 = op->tmp3()->as_register();
2611 
2612     __ verify_oop(value);
2613     CodeStub* stub = op->stub();
2614     // check if it needs to be profiled
2615     ciMethodData* md;
2616     ciProfileData* data;
2617     int mdo_offset_bias = 0;
2618     if (op->should_profile()) {
2619       ciMethod* method = op->profiled_method();
2620       assert(method != NULL, "Should have method");
2621       setup_md_access(method, op->profiled_bci(), md, data, mdo_offset_bias);
2622     }
2623     Label profile_cast_success, profile_cast_failure, done;
2624     Label *success_target = op->should_profile() ? &profile_cast_success : &done;
2625     Label *failure_target = op->should_profile() ? &profile_cast_failure : stub->entry();
2626 
2627     if (op->should_profile()) {
2628       Label not_null;
2629       __ br_notnull_short(value, Assembler::pn, not_null);
2630       Register mdo      = k_RInfo;
2631       Register data_val = Rtmp1;
2632       metadata2reg(md->constant_encoding(), mdo);
2633       if (mdo_offset_bias > 0) {
2634         __ set(mdo_offset_bias, data_val);
2635         __ add(mdo, data_val, mdo);
2636       }
2637       Address flags_addr(mdo, md->byte_offset_of_slot(data, DataLayout::flags_offset()) - mdo_offset_bias);
2638       __ ldub(flags_addr, data_val);
2639       __ or3(data_val, BitData::null_seen_byte_constant(), data_val);
2640       __ stb(data_val, flags_addr);
2641       __ ba_short(done);
2642       __ bind(not_null);
2643     } else {
2644       __ br_null_short(value, Assembler::pn, done);
2645     }
2646     add_debug_info_for_null_check_here(op->info_for_exception());
2647     __ load_klass(array, k_RInfo);
2648     __ load_klass(value, klass_RInfo);
2649 
2650     // get instance klass
2651     __ ld_ptr(Address(k_RInfo, ObjArrayKlass::element_klass_offset()), k_RInfo);
2652     // perform the fast part of the checking logic
2653     __ check_klass_subtype_fast_path(klass_RInfo, k_RInfo, Rtmp1, O7, success_target, failure_target, NULL);
2654 
2655     // call out-of-line instance of __ check_klass_subtype_slow_path(...):
2656     assert(klass_RInfo == G3 && k_RInfo == G1, "incorrect call setup");
2657     __ call(Runtime1::entry_for(Runtime1::slow_subtype_check_id), relocInfo::runtime_call_type);
2658     __ delayed()->nop();
2659     __ cmp(G3, 0);
2660     __ br(Assembler::equal, false, Assembler::pn, *failure_target);
2661     __ delayed()->nop();
2662     // fall through to the success case
2663 
2664     if (op->should_profile()) {
2665       Register mdo  = klass_RInfo, recv = k_RInfo, tmp1 = Rtmp1;
2666       assert_different_registers(value, mdo, recv, tmp1);
2667       __ bind(profile_cast_success);
2668       metadata2reg(md->constant_encoding(), mdo);
2669       if (mdo_offset_bias > 0) {
2670         __ set(mdo_offset_bias, tmp1);
2671         __ add(mdo, tmp1, mdo);
2672       }
2673       __ load_klass(value, recv);
2674       type_profile_helper(mdo, mdo_offset_bias, md, data, recv, tmp1, &done);
2675       __ ba_short(done);
2676       // Cast failure case
2677       __ bind(profile_cast_failure);
2678       metadata2reg(md->constant_encoding(), mdo);
2679       if (mdo_offset_bias > 0) {
2680         __ set(mdo_offset_bias, tmp1);
2681         __ add(mdo, tmp1, mdo);
2682       }
2683       Address data_addr(mdo, md->byte_offset_of_slot(data, CounterData::count_offset()) - mdo_offset_bias);
2684       __ ld_ptr(data_addr, tmp1);
2685       __ sub(tmp1, DataLayout::counter_increment, tmp1);
2686       __ st_ptr(tmp1, data_addr);
2687       __ ba(*stub->entry());
2688       __ delayed()->nop();
2689     }
2690     __ bind(done);
2691   } else if (code == lir_checkcast) {
2692     Register obj = op->object()->as_register();
2693     Register dst = op->result_opr()->as_register();
2694     Label success;
2695     emit_typecheck_helper(op, &success, op->stub()->entry(), &success);
2696     __ bind(success);
2697     __ mov(obj, dst);
2698   } else if (code == lir_instanceof) {
2699     Register obj = op->object()->as_register();
2700     Register dst = op->result_opr()->as_register();
2701     Label success, failure, done;
2702     emit_typecheck_helper(op, &success, &failure, &failure);
2703     __ bind(failure);
2704     __ set(0, dst);
2705     __ ba_short(done);
2706     __ bind(success);
2707     __ set(1, dst);
2708     __ bind(done);
2709   } else {
2710     ShouldNotReachHere();
2711   }
2712 
2713 }
2714 
2715 
2716 void LIR_Assembler::emit_compare_and_swap(LIR_OpCompareAndSwap* op) {
2717   if (op->code() == lir_cas_long) {
2718     assert(VM_Version::supports_cx8(), "wrong machine");
2719     Register addr = op->addr()->as_pointer_register();
2720     Register cmp_value_lo = op->cmp_value()->as_register_lo();
2721     Register cmp_value_hi = op->cmp_value()->as_register_hi();
2722     Register new_value_lo = op->new_value()->as_register_lo();
2723     Register new_value_hi = op->new_value()->as_register_hi();
2724     Register t1 = op->tmp1()->as_register();
2725     Register t2 = op->tmp2()->as_register();
2726 #ifdef _LP64
2727     __ mov(cmp_value_lo, t1);
2728     __ mov(new_value_lo, t2);
2729     // perform the compare and swap operation
2730     __ casx(addr, t1, t2);
2731     // generate condition code - if the swap succeeded, t2 ("new value" reg) was
2732     // overwritten with the original value in "addr" and will be equal to t1.
2733     __ cmp(t1, t2);
2734 #else
2735     // move high and low halves of long values into single registers
2736     __ sllx(cmp_value_hi, 32, t1);         // shift high half into temp reg
2737     __ srl(cmp_value_lo, 0, cmp_value_lo); // clear upper 32 bits of low half
2738     __ or3(t1, cmp_value_lo, t1);          // t1 holds 64-bit compare value
2739     __ sllx(new_value_hi, 32, t2);
2740     __ srl(new_value_lo, 0, new_value_lo);
2741     __ or3(t2, new_value_lo, t2);          // t2 holds 64-bit value to swap
2742     // perform the compare and swap operation
2743     __ casx(addr, t1, t2);
2744     // generate condition code - if the swap succeeded, t2 ("new value" reg) was
2745     // overwritten with the original value in "addr" and will be equal to t1.
2746     // Produce icc flag for 32bit.
2747     __ sub(t1, t2, t2);
2748     __ srlx(t2, 32, t1);
2749     __ orcc(t2, t1, G0);
2750 #endif
2751   } else if (op->code() == lir_cas_int || op->code() == lir_cas_obj) {
2752     Register addr = op->addr()->as_pointer_register();
2753     Register cmp_value = op->cmp_value()->as_register();
2754     Register new_value = op->new_value()->as_register();
2755     Register t1 = op->tmp1()->as_register();
2756     Register t2 = op->tmp2()->as_register();
2757     __ mov(cmp_value, t1);
2758     __ mov(new_value, t2);
2759     if (op->code() == lir_cas_obj) {
2760       if (UseCompressedOops) {
2761         __ encode_heap_oop(t1);
2762         __ encode_heap_oop(t2);
2763         __ cas(addr, t1, t2);
2764       } else {
2765         __ cas_ptr(addr, t1, t2);
2766       }
2767     } else {
2768       __ cas(addr, t1, t2);
2769     }
2770     __ cmp(t1, t2);
2771   } else {
2772     Unimplemented();
2773   }
2774 }
2775 
2776 void LIR_Assembler::set_24bit_FPU() {
2777   Unimplemented();
2778 }
2779 
2780 
2781 void LIR_Assembler::reset_FPU() {
2782   Unimplemented();
2783 }
2784 
2785 
2786 void LIR_Assembler::breakpoint() {
2787   __ breakpoint_trap();
2788 }
2789 
2790 
2791 void LIR_Assembler::push(LIR_Opr opr) {
2792   Unimplemented();
2793 }
2794 
2795 
2796 void LIR_Assembler::pop(LIR_Opr opr) {
2797   Unimplemented();
2798 }
2799 
2800 
2801 void LIR_Assembler::monitor_address(int monitor_no, LIR_Opr dst_opr) {
2802   Address mon_addr = frame_map()->address_for_monitor_lock(monitor_no);
2803   Register dst = dst_opr->as_register();
2804   Register reg = mon_addr.base();
2805   int offset = mon_addr.disp();
2806   // compute pointer to BasicLock
2807   if (mon_addr.is_simm13()) {
2808     __ add(reg, offset, dst);
2809   } else {
2810     __ set(offset, dst);
2811     __ add(dst, reg, dst);
2812   }
2813 }
2814 
2815 void LIR_Assembler::emit_updatecrc32(LIR_OpUpdateCRC32* op) {
2816   assert(op->crc()->is_single_cpu(),  "crc must be register");
2817   assert(op->val()->is_single_cpu(),  "byte value must be register");
2818   assert(op->result_opr()->is_single_cpu(), "result must be register");
2819   Register crc = op->crc()->as_register();
2820   Register val = op->val()->as_register();
2821   Register table = op->result_opr()->as_register();
2822   Register res   = op->result_opr()->as_register();
2823 
2824   assert_different_registers(val, crc, table);
2825 
2826   __ set(ExternalAddress(StubRoutines::crc_table_addr()), table);
2827   __ not1(crc);
2828   __ clruwu(crc);
2829   __ update_byte_crc32(crc, val, table);
2830   __ not1(crc);
2831 
2832   __ mov(crc, res);
2833 }
2834 
2835 void LIR_Assembler::emit_lock(LIR_OpLock* op) {
2836   Register obj = op->obj_opr()->as_register();
2837   Register hdr = op->hdr_opr()->as_register();
2838   Register lock = op->lock_opr()->as_register();
2839 
2840   // obj may not be an oop
2841   if (op->code() == lir_lock) {
2842     MonitorEnterStub* stub = (MonitorEnterStub*)op->stub();
2843     if (UseFastLocking) {
2844       assert(BasicLock::displaced_header_offset_in_bytes() == 0, "lock_reg must point to the displaced header");
2845       // add debug info for NullPointerException only if one is possible
2846       if (op->info() != NULL) {
2847         add_debug_info_for_null_check_here(op->info());
2848       }
2849       __ lock_object(hdr, obj, lock, op->scratch_opr()->as_register(), *op->stub()->entry());
2850     } else {
2851       // always do slow locking
2852       // note: the slow locking code could be inlined here, however if we use
2853       //       slow locking, speed doesn't matter anyway and this solution is
2854       //       simpler and requires less duplicated code - additionally, the
2855       //       slow locking code is the same in either case which simplifies
2856       //       debugging
2857       __ br(Assembler::always, false, Assembler::pt, *op->stub()->entry());
2858       __ delayed()->nop();
2859     }
2860   } else {
2861     assert (op->code() == lir_unlock, "Invalid code, expected lir_unlock");
2862     if (UseFastLocking) {
2863       assert(BasicLock::displaced_header_offset_in_bytes() == 0, "lock_reg must point to the displaced header");
2864       __ unlock_object(hdr, obj, lock, *op->stub()->entry());
2865     } else {
2866       // always do slow unlocking
2867       // note: the slow unlocking code could be inlined here, however if we use
2868       //       slow unlocking, speed doesn't matter anyway and this solution is
2869       //       simpler and requires less duplicated code - additionally, the
2870       //       slow unlocking code is the same in either case which simplifies
2871       //       debugging
2872       __ br(Assembler::always, false, Assembler::pt, *op->stub()->entry());
2873       __ delayed()->nop();
2874     }
2875   }
2876   __ bind(*op->stub()->continuation());
2877 }
2878 
2879 
2880 void LIR_Assembler::emit_profile_call(LIR_OpProfileCall* op) {
2881   ciMethod* method = op->profiled_method();
2882   int bci          = op->profiled_bci();
2883   ciMethod* callee = op->profiled_callee();
2884 
2885   // Update counter for all call types
2886   ciMethodData* md = method->method_data_or_null();
2887   assert(md != NULL, "Sanity");
2888   ciProfileData* data = md->bci_to_data(bci);
2889   assert(data->is_CounterData(), "need CounterData for calls");
2890   assert(op->mdo()->is_single_cpu(),  "mdo must be allocated");
2891   Register mdo  = op->mdo()->as_register();
2892 #ifdef _LP64
2893   assert(op->tmp1()->is_double_cpu(), "tmp1 must be allocated");
2894   Register tmp1 = op->tmp1()->as_register_lo();
2895 #else
2896   assert(op->tmp1()->is_single_cpu(), "tmp1 must be allocated");
2897   Register tmp1 = op->tmp1()->as_register();
2898 #endif
2899   metadata2reg(md->constant_encoding(), mdo);
2900   int mdo_offset_bias = 0;
2901   if (!Assembler::is_simm13(md->byte_offset_of_slot(data, CounterData::count_offset()) +
2902                             data->size_in_bytes())) {
2903     // The offset is large so bias the mdo by the base of the slot so
2904     // that the ld can use simm13s to reference the slots of the data
2905     mdo_offset_bias = md->byte_offset_of_slot(data, CounterData::count_offset());
2906     __ set(mdo_offset_bias, O7);
2907     __ add(mdo, O7, mdo);
2908   }
2909 
2910   Address counter_addr(mdo, md->byte_offset_of_slot(data, CounterData::count_offset()) - mdo_offset_bias);
2911   Bytecodes::Code bc = method->java_code_at_bci(bci);
2912   const bool callee_is_static = callee->is_loaded() && callee->is_static();
2913   // Perform additional virtual call profiling for invokevirtual and
2914   // invokeinterface bytecodes
2915   if ((bc == Bytecodes::_invokevirtual || bc == Bytecodes::_invokeinterface) &&
2916       !callee_is_static &&  // required for optimized MH invokes
2917       C1ProfileVirtualCalls) {
2918     assert(op->recv()->is_single_cpu(), "recv must be allocated");
2919     Register recv = op->recv()->as_register();
2920     assert_different_registers(mdo, tmp1, recv);
2921     assert(data->is_VirtualCallData(), "need VirtualCallData for virtual calls");
2922     ciKlass* known_klass = op->known_holder();
2923     if (C1OptimizeVirtualCallProfiling && known_klass != NULL) {
2924       // We know the type that will be seen at this call site; we can
2925       // statically update the MethodData* rather than needing to do
2926       // dynamic tests on the receiver type
2927 
2928       // NOTE: we should probably put a lock around this search to
2929       // avoid collisions by concurrent compilations
2930       ciVirtualCallData* vc_data = (ciVirtualCallData*) data;
2931       uint i;
2932       for (i = 0; i < VirtualCallData::row_limit(); i++) {
2933         ciKlass* receiver = vc_data->receiver(i);
2934         if (known_klass->equals(receiver)) {
2935           Address data_addr(mdo, md->byte_offset_of_slot(data,
2936                                                          VirtualCallData::receiver_count_offset(i)) -
2937                             mdo_offset_bias);
2938           __ ld_ptr(data_addr, tmp1);
2939           __ add(tmp1, DataLayout::counter_increment, tmp1);
2940           __ st_ptr(tmp1, data_addr);
2941           return;
2942         }
2943       }
2944 
2945       // Receiver type not found in profile data; select an empty slot
2946 
2947       // Note that this is less efficient than it should be because it
2948       // always does a write to the receiver part of the
2949       // VirtualCallData rather than just the first time
2950       for (i = 0; i < VirtualCallData::row_limit(); i++) {
2951         ciKlass* receiver = vc_data->receiver(i);
2952         if (receiver == NULL) {
2953           Address recv_addr(mdo, md->byte_offset_of_slot(data, VirtualCallData::receiver_offset(i)) -
2954                             mdo_offset_bias);
2955           metadata2reg(known_klass->constant_encoding(), tmp1);
2956           __ st_ptr(tmp1, recv_addr);
2957           Address data_addr(mdo, md->byte_offset_of_slot(data, VirtualCallData::receiver_count_offset(i)) -
2958                             mdo_offset_bias);
2959           __ ld_ptr(data_addr, tmp1);
2960           __ add(tmp1, DataLayout::counter_increment, tmp1);
2961           __ st_ptr(tmp1, data_addr);
2962           return;
2963         }
2964       }
2965     } else {
2966       __ load_klass(recv, recv);
2967       Label update_done;
2968       type_profile_helper(mdo, mdo_offset_bias, md, data, recv, tmp1, &update_done);
2969       // Receiver did not match any saved receiver and there is no empty row for it.
2970       // Increment total counter to indicate polymorphic case.
2971       __ ld_ptr(counter_addr, tmp1);
2972       __ add(tmp1, DataLayout::counter_increment, tmp1);
2973       __ st_ptr(tmp1, counter_addr);
2974 
2975       __ bind(update_done);
2976     }
2977   } else {
2978     // Static call
2979     __ ld_ptr(counter_addr, tmp1);
2980     __ add(tmp1, DataLayout::counter_increment, tmp1);
2981     __ st_ptr(tmp1, counter_addr);
2982   }
2983 }
2984 
2985 void LIR_Assembler::emit_profile_type(LIR_OpProfileType* op) {
2986   Register obj = op->obj()->as_register();
2987   Register tmp1 = op->tmp()->as_pointer_register();
2988   Register tmp2 = G1;
2989   Address mdo_addr = as_Address(op->mdp()->as_address_ptr());
2990   ciKlass* exact_klass = op->exact_klass();
2991   intptr_t current_klass = op->current_klass();
2992   bool not_null = op->not_null();
2993   bool no_conflict = op->no_conflict();
2994 
2995   Label update, next, none;
2996 
2997   bool do_null = !not_null;
2998   bool exact_klass_set = exact_klass != NULL && ciTypeEntries::valid_ciklass(current_klass) == exact_klass;
2999   bool do_update = !TypeEntries::is_type_unknown(current_klass) && !exact_klass_set;
3000 
3001   assert(do_null || do_update, "why are we here?");
3002   assert(!TypeEntries::was_null_seen(current_klass) || do_update, "why are we here?");
3003 
3004   __ verify_oop(obj);
3005 
3006   if (tmp1 != obj) {
3007     __ mov(obj, tmp1);
3008   }
3009   if (do_null) {
3010     __ br_notnull_short(tmp1, Assembler::pt, update);
3011     if (!TypeEntries::was_null_seen(current_klass)) {
3012       __ ld_ptr(mdo_addr, tmp1);
3013       __ or3(tmp1, TypeEntries::null_seen, tmp1);
3014       __ st_ptr(tmp1, mdo_addr);
3015     }
3016     if (do_update) {
3017       __ ba(next);
3018       __ delayed()->nop();
3019     }
3020 #ifdef ASSERT
3021   } else {
3022     __ br_notnull_short(tmp1, Assembler::pt, update);
3023     __ stop("unexpect null obj");
3024 #endif
3025   }
3026 
3027   __ bind(update);
3028 
3029   if (do_update) {
3030 #ifdef ASSERT
3031     if (exact_klass != NULL) {
3032       Label ok;
3033       __ load_klass(tmp1, tmp1);
3034       metadata2reg(exact_klass->constant_encoding(), tmp2);
3035       __ cmp_and_br_short(tmp1, tmp2, Assembler::equal, Assembler::pt, ok);
3036       __ stop("exact klass and actual klass differ");
3037       __ bind(ok);
3038     }
3039 #endif
3040 
3041     Label do_update;
3042     __ ld_ptr(mdo_addr, tmp2);
3043 
3044     if (!no_conflict) {
3045       if (exact_klass == NULL || TypeEntries::is_type_none(current_klass)) {
3046         if (exact_klass != NULL) {
3047           metadata2reg(exact_klass->constant_encoding(), tmp1);
3048         } else {
3049           __ load_klass(tmp1, tmp1);
3050         }
3051 
3052         __ xor3(tmp1, tmp2, tmp1);
3053         __ btst(TypeEntries::type_klass_mask, tmp1);
3054         // klass seen before, nothing to do. The unknown bit may have been
3055         // set already but no need to check.
3056         __ brx(Assembler::zero, false, Assembler::pt, next);
3057         __ delayed()->
3058 
3059            btst(TypeEntries::type_unknown, tmp1);
3060         // already unknown. Nothing to do anymore.
3061         __ brx(Assembler::notZero, false, Assembler::pt, next);
3062 
3063         if (TypeEntries::is_type_none(current_klass)) {
3064           __ delayed()->btst(TypeEntries::type_mask, tmp2);
3065           __ brx(Assembler::zero, true, Assembler::pt, do_update);
3066           // first time here. Set profile type.
3067           __ delayed()->or3(tmp2, tmp1, tmp2);
3068         } else {
3069           __ delayed()->nop();
3070         }
3071       } else {
3072         assert(ciTypeEntries::valid_ciklass(current_klass) != NULL &&
3073                ciTypeEntries::valid_ciklass(current_klass) != exact_klass, "conflict only");
3074 
3075         __ btst(TypeEntries::type_unknown, tmp2);
3076         // already unknown. Nothing to do anymore.
3077         __ brx(Assembler::notZero, false, Assembler::pt, next);
3078         __ delayed()->nop();
3079       }
3080 
3081       // different than before. Cannot keep accurate profile.
3082       __ or3(tmp2, TypeEntries::type_unknown, tmp2);
3083     } else {
3084       // There's a single possible klass at this profile point
3085       assert(exact_klass != NULL, "should be");
3086       if (TypeEntries::is_type_none(current_klass)) {
3087         metadata2reg(exact_klass->constant_encoding(), tmp1);
3088         __ xor3(tmp1, tmp2, tmp1);
3089         __ btst(TypeEntries::type_klass_mask, tmp1);
3090         __ brx(Assembler::zero, false, Assembler::pt, next);
3091 #ifdef ASSERT
3092 
3093         {
3094           Label ok;
3095           __ delayed()->btst(TypeEntries::type_mask, tmp2);
3096           __ brx(Assembler::zero, true, Assembler::pt, ok);
3097           __ delayed()->nop();
3098 
3099           __ stop("unexpected profiling mismatch");
3100           __ bind(ok);
3101         }
3102         // first time here. Set profile type.
3103         __ or3(tmp2, tmp1, tmp2);
3104 #else
3105         // first time here. Set profile type.
3106         __ delayed()->or3(tmp2, tmp1, tmp2);
3107 #endif
3108 
3109       } else {
3110         assert(ciTypeEntries::valid_ciklass(current_klass) != NULL &&
3111                ciTypeEntries::valid_ciklass(current_klass) != exact_klass, "inconsistent");
3112 
3113         // already unknown. Nothing to do anymore.
3114         __ btst(TypeEntries::type_unknown, tmp2);
3115         __ brx(Assembler::notZero, false, Assembler::pt, next);
3116         __ delayed()->or3(tmp2, TypeEntries::type_unknown, tmp2);
3117       }
3118     }
3119 
3120     __ bind(do_update);
3121     __ st_ptr(tmp2, mdo_addr);
3122 
3123     __ bind(next);
3124   }
3125 }
3126 
3127 void LIR_Assembler::align_backward_branch_target() {
3128   __ align(OptoLoopAlignment);
3129 }
3130 
3131 
3132 void LIR_Assembler::emit_delay(LIR_OpDelay* op) {
3133   // make sure we are expecting a delay
3134   // this has the side effect of clearing the delay state
3135   // so we can use _masm instead of _masm->delayed() to do the
3136   // code generation.
3137   __ delayed();
3138 
3139   // make sure we only emit one instruction
3140   int offset = code_offset();
3141   op->delay_op()->emit_code(this);
3142 #ifdef ASSERT
3143   if (code_offset() - offset != NativeInstruction::nop_instruction_size) {
3144     op->delay_op()->print();
3145   }
3146   assert(code_offset() - offset == NativeInstruction::nop_instruction_size,
3147          "only one instruction can go in a delay slot");
3148 #endif
3149 
3150   // we may also be emitting the call info for the instruction
3151   // which we are the delay slot of.
3152   CodeEmitInfo* call_info = op->call_info();
3153   if (call_info) {
3154     add_call_info(code_offset(), call_info);
3155   }
3156 
3157   if (VerifyStackAtCalls) {
3158     _masm->sub(FP, SP, O7);
3159     _masm->cmp(O7, initial_frame_size_in_bytes());
3160     _masm->trap(Assembler::notEqual, Assembler::ptr_cc, G0, ST_RESERVED_FOR_USER_0+2 );
3161   }
3162 }
3163 
3164 
3165 void LIR_Assembler::negate(LIR_Opr left, LIR_Opr dest) {
3166   assert(left->is_register(), "can only handle registers");
3167 
3168   if (left->is_single_cpu()) {
3169     __ neg(left->as_register(), dest->as_register());
3170   } else if (left->is_single_fpu()) {
3171     __ fneg(FloatRegisterImpl::S, left->as_float_reg(), dest->as_float_reg());
3172   } else if (left->is_double_fpu()) {
3173     __ fneg(FloatRegisterImpl::D, left->as_double_reg(), dest->as_double_reg());
3174   } else {
3175     assert (left->is_double_cpu(), "Must be a long");
3176     Register Rlow = left->as_register_lo();
3177     Register Rhi = left->as_register_hi();
3178 #ifdef _LP64
3179     __ sub(G0, Rlow, dest->as_register_lo());
3180 #else
3181     __ subcc(G0, Rlow, dest->as_register_lo());
3182     __ subc (G0, Rhi,  dest->as_register_hi());
3183 #endif
3184   }
3185 }
3186 
3187 
3188 void LIR_Assembler::fxch(int i) {
3189   Unimplemented();
3190 }
3191 
3192 void LIR_Assembler::fld(int i) {
3193   Unimplemented();
3194 }
3195 
3196 void LIR_Assembler::ffree(int i) {
3197   Unimplemented();
3198 }
3199 
3200 void LIR_Assembler::rt_call(LIR_Opr result, address dest,
3201                             const LIR_OprList* args, LIR_Opr tmp, CodeEmitInfo* info) {
3202 
3203   // if tmp is invalid, then the function being called doesn't destroy the thread
3204   if (tmp->is_valid()) {
3205     __ save_thread(tmp->as_pointer_register());
3206   }
3207   __ call(dest, relocInfo::runtime_call_type);
3208   __ delayed()->nop();
3209   if (info != NULL) {
3210     add_call_info_here(info);
3211   }
3212   if (tmp->is_valid()) {
3213     __ restore_thread(tmp->as_pointer_register());
3214   }
3215 
3216 #ifdef ASSERT
3217   __ verify_thread();
3218 #endif // ASSERT
3219 }
3220 
3221 
3222 void LIR_Assembler::volatile_move_op(LIR_Opr src, LIR_Opr dest, BasicType type, CodeEmitInfo* info) {
3223 #ifdef _LP64
3224   ShouldNotReachHere();
3225 #endif
3226 
3227   NEEDS_CLEANUP;
3228   if (type == T_LONG) {
3229     LIR_Address* mem_addr = dest->is_address() ? dest->as_address_ptr() : src->as_address_ptr();
3230 
3231     // (extended to allow indexed as well as constant displaced for JSR-166)
3232     Register idx = noreg; // contains either constant offset or index
3233 
3234     int disp = mem_addr->disp();
3235     if (mem_addr->index() == LIR_OprFact::illegalOpr) {
3236       if (!Assembler::is_simm13(disp)) {
3237         idx = O7;
3238         __ set(disp, idx);
3239       }
3240     } else {
3241       assert(disp == 0, "not both indexed and disp");
3242       idx = mem_addr->index()->as_register();
3243     }
3244 
3245     int null_check_offset = -1;
3246 
3247     Register base = mem_addr->base()->as_register();
3248     if (src->is_register() && dest->is_address()) {
3249       // G4 is high half, G5 is low half
3250       // clear the top bits of G5, and scale up G4
3251       __ srl (src->as_register_lo(),  0, G5);
3252       __ sllx(src->as_register_hi(), 32, G4);
3253       // combine the two halves into the 64 bits of G4
3254       __ or3(G4, G5, G4);
3255       null_check_offset = __ offset();
3256       if (idx == noreg) {
3257         __ stx(G4, base, disp);
3258       } else {
3259         __ stx(G4, base, idx);
3260       }
3261     } else if (src->is_address() && dest->is_register()) {
3262       null_check_offset = __ offset();
3263       if (idx == noreg) {
3264         __ ldx(base, disp, G5);
3265       } else {
3266         __ ldx(base, idx, G5);
3267       }
3268       __ srax(G5, 32, dest->as_register_hi()); // fetch the high half into hi
3269       __ mov (G5, dest->as_register_lo());     // copy low half into lo
3270     } else {
3271       Unimplemented();
3272     }
3273     if (info != NULL) {
3274       add_debug_info_for_null_check(null_check_offset, info);
3275     }
3276 
3277   } else {
3278     // use normal move for all other volatiles since they don't need
3279     // special handling to remain atomic.
3280     move_op(src, dest, type, lir_patch_none, info, false, false, false);
3281   }
3282 }
3283 
3284 void LIR_Assembler::membar() {
3285   // only StoreLoad membars are ever explicitly needed on sparcs in TSO mode
3286   __ membar( Assembler::Membar_mask_bits(Assembler::StoreLoad) );
3287 }
3288 
3289 void LIR_Assembler::membar_acquire() {
3290   // no-op on TSO
3291 }
3292 
3293 void LIR_Assembler::membar_release() {
3294   // no-op on TSO
3295 }
3296 
3297 void LIR_Assembler::membar_loadload() {
3298   // no-op
3299   //__ membar(Assembler::Membar_mask_bits(Assembler::loadload));
3300 }
3301 
3302 void LIR_Assembler::membar_storestore() {
3303   // no-op
3304   //__ membar(Assembler::Membar_mask_bits(Assembler::storestore));
3305 }
3306 
3307 void LIR_Assembler::membar_loadstore() {
3308   // no-op
3309   //__ membar(Assembler::Membar_mask_bits(Assembler::loadstore));
3310 }
3311 
3312 void LIR_Assembler::membar_storeload() {
3313   __ membar(Assembler::Membar_mask_bits(Assembler::StoreLoad));
3314 }
3315 
3316 void LIR_Assembler::on_spin_wait() {
3317   Unimplemented();
3318 }
3319 
3320 // Pack two sequential registers containing 32 bit values
3321 // into a single 64 bit register.
3322 // src and src->successor() are packed into dst
3323 // src and dst may be the same register.
3324 // Note: src is destroyed
3325 void LIR_Assembler::pack64(LIR_Opr src, LIR_Opr dst) {
3326   Register rs = src->as_register();
3327   Register rd = dst->as_register_lo();
3328   __ sllx(rs, 32, rs);
3329   __ srl(rs->successor(), 0, rs->successor());
3330   __ or3(rs, rs->successor(), rd);
3331 }
3332 
3333 // Unpack a 64 bit value in a register into
3334 // two sequential registers.
3335 // src is unpacked into dst and dst->successor()
3336 void LIR_Assembler::unpack64(LIR_Opr src, LIR_Opr dst) {
3337   Register rs = src->as_register_lo();
3338   Register rd = dst->as_register_hi();
3339   assert_different_registers(rs, rd, rd->successor());
3340   __ srlx(rs, 32, rd);
3341   __ srl (rs,  0, rd->successor());
3342 }
3343 
3344 
3345 void LIR_Assembler::leal(LIR_Opr addr_opr, LIR_Opr dest) {
3346   LIR_Address* addr = addr_opr->as_address_ptr();
3347   assert(addr->index()->is_illegal() && addr->scale() == LIR_Address::times_1, "can't handle complex addresses yet");
3348 
3349   if (Assembler::is_simm13(addr->disp())) {
3350     __ add(addr->base()->as_pointer_register(), addr->disp(), dest->as_pointer_register());
3351   } else {
3352     __ set(addr->disp(), G3_scratch);
3353     __ add(addr->base()->as_pointer_register(), G3_scratch, dest->as_pointer_register());
3354   }
3355 }
3356 
3357 
3358 void LIR_Assembler::get_thread(LIR_Opr result_reg) {
3359   assert(result_reg->is_register(), "check");
3360   __ mov(G2_thread, result_reg->as_register());
3361 }
3362 
3363 #ifdef ASSERT
3364 // emit run-time assertion
3365 void LIR_Assembler::emit_assert(LIR_OpAssert* op) {
3366   assert(op->code() == lir_assert, "must be");
3367 
3368   if (op->in_opr1()->is_valid()) {
3369     assert(op->in_opr2()->is_valid(), "both operands must be valid");
3370     comp_op(op->condition(), op->in_opr1(), op->in_opr2(), op);
3371   } else {
3372     assert(op->in_opr2()->is_illegal(), "both operands must be illegal");
3373     assert(op->condition() == lir_cond_always, "no other conditions allowed");
3374   }
3375 
3376   Label ok;
3377   if (op->condition() != lir_cond_always) {
3378     Assembler::Condition acond;
3379     switch (op->condition()) {
3380       case lir_cond_equal:        acond = Assembler::equal;                break;
3381       case lir_cond_notEqual:     acond = Assembler::notEqual;             break;
3382       case lir_cond_less:         acond = Assembler::less;                 break;
3383       case lir_cond_lessEqual:    acond = Assembler::lessEqual;            break;
3384       case lir_cond_greaterEqual: acond = Assembler::greaterEqual;         break;
3385       case lir_cond_greater:      acond = Assembler::greater;              break;
3386       case lir_cond_aboveEqual:   acond = Assembler::greaterEqualUnsigned; break;
3387       case lir_cond_belowEqual:   acond = Assembler::lessEqualUnsigned;    break;
3388       default:                         ShouldNotReachHere();
3389     };
3390     __ br(acond, false, Assembler::pt, ok);
3391     __ delayed()->nop();
3392   }
3393   if (op->halt()) {
3394     const char* str = __ code_string(op->msg());
3395     __ stop(str);
3396   } else {
3397     breakpoint();
3398   }
3399   __ bind(ok);
3400 }
3401 #endif
3402 
3403 void LIR_Assembler::peephole(LIR_List* lir) {
3404   LIR_OpList* inst = lir->instructions_list();
3405   for (int i = 0; i < inst->length(); i++) {
3406     LIR_Op* op = inst->at(i);
3407     switch (op->code()) {
3408       case lir_cond_float_branch:
3409       case lir_branch: {
3410         LIR_OpBranch* branch = op->as_OpBranch();
3411         assert(branch->info() == NULL, "shouldn't be state on branches anymore");
3412         LIR_Op* delay_op = NULL;
3413         // we'd like to be able to pull following instructions into
3414         // this slot but we don't know enough to do it safely yet so
3415         // only optimize block to block control flow.
3416         if (LIRFillDelaySlots && branch->block()) {
3417           LIR_Op* prev = inst->at(i - 1);
3418           if (prev && LIR_Assembler::is_single_instruction(prev) && prev->info() == NULL) {
3419             // swap previous instruction into delay slot
3420             inst->at_put(i - 1, op);
3421             inst->at_put(i, new LIR_OpDelay(prev, op->info()));
3422 #ifndef PRODUCT
3423             if (LIRTracePeephole) {
3424               tty->print_cr("delayed");
3425               inst->at(i - 1)->print();
3426               inst->at(i)->print();
3427               tty->cr();
3428             }
3429 #endif
3430             continue;
3431           }
3432         }
3433 
3434         if (!delay_op) {
3435           delay_op = new LIR_OpDelay(new LIR_Op0(lir_nop), NULL);
3436         }
3437         inst->insert_before(i + 1, delay_op);
3438         break;
3439       }
3440       case lir_static_call:
3441       case lir_virtual_call:
3442       case lir_icvirtual_call:
3443       case lir_optvirtual_call:
3444       case lir_dynamic_call: {
3445         LIR_Op* prev = inst->at(i - 1);
3446         if (LIRFillDelaySlots && prev && prev->code() == lir_move && prev->info() == NULL &&
3447             (op->code() != lir_virtual_call ||
3448              !prev->result_opr()->is_single_cpu() ||
3449              prev->result_opr()->as_register() != O0) &&
3450             LIR_Assembler::is_single_instruction(prev)) {
3451           // Only moves without info can be put into the delay slot.
3452           // Also don't allow the setup of the receiver in the delay
3453           // slot for vtable calls.
3454           inst->at_put(i - 1, op);
3455           inst->at_put(i, new LIR_OpDelay(prev, op->info()));
3456 #ifndef PRODUCT
3457           if (LIRTracePeephole) {
3458             tty->print_cr("delayed");
3459             inst->at(i - 1)->print();
3460             inst->at(i)->print();
3461             tty->cr();
3462           }
3463 #endif
3464         } else {
3465           LIR_Op* delay_op = new LIR_OpDelay(new LIR_Op0(lir_nop), op->as_OpJavaCall()->info());
3466           inst->insert_before(i + 1, delay_op);
3467           i++;
3468         }
3469 
3470 #if defined(TIERED) && !defined(_LP64)
3471         // fixup the return value from G1 to O0/O1 for long returns.
3472         // It's done here instead of in LIRGenerator because there's
3473         // such a mismatch between the single reg and double reg
3474         // calling convention.
3475         LIR_OpJavaCall* callop = op->as_OpJavaCall();
3476         if (callop->result_opr() == FrameMap::out_long_opr) {
3477           LIR_OpJavaCall* call;
3478           LIR_OprList* arguments = new LIR_OprList(callop->arguments()->length());
3479           for (int a = 0; a < arguments->length(); a++) {
3480             arguments[a] = callop->arguments()[a];
3481           }
3482           if (op->code() == lir_virtual_call) {
3483             call = new LIR_OpJavaCall(op->code(), callop->method(), callop->receiver(), FrameMap::g1_long_single_opr,
3484                                       callop->vtable_offset(), arguments, callop->info());
3485           } else {
3486             call = new LIR_OpJavaCall(op->code(), callop->method(), callop->receiver(), FrameMap::g1_long_single_opr,
3487                                       callop->addr(), arguments, callop->info());
3488           }
3489           inst->at_put(i - 1, call);
3490           inst->insert_before(i + 1, new LIR_Op1(lir_unpack64, FrameMap::g1_long_single_opr, callop->result_opr(),
3491                                                  T_LONG, lir_patch_none, NULL));
3492         }
3493 #endif
3494         break;
3495       }
3496     }
3497   }
3498 }
3499 
3500 void LIR_Assembler::atomic_op(LIR_Code code, LIR_Opr src, LIR_Opr data, LIR_Opr dest, LIR_Opr tmp) {
3501   LIR_Address* addr = src->as_address_ptr();
3502 
3503   assert(data == dest, "swap uses only 2 operands");
3504   assert (code == lir_xchg, "no xadd on sparc");
3505 
3506   if (data->type() == T_INT) {
3507     __ swap(as_Address(addr), data->as_register());
3508   } else if (data->is_oop()) {
3509     Register obj = data->as_register();
3510     Register narrow = tmp->as_register();
3511 #ifdef _LP64
3512     assert(UseCompressedOops, "swap is 32bit only");
3513     __ encode_heap_oop(obj, narrow);
3514     __ swap(as_Address(addr), narrow);
3515     __ decode_heap_oop(narrow, obj);
3516 #else
3517     __ swap(as_Address(addr), obj);
3518 #endif
3519   } else {
3520     ShouldNotReachHere();
3521   }
3522 }
3523 
3524 #undef __