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