1 /* 2 * Copyright (c) 2013, Red Hat Inc. 3 * Copyright (c) 1999, 2011, Oracle and/or its affiliates. 4 * All rights reserved. 5 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 6 * 7 * This code is free software; you can redistribute it and/or modify it 8 * under the terms of the GNU General Public License version 2 only, as 9 * published by the Free Software Foundation. 10 * 11 * This code is distributed in the hope that it will be useful, but WITHOUT 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 14 * version 2 for more details (a copy is included in the LICENSE file that 15 * accompanied this code). 16 * 17 * You should have received a copy of the GNU General Public License version 18 * 2 along with this work; if not, write to the Free Software Foundation, 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 20 * 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 22 * or visit www.oracle.com if you need additional information or have any 23 * questions. 24 * 25 */ 26 27 #include "precompiled.hpp" 28 #include "c1/c1_MacroAssembler.hpp" 29 #include "c1/c1_Runtime1.hpp" 30 #include "classfile/systemDictionary.hpp" 31 #include "gc_interface/collectedHeap.hpp" 32 #include "interpreter/interpreter.hpp" 33 #include "oops/arrayOop.hpp" 34 #include "oops/markOop.hpp" 35 #include "runtime/basicLock.hpp" 36 #include "runtime/biasedLocking.hpp" 37 #include "runtime/os.hpp" 38 #include "runtime/stubRoutines.hpp" 39 40 void C1_MacroAssembler::float_cmp(bool is_float, int unordered_result, 41 FloatRegister f0, FloatRegister f1, 42 Register result) 43 { 44 Label done; 45 if (is_float) { 46 fcmps(f0, f1); 47 } else { 48 fcmpd(f0, f1); 49 } 50 if (unordered_result < 0) { 51 // we want -1 for unordered or less than, 0 for equal and 1 for 52 // greater than. 53 cset(result, NE); // Not equal or unordered 54 cneg(result, result, LT); // Less than or unordered 55 } else { 56 // we want -1 for less than, 0 for equal and 1 for unordered or 57 // greater than. 58 cset(result, NE); // Not equal or unordered 59 cneg(result, result, LO); // Less than 60 } 61 } 62 63 int C1_MacroAssembler::lock_object(Register hdr, Register obj, Register disp_hdr, Register scratch, Label& slow_case) { 64 const int aligned_mask = BytesPerWord -1; 65 const int hdr_offset = oopDesc::mark_offset_in_bytes(); 66 assert(hdr != obj && hdr != disp_hdr && obj != disp_hdr, "registers must be different"); 67 Label done, fail; 68 int null_check_offset = -1; 69 70 verify_oop(obj); 71 72 // save object being locked into the BasicObjectLock 73 str(obj, Address(disp_hdr, BasicObjectLock::obj_offset_in_bytes())); 74 75 if (UseBiasedLocking) { 76 assert(scratch != noreg, "should have scratch register at this point"); 77 null_check_offset = biased_locking_enter(disp_hdr, obj, hdr, scratch, false, done, &slow_case); 78 } else { 79 null_check_offset = offset(); 80 } 81 82 // Load object header 83 ldr(hdr, Address(obj, hdr_offset)); 84 // and mark it as unlocked 85 orr(hdr, hdr, markOopDesc::unlocked_value); 86 // save unlocked object header into the displaced header location on the stack 87 str(hdr, Address(disp_hdr, 0)); 88 // test if object header is still the same (i.e. unlocked), and if so, store the 89 // displaced header address in the object header - if it is not the same, get the 90 // object header instead 91 lea(rscratch2, Address(obj, hdr_offset)); 92 cmpxchgptr(hdr, disp_hdr, rscratch2, rscratch1, done, /*fallthough*/NULL); 93 // if the object header was the same, we're done 94 // if the object header was not the same, it is now in the hdr register 95 // => test if it is a stack pointer into the same stack (recursive locking), i.e.: 96 // 97 // 1) (hdr & aligned_mask) == 0 98 // 2) sp <= hdr 99 // 3) hdr <= sp + page_size 100 // 101 // these 3 tests can be done by evaluating the following expression: 102 // 103 // (hdr - sp) & (aligned_mask - page_size) 104 // 105 // assuming both the stack pointer and page_size have their least 106 // significant 2 bits cleared and page_size is a power of 2 107 mov(rscratch1, sp); 108 sub(hdr, hdr, rscratch1); 109 ands(hdr, hdr, aligned_mask - os::vm_page_size()); 110 // for recursive locking, the result is zero => save it in the displaced header 111 // location (NULL in the displaced hdr location indicates recursive locking) 112 str(hdr, Address(disp_hdr, 0)); 113 // otherwise we don't care about the result and handle locking via runtime call 114 cbnz(hdr, slow_case); 115 // done 116 bind(done); 117 if (PrintBiasedLockingStatistics) { 118 lea(rscratch2, ExternalAddress((address)BiasedLocking::fast_path_entry_count_addr())); 119 addmw(Address(rscratch2, 0), 1, rscratch1); 120 } 121 return null_check_offset; 122 } 123 124 125 void C1_MacroAssembler::unlock_object(Register hdr, Register obj, Register disp_hdr, Label& slow_case) { 126 const int aligned_mask = BytesPerWord -1; 127 const int hdr_offset = oopDesc::mark_offset_in_bytes(); 128 assert(hdr != obj && hdr != disp_hdr && obj != disp_hdr, "registers must be different"); 129 Label done; 130 131 if (UseBiasedLocking) { 132 // load object 133 ldr(obj, Address(disp_hdr, BasicObjectLock::obj_offset_in_bytes())); 134 biased_locking_exit(obj, hdr, done); 135 } 136 137 // load displaced header 138 ldr(hdr, Address(disp_hdr, 0)); 139 // if the loaded hdr is NULL we had recursive locking 140 // if we had recursive locking, we are done 141 cbz(hdr, done); 142 if (!UseBiasedLocking) { 143 // load object 144 ldr(obj, Address(disp_hdr, BasicObjectLock::obj_offset_in_bytes())); 145 } 146 verify_oop(obj); 147 148 // test if object header is pointing to the displaced header, and if so, restore 149 // the displaced header in the object - if the object header is not pointing to 150 // the displaced header, get the object header instead 151 // if the object header was not pointing to the displaced header, 152 // we do unlocking via runtime call 153 if (hdr_offset) { 154 lea(rscratch1, Address(obj, hdr_offset)); 155 cmpxchgptr(disp_hdr, hdr, rscratch1, rscratch2, done, &slow_case); 156 } else { 157 cmpxchgptr(disp_hdr, hdr, obj, rscratch2, done, &slow_case); 158 } 159 // done 160 bind(done); 161 } 162 163 164 // Defines obj, preserves var_size_in_bytes 165 void C1_MacroAssembler::try_allocate(Register obj, Register var_size_in_bytes, int con_size_in_bytes, Register t1, Register t2, Label& slow_case) { 166 if (UseTLAB) { 167 tlab_allocate(obj, var_size_in_bytes, con_size_in_bytes, t1, t2, slow_case); 168 } else { 169 eden_allocate(obj, var_size_in_bytes, con_size_in_bytes, t1, slow_case); 170 incr_allocated_bytes(noreg, var_size_in_bytes, con_size_in_bytes, t1); 171 } 172 } 173 174 void C1_MacroAssembler::initialize_header(Register obj, Register klass, Register len, Register t1, Register t2) { 175 assert_different_registers(obj, klass, len); 176 if (UseBiasedLocking && !len->is_valid()) { 177 assert_different_registers(obj, klass, len, t1, t2); 178 ldr(t1, Address(klass, Klass::prototype_header_offset())); 179 } else { 180 // This assumes that all prototype bits fit in an int32_t 181 mov(t1, (int32_t)(intptr_t)markOopDesc::prototype()); 182 } 183 str(t1, Address(obj, oopDesc::mark_offset_in_bytes())); 184 185 if (UseCompressedClassPointers) { // Take care not to kill klass 186 encode_klass_not_null(t1, klass); 187 strw(t1, Address(obj, oopDesc::klass_offset_in_bytes())); 188 } else { 189 str(klass, Address(obj, oopDesc::klass_offset_in_bytes())); 190 } 191 192 if (len->is_valid()) { 193 strw(len, Address(obj, arrayOopDesc::length_offset_in_bytes())); 194 } else if (UseCompressedClassPointers) { 195 store_klass_gap(obj, zr); 196 } 197 } 198 199 // Zero words; len is in bytes 200 // Destroys all registers except addr 201 // len must be a nonzero multiple of wordSize 202 void C1_MacroAssembler::zero_memory(Register addr, Register len, Register t1) { 203 assert_different_registers(addr, len, t1, rscratch1, rscratch2); 204 205 #ifdef ASSERT 206 { Label L; 207 tst(len, BytesPerWord - 1); 208 br(Assembler::EQ, L); 209 stop("len is not a multiple of BytesPerWord"); 210 bind(L); 211 } 212 #endif 213 214 #ifndef PRODUCT 215 block_comment("zero memory"); 216 #endif 217 218 Label loop; 219 Label entry; 220 221 // Algorithm: 222 // 223 // scratch1 = cnt & 7; 224 // cnt -= scratch1; 225 // p += scratch1; 226 // switch (scratch1) { 227 // do { 228 // cnt -= 8; 229 // p[-8] = 0; 230 // case 7: 231 // p[-7] = 0; 232 // case 6: 233 // p[-6] = 0; 234 // // ... 235 // case 1: 236 // p[-1] = 0; 237 // case 0: 238 // p += 8; 239 // } while (cnt); 240 // } 241 242 const int unroll = 8; // Number of str(zr) instructions we'll unroll 243 244 lsr(len, len, LogBytesPerWord); 245 andr(rscratch1, len, unroll - 1); // tmp1 = cnt % unroll 246 sub(len, len, rscratch1); // cnt -= unroll 247 // t1 always points to the end of the region we're about to zero 248 add(t1, addr, rscratch1, Assembler::LSL, LogBytesPerWord); 249 adr(rscratch2, entry); 250 sub(rscratch2, rscratch2, rscratch1, Assembler::LSL, 2); 251 br(rscratch2); 252 bind(loop); 253 sub(len, len, unroll); 254 for (int i = -unroll; i < 0; i++) 255 str(zr, Address(t1, i * wordSize)); 256 bind(entry); 257 add(t1, t1, unroll * wordSize); 258 cbnz(len, loop); 259 } 260 261 // preserves obj, destroys len_in_bytes 262 void C1_MacroAssembler::initialize_body(Register obj, Register len_in_bytes, int hdr_size_in_bytes, Register t1) { 263 Label done; 264 assert(obj != len_in_bytes && obj != t1 && t1 != len_in_bytes, "registers must be different"); 265 assert((hdr_size_in_bytes & (BytesPerWord - 1)) == 0, "header size is not a multiple of BytesPerWord"); 266 Register index = len_in_bytes; 267 // index is positive and ptr sized 268 subs(index, index, hdr_size_in_bytes); 269 br(Assembler::EQ, done); 270 // note: for the remaining code to work, index must be a multiple of BytesPerWord 271 #ifdef ASSERT 272 { Label L; 273 tst(index, BytesPerWord - 1); 274 br(Assembler::EQ, L); 275 stop("index is not a multiple of BytesPerWord"); 276 bind(L); 277 } 278 #endif 279 280 // Preserve obj 281 if (hdr_size_in_bytes) 282 add(obj, obj, hdr_size_in_bytes); 283 zero_memory(obj, index, t1); 284 if (hdr_size_in_bytes) 285 sub(obj, obj, hdr_size_in_bytes); 286 287 // done 288 bind(done); 289 } 290 291 292 void C1_MacroAssembler::allocate_object(Register obj, Register t1, Register t2, int header_size, int object_size, Register klass, Label& slow_case) { 293 assert_different_registers(obj, t1, t2); // XXX really? 294 assert(header_size >= 0 && object_size >= header_size, "illegal sizes"); 295 296 try_allocate(obj, noreg, object_size * BytesPerWord, t1, t2, slow_case); 297 298 initialize_object(obj, klass, noreg, object_size * HeapWordSize, t1, t2); 299 } 300 301 void C1_MacroAssembler::initialize_object(Register obj, Register klass, Register var_size_in_bytes, int con_size_in_bytes, Register t1, Register t2) { 302 assert((con_size_in_bytes & MinObjAlignmentInBytesMask) == 0, 303 "con_size_in_bytes is not multiple of alignment"); 304 const int hdr_size_in_bytes = instanceOopDesc::header_size() * HeapWordSize; 305 306 initialize_header(obj, klass, noreg, t1, t2); 307 308 // clear rest of allocated space 309 const Register index = t2; 310 const int threshold = 16 * BytesPerWord; // approximate break even point for code size (see comments below) 311 if (var_size_in_bytes != noreg) { 312 mov(index, var_size_in_bytes); 313 initialize_body(obj, index, hdr_size_in_bytes, t1); 314 } else if (con_size_in_bytes <= threshold) { 315 // use explicit null stores 316 int i = hdr_size_in_bytes; 317 if (i < con_size_in_bytes && (con_size_in_bytes % (2 * BytesPerWord))) { 318 str(zr, Address(obj, i)); 319 i += BytesPerWord; 320 } 321 for (; i < con_size_in_bytes; i += 2 * BytesPerWord) 322 stp(zr, zr, Address(obj, i)); 323 } else if (con_size_in_bytes > hdr_size_in_bytes) { 324 block_comment("zero memory"); 325 // use loop to null out the fields 326 327 int words = (con_size_in_bytes - hdr_size_in_bytes) / BytesPerWord; 328 mov(index, words / 8); 329 330 const int unroll = 8; // Number of str(zr) instructions we'll unroll 331 int remainder = words % unroll; 332 lea(rscratch1, Address(obj, hdr_size_in_bytes + remainder * BytesPerWord)); 333 334 Label entry_point, loop; 335 b(entry_point); 336 337 bind(loop); 338 sub(index, index, 1); 339 for (int i = -unroll; i < 0; i++) { 340 if (-i == remainder) 341 bind(entry_point); 342 str(zr, Address(rscratch1, i * wordSize)); 343 } 344 if (remainder == 0) 345 bind(entry_point); 346 add(rscratch1, rscratch1, unroll * wordSize); 347 cbnz(index, loop); 348 349 } 350 351 membar(StoreStore); 352 353 if (CURRENT_ENV->dtrace_alloc_probes()) { 354 assert(obj == r0, "must be"); 355 far_call(RuntimeAddress(Runtime1::entry_for(Runtime1::dtrace_object_alloc_id))); 356 } 357 358 verify_oop(obj); 359 } 360 void C1_MacroAssembler::allocate_array(Register obj, Register len, Register t1, Register t2, int header_size, int f, Register klass, Label& slow_case) { 361 assert_different_registers(obj, len, t1, t2, klass); 362 363 // determine alignment mask 364 assert(!(BytesPerWord & 1), "must be a multiple of 2 for masking code to work"); 365 366 // check for negative or excessive length 367 mov(rscratch1, (int32_t)max_array_allocation_length); 368 cmp(len, rscratch1); 369 br(Assembler::HS, slow_case); 370 371 const Register arr_size = t2; // okay to be the same 372 // align object end 373 mov(arr_size, (int32_t)header_size * BytesPerWord + MinObjAlignmentInBytesMask); 374 add(arr_size, arr_size, len, ext::uxtw, f); 375 andr(arr_size, arr_size, ~MinObjAlignmentInBytesMask); 376 377 try_allocate(obj, arr_size, 0, t1, t2, slow_case); 378 379 initialize_header(obj, klass, len, t1, t2); 380 381 // clear rest of allocated space 382 const Register len_zero = len; 383 initialize_body(obj, arr_size, header_size * BytesPerWord, len_zero); 384 385 membar(StoreStore); 386 387 if (CURRENT_ENV->dtrace_alloc_probes()) { 388 assert(obj == r0, "must be"); 389 far_call(RuntimeAddress(Runtime1::entry_for(Runtime1::dtrace_object_alloc_id))); 390 } 391 392 verify_oop(obj); 393 } 394 395 396 void C1_MacroAssembler::inline_cache_check(Register receiver, Register iCache) { 397 verify_oop(receiver); 398 // explicit NULL check not needed since load from [klass_offset] causes a trap 399 // check against inline cache 400 assert(!MacroAssembler::needs_explicit_null_check(oopDesc::klass_offset_in_bytes()), "must add explicit null check"); 401 402 cmp_klass(receiver, iCache, rscratch1); 403 } 404 405 406 void C1_MacroAssembler::build_frame(int framesize, int bang_size_in_bytes) { 407 // If we have to make this method not-entrant we'll overwrite its 408 // first instruction with a jump. For this action to be legal we 409 // must ensure that this first instruction is a B, BL, NOP, BKPT, 410 // SVC, HVC, or SMC. Make it a NOP. 411 nop(); 412 assert(bang_size_in_bytes >= framesize, "stack bang size incorrect"); 413 // Make sure there is enough stack space for this method's activation. 414 // Note that we do this before doing an enter(). 415 generate_stack_overflow_check(bang_size_in_bytes); 416 MacroAssembler::build_frame(framesize + 2 * wordSize); 417 if (NotifySimulator) { 418 notify(Assembler::method_entry); 419 } 420 } 421 422 void C1_MacroAssembler::remove_frame(int framesize) { 423 MacroAssembler::remove_frame(framesize + 2 * wordSize); 424 if (NotifySimulator) { 425 notify(Assembler::method_reentry); 426 } 427 } 428 429 430 void C1_MacroAssembler::verified_entry() { 431 } 432 433 #ifndef PRODUCT 434 435 void C1_MacroAssembler::verify_stack_oop(int stack_offset) { 436 if (!VerifyOops) return; 437 verify_oop_addr(Address(sp, stack_offset), "oop"); 438 } 439 440 void C1_MacroAssembler::verify_not_null_oop(Register r) { 441 if (!VerifyOops) return; 442 Label not_null; 443 cbnz(r, not_null); 444 stop("non-null oop required"); 445 bind(not_null); 446 verify_oop(r); 447 } 448 449 void C1_MacroAssembler::invalidate_registers(bool inv_r0, bool inv_r19, bool inv_r2, bool inv_r3, bool inv_r4, bool inv_r5) { 450 #ifdef ASSERT 451 static int nn; 452 if (inv_r0) mov(r0, 0xDEAD); 453 if (inv_r19) mov(r19, 0xDEAD); 454 if (inv_r2) mov(r2, nn++); 455 if (inv_r3) mov(r3, 0xDEAD); 456 if (inv_r4) mov(r4, 0xDEAD); 457 if (inv_r5) mov(r5, 0xDEAD); 458 #endif 459 } 460 #endif // ifndef PRODUCT