1 /*
  2  * Copyright (c) 2008, 2026, Oracle and/or its affiliates. All rights reserved.
  3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  4  *
  5  * This code is free software; you can redistribute it and/or modify it
  6  * under the terms of the GNU General Public License version 2 only, as
  7  * published by the Free Software Foundation.
  8  *
  9  * This code is distributed in the hope that it will be useful, but WITHOUT
 10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 12  * version 2 for more details (a copy is included in the LICENSE file that
 13  * accompanied this code).
 14  *
 15  * You should have received a copy of the GNU General Public License version
 16  * 2 along with this work; if not, write to the Free Software Foundation,
 17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 18  *
 19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 20  * or visit www.oracle.com if you need additional information or have any
 21  * questions.
 22  *
 23  */
 24 
 25 #include "c1/c1_MacroAssembler.hpp"
 26 #include "c1/c1_Runtime1.hpp"
 27 #include "gc/shared/barrierSet.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 #include "utilities/powerOfTwo.hpp"
 39 
 40 // Note: Rtemp usage is this file should not impact C2 and should be
 41 // correct as long as it is not implicitly used in lower layers (the
 42 // arm [macro]assembler) and used with care in the other C1 specific
 43 // files.
 44 
 45 void C1_MacroAssembler::build_frame(int frame_size_in_bytes, int bang_size_in_bytes,
 46                                     int sp_offset_for_orig_pc,
 47                                     bool needs_stack_repair, bool has_scalarized_args,
 48                                     Label* verified_inline_entry_label) {
 49   assert(bang_size_in_bytes >= frame_size_in_bytes, "stack bang size incorrect");
 50   assert((frame_size_in_bytes % StackAlignmentInBytes) == 0, "frame size should be aligned");
 51 
 52 
 53   arm_stack_overflow_check(bang_size_in_bytes, Rtemp);
 54 
 55   // FP can no longer be used to memorize SP. It may be modified
 56   // if this method contains a methodHandle call site
 57   raw_push(FP, LR);
 58   sub_slow(SP, SP, frame_size_in_bytes);
 59 
 60   // Insert nmethod entry barrier into frame.
 61   BarrierSetAssembler* bs = BarrierSet::barrier_set()->barrier_set_assembler();
 62   bs->nmethod_entry_barrier(this);
 63 }
 64 
 65 void C1_MacroAssembler::verified_entry(bool breakAtEntry) {
 66   if (breakAtEntry) {
 67     breakpoint();
 68   }
 69 }
 70 
 71 // Puts address of allocated object into register `obj` and end of allocated object into register `obj_end`.
 72 void C1_MacroAssembler::try_allocate(Register obj, Register obj_end, Register tmp1, Register tmp2,
 73                                      RegisterOrConstant size_expression, Label& slow_case) {
 74   if (UseTLAB) {
 75     tlab_allocate(obj, obj_end, tmp1, size_expression, slow_case);
 76   } else {
 77     b(slow_case);
 78   }
 79 }
 80 
 81 
 82 void C1_MacroAssembler::initialize_header(Register obj, Register klass, Register len, Register tmp) {
 83   assert_different_registers(obj, klass, len, tmp);
 84 
 85   mov(tmp, (intptr_t)markWord::prototype().value());
 86 
 87   str(tmp, Address(obj, oopDesc::mark_offset_in_bytes()));
 88   str(klass, Address(obj, oopDesc::klass_offset_in_bytes()));
 89 
 90   if (len->is_valid()) {
 91     str_32(len, Address(obj, arrayOopDesc::length_offset_in_bytes()));
 92   }
 93 }
 94 
 95 
 96 // Cleans object body [base..obj_end]. Clobbers `base` and `tmp` registers.
 97 void C1_MacroAssembler::initialize_body(Register base, Register obj_end, Register tmp) {
 98   zero_memory(base, obj_end, tmp);
 99 }
100 
101 
102 void C1_MacroAssembler::initialize_object(Register obj, Register obj_end, Register klass,
103                                           Register len, Register tmp1, Register tmp2,
104                                           RegisterOrConstant header_size, int obj_size_in_bytes,
105                                           bool is_tlab_allocated)
106 {
107   assert_different_registers(obj, obj_end, klass, len, tmp1, tmp2);
108   initialize_header(obj, klass, len, tmp1);
109 
110   const Register ptr = tmp2;
111 
112   if (!(UseTLAB && ZeroTLAB && is_tlab_allocated)) {
113     if (obj_size_in_bytes >= 0 && obj_size_in_bytes <= 8 * BytesPerWord) {
114       mov(tmp1, 0);
115       const int base = instanceOopDesc::header_size() * HeapWordSize;
116       for (int i = base; i < obj_size_in_bytes; i += wordSize) {
117         str(tmp1, Address(obj, i));
118       }
119     } else {
120       assert(header_size.is_constant() || header_size.as_register() == ptr, "code assumption");
121       add(ptr, obj, header_size);
122       initialize_body(ptr, obj_end, tmp1);
123     }
124   }
125 
126   // StoreStore barrier required after complete initialization
127   // (headers + content zeroing), before the object may escape.
128   membar(MacroAssembler::StoreStore, tmp1);
129 }
130 
131 void C1_MacroAssembler::allocate_object(Register obj, Register tmp1, Register tmp2, Register tmp3,
132                                         int header_size, int object_size,
133                                         Register klass, Label& slow_case) {
134   assert_different_registers(obj, tmp1, tmp2, tmp3, klass, Rtemp);
135   assert(header_size >= 0 && object_size >= header_size, "illegal sizes");
136   const int object_size_in_bytes = object_size * BytesPerWord;
137 
138   const Register obj_end = tmp1;
139   const Register len = noreg;
140 
141   if (Assembler::is_arith_imm_in_range(object_size_in_bytes)) {
142     try_allocate(obj, obj_end, tmp2, tmp3, object_size_in_bytes, slow_case);
143   } else {
144     // Rtemp should be free at c1 LIR level
145     mov_slow(Rtemp, object_size_in_bytes);
146     try_allocate(obj, obj_end, tmp2, tmp3, Rtemp, slow_case);
147   }
148   initialize_object(obj, obj_end, klass, len, tmp2, tmp3, instanceOopDesc::header_size() * HeapWordSize, object_size_in_bytes, /* is_tlab_allocated */ UseTLAB);
149 }
150 
151 void C1_MacroAssembler::allocate_array(Register obj, Register len,
152                                        Register tmp1, Register tmp2, Register tmp3,
153                                        int header_size_in_bytes, int element_size,
154                                        Register klass, Label& slow_case) {
155   assert_different_registers(obj, len, tmp1, tmp2, tmp3, klass, Rtemp);
156   const int scale_shift = exact_log2(element_size);
157   const Register obj_size = Rtemp; // Rtemp should be free at c1 LIR level
158 
159   cmp_32(len, max_array_allocation_length);
160   b(slow_case, hs);
161 
162   bool align_header = ((header_size_in_bytes | element_size) & MinObjAlignmentInBytesMask) != 0;
163   assert(align_header || ((header_size_in_bytes & MinObjAlignmentInBytesMask) == 0), "must be");
164   assert(align_header || ((element_size & MinObjAlignmentInBytesMask) == 0), "must be");
165 
166   mov(obj_size, header_size_in_bytes + (align_header ? (MinObjAlignmentInBytes - 1) : 0));
167   add_ptr_scaled_int32(obj_size, obj_size, len, scale_shift);
168 
169   if (align_header) {
170     align_reg(obj_size, obj_size, MinObjAlignmentInBytes);
171   }
172 
173   try_allocate(obj, tmp1, tmp2, tmp3, obj_size, slow_case);
174   initialize_object(obj, tmp1, klass, len, tmp2, tmp3, header_size_in_bytes, -1, /* is_tlab_allocated */ UseTLAB);
175 }
176 
177 int C1_MacroAssembler::lock_object(Register hdr, Register obj, Register basic_lock, Label& slow_case) {
178   int null_check_offset = 0;
179 
180   const Register tmp2 = Rtemp; // Rtemp should be free at c1 LIR level
181   assert_different_registers(hdr, obj, basic_lock, tmp2);
182 
183   assert(BasicObjectLock::lock_offset() == 0, "adjust this code");
184   assert(oopDesc::mark_offset_in_bytes() == 0, "Required by atomic instructions");
185 
186   // save object being locked into the BasicObjectLock
187   str(obj, Address(basic_lock, BasicObjectLock::obj_offset()));
188 
189   null_check_offset = offset();
190 
191   if (DiagnoseSyncOnValueBasedClasses != 0) {
192     load_klass(tmp2, obj);
193     ldrb(tmp2, Address(tmp2, Klass::misc_flags_offset()));
194     tst(tmp2, KlassFlags::_misc_is_value_based_class);
195     b(slow_case, ne);
196   }
197 
198   Register t1 = basic_lock; // Needs saving, probably
199   Register t2 = hdr;        // blow
200   Register t3 = Rtemp;      // blow
201 
202   fast_lock(obj, t1, t2, t3, 1 /* savemask - save t1 */, slow_case);
203   // Success: fall through
204   return null_check_offset;
205 }
206 
207 void C1_MacroAssembler::unlock_object(Register hdr, Register obj, Register basic_lock, Label& slow_case) {
208   assert_different_registers(hdr, obj, basic_lock, Rtemp);
209 
210   assert(BasicObjectLock::lock_offset() == 0, "adjust this code");
211   assert(oopDesc::mark_offset_in_bytes() == 0, "Required by atomic instructions");
212 
213   ldr(obj, Address(basic_lock, BasicObjectLock::obj_offset()));
214 
215   Register t1 = basic_lock; // Needs saving, probably
216   Register t2 = hdr;        // blow
217   Register t3 = Rtemp;      // blow
218 
219   fast_unlock(obj, t1, t2, t3, 1 /* savemask - save t1 */, slow_case);
220   // Success: fall through
221 }
222 
223 #ifndef PRODUCT
224 
225 void C1_MacroAssembler::verify_stack_oop(int stack_offset) {
226   if (!VerifyOops) return;
227   verify_oop_addr(Address(SP, stack_offset));
228 }
229 
230 void C1_MacroAssembler::verify_not_null_oop(Register r) {
231   Label not_null;
232   cbnz(r, not_null);
233   stop("non-null oop required");
234   bind(not_null);
235   if (!VerifyOops) return;
236   verify_oop(r);
237 }
238 
239 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) {
240   Unimplemented();
241 }
242 
243 #endif // !PRODUCT