1 /*
2  * Copyright (c) 2016, 2019, Oracle and/or its affiliates. All rights reserved.
3  * Copyright (c) 2016, 2019, SAP SE. All rights reserved.
4  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5  *
6  * This code is free software; you can redistribute it and/or modify it
7  * under the terms of the GNU General Public License version 2 only, as
8  * published by the Free Software Foundation.
9  *
10  * This code is distributed in the hope that it will be useful, but WITHOUT
11  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
13  * version 2 for more details (a copy is included in the LICENSE file that
14  * accompanied this code).
15  *
16  * You should have received a copy of the GNU General Public License version
17  * 2 along with this work; if not, write to the Free Software Foundation,
18  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
19  *
20  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
21  * or visit www.oracle.com if you need additional information or have any
22  * questions.
23  *
24  */
25 
26 #include "precompiled.hpp"
27 #include "asm/macroAssembler.inline.hpp"
28 #include "c1/c1_Compilation.hpp"
29 #include "c1/c1_LIRAssembler.hpp"
30 #include "c1/c1_MacroAssembler.hpp"
31 #include "c1/c1_Runtime1.hpp"
32 #include "c1/c1_ValueStack.hpp"
33 #include "ci/ciArrayKlass.hpp"
34 #include "ci/ciInstance.hpp"
35 #include "gc/shared/collectedHeap.hpp"
36 #include "gc/shared/barrierSet.hpp"
37 #include "gc/shared/cardTableBarrierSet.hpp"
38 #include "nativeInst_s390.hpp"
39 #include "oops/objArrayKlass.hpp"
40 #include "runtime/frame.inline.hpp"
41 #include "runtime/safepointMechanism.inline.hpp"
42 #include "runtime/sharedRuntime.hpp"
43 #include "vmreg_s390.inline.hpp"
44 
45 #define __ _masm->
46 
47 #ifndef PRODUCT
48 #undef __
49 #define __ (Verbose ? (_masm->block_comment(FILE_AND_LINE),_masm) : _masm)->
50 #endif
51 
52 //------------------------------------------------------------
53 
is_small_constant(LIR_Opr opr)54 bool LIR_Assembler::is_small_constant(LIR_Opr opr) {
55   // Not used on ZARCH_64
56   ShouldNotCallThis();
57   return false;
58 }
59 
receiverOpr()60 LIR_Opr LIR_Assembler::receiverOpr() {
61   return FrameMap::Z_R2_oop_opr;
62 }
63 
osrBufferPointer()64 LIR_Opr LIR_Assembler::osrBufferPointer() {
65   return FrameMap::Z_R2_opr;
66 }
67 
initial_frame_size_in_bytes() const68 int LIR_Assembler::initial_frame_size_in_bytes() const {
69   return in_bytes(frame_map()->framesize_in_bytes());
70 }
71 
72 // Inline cache check: done before the frame is built.
73 // The inline cached class is in Z_inline_cache(Z_R9).
74 // We fetch the class of the receiver and compare it with the cached class.
75 // If they do not match we jump to the slow case.
check_icache()76 int LIR_Assembler::check_icache() {
77   Register receiver = receiverOpr()->as_register();
78   int offset = __ offset();
79   __ inline_cache_check(receiver, Z_inline_cache);
80   return offset;
81 }
82 
osr_entry()83 void LIR_Assembler::osr_entry() {
84   // On-stack-replacement entry sequence (interpreter frame layout described in interpreter_sparc.cpp):
85   //
86   //   1. Create a new compiled activation.
87   //   2. Initialize local variables in the compiled activation. The expression stack must be empty
88   //      at the osr_bci; it is not initialized.
89   //   3. Jump to the continuation address in compiled code to resume execution.
90 
91   // OSR entry point
92   offsets()->set_value(CodeOffsets::OSR_Entry, code_offset());
93   BlockBegin* osr_entry = compilation()->hir()->osr_entry();
94   ValueStack* entry_state = osr_entry->end()->state();
95   int number_of_locks = entry_state->locks_size();
96 
97   // Create a frame for the compiled activation.
98   __ build_frame(initial_frame_size_in_bytes(), bang_size_in_bytes());
99 
100   // OSR buffer is
101   //
102   // locals[nlocals-1..0]
103   // monitors[number_of_locks-1..0]
104   //
105   // Locals is a direct copy of the interpreter frame so in the osr buffer
106   // the first slot in the local array is the last local from the interpreter
107   // and the last slot is local[0] (receiver) from the interpreter
108   //
109   // Similarly with locks. The first lock slot in the osr buffer is the nth lock
110   // from the interpreter frame, the nth lock slot in the osr buffer is 0th lock
111   // in the interpreter frame (the method lock if a sync method)
112 
113   // Initialize monitors in the compiled activation.
114   //   I0: pointer to osr buffer
115   //
116   // All other registers are dead at this point and the locals will be
117   // copied into place by code emitted in the IR.
118 
119   Register OSR_buf = osrBufferPointer()->as_register();
120   { assert(frame::interpreter_frame_monitor_size() == BasicObjectLock::size(), "adjust code below");
121     int monitor_offset = BytesPerWord * method()->max_locals() +
122       (2 * BytesPerWord) * (number_of_locks - 1);
123     // SharedRuntime::OSR_migration_begin() packs BasicObjectLocks in
124     // the OSR buffer using 2 word entries: first the lock and then
125     // the oop.
126     for (int i = 0; i < number_of_locks; i++) {
127       int slot_offset = monitor_offset - ((i * 2) * BytesPerWord);
128       // Verify the interpreter's monitor has a non-null object.
129       __ asm_assert_mem8_isnot_zero(slot_offset + 1*BytesPerWord, OSR_buf, "locked object is NULL", __LINE__);
130       // Copy the lock field into the compiled activation.
131       __ z_lg(Z_R1_scratch, slot_offset + 0, OSR_buf);
132       __ z_stg(Z_R1_scratch, frame_map()->address_for_monitor_lock(i));
133       __ z_lg(Z_R1_scratch, slot_offset + 1*BytesPerWord, OSR_buf);
134       __ z_stg(Z_R1_scratch, frame_map()->address_for_monitor_object(i));
135     }
136   }
137 }
138 
139 // --------------------------------------------------------------------------------------------
140 
emit_call_c(address a)141 address LIR_Assembler::emit_call_c(address a) {
142   __ align_call_far_patchable(__ pc());
143   address call_addr = __ call_c_opt(a);
144   if (call_addr == NULL) {
145     bailout("const section overflow");
146   }
147   return call_addr;
148 }
149 
emit_exception_handler()150 int LIR_Assembler::emit_exception_handler() {
151   // If the last instruction is a call (typically to do a throw which
152   // is coming at the end after block reordering) the return address
153   // must still point into the code area in order to avoid assertion
154   // failures when searching for the corresponding bci. => Add a nop.
155   // (was bug 5/14/1999 - gri)
156   __ nop();
157 
158   // Generate code for exception handler.
159   address handler_base = __ start_a_stub(exception_handler_size());
160   if (handler_base == NULL) {
161     // Not enough space left for the handler.
162     bailout("exception handler overflow");
163     return -1;
164   }
165 
166   int offset = code_offset();
167 
168   address a = Runtime1::entry_for (Runtime1::handle_exception_from_callee_id);
169   address call_addr = emit_call_c(a);
170   CHECK_BAILOUT_(-1);
171   __ should_not_reach_here();
172   guarantee(code_offset() - offset <= exception_handler_size(), "overflow");
173   __ end_a_stub();
174 
175   return offset;
176 }
177 
178 // Emit the code to remove the frame from the stack in the exception
179 // unwind path.
emit_unwind_handler()180 int LIR_Assembler::emit_unwind_handler() {
181 #ifndef PRODUCT
182   if (CommentedAssembly) {
183     _masm->block_comment("Unwind handler");
184   }
185 #endif
186 
187   int offset = code_offset();
188   Register exception_oop_callee_saved = Z_R10; // Z_R10 is callee-saved.
189   Register Rtmp1                      = Z_R11;
190   Register Rtmp2                      = Z_R12;
191 
192   // Fetch the exception from TLS and clear out exception related thread state.
193   Address exc_oop_addr = Address(Z_thread, JavaThread::exception_oop_offset());
194   Address exc_pc_addr  = Address(Z_thread, JavaThread::exception_pc_offset());
195   __ z_lg(Z_EXC_OOP, exc_oop_addr);
196   __ clear_mem(exc_oop_addr, sizeof(oop));
197   __ clear_mem(exc_pc_addr, sizeof(intptr_t));
198 
199   __ bind(_unwind_handler_entry);
200   __ verify_not_null_oop(Z_EXC_OOP);
201   if (method()->is_synchronized() || compilation()->env()->dtrace_method_probes()) {
202     __ lgr_if_needed(exception_oop_callee_saved, Z_EXC_OOP); // Preserve the exception.
203   }
204 
205   // Preform needed unlocking.
206   MonitorExitStub* stub = NULL;
207   if (method()->is_synchronized()) {
208     // Runtime1::monitorexit_id expects lock address in Z_R1_scratch.
209     LIR_Opr lock = FrameMap::as_opr(Z_R1_scratch);
210     monitor_address(0, lock);
211     stub = new MonitorExitStub(lock, true, 0);
212     __ unlock_object(Rtmp1, Rtmp2, lock->as_register(), *stub->entry());
213     __ bind(*stub->continuation());
214   }
215 
216   if (compilation()->env()->dtrace_method_probes()) {
217     ShouldNotReachHere(); // Not supported.
218 #if 0
219     __ mov(rdi, r15_thread);
220     __ mov_metadata(rsi, method()->constant_encoding());
221     __ call(RuntimeAddress(CAST_FROM_FN_PTR(address, SharedRuntime::dtrace_method_exit)));
222 #endif
223   }
224 
225   if (method()->is_synchronized() || compilation()->env()->dtrace_method_probes()) {
226     __ lgr_if_needed(Z_EXC_OOP, exception_oop_callee_saved);  // Restore the exception.
227   }
228 
229   // Remove the activation and dispatch to the unwind handler.
230   __ pop_frame();
231   __ z_lg(Z_EXC_PC, _z_abi16(return_pc), Z_SP);
232 
233   // Z_EXC_OOP: exception oop
234   // Z_EXC_PC: exception pc
235 
236   // Dispatch to the unwind logic.
237   __ load_const_optimized(Z_R5, Runtime1::entry_for (Runtime1::unwind_exception_id));
238   __ z_br(Z_R5);
239 
240   // Emit the slow path assembly.
241   if (stub != NULL) {
242     stub->emit_code(this);
243   }
244 
245   return offset;
246 }
247 
emit_deopt_handler()248 int LIR_Assembler::emit_deopt_handler() {
249   // If the last instruction is a call (typically to do a throw which
250   // is coming at the end after block reordering) the return address
251   // must still point into the code area in order to avoid assertion
252   // failures when searching for the corresponding bci. => Add a nop.
253   // (was bug 5/14/1999 - gri)
254   __ nop();
255 
256   // Generate code for exception handler.
257   address handler_base = __ start_a_stub(deopt_handler_size());
258   if (handler_base == NULL) {
259     // Not enough space left for the handler.
260     bailout("deopt handler overflow");
261     return -1;
262   }  int offset = code_offset();
263   // Size must be constant (see HandlerImpl::emit_deopt_handler).
264   __ load_const(Z_R1_scratch, SharedRuntime::deopt_blob()->unpack());
265   __ call(Z_R1_scratch);
266   guarantee(code_offset() - offset <= deopt_handler_size(), "overflow");
267   __ end_a_stub();
268 
269   return offset;
270 }
271 
jobject2reg(jobject o,Register reg)272 void LIR_Assembler::jobject2reg(jobject o, Register reg) {
273   if (o == NULL) {
274     __ clear_reg(reg, true/*64bit*/, false/*set cc*/); // Must not kill cc set by cmove.
275   } else {
276     AddressLiteral a = __ allocate_oop_address(o);
277     bool success = __ load_oop_from_toc(reg, a, reg);
278     if (!success) {
279       bailout("const section overflow");
280     }
281   }
282 }
283 
jobject2reg_with_patching(Register reg,CodeEmitInfo * info)284 void LIR_Assembler::jobject2reg_with_patching(Register reg, CodeEmitInfo *info) {
285   // Allocate a new index in table to hold the object once it's been patched.
286   int oop_index = __ oop_recorder()->allocate_oop_index(NULL);
287   PatchingStub* patch = new PatchingStub(_masm, patching_id(info), oop_index);
288 
289   AddressLiteral addrlit((intptr_t)0, oop_Relocation::spec(oop_index));
290   assert(addrlit.rspec().type() == relocInfo::oop_type, "must be an oop reloc");
291   // The NULL will be dynamically patched later so the sequence to
292   // load the address literal must not be optimized.
293   __ load_const(reg, addrlit);
294 
295   patching_epilog(patch, lir_patch_normal, reg, info);
296 }
297 
metadata2reg(Metadata * md,Register reg)298 void LIR_Assembler::metadata2reg(Metadata* md, Register reg) {
299   bool success = __ set_metadata_constant(md, reg);
300   if (!success) {
301     bailout("const section overflow");
302     return;
303   }
304 }
305 
klass2reg_with_patching(Register reg,CodeEmitInfo * info)306 void LIR_Assembler::klass2reg_with_patching(Register reg, CodeEmitInfo *info) {
307   // Allocate a new index in table to hold the klass once it's been patched.
308   int index = __ oop_recorder()->allocate_metadata_index(NULL);
309   PatchingStub* patch = new PatchingStub(_masm, PatchingStub::load_klass_id, index);
310   AddressLiteral addrlit((intptr_t)0, metadata_Relocation::spec(index));
311   assert(addrlit.rspec().type() == relocInfo::metadata_type, "must be an metadata reloc");
312   // The NULL will be dynamically patched later so the sequence to
313   // load the address literal must not be optimized.
314   __ load_const(reg, addrlit);
315 
316   patching_epilog(patch, lir_patch_normal, reg, info);
317 }
318 
emit_op3(LIR_Op3 * op)319 void LIR_Assembler::emit_op3(LIR_Op3* op) {
320   switch (op->code()) {
321     case lir_idiv:
322     case lir_irem:
323       arithmetic_idiv(op->code(),
324                       op->in_opr1(),
325                       op->in_opr2(),
326                       op->in_opr3(),
327                       op->result_opr(),
328                       op->info());
329       break;
330     case lir_fmad: {
331       const FloatRegister opr1 = op->in_opr1()->as_double_reg(),
332                           opr2 = op->in_opr2()->as_double_reg(),
333                           opr3 = op->in_opr3()->as_double_reg(),
334                           res  = op->result_opr()->as_double_reg();
335       __ z_madbr(opr3, opr1, opr2);
336       if (res != opr3) { __ z_ldr(res, opr3); }
337     } break;
338     case lir_fmaf: {
339       const FloatRegister opr1 = op->in_opr1()->as_float_reg(),
340                           opr2 = op->in_opr2()->as_float_reg(),
341                           opr3 = op->in_opr3()->as_float_reg(),
342                           res  = op->result_opr()->as_float_reg();
343       __ z_maebr(opr3, opr1, opr2);
344       if (res != opr3) { __ z_ler(res, opr3); }
345     } break;
346     default: ShouldNotReachHere(); break;
347   }
348 }
349 
350 
emit_opBranch(LIR_OpBranch * op)351 void LIR_Assembler::emit_opBranch(LIR_OpBranch* op) {
352 #ifdef ASSERT
353   assert(op->block() == NULL || op->block()->label() == op->label(), "wrong label");
354   if (op->block() != NULL)  { _branch_target_blocks.append(op->block()); }
355   if (op->ublock() != NULL) { _branch_target_blocks.append(op->ublock()); }
356 #endif
357 
358   if (op->cond() == lir_cond_always) {
359     if (op->info() != NULL) { add_debug_info_for_branch(op->info()); }
360     __ branch_optimized(Assembler::bcondAlways, *(op->label()));
361   } else {
362     Assembler::branch_condition acond = Assembler::bcondZero;
363     if (op->code() == lir_cond_float_branch) {
364       assert(op->ublock() != NULL, "must have unordered successor");
365       __ branch_optimized(Assembler::bcondNotOrdered, *(op->ublock()->label()));
366     }
367     switch (op->cond()) {
368       case lir_cond_equal:        acond = Assembler::bcondEqual;     break;
369       case lir_cond_notEqual:     acond = Assembler::bcondNotEqual;  break;
370       case lir_cond_less:         acond = Assembler::bcondLow;       break;
371       case lir_cond_lessEqual:    acond = Assembler::bcondNotHigh;   break;
372       case lir_cond_greaterEqual: acond = Assembler::bcondNotLow;    break;
373       case lir_cond_greater:      acond = Assembler::bcondHigh;      break;
374       case lir_cond_belowEqual:   acond = Assembler::bcondNotHigh;   break;
375       case lir_cond_aboveEqual:   acond = Assembler::bcondNotLow;    break;
376       default:                         ShouldNotReachHere();
377     }
378     __ branch_optimized(acond,*(op->label()));
379   }
380 }
381 
382 
emit_opConvert(LIR_OpConvert * op)383 void LIR_Assembler::emit_opConvert(LIR_OpConvert* op) {
384   LIR_Opr src  = op->in_opr();
385   LIR_Opr dest = op->result_opr();
386 
387   switch (op->bytecode()) {
388     case Bytecodes::_i2l:
389       __ move_reg_if_needed(dest->as_register_lo(), T_LONG, src->as_register(), T_INT);
390       break;
391 
392     case Bytecodes::_l2i:
393       __ move_reg_if_needed(dest->as_register(), T_INT, src->as_register_lo(), T_LONG);
394       break;
395 
396     case Bytecodes::_i2b:
397       __ move_reg_if_needed(dest->as_register(), T_BYTE, src->as_register(), T_INT);
398       break;
399 
400     case Bytecodes::_i2c:
401       __ move_reg_if_needed(dest->as_register(), T_CHAR, src->as_register(), T_INT);
402       break;
403 
404     case Bytecodes::_i2s:
405       __ move_reg_if_needed(dest->as_register(), T_SHORT, src->as_register(), T_INT);
406       break;
407 
408     case Bytecodes::_f2d:
409       assert(dest->is_double_fpu(), "check");
410       __ move_freg_if_needed(dest->as_double_reg(), T_DOUBLE, src->as_float_reg(), T_FLOAT);
411       break;
412 
413     case Bytecodes::_d2f:
414       assert(dest->is_single_fpu(), "check");
415       __ move_freg_if_needed(dest->as_float_reg(), T_FLOAT, src->as_double_reg(), T_DOUBLE);
416       break;
417 
418     case Bytecodes::_i2f:
419       __ z_cefbr(dest->as_float_reg(), src->as_register());
420       break;
421 
422     case Bytecodes::_i2d:
423       __ z_cdfbr(dest->as_double_reg(), src->as_register());
424       break;
425 
426     case Bytecodes::_l2f:
427       __ z_cegbr(dest->as_float_reg(), src->as_register_lo());
428       break;
429     case Bytecodes::_l2d:
430       __ z_cdgbr(dest->as_double_reg(), src->as_register_lo());
431       break;
432 
433     case Bytecodes::_f2i:
434     case Bytecodes::_f2l: {
435       Label done;
436       FloatRegister Rsrc = src->as_float_reg();
437       Register Rdst = (op->bytecode() == Bytecodes::_f2i ? dest->as_register() : dest->as_register_lo());
438       __ clear_reg(Rdst, true, false);
439       __ z_cebr(Rsrc, Rsrc);
440       __ z_brno(done); // NaN -> 0
441       if (op->bytecode() == Bytecodes::_f2i) {
442         __ z_cfebr(Rdst, Rsrc, Assembler::to_zero);
443       } else { // op->bytecode() == Bytecodes::_f2l
444         __ z_cgebr(Rdst, Rsrc, Assembler::to_zero);
445       }
446       __ bind(done);
447     }
448     break;
449 
450     case Bytecodes::_d2i:
451     case Bytecodes::_d2l: {
452       Label done;
453       FloatRegister Rsrc = src->as_double_reg();
454       Register Rdst = (op->bytecode() == Bytecodes::_d2i ? dest->as_register() : dest->as_register_lo());
455       __ clear_reg(Rdst, true, false);  // Don't set CC.
456       __ z_cdbr(Rsrc, Rsrc);
457       __ z_brno(done); // NaN -> 0
458       if (op->bytecode() == Bytecodes::_d2i) {
459         __ z_cfdbr(Rdst, Rsrc, Assembler::to_zero);
460       } else { // Bytecodes::_d2l
461         __ z_cgdbr(Rdst, Rsrc, Assembler::to_zero);
462       }
463       __ bind(done);
464     }
465     break;
466 
467     default: ShouldNotReachHere();
468   }
469 }
470 
align_call(LIR_Code code)471 void LIR_Assembler::align_call(LIR_Code code) {
472   // End of call instruction must be 4 byte aligned.
473   int offset = __ offset();
474   switch (code) {
475     case lir_icvirtual_call:
476       offset += MacroAssembler::load_const_from_toc_size();
477       // no break
478     case lir_static_call:
479     case lir_optvirtual_call:
480     case lir_dynamic_call:
481       offset += NativeCall::call_far_pcrelative_displacement_offset;
482       break;
483     case lir_virtual_call:   // currently, sparc-specific for niagara
484     default: ShouldNotReachHere();
485   }
486   if ((offset & (NativeCall::call_far_pcrelative_displacement_alignment-1)) != 0) {
487     __ nop();
488   }
489 }
490 
call(LIR_OpJavaCall * op,relocInfo::relocType rtype)491 void LIR_Assembler::call(LIR_OpJavaCall* op, relocInfo::relocType rtype) {
492   assert((__ offset() + NativeCall::call_far_pcrelative_displacement_offset) % NativeCall::call_far_pcrelative_displacement_alignment == 0,
493          "must be aligned (offset=%d)", __ offset());
494   assert(rtype == relocInfo::none ||
495          rtype == relocInfo::opt_virtual_call_type ||
496          rtype == relocInfo::static_call_type, "unexpected rtype");
497   // Prepend each BRASL with a nop.
498   __ relocate(rtype);
499   __ z_nop();
500   __ z_brasl(Z_R14, op->addr());
501   add_call_info(code_offset(), op->info());
502 }
503 
ic_call(LIR_OpJavaCall * op)504 void LIR_Assembler::ic_call(LIR_OpJavaCall* op) {
505   address virtual_call_oop_addr = NULL;
506   AddressLiteral empty_ic((address) Universe::non_oop_word());
507   virtual_call_oop_addr = __ pc();
508   bool success = __ load_const_from_toc(Z_inline_cache, empty_ic);
509   if (!success) {
510     bailout("const section overflow");
511     return;
512   }
513 
514   // CALL to fixup routine. Fixup routine uses ScopeDesc info
515   // to determine who we intended to call.
516   __ relocate(virtual_call_Relocation::spec(virtual_call_oop_addr));
517   call(op, relocInfo::none);
518 }
519 
520 // not supported
vtable_call(LIR_OpJavaCall * op)521 void LIR_Assembler::vtable_call(LIR_OpJavaCall* op) {
522   ShouldNotReachHere();
523 }
524 
move_regs(Register from_reg,Register to_reg)525 void LIR_Assembler::move_regs(Register from_reg, Register to_reg) {
526   if (from_reg != to_reg) __ z_lgr(to_reg, from_reg);
527 }
528 
const2stack(LIR_Opr src,LIR_Opr dest)529 void LIR_Assembler::const2stack(LIR_Opr src, LIR_Opr dest) {
530   assert(src->is_constant(), "should not call otherwise");
531   assert(dest->is_stack(), "should not call otherwise");
532   LIR_Const* c = src->as_constant_ptr();
533 
534   unsigned int lmem = 0;
535   unsigned int lcon = 0;
536   int64_t cbits = 0;
537   Address dest_addr;
538   switch (c->type()) {
539     case T_INT:  // fall through
540     case T_FLOAT:
541       dest_addr = frame_map()->address_for_slot(dest->single_stack_ix());
542       lmem = 4; lcon = 4; cbits = c->as_jint_bits();
543       break;
544 
545     case T_ADDRESS:
546       dest_addr = frame_map()->address_for_slot(dest->single_stack_ix());
547       lmem = 8; lcon = 4; cbits = c->as_jint_bits();
548       break;
549 
550     case T_OBJECT:
551       dest_addr = frame_map()->address_for_slot(dest->single_stack_ix());
552       if (c->as_jobject() == NULL) {
553         __ store_const(dest_addr, (int64_t)NULL_WORD, 8, 8);
554       } else {
555         jobject2reg(c->as_jobject(), Z_R1_scratch);
556         __ reg2mem_opt(Z_R1_scratch, dest_addr, true);
557       }
558       return;
559 
560     case T_LONG:  // fall through
561     case T_DOUBLE:
562       dest_addr = frame_map()->address_for_slot(dest->double_stack_ix());
563       lmem = 8; lcon = 8; cbits = (int64_t)(c->as_jlong_bits());
564       break;
565 
566     default:
567       ShouldNotReachHere();
568   }
569 
570   __ store_const(dest_addr, cbits, lmem, lcon);
571 }
572 
const2mem(LIR_Opr src,LIR_Opr dest,BasicType type,CodeEmitInfo * info,bool wide)573 void LIR_Assembler::const2mem(LIR_Opr src, LIR_Opr dest, BasicType type, CodeEmitInfo* info, bool wide) {
574   assert(src->is_constant(), "should not call otherwise");
575   assert(dest->is_address(), "should not call otherwise");
576 
577   LIR_Const* c = src->as_constant_ptr();
578   Address addr = as_Address(dest->as_address_ptr());
579 
580   int store_offset = -1;
581 
582   if (dest->as_address_ptr()->index()->is_valid()) {
583     switch (type) {
584       case T_INT:    // fall through
585       case T_FLOAT:
586         __ load_const_optimized(Z_R0_scratch, c->as_jint_bits());
587         store_offset = __ offset();
588         if (Immediate::is_uimm12(addr.disp())) {
589           __ z_st(Z_R0_scratch, addr);
590         } else {
591           __ z_sty(Z_R0_scratch, addr);
592         }
593         break;
594 
595       case T_ADDRESS:
596         __ load_const_optimized(Z_R1_scratch, c->as_jint_bits());
597         store_offset = __ reg2mem_opt(Z_R1_scratch, addr, true);
598         break;
599 
600       case T_OBJECT:  // fall through
601       case T_ARRAY:
602         if (c->as_jobject() == NULL) {
603           if (UseCompressedOops && !wide) {
604             __ clear_reg(Z_R1_scratch, false);
605             store_offset = __ reg2mem_opt(Z_R1_scratch, addr, false);
606           } else {
607             __ clear_reg(Z_R1_scratch, true);
608             store_offset = __ reg2mem_opt(Z_R1_scratch, addr, true);
609           }
610         } else {
611           jobject2reg(c->as_jobject(), Z_R1_scratch);
612           if (UseCompressedOops && !wide) {
613             __ encode_heap_oop(Z_R1_scratch);
614             store_offset = __ reg2mem_opt(Z_R1_scratch, addr, false);
615           } else {
616             store_offset = __ reg2mem_opt(Z_R1_scratch, addr, true);
617           }
618         }
619         assert(store_offset >= 0, "check");
620         break;
621 
622       case T_LONG:    // fall through
623       case T_DOUBLE:
624         __ load_const_optimized(Z_R1_scratch, (int64_t)(c->as_jlong_bits()));
625         store_offset = __ reg2mem_opt(Z_R1_scratch, addr, true);
626         break;
627 
628       case T_BOOLEAN: // fall through
629       case T_BYTE:
630         __ load_const_optimized(Z_R0_scratch, (int8_t)(c->as_jint()));
631         store_offset = __ offset();
632         if (Immediate::is_uimm12(addr.disp())) {
633           __ z_stc(Z_R0_scratch, addr);
634         } else {
635           __ z_stcy(Z_R0_scratch, addr);
636         }
637         break;
638 
639       case T_CHAR:    // fall through
640       case T_SHORT:
641         __ load_const_optimized(Z_R0_scratch, (int16_t)(c->as_jint()));
642         store_offset = __ offset();
643         if (Immediate::is_uimm12(addr.disp())) {
644           __ z_sth(Z_R0_scratch, addr);
645         } else {
646           __ z_sthy(Z_R0_scratch, addr);
647         }
648         break;
649 
650       default:
651         ShouldNotReachHere();
652     }
653 
654   } else { // no index
655 
656     unsigned int lmem = 0;
657     unsigned int lcon = 0;
658     int64_t cbits = 0;
659 
660     switch (type) {
661       case T_INT:    // fall through
662       case T_FLOAT:
663         lmem = 4; lcon = 4; cbits = c->as_jint_bits();
664         break;
665 
666       case T_ADDRESS:
667         lmem = 8; lcon = 4; cbits = c->as_jint_bits();
668         break;
669 
670       case T_OBJECT:  // fall through
671       case T_ARRAY:
672         if (c->as_jobject() == NULL) {
673           if (UseCompressedOops && !wide) {
674             store_offset = __ store_const(addr, (int32_t)NULL_WORD, 4, 4);
675           } else {
676             store_offset = __ store_const(addr, (int64_t)NULL_WORD, 8, 8);
677           }
678         } else {
679           jobject2reg(c->as_jobject(), Z_R1_scratch);
680           if (UseCompressedOops && !wide) {
681             __ encode_heap_oop(Z_R1_scratch);
682             store_offset = __ reg2mem_opt(Z_R1_scratch, addr, false);
683           } else {
684             store_offset = __ reg2mem_opt(Z_R1_scratch, addr, true);
685           }
686         }
687         assert(store_offset >= 0, "check");
688         break;
689 
690       case T_LONG:    // fall through
691       case T_DOUBLE:
692         lmem = 8; lcon = 8; cbits = (int64_t)(c->as_jlong_bits());
693         break;
694 
695       case T_BOOLEAN: // fall through
696       case T_BYTE:
697         lmem = 1; lcon = 1; cbits = (int8_t)(c->as_jint());
698         break;
699 
700       case T_CHAR:    // fall through
701       case T_SHORT:
702         lmem = 2; lcon = 2; cbits = (int16_t)(c->as_jint());
703         break;
704 
705       default:
706         ShouldNotReachHere();
707     }
708 
709     if (store_offset == -1) {
710       store_offset = __ store_const(addr, cbits, lmem, lcon);
711       assert(store_offset >= 0, "check");
712     }
713   }
714 
715   if (info != NULL) {
716     add_debug_info_for_null_check(store_offset, info);
717   }
718 }
719 
const2reg(LIR_Opr src,LIR_Opr dest,LIR_PatchCode patch_code,CodeEmitInfo * info)720 void LIR_Assembler::const2reg(LIR_Opr src, LIR_Opr dest, LIR_PatchCode patch_code, CodeEmitInfo* info) {
721   assert(src->is_constant(), "should not call otherwise");
722   assert(dest->is_register(), "should not call otherwise");
723   LIR_Const* c = src->as_constant_ptr();
724 
725   switch (c->type()) {
726     case T_INT: {
727       assert(patch_code == lir_patch_none, "no patching handled here");
728       __ load_const_optimized(dest->as_register(), c->as_jint());
729       break;
730     }
731 
732     case T_ADDRESS: {
733       assert(patch_code == lir_patch_none, "no patching handled here");
734       __ load_const_optimized(dest->as_register(), c->as_jint());
735       break;
736     }
737 
738     case T_LONG: {
739       assert(patch_code == lir_patch_none, "no patching handled here");
740       __ load_const_optimized(dest->as_register_lo(), (intptr_t)c->as_jlong());
741       break;
742     }
743 
744     case T_OBJECT: {
745       if (patch_code != lir_patch_none) {
746         jobject2reg_with_patching(dest->as_register(), info);
747       } else {
748         jobject2reg(c->as_jobject(), dest->as_register());
749       }
750       break;
751     }
752 
753     case T_METADATA: {
754       if (patch_code != lir_patch_none) {
755         klass2reg_with_patching(dest->as_register(), info);
756       } else {
757         metadata2reg(c->as_metadata(), dest->as_register());
758       }
759       break;
760     }
761 
762     case T_FLOAT: {
763       Register toc_reg = Z_R1_scratch;
764       __ load_toc(toc_reg);
765       address const_addr = __ float_constant(c->as_jfloat());
766       if (const_addr == NULL) {
767         bailout("const section overflow");
768         break;
769       }
770       int displ = const_addr - _masm->code()->consts()->start();
771       if (dest->is_single_fpu()) {
772         __ z_ley(dest->as_float_reg(), displ, toc_reg);
773       } else {
774         assert(dest->is_single_cpu(), "Must be a cpu register.");
775         __ z_ly(dest->as_register(), displ, toc_reg);
776       }
777     }
778     break;
779 
780     case T_DOUBLE: {
781       Register toc_reg = Z_R1_scratch;
782       __ load_toc(toc_reg);
783       address const_addr = __ double_constant(c->as_jdouble());
784       if (const_addr == NULL) {
785         bailout("const section overflow");
786         break;
787       }
788       int displ = const_addr - _masm->code()->consts()->start();
789       if (dest->is_double_fpu()) {
790         __ z_ldy(dest->as_double_reg(), displ, toc_reg);
791       } else {
792         assert(dest->is_double_cpu(), "Must be a long register.");
793         __ z_lg(dest->as_register_lo(), displ, toc_reg);
794       }
795     }
796     break;
797 
798     default:
799       ShouldNotReachHere();
800   }
801 }
802 
as_Address(LIR_Address * addr)803 Address LIR_Assembler::as_Address(LIR_Address* addr) {
804   if (addr->base()->is_illegal()) {
805     Unimplemented();
806   }
807 
808   Register base = addr->base()->as_pointer_register();
809 
810   if (addr->index()->is_illegal()) {
811     return Address(base, addr->disp());
812   } else if (addr->index()->is_cpu_register()) {
813     Register index = addr->index()->as_pointer_register();
814     return Address(base, index, addr->disp());
815   } else if (addr->index()->is_constant()) {
816     intptr_t addr_offset = addr->index()->as_constant_ptr()->as_jint() + addr->disp();
817     return Address(base, addr_offset);
818   } else {
819     ShouldNotReachHere();
820     return Address();
821   }
822 }
823 
stack2stack(LIR_Opr src,LIR_Opr dest,BasicType type)824 void LIR_Assembler::stack2stack(LIR_Opr src, LIR_Opr dest, BasicType type) {
825   switch (type) {
826     case T_INT:
827     case T_FLOAT: {
828       Register tmp = Z_R1_scratch;
829       Address from = frame_map()->address_for_slot(src->single_stack_ix());
830       Address to   = frame_map()->address_for_slot(dest->single_stack_ix());
831       __ mem2reg_opt(tmp, from, false);
832       __ reg2mem_opt(tmp, to, false);
833       break;
834     }
835     case T_ADDRESS:
836     case T_OBJECT: {
837       Register tmp = Z_R1_scratch;
838       Address from = frame_map()->address_for_slot(src->single_stack_ix());
839       Address to   = frame_map()->address_for_slot(dest->single_stack_ix());
840       __ mem2reg_opt(tmp, from, true);
841       __ reg2mem_opt(tmp, to, true);
842       break;
843     }
844     case T_LONG:
845     case T_DOUBLE: {
846       Register tmp = Z_R1_scratch;
847       Address from = frame_map()->address_for_double_slot(src->double_stack_ix());
848       Address to   = frame_map()->address_for_double_slot(dest->double_stack_ix());
849       __ mem2reg_opt(tmp, from, true);
850       __ reg2mem_opt(tmp, to, true);
851       break;
852     }
853 
854     default:
855       ShouldNotReachHere();
856   }
857 }
858 
859 // 4-byte accesses only! Don't use it to access 8 bytes!
as_Address_hi(LIR_Address * addr)860 Address LIR_Assembler::as_Address_hi(LIR_Address* addr) {
861   ShouldNotCallThis();
862   return 0; // unused
863 }
864 
865 // 4-byte accesses only! Don't use it to access 8 bytes!
as_Address_lo(LIR_Address * addr)866 Address LIR_Assembler::as_Address_lo(LIR_Address* addr) {
867   ShouldNotCallThis();
868   return 0; // unused
869 }
870 
mem2reg(LIR_Opr src_opr,LIR_Opr dest,BasicType type,LIR_PatchCode patch_code,CodeEmitInfo * info,bool wide,bool unaligned)871 void LIR_Assembler::mem2reg(LIR_Opr src_opr, LIR_Opr dest, BasicType type, LIR_PatchCode patch_code,
872                             CodeEmitInfo* info, bool wide, bool unaligned) {
873 
874   assert(type != T_METADATA, "load of metadata ptr not supported");
875   LIR_Address* addr = src_opr->as_address_ptr();
876   LIR_Opr to_reg = dest;
877 
878   Register src = addr->base()->as_pointer_register();
879   Register disp_reg = Z_R0;
880   int disp_value = addr->disp();
881   bool needs_patching = (patch_code != lir_patch_none);
882 
883   if (addr->base()->type() == T_OBJECT) {
884     __ verify_oop(src);
885   }
886 
887   PatchingStub* patch = NULL;
888   if (needs_patching) {
889     patch = new PatchingStub(_masm, PatchingStub::access_field_id);
890     assert(!to_reg->is_double_cpu() ||
891            patch_code == lir_patch_none ||
892            patch_code == lir_patch_normal, "patching doesn't match register");
893   }
894 
895   if (addr->index()->is_illegal()) {
896     if (!Immediate::is_simm20(disp_value)) {
897       if (needs_patching) {
898         __ load_const(Z_R1_scratch, (intptr_t)0);
899       } else {
900         __ load_const_optimized(Z_R1_scratch, disp_value);
901       }
902       disp_reg = Z_R1_scratch;
903       disp_value = 0;
904     }
905   } else {
906     if (!Immediate::is_simm20(disp_value)) {
907       __ load_const_optimized(Z_R1_scratch, disp_value);
908       __ z_la(Z_R1_scratch, 0, Z_R1_scratch, addr->index()->as_register());
909       disp_reg = Z_R1_scratch;
910       disp_value = 0;
911     }
912     disp_reg = addr->index()->as_pointer_register();
913   }
914 
915   // Remember the offset of the load. The patching_epilog must be done
916   // before the call to add_debug_info, otherwise the PcDescs don't get
917   // entered in increasing order.
918   int offset = code_offset();
919 
920   assert(disp_reg != Z_R0 || Immediate::is_simm20(disp_value), "should have set this up");
921 
922   bool short_disp = Immediate::is_uimm12(disp_value);
923 
924   switch (type) {
925     case T_BOOLEAN: // fall through
926     case T_BYTE  :  __ z_lb(dest->as_register(),   disp_value, disp_reg, src); break;
927     case T_CHAR  :  __ z_llgh(dest->as_register(), disp_value, disp_reg, src); break;
928     case T_SHORT :
929       if (short_disp) {
930                     __ z_lh(dest->as_register(),   disp_value, disp_reg, src);
931       } else {
932                     __ z_lhy(dest->as_register(),  disp_value, disp_reg, src);
933       }
934       break;
935     case T_INT   :
936       if (short_disp) {
937                     __ z_l(dest->as_register(),    disp_value, disp_reg, src);
938       } else {
939                     __ z_ly(dest->as_register(),   disp_value, disp_reg, src);
940       }
941       break;
942     case T_ADDRESS:
943       if (UseCompressedClassPointers && addr->disp() == oopDesc::klass_offset_in_bytes()) {
944         __ z_llgf(dest->as_register(), disp_value, disp_reg, src);
945         __ decode_klass_not_null(dest->as_register());
946       } else {
947         __ z_lg(dest->as_register(), disp_value, disp_reg, src);
948       }
949       break;
950     case T_ARRAY : // fall through
951     case T_OBJECT:
952     {
953       if (UseCompressedOops && !wide) {
954         __ z_llgf(dest->as_register(), disp_value, disp_reg, src);
955         __ oop_decoder(dest->as_register(), dest->as_register(), true);
956       } else {
957         __ z_lg(dest->as_register(), disp_value, disp_reg, src);
958       }
959       break;
960     }
961     case T_FLOAT:
962       if (short_disp) {
963                     __ z_le(dest->as_float_reg(),  disp_value, disp_reg, src);
964       } else {
965                     __ z_ley(dest->as_float_reg(), disp_value, disp_reg, src);
966       }
967       break;
968     case T_DOUBLE:
969       if (short_disp) {
970                     __ z_ld(dest->as_double_reg(),  disp_value, disp_reg, src);
971       } else {
972                     __ z_ldy(dest->as_double_reg(), disp_value, disp_reg, src);
973       }
974       break;
975     case T_LONG  :  __ z_lg(dest->as_register_lo(), disp_value, disp_reg, src); break;
976     default      : ShouldNotReachHere();
977   }
978   if (type == T_ARRAY || type == T_OBJECT) {
979     __ verify_oop(dest->as_register());
980   }
981 
982   if (patch != NULL) {
983     patching_epilog(patch, patch_code, src, info);
984   }
985   if (info != NULL) add_debug_info_for_null_check(offset, info);
986 }
987 
stack2reg(LIR_Opr src,LIR_Opr dest,BasicType type)988 void LIR_Assembler::stack2reg(LIR_Opr src, LIR_Opr dest, BasicType type) {
989   assert(src->is_stack(), "should not call otherwise");
990   assert(dest->is_register(), "should not call otherwise");
991 
992   if (dest->is_single_cpu()) {
993     if (type == T_ARRAY || type == T_OBJECT) {
994       __ mem2reg_opt(dest->as_register(), frame_map()->address_for_slot(src->single_stack_ix()), true);
995       __ verify_oop(dest->as_register());
996     } else if (type == T_METADATA || type == T_ADDRESS) {
997       __ mem2reg_opt(dest->as_register(), frame_map()->address_for_slot(src->single_stack_ix()), true);
998     } else {
999       __ mem2reg_opt(dest->as_register(), frame_map()->address_for_slot(src->single_stack_ix()), false);
1000     }
1001   } else if (dest->is_double_cpu()) {
1002     Address src_addr_LO = frame_map()->address_for_slot(src->double_stack_ix());
1003     __ mem2reg_opt(dest->as_register_lo(), src_addr_LO, true);
1004   } else if (dest->is_single_fpu()) {
1005     Address src_addr = frame_map()->address_for_slot(src->single_stack_ix());
1006     __ mem2freg_opt(dest->as_float_reg(), src_addr, false);
1007   } else if (dest->is_double_fpu()) {
1008     Address src_addr = frame_map()->address_for_slot(src->double_stack_ix());
1009     __ mem2freg_opt(dest->as_double_reg(), src_addr, true);
1010   } else {
1011     ShouldNotReachHere();
1012   }
1013 }
1014 
reg2stack(LIR_Opr src,LIR_Opr dest,BasicType type,bool pop_fpu_stack)1015 void LIR_Assembler::reg2stack(LIR_Opr src, LIR_Opr dest, BasicType type, bool pop_fpu_stack) {
1016   assert(src->is_register(), "should not call otherwise");
1017   assert(dest->is_stack(), "should not call otherwise");
1018 
1019   if (src->is_single_cpu()) {
1020     const Address dst = frame_map()->address_for_slot(dest->single_stack_ix());
1021     if (type == T_OBJECT || type == T_ARRAY) {
1022       __ verify_oop(src->as_register());
1023       __ reg2mem_opt(src->as_register(), dst, true);
1024     } else if (type == T_METADATA || type == T_ADDRESS) {
1025       __ reg2mem_opt(src->as_register(), dst, true);
1026     } else {
1027       __ reg2mem_opt(src->as_register(), dst, false);
1028     }
1029   } else if (src->is_double_cpu()) {
1030     Address dstLO = frame_map()->address_for_slot(dest->double_stack_ix());
1031     __ reg2mem_opt(src->as_register_lo(), dstLO, true);
1032   } else if (src->is_single_fpu()) {
1033     Address dst_addr = frame_map()->address_for_slot(dest->single_stack_ix());
1034     __ freg2mem_opt(src->as_float_reg(), dst_addr, false);
1035   } else if (src->is_double_fpu()) {
1036     Address dst_addr = frame_map()->address_for_slot(dest->double_stack_ix());
1037     __ freg2mem_opt(src->as_double_reg(), dst_addr, true);
1038   } else {
1039     ShouldNotReachHere();
1040   }
1041 }
1042 
reg2reg(LIR_Opr from_reg,LIR_Opr to_reg)1043 void LIR_Assembler::reg2reg(LIR_Opr from_reg, LIR_Opr to_reg) {
1044   if (from_reg->is_float_kind() && to_reg->is_float_kind()) {
1045     if (from_reg->is_double_fpu()) {
1046       // double to double moves
1047       assert(to_reg->is_double_fpu(), "should match");
1048       __ z_ldr(to_reg->as_double_reg(), from_reg->as_double_reg());
1049     } else {
1050       // float to float moves
1051       assert(to_reg->is_single_fpu(), "should match");
1052       __ z_ler(to_reg->as_float_reg(), from_reg->as_float_reg());
1053     }
1054   } else if (!from_reg->is_float_kind() && !to_reg->is_float_kind()) {
1055     if (from_reg->is_double_cpu()) {
1056       __ z_lgr(to_reg->as_pointer_register(), from_reg->as_pointer_register());
1057     } else if (to_reg->is_double_cpu()) {
1058       // int to int moves
1059       __ z_lgr(to_reg->as_register_lo(), from_reg->as_register());
1060     } else {
1061       // int to int moves
1062       __ z_lgr(to_reg->as_register(), from_reg->as_register());
1063     }
1064   } else {
1065     ShouldNotReachHere();
1066   }
1067   if (to_reg->type() == T_OBJECT || to_reg->type() == T_ARRAY) {
1068     __ verify_oop(to_reg->as_register());
1069   }
1070 }
1071 
reg2mem(LIR_Opr from,LIR_Opr dest_opr,BasicType type,LIR_PatchCode patch_code,CodeEmitInfo * info,bool pop_fpu_stack,bool wide,bool unaligned)1072 void LIR_Assembler::reg2mem(LIR_Opr from, LIR_Opr dest_opr, BasicType type,
1073                             LIR_PatchCode patch_code, CodeEmitInfo* info, bool pop_fpu_stack,
1074                             bool wide, bool unaligned) {
1075   assert(type != T_METADATA, "store of metadata ptr not supported");
1076   LIR_Address* addr = dest_opr->as_address_ptr();
1077 
1078   Register dest = addr->base()->as_pointer_register();
1079   Register disp_reg = Z_R0;
1080   int disp_value = addr->disp();
1081   bool needs_patching = (patch_code != lir_patch_none);
1082 
1083   if (addr->base()->is_oop_register()) {
1084     __ verify_oop(dest);
1085   }
1086 
1087   PatchingStub* patch = NULL;
1088   if (needs_patching) {
1089     patch = new PatchingStub(_masm, PatchingStub::access_field_id);
1090     assert(!from->is_double_cpu() ||
1091            patch_code == lir_patch_none ||
1092            patch_code == lir_patch_normal, "patching doesn't match register");
1093   }
1094 
1095   assert(!needs_patching || (!Immediate::is_simm20(disp_value) && addr->index()->is_illegal()), "assumption");
1096   if (addr->index()->is_illegal()) {
1097     if (!Immediate::is_simm20(disp_value)) {
1098       if (needs_patching) {
1099         __ load_const(Z_R1_scratch, (intptr_t)0);
1100       } else {
1101         __ load_const_optimized(Z_R1_scratch, disp_value);
1102       }
1103       disp_reg = Z_R1_scratch;
1104       disp_value = 0;
1105     }
1106   } else {
1107     if (!Immediate::is_simm20(disp_value)) {
1108       __ load_const_optimized(Z_R1_scratch, disp_value);
1109       __ z_la(Z_R1_scratch, 0, Z_R1_scratch, addr->index()->as_register());
1110       disp_reg = Z_R1_scratch;
1111       disp_value = 0;
1112     }
1113     disp_reg = addr->index()->as_pointer_register();
1114   }
1115 
1116   assert(disp_reg != Z_R0 || Immediate::is_simm20(disp_value), "should have set this up");
1117 
1118   if (type == T_ARRAY || type == T_OBJECT) {
1119     __ verify_oop(from->as_register());
1120   }
1121 
1122   bool short_disp = Immediate::is_uimm12(disp_value);
1123 
1124   // Remember the offset of the store. The patching_epilog must be done
1125   // before the call to add_debug_info_for_null_check, otherwise the PcDescs don't get
1126   // entered in increasing order.
1127   int offset = code_offset();
1128   switch (type) {
1129     case T_BOOLEAN: // fall through
1130     case T_BYTE  :
1131       if (short_disp) {
1132                     __ z_stc(from->as_register(),  disp_value, disp_reg, dest);
1133       } else {
1134                     __ z_stcy(from->as_register(), disp_value, disp_reg, dest);
1135       }
1136       break;
1137     case T_CHAR  : // fall through
1138     case T_SHORT :
1139       if (short_disp) {
1140                     __ z_sth(from->as_register(),  disp_value, disp_reg, dest);
1141       } else {
1142                     __ z_sthy(from->as_register(), disp_value, disp_reg, dest);
1143       }
1144       break;
1145     case T_INT   :
1146       if (short_disp) {
1147                     __ z_st(from->as_register(),  disp_value, disp_reg, dest);
1148       } else {
1149                     __ z_sty(from->as_register(), disp_value, disp_reg, dest);
1150       }
1151       break;
1152     case T_LONG  :  __ z_stg(from->as_register_lo(), disp_value, disp_reg, dest); break;
1153     case T_ADDRESS: __ z_stg(from->as_register(),    disp_value, disp_reg, dest); break;
1154       break;
1155     case T_ARRAY : // fall through
1156     case T_OBJECT:
1157       {
1158         if (UseCompressedOops && !wide) {
1159           Register compressed_src = Z_R14;
1160           __ oop_encoder(compressed_src, from->as_register(), true, (disp_reg != Z_R1) ? Z_R1 : Z_R0, -1, true);
1161           offset = code_offset();
1162           if (short_disp) {
1163             __ z_st(compressed_src,  disp_value, disp_reg, dest);
1164           } else {
1165             __ z_sty(compressed_src, disp_value, disp_reg, dest);
1166           }
1167         } else {
1168           __ z_stg(from->as_register(), disp_value, disp_reg, dest);
1169         }
1170         break;
1171       }
1172     case T_FLOAT :
1173       if (short_disp) {
1174         __ z_ste(from->as_float_reg(),  disp_value, disp_reg, dest);
1175       } else {
1176         __ z_stey(from->as_float_reg(), disp_value, disp_reg, dest);
1177       }
1178       break;
1179     case T_DOUBLE:
1180       if (short_disp) {
1181         __ z_std(from->as_double_reg(),  disp_value, disp_reg, dest);
1182       } else {
1183         __ z_stdy(from->as_double_reg(), disp_value, disp_reg, dest);
1184       }
1185       break;
1186     default: ShouldNotReachHere();
1187   }
1188 
1189   if (patch != NULL) {
1190     patching_epilog(patch, patch_code, dest, info);
1191   }
1192 
1193   if (info != NULL) add_debug_info_for_null_check(offset, info);
1194 }
1195 
1196 
return_op(LIR_Opr result)1197 void LIR_Assembler::return_op(LIR_Opr result) {
1198   assert(result->is_illegal() ||
1199          (result->is_single_cpu() && result->as_register() == Z_R2) ||
1200          (result->is_double_cpu() && result->as_register_lo() == Z_R2) ||
1201          (result->is_single_fpu() && result->as_float_reg() == Z_F0) ||
1202          (result->is_double_fpu() && result->as_double_reg() == Z_F0), "convention");
1203 
1204   if (SafepointMechanism::uses_thread_local_poll()) {
1205     __ z_lg(Z_R1_scratch, Address(Z_thread, Thread::polling_page_offset()));
1206   } else {
1207     AddressLiteral pp(os::get_polling_page());
1208     __ load_const_optimized(Z_R1_scratch, pp);
1209   }
1210 
1211   // Pop the frame before the safepoint code.
1212   __ pop_frame_restore_retPC(initial_frame_size_in_bytes());
1213 
1214   if (StackReservedPages > 0 && compilation()->has_reserved_stack_access()) {
1215     __ reserved_stack_check(Z_R14);
1216   }
1217 
1218   // We need to mark the code position where the load from the safepoint
1219   // polling page was emitted as relocInfo::poll_return_type here.
1220   __ relocate(relocInfo::poll_return_type);
1221   __ load_from_polling_page(Z_R1_scratch);
1222 
1223   __ z_br(Z_R14); // Return to caller.
1224 }
1225 
safepoint_poll(LIR_Opr tmp,CodeEmitInfo * info)1226 int LIR_Assembler::safepoint_poll(LIR_Opr tmp, CodeEmitInfo* info) {
1227   const Register poll_addr = tmp->as_register_lo();
1228   if (SafepointMechanism::uses_thread_local_poll()) {
1229     __ z_lg(poll_addr, Address(Z_thread, Thread::polling_page_offset()));
1230   } else {
1231     AddressLiteral pp(os::get_polling_page());
1232     __ load_const_optimized(poll_addr, pp);
1233   }
1234   guarantee(info != NULL, "Shouldn't be NULL");
1235   add_debug_info_for_branch(info);
1236   int offset = __ offset();
1237   __ relocate(relocInfo::poll_type);
1238   __ load_from_polling_page(poll_addr);
1239   return offset;
1240 }
1241 
emit_static_call_stub()1242 void LIR_Assembler::emit_static_call_stub() {
1243 
1244   // Stub is fixed up when the corresponding call is converted from calling
1245   // compiled code to calling interpreted code.
1246 
1247   address call_pc = __ pc();
1248   address stub = __ start_a_stub(call_stub_size());
1249   if (stub == NULL) {
1250     bailout("static call stub overflow");
1251     return;
1252   }
1253 
1254   int start = __ offset();
1255 
1256   __ relocate(static_stub_Relocation::spec(call_pc));
1257 
1258   // See also Matcher::interpreter_method_oop_reg().
1259   AddressLiteral meta = __ allocate_metadata_address(NULL);
1260   bool success = __ load_const_from_toc(Z_method, meta);
1261 
1262   __ set_inst_mark();
1263   AddressLiteral a((address)-1);
1264   success = success && __ load_const_from_toc(Z_R1, a);
1265   if (!success) {
1266     bailout("const section overflow");
1267     return;
1268   }
1269 
1270   __ z_br(Z_R1);
1271   assert(__ offset() - start <= call_stub_size(), "stub too big");
1272   __ end_a_stub(); // Update current stubs pointer and restore insts_end.
1273 }
1274 
comp_op(LIR_Condition condition,LIR_Opr opr1,LIR_Opr opr2,LIR_Op2 * op)1275 void LIR_Assembler::comp_op(LIR_Condition condition, LIR_Opr opr1, LIR_Opr opr2, LIR_Op2* op) {
1276   bool unsigned_comp = condition == lir_cond_belowEqual || condition == lir_cond_aboveEqual;
1277   if (opr1->is_single_cpu()) {
1278     Register reg1 = opr1->as_register();
1279     if (opr2->is_single_cpu()) {
1280       // cpu register - cpu register
1281       if (opr1->type() == T_OBJECT || opr1->type() == T_ARRAY) {
1282         __ z_clgr(reg1, opr2->as_register());
1283       } else {
1284         assert(opr2->type() != T_OBJECT && opr2->type() != T_ARRAY, "cmp int, oop?");
1285         if (unsigned_comp) {
1286           __ z_clr(reg1, opr2->as_register());
1287         } else {
1288           __ z_cr(reg1, opr2->as_register());
1289         }
1290       }
1291     } else if (opr2->is_stack()) {
1292       // cpu register - stack
1293       if (opr1->type() == T_OBJECT || opr1->type() == T_ARRAY) {
1294         __ z_cg(reg1, frame_map()->address_for_slot(opr2->single_stack_ix()));
1295       } else {
1296         if (unsigned_comp) {
1297           __ z_cly(reg1, frame_map()->address_for_slot(opr2->single_stack_ix()));
1298         } else {
1299           __ z_cy(reg1, frame_map()->address_for_slot(opr2->single_stack_ix()));
1300         }
1301       }
1302     } else if (opr2->is_constant()) {
1303       // cpu register - constant
1304       LIR_Const* c = opr2->as_constant_ptr();
1305       if (c->type() == T_INT) {
1306         if (unsigned_comp) {
1307           __ z_clfi(reg1, c->as_jint());
1308         } else {
1309           __ z_cfi(reg1, c->as_jint());
1310         }
1311       } else if (c->type() == T_METADATA) {
1312         // We only need, for now, comparison with NULL for metadata.
1313         assert(condition == lir_cond_equal || condition == lir_cond_notEqual, "oops");
1314         Metadata* m = c->as_metadata();
1315         if (m == NULL) {
1316           __ z_cghi(reg1, 0);
1317         } else {
1318           ShouldNotReachHere();
1319         }
1320       } else if (c->type() == T_OBJECT || c->type() == T_ARRAY) {
1321         // In 64bit oops are single register.
1322         jobject o = c->as_jobject();
1323         if (o == NULL) {
1324           __ z_ltgr(reg1, reg1);
1325         } else {
1326           jobject2reg(o, Z_R1_scratch);
1327           __ z_cgr(reg1, Z_R1_scratch);
1328         }
1329       } else {
1330         fatal("unexpected type: %s", basictype_to_str(c->type()));
1331       }
1332       // cpu register - address
1333     } else if (opr2->is_address()) {
1334       if (op->info() != NULL) {
1335         add_debug_info_for_null_check_here(op->info());
1336       }
1337       if (unsigned_comp) {
1338         __ z_cly(reg1, as_Address(opr2->as_address_ptr()));
1339       } else {
1340         __ z_cy(reg1, as_Address(opr2->as_address_ptr()));
1341       }
1342     } else {
1343       ShouldNotReachHere();
1344     }
1345 
1346   } else if (opr1->is_double_cpu()) {
1347     assert(!unsigned_comp, "unexpected");
1348     Register xlo = opr1->as_register_lo();
1349     Register xhi = opr1->as_register_hi();
1350     if (opr2->is_double_cpu()) {
1351       __ z_cgr(xlo, opr2->as_register_lo());
1352     } else if (opr2->is_constant()) {
1353       // cpu register - constant 0
1354       assert(opr2->as_jlong() == (jlong)0, "only handles zero");
1355       __ z_ltgr(xlo, xlo);
1356     } else {
1357       ShouldNotReachHere();
1358     }
1359 
1360   } else if (opr1->is_single_fpu()) {
1361     if (opr2->is_single_fpu()) {
1362       __ z_cebr(opr1->as_float_reg(), opr2->as_float_reg());
1363     } else {
1364       // stack slot
1365       Address addr = frame_map()->address_for_slot(opr2->single_stack_ix());
1366       if (Immediate::is_uimm12(addr.disp())) {
1367         __ z_ceb(opr1->as_float_reg(), addr);
1368       } else {
1369         __ z_ley(Z_fscratch_1, addr);
1370         __ z_cebr(opr1->as_float_reg(), Z_fscratch_1);
1371       }
1372     }
1373   } else if (opr1->is_double_fpu()) {
1374     if (opr2->is_double_fpu()) {
1375     __ z_cdbr(opr1->as_double_reg(), opr2->as_double_reg());
1376     } else {
1377       // stack slot
1378       Address addr = frame_map()->address_for_slot(opr2->double_stack_ix());
1379       if (Immediate::is_uimm12(addr.disp())) {
1380         __ z_cdb(opr1->as_double_reg(), addr);
1381       } else {
1382         __ z_ldy(Z_fscratch_1, addr);
1383         __ z_cdbr(opr1->as_double_reg(), Z_fscratch_1);
1384       }
1385     }
1386   } else {
1387     ShouldNotReachHere();
1388   }
1389 }
1390 
comp_fl2i(LIR_Code code,LIR_Opr left,LIR_Opr right,LIR_Opr dst,LIR_Op2 * op)1391 void LIR_Assembler::comp_fl2i(LIR_Code code, LIR_Opr left, LIR_Opr right, LIR_Opr dst, LIR_Op2* op) {
1392   Label    done;
1393   Register dreg = dst->as_register();
1394 
1395   if (code == lir_cmp_fd2i || code == lir_ucmp_fd2i) {
1396     assert((left->is_single_fpu() && right->is_single_fpu()) ||
1397            (left->is_double_fpu() && right->is_double_fpu()), "unexpected operand types");
1398     bool is_single = left->is_single_fpu();
1399     bool is_unordered_less = (code == lir_ucmp_fd2i);
1400     FloatRegister lreg = is_single ? left->as_float_reg() : left->as_double_reg();
1401     FloatRegister rreg = is_single ? right->as_float_reg() : right->as_double_reg();
1402     if (is_single) {
1403       __ z_cebr(lreg, rreg);
1404     } else {
1405       __ z_cdbr(lreg, rreg);
1406     }
1407     if (VM_Version::has_LoadStoreConditional()) {
1408       Register one       = Z_R0_scratch;
1409       Register minus_one = Z_R1_scratch;
1410       __ z_lghi(minus_one, -1);
1411       __ z_lghi(one,  1);
1412       __ z_lghi(dreg, 0);
1413       __ z_locgr(dreg, one,       is_unordered_less ? Assembler::bcondHigh            : Assembler::bcondHighOrNotOrdered);
1414       __ z_locgr(dreg, minus_one, is_unordered_less ? Assembler::bcondLowOrNotOrdered : Assembler::bcondLow);
1415     } else {
1416       __ clear_reg(dreg, true, false);
1417       __ z_bre(done); // if (left == right) dst = 0
1418 
1419       // if (left > right || ((code ~= cmpg) && (left <> right)) dst := 1
1420       __ z_lhi(dreg, 1);
1421       __ z_brc(is_unordered_less ? Assembler::bcondHigh : Assembler::bcondHighOrNotOrdered, done);
1422 
1423       // if (left < right || ((code ~= cmpl) && (left <> right)) dst := -1
1424       __ z_lhi(dreg, -1);
1425     }
1426   } else {
1427     assert(code == lir_cmp_l2i, "check");
1428     if (VM_Version::has_LoadStoreConditional()) {
1429       Register one       = Z_R0_scratch;
1430       Register minus_one = Z_R1_scratch;
1431       __ z_cgr(left->as_register_lo(), right->as_register_lo());
1432       __ z_lghi(minus_one, -1);
1433       __ z_lghi(one,  1);
1434       __ z_lghi(dreg, 0);
1435       __ z_locgr(dreg, one, Assembler::bcondHigh);
1436       __ z_locgr(dreg, minus_one, Assembler::bcondLow);
1437     } else {
1438       __ z_cgr(left->as_register_lo(), right->as_register_lo());
1439       __ z_lghi(dreg,  0);     // eq value
1440       __ z_bre(done);
1441       __ z_lghi(dreg,  1);     // gt value
1442       __ z_brh(done);
1443       __ z_lghi(dreg, -1);     // lt value
1444     }
1445   }
1446   __ bind(done);
1447 }
1448 
1449 // result = condition ? opr1 : opr2
cmove(LIR_Condition condition,LIR_Opr opr1,LIR_Opr opr2,LIR_Opr result,BasicType type)1450 void LIR_Assembler::cmove(LIR_Condition condition, LIR_Opr opr1, LIR_Opr opr2, LIR_Opr result, BasicType type) {
1451   Assembler::branch_condition acond = Assembler::bcondEqual, ncond = Assembler::bcondNotEqual;
1452   switch (condition) {
1453     case lir_cond_equal:        acond = Assembler::bcondEqual;    ncond = Assembler::bcondNotEqual; break;
1454     case lir_cond_notEqual:     acond = Assembler::bcondNotEqual; ncond = Assembler::bcondEqual;    break;
1455     case lir_cond_less:         acond = Assembler::bcondLow;      ncond = Assembler::bcondNotLow;   break;
1456     case lir_cond_lessEqual:    acond = Assembler::bcondNotHigh;  ncond = Assembler::bcondHigh;     break;
1457     case lir_cond_greaterEqual: acond = Assembler::bcondNotLow;   ncond = Assembler::bcondLow;      break;
1458     case lir_cond_greater:      acond = Assembler::bcondHigh;     ncond = Assembler::bcondNotHigh;  break;
1459     case lir_cond_belowEqual:   acond = Assembler::bcondNotHigh;  ncond = Assembler::bcondHigh;     break;
1460     case lir_cond_aboveEqual:   acond = Assembler::bcondNotLow;   ncond = Assembler::bcondLow;      break;
1461     default:                    ShouldNotReachHere();
1462   }
1463 
1464   if (opr1->is_cpu_register()) {
1465     reg2reg(opr1, result);
1466   } else if (opr1->is_stack()) {
1467     stack2reg(opr1, result, result->type());
1468   } else if (opr1->is_constant()) {
1469     const2reg(opr1, result, lir_patch_none, NULL);
1470   } else {
1471     ShouldNotReachHere();
1472   }
1473 
1474   if (VM_Version::has_LoadStoreConditional() && !opr2->is_constant()) {
1475     // Optimized version that does not require a branch.
1476     if (opr2->is_single_cpu()) {
1477       assert(opr2->cpu_regnr() != result->cpu_regnr(), "opr2 already overwritten by previous move");
1478       __ z_locgr(result->as_register(), opr2->as_register(), ncond);
1479     } else if (opr2->is_double_cpu()) {
1480       assert(opr2->cpu_regnrLo() != result->cpu_regnrLo() && opr2->cpu_regnrLo() != result->cpu_regnrHi(), "opr2 already overwritten by previous move");
1481       assert(opr2->cpu_regnrHi() != result->cpu_regnrLo() && opr2->cpu_regnrHi() != result->cpu_regnrHi(), "opr2 already overwritten by previous move");
1482       __ z_locgr(result->as_register_lo(), opr2->as_register_lo(), ncond);
1483     } else if (opr2->is_single_stack()) {
1484       __ z_loc(result->as_register(), frame_map()->address_for_slot(opr2->single_stack_ix()), ncond);
1485     } else if (opr2->is_double_stack()) {
1486       __ z_locg(result->as_register_lo(), frame_map()->address_for_slot(opr2->double_stack_ix()), ncond);
1487     } else {
1488       ShouldNotReachHere();
1489     }
1490   } else {
1491     Label skip;
1492     __ z_brc(acond, skip);
1493     if (opr2->is_cpu_register()) {
1494       reg2reg(opr2, result);
1495     } else if (opr2->is_stack()) {
1496       stack2reg(opr2, result, result->type());
1497     } else if (opr2->is_constant()) {
1498       const2reg(opr2, result, lir_patch_none, NULL);
1499     } else {
1500       ShouldNotReachHere();
1501     }
1502     __ bind(skip);
1503   }
1504 }
1505 
arith_op(LIR_Code code,LIR_Opr left,LIR_Opr right,LIR_Opr dest,CodeEmitInfo * info,bool pop_fpu_stack)1506 void LIR_Assembler::arith_op(LIR_Code code, LIR_Opr left, LIR_Opr right, LIR_Opr dest,
1507                              CodeEmitInfo* info, bool pop_fpu_stack) {
1508   assert(info == NULL, "should never be used, idiv/irem and ldiv/lrem not handled by this method");
1509 
1510   if (left->is_single_cpu()) {
1511     assert(left == dest, "left and dest must be equal");
1512     Register lreg = left->as_register();
1513 
1514     if (right->is_single_cpu()) {
1515       // cpu register - cpu register
1516       Register rreg = right->as_register();
1517       switch (code) {
1518         case lir_add: __ z_ar (lreg, rreg); break;
1519         case lir_sub: __ z_sr (lreg, rreg); break;
1520         case lir_mul: __ z_msr(lreg, rreg); break;
1521         default: ShouldNotReachHere();
1522       }
1523 
1524     } else if (right->is_stack()) {
1525       // cpu register - stack
1526       Address raddr = frame_map()->address_for_slot(right->single_stack_ix());
1527       switch (code) {
1528         case lir_add: __ z_ay(lreg, raddr); break;
1529         case lir_sub: __ z_sy(lreg, raddr); break;
1530         default: ShouldNotReachHere();
1531       }
1532 
1533     } else if (right->is_constant()) {
1534       // cpu register - constant
1535       jint c = right->as_constant_ptr()->as_jint();
1536       switch (code) {
1537         case lir_add: __ z_agfi(lreg, c);  break;
1538         case lir_sub: __ z_agfi(lreg, -c); break; // note: -min_jint == min_jint
1539         case lir_mul: __ z_msfi(lreg, c);  break;
1540         default: ShouldNotReachHere();
1541       }
1542 
1543     } else {
1544       ShouldNotReachHere();
1545     }
1546 
1547   } else if (left->is_double_cpu()) {
1548     assert(left == dest, "left and dest must be equal");
1549     Register lreg_lo = left->as_register_lo();
1550     Register lreg_hi = left->as_register_hi();
1551 
1552     if (right->is_double_cpu()) {
1553       // cpu register - cpu register
1554       Register rreg_lo = right->as_register_lo();
1555       Register rreg_hi = right->as_register_hi();
1556       assert_different_registers(lreg_lo, rreg_lo);
1557       switch (code) {
1558         case lir_add:
1559           __ z_agr(lreg_lo, rreg_lo);
1560           break;
1561         case lir_sub:
1562           __ z_sgr(lreg_lo, rreg_lo);
1563           break;
1564         case lir_mul:
1565           __ z_msgr(lreg_lo, rreg_lo);
1566           break;
1567         default:
1568           ShouldNotReachHere();
1569       }
1570 
1571     } else if (right->is_constant()) {
1572       // cpu register - constant
1573       jlong c = right->as_constant_ptr()->as_jlong_bits();
1574       switch (code) {
1575         case lir_add: __ z_agfi(lreg_lo, c); break;
1576         case lir_sub:
1577           if (c != min_jint) {
1578                       __ z_agfi(lreg_lo, -c);
1579           } else {
1580             // -min_jint cannot be represented as simm32 in z_agfi
1581             // min_jint sign extended:      0xffffffff80000000
1582             // -min_jint as 64 bit integer: 0x0000000080000000
1583             // 0x80000000 can be represented as uimm32 in z_algfi
1584             // lreg_lo := lreg_lo + -min_jint == lreg_lo + 0x80000000
1585                       __ z_algfi(lreg_lo, UCONST64(0x80000000));
1586           }
1587           break;
1588         case lir_mul: __ z_msgfi(lreg_lo, c); break;
1589         default:
1590           ShouldNotReachHere();
1591       }
1592 
1593     } else {
1594       ShouldNotReachHere();
1595     }
1596 
1597   } else if (left->is_single_fpu()) {
1598     assert(left == dest, "left and dest must be equal");
1599     FloatRegister lreg = left->as_float_reg();
1600     FloatRegister rreg = right->is_single_fpu() ? right->as_float_reg() : fnoreg;
1601     Address raddr;
1602 
1603     if (rreg == fnoreg) {
1604       assert(right->is_single_stack(), "constants should be loaded into register");
1605       raddr = frame_map()->address_for_slot(right->single_stack_ix());
1606       if (!Immediate::is_uimm12(raddr.disp())) {
1607         __ mem2freg_opt(rreg = Z_fscratch_1, raddr, false);
1608       }
1609     }
1610 
1611     if (rreg != fnoreg) {
1612       switch (code) {
1613         case lir_add: __ z_aebr(lreg, rreg);  break;
1614         case lir_sub: __ z_sebr(lreg, rreg);  break;
1615         case lir_mul_strictfp: // fall through
1616         case lir_mul: __ z_meebr(lreg, rreg); break;
1617         case lir_div_strictfp: // fall through
1618         case lir_div: __ z_debr(lreg, rreg);  break;
1619         default: ShouldNotReachHere();
1620       }
1621     } else {
1622       switch (code) {
1623         case lir_add: __ z_aeb(lreg, raddr);  break;
1624         case lir_sub: __ z_seb(lreg, raddr);  break;
1625         case lir_mul_strictfp: // fall through
1626         case lir_mul: __ z_meeb(lreg, raddr);  break;
1627         case lir_div_strictfp: // fall through
1628         case lir_div: __ z_deb(lreg, raddr);  break;
1629         default: ShouldNotReachHere();
1630       }
1631     }
1632   } else if (left->is_double_fpu()) {
1633     assert(left == dest, "left and dest must be equal");
1634     FloatRegister lreg = left->as_double_reg();
1635     FloatRegister rreg = right->is_double_fpu() ? right->as_double_reg() : fnoreg;
1636     Address raddr;
1637 
1638     if (rreg == fnoreg) {
1639       assert(right->is_double_stack(), "constants should be loaded into register");
1640       raddr = frame_map()->address_for_slot(right->double_stack_ix());
1641       if (!Immediate::is_uimm12(raddr.disp())) {
1642         __ mem2freg_opt(rreg = Z_fscratch_1, raddr, true);
1643       }
1644     }
1645 
1646     if (rreg != fnoreg) {
1647       switch (code) {
1648         case lir_add: __ z_adbr(lreg, rreg); break;
1649         case lir_sub: __ z_sdbr(lreg, rreg); break;
1650         case lir_mul_strictfp: // fall through
1651         case lir_mul: __ z_mdbr(lreg, rreg); break;
1652         case lir_div_strictfp: // fall through
1653         case lir_div: __ z_ddbr(lreg, rreg); break;
1654         default: ShouldNotReachHere();
1655       }
1656     } else {
1657       switch (code) {
1658         case lir_add: __ z_adb(lreg, raddr); break;
1659         case lir_sub: __ z_sdb(lreg, raddr); break;
1660         case lir_mul_strictfp: // fall through
1661         case lir_mul: __ z_mdb(lreg, raddr); break;
1662         case lir_div_strictfp: // fall through
1663         case lir_div: __ z_ddb(lreg, raddr); break;
1664         default: ShouldNotReachHere();
1665       }
1666     }
1667   } else if (left->is_address()) {
1668     assert(left == dest, "left and dest must be equal");
1669     assert(code == lir_add, "unsupported operation");
1670     assert(right->is_constant(), "unsupported operand");
1671     jint c = right->as_constant_ptr()->as_jint();
1672     LIR_Address* lir_addr = left->as_address_ptr();
1673     Address addr = as_Address(lir_addr);
1674     switch (lir_addr->type()) {
1675       case T_INT:
1676         __ add2mem_32(addr, c, Z_R1_scratch);
1677         break;
1678       case T_LONG:
1679         __ add2mem_64(addr, c, Z_R1_scratch);
1680         break;
1681       default:
1682         ShouldNotReachHere();
1683     }
1684   } else {
1685     ShouldNotReachHere();
1686   }
1687 }
1688 
fpop()1689 void LIR_Assembler::fpop() {
1690   // do nothing
1691 }
1692 
intrinsic_op(LIR_Code code,LIR_Opr value,LIR_Opr thread,LIR_Opr dest,LIR_Op * op)1693 void LIR_Assembler::intrinsic_op(LIR_Code code, LIR_Opr value, LIR_Opr thread, LIR_Opr dest, LIR_Op* op) {
1694   switch (code) {
1695     case lir_sqrt: {
1696       assert(!thread->is_valid(), "there is no need for a thread_reg for dsqrt");
1697       FloatRegister src_reg = value->as_double_reg();
1698       FloatRegister dst_reg = dest->as_double_reg();
1699       __ z_sqdbr(dst_reg, src_reg);
1700       break;
1701     }
1702     case lir_abs: {
1703       assert(!thread->is_valid(), "there is no need for a thread_reg for fabs");
1704       FloatRegister src_reg = value->as_double_reg();
1705       FloatRegister dst_reg = dest->as_double_reg();
1706       __ z_lpdbr(dst_reg, src_reg);
1707       break;
1708     }
1709     default: {
1710       ShouldNotReachHere();
1711       break;
1712     }
1713   }
1714 }
1715 
logic_op(LIR_Code code,LIR_Opr left,LIR_Opr right,LIR_Opr dst)1716 void LIR_Assembler::logic_op(LIR_Code code, LIR_Opr left, LIR_Opr right, LIR_Opr dst) {
1717   if (left->is_single_cpu()) {
1718     Register reg = left->as_register();
1719     if (right->is_constant()) {
1720       int val = right->as_constant_ptr()->as_jint();
1721       switch (code) {
1722         case lir_logic_and: __ z_nilf(reg, val); break;
1723         case lir_logic_or:  __ z_oilf(reg, val); break;
1724         case lir_logic_xor: __ z_xilf(reg, val); break;
1725         default: ShouldNotReachHere();
1726       }
1727     } else if (right->is_stack()) {
1728       Address raddr = frame_map()->address_for_slot(right->single_stack_ix());
1729       switch (code) {
1730         case lir_logic_and: __ z_ny(reg, raddr); break;
1731         case lir_logic_or:  __ z_oy(reg, raddr); break;
1732         case lir_logic_xor: __ z_xy(reg, raddr); break;
1733         default: ShouldNotReachHere();
1734       }
1735     } else {
1736       Register rright = right->as_register();
1737       switch (code) {
1738         case lir_logic_and: __ z_nr(reg, rright); break;
1739         case lir_logic_or : __ z_or(reg, rright); break;
1740         case lir_logic_xor: __ z_xr(reg, rright); break;
1741         default: ShouldNotReachHere();
1742       }
1743     }
1744     move_regs(reg, dst->as_register());
1745   } else {
1746     Register l_lo = left->as_register_lo();
1747     if (right->is_constant()) {
1748       __ load_const_optimized(Z_R1_scratch, right->as_constant_ptr()->as_jlong());
1749       switch (code) {
1750         case lir_logic_and:
1751           __ z_ngr(l_lo, Z_R1_scratch);
1752           break;
1753         case lir_logic_or:
1754           __ z_ogr(l_lo, Z_R1_scratch);
1755           break;
1756         case lir_logic_xor:
1757           __ z_xgr(l_lo, Z_R1_scratch);
1758           break;
1759         default: ShouldNotReachHere();
1760       }
1761     } else {
1762       Register r_lo;
1763       if (right->type() == T_OBJECT || right->type() == T_ARRAY) {
1764         r_lo = right->as_register();
1765       } else {
1766         r_lo = right->as_register_lo();
1767       }
1768       switch (code) {
1769         case lir_logic_and:
1770           __ z_ngr(l_lo, r_lo);
1771           break;
1772         case lir_logic_or:
1773           __ z_ogr(l_lo, r_lo);
1774           break;
1775         case lir_logic_xor:
1776           __ z_xgr(l_lo, r_lo);
1777           break;
1778         default: ShouldNotReachHere();
1779       }
1780     }
1781 
1782     Register dst_lo = dst->as_register_lo();
1783 
1784     move_regs(l_lo, dst_lo);
1785   }
1786 }
1787 
1788 // See operand selection in LIRGenerator::do_ArithmeticOp_Int().
arithmetic_idiv(LIR_Code code,LIR_Opr left,LIR_Opr right,LIR_Opr temp,LIR_Opr result,CodeEmitInfo * info)1789 void LIR_Assembler::arithmetic_idiv(LIR_Code code, LIR_Opr left, LIR_Opr right, LIR_Opr temp, LIR_Opr result, CodeEmitInfo* info) {
1790   if (left->is_double_cpu()) {
1791     // 64 bit integer case
1792     assert(left->is_double_cpu(), "left must be register");
1793     assert(right->is_double_cpu() || is_power_of_2_long(right->as_jlong()),
1794            "right must be register or power of 2 constant");
1795     assert(result->is_double_cpu(), "result must be register");
1796 
1797     Register lreg = left->as_register_lo();
1798     Register dreg = result->as_register_lo();
1799 
1800     if (right->is_constant()) {
1801       // Convert division by a power of two into some shifts and logical operations.
1802       Register treg1 = Z_R0_scratch;
1803       Register treg2 = Z_R1_scratch;
1804       jlong divisor = right->as_jlong();
1805       jlong log_divisor = log2_long(right->as_jlong());
1806 
1807       if (divisor == min_jlong) {
1808         // Min_jlong is special. Result is '0' except for min_jlong/min_jlong = 1.
1809         if (dreg == lreg) {
1810           NearLabel done;
1811           __ load_const_optimized(treg2, min_jlong);
1812           __ z_cgr(lreg, treg2);
1813           __ z_lghi(dreg, 0);           // Preserves condition code.
1814           __ z_brne(done);
1815           __ z_lghi(dreg, 1);           // min_jlong / min_jlong = 1
1816           __ bind(done);
1817         } else {
1818           assert_different_registers(dreg, lreg);
1819           NearLabel done;
1820           __ z_lghi(dreg, 0);
1821           __ compare64_and_branch(lreg, min_jlong, Assembler::bcondNotEqual, done);
1822           __ z_lghi(dreg, 1);
1823           __ bind(done);
1824         }
1825         return;
1826       }
1827       __ move_reg_if_needed(dreg, T_LONG, lreg, T_LONG);
1828       if (divisor == 2) {
1829         __ z_srlg(treg2, dreg, 63);     // dividend < 0 ? 1 : 0
1830       } else {
1831         __ z_srag(treg2, dreg, 63);     // dividend < 0 ? -1 : 0
1832         __ and_imm(treg2, divisor - 1, treg1, true);
1833       }
1834       if (code == lir_idiv) {
1835         __ z_agr(dreg, treg2);
1836         __ z_srag(dreg, dreg, log_divisor);
1837       } else {
1838         assert(code == lir_irem, "check");
1839         __ z_agr(treg2, dreg);
1840         __ and_imm(treg2, ~(divisor - 1), treg1, true);
1841         __ z_sgr(dreg, treg2);
1842       }
1843       return;
1844     }
1845 
1846     // Divisor is not a power of 2 constant.
1847     Register rreg = right->as_register_lo();
1848     Register treg = temp->as_register_lo();
1849     assert(right->is_double_cpu(), "right must be register");
1850     assert(lreg == Z_R11, "see ldivInOpr()");
1851     assert(rreg != lreg, "right register must not be same as left register");
1852     assert((code == lir_idiv && dreg == Z_R11 && treg == Z_R10) ||
1853            (code == lir_irem && dreg == Z_R10 && treg == Z_R11), "see ldivInOpr(), ldivOutOpr(), lremOutOpr()");
1854 
1855     Register R1 = lreg->predecessor();
1856     Register R2 = rreg;
1857     assert(code != lir_idiv || lreg==dreg, "see code below");
1858     if (code == lir_idiv) {
1859       __ z_lcgr(lreg, lreg);
1860     } else {
1861       __ clear_reg(dreg, true, false);
1862     }
1863     NearLabel done;
1864     __ compare64_and_branch(R2, -1, Assembler::bcondEqual, done);
1865     if (code == lir_idiv) {
1866       __ z_lcgr(lreg, lreg); // Revert lcgr above.
1867     }
1868     if (ImplicitDiv0Checks) {
1869       // No debug info because the idiv won't trap.
1870       // Add_debug_info_for_div0 would instantiate another DivByZeroStub,
1871       // which is unnecessary, too.
1872       add_debug_info_for_div0(__ offset(), info);
1873     }
1874     __ z_dsgr(R1, R2);
1875     __ bind(done);
1876     return;
1877   }
1878 
1879   // 32 bit integer case
1880 
1881   assert(left->is_single_cpu(), "left must be register");
1882   assert(right->is_single_cpu() || is_power_of_2(right->as_jint()), "right must be register or power of 2 constant");
1883   assert(result->is_single_cpu(), "result must be register");
1884 
1885   Register lreg = left->as_register();
1886   Register dreg = result->as_register();
1887 
1888   if (right->is_constant()) {
1889     // Convert division by a power of two into some shifts and logical operations.
1890     Register treg1 = Z_R0_scratch;
1891     Register treg2 = Z_R1_scratch;
1892     jlong divisor = right->as_jint();
1893     jlong log_divisor = log2_long(right->as_jint());
1894     __ move_reg_if_needed(dreg, T_LONG, lreg, T_INT); // sign extend
1895     if (divisor == 2) {
1896       __ z_srlg(treg2, dreg, 63);     // dividend < 0 ?  1 : 0
1897     } else {
1898       __ z_srag(treg2, dreg, 63);     // dividend < 0 ? -1 : 0
1899       __ and_imm(treg2, divisor - 1, treg1, true);
1900     }
1901     if (code == lir_idiv) {
1902       __ z_agr(dreg, treg2);
1903       __ z_srag(dreg, dreg, log_divisor);
1904     } else {
1905       assert(code == lir_irem, "check");
1906       __ z_agr(treg2, dreg);
1907       __ and_imm(treg2, ~(divisor - 1), treg1, true);
1908       __ z_sgr(dreg, treg2);
1909     }
1910     return;
1911   }
1912 
1913   // Divisor is not a power of 2 constant.
1914   Register rreg = right->as_register();
1915   Register treg = temp->as_register();
1916   assert(right->is_single_cpu(), "right must be register");
1917   assert(lreg == Z_R11, "left register must be rax,");
1918   assert(rreg != lreg, "right register must not be same as left register");
1919   assert((code == lir_idiv && dreg == Z_R11 && treg == Z_R10)
1920       || (code == lir_irem && dreg == Z_R10 && treg == Z_R11), "see divInOpr(), divOutOpr(), remOutOpr()");
1921 
1922   Register R1 = lreg->predecessor();
1923   Register R2 = rreg;
1924   __ move_reg_if_needed(lreg, T_LONG, lreg, T_INT); // sign extend
1925   if (ImplicitDiv0Checks) {
1926     // No debug info because the idiv won't trap.
1927     // Add_debug_info_for_div0 would instantiate another DivByZeroStub,
1928     // which is unnecessary, too.
1929     add_debug_info_for_div0(__ offset(), info);
1930   }
1931   __ z_dsgfr(R1, R2);
1932 }
1933 
throw_op(LIR_Opr exceptionPC,LIR_Opr exceptionOop,CodeEmitInfo * info)1934 void LIR_Assembler::throw_op(LIR_Opr exceptionPC, LIR_Opr exceptionOop, CodeEmitInfo* info) {
1935   assert(exceptionOop->as_register() == Z_EXC_OOP, "should match");
1936   assert(exceptionPC->as_register() == Z_EXC_PC, "should match");
1937 
1938   // Exception object is not added to oop map by LinearScan
1939   // (LinearScan assumes that no oops are in fixed registers).
1940   info->add_register_oop(exceptionOop);
1941 
1942   // Reuse the debug info from the safepoint poll for the throw op itself.
1943   __ get_PC(Z_EXC_PC);
1944   add_call_info(__ offset(), info); // for exception handler
1945   address stub = Runtime1::entry_for (compilation()->has_fpu_code() ? Runtime1::handle_exception_id
1946                                                                     : Runtime1::handle_exception_nofpu_id);
1947   emit_call_c(stub);
1948 }
1949 
unwind_op(LIR_Opr exceptionOop)1950 void LIR_Assembler::unwind_op(LIR_Opr exceptionOop) {
1951   assert(exceptionOop->as_register() == Z_EXC_OOP, "should match");
1952 
1953   __ branch_optimized(Assembler::bcondAlways, _unwind_handler_entry);
1954 }
1955 
emit_arraycopy(LIR_OpArrayCopy * op)1956 void LIR_Assembler::emit_arraycopy(LIR_OpArrayCopy* op) {
1957   ciArrayKlass* default_type = op->expected_type();
1958   Register src = op->src()->as_register();
1959   Register dst = op->dst()->as_register();
1960   Register src_pos = op->src_pos()->as_register();
1961   Register dst_pos = op->dst_pos()->as_register();
1962   Register length  = op->length()->as_register();
1963   Register tmp = op->tmp()->as_register();
1964 
1965   CodeStub* stub = op->stub();
1966   int flags = op->flags();
1967   BasicType basic_type = default_type != NULL ? default_type->element_type()->basic_type() : T_ILLEGAL;
1968   if (basic_type == T_ARRAY) basic_type = T_OBJECT;
1969 
1970   // If we don't know anything, just go through the generic arraycopy.
1971   if (default_type == NULL) {
1972     address copyfunc_addr = StubRoutines::generic_arraycopy();
1973 
1974     if (copyfunc_addr == NULL) {
1975       // Take a slow path for generic arraycopy.
1976       __ branch_optimized(Assembler::bcondAlways, *stub->entry());
1977       __ bind(*stub->continuation());
1978       return;
1979     }
1980 
1981     // Save outgoing arguments in callee saved registers (C convention) in case
1982     // a call to System.arraycopy is needed.
1983     Register callee_saved_src     = Z_R10;
1984     Register callee_saved_src_pos = Z_R11;
1985     Register callee_saved_dst     = Z_R12;
1986     Register callee_saved_dst_pos = Z_R13;
1987     Register callee_saved_length  = Z_ARG5; // Z_ARG5 == Z_R6 is callee saved.
1988 
1989     __ lgr_if_needed(callee_saved_src, src);
1990     __ lgr_if_needed(callee_saved_src_pos, src_pos);
1991     __ lgr_if_needed(callee_saved_dst, dst);
1992     __ lgr_if_needed(callee_saved_dst_pos, dst_pos);
1993     __ lgr_if_needed(callee_saved_length, length);
1994 
1995     // C function requires 64 bit values.
1996     __ z_lgfr(src_pos, src_pos);
1997     __ z_lgfr(dst_pos, dst_pos);
1998     __ z_lgfr(length, length);
1999 
2000     // Pass arguments: may push as this is not a safepoint; SP must be fix at each safepoint.
2001 
2002     // The arguments are in the corresponding registers.
2003     assert(Z_ARG1 == src,     "assumption");
2004     assert(Z_ARG2 == src_pos, "assumption");
2005     assert(Z_ARG3 == dst,     "assumption");
2006     assert(Z_ARG4 == dst_pos, "assumption");
2007     assert(Z_ARG5 == length,  "assumption");
2008 #ifndef PRODUCT
2009     if (PrintC1Statistics) {
2010       __ load_const_optimized(Z_R1_scratch, (address)&Runtime1::_generic_arraycopystub_cnt);
2011       __ add2mem_32(Address(Z_R1_scratch), 1, Z_R0_scratch);
2012     }
2013 #endif
2014     emit_call_c(copyfunc_addr);
2015     CHECK_BAILOUT();
2016 
2017     __ compare32_and_branch(Z_RET, (intptr_t)0, Assembler::bcondEqual, *stub->continuation());
2018 
2019     __ z_lgr(tmp, Z_RET);
2020     __ z_xilf(tmp, -1);
2021 
2022     // Restore values from callee saved registers so they are where the stub
2023     // expects them.
2024     __ lgr_if_needed(src, callee_saved_src);
2025     __ lgr_if_needed(src_pos, callee_saved_src_pos);
2026     __ lgr_if_needed(dst, callee_saved_dst);
2027     __ lgr_if_needed(dst_pos, callee_saved_dst_pos);
2028     __ lgr_if_needed(length, callee_saved_length);
2029 
2030     __ z_sr(length, tmp);
2031     __ z_ar(src_pos, tmp);
2032     __ z_ar(dst_pos, tmp);
2033     __ branch_optimized(Assembler::bcondAlways, *stub->entry());
2034 
2035     __ bind(*stub->continuation());
2036     return;
2037   }
2038 
2039   assert(default_type != NULL && default_type->is_array_klass() && default_type->is_loaded(), "must be true at this point");
2040 
2041   int elem_size = type2aelembytes(basic_type);
2042   int shift_amount;
2043 
2044   switch (elem_size) {
2045     case 1 :
2046       shift_amount = 0;
2047       break;
2048     case 2 :
2049       shift_amount = 1;
2050       break;
2051     case 4 :
2052       shift_amount = 2;
2053       break;
2054     case 8 :
2055       shift_amount = 3;
2056       break;
2057     default:
2058       shift_amount = -1;
2059       ShouldNotReachHere();
2060   }
2061 
2062   Address src_length_addr = Address(src, arrayOopDesc::length_offset_in_bytes());
2063   Address dst_length_addr = Address(dst, arrayOopDesc::length_offset_in_bytes());
2064   Address src_klass_addr = Address(src, oopDesc::klass_offset_in_bytes());
2065   Address dst_klass_addr = Address(dst, oopDesc::klass_offset_in_bytes());
2066 
2067   // Length and pos's are all sign extended at this point on 64bit.
2068 
2069   // test for NULL
2070   if (flags & LIR_OpArrayCopy::src_null_check) {
2071     __ compareU64_and_branch(src, (intptr_t)0, Assembler::bcondZero, *stub->entry());
2072   }
2073   if (flags & LIR_OpArrayCopy::dst_null_check) {
2074     __ compareU64_and_branch(dst, (intptr_t)0, Assembler::bcondZero, *stub->entry());
2075   }
2076 
2077   // Check if negative.
2078   if (flags & LIR_OpArrayCopy::src_pos_positive_check) {
2079     __ compare32_and_branch(src_pos, (intptr_t)0, Assembler::bcondLow, *stub->entry());
2080   }
2081   if (flags & LIR_OpArrayCopy::dst_pos_positive_check) {
2082     __ compare32_and_branch(dst_pos, (intptr_t)0, Assembler::bcondLow, *stub->entry());
2083   }
2084 
2085   // If the compiler was not able to prove that exact type of the source or the destination
2086   // of the arraycopy is an array type, check at runtime if the source or the destination is
2087   // an instance type.
2088   if (flags & LIR_OpArrayCopy::type_check) {
2089     assert(Klass::_lh_neutral_value == 0, "or replace z_lt instructions");
2090 
2091     if (!(flags & LIR_OpArrayCopy::dst_objarray)) {
2092       __ load_klass(tmp, dst);
2093       __ z_lt(tmp, Address(tmp, in_bytes(Klass::layout_helper_offset())));
2094       __ branch_optimized(Assembler::bcondNotLow, *stub->entry());
2095     }
2096 
2097     if (!(flags & LIR_OpArrayCopy::src_objarray)) {
2098       __ load_klass(tmp, src);
2099       __ z_lt(tmp, Address(tmp, in_bytes(Klass::layout_helper_offset())));
2100       __ branch_optimized(Assembler::bcondNotLow, *stub->entry());
2101     }
2102   }
2103 
2104   if (flags & LIR_OpArrayCopy::src_range_check) {
2105     __ z_la(tmp, Address(src_pos, length));
2106     __ z_cl(tmp, src_length_addr);
2107     __ branch_optimized(Assembler::bcondHigh, *stub->entry());
2108   }
2109   if (flags & LIR_OpArrayCopy::dst_range_check) {
2110     __ z_la(tmp, Address(dst_pos, length));
2111     __ z_cl(tmp, dst_length_addr);
2112     __ branch_optimized(Assembler::bcondHigh, *stub->entry());
2113   }
2114 
2115   if (flags & LIR_OpArrayCopy::length_positive_check) {
2116     __ z_ltr(length, length);
2117     __ branch_optimized(Assembler::bcondNegative, *stub->entry());
2118   }
2119 
2120   // Stubs require 64 bit values.
2121   __ z_lgfr(src_pos, src_pos); // int -> long
2122   __ z_lgfr(dst_pos, dst_pos); // int -> long
2123   __ z_lgfr(length, length);   // int -> long
2124 
2125   if (flags & LIR_OpArrayCopy::type_check) {
2126     // We don't know the array types are compatible.
2127     if (basic_type != T_OBJECT) {
2128       // Simple test for basic type arrays.
2129       if (UseCompressedClassPointers) {
2130         __ z_l(tmp, src_klass_addr);
2131         __ z_c(tmp, dst_klass_addr);
2132       } else {
2133         __ z_lg(tmp, src_klass_addr);
2134         __ z_cg(tmp, dst_klass_addr);
2135       }
2136       __ branch_optimized(Assembler::bcondNotEqual, *stub->entry());
2137     } else {
2138       // For object arrays, if src is a sub class of dst then we can
2139       // safely do the copy.
2140       NearLabel cont, slow;
2141       Register src_klass = Z_R1_scratch;
2142       Register dst_klass = Z_R10;
2143 
2144       __ load_klass(src_klass, src);
2145       __ load_klass(dst_klass, dst);
2146 
2147       __ check_klass_subtype_fast_path(src_klass, dst_klass, tmp, &cont, &slow, NULL);
2148 
2149       store_parameter(src_klass, 0); // sub
2150       store_parameter(dst_klass, 1); // super
2151       emit_call_c(Runtime1::entry_for (Runtime1::slow_subtype_check_id));
2152       CHECK_BAILOUT2(cont, slow);
2153       // Sets condition code 0 for match (2 otherwise).
2154       __ branch_optimized(Assembler::bcondEqual, cont);
2155 
2156       __ bind(slow);
2157 
2158       address copyfunc_addr = StubRoutines::checkcast_arraycopy();
2159       if (copyfunc_addr != NULL) { // use stub if available
2160         // Src is not a sub class of dst so we have to do a
2161         // per-element check.
2162 
2163         int mask = LIR_OpArrayCopy::src_objarray|LIR_OpArrayCopy::dst_objarray;
2164         if ((flags & mask) != mask) {
2165           // Check that at least both of them object arrays.
2166           assert(flags & mask, "one of the two should be known to be an object array");
2167 
2168           if (!(flags & LIR_OpArrayCopy::src_objarray)) {
2169             __ load_klass(tmp, src);
2170           } else if (!(flags & LIR_OpArrayCopy::dst_objarray)) {
2171             __ load_klass(tmp, dst);
2172           }
2173           Address klass_lh_addr(tmp, Klass::layout_helper_offset());
2174           jint objArray_lh = Klass::array_layout_helper(T_OBJECT);
2175           __ load_const_optimized(Z_R1_scratch, objArray_lh);
2176           __ z_c(Z_R1_scratch, klass_lh_addr);
2177           __ branch_optimized(Assembler::bcondNotEqual, *stub->entry());
2178         }
2179 
2180         // Save outgoing arguments in callee saved registers (C convention) in case
2181         // a call to System.arraycopy is needed.
2182         Register callee_saved_src     = Z_R10;
2183         Register callee_saved_src_pos = Z_R11;
2184         Register callee_saved_dst     = Z_R12;
2185         Register callee_saved_dst_pos = Z_R13;
2186         Register callee_saved_length  = Z_ARG5; // Z_ARG5 == Z_R6 is callee saved.
2187 
2188         __ lgr_if_needed(callee_saved_src, src);
2189         __ lgr_if_needed(callee_saved_src_pos, src_pos);
2190         __ lgr_if_needed(callee_saved_dst, dst);
2191         __ lgr_if_needed(callee_saved_dst_pos, dst_pos);
2192         __ lgr_if_needed(callee_saved_length, length);
2193 
2194         __ z_llgfr(length, length); // Higher 32bits must be null.
2195 
2196         __ z_sllg(Z_ARG1, src_pos, shift_amount); // index -> byte offset
2197         __ z_sllg(Z_ARG2, dst_pos, shift_amount); // index -> byte offset
2198 
2199         __ z_la(Z_ARG1, Address(src, Z_ARG1, arrayOopDesc::base_offset_in_bytes(basic_type)));
2200         assert_different_registers(Z_ARG1, dst, dst_pos, length);
2201         __ z_la(Z_ARG2, Address(dst, Z_ARG2, arrayOopDesc::base_offset_in_bytes(basic_type)));
2202         assert_different_registers(Z_ARG2, dst, length);
2203 
2204         __ z_lgr(Z_ARG3, length);
2205         assert_different_registers(Z_ARG3, dst);
2206 
2207         __ load_klass(Z_ARG5, dst);
2208         __ z_lg(Z_ARG5, Address(Z_ARG5, ObjArrayKlass::element_klass_offset()));
2209         __ z_lg(Z_ARG4, Address(Z_ARG5, Klass::super_check_offset_offset()));
2210         emit_call_c(copyfunc_addr);
2211         CHECK_BAILOUT2(cont, slow);
2212 
2213 #ifndef PRODUCT
2214         if (PrintC1Statistics) {
2215           NearLabel failed;
2216           __ compareU32_and_branch(Z_RET, (intptr_t)0, Assembler::bcondNotEqual, failed);
2217           __ load_const_optimized(Z_R1_scratch, (address)&Runtime1::_arraycopy_checkcast_cnt);
2218           __ add2mem_32(Address(Z_R1_scratch), 1, Z_R0_scratch);
2219           __ bind(failed);
2220         }
2221 #endif
2222 
2223         __ compareU32_and_branch(Z_RET, (intptr_t)0, Assembler::bcondEqual, *stub->continuation());
2224 
2225 #ifndef PRODUCT
2226         if (PrintC1Statistics) {
2227           __ load_const_optimized(Z_R1_scratch, (address)&Runtime1::_arraycopy_checkcast_attempt_cnt);
2228           __ add2mem_32(Address(Z_R1_scratch), 1, Z_R0_scratch);
2229         }
2230 #endif
2231 
2232         __ z_lgr(tmp, Z_RET);
2233         __ z_xilf(tmp, -1);
2234 
2235         // Restore previously spilled arguments
2236         __ lgr_if_needed(src, callee_saved_src);
2237         __ lgr_if_needed(src_pos, callee_saved_src_pos);
2238         __ lgr_if_needed(dst, callee_saved_dst);
2239         __ lgr_if_needed(dst_pos, callee_saved_dst_pos);
2240         __ lgr_if_needed(length, callee_saved_length);
2241 
2242         __ z_sr(length, tmp);
2243         __ z_ar(src_pos, tmp);
2244         __ z_ar(dst_pos, tmp);
2245       }
2246 
2247       __ branch_optimized(Assembler::bcondAlways, *stub->entry());
2248 
2249       __ bind(cont);
2250     }
2251   }
2252 
2253 #ifdef ASSERT
2254   if (basic_type != T_OBJECT || !(flags & LIR_OpArrayCopy::type_check)) {
2255     // Sanity check the known type with the incoming class. For the
2256     // primitive case the types must match exactly with src.klass and
2257     // dst.klass each exactly matching the default type. For the
2258     // object array case, if no type check is needed then either the
2259     // dst type is exactly the expected type and the src type is a
2260     // subtype which we can't check or src is the same array as dst
2261     // but not necessarily exactly of type default_type.
2262     NearLabel known_ok, halt;
2263     metadata2reg(default_type->constant_encoding(), tmp);
2264     if (UseCompressedClassPointers) {
2265       __ encode_klass_not_null(tmp);
2266     }
2267 
2268     if (basic_type != T_OBJECT) {
2269       if (UseCompressedClassPointers)         { __ z_c (tmp, dst_klass_addr); }
2270       else                                    { __ z_cg(tmp, dst_klass_addr); }
2271       __ branch_optimized(Assembler::bcondNotEqual, halt);
2272       if (UseCompressedClassPointers)         { __ z_c (tmp, src_klass_addr); }
2273       else                                    { __ z_cg(tmp, src_klass_addr); }
2274       __ branch_optimized(Assembler::bcondEqual, known_ok);
2275     } else {
2276       if (UseCompressedClassPointers)         { __ z_c (tmp, dst_klass_addr); }
2277       else                                    { __ z_cg(tmp, dst_klass_addr); }
2278       __ branch_optimized(Assembler::bcondEqual, known_ok);
2279       __ compareU64_and_branch(src, dst, Assembler::bcondEqual, known_ok);
2280     }
2281     __ bind(halt);
2282     __ stop("incorrect type information in arraycopy");
2283     __ bind(known_ok);
2284   }
2285 #endif
2286 
2287 #ifndef PRODUCT
2288   if (PrintC1Statistics) {
2289     __ load_const_optimized(Z_R1_scratch, Runtime1::arraycopy_count_address(basic_type));
2290     __ add2mem_32(Address(Z_R1_scratch), 1, Z_R0_scratch);
2291   }
2292 #endif
2293 
2294   __ z_sllg(tmp, src_pos, shift_amount); // index -> byte offset
2295   __ z_sllg(Z_R1_scratch, dst_pos, shift_amount); // index -> byte offset
2296 
2297   assert_different_registers(Z_ARG1, dst, dst_pos, length);
2298   __ z_la(Z_ARG1, Address(src, tmp, arrayOopDesc::base_offset_in_bytes(basic_type)));
2299   assert_different_registers(Z_ARG2, length);
2300   __ z_la(Z_ARG2, Address(dst, Z_R1_scratch, arrayOopDesc::base_offset_in_bytes(basic_type)));
2301   __ lgr_if_needed(Z_ARG3, length);
2302 
2303   bool disjoint = (flags & LIR_OpArrayCopy::overlapping) == 0;
2304   bool aligned = (flags & LIR_OpArrayCopy::unaligned) == 0;
2305   const char *name;
2306   address entry = StubRoutines::select_arraycopy_function(basic_type, aligned, disjoint, name, false);
2307   __ call_VM_leaf(entry);
2308 
2309   __ bind(*stub->continuation());
2310 }
2311 
shift_op(LIR_Code code,LIR_Opr left,LIR_Opr count,LIR_Opr dest,LIR_Opr tmp)2312 void LIR_Assembler::shift_op(LIR_Code code, LIR_Opr left, LIR_Opr count, LIR_Opr dest, LIR_Opr tmp) {
2313   if (dest->is_single_cpu()) {
2314     if (left->type() == T_OBJECT) {
2315       switch (code) {
2316         case lir_shl:  __ z_sllg (dest->as_register(), left->as_register(), 0, count->as_register()); break;
2317         case lir_shr:  __ z_srag (dest->as_register(), left->as_register(), 0, count->as_register()); break;
2318         case lir_ushr: __ z_srlg (dest->as_register(), left->as_register(), 0, count->as_register()); break;
2319         default: ShouldNotReachHere();
2320       }
2321     } else {
2322       assert(code == lir_shl || left == dest, "left and dest must be equal for 2 operand form right shifts");
2323       Register masked_count = Z_R1_scratch;
2324       __ z_lr(masked_count, count->as_register());
2325       __ z_nill(masked_count, 31);
2326       switch (code) {
2327         case lir_shl:  __ z_sllg (dest->as_register(), left->as_register(), 0, masked_count); break;
2328         case lir_shr:  __ z_sra  (dest->as_register(), 0, masked_count); break;
2329         case lir_ushr: __ z_srl  (dest->as_register(), 0, masked_count); break;
2330         default: ShouldNotReachHere();
2331       }
2332     }
2333   } else {
2334     switch (code) {
2335       case lir_shl:  __ z_sllg (dest->as_register_lo(), left->as_register_lo(), 0, count->as_register()); break;
2336       case lir_shr:  __ z_srag (dest->as_register_lo(), left->as_register_lo(), 0, count->as_register()); break;
2337       case lir_ushr: __ z_srlg (dest->as_register_lo(), left->as_register_lo(), 0, count->as_register()); break;
2338       default: ShouldNotReachHere();
2339     }
2340   }
2341 }
2342 
shift_op(LIR_Code code,LIR_Opr left,jint count,LIR_Opr dest)2343 void LIR_Assembler::shift_op(LIR_Code code, LIR_Opr left, jint count, LIR_Opr dest) {
2344   if (left->type() == T_OBJECT) {
2345     count = count & 63;  // Shouldn't shift by more than sizeof(intptr_t).
2346     Register l = left->as_register();
2347     Register d = dest->as_register_lo();
2348     switch (code) {
2349       case lir_shl:  __ z_sllg (d, l, count); break;
2350       case lir_shr:  __ z_srag (d, l, count); break;
2351       case lir_ushr: __ z_srlg (d, l, count); break;
2352       default: ShouldNotReachHere();
2353     }
2354     return;
2355   }
2356   if (dest->is_single_cpu()) {
2357     assert(code == lir_shl || left == dest, "left and dest must be equal for 2 operand form right shifts");
2358     count = count & 0x1F; // Java spec
2359     switch (code) {
2360       case lir_shl:  __ z_sllg (dest->as_register(), left->as_register(), count); break;
2361       case lir_shr:  __ z_sra  (dest->as_register(), count); break;
2362       case lir_ushr: __ z_srl  (dest->as_register(), count); break;
2363       default: ShouldNotReachHere();
2364     }
2365   } else if (dest->is_double_cpu()) {
2366     count = count & 63; // Java spec
2367     Register l = left->as_pointer_register();
2368     Register d = dest->as_pointer_register();
2369     switch (code) {
2370       case lir_shl:  __ z_sllg (d, l, count); break;
2371       case lir_shr:  __ z_srag (d, l, count); break;
2372       case lir_ushr: __ z_srlg (d, l, count); break;
2373       default: ShouldNotReachHere();
2374     }
2375   } else {
2376     ShouldNotReachHere();
2377   }
2378 }
2379 
emit_alloc_obj(LIR_OpAllocObj * op)2380 void LIR_Assembler::emit_alloc_obj(LIR_OpAllocObj* op) {
2381   if (op->init_check()) {
2382     // Make sure klass is initialized & doesn't have finalizer.
2383     const int state_offset = in_bytes(InstanceKlass::init_state_offset());
2384     Register iklass = op->klass()->as_register();
2385     add_debug_info_for_null_check_here(op->stub()->info());
2386     if (Immediate::is_uimm12(state_offset)) {
2387       __ z_cli(state_offset, iklass, InstanceKlass::fully_initialized);
2388     } else {
2389       __ z_cliy(state_offset, iklass, InstanceKlass::fully_initialized);
2390     }
2391     __ branch_optimized(Assembler::bcondNotEqual, *op->stub()->entry()); // Use long branch, because slow_case might be far.
2392   }
2393   __ allocate_object(op->obj()->as_register(),
2394                      op->tmp1()->as_register(),
2395                      op->tmp2()->as_register(),
2396                      op->header_size(),
2397                      op->object_size(),
2398                      op->klass()->as_register(),
2399                      *op->stub()->entry());
2400   __ bind(*op->stub()->continuation());
2401   __ verify_oop(op->obj()->as_register());
2402 }
2403 
emit_alloc_array(LIR_OpAllocArray * op)2404 void LIR_Assembler::emit_alloc_array(LIR_OpAllocArray* op) {
2405   Register len = op->len()->as_register();
2406   __ move_reg_if_needed(len, T_LONG, len, T_INT); // sign extend
2407 
2408   if (UseSlowPath ||
2409       (!UseFastNewObjectArray && (op->type() == T_OBJECT || op->type() == T_ARRAY)) ||
2410       (!UseFastNewTypeArray   && (op->type() != T_OBJECT && op->type() != T_ARRAY))) {
2411     __ z_brul(*op->stub()->entry());
2412   } else {
2413     __ allocate_array(op->obj()->as_register(),
2414                       op->len()->as_register(),
2415                       op->tmp1()->as_register(),
2416                       op->tmp2()->as_register(),
2417                       arrayOopDesc::header_size(op->type()),
2418                       type2aelembytes(op->type()),
2419                       op->klass()->as_register(),
2420                       *op->stub()->entry());
2421   }
2422   __ bind(*op->stub()->continuation());
2423 }
2424 
type_profile_helper(Register mdo,ciMethodData * md,ciProfileData * data,Register recv,Register tmp1,Label * update_done)2425 void LIR_Assembler::type_profile_helper(Register mdo, 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     __ z_cg(recv, receiver_addr);
2433     __ z_brne(next_test);
2434     Address data_addr(mdo, md->byte_offset_of_slot(data, ReceiverTypeData::receiver_count_offset(i)));
2435     __ add2mem_64(data_addr, DataLayout::counter_increment, tmp1);
2436     __ branch_optimized(Assembler::bcondAlways, *update_done);
2437     __ bind(next_test);
2438   }
2439 
2440   // Didn't find receiver; find next empty slot and fill it in.
2441   for (i = 0; i < VirtualCallData::row_limit(); i++) {
2442     Label next_test;
2443     Address recv_addr(mdo, md->byte_offset_of_slot(data, ReceiverTypeData::receiver_offset(i)));
2444     __ z_ltg(Z_R0_scratch, recv_addr);
2445     __ z_brne(next_test);
2446     __ z_stg(recv, recv_addr);
2447     __ load_const_optimized(tmp1, DataLayout::counter_increment);
2448     __ z_stg(tmp1, md->byte_offset_of_slot(data, ReceiverTypeData::receiver_count_offset(i)), mdo);
2449     __ branch_optimized(Assembler::bcondAlways, *update_done);
2450     __ bind(next_test);
2451   }
2452 }
2453 
setup_md_access(ciMethod * method,int bci,ciMethodData * & md,ciProfileData * & data,int & mdo_offset_bias)2454 void LIR_Assembler::setup_md_access(ciMethod* method, int bci,
2455                                     ciMethodData*& md, ciProfileData*& data, int& mdo_offset_bias) {
2456   Unimplemented();
2457 }
2458 
store_parameter(Register r,int param_num)2459 void LIR_Assembler::store_parameter(Register r, int param_num) {
2460   assert(param_num >= 0, "invalid num");
2461   int offset_in_bytes = param_num * BytesPerWord + FrameMap::first_available_sp_in_frame;
2462   assert(offset_in_bytes < frame_map()->reserved_argument_area_size(), "invalid offset");
2463   __ z_stg(r, offset_in_bytes, Z_SP);
2464 }
2465 
store_parameter(jint c,int param_num)2466 void LIR_Assembler::store_parameter(jint c, int param_num) {
2467   assert(param_num >= 0, "invalid num");
2468   int offset_in_bytes = param_num * BytesPerWord + FrameMap::first_available_sp_in_frame;
2469   assert(offset_in_bytes < frame_map()->reserved_argument_area_size(), "invalid offset");
2470   __ store_const(Address(Z_SP, offset_in_bytes), c, Z_R1_scratch, true);
2471 }
2472 
emit_typecheck_helper(LIR_OpTypeCheck * op,Label * success,Label * failure,Label * obj_is_null)2473 void LIR_Assembler::emit_typecheck_helper(LIR_OpTypeCheck *op, Label* success, Label* failure, Label* obj_is_null) {
2474   // We always need a stub for the failure case.
2475   CodeStub* stub = op->stub();
2476   Register obj = op->object()->as_register();
2477   Register k_RInfo = op->tmp1()->as_register();
2478   Register klass_RInfo = op->tmp2()->as_register();
2479   Register dst = op->result_opr()->as_register();
2480   Register Rtmp1 = Z_R1_scratch;
2481   ciKlass* k = op->klass();
2482 
2483   assert(!op->tmp3()->is_valid(), "tmp3's not needed");
2484 
2485   // Check if it needs to be profiled.
2486   ciMethodData* md = NULL;
2487   ciProfileData* data = NULL;
2488 
2489   if (op->should_profile()) {
2490     ciMethod* method = op->profiled_method();
2491     assert(method != NULL, "Should have method");
2492     int bci = op->profiled_bci();
2493     md = method->method_data_or_null();
2494     assert(md != NULL, "Sanity");
2495     data = md->bci_to_data(bci);
2496     assert(data != NULL,                "need data for type check");
2497     assert(data->is_ReceiverTypeData(), "need ReceiverTypeData for type check");
2498   }
2499 
2500   // Temp operands do not overlap with inputs, if this is their last
2501   // use (end of range is exclusive), so a register conflict is possible.
2502   if (obj == k_RInfo) {
2503     k_RInfo = dst;
2504   } else if (obj == klass_RInfo) {
2505     klass_RInfo = dst;
2506   }
2507   assert_different_registers(obj, k_RInfo, klass_RInfo);
2508 
2509   if (op->should_profile()) {
2510     NearLabel not_null;
2511     __ compareU64_and_branch(obj, (intptr_t) 0, Assembler::bcondNotEqual, not_null);
2512     // Object is null; update MDO and exit.
2513     Register mdo = klass_RInfo;
2514     metadata2reg(md->constant_encoding(), mdo);
2515     Address data_addr(mdo, md->byte_offset_of_slot(data, DataLayout::header_offset()));
2516     int header_bits = DataLayout::flag_mask_to_header_mask(BitData::null_seen_byte_constant());
2517     __ or2mem_8(data_addr, header_bits);
2518     __ branch_optimized(Assembler::bcondAlways, *obj_is_null);
2519     __ bind(not_null);
2520   } else {
2521     __ compareU64_and_branch(obj, (intptr_t) 0, Assembler::bcondEqual, *obj_is_null);
2522   }
2523 
2524   NearLabel profile_cast_failure, profile_cast_success;
2525   Label *failure_target = op->should_profile() ? &profile_cast_failure : failure;
2526   Label *success_target = op->should_profile() ? &profile_cast_success : success;
2527 
2528   // Patching may screw with our temporaries on sparc,
2529   // so let's do it before loading the class.
2530   if (k->is_loaded()) {
2531     metadata2reg(k->constant_encoding(), k_RInfo);
2532   } else {
2533     klass2reg_with_patching(k_RInfo, op->info_for_patch());
2534   }
2535   assert(obj != k_RInfo, "must be different");
2536 
2537   __ verify_oop(obj);
2538 
2539   // Get object class.
2540   // Not a safepoint as obj null check happens earlier.
2541   if (op->fast_check()) {
2542     if (UseCompressedClassPointers) {
2543       __ load_klass(klass_RInfo, obj);
2544       __ compareU64_and_branch(k_RInfo, klass_RInfo, Assembler::bcondNotEqual, *failure_target);
2545     } else {
2546       __ z_cg(k_RInfo, Address(obj, oopDesc::klass_offset_in_bytes()));
2547       __ branch_optimized(Assembler::bcondNotEqual, *failure_target);
2548     }
2549     // Successful cast, fall through to profile or jump.
2550   } else {
2551     bool need_slow_path = !k->is_loaded() ||
2552                           ((int) k->super_check_offset() == in_bytes(Klass::secondary_super_cache_offset()));
2553     intptr_t super_check_offset = k->is_loaded() ? k->super_check_offset() : -1L;
2554     __ load_klass(klass_RInfo, obj);
2555     // Perform the fast part of the checking logic.
2556     __ check_klass_subtype_fast_path(klass_RInfo, k_RInfo, Rtmp1,
2557                                      (need_slow_path ? success_target : NULL),
2558                                      failure_target, NULL,
2559                                      RegisterOrConstant(super_check_offset));
2560     if (need_slow_path) {
2561       // Call out-of-line instance of __ check_klass_subtype_slow_path(...):
2562       address a = Runtime1::entry_for (Runtime1::slow_subtype_check_id);
2563       store_parameter(klass_RInfo, 0); // sub
2564       store_parameter(k_RInfo, 1);     // super
2565       emit_call_c(a); // Sets condition code 0 for match (2 otherwise).
2566       CHECK_BAILOUT2(profile_cast_failure, profile_cast_success);
2567       __ branch_optimized(Assembler::bcondNotEqual, *failure_target);
2568       // Fall through to success case.
2569     }
2570   }
2571 
2572   if (op->should_profile()) {
2573     Register mdo = klass_RInfo, recv = k_RInfo;
2574     assert_different_registers(obj, mdo, recv);
2575     __ bind(profile_cast_success);
2576     metadata2reg(md->constant_encoding(), mdo);
2577     __ load_klass(recv, obj);
2578     type_profile_helper(mdo, md, data, recv, Rtmp1, success);
2579     __ branch_optimized(Assembler::bcondAlways, *success);
2580 
2581     __ bind(profile_cast_failure);
2582     metadata2reg(md->constant_encoding(), mdo);
2583     __ add2mem_64(Address(mdo, md->byte_offset_of_slot(data, CounterData::count_offset())), -(int)DataLayout::counter_increment, Rtmp1);
2584     __ branch_optimized(Assembler::bcondAlways, *failure);
2585   } else {
2586     __ branch_optimized(Assembler::bcondAlways, *success);
2587   }
2588 }
2589 
emit_opTypeCheck(LIR_OpTypeCheck * op)2590 void LIR_Assembler::emit_opTypeCheck(LIR_OpTypeCheck* op) {
2591   LIR_Code code = op->code();
2592   if (code == lir_store_check) {
2593     Register value = op->object()->as_register();
2594     Register array = op->array()->as_register();
2595     Register k_RInfo = op->tmp1()->as_register();
2596     Register klass_RInfo = op->tmp2()->as_register();
2597     Register Rtmp1 = Z_R1_scratch;
2598 
2599     CodeStub* stub = op->stub();
2600 
2601     // Check if it needs to be profiled.
2602     ciMethodData* md = NULL;
2603     ciProfileData* data = NULL;
2604 
2605     assert_different_registers(value, k_RInfo, klass_RInfo);
2606 
2607     if (op->should_profile()) {
2608       ciMethod* method = op->profiled_method();
2609       assert(method != NULL, "Should have method");
2610       int bci = op->profiled_bci();
2611       md = method->method_data_or_null();
2612       assert(md != NULL, "Sanity");
2613       data = md->bci_to_data(bci);
2614       assert(data != NULL,                "need data for type check");
2615       assert(data->is_ReceiverTypeData(), "need ReceiverTypeData for type check");
2616     }
2617     NearLabel profile_cast_success, profile_cast_failure, done;
2618     Label *success_target = op->should_profile() ? &profile_cast_success : &done;
2619     Label *failure_target = op->should_profile() ? &profile_cast_failure : stub->entry();
2620 
2621     if (op->should_profile()) {
2622       NearLabel not_null;
2623       __ compareU64_and_branch(value, (intptr_t) 0, Assembler::bcondNotEqual, not_null);
2624       // Object is null; update MDO and exit.
2625       Register mdo = klass_RInfo;
2626       metadata2reg(md->constant_encoding(), mdo);
2627       Address data_addr(mdo, md->byte_offset_of_slot(data, DataLayout::header_offset()));
2628       int header_bits = DataLayout::flag_mask_to_header_mask(BitData::null_seen_byte_constant());
2629       __ or2mem_8(data_addr, header_bits);
2630       __ branch_optimized(Assembler::bcondAlways, done);
2631       __ bind(not_null);
2632     } else {
2633       __ compareU64_and_branch(value, (intptr_t) 0, Assembler::bcondEqual, done);
2634     }
2635 
2636     add_debug_info_for_null_check_here(op->info_for_exception());
2637     __ load_klass(k_RInfo, array);
2638     __ load_klass(klass_RInfo, value);
2639 
2640     // Get instance klass (it's already uncompressed).
2641     __ z_lg(k_RInfo, Address(k_RInfo, ObjArrayKlass::element_klass_offset()));
2642     // Perform the fast part of the checking logic.
2643     __ check_klass_subtype_fast_path(klass_RInfo, k_RInfo, Rtmp1, success_target, failure_target, NULL);
2644     // Call out-of-line instance of __ check_klass_subtype_slow_path(...):
2645     address a = Runtime1::entry_for (Runtime1::slow_subtype_check_id);
2646     store_parameter(klass_RInfo, 0); // sub
2647     store_parameter(k_RInfo, 1);     // super
2648     emit_call_c(a); // Sets condition code 0 for match (2 otherwise).
2649     CHECK_BAILOUT3(profile_cast_success, profile_cast_failure, done);
2650     __ branch_optimized(Assembler::bcondNotEqual, *failure_target);
2651     // Fall through to success case.
2652 
2653     if (op->should_profile()) {
2654       Register mdo = klass_RInfo, recv = k_RInfo;
2655       assert_different_registers(value, mdo, recv);
2656       __ bind(profile_cast_success);
2657       metadata2reg(md->constant_encoding(), mdo);
2658       __ load_klass(recv, value);
2659       type_profile_helper(mdo, md, data, recv, Rtmp1, &done);
2660       __ branch_optimized(Assembler::bcondAlways, done);
2661 
2662       __ bind(profile_cast_failure);
2663       metadata2reg(md->constant_encoding(), mdo);
2664       __ add2mem_64(Address(mdo, md->byte_offset_of_slot(data, CounterData::count_offset())), -(int)DataLayout::counter_increment, Rtmp1);
2665       __ branch_optimized(Assembler::bcondAlways, *stub->entry());
2666     }
2667 
2668     __ bind(done);
2669   } else {
2670     if (code == lir_checkcast) {
2671       Register obj = op->object()->as_register();
2672       Register dst = op->result_opr()->as_register();
2673       NearLabel success;
2674       emit_typecheck_helper(op, &success, op->stub()->entry(), &success);
2675       __ bind(success);
2676       __ lgr_if_needed(dst, obj);
2677     } else {
2678       if (code == lir_instanceof) {
2679         Register obj = op->object()->as_register();
2680         Register dst = op->result_opr()->as_register();
2681         NearLabel success, failure, done;
2682         emit_typecheck_helper(op, &success, &failure, &failure);
2683         __ bind(failure);
2684         __ clear_reg(dst);
2685         __ branch_optimized(Assembler::bcondAlways, done);
2686         __ bind(success);
2687         __ load_const_optimized(dst, 1);
2688         __ bind(done);
2689       } else {
2690         ShouldNotReachHere();
2691       }
2692     }
2693   }
2694 }
2695 
emit_compare_and_swap(LIR_OpCompareAndSwap * op)2696 void LIR_Assembler::emit_compare_and_swap(LIR_OpCompareAndSwap* op) {
2697   Register addr = op->addr()->as_pointer_register();
2698   Register t1_cmp = Z_R1_scratch;
2699   if (op->code() == lir_cas_long) {
2700     assert(VM_Version::supports_cx8(), "wrong machine");
2701     Register cmp_value_lo = op->cmp_value()->as_register_lo();
2702     Register new_value_lo = op->new_value()->as_register_lo();
2703     __ z_lgr(t1_cmp, cmp_value_lo);
2704     // Perform the compare and swap operation.
2705     __ z_csg(t1_cmp, new_value_lo, 0, addr);
2706   } else if (op->code() == lir_cas_int || op->code() == lir_cas_obj) {
2707     Register cmp_value = op->cmp_value()->as_register();
2708     Register new_value = op->new_value()->as_register();
2709     if (op->code() == lir_cas_obj) {
2710       if (UseCompressedOops) {
2711                  t1_cmp = op->tmp1()->as_register();
2712         Register t2_new = op->tmp2()->as_register();
2713         assert_different_registers(cmp_value, new_value, addr, t1_cmp, t2_new);
2714         __ oop_encoder(t1_cmp, cmp_value, true /*maybe null*/);
2715         __ oop_encoder(t2_new, new_value, true /*maybe null*/);
2716         __ z_cs(t1_cmp, t2_new, 0, addr);
2717       } else {
2718         __ z_lgr(t1_cmp, cmp_value);
2719         __ z_csg(t1_cmp, new_value, 0, addr);
2720       }
2721     } else {
2722       __ z_lr(t1_cmp, cmp_value);
2723       __ z_cs(t1_cmp, new_value, 0, addr);
2724     }
2725   } else {
2726     ShouldNotReachHere(); // new lir_cas_??
2727   }
2728 }
2729 
set_24bit_FPU()2730 void LIR_Assembler::set_24bit_FPU() {
2731   ShouldNotCallThis(); // x86 only
2732 }
2733 
reset_FPU()2734 void LIR_Assembler::reset_FPU() {
2735   ShouldNotCallThis(); // x86 only
2736 }
2737 
breakpoint()2738 void LIR_Assembler::breakpoint() {
2739   Unimplemented();
2740   //  __ breakpoint_trap();
2741 }
2742 
push(LIR_Opr opr)2743 void LIR_Assembler::push(LIR_Opr opr) {
2744   ShouldNotCallThis(); // unused
2745 }
2746 
pop(LIR_Opr opr)2747 void LIR_Assembler::pop(LIR_Opr opr) {
2748   ShouldNotCallThis(); // unused
2749 }
2750 
monitor_address(int monitor_no,LIR_Opr dst_opr)2751 void LIR_Assembler::monitor_address(int monitor_no, LIR_Opr dst_opr) {
2752   Address addr = frame_map()->address_for_monitor_lock(monitor_no);
2753   __ add2reg(dst_opr->as_register(), addr.disp(), addr.base());
2754 }
2755 
emit_lock(LIR_OpLock * op)2756 void LIR_Assembler::emit_lock(LIR_OpLock* op) {
2757   Register obj = op->obj_opr()->as_register();  // May not be an oop.
2758   Register hdr = op->hdr_opr()->as_register();
2759   Register lock = op->lock_opr()->as_register();
2760   if (!UseFastLocking) {
2761     __ branch_optimized(Assembler::bcondAlways, *op->stub()->entry());
2762   } else if (op->code() == lir_lock) {
2763     assert(BasicLock::displaced_header_offset_in_bytes() == 0, "lock_reg must point to the displaced header");
2764     // Add debug info for NullPointerException only if one is possible.
2765     if (op->info() != NULL) {
2766       add_debug_info_for_null_check_here(op->info());
2767     }
2768     __ lock_object(hdr, obj, lock, *op->stub()->entry());
2769     // done
2770   } else if (op->code() == lir_unlock) {
2771     assert(BasicLock::displaced_header_offset_in_bytes() == 0, "lock_reg must point to the displaced header");
2772     __ unlock_object(hdr, obj, lock, *op->stub()->entry());
2773   } else {
2774     ShouldNotReachHere();
2775   }
2776   __ bind(*op->stub()->continuation());
2777 }
2778 
emit_profile_call(LIR_OpProfileCall * op)2779 void LIR_Assembler::emit_profile_call(LIR_OpProfileCall* op) {
2780   ciMethod* method = op->profiled_method();
2781   int bci          = op->profiled_bci();
2782   ciMethod* callee = op->profiled_callee();
2783 
2784   // Update counter for all call types.
2785   ciMethodData* md = method->method_data_or_null();
2786   assert(md != NULL, "Sanity");
2787   ciProfileData* data = md->bci_to_data(bci);
2788   assert(data != NULL && data->is_CounterData(), "need CounterData for calls");
2789   assert(op->mdo()->is_single_cpu(),  "mdo must be allocated");
2790   Register mdo  = op->mdo()->as_register();
2791   assert(op->tmp1()->is_double_cpu(), "tmp1 must be allocated");
2792   Register tmp1 = op->tmp1()->as_register_lo();
2793   metadata2reg(md->constant_encoding(), mdo);
2794 
2795   Address counter_addr(mdo, md->byte_offset_of_slot(data, CounterData::count_offset()));
2796   // Perform additional virtual call profiling for invokevirtual and
2797   // invokeinterface bytecodes
2798   if (op->should_profile_receiver_type()) {
2799     assert(op->recv()->is_single_cpu(), "recv must be allocated");
2800     Register recv = op->recv()->as_register();
2801     assert_different_registers(mdo, tmp1, recv);
2802     assert(data->is_VirtualCallData(), "need VirtualCallData for virtual calls");
2803     ciKlass* known_klass = op->known_holder();
2804     if (C1OptimizeVirtualCallProfiling && known_klass != NULL) {
2805       // We know the type that will be seen at this call site; we can
2806       // statically update the MethodData* rather than needing to do
2807       // dynamic tests on the receiver type.
2808 
2809       // NOTE: we should probably put a lock around this search to
2810       // avoid collisions by concurrent compilations.
2811       ciVirtualCallData* vc_data = (ciVirtualCallData*) data;
2812       uint i;
2813       for (i = 0; i < VirtualCallData::row_limit(); i++) {
2814         ciKlass* receiver = vc_data->receiver(i);
2815         if (known_klass->equals(receiver)) {
2816           Address data_addr(mdo, md->byte_offset_of_slot(data, VirtualCallData::receiver_count_offset(i)));
2817           __ add2mem_64(data_addr, DataLayout::counter_increment, tmp1);
2818           return;
2819         }
2820       }
2821 
2822       // Receiver type not found in profile data. Select an empty slot.
2823 
2824       // Note that this is less efficient than it should be because it
2825       // always does a write to the receiver part of the
2826       // VirtualCallData rather than just the first time.
2827       for (i = 0; i < VirtualCallData::row_limit(); i++) {
2828         ciKlass* receiver = vc_data->receiver(i);
2829         if (receiver == NULL) {
2830           Address recv_addr(mdo, md->byte_offset_of_slot(data, VirtualCallData::receiver_offset(i)));
2831           metadata2reg(known_klass->constant_encoding(), tmp1);
2832           __ z_stg(tmp1, recv_addr);
2833           Address data_addr(mdo, md->byte_offset_of_slot(data, VirtualCallData::receiver_count_offset(i)));
2834           __ add2mem_64(data_addr, DataLayout::counter_increment, tmp1);
2835           return;
2836         }
2837       }
2838     } else {
2839       __ load_klass(recv, recv);
2840       NearLabel update_done;
2841       type_profile_helper(mdo, md, data, recv, tmp1, &update_done);
2842       // Receiver did not match any saved receiver and there is no empty row for it.
2843       // Increment total counter to indicate polymorphic case.
2844       __ add2mem_64(counter_addr, DataLayout::counter_increment, tmp1);
2845       __ bind(update_done);
2846     }
2847   } else {
2848     // static call
2849     __ add2mem_64(counter_addr, DataLayout::counter_increment, tmp1);
2850   }
2851 }
2852 
align_backward_branch_target()2853 void LIR_Assembler::align_backward_branch_target() {
2854   __ align(OptoLoopAlignment);
2855 }
2856 
emit_delay(LIR_OpDelay * op)2857 void LIR_Assembler::emit_delay(LIR_OpDelay* op) {
2858   ShouldNotCallThis(); // There are no delay slots on ZARCH_64.
2859 }
2860 
negate(LIR_Opr left,LIR_Opr dest,LIR_Opr tmp)2861 void LIR_Assembler::negate(LIR_Opr left, LIR_Opr dest, LIR_Opr tmp) {
2862   // tmp must be unused
2863   assert(tmp->is_illegal(), "wasting a register if tmp is allocated");
2864   assert(left->is_register(), "can only handle registers");
2865 
2866   if (left->is_single_cpu()) {
2867     __ z_lcr(dest->as_register(), left->as_register());
2868   } else if (left->is_single_fpu()) {
2869     __ z_lcebr(dest->as_float_reg(), left->as_float_reg());
2870   } else if (left->is_double_fpu()) {
2871     __ z_lcdbr(dest->as_double_reg(), left->as_double_reg());
2872   } else {
2873     assert(left->is_double_cpu(), "Must be a long");
2874     __ z_lcgr(dest->as_register_lo(), left->as_register_lo());
2875   }
2876 }
2877 
fxch(int i)2878 void LIR_Assembler::fxch(int i) {
2879   ShouldNotCallThis(); // x86 only
2880 }
2881 
fld(int i)2882 void LIR_Assembler::fld(int i) {
2883   ShouldNotCallThis(); // x86 only
2884 }
2885 
ffree(int i)2886 void LIR_Assembler::ffree(int i) {
2887   ShouldNotCallThis(); // x86 only
2888 }
2889 
rt_call(LIR_Opr result,address dest,const LIR_OprList * args,LIR_Opr tmp,CodeEmitInfo * info)2890 void LIR_Assembler::rt_call(LIR_Opr result, address dest,
2891                             const LIR_OprList* args, LIR_Opr tmp, CodeEmitInfo* info) {
2892   assert(!tmp->is_valid(), "don't need temporary");
2893   emit_call_c(dest);
2894   CHECK_BAILOUT();
2895   if (info != NULL) {
2896     add_call_info_here(info);
2897   }
2898 }
2899 
volatile_move_op(LIR_Opr src,LIR_Opr dest,BasicType type,CodeEmitInfo * info)2900 void LIR_Assembler::volatile_move_op(LIR_Opr src, LIR_Opr dest, BasicType type, CodeEmitInfo* info) {
2901   ShouldNotCallThis(); // not needed on ZARCH_64
2902 }
2903 
membar()2904 void LIR_Assembler::membar() {
2905   __ z_fence();
2906 }
2907 
membar_acquire()2908 void LIR_Assembler::membar_acquire() {
2909   __ z_acquire();
2910 }
2911 
membar_release()2912 void LIR_Assembler::membar_release() {
2913   __ z_release();
2914 }
2915 
membar_loadload()2916 void LIR_Assembler::membar_loadload() {
2917   __ z_acquire();
2918 }
2919 
membar_storestore()2920 void LIR_Assembler::membar_storestore() {
2921   __ z_release();
2922 }
2923 
membar_loadstore()2924 void LIR_Assembler::membar_loadstore() {
2925   __ z_acquire();
2926 }
2927 
membar_storeload()2928 void LIR_Assembler::membar_storeload() {
2929   __ z_fence();
2930 }
2931 
on_spin_wait()2932 void LIR_Assembler::on_spin_wait() {
2933   Unimplemented();
2934 }
2935 
leal(LIR_Opr addr_opr,LIR_Opr dest,LIR_PatchCode patch_code,CodeEmitInfo * info)2936 void LIR_Assembler::leal(LIR_Opr addr_opr, LIR_Opr dest, LIR_PatchCode patch_code, CodeEmitInfo* info) {
2937   assert(patch_code == lir_patch_none, "Patch code not supported");
2938   LIR_Address* addr = addr_opr->as_address_ptr();
2939   assert(addr->scale() == LIR_Address::times_1, "scaling unsupported");
2940   __ load_address(dest->as_pointer_register(), as_Address(addr));
2941 }
2942 
get_thread(LIR_Opr result_reg)2943 void LIR_Assembler::get_thread(LIR_Opr result_reg) {
2944   ShouldNotCallThis(); // unused
2945 }
2946 
2947 #ifdef ASSERT
2948 // Emit run-time assertion.
emit_assert(LIR_OpAssert * op)2949 void LIR_Assembler::emit_assert(LIR_OpAssert* op) {
2950   Unimplemented();
2951 }
2952 #endif
2953 
peephole(LIR_List *)2954 void LIR_Assembler::peephole(LIR_List*) {
2955   // Do nothing for now.
2956 }
2957 
atomic_op(LIR_Code code,LIR_Opr src,LIR_Opr data,LIR_Opr dest,LIR_Opr tmp)2958 void LIR_Assembler::atomic_op(LIR_Code code, LIR_Opr src, LIR_Opr data, LIR_Opr dest, LIR_Opr tmp) {
2959   assert(code == lir_xadd, "lir_xchg not supported");
2960   Address src_addr = as_Address(src->as_address_ptr());
2961   Register base = src_addr.base();
2962   intptr_t disp = src_addr.disp();
2963   if (src_addr.index()->is_valid()) {
2964     // LAA and LAAG do not support index register.
2965     __ load_address(Z_R1_scratch, src_addr);
2966     base = Z_R1_scratch;
2967     disp = 0;
2968   }
2969   if (data->type() == T_INT) {
2970     __ z_laa(dest->as_register(), data->as_register(), disp, base);
2971   } else if (data->type() == T_LONG) {
2972     assert(data->as_register_lo() == data->as_register_hi(), "should be a single register");
2973     __ z_laag(dest->as_register_lo(), data->as_register_lo(), disp, base);
2974   } else {
2975     ShouldNotReachHere();
2976   }
2977 }
2978 
emit_profile_type(LIR_OpProfileType * op)2979 void LIR_Assembler::emit_profile_type(LIR_OpProfileType* op) {
2980   Register obj = op->obj()->as_register();
2981   Register tmp1 = op->tmp()->as_pointer_register();
2982   Register tmp2 = Z_R1_scratch;
2983   Address mdo_addr = as_Address(op->mdp()->as_address_ptr());
2984   ciKlass* exact_klass = op->exact_klass();
2985   intptr_t current_klass = op->current_klass();
2986   bool not_null = op->not_null();
2987   bool no_conflict = op->no_conflict();
2988 
2989   Label update, next, none, null_seen, init_klass;
2990 
2991   bool do_null = !not_null;
2992   bool exact_klass_set = exact_klass != NULL && ciTypeEntries::valid_ciklass(current_klass) == exact_klass;
2993   bool do_update = !TypeEntries::is_type_unknown(current_klass) && !exact_klass_set;
2994 
2995   assert(do_null || do_update, "why are we here?");
2996   assert(!TypeEntries::was_null_seen(current_klass) || do_update, "why are we here?");
2997 
2998   __ verify_oop(obj);
2999 
3000   if (do_null || tmp1 != obj DEBUG_ONLY(|| true)) {
3001     __ z_ltgr(tmp1, obj);
3002   }
3003   if (do_null) {
3004     __ z_brnz(update);
3005     if (!TypeEntries::was_null_seen(current_klass)) {
3006       __ z_lg(tmp1, mdo_addr);
3007       __ z_oill(tmp1, TypeEntries::null_seen);
3008       __ z_stg(tmp1, mdo_addr);
3009     }
3010     if (do_update) {
3011       __ z_bru(next);
3012     }
3013   } else {
3014     __ asm_assert_ne("unexpect null obj", __LINE__);
3015   }
3016 
3017   __ bind(update);
3018 
3019   if (do_update) {
3020 #ifdef ASSERT
3021     if (exact_klass != NULL) {
3022       __ load_klass(tmp1, tmp1);
3023       metadata2reg(exact_klass->constant_encoding(), tmp2);
3024       __ z_cgr(tmp1, tmp2);
3025       __ asm_assert_eq("exact klass and actual klass differ", __LINE__);
3026     }
3027 #endif
3028 
3029     Label do_update;
3030     __ z_lg(tmp2, mdo_addr);
3031 
3032     if (!no_conflict) {
3033       if (exact_klass == NULL || TypeEntries::is_type_none(current_klass)) {
3034         if (exact_klass != NULL) {
3035           metadata2reg(exact_klass->constant_encoding(), tmp1);
3036         } else {
3037           __ load_klass(tmp1, tmp1);
3038         }
3039 
3040         // Klass seen before: nothing to do (regardless of unknown bit).
3041         __ z_lgr(Z_R0_scratch, tmp2);
3042         assert(Immediate::is_uimm(~TypeEntries::type_klass_mask, 16), "or change following instruction");
3043         __ z_nill(Z_R0_scratch, TypeEntries::type_klass_mask & 0xFFFF);
3044         __ compareU64_and_branch(Z_R0_scratch, tmp1, Assembler::bcondEqual, next);
3045 
3046         // Already unknown: Nothing to do anymore.
3047         __ z_tmll(tmp2, TypeEntries::type_unknown);
3048         __ z_brc(Assembler::bcondAllOne, next);
3049 
3050         if (TypeEntries::is_type_none(current_klass)) {
3051           __ z_lgr(Z_R0_scratch, tmp2);
3052           assert(Immediate::is_uimm(~TypeEntries::type_mask, 16), "or change following instruction");
3053           __ z_nill(Z_R0_scratch, TypeEntries::type_mask & 0xFFFF);
3054           __ compareU64_and_branch(Z_R0_scratch, (intptr_t)0, Assembler::bcondEqual, init_klass);
3055         }
3056       } else {
3057         assert(ciTypeEntries::valid_ciklass(current_klass) != NULL &&
3058                ciTypeEntries::valid_ciklass(current_klass) != exact_klass, "conflict only");
3059 
3060         // Already unknown: Nothing to do anymore.
3061         __ z_tmll(tmp2, TypeEntries::type_unknown);
3062         __ z_brc(Assembler::bcondAllOne, next);
3063       }
3064 
3065       // Different than before. Cannot keep accurate profile.
3066       __ z_oill(tmp2, TypeEntries::type_unknown);
3067       __ z_bru(do_update);
3068     } else {
3069       // There's a single possible klass at this profile point.
3070       assert(exact_klass != NULL, "should be");
3071       if (TypeEntries::is_type_none(current_klass)) {
3072         metadata2reg(exact_klass->constant_encoding(), tmp1);
3073         __ z_lgr(Z_R0_scratch, tmp2);
3074         assert(Immediate::is_uimm(~TypeEntries::type_klass_mask, 16), "or change following instruction");
3075         __ z_nill(Z_R0_scratch, TypeEntries::type_klass_mask & 0xFFFF);
3076         __ compareU64_and_branch(Z_R0_scratch, tmp1, Assembler::bcondEqual, next);
3077 #ifdef ASSERT
3078         {
3079           Label ok;
3080           __ z_lgr(Z_R0_scratch, tmp2);
3081           assert(Immediate::is_uimm(~TypeEntries::type_mask, 16), "or change following instruction");
3082           __ z_nill(Z_R0_scratch, TypeEntries::type_mask & 0xFFFF);
3083           __ compareU64_and_branch(Z_R0_scratch, (intptr_t)0, Assembler::bcondEqual, ok);
3084           __ stop("unexpected profiling mismatch");
3085           __ bind(ok);
3086         }
3087 #endif
3088 
3089       } else {
3090         assert(ciTypeEntries::valid_ciklass(current_klass) != NULL &&
3091                ciTypeEntries::valid_ciklass(current_klass) != exact_klass, "inconsistent");
3092 
3093         // Already unknown: Nothing to do anymore.
3094         __ z_tmll(tmp2, TypeEntries::type_unknown);
3095         __ z_brc(Assembler::bcondAllOne, next);
3096         __ z_oill(tmp2, TypeEntries::type_unknown);
3097         __ z_bru(do_update);
3098       }
3099     }
3100 
3101     __ bind(init_klass);
3102     // Combine klass and null_seen bit (only used if (tmp & type_mask)==0).
3103     __ z_ogr(tmp2, tmp1);
3104 
3105     __ bind(do_update);
3106     __ z_stg(tmp2, mdo_addr);
3107 
3108     __ bind(next);
3109   }
3110 }
3111 
emit_updatecrc32(LIR_OpUpdateCRC32 * op)3112 void LIR_Assembler::emit_updatecrc32(LIR_OpUpdateCRC32* op) {
3113   assert(op->crc()->is_single_cpu(), "crc must be register");
3114   assert(op->val()->is_single_cpu(), "byte value must be register");
3115   assert(op->result_opr()->is_single_cpu(), "result must be register");
3116   Register crc = op->crc()->as_register();
3117   Register val = op->val()->as_register();
3118   Register res = op->result_opr()->as_register();
3119 
3120   assert_different_registers(val, crc, res);
3121 
3122   __ load_const_optimized(res, StubRoutines::crc_table_addr());
3123   __ kernel_crc32_singleByteReg(crc, val, res, true);
3124   __ z_lgfr(res, crc);
3125 }
3126 
3127 #undef __
3128