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