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_4
20 #define OP_SIZE_ADDRESS OP_SIZE_4
22 #define JMP_LIMIT JMP_SHORTEST
24 #ifndef __ARM_FEATURE_UNALIGNED
25 #define UNALIGNED_TRAP 1
27 #define UNALIGNED_TRAP 0
30 #define ALU_WRITES_FLAGS(size, alu, is_mem, is_imm, imm) 0
31 #define ALU1_WRITES_FLAGS(alu) 0
32 #define ROT_WRITES_FLAGS(alu, size, im) 0
33 #define COND_IS_LOGICAL(cond) 0
35 #define ARCH_PARTIAL_ALU(size) 0
36 #define ARCH_IS_3ADDRESS(alu, f) 1
37 #define ARCH_IS_3ADDRESS_IMM(alu, f) 1
38 #define ARCH_IS_3ADDRESS_ROT(alu, size) 1
39 #define ARCH_IS_3ADDRESS_ROT_IMM(alu) 1
40 #define ARCH_IS_2ADDRESS(alu) 1
41 #define ARCH_IS_3ADDRESS_FP 1
42 #define ARCH_HAS_JMP_2REGS(cond) 0
43 #define ARCH_HAS_FLAGS 1
44 #define ARCH_PREFERS_SX(size) 0
45 #define ARCH_HAS_BWX 1
46 #define ARCH_HAS_MUL 1
47 #define ARCH_HAS_DIV cpu_test_feature(CPU_FEATURE_idiv)
48 #define ARCH_HAS_ANDN 1
49 #define ARCH_HAS_SHIFTED_ADD(bits) 1
50 #define ARCH_HAS_BTX(btx, size, cnst) 0
51 #define ARCH_SHIFT_SIZE 32
52 #define ARCH_BOOL_SIZE OP_SIZE_4
53 #define ARCH_HAS_FP_GP_MOV 1
54 #define ARCH_NEEDS_BARRIER 0
56 #define i_size(size) OP_SIZE_4
57 #define i_size_rot(size) OP_SIZE_4
58 #define i_size_cmp(size) OP_SIZE_4
147 #define R_SCRATCH_1 R_0
148 #define R_SCRATCH_2 R_1
149 #define R_SCRATCH_3 R_2
150 #define R_SCRATCH_4 R_SAVED_2
152 #define R_SAVED_1 R_6
153 #define R_SAVED_2 R_7
155 #define R_OFFSET_IMM R_LR
156 #define R_CONST_IMM R_IP
158 #define R_SCRATCH_NA_1 R_10
159 #define R_SCRATCH_NA_2 R_FP
160 #ifdef HAVE_BITWISE_FRAME
161 #define R_SCRATCH_NA_3 R_8
170 #define FR_SCRATCH_1 FDR_0
171 #define FR_SCRATCH_2 FDR_2
173 #define SUPPORTED_FP (cpu_test_feature(CPU_FEATURE_vfp) * 0x6)
174 #define SUPPORTED_FP_HALF_CVT (cpu_test_feature(CPU_FEATURE_half) * 0x1)
176 static bool reg_is_fp(unsigned reg)
178 return reg >= 0x20 && reg < 0x40;
181 static const uint8_t regs_saved[] = {
182 #ifndef HAVE_BITWISE_FRAME
186 static const uint8_t regs_volatile[] = { R_3 };
187 static const uint8_t fp_saved[] = { 0 };
188 #define n_fp_saved 0U
189 static const uint8_t fp_volatile[] = { FDR_4, FDR_6, FDR_8, FDR_10, FDR_12, FDR_14 };
190 #define reg_is_saved(r) ((r) == R_8 || (r) == R_9)
192 static int gen_imm12(uint32_t c)
195 for (rot = 0; rot < 32; rot += 2) {
196 uint32_t val = c << rot | c >> (-rot & 31);
198 return val | (rot << 7);
203 static bool attr_w gen_load_constant(struct codegen_context *ctx, unsigned reg, uint32_t c)
205 if (gen_imm12(c) >= 0 || gen_imm12(~c) >= 0) {
206 gen_insn(INSN_MOV, OP_SIZE_4, 0, 0);
212 if (likely(cpu_test_feature(CPU_FEATURE_armv6t2))) {
213 gen_insn(INSN_MOV, OP_SIZE_4, 0, 0);
216 gen_eight(c & 0xffff);
218 gen_insn(INSN_MOV_MASK, OP_SIZE_4, MOV_MASK_16_32, 0);
225 bool need_init = true;
227 for (p = 0; p < 32; p += 8) {
228 if ((c >> p) & 0xff) {
230 gen_insn(INSN_MOV, OP_SIZE_4, 0, 0);
233 gen_eight(c & (0xff << p));
236 gen_insn(INSN_ALU, OP_SIZE_4, ALU_OR, 0);
240 gen_eight(c & (0xff << p));
245 gen_insn(INSN_MOV, OP_SIZE_4, 0, 0);
254 static bool attr_w gen_address(struct codegen_context *ctx, unsigned base, int64_t imm, unsigned purpose, unsigned size)
256 ctx->base_reg = base;
257 ctx->offset_imm = imm;
258 ctx->offset_reg = false;
260 case IMM_PURPOSE_LDR_OFFSET:
261 case IMM_PURPOSE_STR_OFFSET:
262 case IMM_PURPOSE_MVI_CLI_OFFSET:
263 if (size == OP_SIZE_2) {
264 if (imm >= -255 && imm <= 255)
267 if (imm >= -4095 && imm <= 4095)
271 case IMM_PURPOSE_LDR_SX_OFFSET:
272 case IMM_PURPOSE_LDP_STP_OFFSET:
273 if (imm >= -255 && imm <= 255)
276 case IMM_PURPOSE_VLDR_VSTR_OFFSET:
277 if (size < OP_SIZE_4 && imm != 0)
279 if (unlikely((imm & 3) != 0))
281 if (imm >= -1023 && imm <= 1023)
285 internal(file_line, "gen_address: invalid purpose %d", purpose);
287 if (purpose == IMM_PURPOSE_VLDR_VSTR_OFFSET) {
288 if (gen_imm12(imm) >= 0) {
289 gen_insn(INSN_ALU, OP_SIZE_ADDRESS, ALU_ADD, 0);
290 gen_one(R_OFFSET_IMM);
295 g(gen_load_constant(ctx, R_OFFSET_IMM, imm));
296 gen_insn(INSN_ALU, OP_SIZE_ADDRESS, ALU_ADD, 0);
297 gen_one(R_OFFSET_IMM);
298 gen_one(R_OFFSET_IMM);
301 ctx->base_reg = R_OFFSET_IMM;
305 g(gen_load_constant(ctx, R_OFFSET_IMM, imm));
306 ctx->offset_reg = true;
310 static bool is_direct_const(int64_t imm, unsigned purpose, unsigned size)
314 case IMM_PURPOSE_STORE_VALUE:
316 case IMM_PURPOSE_ADD:
317 case IMM_PURPOSE_SUB:
318 case IMM_PURPOSE_CMP:
319 case IMM_PURPOSE_CMP_LOGICAL:
320 case IMM_PURPOSE_AND:
322 case IMM_PURPOSE_XOR:
323 case IMM_PURPOSE_ANDN:
324 case IMM_PURPOSE_TEST:
325 imm12 = gen_imm12(imm);
326 if (unlikely(imm12 == -1))
329 case IMM_PURPOSE_CMOV:
330 if (gen_imm12(imm) >= 0 || gen_imm12(~imm) >= 0)
332 if ((uint32_t)imm < 0x10000 && likely(cpu_test_feature(CPU_FEATURE_armv6t2)))
335 case IMM_PURPOSE_MUL:
338 internal(file_line, "is_direct_const: invalid purpose %u (imm %"PRIxMAX", size %u)", purpose, (uintmax_t)imm, size);
343 static bool attr_w gen_entry(struct codegen_context *ctx)
345 gen_insn(INSN_ARM_PUSH, OP_SIZE_NATIVE, 0, 0);
347 gen_insn(INSN_MOV, OP_SIZE_NATIVE, 0, 0);
351 gen_insn(INSN_MOV, OP_SIZE_NATIVE, 0, 0);
355 gen_insn(INSN_JMP_INDIRECT, 0, 0, 0);
361 static bool attr_w gen_escape_arg(struct codegen_context *ctx, ip_t ip, uint32_t escape_label)
363 g(gen_load_constant(ctx, R_ARG1, ip));
365 gen_insn(INSN_JMP, 0, 0, 0);
366 gen_four(escape_label);
371 static bool attr_w gen_escape(struct codegen_context *ctx)
373 gen_insn(INSN_MOV, OP_SIZE_NATIVE, 0, 0);
377 gen_insn(INSN_ARM_POP, OP_SIZE_NATIVE, 0, 0);
382 static bool attr_w gen_upcall_argument(struct codegen_context attr_unused *ctx, unsigned attr_unused arg)
384 if (unlikely(arg >= 4))
385 internal(file_line, "gen_upcall_argument: only 4 arguments supported");
389 static bool attr_w gen_get_upcall_pointer(struct codegen_context *ctx, unsigned offset, unsigned reg)
391 g(gen_address(ctx, R_UPCALL, offset, IMM_PURPOSE_LDR_OFFSET, OP_SIZE_4));
392 gen_insn(INSN_MOV, OP_SIZE_4, 0, 0);
394 gen_address_offset();
399 static bool attr_w gen_upcall(struct codegen_context *ctx, unsigned offset, unsigned n_args)
401 g(gen_get_upcall_pointer(ctx, offset, R_SCRATCH_NA_1));
403 gen_insn(INSN_CALL_INDIRECT, OP_SIZE_4, 0, 0);
404 gen_one(R_SCRATCH_NA_1);
406 g(gen_upcall_end(ctx, n_args));
411 static bool attr_w gen_timestamp_test(struct codegen_context *ctx, uint32_t escape_label)
413 g(gen_address(ctx, R_UPCALL, offsetof(struct cg_upcall_vector_s, ts), IMM_PURPOSE_LDR_OFFSET, OP_SIZE_4));
414 gen_insn(INSN_MOV, OP_SIZE_4, 0, 0);
415 gen_one(R_SCRATCH_1);
416 gen_address_offset();
418 gen_insn(INSN_MOV, OP_SIZE_4, 0, 0);
419 gen_one(R_SCRATCH_2);
420 gen_one(ARG_ADDRESS_1);
424 gen_insn(INSN_CMP, OP_SIZE_4, 0, 1);
425 gen_one(R_SCRATCH_1);
426 gen_one(R_SCRATCH_2);
428 gen_insn(INSN_JMP_COND, OP_SIZE_4, COND_NE, 0);
429 gen_four(escape_label);