fix(abi): 修复void类型的ABI计算缺少break语句
在scc_type_abi.c文件中,void类型的case分支缺少break语句, 导致执行流程错误地进入下一个case分支。 feat(ast): 为参数声明添加索引字段 在ast_def.h头文件中为参数声明结构体添加param_idx字段, 用于跟踪参数在函数参数列表中的位置索引。 feat(ast): 更新参数初始化函数以支持索引参数 修改scc_ast.h中的scc_ast_decl_param_init函数签名, 添加参数索引idx参数,并将该值存储到参数声明结构体中。 feat(ast2ir): 添加IR转换上下文的值使用提示选项 在ast2ir.h中为scc_ast2ir_ctx_t结构体添加hint_using_value字段, 控制参数转换时是使用值还是分配内存的方式。 fix(ast2ir): 正确处理void类型到IR的转换 当遇到大小为0的类型(如void)时,直接返回void类型, 而不是尝试匹配其他大小分支。 refactor(ast2ir): 统一基本块引用类型为value_ref 将逻辑表达式、条件语句、循环语句中的基本块引用类型 从bblock_ref_t改为value_ref_t,保持类型一致性。 fix(ast2ir): 修正函数引用空值检查 使用SCC_IR_REF_nullptr常量替代0进行函数引用的空值检查, 提高代码的可读性和正确性。 refactor(ast2ir): 简化参数处理逻辑 移除不必要的函数参数获取和命名设置逻辑, 通过递归调用scc_ast2ir_decl来处理参数声明。 feat(ast2ir): 实现参数声明到IR的转换 为参数声明添加完整的IR转换逻辑,包括类型转换、 参数引用创建和内存分配处理。 refactor(ast2ir): 更新哈希表初始化接口 适配新的哈希表初始化函数签名,添加userdata参数支持, 并初始化hint_using_value字段为false。 refactor(ir): 移除函数参数的预分配逻辑 删除IR构建器中函数参数的预分配和循环添加逻辑, 简化函数开始构建的处理流程。 refactor(ir): 更新类型哈希表键值处理 修改类型哈希表的哈希和比较函数以接受模块参数, 正确处理空引用情况并支持新的键值传递方式。 fix(ir): 修复IR转储中的字符串格式 移除IR函数转储时多余的换行符,确保输出格式正确。 refactor(ir): 更新模块哈希表初始化 适配哈希表初始化接口变更,添加userdata参数, 并为各种向量预留UID 0作为无效引用。 fix(ir): 修复模块清理中的循环起始索引 将模块清理循环的起始索引从0改为1,跳过预留的 无效引用项,避免访问空指针。 refactor(ir): 调整向量和哈希表操作顺序 调整模块中向量push和哈希表set的操作顺序, 确保数据一致性和正确的UID分配。 chore(build): 移除ir2mcode模块相关文件 移除ir2mcode相关的头文件和源文件,这些组件 将在后续重构中重新设计或替换。
This commit is contained in:
35
runtime/scc_core/include/scc_pos.h
Normal file
35
runtime/scc_core/include/scc_pos.h
Normal file
@@ -0,0 +1,35 @@
|
||||
#ifndef __SCC_POS_H__
|
||||
#define __SCC_POS_H__
|
||||
|
||||
#include <scc_core_str.h>
|
||||
#include <scc_core_type.h>
|
||||
|
||||
typedef struct scc_pos {
|
||||
const char *name;
|
||||
usize line;
|
||||
usize col;
|
||||
usize offset;
|
||||
} scc_pos_t;
|
||||
|
||||
#define scc_pos_pnt_fmt "%s:%zu:%zu"
|
||||
#define scc_pos_pnt_val(val) (val).name, (val).line, (val).col
|
||||
|
||||
static inline scc_pos_t scc_pos_create() { return (scc_pos_t){0, 1, 1, 0}; }
|
||||
|
||||
static inline void scc_pos_next(scc_pos_t *pos) {
|
||||
pos->offset++;
|
||||
pos->col++;
|
||||
}
|
||||
|
||||
static inline void scc_pos_next_offset(scc_pos_t *pos, int offset) {
|
||||
pos->offset += offset;
|
||||
pos->offset += offset;
|
||||
}
|
||||
|
||||
static inline void scc_pos_next_line(scc_pos_t *pos) {
|
||||
pos->offset++;
|
||||
pos->line++;
|
||||
pos->col = 1;
|
||||
}
|
||||
|
||||
#endif /* __SCC_POS_H__ */
|
||||
@@ -82,7 +82,6 @@ int scc_vfprintf(scc_file_t file, const char *format, va_list args) {
|
||||
va_list args_copy;
|
||||
va_copy(args_copy, args);
|
||||
|
||||
// 先计算所需长度
|
||||
int required_size = vsnprintf_(nullptr, 0, format, args_copy);
|
||||
va_end(args_copy);
|
||||
|
||||
@@ -90,22 +89,20 @@ int scc_vfprintf(scc_file_t file, const char *format, va_list args) {
|
||||
return -1;
|
||||
|
||||
char *buf = nullptr;
|
||||
char stack_buf[4096]; // 移到外部,作用域为整个函数
|
||||
int size = 0;
|
||||
|
||||
if (required_size < 4096) {
|
||||
// 小输出使用栈上缓冲区
|
||||
char stack_buf[4096];
|
||||
size = vsnprintf_(stack_buf, sizeof(stack_buf), format, args);
|
||||
buf = stack_buf;
|
||||
} else {
|
||||
// 大输出使用堆分配
|
||||
buf = scc_pal_malloc(required_size + 1);
|
||||
if (!buf)
|
||||
return -1;
|
||||
size = vsnprintf_(buf, required_size + 1, format, args);
|
||||
}
|
||||
|
||||
// 输出处理...
|
||||
// 输出处理(现在 buf 始终有效)
|
||||
if (file == scc_stdout) {
|
||||
scc_pal_write(buf, size);
|
||||
} else if (file == scc_stderr) {
|
||||
@@ -114,14 +111,12 @@ int scc_vfprintf(scc_file_t file, const char *format, va_list args) {
|
||||
scc_pal_fwrite(file, buf, size);
|
||||
}
|
||||
|
||||
// 如果使用了堆分配,释放内存
|
||||
if (buf && required_size >= 4096) {
|
||||
if (required_size >= 4096 && buf) {
|
||||
scc_pal_free(buf);
|
||||
}
|
||||
|
||||
return size;
|
||||
}
|
||||
|
||||
int scc_snprintf(char *buff, usize buff_size, const char *fmt, ...) {
|
||||
int ret;
|
||||
va_list args;
|
||||
|
||||
@@ -33,8 +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);
|
||||
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
|
||||
@@ -43,6 +44,7 @@ typedef int (*scc_hashtable_equal_func_t)(const void *key1, const void *key2);
|
||||
* 使用开放寻址法实现,采用墓碑标记处理删除操作
|
||||
*/
|
||||
typedef struct scc_hashtable {
|
||||
void *userdata;
|
||||
SCC_VEC(scc_hashtable_entry_t) entries; /**< 条目存储容器 */
|
||||
u32 count; /**< 有效条目数量(不含墓碑) */
|
||||
u32 tombstone_count; /**< 墓碑条目数量 */
|
||||
@@ -69,7 +71,9 @@ typedef struct scc_hashtable {
|
||||
*/
|
||||
void scc_hashtable_init(scc_hashtable_t *ht,
|
||||
scc_hashtable_hash_func_t hash_func,
|
||||
scc_hashtable_equal_func_t cmp_func);
|
||||
scc_hashtable_equal_func_t cmp_func, void *userdata);
|
||||
|
||||
void scc_hashtable_cstr_init(scc_hashtable_t *ht);
|
||||
|
||||
/**
|
||||
* @brief 插入/更新键值对
|
||||
|
||||
31
runtime/scc_utils/include/scc_pos_log.h
Normal file
31
runtime/scc_utils/include/scc_pos_log.h
Normal file
@@ -0,0 +1,31 @@
|
||||
#ifndef __SCC_POS_LOG_H__
|
||||
#define __SCC_POS_LOG_H__
|
||||
|
||||
#include "scc_pos.h"
|
||||
#include <scc_core.h>
|
||||
|
||||
extern logger_t __scc_pos_log;
|
||||
|
||||
#define SCC_POS_LOG(level, pos, fmt, ...) \
|
||||
do { \
|
||||
char _full_msg[LOGGER_MAX_BUF_SIZE]; \
|
||||
int _n = scc_snprintf(_full_msg, sizeof(_full_msg), \
|
||||
scc_pos_pnt_fmt ": ", scc_pos_pnt_val(pos)); \
|
||||
scc_snprintf(_full_msg + _n, sizeof(_full_msg) - _n, fmt, \
|
||||
##__VA_ARGS__); \
|
||||
__scc_pos_log.handler(&__scc_pos_log, level, nullptr, 0, nullptr, \
|
||||
"%s", _full_msg); \
|
||||
} while (0)
|
||||
|
||||
#define SCC_DEBUG(pos, fmt, ...) \
|
||||
SCC_POS_LOG(LOG_LEVEL_DEBUG, pos, fmt, ##__VA_ARGS__)
|
||||
#define SCC_INFO(pos, fmt, ...) \
|
||||
SCC_POS_LOG(LOG_LEVEL_INFO, pos, fmt, ##__VA_ARGS__)
|
||||
#define SCC_WARN(pos, fmt, ...) \
|
||||
SCC_POS_LOG(LOG_LEVEL_WARN, pos, fmt, ##__VA_ARGS__)
|
||||
#define SCC_ERROR(pos, fmt, ...) \
|
||||
SCC_POS_LOG(LOG_LEVEL_ERROR, pos, fmt, ##__VA_ARGS__)
|
||||
#define SCC_FATAL(pos, fmt, ...) \
|
||||
SCC_POS_LOG(LOG_LEVEL_FATAL, pos, fmt, ##__VA_ARGS__)
|
||||
|
||||
#endif /* __SCC_POS_LOG_H__ */
|
||||
@@ -6,16 +6,31 @@
|
||||
|
||||
void scc_hashtable_init(scc_hashtable_t *ht,
|
||||
scc_hashtable_hash_func_t hash_func,
|
||||
scc_hashtable_equal_func_t cmp_func) {
|
||||
scc_hashtable_equal_func_t cmp_func, void *userdata) {
|
||||
|
||||
scc_vec_init(ht->entries);
|
||||
ht->count = 0;
|
||||
ht->tombstone_count = 0;
|
||||
ht->cmp_func = cmp_func;
|
||||
ht->hash_func = hash_func;
|
||||
ht->userdata = userdata;
|
||||
Assert(ht->cmp_func != nullptr && ht->hash_func != nullptr);
|
||||
}
|
||||
|
||||
static u32 ht_strhash(const void *key, void *userdata) {
|
||||
(void)userdata;
|
||||
return scc_strhash32(key);
|
||||
}
|
||||
|
||||
static int ht_strcmp(const void *key1, const void *key2, void *userdata) {
|
||||
(void)userdata;
|
||||
return scc_strcmp(key1, key2);
|
||||
}
|
||||
|
||||
void scc_hashtable_cstr_init(scc_hashtable_t *ht) {
|
||||
scc_hashtable_init(ht, ht_strhash, ht_strcmp, nullptr);
|
||||
}
|
||||
|
||||
static int next_power_of_two(int n) {
|
||||
n--;
|
||||
n |= n >> 1;
|
||||
@@ -45,7 +60,8 @@ 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->cmp_func(entry->key, key) == 0) {
|
||||
} else if (entry->hash == hash &&
|
||||
ht->cmp_func(entry->key, key, ht->userdata) == 0) {
|
||||
return entry;
|
||||
}
|
||||
|
||||
@@ -96,7 +112,7 @@ void *scc_hashtable_set(scc_hashtable_t *ht, const void *key, void *value) {
|
||||
adjust_capacity(ht, new_cap);
|
||||
}
|
||||
|
||||
u32 hash = ht->hash_func(key);
|
||||
u32 hash = ht->hash_func(key, ht->userdata);
|
||||
scc_hashtable_entry_t *entry = find_entry(ht, key, hash);
|
||||
|
||||
void *old_value = nullptr;
|
||||
@@ -119,7 +135,7 @@ void *scc_hashtable_get(scc_hashtable_t *ht, const void *key) {
|
||||
if (ht->entries.cap == 0)
|
||||
return nullptr;
|
||||
|
||||
u32 hash = ht->hash_func(key);
|
||||
u32 hash = ht->hash_func(key, ht->userdata);
|
||||
scc_hashtable_entry_t *entry = find_entry(ht, key, hash);
|
||||
return (entry && entry->state == ENTRY_ACTIVE) ? entry->value : nullptr;
|
||||
}
|
||||
@@ -128,7 +144,7 @@ void *scc_hashtable_del(scc_hashtable_t *ht, const void *key) {
|
||||
if (ht->entries.cap == 0)
|
||||
return nullptr;
|
||||
|
||||
u32 hash = ht->hash_func(key);
|
||||
u32 hash = ht->hash_func(key, ht->userdata);
|
||||
scc_hashtable_entry_t *entry = find_entry(ht, key, hash);
|
||||
|
||||
if (entry == nullptr || entry->state != ENTRY_ACTIVE)
|
||||
|
||||
57
runtime/scc_utils/src/scc_pos_log.c
Normal file
57
runtime/scc_utils/src/scc_pos_log.c
Normal file
@@ -0,0 +1,57 @@
|
||||
#include <scc_pos_log.h>
|
||||
|
||||
static int pos_log_handler(logger_t *module, log_level_t level,
|
||||
const char *file, int line, const char *func,
|
||||
const char *fmt, ...) {
|
||||
|
||||
/* clang-format off */
|
||||
(void) module, (void)file, (void)line, (void)func; // 不再使用
|
||||
|
||||
const char *level_str = nullptr;
|
||||
switch (level) {
|
||||
case LOG_LEVEL_DEBUG: level_str = "DEBUG"; break;
|
||||
case LOG_LEVEL_INFO: level_str = "INFO "; break;
|
||||
case LOG_LEVEL_WARN: level_str = "WARN "; break;
|
||||
case LOG_LEVEL_ERROR: level_str = "ERROR"; break;
|
||||
case LOG_LEVEL_FATAL: level_str = "FATAL"; break;
|
||||
case LOG_LEVEL_TRACE: level_str = "TRACE"; break;
|
||||
default: level_str = "NOTSET"; break;
|
||||
}
|
||||
/// @note: 定义 __LOG_NO_COLOR__ 会取消颜色输出
|
||||
#ifndef __LOG_NO_COLOR__
|
||||
const char *color_code;
|
||||
switch (level) {
|
||||
case LOG_LEVEL_DEBUG: color_code = ANSI_FG_CYAN; break;
|
||||
case LOG_LEVEL_INFO: color_code = ANSI_FG_GREEN; break;
|
||||
case LOG_LEVEL_TRACE: color_code = ANSI_FG_BLUE; break;
|
||||
case LOG_LEVEL_WARN: color_code = ANSI_FG_YELLOW; break;
|
||||
case LOG_LEVEL_ERROR: color_code = ANSI_FG_RED; break;
|
||||
case LOG_LEVEL_FATAL: color_code = ANSI_FG_RED ANSI_UNDERLINED; break;
|
||||
default: color_code = ANSI_NONE;
|
||||
}
|
||||
#endif
|
||||
/* clang-format on */
|
||||
char buf[LOGGER_MAX_BUF_SIZE];
|
||||
int off = scc_snprintf(buf, sizeof(buf), "%s[%s]%s ",
|
||||
color_code ? color_code : "", level_str,
|
||||
color_code ? ANSI_NONE : "");
|
||||
|
||||
va_list args;
|
||||
va_start(args, fmt);
|
||||
scc_vsnprintf(buf + off, sizeof(buf) - off, fmt, args);
|
||||
va_end(args);
|
||||
|
||||
log_puts(buf);
|
||||
log_puts("\n");
|
||||
|
||||
if (level == LOG_LEVEL_FATAL) {
|
||||
log_abort();
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
logger_t __scc_pos_log = {
|
||||
.name = "pos_log",
|
||||
.handler = pos_log_handler,
|
||||
.level = LOG_LEVEL_ALL,
|
||||
};
|
||||
@@ -1,8 +1,7 @@
|
||||
#include <scc_strpool.h>
|
||||
|
||||
void scc_strpool_init(scc_strpool_t *pool) {
|
||||
scc_hashtable_init(&pool->ht, (scc_hashtable_hash_func_t)scc_strhash32,
|
||||
(scc_hashtable_equal_func_t)scc_strcmp);
|
||||
scc_hashtable_cstr_init(&pool->ht);
|
||||
}
|
||||
|
||||
const char *scc_strpool_intern(scc_strpool_t *pool, const char *str) {
|
||||
|
||||
Reference in New Issue
Block a user