1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/src/cpu/x86/vm/c1_MacroAssembler_x86.cpp Wed Apr 27 01:25:04 2016 +0800 1.3 @@ -0,0 +1,429 @@ 1.4 +/* 1.5 + * Copyright (c) 1999, 2013, Oracle and/or its affiliates. All rights reserved. 1.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 1.7 + * 1.8 + * This code is free software; you can redistribute it and/or modify it 1.9 + * under the terms of the GNU General Public License version 2 only, as 1.10 + * published by the Free Software Foundation. 1.11 + * 1.12 + * This code is distributed in the hope that it will be useful, but WITHOUT 1.13 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 1.14 + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 1.15 + * version 2 for more details (a copy is included in the LICENSE file that 1.16 + * accompanied this code). 1.17 + * 1.18 + * You should have received a copy of the GNU General Public License version 1.19 + * 2 along with this work; if not, write to the Free Software Foundation, 1.20 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 1.21 + * 1.22 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 1.23 + * or visit www.oracle.com if you need additional information or have any 1.24 + * questions. 1.25 + * 1.26 + */ 1.27 + 1.28 +#include "precompiled.hpp" 1.29 +#include "c1/c1_MacroAssembler.hpp" 1.30 +#include "c1/c1_Runtime1.hpp" 1.31 +#include "classfile/systemDictionary.hpp" 1.32 +#include "gc_interface/collectedHeap.hpp" 1.33 +#include "interpreter/interpreter.hpp" 1.34 +#include "oops/arrayOop.hpp" 1.35 +#include "oops/markOop.hpp" 1.36 +#include "runtime/basicLock.hpp" 1.37 +#include "runtime/biasedLocking.hpp" 1.38 +#include "runtime/os.hpp" 1.39 +#include "runtime/stubRoutines.hpp" 1.40 + 1.41 +int C1_MacroAssembler::lock_object(Register hdr, Register obj, Register disp_hdr, Register scratch, Label& slow_case) { 1.42 + const int aligned_mask = BytesPerWord -1; 1.43 + const int hdr_offset = oopDesc::mark_offset_in_bytes(); 1.44 + assert(hdr == rax, "hdr must be rax, for the cmpxchg instruction"); 1.45 + assert(hdr != obj && hdr != disp_hdr && obj != disp_hdr, "registers must be different"); 1.46 + Label done; 1.47 + int null_check_offset = -1; 1.48 + 1.49 + verify_oop(obj); 1.50 + 1.51 + // save object being locked into the BasicObjectLock 1.52 + movptr(Address(disp_hdr, BasicObjectLock::obj_offset_in_bytes()), obj); 1.53 + 1.54 + if (UseBiasedLocking) { 1.55 + assert(scratch != noreg, "should have scratch register at this point"); 1.56 + null_check_offset = biased_locking_enter(disp_hdr, obj, hdr, scratch, false, done, &slow_case); 1.57 + } else { 1.58 + null_check_offset = offset(); 1.59 + } 1.60 + 1.61 + // Load object header 1.62 + movptr(hdr, Address(obj, hdr_offset)); 1.63 + // and mark it as unlocked 1.64 + orptr(hdr, markOopDesc::unlocked_value); 1.65 + // save unlocked object header into the displaced header location on the stack 1.66 + movptr(Address(disp_hdr, 0), hdr); 1.67 + // test if object header is still the same (i.e. unlocked), and if so, store the 1.68 + // displaced header address in the object header - if it is not the same, get the 1.69 + // object header instead 1.70 + if (os::is_MP()) MacroAssembler::lock(); // must be immediately before cmpxchg! 1.71 + cmpxchgptr(disp_hdr, Address(obj, hdr_offset)); 1.72 + // if the object header was the same, we're done 1.73 + if (PrintBiasedLockingStatistics) { 1.74 + cond_inc32(Assembler::equal, 1.75 + ExternalAddress((address)BiasedLocking::fast_path_entry_count_addr())); 1.76 + } 1.77 + jcc(Assembler::equal, done); 1.78 + // if the object header was not the same, it is now in the hdr register 1.79 + // => test if it is a stack pointer into the same stack (recursive locking), i.e.: 1.80 + // 1.81 + // 1) (hdr & aligned_mask) == 0 1.82 + // 2) rsp <= hdr 1.83 + // 3) hdr <= rsp + page_size 1.84 + // 1.85 + // these 3 tests can be done by evaluating the following expression: 1.86 + // 1.87 + // (hdr - rsp) & (aligned_mask - page_size) 1.88 + // 1.89 + // assuming both the stack pointer and page_size have their least 1.90 + // significant 2 bits cleared and page_size is a power of 2 1.91 + subptr(hdr, rsp); 1.92 + andptr(hdr, aligned_mask - os::vm_page_size()); 1.93 + // for recursive locking, the result is zero => save it in the displaced header 1.94 + // location (NULL in the displaced hdr location indicates recursive locking) 1.95 + movptr(Address(disp_hdr, 0), hdr); 1.96 + // otherwise we don't care about the result and handle locking via runtime call 1.97 + jcc(Assembler::notZero, slow_case); 1.98 + // done 1.99 + bind(done); 1.100 + return null_check_offset; 1.101 +} 1.102 + 1.103 + 1.104 +void C1_MacroAssembler::unlock_object(Register hdr, Register obj, Register disp_hdr, Label& slow_case) { 1.105 + const int aligned_mask = BytesPerWord -1; 1.106 + const int hdr_offset = oopDesc::mark_offset_in_bytes(); 1.107 + assert(disp_hdr == rax, "disp_hdr must be rax, for the cmpxchg instruction"); 1.108 + assert(hdr != obj && hdr != disp_hdr && obj != disp_hdr, "registers must be different"); 1.109 + Label done; 1.110 + 1.111 + if (UseBiasedLocking) { 1.112 + // load object 1.113 + movptr(obj, Address(disp_hdr, BasicObjectLock::obj_offset_in_bytes())); 1.114 + biased_locking_exit(obj, hdr, done); 1.115 + } 1.116 + 1.117 + // load displaced header 1.118 + movptr(hdr, Address(disp_hdr, 0)); 1.119 + // if the loaded hdr is NULL we had recursive locking 1.120 + testptr(hdr, hdr); 1.121 + // if we had recursive locking, we are done 1.122 + jcc(Assembler::zero, done); 1.123 + if (!UseBiasedLocking) { 1.124 + // load object 1.125 + movptr(obj, Address(disp_hdr, BasicObjectLock::obj_offset_in_bytes())); 1.126 + } 1.127 + verify_oop(obj); 1.128 + // test if object header is pointing to the displaced header, and if so, restore 1.129 + // the displaced header in the object - if the object header is not pointing to 1.130 + // the displaced header, get the object header instead 1.131 + if (os::is_MP()) MacroAssembler::lock(); // must be immediately before cmpxchg! 1.132 + cmpxchgptr(hdr, Address(obj, hdr_offset)); 1.133 + // if the object header was not pointing to the displaced header, 1.134 + // we do unlocking via runtime call 1.135 + jcc(Assembler::notEqual, slow_case); 1.136 + // done 1.137 + bind(done); 1.138 +} 1.139 + 1.140 + 1.141 +// Defines obj, preserves var_size_in_bytes 1.142 +void C1_MacroAssembler::try_allocate(Register obj, Register var_size_in_bytes, int con_size_in_bytes, Register t1, Register t2, Label& slow_case) { 1.143 + if (UseTLAB) { 1.144 + tlab_allocate(obj, var_size_in_bytes, con_size_in_bytes, t1, t2, slow_case); 1.145 + } else { 1.146 + eden_allocate(obj, var_size_in_bytes, con_size_in_bytes, t1, slow_case); 1.147 + incr_allocated_bytes(noreg, var_size_in_bytes, con_size_in_bytes, t1); 1.148 + } 1.149 +} 1.150 + 1.151 + 1.152 +void C1_MacroAssembler::initialize_header(Register obj, Register klass, Register len, Register t1, Register t2) { 1.153 + assert_different_registers(obj, klass, len); 1.154 + if (UseBiasedLocking && !len->is_valid()) { 1.155 + assert_different_registers(obj, klass, len, t1, t2); 1.156 + movptr(t1, Address(klass, Klass::prototype_header_offset())); 1.157 + movptr(Address(obj, oopDesc::mark_offset_in_bytes()), t1); 1.158 + } else { 1.159 + // This assumes that all prototype bits fit in an int32_t 1.160 + movptr(Address(obj, oopDesc::mark_offset_in_bytes ()), (int32_t)(intptr_t)markOopDesc::prototype()); 1.161 + } 1.162 +#ifdef _LP64 1.163 + if (UseCompressedClassPointers) { // Take care not to kill klass 1.164 + movptr(t1, klass); 1.165 + encode_klass_not_null(t1); 1.166 + movl(Address(obj, oopDesc::klass_offset_in_bytes()), t1); 1.167 + } else 1.168 +#endif 1.169 + { 1.170 + movptr(Address(obj, oopDesc::klass_offset_in_bytes()), klass); 1.171 + } 1.172 + 1.173 + if (len->is_valid()) { 1.174 + movl(Address(obj, arrayOopDesc::length_offset_in_bytes()), len); 1.175 + } 1.176 +#ifdef _LP64 1.177 + else if (UseCompressedClassPointers) { 1.178 + xorptr(t1, t1); 1.179 + store_klass_gap(obj, t1); 1.180 + } 1.181 +#endif 1.182 +} 1.183 + 1.184 + 1.185 +// preserves obj, destroys len_in_bytes 1.186 +void C1_MacroAssembler::initialize_body(Register obj, Register len_in_bytes, int hdr_size_in_bytes, Register t1) { 1.187 + Label done; 1.188 + assert(obj != len_in_bytes && obj != t1 && t1 != len_in_bytes, "registers must be different"); 1.189 + assert((hdr_size_in_bytes & (BytesPerWord - 1)) == 0, "header size is not a multiple of BytesPerWord"); 1.190 + Register index = len_in_bytes; 1.191 + // index is positive and ptr sized 1.192 + subptr(index, hdr_size_in_bytes); 1.193 + jcc(Assembler::zero, done); 1.194 + // initialize topmost word, divide index by 2, check if odd and test if zero 1.195 + // note: for the remaining code to work, index must be a multiple of BytesPerWord 1.196 +#ifdef ASSERT 1.197 + { Label L; 1.198 + testptr(index, BytesPerWord - 1); 1.199 + jcc(Assembler::zero, L); 1.200 + stop("index is not a multiple of BytesPerWord"); 1.201 + bind(L); 1.202 + } 1.203 +#endif 1.204 + xorptr(t1, t1); // use _zero reg to clear memory (shorter code) 1.205 + if (UseIncDec) { 1.206 + shrptr(index, 3); // divide by 8/16 and set carry flag if bit 2 was set 1.207 + } else { 1.208 + shrptr(index, 2); // use 2 instructions to avoid partial flag stall 1.209 + shrptr(index, 1); 1.210 + } 1.211 +#ifndef _LP64 1.212 + // index could have been not a multiple of 8 (i.e., bit 2 was set) 1.213 + { Label even; 1.214 + // note: if index was a multiple of 8, than it cannot 1.215 + // be 0 now otherwise it must have been 0 before 1.216 + // => if it is even, we don't need to check for 0 again 1.217 + jcc(Assembler::carryClear, even); 1.218 + // clear topmost word (no jump needed if conditional assignment would work here) 1.219 + movptr(Address(obj, index, Address::times_8, hdr_size_in_bytes - 0*BytesPerWord), t1); 1.220 + // index could be 0 now, need to check again 1.221 + jcc(Assembler::zero, done); 1.222 + bind(even); 1.223 + } 1.224 +#endif // !_LP64 1.225 + // initialize remaining object fields: rdx is a multiple of 2 now 1.226 + { Label loop; 1.227 + bind(loop); 1.228 + movptr(Address(obj, index, Address::times_8, hdr_size_in_bytes - 1*BytesPerWord), t1); 1.229 + NOT_LP64(movptr(Address(obj, index, Address::times_8, hdr_size_in_bytes - 2*BytesPerWord), t1);) 1.230 + decrement(index); 1.231 + jcc(Assembler::notZero, loop); 1.232 + } 1.233 + 1.234 + // done 1.235 + bind(done); 1.236 +} 1.237 + 1.238 + 1.239 +void C1_MacroAssembler::allocate_object(Register obj, Register t1, Register t2, int header_size, int object_size, Register klass, Label& slow_case) { 1.240 + assert(obj == rax, "obj must be in rax, for cmpxchg"); 1.241 + assert_different_registers(obj, t1, t2); // XXX really? 1.242 + assert(header_size >= 0 && object_size >= header_size, "illegal sizes"); 1.243 + 1.244 + try_allocate(obj, noreg, object_size * BytesPerWord, t1, t2, slow_case); 1.245 + 1.246 + initialize_object(obj, klass, noreg, object_size * HeapWordSize, t1, t2); 1.247 +} 1.248 + 1.249 +void C1_MacroAssembler::initialize_object(Register obj, Register klass, Register var_size_in_bytes, int con_size_in_bytes, Register t1, Register t2) { 1.250 + assert((con_size_in_bytes & MinObjAlignmentInBytesMask) == 0, 1.251 + "con_size_in_bytes is not multiple of alignment"); 1.252 + const int hdr_size_in_bytes = instanceOopDesc::header_size() * HeapWordSize; 1.253 + 1.254 + initialize_header(obj, klass, noreg, t1, t2); 1.255 + 1.256 + // clear rest of allocated space 1.257 + const Register t1_zero = t1; 1.258 + const Register index = t2; 1.259 + const int threshold = 6 * BytesPerWord; // approximate break even point for code size (see comments below) 1.260 + if (var_size_in_bytes != noreg) { 1.261 + mov(index, var_size_in_bytes); 1.262 + initialize_body(obj, index, hdr_size_in_bytes, t1_zero); 1.263 + } else if (con_size_in_bytes <= threshold) { 1.264 + // use explicit null stores 1.265 + // code size = 2 + 3*n bytes (n = number of fields to clear) 1.266 + xorptr(t1_zero, t1_zero); // use t1_zero reg to clear memory (shorter code) 1.267 + for (int i = hdr_size_in_bytes; i < con_size_in_bytes; i += BytesPerWord) 1.268 + movptr(Address(obj, i), t1_zero); 1.269 + } else if (con_size_in_bytes > hdr_size_in_bytes) { 1.270 + // use loop to null out the fields 1.271 + // code size = 16 bytes for even n (n = number of fields to clear) 1.272 + // initialize last object field first if odd number of fields 1.273 + xorptr(t1_zero, t1_zero); // use t1_zero reg to clear memory (shorter code) 1.274 + movptr(index, (con_size_in_bytes - hdr_size_in_bytes) >> 3); 1.275 + // initialize last object field if constant size is odd 1.276 + if (((con_size_in_bytes - hdr_size_in_bytes) & 4) != 0) 1.277 + movptr(Address(obj, con_size_in_bytes - (1*BytesPerWord)), t1_zero); 1.278 + // initialize remaining object fields: rdx is a multiple of 2 1.279 + { Label loop; 1.280 + bind(loop); 1.281 + movptr(Address(obj, index, Address::times_8, hdr_size_in_bytes - (1*BytesPerWord)), 1.282 + t1_zero); 1.283 + NOT_LP64(movptr(Address(obj, index, Address::times_8, hdr_size_in_bytes - (2*BytesPerWord)), 1.284 + t1_zero);) 1.285 + decrement(index); 1.286 + jcc(Assembler::notZero, loop); 1.287 + } 1.288 + } 1.289 + 1.290 + if (CURRENT_ENV->dtrace_alloc_probes()) { 1.291 + assert(obj == rax, "must be"); 1.292 + call(RuntimeAddress(Runtime1::entry_for(Runtime1::dtrace_object_alloc_id))); 1.293 + } 1.294 + 1.295 + verify_oop(obj); 1.296 +} 1.297 + 1.298 +void C1_MacroAssembler::allocate_array(Register obj, Register len, Register t1, Register t2, int header_size, Address::ScaleFactor f, Register klass, Label& slow_case) { 1.299 + assert(obj == rax, "obj must be in rax, for cmpxchg"); 1.300 + assert_different_registers(obj, len, t1, t2, klass); 1.301 + 1.302 + // determine alignment mask 1.303 + assert(!(BytesPerWord & 1), "must be a multiple of 2 for masking code to work"); 1.304 + 1.305 + // check for negative or excessive length 1.306 + cmpptr(len, (int32_t)max_array_allocation_length); 1.307 + jcc(Assembler::above, slow_case); 1.308 + 1.309 + const Register arr_size = t2; // okay to be the same 1.310 + // align object end 1.311 + movptr(arr_size, (int32_t)header_size * BytesPerWord + MinObjAlignmentInBytesMask); 1.312 + lea(arr_size, Address(arr_size, len, f)); 1.313 + andptr(arr_size, ~MinObjAlignmentInBytesMask); 1.314 + 1.315 + try_allocate(obj, arr_size, 0, t1, t2, slow_case); 1.316 + 1.317 + initialize_header(obj, klass, len, t1, t2); 1.318 + 1.319 + // clear rest of allocated space 1.320 + const Register len_zero = len; 1.321 + initialize_body(obj, arr_size, header_size * BytesPerWord, len_zero); 1.322 + 1.323 + if (CURRENT_ENV->dtrace_alloc_probes()) { 1.324 + assert(obj == rax, "must be"); 1.325 + call(RuntimeAddress(Runtime1::entry_for(Runtime1::dtrace_object_alloc_id))); 1.326 + } 1.327 + 1.328 + verify_oop(obj); 1.329 +} 1.330 + 1.331 + 1.332 + 1.333 +void C1_MacroAssembler::inline_cache_check(Register receiver, Register iCache) { 1.334 + verify_oop(receiver); 1.335 + // explicit NULL check not needed since load from [klass_offset] causes a trap 1.336 + // check against inline cache 1.337 + assert(!MacroAssembler::needs_explicit_null_check(oopDesc::klass_offset_in_bytes()), "must add explicit null check"); 1.338 + int start_offset = offset(); 1.339 + 1.340 + if (UseCompressedClassPointers) { 1.341 + load_klass(rscratch1, receiver); 1.342 + cmpptr(rscratch1, iCache); 1.343 + } else { 1.344 + cmpptr(iCache, Address(receiver, oopDesc::klass_offset_in_bytes())); 1.345 + } 1.346 + // if icache check fails, then jump to runtime routine 1.347 + // Note: RECEIVER must still contain the receiver! 1.348 + jump_cc(Assembler::notEqual, 1.349 + RuntimeAddress(SharedRuntime::get_ic_miss_stub())); 1.350 + const int ic_cmp_size = LP64_ONLY(10) NOT_LP64(9); 1.351 + assert(UseCompressedClassPointers || offset() - start_offset == ic_cmp_size, "check alignment in emit_method_entry"); 1.352 +} 1.353 + 1.354 + 1.355 +void C1_MacroAssembler::build_frame(int frame_size_in_bytes, int bang_size_in_bytes) { 1.356 + assert(bang_size_in_bytes >= frame_size_in_bytes, "stack bang size incorrect"); 1.357 + // Make sure there is enough stack space for this method's activation. 1.358 + // Note that we do this before doing an enter(). This matches the 1.359 + // ordering of C2's stack overflow check / rsp decrement and allows 1.360 + // the SharedRuntime stack overflow handling to be consistent 1.361 + // between the two compilers. 1.362 + generate_stack_overflow_check(bang_size_in_bytes); 1.363 + 1.364 + push(rbp); 1.365 +#ifdef TIERED 1.366 + // c2 leaves fpu stack dirty. Clean it on entry 1.367 + if (UseSSE < 2 ) { 1.368 + empty_FPU_stack(); 1.369 + } 1.370 +#endif // TIERED 1.371 + decrement(rsp, frame_size_in_bytes); // does not emit code for frame_size == 0 1.372 +} 1.373 + 1.374 + 1.375 +void C1_MacroAssembler::remove_frame(int frame_size_in_bytes) { 1.376 + increment(rsp, frame_size_in_bytes); // Does not emit code for frame_size == 0 1.377 + pop(rbp); 1.378 +} 1.379 + 1.380 + 1.381 +void C1_MacroAssembler::unverified_entry(Register receiver, Register ic_klass) { 1.382 + if (C1Breakpoint) int3(); 1.383 + inline_cache_check(receiver, ic_klass); 1.384 +} 1.385 + 1.386 + 1.387 +void C1_MacroAssembler::verified_entry() { 1.388 + if (C1Breakpoint || VerifyFPU || !UseStackBanging) { 1.389 + // Verified Entry first instruction should be 5 bytes long for correct 1.390 + // patching by patch_verified_entry(). 1.391 + // 1.392 + // C1Breakpoint and VerifyFPU have one byte first instruction. 1.393 + // Also first instruction will be one byte "push(rbp)" if stack banging 1.394 + // code is not generated (see build_frame() above). 1.395 + // For all these cases generate long instruction first. 1.396 + fat_nop(); 1.397 + } 1.398 + if (C1Breakpoint)int3(); 1.399 + // build frame 1.400 + verify_FPU(0, "method_entry"); 1.401 +} 1.402 + 1.403 + 1.404 +#ifndef PRODUCT 1.405 + 1.406 +void C1_MacroAssembler::verify_stack_oop(int stack_offset) { 1.407 + if (!VerifyOops) return; 1.408 + verify_oop_addr(Address(rsp, stack_offset)); 1.409 +} 1.410 + 1.411 +void C1_MacroAssembler::verify_not_null_oop(Register r) { 1.412 + if (!VerifyOops) return; 1.413 + Label not_null; 1.414 + testptr(r, r); 1.415 + jcc(Assembler::notZero, not_null); 1.416 + stop("non-null oop required"); 1.417 + bind(not_null); 1.418 + verify_oop(r); 1.419 +} 1.420 + 1.421 +void C1_MacroAssembler::invalidate_registers(bool inv_rax, bool inv_rbx, bool inv_rcx, bool inv_rdx, bool inv_rsi, bool inv_rdi) { 1.422 +#ifdef ASSERT 1.423 + if (inv_rax) movptr(rax, 0xDEAD); 1.424 + if (inv_rbx) movptr(rbx, 0xDEAD); 1.425 + if (inv_rcx) movptr(rcx, 0xDEAD); 1.426 + if (inv_rdx) movptr(rdx, 0xDEAD); 1.427 + if (inv_rsi) movptr(rsi, 0xDEAD); 1.428 + if (inv_rdi) movptr(rdi, 0xDEAD); 1.429 +#endif 1.430 +} 1.431 + 1.432 +#endif // ifndef PRODUCT