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_NATIVE
22 #define JMP_LIMIT (cpu_test_feature(CPU_FEATURE_brl) ? JMP_SHORT : JMP_SHORTEST)
24 #define UNALIGNED_TRAP 1
26 #define ALU_WRITES_FLAGS(size, alu, is_mem, is_imm, imm) 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 0
40 #define ARCH_PREFERS_SX(size) 0
41 #define ARCH_HAS_BWX 1
42 #define ARCH_HAS_MUL 0
43 #define ARCH_HAS_DIV 0
44 #define ARCH_HAS_ANDN 1
45 #define ARCH_HAS_SHIFTED_ADD(bits) ((bits) <= 4)
46 #define ARCH_HAS_BTX(btx, size, cnst) (((btx) == BTX_BTS || (btx) == BTX_BTR) && (cnst))
47 #define ARCH_SHIFT_SIZE 32
48 #define ARCH_BOOL_SIZE OP_SIZE_NATIVE
49 #define ARCH_HAS_FP_GP_MOV 1
50 #define ARCH_NEEDS_BARRIER 0
52 #define i_size(size) OP_SIZE_NATIVE
53 #define i_size_rot(size) OP_SIZE_NATIVE
54 #define i_size_cmp(size) maximum(size, OP_SIZE_4)
56 #define N_SAVED_REGS 0x40
239 #define R_UPCALL R_33
240 #define R_TIMESTAMP R_34
242 #define R_SAVED_1 R_35
243 #define R_SAVED_2 R_36
244 #define R_SAVED_B0 R_37
245 #define R_SAVED_AR_PFS R_38
246 #define R_ARG0 (R_32 + N_SAVED_REGS - 4)
247 #define R_ARG1 (R_32 + N_SAVED_REGS - 3)
248 #define R_ARG2 (R_32 + N_SAVED_REGS - 2)
249 #define R_ARG3 (R_32 + N_SAVED_REGS - 1)
253 #define R_CG_SCRATCH R_10
254 #define R_SCRATCH_NA_1 R_14
255 #define R_SCRATCH_NA_2 R_15
256 #define R_SCRATCH_NA_3 R_16
257 #define R_SCRATCH_1 R_17
258 #define R_SCRATCH_2 R_18
259 #define R_SCRATCH_3 R_19
260 #define R_SCRATCH_4 R_20
262 #define R_OFFSET_IMM R_2
263 #define R_CONST_IMM R_3
264 #define R_CMP_RESULT P_6
266 #define R_SCRATCH_B B_6
268 #define FR_SCRATCH_1 FR_6
269 #define FR_SCRATCH_2 FR_7
271 #define SUPPORTED_FP 0xe
273 static inline bool reg_is_gr(unsigned reg)
278 static inline bool reg_is_fp(unsigned reg)
280 return reg >= 0x60 && reg < 0xa0;
283 static inline bool reg_is_p(unsigned reg)
285 return reg >= 0xa0 && reg < 0xa8;
288 static inline bool reg_is_b(unsigned reg)
290 return reg >= 0xb0 && reg < 0xb8;
293 static inline uint64_t bits_gr(unsigned reg)
295 ajla_assert_lo(reg_is_gr(reg), (file_line, "bits_gr: register %x", reg));
299 static inline uint64_t bits_fp(unsigned reg)
301 ajla_assert_lo(reg_is_fp(reg), (file_line, "bits_fp: register %x", reg));
305 static inline uint64_t bits_p(unsigned reg)
307 ajla_assert_lo(reg_is_p(reg), (file_line, "bits_p: register %x", reg));
311 static inline uint64_t bits_b(unsigned reg)
313 ajla_assert_lo(reg_is_b(reg), (file_line, "bits_b: register %x", reg));
317 static const uint8_t regs_saved[] = {
372 static const uint8_t regs_volatile[] = {
386 static const uint8_t fp_saved[] = { 0 };
387 #define n_fp_saved 0U
388 static const uint8_t fp_volatile[] = {
430 #define reg_is_saved(r) ((r) >= R_32 && (r) <= R_95)
432 static bool attr_w gen_load_constant(struct codegen_context *ctx, unsigned reg, uint64_t c)
434 gen_insn(INSN_MOV, OP_SIZE_NATIVE, 0, 0);
441 static bool attr_w gen_address(struct codegen_context *ctx, unsigned base, int64_t imm, unsigned attr_unused purpose, unsigned attr_unused size)
444 ctx->offset_imm = imm;
445 ctx->offset_reg = false;
446 ctx->base_reg = base;
448 g(gen_imm(ctx, imm, IMM_PURPOSE_ADD, OP_SIZE_NATIVE));
449 gen_insn(INSN_ALU, OP_SIZE_NATIVE, ALU_ADD, 0);
450 gen_one(R_OFFSET_IMM);
454 ctx->offset_reg = false;
455 ctx->base_reg = R_OFFSET_IMM;
460 static bool is_direct_const(int64_t imm, unsigned purpose, unsigned size)
463 case IMM_PURPOSE_STORE_VALUE:
467 case IMM_PURPOSE_ADD:
468 case IMM_PURPOSE_MOVR:
469 if (imm >= -0x2000 && imm < 0x2000)
472 case IMM_PURPOSE_SUB:
473 if (imm > -0x2000 && imm <= 0x2000)
476 case IMM_PURPOSE_AND:
478 case IMM_PURPOSE_XOR:
479 if (imm >= -0x80 && imm < 0x80)
482 case IMM_PURPOSE_CMP:
483 case IMM_PURPOSE_CMP_LOGICAL:
484 if (imm > -0x80 && imm < 0x80)
487 case IMM_PURPOSE_ANDN:
489 case IMM_PURPOSE_TEST:
491 case IMM_PURPOSE_BITWISE:
494 internal(file_line, "is_direct_const: invalid purpose %u (imm %"PRIxMAX", size %u)", purpose, (uintmax_t)imm, size);
499 static bool attr_w gen_entry(struct codegen_context *ctx)
501 gen_insn(INSN_IA64_ALLOC, OP_SIZE_NATIVE, 0, 0);
502 gen_one(R_SAVED_AR_PFS);
504 gen_eight(N_SAVED_REGS);
506 gen_eight(N_SAVED_REGS - 4);
508 gen_insn(INSN_MOV, OP_SIZE_NATIVE, 0, 0);
512 gen_insn(INSN_JMP_INDIRECT, 0, 0, 0);
515 gen_insn(INSN_RET, 0, 0, 0);
520 static bool attr_w gen_escape_arg(struct codegen_context *ctx, ip_t ip, uint32_t escape_label)
522 g(gen_load_constant(ctx, R_RET1, ip));
524 gen_insn(INSN_JMP, 0, 0, 0);
525 gen_four(escape_label);
530 static bool attr_w gen_escape(struct codegen_context *ctx)
532 gen_insn(INSN_MOV, OP_SIZE_NATIVE, 0, 0);
536 gen_insn(INSN_MOV, OP_SIZE_NATIVE, 0, 0);
540 gen_insn(INSN_IA64_DEALLOC, OP_SIZE_NATIVE, 0, 0);
541 gen_one(R_SAVED_AR_PFS);
543 gen_insn(INSN_RET, 0, 0, 0);
548 static bool attr_w gen_upcall_argument(struct codegen_context attr_unused *ctx, unsigned attr_unused arg)
553 static bool attr_w gen_get_upcall_pointer(struct codegen_context *ctx, unsigned offset, unsigned reg)
555 g(gen_address(ctx, R_UPCALL, offset, IMM_PURPOSE_LDR_OFFSET, OP_SIZE_NATIVE));
556 gen_insn(INSN_MOV, OP_SIZE_ADDRESS, 0, 0);
558 gen_address_offset();
563 static bool attr_w gen_upcall(struct codegen_context *ctx, unsigned offset, unsigned n_args)
565 g(gen_get_upcall_pointer(ctx, offset, R_SCRATCH_NA_1));
567 g(gen_address(ctx, R_SCRATCH_NA_1, 0, IMM_PURPOSE_LDR_OFFSET, OP_SIZE_NATIVE));
568 gen_insn(INSN_MOV, OP_SIZE_ADDRESS, 0, 0);
569 gen_one(R_SCRATCH_NA_2);
570 gen_address_offset();
572 g(gen_address(ctx, R_SCRATCH_NA_1, 8, IMM_PURPOSE_LDR_OFFSET, OP_SIZE_NATIVE));
573 gen_insn(INSN_MOV, OP_SIZE_ADDRESS, 0, 0);
575 gen_address_offset();
577 gen_insn(INSN_MOV, OP_SIZE_NATIVE, 0, 0);
578 gen_one(R_SCRATCH_B);
579 gen_one(R_SCRATCH_NA_2);
581 gen_insn(INSN_CALL_INDIRECT, OP_SIZE_8, 0, 0);
582 gen_one(R_SCRATCH_B);
584 g(gen_upcall_end(ctx, n_args));
589 static bool attr_w gen_cmp_test_jmp(struct codegen_context *ctx, unsigned insn, unsigned op_size, unsigned reg1, unsigned reg2, unsigned cond, uint32_t label);
591 static bool attr_w gen_timestamp_test(struct codegen_context *ctx, uint32_t escape_label)
593 g(gen_address(ctx, R_UPCALL, offsetof(struct cg_upcall_vector_s, ts), IMM_PURPOSE_LDR_OFFSET, OP_SIZE_NATIVE));
594 gen_insn(INSN_MOV, OP_SIZE_4, 0, 0);
595 gen_one(R_SCRATCH_1);
596 gen_address_offset();
598 g(gen_cmp_test_jmp(ctx, INSN_CMP, OP_SIZE_4, R_SCRATCH_1, R_TIMESTAMP, COND_NE, escape_label));