refactor(argparse): 将null替换为nullptr以提高C++兼容性
- 在argparse库中将所有null指针常量替换为nullptr - 更新头文件和源文件中的指针初始化和比较操作 - 修改测试文件中的相关断言检查 - 更新AST定义文件中的注释说明
This commit is contained in:
@@ -68,15 +68,32 @@ SCC_IR_BUILDER_TYPE_FUNC(f32)
|
||||
SCC_IR_BUILDER_TYPE_FUNC(f64)
|
||||
SCC_IR_BUILDER_TYPE_FUNC(f128)
|
||||
|
||||
static inline scc_ir_value_ref_t
|
||||
scc_ir_builder_builtin_memcpy(scc_ir_builder_t *builder,
|
||||
scc_ir_value_ref_t dest, scc_ir_value_ref_t src,
|
||||
scc_ir_value_ref_t len) {
|
||||
scc_ir_value_t value;
|
||||
scc_ir_value_init(&value, nullptr, SCC_IR_VALUE_TAG_BUILTIN);
|
||||
value.type = scc_ir_builder_type_void(builder); // memcpy 返回 void*
|
||||
value.data.builtin.tag = SCC_IR_BUILTIN_TAG_MEMCPY;
|
||||
value.data.builtin.func.memcpy.dest = dest;
|
||||
value.data.builtin.func.memcpy.src = src;
|
||||
value.data.builtin.func.memcpy.size = len;
|
||||
scc_ir_value_ref_t ref =
|
||||
scc_ir_module_add_value(builder->ctx.module, &value);
|
||||
scc_ir_builder_add_instr(builder, ref);
|
||||
return ref;
|
||||
}
|
||||
|
||||
// TODO
|
||||
static inline scc_ir_value_ref_t
|
||||
scc_ir_builder_const_int(scc_ir_builder_t *builder, scc_ir_type_ref_t type,
|
||||
scc_ir_const_int_t value) {
|
||||
scc_ir_value_t node;
|
||||
scc_ir_node_init(&node, null, SCC_IR_VALUE_TAG_CONST_INT);
|
||||
node.data.const_int = value;
|
||||
node.type = type;
|
||||
return scc_ir_module_add_value(&builder->cprog->module, &node);
|
||||
scc_ir_const_int_t val) {
|
||||
scc_ir_value_t value;
|
||||
scc_ir_value_init(&value, nullptr, SCC_IR_VALUE_TAG_CONST_INT);
|
||||
value.data.const_int = val;
|
||||
value.type = type;
|
||||
return scc_ir_module_add_value(&builder->cprog->module, &value);
|
||||
}
|
||||
|
||||
static inline scc_ir_value_ref_t
|
||||
@@ -86,17 +103,11 @@ scc_ir_builder_const_string(scc_ir_builder_t *builder, const char *str,
|
||||
scc_ir_type_t array_type = {
|
||||
.tag = SCC_IR_TYPE_ARRAY,
|
||||
.data.array.base = u8_type,
|
||||
.data.array.len = len - 1, // 包含 null 结尾
|
||||
.data.array.len = len - 1, // 包含 nullptr 结尾
|
||||
};
|
||||
scc_ir_type_ref_t array_type_ref =
|
||||
scc_ir_ctx_get_type(&builder->ctx, &array_type);
|
||||
|
||||
// 2. 创建指针类型:指向 array_type
|
||||
scc_ir_type_t ptr_type = {.tag = SCC_IR_TYPE_PTR,
|
||||
.data.pointer.base = u8_type};
|
||||
scc_ir_type_ref_t ptr_type_ref =
|
||||
scc_ir_ctx_get_type(&builder->ctx, &ptr_type);
|
||||
|
||||
// 5. 创建聚合节点
|
||||
scc_ir_value_t const_array_value = {
|
||||
.tag = SCC_IR_VALUE_TAG_CONST_ARRAY,
|
||||
@@ -111,10 +122,10 @@ scc_ir_builder_const_string(scc_ir_builder_t *builder, const char *str,
|
||||
}
|
||||
buff[len - 2] = '\0';
|
||||
scc_vec_unsafe_from_buffer(const_array_value.data.const_array.elements,
|
||||
buff, len - 1);
|
||||
(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_NULL);
|
||||
Assert(const_array_ref != SCC_IR_REF_nullptr);
|
||||
|
||||
// 3. 创建全局变量节点,类型为指针,初始值指向常量数组
|
||||
char *name = scc_malloc(32);
|
||||
@@ -124,7 +135,7 @@ scc_ir_builder_const_string(scc_ir_builder_t *builder, const char *str,
|
||||
builder->ctx.module, &(scc_ir_value_t){
|
||||
.name = name,
|
||||
.tag = SCC_IR_VALUE_TAG_GLOBAL_ALLOC,
|
||||
.type = ptr_type_ref,
|
||||
.type = array_type_ref,
|
||||
.data.global_alloc.value = const_array_ref,
|
||||
});
|
||||
scc_snprintf(name, 32, "$G%u", global_value_ref);
|
||||
@@ -135,7 +146,7 @@ scc_ir_builder_const_string(scc_ir_builder_t *builder, const char *str,
|
||||
builder->ctx.module, &(scc_ir_value_t){
|
||||
.tag = SCC_IR_VALUE_TAG_GET_PTR,
|
||||
.data.get_ptr.src_addr = global_value_ref,
|
||||
.data.get_ptr.index = SCC_IR_VALUE_TAG_NULL,
|
||||
.data.get_ptr.index = SCC_IR_VALUE_TAG_NULLPTR,
|
||||
});
|
||||
scc_ir_builder_add_instr(builder, pointer_to_global_value);
|
||||
return pointer_to_global_value;
|
||||
@@ -144,7 +155,7 @@ scc_ir_builder_const_string(scc_ir_builder_t *builder, const char *str,
|
||||
/**
|
||||
* @brief 开始构建函数
|
||||
* @param func_ref 函数引用
|
||||
* @param param_names 参数名列表(可为NULL)
|
||||
* @param param_names 参数名列表(可为nullptr)
|
||||
* @return void
|
||||
*/
|
||||
void scc_ir_builder_begin_func(scc_ir_builder_t *builder,
|
||||
@@ -164,7 +175,7 @@ scc_ir_func_ref_t scc_ir_builder_current_func(scc_ir_builder_t *builder);
|
||||
/**
|
||||
* @brief 创建一个新的基本块,并自动添加到当前函数中,但不改变当前块。
|
||||
* @param builder IR构建器
|
||||
* @param label 基本块标签(可为 NULL,自动生成)
|
||||
* @param label 基本块标签(可为 nullptr,自动生成)
|
||||
* @return 新基本块的引用
|
||||
*/
|
||||
scc_ir_bblock_ref_t scc_ir_builder_bblock(scc_ir_builder_t *builder,
|
||||
@@ -172,7 +183,7 @@ scc_ir_bblock_ref_t scc_ir_builder_bblock(scc_ir_builder_t *builder,
|
||||
|
||||
/**
|
||||
* @brief 开始构建新的基本块
|
||||
* @param label 基本块标签(可为NULL,自动生成)
|
||||
* @param label 基本块标签(可为nullptr,自动生成)
|
||||
* @return 基本块引用
|
||||
*/
|
||||
scc_ir_bblock_ref_t scc_ir_builder_begin_bblock(scc_ir_builder_t *builder,
|
||||
@@ -193,7 +204,7 @@ void scc_ir_builder_set_current_bblock(scc_ir_builder_t *builder,
|
||||
/**
|
||||
* @brief 创建alloca指令(在当前基本块中)
|
||||
* @param type 分配的类型
|
||||
* @param name 变量名(可为NULL)
|
||||
* @param name 变量名(可为nullptr)
|
||||
*/
|
||||
scc_ir_value_ref_t scc_ir_builder_alloca(scc_ir_builder_t *builder,
|
||||
scc_ir_type_ref_t type,
|
||||
|
||||
@@ -3,7 +3,7 @@
|
||||
|
||||
#include <scc_core.h>
|
||||
|
||||
#define SCC_IR_REF_NULL 0
|
||||
#define SCC_IR_REF_nullptr 0
|
||||
|
||||
typedef unsigned int ir_handle_t;
|
||||
typedef const char *scc_ir_label_t;
|
||||
@@ -86,7 +86,8 @@ struct scc_ir_func {
|
||||
};
|
||||
|
||||
typedef enum scc_ir_value_tag {
|
||||
SCC_IR_VALUE_TAG_NULL,
|
||||
SCC_IR_VALUE_TAG_NULLPTR,
|
||||
SCC_IR_VALUE_TAG_BUILTIN,
|
||||
SCC_IR_VALUE_TAG_CONST_INT,
|
||||
SCC_IR_VALUE_TAG_CONST_UINT,
|
||||
SCC_IR_VALUE_TAG_CONST_FLOAT,
|
||||
@@ -175,12 +176,60 @@ typedef union {
|
||||
u8 float_any[16];
|
||||
} scc_ir_const_float_t;
|
||||
|
||||
typedef enum {
|
||||
SCC_IR_BUILTIN_TAG_MEMCPY,
|
||||
SCC_IR_BUILTIN_TAG_MEMSET,
|
||||
SCC_IR_BUILTIN_TAG_VA_START,
|
||||
SCC_IR_BUILTIN_TAG_VA_ARG,
|
||||
SCC_IR_BUILTIN_TAG_VA_END,
|
||||
SCC_IR_BUILTIN_TAG_VA_COPY,
|
||||
} scc_ir_builtin_tag_t;
|
||||
|
||||
typedef struct {
|
||||
scc_ir_builtin_tag_t tag;
|
||||
union {
|
||||
struct {
|
||||
scc_ir_value_ref_t dest;
|
||||
scc_ir_value_ref_t src;
|
||||
scc_ir_value_ref_t size;
|
||||
} memcpy;
|
||||
struct {
|
||||
scc_ir_value_ref_t dest;
|
||||
scc_ir_value_ref_t value;
|
||||
scc_ir_value_ref_t size;
|
||||
} memset;
|
||||
struct {
|
||||
scc_ir_value_ref_t ap; // va_list 的地址(i8* 或 struct*)
|
||||
scc_ir_value_ref_t last; // 最后一个固定参数的引用(用于 va_start)
|
||||
} va_start;
|
||||
struct {
|
||||
scc_ir_value_ref_t ap; // va_list 的地址
|
||||
scc_ir_type_ref_t type; // 要提取的参数的类型
|
||||
} va_arg;
|
||||
struct {
|
||||
scc_ir_value_ref_t ap; // va_list 的地址
|
||||
} va_end;
|
||||
struct {
|
||||
scc_ir_value_ref_t dest; // 目标 va_list 地址
|
||||
scc_ir_value_ref_t src; // 源 va_list 地址
|
||||
} va_copy;
|
||||
} func;
|
||||
} scc_ir_builtin_t;
|
||||
|
||||
typedef enum {
|
||||
SCC_IR_LINKAGE_EXTERNAL,
|
||||
SCC_IR_LINKAGE_INTERNAL,
|
||||
SCC_IR_LINKAGE_PRIVATE,
|
||||
SCC_IR_LINKAGE_WEAK,
|
||||
} scc_ir_linkage_t;
|
||||
|
||||
struct scc_ir_value {
|
||||
scc_ir_type_ref_t type;
|
||||
scc_ir_label_t name;
|
||||
scc_ir_node_ref_vec_t used_by;
|
||||
scc_ir_value_tag_t tag;
|
||||
union {
|
||||
scc_ir_builtin_t builtin;
|
||||
scc_ir_const_int_t const_int;
|
||||
scc_ir_const_uint_t const_uint;
|
||||
scc_ir_const_float_t const_float;
|
||||
|
||||
@@ -42,7 +42,7 @@ scc_ir_func_t *scc_ir_module_get_func(scc_ir_module_t *ctx,
|
||||
static inline scc_ir_type_t *
|
||||
scc_ir_module_get_type_by_value(scc_ir_module_t *ctx, scc_ir_value_ref_t ref) {
|
||||
scc_ir_value_t *value = scc_ir_module_get_value(ctx, ref);
|
||||
Assert(value != null);
|
||||
Assert(value != nullptr);
|
||||
return scc_ir_module_get_type(ctx, value->type);
|
||||
}
|
||||
|
||||
|
||||
@@ -8,8 +8,8 @@ void scc_ir_type_init(scc_ir_type_t *in, scc_ir_type_tag_t tag);
|
||||
void scc_ir_bblock_init(scc_ir_bblock_t *in, const char *label);
|
||||
void scc_ir_func_init(scc_ir_func_t *in, const char *name);
|
||||
|
||||
// node name can be null ptr
|
||||
void scc_ir_node_init(scc_ir_value_t *in, const char *name,
|
||||
scc_ir_value_tag_t tag);
|
||||
// node name can be nullptr ptr
|
||||
void scc_ir_value_init(scc_ir_value_t *in, const char *name,
|
||||
scc_ir_value_tag_t tag);
|
||||
|
||||
#endif /* __SCC_IR_H__ */
|
||||
|
||||
@@ -4,8 +4,8 @@
|
||||
#define GET_MODULE(builder) (&(builder->cprog->module))
|
||||
|
||||
void scc_ir_builder_init(scc_ir_builder_t *builder, scc_ir_cprog_t *cprog) {
|
||||
builder->current_bblock = SCC_IR_REF_NULL;
|
||||
builder->current_func = SCC_IR_REF_NULL;
|
||||
builder->current_bblock = SCC_IR_REF_nullptr;
|
||||
builder->current_func = SCC_IR_REF_nullptr;
|
||||
builder->cprog = cprog;
|
||||
|
||||
scc_ir_ctx_init(&builder->ctx, GET_MODULE(builder));
|
||||
@@ -40,12 +40,12 @@ void scc_ir_builder_begin_func(scc_ir_builder_t *builder,
|
||||
scc_ir_type_t *func_type =
|
||||
scc_ir_module_get_type(GET_MODULE(builder), func_ptr->type);
|
||||
|
||||
if (func_type == null || func_type->tag != SCC_IR_TYPE_FUNC) {
|
||||
if (func_type == nullptr || func_type->tag != SCC_IR_TYPE_FUNC) {
|
||||
LOG_ERROR("Invalid function type");
|
||||
return;
|
||||
}
|
||||
|
||||
if (func_ptr == null) {
|
||||
if (func_ptr == nullptr) {
|
||||
LOG_ERROR("Invalid function reference");
|
||||
return;
|
||||
}
|
||||
@@ -57,7 +57,7 @@ void scc_ir_builder_begin_func(scc_ir_builder_t *builder,
|
||||
}
|
||||
|
||||
scc_ir_type_ref_vec_t params = func_type->data.function.params;
|
||||
func_type = null;
|
||||
func_type = nullptr;
|
||||
scc_vec_foreach(params, i) {
|
||||
scc_ir_type_ref_t param_type = scc_vec_at(params, i);
|
||||
|
||||
@@ -67,7 +67,7 @@ void scc_ir_builder_begin_func(scc_ir_builder_t *builder,
|
||||
GET_MODULE(builder),
|
||||
&(scc_ir_type_t){.tag = SCC_IR_TYPE_PTR,
|
||||
.data.pointer.base = param_type});
|
||||
param_node.name = param_names ? param_names[i] : null;
|
||||
param_node.name = param_names ? param_names[i] : nullptr;
|
||||
param_node.data.arg_ref.idx = i;
|
||||
scc_vec_init(param_node.used_by);
|
||||
|
||||
@@ -81,7 +81,7 @@ void scc_ir_builder_begin_func(scc_ir_builder_t *builder,
|
||||
void scc_ir_builder_end_func(scc_ir_builder_t *builder) {
|
||||
scc_ir_func_t *func_ptr =
|
||||
scc_ir_module_get_func(GET_MODULE(builder), builder->current_func);
|
||||
if (func_ptr == null) {
|
||||
if (func_ptr == nullptr) {
|
||||
LOG_FATAL("Invalid function reference");
|
||||
return;
|
||||
}
|
||||
@@ -157,30 +157,30 @@ scc_ir_value_ref_t scc_ir_builder_alloca(scc_ir_builder_t *builder,
|
||||
&(scc_ir_type_t){.tag = SCC_IR_TYPE_PTR, .data.pointer.base = type});
|
||||
alloc_node.name = name;
|
||||
|
||||
scc_ir_value_ref_t node_ref =
|
||||
scc_ir_value_ref_t value_ref =
|
||||
scc_ir_module_add_value(GET_MODULE(builder), &alloc_node);
|
||||
|
||||
// 添加到当前基本块
|
||||
scc_ir_builder_add_instr(builder, node_ref);
|
||||
scc_ir_builder_add_instr(builder, value_ref);
|
||||
|
||||
return node_ref;
|
||||
return value_ref;
|
||||
}
|
||||
|
||||
scc_ir_value_ref_t scc_ir_builder_func_arg_ref(scc_ir_builder_t *builder,
|
||||
scc_ir_type_ref_t type,
|
||||
const char *name,
|
||||
usize arg_idx) {
|
||||
scc_ir_value_t node = {0};
|
||||
node.tag = SCC_IR_VALUE_TAG_FUNC_ARG_REF;
|
||||
node.type = type;
|
||||
node.name = name;
|
||||
node.data.arg_ref.idx = arg_idx;
|
||||
scc_ir_value_t value = {0};
|
||||
value.tag = SCC_IR_VALUE_TAG_FUNC_ARG_REF;
|
||||
value.type = type;
|
||||
value.name = name;
|
||||
value.data.arg_ref.idx = arg_idx;
|
||||
|
||||
scc_ir_value_ref_t node_ref =
|
||||
scc_ir_module_add_value(GET_MODULE(builder), &node);
|
||||
scc_ir_value_ref_t value_ref =
|
||||
scc_ir_module_add_value(GET_MODULE(builder), &value);
|
||||
// 添加到当前基本块
|
||||
scc_ir_builder_add_instr(builder, node_ref);
|
||||
return node_ref;
|
||||
scc_ir_builder_add_instr(builder, value_ref);
|
||||
return value_ref;
|
||||
}
|
||||
|
||||
scc_ir_value_ref_t scc_ir_builder_load(scc_ir_builder_t *builder,
|
||||
@@ -200,31 +200,31 @@ scc_ir_value_ref_t scc_ir_builder_load(scc_ir_builder_t *builder,
|
||||
}
|
||||
}
|
||||
|
||||
scc_ir_value_ref_t node_ref =
|
||||
scc_ir_value_ref_t value_ref =
|
||||
scc_ir_module_add_value(GET_MODULE(builder), &load_node);
|
||||
|
||||
// 添加到当前基本块
|
||||
scc_ir_builder_add_instr(builder, node_ref);
|
||||
scc_ir_builder_add_instr(builder, value_ref);
|
||||
|
||||
return node_ref;
|
||||
return value_ref;
|
||||
}
|
||||
|
||||
scc_ir_value_ref_t scc_ir_builder_store(scc_ir_builder_t *builder,
|
||||
scc_ir_value_ref_t target,
|
||||
scc_ir_value_ref_t value) {
|
||||
Assert(target != SCC_IR_REF_NULL && value != SCC_IR_REF_NULL);
|
||||
Assert(target != SCC_IR_REF_nullptr && value != SCC_IR_REF_nullptr);
|
||||
scc_ir_value_t store_node = {0};
|
||||
store_node.tag = SCC_IR_VALUE_TAG_STORE;
|
||||
store_node.data.store.target = target;
|
||||
store_node.data.store.value = value;
|
||||
|
||||
scc_ir_value_ref_t node_ref =
|
||||
scc_ir_value_ref_t value_ref =
|
||||
scc_ir_module_add_value(GET_MODULE(builder), &store_node);
|
||||
|
||||
// 添加到当前基本块
|
||||
scc_ir_builder_add_instr(builder, node_ref);
|
||||
scc_ir_builder_add_instr(builder, value_ref);
|
||||
|
||||
return node_ref;
|
||||
return value_ref;
|
||||
}
|
||||
|
||||
scc_ir_value_ref_t scc_ir_builder_get_ptr(scc_ir_builder_t *builder,
|
||||
@@ -242,13 +242,13 @@ scc_ir_value_ref_t scc_ir_builder_get_ptr(scc_ir_builder_t *builder,
|
||||
get_ptr_node.type = src_node->type;
|
||||
}
|
||||
|
||||
scc_ir_value_ref_t node_ref =
|
||||
scc_ir_value_ref_t value_ref =
|
||||
scc_ir_module_add_value(GET_MODULE(builder), &get_ptr_node);
|
||||
|
||||
// 添加到当前基本块
|
||||
scc_ir_builder_add_instr(builder, node_ref);
|
||||
scc_ir_builder_add_instr(builder, value_ref);
|
||||
|
||||
return node_ref;
|
||||
return value_ref;
|
||||
}
|
||||
|
||||
scc_ir_value_ref_t scc_ir_builder_binop(scc_ir_builder_t *builder,
|
||||
@@ -268,13 +268,13 @@ scc_ir_value_ref_t scc_ir_builder_binop(scc_ir_builder_t *builder,
|
||||
binop_node.type = lhs_node->type;
|
||||
}
|
||||
|
||||
scc_ir_value_ref_t node_ref =
|
||||
scc_ir_value_ref_t value_ref =
|
||||
scc_ir_module_add_value(GET_MODULE(builder), &binop_node);
|
||||
|
||||
// 添加到当前基本块
|
||||
scc_ir_builder_add_instr(builder, node_ref);
|
||||
scc_ir_builder_add_instr(builder, value_ref);
|
||||
|
||||
return node_ref;
|
||||
return value_ref;
|
||||
}
|
||||
|
||||
scc_ir_value_ref_t scc_ir_builder_cmp(scc_ir_builder_t *builder,
|
||||
@@ -291,13 +291,13 @@ scc_ir_value_ref_t scc_ir_builder_cmp(scc_ir_builder_t *builder,
|
||||
cmp_node.type =
|
||||
0; // FIXME scc_ir_module_get_builtin_i32(GET_MODULE(builder));
|
||||
|
||||
scc_ir_value_ref_t node_ref =
|
||||
scc_ir_value_ref_t value_ref =
|
||||
scc_ir_module_add_value(GET_MODULE(builder), &cmp_node);
|
||||
|
||||
// 添加到当前基本块
|
||||
scc_ir_builder_add_instr(builder, node_ref);
|
||||
scc_ir_builder_add_instr(builder, value_ref);
|
||||
|
||||
return node_ref;
|
||||
return value_ref;
|
||||
}
|
||||
|
||||
scc_ir_value_ref_t scc_ir_builder_jump(scc_ir_builder_t *builder,
|
||||
@@ -306,13 +306,13 @@ scc_ir_value_ref_t scc_ir_builder_jump(scc_ir_builder_t *builder,
|
||||
jump_node.tag = SCC_IR_VALUE_TAG_JUMP;
|
||||
jump_node.data.jump.target_bblock = target;
|
||||
|
||||
scc_ir_value_ref_t node_ref =
|
||||
scc_ir_value_ref_t value_ref =
|
||||
scc_ir_module_add_value(GET_MODULE(builder), &jump_node);
|
||||
|
||||
// 添加到当前基本块
|
||||
scc_ir_builder_add_instr(builder, node_ref);
|
||||
scc_ir_builder_add_instr(builder, value_ref);
|
||||
|
||||
return node_ref;
|
||||
return value_ref;
|
||||
}
|
||||
|
||||
scc_ir_value_ref_t scc_ir_builder_branch(scc_ir_builder_t *builder,
|
||||
@@ -325,13 +325,13 @@ scc_ir_value_ref_t scc_ir_builder_branch(scc_ir_builder_t *builder,
|
||||
branch_node.data.branch.true_bblock = true_target;
|
||||
branch_node.data.branch.false_bblock = false_target;
|
||||
|
||||
scc_ir_value_ref_t node_ref =
|
||||
scc_ir_value_ref_t value_ref =
|
||||
scc_ir_module_add_value(GET_MODULE(builder), &branch_node);
|
||||
|
||||
// 添加到当前基本块
|
||||
scc_ir_builder_add_instr(builder, node_ref);
|
||||
scc_ir_builder_add_instr(builder, value_ref);
|
||||
|
||||
return node_ref;
|
||||
return value_ref;
|
||||
}
|
||||
|
||||
scc_ir_value_ref_t scc_ir_builder_call(scc_ir_builder_t *builder,
|
||||
@@ -358,13 +358,13 @@ scc_ir_value_ref_t scc_ir_builder_call(scc_ir_builder_t *builder,
|
||||
}
|
||||
}
|
||||
|
||||
scc_ir_value_ref_t node_ref =
|
||||
scc_ir_value_ref_t value_ref =
|
||||
scc_ir_module_add_value(GET_MODULE(builder), &call_node);
|
||||
|
||||
// 添加到当前基本块
|
||||
scc_ir_builder_add_instr(builder, node_ref);
|
||||
scc_ir_builder_add_instr(builder, value_ref);
|
||||
|
||||
return node_ref;
|
||||
return value_ref;
|
||||
}
|
||||
|
||||
scc_ir_value_ref_t scc_ir_builder_ret(scc_ir_builder_t *builder,
|
||||
@@ -373,13 +373,13 @@ scc_ir_value_ref_t scc_ir_builder_ret(scc_ir_builder_t *builder,
|
||||
ret_node.tag = SCC_IR_VALUE_TAG_RET;
|
||||
ret_node.data.ret.ret_val = value;
|
||||
|
||||
scc_ir_value_ref_t node_ref =
|
||||
scc_ir_value_ref_t value_ref =
|
||||
scc_ir_module_add_value(GET_MODULE(builder), &ret_node);
|
||||
|
||||
// 添加到当前基本块
|
||||
scc_ir_builder_add_instr(builder, node_ref);
|
||||
scc_ir_builder_add_instr(builder, value_ref);
|
||||
|
||||
return node_ref;
|
||||
return value_ref;
|
||||
}
|
||||
|
||||
scc_ir_value_ref_t scc_ir_builder_ret_void(scc_ir_builder_t *builder) {
|
||||
@@ -387,10 +387,10 @@ scc_ir_value_ref_t scc_ir_builder_ret_void(scc_ir_builder_t *builder) {
|
||||
ret_node.tag = SCC_IR_VALUE_TAG_RET;
|
||||
ret_node.data.ret.ret_val = 0; // 无返回值
|
||||
|
||||
scc_ir_value_ref_t node_ref =
|
||||
scc_ir_value_ref_t value_ref =
|
||||
scc_ir_module_add_value(GET_MODULE(builder), &ret_node);
|
||||
|
||||
// 添加到当前基本块
|
||||
scc_ir_builder_add_instr(builder, node_ref);
|
||||
return node_ref;
|
||||
scc_ir_builder_add_instr(builder, value_ref);
|
||||
return value_ref;
|
||||
}
|
||||
|
||||
@@ -88,21 +88,17 @@ static int cmp_type(const void *_key1, const void *_key2) {
|
||||
return 0; // 基本类型,tag相同即可
|
||||
case SCC_IR_TYPE_PTR:
|
||||
return key1->data.pointer.base != key2->data.pointer.base;
|
||||
|
||||
case SCC_IR_TYPE_ARRAY:
|
||||
return (key1->data.array.base != key2->data.array.base) ||
|
||||
(key1->data.array.len != key2->data.array.len);
|
||||
|
||||
case SCC_IR_TYPE_FUNC: {
|
||||
if (key1->data.function.ret_type != key2->data.function.ret_type) {
|
||||
return 1;
|
||||
}
|
||||
|
||||
if (key1->data.function.params.size !=
|
||||
key2->data.function.params.size) {
|
||||
return 1;
|
||||
}
|
||||
|
||||
for (usize i = 0; i < key1->data.function.params.size; i++) {
|
||||
if (key1->data.function.params.data[i] !=
|
||||
key2->data.function.params.data[i]) {
|
||||
@@ -136,6 +132,7 @@ void scc_ir_ctx_drop(scc_ir_ctx_t *ctx) {
|
||||
|
||||
scc_ir_type_ref_t scc_ir_ctx_get_type(scc_ir_ctx_t *ctx,
|
||||
const scc_ir_type_t *type_desc) {
|
||||
Assert(type_desc->tag != SCC_IR_TYPE_unknown);
|
||||
// 先查哈希表
|
||||
void *found = scc_hashtable_get(&ctx->type_uniquing, (void *)type_desc);
|
||||
if (found) {
|
||||
|
||||
@@ -6,7 +6,8 @@
|
||||
|
||||
static const char *get_node_type_str(scc_ir_value_tag_t tag) {
|
||||
static const char *node_types[] = {
|
||||
[SCC_IR_VALUE_TAG_NULL] = "Null",
|
||||
[SCC_IR_VALUE_TAG_NULLPTR] = "NullPtr",
|
||||
[SCC_IR_VALUE_TAG_BUILTIN] = "Builtin",
|
||||
[SCC_IR_VALUE_TAG_CONST_INT] = "ConstInt",
|
||||
[SCC_IR_VALUE_TAG_CONST_UINT] = "ConstUint",
|
||||
[SCC_IR_VALUE_TAG_CONST_FLOAT] = "ConstFloat",
|
||||
@@ -81,56 +82,57 @@ static inline void dump_child_node_ref(scc_ir_dump_ctx_t *ctx,
|
||||
}
|
||||
|
||||
static void dump_const_int_node(scc_ir_dump_ctx_t *ctx,
|
||||
const scc_ir_value_t *node) {
|
||||
const scc_ir_value_t *value) {
|
||||
scc_tree_dump_push(ctx->dump_ctx, true);
|
||||
scc_tree_dump_begin_line(ctx->dump_ctx);
|
||||
scc_tree_dump_value(ctx->dump_ctx, "%d", node->data.const_int.int32);
|
||||
scc_tree_dump_value(ctx->dump_ctx, "%d", value->data.const_int.int32);
|
||||
scc_tree_dump_pop(ctx->dump_ctx);
|
||||
}
|
||||
|
||||
static void dump_op_node(scc_ir_dump_ctx_t *ctx, const scc_ir_value_t *node) {
|
||||
static void dump_op_node(scc_ir_dump_ctx_t *ctx, const scc_ir_value_t *value) {
|
||||
scc_tree_dump_push(ctx->dump_ctx, false);
|
||||
scc_tree_dump_begin_line(ctx->dump_ctx);
|
||||
scc_tree_dump_node(ctx->dump_ctx, "op: ");
|
||||
scc_tree_dump_value(ctx->dump_ctx, "%s", get_op_str(node->data.op.op));
|
||||
scc_tree_dump_value(ctx->dump_ctx, "%s", get_op_str(value->data.op.op));
|
||||
scc_tree_dump_pop(ctx->dump_ctx);
|
||||
|
||||
if (node->data.op.lhs)
|
||||
dump_child_node_ref(ctx, node->data.op.lhs,
|
||||
node->data.op.rhs ? false : true);
|
||||
if (node->data.op.rhs)
|
||||
dump_child_node_ref(ctx, node->data.op.rhs, true);
|
||||
if (value->data.op.lhs)
|
||||
dump_child_node_ref(ctx, value->data.op.lhs,
|
||||
value->data.op.rhs ? false : true);
|
||||
if (value->data.op.rhs)
|
||||
dump_child_node_ref(ctx, value->data.op.rhs, true);
|
||||
}
|
||||
|
||||
static void dump_load_node(scc_ir_dump_ctx_t *ctx, const scc_ir_value_t *node) {
|
||||
if (node->data.load.target)
|
||||
dump_child_node_ref(ctx, node->data.load.target, true);
|
||||
static void dump_load_node(scc_ir_dump_ctx_t *ctx,
|
||||
const scc_ir_value_t *value) {
|
||||
if (value->data.load.target)
|
||||
dump_child_node_ref(ctx, value->data.load.target, true);
|
||||
}
|
||||
|
||||
static void dump_store_node(scc_ir_dump_ctx_t *ctx,
|
||||
const scc_ir_value_t *node) {
|
||||
if (node->data.store.target)
|
||||
dump_child_node_ref(ctx, node->data.store.target, false);
|
||||
if (node->data.store.value)
|
||||
dump_child_node_ref(ctx, node->data.store.value, true);
|
||||
const scc_ir_value_t *value) {
|
||||
if (value->data.store.target)
|
||||
dump_child_node_ref(ctx, value->data.store.target, false);
|
||||
if (value->data.store.value)
|
||||
dump_child_node_ref(ctx, value->data.store.value, true);
|
||||
}
|
||||
|
||||
static void dump_get_ptr_node(scc_ir_dump_ctx_t *ctx,
|
||||
const scc_ir_value_t *node) {
|
||||
if (node->data.get_ptr.src_addr)
|
||||
dump_child_node_ref(ctx, node->data.get_ptr.src_addr, false);
|
||||
if (node->data.get_ptr.index)
|
||||
dump_child_node_ref(ctx, node->data.get_ptr.index, true);
|
||||
const scc_ir_value_t *value) {
|
||||
if (value->data.get_ptr.src_addr)
|
||||
dump_child_node_ref(ctx, value->data.get_ptr.src_addr, false);
|
||||
if (value->data.get_ptr.index)
|
||||
dump_child_node_ref(ctx, value->data.get_ptr.index, true);
|
||||
}
|
||||
|
||||
static void dump_branch_node(scc_ir_dump_ctx_t *ctx,
|
||||
const scc_ir_value_t *node) {
|
||||
if (node->data.branch.cond)
|
||||
dump_child_node_ref(ctx, node->data.branch.cond, false);
|
||||
const scc_ir_value_t *value) {
|
||||
if (value->data.branch.cond)
|
||||
dump_child_node_ref(ctx, value->data.branch.cond, false);
|
||||
|
||||
if (node->data.branch.true_bblock) {
|
||||
if (value->data.branch.true_bblock) {
|
||||
scc_ir_bblock_t *true_bblock = scc_ir_module_get_bblock(
|
||||
GET_MODULE(ctx), node->data.branch.true_bblock);
|
||||
GET_MODULE(ctx), value->data.branch.true_bblock);
|
||||
if (true_bblock) {
|
||||
scc_tree_dump_begin_line(ctx->dump_ctx);
|
||||
scc_tree_dump_node(ctx->dump_ctx, "TrueBlock: ");
|
||||
@@ -139,9 +141,9 @@ static void dump_branch_node(scc_ir_dump_ctx_t *ctx,
|
||||
: "<unnamed>");
|
||||
}
|
||||
}
|
||||
if (node->data.branch.false_bblock) {
|
||||
if (value->data.branch.false_bblock) {
|
||||
scc_ir_bblock_t *false_bblock = scc_ir_module_get_bblock(
|
||||
GET_MODULE(ctx), node->data.branch.false_bblock);
|
||||
GET_MODULE(ctx), value->data.branch.false_bblock);
|
||||
if (false_bblock) {
|
||||
scc_tree_dump_begin_line(ctx->dump_ctx);
|
||||
scc_tree_dump_node(ctx->dump_ctx, "FalseBlock: ");
|
||||
@@ -152,45 +154,47 @@ static void dump_branch_node(scc_ir_dump_ctx_t *ctx,
|
||||
}
|
||||
}
|
||||
|
||||
static void dump_jump_node(scc_ir_dump_ctx_t *ctx, const scc_ir_value_t *node) {
|
||||
static void dump_jump_node(scc_ir_dump_ctx_t *ctx,
|
||||
const scc_ir_value_t *value) {
|
||||
scc_tree_dump_begin_line(ctx->dump_ctx);
|
||||
if (node->data.jump.target_bblock) {
|
||||
if (value->data.jump.target_bblock) {
|
||||
scc_ir_bblock_t *target = scc_ir_module_get_bblock(
|
||||
GET_MODULE(ctx), node->data.jump.target_bblock);
|
||||
GET_MODULE(ctx), value->data.jump.target_bblock);
|
||||
if (target)
|
||||
scc_tree_dump_value(ctx->dump_ctx, "to '%s'",
|
||||
target->label ? target->label : "<unnamed>");
|
||||
else
|
||||
scc_tree_dump_value(ctx->dump_ctx, "to invalid block");
|
||||
} else {
|
||||
scc_tree_dump_value(ctx->dump_ctx, "to NULL");
|
||||
scc_tree_dump_value(ctx->dump_ctx, "to nullptr");
|
||||
}
|
||||
}
|
||||
|
||||
static void dump_call_node(scc_ir_dump_ctx_t *ctx, const scc_ir_value_t *node) {
|
||||
static void dump_call_node(scc_ir_dump_ctx_t *ctx,
|
||||
const scc_ir_value_t *value) {
|
||||
scc_tree_dump_begin_line(ctx->dump_ctx);
|
||||
if (node->data.call.callee) {
|
||||
if (value->data.call.callee) {
|
||||
scc_ir_func_t *callee =
|
||||
scc_ir_module_get_func(GET_MODULE(ctx), node->data.call.callee);
|
||||
scc_ir_module_get_func(GET_MODULE(ctx), value->data.call.callee);
|
||||
scc_tree_dump_value(ctx->dump_ctx, "func='%s'",
|
||||
callee ? (callee->name ? callee->name : "<unnamed>")
|
||||
: "<invalid>");
|
||||
} else {
|
||||
scc_tree_dump_value(ctx->dump_ctx, "func=NULL");
|
||||
scc_tree_dump_value(ctx->dump_ctx, "func=nullptr");
|
||||
}
|
||||
|
||||
for (usize i = 0; i < scc_vec_size(node->data.call.args); i++) {
|
||||
cbool is_last = (i + 1 == scc_vec_size(node->data.call.args));
|
||||
for (usize i = 0; i < scc_vec_size(value->data.call.args); i++) {
|
||||
cbool is_last = (i + 1 == scc_vec_size(value->data.call.args));
|
||||
scc_tree_dump_push(ctx->dump_ctx, is_last);
|
||||
scc_ir_value_ref_t arg = scc_vec_at(node->data.call.args, i);
|
||||
scc_ir_value_ref_t arg = scc_vec_at(value->data.call.args, i);
|
||||
dump_child_node_ref(ctx, arg, is_last);
|
||||
scc_tree_dump_pop(ctx->dump_ctx);
|
||||
}
|
||||
}
|
||||
|
||||
static void dump_ret_node(scc_ir_dump_ctx_t *ctx, const scc_ir_value_t *node) {
|
||||
if (node->data.ret.ret_val)
|
||||
dump_child_node_ref(ctx, node->data.ret.ret_val, true);
|
||||
static void dump_ret_node(scc_ir_dump_ctx_t *ctx, const scc_ir_value_t *value) {
|
||||
if (value->data.ret.ret_val)
|
||||
dump_child_node_ref(ctx, value->data.ret.ret_val, true);
|
||||
}
|
||||
|
||||
void scc_ir_dump_ctx_init(scc_ir_dump_ctx_t *ctx, scc_tree_dump_t *td,
|
||||
@@ -199,20 +203,20 @@ void scc_ir_dump_ctx_init(scc_ir_dump_ctx_t *ctx, scc_tree_dump_t *td,
|
||||
ctx->dump_ctx = td;
|
||||
}
|
||||
|
||||
void scc_ir_dump_value(scc_ir_dump_ctx_t *ctx, scc_ir_value_ref_t node_ref) {
|
||||
scc_ir_value_t *node = scc_ir_module_get_value(GET_MODULE(ctx), node_ref);
|
||||
if (!node) {
|
||||
LOG_ERROR("Invalid node ref");
|
||||
void scc_ir_dump_value(scc_ir_dump_ctx_t *ctx, scc_ir_value_ref_t value_ref) {
|
||||
scc_ir_value_t *value = scc_ir_module_get_value(GET_MODULE(ctx), value_ref);
|
||||
if (!value) {
|
||||
LOG_ERROR("Invalid value ref");
|
||||
return;
|
||||
}
|
||||
|
||||
scc_tree_dump_begin_line(ctx->dump_ctx);
|
||||
scc_tree_dump_node(ctx->dump_ctx, "%s", get_node_type_str(node->tag));
|
||||
if (node->name && node->name[0])
|
||||
scc_tree_dump_value(ctx->dump_ctx, " [%s]", node->name);
|
||||
if (node->type) {
|
||||
scc_tree_dump_node(ctx->dump_ctx, "%s", get_node_type_str(value->tag));
|
||||
if (value->name && value->name[0])
|
||||
scc_tree_dump_value(ctx->dump_ctx, " [%s]", value->name);
|
||||
if (value->type) {
|
||||
scc_ir_type_t *type =
|
||||
scc_ir_module_get_type(GET_MODULE(ctx), node->type);
|
||||
scc_ir_module_get_type(GET_MODULE(ctx), value->type);
|
||||
if (type) {
|
||||
scc_tree_dump_append(ctx->dump_ctx, " : ");
|
||||
scc_tree_dump_value(ctx->dump_ctx, "%s",
|
||||
@@ -220,39 +224,39 @@ void scc_ir_dump_value(scc_ir_dump_ctx_t *ctx, scc_ir_value_ref_t node_ref) {
|
||||
}
|
||||
}
|
||||
|
||||
switch (node->tag) {
|
||||
switch (value->tag) {
|
||||
case SCC_IR_VALUE_TAG_CONST_INT:
|
||||
dump_const_int_node(ctx, node);
|
||||
dump_const_int_node(ctx, value);
|
||||
break;
|
||||
case SCC_IR_VALUE_TAG_ALLOC:
|
||||
break;
|
||||
case SCC_IR_VALUE_TAG_LOAD:
|
||||
dump_load_node(ctx, node);
|
||||
dump_load_node(ctx, value);
|
||||
break;
|
||||
case SCC_IR_VALUE_TAG_STORE:
|
||||
dump_store_node(ctx, node);
|
||||
dump_store_node(ctx, value);
|
||||
break;
|
||||
case SCC_IR_VALUE_TAG_GET_PTR:
|
||||
dump_get_ptr_node(ctx, node);
|
||||
dump_get_ptr_node(ctx, value);
|
||||
break;
|
||||
case SCC_IR_VALUE_TAG_OP:
|
||||
dump_op_node(ctx, node);
|
||||
dump_op_node(ctx, value);
|
||||
break;
|
||||
case SCC_IR_VALUE_TAG_BRANCH:
|
||||
dump_branch_node(ctx, node);
|
||||
dump_branch_node(ctx, value);
|
||||
break;
|
||||
case SCC_IR_VALUE_TAG_JUMP:
|
||||
dump_jump_node(ctx, node);
|
||||
dump_jump_node(ctx, value);
|
||||
break;
|
||||
case SCC_IR_VALUE_TAG_CALL:
|
||||
dump_call_node(ctx, node);
|
||||
dump_call_node(ctx, value);
|
||||
break;
|
||||
case SCC_IR_VALUE_TAG_RET:
|
||||
dump_ret_node(ctx, node);
|
||||
dump_ret_node(ctx, value);
|
||||
break;
|
||||
default:
|
||||
scc_tree_dump_value(ctx->dump_ctx, "unknown");
|
||||
scc_tree_dump_append_fmt(ctx->dump_ctx, " tag(%d)", node->tag);
|
||||
scc_tree_dump_append_fmt(ctx->dump_ctx, " tag(%d)", value->tag);
|
||||
break;
|
||||
}
|
||||
}
|
||||
@@ -434,73 +438,99 @@ void scc_ir_dump_type_linear(scc_ir_dump_ctx_t *ctx,
|
||||
}
|
||||
}
|
||||
|
||||
static void format_node_ref_or_value(scc_ir_dump_ctx_t *ctx,
|
||||
scc_ir_value_ref_t node_ref) {
|
||||
scc_ir_value_t *node = scc_ir_module_get_value(GET_MODULE(ctx), node_ref);
|
||||
if (!node) {
|
||||
scc_tree_dump_append_fmt(ctx->dump_ctx, "%%%u", node_ref);
|
||||
static void format_ref_or_value(scc_ir_dump_ctx_t *ctx,
|
||||
scc_ir_value_ref_t value_ref) {
|
||||
scc_ir_value_t *value = scc_ir_module_get_value(GET_MODULE(ctx), value_ref);
|
||||
if (!value) {
|
||||
scc_tree_dump_append_fmt(ctx->dump_ctx, "%%%u", value_ref);
|
||||
return;
|
||||
}
|
||||
if (node->tag == SCC_IR_VALUE_TAG_CONST_INT) {
|
||||
if (value->tag == SCC_IR_VALUE_TAG_CONST_INT) {
|
||||
scc_tree_dump_append_fmt(ctx->dump_ctx, "%d",
|
||||
node->data.const_int.int32);
|
||||
value->data.const_int.int32);
|
||||
return;
|
||||
}
|
||||
if (node->name && node->name[0] != '\0') {
|
||||
scc_tree_dump_node(ctx->dump_ctx, "%%%u[%s]", node_ref, node->name);
|
||||
if (value->name && value->name[0] != '\0') {
|
||||
scc_tree_dump_node(ctx->dump_ctx, "%%%u[%s]", value_ref, value->name);
|
||||
} else {
|
||||
scc_tree_dump_node(ctx->dump_ctx, "%%%u", node_ref);
|
||||
scc_tree_dump_node(ctx->dump_ctx, "%%%u", value_ref);
|
||||
}
|
||||
if (node->type != SCC_IR_REF_NULL) {
|
||||
if (value->type != SCC_IR_REF_nullptr) {
|
||||
scc_tree_dump_append(ctx->dump_ctx, ":");
|
||||
scc_ir_dump_type_linear(ctx, node->type);
|
||||
scc_ir_dump_type_linear(ctx, value->type);
|
||||
}
|
||||
}
|
||||
|
||||
void scc_ir_dump_node_linear(scc_ir_dump_ctx_t *ctx,
|
||||
scc_ir_value_ref_t node_ref) {
|
||||
scc_ir_value_t *node = scc_ir_module_get_value(GET_MODULE(ctx), node_ref);
|
||||
if (!node) {
|
||||
scc_tree_dump_append(ctx->dump_ctx, "<invalid node>\n");
|
||||
void scc_ir_dump_value_linear(scc_ir_dump_ctx_t *ctx,
|
||||
scc_ir_value_ref_t value_ref) {
|
||||
scc_ir_value_t *value = scc_ir_module_get_value(GET_MODULE(ctx), value_ref);
|
||||
if (!value) {
|
||||
scc_tree_dump_append(ctx->dump_ctx, "<invalid value>\n");
|
||||
return;
|
||||
}
|
||||
|
||||
cbool needs_equals = (node->tag != SCC_IR_VALUE_TAG_BRANCH &&
|
||||
node->tag != SCC_IR_VALUE_TAG_JUMP &&
|
||||
node->tag != SCC_IR_VALUE_TAG_RET &&
|
||||
node->tag != SCC_IR_VALUE_TAG_STORE);
|
||||
cbool needs_equals = (value->tag != SCC_IR_VALUE_TAG_BRANCH &&
|
||||
value->tag != SCC_IR_VALUE_TAG_JUMP &&
|
||||
value->tag != SCC_IR_VALUE_TAG_RET &&
|
||||
value->tag != SCC_IR_VALUE_TAG_STORE);
|
||||
|
||||
if (needs_equals) {
|
||||
format_node_ref_or_value(ctx, node_ref);
|
||||
format_ref_or_value(ctx, value_ref);
|
||||
scc_tree_dump_append(ctx->dump_ctx, " = ");
|
||||
}
|
||||
|
||||
switch (node->tag) {
|
||||
switch (value->tag) {
|
||||
case SCC_IR_VALUE_TAG_BUILTIN: {
|
||||
switch (value->data.builtin.tag) {
|
||||
case SCC_IR_BUILTIN_TAG_MEMCPY:
|
||||
scc_tree_dump_append(ctx->dump_ctx, "memcpy");
|
||||
break;
|
||||
case SCC_IR_BUILTIN_TAG_MEMSET:
|
||||
scc_tree_dump_append(ctx->dump_ctx, "memset");
|
||||
break;
|
||||
case SCC_IR_BUILTIN_TAG_VA_ARG:
|
||||
scc_tree_dump_append(ctx->dump_ctx, "va_arg");
|
||||
break;
|
||||
case SCC_IR_BUILTIN_TAG_VA_END:
|
||||
scc_tree_dump_append(ctx->dump_ctx, "va_end");
|
||||
break;
|
||||
case SCC_IR_BUILTIN_TAG_VA_COPY:
|
||||
scc_tree_dump_append(ctx->dump_ctx, "va_copy");
|
||||
break;
|
||||
case SCC_IR_BUILTIN_TAG_VA_START:
|
||||
scc_tree_dump_append(ctx->dump_ctx, "va_start");
|
||||
break;
|
||||
default:
|
||||
Panic("Unknown builtin tag");
|
||||
break;
|
||||
}
|
||||
break;
|
||||
}
|
||||
case SCC_IR_VALUE_TAG_CONST_INT:
|
||||
// 值已经在 format 中输出,这里不需要再做
|
||||
break;
|
||||
case SCC_IR_VALUE_TAG_CONST_UINT: {
|
||||
scc_ir_type_t *type =
|
||||
scc_ir_module_get_type(GET_MODULE(ctx), node->type);
|
||||
Assert(type != null);
|
||||
scc_ir_module_get_type(GET_MODULE(ctx), value->type);
|
||||
Assert(type != nullptr);
|
||||
if (type->tag == SCC_IR_TYPE_u8) {
|
||||
scc_tree_dump_append_fmt(ctx->dump_ctx, "%c",
|
||||
node->data.const_uint.uint8);
|
||||
value->data.const_uint.uint8);
|
||||
} else {
|
||||
scc_tree_dump_append_fmt(ctx->dump_ctx, "%u",
|
||||
node->data.const_uint.uint32);
|
||||
value->data.const_uint.uint32);
|
||||
}
|
||||
break;
|
||||
}
|
||||
case SCC_IR_VALUE_TAG_CONST_FLOAT:
|
||||
scc_tree_dump_append_fmt(ctx->dump_ctx, "%f",
|
||||
node->data.const_float.float32);
|
||||
value->data.const_float.float32);
|
||||
break;
|
||||
case SCC_IR_VALUE_TAG_AGGREGATE:
|
||||
// 聚合类型:递归输出每个元素(每个占一行)
|
||||
scc_vec_foreach(node->data.aggregate.elements, i) {
|
||||
scc_ir_dump_node_linear(
|
||||
ctx, scc_vec_at(node->data.aggregate.elements, i));
|
||||
scc_vec_foreach(value->data.aggregate.elements, i) {
|
||||
scc_ir_dump_value_linear(
|
||||
ctx, scc_vec_at(value->data.aggregate.elements, i));
|
||||
scc_tree_dump_append(ctx->dump_ctx, "\n");
|
||||
}
|
||||
return;
|
||||
@@ -509,88 +539,89 @@ void scc_ir_dump_node_linear(scc_ir_dump_ctx_t *ctx,
|
||||
break;
|
||||
case SCC_IR_VALUE_TAG_LOAD:
|
||||
scc_tree_dump_append(ctx->dump_ctx, "load ");
|
||||
format_node_ref_or_value(ctx, node->data.load.target);
|
||||
format_ref_or_value(ctx, value->data.load.target);
|
||||
break;
|
||||
case SCC_IR_VALUE_TAG_STORE:
|
||||
scc_tree_dump_append(ctx->dump_ctx, "store ");
|
||||
format_node_ref_or_value(ctx, node->data.store.value);
|
||||
format_ref_or_value(ctx, value->data.store.value);
|
||||
scc_tree_dump_append(ctx->dump_ctx, " -> ");
|
||||
format_node_ref_or_value(ctx, node->data.store.target);
|
||||
format_ref_or_value(ctx, value->data.store.target);
|
||||
break;
|
||||
case SCC_IR_VALUE_TAG_GET_ELEM_PTR:
|
||||
case SCC_IR_VALUE_TAG_GET_PTR:
|
||||
scc_tree_dump_append(
|
||||
ctx->dump_ctx,
|
||||
node->tag == SCC_IR_VALUE_TAG_GET_PTR ? "getptr " : "getelemptr ");
|
||||
format_node_ref_or_value(ctx, node->data.get_ptr.src_addr);
|
||||
value->tag == SCC_IR_VALUE_TAG_GET_PTR ? "getptr " : "getelemptr ");
|
||||
format_ref_or_value(ctx, value->data.get_ptr.src_addr);
|
||||
scc_tree_dump_append(ctx->dump_ctx, ", ");
|
||||
format_node_ref_or_value(ctx, node->data.get_ptr.index);
|
||||
format_ref_or_value(ctx, value->data.get_ptr.index);
|
||||
break;
|
||||
case SCC_IR_VALUE_TAG_OP:
|
||||
format_node_ref_or_value(ctx, node->data.op.lhs);
|
||||
format_ref_or_value(ctx, value->data.op.lhs);
|
||||
scc_tree_dump_append_fmt(ctx->dump_ctx, " %s ",
|
||||
get_op_str(node->data.op.op));
|
||||
format_node_ref_or_value(ctx, node->data.op.rhs);
|
||||
get_op_str(value->data.op.op));
|
||||
format_ref_or_value(ctx, value->data.op.rhs);
|
||||
break;
|
||||
case SCC_IR_VALUE_TAG_BRANCH:
|
||||
if (node->data.branch.cond) {
|
||||
if (value->data.branch.cond) {
|
||||
scc_tree_dump_append(ctx->dump_ctx, "br ");
|
||||
format_node_ref_or_value(ctx, node->data.branch.cond);
|
||||
scc_tree_dump_append_fmt(
|
||||
ctx->dump_ctx, ", label %%L%u, label %%L%u",
|
||||
node->data.branch.true_bblock, node->data.branch.false_bblock);
|
||||
format_ref_or_value(ctx, value->data.branch.cond);
|
||||
scc_tree_dump_append_fmt(ctx->dump_ctx,
|
||||
", label %%L%u, label %%L%u",
|
||||
value->data.branch.true_bblock,
|
||||
value->data.branch.false_bblock);
|
||||
} else {
|
||||
scc_tree_dump_append_fmt(ctx->dump_ctx, "br label %%L%u",
|
||||
node->data.branch.true_bblock);
|
||||
value->data.branch.true_bblock);
|
||||
}
|
||||
break;
|
||||
case SCC_IR_VALUE_TAG_JUMP:
|
||||
scc_tree_dump_append_fmt(ctx->dump_ctx, "jmp label %%%u",
|
||||
node->data.jump.target_bblock);
|
||||
scc_tree_dump_append_fmt(ctx->dump_ctx, "jmp label %%L%u",
|
||||
value->data.jump.target_bblock);
|
||||
break;
|
||||
case SCC_IR_VALUE_TAG_CALL: {
|
||||
scc_ir_func_t *func =
|
||||
scc_ir_module_get_func(GET_MODULE(ctx), node->data.call.callee);
|
||||
scc_ir_module_get_func(GET_MODULE(ctx), value->data.call.callee);
|
||||
scc_tree_dump_append_fmt(ctx->dump_ctx, "call @%s(",
|
||||
func ? (func->name ? func->name : "<unnamed>")
|
||||
: "<invalid>");
|
||||
for (usize i = 0; i < scc_vec_size(node->data.call.args); i++) {
|
||||
for (usize i = 0; i < scc_vec_size(value->data.call.args); i++) {
|
||||
if (i > 0)
|
||||
scc_tree_dump_append(ctx->dump_ctx, ", ");
|
||||
format_node_ref_or_value(ctx, scc_vec_at(node->data.call.args, i));
|
||||
format_ref_or_value(ctx, scc_vec_at(value->data.call.args, i));
|
||||
}
|
||||
scc_tree_dump_append(ctx->dump_ctx, ")");
|
||||
break;
|
||||
}
|
||||
case SCC_IR_VALUE_TAG_RET:
|
||||
if (node->data.ret.ret_val != 0) {
|
||||
if (value->data.ret.ret_val != 0) {
|
||||
scc_tree_dump_append(ctx->dump_ctx, "ret ");
|
||||
format_node_ref_or_value(ctx, node->data.ret.ret_val);
|
||||
format_ref_or_value(ctx, value->data.ret.ret_val);
|
||||
} else {
|
||||
scc_tree_dump_append(ctx->dump_ctx, "ret void");
|
||||
}
|
||||
break;
|
||||
case SCC_IR_VALUE_TAG_FUNC_ARG_REF:
|
||||
scc_tree_dump_append_fmt(ctx->dump_ctx, "arg[%zu]",
|
||||
node->data.arg_ref.idx);
|
||||
value->data.arg_ref.idx);
|
||||
break;
|
||||
case SCC_IR_VALUE_TAG_GLOBAL_ALLOC:
|
||||
scc_tree_dump_append_fmt(ctx->dump_ctx, "global %s\n", node->name);
|
||||
scc_ir_dump_node_linear(ctx, node->data.global_alloc.value);
|
||||
scc_tree_dump_append_fmt(ctx->dump_ctx, "global %s\n", value->name);
|
||||
scc_ir_dump_value_linear(ctx, value->data.global_alloc.value);
|
||||
return;
|
||||
case SCC_IR_VALUE_TAG_CONST_ARRAY:
|
||||
scc_tree_dump_append(ctx->dump_ctx, "const_array ");
|
||||
scc_ir_dump_type_linear(ctx, node->data.const_array.base_type);
|
||||
scc_ir_dump_type_linear(ctx, value->data.const_array.base_type);
|
||||
scc_tree_dump_append(ctx->dump_ctx, " [");
|
||||
scc_vec_foreach(node->data.const_array.elements, i) {
|
||||
u8 ch = scc_vec_at(node->data.const_array.elements, i);
|
||||
scc_vec_foreach(value->data.const_array.elements, i) {
|
||||
u8 ch = scc_vec_at(value->data.const_array.elements, i);
|
||||
scc_tree_dump_append_fmt(ctx->dump_ctx, " `%c`, ", ch ? ch : ' ');
|
||||
}
|
||||
scc_tree_dump_append(ctx->dump_ctx, " ]");
|
||||
break;
|
||||
default:
|
||||
scc_tree_dump_append_fmt(ctx->dump_ctx, "<%s node %u>",
|
||||
get_node_type_str(node->tag), node_ref);
|
||||
scc_tree_dump_append_fmt(ctx->dump_ctx, "<%s value %u>",
|
||||
get_node_type_str(value->tag), value_ref);
|
||||
break;
|
||||
}
|
||||
}
|
||||
@@ -613,7 +644,7 @@ void scc_ir_dump_bblock_linear(scc_ir_dump_ctx_t *ctx,
|
||||
for (usize i = 0; i < scc_vec_size(bblock->instrs); i++) {
|
||||
scc_tree_dump_begin_line(ctx->dump_ctx);
|
||||
scc_tree_dump_append(ctx->dump_ctx, " ");
|
||||
scc_ir_dump_node_linear(ctx, scc_vec_at(bblock->instrs, i));
|
||||
scc_ir_dump_value_linear(ctx, scc_vec_at(bblock->instrs, i));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -668,7 +699,7 @@ void scc_ir_dump_func_linear(scc_ir_dump_ctx_t *ctx, scc_ir_func_ref_t func_ref,
|
||||
|
||||
void scc_ir_dump_cprog_linear(scc_ir_dump_ctx_t *ctx) {
|
||||
for (usize i = 0; i < scc_vec_size(ctx->cprog->global_vals); i++) {
|
||||
scc_ir_dump_node_linear(ctx, scc_vec_at(ctx->cprog->global_vals, i));
|
||||
scc_ir_dump_value_linear(ctx, scc_vec_at(ctx->cprog->global_vals, i));
|
||||
}
|
||||
for (usize i = 0; i < scc_vec_size(ctx->cprog->func_decls); i++) {
|
||||
scc_ir_func_ref_t func_decl = scc_vec_at(ctx->cprog->func_decls, i);
|
||||
|
||||
@@ -99,39 +99,39 @@ scc_ir_func_ref_t scc_ir_module_add_func(scc_ir_module_t *ctx,
|
||||
scc_ir_type_t *scc_ir_module_get_type(scc_ir_module_t *ctx,
|
||||
scc_ir_type_ref_t ref) {
|
||||
if (ref == 0)
|
||||
return null;
|
||||
return nullptr;
|
||||
usize idx = GET_ENTITY_INDEX(ctx, ref, uid2type);
|
||||
if (idx >= ctx->types.size)
|
||||
return null;
|
||||
return nullptr;
|
||||
return &ctx->types.data[idx];
|
||||
}
|
||||
|
||||
scc_ir_value_t *scc_ir_module_get_value(scc_ir_module_t *ctx,
|
||||
scc_ir_value_ref_t ref) {
|
||||
if (ref == 0)
|
||||
return null;
|
||||
return nullptr;
|
||||
usize idx = GET_ENTITY_INDEX(ctx, ref, uid2value);
|
||||
if (idx >= ctx->values.size)
|
||||
return null;
|
||||
return nullptr;
|
||||
return &ctx->values.data[idx];
|
||||
}
|
||||
|
||||
scc_ir_bblock_t *scc_ir_module_get_bblock(scc_ir_module_t *ctx,
|
||||
scc_ir_bblock_ref_t ref) {
|
||||
if (ref == 0)
|
||||
return null;
|
||||
return nullptr;
|
||||
usize idx = GET_ENTITY_INDEX(ctx, ref, uid2bblock);
|
||||
if (idx >= ctx->bblocks.size)
|
||||
return null;
|
||||
return nullptr;
|
||||
return &ctx->bblocks.data[idx];
|
||||
}
|
||||
|
||||
scc_ir_func_t *scc_ir_module_get_func(scc_ir_module_t *ctx,
|
||||
scc_ir_func_ref_t ref) {
|
||||
if (ref == 0)
|
||||
return null;
|
||||
return nullptr;
|
||||
usize idx = GET_ENTITY_INDEX(ctx, ref, uid2func);
|
||||
if (idx >= ctx->funcs.size)
|
||||
return null;
|
||||
return nullptr;
|
||||
return &ctx->funcs.data[idx];
|
||||
}
|
||||
|
||||
@@ -1,9 +1,9 @@
|
||||
#include <scc_ir.h>
|
||||
|
||||
void scc_ir_type_init(scc_ir_type_t *in, scc_ir_type_tag_t tag) {
|
||||
Assert(in != null);
|
||||
Assert(in != nullptr);
|
||||
in->tag = tag;
|
||||
in->name = null;
|
||||
in->name = nullptr;
|
||||
switch (tag) {
|
||||
case SCC_IR_TYPE_unknown:
|
||||
case SCC_IR_TYPE_void:
|
||||
@@ -40,31 +40,33 @@ void scc_ir_type_init(scc_ir_type_t *in, scc_ir_type_tag_t tag) {
|
||||
}
|
||||
|
||||
void scc_ir_bblock_init(scc_ir_bblock_t *in, const char *label) {
|
||||
Assert(in != null);
|
||||
Assert(label != null);
|
||||
Assert(in != nullptr);
|
||||
Assert(label != nullptr);
|
||||
in->label = label;
|
||||
scc_vec_init(in->instrs);
|
||||
}
|
||||
|
||||
void scc_ir_func_init(scc_ir_func_t *in, const char *name) {
|
||||
Assert(in != null);
|
||||
Assert(name != null);
|
||||
Assert(in != nullptr);
|
||||
Assert(name != nullptr);
|
||||
in->name = name;
|
||||
in->type = 0;
|
||||
scc_vec_init(in->bblocks);
|
||||
scc_vec_init(in->params);
|
||||
}
|
||||
|
||||
void scc_ir_node_init(scc_ir_value_t *in, const char *name,
|
||||
scc_ir_value_tag_t tag) {
|
||||
Assert(in != null);
|
||||
void scc_ir_value_init(scc_ir_value_t *in, const char *name,
|
||||
scc_ir_value_tag_t tag) {
|
||||
Assert(in != nullptr);
|
||||
in->name = name;
|
||||
in->tag = tag;
|
||||
scc_vec_init(in->used_by);
|
||||
in->type = 0;
|
||||
|
||||
switch (tag) {
|
||||
case SCC_IR_VALUE_TAG_NULL:
|
||||
case SCC_IR_VALUE_TAG_NULLPTR:
|
||||
break;
|
||||
case SCC_IR_VALUE_TAG_BUILTIN:
|
||||
break;
|
||||
case SCC_IR_VALUE_TAG_CONST_INT:
|
||||
// TODO
|
||||
|
||||
Reference in New Issue
Block a user