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