Files
scc/libs/mcode/include/x86/scc_x86_encode.h
zzy 41d060d7e7 feat(lir): 修改HIR到LIR转换以支持可变参数函数
- 移除SCC_LIR_LEA指令类型,改用SCC_LIR_LOAD_ADDR
- 在scc_lir_func_meta_t中添加is_va_arg字段用于标识可变参数函数
- 修改scc_hir2lir函数参数类型,移除const限定符
- 更新比较操作的指令映射逻辑,将条件信息存储在metadata中
- 调整代码结构,在各个switch case分支中统一使用"} break"格式

fix(x86-isel): 修复x86指令选择中的立即数和重定位处理

- 修改emit_direct_call函数以正确处理全局符号重定位
- 更新立即数字段访问从imm到imm0
- 添加新的重定位操作数类型SCC_X86_OPR_RELOC
- 实现重定位目标类型的完整处理逻辑,包括基本块和符号

refactor(x86-mir): 重构x86操作数结构以支持重定位机制

- 将内存操作数的disp字段改为结构体形式包含displacement信息
- 移除不再使用的常用操作数构造器函数
- 保留并完善slot操作数构造器
- 更新内存操作数的调试输出格式

feat(ir2mcode): 添加重定位表支持以处理符号引用

- 定义新的重定位结构体scc_reloc_t用于记录重定位信息
- 修改scc_ir2mcode_emit_instr函数签名以传递重定位表
- 实现重定位补丁应用功能scc_ir2mcode_patch
- 更新机器码生成流程以收集和处理重定位信息

refactor(ir2sccf): 重构SCEF文件生成以支持重定位处理

- 提取独立的emit_mir_module函数处理MIR模块的机器码生成
- 实现基本块间重定位的地址解析和补丁应用
- 改进符号重定位的处理机制
- 简化机器码段数据的最终处理流程
2026-05-21 16:19:49 +08:00

124 lines
3.8 KiB
C
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
#ifndef __SCC_X86_ENCODE_H__
#define __SCC_X86_ENCODE_H__
#include "../scc_mcode.h"
#include "scc_x86_iform.h"
#include "scc_x86_reg.h"
typedef struct {
i64 displacement;
u32 displacement_bits;
} scc_x86_disp_t;
typedef struct {
scc_x86_reg_t seg;
scc_x86_reg_t base;
scc_x86_reg_t index;
u32 scale;
scc_x86_disp_t disp;
} scc_x86_mem_t;
typedef enum {
SCC_X86_RELOC_TARGET_SYMBOL,
SCC_X86_RELOC_TARGET_BBLOCK,
} scc_x86_reloc_target_t;
typedef struct scc_x86_reloc_op {
scc_x86_operand_kind_t kind; // 原始操作数类型
scc_x86_reloc_target_t target;
union {
i64 imm;
int bblock_id; // 如果 kind == RELBR
const char *global_name; // 如果 kind == SYMBOL
};
i64 addend; // 额外常量偏移,用于 sym+addend
} scc_x86_reloc_op_t;
typedef struct {
scc_x86_operand_kind_t kind;
union {
scc_x86_reg_t reg;
i32 brdisp;
i64 simm0;
u64 imm0;
u8 imm1;
scc_x86_mem_t mem;
scc_x86_reloc_op_t reloc;
};
} scc_x86_operand_value_t;
static inline scc_x86_operand_value_t scc_x86_op_preg(scc_x86_reg_t reg) {
scc_x86_operand_value_t o = {.kind = SCC_X86_OPR_REG, .reg = reg};
return o;
}
static inline scc_x86_operand_value_t scc_x86_op_vreg(int vreg) {
scc_x86_operand_value_t o = {
.kind = SCC_X86_OPR_REG,
.reg = (scc_x86_reg_t)((int)SCC_X86_REG_COUNT + vreg)};
return o;
}
static inline scc_x86_operand_value_t scc_x86_op_relbr(i32 rel) {
scc_x86_operand_value_t o = {.kind = SCC_X86_OPR_RELBR, .brdisp = rel};
return o;
}
static inline scc_x86_operand_value_t scc_x86_op_imm(i64 imm) {
scc_x86_operand_value_t o = {.kind = SCC_X86_OPR_IMM, .simm0 = imm};
return o;
}
static inline scc_x86_operand_value_t scc_x86_op_mem(scc_x86_mem_t mem) {
scc_x86_operand_value_t o = {.kind = SCC_X86_OPR_MEM, .mem = mem};
return o;
}
// 1. 全局符号重定位绝对地址加载MOV reg, imm32/64
static inline scc_x86_operand_value_t
scc_x86_op_reloc_global_imm(const char *sym, i64 addend) {
scc_x86_operand_value_t op = {.kind = SCC_X86_OPR_RELOC};
op.reloc.kind = SCC_X86_OPR_IMM;
op.reloc.target = SCC_X86_RELOC_TARGET_SYMBOL;
op.reloc.global_name = sym;
op.reloc.addend = addend;
return op;
}
// 2. 全局符号重定位(相对跳转/调用CALL/JMP rel32
static inline scc_x86_operand_value_t
scc_x86_op_reloc_global_relbr(const char *sym, i64 addend) {
scc_x86_operand_value_t op = {.kind = SCC_X86_OPR_RELOC};
op.reloc.kind = SCC_X86_OPR_RELBR;
op.reloc.target = SCC_X86_RELOC_TARGET_SYMBOL;
op.reloc.global_name = sym;
op.reloc.addend = addend;
return op;
}
// 3. 基本块重定位(相对跳转)
static inline scc_x86_operand_value_t scc_x86_op_reloc_block(int bid,
i64 addend) {
scc_x86_operand_value_t op = {.kind = SCC_X86_OPR_RELOC};
op.reloc.kind = SCC_X86_OPR_RELBR;
op.reloc.target = SCC_X86_RELOC_TARGET_BBLOCK;
op.reloc.bblock_id = bid;
op.reloc.addend = addend;
return op;
}
// 4. 如果需要支持符号的内存寻址(如 [RIP + sym]),可扩展 kind =
// SCC_X86_OPR_MEM
static inline scc_x86_operand_value_t
scc_x86_op_reloc_global_mem(const char *sym, i64 addend) {
scc_x86_operand_value_t op = {.kind = SCC_X86_OPR_RELOC};
op.reloc.kind = SCC_X86_OPR_MEM;
op.reloc.target = SCC_X86_RELOC_TARGET_SYMBOL;
op.reloc.global_name = sym;
op.reloc.addend = addend;
// 编码时需生成 RIP 相对寻址的 ModRM/SIB
return op;
}
/* 按 iform 发射一条指令ops 数组长度需与 iform 定义的操作数数目一致 */
int scc_x86_encode_inst(scc_mcode_t *mcode, scc_x86_iform_t iform,
const scc_x86_operand_value_t *ops);
#endif /* __SCC_X86_ENCODE_H__ */