1 /* 2 * Copyright (c) 1999, 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 "code/compiledIC.hpp" 28 #include "compiler/compilerDefinitions.inline.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/frame.inline.hpp" 38 #include "runtime/globals.hpp" 39 #include "runtime/os.hpp" 40 #include "runtime/sharedRuntime.hpp" 41 #include "runtime/stubRoutines.hpp" 42 #include "utilities/checkedCast.hpp" 43 #include "utilities/globalDefinitions.hpp" 44 45 int C1_MacroAssembler::lock_object(Register hdr, Register obj, Register disp_hdr, Register tmp, Label& slow_case) { 46 assert(hdr == rax, "hdr must be rax, for the cmpxchg instruction"); 47 assert_different_registers(hdr, obj, disp_hdr, tmp); 48 int null_check_offset = -1; 49 50 verify_oop(obj); 51 52 // save object being locked into the BasicObjectLock 53 movptr(Address(disp_hdr, BasicObjectLock::obj_offset()), obj); 54 55 null_check_offset = offset(); 56 57 lightweight_lock(disp_hdr, obj, hdr, tmp, slow_case); 58 59 return null_check_offset; 60 } 61 62 void C1_MacroAssembler::unlock_object(Register hdr, Register obj, Register disp_hdr, Label& slow_case) { 63 assert(disp_hdr == rax, "disp_hdr must be rax, for the cmpxchg instruction"); 64 assert(hdr != obj && hdr != disp_hdr && obj != disp_hdr, "registers must be different"); 65 66 // load object 67 movptr(obj, Address(disp_hdr, BasicObjectLock::obj_offset())); 68 verify_oop(obj); 69 70 lightweight_unlock(obj, disp_hdr, hdr, slow_case); 71 } 72 73 74 // Defines obj, preserves var_size_in_bytes 75 void C1_MacroAssembler::try_allocate(Register obj, Register var_size_in_bytes, int con_size_in_bytes, Register t1, Register t2, Label& slow_case) { 76 if (UseTLAB) { 77 tlab_allocate(obj, var_size_in_bytes, con_size_in_bytes, t1, t2, slow_case); 78 } else { 79 jmp(slow_case); 80 } 81 } 82 83 84 void C1_MacroAssembler::initialize_header(Register obj, Register klass, Register len, Register t1, Register t2) { 85 assert_different_registers(obj, klass, len, t1, t2); 86 if (UseCompactObjectHeaders || EnableValhalla) { 87 // COH: Markword contains class pointer which is only known at runtime. 88 // Valhalla: Could have value class which has a different prototype header to a normal object. 89 // In both cases, we need to fetch dynamically. 90 movptr(t1, Address(klass, Klass::prototype_header_offset())); 91 movptr(Address(obj, oopDesc::mark_offset_in_bytes()), t1); 92 } else { 93 // Otherwise: Can use the statically computed prototype header which is the same for every object. 94 movptr(Address(obj, oopDesc::mark_offset_in_bytes()), checked_cast<int32_t>(markWord::prototype().value())); 95 } 96 if (!UseCompactObjectHeaders) { 97 // COH: Markword already contains class pointer. Nothing else to do. 98 // Otherwise: Fetch klass pointer following the markword 99 if (UseCompressedClassPointers) { // Take care not to kill klass 100 movptr(t1, klass); 101 encode_klass_not_null(t1, rscratch1); 102 movl(Address(obj, oopDesc::klass_offset_in_bytes()), t1); 103 } else { 104 movptr(Address(obj, oopDesc::klass_offset_in_bytes()), klass); 105 } 106 } 107 108 if (len->is_valid()) { 109 movl(Address(obj, arrayOopDesc::length_offset_in_bytes()), len); 110 int base_offset = arrayOopDesc::length_offset_in_bytes() + BytesPerInt; 111 if (!is_aligned(base_offset, BytesPerWord)) { 112 assert(is_aligned(base_offset, BytesPerInt), "must be 4-byte aligned"); 113 // Clear gap/first 4 bytes following the length field. 114 xorl(t1, t1); 115 movl(Address(obj, base_offset), t1); 116 } 117 } else if (UseCompressedClassPointers && !UseCompactObjectHeaders) { 118 xorptr(t1, t1); 119 store_klass_gap(obj, t1); 120 } 121 } 122 123 124 // preserves obj, destroys len_in_bytes 125 void C1_MacroAssembler::initialize_body(Register obj, Register len_in_bytes, int hdr_size_in_bytes, Register t1) { 126 assert(hdr_size_in_bytes >= 0, "header size must be positive or 0"); 127 Label done; 128 129 // len_in_bytes is positive and ptr sized 130 subptr(len_in_bytes, hdr_size_in_bytes); 131 zero_memory(obj, len_in_bytes, hdr_size_in_bytes, t1); 132 bind(done); 133 } 134 135 136 void C1_MacroAssembler::allocate_object(Register obj, Register t1, Register t2, int header_size, int object_size, Register klass, Label& slow_case) { 137 assert(obj == rax, "obj must be in rax, for cmpxchg"); 138 assert_different_registers(obj, t1, t2); // XXX really? 139 assert(header_size >= 0 && object_size >= header_size, "illegal sizes"); 140 141 try_allocate(obj, noreg, object_size * BytesPerWord, t1, t2, slow_case); 142 143 initialize_object(obj, klass, noreg, object_size * HeapWordSize, t1, t2, UseTLAB); 144 } 145 146 void C1_MacroAssembler::initialize_object(Register obj, Register klass, Register var_size_in_bytes, int con_size_in_bytes, Register t1, Register t2, bool is_tlab_allocated) { 147 assert((con_size_in_bytes & MinObjAlignmentInBytesMask) == 0, 148 "con_size_in_bytes is not multiple of alignment"); 149 const int hdr_size_in_bytes = instanceOopDesc::header_size() * HeapWordSize; 150 if (UseCompactObjectHeaders) { 151 assert(hdr_size_in_bytes == 8, "check object headers size"); 152 } 153 initialize_header(obj, klass, noreg, t1, t2); 154 155 if (!(UseTLAB && ZeroTLAB && is_tlab_allocated)) { 156 // clear rest of allocated space 157 const Register t1_zero = t1; 158 const Register index = t2; 159 const int threshold = 6 * BytesPerWord; // approximate break even point for code size (see comments below) 160 if (var_size_in_bytes != noreg) { 161 mov(index, var_size_in_bytes); 162 initialize_body(obj, index, hdr_size_in_bytes, t1_zero); 163 } else if (con_size_in_bytes <= threshold) { 164 // use explicit null stores 165 // code size = 2 + 3*n bytes (n = number of fields to clear) 166 xorptr(t1_zero, t1_zero); // use t1_zero reg to clear memory (shorter code) 167 for (int i = hdr_size_in_bytes; i < con_size_in_bytes; i += BytesPerWord) 168 movptr(Address(obj, i), t1_zero); 169 } else if (con_size_in_bytes > hdr_size_in_bytes) { 170 // use loop to null out the fields 171 // code size = 16 bytes for even n (n = number of fields to clear) 172 // initialize last object field first if odd number of fields 173 xorptr(t1_zero, t1_zero); // use t1_zero reg to clear memory (shorter code) 174 movptr(index, (con_size_in_bytes - hdr_size_in_bytes) >> 3); 175 // initialize last object field if constant size is odd 176 if (((con_size_in_bytes - hdr_size_in_bytes) & 4) != 0) 177 movptr(Address(obj, con_size_in_bytes - (1*BytesPerWord)), t1_zero); 178 // initialize remaining object fields: rdx is a multiple of 2 179 { Label loop; 180 bind(loop); 181 movptr(Address(obj, index, Address::times_8, hdr_size_in_bytes - (1*BytesPerWord)), 182 t1_zero); 183 decrement(index); 184 jcc(Assembler::notZero, loop); 185 } 186 } 187 } 188 189 if (CURRENT_ENV->dtrace_alloc_probes()) { 190 assert(obj == rax, "must be"); 191 call(RuntimeAddress(Runtime1::entry_for(StubId::c1_dtrace_object_alloc_id))); 192 } 193 194 verify_oop(obj); 195 } 196 197 void C1_MacroAssembler::allocate_array(Register obj, Register len, Register t1, Register t2, int base_offset_in_bytes, Address::ScaleFactor f, Register klass, Label& slow_case, bool zero_array) { 198 assert(obj == rax, "obj must be in rax, for cmpxchg"); 199 assert_different_registers(obj, len, t1, t2, klass); 200 201 // determine alignment mask 202 assert(!(BytesPerWord & 1), "must be a multiple of 2 for masking code to work"); 203 204 // check for negative or excessive length 205 cmpptr(len, checked_cast<int32_t>(max_array_allocation_length)); 206 jcc(Assembler::above, slow_case); 207 208 const Register arr_size = t2; // okay to be the same 209 // align object end 210 movptr(arr_size, base_offset_in_bytes + MinObjAlignmentInBytesMask); 211 lea(arr_size, Address(arr_size, len, f)); 212 andptr(arr_size, ~MinObjAlignmentInBytesMask); 213 214 try_allocate(obj, arr_size, 0, t1, t2, slow_case); 215 216 initialize_header(obj, klass, len, t1, t2); 217 218 // clear rest of allocated space 219 if (zero_array) { 220 const Register len_zero = len; 221 // Align-up to word boundary, because we clear the 4 bytes potentially 222 // following the length field in initialize_header(). 223 int base_offset = align_up(base_offset_in_bytes, BytesPerWord); 224 initialize_body(obj, arr_size, base_offset, len_zero); 225 } 226 227 if (CURRENT_ENV->dtrace_alloc_probes()) { 228 assert(obj == rax, "must be"); 229 call(RuntimeAddress(Runtime1::entry_for(StubId::c1_dtrace_object_alloc_id))); 230 } 231 232 verify_oop(obj); 233 } 234 235 void C1_MacroAssembler::build_frame_helper(int frame_size_in_bytes, int sp_offset_for_orig_pc, int sp_inc, bool reset_orig_pc, bool needs_stack_repair) { 236 push(rbp); 237 if (PreserveFramePointer) { 238 mov(rbp, rsp); 239 } 240 decrement(rsp, frame_size_in_bytes); 241 242 if (needs_stack_repair) { 243 // Save stack increment (also account for fixed framesize and rbp) 244 assert((sp_inc & (StackAlignmentInBytes-1)) == 0, "stack increment not aligned"); 245 int real_frame_size = sp_inc + frame_size_in_bytes + wordSize; 246 movptr(Address(rsp, frame_size_in_bytes - wordSize), real_frame_size); 247 } 248 if (reset_orig_pc) { 249 // Zero orig_pc to detect deoptimization during buffering in the entry points 250 movptr(Address(rsp, sp_offset_for_orig_pc), 0); 251 } 252 } 253 254 void C1_MacroAssembler::build_frame(int frame_size_in_bytes, int bang_size_in_bytes, int sp_offset_for_orig_pc, bool needs_stack_repair, bool has_scalarized_args, Label* verified_inline_entry_label) { 255 // Make sure there is enough stack space for this method's activation. 256 // Note that we do this before doing an enter(). This matches the 257 // ordering of C2's stack overflow check / rsp decrement and allows 258 // the SharedRuntime stack overflow handling to be consistent 259 // between the two compilers. 260 assert(bang_size_in_bytes >= frame_size_in_bytes, "stack bang size incorrect"); 261 generate_stack_overflow_check(bang_size_in_bytes); 262 263 build_frame_helper(frame_size_in_bytes, sp_offset_for_orig_pc, 0, has_scalarized_args, needs_stack_repair); 264 265 BarrierSetAssembler* bs = BarrierSet::barrier_set()->barrier_set_assembler(); 266 // C1 code is not hot enough to micro optimize the nmethod entry barrier with an out-of-line stub 267 bs->nmethod_entry_barrier(this, nullptr /* slow_path */, nullptr /* continuation */); 268 269 if (verified_inline_entry_label != nullptr) { 270 // Jump here from the scalarized entry points that already created the frame. 271 bind(*verified_inline_entry_label); 272 } 273 } 274 275 void C1_MacroAssembler::verified_entry(bool breakAtEntry) { 276 if (breakAtEntry) int3(); 277 // build frame 278 } 279 280 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) { 281 assert(InlineTypePassFieldsAsArgs, "sanity"); 282 // Make sure there is enough stack space for this method's activation. 283 assert(bang_size_in_bytes >= frame_size_in_bytes, "stack bang size incorrect"); 284 generate_stack_overflow_check(bang_size_in_bytes); 285 286 GrowableArray<SigEntry>* sig = ces->sig(); 287 GrowableArray<SigEntry>* sig_cc = is_inline_ro_entry ? ces->sig_cc_ro() : ces->sig_cc(); 288 VMRegPair* regs = ces->regs(); 289 VMRegPair* regs_cc = is_inline_ro_entry ? ces->regs_cc_ro() : ces->regs_cc(); 290 int args_on_stack = ces->args_on_stack(); 291 int args_on_stack_cc = is_inline_ro_entry ? ces->args_on_stack_cc_ro() : ces->args_on_stack_cc(); 292 293 assert(sig->length() <= sig_cc->length(), "Zero-sized inline class not allowed!"); 294 BasicType* sig_bt = NEW_RESOURCE_ARRAY(BasicType, sig_cc->length()); 295 int args_passed = sig->length(); 296 int args_passed_cc = SigEntry::fill_sig_bt(sig_cc, sig_bt); 297 298 // Create a temp frame so we can call into the runtime. It must be properly set up to accommodate GC. 299 build_frame_helper(frame_size_in_bytes, sp_offset_for_orig_pc, 0, true, ces->c1_needs_stack_repair()); 300 301 // The runtime call might safepoint, make sure nmethod entry barrier is executed 302 BarrierSetAssembler* bs = BarrierSet::barrier_set()->barrier_set_assembler(); 303 // C1 code is not hot enough to micro optimize the nmethod entry barrier with an out-of-line stub 304 bs->nmethod_entry_barrier(this, nullptr /* slow_path */, nullptr /* continuation */); 305 306 // FIXME -- call runtime only if we cannot in-line allocate all the incoming inline type args. 307 movptr(rbx, (intptr_t)(ces->method())); 308 if (is_inline_ro_entry) { 309 call(RuntimeAddress(Runtime1::entry_for(StubId::c1_buffer_inline_args_no_receiver_id))); 310 } else { 311 call(RuntimeAddress(Runtime1::entry_for(StubId::c1_buffer_inline_args_id))); 312 } 313 int rt_call_offset = offset(); 314 315 // Remove the temp frame 316 addptr(rsp, frame_size_in_bytes); 317 pop(rbp); 318 319 // Check if we need to extend the stack for packing 320 int sp_inc = 0; 321 if (args_on_stack > args_on_stack_cc) { 322 sp_inc = extend_stack_for_inline_args(args_on_stack); 323 } 324 325 shuffle_inline_args(true, is_inline_ro_entry, sig_cc, 326 args_passed_cc, args_on_stack_cc, regs_cc, // from 327 args_passed, args_on_stack, regs, // to 328 sp_inc, rax); 329 330 // Create the real frame. Below jump will then skip over the stack banging and frame 331 // setup code in the verified_inline_entry (which has a different real_frame_size). 332 build_frame_helper(frame_size_in_bytes, sp_offset_for_orig_pc, sp_inc, false, ces->c1_needs_stack_repair()); 333 334 jmp(verified_inline_entry_label); 335 return rt_call_offset; 336 } 337 338 void C1_MacroAssembler::load_parameter(int offset_in_words, Register reg) { 339 // rbp, + 0: link 340 // + 1: return address 341 // + 2: argument with offset 0 342 // + 3: argument with offset 1 343 // + 4: ... 344 345 movptr(reg, Address(rbp, (offset_in_words + 2) * BytesPerWord)); 346 } 347 348 #ifndef PRODUCT 349 350 void C1_MacroAssembler::verify_stack_oop(int stack_offset) { 351 if (!VerifyOops) return; 352 verify_oop_addr(Address(rsp, stack_offset)); 353 } 354 355 void C1_MacroAssembler::verify_not_null_oop(Register r) { 356 if (!VerifyOops) return; 357 Label not_null; 358 testptr(r, r); 359 jcc(Assembler::notZero, not_null); 360 stop("non-null oop required"); 361 bind(not_null); 362 verify_oop(r); 363 } 364 365 void C1_MacroAssembler::invalidate_registers(bool inv_rax, bool inv_rbx, bool inv_rcx, bool inv_rdx, bool inv_rsi, bool inv_rdi) { 366 #ifdef ASSERT 367 if (inv_rax) movptr(rax, 0xDEAD); 368 if (inv_rbx) movptr(rbx, 0xDEAD); 369 if (inv_rcx) movptr(rcx, 0xDEAD); 370 if (inv_rdx) movptr(rdx, 0xDEAD); 371 if (inv_rsi) movptr(rsi, 0xDEAD); 372 if (inv_rdi) movptr(rdi, 0xDEAD); 373 #endif 374 } 375 376 #endif // ifndef PRODUCT