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(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)
102 #define R_UPCALL R_13
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
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
128 #define FR_SCRATCH_1 FR_0 /* + FR_2 */
129 #define FR_SCRATCH_2 FR_4 /* + FR_6 */
131 #define SUPPORTED_FP 0x16
134 #define FRAME_SIZE 160
135 #define FRAME_REGS 48
136 #define FRAME_RETPTR (FRAME_SIZE + 16)
137 #define FRAME_TIMESTAMP (FRAME_SIZE + 40)
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)
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
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)
169 if ((int64_t)c >= -0x8000 && (int64_t)c < 0x8000) {
170 gen_insn(INSN_MOV, OP_SIZE_NATIVE, 0, 0);
176 if (cpu_test_feature(CPU_FEATURE_extended_imm)) {
178 gen_insn(INSN_MOV, OP_SIZE_NATIVE, 0, 0);
181 gen_eight((int32_t)(int64_t)c);
182 sign = ((c >> 31) & 1) != 0;
184 if (c != (sign ? 0xffffffffU : 0)) {
185 gen_insn(INSN_MOV_MASK, OP_SIZE_8, MOV_MASK_32_64, 0);
193 gen_insn(INSN_MOV, OP_SIZE_NATIVE, 0, 0);
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))
207 if (imm >= 0 && imm < 0x1000)
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;
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:
224 case IMM_PURPOSE_LDP_STP_OFFSET:
225 if (imm >= 0 && imm < 0x1000)
229 internal(file_line, "gen_address: invalid purpose %d", purpose);
231 if (s390_inline_address(imm))
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);
244 ctx->base_reg = R_OFFSET_IMM;
249 ctx->offset_reg = true;
254 static bool is_direct_const(int64_t imm, unsigned purpose, unsigned size)
257 case IMM_PURPOSE_ADD:
258 if (imm >= -0x8000 && imm < 0x8000)
261 case IMM_PURPOSE_SUB:
262 if (imm > -0x8000 && imm <= 0x8000)
265 case IMM_PURPOSE_CMP:
266 if (imm >= -0x8000 && imm < 0x8000)
268 /*if (cpu_test_feature(CPU_FEATURE_extended_imm)) {
269 if (imm >= -0x80000000LL && imm < 0x80000000LL)
273 case IMM_PURPOSE_CMP_LOGICAL:
274 /*if (cpu_test_feature(CPU_FEATURE_extended_imm)) {
275 if (imm >= 0LL && imm < 0x100000000LL)
279 case IMM_PURPOSE_AND:
284 if (!(imm & ~0xffffULL))
286 if (!(imm & ~0xffff0000ULL))
288 if (!(imm & ~0xffff00000000ULL))
290 if (!(imm & ~0xffff000000000000ULL))
294 case IMM_PURPOSE_XOR:
295 if (cpu_test_feature(CPU_FEATURE_extended_imm)) {
296 if (!(imm & ~0xffffffffULL))
298 if (!(imm & ~0xffffffff00000000ULL))
302 case IMM_PURPOSE_TEST:
303 if (!(imm & ~0xffffULL))
305 if (!(imm & ~0xffff0000ULL))
307 if (!(imm & ~0xffff00000000ULL))
309 if (!(imm & ~0xffff000000000000ULL))
312 case IMM_PURPOSE_STORE_VALUE:
313 if (size == OP_SIZE_1)
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);
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);
332 gen_eight(FRAME_FP_REGS);
335 gen_insn(INSN_MOV, OP_SIZE_8, 0, 0);
336 gen_one(ARG_ADDRESS_1);
338 gen_eight(FRAME_FP_REGS + 8);
341 gen_insn(INSN_ALU, OP_SIZE_NATIVE, ALU_ADD, ALU_WRITES_FLAGS(OP_SIZE_NATIVE, ALU_ADD, false, true, -FRAME_SIZE));
345 gen_eight(-FRAME_SIZE);
347 gen_insn(INSN_MOV, OP_SIZE_ADDRESS, 0, 0);
348 gen_one(ARG_ADDRESS_1);
350 gen_eight(FRAME_RETPTR);
353 gen_insn(INSN_MOV, OP_SIZE_NATIVE, 0, 0);
357 gen_insn(INSN_MOV, OP_SIZE_NATIVE, 0, 0);
361 gen_insn(INSN_MOV, OP_SIZE_4, 0, 0);
362 gen_one(ARG_ADDRESS_1);
364 gen_eight(FRAME_TIMESTAMP);
367 gen_insn(INSN_JMP_INDIRECT, 0, 0, 0);
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);
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);
388 gen_one(ARG_ADDRESS_1);
390 gen_eight(FRAME_SIZE + FRAME_FP_REGS);
392 gen_insn(INSN_MOV, OP_SIZE_8, 0, 0);
394 gen_one(ARG_ADDRESS_1);
396 gen_eight(FRAME_SIZE + FRAME_FP_REGS + 8);
398 gen_insn(INSN_MOV, OP_SIZE_ADDRESS, 0, 0);
400 gen_one(ARG_ADDRESS_1);
402 gen_eight(FRAME_RETPTR);
404 gen_insn(INSN_STP, OP_SIZE_ADDRESS, 0, 0);
405 gen_one(ARG_ADDRESS_1);
411 gen_insn(INSN_S390_POP, OP_SIZE_ADDRESS, 0, 0);
412 gen_one(ARG_ADDRESS_1);
414 gen_eight(FRAME_SIZE + FRAME_REGS);
416 gen_insn(INSN_RET, 0, 0, 0);
420 static bool attr_w gen_upcall_argument(struct codegen_context attr_unused *ctx, unsigned attr_unused arg)
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);
430 gen_address_offset();
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));
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);
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);