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_FLAGS 0
39 #define ARCH_PREFERS_SX(size) 0
40 #define ARCH_HAS_BWX 1
41 #define ARCH_HAS_MUL 0
42 #define ARCH_HAS_DIV 0
43 #define ARCH_HAS_ANDN 1
44 #define ARCH_HAS_SHIFTED_ADD(bits) ((bits) <= 4)
45 #define ARCH_HAS_BTX(btx, size, cnst) (((btx) == BTX_BTS || (btx) == BTX_BTR) && (cnst))
46 #define ARCH_SHIFT_SIZE 32
47 #define ARCH_NEEDS_BARRIER 0
49 #define i_size(size) OP_SIZE_NATIVE
50 #define i_size_rot(size) OP_SIZE_NATIVE
51 #define i_size_cmp(size) maximum(size, OP_SIZE_4)
53 #define N_SAVED_REGS 0x40
236 #define R_UPCALL R_33
237 #define R_TIMESTAMP R_34
239 #define R_SAVED_1 R_35
240 #define R_SAVED_2 R_36
241 #define R_SAVED_B0 R_37
242 #define R_SAVED_AR_PFS R_38
243 #define R_ARG0 (R_32 + N_SAVED_REGS - 4)
244 #define R_ARG1 (R_32 + N_SAVED_REGS - 3)
245 #define R_ARG2 (R_32 + N_SAVED_REGS - 2)
246 #define R_ARG3 (R_32 + N_SAVED_REGS - 1)
250 #define R_SCRATCH_NA_1 R_14
251 #define R_SCRATCH_NA_2 R_15
252 #define R_SCRATCH_NA_3 R_16
253 #define R_SCRATCH_1 R_17
254 #define R_SCRATCH_2 R_18
255 #define R_SCRATCH_3 R_19
256 #define R_SCRATCH_4 R_20
258 #define R_OFFSET_IMM R_2
259 #define R_CONST_IMM R_3
260 #define R_CMP_RESULT P_6
262 #define R_SCRATCH_B B_6
264 #define FR_SCRATCH_1 FR_6
265 #define FR_SCRATCH_2 FR_7
267 #define SUPPORTED_FP 0xe
269 static inline bool reg_is_gr(unsigned reg)
274 static inline bool reg_is_fp(unsigned reg)
276 return reg >= 0x60 && reg < 0xa0;
279 static inline bool reg_is_p(unsigned reg)
281 return reg >= 0xa0 && reg < 0xa8;
284 static inline bool reg_is_b(unsigned reg)
286 return reg >= 0xb0 && reg < 0xb8;
289 static inline uint64_t bits_gr(unsigned reg)
291 ajla_assert_lo(reg_is_gr(reg), (file_line, "bits_gr: register %x", reg));
295 static inline uint64_t bits_fp(unsigned reg)
297 ajla_assert_lo(reg_is_fp(reg), (file_line, "bits_fp: register %x", reg));
301 static inline uint64_t bits_p(unsigned reg)
303 ajla_assert_lo(reg_is_p(reg), (file_line, "bits_p: register %x", reg));
307 static inline uint64_t bits_b(unsigned reg)
309 ajla_assert_lo(reg_is_b(reg), (file_line, "bits_b: register %x", reg));
313 static const uint8_t regs_saved[] = {
368 static const uint8_t regs_volatile[] = {
383 static const uint8_t fp_saved[] = { 0 };
384 #define n_fp_saved 0U
385 static const uint8_t fp_volatile[] = {
427 #define reg_is_saved(r) ((r) >= R_32 && (r) <= R_95)
429 static bool attr_w gen_load_constant(struct codegen_context *ctx, unsigned reg, uint64_t c)
431 gen_insn(INSN_MOV, OP_SIZE_NATIVE, 0, 0);
438 static bool attr_w gen_imm(struct codegen_context *ctx, int64_t imm, unsigned purpose, unsigned size);
440 static bool attr_w gen_address(struct codegen_context *ctx, unsigned base, int64_t imm, unsigned attr_unused purpose, unsigned attr_unused size)
443 ctx->offset_imm = imm;
444 ctx->offset_reg = false;
445 ctx->base_reg = base;
447 g(gen_imm(ctx, imm, IMM_PURPOSE_ADD, OP_SIZE_NATIVE));
448 gen_insn(INSN_ALU, OP_SIZE_NATIVE, ALU_ADD, 0);
449 gen_one(R_OFFSET_IMM);
453 ctx->offset_reg = false;
454 ctx->base_reg = R_OFFSET_IMM;
459 static bool is_direct_const(int64_t imm, unsigned purpose, unsigned size)
462 case IMM_PURPOSE_STORE_VALUE:
466 case IMM_PURPOSE_ADD:
467 case IMM_PURPOSE_MOVR:
468 if (imm >= -0x2000 && imm < 0x2000)
471 case IMM_PURPOSE_SUB:
472 if (imm > -0x2000 && imm <= 0x2000)
475 case IMM_PURPOSE_AND:
477 case IMM_PURPOSE_XOR:
478 if (imm >= -0x80 && imm < 0x80)
481 case IMM_PURPOSE_CMP:
482 case IMM_PURPOSE_CMP_LOGICAL:
483 if (imm > -0x80 && imm < 0x80)
486 case IMM_PURPOSE_ANDN:
488 case IMM_PURPOSE_TEST:
490 case IMM_PURPOSE_BITWISE:
493 internal(file_line, "is_direct_const: invalid purpose %u (imm %"PRIxMAX", size %u)", purpose, (uintmax_t)imm, size);
498 static bool attr_w gen_imm(struct codegen_context *ctx, int64_t imm, unsigned purpose, unsigned size)
500 if (is_direct_const(imm, purpose, size)) {
501 ctx->const_imm = imm;
502 ctx->const_reg = false;
504 g(gen_load_constant(ctx, R_CONST_IMM, imm));
505 ctx->const_reg = true;
510 static bool attr_w gen_entry(struct codegen_context *ctx)
512 gen_insn(INSN_IA64_ALLOC, OP_SIZE_NATIVE, 0, 0);
513 gen_one(R_SAVED_AR_PFS);
515 gen_eight(N_SAVED_REGS);
517 gen_eight(N_SAVED_REGS - 4);
519 gen_insn(INSN_MOV, OP_SIZE_NATIVE, 0, 0);
523 gen_insn(INSN_JMP_INDIRECT, 0, 0, 0);
526 gen_insn(INSN_RET, 0, 0, 0);
531 static bool attr_w gen_escape_arg(struct codegen_context *ctx, ip_t ip, uint32_t escape_label)
533 g(gen_load_constant(ctx, R_RET1, ip));
535 gen_insn(INSN_JMP, 0, 0, 0);
536 gen_four(escape_label);
541 static bool attr_w gen_escape(struct codegen_context *ctx)
543 gen_insn(INSN_MOV, OP_SIZE_NATIVE, 0, 0);
547 gen_insn(INSN_MOV, OP_SIZE_NATIVE, 0, 0);
551 gen_insn(INSN_IA64_DEALLOC, OP_SIZE_NATIVE, 0, 0);
552 gen_one(R_SAVED_AR_PFS);
554 gen_insn(INSN_RET, 0, 0, 0);
559 static bool attr_w gen_upcall_argument(struct codegen_context attr_unused *ctx, unsigned attr_unused arg)
564 static bool attr_w gen_upcall(struct codegen_context *ctx, unsigned offset, unsigned n_args)
566 g(gen_address(ctx, R_UPCALL, offset, IMM_PURPOSE_LDR_OFFSET, OP_SIZE_NATIVE));
567 gen_insn(INSN_MOV, OP_SIZE_ADDRESS, 0, 0);
568 gen_one(R_SCRATCH_NA_1);
569 gen_address_offset();
571 g(gen_address(ctx, R_SCRATCH_NA_1, 0, IMM_PURPOSE_LDR_OFFSET, OP_SIZE_NATIVE));
572 gen_insn(INSN_MOV, OP_SIZE_ADDRESS, 0, 0);
573 gen_one(R_SCRATCH_NA_2);
574 gen_address_offset();
576 g(gen_address(ctx, R_SCRATCH_NA_1, 8, IMM_PURPOSE_LDR_OFFSET, OP_SIZE_NATIVE));
577 gen_insn(INSN_MOV, OP_SIZE_ADDRESS, 0, 0);
579 gen_address_offset();
581 gen_insn(INSN_MOV, OP_SIZE_NATIVE, 0, 0);
582 gen_one(R_SCRATCH_B);
583 gen_one(R_SCRATCH_NA_2);
585 gen_insn(INSN_CALL_INDIRECT, OP_SIZE_8, 0, 0);
586 gen_one(R_SCRATCH_B);
588 g(gen_upcall_end(ctx, n_args));
593 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);
595 static bool attr_w gen_timestamp_test(struct codegen_context *ctx, uint32_t escape_label)
597 g(gen_address(ctx, R_UPCALL, offsetof(struct cg_upcall_vector_s, ts), IMM_PURPOSE_LDR_OFFSET, OP_SIZE_NATIVE));
598 gen_insn(INSN_MOV, OP_SIZE_4, 0, 0);
599 gen_one(R_SCRATCH_1);
600 gen_address_offset();
602 g(gen_cmp_test_jmp(ctx, INSN_CMP, OP_SIZE_4, R_SCRATCH_1, R_TIMESTAMP, COND_NE, escape_label));