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