1 /*
   2  * Copyright 1999-2009 Sun Microsystems, Inc.  All Rights Reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  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 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
  20  * CA 95054 USA or visit www.sun.com if you need additional information or
  21  * have any questions.
  22  *
  23  */
  24 
  25 #include "incls/_precompiled.incl"
  26 #include "incls/_c1_MacroAssembler_sparc.cpp.incl"
  27 
  28 void C1_MacroAssembler::inline_cache_check(Register receiver, Register iCache) {
  29   Label L;
  30   const Register temp_reg = G3_scratch;
  31   // Note: needs more testing of out-of-line vs. inline slow case
  32   verify_oop(receiver);
  33   ld_ptr(receiver, oopDesc::klass_offset_in_bytes(), temp_reg);
  34   cmp(temp_reg, iCache);
  35   brx(Assembler::equal, true, Assembler::pt, L);
  36   delayed()->nop();
  37   AddressLiteral ic_miss(SharedRuntime::get_ic_miss_stub());
  38   jump_to(ic_miss, temp_reg);
  39   delayed()->nop();
  40   align(CodeEntryAlignment);
  41   bind(L);
  42 }
  43 
  44 
  45 void C1_MacroAssembler::method_exit(bool restore_frame) {
  46   // this code must be structured this way so that the return
  47   // instruction can be a safepoint.
  48   if (restore_frame) {
  49     restore();
  50   }
  51   retl();
  52   delayed()->nop();
  53 }
  54 
  55 
  56 void C1_MacroAssembler::explicit_null_check(Register base) {
  57   Unimplemented();
  58 }
  59 
  60 
  61 void C1_MacroAssembler::build_frame(int frame_size_in_bytes) {
  62 
  63   generate_stack_overflow_check(frame_size_in_bytes);
  64   // Create the frame.
  65   save_frame_c1(frame_size_in_bytes);
  66 }
  67 
  68 
  69 void C1_MacroAssembler::unverified_entry(Register receiver, Register ic_klass) {
  70   if (C1Breakpoint) breakpoint_trap();
  71   inline_cache_check(receiver, ic_klass);
  72 }
  73 
  74 
  75 void C1_MacroAssembler::verified_entry() {
  76   if (C1Breakpoint) breakpoint_trap();
  77   // build frame
  78   verify_FPU(0, "method_entry");
  79 }
  80 
  81 
  82 void C1_MacroAssembler::lock_object(Register Rmark, Register Roop, Register Rbox, Register Rscratch, Label& slow_case) {
  83   assert_different_registers(Rmark, Roop, Rbox, Rscratch);
  84 
  85   Label done;
  86 
  87   Address mark_addr(Roop, oopDesc::mark_offset_in_bytes());
  88 
  89   // The following move must be the first instruction of emitted since debug
  90   // information may be generated for it.
  91   // Load object header
  92   ld_ptr(mark_addr, Rmark);
  93 
  94   verify_oop(Roop);
  95 
  96   // save object being locked into the BasicObjectLock
  97   st_ptr(Roop, Rbox, BasicObjectLock::obj_offset_in_bytes());
  98 
  99   if (UseBiasedLocking) {
 100     biased_locking_enter(Roop, Rmark, Rscratch, done, &slow_case);
 101   }
 102 
 103   // Save Rbox in Rscratch to be used for the cas operation
 104   mov(Rbox, Rscratch);
 105 
 106   // and mark it unlocked
 107   or3(Rmark, markOopDesc::unlocked_value, Rmark);
 108 
 109   // save unlocked object header into the displaced header location on the stack
 110   st_ptr(Rmark, Rbox, BasicLock::displaced_header_offset_in_bytes());
 111 
 112   // compare object markOop with Rmark and if equal exchange Rscratch with object markOop
 113   assert(mark_addr.disp() == 0, "cas must take a zero displacement");
 114   casx_under_lock(mark_addr.base(), Rmark, Rscratch, (address)StubRoutines::Sparc::atomic_memory_operation_lock_addr());
 115   // if compare/exchange succeeded we found an unlocked object and we now have locked it
 116   // hence we are done
 117   cmp(Rmark, Rscratch);
 118   brx(Assembler::equal, false, Assembler::pt, done);
 119   delayed()->sub(Rscratch, SP, Rscratch);  //pull next instruction into delay slot
 120   // we did not find an unlocked object so see if this is a recursive case
 121   // sub(Rscratch, SP, Rscratch);
 122   assert(os::vm_page_size() > 0xfff, "page size too small - change the constant");
 123   andcc(Rscratch, 0xfffff003, Rscratch);
 124   brx(Assembler::notZero, false, Assembler::pn, slow_case);
 125   delayed()->st_ptr(Rscratch, Rbox, BasicLock::displaced_header_offset_in_bytes());
 126   bind(done);
 127 }
 128 
 129 
 130 void C1_MacroAssembler::unlock_object(Register Rmark, Register Roop, Register Rbox, Label& slow_case) {
 131   assert_different_registers(Rmark, Roop, Rbox);
 132 
 133   Label done;
 134 
 135   Address mark_addr(Roop, oopDesc::mark_offset_in_bytes());
 136   assert(mark_addr.disp() == 0, "cas must take a zero displacement");
 137 
 138   if (UseBiasedLocking) {
 139     // load the object out of the BasicObjectLock
 140     ld_ptr(Rbox, BasicObjectLock::obj_offset_in_bytes(), Roop);
 141     verify_oop(Roop);
 142     biased_locking_exit(mark_addr, Rmark, done);
 143   }
 144   // Test first it it is a fast recursive unlock
 145   ld_ptr(Rbox, BasicLock::displaced_header_offset_in_bytes(), Rmark);
 146   br_null(Rmark, false, Assembler::pt, done);
 147   delayed()->nop();
 148   if (!UseBiasedLocking) {
 149     // load object
 150     ld_ptr(Rbox, BasicObjectLock::obj_offset_in_bytes(), Roop);
 151     verify_oop(Roop);
 152   }
 153 
 154   // Check if it is still a light weight lock, this is is true if we see
 155   // the stack address of the basicLock in the markOop of the object
 156   casx_under_lock(mark_addr.base(), Rbox, Rmark, (address)StubRoutines::Sparc::atomic_memory_operation_lock_addr());
 157   cmp(Rbox, Rmark);
 158 
 159   brx(Assembler::notEqual, false, Assembler::pn, slow_case);
 160   delayed()->nop();
 161   // Done
 162   bind(done);
 163 }
 164 
 165 
 166 void C1_MacroAssembler::try_allocate(
 167   Register obj,                        // result: pointer to object after successful allocation
 168   Register var_size_in_bytes,          // object size in bytes if unknown at compile time; invalid otherwise
 169   int      con_size_in_bytes,          // object size in bytes if   known at compile time
 170   Register t1,                         // temp register
 171   Register t2,                         // temp register
 172   Label&   slow_case                   // continuation point if fast allocation fails
 173 ) {
 174   if (UseTLAB) {
 175     tlab_allocate(obj, var_size_in_bytes, con_size_in_bytes, t1, slow_case);
 176   } else {
 177     eden_allocate(obj, var_size_in_bytes, con_size_in_bytes, t1, t2, slow_case);
 178   }
 179 }
 180 
 181 
 182 void C1_MacroAssembler::initialize_header(Register obj, Register klass, Register len, Register t1, Register t2) {
 183   assert_different_registers(obj, klass, len, t1, t2);
 184   if (UseBiasedLocking && !len->is_valid()) {
 185     ld_ptr(klass, Klass::prototype_header_offset_in_bytes() + klassOopDesc::klass_part_offset_in_bytes(), t1);
 186   } else {
 187     set((intx)markOopDesc::prototype(), t1);
 188   }
 189   st_ptr(t1  , obj, oopDesc::mark_offset_in_bytes       ());
 190   st_ptr(klass, obj, oopDesc::klass_offset_in_bytes      ());
 191   if (len->is_valid()) st(len  , obj, arrayOopDesc::length_offset_in_bytes());
 192 }
 193 
 194 
 195 void C1_MacroAssembler::initialize_body(Register base, Register index) {
 196   assert_different_registers(base, index);
 197   Label loop;
 198   bind(loop);
 199   subcc(index, HeapWordSize, index);
 200   brx(Assembler::greaterEqual, true, Assembler::pt, loop);
 201   delayed()->st_ptr(G0, base, index);
 202 }
 203 
 204 
 205 void C1_MacroAssembler::allocate_object(
 206   Register obj,                        // result: pointer to object after successful allocation
 207   Register t1,                         // temp register
 208   Register t2,                         // temp register
 209   Register t3,                         // temp register
 210   int      hdr_size,                   // object header size in words
 211   int      obj_size,                   // object size in words
 212   Register klass,                      // object klass
 213   Label&   slow_case                   // continuation point if fast allocation fails
 214 ) {
 215   assert_different_registers(obj, t1, t2, t3, klass);
 216   assert(klass == G5, "must be G5");
 217 
 218   // allocate space & initialize header
 219   if (!is_simm13(obj_size * wordSize)) {
 220     // would need to use extra register to load
 221     // object size => go the slow case for now
 222     br(Assembler::always, false, Assembler::pt, slow_case);
 223     delayed()->nop();
 224     return;
 225   }
 226   try_allocate(obj, noreg, obj_size * wordSize, t2, t3, slow_case);
 227 
 228   initialize_object(obj, klass, noreg, obj_size * HeapWordSize, t1, t2);
 229 }
 230 
 231 void C1_MacroAssembler::initialize_object(
 232   Register obj,                        // result: pointer to object after successful allocation
 233   Register klass,                      // object klass
 234   Register var_size_in_bytes,          // object size in bytes if unknown at compile time; invalid otherwise
 235   int      con_size_in_bytes,          // object size in bytes if   known at compile time
 236   Register t1,                         // temp register
 237   Register t2                          // temp register
 238   ) {
 239   const int hdr_size_in_bytes = instanceOopDesc::base_offset_in_bytes();
 240 
 241   initialize_header(obj, klass, noreg, t1, t2);
 242 
 243 #ifdef ASSERT
 244   {
 245     Label ok;
 246     ld(klass, klassOopDesc::header_size() * HeapWordSize + Klass::layout_helper_offset_in_bytes(), t1);
 247     if (var_size_in_bytes != noreg) {
 248       cmp(t1, var_size_in_bytes);
 249     } else {
 250       cmp(t1, con_size_in_bytes);
 251     }
 252     brx(Assembler::equal, false, Assembler::pt, ok);
 253     delayed()->nop();
 254     stop("bad size in initialize_object");
 255     should_not_reach_here();
 256 
 257     bind(ok);
 258   }
 259 
 260 #endif
 261 
 262   // initialize body
 263   const int threshold = 5 * HeapWordSize;              // approximate break even point for code size
 264   if (var_size_in_bytes != noreg) {
 265     // use a loop
 266     add(obj, hdr_size_in_bytes, t1);               // compute address of first element
 267     sub(var_size_in_bytes, hdr_size_in_bytes, t2); // compute size of body
 268     initialize_body(t1, t2);
 269 #ifndef _LP64
 270   } else if (VM_Version::v9_instructions_work() && con_size_in_bytes < threshold * 2) {
 271     // on v9 we can do double word stores to fill twice as much space.
 272     assert(hdr_size_in_bytes % 8 == 0, "double word aligned");
 273     assert(con_size_in_bytes % 8 == 0, "double word aligned");
 274     for (int i = hdr_size_in_bytes; i < con_size_in_bytes; i += 2 * HeapWordSize) stx(G0, obj, i);
 275 #endif
 276   } else if (con_size_in_bytes <= threshold) {
 277     // use explicit NULL stores
 278     for (int i = hdr_size_in_bytes; i < con_size_in_bytes; i += HeapWordSize)     st_ptr(G0, obj, i);
 279   } else if (con_size_in_bytes > hdr_size_in_bytes) {
 280     // use a loop
 281     const Register base  = t1;
 282     const Register index = t2;
 283     add(obj, hdr_size_in_bytes, base);               // compute address of first element
 284     // compute index = number of words to clear
 285     set(con_size_in_bytes - hdr_size_in_bytes, index);
 286     initialize_body(base, index);
 287   }
 288 
 289   if (CURRENT_ENV->dtrace_alloc_probes()) {
 290     assert(obj == O0, "must be");
 291     call(CAST_FROM_FN_PTR(address, Runtime1::entry_for(Runtime1::dtrace_object_alloc_id)),
 292          relocInfo::runtime_call_type);
 293     delayed()->nop();
 294   }
 295 
 296   verify_oop(obj);
 297 }
 298 
 299 
 300 void C1_MacroAssembler::allocate_array(
 301   Register obj,                        // result: pointer to array after successful allocation
 302   Register len,                        // array length
 303   Register t1,                         // temp register
 304   Register t2,                         // temp register
 305   Register t3,                         // temp register
 306   int      hdr_size,                   // object header size in words
 307   int      elt_size,                   // element size in bytes
 308   Register klass,                      // object klass
 309   Label&   slow_case                   // continuation point if fast allocation fails
 310 ) {
 311   assert_different_registers(obj, len, t1, t2, t3, klass);
 312   assert(klass == G5, "must be G5");
 313   assert(t1 == G1, "must be G1");
 314 
 315   // determine alignment mask
 316   assert(!(BytesPerWord & 1), "must be a multiple of 2 for masking code to work");
 317 
 318   // check for negative or excessive length
 319   // note: the maximum length allowed is chosen so that arrays of any
 320   //       element size with this length are always smaller or equal
 321   //       to the largest integer (i.e., array size computation will
 322   //       not overflow)
 323   set(max_array_allocation_length, t1);
 324   cmp(len, t1);
 325   br(Assembler::greaterUnsigned, false, Assembler::pn, slow_case);
 326 
 327   // compute array size
 328   // note: if 0 <= len <= max_length, len*elt_size + header + alignment is
 329   //       smaller or equal to the largest integer; also, since top is always
 330   //       aligned, we can do the alignment here instead of at the end address
 331   //       computation
 332   const Register arr_size = t1;
 333   switch (elt_size) {
 334     case  1: delayed()->mov(len,    arr_size); break;
 335     case  2: delayed()->sll(len, 1, arr_size); break;
 336     case  4: delayed()->sll(len, 2, arr_size); break;
 337     case  8: delayed()->sll(len, 3, arr_size); break;
 338     default: ShouldNotReachHere();
 339   }
 340   add(arr_size, hdr_size * wordSize + MinObjAlignmentInBytesMask, arr_size); // add space for header & alignment
 341   and3(arr_size, ~MinObjAlignmentInBytesMask, arr_size);                     // align array size
 342 
 343   // allocate space & initialize header
 344   if (UseTLAB) {
 345     tlab_allocate(obj, arr_size, 0, t2, slow_case);
 346   } else {
 347     eden_allocate(obj, arr_size, 0, t2, t3, slow_case);
 348   }
 349   initialize_header(obj, klass, len, t2, t3);
 350 
 351   // initialize body
 352   const Register base  = t2;
 353   const Register index = t3;
 354   add(obj, hdr_size * wordSize, base);               // compute address of first element
 355   sub(arr_size, hdr_size * wordSize, index);         // compute index = number of words to clear
 356   initialize_body(base, index);
 357 
 358   if (CURRENT_ENV->dtrace_alloc_probes()) {
 359     assert(obj == O0, "must be");
 360     call(CAST_FROM_FN_PTR(address, Runtime1::entry_for(Runtime1::dtrace_object_alloc_id)),
 361          relocInfo::runtime_call_type);
 362     delayed()->nop();
 363   }
 364 
 365   verify_oop(obj);
 366 }
 367 
 368 
 369 #ifndef PRODUCT
 370 
 371 void C1_MacroAssembler::verify_stack_oop(int stack_offset) {
 372   if (!VerifyOops) return;
 373   verify_oop_addr(Address(SP, stack_offset + STACK_BIAS));
 374 }
 375 
 376 void C1_MacroAssembler::verify_not_null_oop(Register r) {
 377   Label not_null;
 378   br_zero(Assembler::notEqual, false, Assembler::pt, r, not_null);
 379   delayed()->nop();
 380   stop("non-null oop required");
 381   bind(not_null);
 382   if (!VerifyOops) return;
 383   verify_oop(r);
 384 }
 385 
 386 void C1_MacroAssembler::invalidate_registers(bool iregisters, bool lregisters, bool oregisters,
 387                                              Register preserve1, Register preserve2) {
 388   if (iregisters) {
 389     for (int i = 0; i < 6; i++) {
 390       Register r = as_iRegister(i);
 391       if (r != preserve1 && r != preserve2)  set(0xdead, r);
 392     }
 393   }
 394   if (oregisters) {
 395     for (int i = 0; i < 6; i++) {
 396       Register r = as_oRegister(i);
 397       if (r != preserve1 && r != preserve2)  set(0xdead, r);
 398     }
 399   }
 400   if (lregisters) {
 401     for (int i = 0; i < 8; i++) {
 402       Register r = as_lRegister(i);
 403       if (r != preserve1 && r != preserve2)  set(0xdead, r);
 404     }
 405   }
 406 }
 407 
 408 
 409 #endif