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