x86-64: hack the ABI of cg_upcall_ipret_copy_variable_to_pointer
[ajla.git] / c1-s390.inc
blobf021ce86c87b5ff5b5813446601c88e5e65ba375
1 /*
2  * Copyright (C) 2024 Mikulas Patocka
3  *
4  * This file is part of Ajla.
5  *
6  * Ajla is free software: you can redistribute it and/or modify it under the
7  * terms of the GNU General Public License as published by the Free Software
8  * Foundation, either version 3 of the License, or (at your option) any later
9  * version.
10  *
11  * Ajla is distributed in the hope that it will be useful, but WITHOUT ANY
12  * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
13  * A PARTICULAR PURPOSE. See the GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License along with
16  * Ajla. If not, see <https://www.gnu.org/licenses/>.
17  */
19 #ifdef ARCH_S390_64
20 #define Z                               1
21 #else
22 #define Z                               cpu_test_feature(CPU_FEATURE_z)
23 #endif
25 #ifdef ARCH_S390_64
26 #define OP_SIZE_ADDRESS                 OP_SIZE_8
27 #else
28 #define OP_SIZE_ADDRESS                 OP_SIZE_4
29 #endif
31 #define OP_SIZE_NATIVE                  (Z ? OP_SIZE_8 : OP_SIZE_4)
33 #define JMP_LIMIT                       JMP_SHORT
35 #define UNALIGNED_TRAP                  0
37 #define ALU_WRITES_FLAGS(size, alu, is_mem, is_imm, imm)        ((alu) != ALU_MUL)
38 #define ALU1_WRITES_FLAGS(alu)          1
39 #define ROT_WRITES_FLAGS(alu, size, im) ((alu) == ROT_SAR || (alu) == ROT_SAL)
40 #define COND_IS_LOGICAL(cond)           ((cond) == COND_B || (cond) == COND_AE || (cond) == COND_BE || (cond) == COND_A)
42 #define ARCH_PARTIAL_ALU(size)          ((size) < OP_SIZE_NATIVE)
43 #define ARCH_IS_3ADDRESS(alu, f)        cpu_test_feature(CPU_FEATURE_misc_45)
44 #define ARCH_IS_3ADDRESS_IMM(alu, f)    0
45 #define ARCH_IS_3ADDRESS_ROT(alu, size) cpu_test_feature(CPU_FEATURE_misc_45)
46 #define ARCH_IS_3ADDRESS_ROT_IMM(alu)   0
47 #define ARCH_IS_2ADDRESS(alu)           1
48 #define ARCH_IS_3ADDRESS_FP             0
49 #define ARCH_HAS_JMP_2REGS(cond)        0
50 #define ARCH_HAS_FLAGS                  1
51 #define ARCH_PREFERS_SX(size)           1
52 #define ARCH_HAS_BWX                    1
53 #define ARCH_HAS_MUL                    1
54 #define ARCH_HAS_DIV                    1
55 #define ARCH_HAS_ANDN                   cpu_test_feature(CPU_FEATURE_misc_insn_ext_3)
56 #define ARCH_HAS_SHIFTED_ADD(bits)      0
57 #define ARCH_HAS_BTX(btx, size, cnst)   0
58 #define ARCH_SHIFT_SIZE                 OP_SIZE_8
59 #define ARCH_BOOL_SIZE                  OP_SIZE_4
60 #define ARCH_HAS_FP_GP_MOV              0
61 #define ARCH_NEEDS_BARRIER              0
63 #define i_size(size)                    maximum(size, OP_SIZE_4)
64 #define i_size_rot(size)                maximum(size, OP_SIZE_4)
65 #define i_size_cmp(size)                maximum(size, OP_SIZE_4)
67 #define R_0             0x00
68 #define R_1             0x01
69 #define R_2             0x02
70 #define R_3             0x03
71 #define R_4             0x04
72 #define R_5             0x05
73 #define R_6             0x06
74 #define R_7             0x07
75 #define R_8             0x08
76 #define R_9             0x09
77 #define R_10            0x0a
78 #define R_11            0x0b
79 #define R_12            0x0c
80 #define R_13            0x0d
81 #define R_14            0x0e
82 #define R_15            0x0f
84 #define FR_0            0x10
85 #define FR_1            0x11
86 #define FR_2            0x12
87 #define FR_3            0x13
88 #define FR_4            0x14
89 #define FR_5            0x15
90 #define FR_6            0x16
91 #define FR_7            0x17
92 #define FR_8            0x18
93 #define FR_9            0x19
94 #define FR_10           0x1a
95 #define FR_11           0x1b
96 #define FR_12           0x1c
97 #define FR_13           0x1d
98 #define FR_14           0x1e
99 #define FR_15           0x1f
101 #define R_FRAME         R_12
102 #define R_UPCALL        R_13
104 #define R_SP            R_15
106 #define R_SAVED_1       R_9
107 #define R_SAVED_2       R_10
109 #define R_SCRATCH_NA_1  R_1
110 #ifdef HAVE_BITWISE_FRAME
111 #define R_SCRATCH_NA_2  R_6
112 #define R_SCRATCH_NA_3  R_7
113 #endif
114 #define R_SCRATCH_1     R_3
115 #define R_SCRATCH_2     R_2
116 #define R_SCRATCH_3     R_5
117 #define R_SCRATCH_4     R_4
118 #define R_CONST_IMM     (cpu_test_feature(CPU_FEATURE_extended_imm) ? R_0 : R_11)
119 #define R_OFFSET_IMM    R_14
121 #define R_ARG0          R_2
122 #define R_ARG1          R_3
123 #define R_ARG2          R_4
124 #define R_ARG3          R_5
125 #define R_ARG4          R_6
126 #define R_RET0          R_2
128 #define FR_SCRATCH_1    FR_0    /* + FR_2 */
129 #define FR_SCRATCH_2    FR_4    /* + FR_6 */
131 #define SUPPORTED_FP    0x16
133 #ifdef ARCH_S390_64
134 #define FRAME_SIZE      160
135 #define FRAME_REGS      48
136 #define FRAME_RETPTR    (FRAME_SIZE + 16)
137 #define FRAME_TIMESTAMP (FRAME_SIZE + 40)
138 #else
139 #define FRAME_SIZE      96
140 #define FRAME_REGS      24
141 #define FRAME_FP_REGS   80
142 #define FRAME_RETPTR    (FRAME_SIZE + 8)
143 #define FRAME_TIMESTAMP (FRAME_SIZE + 20)
144 #endif
146 static bool reg_is_fp(unsigned reg)
148         return reg >= 0x10 && reg < 0x20;
151 static const uint8_t regs_saved[] = {
152 #ifndef HAVE_BITWISE_FRAME
153         R_6, R_7,
154 #endif
155         R_8, R_11 };
156 #define n_regs_saved (cpu_test_feature(CPU_FEATURE_extended_imm) ? n_array_elements(regs_saved) : n_array_elements(regs_saved) - 1)
157 static const uint8_t regs_volatile[] = { 0 };
158 #define n_regs_volatile 0U
159 static const uint8_t fp_saved[] = { 0 };
160 #define n_fp_saved 0U
161 static const uint8_t fp_volatile[] = { FR_1, FR_3, FR_5, FR_7, FR_2, FR_6 };
162 #define n_fp_volatile   (!uses_x ? 6U : 4U)
163 #define reg_is_saved(r) (!reg_is_fp(r))
165 static bool attr_w gen_load_constant(struct codegen_context *ctx, unsigned reg, uint64_t c)
167         if (OP_SIZE_NATIVE == OP_SIZE_4)
168                 c = (int32_t)c;
169         if ((int64_t)c >= -0x8000 && (int64_t)c < 0x8000) {
170                 gen_insn(INSN_MOV, OP_SIZE_NATIVE, 0, 0);
171                 gen_one(reg);
172                 gen_one(ARG_IMM);
173                 gen_eight(c);
174                 return true;
175         }
176         if (cpu_test_feature(CPU_FEATURE_extended_imm)) {
177                 bool sign;
178                 gen_insn(INSN_MOV, OP_SIZE_NATIVE, 0, 0);
179                 gen_one(reg);
180                 gen_one(ARG_IMM);
181                 gen_eight((int32_t)(int64_t)c);
182                 sign = ((c >> 31) & 1) != 0;
183                 c >>= 32;
184                 if (c != (sign ? 0xffffffffU : 0)) {
185                         gen_insn(INSN_MOV_MASK, OP_SIZE_8, MOV_MASK_32_64, 0);
186                         gen_one(reg);
187                         gen_one(reg);
188                         gen_one(ARG_IMM);
189                         gen_eight(c);
190                 }
191                 return true;
192         } else {
193                 gen_insn(INSN_MOV, OP_SIZE_NATIVE, 0, 0);
194                 gen_one(reg);
195                 gen_one(ARG_IMM);
196                 gen_eight(c);
197                 return true;
198         }
201 static bool attr_w s390_inline_address(int64_t imm)
203         if (likely(cpu_test_feature(CPU_FEATURE_long_displacement))) {
204                 if (likely(imm >= -0x80000) && likely(imm < 0x80000))
205                         return true;
206         }
207         if (imm >= 0 && imm < 0x1000)
208                 return true;
209         return false;
212 static bool attr_w gen_address(struct codegen_context *ctx, unsigned base, int64_t imm, unsigned purpose, unsigned attr_unused size)
214         ctx->offset_imm = imm;
215         ctx->offset_reg = false;
216         ctx->base_reg = base;
217         switch (purpose) {
218                 case IMM_PURPOSE_LDR_OFFSET:
219                 case IMM_PURPOSE_LDR_SX_OFFSET:
220                 case IMM_PURPOSE_STR_OFFSET:
221                 case IMM_PURPOSE_VLDR_VSTR_OFFSET:
222                 case IMM_PURPOSE_MVI_CLI_OFFSET:
223                         break;
224                 case IMM_PURPOSE_LDP_STP_OFFSET:
225                         if (imm >= 0 && imm < 0x1000)
226                                 return true;
227                         goto load_cnst;
228                 default:
229                         internal(file_line, "gen_address: invalid purpose %d", purpose);
230         }
231         if (s390_inline_address(imm))
232                 return true;
234 load_cnst:
235         g(gen_load_constant(ctx, R_OFFSET_IMM, imm));
237         if (purpose == IMM_PURPOSE_MVI_CLI_OFFSET || purpose == IMM_PURPOSE_LDP_STP_OFFSET) {
238                 gen_insn(INSN_LEA3, OP_SIZE_ADDRESS, 0, 0);
239                 gen_one(R_OFFSET_IMM);
240                 gen_one(R_OFFSET_IMM);
241                 gen_one(base);
242                 gen_one(ARG_IMM);
243                 gen_eight(0);
244                 ctx->base_reg = R_OFFSET_IMM;
245                 ctx->offset_imm = 0;
246                 return true;
247         }
249         ctx->offset_reg = true;
251         return true;
254 static bool is_direct_const(int64_t imm, unsigned purpose, unsigned size)
256         switch (purpose) {
257                 case IMM_PURPOSE_ADD:
258                         if (imm >= -0x8000 && imm < 0x8000)
259                                 return true;
260                         break;
261                 case IMM_PURPOSE_SUB:
262                         if (imm > -0x8000 && imm <= 0x8000)
263                                 return true;
264                         break;
265                 case IMM_PURPOSE_CMP:
266                         if (imm >= -0x8000 && imm < 0x8000)
267                                 return true;
268                         /*if (cpu_test_feature(CPU_FEATURE_extended_imm)) {
269                                 if (imm >= -0x80000000LL && imm < 0x80000000LL)
270                                         return true;
271                         }*/
272                         break;
273                 case IMM_PURPOSE_CMP_LOGICAL:
274                         /*if (cpu_test_feature(CPU_FEATURE_extended_imm)) {
275                                 if (imm >= 0LL && imm < 0x100000000LL)
276                                         return true;
277                         }*/
278                         break;
279                 case IMM_PURPOSE_AND:
280                         imm = ~imm;
281                         /*-fallthrough*/
282                 case IMM_PURPOSE_OR:
283                         if (Z) {
284                                 if (!(imm & ~0xffffULL))
285                                         return true;
286                                 if (!(imm & ~0xffff0000ULL))
287                                         return true;
288                                 if (!(imm & ~0xffff00000000ULL))
289                                         return true;
290                                 if (!(imm & ~0xffff000000000000ULL))
291                                         return true;
292                         }
293                         /*-fallthrough*/
294                 case IMM_PURPOSE_XOR:
295                         if (cpu_test_feature(CPU_FEATURE_extended_imm)) {
296                                 if (!(imm & ~0xffffffffULL))
297                                         return true;
298                                 if (!(imm & ~0xffffffff00000000ULL))
299                                         return true;
300                         }
301                         break;
302                 case IMM_PURPOSE_TEST:
303                         if (!(imm & ~0xffffULL))
304                                 return true;
305                         if (!(imm & ~0xffff0000ULL))
306                                 return true;
307                         if (!(imm & ~0xffff00000000ULL))
308                                 return true;
309                         if (!(imm & ~0xffff000000000000ULL))
310                                 return true;
311                         break;
312                 case IMM_PURPOSE_STORE_VALUE:
313                         if (size == OP_SIZE_1)
314                                 return true;
315                         break;
316                 default:
317                         break;
318         }
319         return false;
322 static bool attr_w gen_entry(struct codegen_context *ctx)
324         gen_insn(INSN_S390_PUSH, OP_SIZE_ADDRESS, 0, 0);
325         gen_one(ARG_ADDRESS_1);
326         gen_one(R_SP);
327         gen_eight(FRAME_REGS);
328 #if defined(ARCH_S390_32)
329         gen_insn(INSN_MOV, OP_SIZE_8, 0, 0);
330         gen_one(ARG_ADDRESS_1);
331         gen_one(R_SP);
332         gen_eight(FRAME_FP_REGS);
333         gen_one(FR_4);
335         gen_insn(INSN_MOV, OP_SIZE_8, 0, 0);
336         gen_one(ARG_ADDRESS_1);
337         gen_one(R_SP);
338         gen_eight(FRAME_FP_REGS + 8);
339         gen_one(FR_6);
340 #endif
341         gen_insn(INSN_ALU, OP_SIZE_NATIVE, ALU_ADD, ALU_WRITES_FLAGS(OP_SIZE_NATIVE, ALU_ADD, false, true, -FRAME_SIZE));
342         gen_one(R_SP);
343         gen_one(R_SP);
344         gen_one(ARG_IMM);
345         gen_eight(-FRAME_SIZE);
347         gen_insn(INSN_MOV, OP_SIZE_ADDRESS, 0, 0);
348         gen_one(ARG_ADDRESS_1);
349         gen_one(R_SP);
350         gen_eight(FRAME_RETPTR);
351         gen_one(R_ARG0);
353         gen_insn(INSN_MOV, OP_SIZE_NATIVE, 0, 0);
354         gen_one(R_FRAME);
355         gen_one(R_ARG1);
357         gen_insn(INSN_MOV, OP_SIZE_NATIVE, 0, 0);
358         gen_one(R_UPCALL);
359         gen_one(R_ARG2);
361         gen_insn(INSN_MOV, OP_SIZE_4, 0, 0);
362         gen_one(ARG_ADDRESS_1);
363         gen_one(R_SP);
364         gen_eight(FRAME_TIMESTAMP);
365         gen_one(R_ARG3);
367         gen_insn(INSN_JMP_INDIRECT, 0, 0, 0);
368         gen_one(R_ARG4);
370         return true;
373 static bool attr_w gen_escape_arg(struct codegen_context *ctx, ip_t ip, uint32_t escape_label)
375         g(gen_load_constant(ctx, R_UPCALL, ip));
377         gen_insn(INSN_JMP, 0, 0, 0);
378         gen_four(escape_label);
380         return true;
383 static bool attr_w gen_escape(struct codegen_context *ctx)
385 #if defined(ARCH_S390_32)
386         gen_insn(INSN_MOV, OP_SIZE_8, 0, 0);
387         gen_one(FR_4);
388         gen_one(ARG_ADDRESS_1);
389         gen_one(R_SP);
390         gen_eight(FRAME_SIZE + FRAME_FP_REGS);
392         gen_insn(INSN_MOV, OP_SIZE_8, 0, 0);
393         gen_one(FR_6);
394         gen_one(ARG_ADDRESS_1);
395         gen_one(R_SP);
396         gen_eight(FRAME_SIZE + FRAME_FP_REGS + 8);
397 #endif
398         gen_insn(INSN_MOV, OP_SIZE_ADDRESS, 0, 0);
399         gen_one(R_ARG0);
400         gen_one(ARG_ADDRESS_1);
401         gen_one(R_SP);
402         gen_eight(FRAME_RETPTR);
404         gen_insn(INSN_STP, OP_SIZE_ADDRESS, 0, 0);
405         gen_one(ARG_ADDRESS_1);
406         gen_one(R_ARG0);
407         gen_eight(0);
408         gen_one(R_UPCALL);
409         gen_one(R_FRAME);
411         gen_insn(INSN_S390_POP, OP_SIZE_ADDRESS, 0, 0);
412         gen_one(ARG_ADDRESS_1);
413         gen_one(R_SP);
414         gen_eight(FRAME_SIZE + FRAME_REGS);
416         gen_insn(INSN_RET, 0, 0, 0);
417         return true;
420 static bool attr_w gen_upcall_argument(struct codegen_context attr_unused *ctx, unsigned attr_unused arg)
422         return true;
425 static bool attr_w gen_get_upcall_pointer(struct codegen_context *ctx, unsigned offset, unsigned reg)
427         g(gen_address(ctx, R_UPCALL, offset, IMM_PURPOSE_LDR_OFFSET, OP_SIZE_ADDRESS));
428         gen_insn(INSN_MOV, OP_SIZE_ADDRESS, 0, 0);
429         gen_one(reg);
430         gen_address_offset();
432         return true;
435 static bool attr_w gen_upcall(struct codegen_context *ctx, unsigned offset, unsigned n_args)
437         g(gen_get_upcall_pointer(ctx, offset, R_OFFSET_IMM));
439         gen_insn(INSN_CALL_INDIRECT, OP_SIZE_ADDRESS, 0, 0);
440         gen_one(R_OFFSET_IMM);
442         g(gen_upcall_end(ctx, offset, n_args));
444         return true;
447 static bool attr_w gen_timestamp_test(struct codegen_context *ctx, uint32_t escape_label)
449         gen_insn(INSN_MOV, OP_SIZE_4, 0, 0);
450         gen_one(R_SCRATCH_1);
451         gen_one(ARG_ADDRESS_1);
452         gen_one(R_SP);
453         gen_eight(FRAME_TIMESTAMP);
455         g(gen_address(ctx, R_UPCALL, offsetof(struct cg_upcall_vector_s, ts), IMM_PURPOSE_LDR_OFFSET, OP_SIZE_4));
456         gen_insn(INSN_CMP, OP_SIZE_4, 0, 1);
457         gen_one(R_SCRATCH_1);
458         gen_address_offset();
460         gen_insn(INSN_JMP_COND, OP_SIZE_4, COND_NE, 0);
461         gen_four(escape_label);
463         return true;