From 4015acd86694db0da78d95ee7dbf8b6fb0cd1e18 Mon Sep 17 00:00:00 2001 From: zzy <2450266535@qq.com> Date: Wed, 4 Mar 2026 16:47:28 +0800 Subject: [PATCH] =?UTF-8?q?refactor(hashtable):=20=E7=AE=80=E5=8C=96?= =?UTF-8?q?=E5=93=88=E5=B8=8C=E8=A1=A8=E5=88=9D=E5=A7=8B=E5=8C=96=E6=8E=A5?= =?UTF-8?q?=E5=8F=A3=E5=B9=B6=E4=BC=98=E5=8C=96=E6=96=87=E4=BB=B6=E6=89=93?= =?UTF-8?q?=E5=BC=80=E6=A8=A1=E5=BC=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 将哈希表初始化从两步设置改为一步完成,直接传递哈希函数和比较函数 - 重构scc_hashtable_init函数签名,接受函数指针参数 - 更新内部字段名从key_cmp为cmp_func以保持一致性 - 添加类型定义scc_hashtable_hash_func_t和scc_hashtable_equal_func_t - 修改PAL层文件操作接口,使用枚举模式替代固定只读模式 - 更新IR上下文、预处理器宏表等组件使用新的初始化方式 - 移除AST头文件中未使用的语义分析回调类型定义 - 修复预处理器中的空指针访问问题 --- libs/ast/include/scc_ast.h | 17 ----------------- libs/ir/src/ir_ctx.c | 23 +++++++---------------- libs/pproc/src/pproc_macro.c | 4 +--- libs/pproc/src/scc_pproc.c | 6 ++++-- runtime/scc_core/include/scc_core_pal.h | 7 ++++++- runtime/scc_core/src/cfg.std_impl.c | 13 +++++++++++-- runtime/scc_core/src/core_impl.c | 2 +- runtime/scc_utils/include/scc_hashtable.h | 11 ++++++++--- runtime/scc_utils/src/hashtable.c | 11 ++++++++--- runtime/scc_utils/src/strpool.c | 5 ++--- 10 files changed, 48 insertions(+), 51 deletions(-) diff --git a/libs/ast/include/scc_ast.h b/libs/ast/include/scc_ast.h index 2e1d6e8..feb4425 100644 --- a/libs/ast/include/scc_ast.h +++ b/libs/ast/include/scc_ast.h @@ -3,21 +3,4 @@ #include "ast_def.h" -/** - * @brief 语义分析回调函数类型 - */ -typedef void (*scc_sema_callback_t)(void *context, - scc_ast_node_type_t node_type, void *node); - -/** - * @brief 语义分析回调集合 - */ -typedef struct scc_sema_callbacks { - scc_sema_callback_t on_decl; - scc_sema_callback_t on_stmt; - scc_sema_callback_t on_expr; - scc_sema_callback_t on_type; - void *context; -} scc_sema_callbacks_t; - #endif /* __SCC_AST_H__ */ diff --git a/libs/ir/src/ir_ctx.c b/libs/ir/src/ir_ctx.c index b9cd6ce..b67ed9e 100644 --- a/libs/ir/src/ir_ctx.c +++ b/libs/ir/src/ir_ctx.c @@ -120,24 +120,15 @@ void scc_ir_ctx_init(scc_ir_cprog_ctx_t *ctx) { scc_vec_init(ctx->bblocks); scc_vec_init(ctx->funcs); - // 设置哈希函数 - ctx->uid2nodes.hash_func = hash_key; - ctx->uid2nodes.key_cmp = cmp_key; - ctx->uid2types.hash_func = hash_key; - ctx->uid2types.key_cmp = cmp_key; - ctx->uid2bblocks.hash_func = hash_key; - ctx->uid2bblocks.key_cmp = cmp_key; - ctx->uid2funcs.hash_func = hash_key; - ctx->uid2funcs.key_cmp = cmp_key; // 初始化哈希表 - scc_hashtable_init(&ctx->uid2nodes); - scc_hashtable_init(&ctx->uid2types); - scc_hashtable_init(&ctx->uid2bblocks); - scc_hashtable_init(&ctx->uid2funcs); + scc_hashtable_init(&ctx->uid2nodes, hash_key, cmp_key); + scc_hashtable_init(&ctx->uid2types, hash_key, cmp_key); + scc_hashtable_init(&ctx->uid2bblocks, hash_key, cmp_key); + scc_hashtable_init(&ctx->uid2funcs, hash_key, cmp_key); - ctx->type_uniquing.hash_func = (void *)hash_type; - ctx->type_uniquing.key_cmp = (void *)cmp_type; - scc_hashtable_init(&ctx->type_uniquing); + scc_hashtable_init(&ctx->type_uniquing, + (scc_hashtable_hash_func_t)hash_type, + (scc_hashtable_equal_func_t)cmp_type); // 预留UID 0 作为无效引用 ctx->node_uid = 1; diff --git a/libs/pproc/src/pproc_macro.c b/libs/pproc/src/pproc_macro.c index d2e0a89..f45ff79 100644 --- a/libs/pproc/src/pproc_macro.c +++ b/libs/pproc/src/pproc_macro.c @@ -143,9 +143,7 @@ static int hash_cmp(const void *key1, const void *key2) { void scc_pproc_marco_table_init(scc_pproc_macro_table_t *macros) { Assert(macros != null); - macros->table.hash_func = hash_func; - macros->table.key_cmp = hash_cmp; - scc_hashtable_init(¯os->table); + scc_hashtable_init(¯os->table, hash_func, hash_cmp); } static int macro_free(const void *key, void *value, void *context) { diff --git a/libs/pproc/src/scc_pproc.c b/libs/pproc/src/scc_pproc.c index a97f48e..6ac163a 100644 --- a/libs/pproc/src/scc_pproc.c +++ b/libs/pproc/src/scc_pproc.c @@ -3,10 +3,11 @@ #include static int pproc_next_one_file(scc_pproc_t *pp, scc_lexer_tok_t *out) { - scc_lexer_tok_ring_t *stream = pp->cur_ring; + scc_lexer_tok_ring_t *stream = null; scc_lexer_tok_t tok = {0}; int ok = 0; CONTINUE: + stream = pp->cur_ring; if (pp->expanded_ring.cap) { scc_ring_next_consume(pp->expanded_ring, *out, ok); if (ok == false) { @@ -71,8 +72,9 @@ CONTINUE: } static int pproc_next(scc_pproc_t *pp, scc_lexer_tok_t *tok) { - int ret = pproc_next_one_file(pp, tok); + int ret = 0; CONTINUE: + ret = pproc_next_one_file(pp, tok); if (ret != 0) { return true; } diff --git a/runtime/scc_core/include/scc_core_pal.h b/runtime/scc_core/include/scc_core_pal.h index 6919a08..f14e123 100644 --- a/runtime/scc_core/include/scc_core_pal.h +++ b/runtime/scc_core/include/scc_core_pal.h @@ -34,7 +34,12 @@ void scc_pal_ewrite(const char *data, size_t len); /* File */ typedef void *scc_pal_file_t; -scc_pal_file_t scc_pal_fopen(const char *path); +typedef enum { + SCC_FILE_PAL_READ, + SCC_FILE_PAL_WRITE, + SCC_FILE_PAL_APPEND, +} scc_pal_fmode_t; +scc_pal_file_t scc_pal_fopen(const char *path, scc_pal_fmode_t mode); void scc_pal_fclose(scc_pal_file_t f); size_t scc_pal_fread(scc_pal_file_t f, void *buf, size_t size); size_t scc_pal_fwrite(scc_pal_file_t f, const void *buf, size_t size); diff --git a/runtime/scc_core/src/cfg.std_impl.c b/runtime/scc_core/src/cfg.std_impl.c index d971046..adc36ac 100644 --- a/runtime/scc_core/src/cfg.std_impl.c +++ b/runtime/scc_core/src/cfg.std_impl.c @@ -20,8 +20,17 @@ void *scc_pal_realloc(void *ptr, size_t new_size) { void scc_pal_free(void *ptr) { free(ptr); } -scc_pal_file_t scc_pal_fopen(const char *path) { - return (scc_pal_file_t)fopen(path, "r+b"); +scc_pal_file_t scc_pal_fopen(const char *path, scc_pal_fmode_t mode) { + switch (mode) { + case SCC_FILE_PAL_READ: + return fopen(path, "rb"); + case SCC_FILE_PAL_WRITE: + return fopen(path, "wb"); + case SCC_FILE_PAL_APPEND: + return fopen(path, "ab"); + default: + return (scc_pal_file_t)0; + } } void scc_pal_fclose(scc_pal_file_t file) { diff --git a/runtime/scc_core/src/core_impl.c b/runtime/scc_core/src/core_impl.c index a294cb5..31e59d3 100644 --- a/runtime/scc_core/src/core_impl.c +++ b/runtime/scc_core/src/core_impl.c @@ -11,7 +11,7 @@ void putchar_(char ch) { LOG_FATAL("you can't use printf.c directly"); } scc_file_t scc_fopen(const char *path, scc_fmode_t mode) { - return scc_pal_fopen(path); + return scc_pal_fopen(path, (scc_pal_fmode_t)mode); // FIXME } void scc_fclose(scc_file_t file) { scc_pal_fclose(file); } diff --git a/runtime/scc_utils/include/scc_hashtable.h b/runtime/scc_utils/include/scc_hashtable.h index 7de94c2..ab76298 100644 --- a/runtime/scc_utils/include/scc_hashtable.h +++ b/runtime/scc_utils/include/scc_hashtable.h @@ -33,6 +33,9 @@ typedef struct scc_hashtable_entry { scc_hashtable_entry_state_t state; /**< 当前条目状态 */ } scc_hashtable_entry_t; +typedef u32 (*scc_hashtable_hash_func_t)(const void *key); +typedef int (*scc_hashtable_equal_func_t)(const void *key1, const void *key2); + /** * @struct scc_hashtable_t * @brief 哈希表主体结构 @@ -48,14 +51,14 @@ typedef struct scc_hashtable { * @param key 键指针 * @return 32位无符号哈希值 */ - u32 (*hash_func)(const void *key); + scc_hashtable_hash_func_t hash_func; /** * @brief 键比较函数指针 * @param key1 第一个键指针 * @param key2 第二个键指针 * @return 相同返回0,不同返回非0 */ - int (*key_cmp)(const void *key1, const void *key2); + scc_hashtable_equal_func_t cmp_func; } scc_hashtable_t; /** @@ -64,7 +67,9 @@ typedef struct scc_hashtable { * * @warning 必须设置hash_func和key_cmp后才能使用 */ -void scc_hashtable_init(scc_hashtable_t *ht); +void scc_hashtable_init(scc_hashtable_t *ht, + scc_hashtable_hash_func_t hash_func, + scc_hashtable_equal_func_t cmp_func); /** * @brief 插入/更新键值对 diff --git a/runtime/scc_utils/src/hashtable.c b/runtime/scc_utils/src/hashtable.c index 817334c..43b044e 100644 --- a/runtime/scc_utils/src/hashtable.c +++ b/runtime/scc_utils/src/hashtable.c @@ -4,11 +4,16 @@ #define SCC_INIT_HASHMAP_SIZE (32) #endif -void scc_hashtable_init(scc_hashtable_t *ht) { +void scc_hashtable_init(scc_hashtable_t *ht, + scc_hashtable_hash_func_t hash_func, + scc_hashtable_equal_func_t cmp_func) { + scc_vec_init(ht->entries); ht->count = 0; ht->tombstone_count = 0; - Assert(ht->key_cmp != NULL && ht->hash_func != NULL); + ht->cmp_func = cmp_func; + ht->hash_func = hash_func; + Assert(ht->cmp_func != null && ht->hash_func != null); } static int next_power_of_two(int n) { @@ -40,7 +45,7 @@ static scc_hashtable_entry_t *find_entry(scc_hashtable_t *ht, const void *key, if (entry->state == ENTRY_TOMBSTONE) { if (!tombstone) tombstone = entry; - } else if (entry->hash == hash && ht->key_cmp(entry->key, key) == 0) { + } else if (entry->hash == hash && ht->cmp_func(entry->key, key) == 0) { return entry; } diff --git a/runtime/scc_utils/src/strpool.c b/runtime/scc_utils/src/strpool.c index b82a9b9..d6fdf9f 100644 --- a/runtime/scc_utils/src/strpool.c +++ b/runtime/scc_utils/src/strpool.c @@ -1,9 +1,8 @@ #include void scc_strpool_init(scc_strpool_t *pool) { - pool->ht.hash_func = (u32 (*)(const void *))scc_strhash32; - pool->ht.key_cmp = (int (*)(const void *, const void *))scc_strcmp; - scc_hashtable_init(&pool->ht); + scc_hashtable_init(&pool->ht, (scc_hashtable_hash_func_t)scc_strhash32, + (scc_hashtable_equal_func_t)scc_strcmp); } const char *scc_strpool_intern(scc_strpool_t *pool, const char *str) {