1 /*
2 * Copyright (c) 1999, 2025, Oracle and/or its affiliates. All rights reserved.
3 * Copyright (c) 2014, 2021, Red Hat Inc. All rights reserved.
4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 *
6 * This code is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License version 2 only, as
8 * published by the Free Software Foundation.
9 *
10 * This code is distributed in the hope that it will be useful, but WITHOUT
11 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
13 * version 2 for more details (a copy is included in the LICENSE file that
14 * accompanied this code).
15 *
16 * You should have received a copy of the GNU General Public License version
17 * 2 along with this work; if not, write to the Free Software Foundation,
18 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
19 *
20 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
21 * or visit www.oracle.com if you need additional information or have any
22 * questions.
23 *
24 */
25
26 #include "c1/c1_MacroAssembler.hpp"
27 #include "c1/c1_Runtime1.hpp"
28 #include "gc/shared/barrierSetAssembler.hpp"
29 #include "gc/shared/collectedHeap.hpp"
30 #include "gc/shared/barrierSet.hpp"
31 #include "gc/shared/barrierSetAssembler.hpp"
32 #include "gc/shared/tlab_globals.hpp"
33 #include "interpreter/interpreter.hpp"
34 #include "oops/arrayOop.hpp"
35 #include "oops/markWord.hpp"
36 #include "runtime/arguments.hpp"
37 #include "runtime/basicLock.hpp"
38 #include "runtime/os.hpp"
39 #include "runtime/sharedRuntime.hpp"
40 #include "runtime/stubRoutines.hpp"
41
42 void C1_MacroAssembler::float_cmp(bool is_float, int unordered_result,
43 FloatRegister f0, FloatRegister f1,
44 Register result)
45 {
46 Label done;
47 if (is_float) {
48 fcmps(f0, f1);
49 } else {
50 fcmpd(f0, f1);
51 }
52 if (unordered_result < 0) {
53 // we want -1 for unordered or less than, 0 for equal and 1 for
54 // greater than.
55 cset(result, NE); // Not equal or unordered
56 cneg(result, result, LT); // Less than or unordered
57 } else {
58 // we want -1 for less than, 0 for equal and 1 for unordered or
59 // greater than.
60 cset(result, NE); // Not equal or unordered
61 cneg(result, result, LO); // Less than
62 }
63 }
64
65 int C1_MacroAssembler::lock_object(Register hdr, Register obj, Register basic_lock, Register temp, Label& slow_case) {
66 assert_different_registers(hdr, obj, basic_lock, temp, rscratch2);
67 int null_check_offset = -1;
68
69 verify_oop(obj);
70
71 // save object being locked into the BasicObjectLock
72 str(obj, Address(basic_lock, BasicObjectLock::obj_offset()));
73
74 null_check_offset = offset();
75
76 fast_lock(basic_lock, obj, hdr, temp, rscratch2, slow_case);
77
78 return null_check_offset;
79 }
80
81
82 void C1_MacroAssembler::unlock_object(Register hdr, Register obj, Register basic_lock, Register temp, Label& slow_case) {
83 assert_different_registers(hdr, obj, basic_lock, temp, rscratch2);
84
85 // load object
86 ldr(obj, Address(basic_lock, BasicObjectLock::obj_offset()));
87 verify_oop(obj);
88
89 fast_unlock(obj, hdr, temp, rscratch2, slow_case);
90 }
91
92
93 // Defines obj, preserves var_size_in_bytes
94 void C1_MacroAssembler::try_allocate(Register obj, Register var_size_in_bytes, int con_size_in_bytes, Register t1, Register t2, Label& slow_case) {
95 if (UseTLAB) {
96 tlab_allocate(obj, var_size_in_bytes, con_size_in_bytes, t1, t2, slow_case);
97 } else {
98 b(slow_case);
99 }
100 }
101
102 void C1_MacroAssembler::initialize_header(Register obj, Register klass, Register len, Register t1, Register t2) {
103 assert_different_registers(obj, klass, len);
104
105 if (UseCompactObjectHeaders || Arguments::is_valhalla_enabled()) {
106 // COH: Markword contains class pointer which is only known at runtime.
107 // Valhalla: Could have value class which has a different prototype header to a normal object.
108 // In both cases, we need to fetch dynamically.
109 ldr(t1, Address(klass, Klass::prototype_header_offset()));
110 str(t1, Address(obj, oopDesc::mark_offset_in_bytes()));
111 } else {
112 // Otherwise: Can use the statically computed prototype header which is the same for every object.
113 mov(t1, checked_cast<int32_t>(markWord::prototype().value()));
114 str(t1, Address(obj, oopDesc::mark_offset_in_bytes()));
115 }
116
117 if (!UseCompactObjectHeaders) {
118 // COH: Markword already contains class pointer. Nothing else to do.
119 // Otherwise: Fetch klass pointer following the markword
120 if (UseCompressedClassPointers) { // Take care not to kill klass
121 encode_klass_not_null(t1, klass);
122 strw(t1, Address(obj, oopDesc::klass_offset_in_bytes()));
123 } else {
124 str(klass, Address(obj, oopDesc::klass_offset_in_bytes()));
125 }
126 }
127
128 if (len->is_valid()) {
129 strw(len, Address(obj, arrayOopDesc::length_offset_in_bytes()));
130 int base_offset = arrayOopDesc::length_offset_in_bytes() + BytesPerInt;
131 if (!is_aligned(base_offset, BytesPerWord)) {
132 assert(is_aligned(base_offset, BytesPerInt), "must be 4-byte aligned");
133 // Clear gap/first 4 bytes following the length field.
134 strw(zr, Address(obj, base_offset));
135 }
136 } else if (UseCompressedClassPointers && !UseCompactObjectHeaders) {
137 store_klass_gap(obj, zr);
138 }
139 }
140
141 // preserves obj, destroys len_in_bytes
142 //
143 // Scratch registers: t1 = r10, t2 = r11
144 //
145 void C1_MacroAssembler::initialize_body(Register obj, Register len_in_bytes, int hdr_size_in_bytes, Register t1, Register t2) {
146 assert(hdr_size_in_bytes >= 0, "header size must be positive or 0");
147 assert(t1 == r10 && t2 == r11, "must be");
148
149 Label done;
150
151 // len_in_bytes is positive and ptr sized
152 subs(len_in_bytes, len_in_bytes, hdr_size_in_bytes);
153 br(Assembler::EQ, done);
154
155 // zero_words() takes ptr in r10 and count in words in r11
156 mov(rscratch1, len_in_bytes);
157 lea(t1, Address(obj, hdr_size_in_bytes));
158 lsr(t2, rscratch1, LogBytesPerWord);
159 address tpc = zero_words(t1, t2);
160
161 bind(done);
162 if (tpc == nullptr) {
163 Compilation::current()->bailout("no space for trampoline stub");
164 }
165 }
166
167
168 void C1_MacroAssembler::allocate_object(Register obj, Register t1, Register t2, int header_size, int object_size, Register klass, Label& slow_case) {
169 assert_different_registers(obj, t1, t2); // XXX really?
170 assert(header_size >= 0 && object_size >= header_size, "illegal sizes");
171
172 try_allocate(obj, noreg, object_size * BytesPerWord, t1, t2, slow_case);
173
174 initialize_object(obj, klass, noreg, object_size * HeapWordSize, t1, t2, UseTLAB);
175 }
176
177 // Scratch registers: t1 = r10, t2 = r11
178 void C1_MacroAssembler::initialize_object(Register obj, Register klass, Register var_size_in_bytes, int con_size_in_bytes, Register t1, Register t2, bool is_tlab_allocated) {
179 assert((con_size_in_bytes & MinObjAlignmentInBytesMask) == 0,
180 "con_size_in_bytes is not multiple of alignment");
181 const int hdr_size_in_bytes = instanceOopDesc::header_size() * HeapWordSize;
182
183 initialize_header(obj, klass, noreg, t1, t2);
184
185 if (!(UseTLAB && ZeroTLAB && is_tlab_allocated)) {
186 // clear rest of allocated space
187 const Register index = t2;
188 if (var_size_in_bytes != noreg) {
189 mov(index, var_size_in_bytes);
190 initialize_body(obj, index, hdr_size_in_bytes, t1, t2);
191 if (Compilation::current()->bailed_out()) {
192 return;
193 }
194 } else if (con_size_in_bytes > hdr_size_in_bytes) {
195 con_size_in_bytes -= hdr_size_in_bytes;
196 lea(t1, Address(obj, hdr_size_in_bytes));
197 address tpc = zero_words(t1, con_size_in_bytes / BytesPerWord);
198 if (tpc == nullptr) {
199 Compilation::current()->bailout("no space for trampoline stub");
200 return;
201 }
202 }
203 }
204
205 membar(StoreStore);
206
207 if (CURRENT_ENV->dtrace_alloc_probes()) {
208 assert(obj == r0, "must be");
209 far_call(RuntimeAddress(Runtime1::entry_for(StubId::c1_dtrace_object_alloc_id)));
210 }
211
212 verify_oop(obj);
213 }
214 void C1_MacroAssembler::allocate_array(Register obj, Register len, Register t1, Register t2, int base_offset_in_bytes, int f, Register klass, Label& slow_case, bool zero_array) {
215 assert_different_registers(obj, len, t1, t2, klass);
216
217 // determine alignment mask
218 assert(!(BytesPerWord & 1), "must be a multiple of 2 for masking code to work");
219
220 // check for negative or excessive length
221 mov(rscratch1, (int32_t)max_array_allocation_length);
222 cmp(len, rscratch1);
223 br(Assembler::HS, slow_case);
224
225 const Register arr_size = t2; // okay to be the same
226 // align object end
227 mov(arr_size, (int32_t)base_offset_in_bytes + MinObjAlignmentInBytesMask);
228 add(arr_size, arr_size, len, ext::uxtw, f);
229 andr(arr_size, arr_size, ~MinObjAlignmentInBytesMask);
230
231 try_allocate(obj, arr_size, 0, t1, t2, slow_case);
232
233 initialize_header(obj, klass, len, t1, t2);
234
235 // Align-up to word boundary, because we clear the 4 bytes potentially
236 // following the length field in initialize_header().
237 int base_offset = align_up(base_offset_in_bytes, BytesPerWord);
238 // clear rest of allocated space
239 if (zero_array) {
240 initialize_body(obj, arr_size, base_offset, t1, t2);
241 }
242 if (Compilation::current()->bailed_out()) {
243 return;
244 }
245
246 membar(StoreStore);
247
248 if (CURRENT_ENV->dtrace_alloc_probes()) {
249 assert(obj == r0, "must be");
250 far_call(RuntimeAddress(Runtime1::entry_for(StubId::c1_dtrace_object_alloc_id)));
251 }
252
253 verify_oop(obj);
254 }
255
256 void C1_MacroAssembler::build_frame_helper(int frame_size_in_bytes, int sp_offset_for_orig_pc, int sp_inc, bool reset_orig_pc, bool needs_stack_repair) {
257 MacroAssembler::build_frame(frame_size_in_bytes);
258
259 if (needs_stack_repair) {
260 save_stack_increment(sp_inc, frame_size_in_bytes);
261 }
262 if (reset_orig_pc) {
263 // Zero orig_pc to detect deoptimization during buffering in the entry points
264 str(zr, Address(sp, sp_offset_for_orig_pc));
265 }
266 }
267
268 void C1_MacroAssembler::build_frame(int frame_size_in_bytes, int bang_size_in_bytes, int sp_offset_for_orig_pc, bool needs_stack_repair, bool has_scalarized_args, Label* verified_inline_entry_label) {
269 // Make sure there is enough stack space for this method's activation.
270 // Note that we do this before creating a frame.
271 assert(bang_size_in_bytes >= frame_size_in_bytes, "stack bang size incorrect");
272 generate_stack_overflow_check(bang_size_in_bytes);
273
274 build_frame_helper(frame_size_in_bytes, sp_offset_for_orig_pc, 0, has_scalarized_args, needs_stack_repair);
275
276 // Insert nmethod entry barrier into frame.
277 BarrierSetAssembler* bs = BarrierSet::barrier_set()->barrier_set_assembler();
278 bs->nmethod_entry_barrier(this, nullptr /* slow_path */, nullptr /* continuation */, nullptr /* guard */);
279
280 if (verified_inline_entry_label != nullptr) {
281 // Jump here from the scalarized entry points that already created the frame.
282 bind(*verified_inline_entry_label);
283 }
284 }
285
286 void C1_MacroAssembler::verified_entry(bool breakAtEntry) {
287 // If we have to make this method not-entrant we'll overwrite its
288 // first instruction with a jump. For this action to be legal we
289 // must ensure that this first instruction is a B, BL, NOP, BKPT,
290 // SVC, HVC, or SMC. Make it a NOP.
291 nop();
292 if (C1Breakpoint) brk(1);
293 }
294
295 int C1_MacroAssembler::scalarized_entry(const CompiledEntrySignature* ces, int frame_size_in_bytes, int bang_size_in_bytes, int sp_offset_for_orig_pc, Label& verified_inline_entry_label, bool is_inline_ro_entry) {
296 assert(InlineTypePassFieldsAsArgs, "sanity");
297 // Make sure there is enough stack space for this method's activation.
298 assert(bang_size_in_bytes >= frame_size_in_bytes, "stack bang size incorrect");
299 generate_stack_overflow_check(bang_size_in_bytes);
300
301 GrowableArray<SigEntry>* sig = ces->sig();
302 GrowableArray<SigEntry>* sig_cc = is_inline_ro_entry ? ces->sig_cc_ro() : ces->sig_cc();
303 VMRegPair* regs = ces->regs();
304 VMRegPair* regs_cc = is_inline_ro_entry ? ces->regs_cc_ro() : ces->regs_cc();
305 int args_on_stack = ces->args_on_stack();
306 int args_on_stack_cc = is_inline_ro_entry ? ces->args_on_stack_cc_ro() : ces->args_on_stack_cc();
307
308 assert(sig->length() <= sig_cc->length(), "Zero-sized inline class not allowed!");
309 BasicType* sig_bt = NEW_RESOURCE_ARRAY(BasicType, sig_cc->length());
310 int args_passed = sig->length();
311 int args_passed_cc = SigEntry::fill_sig_bt(sig_cc, sig_bt);
312
313 // Create a temp frame so we can call into the runtime. It must be properly set up to accommodate GC.
314 build_frame_helper(frame_size_in_bytes, sp_offset_for_orig_pc, 0, true, ces->c1_needs_stack_repair());
315
316 // The runtime call might safepoint, make sure nmethod entry barrier is executed
317 BarrierSetAssembler* bs = BarrierSet::barrier_set()->barrier_set_assembler();
318 // C1 code is not hot enough to micro optimize the nmethod entry barrier with an out-of-line stub
319 bs->nmethod_entry_barrier(this, nullptr /* slow_path */, nullptr /* continuation */, nullptr /* guard */);
320
321 // FIXME -- call runtime only if we cannot in-line allocate all the incoming inline type args.
322 mov(r19, (intptr_t) ces->method());
323 if (is_inline_ro_entry) {
324 far_call(RuntimeAddress(Runtime1::entry_for(StubId::c1_buffer_inline_args_no_receiver_id)));
325 } else {
326 far_call(RuntimeAddress(Runtime1::entry_for(StubId::c1_buffer_inline_args_id)));
327 }
328 int rt_call_offset = offset();
329
330 // The runtime call returns the new array in r20 instead of the usual r0
331 // because r0 is also j_rarg7 which may be holding a live argument here.
332 Register val_array = r20;
333
334 // Remove the temp frame
335 MacroAssembler::remove_frame(frame_size_in_bytes);
336
337 // Check if we need to extend the stack for packing
338 int sp_inc = 0;
339 if (args_on_stack > args_on_stack_cc) {
340 sp_inc = extend_stack_for_inline_args(args_on_stack);
341 }
342
343 shuffle_inline_args(true, is_inline_ro_entry, sig_cc,
344 args_passed_cc, args_on_stack_cc, regs_cc, // from
345 args_passed, args_on_stack, regs, // to
346 sp_inc, val_array);
347
348 // Create the real frame. Below jump will then skip over the stack banging and frame
349 // setup code in the verified_inline_entry (which has a different real_frame_size).
350 build_frame_helper(frame_size_in_bytes, sp_offset_for_orig_pc, sp_inc, false, ces->c1_needs_stack_repair());
351
352 b(verified_inline_entry_label);
353 return rt_call_offset;
354 }
355
356
357 void C1_MacroAssembler::load_parameter(int offset_in_words, Register reg) {
358 // rfp, + 0: link
359 // + 1: return address
360 // + 2: argument with offset 0
361 // + 3: argument with offset 1
362 // + 4: ...
363
364 ldr(reg, Address(rfp, (offset_in_words + 2) * BytesPerWord));
365 }
366
367 #ifndef PRODUCT
368
369 void C1_MacroAssembler::verify_stack_oop(int stack_offset) {
370 if (!VerifyOops) return;
371 verify_oop_addr(Address(sp, stack_offset));
372 }
373
374 void C1_MacroAssembler::verify_not_null_oop(Register r) {
375 if (!VerifyOops) return;
376 Label not_null;
377 cbnz(r, not_null);
378 stop("non-null oop required");
379 bind(not_null);
380 verify_oop(r);
381 }
382
383 void C1_MacroAssembler::invalidate_registers(bool inv_r0, bool inv_r19, bool inv_r2, bool inv_r3, bool inv_r4, bool inv_r5) {
384 #ifdef ASSERT
385 static int nn;
386 if (inv_r0) mov(r0, 0xDEAD);
387 if (inv_r19) mov(r19, 0xDEAD);
388 if (inv_r2) mov(r2, nn++);
389 if (inv_r3) mov(r3, 0xDEAD);
390 if (inv_r4) mov(r4, 0xDEAD);
391 if (inv_r5) mov(r5, 0xDEAD);
392 #endif
393 }
394 #endif // ifndef PRODUCT