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