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
427 : Klass::_lh_array_tag_obj_value);
428 __ compare32_and_branch(t0, tag, Assembler::bcondEqual, ok);
429 __ stop("assert(is an array klass)");
430 __ should_not_reach_here();
431 __ bind(ok);
432 }
433 #endif // ASSERT
434
435 OopMap* map = save_live_registers_except_r2(sasm);
436 int call_offset;
437 if (id == StubId::c1_new_type_array_id) {
438 call_offset = __ call_RT(obj, noreg, CAST_FROM_FN_PTR(address, new_type_array), klass, length);
439 } else {
440 call_offset = __ call_RT(obj, noreg, CAST_FROM_FN_PTR(address, new_object_array), klass, length);
441 }
442
443 oop_maps = new OopMapSet();
444 oop_maps->add_gc_map(call_offset, map);
445 restore_live_registers_except_r2(sasm);
446
447 __ verify_oop(obj, FILE_AND_LINE);
448 __ z_br(Z_R14);
449 }
450 break;
451
452 case StubId::c1_new_multi_array_id:
453 { __ set_info("new_multi_array", dont_gc_arguments);
454 // Z_R3,: klass
455 // Z_R4,: rank
456 // Z_R5: address of 1st dimension
457 OopMap* map = save_live_registers(sasm);
458 int call_offset = __ call_RT(Z_R2, noreg, CAST_FROM_FN_PTR(address, new_multi_array), Z_R3, Z_R4, Z_R5);
459
460 oop_maps = new OopMapSet();
461 oop_maps->add_gc_map(call_offset, map);
462 restore_live_registers_except_r2(sasm);
463
464 // Z_R2,: new multi array
465 __ verify_oop(Z_R2, FILE_AND_LINE);
466 __ z_br(Z_R14);
467 }
468 break;
469
470 case StubId::c1_register_finalizer_id:
471 {
472 __ set_info("register_finalizer", dont_gc_arguments);
473
474 // Load the klass and check the has finalizer flag.
475 Register klass = Z_ARG2;
476 __ load_klass(klass, Z_ARG1);
477 __ z_tm(Address(klass, Klass::misc_flags_offset()), KlassFlags::_misc_has_finalizer);
478 __ z_bcr(Assembler::bcondAllZero, Z_R14); // Return if bit is not set.
479
480 OopMap* oop_map = save_live_registers(sasm);
481 int call_offset = __ call_RT(noreg, noreg,
482 CAST_FROM_FN_PTR(address, SharedRuntime::register_finalizer), Z_ARG1);
483 oop_maps = new OopMapSet();
484 oop_maps->add_gc_map(call_offset, oop_map);
485
486 // Now restore all the live registers.
487 restore_live_registers(sasm);
488
489 __ z_br(Z_R14);
490 }
491 break;
492
493 case StubId::c1_throw_range_check_failed_id:
494 { __ set_info("range_check_failed", dont_gc_arguments);
495 oop_maps = generate_exception_throw(sasm, CAST_FROM_FN_PTR(address, throw_range_check_exception), true);
496 }
497 break;
498
499 case StubId::c1_throw_index_exception_id:
500 { __ set_info("index_range_check_failed", dont_gc_arguments);
501 oop_maps = generate_exception_throw(sasm, CAST_FROM_FN_PTR(address, throw_index_exception), true);
502 }
503 break;
504 case StubId::c1_throw_div0_exception_id:
505 { __ set_info("throw_div0_exception", dont_gc_arguments);
506 oop_maps = generate_exception_throw(sasm, CAST_FROM_FN_PTR(address, throw_div0_exception), false);
507 }
508 break;
509 case StubId::c1_throw_null_pointer_exception_id:
510 { __ set_info("throw_null_pointer_exception", dont_gc_arguments);
511 oop_maps = generate_exception_throw(sasm, CAST_FROM_FN_PTR(address, throw_null_pointer_exception), false);
512 }
513 break;
514 case StubId::c1_handle_exception_nofpu_id:
515 case StubId::c1_handle_exception_id:
516 { __ set_info("handle_exception", dont_gc_arguments);
517 oop_maps = generate_handle_exception(id, sasm);
518 }
519 break;
520 case StubId::c1_handle_exception_from_callee_id:
521 { __ set_info("handle_exception_from_callee", dont_gc_arguments);
522 oop_maps = generate_handle_exception(id, sasm);
523 }
524 break;
525 case StubId::c1_unwind_exception_id:
526 { __ set_info("unwind_exception", dont_gc_arguments);
527 // Note: no stubframe since we are about to leave the current
528 // activation and we are calling a leaf VM function only.
529 generate_unwind_exception(sasm);
530 }
531 break;
532 case StubId::c1_throw_array_store_exception_id:
533 { __ set_info("throw_array_store_exception", dont_gc_arguments);
534 oop_maps = generate_exception_throw(sasm, CAST_FROM_FN_PTR(address, throw_array_store_exception), true);
535 }
536 break;
537 case StubId::c1_throw_class_cast_exception_id:
538 { // Z_R1_scratch: object
539 __ set_info("throw_class_cast_exception", dont_gc_arguments);
540 oop_maps = generate_exception_throw(sasm, CAST_FROM_FN_PTR(address, throw_class_cast_exception), true);
541 }
542 break;
543 case StubId::c1_throw_incompatible_class_change_error_id:
544 { __ set_info("throw_incompatible_class_cast_exception", dont_gc_arguments);
545 oop_maps = generate_exception_throw(sasm, CAST_FROM_FN_PTR(address, throw_incompatible_class_change_error), false);
546 }
547 break;
548 case StubId::c1_slow_subtype_check_id:
549 {
550 // Arguments :
551 // sub : stack param 0
552 // super: stack param 1
553 // raddr: Z_R14, blown by call
554 //
555 // Result : condition code 0 for match (bcondEqual will be true),
556 // condition code 2 for miss (bcondNotEqual will be true)
557 NearLabel miss;
558 const Register Rsubklass = Z_ARG2; // sub
559 const Register Rsuperklass = Z_ARG3; // super
560
561 // No args, but tmp registers that are killed.
562 const Register Rlength = Z_ARG4; // cache array length
563 const Register Rarray_ptr = Z_ARG5; // Current value from cache array.
564
565 if (UseCompressedOops) {
566 assert(Universe::heap() != nullptr, "java heap must be initialized to generate partial_subtype_check stub");
567 }
568
569 const int frame_size = 4*BytesPerWord + frame::z_abi_160_size;
570 // Save return pc. This is not necessary, but could be helpful
571 // in the case of crashes.
572 __ save_return_pc();
573 __ push_frame(frame_size);
574 // Save registers before changing them.
575 int i = 0;
576 __ z_stg(Rsubklass, (i++)*BytesPerWord + frame::z_abi_160_size, Z_SP);
577 __ z_stg(Rsuperklass, (i++)*BytesPerWord + frame::z_abi_160_size, Z_SP);
578 __ z_stg(Rlength, (i++)*BytesPerWord + frame::z_abi_160_size, Z_SP);
579 __ z_stg(Rarray_ptr, (i++)*BytesPerWord + frame::z_abi_160_size, Z_SP);
580 assert(i*BytesPerWord + frame::z_abi_160_size == frame_size, "check");
581
582 // Get sub and super from stack.
583 __ z_lg(Rsubklass, 0*BytesPerWord + FrameMap::first_available_sp_in_frame + frame_size, Z_SP);
584 __ z_lg(Rsuperklass, 1*BytesPerWord + FrameMap::first_available_sp_in_frame + frame_size, Z_SP);
585
586 __ check_klass_subtype_slow_path(Rsubklass,
587 Rsuperklass,
588 Rarray_ptr /* temp_reg */,
589 Rlength /* temp2_reg */,
590 nullptr /* L_success */,
591 &miss /* L_failure */);
592
593 // Match falls through here.
594 i = 0;
595 __ z_lg(Rsubklass, (i++)*BytesPerWord + frame::z_abi_160_size, Z_SP);
596 __ z_lg(Rsuperklass, (i++)*BytesPerWord + frame::z_abi_160_size, Z_SP);
597 __ z_lg(Rlength, (i++)*BytesPerWord + frame::z_abi_160_size, Z_SP);
598 __ z_lg(Rarray_ptr, (i++)*BytesPerWord + frame::z_abi_160_size, Z_SP);
599 assert(i*BytesPerWord + frame::z_abi_160_size == frame_size, "check");
600 __ pop_frame();
601 // Return pc is still in R_14.
602 __ clear_reg(Z_R0_scratch); // Zero indicates a match. Set CC 0 (bcondEqual will be true)
603 __ z_br(Z_R14);
604
605 __ BIND(miss);
606 i = 0;
607 __ z_lg(Rsubklass, (i++)*BytesPerWord + frame::z_abi_160_size, Z_SP);
608 __ z_lg(Rsuperklass, (i++)*BytesPerWord + frame::z_abi_160_size, Z_SP);
609 __ z_lg(Rlength, (i++)*BytesPerWord + frame::z_abi_160_size, Z_SP);
610 __ z_lg(Rarray_ptr, (i++)*BytesPerWord + frame::z_abi_160_size, Z_SP);
611 assert(i*BytesPerWord + frame::z_abi_160_size == frame_size, "check");
612 __ pop_frame();
613 // return pc is still in R_14
614 __ load_const_optimized(Z_R0_scratch, 1); // One indicates a miss.
615 __ z_ltgr(Z_R0_scratch, Z_R0_scratch); // Set CC 2 (bcondNotEqual will be true).
616 __ z_br(Z_R14);
617 }
618 break;
619 case StubId::c1_is_instance_of_id:
620 {
621 // Mirror: Z_ARG1(R2)
622 // Object: Z_ARG2
623 // Temps: Z_ARG3, Z_ARG4, Z_ARG5, Z_R10, Z_R11
624 // Result: Z_RET(R2)
625
626 // Get the Klass* into Z_ARG3
627 Register klass = Z_ARG3 , obj = Z_ARG2, result = Z_RET;
628 Register temp0 = Z_ARG4, temp1 = Z_ARG5, temp2 = Z_R10, temp3 = Z_R11;
629
630 __ z_ltg(klass, Address(Z_ARG1, java_lang_Class::klass_offset())); // Klass is null
631
632 Label is_secondary;
633
634 __ clear_reg(result /* Z_R2 */, true /* whole_reg */, false /* set_cc */); // sets result=0 (failure)
635
636 __ z_bcr(Assembler::bcondEqual, Z_R14); // cc set by z_ltg above
637
638 __ z_ltgr(obj, obj); // obj is null
639 __ z_bcr(Assembler::bcondEqual, Z_R14);
640
641 __ z_llgf(temp0, Address(klass, in_bytes(Klass::super_check_offset_offset())));
642 __ compare32_and_branch(temp0, in_bytes(Klass::secondary_super_cache_offset()), Assembler::bcondEqual, is_secondary); // Klass is a secondary superclass
643
644 // Klass is a concrete class
645 __ load_klass(temp1, obj);
646 __ z_cg(klass, Address(temp1, temp0));
647
648 // result is already holding 0, denoting NotEqual case
649 __ load_on_condition_imm_32(result, 1, Assembler::bcondEqual);
650 __ z_br(Z_R14);
651
652 __ bind(is_secondary);
653
654 __ load_klass(obj, obj);
655
656 // This is necessary because I am never in my own secondary_super list.
657 __ z_cgr(obj, klass);
658 __ load_on_condition_imm_32(result, 1, Assembler::bcondEqual);
659 __ z_bcr(Assembler::bcondEqual, Z_R14);
660
661 // Z_R10 and Z_R11 are callee saved, so we must need to preserve them before any use
662 __ z_ldgr(Z_F1, Z_R10);
663 __ z_ldgr(Z_F3, Z_R11);
664
665 __ lookup_secondary_supers_table_var(obj, klass,
666 /*temps*/ temp0, temp1, temp2, temp3,
667 result);
668
669 // lookup_secondary_supers_table_var return 0 on success and 1 on failure.
670 // but this method returns 0 on failure and 1 on success.
671 // so we have to invert the result from lookup_secondary_supers_table_var.
672 __ z_xilf(result, 1); // invert the result
673
674 __ z_lgdr(Z_R10, Z_F1);
675 __ z_lgdr(Z_R11, Z_F3);
676
677 __ z_br(Z_R14);
678
679 }
680 case StubId::c1_monitorenter_nofpu_id:
681 case StubId::c1_monitorenter_id:
682 { // Z_R1_scratch : object
683 // Z_R13 : lock address (see LIRGenerator::syncTempOpr())
684 __ set_info("monitorenter", dont_gc_arguments);
685
686 int save_fpu_registers = (id == StubId::c1_monitorenter_id);
687 // Make a frame and preserve the caller's caller-save registers.
688 OopMap* oop_map = save_live_registers(sasm, save_fpu_registers);
689
690 int call_offset = __ call_RT(noreg, noreg, CAST_FROM_FN_PTR(address, monitorenter), Z_R1_scratch, Z_R13);
691
692 oop_maps = new OopMapSet();
693 oop_maps->add_gc_map(call_offset, oop_map);
694 restore_live_registers(sasm, save_fpu_registers);
695
696 __ z_br(Z_R14);
697 }
698 break;
699
700 case StubId::c1_monitorexit_nofpu_id:
701 case StubId::c1_monitorexit_id:
702 { // Z_R1_scratch : lock address
703 // Note: really a leaf routine but must setup last java sp
704 // => Use call_RT for now (speed can be improved by
705 // doing last java sp setup manually).
706 __ set_info("monitorexit", dont_gc_arguments);
707
708 int save_fpu_registers = (id == StubId::c1_monitorexit_id);
709 // Make a frame and preserve the caller's caller-save registers.
710 OopMap* oop_map = save_live_registers(sasm, save_fpu_registers);
711
712 int call_offset = __ call_RT(noreg, noreg, CAST_FROM_FN_PTR(address, monitorexit), Z_R1_scratch);
713
714 oop_maps = new OopMapSet();
715 oop_maps->add_gc_map(call_offset, oop_map);
716 restore_live_registers(sasm, save_fpu_registers);
717
718 __ z_br(Z_R14);
719 }
720 break;
721
722 case StubId::c1_deoptimize_id:
723 { // Args: Z_R1_scratch: trap request
724 __ set_info("deoptimize", dont_gc_arguments);
725 Register trap_request = Z_R1_scratch;
726 OopMap* oop_map = save_live_registers(sasm);
727 int call_offset = __ call_RT(noreg, noreg, CAST_FROM_FN_PTR(address, deoptimize), trap_request);
728 oop_maps = new OopMapSet();
729 oop_maps->add_gc_map(call_offset, oop_map);
730 restore_live_registers(sasm);
731 DeoptimizationBlob* deopt_blob = SharedRuntime::deopt_blob();
732 assert(deopt_blob != nullptr, "deoptimization blob must have been created");
733 AddressLiteral dest(deopt_blob->unpack_with_reexecution());
734 __ load_const_optimized(Z_R1_scratch, dest);
735 __ z_br(Z_R1_scratch);
736 }
737 break;
738
739 case StubId::c1_access_field_patching_id:
740 { __ set_info("access_field_patching", dont_gc_arguments);
741 oop_maps = generate_patching(sasm, CAST_FROM_FN_PTR(address, access_field_patching));
742 }
743 break;
744
745 case StubId::c1_load_klass_patching_id:
746 { __ set_info("load_klass_patching", dont_gc_arguments);
747 // We should set up register map.
748 oop_maps = generate_patching(sasm, CAST_FROM_FN_PTR(address, move_klass_patching));
749 }
750 break;
751
752 case StubId::c1_load_mirror_patching_id:
753 { __ set_info("load_mirror_patching", dont_gc_arguments);
754 oop_maps = generate_patching(sasm, CAST_FROM_FN_PTR(address, move_mirror_patching));
755 }
756 break;
757
758 case StubId::c1_load_appendix_patching_id:
759 { __ set_info("load_appendix_patching", dont_gc_arguments);
760 oop_maps = generate_patching(sasm, CAST_FROM_FN_PTR(address, move_appendix_patching));
761 }
762 break;
763 #if 0
764 case StubId::c1_dtrace_object_alloc_id:
765 { // rax,: object
766 StubFrame f(sasm, "dtrace_object_alloc", dont_gc_arguments);
767 // We can't gc here so skip the oopmap but make sure that all
768 // the live registers get saved.
769 save_live_registers(sasm, 1);
770
771 __ NOT_LP64(push(rax)) LP64_ONLY(mov(c_rarg0, rax));
772 __ call(RuntimeAddress(CAST_FROM_FN_PTR(address, static_cast<int (*)(oopDesc*)>(SharedRuntime::dtrace_object_alloc))));
773 NOT_LP64(__ pop(rax));
774
775 restore_live_registers(sasm);
776 }
777 break;
778
779 case StubId::c1_fpu2long_stub_id:
780 {
781 // rax, and rdx are destroyed, but should be free since the result is returned there
782 // preserve rsi,ecx
783 __ push(rsi);
784 __ push(rcx);
785 LP64_ONLY(__ push(rdx);)
786
787 // check for NaN
788 Label return0, do_return, return_min_jlong, do_convert;
789
790 Address value_high_word(rsp, wordSize + 4);
791 Address value_low_word(rsp, wordSize);
792 Address result_high_word(rsp, 3*wordSize + 4);
793 Address result_low_word(rsp, 3*wordSize);
794
795 __ subptr(rsp, 32); // more than enough on 32bit
796 __ fst_d(value_low_word);
797 __ movl(rax, value_high_word);
798 __ andl(rax, 0x7ff00000);
799 __ cmpl(rax, 0x7ff00000);
800 __ jcc(Assembler::notEqual, do_convert);
801 __ movl(rax, value_high_word);
802 __ andl(rax, 0xfffff);
803 __ orl(rax, value_low_word);
804 __ jcc(Assembler::notZero, return0);
805
806 __ bind(do_convert);
807 __ fnstcw(Address(rsp, 0));
808 __ movzwl(rax, Address(rsp, 0));
809 __ orl(rax, 0xc00);
810 __ movw(Address(rsp, 2), rax);
811 __ fldcw(Address(rsp, 2));
812 __ fwait();
813 __ fistp_d(result_low_word);
814 __ fldcw(Address(rsp, 0));
815 __ fwait();
816 // This gets the entire long in rax on 64bit
817 __ movptr(rax, result_low_word);
818 // testing of high bits
819 __ movl(rdx, result_high_word);
820 __ mov(rcx, rax);
821 // What the heck is the point of the next instruction???
822 __ xorl(rcx, 0x0);
823 __ movl(rsi, 0x80000000);
824 __ xorl(rsi, rdx);
825 __ orl(rcx, rsi);
826 __ jcc(Assembler::notEqual, do_return);
827 __ fldz();
828 __ fcomp_d(value_low_word);
829 __ fnstsw_ax();
830 __ testl(rax, 0x4100); // ZF & CF == 0
831 __ jcc(Assembler::equal, return_min_jlong);
832 // return max_jlong
833 __ mov64(rax, CONST64(0x7fffffffffffffff));
834 __ jmp(do_return);
835
836 __ bind(return_min_jlong);
837 __ mov64(rax, UCONST64(0x8000000000000000));
838 __ jmp(do_return);
839
840 __ bind(return0);
841 __ fpop();
842 __ xorptr(rax, rax);
843
844 __ bind(do_return);
845 __ addptr(rsp, 32);
846 LP64_ONLY(__ pop(rdx);)
847 __ pop(rcx);
848 __ pop(rsi);
849 __ ret(0);
850 }
851 break;
852 #endif // TODO
853
854 case StubId::c1_predicate_failed_trap_id:
855 {
856 __ set_info("predicate_failed_trap", dont_gc_arguments);
857
858 OopMap* map = save_live_registers(sasm);
859
860 int call_offset = __ call_RT(noreg, noreg, CAST_FROM_FN_PTR(address, predicate_failed_trap));
861 oop_maps = new OopMapSet();
862 oop_maps->add_gc_map(call_offset, map);
863 restore_live_registers(sasm);
864
865 DeoptimizationBlob* deopt_blob = SharedRuntime::deopt_blob();
866 assert(deopt_blob != nullptr, "deoptimization blob must have been created");
867
868 __ load_const_optimized(Z_R1_scratch, deopt_blob->unpack_with_reexecution());
869 __ z_br(Z_R1_scratch);
870 }
871 break;
872
873 default:
874 {
875 __ should_not_reach_here(FILE_AND_LINE, (int)id);
876 }
877 break;
878 }
879 return oop_maps;
880 }
881
882 OopMapSet* Runtime1::generate_handle_exception(StubId id, StubAssembler *sasm) {
883 __ block_comment("generate_handle_exception");
884
885 // incoming parameters: Z_EXC_OOP, Z_EXC_PC
886
887 // Save registers if required.
888 OopMapSet* oop_maps = new OopMapSet();
889 OopMap* oop_map = nullptr;
890 Register reg_fp = Z_R1_scratch;
891
892 switch (id) {
893 case StubId::c1_forward_exception_id: {
894 // We're handling an exception in the context of a compiled frame.
895 // The registers have been saved in the standard places. Perform
896 // an exception lookup in the caller and dispatch to the handler
897 // if found. Otherwise unwind and dispatch to the callers
898 // exception handler.
899 oop_map = generate_oop_map(sasm);
900
901 // Load and clear pending exception oop into.
902 __ z_lg(Z_EXC_OOP, Address(Z_thread, Thread::pending_exception_offset()));
903 __ clear_mem(Address(Z_thread, Thread::pending_exception_offset()), 8);
904
905 // Different stubs forward their exceptions; they should all have similar frame layouts
906 // (a) to find their return address (b) for a correct oop_map generated above.
907 assert(RegisterSaver::live_reg_frame_size(RegisterSaver::all_registers) ==
908 RegisterSaver::live_reg_frame_size(RegisterSaver::all_registers_except_r2), "requirement");
909
910 // Load issuing PC (the return address for this stub).
911 const int frame_size_in_bytes = sasm->frame_size() * VMRegImpl::slots_per_word * VMRegImpl::stack_slot_size;
912 __ z_lg(Z_EXC_PC, Address(Z_SP, frame_size_in_bytes + _z_common_abi(return_pc)));
913 DEBUG_ONLY(__ z_lay(reg_fp, Address(Z_SP, frame_size_in_bytes));)
914
915 // Make sure that the vm_results are cleared (may be unnecessary).
916 __ clear_mem(Address(Z_thread, JavaThread::vm_result_oop_offset()), sizeof(oop));
917 __ clear_mem(Address(Z_thread, JavaThread::vm_result_metadata_offset()), sizeof(Metadata*));
918 break;
919 }
920 case StubId::c1_handle_exception_nofpu_id:
921 case StubId::c1_handle_exception_id:
922 // At this point all registers MAY be live.
923 DEBUG_ONLY(__ z_lgr(reg_fp, Z_SP);)
924 oop_map = save_live_registers(sasm, id != StubId::c1_handle_exception_nofpu_id, Z_EXC_PC);
925 break;
926 case StubId::c1_handle_exception_from_callee_id: {
927 // At this point all registers except Z_EXC_OOP and Z_EXC_PC are dead.
928 DEBUG_ONLY(__ z_lgr(reg_fp, Z_SP);)
929 __ save_return_pc(Z_EXC_PC);
930 const int frame_size_in_bytes = __ push_frame_abi160(0);
931 oop_map = new OopMap(frame_size_in_bytes / VMRegImpl::stack_slot_size, 0);
932 sasm->set_frame_size(frame_size_in_bytes / BytesPerWord);
933 break;
934 }
935 default: ShouldNotReachHere();
936 }
937
938 // Verify that only Z_EXC_OOP, and Z_EXC_PC are valid at this time.
939 __ invalidate_registers(Z_EXC_OOP, Z_EXC_PC, reg_fp);
940 // Verify that Z_EXC_OOP, contains a valid exception.
941 __ verify_not_null_oop(Z_EXC_OOP);
942
943 // Check that fields in JavaThread for exception oop and issuing pc
944 // are empty before writing to them.
945 __ asm_assert_mem8_is_zero(in_bytes(JavaThread::exception_oop_offset()), Z_thread, "exception oop already set : " FILE_AND_LINE, 0);
946 __ asm_assert_mem8_is_zero(in_bytes(JavaThread::exception_pc_offset()), Z_thread, "exception pc already set : " FILE_AND_LINE, 0);
947
948 // Save exception oop and issuing pc into JavaThread.
949 // (Exception handler will load it from here.)
950 __ z_stg(Z_EXC_OOP, Address(Z_thread, JavaThread::exception_oop_offset()));
951 __ z_stg(Z_EXC_PC, Address(Z_thread, JavaThread::exception_pc_offset()));
952
953 #ifdef ASSERT
954 { NearLabel ok;
955 __ z_cg(Z_EXC_PC, Address(reg_fp, _z_common_abi(return_pc)));
956 __ branch_optimized(Assembler::bcondEqual, ok);
957 __ stop("use throwing pc as return address (has bci & oop map)");
958 __ bind(ok);
959 }
960 #endif
961
962 // Compute the exception handler.
963 // The exception oop and the throwing pc are read from the fields in JavaThread.
964 int call_offset = __ call_RT(noreg, noreg, CAST_FROM_FN_PTR(address, exception_handler_for_pc));
965 oop_maps->add_gc_map(call_offset, oop_map);
966
967 // Z_RET(Z_R2): handler address
968 // will be the deopt blob if nmethod was deoptimized while we looked up
969 // handler regardless of whether handler existed in the nmethod.
970
971 // Only Z_R2, is valid at this time, all other registers have been destroyed by the runtime call.
972 __ invalidate_registers(Z_R2);
973
974 switch(id) {
975 case StubId::c1_forward_exception_id:
976 case StubId::c1_handle_exception_nofpu_id:
977 case StubId::c1_handle_exception_id:
978 // Restore the registers that were saved at the beginning.
979 __ z_lgr(Z_R1_scratch, Z_R2); // Restoring live registers kills Z_R2.
980 restore_live_registers(sasm, id != StubId::c1_handle_exception_nofpu_id); // Pops as well the frame.
981 __ z_br(Z_R1_scratch);
982 break;
983 case StubId::c1_handle_exception_from_callee_id: {
984 __ pop_frame();
985 __ z_br(Z_R2); // Jump to exception handler.
986 }
987 break;
988 default: ShouldNotReachHere();
989 }
990
991 return oop_maps;
992 }
993
994
995 #undef __
996
997 const char *Runtime1::pd_name_for_address(address entry) {
998 return "<unknown function>";
999 }