1 /*
   2  * Copyright (c) 1999, 2018, Oracle and/or its affiliates. All rights reserved.
   3  * Copyright (c) 2012, 2018 SAP SE. 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 #include "utilities/align.hpp"
  40 
  41 
  42 void C1_MacroAssembler::inline_cache_check(Register receiver, Register iCache) {
  43   const Register temp_reg = R12_scratch2;
  44   Label Lmiss;
  45 
  46   verify_oop(receiver);
  47   MacroAssembler::null_check(receiver, oopDesc::klass_offset_in_bytes(), &Lmiss);
  48   load_klass(temp_reg, receiver);
  49 
  50   if (TrapBasedICMissChecks && TrapBasedNullChecks) {
  51     trap_ic_miss_check(temp_reg, iCache);
  52   } else {
  53     Label Lok;
  54     cmpd(CCR0, temp_reg, iCache);
  55     beq(CCR0, Lok);
  56     bind(Lmiss);
  57     //load_const_optimized(temp_reg, SharedRuntime::get_ic_miss_stub(), R0);
  58     calculate_address_from_global_toc(temp_reg, SharedRuntime::get_ic_miss_stub(), true, true, false);
  59     mtctr(temp_reg);
  60     bctr();
  61     align(32, 12);
  62     bind(Lok);
  63   }
  64 }
  65 
  66 
  67 void C1_MacroAssembler::explicit_null_check(Register base) {
  68   Unimplemented();
  69 }
  70 
  71 
  72 void C1_MacroAssembler::build_frame(int frame_size_in_bytes, int bang_size_in_bytes) {
  73   // Avoid stack bang as first instruction. It may get overwritten by patch_verified_entry.
  74   const Register return_pc = R20;
  75   mflr(return_pc);
  76 
  77   // Make sure there is enough stack space for this method's activation.
  78   assert(bang_size_in_bytes >= frame_size_in_bytes, "stack bang size incorrect");
  79   generate_stack_overflow_check(bang_size_in_bytes);
  80 
  81   std(return_pc, _abi(lr), R1_SP);     // SP->lr = return_pc
  82   push_frame(frame_size_in_bytes, R0); // SP -= frame_size_in_bytes
  83 }
  84 
  85 
  86 void C1_MacroAssembler::unverified_entry(Register receiver, Register ic_klass) {
  87   Unimplemented(); // Currently unused.
  88   //if (C1Breakpoint) illtrap();
  89   //inline_cache_check(receiver, ic_klass);
  90 }
  91 
  92 
  93 void C1_MacroAssembler::verified_entry() {
  94   if (C1Breakpoint) illtrap();
  95   // build frame
  96 }
  97 
  98 
  99 void C1_MacroAssembler::lock_object(Register Rmark, Register Roop, Register Rbox, Register Rscratch, Label& slow_case) {
 100   assert_different_registers(Rmark, Roop, Rbox, Rscratch);
 101 
 102   Label done, cas_failed, slow_int;
 103 
 104   // The following move must be the first instruction of emitted since debug
 105   // information may be generated for it.
 106   // Load object header.
 107   ld(Rmark, oopDesc::mark_offset_in_bytes(), Roop);
 108 
 109   verify_oop(Roop);
 110 
 111   // Save object being locked into the BasicObjectLock...
 112   std(Roop, BasicObjectLock::obj_offset_in_bytes(), Rbox);
 113 
 114   if (UseBiasedLocking) {
 115     biased_locking_enter(CCR0, Roop, Rmark, Rscratch, R0, done, &slow_int);
 116   }
 117 
 118   // ... and mark it unlocked.
 119   ori(Rmark, Rmark, markOopDesc::unlocked_value);
 120 
 121   // Save unlocked object header into the displaced header location on the stack.
 122   std(Rmark, BasicLock::displaced_header_offset_in_bytes(), Rbox);
 123 
 124   // Compare object markOop with Rmark and if equal exchange Rscratch with object markOop.
 125   assert(oopDesc::mark_offset_in_bytes() == 0, "cas must take a zero displacement");
 126   cmpxchgd(/*flag=*/CCR0,
 127            /*current_value=*/Rscratch,
 128            /*compare_value=*/Rmark,
 129            /*exchange_value=*/Rbox,
 130            /*where=*/Roop/*+0==mark_offset_in_bytes*/,
 131            MacroAssembler::MemBarRel | MacroAssembler::MemBarAcq,
 132            MacroAssembler::cmpxchgx_hint_acquire_lock(),
 133            noreg,
 134            &cas_failed,
 135            /*check without membar and ldarx first*/true);
 136   // If compare/exchange succeeded we found an unlocked object and we now have locked it
 137   // hence we are done.
 138   b(done);
 139 
 140   bind(slow_int);
 141   b(slow_case); // far
 142 
 143   bind(cas_failed);
 144   // We did not find an unlocked object so see if this is a recursive case.
 145   sub(Rscratch, Rscratch, R1_SP);
 146   load_const_optimized(R0, (~(os::vm_page_size()-1) | markOopDesc::lock_mask_in_place));
 147   and_(R0/*==0?*/, Rscratch, R0);
 148   std(R0/*==0, perhaps*/, BasicLock::displaced_header_offset_in_bytes(), Rbox);
 149   bne(CCR0, slow_int);
 150 
 151   bind(done);
 152 }
 153 
 154 
 155 void C1_MacroAssembler::unlock_object(Register Rmark, Register Roop, Register Rbox, Label& slow_case) {
 156   assert_different_registers(Rmark, Roop, Rbox);
 157 
 158   Label slow_int, done;
 159 
 160   Address mark_addr(Roop, oopDesc::mark_offset_in_bytes());
 161   assert(mark_addr.disp() == 0, "cas must take a zero displacement");
 162 
 163   if (UseBiasedLocking) {
 164     // Load the object out of the BasicObjectLock.
 165     ld(Roop, BasicObjectLock::obj_offset_in_bytes(), Rbox);
 166     verify_oop(Roop);
 167     biased_locking_exit(CCR0, Roop, R0, done);
 168   }
 169   // Test first it it is a fast recursive unlock.
 170   ld(Rmark, BasicLock::displaced_header_offset_in_bytes(), Rbox);
 171   cmpdi(CCR0, Rmark, 0);
 172   beq(CCR0, done);
 173   if (!UseBiasedLocking) {
 174     // Load object.
 175     ld(Roop, BasicObjectLock::obj_offset_in_bytes(), Rbox);
 176     verify_oop(Roop);
 177   }
 178 
 179   // Check if it is still a light weight lock, this is is true if we see
 180   // the stack address of the basicLock in the markOop of the object.
 181   cmpxchgd(/*flag=*/CCR0,
 182            /*current_value=*/R0,
 183            /*compare_value=*/Rbox,
 184            /*exchange_value=*/Rmark,
 185            /*where=*/Roop,
 186            MacroAssembler::MemBarRel,
 187            MacroAssembler::cmpxchgx_hint_release_lock(),
 188            noreg,
 189            &slow_int);
 190   b(done);
 191   bind(slow_int);
 192   b(slow_case); // far
 193 
 194   // Done
 195   bind(done);
 196 }
 197 
 198 
 199 void C1_MacroAssembler::try_allocate(
 200   Register obj,                        // result: pointer to object after successful allocation
 201   Register var_size_in_bytes,          // object size in bytes if unknown at compile time; invalid otherwise
 202   int      con_size_in_bytes,          // object size in bytes if   known at compile time
 203   Register t1,                         // temp register, must be global register for incr_allocated_bytes
 204   Register t2,                         // temp register
 205   Label&   slow_case                   // continuation point if fast allocation fails
 206 ) {
 207   if (UseTLAB) {
 208     tlab_allocate(obj, var_size_in_bytes, con_size_in_bytes, t1, slow_case);
 209   } else {
 210     eden_allocate(obj, var_size_in_bytes, con_size_in_bytes, t1, t2, slow_case);
 211     RegisterOrConstant size_in_bytes = var_size_in_bytes->is_valid()
 212                                        ? RegisterOrConstant(var_size_in_bytes)
 213                                        : RegisterOrConstant(con_size_in_bytes);
 214     incr_allocated_bytes(size_in_bytes, t1, t2);
 215   }
 216 }
 217 
 218 
 219 void C1_MacroAssembler::initialize_header(Register obj, Register klass, Register len, Register t1, Register t2) {
 220   assert_different_registers(obj, klass, len, t1, t2);
 221   if (UseBiasedLocking && !len->is_valid()) {
 222     ld(t1, in_bytes(Klass::prototype_header_offset()), klass);
 223   } else {
 224     load_const_optimized(t1, (intx)markOopDesc::prototype());
 225   }
 226   std(t1, oopDesc::mark_offset_in_bytes(), obj);
 227   store_klass(obj, klass);
 228   if (len->is_valid()) {
 229     stw(len, arrayOopDesc::length_offset_in_bytes(), obj);
 230   } else if (UseCompressedClassPointers) {
 231     // Otherwise length is in the class gap.
 232     store_klass_gap(obj);
 233   }
 234 }
 235 
 236 
 237 void C1_MacroAssembler::initialize_body(Register base, Register index) {
 238   assert_different_registers(base, index);
 239   srdi(index, index, LogBytesPerWord);
 240   clear_memory_doubleword(base, index);
 241 }
 242 
 243 void C1_MacroAssembler::initialize_body(Register obj, Register tmp1, Register tmp2,
 244                                         int obj_size_in_bytes, int hdr_size_in_bytes) {
 245   const int index = (obj_size_in_bytes - hdr_size_in_bytes) / HeapWordSize;
 246 
 247   // 2x unrolled loop is shorter with more than 9 HeapWords.
 248   if (index <= 9) {
 249     clear_memory_unrolled(obj, index, R0, hdr_size_in_bytes);
 250   } else {
 251     const Register base_ptr = tmp1,
 252                    cnt_dwords = tmp2;
 253 
 254     addi(base_ptr, obj, hdr_size_in_bytes); // Compute address of first element.
 255     clear_memory_doubleword(base_ptr, cnt_dwords, R0, index);
 256   }
 257 }
 258 
 259 void C1_MacroAssembler::allocate_object(
 260   Register obj,                        // result: pointer to object after successful allocation
 261   Register t1,                         // temp register
 262   Register t2,                         // temp register
 263   Register t3,                         // temp register
 264   int      hdr_size,                   // object header size in words
 265   int      obj_size,                   // object size in words
 266   Register klass,                      // object klass
 267   Label&   slow_case                   // continuation point if fast allocation fails
 268 ) {
 269   assert_different_registers(obj, t1, t2, t3, klass);
 270 
 271   // allocate space & initialize header
 272   if (!is_simm16(obj_size * wordSize)) {
 273     // Would need to use extra register to load
 274     // object size => go the slow case for now.
 275     b(slow_case);
 276     return;
 277   }
 278   try_allocate(obj, noreg, obj_size * wordSize, t2, t3, slow_case);
 279 
 280   initialize_object(obj, klass, noreg, obj_size * HeapWordSize, t1, t2);
 281 }
 282 
 283 void C1_MacroAssembler::initialize_object(
 284   Register obj,                        // result: pointer to object after successful allocation
 285   Register klass,                      // object klass
 286   Register var_size_in_bytes,          // object size in bytes if unknown at compile time; invalid otherwise
 287   int      con_size_in_bytes,          // object size in bytes if   known at compile time
 288   Register t1,                         // temp register
 289   Register t2                          // temp register
 290   ) {
 291   const int hdr_size_in_bytes = instanceOopDesc::header_size() * HeapWordSize;
 292 
 293   initialize_header(obj, klass, noreg, t1, t2);
 294 
 295 #ifdef ASSERT
 296   {
 297     lwz(t1, in_bytes(Klass::layout_helper_offset()), klass);
 298     if (var_size_in_bytes != noreg) {
 299       cmpw(CCR0, t1, var_size_in_bytes);
 300     } else {
 301       cmpwi(CCR0, t1, con_size_in_bytes);
 302     }
 303     asm_assert_eq("bad size in initialize_object", 0x753);
 304   }
 305 #endif
 306 
 307   // Initialize body.
 308   if (var_size_in_bytes != noreg) {
 309     // Use a loop.
 310     addi(t1, obj, hdr_size_in_bytes);                // Compute address of first element.
 311     addi(t2, var_size_in_bytes, -hdr_size_in_bytes); // Compute size of body.
 312     initialize_body(t1, t2);
 313   } else if (con_size_in_bytes > hdr_size_in_bytes) {
 314     // Use a loop.
 315     initialize_body(obj, t1, t2, con_size_in_bytes, hdr_size_in_bytes);
 316   }
 317 
 318   if (CURRENT_ENV->dtrace_alloc_probes()) {
 319     Unimplemented();
 320 //    assert(obj == O0, "must be");
 321 //    call(CAST_FROM_FN_PTR(address, Runtime1::entry_for(Runtime1::dtrace_object_alloc_id)),
 322 //         relocInfo::runtime_call_type);
 323   }
 324 
 325   verify_oop(obj);
 326 }
 327 
 328 
 329 void C1_MacroAssembler::allocate_array(
 330   Register obj,                        // result: pointer to array after successful allocation
 331   Register len,                        // array length
 332   Register t1,                         // temp register
 333   Register t2,                         // temp register
 334   Register t3,                         // temp register
 335   int      hdr_size,                   // object header size in words
 336   int      elt_size,                   // element size in bytes
 337   Register klass,                      // object klass
 338   Label&   slow_case                   // continuation point if fast allocation fails
 339 ) {
 340   assert_different_registers(obj, len, t1, t2, t3, klass);
 341 
 342   // Determine alignment mask.
 343   assert(!(BytesPerWord & 1), "must be a multiple of 2 for masking code to work");
 344   int log2_elt_size = exact_log2(elt_size);
 345 
 346   // Check for negative or excessive length.
 347   size_t max_length = max_array_allocation_length >> log2_elt_size;
 348   if (UseTLAB) {
 349     size_t max_tlab = align_up(ThreadLocalAllocBuffer::max_size() >> log2_elt_size, 64*K);
 350     if (max_tlab < max_length) { max_length = max_tlab; }
 351   }
 352   load_const_optimized(t1, max_length);
 353   cmpld(CCR0, len, t1);
 354   bc_far_optimized(Assembler::bcondCRbiIs1, bi0(CCR0, Assembler::greater), slow_case);
 355 
 356   // compute array size
 357   // note: If 0 <= len <= max_length, len*elt_size + header + alignment is
 358   //       smaller or equal to the largest integer; also, since top is always
 359   //       aligned, we can do the alignment here instead of at the end address
 360   //       computation.
 361   const Register arr_size = t1;
 362   Register arr_len_in_bytes = len;
 363   if (elt_size != 1) {
 364     sldi(t1, len, log2_elt_size);
 365     arr_len_in_bytes = t1;
 366   }
 367   addi(arr_size, arr_len_in_bytes, hdr_size * wordSize + MinObjAlignmentInBytesMask); // Add space for header & alignment.
 368   clrrdi(arr_size, arr_size, LogMinObjAlignmentInBytes);                              // Align array size.
 369 
 370   // Allocate space & initialize header.
 371   if (UseTLAB) {
 372     tlab_allocate(obj, arr_size, 0, t2, slow_case);
 373   } else {
 374     eden_allocate(obj, arr_size, 0, t2, t3, slow_case);
 375   }
 376   initialize_header(obj, klass, len, t2, t3);
 377 
 378   // Initialize body.
 379   const Register base  = t2;
 380   const Register index = t3;
 381   addi(base, obj, hdr_size * wordSize);               // compute address of first element
 382   addi(index, arr_size, -(hdr_size * wordSize));      // compute index = number of bytes to clear
 383   initialize_body(base, index);
 384 
 385   if (CURRENT_ENV->dtrace_alloc_probes()) {
 386     Unimplemented();
 387     //assert(obj == O0, "must be");
 388     //call(CAST_FROM_FN_PTR(address, Runtime1::entry_for(Runtime1::dtrace_object_alloc_id)),
 389     //     relocInfo::runtime_call_type);
 390   }
 391 
 392   verify_oop(obj);
 393 }
 394 
 395 
 396 #ifndef PRODUCT
 397 
 398 void C1_MacroAssembler::verify_stack_oop(int stack_offset) {
 399   verify_oop_addr((RegisterOrConstant)(stack_offset + STACK_BIAS), R1_SP, "broken oop in stack slot");
 400 }
 401 
 402 void C1_MacroAssembler::verify_not_null_oop(Register r) {
 403   Label not_null;
 404   cmpdi(CCR0, r, 0);
 405   bne(CCR0, not_null);
 406   stop("non-null oop required");
 407   bind(not_null);
 408   if (!VerifyOops) return;
 409   verify_oop(r);
 410 }
 411 
 412 #endif // PRODUCT
 413 
 414 void C1_MacroAssembler::null_check(Register r, Label* Lnull) {
 415   if (TrapBasedNullChecks) { // SIGTRAP based
 416     trap_null_check(r);
 417   } else { // explicit
 418     //const address exception_entry = Runtime1::entry_for(Runtime1::throw_null_pointer_exception_id);
 419     assert(Lnull != NULL, "must have Label for explicit check");
 420     cmpdi(CCR0, r, 0);
 421     bc_far_optimized(Assembler::bcondCRbiIs1, bi0(CCR0, Assembler::equal), *Lnull);
 422   }
 423 }
 424 
 425 address C1_MacroAssembler::call_c_with_frame_resize(address dest, int frame_resize) {
 426   if (frame_resize) { resize_frame(-frame_resize, R0); }
 427 #if defined(ABI_ELFv2)
 428   address return_pc = call_c(dest, relocInfo::runtime_call_type);
 429 #else
 430   address return_pc = call_c(CAST_FROM_FN_PTR(FunctionDescriptor*, dest), relocInfo::runtime_call_type);
 431 #endif
 432   if (frame_resize) { resize_frame(frame_resize, R0); }
 433   return return_pc;
 434 }