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) 0
29 #define COND_IS_LOGICAL(cond) 0
31 #define ARCH_PARTIAL_ALU(size) 0
32 #define ARCH_IS_3ADDRESS 1
33 #define ARCH_HAS_FLAGS 1
34 #define ARCH_PREFERS_SX(size) 0
35 #define ARCH_HAS_BWX 1
36 #define ARCH_HAS_MUL 1
37 #define ARCH_HAS_DIV 1
38 #define ARCH_HAS_ANDN 1
39 #define ARCH_HAS_SHIFTED_ADD(bits) 1
40 #define ARCH_HAS_BTX(btx, size, cnst) 0
41 #define ARCH_SHIFT_SIZE OP_SIZE_4
42 #define ARCH_NEEDS_BARRIER 0
44 #define i_size(size) maximum(size, OP_SIZE_4)
45 #define i_size_rot(size) maximum(size, OP_SIZE_4)
113 #define FRAME_SIZE 0x40
116 #define R_UPCALL R_27
117 #define R_TIMESTAMP R_26
118 #define R_SAVED_1 R_25
119 #define R_SAVED_2 R_24
120 #define R_SAVED_3 R_23
122 #define R_SCRATCH_1 R_0
123 #define R_SCRATCH_2 R_1
124 #define R_SCRATCH_3 R_2
125 #define R_SCRATCH_4 R_3
126 #define R_SCRATCH_NA_1 R_8
127 #define R_SCRATCH_NA_2 R_9
128 #define R_SCRATCH_NA_3 R_10
129 #define R_OFFSET_IMM R_16
130 #define R_CONST_IMM R_17
139 #define FR_SCRATCH_1 FR_0
140 #define FR_SCRATCH_2 FR_1
142 #define SUPPORTED_FP 0x6
143 #define SUPPORTED_FP_HALF_CVT 0x1
145 static bool reg_is_fp(unsigned reg)
147 return reg >= 0x20 && reg < 0x40;
155 static const struct logical_imm value_to_code_4_table[] = {
156 #include "arm64-w.inc"
159 static const struct logical_imm value_to_code_8_table[] = {
160 #include "arm64-x.inc"
163 static int16_t value_to_code(uint8_t size, uint64_t value)
166 if (size == OP_SIZE_4) {
167 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);
168 return value_to_code_4_table[result].code;
170 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);
171 return value_to_code_8_table[result].code;
175 static bool attr_w gen_load_constant(struct codegen_context *ctx, unsigned reg, uint64_t c)
182 code = value_to_code(OP_SIZE_4, c);
184 gen_insn(INSN_ALU, OP_SIZE_4, ALU_OR, 0);
192 code = value_to_code(OP_SIZE_8, c);
194 gen_insn(INSN_ALU, OP_SIZE_8, ALU_OR, 0);
203 if ((int64_t)c < 0) {
204 gen_insn(INSN_MOV, OP_SIZE_8, 0, 0);
207 gen_eight((c & 0xffff) | 0xffffffffffff0000ULL);
208 if ((c & 0xffff0000ULL) != 0xffff0000ULL) {
209 gen_insn(INSN_MOV_MASK, OP_SIZE_8, MOV_MASK_16_32, 0);
213 gen_eight((c >> 16) & 0xffff);
215 if ((c & 0xffff00000000ULL) != 0xffff00000000ULL) {
216 gen_insn(INSN_MOV_MASK, OP_SIZE_8, MOV_MASK_32_48, 0);
220 gen_eight((c >> 32) & 0xffff);
222 if ((c & 0xffff000000000000ULL) != 0xffff000000000000ULL) {
223 gen_insn(INSN_MOV_MASK, OP_SIZE_8, MOV_MASK_48_64, 0);
227 gen_eight((c >> 48) & 0xffff);
230 gen_insn(INSN_MOV, OP_SIZE_8, 0, 0);
233 gen_eight(c & 0xffff);
234 if (c & 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) {
242 gen_insn(INSN_MOV_MASK, OP_SIZE_8, MOV_MASK_32_48, 0);
246 gen_eight((c >> 32) & 0xffff);
248 if (c & 0xffff000000000000ULL) {
249 gen_insn(INSN_MOV_MASK, OP_SIZE_8, MOV_MASK_48_64, 0);
253 gen_eight((c >> 48) & 0xffff);
259 static bool attr_w gen_address(struct codegen_context *ctx, unsigned base, int64_t imm, unsigned purpose, unsigned size)
261 ctx->base_reg = base;
262 ctx->offset_imm = imm;
263 ctx->offset_reg = false;
265 case IMM_PURPOSE_LDR_OFFSET:
266 case IMM_PURPOSE_LDR_SX_OFFSET:
267 case IMM_PURPOSE_STR_OFFSET:
268 case IMM_PURPOSE_VLDR_VSTR_OFFSET:
269 case IMM_PURPOSE_MVI_CLI_OFFSET:
270 if (likely(imm >= -256) && likely(imm <= 255))
272 if (likely(imm >= 0)) {
273 if (unlikely((imm & ((1 << size) - 1)) != 0))
275 if (likely((imm >> size) <= 4095))
279 case IMM_PURPOSE_LDP_STP_OFFSET:
280 if (unlikely((imm & ((1 << size) - 1)) != 0))
282 if (imm / (1 << size) >= -64 && imm / (1 << size) <= 63)
286 internal(file_line, "gen_address: invalid purpose %d", purpose);
288 g(gen_load_constant(ctx, R_OFFSET_IMM, imm));
289 if (purpose == IMM_PURPOSE_LDP_STP_OFFSET) {
290 gen_insn(INSN_ALU, OP_SIZE_ADDRESS, ALU_ADD, 0);
291 gen_one(R_OFFSET_IMM);
292 gen_one(R_OFFSET_IMM);
294 ctx->base_reg = R_OFFSET_IMM;
298 ctx->offset_reg = true;
303 static bool is_direct_const(int64_t imm, unsigned purpose, unsigned size)
306 case IMM_PURPOSE_STORE_VALUE:
310 case IMM_PURPOSE_ADD:
311 case IMM_PURPOSE_SUB:
312 case IMM_PURPOSE_CMP:
313 case IMM_PURPOSE_CMP_LOGICAL:
314 if (imm >= 0 && imm < 4096)
317 case IMM_PURPOSE_AND:
319 case IMM_PURPOSE_XOR:
320 case IMM_PURPOSE_ANDN:
321 case IMM_PURPOSE_TEST:
322 if (value_to_code(size, imm) >= 0)
325 case IMM_PURPOSE_MUL:
328 internal(file_line, "is_direct_const: invalid purpose %u (imm %"PRIxMAX", size %u)", purpose, (uintmax_t)imm, size);
333 static bool attr_w gen_imm(struct codegen_context *ctx, int64_t imm, unsigned purpose, unsigned size)
335 if (is_direct_const(imm, purpose, size)) {
336 ctx->const_imm = imm;
337 ctx->const_reg = false;
339 g(gen_load_constant(ctx, R_CONST_IMM, imm));
340 ctx->const_reg = true;
345 static bool attr_w gen_entry(struct codegen_context *ctx)
347 gen_insn(INSN_STP, OP_SIZE_8, 0, 0);
348 gen_one(ARG_ADDRESS_1_PRE_I);
350 gen_eight(-FRAME_SIZE);
354 gen_insn(INSN_MOV, OP_SIZE_8, 0, 0);
358 gen_insn(INSN_STP, OP_SIZE_8, 0, 0);
359 gen_one(ARG_ADDRESS_1);
365 gen_insn(INSN_STP, OP_SIZE_8, 0, 0);
366 gen_one(ARG_ADDRESS_1);
370 gen_one(R_TIMESTAMP);
372 gen_insn(INSN_STP, OP_SIZE_8, 0, 0);
373 gen_one(ARG_ADDRESS_1);
379 gen_insn(INSN_MOV, OP_SIZE_8, 0, 0);
383 gen_insn(INSN_MOV, OP_SIZE_8, 0, 0);
387 gen_insn(INSN_MOV, OP_SIZE_8, 0, 0);
388 gen_one(R_TIMESTAMP);
391 gen_insn(INSN_JMP_INDIRECT, 0, 0, 0);
397 static bool attr_w gen_escape_arg(struct codegen_context *ctx, ip_t ip, uint32_t escape_label)
399 g(gen_load_constant(ctx, R_RET1, ip));
401 gen_insn(INSN_JMP, 0, 0, 0);
402 gen_four(escape_label);
407 static bool attr_w gen_escape(struct codegen_context *ctx)
409 gen_insn(INSN_MOV, OP_SIZE_8, 0, 0);
413 gen_insn(INSN_LDP, OP_SIZE_8, 0, 0);
416 gen_one(ARG_ADDRESS_1);
420 gen_insn(INSN_LDP, OP_SIZE_8, 0, 0);
422 gen_one(R_TIMESTAMP);
423 gen_one(ARG_ADDRESS_1);
427 gen_insn(INSN_LDP, OP_SIZE_8, 0, 0);
430 gen_one(ARG_ADDRESS_1);
434 gen_insn(INSN_LDP, OP_SIZE_8, 0, 0);
437 gen_one(ARG_ADDRESS_1_POST_I);
439 gen_eight(FRAME_SIZE);
441 gen_insn(INSN_RET, 0, 0, 0);
446 static bool attr_w gen_upcall_argument(struct codegen_context attr_unused *ctx, unsigned attr_unused arg)
451 static bool attr_w gen_upcall(struct codegen_context *ctx, unsigned offset, unsigned attr_unused n_args)
453 g(gen_address(ctx, R_UPCALL, offset, IMM_PURPOSE_LDR_OFFSET, OP_SIZE_8));
454 gen_insn(INSN_MOV, OP_SIZE_8, 0, 0);
455 gen_one(R_SCRATCH_NA_1);
456 gen_address_offset();
458 gen_insn(INSN_CALL_INDIRECT, OP_SIZE_8, 0, 0);
459 gen_one(R_SCRATCH_NA_1);
464 static bool attr_w gen_timestamp_test(struct codegen_context *ctx, uint32_t escape_label)
466 g(gen_address(ctx, R_UPCALL, offsetof(struct cg_upcall_vector_s, ts), IMM_PURPOSE_LDR_OFFSET, OP_SIZE_4));
467 gen_insn(INSN_MOV, OP_SIZE_4, 0, 0);
468 gen_one(R_SCRATCH_1);
469 gen_address_offset();
471 gen_insn(INSN_CMP, OP_SIZE_4, 0, 1);
472 gen_one(R_SCRATCH_1);
473 gen_one(R_TIMESTAMP);
475 gen_insn(INSN_JMP_COND, OP_SIZE_4, COND_NE, 0);
476 gen_four(escape_label);