1 /*
2 * Copyright (c) 1999, 2026, 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/tlab_globals.hpp"
31 #include "interpreter/interpreter.hpp"
32 #include "oops/arrayOop.hpp"
33 #include "oops/markWord.hpp"
34 #include "runtime/basicLock.hpp"
35 #include "runtime/os.hpp"
36 #include "runtime/sharedRuntime.hpp"
37 #include "runtime/stubRoutines.hpp"
38
39 void C1_MacroAssembler::float_cmp(bool is_float, int unordered_result,
40 FloatRegister f0, FloatRegister f1,
41 Register result)
42 {
43 Label done;
44 if (is_float) {
45 fcmps(f0, f1);
46 } else {
47 fcmpd(f0, f1);
48 }
49 if (unordered_result < 0) {
50 // we want -1 for unordered or less than, 0 for equal and 1 for
51 // greater than.
52 cset(result, NE); // Not equal or unordered
53 cneg(result, result, LT); // Less than or unordered
54 } else {
55 // we want -1 for less than, 0 for equal and 1 for unordered or
56 // greater than.
57 cset(result, NE); // Not equal or unordered
58 cneg(result, result, LO); // Less than
59 }
60 }
61
62 int C1_MacroAssembler::lock_object(Register hdr, Register obj, Register basic_lock, Register temp, Label& slow_case) {
63 assert_different_registers(hdr, obj, basic_lock, temp, rscratch2);
64 int null_check_offset = -1;
65
66 verify_oop(obj);
67
68 // save object being locked into the BasicObjectLock
69 str(obj, Address(basic_lock, BasicObjectLock::obj_offset()));
70
71 null_check_offset = offset();
72
73 fast_lock(basic_lock, obj, hdr, temp, rscratch2, slow_case);
74
75 return null_check_offset;
76 }
77
78
79 void C1_MacroAssembler::unlock_object(Register hdr, Register obj, Register basic_lock, Register temp, Label& slow_case) {
80 assert_different_registers(hdr, obj, basic_lock, temp, rscratch2);
81
82 // load object
83 ldr(obj, Address(basic_lock, BasicObjectLock::obj_offset()));
84 verify_oop(obj);
85
86 fast_unlock(obj, hdr, temp, rscratch2, slow_case);
87 }
88
89
90 // Defines obj, preserves var_size_in_bytes
91 void C1_MacroAssembler::try_allocate(Register obj, Register var_size_in_bytes, int con_size_in_bytes, Register t1, Register t2, Label& slow_case) {
92 if (UseTLAB) {
93 tlab_allocate(obj, var_size_in_bytes, con_size_in_bytes, t1, t2, slow_case);
94 } else {
95 b(slow_case);
96 }
97 }
98
99 void C1_MacroAssembler::initialize_header(Register obj, Register klass, Register len, Register t1, Register t2) {
100 assert_different_registers(obj, klass, len);
101
102 if (UseCompactObjectHeaders) {
103 ldr(t1, Address(klass, Klass::prototype_header_offset()));
104 str(t1, Address(obj, oopDesc::mark_offset_in_bytes()));
105 } else {
106 mov(t1, checked_cast<int32_t>(markWord::prototype().value()));
107 str(t1, Address(obj, oopDesc::mark_offset_in_bytes()));
108 encode_klass_not_null(t1, klass); // Take care not to kill klass
109 strw(t1, Address(obj, oopDesc::klass_offset_in_bytes()));
110 }
111
112 if (len->is_valid()) {
113 strw(len, Address(obj, arrayOopDesc::length_offset_in_bytes()));
114 int base_offset = arrayOopDesc::length_offset_in_bytes() + BytesPerInt;
115 if (!is_aligned(base_offset, BytesPerWord)) {
116 assert(is_aligned(base_offset, BytesPerInt), "must be 4-byte aligned");
117 // Clear gap/first 4 bytes following the length field.
118 strw(zr, Address(obj, base_offset));
119 }
120 } else if (!UseCompactObjectHeaders) {
121 store_klass_gap(obj, zr);
122 }
123 }
124
125 // preserves obj, destroys len_in_bytes
126 //
127 // Scratch registers: t1 = r10, t2 = r11
128 //
129 void C1_MacroAssembler::initialize_body(Register obj, Register len_in_bytes, int hdr_size_in_bytes, Register t1, Register t2) {
130 assert(hdr_size_in_bytes >= 0, "header size must be positive or 0");
131 assert(t1 == r10 && t2 == r11, "must be");
132
133 Label done;
134
135 // len_in_bytes is positive and ptr sized
136 subs(len_in_bytes, len_in_bytes, hdr_size_in_bytes);
137 br(Assembler::EQ, done);
138
139 // zero_words() takes ptr in r10 and count in words in r11
140 mov(rscratch1, len_in_bytes);
141 lea(t1, Address(obj, hdr_size_in_bytes));
142 lsr(t2, rscratch1, LogBytesPerWord);
143 address tpc = zero_words(t1, t2);
144
145 bind(done);
146 if (tpc == nullptr) {
147 Compilation::current()->bailout("no space for trampoline stub");
148 }
149 }
150
151
152 void C1_MacroAssembler::allocate_object(Register obj, Register t1, Register t2, int header_size, int object_size, Register klass, Label& slow_case) {
153 assert_different_registers(obj, t1, t2); // XXX really?
154 assert(header_size >= 0 && object_size >= header_size, "illegal sizes");
155
156 try_allocate(obj, noreg, object_size * BytesPerWord, t1, t2, slow_case);
157
158 initialize_object(obj, klass, noreg, object_size * HeapWordSize, t1, t2, UseTLAB);
159 }
160
161 // Scratch registers: t1 = r10, t2 = r11
162 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) {
163 assert((con_size_in_bytes & MinObjAlignmentInBytesMask) == 0,
164 "con_size_in_bytes is not multiple of alignment");
165 const int hdr_size_in_bytes = instanceOopDesc::header_size() * HeapWordSize;
166
167 initialize_header(obj, klass, noreg, t1, t2);
168
169 if (!(UseTLAB && ZeroTLAB && is_tlab_allocated)) {
170 // clear rest of allocated space
171 const Register index = t2;
172 if (var_size_in_bytes != noreg) {
173 mov(index, var_size_in_bytes);
174 initialize_body(obj, index, hdr_size_in_bytes, t1, t2);
175 if (Compilation::current()->bailed_out()) {
176 return;
177 }
178 } else if (con_size_in_bytes > hdr_size_in_bytes) {
179 con_size_in_bytes -= hdr_size_in_bytes;
180 lea(t1, Address(obj, hdr_size_in_bytes));
181 address tpc = zero_words(t1, con_size_in_bytes / BytesPerWord);
182 if (tpc == nullptr) {
183 Compilation::current()->bailout("no space for trampoline stub");
184 return;
185 }
186 }
187 }
188
189 membar(StoreStore);
190
191 if (CURRENT_ENV->dtrace_alloc_probes()) {
192 assert(obj == r0, "must be");
193 far_call(RuntimeAddress(Runtime1::entry_for(StubId::c1_dtrace_object_alloc_id)));
194 }
195
196 verify_oop(obj);
197 }
198 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) {
199 assert_different_registers(obj, len, t1, t2, klass);
200
201 // determine alignment mask
202 assert(!(BytesPerWord & 1), "must be a multiple of 2 for masking code to work");
203
204 // check for negative or excessive length
205 mov(rscratch1, (int32_t)max_array_allocation_length);
206 cmp(len, rscratch1);
207 br(Assembler::HS, slow_case);
208
209 const Register arr_size = t2; // okay to be the same
210 // align object end
211 mov(arr_size, (int32_t)base_offset_in_bytes + MinObjAlignmentInBytesMask);
212 add(arr_size, arr_size, len, ext::uxtw, f);
213 andr(arr_size, arr_size, ~MinObjAlignmentInBytesMask);
214
215 try_allocate(obj, arr_size, 0, t1, t2, slow_case);
216
217 initialize_header(obj, klass, len, t1, t2);
218
219 // Align-up to word boundary, because we clear the 4 bytes potentially
220 // following the length field in initialize_header().
221 int base_offset = align_up(base_offset_in_bytes, BytesPerWord);
222 // clear rest of allocated space
223 if (zero_array) {
224 initialize_body(obj, arr_size, base_offset, t1, t2);
225 }
226 if (Compilation::current()->bailed_out()) {
227 return;
228 }
229
230 membar(StoreStore);
231
232 if (CURRENT_ENV->dtrace_alloc_probes()) {
233 assert(obj == r0, "must be");
234 far_call(RuntimeAddress(Runtime1::entry_for(StubId::c1_dtrace_object_alloc_id)));
235 }
236
237 verify_oop(obj);
238 }
239
240 void C1_MacroAssembler::build_frame(int framesize, int bang_size_in_bytes) {
241 assert(bang_size_in_bytes >= framesize, "stack bang size incorrect");
242 // Make sure there is enough stack space for this method's activation.
243 // Note that we do this before creating a frame.
244 generate_stack_overflow_check(bang_size_in_bytes);
245 MacroAssembler::build_frame(framesize);
246
247 // Insert nmethod entry barrier into frame.
248 BarrierSetAssembler* bs = BarrierSet::barrier_set()->barrier_set_assembler();
249 bs->nmethod_entry_barrier(this, nullptr /* slow_path */, nullptr /* continuation */, nullptr /* guard */);
250 }
251
252 void C1_MacroAssembler::remove_frame(int framesize) {
253 MacroAssembler::remove_frame(framesize);
254 }
255
256
257 void C1_MacroAssembler::verified_entry(bool breakAtEntry) {
258 // If we have to make this method not-entrant we'll overwrite its
259 // first instruction with a jump. For this action to be legal we
260 // must ensure that this first instruction is a B, BL, NOP, BKPT,
261 // SVC, HVC, or SMC. Make it a NOP.
262 nop();
263 }
264
265 void C1_MacroAssembler::load_parameter(int offset_in_words, Register reg) {
266 // rfp, + 0: link
267 // + 1: return address
268 // + 2: argument with offset 0
269 // + 3: argument with offset 1
270 // + 4: ...
271
272 ldr(reg, Address(rfp, (offset_in_words + 2) * BytesPerWord));
273 }
274
275 #ifndef PRODUCT
276
277 void C1_MacroAssembler::verify_stack_oop(int stack_offset) {
278 if (!VerifyOops) return;
279 verify_oop_addr(Address(sp, stack_offset));
280 }
281
282 void C1_MacroAssembler::verify_not_null_oop(Register r) {
283 if (!VerifyOops) return;
284 Label not_null;
285 cbnz(r, not_null);
286 stop("non-null oop required");
287 bind(not_null);
288 verify_oop(r);
289 }
290
291 void C1_MacroAssembler::invalidate_registers(bool inv_r0, bool inv_r19, bool inv_r2, bool inv_r3, bool inv_r4, bool inv_r5) {
292 #ifdef ASSERT
293 static int nn;
294 if (inv_r0) mov(r0, 0xDEAD);
295 if (inv_r19) mov(r19, 0xDEAD);
296 if (inv_r2) mov(r2, nn++);
297 if (inv_r3) mov(r3, 0xDEAD);
298 if (inv_r4) mov(r4, 0xDEAD);
299 if (inv_r5) mov(r5, 0xDEAD);
300 #endif
301 }
302 #endif // ifndef PRODUCT