1 /*
   2  * Copyright (c) 2016, Oracle and/or its affiliates. All rights reserved.
   3  * Copyright (c) 2016 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 
  40 void C1_MacroAssembler::inline_cache_check(Register receiver, Register iCache) {
  41   Label ic_miss, ic_hit;
  42   verify_oop(receiver);
  43   int klass_offset = oopDesc::klass_offset_in_bytes();
  44 
  45   if (!ImplicitNullChecks || MacroAssembler::needs_explicit_null_check(klass_offset)) {
  46     if (VM_Version::has_CompareBranch()) {
  47       z_cgij(receiver, 0, Assembler::bcondEqual, ic_miss);
  48     } else {
  49       z_ltgr(receiver, receiver);
  50       z_bre(ic_miss);
  51     }
  52   }
  53 
  54   compare_klass_ptr(iCache, klass_offset, receiver, false);
  55   z_bre(ic_hit);
  56 
  57   // If icache check fails, then jump to runtime routine.
  58   // Note: RECEIVER must still contain the receiver!
  59   load_const_optimized(Z_R1_scratch, AddressLiteral(SharedRuntime::get_ic_miss_stub()));
  60   z_br(Z_R1_scratch);
  61   align(CodeEntryAlignment);
  62   bind(ic_hit);
  63 }
  64 
  65 void C1_MacroAssembler::explicit_null_check(Register base) {
  66   ShouldNotCallThis(); // unused
  67 }
  68 
  69 void C1_MacroAssembler::build_frame(int frame_size_in_bytes, int bang_size_in_bytes) {
  70   assert(bang_size_in_bytes >= frame_size_in_bytes, "stack bang size incorrect");
  71   generate_stack_overflow_check(bang_size_in_bytes);
  72   save_return_pc();
  73   push_frame(frame_size_in_bytes);
  74 }
  75 
  76 void C1_MacroAssembler::unverified_entry(Register receiver, Register ic_klass) {
  77   ShouldNotCallThis(); // unused
  78 }
  79 
  80 void C1_MacroAssembler::verified_entry() {
  81   if (C1Breakpoint) z_illtrap(0xC1);
  82 }
  83 
  84 void C1_MacroAssembler::lock_object(Register hdr, Register obj, Register disp_hdr, Label& slow_case) {
  85   const int hdr_offset = oopDesc::mark_offset_in_bytes();
  86   assert_different_registers(hdr, obj, disp_hdr);
  87   NearLabel done;
  88 
  89   verify_oop(obj);
  90 
  91   // Load object header.
  92   z_lg(hdr, Address(obj, hdr_offset));
  93 
  94   // Save object being locked into the BasicObjectLock...
  95   z_stg(obj, Address(disp_hdr, BasicObjectLock::obj_offset_in_bytes()));
  96 
  97   if (UseBiasedLocking) {
  98     biased_locking_enter(obj, hdr, Z_R1_scratch, Z_R0_scratch, done, &slow_case);
  99   }
 100 
 101   // and mark it as unlocked.
 102   z_oill(hdr, markOopDesc::unlocked_value);
 103   // Save unlocked object header into the displaced header location on the stack.
 104   z_stg(hdr, Address(disp_hdr, (intptr_t)0));
 105   // Test if object header is still the same (i.e. unlocked), and if so, store the
 106   // displaced header address in the object header. If it is not the same, get the
 107   // object header instead.
 108   z_csg(hdr, disp_hdr, hdr_offset, obj);
 109   // If the object header was the same, we're done.
 110   if (PrintBiasedLockingStatistics) {
 111     Unimplemented();
 112 #if 0
 113     cond_inc32(Assembler::equal,
 114                ExternalAddress((address)BiasedLocking::fast_path_entry_count_addr()));
 115 #endif
 116   }
 117   branch_optimized(Assembler::bcondEqual, done);
 118   // If the object header was not the same, it is now in the hdr register.
 119   // => Test if it is a stack pointer into the same stack (recursive locking), i.e.:
 120   //
 121   // 1) (hdr & markOopDesc::lock_mask_in_place) == 0
 122   // 2) rsp <= hdr
 123   // 3) hdr <= rsp + page_size
 124   //
 125   // These 3 tests can be done by evaluating the following expression:
 126   //
 127   // (hdr - Z_SP) & (~(page_size-1) | markOopDesc::lock_mask_in_place)
 128   //
 129   // assuming both the stack pointer and page_size have their least
 130   // significant 2 bits cleared and page_size is a power of 2
 131   z_sgr(hdr, Z_SP);
 132 
 133   load_const_optimized(Z_R0_scratch, (~(os::vm_page_size()-1) | markOopDesc::lock_mask_in_place));
 134   z_ngr(hdr, Z_R0_scratch); // AND sets CC (result eq/ne 0).
 135   // For recursive locking, the result is zero. => Save it in the displaced header
 136   // location (NULL in the displaced hdr location indicates recursive locking).
 137   z_stg(hdr, Address(disp_hdr, (intptr_t)0));
 138   // Otherwise we don't care about the result and handle locking via runtime call.
 139   branch_optimized(Assembler::bcondNotZero, slow_case);
 140   // done
 141   bind(done);
 142 }
 143 
 144 void C1_MacroAssembler::unlock_object(Register hdr, Register obj, Register disp_hdr, Label& slow_case) {
 145   const int aligned_mask = BytesPerWord -1;
 146   const int hdr_offset = oopDesc::mark_offset_in_bytes();
 147   assert_different_registers(hdr, obj, disp_hdr);
 148   NearLabel done;
 149 
 150   if (UseBiasedLocking) {
 151     // Load object.
 152     z_lg(obj, Address(disp_hdr, BasicObjectLock::obj_offset_in_bytes()));
 153     biased_locking_exit(obj, hdr, done);
 154   }
 155 
 156   // Load displaced header.
 157   z_ltg(hdr, Address(disp_hdr, (intptr_t)0));
 158   // If the loaded hdr is NULL we had recursive locking, and we are done.
 159   z_bre(done);
 160   if (!UseBiasedLocking) {
 161     // Load object.
 162     z_lg(obj, Address(disp_hdr, BasicObjectLock::obj_offset_in_bytes()));
 163   }
 164   verify_oop(obj);
 165   // Test if object header is pointing to the displaced header, and if so, restore
 166   // the displaced header in the object. If the object header is not pointing to
 167   // the displaced header, get the object header instead.
 168   z_csg(disp_hdr, hdr, hdr_offset, obj);
 169   // If the object header was not pointing to the displaced header,
 170   // we do unlocking via runtime call.
 171   branch_optimized(Assembler::bcondNotEqual, slow_case);
 172   // done
 173   bind(done);
 174 }
 175 
 176 void C1_MacroAssembler::try_allocate(
 177   Register obj,                        // result: Pointer to object after successful allocation.
 178   Register var_size_in_bytes,          // Object size in bytes if unknown at compile time; invalid otherwise.
 179   int      con_size_in_bytes,          // Object size in bytes if   known at compile time.
 180   Register t1,                         // Temp register: Must be global register for incr_allocated_bytes.
 181   Label&   slow_case                   // Continuation point if fast allocation fails.
 182 ) {
 183   if (UseTLAB) {
 184     tlab_allocate(obj, var_size_in_bytes, con_size_in_bytes, t1, slow_case);
 185   } else {
 186     // Allocation in shared Eden not implemented, because sapjvm allocation trace does not allow it.
 187     z_brul(slow_case);
 188   }
 189 }
 190 
 191 void C1_MacroAssembler::initialize_header(Register obj, Register klass, Register len, Register Rzero, Register t1) {
 192   assert_different_registers(obj, klass, len, t1, Rzero);
 193   if (UseBiasedLocking && !len->is_valid()) {
 194     assert_different_registers(obj, klass, len, t1);
 195     z_lg(t1, Address(klass, Klass::prototype_header_offset()));
 196   } else {
 197     // This assumes that all prototype bits fit in an int32_t.
 198     load_const_optimized(t1, (intx)markOopDesc::prototype());
 199   }
 200   z_stg(t1, Address(obj, oopDesc::mark_offset_in_bytes()));
 201 
 202   if (len->is_valid()) {
 203     // Length will be in the klass gap, if one exists.
 204     z_st(len, Address(obj, arrayOopDesc::length_offset_in_bytes()));
 205   } else if (UseCompressedClassPointers) {
 206     store_klass_gap(Rzero, obj);  // Zero klass gap for compressed oops.
 207   }
 208   store_klass(klass, obj, t1);
 209 }
 210 
 211 void C1_MacroAssembler::initialize_body(Register objectFields, Register len_in_bytes, Register Rzero) {
 212   Label done;
 213   assert_different_registers(objectFields, len_in_bytes, Rzero);
 214 
 215   // Initialize object fields.
 216   // See documentation for MVCLE instruction!!!
 217   assert(objectFields->encoding()%2==0, "objectFields must be an even register");
 218   assert(len_in_bytes->encoding() == (objectFields->encoding()+1), "objectFields and len_in_bytes must be a register pair");
 219   assert(Rzero->encoding()%2==1, "Rzero must be an odd register");
 220 
 221   // Use Rzero as src length, then mvcle will copy nothing
 222   // and fill the object with the padding value 0.
 223   move_long_ext(objectFields, as_Register(Rzero->encoding()-1), 0);
 224   bind(done);
 225 }
 226 
 227 void C1_MacroAssembler::allocate_object(
 228   Register obj,                        // Result: pointer to object after successful allocation.
 229   Register t1,                         // temp register
 230   Register t2,                         // temp register: Must be a global register for try_allocate.
 231   int      hdr_size,                   // object header size in words
 232   int      obj_size,                   // object size in words
 233   Register klass,                      // object klass
 234   Label&   slow_case                   // Continuation point if fast allocation fails.
 235 ) {
 236   assert_different_registers(obj, t1, t2, klass);
 237 
 238   // Allocate space and initialize header.
 239   try_allocate(obj, noreg, obj_size * wordSize, t1, slow_case);
 240 
 241   initialize_object(obj, klass, noreg, obj_size * HeapWordSize, t1, t2);
 242 }
 243 
 244 void C1_MacroAssembler::initialize_object(
 245   Register obj,                        // result: Pointer to object after successful allocation.
 246   Register klass,                      // object klass
 247   Register var_size_in_bytes,          // Object size in bytes if unknown at compile time; invalid otherwise.
 248   int      con_size_in_bytes,          // Object size in bytes if   known at compile time.
 249   Register t1,                         // temp register
 250   Register t2                          // temp register
 251  ) {
 252   assert((con_size_in_bytes & MinObjAlignmentInBytesMask) == 0,
 253          "con_size_in_bytes is not multiple of alignment");
 254   assert(var_size_in_bytes == noreg, "not implemented");
 255   const int hdr_size_in_bytes = instanceOopDesc::header_size() * HeapWordSize;
 256 
 257   const Register Rzero = t2;
 258 
 259   z_xgr(Rzero, Rzero);
 260   initialize_header(obj, klass, noreg, Rzero, t1);
 261 
 262   // Clear rest of allocated space.
 263   const int threshold = 4 * BytesPerWord;
 264   if (con_size_in_bytes <= threshold) {
 265     // Use explicit null stores.
 266     // code size = 6*n bytes (n = number of fields to clear)
 267     for (int i = hdr_size_in_bytes; i < con_size_in_bytes; i += BytesPerWord)
 268       z_stg(Rzero, Address(obj, i));
 269   } else {
 270     // Code size generated by initialize_body() is 16.
 271     Register object_fields = Z_R0_scratch;
 272     Register len_in_bytes  = Z_R1_scratch;
 273     z_la(object_fields, hdr_size_in_bytes, obj);
 274     load_const_optimized(len_in_bytes, con_size_in_bytes - hdr_size_in_bytes);
 275     initialize_body(object_fields, len_in_bytes, Rzero);
 276   }
 277 
 278   // Dtrace support is unimplemented.
 279   //  if (CURRENT_ENV->dtrace_alloc_probes()) {
 280   //    assert(obj == rax, "must be");
 281   //    call(RuntimeAddress(Runtime1::entry_for (Runtime1::dtrace_object_alloc_id)));
 282   //  }
 283 
 284   verify_oop(obj);
 285 }
 286 
 287 void C1_MacroAssembler::allocate_array(
 288   Register obj,                        // result: Pointer to array after successful allocation.
 289   Register len,                        // array length
 290   Register t1,                         // temp register
 291   Register t2,                         // temp register
 292   int      hdr_size,                   // object header size in words
 293   int      elt_size,                   // element size in bytes
 294   Register klass,                      // object klass
 295   Label&   slow_case                   // Continuation point if fast allocation fails.
 296 ) {
 297   assert_different_registers(obj, len, t1, t2, klass);
 298 
 299   // Determine alignment mask.
 300   assert(!(BytesPerWord & 1), "must be a multiple of 2 for masking code to work");
 301 
 302   // Check for negative or excessive length.
 303   compareU64_and_branch(len, (int32_t)max_array_allocation_length, bcondHigh, slow_case);
 304 
 305   // Compute array size.
 306   // Note: If 0 <= len <= max_length, len*elt_size + header + alignment is
 307   // smaller or equal to the largest integer. Also, since top is always
 308   // aligned, we can do the alignment here instead of at the end address
 309   // computation.
 310   const Register arr_size = t2;
 311   switch (elt_size) {
 312     case  1: lgr_if_needed(arr_size, len); break;
 313     case  2: z_sllg(arr_size, len, 1); break;
 314     case  4: z_sllg(arr_size, len, 2); break;
 315     case  8: z_sllg(arr_size, len, 3); break;
 316     default: ShouldNotReachHere();
 317   }
 318   add2reg(arr_size, hdr_size * wordSize + MinObjAlignmentInBytesMask); // Add space for header & alignment.
 319   z_nill(arr_size, (~MinObjAlignmentInBytesMask) & 0xffff);            // Align array size.
 320 
 321   try_allocate(obj, arr_size, 0, t1, slow_case);
 322 
 323   initialize_header(obj, klass, len, noreg, t1);
 324 
 325   // Clear rest of allocated space.
 326   Label done;
 327   Register object_fields = t1;
 328   Register Rzero = Z_R1_scratch;
 329   z_aghi(arr_size, -(hdr_size * BytesPerWord));
 330   z_bre(done); // Jump if size of fields is zero.
 331   z_la(object_fields, hdr_size * BytesPerWord, obj);
 332   z_xgr(Rzero, Rzero);
 333   initialize_body(object_fields, arr_size, Rzero);
 334   bind(done);
 335 
 336   // Dtrace support is unimplemented.
 337   // if (CURRENT_ENV->dtrace_alloc_probes()) {
 338   //   assert(obj == rax, "must be");
 339   //   call(RuntimeAddress(Runtime1::entry_for (Runtime1::dtrace_object_alloc_id)));
 340   // }
 341 
 342   verify_oop(obj);
 343 }
 344 
 345 
 346 #ifndef PRODUCT
 347 
 348 void C1_MacroAssembler::verify_stack_oop(int stack_offset) {
 349   Unimplemented();
 350   // if (!VerifyOops) return;
 351   // verify_oop_addr(Address(SP, stack_offset + STACK_BIAS));
 352 }
 353 
 354 void C1_MacroAssembler::verify_not_null_oop(Register r) {
 355   if (!VerifyOops) return;
 356   NearLabel not_null;
 357   compareU64_and_branch(r, (intptr_t)0, bcondNotEqual, not_null);
 358   stop("non-null oop required");
 359   bind(not_null);
 360   verify_oop(r);
 361 }
 362 
 363 void C1_MacroAssembler::invalidate_registers(Register preserve1,
 364                                              Register preserve2,
 365                                              Register preserve3) {
 366   Register dead_value = noreg;
 367   for (int i = 0; i < FrameMap::nof_cpu_regs; i++) {
 368     Register r = as_Register(i);
 369     if (r != preserve1 && r != preserve2 && r != preserve3 && r != Z_SP && r != Z_thread) {
 370       if (dead_value == noreg) {
 371         load_const_optimized(r, 0xc1dead);
 372         dead_value = r;
 373       } else {
 374         z_lgr(r, dead_value);
 375       }
 376     }
 377   }
 378 }
 379 
 380 #endif // !PRODUCT