1 /* 2 * Copyright (c) 1999, 2017, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. 8 * 9 * This code is distributed in the hope that it will be useful, but WITHOUT 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 12 * version 2 for more details (a copy is included in the LICENSE file that 13 * accompanied this code). 14 * 15 * You should have received a copy of the GNU General Public License version 16 * 2 along with this work; if not, write to the Free Software Foundation, 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 18 * 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 20 * or visit www.oracle.com if you need additional information or have any 21 * questions. 22 * 23 */ 24 25 #include "precompiled.hpp" 26 #include "c1/c1_MacroAssembler.hpp" 27 #include "c1/c1_Runtime1.hpp" 28 #include "classfile/systemDictionary.hpp" 29 #include "gc/shared/collectedHeap.hpp" 30 #include "interpreter/interpreter.hpp" 31 #include "oops/arrayOop.hpp" 32 #include "oops/markOop.hpp" 33 #include "runtime/basicLock.hpp" 34 #include "runtime/biasedLocking.hpp" 35 #include "runtime/os.hpp" 36 #include "runtime/sharedRuntime.hpp" 37 #include "runtime/stubRoutines.hpp" 38 39 void C1_MacroAssembler::inline_cache_check(Register receiver, Register iCache) { 40 Label L; 41 const Register temp_reg = G3_scratch; 42 // Note: needs more testing of out-of-line vs. inline slow case 43 verify_oop(receiver); 44 load_klass(receiver, temp_reg); 45 cmp_and_brx_short(temp_reg, iCache, Assembler::equal, Assembler::pt, L); 46 AddressLiteral ic_miss(SharedRuntime::get_ic_miss_stub()); 47 jump_to(ic_miss, temp_reg); 48 delayed()->nop(); 49 align(CodeEntryAlignment); 50 bind(L); 51 } 52 53 54 void C1_MacroAssembler::explicit_null_check(Register base) { 55 Unimplemented(); 56 } 57 58 59 void C1_MacroAssembler::build_frame(int frame_size_in_bytes, int bang_size_in_bytes) { 60 assert(bang_size_in_bytes >= frame_size_in_bytes, "stack bang size incorrect"); 61 generate_stack_overflow_check(bang_size_in_bytes); 62 // Create the frame. 63 save_frame_c1(frame_size_in_bytes); 64 } 65 66 67 void C1_MacroAssembler::verified_entry() { 68 if (C1Breakpoint) breakpoint_trap(); 69 // build frame 70 verify_FPU(0, "method_entry"); 71 } 72 73 74 void C1_MacroAssembler::lock_object(Register Rmark, Register Roop, Register Rbox, Register Rscratch, Label& slow_case) { 75 assert_different_registers(Rmark, Roop, Rbox, Rscratch); 76 77 Label done; 78 79 Address mark_addr(Roop, oopDesc::mark_offset_in_bytes()); 80 81 // The following move must be the first instruction of emitted since debug 82 // information may be generated for it. 83 // Load object header 84 ld_ptr(mark_addr, Rmark); 85 86 verify_oop(Roop); 87 88 // save object being locked into the BasicObjectLock 89 st_ptr(Roop, Rbox, BasicObjectLock::obj_offset_in_bytes()); 90 91 if (UseBiasedLocking) { 92 biased_locking_enter(Roop, Rmark, Rscratch, done, &slow_case); 93 } 94 95 // Save Rbox in Rscratch to be used for the cas operation 96 mov(Rbox, Rscratch); 97 98 // and mark it unlocked 99 or3(Rmark, markOopDesc::unlocked_value, Rmark); 100 101 // save unlocked object header into the displaced header location on the stack 102 st_ptr(Rmark, Rbox, BasicLock::displaced_header_offset_in_bytes()); 103 104 // compare object markOop with Rmark and if equal exchange Rscratch with object markOop 105 assert(mark_addr.disp() == 0, "cas must take a zero displacement"); 106 cas_ptr(mark_addr.base(), Rmark, Rscratch); 107 // if compare/exchange succeeded we found an unlocked object and we now have locked it 108 // hence we are done 109 cmp(Rmark, Rscratch); 110 brx(Assembler::equal, false, Assembler::pt, done); 111 delayed()->sub(Rscratch, SP, Rscratch); //pull next instruction into delay slot 112 // we did not find an unlocked object so see if this is a recursive case 113 // sub(Rscratch, SP, Rscratch); 114 assert(os::vm_page_size() > 0xfff, "page size too small - change the constant"); 115 andcc(Rscratch, 0xfffff003, Rscratch); 116 brx(Assembler::notZero, false, Assembler::pn, slow_case); 117 delayed()->st_ptr(Rscratch, Rbox, BasicLock::displaced_header_offset_in_bytes()); 118 bind(done); 119 } 120 121 122 void C1_MacroAssembler::unlock_object(Register Rmark, Register Roop, Register Rbox, Label& slow_case) { 123 assert_different_registers(Rmark, Roop, Rbox); 124 125 Label done; 126 127 Address mark_addr(Roop, oopDesc::mark_offset_in_bytes()); 128 assert(mark_addr.disp() == 0, "cas must take a zero displacement"); 129 130 if (UseBiasedLocking) { 131 // load the object out of the BasicObjectLock 132 ld_ptr(Rbox, BasicObjectLock::obj_offset_in_bytes(), Roop); 133 verify_oop(Roop); 134 biased_locking_exit(mark_addr, Rmark, done); 135 } 136 // Test first it it is a fast recursive unlock 137 ld_ptr(Rbox, BasicLock::displaced_header_offset_in_bytes(), Rmark); 138 br_null_short(Rmark, Assembler::pt, done); 139 if (!UseBiasedLocking) { 140 // load object 141 ld_ptr(Rbox, BasicObjectLock::obj_offset_in_bytes(), Roop); 142 verify_oop(Roop); 143 } 144 145 // Check if it is still a light weight lock, this is is true if we see 146 // the stack address of the basicLock in the markOop of the object 147 cas_ptr(mark_addr.base(), Rbox, Rmark); 148 cmp(Rbox, Rmark); 149 150 brx(Assembler::notEqual, false, Assembler::pn, slow_case); 151 delayed()->nop(); 152 // Done 153 bind(done); 154 } 155 156 157 void C1_MacroAssembler::try_allocate( 158 Register obj, // result: pointer to object after successful allocation 159 Register var_size_in_bytes, // object size in bytes if unknown at compile time; invalid otherwise 160 int con_size_in_bytes, // object size in bytes if known at compile time 161 Register t1, // temp register, must be global register for incr_allocated_bytes 162 Register t2, // temp register 163 Label& slow_case // continuation point if fast allocation fails 164 ) { 165 RegisterOrConstant size_in_bytes = var_size_in_bytes->is_valid() 166 ? RegisterOrConstant(var_size_in_bytes) : RegisterOrConstant(con_size_in_bytes); 167 if (UseTLAB) { 168 tlab_allocate(obj, var_size_in_bytes, con_size_in_bytes, t1, slow_case); 169 } else { 170 eden_allocate(obj, var_size_in_bytes, con_size_in_bytes, t1, t2, slow_case); 171 incr_allocated_bytes(size_in_bytes, t1, t2); 172 } 173 } 174 175 176 void C1_MacroAssembler::initialize_header(Register obj, Register klass, Register len, Register t1, Register t2) { 177 assert_different_registers(obj, klass, len, t1, t2); 178 if (UseBiasedLocking && !len->is_valid()) { 179 ld_ptr(klass, in_bytes(Klass::prototype_header_offset()), t1); 180 } else { 181 set((intx)markOopDesc::prototype(), t1); 182 } 183 st_ptr(t1, obj, oopDesc::mark_offset_in_bytes()); 184 if (UseCompressedClassPointers) { 185 // Save klass 186 mov(klass, t1); 187 encode_klass_not_null(t1); 188 stw(t1, obj, oopDesc::klass_offset_in_bytes()); 189 } else { 190 st_ptr(klass, obj, oopDesc::klass_offset_in_bytes()); 191 } 192 if (len->is_valid()) { 193 st(len, obj, arrayOopDesc::length_offset_in_bytes()); 194 } else if (UseCompressedClassPointers) { 195 // otherwise length is in the class gap 196 store_klass_gap(G0, obj); 197 } 198 } 199 200 201 void C1_MacroAssembler::initialize_body(Register base, Register index) { 202 zero_memory(base, index); 203 } 204 205 206 void C1_MacroAssembler::allocate_object( 207 Register obj, // result: pointer to object after successful allocation 208 Register t1, // temp register 209 Register t2, // temp register, must be a global register for try_allocate 210 Register t3, // temp register 211 int hdr_size, // object header size in words 212 int obj_size, // object size in words 213 Register klass, // object klass 214 Label& slow_case // continuation point if fast allocation fails 215 ) { 216 assert_different_registers(obj, t1, t2, t3, klass); 217 assert(klass == G5, "must be G5"); 218 219 // allocate space & initialize header 220 if (!is_simm13(obj_size * wordSize)) { 221 // would need to use extra register to load 222 // object size => go the slow case for now 223 ba(slow_case); 224 delayed()->nop(); 225 return; 226 } 227 try_allocate(obj, noreg, obj_size * wordSize, t2, t3, slow_case); 228 229 initialize_object(obj, klass, noreg, obj_size * HeapWordSize, t1, t2, /* is_tlab_allocated */ UseTLAB); 230 } 231 232 void C1_MacroAssembler::initialize_object( 233 Register obj, // result: pointer to object after successful allocation 234 Register klass, // object klass 235 Register var_size_in_bytes, // object size in bytes if unknown at compile time; invalid otherwise 236 int con_size_in_bytes, // object size in bytes if known at compile time 237 Register t1, // temp register 238 Register t2, // temp register 239 bool is_tlab_allocated // the object was allocated in a TLAB; relevant for the implementation of ZeroTLAB 240 ) { 241 const int hdr_size_in_bytes = instanceOopDesc::header_size() * HeapWordSize; 242 243 initialize_header(obj, klass, noreg, t1, t2); 244 245 #ifdef ASSERT 246 { 247 Label ok; 248 ld(klass, in_bytes(Klass::layout_helper_offset()), t1); 249 if (var_size_in_bytes != noreg) { 250 cmp_and_brx_short(t1, var_size_in_bytes, Assembler::equal, Assembler::pt, ok); 251 } else { 252 cmp_and_brx_short(t1, con_size_in_bytes, Assembler::equal, Assembler::pt, ok); 253 } 254 stop("bad size in initialize_object"); 255 should_not_reach_here(); 256 257 bind(ok); 258 } 259 260 #endif 261 262 if (!(UseTLAB && ZeroTLAB && is_tlab_allocated)) { 263 // initialize body 264 const int threshold = 5 * HeapWordSize; // approximate break even point for code size 265 if (var_size_in_bytes != noreg) { 266 // use a loop 267 add(obj, hdr_size_in_bytes, t1); // compute address of first element 268 sub(var_size_in_bytes, hdr_size_in_bytes, t2); // compute size of body 269 initialize_body(t1, t2); 270 } else if (con_size_in_bytes <= threshold) { 271 // use explicit NULL stores 272 for (int i = hdr_size_in_bytes; i < con_size_in_bytes; i += HeapWordSize) st_ptr(G0, obj, i); 273 } else if (con_size_in_bytes > hdr_size_in_bytes) { 274 // use a loop 275 const Register base = t1; 276 const Register index = t2; 277 add(obj, hdr_size_in_bytes, base); // compute address of first element 278 // compute index = number of words to clear 279 set(con_size_in_bytes - hdr_size_in_bytes, index); 280 initialize_body(base, index); 281 } 282 } 283 284 if (CURRENT_ENV->dtrace_alloc_probes()) { 285 assert(obj == O0, "must be"); 286 call(CAST_FROM_FN_PTR(address, Runtime1::entry_for(Runtime1::dtrace_object_alloc_id)), 287 relocInfo::runtime_call_type); 288 delayed()->nop(); 289 } 290 291 verify_oop(obj); 292 } 293 294 295 void C1_MacroAssembler::allocate_array( 296 Register obj, // result: pointer to array after successful allocation 297 Register len, // array length 298 Register t1, // temp register 299 Register t2, // temp register 300 Register t3, // temp register 301 int hdr_size, // object header size in words 302 int elt_size, // element size in bytes 303 Register klass, // object klass 304 Label& slow_case // continuation point if fast allocation fails 305 ) { 306 assert_different_registers(obj, len, t1, t2, t3, klass); 307 assert(klass == G5, "must be G5"); 308 assert(t1 == G1, "must be G1"); 309 310 // determine alignment mask 311 assert(!(BytesPerWord & 1), "must be a multiple of 2 for masking code to work"); 312 313 // check for negative or excessive length 314 // note: the maximum length allowed is chosen so that arrays of any 315 // element size with this length are always smaller or equal 316 // to the largest integer (i.e., array size computation will 317 // not overflow) 318 set(max_array_allocation_length, t1); 319 cmp(len, t1); 320 br(Assembler::greaterUnsigned, false, Assembler::pn, slow_case); 321 322 // compute array size 323 // note: if 0 <= len <= max_length, len*elt_size + header + alignment is 324 // smaller or equal to the largest integer; also, since top is always 325 // aligned, we can do the alignment here instead of at the end address 326 // computation 327 const Register arr_size = t1; 328 switch (elt_size) { 329 case 1: delayed()->mov(len, arr_size); break; 330 case 2: delayed()->sll(len, 1, arr_size); break; 331 case 4: delayed()->sll(len, 2, arr_size); break; 332 case 8: delayed()->sll(len, 3, arr_size); break; 333 default: ShouldNotReachHere(); 334 } 335 add(arr_size, hdr_size * wordSize + MinObjAlignmentInBytesMask, arr_size); // add space for header & alignment 336 and3(arr_size, ~MinObjAlignmentInBytesMask, arr_size); // align array size 337 338 // allocate space & initialize header 339 if (UseTLAB) { 340 tlab_allocate(obj, arr_size, 0, t2, slow_case); 341 } else { 342 eden_allocate(obj, arr_size, 0, t2, t3, slow_case); 343 } 344 initialize_header(obj, klass, len, t2, t3); 345 346 // initialize body 347 const Register base = t2; 348 const Register index = t3; 349 add(obj, hdr_size * wordSize, base); // compute address of first element 350 sub(arr_size, hdr_size * wordSize, index); // compute index = number of words to clear 351 initialize_body(base, index); 352 353 if (CURRENT_ENV->dtrace_alloc_probes()) { 354 assert(obj == O0, "must be"); 355 call(CAST_FROM_FN_PTR(address, Runtime1::entry_for(Runtime1::dtrace_object_alloc_id)), 356 relocInfo::runtime_call_type); 357 delayed()->nop(); 358 } 359 360 verify_oop(obj); 361 } 362 363 364 #ifndef PRODUCT 365 366 void C1_MacroAssembler::verify_stack_oop(int stack_offset) { 367 if (!VerifyOops) return; 368 verify_oop_addr(Address(SP, stack_offset + STACK_BIAS)); 369 } 370 371 void C1_MacroAssembler::verify_not_null_oop(Register r) { 372 Label not_null; 373 br_notnull_short(r, Assembler::pt, not_null); 374 stop("non-null oop required"); 375 bind(not_null); 376 if (!VerifyOops) return; 377 verify_oop(r); 378 } 379 380 void C1_MacroAssembler::invalidate_registers(bool iregisters, bool lregisters, bool oregisters, 381 Register preserve1, Register preserve2) { 382 if (iregisters) { 383 for (int i = 0; i < 6; i++) { 384 Register r = as_iRegister(i); 385 if (r != preserve1 && r != preserve2) set(0xdead, r); 386 } 387 } 388 if (oregisters) { 389 for (int i = 0; i < 6; i++) { 390 Register r = as_oRegister(i); 391 if (r != preserve1 && r != preserve2) set(0xdead, r); 392 } 393 } 394 if (lregisters) { 395 for (int i = 0; i < 8; i++) { 396 Register r = as_lRegister(i); 397 if (r != preserve1 && r != preserve2) set(0xdead, r); 398 } 399 } 400 } 401 402 403 #endif