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 * 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, CodeEmitInfo* info) {
170 _klass_reg = klass_reg;
171 _result = result;
172 _length = length;
173 _info = new CodeEmitInfo(info);
174 }
175
176 void NewObjectArrayStub::emit_code(LIR_Assembler* ce) {
177 assert(__ rsp_offset() == 0, "frame size should be fixed");
178 __ bind(_entry);
179 assert(_length->as_register() == x9, "length must in x9");
180 assert(_klass_reg->as_register() == x13, "klass_reg must in x13");
181 __ far_call(RuntimeAddress(Runtime1::entry_for(StubId::c1_new_object_array_id)));
182 ce->add_call_info_here(_info);
183 ce->verify_oop_map(_info);
184 assert(_result->as_register() == x10, "result must in x10");
185 __ j(_continuation);
186 }
187
188 void MonitorEnterStub::emit_code(LIR_Assembler* ce) {
189 assert(__ rsp_offset() == 0, "frame size should be fixed");
190 __ bind(_entry);
191 ce->store_parameter(_obj_reg->as_register(), 1);
192 ce->store_parameter(_lock_reg->as_register(), 0);
193 StubId enter_id;
194 if (ce->compilation()->has_fpu_code()) {
195 enter_id = StubId::c1_monitorenter_id;
196 } else {
197 enter_id = StubId::c1_monitorenter_nofpu_id;
198 }
199 __ far_call(RuntimeAddress(Runtime1::entry_for(enter_id)));
200 ce->add_call_info_here(_info);
201 ce->verify_oop_map(_info);
202 __ j(_continuation);
203 }
204
205 void MonitorExitStub::emit_code(LIR_Assembler* ce) {
206 __ bind(_entry);
207
208 // lock_reg was destroyed by fast unlocking attempt => recompute it
209 ce->monitor_address(_monitor_ix, _lock_reg);
210
211 ce->store_parameter(_lock_reg->as_register(), 0);
212 // note: non-blocking leaf routine => no call info needed
213 StubId exit_id;
214 if (ce->compilation()->has_fpu_code()) {
215 exit_id = StubId::c1_monitorexit_id;
216 } else {
217 exit_id = StubId::c1_monitorexit_nofpu_id;
218 }
219 __ la(ra, _continuation);
220 __ far_jump(RuntimeAddress(Runtime1::entry_for(exit_id)));
221 }
222
223 // Implementation of patching:
224 // - Copy the code at given offset to an inlined buffer (first the bytes, then the number of bytes)
225 // - Replace original code with a call to the stub
226 // At Runtime:
227 // - call to stub, jump to runtime
228 // - in runtime: preserve all registers (rspecially objects, i.e., source and destination object)
229 // - in runtime: after initializing class, restore original code, reexecute instruction
230
231 int PatchingStub::_patch_info_offset = -NativeGeneralJump::instruction_size;
232
233 void PatchingStub::align_patch_site(MacroAssembler* masm) {}
234
235 void PatchingStub::emit_code(LIR_Assembler* ce) {
236 assert(false, "RISCV should not use C1 runtime patching");
237 }
238
239 void DeoptimizeStub::emit_code(LIR_Assembler* ce) {
240 __ bind(_entry);
241 ce->store_parameter(_trap_request, 0);
242 __ far_call(RuntimeAddress(Runtime1::entry_for(StubId::c1_deoptimize_id)));
243 ce->add_call_info_here(_info);
244 DEBUG_ONLY(__ should_not_reach_here());
245 }
246
247 void ImplicitNullCheckStub::emit_code(LIR_Assembler* ce) {
248 address a = nullptr;
249 if (_info->deoptimize_on_exception()) {
250 // Deoptimize, do not throw the exception, because it is probably wrong to do it here.
251 a = Runtime1::entry_for(StubId::c1_predicate_failed_trap_id);
252 } else {
253 a = Runtime1::entry_for(StubId::c1_throw_null_pointer_exception_id);
254 }
255
256 ce->compilation()->implicit_exception_table()->append(_offset, __ offset());
257 __ bind(_entry);
258 __ far_call(RuntimeAddress(a));
259 ce->add_call_info_here(_info);
260 ce->verify_oop_map(_info);
261 DEBUG_ONLY(__ should_not_reach_here());
262 }
263
264 void SimpleExceptionStub::emit_code(LIR_Assembler* ce) {
265 assert(__ rsp_offset() == 0, "frame size should be fixed");
266
267 __ bind(_entry);
268 // pass the object in a tmp register because all other registers
269 // must be preserved
270 if (_obj->is_cpu_register()) {
271 __ mv(t0, _obj->as_register());
272 }
273 __ far_call(RuntimeAddress(Runtime1::entry_for(_stub)));
274 ce->add_call_info_here(_info);
275 DEBUG_ONLY(__ should_not_reach_here());
276 }
277
278 void ArrayCopyStub::emit_code(LIR_Assembler* ce) {
279 // ---------------slow case: call to native-----------------
280 __ bind(_entry);
281 // Figure out where the args should go
282 // This should really convert the IntrinsicID to the Method* and signature
283 // but I don't know how to do that.
284 const int args_num = 5;
285 VMRegPair args[args_num];
286 BasicType signature[args_num] = { T_OBJECT, T_INT, T_OBJECT, T_INT, T_INT };
287 SharedRuntime::java_calling_convention(signature, args, args_num);
288
289 // push parameters
290 Register r[args_num];
291 r[0] = src()->as_register();
292 r[1] = src_pos()->as_register();
293 r[2] = dst()->as_register();
294 r[3] = dst_pos()->as_register();
295 r[4] = length()->as_register();
296
297 // next registers will get stored on the stack
298 for (int j = 0; j < args_num; j++) {
299 VMReg r_1 = args[j].first();
300 if (r_1->is_stack()) {
301 int st_off = r_1->reg2stack() * wordSize;
302 __ sd(r[j], Address(sp, st_off));
303 } else {
304 assert(r[j] == args[j].first()->as_Register(), "Wrong register for arg");
305 }
306 }
307
308 ce->align_call(lir_static_call);
309
310 ce->emit_static_call_stub();
311 if (ce->compilation()->bailed_out()) {
312 return; // CodeCache is full
313 }
314 Address resolve(SharedRuntime::get_resolve_static_call_stub(),
315 relocInfo::static_call_type);
316 address call = __ reloc_call(resolve);
317 if (call == nullptr) {
318 ce->bailout("reloc call address stub overflow");
319 return;
320 }
321 ce->add_call_info_here(info());
322
323 #ifndef PRODUCT
324 if (PrintC1Statistics) {
325 __ la(t1, ExternalAddress((address)&Runtime1::_arraycopy_slowcase_cnt));
326 __ incrementw(Address(t1));
327 }
328 #endif
329
330 __ j(_continuation);
331 }
332
333 #undef __