feat(ast2ir): 添加数组初始化支持和AST转IR优化

添加了对未知长度数组的自动长度推导功能,支持字符串字面量和复合
初始化的数组长度计算。新增辅助函数resolve_array_length用于计算
数组实际长度,以及emit_array_initialization用于生成数组初始化
代码。

同时将AST转IR过程中的参数改为const引用,提高代码安全性。
新增IR构建器的借用检查机制,防止在借用期间进行重分配操作。

fix(ast): 为AST结构体添加详细注释说明字段用途
This commit is contained in:
zzy
2026-04-13 11:36:52 +08:00
parent 694778e4a0
commit ffb23afaf4
13 changed files with 480 additions and 220 deletions

View File

@@ -20,8 +20,41 @@ struct scc_ir_builder {
scc_ir_ctx_t ctx; ///< 核心上下文
scc_ir_func_ref_t current_func; ///< 当前正在构建的函数
scc_ir_bblock_ref_t current_bblock; ///< 当前基本块
#ifndef SCC_NO_DEBUG
int borrow_depth;
const char *dbg_file;
int dbg_line;
#endif
};
#ifndef SCC_NO_DEBUG
#define SCC_IR_BUILDER_BEGIN_BORROW(builder, ptr_var, ptr_expr) \
do { \
(builder)->borrow_depth++; \
(builder)->dbg_file = __FILE__; \
(builder)->dbg_line = __LINE__; \
ptr_var = (ptr_expr); \
} while (0)
#define SCC_IR_BUILDER_END_BORROW(builder) \
do { \
(builder)->borrow_depth--; \
} while (0)
#define SCC_IR_BUILDER_CHECK_NO_BORROW(builder) \
do { \
if ((builder)->borrow_depth != 0) { \
Panic("IR Builder: attempt to reallocate while borrowed at %s:%d", \
(builder)->dbg_file, (builder)->dbg_line); \
} \
} while (0)
#else
#define SCC_IR_BUILDER_BEGIN_BORROW(builder, ptr_var, ptr_expr) \
ptr_var = (ptr_expr)
#define SCC_IR_BUILDER_END_BORROW(builder) ((void)0)
#define SCC_IR_BUILDER_CHECK_NO_BORROW(builder) ((void)0)
#endif
/**
* @brief 初始化 IR 构建器
*/
@@ -110,52 +143,8 @@ scc_ir_builder_const_int(scc_ir_builder_t *builder, scc_ir_type_ref_t type,
return scc_ir_module_add_value(&builder->cprog->module, &value);
}
static inline scc_ir_value_ref_t
scc_ir_builder_const_string(scc_ir_builder_t *builder, const char *str,
usize len) {
scc_ir_type_ref_t u8_type = scc_ir_builder_type_u8(builder);
scc_ir_type_t array_type = {
.tag = SCC_IR_TYPE_ARRAY,
.data.array.base = u8_type,
.data.array.len = len - 1, // 包含 nullptr 结尾
};
scc_ir_type_ref_t array_type_ref =
scc_ir_ctx_get_type(&builder->ctx, &array_type);
// 5. 创建聚合节点
scc_ir_value_t const_array_value = {
.tag = SCC_IR_VALUE_TAG_CONST_ARRAY,
.type = array_type_ref,
.data.const_array.base_type = u8_type,
};
char *buff = scc_malloc(len - 1);
Assert(buff);
// FIXME content to real string
for (usize i = 1; i < len - 1; i++) {
buff[i - 1] = str[i];
}
buff[len - 2] = '\0';
scc_vec_unsafe_from_buffer(const_array_value.data.const_array.fields,
(u8 *)buff, len - 1);
scc_ir_value_ref_t const_array_ref =
scc_ir_module_add_value(builder->ctx.module, &const_array_value);
Assert(const_array_ref != SCC_IR_REF_nullptr);
// 3. 创建全局变量节点,类型为指针,初始值指向常量数组
scc_ir_value_ref_t global_value_ref =
scc_ir_builder_global_alloca(builder, array_type_ref, const_array_ref);
// scc_hashtable_insert(builder);
scc_ir_value_ref_t pointer_to_global_value = scc_ir_module_add_value(
builder->ctx.module,
&(scc_ir_value_t){
.tag = SCC_IR_VALUE_TAG_GET_ELEM_PTR,
.data.get_elem_ptr.src_addr = global_value_ref,
.data.get_elem_ptr.index = SCC_IR_VALUE_TAG_NULLPTR,
});
scc_ir_builder_add_instr(builder, pointer_to_global_value);
return pointer_to_global_value;
}
scc_ir_value_ref_t scc_ir_builder_const_string(scc_ir_builder_t *builder,
const char *str, usize len);
/**
* @brief 开始构建函数