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 * Copyright (c) 2020, 2023, Huawei Technologies Co., Ltd. All rights reserved.
5 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
6 *
7 * This code is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU General Public License version 2 only, as
9 * published by the Free Software Foundation.
10 *
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
23 * questions.
24 *
25 */
26
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_riscv.hpp"
35 #include "runtime/sharedRuntime.hpp"
36 #include "vmreg_riscv.inline.hpp"
37
38
39 #define __ ce->masm()->
40
41 void C1SafepointPollStub::emit_code(LIR_Assembler* ce) {
42 __ bind(_entry);
43 InternalAddress safepoint_pc(__ pc() - __ offset() + safepoint_offset());
44 __ la(t0, safepoint_pc);
45 __ sd(t0, Address(xthread, JavaThread::saved_exception_pc_offset()));
46
47 assert(SharedRuntime::polling_page_return_handler_blob() != nullptr,
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(t0, m);
58 ce->store_parameter(t0, 1);
59 ce->store_parameter(_bci, 0);
60 __ far_call(RuntimeAddress(Runtime1::entry_for(StubId::c1_counter_overflow_id)));
61 ce->add_call_info_here(_info);
62 ce->verify_oop_map(_info);
63 __ j(_continuation);
64 }
65
66 void RangeCheckStub::emit_code(LIR_Assembler* ce) {
67 __ bind(_entry);
68 if (_info->deoptimize_on_exception()) {
69 address a = Runtime1::entry_for(StubId::c1_predicate_failed_trap_id);
70 __ far_call(RuntimeAddress(a));
71 ce->add_call_info_here(_info);
72 ce->verify_oop_map(_info);
73 DEBUG_ONLY(__ should_not_reach_here());
74 return;
75 }
76
77 if (_index->is_cpu_register()) {
78 __ mv(t0, _index->as_register());
79 } else {
80 __ mv(t0, _index->as_jint());
81 }
82 StubId stub_id;
83 if (_throw_index_out_of_bounds_exception) {
84 stub_id = StubId::c1_throw_index_exception_id;
85 } else {
86 assert(_array != LIR_Opr::nullOpr(), "sanity");
87 __ mv(t1, _array->as_pointer_register());
88 stub_id = StubId::c1_throw_range_check_failed_id;
89 }
90 // t0 and t1 are used as args in generate_exception_throw,
91 // so use x1/ra as the tmp register for rt_call.
92 __ rt_call(Runtime1::entry_for(stub_id), ra);
93 ce->add_call_info_here(_info);
94 ce->verify_oop_map(_info);
95 DEBUG_ONLY(__ should_not_reach_here());
96 }
97
98 PredicateFailedStub::PredicateFailedStub(CodeEmitInfo* info) {
99 _info = new CodeEmitInfo(info);
100 }
101
102 void PredicateFailedStub::emit_code(LIR_Assembler* ce) {
103 __ bind(_entry);
104 address a = Runtime1::entry_for(StubId::c1_predicate_failed_trap_id);
105 __ far_call(RuntimeAddress(a));
106 ce->add_call_info_here(_info);
107 ce->verify_oop_map(_info);
108 DEBUG_ONLY(__ should_not_reach_here());
109 }
110
111 void DivByZeroStub::emit_code(LIR_Assembler* ce) {
112 if (_offset != -1) {
113 ce->compilation()->implicit_exception_table()->append(_offset, __ offset());
114 }
115 __ bind(_entry);
116 __ far_call(RuntimeAddress(Runtime1::entry_for(StubId::c1_throw_div0_exception_id)));
117 ce->add_call_info_here(_info);
118 ce->verify_oop_map(_info);
119 #ifdef ASSERT
120 __ should_not_reach_here();
121 #endif
122 }
123
124 // Implementation of NewInstanceStub
125 NewInstanceStub::NewInstanceStub(LIR_Opr klass_reg, LIR_Opr result, ciInstanceKlass* klass, CodeEmitInfo* info, StubId stub_id) {
126 _result = result;
127 _klass = klass;
128 _klass_reg = klass_reg;
129 _info = new CodeEmitInfo(info);
130 assert(stub_id == StubId::c1_new_instance_id ||
131 stub_id == StubId::c1_fast_new_instance_id ||
132 stub_id == StubId::c1_fast_new_instance_init_check_id,
133 "need new_instance id");
134 _stub_id = stub_id;
135 }
136
137 void NewInstanceStub::emit_code(LIR_Assembler* ce) {
138 assert(__ rsp_offset() == 0, "frame size should be fixed");
139 __ bind(_entry);
140 __ mv(x13, _klass_reg->as_register());
141 __ far_call(RuntimeAddress(Runtime1::entry_for(_stub_id)));
142 ce->add_call_info_here(_info);
143 ce->verify_oop_map(_info);
144 assert(_result->as_register() == x10, "result must in x10");
145 __ j(_continuation);
146 }
147
148 // Implementation of NewTypeArrayStub
149 NewTypeArrayStub::NewTypeArrayStub(LIR_Opr klass_reg, LIR_Opr length, LIR_Opr result, CodeEmitInfo* info) {
150 _klass_reg = klass_reg;
151 _length = length;
152 _result = result;
153 _info = new CodeEmitInfo(info);
154 }
155
156 void NewTypeArrayStub::emit_code(LIR_Assembler* ce) {
157 assert(__ rsp_offset() == 0, "frame size should be fixed");
158 __ bind(_entry);
159 assert(_length->as_register() == x9, "length must in x9");
160 assert(_klass_reg->as_register() == x13, "klass_reg must in x13");
161 __ far_call(RuntimeAddress(Runtime1::entry_for(StubId::c1_new_type_array_id)));
162 ce->add_call_info_here(_info);
163 ce->verify_oop_map(_info);
164 assert(_result->as_register() == x10, "result must in x10");
165 __ j(_continuation);
166 }
167
168 // Implementation of NewObjectArrayStub
169 NewObjectArrayStub::NewObjectArrayStub(LIR_Opr klass_reg, LIR_Opr length, LIR_Opr result,
170 CodeEmitInfo* info, bool is_null_free) {
171 _klass_reg = klass_reg;
172 _result = result;
173 _length = length;
174 _info = new CodeEmitInfo(info);
175 _is_null_free = is_null_free; // unimplemented
176 }
177
178 void NewObjectArrayStub::emit_code(LIR_Assembler* ce) {
179 assert(__ rsp_offset() == 0, "frame size should be fixed");
180 __ bind(_entry);
181 assert(_length->as_register() == x9, "length must in x9");
182 assert(_klass_reg->as_register() == x13, "klass_reg must in x13");
183 __ far_call(RuntimeAddress(Runtime1::entry_for(StubId::c1_new_object_array_id)));
184 ce->add_call_info_here(_info);
185 ce->verify_oop_map(_info);
186 assert(_result->as_register() == x10, "result must in x10");
187 __ j(_continuation);
188 }
189
190 void MonitorEnterStub::emit_code(LIR_Assembler* ce) {
191 assert(__ rsp_offset() == 0, "frame size should be fixed");
192 __ bind(_entry);
193 ce->store_parameter(_obj_reg->as_register(), 1);
194 ce->store_parameter(_lock_reg->as_register(), 0);
195 StubId enter_id;
196 if (ce->compilation()->has_fpu_code()) {
197 enter_id = StubId::c1_monitorenter_id;
198 } else {
199 enter_id = StubId::c1_monitorenter_nofpu_id;
200 }
201 __ far_call(RuntimeAddress(Runtime1::entry_for(enter_id)));
202 ce->add_call_info_here(_info);
203 ce->verify_oop_map(_info);
204 __ j(_continuation);
205 }
206
207 void MonitorExitStub::emit_code(LIR_Assembler* ce) {
208 __ bind(_entry);
209
210 // lock_reg was destroyed by fast unlocking attempt => recompute it
211 ce->monitor_address(_monitor_ix, _lock_reg);
212
213 ce->store_parameter(_lock_reg->as_register(), 0);
214 // note: non-blocking leaf routine => no call info needed
215 StubId exit_id;
216 if (ce->compilation()->has_fpu_code()) {
217 exit_id = StubId::c1_monitorexit_id;
218 } else {
219 exit_id = StubId::c1_monitorexit_nofpu_id;
220 }
221 __ la(ra, _continuation);
222 __ far_jump(RuntimeAddress(Runtime1::entry_for(exit_id)));
223 }
224
225 // Implementation of patching:
226 // - Copy the code at given offset to an inlined buffer (first the bytes, then the number of bytes)
227 // - Replace original code with a call to the stub
228 // At Runtime:
229 // - call to stub, jump to runtime
230 // - in runtime: preserve all registers (rspecially objects, i.e., source and destination object)
231 // - in runtime: after initializing class, restore original code, reexecute instruction
232
233 int PatchingStub::_patch_info_offset = -NativeGeneralJump::instruction_size;
234
235 void PatchingStub::align_patch_site(MacroAssembler* masm) {}
236
237 void PatchingStub::emit_code(LIR_Assembler* ce) {
238 assert(false, "RISCV should not use C1 runtime patching");
239 }
240
241 void DeoptimizeStub::emit_code(LIR_Assembler* ce) {
242 __ bind(_entry);
243 ce->store_parameter(_trap_request, 0);
244 __ far_call(RuntimeAddress(Runtime1::entry_for(StubId::c1_deoptimize_id)));
245 ce->add_call_info_here(_info);
246 DEBUG_ONLY(__ should_not_reach_here());
247 }
248
249 void ImplicitNullCheckStub::emit_code(LIR_Assembler* ce) {
250 address a = nullptr;
251 if (_info->deoptimize_on_exception()) {
252 // Deoptimize, do not throw the exception, because it is probably wrong to do it here.
253 a = Runtime1::entry_for(StubId::c1_predicate_failed_trap_id);
254 } else {
255 a = Runtime1::entry_for(StubId::c1_throw_null_pointer_exception_id);
256 }
257
258 ce->compilation()->implicit_exception_table()->append(_offset, __ offset());
259 __ bind(_entry);
260 __ far_call(RuntimeAddress(a));
261 ce->add_call_info_here(_info);
262 ce->verify_oop_map(_info);
263 DEBUG_ONLY(__ should_not_reach_here());
264 }
265
266 void SimpleExceptionStub::emit_code(LIR_Assembler* ce) {
267 assert(__ rsp_offset() == 0, "frame size should be fixed");
268
269 __ bind(_entry);
270 // pass the object in a tmp register because all other registers
271 // must be preserved
272 if (_obj->is_cpu_register()) {
273 __ mv(t0, _obj->as_register());
274 }
275 __ far_call(RuntimeAddress(Runtime1::entry_for(_stub)));
276 ce->add_call_info_here(_info);
277 DEBUG_ONLY(__ should_not_reach_here());
278 }
279
280 void ArrayCopyStub::emit_code(LIR_Assembler* ce) {
281 // ---------------slow case: call to native-----------------
282 __ bind(_entry);
283 // Figure out where the args should go
284 // This should really convert the IntrinsicID to the Method* and signature
285 // but I don't know how to do that.
286 const int args_num = 5;
287 VMRegPair args[args_num];
288 BasicType signature[args_num] = { T_OBJECT, T_INT, T_OBJECT, T_INT, T_INT };
289 SharedRuntime::java_calling_convention(signature, args, args_num);
290
291 // push parameters
292 Register r[args_num];
293 r[0] = src()->as_register();
294 r[1] = src_pos()->as_register();
295 r[2] = dst()->as_register();
296 r[3] = dst_pos()->as_register();
297 r[4] = length()->as_register();
298
299 // next registers will get stored on the stack
300 for (int j = 0; j < args_num; j++) {
301 VMReg r_1 = args[j].first();
302 if (r_1->is_stack()) {
303 int st_off = r_1->reg2stack() * wordSize;
304 __ sd(r[j], Address(sp, st_off));
305 } else {
306 assert(r[j] == args[j].first()->as_Register(), "Wrong register for arg");
307 }
308 }
309
310 ce->align_call(lir_static_call);
311
312 ce->emit_static_call_stub();
313 if (ce->compilation()->bailed_out()) {
314 return; // CodeCache is full
315 }
316 Address resolve(SharedRuntime::get_resolve_static_call_stub(),
317 relocInfo::static_call_type);
318 address call = __ reloc_call(resolve);
319 if (call == nullptr) {
320 ce->bailout("reloc call address stub overflow");
321 return;
322 }
323 ce->add_call_info_here(info());
324
325 #ifndef PRODUCT
326 if (PrintC1Statistics) {
327 __ la(t1, ExternalAddress((address)&Runtime1::_arraycopy_slowcase_cnt));
328 __ incrementw(Address(t1));
329 }
330 #endif
331
332 __ j(_continuation);
333 }
334
335 // Implementation of SubstitutabilityCheckStub
336 SubstitutabilityCheckStub::SubstitutabilityCheckStub(LIR_Opr left, LIR_Opr right, CodeEmitInfo* info) {
337 Unimplemented();
338 }
339
340 void SubstitutabilityCheckStub::emit_code(LIR_Assembler* ce) {
341 Unimplemented();
342 }
343
344 LoadFlattenedArrayStub::LoadFlattenedArrayStub(LIR_Opr array, LIR_Opr index, LIR_Opr result, CodeEmitInfo* info) {
345 Unimplemented();
346 }
347
348 void LoadFlattenedArrayStub::emit_code(LIR_Assembler* ce) {
349 Unimplemented();
350 }
351
352 // Implementation of StoreFlattenedArrayStub
353
354 StoreFlattenedArrayStub::StoreFlattenedArrayStub(LIR_Opr array, LIR_Opr index, LIR_Opr value, CodeEmitInfo* info) {
355 Unimplemented();
356 }
357
358 void StoreFlattenedArrayStub::emit_code(LIR_Assembler* ce) {
359 Unimplemented();
360 }
361 #undef __