移除了在全局变量声明处理中对全局值向量的直接推送操作, 以优化内存管理和值引用的一致性。 fix(hir): 修复表达式索引类型检查中的空指针访问 修正了表达式处理中索引类型的获取方式,从使用类型指针改为使用类型引用, 并更新了空值检查条件以避免潜在的空指针解引用问题。 perf(hir): 优化类型大小计算性能 将类型大小计算逻辑从模块内部实现替换为使用HIR布局系统提供的统一接口, 提高计算效率和代码复用性。 refactor(hir): 统一字符串常量构建流程 重构了字符串常量的创建过程,简化了类型定义步骤并确保包含正确的空终止符。 fix(dump): 改进全局分配值转储的健壮性 添加了对空初始化值的检查,当全局分配没有初始值时显示零初始化器, 避免访问空指针导致的程序崩溃。 refactor(x86): 增强操作数编码的安全性 在x86指令编码中添加了对操作数字节对齐的断言检查,确保所有操作数都符合 字节边界对齐要求。 chore(build): 更新头文件包含路径和初始化参数 调整了头文件包含路径格式,并更新了HIR程序和模块的初始化函数签名, 传入ABI参数以支持更准确的目标平台特性。
170 lines
5.1 KiB
C
170 lines
5.1 KiB
C
#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;
|
||
};
|
||
u8 size_bits;
|
||
} scc_x86_operand_value_t;
|
||
|
||
static inline scc_x86_operand_value_t scc_x86_op_preg(scc_x86_reg_t reg,
|
||
u8 size_bits) {
|
||
Assert(size_bits % 8 == 0);
|
||
scc_x86_operand_value_t o = {
|
||
.kind = SCC_X86_OPR_REG,
|
||
.reg = reg,
|
||
.size_bits = size_bits,
|
||
};
|
||
return o;
|
||
}
|
||
static inline scc_x86_reg_t scc_x86_op_vreg_reg(int vreg) {
|
||
return (int)SCC_X86_REG_COUNT + vreg;
|
||
}
|
||
static inline scc_x86_operand_value_t scc_x86_op_vreg(int vreg, u8 size_bits) {
|
||
Assert(size_bits % 8 == 0);
|
||
scc_x86_operand_value_t o = {
|
||
.kind = SCC_X86_OPR_REG,
|
||
.reg = scc_x86_op_vreg_reg(vreg),
|
||
.size_bits = size_bits,
|
||
};
|
||
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,
|
||
.size_bits = 32,
|
||
};
|
||
return o;
|
||
}
|
||
static inline scc_x86_operand_value_t scc_x86_op_imm(i64 imm, u8 size_bits) {
|
||
Assert(size_bits % 8 == 0);
|
||
scc_x86_operand_value_t o = {
|
||
.kind = SCC_X86_OPR_IMM,
|
||
.simm0 = imm,
|
||
.size_bits = size_bits,
|
||
};
|
||
return o;
|
||
}
|
||
static inline scc_x86_operand_value_t scc_x86_op_mem(scc_x86_mem_t mem,
|
||
u8 size_bits) {
|
||
Assert(size_bits % 8 == 0);
|
||
scc_x86_operand_value_t o = {
|
||
.kind = SCC_X86_OPR_MEM,
|
||
.mem = mem,
|
||
.size_bits = size_bits,
|
||
};
|
||
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;
|
||
op.size_bits = 0;
|
||
return op;
|
||
}
|
||
|
||
static inline scc_x86_operand_value_t
|
||
scc_x86_op_reloc_global_relrip(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;
|
||
op.size_bits = 32;
|
||
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;
|
||
op.size_bits = 32;
|
||
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;
|
||
op.size_bits = 32;
|
||
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
|
||
op.size_bits = 0;
|
||
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);
|
||
scc_x86_iform_t scc_x86_iclass_to_iform(scc_x86_iclass_t iclass,
|
||
const scc_x86_operand_value_t *ops,
|
||
int num_ops);
|
||
|
||
#endif /* __SCC_X86_ENCODE_H__ */
|