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_HAS_FLAGS 1
49 #define ARCH_PREFERS_SX(size) 1
50 #define ARCH_HAS_BWX 1
51 #define ARCH_HAS_MUL 1
52 #define ARCH_HAS_DIV 1
53 #define ARCH_HAS_ANDN cpu_test_feature(CPU_FEATURE_misc_insn_ext_3)
54 #define ARCH_HAS_SHIFTED_ADD(bits) 0
55 #define ARCH_HAS_BTX(btx, size, cnst) 0
56 #define ARCH_SHIFT_SIZE OP_SIZE_8
57 #define ARCH_NEEDS_BARRIER 0
59 #define i_size(size) maximum(size, OP_SIZE_4)
60 #define i_size_rot(size) maximum(size, OP_SIZE_4)
101 #define R_SAVED_1 R_9
102 #define R_SAVED_2 R_10
104 #define R_SCRATCH_NA_1 R_1
105 #ifdef HAVE_BITWISE_FRAME
106 #define R_SCRATCH_NA_2 R_6
107 #define R_SCRATCH_NA_3 R_7
109 #define R_SCRATCH_1 R_3
110 #define R_SCRATCH_2 R_2
111 #define R_SCRATCH_3 R_5
112 #define R_SCRATCH_4 R_4
113 #define R_CONST_IMM (cpu_test_feature(CPU_FEATURE_extended_imm) ? R_0 : R_11)
114 #define R_OFFSET_IMM R_14
123 #define FR_SCRATCH_1 FR_0 /* + FR_2 */
124 #define FR_SCRATCH_2 FR_4 /* + FR_6 */
126 #define SUPPORTED_FP 0x16
129 #define FRAME_SIZE 160
130 #define FRAME_REGS 48
131 #define FRAME_RETPTR (FRAME_SIZE + 16)
132 #define FRAME_TIMESTAMP (FRAME_SIZE + 40)
134 #define FRAME_SIZE 96
135 #define FRAME_REGS 24
136 #define FRAME_FP_REGS 80
137 #define FRAME_RETPTR (FRAME_SIZE + 8)
138 #define FRAME_TIMESTAMP (FRAME_SIZE + 20)
141 static bool reg_is_fp(unsigned reg)
143 return reg >= 0x10 && reg < 0x20;
146 static const uint8_t regs_saved[] = {
147 #ifndef HAVE_BITWISE_FRAME
151 #define n_regs_saved (cpu_test_feature(CPU_FEATURE_extended_imm) ? n_array_elements(regs_saved) : n_array_elements(regs_saved) - 1)
152 static const uint8_t regs_volatile[] = { 0 };
153 #define n_regs_volatile 0U
154 static const uint8_t fp_saved[] = { 0 };
155 #define n_fp_saved 0U
156 static const uint8_t fp_volatile[] = { FR_1, FR_3, FR_5, FR_7, FR_2, FR_6 };
157 #define n_fp_volatile (!uses_x ? 6U : 4U)
158 #define reg_is_saved(r) (!reg_is_fp(r))
160 static bool attr_w gen_load_constant(struct codegen_context *ctx, unsigned reg, uint64_t c)
162 if (OP_SIZE_NATIVE == OP_SIZE_4)
164 if ((int64_t)c >= -0x8000 && (int64_t)c < 0x8000) {
165 gen_insn(INSN_MOV, OP_SIZE_NATIVE, 0, 0);
171 if (cpu_test_feature(CPU_FEATURE_extended_imm)) {
173 gen_insn(INSN_MOV, OP_SIZE_NATIVE, 0, 0);
176 gen_eight((int32_t)(int64_t)c);
177 sign = ((c >> 31) & 1) != 0;
179 if (c != (sign ? 0xffffffffU : 0)) {
180 gen_insn(INSN_MOV_MASK, OP_SIZE_8, MOV_MASK_32_64, 0);
188 gen_insn(INSN_MOV, OP_SIZE_NATIVE, 0, 0);
196 static bool attr_w s390_inline_address(int64_t imm)
198 if (likely(cpu_test_feature(CPU_FEATURE_long_displacement))) {
199 if (likely(imm >= -0x80000) && likely(imm < 0x80000))
202 if (imm >= 0 && imm < 0x1000)
207 static bool attr_w gen_address(struct codegen_context *ctx, unsigned base, int64_t imm, unsigned purpose, unsigned attr_unused size)
209 ctx->offset_imm = imm;
210 ctx->offset_reg = false;
211 ctx->base_reg = base;
213 case IMM_PURPOSE_LDR_OFFSET:
214 case IMM_PURPOSE_LDR_SX_OFFSET:
215 case IMM_PURPOSE_STR_OFFSET:
216 case IMM_PURPOSE_VLDR_VSTR_OFFSET:
217 case IMM_PURPOSE_MVI_CLI_OFFSET:
219 case IMM_PURPOSE_LDP_STP_OFFSET:
220 if (imm >= 0 && imm < 0x1000)
224 internal(file_line, "gen_address: invalid purpose %d", purpose);
226 if (s390_inline_address(imm))
230 g(gen_load_constant(ctx, R_OFFSET_IMM, imm));
232 if (purpose == IMM_PURPOSE_MVI_CLI_OFFSET || purpose == IMM_PURPOSE_LDP_STP_OFFSET) {
233 gen_insn(INSN_LEA3, OP_SIZE_ADDRESS, 0, 0);
234 gen_one(R_OFFSET_IMM);
235 gen_one(R_OFFSET_IMM);
239 ctx->base_reg = R_OFFSET_IMM;
244 ctx->offset_reg = true;
249 static bool is_direct_const(int64_t imm, unsigned purpose, unsigned size)
252 case IMM_PURPOSE_ADD:
253 if (imm >= -0x8000 && imm < 0x8000)
256 case IMM_PURPOSE_SUB:
257 if (imm > -0x8000 && imm <= 0x8000)
260 case IMM_PURPOSE_AND:
265 if (!(imm & ~0xffffULL))
267 if (!(imm & ~0xffff0000ULL))
269 if (!(imm & ~0xffff00000000ULL))
271 if (!(imm & ~0xffff000000000000ULL))
275 case IMM_PURPOSE_XOR:
276 if (cpu_test_feature(CPU_FEATURE_extended_imm)) {
277 if (!(imm & ~0xffffffffULL))
279 if (!(imm & ~0xffffffff00000000ULL))
283 case IMM_PURPOSE_TEST:
284 if (!(imm & ~0xffffULL))
286 if (!(imm & ~0xffff0000ULL))
288 if (!(imm & ~0xffff00000000ULL))
290 if (!(imm & ~0xffff000000000000ULL))
293 case IMM_PURPOSE_STORE_VALUE:
294 if (size == OP_SIZE_1)
303 static bool attr_w gen_imm(struct codegen_context *ctx, int64_t imm, unsigned purpose, unsigned size)
305 if (is_direct_const(imm, purpose, size)/* && (purpose == IMM_PURPOSE_TEST || purpose == IMM_PURPOSE_CMP)*/) {
306 ctx->const_imm = imm;
307 ctx->const_reg = false;
309 g(gen_load_constant(ctx, R_CONST_IMM, imm));
310 ctx->const_reg = true;
315 static bool attr_w gen_entry(struct codegen_context *ctx)
317 gen_insn(INSN_S390_PUSH, OP_SIZE_ADDRESS, 0, 0);
318 gen_one(ARG_ADDRESS_1);
320 gen_eight(FRAME_REGS);
321 #if defined(ARCH_S390_32)
322 gen_insn(INSN_MOV, OP_SIZE_8, 0, 0);
323 gen_one(ARG_ADDRESS_1);
325 gen_eight(FRAME_FP_REGS);
328 gen_insn(INSN_MOV, OP_SIZE_8, 0, 0);
329 gen_one(ARG_ADDRESS_1);
331 gen_eight(FRAME_FP_REGS + 8);
334 gen_insn(INSN_ALU, OP_SIZE_NATIVE, ALU_ADD, ALU_WRITES_FLAGS(ALU_ADD, true));
338 gen_eight(-FRAME_SIZE);
340 gen_insn(INSN_MOV, OP_SIZE_ADDRESS, 0, 0);
341 gen_one(ARG_ADDRESS_1);
343 gen_eight(FRAME_RETPTR);
346 gen_insn(INSN_MOV, OP_SIZE_NATIVE, 0, 0);
350 gen_insn(INSN_MOV, OP_SIZE_NATIVE, 0, 0);
354 gen_insn(INSN_MOV, OP_SIZE_4, 0, 0);
355 gen_one(ARG_ADDRESS_1);
357 gen_eight(FRAME_TIMESTAMP);
360 gen_insn(INSN_JMP_INDIRECT, 0, 0, 0);
366 static bool attr_w gen_escape_arg(struct codegen_context *ctx, ip_t ip, uint32_t escape_label)
368 g(gen_load_constant(ctx, R_UPCALL, ip));
370 gen_insn(INSN_JMP, 0, 0, 0);
371 gen_four(escape_label);
376 static bool attr_w gen_escape(struct codegen_context *ctx)
378 #if defined(ARCH_S390_32)
379 gen_insn(INSN_MOV, OP_SIZE_8, 0, 0);
381 gen_one(ARG_ADDRESS_1);
383 gen_eight(FRAME_SIZE + FRAME_FP_REGS);
385 gen_insn(INSN_MOV, OP_SIZE_8, 0, 0);
387 gen_one(ARG_ADDRESS_1);
389 gen_eight(FRAME_SIZE + FRAME_FP_REGS + 8);
391 gen_insn(INSN_MOV, OP_SIZE_ADDRESS, 0, 0);
393 gen_one(ARG_ADDRESS_1);
395 gen_eight(FRAME_RETPTR);
397 gen_insn(INSN_STP, OP_SIZE_ADDRESS, 0, 0);
398 gen_one(ARG_ADDRESS_1);
404 gen_insn(INSN_S390_POP, OP_SIZE_ADDRESS, 0, 0);
405 gen_one(ARG_ADDRESS_1);
407 gen_eight(FRAME_SIZE + FRAME_REGS);
409 gen_insn(INSN_RET, 0, 0, 0);
413 static bool attr_w gen_upcall_argument(struct codegen_context attr_unused *ctx, unsigned attr_unused arg)
418 static bool attr_w gen_upcall(struct codegen_context *ctx, unsigned offset, unsigned n_args)
420 g(gen_address(ctx, R_UPCALL, offset, IMM_PURPOSE_LDR_OFFSET, OP_SIZE_ADDRESS));
421 gen_insn(INSN_MOV, OP_SIZE_ADDRESS, 0, 0);
422 gen_one(R_OFFSET_IMM);
423 gen_address_offset();
425 gen_insn(INSN_CALL_INDIRECT, OP_SIZE_ADDRESS, 0, 0);
426 gen_one(R_OFFSET_IMM);
428 g(gen_upcall_end(ctx, n_args));
433 static bool attr_w gen_timestamp_test(struct codegen_context *ctx, uint32_t escape_label)
435 gen_insn(INSN_MOV, OP_SIZE_4, 0, 0);
436 gen_one(R_SCRATCH_1);
437 gen_one(ARG_ADDRESS_1);
439 gen_eight(FRAME_TIMESTAMP);
441 g(gen_address(ctx, R_UPCALL, offsetof(struct cg_upcall_vector_s, ts), IMM_PURPOSE_LDR_OFFSET, OP_SIZE_4));
442 gen_insn(INSN_CMP, OP_SIZE_4, 0, 1);
443 gen_one(R_SCRATCH_1);
444 gen_address_offset();
446 gen_insn(INSN_JMP_COND, OP_SIZE_4, COND_NE, 0);
447 gen_four(escape_label);