1 /*
  2  * Copyright (c) 1999, 2026, Oracle and/or its affiliates. All rights reserved.
  3  * Copyright (c) 2012, 2026 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)StubId::c1_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(StubId::c1_forward_exception_id));
105       add_const_optimized(R0, R29_TOC, MacroAssembler::offset_to_global_toc(Runtime1::entry_for(StubId::c1_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 oop_result, address target,
333                                              int stack_parms, bool do_return = true, Register result2 = noreg) {
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(oop_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, oop_result, result2);
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(StubId 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 StubId::c1_forward_exception_id:
404       {
405         oop_maps = generate_handle_exception(id, sasm);
406       }
407       break;
408 
409     case StubId::c1_new_instance_id:
410     case StubId::c1_fast_new_instance_id:
411     case StubId::c1_fast_new_instance_init_check_id:
412       {
413         if (id == StubId::c1_new_instance_id) {
414           __ set_info("new_instance", dont_gc_arguments);
415         } else if (id == StubId::c1_fast_new_instance_id) {
416           __ set_info("fast new_instance", dont_gc_arguments);
417         } else {
418           assert(id == StubId::c1_fast_new_instance_init_check_id, "bad StubId");
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 StubId::c1_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 StubId::c1_new_type_array_id:
434     case StubId::c1_new_object_array_id:
435     case StubId::c1_new_null_free_array_id:
436       {
437         if (id == StubId::c1_new_type_array_id) {
438           __ set_info("new_type_array", dont_gc_arguments);
439         } else if (id == StubId::c1_new_object_array_id) {
440           __ set_info("new_object_array", dont_gc_arguments);
441         } else {
442           __ set_info("new_null_free_array", dont_gc_arguments);
443         }
444 
445 #ifdef ASSERT
446         // Assert object type is really an array of the proper kind.
447         __ lwz(R0, in_bytes(Klass::layout_helper_offset()), R4_ARG2);
448         __ srawi(R0, R0, Klass::_lh_array_tag_shift);
449         switch (id) {
450           case StubId::c1_new_type_array_id:
451             __ cmpwi(CR0, R0, Klass::_lh_array_tag_type_value);
452             __ asm_assert_eq("assert(is a type array klass)");
453             break;
454           case StubId::c1_new_object_array_id:
455             __ cmpwi(CR0, R0, Klass::_lh_array_tag_ref_value);  // new "[Ljava/lang/Object;"
456             __ cmpwi(CR1, R0, Klass::_lh_array_tag_flat_value); // new "[LVT;"
457             __ cror(CR0, Assembler::equal, CR1, Assembler::equal);
458             __ asm_assert_eq("assert(is an object or inline type array klass)");
459             break;
460           case StubId::c1_new_null_free_array_id:
461             __ cmpwi(CR0, R0, Klass::_lh_array_tag_flat_value); // the array can be a flat array.
462             __ cmpwi(CR1, R0, Klass::_lh_array_tag_ref_value);  // the array cannot be a flat array (due to the InlineArrayElementMaxFlatSize, etc.)
463             __ cror(CR0, Assembler::equal, CR1, Assembler::equal);
464             __ asm_assert_eq("assert(is an object or inline type array klass)");
465             break;
466           default: ShouldNotReachHere();
467         }
468 #endif // ASSERT
469 
470         // We don't support eden allocation.
471 
472         if (id == StubId::c1_new_type_array_id) {
473           oop_maps = generate_stub_call(sasm, R3_RET, CAST_FROM_FN_PTR(address, new_type_array), R4_ARG2, R5_ARG3);
474         } else if (id == StubId::c1_new_object_array_id) {
475           oop_maps = generate_stub_call(sasm, R3_RET, CAST_FROM_FN_PTR(address, new_object_array), R4_ARG2, R5_ARG3);
476         } else {
477           assert(id == StubId::c1_new_null_free_array_id, "must be");
478           oop_maps = generate_stub_call(sasm, R3_RET, CAST_FROM_FN_PTR(address, new_null_free_array), R4_ARG2, R5_ARG3);
479         }
480       }
481       break;
482 
483     case StubId::c1_new_multi_array_id:
484       {
485         // R4: klass
486         // R5: rank
487         // R6: address of 1st dimension
488         __ set_info("new_multi_array", dont_gc_arguments);
489         oop_maps = generate_stub_call(sasm, R3_RET, CAST_FROM_FN_PTR(address, new_multi_array), R4_ARG2, R5_ARG3, R6_ARG4);
490       }
491       break;
492 
493     case StubId::c1_buffer_inline_args_id:
494     case StubId::c1_buffer_inline_args_no_receiver_id:
495       {
496         address entry = (id == StubId::c1_buffer_inline_args_id) ?
497           CAST_FROM_FN_PTR(address, buffer_inline_args) :
498           CAST_FROM_FN_PTR(address, buffer_inline_args_no_receiver);
499 
500         __ unimplemented("c1_buffer_inline_args"); // TODO: handle arguments and return value
501         OopMap* oop_map = save_live_registers(sasm);
502         int call_offset = __ call_RT(noreg, noreg, entry, R3_ARG1);
503 
504         oop_maps = new OopMapSet();
505         oop_maps->add_gc_map(call_offset, oop_map);
506         restore_live_registers(sasm, R3_RET, noreg);
507         __ blr();
508       }
509       break;
510 
511     case StubId::c1_load_flat_array_id:
512       oop_maps = stub_call_with_stack_parms(sasm, R3_RET, CAST_FROM_FN_PTR(address, load_flat_array), 2);
513       break;
514 
515     case StubId::c1_store_flat_array_id:
516       oop_maps = stub_call_with_stack_parms(sasm, noreg, CAST_FROM_FN_PTR(address, store_flat_array), 3);
517       break;
518 
519     case StubId::c1_substitutability_check_id:
520       oop_maps = stub_call_with_stack_parms(sasm, noreg, CAST_FROM_FN_PTR(address, substitutability_check), 2, true, R3_RET);
521       break;
522 
523     case StubId::c1_register_finalizer_id:
524       {
525         __ set_info("register_finalizer", dont_gc_arguments);
526         // This code is called via rt_call. Hence, caller-save registers have been saved.
527         Register t = R11_scratch1;
528 
529         // Load the klass and check the has finalizer flag.
530         __ load_klass(t, R3_ARG1);
531         __ lbz(t, in_bytes(Klass::misc_flags_offset()), t);
532         __ testbitdi(CR0, R0, t, exact_log2(KlassFlags::_misc_has_finalizer));
533         // Return if has_finalizer bit == 0 (CR0.eq).
534         __ bclr(Assembler::bcondCRbiIs1, Assembler::bi0(CR0, Assembler::equal), Assembler::bhintbhBCLRisReturn);
535 
536         __ mflr(R0);
537         __ std(R0, _abi0(lr), R1_SP);
538         __ push_frame(frame::native_abi_reg_args_size, R0); // Empty dummy frame (no callee-save regs).
539         sasm->set_frame_size(frame::native_abi_reg_args_size / BytesPerWord);
540         OopMap* oop_map = new OopMap(frame::native_abi_reg_args_size / sizeof(jint), 0);
541         int call_offset = __ call_RT(noreg, noreg,
542                                      CAST_FROM_FN_PTR(address, SharedRuntime::register_finalizer), R3_ARG1);
543         oop_maps = new OopMapSet();
544         oop_maps->add_gc_map(call_offset, oop_map);
545 
546         __ pop_frame();
547         __ ld(R0, _abi0(lr), R1_SP);
548         __ mtlr(R0);
549         __ blr();
550       }
551       break;
552 
553     case StubId::c1_throw_range_check_failed_id:
554       {
555         __ set_info("range_check_failed", dont_gc_arguments); // Arguments will be discarded.
556         oop_maps = generate_exception_throw_with_stack_parms(sasm, CAST_FROM_FN_PTR(address, throw_range_check_exception), 2);
557       }
558       break;
559 
560     case StubId::c1_throw_index_exception_id:
561       {
562         __ set_info("index_range_check_failed", dont_gc_arguments); // Arguments will be discarded.
563         oop_maps = generate_exception_throw(sasm, CAST_FROM_FN_PTR(address, throw_index_exception), true);
564       }
565       break;
566 
567     case StubId::c1_throw_div0_exception_id:
568       {
569         __ set_info("throw_div0_exception", dont_gc_arguments);
570         oop_maps = generate_exception_throw(sasm, CAST_FROM_FN_PTR(address, throw_div0_exception), false);
571       }
572       break;
573 
574     case StubId::c1_throw_null_pointer_exception_id:
575       {
576         __ set_info("throw_null_pointer_exception", dont_gc_arguments);
577         oop_maps = generate_exception_throw(sasm, CAST_FROM_FN_PTR(address, throw_null_pointer_exception), false);
578       }
579       break;
580 
581     case StubId::c1_handle_exception_nofpu_id:
582     case StubId::c1_handle_exception_id:
583       {
584         __ set_info("handle_exception", dont_gc_arguments);
585         oop_maps = generate_handle_exception(id, sasm);
586       }
587       break;
588 
589     case StubId::c1_handle_exception_from_callee_id:
590       {
591         __ set_info("handle_exception_from_callee", dont_gc_arguments);
592         oop_maps = generate_handle_exception(id, sasm);
593       }
594       break;
595 
596     case StubId::c1_unwind_exception_id:
597       {
598         const Register Rexception    = R3 /*LIRGenerator::exceptionOopOpr()*/,
599                        Rexception_pc = R4 /*LIRGenerator::exceptionPcOpr()*/,
600                        Rexception_save = R31, Rcaller_sp = R30;
601         __ set_info("unwind_exception", dont_gc_arguments);
602 
603         if (AbortVMOnException) {
604           save_live_registers(sasm);
605           __ call_VM_leaf(CAST_FROM_FN_PTR(address, check_abort_on_vm_exception), Rexception);
606           restore_live_registers(sasm, noreg, noreg);
607         }
608 
609         __ ld(Rcaller_sp, 0, R1_SP);
610         __ push_frame_reg_args(0, R0); // dummy frame for C call
611         __ mr(Rexception_save, Rexception); // save over C call
612         __ ld(Rexception_pc, _abi0(lr), Rcaller_sp); // return pc
613         __ call_VM_leaf(CAST_FROM_FN_PTR(address, SharedRuntime::exception_handler_for_return_address), R16_thread, Rexception_pc);
614         __ verify_not_null_oop(Rexception_save);
615         __ mtctr(R3_RET);
616         __ ld(Rexception_pc, _abi0(lr), Rcaller_sp); // return pc
617         __ mr(R1_SP, Rcaller_sp); // Pop both frames at once.
618         __ mr(Rexception, Rexception_save); // restore
619         __ mtlr(Rexception_pc);
620         __ bctr();
621       }
622       break;
623 
624     case StubId::c1_throw_array_store_exception_id:
625       {
626         __ set_info("throw_array_store_exception", dont_gc_arguments);
627         oop_maps = generate_exception_throw(sasm, CAST_FROM_FN_PTR(address, throw_array_store_exception), true);
628       }
629       break;
630 
631     case StubId::c1_throw_class_cast_exception_id:
632       {
633         __ set_info("throw_class_cast_exception", dont_gc_arguments);
634         oop_maps = generate_exception_throw(sasm, CAST_FROM_FN_PTR(address, throw_class_cast_exception), true);
635       }
636       break;
637 
638     case StubId::c1_throw_incompatible_class_change_error_id:
639       {
640         __ set_info("throw_incompatible_class_change_error", dont_gc_arguments);
641         oop_maps = generate_exception_throw(sasm, CAST_FROM_FN_PTR(address, throw_incompatible_class_change_error), false);
642       }
643       break;
644 
645     case StubId::c1_throw_illegal_monitor_state_exception_id:
646       {
647         __ set_info("c1_throw_illegal_monitor_state_exception", dont_gc_arguments);
648         oop_maps = generate_exception_throw(sasm, CAST_FROM_FN_PTR(address, throw_illegal_monitor_state_exception), false);
649       }
650       break;
651 
652     case StubId::c1_throw_identity_exception_id:
653       {
654         __ set_info("throw_identity_exception", dont_gc_arguments);
655         oop_maps = generate_exception_throw(sasm, CAST_FROM_FN_PTR(address, throw_identity_exception), true);
656       }
657       break;
658 
659     case StubId::c1_slow_subtype_check_id:
660       { // Support for uint StubRoutine::partial_subtype_check( Klass sub, Klass super );
661         const Register sub_klass = R5,
662                        super_klass = R4,
663                        temp1_reg = R6;
664         __ check_klass_subtype_slow_path(sub_klass, super_klass, temp1_reg, noreg);
665         // Result is in CR0.
666         __ blr();
667       }
668       break;
669 
670     case StubId::c1_is_instance_of_id:
671       {
672         // Called like a C function, but without FunctionDescriptor (see LIR_Assembler::rt_call).
673 
674         // Arguments and return value.
675         Register mirror = R3_ARG1;
676         Register obj    = R4_ARG2;
677         Register result = R3_RET;
678 
679         // Other argument registers can be used as temp registers.
680         Register klass  = R5;
681         Register offset = R6;
682         Register sub_klass = R7;
683 
684         Label is_secondary, success;
685 
686         // Get the Klass*.
687         __ ld(klass, java_lang_Class::klass_offset(), mirror);
688 
689         // Return false if obj or klass is null.
690         mirror = noreg; // killed by next instruction
691         __ li(result, 0); // assume result is false
692         __ cmpdi(CR0, obj, 0);
693         __ cmpdi(CR1, klass, 0);
694         __ cror(CR0, Assembler::equal, CR1, Assembler::equal);
695         __ bclr(Assembler::bcondCRbiIs1, Assembler::bi0(CR0, Assembler::equal), Assembler::bhintbhBCLRisReturn);
696 
697         __ lwz(offset, in_bytes(Klass::super_check_offset_offset()), klass);
698         __ load_klass(sub_klass, obj);
699         __ cmpwi(CR0, offset, in_bytes(Klass::secondary_super_cache_offset()));
700         __ beq(CR0, is_secondary); // Klass is a secondary superclass
701 
702         // Klass is a concrete class
703         __ ldx(R0, sub_klass, offset);
704         __ cmpd(CR0, klass, R0);
705         if (VM_Version::has_brw()) {
706           // Power10 can set the result by one instruction. No need for a branch.
707           __ setbc(result, CR0, Assembler::equal);
708         } else {
709           __ beq(CR0, success);
710         }
711         __ blr();
712 
713         __ bind(is_secondary);
714 
715         // This is necessary because I am never in my own secondary_super list.
716         __ cmpd(CR0, sub_klass, klass);
717         __ beq(CR0, success);
718 
719         __ lookup_secondary_supers_table_var(sub_klass, klass,
720                                              /*temps*/R9, R10, R11, R12,
721                                              /*result*/R8);
722         __ cmpdi(CR0, R8, 0); // 0 means is subclass
723         if (VM_Version::has_brw()) {
724           // Power10 can set the result by one instruction. No need for a branch.
725           __ setbc(result, CR0, Assembler::equal);
726         } else {
727           __ beq(CR0, success);
728         }
729         __ blr();
730 
731         __ bind(success);
732         __ li(result, 1);
733         __ blr();
734       }
735       break;
736 
737     case StubId::c1_monitorenter_nofpu_id:
738     case StubId::c1_monitorenter_id:
739       {
740         __ set_info("monitorenter", dont_gc_arguments);
741 
742         int save_fpu_registers = (id == StubId::c1_monitorenter_id);
743         // Make a frame and preserve the caller's caller-save registers.
744         OopMap* oop_map = save_live_registers(sasm, save_fpu_registers);
745 
746         int call_offset = __ call_RT(noreg, noreg, CAST_FROM_FN_PTR(address, monitorenter), R4_ARG2, R5_ARG3);
747 
748         oop_maps = new OopMapSet();
749         oop_maps->add_gc_map(call_offset, oop_map);
750 
751         restore_live_registers(sasm, noreg, noreg, save_fpu_registers);
752         __ blr();
753       }
754       break;
755 
756     case StubId::c1_monitorexit_nofpu_id:
757     case StubId::c1_monitorexit_id:
758       {
759         // note: Really a leaf routine but must setup last java sp
760         //       => use call_RT for now (speed can be improved by
761         //       doing last java sp setup manually).
762         __ set_info("monitorexit", dont_gc_arguments);
763 
764         int save_fpu_registers = (id == StubId::c1_monitorexit_id);
765         // Make a frame and preserve the caller's caller-save registers.
766         OopMap* oop_map = save_live_registers(sasm, save_fpu_registers);
767 
768         int call_offset = __ call_RT(noreg, noreg, CAST_FROM_FN_PTR(address, monitorexit), R4_ARG2);
769 
770         oop_maps = new OopMapSet();
771         oop_maps->add_gc_map(call_offset, oop_map);
772 
773         restore_live_registers(sasm, noreg, noreg, save_fpu_registers);
774         __ blr();
775       }
776       break;
777 
778     case StubId::c1_deoptimize_id:
779       {
780         __ set_info("deoptimize", dont_gc_arguments);
781         __ std(R0, -8, R1_SP); // Pass trap_request on stack.
782         oop_maps = stub_call_with_stack_parms(sasm, noreg, CAST_FROM_FN_PTR(address, deoptimize), 1, /*do_return*/ false);
783 
784         DeoptimizationBlob* deopt_blob = SharedRuntime::deopt_blob();
785         assert(deopt_blob != nullptr, "deoptimization blob must have been created");
786         address stub = deopt_blob->unpack_with_reexecution();
787         //__ load_const_optimized(R0, stub);
788         __ add_const_optimized(R0, R29_TOC, MacroAssembler::offset_to_global_toc(stub));
789         __ mtctr(R0);
790         __ bctr();
791       }
792       break;
793 
794     case StubId::c1_access_field_patching_id:
795       {
796         __ set_info("access_field_patching", dont_gc_arguments);
797         oop_maps = generate_patching(sasm, CAST_FROM_FN_PTR(address, access_field_patching));
798       }
799       break;
800 
801     case StubId::c1_load_klass_patching_id:
802       {
803         __ set_info("load_klass_patching", dont_gc_arguments);
804         oop_maps = generate_patching(sasm, CAST_FROM_FN_PTR(address, move_klass_patching));
805       }
806       break;
807 
808     case StubId::c1_load_mirror_patching_id:
809       {
810         __ set_info("load_mirror_patching", dont_gc_arguments);
811         oop_maps = generate_patching(sasm, CAST_FROM_FN_PTR(address, move_mirror_patching));
812       }
813       break;
814 
815     case StubId::c1_load_appendix_patching_id:
816       {
817         __ set_info("load_appendix_patching", dont_gc_arguments);
818         oop_maps = generate_patching(sasm, CAST_FROM_FN_PTR(address, move_appendix_patching));
819       }
820       break;
821 
822     case StubId::c1_dtrace_object_alloc_id:
823       {
824         __ unimplemented("stub dtrace_object_alloc_id");
825         __ set_info("dtrace_object_alloc", dont_gc_arguments);
826       }
827       break;
828 
829     case StubId::c1_predicate_failed_trap_id:
830       {
831         __ set_info("predicate_failed_trap", dont_gc_arguments);
832         OopMap* oop_map = save_live_registers(sasm);
833 
834         int call_offset = __ call_RT(noreg, noreg, CAST_FROM_FN_PTR(address, predicate_failed_trap));
835 
836         oop_maps = new OopMapSet();
837         oop_maps->add_gc_map(call_offset, oop_map);
838 
839         DeoptimizationBlob* deopt_blob = SharedRuntime::deopt_blob();
840         assert(deopt_blob != nullptr, "deoptimization blob must have been created");
841         restore_live_registers(sasm, noreg, noreg);
842 
843         address stub = deopt_blob->unpack_with_reexecution();
844         //__ load_const_optimized(R0, stub);
845         __ add_const_optimized(R0, R29_TOC, MacroAssembler::offset_to_global_toc(stub));
846         __ mtctr(R0);
847         __ bctr();
848       }
849       break;
850 
851   default:
852       {
853         __ set_info("unimplemented entry", dont_gc_arguments);
854         __ mflr(R0);
855         __ std(R0, _abi0(lr), R1_SP);
856         __ push_frame(frame::native_abi_reg_args_size, R0); // empty dummy frame
857         sasm->set_frame_size(frame::native_abi_reg_args_size / BytesPerWord);
858         OopMap* oop_map = new OopMap(frame::native_abi_reg_args_size / sizeof(jint), 0);
859 
860         __ load_const_optimized(R4_ARG2, (int)id);
861         int call_offset = __ call_RT(noreg, noreg, CAST_FROM_FN_PTR(address, unimplemented_entry), R4_ARG2);
862 
863         oop_maps = new OopMapSet();
864         oop_maps->add_gc_map(call_offset, oop_map);
865         __ should_not_reach_here();
866       }
867       break;
868   }
869   return oop_maps;
870 }
871 
872 
873 OopMapSet* Runtime1::generate_handle_exception(StubId id, StubAssembler* sasm) {
874   __ block_comment("generate_handle_exception");
875 
876   // Save registers, if required.
877   OopMapSet* oop_maps = new OopMapSet();
878   OopMap* oop_map = nullptr;
879   const Register Rexception    = R3 /*LIRGenerator::exceptionOopOpr()*/,
880                  Rexception_pc = R4 /*LIRGenerator::exceptionPcOpr()*/;
881 
882   switch (id) {
883   case StubId::c1_forward_exception_id:
884     // We're handling an exception in the context of a compiled frame.
885     // The registers have been saved in the standard places. Perform
886     // an exception lookup in the caller and dispatch to the handler
887     // if found. Otherwise unwind and dispatch to the callers
888     // exception handler.
889     oop_map = generate_oop_map(sasm, true);
890     // Transfer the pending exception to the exception_oop.
891     // Also load the PC which is typically at SP + frame_size_in_bytes +_abi0(lr),
892     // but we support additional slots in the frame for parameter passing.
893     __ ld(Rexception_pc, 0, R1_SP);
894     __ ld(Rexception, in_bytes(JavaThread::pending_exception_offset()), R16_thread);
895     __ li(R0, 0);
896     __ ld(Rexception_pc, _abi0(lr), Rexception_pc);
897     __ std(R0, in_bytes(JavaThread::pending_exception_offset()), R16_thread);
898     break;
899   case StubId::c1_handle_exception_nofpu_id:
900   case StubId::c1_handle_exception_id:
901     // At this point all registers MAY be live.
902     oop_map = save_live_registers(sasm, id != StubId::c1_handle_exception_nofpu_id, Rexception_pc);
903     break;
904   case StubId::c1_handle_exception_from_callee_id:
905     // At this point all registers except exception oop and exception pc are dead.
906     oop_map = new OopMap(frame_size_in_bytes / sizeof(jint), 0);
907     sasm->set_frame_size(frame_size_in_bytes / BytesPerWord);
908     __ std(Rexception_pc, _abi0(lr), R1_SP);
909     __ push_frame(frame_size_in_bytes, R0);
910     break;
911   default:  ShouldNotReachHere();
912   }
913 
914   __ verify_not_null_oop(Rexception);
915 
916 #ifdef ASSERT
917   // Check that fields in JavaThread for exception oop and issuing pc are
918   // empty before writing to them.
919   __ ld(R0, in_bytes(JavaThread::exception_oop_offset()), R16_thread);
920   __ cmpdi(CR0, R0, 0);
921   __ asm_assert_eq("exception oop already set");
922   __ ld(R0, in_bytes(JavaThread::exception_pc_offset() ), R16_thread);
923   __ cmpdi(CR0, R0, 0);
924   __ asm_assert_eq("exception pc already set");
925 #endif
926 
927   // Save the exception and issuing pc in the thread.
928   __ std(Rexception,    in_bytes(JavaThread::exception_oop_offset()), R16_thread);
929   __ std(Rexception_pc, in_bytes(JavaThread::exception_pc_offset() ), R16_thread);
930 
931   int call_offset = __ call_RT(noreg, noreg, CAST_FROM_FN_PTR(address, exception_handler_for_pc));
932   oop_maps->add_gc_map(call_offset, oop_map);
933 
934   __ mtctr(R3_RET);
935 
936   // Note: if nmethod has been deoptimized then regardless of
937   // whether it had a handler or not we will deoptimize
938   // by entering the deopt blob with a pending exception.
939 
940   // Restore the registers that were saved at the beginning, remove
941   // the frame and jump to the exception handler.
942   switch (id) {
943   case StubId::c1_forward_exception_id:
944   case StubId::c1_handle_exception_nofpu_id:
945   case StubId::c1_handle_exception_id:
946     restore_live_registers(sasm, noreg, noreg, id != StubId::c1_handle_exception_nofpu_id);
947     __ bctr();
948     break;
949   case StubId::c1_handle_exception_from_callee_id: {
950     __ pop_frame();
951     __ ld(Rexception_pc, _abi0(lr), R1_SP);
952     __ mtlr(Rexception_pc);
953     __ bctr();
954     break;
955   }
956   default:  ShouldNotReachHere();
957   }
958 
959   return oop_maps;
960 }
961 
962 const char *Runtime1::pd_name_for_address(address entry) {
963   return "<unknown function>";
964 }
965 
966 #undef __