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