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 // 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(C1StubId::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(C1StubId::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(C1StubId::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, C1StubId stub_id) {
193   _result = result;
194   _klass = klass;
195   _klass_reg = klass_reg;
196   _info = new CodeEmitInfo(info);
197   assert(stub_id == C1StubId::new_instance_id                 ||
198          stub_id == C1StubId::fast_new_instance_id            ||
199          stub_id == C1StubId::fast_new_instance_init_check_id,
200          "need new_instance id");
201   _stub_id   = stub_id;
202 }
203 
204 void NewInstanceStub::emit_code(LIR_Assembler* ce) {
205   assert(__ rsp_offset() == 0, "frame size should be fixed");
206   __ bind(_entry);
207   __ mov(r3, _klass_reg->as_register());
208   __ far_call(RuntimeAddress(Runtime1::entry_for(_stub_id)));
209   ce->add_call_info_here(_info);
210   ce->verify_oop_map(_info);
211   assert(_result->as_register() == r0, "result must in r0,");
212   __ b(_continuation);
213 }
214 
215 
216 // Implementation of NewTypeArrayStub
217 
218 // Implementation of NewTypeArrayStub
219 
220 NewTypeArrayStub::NewTypeArrayStub(LIR_Opr klass_reg, LIR_Opr length, LIR_Opr result, CodeEmitInfo* info) {
221   _klass_reg = klass_reg;
222   _length = length;
223   _result = result;
224   _info = new CodeEmitInfo(info);
225 }
226 
227 
228 void NewTypeArrayStub::emit_code(LIR_Assembler* ce) {
229   assert(__ rsp_offset() == 0, "frame size should be fixed");
230   __ bind(_entry);
231   assert(_length->as_register() == r19, "length must in r19,");
232   assert(_klass_reg->as_register() == r3, "klass_reg must in r3");
233   __ far_call(RuntimeAddress(Runtime1::entry_for(C1StubId::new_type_array_id)));
234   ce->add_call_info_here(_info);
235   ce->verify_oop_map(_info);
236   assert(_result->as_register() == r0, "result must in r0");
237   __ b(_continuation);
238 }
239 
240 
241 // Implementation of NewObjectArrayStub
242 
243 NewObjectArrayStub::NewObjectArrayStub(LIR_Opr klass_reg, LIR_Opr length, LIR_Opr result,
244                                        CodeEmitInfo* info, bool is_null_free) {
245   _klass_reg = klass_reg;
246   _result = result;
247   _length = length;
248   _info = new CodeEmitInfo(info);
249   _is_null_free = is_null_free;
250 }
251 
252 
253 void NewObjectArrayStub::emit_code(LIR_Assembler* ce) {
254   assert(__ rsp_offset() == 0, "frame size should be fixed");
255   __ bind(_entry);
256   assert(_length->as_register() == r19, "length must in r19,");
257   assert(_klass_reg->as_register() == r3, "klass_reg must in r3");
258 
259   if (_is_null_free) {
260     __ far_call(RuntimeAddress(Runtime1::entry_for(C1StubId::new_null_free_array_id)));
261   } else {
262     __ far_call(RuntimeAddress(Runtime1::entry_for(C1StubId::new_object_array_id)));
263   }
264 
265   ce->add_call_info_here(_info);
266   ce->verify_oop_map(_info);
267   assert(_result->as_register() == r0, "result must in r0");
268   __ b(_continuation);
269 }
270 
271 void MonitorEnterStub::emit_code(LIR_Assembler* ce) {
272   assert(__ rsp_offset() == 0, "frame size should be fixed");
273   __ bind(_entry);
274   if (_throw_ie_stub != nullptr) {
275     // When we come here, _obj_reg has already been checked to be non-null.
276     __ ldr(rscratch1, Address(_obj_reg->as_register(), oopDesc::mark_offset_in_bytes()));
277     __ mov(rscratch2, markWord::inline_type_pattern);
278     __ andr(rscratch1, rscratch1, rscratch2);
279 
280     __ cmp(rscratch1, rscratch2);
281     __ br(Assembler::EQ, *_throw_ie_stub->entry());
282   }
283 
284   ce->store_parameter(_obj_reg->as_register(),  1);
285   ce->store_parameter(_lock_reg->as_register(), 0);
286   C1StubId enter_id;
287   if (ce->compilation()->has_fpu_code()) {
288     enter_id = C1StubId::monitorenter_id;
289   } else {
290     enter_id = C1StubId::monitorenter_nofpu_id;
291   }
292   __ far_call(RuntimeAddress(Runtime1::entry_for(enter_id)));
293   ce->add_call_info_here(_info);
294   ce->verify_oop_map(_info);
295   __ b(_continuation);
296 }
297 
298 
299 void MonitorExitStub::emit_code(LIR_Assembler* ce) {
300   __ bind(_entry);
301   if (_compute_lock) {
302     // lock_reg was destroyed by fast unlocking attempt => recompute it
303     ce->monitor_address(_monitor_ix, _lock_reg);
304   }
305   ce->store_parameter(_lock_reg->as_register(), 0);
306   // note: non-blocking leaf routine => no call info needed
307   C1StubId exit_id;
308   if (ce->compilation()->has_fpu_code()) {
309     exit_id = C1StubId::monitorexit_id;
310   } else {
311     exit_id = C1StubId::monitorexit_nofpu_id;
312   }
313   __ adr(lr, _continuation);
314   __ far_jump(RuntimeAddress(Runtime1::entry_for(exit_id)));
315 }
316 
317 
318 // Implementation of patching:
319 // - Copy the code at given offset to an inlined buffer (first the bytes, then the number of bytes)
320 // - Replace original code with a call to the stub
321 // At Runtime:
322 // - call to stub, jump to runtime
323 // - in runtime: preserve all registers (rspecially objects, i.e., source and destination object)
324 // - in runtime: after initializing class, restore original code, reexecute instruction
325 
326 int PatchingStub::_patch_info_offset = -NativeGeneralJump::instruction_size;
327 
328 void PatchingStub::align_patch_site(MacroAssembler* masm) {
329 }
330 
331 void PatchingStub::emit_code(LIR_Assembler* ce) {
332   assert(false, "AArch64 should not use C1 runtime patching");
333 }
334 
335 
336 void DeoptimizeStub::emit_code(LIR_Assembler* ce) {
337   __ bind(_entry);
338   ce->store_parameter(_trap_request, 0);
339   __ far_call(RuntimeAddress(Runtime1::entry_for(C1StubId::deoptimize_id)));
340   ce->add_call_info_here(_info);
341   DEBUG_ONLY(__ should_not_reach_here());
342 }
343 
344 
345 void ImplicitNullCheckStub::emit_code(LIR_Assembler* ce) {
346   address a;
347   if (_info->deoptimize_on_exception()) {
348     // Deoptimize, do not throw the exception, because it is probably wrong to do it here.
349     a = Runtime1::entry_for(C1StubId::predicate_failed_trap_id);
350   } else {
351     a = Runtime1::entry_for(C1StubId::throw_null_pointer_exception_id);
352   }
353 
354   ce->compilation()->implicit_exception_table()->append(_offset, __ offset());
355   __ bind(_entry);
356   __ far_call(RuntimeAddress(a));
357   ce->add_call_info_here(_info);
358   ce->verify_oop_map(_info);
359   debug_only(__ should_not_reach_here());
360 }
361 
362 
363 void SimpleExceptionStub::emit_code(LIR_Assembler* ce) {
364   assert(__ rsp_offset() == 0, "frame size should be fixed");
365 
366   __ bind(_entry);
367   // pass the object in a scratch register because all other registers
368   // must be preserved
369   if (_obj->is_cpu_register()) {
370     __ mov(rscratch1, _obj->as_register());
371   }
372   __ far_call(RuntimeAddress(Runtime1::entry_for(_stub)), rscratch2);
373   ce->add_call_info_here(_info);
374   debug_only(__ should_not_reach_here());
375 }
376 
377 
378 void ArrayCopyStub::emit_code(LIR_Assembler* ce) {
379   //---------------slow case: call to native-----------------
380   __ bind(_entry);
381   // Figure out where the args should go
382   // This should really convert the IntrinsicID to the Method* and signature
383   // but I don't know how to do that.
384   //
385   VMRegPair args[5];
386   BasicType signature[5] = { T_OBJECT, T_INT, T_OBJECT, T_INT, T_INT};
387   SharedRuntime::java_calling_convention(signature, args, 5);
388 
389   // push parameters
390   // (src, src_pos, dest, destPos, length)
391   Register r[5];
392   r[0] = src()->as_register();
393   r[1] = src_pos()->as_register();
394   r[2] = dst()->as_register();
395   r[3] = dst_pos()->as_register();
396   r[4] = length()->as_register();
397 
398   // next registers will get stored on the stack
399   for (int i = 0; i < 5 ; i++ ) {
400     VMReg r_1 = args[i].first();
401     if (r_1->is_stack()) {
402       int st_off = r_1->reg2stack() * wordSize;
403       __ str (r[i], Address(sp, st_off));
404     } else {
405       assert(r[i] == args[i].first()->as_Register(), "Wrong register for arg ");
406     }
407   }
408 
409   ce->align_call(lir_static_call);
410 
411   ce->emit_static_call_stub();
412   if (ce->compilation()->bailed_out()) {
413     return; // CodeCache is full
414   }
415   Address resolve(SharedRuntime::get_resolve_static_call_stub(),
416                   relocInfo::static_call_type);
417   address call = __ trampoline_call(resolve);
418   if (call == nullptr) {
419     ce->bailout("trampoline stub overflow");
420     return;
421   }
422   ce->add_call_info_here(info());
423 
424 #ifndef PRODUCT
425   if (PrintC1Statistics) {
426     __ lea(rscratch2, ExternalAddress((address)&Runtime1::_arraycopy_slowcase_cnt));
427     __ incrementw(Address(rscratch2));
428   }
429 #endif
430 
431   __ b(_continuation);
432 }
433 
434 #undef __