2022-11-08 09:32:46 +01:00
|
|
|
/*
|
|
|
|
* IR - Lightweight JIT Compilation Framework
|
|
|
|
* (Aarch64 native code generator based on DynAsm)
|
|
|
|
* Copyright (C) 2022 Zend by Perforce.
|
|
|
|
* Authors: Dmitry Stogov <dmitry@php.net>
|
|
|
|
*/
|
|
|
|
|
2022-05-31 23:51:02 +02:00
|
|
|
|.arch arm64
|
|
|
|
|
|
|
|
|.actionlist dasm_actions
|
|
|
|
|.globals ir_lb
|
|
|
|
|.section code, cold_code, rodata, jmp_table
|
|
|
|
|
|
|
|
#define IR_SPILL_POS_TO_OFFSET(offset) \
|
|
|
|
((ctx->flags & IR_USE_FRAME_POINTER) ? \
|
2022-11-08 21:09:35 +01:00
|
|
|
((offset) + (int32_t)sizeof(void*) * 2) : \
|
2023-06-15 18:28:54 +02:00
|
|
|
((offset) + ctx->call_stack_size))
|
2022-05-31 23:51:02 +02:00
|
|
|
|
2022-06-28 00:43:59 +02:00
|
|
|
#define B_IMM (1<<27) // signed imm26 * 4
|
|
|
|
#define ADR_IMM (1<<20) // signed imm21
|
|
|
|
#define ADRP_IMM (1LL<<32) // signed imm21 * 4096
|
|
|
|
|
2023-12-19 20:22:49 +01:00
|
|
|
static bool aarch64_may_use_b(ir_code_buffer *code_buffer, const void *addr)
|
2022-06-28 00:43:59 +02:00
|
|
|
{
|
2023-12-19 20:22:49 +01:00
|
|
|
if (code_buffer) {
|
|
|
|
if (addr >= code_buffer->start && (char*)addr < (char*)code_buffer->end) {
|
|
|
|
return (((char*)code_buffer->end - (char*)code_buffer->start) < B_IMM);
|
|
|
|
} else if ((char*)addr >= (char*)code_buffer->end) {
|
|
|
|
return (((char*)addr - (char*)code_buffer->start) < B_IMM);
|
|
|
|
} else if (addr < code_buffer->start) {
|
|
|
|
return (((char*)code_buffer->end - (char*)addr) < B_IMM);
|
2022-06-28 00:43:59 +02:00
|
|
|
}
|
|
|
|
}
|
2023-12-19 20:22:49 +01:00
|
|
|
return 0;
|
2022-06-28 00:43:59 +02:00
|
|
|
}
|
|
|
|
|
2023-12-07 23:58:45 +01:00
|
|
|
#if 0
|
2023-12-19 20:22:49 +01:00
|
|
|
static bool aarch64_may_use_adr(ir_code_buffer *code_buffer, const void *addr)
|
2022-06-28 00:43:59 +02:00
|
|
|
{
|
2023-12-19 20:22:49 +01:00
|
|
|
if (code_buffer) {
|
|
|
|
if (addr >= code_buffer->start && (char*)addr < (char*)code_buffer->end) {
|
|
|
|
return (((char*)code_buffer->end - (char*)code_buffer->start) < ADR_IMM);
|
|
|
|
} else if ((char*)addr >= (char*)code_buffer->end) {
|
|
|
|
return (((char*)addr - (char*)code_buffer->start) < ADR_IMM);
|
|
|
|
} else if (addr < code_buffer->start) {
|
|
|
|
return (((char*)code_buffer->end - (char*)addr) < ADR_IMM);
|
2022-06-28 00:43:59 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2023-12-19 20:22:49 +01:00
|
|
|
static bool aarch64_may_use_adrp(ir_code_buffer *code_buffer, const void *addr)
|
2022-06-28 00:43:59 +02:00
|
|
|
{
|
2023-12-19 20:22:49 +01:00
|
|
|
if (code_buffer) {
|
|
|
|
if (addr >= code_buffer->start && (char*)addr < (char*)code_buffer->end) {
|
|
|
|
return (((char*)code_buffer->end - (char*)code_buffer->start) < ADRP_IMM);
|
|
|
|
} else if ((char*)addr >= (char*)code_buffer->end) {
|
|
|
|
return (((char*)addr - (char*)code_buffer->start) < ADRP_IMM);
|
|
|
|
} else if (addr < code_buffer->start) {
|
|
|
|
return (((char*)code_buffer->end - (char*)addr) < ADRP_IMM);
|
2022-06-28 00:43:59 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
2023-12-07 23:58:45 +01:00
|
|
|
#endif
|
2022-06-28 00:43:59 +02:00
|
|
|
|
2022-05-31 23:51:02 +02:00
|
|
|
/* Determine whether "val" falls into two allowed ranges:
|
|
|
|
* Range 1: [0, 0xfff]
|
|
|
|
* Range 2: LSL #12 to Range 1
|
|
|
|
* Used to guard the immediate encoding for add/adds/sub/subs/cmp/cmn instructions. */
|
|
|
|
static bool aarch64_may_encode_imm12(const int64_t val)
|
|
|
|
{
|
|
|
|
return (val >= 0 && (val <= 0xfff || !(val & 0xffffffffff000fff)));
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Determine whether an immediate value can be encoded as the immediate operand of logical instructions. */
|
|
|
|
static bool aarch64_may_encode_logical_imm(uint64_t value, uint32_t type_size)
|
|
|
|
{
|
|
|
|
/* fast path: power of two */
|
|
|
|
if (value > 0 && !(value & (value - 1))) {
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (type_size == 8) {
|
|
|
|
if (dasm_imm13((uint32_t)value, (uint32_t)(value >> 32)) != -1) {
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (dasm_imm13((uint32_t)value, (uint32_t)value) != -1) {
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool aarch64_may_encode_addr_offset(int64_t offset, uint32_t type_size)
|
|
|
|
{
|
2022-06-02 23:38:49 +02:00
|
|
|
return (uintptr_t)(offset) % type_size == 0 && (uintptr_t)(offset) < 0xfff * type_size;
|
2022-05-31 23:51:02 +02:00
|
|
|
}
|
|
|
|
|
2022-06-01 17:16:32 +02:00
|
|
|
|.macro ASM_REG_REG_OP, op, type, dst, src
|
|
|
|
|| if (ir_type_size[type] == 8) {
|
|
|
|
| op Rx(dst), Rx(src)
|
|
|
|
|| } else {
|
|
|
|
| op Rw(dst), Rw(src)
|
|
|
|
|| }
|
|
|
|
|.endmacro
|
|
|
|
|
2022-05-31 23:51:02 +02:00
|
|
|
|.macro ASM_REG_REG_REG_OP, op, type, dst, src1, src2
|
|
|
|
|| if (ir_type_size[type] == 8) {
|
|
|
|
| op Rx(dst), Rx(src1), Rx(src2)
|
|
|
|
|| } else {
|
|
|
|
| op Rw(dst), Rw(src1), Rw(src2)
|
|
|
|
|| }
|
|
|
|
|.endmacro
|
|
|
|
|
2022-06-01 17:16:32 +02:00
|
|
|
|.macro ASM_REG_REG_REG_REG_OP, op, type, dst, src1, src2, src3
|
|
|
|
|| if (ir_type_size[type] == 8) {
|
|
|
|
| op Rx(dst), Rx(src1), Rx(src2), Rx(src3)
|
|
|
|
|| } else {
|
|
|
|
| op Rw(dst), Rw(src1), Rw(src2), Rw(src3);
|
|
|
|
|| }
|
|
|
|
|.endmacro
|
|
|
|
|
2022-05-31 23:51:02 +02:00
|
|
|
|.macro ASM_REG_REG_IMM_OP, op, type, dst, src1, val
|
|
|
|
|| if (ir_type_size[type] == 8) {
|
|
|
|
| op Rx(dst), Rx(src1), #val
|
|
|
|
|| } else {
|
|
|
|
| op Rw(dst), Rw(src1), #val
|
|
|
|
|| }
|
|
|
|
|.endmacro
|
|
|
|
|
2022-06-01 17:16:32 +02:00
|
|
|
|.macro ASM_REG_IMM_OP, op, type, reg, val
|
|
|
|
|| if (ir_type_size[type] == 8) {
|
|
|
|
| op Rx(reg), #val
|
|
|
|
|| } else {
|
|
|
|
| op Rw(reg), #val
|
2022-05-31 23:51:02 +02:00
|
|
|
|| }
|
|
|
|
|.endmacro
|
|
|
|
|
2023-10-13 19:50:23 +02:00
|
|
|
|.macro ASM_FP_REG_IMM_OP, op, type, reg, val
|
|
|
|
|| if (type == IR_DOUBLE) {
|
|
|
|
| op Rd(reg-IR_REG_FP_FIRST), #val
|
|
|
|
|| } else {
|
|
|
|
|| IR_ASSERT(type == IR_FLOAT);
|
|
|
|
| op Rs(reg-IR_REG_FP_FIRST), #val
|
|
|
|
|| }
|
|
|
|
|.endmacro
|
|
|
|
|
2022-05-31 23:51:02 +02:00
|
|
|
|.macro ASM_FP_REG_REG_REG_OP, op, type, dst, src1, src2
|
|
|
|
|| if (type == IR_DOUBLE) {
|
|
|
|
| op Rd(dst-IR_REG_FP_FIRST), Rd(src1-IR_REG_FP_FIRST), Rd(src2-IR_REG_FP_FIRST)
|
|
|
|
|| } else {
|
|
|
|
|| IR_ASSERT(type == IR_FLOAT);
|
|
|
|
| op Rs(dst-IR_REG_FP_FIRST), Rs(src1-IR_REG_FP_FIRST), Rs(src2-IR_REG_FP_FIRST)
|
|
|
|
|| }
|
|
|
|
|.endmacro
|
|
|
|
|
|
|
|
typedef struct _ir_backend_data {
|
|
|
|
ir_reg_alloc_data ra_data;
|
|
|
|
uint32_t dessa_from_block;
|
|
|
|
dasm_State *dasm_state;
|
2023-11-30 19:10:41 +01:00
|
|
|
ir_bitset emit_constants;
|
2022-05-31 23:51:02 +02:00
|
|
|
int rodata_label, jmp_table_label;
|
|
|
|
} ir_backend_data;
|
|
|
|
|
|
|
|
#define IR_GP_REG_NAME(code, name64, name32) \
|
|
|
|
#name64,
|
|
|
|
#define IR_GP_REG_NAME32(code, name64, name32) \
|
|
|
|
#name32,
|
|
|
|
#define IR_FP_REG_NAME(code, name64, name32, name16, name8) \
|
|
|
|
#name64,
|
|
|
|
#define IR_FP_REG_NAME32(code, name64, name32, name16, name8) \
|
|
|
|
#name32,
|
|
|
|
|
|
|
|
static const char *_ir_reg_name[IR_REG_NUM] = {
|
|
|
|
IR_GP_REGS(IR_GP_REG_NAME)
|
|
|
|
IR_FP_REGS(IR_FP_REG_NAME)
|
|
|
|
};
|
|
|
|
|
|
|
|
static const char *_ir_reg_name32[IR_REG_NUM] = {
|
|
|
|
IR_GP_REGS(IR_GP_REG_NAME32)
|
|
|
|
IR_FP_REGS(IR_FP_REG_NAME32)
|
|
|
|
};
|
|
|
|
|
|
|
|
/* Calling Convention */
|
|
|
|
static const int8_t _ir_int_reg_params[IR_REG_INT_ARGS] = {
|
|
|
|
IR_REG_INT_ARG1,
|
|
|
|
IR_REG_INT_ARG2,
|
|
|
|
IR_REG_INT_ARG3,
|
|
|
|
IR_REG_INT_ARG4,
|
|
|
|
IR_REG_INT_ARG5,
|
|
|
|
IR_REG_INT_ARG6,
|
|
|
|
IR_REG_INT_ARG7,
|
|
|
|
IR_REG_INT_ARG8,
|
|
|
|
};
|
|
|
|
|
|
|
|
static const int8_t _ir_fp_reg_params[IR_REG_FP_ARGS] = {
|
|
|
|
IR_REG_FP_ARG1,
|
|
|
|
IR_REG_FP_ARG2,
|
|
|
|
IR_REG_FP_ARG3,
|
|
|
|
IR_REG_FP_ARG4,
|
|
|
|
IR_REG_FP_ARG5,
|
|
|
|
IR_REG_FP_ARG6,
|
|
|
|
IR_REG_FP_ARG7,
|
|
|
|
IR_REG_FP_ARG8,
|
|
|
|
};
|
|
|
|
|
|
|
|
const char *ir_reg_name(int8_t reg, ir_type type)
|
|
|
|
{
|
2023-03-21 11:45:37 +01:00
|
|
|
if (reg >= IR_REG_NUM) {
|
|
|
|
if (reg == IR_REG_SCRATCH) {
|
|
|
|
return "SCRATCH";
|
|
|
|
} else {
|
|
|
|
IR_ASSERT(reg == IR_REG_ALL);
|
|
|
|
return "ALL";
|
|
|
|
}
|
2022-08-11 18:56:59 +02:00
|
|
|
}
|
2022-05-31 23:51:02 +02:00
|
|
|
IR_ASSERT(reg >= 0 && reg < IR_REG_NUM);
|
2022-06-22 15:02:43 +02:00
|
|
|
if (type == IR_VOID) {
|
|
|
|
type = (reg < IR_REG_FP_FIRST) ? IR_ADDR : IR_DOUBLE;
|
|
|
|
}
|
2022-05-31 23:51:02 +02:00
|
|
|
if (ir_type_size[type] == 8) {
|
|
|
|
return _ir_reg_name[reg];
|
|
|
|
} else {
|
|
|
|
return _ir_reg_name32[reg];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-06-21 12:28:15 +02:00
|
|
|
#define IR_RULES(_) \
|
|
|
|
_(CMP_INT) \
|
|
|
|
_(CMP_FP) \
|
|
|
|
_(MUL_PWR2) \
|
|
|
|
_(DIV_PWR2) \
|
|
|
|
_(MOD_PWR2) \
|
|
|
|
_(OP_INT) \
|
|
|
|
_(OP_FP) \
|
|
|
|
_(BINOP_INT) \
|
|
|
|
_(BINOP_FP) \
|
|
|
|
_(SHIFT) \
|
|
|
|
_(SHIFT_CONST) \
|
|
|
|
_(COPY_INT) \
|
|
|
|
_(COPY_FP) \
|
|
|
|
_(CMP_AND_BRANCH_INT) \
|
|
|
|
_(CMP_AND_BRANCH_FP) \
|
|
|
|
_(GUARD_CMP_INT) \
|
|
|
|
_(GUARD_CMP_FP) \
|
|
|
|
_(GUARD_OVERFLOW) \
|
|
|
|
_(OVERFLOW_AND_BRANCH) \
|
|
|
|
_(MIN_MAX_INT) \
|
|
|
|
_(REG_BINOP_INT) \
|
|
|
|
_(LOAD_INT) \
|
|
|
|
_(LOAD_FP) \
|
|
|
|
_(STORE_INT) \
|
|
|
|
_(STORE_FP) \
|
|
|
|
_(IF_INT) \
|
|
|
|
_(RETURN_VOID) \
|
|
|
|
_(RETURN_INT) \
|
|
|
|
_(RETURN_FP) \
|
|
|
|
|
|
|
|
#define IR_RULE_ENUM(name) IR_ ## name,
|
|
|
|
|
2022-10-26 11:49:34 +02:00
|
|
|
enum _ir_rule {
|
2023-06-21 12:28:15 +02:00
|
|
|
IR_FIRST_RULE = IR_LAST_OP,
|
|
|
|
IR_RULES(IR_RULE_ENUM)
|
|
|
|
IR_LAST_RULE
|
|
|
|
};
|
|
|
|
|
|
|
|
#define IR_RULE_NAME(name) #name,
|
|
|
|
const char *ir_rule_name[IR_LAST_OP] = {
|
|
|
|
NULL,
|
|
|
|
IR_RULES(IR_RULE_NAME)
|
|
|
|
NULL
|
2022-10-26 11:49:34 +02:00
|
|
|
};
|
2022-05-31 23:51:02 +02:00
|
|
|
|
2022-10-26 21:52:19 +02:00
|
|
|
/* register allocation */
|
2023-03-28 12:18:12 +02:00
|
|
|
int ir_get_target_constraints(const ir_ctx *ctx, ir_ref ref, ir_target_constraints *constraints)
|
2022-05-31 23:51:02 +02:00
|
|
|
{
|
2022-10-26 11:49:34 +02:00
|
|
|
uint32_t rule = ir_rule(ctx, ref);
|
2023-03-28 12:18:12 +02:00
|
|
|
const ir_insn *insn;
|
2023-04-05 18:20:43 +02:00
|
|
|
int n = 0;
|
2022-11-17 21:30:35 +01:00
|
|
|
int flags = IR_USE_MUST_BE_IN_REG | IR_OP1_MUST_BE_IN_REG | IR_OP2_MUST_BE_IN_REG | IR_OP3_MUST_BE_IN_REG;
|
2022-05-31 23:51:02 +02:00
|
|
|
|
2023-04-05 18:20:43 +02:00
|
|
|
constraints->def_reg = IR_REG_NONE;
|
|
|
|
constraints->hints_count = 0;
|
|
|
|
switch (rule & IR_RULE_MASK) {
|
2022-05-31 23:51:02 +02:00
|
|
|
case IR_BINOP_INT:
|
|
|
|
insn = &ctx->ir_base[ref];
|
|
|
|
n = 0;
|
|
|
|
if (IR_IS_CONST_REF(insn->op1)) {
|
2022-11-17 21:30:35 +01:00
|
|
|
constraints->tmp_regs[n] = IR_TMP_REG(1, insn->type, IR_LOAD_SUB_REF, IR_DEF_SUB_REF);
|
2022-05-31 23:51:02 +02:00
|
|
|
n++;
|
|
|
|
}
|
|
|
|
if (IR_IS_CONST_REF(insn->op2) && insn->op1 != insn->op2) {
|
2023-03-28 12:18:12 +02:00
|
|
|
const ir_insn *val_insn = &ctx->ir_base[insn->op2];
|
2022-05-31 23:51:02 +02:00
|
|
|
switch (insn->op) {
|
|
|
|
case IR_ADD:
|
|
|
|
case IR_ADD_OV:
|
|
|
|
case IR_SUB:
|
|
|
|
case IR_SUB_OV:
|
2023-12-27 08:18:56 +01:00
|
|
|
if (IR_IS_SYM_CONST(val_insn->op) || !aarch64_may_encode_imm12(val_insn->val.u64)) {
|
2022-11-17 21:30:35 +01:00
|
|
|
constraints->tmp_regs[n] = IR_TMP_REG(2, insn->type, IR_LOAD_SUB_REF, IR_DEF_SUB_REF);
|
2022-05-31 23:51:02 +02:00
|
|
|
n++;
|
|
|
|
}
|
|
|
|
break;
|
2022-06-06 14:27:25 +02:00
|
|
|
case IR_MUL_OV:
|
2022-11-17 21:30:35 +01:00
|
|
|
constraints->tmp_regs[n] = IR_TMP_REG(2, insn->type, IR_LOAD_SUB_REF, IR_SAVE_SUB_REF);
|
2022-06-06 14:27:25 +02:00
|
|
|
n++;
|
|
|
|
break;
|
2022-05-31 23:51:02 +02:00
|
|
|
case IR_AND:
|
|
|
|
case IR_OR:
|
|
|
|
case IR_XOR:
|
2023-12-27 08:18:56 +01:00
|
|
|
if (IR_IS_SYM_CONST(val_insn->op) || !aarch64_may_encode_logical_imm(val_insn->val.u64, ir_type_size[insn->type])) {
|
2022-11-17 21:30:35 +01:00
|
|
|
constraints->tmp_regs[n] = IR_TMP_REG(2, insn->type, IR_LOAD_SUB_REF, IR_DEF_SUB_REF);
|
2022-05-31 23:51:02 +02:00
|
|
|
n++;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case IR_MUL:
|
2022-06-01 17:16:32 +02:00
|
|
|
case IR_DIV:
|
|
|
|
case IR_MOD:
|
2022-11-17 21:30:35 +01:00
|
|
|
constraints->tmp_regs[n] = IR_TMP_REG(2, insn->type, IR_LOAD_SUB_REF, IR_DEF_SUB_REF);
|
2022-05-31 23:51:02 +02:00
|
|
|
n++;
|
|
|
|
break;
|
|
|
|
}
|
2022-06-01 17:16:32 +02:00
|
|
|
}
|
|
|
|
if (insn->op == IR_MOD) {
|
2022-11-17 21:30:35 +01:00
|
|
|
constraints->tmp_regs[n] = IR_TMP_REG(3, insn->type, IR_LOAD_SUB_REF, IR_DEF_SUB_REF);
|
2022-06-01 17:16:32 +02:00
|
|
|
n++;
|
2022-06-06 14:27:25 +02:00
|
|
|
} else if (insn->op == IR_MUL_OV && (ir_type_size[insn->type] == 8 || IR_IS_TYPE_SIGNED(insn->type))) {
|
2022-11-17 21:30:35 +01:00
|
|
|
constraints->tmp_regs[n] = IR_TMP_REG(3, insn->type, IR_LOAD_SUB_REF, IR_SAVE_SUB_REF);
|
2022-06-06 14:27:25 +02:00
|
|
|
n++;
|
2022-06-01 17:16:32 +02:00
|
|
|
}
|
2022-11-17 21:30:35 +01:00
|
|
|
break;
|
|
|
|
case IR_SEXT:
|
|
|
|
case IR_ZEXT:
|
|
|
|
flags = IR_USE_MUST_BE_IN_REG | IR_OP1_SHOULD_BE_IN_REG;
|
|
|
|
IR_FALLTHROUGH;
|
2022-06-01 17:16:32 +02:00
|
|
|
case IR_MUL_PWR2:
|
|
|
|
case IR_DIV_PWR2:
|
|
|
|
case IR_MOD_PWR2:
|
|
|
|
case IR_SHIFT:
|
|
|
|
case IR_SHIFT_CONST:
|
|
|
|
case IR_OP_INT:
|
|
|
|
case IR_OP_FP:
|
2022-06-02 14:12:56 +02:00
|
|
|
case IR_INT2FP:
|
|
|
|
case IR_FP2INT:
|
|
|
|
case IR_FP2FP:
|
2022-06-01 17:16:32 +02:00
|
|
|
insn = &ctx->ir_base[ref];
|
|
|
|
n = 0;
|
|
|
|
if (IR_IS_CONST_REF(insn->op1)) {
|
2022-11-17 21:30:35 +01:00
|
|
|
constraints->tmp_regs[n] = IR_TMP_REG(1, insn->type, IR_LOAD_SUB_REF, IR_DEF_SUB_REF);
|
2022-06-01 17:16:32 +02:00
|
|
|
n++;
|
|
|
|
}
|
2022-06-02 14:12:56 +02:00
|
|
|
if (rule == IR_SHIFT && insn->op == IR_ROL) {
|
2022-11-17 21:30:35 +01:00
|
|
|
constraints->tmp_regs[n] = IR_TMP_REG(3, insn->type, IR_LOAD_SUB_REF, IR_DEF_SUB_REF);
|
2022-06-02 14:12:56 +02:00
|
|
|
n++;
|
|
|
|
}
|
2022-11-17 21:30:35 +01:00
|
|
|
break;
|
2023-11-16 17:28:12 +01:00
|
|
|
case IR_CTPOP:
|
|
|
|
flags = IR_USE_MUST_BE_IN_REG | IR_OP1_MUST_BE_IN_REG;
|
|
|
|
insn = &ctx->ir_base[ref];
|
|
|
|
constraints->tmp_regs[0] = IR_TMP_REG(2, IR_DOUBLE, IR_USE_SUB_REF, IR_SAVE_SUB_REF);
|
|
|
|
n = 1;
|
|
|
|
break;
|
2022-05-31 23:51:02 +02:00
|
|
|
case IR_BINOP_FP:
|
2022-06-01 17:16:32 +02:00
|
|
|
case IR_MIN_MAX_INT:
|
2022-05-31 23:51:02 +02:00
|
|
|
insn = &ctx->ir_base[ref];
|
|
|
|
n = 0;
|
|
|
|
if (IR_IS_CONST_REF(insn->op1)) {
|
2022-11-17 21:30:35 +01:00
|
|
|
constraints->tmp_regs[n] = IR_TMP_REG(1, insn->type, IR_LOAD_SUB_REF, IR_DEF_SUB_REF);
|
2022-05-31 23:51:02 +02:00
|
|
|
n++;
|
|
|
|
}
|
|
|
|
if (IR_IS_CONST_REF(insn->op2) && insn->op1 != insn->op2) {
|
2022-11-17 21:30:35 +01:00
|
|
|
constraints->tmp_regs[n] = IR_TMP_REG(2, insn->type, IR_LOAD_SUB_REF, IR_DEF_SUB_REF);
|
2022-05-31 23:51:02 +02:00
|
|
|
n++;
|
|
|
|
}
|
2022-11-17 21:30:35 +01:00
|
|
|
break;
|
2022-05-31 23:51:02 +02:00
|
|
|
case IR_CMP_INT:
|
|
|
|
insn = &ctx->ir_base[ref];
|
|
|
|
n = 0;
|
|
|
|
if (IR_IS_CONST_REF(insn->op1)) {
|
2022-11-17 21:30:35 +01:00
|
|
|
constraints->tmp_regs[n] = IR_TMP_REG(1, insn->type, IR_LOAD_SUB_REF, IR_DEF_SUB_REF);
|
2022-05-31 23:51:02 +02:00
|
|
|
n++;
|
|
|
|
}
|
|
|
|
if (IR_IS_CONST_REF(insn->op2) && insn->op1 != insn->op2) {
|
|
|
|
insn = &ctx->ir_base[insn->op2];
|
2023-12-27 08:18:56 +01:00
|
|
|
if (IR_IS_SYM_CONST(insn->op) || !aarch64_may_encode_imm12(insn->val.u64)) {
|
2022-11-17 21:30:35 +01:00
|
|
|
constraints->tmp_regs[n] = IR_TMP_REG(2, insn->type, IR_LOAD_SUB_REF, IR_DEF_SUB_REF);
|
2022-05-31 23:51:02 +02:00
|
|
|
n++;
|
|
|
|
}
|
|
|
|
}
|
2022-11-17 21:30:35 +01:00
|
|
|
break;
|
2022-05-31 23:51:02 +02:00
|
|
|
case IR_CMP_FP:
|
|
|
|
insn = &ctx->ir_base[ref];
|
2022-06-02 23:38:49 +02:00
|
|
|
n = 0;
|
2022-05-31 23:51:02 +02:00
|
|
|
if (IR_IS_CONST_REF(insn->op1)) {
|
2023-03-28 12:18:12 +02:00
|
|
|
const ir_insn *val_insn = &ctx->ir_base[insn->op1];
|
2022-11-17 21:30:35 +01:00
|
|
|
constraints->tmp_regs[n] = IR_TMP_REG(1, val_insn->type, IR_LOAD_SUB_REF, IR_DEF_SUB_REF);
|
2022-05-31 23:51:02 +02:00
|
|
|
n++;
|
|
|
|
}
|
|
|
|
if (IR_IS_CONST_REF(insn->op2) && insn->op1 != insn->op2) {
|
2023-03-28 12:18:12 +02:00
|
|
|
const ir_insn *val_insn = &ctx->ir_base[insn->op2];
|
2022-11-17 21:30:35 +01:00
|
|
|
constraints->tmp_regs[n] = IR_TMP_REG(2, val_insn->type, IR_LOAD_SUB_REF, IR_DEF_SUB_REF);
|
2022-05-31 23:51:02 +02:00
|
|
|
n++;
|
|
|
|
}
|
2022-11-17 21:30:35 +01:00
|
|
|
break;
|
2022-06-06 17:10:41 +02:00
|
|
|
case IR_VSTORE:
|
2022-05-31 23:51:02 +02:00
|
|
|
insn = &ctx->ir_base[ref];
|
|
|
|
if (IR_IS_CONST_REF(insn->op3)) {
|
|
|
|
insn = &ctx->ir_base[insn->op3];
|
2022-11-17 21:30:35 +01:00
|
|
|
constraints->tmp_regs[0] = IR_TMP_REG(3, insn->type, IR_LOAD_SUB_REF, IR_DEF_SUB_REF);
|
|
|
|
n = 1;
|
2022-05-31 23:51:02 +02:00
|
|
|
}
|
|
|
|
break;
|
2022-06-14 21:38:35 +02:00
|
|
|
case IR_LOAD_FP:
|
|
|
|
insn = &ctx->ir_base[ref];
|
|
|
|
n = 0;
|
|
|
|
if (IR_IS_CONST_REF(insn->op2)) {
|
|
|
|
IR_ASSERT(ctx->ir_base[insn->op2].type == IR_ADDR);
|
2022-11-17 21:30:35 +01:00
|
|
|
constraints->tmp_regs[n] = IR_TMP_REG(2, IR_ADDR, IR_LOAD_SUB_REF, IR_DEF_SUB_REF);
|
2022-06-14 21:38:35 +02:00
|
|
|
n++;
|
|
|
|
}
|
2022-11-17 21:30:35 +01:00
|
|
|
break;
|
2022-06-14 21:38:35 +02:00
|
|
|
case IR_STORE_INT:
|
|
|
|
case IR_STORE_FP:
|
|
|
|
insn = &ctx->ir_base[ref];
|
|
|
|
n = 0;
|
|
|
|
if (IR_IS_CONST_REF(insn->op2)) {
|
|
|
|
IR_ASSERT(ctx->ir_base[insn->op2].type == IR_ADDR);
|
2022-11-17 21:30:35 +01:00
|
|
|
constraints->tmp_regs[n] = IR_TMP_REG(2, IR_ADDR, IR_LOAD_SUB_REF, IR_DEF_SUB_REF);
|
2022-06-14 21:38:35 +02:00
|
|
|
n++;
|
|
|
|
}
|
|
|
|
if (IR_IS_CONST_REF(insn->op3)) {
|
|
|
|
insn = &ctx->ir_base[insn->op3];
|
2023-12-27 08:18:56 +01:00
|
|
|
if (!IR_IS_TYPE_INT(insn->type) || IR_IS_SYM_CONST(insn->op) || insn->val.i64 != 0) {
|
2022-11-17 21:30:35 +01:00
|
|
|
constraints->tmp_regs[n] = IR_TMP_REG(3, insn->type, IR_LOAD_SUB_REF, IR_DEF_SUB_REF);
|
2022-06-28 11:24:50 +02:00
|
|
|
n++;
|
|
|
|
}
|
2022-06-14 21:38:35 +02:00
|
|
|
}
|
2022-11-17 21:30:35 +01:00
|
|
|
break;
|
2022-05-31 23:51:02 +02:00
|
|
|
case IR_SWITCH:
|
|
|
|
insn = &ctx->ir_base[ref];
|
|
|
|
n = 0;
|
|
|
|
if (IR_IS_CONST_REF(insn->op2)) {
|
|
|
|
insn = &ctx->ir_base[insn->op2];
|
2022-11-17 21:30:35 +01:00
|
|
|
constraints->tmp_regs[n] = IR_TMP_REG(2, insn->type, IR_LOAD_SUB_REF, IR_DEF_SUB_REF);
|
2022-05-31 23:51:02 +02:00
|
|
|
n++;
|
2022-06-06 14:27:25 +02:00
|
|
|
} else {
|
|
|
|
insn = &ctx->ir_base[insn->op2];
|
2022-11-17 21:30:35 +01:00
|
|
|
constraints->tmp_regs[n] = IR_TMP_REG(1, insn->type, IR_LOAD_SUB_REF, IR_DEF_SUB_REF);
|
2022-05-31 23:51:02 +02:00
|
|
|
n++;
|
|
|
|
}
|
2022-11-17 21:30:35 +01:00
|
|
|
constraints->tmp_regs[n] = IR_TMP_REG(3, IR_ADDR, IR_LOAD_SUB_REF, IR_DEF_SUB_REF);
|
2022-06-06 14:27:25 +02:00
|
|
|
n++;
|
2022-11-17 21:30:35 +01:00
|
|
|
break;
|
2022-05-31 23:51:02 +02:00
|
|
|
case IR_CALL:
|
2022-11-17 21:30:35 +01:00
|
|
|
insn = &ctx->ir_base[ref];
|
2023-04-05 18:20:43 +02:00
|
|
|
constraints->def_reg = (IR_IS_TYPE_INT(insn->type)) ? IR_REG_INT_RET1 : IR_REG_FP_RET1;
|
2023-03-21 11:45:37 +01:00
|
|
|
constraints->tmp_regs[0] = IR_SCRATCH_REG(IR_REG_SCRATCH, IR_USE_SUB_REF, IR_DEF_SUB_REF);
|
2022-11-17 21:30:35 +01:00
|
|
|
n = 1;
|
|
|
|
IR_FALLTHROUGH;
|
2022-05-31 23:51:02 +02:00
|
|
|
case IR_TAILCALL:
|
2022-11-17 21:30:35 +01:00
|
|
|
insn = &ctx->ir_base[ref];
|
|
|
|
if (insn->inputs_count > 2) {
|
|
|
|
constraints->hints[2] = IR_REG_NONE;
|
2023-04-05 18:20:43 +02:00
|
|
|
constraints->hints_count = ir_get_args_regs(ctx, insn, constraints->hints);
|
2023-04-26 13:10:58 +02:00
|
|
|
if (!IR_IS_CONST_REF(insn->op2)) {
|
|
|
|
constraints->tmp_regs[n] = IR_TMP_REG(1, IR_ADDR, IR_LOAD_SUB_REF, IR_USE_SUB_REF);
|
|
|
|
n++;
|
|
|
|
}
|
2022-11-17 21:30:35 +01:00
|
|
|
}
|
2023-06-21 00:14:31 +02:00
|
|
|
flags = IR_USE_SHOULD_BE_IN_REG | IR_OP2_SHOULD_BE_IN_REG | IR_OP3_SHOULD_BE_IN_REG;
|
2022-11-17 21:30:35 +01:00
|
|
|
break;
|
2023-10-24 09:22:04 +02:00
|
|
|
case IR_COND:
|
|
|
|
insn = &ctx->ir_base[ref];
|
|
|
|
n = 0;
|
|
|
|
if (IR_IS_CONST_REF(insn->op1)) {
|
|
|
|
constraints->tmp_regs[n] = IR_TMP_REG(1, ctx->ir_base[insn->op1].type, IR_LOAD_SUB_REF, IR_DEF_SUB_REF);
|
|
|
|
n++;
|
|
|
|
}
|
|
|
|
if (IR_IS_CONST_REF(insn->op2)) {
|
|
|
|
constraints->tmp_regs[n] = IR_TMP_REG(2, insn->type, IR_LOAD_SUB_REF, IR_DEF_SUB_REF);
|
|
|
|
n++;
|
|
|
|
}
|
|
|
|
if (IR_IS_CONST_REF(insn->op3)) {
|
|
|
|
constraints->tmp_regs[n] = IR_TMP_REG(3, insn->type, IR_LOAD_SUB_REF, IR_DEF_SUB_REF);
|
|
|
|
n++;
|
|
|
|
}
|
|
|
|
break;
|
2022-11-17 21:30:35 +01:00
|
|
|
case IR_COPY_INT:
|
|
|
|
case IR_COPY_FP:
|
|
|
|
flags = IR_DEF_REUSES_OP1_REG | IR_USE_MUST_BE_IN_REG;
|
|
|
|
break;
|
|
|
|
case IR_PARAM:
|
2023-04-05 18:20:43 +02:00
|
|
|
constraints->def_reg = ir_get_param_reg(ctx, ref);
|
2023-06-08 23:35:15 +02:00
|
|
|
flags = 0;
|
2022-11-17 21:30:35 +01:00
|
|
|
break;
|
|
|
|
case IR_PI:
|
|
|
|
case IR_PHI:
|
2023-05-19 11:34:54 +02:00
|
|
|
flags = IR_USE_SHOULD_BE_IN_REG;
|
|
|
|
break;
|
2022-11-17 21:30:35 +01:00
|
|
|
case IR_RLOAD:
|
2023-05-19 11:34:54 +02:00
|
|
|
constraints->def_reg = ctx->ir_base[ref].op2;
|
2022-11-17 21:30:35 +01:00
|
|
|
flags = IR_USE_SHOULD_BE_IN_REG;
|
|
|
|
break;
|
|
|
|
case IR_EXITCALL:
|
2023-04-05 18:20:43 +02:00
|
|
|
constraints->def_reg = IR_REG_INT_RET1;
|
2022-11-17 21:30:35 +01:00
|
|
|
break;
|
|
|
|
case IR_TRUNC:
|
|
|
|
case IR_BITCAST:
|
2023-11-30 19:10:41 +01:00
|
|
|
case IR_PROTO:
|
2022-11-17 21:30:35 +01:00
|
|
|
flags = IR_USE_MUST_BE_IN_REG | IR_OP1_SHOULD_BE_IN_REG;
|
|
|
|
break;
|
|
|
|
case IR_RSTORE:
|
|
|
|
flags = IR_OP3_SHOULD_BE_IN_REG;
|
|
|
|
break;
|
|
|
|
case IR_RETURN_INT:
|
|
|
|
flags = IR_OP2_SHOULD_BE_IN_REG;
|
|
|
|
constraints->hints[2] = IR_REG_INT_RET1;
|
2023-04-05 18:20:43 +02:00
|
|
|
constraints->hints_count = 3;
|
2022-11-17 21:30:35 +01:00
|
|
|
break;
|
|
|
|
case IR_RETURN_FP:
|
|
|
|
flags = IR_OP2_SHOULD_BE_IN_REG;
|
|
|
|
constraints->hints[2] = IR_REG_FP_RET1;
|
2023-04-05 18:20:43 +02:00
|
|
|
constraints->hints_count = 3;
|
2022-11-17 21:30:35 +01:00
|
|
|
break;
|
|
|
|
case IR_SNAPSHOT:
|
|
|
|
flags = 0;
|
2022-05-31 23:51:02 +02:00
|
|
|
break;
|
2023-11-23 17:38:33 +01:00
|
|
|
case IR_VA_START:
|
|
|
|
flags = IR_OP1_MUST_BE_IN_REG;
|
|
|
|
constraints->tmp_regs[0] = IR_TMP_REG(3, IR_ADDR, IR_LOAD_SUB_REF, IR_DEF_SUB_REF);
|
|
|
|
n = 1;
|
|
|
|
break;
|
|
|
|
case IR_VA_ARG:
|
|
|
|
flags = IR_USE_MUST_BE_IN_REG | IR_OP1_MUST_BE_IN_REG;
|
|
|
|
constraints->tmp_regs[0] = IR_TMP_REG(3, IR_ADDR, IR_LOAD_SUB_REF, IR_SAVE_SUB_REF);
|
|
|
|
n = 1;
|
|
|
|
break;
|
2022-05-31 23:51:02 +02:00
|
|
|
}
|
2022-11-17 21:30:35 +01:00
|
|
|
constraints->tmps_count = n;
|
|
|
|
|
|
|
|
return flags;
|
2022-05-31 23:51:02 +02:00
|
|
|
}
|
|
|
|
|
2022-10-26 21:52:19 +02:00
|
|
|
/* instruction selection */
|
2022-09-16 11:05:36 +02:00
|
|
|
static void ir_match_fuse_addr(ir_ctx *ctx, ir_ref addr_ref, ir_type type)
|
|
|
|
{
|
|
|
|
if (!IR_IS_CONST_REF(addr_ref)) {
|
|
|
|
ir_insn *addr_insn = &ctx->ir_base[addr_ref];
|
|
|
|
|
|
|
|
if (addr_insn->op == IR_ADD
|
|
|
|
&& !IR_IS_CONST_REF(addr_insn->op1)
|
|
|
|
&& IR_IS_CONST_REF(addr_insn->op2) // TODO: temporary workaround
|
2023-12-27 08:18:56 +01:00
|
|
|
&& !IR_IS_SYM_CONST(ctx->ir_base[addr_insn->op2].op)
|
2022-09-16 11:05:36 +02:00
|
|
|
&& aarch64_may_encode_addr_offset(ctx->ir_base[addr_insn->op2].val.i64, ir_type_size[type])) {
|
|
|
|
ir_use_list *use_list = &ctx->use_lists[addr_ref];
|
|
|
|
ir_ref j = use_list->count;
|
|
|
|
|
|
|
|
if (j > 1) {
|
|
|
|
/* check if address is used only in LOAD and STORE */
|
|
|
|
ir_ref *p = &ctx->use_edges[use_list->refs];
|
|
|
|
|
|
|
|
do {
|
|
|
|
ir_insn *insn = &ctx->ir_base[*p];
|
|
|
|
if (insn->op != IR_LOAD && (insn->op != IR_STORE || insn->op3 == addr_ref)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
p++;
|
|
|
|
} while (--j);
|
|
|
|
}
|
2023-04-05 18:20:43 +02:00
|
|
|
ctx->rules[addr_ref] = IR_FUSED | IR_SIMPLE | addr_insn->op;
|
2022-09-16 11:05:36 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-03-29 00:21:54 +02:00
|
|
|
static uint32_t ir_match_insn(ir_ctx *ctx, ir_ref ref)
|
2022-05-31 23:51:02 +02:00
|
|
|
{
|
|
|
|
ir_insn *op2_insn;
|
|
|
|
ir_insn *insn = &ctx->ir_base[ref];
|
|
|
|
|
|
|
|
switch (insn->op) {
|
|
|
|
case IR_EQ:
|
|
|
|
case IR_NE:
|
|
|
|
case IR_LT:
|
|
|
|
case IR_GE:
|
|
|
|
case IR_LE:
|
|
|
|
case IR_GT:
|
|
|
|
case IR_ULT:
|
|
|
|
case IR_UGE:
|
|
|
|
case IR_ULE:
|
|
|
|
case IR_UGT:
|
|
|
|
if (IR_IS_TYPE_INT(ctx->ir_base[insn->op1].type)) {
|
|
|
|
return IR_CMP_INT;
|
|
|
|
} else {
|
|
|
|
return IR_CMP_FP;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case IR_ADD:
|
|
|
|
case IR_SUB:
|
|
|
|
if (IR_IS_TYPE_INT(insn->type)) {
|
|
|
|
if ((ctx->flags & IR_OPT_CODEGEN) && IR_IS_CONST_REF(insn->op2)) {
|
|
|
|
op2_insn = &ctx->ir_base[insn->op2];
|
2023-12-27 08:18:56 +01:00
|
|
|
if (IR_IS_SYM_CONST(op2_insn->op)) {
|
|
|
|
/* pass */
|
|
|
|
} else if (IR_IS_CONST_REF(insn->op1)) {
|
2022-05-31 23:51:02 +02:00
|
|
|
// const
|
|
|
|
} else if (op2_insn->val.i64 == 0) {
|
|
|
|
return IR_COPY_INT;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
binop_int:
|
|
|
|
return IR_BINOP_INT;
|
|
|
|
} else {
|
|
|
|
binop_fp:
|
|
|
|
return IR_BINOP_FP;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case IR_MUL:
|
|
|
|
if (IR_IS_TYPE_INT(insn->type)) {
|
|
|
|
if ((ctx->flags & IR_OPT_CODEGEN) && IR_IS_CONST_REF(insn->op2)) {
|
|
|
|
op2_insn = &ctx->ir_base[insn->op2];
|
2023-12-27 08:18:56 +01:00
|
|
|
if (IR_IS_SYM_CONST(op2_insn->op)) {
|
|
|
|
/* pass */
|
|
|
|
} else if (IR_IS_CONST_REF(insn->op1)) {
|
2022-05-31 23:51:02 +02:00
|
|
|
// const
|
|
|
|
} else if (op2_insn->val.u64 == 0) {
|
|
|
|
// 0
|
|
|
|
} else if (op2_insn->val.u64 == 1) {
|
|
|
|
return IR_COPY_INT;
|
|
|
|
} else if (IR_IS_POWER_OF_TWO(op2_insn->val.u64)) {
|
2022-06-06 17:10:41 +02:00
|
|
|
return IR_MUL_PWR2;
|
2022-05-31 23:51:02 +02:00
|
|
|
}
|
|
|
|
}
|
2022-06-01 17:16:32 +02:00
|
|
|
return IR_BINOP_INT;
|
2022-05-31 23:51:02 +02:00
|
|
|
} else {
|
|
|
|
goto binop_fp;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case IR_ADD_OV:
|
|
|
|
case IR_SUB_OV:
|
|
|
|
IR_ASSERT(IR_IS_TYPE_INT(insn->type));
|
|
|
|
goto binop_int;
|
|
|
|
case IR_MUL_OV:
|
|
|
|
IR_ASSERT(IR_IS_TYPE_INT(insn->type));
|
2022-06-01 17:16:32 +02:00
|
|
|
goto binop_int;
|
2022-05-31 23:51:02 +02:00
|
|
|
case IR_DIV:
|
|
|
|
if (IR_IS_TYPE_INT(insn->type)) {
|
|
|
|
if ((ctx->flags & IR_OPT_CODEGEN) && IR_IS_CONST_REF(insn->op2)) {
|
|
|
|
op2_insn = &ctx->ir_base[insn->op2];
|
2023-12-27 08:18:56 +01:00
|
|
|
if (IR_IS_SYM_CONST(op2_insn->op)) {
|
|
|
|
/* pass */
|
|
|
|
} else if (IR_IS_CONST_REF(insn->op1)) {
|
2022-05-31 23:51:02 +02:00
|
|
|
// const
|
|
|
|
} else if (op2_insn->val.u64 == 1) {
|
|
|
|
return IR_COPY_INT;
|
2023-12-15 12:09:26 +01:00
|
|
|
} else if (IR_IS_TYPE_UNSIGNED(insn->type) && IR_IS_POWER_OF_TWO(op2_insn->val.u64)) {
|
|
|
|
// TODO: signed division by power of two ???
|
2022-06-06 17:10:41 +02:00
|
|
|
return IR_DIV_PWR2;
|
2022-05-31 23:51:02 +02:00
|
|
|
}
|
|
|
|
}
|
2022-06-01 17:16:32 +02:00
|
|
|
return IR_BINOP_INT;
|
2022-05-31 23:51:02 +02:00
|
|
|
} else {
|
|
|
|
goto binop_fp;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case IR_MOD:
|
|
|
|
if ((ctx->flags & IR_OPT_CODEGEN) && IR_IS_CONST_REF(insn->op2)) {
|
|
|
|
op2_insn = &ctx->ir_base[insn->op2];
|
2023-12-27 08:18:56 +01:00
|
|
|
if (IR_IS_SYM_CONST(op2_insn->op)) {
|
|
|
|
/* pass */
|
|
|
|
} else if (IR_IS_CONST_REF(insn->op1)) {
|
2022-05-31 23:51:02 +02:00
|
|
|
// const
|
2022-09-19 22:22:11 +02:00
|
|
|
} else if (IR_IS_TYPE_UNSIGNED(insn->type) && IR_IS_POWER_OF_TWO(op2_insn->val.u64)) {
|
2023-12-15 12:09:26 +01:00
|
|
|
// TODO: signed division by power of two ???
|
2022-06-06 17:10:41 +02:00
|
|
|
return IR_MOD_PWR2;
|
2022-05-31 23:51:02 +02:00
|
|
|
}
|
|
|
|
}
|
2022-06-01 17:16:32 +02:00
|
|
|
return IR_BINOP_INT;
|
2022-05-31 23:51:02 +02:00
|
|
|
case IR_BSWAP:
|
|
|
|
case IR_NOT:
|
2023-11-16 20:48:05 +01:00
|
|
|
case IR_CTLZ:
|
|
|
|
case IR_CTTZ:
|
2023-07-27 10:16:00 +02:00
|
|
|
IR_ASSERT(IR_IS_TYPE_INT(insn->type));
|
|
|
|
return IR_OP_INT;
|
2022-05-31 23:51:02 +02:00
|
|
|
case IR_NEG:
|
|
|
|
case IR_ABS:
|
|
|
|
if (IR_IS_TYPE_INT(insn->type)) {
|
2022-06-01 17:16:32 +02:00
|
|
|
return IR_OP_INT;
|
2022-05-31 23:51:02 +02:00
|
|
|
} else {
|
|
|
|
return IR_OP_FP;
|
|
|
|
}
|
|
|
|
case IR_OR:
|
|
|
|
if ((ctx->flags & IR_OPT_CODEGEN) && IR_IS_CONST_REF(insn->op2)) {
|
|
|
|
op2_insn = &ctx->ir_base[insn->op2];
|
2023-12-27 08:18:56 +01:00
|
|
|
if (IR_IS_SYM_CONST(op2_insn->op)) {
|
|
|
|
/* pass */
|
|
|
|
} else if (IR_IS_CONST_REF(insn->op1)) {
|
2022-05-31 23:51:02 +02:00
|
|
|
// const
|
|
|
|
} else if (op2_insn->val.i64 == 0) {
|
|
|
|
return IR_COPY_INT;
|
|
|
|
} else if (op2_insn->val.i64 == -1) {
|
|
|
|
// -1
|
|
|
|
}
|
|
|
|
}
|
|
|
|
goto binop_int;
|
|
|
|
case IR_AND:
|
|
|
|
if ((ctx->flags & IR_OPT_CODEGEN) && IR_IS_CONST_REF(insn->op2)) {
|
|
|
|
op2_insn = &ctx->ir_base[insn->op2];
|
2023-12-27 08:18:56 +01:00
|
|
|
if (IR_IS_SYM_CONST(op2_insn->op)) {
|
|
|
|
/* pass */
|
|
|
|
} else if (IR_IS_CONST_REF(insn->op1)) {
|
2022-05-31 23:51:02 +02:00
|
|
|
// const
|
|
|
|
} else if (op2_insn->val.i64 == 0) {
|
|
|
|
// 0
|
|
|
|
} else if (op2_insn->val.i64 == -1) {
|
|
|
|
return IR_COPY_INT;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
goto binop_int;
|
|
|
|
case IR_XOR:
|
|
|
|
if ((ctx->flags & IR_OPT_CODEGEN) && IR_IS_CONST_REF(insn->op2)) {
|
|
|
|
op2_insn = &ctx->ir_base[insn->op2];
|
2023-12-27 08:18:56 +01:00
|
|
|
if (IR_IS_SYM_CONST(op2_insn->op)) {
|
|
|
|
/* pass */
|
|
|
|
} else if (IR_IS_CONST_REF(insn->op1)) {
|
2022-05-31 23:51:02 +02:00
|
|
|
// const
|
|
|
|
}
|
|
|
|
}
|
|
|
|
goto binop_int;
|
|
|
|
case IR_SHL:
|
|
|
|
if (IR_IS_CONST_REF(insn->op2)) {
|
|
|
|
if (ctx->flags & IR_OPT_CODEGEN) {
|
|
|
|
op2_insn = &ctx->ir_base[insn->op2];
|
2023-12-27 08:18:56 +01:00
|
|
|
if (IR_IS_SYM_CONST(op2_insn->op)) {
|
|
|
|
/* pass */
|
|
|
|
} else if (IR_IS_CONST_REF(insn->op1)) {
|
2022-05-31 23:51:02 +02:00
|
|
|
// const
|
|
|
|
} else if (op2_insn->val.u64 == 0) {
|
|
|
|
return IR_COPY_INT;
|
|
|
|
} else if (ir_type_size[insn->type] >= 4) {
|
|
|
|
if (op2_insn->val.u64 == 1) {
|
|
|
|
// lea [op1*2]
|
|
|
|
} else if (op2_insn->val.u64 == 2) {
|
|
|
|
// lea [op1*4]
|
|
|
|
} else if (op2_insn->val.u64 == 3) {
|
|
|
|
// lea [op1*8]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return IR_SHIFT_CONST;
|
|
|
|
}
|
|
|
|
return IR_SHIFT;
|
|
|
|
case IR_SHR:
|
|
|
|
case IR_SAR:
|
|
|
|
case IR_ROL:
|
|
|
|
case IR_ROR:
|
|
|
|
if (IR_IS_CONST_REF(insn->op2)) {
|
|
|
|
if (ctx->flags & IR_OPT_CODEGEN) {
|
|
|
|
op2_insn = &ctx->ir_base[insn->op2];
|
2023-12-27 08:18:56 +01:00
|
|
|
if (IR_IS_SYM_CONST(op2_insn->op)) {
|
|
|
|
/* pass */
|
|
|
|
} else if (IR_IS_CONST_REF(insn->op1)) {
|
2022-05-31 23:51:02 +02:00
|
|
|
// const
|
|
|
|
} else if (op2_insn->val.u64 == 0) {
|
|
|
|
return IR_COPY_INT;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return IR_SHIFT_CONST;
|
|
|
|
}
|
|
|
|
return IR_SHIFT;
|
|
|
|
case IR_MIN:
|
|
|
|
case IR_MAX:
|
|
|
|
if (IR_IS_TYPE_INT(insn->type)) {
|
|
|
|
return IR_MIN_MAX_INT;
|
|
|
|
} else {
|
|
|
|
goto binop_fp;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
// case IR_COND:
|
|
|
|
case IR_COPY:
|
|
|
|
if (IR_IS_TYPE_INT(insn->type)) {
|
|
|
|
return IR_COPY_INT;
|
|
|
|
} else {
|
|
|
|
return IR_COPY_FP;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case IR_CALL:
|
2023-11-17 12:24:33 +01:00
|
|
|
ctx->flags2 |= IR_HAS_CALLS;
|
2022-05-31 23:51:02 +02:00
|
|
|
return IR_CALL;
|
|
|
|
case IR_VAR:
|
2023-05-18 20:00:57 +02:00
|
|
|
return IR_SKIPPED | IR_VAR;
|
2023-06-05 17:22:12 +02:00
|
|
|
case IR_PARAM:
|
|
|
|
return ctx->use_lists[ref].count > 0 ? IR_PARAM : IR_SKIPPED | IR_PARAM;
|
2022-05-31 23:51:02 +02:00
|
|
|
case IR_ALLOCA:
|
2022-12-07 11:09:00 +01:00
|
|
|
if (ctx->flags & IR_FUNCTION) {
|
2023-11-17 12:24:33 +01:00
|
|
|
ctx->flags |= IR_USE_FRAME_POINTER;
|
|
|
|
ctx->flags2 |= IR_HAS_ALLOCA;
|
2022-12-07 11:09:00 +01:00
|
|
|
}
|
2022-05-31 23:51:02 +02:00
|
|
|
return IR_ALLOCA;
|
|
|
|
case IR_LOAD:
|
2022-09-16 11:05:36 +02:00
|
|
|
ir_match_fuse_addr(ctx, insn->op2, insn->type);
|
2022-06-28 11:24:50 +02:00
|
|
|
if (IR_IS_TYPE_INT(insn->type)) {
|
2022-05-31 23:51:02 +02:00
|
|
|
return IR_LOAD_INT;
|
|
|
|
} else {
|
|
|
|
return IR_LOAD_FP;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case IR_STORE:
|
2022-09-16 11:05:36 +02:00
|
|
|
ir_match_fuse_addr(ctx, insn->op2, ctx->ir_base[insn->op3].type);
|
2022-05-31 23:51:02 +02:00
|
|
|
if (IR_IS_TYPE_INT(ctx->ir_base[insn->op3].type)) {
|
2022-06-01 17:16:32 +02:00
|
|
|
return IR_STORE_INT;
|
2022-05-31 23:51:02 +02:00
|
|
|
} else {
|
|
|
|
return IR_STORE_FP;
|
|
|
|
}
|
|
|
|
break;
|
2023-05-18 12:37:12 +02:00
|
|
|
case IR_RLOAD:
|
2023-06-09 09:58:58 +02:00
|
|
|
if (IR_REGSET_IN(IR_REGSET_UNION((ir_regset)ctx->fixed_regset, IR_REGSET_FIXED), insn->op2)) {
|
2023-05-18 12:37:12 +02:00
|
|
|
return IR_SKIPPED | IR_RLOAD;
|
|
|
|
}
|
|
|
|
return IR_RLOAD;
|
2022-06-28 15:57:35 +02:00
|
|
|
case IR_RSTORE:
|
|
|
|
if (IR_IS_TYPE_INT(ctx->ir_base[insn->op2].type)) {
|
2023-03-29 00:21:54 +02:00
|
|
|
if ((ctx->flags & IR_OPT_CODEGEN) && ir_in_same_block(ctx, insn->op2) && ctx->use_lists[insn->op2].count == 1) {
|
2022-06-28 15:57:35 +02:00
|
|
|
ir_insn *op_insn = &ctx->ir_base[insn->op2];
|
|
|
|
|
|
|
|
if (!ctx->rules[insn->op2]) {
|
2023-03-29 00:21:54 +02:00
|
|
|
ctx->rules[insn->op2] = ir_match_insn(ctx, insn->op2);
|
2022-06-28 15:57:35 +02:00
|
|
|
}
|
|
|
|
if (ctx->rules[insn->op2] == IR_BINOP_INT) {
|
|
|
|
if (ctx->ir_base[op_insn->op1].op == IR_RLOAD
|
|
|
|
&& ctx->ir_base[op_insn->op1].op2 == insn->op3) {
|
2023-04-05 18:20:43 +02:00
|
|
|
ctx->rules[insn->op2] = IR_FUSED | IR_BINOP_INT;
|
|
|
|
ctx->rules[op_insn->op1] = IR_SKIPPED | IR_RLOAD;
|
2022-06-28 15:57:35 +02:00
|
|
|
return IR_REG_BINOP_INT;
|
|
|
|
} else if ((ir_op_flags[op_insn->op] & IR_OP_FLAG_COMMUTATIVE)
|
|
|
|
&& ctx->ir_base[op_insn->op2].op == IR_RLOAD
|
|
|
|
&& ctx->ir_base[op_insn->op2].op2 == insn->op3) {
|
|
|
|
ir_ref tmp = op_insn->op1;
|
|
|
|
op_insn->op1 = op_insn->op2;
|
|
|
|
op_insn->op2 = tmp;
|
2023-04-05 18:20:43 +02:00
|
|
|
ctx->rules[insn->op2] = IR_FUSED | IR_BINOP_INT;
|
|
|
|
ctx->rules[op_insn->op1] = IR_SKIPPED | IR_RLOAD;
|
2022-06-28 15:57:35 +02:00
|
|
|
return IR_REG_BINOP_INT;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return IR_RSTORE;
|
2022-05-31 23:51:02 +02:00
|
|
|
case IR_START:
|
|
|
|
case IR_BEGIN:
|
|
|
|
case IR_IF_TRUE:
|
|
|
|
case IR_IF_FALSE:
|
|
|
|
case IR_CASE_VAL:
|
|
|
|
case IR_CASE_DEFAULT:
|
|
|
|
case IR_MERGE:
|
|
|
|
case IR_LOOP_BEGIN:
|
|
|
|
case IR_UNREACHABLE:
|
2023-04-05 18:20:43 +02:00
|
|
|
return IR_SKIPPED | insn->op;
|
2022-05-31 23:51:02 +02:00
|
|
|
case IR_RETURN:
|
|
|
|
if (!insn->op2) {
|
|
|
|
return IR_RETURN_VOID;
|
|
|
|
} else if (IR_IS_TYPE_INT(ctx->ir_base[insn->op2].type)) {
|
|
|
|
return IR_RETURN_INT;
|
|
|
|
} else {
|
|
|
|
return IR_RETURN_FP;
|
|
|
|
}
|
|
|
|
case IR_IF:
|
2023-03-29 00:21:54 +02:00
|
|
|
if (ir_in_same_block(ctx, insn->op2) && ctx->use_lists[insn->op2].count == 1) {
|
2022-05-31 23:51:02 +02:00
|
|
|
op2_insn = &ctx->ir_base[insn->op2];
|
|
|
|
if (op2_insn->op >= IR_EQ && op2_insn->op <= IR_UGT) {
|
|
|
|
if (IR_IS_TYPE_INT(ctx->ir_base[op2_insn->op1].type)) {
|
2023-04-05 18:20:43 +02:00
|
|
|
ctx->rules[insn->op2] = IR_FUSED | IR_CMP_INT;
|
2022-05-31 23:51:02 +02:00
|
|
|
return IR_CMP_AND_BRANCH_INT;
|
|
|
|
} else {
|
2023-04-05 18:20:43 +02:00
|
|
|
ctx->rules[insn->op2] = IR_FUSED | IR_CMP_FP;
|
2022-05-31 23:51:02 +02:00
|
|
|
return IR_CMP_AND_BRANCH_FP;
|
|
|
|
}
|
|
|
|
} else if (op2_insn->op == IR_OVERFLOW) {
|
2023-04-05 18:20:43 +02:00
|
|
|
ctx->rules[insn->op2] = IR_FUSED | IR_SIMPLE | IR_OVERFLOW;
|
2022-05-31 23:51:02 +02:00
|
|
|
return IR_OVERFLOW_AND_BRANCH;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (IR_IS_TYPE_INT(ctx->ir_base[insn->op2].type)) {
|
|
|
|
return IR_IF_INT;
|
|
|
|
} else {
|
|
|
|
IR_ASSERT(0 && "NIY IR_IF_FP");
|
2023-04-12 09:48:30 +02:00
|
|
|
break;
|
2022-05-31 23:51:02 +02:00
|
|
|
}
|
2022-09-02 12:54:31 +02:00
|
|
|
case IR_GUARD:
|
|
|
|
case IR_GUARD_NOT:
|
2023-03-29 00:21:54 +02:00
|
|
|
if (ir_in_same_block(ctx, insn->op2) && ctx->use_lists[insn->op2].count == 1) {
|
2022-06-28 15:36:06 +02:00
|
|
|
op2_insn = &ctx->ir_base[insn->op2];
|
2022-10-25 21:09:32 +02:00
|
|
|
if (op2_insn->op >= IR_EQ && op2_insn->op <= IR_UGT
|
|
|
|
// TODO: register allocator may clobber operands of CMP before they are used in the GUARD_CMP
|
|
|
|
&& (insn->op2 == ref - 1 ||
|
2022-11-18 11:59:49 +01:00
|
|
|
(insn->op2 == ctx->prev_ref[ref] - 1
|
|
|
|
&& ctx->ir_base[ctx->prev_ref[ref]].op == IR_SNAPSHOT))) {
|
2022-06-28 15:36:06 +02:00
|
|
|
if (IR_IS_TYPE_INT(ctx->ir_base[op2_insn->op1].type)) {
|
2023-04-05 18:20:43 +02:00
|
|
|
ctx->rules[insn->op2] = IR_FUSED | IR_CMP_INT;
|
2022-06-28 15:36:06 +02:00
|
|
|
return IR_GUARD_CMP_INT;
|
2022-09-26 19:47:29 +02:00
|
|
|
} else {
|
2023-04-05 18:20:43 +02:00
|
|
|
ctx->rules[insn->op2] = IR_FUSED | IR_CMP_FP;
|
2022-09-26 19:47:29 +02:00
|
|
|
return IR_GUARD_CMP_FP;
|
2022-06-28 15:36:06 +02:00
|
|
|
}
|
2022-09-26 19:47:29 +02:00
|
|
|
} else if (op2_insn->op == IR_OVERFLOW) {
|
2023-04-05 18:20:43 +02:00
|
|
|
ctx->rules[insn->op2] = IR_FUSED | IR_SIMPLE | IR_OVERFLOW;
|
2022-09-26 19:47:29 +02:00
|
|
|
return IR_GUARD_OVERFLOW;
|
2022-06-28 15:36:06 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return insn->op;
|
2023-11-23 17:38:33 +01:00
|
|
|
case IR_VA_START:
|
|
|
|
ctx->flags2 |= IR_HAS_VA_START;
|
|
|
|
if (ctx->ir_base[insn->op2].op == IR_ALLOCA) {
|
|
|
|
ir_use_list *use_list = &ctx->use_lists[insn->op2];
|
|
|
|
ir_ref *p, n = use_list->count;
|
|
|
|
for (p = &ctx->use_edges[use_list->refs]; n > 0; p++, n--) {
|
|
|
|
ir_insn *use_insn = &ctx->ir_base[*p];
|
|
|
|
if (use_insn->op == IR_VA_START || use_insn->op == IR_VA_END) {
|
|
|
|
} else if (use_insn->op == IR_VA_COPY) {
|
|
|
|
if (use_insn->op3 == insn->op2) {
|
|
|
|
ctx->flags2 |= IR_HAS_VA_COPY;
|
|
|
|
}
|
|
|
|
} else if (use_insn->op == IR_VA_ARG) {
|
|
|
|
if (use_insn->op2 == insn->op2) {
|
|
|
|
if (IR_IS_TYPE_INT(use_insn->type)) {
|
|
|
|
ctx->flags2 |= IR_HAS_VA_ARG_GP;
|
|
|
|
} else {
|
|
|
|
IR_ASSERT(IR_IS_TYPE_FP(use_insn->type));
|
|
|
|
ctx->flags2 |= IR_HAS_VA_ARG_FP;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else if (*p > ref) {
|
|
|
|
/* diriect va_list access */
|
|
|
|
ctx->flags2 |= IR_HAS_VA_ARG_GP|IR_HAS_VA_ARG_FP;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return IR_VA_START;
|
|
|
|
case IR_VA_END:
|
|
|
|
return IR_SKIPPED | IR_NOP;
|
|
|
|
case IR_VADDR:
|
|
|
|
if (ctx->use_lists[ref].count > 0) {
|
|
|
|
ir_use_list *use_list = &ctx->use_lists[ref];
|
|
|
|
ir_ref *p, n = use_list->count;
|
|
|
|
|
|
|
|
for (p = &ctx->use_edges[use_list->refs]; n > 0; p++, n--) {
|
|
|
|
if (ctx->ir_base[*p].op != IR_VA_END) {
|
|
|
|
return IR_VADDR;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return IR_SKIPPED | IR_NOP;
|
2022-05-31 23:51:02 +02:00
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return insn->op;
|
|
|
|
}
|
|
|
|
|
2023-03-29 00:21:54 +02:00
|
|
|
static void ir_match_insn2(ir_ctx *ctx, ir_ref ref, uint32_t rule)
|
2022-12-28 20:24:42 +01:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2023-10-03 07:34:02 +02:00
|
|
|
/* code generation */
|
2022-10-26 21:52:19 +02:00
|
|
|
static int32_t ir_ref_spill_slot(ir_ctx *ctx, ir_ref ref, ir_reg *reg)
|
2022-05-31 23:51:02 +02:00
|
|
|
{
|
|
|
|
int32_t offset;
|
|
|
|
|
|
|
|
IR_ASSERT(ref >= 0);
|
|
|
|
offset = ctx->live_intervals[ctx->vregs[ref]]->stack_spill_pos;
|
|
|
|
IR_ASSERT(offset != -1);
|
2022-09-15 14:26:43 +02:00
|
|
|
if (ctx->live_intervals[ctx->vregs[ref]]->flags & IR_LIVE_INTERVAL_SPILL_SPECIAL) {
|
|
|
|
IR_ASSERT(ctx->spill_base != IR_REG_NONE);
|
|
|
|
*reg = ctx->spill_base;
|
|
|
|
return offset;
|
|
|
|
}
|
2022-09-07 22:47:30 +02:00
|
|
|
*reg = (ctx->flags & IR_USE_FRAME_POINTER) ? IR_REG_FRAME_POINTER : IR_REG_STACK_POINTER;
|
2022-05-31 23:51:02 +02:00
|
|
|
return IR_SPILL_POS_TO_OFFSET(offset);
|
|
|
|
}
|
|
|
|
|
2023-06-21 22:20:58 +02:00
|
|
|
static bool ir_is_same_spill_slot(ir_ctx *ctx, ir_ref ref, ir_reg reg, int32_t offset)
|
|
|
|
{
|
|
|
|
ir_reg fp;
|
|
|
|
|
|
|
|
return ir_ref_spill_slot(ctx, ref, &fp) == offset && reg == fp;
|
|
|
|
}
|
|
|
|
|
2023-05-18 20:00:57 +02:00
|
|
|
static int32_t ir_var_spill_slot(ir_ctx *ctx, ir_ref ref, ir_reg *reg)
|
|
|
|
{
|
|
|
|
ir_insn *var_insn = &ctx->ir_base[ref];
|
|
|
|
|
|
|
|
IR_ASSERT(var_insn->op == IR_VAR);
|
|
|
|
*reg = (ctx->flags & IR_USE_FRAME_POINTER) ? IR_REG_FRAME_POINTER : IR_REG_STACK_POINTER;
|
|
|
|
return IR_SPILL_POS_TO_OFFSET(var_insn->op3);
|
|
|
|
}
|
|
|
|
|
2023-09-12 15:12:12 +02:00
|
|
|
static bool ir_may_avoid_spill_load(ir_ctx *ctx, ir_ref ref, ir_ref use)
|
|
|
|
{
|
|
|
|
ir_live_interval *ival;
|
|
|
|
|
|
|
|
IR_ASSERT(ctx->vregs[ref] && ctx->live_intervals[ctx->vregs[ref]]);
|
|
|
|
ival = ctx->live_intervals[ctx->vregs[ref]];
|
|
|
|
while (ival) {
|
|
|
|
ir_use_pos *use_pos = ival->use_pos;
|
|
|
|
while (use_pos) {
|
|
|
|
if (IR_LIVE_POS_TO_REF(use_pos->pos) == use) {
|
|
|
|
return !use_pos->next || use_pos->next->op_num == 0;
|
|
|
|
}
|
|
|
|
use_pos = use_pos->next;
|
|
|
|
}
|
|
|
|
ival = ival->next;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2022-05-31 23:51:02 +02:00
|
|
|
static void ir_emit_load_imm_int(ir_ctx *ctx, ir_type type, ir_reg reg, int64_t val)
|
|
|
|
{
|
|
|
|
ir_backend_data *data = ctx->data;
|
|
|
|
dasm_State **Dst = &data->dasm_state;
|
|
|
|
|
|
|
|
IR_ASSERT(IR_IS_TYPE_INT(type));
|
|
|
|
if (ir_type_size[type] == 8) {
|
|
|
|
if (val == 0) {
|
2022-06-28 11:24:50 +02:00
|
|
|
if (reg != IR_REG_ZR) {
|
|
|
|
| mov Rx(reg), xzr
|
|
|
|
}
|
2022-05-31 23:51:02 +02:00
|
|
|
} else if (((uint64_t)(val)) <= 0xffff) {
|
|
|
|
| movz Rx(reg), #((uint64_t)(val))
|
|
|
|
} else if (~((uint64_t)(val)) <= 0xffff) {
|
|
|
|
| movn Rx(reg), #(~((uint64_t)(val)))
|
|
|
|
} else if ((uint64_t)(val) & 0xffff) {
|
|
|
|
| movz Rx(reg), #((uint64_t)(val) & 0xffff)
|
|
|
|
if (((uint64_t)(val) >> 16) & 0xffff) {
|
|
|
|
| movk Rx(reg), #(((uint64_t)(val) >> 16) & 0xffff), lsl #16
|
|
|
|
}
|
|
|
|
if (((uint64_t)(val) >> 32) & 0xffff) {
|
|
|
|
| movk Rx(reg), #(((uint64_t)(val) >> 32) & 0xffff), lsl #32
|
|
|
|
}
|
|
|
|
if ((((uint64_t)(val) >> 48) & 0xffff)) {
|
|
|
|
| movk Rx(reg), #(((uint64_t)(val) >> 48) & 0xffff), lsl #48
|
|
|
|
}
|
|
|
|
} else if (((uint64_t)(val) >> 16) & 0xffff) {
|
|
|
|
| movz Rx(reg), #(((uint64_t)(val) >> 16) & 0xffff), lsl #16
|
|
|
|
if (((uint64_t)(val) >> 32) & 0xffff) {
|
|
|
|
| movk Rx(reg), #(((uint64_t)(val) >> 32) & 0xffff), lsl #32
|
|
|
|
}
|
|
|
|
if ((((uint64_t)(val) >> 48) & 0xffff)) {
|
|
|
|
| movk Rx(reg), #(((uint64_t)(val) >> 48) & 0xffff), lsl #48
|
|
|
|
}
|
|
|
|
} else if (((uint64_t)(val) >> 32) & 0xffff) {
|
|
|
|
| movz Rx(reg), #(((uint64_t)(val) >> 32) & 0xffff), lsl #32
|
|
|
|
if ((((uint64_t)(val) >> 48) & 0xffff)) {
|
|
|
|
| movk Rx(reg), #(((uint64_t)(val) >> 48) & 0xffff), lsl #48
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
| movz Rx(reg), #(((uint64_t)(val) >> 48) & 0xffff), lsl #48
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (val == 0) {
|
2022-06-28 11:24:50 +02:00
|
|
|
if (reg != IR_REG_ZR) {
|
|
|
|
| mov Rw(reg), wzr
|
|
|
|
}
|
2022-05-31 23:51:02 +02:00
|
|
|
} else if (((uint64_t)(val)) <= 0xffff) {
|
|
|
|
| movz Rw(reg), #((uint64_t)(val))
|
|
|
|
} else if (~((uint64_t)(val)) <= 0xffff) {
|
|
|
|
| movn Rw(reg), #(~((uint64_t)(val)))
|
|
|
|
} else if ((uint64_t)(val) & 0xffff) {
|
|
|
|
| movz Rw(reg), #((uint64_t)(val) & 0xffff)
|
|
|
|
if (((uint64_t)(val) >> 16) & 0xffff) {
|
|
|
|
| movk Rw(reg), #(((uint64_t)(val) >> 16) & 0xffff), lsl #16
|
|
|
|
}
|
|
|
|
} else if (((uint64_t)(val) >> 16) & 0xffff) {
|
|
|
|
| movz Rw(reg), #(((uint64_t)(val) >> 16) & 0xffff), lsl #16
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void ir_emit_load_mem_int(ir_ctx *ctx, ir_type type, ir_reg reg, ir_reg base_reg, int32_t offset)
|
|
|
|
{
|
|
|
|
ir_backend_data *data = ctx->data;
|
|
|
|
dasm_State **Dst = &data->dasm_state;
|
|
|
|
|
|
|
|
if (aarch64_may_encode_addr_offset(offset, ir_type_size[type])) {
|
|
|
|
switch (ir_type_size[type]) {
|
2023-07-27 10:16:00 +02:00
|
|
|
default:
|
|
|
|
IR_ASSERT(0);
|
2022-05-31 23:51:02 +02:00
|
|
|
case 8:
|
|
|
|
| ldr Rx(reg), [Rx(base_reg), #offset]
|
|
|
|
break;
|
|
|
|
case 4:
|
2022-06-03 11:47:02 +02:00
|
|
|
| ldr Rw(reg), [Rx(base_reg), #offset]
|
2022-05-31 23:51:02 +02:00
|
|
|
break;
|
|
|
|
case 2:
|
|
|
|
if (IR_IS_TYPE_SIGNED(type)) {
|
2022-06-03 11:47:02 +02:00
|
|
|
| ldrsh Rw(reg), [Rx(base_reg), #offset]
|
2022-05-31 23:51:02 +02:00
|
|
|
} else {
|
|
|
|
| ldrh Rw(reg), [Rx(base_reg), #offset]
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 1:
|
|
|
|
if (IR_IS_TYPE_SIGNED(type)) {
|
2022-06-03 11:47:02 +02:00
|
|
|
| ldrsb Rw(reg), [Rx(base_reg), #offset]
|
2022-05-31 23:51:02 +02:00
|
|
|
} else {
|
|
|
|
| ldrb Rw(reg), [Rx(base_reg), #offset]
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
} else {
|
2022-06-28 11:24:50 +02:00
|
|
|
ir_reg tmp_reg = IR_REG_INT_TMP; /* reserved temporary register */
|
|
|
|
|
|
|
|
ir_emit_load_imm_int(ctx, IR_ADDR, tmp_reg, offset);
|
2022-05-31 23:51:02 +02:00
|
|
|
switch (ir_type_size[type]) {
|
2023-07-27 10:16:00 +02:00
|
|
|
default:
|
|
|
|
IR_ASSERT(0);
|
2022-05-31 23:51:02 +02:00
|
|
|
case 8:
|
2022-06-28 11:24:50 +02:00
|
|
|
| ldr Rx(reg), [Rx(base_reg), Rx(tmp_reg)]
|
2022-05-31 23:51:02 +02:00
|
|
|
break;
|
|
|
|
case 4:
|
2022-06-28 11:24:50 +02:00
|
|
|
| ldr Rw(reg), [Rx(base_reg), Rx(tmp_reg)]
|
2022-05-31 23:51:02 +02:00
|
|
|
break;
|
|
|
|
case 2:
|
|
|
|
if (IR_IS_TYPE_SIGNED(type)) {
|
2022-06-28 11:24:50 +02:00
|
|
|
| ldrsh Rw(reg), [Rx(base_reg), Rx(tmp_reg)]
|
2022-05-31 23:51:02 +02:00
|
|
|
} else {
|
2022-06-28 11:24:50 +02:00
|
|
|
| ldrh Rw(reg), [Rx(base_reg), Rx(tmp_reg)]
|
2022-05-31 23:51:02 +02:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 1:
|
|
|
|
if (IR_IS_TYPE_SIGNED(type)) {
|
2022-06-28 11:24:50 +02:00
|
|
|
| ldrsb Rw(reg), [Rx(base_reg), Rx(tmp_reg)]
|
2022-05-31 23:51:02 +02:00
|
|
|
} else {
|
2022-06-28 11:24:50 +02:00
|
|
|
| ldrb Rw(reg), [Rx(base_reg), Rx(tmp_reg)]
|
2022-05-31 23:51:02 +02:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void ir_emit_load_imm_fp(ir_ctx *ctx, ir_type type, ir_reg reg, ir_ref src)
|
|
|
|
{
|
|
|
|
ir_backend_data *data = ctx->data;
|
|
|
|
dasm_State **Dst = &data->dasm_state;
|
|
|
|
ir_insn *insn = &ctx->ir_base[src];
|
|
|
|
int label;
|
|
|
|
|
2022-08-03 23:22:19 +02:00
|
|
|
if (type == IR_FLOAT && insn->val.u32 == 0) {
|
2022-05-31 23:51:02 +02:00
|
|
|
| fmov Rs(reg-IR_REG_FP_FIRST), wzr
|
2022-08-03 23:22:19 +02:00
|
|
|
} else if (type == IR_DOUBLE && insn->val.u64 == 0) {
|
2022-05-31 23:51:02 +02:00
|
|
|
| fmov Rd(reg-IR_REG_FP_FIRST), xzr
|
|
|
|
} else {
|
2023-12-08 15:35:41 +01:00
|
|
|
label = ir_const_label(ctx, src);
|
2022-05-31 23:51:02 +02:00
|
|
|
if (type == IR_DOUBLE) {
|
|
|
|
| ldr Rd(reg-IR_REG_FP_FIRST), =>label
|
|
|
|
} else {
|
|
|
|
IR_ASSERT(type == IR_FLOAT);
|
|
|
|
| ldr Rs(reg-IR_REG_FP_FIRST), =>label
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void ir_emit_load_mem_fp(ir_ctx *ctx, ir_type type, ir_reg reg, ir_reg base_reg, int32_t offset)
|
|
|
|
{
|
|
|
|
ir_backend_data *data = ctx->data;
|
|
|
|
dasm_State **Dst = &data->dasm_state;
|
|
|
|
|
|
|
|
if (aarch64_may_encode_addr_offset(offset, ir_type_size[type])) {
|
|
|
|
if (type == IR_DOUBLE) {
|
|
|
|
| ldr Rd(reg-IR_REG_FP_FIRST), [Rx(base_reg), #offset]
|
|
|
|
} else {
|
|
|
|
IR_ASSERT(type == IR_FLOAT);
|
|
|
|
| ldr Rs(reg-IR_REG_FP_FIRST), [Rx(base_reg), #offset]
|
|
|
|
}
|
|
|
|
} else {
|
2022-06-02 17:34:47 +02:00
|
|
|
ir_reg tmp_reg = IR_REG_INT_TMP; /* reserved temporary register */
|
2022-05-31 23:51:02 +02:00
|
|
|
|
2022-06-02 17:34:47 +02:00
|
|
|
ir_emit_load_imm_int(ctx, IR_ADDR, tmp_reg, offset);
|
2022-05-31 23:51:02 +02:00
|
|
|
if (type == IR_DOUBLE) {
|
|
|
|
| ldr Rd(reg-IR_REG_FP_FIRST), [Rx(base_reg), Rx(tmp_reg)]
|
|
|
|
} else {
|
|
|
|
IR_ASSERT(type == IR_FLOAT);
|
|
|
|
| ldr Rs(reg-IR_REG_FP_FIRST), [Rx(base_reg), Rx(tmp_reg)]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void ir_emit_load(ir_ctx *ctx, ir_type type, ir_reg reg, ir_ref src)
|
|
|
|
{
|
|
|
|
int32_t offset;
|
|
|
|
ir_reg fp;
|
|
|
|
|
|
|
|
if (IR_IS_CONST_REF(src)) {
|
|
|
|
if (IR_IS_TYPE_INT(type)) {
|
|
|
|
ir_insn *insn = &ctx->ir_base[src];
|
|
|
|
|
2023-12-06 14:30:44 +01:00
|
|
|
if (insn->op == IR_SYM || insn->op == IR_FUNC) {
|
2023-12-20 19:06:53 +01:00
|
|
|
const char *name = ir_get_str(ctx, insn->val.name);
|
2023-12-06 14:30:44 +01:00
|
|
|
void *addr = (ctx->loader && ctx->loader->resolve_sym_name) ?
|
2023-12-20 19:06:53 +01:00
|
|
|
ctx->loader->resolve_sym_name(ctx->loader, name, insn->op == IR_FUNC) :
|
|
|
|
ir_resolve_sym_name(name);
|
2023-12-06 14:30:44 +01:00
|
|
|
IR_ASSERT(addr);
|
|
|
|
ir_emit_load_imm_int(ctx, type, reg, (intptr_t)addr);
|
2023-12-08 15:35:41 +01:00
|
|
|
} else if (insn->op == IR_STR) {
|
|
|
|
ir_backend_data *data = ctx->data;
|
|
|
|
dasm_State **Dst = &data->dasm_state;
|
|
|
|
int label = ir_const_label(ctx, src);
|
|
|
|
|
|
|
|
| adr Rx(reg), =>label
|
2023-12-06 14:30:44 +01:00
|
|
|
} else {
|
|
|
|
ir_emit_load_imm_int(ctx, type, reg, insn->val.i64);
|
|
|
|
}
|
2022-05-31 23:51:02 +02:00
|
|
|
} else {
|
|
|
|
ir_emit_load_imm_fp(ctx, type, reg, src);
|
|
|
|
}
|
|
|
|
} else {
|
2022-09-07 22:47:30 +02:00
|
|
|
offset = ir_ref_spill_slot(ctx, src, &fp);
|
2022-05-31 23:51:02 +02:00
|
|
|
if (IR_IS_TYPE_INT(type)) {
|
|
|
|
ir_emit_load_mem_int(ctx, type, reg, fp, offset);
|
|
|
|
} else {
|
|
|
|
ir_emit_load_mem_fp(ctx, type, reg, fp, offset);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void ir_emit_store_mem_int(ir_ctx *ctx, ir_type type, ir_reg base_reg, int32_t offset, ir_reg reg)
|
|
|
|
{
|
|
|
|
ir_backend_data *data = ctx->data;
|
|
|
|
dasm_State **Dst = &data->dasm_state;
|
|
|
|
|
|
|
|
if (aarch64_may_encode_addr_offset(offset, ir_type_size[type])) {
|
|
|
|
switch (ir_type_size[type]) {
|
2023-07-27 10:16:00 +02:00
|
|
|
default:
|
|
|
|
IR_ASSERT(0);
|
2022-05-31 23:51:02 +02:00
|
|
|
case 8:
|
|
|
|
| str Rx(reg), [Rx(base_reg), #offset]
|
|
|
|
break;
|
|
|
|
case 4:
|
|
|
|
| str Rw(reg), [Rx(base_reg), #offset]
|
|
|
|
break;
|
|
|
|
case 2:
|
|
|
|
| strh Rw(reg), [Rx(base_reg), #offset]
|
|
|
|
break;
|
|
|
|
case 1:
|
|
|
|
| strb Rw(reg), [Rx(base_reg), #offset]
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
} else {
|
2022-06-28 11:24:50 +02:00
|
|
|
ir_reg tmp_reg = IR_REG_INT_TMP; /* reserved temporary register */
|
|
|
|
|
|
|
|
ir_emit_load_imm_int(ctx, IR_ADDR, tmp_reg, offset);
|
2022-05-31 23:51:02 +02:00
|
|
|
switch (ir_type_size[type]) {
|
2023-07-27 10:16:00 +02:00
|
|
|
default:
|
|
|
|
IR_ASSERT(0);
|
2022-05-31 23:51:02 +02:00
|
|
|
case 8:
|
2022-06-28 11:24:50 +02:00
|
|
|
| str Rx(reg), [Rx(base_reg), Rx(tmp_reg)]
|
2022-05-31 23:51:02 +02:00
|
|
|
break;
|
|
|
|
case 4:
|
2022-06-28 11:24:50 +02:00
|
|
|
| str Rw(reg), [Rx(base_reg), Rx(tmp_reg)]
|
2022-05-31 23:51:02 +02:00
|
|
|
break;
|
|
|
|
case 2:
|
2022-06-28 11:24:50 +02:00
|
|
|
| strh Rw(reg), [Rx(base_reg), Rx(tmp_reg)]
|
2022-05-31 23:51:02 +02:00
|
|
|
break;
|
|
|
|
case 1:
|
2022-06-28 11:24:50 +02:00
|
|
|
| strb Rw(reg), [Rx(base_reg), Rx(tmp_reg)]
|
2022-05-31 23:51:02 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void ir_emit_store_mem_fp(ir_ctx *ctx, ir_type type, ir_reg base_reg, int32_t offset, ir_reg reg)
|
|
|
|
{
|
|
|
|
ir_backend_data *data = ctx->data;
|
|
|
|
dasm_State **Dst = &data->dasm_state;
|
|
|
|
|
|
|
|
if (aarch64_may_encode_addr_offset(offset, ir_type_size[type])) {
|
|
|
|
if (type == IR_DOUBLE) {
|
|
|
|
| str Rd(reg-IR_REG_FP_FIRST), [Rx(base_reg), #offset]
|
|
|
|
} else {
|
|
|
|
IR_ASSERT(type == IR_FLOAT);
|
|
|
|
| str Rs(reg-IR_REG_FP_FIRST), [Rx(base_reg), #offset]
|
|
|
|
}
|
|
|
|
} else {
|
2022-06-02 17:34:47 +02:00
|
|
|
ir_reg tmp_reg = IR_REG_INT_TMP; /* reserved temporary register */
|
2022-05-31 23:51:02 +02:00
|
|
|
|
2022-06-02 17:34:47 +02:00
|
|
|
ir_emit_load_imm_int(ctx, IR_ADDR, tmp_reg, offset);
|
2022-05-31 23:51:02 +02:00
|
|
|
if (type == IR_DOUBLE) {
|
|
|
|
| str Rd(reg-IR_REG_FP_FIRST), [Rx(base_reg), Rx(tmp_reg)]
|
|
|
|
} else {
|
|
|
|
IR_ASSERT(type == IR_FLOAT);
|
|
|
|
| str Rs(reg-IR_REG_FP_FIRST), [Rx(base_reg), Rx(tmp_reg)]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void ir_emit_store(ir_ctx *ctx, ir_type type, ir_ref dst, ir_reg reg)
|
|
|
|
{
|
|
|
|
int32_t offset;
|
|
|
|
ir_reg fp;
|
|
|
|
|
|
|
|
IR_ASSERT(dst >= 0);
|
2022-09-07 22:47:30 +02:00
|
|
|
offset = ir_ref_spill_slot(ctx, dst, &fp);
|
2022-05-31 23:51:02 +02:00
|
|
|
if (IR_IS_TYPE_INT(type)) {
|
|
|
|
ir_emit_store_mem_int(ctx, type, fp, offset, reg);
|
|
|
|
} else {
|
|
|
|
ir_emit_store_mem_fp(ctx, type, fp, offset, reg);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void ir_emit_mov(ir_ctx *ctx, ir_type type, ir_reg dst, ir_reg src)
|
|
|
|
{
|
|
|
|
ir_backend_data *data = ctx->data;
|
|
|
|
dasm_State **Dst = &data->dasm_state;
|
|
|
|
|
|
|
|
if (ir_type_size[type] == 8) {
|
2023-02-21 09:42:05 +01:00
|
|
|
if (dst == IR_REG_STACK_POINTER) {
|
|
|
|
| mov sp, Rx(src)
|
|
|
|
} else if (src == IR_REG_STACK_POINTER) {
|
|
|
|
| mov Rx(dst), sp
|
|
|
|
} else {
|
|
|
|
| mov Rx(dst), Rx(src)
|
|
|
|
}
|
2022-05-31 23:51:02 +02:00
|
|
|
} else {
|
|
|
|
| mov Rw(dst), Rw(src)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void ir_emit_fp_mov(ir_ctx *ctx, ir_type type, ir_reg dst, ir_reg src)
|
|
|
|
{
|
|
|
|
ir_backend_data *data = ctx->data;
|
|
|
|
dasm_State **Dst = &data->dasm_state;
|
|
|
|
|
|
|
|
if (ir_type_size[type] == 8) {
|
|
|
|
| fmov Rd(dst-IR_REG_FP_FIRST), Rd(src-IR_REG_FP_FIRST)
|
|
|
|
} else {
|
|
|
|
| fmov Rs(dst-IR_REG_FP_FIRST), Rs(src-IR_REG_FP_FIRST)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void ir_emit_prologue(ir_ctx *ctx)
|
|
|
|
{
|
|
|
|
ir_backend_data *data = ctx->data;
|
|
|
|
dasm_State **Dst = &data->dasm_state;
|
|
|
|
|
|
|
|
if (ctx->flags & IR_USE_FRAME_POINTER) {
|
2023-06-15 18:28:54 +02:00
|
|
|
| stp x29, x30, [sp, # (-(ctx->stack_frame_size+16))]!
|
2022-06-02 17:34:47 +02:00
|
|
|
| mov x29, sp
|
2023-06-15 18:28:54 +02:00
|
|
|
if (ctx->call_stack_size) {
|
|
|
|
| sub sp, sp, #(ctx->call_stack_size)
|
2022-06-02 17:34:47 +02:00
|
|
|
}
|
2023-06-15 18:28:54 +02:00
|
|
|
} else if (ctx->stack_frame_size + ctx->call_stack_size) {
|
2023-02-17 13:52:26 +01:00
|
|
|
if (ctx->fixed_stack_red_zone) {
|
2023-06-15 18:28:54 +02:00
|
|
|
IR_ASSERT(ctx->stack_frame_size + ctx->call_stack_size <= ctx->fixed_stack_red_zone);
|
2023-02-17 13:52:26 +01:00
|
|
|
} else {
|
2023-06-15 18:28:54 +02:00
|
|
|
| sub sp, sp, #(ctx->stack_frame_size + ctx->call_stack_size)
|
2023-02-17 13:52:26 +01:00
|
|
|
}
|
2022-05-31 23:51:02 +02:00
|
|
|
}
|
2023-06-21 18:04:22 +02:00
|
|
|
if (ctx->used_preserved_regs) {
|
2023-11-23 17:38:33 +01:00
|
|
|
ir_reg fp;
|
2022-05-31 23:51:02 +02:00
|
|
|
int offset;
|
|
|
|
uint32_t i;
|
2022-06-03 11:47:02 +02:00
|
|
|
ir_reg prev = IR_REG_NONE;
|
2023-06-22 13:50:14 +02:00
|
|
|
ir_regset used_preserved_regs = (ir_regset)ctx->used_preserved_regs;
|
2022-05-31 23:51:02 +02:00
|
|
|
|
|
|
|
if (ctx->flags & IR_USE_FRAME_POINTER) {
|
2023-11-23 17:38:33 +01:00
|
|
|
fp = IR_REG_FRAME_POINTER;
|
2023-06-15 18:28:54 +02:00
|
|
|
offset = ctx->stack_frame_size + sizeof(void*) * 2;
|
2022-05-31 23:51:02 +02:00
|
|
|
} else {
|
2023-11-23 17:38:33 +01:00
|
|
|
fp = IR_REG_STACK_POINTER;
|
2023-06-15 18:28:54 +02:00
|
|
|
offset = ctx->stack_frame_size + ctx->call_stack_size;
|
2022-05-31 23:51:02 +02:00
|
|
|
}
|
|
|
|
for (i = 0; i < IR_REG_NUM; i++) {
|
2023-06-21 18:04:22 +02:00
|
|
|
if (IR_REGSET_IN(used_preserved_regs, i)) {
|
2022-06-03 11:47:02 +02:00
|
|
|
if (prev == IR_REG_NONE) {
|
|
|
|
prev = i;
|
|
|
|
} else if (i < IR_REG_FP_FIRST) {
|
|
|
|
offset -= sizeof(void*) * 2;
|
|
|
|
| stp Rx(prev), Rx(i), [Rx(fp), #offset]
|
|
|
|
prev = IR_REG_NONE;
|
2022-05-31 23:51:02 +02:00
|
|
|
} else {
|
2022-06-03 11:47:02 +02:00
|
|
|
if (prev < IR_REG_FP_FIRST) {
|
|
|
|
offset -= sizeof(void*);
|
|
|
|
| str Rx(prev), [Rx(fp), #offset]
|
|
|
|
offset -= sizeof(void*);
|
|
|
|
| str Rd(i-IR_REG_FP_FIRST), [Rx(fp), #offset]
|
|
|
|
} else {
|
|
|
|
offset -= sizeof(void*) * 2;
|
|
|
|
| stp Rd(prev-IR_REG_FP_FIRST), Rd(i-IR_REG_FP_FIRST), [Rx(fp), #offset]
|
|
|
|
}
|
|
|
|
prev = IR_REG_NONE;
|
2022-05-31 23:51:02 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-06-03 11:47:02 +02:00
|
|
|
if (prev != IR_REG_NONE) {
|
|
|
|
if (prev < IR_REG_FP_FIRST) {
|
|
|
|
offset -= sizeof(void*);
|
|
|
|
| str Rx(prev), [Rx(fp), #offset]
|
|
|
|
} else {
|
|
|
|
offset -= sizeof(void*);
|
|
|
|
| str Rd(prev-IR_REG_FP_FIRST), [Rx(fp), #offset]
|
|
|
|
}
|
|
|
|
}
|
2022-05-31 23:51:02 +02:00
|
|
|
}
|
2023-11-23 17:38:33 +01:00
|
|
|
if ((ctx->flags & IR_VARARG_FUNC) && (ctx->flags2 & IR_HAS_VA_START)) {
|
|
|
|
#ifndef __APPLE__
|
|
|
|
const int8_t *int_reg_params = _ir_int_reg_params;
|
|
|
|
const int8_t *fp_reg_params = _ir_fp_reg_params;
|
|
|
|
ir_reg fp;
|
|
|
|
int offset;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
if (ctx->flags & IR_USE_FRAME_POINTER) {
|
|
|
|
fp = IR_REG_FRAME_POINTER;
|
|
|
|
|
|
|
|
offset = ctx->locals_area_size + sizeof(void*) * 2;
|
|
|
|
} else {
|
|
|
|
fp = IR_REG_STACK_POINTER;
|
|
|
|
offset = ctx->locals_area_size + ctx->call_stack_size;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((ctx->flags2 & (IR_HAS_VA_ARG_GP|IR_HAS_VA_COPY)) && ctx->gp_reg_params < IR_REG_INT_ARGS) {
|
|
|
|
ir_reg prev = IR_REG_NONE;
|
|
|
|
|
|
|
|
/* skip named args */
|
|
|
|
offset += sizeof(void*) * ctx->gp_reg_params;
|
|
|
|
for (i = ctx->gp_reg_params; i < IR_REG_INT_ARGS; i++) {
|
|
|
|
if (prev != IR_REG_NONE) {
|
|
|
|
| stp Rx(prev), Rx(int_reg_params[i]), [Rx(fp), #offset]
|
|
|
|
prev = IR_REG_NONE;
|
|
|
|
offset += sizeof(void*) * 2;
|
|
|
|
} else {
|
|
|
|
prev = int_reg_params[i];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (prev != IR_REG_NONE) {
|
|
|
|
| str Rx(prev), [Rx(fp), #offset]
|
|
|
|
offset += sizeof(void*);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if ((ctx->flags2 & (IR_HAS_VA_ARG_FP|IR_HAS_VA_COPY)) && ctx->fp_reg_params < IR_REG_FP_ARGS) {
|
|
|
|
/* skip named args */
|
|
|
|
offset += 16 * ctx->fp_reg_params;
|
|
|
|
for (i = ctx->fp_reg_params; i < IR_REG_FP_ARGS; i++) {
|
|
|
|
// TODO: Rd->Rq stur->str ???
|
|
|
|
| str Rd(fp_reg_params[i]-IR_REG_FP_FIRST), [Rx(fp), #offset]
|
|
|
|
offset += 16;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
}
|
2022-05-31 23:51:02 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static void ir_emit_epilogue(ir_ctx *ctx)
|
|
|
|
{
|
|
|
|
ir_backend_data *data = ctx->data;
|
|
|
|
dasm_State **Dst = &data->dasm_state;
|
|
|
|
|
2023-06-21 18:04:22 +02:00
|
|
|
if (ctx->used_preserved_regs) {
|
2022-05-31 23:51:02 +02:00
|
|
|
int offset;
|
|
|
|
uint32_t i;
|
2022-06-03 11:47:02 +02:00
|
|
|
ir_reg prev = IR_REG_NONE;
|
|
|
|
ir_reg fp = (ctx->flags & IR_USE_FRAME_POINTER) ? IR_REG_FRAME_POINTER : IR_REG_STACK_POINTER;
|
2023-06-22 13:50:14 +02:00
|
|
|
ir_regset used_preserved_regs = (ir_regset)ctx->used_preserved_regs;
|
2022-05-31 23:51:02 +02:00
|
|
|
|
|
|
|
if (ctx->flags & IR_USE_FRAME_POINTER) {
|
2023-06-15 18:28:54 +02:00
|
|
|
offset = ctx->stack_frame_size + sizeof(void*) * 2;
|
2022-05-31 23:51:02 +02:00
|
|
|
} else {
|
2023-06-15 18:28:54 +02:00
|
|
|
offset = ctx->stack_frame_size + ctx->call_stack_size;
|
2022-05-31 23:51:02 +02:00
|
|
|
}
|
|
|
|
for (i = 0; i < IR_REG_NUM; i++) {
|
2023-06-21 18:04:22 +02:00
|
|
|
if (IR_REGSET_IN(used_preserved_regs, i)) {
|
2022-06-03 11:47:02 +02:00
|
|
|
if (prev == IR_REG_NONE) {
|
|
|
|
prev = i;
|
|
|
|
} else if (i < IR_REG_FP_FIRST) {
|
|
|
|
offset -= sizeof(void*) * 2;
|
|
|
|
| ldp Rx(prev), Rx(i), [Rx(fp), #offset]
|
|
|
|
prev = IR_REG_NONE;
|
2022-05-31 23:51:02 +02:00
|
|
|
} else {
|
2022-06-03 11:47:02 +02:00
|
|
|
if (prev < IR_REG_FP_FIRST) {
|
|
|
|
offset -= sizeof(void*);
|
|
|
|
| ldr Rx(prev), [Rx(fp), #offset]
|
|
|
|
offset -= sizeof(void*);
|
|
|
|
| ldr Rd(i-IR_REG_FP_FIRST), [Rx(fp), #offset]
|
|
|
|
} else {
|
|
|
|
offset -= sizeof(void*) * 2;
|
|
|
|
| ldp Rd(prev-IR_REG_FP_FIRST), Rd(i-IR_REG_FP_FIRST), [Rx(fp), #offset]
|
|
|
|
}
|
|
|
|
prev = IR_REG_NONE;
|
2022-05-31 23:51:02 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-06-03 11:47:02 +02:00
|
|
|
if (prev != IR_REG_NONE) {
|
|
|
|
if (prev < IR_REG_FP_FIRST) {
|
|
|
|
offset -= sizeof(void*);
|
|
|
|
| ldr Rx(prev), [Rx(fp), #offset]
|
|
|
|
} else {
|
|
|
|
offset -= sizeof(void*);
|
|
|
|
| ldr Rd(prev-IR_REG_FP_FIRST), [Rx(fp), #offset]
|
|
|
|
}
|
|
|
|
}
|
2022-05-31 23:51:02 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (ctx->flags & IR_USE_FRAME_POINTER) {
|
2023-11-17 12:24:33 +01:00
|
|
|
if (ctx->call_stack_size || (ctx->flags2 & IR_HAS_ALLOCA)) {
|
2022-06-02 23:38:49 +02:00
|
|
|
| mov sp, x29
|
2022-06-02 17:34:47 +02:00
|
|
|
}
|
2023-06-15 18:28:54 +02:00
|
|
|
| ldp x29, x30, [sp], # (ctx->stack_frame_size+16)
|
|
|
|
} else if (ctx->stack_frame_size + ctx->call_stack_size) {
|
2023-02-17 13:52:26 +01:00
|
|
|
if (ctx->fixed_stack_red_zone) {
|
2023-06-15 18:28:54 +02:00
|
|
|
IR_ASSERT(ctx->stack_frame_size + ctx->call_stack_size <= ctx->fixed_stack_red_zone);
|
2023-02-17 13:52:26 +01:00
|
|
|
} else {
|
2023-06-15 18:28:54 +02:00
|
|
|
| add sp, sp, #(ctx->stack_frame_size + ctx->call_stack_size)
|
2023-02-17 13:52:26 +01:00
|
|
|
}
|
2022-05-31 23:51:02 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void ir_emit_binop_int(ir_ctx *ctx, ir_ref def, ir_insn *insn)
|
|
|
|
{
|
|
|
|
ir_backend_data *data = ctx->data;
|
|
|
|
dasm_State **Dst = &data->dasm_state;
|
|
|
|
ir_type type = insn->type;
|
|
|
|
ir_ref op1 = insn->op1;
|
|
|
|
ir_ref op2 = insn->op2;
|
|
|
|
ir_reg def_reg = IR_REG_NUM(ctx->regs[def][0]);
|
|
|
|
ir_reg op1_reg = ctx->regs[def][1];
|
|
|
|
ir_reg op2_reg = ctx->regs[def][2];
|
2022-06-01 17:16:32 +02:00
|
|
|
ir_reg tmp_reg;
|
2022-05-31 23:51:02 +02:00
|
|
|
|
|
|
|
IR_ASSERT(def_reg != IR_REG_NONE && op1_reg != IR_REG_NONE);
|
|
|
|
|
2023-06-14 19:23:32 +02:00
|
|
|
if (IR_REG_SPILLED(op1_reg) || IR_IS_CONST_REF(op1)) {
|
|
|
|
op1_reg = IR_REG_NUM(op1_reg);
|
2022-05-31 23:51:02 +02:00
|
|
|
ir_emit_load(ctx, type, op1_reg, op1);
|
|
|
|
}
|
|
|
|
if (op2_reg != IR_REG_NONE) {
|
2023-06-14 19:23:32 +02:00
|
|
|
if (IR_REG_SPILLED(op2_reg) || IR_IS_CONST_REF(op2)) {
|
|
|
|
op2_reg = IR_REG_NUM(op2_reg);
|
2022-05-31 23:51:02 +02:00
|
|
|
if (op1 != op2) {
|
|
|
|
ir_emit_load(ctx, type, op2_reg, op2);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
switch (insn->op) {
|
2023-07-27 10:16:00 +02:00
|
|
|
default:
|
|
|
|
IR_ASSERT(0 && "NIY binary op");
|
2022-05-31 23:51:02 +02:00
|
|
|
case IR_ADD:
|
|
|
|
| ASM_REG_REG_REG_OP add, type, def_reg, op1_reg, op2_reg
|
|
|
|
break;
|
|
|
|
case IR_ADD_OV:
|
|
|
|
| ASM_REG_REG_REG_OP adds, type, def_reg, op1_reg, op2_reg
|
|
|
|
break;
|
|
|
|
case IR_SUB:
|
|
|
|
| ASM_REG_REG_REG_OP sub, type, def_reg, op1_reg, op2_reg
|
|
|
|
break;
|
|
|
|
case IR_SUB_OV:
|
|
|
|
| ASM_REG_REG_REG_OP subs, type, def_reg, op1_reg, op2_reg
|
|
|
|
break;
|
|
|
|
case IR_MUL:
|
|
|
|
| ASM_REG_REG_REG_OP mul, type, def_reg, op1_reg, op2_reg
|
|
|
|
break;
|
2022-06-06 14:27:25 +02:00
|
|
|
case IR_MUL_OV:
|
|
|
|
if (ir_type_size[type] == 8) {
|
|
|
|
if (IR_IS_TYPE_SIGNED(type)) {
|
|
|
|
tmp_reg = ctx->regs[def][3];
|
|
|
|
IR_ASSERT(tmp_reg != IR_REG_NONE);
|
|
|
|
| smulh Rx(tmp_reg), Rx(op1_reg), Rx(op2_reg)
|
2023-01-26 11:20:08 +01:00
|
|
|
| mul Rx(def_reg), Rx(op1_reg), Rx(op2_reg)
|
2022-06-06 14:27:25 +02:00
|
|
|
| cmp Rx(tmp_reg), Rx(def_reg), asr #63
|
|
|
|
} else {
|
|
|
|
tmp_reg = ctx->regs[def][3];
|
|
|
|
IR_ASSERT(tmp_reg != IR_REG_NONE);
|
|
|
|
| umulh Rx(tmp_reg), Rx(op1_reg), Rx(op2_reg)
|
|
|
|
| mul Rx(def_reg), Rx(op1_reg), Rx(op2_reg)
|
|
|
|
| cmp Rx(tmp_reg), xzr
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (IR_IS_TYPE_SIGNED(type)) {
|
|
|
|
tmp_reg = ctx->regs[def][3];
|
|
|
|
IR_ASSERT(tmp_reg != IR_REG_NONE);
|
|
|
|
| smull Rx(def_reg), Rw(op1_reg), Rw(op2_reg)
|
|
|
|
| asr Rx(tmp_reg), Rx(def_reg), #32
|
|
|
|
| cmp Rx(tmp_reg), Rx(def_reg), asr #31
|
|
|
|
} else {
|
|
|
|
| umull Rx(def_reg), Rw(op1_reg), Rw(op2_reg)
|
|
|
|
| cmp xzr, Rx(def_reg), lsr #32
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
2022-06-01 17:16:32 +02:00
|
|
|
case IR_DIV:
|
|
|
|
if (IR_IS_TYPE_SIGNED(type)) {
|
|
|
|
| ASM_REG_REG_REG_OP sdiv, type, def_reg, op1_reg, op2_reg
|
|
|
|
} else {
|
|
|
|
| ASM_REG_REG_REG_OP udiv, type, def_reg, op1_reg, op2_reg
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case IR_MOD:
|
|
|
|
tmp_reg = ctx->regs[def][3];
|
|
|
|
IR_ASSERT(tmp_reg != IR_REG_NONE);
|
|
|
|
if (IR_IS_TYPE_SIGNED(type)) {
|
|
|
|
| ASM_REG_REG_REG_OP sdiv, type, tmp_reg, op1_reg, op2_reg
|
|
|
|
| ASM_REG_REG_REG_REG_OP msub, type, def_reg, tmp_reg, op2_reg, op1_reg
|
|
|
|
} else {
|
|
|
|
| ASM_REG_REG_REG_OP udiv, type, tmp_reg, op1_reg, op2_reg
|
|
|
|
| ASM_REG_REG_REG_REG_OP msub, type, def_reg, tmp_reg, op2_reg, op1_reg
|
|
|
|
}
|
|
|
|
break;
|
2022-05-31 23:51:02 +02:00
|
|
|
case IR_OR:
|
|
|
|
| ASM_REG_REG_REG_OP orr, type, def_reg, op1_reg, op2_reg
|
|
|
|
break;
|
|
|
|
case IR_AND:
|
|
|
|
| ASM_REG_REG_REG_OP and, type, def_reg, op1_reg, op2_reg
|
|
|
|
break;
|
|
|
|
case IR_XOR:
|
|
|
|
| ASM_REG_REG_REG_OP eor, type, def_reg, op1_reg, op2_reg
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
} else {
|
2022-06-01 17:16:32 +02:00
|
|
|
IR_ASSERT(IR_IS_CONST_REF(op2));
|
2023-12-27 08:18:56 +01:00
|
|
|
IR_ASSERT(!IR_IS_SYM_CONST(ctx->ir_base[op2].op));
|
2022-05-31 23:51:02 +02:00
|
|
|
int32_t val = ctx->ir_base[op2].val.i32;
|
|
|
|
switch (insn->op) {
|
2023-07-27 10:16:00 +02:00
|
|
|
default:
|
|
|
|
IR_ASSERT(0 && "NIY binary op");
|
2022-05-31 23:51:02 +02:00
|
|
|
case IR_ADD:
|
|
|
|
| ASM_REG_REG_IMM_OP add, type, def_reg, op1_reg, val
|
|
|
|
break;
|
|
|
|
case IR_ADD_OV:
|
2022-06-01 17:16:32 +02:00
|
|
|
| ASM_REG_REG_IMM_OP adds, type, def_reg, op1_reg, val
|
2022-05-31 23:51:02 +02:00
|
|
|
break;
|
|
|
|
case IR_SUB:
|
|
|
|
| ASM_REG_REG_IMM_OP sub, type, def_reg, op1_reg, val
|
|
|
|
break;
|
|
|
|
case IR_SUB_OV:
|
|
|
|
| ASM_REG_REG_IMM_OP subs, type, def_reg, op1_reg, val
|
|
|
|
break;
|
|
|
|
case IR_OR:
|
2023-08-30 11:03:43 +02:00
|
|
|
if (ir_type_size[type] == 8) {
|
|
|
|
uint64_t val = ctx->ir_base[op2].val.u64;
|
|
|
|
| ASM_REG_REG_IMM_OP orr, type, def_reg, op1_reg, val
|
|
|
|
} else {
|
|
|
|
| ASM_REG_REG_IMM_OP orr, type, def_reg, op1_reg, val
|
|
|
|
}
|
2022-05-31 23:51:02 +02:00
|
|
|
break;
|
|
|
|
case IR_AND:
|
2023-08-30 11:03:43 +02:00
|
|
|
if (ir_type_size[type] == 8) {
|
|
|
|
uint64_t val = ctx->ir_base[op2].val.u64;
|
|
|
|
| ASM_REG_REG_IMM_OP and, type, def_reg, op1_reg, val
|
|
|
|
} else {
|
|
|
|
| ASM_REG_REG_IMM_OP and, type, def_reg, op1_reg, val
|
|
|
|
}
|
2022-05-31 23:51:02 +02:00
|
|
|
break;
|
|
|
|
case IR_XOR:
|
2023-08-30 11:03:43 +02:00
|
|
|
if (ir_type_size[type] == 8) {
|
|
|
|
uint64_t val = ctx->ir_base[op2].val.u64;
|
|
|
|
| ASM_REG_REG_IMM_OP eor, type, def_reg, op1_reg, val
|
|
|
|
} else {
|
|
|
|
| ASM_REG_REG_IMM_OP eor, type, def_reg, op1_reg, val
|
|
|
|
}
|
2022-05-31 23:51:02 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2023-06-14 19:23:32 +02:00
|
|
|
if (IR_REG_SPILLED(ctx->regs[def][0])) {
|
2022-05-31 23:51:02 +02:00
|
|
|
ir_emit_store(ctx, type, def, def_reg);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void ir_emit_min_max_int(ir_ctx *ctx, ir_ref def, ir_insn *insn)
|
|
|
|
{
|
|
|
|
ir_backend_data *data = ctx->data;
|
|
|
|
dasm_State **Dst = &data->dasm_state;
|
|
|
|
ir_type type = insn->type;
|
|
|
|
ir_ref op1 = insn->op1;
|
|
|
|
ir_ref op2 = insn->op2;
|
|
|
|
ir_reg def_reg = IR_REG_NUM(ctx->regs[def][0]);
|
|
|
|
ir_reg op1_reg = ctx->regs[def][1];
|
|
|
|
ir_reg op2_reg = ctx->regs[def][2];
|
|
|
|
|
2022-06-01 17:16:32 +02:00
|
|
|
IR_ASSERT(def_reg != IR_REG_NONE && op1_reg != IR_REG_NONE && op2_reg != IR_REG_NONE);
|
2022-05-31 23:51:02 +02:00
|
|
|
|
2023-06-14 19:23:32 +02:00
|
|
|
if (IR_REG_SPILLED(op1_reg) || IR_IS_CONST_REF(op1)) {
|
|
|
|
op1_reg = IR_REG_NUM(op1_reg);
|
2022-05-31 23:51:02 +02:00
|
|
|
ir_emit_load(ctx, type, op1_reg, op1);
|
|
|
|
}
|
2023-06-14 19:23:32 +02:00
|
|
|
if (IR_REG_SPILLED(op2_reg) || IR_IS_CONST_REF(op2)) {
|
|
|
|
op2_reg = IR_REG_NUM(op2_reg);
|
2022-06-01 17:16:32 +02:00
|
|
|
ir_emit_load(ctx, type, op2_reg, op2);
|
2022-05-31 23:51:02 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (op1 == op2) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-06-01 17:16:32 +02:00
|
|
|
if (ir_type_size[type] == 8) {
|
|
|
|
| cmp Rx(op1_reg), Rx(op2_reg)
|
|
|
|
if (insn->op == IR_MIN) {
|
|
|
|
if (IR_IS_TYPE_SIGNED(type)) {
|
|
|
|
| csel Rx(def_reg), Rx(op1_reg), Rx(op2_reg), le
|
|
|
|
} else {
|
|
|
|
| csel Rx(def_reg), Rx(op1_reg), Rx(op2_reg), ls
|
|
|
|
}
|
2022-05-31 23:51:02 +02:00
|
|
|
} else {
|
2022-06-01 17:16:32 +02:00
|
|
|
IR_ASSERT(insn->op == IR_MAX);
|
|
|
|
if (IR_IS_TYPE_SIGNED(type)) {
|
|
|
|
| csel Rx(def_reg), Rx(op1_reg), Rx(op2_reg), ge
|
|
|
|
} else {
|
|
|
|
| csel Rx(def_reg), Rx(op1_reg), Rx(op2_reg), hs
|
|
|
|
}
|
2022-05-31 23:51:02 +02:00
|
|
|
}
|
|
|
|
} else {
|
2022-06-01 17:16:32 +02:00
|
|
|
| cmp Rw(op1_reg), Rw(op2_reg)
|
|
|
|
if (insn->op == IR_MIN) {
|
|
|
|
if (IR_IS_TYPE_SIGNED(type)) {
|
|
|
|
| csel Rw(def_reg), Rw(op1_reg), Rw(op2_reg), le
|
|
|
|
} else {
|
|
|
|
| csel Rw(def_reg), Rw(op1_reg), Rw(op2_reg), ls
|
|
|
|
}
|
2022-05-31 23:51:02 +02:00
|
|
|
} else {
|
2022-06-01 17:16:32 +02:00
|
|
|
IR_ASSERT(insn->op == IR_MAX);
|
|
|
|
if (IR_IS_TYPE_SIGNED(type)) {
|
|
|
|
| csel Rw(def_reg), Rw(op1_reg), Rw(op2_reg), ge
|
|
|
|
} else {
|
|
|
|
| csel Rw(def_reg), Rw(op1_reg), Rw(op2_reg), hs
|
|
|
|
}
|
2022-05-31 23:51:02 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-06-14 19:23:32 +02:00
|
|
|
if (IR_REG_SPILLED(ctx->regs[def][0])) {
|
2022-05-31 23:51:02 +02:00
|
|
|
ir_emit_store(ctx, type, def, def_reg);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void ir_emit_overflow(ir_ctx *ctx, ir_ref def, ir_insn *insn)
|
|
|
|
{
|
|
|
|
ir_backend_data *data = ctx->data;
|
|
|
|
dasm_State **Dst = &data->dasm_state;
|
|
|
|
ir_reg def_reg = IR_REG_NUM(ctx->regs[def][0]);
|
2022-06-06 14:27:25 +02:00
|
|
|
ir_insn *math_insn = &ctx->ir_base[insn->op1];
|
|
|
|
ir_type type = math_insn->type;
|
2022-05-31 23:51:02 +02:00
|
|
|
|
|
|
|
IR_ASSERT(def_reg != IR_REG_NONE);
|
|
|
|
IR_ASSERT(IR_IS_TYPE_INT(type));
|
2022-06-06 14:27:25 +02:00
|
|
|
if (math_insn->op == IR_MUL_OV) {
|
|
|
|
| cset Rw(def_reg), ne
|
|
|
|
} else if (IR_IS_TYPE_SIGNED(type)) {
|
2022-06-01 17:16:32 +02:00
|
|
|
| cset Rw(def_reg), vs
|
2022-05-31 23:51:02 +02:00
|
|
|
} else {
|
2022-06-01 17:16:32 +02:00
|
|
|
| cset Rw(def_reg), cs
|
2022-05-31 23:51:02 +02:00
|
|
|
}
|
2023-06-14 19:23:32 +02:00
|
|
|
if (IR_REG_SPILLED(ctx->regs[def][0])) {
|
2022-05-31 23:51:02 +02:00
|
|
|
ir_emit_store(ctx, insn->type, def, def_reg);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-11-08 21:09:35 +01:00
|
|
|
static void ir_emit_overflow_and_branch(ir_ctx *ctx, uint32_t b, ir_ref def, ir_insn *insn)
|
2022-05-31 23:51:02 +02:00
|
|
|
{
|
|
|
|
ir_backend_data *data = ctx->data;
|
|
|
|
dasm_State **Dst = &data->dasm_state;
|
|
|
|
ir_insn *overflow_insn = &ctx->ir_base[insn->op2];
|
2022-06-06 14:27:25 +02:00
|
|
|
ir_insn *math_insn = &ctx->ir_base[overflow_insn->op1];
|
|
|
|
ir_type type = math_insn->type;
|
2022-11-08 21:09:35 +01:00
|
|
|
uint32_t true_block, false_block, next_block;
|
2022-05-31 23:51:02 +02:00
|
|
|
bool reverse = 0;
|
|
|
|
|
2022-06-06 21:36:11 +02:00
|
|
|
ir_get_true_false_blocks(ctx, b, &true_block, &false_block, &next_block);
|
2022-05-31 23:51:02 +02:00
|
|
|
if (true_block == next_block) {
|
|
|
|
reverse = 1;
|
|
|
|
true_block = false_block;
|
|
|
|
false_block = 0;
|
|
|
|
} else if (false_block == next_block) {
|
|
|
|
false_block = 0;
|
|
|
|
}
|
|
|
|
|
2022-06-06 14:27:25 +02:00
|
|
|
if (math_insn->op == IR_MUL_OV) {
|
|
|
|
if (reverse) {
|
|
|
|
| beq =>true_block
|
|
|
|
} else {
|
|
|
|
| bne =>true_block
|
|
|
|
}
|
|
|
|
} else if (IR_IS_TYPE_SIGNED(type)) {
|
2022-05-31 23:51:02 +02:00
|
|
|
if (reverse) {
|
2022-06-01 17:16:32 +02:00
|
|
|
| bvc =>true_block
|
2022-05-31 23:51:02 +02:00
|
|
|
} else {
|
2022-06-01 17:16:32 +02:00
|
|
|
| bvs =>true_block
|
2022-05-31 23:51:02 +02:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (reverse) {
|
2022-06-01 17:16:32 +02:00
|
|
|
| bcc =>true_block
|
2022-05-31 23:51:02 +02:00
|
|
|
} else {
|
2022-06-01 17:16:32 +02:00
|
|
|
| bcs =>true_block
|
2022-05-31 23:51:02 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if (false_block) {
|
|
|
|
| b =>false_block
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-06-28 15:57:35 +02:00
|
|
|
static void ir_emit_reg_binop_int(ir_ctx *ctx, ir_ref def, ir_insn *insn)
|
|
|
|
{
|
|
|
|
ir_backend_data *data = ctx->data;
|
|
|
|
dasm_State **Dst = &data->dasm_state;
|
|
|
|
ir_insn *op_insn = &ctx->ir_base[insn->op2];
|
|
|
|
ir_type type = op_insn->type;
|
|
|
|
ir_ref op2 = op_insn->op2;
|
|
|
|
ir_reg op2_reg = ctx->regs[insn->op2][2];
|
|
|
|
ir_reg reg;
|
|
|
|
|
|
|
|
IR_ASSERT(insn->op == IR_RSTORE);
|
|
|
|
reg = insn->op3;
|
|
|
|
|
|
|
|
if (op2_reg == IR_REG_NONE) {
|
|
|
|
ir_val *val = &ctx->ir_base[op2].val;
|
|
|
|
|
|
|
|
IR_ASSERT(IR_IS_CONST_REF(op2));
|
2023-12-27 08:18:56 +01:00
|
|
|
IR_ASSERT(!IR_IS_SYM_CONST(ctx->ir_base[op2].op));
|
2022-06-28 15:57:35 +02:00
|
|
|
switch (op_insn->op) {
|
2023-07-27 10:16:00 +02:00
|
|
|
default:
|
|
|
|
IR_ASSERT(0 && "NIY binary op");
|
2022-06-28 15:57:35 +02:00
|
|
|
case IR_ADD:
|
|
|
|
| ASM_REG_REG_IMM_OP add, type, reg, reg, val->i32
|
|
|
|
break;
|
|
|
|
case IR_SUB:
|
|
|
|
| ASM_REG_REG_IMM_OP sub, type, reg, reg, val->i32
|
|
|
|
break;
|
|
|
|
case IR_OR:
|
|
|
|
| ASM_REG_REG_IMM_OP orr, type, reg, reg, val->i32
|
|
|
|
break;
|
|
|
|
case IR_AND:
|
|
|
|
| ASM_REG_REG_IMM_OP and, type, reg, reg, val->i32
|
|
|
|
break;
|
|
|
|
case IR_XOR:
|
|
|
|
| ASM_REG_REG_IMM_OP eor, type, reg, reg, val->i32
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
} else {
|
2023-06-14 19:23:32 +02:00
|
|
|
if (IR_REG_SPILLED(op2_reg) || IR_IS_CONST_REF(op2)) {
|
|
|
|
op2_reg = IR_REG_NUM(op2_reg);
|
2022-06-28 15:57:35 +02:00
|
|
|
ir_emit_load(ctx, type, op2_reg, op2);
|
|
|
|
}
|
|
|
|
switch (op_insn->op) {
|
2023-07-27 10:16:00 +02:00
|
|
|
default:
|
|
|
|
IR_ASSERT(0 && "NIY binary op");
|
2022-06-28 15:57:35 +02:00
|
|
|
case IR_ADD:
|
|
|
|
| ASM_REG_REG_REG_OP add, type, reg, reg, op2_reg
|
|
|
|
break;
|
|
|
|
case IR_SUB:
|
|
|
|
| ASM_REG_REG_REG_OP sub, type, reg, reg, op2_reg
|
|
|
|
break;
|
|
|
|
case IR_MUL:
|
|
|
|
| ASM_REG_REG_REG_OP mul, type, reg, reg, op2_reg
|
|
|
|
break;
|
|
|
|
case IR_OR:
|
|
|
|
| ASM_REG_REG_REG_OP orr, type, reg, reg, op2_reg
|
|
|
|
break;
|
|
|
|
case IR_AND:
|
|
|
|
| ASM_REG_REG_REG_OP and, type, reg, reg, op2_reg
|
|
|
|
break;
|
|
|
|
case IR_XOR:
|
|
|
|
| ASM_REG_REG_REG_OP eor, type, reg, reg, op2_reg
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-05-31 23:51:02 +02:00
|
|
|
static void ir_emit_mul_div_mod_pwr2(ir_ctx *ctx, ir_ref def, ir_insn *insn)
|
|
|
|
{
|
|
|
|
ir_backend_data *data = ctx->data;
|
|
|
|
dasm_State **Dst = &data->dasm_state;
|
|
|
|
ir_type type = insn->type;
|
|
|
|
ir_ref op1 = insn->op1;
|
|
|
|
ir_reg def_reg = IR_REG_NUM(ctx->regs[def][0]);
|
|
|
|
ir_reg op1_reg = ctx->regs[def][1];
|
|
|
|
|
2023-12-27 08:18:56 +01:00
|
|
|
IR_ASSERT(IR_IS_CONST_REF(insn->op2));
|
|
|
|
IR_ASSERT(!IR_IS_SYM_CONST(ctx->ir_base[insn->op2].op));
|
2022-06-01 17:16:32 +02:00
|
|
|
IR_ASSERT(def_reg != IR_REG_NONE && op1_reg != IR_REG_NONE);
|
2022-05-31 23:51:02 +02:00
|
|
|
|
2023-06-14 19:23:32 +02:00
|
|
|
if (IR_REG_SPILLED(op1_reg) || IR_IS_CONST_REF(op1)) {
|
|
|
|
op1_reg = IR_REG_NUM(op1_reg);
|
2022-05-31 23:51:02 +02:00
|
|
|
ir_emit_load(ctx, type, op1_reg, op1);
|
|
|
|
}
|
|
|
|
if (insn->op == IR_MUL) {
|
|
|
|
uint32_t shift = IR_LOG2(ctx->ir_base[insn->op2].val.u64);
|
|
|
|
if (shift == 1) {
|
2022-06-01 17:16:32 +02:00
|
|
|
| ASM_REG_REG_REG_OP add, insn->type, def_reg, op1_reg, op1_reg
|
2022-05-31 23:51:02 +02:00
|
|
|
} else {
|
2022-06-01 17:16:32 +02:00
|
|
|
| ASM_REG_REG_IMM_OP lsl, insn->type, def_reg, op1_reg, shift
|
2022-05-31 23:51:02 +02:00
|
|
|
}
|
|
|
|
} else if (insn->op == IR_DIV) {
|
|
|
|
uint32_t shift = IR_LOG2(ctx->ir_base[insn->op2].val.u64);
|
2023-12-15 12:09:26 +01:00
|
|
|
IR_ASSERT(IR_IS_TYPE_UNSIGNED(insn->type));
|
2022-06-01 17:16:32 +02:00
|
|
|
| ASM_REG_REG_IMM_OP lsr, insn->type, def_reg, op1_reg, shift
|
2023-07-27 10:16:00 +02:00
|
|
|
} else {
|
|
|
|
IR_ASSERT(insn->op == IR_MOD);
|
2023-12-15 12:09:26 +01:00
|
|
|
IR_ASSERT(IR_IS_TYPE_UNSIGNED(insn->type));
|
2022-05-31 23:51:02 +02:00
|
|
|
uint64_t mask = ctx->ir_base[insn->op2].val.u64 - 1;
|
2022-06-01 17:16:32 +02:00
|
|
|
| ASM_REG_REG_IMM_OP and, insn->type, def_reg, op1_reg, mask
|
2022-05-31 23:51:02 +02:00
|
|
|
}
|
2023-06-14 19:23:32 +02:00
|
|
|
if (IR_REG_SPILLED(ctx->regs[def][0])) {
|
2022-05-31 23:51:02 +02:00
|
|
|
ir_emit_store(ctx, type, def, def_reg);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void ir_emit_shift(ir_ctx *ctx, ir_ref def, ir_insn *insn)
|
|
|
|
{
|
|
|
|
ir_backend_data *data = ctx->data;
|
|
|
|
dasm_State **Dst = &data->dasm_state;
|
|
|
|
ir_type type = insn->type;
|
|
|
|
ir_reg def_reg = IR_REG_NUM(ctx->regs[def][0]);
|
|
|
|
ir_reg op1_reg = ctx->regs[def][1];
|
|
|
|
ir_reg op2_reg = ctx->regs[def][2];
|
2022-06-02 14:12:56 +02:00
|
|
|
ir_reg tmp_reg;
|
2022-05-31 23:51:02 +02:00
|
|
|
|
2022-06-01 17:16:32 +02:00
|
|
|
IR_ASSERT(def_reg != IR_REG_NONE && op1_reg != IR_REG_NONE && op2_reg != IR_REG_NONE);
|
2023-06-14 19:23:32 +02:00
|
|
|
if (IR_REG_SPILLED(op1_reg) || IR_IS_CONST_REF(insn->op1)) {
|
|
|
|
op1_reg = IR_REG_NUM(op1_reg);
|
2022-05-31 23:51:02 +02:00
|
|
|
ir_emit_load(ctx, type, op1_reg, insn->op1);
|
|
|
|
}
|
2023-06-14 19:23:32 +02:00
|
|
|
if (IR_REG_SPILLED(op2_reg)) {
|
|
|
|
op2_reg = IR_REG_NUM(op2_reg);
|
2022-05-31 23:51:02 +02:00
|
|
|
ir_emit_load(ctx, type, op2_reg, insn->op2);
|
|
|
|
}
|
|
|
|
switch (insn->op) {
|
2023-07-27 10:16:00 +02:00
|
|
|
default:
|
|
|
|
IR_ASSERT(0);
|
2022-05-31 23:51:02 +02:00
|
|
|
case IR_SHL:
|
2022-06-02 14:12:56 +02:00
|
|
|
| ASM_REG_REG_REG_OP lsl, type, def_reg, op1_reg, op2_reg
|
2022-05-31 23:51:02 +02:00
|
|
|
break;
|
|
|
|
case IR_SHR:
|
2022-06-02 14:12:56 +02:00
|
|
|
| ASM_REG_REG_REG_OP lsr, type, def_reg, op1_reg, op2_reg
|
2022-05-31 23:51:02 +02:00
|
|
|
break;
|
|
|
|
case IR_SAR:
|
2022-06-02 14:12:56 +02:00
|
|
|
| ASM_REG_REG_REG_OP asr, type, def_reg, op1_reg, op2_reg
|
2022-05-31 23:51:02 +02:00
|
|
|
break;
|
|
|
|
case IR_ROL:
|
2022-06-02 14:12:56 +02:00
|
|
|
tmp_reg = ctx->regs[def][3];
|
|
|
|
IR_ASSERT(tmp_reg != IR_REG_NONE);
|
|
|
|
if (ir_type_size[type] == 8) {
|
|
|
|
| neg Rx(tmp_reg), Rx(op2_reg)
|
|
|
|
| ror Rx(def_reg), Rx(op1_reg), Rx(tmp_reg)
|
|
|
|
} else {
|
|
|
|
| neg Rw(tmp_reg), Rw(op2_reg)
|
|
|
|
| ror Rw(def_reg), Rw(op1_reg), Rw(tmp_reg)
|
|
|
|
}
|
2022-05-31 23:51:02 +02:00
|
|
|
break;
|
|
|
|
case IR_ROR:
|
2022-06-02 14:12:56 +02:00
|
|
|
| ASM_REG_REG_REG_OP ror, type, def_reg, op1_reg, op2_reg
|
2022-05-31 23:51:02 +02:00
|
|
|
break;
|
|
|
|
}
|
2023-06-14 19:23:32 +02:00
|
|
|
if (IR_REG_SPILLED(ctx->regs[def][0])) {
|
2022-05-31 23:51:02 +02:00
|
|
|
ir_emit_store(ctx, type, def, def_reg);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void ir_emit_shift_const(ir_ctx *ctx, ir_ref def, ir_insn *insn)
|
|
|
|
{
|
|
|
|
ir_backend_data *data = ctx->data;
|
|
|
|
dasm_State **Dst = &data->dasm_state;
|
|
|
|
uint32_t shift = ctx->ir_base[insn->op2].val.u64;
|
|
|
|
ir_type type = insn->type;
|
|
|
|
ir_ref op1 = insn->op1;
|
|
|
|
ir_reg def_reg = IR_REG_NUM(ctx->regs[def][0]);
|
|
|
|
ir_reg op1_reg = ctx->regs[def][1];
|
|
|
|
|
2023-12-27 08:18:56 +01:00
|
|
|
IR_ASSERT(IR_IS_CONST_REF(insn->op2));
|
|
|
|
IR_ASSERT(!IR_IS_SYM_CONST(ctx->ir_base[insn->op2].op));
|
2022-06-01 17:16:32 +02:00
|
|
|
IR_ASSERT(def_reg != IR_REG_NONE && op1_reg != IR_REG_NONE);
|
2022-05-31 23:51:02 +02:00
|
|
|
|
2023-06-14 19:23:32 +02:00
|
|
|
if (IR_REG_SPILLED(op1_reg) || IR_IS_CONST_REF(op1)) {
|
|
|
|
op1_reg = IR_REG_NUM(op1_reg);
|
2022-05-31 23:51:02 +02:00
|
|
|
ir_emit_load(ctx, type, op1_reg, op1);
|
|
|
|
}
|
|
|
|
switch (insn->op) {
|
2023-07-27 10:16:00 +02:00
|
|
|
default:
|
|
|
|
IR_ASSERT(0);
|
2022-05-31 23:51:02 +02:00
|
|
|
case IR_SHL:
|
2022-06-02 14:12:56 +02:00
|
|
|
| ASM_REG_REG_IMM_OP lsl, type, def_reg, op1_reg, shift
|
2022-05-31 23:51:02 +02:00
|
|
|
break;
|
|
|
|
case IR_SHR:
|
2022-06-02 14:12:56 +02:00
|
|
|
| ASM_REG_REG_IMM_OP lsr, type, def_reg, op1_reg, shift
|
2022-05-31 23:51:02 +02:00
|
|
|
break;
|
|
|
|
case IR_SAR:
|
2022-06-02 14:12:56 +02:00
|
|
|
| ASM_REG_REG_IMM_OP asr, type, def_reg, op1_reg, shift
|
2022-05-31 23:51:02 +02:00
|
|
|
break;
|
|
|
|
case IR_ROL:
|
2022-06-02 14:12:56 +02:00
|
|
|
if (ir_type_size[type] == 8) {
|
|
|
|
shift = (64 - shift) % 64;
|
|
|
|
| ror Rx(def_reg), Rx(op1_reg), #shift
|
|
|
|
} else {
|
|
|
|
shift = (32 - shift) % 32;
|
|
|
|
| ror Rw(def_reg), Rw(op1_reg), #shift
|
|
|
|
}
|
2022-05-31 23:51:02 +02:00
|
|
|
break;
|
|
|
|
case IR_ROR:
|
2022-06-02 14:12:56 +02:00
|
|
|
| ASM_REG_REG_IMM_OP ror, type, def_reg, op1_reg, shift
|
2022-05-31 23:51:02 +02:00
|
|
|
break;
|
|
|
|
}
|
2023-06-14 19:23:32 +02:00
|
|
|
if (IR_REG_SPILLED(ctx->regs[def][0])) {
|
2022-05-31 23:51:02 +02:00
|
|
|
ir_emit_store(ctx, type, def, def_reg);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void ir_emit_op_int(ir_ctx *ctx, ir_ref def, ir_insn *insn)
|
|
|
|
{
|
|
|
|
ir_backend_data *data = ctx->data;
|
|
|
|
dasm_State **Dst = &data->dasm_state;
|
|
|
|
ir_type type = insn->type;
|
|
|
|
ir_ref op1 = insn->op1;
|
|
|
|
ir_reg def_reg = IR_REG_NUM(ctx->regs[def][0]);
|
|
|
|
ir_reg op1_reg = ctx->regs[def][1];
|
|
|
|
|
|
|
|
IR_ASSERT(def_reg != IR_REG_NONE && op1_reg != IR_REG_NONE);
|
|
|
|
|
2023-06-14 19:23:32 +02:00
|
|
|
if (IR_REG_SPILLED(op1_reg) || IR_IS_CONST_REF(op1)) {
|
|
|
|
op1_reg = IR_REG_NUM(op1_reg);
|
2022-05-31 23:51:02 +02:00
|
|
|
ir_emit_load(ctx, type, op1_reg, op1);
|
|
|
|
}
|
2022-06-01 17:16:32 +02:00
|
|
|
if (insn->op == IR_NOT) {
|
|
|
|
if (insn->type == IR_BOOL) {
|
2023-12-27 08:18:56 +01:00
|
|
|
| ASM_REG_IMM_OP cmp, type, op1_reg, 0
|
2022-06-01 17:16:32 +02:00
|
|
|
| cset Rw(def_reg), eq
|
2022-05-31 23:51:02 +02:00
|
|
|
} else {
|
2022-06-01 17:16:32 +02:00
|
|
|
| ASM_REG_REG_OP mvn, insn->type, def_reg, op1_reg
|
2022-05-31 23:51:02 +02:00
|
|
|
}
|
2022-06-01 17:16:32 +02:00
|
|
|
} else if (insn->op == IR_NEG) {
|
|
|
|
| ASM_REG_REG_OP neg, insn->type, def_reg, op1_reg
|
|
|
|
} else if (insn->op == IR_ABS) {
|
|
|
|
if (ir_type_size[type] == 8) {
|
|
|
|
| cmp Rx(op1_reg), #0
|
|
|
|
| cneg Rx(def_reg), Rx(op1_reg), lt
|
2022-05-31 23:51:02 +02:00
|
|
|
} else {
|
2022-06-01 17:16:32 +02:00
|
|
|
| cmp Rw(op1_reg), #0
|
|
|
|
| cneg Rw(def_reg), Rw(op1_reg), lt
|
2022-05-31 23:51:02 +02:00
|
|
|
}
|
2023-11-16 20:48:05 +01:00
|
|
|
} else if (insn->op == IR_CTLZ) {
|
|
|
|
if (ir_type_size[type] == 1) {
|
|
|
|
| and Rw(def_reg), Rw(op1_reg), #0xff
|
|
|
|
| clz Rw(def_reg), Rw(def_reg)
|
|
|
|
| sub Rw(def_reg), Rw(def_reg), #24
|
|
|
|
} else if (ir_type_size[type] == 2) {
|
|
|
|
| and Rw(def_reg), Rw(op1_reg), #0xffff
|
|
|
|
| clz Rw(def_reg), Rw(def_reg)
|
|
|
|
| sub Rw(def_reg), Rw(def_reg), #16
|
|
|
|
} else {
|
|
|
|
| ASM_REG_REG_OP clz, type, def_reg, op1_reg
|
|
|
|
}
|
|
|
|
} else if (insn->op == IR_CTTZ) {
|
|
|
|
| ASM_REG_REG_OP rbit, insn->type, def_reg, op1_reg
|
|
|
|
| ASM_REG_REG_OP clz, insn->type, def_reg, def_reg
|
2022-05-31 23:51:02 +02:00
|
|
|
} else {
|
2023-07-27 10:16:00 +02:00
|
|
|
IR_ASSERT(insn->op == IR_BSWAP);
|
|
|
|
| ASM_REG_REG_OP rev, insn->type, def_reg, op1_reg
|
2022-05-31 23:51:02 +02:00
|
|
|
}
|
2023-06-14 19:23:32 +02:00
|
|
|
if (IR_REG_SPILLED(ctx->regs[def][0])) {
|
2022-05-31 23:51:02 +02:00
|
|
|
ir_emit_store(ctx, type, def, def_reg);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-11-16 17:28:12 +01:00
|
|
|
static void ir_emit_ctpop(ir_ctx *ctx, ir_ref def, ir_insn *insn)
|
|
|
|
{
|
|
|
|
ir_backend_data *data = ctx->data;
|
|
|
|
dasm_State **Dst = &data->dasm_state;
|
|
|
|
ir_type type = insn->type;
|
|
|
|
ir_ref op1 = insn->op1;
|
|
|
|
ir_reg def_reg = IR_REG_NUM(ctx->regs[def][0]);
|
|
|
|
ir_reg op1_reg = ctx->regs[def][1];
|
|
|
|
ir_reg tmp_reg = ctx->regs[def][2];
|
|
|
|
uint32_t code1 = 0x0e205800 | (tmp_reg-IR_REG_FP_FIRST); // cnt v0.8b, v0.8b
|
|
|
|
uint32_t code2 = 0x0e31b800 | (tmp_reg-IR_REG_FP_FIRST); // addv b0, v0.8b
|
|
|
|
|
|
|
|
IR_ASSERT(def_reg != IR_REG_NONE && op1_reg != IR_REG_NONE && tmp_reg != IR_REG_NONE);
|
|
|
|
|
|
|
|
if (IR_REG_SPILLED(op1_reg) || IR_IS_CONST_REF(op1)) {
|
|
|
|
op1_reg = IR_REG_NUM(op1_reg);
|
|
|
|
ir_emit_load(ctx, type, op1_reg, op1);
|
|
|
|
}
|
|
|
|
switch (ir_type_size[insn->type]) {
|
|
|
|
default:
|
|
|
|
IR_ASSERT(0);
|
|
|
|
case 1:
|
2023-11-16 20:48:05 +01:00
|
|
|
| and Rw(def_reg), Rw(op1_reg), #0xff
|
2023-11-16 17:28:12 +01:00
|
|
|
| fmov Rs(tmp_reg-IR_REG_FP_FIRST), Rw(def_reg)
|
|
|
|
| .long code1 // cnt v0.8b, v0.8b
|
|
|
|
| .long code2 // addv b0, v0.8b
|
|
|
|
| fmov Rw(def_reg), Rs(tmp_reg-IR_REG_FP_FIRST)
|
|
|
|
break;
|
|
|
|
case 2:
|
2023-11-16 20:48:05 +01:00
|
|
|
| and Rw(def_reg), Rw(op1_reg), #0xffff
|
2023-11-16 17:28:12 +01:00
|
|
|
| fmov Rs(tmp_reg-IR_REG_FP_FIRST), Rw(def_reg)
|
|
|
|
| .long code1 // cnt v0.8b, v0.8b
|
|
|
|
| .long code2 // addv b0, v0.8b
|
|
|
|
| fmov Rw(def_reg), Rs(tmp_reg-IR_REG_FP_FIRST)
|
|
|
|
break;
|
|
|
|
case 4:
|
|
|
|
| fmov Rs(tmp_reg-IR_REG_FP_FIRST), Rw(op1_reg)
|
|
|
|
| .long code1 // cnt v0.8b, v0.8b
|
|
|
|
| .long code2 // addv b0, v0.8b
|
|
|
|
| fmov Rw(def_reg), Rs(tmp_reg-IR_REG_FP_FIRST)
|
|
|
|
break;
|
|
|
|
case 8:
|
|
|
|
| fmov Rd(tmp_reg-IR_REG_FP_FIRST), Rx(op1_reg)
|
|
|
|
| .long code1 // cnt v0.8b, v0.8b
|
|
|
|
| .long code2 // addv b0, v0.8b
|
|
|
|
| fmov Rx(def_reg), Rd(tmp_reg-IR_REG_FP_FIRST)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (IR_REG_SPILLED(ctx->regs[def][0])) {
|
|
|
|
ir_emit_store(ctx, type, def, def_reg);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-05-31 23:51:02 +02:00
|
|
|
static void ir_emit_op_fp(ir_ctx *ctx, ir_ref def, ir_insn *insn)
|
|
|
|
{
|
|
|
|
ir_backend_data *data = ctx->data;
|
|
|
|
dasm_State **Dst = &data->dasm_state;
|
|
|
|
ir_type type = insn->type;
|
|
|
|
ir_ref op1 = insn->op1;
|
|
|
|
ir_reg def_reg = IR_REG_NUM(ctx->regs[def][0]);
|
|
|
|
ir_reg op1_reg = ctx->regs[def][1];
|
|
|
|
|
2022-06-01 17:16:32 +02:00
|
|
|
IR_ASSERT(def_reg != IR_REG_NONE && op1_reg != IR_REG_NONE);
|
2022-05-31 23:51:02 +02:00
|
|
|
|
2023-06-14 19:23:32 +02:00
|
|
|
if (IR_REG_SPILLED(op1_reg) || IR_IS_CONST_REF(op1)) {
|
|
|
|
op1_reg = IR_REG_NUM(op1_reg);
|
2022-05-31 23:51:02 +02:00
|
|
|
ir_emit_load(ctx, type, op1_reg, op1);
|
|
|
|
}
|
|
|
|
if (insn->op == IR_NEG) {
|
2022-06-02 14:12:56 +02:00
|
|
|
if (type == IR_DOUBLE) {
|
|
|
|
| fneg Rd(def_reg-IR_REG_FP_FIRST), Rd(op1_reg-IR_REG_FP_FIRST)
|
2022-05-31 23:51:02 +02:00
|
|
|
} else {
|
2022-06-02 14:12:56 +02:00
|
|
|
IR_ASSERT(type == IR_FLOAT);
|
|
|
|
| fneg Rs(def_reg-IR_REG_FP_FIRST), Rs(op1_reg-IR_REG_FP_FIRST)
|
2022-05-31 23:51:02 +02:00
|
|
|
}
|
2023-07-27 10:16:00 +02:00
|
|
|
} else {
|
|
|
|
IR_ASSERT(insn->op == IR_ABS);
|
2022-06-02 14:12:56 +02:00
|
|
|
if (type == IR_DOUBLE) {
|
|
|
|
| fabs Rd(def_reg-IR_REG_FP_FIRST), Rd(op1_reg-IR_REG_FP_FIRST)
|
2022-05-31 23:51:02 +02:00
|
|
|
} else {
|
2022-06-02 14:12:56 +02:00
|
|
|
IR_ASSERT(type == IR_FLOAT);
|
|
|
|
| fabs Rs(def_reg-IR_REG_FP_FIRST), Rs(op1_reg-IR_REG_FP_FIRST)
|
2022-05-31 23:51:02 +02:00
|
|
|
}
|
|
|
|
}
|
2023-06-14 19:23:32 +02:00
|
|
|
if (IR_REG_SPILLED(ctx->regs[def][0])) {
|
2022-05-31 23:51:02 +02:00
|
|
|
ir_emit_store(ctx, insn->type, def, def_reg);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void ir_emit_binop_fp(ir_ctx *ctx, ir_ref def, ir_insn *insn)
|
|
|
|
{
|
|
|
|
ir_backend_data *data = ctx->data;
|
|
|
|
dasm_State **Dst = &data->dasm_state;
|
|
|
|
ir_type type = insn->type;
|
|
|
|
ir_ref op1 = insn->op1;
|
|
|
|
ir_ref op2 = insn->op2;
|
|
|
|
ir_reg def_reg = IR_REG_NUM(ctx->regs[def][0]);
|
|
|
|
ir_reg op1_reg = ctx->regs[def][1];
|
|
|
|
ir_reg op2_reg = ctx->regs[def][2];
|
|
|
|
|
|
|
|
IR_ASSERT(def_reg != IR_REG_NONE && op1_reg != IR_REG_NONE && op2_reg != IR_REG_NONE);
|
2023-06-14 19:23:32 +02:00
|
|
|
if (IR_REG_SPILLED(op1_reg) || IR_IS_CONST_REF(op1)) {
|
|
|
|
op1_reg = IR_REG_NUM(op1_reg);
|
2022-05-31 23:51:02 +02:00
|
|
|
ir_emit_load(ctx, type, op1_reg, op1);
|
|
|
|
}
|
2023-06-14 19:23:32 +02:00
|
|
|
if (IR_REG_SPILLED(op2_reg) || IR_IS_CONST_REF(op2)) {
|
|
|
|
op2_reg = IR_REG_NUM(op2_reg);
|
2022-05-31 23:51:02 +02:00
|
|
|
if (op1 != op2) {
|
|
|
|
ir_emit_load(ctx, type, op2_reg, op2);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
switch (insn->op) {
|
2023-07-27 10:16:00 +02:00
|
|
|
default:
|
|
|
|
IR_ASSERT(0 && "NIY binary op");
|
2022-05-31 23:51:02 +02:00
|
|
|
case IR_ADD:
|
|
|
|
| ASM_FP_REG_REG_REG_OP fadd, type, def_reg, op1_reg, op2_reg
|
|
|
|
break;
|
|
|
|
case IR_SUB:
|
|
|
|
| ASM_FP_REG_REG_REG_OP fsub, type, def_reg, op1_reg, op2_reg
|
|
|
|
break;
|
|
|
|
case IR_MUL:
|
|
|
|
| ASM_FP_REG_REG_REG_OP fmul, type, def_reg, op1_reg, op2_reg
|
|
|
|
break;
|
|
|
|
case IR_DIV:
|
|
|
|
| ASM_FP_REG_REG_REG_OP fdiv, type, def_reg, op1_reg, op2_reg
|
|
|
|
break;
|
|
|
|
case IR_MIN:
|
2022-06-01 17:16:32 +02:00
|
|
|
| ASM_FP_REG_REG_REG_OP fmin, type, def_reg, op1_reg, op2_reg
|
2022-05-31 23:51:02 +02:00
|
|
|
break;
|
|
|
|
case IR_MAX:
|
2022-06-01 17:16:32 +02:00
|
|
|
| ASM_FP_REG_REG_REG_OP fmax, type, def_reg, op1_reg, op2_reg
|
2022-05-31 23:51:02 +02:00
|
|
|
break;
|
|
|
|
}
|
2023-06-14 19:23:32 +02:00
|
|
|
if (IR_REG_SPILLED(ctx->regs[def][0])) {
|
2022-05-31 23:51:02 +02:00
|
|
|
ir_emit_store(ctx, insn->type, def, def_reg);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void ir_emit_cmp_int_common(ir_ctx *ctx, ir_type type, ir_reg op1_reg, ir_ref op1, ir_reg op2_reg, ir_ref op2)
|
|
|
|
{
|
|
|
|
ir_backend_data *data = ctx->data;
|
|
|
|
dasm_State **Dst = &data->dasm_state;
|
|
|
|
|
|
|
|
IR_ASSERT(op1_reg != IR_REG_NONE);
|
2023-12-28 16:44:32 +01:00
|
|
|
// TODO: prevent repeatable sign/zero extension ???
|
|
|
|
if (ir_type_size[type] == 2) {
|
|
|
|
if (IR_IS_TYPE_SIGNED(type)) {
|
|
|
|
| sxth Rw(op1_reg), Rw(op1_reg)
|
|
|
|
} else {
|
|
|
|
| uxth Rw(op1_reg), Rw(op1_reg)
|
|
|
|
}
|
|
|
|
} else if (ir_type_size[type] == 1) {
|
|
|
|
if (IR_IS_TYPE_SIGNED(type)) {
|
|
|
|
| sxtb Rw(op1_reg), Rw(op1_reg)
|
|
|
|
} else {
|
|
|
|
| uxtb Rw(op1_reg), Rw(op1_reg)
|
|
|
|
}
|
|
|
|
}
|
2022-05-31 23:51:02 +02:00
|
|
|
if (op2_reg != IR_REG_NONE) {
|
|
|
|
if (ir_type_size[type] == 8) {
|
|
|
|
| cmp Rx(op1_reg), Rx(op2_reg)
|
2023-12-28 16:44:32 +01:00
|
|
|
} else if (ir_type_size[type] == 4) {
|
2022-05-31 23:51:02 +02:00
|
|
|
| cmp Rw(op1_reg), Rw(op2_reg)
|
2023-12-28 16:44:32 +01:00
|
|
|
} else if (ir_type_size[type] == 2) {
|
|
|
|
if (IR_IS_TYPE_SIGNED(type)) {
|
|
|
|
| cmp Rw(op1_reg), Rw(op2_reg), sxth
|
|
|
|
} else {
|
|
|
|
| cmp Rw(op1_reg), Rw(op2_reg), uxth
|
|
|
|
}
|
|
|
|
} else if (ir_type_size[type] == 1) {
|
|
|
|
if (IR_IS_TYPE_SIGNED(type)) {
|
|
|
|
| cmp Rw(op1_reg), Rw(op2_reg), sxtb
|
|
|
|
} else {
|
|
|
|
| cmp Rw(op1_reg), Rw(op2_reg), uxtb
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
IR_ASSERT(0);
|
2022-05-31 23:51:02 +02:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
IR_ASSERT(IR_IS_CONST_REF(op2));
|
2023-12-27 08:18:56 +01:00
|
|
|
IR_ASSERT(!IR_IS_SYM_CONST(ctx->ir_base[op2].op));
|
2022-05-31 23:51:02 +02:00
|
|
|
int32_t val = ctx->ir_base[op2].val.i32;
|
|
|
|
|
|
|
|
if (ir_type_size[type] == 8) {
|
|
|
|
| cmp Rx(op1_reg), #val
|
|
|
|
} else {
|
|
|
|
| cmp Rw(op1_reg), #val
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void ir_emit_cmp_int(ir_ctx *ctx, ir_ref def, ir_insn *insn)
|
|
|
|
{
|
|
|
|
ir_backend_data *data = ctx->data;
|
|
|
|
dasm_State **Dst = &data->dasm_state;
|
|
|
|
ir_type type = ctx->ir_base[insn->op1].type;
|
|
|
|
ir_op op = insn->op;
|
|
|
|
ir_ref op1 = insn->op1;
|
|
|
|
ir_ref op2 = insn->op2;
|
|
|
|
ir_reg def_reg = IR_REG_NUM(ctx->regs[def][0]);
|
|
|
|
ir_reg op1_reg = ctx->regs[def][1];
|
|
|
|
ir_reg op2_reg = ctx->regs[def][2];
|
|
|
|
|
|
|
|
IR_ASSERT(def_reg != IR_REG_NONE && op1_reg != IR_REG_NONE);
|
2023-06-14 19:23:32 +02:00
|
|
|
if (IR_REG_SPILLED(op1_reg) || IR_IS_CONST_REF(op1)) {
|
|
|
|
op1_reg = IR_REG_NUM(op1_reg);
|
2022-05-31 23:51:02 +02:00
|
|
|
ir_emit_load(ctx, type, op1_reg, op1);
|
|
|
|
}
|
|
|
|
if (op2_reg != IR_REG_NONE) {
|
2023-06-14 19:23:32 +02:00
|
|
|
if (IR_REG_SPILLED(op2_reg)) {
|
|
|
|
op2_reg = IR_REG_NUM(op2_reg);
|
2022-05-31 23:51:02 +02:00
|
|
|
if (op1 != op2) {
|
|
|
|
ir_emit_load(ctx, type, op2_reg, op2);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (IR_IS_CONST_REF(op2)) {
|
|
|
|
ir_emit_load(ctx, type, op2_reg, op2);
|
|
|
|
}
|
|
|
|
}
|
2023-12-27 08:18:56 +01:00
|
|
|
if (IR_IS_CONST_REF(insn->op2)
|
|
|
|
&& !IR_IS_SYM_CONST(ctx->ir_base[insn->op2].op)
|
|
|
|
&& ctx->ir_base[insn->op2].val.u64 == 0) {
|
2022-05-31 23:51:02 +02:00
|
|
|
if (op == IR_ULT) {
|
|
|
|
/* always false */
|
2022-06-01 17:16:32 +02:00
|
|
|
ir_emit_load_imm_int(ctx, IR_BOOL, def_reg, 0);
|
2023-06-14 19:23:32 +02:00
|
|
|
if (IR_REG_SPILLED(ctx->regs[def][0])) {
|
2022-05-31 23:51:02 +02:00
|
|
|
ir_emit_store(ctx, insn->type, def, def_reg);
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
} else if (op == IR_UGE) {
|
|
|
|
/* always true */
|
2022-06-01 17:16:32 +02:00
|
|
|
ir_emit_load_imm_int(ctx, IR_BOOL, def_reg, 1);
|
2023-06-14 19:23:32 +02:00
|
|
|
if (IR_REG_SPILLED(ctx->regs[def][0])) {
|
2022-05-31 23:51:02 +02:00
|
|
|
ir_emit_store(ctx, insn->type, def, def_reg);
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
} else if (op == IR_ULE) {
|
|
|
|
op = IR_EQ;
|
|
|
|
} else if (op == IR_UGT) {
|
|
|
|
op = IR_NE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
ir_emit_cmp_int_common(ctx, type, op1_reg, op1, op2_reg, op2);
|
|
|
|
switch (op) {
|
2023-07-27 10:16:00 +02:00
|
|
|
default:
|
|
|
|
IR_ASSERT(0 && "NIY binary op");
|
2022-05-31 23:51:02 +02:00
|
|
|
case IR_EQ:
|
2022-06-01 17:16:32 +02:00
|
|
|
| cset Rw(def_reg), eq
|
2022-05-31 23:51:02 +02:00
|
|
|
break;
|
|
|
|
case IR_NE:
|
2022-06-01 17:16:32 +02:00
|
|
|
| cset Rw(def_reg), ne
|
2022-05-31 23:51:02 +02:00
|
|
|
break;
|
|
|
|
case IR_LT:
|
2022-06-01 17:16:32 +02:00
|
|
|
| cset Rw(def_reg), lt
|
2022-05-31 23:51:02 +02:00
|
|
|
break;
|
|
|
|
case IR_GE:
|
2022-06-01 17:16:32 +02:00
|
|
|
| cset Rw(def_reg), ge
|
2022-05-31 23:51:02 +02:00
|
|
|
break;
|
|
|
|
case IR_LE:
|
2022-06-01 17:16:32 +02:00
|
|
|
| cset Rw(def_reg), le
|
2022-05-31 23:51:02 +02:00
|
|
|
break;
|
|
|
|
case IR_GT:
|
2022-06-01 17:16:32 +02:00
|
|
|
| cset Rw(def_reg), gt
|
2022-05-31 23:51:02 +02:00
|
|
|
break;
|
|
|
|
case IR_ULT:
|
2022-06-01 17:16:32 +02:00
|
|
|
| cset Rw(def_reg), lo
|
2022-05-31 23:51:02 +02:00
|
|
|
break;
|
|
|
|
case IR_UGE:
|
2022-06-01 17:16:32 +02:00
|
|
|
| cset Rw(def_reg), hs
|
2022-05-31 23:51:02 +02:00
|
|
|
break;
|
|
|
|
case IR_ULE:
|
2022-06-01 17:16:32 +02:00
|
|
|
| cset Rw(def_reg), ls
|
2022-05-31 23:51:02 +02:00
|
|
|
break;
|
|
|
|
case IR_UGT:
|
2022-06-01 17:16:32 +02:00
|
|
|
| cset Rw(def_reg), hi
|
2022-05-31 23:51:02 +02:00
|
|
|
break;
|
|
|
|
}
|
2023-06-14 19:23:32 +02:00
|
|
|
if (IR_REG_SPILLED(ctx->regs[def][0])) {
|
2022-05-31 23:51:02 +02:00
|
|
|
ir_emit_store(ctx, insn->type, def, def_reg);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static ir_op ir_emit_cmp_fp_common(ir_ctx *ctx, ir_ref cmp_ref, ir_insn *cmp_insn)
|
|
|
|
{
|
|
|
|
ir_backend_data *data = ctx->data;
|
|
|
|
dasm_State **Dst = &data->dasm_state;
|
|
|
|
ir_type type = ctx->ir_base[cmp_insn->op1].type;
|
|
|
|
ir_op op = cmp_insn->op;
|
|
|
|
ir_ref op1, op2;
|
|
|
|
ir_reg op1_reg, op2_reg;
|
|
|
|
|
|
|
|
if (op == IR_LT || op == IR_LE) {
|
|
|
|
/* swap operands to avoid P flag check */
|
|
|
|
op ^= 3;
|
|
|
|
op1 = cmp_insn->op2;
|
|
|
|
op2 = cmp_insn->op1;
|
|
|
|
op1_reg = ctx->regs[cmp_ref][2];
|
|
|
|
op2_reg = ctx->regs[cmp_ref][1];
|
|
|
|
} else {
|
|
|
|
op1 = cmp_insn->op1;
|
|
|
|
op2 = cmp_insn->op2;
|
|
|
|
op1_reg = ctx->regs[cmp_ref][1];
|
|
|
|
op2_reg = ctx->regs[cmp_ref][2];
|
|
|
|
}
|
|
|
|
|
|
|
|
IR_ASSERT(op1_reg != IR_REG_NONE && op2_reg != IR_REG_NONE);
|
2023-06-14 19:23:32 +02:00
|
|
|
if (IR_REG_SPILLED(op1_reg) || IR_IS_CONST_REF(op1)) {
|
|
|
|
op1_reg = IR_REG_NUM(op1_reg);
|
2022-05-31 23:51:02 +02:00
|
|
|
ir_emit_load(ctx, type, op1_reg, op1);
|
|
|
|
}
|
2023-06-14 19:23:32 +02:00
|
|
|
if (IR_REG_SPILLED(op2_reg) || IR_IS_CONST_REF(op2)) {
|
|
|
|
op2_reg = IR_REG_NUM(op2_reg);
|
2022-05-31 23:51:02 +02:00
|
|
|
if (op1 != op2) {
|
|
|
|
ir_emit_load(ctx, type, op2_reg, op2);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (type == IR_DOUBLE) {
|
|
|
|
| fcmp Rd(op1_reg-IR_REG_FP_FIRST), Rd(op2_reg-IR_REG_FP_FIRST)
|
|
|
|
} else {
|
|
|
|
IR_ASSERT(type == IR_FLOAT);
|
|
|
|
| fcmp Rs(op1_reg-IR_REG_FP_FIRST), Rs(op2_reg-IR_REG_FP_FIRST)
|
|
|
|
}
|
|
|
|
return op;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void ir_emit_cmp_fp(ir_ctx *ctx, ir_ref def, ir_insn *insn)
|
|
|
|
{
|
|
|
|
ir_backend_data *data = ctx->data;
|
|
|
|
dasm_State **Dst = &data->dasm_state;
|
|
|
|
ir_op op = ir_emit_cmp_fp_common(ctx, def, insn);
|
|
|
|
ir_reg def_reg = IR_REG_NUM(ctx->regs[def][0]);
|
2023-10-03 07:34:02 +02:00
|
|
|
//??? ir_reg tmp_reg = ctx->regs[def][3]; // TODO: take into account vs flag
|
2022-05-31 23:51:02 +02:00
|
|
|
|
|
|
|
IR_ASSERT(def_reg != IR_REG_NONE);
|
|
|
|
switch (op) {
|
2023-07-27 10:16:00 +02:00
|
|
|
default:
|
|
|
|
IR_ASSERT(0 && "NIY binary op");
|
2022-05-31 23:51:02 +02:00
|
|
|
case IR_EQ:
|
2022-06-02 14:12:56 +02:00
|
|
|
| cset Rw(def_reg), eq
|
2022-05-31 23:51:02 +02:00
|
|
|
break;
|
|
|
|
case IR_NE:
|
2022-06-02 14:12:56 +02:00
|
|
|
| cset Rw(def_reg), ne
|
2022-05-31 23:51:02 +02:00
|
|
|
break;
|
|
|
|
case IR_LT:
|
2022-09-19 20:55:08 +02:00
|
|
|
| cset Rw(def_reg), mi
|
2022-05-31 23:51:02 +02:00
|
|
|
break;
|
|
|
|
case IR_GE:
|
2022-09-19 20:55:08 +02:00
|
|
|
| cset Rw(def_reg), ge
|
2022-05-31 23:51:02 +02:00
|
|
|
break;
|
|
|
|
case IR_LE:
|
2022-06-02 14:12:56 +02:00
|
|
|
| cset Rw(def_reg), ls
|
2022-05-31 23:51:02 +02:00
|
|
|
break;
|
|
|
|
case IR_GT:
|
2022-09-19 20:55:08 +02:00
|
|
|
| cset Rw(def_reg), gt
|
2022-05-31 23:51:02 +02:00
|
|
|
break;
|
2023-10-24 09:22:04 +02:00
|
|
|
case IR_ULT:
|
|
|
|
| cset Rw(def_reg), lt
|
|
|
|
break;
|
|
|
|
case IR_UGE:
|
|
|
|
| cset Rw(def_reg), hs
|
|
|
|
break;
|
|
|
|
case IR_ULE:
|
|
|
|
| cset Rw(def_reg), le
|
|
|
|
break;
|
|
|
|
case IR_UGT:
|
|
|
|
| cset Rw(def_reg), hi
|
|
|
|
break;
|
2022-05-31 23:51:02 +02:00
|
|
|
}
|
2023-06-14 19:23:32 +02:00
|
|
|
if (IR_REG_SPILLED(ctx->regs[def][0])) {
|
2022-05-31 23:51:02 +02:00
|
|
|
ir_emit_store(ctx, insn->type, def, def_reg);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-11-08 21:09:35 +01:00
|
|
|
static void ir_emit_jmp_true(ir_ctx *ctx, uint32_t b, ir_ref def)
|
2022-05-31 23:51:02 +02:00
|
|
|
{
|
2022-11-08 21:09:35 +01:00
|
|
|
uint32_t true_block, false_block, next_block;
|
2022-05-31 23:51:02 +02:00
|
|
|
ir_backend_data *data = ctx->data;
|
|
|
|
dasm_State **Dst = &data->dasm_state;
|
|
|
|
|
2022-06-06 21:36:11 +02:00
|
|
|
ir_get_true_false_blocks(ctx, b, &true_block, &false_block, &next_block);
|
|
|
|
if (true_block != next_block) {
|
2022-05-31 23:51:02 +02:00
|
|
|
| b =>true_block
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-11-08 21:09:35 +01:00
|
|
|
static void ir_emit_jmp_false(ir_ctx *ctx, uint32_t b, ir_ref def)
|
2022-05-31 23:51:02 +02:00
|
|
|
{
|
2022-11-08 21:09:35 +01:00
|
|
|
uint32_t true_block, false_block, next_block;
|
2022-05-31 23:51:02 +02:00
|
|
|
ir_backend_data *data = ctx->data;
|
|
|
|
dasm_State **Dst = &data->dasm_state;
|
|
|
|
|
2022-06-06 21:36:11 +02:00
|
|
|
ir_get_true_false_blocks(ctx, b, &true_block, &false_block, &next_block);
|
|
|
|
if (false_block != next_block) {
|
2022-05-31 23:51:02 +02:00
|
|
|
| b =>false_block
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-11-08 21:09:35 +01:00
|
|
|
static void ir_emit_jz(ir_ctx *ctx, uint8_t op, uint32_t b, ir_type type, ir_reg reg)
|
2022-06-28 15:36:06 +02:00
|
|
|
{
|
2022-11-08 21:09:35 +01:00
|
|
|
uint32_t true_block, false_block, next_block;
|
2022-06-28 15:36:06 +02:00
|
|
|
ir_backend_data *data = ctx->data;
|
|
|
|
dasm_State **Dst = &data->dasm_state;
|
|
|
|
|
|
|
|
ir_get_true_false_blocks(ctx, b, &true_block, &false_block, &next_block);
|
|
|
|
if (true_block == next_block) {
|
|
|
|
IR_ASSERT(op < IR_LT);
|
|
|
|
op ^= 1; // reverse
|
|
|
|
true_block = false_block;
|
|
|
|
false_block = 0;
|
|
|
|
} else if (false_block == next_block) {
|
|
|
|
false_block = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (op == IR_EQ) {
|
|
|
|
if (ir_type_size[type] == 8) {
|
|
|
|
| cbz Rx(reg), =>true_block
|
|
|
|
} else {
|
|
|
|
| cbz Rw(reg), =>true_block
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
IR_ASSERT(op == IR_NE);
|
|
|
|
if (ir_type_size[type] == 8) {
|
|
|
|
| cbnz Rx(reg), =>true_block
|
|
|
|
} else {
|
|
|
|
| cbnz Rw(reg), =>true_block
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (false_block) {
|
|
|
|
| b =>false_block
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-11-08 21:09:35 +01:00
|
|
|
static void ir_emit_jcc(ir_ctx *ctx, uint8_t op, uint32_t b, ir_ref def, ir_insn *insn, bool int_cmp)
|
2022-05-31 23:51:02 +02:00
|
|
|
{
|
2022-11-08 21:09:35 +01:00
|
|
|
uint32_t true_block, false_block, next_block;
|
2022-05-31 23:51:02 +02:00
|
|
|
ir_backend_data *data = ctx->data;
|
|
|
|
dasm_State **Dst = &data->dasm_state;
|
|
|
|
|
2022-06-06 21:36:11 +02:00
|
|
|
ir_get_true_false_blocks(ctx, b, &true_block, &false_block, &next_block);
|
2022-05-31 23:51:02 +02:00
|
|
|
if (true_block == next_block) {
|
2022-08-25 17:16:17 +02:00
|
|
|
/* swap to avoid unconditional JMP */
|
2023-10-24 09:22:04 +02:00
|
|
|
if (int_cmp || op == IR_EQ || op == IR_NE) {
|
|
|
|
op ^= 1; // reverse
|
|
|
|
} else {
|
|
|
|
op ^= 5; // reverse
|
|
|
|
}
|
2022-08-25 17:16:17 +02:00
|
|
|
true_block = false_block;
|
|
|
|
false_block = 0;
|
2022-05-31 23:51:02 +02:00
|
|
|
} else if (false_block == next_block) {
|
|
|
|
false_block = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (int_cmp) {
|
|
|
|
switch (op) {
|
2023-07-27 10:16:00 +02:00
|
|
|
default:
|
|
|
|
IR_ASSERT(0 && "NIY binary op");
|
2022-05-31 23:51:02 +02:00
|
|
|
case IR_EQ:
|
|
|
|
| beq =>true_block
|
|
|
|
break;
|
|
|
|
case IR_NE:
|
|
|
|
| bne =>true_block
|
|
|
|
break;
|
|
|
|
case IR_LT:
|
|
|
|
| blt =>true_block
|
|
|
|
break;
|
|
|
|
case IR_GE:
|
|
|
|
| bge =>true_block
|
|
|
|
break;
|
|
|
|
case IR_LE:
|
|
|
|
| ble =>true_block
|
|
|
|
break;
|
|
|
|
case IR_GT:
|
|
|
|
| bgt =>true_block
|
|
|
|
break;
|
|
|
|
case IR_ULT:
|
|
|
|
| blo =>true_block
|
|
|
|
break;
|
|
|
|
case IR_UGE:
|
|
|
|
| bhs =>true_block
|
|
|
|
break;
|
|
|
|
case IR_ULE:
|
|
|
|
| bls =>true_block
|
|
|
|
break;
|
|
|
|
case IR_UGT:
|
|
|
|
| bhi =>true_block
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
switch (op) {
|
2023-07-27 10:16:00 +02:00
|
|
|
default:
|
|
|
|
IR_ASSERT(0 && "NIY binary op");
|
2022-05-31 23:51:02 +02:00
|
|
|
case IR_EQ:
|
2022-09-19 20:55:08 +02:00
|
|
|
| beq =>true_block
|
2022-05-31 23:51:02 +02:00
|
|
|
break;
|
|
|
|
case IR_NE:
|
|
|
|
| bne =>true_block
|
|
|
|
break;
|
|
|
|
case IR_LT:
|
2022-09-19 20:55:08 +02:00
|
|
|
| bmi =>true_block
|
2022-05-31 23:51:02 +02:00
|
|
|
break;
|
|
|
|
case IR_GE:
|
2022-09-19 20:55:08 +02:00
|
|
|
| bge =>true_block
|
2022-05-31 23:51:02 +02:00
|
|
|
break;
|
|
|
|
case IR_LE:
|
2022-09-19 20:55:08 +02:00
|
|
|
| bls =>true_block
|
2022-05-31 23:51:02 +02:00
|
|
|
break;
|
|
|
|
case IR_GT:
|
2022-09-19 20:55:08 +02:00
|
|
|
| bgt =>true_block
|
2022-05-31 23:51:02 +02:00
|
|
|
break;
|
2023-10-24 09:22:04 +02:00
|
|
|
case IR_ULT:
|
|
|
|
| blt =>true_block
|
|
|
|
break;
|
|
|
|
case IR_UGE:
|
|
|
|
| bhs =>true_block
|
|
|
|
break;
|
|
|
|
case IR_ULE:
|
|
|
|
| ble =>true_block
|
|
|
|
break;
|
|
|
|
case IR_UGT:
|
|
|
|
| bhi =>true_block
|
|
|
|
break;
|
2022-05-31 23:51:02 +02:00
|
|
|
// case IR_ULT: fprintf(stderr, "\tjb .LL%d\n", true_block); break;
|
|
|
|
// case IR_UGE: fprintf(stderr, "\tjae .LL%d\n", true_block); break;
|
|
|
|
// case IR_ULE: fprintf(stderr, "\tjbe .LL%d\n", true_block); break;
|
|
|
|
// case IR_UGT: fprintf(stderr, "\tja .LL%d\n", true_block); break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (false_block) {
|
|
|
|
| b =>false_block
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-11-08 21:09:35 +01:00
|
|
|
static void ir_emit_cmp_and_branch_int(ir_ctx *ctx, uint32_t b, ir_ref def, ir_insn *insn)
|
2022-05-31 23:51:02 +02:00
|
|
|
{
|
|
|
|
ir_insn *cmp_insn = &ctx->ir_base[insn->op2];
|
|
|
|
ir_op op = cmp_insn->op;
|
|
|
|
ir_type type = ctx->ir_base[cmp_insn->op1].type;
|
|
|
|
ir_ref op1 = cmp_insn->op1;
|
|
|
|
ir_ref op2 = cmp_insn->op2;
|
|
|
|
ir_reg op1_reg = ctx->regs[insn->op2][1];
|
|
|
|
ir_reg op2_reg = ctx->regs[insn->op2][2];
|
|
|
|
|
2023-06-14 19:23:32 +02:00
|
|
|
if (op1_reg != IR_REG_NONE && IR_REG_SPILLED(op1_reg)) {
|
|
|
|
op1_reg = IR_REG_NUM(op1_reg);
|
2022-05-31 23:51:02 +02:00
|
|
|
ir_emit_load(ctx, type, op1_reg, op1);
|
|
|
|
}
|
|
|
|
if (op1_reg != IR_REG_NONE && IR_IS_CONST_REF(op1)) {
|
|
|
|
ir_emit_load(ctx, type, op1_reg, op1);
|
|
|
|
}
|
|
|
|
if (op2_reg != IR_REG_NONE) {
|
2023-06-14 19:23:32 +02:00
|
|
|
if (IR_REG_SPILLED(op2_reg)) {
|
|
|
|
op2_reg = IR_REG_NUM(op2_reg);
|
2022-05-31 23:51:02 +02:00
|
|
|
if (op1 != op2) {
|
|
|
|
ir_emit_load(ctx, type, op2_reg, op2);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (IR_IS_CONST_REF(op2)) {
|
|
|
|
ir_emit_load(ctx, type, op2_reg, op2);
|
|
|
|
}
|
|
|
|
}
|
2023-12-27 08:18:56 +01:00
|
|
|
if (IR_IS_CONST_REF(op2)
|
|
|
|
&& !IR_IS_SYM_CONST(ctx->ir_base[op2].op)
|
|
|
|
&& ctx->ir_base[op2].val.u64 == 0) {
|
2022-05-31 23:51:02 +02:00
|
|
|
if (op == IR_ULT) {
|
|
|
|
/* always false */
|
|
|
|
ir_emit_jmp_false(ctx, b, def);
|
|
|
|
return;
|
|
|
|
} else if (op == IR_UGE) {
|
|
|
|
/* always true */
|
|
|
|
ir_emit_jmp_true(ctx, b, def);
|
|
|
|
return;
|
|
|
|
} else if (op == IR_ULE) {
|
|
|
|
op = IR_EQ;
|
|
|
|
} else if (op == IR_UGT) {
|
|
|
|
op = IR_NE;
|
|
|
|
}
|
2022-06-28 15:36:06 +02:00
|
|
|
if (op1_reg != IR_REG_NONE && (op == IR_EQ || op == IR_NE)) {
|
|
|
|
ir_emit_jz(ctx, op, b, type, op1_reg);
|
|
|
|
return;
|
|
|
|
}
|
2022-05-31 23:51:02 +02:00
|
|
|
}
|
|
|
|
ir_emit_cmp_int_common(ctx, type, op1_reg, op1, op2_reg, op2);
|
|
|
|
ir_emit_jcc(ctx, op, b, def, insn, 1);
|
|
|
|
}
|
|
|
|
|
2022-11-08 21:09:35 +01:00
|
|
|
static void ir_emit_cmp_and_branch_fp(ir_ctx *ctx, uint32_t b, ir_ref def, ir_insn *insn)
|
2022-05-31 23:51:02 +02:00
|
|
|
{
|
|
|
|
ir_op op = ir_emit_cmp_fp_common(ctx, insn->op2, &ctx->ir_base[insn->op2]);
|
|
|
|
ir_emit_jcc(ctx, op, b, def, insn, 0);
|
|
|
|
}
|
|
|
|
|
2022-11-08 21:09:35 +01:00
|
|
|
static void ir_emit_if_int(ir_ctx *ctx, uint32_t b, ir_ref def, ir_insn *insn)
|
2022-05-31 23:51:02 +02:00
|
|
|
{
|
|
|
|
ir_type type = ctx->ir_base[insn->op2].type;
|
|
|
|
ir_reg op2_reg = ctx->regs[def][2];
|
|
|
|
ir_backend_data *data = ctx->data;
|
|
|
|
dasm_State **Dst = &data->dasm_state;
|
|
|
|
|
2022-11-29 18:02:07 +01:00
|
|
|
if (IR_IS_CONST_REF(insn->op2)) {
|
|
|
|
uint32_t true_block, false_block, next_block;
|
|
|
|
|
|
|
|
ir_get_true_false_blocks(ctx, b, &true_block, &false_block, &next_block);
|
|
|
|
if (ir_const_is_true(&ctx->ir_base[insn->op2])) {
|
|
|
|
if (true_block != next_block) {
|
|
|
|
| b =>true_block
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (false_block != next_block) {
|
|
|
|
| b =>false_block
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
2022-06-02 14:12:56 +02:00
|
|
|
IR_ASSERT(op2_reg != IR_REG_NONE);
|
2023-06-14 19:23:32 +02:00
|
|
|
if (IR_REG_SPILLED(op2_reg) || IR_IS_CONST_REF(insn->op2)) {
|
|
|
|
op2_reg = IR_REG_NUM(op2_reg);
|
2022-06-02 14:12:56 +02:00
|
|
|
ir_emit_load(ctx, type, op2_reg, insn->op2);
|
2022-05-31 23:51:02 +02:00
|
|
|
}
|
2022-06-02 14:12:56 +02:00
|
|
|
| ASM_REG_IMM_OP cmp, type, op2_reg, 0
|
2022-05-31 23:51:02 +02:00
|
|
|
ir_emit_jcc(ctx, IR_NE, b, def, insn, 1);
|
|
|
|
}
|
|
|
|
|
2023-10-13 19:50:23 +02:00
|
|
|
static void ir_emit_cond(ir_ctx *ctx, ir_ref def, ir_insn *insn)
|
|
|
|
{
|
|
|
|
ir_backend_data *data = ctx->data;
|
|
|
|
dasm_State **Dst = &data->dasm_state;
|
|
|
|
ir_type type = insn->type;
|
|
|
|
ir_ref op1 = insn->op1;
|
|
|
|
ir_ref op2 = insn->op2;
|
|
|
|
ir_ref op3 = insn->op3;
|
|
|
|
ir_type op1_type = ctx->ir_base[op1].type;
|
|
|
|
ir_reg def_reg = IR_REG_NUM(ctx->regs[def][0]);
|
|
|
|
ir_reg op1_reg = ctx->regs[def][1];
|
|
|
|
ir_reg op2_reg = ctx->regs[def][2];
|
|
|
|
ir_reg op3_reg = ctx->regs[def][3];
|
|
|
|
|
|
|
|
IR_ASSERT(def_reg != IR_REG_NONE);
|
|
|
|
|
|
|
|
if (IR_REG_SPILLED(op2_reg) || IR_IS_CONST_REF(op2)) {
|
|
|
|
op2_reg = IR_REG_NUM(op2_reg);
|
|
|
|
ir_emit_load(ctx, type, op2_reg, op2);
|
|
|
|
if (op1 == op2) {
|
|
|
|
op1_reg = op2_reg;
|
|
|
|
}
|
|
|
|
if (op3 == op2) {
|
|
|
|
op3_reg = op2_reg;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (op3 != op2 && (IR_REG_SPILLED(op3_reg) || IR_IS_CONST_REF(op3))) {
|
|
|
|
op3_reg = IR_REG_NUM(op3_reg);
|
|
|
|
ir_emit_load(ctx, type, op3_reg, op3);
|
|
|
|
if (op1 == op2) {
|
|
|
|
op1_reg = op3_reg;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (op1 != op2 && op1 != op3 && (IR_REG_SPILLED(op1_reg) || IR_IS_CONST_REF(op1))) {
|
|
|
|
op1_reg = IR_REG_NUM(op1_reg);
|
|
|
|
ir_emit_load(ctx, op1_type, op1_reg, op1);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (IR_IS_TYPE_INT(op1_type)) {
|
|
|
|
| ASM_REG_IMM_OP cmp, op1_type, op1_reg, 0
|
|
|
|
} else{
|
|
|
|
| ASM_FP_REG_IMM_OP fcmp, op1_type, op1_reg, 0.0
|
|
|
|
}
|
|
|
|
|
|
|
|
if (IR_IS_TYPE_INT(type)) {
|
|
|
|
if (ir_type_size[type] == 8) {
|
2023-10-24 09:22:04 +02:00
|
|
|
| csel Rx(def_reg), Rx(op2_reg), Rx(op3_reg), ne
|
2023-10-13 19:50:23 +02:00
|
|
|
} else {
|
2023-10-24 09:22:04 +02:00
|
|
|
| csel Rw(def_reg), Rw(op2_reg), Rw(op3_reg), ne
|
2023-10-13 19:50:23 +02:00
|
|
|
}
|
|
|
|
} else{
|
|
|
|
if (type == IR_DOUBLE) {
|
2023-10-24 09:22:04 +02:00
|
|
|
| fcsel Rd(def_reg-IR_REG_FP_FIRST), Rd(op2_reg-IR_REG_FP_FIRST), Rd(op3_reg-IR_REG_FP_FIRST), ne
|
2023-10-13 19:50:23 +02:00
|
|
|
} else {
|
2023-10-24 09:22:04 +02:00
|
|
|
| fcsel Rs(def_reg-IR_REG_FP_FIRST), Rs(op2_reg-IR_REG_FP_FIRST), Rs(op3_reg-IR_REG_FP_FIRST), ne
|
2023-10-13 19:50:23 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (IR_REG_SPILLED(ctx->regs[def][0])) {
|
|
|
|
ir_emit_store(ctx, type, def, def_reg);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-05-31 23:51:02 +02:00
|
|
|
static void ir_emit_return_void(ir_ctx *ctx)
|
|
|
|
{
|
|
|
|
ir_backend_data *data = ctx->data;
|
|
|
|
dasm_State **Dst = &data->dasm_state;
|
|
|
|
|
|
|
|
ir_emit_epilogue(ctx);
|
|
|
|
| ret
|
|
|
|
}
|
|
|
|
|
2022-11-17 21:30:35 +01:00
|
|
|
static void ir_emit_return_int(ir_ctx *ctx, ir_ref ref, ir_insn *insn)
|
2022-05-31 23:51:02 +02:00
|
|
|
{
|
|
|
|
ir_reg op2_reg = ctx->regs[ref][2];
|
|
|
|
|
|
|
|
if (op2_reg != IR_REG_INT_RET1) {
|
|
|
|
ir_type type = ctx->ir_base[insn->op2].type;
|
|
|
|
|
2023-06-14 19:23:32 +02:00
|
|
|
if (op2_reg != IR_REG_NONE && !IR_REG_SPILLED(op2_reg)) {
|
2022-06-06 17:10:41 +02:00
|
|
|
ir_emit_mov(ctx, type, IR_REG_INT_RET1, op2_reg);
|
2022-05-31 23:51:02 +02:00
|
|
|
} else {
|
|
|
|
ir_emit_load(ctx, type, IR_REG_INT_RET1, insn->op2);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
ir_emit_return_void(ctx);
|
|
|
|
}
|
|
|
|
|
2022-11-17 21:30:35 +01:00
|
|
|
static void ir_emit_return_fp(ir_ctx *ctx, ir_ref ref, ir_insn *insn)
|
2022-05-31 23:51:02 +02:00
|
|
|
{
|
|
|
|
ir_reg op2_reg = ctx->regs[ref][2];
|
|
|
|
ir_type type = ctx->ir_base[insn->op2].type;
|
|
|
|
|
|
|
|
if (op2_reg != IR_REG_FP_RET1) {
|
2023-06-14 19:23:32 +02:00
|
|
|
if (op2_reg != IR_REG_NONE && !IR_REG_SPILLED(op2_reg)) {
|
2022-06-06 17:10:41 +02:00
|
|
|
ir_emit_fp_mov(ctx, type, IR_REG_FP_RET1, op2_reg);
|
2022-05-31 23:51:02 +02:00
|
|
|
} else {
|
|
|
|
ir_emit_load(ctx, type, IR_REG_FP_RET1, insn->op2);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
ir_emit_return_void(ctx);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void ir_emit_sext(ir_ctx *ctx, ir_ref def, ir_insn *insn)
|
|
|
|
{
|
|
|
|
ir_type dst_type = insn->type;
|
|
|
|
ir_type src_type = ctx->ir_base[insn->op1].type;
|
|
|
|
ir_backend_data *data = ctx->data;
|
|
|
|
dasm_State **Dst = &data->dasm_state;
|
|
|
|
ir_reg def_reg = IR_REG_NUM(ctx->regs[def][0]);
|
|
|
|
ir_reg op1_reg = ctx->regs[def][1];
|
|
|
|
|
|
|
|
IR_ASSERT(IR_IS_TYPE_INT(src_type));
|
|
|
|
IR_ASSERT(IR_IS_TYPE_INT(dst_type));
|
|
|
|
IR_ASSERT(ir_type_size[dst_type] > ir_type_size[src_type]);
|
|
|
|
IR_ASSERT(def_reg != IR_REG_NONE);
|
2023-06-14 19:23:32 +02:00
|
|
|
if ((op1_reg != IR_REG_NONE) && (IR_REG_SPILLED(op1_reg) || IR_IS_CONST_REF(insn->op1))) {
|
|
|
|
op1_reg = IR_REG_NUM(op1_reg);
|
2022-05-31 23:51:02 +02:00
|
|
|
ir_emit_load(ctx, src_type, op1_reg, insn->op1);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (op1_reg != IR_REG_NONE) {
|
|
|
|
if (ir_type_size[src_type] == 1) {
|
|
|
|
if (ir_type_size[dst_type] == 2) {
|
2022-06-02 14:12:56 +02:00
|
|
|
| sxtb Rw(def_reg), Rw(op1_reg)
|
2022-05-31 23:51:02 +02:00
|
|
|
} else if (ir_type_size[dst_type] == 4) {
|
2022-06-02 14:12:56 +02:00
|
|
|
| sxtb Rw(def_reg), Rw(op1_reg)
|
2022-05-31 23:51:02 +02:00
|
|
|
} else {
|
|
|
|
IR_ASSERT(ir_type_size[dst_type] == 8);
|
2022-06-02 14:12:56 +02:00
|
|
|
| sxtb Rx(def_reg), Rx(op1_reg)
|
2022-05-31 23:51:02 +02:00
|
|
|
}
|
|
|
|
} else if (ir_type_size[src_type] == 2) {
|
|
|
|
if (ir_type_size[dst_type] == 4) {
|
2022-06-02 14:12:56 +02:00
|
|
|
| sxth Rw(def_reg), Rw(op1_reg)
|
2022-05-31 23:51:02 +02:00
|
|
|
} else {
|
|
|
|
IR_ASSERT(ir_type_size[dst_type] == 8);
|
2022-06-02 14:12:56 +02:00
|
|
|
| sxth Rx(def_reg), Rx(op1_reg)
|
2022-05-31 23:51:02 +02:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
IR_ASSERT(ir_type_size[src_type] == 4);
|
|
|
|
IR_ASSERT(ir_type_size[dst_type] == 8);
|
2022-06-02 14:12:56 +02:00
|
|
|
| sxtw Rx(def_reg), Rw(op1_reg)
|
2022-05-31 23:51:02 +02:00
|
|
|
}
|
|
|
|
} else if (IR_IS_CONST_REF(insn->op1)) {
|
|
|
|
IR_ASSERT(0);
|
|
|
|
} else {
|
2022-09-07 22:47:30 +02:00
|
|
|
ir_reg fp;
|
|
|
|
int32_t offset = ir_ref_spill_slot(ctx, insn->op1, &fp);
|
2022-05-31 23:51:02 +02:00
|
|
|
|
|
|
|
if (ir_type_size[src_type] == 1) {
|
|
|
|
if (ir_type_size[dst_type] == 2) {
|
2022-06-02 14:12:56 +02:00
|
|
|
| ldrsb Rw(def_reg), [Rx(fp), #offset]
|
2022-05-31 23:51:02 +02:00
|
|
|
} else if (ir_type_size[dst_type] == 4) {
|
2022-06-02 14:12:56 +02:00
|
|
|
| ldrsb Rw(def_reg), [Rx(fp), #offset]
|
2022-05-31 23:51:02 +02:00
|
|
|
} else {
|
|
|
|
IR_ASSERT(ir_type_size[dst_type] == 8);
|
2022-06-02 14:12:56 +02:00
|
|
|
| ldrsb Rx(def_reg), [Rx(fp), #offset]
|
2022-05-31 23:51:02 +02:00
|
|
|
}
|
|
|
|
} else if (ir_type_size[src_type] == 2) {
|
|
|
|
if (ir_type_size[dst_type] == 4) {
|
2022-06-02 14:12:56 +02:00
|
|
|
| ldrsh Rw(def_reg), [Rx(fp), #offset]
|
2022-05-31 23:51:02 +02:00
|
|
|
} else {
|
|
|
|
IR_ASSERT(ir_type_size[dst_type] == 8);
|
2022-06-02 14:12:56 +02:00
|
|
|
| ldrsh Rx(def_reg), [Rx(fp), #offset]
|
2022-05-31 23:51:02 +02:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
IR_ASSERT(ir_type_size[src_type] == 4);
|
|
|
|
IR_ASSERT(ir_type_size[dst_type] == 8);
|
2022-06-02 14:12:56 +02:00
|
|
|
| ldrsw Rx(def_reg), [Rx(fp), #offset]
|
2022-05-31 23:51:02 +02:00
|
|
|
}
|
|
|
|
}
|
2023-06-14 19:23:32 +02:00
|
|
|
if (IR_REG_SPILLED(ctx->regs[def][0])) {
|
2022-05-31 23:51:02 +02:00
|
|
|
ir_emit_store(ctx, dst_type, def, def_reg);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void ir_emit_zext(ir_ctx *ctx, ir_ref def, ir_insn *insn)
|
|
|
|
{
|
|
|
|
ir_type dst_type = insn->type;
|
|
|
|
ir_type src_type = ctx->ir_base[insn->op1].type;
|
|
|
|
ir_backend_data *data = ctx->data;
|
|
|
|
dasm_State **Dst = &data->dasm_state;
|
|
|
|
ir_reg def_reg = IR_REG_NUM(ctx->regs[def][0]);
|
|
|
|
ir_reg op1_reg = ctx->regs[def][1];
|
|
|
|
|
|
|
|
IR_ASSERT(IR_IS_TYPE_INT(src_type));
|
|
|
|
IR_ASSERT(IR_IS_TYPE_INT(dst_type));
|
|
|
|
IR_ASSERT(ir_type_size[dst_type] > ir_type_size[src_type]);
|
|
|
|
IR_ASSERT(def_reg != IR_REG_NONE);
|
2023-06-14 19:23:32 +02:00
|
|
|
if ((op1_reg != IR_REG_NONE) && (IR_REG_SPILLED(op1_reg) || IR_IS_CONST_REF(insn->op1))) {
|
|
|
|
op1_reg = IR_REG_NUM(op1_reg);
|
2022-05-31 23:51:02 +02:00
|
|
|
ir_emit_load(ctx, src_type, op1_reg, insn->op1);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (op1_reg != IR_REG_NONE) {
|
|
|
|
if (ir_type_size[src_type] == 1) {
|
2022-06-02 14:12:56 +02:00
|
|
|
| uxtb Rw(def_reg), Rw(op1_reg)
|
2022-05-31 23:51:02 +02:00
|
|
|
} else if (ir_type_size[src_type] == 2) {
|
2022-06-02 14:12:56 +02:00
|
|
|
| uxth Rw(def_reg), Rw(op1_reg)
|
2022-05-31 23:51:02 +02:00
|
|
|
} else {
|
2022-06-02 14:12:56 +02:00
|
|
|
| mov Rw(def_reg), Rw(op1_reg)
|
2022-05-31 23:51:02 +02:00
|
|
|
}
|
|
|
|
} else if (IR_IS_CONST_REF(insn->op1)) {
|
|
|
|
IR_ASSERT(0);
|
|
|
|
} else {
|
2022-09-07 22:47:30 +02:00
|
|
|
ir_reg fp;
|
|
|
|
int32_t offset = ir_ref_spill_slot(ctx, insn->op1, &fp);
|
2022-05-31 23:51:02 +02:00
|
|
|
|
|
|
|
if (ir_type_size[src_type] == 1) {
|
2022-06-02 14:12:56 +02:00
|
|
|
| ldrb Rw(def_reg), [Rx(fp), #offset]
|
2022-05-31 23:51:02 +02:00
|
|
|
} else if (ir_type_size[src_type] == 2) {
|
2022-06-02 14:12:56 +02:00
|
|
|
| ldrh Rw(def_reg), [Rx(fp), #offset]
|
2022-05-31 23:51:02 +02:00
|
|
|
} else {
|
|
|
|
IR_ASSERT(ir_type_size[src_type] == 4);
|
|
|
|
IR_ASSERT(ir_type_size[dst_type] == 8);
|
2022-06-02 14:12:56 +02:00
|
|
|
| ldr Rw(def_reg), [Rx(fp), #offset]
|
2022-05-31 23:51:02 +02:00
|
|
|
}
|
|
|
|
}
|
2023-06-14 19:23:32 +02:00
|
|
|
if (IR_REG_SPILLED(ctx->regs[def][0])) {
|
2022-05-31 23:51:02 +02:00
|
|
|
ir_emit_store(ctx, dst_type, def, def_reg);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void ir_emit_trunc(ir_ctx *ctx, ir_ref def, ir_insn *insn)
|
|
|
|
{
|
2022-12-07 09:56:53 +01:00
|
|
|
ir_backend_data *data = ctx->data;
|
|
|
|
dasm_State **Dst = &data->dasm_state;
|
2022-05-31 23:51:02 +02:00
|
|
|
ir_type dst_type = insn->type;
|
|
|
|
ir_type src_type = ctx->ir_base[insn->op1].type;
|
|
|
|
ir_reg def_reg = IR_REG_NUM(ctx->regs[def][0]);
|
|
|
|
ir_reg op1_reg = ctx->regs[def][1];
|
|
|
|
|
|
|
|
IR_ASSERT(IR_IS_TYPE_INT(src_type));
|
|
|
|
IR_ASSERT(IR_IS_TYPE_INT(dst_type));
|
|
|
|
IR_ASSERT(ir_type_size[dst_type] < ir_type_size[src_type]);
|
|
|
|
IR_ASSERT(def_reg != IR_REG_NONE);
|
2023-06-14 19:23:32 +02:00
|
|
|
if (op1_reg != IR_REG_NONE && IR_REG_SPILLED(op1_reg)) {
|
|
|
|
op1_reg = IR_REG_NUM(op1_reg);
|
2022-05-31 23:51:02 +02:00
|
|
|
ir_emit_load(ctx, src_type, op1_reg, insn->op1);
|
|
|
|
}
|
|
|
|
if (op1_reg != IR_REG_NONE) {
|
2022-12-07 09:56:53 +01:00
|
|
|
if (ir_type_size[dst_type] == 1) {
|
|
|
|
| and Rw(def_reg), Rw(op1_reg), #0xff
|
|
|
|
} else if (ir_type_size[dst_type] == 2) {
|
|
|
|
| and Rw(def_reg), Rw(op1_reg), #0xffff
|
|
|
|
} else if (op1_reg != def_reg) {
|
2022-05-31 23:51:02 +02:00
|
|
|
ir_emit_mov(ctx, dst_type, def_reg, op1_reg);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
ir_emit_load(ctx, dst_type, def_reg, insn->op1);
|
|
|
|
}
|
2023-06-14 19:23:32 +02:00
|
|
|
if (IR_REG_SPILLED(ctx->regs[def][0])) {
|
2022-05-31 23:51:02 +02:00
|
|
|
ir_emit_store(ctx, dst_type, def, def_reg);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void ir_emit_bitcast(ir_ctx *ctx, ir_ref def, ir_insn *insn)
|
|
|
|
{
|
|
|
|
ir_type dst_type = insn->type;
|
|
|
|
ir_type src_type = ctx->ir_base[insn->op1].type;
|
|
|
|
ir_backend_data *data = ctx->data;
|
|
|
|
dasm_State **Dst = &data->dasm_state;
|
|
|
|
ir_reg def_reg = IR_REG_NUM(ctx->regs[def][0]);
|
|
|
|
ir_reg op1_reg = ctx->regs[def][1];
|
|
|
|
|
|
|
|
IR_ASSERT(ir_type_size[dst_type] == ir_type_size[src_type]);
|
|
|
|
IR_ASSERT(def_reg != IR_REG_NONE);
|
2023-06-14 19:23:32 +02:00
|
|
|
if (op1_reg != IR_REG_NONE && IR_REG_SPILLED(op1_reg)) {
|
|
|
|
op1_reg = IR_REG_NUM(op1_reg);
|
2022-05-31 23:51:02 +02:00
|
|
|
ir_emit_load(ctx, src_type, op1_reg, insn->op1);
|
|
|
|
}
|
|
|
|
if (IR_IS_TYPE_INT(src_type) && IR_IS_TYPE_INT(dst_type)) {
|
|
|
|
if (op1_reg != IR_REG_NONE) {
|
2023-06-14 19:23:32 +02:00
|
|
|
if (IR_REG_SPILLED(op1_reg)) {
|
|
|
|
op1_reg = IR_REG_NUM(op1_reg);
|
2022-12-15 21:27:30 +01:00
|
|
|
ir_emit_load(ctx, src_type, op1_reg, insn->op1);
|
|
|
|
}
|
2022-05-31 23:51:02 +02:00
|
|
|
if (op1_reg != def_reg) {
|
|
|
|
ir_emit_mov(ctx, dst_type, def_reg, op1_reg);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
ir_emit_load(ctx, dst_type, def_reg, insn->op1);
|
|
|
|
}
|
|
|
|
} else if (IR_IS_TYPE_FP(src_type) && IR_IS_TYPE_FP(dst_type)) {
|
|
|
|
if (op1_reg != IR_REG_NONE) {
|
2023-06-14 19:23:32 +02:00
|
|
|
if (IR_REG_SPILLED(op1_reg)) {
|
|
|
|
op1_reg = IR_REG_NUM(op1_reg);
|
2022-12-15 21:27:30 +01:00
|
|
|
ir_emit_load(ctx, src_type, op1_reg, insn->op1);
|
|
|
|
}
|
2022-05-31 23:51:02 +02:00
|
|
|
if (op1_reg != def_reg) {
|
|
|
|
ir_emit_fp_mov(ctx, dst_type, def_reg, op1_reg);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
ir_emit_load(ctx, dst_type, def_reg, insn->op1);
|
|
|
|
}
|
|
|
|
} else if (IR_IS_TYPE_FP(src_type)) {
|
|
|
|
IR_ASSERT(IR_IS_TYPE_INT(dst_type));
|
|
|
|
if (op1_reg != IR_REG_NONE) {
|
2023-06-14 19:23:32 +02:00
|
|
|
if (IR_REG_SPILLED(op1_reg)) {
|
|
|
|
op1_reg = IR_REG_NUM(op1_reg);
|
2022-12-15 21:27:30 +01:00
|
|
|
ir_emit_load(ctx, src_type, op1_reg, insn->op1);
|
|
|
|
}
|
2022-05-31 23:51:02 +02:00
|
|
|
if (src_type == IR_DOUBLE) {
|
2022-06-02 14:12:56 +02:00
|
|
|
| fmov Rx(def_reg), Rd(op1_reg-IR_REG_FP_FIRST)
|
2022-05-31 23:51:02 +02:00
|
|
|
} else {
|
|
|
|
IR_ASSERT(src_type == IR_FLOAT);
|
2022-06-02 14:12:56 +02:00
|
|
|
| fmov Rw(def_reg), Rs(op1_reg-IR_REG_FP_FIRST)
|
2022-05-31 23:51:02 +02:00
|
|
|
}
|
|
|
|
} else if (IR_IS_CONST_REF(insn->op1)) {
|
2022-06-02 14:12:56 +02:00
|
|
|
IR_ASSERT(0); //???
|
2022-05-31 23:51:02 +02:00
|
|
|
} else {
|
2022-09-07 22:47:30 +02:00
|
|
|
ir_reg fp;
|
|
|
|
int32_t offset = ir_ref_spill_slot(ctx, insn->op1, &fp);
|
|
|
|
|
2022-05-31 23:51:02 +02:00
|
|
|
if (src_type == IR_DOUBLE) {
|
2022-06-02 14:12:56 +02:00
|
|
|
| ldr Rx(def_reg), [Rx(fp), #offset]
|
2022-05-31 23:51:02 +02:00
|
|
|
} else {
|
|
|
|
IR_ASSERT(src_type == IR_FLOAT);
|
2022-06-02 14:12:56 +02:00
|
|
|
| ldr Rw(def_reg), [Rx(fp), #offset]
|
2022-05-31 23:51:02 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
} else if (IR_IS_TYPE_FP(dst_type)) {
|
|
|
|
IR_ASSERT(IR_IS_TYPE_INT(src_type));
|
|
|
|
if (op1_reg != IR_REG_NONE) {
|
2023-06-14 19:23:32 +02:00
|
|
|
if (IR_REG_SPILLED(op1_reg)) {
|
|
|
|
op1_reg = IR_REG_NUM(op1_reg);
|
2022-12-15 21:27:30 +01:00
|
|
|
ir_emit_load(ctx, src_type, op1_reg, insn->op1);
|
|
|
|
}
|
2022-05-31 23:51:02 +02:00
|
|
|
if (dst_type == IR_DOUBLE) {
|
2022-06-02 14:12:56 +02:00
|
|
|
| fmov Rd(def_reg-IR_REG_FP_FIRST), Rx(op1_reg)
|
2022-05-31 23:51:02 +02:00
|
|
|
} else {
|
|
|
|
IR_ASSERT(dst_type == IR_FLOAT);
|
2022-06-02 14:12:56 +02:00
|
|
|
| fmov Rs(def_reg-IR_REG_FP_FIRST), Rw(op1_reg)
|
2022-05-31 23:51:02 +02:00
|
|
|
}
|
2022-06-02 14:12:56 +02:00
|
|
|
} else if (IR_IS_CONST_REF(insn->op1)) {
|
|
|
|
IR_ASSERT(0); //???
|
2022-05-31 23:51:02 +02:00
|
|
|
} else {
|
2022-09-07 22:47:30 +02:00
|
|
|
ir_reg fp;
|
|
|
|
int32_t offset = ir_ref_spill_slot(ctx, insn->op1, &fp);
|
|
|
|
|
2022-06-02 14:12:56 +02:00
|
|
|
if (dst_type == IR_DOUBLE) {
|
|
|
|
| ldr Rd(def_reg), [Rx(fp), #offset]
|
|
|
|
} else {
|
|
|
|
IR_ASSERT(src_type == IR_FLOAT);
|
|
|
|
| ldr Rs(def_reg), [Rx(fp), #offset]
|
|
|
|
}
|
2022-05-31 23:51:02 +02:00
|
|
|
}
|
|
|
|
}
|
2023-06-14 19:23:32 +02:00
|
|
|
if (IR_REG_SPILLED(ctx->regs[def][0])) {
|
2022-05-31 23:51:02 +02:00
|
|
|
ir_emit_store(ctx, dst_type, def, def_reg);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void ir_emit_int2fp(ir_ctx *ctx, ir_ref def, ir_insn *insn)
|
|
|
|
{
|
|
|
|
ir_type dst_type = insn->type;
|
|
|
|
ir_type src_type = ctx->ir_base[insn->op1].type;
|
|
|
|
ir_backend_data *data = ctx->data;
|
|
|
|
dasm_State **Dst = &data->dasm_state;
|
|
|
|
ir_reg def_reg = IR_REG_NUM(ctx->regs[def][0]);
|
|
|
|
ir_reg op1_reg = ctx->regs[def][1];
|
|
|
|
|
|
|
|
IR_ASSERT(IR_IS_TYPE_INT(src_type));
|
|
|
|
IR_ASSERT(IR_IS_TYPE_FP(dst_type));
|
2022-06-02 14:12:56 +02:00
|
|
|
IR_ASSERT(def_reg != IR_REG_NONE && op1_reg != IR_REG_NONE);
|
2023-06-14 19:23:32 +02:00
|
|
|
if (IR_REG_SPILLED(op1_reg) || IR_IS_CONST_REF(insn->op1)) {
|
|
|
|
op1_reg = IR_REG_NUM(op1_reg);
|
2022-05-31 23:51:02 +02:00
|
|
|
ir_emit_load(ctx, src_type, op1_reg, insn->op1);
|
|
|
|
}
|
2022-06-02 14:12:56 +02:00
|
|
|
|
|
|
|
if (ir_type_size[src_type] == 8) {
|
|
|
|
if (IR_IS_TYPE_SIGNED(src_type)) {
|
2022-05-31 23:51:02 +02:00
|
|
|
if (dst_type == IR_DOUBLE) {
|
2022-06-02 14:12:56 +02:00
|
|
|
| scvtf Rd(def_reg-IR_REG_FP_FIRST), Rx(op1_reg)
|
2022-05-31 23:51:02 +02:00
|
|
|
} else {
|
|
|
|
IR_ASSERT(dst_type == IR_FLOAT);
|
2022-06-02 14:12:56 +02:00
|
|
|
| scvtf Rs(def_reg-IR_REG_FP_FIRST), Rx(op1_reg)
|
2022-05-31 23:51:02 +02:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (dst_type == IR_DOUBLE) {
|
2022-06-02 14:12:56 +02:00
|
|
|
| ucvtf Rd(def_reg-IR_REG_FP_FIRST), Rx(op1_reg)
|
2022-05-31 23:51:02 +02:00
|
|
|
} else {
|
|
|
|
IR_ASSERT(dst_type == IR_FLOAT);
|
2022-06-02 14:12:56 +02:00
|
|
|
| ucvtf Rs(def_reg-IR_REG_FP_FIRST), Rx(op1_reg)
|
2022-05-31 23:51:02 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
2022-06-02 14:12:56 +02:00
|
|
|
if (IR_IS_TYPE_SIGNED(src_type)) {
|
2022-05-31 23:51:02 +02:00
|
|
|
if (dst_type == IR_DOUBLE) {
|
2022-06-02 14:12:56 +02:00
|
|
|
| scvtf Rd(def_reg-IR_REG_FP_FIRST), Rw(op1_reg)
|
2022-05-31 23:51:02 +02:00
|
|
|
} else {
|
|
|
|
IR_ASSERT(dst_type == IR_FLOAT);
|
2022-06-02 14:12:56 +02:00
|
|
|
| scvtf Rs(def_reg-IR_REG_FP_FIRST), Rw(op1_reg)
|
2022-05-31 23:51:02 +02:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (dst_type == IR_DOUBLE) {
|
2022-06-02 14:12:56 +02:00
|
|
|
| ucvtf Rd(def_reg-IR_REG_FP_FIRST), Rw(op1_reg)
|
2022-05-31 23:51:02 +02:00
|
|
|
} else {
|
|
|
|
IR_ASSERT(dst_type == IR_FLOAT);
|
2022-06-02 14:12:56 +02:00
|
|
|
| ucvtf Rs(def_reg-IR_REG_FP_FIRST), Rw(op1_reg)
|
2022-05-31 23:51:02 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2023-06-14 19:23:32 +02:00
|
|
|
if (IR_REG_SPILLED(ctx->regs[def][0])) {
|
2022-05-31 23:51:02 +02:00
|
|
|
ir_emit_store(ctx, dst_type, def, def_reg);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void ir_emit_fp2int(ir_ctx *ctx, ir_ref def, ir_insn *insn)
|
|
|
|
{
|
|
|
|
ir_type dst_type = insn->type;
|
|
|
|
ir_type src_type = ctx->ir_base[insn->op1].type;
|
|
|
|
ir_backend_data *data = ctx->data;
|
|
|
|
dasm_State **Dst = &data->dasm_state;
|
|
|
|
ir_reg def_reg = IR_REG_NUM(ctx->regs[def][0]);
|
|
|
|
ir_reg op1_reg = ctx->regs[def][1];
|
|
|
|
|
|
|
|
IR_ASSERT(IR_IS_TYPE_FP(src_type));
|
|
|
|
IR_ASSERT(IR_IS_TYPE_INT(dst_type));
|
2022-06-02 14:12:56 +02:00
|
|
|
IR_ASSERT(def_reg != IR_REG_NONE && op1_reg != IR_REG_NONE);
|
2023-06-14 19:23:32 +02:00
|
|
|
if (IR_REG_SPILLED(op1_reg) || IR_IS_CONST_REF(insn->op1)) {
|
|
|
|
op1_reg = IR_REG_NUM(op1_reg);
|
2022-05-31 23:51:02 +02:00
|
|
|
ir_emit_load(ctx, src_type, op1_reg, insn->op1);
|
|
|
|
}
|
2022-06-02 14:12:56 +02:00
|
|
|
if (ir_type_size[dst_type] == 8) {
|
|
|
|
if (IR_IS_TYPE_SIGNED(dst_type)) {
|
2022-05-31 23:51:02 +02:00
|
|
|
if (src_type == IR_DOUBLE) {
|
2022-06-02 14:12:56 +02:00
|
|
|
| fcvtzs Rx(def_reg), Rd(op1_reg-IR_REG_FP_FIRST)
|
2022-05-31 23:51:02 +02:00
|
|
|
} else {
|
|
|
|
IR_ASSERT(src_type == IR_FLOAT);
|
2022-06-02 14:12:56 +02:00
|
|
|
| fcvtzs Rx(def_reg), Rs(op1_reg-IR_REG_FP_FIRST)
|
2022-05-31 23:51:02 +02:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (src_type == IR_DOUBLE) {
|
2022-06-02 14:12:56 +02:00
|
|
|
| fcvtzu Rx(def_reg), Rd(op1_reg-IR_REG_FP_FIRST)
|
2022-05-31 23:51:02 +02:00
|
|
|
} else {
|
|
|
|
IR_ASSERT(src_type == IR_FLOAT);
|
2022-06-02 14:12:56 +02:00
|
|
|
| fcvtzu Rx(def_reg), Rs(op1_reg-IR_REG_FP_FIRST)
|
2022-05-31 23:51:02 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
2022-06-02 14:12:56 +02:00
|
|
|
if (IR_IS_TYPE_SIGNED(dst_type)) {
|
2022-05-31 23:51:02 +02:00
|
|
|
if (src_type == IR_DOUBLE) {
|
2022-06-02 14:12:56 +02:00
|
|
|
| fcvtzs Rw(def_reg), Rd(op1_reg-IR_REG_FP_FIRST)
|
2022-05-31 23:51:02 +02:00
|
|
|
} else {
|
|
|
|
IR_ASSERT(src_type == IR_FLOAT);
|
2022-06-02 14:12:56 +02:00
|
|
|
| fcvtzs Rw(def_reg), Rs(op1_reg-IR_REG_FP_FIRST)
|
2022-05-31 23:51:02 +02:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (src_type == IR_DOUBLE) {
|
2022-06-02 14:12:56 +02:00
|
|
|
| fcvtzu Rw(def_reg), Rd(op1_reg-IR_REG_FP_FIRST)
|
2022-05-31 23:51:02 +02:00
|
|
|
} else {
|
|
|
|
IR_ASSERT(src_type == IR_FLOAT);
|
2022-06-02 14:12:56 +02:00
|
|
|
| fcvtzu Rw(def_reg), Rs(op1_reg-IR_REG_FP_FIRST)
|
2022-05-31 23:51:02 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2023-06-14 19:23:32 +02:00
|
|
|
if (IR_REG_SPILLED(ctx->regs[def][0])) {
|
2022-05-31 23:51:02 +02:00
|
|
|
ir_emit_store(ctx, dst_type, def, def_reg);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void ir_emit_fp2fp(ir_ctx *ctx, ir_ref def, ir_insn *insn)
|
|
|
|
{
|
|
|
|
ir_type dst_type = insn->type;
|
|
|
|
ir_type src_type = ctx->ir_base[insn->op1].type;
|
|
|
|
ir_backend_data *data = ctx->data;
|
|
|
|
dasm_State **Dst = &data->dasm_state;
|
|
|
|
ir_reg def_reg = IR_REG_NUM(ctx->regs[def][0]);
|
|
|
|
ir_reg op1_reg = ctx->regs[def][1];
|
|
|
|
|
|
|
|
IR_ASSERT(IR_IS_TYPE_FP(src_type));
|
|
|
|
IR_ASSERT(IR_IS_TYPE_FP(dst_type));
|
2022-06-02 14:12:56 +02:00
|
|
|
IR_ASSERT(def_reg != IR_REG_NONE && op1_reg != IR_REG_NONE);
|
2023-06-14 19:23:32 +02:00
|
|
|
if (IR_REG_SPILLED(op1_reg) || IR_IS_CONST_REF(insn->op1)) {
|
|
|
|
op1_reg = IR_REG_NUM(op1_reg);
|
2022-05-31 23:51:02 +02:00
|
|
|
ir_emit_load(ctx, src_type, op1_reg, insn->op1);
|
|
|
|
}
|
2022-06-02 14:12:56 +02:00
|
|
|
if (src_type == dst_type) {
|
|
|
|
if (op1_reg != def_reg) {
|
|
|
|
ir_emit_fp_mov(ctx, dst_type, def_reg, op1_reg);
|
2022-05-31 23:51:02 +02:00
|
|
|
}
|
2022-06-02 14:12:56 +02:00
|
|
|
} else if (src_type == IR_DOUBLE) {
|
|
|
|
| fcvt Rs(def_reg-IR_REG_FP_FIRST), Rd(op1_reg-IR_REG_FP_FIRST)
|
2022-05-31 23:51:02 +02:00
|
|
|
} else {
|
2022-06-02 14:12:56 +02:00
|
|
|
IR_ASSERT(src_type == IR_FLOAT);
|
|
|
|
| fcvt Rd(def_reg-IR_REG_FP_FIRST), Rs(op1_reg-IR_REG_FP_FIRST)
|
2022-05-31 23:51:02 +02:00
|
|
|
}
|
2023-06-14 19:23:32 +02:00
|
|
|
if (IR_REG_SPILLED(ctx->regs[def][0])) {
|
2022-05-31 23:51:02 +02:00
|
|
|
ir_emit_store(ctx, dst_type, def, def_reg);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void ir_emit_copy_int(ir_ctx *ctx, ir_ref def, ir_insn *insn)
|
|
|
|
{
|
|
|
|
ir_ref type = insn->type;
|
|
|
|
ir_reg def_reg = IR_REG_NUM(ctx->regs[def][0]);
|
|
|
|
ir_reg op1_reg = ctx->regs[def][1];
|
|
|
|
|
|
|
|
IR_ASSERT(def_reg != IR_REG_NONE || op1_reg != IR_REG_NONE);
|
2023-06-14 19:23:32 +02:00
|
|
|
if (op1_reg != IR_REG_NONE && IR_REG_SPILLED(op1_reg)) {
|
|
|
|
op1_reg = IR_REG_NUM(op1_reg);
|
2022-05-31 23:51:02 +02:00
|
|
|
ir_emit_load(ctx, type, op1_reg, insn->op1);
|
|
|
|
}
|
|
|
|
if (def_reg == op1_reg) {
|
|
|
|
/* same reg */
|
|
|
|
} else if (def_reg != IR_REG_NONE && op1_reg != IR_REG_NONE) {
|
|
|
|
ir_emit_mov(ctx, type, def_reg, op1_reg);
|
|
|
|
} else if (def_reg != IR_REG_NONE) {
|
|
|
|
ir_emit_load(ctx, type, def_reg, insn->op1);
|
|
|
|
} else if (op1_reg != IR_REG_NONE) {
|
|
|
|
ir_emit_store(ctx, type, def, op1_reg);
|
|
|
|
} else {
|
|
|
|
IR_ASSERT(0);
|
|
|
|
}
|
2023-06-14 19:23:32 +02:00
|
|
|
if (def_reg != IR_REG_NONE && IR_REG_SPILLED(ctx->regs[def][0])) {
|
2022-05-31 23:51:02 +02:00
|
|
|
ir_emit_store(ctx, type, def, def_reg);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void ir_emit_copy_fp(ir_ctx *ctx, ir_ref def, ir_insn *insn)
|
|
|
|
{
|
|
|
|
ir_type type = insn->type;
|
|
|
|
ir_reg def_reg = IR_REG_NUM(ctx->regs[def][0]);
|
|
|
|
ir_reg op1_reg = ctx->regs[def][1];
|
|
|
|
|
|
|
|
IR_ASSERT(def_reg != IR_REG_NONE || op1_reg != IR_REG_NONE);
|
2023-06-14 19:23:32 +02:00
|
|
|
if (op1_reg != IR_REG_NONE && IR_REG_SPILLED(op1_reg)) {
|
|
|
|
op1_reg = IR_REG_NUM(op1_reg);
|
2022-05-31 23:51:02 +02:00
|
|
|
ir_emit_load(ctx, type, op1_reg, insn->op1);
|
|
|
|
}
|
|
|
|
if (def_reg == op1_reg) {
|
|
|
|
/* same reg */
|
|
|
|
} else if (def_reg != IR_REG_NONE && op1_reg != IR_REG_NONE) {
|
2022-10-12 11:01:49 +02:00
|
|
|
ir_emit_fp_mov(ctx, type, def_reg, op1_reg);
|
2022-05-31 23:51:02 +02:00
|
|
|
} else if (def_reg != IR_REG_NONE) {
|
|
|
|
ir_emit_load(ctx, type, def_reg, insn->op1);
|
|
|
|
} else if (op1_reg != IR_REG_NONE) {
|
|
|
|
ir_emit_store(ctx, type, def, op1_reg);
|
|
|
|
} else {
|
|
|
|
IR_ASSERT(0);
|
|
|
|
}
|
2023-06-14 19:23:32 +02:00
|
|
|
if (def_reg != IR_REG_NONE && IR_REG_SPILLED(ctx->regs[def][0])) {
|
2022-05-31 23:51:02 +02:00
|
|
|
ir_emit_store(ctx, type, def, def_reg);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void ir_emit_vaddr(ir_ctx *ctx, ir_ref def, ir_insn *insn)
|
|
|
|
{
|
|
|
|
ir_backend_data *data = ctx->data;
|
|
|
|
dasm_State **Dst = &data->dasm_state;
|
|
|
|
ir_ref type = insn->type;
|
|
|
|
ir_reg def_reg = IR_REG_NUM(ctx->regs[def][0]);
|
|
|
|
int32_t offset;
|
|
|
|
ir_reg fp;
|
|
|
|
|
|
|
|
IR_ASSERT(def_reg != IR_REG_NONE);
|
2023-05-18 20:00:57 +02:00
|
|
|
offset = ir_var_spill_slot(ctx, insn->op1, &fp);
|
2022-06-02 23:38:49 +02:00
|
|
|
| add Rx(def_reg), Rx(fp), #offset
|
2023-06-14 19:23:32 +02:00
|
|
|
if (IR_REG_SPILLED(ctx->regs[def][0])) {
|
2022-05-31 23:51:02 +02:00
|
|
|
ir_emit_store(ctx, type, def, def_reg);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-06-06 17:10:41 +02:00
|
|
|
static void ir_emit_vload(ir_ctx *ctx, ir_ref def, ir_insn *insn)
|
2022-05-31 23:51:02 +02:00
|
|
|
{
|
2023-05-18 20:00:57 +02:00
|
|
|
ir_insn *var_insn = &ctx->ir_base[insn->op2];
|
2022-05-31 23:51:02 +02:00
|
|
|
ir_ref type = insn->type;
|
|
|
|
ir_reg def_reg = IR_REG_NUM(ctx->regs[def][0]);
|
2023-05-18 20:00:57 +02:00
|
|
|
ir_reg fp;
|
|
|
|
int32_t offset;
|
2022-05-31 23:51:02 +02:00
|
|
|
|
2023-05-18 20:00:57 +02:00
|
|
|
IR_ASSERT(var_insn->op == IR_VAR);
|
|
|
|
fp = (ctx->flags & IR_USE_FRAME_POINTER) ? IR_REG_FRAME_POINTER : IR_REG_STACK_POINTER;
|
|
|
|
offset = IR_SPILL_POS_TO_OFFSET(var_insn->op3);
|
|
|
|
if (def_reg == IR_REG_NONE && ir_is_same_mem_var(ctx, def, var_insn->op3)) {
|
2022-05-31 23:51:02 +02:00
|
|
|
return; // fake load
|
|
|
|
}
|
|
|
|
IR_ASSERT(def_reg != IR_REG_NONE);
|
2023-05-18 20:00:57 +02:00
|
|
|
if (IR_IS_TYPE_INT(type)) {
|
|
|
|
ir_emit_load_mem_int(ctx, type, def_reg, fp, offset);
|
|
|
|
} else {
|
|
|
|
ir_emit_load_mem_fp(ctx, type, def_reg, fp, offset);
|
|
|
|
}
|
2023-06-14 19:23:32 +02:00
|
|
|
if (IR_REG_SPILLED(ctx->regs[def][0])) {
|
2022-05-31 23:51:02 +02:00
|
|
|
ir_emit_store(ctx, type, def, def_reg);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-06-06 17:10:41 +02:00
|
|
|
static void ir_emit_vstore(ir_ctx *ctx, ir_ref ref, ir_insn *insn)
|
2022-05-31 23:51:02 +02:00
|
|
|
{
|
2023-05-18 20:00:57 +02:00
|
|
|
ir_insn *var_insn = &ctx->ir_base[insn->op2];
|
2022-05-31 23:51:02 +02:00
|
|
|
ir_insn *val_insn = &ctx->ir_base[insn->op3];
|
|
|
|
ir_ref type = val_insn->type;
|
|
|
|
ir_reg op3_reg = ctx->regs[ref][3];
|
2023-05-18 20:00:57 +02:00
|
|
|
ir_reg fp;
|
|
|
|
int32_t offset;
|
2022-05-31 23:51:02 +02:00
|
|
|
|
2023-05-18 20:00:57 +02:00
|
|
|
IR_ASSERT(var_insn->op == IR_VAR);
|
|
|
|
fp = (ctx->flags & IR_USE_FRAME_POINTER) ? IR_REG_FRAME_POINTER : IR_REG_STACK_POINTER;
|
|
|
|
offset = IR_SPILL_POS_TO_OFFSET(var_insn->op3);
|
2022-06-02 23:38:49 +02:00
|
|
|
IR_ASSERT(op3_reg != IR_REG_NONE);
|
2023-06-14 19:23:32 +02:00
|
|
|
if (IR_REG_SPILLED(op3_reg) && ir_is_same_mem_var(ctx, insn->op3, var_insn->op3)) {
|
2022-05-31 23:51:02 +02:00
|
|
|
return; // fake store
|
|
|
|
}
|
2023-06-14 19:23:32 +02:00
|
|
|
if (IR_REG_SPILLED(op3_reg) || IR_IS_CONST_REF(insn->op3)) {
|
|
|
|
op3_reg = IR_REG_NUM(op3_reg);
|
2022-06-02 23:38:49 +02:00
|
|
|
ir_emit_load(ctx, type, op3_reg, insn->op3);
|
|
|
|
}
|
2023-05-18 20:00:57 +02:00
|
|
|
if (IR_IS_TYPE_INT(type)) {
|
|
|
|
ir_emit_store_mem_int(ctx, type, fp, offset, op3_reg);
|
|
|
|
} else {
|
|
|
|
ir_emit_store_mem_fp(ctx, type, fp, offset, op3_reg);
|
|
|
|
}
|
2022-05-31 23:51:02 +02:00
|
|
|
}
|
|
|
|
|
2023-12-26 15:59:38 +01:00
|
|
|
static int32_t ir_fuse_addr(ir_ctx *ctx, ir_ref root, ir_ref ref, ir_reg *preg1, ir_reg *preg2)
|
2023-04-05 18:20:43 +02:00
|
|
|
{
|
|
|
|
ir_insn *addr_insn = &ctx->ir_base[ref];
|
|
|
|
ir_reg reg;
|
|
|
|
|
|
|
|
IR_ASSERT(addr_insn->op == IR_ADD);
|
|
|
|
IR_ASSERT(!IR_IS_CONST_REF(addr_insn->op1) && IR_IS_CONST_REF(addr_insn->op2));
|
2023-12-27 08:18:56 +01:00
|
|
|
IR_ASSERT(!IR_IS_SYM_CONST(ctx->ir_base[addr_insn->op2].op));
|
2023-12-26 15:59:38 +01:00
|
|
|
if (UNEXPECTED(ctx->rules[ref] & IR_FUSED_REG)) {
|
|
|
|
reg = ir_get_fused_reg(ctx, root, ref, 1);
|
|
|
|
} else {
|
|
|
|
reg = ctx->regs[ref][1];
|
|
|
|
}
|
2023-06-14 19:23:32 +02:00
|
|
|
if (IR_REG_SPILLED(reg)) {
|
|
|
|
reg = IR_REG_NUM(reg);
|
2023-04-05 18:20:43 +02:00
|
|
|
ir_emit_load(ctx, IR_ADDR, reg, addr_insn->op1);
|
|
|
|
}
|
|
|
|
*preg1 = reg;
|
|
|
|
*preg2 = IR_REG_NONE; // TODO: ???
|
|
|
|
return ctx->ir_base[addr_insn->op2].val.i32;
|
|
|
|
}
|
|
|
|
|
2022-05-31 23:51:02 +02:00
|
|
|
static void ir_emit_load_int(ir_ctx *ctx, ir_ref def, ir_insn *insn)
|
|
|
|
{
|
2022-06-28 11:24:50 +02:00
|
|
|
ir_backend_data *data = ctx->data;
|
|
|
|
dasm_State **Dst = &data->dasm_state;
|
2022-05-31 23:51:02 +02:00
|
|
|
ir_ref type = insn->type;
|
|
|
|
ir_reg op2_reg = ctx->regs[def][2];
|
|
|
|
ir_reg def_reg = IR_REG_NUM(ctx->regs[def][0]);
|
|
|
|
|
2022-08-23 11:35:10 +02:00
|
|
|
if (ctx->use_lists[def].count == 1) {
|
|
|
|
/* dead load */
|
|
|
|
return;
|
|
|
|
}
|
2022-05-31 23:51:02 +02:00
|
|
|
IR_ASSERT(def_reg != IR_REG_NONE);
|
2023-04-05 18:20:43 +02:00
|
|
|
if (!IR_IS_CONST_REF(insn->op2) && (ir_rule(ctx, insn->op2) & IR_FUSED)) {
|
|
|
|
ir_reg op1_reg;
|
2023-12-26 15:59:38 +01:00
|
|
|
int32_t offset = ir_fuse_addr(ctx, def, insn->op2, &op1_reg, &op2_reg);
|
2022-06-28 11:24:50 +02:00
|
|
|
|
2023-04-05 18:20:43 +02:00
|
|
|
if (op2_reg == IR_REG_NONE) {
|
2023-06-21 22:20:58 +02:00
|
|
|
if (IR_REG_SPILLED(ctx->regs[def][0]) && ir_is_same_spill_slot(ctx, def, op1_reg, offset)) {
|
2023-09-12 15:12:12 +02:00
|
|
|
if (!ir_may_avoid_spill_load(ctx, def, def)) {
|
|
|
|
ir_emit_load_mem_int(ctx, type, def_reg, op1_reg, offset);
|
|
|
|
}
|
2023-06-21 22:20:58 +02:00
|
|
|
/* avoid load to the same location (valid only when register is not reused) */
|
|
|
|
return;
|
2023-06-07 13:43:16 +02:00
|
|
|
}
|
2023-04-05 18:20:43 +02:00
|
|
|
ir_emit_load_mem_int(ctx, type, def_reg, op1_reg, offset);
|
|
|
|
} else {
|
|
|
|
switch (ir_type_size[type]) {
|
2023-07-27 10:16:00 +02:00
|
|
|
default:
|
|
|
|
IR_ASSERT(0);
|
2023-04-05 18:20:43 +02:00
|
|
|
case 8:
|
|
|
|
| ldr Rx(def_reg), [Rx(op1_reg), Rx(op2_reg)]
|
|
|
|
break;
|
|
|
|
case 4:
|
|
|
|
| ldr Rw(def_reg), [Rx(op1_reg), Rx(op2_reg)]
|
|
|
|
break;
|
|
|
|
case 2:
|
|
|
|
if (IR_IS_TYPE_SIGNED(type)) {
|
|
|
|
| ldrsh Rw(def_reg), [Rx(op1_reg), Rx(op2_reg)]
|
|
|
|
} else {
|
|
|
|
| ldrh Rw(def_reg), [Rx(op1_reg), Rx(op2_reg)]
|
2022-10-12 11:09:52 +02:00
|
|
|
}
|
2023-04-05 18:20:43 +02:00
|
|
|
break;
|
|
|
|
case 1:
|
|
|
|
if (IR_IS_TYPE_SIGNED(type)) {
|
|
|
|
| ldrsb Rw(def_reg), [Rx(op1_reg), Rx(op2_reg)]
|
|
|
|
} else {
|
|
|
|
| ldrb Rw(def_reg), [Rx(op1_reg), Rx(op2_reg)]
|
|
|
|
}
|
|
|
|
break;
|
2022-06-28 11:24:50 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
2023-04-05 18:20:43 +02:00
|
|
|
if (op2_reg == IR_REG_NONE) {
|
|
|
|
op2_reg = def_reg;
|
|
|
|
}
|
2023-06-14 19:23:32 +02:00
|
|
|
if (IR_REG_SPILLED(op2_reg) || IR_IS_CONST_REF(insn->op2)) {
|
|
|
|
op2_reg = IR_REG_NUM(op2_reg);
|
2023-04-05 18:20:43 +02:00
|
|
|
IR_ASSERT(ctx->ir_base[insn->op2].type == IR_ADDR);
|
|
|
|
ir_emit_load(ctx, IR_ADDR, op2_reg, insn->op2);
|
|
|
|
}
|
2022-06-28 11:24:50 +02:00
|
|
|
ir_emit_load_mem_int(ctx, type, def_reg, op2_reg, 0);
|
|
|
|
}
|
2023-06-14 19:23:32 +02:00
|
|
|
if (IR_REG_SPILLED(ctx->regs[def][0])) {
|
2022-05-31 23:51:02 +02:00
|
|
|
ir_emit_store(ctx, type, def, def_reg);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void ir_emit_load_fp(ir_ctx *ctx, ir_ref def, ir_insn *insn)
|
|
|
|
{
|
2022-09-01 18:19:01 +02:00
|
|
|
ir_backend_data *data = ctx->data;
|
|
|
|
dasm_State **Dst = &data->dasm_state;
|
2022-05-31 23:51:02 +02:00
|
|
|
ir_ref type = insn->type;
|
|
|
|
ir_reg op2_reg = ctx->regs[def][2];
|
|
|
|
ir_reg def_reg = IR_REG_NUM(ctx->regs[def][0]);
|
|
|
|
|
2022-08-23 11:35:10 +02:00
|
|
|
if (ctx->use_lists[def].count == 1) {
|
|
|
|
/* dead load */
|
|
|
|
return;
|
|
|
|
}
|
2022-06-14 21:38:35 +02:00
|
|
|
IR_ASSERT(def_reg != IR_REG_NONE);
|
2023-04-05 18:20:43 +02:00
|
|
|
if (!IR_IS_CONST_REF(insn->op2) && (ir_rule(ctx, insn->op2) & IR_FUSED)) {
|
|
|
|
ir_reg op1_reg;
|
2023-12-26 15:59:38 +01:00
|
|
|
int32_t offset = ir_fuse_addr(ctx, def, insn->op2, &op1_reg, &op2_reg);
|
2022-09-01 18:19:01 +02:00
|
|
|
|
2023-04-05 18:20:43 +02:00
|
|
|
if (op2_reg == IR_REG_NONE) {
|
2023-06-21 22:20:58 +02:00
|
|
|
if (IR_REG_SPILLED(ctx->regs[def][0]) && ir_is_same_spill_slot(ctx, def, op1_reg, offset)) {
|
2023-09-12 15:12:12 +02:00
|
|
|
if (!ir_may_avoid_spill_load(ctx, def, def)) {
|
|
|
|
ir_emit_load_mem_fp(ctx, type, def_reg, op1_reg, offset);
|
|
|
|
}
|
2023-06-21 22:20:58 +02:00
|
|
|
/* avoid load to the same location (valid only when register is not reused) */
|
|
|
|
return;
|
2023-06-07 13:43:16 +02:00
|
|
|
}
|
2023-04-05 18:20:43 +02:00
|
|
|
ir_emit_load_mem_fp(ctx, type, def_reg, op1_reg, offset);
|
|
|
|
} else {
|
|
|
|
if (type == IR_DOUBLE) {
|
|
|
|
| ldr Rd(def_reg-IR_REG_FP_FIRST), [Rx(op1_reg), Rx(op2_reg)]
|
2022-09-01 18:19:01 +02:00
|
|
|
} else {
|
2023-04-05 18:20:43 +02:00
|
|
|
IR_ASSERT(type == IR_FLOAT);
|
|
|
|
| ldr Rs(def_reg-IR_REG_FP_FIRST), [Rx(op1_reg), Rx(op2_reg)]
|
2022-09-01 18:19:01 +02:00
|
|
|
}
|
|
|
|
}
|
2022-06-28 11:24:50 +02:00
|
|
|
} else {
|
2023-12-27 10:51:56 +01:00
|
|
|
if (op2_reg != IR_REG_NONE && (IR_REG_SPILLED(op2_reg) || IR_IS_CONST_REF(insn->op2))) {
|
2023-06-14 19:23:32 +02:00
|
|
|
op2_reg = IR_REG_NUM(op2_reg);
|
2023-12-27 10:51:56 +01:00
|
|
|
ir_emit_load(ctx, IR_ADDR, op2_reg, insn->op2);
|
2023-04-05 18:20:43 +02:00
|
|
|
}
|
|
|
|
if (op2_reg == IR_REG_NONE) {
|
|
|
|
op2_reg = def_reg;
|
|
|
|
IR_ASSERT(ctx->ir_base[insn->op2].type == IR_ADDR);
|
|
|
|
ir_emit_load(ctx, IR_ADDR, op2_reg, insn->op2);
|
|
|
|
}
|
2022-06-28 11:24:50 +02:00
|
|
|
ir_emit_load_mem_fp(ctx, type, def_reg, op2_reg, 0);
|
|
|
|
}
|
2023-06-14 19:23:32 +02:00
|
|
|
if (IR_REG_SPILLED(ctx->regs[def][0])) {
|
2022-05-31 23:51:02 +02:00
|
|
|
ir_emit_store(ctx, type, def, def_reg);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-11-17 21:30:35 +01:00
|
|
|
static void ir_emit_store_int(ir_ctx *ctx, ir_ref ref, ir_insn *insn)
|
2022-05-31 23:51:02 +02:00
|
|
|
{
|
2022-06-28 11:24:50 +02:00
|
|
|
ir_backend_data *data = ctx->data;
|
|
|
|
dasm_State **Dst = &data->dasm_state;
|
2022-05-31 23:51:02 +02:00
|
|
|
ir_insn *val_insn = &ctx->ir_base[insn->op3];
|
|
|
|
ir_ref type = val_insn->type;
|
|
|
|
ir_reg op2_reg = ctx->regs[ref][2];
|
|
|
|
ir_reg op3_reg = ctx->regs[ref][3];
|
|
|
|
|
2023-04-05 18:20:43 +02:00
|
|
|
if (!IR_IS_CONST_REF(insn->op2) && (ir_rule(ctx, insn->op2) & IR_FUSED)) {
|
|
|
|
ir_reg op1_reg;
|
2023-12-26 15:59:38 +01:00
|
|
|
int32_t offset = ir_fuse_addr(ctx, ref, insn->op2, &op1_reg, &op2_reg);
|
2022-06-28 11:24:50 +02:00
|
|
|
|
2023-04-05 18:20:43 +02:00
|
|
|
if (op2_reg == IR_REG_NONE) {
|
2023-06-21 22:20:58 +02:00
|
|
|
if (!IR_IS_CONST_REF(insn->op3) && IR_REG_SPILLED(op3_reg) && ir_is_same_spill_slot(ctx, insn->op3, op1_reg, offset)) {
|
2023-09-12 15:12:12 +02:00
|
|
|
if (!ir_may_avoid_spill_load(ctx, insn->op3, ref)) {
|
|
|
|
op3_reg = IR_REG_NUM(op3_reg);
|
|
|
|
ir_emit_load(ctx, type, op3_reg, insn->op3);
|
|
|
|
}
|
2023-06-21 22:20:58 +02:00
|
|
|
/* avoid store to the same location */
|
|
|
|
return;
|
2023-06-07 13:43:16 +02:00
|
|
|
}
|
|
|
|
if (op3_reg == IR_REG_NONE) {
|
2023-12-27 08:18:56 +01:00
|
|
|
IR_ASSERT(IR_IS_CONST_REF(insn->op3) && !IR_IS_SYM_CONST(ctx->ir_base[insn->op3].op) && ctx->ir_base[insn->op3].val.i64 == 0);
|
2023-06-07 13:43:16 +02:00
|
|
|
op3_reg = IR_REG_ZR;
|
2023-06-14 19:23:32 +02:00
|
|
|
} else if (IR_REG_SPILLED(op3_reg) || IR_IS_CONST_REF(insn->op3)) {
|
|
|
|
op3_reg = IR_REG_NUM(op3_reg);
|
2023-06-07 13:43:16 +02:00
|
|
|
ir_emit_load(ctx, type, op3_reg, insn->op3);
|
|
|
|
}
|
2023-04-05 18:20:43 +02:00
|
|
|
ir_emit_store_mem_int(ctx, type, op1_reg, offset, op3_reg);
|
|
|
|
} else {
|
2023-06-07 13:43:16 +02:00
|
|
|
if (op3_reg == IR_REG_NONE) {
|
2023-12-27 08:18:56 +01:00
|
|
|
IR_ASSERT(IR_IS_CONST_REF(insn->op3) && !IR_IS_SYM_CONST(ctx->ir_base[insn->op3].op) && ctx->ir_base[insn->op3].val.i64 == 0);
|
2023-06-07 13:43:16 +02:00
|
|
|
op3_reg = IR_REG_ZR;
|
2023-06-14 19:23:32 +02:00
|
|
|
} else if (IR_REG_SPILLED(op3_reg) || IR_IS_CONST_REF(insn->op3)) {
|
|
|
|
op3_reg = IR_REG_NUM(op3_reg);
|
2023-06-07 13:43:16 +02:00
|
|
|
ir_emit_load(ctx, type, op3_reg, insn->op3);
|
|
|
|
}
|
2023-04-05 18:20:43 +02:00
|
|
|
switch (ir_type_size[type]) {
|
2023-07-27 10:16:00 +02:00
|
|
|
default:
|
|
|
|
IR_ASSERT(0);
|
2023-04-05 18:20:43 +02:00
|
|
|
case 8:
|
|
|
|
| str Rx(op3_reg), [Rx(op1_reg), Rx(op2_reg)]
|
|
|
|
break;
|
|
|
|
case 4:
|
|
|
|
| str Rw(op3_reg), [Rx(op1_reg), Rx(op2_reg)]
|
|
|
|
break;
|
|
|
|
case 2:
|
|
|
|
| strh Rw(op3_reg), [Rx(op1_reg), Rx(op2_reg)]
|
|
|
|
break;
|
|
|
|
case 1:
|
|
|
|
| strb Rw(op3_reg), [Rx(op1_reg), Rx(op2_reg)]
|
|
|
|
break;
|
2022-06-28 11:24:50 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
IR_ASSERT(op2_reg != IR_REG_NONE);
|
2023-06-14 19:23:32 +02:00
|
|
|
if (IR_REG_SPILLED(op2_reg) || IR_IS_CONST_REF(insn->op2)) {
|
|
|
|
op2_reg = IR_REG_NUM(op2_reg);
|
2022-06-28 11:24:50 +02:00
|
|
|
IR_ASSERT(ctx->ir_base[insn->op2].type == IR_ADDR);
|
|
|
|
ir_emit_load(ctx, IR_ADDR, op2_reg, insn->op2);
|
|
|
|
}
|
2023-06-07 13:43:16 +02:00
|
|
|
if (op3_reg == IR_REG_NONE) {
|
2023-12-27 08:18:56 +01:00
|
|
|
IR_ASSERT(IR_IS_CONST_REF(insn->op3) && !IR_IS_SYM_CONST(ctx->ir_base[insn->op3].op) && ctx->ir_base[insn->op3].val.i64 == 0);
|
2023-06-07 13:43:16 +02:00
|
|
|
op3_reg = IR_REG_ZR;
|
2023-06-14 19:23:32 +02:00
|
|
|
} else if (IR_REG_SPILLED(op3_reg) || IR_IS_CONST_REF(insn->op3)) {
|
|
|
|
op3_reg = IR_REG_NUM(op3_reg);
|
2023-06-07 13:43:16 +02:00
|
|
|
ir_emit_load(ctx, type, op3_reg, insn->op3);
|
|
|
|
}
|
2022-06-28 11:24:50 +02:00
|
|
|
ir_emit_store_mem_int(ctx, type, op2_reg, 0, op3_reg);
|
|
|
|
}
|
2022-05-31 23:51:02 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static void ir_emit_store_fp(ir_ctx *ctx, ir_ref ref, ir_insn *insn)
|
|
|
|
{
|
2022-09-01 18:19:01 +02:00
|
|
|
ir_backend_data *data = ctx->data;
|
|
|
|
dasm_State **Dst = &data->dasm_state;
|
2022-05-31 23:51:02 +02:00
|
|
|
ir_ref type = ctx->ir_base[insn->op3].type;
|
|
|
|
ir_reg op2_reg = ctx->regs[ref][2];
|
|
|
|
ir_reg op3_reg = ctx->regs[ref][3];
|
|
|
|
|
2023-04-05 18:20:43 +02:00
|
|
|
IR_ASSERT(op3_reg != IR_REG_NONE);
|
|
|
|
if (!IR_IS_CONST_REF(insn->op2) && (ir_rule(ctx, insn->op2) & IR_FUSED)) {
|
|
|
|
ir_reg op1_reg;
|
2023-12-26 15:59:38 +01:00
|
|
|
int32_t offset = ir_fuse_addr(ctx, ref, insn->op2, &op1_reg, &op2_reg);
|
2022-09-01 18:19:01 +02:00
|
|
|
|
2023-04-05 18:20:43 +02:00
|
|
|
if (op2_reg == IR_REG_NONE) {
|
2023-06-21 22:20:58 +02:00
|
|
|
if (!IR_IS_CONST_REF(insn->op3) && IR_REG_SPILLED(op3_reg) && ir_is_same_spill_slot(ctx, insn->op3, op1_reg, offset)) {
|
2023-09-12 15:12:12 +02:00
|
|
|
if (!ir_may_avoid_spill_load(ctx, insn->op3, ref)) {
|
|
|
|
op3_reg = IR_REG_NUM(op3_reg);
|
|
|
|
ir_emit_load(ctx, type, op3_reg, insn->op3);
|
|
|
|
}
|
2023-06-21 22:20:58 +02:00
|
|
|
/* avoid store to the same location */
|
|
|
|
return;
|
2023-06-07 13:43:16 +02:00
|
|
|
}
|
2023-06-14 19:23:32 +02:00
|
|
|
if (IR_REG_SPILLED(op3_reg) || IR_IS_CONST_REF(insn->op3)) {
|
|
|
|
op3_reg = IR_REG_NUM(op3_reg);
|
2023-06-07 13:43:16 +02:00
|
|
|
ir_emit_load(ctx, type, op3_reg, insn->op3);
|
|
|
|
}
|
2023-04-05 18:20:43 +02:00
|
|
|
ir_emit_store_mem_fp(ctx, type, op1_reg, offset, op3_reg);
|
|
|
|
} else {
|
2023-06-14 19:23:32 +02:00
|
|
|
if (IR_REG_SPILLED(op3_reg) || IR_IS_CONST_REF(insn->op3)) {
|
|
|
|
op3_reg = IR_REG_NUM(op3_reg);
|
2023-06-07 13:43:16 +02:00
|
|
|
ir_emit_load(ctx, type, op3_reg, insn->op3);
|
|
|
|
}
|
2023-04-05 18:20:43 +02:00
|
|
|
if (type == IR_DOUBLE) {
|
|
|
|
| str Rd(op3_reg-IR_REG_FP_FIRST), [Rx(op1_reg), Rx(op2_reg)]
|
2022-09-01 18:19:01 +02:00
|
|
|
} else {
|
2023-04-05 18:20:43 +02:00
|
|
|
IR_ASSERT(type == IR_FLOAT);
|
|
|
|
| str Rs(op3_reg-IR_REG_FP_FIRST), [Rx(op1_reg), Rx(op2_reg)]
|
2022-09-01 18:19:01 +02:00
|
|
|
}
|
|
|
|
}
|
2022-06-28 11:24:50 +02:00
|
|
|
} else {
|
2023-06-14 19:23:32 +02:00
|
|
|
if (IR_REG_SPILLED(op2_reg) || IR_IS_CONST_REF(insn->op2)) {
|
|
|
|
op2_reg = IR_REG_NUM(op2_reg);
|
2023-04-05 18:20:43 +02:00
|
|
|
IR_ASSERT(ctx->ir_base[insn->op2].type == IR_ADDR);
|
|
|
|
ir_emit_load(ctx, IR_ADDR, op2_reg, insn->op2);
|
|
|
|
}
|
2023-06-14 19:23:32 +02:00
|
|
|
if (IR_REG_SPILLED(op3_reg) || IR_IS_CONST_REF(insn->op3)) {
|
|
|
|
op3_reg = IR_REG_NUM(op3_reg);
|
2023-06-07 13:43:16 +02:00
|
|
|
ir_emit_load(ctx, type, op3_reg, insn->op3);
|
|
|
|
}
|
2022-06-28 11:24:50 +02:00
|
|
|
ir_emit_store_mem_fp(ctx, type, op2_reg, 0, op3_reg);
|
|
|
|
}
|
2022-05-31 23:51:02 +02:00
|
|
|
}
|
|
|
|
|
2022-12-06 22:02:02 +01:00
|
|
|
static void ir_emit_rload(ir_ctx *ctx, ir_ref def, ir_insn *insn)
|
|
|
|
{
|
2022-12-27 20:34:52 +01:00
|
|
|
ir_reg src_reg = insn->op2;
|
|
|
|
ir_type type = insn->type;
|
2022-12-06 22:02:02 +01:00
|
|
|
|
2023-06-09 09:58:58 +02:00
|
|
|
if (IR_REGSET_IN(IR_REGSET_UNION((ir_regset)ctx->fixed_regset, IR_REGSET_FIXED), src_reg)) {
|
2022-12-27 20:34:52 +01:00
|
|
|
if (ctx->vregs[def]
|
|
|
|
&& ctx->live_intervals[ctx->vregs[def]]
|
2023-04-28 08:49:12 +02:00
|
|
|
&& ctx->live_intervals[ctx->vregs[def]]->stack_spill_pos != -1) {
|
2022-12-27 20:34:52 +01:00
|
|
|
ir_emit_store(ctx, type, def, src_reg);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
ir_reg def_reg = IR_REG_NUM(ctx->regs[def][0]);
|
|
|
|
|
|
|
|
if (def_reg == IR_REG_NONE) {
|
2023-02-21 13:41:41 +01:00
|
|
|
/* op3 is used as a flag that the value is already stored in memory.
|
|
|
|
* If op3 is set we don't have to store the value once again (in case of spilling)
|
|
|
|
*/
|
2023-06-21 22:20:58 +02:00
|
|
|
if (!insn->op3 || !ir_is_same_spill_slot(ctx, def, ctx->spill_base, insn->op3)) {
|
2023-01-30 14:33:57 +01:00
|
|
|
ir_emit_store(ctx, type, def, src_reg);
|
|
|
|
}
|
2022-12-27 20:34:52 +01:00
|
|
|
} else {
|
|
|
|
if (src_reg != def_reg) {
|
|
|
|
if (IR_IS_TYPE_INT(type)) {
|
|
|
|
ir_emit_mov(ctx, type, def_reg, src_reg);
|
|
|
|
} else {
|
|
|
|
IR_ASSERT(IR_IS_TYPE_FP(type));
|
|
|
|
ir_emit_fp_mov(ctx, type, def_reg, src_reg);
|
|
|
|
}
|
|
|
|
}
|
2023-06-16 01:14:02 +02:00
|
|
|
if (IR_REG_SPILLED(ctx->regs[def][0])
|
2023-06-21 22:20:58 +02:00
|
|
|
&& (!insn->op3 || !ir_is_same_spill_slot(ctx, def, ctx->spill_base, insn->op3))) {
|
2022-12-27 20:34:52 +01:00
|
|
|
ir_emit_store(ctx, type, def, def_reg);
|
|
|
|
}
|
|
|
|
}
|
2022-12-06 22:02:02 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-06-09 23:16:29 +02:00
|
|
|
static void ir_emit_rstore(ir_ctx *ctx, ir_ref ref, ir_insn *insn)
|
|
|
|
{
|
|
|
|
ir_ref type = ctx->ir_base[insn->op2].type;
|
|
|
|
ir_reg op2_reg = ctx->regs[ref][2];
|
|
|
|
ir_reg dst_reg = insn->op3;
|
|
|
|
|
|
|
|
if (op2_reg != IR_REG_NONE) {
|
2023-06-14 19:23:32 +02:00
|
|
|
if (IR_REG_SPILLED(op2_reg)) {
|
|
|
|
op2_reg = IR_REG_NUM(op2_reg);
|
2022-06-09 23:16:29 +02:00
|
|
|
ir_emit_load(ctx, type, op2_reg, insn->op2);
|
|
|
|
}
|
2022-06-15 13:37:16 +02:00
|
|
|
if (op2_reg != dst_reg) {
|
|
|
|
if (IR_IS_TYPE_INT(type)) {
|
|
|
|
ir_emit_mov(ctx, type, dst_reg, op2_reg);
|
|
|
|
} else {
|
|
|
|
IR_ASSERT(IR_IS_TYPE_FP(type));
|
|
|
|
ir_emit_fp_mov(ctx, type, dst_reg, op2_reg);
|
|
|
|
}
|
2022-06-09 23:16:29 +02:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
ir_emit_load(ctx, type, dst_reg, insn->op2);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-05-31 23:51:02 +02:00
|
|
|
static void ir_emit_alloca(ir_ctx *ctx, ir_ref def, ir_insn *insn)
|
|
|
|
{
|
|
|
|
ir_backend_data *data = ctx->data;
|
|
|
|
dasm_State **Dst = &data->dasm_state;
|
|
|
|
ir_reg def_reg = IR_REG_NUM(ctx->regs[def][0]);
|
|
|
|
|
|
|
|
if (IR_IS_CONST_REF(insn->op2)) {
|
|
|
|
ir_insn *val = &ctx->ir_base[insn->op2];
|
|
|
|
int32_t size = val->val.i32;
|
|
|
|
|
|
|
|
IR_ASSERT(IR_IS_TYPE_INT(val->type));
|
2023-12-27 08:18:56 +01:00
|
|
|
IR_ASSERT(!IR_IS_SYM_CONST(val->op));
|
2022-05-31 23:51:02 +02:00
|
|
|
IR_ASSERT(IR_IS_TYPE_UNSIGNED(val->type) || val->val.i64 > 0);
|
|
|
|
|
2023-11-17 12:24:33 +01:00
|
|
|
if (ctx->flags2 & IR_HAS_CALLS) {
|
2022-05-31 23:51:02 +02:00
|
|
|
/* Stack must be 16 byte aligned */
|
|
|
|
size = IR_ALIGNED_SIZE(size, 16);
|
|
|
|
} else {
|
|
|
|
size = IR_ALIGNED_SIZE(size, 8);
|
|
|
|
}
|
2023-12-27 08:18:56 +01:00
|
|
|
if (aarch64_may_encode_imm12(size)) {
|
|
|
|
| sub sp, sp, #size
|
|
|
|
} else {
|
|
|
|
ir_emit_load_imm_int(ctx, IR_ADDR, IR_REG_INT_TMP, size);
|
|
|
|
| sub sp, sp, Rx(IR_REG_INT_TMP)
|
|
|
|
}
|
2023-01-23 14:05:06 +01:00
|
|
|
if (!(ctx->flags & IR_USE_FRAME_POINTER)) {
|
2023-06-15 18:28:54 +02:00
|
|
|
ctx->call_stack_size += size;
|
2022-12-07 11:09:00 +01:00
|
|
|
}
|
2022-05-31 23:51:02 +02:00
|
|
|
} else {
|
2023-11-17 12:24:33 +01:00
|
|
|
int32_t alignment = (ctx->flags2 & IR_HAS_CALLS) ? 16 : 8;
|
2022-05-31 23:51:02 +02:00
|
|
|
ir_reg op2_reg = ctx->regs[def][2];
|
|
|
|
ir_type type = ctx->ir_base[insn->op2].type;
|
|
|
|
|
2022-12-07 11:09:00 +01:00
|
|
|
IR_ASSERT(ctx->flags & IR_FUNCTION);
|
2023-11-15 23:59:26 +01:00
|
|
|
IR_ASSERT(ctx->flags & IR_USE_FRAME_POINTER);
|
2022-06-02 23:38:49 +02:00
|
|
|
IR_ASSERT(def_reg != IR_REG_NONE && op2_reg != IR_REG_NONE);
|
2023-06-14 19:23:32 +02:00
|
|
|
if (IR_REG_SPILLED(op2_reg)) {
|
|
|
|
op2_reg = IR_REG_NUM(op2_reg);
|
2022-05-31 23:51:02 +02:00
|
|
|
ir_emit_load(ctx, type, op2_reg, insn->op2);
|
|
|
|
}
|
2022-06-02 23:38:49 +02:00
|
|
|
| add Rx(def_reg), Rx(op2_reg), #(alignment-1)
|
|
|
|
| and Rx(def_reg), Rx(def_reg), #(~(alignment-1))
|
|
|
|
| sub sp, sp, Rx(def_reg);
|
2022-05-31 23:51:02 +02:00
|
|
|
}
|
|
|
|
if (def_reg != IR_REG_NONE) {
|
2022-06-02 23:38:49 +02:00
|
|
|
| mov Rx(def_reg), sp
|
2023-06-14 19:23:32 +02:00
|
|
|
if (IR_REG_SPILLED(ctx->regs[def][0])) {
|
2022-05-31 23:51:02 +02:00
|
|
|
ir_emit_store(ctx, insn->type, def, def_reg);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
ir_emit_store(ctx, IR_ADDR, def, IR_REG_STACK_POINTER);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-12-07 11:09:00 +01:00
|
|
|
static void ir_emit_afree(ir_ctx *ctx, ir_ref def, ir_insn *insn)
|
|
|
|
{
|
|
|
|
ir_backend_data *data = ctx->data;
|
|
|
|
dasm_State **Dst = &data->dasm_state;
|
|
|
|
|
|
|
|
if (IR_IS_CONST_REF(insn->op2)) {
|
|
|
|
ir_insn *val = &ctx->ir_base[insn->op2];
|
|
|
|
int32_t size = val->val.i32;
|
|
|
|
|
|
|
|
IR_ASSERT(IR_IS_TYPE_INT(val->type));
|
2023-12-27 08:18:56 +01:00
|
|
|
IR_ASSERT(!IR_IS_SYM_CONST(val->op));
|
2022-12-07 11:09:00 +01:00
|
|
|
IR_ASSERT(IR_IS_TYPE_UNSIGNED(val->type) || val->val.i64 > 0);
|
|
|
|
|
2023-11-17 12:24:33 +01:00
|
|
|
if (ctx->flags2 & IR_HAS_CALLS) {
|
2022-12-07 11:09:00 +01:00
|
|
|
/* Stack must be 16 byte aligned */
|
|
|
|
size = IR_ALIGNED_SIZE(size, 16);
|
|
|
|
} else {
|
|
|
|
size = IR_ALIGNED_SIZE(size, 8);
|
|
|
|
}
|
|
|
|
| add sp, sp, #size
|
2023-01-23 14:05:06 +01:00
|
|
|
if (!(ctx->flags & IR_USE_FRAME_POINTER)) {
|
2023-06-15 18:28:54 +02:00
|
|
|
ctx->call_stack_size -= size;
|
2022-12-07 11:09:00 +01:00
|
|
|
}
|
|
|
|
} else {
|
2023-11-17 12:24:33 +01:00
|
|
|
// int32_t alignment = (ctx->flags2 & IR_HAS_CALLS) ? 16 : 8;
|
2022-12-07 11:09:00 +01:00
|
|
|
ir_reg op2_reg = ctx->regs[def][2];
|
|
|
|
ir_type type = ctx->ir_base[insn->op2].type;
|
|
|
|
|
|
|
|
IR_ASSERT(ctx->flags & IR_FUNCTION);
|
|
|
|
IR_ASSERT(op2_reg != IR_REG_NONE);
|
2023-06-14 19:23:32 +02:00
|
|
|
if (IR_REG_SPILLED(op2_reg)) {
|
|
|
|
op2_reg = IR_REG_NUM(op2_reg);
|
2022-12-07 11:09:00 +01:00
|
|
|
ir_emit_load(ctx, type, op2_reg, insn->op2);
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: alignment
|
|
|
|
|
|
|
|
| add sp, sp, Rx(op2_reg);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-11-15 23:59:26 +01:00
|
|
|
static void ir_emit_frame_addr(ir_ctx *ctx, ir_ref def)
|
|
|
|
{
|
|
|
|
ir_backend_data *data = ctx->data;
|
|
|
|
dasm_State **Dst = &data->dasm_state;
|
|
|
|
ir_reg def_reg = IR_REG_NUM(ctx->regs[def][0]);
|
|
|
|
|
|
|
|
if (ctx->flags & IR_USE_FRAME_POINTER) {
|
|
|
|
| mov Rx(def_reg), Rx(IR_REG_X29)
|
|
|
|
} else {
|
|
|
|
| add Rx(def_reg), Rx(IR_REG_X31), #(ctx->stack_frame_size + ctx->call_stack_size)
|
|
|
|
}
|
|
|
|
if (IR_REG_SPILLED(ctx->regs[def][0])) {
|
|
|
|
ir_emit_store(ctx, IR_ADDR, def, def_reg);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-11-17 12:24:33 +01:00
|
|
|
static void ir_emit_va_start(ir_ctx *ctx, ir_ref def, ir_insn *insn)
|
|
|
|
{
|
2023-11-23 20:52:08 +01:00
|
|
|
#ifdef __APPLE__
|
|
|
|
ir_backend_data *data = ctx->data;
|
|
|
|
dasm_State **Dst = &data->dasm_state;
|
|
|
|
ir_reg fp;
|
|
|
|
int arg_area_offset;
|
|
|
|
ir_reg op2_reg = ctx->regs[def][2];
|
|
|
|
ir_reg tmp_reg = ctx->regs[def][3];
|
|
|
|
|
|
|
|
IR_ASSERT(tmp_reg != IR_REG_NONE);
|
|
|
|
if (op2_reg != IR_REG_NONE && IR_REG_SPILLED(op2_reg)) {
|
|
|
|
op2_reg = IR_REG_NUM(op2_reg);
|
|
|
|
ir_emit_load(ctx, IR_ADDR, op2_reg, insn->op2);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ctx->flags & IR_USE_FRAME_POINTER) {
|
|
|
|
fp = IR_REG_FRAME_POINTER;
|
|
|
|
arg_area_offset = ctx->stack_frame_size + sizeof(void*) * 2 + ctx->param_stack_size;
|
|
|
|
} else {
|
|
|
|
fp = IR_REG_STACK_POINTER;
|
|
|
|
arg_area_offset = ctx->call_stack_size + ctx->stack_frame_size + ctx->param_stack_size;
|
|
|
|
}
|
|
|
|
| add Rx(tmp_reg), Rx(fp), #arg_area_offset
|
|
|
|
if (op2_reg != IR_REG_NONE) {
|
|
|
|
| str Rx(tmp_reg), [Rx(op2_reg)]
|
|
|
|
} else {
|
|
|
|
int32_t offset = ir_ref_spill_slot(ctx, insn->op2, &op2_reg);
|
|
|
|
|
|
|
|
| str Rx(tmp_reg), [Rx(op2_reg), #offset]
|
|
|
|
}
|
|
|
|
#else
|
2023-11-23 17:38:33 +01:00
|
|
|
ir_backend_data *data = ctx->data;
|
|
|
|
dasm_State **Dst = &data->dasm_state;
|
|
|
|
ir_reg fp;
|
|
|
|
int reg_save_area_offset;
|
|
|
|
int overflow_arg_area_offset;
|
|
|
|
ir_reg op2_reg = ctx->regs[def][2];
|
|
|
|
ir_reg tmp_reg = ctx->regs[def][3];
|
|
|
|
|
|
|
|
IR_ASSERT(op2_reg != IR_REG_NONE && tmp_reg != IR_REG_NONE);
|
|
|
|
if (IR_REG_SPILLED(op2_reg)) {
|
|
|
|
op2_reg = IR_REG_NUM(op2_reg);
|
|
|
|
ir_emit_load(ctx, IR_ADDR, op2_reg, insn->op2);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ctx->flags & IR_USE_FRAME_POINTER) {
|
|
|
|
fp = IR_REG_FRAME_POINTER;
|
|
|
|
reg_save_area_offset = ctx->locals_area_size + sizeof(void*) * 2;
|
2023-11-23 20:52:08 +01:00
|
|
|
overflow_arg_area_offset = ctx->stack_frame_size + sizeof(void*) * 2 + ctx->param_stack_size;
|
2023-11-23 17:38:33 +01:00
|
|
|
} else {
|
|
|
|
fp = IR_REG_STACK_POINTER;
|
2023-11-23 20:52:08 +01:00
|
|
|
reg_save_area_offset = ctx->locals_area_size + ctx->call_stack_size;
|
|
|
|
overflow_arg_area_offset = ctx->call_stack_size + ctx->stack_frame_size + ctx->param_stack_size;
|
2023-11-23 17:38:33 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Set va_list.stack */
|
|
|
|
| add Rx(tmp_reg), Rx(fp), #overflow_arg_area_offset
|
|
|
|
| str Rx(tmp_reg), [Rx(op2_reg), #offsetof(ir_va_list, stack)]
|
|
|
|
if ((ctx->flags2 & (IR_HAS_VA_ARG_GP|IR_HAS_VA_COPY)) && ctx->gp_reg_params < IR_REG_INT_ARGS) {
|
|
|
|
reg_save_area_offset += sizeof(void*) * IR_REG_INT_ARGS;
|
|
|
|
/* Set va_list.gr_top */
|
|
|
|
if (overflow_arg_area_offset != reg_save_area_offset) {
|
|
|
|
| add Rx(tmp_reg), Rx(fp), #reg_save_area_offset
|
|
|
|
}
|
|
|
|
| str Rx(tmp_reg), [Rx(op2_reg), #offsetof(ir_va_list, gr_top)]
|
|
|
|
/* Set va_list.gr_offset */
|
|
|
|
| movn Rw(tmp_reg), #~(0 - (sizeof(void*) * (IR_REG_INT_ARGS - ctx->gp_reg_params)))
|
|
|
|
| str Rw(tmp_reg), [Rx(op2_reg), #offsetof(ir_va_list, gr_offset)]
|
|
|
|
} else {
|
|
|
|
/* Set va_list.gr_offset */
|
|
|
|
| str wzr, [Rx(op2_reg), #offsetof(ir_va_list, gr_offset)]
|
|
|
|
}
|
|
|
|
if ((ctx->flags2 & (IR_HAS_VA_ARG_FP|IR_HAS_VA_COPY)) && ctx->fp_reg_params < IR_REG_FP_ARGS) {
|
|
|
|
reg_save_area_offset += 16 * IR_REG_FP_ARGS;
|
|
|
|
/* Set va_list.vr_top */
|
|
|
|
if (overflow_arg_area_offset != reg_save_area_offset) {
|
|
|
|
| add Rx(tmp_reg), Rx(fp), #reg_save_area_offset
|
|
|
|
}
|
|
|
|
| str Rx(tmp_reg), [Rx(op2_reg), #offsetof(ir_va_list, vr_top)]
|
|
|
|
/* Set va_list.vr_offset */
|
|
|
|
| movn Rw(tmp_reg), #~(0 - (16 * (IR_REG_FP_ARGS - ctx->fp_reg_params)))
|
|
|
|
| str Rw(tmp_reg), [Rx(op2_reg), #offsetof(ir_va_list, vr_offset)]
|
|
|
|
} else {
|
|
|
|
/* Set va_list.vr_offset */
|
|
|
|
| str wzr, [Rx(op2_reg), #offsetof(ir_va_list, vr_offset)]
|
|
|
|
}
|
2023-11-23 20:52:08 +01:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
static void ir_emit_va_copy(ir_ctx *ctx, ir_ref def, ir_insn *insn)
|
|
|
|
{
|
|
|
|
IR_ASSERT(0 && "NIY va_copy");
|
|
|
|
}
|
|
|
|
|
|
|
|
static void ir_emit_va_arg(ir_ctx *ctx, ir_ref def, ir_insn *insn)
|
|
|
|
{
|
|
|
|
#ifdef __APPLE__
|
2023-11-23 17:38:33 +01:00
|
|
|
ir_backend_data *data = ctx->data;
|
|
|
|
dasm_State **Dst = &data->dasm_state;
|
2023-11-23 20:52:08 +01:00
|
|
|
ir_type type = insn->type;
|
|
|
|
ir_reg def_reg = ctx->regs[def][0];
|
2023-11-23 17:38:33 +01:00
|
|
|
ir_reg op2_reg = ctx->regs[def][2];
|
|
|
|
ir_reg tmp_reg = ctx->regs[def][3];
|
|
|
|
|
2023-11-23 20:52:08 +01:00
|
|
|
IR_ASSERT(def_reg != IR_REG_NONE && tmp_reg != IR_REG_NONE);
|
2023-11-23 17:38:33 +01:00
|
|
|
if (op2_reg != IR_REG_NONE && IR_REG_SPILLED(op2_reg)) {
|
|
|
|
op2_reg = IR_REG_NUM(op2_reg);
|
|
|
|
ir_emit_load(ctx, IR_ADDR, op2_reg, insn->op2);
|
|
|
|
}
|
2023-11-23 20:52:08 +01:00
|
|
|
| ldr Rx(tmp_reg), [Rx(op2_reg)]
|
|
|
|
if (IR_IS_TYPE_INT(type)) {
|
|
|
|
ir_emit_load_mem_int(ctx, type, def_reg, tmp_reg, 0);
|
2023-11-23 17:38:33 +01:00
|
|
|
} else {
|
2023-11-23 20:52:08 +01:00
|
|
|
ir_emit_load_mem_fp(ctx, type, def_reg, tmp_reg, 0);
|
2023-11-23 17:38:33 +01:00
|
|
|
}
|
2023-11-23 20:52:08 +01:00
|
|
|
| add Rx(tmp_reg), Rx(tmp_reg), #IR_MAX(ir_type_size[type], sizeof(void*))
|
2023-11-23 17:38:33 +01:00
|
|
|
if (op2_reg != IR_REG_NONE) {
|
|
|
|
| str Rx(tmp_reg), [Rx(op2_reg)]
|
|
|
|
} else {
|
|
|
|
int32_t offset = ir_ref_spill_slot(ctx, insn->op2, &op2_reg);
|
|
|
|
|
|
|
|
| str Rx(tmp_reg), [Rx(op2_reg), #offset]
|
2023-11-23 20:52:08 +01:00
|
|
|
}
|
|
|
|
if (IR_REG_SPILLED(ctx->regs[def][0])) {
|
|
|
|
ir_emit_store(ctx, type, def, def_reg);
|
|
|
|
}
|
|
|
|
#else
|
2023-11-23 17:38:33 +01:00
|
|
|
ir_backend_data *data = ctx->data;
|
|
|
|
dasm_State **Dst = &data->dasm_state;
|
|
|
|
ir_type type = insn->type;
|
|
|
|
ir_reg def_reg = ctx->regs[def][0];
|
|
|
|
ir_reg op2_reg = ctx->regs[def][2];
|
|
|
|
ir_reg tmp_reg = ctx->regs[def][3];
|
|
|
|
|
|
|
|
IR_ASSERT(def_reg != IR_REG_NONE && op2_reg != IR_REG_NONE && tmp_reg != IR_REG_NONE);
|
|
|
|
if (IR_REG_SPILLED(op2_reg)) {
|
|
|
|
op2_reg = IR_REG_NUM(op2_reg);
|
|
|
|
ir_emit_load(ctx, IR_ADDR, op2_reg, insn->op2);
|
|
|
|
}
|
|
|
|
if (IR_IS_TYPE_INT(type)) {
|
|
|
|
| ldr Rw(tmp_reg), [Rx(op2_reg), #offsetof(ir_va_list, gr_offset)]
|
|
|
|
| cmp Rw(tmp_reg), wzr
|
|
|
|
| bge >1
|
|
|
|
| ldr Rx(IR_REG_INT_TMP), [Rx(op2_reg), #offsetof(ir_va_list, gr_top)]
|
|
|
|
| sxtw Rx(tmp_reg), Rw(tmp_reg)
|
|
|
|
| add Rx(IR_REG_INT_TMP), Rx(tmp_reg), Rx(IR_REG_INT_TMP)
|
|
|
|
| ldr Rx(def_reg), [Rx(IR_REG_INT_TMP)]
|
|
|
|
| add Rw(tmp_reg), Rw(tmp_reg), #sizeof(void*)
|
|
|
|
| str Rw(tmp_reg), [Rx(op2_reg), #offsetof(ir_va_list, gr_offset)]
|
|
|
|
| b >2
|
|
|
|
|1:
|
|
|
|
| ldr Rx(tmp_reg), [Rx(op2_reg), #offsetof(ir_va_list, stack)]
|
|
|
|
| ldr Rx(def_reg), [Rx(tmp_reg)]
|
|
|
|
| add Rx(tmp_reg), Rx(tmp_reg), #sizeof(void*)
|
|
|
|
| str Rx(tmp_reg), [Rx(op2_reg), #offsetof(ir_va_list, stack)]
|
|
|
|
|2:
|
|
|
|
} else {
|
|
|
|
| ldr Rw(tmp_reg), [Rx(op2_reg), #offsetof(ir_va_list, vr_offset)]
|
|
|
|
| cmp Rw(tmp_reg), wzr
|
|
|
|
| bge >1
|
|
|
|
| ldr Rx(IR_REG_INT_TMP), [Rx(op2_reg), #offsetof(ir_va_list, vr_top)]
|
|
|
|
| sxtw Rx(tmp_reg), Rw(tmp_reg)
|
|
|
|
| add Rx(IR_REG_INT_TMP), Rx(tmp_reg), Rx(IR_REG_INT_TMP)
|
|
|
|
| ldr Rd(def_reg-IR_REG_FP_FIRST), [Rx(IR_REG_INT_TMP)]
|
|
|
|
| add Rw(tmp_reg), Rw(tmp_reg), #16
|
|
|
|
| str Rw(tmp_reg), [Rx(op2_reg), #offsetof(ir_va_list, vr_offset)]
|
|
|
|
| b >2
|
|
|
|
|1:
|
|
|
|
| ldr Rx(tmp_reg), [Rx(op2_reg), #offsetof(ir_va_list, stack)]
|
|
|
|
| ldr Rd(def_reg-IR_REG_FP_FIRST), [Rx(tmp_reg)]
|
|
|
|
| add Rx(tmp_reg), Rx(tmp_reg), #sizeof(void*)
|
|
|
|
| str Rx(tmp_reg), [Rx(op2_reg), #offsetof(ir_va_list, stack)]
|
|
|
|
|2:
|
|
|
|
}
|
|
|
|
if (IR_REG_SPILLED(ctx->regs[def][0])) {
|
|
|
|
ir_emit_store(ctx, type, def, def_reg);
|
|
|
|
}
|
|
|
|
#endif
|
2023-11-17 12:24:33 +01:00
|
|
|
}
|
|
|
|
|
2022-11-08 21:09:35 +01:00
|
|
|
static void ir_emit_switch(ir_ctx *ctx, uint32_t b, ir_ref def, ir_insn *insn)
|
2022-05-31 23:51:02 +02:00
|
|
|
{
|
|
|
|
ir_backend_data *data = ctx->data;
|
|
|
|
dasm_State **Dst = &data->dasm_state;
|
|
|
|
ir_type type;
|
|
|
|
ir_block *bb;
|
|
|
|
ir_insn *use_insn, *val;
|
|
|
|
uint32_t n, *p, use_block;
|
|
|
|
int i;
|
|
|
|
int label, default_label = 0;
|
|
|
|
int count = 0;
|
|
|
|
ir_val min, max;
|
2022-06-06 14:27:25 +02:00
|
|
|
ir_reg op1_reg, op2_reg, tmp_reg;
|
2022-05-31 23:51:02 +02:00
|
|
|
|
|
|
|
type = ctx->ir_base[insn->op2].type;
|
|
|
|
if (IR_IS_TYPE_SIGNED(type)) {
|
|
|
|
min.u64 = 0x7fffffffffffffff;
|
|
|
|
max.u64 = 0x8000000000000000;
|
|
|
|
} else {
|
|
|
|
min.u64 = 0xffffffffffffffff;
|
|
|
|
max.u64 = 0x0;
|
|
|
|
}
|
|
|
|
|
|
|
|
bb = &ctx->cfg_blocks[b];
|
|
|
|
p = &ctx->cfg_edges[bb->successors];
|
|
|
|
for (n = bb->successors_count; n != 0; p++, n--) {
|
|
|
|
use_block = *p;
|
|
|
|
use_insn = &ctx->ir_base[ctx->cfg_blocks[use_block].start];
|
|
|
|
if (use_insn->op == IR_CASE_VAL) {
|
|
|
|
val = &ctx->ir_base[use_insn->op2];
|
2023-12-27 08:18:56 +01:00
|
|
|
IR_ASSERT(!IR_IS_SYM_CONST(val->op));
|
2022-05-31 23:51:02 +02:00
|
|
|
if (IR_IS_TYPE_SIGNED(type)) {
|
|
|
|
IR_ASSERT(IR_IS_TYPE_SIGNED(val->type));
|
|
|
|
min.i64 = IR_MIN(min.i64, val->val.i64);
|
|
|
|
max.i64 = IR_MAX(max.i64, val->val.i64);
|
|
|
|
} else {
|
|
|
|
IR_ASSERT(!IR_IS_TYPE_SIGNED(val->type));
|
|
|
|
min.u64 = (int64_t)IR_MIN(min.u64, val->val.u64);
|
|
|
|
max.u64 = (int64_t)IR_MAX(max.u64, val->val.u64);
|
|
|
|
}
|
|
|
|
count++;
|
|
|
|
} else {
|
2023-07-27 10:16:00 +02:00
|
|
|
IR_ASSERT(use_insn->op == IR_CASE_DEFAULT);
|
|
|
|
default_label = ir_skip_empty_target_blocks(ctx, use_block);
|
2022-05-31 23:51:02 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-06-06 14:27:25 +02:00
|
|
|
op1_reg = ctx->regs[def][1];
|
2022-05-31 23:51:02 +02:00
|
|
|
op2_reg = ctx->regs[def][2];
|
|
|
|
tmp_reg = ctx->regs[def][3];
|
|
|
|
|
2022-06-06 14:27:25 +02:00
|
|
|
IR_ASSERT(op2_reg != IR_REG_NONE && tmp_reg != IR_REG_NONE);
|
2023-06-14 19:23:32 +02:00
|
|
|
if (IR_REG_SPILLED(op2_reg)) {
|
|
|
|
op2_reg = IR_REG_NUM(op2_reg);
|
2022-05-31 23:51:02 +02:00
|
|
|
ir_emit_load(ctx, type, op2_reg, insn->op2);
|
|
|
|
} else if (IR_IS_CONST_REF(insn->op2)) {
|
|
|
|
ir_emit_load(ctx, type, op2_reg, insn->op2);
|
|
|
|
}
|
|
|
|
|
2023-10-03 07:34:02 +02:00
|
|
|
/* Generate a table jmp or a sequence of calls */
|
2022-05-31 23:51:02 +02:00
|
|
|
if ((max.i64-min.i64) < count * 8) {
|
|
|
|
int *labels = ir_mem_malloc(sizeof(int) * (max.i64 - min.i64 + 1));
|
|
|
|
|
|
|
|
for (i = 0; i <= (max.i64 - min.i64); i++) {
|
|
|
|
labels[i] = default_label;
|
|
|
|
}
|
|
|
|
p = &ctx->cfg_edges[bb->successors];
|
|
|
|
for (n = bb->successors_count; n != 0; p++, n--) {
|
|
|
|
use_block = *p;
|
|
|
|
use_insn = &ctx->ir_base[ctx->cfg_blocks[use_block].start];
|
|
|
|
if (use_insn->op == IR_CASE_VAL) {
|
|
|
|
val = &ctx->ir_base[use_insn->op2];
|
2023-12-27 08:18:56 +01:00
|
|
|
IR_ASSERT(!IR_IS_SYM_CONST(val->op));
|
2022-08-30 22:15:20 +02:00
|
|
|
label = ir_skip_empty_target_blocks(ctx, use_block);
|
2022-05-31 23:51:02 +02:00
|
|
|
labels[val->val.i64 - min.i64] = label;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-06-06 14:27:25 +02:00
|
|
|
if (aarch64_may_encode_imm12(max.i64)) {
|
|
|
|
| ASM_REG_IMM_OP cmp, type, op2_reg, max.i64
|
2022-05-31 23:51:02 +02:00
|
|
|
} else {
|
2022-06-06 14:27:25 +02:00
|
|
|
ir_emit_load_imm_int(ctx, type, tmp_reg, max.i64);
|
|
|
|
| ASM_REG_REG_OP cmp, type, op2_reg, tmp_reg
|
|
|
|
}
|
2022-05-31 23:51:02 +02:00
|
|
|
if (IR_IS_TYPE_SIGNED(type)) {
|
2022-06-06 14:27:25 +02:00
|
|
|
| bgt =>default_label
|
2022-05-31 23:51:02 +02:00
|
|
|
} else {
|
2022-06-06 14:27:25 +02:00
|
|
|
| bhi =>default_label
|
2022-05-31 23:51:02 +02:00
|
|
|
}
|
2022-06-06 14:27:25 +02:00
|
|
|
|
|
|
|
if (op1_reg == IR_REG_NONE) {
|
|
|
|
op1_reg = op2_reg;
|
2022-05-31 23:51:02 +02:00
|
|
|
}
|
2022-06-06 14:27:25 +02:00
|
|
|
if (aarch64_may_encode_imm12(min.i64)) {
|
|
|
|
| ASM_REG_REG_IMM_OP subs, type, op1_reg, op2_reg, min.i64
|
|
|
|
} else {
|
|
|
|
ir_emit_load_imm_int(ctx, type, tmp_reg, min.i64);
|
|
|
|
| ASM_REG_REG_REG_OP subs, type, op1_reg, op2_reg, tmp_reg
|
|
|
|
}
|
|
|
|
if (IR_IS_TYPE_SIGNED(type)) {
|
|
|
|
| blt =>default_label
|
|
|
|
} else {
|
|
|
|
| blo =>default_label
|
|
|
|
}
|
|
|
|
| adr Rx(tmp_reg), >1
|
|
|
|
| ldr Rx(tmp_reg), [Rx(tmp_reg), Rx(op1_reg), lsl #3]
|
|
|
|
| br Rx(tmp_reg)
|
2022-05-31 23:51:02 +02:00
|
|
|
|.jmp_table
|
|
|
|
if (!data->jmp_table_label) {
|
|
|
|
data->jmp_table_label = ctx->cfg_blocks_count + ctx->consts_count + 3;
|
|
|
|
|=>data->jmp_table_label:
|
|
|
|
}
|
|
|
|
|.align 8
|
|
|
|
|1:
|
|
|
|
for (i = 0; i <= (max.i64 - min.i64); i++) {
|
2023-02-16 20:41:55 +01:00
|
|
|
int b = labels[i];
|
|
|
|
ir_block *bb = &ctx->cfg_blocks[b];
|
|
|
|
ir_insn *insn = &ctx->ir_base[bb->end];
|
|
|
|
|
|
|
|
if (insn->op == IR_IJMP && IR_IS_CONST_REF(insn->op2)) {
|
|
|
|
ir_ref prev = ctx->prev_ref[bb->end];
|
|
|
|
if (prev != bb->start && ctx->ir_base[prev].op == IR_SNAPSHOT) {
|
|
|
|
prev = ctx->prev_ref[prev];
|
|
|
|
}
|
|
|
|
if (prev == bb->start) {
|
|
|
|
void *addr = ir_jmp_addr(ctx, insn, &ctx->ir_base[insn->op2]);
|
|
|
|
|
|
|
|
| .addr &addr
|
2023-05-26 08:08:57 +02:00
|
|
|
if (ctx->ir_base[bb->start].op != IR_CASE_DEFAULT) {
|
|
|
|
bb->flags |= IR_BB_EMPTY;
|
|
|
|
}
|
2023-02-16 20:41:55 +01:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
| .addr =>b
|
2022-05-31 23:51:02 +02:00
|
|
|
}
|
|
|
|
|.code
|
|
|
|
ir_mem_free(labels);
|
|
|
|
} else {
|
|
|
|
p = &ctx->cfg_edges[bb->successors];
|
|
|
|
for (n = bb->successors_count; n != 0; p++, n--) {
|
|
|
|
use_block = *p;
|
|
|
|
use_insn = &ctx->ir_base[ctx->cfg_blocks[use_block].start];
|
|
|
|
if (use_insn->op == IR_CASE_VAL) {
|
|
|
|
val = &ctx->ir_base[use_insn->op2];
|
2023-12-27 08:18:56 +01:00
|
|
|
IR_ASSERT(!IR_IS_SYM_CONST(val->op));
|
2022-08-30 22:15:20 +02:00
|
|
|
label = ir_skip_empty_target_blocks(ctx, use_block);
|
2022-06-06 14:27:25 +02:00
|
|
|
if (aarch64_may_encode_imm12(val->val.i64)) {
|
|
|
|
| ASM_REG_IMM_OP cmp, type, op2_reg, val->val.i64
|
|
|
|
} else {
|
|
|
|
ir_emit_load_imm_int(ctx, type, tmp_reg, val->val.i64);
|
|
|
|
| ASM_REG_REG_OP cmp, type, op2_reg, tmp_reg
|
|
|
|
|
|
|
|
}
|
|
|
|
| beq =>label
|
2022-05-31 23:51:02 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if (default_label) {
|
2022-06-06 14:27:25 +02:00
|
|
|
| b =>default_label
|
2022-05-31 23:51:02 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static int ir_parallel_copy(ir_ctx *ctx, ir_copy *copies, int count, ir_reg tmp_reg, ir_reg tmp_fp_reg)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
int8_t *pred, *loc, *types;
|
2023-06-22 11:07:19 +02:00
|
|
|
ir_reg to, from_reg, c;
|
2022-05-31 23:51:02 +02:00
|
|
|
ir_type type;
|
|
|
|
ir_regset todo, ready;
|
2023-06-22 11:07:19 +02:00
|
|
|
ir_reg last_reg = IR_REG_NONE, last_fp_reg = IR_REG_NONE;
|
2022-05-31 23:51:02 +02:00
|
|
|
|
|
|
|
loc = ir_mem_malloc(IR_REG_NUM * 3 * sizeof(int8_t));
|
|
|
|
pred = loc + IR_REG_NUM;
|
|
|
|
types = pred + IR_REG_NUM;
|
|
|
|
memset(loc, IR_REG_NONE, IR_REG_NUM * 2 * sizeof(int8_t));
|
|
|
|
todo = IR_REGSET_EMPTY;
|
|
|
|
ready = IR_REGSET_EMPTY;
|
|
|
|
|
|
|
|
for (i = 0; i < count; i++) {
|
|
|
|
from_reg = copies[i].from;
|
|
|
|
to = copies[i].to;
|
|
|
|
if (from_reg != to) {
|
|
|
|
loc[from_reg] = from_reg;
|
|
|
|
pred[to] = from_reg;
|
2023-05-17 21:37:45 +02:00
|
|
|
types[from_reg] = copies[i].type;
|
2023-06-22 11:07:19 +02:00
|
|
|
if (to == tmp_reg) {
|
|
|
|
IR_ASSERT(last_reg == IR_REG_NONE);
|
|
|
|
last_reg = to;
|
|
|
|
} else if (to == tmp_fp_reg) {
|
|
|
|
IR_ASSERT(last_fp_reg == IR_REG_NONE);
|
|
|
|
last_fp_reg = to;
|
|
|
|
} else {
|
|
|
|
IR_ASSERT(!IR_REGSET_IN(todo, to));
|
|
|
|
IR_REGSET_INCL(todo, to);
|
|
|
|
}
|
2022-05-31 23:51:02 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
IR_REGSET_FOREACH(todo, i) {
|
|
|
|
if (loc[i] == IR_REG_NONE) {
|
|
|
|
IR_REGSET_INCL(ready, i);
|
|
|
|
}
|
|
|
|
} IR_REGSET_FOREACH_END();
|
|
|
|
|
2023-04-26 09:56:55 +02:00
|
|
|
while (1) {
|
2022-05-31 23:51:02 +02:00
|
|
|
while (ready != IR_REGSET_EMPTY) {
|
2023-04-26 09:56:55 +02:00
|
|
|
to = ir_regset_pop_first(&ready);
|
2022-05-31 23:51:02 +02:00
|
|
|
from_reg = pred[to];
|
|
|
|
c = loc[from_reg];
|
2023-05-17 21:37:45 +02:00
|
|
|
type = types[from_reg];
|
2022-05-31 23:51:02 +02:00
|
|
|
if (IR_IS_TYPE_INT(type)) {
|
|
|
|
ir_emit_mov(ctx, type, to, c);
|
|
|
|
} else {
|
|
|
|
ir_emit_fp_mov(ctx, type, to, c);
|
|
|
|
}
|
2023-04-26 09:56:55 +02:00
|
|
|
IR_REGSET_EXCL(todo, to);
|
2022-05-31 23:51:02 +02:00
|
|
|
loc[from_reg] = to;
|
|
|
|
if (from_reg == c && pred[from_reg] != IR_REG_NONE) {
|
|
|
|
IR_REGSET_INCL(ready, from_reg);
|
|
|
|
}
|
|
|
|
}
|
2023-04-26 09:56:55 +02:00
|
|
|
|
|
|
|
if (todo == IR_REGSET_EMPTY) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
to = ir_regset_pop_first(&todo);
|
2022-05-31 23:51:02 +02:00
|
|
|
from_reg = pred[to];
|
2023-04-26 09:56:55 +02:00
|
|
|
IR_ASSERT(to != loc[from_reg]);
|
2023-05-17 21:37:45 +02:00
|
|
|
type = types[from_reg];
|
2023-04-26 09:56:55 +02:00
|
|
|
if (IR_IS_TYPE_INT(type)) {
|
|
|
|
IR_ASSERT(tmp_reg != IR_REG_NONE);
|
|
|
|
IR_ASSERT(tmp_reg >= IR_REG_GP_FIRST && tmp_reg <= IR_REG_GP_LAST);
|
|
|
|
ir_emit_mov(ctx, type, tmp_reg, to);
|
|
|
|
loc[to] = tmp_reg;
|
|
|
|
} else {
|
|
|
|
IR_ASSERT(tmp_fp_reg != IR_REG_NONE);
|
|
|
|
IR_ASSERT(tmp_fp_reg >= IR_REG_FP_FIRST && tmp_fp_reg <= IR_REG_FP_LAST);
|
|
|
|
ir_emit_fp_mov(ctx, type, tmp_fp_reg, to);
|
|
|
|
loc[to] = tmp_fp_reg;
|
2022-05-31 23:51:02 +02:00
|
|
|
}
|
2023-04-26 09:56:55 +02:00
|
|
|
IR_REGSET_INCL(ready, to);
|
2022-05-31 23:51:02 +02:00
|
|
|
}
|
|
|
|
|
2023-06-22 11:07:19 +02:00
|
|
|
if (last_reg != IR_REG_NONE) {
|
|
|
|
to = last_reg;
|
|
|
|
from_reg = pred[to];
|
|
|
|
c = loc[from_reg];
|
|
|
|
if (to != c) {
|
|
|
|
type = types[from_reg];
|
|
|
|
IR_ASSERT(IR_IS_TYPE_INT(type));
|
|
|
|
ir_emit_mov(ctx, type, to, c);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (last_fp_reg != IR_REG_NONE) {
|
|
|
|
to = last_fp_reg;
|
|
|
|
from_reg = pred[to];
|
|
|
|
c = loc[from_reg];
|
|
|
|
if (to != c) {
|
|
|
|
type = types[from_reg];
|
|
|
|
IR_ASSERT(!IR_IS_TYPE_INT(type));
|
|
|
|
ir_emit_fp_mov(ctx, type, to, c);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-05-31 23:51:02 +02:00
|
|
|
ir_mem_free(loc);
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2022-08-29 23:42:06 +02:00
|
|
|
static int32_t ir_call_used_stack(ir_ctx *ctx, ir_insn *insn)
|
2022-05-31 23:51:02 +02:00
|
|
|
{
|
|
|
|
int j, n;
|
|
|
|
ir_type type;
|
|
|
|
int int_param = 0;
|
|
|
|
int fp_param = 0;
|
|
|
|
int int_reg_params_count = IR_REG_INT_ARGS;
|
|
|
|
int fp_reg_params_count = IR_REG_FP_ARGS;
|
|
|
|
int32_t used_stack = 0;
|
2023-11-30 21:50:53 +01:00
|
|
|
#ifdef __APPLE__
|
|
|
|
const ir_proto_t *proto = ir_call_proto(ctx, insn);
|
|
|
|
int last_named_input = (proto && (proto->flags & IR_VARARG_FUNC)) ? proto->params_count + 2 : insn->inputs_count;
|
|
|
|
#endif
|
2022-05-31 23:51:02 +02:00
|
|
|
|
2023-07-06 00:15:08 +02:00
|
|
|
n = insn->inputs_count;
|
2022-05-31 23:51:02 +02:00
|
|
|
for (j = 3; j <= n; j++) {
|
2022-11-08 16:17:29 +01:00
|
|
|
type = ctx->ir_base[ir_insn_op(insn, j)].type;
|
2023-11-30 21:50:53 +01:00
|
|
|
#ifdef __APPLE__
|
|
|
|
if (j > last_named_input) {
|
|
|
|
used_stack += IR_MAX(sizeof(void*), ir_type_size[type]);
|
|
|
|
} else
|
|
|
|
#endif
|
2022-05-31 23:51:02 +02:00
|
|
|
if (IR_IS_TYPE_INT(type)) {
|
|
|
|
if (int_param >= int_reg_params_count) {
|
|
|
|
used_stack += IR_MAX(sizeof(void*), ir_type_size[type]);
|
|
|
|
}
|
|
|
|
int_param++;
|
2023-07-27 10:16:00 +02:00
|
|
|
} else {
|
|
|
|
IR_ASSERT(IR_IS_TYPE_FP(type));
|
2022-05-31 23:51:02 +02:00
|
|
|
if (fp_param >= fp_reg_params_count) {
|
|
|
|
used_stack += IR_MAX(sizeof(void*), ir_type_size[type]);
|
|
|
|
}
|
|
|
|
fp_param++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return used_stack;
|
|
|
|
}
|
|
|
|
|
2023-04-26 13:10:58 +02:00
|
|
|
static int32_t ir_emit_arguments(ir_ctx *ctx, ir_ref def, ir_insn *insn, ir_reg tmp_reg)
|
2022-05-31 23:51:02 +02:00
|
|
|
{
|
|
|
|
ir_backend_data *data = ctx->data;
|
|
|
|
dasm_State **Dst = &data->dasm_state;
|
|
|
|
int j, n;
|
|
|
|
ir_ref arg;
|
|
|
|
ir_insn *arg_insn;
|
|
|
|
uint8_t type;
|
|
|
|
ir_reg src_reg, dst_reg;
|
|
|
|
int int_param = 0;
|
|
|
|
int fp_param = 0;
|
|
|
|
int count = 0;
|
|
|
|
int int_reg_params_count = IR_REG_INT_ARGS;
|
|
|
|
int fp_reg_params_count = IR_REG_FP_ARGS;
|
|
|
|
const int8_t *int_reg_params = _ir_int_reg_params;
|
|
|
|
const int8_t *fp_reg_params = _ir_fp_reg_params;
|
|
|
|
int32_t used_stack, stack_offset = 0;
|
|
|
|
ir_copy *copies;
|
2022-12-26 16:27:53 +01:00
|
|
|
bool do_pass3 = 0;
|
2023-04-26 13:10:58 +02:00
|
|
|
/* For temporaries we may use any scratch registers except for registers used for parameters */
|
2022-05-31 23:51:02 +02:00
|
|
|
ir_reg tmp_fp_reg = IR_REG_FP_LAST; /* Temporary register for FP loads and swap */
|
|
|
|
|
2023-07-06 00:15:08 +02:00
|
|
|
n = insn->inputs_count;
|
2022-06-21 12:04:33 +02:00
|
|
|
if (n < 3) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2023-04-26 13:10:58 +02:00
|
|
|
if (tmp_reg == IR_REG_NONE) {
|
|
|
|
tmp_reg = IR_REG_IP0;
|
|
|
|
}
|
|
|
|
|
2022-05-31 23:51:02 +02:00
|
|
|
if (insn->op == IR_CALL && (ctx->flags & IR_PREALLOCATED_STACK)) {
|
|
|
|
// TODO: support for preallocated stack
|
|
|
|
used_stack = 0;
|
|
|
|
} else {
|
2022-08-29 23:42:06 +02:00
|
|
|
used_stack = ir_call_used_stack(ctx, insn);
|
|
|
|
/* Stack must be 16 byte aligned */
|
|
|
|
used_stack = IR_ALIGNED_SIZE(used_stack, 16);
|
2023-02-17 13:52:26 +01:00
|
|
|
if (ctx->fixed_call_stack_size && used_stack <= ctx->fixed_call_stack_size) {
|
|
|
|
used_stack = 0;
|
|
|
|
} else {
|
2023-06-15 18:28:54 +02:00
|
|
|
ctx->call_stack_size += used_stack;
|
2023-02-17 13:52:26 +01:00
|
|
|
if (used_stack) {
|
2023-12-04 09:32:26 +01:00
|
|
|
if (insn->op == IR_TAILCALL && !(ctx->flags & IR_USE_FRAME_POINTER)) {
|
|
|
|
ctx->flags |= IR_USE_FRAME_POINTER;
|
|
|
|
| stp x29, x30, [sp, # (-(ctx->stack_frame_size+16))]!
|
|
|
|
| mov x29, sp
|
|
|
|
}
|
2023-02-17 13:52:26 +01:00
|
|
|
| sub sp, sp, #used_stack
|
|
|
|
}
|
2022-05-31 23:51:02 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-11-30 21:50:53 +01:00
|
|
|
#ifdef __APPLE__
|
|
|
|
const ir_proto_t *proto = ir_call_proto(ctx, insn);
|
|
|
|
int last_named_input = (proto && (proto->flags & IR_VARARG_FUNC)) ? proto->params_count + 2 : insn->inputs_count;
|
|
|
|
#endif
|
|
|
|
|
2022-12-26 16:27:53 +01:00
|
|
|
/* 1. move all register arguments that should be passed through stack
|
2022-05-31 23:51:02 +02:00
|
|
|
* and collect arguments that should be passed through registers */
|
|
|
|
copies = ir_mem_malloc((n - 2) * sizeof(ir_copy));
|
|
|
|
for (j = 3; j <= n; j++) {
|
2022-11-08 16:17:29 +01:00
|
|
|
arg = ir_insn_op(insn, j);
|
2023-01-20 13:35:02 +01:00
|
|
|
src_reg = ir_get_alocated_reg(ctx, def, j);
|
2022-05-31 23:51:02 +02:00
|
|
|
arg_insn = &ctx->ir_base[arg];
|
|
|
|
type = arg_insn->type;
|
2023-11-30 21:50:53 +01:00
|
|
|
#ifdef __APPLE__
|
|
|
|
if (j > last_named_input) {
|
|
|
|
dst_reg = IR_REG_NONE; /* pass argument through stack */
|
|
|
|
} else
|
|
|
|
#endif
|
2022-05-31 23:51:02 +02:00
|
|
|
if (IR_IS_TYPE_INT(type)) {
|
|
|
|
if (int_param < int_reg_params_count) {
|
|
|
|
dst_reg = int_reg_params[int_param];
|
|
|
|
} else {
|
|
|
|
dst_reg = IR_REG_NONE; /* pass argument through stack */
|
|
|
|
}
|
|
|
|
int_param++;
|
2023-07-27 10:16:00 +02:00
|
|
|
} else {
|
|
|
|
IR_ASSERT(IR_IS_TYPE_FP(type));
|
2022-05-31 23:51:02 +02:00
|
|
|
if (fp_param < fp_reg_params_count) {
|
|
|
|
dst_reg = fp_reg_params[fp_param];
|
|
|
|
} else {
|
|
|
|
dst_reg = IR_REG_NONE; /* pass argument through stack */
|
|
|
|
}
|
|
|
|
fp_param++;
|
|
|
|
}
|
|
|
|
if (dst_reg != IR_REG_NONE) {
|
2022-12-26 16:27:53 +01:00
|
|
|
if (IR_IS_CONST_REF(arg) || src_reg == IR_REG_NONE) {
|
|
|
|
/* delay CONST->REG and MEM->REG moves to third pass */
|
|
|
|
do_pass3 = 1;
|
|
|
|
} else {
|
|
|
|
IR_ASSERT(src_reg != IR_REG_NONE);
|
2023-06-14 19:23:32 +02:00
|
|
|
if (IR_REG_SPILLED(src_reg)) {
|
|
|
|
src_reg = IR_REG_NUM(src_reg);
|
2022-12-26 16:27:53 +01:00
|
|
|
ir_emit_load(ctx, type, src_reg, arg);
|
|
|
|
}
|
|
|
|
if (src_reg != dst_reg) {
|
|
|
|
/* delay REG->REG moves to second pass */
|
|
|
|
copies[count].type = type;
|
|
|
|
copies[count].from = src_reg;
|
|
|
|
copies[count].to = dst_reg;
|
|
|
|
count++;
|
|
|
|
}
|
2022-05-31 23:51:02 +02:00
|
|
|
}
|
|
|
|
} else {
|
2022-12-26 16:27:53 +01:00
|
|
|
/* Pass register arguments to stack (REG->MEM moves) */
|
2023-06-14 19:23:32 +02:00
|
|
|
if (!IR_IS_CONST_REF(arg) && src_reg != IR_REG_NONE && !IR_REG_SPILLED(src_reg)) {
|
2022-12-26 16:27:53 +01:00
|
|
|
if (IR_IS_TYPE_INT(type)) {
|
2022-05-31 23:51:02 +02:00
|
|
|
ir_emit_store_mem_int(ctx, type, IR_REG_STACK_POINTER, stack_offset, src_reg);
|
|
|
|
} else {
|
|
|
|
ir_emit_store_mem_fp(ctx, type, IR_REG_STACK_POINTER, stack_offset, src_reg);
|
|
|
|
}
|
2022-12-26 16:27:53 +01:00
|
|
|
} else {
|
|
|
|
do_pass3 = 1;
|
2022-05-31 23:51:02 +02:00
|
|
|
}
|
|
|
|
stack_offset += IR_MAX(sizeof(void*), ir_type_size[type]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-10-03 07:34:02 +02:00
|
|
|
/* 2. move all arguments that should be passed from one register to another (REG->REG movs) */
|
2022-05-31 23:51:02 +02:00
|
|
|
if (count) {
|
|
|
|
ir_parallel_copy(ctx, copies, count, tmp_reg, tmp_fp_reg);
|
|
|
|
}
|
|
|
|
ir_mem_free(copies);
|
|
|
|
|
2022-12-26 16:27:53 +01:00
|
|
|
/* 3. move the remaining memory and immediate values */
|
|
|
|
if (do_pass3) {
|
|
|
|
stack_offset = 0;
|
2022-05-31 23:51:02 +02:00
|
|
|
int_param = 0;
|
|
|
|
fp_param = 0;
|
|
|
|
for (j = 3; j <= n; j++) {
|
2022-11-08 16:17:29 +01:00
|
|
|
arg = ir_insn_op(insn, j);
|
2023-01-20 13:35:02 +01:00
|
|
|
src_reg = ir_get_alocated_reg(ctx, def, j);
|
2022-05-31 23:51:02 +02:00
|
|
|
arg_insn = &ctx->ir_base[arg];
|
|
|
|
type = arg_insn->type;
|
2023-11-30 21:50:53 +01:00
|
|
|
#ifdef __APPLE__
|
|
|
|
if (j > last_named_input) {
|
|
|
|
dst_reg = IR_REG_NONE; /* pass argument through stack */
|
|
|
|
} else
|
|
|
|
#endif
|
2022-05-31 23:51:02 +02:00
|
|
|
if (IR_IS_TYPE_INT(type)) {
|
|
|
|
if (int_param < int_reg_params_count) {
|
|
|
|
dst_reg = int_reg_params[int_param];
|
|
|
|
} else {
|
|
|
|
dst_reg = IR_REG_NONE; /* argument already passed through stack */
|
|
|
|
}
|
|
|
|
int_param++;
|
2023-07-27 10:16:00 +02:00
|
|
|
} else {
|
|
|
|
IR_ASSERT(IR_IS_TYPE_FP(type));
|
2022-05-31 23:51:02 +02:00
|
|
|
if (fp_param < fp_reg_params_count) {
|
|
|
|
dst_reg = fp_reg_params[fp_param];
|
|
|
|
} else {
|
|
|
|
dst_reg = IR_REG_NONE; /* argument already passed through stack */
|
|
|
|
}
|
|
|
|
fp_param++;
|
|
|
|
}
|
2022-12-26 16:27:53 +01:00
|
|
|
if (dst_reg != IR_REG_NONE) {
|
|
|
|
if (IR_IS_CONST_REF(arg) || src_reg == IR_REG_NONE) {
|
2023-12-08 15:35:41 +01:00
|
|
|
if (IR_IS_CONST_REF(arg) && IR_IS_TYPE_INT(type)) {
|
|
|
|
if (ir_type_size[type] == 1) {
|
|
|
|
type = IR_ADDR;
|
2022-12-26 16:27:53 +01:00
|
|
|
}
|
|
|
|
}
|
2023-12-08 15:35:41 +01:00
|
|
|
ir_emit_load(ctx, type, dst_reg, arg);
|
2022-12-26 16:27:53 +01:00
|
|
|
}
|
|
|
|
} else {
|
2022-05-31 23:51:02 +02:00
|
|
|
if (IR_IS_TYPE_INT(type)) {
|
2023-12-08 15:35:41 +01:00
|
|
|
if (IR_IS_CONST_REF(arg) || src_reg == IR_REG_NONE) {
|
2022-12-26 16:27:53 +01:00
|
|
|
IR_ASSERT(tmp_reg != IR_REG_NONE);
|
|
|
|
ir_emit_load(ctx, type, tmp_reg, arg);
|
2023-12-08 15:35:41 +01:00
|
|
|
if (IR_IS_CONST_REF(arg)) {
|
|
|
|
type = IR_ADDR; //TODO: ???
|
|
|
|
}
|
2022-12-26 16:27:53 +01:00
|
|
|
ir_emit_store_mem_int(ctx, type, IR_REG_STACK_POINTER, stack_offset, tmp_reg);
|
2023-06-14 19:23:32 +02:00
|
|
|
} else if (IR_REG_SPILLED(src_reg)) {
|
|
|
|
src_reg = IR_REG_NUM(src_reg);
|
2022-12-26 18:25:11 +01:00
|
|
|
ir_emit_load(ctx, type, src_reg, arg);
|
|
|
|
ir_emit_store_mem_int(ctx, type, IR_REG_STACK_POINTER, stack_offset, src_reg);
|
2022-05-31 23:51:02 +02:00
|
|
|
}
|
|
|
|
} else {
|
2022-12-26 16:27:53 +01:00
|
|
|
if (IR_IS_CONST_REF(arg)) {
|
|
|
|
ir_emit_load(ctx, type, tmp_fp_reg, arg);
|
|
|
|
ir_emit_store_mem_fp(ctx, IR_DOUBLE, IR_REG_STACK_POINTER, stack_offset, tmp_fp_reg);
|
|
|
|
} else if (src_reg == IR_REG_NONE) {
|
|
|
|
IR_ASSERT(tmp_fp_reg != IR_REG_NONE);
|
|
|
|
ir_emit_load(ctx, type, tmp_fp_reg, arg);
|
|
|
|
ir_emit_store_mem_fp(ctx, IR_DOUBLE, IR_REG_STACK_POINTER, stack_offset, tmp_fp_reg);
|
2023-06-14 19:23:32 +02:00
|
|
|
} else if (IR_REG_SPILLED(src_reg)) {
|
|
|
|
src_reg = IR_REG_NUM(src_reg);
|
2022-12-26 18:25:11 +01:00
|
|
|
ir_emit_load(ctx, type, src_reg, arg);
|
|
|
|
ir_emit_store_mem_fp(ctx, type, IR_REG_STACK_POINTER, stack_offset, src_reg);
|
2022-12-26 16:27:53 +01:00
|
|
|
}
|
2022-05-31 23:51:02 +02:00
|
|
|
}
|
2022-12-26 16:27:53 +01:00
|
|
|
stack_offset += IR_MAX(sizeof(void*), ir_type_size[type]);
|
2022-05-31 23:51:02 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return used_stack;
|
|
|
|
}
|
|
|
|
|
2023-12-04 09:32:26 +01:00
|
|
|
static void ir_emit_call_ex(ir_ctx *ctx, ir_ref def, ir_insn *insn, int32_t used_stack)
|
2022-05-31 23:51:02 +02:00
|
|
|
{
|
|
|
|
ir_backend_data *data = ctx->data;
|
|
|
|
dasm_State **Dst = &data->dasm_state;
|
|
|
|
ir_reg def_reg;
|
|
|
|
|
|
|
|
if (IR_IS_CONST_REF(insn->op2)) {
|
2023-12-08 15:35:41 +01:00
|
|
|
void *addr = ir_call_addr(ctx, insn, &ctx->ir_base[insn->op2]);
|
|
|
|
|
2023-12-19 20:22:49 +01:00
|
|
|
if (aarch64_may_use_b(ctx->code_buffer, addr)) {
|
2022-06-28 00:43:59 +02:00
|
|
|
| bl &addr
|
|
|
|
} else {
|
|
|
|
ir_emit_load_imm_int(ctx, IR_ADDR, IR_REG_INT_TMP, (intptr_t)addr);
|
|
|
|
| blr Rx(IR_REG_INT_TMP)
|
|
|
|
}
|
2022-05-31 23:51:02 +02:00
|
|
|
} else {
|
|
|
|
ir_reg op2_reg = ctx->regs[def][2];
|
|
|
|
|
2022-06-02 23:38:49 +02:00
|
|
|
IR_ASSERT(op2_reg != IR_REG_NONE);
|
2023-06-14 19:23:32 +02:00
|
|
|
if (IR_REG_SPILLED(op2_reg)) {
|
|
|
|
op2_reg = IR_REG_NUM(op2_reg);
|
2022-06-02 23:38:49 +02:00
|
|
|
ir_emit_load(ctx, IR_ADDR, op2_reg, insn->op2);
|
2022-05-31 23:51:02 +02:00
|
|
|
}
|
2022-06-02 23:38:49 +02:00
|
|
|
| blr Rx(op2_reg)
|
2022-05-31 23:51:02 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (used_stack) {
|
2022-06-02 17:34:47 +02:00
|
|
|
| add sp, sp, #used_stack
|
2023-06-15 18:28:54 +02:00
|
|
|
ctx->call_stack_size -= used_stack;
|
2022-05-31 23:51:02 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (insn->type != IR_VOID) {
|
|
|
|
if (IR_IS_TYPE_INT(insn->type)) {
|
|
|
|
def_reg = IR_REG_NUM(ctx->regs[def][0]);
|
|
|
|
if (def_reg != IR_REG_NONE) {
|
|
|
|
if (def_reg != IR_REG_INT_RET1) {
|
|
|
|
ir_emit_mov(ctx, insn->type, def_reg, IR_REG_INT_RET1);
|
|
|
|
}
|
2023-06-14 19:23:32 +02:00
|
|
|
if (IR_REG_SPILLED(ctx->regs[def][0])) {
|
2022-05-31 23:51:02 +02:00
|
|
|
ir_emit_store(ctx, insn->type, def, def_reg);
|
|
|
|
}
|
2022-08-03 23:22:19 +02:00
|
|
|
} else if (ctx->use_lists[def].count > 1) {
|
2022-05-31 23:51:02 +02:00
|
|
|
ir_emit_store(ctx, insn->type, def, IR_REG_INT_RET1);
|
|
|
|
}
|
2023-07-27 10:16:00 +02:00
|
|
|
} else {
|
|
|
|
IR_ASSERT(IR_IS_TYPE_FP(insn->type));
|
2022-05-31 23:51:02 +02:00
|
|
|
def_reg = IR_REG_NUM(ctx->regs[def][0]);
|
|
|
|
if (def_reg != IR_REG_NONE) {
|
|
|
|
if (def_reg != IR_REG_FP_RET1) {
|
|
|
|
ir_emit_fp_mov(ctx, insn->type, def_reg, IR_REG_FP_RET1);
|
|
|
|
}
|
2023-06-14 19:23:32 +02:00
|
|
|
if (IR_REG_SPILLED(ctx->regs[def][0])) {
|
2022-05-31 23:51:02 +02:00
|
|
|
ir_emit_store(ctx, insn->type, def, def_reg);
|
|
|
|
}
|
2022-08-03 23:22:19 +02:00
|
|
|
} else if (ctx->use_lists[def].count > 1) {
|
2022-05-31 23:51:02 +02:00
|
|
|
ir_emit_store(ctx, insn->type, def, IR_REG_FP_RET1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-12-04 09:32:26 +01:00
|
|
|
static void ir_emit_call(ir_ctx *ctx, ir_ref def, ir_insn *insn)
|
|
|
|
{
|
|
|
|
int32_t used_stack = ir_emit_arguments(ctx, def, insn, ctx->regs[def][1]);
|
|
|
|
ir_emit_call_ex(ctx, def, insn, used_stack);
|
|
|
|
}
|
|
|
|
|
2022-05-31 23:51:02 +02:00
|
|
|
static void ir_emit_tailcall(ir_ctx *ctx, ir_ref def, ir_insn *insn)
|
|
|
|
{
|
|
|
|
ir_backend_data *data = ctx->data;
|
|
|
|
dasm_State **Dst = &data->dasm_state;
|
2023-04-26 13:10:58 +02:00
|
|
|
int32_t used_stack = ir_emit_arguments(ctx, def, insn, ctx->regs[def][1]);
|
2022-05-31 23:51:02 +02:00
|
|
|
|
2022-06-22 16:57:31 +02:00
|
|
|
if (used_stack != 0) {
|
2023-12-04 09:32:26 +01:00
|
|
|
ir_emit_call_ex(ctx, def, insn, used_stack);
|
2022-06-22 16:57:31 +02:00
|
|
|
ir_emit_return_void(ctx);
|
|
|
|
return;
|
|
|
|
}
|
2022-05-31 23:51:02 +02:00
|
|
|
|
|
|
|
ir_emit_epilogue(ctx);
|
|
|
|
|
|
|
|
if (IR_IS_CONST_REF(insn->op2)) {
|
2023-12-08 15:35:41 +01:00
|
|
|
void *addr = ir_call_addr(ctx, insn, &ctx->ir_base[insn->op2]);
|
2022-05-31 23:51:02 +02:00
|
|
|
|
2023-12-19 20:22:49 +01:00
|
|
|
if (aarch64_may_use_b(ctx->code_buffer, addr)) {
|
2022-06-28 00:43:59 +02:00
|
|
|
| b &addr
|
|
|
|
} else {
|
|
|
|
ir_emit_load_imm_int(ctx, IR_ADDR, IR_REG_INT_TMP, (intptr_t)addr);
|
|
|
|
| br Rx(IR_REG_INT_TMP)
|
|
|
|
}
|
2022-05-31 23:51:02 +02:00
|
|
|
} else {
|
|
|
|
ir_reg op2_reg = ctx->regs[def][2];
|
|
|
|
|
2022-06-02 23:38:49 +02:00
|
|
|
IR_ASSERT(op2_reg != IR_REG_NONE);
|
2023-06-14 19:23:32 +02:00
|
|
|
if (IR_REG_SPILLED(op2_reg)) {
|
|
|
|
op2_reg = IR_REG_NUM(op2_reg);
|
2022-06-02 23:38:49 +02:00
|
|
|
ir_emit_load(ctx, IR_ADDR, op2_reg, insn->op2);
|
2022-05-31 23:51:02 +02:00
|
|
|
}
|
2022-06-02 23:38:49 +02:00
|
|
|
| br Rx(op2_reg)
|
2022-05-31 23:51:02 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void ir_emit_ijmp(ir_ctx *ctx, ir_ref def, ir_insn *insn)
|
|
|
|
{
|
|
|
|
ir_backend_data *data = ctx->data;
|
|
|
|
dasm_State **Dst = &data->dasm_state;
|
|
|
|
ir_reg op2_reg = ctx->regs[def][2];
|
|
|
|
|
2022-09-01 18:19:01 +02:00
|
|
|
if (op2_reg != IR_REG_NONE) {
|
2023-06-14 19:23:32 +02:00
|
|
|
if (IR_REG_SPILLED(op2_reg)) {
|
|
|
|
op2_reg = IR_REG_NUM(op2_reg);
|
2022-09-01 18:19:01 +02:00
|
|
|
ir_emit_load(ctx, IR_ADDR, op2_reg, insn->op2);
|
|
|
|
}
|
|
|
|
| br Rx(op2_reg)
|
|
|
|
} else if (IR_IS_CONST_REF(insn->op2)) {
|
2022-10-21 16:16:25 +02:00
|
|
|
void *addr = ir_jmp_addr(ctx, insn, &ctx->ir_base[insn->op2]);
|
2022-09-01 18:19:01 +02:00
|
|
|
|
2023-12-19 20:22:49 +01:00
|
|
|
if (aarch64_may_use_b(ctx->code_buffer, addr)) {
|
2022-09-29 00:25:42 +02:00
|
|
|
| b &addr
|
|
|
|
} else {
|
|
|
|
ir_emit_load_imm_int(ctx, IR_ADDR, IR_REG_INT_TMP, (intptr_t)addr);
|
|
|
|
| br Rx(IR_REG_INT_TMP)
|
|
|
|
}
|
2022-09-01 18:19:01 +02:00
|
|
|
} else {
|
|
|
|
IR_ASSERT(0);
|
2022-05-31 23:51:02 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-06-28 00:43:59 +02:00
|
|
|
static void ir_emit_guard(ir_ctx *ctx, ir_ref def, ir_insn *insn)
|
|
|
|
{
|
|
|
|
ir_backend_data *data = ctx->data;
|
|
|
|
dasm_State **Dst = &data->dasm_state;
|
|
|
|
ir_reg op2_reg = ctx->regs[def][2];
|
|
|
|
ir_type type = ctx->ir_base[insn->op2].type;
|
|
|
|
|
2022-09-29 10:31:07 +02:00
|
|
|
IR_ASSERT(IR_IS_TYPE_INT(type));
|
2022-09-16 11:29:55 +02:00
|
|
|
if (IR_IS_CONST_REF(insn->op2)) {
|
2023-06-29 22:49:20 +02:00
|
|
|
bool is_true = ir_ref_is_true(ctx, insn->op2);
|
|
|
|
|
|
|
|
if ((insn->op == IR_GUARD && !is_true) || (insn->op == IR_GUARD_NOT && is_true)) {
|
2022-09-16 11:29:55 +02:00
|
|
|
if (IR_IS_CONST_REF(insn->op3)) {
|
2022-10-21 16:16:25 +02:00
|
|
|
void *addr = ir_jmp_addr(ctx, insn, &ctx->ir_base[insn->op3]);
|
2022-09-16 11:29:55 +02:00
|
|
|
|
2023-12-19 20:22:49 +01:00
|
|
|
if (aarch64_may_use_b(ctx->code_buffer, addr)) {
|
2022-09-29 00:25:42 +02:00
|
|
|
| b &addr
|
|
|
|
} else {
|
|
|
|
ir_emit_load_imm_int(ctx, IR_ADDR, IR_REG_INT_TMP, (intptr_t)addr);
|
|
|
|
| br Rx(IR_REG_INT_TMP)
|
|
|
|
}
|
2022-09-16 11:29:55 +02:00
|
|
|
} else {
|
|
|
|
IR_ASSERT(0);
|
|
|
|
}
|
|
|
|
}
|
2022-09-29 01:10:44 +02:00
|
|
|
return;
|
2022-09-16 11:29:55 +02:00
|
|
|
}
|
|
|
|
|
2022-06-28 00:43:59 +02:00
|
|
|
IR_ASSERT(op2_reg != IR_REG_NONE);
|
2023-06-14 19:23:32 +02:00
|
|
|
if (IR_REG_SPILLED(op2_reg)) {
|
|
|
|
op2_reg = IR_REG_NUM(op2_reg);
|
2022-06-28 00:43:59 +02:00
|
|
|
ir_emit_load(ctx, type, op2_reg, insn->op2);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (IR_IS_CONST_REF(insn->op3)) {
|
2022-10-21 16:16:25 +02:00
|
|
|
void *addr = ir_jmp_addr(ctx, insn, &ctx->ir_base[insn->op3]);
|
2022-06-28 00:43:59 +02:00
|
|
|
|
2022-09-02 12:54:31 +02:00
|
|
|
if (insn->op == IR_GUARD) {
|
2022-09-29 10:31:07 +02:00
|
|
|
if (ir_type_size[type] == 8) {
|
|
|
|
| cbz Rx(op2_reg), &addr
|
|
|
|
} else {
|
|
|
|
| cbz Rw(op2_reg), &addr
|
|
|
|
}
|
2022-09-02 12:54:31 +02:00
|
|
|
} else {
|
2022-09-29 10:31:07 +02:00
|
|
|
if (ir_type_size[type] == 8) {
|
|
|
|
| cbnz Rx(op2_reg), &addr
|
|
|
|
} else {
|
|
|
|
| cbnz Rw(op2_reg), &addr
|
|
|
|
}
|
2022-09-02 12:54:31 +02:00
|
|
|
}
|
2022-06-28 00:43:59 +02:00
|
|
|
} else {
|
|
|
|
IR_ASSERT(0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-06-28 15:36:06 +02:00
|
|
|
static void ir_emit_guard_jz(ir_ctx *ctx, uint8_t op, void *addr, ir_type type, ir_reg reg)
|
|
|
|
{
|
|
|
|
ir_backend_data *data = ctx->data;
|
|
|
|
dasm_State **Dst = &data->dasm_state;
|
|
|
|
|
|
|
|
if (op == IR_EQ) {
|
|
|
|
if (ir_type_size[type] == 8) {
|
|
|
|
| cbnz Rx(reg), &addr
|
|
|
|
} else {
|
|
|
|
| cbnz Rw(reg), &addr
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
IR_ASSERT(op == IR_NE);
|
|
|
|
if (ir_type_size[type] == 8) {
|
|
|
|
| cbz Rx(reg), &addr
|
|
|
|
} else {
|
|
|
|
| cbz Rw(reg), &addr
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void ir_emit_guard_jcc(ir_ctx *ctx, uint8_t op, void *addr, bool int_cmp)
|
|
|
|
{
|
|
|
|
ir_backend_data *data = ctx->data;
|
|
|
|
dasm_State **Dst = &data->dasm_state;
|
|
|
|
|
|
|
|
if (int_cmp) {
|
|
|
|
switch (op) {
|
2023-07-27 10:16:00 +02:00
|
|
|
default:
|
|
|
|
IR_ASSERT(0 && "NIY binary op");
|
2022-06-28 15:36:06 +02:00
|
|
|
case IR_EQ:
|
|
|
|
| beq &addr
|
|
|
|
break;
|
|
|
|
case IR_NE:
|
|
|
|
| bne &addr
|
|
|
|
break;
|
|
|
|
case IR_LT:
|
|
|
|
| blt &addr
|
|
|
|
break;
|
|
|
|
case IR_GE:
|
|
|
|
| bge &addr
|
|
|
|
break;
|
|
|
|
case IR_LE:
|
|
|
|
| ble &addr
|
|
|
|
break;
|
|
|
|
case IR_GT:
|
|
|
|
| bgt &addr
|
|
|
|
break;
|
|
|
|
case IR_ULT:
|
|
|
|
| blo &addr
|
|
|
|
break;
|
|
|
|
case IR_UGE:
|
|
|
|
| bhs &addr
|
|
|
|
break;
|
|
|
|
case IR_ULE:
|
|
|
|
| bls &addr
|
|
|
|
break;
|
|
|
|
case IR_UGT:
|
|
|
|
| bhi &addr
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
switch (op) {
|
2023-07-27 10:16:00 +02:00
|
|
|
default:
|
|
|
|
IR_ASSERT(0 && "NIY binary op");
|
2022-06-28 15:36:06 +02:00
|
|
|
case IR_EQ:
|
|
|
|
| beq &addr
|
|
|
|
break;
|
|
|
|
case IR_NE:
|
|
|
|
| bne &addr
|
|
|
|
break;
|
|
|
|
case IR_LT:
|
2022-09-19 20:55:08 +02:00
|
|
|
| bmi &addr
|
2022-06-28 15:36:06 +02:00
|
|
|
break;
|
|
|
|
case IR_GE:
|
2022-09-19 20:55:08 +02:00
|
|
|
| bge &addr
|
2022-06-28 15:36:06 +02:00
|
|
|
break;
|
|
|
|
case IR_LE:
|
|
|
|
| bls &addr
|
|
|
|
break;
|
|
|
|
case IR_GT:
|
2022-09-19 20:55:08 +02:00
|
|
|
| bgt &addr
|
2022-06-28 15:36:06 +02:00
|
|
|
break;
|
|
|
|
// case IR_ULT: fprintf(stderr, "\tjb .LL%d\n", true_block); break;
|
|
|
|
// case IR_UGE: fprintf(stderr, "\tjae .LL%d\n", true_block); break;
|
|
|
|
// case IR_ULE: fprintf(stderr, "\tjbe .LL%d\n", true_block); break;
|
|
|
|
// case IR_UGT: fprintf(stderr, "\tja .LL%d\n", true_block); break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-11-08 21:09:35 +01:00
|
|
|
static void ir_emit_guard_cmp_int(ir_ctx *ctx, uint32_t b, ir_ref def, ir_insn *insn)
|
2022-06-28 15:36:06 +02:00
|
|
|
{
|
|
|
|
ir_backend_data *data = ctx->data;
|
|
|
|
dasm_State **Dst = &data->dasm_state;
|
|
|
|
ir_insn *cmp_insn = &ctx->ir_base[insn->op2];
|
|
|
|
ir_op op = cmp_insn->op;
|
|
|
|
ir_type type = ctx->ir_base[cmp_insn->op1].type;
|
|
|
|
ir_ref op1 = cmp_insn->op1;
|
|
|
|
ir_ref op2 = cmp_insn->op2;
|
|
|
|
ir_reg op1_reg = ctx->regs[insn->op2][1];
|
|
|
|
ir_reg op2_reg = ctx->regs[insn->op2][2];
|
|
|
|
void *addr;
|
|
|
|
|
2023-06-14 19:23:32 +02:00
|
|
|
if (op1_reg != IR_REG_NONE && (IR_REG_SPILLED(op1_reg) || IR_IS_CONST_REF(op1))) {
|
|
|
|
op1_reg = IR_REG_NUM(op1_reg);
|
2022-06-28 15:36:06 +02:00
|
|
|
ir_emit_load(ctx, type, op1_reg, op1);
|
|
|
|
}
|
2023-06-14 19:23:32 +02:00
|
|
|
if (op2_reg != IR_REG_NONE && (IR_REG_SPILLED(op2_reg) || IR_IS_CONST_REF(op2))) {
|
|
|
|
op2_reg = IR_REG_NUM(op2_reg);
|
2022-06-28 15:36:06 +02:00
|
|
|
if (op1 != op2) {
|
|
|
|
ir_emit_load(ctx, type, op2_reg, op2);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-10-21 16:16:25 +02:00
|
|
|
addr = ir_jmp_addr(ctx, insn, &ctx->ir_base[insn->op3]);
|
2022-06-28 15:36:06 +02:00
|
|
|
|
2023-12-27 08:18:56 +01:00
|
|
|
if (IR_IS_CONST_REF(op2)
|
|
|
|
&& !IR_IS_SYM_CONST(ctx->ir_base[op2].op)
|
|
|
|
&& ctx->ir_base[op2].val.u64 == 0) {
|
2022-06-28 15:36:06 +02:00
|
|
|
if (op == IR_ULT) {
|
|
|
|
/* always false */
|
2023-12-19 20:22:49 +01:00
|
|
|
if (aarch64_may_use_b(ctx->code_buffer, addr)) {
|
2022-06-28 15:36:06 +02:00
|
|
|
| b &addr
|
|
|
|
} else {
|
|
|
|
ir_emit_load_imm_int(ctx, IR_ADDR, IR_REG_INT_TMP, (intptr_t)addr);
|
|
|
|
| br Rx(IR_REG_INT_TMP)
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
} else if (op == IR_UGE) {
|
|
|
|
/* always true */
|
|
|
|
return;
|
|
|
|
} else if (op == IR_ULE) {
|
|
|
|
op = IR_EQ;
|
|
|
|
} else if (op == IR_UGT) {
|
|
|
|
op = IR_NE;
|
|
|
|
}
|
|
|
|
if (op1_reg != IR_REG_NONE && (op == IR_EQ || op == IR_NE)) {
|
2022-09-02 12:54:31 +02:00
|
|
|
if (insn->op == IR_GUARD_NOT) {
|
|
|
|
op ^= 1; // reverse
|
|
|
|
}
|
2022-06-28 15:36:06 +02:00
|
|
|
ir_emit_guard_jz(ctx, op, addr, type, op1_reg);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
ir_emit_cmp_int_common(ctx, type, op1_reg, op1, op2_reg, op2);
|
|
|
|
|
2022-09-02 12:54:31 +02:00
|
|
|
if (insn->op == IR_GUARD) {
|
|
|
|
op ^= 1; // reverse
|
|
|
|
}
|
2022-06-28 15:36:06 +02:00
|
|
|
|
|
|
|
ir_emit_guard_jcc(ctx, op, addr, 1);
|
|
|
|
}
|
|
|
|
|
2022-11-08 21:09:35 +01:00
|
|
|
static void ir_emit_guard_cmp_fp(ir_ctx *ctx, uint32_t b, ir_ref def, ir_insn *insn)
|
2022-09-26 19:47:29 +02:00
|
|
|
{
|
|
|
|
ir_op op = ir_emit_cmp_fp_common(ctx, insn->op2, &ctx->ir_base[insn->op2]);
|
2022-10-21 16:16:25 +02:00
|
|
|
void *addr = ir_jmp_addr(ctx, insn, &ctx->ir_base[insn->op3]);
|
2022-09-26 19:47:29 +02:00
|
|
|
|
2022-09-27 15:52:15 +02:00
|
|
|
if (insn->op == IR_GUARD) {
|
|
|
|
op ^= 1; // reverse
|
|
|
|
}
|
2022-09-26 19:47:29 +02:00
|
|
|
ir_emit_guard_jcc(ctx, op, addr, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void ir_emit_guard_overflow(ir_ctx *ctx, ir_ref def, ir_insn *insn)
|
|
|
|
{
|
|
|
|
ir_backend_data *data = ctx->data;
|
|
|
|
dasm_State **Dst = &data->dasm_state;
|
2023-01-26 08:08:40 +01:00
|
|
|
ir_insn *overflow_insn = &ctx->ir_base[insn->op2];
|
|
|
|
ir_insn *math_insn = &ctx->ir_base[overflow_insn->op1];
|
|
|
|
ir_type type = math_insn->type;
|
2022-10-21 16:16:25 +02:00
|
|
|
void *addr = ir_jmp_addr(ctx, insn, &ctx->ir_base[insn->op3]);
|
2022-09-26 19:47:29 +02:00
|
|
|
|
|
|
|
IR_ASSERT(IR_IS_TYPE_INT(type));
|
2023-01-26 08:08:40 +01:00
|
|
|
if (math_insn->op == IR_MUL_OV) {
|
|
|
|
if (insn->op == IR_GUARD) {
|
|
|
|
| beq &addr
|
|
|
|
} else {
|
|
|
|
| bne &addr
|
|
|
|
}
|
|
|
|
} else if (IR_IS_TYPE_SIGNED(type)) {
|
2022-09-26 19:47:29 +02:00
|
|
|
if (insn->op == IR_GUARD) {
|
|
|
|
| bvc &addr
|
|
|
|
} else {
|
|
|
|
| bvs &addr
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (insn->op == IR_GUARD) {
|
|
|
|
| bcc &addr
|
|
|
|
} else {
|
|
|
|
| bcs &addr
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-06-28 00:43:59 +02:00
|
|
|
static void ir_emit_tls(ir_ctx *ctx, ir_ref def, ir_insn *insn)
|
|
|
|
{
|
|
|
|
ir_backend_data *data = ctx->data;
|
|
|
|
dasm_State **Dst = &data->dasm_state;
|
|
|
|
uint32_t code;
|
|
|
|
ir_reg reg = IR_REG_NUM(ctx->regs[def][0]);
|
|
|
|
|
2022-09-28 20:56:10 +02:00
|
|
|
if (ctx->use_lists[def].count == 1) {
|
|
|
|
/* dead load */
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-06-28 00:43:59 +02:00
|
|
|
||#ifdef __APPLE__
|
|
|
|
|| code = 0xd53bd060 | reg; // TODO: hard-coded: mrs reg, tpidrro_el0
|
|
|
|
| .long code
|
|
|
|
| and Rx(reg), Rx(reg), #0xfffffffffffffff8
|
|
|
|
|//??? MEM_ACCESS_64_WITH_UOFFSET_64 ldr, Rx(reg), Rx(reg), #insn->op2, TMP1
|
|
|
|
|//??? MEM_ACCESS_64_WITH_UOFFSET_64 ldr, Rx(reg), Rx(reg), #insn->op3, TMP1
|
|
|
|
||#else
|
|
|
|
|| code = 0xd53bd040 | reg; // TODO: hard-coded: mrs reg, tpidr_el0
|
|
|
|
| .long code
|
2023-06-01 23:49:32 +02:00
|
|
|
||//??? IR_ASSERT(insn->op2 <= LDR_STR_PIMM64);
|
2022-06-28 00:43:59 +02:00
|
|
|
| ldr Rx(reg), [Rx(reg), #insn->op2]
|
|
|
|
||#endif
|
2023-06-14 19:23:32 +02:00
|
|
|
if (IR_REG_SPILLED(ctx->regs[def][0])) {
|
2022-06-28 00:43:59 +02:00
|
|
|
ir_emit_store(ctx, IR_ADDR, def, reg);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-09-29 00:25:42 +02:00
|
|
|
static void ir_emit_exitcall(ir_ctx *ctx, ir_ref def, ir_insn *insn)
|
|
|
|
{
|
|
|
|
ir_backend_data *data = ctx->data;
|
|
|
|
dasm_State **Dst = &data->dasm_state;
|
|
|
|
ir_reg def_reg = IR_REG_NUM(ctx->regs[def][0]);
|
|
|
|
|
|
|
|
IR_ASSERT(def_reg != IR_REG_NONE);
|
|
|
|
|
|
|
|
| stp d30, d31, [sp, #-16]!
|
|
|
|
| stp d28, d29, [sp, #-16]!
|
|
|
|
| stp d26, d27, [sp, #-16]!
|
|
|
|
| stp d24, d25, [sp, #-16]!
|
|
|
|
| stp d22, d23, [sp, #-16]!
|
|
|
|
| stp d20, d21, [sp, #-16]!
|
|
|
|
| stp d18, d19, [sp, #-16]!
|
|
|
|
| stp d16, d17, [sp, #-16]!
|
|
|
|
| stp d14, d15, [sp, #-16]!
|
|
|
|
| stp d12, d13, [sp, #-16]!
|
|
|
|
| stp d10, d11, [sp, #-16]!
|
|
|
|
| stp d8, d9, [sp, #-16]!
|
|
|
|
| stp d6, d7, [sp, #-16]!
|
|
|
|
| stp d4, d5, [sp, #-16]!
|
|
|
|
| stp d2, d3, [sp, #-16]!
|
|
|
|
| stp d0, d1, [sp, #-16]!
|
|
|
|
|
|
|
|
| str x30, [sp, #-16]!
|
|
|
|
| stp x28, x29, [sp, #-16]!
|
|
|
|
| stp x26, x27, [sp, #-16]!
|
|
|
|
| stp x24, x25, [sp, #-16]!
|
|
|
|
| stp x22, x23, [sp, #-16]!
|
|
|
|
| stp x20, x21, [sp, #-16]!
|
|
|
|
| stp x18, x19, [sp, #-16]!
|
|
|
|
| stp x16, x17, [sp, #-16]!
|
|
|
|
| stp x14, x15, [sp, #-16]!
|
|
|
|
| stp x12, x13, [sp, #-16]!
|
|
|
|
| stp x10, x11, [sp, #-16]!
|
|
|
|
| stp x8, x9, [sp, #-16]!
|
|
|
|
| stp x6, x7, [sp, #-16]!
|
|
|
|
| stp x4, x5, [sp, #-16]!
|
|
|
|
| stp x2, x3, [sp, #-16]!
|
|
|
|
| stp x0, x1, [sp, #-16]!
|
|
|
|
|
2022-09-29 13:17:54 +02:00
|
|
|
| mov Rx(IR_REG_INT_ARG2), sp
|
2023-06-13 17:22:21 +02:00
|
|
|
| add Rx(IR_REG_INT_ARG1), Rx(IR_REG_INT_ARG2), #(32*8+32*8)
|
|
|
|
| str Rx(IR_REG_INT_ARG1), [sp, #(31*8)]
|
2022-09-29 00:25:42 +02:00
|
|
|
| mov Rx(IR_REG_INT_ARG1), Rx(IR_REG_INT_TMP)
|
|
|
|
|
|
|
|
if (IR_IS_CONST_REF(insn->op2)) {
|
2023-12-08 15:35:41 +01:00
|
|
|
void *addr = ir_call_addr(ctx, insn, &ctx->ir_base[insn->op2]);
|
2022-09-29 00:25:42 +02:00
|
|
|
|
2023-12-19 20:22:49 +01:00
|
|
|
if (aarch64_may_use_b(ctx->code_buffer, addr)) {
|
2022-09-29 01:10:44 +02:00
|
|
|
| bl &addr
|
2022-09-29 00:25:42 +02:00
|
|
|
} else {
|
|
|
|
ir_emit_load_imm_int(ctx, IR_ADDR, IR_REG_INT_TMP, (intptr_t)addr);
|
2022-09-29 01:10:44 +02:00
|
|
|
| blr Rx(IR_REG_INT_TMP)
|
2022-09-29 00:25:42 +02:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
IR_ASSERT(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
| add sp, sp, #(32*8+32*8)
|
|
|
|
|
|
|
|
if (def_reg != IR_REG_INT_RET1) {
|
|
|
|
ir_emit_mov(ctx, insn->type, def_reg, IR_REG_INT_RET1);
|
|
|
|
}
|
2023-06-14 19:23:32 +02:00
|
|
|
if (IR_REG_SPILLED(ctx->regs[def][0])) {
|
2022-09-29 00:25:42 +02:00
|
|
|
ir_emit_store(ctx, insn->type, def, def_reg);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-05-31 23:51:02 +02:00
|
|
|
static void ir_emit_param_move(ir_ctx *ctx, uint8_t type, ir_reg from_reg, ir_reg to_reg, ir_ref to, int32_t offset)
|
|
|
|
{
|
|
|
|
ir_reg fp = (ctx->flags & IR_USE_FRAME_POINTER) ? IR_REG_FRAME_POINTER : IR_REG_STACK_POINTER;
|
|
|
|
|
|
|
|
IR_ASSERT(from_reg != IR_REG_NONE || to_reg != IR_REG_NONE);
|
|
|
|
|
|
|
|
if (IR_IS_TYPE_INT(type)) {
|
|
|
|
if (from_reg != IR_REG_NONE) {
|
|
|
|
if (to_reg != IR_REG_NONE) {
|
|
|
|
ir_emit_mov(ctx, type, to_reg, from_reg);
|
|
|
|
} else {
|
|
|
|
ir_emit_store(ctx, type, to, from_reg);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
ir_emit_load_mem_int(ctx, type, to_reg, fp, offset);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (from_reg != IR_REG_NONE) {
|
|
|
|
if (to_reg != IR_REG_NONE) {
|
|
|
|
ir_emit_fp_mov(ctx, type, to_reg, from_reg);
|
|
|
|
} else {
|
|
|
|
ir_emit_store(ctx, type, to, from_reg);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
ir_emit_load_mem_fp(ctx, type, to_reg, fp, offset);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void ir_emit_load_params(ir_ctx *ctx)
|
|
|
|
{
|
|
|
|
ir_use_list *use_list = &ctx->use_lists[1];
|
|
|
|
ir_insn *insn;
|
|
|
|
ir_ref i, n, *p, use;
|
|
|
|
int int_param_num = 0;
|
|
|
|
int fp_param_num = 0;
|
|
|
|
ir_reg src_reg;
|
|
|
|
ir_reg dst_reg;
|
|
|
|
// TODO: Calling convention specific
|
|
|
|
int int_reg_params_count = IR_REG_INT_ARGS;
|
|
|
|
int fp_reg_params_count = IR_REG_FP_ARGS;
|
|
|
|
const int8_t *int_reg_params = _ir_int_reg_params;
|
|
|
|
const int8_t *fp_reg_params = _ir_fp_reg_params;
|
|
|
|
int32_t stack_offset = 0;
|
|
|
|
|
|
|
|
if (ctx->flags & IR_USE_FRAME_POINTER) {
|
|
|
|
stack_offset = sizeof(void*) * 2; /* skip old frame pointer and return address */
|
|
|
|
} else {
|
2023-06-15 18:28:54 +02:00
|
|
|
stack_offset = sizeof(void*) + ctx->stack_frame_size + ctx->call_stack_size; /* skip return address */
|
2022-05-31 23:51:02 +02:00
|
|
|
}
|
|
|
|
n = use_list->count;
|
|
|
|
for (i = 0, p = &ctx->use_edges[use_list->refs]; i < n; i++, p++) {
|
|
|
|
use = *p;
|
|
|
|
insn = &ctx->ir_base[use];
|
|
|
|
if (insn->op == IR_PARAM) {
|
|
|
|
if (IR_IS_TYPE_INT(insn->type)) {
|
|
|
|
if (int_param_num < int_reg_params_count) {
|
|
|
|
src_reg = int_reg_params[int_param_num];
|
|
|
|
} else {
|
|
|
|
src_reg = IR_REG_NONE;
|
|
|
|
}
|
|
|
|
int_param_num++;
|
|
|
|
} else {
|
|
|
|
if (fp_param_num < fp_reg_params_count) {
|
|
|
|
src_reg = fp_reg_params[fp_param_num];
|
|
|
|
} else {
|
|
|
|
src_reg = IR_REG_NONE;
|
|
|
|
}
|
|
|
|
fp_param_num++;
|
|
|
|
}
|
|
|
|
if (ctx->vregs[use]) {
|
|
|
|
dst_reg = IR_REG_NUM(ctx->regs[use][0]);
|
|
|
|
IR_ASSERT(src_reg != IR_REG_NONE || dst_reg != IR_REG_NONE ||
|
|
|
|
stack_offset == ctx->live_intervals[ctx->vregs[use]]->stack_spill_pos +
|
2023-06-15 18:28:54 +02:00
|
|
|
((ctx->flags & IR_USE_FRAME_POINTER) ? -ctx->stack_frame_size : ctx->call_stack_size));
|
2022-05-31 23:51:02 +02:00
|
|
|
if (src_reg != dst_reg) {
|
|
|
|
ir_emit_param_move(ctx, insn->type, src_reg, dst_reg, use, stack_offset);
|
|
|
|
}
|
2023-06-14 19:23:32 +02:00
|
|
|
if (dst_reg != IR_REG_NONE && IR_REG_SPILLED(ctx->regs[use][0])) {
|
2022-05-31 23:51:02 +02:00
|
|
|
ir_emit_store(ctx, insn->type, use, dst_reg);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (src_reg == IR_REG_NONE) {
|
|
|
|
if (sizeof(void*) == 8) {
|
|
|
|
stack_offset += sizeof(void*);
|
|
|
|
} else {
|
|
|
|
stack_offset += IR_MAX(sizeof(void*), ir_type_size[insn->type]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static ir_reg ir_get_free_reg(ir_type type, ir_regset available)
|
|
|
|
{
|
|
|
|
if (IR_IS_TYPE_INT(type)) {
|
|
|
|
available = IR_REGSET_INTERSECTION(available, IR_REGSET_GP);
|
|
|
|
} else {
|
|
|
|
IR_ASSERT(IR_IS_TYPE_FP(type));
|
|
|
|
available = IR_REGSET_INTERSECTION(available, IR_REGSET_FP);
|
|
|
|
}
|
|
|
|
IR_ASSERT(!IR_REGSET_IS_EMPTY(available));
|
|
|
|
return IR_REGSET_FIRST(available);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int ir_fix_dessa_tmps(ir_ctx *ctx, uint8_t type, ir_ref from, ir_ref to)
|
|
|
|
{
|
|
|
|
ir_backend_data *data = ctx->data;
|
|
|
|
ir_ref ref = ctx->cfg_blocks[data->dessa_from_block].end;
|
|
|
|
|
|
|
|
if (to == 0) {
|
|
|
|
if (IR_IS_TYPE_INT(type)) {
|
|
|
|
if (ctx->regs[ref][0] == IR_REG_NONE) {
|
2023-05-17 21:37:45 +02:00
|
|
|
ctx->regs[ref][0] = IR_REG_X0;
|
2022-05-31 23:51:02 +02:00
|
|
|
}
|
2023-07-27 10:16:00 +02:00
|
|
|
} else {
|
|
|
|
IR_ASSERT(IR_IS_TYPE_FP(type));
|
2023-05-17 21:37:45 +02:00
|
|
|
if (ctx->regs[ref][1] == IR_REG_NONE) {
|
|
|
|
ctx->regs[ref][1] = IR_REG_V0;
|
2022-05-31 23:51:02 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
} else if (from != 0) {
|
|
|
|
if (IR_IS_TYPE_INT(type)) {
|
2023-05-17 21:37:45 +02:00
|
|
|
if (ctx->regs[ref][0] == IR_REG_NONE) {
|
|
|
|
ctx->regs[ref][0] = IR_REG_X0;
|
2022-05-31 23:51:02 +02:00
|
|
|
}
|
2023-07-27 10:16:00 +02:00
|
|
|
} else {
|
|
|
|
IR_ASSERT(IR_IS_TYPE_FP(type));
|
2023-05-17 21:37:45 +02:00
|
|
|
if (ctx->regs[ref][1] == IR_REG_NONE) {
|
|
|
|
ctx->regs[ref][1] = IR_REG_V0;
|
2022-05-31 23:51:02 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void ir_fix_param_spills(ir_ctx *ctx)
|
|
|
|
{
|
|
|
|
ir_use_list *use_list = &ctx->use_lists[1];
|
|
|
|
ir_insn *insn;
|
|
|
|
ir_ref i, n, *p, use;
|
|
|
|
int int_param_num = 0;
|
|
|
|
int fp_param_num = 0;
|
|
|
|
ir_reg src_reg;
|
|
|
|
// TODO: Calling convention specific
|
|
|
|
int int_reg_params_count = IR_REG_INT_ARGS;
|
|
|
|
int fp_reg_params_count = IR_REG_FP_ARGS;
|
|
|
|
const int8_t *int_reg_params = _ir_int_reg_params;
|
|
|
|
const int8_t *fp_reg_params = _ir_fp_reg_params;
|
|
|
|
int32_t stack_offset = 0;
|
2023-11-23 20:52:08 +01:00
|
|
|
int32_t param_stack_size = 0;
|
2022-05-31 23:51:02 +02:00
|
|
|
|
|
|
|
if (ctx->flags & IR_USE_FRAME_POINTER) {
|
|
|
|
/* skip old frame pointer and return address */
|
2023-06-15 18:28:54 +02:00
|
|
|
stack_offset = sizeof(void*) * 2 + (ctx->stack_frame_size - ctx->stack_frame_alignment);
|
2022-05-31 23:51:02 +02:00
|
|
|
} else {
|
|
|
|
/* skip return address */
|
2023-06-15 18:28:54 +02:00
|
|
|
stack_offset = sizeof(void*) + ctx->stack_frame_size;
|
2022-05-31 23:51:02 +02:00
|
|
|
}
|
|
|
|
n = use_list->count;
|
|
|
|
for (i = 0, p = &ctx->use_edges[use_list->refs]; i < n; i++, p++) {
|
|
|
|
use = *p;
|
|
|
|
insn = &ctx->ir_base[use];
|
|
|
|
if (insn->op == IR_PARAM) {
|
|
|
|
if (IR_IS_TYPE_INT(insn->type)) {
|
|
|
|
if (int_param_num < int_reg_params_count) {
|
|
|
|
src_reg = int_reg_params[int_param_num];
|
|
|
|
} else {
|
|
|
|
src_reg = IR_REG_NONE;
|
|
|
|
}
|
|
|
|
int_param_num++;
|
|
|
|
} else {
|
|
|
|
if (fp_param_num < fp_reg_params_count) {
|
|
|
|
src_reg = fp_reg_params[fp_param_num];
|
|
|
|
} else {
|
|
|
|
src_reg = IR_REG_NONE;
|
|
|
|
}
|
|
|
|
fp_param_num++;
|
|
|
|
}
|
|
|
|
if (src_reg == IR_REG_NONE) {
|
|
|
|
if (ctx->vregs[use]) {
|
|
|
|
ir_live_interval *ival = ctx->live_intervals[ctx->vregs[use]];
|
|
|
|
if ((ival->flags & IR_LIVE_INTERVAL_MEM_PARAM)
|
|
|
|
&& ival->stack_spill_pos == -1
|
|
|
|
&& (ival->next || ival->reg == IR_REG_NONE)) {
|
|
|
|
ival->stack_spill_pos = stack_offset;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (sizeof(void*) == 8) {
|
|
|
|
stack_offset += sizeof(void*);
|
2023-11-23 20:52:08 +01:00
|
|
|
param_stack_size += sizeof(void*);
|
2022-05-31 23:51:02 +02:00
|
|
|
} else {
|
|
|
|
stack_offset += IR_MAX(sizeof(void*), ir_type_size[insn->type]);
|
2023-11-23 20:52:08 +01:00
|
|
|
param_stack_size += IR_MAX(sizeof(void*), ir_type_size[insn->type]);
|
2022-05-31 23:51:02 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2023-11-23 20:52:08 +01:00
|
|
|
|
|
|
|
ctx->gp_reg_params = IR_MIN(int_param_num, int_reg_params_count);
|
|
|
|
ctx->fp_reg_params = IR_MIN(fp_param_num, fp_reg_params_count);
|
|
|
|
ctx->param_stack_size = param_stack_size;
|
2022-05-31 23:51:02 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static void ir_allocate_unique_spill_slots(ir_ctx *ctx)
|
|
|
|
{
|
2022-11-08 21:09:35 +01:00
|
|
|
uint32_t b;
|
2022-05-31 23:51:02 +02:00
|
|
|
ir_block *bb;
|
|
|
|
ir_insn *insn;
|
|
|
|
ir_ref i, n, j, *p;
|
2022-08-30 22:15:20 +02:00
|
|
|
uint32_t *rule, insn_flags;
|
2022-05-31 23:51:02 +02:00
|
|
|
ir_backend_data *data = ctx->data;
|
|
|
|
ir_regset available = 0;
|
2022-11-17 21:30:35 +01:00
|
|
|
ir_target_constraints constraints;
|
|
|
|
uint32_t def_flags;
|
2022-08-12 20:17:19 +02:00
|
|
|
ir_reg reg;
|
2022-05-31 23:51:02 +02:00
|
|
|
|
|
|
|
ctx->regs = ir_mem_malloc(sizeof(ir_regs) * ctx->insns_count);
|
|
|
|
memset(ctx->regs, IR_REG_NONE, sizeof(ir_regs) * ctx->insns_count);
|
|
|
|
|
2023-03-21 11:45:37 +01:00
|
|
|
/* vregs + tmp + fixed + SRATCH + ALL */
|
|
|
|
ctx->live_intervals = ir_mem_calloc(ctx->vregs_count + 1 + IR_REG_NUM + 2, sizeof(ir_live_interval*));
|
2023-04-13 12:47:16 +02:00
|
|
|
|
|
|
|
if (!ctx->arena) {
|
|
|
|
ctx->arena = ir_arena_create(16 * 1024);
|
|
|
|
}
|
|
|
|
|
2022-05-31 23:51:02 +02:00
|
|
|
for (b = 1, bb = ctx->cfg_blocks + b; b <= ctx->cfg_blocks_count; b++, bb++) {
|
2022-11-29 18:02:07 +01:00
|
|
|
IR_ASSERT(!(bb->flags & IR_BB_UNREACHABLE));
|
2022-05-31 23:51:02 +02:00
|
|
|
for (i = bb->start, insn = ctx->ir_base + i, rule = ctx->rules + i; i <= bb->end;) {
|
|
|
|
switch (ctx->rules ? *rule : insn->op) {
|
|
|
|
case IR_START:
|
|
|
|
case IR_BEGIN:
|
|
|
|
case IR_END:
|
|
|
|
case IR_IF_TRUE:
|
|
|
|
case IR_IF_FALSE:
|
|
|
|
case IR_CASE_VAL:
|
|
|
|
case IR_CASE_DEFAULT:
|
|
|
|
case IR_MERGE:
|
|
|
|
case IR_LOOP_BEGIN:
|
|
|
|
case IR_LOOP_END:
|
|
|
|
break;
|
|
|
|
default:
|
2022-11-17 21:30:35 +01:00
|
|
|
def_flags = ir_get_target_constraints(ctx, i, &constraints);
|
2022-06-28 15:36:06 +02:00
|
|
|
if (ctx->rules
|
|
|
|
&& *rule != IR_CMP_AND_BRANCH_INT
|
|
|
|
&& *rule != IR_CMP_AND_BRANCH_FP
|
|
|
|
&& *rule != IR_GUARD_CMP_INT
|
|
|
|
&& *rule != IR_GUARD_CMP_FP) {
|
2022-05-31 23:51:02 +02:00
|
|
|
available = IR_REGSET_SCRATCH;
|
|
|
|
}
|
|
|
|
if (ctx->vregs[i]) {
|
2022-11-17 21:30:35 +01:00
|
|
|
reg = constraints.def_reg;
|
2022-05-31 23:51:02 +02:00
|
|
|
if (reg != IR_REG_NONE && IR_REGSET_IN(available, reg)) {
|
|
|
|
IR_REGSET_EXCL(available, reg);
|
|
|
|
ctx->regs[i][0] = reg | IR_REG_SPILL_STORE;
|
|
|
|
} else if (def_flags & IR_USE_MUST_BE_IN_REG) {
|
|
|
|
if (insn->op == IR_VLOAD
|
|
|
|
&& ctx->live_intervals[ctx->vregs[i]]
|
|
|
|
&& ctx->live_intervals[ctx->vregs[i]]->stack_spill_pos != -1) {
|
|
|
|
/* pass */
|
|
|
|
} else if (insn->op != IR_PARAM) {
|
|
|
|
reg = ir_get_free_reg(insn->type, available);
|
|
|
|
IR_REGSET_EXCL(available, reg);
|
|
|
|
ctx->regs[i][0] = reg | IR_REG_SPILL_STORE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!ctx->live_intervals[ctx->vregs[i]]) {
|
2023-04-13 12:47:16 +02:00
|
|
|
ir_live_interval *ival = ir_arena_alloc(&ctx->arena, sizeof(ir_live_interval));
|
|
|
|
memset(ival, 0, sizeof(ir_live_interval));
|
2022-05-31 23:51:02 +02:00
|
|
|
ctx->live_intervals[ctx->vregs[i]] = ival;
|
|
|
|
ival->type = insn->type;
|
|
|
|
ival->reg = IR_REG_NONE;
|
|
|
|
ival->vreg = ctx->vregs[i];
|
|
|
|
ival->stack_spill_pos = -1;
|
|
|
|
if (insn->op == IR_PARAM && reg == IR_REG_NONE) {
|
|
|
|
ival->flags |= IR_LIVE_INTERVAL_MEM_PARAM;
|
|
|
|
} else {
|
|
|
|
ival->stack_spill_pos = ir_allocate_spill_slot(ctx, ival->type, &data->ra_data);
|
|
|
|
}
|
|
|
|
} else if (insn->op == IR_PARAM) {
|
|
|
|
IR_ASSERT(0 && "unexpected PARAM");
|
|
|
|
return;
|
|
|
|
}
|
2023-05-18 20:00:57 +02:00
|
|
|
} else if (insn->op == IR_VAR) {
|
|
|
|
ir_use_list *use_list = &ctx->use_lists[i];
|
|
|
|
ir_ref n = use_list->count;
|
|
|
|
|
|
|
|
if (n > 0) {
|
|
|
|
int32_t stack_spill_pos = insn->op3 = ir_allocate_spill_slot(ctx, insn->type, &data->ra_data);
|
|
|
|
ir_ref i, *p, use;
|
|
|
|
ir_insn *use_insn;
|
|
|
|
|
|
|
|
for (i = 0, p = &ctx->use_edges[use_list->refs]; i < n; i++, p++) {
|
|
|
|
use = *p;
|
|
|
|
use_insn = &ctx->ir_base[use];
|
|
|
|
if (use_insn->op == IR_VLOAD) {
|
|
|
|
if (ctx->vregs[use]
|
|
|
|
&& !ctx->live_intervals[ctx->vregs[use]]) {
|
|
|
|
ir_live_interval *ival = ir_arena_alloc(&ctx->arena, sizeof(ir_live_interval));
|
|
|
|
memset(ival, 0, sizeof(ir_live_interval));
|
|
|
|
ctx->live_intervals[ctx->vregs[use]] = ival;
|
|
|
|
ival->type = insn->type;
|
|
|
|
ival->reg = IR_REG_NONE;
|
|
|
|
ival->vreg = ctx->vregs[use];
|
|
|
|
ival->stack_spill_pos = stack_spill_pos;
|
|
|
|
}
|
|
|
|
} else if (use_insn->op == IR_VSTORE) {
|
|
|
|
if (!IR_IS_CONST_REF(use_insn->op3)
|
|
|
|
&& ctx->vregs[use_insn->op3]
|
|
|
|
&& !ctx->live_intervals[ctx->vregs[use_insn->op3]]) {
|
|
|
|
ir_live_interval *ival = ir_arena_alloc(&ctx->arena, sizeof(ir_live_interval));
|
|
|
|
memset(ival, 0, sizeof(ir_live_interval));
|
|
|
|
ctx->live_intervals[ctx->vregs[use_insn->op3]] = ival;
|
|
|
|
ival->type = insn->type;
|
|
|
|
ival->reg = IR_REG_NONE;
|
2023-05-29 14:52:17 +02:00
|
|
|
ival->vreg = ctx->vregs[use_insn->op3];
|
2023-05-18 20:00:57 +02:00
|
|
|
ival->stack_spill_pos = stack_spill_pos;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-05-31 23:51:02 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
insn_flags = ir_op_flags[insn->op];
|
2022-11-17 21:30:35 +01:00
|
|
|
n = constraints.tmps_count;
|
|
|
|
if (n) {
|
|
|
|
do {
|
|
|
|
n--;
|
|
|
|
if (constraints.tmp_regs[n].type) {
|
|
|
|
ir_reg reg = ir_get_free_reg(constraints.tmp_regs[n].type, available);
|
|
|
|
IR_REGSET_EXCL(available, reg);
|
|
|
|
ctx->regs[i][constraints.tmp_regs[n].num] = reg;
|
2023-03-21 11:45:37 +01:00
|
|
|
} else if (constraints.tmp_regs[n].reg == IR_REG_SCRATCH) {
|
2022-11-17 21:30:35 +01:00
|
|
|
available = IR_REGSET_DIFFERENCE(available, IR_REGSET_SCRATCH);
|
|
|
|
} else {
|
|
|
|
IR_REGSET_EXCL(available, constraints.tmp_regs[n].reg);
|
|
|
|
}
|
|
|
|
} while (n);
|
2022-05-31 23:51:02 +02:00
|
|
|
}
|
2023-07-06 00:15:08 +02:00
|
|
|
n = insn->inputs_count;
|
2022-05-31 23:51:02 +02:00
|
|
|
for (j = 1, p = insn->ops + 1; j <= n; j++, p++) {
|
|
|
|
ir_ref input = *p;
|
|
|
|
if (IR_OPND_KIND(insn_flags, j) == IR_OPND_DATA && input > 0 && ctx->vregs[input]) {
|
|
|
|
if ((def_flags & IR_DEF_REUSES_OP1_REG) && j == 1) {
|
2023-06-14 19:23:32 +02:00
|
|
|
ir_reg reg = IR_REG_NUM(ctx->regs[i][0]);
|
2022-05-31 23:51:02 +02:00
|
|
|
ctx->regs[i][1] = reg | IR_REG_SPILL_LOAD;
|
|
|
|
} else {
|
2022-11-17 21:30:35 +01:00
|
|
|
uint8_t use_flags = IR_USE_FLAGS(def_flags, j);
|
|
|
|
ir_reg reg = (j < constraints.hints_count) ? constraints.hints[j] : IR_REG_NONE;
|
2022-05-31 23:51:02 +02:00
|
|
|
|
|
|
|
if (reg != IR_REG_NONE && IR_REGSET_IN(available, reg)) {
|
|
|
|
IR_REGSET_EXCL(available, reg);
|
|
|
|
ctx->regs[i][j] = reg | IR_REG_SPILL_LOAD;
|
2022-06-06 14:27:25 +02:00
|
|
|
} else if (j > 1 && input == insn->op1 && ctx->regs[i][1] != IR_REG_NONE) {
|
|
|
|
ctx->regs[i][j] = ctx->regs[i][1];
|
2022-08-12 20:17:19 +02:00
|
|
|
} else if (use_flags & IR_USE_MUST_BE_IN_REG) {
|
2022-05-31 23:51:02 +02:00
|
|
|
reg = ir_get_free_reg(ctx->ir_base[input].type, available);
|
|
|
|
IR_REGSET_EXCL(available, reg);
|
|
|
|
ctx->regs[i][j] = reg | IR_REG_SPILL_LOAD;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
2023-04-21 12:40:55 +02:00
|
|
|
n = ir_insn_len(insn);
|
2022-05-31 23:51:02 +02:00
|
|
|
i += n;
|
|
|
|
insn += n;
|
|
|
|
rule += n;
|
|
|
|
}
|
|
|
|
if (bb->flags & IR_BB_DESSA_MOVES) {
|
|
|
|
data->dessa_from_block = b;
|
|
|
|
ir_gen_dessa_moves(ctx, b, ir_fix_dessa_tmps);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-06-21 18:04:22 +02:00
|
|
|
ctx->used_preserved_regs = ctx->fixed_save_regset;
|
|
|
|
ctx->flags |= IR_NO_STACK_COMBINE;
|
|
|
|
ir_fix_stack_frame(ctx);
|
2022-05-31 23:51:02 +02:00
|
|
|
}
|
|
|
|
|
2023-06-21 18:04:22 +02:00
|
|
|
static void ir_preallocate_call_stack(ir_ctx *ctx)
|
2022-05-31 23:51:02 +02:00
|
|
|
{
|
|
|
|
int call_stack_size, peak_call_stack_size = 0;
|
|
|
|
ir_ref i, n;
|
|
|
|
ir_insn *insn;
|
|
|
|
|
|
|
|
for (i = 1, insn = ctx->ir_base + 1; i < ctx->insns_count;) {
|
|
|
|
if (insn->op == IR_CALL) {
|
2022-08-29 23:42:06 +02:00
|
|
|
call_stack_size = ir_call_used_stack(ctx, insn);
|
2022-05-31 23:51:02 +02:00
|
|
|
if (call_stack_size > peak_call_stack_size) {
|
|
|
|
peak_call_stack_size = call_stack_size;
|
|
|
|
}
|
|
|
|
}
|
2023-04-21 12:40:55 +02:00
|
|
|
n = ir_insn_len(insn);
|
2022-05-31 23:51:02 +02:00
|
|
|
i += n;
|
|
|
|
insn += n;
|
|
|
|
}
|
2023-01-31 14:13:15 +01:00
|
|
|
if (peak_call_stack_size) {
|
2023-06-15 18:28:54 +02:00
|
|
|
ctx->call_stack_size = peak_call_stack_size;
|
2023-01-31 14:13:15 +01:00
|
|
|
ctx->flags |= IR_PREALLOCATED_STACK;
|
|
|
|
}
|
2022-05-31 23:51:02 +02:00
|
|
|
}
|
|
|
|
|
2023-06-21 18:04:22 +02:00
|
|
|
void ir_fix_stack_frame(ir_ctx *ctx)
|
2022-05-31 23:51:02 +02:00
|
|
|
{
|
|
|
|
uint32_t additional_size = 0;
|
2022-06-23 21:39:00 +02:00
|
|
|
|
2023-11-23 17:38:33 +01:00
|
|
|
ctx->locals_area_size = ctx->stack_frame_size;
|
|
|
|
|
2023-06-21 18:04:22 +02:00
|
|
|
if (ctx->used_preserved_regs) {
|
|
|
|
ir_regset used_preserved_regs = (ir_regset)ctx->used_preserved_regs;
|
2022-06-23 21:39:00 +02:00
|
|
|
ir_reg reg;
|
|
|
|
(void) reg;
|
|
|
|
|
2023-06-21 18:04:22 +02:00
|
|
|
IR_REGSET_FOREACH(used_preserved_regs, reg) {
|
2022-06-23 21:39:00 +02:00
|
|
|
additional_size += sizeof(void*);
|
|
|
|
} IR_REGSET_FOREACH_END();
|
|
|
|
}
|
2022-05-31 23:51:02 +02:00
|
|
|
|
2023-11-23 17:38:33 +01:00
|
|
|
if ((ctx->flags & IR_VARARG_FUNC) && (ctx->flags2 & IR_HAS_VA_START)) {
|
|
|
|
if ((ctx->flags2 & (IR_HAS_VA_ARG_GP|IR_HAS_VA_COPY)) && ctx->gp_reg_params < IR_REG_INT_ARGS) {
|
|
|
|
additional_size += sizeof(void*) * IR_REG_INT_ARGS;
|
|
|
|
}
|
|
|
|
if ((ctx->flags2 & (IR_HAS_VA_ARG_FP|IR_HAS_VA_COPY)) && ctx->fp_reg_params < IR_REG_FP_ARGS) {
|
|
|
|
additional_size += 16 * IR_REG_FP_ARGS;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-06-15 18:28:54 +02:00
|
|
|
ctx->stack_frame_size = IR_ALIGNED_SIZE(ctx->stack_frame_size, sizeof(void*));
|
|
|
|
ctx->stack_frame_size += additional_size;
|
2023-06-21 18:04:22 +02:00
|
|
|
ctx->stack_frame_alignment = 0;
|
|
|
|
ctx->call_stack_size = 0;
|
2022-05-31 23:51:02 +02:00
|
|
|
|
2023-11-17 12:24:33 +01:00
|
|
|
if ((ctx->flags2 & IR_HAS_CALLS) && !(ctx->flags & IR_FUNCTION)) {
|
2023-06-15 18:28:54 +02:00
|
|
|
while (IR_ALIGNED_SIZE(ctx->stack_frame_size, 16) != ctx->stack_frame_size) {
|
|
|
|
ctx->stack_frame_size += sizeof(void*);
|
|
|
|
ctx->stack_frame_alignment += sizeof(void*);
|
2022-06-28 11:24:50 +02:00
|
|
|
}
|
2023-11-17 12:24:33 +01:00
|
|
|
} else if (ctx->flags2 & IR_HAS_CALLS) {
|
2022-06-02 17:34:47 +02:00
|
|
|
ctx->flags |= IR_USE_FRAME_POINTER;
|
2022-05-31 23:51:02 +02:00
|
|
|
/* Stack must be 16 byte aligned */
|
2022-06-23 21:39:00 +02:00
|
|
|
if (!(ctx->flags & IR_FUNCTION)) {
|
2023-06-15 18:28:54 +02:00
|
|
|
while (IR_ALIGNED_SIZE(ctx->stack_frame_size, 16) != ctx->stack_frame_size) {
|
|
|
|
ctx->stack_frame_size += sizeof(void*);
|
|
|
|
ctx->stack_frame_alignment += sizeof(void*);
|
2022-06-23 21:39:00 +02:00
|
|
|
}
|
|
|
|
} else if (ctx->flags & IR_USE_FRAME_POINTER) {
|
2023-06-15 18:28:54 +02:00
|
|
|
while (IR_ALIGNED_SIZE(ctx->stack_frame_size + sizeof(void*) * 2, 16) != ctx->stack_frame_size + sizeof(void*) * 2) {
|
|
|
|
ctx->stack_frame_size += sizeof(void*);
|
|
|
|
ctx->stack_frame_alignment += sizeof(void*);
|
2022-05-31 23:51:02 +02:00
|
|
|
}
|
|
|
|
} else {
|
2022-06-23 21:39:00 +02:00
|
|
|
if (!(ctx->flags & IR_NO_STACK_COMBINE)) {
|
2023-06-21 18:04:22 +02:00
|
|
|
ir_preallocate_call_stack(ctx);
|
2022-06-23 21:39:00 +02:00
|
|
|
}
|
2023-06-15 18:28:54 +02:00
|
|
|
while (IR_ALIGNED_SIZE(ctx->stack_frame_size + ctx->call_stack_size, 16) !=
|
|
|
|
ctx->stack_frame_size + ctx->call_stack_size) {
|
|
|
|
ctx->stack_frame_size += sizeof(void*);
|
|
|
|
ctx->stack_frame_alignment += sizeof(void*);
|
2022-05-31 23:51:02 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
ir_fix_param_spills(ctx);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void* dasm_labels[ir_lb_MAX];
|
|
|
|
|
2023-06-01 23:49:32 +02:00
|
|
|
/* Veneers support (TODO: avid global variable usage) */
|
|
|
|
static ir_ctx *ir_current_ctx;
|
|
|
|
|
2022-06-10 10:30:19 +02:00
|
|
|
void *ir_emit_code(ir_ctx *ctx, size_t *size_ptr)
|
2022-05-31 23:51:02 +02:00
|
|
|
{
|
2022-11-08 21:09:35 +01:00
|
|
|
uint32_t b, n, target;
|
2022-05-31 23:51:02 +02:00
|
|
|
ir_block *bb;
|
|
|
|
ir_ref i;
|
|
|
|
ir_insn *insn;
|
|
|
|
uint32_t *rule;
|
|
|
|
ir_backend_data data;
|
|
|
|
dasm_State **Dst;
|
|
|
|
int ret;
|
|
|
|
void *entry;
|
2022-06-10 10:30:19 +02:00
|
|
|
size_t size;
|
2022-05-31 23:51:02 +02:00
|
|
|
|
|
|
|
data.ra_data.unused_slot_4 = 0;
|
|
|
|
data.ra_data.unused_slot_2 = 0;
|
|
|
|
data.ra_data.unused_slot_1 = 0;
|
2023-08-02 12:20:13 +02:00
|
|
|
data.ra_data.handled = NULL;
|
2022-05-31 23:51:02 +02:00
|
|
|
data.rodata_label = 0;
|
|
|
|
data.jmp_table_label = 0;
|
2023-05-18 20:00:57 +02:00
|
|
|
ctx->data = &data;
|
2022-05-31 23:51:02 +02:00
|
|
|
|
|
|
|
if (!ctx->live_intervals) {
|
2023-06-21 18:04:22 +02:00
|
|
|
ctx->stack_frame_size = 0;
|
|
|
|
ctx->stack_frame_alignment = 0;
|
|
|
|
ctx->call_stack_size = 0;
|
|
|
|
ctx->used_preserved_regs = 0;
|
2022-05-31 23:51:02 +02:00
|
|
|
ir_allocate_unique_spill_slots(ctx);
|
|
|
|
}
|
|
|
|
|
2022-08-11 12:32:44 +02:00
|
|
|
if (ctx->fixed_stack_frame_size != -1) {
|
2023-02-17 13:52:26 +01:00
|
|
|
if (ctx->fixed_stack_red_zone) {
|
|
|
|
IR_ASSERT(ctx->fixed_stack_red_zone == ctx->fixed_stack_frame_size + ctx->fixed_call_stack_size);
|
|
|
|
}
|
2023-06-15 18:28:54 +02:00
|
|
|
if (ctx->stack_frame_size > ctx->fixed_stack_frame_size) {
|
2023-06-07 17:39:51 +02:00
|
|
|
// TODO: report error to caller
|
2023-06-16 01:14:02 +02:00
|
|
|
#ifdef IR_DEBUG_MESSAGES
|
2023-06-15 18:28:54 +02:00
|
|
|
fprintf(stderr, "IR Compilation Aborted: ctx->stack_frame_size > ctx->fixed_stack_frame_size at %s:%d\n",
|
2023-06-07 17:39:51 +02:00
|
|
|
__FILE__, __LINE__);
|
2023-06-16 01:14:02 +02:00
|
|
|
#endif
|
2023-06-21 18:04:22 +02:00
|
|
|
ctx->data = NULL;
|
2023-09-14 19:15:30 +02:00
|
|
|
ctx->status = IR_ERROR_FIXED_STACK_FRAME_OVERFLOW;
|
2023-06-07 17:39:51 +02:00
|
|
|
return NULL;
|
|
|
|
}
|
2023-06-15 18:28:54 +02:00
|
|
|
ctx->stack_frame_size = ctx->fixed_stack_frame_size;
|
|
|
|
ctx->call_stack_size = ctx->fixed_call_stack_size;
|
|
|
|
ctx->stack_frame_alignment = 0;
|
2022-08-11 12:32:44 +02:00
|
|
|
}
|
|
|
|
|
2022-05-31 23:51:02 +02:00
|
|
|
Dst = &data.dasm_state;
|
|
|
|
data.dasm_state = NULL;
|
|
|
|
dasm_init(&data.dasm_state, DASM_MAXSECTION);
|
|
|
|
dasm_setupglobal(&data.dasm_state, dasm_labels, ir_lb_MAX);
|
|
|
|
dasm_setup(&data.dasm_state, dasm_actions);
|
2023-12-11 13:06:52 +01:00
|
|
|
/* labels for each block + for each constant + rodata label + jmp_table label + for each entry + exit_table label */
|
|
|
|
dasm_growpc(&data.dasm_state, ctx->cfg_blocks_count + 1 + ctx->consts_count + 1 + 1 + 1 + ctx->entries_count + 1);
|
2023-11-30 19:10:41 +01:00
|
|
|
data.emit_constants = ir_bitset_malloc(ctx->consts_count);
|
2022-05-31 23:51:02 +02:00
|
|
|
|
2022-10-27 11:58:04 +02:00
|
|
|
if (!(ctx->flags & IR_SKIP_PROLOGUE)) {
|
|
|
|
ir_emit_prologue(ctx);
|
|
|
|
}
|
2022-05-31 23:51:02 +02:00
|
|
|
if (ctx->flags & IR_FUNCTION) {
|
|
|
|
ir_emit_load_params(ctx);
|
|
|
|
}
|
|
|
|
|
|
|
|
for (b = 1, bb = ctx->cfg_blocks + b; b <= ctx->cfg_blocks_count; b++, bb++) {
|
2022-11-29 18:02:07 +01:00
|
|
|
IR_ASSERT(!(bb->flags & IR_BB_UNREACHABLE));
|
2023-03-21 11:45:37 +01:00
|
|
|
if ((bb->flags & (IR_BB_START|IR_BB_ENTRY|IR_BB_EMPTY)) == IR_BB_EMPTY) {
|
2022-08-30 22:15:20 +02:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|=>b:
|
2022-11-18 13:07:19 +01:00
|
|
|
|
|
|
|
i = bb->start;
|
|
|
|
insn = ctx->ir_base + i;
|
|
|
|
if (bb->flags & IR_BB_ENTRY) {
|
2023-03-17 07:02:37 +01:00
|
|
|
uint32_t label = ctx->cfg_blocks_count + ctx->consts_count + 4 + insn->op3;
|
2022-11-18 13:07:19 +01:00
|
|
|
|
|
|
|
|=>label:
|
|
|
|
ir_emit_prologue(ctx);
|
2023-03-17 07:02:37 +01:00
|
|
|
ctx->entries[insn->op3] = i;
|
2022-11-18 13:07:19 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/* skip first instruction */
|
2023-04-21 12:40:55 +02:00
|
|
|
n = ir_insn_len(insn);
|
2022-11-18 13:07:19 +01:00
|
|
|
i += n;
|
|
|
|
insn += n;
|
|
|
|
rule = ctx->rules + i;
|
|
|
|
|
|
|
|
while (i <= bb->end) {
|
2023-04-05 18:20:43 +02:00
|
|
|
if (!((*rule) & (IR_FUSED|IR_SKIPPED)))
|
2022-05-31 23:51:02 +02:00
|
|
|
switch (*rule) {
|
|
|
|
case IR_VAR:
|
2022-11-17 21:30:35 +01:00
|
|
|
case IR_PARAM:
|
|
|
|
case IR_PI:
|
|
|
|
case IR_PHI:
|
|
|
|
case IR_SNAPSHOT:
|
2023-11-17 12:24:33 +01:00
|
|
|
case IR_VA_END:
|
2022-05-31 23:51:02 +02:00
|
|
|
break;
|
|
|
|
case IR_MUL_PWR2:
|
|
|
|
case IR_DIV_PWR2:
|
|
|
|
case IR_MOD_PWR2:
|
|
|
|
ir_emit_mul_div_mod_pwr2(ctx, i, insn);
|
|
|
|
break;
|
|
|
|
case IR_SHIFT:
|
|
|
|
ir_emit_shift(ctx, i, insn);
|
|
|
|
break;
|
|
|
|
case IR_SHIFT_CONST:
|
|
|
|
ir_emit_shift_const(ctx, i, insn);
|
|
|
|
break;
|
2023-11-16 17:28:12 +01:00
|
|
|
case IR_CTPOP:
|
|
|
|
ir_emit_ctpop(ctx, i, insn);
|
|
|
|
break;
|
2022-05-31 23:51:02 +02:00
|
|
|
case IR_OP_INT:
|
|
|
|
ir_emit_op_int(ctx, i, insn);
|
|
|
|
break;
|
|
|
|
case IR_OP_FP:
|
|
|
|
ir_emit_op_fp(ctx, i, insn);
|
|
|
|
break;
|
|
|
|
case IR_BINOP_INT:
|
|
|
|
ir_emit_binop_int(ctx, i, insn);
|
|
|
|
break;
|
|
|
|
case IR_BINOP_FP:
|
|
|
|
ir_emit_binop_fp(ctx, i, insn);
|
|
|
|
break;
|
|
|
|
case IR_CMP_INT:
|
|
|
|
ir_emit_cmp_int(ctx, i, insn);
|
|
|
|
break;
|
|
|
|
case IR_CMP_FP:
|
|
|
|
ir_emit_cmp_fp(ctx, i, insn);
|
|
|
|
break;
|
|
|
|
case IR_SEXT:
|
|
|
|
ir_emit_sext(ctx, i, insn);
|
|
|
|
break;
|
|
|
|
case IR_ZEXT:
|
|
|
|
ir_emit_zext(ctx, i, insn);
|
|
|
|
break;
|
|
|
|
case IR_TRUNC:
|
|
|
|
ir_emit_trunc(ctx, i, insn);
|
|
|
|
break;
|
|
|
|
case IR_BITCAST:
|
2023-11-30 19:10:41 +01:00
|
|
|
case IR_PROTO:
|
2022-05-31 23:51:02 +02:00
|
|
|
ir_emit_bitcast(ctx, i, insn);
|
|
|
|
break;
|
|
|
|
case IR_INT2FP:
|
|
|
|
ir_emit_int2fp(ctx, i, insn);
|
|
|
|
break;
|
|
|
|
case IR_FP2INT:
|
|
|
|
ir_emit_fp2int(ctx, i, insn);
|
|
|
|
break;
|
|
|
|
case IR_FP2FP:
|
|
|
|
ir_emit_fp2fp(ctx, i, insn);
|
|
|
|
break;
|
|
|
|
case IR_COPY_INT:
|
|
|
|
ir_emit_copy_int(ctx, i, insn);
|
|
|
|
break;
|
|
|
|
case IR_COPY_FP:
|
|
|
|
ir_emit_copy_fp(ctx, i, insn);
|
|
|
|
break;
|
|
|
|
case IR_CMP_AND_BRANCH_INT:
|
|
|
|
ir_emit_cmp_and_branch_int(ctx, b, i, insn);
|
|
|
|
break;
|
|
|
|
case IR_CMP_AND_BRANCH_FP:
|
|
|
|
ir_emit_cmp_and_branch_fp(ctx, b, i, insn);
|
|
|
|
break;
|
2022-06-28 15:36:06 +02:00
|
|
|
case IR_GUARD_CMP_INT:
|
|
|
|
ir_emit_guard_cmp_int(ctx, b, i, insn);
|
|
|
|
break;
|
2022-09-26 19:47:29 +02:00
|
|
|
case IR_GUARD_CMP_FP:
|
|
|
|
ir_emit_guard_cmp_fp(ctx, b, i, insn);
|
|
|
|
break;
|
2022-05-31 23:51:02 +02:00
|
|
|
case IR_IF_INT:
|
|
|
|
ir_emit_if_int(ctx, b, i, insn);
|
|
|
|
break;
|
2023-10-13 19:50:23 +02:00
|
|
|
case IR_COND:
|
|
|
|
ir_emit_cond(ctx, i, insn);
|
|
|
|
break;
|
2022-05-31 23:51:02 +02:00
|
|
|
case IR_SWITCH:
|
|
|
|
ir_emit_switch(ctx, b, i, insn);
|
|
|
|
break;
|
|
|
|
case IR_MIN_MAX_INT:
|
|
|
|
ir_emit_min_max_int(ctx, i, insn);
|
|
|
|
break;
|
|
|
|
case IR_OVERFLOW:
|
|
|
|
ir_emit_overflow(ctx, i, insn);
|
|
|
|
break;
|
|
|
|
case IR_OVERFLOW_AND_BRANCH:
|
|
|
|
ir_emit_overflow_and_branch(ctx, b, i, insn);
|
|
|
|
break;
|
|
|
|
case IR_END:
|
|
|
|
case IR_LOOP_END:
|
2023-03-21 11:45:37 +01:00
|
|
|
if (bb->flags & IR_BB_OSR_ENTRY_LOADS) {
|
|
|
|
ir_emit_osr_entry_loads(ctx, b, bb);
|
|
|
|
}
|
2022-05-31 23:51:02 +02:00
|
|
|
if (bb->flags & IR_BB_DESSA_MOVES) {
|
2023-05-17 21:37:45 +02:00
|
|
|
ir_emit_dessa_moves(ctx, b, bb);
|
2022-05-31 23:51:02 +02:00
|
|
|
}
|
2023-03-17 07:02:37 +01:00
|
|
|
do {
|
|
|
|
ir_ref succ = ctx->cfg_edges[bb->successors];
|
|
|
|
|
|
|
|
if (UNEXPECTED(bb->successors_count == 2)) {
|
|
|
|
if (ctx->cfg_blocks[succ].flags & IR_BB_ENTRY) {
|
|
|
|
succ = ctx->cfg_edges[bb->successors + 1];
|
|
|
|
} else {
|
|
|
|
IR_ASSERT(ctx->cfg_blocks[ctx->cfg_edges[bb->successors + 1]].flags & IR_BB_ENTRY);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
IR_ASSERT(bb->successors_count == 1);
|
|
|
|
}
|
|
|
|
target = ir_skip_empty_target_blocks(ctx, succ);
|
|
|
|
if (b == ctx->cfg_blocks_count || target != ir_skip_empty_next_blocks(ctx, b + 1)) {
|
|
|
|
| b =>target
|
|
|
|
}
|
|
|
|
} while (0);
|
2022-05-31 23:51:02 +02:00
|
|
|
break;
|
|
|
|
case IR_RETURN_VOID:
|
|
|
|
ir_emit_return_void(ctx);
|
|
|
|
break;
|
|
|
|
case IR_RETURN_INT:
|
|
|
|
ir_emit_return_int(ctx, i, insn);
|
|
|
|
break;
|
|
|
|
case IR_RETURN_FP:
|
|
|
|
ir_emit_return_fp(ctx, i, insn);
|
|
|
|
break;
|
|
|
|
case IR_CALL:
|
|
|
|
ir_emit_call(ctx, i, insn);
|
|
|
|
break;
|
|
|
|
case IR_TAILCALL:
|
|
|
|
ir_emit_tailcall(ctx, i, insn);
|
|
|
|
break;
|
|
|
|
case IR_IJMP:
|
|
|
|
ir_emit_ijmp(ctx, i, insn);
|
|
|
|
break;
|
2022-06-28 15:57:35 +02:00
|
|
|
case IR_REG_BINOP_INT:
|
|
|
|
ir_emit_reg_binop_int(ctx, i, insn);
|
|
|
|
break;
|
2022-05-31 23:51:02 +02:00
|
|
|
case IR_VADDR:
|
|
|
|
ir_emit_vaddr(ctx, i, insn);
|
|
|
|
break;
|
2022-06-06 17:10:41 +02:00
|
|
|
case IR_VLOAD:
|
|
|
|
ir_emit_vload(ctx, i, insn);
|
2022-05-31 23:51:02 +02:00
|
|
|
break;
|
2022-06-06 17:10:41 +02:00
|
|
|
case IR_VSTORE:
|
|
|
|
ir_emit_vstore(ctx, i, insn);
|
2022-05-31 23:51:02 +02:00
|
|
|
break;
|
2022-12-06 22:02:02 +01:00
|
|
|
case IR_RLOAD:
|
|
|
|
ir_emit_rload(ctx, i, insn);
|
|
|
|
break;
|
2022-06-09 23:16:29 +02:00
|
|
|
case IR_RSTORE:
|
|
|
|
ir_emit_rstore(ctx, i, insn);
|
|
|
|
break;
|
2022-05-31 23:51:02 +02:00
|
|
|
case IR_LOAD_INT:
|
|
|
|
ir_emit_load_int(ctx, i, insn);
|
|
|
|
break;
|
|
|
|
case IR_LOAD_FP:
|
|
|
|
ir_emit_load_fp(ctx, i, insn);
|
|
|
|
break;
|
|
|
|
case IR_STORE_INT:
|
|
|
|
ir_emit_store_int(ctx, i, insn);
|
|
|
|
break;
|
|
|
|
case IR_STORE_FP:
|
|
|
|
ir_emit_store_fp(ctx, i, insn);
|
|
|
|
break;
|
|
|
|
case IR_ALLOCA:
|
|
|
|
ir_emit_alloca(ctx, i, insn);
|
|
|
|
break;
|
2023-11-17 12:24:33 +01:00
|
|
|
case IR_VA_START:
|
|
|
|
ir_emit_va_start(ctx, i, insn);
|
|
|
|
break;
|
|
|
|
case IR_VA_COPY:
|
|
|
|
ir_emit_va_copy(ctx, i, insn);
|
|
|
|
break;
|
|
|
|
case IR_VA_ARG:
|
|
|
|
ir_emit_va_arg(ctx, i, insn);
|
|
|
|
break;
|
2022-12-07 11:09:00 +01:00
|
|
|
case IR_AFREE:
|
|
|
|
ir_emit_afree(ctx, i, insn);
|
|
|
|
break;
|
2023-11-15 23:59:26 +01:00
|
|
|
case IR_FRAME_ADDR:
|
|
|
|
ir_emit_frame_addr(ctx, i);
|
|
|
|
break;
|
2022-09-29 00:25:42 +02:00
|
|
|
case IR_EXITCALL:
|
|
|
|
ir_emit_exitcall(ctx, i, insn);
|
|
|
|
break;
|
2022-09-02 12:54:31 +02:00
|
|
|
case IR_GUARD:
|
|
|
|
case IR_GUARD_NOT:
|
2022-06-28 00:43:59 +02:00
|
|
|
ir_emit_guard(ctx, i, insn);
|
|
|
|
break;
|
2022-09-26 19:47:29 +02:00
|
|
|
case IR_GUARD_OVERFLOW:
|
|
|
|
ir_emit_guard_overflow(ctx, i, insn);
|
|
|
|
break;
|
2022-06-28 00:43:59 +02:00
|
|
|
case IR_TLS:
|
|
|
|
ir_emit_tls(ctx, i, insn);
|
|
|
|
break;
|
2023-11-22 09:43:33 +01:00
|
|
|
case IR_TRAP:
|
2023-11-23 17:38:33 +01:00
|
|
|
| brk
|
2023-11-22 09:43:33 +01:00
|
|
|
break;
|
2022-05-31 23:51:02 +02:00
|
|
|
default:
|
2023-10-03 07:34:02 +02:00
|
|
|
IR_ASSERT(0 && "NIY rule/instruction");
|
2023-11-30 19:10:41 +01:00
|
|
|
ir_mem_free(data.emit_constants);
|
2023-09-14 19:15:30 +02:00
|
|
|
dasm_free(&data.dasm_state);
|
|
|
|
ctx->data = NULL;
|
|
|
|
ctx->status = IR_ERROR_UNSUPPORTED_CODE_RULE;
|
|
|
|
return NULL;
|
2022-05-31 23:51:02 +02:00
|
|
|
}
|
2023-04-21 12:40:55 +02:00
|
|
|
n = ir_insn_len(insn);
|
2022-05-31 23:51:02 +02:00
|
|
|
i += n;
|
|
|
|
insn += n;
|
|
|
|
rule += n;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-06-01 23:49:32 +02:00
|
|
|
if (ctx->deoptimization_exits) {
|
2023-12-11 13:06:52 +01:00
|
|
|
uint32_t exit_table_label = ctx->cfg_blocks_count + 1 + ctx->consts_count + 1 + 1 + 1 + ctx->entries_count;
|
|
|
|
|
|
|
|
|=>exit_table_label:
|
2023-06-01 23:49:32 +02:00
|
|
|
for (i = 0; i < ctx->deoptimization_exits; i++) {
|
|
|
|
const void *exit_addr = ctx->get_exit_addr(i);
|
|
|
|
|
|
|
|
if (!exit_addr) {
|
2023-06-21 18:04:22 +02:00
|
|
|
ctx->data = NULL;
|
2023-06-01 23:49:32 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
| b &exit_addr
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-05-31 23:51:02 +02:00
|
|
|
if (data.rodata_label) {
|
|
|
|
|.rodata
|
|
|
|
}
|
2023-11-30 19:10:41 +01:00
|
|
|
IR_BITSET_FOREACH(data.emit_constants, ir_bitset_len(ctx->consts_count), i) {
|
|
|
|
insn = &ctx->ir_base[-i];
|
|
|
|
if (IR_IS_TYPE_FP(insn->type)) {
|
|
|
|
int label = ctx->cfg_blocks_count + i;
|
2022-05-31 23:51:02 +02:00
|
|
|
|
2023-11-30 19:10:41 +01:00
|
|
|
if (!data.rodata_label) {
|
|
|
|
data.rodata_label = ctx->cfg_blocks_count + ctx->consts_count + 2;
|
2022-05-31 23:51:02 +02:00
|
|
|
|
2023-11-30 19:10:41 +01:00
|
|
|
|.rodata
|
|
|
|
|=>data.rodata_label:
|
|
|
|
}
|
|
|
|
if (insn->type == IR_DOUBLE) {
|
2022-05-31 23:51:02 +02:00
|
|
|
|.align 8
|
|
|
|
|=>label:
|
2023-11-30 19:10:41 +01:00
|
|
|
|.long insn->val.u32, insn->val.u32_hi
|
|
|
|
} else {
|
|
|
|
IR_ASSERT(insn->type == IR_FLOAT);
|
|
|
|
|.align 4
|
|
|
|
|=>label:
|
|
|
|
|.long insn->val.u32
|
|
|
|
}
|
|
|
|
} else if (insn->op == IR_STR) {
|
|
|
|
int label = ctx->cfg_blocks_count + i;
|
|
|
|
const char *str = ir_get_str(ctx, insn->val.str);
|
|
|
|
int i = 0;
|
|
|
|
|
|
|
|
if (!data.rodata_label) {
|
|
|
|
data.rodata_label = ctx->cfg_blocks_count + ctx->consts_count + 2;
|
|
|
|
|
|
|
|
|.rodata
|
|
|
|
|=>data.rodata_label:
|
|
|
|
}
|
|
|
|
|.align 8
|
|
|
|
|=>label:
|
|
|
|
while (1) {
|
|
|
|
char c;
|
|
|
|
uint32_t w = 0;
|
|
|
|
int j;
|
|
|
|
|
|
|
|
for (j = 0; j < 4; j++) {
|
|
|
|
c = str[i];
|
2022-06-02 17:34:47 +02:00
|
|
|
if (!c) {
|
|
|
|
break;
|
2023-11-30 19:10:41 +01:00
|
|
|
} else if (c == '\\') {
|
|
|
|
if (str[i+1] == '\\') {
|
|
|
|
i++;
|
|
|
|
c = '\\';
|
|
|
|
} else if (str[i+1] == '\'') {
|
|
|
|
i++;
|
|
|
|
c = '\'';
|
|
|
|
} else if (str[i+1] == '"') {
|
|
|
|
i++;
|
|
|
|
c = '"';
|
|
|
|
} else if (str[i+1] == 'a') {
|
|
|
|
i++;
|
|
|
|
c = '\a';
|
|
|
|
} else if (str[i+1] == 'b') {
|
|
|
|
i++;
|
|
|
|
c = '\b';
|
|
|
|
} else if (str[i+1] == 'e') {
|
|
|
|
i++;
|
|
|
|
c = 27; /* '\e'; */
|
|
|
|
} else if (str[i+1] == 'f') {
|
|
|
|
i++;
|
|
|
|
c = '\f';
|
|
|
|
} else if (str[i+1] == 'n') {
|
|
|
|
i++;
|
|
|
|
c = '\n';
|
|
|
|
} else if (str[i+1] == 'r') {
|
|
|
|
i++;
|
|
|
|
c = '\r';
|
|
|
|
} else if (str[i+1] == 't') {
|
|
|
|
i++;
|
|
|
|
c = '\t';
|
|
|
|
} else if (str[i+1] == 'v') {
|
|
|
|
i++;
|
|
|
|
c = '\v';
|
|
|
|
} else if (str[i+1] == '?') {
|
|
|
|
i++;
|
|
|
|
c = 0x3f;
|
|
|
|
}
|
2022-05-31 23:51:02 +02:00
|
|
|
}
|
2023-11-30 19:10:41 +01:00
|
|
|
w |= c << (8 * j);
|
|
|
|
i++;
|
|
|
|
}
|
|
|
|
| .long w
|
|
|
|
if (!c) {
|
|
|
|
break;
|
2022-05-31 23:51:02 +02:00
|
|
|
}
|
|
|
|
}
|
2023-11-30 19:10:41 +01:00
|
|
|
|
|
|
|
} else {
|
|
|
|
IR_ASSERT(0);
|
2022-05-31 23:51:02 +02:00
|
|
|
}
|
2023-11-30 19:10:41 +01:00
|
|
|
} IR_BITSET_FOREACH_END();
|
2022-05-31 23:51:02 +02:00
|
|
|
if (data.rodata_label) {
|
|
|
|
|.code
|
|
|
|
}
|
2023-11-30 19:10:41 +01:00
|
|
|
ir_mem_free(data.emit_constants);
|
2022-05-31 23:51:02 +02:00
|
|
|
|
2023-09-14 19:15:30 +02:00
|
|
|
if (ctx->status) {
|
|
|
|
dasm_free(&data.dasm_state);
|
|
|
|
ctx->data = NULL;
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2022-06-10 10:30:19 +02:00
|
|
|
ret = dasm_link(&data.dasm_state, size_ptr);
|
2022-05-31 23:51:02 +02:00
|
|
|
if (ret != DASM_S_OK) {
|
2022-06-03 11:47:02 +02:00
|
|
|
IR_ASSERT(0);
|
2022-12-26 18:58:54 +01:00
|
|
|
dasm_free(&data.dasm_state);
|
2023-06-21 18:04:22 +02:00
|
|
|
ctx->data = NULL;
|
2023-09-14 19:15:30 +02:00
|
|
|
ctx->status = IR_ERROR_LINK;
|
2022-06-03 11:47:02 +02:00
|
|
|
return NULL;
|
2022-05-31 23:51:02 +02:00
|
|
|
}
|
2022-06-10 10:30:19 +02:00
|
|
|
size = *size_ptr;
|
2022-05-31 23:51:02 +02:00
|
|
|
|
2023-12-07 23:58:45 +01:00
|
|
|
if (ctx->code_buffer) {
|
|
|
|
entry = ctx->code_buffer->pos;
|
|
|
|
entry = (void*)IR_ALIGNED_SIZE(((size_t)(entry)), 16);
|
|
|
|
if (size > (size_t)((char*)ctx->code_buffer->end - (char*)entry)) {
|
2023-06-21 18:04:22 +02:00
|
|
|
ctx->data = NULL;
|
2023-09-14 19:15:30 +02:00
|
|
|
ctx->status = IR_ERROR_CODE_MEM_OVERFLOW;
|
2022-06-10 10:30:19 +02:00
|
|
|
return NULL;
|
|
|
|
}
|
2023-12-07 23:58:45 +01:00
|
|
|
ctx->code_buffer->pos = (char*)entry + size;
|
2022-06-10 10:30:19 +02:00
|
|
|
} else {
|
|
|
|
entry = ir_mem_mmap(size);
|
2023-09-14 19:15:30 +02:00
|
|
|
if (!entry) {
|
|
|
|
dasm_free(&data.dasm_state);
|
|
|
|
ctx->data = NULL;
|
|
|
|
ctx->status = IR_ERROR_CODE_MEM_OVERFLOW;
|
|
|
|
return NULL;
|
|
|
|
}
|
2022-06-17 12:22:26 +02:00
|
|
|
ir_mem_unprotect(entry, size);
|
2022-06-10 10:30:19 +02:00
|
|
|
}
|
2022-05-31 23:51:02 +02:00
|
|
|
|
2023-12-11 13:06:52 +01:00
|
|
|
if (ctx->deoptimization_exits) {
|
|
|
|
uint32_t exit_table_label = ctx->cfg_blocks_count + 1 + ctx->consts_count + 1 + 1 + 1 + ctx->entries_count;
|
|
|
|
|
|
|
|
ctx->deoptimization_exits_base = (const void*)((char*)entry + dasm_getpclabel(&data.dasm_state, exit_table_label));
|
|
|
|
}
|
|
|
|
|
2023-06-01 23:49:32 +02:00
|
|
|
ir_current_ctx = ctx;
|
2022-05-31 23:51:02 +02:00
|
|
|
ret = dasm_encode(&data.dasm_state, entry);
|
2022-06-28 00:43:59 +02:00
|
|
|
if (ret != DASM_S_OK) {
|
|
|
|
IR_ASSERT(0);
|
2022-12-26 18:58:54 +01:00
|
|
|
dasm_free(&data.dasm_state);
|
2023-12-07 23:58:45 +01:00
|
|
|
if (ctx->code_buffer) {
|
|
|
|
if (ctx->code_buffer->pos == (char*)entry + size) {
|
|
|
|
/* rollback */
|
|
|
|
ctx->code_buffer->pos = (char*)entry - size;
|
|
|
|
}
|
|
|
|
} else {
|
2022-06-28 00:43:59 +02:00
|
|
|
ir_mem_unmap(entry, size);
|
|
|
|
}
|
2023-06-21 18:04:22 +02:00
|
|
|
ctx->data = NULL;
|
2023-09-14 19:15:30 +02:00
|
|
|
ctx->status = IR_ERROR_ENCODE;
|
2022-06-28 00:43:59 +02:00
|
|
|
return NULL;
|
|
|
|
}
|
2022-05-31 23:51:02 +02:00
|
|
|
|
|
|
|
if (data.jmp_table_label) {
|
|
|
|
uint32_t offset = dasm_getpclabel(&data.dasm_state, data.jmp_table_label);
|
|
|
|
ctx->jmp_table_offset = offset;
|
|
|
|
} else {
|
|
|
|
ctx->jmp_table_offset = 0;
|
|
|
|
}
|
|
|
|
if (data.rodata_label) {
|
|
|
|
uint32_t offset = dasm_getpclabel(&data.dasm_state, data.rodata_label);
|
|
|
|
ctx->rodata_offset = offset;
|
|
|
|
} else {
|
|
|
|
ctx->rodata_offset = 0;
|
|
|
|
}
|
|
|
|
|
2023-03-17 07:02:37 +01:00
|
|
|
if (ctx->entries_count) {
|
|
|
|
/* For all entries */
|
|
|
|
i = ctx->entries_count;
|
|
|
|
do {
|
|
|
|
ir_insn *insn = &ctx->ir_base[ctx->entries[--i]];
|
|
|
|
uint32_t offset = dasm_getpclabel(&data.dasm_state, ctx->cfg_blocks_count + ctx->consts_count + 4 + insn->op3);
|
|
|
|
insn->op3 = offset;
|
|
|
|
} while (i != 0);
|
2022-06-16 22:49:27 +02:00
|
|
|
}
|
|
|
|
|
2022-05-31 23:51:02 +02:00
|
|
|
dasm_free(&data.dasm_state);
|
|
|
|
|
2023-12-07 23:58:45 +01:00
|
|
|
if (ctx->code_buffer) {
|
|
|
|
size = (char*)ctx->code_buffer->pos - (char*)entry;
|
|
|
|
}
|
2023-06-01 23:49:32 +02:00
|
|
|
|
2022-06-10 10:30:19 +02:00
|
|
|
ir_mem_flush(entry, size);
|
2022-06-17 12:22:26 +02:00
|
|
|
|
2023-12-07 23:58:45 +01:00
|
|
|
if (!ctx->code_buffer) {
|
2022-06-17 12:22:26 +02:00
|
|
|
ir_mem_protect(entry, size);
|
|
|
|
}
|
2022-05-31 23:51:02 +02:00
|
|
|
|
2023-06-21 18:04:22 +02:00
|
|
|
ctx->data = NULL;
|
2022-05-31 23:51:02 +02:00
|
|
|
return entry;
|
|
|
|
}
|
2022-10-26 14:46:59 +02:00
|
|
|
|
2023-12-07 23:58:45 +01:00
|
|
|
const void *ir_emit_exitgroup(uint32_t first_exit_point, uint32_t exit_points_per_group, const void *exit_addr, ir_code_buffer *code_buffer, size_t *size_ptr)
|
2022-10-26 14:46:59 +02:00
|
|
|
{
|
|
|
|
void *entry;
|
|
|
|
size_t size;
|
|
|
|
uint32_t i;
|
|
|
|
dasm_State **Dst, *dasm_state;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
IR_ASSERT(code_buffer);
|
2023-12-19 20:22:49 +01:00
|
|
|
IR_ASSERT(aarch64_may_use_b(code_buffer, exit_addr));
|
2022-10-26 14:46:59 +02:00
|
|
|
|
|
|
|
Dst = &dasm_state;
|
|
|
|
dasm_state = NULL;
|
|
|
|
dasm_init(&dasm_state, DASM_MAXSECTION);
|
|
|
|
dasm_setupglobal(&dasm_state, dasm_labels, ir_lb_MAX);
|
|
|
|
dasm_setup(&dasm_state, dasm_actions);
|
|
|
|
|
|
|
|
| bl >2
|
|
|
|
|1:
|
|
|
|
for (i = 1; i < exit_points_per_group; i++) {
|
|
|
|
| bl >2
|
|
|
|
}
|
|
|
|
|2:
|
|
|
|
| adr Rx(IR_REG_INT_TMP), <1
|
|
|
|
| sub Rx(IR_REG_INT_TMP), lr, Rx(IR_REG_INT_TMP)
|
|
|
|
| lsr Rx(IR_REG_INT_TMP), Rx(IR_REG_INT_TMP), #2
|
|
|
|
if (first_exit_point) {
|
|
|
|
| add Rx(IR_REG_INT_TMP), Rx(IR_REG_INT_TMP), #first_exit_point
|
|
|
|
}
|
|
|
|
| b &exit_addr
|
|
|
|
|
|
|
|
ret = dasm_link(&dasm_state, &size);
|
|
|
|
if (ret != DASM_S_OK) {
|
|
|
|
IR_ASSERT(0);
|
2022-12-26 12:44:57 +01:00
|
|
|
dasm_free(&dasm_state);
|
2022-10-26 14:46:59 +02:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2023-12-07 23:58:45 +01:00
|
|
|
entry = code_buffer->pos;
|
|
|
|
entry = (void*)IR_ALIGNED_SIZE(((size_t)(entry)), 16);
|
|
|
|
if (size > (size_t)((char*)code_buffer->end - (char*)entry)) {
|
|
|
|
return NULL;
|
2022-10-26 14:46:59 +02:00
|
|
|
}
|
2023-12-07 23:58:45 +01:00
|
|
|
code_buffer->pos = (char*)entry + size;
|
2022-10-26 14:46:59 +02:00
|
|
|
|
2023-06-01 23:49:32 +02:00
|
|
|
ir_current_ctx = NULL;
|
2022-10-26 14:46:59 +02:00
|
|
|
ret = dasm_encode(&dasm_state, entry);
|
|
|
|
if (ret != DASM_S_OK) {
|
|
|
|
IR_ASSERT(0);
|
2022-12-26 12:44:57 +01:00
|
|
|
dasm_free(&dasm_state);
|
2023-12-07 23:58:45 +01:00
|
|
|
if (code_buffer->pos == (char*)entry + size) {
|
|
|
|
/* rollback */
|
|
|
|
code_buffer->pos = (char*)entry - size;
|
2022-10-26 14:46:59 +02:00
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
dasm_free(&dasm_state);
|
|
|
|
|
|
|
|
ir_mem_flush(entry, size);
|
|
|
|
|
|
|
|
*size_ptr = size;
|
|
|
|
return entry;
|
|
|
|
}
|
2023-06-01 23:49:32 +02:00
|
|
|
|
|
|
|
static int ir_add_veneer(dasm_State *Dst, void *buffer, uint32_t ins, int *b, uint32_t *cp, ptrdiff_t offset)
|
|
|
|
{
|
|
|
|
ir_ctx *ctx = ir_current_ctx;
|
|
|
|
const void *addr, *veneer = NULL;
|
|
|
|
ptrdiff_t na;
|
|
|
|
int n, m;
|
|
|
|
|
|
|
|
IR_ASSERT(ctx && ctx->code_buffer);
|
|
|
|
|
|
|
|
if ((ins >> 16) == DASM_REL_A) {
|
|
|
|
addr = (void*)((((ptrdiff_t)(*(b-1))) << 32) | (unsigned int)(*(b-2)));
|
|
|
|
if (ctx->get_veneer) {
|
|
|
|
veneer = ctx->get_veneer(ctx, addr);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
IR_ASSERT(0 && "too long jmp distance");
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (veneer) {
|
|
|
|
na = (ptrdiff_t)veneer - (ptrdiff_t)cp + 4;
|
|
|
|
n = (int)na;
|
|
|
|
|
|
|
|
/* check if we can jump to veneer */
|
|
|
|
if ((ptrdiff_t)n != na) {
|
|
|
|
/* pass */
|
|
|
|
} else if (!(ins & 0xf800)) { /* B, BL */
|
|
|
|
if ((n & 3) == 0 && ((n+0x08000000) >> 28) == 0) {
|
|
|
|
return n;
|
|
|
|
}
|
|
|
|
} else if ((ins & 0x800)) { /* B.cond, CBZ, CBNZ, LDR* literal */
|
|
|
|
if ((n & 3) == 0 && ((n+0x00100000) >> 21) == 0) {
|
|
|
|
return n;
|
|
|
|
}
|
|
|
|
} else if ((ins & 0x3000) == 0x2000) { /* ADR */
|
|
|
|
/* pass */
|
|
|
|
} else if ((ins & 0x3000) == 0x3000) { /* ADRP */
|
|
|
|
/* pass */
|
|
|
|
} else if ((ins & 0x1000)) { /* TBZ, TBNZ */
|
|
|
|
if ((n & 3) == 0 && ((n+0x00008000) >> 16) == 0) {
|
|
|
|
return n;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-12-07 23:58:45 +01:00
|
|
|
veneer = ctx->code_buffer->pos;
|
|
|
|
if ((char*)ctx->code_buffer->end - (char*)veneer < 4 ) {
|
2023-06-01 23:49:32 +02:00
|
|
|
IR_ASSERT(0 && "too long jmp distance" && "jit buffer overflow");
|
|
|
|
return 0; /* jit_buffer_size overflow */
|
|
|
|
}
|
|
|
|
|
|
|
|
na = (ptrdiff_t)veneer - (ptrdiff_t)cp + 4;
|
|
|
|
n = (int)na;
|
|
|
|
|
|
|
|
/* check if we can jump to veneer */
|
|
|
|
if ((ptrdiff_t)n != na) {
|
|
|
|
IR_ASSERT(0 && "too long jmp distance");
|
|
|
|
return 0;
|
|
|
|
} else if (!(ins & 0xf800)) { /* B, BL */
|
|
|
|
if ((n & 3) != 0 || ((n+0x08000000) >> 28) != 0) {
|
|
|
|
IR_ASSERT(0 && "too long jmp distance");
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
} else if ((ins & 0x800)) { /* B.cond, CBZ, CBNZ, LDR* literal */
|
|
|
|
if ((n & 3) != 0 || ((n+0x00100000) >> 21) != 0) {
|
|
|
|
IR_ASSERT(0 && "too long jmp distance");
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
} else if ((ins & 0x3000) == 0x2000) { /* ADR */
|
|
|
|
IR_ASSERT(0 && "too long jmp distance");
|
|
|
|
return 0;
|
|
|
|
} else if ((ins & 0x3000) == 0x3000) { /* ADRP */
|
|
|
|
IR_ASSERT(0 && "too long jmp distance");
|
|
|
|
return 0;
|
|
|
|
} else if ((ins & 0x1000)) { /* TBZ, TBNZ */
|
|
|
|
if ((n & 3) != 0 || ((n+0x00008000) >> 16) != 0) {
|
|
|
|
IR_ASSERT(0 && "too long jmp distance");
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
} else if ((ins & 0x8000)) { /* absolute */
|
|
|
|
IR_ASSERT(0 && "too long jmp distance");
|
|
|
|
return 0;
|
|
|
|
} else {
|
|
|
|
IR_ASSERT(0 && "too long jmp distance");
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* check if we can use B to jump from veneer */
|
|
|
|
na = (ptrdiff_t)cp + offset - (ptrdiff_t)veneer - 4;
|
|
|
|
m = (int)na;
|
|
|
|
if ((ptrdiff_t)m != na) {
|
|
|
|
IR_ASSERT(0 && "too long jmp distance");
|
|
|
|
return 0;
|
|
|
|
} else if ((m & 3) != 0 || ((m+0x08000000) >> 28) != 0) {
|
|
|
|
IR_ASSERT(0 && "too long jmp distance");
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!ctx->set_veneer || !ctx->set_veneer(ctx, addr, veneer)) {
|
|
|
|
IR_ASSERT(0 && "too long jmp distance");
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* generate B instruction */
|
|
|
|
*(uint32_t*)veneer = 0x14000000 | ((m >> 2) & 0x03ffffff);
|
2023-12-07 23:58:45 +01:00
|
|
|
ctx->code_buffer->pos = (char*)ctx->code_buffer->pos + 4;
|
2023-06-01 23:49:32 +02:00
|
|
|
|
|
|
|
return n;
|
|
|
|
}
|
2023-12-19 20:22:49 +01:00
|
|
|
|
|
|
|
bool ir_needs_thunk(ir_code_buffer *code_buffer, void *addr)
|
|
|
|
{
|
|
|
|
return !aarch64_may_use_b(code_buffer, addr);
|
|
|
|
}
|
|
|
|
|
|
|
|
void *ir_emit_thunk(ir_code_buffer *code_buffer, void *addr, size_t *size_ptr)
|
|
|
|
{
|
|
|
|
void *entry;
|
|
|
|
size_t size;
|
|
|
|
dasm_State **Dst, *dasm_state;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
Dst = &dasm_state;
|
|
|
|
dasm_state = NULL;
|
|
|
|
dasm_init(&dasm_state, DASM_MAXSECTION);
|
|
|
|
dasm_setupglobal(&dasm_state, dasm_labels, ir_lb_MAX);
|
|
|
|
dasm_setup(&dasm_state, dasm_actions);
|
|
|
|
|
|
|
|
|.code
|
|
|
|
| movz Rx(IR_REG_INT_TMP), #((uint64_t)(addr) & 0xffff)
|
|
|
|
| movk Rx(IR_REG_INT_TMP), #(((uint64_t)(addr) >> 16) & 0xffff), lsl #16
|
|
|
|
| movk Rx(IR_REG_INT_TMP), #(((uint64_t)(addr) >> 32) & 0xffff), lsl #32
|
|
|
|
| movk Rx(IR_REG_INT_TMP), #(((uint64_t)(addr) >> 48) & 0xffff), lsl #48
|
|
|
|
| br Rx(IR_REG_INT_TMP)
|
|
|
|
|
|
|
|
ret = dasm_link(&dasm_state, &size);
|
|
|
|
if (ret != DASM_S_OK) {
|
|
|
|
IR_ASSERT(0);
|
|
|
|
dasm_free(&dasm_state);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2023-12-27 08:18:56 +01:00
|
|
|
entry = code_buffer->pos;
|
|
|
|
entry = (void*)IR_ALIGNED_SIZE(((size_t)(entry)), 4);
|
|
|
|
if (size > (size_t)((char*)code_buffer->end - (char*)entry)) {
|
2023-12-19 20:22:49 +01:00
|
|
|
dasm_free(&dasm_state);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
ret = dasm_encode(&dasm_state, entry);
|
|
|
|
if (ret != DASM_S_OK) {
|
|
|
|
dasm_free(&dasm_state);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
*size_ptr = size;
|
|
|
|
code_buffer->pos = (char*)code_buffer->pos + size;
|
|
|
|
|
|
|
|
dasm_free(&dasm_state);
|
|
|
|
ir_mem_flush(entry, size);
|
|
|
|
|
|
|
|
return entry;
|
2023-12-20 19:06:53 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void ir_fix_thunk(void *thunk_entry, void *addr)
|
|
|
|
{
|
|
|
|
uint32_t *code = thunk_entry;
|
|
|
|
IR_ASSERT((code[0] & 0xffe00000) == 0xd2800000
|
|
|
|
&& (code[1] & 0xffe00000) == 0xf2a00000
|
|
|
|
&& (code[2] & 0xffe00000) == 0xf2c00000
|
|
|
|
&& (code[3] & 0xffe00000) == 0xf2e00000
|
|
|
|
&& (code[4] & 0xfffffc1f) == 0xd61f0000);
|
|
|
|
|
|
|
|
code[0] = (code[0] & 0xffe0001f) | (uint32_t)((uint64_t)(addr) & 0xffff) << 5;
|
|
|
|
code[1] = (code[1] & 0xffe0001f) | (uint32_t)(((uint64_t)(addr) >> 16) & 0xffff) << 5;
|
|
|
|
code[2] = (code[2] & 0xffe0001f) | (uint32_t)(((uint64_t)(addr) >> 32) & 0xffff) << 5;
|
|
|
|
code[3] = (code[3] & 0xffe0001f) | (uint32_t)(((uint64_t)(addr) >> 48) & 0xffff) << 5;
|
|
|
|
|
|
|
|
ir_mem_flush(code, sizeof(uint32_t) * 4);
|
|
|
|
}
|