1 /*
   2  * Copyright (c) 1999, 2015, Oracle and/or its affiliates. All rights reserved.
   3  * Copyright (c) 2014, Red Hat Inc. All rights reserved.
   4  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   5  *
   6  * This code is free software; you can redistribute it and/or modify it
   7  * under the terms of the GNU General Public License version 2 only, as
   8  * published by the Free Software Foundation.
   9  *
  10  * This code is distributed in the hope that it will be useful, but WITHOUT
  11  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  12  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  13  * version 2 for more details (a copy is included in the LICENSE file that
  14  * accompanied this code).
  15  *
  16  * You should have received a copy of the GNU General Public License version
  17  * 2 along with this work; if not, write to the Free Software Foundation,
  18  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  19  *
  20  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  21  * or visit www.oracle.com if you need additional information or have any
  22  * questions.
  23  *
  24  */
  25 
  26 #include "precompiled.hpp"
  27 #include "c1/c1_MacroAssembler.hpp"
  28 #include "c1/c1_Runtime1.hpp"
  29 #include "classfile/systemDictionary.hpp"
  30 #include "gc/shared/collectedHeap.hpp"
  31 #include "interpreter/interpreter.hpp"
  32 #include "oops/arrayOop.hpp"
  33 #include "oops/markOop.hpp"
  34 #include "runtime/basicLock.hpp"
  35 #include "runtime/biasedLocking.hpp"
  36 #include "runtime/os.hpp"
  37 #include "runtime/sharedRuntime.hpp"
  38 #include "runtime/stubRoutines.hpp"
  39 
  40 void C1_MacroAssembler::float_cmp(bool is_float, int unordered_result,
  41                                   FloatRegister f0, FloatRegister f1,
  42                                   Register result)
  43 {
  44   Label done;
  45   if (is_float) {
  46     fcmps(f0, f1);
  47   } else {
  48     fcmpd(f0, f1);
  49   }
  50   if (unordered_result < 0) {
  51     // we want -1 for unordered or less than, 0 for equal and 1 for
  52     // greater than.
  53     cset(result, NE);  // Not equal or unordered
  54     cneg(result, result, LT);  // Less than or unordered
  55   } else {
  56     // we want -1 for less than, 0 for equal and 1 for unordered or
  57     // greater than.
  58     cset(result, NE);  // Not equal or unordered
  59     cneg(result, result, LO);  // Less than
  60   }
  61 }
  62 
  63 int C1_MacroAssembler::lock_object(Register hdr, Register obj, Register disp_hdr, Register scratch, Label& slow_case) {
  64   const int aligned_mask = BytesPerWord -1;
  65   const int hdr_offset = oopDesc::mark_offset_in_bytes();
  66   assert(hdr != obj && hdr != disp_hdr && obj != disp_hdr, "registers must be different");
  67   Label done, fail;
  68   int null_check_offset = -1;
  69 
  70   verify_oop(obj);
  71 
  72   shenandoah_store_addr_check(obj);
  73 
  74   // save object being locked into the BasicObjectLock
  75   str(obj, Address(disp_hdr, BasicObjectLock::obj_offset_in_bytes()));
  76 
  77   if (UseBiasedLocking) {
  78     assert(scratch != noreg, "should have scratch register at this point");
  79     null_check_offset = biased_locking_enter(disp_hdr, obj, hdr, scratch, false, done, &slow_case);
  80   } else {
  81     null_check_offset = offset();
  82   }
  83 
  84   // Load object header
  85   ldr(hdr, Address(obj, hdr_offset));
  86   // and mark it as unlocked
  87   orr(hdr, hdr, markOopDesc::unlocked_value);
  88   // save unlocked object header into the displaced header location on the stack
  89   str(hdr, Address(disp_hdr, 0));
  90   // test if object header is still the same (i.e. unlocked), and if so, store the
  91   // displaced header address in the object header - if it is not the same, get the
  92   // object header instead
  93   lea(rscratch2, Address(obj, hdr_offset));
  94   cmpxchgptr(hdr, disp_hdr, rscratch2, rscratch1, done, /*fallthough*/NULL);
  95   // if the object header was the same, we're done
  96   // if the object header was not the same, it is now in the hdr register
  97   // => test if it is a stack pointer into the same stack (recursive locking), i.e.:
  98   //
  99   // 1) (hdr & aligned_mask) == 0
 100   // 2) sp <= hdr
 101   // 3) hdr <= sp + page_size
 102   //
 103   // these 3 tests can be done by evaluating the following expression:
 104   //
 105   // (hdr - sp) & (aligned_mask - page_size)
 106   //
 107   // assuming both the stack pointer and page_size have their least
 108   // significant 2 bits cleared and page_size is a power of 2
 109   mov(rscratch1, sp);
 110   sub(hdr, hdr, rscratch1);
 111   ands(hdr, hdr, aligned_mask - os::vm_page_size());
 112   // for recursive locking, the result is zero => save it in the displaced header
 113   // location (NULL in the displaced hdr location indicates recursive locking)
 114   str(hdr, Address(disp_hdr, 0));
 115   // otherwise we don't care about the result and handle locking via runtime call
 116   cbnz(hdr, slow_case);
 117   // done
 118   bind(done);
 119   if (PrintBiasedLockingStatistics) {
 120     lea(rscratch2, ExternalAddress((address)BiasedLocking::fast_path_entry_count_addr()));
 121     addmw(Address(rscratch2, 0), 1, rscratch1);
 122   }
 123   return null_check_offset;
 124 }
 125 
 126 
 127 void C1_MacroAssembler::unlock_object(Register hdr, Register obj, Register disp_hdr, Label& slow_case) {
 128   const int aligned_mask = BytesPerWord -1;
 129   const int hdr_offset = oopDesc::mark_offset_in_bytes();
 130   assert(hdr != obj && hdr != disp_hdr && obj != disp_hdr, "registers must be different");
 131   Label done;
 132 
 133   if (UseBiasedLocking) {
 134     // load object
 135     ldr(obj, Address(disp_hdr, BasicObjectLock::obj_offset_in_bytes()));
 136 
 137     biased_locking_exit(obj, hdr, done);
 138   }
 139 
 140   // load displaced header
 141   ldr(hdr, Address(disp_hdr, 0));
 142   // if the loaded hdr is NULL we had recursive locking
 143   // if we had recursive locking, we are done
 144   cbz(hdr, done);
 145   if (!UseBiasedLocking) {
 146     // load object
 147     ldr(obj, Address(disp_hdr, BasicObjectLock::obj_offset_in_bytes()));
 148   }
 149   verify_oop(obj);
 150 
 151   shenandoah_store_addr_check(obj);
 152 
 153   // test if object header is pointing to the displaced header, and if so, restore
 154   // the displaced header in the object - if the object header is not pointing to
 155   // the displaced header, get the object header instead
 156   // if the object header was not pointing to the displaced header,
 157   // we do unlocking via runtime call
 158   if (hdr_offset) {
 159     lea(rscratch1, Address(obj, hdr_offset));
 160     cmpxchgptr(disp_hdr, hdr, rscratch1, rscratch2, done, &slow_case);
 161   } else {
 162     cmpxchgptr(disp_hdr, hdr, obj, rscratch2, done, &slow_case);
 163   }
 164   // done
 165   bind(done);
 166 }
 167 
 168 
 169 // Defines obj, preserves var_size_in_bytes
 170 void C1_MacroAssembler::try_allocate(Register obj, Register var_size_in_bytes, int con_size_in_bytes, Register t1, Register t2, Label& slow_case) {
 171   if (UseTLAB) {
 172     tlab_allocate(obj, var_size_in_bytes, con_size_in_bytes, t1, t2, slow_case);
 173   } else {
 174     eden_allocate(obj, var_size_in_bytes, con_size_in_bytes, t1, slow_case);
 175     incr_allocated_bytes(noreg, var_size_in_bytes, con_size_in_bytes, t1);
 176   }
 177 }
 178 
 179 void C1_MacroAssembler::initialize_header(Register obj, Register klass, Register len, Register t1, Register t2) {
 180   assert_different_registers(obj, klass, len);
 181   if (UseBiasedLocking && !len->is_valid()) {
 182     assert_different_registers(obj, klass, len, t1, t2);
 183     ldr(t1, Address(klass, Klass::prototype_header_offset()));
 184   } else {
 185     // This assumes that all prototype bits fit in an int32_t
 186     mov(t1, (int32_t)(intptr_t)markOopDesc::prototype());
 187   }
 188   str(t1, Address(obj, oopDesc::mark_offset_in_bytes()));
 189 
 190   if (UseCompressedClassPointers) { // Take care not to kill klass
 191     encode_klass_not_null(t1, klass);
 192     strw(t1, Address(obj, oopDesc::klass_offset_in_bytes()));
 193   } else {
 194     str(klass, Address(obj, oopDesc::klass_offset_in_bytes()));
 195   }
 196 
 197   if (len->is_valid()) {
 198     strw(len, Address(obj, arrayOopDesc::length_offset_in_bytes()));
 199   } else if (UseCompressedClassPointers) {
 200     store_klass_gap(obj, zr);
 201   }
 202 }
 203 
 204 // Zero words; len is in bytes
 205 // Destroys all registers except addr
 206 // len must be a nonzero multiple of wordSize
 207 void C1_MacroAssembler::zero_memory(Register addr, Register len, Register t1) {
 208   assert_different_registers(addr, len, t1, rscratch1, rscratch2);
 209 
 210 #ifdef ASSERT
 211   { Label L;
 212     tst(len, BytesPerWord - 1);
 213     br(Assembler::EQ, L);
 214     stop("len is not a multiple of BytesPerWord");
 215     bind(L);
 216   }
 217 #endif
 218 
 219 #ifndef PRODUCT
 220   block_comment("zero memory");
 221 #endif
 222 
 223   Label loop;
 224   Label entry;
 225 
 226 //  Algorithm:
 227 //
 228 //    scratch1 = cnt & 7;
 229 //    cnt -= scratch1;
 230 //    p += scratch1;
 231 //    switch (scratch1) {
 232 //      do {
 233 //        cnt -= 8;
 234 //          p[-8] = 0;
 235 //        case 7:
 236 //          p[-7] = 0;
 237 //        case 6:
 238 //          p[-6] = 0;
 239 //          // ...
 240 //        case 1:
 241 //          p[-1] = 0;
 242 //        case 0:
 243 //          p += 8;
 244 //      } while (cnt);
 245 //    }
 246 
 247   const int unroll = 8; // Number of str(zr) instructions we'll unroll
 248 
 249   lsr(len, len, LogBytesPerWord);
 250   andr(rscratch1, len, unroll - 1);  // tmp1 = cnt % unroll
 251   sub(len, len, rscratch1);      // cnt -= unroll
 252   // t1 always points to the end of the region we're about to zero
 253   add(t1, addr, rscratch1, Assembler::LSL, LogBytesPerWord);
 254   adr(rscratch2, entry);
 255   sub(rscratch2, rscratch2, rscratch1, Assembler::LSL, 2);
 256   br(rscratch2);
 257   bind(loop);
 258   sub(len, len, unroll);
 259   for (int i = -unroll; i < 0; i++)
 260     str(zr, Address(t1, i * wordSize));
 261   bind(entry);
 262   add(t1, t1, unroll * wordSize);
 263   cbnz(len, loop);
 264 }
 265 
 266 // preserves obj, destroys len_in_bytes
 267 void C1_MacroAssembler::initialize_body(Register obj, Register len_in_bytes, int hdr_size_in_bytes, Register t1) {
 268   Label done;
 269   assert(obj != len_in_bytes && obj != t1 && t1 != len_in_bytes, "registers must be different");
 270   assert((hdr_size_in_bytes & (BytesPerWord - 1)) == 0, "header size is not a multiple of BytesPerWord");
 271   Register index = len_in_bytes;
 272   // index is positive and ptr sized
 273   subs(index, index, hdr_size_in_bytes);
 274   br(Assembler::EQ, done);
 275   // note: for the remaining code to work, index must be a multiple of BytesPerWord
 276 #ifdef ASSERT
 277   { Label L;
 278     tst(index, BytesPerWord - 1);
 279     br(Assembler::EQ, L);
 280     stop("index is not a multiple of BytesPerWord");
 281     bind(L);
 282   }
 283 #endif
 284 
 285   // Preserve obj
 286   if (hdr_size_in_bytes)
 287     add(obj, obj, hdr_size_in_bytes);
 288   zero_memory(obj, index, t1);
 289   if (hdr_size_in_bytes)
 290     sub(obj, obj, hdr_size_in_bytes);
 291 
 292   // done
 293   bind(done);
 294 }
 295 
 296 
 297 void C1_MacroAssembler::allocate_object(Register obj, Register t1, Register t2, int header_size, int object_size, Register klass, Label& slow_case) {
 298   assert_different_registers(obj, t1, t2); // XXX really?
 299   assert(header_size >= 0 && object_size >= header_size, "illegal sizes");
 300 
 301   try_allocate(obj, noreg, object_size * BytesPerWord, t1, t2, slow_case);
 302 
 303   initialize_object(obj, klass, noreg, object_size * HeapWordSize, t1, t2);
 304 }
 305 
 306 void C1_MacroAssembler::initialize_object(Register obj, Register klass, Register var_size_in_bytes, int con_size_in_bytes, Register t1, Register t2) {
 307   assert((con_size_in_bytes & MinObjAlignmentInBytesMask) == 0,
 308          "con_size_in_bytes is not multiple of alignment");
 309   const int hdr_size_in_bytes = instanceOopDesc::header_size() * HeapWordSize;
 310 
 311   initialize_header(obj, klass, noreg, t1, t2);
 312 
 313   // clear rest of allocated space
 314   const Register index = t2;
 315   const int threshold = 16 * BytesPerWord;   // approximate break even point for code size (see comments below)
 316   if (var_size_in_bytes != noreg) {
 317     mov(index, var_size_in_bytes);
 318     initialize_body(obj, index, hdr_size_in_bytes, t1);
 319   } else if (con_size_in_bytes <= threshold) {
 320     // use explicit null stores
 321     int i = hdr_size_in_bytes;
 322     if (i < con_size_in_bytes && (con_size_in_bytes % (2 * BytesPerWord))) {
 323       str(zr, Address(obj, i));
 324       i += BytesPerWord;
 325     }
 326     for (; i < con_size_in_bytes; i += 2 * BytesPerWord)
 327       stp(zr, zr, Address(obj, i));
 328   } else if (con_size_in_bytes > hdr_size_in_bytes) {
 329     block_comment("zero memory");
 330     // use loop to null out the fields
 331 
 332     int words = (con_size_in_bytes - hdr_size_in_bytes) / BytesPerWord;
 333     mov(index,  words / 8);
 334 
 335     const int unroll = 8; // Number of str(zr) instructions we'll unroll
 336     int remainder = words % unroll;
 337     lea(rscratch1, Address(obj, hdr_size_in_bytes + remainder * BytesPerWord));
 338 
 339     Label entry_point, loop;
 340     b(entry_point);
 341 
 342     bind(loop);
 343     sub(index, index, 1);
 344     for (int i = -unroll; i < 0; i++) {
 345       if (-i == remainder)
 346         bind(entry_point);
 347       str(zr, Address(rscratch1, i * wordSize));
 348     }
 349     if (remainder == 0)
 350       bind(entry_point);
 351     add(rscratch1, rscratch1, unroll * wordSize);
 352     cbnz(index, loop);
 353 
 354   }
 355 
 356   membar(StoreStore);
 357 
 358   if (CURRENT_ENV->dtrace_alloc_probes()) {
 359     assert(obj == r0, "must be");
 360     far_call(RuntimeAddress(Runtime1::entry_for(Runtime1::dtrace_object_alloc_id)));
 361   }
 362 
 363   verify_oop(obj);
 364 }
 365 void C1_MacroAssembler::allocate_array(Register obj, Register len, Register t1, Register t2, int header_size, int f, Register klass, Label& slow_case) {
 366   assert_different_registers(obj, len, t1, t2, klass);
 367 
 368   // determine alignment mask
 369   assert(!(BytesPerWord & 1), "must be a multiple of 2 for masking code to work");
 370 
 371   // check for negative or excessive length
 372   mov(rscratch1, (int32_t)max_array_allocation_length);
 373   cmp(len, rscratch1);
 374   br(Assembler::HS, slow_case);
 375 
 376   const Register arr_size = t2; // okay to be the same
 377   // align object end
 378   mov(arr_size, (int32_t)header_size * BytesPerWord + MinObjAlignmentInBytesMask);
 379   add(arr_size, arr_size, len, ext::uxtw, f);
 380   andr(arr_size, arr_size, ~MinObjAlignmentInBytesMask);
 381 
 382   try_allocate(obj, arr_size, 0, t1, t2, slow_case);
 383 
 384   initialize_header(obj, klass, len, t1, t2);
 385 
 386   // clear rest of allocated space
 387   const Register len_zero = len;
 388   initialize_body(obj, arr_size, header_size * BytesPerWord, len_zero);
 389 
 390   membar(StoreStore);
 391 
 392   if (CURRENT_ENV->dtrace_alloc_probes()) {
 393     assert(obj == r0, "must be");
 394     far_call(RuntimeAddress(Runtime1::entry_for(Runtime1::dtrace_object_alloc_id)));
 395   }
 396 
 397   verify_oop(obj);
 398 }
 399 
 400 
 401 void C1_MacroAssembler::inline_cache_check(Register receiver, Register iCache) {
 402   verify_oop(receiver);
 403   // explicit NULL check not needed since load from [klass_offset] causes a trap
 404   // check against inline cache
 405   assert(!MacroAssembler::needs_explicit_null_check(oopDesc::klass_offset_in_bytes()), "must add explicit null check");
 406 
 407   cmp_klass(receiver, iCache, rscratch1);
 408 }
 409 
 410 
 411 void C1_MacroAssembler::build_frame(int framesize, int bang_size_in_bytes) {
 412   // If we have to make this method not-entrant we'll overwrite its
 413   // first instruction with a jump.  For this action to be legal we
 414   // must ensure that this first instruction is a B, BL, NOP, BKPT,
 415   // SVC, HVC, or SMC.  Make it a NOP.
 416   nop();
 417   assert(bang_size_in_bytes >= framesize, "stack bang size incorrect");
 418   // Make sure there is enough stack space for this method's activation.
 419   // Note that we do this before doing an enter().
 420   generate_stack_overflow_check(bang_size_in_bytes);
 421   MacroAssembler::build_frame(framesize + 2 * wordSize);
 422   if (NotifySimulator) {
 423     notify(Assembler::method_entry);
 424   }
 425 }
 426 
 427 void C1_MacroAssembler::remove_frame(int framesize) {
 428   MacroAssembler::remove_frame(framesize + 2 * wordSize);
 429   if (NotifySimulator) {
 430     notify(Assembler::method_reentry);
 431   }
 432 }
 433 
 434 
 435 void C1_MacroAssembler::verified_entry() {
 436 }
 437 
 438 #ifndef PRODUCT
 439 
 440 void C1_MacroAssembler::verify_stack_oop(int stack_offset) {
 441   if (!VerifyOops) return;
 442   verify_oop_addr(Address(sp, stack_offset), "oop");
 443 }
 444 
 445 void C1_MacroAssembler::verify_not_null_oop(Register r) {
 446   if (!VerifyOops) return;
 447   Label not_null;
 448   cbnz(r, not_null);
 449   stop("non-null oop required");
 450   bind(not_null);
 451   verify_oop(r);
 452 }
 453 
 454 void C1_MacroAssembler::invalidate_registers(bool inv_r0, bool inv_r19, bool inv_r2, bool inv_r3, bool inv_r4, bool inv_r5) {
 455 #ifdef ASSERT
 456   static int nn;
 457   if (inv_r0) mov(r0, 0xDEAD);
 458   if (inv_r19) mov(r19, 0xDEAD);
 459   if (inv_r2) mov(r2, nn++);
 460   if (inv_r3) mov(r3, 0xDEAD);
 461   if (inv_r4) mov(r4, 0xDEAD);
 462   if (inv_r5) mov(r5, 0xDEAD);
 463 #endif
 464 }
 465 #endif // ifndef PRODUCT