1 /*
  2  * Copyright (c) 1999, 2026, 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(StubId::c1_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(StubId::c1_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   StubId stub_id;
 82   if (_throw_index_out_of_bounds_exception) {
 83     stub_id = StubId::c1_throw_index_exception_id;
 84   } else {
 85     assert(_array != LIR_Opr::nullOpr(), "sanity");
 86     __ mov(rscratch2, _array->as_pointer_register());
 87     stub_id = StubId::c1_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(StubId::c1_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(StubId::c1_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 // Implementation of LoadFlattenedArrayStub
123 
124 LoadFlattenedArrayStub::LoadFlattenedArrayStub(LIR_Opr array, LIR_Opr index, LIR_Opr result, CodeEmitInfo* info) {
125   _array = array;
126   _index = index;
127   _result = result;
128   _scratch_reg = FrameMap::r0_oop_opr;
129   _info = new CodeEmitInfo(info);
130 }
131 
132 void LoadFlattenedArrayStub::emit_code(LIR_Assembler* ce) {
133   assert(__ rsp_offset() == 0, "frame size should be fixed");
134   __ bind(_entry);
135   ce->store_parameter(_array->as_register(), 1);
136   ce->store_parameter(_index->as_register(), 0);
137   __ far_call(RuntimeAddress(Runtime1::entry_for(StubId::c1_load_flat_array_id)));
138   ce->add_call_info_here(_info);
139   ce->verify_oop_map(_info);
140   if (_result->as_register() != r0) {
141     __ mov(_result->as_register(), r0);
142   }
143   __ b(_continuation);
144 }
145 
146 
147 // Implementation of StoreFlattenedArrayStub
148 
149 StoreFlattenedArrayStub::StoreFlattenedArrayStub(LIR_Opr array, LIR_Opr index, LIR_Opr value, CodeEmitInfo* info) {
150   _array = array;
151   _index = index;
152   _value = value;
153   _scratch_reg = FrameMap::r0_oop_opr;
154   _info = new CodeEmitInfo(info);
155 }
156 
157 
158 void StoreFlattenedArrayStub::emit_code(LIR_Assembler* ce) {
159   assert(__ rsp_offset() == 0, "frame size should be fixed");
160   __ bind(_entry);
161   ce->store_parameter(_array->as_register(), 2);
162   ce->store_parameter(_index->as_register(), 1);
163   ce->store_parameter(_value->as_register(), 0);
164   __ far_call(RuntimeAddress(Runtime1::entry_for(StubId::c1_store_flat_array_id)));
165   ce->add_call_info_here(_info);
166   ce->verify_oop_map(_info);
167   __ b(_continuation);
168 }
169 
170 // Implementation of SubstitutabilityCheckStub
171 SubstitutabilityCheckStub::SubstitutabilityCheckStub(LIR_Opr left, LIR_Opr right, CodeEmitInfo* info) {
172   _left = left;
173   _right = right;
174   _scratch_reg = FrameMap::r0_oop_opr;
175   _info = new CodeEmitInfo(info);
176 }
177 
178 void SubstitutabilityCheckStub::emit_code(LIR_Assembler* ce) {
179   assert(__ rsp_offset() == 0, "frame size should be fixed");
180   __ bind(_entry);
181   ce->store_parameter(_left->as_register(), 1);
182   ce->store_parameter(_right->as_register(), 0);
183   __ far_call(RuntimeAddress(Runtime1::entry_for(StubId::c1_substitutability_check_id)));
184   ce->add_call_info_here(_info);
185   ce->verify_oop_map(_info);
186   __ b(_continuation);
187 }
188 
189 
190 // Implementation of NewInstanceStub
191 
192 NewInstanceStub::NewInstanceStub(LIR_Opr klass_reg, LIR_Opr result, ciInstanceKlass* klass, CodeEmitInfo* info, StubId stub_id) {
193   _result = result;
194   _klass = klass;
195   _klass_reg = klass_reg;
196   _info = new CodeEmitInfo(info);
197   assert(stub_id == StubId::c1_new_instance_id                 ||
198          stub_id == StubId::c1_fast_new_instance_id            ||
199          stub_id == StubId::c1_fast_new_instance_init_check_id,
200          "need new_instance id");
201   _stub_id   = stub_id;
202 }
203 
204 
205 
206 void NewInstanceStub::emit_code(LIR_Assembler* ce) {
207   assert(__ rsp_offset() == 0, "frame size should be fixed");
208   __ bind(_entry);
209   __ mov(r3, _klass_reg->as_register());
210   __ far_call(RuntimeAddress(Runtime1::entry_for(_stub_id)));
211   ce->add_call_info_here(_info);
212   ce->verify_oop_map(_info);
213   assert(_result->as_register() == r0, "result must in r0,");
214   __ b(_continuation);
215 }
216 
217 
218 // Implementation of NewTypeArrayStub
219 
220 // Implementation of NewTypeArrayStub
221 
222 NewTypeArrayStub::NewTypeArrayStub(LIR_Opr klass_reg, LIR_Opr length, LIR_Opr result, CodeEmitInfo* info) {
223   _klass_reg = klass_reg;
224   _length = length;
225   _result = result;
226   _info = new CodeEmitInfo(info);
227 }
228 
229 
230 void NewTypeArrayStub::emit_code(LIR_Assembler* ce) {
231   assert(__ rsp_offset() == 0, "frame size should be fixed");
232   __ bind(_entry);
233   assert(_length->as_register() == r19, "length must in r19,");
234   assert(_klass_reg->as_register() == r3, "klass_reg must in r3");
235   __ far_call(RuntimeAddress(Runtime1::entry_for(StubId::c1_new_type_array_id)));
236   ce->add_call_info_here(_info);
237   ce->verify_oop_map(_info);
238   assert(_result->as_register() == r0, "result must in r0");
239   __ b(_continuation);
240 }
241 
242 
243 // Implementation of NewObjectArrayStub
244 
245 NewObjectArrayStub::NewObjectArrayStub(LIR_Opr klass_reg, LIR_Opr length, LIR_Opr result,
246                                        CodeEmitInfo* info, bool is_null_free) {
247   _klass_reg = klass_reg;
248   _result = result;
249   _length = length;
250   _info = new CodeEmitInfo(info);
251   _is_null_free = is_null_free;
252 }
253 
254 
255 void NewObjectArrayStub::emit_code(LIR_Assembler* ce) {
256   assert(__ rsp_offset() == 0, "frame size should be fixed");
257   __ bind(_entry);
258   assert(_length->as_register() == r19, "length must in r19,");
259   assert(_klass_reg->as_register() == r3, "klass_reg must in r3");
260 
261   if (_is_null_free) {
262     __ far_call(RuntimeAddress(Runtime1::entry_for(StubId::c1_new_null_free_array_id)));
263   } else {
264     __ far_call(RuntimeAddress(Runtime1::entry_for(StubId::c1_new_object_array_id)));
265   }
266 
267   ce->add_call_info_here(_info);
268   ce->verify_oop_map(_info);
269   assert(_result->as_register() == r0, "result must in r0");
270   __ b(_continuation);
271 }
272 
273 void MonitorEnterStub::emit_code(LIR_Assembler* ce) {
274   assert(__ rsp_offset() == 0, "frame size should be fixed");
275   __ bind(_entry);
276   if (_throw_ie_stub != nullptr) {
277     // When we come here, _obj_reg has already been checked to be non-null.
278     __ ldr(rscratch1, Address(_obj_reg->as_register(), oopDesc::mark_offset_in_bytes()));
279     __ mov(rscratch2, markWord::inline_type_pattern);
280     __ andr(rscratch1, rscratch1, rscratch2);
281 
282     __ cmp(rscratch1, rscratch2);
283     __ br(Assembler::EQ, *_throw_ie_stub->entry());
284   }
285 
286   ce->store_parameter(_obj_reg->as_register(),  1);
287   ce->store_parameter(_lock_reg->as_register(), 0);
288   StubId enter_id;
289   if (ce->compilation()->has_fpu_code()) {
290     enter_id = StubId::c1_monitorenter_id;
291   } else {
292     enter_id = StubId::c1_monitorenter_nofpu_id;
293   }
294   __ far_call(RuntimeAddress(Runtime1::entry_for(enter_id)));
295   ce->add_call_info_here(_info);
296   ce->verify_oop_map(_info);
297   __ b(_continuation);
298 }
299 
300 
301 void MonitorExitStub::emit_code(LIR_Assembler* ce) {
302   __ bind(_entry);
303 
304   // lock_reg was destroyed by fast unlocking attempt => recompute it
305   ce->monitor_address(_monitor_ix, _lock_reg);
306 
307   ce->store_parameter(_lock_reg->as_register(), 0);
308   // note: non-blocking leaf routine => no call info needed
309   StubId exit_id;
310   if (ce->compilation()->has_fpu_code()) {
311     exit_id = StubId::c1_monitorexit_id;
312   } else {
313     exit_id = StubId::c1_monitorexit_nofpu_id;
314   }
315   __ adr(lr, _continuation);
316   __ far_jump(RuntimeAddress(Runtime1::entry_for(exit_id)));
317 }
318 
319 
320 // Implementation of patching:
321 // - Copy the code at given offset to an inlined buffer (first the bytes, then the number of bytes)
322 // - Replace original code with a call to the stub
323 // At Runtime:
324 // - call to stub, jump to runtime
325 // - in runtime: preserve all registers (rspecially objects, i.e., source and destination object)
326 // - in runtime: after initializing class, restore original code, reexecute instruction
327 
328 int PatchingStub::_patch_info_offset = -NativeGeneralJump::instruction_size;
329 
330 void PatchingStub::align_patch_site(MacroAssembler* masm) {
331 }
332 
333 void PatchingStub::emit_code(LIR_Assembler* ce) {
334   assert(false, "AArch64 should not use C1 runtime patching");
335 }
336 
337 
338 void DeoptimizeStub::emit_code(LIR_Assembler* ce) {
339   __ bind(_entry);
340   ce->store_parameter(_trap_request, 0);
341   __ far_call(RuntimeAddress(Runtime1::entry_for(StubId::c1_deoptimize_id)));
342   ce->add_call_info_here(_info);
343   DEBUG_ONLY(__ should_not_reach_here());
344 }
345 
346 
347 void ImplicitNullCheckStub::emit_code(LIR_Assembler* ce) {
348   address a;
349   if (_info->deoptimize_on_exception()) {
350     // Deoptimize, do not throw the exception, because it is probably wrong to do it here.
351     a = Runtime1::entry_for(StubId::c1_predicate_failed_trap_id);
352   } else {
353     a = Runtime1::entry_for(StubId::c1_throw_null_pointer_exception_id);
354   }
355 
356   ce->compilation()->implicit_exception_table()->append(_offset, __ offset());
357   __ bind(_entry);
358   __ far_call(RuntimeAddress(a));
359   ce->add_call_info_here(_info);
360   ce->verify_oop_map(_info);
361   DEBUG_ONLY(__ should_not_reach_here());
362 }
363 
364 
365 void SimpleExceptionStub::emit_code(LIR_Assembler* ce) {
366   assert(__ rsp_offset() == 0, "frame size should be fixed");
367 
368   __ bind(_entry);
369   // pass the object in a scratch register because all other registers
370   // must be preserved
371   if (_obj->is_cpu_register()) {
372     __ mov(rscratch1, _obj->as_register());
373   }
374   __ far_call(RuntimeAddress(Runtime1::entry_for(_stub)), rscratch2);
375   ce->add_call_info_here(_info);
376   DEBUG_ONLY(__ should_not_reach_here());
377 }
378 
379 
380 void ArrayCopyStub::emit_code(LIR_Assembler* ce) {
381   //---------------slow case: call to native-----------------
382   __ bind(_entry);
383   // Figure out where the args should go
384   // This should really convert the IntrinsicID to the Method* and signature
385   // but I don't know how to do that.
386   //
387   VMRegPair args[5];
388   BasicType signature[5] = { T_OBJECT, T_INT, T_OBJECT, T_INT, T_INT};
389   SharedRuntime::java_calling_convention(signature, args, 5);
390 
391   // push parameters
392   // (src, src_pos, dest, destPos, length)
393   Register r[5];
394   r[0] = src()->as_register();
395   r[1] = src_pos()->as_register();
396   r[2] = dst()->as_register();
397   r[3] = dst_pos()->as_register();
398   r[4] = length()->as_register();
399 
400   // next registers will get stored on the stack
401   for (int i = 0; i < 5 ; i++ ) {
402     VMReg r_1 = args[i].first();
403     if (r_1->is_stack()) {
404       int st_off = r_1->reg2stack() * wordSize;
405       __ str (r[i], Address(sp, st_off));
406     } else {
407       assert(r[i] == args[i].first()->as_Register(), "Wrong register for arg ");
408     }
409   }
410 
411   ce->align_call(lir_static_call);
412 
413   ce->emit_static_call_stub();
414   if (ce->compilation()->bailed_out()) {
415     return; // CodeCache is full
416   }
417   Address resolve(SharedRuntime::get_resolve_static_call_stub(),
418                   relocInfo::static_call_type);
419   address call = __ trampoline_call(resolve);
420   if (call == nullptr) {
421     ce->bailout("trampoline stub overflow");
422     return;
423   }
424   ce->add_call_info_here(info());
425 
426 #ifndef PRODUCT
427   if (PrintC1Statistics) {
428     __ lea(rscratch2, ExternalAddress((address)&Runtime1::_arraycopy_slowcase_cnt));
429     __ incrementw(Address(rscratch2));
430   }
431 #endif
432 
433   __ b(_continuation);
434 }
435 
436 #undef __