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(size, alu, is_mem, is_imm, imm) 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_JMP_2REGS(cond) ((cond) == COND_E || (cond) == COND_NE)
39 #define ARCH_HAS_FLAGS 0
40 #define ARCH_PREFERS_SX(size) 0
41 #define ARCH_HAS_BWX 1
42 #define ARCH_HAS_MUL 1
43 #define ARCH_HAS_DIV 1
44 #define ARCH_HAS_ANDN 1
45 #define ARCH_HAS_SHIFTED_ADD(bits) 0
46 #define ARCH_HAS_BTX(btx, size, cnst) (((btx) == BTX_BTR || (btx) == BTX_BTEXT) && (cnst))
47 #define ARCH_SHIFT_SIZE OP_SIZE_4
48 #define ARCH_BOOL_SIZE OP_SIZE_NATIVE
49 #define ARCH_HAS_FP_GP_MOV 1
50 #define ARCH_NEEDS_BARRIER 0
52 #define i_size(size) OP_SIZE_NATIVE
53 #define i_size_rot(size) maximum(size, OP_SIZE_4)
54 #define i_size_cmp(size) OP_SIZE_NATIVE
56 /*#define TIMESTAMP_IN_REGISTER*/
79 #define R_RESERVED 0x15
125 #define R_UPCALL R_S1
126 #ifdef TIMESTAMP_IN_REGISTER
127 #define R_TIMESTAMP R_S4
130 #define R_SCRATCH_1 R_A0
131 #define R_SCRATCH_2 R_A1
132 #define R_SCRATCH_3 R_A2
133 #define R_SCRATCH_4 R_SAVED_2
134 #define R_SCRATCH_NA_1 R_A4
135 #define R_SCRATCH_NA_2 R_A5
136 #define R_SCRATCH_NA_3 R_A6
138 #define R_SAVED_1 R_S2
139 #define R_SAVED_2 R_S3
148 #define R_OFFSET_IMM R_T0
149 #define R_CONST_IMM R_T1
150 #define R_CMP_RESULT R_T2
152 #define FR_SCRATCH_1 R_FA0
153 #define FR_SCRATCH_2 R_FA1
155 #define SUPPORTED_FP 0x6
157 #define FRAME_SIZE 0x60
159 static bool reg_is_fp(unsigned reg)
161 return reg >= 0x20 && reg < 0x40;
164 static const uint8_t regs_saved[] = {
165 #ifndef TIMESTAMP_IN_REGISTER
168 R_S5, R_S6, R_S7, R_S8, R_FP };
169 static const uint8_t regs_volatile[] = { R_RA, R_A3, R_A7, R_T3, R_T4, R_T5, R_T6, R_T7, R_T8 };
170 static const uint8_t fp_saved[] = { 0 };
171 #define n_fp_saved 0U
172 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 };
173 #define reg_is_saved(r) ((r) >= R_FP && (r) <= R_S8)
175 static bool attr_w gen_load_constant(struct codegen_context *ctx, unsigned reg, uint64_t c)
177 uint64_t c0 = c & 0x0000000000000fffULL;
178 uint64_t c1 = c & 0x00000000fffff000ULL;
179 uint64_t c2 = c & 0x000fffff00000000ULL;
180 uint64_t c3 = c & 0xfff0000000000000ULL;
181 uint64_t top_bits = 0;
182 if (!(c0 | c1 | c2)) {
183 gen_insn(INSN_MOV, OP_SIZE_NATIVE, 0, 0);
189 if (c0 & 0x800ULL && c1 == 0xfffff000ULL) {
190 gen_insn(INSN_MOV, OP_SIZE_NATIVE, 0, 0);
193 gen_eight(c0 | 0xfffffffffffff000ULL);
194 top_bits = 0xffffffff00000000ULL;
196 bool have_reg = false;
198 gen_insn(INSN_MOV, OP_SIZE_NATIVE, 0, 0);
201 gen_eight((uint64_t)(int32_t)c1);
202 top_bits = (uint64_t)(int32_t)c1 & 0xffffffff00000000ULL;
205 if (!have_reg || c0) {
207 gen_insn(INSN_MOV, OP_SIZE_NATIVE, 0, 0);
212 gen_insn(INSN_ALU, OP_SIZE_NATIVE, ALU_OR, 0);
220 if (top_bits != (c2 | c3)) {
222 if (c2 & 0x0008000000000000ULL)
223 c2x |= 0xfff0000000000000ULL;
224 if (top_bits != c2x) {
225 gen_insn(INSN_MOV_MASK, OP_SIZE_NATIVE, MOV_MASK_32_64, 0);
229 gen_eight(c2x >> 32);
231 top_bits = c2x & 0xfff0000000000000ULL;
232 if (top_bits != c3) {
233 gen_insn(INSN_MOV_MASK, OP_SIZE_NATIVE, MOV_MASK_52_64, 0);
243 static bool attr_w gen_address(struct codegen_context *ctx, unsigned base, int64_t imm, unsigned purpose, unsigned size)
245 ctx->base_reg = base;
246 ctx->offset_imm = imm;
247 ctx->offset_reg = false;
249 case IMM_PURPOSE_LDR_OFFSET:
250 case IMM_PURPOSE_LDR_SX_OFFSET:
251 case IMM_PURPOSE_STR_OFFSET:
252 case IMM_PURPOSE_VLDR_VSTR_OFFSET:
253 case IMM_PURPOSE_MVI_CLI_OFFSET:
254 if (likely(imm >= -0x800) && likely(imm < 0x800)) {
257 if (imm >= -0x8000 && imm < 0x8000 && !(imm & 3)) {
258 if (size == OP_SIZE_NATIVE)
260 if (purpose == IMM_PURPOSE_LDR_SX_OFFSET && size == OP_SIZE_4)
262 if (purpose == IMM_PURPOSE_STR_OFFSET && size == OP_SIZE_4)
267 internal(file_line, "gen_address: invalid purpose %u (imm %"PRIxMAX", size %u)", purpose, (uintmax_t)imm, size);
269 g(gen_load_constant(ctx, R_OFFSET_IMM, imm));
270 ctx->offset_reg = true;
274 static bool is_direct_const(int64_t imm, unsigned purpose, unsigned size)
277 case IMM_PURPOSE_STORE_VALUE:
281 case IMM_PURPOSE_ADD:
282 case IMM_PURPOSE_CMP:
283 case IMM_PURPOSE_CMP_LOGICAL:
284 if (likely(imm >= -0x800) && likely(imm < 0x800))
287 case IMM_PURPOSE_SUB:
288 if (likely(imm > -0x800) && likely(imm <= 0x800))
291 case IMM_PURPOSE_AND:
293 case IMM_PURPOSE_XOR:
294 if (likely(imm >= 0) && likely(imm < 0x1000))
297 case IMM_PURPOSE_ANDN:
299 case IMM_PURPOSE_TEST:
301 case IMM_PURPOSE_JMP_2REGS:
303 case IMM_PURPOSE_MUL:
305 case IMM_PURPOSE_BITWISE:
308 internal(file_line, "is_direct_const: invalid purpose %u (imm %"PRIxMAX", size %u)", purpose, (uintmax_t)imm, size);
313 static bool attr_w gen_entry(struct codegen_context *ctx)
317 g(gen_imm(ctx, -FRAME_SIZE, IMM_PURPOSE_ADD, OP_SIZE_NATIVE));
318 gen_insn(INSN_ALU, OP_SIZE_NATIVE, ALU_ADD, 0);
323 offset = FRAME_SIZE - (1 << OP_SIZE_NATIVE);
325 g(gen_address(ctx, R_SP, FRAME_SIZE - 0x08, IMM_PURPOSE_STR_OFFSET, OP_SIZE_NATIVE));
326 gen_insn(INSN_MOV, OP_SIZE_NATIVE, 0, 0);
327 gen_address_offset();
329 offset -= 1 << OP_SIZE_NATIVE;
331 for (i = R_FP; i <= R_S8; i++) {
332 g(gen_address(ctx, R_SP, offset, IMM_PURPOSE_STR_OFFSET, OP_SIZE_NATIVE));
333 gen_insn(INSN_MOV, OP_SIZE_NATIVE, 0, 0);
334 gen_address_offset();
336 offset -= 1 << OP_SIZE_NATIVE;
339 #ifndef TIMESTAMP_IN_REGISTER
340 g(gen_address(ctx, R_SP, offset, IMM_PURPOSE_STR_OFFSET, OP_SIZE_NATIVE));
341 gen_insn(INSN_MOV, OP_SIZE_4, 0, 0);
342 gen_address_offset();
346 gen_insn(INSN_MOV, OP_SIZE_NATIVE, 0, 0);
350 gen_insn(INSN_MOV, OP_SIZE_NATIVE, 0, 0);
354 #ifdef TIMESTAMP_IN_REGISTER
355 gen_insn(INSN_MOV, OP_SIZE_NATIVE, 0, 0);
356 gen_one(R_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_RET1, 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)
380 gen_insn(INSN_MOV, OP_SIZE_NATIVE, 0, 0);
384 offset = FRAME_SIZE - (1 << OP_SIZE_NATIVE);
386 g(gen_address(ctx, R_SP, FRAME_SIZE - 0x08, IMM_PURPOSE_STR_OFFSET, OP_SIZE_NATIVE));
387 gen_insn(INSN_MOV, OP_SIZE_NATIVE, 0, 0);
389 gen_address_offset();
390 offset -= 1 << OP_SIZE_NATIVE;
392 for (i = R_FP; i <= R_S8; i++) {
393 g(gen_address(ctx, R_SP, offset, IMM_PURPOSE_STR_OFFSET, OP_SIZE_NATIVE));
394 gen_insn(INSN_MOV, OP_SIZE_NATIVE, 0, 0);
396 gen_address_offset();
397 offset -= 1 << OP_SIZE_NATIVE;
400 g(gen_imm(ctx, FRAME_SIZE, IMM_PURPOSE_ADD, OP_SIZE_NATIVE));
401 gen_insn(INSN_ALU, OP_SIZE_NATIVE, ALU_ADD, 0);
406 gen_insn(INSN_RET, 0, 0, 0);
411 static bool attr_w gen_upcall_argument(struct codegen_context attr_unused *ctx, unsigned attr_unused arg)
416 static bool attr_w gen_get_upcall_pointer(struct codegen_context *ctx, unsigned offset, unsigned reg)
418 g(gen_address(ctx, R_UPCALL, offset, IMM_PURPOSE_LDR_OFFSET, OP_SIZE_ADDRESS));
419 gen_insn(INSN_MOV, OP_SIZE_ADDRESS, 0, 0);
421 gen_address_offset();
426 static bool attr_w gen_upcall(struct codegen_context *ctx, unsigned offset, unsigned n_args)
428 g(gen_get_upcall_pointer(ctx, offset, R_SCRATCH_NA_1));
430 gen_insn(INSN_CALL_INDIRECT, OP_SIZE_ADDRESS, 0, 0);
431 gen_one(R_SCRATCH_NA_1);
433 g(gen_upcall_end(ctx, offset, n_args));
438 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);
440 static bool attr_w gen_timestamp_test(struct codegen_context *ctx, uint32_t escape_label)
442 g(gen_address(ctx, R_UPCALL, offsetof(struct cg_upcall_vector_s, ts), IMM_PURPOSE_LDR_SX_OFFSET, OP_SIZE_4));
443 gen_insn(INSN_MOVSX, OP_SIZE_4, 0, 0);
444 gen_one(R_SCRATCH_1);
445 gen_address_offset();
447 #ifdef TIMESTAMP_IN_REGISTER
448 g(gen_cmp_test_jmp(ctx, INSN_CMP, OP_SIZE_NATIVE, R_SCRATCH_1, R_TIMESTAMP, COND_NE, escape_label));
450 g(gen_address(ctx, R_SP, 0, IMM_PURPOSE_LDR_SX_OFFSET, OP_SIZE_4));
451 gen_insn(INSN_MOVSX, OP_SIZE_4, 0, 0);
452 gen_one(R_SCRATCH_2);
453 gen_address_offset();
455 g(gen_cmp_test_jmp(ctx, INSN_CMP, OP_SIZE_NATIVE, R_SCRATCH_1, R_SCRATCH_2, COND_NE, escape_label));