1 /*
2 * Copyright (c) 2003, 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/assembler.inline.hpp"
28 #include "asm/macroAssembler.inline.hpp"
29 #include "code/compiledIC.hpp"
30 #include "code/vtableStubs.hpp"
31 #include "interp_masm_riscv.hpp"
32 #include "memory/resourceArea.hpp"
33 #include "oops/instanceKlass.hpp"
34 #include "oops/klassVtable.hpp"
35 #include "runtime/sharedRuntime.hpp"
36 #include "vmreg_riscv.inline.hpp"
37 #ifdef COMPILER2
38 #include "opto/runtime.hpp"
39 #endif
40
41 // machine-dependent part of VtableStubs: create VtableStub of correct size and
42 // initialize its code
43
44 #define __ masm->
45
46 #ifndef PRODUCT
47 extern "C" void bad_compiled_vtable_index(JavaThread* thread, oop receiver, int index);
48 #endif
49
50 VtableStub* VtableStubs::create_vtable_stub(int vtable_index, bool caller_is_c1) {
51 // Read "A word on VtableStub sizing" in share/code/vtableStubs.hpp for details on stub sizing.
52 const int stub_code_length = code_size_limit(true);
53 VtableStub* s = new(stub_code_length) VtableStub(true, vtable_index, caller_is_c1);
54 // Can be null if there is no free space in the code cache.
55 if (s == nullptr) {
56 return nullptr;
57 }
58
59 // Count unused bytes in instruction sequences of variable size.
60 // We add them to the computed buffer size in order to avoid
61 // overflow in subsequently generated stubs.
62 address start_pc = nullptr;
63 int slop_bytes = 0;
64 int slop_delta = 0;
65
66 ByteSize entry_offset = caller_is_c1
67 ? Method::from_compiled_inline_offset()
68 : Method::from_compiled_inline_ro_offset();
69
70 ResourceMark rm;
71 CodeBuffer cb(s->entry_point(), stub_code_length);
72 MacroAssembler* masm = new MacroAssembler(&cb);
73 assert_cond(masm != nullptr);
74
75 #if (!defined(PRODUCT) && defined(COMPILER2))
76 if (CountCompiledCalls) {
77 __ la(t2, ExternalAddress((address) SharedRuntime::nof_megamorphic_calls_addr()));
78 __ increment(Address(t2));
79 }
80 #endif
81
82 // get receiver (need to skip return address on top of stack)
83 assert(VtableStub::receiver_location() == j_rarg0->as_VMReg(), "receiver expected in j_rarg0");
84
85 // get receiver klass
86 address npe_addr = __ pc();
87 __ load_klass(t2, j_rarg0);
88
89 #ifndef PRODUCT
90 if (DebugVtables) {
91 Label L;
92 start_pc = __ pc();
93
94 // check offset vs vtable length
95 __ lwu(t0, Address(t2, Klass::vtable_length_offset()));
96 __ mv(t1, vtable_index * vtableEntry::size());
97 __ bgt(t0, t1, L);
98 __ enter();
99 __ mv(x12, vtable_index);
100
101 __ call_VM(noreg, CAST_FROM_FN_PTR(address, bad_compiled_vtable_index), j_rarg0, x12);
102 const ptrdiff_t estimate = 256;
103 const ptrdiff_t codesize = __ pc() - start_pc;
104 slop_delta = estimate - codesize; // call_VM varies in length, depending on data
105 slop_bytes += slop_delta;
106 assert(slop_delta >= 0, "vtable #%d: Code size estimate (%d) for DebugVtables too small, required: %d", vtable_index, (int)estimate, (int)codesize);
107
108 __ leave();
109 __ bind(L);
110 }
111 #endif // PRODUCT
112
113 start_pc = __ pc();
114 __ lookup_virtual_method(t2, vtable_index, xmethod);
115 // lookup_virtual_method generates
116 // 4 instructions (maximum value encountered in normal case):li(lui + addiw) + add + ld
117 // 1 instruction (best case):ld * 1
118 slop_delta = 16 - (int)(__ pc() - start_pc);
119 slop_bytes += slop_delta;
120 assert(slop_delta >= 0, "negative slop(%d) encountered, adjust code size estimate!", slop_delta);
121
122 #ifndef PRODUCT
123 if (DebugVtables) {
124 Label L;
125 __ beqz(xmethod, L);
126 __ ld(t0, Address(xmethod, entry_offset));
127 __ bnez(t0, L);
128 __ stop("Vtable entry is null");
129 __ bind(L);
130 }
131 #endif // PRODUCT
132
133 // x10: receiver klass
134 // xmethod: Method*
135 // x12: receiver
136 address ame_addr = __ pc();
137 __ ld(t1, Address(xmethod, entry_offset));
138 __ jr(t1);
139
140 masm->flush();
141 bookkeeping(masm, tty, s, npe_addr, ame_addr, true, vtable_index, slop_bytes, 0);
142
143 return s;
144 }
145
146 VtableStub* VtableStubs::create_itable_stub(int itable_index, bool caller_is_c1) {
147 // Read "A word on VtableStub sizing" in share/code/vtableStubs.hpp for details on stub sizing.
148 const int stub_code_length = code_size_limit(false);
149 VtableStub* s = new(stub_code_length) VtableStub(false, itable_index, caller_is_c1);
150 // Can be null if there is no free space in the code cache.
151 if (s == nullptr) {
152 return nullptr;
153 }
154 // Count unused bytes in instruction sequences of variable size.
155 // We add them to the computed buffer size in order to avoid
156 // overflow in subsequently generated stubs.
157 address start_pc = nullptr;
158 int slop_bytes = 0;
159 int slop_delta = 0;
160
161 ByteSize entry_offset = caller_is_c1
162 ? Method::from_compiled_inline_offset()
163 : Method::from_compiled_inline_ro_offset();
164
165 ResourceMark rm;
166 CodeBuffer cb(s->entry_point(), stub_code_length);
167 MacroAssembler* masm = new MacroAssembler(&cb);
168 assert_cond(masm != nullptr);
169
170 // Real entry arguments:
171 // t0: CompiledICData
172 // j_rarg0: Receiver
173 // Make sure the move of CompiledICData from t0 to t1 is the frist thing that happens.
174 // Otherwise we risk clobber t0 as it is used as scratch.
175 __ mv(t1, t0);
176
177 #if (!defined(PRODUCT) && defined(COMPILER2))
178 if (CountCompiledCalls) {
179 __ la(x18, ExternalAddress((address) SharedRuntime::nof_megamorphic_calls_addr()));
180 __ increment(Address(x18));
181 }
182 #endif
183
184 // get receiver (need to skip return address on top of stack)
185 assert(VtableStub::receiver_location() == j_rarg0->as_VMReg(), "receiver expected in j_rarg0");
186
187 // Arguments from this point:
188 // t1 (moved from t0): CompiledICData
189 // j_rarg0: Receiver
190
191 // This stub is called from compiled code which has no callee-saved registers,
192 // so all registers except arguments are free at this point.
193 const Register recv_klass_reg = x18;
194 const Register holder_klass_reg = x19; // declaring interface klass (DEFC)
195 const Register resolved_klass_reg = x30; // resolved interface klass (REFC)
196 const Register temp_reg = x28;
197 const Register temp_reg2 = x29;
198 const Register icdata_reg = t1;
199
200 Label L_no_such_interface;
201
202 __ ld(resolved_klass_reg, Address(icdata_reg, CompiledICData::itable_refc_klass_offset()));
203 __ ld(holder_klass_reg, Address(icdata_reg, CompiledICData::itable_defc_klass_offset()));
204
205 start_pc = __ pc();
206
207 // get receiver klass (also an implicit null-check)
208 address npe_addr = __ pc();
209 __ load_klass(recv_klass_reg, j_rarg0);
210
211 // Receiver subtype check against REFC.
212 // Get selected method from declaring class and itable index
213 __ lookup_interface_method_stub(recv_klass_reg, holder_klass_reg, resolved_klass_reg, xmethod,
214 temp_reg, temp_reg2, itable_index, L_no_such_interface);
215
216 // Reduce "estimate" such that "padding" does not drop below 8.
217 const ptrdiff_t estimate = 256;
218 const ptrdiff_t codesize = __ pc() - start_pc;
219 slop_delta = (int)(estimate - codesize);
220 slop_bytes += slop_delta;
221 assert(slop_delta >= 0, "itable #%d: Code size estimate (%d) for lookup_interface_method too small, required: %d", itable_index, (int)estimate, (int)codesize);
222
223 #ifdef ASSERT
224 if (DebugVtables) {
225 Label L2;
226 __ beqz(xmethod, L2);
227 __ ld(t0, Address(xmethod, entry_offset));
228 __ bnez(t0, L2);
229 __ stop("compiler entrypoint is null");
230 __ bind(L2);
231 }
232 #endif // ASSERT
233
234 // xmethod: Method*
235 // j_rarg0: receiver
236 address ame_addr = __ pc();
237 __ ld(t1, Address(xmethod, entry_offset));
238 __ jr(t1);
239
240 __ bind(L_no_such_interface);
241 // Handle IncompatibleClassChangeError in itable stubs.
242 // More detailed error message.
243 // We force resolving of the call site by jumping to the "handle
244 // wrong method" stub, and so let the interpreter runtime do all the
245 // dirty work.
246 assert(SharedRuntime::get_handle_wrong_method_stub() != nullptr, "check initialization order");
247 __ far_jump(RuntimeAddress(SharedRuntime::get_handle_wrong_method_stub()));
248
249 masm->flush();
250 bookkeeping(masm, tty, s, npe_addr, ame_addr, false, itable_index, slop_bytes, 0);
251
252 return s;
253 }
254
255 int VtableStub::pd_code_alignment() {
256 // RISCV cache line size is not an architected constant. We just align on word size.
257 const unsigned int icache_line_size = wordSize;
258 return icache_line_size;
259 }