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/>.
19 #define OP_SIZE_NATIVE OP_SIZE_8
20 #define OP_SIZE_ADDRESS OP_SIZE_NATIVE
22 #define JMP_LIMIT JMP_LONG
24 #define UNALIGNED_TRAP (!cpu_test_feature(CPU_FEATURE_unaligned))
26 #define ALU_WRITES_FLAGS(alu, im) 0
27 #define ALU1_WRITES_FLAGS(alu) 0
28 #define ROT_WRITES_FLAGS(alu, size, im) 0
29 #define COND_IS_LOGICAL(cond) 0
31 #define ARCH_PARTIAL_ALU(size) 0
32 #define ARCH_IS_3ADDRESS(alu, f) 1
33 #define ARCH_IS_3ADDRESS_IMM(alu, f) 1
34 #define ARCH_IS_3ADDRESS_ROT(alu, size) 1
35 #define ARCH_IS_3ADDRESS_ROT_IMM(alu) 1
36 #define ARCH_IS_2ADDRESS(alu) 1
37 #define ARCH_IS_3ADDRESS_FP 1
38 #define ARCH_HAS_FLAGS 0
39 #define ARCH_PREFERS_SX(size) 0
40 #define ARCH_HAS_BWX 1
41 #define ARCH_HAS_MUL 1
42 #define ARCH_HAS_DIV 1
43 #define ARCH_HAS_ANDN 1
44 #define ARCH_HAS_SHIFTED_ADD(bits) 0
45 #define ARCH_HAS_BTX(btx, size, cnst) (((btx) == BTX_BTR || (btx) == BTX_BTEXT) && (cnst))
46 #define ARCH_SHIFT_SIZE OP_SIZE_4
47 #define ARCH_NEEDS_BARRIER 0
49 #define i_size(size) OP_SIZE_NATIVE
50 #define i_size_rot(size) maximum(size, OP_SIZE_4)
51 #define i_size_cmp(size) OP_SIZE_NATIVE
53 /*#define TIMESTAMP_IN_REGISTER*/
76 #define R_RESERVED 0x15
122 #define R_UPCALL R_S1
123 #ifdef TIMESTAMP_IN_REGISTER
124 #define R_TIMESTAMP R_S4
127 #define R_SCRATCH_1 R_A0
128 #define R_SCRATCH_2 R_A1
129 #define R_SCRATCH_3 R_A2
130 #define R_SCRATCH_4 R_SAVED_2
131 #define R_SCRATCH_NA_1 R_A4
132 #define R_SCRATCH_NA_2 R_A5
133 #define R_SCRATCH_NA_3 R_A6
135 #define R_SAVED_1 R_S2
136 #define R_SAVED_2 R_S3
145 #define R_OFFSET_IMM R_T0
146 #define R_CONST_IMM R_T1
147 #define R_CMP_RESULT R_T2
149 #define FR_SCRATCH_1 R_FA0
150 #define FR_SCRATCH_2 R_FA1
152 #define SUPPORTED_FP 0x6
154 #define FRAME_SIZE 0x60
156 static bool reg_is_fp(unsigned reg)
158 return reg >= 0x20 && reg < 0x40;
161 static const uint8_t regs_saved[] = {
162 #ifndef TIMESTAMP_IN_REGISTER
165 R_S5, R_S6, R_S7, R_S8, R_FP };
166 static const uint8_t regs_volatile[] = { R_RA, R_A3, R_A7, R_T3, R_T4, R_T5, R_T6, R_T7, R_T8 };
167 static const uint8_t fp_saved[] = { 0 };
168 #define n_fp_saved 0U
169 static const uint8_t fp_volatile[] = { R_FA2, R_FA3, R_FA4, R_FA5, R_FA6, R_FA7, R_FT0, R_FT1, R_FT2, R_FT3, R_FT4, R_FT5, R_FT6, R_FT7, R_FT8, R_FT9, R_FT10, R_FT11, R_FT12, R_FT13, R_FT14, R_FT15 };
170 #define reg_is_saved(r) ((r) >= R_FP && (r) <= R_S8)
172 static bool attr_w gen_load_constant(struct codegen_context *ctx, unsigned reg, uint64_t c)
174 uint64_t c0 = c & 0x0000000000000fffULL;
175 uint64_t c1 = c & 0x00000000fffff000ULL;
176 uint64_t c2 = c & 0x000fffff00000000ULL;
177 uint64_t c3 = c & 0xfff0000000000000ULL;
178 uint64_t top_bits = 0;
179 if (!(c0 | c1 | c2)) {
180 gen_insn(INSN_MOV, OP_SIZE_NATIVE, 0, 0);
186 if (c0 & 0x800ULL && c1 == 0xfffff000ULL) {
187 gen_insn(INSN_MOV, OP_SIZE_NATIVE, 0, 0);
190 gen_eight(c0 | 0xfffffffffffff000ULL);
191 top_bits = 0xffffffff00000000ULL;
193 bool have_reg = false;
195 gen_insn(INSN_MOV, OP_SIZE_NATIVE, 0, 0);
198 gen_eight((uint64_t)(int32_t)c1);
199 top_bits = (uint64_t)(int32_t)c1 & 0xffffffff00000000ULL;
202 if (!have_reg || c0) {
204 gen_insn(INSN_MOV, OP_SIZE_NATIVE, 0, 0);
209 gen_insn(INSN_ALU, OP_SIZE_NATIVE, ALU_OR, 0);
217 if (top_bits != (c2 | c3)) {
219 if (c2 & 0x0008000000000000ULL)
220 c2x |= 0xfff0000000000000ULL;
221 if (top_bits != c2x) {
222 gen_insn(INSN_MOV_MASK, OP_SIZE_NATIVE, MOV_MASK_32_64, 0);
226 gen_eight(c2x >> 32);
228 top_bits = c2x & 0xfff0000000000000ULL;
229 if (top_bits != c3) {
230 gen_insn(INSN_MOV_MASK, OP_SIZE_NATIVE, MOV_MASK_52_64, 0);
240 static bool attr_w gen_address(struct codegen_context *ctx, unsigned base, int64_t imm, unsigned purpose, unsigned size)
242 ctx->base_reg = base;
243 ctx->offset_imm = imm;
244 ctx->offset_reg = false;
246 case IMM_PURPOSE_LDR_OFFSET:
247 case IMM_PURPOSE_LDR_SX_OFFSET:
248 case IMM_PURPOSE_STR_OFFSET:
249 case IMM_PURPOSE_VLDR_VSTR_OFFSET:
250 case IMM_PURPOSE_MVI_CLI_OFFSET:
251 if (likely(imm >= -0x800) && likely(imm < 0x800)) {
254 if (imm >= -0x8000 && imm < 0x8000 && !(imm & 3)) {
255 if (size == OP_SIZE_NATIVE)
257 if (purpose == IMM_PURPOSE_LDR_SX_OFFSET && size == OP_SIZE_4)
259 if (purpose == IMM_PURPOSE_STR_OFFSET && size == OP_SIZE_4)
264 internal(file_line, "gen_address: invalid purpose %u (imm %"PRIxMAX", size %u)", purpose, (uintmax_t)imm, size);
266 g(gen_load_constant(ctx, R_OFFSET_IMM, imm));
267 ctx->offset_reg = true;
271 static bool is_direct_const(int64_t imm, unsigned purpose, unsigned size)
274 case IMM_PURPOSE_STORE_VALUE:
278 case IMM_PURPOSE_ADD:
279 case IMM_PURPOSE_CMP:
280 case IMM_PURPOSE_CMP_LOGICAL:
281 if (likely(imm >= -0x800) && likely(imm < 0x800))
284 case IMM_PURPOSE_SUB:
285 if (likely(imm > -0x800) && likely(imm <= 0x800))
288 case IMM_PURPOSE_AND:
290 case IMM_PURPOSE_XOR:
291 if (likely(imm >= 0) && likely(imm < 0x1000))
294 case IMM_PURPOSE_ANDN:
296 case IMM_PURPOSE_TEST:
298 case IMM_PURPOSE_JMP_2REGS:
300 case IMM_PURPOSE_MUL:
302 case IMM_PURPOSE_BITWISE:
305 internal(file_line, "is_direct_const: invalid purpose %u (imm %"PRIxMAX", size %u)", purpose, (uintmax_t)imm, size);
310 static bool attr_w gen_imm(struct codegen_context *ctx, int64_t imm, unsigned purpose, unsigned size)
312 if (is_direct_const(imm, purpose, size)) {
313 ctx->const_imm = imm;
314 ctx->const_reg = false;
316 g(gen_load_constant(ctx, R_CONST_IMM, imm));
317 ctx->const_reg = true;
322 static bool attr_w gen_entry(struct codegen_context *ctx)
326 g(gen_imm(ctx, -FRAME_SIZE, IMM_PURPOSE_ADD, OP_SIZE_NATIVE));
327 gen_insn(INSN_ALU, OP_SIZE_NATIVE, ALU_ADD, 0);
332 offset = FRAME_SIZE - (1 << OP_SIZE_NATIVE);
334 g(gen_address(ctx, R_SP, FRAME_SIZE - 0x08, IMM_PURPOSE_STR_OFFSET, OP_SIZE_NATIVE));
335 gen_insn(INSN_MOV, OP_SIZE_NATIVE, 0, 0);
336 gen_address_offset();
338 offset -= 1 << OP_SIZE_NATIVE;
340 for (i = R_FP; i <= R_S8; i++) {
341 g(gen_address(ctx, R_SP, offset, IMM_PURPOSE_STR_OFFSET, OP_SIZE_NATIVE));
342 gen_insn(INSN_MOV, OP_SIZE_NATIVE, 0, 0);
343 gen_address_offset();
345 offset -= 1 << OP_SIZE_NATIVE;
348 #ifndef TIMESTAMP_IN_REGISTER
349 g(gen_address(ctx, R_SP, offset, IMM_PURPOSE_STR_OFFSET, OP_SIZE_NATIVE));
350 gen_insn(INSN_MOV, OP_SIZE_4, 0, 0);
351 gen_address_offset();
355 gen_insn(INSN_MOV, OP_SIZE_NATIVE, 0, 0);
359 gen_insn(INSN_MOV, OP_SIZE_NATIVE, 0, 0);
363 #ifdef TIMESTAMP_IN_REGISTER
364 gen_insn(INSN_MOV, OP_SIZE_NATIVE, 0, 0);
365 gen_one(R_TIMESTAMP);
369 gen_insn(INSN_JMP_INDIRECT, 0, 0, 0);
375 static bool attr_w gen_escape_arg(struct codegen_context *ctx, ip_t ip, uint32_t escape_label)
377 g(gen_load_constant(ctx, R_RET1, ip));
379 gen_insn(INSN_JMP, 0, 0, 0);
380 gen_four(escape_label);
385 static bool attr_w gen_escape(struct codegen_context *ctx)
389 gen_insn(INSN_MOV, OP_SIZE_NATIVE, 0, 0);
393 offset = FRAME_SIZE - (1 << OP_SIZE_NATIVE);
395 g(gen_address(ctx, R_SP, FRAME_SIZE - 0x08, IMM_PURPOSE_STR_OFFSET, OP_SIZE_NATIVE));
396 gen_insn(INSN_MOV, OP_SIZE_NATIVE, 0, 0);
398 gen_address_offset();
399 offset -= 1 << OP_SIZE_NATIVE;
401 for (i = R_FP; i <= R_S8; i++) {
402 g(gen_address(ctx, R_SP, offset, IMM_PURPOSE_STR_OFFSET, OP_SIZE_NATIVE));
403 gen_insn(INSN_MOV, OP_SIZE_NATIVE, 0, 0);
405 gen_address_offset();
406 offset -= 1 << OP_SIZE_NATIVE;
409 g(gen_imm(ctx, FRAME_SIZE, IMM_PURPOSE_ADD, OP_SIZE_NATIVE));
410 gen_insn(INSN_ALU, OP_SIZE_NATIVE, ALU_ADD, 0);
415 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_upcall(struct codegen_context *ctx, unsigned offset, unsigned n_args)
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(R_SCRATCH_NA_1);
430 gen_address_offset();
432 gen_insn(INSN_CALL_INDIRECT, OP_SIZE_ADDRESS, 0, 0);
433 gen_one(R_SCRATCH_NA_1);
435 g(gen_upcall_end(ctx, n_args));
440 static bool attr_w gen_cmp_test_jmp(struct codegen_context *ctx, unsigned insn, unsigned op_size, unsigned reg1, unsigned reg2, unsigned cond, uint32_t label);
442 static bool attr_w gen_timestamp_test(struct codegen_context *ctx, uint32_t escape_label)
444 g(gen_address(ctx, R_UPCALL, offsetof(struct cg_upcall_vector_s, ts), IMM_PURPOSE_LDR_SX_OFFSET, OP_SIZE_4));
445 gen_insn(INSN_MOVSX, OP_SIZE_4, 0, 0);
446 gen_one(R_SCRATCH_1);
447 gen_address_offset();
449 #ifdef TIMESTAMP_IN_REGISTER
450 g(gen_cmp_test_jmp(ctx, INSN_CMP, OP_SIZE_NATIVE, R_SCRATCH_1, R_TIMESTAMP, COND_NE, escape_label));
452 g(gen_address(ctx, R_SP, 0, IMM_PURPOSE_LDR_SX_OFFSET, OP_SIZE_4));
453 gen_insn(INSN_MOVSX, OP_SIZE_4, 0, 0);
454 gen_one(R_SCRATCH_2);
455 gen_address_offset();
457 g(gen_cmp_test_jmp(ctx, INSN_CMP, OP_SIZE_NATIVE, R_SCRATCH_1, R_SCRATCH_2, COND_NE, escape_label));