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_8
22 #define JMP_LIMIT JMP_LONG
24 #define UNALIGNED_TRAP 0
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_JMP_2REGS(cond) 0
39 #define ARCH_HAS_FLAGS 1
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) 1
46 #define ARCH_HAS_BTX(btx, size, cnst) 0
47 #define ARCH_SHIFT_SIZE OP_SIZE_4
48 #define ARCH_BOOL_SIZE OP_SIZE_4
49 #define ARCH_HAS_FP_GP_MOV 1
50 #define ARCH_NEEDS_BARRIER 0
52 #define i_size(size) maximum(size, OP_SIZE_4)
53 #define i_size_rot(size) maximum(size, OP_SIZE_4)
54 #define i_size_cmp(size) maximum(size, OP_SIZE_4)
122 #define FRAME_SIZE 0x60
125 #define R_UPCALL R_27
126 #define R_TIMESTAMP R_26
127 #define R_SAVED_1 R_25
128 #define R_SAVED_2 R_24
129 #define R_SAVED_3 R_23
130 #define R_SAVED_4 R_22
131 #define R_SAVED_5 R_21
132 #define R_SAVED_6 R_20
133 #define R_SAVED_7 R_19
135 #define R_SCRATCH_1 R_0
136 #define R_SCRATCH_2 R_1
137 #define R_SCRATCH_3 R_2
138 #define R_SCRATCH_4 R_3
139 #define R_SCRATCH_NA_1 R_8
140 #define R_SCRATCH_NA_2 R_9
141 #ifdef HAVE_BITWISE_FRAME
142 #define R_SCRATCH_NA_3 R_10
144 #define R_OFFSET_IMM R_16
145 #define R_CONST_IMM R_17
154 #define FR_SCRATCH_1 FR_0
155 #define FR_SCRATCH_2 FR_1
157 #define SUPPORTED_FP 0x6
158 #define SUPPORTED_FP_HALF_CVT 0x1
160 static bool reg_is_fp(unsigned reg)
162 return reg >= 0x20 && reg < 0x40;
165 static const uint8_t regs_saved[] = { R_SAVED_7, R_SAVED_6, R_SAVED_5, R_SAVED_4, R_SAVED_3 };
166 static const uint8_t regs_volatile[] = { R_4, R_5, R_6, R_7,
167 #ifndef HAVE_BITWISE_FRAME
170 R_11, R_12, R_13, R_14, R_15, R_LR };
171 static const uint8_t fp_saved[] = { 0 };
172 #define n_fp_saved 0U
173 static const uint8_t fp_volatile[] = { FR_2, FR_3, FR_4, FR_5, FR_6, FR_7, FR_16, FR_17, FR_18, FR_19, FR_20, FR_21, FR_22, FR_23, FR_24, FR_25, FR_26, FR_27, FR_28, FR_29, FR_30, FR_31 };
174 #define reg_is_saved(r) ((r) >= R_19 && (r) <= R_FP)
181 static const struct logical_imm value_to_code_4_table[] = {
182 #include "arm64-w.inc"
185 static const struct logical_imm value_to_code_8_table[] = {
186 #include "arm64-x.inc"
189 static int16_t value_to_code(uint8_t size, uint64_t value)
192 if (size == OP_SIZE_4) {
193 binary_search(size_t, n_array_elements(value_to_code_4_table), result, value_to_code_4_table[result].value == value, value_to_code_4_table[result].value < value, return -1);
194 return value_to_code_4_table[result].code;
196 binary_search(size_t, n_array_elements(value_to_code_8_table), result, value_to_code_8_table[result].value == value, value_to_code_8_table[result].value < value, return -1);
197 return value_to_code_8_table[result].code;
201 static bool attr_w gen_load_constant(struct codegen_context *ctx, unsigned reg, uint64_t c)
208 code = value_to_code(OP_SIZE_4, c);
210 gen_insn(INSN_ALU, OP_SIZE_4, ALU_OR, 0);
218 code = value_to_code(OP_SIZE_8, c);
220 gen_insn(INSN_ALU, OP_SIZE_8, ALU_OR, 0);
229 if ((int64_t)c < 0) {
230 gen_insn(INSN_MOV, OP_SIZE_8, 0, 0);
233 gen_eight((c & 0xffff) | 0xffffffffffff0000ULL);
234 if ((c & 0xffff0000ULL) != 0xffff0000ULL) {
235 gen_insn(INSN_MOV_MASK, OP_SIZE_8, MOV_MASK_16_32, 0);
239 gen_eight((c >> 16) & 0xffff);
241 if ((c & 0xffff00000000ULL) != 0xffff00000000ULL) {
242 gen_insn(INSN_MOV_MASK, OP_SIZE_8, MOV_MASK_32_48, 0);
246 gen_eight((c >> 32) & 0xffff);
248 if ((c & 0xffff000000000000ULL) != 0xffff000000000000ULL) {
249 gen_insn(INSN_MOV_MASK, OP_SIZE_8, MOV_MASK_48_64, 0);
253 gen_eight((c >> 48) & 0xffff);
256 gen_insn(INSN_MOV, OP_SIZE_8, 0, 0);
259 gen_eight(c & 0xffff);
260 if (c & 0xffff0000ULL) {
261 gen_insn(INSN_MOV_MASK, OP_SIZE_8, MOV_MASK_16_32, 0);
265 gen_eight((c >> 16) & 0xffff);
267 if (c & 0xffff00000000ULL) {
268 gen_insn(INSN_MOV_MASK, OP_SIZE_8, MOV_MASK_32_48, 0);
272 gen_eight((c >> 32) & 0xffff);
274 if (c & 0xffff000000000000ULL) {
275 gen_insn(INSN_MOV_MASK, OP_SIZE_8, MOV_MASK_48_64, 0);
279 gen_eight((c >> 48) & 0xffff);
285 static bool attr_w gen_address(struct codegen_context *ctx, unsigned base, int64_t imm, unsigned purpose, unsigned size)
287 ctx->base_reg = base;
288 ctx->offset_imm = imm;
289 ctx->offset_reg = false;
291 case IMM_PURPOSE_LDR_OFFSET:
292 case IMM_PURPOSE_LDR_SX_OFFSET:
293 case IMM_PURPOSE_STR_OFFSET:
294 case IMM_PURPOSE_VLDR_VSTR_OFFSET:
295 case IMM_PURPOSE_MVI_CLI_OFFSET:
296 if (likely(imm >= -256) && likely(imm <= 255))
298 if (likely(imm >= 0)) {
299 if (unlikely((imm & ((1 << size) - 1)) != 0))
301 if (likely((imm >> size) <= 4095))
305 case IMM_PURPOSE_LDP_STP_OFFSET:
306 if (unlikely((imm & ((1 << size) - 1)) != 0))
308 if (imm / (1 << size) >= -64 && imm / (1 << size) <= 63)
312 internal(file_line, "gen_address: invalid purpose %d", purpose);
314 g(gen_load_constant(ctx, R_OFFSET_IMM, imm));
315 if (purpose == IMM_PURPOSE_LDP_STP_OFFSET) {
316 gen_insn(INSN_ALU, OP_SIZE_ADDRESS, ALU_ADD, 0);
317 gen_one(R_OFFSET_IMM);
318 gen_one(R_OFFSET_IMM);
320 ctx->base_reg = R_OFFSET_IMM;
324 ctx->offset_reg = true;
329 static bool is_direct_const(int64_t imm, unsigned purpose, unsigned size)
332 case IMM_PURPOSE_STORE_VALUE:
336 case IMM_PURPOSE_ADD:
337 case IMM_PURPOSE_SUB:
338 case IMM_PURPOSE_CMP:
339 case IMM_PURPOSE_CMP_LOGICAL:
340 if (imm >= 0 && imm < 4096)
343 case IMM_PURPOSE_AND:
345 case IMM_PURPOSE_XOR:
346 case IMM_PURPOSE_ANDN:
347 case IMM_PURPOSE_TEST:
348 if (value_to_code(size, imm) >= 0)
351 case IMM_PURPOSE_MUL:
354 internal(file_line, "is_direct_const: invalid purpose %u (imm %"PRIxMAX", size %u)", purpose, (uintmax_t)imm, size);
359 static bool attr_w gen_imm(struct codegen_context *ctx, int64_t imm, unsigned purpose, unsigned size)
361 if (is_direct_const(imm, purpose, size)) {
362 ctx->const_imm = imm;
363 ctx->const_reg = false;
365 g(gen_load_constant(ctx, R_CONST_IMM, imm));
366 ctx->const_reg = true;
371 static bool attr_w gen_entry(struct codegen_context *ctx)
373 gen_insn(INSN_STP, OP_SIZE_8, 0, 0);
374 gen_one(ARG_ADDRESS_1_PRE_I);
376 gen_eight(-FRAME_SIZE);
380 gen_insn(INSN_MOV, OP_SIZE_8, 0, 0);
384 gen_insn(INSN_STP, OP_SIZE_8, 0, 0);
385 gen_one(ARG_ADDRESS_1);
391 gen_insn(INSN_STP, OP_SIZE_8, 0, 0);
392 gen_one(ARG_ADDRESS_1);
396 gen_one(R_TIMESTAMP);
398 gen_insn(INSN_STP, OP_SIZE_8, 0, 0);
399 gen_one(ARG_ADDRESS_1);
405 gen_insn(INSN_STP, OP_SIZE_8, 0, 0);
406 gen_one(ARG_ADDRESS_1);
412 gen_insn(INSN_STP, OP_SIZE_8, 0, 0);
413 gen_one(ARG_ADDRESS_1);
419 gen_insn(INSN_MOV, OP_SIZE_8, 0, 0);
423 gen_insn(INSN_MOV, OP_SIZE_8, 0, 0);
427 gen_insn(INSN_MOV, OP_SIZE_8, 0, 0);
428 gen_one(R_TIMESTAMP);
431 gen_insn(INSN_JMP_INDIRECT, 0, 0, 0);
437 static bool attr_w gen_escape_arg(struct codegen_context *ctx, ip_t ip, uint32_t escape_label)
439 g(gen_load_constant(ctx, R_RET1, ip));
441 gen_insn(INSN_JMP, 0, 0, 0);
442 gen_four(escape_label);
447 static bool attr_w gen_escape(struct codegen_context *ctx)
449 gen_insn(INSN_MOV, OP_SIZE_8, 0, 0);
453 gen_insn(INSN_LDP, OP_SIZE_8, 0, 0);
456 gen_one(ARG_ADDRESS_1);
460 gen_insn(INSN_LDP, OP_SIZE_8, 0, 0);
463 gen_one(ARG_ADDRESS_1);
467 gen_insn(INSN_LDP, OP_SIZE_8, 0, 0);
470 gen_one(ARG_ADDRESS_1);
474 gen_insn(INSN_LDP, OP_SIZE_8, 0, 0);
476 gen_one(R_TIMESTAMP);
477 gen_one(ARG_ADDRESS_1);
481 gen_insn(INSN_LDP, OP_SIZE_8, 0, 0);
484 gen_one(ARG_ADDRESS_1);
488 gen_insn(INSN_LDP, OP_SIZE_8, 0, 0);
491 gen_one(ARG_ADDRESS_1_POST_I);
493 gen_eight(FRAME_SIZE);
495 gen_insn(INSN_RET, 0, 0, 0);
500 static bool attr_w gen_upcall_argument(struct codegen_context attr_unused *ctx, unsigned attr_unused arg)
505 static bool attr_w gen_get_upcall_pointer(struct codegen_context *ctx, unsigned offset, unsigned reg)
507 g(gen_address(ctx, R_UPCALL, offset, IMM_PURPOSE_LDR_OFFSET, OP_SIZE_8));
508 gen_insn(INSN_MOV, OP_SIZE_8, 0, 0);
510 gen_address_offset();
515 static bool attr_w gen_upcall(struct codegen_context *ctx, unsigned offset, unsigned n_args)
517 g(gen_get_upcall_pointer(ctx, offset, R_SCRATCH_NA_1));
519 gen_insn(INSN_CALL_INDIRECT, OP_SIZE_8, 0, 0);
520 gen_one(R_SCRATCH_NA_1);
522 g(gen_upcall_end(ctx, n_args));
527 static bool attr_w gen_timestamp_test(struct codegen_context *ctx, uint32_t escape_label)
529 g(gen_address(ctx, R_UPCALL, offsetof(struct cg_upcall_vector_s, ts), IMM_PURPOSE_LDR_OFFSET, OP_SIZE_4));
530 gen_insn(INSN_MOV, OP_SIZE_4, 0, 0);
531 gen_one(R_SCRATCH_1);
532 gen_address_offset();
534 gen_insn(INSN_CMP, OP_SIZE_4, 0, 1);
535 gen_one(R_SCRATCH_1);
536 gen_one(R_TIMESTAMP);
538 gen_insn(INSN_JMP_COND, OP_SIZE_4, COND_NE, 0);
539 gen_four(escape_label);