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/globals.hpp"
38 #include "runtime/os.hpp"
39 #include "runtime/sharedRuntime.hpp"
40 #include "runtime/stubRoutines.hpp"
41 #include "utilities/checkedCast.hpp"
42 #include "utilities/globalDefinitions.hpp"
43
44 int C1_MacroAssembler::lock_object(Register hdr, Register obj, Register basic_lock, Register tmp, Label& slow_case) {
45 assert(hdr == rax, "hdr must be rax, for the cmpxchg instruction");
46 assert_different_registers(hdr, obj, basic_lock, tmp);
47 int null_check_offset = -1;
48
49 verify_oop(obj);
50
51 // save object being locked into the BasicObjectLock
52 movptr(Address(basic_lock, BasicObjectLock::obj_offset()), obj);
53
54 null_check_offset = offset();
55
56 fast_lock(basic_lock, obj, hdr, tmp, slow_case);
65 // load object
66 movptr(obj, Address(basic_lock, BasicObjectLock::obj_offset()));
67 verify_oop(obj);
68
69 fast_unlock(obj, rax, hdr, slow_case);
70 }
71
72
73 // Defines obj, preserves var_size_in_bytes
74 void C1_MacroAssembler::try_allocate(Register obj, Register var_size_in_bytes, int con_size_in_bytes, Register t1, Register t2, Label& slow_case) {
75 if (UseTLAB) {
76 tlab_allocate(obj, var_size_in_bytes, con_size_in_bytes, t1, t2, slow_case);
77 } else {
78 jmp(slow_case);
79 }
80 }
81
82
83 void C1_MacroAssembler::initialize_header(Register obj, Register klass, Register len, Register t1, Register t2) {
84 assert_different_registers(obj, klass, len, t1, t2);
85 if (UseCompactObjectHeaders) {
86 movptr(t1, Address(klass, Klass::prototype_header_offset()));
87 movptr(Address(obj, oopDesc::mark_offset_in_bytes()), t1);
88 } else if (UseCompressedClassPointers) { // Take care not to kill klass
89 movptr(Address(obj, oopDesc::mark_offset_in_bytes()), checked_cast<int32_t>(markWord::prototype().value()));
90 movptr(t1, klass);
91 encode_klass_not_null(t1, rscratch1);
92 movl(Address(obj, oopDesc::klass_offset_in_bytes()), t1);
93 } else {
94 movptr(Address(obj, oopDesc::mark_offset_in_bytes()), checked_cast<int32_t>(markWord::prototype().value()));
95 movptr(Address(obj, oopDesc::klass_offset_in_bytes()), klass);
96 }
97
98 if (len->is_valid()) {
99 movl(Address(obj, arrayOopDesc::length_offset_in_bytes()), len);
100 int base_offset = arrayOopDesc::length_offset_in_bytes() + BytesPerInt;
101 if (!is_aligned(base_offset, BytesPerWord)) {
102 assert(is_aligned(base_offset, BytesPerInt), "must be 4-byte aligned");
103 // Clear gap/first 4 bytes following the length field.
104 xorl(t1, t1);
105 movl(Address(obj, base_offset), t1);
106 }
107 } else if (UseCompressedClassPointers && !UseCompactObjectHeaders) {
108 xorptr(t1, t1);
109 store_klass_gap(obj, t1);
110 }
111 }
112
113
114 // preserves obj, destroys len_in_bytes
115 void C1_MacroAssembler::initialize_body(Register obj, Register len_in_bytes, int hdr_size_in_bytes, Register t1) {
205
206 initialize_header(obj, klass, len, t1, t2);
207
208 // clear rest of allocated space
209 if (zero_array) {
210 const Register len_zero = len;
211 // Align-up to word boundary, because we clear the 4 bytes potentially
212 // following the length field in initialize_header().
213 int base_offset = align_up(base_offset_in_bytes, BytesPerWord);
214 initialize_body(obj, arr_size, base_offset, len_zero);
215 }
216
217 if (CURRENT_ENV->dtrace_alloc_probes()) {
218 assert(obj == rax, "must be");
219 call(RuntimeAddress(Runtime1::entry_for(StubId::c1_dtrace_object_alloc_id)));
220 }
221
222 verify_oop(obj);
223 }
224
225 void C1_MacroAssembler::build_frame(int frame_size_in_bytes, int bang_size_in_bytes) {
226 assert(bang_size_in_bytes >= frame_size_in_bytes, "stack bang size incorrect");
227 // Make sure there is enough stack space for this method's activation.
228 // Note that we do this before doing an enter(). This matches the
229 // ordering of C2's stack overflow check / rsp decrement and allows
230 // the SharedRuntime stack overflow handling to be consistent
231 // between the two compilers.
232 generate_stack_overflow_check(bang_size_in_bytes);
233
234 push(rbp);
235 if (PreserveFramePointer) {
236 mov(rbp, rsp);
237 }
238 decrement(rsp, frame_size_in_bytes); // does not emit code for frame_size == 0
239
240 BarrierSetAssembler* bs = BarrierSet::barrier_set()->barrier_set_assembler();
241 // C1 code is not hot enough to micro optimize the nmethod entry barrier with an out-of-line stub
242 bs->nmethod_entry_barrier(this, nullptr /* slow_path */, nullptr /* continuation */);
243 }
244
245
246 void C1_MacroAssembler::remove_frame(int frame_size_in_bytes) {
247 increment(rsp, frame_size_in_bytes); // Does not emit code for frame_size == 0
248 pop(rbp);
249 }
250
251
252 void C1_MacroAssembler::verified_entry(bool breakAtEntry) {
253 if (breakAtEntry) int3();
254 // build frame
255 }
256
257 void C1_MacroAssembler::load_parameter(int offset_in_words, Register reg) {
258 // rbp, + 0: link
259 // + 1: return address
260 // + 2: argument with offset 0
261 // + 3: argument with offset 1
262 // + 4: ...
263
264 movptr(reg, Address(rbp, (offset_in_words + 2) * BytesPerWord));
265 }
266
267 #ifndef PRODUCT
268
269 void C1_MacroAssembler::verify_stack_oop(int stack_offset) {
270 if (!VerifyOops) return;
271 verify_oop_addr(Address(rsp, stack_offset));
272 }
273
274 void C1_MacroAssembler::verify_not_null_oop(Register r) {
275 if (!VerifyOops) return;
276 Label not_null;
|
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/arguments.hpp"
37 #include "runtime/basicLock.hpp"
38 #include "runtime/frame.inline.hpp"
39 #include "runtime/globals.hpp"
40 #include "runtime/os.hpp"
41 #include "runtime/sharedRuntime.hpp"
42 #include "runtime/stubRoutines.hpp"
43 #include "utilities/checkedCast.hpp"
44 #include "utilities/globalDefinitions.hpp"
45
46 int C1_MacroAssembler::lock_object(Register hdr, Register obj, Register basic_lock, Register tmp, Label& slow_case) {
47 assert(hdr == rax, "hdr must be rax, for the cmpxchg instruction");
48 assert_different_registers(hdr, obj, basic_lock, tmp);
49 int null_check_offset = -1;
50
51 verify_oop(obj);
52
53 // save object being locked into the BasicObjectLock
54 movptr(Address(basic_lock, BasicObjectLock::obj_offset()), obj);
55
56 null_check_offset = offset();
57
58 fast_lock(basic_lock, obj, hdr, tmp, slow_case);
67 // load object
68 movptr(obj, Address(basic_lock, BasicObjectLock::obj_offset()));
69 verify_oop(obj);
70
71 fast_unlock(obj, rax, hdr, slow_case);
72 }
73
74
75 // Defines obj, preserves var_size_in_bytes
76 void C1_MacroAssembler::try_allocate(Register obj, Register var_size_in_bytes, int con_size_in_bytes, Register t1, Register t2, Label& slow_case) {
77 if (UseTLAB) {
78 tlab_allocate(obj, var_size_in_bytes, con_size_in_bytes, t1, t2, slow_case);
79 } else {
80 jmp(slow_case);
81 }
82 }
83
84
85 void C1_MacroAssembler::initialize_header(Register obj, Register klass, Register len, Register t1, Register t2) {
86 assert_different_registers(obj, klass, len, t1, t2);
87 if (UseCompactObjectHeaders || Arguments::is_valhalla_enabled()) {
88 // COH: Markword contains class pointer which is only known at runtime.
89 // Valhalla: Could have value class which has a different prototype header to a normal object.
90 // In both cases, we need to fetch dynamically.
91 movptr(t1, Address(klass, Klass::prototype_header_offset()));
92 movptr(Address(obj, oopDesc::mark_offset_in_bytes()), t1);
93 } else {
94 // Otherwise: Can use the statically computed prototype header which is the same for every object.
95 movptr(Address(obj, oopDesc::mark_offset_in_bytes()), checked_cast<int32_t>(markWord::prototype().value()));
96 }
97 if (!UseCompactObjectHeaders) {
98 // COH: Markword already contains class pointer. Nothing else to do.
99 // Otherwise: Fetch klass pointer following the markword
100 if (UseCompressedClassPointers) { // Take care not to kill klass
101 movptr(t1, klass);
102 encode_klass_not_null(t1, rscratch1);
103 movl(Address(obj, oopDesc::klass_offset_in_bytes()), t1);
104 } else {
105 movptr(Address(obj, oopDesc::klass_offset_in_bytes()), klass);
106 }
107 }
108
109 if (len->is_valid()) {
110 movl(Address(obj, arrayOopDesc::length_offset_in_bytes()), len);
111 int base_offset = arrayOopDesc::length_offset_in_bytes() + BytesPerInt;
112 if (!is_aligned(base_offset, BytesPerWord)) {
113 assert(is_aligned(base_offset, BytesPerInt), "must be 4-byte aligned");
114 // Clear gap/first 4 bytes following the length field.
115 xorl(t1, t1);
116 movl(Address(obj, base_offset), t1);
117 }
118 } else if (UseCompressedClassPointers && !UseCompactObjectHeaders) {
119 xorptr(t1, t1);
120 store_klass_gap(obj, t1);
121 }
122 }
123
124
125 // preserves obj, destroys len_in_bytes
126 void C1_MacroAssembler::initialize_body(Register obj, Register len_in_bytes, int hdr_size_in_bytes, Register t1) {
216
217 initialize_header(obj, klass, len, t1, t2);
218
219 // clear rest of allocated space
220 if (zero_array) {
221 const Register len_zero = len;
222 // Align-up to word boundary, because we clear the 4 bytes potentially
223 // following the length field in initialize_header().
224 int base_offset = align_up(base_offset_in_bytes, BytesPerWord);
225 initialize_body(obj, arr_size, base_offset, len_zero);
226 }
227
228 if (CURRENT_ENV->dtrace_alloc_probes()) {
229 assert(obj == rax, "must be");
230 call(RuntimeAddress(Runtime1::entry_for(StubId::c1_dtrace_object_alloc_id)));
231 }
232
233 verify_oop(obj);
234 }
235
236 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) {
237 push(rbp);
238 if (PreserveFramePointer) {
239 mov(rbp, rsp);
240 }
241 decrement(rsp, frame_size_in_bytes);
242
243 if (needs_stack_repair) {
244 // Save stack increment (also account for fixed framesize and rbp)
245 assert((sp_inc & (StackAlignmentInBytes-1)) == 0, "stack increment not aligned");
246 int real_frame_size = sp_inc + frame_size_in_bytes + wordSize;
247 movptr(Address(rsp, frame_size_in_bytes - wordSize), real_frame_size);
248 }
249 if (reset_orig_pc) {
250 // Zero orig_pc to detect deoptimization during buffering in the entry points
251 movptr(Address(rsp, sp_offset_for_orig_pc), 0);
252 }
253 }
254
255 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) {
256 // Make sure there is enough stack space for this method's activation.
257 // Note that we do this before doing an enter(). This matches the
258 // ordering of C2's stack overflow check / rsp decrement and allows
259 // the SharedRuntime stack overflow handling to be consistent
260 // between the two compilers.
261 assert(bang_size_in_bytes >= frame_size_in_bytes, "stack bang size incorrect");
262 generate_stack_overflow_check(bang_size_in_bytes);
263
264 build_frame_helper(frame_size_in_bytes, sp_offset_for_orig_pc, 0, has_scalarized_args, needs_stack_repair);
265
266 BarrierSetAssembler* bs = BarrierSet::barrier_set()->barrier_set_assembler();
267 // C1 code is not hot enough to micro optimize the nmethod entry barrier with an out-of-line stub
268 bs->nmethod_entry_barrier(this, nullptr /* slow_path */, nullptr /* continuation */);
269
270 if (verified_inline_entry_label != nullptr) {
271 // Jump here from the scalarized entry points that already created the frame.
272 bind(*verified_inline_entry_label);
273 }
274 }
275
276 void C1_MacroAssembler::verified_entry(bool breakAtEntry) {
277 if (breakAtEntry) int3();
278 // build frame
279 }
280
281 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) {
282 assert(InlineTypePassFieldsAsArgs, "sanity");
283 // Make sure there is enough stack space for this method's activation.
284 assert(bang_size_in_bytes >= frame_size_in_bytes, "stack bang size incorrect");
285 generate_stack_overflow_check(bang_size_in_bytes);
286
287 GrowableArray<SigEntry>* sig = ces->sig();
288 GrowableArray<SigEntry>* sig_cc = is_inline_ro_entry ? ces->sig_cc_ro() : ces->sig_cc();
289 VMRegPair* regs = ces->regs();
290 VMRegPair* regs_cc = is_inline_ro_entry ? ces->regs_cc_ro() : ces->regs_cc();
291 int args_on_stack = ces->args_on_stack();
292 int args_on_stack_cc = is_inline_ro_entry ? ces->args_on_stack_cc_ro() : ces->args_on_stack_cc();
293
294 assert(sig->length() <= sig_cc->length(), "Zero-sized inline class not allowed!");
295 BasicType* sig_bt = NEW_RESOURCE_ARRAY(BasicType, sig_cc->length());
296 int args_passed = sig->length();
297 int args_passed_cc = SigEntry::fill_sig_bt(sig_cc, sig_bt);
298
299 // Create a temp frame so we can call into the runtime. It must be properly set up to accommodate GC.
300 build_frame_helper(frame_size_in_bytes, sp_offset_for_orig_pc, 0, true, ces->c1_needs_stack_repair());
301
302 // The runtime call might safepoint, make sure nmethod entry barrier is executed
303 BarrierSetAssembler* bs = BarrierSet::barrier_set()->barrier_set_assembler();
304 // C1 code is not hot enough to micro optimize the nmethod entry barrier with an out-of-line stub
305 bs->nmethod_entry_barrier(this, nullptr /* slow_path */, nullptr /* continuation */);
306
307 // FIXME -- call runtime only if we cannot in-line allocate all the incoming inline type args.
308 movptr(rbx, (intptr_t)(ces->method()));
309 if (is_inline_ro_entry) {
310 call(RuntimeAddress(Runtime1::entry_for(StubId::c1_buffer_inline_args_no_receiver_id)));
311 } else {
312 call(RuntimeAddress(Runtime1::entry_for(StubId::c1_buffer_inline_args_id)));
313 }
314 int rt_call_offset = offset();
315
316 // Remove the temp frame
317 addptr(rsp, frame_size_in_bytes);
318 pop(rbp);
319
320 // Check if we need to extend the stack for packing
321 int sp_inc = 0;
322 if (args_on_stack > args_on_stack_cc) {
323 sp_inc = extend_stack_for_inline_args(args_on_stack);
324 }
325
326 shuffle_inline_args(true, is_inline_ro_entry, sig_cc,
327 args_passed_cc, args_on_stack_cc, regs_cc, // from
328 args_passed, args_on_stack, regs, // to
329 sp_inc, rax);
330
331 // Create the real frame. Below jump will then skip over the stack banging and frame
332 // setup code in the verified_inline_entry (which has a different real_frame_size).
333 build_frame_helper(frame_size_in_bytes, sp_offset_for_orig_pc, sp_inc, false, ces->c1_needs_stack_repair());
334
335 jmp(verified_inline_entry_label);
336 return rt_call_offset;
337 }
338
339 void C1_MacroAssembler::load_parameter(int offset_in_words, Register reg) {
340 // rbp, + 0: link
341 // + 1: return address
342 // + 2: argument with offset 0
343 // + 3: argument with offset 1
344 // + 4: ...
345
346 movptr(reg, Address(rbp, (offset_in_words + 2) * BytesPerWord));
347 }
348
349 #ifndef PRODUCT
350
351 void C1_MacroAssembler::verify_stack_oop(int stack_offset) {
352 if (!VerifyOops) return;
353 verify_oop_addr(Address(rsp, stack_offset));
354 }
355
356 void C1_MacroAssembler::verify_not_null_oop(Register r) {
357 if (!VerifyOops) return;
358 Label not_null;
|