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