src/cpu/x86/vm/c1_MacroAssembler_x86.cpp

changeset 435
a61af66fc99e
child 548
ba764ed4b6f2
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/cpu/x86/vm/c1_MacroAssembler_x86.cpp	Sat Dec 01 00:00:00 2007 +0000
     1.3 @@ -0,0 +1,385 @@
     1.4 +/*
     1.5 + * Copyright 1999-2007 Sun Microsystems, Inc.  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 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
    1.23 + * CA 95054 USA or visit www.sun.com if you need additional information or
    1.24 + * have any questions.
    1.25 + *
    1.26 + */
    1.27 +
    1.28 +#include "incls/_precompiled.incl"
    1.29 +#include "incls/_c1_MacroAssembler_x86.cpp.incl"
    1.30 +
    1.31 +int C1_MacroAssembler::lock_object(Register hdr, Register obj, Register disp_hdr, Register scratch, Label& slow_case) {
    1.32 +  const int aligned_mask = 3;
    1.33 +  const int hdr_offset = oopDesc::mark_offset_in_bytes();
    1.34 +  assert(hdr == rax, "hdr must be rax, for the cmpxchg instruction");
    1.35 +  assert(hdr != obj && hdr != disp_hdr && obj != disp_hdr, "registers must be different");
    1.36 +  assert(BytesPerWord == 4, "adjust aligned_mask and code");
    1.37 +  Label done;
    1.38 +  int null_check_offset = -1;
    1.39 +
    1.40 +  verify_oop(obj);
    1.41 +
    1.42 +  // save object being locked into the BasicObjectLock
    1.43 +  movl(Address(disp_hdr, BasicObjectLock::obj_offset_in_bytes()), obj);
    1.44 +
    1.45 +  if (UseBiasedLocking) {
    1.46 +    assert(scratch != noreg, "should have scratch register at this point");
    1.47 +    null_check_offset = biased_locking_enter(disp_hdr, obj, hdr, scratch, false, done, &slow_case);
    1.48 +  } else {
    1.49 +    null_check_offset = offset();
    1.50 +  }
    1.51 +
    1.52 +  // Load object header
    1.53 +  movl(hdr, Address(obj, hdr_offset));
    1.54 +  // and mark it as unlocked
    1.55 +  orl(hdr, markOopDesc::unlocked_value);
    1.56 +  // save unlocked object header into the displaced header location on the stack
    1.57 +  movl(Address(disp_hdr, 0), hdr);
    1.58 +  // test if object header is still the same (i.e. unlocked), and if so, store the
    1.59 +  // displaced header address in the object header - if it is not the same, get the
    1.60 +  // object header instead
    1.61 +  if (os::is_MP()) MacroAssembler::lock(); // must be immediately before cmpxchg!
    1.62 +  cmpxchg(disp_hdr, Address(obj, hdr_offset));
    1.63 +  // if the object header was the same, we're done
    1.64 +  if (PrintBiasedLockingStatistics) {
    1.65 +    cond_inc32(Assembler::equal,
    1.66 +               ExternalAddress((address)BiasedLocking::fast_path_entry_count_addr()));
    1.67 +  }
    1.68 +  jcc(Assembler::equal, done);
    1.69 +  // if the object header was not the same, it is now in the hdr register
    1.70 +  // => test if it is a stack pointer into the same stack (recursive locking), i.e.:
    1.71 +  //
    1.72 +  // 1) (hdr & aligned_mask) == 0
    1.73 +  // 2) rsp <= hdr
    1.74 +  // 3) hdr <= rsp + page_size
    1.75 +  //
    1.76 +  // these 3 tests can be done by evaluating the following expression:
    1.77 +  //
    1.78 +  // (hdr - rsp) & (aligned_mask - page_size)
    1.79 +  //
    1.80 +  // assuming both the stack pointer and page_size have their least
    1.81 +  // significant 2 bits cleared and page_size is a power of 2
    1.82 +  subl(hdr, rsp);
    1.83 +  andl(hdr, aligned_mask - os::vm_page_size());
    1.84 +  // for recursive locking, the result is zero => save it in the displaced header
    1.85 +  // location (NULL in the displaced hdr location indicates recursive locking)
    1.86 +  movl(Address(disp_hdr, 0), hdr);
    1.87 +  // otherwise we don't care about the result and handle locking via runtime call
    1.88 +  jcc(Assembler::notZero, slow_case);
    1.89 +  // done
    1.90 +  bind(done);
    1.91 +  return null_check_offset;
    1.92 +}
    1.93 +
    1.94 +
    1.95 +void C1_MacroAssembler::unlock_object(Register hdr, Register obj, Register disp_hdr, Label& slow_case) {
    1.96 +  const int aligned_mask = 3;
    1.97 +  const int hdr_offset = oopDesc::mark_offset_in_bytes();
    1.98 +  assert(disp_hdr == rax, "disp_hdr must be rax, for the cmpxchg instruction");
    1.99 +  assert(hdr != obj && hdr != disp_hdr && obj != disp_hdr, "registers must be different");
   1.100 +  assert(BytesPerWord == 4, "adjust aligned_mask and code");
   1.101 +  Label done;
   1.102 +
   1.103 +  if (UseBiasedLocking) {
   1.104 +    // load object
   1.105 +    movl(obj, Address(disp_hdr, BasicObjectLock::obj_offset_in_bytes()));
   1.106 +    biased_locking_exit(obj, hdr, done);
   1.107 +  }
   1.108 +
   1.109 +  // load displaced header
   1.110 +  movl(hdr, Address(disp_hdr, 0));
   1.111 +  // if the loaded hdr is NULL we had recursive locking
   1.112 +  testl(hdr, hdr);
   1.113 +  // if we had recursive locking, we are done
   1.114 +  jcc(Assembler::zero, done);
   1.115 +  if (!UseBiasedLocking) {
   1.116 +    // load object
   1.117 +    movl(obj, Address(disp_hdr, BasicObjectLock::obj_offset_in_bytes()));
   1.118 +  }
   1.119 +  verify_oop(obj);
   1.120 +  // test if object header is pointing to the displaced header, and if so, restore
   1.121 +  // the displaced header in the object - if the object header is not pointing to
   1.122 +  // the displaced header, get the object header instead
   1.123 +  if (os::is_MP()) MacroAssembler::lock(); // must be immediately before cmpxchg!
   1.124 +  cmpxchg(hdr, Address(obj, hdr_offset));
   1.125 +  // if the object header was not pointing to the displaced header,
   1.126 +  // we do unlocking via runtime call
   1.127 +  jcc(Assembler::notEqual, slow_case);
   1.128 +  // done
   1.129 +  bind(done);
   1.130 +}
   1.131 +
   1.132 +
   1.133 +// Defines obj, preserves var_size_in_bytes
   1.134 +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.135 +  if (UseTLAB) {
   1.136 +    tlab_allocate(obj, var_size_in_bytes, con_size_in_bytes, t1, t2, slow_case);
   1.137 +  } else {
   1.138 +    eden_allocate(obj, var_size_in_bytes, con_size_in_bytes, t1, slow_case);
   1.139 +  }
   1.140 +}
   1.141 +
   1.142 +
   1.143 +void C1_MacroAssembler::initialize_header(Register obj, Register klass, Register len, Register t1, Register t2) {
   1.144 +  assert_different_registers(obj, klass, len);
   1.145 +  if (UseBiasedLocking && !len->is_valid()) {
   1.146 +    assert_different_registers(obj, klass, len, t1, t2);
   1.147 +    movl(t1, Address(klass, Klass::prototype_header_offset_in_bytes() + klassOopDesc::klass_part_offset_in_bytes()));
   1.148 +    movl(Address(obj, oopDesc::mark_offset_in_bytes()), t1);
   1.149 +  } else {
   1.150 +    movl(Address(obj, oopDesc::mark_offset_in_bytes ()), (int)markOopDesc::prototype());
   1.151 +  }
   1.152 +
   1.153 +  movl(Address(obj, oopDesc::klass_offset_in_bytes()), klass);
   1.154 +  if (len->is_valid()) {
   1.155 +    movl(Address(obj, arrayOopDesc::length_offset_in_bytes()), len);
   1.156 +  }
   1.157 +}
   1.158 +
   1.159 +
   1.160 +// preserves obj, destroys len_in_bytes
   1.161 +void C1_MacroAssembler::initialize_body(Register obj, Register len_in_bytes, int hdr_size_in_bytes, Register t1) {
   1.162 +  Label done;
   1.163 +  assert(obj != len_in_bytes && obj != t1 && t1 != len_in_bytes, "registers must be different");
   1.164 +  assert((hdr_size_in_bytes & (BytesPerWord - 1)) == 0, "header size is not a multiple of BytesPerWord");
   1.165 +  Register index = len_in_bytes;
   1.166 +  subl(index, hdr_size_in_bytes);
   1.167 +  jcc(Assembler::zero, done);
   1.168 +  // initialize topmost word, divide index by 2, check if odd and test if zero
   1.169 +  // note: for the remaining code to work, index must be a multiple of BytesPerWord
   1.170 +#ifdef ASSERT
   1.171 +  { Label L;
   1.172 +    testl(index, BytesPerWord - 1);
   1.173 +    jcc(Assembler::zero, L);
   1.174 +    stop("index is not a multiple of BytesPerWord");
   1.175 +    bind(L);
   1.176 +  }
   1.177 +#endif
   1.178 +  xorl(t1, t1);      // use _zero reg to clear memory (shorter code)
   1.179 +  if (UseIncDec) {
   1.180 +    shrl(index, 3);  // divide by 8 and set carry flag if bit 2 was set
   1.181 +  } else {
   1.182 +    shrl(index, 2);  // use 2 instructions to avoid partial flag stall
   1.183 +    shrl(index, 1);
   1.184 +  }
   1.185 +  // index could have been not a multiple of 8 (i.e., bit 2 was set)
   1.186 +  { Label even;
   1.187 +    // note: if index was a multiple of 8, than it cannot
   1.188 +    //       be 0 now otherwise it must have been 0 before
   1.189 +    //       => if it is even, we don't need to check for 0 again
   1.190 +    jcc(Assembler::carryClear, even);
   1.191 +    // clear topmost word (no jump needed if conditional assignment would work here)
   1.192 +    movl(Address(obj, index, Address::times_8, hdr_size_in_bytes - 0*BytesPerWord), t1);
   1.193 +    // index could be 0 now, need to check again
   1.194 +    jcc(Assembler::zero, done);
   1.195 +    bind(even);
   1.196 +  }
   1.197 +  // initialize remaining object fields: rdx is a multiple of 2 now
   1.198 +  { Label loop;
   1.199 +    bind(loop);
   1.200 +    movl(Address(obj, index, Address::times_8, hdr_size_in_bytes - 1*BytesPerWord), t1);
   1.201 +    movl(Address(obj, index, Address::times_8, hdr_size_in_bytes - 2*BytesPerWord), t1);
   1.202 +    decrement(index);
   1.203 +    jcc(Assembler::notZero, loop);
   1.204 +  }
   1.205 +
   1.206 +  // done
   1.207 +  bind(done);
   1.208 +}
   1.209 +
   1.210 +
   1.211 +void C1_MacroAssembler::allocate_object(Register obj, Register t1, Register t2, int header_size, int object_size, Register klass, Label& slow_case) {
   1.212 +  assert(obj == rax, "obj must be in rax, for cmpxchg");
   1.213 +  assert(obj != t1 && obj != t2 && t1 != t2, "registers must be different"); // XXX really?
   1.214 +  assert(header_size >= 0 && object_size >= header_size, "illegal sizes");
   1.215 +
   1.216 +  try_allocate(obj, noreg, object_size * BytesPerWord, t1, t2, slow_case);
   1.217 +
   1.218 +  initialize_object(obj, klass, noreg, object_size * HeapWordSize, t1, t2);
   1.219 +}
   1.220 +
   1.221 +void C1_MacroAssembler::initialize_object(Register obj, Register klass, Register var_size_in_bytes, int con_size_in_bytes, Register t1, Register t2) {
   1.222 +  assert((con_size_in_bytes & MinObjAlignmentInBytesMask) == 0,
   1.223 +         "con_size_in_bytes is not multiple of alignment");
   1.224 +  const int hdr_size_in_bytes = oopDesc::header_size_in_bytes();
   1.225 +
   1.226 +  initialize_header(obj, klass, noreg, t1, t2);
   1.227 +
   1.228 +  // clear rest of allocated space
   1.229 +  const Register t1_zero = t1;
   1.230 +  const Register index = t2;
   1.231 +  const int threshold = 6 * BytesPerWord;   // approximate break even point for code size (see comments below)
   1.232 +  if (var_size_in_bytes != noreg) {
   1.233 +    movl(index, var_size_in_bytes);
   1.234 +    initialize_body(obj, index, hdr_size_in_bytes, t1_zero);
   1.235 +  } else if (con_size_in_bytes <= threshold) {
   1.236 +    // use explicit null stores
   1.237 +    // code size = 2 + 3*n bytes (n = number of fields to clear)
   1.238 +    xorl(t1_zero, t1_zero); // use t1_zero reg to clear memory (shorter code)
   1.239 +    for (int i = hdr_size_in_bytes; i < con_size_in_bytes; i += BytesPerWord)
   1.240 +      movl(Address(obj, i), t1_zero);
   1.241 +  } else if (con_size_in_bytes > hdr_size_in_bytes) {
   1.242 +    // use loop to null out the fields
   1.243 +    // code size = 16 bytes for even n (n = number of fields to clear)
   1.244 +    // initialize last object field first if odd number of fields
   1.245 +    xorl(t1_zero, t1_zero); // use t1_zero reg to clear memory (shorter code)
   1.246 +    movl(index, (con_size_in_bytes - hdr_size_in_bytes) >> 3);
   1.247 +    // initialize last object field if constant size is odd
   1.248 +    if (((con_size_in_bytes - hdr_size_in_bytes) & 4) != 0)
   1.249 +      movl(Address(obj, con_size_in_bytes - (1*BytesPerWord)), t1_zero);
   1.250 +    // initialize remaining object fields: rdx is a multiple of 2
   1.251 +    { Label loop;
   1.252 +      bind(loop);
   1.253 +      movl(Address(obj, index, Address::times_8,
   1.254 +        hdr_size_in_bytes - (1*BytesPerWord)), t1_zero);
   1.255 +      movl(Address(obj, index, Address::times_8,
   1.256 +        hdr_size_in_bytes - (2*BytesPerWord)), t1_zero);
   1.257 +      decrement(index);
   1.258 +      jcc(Assembler::notZero, loop);
   1.259 +    }
   1.260 +  }
   1.261 +
   1.262 +  if (DTraceAllocProbes) {
   1.263 +    assert(obj == rax, "must be");
   1.264 +    call(RuntimeAddress(Runtime1::entry_for(Runtime1::dtrace_object_alloc_id)));
   1.265 +  }
   1.266 +
   1.267 +  verify_oop(obj);
   1.268 +}
   1.269 +
   1.270 +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.271 +  assert(obj == rax, "obj must be in rax, for cmpxchg");
   1.272 +  assert_different_registers(obj, len, t1, t2, klass);
   1.273 +
   1.274 +  // determine alignment mask
   1.275 +  assert(BytesPerWord == 4, "must be a multiple of 2 for masking code to work");
   1.276 +
   1.277 +  // check for negative or excessive length
   1.278 +  cmpl(len, max_array_allocation_length);
   1.279 +  jcc(Assembler::above, slow_case);
   1.280 +
   1.281 +  const Register arr_size = t2; // okay to be the same
   1.282 +  // align object end
   1.283 +  movl(arr_size, header_size * BytesPerWord + MinObjAlignmentInBytesMask);
   1.284 +  leal(arr_size, Address(arr_size, len, f));
   1.285 +  andl(arr_size, ~MinObjAlignmentInBytesMask);
   1.286 +
   1.287 +  try_allocate(obj, arr_size, 0, t1, t2, slow_case);
   1.288 +
   1.289 +  initialize_header(obj, klass, len, t1, t2);
   1.290 +
   1.291 +  // clear rest of allocated space
   1.292 +  const Register len_zero = len;
   1.293 +  initialize_body(obj, arr_size, header_size * BytesPerWord, len_zero);
   1.294 +
   1.295 +  if (DTraceAllocProbes) {
   1.296 +    assert(obj == rax, "must be");
   1.297 +    call(RuntimeAddress(Runtime1::entry_for(Runtime1::dtrace_object_alloc_id)));
   1.298 +  }
   1.299 +
   1.300 +  verify_oop(obj);
   1.301 +}
   1.302 +
   1.303 +
   1.304 +
   1.305 +void C1_MacroAssembler::inline_cache_check(Register receiver, Register iCache) {
   1.306 +  verify_oop(receiver);
   1.307 +  // explicit NULL check not needed since load from [klass_offset] causes a trap
   1.308 +  // check against inline cache
   1.309 +  assert(!MacroAssembler::needs_explicit_null_check(oopDesc::klass_offset_in_bytes()), "must add explicit null check");
   1.310 +  int start_offset = offset();
   1.311 +  cmpl(iCache, Address(receiver, oopDesc::klass_offset_in_bytes()));
   1.312 +  // if icache check fails, then jump to runtime routine
   1.313 +  // Note: RECEIVER must still contain the receiver!
   1.314 +  jump_cc(Assembler::notEqual,
   1.315 +          RuntimeAddress(SharedRuntime::get_ic_miss_stub()));
   1.316 +  assert(offset() - start_offset == 9, "check alignment in emit_method_entry");
   1.317 +}
   1.318 +
   1.319 +
   1.320 +void C1_MacroAssembler::method_exit(bool restore_frame) {
   1.321 +  if (restore_frame) {
   1.322 +    leave();
   1.323 +  }
   1.324 +  ret(0);
   1.325 +}
   1.326 +
   1.327 +
   1.328 +void C1_MacroAssembler::build_frame(int frame_size_in_bytes) {
   1.329 +  // Make sure there is enough stack space for this method's activation.
   1.330 +  // Note that we do this before doing an enter(). This matches the
   1.331 +  // ordering of C2's stack overflow check / rsp decrement and allows
   1.332 +  // the SharedRuntime stack overflow handling to be consistent
   1.333 +  // between the two compilers.
   1.334 +  generate_stack_overflow_check(frame_size_in_bytes);
   1.335 +
   1.336 +  enter();
   1.337 +#ifdef TIERED
   1.338 +  // c2 leaves fpu stack dirty. Clean it on entry
   1.339 +  if (UseSSE < 2 ) {
   1.340 +    empty_FPU_stack();
   1.341 +  }
   1.342 +#endif // TIERED
   1.343 +  decrement(rsp, frame_size_in_bytes); // does not emit code for frame_size == 0
   1.344 +}
   1.345 +
   1.346 +
   1.347 +void C1_MacroAssembler::unverified_entry(Register receiver, Register ic_klass) {
   1.348 +  if (C1Breakpoint) int3();
   1.349 +  inline_cache_check(receiver, ic_klass);
   1.350 +}
   1.351 +
   1.352 +
   1.353 +void C1_MacroAssembler::verified_entry() {
   1.354 +  if (C1Breakpoint)int3();
   1.355 +  // build frame
   1.356 +  verify_FPU(0, "method_entry");
   1.357 +}
   1.358 +
   1.359 +
   1.360 +#ifndef PRODUCT
   1.361 +
   1.362 +void C1_MacroAssembler::verify_stack_oop(int stack_offset) {
   1.363 +  if (!VerifyOops) return;
   1.364 +  verify_oop_addr(Address(rsp, stack_offset));
   1.365 +}
   1.366 +
   1.367 +void C1_MacroAssembler::verify_not_null_oop(Register r) {
   1.368 +  if (!VerifyOops) return;
   1.369 +  Label not_null;
   1.370 +  testl(r, r);
   1.371 +  jcc(Assembler::notZero, not_null);
   1.372 +  stop("non-null oop required");
   1.373 +  bind(not_null);
   1.374 +  verify_oop(r);
   1.375 +}
   1.376 +
   1.377 +void C1_MacroAssembler::invalidate_registers(bool inv_rax, bool inv_rbx, bool inv_rcx, bool inv_rdx, bool inv_rsi, bool inv_rdi) {
   1.378 +#ifdef ASSERT
   1.379 +  if (inv_rax) movl(rax, 0xDEAD);
   1.380 +  if (inv_rbx) movl(rbx, 0xDEAD);
   1.381 +  if (inv_rcx) movl(rcx, 0xDEAD);
   1.382 +  if (inv_rdx) movl(rdx, 0xDEAD);
   1.383 +  if (inv_rsi) movl(rsi, 0xDEAD);
   1.384 +  if (inv_rdi) movl(rdi, 0xDEAD);
   1.385 +#endif
   1.386 +}
   1.387 +
   1.388 +#endif // ifndef PRODUCT

mercurial