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