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_Defs.hpp"
 28 #include "c1/c1_MacroAssembler.hpp"
 29 #include "c1/c1_Runtime1.hpp"
 30 #include "ci/ciUtilities.hpp"
 31 #include "compiler/oopMap.hpp"
 32 #include "gc/shared/cardTable.hpp"
 33 #include "gc/shared/cardTableBarrierSet.hpp"
 34 #include "interpreter/interpreter.hpp"
 35 #include "nativeInst_ppc.hpp"
 36 #include "oops/oop.inline.hpp"
 37 #include "prims/jvmtiExport.hpp"
 38 #include "register_ppc.hpp"
 39 #include "runtime/sharedRuntime.hpp"
 40 #include "runtime/signature.hpp"
 41 #include "runtime/vframeArray.hpp"
 42 #include "utilities/align.hpp"
 43 #include "utilities/macros.hpp"
 44 #include "utilities/powerOfTwo.hpp"
 45 #include "vmreg_ppc.inline.hpp"
 46 
 47 // Implementation of StubAssembler
 48 
 49 int StubAssembler::call_RT(Register oop_result1, Register metadata_result,
 50                            address entry_point, int number_of_arguments) {
 51   set_num_rt_args(0); // Nothing on stack
 52   assert(!(oop_result1->is_valid() || metadata_result->is_valid()) ||
 53          oop_result1 != metadata_result, "registers must be different");
 54 
 55   // Currently no stack banging. We assume that there are enough
 56   // StackShadowPages (which have been banged in generate_stack_overflow_check)
 57   // for the stub frame and the runtime frames.
 58 
 59   set_last_Java_frame(R1_SP, noreg);
 60 
 61   // ARG1 must hold thread address.
 62   mr(R3_ARG1, R16_thread);
 63 
 64   address return_pc = call_c(entry_point);
 65 
 66   // Last java sp can be null when the RT call was preempted
 67   reset_last_Java_frame(false /* check_last_java_sp */);
 68 
 69   // Check for pending exceptions.
 70   {
 71     ld(R0, in_bytes(Thread::pending_exception_offset()), R16_thread);
 72     cmpdi(CR0, R0, 0);
 73 
 74     // This used to conditionally jump to forward_exception however it is
 75     // possible if we relocate that the branch will not reach. So we must jump
 76     // around so we can always reach.
 77 
 78     Label ok;
 79     beq(CR0, ok);
 80 
 81     // Make sure that the vm_results are cleared.
 82     if (oop_result1->is_valid() || metadata_result->is_valid()) {
 83       li(R0, 0);
 84       if (oop_result1->is_valid()) {
 85         std(R0, in_bytes(JavaThread::vm_result_oop_offset()), R16_thread);
 86       }
 87       if (metadata_result->is_valid()) {
 88         std(R0, in_bytes(JavaThread::vm_result_metadata_offset()), R16_thread);
 89       }
 90     }
 91 
 92     if (frame_size() == no_frame_size) {
 93       ShouldNotReachHere(); // We always have a frame size.
 94       //pop_frame(); // pop the stub frame
 95       //ld(R0, _abi0(lr), R1_SP);
 96       //mtlr(R0);
 97       //load_const_optimized(R0, StubRoutines::forward_exception_entry());
 98       //mtctr(R0);
 99       //bctr();
100     } else if (_stub_id == (int)C1StubId::forward_exception_id) {
101       should_not_reach_here();
102     } else {
103       // keep stub frame for next call_RT
104       //load_const_optimized(R0, Runtime1::entry_for(C1StubId::forward_exception_id));
105       add_const_optimized(R0, R29_TOC, MacroAssembler::offset_to_global_toc(Runtime1::entry_for(C1StubId::forward_exception_id)));
106       mtctr(R0);
107       bctr();
108     }
109 
110     bind(ok);
111   }
112 
113   // Get oop results if there are any and reset the values in the thread.
114   if (oop_result1->is_valid()) {
115     get_vm_result_oop(oop_result1);
116   }
117   if (metadata_result->is_valid()) {
118     get_vm_result_metadata(metadata_result);
119   }
120 
121   return (int)(return_pc - code_section()->start());
122 }
123 
124 
125 int StubAssembler::call_RT(Register oop_result1, Register metadata_result, address entry, Register arg1) {
126   mr_if_needed(R4_ARG2, arg1);
127   return call_RT(oop_result1, metadata_result, entry, 1);
128 }
129 
130 
131 int StubAssembler::call_RT(Register oop_result1, Register metadata_result, address entry, Register arg1, Register arg2) {
132   mr_if_needed(R4_ARG2, arg1);
133   mr_if_needed(R5_ARG3, arg2); assert(arg2 != R4_ARG2, "smashed argument");
134   return call_RT(oop_result1, metadata_result, entry, 2);
135 }
136 
137 
138 int StubAssembler::call_RT(Register oop_result1, Register metadata_result, address entry, Register arg1, Register arg2, Register arg3) {
139   mr_if_needed(R4_ARG2, arg1);
140   mr_if_needed(R5_ARG3, arg2); assert(arg2 != R4_ARG2, "smashed argument");
141   mr_if_needed(R6_ARG4, arg3); assert(arg3 != R4_ARG2 && arg3 != R5_ARG3, "smashed argument");
142   return call_RT(oop_result1, metadata_result, entry, 3);
143 }
144 
145 
146 // Implementation of Runtime1
147 
148 #define __ sasm->
149 
150 static int cpu_reg_save_offsets[FrameMap::nof_cpu_regs];
151 static int fpu_reg_save_offsets[FrameMap::nof_fpu_regs];
152 static int frame_size_in_bytes = -1;
153 
154 static OopMap* generate_oop_map(StubAssembler* sasm, bool save_fpu_registers) {
155   assert(frame_size_in_bytes > frame::native_abi_reg_args_size, "init");
156   sasm->set_frame_size(frame_size_in_bytes / BytesPerWord);
157   int frame_size_in_slots = frame_size_in_bytes / sizeof(jint);
158   OopMap* oop_map = new OopMap(frame_size_in_slots, 0);
159 
160   int i;
161   for (i = 0; i < FrameMap::nof_cpu_regs; i++) {
162     Register r = as_Register(i);
163     if (FrameMap::reg_needs_save(r)) {
164       int sp_offset = cpu_reg_save_offsets[i];
165       oop_map->set_callee_saved(VMRegImpl::stack2reg(sp_offset >> 2), r->as_VMReg());
166     }
167   }
168 
169   if (save_fpu_registers) {
170     for (i = 0; i < FrameMap::nof_fpu_regs; i++) {
171       FloatRegister r = as_FloatRegister(i);
172       int sp_offset = fpu_reg_save_offsets[i];
173       oop_map->set_callee_saved(VMRegImpl::stack2reg(sp_offset >> 2), r->as_VMReg());
174     }
175   }
176 
177   return oop_map;
178 }
179 
180 static OopMap* save_live_registers(StubAssembler* sasm, bool save_fpu_registers = true,
181                                    Register ret_pc = noreg, int stack_preserve = 0) {
182   if (ret_pc == noreg) {
183     ret_pc = R0;
184     __ mflr(ret_pc);
185   }
186   __ std(ret_pc, _abi0(lr), R1_SP); // C code needs pc in C1 method.
187   __ push_frame(frame_size_in_bytes + stack_preserve, R0);
188 
189   // Record volatile registers as callee-save values in an OopMap so
190   // their save locations will be propagated to the caller frame's
191   // RegisterMap during StackFrameStream construction (needed for
192   // deoptimization; see compiledVFrame::create_stack_value).
193   // OopMap frame sizes are in c2 stack slot sizes (sizeof(jint)).
194 
195   int i;
196   for (i = 0; i < FrameMap::nof_cpu_regs; i++) {
197     Register r = as_Register(i);
198     if (FrameMap::reg_needs_save(r)) {
199       int sp_offset = cpu_reg_save_offsets[i];
200       __ std(r, sp_offset, R1_SP);
201     }
202   }
203 
204   if (save_fpu_registers) {
205     for (i = 0; i < FrameMap::nof_fpu_regs; i++) {
206       FloatRegister r = as_FloatRegister(i);
207       int sp_offset = fpu_reg_save_offsets[i];
208       __ stfd(r, sp_offset, R1_SP);
209     }
210   }
211 
212   return generate_oop_map(sasm, save_fpu_registers);
213 }
214 
215 static void restore_live_registers(StubAssembler* sasm, Register result1, Register result2,
216                                    bool restore_fpu_registers = true) {
217   for (int i = 0; i < FrameMap::nof_cpu_regs; i++) {
218     Register r = as_Register(i);
219     if (FrameMap::reg_needs_save(r) && r != result1 && r != result2) {
220       int sp_offset = cpu_reg_save_offsets[i];
221       __ ld(r, sp_offset, R1_SP);
222     }
223   }
224 
225   if (restore_fpu_registers) {
226     for (int i = 0; i < FrameMap::nof_fpu_regs; i++) {
227       FloatRegister r = as_FloatRegister(i);
228       int sp_offset = fpu_reg_save_offsets[i];
229       __ lfd(r, sp_offset, R1_SP);
230     }
231   }
232 
233   __ pop_frame();
234   __ ld(R0, _abi0(lr), R1_SP);
235   __ mtlr(R0);
236 }
237 
238 
239 void Runtime1::initialize_pd() {
240   int i;
241   int sp_offset = frame::native_abi_reg_args_size;
242 
243   for (i = 0; i < FrameMap::nof_cpu_regs; i++) {
244     Register r = as_Register(i);
245     if (FrameMap::reg_needs_save(r)) {
246       cpu_reg_save_offsets[i] = sp_offset;
247       sp_offset += BytesPerWord;
248     }
249   }
250 
251   for (i = 0; i < FrameMap::nof_fpu_regs; i++) {
252     fpu_reg_save_offsets[i] = sp_offset;
253     sp_offset += BytesPerWord;
254   }
255   frame_size_in_bytes = align_up(sp_offset, frame::alignment_in_bytes);
256 }
257 
258 uint Runtime1::runtime_blob_current_thread_offset(frame f) {
259   // On PPC virtual threads don't save the JavaThread* in their context (e.g. C1 stub frames).
260   ShouldNotCallThis();
261   return 0;
262 }
263 
264 OopMapSet* Runtime1::generate_exception_throw(StubAssembler* sasm, address target, bool has_argument) {
265   // Make a frame and preserve the caller's caller-save registers.
266   OopMap* oop_map = save_live_registers(sasm);
267 
268   int call_offset;
269   if (!has_argument) {
270     call_offset = __ call_RT(noreg, noreg, target);
271   } else {
272     call_offset = __ call_RT(noreg, noreg, target, R4_ARG2);
273   }
274   OopMapSet* oop_maps = new OopMapSet();
275   oop_maps->add_gc_map(call_offset, oop_map);
276 
277   __ should_not_reach_here();
278   return oop_maps;
279 }
280 
281 static OopMapSet* generate_exception_throw_with_stack_parms(StubAssembler* sasm, address target,
282                                                             int stack_parms) {
283   // Make a frame and preserve the caller's caller-save registers.
284   const int parm_size_in_bytes = align_up(stack_parms << LogBytesPerWord, frame::alignment_in_bytes);
285   const int padding = parm_size_in_bytes - (stack_parms << LogBytesPerWord);
286   OopMap* oop_map = save_live_registers(sasm, true, noreg, parm_size_in_bytes);
287 
288   int call_offset = 0;
289   switch (stack_parms) {
290     case 3:
291     __ ld(R6_ARG4, frame_size_in_bytes + padding + 16, R1_SP);
292     case 2:
293     __ ld(R5_ARG3, frame_size_in_bytes + padding + 8, R1_SP);
294     case 1:
295     __ ld(R4_ARG2, frame_size_in_bytes + padding + 0, R1_SP);
296     case 0:
297     call_offset = __ call_RT(noreg, noreg, target);
298     break;
299     default: Unimplemented(); break;
300   }
301   OopMapSet* oop_maps = new OopMapSet();
302   oop_maps->add_gc_map(call_offset, oop_map);
303 
304   __ should_not_reach_here();
305   return oop_maps;
306 }
307 
308 
309 OopMapSet* Runtime1::generate_stub_call(StubAssembler* sasm, Register result, address target,
310                                         Register arg1, Register arg2, Register arg3) {
311   // Make a frame and preserve the caller's caller-save registers.
312   OopMap* oop_map = save_live_registers(sasm);
313 
314   int call_offset;
315   if (arg1 == noreg) {
316     call_offset = __ call_RT(result, noreg, target);
317   } else if (arg2 == noreg) {
318     call_offset = __ call_RT(result, noreg, target, arg1);
319   } else if (arg3 == noreg) {
320     call_offset = __ call_RT(result, noreg, target, arg1, arg2);
321   } else {
322     call_offset = __ call_RT(result, noreg, target, arg1, arg2, arg3);
323   }
324   OopMapSet* oop_maps = new OopMapSet();
325   oop_maps->add_gc_map(call_offset, oop_map);
326 
327   restore_live_registers(sasm, result, noreg);
328   __ blr();
329   return oop_maps;
330 }
331 
332 static OopMapSet* stub_call_with_stack_parms(StubAssembler* sasm, Register result, address target,
333                                              int stack_parms, bool do_return = true) {
334   // Make a frame and preserve the caller's caller-save registers.
335   const int parm_size_in_bytes = align_up(stack_parms << LogBytesPerWord, frame::alignment_in_bytes);
336   const int padding = parm_size_in_bytes - (stack_parms << LogBytesPerWord);
337   OopMap* oop_map = save_live_registers(sasm, true, noreg, parm_size_in_bytes);
338 
339   int call_offset = 0;
340   switch (stack_parms) {
341     case 3:
342     __ ld(R6_ARG4, frame_size_in_bytes + padding + 16, R1_SP);
343     case 2:
344     __ ld(R5_ARG3, frame_size_in_bytes + padding + 8, R1_SP);
345     case 1:
346     __ ld(R4_ARG2, frame_size_in_bytes + padding + 0, R1_SP);
347     case 0:
348     call_offset = __ call_RT(result, noreg, target);
349     break;
350     default: Unimplemented(); break;
351   }
352   OopMapSet* oop_maps = new OopMapSet();
353   oop_maps->add_gc_map(call_offset, oop_map);
354 
355   restore_live_registers(sasm, result, noreg);
356   if (do_return) __ blr();
357   return oop_maps;
358 }
359 
360 
361 OopMapSet* Runtime1::generate_patching(StubAssembler* sasm, address target) {
362   // Make a frame and preserve the caller's caller-save registers.
363   OopMap* oop_map = save_live_registers(sasm);
364 
365   // Call the runtime patching routine, returns non-zero if nmethod got deopted.
366   int call_offset = __ call_RT(noreg, noreg, target);
367   OopMapSet* oop_maps = new OopMapSet();
368   oop_maps->add_gc_map(call_offset, oop_map);
369   __ cmpdi(CR0, R3_RET, 0);
370 
371   // Re-execute the patched instruction or, if the nmethod was deoptmized,
372   // return to the deoptimization handler entry that will cause re-execution
373   // of the current bytecode.
374   DeoptimizationBlob* deopt_blob = SharedRuntime::deopt_blob();
375   assert(deopt_blob != nullptr, "deoptimization blob must have been created");
376 
377   // Return to the deoptimization handler entry for unpacking and rexecute.
378   // If we simply returned the we'd deopt as if any call we patched had just
379   // returned.
380 
381   restore_live_registers(sasm, noreg, noreg);
382   // Return if patching routine returned 0.
383   __ bclr(Assembler::bcondCRbiIs1, Assembler::bi0(CR0, Assembler::equal), Assembler::bhintbhBCLRisReturn);
384 
385   address stub = deopt_blob->unpack_with_reexecution();
386   //__ load_const_optimized(R0, stub);
387   __ add_const_optimized(R0, R29_TOC, MacroAssembler::offset_to_global_toc(stub));
388   __ mtctr(R0);
389   __ bctr();
390 
391   return oop_maps;
392 }
393 
394 OopMapSet* Runtime1::generate_code_for(C1StubId id, StubAssembler* sasm) {
395   OopMapSet* oop_maps = nullptr;
396 
397   // For better readability.
398   const bool must_gc_arguments = true;
399   const bool dont_gc_arguments = false;
400 
401   // Stub code & info for the different stubs.
402   switch (id) {
403     case C1StubId::forward_exception_id:
404       {
405         oop_maps = generate_handle_exception(id, sasm);
406       }
407       break;
408 
409     case C1StubId::new_instance_id:
410     case C1StubId::fast_new_instance_id:
411     case C1StubId::fast_new_instance_init_check_id:
412       {
413         if (id == C1StubId::new_instance_id) {
414           __ set_info("new_instance", dont_gc_arguments);
415         } else if (id == C1StubId::fast_new_instance_id) {
416           __ set_info("fast new_instance", dont_gc_arguments);
417         } else {
418           assert(id == C1StubId::fast_new_instance_init_check_id, "bad C1StubId");
419           __ set_info("fast new_instance init check", dont_gc_arguments);
420         }
421 
422         // We don't support eden allocation.
423 
424         oop_maps = generate_stub_call(sasm, R3_RET, CAST_FROM_FN_PTR(address, new_instance), R4_ARG2);
425       }
426       break;
427 
428     case C1StubId::counter_overflow_id:
429         // Bci and method are on stack.
430         oop_maps = stub_call_with_stack_parms(sasm, noreg, CAST_FROM_FN_PTR(address, counter_overflow), 2);
431       break;
432 
433     case C1StubId::new_type_array_id:
434     case C1StubId::new_object_array_id:
435       {
436         if (id == C1StubId::new_type_array_id) {
437           __ set_info("new_type_array", dont_gc_arguments);
438         } else {
439           __ set_info("new_object_array", dont_gc_arguments);
440         }
441 
442 #ifdef ASSERT
443         // Assert object type is really an array of the proper kind.
444         {
445           int tag = (id == C1StubId::new_type_array_id) ? Klass::_lh_array_tag_type_value : Klass::_lh_array_tag_ref_value;
446           Label ok;
447           __ lwz(R0, in_bytes(Klass::layout_helper_offset()), R4_ARG2);
448           __ srawi(R0, R0, Klass::_lh_array_tag_shift);
449           __ cmpwi(CR0, R0, tag);
450           __ beq(CR0, ok);
451           __ stop("assert(is an array klass)");
452           __ should_not_reach_here();
453           __ bind(ok);
454         }
455 #endif // ASSERT
456 
457         // We don't support eden allocation.
458 
459         if (id == C1StubId::new_type_array_id) {
460           oop_maps = generate_stub_call(sasm, R3_RET, CAST_FROM_FN_PTR(address, new_type_array), R4_ARG2, R5_ARG3);
461         } else {
462           oop_maps = generate_stub_call(sasm, R3_RET, CAST_FROM_FN_PTR(address, new_object_array), R4_ARG2, R5_ARG3);
463         }
464       }
465       break;
466 
467     case C1StubId::new_multi_array_id:
468       {
469         // R4: klass
470         // R5: rank
471         // R6: address of 1st dimension
472         __ set_info("new_multi_array", dont_gc_arguments);
473         oop_maps = generate_stub_call(sasm, R3_RET, CAST_FROM_FN_PTR(address, new_multi_array), R4_ARG2, R5_ARG3, R6_ARG4);
474       }
475       break;
476 
477     case C1StubId::register_finalizer_id:
478       {
479         __ set_info("register_finalizer", dont_gc_arguments);
480         // This code is called via rt_call. Hence, caller-save registers have been saved.
481         Register t = R11_scratch1;
482 
483         // Load the klass and check the has finalizer flag.
484         __ load_klass(t, R3_ARG1);
485         __ lbz(t, in_bytes(Klass::misc_flags_offset()), t);
486         __ testbitdi(CR0, R0, t, exact_log2(KlassFlags::_misc_has_finalizer));
487         // Return if has_finalizer bit == 0 (CR0.eq).
488         __ bclr(Assembler::bcondCRbiIs1, Assembler::bi0(CR0, Assembler::equal), Assembler::bhintbhBCLRisReturn);
489 
490         __ mflr(R0);
491         __ std(R0, _abi0(lr), R1_SP);
492         __ push_frame(frame::native_abi_reg_args_size, R0); // Empty dummy frame (no callee-save regs).
493         sasm->set_frame_size(frame::native_abi_reg_args_size / BytesPerWord);
494         OopMap* oop_map = new OopMap(frame::native_abi_reg_args_size / sizeof(jint), 0);
495         int call_offset = __ call_RT(noreg, noreg,
496                                      CAST_FROM_FN_PTR(address, SharedRuntime::register_finalizer), R3_ARG1);
497         oop_maps = new OopMapSet();
498         oop_maps->add_gc_map(call_offset, oop_map);
499 
500         __ pop_frame();
501         __ ld(R0, _abi0(lr), R1_SP);
502         __ mtlr(R0);
503         __ blr();
504       }
505       break;
506 
507     case C1StubId::throw_range_check_failed_id:
508       {
509         __ set_info("range_check_failed", dont_gc_arguments); // Arguments will be discarded.
510         oop_maps = generate_exception_throw_with_stack_parms(sasm, CAST_FROM_FN_PTR(address, throw_range_check_exception), 2);
511       }
512       break;
513 
514     case C1StubId::throw_index_exception_id:
515       {
516         __ set_info("index_range_check_failed", dont_gc_arguments); // Arguments will be discarded.
517         oop_maps = generate_exception_throw(sasm, CAST_FROM_FN_PTR(address, throw_index_exception), true);
518       }
519       break;
520 
521     case C1StubId::throw_div0_exception_id:
522       {
523         __ set_info("throw_div0_exception", dont_gc_arguments);
524         oop_maps = generate_exception_throw(sasm, CAST_FROM_FN_PTR(address, throw_div0_exception), false);
525       }
526       break;
527 
528     case C1StubId::throw_null_pointer_exception_id:
529       {
530         __ set_info("throw_null_pointer_exception", dont_gc_arguments);
531         oop_maps = generate_exception_throw(sasm, CAST_FROM_FN_PTR(address, throw_null_pointer_exception), false);
532       }
533       break;
534 
535     case C1StubId::handle_exception_nofpu_id:
536     case C1StubId::handle_exception_id:
537       {
538         __ set_info("handle_exception", dont_gc_arguments);
539         oop_maps = generate_handle_exception(id, sasm);
540       }
541       break;
542 
543     case C1StubId::handle_exception_from_callee_id:
544       {
545         __ set_info("handle_exception_from_callee", dont_gc_arguments);
546         oop_maps = generate_handle_exception(id, sasm);
547       }
548       break;
549 
550     case C1StubId::unwind_exception_id:
551       {
552         const Register Rexception    = R3 /*LIRGenerator::exceptionOopOpr()*/,
553                        Rexception_pc = R4 /*LIRGenerator::exceptionPcOpr()*/,
554                        Rexception_save = R31, Rcaller_sp = R30;
555         __ set_info("unwind_exception", dont_gc_arguments);
556 
557         if (AbortVMOnException) {
558           save_live_registers(sasm);
559           __ call_VM_leaf(CAST_FROM_FN_PTR(address, check_abort_on_vm_exception), Rexception);
560           restore_live_registers(sasm, noreg, noreg);
561         }
562 
563         __ ld(Rcaller_sp, 0, R1_SP);
564         __ push_frame_reg_args(0, R0); // dummy frame for C call
565         __ mr(Rexception_save, Rexception); // save over C call
566         __ ld(Rexception_pc, _abi0(lr), Rcaller_sp); // return pc
567         __ call_VM_leaf(CAST_FROM_FN_PTR(address, SharedRuntime::exception_handler_for_return_address), R16_thread, Rexception_pc);
568         __ verify_not_null_oop(Rexception_save);
569         __ mtctr(R3_RET);
570         __ ld(Rexception_pc, _abi0(lr), Rcaller_sp); // return pc
571         __ mr(R1_SP, Rcaller_sp); // Pop both frames at once.
572         __ mr(Rexception, Rexception_save); // restore
573         __ mtlr(Rexception_pc);
574         __ bctr();
575       }
576       break;
577 
578     case C1StubId::throw_array_store_exception_id:
579       {
580         __ set_info("throw_array_store_exception", dont_gc_arguments);
581         oop_maps = generate_exception_throw(sasm, CAST_FROM_FN_PTR(address, throw_array_store_exception), true);
582       }
583       break;
584 
585     case C1StubId::throw_class_cast_exception_id:
586       {
587         __ set_info("throw_class_cast_exception", dont_gc_arguments);
588         oop_maps = generate_exception_throw(sasm, CAST_FROM_FN_PTR(address, throw_class_cast_exception), true);
589       }
590       break;
591 
592     case C1StubId::throw_incompatible_class_change_error_id:
593       {
594         __ set_info("throw_incompatible_class_cast_exception", dont_gc_arguments);
595         oop_maps = generate_exception_throw(sasm, CAST_FROM_FN_PTR(address, throw_incompatible_class_change_error), false);
596       }
597       break;
598 
599     case C1StubId::slow_subtype_check_id:
600       { // Support for uint StubRoutine::partial_subtype_check( Klass sub, Klass super );
601         const Register sub_klass = R5,
602                        super_klass = R4,
603                        temp1_reg = R6;
604         __ check_klass_subtype_slow_path(sub_klass, super_klass, temp1_reg, noreg);
605         // Result is in CR0.
606         __ blr();
607       }
608       break;
609 
610     case C1StubId::is_instance_of_id:
611       {
612         // Called like a C function, but without FunctionDescriptor (see LIR_Assembler::rt_call).
613 
614         // Arguments and return value.
615         Register mirror = R3_ARG1;
616         Register obj    = R4_ARG2;
617         Register result = R3_RET;
618 
619         // Other argument registers can be used as temp registers.
620         Register klass  = R5;
621         Register offset = R6;
622         Register sub_klass = R7;
623 
624         Label is_secondary, success;
625 
626         // Get the Klass*.
627         __ ld(klass, java_lang_Class::klass_offset(), mirror);
628 
629         // Return false if obj or klass is null.
630         mirror = noreg; // killed by next instruction
631         __ li(result, 0); // assume result is false
632         __ cmpdi(CR0, obj, 0);
633         __ cmpdi(CR1, klass, 0);
634         __ cror(CR0, Assembler::equal, CR1, Assembler::equal);
635         __ bclr(Assembler::bcondCRbiIs1, Assembler::bi0(CR0, Assembler::equal), Assembler::bhintbhBCLRisReturn);
636 
637         __ lwz(offset, in_bytes(Klass::super_check_offset_offset()), klass);
638         __ load_klass(sub_klass, obj);
639         __ cmpwi(CR0, offset, in_bytes(Klass::secondary_super_cache_offset()));
640         __ beq(CR0, is_secondary); // Klass is a secondary superclass
641 
642         // Klass is a concrete class
643         __ ldx(R0, sub_klass, offset);
644         __ cmpd(CR0, klass, R0);
645         if (VM_Version::has_brw()) {
646           // Power10 can set the result by one instruction. No need for a branch.
647           __ setbc(result, CR0, Assembler::equal);
648         } else {
649           __ beq(CR0, success);
650         }
651         __ blr();
652 
653         __ bind(is_secondary);
654 
655         // This is necessary because I am never in my own secondary_super list.
656         __ cmpd(CR0, sub_klass, klass);
657         __ beq(CR0, success);
658 
659         __ lookup_secondary_supers_table_var(sub_klass, klass,
660                                              /*temps*/R9, R10, R11, R12,
661                                              /*result*/R8);
662         __ cmpdi(CR0, R8, 0); // 0 means is subclass
663         if (VM_Version::has_brw()) {
664           // Power10 can set the result by one instruction. No need for a branch.
665           __ setbc(result, CR0, Assembler::equal);
666         } else {
667           __ beq(CR0, success);
668         }
669         __ blr();
670 
671         __ bind(success);
672         __ li(result, 1);
673         __ blr();
674       }
675       break;
676 
677     case C1StubId::monitorenter_nofpu_id:
678     case C1StubId::monitorenter_id:
679       {
680         __ set_info("monitorenter", dont_gc_arguments);
681 
682         int save_fpu_registers = (id == C1StubId::monitorenter_id);
683         // Make a frame and preserve the caller's caller-save registers.
684         OopMap* oop_map = save_live_registers(sasm, save_fpu_registers);
685 
686         int call_offset = __ call_RT(noreg, noreg, CAST_FROM_FN_PTR(address, monitorenter), R4_ARG2, R5_ARG3);
687 
688         oop_maps = new OopMapSet();
689         oop_maps->add_gc_map(call_offset, oop_map);
690 
691         restore_live_registers(sasm, noreg, noreg, save_fpu_registers);
692         __ blr();
693       }
694       break;
695 
696     case C1StubId::monitorexit_nofpu_id:
697     case C1StubId::monitorexit_id:
698       {
699         // note: Really a leaf routine but must setup last java sp
700         //       => use call_RT for now (speed can be improved by
701         //       doing last java sp setup manually).
702         __ set_info("monitorexit", dont_gc_arguments);
703 
704         int save_fpu_registers = (id == C1StubId::monitorexit_id);
705         // Make a frame and preserve the caller's caller-save registers.
706         OopMap* oop_map = save_live_registers(sasm, save_fpu_registers);
707 
708         int call_offset = __ call_RT(noreg, noreg, CAST_FROM_FN_PTR(address, monitorexit), R4_ARG2);
709 
710         oop_maps = new OopMapSet();
711         oop_maps->add_gc_map(call_offset, oop_map);
712 
713         restore_live_registers(sasm, noreg, noreg, save_fpu_registers);
714         __ blr();
715       }
716       break;
717 
718     case C1StubId::deoptimize_id:
719       {
720         __ set_info("deoptimize", dont_gc_arguments);
721         __ std(R0, -8, R1_SP); // Pass trap_request on stack.
722         oop_maps = stub_call_with_stack_parms(sasm, noreg, CAST_FROM_FN_PTR(address, deoptimize), 1, /*do_return*/ false);
723 
724         DeoptimizationBlob* deopt_blob = SharedRuntime::deopt_blob();
725         assert(deopt_blob != nullptr, "deoptimization blob must have been created");
726         address stub = deopt_blob->unpack_with_reexecution();
727         //__ load_const_optimized(R0, stub);
728         __ add_const_optimized(R0, R29_TOC, MacroAssembler::offset_to_global_toc(stub));
729         __ mtctr(R0);
730         __ bctr();
731       }
732       break;
733 
734     case C1StubId::access_field_patching_id:
735       {
736         __ set_info("access_field_patching", dont_gc_arguments);
737         oop_maps = generate_patching(sasm, CAST_FROM_FN_PTR(address, access_field_patching));
738       }
739       break;
740 
741     case C1StubId::load_klass_patching_id:
742       {
743         __ set_info("load_klass_patching", dont_gc_arguments);
744         oop_maps = generate_patching(sasm, CAST_FROM_FN_PTR(address, move_klass_patching));
745       }
746       break;
747 
748     case C1StubId::load_mirror_patching_id:
749       {
750         __ set_info("load_mirror_patching", dont_gc_arguments);
751         oop_maps = generate_patching(sasm, CAST_FROM_FN_PTR(address, move_mirror_patching));
752       }
753       break;
754 
755     case C1StubId::load_appendix_patching_id:
756       {
757         __ set_info("load_appendix_patching", dont_gc_arguments);
758         oop_maps = generate_patching(sasm, CAST_FROM_FN_PTR(address, move_appendix_patching));
759       }
760       break;
761 
762     case C1StubId::dtrace_object_alloc_id:
763       { // O0: object
764         __ unimplemented("stub dtrace_object_alloc_id");
765         __ set_info("dtrace_object_alloc", dont_gc_arguments);
766 //        // We can't gc here so skip the oopmap but make sure that all
767 //        // the live registers get saved.
768 //        save_live_registers(sasm);
769 //
770 //        __ save_thread(L7_thread_cache);
771 //        __ call(CAST_FROM_FN_PTR(address, static_cast<int (*)(oopDesc*)>(SharedRuntime::dtrace_object_alloc)),
772 //                relocInfo::runtime_call_type);
773 //        __ delayed()->mov(I0, O0);
774 //        __ restore_thread(L7_thread_cache);
775 //
776 //        restore_live_registers(sasm);
777 //        __ ret();
778 //        __ delayed()->restore();
779       }
780       break;
781 
782     case C1StubId::predicate_failed_trap_id:
783       {
784         __ set_info("predicate_failed_trap", dont_gc_arguments);
785         OopMap* oop_map = save_live_registers(sasm);
786 
787         int call_offset = __ call_RT(noreg, noreg, CAST_FROM_FN_PTR(address, predicate_failed_trap));
788 
789         oop_maps = new OopMapSet();
790         oop_maps->add_gc_map(call_offset, oop_map);
791 
792         DeoptimizationBlob* deopt_blob = SharedRuntime::deopt_blob();
793         assert(deopt_blob != nullptr, "deoptimization blob must have been created");
794         restore_live_registers(sasm, noreg, noreg);
795 
796         address stub = deopt_blob->unpack_with_reexecution();
797         //__ load_const_optimized(R0, stub);
798         __ add_const_optimized(R0, R29_TOC, MacroAssembler::offset_to_global_toc(stub));
799         __ mtctr(R0);
800         __ bctr();
801       }
802       break;
803 
804   default:
805       {
806         __ set_info("unimplemented entry", dont_gc_arguments);
807         __ mflr(R0);
808         __ std(R0, _abi0(lr), R1_SP);
809         __ push_frame(frame::native_abi_reg_args_size, R0); // empty dummy frame
810         sasm->set_frame_size(frame::native_abi_reg_args_size / BytesPerWord);
811         OopMap* oop_map = new OopMap(frame::native_abi_reg_args_size / sizeof(jint), 0);
812 
813         __ load_const_optimized(R4_ARG2, (int)id);
814         int call_offset = __ call_RT(noreg, noreg, CAST_FROM_FN_PTR(address, unimplemented_entry), R4_ARG2);
815 
816         oop_maps = new OopMapSet();
817         oop_maps->add_gc_map(call_offset, oop_map);
818         __ should_not_reach_here();
819       }
820       break;
821   }
822   return oop_maps;
823 }
824 
825 
826 OopMapSet* Runtime1::generate_handle_exception(C1StubId id, StubAssembler* sasm) {
827   __ block_comment("generate_handle_exception");
828 
829   // Save registers, if required.
830   OopMapSet* oop_maps = new OopMapSet();
831   OopMap* oop_map = nullptr;
832   const Register Rexception    = R3 /*LIRGenerator::exceptionOopOpr()*/,
833                  Rexception_pc = R4 /*LIRGenerator::exceptionPcOpr()*/;
834 
835   switch (id) {
836   case C1StubId::forward_exception_id:
837     // We're handling an exception in the context of a compiled frame.
838     // The registers have been saved in the standard places. Perform
839     // an exception lookup in the caller and dispatch to the handler
840     // if found. Otherwise unwind and dispatch to the callers
841     // exception handler.
842     oop_map = generate_oop_map(sasm, true);
843     // Transfer the pending exception to the exception_oop.
844     // Also load the PC which is typically at SP + frame_size_in_bytes +_abi0(lr),
845     // but we support additional slots in the frame for parameter passing.
846     __ ld(Rexception_pc, 0, R1_SP);
847     __ ld(Rexception, in_bytes(JavaThread::pending_exception_offset()), R16_thread);
848     __ li(R0, 0);
849     __ ld(Rexception_pc, _abi0(lr), Rexception_pc);
850     __ std(R0, in_bytes(JavaThread::pending_exception_offset()), R16_thread);
851     break;
852   case C1StubId::handle_exception_nofpu_id:
853   case C1StubId::handle_exception_id:
854     // At this point all registers MAY be live.
855     oop_map = save_live_registers(sasm, id != C1StubId::handle_exception_nofpu_id, Rexception_pc);
856     break;
857   case C1StubId::handle_exception_from_callee_id:
858     // At this point all registers except exception oop and exception pc are dead.
859     oop_map = new OopMap(frame_size_in_bytes / sizeof(jint), 0);
860     sasm->set_frame_size(frame_size_in_bytes / BytesPerWord);
861     __ std(Rexception_pc, _abi0(lr), R1_SP);
862     __ push_frame(frame_size_in_bytes, R0);
863     break;
864   default:  ShouldNotReachHere();
865   }
866 
867   __ verify_not_null_oop(Rexception);
868 
869 #ifdef ASSERT
870   // Check that fields in JavaThread for exception oop and issuing pc are
871   // empty before writing to them.
872   __ ld(R0, in_bytes(JavaThread::exception_oop_offset()), R16_thread);
873   __ cmpdi(CR0, R0, 0);
874   __ asm_assert_eq("exception oop already set");
875   __ ld(R0, in_bytes(JavaThread::exception_pc_offset() ), R16_thread);
876   __ cmpdi(CR0, R0, 0);
877   __ asm_assert_eq("exception pc already set");
878 #endif
879 
880   // Save the exception and issuing pc in the thread.
881   __ std(Rexception,    in_bytes(JavaThread::exception_oop_offset()), R16_thread);
882   __ std(Rexception_pc, in_bytes(JavaThread::exception_pc_offset() ), R16_thread);
883 
884   int call_offset = __ call_RT(noreg, noreg, CAST_FROM_FN_PTR(address, exception_handler_for_pc));
885   oop_maps->add_gc_map(call_offset, oop_map);
886 
887   __ mtctr(R3_RET);
888 
889   // Note: if nmethod has been deoptimized then regardless of
890   // whether it had a handler or not we will deoptimize
891   // by entering the deopt blob with a pending exception.
892 
893   // Restore the registers that were saved at the beginning, remove
894   // the frame and jump to the exception handler.
895   switch (id) {
896   case C1StubId::forward_exception_id:
897   case C1StubId::handle_exception_nofpu_id:
898   case C1StubId::handle_exception_id:
899     restore_live_registers(sasm, noreg, noreg, id != C1StubId::handle_exception_nofpu_id);
900     __ bctr();
901     break;
902   case C1StubId::handle_exception_from_callee_id: {
903     __ pop_frame();
904     __ ld(Rexception_pc, _abi0(lr), R1_SP);
905     __ mtlr(Rexception_pc);
906     __ bctr();
907     break;
908   }
909   default:  ShouldNotReachHere();
910   }
911 
912   return oop_maps;
913 }
914 
915 const char *Runtime1::pd_name_for_address(address entry) {
916   return "<unknown function>";
917 }
918 
919 #undef __