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