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