annotate src/cpu/x86/vm/c1_MacroAssembler_x86.cpp @ 0:a61af66fc99e

Initial load
author duke
date Sat, 01 Dec 2007 00:00:00 +0000
parents
children ba764ed4b6f2
rev   line source
duke@0 1 /*
duke@0 2 * Copyright 1999-2007 Sun Microsystems, Inc. All Rights Reserved.
duke@0 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
duke@0 4 *
duke@0 5 * This code is free software; you can redistribute it and/or modify it
duke@0 6 * under the terms of the GNU General Public License version 2 only, as
duke@0 7 * published by the Free Software Foundation.
duke@0 8 *
duke@0 9 * This code is distributed in the hope that it will be useful, but WITHOUT
duke@0 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
duke@0 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
duke@0 12 * version 2 for more details (a copy is included in the LICENSE file that
duke@0 13 * accompanied this code).
duke@0 14 *
duke@0 15 * You should have received a copy of the GNU General Public License version
duke@0 16 * 2 along with this work; if not, write to the Free Software Foundation,
duke@0 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
duke@0 18 *
duke@0 19 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
duke@0 20 * CA 95054 USA or visit www.sun.com if you need additional information or
duke@0 21 * have any questions.
duke@0 22 *
duke@0 23 */
duke@0 24
duke@0 25 #include "incls/_precompiled.incl"
duke@0 26 #include "incls/_c1_MacroAssembler_x86.cpp.incl"
duke@0 27
duke@0 28 int C1_MacroAssembler::lock_object(Register hdr, Register obj, Register disp_hdr, Register scratch, Label& slow_case) {
duke@0 29 const int aligned_mask = 3;
duke@0 30 const int hdr_offset = oopDesc::mark_offset_in_bytes();
duke@0 31 assert(hdr == rax, "hdr must be rax, for the cmpxchg instruction");
duke@0 32 assert(hdr != obj && hdr != disp_hdr && obj != disp_hdr, "registers must be different");
duke@0 33 assert(BytesPerWord == 4, "adjust aligned_mask and code");
duke@0 34 Label done;
duke@0 35 int null_check_offset = -1;
duke@0 36
duke@0 37 verify_oop(obj);
duke@0 38
duke@0 39 // save object being locked into the BasicObjectLock
duke@0 40 movl(Address(disp_hdr, BasicObjectLock::obj_offset_in_bytes()), obj);
duke@0 41
duke@0 42 if (UseBiasedLocking) {
duke@0 43 assert(scratch != noreg, "should have scratch register at this point");
duke@0 44 null_check_offset = biased_locking_enter(disp_hdr, obj, hdr, scratch, false, done, &slow_case);
duke@0 45 } else {
duke@0 46 null_check_offset = offset();
duke@0 47 }
duke@0 48
duke@0 49 // Load object header
duke@0 50 movl(hdr, Address(obj, hdr_offset));
duke@0 51 // and mark it as unlocked
duke@0 52 orl(hdr, markOopDesc::unlocked_value);
duke@0 53 // save unlocked object header into the displaced header location on the stack
duke@0 54 movl(Address(disp_hdr, 0), hdr);
duke@0 55 // test if object header is still the same (i.e. unlocked), and if so, store the
duke@0 56 // displaced header address in the object header - if it is not the same, get the
duke@0 57 // object header instead
duke@0 58 if (os::is_MP()) MacroAssembler::lock(); // must be immediately before cmpxchg!
duke@0 59 cmpxchg(disp_hdr, Address(obj, hdr_offset));
duke@0 60 // if the object header was the same, we're done
duke@0 61 if (PrintBiasedLockingStatistics) {
duke@0 62 cond_inc32(Assembler::equal,
duke@0 63 ExternalAddress((address)BiasedLocking::fast_path_entry_count_addr()));
duke@0 64 }
duke@0 65 jcc(Assembler::equal, done);
duke@0 66 // if the object header was not the same, it is now in the hdr register
duke@0 67 // => test if it is a stack pointer into the same stack (recursive locking), i.e.:
duke@0 68 //
duke@0 69 // 1) (hdr & aligned_mask) == 0
duke@0 70 // 2) rsp <= hdr
duke@0 71 // 3) hdr <= rsp + page_size
duke@0 72 //
duke@0 73 // these 3 tests can be done by evaluating the following expression:
duke@0 74 //
duke@0 75 // (hdr - rsp) & (aligned_mask - page_size)
duke@0 76 //
duke@0 77 // assuming both the stack pointer and page_size have their least
duke@0 78 // significant 2 bits cleared and page_size is a power of 2
duke@0 79 subl(hdr, rsp);
duke@0 80 andl(hdr, aligned_mask - os::vm_page_size());
duke@0 81 // for recursive locking, the result is zero => save it in the displaced header
duke@0 82 // location (NULL in the displaced hdr location indicates recursive locking)
duke@0 83 movl(Address(disp_hdr, 0), hdr);
duke@0 84 // otherwise we don't care about the result and handle locking via runtime call
duke@0 85 jcc(Assembler::notZero, slow_case);
duke@0 86 // done
duke@0 87 bind(done);
duke@0 88 return null_check_offset;
duke@0 89 }
duke@0 90
duke@0 91
duke@0 92 void C1_MacroAssembler::unlock_object(Register hdr, Register obj, Register disp_hdr, Label& slow_case) {
duke@0 93 const int aligned_mask = 3;
duke@0 94 const int hdr_offset = oopDesc::mark_offset_in_bytes();
duke@0 95 assert(disp_hdr == rax, "disp_hdr must be rax, for the cmpxchg instruction");
duke@0 96 assert(hdr != obj && hdr != disp_hdr && obj != disp_hdr, "registers must be different");
duke@0 97 assert(BytesPerWord == 4, "adjust aligned_mask and code");
duke@0 98 Label done;
duke@0 99
duke@0 100 if (UseBiasedLocking) {
duke@0 101 // load object
duke@0 102 movl(obj, Address(disp_hdr, BasicObjectLock::obj_offset_in_bytes()));
duke@0 103 biased_locking_exit(obj, hdr, done);
duke@0 104 }
duke@0 105
duke@0 106 // load displaced header
duke@0 107 movl(hdr, Address(disp_hdr, 0));
duke@0 108 // if the loaded hdr is NULL we had recursive locking
duke@0 109 testl(hdr, hdr);
duke@0 110 // if we had recursive locking, we are done
duke@0 111 jcc(Assembler::zero, done);
duke@0 112 if (!UseBiasedLocking) {
duke@0 113 // load object
duke@0 114 movl(obj, Address(disp_hdr, BasicObjectLock::obj_offset_in_bytes()));
duke@0 115 }
duke@0 116 verify_oop(obj);
duke@0 117 // test if object header is pointing to the displaced header, and if so, restore
duke@0 118 // the displaced header in the object - if the object header is not pointing to
duke@0 119 // the displaced header, get the object header instead
duke@0 120 if (os::is_MP()) MacroAssembler::lock(); // must be immediately before cmpxchg!
duke@0 121 cmpxchg(hdr, Address(obj, hdr_offset));
duke@0 122 // if the object header was not pointing to the displaced header,
duke@0 123 // we do unlocking via runtime call
duke@0 124 jcc(Assembler::notEqual, slow_case);
duke@0 125 // done
duke@0 126 bind(done);
duke@0 127 }
duke@0 128
duke@0 129
duke@0 130 // Defines obj, preserves var_size_in_bytes
duke@0 131 void C1_MacroAssembler::try_allocate(Register obj, Register var_size_in_bytes, int con_size_in_bytes, Register t1, Register t2, Label& slow_case) {
duke@0 132 if (UseTLAB) {
duke@0 133 tlab_allocate(obj, var_size_in_bytes, con_size_in_bytes, t1, t2, slow_case);
duke@0 134 } else {
duke@0 135 eden_allocate(obj, var_size_in_bytes, con_size_in_bytes, t1, slow_case);
duke@0 136 }
duke@0 137 }
duke@0 138
duke@0 139
duke@0 140 void C1_MacroAssembler::initialize_header(Register obj, Register klass, Register len, Register t1, Register t2) {
duke@0 141 assert_different_registers(obj, klass, len);
duke@0 142 if (UseBiasedLocking && !len->is_valid()) {
duke@0 143 assert_different_registers(obj, klass, len, t1, t2);
duke@0 144 movl(t1, Address(klass, Klass::prototype_header_offset_in_bytes() + klassOopDesc::klass_part_offset_in_bytes()));
duke@0 145 movl(Address(obj, oopDesc::mark_offset_in_bytes()), t1);
duke@0 146 } else {
duke@0 147 movl(Address(obj, oopDesc::mark_offset_in_bytes ()), (int)markOopDesc::prototype());
duke@0 148 }
duke@0 149
duke@0 150 movl(Address(obj, oopDesc::klass_offset_in_bytes()), klass);
duke@0 151 if (len->is_valid()) {
duke@0 152 movl(Address(obj, arrayOopDesc::length_offset_in_bytes()), len);
duke@0 153 }
duke@0 154 }
duke@0 155
duke@0 156
duke@0 157 // preserves obj, destroys len_in_bytes
duke@0 158 void C1_MacroAssembler::initialize_body(Register obj, Register len_in_bytes, int hdr_size_in_bytes, Register t1) {
duke@0 159 Label done;
duke@0 160 assert(obj != len_in_bytes && obj != t1 && t1 != len_in_bytes, "registers must be different");
duke@0 161 assert((hdr_size_in_bytes & (BytesPerWord - 1)) == 0, "header size is not a multiple of BytesPerWord");
duke@0 162 Register index = len_in_bytes;
duke@0 163 subl(index, hdr_size_in_bytes);
duke@0 164 jcc(Assembler::zero, done);
duke@0 165 // initialize topmost word, divide index by 2, check if odd and test if zero
duke@0 166 // note: for the remaining code to work, index must be a multiple of BytesPerWord
duke@0 167 #ifdef ASSERT
duke@0 168 { Label L;
duke@0 169 testl(index, BytesPerWord - 1);
duke@0 170 jcc(Assembler::zero, L);
duke@0 171 stop("index is not a multiple of BytesPerWord");
duke@0 172 bind(L);
duke@0 173 }
duke@0 174 #endif
duke@0 175 xorl(t1, t1); // use _zero reg to clear memory (shorter code)
duke@0 176 if (UseIncDec) {
duke@0 177 shrl(index, 3); // divide by 8 and set carry flag if bit 2 was set
duke@0 178 } else {
duke@0 179 shrl(index, 2); // use 2 instructions to avoid partial flag stall
duke@0 180 shrl(index, 1);
duke@0 181 }
duke@0 182 // index could have been not a multiple of 8 (i.e., bit 2 was set)
duke@0 183 { Label even;
duke@0 184 // note: if index was a multiple of 8, than it cannot
duke@0 185 // be 0 now otherwise it must have been 0 before
duke@0 186 // => if it is even, we don't need to check for 0 again
duke@0 187 jcc(Assembler::carryClear, even);
duke@0 188 // clear topmost word (no jump needed if conditional assignment would work here)
duke@0 189 movl(Address(obj, index, Address::times_8, hdr_size_in_bytes - 0*BytesPerWord), t1);
duke@0 190 // index could be 0 now, need to check again
duke@0 191 jcc(Assembler::zero, done);
duke@0 192 bind(even);
duke@0 193 }
duke@0 194 // initialize remaining object fields: rdx is a multiple of 2 now
duke@0 195 { Label loop;
duke@0 196 bind(loop);
duke@0 197 movl(Address(obj, index, Address::times_8, hdr_size_in_bytes - 1*BytesPerWord), t1);
duke@0 198 movl(Address(obj, index, Address::times_8, hdr_size_in_bytes - 2*BytesPerWord), t1);
duke@0 199 decrement(index);
duke@0 200 jcc(Assembler::notZero, loop);
duke@0 201 }
duke@0 202
duke@0 203 // done
duke@0 204 bind(done);
duke@0 205 }
duke@0 206
duke@0 207
duke@0 208 void C1_MacroAssembler::allocate_object(Register obj, Register t1, Register t2, int header_size, int object_size, Register klass, Label& slow_case) {
duke@0 209 assert(obj == rax, "obj must be in rax, for cmpxchg");
duke@0 210 assert(obj != t1 && obj != t2 && t1 != t2, "registers must be different"); // XXX really?
duke@0 211 assert(header_size >= 0 && object_size >= header_size, "illegal sizes");
duke@0 212
duke@0 213 try_allocate(obj, noreg, object_size * BytesPerWord, t1, t2, slow_case);
duke@0 214
duke@0 215 initialize_object(obj, klass, noreg, object_size * HeapWordSize, t1, t2);
duke@0 216 }
duke@0 217
duke@0 218 void C1_MacroAssembler::initialize_object(Register obj, Register klass, Register var_size_in_bytes, int con_size_in_bytes, Register t1, Register t2) {
duke@0 219 assert((con_size_in_bytes & MinObjAlignmentInBytesMask) == 0,
duke@0 220 "con_size_in_bytes is not multiple of alignment");
duke@0 221 const int hdr_size_in_bytes = oopDesc::header_size_in_bytes();
duke@0 222
duke@0 223 initialize_header(obj, klass, noreg, t1, t2);
duke@0 224
duke@0 225 // clear rest of allocated space
duke@0 226 const Register t1_zero = t1;
duke@0 227 const Register index = t2;
duke@0 228 const int threshold = 6 * BytesPerWord; // approximate break even point for code size (see comments below)
duke@0 229 if (var_size_in_bytes != noreg) {
duke@0 230 movl(index, var_size_in_bytes);
duke@0 231 initialize_body(obj, index, hdr_size_in_bytes, t1_zero);
duke@0 232 } else if (con_size_in_bytes <= threshold) {
duke@0 233 // use explicit null stores
duke@0 234 // code size = 2 + 3*n bytes (n = number of fields to clear)
duke@0 235 xorl(t1_zero, t1_zero); // use t1_zero reg to clear memory (shorter code)
duke@0 236 for (int i = hdr_size_in_bytes; i < con_size_in_bytes; i += BytesPerWord)
duke@0 237 movl(Address(obj, i), t1_zero);
duke@0 238 } else if (con_size_in_bytes > hdr_size_in_bytes) {
duke@0 239 // use loop to null out the fields
duke@0 240 // code size = 16 bytes for even n (n = number of fields to clear)
duke@0 241 // initialize last object field first if odd number of fields
duke@0 242 xorl(t1_zero, t1_zero); // use t1_zero reg to clear memory (shorter code)
duke@0 243 movl(index, (con_size_in_bytes - hdr_size_in_bytes) >> 3);
duke@0 244 // initialize last object field if constant size is odd
duke@0 245 if (((con_size_in_bytes - hdr_size_in_bytes) & 4) != 0)
duke@0 246 movl(Address(obj, con_size_in_bytes - (1*BytesPerWord)), t1_zero);
duke@0 247 // initialize remaining object fields: rdx is a multiple of 2
duke@0 248 { Label loop;
duke@0 249 bind(loop);
duke@0 250 movl(Address(obj, index, Address::times_8,
duke@0 251 hdr_size_in_bytes - (1*BytesPerWord)), t1_zero);
duke@0 252 movl(Address(obj, index, Address::times_8,
duke@0 253 hdr_size_in_bytes - (2*BytesPerWord)), t1_zero);
duke@0 254 decrement(index);
duke@0 255 jcc(Assembler::notZero, loop);
duke@0 256 }
duke@0 257 }
duke@0 258
duke@0 259 if (DTraceAllocProbes) {
duke@0 260 assert(obj == rax, "must be");
duke@0 261 call(RuntimeAddress(Runtime1::entry_for(Runtime1::dtrace_object_alloc_id)));
duke@0 262 }
duke@0 263
duke@0 264 verify_oop(obj);
duke@0 265 }
duke@0 266
duke@0 267 void C1_MacroAssembler::allocate_array(Register obj, Register len, Register t1, Register t2, int header_size, Address::ScaleFactor f, Register klass, Label& slow_case) {
duke@0 268 assert(obj == rax, "obj must be in rax, for cmpxchg");
duke@0 269 assert_different_registers(obj, len, t1, t2, klass);
duke@0 270
duke@0 271 // determine alignment mask
duke@0 272 assert(BytesPerWord == 4, "must be a multiple of 2 for masking code to work");
duke@0 273
duke@0 274 // check for negative or excessive length
duke@0 275 cmpl(len, max_array_allocation_length);
duke@0 276 jcc(Assembler::above, slow_case);
duke@0 277
duke@0 278 const Register arr_size = t2; // okay to be the same
duke@0 279 // align object end
duke@0 280 movl(arr_size, header_size * BytesPerWord + MinObjAlignmentInBytesMask);
duke@0 281 leal(arr_size, Address(arr_size, len, f));
duke@0 282 andl(arr_size, ~MinObjAlignmentInBytesMask);
duke@0 283
duke@0 284 try_allocate(obj, arr_size, 0, t1, t2, slow_case);
duke@0 285
duke@0 286 initialize_header(obj, klass, len, t1, t2);
duke@0 287
duke@0 288 // clear rest of allocated space
duke@0 289 const Register len_zero = len;
duke@0 290 initialize_body(obj, arr_size, header_size * BytesPerWord, len_zero);
duke@0 291
duke@0 292 if (DTraceAllocProbes) {
duke@0 293 assert(obj == rax, "must be");
duke@0 294 call(RuntimeAddress(Runtime1::entry_for(Runtime1::dtrace_object_alloc_id)));
duke@0 295 }
duke@0 296
duke@0 297 verify_oop(obj);
duke@0 298 }
duke@0 299
duke@0 300
duke@0 301
duke@0 302 void C1_MacroAssembler::inline_cache_check(Register receiver, Register iCache) {
duke@0 303 verify_oop(receiver);
duke@0 304 // explicit NULL check not needed since load from [klass_offset] causes a trap
duke@0 305 // check against inline cache
duke@0 306 assert(!MacroAssembler::needs_explicit_null_check(oopDesc::klass_offset_in_bytes()), "must add explicit null check");
duke@0 307 int start_offset = offset();
duke@0 308 cmpl(iCache, Address(receiver, oopDesc::klass_offset_in_bytes()));
duke@0 309 // if icache check fails, then jump to runtime routine
duke@0 310 // Note: RECEIVER must still contain the receiver!
duke@0 311 jump_cc(Assembler::notEqual,
duke@0 312 RuntimeAddress(SharedRuntime::get_ic_miss_stub()));
duke@0 313 assert(offset() - start_offset == 9, "check alignment in emit_method_entry");
duke@0 314 }
duke@0 315
duke@0 316
duke@0 317 void C1_MacroAssembler::method_exit(bool restore_frame) {
duke@0 318 if (restore_frame) {
duke@0 319 leave();
duke@0 320 }
duke@0 321 ret(0);
duke@0 322 }
duke@0 323
duke@0 324
duke@0 325 void C1_MacroAssembler::build_frame(int frame_size_in_bytes) {
duke@0 326 // Make sure there is enough stack space for this method's activation.
duke@0 327 // Note that we do this before doing an enter(). This matches the
duke@0 328 // ordering of C2's stack overflow check / rsp decrement and allows
duke@0 329 // the SharedRuntime stack overflow handling to be consistent
duke@0 330 // between the two compilers.
duke@0 331 generate_stack_overflow_check(frame_size_in_bytes);
duke@0 332
duke@0 333 enter();
duke@0 334 #ifdef TIERED
duke@0 335 // c2 leaves fpu stack dirty. Clean it on entry
duke@0 336 if (UseSSE < 2 ) {
duke@0 337 empty_FPU_stack();
duke@0 338 }
duke@0 339 #endif // TIERED
duke@0 340 decrement(rsp, frame_size_in_bytes); // does not emit code for frame_size == 0
duke@0 341 }
duke@0 342
duke@0 343
duke@0 344 void C1_MacroAssembler::unverified_entry(Register receiver, Register ic_klass) {
duke@0 345 if (C1Breakpoint) int3();
duke@0 346 inline_cache_check(receiver, ic_klass);
duke@0 347 }
duke@0 348
duke@0 349
duke@0 350 void C1_MacroAssembler::verified_entry() {
duke@0 351 if (C1Breakpoint)int3();
duke@0 352 // build frame
duke@0 353 verify_FPU(0, "method_entry");
duke@0 354 }
duke@0 355
duke@0 356
duke@0 357 #ifndef PRODUCT
duke@0 358
duke@0 359 void C1_MacroAssembler::verify_stack_oop(int stack_offset) {
duke@0 360 if (!VerifyOops) return;
duke@0 361 verify_oop_addr(Address(rsp, stack_offset));
duke@0 362 }
duke@0 363
duke@0 364 void C1_MacroAssembler::verify_not_null_oop(Register r) {
duke@0 365 if (!VerifyOops) return;
duke@0 366 Label not_null;
duke@0 367 testl(r, r);
duke@0 368 jcc(Assembler::notZero, not_null);
duke@0 369 stop("non-null oop required");
duke@0 370 bind(not_null);
duke@0 371 verify_oop(r);
duke@0 372 }
duke@0 373
duke@0 374 void C1_MacroAssembler::invalidate_registers(bool inv_rax, bool inv_rbx, bool inv_rcx, bool inv_rdx, bool inv_rsi, bool inv_rdi) {
duke@0 375 #ifdef ASSERT
duke@0 376 if (inv_rax) movl(rax, 0xDEAD);
duke@0 377 if (inv_rbx) movl(rbx, 0xDEAD);
duke@0 378 if (inv_rcx) movl(rcx, 0xDEAD);
duke@0 379 if (inv_rdx) movl(rdx, 0xDEAD);
duke@0 380 if (inv_rsi) movl(rsi, 0xDEAD);
duke@0 381 if (inv_rdi) movl(rdi, 0xDEAD);
duke@0 382 #endif
duke@0 383 }
duke@0 384
duke@0 385 #endif // ifndef PRODUCT