1 /*
  2  * Copyright (c) 2008, 2025, 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   assert(bang_size_in_bytes >= frame_size_in_bytes, "stack bang size incorrect");
 47   assert((frame_size_in_bytes % StackAlignmentInBytes) == 0, "frame size should be aligned");
 48 
 49 
 50   arm_stack_overflow_check(bang_size_in_bytes, Rtemp);
 51 
 52   // FP can no longer be used to memorize SP. It may be modified
 53   // if this method contains a methodHandle call site
 54   raw_push(FP, LR);
 55   sub_slow(SP, SP, frame_size_in_bytes);
 56 
 57   // Insert nmethod entry barrier into frame.
 58   BarrierSetAssembler* bs = BarrierSet::barrier_set()->barrier_set_assembler();
 59   bs->nmethod_entry_barrier(this);
 60 }
 61 
 62 void C1_MacroAssembler::remove_frame(int frame_size_in_bytes) {
 63   add_slow(SP, SP, frame_size_in_bytes);
 64   raw_pop(FP, LR);
 65 }
 66 
 67 void C1_MacroAssembler::verified_entry(bool breakAtEntry) {
 68   if (breakAtEntry) {
 69     breakpoint();
 70   }
 71 }
 72 
 73 // Puts address of allocated object into register `obj` and end of allocated object into register `obj_end`.
 74 void C1_MacroAssembler::try_allocate(Register obj, Register obj_end, Register tmp1, Register tmp2,
 75                                      RegisterOrConstant size_expression, Label& slow_case) {
 76   if (UseTLAB) {
 77     tlab_allocate(obj, obj_end, tmp1, size_expression, slow_case);
 78   } else {
 79     b(slow_case);
 80   }
 81 }
 82 
 83 
 84 void C1_MacroAssembler::initialize_header(Register obj, Register klass, Register len, Register tmp) {
 85   assert_different_registers(obj, klass, len, tmp);
 86 
 87   mov(tmp, (intptr_t)markWord::prototype().value());
 88 
 89   str(tmp, Address(obj, oopDesc::mark_offset_in_bytes()));
 90   str(klass, Address(obj, oopDesc::klass_offset_in_bytes()));
 91 
 92   if (len->is_valid()) {
 93     str_32(len, Address(obj, arrayOopDesc::length_offset_in_bytes()));
 94   }
 95 }
 96 
 97 
 98 // Cleans object body [base..obj_end]. Clobbers `base` and `tmp` registers.
 99 void C1_MacroAssembler::initialize_body(Register base, Register obj_end, Register tmp) {
100   zero_memory(base, obj_end, tmp);
101 }
102 
103 
104 void C1_MacroAssembler::initialize_object(Register obj, Register obj_end, Register klass,
105                                           Register len, Register tmp1, Register tmp2,
106                                           RegisterOrConstant header_size, int obj_size_in_bytes,
107                                           bool is_tlab_allocated)
108 {
109   assert_different_registers(obj, obj_end, klass, len, tmp1, tmp2);
110   initialize_header(obj, klass, len, tmp1);
111 
112   const Register ptr = tmp2;
113 
114   if (!(UseTLAB && ZeroTLAB && is_tlab_allocated)) {
115     if (obj_size_in_bytes >= 0 && obj_size_in_bytes <= 8 * BytesPerWord) {
116       mov(tmp1, 0);
117       const int base = instanceOopDesc::header_size() * HeapWordSize;
118       for (int i = base; i < obj_size_in_bytes; i += wordSize) {
119         str(tmp1, Address(obj, i));
120       }
121     } else {
122       assert(header_size.is_constant() || header_size.as_register() == ptr, "code assumption");
123       add(ptr, obj, header_size);
124       initialize_body(ptr, obj_end, tmp1);
125     }
126   }
127 
128   // StoreStore barrier required after complete initialization
129   // (headers + content zeroing), before the object may escape.
130   membar(MacroAssembler::StoreStore, tmp1);
131 }
132 
133 void C1_MacroAssembler::allocate_object(Register obj, Register tmp1, Register tmp2, Register tmp3,
134                                         int header_size, int object_size,
135                                         Register klass, Label& slow_case) {
136   assert_different_registers(obj, tmp1, tmp2, tmp3, klass, Rtemp);
137   assert(header_size >= 0 && object_size >= header_size, "illegal sizes");
138   const int object_size_in_bytes = object_size * BytesPerWord;
139 
140   const Register obj_end = tmp1;
141   const Register len = noreg;
142 
143   if (Assembler::is_arith_imm_in_range(object_size_in_bytes)) {
144     try_allocate(obj, obj_end, tmp2, tmp3, object_size_in_bytes, slow_case);
145   } else {
146     // Rtemp should be free at c1 LIR level
147     mov_slow(Rtemp, object_size_in_bytes);
148     try_allocate(obj, obj_end, tmp2, tmp3, Rtemp, slow_case);
149   }
150   initialize_object(obj, obj_end, klass, len, tmp2, tmp3, instanceOopDesc::header_size() * HeapWordSize, object_size_in_bytes, /* is_tlab_allocated */ UseTLAB);
151 }
152 
153 void C1_MacroAssembler::allocate_array(Register obj, Register len,
154                                        Register tmp1, Register tmp2, Register tmp3,
155                                        int header_size_in_bytes, int element_size,
156                                        Register klass, Label& slow_case) {
157   assert_different_registers(obj, len, tmp1, tmp2, tmp3, klass, Rtemp);
158   const int scale_shift = exact_log2(element_size);
159   const Register obj_size = Rtemp; // Rtemp should be free at c1 LIR level
160 
161   cmp_32(len, max_array_allocation_length);
162   b(slow_case, hs);
163 
164   bool align_header = ((header_size_in_bytes | element_size) & MinObjAlignmentInBytesMask) != 0;
165   assert(align_header || ((header_size_in_bytes & MinObjAlignmentInBytesMask) == 0), "must be");
166   assert(align_header || ((element_size & MinObjAlignmentInBytesMask) == 0), "must be");
167 
168   mov(obj_size, header_size_in_bytes + (align_header ? (MinObjAlignmentInBytes - 1) : 0));
169   add_ptr_scaled_int32(obj_size, obj_size, len, scale_shift);
170 
171   if (align_header) {
172     align_reg(obj_size, obj_size, MinObjAlignmentInBytes);
173   }
174 
175   try_allocate(obj, tmp1, tmp2, tmp3, obj_size, slow_case);
176   initialize_object(obj, tmp1, klass, len, tmp2, tmp3, header_size_in_bytes, -1, /* is_tlab_allocated */ UseTLAB);
177 }
178 
179 int C1_MacroAssembler::lock_object(Register hdr, Register obj, Register basic_lock, Label& slow_case) {
180   int null_check_offset = 0;
181 
182   const Register tmp2 = Rtemp; // Rtemp should be free at c1 LIR level
183   assert_different_registers(hdr, obj, basic_lock, tmp2);
184 
185   assert(BasicObjectLock::lock_offset() == 0, "adjust this code");
186   assert(oopDesc::mark_offset_in_bytes() == 0, "Required by atomic instructions");
187 
188   // save object being locked into the BasicObjectLock
189   str(obj, Address(basic_lock, BasicObjectLock::obj_offset()));
190 
191   null_check_offset = offset();
192 
193   if (DiagnoseSyncOnValueBasedClasses != 0) {
194     load_klass(tmp2, obj);
195     ldrb(tmp2, Address(tmp2, Klass::misc_flags_offset()));
196     tst(tmp2, KlassFlags::_misc_is_value_based_class);
197     b(slow_case, ne);
198   }
199 
200   Register t1 = basic_lock; // Needs saving, probably
201   Register t2 = hdr;        // blow
202   Register t3 = Rtemp;      // blow
203 
204   fast_lock(obj, t1, t2, t3, 1 /* savemask - save t1 */, slow_case);
205   // Success: fall through
206   return null_check_offset;
207 }
208 
209 void C1_MacroAssembler::unlock_object(Register hdr, Register obj, Register basic_lock, Label& slow_case) {
210   assert_different_registers(hdr, obj, basic_lock, Rtemp);
211 
212   assert(BasicObjectLock::lock_offset() == 0, "adjust this code");
213   assert(oopDesc::mark_offset_in_bytes() == 0, "Required by atomic instructions");
214 
215   ldr(obj, Address(basic_lock, BasicObjectLock::obj_offset()));
216 
217   Register t1 = basic_lock; // Needs saving, probably
218   Register t2 = hdr;        // blow
219   Register t3 = Rtemp;      // blow
220 
221   fast_unlock(obj, t1, t2, t3, 1 /* savemask - save t1 */, slow_case);
222   // Success: fall through
223 }
224 
225 #ifndef PRODUCT
226 
227 void C1_MacroAssembler::verify_stack_oop(int stack_offset) {
228   if (!VerifyOops) return;
229   verify_oop_addr(Address(SP, stack_offset));
230 }
231 
232 void C1_MacroAssembler::verify_not_null_oop(Register r) {
233   Label not_null;
234   cbnz(r, not_null);
235   stop("non-null oop required");
236   bind(not_null);
237   if (!VerifyOops) return;
238   verify_oop(r);
239 }
240 
241 #endif // !PRODUCT