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(alu, im) 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_HAS_FP_GP_MOV 1
53 #define ARCH_NEEDS_BARRIER 0
55 #define i_size(size) OP_SIZE_4
56 #define i_size_rot(size) OP_SIZE_4
57 #define i_size_cmp(size) OP_SIZE_4
146 #define R_SCRATCH_1 R_0
147 #define R_SCRATCH_2 R_1
148 #define R_SCRATCH_3 R_2
149 #define R_SCRATCH_4 R_SAVED_2
151 #define R_SAVED_1 R_6
152 #define R_SAVED_2 R_7
154 #define R_OFFSET_IMM R_LR
155 #define R_CONST_IMM R_IP
157 #define R_SCRATCH_NA_1 R_10
158 #define R_SCRATCH_NA_2 R_FP
159 #ifdef HAVE_BITWISE_FRAME
160 #define R_SCRATCH_NA_3 R_8
169 #define FR_SCRATCH_1 FDR_0
170 #define FR_SCRATCH_2 FDR_2
172 #define SUPPORTED_FP (cpu_test_feature(CPU_FEATURE_vfp) * 0x6)
173 #define SUPPORTED_FP_HALF_CVT (cpu_test_feature(CPU_FEATURE_half) * 0x1)
175 static bool reg_is_fp(unsigned reg)
177 return reg >= 0x20 && reg < 0x40;
180 static const uint8_t regs_saved[] = {
181 #ifndef HAVE_BITWISE_FRAME
185 static const uint8_t regs_volatile[] = { R_3 };
186 static const uint8_t fp_saved[] = { 0 };
187 #define n_fp_saved 0U
188 static const uint8_t fp_volatile[] = { FDR_4, FDR_6, FDR_8, FDR_10, FDR_12, FDR_14 };
189 #define reg_is_saved(r) ((r) == R_8 || (r) == R_9)
191 static int gen_imm12(uint32_t c)
194 for (rot = 0; rot < 32; rot += 2) {
195 uint32_t val = c << rot | c >> (-rot & 31);
197 return val | (rot << 7);
202 static bool attr_w gen_load_constant(struct codegen_context *ctx, unsigned reg, uint32_t c)
204 if (gen_imm12(c) >= 0 || gen_imm12(~c) >= 0) {
205 gen_insn(INSN_MOV, OP_SIZE_4, 0, 0);
211 if (likely(cpu_test_feature(CPU_FEATURE_armv6t2))) {
212 gen_insn(INSN_MOV, OP_SIZE_4, 0, 0);
215 gen_eight(c & 0xffff);
217 gen_insn(INSN_MOV_MASK, OP_SIZE_4, MOV_MASK_16_32, 0);
224 bool need_init = true;
226 for (p = 0; p < 32; p += 8) {
227 if ((c >> p) & 0xff) {
229 gen_insn(INSN_MOV, OP_SIZE_4, 0, 0);
232 gen_eight(c & (0xff << p));
235 gen_insn(INSN_ALU, OP_SIZE_4, ALU_OR, 0);
239 gen_eight(c & (0xff << p));
244 gen_insn(INSN_MOV, OP_SIZE_4, 0, 0);
253 static bool attr_w gen_address(struct codegen_context *ctx, unsigned base, int64_t imm, unsigned purpose, unsigned size)
255 ctx->base_reg = base;
256 ctx->offset_imm = imm;
257 ctx->offset_reg = false;
259 case IMM_PURPOSE_LDR_OFFSET:
260 case IMM_PURPOSE_STR_OFFSET:
261 case IMM_PURPOSE_MVI_CLI_OFFSET:
262 if (size == OP_SIZE_2) {
263 if (imm >= -255 && imm <= 255)
266 if (imm >= -4095 && imm <= 4095)
270 case IMM_PURPOSE_LDR_SX_OFFSET:
271 case IMM_PURPOSE_LDP_STP_OFFSET:
272 if (imm >= -255 && imm <= 255)
275 case IMM_PURPOSE_VLDR_VSTR_OFFSET:
276 if (size < OP_SIZE_4 && imm != 0)
278 if (unlikely((imm & 3) != 0))
280 if (imm >= -1023 && imm <= 1023)
284 internal(file_line, "gen_address: invalid purpose %d", purpose);
286 if (purpose == IMM_PURPOSE_VLDR_VSTR_OFFSET) {
287 if (gen_imm12(imm) >= 0) {
288 gen_insn(INSN_ALU, OP_SIZE_ADDRESS, ALU_ADD, 0);
289 gen_one(R_OFFSET_IMM);
294 g(gen_load_constant(ctx, R_OFFSET_IMM, imm));
295 gen_insn(INSN_ALU, OP_SIZE_ADDRESS, ALU_ADD, 0);
296 gen_one(R_OFFSET_IMM);
297 gen_one(R_OFFSET_IMM);
300 ctx->base_reg = R_OFFSET_IMM;
304 g(gen_load_constant(ctx, R_OFFSET_IMM, imm));
305 ctx->offset_reg = true;
309 static bool is_direct_const(int64_t imm, unsigned purpose, unsigned size)
313 case IMM_PURPOSE_STORE_VALUE:
315 case IMM_PURPOSE_ADD:
316 case IMM_PURPOSE_SUB:
317 case IMM_PURPOSE_CMP:
318 case IMM_PURPOSE_CMP_LOGICAL:
319 case IMM_PURPOSE_AND:
321 case IMM_PURPOSE_XOR:
322 case IMM_PURPOSE_ANDN:
323 case IMM_PURPOSE_TEST:
324 imm12 = gen_imm12(imm);
325 if (unlikely(imm12 == -1))
328 case IMM_PURPOSE_CMOV:
329 if (gen_imm12(imm) >= 0 || gen_imm12(~imm) >= 0)
331 if ((uint32_t)imm < 0x10000 && likely(cpu_test_feature(CPU_FEATURE_armv6t2)))
334 case IMM_PURPOSE_MUL:
337 internal(file_line, "is_direct_const: invalid purpose %u (imm %"PRIxMAX", size %u)", purpose, (uintmax_t)imm, size);
342 static bool attr_w gen_imm(struct codegen_context *ctx, int64_t imm, unsigned purpose, unsigned size)
344 if (is_direct_const(imm, purpose, size)) {
345 ctx->const_imm = imm;
346 ctx->const_reg = false;
348 g(gen_load_constant(ctx, R_CONST_IMM, imm));
349 ctx->const_reg = true;
354 static bool attr_w gen_entry(struct codegen_context *ctx)
356 gen_insn(INSN_ARM_PUSH, OP_SIZE_NATIVE, 0, 0);
358 gen_insn(INSN_MOV, OP_SIZE_NATIVE, 0, 0);
362 gen_insn(INSN_MOV, OP_SIZE_NATIVE, 0, 0);
366 gen_insn(INSN_JMP_INDIRECT, 0, 0, 0);
372 static bool attr_w gen_escape_arg(struct codegen_context *ctx, ip_t ip, uint32_t escape_label)
374 g(gen_load_constant(ctx, R_ARG1, ip));
376 gen_insn(INSN_JMP, 0, 0, 0);
377 gen_four(escape_label);
382 static bool attr_w gen_escape(struct codegen_context *ctx)
384 gen_insn(INSN_MOV, OP_SIZE_NATIVE, 0, 0);
388 gen_insn(INSN_ARM_POP, OP_SIZE_NATIVE, 0, 0);
393 static bool attr_w gen_upcall_argument(struct codegen_context attr_unused *ctx, unsigned attr_unused arg)
395 if (unlikely(arg >= 4))
396 internal(file_line, "gen_upcall_argument: only 4 arguments supported");
400 static bool attr_w gen_upcall(struct codegen_context *ctx, unsigned offset, unsigned n_args)
402 g(gen_address(ctx, R_UPCALL, offset, IMM_PURPOSE_LDR_OFFSET, OP_SIZE_4));
403 gen_insn(INSN_MOV, OP_SIZE_4, 0, 0);
404 gen_one(R_SCRATCH_NA_1);
405 gen_address_offset();
407 gen_insn(INSN_CALL_INDIRECT, OP_SIZE_4, 0, 0);
408 gen_one(R_SCRATCH_NA_1);
410 g(gen_upcall_end(ctx, n_args));
415 static bool attr_w gen_timestamp_test(struct codegen_context *ctx, uint32_t escape_label)
417 g(gen_address(ctx, R_UPCALL, offsetof(struct cg_upcall_vector_s, ts), IMM_PURPOSE_LDR_OFFSET, OP_SIZE_4));
418 gen_insn(INSN_MOV, OP_SIZE_4, 0, 0);
419 gen_one(R_SCRATCH_1);
420 gen_address_offset();
422 gen_insn(INSN_MOV, OP_SIZE_4, 0, 0);
423 gen_one(R_SCRATCH_2);
424 gen_one(ARG_ADDRESS_1);
428 gen_insn(INSN_CMP, OP_SIZE_4, 0, 1);
429 gen_one(R_SCRATCH_1);
430 gen_one(R_SCRATCH_2);
432 gen_insn(INSN_JMP_COND, OP_SIZE_4, COND_NE, 0);
433 gen_four(escape_label);