aoqi@0: /* aoqi@0: * Copyright (c) 1999, 2013, Oracle and/or its affiliates. All rights reserved. aoqi@0: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. aoqi@0: * aoqi@0: * This code is free software; you can redistribute it and/or modify it aoqi@0: * under the terms of the GNU General Public License version 2 only, as aoqi@0: * published by the Free Software Foundation. aoqi@0: * aoqi@0: * This code is distributed in the hope that it will be useful, but WITHOUT aoqi@0: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or aoqi@0: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License aoqi@0: * version 2 for more details (a copy is included in the LICENSE file that aoqi@0: * accompanied this code). aoqi@0: * aoqi@0: * You should have received a copy of the GNU General Public License version aoqi@0: * 2 along with this work; if not, write to the Free Software Foundation, aoqi@0: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. aoqi@0: * aoqi@0: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA aoqi@0: * or visit www.oracle.com if you need additional information or have any aoqi@0: * questions. aoqi@0: * aoqi@0: */ aoqi@0: aoqi@0: #include "precompiled.hpp" aoqi@0: #include "c1/c1_MacroAssembler.hpp" aoqi@0: #include "c1/c1_Runtime1.hpp" aoqi@0: #include "classfile/systemDictionary.hpp" aoqi@0: #include "gc_interface/collectedHeap.hpp" aoqi@0: #include "interpreter/interpreter.hpp" aoqi@0: #include "oops/arrayOop.hpp" aoqi@0: #include "oops/markOop.hpp" aoqi@0: #include "runtime/basicLock.hpp" aoqi@0: #include "runtime/biasedLocking.hpp" aoqi@0: #include "runtime/os.hpp" aoqi@0: #include "runtime/stubRoutines.hpp" aoqi@0: aoqi@0: int C1_MacroAssembler::lock_object(Register hdr, Register obj, Register disp_hdr, Register scratch, Label& slow_case) { aoqi@0: const int aligned_mask = BytesPerWord -1; aoqi@0: const int hdr_offset = oopDesc::mark_offset_in_bytes(); aoqi@0: assert(hdr == rax, "hdr must be rax, for the cmpxchg instruction"); aoqi@0: assert(hdr != obj && hdr != disp_hdr && obj != disp_hdr, "registers must be different"); aoqi@0: Label done; aoqi@0: int null_check_offset = -1; aoqi@0: aoqi@0: verify_oop(obj); aoqi@0: aoqi@0: // save object being locked into the BasicObjectLock aoqi@0: movptr(Address(disp_hdr, BasicObjectLock::obj_offset_in_bytes()), obj); aoqi@0: aoqi@0: if (UseBiasedLocking) { aoqi@0: assert(scratch != noreg, "should have scratch register at this point"); aoqi@0: null_check_offset = biased_locking_enter(disp_hdr, obj, hdr, scratch, false, done, &slow_case); aoqi@0: } else { aoqi@0: null_check_offset = offset(); aoqi@0: } aoqi@0: aoqi@0: // Load object header aoqi@0: movptr(hdr, Address(obj, hdr_offset)); aoqi@0: // and mark it as unlocked aoqi@0: orptr(hdr, markOopDesc::unlocked_value); aoqi@0: // save unlocked object header into the displaced header location on the stack aoqi@0: movptr(Address(disp_hdr, 0), hdr); aoqi@0: // test if object header is still the same (i.e. unlocked), and if so, store the aoqi@0: // displaced header address in the object header - if it is not the same, get the aoqi@0: // object header instead aoqi@0: if (os::is_MP()) MacroAssembler::lock(); // must be immediately before cmpxchg! aoqi@0: cmpxchgptr(disp_hdr, Address(obj, hdr_offset)); aoqi@0: // if the object header was the same, we're done aoqi@0: if (PrintBiasedLockingStatistics) { aoqi@0: cond_inc32(Assembler::equal, aoqi@0: ExternalAddress((address)BiasedLocking::fast_path_entry_count_addr())); aoqi@0: } aoqi@0: jcc(Assembler::equal, done); aoqi@0: // if the object header was not the same, it is now in the hdr register aoqi@0: // => test if it is a stack pointer into the same stack (recursive locking), i.e.: aoqi@0: // aoqi@0: // 1) (hdr & aligned_mask) == 0 aoqi@0: // 2) rsp <= hdr aoqi@0: // 3) hdr <= rsp + page_size aoqi@0: // aoqi@0: // these 3 tests can be done by evaluating the following expression: aoqi@0: // aoqi@0: // (hdr - rsp) & (aligned_mask - page_size) aoqi@0: // aoqi@0: // assuming both the stack pointer and page_size have their least aoqi@0: // significant 2 bits cleared and page_size is a power of 2 aoqi@0: subptr(hdr, rsp); aoqi@0: andptr(hdr, aligned_mask - os::vm_page_size()); aoqi@0: // for recursive locking, the result is zero => save it in the displaced header aoqi@0: // location (NULL in the displaced hdr location indicates recursive locking) aoqi@0: movptr(Address(disp_hdr, 0), hdr); aoqi@0: // otherwise we don't care about the result and handle locking via runtime call aoqi@0: jcc(Assembler::notZero, slow_case); aoqi@0: // done aoqi@0: bind(done); aoqi@0: return null_check_offset; aoqi@0: } aoqi@0: aoqi@0: aoqi@0: void C1_MacroAssembler::unlock_object(Register hdr, Register obj, Register disp_hdr, Label& slow_case) { aoqi@0: const int aligned_mask = BytesPerWord -1; aoqi@0: const int hdr_offset = oopDesc::mark_offset_in_bytes(); aoqi@0: assert(disp_hdr == rax, "disp_hdr must be rax, for the cmpxchg instruction"); aoqi@0: assert(hdr != obj && hdr != disp_hdr && obj != disp_hdr, "registers must be different"); aoqi@0: Label done; aoqi@0: aoqi@0: if (UseBiasedLocking) { aoqi@0: // load object aoqi@0: movptr(obj, Address(disp_hdr, BasicObjectLock::obj_offset_in_bytes())); aoqi@0: biased_locking_exit(obj, hdr, done); aoqi@0: } aoqi@0: aoqi@0: // load displaced header aoqi@0: movptr(hdr, Address(disp_hdr, 0)); aoqi@0: // if the loaded hdr is NULL we had recursive locking aoqi@0: testptr(hdr, hdr); aoqi@0: // if we had recursive locking, we are done aoqi@0: jcc(Assembler::zero, done); aoqi@0: if (!UseBiasedLocking) { aoqi@0: // load object aoqi@0: movptr(obj, Address(disp_hdr, BasicObjectLock::obj_offset_in_bytes())); aoqi@0: } aoqi@0: verify_oop(obj); aoqi@0: // test if object header is pointing to the displaced header, and if so, restore aoqi@0: // the displaced header in the object - if the object header is not pointing to aoqi@0: // the displaced header, get the object header instead aoqi@0: if (os::is_MP()) MacroAssembler::lock(); // must be immediately before cmpxchg! aoqi@0: cmpxchgptr(hdr, Address(obj, hdr_offset)); aoqi@0: // if the object header was not pointing to the displaced header, aoqi@0: // we do unlocking via runtime call aoqi@0: jcc(Assembler::notEqual, slow_case); aoqi@0: // done aoqi@0: bind(done); aoqi@0: } aoqi@0: aoqi@0: aoqi@0: // Defines obj, preserves var_size_in_bytes aoqi@0: void C1_MacroAssembler::try_allocate(Register obj, Register var_size_in_bytes, int con_size_in_bytes, Register t1, Register t2, Label& slow_case) { aoqi@0: if (UseTLAB) { aoqi@0: tlab_allocate(obj, var_size_in_bytes, con_size_in_bytes, t1, t2, slow_case); aoqi@0: } else { aoqi@0: eden_allocate(obj, var_size_in_bytes, con_size_in_bytes, t1, slow_case); aoqi@0: incr_allocated_bytes(noreg, var_size_in_bytes, con_size_in_bytes, t1); aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: aoqi@0: void C1_MacroAssembler::initialize_header(Register obj, Register klass, Register len, Register t1, Register t2) { aoqi@0: assert_different_registers(obj, klass, len); aoqi@0: if (UseBiasedLocking && !len->is_valid()) { aoqi@0: assert_different_registers(obj, klass, len, t1, t2); aoqi@0: movptr(t1, Address(klass, Klass::prototype_header_offset())); aoqi@0: movptr(Address(obj, oopDesc::mark_offset_in_bytes()), t1); aoqi@0: } else { aoqi@0: // This assumes that all prototype bits fit in an int32_t aoqi@0: movptr(Address(obj, oopDesc::mark_offset_in_bytes ()), (int32_t)(intptr_t)markOopDesc::prototype()); aoqi@0: } aoqi@0: #ifdef _LP64 aoqi@0: if (UseCompressedClassPointers) { // Take care not to kill klass aoqi@0: movptr(t1, klass); aoqi@0: encode_klass_not_null(t1); aoqi@0: movl(Address(obj, oopDesc::klass_offset_in_bytes()), t1); aoqi@0: } else aoqi@0: #endif aoqi@0: { aoqi@0: movptr(Address(obj, oopDesc::klass_offset_in_bytes()), klass); aoqi@0: } aoqi@0: aoqi@0: if (len->is_valid()) { aoqi@0: movl(Address(obj, arrayOopDesc::length_offset_in_bytes()), len); aoqi@0: } aoqi@0: #ifdef _LP64 aoqi@0: else if (UseCompressedClassPointers) { aoqi@0: xorptr(t1, t1); aoqi@0: store_klass_gap(obj, t1); aoqi@0: } aoqi@0: #endif aoqi@0: } aoqi@0: aoqi@0: aoqi@0: // preserves obj, destroys len_in_bytes aoqi@0: void C1_MacroAssembler::initialize_body(Register obj, Register len_in_bytes, int hdr_size_in_bytes, Register t1) { aoqi@0: Label done; aoqi@0: assert(obj != len_in_bytes && obj != t1 && t1 != len_in_bytes, "registers must be different"); aoqi@0: assert((hdr_size_in_bytes & (BytesPerWord - 1)) == 0, "header size is not a multiple of BytesPerWord"); aoqi@0: Register index = len_in_bytes; aoqi@0: // index is positive and ptr sized aoqi@0: subptr(index, hdr_size_in_bytes); aoqi@0: jcc(Assembler::zero, done); aoqi@0: // initialize topmost word, divide index by 2, check if odd and test if zero aoqi@0: // note: for the remaining code to work, index must be a multiple of BytesPerWord aoqi@0: #ifdef ASSERT aoqi@0: { Label L; aoqi@0: testptr(index, BytesPerWord - 1); aoqi@0: jcc(Assembler::zero, L); aoqi@0: stop("index is not a multiple of BytesPerWord"); aoqi@0: bind(L); aoqi@0: } aoqi@0: #endif aoqi@0: xorptr(t1, t1); // use _zero reg to clear memory (shorter code) aoqi@0: if (UseIncDec) { aoqi@0: shrptr(index, 3); // divide by 8/16 and set carry flag if bit 2 was set aoqi@0: } else { aoqi@0: shrptr(index, 2); // use 2 instructions to avoid partial flag stall aoqi@0: shrptr(index, 1); aoqi@0: } aoqi@0: #ifndef _LP64 aoqi@0: // index could have been not a multiple of 8 (i.e., bit 2 was set) aoqi@0: { Label even; aoqi@0: // note: if index was a multiple of 8, than it cannot aoqi@0: // be 0 now otherwise it must have been 0 before aoqi@0: // => if it is even, we don't need to check for 0 again aoqi@0: jcc(Assembler::carryClear, even); aoqi@0: // clear topmost word (no jump needed if conditional assignment would work here) aoqi@0: movptr(Address(obj, index, Address::times_8, hdr_size_in_bytes - 0*BytesPerWord), t1); aoqi@0: // index could be 0 now, need to check again aoqi@0: jcc(Assembler::zero, done); aoqi@0: bind(even); aoqi@0: } aoqi@0: #endif // !_LP64 aoqi@0: // initialize remaining object fields: rdx is a multiple of 2 now aoqi@0: { Label loop; aoqi@0: bind(loop); aoqi@0: movptr(Address(obj, index, Address::times_8, hdr_size_in_bytes - 1*BytesPerWord), t1); aoqi@0: NOT_LP64(movptr(Address(obj, index, Address::times_8, hdr_size_in_bytes - 2*BytesPerWord), t1);) aoqi@0: decrement(index); aoqi@0: jcc(Assembler::notZero, loop); aoqi@0: } aoqi@0: aoqi@0: // done aoqi@0: bind(done); aoqi@0: } aoqi@0: aoqi@0: aoqi@0: void C1_MacroAssembler::allocate_object(Register obj, Register t1, Register t2, int header_size, int object_size, Register klass, Label& slow_case) { aoqi@0: assert(obj == rax, "obj must be in rax, for cmpxchg"); aoqi@0: assert_different_registers(obj, t1, t2); // XXX really? aoqi@0: assert(header_size >= 0 && object_size >= header_size, "illegal sizes"); aoqi@0: aoqi@0: try_allocate(obj, noreg, object_size * BytesPerWord, t1, t2, slow_case); aoqi@0: aoqi@0: initialize_object(obj, klass, noreg, object_size * HeapWordSize, t1, t2); aoqi@0: } aoqi@0: aoqi@0: void C1_MacroAssembler::initialize_object(Register obj, Register klass, Register var_size_in_bytes, int con_size_in_bytes, Register t1, Register t2) { aoqi@0: assert((con_size_in_bytes & MinObjAlignmentInBytesMask) == 0, aoqi@0: "con_size_in_bytes is not multiple of alignment"); aoqi@0: const int hdr_size_in_bytes = instanceOopDesc::header_size() * HeapWordSize; aoqi@0: aoqi@0: initialize_header(obj, klass, noreg, t1, t2); aoqi@0: aoqi@0: // clear rest of allocated space aoqi@0: const Register t1_zero = t1; aoqi@0: const Register index = t2; aoqi@0: const int threshold = 6 * BytesPerWord; // approximate break even point for code size (see comments below) aoqi@0: if (var_size_in_bytes != noreg) { aoqi@0: mov(index, var_size_in_bytes); aoqi@0: initialize_body(obj, index, hdr_size_in_bytes, t1_zero); aoqi@0: } else if (con_size_in_bytes <= threshold) { aoqi@0: // use explicit null stores aoqi@0: // code size = 2 + 3*n bytes (n = number of fields to clear) aoqi@0: xorptr(t1_zero, t1_zero); // use t1_zero reg to clear memory (shorter code) aoqi@0: for (int i = hdr_size_in_bytes; i < con_size_in_bytes; i += BytesPerWord) aoqi@0: movptr(Address(obj, i), t1_zero); aoqi@0: } else if (con_size_in_bytes > hdr_size_in_bytes) { aoqi@0: // use loop to null out the fields aoqi@0: // code size = 16 bytes for even n (n = number of fields to clear) aoqi@0: // initialize last object field first if odd number of fields aoqi@0: xorptr(t1_zero, t1_zero); // use t1_zero reg to clear memory (shorter code) aoqi@0: movptr(index, (con_size_in_bytes - hdr_size_in_bytes) >> 3); aoqi@0: // initialize last object field if constant size is odd aoqi@0: if (((con_size_in_bytes - hdr_size_in_bytes) & 4) != 0) aoqi@0: movptr(Address(obj, con_size_in_bytes - (1*BytesPerWord)), t1_zero); aoqi@0: // initialize remaining object fields: rdx is a multiple of 2 aoqi@0: { Label loop; aoqi@0: bind(loop); aoqi@0: movptr(Address(obj, index, Address::times_8, hdr_size_in_bytes - (1*BytesPerWord)), aoqi@0: t1_zero); aoqi@0: NOT_LP64(movptr(Address(obj, index, Address::times_8, hdr_size_in_bytes - (2*BytesPerWord)), aoqi@0: t1_zero);) aoqi@0: decrement(index); aoqi@0: jcc(Assembler::notZero, loop); aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: if (CURRENT_ENV->dtrace_alloc_probes()) { aoqi@0: assert(obj == rax, "must be"); aoqi@0: call(RuntimeAddress(Runtime1::entry_for(Runtime1::dtrace_object_alloc_id))); aoqi@0: } aoqi@0: aoqi@0: verify_oop(obj); aoqi@0: } aoqi@0: aoqi@0: void C1_MacroAssembler::allocate_array(Register obj, Register len, Register t1, Register t2, int header_size, Address::ScaleFactor f, Register klass, Label& slow_case) { aoqi@0: assert(obj == rax, "obj must be in rax, for cmpxchg"); aoqi@0: assert_different_registers(obj, len, t1, t2, klass); aoqi@0: aoqi@0: // determine alignment mask aoqi@0: assert(!(BytesPerWord & 1), "must be a multiple of 2 for masking code to work"); aoqi@0: aoqi@0: // check for negative or excessive length aoqi@0: cmpptr(len, (int32_t)max_array_allocation_length); aoqi@0: jcc(Assembler::above, slow_case); aoqi@0: aoqi@0: const Register arr_size = t2; // okay to be the same aoqi@0: // align object end aoqi@0: movptr(arr_size, (int32_t)header_size * BytesPerWord + MinObjAlignmentInBytesMask); aoqi@0: lea(arr_size, Address(arr_size, len, f)); aoqi@0: andptr(arr_size, ~MinObjAlignmentInBytesMask); aoqi@0: aoqi@0: try_allocate(obj, arr_size, 0, t1, t2, slow_case); aoqi@0: aoqi@0: initialize_header(obj, klass, len, t1, t2); aoqi@0: aoqi@0: // clear rest of allocated space aoqi@0: const Register len_zero = len; aoqi@0: initialize_body(obj, arr_size, header_size * BytesPerWord, len_zero); aoqi@0: aoqi@0: if (CURRENT_ENV->dtrace_alloc_probes()) { aoqi@0: assert(obj == rax, "must be"); aoqi@0: call(RuntimeAddress(Runtime1::entry_for(Runtime1::dtrace_object_alloc_id))); aoqi@0: } aoqi@0: aoqi@0: verify_oop(obj); aoqi@0: } aoqi@0: aoqi@0: aoqi@0: aoqi@0: void C1_MacroAssembler::inline_cache_check(Register receiver, Register iCache) { aoqi@0: verify_oop(receiver); aoqi@0: // explicit NULL check not needed since load from [klass_offset] causes a trap aoqi@0: // check against inline cache aoqi@0: assert(!MacroAssembler::needs_explicit_null_check(oopDesc::klass_offset_in_bytes()), "must add explicit null check"); aoqi@0: int start_offset = offset(); aoqi@0: aoqi@0: if (UseCompressedClassPointers) { aoqi@0: load_klass(rscratch1, receiver); aoqi@0: cmpptr(rscratch1, iCache); aoqi@0: } else { aoqi@0: cmpptr(iCache, Address(receiver, oopDesc::klass_offset_in_bytes())); aoqi@0: } aoqi@0: // if icache check fails, then jump to runtime routine aoqi@0: // Note: RECEIVER must still contain the receiver! aoqi@0: jump_cc(Assembler::notEqual, aoqi@0: RuntimeAddress(SharedRuntime::get_ic_miss_stub())); aoqi@0: const int ic_cmp_size = LP64_ONLY(10) NOT_LP64(9); aoqi@0: assert(UseCompressedClassPointers || offset() - start_offset == ic_cmp_size, "check alignment in emit_method_entry"); aoqi@0: } aoqi@0: aoqi@0: aoqi@0: void C1_MacroAssembler::build_frame(int frame_size_in_bytes, int bang_size_in_bytes) { aoqi@0: assert(bang_size_in_bytes >= frame_size_in_bytes, "stack bang size incorrect"); aoqi@0: // Make sure there is enough stack space for this method's activation. aoqi@0: // Note that we do this before doing an enter(). This matches the aoqi@0: // ordering of C2's stack overflow check / rsp decrement and allows aoqi@0: // the SharedRuntime stack overflow handling to be consistent aoqi@0: // between the two compilers. aoqi@0: generate_stack_overflow_check(bang_size_in_bytes); aoqi@0: aoqi@0: push(rbp); aoqi@0: #ifdef TIERED aoqi@0: // c2 leaves fpu stack dirty. Clean it on entry aoqi@0: if (UseSSE < 2 ) { aoqi@0: empty_FPU_stack(); aoqi@0: } aoqi@0: #endif // TIERED aoqi@0: decrement(rsp, frame_size_in_bytes); // does not emit code for frame_size == 0 aoqi@0: } aoqi@0: aoqi@0: aoqi@0: void C1_MacroAssembler::remove_frame(int frame_size_in_bytes) { aoqi@0: increment(rsp, frame_size_in_bytes); // Does not emit code for frame_size == 0 aoqi@0: pop(rbp); aoqi@0: } aoqi@0: aoqi@0: aoqi@0: void C1_MacroAssembler::unverified_entry(Register receiver, Register ic_klass) { aoqi@0: if (C1Breakpoint) int3(); aoqi@0: inline_cache_check(receiver, ic_klass); aoqi@0: } aoqi@0: aoqi@0: aoqi@0: void C1_MacroAssembler::verified_entry() { aoqi@0: if (C1Breakpoint || VerifyFPU || !UseStackBanging) { aoqi@0: // Verified Entry first instruction should be 5 bytes long for correct aoqi@0: // patching by patch_verified_entry(). aoqi@0: // aoqi@0: // C1Breakpoint and VerifyFPU have one byte first instruction. aoqi@0: // Also first instruction will be one byte "push(rbp)" if stack banging aoqi@0: // code is not generated (see build_frame() above). aoqi@0: // For all these cases generate long instruction first. aoqi@0: fat_nop(); aoqi@0: } aoqi@0: if (C1Breakpoint)int3(); aoqi@0: // build frame aoqi@0: verify_FPU(0, "method_entry"); aoqi@0: } aoqi@0: aoqi@0: aoqi@0: #ifndef PRODUCT aoqi@0: aoqi@0: void C1_MacroAssembler::verify_stack_oop(int stack_offset) { aoqi@0: if (!VerifyOops) return; aoqi@0: verify_oop_addr(Address(rsp, stack_offset)); aoqi@0: } aoqi@0: aoqi@0: void C1_MacroAssembler::verify_not_null_oop(Register r) { aoqi@0: if (!VerifyOops) return; aoqi@0: Label not_null; aoqi@0: testptr(r, r); aoqi@0: jcc(Assembler::notZero, not_null); aoqi@0: stop("non-null oop required"); aoqi@0: bind(not_null); aoqi@0: verify_oop(r); aoqi@0: } aoqi@0: aoqi@0: void C1_MacroAssembler::invalidate_registers(bool inv_rax, bool inv_rbx, bool inv_rcx, bool inv_rdx, bool inv_rsi, bool inv_rdi) { aoqi@0: #ifdef ASSERT aoqi@0: if (inv_rax) movptr(rax, 0xDEAD); aoqi@0: if (inv_rbx) movptr(rbx, 0xDEAD); aoqi@0: if (inv_rcx) movptr(rcx, 0xDEAD); aoqi@0: if (inv_rdx) movptr(rdx, 0xDEAD); aoqi@0: if (inv_rsi) movptr(rsi, 0xDEAD); aoqi@0: if (inv_rdi) movptr(rdi, 0xDEAD); aoqi@0: #endif aoqi@0: } aoqi@0: aoqi@0: #endif // ifndef PRODUCT