1 /*
  2  * Copyright (c) 1999, 2020, Oracle and/or its affiliates. All rights reserved.
  3  * Copyright (c) 2014, Red Hat Inc. 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_CodeStubs.hpp"
 29 #include "c1/c1_FrameMap.hpp"
 30 #include "c1/c1_LIRAssembler.hpp"
 31 #include "c1/c1_MacroAssembler.hpp"
 32 #include "c1/c1_Runtime1.hpp"
 33 #include "classfile/javaClasses.hpp"
 34 #include "nativeInst_aarch64.hpp"
 35 #include "runtime/sharedRuntime.hpp"
 36 #include "vmreg_aarch64.inline.hpp"
 37 
 38 
 39 #define __ ce->masm()->
 40 
 41 void CounterOverflowStub::emit_code(LIR_Assembler* ce) {
 42   __ bind(_entry);
 43   Metadata *m = _method->as_constant_ptr()->as_metadata();
 44   __ mov_metadata(rscratch1, m);
 45   ce->store_parameter(rscratch1, 1);
 46   ce->store_parameter(_bci, 0);
 47   __ far_call(RuntimeAddress(Runtime1::entry_for(Runtime1::counter_overflow_id)));
 48   ce->add_call_info_here(_info);
 49   ce->verify_oop_map(_info);
 50   __ b(_continuation);
 51 }
 52 
 53 RangeCheckStub::RangeCheckStub(CodeEmitInfo* info, LIR_Opr index, LIR_Opr array)
 54   : _index(index), _array(array), _throw_index_out_of_bounds_exception(false) {
 55   assert(info != NULL, "must have info");
 56   _info = new CodeEmitInfo(info);
 57 }
 58 
 59 RangeCheckStub::RangeCheckStub(CodeEmitInfo* info, LIR_Opr index)
 60   : _index(index), _array(NULL), _throw_index_out_of_bounds_exception(true) {
 61   assert(info != NULL, "must have info");
 62   _info = new CodeEmitInfo(info);
 63 }
 64 
 65 void RangeCheckStub::emit_code(LIR_Assembler* ce) {
 66   __ bind(_entry);
 67   if (_info->deoptimize_on_exception()) {
 68     address a = Runtime1::entry_for(Runtime1::predicate_failed_trap_id);
 69     __ far_call(RuntimeAddress(a));
 70     ce->add_call_info_here(_info);
 71     ce->verify_oop_map(_info);
 72     debug_only(__ should_not_reach_here());
 73     return;
 74   }
 75 
 76   if (_index->is_cpu_register()) {
 77     __ mov(rscratch1, _index->as_register());
 78   } else {
 79     __ mov(rscratch1, _index->as_jint());
 80   }
 81   Runtime1::StubID stub_id;
 82   if (_throw_index_out_of_bounds_exception) {
 83     stub_id = Runtime1::throw_index_exception_id;
 84   } else {
 85     assert(_array != NULL, "sanity");
 86     __ mov(rscratch2, _array->as_pointer_register());
 87     stub_id = Runtime1::throw_range_check_failed_id;
 88   }
 89   __ lea(lr, RuntimeAddress(Runtime1::entry_for(stub_id)));
 90   __ blr(lr);
 91   ce->add_call_info_here(_info);
 92   ce->verify_oop_map(_info);
 93   debug_only(__ should_not_reach_here());
 94 }
 95 
 96 PredicateFailedStub::PredicateFailedStub(CodeEmitInfo* info) {
 97   _info = new CodeEmitInfo(info);
 98 }
 99 
100 void PredicateFailedStub::emit_code(LIR_Assembler* ce) {
101   __ bind(_entry);
102   address a = Runtime1::entry_for(Runtime1::predicate_failed_trap_id);
103   __ far_call(RuntimeAddress(a));
104   ce->add_call_info_here(_info);
105   ce->verify_oop_map(_info);
106   debug_only(__ should_not_reach_here());
107 }
108 
109 void DivByZeroStub::emit_code(LIR_Assembler* ce) {
110   if (_offset != -1) {
111     ce->compilation()->implicit_exception_table()->append(_offset, __ offset());
112   }
113   __ bind(_entry);
114   __ far_call(Address(Runtime1::entry_for(Runtime1::throw_div0_exception_id), relocInfo::runtime_call_type));
115   ce->add_call_info_here(_info);
116   ce->verify_oop_map(_info);
117 #ifdef ASSERT
118   __ should_not_reach_here();
119 #endif
120 }
121 
122 
123 
124 // Implementation of NewInstanceStub
125 
126 NewInstanceStub::NewInstanceStub(LIR_Opr klass_reg, LIR_Opr result, ciInstanceKlass* klass, CodeEmitInfo* info, Runtime1::StubID stub_id) {
127   _result = result;
128   _klass = klass;
129   _klass_reg = klass_reg;
130   _info = new CodeEmitInfo(info);
131   assert(stub_id == Runtime1::new_instance_id                 ||
132          stub_id == Runtime1::fast_new_instance_id            ||
133          stub_id == Runtime1::fast_new_instance_init_check_id,
134          "need new_instance id");
135   _stub_id   = stub_id;
136 }
137 
138 
139 
140 void NewInstanceStub::emit_code(LIR_Assembler* ce) {
141   assert(__ rsp_offset() == 0, "frame size should be fixed");
142   __ bind(_entry);
143   __ mov(r3, _klass_reg->as_register());
144   __ far_call(RuntimeAddress(Runtime1::entry_for(_stub_id)));
145   ce->add_call_info_here(_info);
146   ce->verify_oop_map(_info);
147   assert(_result->as_register() == r0, "result must in r0,");
148   __ b(_continuation);
149 }
150 
151 
152 // Implementation of NewTypeArrayStub
153 
154 // Implementation of NewTypeArrayStub
155 
156 NewTypeArrayStub::NewTypeArrayStub(LIR_Opr klass_reg, LIR_Opr length, LIR_Opr result, CodeEmitInfo* info) {
157   _klass_reg = klass_reg;
158   _length = length;
159   _result = result;
160   _info = new CodeEmitInfo(info);
161 }
162 
163 
164 void NewTypeArrayStub::emit_code(LIR_Assembler* ce) {
165   assert(__ rsp_offset() == 0, "frame size should be fixed");
166   __ bind(_entry);
167   assert(_length->as_register() == r19, "length must in r19,");
168   assert(_klass_reg->as_register() == r3, "klass_reg must in r3");
169   __ far_call(RuntimeAddress(Runtime1::entry_for(Runtime1::new_type_array_id)));
170   ce->add_call_info_here(_info);
171   ce->verify_oop_map(_info);
172   assert(_result->as_register() == r0, "result must in r0");
173   __ b(_continuation);
174 }
175 
176 
177 // Implementation of NewObjectArrayStub
178 
179 NewObjectArrayStub::NewObjectArrayStub(LIR_Opr klass_reg, LIR_Opr length, LIR_Opr result, CodeEmitInfo* info) {
180   _klass_reg = klass_reg;
181   _result = result;
182   _length = length;
183   _info = new CodeEmitInfo(info);
184 }
185 
186 
187 void NewObjectArrayStub::emit_code(LIR_Assembler* ce) {
188   assert(__ rsp_offset() == 0, "frame size should be fixed");
189   __ bind(_entry);
190   assert(_length->as_register() == r19, "length must in r19,");
191   assert(_klass_reg->as_register() == r3, "klass_reg must in r3");
192   __ far_call(RuntimeAddress(Runtime1::entry_for(Runtime1::new_object_array_id)));
193   ce->add_call_info_here(_info);
194   ce->verify_oop_map(_info);
195   assert(_result->as_register() == r0, "result must in r0");
196   __ b(_continuation);
197 }
198 // Implementation of MonitorAccessStubs
199 
200 MonitorEnterStub::MonitorEnterStub(LIR_Opr obj_reg, LIR_Opr lock_reg, CodeEmitInfo* info)
201 : MonitorAccessStub(obj_reg, lock_reg)
202 {
203   _info = new CodeEmitInfo(info);
204 }
205 
206 
207 void MonitorEnterStub::emit_code(LIR_Assembler* ce) {
208   assert(__ rsp_offset() == 0, "frame size should be fixed");
209   __ bind(_entry);
210   ce->store_parameter(_obj_reg->as_register(),  1);
211   ce->store_parameter(_lock_reg->as_register(), 0);
212   Runtime1::StubID enter_id;
213   if (ce->compilation()->has_fpu_code()) {
214     enter_id = Runtime1::monitorenter_id;
215   } else {
216     enter_id = Runtime1::monitorenter_nofpu_id;
217   }
218   __ far_call(RuntimeAddress(Runtime1::entry_for(enter_id)));
219   ce->add_call_info_here(_info);
220   ce->verify_oop_map(_info);
221   __ b(_continuation);
222 }
223 
224 
225 void MonitorExitStub::emit_code(LIR_Assembler* ce) {
226   __ bind(_entry);
227   if (_compute_lock) {
228     // lock_reg was destroyed by fast unlocking attempt => recompute it
229     ce->monitor_address(_monitor_ix, _lock_reg);
230   }
231   ce->store_parameter(_lock_reg->as_register(), 0);
232   // note: non-blocking leaf routine => no call info needed
233   Runtime1::StubID exit_id;
234   if (ce->compilation()->has_fpu_code()) {
235     exit_id = Runtime1::monitorexit_id;
236   } else {
237     exit_id = Runtime1::monitorexit_nofpu_id;
238   }
239   __ adr(lr, _continuation);
240   __ far_jump(RuntimeAddress(Runtime1::entry_for(exit_id)));
241 }
242 
243 
244 // Implementation of patching:
245 // - Copy the code at given offset to an inlined buffer (first the bytes, then the number of bytes)
246 // - Replace original code with a call to the stub
247 // At Runtime:
248 // - call to stub, jump to runtime
249 // - in runtime: preserve all registers (rspecially objects, i.e., source and destination object)
250 // - in runtime: after initializing class, restore original code, reexecute instruction
251 
252 int PatchingStub::_patch_info_offset = -NativeGeneralJump::instruction_size;
253 
254 void PatchingStub::align_patch_site(MacroAssembler* masm) {
255 }
256 
257 void PatchingStub::emit_code(LIR_Assembler* ce) {
258   assert(false, "AArch64 should not use C1 runtime patching");
259 }
260 
261 
262 void DeoptimizeStub::emit_code(LIR_Assembler* ce) {
263   __ bind(_entry);
264   ce->store_parameter(_trap_request, 0);
265   __ far_call(RuntimeAddress(Runtime1::entry_for(Runtime1::deoptimize_id)));
266   ce->add_call_info_here(_info);
267   DEBUG_ONLY(__ should_not_reach_here());
268 }
269 
270 
271 void ImplicitNullCheckStub::emit_code(LIR_Assembler* ce) {
272   address a;
273   if (_info->deoptimize_on_exception()) {
274     // Deoptimize, do not throw the exception, because it is probably wrong to do it here.
275     a = Runtime1::entry_for(Runtime1::predicate_failed_trap_id);
276   } else {
277     a = Runtime1::entry_for(Runtime1::throw_null_pointer_exception_id);
278   }
279 
280   ce->compilation()->implicit_exception_table()->append(_offset, __ offset());
281   __ bind(_entry);
282   __ far_call(RuntimeAddress(a));
283   ce->add_call_info_here(_info);
284   ce->verify_oop_map(_info);
285   debug_only(__ should_not_reach_here());
286 }
287 
288 
289 void SimpleExceptionStub::emit_code(LIR_Assembler* ce) {
290   assert(__ rsp_offset() == 0, "frame size should be fixed");
291 
292   __ bind(_entry);
293   // pass the object in a scratch register because all other registers
294   // must be preserved
295   if (_obj->is_cpu_register()) {
296     __ mov(rscratch1, _obj->as_register());
297   }
298   __ far_call(RuntimeAddress(Runtime1::entry_for(_stub)), NULL, rscratch2);
299   ce->add_call_info_here(_info);
300   debug_only(__ should_not_reach_here());
301 }
302 
303 
304 void ArrayCopyStub::emit_code(LIR_Assembler* ce) {
305   //---------------slow case: call to native-----------------
306   __ bind(_entry);
307   // Figure out where the args should go
308   // This should really convert the IntrinsicID to the Method* and signature
309   // but I don't know how to do that.
310   //
311   VMRegPair args[5];
312   BasicType signature[5] = { T_OBJECT, T_INT, T_OBJECT, T_INT, T_INT};
313   SharedRuntime::java_calling_convention(signature, args, 5, true);
314 
315   // push parameters
316   // (src, src_pos, dest, destPos, length)
317   Register r[5];
318   r[0] = src()->as_register();
319   r[1] = src_pos()->as_register();
320   r[2] = dst()->as_register();
321   r[3] = dst_pos()->as_register();
322   r[4] = length()->as_register();
323 
324   // next registers will get stored on the stack
325   for (int i = 0; i < 5 ; i++ ) {
326     VMReg r_1 = args[i].first();
327     if (r_1->is_stack()) {
328       int st_off = r_1->reg2stack() * wordSize;
329       __ str (r[i], Address(sp, st_off));
330     } else {
331       assert(r[i] == args[i].first()->as_Register(), "Wrong register for arg ");
332     }
333   }
334 
335   ce->align_call(lir_static_call);
336 
337   ce->emit_static_call_stub();
338   if (ce->compilation()->bailed_out()) {
339     return; // CodeCache is full
340   }
341   Address resolve(SharedRuntime::get_resolve_static_call_stub(),
342                   relocInfo::static_call_type);
343   address call = __ trampoline_call(resolve);
344   if (call == NULL) {
345     ce->bailout("trampoline stub overflow");
346     return;
347   }
348   ce->add_call_info_here(info());
349 
350 #ifndef PRODUCT
351   __ lea(rscratch2, ExternalAddress((address)&Runtime1::_arraycopy_slowcase_cnt));
352   __ incrementw(Address(rscratch2));
353 #endif
354 
355   __ b(_continuation);
356 }
357 
358 #undef __