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(), _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 != LIR_Opr::nullOpr(), "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(RuntimeAddress(Runtime1::entry_for(Runtime1::throw_div0_exception_id)));
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, CodeEmitInfo* info)
214 : MonitorAccessStub(obj_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(),  0);
224   Runtime1::StubID enter_id;
225   if (ce->compilation()->has_fpu_code()) {
226     enter_id = Runtime1::monitorenter_id;
227   } else {
228     enter_id = Runtime1::monitorenter_nofpu_id;
229   }
230   __ far_call(RuntimeAddress(Runtime1::entry_for(enter_id)));
231   ce->add_call_info_here(_info);
232   ce->verify_oop_map(_info);
233   __ b(_continuation);
234 }
235 
236 
237 void MonitorExitStub::emit_code(LIR_Assembler* ce) {
238   __ bind(_entry);
239   ce->store_parameter(_obj_reg->as_register(), 0);
240   // note: non-blocking leaf routine => no call info needed
241   Runtime1::StubID exit_id;
242   if (ce->compilation()->has_fpu_code()) {
243     exit_id = Runtime1::monitorexit_id;
244   } else {
245     exit_id = Runtime1::monitorexit_nofpu_id;
246   }
247   __ adr(lr, _continuation);
248   __ far_jump(RuntimeAddress(Runtime1::entry_for(exit_id)));
249 }
250 
251 void LoadKlassStub::emit_code(LIR_Assembler* ce) {
252   __ bind(_entry);
253   Register res = _result->as_register();
254   ce->store_parameter(_obj->as_register(), 0);
255   if (res != r0) {
256     // Note: we cannot push/pop r0 around the call, because that
257     // would mess with the stack pointer sp, and we need that to
258     // remain intact for store_paramater/load_argument to work correctly.
259     // We swap r0 and res instead, which preserves current r0 in res.
260     // The preserved value is later saved and restored around the
261     // call in Runtime1::load_klass_id.
262     __ mov(rscratch1, r0);
263     __ mov(r0, res);
264     __ mov(res, rscratch1);
265   }
266   __ far_call(RuntimeAddress(Runtime1::entry_for(Runtime1::load_klass_id)));
267   if (res != r0) {
268     // Swap back r0 and res. This brings the call return value
269     // from r0 into res, and the preserved value in res back into r0.
270     __ mov(rscratch1, r0);
271     __ mov(r0, res);
272     __ mov(res, rscratch1);
273   }
274   __ b(_continuation);
275 }
276 
277 // Implementation of patching:
278 // - Copy the code at given offset to an inlined buffer (first the bytes, then the number of bytes)
279 // - Replace original code with a call to the stub
280 // At Runtime:
281 // - call to stub, jump to runtime
282 // - in runtime: preserve all registers (rspecially objects, i.e., source and destination object)
283 // - in runtime: after initializing class, restore original code, reexecute instruction
284 
285 int PatchingStub::_patch_info_offset = -NativeGeneralJump::instruction_size;
286 
287 void PatchingStub::align_patch_site(MacroAssembler* masm) {
288 }
289 
290 void PatchingStub::emit_code(LIR_Assembler* ce) {
291   assert(false, "AArch64 should not use C1 runtime patching");
292 }
293 
294 
295 void DeoptimizeStub::emit_code(LIR_Assembler* ce) {
296   __ bind(_entry);
297   ce->store_parameter(_trap_request, 0);
298   __ far_call(RuntimeAddress(Runtime1::entry_for(Runtime1::deoptimize_id)));
299   ce->add_call_info_here(_info);
300   DEBUG_ONLY(__ should_not_reach_here());
301 }
302 
303 
304 void ImplicitNullCheckStub::emit_code(LIR_Assembler* ce) {
305   address a;
306   if (_info->deoptimize_on_exception()) {
307     // Deoptimize, do not throw the exception, because it is probably wrong to do it here.
308     a = Runtime1::entry_for(Runtime1::predicate_failed_trap_id);
309   } else {
310     a = Runtime1::entry_for(Runtime1::throw_null_pointer_exception_id);
311   }
312 
313   ce->compilation()->implicit_exception_table()->append(_offset, __ offset());
314   __ bind(_entry);
315   __ far_call(RuntimeAddress(a));
316   ce->add_call_info_here(_info);
317   ce->verify_oop_map(_info);
318   debug_only(__ should_not_reach_here());
319 }
320 
321 
322 void SimpleExceptionStub::emit_code(LIR_Assembler* ce) {
323   assert(__ rsp_offset() == 0, "frame size should be fixed");
324 
325   __ bind(_entry);
326   // pass the object in a scratch register because all other registers
327   // must be preserved
328   if (_obj->is_cpu_register()) {
329     __ mov(rscratch1, _obj->as_register());
330   }
331   __ far_call(RuntimeAddress(Runtime1::entry_for(_stub)), rscratch2);
332   ce->add_call_info_here(_info);
333   debug_only(__ should_not_reach_here());
334 }
335 
336 
337 void ArrayCopyStub::emit_code(LIR_Assembler* ce) {
338   //---------------slow case: call to native-----------------
339   __ bind(_entry);
340   // Figure out where the args should go
341   // This should really convert the IntrinsicID to the Method* and signature
342   // but I don't know how to do that.
343   //
344   VMRegPair args[5];
345   BasicType signature[5] = { T_OBJECT, T_INT, T_OBJECT, T_INT, T_INT};
346   SharedRuntime::java_calling_convention(signature, args, 5);
347 
348   // push parameters
349   // (src, src_pos, dest, destPos, length)
350   Register r[5];
351   r[0] = src()->as_register();
352   r[1] = src_pos()->as_register();
353   r[2] = dst()->as_register();
354   r[3] = dst_pos()->as_register();
355   r[4] = length()->as_register();
356 
357   // next registers will get stored on the stack
358   for (int i = 0; i < 5 ; i++ ) {
359     VMReg r_1 = args[i].first();
360     if (r_1->is_stack()) {
361       int st_off = r_1->reg2stack() * wordSize;
362       __ str (r[i], Address(sp, st_off));
363     } else {
364       assert(r[i] == args[i].first()->as_Register(), "Wrong register for arg ");
365     }
366   }
367 
368   ce->align_call(lir_static_call);
369 
370   ce->emit_static_call_stub();
371   if (ce->compilation()->bailed_out()) {
372     return; // CodeCache is full
373   }
374   Address resolve(SharedRuntime::get_resolve_static_call_stub(),
375                   relocInfo::static_call_type);
376   address call = __ trampoline_call(resolve);
377   if (call == NULL) {
378     ce->bailout("trampoline stub overflow");
379     return;
380   }
381   ce->add_call_info_here(info());
382 
383 #ifndef PRODUCT
384   if (PrintC1Statistics) {
385     __ lea(rscratch2, ExternalAddress((address)&Runtime1::_arraycopy_slowcase_cnt));
386     __ incrementw(Address(rscratch2));
387   }
388 #endif
389 
390   __ b(_continuation);
391 }
392 
393 #undef __