1 /*
  2  * Copyright (c) 1999, 2025, 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 "asm/macroAssembler.inline.hpp"
 27 #include "c1/c1_CodeStubs.hpp"
 28 #include "c1/c1_FrameMap.hpp"
 29 #include "c1/c1_LIRAssembler.hpp"
 30 #include "c1/c1_MacroAssembler.hpp"
 31 #include "c1/c1_Runtime1.hpp"
 32 #include "classfile/javaClasses.hpp"
 33 #include "nativeInst_aarch64.hpp"
 34 #include "runtime/sharedRuntime.hpp"
 35 #include "vmreg_aarch64.inline.hpp"
 36 
 37 
 38 #define __ ce->masm()->
 39 
 40 void C1SafepointPollStub::emit_code(LIR_Assembler* ce) {
 41   __ bind(_entry);
 42   InternalAddress safepoint_pc(ce->masm()->pc() - ce->masm()->offset() + safepoint_offset());
 43   __ adr(rscratch1, safepoint_pc);
 44   __ str(rscratch1, Address(rthread, JavaThread::saved_exception_pc_offset()));
 45 
 46   assert(SharedRuntime::polling_page_return_handler_blob() != nullptr,
 47          "polling page return stub not created yet");
 48   address stub = SharedRuntime::polling_page_return_handler_blob()->entry_point();
 49 
 50   __ far_jump(RuntimeAddress(stub));
 51 }
 52 
 53 void CounterOverflowStub::emit_code(LIR_Assembler* ce) {
 54   __ bind(_entry);
 55   Metadata *m = _method->as_constant_ptr()->as_metadata();
 56   __ mov_metadata(rscratch1, m);
 57   ce->store_parameter(rscratch1, 1);
 58   ce->store_parameter(_bci, 0);
 59   __ far_call(RuntimeAddress(Runtime1::entry_for(C1StubId::counter_overflow_id)));
 60   ce->add_call_info_here(_info);
 61   ce->verify_oop_map(_info);
 62   __ b(_continuation);
 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(C1StubId::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   C1StubId stub_id;
 82   if (_throw_index_out_of_bounds_exception) {
 83     stub_id = C1StubId::throw_index_exception_id;
 84   } else {
 85     assert(_array != LIR_Opr::nullOpr(), "sanity");
 86     __ mov(rscratch2, _array->as_pointer_register());
 87     stub_id = C1StubId::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(C1StubId::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(RuntimeAddress(Runtime1::entry_for(C1StubId::throw_div0_exception_id)));
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, C1StubId stub_id) {
127   _result = result;
128   _klass = klass;
129   _klass_reg = klass_reg;
130   _info = new CodeEmitInfo(info);
131   assert(stub_id == C1StubId::new_instance_id                 ||
132          stub_id == C1StubId::fast_new_instance_id            ||
133          stub_id == C1StubId::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(C1StubId::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(C1StubId::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 
199 void MonitorEnterStub::emit_code(LIR_Assembler* ce) {
200   assert(__ rsp_offset() == 0, "frame size should be fixed");
201   __ bind(_entry);
202   ce->store_parameter(_obj_reg->as_register(),  1);
203   ce->store_parameter(_lock_reg->as_register(), 0);
204   C1StubId enter_id;
205   if (ce->compilation()->has_fpu_code()) {
206     enter_id = C1StubId::monitorenter_id;
207   } else {
208     enter_id = C1StubId::monitorenter_nofpu_id;
209   }
210   __ far_call(RuntimeAddress(Runtime1::entry_for(enter_id)));
211   ce->add_call_info_here(_info);
212   ce->verify_oop_map(_info);
213   __ b(_continuation);
214 }
215 
216 
217 void MonitorExitStub::emit_code(LIR_Assembler* ce) {
218   __ bind(_entry);
219   if (_compute_lock) {
220     // lock_reg was destroyed by fast unlocking attempt => recompute it
221     ce->monitor_address(_monitor_ix, _lock_reg);
222   }
223   ce->store_parameter(_lock_reg->as_register(), 0);
224   // note: non-blocking leaf routine => no call info needed
225   C1StubId exit_id;
226   if (ce->compilation()->has_fpu_code()) {
227     exit_id = C1StubId::monitorexit_id;
228   } else {
229     exit_id = C1StubId::monitorexit_nofpu_id;
230   }
231   __ adr(lr, _continuation);
232   __ far_jump(RuntimeAddress(Runtime1::entry_for(exit_id)));
233 }
234 
235 
236 // Implementation of patching:
237 // - Copy the code at given offset to an inlined buffer (first the bytes, then the number of bytes)
238 // - Replace original code with a call to the stub
239 // At Runtime:
240 // - call to stub, jump to runtime
241 // - in runtime: preserve all registers (rspecially objects, i.e., source and destination object)
242 // - in runtime: after initializing class, restore original code, reexecute instruction
243 
244 int PatchingStub::_patch_info_offset = -NativeGeneralJump::instruction_size;
245 
246 void PatchingStub::align_patch_site(MacroAssembler* masm) {
247 }
248 
249 void PatchingStub::emit_code(LIR_Assembler* ce) {
250   assert(false, "AArch64 should not use C1 runtime patching");
251 }
252 
253 
254 void DeoptimizeStub::emit_code(LIR_Assembler* ce) {
255   __ bind(_entry);
256   ce->store_parameter(_trap_request, 0);
257   __ far_call(RuntimeAddress(Runtime1::entry_for(C1StubId::deoptimize_id)));
258   ce->add_call_info_here(_info);
259   DEBUG_ONLY(__ should_not_reach_here());
260 }
261 
262 
263 void ImplicitNullCheckStub::emit_code(LIR_Assembler* ce) {
264   address a;
265   if (_info->deoptimize_on_exception()) {
266     // Deoptimize, do not throw the exception, because it is probably wrong to do it here.
267     a = Runtime1::entry_for(C1StubId::predicate_failed_trap_id);
268   } else {
269     a = Runtime1::entry_for(C1StubId::throw_null_pointer_exception_id);
270   }
271 
272   ce->compilation()->implicit_exception_table()->append(_offset, __ offset());
273   __ bind(_entry);
274   __ far_call(RuntimeAddress(a));
275   ce->add_call_info_here(_info);
276   ce->verify_oop_map(_info);
277   debug_only(__ should_not_reach_here());
278 }
279 
280 
281 void SimpleExceptionStub::emit_code(LIR_Assembler* ce) {
282   assert(__ rsp_offset() == 0, "frame size should be fixed");
283 
284   __ bind(_entry);
285   // pass the object in a scratch register because all other registers
286   // must be preserved
287   if (_obj->is_cpu_register()) {
288     __ mov(rscratch1, _obj->as_register());
289   }
290   __ far_call(RuntimeAddress(Runtime1::entry_for(_stub)), rscratch2);
291   ce->add_call_info_here(_info);
292   debug_only(__ should_not_reach_here());
293 }
294 
295 
296 void ArrayCopyStub::emit_code(LIR_Assembler* ce) {
297   //---------------slow case: call to native-----------------
298   __ bind(_entry);
299   // Figure out where the args should go
300   // This should really convert the IntrinsicID to the Method* and signature
301   // but I don't know how to do that.
302   //
303   VMRegPair args[5];
304   BasicType signature[5] = { T_OBJECT, T_INT, T_OBJECT, T_INT, T_INT};
305   SharedRuntime::java_calling_convention(signature, args, 5);
306 
307   // push parameters
308   // (src, src_pos, dest, destPos, length)
309   Register r[5];
310   r[0] = src()->as_register();
311   r[1] = src_pos()->as_register();
312   r[2] = dst()->as_register();
313   r[3] = dst_pos()->as_register();
314   r[4] = length()->as_register();
315 
316   // next registers will get stored on the stack
317   for (int i = 0; i < 5 ; i++ ) {
318     VMReg r_1 = args[i].first();
319     if (r_1->is_stack()) {
320       int st_off = r_1->reg2stack() * wordSize;
321       __ str (r[i], Address(sp, st_off));
322     } else {
323       assert(r[i] == args[i].first()->as_Register(), "Wrong register for arg ");
324     }
325   }
326 
327   ce->align_call(lir_static_call);
328 
329   ce->emit_static_call_stub();
330   if (ce->compilation()->bailed_out()) {
331     return; // CodeCache is full
332   }
333   Address resolve(SharedRuntime::get_resolve_static_call_stub(),
334                   relocInfo::static_call_type);
335   address call = __ trampoline_call(resolve);
336   if (call == nullptr) {
337     ce->bailout("trampoline stub overflow");
338     return;
339   }
340   ce->add_call_info_here(info());
341 
342 #ifndef PRODUCT
343   if (PrintC1Statistics) {
344     __ lea(rscratch2, ExternalAddress((address)&Runtime1::_arraycopy_slowcase_cnt));
345     __ incrementw(Address(rscratch2));
346   }
347 #endif
348 
349   __ b(_continuation);
350 }
351 
352 #undef __