1 /*
  2  * Copyright (c) 1999, 2026, Oracle and/or its affiliates. All rights reserved.
  3  * Copyright (c) 2012, 2025 SAP SE. 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 "asm/macroAssembler.inline.hpp"
 27 #include "c1/c1_MacroAssembler.hpp"
 28 #include "c1/c1_Runtime1.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 #include "utilities/align.hpp"
 39 #include "utilities/macros.hpp"
 40 #include "utilities/powerOfTwo.hpp"
 41 
 42 
 43 void C1_MacroAssembler::explicit_null_check(Register base) {
 44   Unimplemented();
 45 }
 46 
 47 
 48 void C1_MacroAssembler::build_frame(int frame_size_in_bytes, int bang_size_in_bytes,
 49                                     int sp_offset_for_orig_pc,
 50                                     bool needs_stack_repair, bool has_scalarized_args,
 51                                     Label* verified_inline_entry_label) {
 52   const Register return_pc = R20;
 53   mflr(return_pc);
 54 
 55   // Make sure there is enough stack space for this method's activation.
 56   assert(bang_size_in_bytes >= frame_size_in_bytes, "stack bang size incorrect");
 57   generate_stack_overflow_check(bang_size_in_bytes);
 58 
 59   std(return_pc, _abi0(lr), R1_SP);     // SP->lr = return_pc
 60   push_frame(frame_size_in_bytes, R0); // SP -= frame_size_in_bytes
 61 
 62   BarrierSetAssembler* bs = BarrierSet::barrier_set()->barrier_set_assembler();
 63   bs->nmethod_entry_barrier(this, R20);
 64 }
 65 
 66 
 67 void C1_MacroAssembler::verified_entry(bool breakAtEntry) {
 68   if (breakAtEntry) illtrap();
 69   // build frame
 70 }
 71 
 72 
 73 void C1_MacroAssembler::lock_object(Register Rmark, Register Roop, Register Rbox, Register Rscratch, Label& slow_case) {
 74   assert_different_registers(Rmark, Roop, Rbox, Rscratch);
 75 
 76   Label done, cas_failed, slow_int;
 77 
 78   // The following move must be the first instruction of emitted since debug
 79   // information may be generated for it.
 80   // Load object header.
 81   ld(Rmark, oopDesc::mark_offset_in_bytes(), Roop);
 82 
 83   verify_oop(Roop, FILE_AND_LINE);
 84 
 85   // Save object being locked into the BasicObjectLock...
 86   std(Roop, in_bytes(BasicObjectLock::obj_offset()), Rbox);
 87 
 88   fast_lock(Rbox, Roop, Rmark, Rscratch, slow_int);
 89   b(done);
 90 
 91   bind(slow_int);
 92   b(slow_case); // far
 93 
 94   bind(done);
 95 }
 96 
 97 
 98 void C1_MacroAssembler::unlock_object(Register Rmark, Register Roop, Register Rbox, Label& slow_case) {
 99   assert_different_registers(Rmark, Roop, Rbox);
100 
101   Label slow_int, done;
102 
103   Address mark_addr(Roop, oopDesc::mark_offset_in_bytes());
104   assert(mark_addr.disp() == 0, "cas must take a zero displacement");
105 
106   // Load object.
107   ld(Roop, in_bytes(BasicObjectLock::obj_offset()), Rbox);
108   verify_oop(Roop, FILE_AND_LINE);
109 
110   fast_unlock(Roop, Rmark, slow_int);
111   b(done);
112   bind(slow_int);
113   b(slow_case); // far
114 
115   // Done
116   bind(done);
117 }
118 
119 
120 void C1_MacroAssembler::try_allocate(
121   Register obj,                        // result: pointer to object after successful allocation
122   Register var_size_in_bytes,          // object size in bytes if unknown at compile time; invalid otherwise
123   int      con_size_in_bytes,          // object size in bytes if   known at compile time
124   Register t1,                         // temp register
125   Register t2,                         // temp register
126   Label&   slow_case                   // continuation point if fast allocation fails
127 ) {
128   if (UseTLAB) {
129     tlab_allocate(obj, var_size_in_bytes, con_size_in_bytes, t1, slow_case);
130   } else {
131     b(slow_case);
132   }
133 }
134 
135 
136 void C1_MacroAssembler::initialize_header(Register obj, Register klass, Register len, Register t1, Register t2) {
137   assert_different_registers(obj, klass, len, t1, t2);
138 
139   if (UseCompactObjectHeaders) {
140     ld(t1, in_bytes(Klass::prototype_header_offset()), klass);
141     std(t1, oopDesc::mark_offset_in_bytes(), obj);
142   } else {
143     load_const_optimized(t1, (intx)markWord::prototype().value());
144     std(t1, oopDesc::mark_offset_in_bytes(), obj);
145     store_klass(obj, klass);
146   }
147 
148   if (len->is_valid()) {
149     stw(len, arrayOopDesc::length_offset_in_bytes(), obj);
150   } else if (!UseCompactObjectHeaders) {
151     // Otherwise length is in the class gap.
152     store_klass_gap(obj);
153   }
154 }
155 
156 
157 void C1_MacroAssembler::initialize_body(Register base, Register index) {
158   assert_different_registers(base, index);
159   srdi(index, index, LogBytesPerWord);
160   clear_memory_doubleword(base, index);
161 }
162 
163 void C1_MacroAssembler::initialize_body(Register obj, Register tmp1, Register tmp2,
164                                         int obj_size_in_bytes, int hdr_size_in_bytes) {
165   const int index = (obj_size_in_bytes - hdr_size_in_bytes) / HeapWordSize;
166 
167   // 2x unrolled loop is shorter with more than 9 HeapWords.
168   if (index <= 9) {
169     clear_memory_unrolled(obj, index, R0, hdr_size_in_bytes);
170   } else {
171     const Register base_ptr = tmp1,
172                    cnt_dwords = tmp2;
173 
174     addi(base_ptr, obj, hdr_size_in_bytes); // Compute address of first element.
175     clear_memory_doubleword(base_ptr, cnt_dwords, R0, index);
176   }
177 }
178 
179 void C1_MacroAssembler::allocate_object(
180   Register obj,                        // result: pointer to object after successful allocation
181   Register t1,                         // temp register
182   Register t2,                         // temp register
183   Register t3,                         // temp register
184   int      hdr_size,                   // object header size in words
185   int      obj_size,                   // object size in words
186   Register klass,                      // object klass
187   Label&   slow_case                   // continuation point if fast allocation fails
188 ) {
189   assert_different_registers(obj, t1, t2, t3, klass);
190 
191   // allocate space & initialize header
192   if (!is_simm16(obj_size * wordSize)) {
193     // Would need to use extra register to load
194     // object size => go the slow case for now.
195     b(slow_case);
196     return;
197   }
198   try_allocate(obj, noreg, obj_size * wordSize, t2, t3, slow_case);
199 
200   initialize_object(obj, klass, noreg, obj_size * HeapWordSize, t1, t2);
201 }
202 
203 void C1_MacroAssembler::initialize_object(
204   Register obj,                        // result: pointer to object after successful allocation
205   Register klass,                      // object klass
206   Register var_size_in_bytes,          // object size in bytes if unknown at compile time; invalid otherwise
207   int      con_size_in_bytes,          // object size in bytes if   known at compile time
208   Register t1,                         // temp register
209   Register t2                          // temp register
210   ) {
211   const int hdr_size_in_bytes = instanceOopDesc::header_size() * HeapWordSize;
212 
213   initialize_header(obj, klass, noreg, t1, t2);
214 
215 #ifdef ASSERT
216   {
217     lwz(t1, in_bytes(Klass::layout_helper_offset()), klass);
218     if (var_size_in_bytes != noreg) {
219       cmpw(CR0, t1, var_size_in_bytes);
220     } else {
221       cmpwi(CR0, t1, con_size_in_bytes);
222     }
223     asm_assert_eq("bad size in initialize_object");
224   }
225 #endif
226 
227   // Initialize body.
228   if (var_size_in_bytes != noreg) {
229     // Use a loop.
230     addi(t1, obj, hdr_size_in_bytes);                // Compute address of first element.
231     addi(t2, var_size_in_bytes, -hdr_size_in_bytes); // Compute size of body.
232     initialize_body(t1, t2);
233   } else if (con_size_in_bytes > hdr_size_in_bytes) {
234     // Use a loop.
235     initialize_body(obj, t1, t2, con_size_in_bytes, hdr_size_in_bytes);
236   }
237 
238   verify_oop(obj, FILE_AND_LINE);
239 }
240 
241 
242 void C1_MacroAssembler::allocate_array(
243   Register obj,                        // result: pointer to array after successful allocation
244   Register len,                        // array length
245   Register t1,                         // temp register
246   Register t2,                         // temp register
247   Register t3,                         // temp register
248   int      base_offset_in_bytes,       // elements offset in bytes
249   int      elt_size,                   // element size in bytes
250   Register klass,                      // object klass
251   Label&   slow_case,                  // continuation point if fast allocation fails
252   bool     zero_array                  // zero the allocated array or not
253 ) {
254   assert_different_registers(obj, len, t1, t2, t3, klass);
255 
256   // Determine alignment mask.
257   assert(!(BytesPerWord & 1), "must be a multiple of 2 for masking code to work");
258   int log2_elt_size = exact_log2(elt_size);
259 
260   // Check for negative or excessive length.
261   size_t max_length = max_array_allocation_length >> log2_elt_size;
262   if (UseTLAB) {
263     size_t max_tlab = align_up(ThreadLocalAllocBuffer::max_size() >> log2_elt_size, 64*K);
264     if (max_tlab < max_length) { max_length = max_tlab; }
265   }
266   load_const_optimized(t1, max_length);
267   cmpld(CR0, len, t1);
268   bc_far_optimized(Assembler::bcondCRbiIs1, bi0(CR0, Assembler::greater), slow_case);
269 
270   // compute array size
271   // note: If 0 <= len <= max_length, len*elt_size + header + alignment is
272   //       smaller or equal to the largest integer; also, since top is always
273   //       aligned, we can do the alignment here instead of at the end address
274   //       computation.
275   const Register arr_size = t1;
276   Register arr_len_in_bytes = len;
277   if (elt_size != 1) {
278     sldi(t1, len, log2_elt_size);
279     arr_len_in_bytes = t1;
280   }
281   addi(arr_size, arr_len_in_bytes, base_offset_in_bytes + MinObjAlignmentInBytesMask); // Add space for header & alignment.
282   clrrdi(arr_size, arr_size, LogMinObjAlignmentInBytes);                              // Align array size.
283 
284   // Allocate space & initialize header.
285   try_allocate(obj, arr_size, 0, t2, t3, slow_case);
286   initialize_header(obj, klass, len, t2, t3);
287 
288   if (zero_array) {
289     // Initialize body.
290     const Register base  = t2;
291     const Register index = t3;
292     addi(base, obj, base_offset_in_bytes);               // compute address of first element
293     addi(index, arr_size, -(base_offset_in_bytes));      // compute index = number of bytes to clear
294 
295     // Zero first 4 bytes, if start offset is not word aligned.
296     if (!is_aligned(base_offset_in_bytes, BytesPerWord)) {
297       assert(is_aligned(base_offset_in_bytes, BytesPerInt), "must be 4-byte aligned");
298       li(t1, 0);
299       stw(t1, 0, base);
300       addi(base, base, BytesPerInt);
301       // Note: initialize_body will align index down, no need to correct it here.
302     }
303 
304     initialize_body(base, index);
305   }
306 
307   verify_oop(obj, FILE_AND_LINE);
308 }
309 
310 
311 #ifndef PRODUCT
312 
313 void C1_MacroAssembler::verify_stack_oop(int stack_offset) {
314   verify_oop_addr((RegisterOrConstant)stack_offset, R1_SP, "broken oop in stack slot");
315 }
316 
317 void C1_MacroAssembler::verify_not_null_oop(Register r) {
318   Label not_null;
319   cmpdi(CR0, r, 0);
320   bne(CR0, not_null);
321   stop("non-null oop required");
322   bind(not_null);
323   verify_oop(r, FILE_AND_LINE);
324 }
325 
326 #endif // PRODUCT
327 
328 void C1_MacroAssembler::null_check(Register r, Label* Lnull) {
329   if (TrapBasedNullChecks) { // SIGTRAP based
330     trap_null_check(r);
331   } else { // explicit
332     //const address exception_entry = Runtime1::entry_for(StubId::c1_throw_null_pointer_exception_id);
333     assert(Lnull != nullptr, "must have Label for explicit check");
334     cmpdi(CR0, r, 0);
335     bc_far_optimized(Assembler::bcondCRbiIs1, bi0(CR0, Assembler::equal), *Lnull);
336   }
337 }
338 
339 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) {
340   Unimplemented();
341 }
342