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) 0
33 #define COND_IS_LOGICAL(cond) 0
35 #define ARCH_PARTIAL_ALU(size) 0
36 #define ARCH_IS_3ADDRESS 1
37 #define ARCH_HAS_FLAGS 1
38 #define ARCH_PREFERS_SX(size) 0
39 #define ARCH_HAS_BWX 1
40 #define ARCH_HAS_MUL 1
41 #define ARCH_HAS_DIV cpu_test_feature(CPU_FEATURE_idiv)
42 #define ARCH_HAS_ANDN 1
43 #define ARCH_HAS_SHIFTED_ADD(bits) 1
44 #define ARCH_HAS_BTX(btx, size, cnst) 0
45 #define ARCH_SHIFT_SIZE 32
46 #define ARCH_NEEDS_BARRIER 0
48 #define i_size(size) OP_SIZE_4
49 #define i_size_rot(size) OP_SIZE_4
138 #define R_SCRATCH_1 R_0
139 #define R_SCRATCH_2 R_1
140 #define R_SCRATCH_3 R_2
141 #define R_SCRATCH_4 R_3
143 #define R_SAVED_1 R_6
144 #define R_SAVED_2 R_7
146 #define R_OFFSET_IMM R_8
147 #define R_CONST_IMM R_IP
149 #define R_SCRATCH_NA_1 R_10
150 #define R_SCRATCH_NA_2 R_FP
151 #define R_SCRATCH_NA_3 R_LR
159 #define FR_SCRATCH_1 FQR_0
160 #define FR_SCRATCH_2 FQR_4
162 #define SUPPORTED_FP (cpu_test_feature(CPU_FEATURE_vfp) * 0x6)
163 #define SUPPORTED_FP_HALF_CVT (cpu_test_feature(CPU_FEATURE_half) * 0x1)
165 static bool reg_is_fp(unsigned reg)
167 return reg >= 0x20 && reg < 0x40;
170 static const uint8_t reg_available[] = { 0 };
171 #define reg_is_saved(r) 1
173 static int gen_imm12(uint32_t c)
176 for (rot = 0; rot < 32; rot += 2) {
177 uint32_t val = c << rot | c >> (-rot & 31);
179 return val | (rot << 7);
184 static bool attr_w gen_load_constant(struct codegen_context *ctx, unsigned reg, uint32_t c)
186 if (gen_imm12(c) >= 0 || gen_imm12(~c) >= 0) {
187 gen_insn(INSN_MOV, OP_SIZE_4, 0, 0);
193 if (likely(cpu_test_feature(CPU_FEATURE_armv6t2))) {
194 gen_insn(INSN_MOV, OP_SIZE_4, 0, 0);
197 gen_eight(c & 0xffff);
199 gen_insn(INSN_MOV_MASK, OP_SIZE_4, MOV_MASK_16_32, 0);
206 bool need_init = true;
208 for (p = 0; p < 32; p += 8) {
209 if ((c >> p) & 0xff) {
211 gen_insn(INSN_MOV, OP_SIZE_4, 0, 0);
214 gen_eight(c & (0xff << p));
217 gen_insn(INSN_ALU, OP_SIZE_4, ALU_OR, 0);
221 gen_eight(c & (0xff << p));
226 gen_insn(INSN_MOV, OP_SIZE_4, 0, 0);
235 static bool attr_w gen_address(struct codegen_context *ctx, unsigned base, int64_t imm, unsigned purpose, unsigned size)
237 ctx->base_reg = base;
238 ctx->offset_imm = imm;
239 ctx->offset_reg = false;
241 case IMM_PURPOSE_LDR_OFFSET:
242 case IMM_PURPOSE_STR_OFFSET:
243 case IMM_PURPOSE_MVI_CLI_OFFSET:
244 if (size == OP_SIZE_2) {
245 if (imm >= -255 && imm <= 255)
248 if (imm >= -4095 && imm <= 4095)
252 case IMM_PURPOSE_LDR_SX_OFFSET:
253 case IMM_PURPOSE_LDP_STP_OFFSET:
254 if (imm >= -255 && imm <= 255)
257 case IMM_PURPOSE_VLDR_VSTR_OFFSET:
258 if (size < OP_SIZE_4 && imm != 0)
260 if (unlikely((imm & 3) != 0))
262 if (imm >= -1023 && imm <= 1023)
266 internal(file_line, "gen_address: invalid purpose %d", purpose);
268 if (purpose == IMM_PURPOSE_VLDR_VSTR_OFFSET) {
269 if (gen_imm12(imm) >= 0) {
270 gen_insn(INSN_ALU, OP_SIZE_ADDRESS, ALU_ADD, 0);
271 gen_one(R_OFFSET_IMM);
276 g(gen_load_constant(ctx, R_OFFSET_IMM, imm));
277 gen_insn(INSN_ALU, OP_SIZE_ADDRESS, ALU_ADD, 0);
278 gen_one(R_OFFSET_IMM);
279 gen_one(R_OFFSET_IMM);
282 ctx->base_reg = R_OFFSET_IMM;
286 g(gen_load_constant(ctx, R_OFFSET_IMM, imm));
287 ctx->offset_reg = true;
291 static bool is_direct_const(int64_t imm, unsigned purpose, unsigned size)
295 case IMM_PURPOSE_STORE_VALUE:
297 case IMM_PURPOSE_ADD:
298 case IMM_PURPOSE_SUB:
299 case IMM_PURPOSE_CMP:
300 case IMM_PURPOSE_CMP_LOGICAL:
301 case IMM_PURPOSE_AND:
303 case IMM_PURPOSE_XOR:
304 case IMM_PURPOSE_ANDN:
305 case IMM_PURPOSE_TEST:
306 imm12 = gen_imm12(imm);
307 if (unlikely(imm12 == -1))
310 case IMM_PURPOSE_CMOV:
311 if (gen_imm12(imm) >= 0 || gen_imm12(~imm) >= 0)
313 if ((uint32_t)imm < 0x10000 && likely(cpu_test_feature(CPU_FEATURE_armv6t2)))
316 case IMM_PURPOSE_MUL:
319 internal(file_line, "is_direct_const: invalid purpose %u (imm %"PRIxMAX", size %u)", purpose, (uintmax_t)imm, size);
324 static bool attr_w gen_imm(struct codegen_context *ctx, int64_t imm, unsigned purpose, unsigned size)
326 if (is_direct_const(imm, purpose, size)) {
327 ctx->const_imm = imm;
328 ctx->const_reg = false;
330 g(gen_load_constant(ctx, R_CONST_IMM, imm));
331 ctx->const_reg = true;
336 static bool attr_w gen_entry(struct codegen_context *ctx)
338 gen_insn(INSN_ARM_PUSH, OP_SIZE_NATIVE, 0, 0);
340 gen_insn(INSN_MOV, OP_SIZE_NATIVE, 0, 0);
344 gen_insn(INSN_MOV, OP_SIZE_NATIVE, 0, 0);
348 gen_insn(INSN_JMP_INDIRECT, 0, 0, 0);
354 static bool attr_w gen_escape_arg(struct codegen_context *ctx, ip_t ip, uint32_t escape_label)
356 g(gen_load_constant(ctx, R_ARG1, ip));
358 gen_insn(INSN_JMP, 0, 0, 0);
359 gen_four(escape_label);
364 static bool attr_w gen_escape(struct codegen_context *ctx)
366 gen_insn(INSN_MOV, OP_SIZE_NATIVE, 0, 0);
370 gen_insn(INSN_ARM_POP, OP_SIZE_NATIVE, 0, 0);
375 static bool attr_w gen_upcall_argument(struct codegen_context attr_unused *ctx, unsigned attr_unused arg)
377 if (unlikely(arg >= 4))
378 internal(file_line, "gen_upcall_argument: only 4 arguments supported");
382 static bool attr_w gen_upcall(struct codegen_context *ctx, unsigned offset, unsigned n_args)
384 g(gen_address(ctx, R_UPCALL, offset, IMM_PURPOSE_LDR_OFFSET, OP_SIZE_4));
385 gen_insn(INSN_MOV, OP_SIZE_4, 0, 0);
386 gen_one(R_SCRATCH_NA_1);
387 gen_address_offset();
389 gen_insn(INSN_CALL_INDIRECT, OP_SIZE_4, 0, 0);
390 gen_one(R_SCRATCH_NA_1);
392 g(gen_upcall_end(ctx, n_args));
397 static bool attr_w gen_timestamp_test(struct codegen_context *ctx, uint32_t escape_label)
399 g(gen_address(ctx, R_UPCALL, offsetof(struct cg_upcall_vector_s, ts), IMM_PURPOSE_LDR_OFFSET, OP_SIZE_4));
400 gen_insn(INSN_MOV, OP_SIZE_4, 0, 0);
401 gen_one(R_SCRATCH_1);
402 gen_address_offset();
404 gen_insn(INSN_MOV, OP_SIZE_4, 0, 0);
405 gen_one(R_SCRATCH_2);
406 gen_one(ARG_ADDRESS_1);
410 gen_insn(INSN_CMP, OP_SIZE_4, 0, 1);
411 gen_one(R_SCRATCH_1);
412 gen_one(R_SCRATCH_2);
414 gen_insn(INSN_JMP_COND, OP_SIZE_4, COND_NE, 0);
415 gen_four(escape_label);