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