Files
scc/runtime/scc_utils/include/scc_hashtable.h
zzy e5bbffe170 feat(cbuild): 添加 lir 库依赖并注释掉未使用的模块
- 添加 { name = "lir", path = "./libs/lir" } 依赖项
- 注释掉 ir2mcode 和 sccf2target 模块以暂时禁用

refactor(ast2ir): 修复类型转换问题并简化哈希表初始化

- 修复 compute_type_layout 函数中的类型转换警告
- 在表达式处理末尾添加 UNREACHABLE() 断言
- 移除 begin_func 中的 param_names 参数
- 使用 scc_hashtable_usize_init 替代自定义比较函数

refactor(ir): 简化函数构建器接口并修复返回值处理

- 移除 scc_ir_builder_begin_func 中的 param_names 参数
- 修改 scc_ir_builder_ret_void 以正确处理 void 返回值
- 初始化返回值节点而不是直接设置为 0

refactor(ir): 简化模块初始化中的哈希表配置

- 移除自定义 hash_key 和 cmp_key 函数
- 使用 scc_hashtable_usize_init 统一初始化哈希表

feat(lir): 添加低层中间表示库基础结构

- 创建 lir 库的包配置文件
- 定义 LIR 的基本数据结构、指令类型和操作枚举
- 实现 LIR 构建器和模块管理功能
- 添加 LIR 转换器头文件和转储功能
2026-04-18 11:35:43 +08:00

135 lines
4.1 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.
/**
* @file hashtable.h
* @brief 开放寻址法哈希表实现
*
* 提供基于向量容器的哈希表实现,支持动态扩容和墓碑机制
*/
#ifndef __SCC_HASHTABLE_H__
#define __SCC_HASHTABLE_H__
#include <scc_core.h>
/**
* @enum hp_entry_state_t
* @brief 哈希表条目状态标识
*/
typedef enum scc_hashtable_entry_state {
ENTRY_EMPTY, /**< 空槽位(从未使用过) */
ENTRY_ACTIVE, /**< 有效条目(包含键值对) */
ENTRY_TOMBSTONE /**< 墓碑标记(已删除条目) */
} scc_hashtable_entry_state_t;
/**
* @struct scc_hashtable_entry_t
* @brief 哈希表条目结构
*
* @note key/value内存由调用者管理哈希表不负责其生命周期
*/
typedef struct scc_hashtable_entry {
const void *key; /**< 键指针(不可变) */
void *value; /**< 值指针 */
u32 hash; /**< 预计算的哈希值(避免重复计算) */
scc_hashtable_entry_state_t state; /**< 当前条目状态 */
} scc_hashtable_entry_t;
typedef u32 (*scc_hashtable_hash_func_t)(const void *key, void *userdata);
typedef int (*scc_hashtable_equal_func_t)(const void *key1, const void *key2,
void *userdata);
/**
* @struct scc_hashtable_t
* @brief 哈希表主体结构
*
* 使用开放寻址法实现,采用墓碑标记处理删除操作
*/
typedef struct scc_hashtable {
void *userdata;
SCC_VEC(scc_hashtable_entry_t) entries; /**< 条目存储容器 */
u32 count; /**< 有效条目数量(不含墓碑) */
u32 tombstone_count; /**< 墓碑条目数量 */
/**
* @brief 哈希函数指针
* @param key 键指针
* @return 32位无符号哈希值
*/
scc_hashtable_hash_func_t hash_func;
/**
* @brief 键比较函数指针
* @param key1 第一个键指针
* @param key2 第二个键指针
* @return 相同返回0不同返回非0
*/
scc_hashtable_equal_func_t cmp_func;
} scc_hashtable_t;
/**
* @brief 初始化哈希表结构
* @param ht 哈希表实例指针
*
* @warning 必须设置hash_func和key_cmp后才能使用
*/
void scc_hashtable_init(scc_hashtable_t *ht,
scc_hashtable_hash_func_t hash_func,
scc_hashtable_equal_func_t cmp_func, void *userdata);
void scc_hashtable_cstr_init(scc_hashtable_t *ht);
void scc_hashtable_usize_init(scc_hashtable_t *ht);
/**
* @brief 插入/更新键值对
* @param ht 哈希表实例指针
* @param key 键指针
* @param value 值指针
* @return 被替换的旧值指针无替换返回nullptr
*/
void *scc_hashtable_set(scc_hashtable_t *ht, const void *key, void *value);
/**
* @brief 查找键对应值
* @param ht 哈希表实例指针
* @param key 查找键指针
* @return 找到返回值指针未找到返回nullptr
*/
void *scc_hashtable_get(scc_hashtable_t *ht, const void *key);
/**
* @brief 删除键值对
* @param ht 哈希表实例指针
* @param key 要删除的键指针
* @return 被删除的值指针不存在返回nullptr
*
* @note 实际采用墓碑标记方式删除
*/
void *scc_hashtable_del(scc_hashtable_t *ht, const void *key);
/**
* @brief 销毁哈希表
* @param ht 哈希表实例指针
*
* @note 仅释放哈希表内部内存不会释放key/value内存
*/
void scc_hashtable_drop(scc_hashtable_t *ht);
/**
* @typedef scc_hashtable_iter_fn
* @brief 哈希表迭代回调函数类型
* @param key 当前键指针
* @param value 当前值指针
* @param context 用户上下文指针
* @return 返回非0停止迭代
*/
typedef int (*scc_hashtable_iter_fn)(const void *key, void *value,
void *context);
/**
* @brief 遍历哈希表所有有效条目
* @param ht 哈希表实例指针
* @param iter_func 迭代回调函数
* @param context 用户上下文指针
*/
void scc_hashtable_foreach(scc_hashtable_t *ht, scc_hashtable_iter_fn iter_func,
void *context);
#endif /* __SCC_HASHTABLE_H__ */