2 * Copyright (C) 2024 Mikulas Patocka
4 * This file is part of Ajla.
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
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.
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/>.
22 #define Z cpu_test_feature(CPU_FEATURE_z)
26 #define OP_SIZE_ADDRESS OP_SIZE_8
28 #define OP_SIZE_ADDRESS OP_SIZE_4
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(alu, im) ((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) ((alu) != ALU1_INC && (alu) != ALU1_DEC)
48 #define ARCH_IS_3ADDRESS_FP 0
49 #define ARCH_HAS_FLAGS 1
50 #define ARCH_PREFERS_SX(size) 1
51 #define ARCH_HAS_BWX 1
52 #define ARCH_HAS_MUL 1
53 #define ARCH_HAS_DIV 1
54 #define ARCH_HAS_ANDN cpu_test_feature(CPU_FEATURE_misc_insn_ext_3)
55 #define ARCH_HAS_SHIFTED_ADD(bits) 0
56 #define ARCH_HAS_BTX(btx, size, cnst) 0
57 #define ARCH_SHIFT_SIZE OP_SIZE_8
58 #define ARCH_NEEDS_BARRIER 0
60 #define i_size(size) maximum(size, OP_SIZE_4)
61 #define i_size_rot(size) maximum(size, OP_SIZE_4)
62 #define i_size_cmp(size) maximum(size, OP_SIZE_4)
103 #define R_SAVED_1 R_9
104 #define R_SAVED_2 R_10
106 #define R_SCRATCH_NA_1 R_1
107 #ifdef HAVE_BITWISE_FRAME
108 #define R_SCRATCH_NA_2 R_6
109 #define R_SCRATCH_NA_3 R_7
111 #define R_SCRATCH_1 R_3
112 #define R_SCRATCH_2 R_2
113 #define R_SCRATCH_3 R_5
114 #define R_SCRATCH_4 R_4
115 #define R_CONST_IMM (cpu_test_feature(CPU_FEATURE_extended_imm) ? R_0 : R_11)
116 #define R_OFFSET_IMM R_14
125 #define FR_SCRATCH_1 FR_0 /* + FR_2 */
126 #define FR_SCRATCH_2 FR_4 /* + FR_6 */
128 #define SUPPORTED_FP 0x16
131 #define FRAME_SIZE 160
132 #define FRAME_REGS 48
133 #define FRAME_RETPTR (FRAME_SIZE + 16)
134 #define FRAME_TIMESTAMP (FRAME_SIZE + 40)
136 #define FRAME_SIZE 96
137 #define FRAME_REGS 24
138 #define FRAME_FP_REGS 80
139 #define FRAME_RETPTR (FRAME_SIZE + 8)
140 #define FRAME_TIMESTAMP (FRAME_SIZE + 20)
143 static bool reg_is_fp(unsigned reg)
145 return reg >= 0x10 && reg < 0x20;
148 static const uint8_t regs_saved[] = {
149 #ifndef HAVE_BITWISE_FRAME
153 #define n_regs_saved (cpu_test_feature(CPU_FEATURE_extended_imm) ? n_array_elements(regs_saved) : n_array_elements(regs_saved) - 1)
154 static const uint8_t regs_volatile[] = { 0 };
155 #define n_regs_volatile 0U
156 static const uint8_t fp_saved[] = { 0 };
157 #define n_fp_saved 0U
158 static const uint8_t fp_volatile[] = { FR_1, FR_3, FR_5, FR_7, FR_2, FR_6 };
159 #define n_fp_volatile (!uses_x ? 6U : 4U)
160 #define reg_is_saved(r) (!reg_is_fp(r))
162 static bool attr_w gen_load_constant(struct codegen_context *ctx, unsigned reg, uint64_t c)
164 if (OP_SIZE_NATIVE == OP_SIZE_4)
166 if ((int64_t)c >= -0x8000 && (int64_t)c < 0x8000) {
167 gen_insn(INSN_MOV, OP_SIZE_NATIVE, 0, 0);
173 if (cpu_test_feature(CPU_FEATURE_extended_imm)) {
175 gen_insn(INSN_MOV, OP_SIZE_NATIVE, 0, 0);
178 gen_eight((int32_t)(int64_t)c);
179 sign = ((c >> 31) & 1) != 0;
181 if (c != (sign ? 0xffffffffU : 0)) {
182 gen_insn(INSN_MOV_MASK, OP_SIZE_8, MOV_MASK_32_64, 0);
190 gen_insn(INSN_MOV, OP_SIZE_NATIVE, 0, 0);
198 static bool attr_w s390_inline_address(int64_t imm)
200 if (likely(cpu_test_feature(CPU_FEATURE_long_displacement))) {
201 if (likely(imm >= -0x80000) && likely(imm < 0x80000))
204 if (imm >= 0 && imm < 0x1000)
209 static bool attr_w gen_address(struct codegen_context *ctx, unsigned base, int64_t imm, unsigned purpose, unsigned attr_unused size)
211 ctx->offset_imm = imm;
212 ctx->offset_reg = false;
213 ctx->base_reg = base;
215 case IMM_PURPOSE_LDR_OFFSET:
216 case IMM_PURPOSE_LDR_SX_OFFSET:
217 case IMM_PURPOSE_STR_OFFSET:
218 case IMM_PURPOSE_VLDR_VSTR_OFFSET:
219 case IMM_PURPOSE_MVI_CLI_OFFSET:
221 case IMM_PURPOSE_LDP_STP_OFFSET:
222 if (imm >= 0 && imm < 0x1000)
226 internal(file_line, "gen_address: invalid purpose %d", purpose);
228 if (s390_inline_address(imm))
232 g(gen_load_constant(ctx, R_OFFSET_IMM, imm));
234 if (purpose == IMM_PURPOSE_MVI_CLI_OFFSET || purpose == IMM_PURPOSE_LDP_STP_OFFSET) {
235 gen_insn(INSN_LEA3, OP_SIZE_ADDRESS, 0, 0);
236 gen_one(R_OFFSET_IMM);
237 gen_one(R_OFFSET_IMM);
241 ctx->base_reg = R_OFFSET_IMM;
246 ctx->offset_reg = true;
251 static bool is_direct_const(int64_t imm, unsigned purpose, unsigned size)
254 case IMM_PURPOSE_ADD:
255 if (imm >= -0x8000 && imm < 0x8000)
258 case IMM_PURPOSE_SUB:
259 if (imm > -0x8000 && imm <= 0x8000)
262 case IMM_PURPOSE_AND:
267 if (!(imm & ~0xffffULL))
269 if (!(imm & ~0xffff0000ULL))
271 if (!(imm & ~0xffff00000000ULL))
273 if (!(imm & ~0xffff000000000000ULL))
277 case IMM_PURPOSE_XOR:
278 if (cpu_test_feature(CPU_FEATURE_extended_imm)) {
279 if (!(imm & ~0xffffffffULL))
281 if (!(imm & ~0xffffffff00000000ULL))
285 case IMM_PURPOSE_TEST:
286 if (!(imm & ~0xffffULL))
288 if (!(imm & ~0xffff0000ULL))
290 if (!(imm & ~0xffff00000000ULL))
292 if (!(imm & ~0xffff000000000000ULL))
295 case IMM_PURPOSE_STORE_VALUE:
296 if (size == OP_SIZE_1)
305 static bool attr_w gen_imm(struct codegen_context *ctx, int64_t imm, unsigned purpose, unsigned size)
307 if (is_direct_const(imm, purpose, size)/* && (purpose == IMM_PURPOSE_TEST || purpose == IMM_PURPOSE_CMP)*/) {
308 ctx->const_imm = imm;
309 ctx->const_reg = false;
311 g(gen_load_constant(ctx, R_CONST_IMM, imm));
312 ctx->const_reg = true;
317 static bool attr_w gen_entry(struct codegen_context *ctx)
319 gen_insn(INSN_S390_PUSH, OP_SIZE_ADDRESS, 0, 0);
320 gen_one(ARG_ADDRESS_1);
322 gen_eight(FRAME_REGS);
323 #if defined(ARCH_S390_32)
324 gen_insn(INSN_MOV, OP_SIZE_8, 0, 0);
325 gen_one(ARG_ADDRESS_1);
327 gen_eight(FRAME_FP_REGS);
330 gen_insn(INSN_MOV, OP_SIZE_8, 0, 0);
331 gen_one(ARG_ADDRESS_1);
333 gen_eight(FRAME_FP_REGS + 8);
336 gen_insn(INSN_ALU, OP_SIZE_NATIVE, ALU_ADD, ALU_WRITES_FLAGS(ALU_ADD, true));
340 gen_eight(-FRAME_SIZE);
342 gen_insn(INSN_MOV, OP_SIZE_ADDRESS, 0, 0);
343 gen_one(ARG_ADDRESS_1);
345 gen_eight(FRAME_RETPTR);
348 gen_insn(INSN_MOV, OP_SIZE_NATIVE, 0, 0);
352 gen_insn(INSN_MOV, OP_SIZE_NATIVE, 0, 0);
356 gen_insn(INSN_MOV, OP_SIZE_4, 0, 0);
357 gen_one(ARG_ADDRESS_1);
359 gen_eight(FRAME_TIMESTAMP);
362 gen_insn(INSN_JMP_INDIRECT, 0, 0, 0);
368 static bool attr_w gen_escape_arg(struct codegen_context *ctx, ip_t ip, uint32_t escape_label)
370 g(gen_load_constant(ctx, R_UPCALL, ip));
372 gen_insn(INSN_JMP, 0, 0, 0);
373 gen_four(escape_label);
378 static bool attr_w gen_escape(struct codegen_context *ctx)
380 #if defined(ARCH_S390_32)
381 gen_insn(INSN_MOV, OP_SIZE_8, 0, 0);
383 gen_one(ARG_ADDRESS_1);
385 gen_eight(FRAME_SIZE + FRAME_FP_REGS);
387 gen_insn(INSN_MOV, OP_SIZE_8, 0, 0);
389 gen_one(ARG_ADDRESS_1);
391 gen_eight(FRAME_SIZE + FRAME_FP_REGS + 8);
393 gen_insn(INSN_MOV, OP_SIZE_ADDRESS, 0, 0);
395 gen_one(ARG_ADDRESS_1);
397 gen_eight(FRAME_RETPTR);
399 gen_insn(INSN_STP, OP_SIZE_ADDRESS, 0, 0);
400 gen_one(ARG_ADDRESS_1);
406 gen_insn(INSN_S390_POP, OP_SIZE_ADDRESS, 0, 0);
407 gen_one(ARG_ADDRESS_1);
409 gen_eight(FRAME_SIZE + FRAME_REGS);
411 gen_insn(INSN_RET, 0, 0, 0);
415 static bool attr_w gen_upcall_argument(struct codegen_context attr_unused *ctx, unsigned attr_unused arg)
420 static bool attr_w gen_upcall(struct codegen_context *ctx, unsigned offset, unsigned n_args)
422 g(gen_address(ctx, R_UPCALL, offset, IMM_PURPOSE_LDR_OFFSET, OP_SIZE_ADDRESS));
423 gen_insn(INSN_MOV, OP_SIZE_ADDRESS, 0, 0);
424 gen_one(R_OFFSET_IMM);
425 gen_address_offset();
427 gen_insn(INSN_CALL_INDIRECT, OP_SIZE_ADDRESS, 0, 0);
428 gen_one(R_OFFSET_IMM);
430 g(gen_upcall_end(ctx, n_args));
435 static bool attr_w gen_timestamp_test(struct codegen_context *ctx, uint32_t escape_label)
437 gen_insn(INSN_MOV, OP_SIZE_4, 0, 0);
438 gen_one(R_SCRATCH_1);
439 gen_one(ARG_ADDRESS_1);
441 gen_eight(FRAME_TIMESTAMP);
443 g(gen_address(ctx, R_UPCALL, offsetof(struct cg_upcall_vector_s, ts), IMM_PURPOSE_LDR_OFFSET, OP_SIZE_4));
444 gen_insn(INSN_CMP, OP_SIZE_4, 0, 1);
445 gen_one(R_SCRATCH_1);
446 gen_address_offset();
448 gen_insn(INSN_JMP_COND, OP_SIZE_4, COND_NE, 0);
449 gen_four(escape_label);