feat(ir): 实现函数调用和参数处理功能

- 在AST定义中移除函数调用结构体中的冗余name字段
- 实现完整的函数声明和定义处理流程,支持符号表查找
- 添加函数参数引用节点类型,支持参数传递和访问
- 实现函数调用的IR生成,包括参数处理和符号解析
- 添加断言确保节点有效性,提升代码健壮性

fix(ast2ir): 优化类型转换处理逻辑

- 移除多余的注释说明
- 简化参数为空检查逻辑,提高代码简洁性
- 修复函数调用时的符号表查找机制

refactor(ir): 改进IR构建器接口设计

- 修改函数构建相关API,使接口更加清晰
- 添加函数声明集合管理
- 重构内置类型缓存机制

feat(ir2mcode): 完善AMD64代码生成

- 实现函数参数到寄存器的映射
- 添加函数调用约定支持(最多4个参数)
- 实现函数符号和重定位处理
- 添加栈帧管理机制
- 修正栈偏移计算

chore(ir): 清理和优化IR dump输出

- 更新节点类型描述信息
- 改进函数声明和定义的输出格式
- 修正格式化输出中的符号显示问题

style: 代码格式化和命名规范化

- 统一重定位类型枚举命名
- 优化函数参数验证和错误处理
This commit is contained in:
zzy
2026-03-23 16:02:23 +08:00
parent 097dbdcc2a
commit 741171dbba
17 changed files with 356 additions and 152 deletions

View File

@@ -74,8 +74,9 @@ typedef enum {
/** 重定位类型 */
typedef enum {
SCCF_RELOC_ABS = 1, ///< 绝对地址
SCCF_RELOC_REL = 2, ///< 对地址
SCCF_RELOC_TYPE_EMPTY = 0, ///< 空展位符
SCCF_RELOC_TYPE_ABS = 1, ///< 对地址
SCCF_RELOC_TYPE_REL = 2, ///< 相对地址
} sccf_reloc_type_t;
/**
@@ -121,11 +122,11 @@ typedef struct {
* @brief SCCF重定向条目
*/
typedef struct {
sccf_enum_t type; ///< 重定位类型
sccf_enum_t sect_type; ///< 需要重定位的段类型(代码段/数据段)
sccf_size_t sym_idx; ///< 符号索引(重定向指向的符号)
sccf_size_t offset; ///< 在段中的偏移量(用于重定向的地址)
sccf_isize_t addend; ///< 加数(用于获取相对位置时PC的额外值)
sccf_enum_t reloc_type; ///< 重定位类型
sccf_enum_t sect_type; ///< 需要重定位的段类型(代码段/数据段)
sccf_size_t sym_idx; ///< 符号索引(重定向指向的符号)
sccf_size_t offset; ///< 在段中的偏移量(用于重定向的地址)
sccf_isize_t addend; ///< 加数(用于获取相对位置时PC的额外值)
} sccf_reloc_t;
/**

View File

@@ -7,6 +7,7 @@
typedef struct {
sccf_t sccf;
int aligned;
const char *entry_symbol_name;
sccf_strtab_t strtab;
sccf_reloc_vec_t relocs;
sccf_sym_vec_t symtab;
@@ -19,10 +20,16 @@ void sccf_builder_init(sccf_builder_t *builder);
usize sccf_builder_add_symbol(sccf_builder_t *builder, const char *name,
sccf_sym_t *sym);
usize sccf_builder_get_symbol_idx(sccf_builder_t *builder, const char *name);
static inline sccf_sym_t *
sccf_builder_get_symbol_unsafe(sccf_builder_t *builder, usize idx) {
sccf_builder_get_symbol_unsafe(sccf_builder_t *builder, const char *name) {
usize idx = sccf_builder_get_symbol_idx(builder, name);
if (idx == 0) {
return null;
}
return &scc_vec_at(builder->symtab, idx);
}
void sccf_builder_add_reloc(sccf_builder_t *builder, sccf_reloc_t reloc);
/**

View File

@@ -13,6 +13,8 @@ void sccf_builder_init(sccf_builder_t *builder) {
scc_vec_init(builder->relocs);
scc_vec_init(builder->symtab);
builder->entry_symbol_name = null;
///< Push null
scc_vec_push(builder->strtab, (char)'\0');
///< Push null
@@ -23,21 +25,26 @@ usize sccf_builder_add_symbol(sccf_builder_t *builder, const char *name,
sccf_sym_t *sym) {
usize offset = 0;
offset = (usize)scc_hashtable_get(&builder->str2offset, name);
const char *key = &scc_vec_at(builder->strtab, offset);
if (offset == 0) {
offset = scc_vec_size(builder->strtab);
scc_hashtable_set(&builder->str2offset, name, (void *)offset);
while (*name) {
scc_vec_push(builder->strtab, *name);
name++;
}
scc_vec_push(builder->strtab, '\0');
key = &scc_vec_at(builder->strtab, offset);
scc_hashtable_set(&builder->str2offset, key, (void *)offset);
}
sym->name_offset = offset;
usize sym_idx = scc_vec_size(builder->symtab);
offset = (usize)scc_hashtable_get(&builder->str2sym, name);
offset = (usize)scc_hashtable_get(&builder->str2sym, key);
if (offset == 0) {
scc_hashtable_set(&builder->str2sym, name, (void *)sym_idx);
scc_hashtable_set(&builder->str2sym, key, (void *)sym_idx);
} else {
LOG_ERROR("symbol %s already exists", key);
}
scc_vec_push(builder->symtab, *sym);
return sym_idx;
@@ -62,9 +69,19 @@ void sccf_builder_add_section(sccf_builder_t *builder,
}
const sccf_t *sccf_builder_to_sccf(sccf_builder_t *builder) {
// TODO symtab strtab reloc
// sccf_sect_header_t symtab_header;
// sccf_sect_data_t symtab_data;
if (builder->entry_symbol_name == null) {
builder->sccf.header.entry_point = 0;
} else {
sccf_sym_t *sym =
sccf_builder_get_symbol_unsafe(builder, builder->entry_symbol_name);
if (sym == null || sym->sccf_sect_type != SCCF_SECT_CODE) {
LOG_ERROR("entry symbol %s not found");
builder->sccf.header.entry_point = 0;
} else {
builder->sccf.header.entry_point = sym->sccf_sect_offset;
}
}
sccf_sect_header_t sect_header;
if (scc_vec_size(builder->strtab)) {
sect_header = (sccf_sect_header_t){