1 /*
2 * Copyright (c) 2003, 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
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) {
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);
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 ResourceMark rm;
67 CodeBuffer cb(s->entry_point(), stub_code_length);
68 MacroAssembler* masm = new MacroAssembler(&cb);
69 assert_cond(masm != nullptr);
70
71 #if (!defined(PRODUCT) && defined(COMPILER2))
72 if (CountCompiledCalls) {
73 __ la(t2, ExternalAddress((address) SharedRuntime::nof_megamorphic_calls_addr()));
74 __ increment(Address(t2));
75 }
76 #endif
77
78 // get receiver (need to skip return address on top of stack)
79 assert(VtableStub::receiver_location() == j_rarg0->as_VMReg(), "receiver expected in j_rarg0");
80
81 // get receiver klass
82 address npe_addr = __ pc();
83 __ load_klass(t2, j_rarg0);
84
85 #ifndef PRODUCT
102 assert(slop_delta >= 0, "vtable #%d: Code size estimate (%d) for DebugVtables too small, required: %d", vtable_index, (int)estimate, (int)codesize);
103
104 __ leave();
105 __ bind(L);
106 }
107 #endif // PRODUCT
108
109 start_pc = __ pc();
110 __ lookup_virtual_method(t2, vtable_index, xmethod);
111 // lookup_virtual_method generates
112 // 4 instructions (maximum value encountered in normal case):li(lui + addiw) + add + ld
113 // 1 instruction (best case):ld * 1
114 slop_delta = 16 - (int)(__ pc() - start_pc);
115 slop_bytes += slop_delta;
116 assert(slop_delta >= 0, "negative slop(%d) encountered, adjust code size estimate!", slop_delta);
117
118 #ifndef PRODUCT
119 if (DebugVtables) {
120 Label L;
121 __ beqz(xmethod, L);
122 __ ld(t0, Address(xmethod, Method::from_compiled_offset()));
123 __ bnez(t0, L);
124 __ stop("Vtable entry is null");
125 __ bind(L);
126 }
127 #endif // PRODUCT
128
129 // x10: receiver klass
130 // xmethod: Method*
131 // x12: receiver
132 address ame_addr = __ pc();
133 __ ld(t1, Address(xmethod, Method::from_compiled_offset()));
134 __ jr(t1);
135
136 masm->flush();
137 bookkeeping(masm, tty, s, npe_addr, ame_addr, true, vtable_index, slop_bytes, 0);
138
139 return s;
140 }
141
142 VtableStub* VtableStubs::create_itable_stub(int itable_index) {
143 // Read "A word on VtableStub sizing" in share/code/vtableStubs.hpp for details on stub sizing.
144 const int stub_code_length = code_size_limit(false);
145 VtableStub* s = new(stub_code_length) VtableStub(false, itable_index);
146 // Can be null if there is no free space in the code cache.
147 if (s == nullptr) {
148 return nullptr;
149 }
150 // Count unused bytes in instruction sequences of variable size.
151 // We add them to the computed buffer size in order to avoid
152 // overflow in subsequently generated stubs.
153 address start_pc = nullptr;
154 int slop_bytes = 0;
155 int slop_delta = 0;
156
157 ResourceMark rm;
158 CodeBuffer cb(s->entry_point(), stub_code_length);
159 MacroAssembler* masm = new MacroAssembler(&cb);
160 assert_cond(masm != nullptr);
161
162 // Real entry arguments:
163 // t0: CompiledICData
164 // j_rarg0: Receiver
165 // Make sure the move of CompiledICData from t0 to t1 is the frist thing that happens.
166 // Otherwise we risk clobber t0 as it is used as scratch.
167 __ mv(t1, t0);
168
169 #if (!defined(PRODUCT) && defined(COMPILER2))
170 if (CountCompiledCalls) {
171 __ la(x18, ExternalAddress((address) SharedRuntime::nof_megamorphic_calls_addr()));
172 __ increment(Address(x18));
173 }
174 #endif
175
176 // get receiver (need to skip return address on top of stack)
199 // get receiver klass (also an implicit null-check)
200 address npe_addr = __ pc();
201 __ load_klass(recv_klass_reg, j_rarg0);
202
203 // Receiver subtype check against REFC.
204 // Get selected method from declaring class and itable index
205 __ lookup_interface_method_stub(recv_klass_reg, holder_klass_reg, resolved_klass_reg, xmethod,
206 temp_reg, temp_reg2, itable_index, L_no_such_interface);
207
208 // Reduce "estimate" such that "padding" does not drop below 8.
209 const ptrdiff_t estimate = 256;
210 const ptrdiff_t codesize = __ pc() - start_pc;
211 slop_delta = (int)(estimate - codesize);
212 slop_bytes += slop_delta;
213 assert(slop_delta >= 0, "itable #%d: Code size estimate (%d) for lookup_interface_method too small, required: %d", itable_index, (int)estimate, (int)codesize);
214
215 #ifdef ASSERT
216 if (DebugVtables) {
217 Label L2;
218 __ beqz(xmethod, L2);
219 __ ld(t0, Address(xmethod, Method::from_compiled_offset()));
220 __ bnez(t0, L2);
221 __ stop("compiler entrypoint is null");
222 __ bind(L2);
223 }
224 #endif // ASSERT
225
226 // xmethod: Method*
227 // j_rarg0: receiver
228 address ame_addr = __ pc();
229 __ ld(t1, Address(xmethod, Method::from_compiled_offset()));
230 __ jr(t1);
231
232 __ bind(L_no_such_interface);
233 // Handle IncompatibleClassChangeError in itable stubs.
234 // More detailed error message.
235 // We force resolving of the call site by jumping to the "handle
236 // wrong method" stub, and so let the interpreter runtime do all the
237 // dirty work.
238 assert(SharedRuntime::get_handle_wrong_method_stub() != nullptr, "check initialization order");
239 __ far_jump(RuntimeAddress(SharedRuntime::get_handle_wrong_method_stub()));
240
241 masm->flush();
242 bookkeeping(masm, tty, s, npe_addr, ame_addr, false, itable_index, slop_bytes, 0);
243
244 return s;
245 }
246
247 int VtableStub::pd_code_alignment() {
248 // RISCV cache line size is not an architected constant. We just align on word size.
249 const unsigned int icache_line_size = wordSize;
|
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
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
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)
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;
|