feat(ast): 将AST上下文重构为模块并添加字符串池支持

- 将scc_ast_ctx重命名为scc_ast_module以更好地反映其功能
- 添加scc_strpool_t用于统一管理AST节点名称字符串的生命周期
- 实现scc_ast_module_intern函数用于字符串驻留
- 更新所有相关的初始化、销毁和访问函数命名
- 修改内存分配宏以使用新的模块结构

refactor(parser): 更新解析器以使用AST模块和字符串池

- 将解析器中的ast_ctx字段替换为ast_module
- 在创建AST节点时使用新的ast_module参数
- 使用scc_ast_module_intern函数处理标识符和字符串字面量
- 确保所有字符串都被正确驻留到模块的字符串池中

refactor(sema): 更新语义分析器使用AST模块

- 将sema_ctx中的ast_ctx字段替换为ast_module
- 更新语义分析器初始化函数参数

refactor(ast2ir): 更新AST到IR转换器使用AST模块

- 将ast_ctx字段替换为ast_module
- 更新上下文初始化函数参数和实现

fix(cfg): 修复CFG模块中的符号查找错误

- 修正scc_cfg_module_unsafe_get_symbol函数中的边界检查条件

perf(ir): 完善各IR层模块的内存清理机制

- 为HIR模块添加函数和基本块元数据的释放逻辑
- 为MIR和LIR模块完善完整的资源清理和内存释放
- 确保所有分配的元数据结构都能被正确释放

chore(deps): 添加scc_utils依赖到AST库

- 在libs/ast/cbuild.toml中添加对scc_utils的依赖
This commit is contained in:
zzy
2026-05-31 19:56:19 +08:00
parent d2eafa9dc6
commit 8b817da3b6
31 changed files with 409 additions and 179 deletions

View File

@@ -6,6 +6,7 @@
typedef struct {
scc_hashtable_t str2libsym;
scc_pe_idata_lib_vec_t idata_libs;
scc_strpool_t pool; /* owns all interned symbol names and library names */
const char *find_path;
} pe_idata_lib_ctx_t;
@@ -24,11 +25,18 @@ static void load_from_def(pe_idata_lib_ctx_t *ctx, const char *file_path,
const char *fname = scc_str_as_cstr(&fpath);
scc_file_t fp = scc_fopen(fname, SCC_FILE_READ);
if (fp == nullptr) {
scc_str_drop(&fpath);
return;
}
LOG_TRACE("load_from_def file read sucessful: %s", fname);
usize fsize = scc_fsize(fp);
if (fsize == 0) {
scc_fclose(fp);
scc_str_drop(&fpath);
return;
}
char *buffer = scc_malloc(fsize);
Assert(buffer != nullptr);
@@ -36,10 +44,9 @@ static void load_from_def(pe_idata_lib_ctx_t *ctx, const char *file_path,
Assert(read_size == fsize);
scc_fclose(fp);
// scc_pe_name_vec_t symbol_names;
usize line = 0;
// FIXME
buffer[fsize - 1] = '\0';
const char *dll_pooled = scc_strpool_intern(&ctx->pool, dll_name);
for (usize i = 0; i < fsize; i += 1) {
if (buffer[i] == '\n') {
line += 1;
@@ -56,15 +63,18 @@ static void load_from_def(pe_idata_lib_ctx_t *ctx, const char *file_path,
break;
}
}
// FIXME memory leak
scc_hashtable_set(&ctx->str2libsym, buffer + i, (void *)dll_name);
const char *sym_pooled = scc_strpool_intern(&ctx->pool, buffer + i);
scc_hashtable_set(&ctx->str2libsym, sym_pooled, (void *)dll_pooled);
}
scc_free(buffer);
scc_str_drop(&fpath);
}
static void pe_idata_lib_init(pe_idata_lib_ctx_t *ctx, const char *find_path) {
// Got .dll.def
scc_hashtable_cstr_init(&ctx->str2libsym);
scc_strpool_init(&ctx->pool);
scc_vec_init(ctx->idata_libs);
ctx->find_path = find_path;
load_from_def(ctx, ctx->find_path, "msvcrt.dll");
@@ -249,4 +259,12 @@ void sccf2pe(scc_pe_builder_t *builder, const sccf_t *sccf) {
scc_pe_write_section(builder, &idata_range,
scc_vec_unsafe_get_data(idata_buffer),
scc_vec_size(idata_buffer));
// Cleanup
scc_vec_foreach(idata_lib_ctx.idata_libs, i) {
scc_vec_free(scc_vec_at(idata_lib_ctx.idata_libs, i).symbol_names);
}
scc_vec_free(idata_lib_ctx.idata_libs);
scc_hashtable_drop(&idata_lib_ctx.str2libsym);
scc_strpool_drop(&idata_lib_ctx.pool);
}