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