1 /*
  2  * Copyright (c) 2016, 2026, Oracle and/or its affiliates. All rights reserved.
  3  * Copyright (c) 2016, 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 "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 "memory/universe.hpp"
 36 #include "nativeInst_s390.hpp"
 37 #include "oops/oop.inline.hpp"
 38 #include "prims/jvmtiExport.hpp"
 39 #include "register_s390.hpp"
 40 #include "registerSaver_s390.hpp"
 41 #include "runtime/sharedRuntime.hpp"
 42 #include "runtime/signature.hpp"
 43 #include "runtime/stubRoutines.hpp"
 44 #include "runtime/vframeArray.hpp"
 45 #include "utilities/macros.hpp"
 46 #include "utilities/powerOfTwo.hpp"
 47 #include "vmreg_s390.inline.hpp"
 48 
 49 // Implementation of StubAssembler
 50 
 51 int StubAssembler::call_RT(Register oop_result1, Register metadata_result, 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()) || oop_result1 != metadata_result, "registers must be different");
 54 
 55   Label resume;
 56   z_larl(Z_R1_scratch, resume);
 57   set_last_Java_frame(Z_SP, Z_R1_scratch);
 58 
 59   // ARG1 must hold thread address.
 60   z_lgr(Z_ARG1, Z_thread);
 61 
 62   address return_pc = nullptr;
 63   align_call_far_patchable(this->pc());
 64   return_pc = call_c_opt(entry_point);
 65 
 66   bind(resume);
 67   int call_offset = offset();
 68   assert(return_pc != nullptr, "const section overflow");
 69 
 70   reset_last_Java_frame(/* check_last_java_sp= */ false);
 71 
 72   // Check for pending exceptions.
 73   {
 74     load_and_test_long(Z_R0_scratch, Address(Z_thread, Thread::pending_exception_offset()));
 75 
 76     // This used to conditionally jump to forward_exception however it is
 77     // possible if we relocate that the branch will not reach. So we must jump
 78     // around so we can always reach.
 79 
 80     Label ok;
 81     z_bre(ok); // Bcondequal is the same as bcondZero.
 82 
 83     // exception pending => forward to exception handler
 84 
 85     // Make sure that the vm_results are cleared.
 86     if (oop_result1->is_valid()) {
 87       clear_mem(Address(Z_thread, JavaThread::vm_result_oop_offset()), sizeof(jlong));
 88     }
 89     if (metadata_result->is_valid()) {
 90       clear_mem(Address(Z_thread, JavaThread::vm_result_metadata_offset()), sizeof(jlong));
 91     }
 92     if (frame_size() == no_frame_size) {
 93       // Pop the stub frame.
 94       pop_frame();
 95       restore_return_pc();
 96       load_const_optimized(Z_R1, StubRoutines::forward_exception_entry());
 97       z_br(Z_R1);
 98     } else if (_stub_id == (int)StubId::c1_forward_exception_id) {
 99       should_not_reach_here();
100     } else {
101       load_const_optimized(Z_R1, Runtime1::entry_for (StubId::c1_forward_exception_id));
102       z_br(Z_R1);
103     }
104 
105     bind(ok);
106   }
107 
108   // Get oop results if there are any and reset the values in the thread.
109   if (oop_result1->is_valid()) {
110     get_vm_result_oop(oop_result1);
111   }
112   if (metadata_result->is_valid()) {
113     get_vm_result_metadata(metadata_result);
114   }
115 
116   return call_offset;
117 }
118 
119 
120 int StubAssembler::call_RT(Register oop_result1, Register metadata_result, address entry, Register arg1) {
121   // Z_ARG1 is reserved for the thread.
122   lgr_if_needed(Z_ARG2, arg1);
123   return call_RT(oop_result1, metadata_result, entry, 1);
124 }
125 
126 
127 int StubAssembler::call_RT(Register oop_result1, Register metadata_result, address entry, Register arg1, Register arg2) {
128   // Z_ARG1 is reserved for the thread.
129   lgr_if_needed(Z_ARG2, arg1);
130   assert(arg2 != Z_ARG2, "smashed argument");
131   lgr_if_needed(Z_ARG3, arg2);
132   return call_RT(oop_result1, metadata_result, entry, 2);
133 }
134 
135 
136 int StubAssembler::call_RT(Register oop_result1, Register metadata_result, address entry, Register arg1, Register arg2, Register arg3) {
137   // Z_ARG1 is reserved for the thread.
138   lgr_if_needed(Z_ARG2, arg1);
139   assert(arg2 != Z_ARG2, "smashed argument");
140   lgr_if_needed(Z_ARG3, arg2);
141   assert(arg3 != Z_ARG3, "smashed argument");
142   lgr_if_needed(Z_ARG4, arg3);
143   return call_RT(oop_result1, metadata_result, entry, 3);
144 }
145 
146 
147 // Implementation of Runtime1
148 
149 #define __ sasm->
150 
151 #ifndef PRODUCT
152 #undef  __
153 #define __ (Verbose ? (sasm->block_comment(FILE_AND_LINE),sasm):sasm)->
154 #endif // !PRODUCT
155 
156 #define BLOCK_COMMENT(str) if (PrintAssembly) __ block_comment(str)
157 #define BIND(label)        bind(label); BLOCK_COMMENT(#label ":")
158 
159 static OopMap* generate_oop_map(StubAssembler* sasm) {
160   RegisterSaver::RegisterSet reg_set = RegisterSaver::all_registers;
161   int frame_size_in_slots =
162     RegisterSaver::live_reg_frame_size(reg_set) / VMRegImpl::stack_slot_size;
163   sasm->set_frame_size(frame_size_in_slots / VMRegImpl::slots_per_word);
164   return RegisterSaver::generate_oop_map(sasm, reg_set);
165 }
166 
167 static OopMap* save_live_registers(StubAssembler* sasm, bool save_fpu_registers = true, Register return_pc = Z_R14) {
168   __ block_comment("save_live_registers");
169   RegisterSaver::RegisterSet reg_set =
170     save_fpu_registers ? RegisterSaver::all_registers : RegisterSaver::all_integer_registers;
171   int frame_size_in_slots =
172     RegisterSaver::live_reg_frame_size(reg_set) / VMRegImpl::stack_slot_size;
173   sasm->set_frame_size(frame_size_in_slots / VMRegImpl::slots_per_word);
174   return RegisterSaver::save_live_registers(sasm, reg_set, return_pc);
175 }
176 
177 static OopMap* save_live_registers_except_r2(StubAssembler* sasm, bool save_fpu_registers = true) {
178   if (!save_fpu_registers) {
179     __ unimplemented(FILE_AND_LINE);
180   }
181   __ block_comment("save_live_registers");
182   RegisterSaver::RegisterSet reg_set = RegisterSaver::all_registers_except_r2;
183   int frame_size_in_slots =
184       RegisterSaver::live_reg_frame_size(reg_set) / VMRegImpl::stack_slot_size;
185   sasm->set_frame_size(frame_size_in_slots / VMRegImpl::slots_per_word);
186   return RegisterSaver::save_live_registers(sasm, reg_set);
187 }
188 
189 static void restore_live_registers(StubAssembler* sasm, bool restore_fpu_registers = true) {
190   __ block_comment("restore_live_registers");
191   RegisterSaver::RegisterSet reg_set =
192     restore_fpu_registers ? RegisterSaver::all_registers : RegisterSaver::all_integer_registers;
193   RegisterSaver::restore_live_registers(sasm, reg_set);
194 }
195 
196 static void restore_live_registers_except_r2(StubAssembler* sasm, bool restore_fpu_registers = true) {
197   if (!restore_fpu_registers) {
198     __ unimplemented(FILE_AND_LINE);
199   }
200   __ block_comment("restore_live_registers_except_r2");
201   RegisterSaver::restore_live_registers(sasm, RegisterSaver::all_registers_except_r2);
202 }
203 
204 void Runtime1::initialize_pd() {
205   // Nothing to do.
206 }
207 
208 uint Runtime1::runtime_blob_current_thread_offset(frame f) {
209   CodeBlob* cb = f.cb();
210   assert(cb == Runtime1::blob_for(StubId::c1_monitorenter_id) ||
211          cb == Runtime1::blob_for(StubId::c1_monitorenter_nofpu_id), "must be");
212   assert(cb != nullptr && cb->is_runtime_stub(), "invalid frame");
213 
214   // Calculate the offset of Z_thread (Z_R8) in the saved register area.
215   // Both c1_monitorenter_id and c1_monitorenter_nofpu_id have the same frame layout:
216   // - c1_monitorenter_id uses RegisterSaver::all_registers (saves FPU regs)
217   // - c1_monitorenter_nofpu_id uses RegisterSaver::all_integer_registers (excludes FPU regs but reserves space)
218   //
219   // From RegisterSaver_LiveRegs and RegisterSaver_LiveIntRegs:
220   // Both have 15 float register slots (F0, F2-F15, F1 is excluded as scratch)
221   // Then integer registers: R2, R3, R4, R5, R6, R7, R8, R9, R10, R11, R12, R13
222   // Z_thread is Z_R8, which is the 7th integer register (index 6 from R2)
223   //
224   // Stack layout from SP:
225   //   [0..159]     : z_abi_160
226   //   [160..279]   : 15 float register slots (15 * 8 = 120 bytes)
227   //   [280..327]   : R2-R7 (6 * 8 = 48 bytes)
228   //   [328..335]   : R8 (Z_thread) <- this is what we need
229   //
230   // Offset = 160 + 120 + 48 = 328 bytes from SP
231   // Return value is in 64-bit words: 328 / 8 = 41
232 
233   const int float_reg_slots = 15;      // F0, F2-F15 (F1 is scratch, excluded)
234   const int int_regs_before_r8 = 6;    // R2, R3, R4, R5, R6, R7
235   const int z_thread_offset = frame::z_abi_160_size +
236                                (float_reg_slots * 8) +
237                                (int_regs_before_r8 * 8);
238 
239   return z_thread_offset / wordSize;
240 }
241 
242 OopMapSet* Runtime1::generate_exception_throw(StubAssembler* sasm, address target, bool has_argument) {
243   // Make a frame and preserve the caller's caller-save registers.
244   OopMap* oop_map = save_live_registers(sasm);
245   int call_offset;
246   if (!has_argument) {
247     call_offset = __ call_RT(noreg, noreg, target);
248   } else {
249     call_offset = __ call_RT(noreg, noreg, target, Z_R1_scratch, Z_R0_scratch);
250   }
251   OopMapSet* oop_maps = new OopMapSet();
252   oop_maps->add_gc_map(call_offset, oop_map);
253 
254   __ should_not_reach_here();
255   return oop_maps;
256 }
257 
258 void Runtime1::generate_unwind_exception(StubAssembler *sasm) {
259   // Incoming parameters: Z_EXC_OOP and Z_EXC_PC.
260   // Keep copies in callee-saved registers during runtime call.
261   const Register exception_oop_callee_saved = Z_R11;
262   const Register exception_pc_callee_saved = Z_R12;
263   // Other registers used in this stub.
264   const Register handler_addr = Z_R4;
265 
266   if (AbortVMOnException) {
267     save_live_registers(sasm);
268     __ call_VM_leaf(CAST_FROM_FN_PTR(address, check_abort_on_vm_exception), Z_EXC_OOP);
269     restore_live_registers(sasm);
270   }
271 
272   // Verify that only exception_oop, is valid at this time.
273   __ invalidate_registers(Z_EXC_OOP, Z_EXC_PC);
274 
275   // Check that fields in JavaThread for exception oop and issuing pc are set.
276   __ asm_assert_mem8_is_zero(in_bytes(JavaThread::exception_oop_offset()), Z_thread, "exception oop already set : " FILE_AND_LINE, 0);
277   __ asm_assert_mem8_is_zero(in_bytes(JavaThread::exception_pc_offset()), Z_thread, "exception pc already set : " FILE_AND_LINE, 0);
278 
279   // Save exception_oop and pc in callee-saved register to preserve it
280   // during runtime calls.
281   __ verify_not_null_oop(Z_EXC_OOP);
282   __ lgr_if_needed(exception_oop_callee_saved, Z_EXC_OOP);
283   __ lgr_if_needed(exception_pc_callee_saved, Z_EXC_PC);
284 
285   __ push_frame_abi160(0); // Runtime code needs the z_abi_160.
286 
287   // Search the exception handler address of the caller (using the return address).
288   __ call_VM_leaf(CAST_FROM_FN_PTR(address, SharedRuntime::exception_handler_for_return_address), Z_thread, Z_EXC_PC);
289   // Z_RET(Z_R2): exception handler address of the caller.
290 
291   __ pop_frame();
292 
293   __ invalidate_registers(exception_oop_callee_saved, exception_pc_callee_saved, Z_RET);
294 
295   // Move result of call into correct register.
296   __ lgr_if_needed(handler_addr, Z_RET);
297 
298   // Restore exception oop and pc to Z_EXC_OOP and Z_EXC_PC (required convention of exception handler).
299   __ lgr_if_needed(Z_EXC_OOP, exception_oop_callee_saved);
300   __ lgr_if_needed(Z_EXC_PC, exception_pc_callee_saved);
301 
302   // Verify that there is really a valid exception in Z_EXC_OOP.
303   __ verify_not_null_oop(Z_EXC_OOP);
304 
305   __ z_br(handler_addr); // Jump to exception handler.
306 }
307 
308 OopMapSet* Runtime1::generate_patching(StubAssembler* sasm, address target) {
309   // Make a frame and preserve the caller's caller-save registers.
310   OopMap* oop_map = save_live_registers(sasm);
311 
312   // Call the runtime patching routine, returns non-zero if nmethod got deopted.
313   int call_offset = __ call_RT(noreg, noreg, target);
314   OopMapSet* oop_maps = new OopMapSet();
315   oop_maps->add_gc_map(call_offset, oop_map);
316 
317   // Re-execute the patched instruction or, if the nmethod was
318   // deoptmized, return to the deoptimization handler entry that will
319   // cause re-execution of the current bytecode.
320   DeoptimizationBlob* deopt_blob = SharedRuntime::deopt_blob();
321   assert(deopt_blob != nullptr, "deoptimization blob must have been created");
322 
323   __ z_ltr(Z_RET, Z_RET); // return value == 0
324 
325   restore_live_registers(sasm);
326 
327   __ z_bcr(Assembler::bcondZero, Z_R14);
328 
329   // Return to the deoptimization handler entry for unpacking and
330   // rexecute if we simply returned then we'd deopt as if any call we
331   // patched had just returned.
332   AddressLiteral dest(deopt_blob->unpack_with_reexecution());
333   __ load_const_optimized(Z_R1_scratch, dest);
334   __ z_br(Z_R1_scratch);
335 
336   return oop_maps;
337 }
338 
339 OopMapSet* Runtime1::generate_code_for(StubId id, StubAssembler* sasm) {
340 
341   // for better readability
342   const bool must_gc_arguments = true;
343   const bool dont_gc_arguments = false;
344 
345   // Default value; overwritten for some optimized stubs that are
346   // called from methods that do not use the fpu.
347   bool save_fpu_registers = true;
348 
349   // Stub code and info for the different stubs.
350   OopMapSet* oop_maps = nullptr;
351   switch (id) {
352     case StubId::c1_forward_exception_id:
353       {
354         oop_maps = generate_handle_exception(id, sasm);
355         // will not return
356       }
357       break;
358 
359     case StubId::c1_new_instance_id:
360     case StubId::c1_fast_new_instance_id:
361     case StubId::c1_fast_new_instance_init_check_id:
362       {
363         Register klass    = Z_R11; // Incoming
364         Register obj      = Z_R2;  // Result
365 
366         if (id == StubId::c1_new_instance_id) {
367           __ set_info("new_instance", dont_gc_arguments);
368         } else if (id == StubId::c1_fast_new_instance_id) {
369           __ set_info("fast new_instance", dont_gc_arguments);
370         } else {
371           assert(id == StubId::c1_fast_new_instance_init_check_id, "bad StubId");
372           __ set_info("fast new_instance init check", dont_gc_arguments);
373         }
374 
375         OopMap* map = save_live_registers_except_r2(sasm);
376         int call_offset = __ call_RT(obj, noreg, CAST_FROM_FN_PTR(address, new_instance), klass);
377         oop_maps = new OopMapSet();
378         oop_maps->add_gc_map(call_offset, map);
379         restore_live_registers_except_r2(sasm);
380 
381         __ verify_oop(obj, FILE_AND_LINE);
382         __ z_br(Z_R14);
383       }
384       break;
385 
386     case StubId::c1_counter_overflow_id:
387       {
388         // Arguments :
389         //   bci    : stack param 0
390         //   method : stack param 1
391         //
392         Register bci = Z_ARG2, method = Z_ARG3;
393         // frame size in bytes
394         OopMap* map = save_live_registers(sasm);
395         const int frame_size = sasm->frame_size() * VMRegImpl::slots_per_word * VMRegImpl::stack_slot_size;
396         __ z_lg(bci,    0*BytesPerWord + FrameMap::first_available_sp_in_frame + frame_size, Z_SP);
397         __ z_lg(method, 1*BytesPerWord + FrameMap::first_available_sp_in_frame + frame_size, Z_SP);
398         int call_offset = __ call_RT(noreg, noreg, CAST_FROM_FN_PTR(address, counter_overflow), bci, method);
399         oop_maps = new OopMapSet();
400         oop_maps->add_gc_map(call_offset, map);
401         restore_live_registers(sasm);
402         __ z_br(Z_R14);
403       }
404       break;
405     case StubId::c1_new_type_array_id:
406     case StubId::c1_new_object_array_id:
407       {
408         Register length   = Z_R13; // Incoming
409         Register klass    = Z_R11; // Incoming
410         Register obj      = Z_R2;  // Result
411 
412         if (id == StubId::c1_new_type_array_id) {
413           __ set_info("new_type_array", dont_gc_arguments);
414         } else {
415           __ set_info("new_object_array", dont_gc_arguments);
416         }
417 
418 #ifdef ASSERT
419         // Assert object type is really an array of the proper kind.
420         {
421           NearLabel ok;
422           Register t0 = obj;
423           __ mem2reg_opt(t0, Address(klass, Klass::layout_helper_offset()), false);
424           __ z_sra(t0, Klass::_lh_array_tag_shift);
425           int tag = ((id == StubId::c1_new_type_array_id)
426                      ? Klass::_lh_array_tag_type_value : Klass::_lh_array_tag_ref_value);
427           __ compare32_and_branch(t0, tag, Assembler::bcondEqual, ok);
428           __ stop("assert(is an array klass)");
429           __ should_not_reach_here();
430           __ bind(ok);
431         }
432 #endif // ASSERT
433 
434         OopMap* map = save_live_registers_except_r2(sasm);
435         int call_offset;
436         if (id == StubId::c1_new_type_array_id) {
437           call_offset = __ call_RT(obj, noreg, CAST_FROM_FN_PTR(address, new_type_array), klass, length);
438         } else {
439           call_offset = __ call_RT(obj, noreg, CAST_FROM_FN_PTR(address, new_object_array), klass, length);
440         }
441 
442         oop_maps = new OopMapSet();
443         oop_maps->add_gc_map(call_offset, map);
444         restore_live_registers_except_r2(sasm);
445 
446         __ verify_oop(obj, FILE_AND_LINE);
447         __ z_br(Z_R14);
448       }
449       break;
450 
451     case StubId::c1_new_multi_array_id:
452       { __ set_info("new_multi_array", dont_gc_arguments);
453         // Z_R3,: klass
454         // Z_R4,: rank
455         // Z_R5: address of 1st dimension
456         OopMap* map = save_live_registers(sasm);
457         int call_offset = __ call_RT(Z_R2, noreg, CAST_FROM_FN_PTR(address, new_multi_array), Z_R3, Z_R4, Z_R5);
458 
459         oop_maps = new OopMapSet();
460         oop_maps->add_gc_map(call_offset, map);
461         restore_live_registers_except_r2(sasm);
462 
463         // Z_R2,: new multi array
464         __ verify_oop(Z_R2, FILE_AND_LINE);
465         __ z_br(Z_R14);
466       }
467       break;
468 
469     case StubId::c1_register_finalizer_id:
470       {
471         __ set_info("register_finalizer", dont_gc_arguments);
472 
473         // Load the klass and check the has finalizer flag.
474         Register klass = Z_ARG2;
475         __ load_klass(klass, Z_ARG1);
476         __ z_tm(Address(klass, Klass::misc_flags_offset()), KlassFlags::_misc_has_finalizer);
477         __ z_bcr(Assembler::bcondAllZero, Z_R14); // Return if bit is not set.
478 
479         OopMap* oop_map = save_live_registers(sasm);
480         int call_offset = __ call_RT(noreg, noreg,
481                                      CAST_FROM_FN_PTR(address, SharedRuntime::register_finalizer), Z_ARG1);
482         oop_maps = new OopMapSet();
483         oop_maps->add_gc_map(call_offset, oop_map);
484 
485         // Now restore all the live registers.
486         restore_live_registers(sasm);
487 
488         __ z_br(Z_R14);
489       }
490       break;
491 
492     case StubId::c1_throw_range_check_failed_id:
493       { __ set_info("range_check_failed", dont_gc_arguments);
494         oop_maps = generate_exception_throw(sasm, CAST_FROM_FN_PTR(address, throw_range_check_exception), true);
495       }
496       break;
497 
498     case StubId::c1_throw_index_exception_id:
499       { __ set_info("index_range_check_failed", dont_gc_arguments);
500         oop_maps = generate_exception_throw(sasm, CAST_FROM_FN_PTR(address, throw_index_exception), true);
501       }
502       break;
503     case StubId::c1_throw_div0_exception_id:
504       { __ set_info("throw_div0_exception", dont_gc_arguments);
505         oop_maps = generate_exception_throw(sasm, CAST_FROM_FN_PTR(address, throw_div0_exception), false);
506       }
507       break;
508     case StubId::c1_throw_null_pointer_exception_id:
509       { __ set_info("throw_null_pointer_exception", dont_gc_arguments);
510         oop_maps = generate_exception_throw(sasm, CAST_FROM_FN_PTR(address, throw_null_pointer_exception), false);
511       }
512       break;
513     case StubId::c1_handle_exception_nofpu_id:
514     case StubId::c1_handle_exception_id:
515       { __ set_info("handle_exception", dont_gc_arguments);
516         oop_maps = generate_handle_exception(id, sasm);
517       }
518       break;
519     case StubId::c1_handle_exception_from_callee_id:
520       { __ set_info("handle_exception_from_callee", dont_gc_arguments);
521         oop_maps = generate_handle_exception(id, sasm);
522       }
523       break;
524     case StubId::c1_unwind_exception_id:
525       { __ set_info("unwind_exception", dont_gc_arguments);
526         // Note: no stubframe since we are about to leave the current
527         // activation and we are calling a leaf VM function only.
528         generate_unwind_exception(sasm);
529       }
530       break;
531     case StubId::c1_throw_array_store_exception_id:
532       { __ set_info("throw_array_store_exception", dont_gc_arguments);
533         oop_maps = generate_exception_throw(sasm, CAST_FROM_FN_PTR(address, throw_array_store_exception), true);
534       }
535       break;
536     case StubId::c1_throw_class_cast_exception_id:
537     { // Z_R1_scratch: object
538       __ set_info("throw_class_cast_exception", dont_gc_arguments);
539       oop_maps = generate_exception_throw(sasm, CAST_FROM_FN_PTR(address, throw_class_cast_exception), true);
540     }
541     break;
542     case StubId::c1_throw_incompatible_class_change_error_id:
543       { __ set_info("throw_incompatible_class_cast_exception", dont_gc_arguments);
544         oop_maps = generate_exception_throw(sasm, CAST_FROM_FN_PTR(address, throw_incompatible_class_change_error), false);
545       }
546       break;
547     case StubId::c1_slow_subtype_check_id:
548     {
549       // Arguments :
550       //   sub  : stack param 0
551       //   super: stack param 1
552       //   raddr: Z_R14, blown by call
553       //
554       // Result : condition code 0 for match (bcondEqual will be true),
555       //          condition code 2 for miss  (bcondNotEqual will be true)
556       NearLabel miss;
557       const Register Rsubklass   = Z_ARG2; // sub
558       const Register Rsuperklass = Z_ARG3; // super
559 
560       // No args, but tmp registers that are killed.
561       const Register Rlength     = Z_ARG4; // cache array length
562       const Register Rarray_ptr  = Z_ARG5; // Current value from cache array.
563 
564       if (UseCompressedOops) {
565         assert(Universe::heap() != nullptr, "java heap must be initialized to generate partial_subtype_check stub");
566       }
567 
568       const int frame_size = 4*BytesPerWord + frame::z_abi_160_size;
569       // Save return pc. This is not necessary, but could be helpful
570       // in the case of crashes.
571       __ save_return_pc();
572       __ push_frame(frame_size);
573       // Save registers before changing them.
574       int i = 0;
575       __ z_stg(Rsubklass,   (i++)*BytesPerWord + frame::z_abi_160_size, Z_SP);
576       __ z_stg(Rsuperklass, (i++)*BytesPerWord + frame::z_abi_160_size, Z_SP);
577       __ z_stg(Rlength,     (i++)*BytesPerWord + frame::z_abi_160_size, Z_SP);
578       __ z_stg(Rarray_ptr,  (i++)*BytesPerWord + frame::z_abi_160_size, Z_SP);
579       assert(i*BytesPerWord + frame::z_abi_160_size == frame_size, "check");
580 
581       // Get sub and super from stack.
582       __ z_lg(Rsubklass,   0*BytesPerWord + FrameMap::first_available_sp_in_frame + frame_size, Z_SP);
583       __ z_lg(Rsuperklass, 1*BytesPerWord + FrameMap::first_available_sp_in_frame + frame_size, Z_SP);
584 
585       __ check_klass_subtype_slow_path(Rsubklass,
586                                        Rsuperklass,
587                                        Rarray_ptr  /* temp_reg  */,
588                                        Rlength     /* temp2_reg */,
589                                        nullptr     /* L_success */,
590                                        &miss       /* L_failure */);
591 
592       // Match falls through here.
593       i = 0;
594       __ z_lg(Rsubklass,   (i++)*BytesPerWord + frame::z_abi_160_size, Z_SP);
595       __ z_lg(Rsuperklass, (i++)*BytesPerWord + frame::z_abi_160_size, Z_SP);
596       __ z_lg(Rlength,     (i++)*BytesPerWord + frame::z_abi_160_size, Z_SP);
597       __ z_lg(Rarray_ptr,  (i++)*BytesPerWord + frame::z_abi_160_size, Z_SP);
598       assert(i*BytesPerWord + frame::z_abi_160_size == frame_size, "check");
599       __ pop_frame();
600       // Return pc is still in R_14.
601       __ clear_reg(Z_R0_scratch);         // Zero indicates a match. Set CC 0 (bcondEqual will be true)
602       __ z_br(Z_R14);
603 
604       __ BIND(miss);
605       i = 0;
606       __ z_lg(Rsubklass,   (i++)*BytesPerWord + frame::z_abi_160_size, Z_SP);
607       __ z_lg(Rsuperklass, (i++)*BytesPerWord + frame::z_abi_160_size, Z_SP);
608       __ z_lg(Rlength,     (i++)*BytesPerWord + frame::z_abi_160_size, Z_SP);
609       __ z_lg(Rarray_ptr,  (i++)*BytesPerWord + frame::z_abi_160_size, Z_SP);
610       assert(i*BytesPerWord + frame::z_abi_160_size == frame_size, "check");
611       __ pop_frame();
612       // return pc is still in R_14
613       __ load_const_optimized(Z_R0_scratch, 1); // One indicates a miss.
614       __ z_ltgr(Z_R0_scratch, Z_R0_scratch);    // Set CC 2 (bcondNotEqual will be true).
615       __ z_br(Z_R14);
616     }
617     break;
618     case StubId::c1_is_instance_of_id:
619     {
620       // Mirror: Z_ARG1(R2)
621       // Object: Z_ARG2
622       // Temps: Z_ARG3, Z_ARG4, Z_ARG5, Z_R10, Z_R11
623       // Result: Z_RET(R2)
624 
625       // Get the Klass* into Z_ARG3
626       Register klass = Z_ARG3 , obj = Z_ARG2, result = Z_RET;
627       Register temp0 = Z_ARG4, temp1 = Z_ARG5, temp2 = Z_R10, temp3 = Z_R11;
628 
629       __ z_ltg(klass, Address(Z_ARG1, java_lang_Class::klass_offset())); // Klass is null
630 
631       Label is_secondary;
632 
633       __ clear_reg(result /* Z_R2 */, true /* whole_reg */, false /* set_cc */);  // sets result=0 (failure)
634 
635       __ z_bcr(Assembler::bcondEqual, Z_R14); // cc set by z_ltg above
636 
637       __ z_ltgr(obj, obj); // obj is null
638       __ z_bcr(Assembler::bcondEqual, Z_R14);
639 
640       __ z_llgf(temp0, Address(klass, in_bytes(Klass::super_check_offset_offset())));
641       __ compare32_and_branch(temp0, in_bytes(Klass::secondary_super_cache_offset()), Assembler::bcondEqual, is_secondary); // Klass is a secondary superclass
642 
643       // Klass is a concrete class
644       __ load_klass(temp1, obj);
645       __ z_cg(klass, Address(temp1, temp0));
646 
647       // result is already holding 0, denoting NotEqual case
648       __ load_on_condition_imm_32(result, 1, Assembler::bcondEqual);
649       __ z_br(Z_R14);
650 
651       __ bind(is_secondary);
652 
653       __ load_klass(obj, obj);
654 
655       // This is necessary because I am never in my own secondary_super list.
656       __ z_cgr(obj, klass);
657       __ load_on_condition_imm_32(result, 1, Assembler::bcondEqual);
658       __ z_bcr(Assembler::bcondEqual, Z_R14);
659 
660       // Z_R10 and Z_R11 are callee saved, so we must need to preserve them before any use
661       __ z_ldgr(Z_F1, Z_R10);
662       __ z_ldgr(Z_F3, Z_R11);
663 
664       __ lookup_secondary_supers_table_var(obj, klass,
665                                           /*temps*/ temp0, temp1, temp2, temp3,
666                                           result);
667 
668       // lookup_secondary_supers_table_var return 0 on success and 1 on failure.
669       // but this method returns 0 on failure and 1 on success.
670       // so we have to invert the result from lookup_secondary_supers_table_var.
671       __ z_xilf(result, 1);  // invert the result
672 
673       __ z_lgdr(Z_R10, Z_F1);
674       __ z_lgdr(Z_R11, Z_F3);
675 
676       __ z_br(Z_R14);
677 
678     }
679     case StubId::c1_monitorenter_nofpu_id:
680     case StubId::c1_monitorenter_id:
681       { // Z_R1_scratch : object
682         // Z_R13       : lock address (see LIRGenerator::syncTempOpr())
683         __ set_info("monitorenter", dont_gc_arguments);
684 
685         int save_fpu_registers = (id == StubId::c1_monitorenter_id);
686         // Make a frame and preserve the caller's caller-save registers.
687         OopMap* oop_map = save_live_registers(sasm, save_fpu_registers);
688 
689         int call_offset = __ call_RT(noreg, noreg, CAST_FROM_FN_PTR(address, monitorenter), Z_R1_scratch, Z_R13);
690 
691         oop_maps = new OopMapSet();
692         oop_maps->add_gc_map(call_offset, oop_map);
693         restore_live_registers(sasm, save_fpu_registers);
694 
695         __ z_br(Z_R14);
696       }
697       break;
698 
699     case StubId::c1_monitorexit_nofpu_id:
700     case StubId::c1_monitorexit_id:
701       { // Z_R1_scratch : lock address
702         // Note: really a leaf routine but must setup last java sp
703         //   => Use call_RT for now (speed can be improved by
704         //      doing last java sp setup manually).
705         __ set_info("monitorexit", dont_gc_arguments);
706 
707         int save_fpu_registers = (id == StubId::c1_monitorexit_id);
708         // Make a frame and preserve the caller's caller-save registers.
709         OopMap* oop_map = save_live_registers(sasm, save_fpu_registers);
710 
711         int call_offset = __ call_RT(noreg, noreg, CAST_FROM_FN_PTR(address, monitorexit), Z_R1_scratch);
712 
713         oop_maps = new OopMapSet();
714         oop_maps->add_gc_map(call_offset, oop_map);
715         restore_live_registers(sasm, save_fpu_registers);
716 
717         __ z_br(Z_R14);
718       }
719       break;
720 
721     case StubId::c1_deoptimize_id:
722       { // Args: Z_R1_scratch: trap request
723         __ set_info("deoptimize", dont_gc_arguments);
724         Register trap_request = Z_R1_scratch;
725         OopMap* oop_map = save_live_registers(sasm);
726         int call_offset = __ call_RT(noreg, noreg, CAST_FROM_FN_PTR(address, deoptimize), trap_request);
727         oop_maps = new OopMapSet();
728         oop_maps->add_gc_map(call_offset, oop_map);
729         restore_live_registers(sasm);
730         DeoptimizationBlob* deopt_blob = SharedRuntime::deopt_blob();
731         assert(deopt_blob != nullptr, "deoptimization blob must have been created");
732         AddressLiteral dest(deopt_blob->unpack_with_reexecution());
733         __ load_const_optimized(Z_R1_scratch, dest);
734         __ z_br(Z_R1_scratch);
735       }
736       break;
737 
738     case StubId::c1_access_field_patching_id:
739       { __ set_info("access_field_patching", dont_gc_arguments);
740         oop_maps = generate_patching(sasm, CAST_FROM_FN_PTR(address, access_field_patching));
741       }
742       break;
743 
744     case StubId::c1_load_klass_patching_id:
745       { __ set_info("load_klass_patching", dont_gc_arguments);
746         // We should set up register map.
747         oop_maps = generate_patching(sasm, CAST_FROM_FN_PTR(address, move_klass_patching));
748       }
749       break;
750 
751     case StubId::c1_load_mirror_patching_id:
752       { __ set_info("load_mirror_patching", dont_gc_arguments);
753         oop_maps = generate_patching(sasm, CAST_FROM_FN_PTR(address, move_mirror_patching));
754       }
755       break;
756 
757     case StubId::c1_load_appendix_patching_id:
758       { __ set_info("load_appendix_patching", dont_gc_arguments);
759         oop_maps = generate_patching(sasm, CAST_FROM_FN_PTR(address, move_appendix_patching));
760       }
761       break;
762 #if 0
763     case StubId::c1_dtrace_object_alloc_id:
764       { // rax,: object
765         StubFrame f(sasm, "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, 1);
769 
770         __ NOT_LP64(push(rax)) LP64_ONLY(mov(c_rarg0, rax));
771         __ call(RuntimeAddress(CAST_FROM_FN_PTR(address, static_cast<int (*)(oopDesc*)>(SharedRuntime::dtrace_object_alloc))));
772         NOT_LP64(__ pop(rax));
773 
774         restore_live_registers(sasm);
775       }
776       break;
777 
778     case StubId::c1_fpu2long_stub_id:
779       {
780         // rax, and rdx are destroyed, but should be free since the result is returned there
781         // preserve rsi,ecx
782         __ push(rsi);
783         __ push(rcx);
784         LP64_ONLY(__ push(rdx);)
785 
786         // check for NaN
787         Label return0, do_return, return_min_jlong, do_convert;
788 
789         Address value_high_word(rsp, wordSize + 4);
790         Address value_low_word(rsp, wordSize);
791         Address result_high_word(rsp, 3*wordSize + 4);
792         Address result_low_word(rsp, 3*wordSize);
793 
794         __ subptr(rsp, 32);                    // more than enough on 32bit
795         __ fst_d(value_low_word);
796         __ movl(rax, value_high_word);
797         __ andl(rax, 0x7ff00000);
798         __ cmpl(rax, 0x7ff00000);
799         __ jcc(Assembler::notEqual, do_convert);
800         __ movl(rax, value_high_word);
801         __ andl(rax, 0xfffff);
802         __ orl(rax, value_low_word);
803         __ jcc(Assembler::notZero, return0);
804 
805         __ bind(do_convert);
806         __ fnstcw(Address(rsp, 0));
807         __ movzwl(rax, Address(rsp, 0));
808         __ orl(rax, 0xc00);
809         __ movw(Address(rsp, 2), rax);
810         __ fldcw(Address(rsp, 2));
811         __ fwait();
812         __ fistp_d(result_low_word);
813         __ fldcw(Address(rsp, 0));
814         __ fwait();
815         // This gets the entire long in rax on 64bit
816         __ movptr(rax, result_low_word);
817         // testing of high bits
818         __ movl(rdx, result_high_word);
819         __ mov(rcx, rax);
820         // What the heck is the point of the next instruction???
821         __ xorl(rcx, 0x0);
822         __ movl(rsi, 0x80000000);
823         __ xorl(rsi, rdx);
824         __ orl(rcx, rsi);
825         __ jcc(Assembler::notEqual, do_return);
826         __ fldz();
827         __ fcomp_d(value_low_word);
828         __ fnstsw_ax();
829         __ testl(rax, 0x4100);  // ZF & CF == 0
830         __ jcc(Assembler::equal, return_min_jlong);
831         // return max_jlong
832         __ mov64(rax, CONST64(0x7fffffffffffffff));
833         __ jmp(do_return);
834 
835         __ bind(return_min_jlong);
836         __ mov64(rax, UCONST64(0x8000000000000000));
837         __ jmp(do_return);
838 
839         __ bind(return0);
840         __ fpop();
841         __ xorptr(rax, rax);
842 
843         __ bind(do_return);
844         __ addptr(rsp, 32);
845         LP64_ONLY(__ pop(rdx);)
846         __ pop(rcx);
847         __ pop(rsi);
848         __ ret(0);
849       }
850       break;
851 #endif // TODO
852 
853     case StubId::c1_predicate_failed_trap_id:
854       {
855         __ set_info("predicate_failed_trap", dont_gc_arguments);
856 
857         OopMap* map = save_live_registers(sasm);
858 
859         int call_offset = __ call_RT(noreg, noreg, CAST_FROM_FN_PTR(address, predicate_failed_trap));
860         oop_maps = new OopMapSet();
861         oop_maps->add_gc_map(call_offset, map);
862         restore_live_registers(sasm);
863 
864         DeoptimizationBlob* deopt_blob = SharedRuntime::deopt_blob();
865         assert(deopt_blob != nullptr, "deoptimization blob must have been created");
866 
867         __ load_const_optimized(Z_R1_scratch, deopt_blob->unpack_with_reexecution());
868         __ z_br(Z_R1_scratch);
869       }
870       break;
871 
872     default:
873       {
874         __ should_not_reach_here(FILE_AND_LINE, (int)id);
875       }
876       break;
877   }
878   return oop_maps;
879 }
880 
881 OopMapSet* Runtime1::generate_handle_exception(StubId id, StubAssembler *sasm) {
882   __ block_comment("generate_handle_exception");
883 
884   // incoming parameters: Z_EXC_OOP, Z_EXC_PC
885 
886   // Save registers if required.
887   OopMapSet* oop_maps = new OopMapSet();
888   OopMap* oop_map = nullptr;
889   Register reg_fp = Z_R1_scratch;
890 
891   switch (id) {
892     case StubId::c1_forward_exception_id: {
893       // We're handling an exception in the context of a compiled frame.
894       // The registers have been saved in the standard places. Perform
895       // an exception lookup in the caller and dispatch to the handler
896       // if found. Otherwise unwind and dispatch to the callers
897       // exception handler.
898       oop_map = generate_oop_map(sasm);
899 
900       // Load and clear pending exception oop into.
901       __ z_lg(Z_EXC_OOP, Address(Z_thread, Thread::pending_exception_offset()));
902       __ clear_mem(Address(Z_thread, Thread::pending_exception_offset()), 8);
903 
904       // Different stubs forward their exceptions; they should all have similar frame layouts
905       // (a) to find their return address (b) for a correct oop_map generated above.
906       assert(RegisterSaver::live_reg_frame_size(RegisterSaver::all_registers) ==
907              RegisterSaver::live_reg_frame_size(RegisterSaver::all_registers_except_r2), "requirement");
908 
909       // Load issuing PC (the return address for this stub).
910       const int frame_size_in_bytes = sasm->frame_size() * VMRegImpl::slots_per_word * VMRegImpl::stack_slot_size;
911       __ z_lg(Z_EXC_PC, Address(Z_SP, frame_size_in_bytes + _z_common_abi(return_pc)));
912       DEBUG_ONLY(__ z_lay(reg_fp, Address(Z_SP, frame_size_in_bytes));)
913 
914       // Make sure that the vm_results are cleared (may be unnecessary).
915       __ clear_mem(Address(Z_thread, JavaThread::vm_result_oop_offset()),   sizeof(oop));
916       __ clear_mem(Address(Z_thread, JavaThread::vm_result_metadata_offset()), sizeof(Metadata*));
917       break;
918     }
919     case StubId::c1_handle_exception_nofpu_id:
920     case StubId::c1_handle_exception_id:
921       // At this point all registers MAY be live.
922       DEBUG_ONLY(__ z_lgr(reg_fp, Z_SP);)
923       oop_map = save_live_registers(sasm, id != StubId::c1_handle_exception_nofpu_id, Z_EXC_PC);
924       break;
925     case StubId::c1_handle_exception_from_callee_id: {
926       // At this point all registers except Z_EXC_OOP and Z_EXC_PC are dead.
927       DEBUG_ONLY(__ z_lgr(reg_fp, Z_SP);)
928       __ save_return_pc(Z_EXC_PC);
929       const int frame_size_in_bytes = __ push_frame_abi160(0);
930       oop_map = new OopMap(frame_size_in_bytes / VMRegImpl::stack_slot_size, 0);
931       sasm->set_frame_size(frame_size_in_bytes / BytesPerWord);
932       break;
933     }
934     default:  ShouldNotReachHere();
935   }
936 
937   // Verify that only Z_EXC_OOP, and Z_EXC_PC are valid at this time.
938   __ invalidate_registers(Z_EXC_OOP, Z_EXC_PC, reg_fp);
939   // Verify that Z_EXC_OOP, contains a valid exception.
940   __ verify_not_null_oop(Z_EXC_OOP);
941 
942   // Check that fields in JavaThread for exception oop and issuing pc
943   // are empty before writing to them.
944   __ asm_assert_mem8_is_zero(in_bytes(JavaThread::exception_oop_offset()), Z_thread, "exception oop already set : " FILE_AND_LINE, 0);
945   __ asm_assert_mem8_is_zero(in_bytes(JavaThread::exception_pc_offset()), Z_thread, "exception pc already set : " FILE_AND_LINE, 0);
946 
947   // Save exception oop and issuing pc into JavaThread.
948   // (Exception handler will load it from here.)
949   __ z_stg(Z_EXC_OOP, Address(Z_thread, JavaThread::exception_oop_offset()));
950   __ z_stg(Z_EXC_PC, Address(Z_thread, JavaThread::exception_pc_offset()));
951 
952 #ifdef ASSERT
953   { NearLabel ok;
954     __ z_cg(Z_EXC_PC, Address(reg_fp, _z_common_abi(return_pc)));
955     __ branch_optimized(Assembler::bcondEqual, ok);
956     __ stop("use throwing pc as return address (has bci & oop map)");
957     __ bind(ok);
958   }
959 #endif
960 
961   // Compute the exception handler.
962   // The exception oop and the throwing pc are read from the fields in JavaThread.
963   int call_offset = __ call_RT(noreg, noreg, CAST_FROM_FN_PTR(address, exception_handler_for_pc));
964   oop_maps->add_gc_map(call_offset, oop_map);
965 
966   // Z_RET(Z_R2): handler address
967   //   will be the deopt blob if nmethod was deoptimized while we looked up
968   //   handler regardless of whether handler existed in the nmethod.
969 
970   // Only Z_R2, is valid at this time, all other registers have been destroyed by the runtime call.
971   __ invalidate_registers(Z_R2);
972 
973   switch(id) {
974     case StubId::c1_forward_exception_id:
975     case StubId::c1_handle_exception_nofpu_id:
976     case StubId::c1_handle_exception_id:
977       // Restore the registers that were saved at the beginning.
978       __ z_lgr(Z_R1_scratch, Z_R2);   // Restoring live registers kills Z_R2.
979       restore_live_registers(sasm, id != StubId::c1_handle_exception_nofpu_id);  // Pops as well the frame.
980       __ z_br(Z_R1_scratch);
981       break;
982     case StubId::c1_handle_exception_from_callee_id: {
983       __ pop_frame();
984       __ z_br(Z_R2); // Jump to exception handler.
985     }
986     break;
987     default:  ShouldNotReachHere();
988   }
989 
990   return oop_maps;
991 }
992 
993 
994 #undef __
995 
996 const char *Runtime1::pd_name_for_address(address entry) {
997   return "<unknown function>";
998 }