1 /*
  2  * Copyright (c) 2016, 2026, Oracle and/or its affiliates. All rights reserved.
  3  * Copyright (c) 2016, 2024 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/barrierSet.hpp"
 30 #include "gc/shared/barrierSetAssembler.hpp"
 31 #include "gc/shared/collectedHeap.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/basicLock.hpp"
 37 #include "runtime/os.hpp"
 38 #include "runtime/sharedRuntime.hpp"
 39 #include "runtime/stubRoutines.hpp"
 40 #include "utilities/macros.hpp"
 41 
 42 void C1_MacroAssembler::explicit_null_check(Register base) {
 43   ShouldNotCallThis(); // unused
 44 }
 45 
 46 void C1_MacroAssembler::build_frame(int frame_size_in_bytes, int bang_size_in_bytes,
 47                                     int sp_offset_for_orig_pc,
 48                                     bool needs_stack_repair, bool has_scalarized_args,
 49                                     Label* verified_inline_entry_label) {
 50   assert(bang_size_in_bytes >= frame_size_in_bytes, "stack bang size incorrect");
 51   generate_stack_overflow_check(bang_size_in_bytes);
 52   save_return_pc();
 53   push_frame(frame_size_in_bytes);
 54 
 55   BarrierSetAssembler* bs = BarrierSet::barrier_set()->barrier_set_assembler();
 56   bs->nmethod_entry_barrier(this);
 57 }
 58 
 59 void C1_MacroAssembler::verified_entry(bool breakAtEntry) {
 60   if (breakAtEntry) z_illtrap(0xC1);
 61 }
 62 
 63 void C1_MacroAssembler::lock_object(Register Rmark, Register Roop, Register Rbox, Label& slow_case) {
 64   const Register tmp   = Z_R1_scratch;
 65 
 66   assert_different_registers(Rmark, Roop, Rbox, tmp);
 67 
 68   verify_oop(Roop, FILE_AND_LINE);
 69 
 70   // Save object being locked into the BasicObjectLock...
 71   z_stg(Roop, Address(Rbox, BasicObjectLock::obj_offset()));
 72 
 73   fast_lock(Rbox, Roop, Rmark, tmp, slow_case);
 74 }
 75 
 76 void C1_MacroAssembler::unlock_object(Register Rmark, Register Roop, Register Rbox, Label& slow_case) {
 77   assert_different_registers(Rmark, Roop, Rbox);
 78 
 79   // Load object.
 80   z_lg(Roop, Address(Rbox, BasicObjectLock::obj_offset()));
 81   verify_oop(Roop, FILE_AND_LINE);
 82 
 83   fast_unlock(Roop, Rmark, Z_R1_scratch, slow_case);
 84 }
 85 
 86 void C1_MacroAssembler::try_allocate(
 87   Register obj,                        // result: Pointer to object after successful allocation.
 88   Register var_size_in_bytes,          // Object size in bytes if unknown at compile time; invalid otherwise.
 89   int      con_size_in_bytes,          // Object size in bytes if   known at compile time.
 90   Register t1,                         // Temp register.
 91   Label&   slow_case                   // Continuation point if fast allocation fails.
 92 ) {
 93   if (UseTLAB) {
 94     tlab_allocate(obj, var_size_in_bytes, con_size_in_bytes, t1, slow_case);
 95   } else {
 96     // Allocation in shared Eden not implemented, because sapjvm allocation trace does not allow it.
 97     z_brul(slow_case);
 98   }
 99 }
100 
101 void C1_MacroAssembler::initialize_header(Register obj, Register klass, Register len, Register Rzero, Register t1) {
102   assert_different_registers(obj, klass, len, t1, Rzero);
103   if (UseCompactObjectHeaders) {
104     z_lg(t1, Address(klass, in_bytes(Klass::prototype_header_offset())));
105     z_stg(t1, Address(obj, oopDesc::mark_offset_in_bytes()));
106   } else {
107     load_const_optimized(t1, (intx)markWord::prototype().value());
108     z_stg(t1, Address(obj, oopDesc::mark_offset_in_bytes()));
109     store_klass(klass, obj, t1);
110   }
111 
112   if (len->is_valid()) {
113     // Length will be in the klass gap.
114     z_st(len, Address(obj, arrayOopDesc::length_offset_in_bytes()));
115   } else if (!UseCompactObjectHeaders) {
116     store_klass_gap(Rzero, obj);  // Zero klass gap.
117   }
118 }
119 
120 void C1_MacroAssembler::initialize_body(Register objectFields, Register len_in_bytes, Register Rzero) {
121   assert_different_registers(objectFields, len_in_bytes, Rzero);
122 
123   // Initialize object fields.
124   // See documentation for MVCLE instruction!!!
125   assert(objectFields->encoding()%2==0, "objectFields must be an even register");
126   assert(len_in_bytes->encoding() == (objectFields->encoding()+1), "objectFields and len_in_bytes must be a register pair");
127   assert(Rzero->encoding()%2==1, "Rzero must be an odd register");
128 
129   // Use Rzero as src length, then mvcle will copy nothing
130   // and fill the object with the padding value 0.
131   move_long_ext(objectFields, as_Register(Rzero->encoding()-1), 0);
132 }
133 
134 void C1_MacroAssembler::allocate_object(
135   Register obj,                        // Result: pointer to object after successful allocation.
136   Register t1,                         // temp register
137   Register t2,                         // temp register: Must be a global register for try_allocate.
138   int      hdr_size,                   // object header size in words
139   int      obj_size,                   // object size in words
140   Register klass,                      // object klass
141   Label&   slow_case                   // Continuation point if fast allocation fails.
142 ) {
143   assert_different_registers(obj, t1, t2, klass);
144 
145   // Allocate space and initialize header.
146   try_allocate(obj, noreg, obj_size * wordSize, t1, slow_case);
147 
148   initialize_object(obj, klass, noreg, obj_size * HeapWordSize, t1, t2);
149 }
150 
151 void C1_MacroAssembler::initialize_object(
152   Register obj,                        // result: Pointer to object after successful allocation.
153   Register klass,                      // object klass
154   Register var_size_in_bytes,          // Object size in bytes if unknown at compile time; invalid otherwise.
155   int      con_size_in_bytes,          // Object size in bytes if   known at compile time.
156   Register t1,                         // temp register
157   Register t2                          // temp register
158  ) {
159   assert((con_size_in_bytes & MinObjAlignmentInBytesMask) == 0,
160          "con_size_in_bytes is not multiple of alignment");
161   assert(var_size_in_bytes == noreg, "not implemented");
162   const int hdr_size_in_bytes = instanceOopDesc::header_size() * HeapWordSize;
163 
164   const Register Rzero = t2;
165 
166   z_xgr(Rzero, Rzero);
167   initialize_header(obj, klass, noreg, Rzero, t1);
168 
169   // Clear rest of allocated space.
170   const int threshold = 4 * BytesPerWord;
171   if (con_size_in_bytes <= threshold) {
172     // Use explicit null stores.
173     // code size = 6*n bytes (n = number of fields to clear)
174     for (int i = hdr_size_in_bytes; i < con_size_in_bytes; i += BytesPerWord)
175       z_stg(Rzero, Address(obj, i));
176   } else {
177     // Code size generated by initialize_body() is 16.
178     Register object_fields = Z_R0_scratch;
179     Register len_in_bytes  = Z_R1_scratch;
180     z_la(object_fields, hdr_size_in_bytes, obj);
181     load_const_optimized(len_in_bytes, con_size_in_bytes - hdr_size_in_bytes);
182     initialize_body(object_fields, len_in_bytes, Rzero);
183   }
184 
185   // Dtrace support is unimplemented.
186   //  if (CURRENT_ENV->dtrace_alloc_probes()) {
187   //    assert(obj == rax, "must be");
188   //    call(RuntimeAddress(Runtime1::entry_for (StubId::c1_dtrace_object_alloc_id)));
189   //  }
190 
191   verify_oop(obj, FILE_AND_LINE);
192 }
193 
194 void C1_MacroAssembler::allocate_array(
195   Register obj,                        // result: Pointer to array after successful allocation.
196   Register len,                        // array length
197   Register t1,                         // temp register
198   Register t2,                         // temp register
199   int      base_offset_in_bytes,       // elements offset in bytes
200   int      elt_size,                   // element size in bytes
201   Register klass,                      // object klass
202   Label&   slow_case,                  // Continuation point if fast allocation fails.
203   bool     zero_array                  // zero the allocated array or not
204 ) {
205   assert_different_registers(obj, len, t1, t2, klass);
206 
207   // Determine alignment mask.
208   assert(!(BytesPerWord & 1), "must be a multiple of 2 for masking code to work");
209 
210   // Check for negative or excessive length.
211   compareU64_and_branch(len, (int32_t)max_array_allocation_length, bcondHigh, slow_case);
212 
213   // Compute array size.
214   // Note: If 0 <= len <= max_length, len*elt_size + header + alignment is
215   // smaller or equal to the largest integer. Also, since top is always
216   // aligned, we can do the alignment here instead of at the end address
217   // computation.
218   const Register arr_size = t2;
219   switch (elt_size) {
220     case  1: lgr_if_needed(arr_size, len); break;
221     case  2: z_sllg(arr_size, len, 1); break;
222     case  4: z_sllg(arr_size, len, 2); break;
223     case  8: z_sllg(arr_size, len, 3); break;
224     default: ShouldNotReachHere();
225   }
226   add2reg(arr_size, base_offset_in_bytes + MinObjAlignmentInBytesMask); // Add space for header & alignment.
227   z_nill(arr_size, (~MinObjAlignmentInBytesMask) & 0xffff);             // Align array size.
228 
229   try_allocate(obj, arr_size, 0, t1, slow_case);
230 
231   initialize_header(obj, klass, len, noreg, t1);
232 
233   // Clear rest of allocated space.
234   if (zero_array) {
235     Label done;
236     Register object_fields = t1;
237     Register Rzero = Z_R1_scratch;
238     z_aghi(arr_size, -base_offset_in_bytes);
239     z_bre(done); // Jump if size of fields is zero.
240     z_la(object_fields, base_offset_in_bytes, obj);
241     z_xgr(Rzero, Rzero);
242     initialize_body(object_fields, arr_size, Rzero);
243     bind(done);
244   }
245 
246   // Dtrace support is unimplemented.
247   // if (CURRENT_ENV->dtrace_alloc_probes()) {
248   //   assert(obj == rax, "must be");
249   //   call(RuntimeAddress(Runtime1::entry_for (StubId::c1_dtrace_object_alloc_id)));
250   // }
251 
252   verify_oop(obj, FILE_AND_LINE);
253 }
254 
255 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) {
256   Unimplemented();
257 }
258 
259 
260 
261 #ifndef PRODUCT
262 
263 void C1_MacroAssembler::verify_stack_oop(int stack_offset) {
264   if (!VerifyOops) return;
265   verify_oop_addr(Address(Z_SP, stack_offset), FILE_AND_LINE);
266 }
267 
268 void C1_MacroAssembler::verify_not_null_oop(Register r) {
269   if (!VerifyOops) return;
270   NearLabel not_null;
271   compareU64_and_branch(r, (intptr_t)0, bcondNotEqual, not_null);
272   stop("non-null oop required");
273   bind(not_null);
274   verify_oop(r, FILE_AND_LINE);
275 }
276 
277 void C1_MacroAssembler::invalidate_registers(Register preserve1,
278                                              Register preserve2,
279                                              Register preserve3) {
280   Register dead_value = noreg;
281   for (int i = 0; i < FrameMap::nof_cpu_regs; i++) {
282     Register r = as_Register(i);
283     if (r != preserve1 && r != preserve2 && r != preserve3 && r != Z_SP && r != Z_thread) {
284       if (dead_value == noreg) {
285         load_const_optimized(r, 0xc1dead);
286         dead_value = r;
287       } else {
288         z_lgr(r, dead_value);
289       }
290     }
291   }
292 }
293 
294 #endif // !PRODUCT