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(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 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_SCRATCH_NA_1 R_14
254 #define R_SCRATCH_NA_2 R_15
255 #define R_SCRATCH_NA_3 R_16
256 #define R_SCRATCH_1 R_17
257 #define R_SCRATCH_2 R_18
258 #define R_SCRATCH_3 R_19
259 #define R_SCRATCH_4 R_20
261 #define R_OFFSET_IMM R_2
262 #define R_CONST_IMM R_3
263 #define R_CMP_RESULT P_6
265 #define R_SCRATCH_B B_6
267 #define FR_SCRATCH_1 FR_6
268 #define FR_SCRATCH_2 FR_7
270 #define SUPPORTED_FP 0xe
272 static inline bool reg_is_gr(unsigned reg)
277 static inline bool reg_is_fp(unsigned reg)
279 return reg >= 0x60 && reg < 0xa0;
282 static inline bool reg_is_p(unsigned reg)
284 return reg >= 0xa0 && reg < 0xa8;
287 static inline bool reg_is_b(unsigned reg)
289 return reg >= 0xb0 && reg < 0xb8;
292 static inline uint64_t bits_gr(unsigned reg)
294 ajla_assert_lo(reg_is_gr(reg), (file_line, "bits_gr: register %x", reg));
298 static inline uint64_t bits_fp(unsigned reg)
300 ajla_assert_lo(reg_is_fp(reg), (file_line, "bits_fp: register %x", reg));
304 static inline uint64_t bits_p(unsigned reg)
306 ajla_assert_lo(reg_is_p(reg), (file_line, "bits_p: register %x", reg));
310 static inline uint64_t bits_b(unsigned reg)
312 ajla_assert_lo(reg_is_b(reg), (file_line, "bits_b: register %x", reg));
316 static const uint8_t regs_saved[] = {
371 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_imm(struct codegen_context *ctx, int64_t imm, unsigned purpose, unsigned size);
443 static bool attr_w gen_address(struct codegen_context *ctx, unsigned base, int64_t imm, unsigned attr_unused purpose, unsigned attr_unused size)
446 ctx->offset_imm = imm;
447 ctx->offset_reg = false;
448 ctx->base_reg = base;
450 g(gen_imm(ctx, imm, IMM_PURPOSE_ADD, OP_SIZE_NATIVE));
451 gen_insn(INSN_ALU, OP_SIZE_NATIVE, ALU_ADD, 0);
452 gen_one(R_OFFSET_IMM);
456 ctx->offset_reg = false;
457 ctx->base_reg = R_OFFSET_IMM;
462 static bool is_direct_const(int64_t imm, unsigned purpose, unsigned size)
465 case IMM_PURPOSE_STORE_VALUE:
469 case IMM_PURPOSE_ADD:
470 case IMM_PURPOSE_MOVR:
471 if (imm >= -0x2000 && imm < 0x2000)
474 case IMM_PURPOSE_SUB:
475 if (imm > -0x2000 && imm <= 0x2000)
478 case IMM_PURPOSE_AND:
480 case IMM_PURPOSE_XOR:
481 if (imm >= -0x80 && imm < 0x80)
484 case IMM_PURPOSE_CMP:
485 case IMM_PURPOSE_CMP_LOGICAL:
486 if (imm > -0x80 && imm < 0x80)
489 case IMM_PURPOSE_ANDN:
491 case IMM_PURPOSE_TEST:
493 case IMM_PURPOSE_BITWISE:
496 internal(file_line, "is_direct_const: invalid purpose %u (imm %"PRIxMAX", size %u)", purpose, (uintmax_t)imm, size);
501 static bool attr_w gen_imm(struct codegen_context *ctx, int64_t imm, unsigned purpose, unsigned size)
503 if (is_direct_const(imm, purpose, size)) {
504 ctx->const_imm = imm;
505 ctx->const_reg = false;
507 g(gen_load_constant(ctx, R_CONST_IMM, imm));
508 ctx->const_reg = true;
513 static bool attr_w gen_entry(struct codegen_context *ctx)
515 gen_insn(INSN_IA64_ALLOC, OP_SIZE_NATIVE, 0, 0);
516 gen_one(R_SAVED_AR_PFS);
518 gen_eight(N_SAVED_REGS);
520 gen_eight(N_SAVED_REGS - 4);
522 gen_insn(INSN_MOV, OP_SIZE_NATIVE, 0, 0);
526 gen_insn(INSN_JMP_INDIRECT, 0, 0, 0);
529 gen_insn(INSN_RET, 0, 0, 0);
534 static bool attr_w gen_escape_arg(struct codegen_context *ctx, ip_t ip, uint32_t escape_label)
536 g(gen_load_constant(ctx, R_RET1, ip));
538 gen_insn(INSN_JMP, 0, 0, 0);
539 gen_four(escape_label);
544 static bool attr_w gen_escape(struct codegen_context *ctx)
546 gen_insn(INSN_MOV, OP_SIZE_NATIVE, 0, 0);
550 gen_insn(INSN_MOV, OP_SIZE_NATIVE, 0, 0);
554 gen_insn(INSN_IA64_DEALLOC, OP_SIZE_NATIVE, 0, 0);
555 gen_one(R_SAVED_AR_PFS);
557 gen_insn(INSN_RET, 0, 0, 0);
562 static bool attr_w gen_upcall_argument(struct codegen_context attr_unused *ctx, unsigned attr_unused arg)
567 static bool attr_w gen_upcall(struct codegen_context *ctx, unsigned offset, unsigned n_args)
569 g(gen_address(ctx, R_UPCALL, offset, IMM_PURPOSE_LDR_OFFSET, OP_SIZE_NATIVE));
570 gen_insn(INSN_MOV, OP_SIZE_ADDRESS, 0, 0);
571 gen_one(R_SCRATCH_NA_1);
572 gen_address_offset();
574 g(gen_address(ctx, R_SCRATCH_NA_1, 0, IMM_PURPOSE_LDR_OFFSET, OP_SIZE_NATIVE));
575 gen_insn(INSN_MOV, OP_SIZE_ADDRESS, 0, 0);
576 gen_one(R_SCRATCH_NA_2);
577 gen_address_offset();
579 g(gen_address(ctx, R_SCRATCH_NA_1, 8, IMM_PURPOSE_LDR_OFFSET, OP_SIZE_NATIVE));
580 gen_insn(INSN_MOV, OP_SIZE_ADDRESS, 0, 0);
582 gen_address_offset();
584 gen_insn(INSN_MOV, OP_SIZE_NATIVE, 0, 0);
585 gen_one(R_SCRATCH_B);
586 gen_one(R_SCRATCH_NA_2);
588 gen_insn(INSN_CALL_INDIRECT, OP_SIZE_8, 0, 0);
589 gen_one(R_SCRATCH_B);
591 g(gen_upcall_end(ctx, n_args));
596 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);
598 static bool attr_w gen_timestamp_test(struct codegen_context *ctx, uint32_t escape_label)
600 g(gen_address(ctx, R_UPCALL, offsetof(struct cg_upcall_vector_s, ts), IMM_PURPOSE_LDR_OFFSET, OP_SIZE_NATIVE));
601 gen_insn(INSN_MOV, OP_SIZE_4, 0, 0);
602 gen_one(R_SCRATCH_1);
603 gen_address_offset();
605 g(gen_cmp_test_jmp(ctx, INSN_CMP, OP_SIZE_4, R_SCRATCH_1, R_TIMESTAMP, COND_NE, escape_label));