1 /* 2 * Copyright (c) 1999, 2018, 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_CodeStubs.hpp" 28 #include "c1/c1_FrameMap.hpp" 29 #include "c1/c1_LIRAssembler.hpp" 30 #include "c1/c1_MacroAssembler.hpp" 31 #include "c1/c1_Runtime1.hpp" 32 #include "nativeInst_ppc.hpp" 33 #include "runtime/sharedRuntime.hpp" 34 #include "utilities/macros.hpp" 35 #include "vmreg_ppc.inline.hpp" 36 #if INCLUDE_ALL_GCS 37 #include "gc/g1/g1BarrierSet.hpp" 38 #endif // INCLUDE_ALL_GCS 39 40 #define __ ce->masm()-> 41 42 43 RangeCheckStub::RangeCheckStub(CodeEmitInfo* info, LIR_Opr index, LIR_Opr array) 44 : _throw_index_out_of_bounds_exception(array == NULL), _index(index), _array(array) { 45 assert(info != NULL, "must have info"); 46 _info = new CodeEmitInfo(info); 47 } 48 49 void RangeCheckStub::emit_code(LIR_Assembler* ce) { 50 __ bind(_entry); 51 52 if (_info->deoptimize_on_exception()) { 53 address a = Runtime1::entry_for(Runtime1::predicate_failed_trap_id); 54 // May be used by optimizations like LoopInvariantCodeMotion or RangeCheckEliminator. 55 DEBUG_ONLY( __ untested("RangeCheckStub: predicate_failed_trap_id"); ) 56 //__ load_const_optimized(R0, a); 57 __ add_const_optimized(R0, R29_TOC, MacroAssembler::offset_to_global_toc(a)); 58 __ mtctr(R0); 59 __ bctrl(); 60 ce->add_call_info_here(_info); 61 ce->verify_oop_map(_info); 62 debug_only(__ illtrap()); 63 return; 64 } 65 66 address stub = _throw_index_out_of_bounds_exception ? Runtime1::entry_for(Runtime1::throw_index_exception_id) 67 : Runtime1::entry_for(Runtime1::throw_range_check_failed_id); 68 //__ load_const_optimized(R0, stub); 69 __ add_const_optimized(R0, R29_TOC, MacroAssembler::offset_to_global_toc(stub)); 70 __ mtctr(R0); 71 72 Register index = R0; // pass in R0 73 if (_index->is_register()) { 74 __ extsw(index, _index->as_register()); 75 } else { 76 __ load_const_optimized(index, _index->as_jint()); 77 } 78 if (_array) { 79 __ std(_array->as_pointer_register(), -8, R1_SP); 80 } 81 __ std(R0, -16, R1_SP); 82 83 __ bctrl(); 84 ce->add_call_info_here(_info); 85 ce->verify_oop_map(_info); 86 debug_only(__ illtrap()); 87 } 88 89 90 PredicateFailedStub::PredicateFailedStub(CodeEmitInfo* info) { 91 _info = new CodeEmitInfo(info); 92 } 93 94 void PredicateFailedStub::emit_code(LIR_Assembler* ce) { 95 __ bind(_entry); 96 address a = Runtime1::entry_for(Runtime1::predicate_failed_trap_id); 97 //__ load_const_optimized(R0, a); 98 __ add_const_optimized(R0, R29_TOC, MacroAssembler::offset_to_global_toc(a)); 99 __ mtctr(R0); 100 __ bctrl(); 101 ce->add_call_info_here(_info); 102 ce->verify_oop_map(_info); 103 debug_only(__ illtrap()); 104 } 105 106 107 void CounterOverflowStub::emit_code(LIR_Assembler* ce) { 108 __ bind(_entry); 109 110 // Parameter 1: bci 111 __ load_const_optimized(R0, _bci); 112 __ std(R0, -16, R1_SP); 113 114 // Parameter 2: Method* 115 Metadata *m = _method->as_constant_ptr()->as_metadata(); 116 AddressLiteral md = __ constant_metadata_address(m); // Notify OOP recorder (don't need the relocation). 117 __ load_const_optimized(R0, md.value()); 118 __ std(R0, -8, R1_SP); 119 120 address a = Runtime1::entry_for(Runtime1::counter_overflow_id); 121 //__ load_const_optimized(R0, a); 122 __ add_const_optimized(R0, R29_TOC, MacroAssembler::offset_to_global_toc(a)); 123 __ mtctr(R0); 124 __ bctrl(); 125 ce->add_call_info_here(_info); 126 ce->verify_oop_map(_info); 127 128 __ b(_continuation); 129 } 130 131 132 void DivByZeroStub::emit_code(LIR_Assembler* ce) { 133 if (_offset != -1) { 134 ce->compilation()->implicit_exception_table()->append(_offset, __ offset()); 135 } 136 __ bind(_entry); 137 address stub = Runtime1::entry_for(Runtime1::throw_div0_exception_id); 138 //__ load_const_optimized(R0, stub); 139 __ add_const_optimized(R0, R29_TOC, MacroAssembler::offset_to_global_toc(stub)); 140 __ mtctr(R0); 141 __ bctrl(); 142 ce->add_call_info_here(_info); 143 ce->verify_oop_map(_info); 144 debug_only(__ illtrap()); 145 } 146 147 148 void ImplicitNullCheckStub::emit_code(LIR_Assembler* ce) { 149 address a; 150 if (_info->deoptimize_on_exception()) { 151 // Deoptimize, do not throw the exception, because it is probably wrong to do it here. 152 a = Runtime1::entry_for(Runtime1::predicate_failed_trap_id); 153 } else { 154 a = Runtime1::entry_for(Runtime1::throw_null_pointer_exception_id); 155 } 156 157 if (ImplicitNullChecks || TrapBasedNullChecks) { 158 ce->compilation()->implicit_exception_table()->append(_offset, __ offset()); 159 } 160 __ bind(_entry); 161 //__ load_const_optimized(R0, a); 162 __ add_const_optimized(R0, R29_TOC, MacroAssembler::offset_to_global_toc(a)); 163 __ mtctr(R0); 164 __ bctrl(); 165 ce->add_call_info_here(_info); 166 ce->verify_oop_map(_info); 167 debug_only(__ illtrap()); 168 } 169 170 171 // Implementation of SimpleExceptionStub 172 void SimpleExceptionStub::emit_code(LIR_Assembler* ce) { 173 __ bind(_entry); 174 address stub = Runtime1::entry_for(_stub); 175 //__ load_const_optimized(R0, stub); 176 __ add_const_optimized(R0, R29_TOC, MacroAssembler::offset_to_global_toc(stub)); 177 if (_obj->is_valid()) { __ mr_if_needed(/*tmp1 in do_CheckCast*/ R4_ARG2, _obj->as_register()); } 178 __ mtctr(R0); 179 __ bctrl(); 180 ce->add_call_info_here(_info); 181 debug_only( __ illtrap(); ) 182 } 183 184 185 // Implementation of NewInstanceStub 186 NewInstanceStub::NewInstanceStub(LIR_Opr klass_reg, LIR_Opr result, ciInstanceKlass* klass, CodeEmitInfo* info, Runtime1::StubID stub_id) { 187 _result = result; 188 _klass = klass; 189 _klass_reg = klass_reg; 190 _info = new CodeEmitInfo(info); 191 assert(stub_id == Runtime1::new_instance_id || 192 stub_id == Runtime1::fast_new_instance_id || 193 stub_id == Runtime1::fast_new_instance_init_check_id, 194 "need new_instance id"); 195 _stub_id = stub_id; 196 } 197 198 void NewInstanceStub::emit_code(LIR_Assembler* ce) { 199 __ bind(_entry); 200 201 address entry = Runtime1::entry_for(_stub_id); 202 //__ load_const_optimized(R0, entry); 203 __ add_const_optimized(R0, R29_TOC, MacroAssembler::offset_to_global_toc(entry)); 204 __ mtctr(R0); 205 __ bctrl(); 206 ce->add_call_info_here(_info); 207 ce->verify_oop_map(_info); 208 __ b(_continuation); 209 } 210 211 212 // Implementation of NewTypeArrayStub 213 NewTypeArrayStub::NewTypeArrayStub(LIR_Opr klass_reg, LIR_Opr length, LIR_Opr result, CodeEmitInfo* info) { 214 _klass_reg = klass_reg; 215 _length = length; 216 _result = result; 217 _info = new CodeEmitInfo(info); 218 } 219 220 void NewTypeArrayStub::emit_code(LIR_Assembler* ce) { 221 __ bind(_entry); 222 223 address entry = Runtime1::entry_for(Runtime1::new_type_array_id); 224 //__ load_const_optimized(R0, entry); 225 __ add_const_optimized(R0, R29_TOC, MacroAssembler::offset_to_global_toc(entry)); 226 __ mr_if_needed(/*op->tmp1()->as_register()*/ R5_ARG3, _length->as_register()); // already sign-extended 227 __ mtctr(R0); 228 __ bctrl(); 229 ce->add_call_info_here(_info); 230 ce->verify_oop_map(_info); 231 __ b(_continuation); 232 } 233 234 235 // Implementation of NewObjectArrayStub 236 NewObjectArrayStub::NewObjectArrayStub(LIR_Opr klass_reg, LIR_Opr length, LIR_Opr result, CodeEmitInfo* info) { 237 _klass_reg = klass_reg; 238 _length = length; 239 _result = result; 240 _info = new CodeEmitInfo(info); 241 } 242 243 void NewObjectArrayStub::emit_code(LIR_Assembler* ce) { 244 __ bind(_entry); 245 246 address entry = Runtime1::entry_for(Runtime1::new_object_array_id); 247 //__ load_const_optimized(R0, entry); 248 __ add_const_optimized(R0, R29_TOC, MacroAssembler::offset_to_global_toc(entry)); 249 __ mr_if_needed(/*op->tmp1()->as_register()*/ R5_ARG3, _length->as_register()); // already sign-extended 250 __ mtctr(R0); 251 __ bctrl(); 252 ce->add_call_info_here(_info); 253 ce->verify_oop_map(_info); 254 __ b(_continuation); 255 } 256 257 258 // Implementation of MonitorAccessStubs 259 MonitorEnterStub::MonitorEnterStub(LIR_Opr obj_reg, LIR_Opr lock_reg, CodeEmitInfo* info) 260 : MonitorAccessStub(obj_reg, lock_reg) { 261 _info = new CodeEmitInfo(info); 262 } 263 264 void MonitorEnterStub::emit_code(LIR_Assembler* ce) { 265 __ bind(_entry); 266 address stub = Runtime1::entry_for(ce->compilation()->has_fpu_code() ? Runtime1::monitorenter_id : Runtime1::monitorenter_nofpu_id); 267 //__ load_const_optimized(R0, stub); 268 __ add_const_optimized(R0, R29_TOC, MacroAssembler::offset_to_global_toc(stub)); 269 __ mr_if_needed(/*scratch_opr()->as_register()*/ R4_ARG2, _obj_reg->as_register()); 270 assert(_lock_reg->as_register() == R5_ARG3, ""); 271 __ mtctr(R0); 272 __ bctrl(); 273 ce->add_call_info_here(_info); 274 ce->verify_oop_map(_info); 275 __ b(_continuation); 276 } 277 278 void MonitorExitStub::emit_code(LIR_Assembler* ce) { 279 __ bind(_entry); 280 if (_compute_lock) { 281 ce->monitor_address(_monitor_ix, _lock_reg); 282 } 283 address stub = Runtime1::entry_for(ce->compilation()->has_fpu_code() ? Runtime1::monitorexit_id : Runtime1::monitorexit_nofpu_id); 284 //__ load_const_optimized(R0, stub); 285 __ add_const_optimized(R0, R29_TOC, MacroAssembler::offset_to_global_toc(stub)); 286 assert(_lock_reg->as_register() == R4_ARG2, ""); 287 __ mtctr(R0); 288 __ bctrl(); 289 __ b(_continuation); 290 } 291 292 293 // Implementation of patching: 294 // - Copy the code at given offset to an inlined buffer (first the bytes, then the number of bytes). 295 // - Replace original code with a call to the stub. 296 // At Runtime: 297 // - call to stub, jump to runtime 298 // - in runtime: preserve all registers (especially objects, i.e., source and destination object) 299 // - in runtime: after initializing class, restore original code, reexecute instruction 300 301 int PatchingStub::_patch_info_offset = -(5 * BytesPerInstWord); 302 303 void PatchingStub::align_patch_site(MacroAssembler* ) { 304 // Patch sites on ppc are always properly aligned. 305 } 306 307 #ifdef ASSERT 308 inline void compare_with_patch_site(address template_start, address pc_start, int bytes_to_copy) { 309 address start = template_start; 310 for (int i = 0; i < bytes_to_copy; i++) { 311 address ptr = (address)(pc_start + i); 312 int a_byte = (*ptr) & 0xFF; 313 assert(a_byte == *start++, "should be the same code"); 314 } 315 } 316 #endif 317 318 void PatchingStub::emit_code(LIR_Assembler* ce) { 319 // copy original code here 320 assert(NativeGeneralJump::instruction_size <= _bytes_to_copy && _bytes_to_copy <= 0xFF, 321 "not enough room for call"); 322 assert((_bytes_to_copy & 0x3) == 0, "must copy a multiple of four bytes"); 323 324 Label call_patch; 325 326 int being_initialized_entry = __ offset(); 327 328 if (_id == load_klass_id) { 329 // Produce a copy of the load klass instruction for use by the being initialized case. 330 AddressLiteral addrlit((address)NULL, metadata_Relocation::spec(_index)); 331 __ load_const(_obj, addrlit, R0); 332 DEBUG_ONLY( compare_with_patch_site(__ code_section()->start() + being_initialized_entry, _pc_start, _bytes_to_copy); ) 333 } else if (_id == load_mirror_id || _id == load_appendix_id) { 334 // Produce a copy of the load mirror instruction for use by the being initialized case. 335 AddressLiteral addrlit((address)NULL, oop_Relocation::spec(_index)); 336 __ load_const(_obj, addrlit, R0); 337 DEBUG_ONLY( compare_with_patch_site(__ code_section()->start() + being_initialized_entry, _pc_start, _bytes_to_copy); ) 338 } else { 339 // Make a copy the code which is going to be patched. 340 for (int i = 0; i < _bytes_to_copy; i++) { 341 address ptr = (address)(_pc_start + i); 342 int a_byte = (*ptr) & 0xFF; 343 __ emit_int8 (a_byte); 344 } 345 } 346 347 address end_of_patch = __ pc(); 348 int bytes_to_skip = 0; 349 if (_id == load_mirror_id) { 350 int offset = __ offset(); 351 __ block_comment(" being_initialized check"); 352 353 // Static field accesses have special semantics while the class 354 // initializer is being run so we emit a test which can be used to 355 // check that this code is being executed by the initializing 356 // thread. 357 assert(_obj != noreg, "must be a valid register"); 358 assert(_index >= 0, "must have oop index"); 359 __ mr(R0, _obj); // spill 360 __ ld(_obj, java_lang_Class::klass_offset_in_bytes(), _obj); 361 __ ld(_obj, in_bytes(InstanceKlass::init_thread_offset()), _obj); 362 __ cmpd(CCR0, _obj, R16_thread); 363 __ mr(_obj, R0); // restore 364 __ bne(CCR0, call_patch); 365 366 // Load_klass patches may execute the patched code before it's 367 // copied back into place so we need to jump back into the main 368 // code of the nmethod to continue execution. 369 __ b(_patch_site_continuation); 370 371 // Make sure this extra code gets skipped. 372 bytes_to_skip += __ offset() - offset; 373 } 374 375 // Now emit the patch record telling the runtime how to find the 376 // pieces of the patch. We only need 3 bytes but it has to be 377 // aligned as an instruction so emit 4 bytes. 378 int sizeof_patch_record = 4; 379 bytes_to_skip += sizeof_patch_record; 380 381 // Emit the offsets needed to find the code to patch. 382 int being_initialized_entry_offset = __ offset() - being_initialized_entry + sizeof_patch_record; 383 384 // Emit the patch record. We need to emit a full word, so emit an extra empty byte. 385 __ emit_int8(0); 386 __ emit_int8(being_initialized_entry_offset); 387 __ emit_int8(bytes_to_skip); 388 __ emit_int8(_bytes_to_copy); 389 address patch_info_pc = __ pc(); 390 assert(patch_info_pc - end_of_patch == bytes_to_skip, "incorrect patch info"); 391 392 address entry = __ pc(); 393 NativeGeneralJump::insert_unconditional((address)_pc_start, entry); 394 address target = NULL; 395 relocInfo::relocType reloc_type = relocInfo::none; 396 switch (_id) { 397 case access_field_id: target = Runtime1::entry_for(Runtime1::access_field_patching_id); break; 398 case load_klass_id: target = Runtime1::entry_for(Runtime1::load_klass_patching_id); 399 reloc_type = relocInfo::metadata_type; break; 400 case load_mirror_id: target = Runtime1::entry_for(Runtime1::load_mirror_patching_id); 401 reloc_type = relocInfo::oop_type; break; 402 case load_appendix_id: target = Runtime1::entry_for(Runtime1::load_appendix_patching_id); 403 reloc_type = relocInfo::oop_type; break; 404 default: ShouldNotReachHere(); 405 } 406 __ bind(call_patch); 407 408 __ block_comment("patch entry point"); 409 //__ load_const(R0, target); + mtctr + bctrl must have size -_patch_info_offset 410 __ load_const32(R0, MacroAssembler::offset_to_global_toc(target)); 411 __ add(R0, R29_TOC, R0); 412 __ mtctr(R0); 413 __ bctrl(); 414 assert(_patch_info_offset == (patch_info_pc - __ pc()), "must not change"); 415 ce->add_call_info_here(_info); 416 __ b(_patch_site_entry); 417 if (_id == load_klass_id || _id == load_mirror_id || _id == load_appendix_id) { 418 CodeSection* cs = __ code_section(); 419 address pc = (address)_pc_start; 420 RelocIterator iter(cs, pc, pc + 1); 421 relocInfo::change_reloc_info_for_address(&iter, (address) pc, reloc_type, relocInfo::none); 422 } 423 } 424 425 426 void DeoptimizeStub::emit_code(LIR_Assembler* ce) { 427 __ bind(_entry); 428 address stub = Runtime1::entry_for(Runtime1::deoptimize_id); 429 //__ load_const_optimized(R0, stub); 430 __ add_const_optimized(R0, R29_TOC, MacroAssembler::offset_to_global_toc(stub)); 431 __ mtctr(R0); 432 433 __ load_const_optimized(R0, _trap_request); // Pass trap request in R0. 434 __ bctrl(); 435 ce->add_call_info_here(_info); 436 debug_only(__ illtrap()); 437 } 438 439 440 void ArrayCopyStub::emit_code(LIR_Assembler* ce) { 441 //---------------slow case: call to native----------------- 442 __ bind(_entry); 443 __ mr(R3_ARG1, src()->as_register()); 444 __ extsw(R4_ARG2, src_pos()->as_register()); 445 __ mr(R5_ARG3, dst()->as_register()); 446 __ extsw(R6_ARG4, dst_pos()->as_register()); 447 __ extsw(R7_ARG5, length()->as_register()); 448 449 ce->emit_static_call_stub(); 450 451 bool success = ce->emit_trampoline_stub_for_call(SharedRuntime::get_resolve_static_call_stub()); 452 if (!success) { return; } 453 454 __ relocate(relocInfo::static_call_type); 455 // Note: At this point we do not have the address of the trampoline 456 // stub, and the entry point might be too far away for bl, so __ pc() 457 // serves as dummy and the bl will be patched later. 458 __ code()->set_insts_mark(); 459 __ bl(__ pc()); 460 ce->add_call_info_here(info()); 461 ce->verify_oop_map(info()); 462 463 #ifndef PRODUCT 464 const address counter = (address)&Runtime1::_arraycopy_slowcase_cnt; 465 const Register tmp = R3, tmp2 = R4; 466 int simm16_offs = __ load_const_optimized(tmp, counter, tmp2, true); 467 __ lwz(tmp2, simm16_offs, tmp); 468 __ addi(tmp2, tmp2, 1); 469 __ stw(tmp2, simm16_offs, tmp); 470 #endif 471 472 __ b(_continuation); 473 } 474 475 476 /////////////////////////////////////////////////////////////////////////////////// 477 #if INCLUDE_ALL_GCS 478 479 void G1PreBarrierStub::emit_code(LIR_Assembler* ce) { 480 // At this point we know that marking is in progress. 481 // If do_load() is true then we have to emit the 482 // load of the previous value; otherwise it has already 483 // been loaded into _pre_val. 484 485 __ bind(_entry); 486 487 assert(pre_val()->is_register(), "Precondition."); 488 Register pre_val_reg = pre_val()->as_register(); 489 490 if (do_load()) { 491 ce->mem2reg(addr(), pre_val(), T_OBJECT, patch_code(), info(), false /*wide*/, false /*unaligned*/); 492 } 493 494 __ cmpdi(CCR0, pre_val_reg, 0); 495 __ bc_far_optimized(Assembler::bcondCRbiIs1, __ bi0(CCR0, Assembler::equal), _continuation); 496 497 address stub = Runtime1::entry_for(Runtime1::Runtime1::g1_pre_barrier_slow_id); 498 //__ load_const_optimized(R0, stub); 499 __ add_const_optimized(R0, R29_TOC, MacroAssembler::offset_to_global_toc(stub)); 500 __ std(pre_val_reg, -8, R1_SP); // Pass pre_val on stack. 501 __ mtctr(R0); 502 __ bctrl(); 503 __ b(_continuation); 504 } 505 506 void G1PostBarrierStub::emit_code(LIR_Assembler* ce) { 507 __ bind(_entry); 508 509 assert(addr()->is_register(), "Precondition."); 510 assert(new_val()->is_register(), "Precondition."); 511 Register addr_reg = addr()->as_pointer_register(); 512 Register new_val_reg = new_val()->as_register(); 513 514 __ cmpdi(CCR0, new_val_reg, 0); 515 __ bc_far_optimized(Assembler::bcondCRbiIs1, __ bi0(CCR0, Assembler::equal), _continuation); 516 517 address stub = Runtime1::entry_for(Runtime1::Runtime1::g1_post_barrier_slow_id); 518 //__ load_const_optimized(R0, stub); 519 __ add_const_optimized(R0, R29_TOC, MacroAssembler::offset_to_global_toc(stub)); 520 __ mtctr(R0); 521 __ mr(R0, addr_reg); // Pass addr in R0. 522 __ bctrl(); 523 __ b(_continuation); 524 } 525 526 #endif // INCLUDE_ALL_GCS 527 /////////////////////////////////////////////////////////////////////////////////// 528 529 #undef __