feat(ir): 启用LIR模块并重构HIR组件结构
- 在cbuild.toml中启用lir依赖项,取消注释相关配置 - 重构libs/README.md文档,添加详细的库说明和层级结构 - 重命名头文件以统一命名规范:ast_def.h → scc_ast_def.h, ast_dump.h → scc_ast_dump.h, hir相关文件添加scc前缀 - 更新include路径以匹配新的文件命名 - 在cfg模块中添加symbol ID类型和linkage枚举,完善符号表功能 - 实现cfg模块中的符号添加、查找和获取功能 - 修改HIR中meta字段替换原有的attribute字段,更新相关访问宏 - 修复HIR构建器中的函数元数据访问错误 - 为LIR模块创建完整的头文件结构,包括指令定义、转换器等组件
This commit is contained in:
@@ -10,7 +10,7 @@ dependencies = [
|
||||
{ name = "ast", path = "./libs/ast" },
|
||||
{ name = "ast2ir", path = "./libs/ast2ir" },
|
||||
{ name = "hir", path = "./libs/ir/hir" },
|
||||
# { name = "lir", path = "./libs/ir/lir" },
|
||||
{ name = "lir", path = "./libs/ir/lir" },
|
||||
|
||||
# { name = "ir2mcode", path = "./libs/ir2mcode" },
|
||||
# { name = "sccf2target", path = "./libs/target/sccf2target" },
|
||||
|
||||
@@ -1,8 +1,7 @@
|
||||
#ifndef __SCC_AST_H__
|
||||
#define __SCC_AST_H__
|
||||
|
||||
#include "ast_def.h"
|
||||
#include "ast_dump.h"
|
||||
#include "scc_ast_def.h"
|
||||
|
||||
// decls can be nullptr but maybe warning
|
||||
static inline void
|
||||
|
||||
@@ -6,7 +6,7 @@
|
||||
#ifndef __SCC_AST_DUMP_H__
|
||||
#define __SCC_AST_DUMP_H__
|
||||
|
||||
#include "ast_def.h"
|
||||
#include "scc_ast_def.h"
|
||||
#include <scc_tree_dump.h>
|
||||
|
||||
/**
|
||||
@@ -3,7 +3,7 @@
|
||||
* @brief AST dump 实现
|
||||
*/
|
||||
|
||||
#include <ast_dump.h>
|
||||
#include <scc_ast_dump.h>
|
||||
#include <scc_tree_dump.h>
|
||||
|
||||
static const char *node_type_names[] = {
|
||||
|
||||
@@ -1,8 +1,8 @@
|
||||
#ifndef __SCC_AST2IR_H__
|
||||
#define __SCC_AST2IR_H__
|
||||
|
||||
#include <hir_builder.h>
|
||||
#include <scc_ast.h>
|
||||
#include <scc_hir_builder.h>
|
||||
#include <scc_type_abi.h>
|
||||
|
||||
typedef struct {
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
#include <ast_def.h>
|
||||
#include <hir_builder.h>
|
||||
#include <hir_def.h>
|
||||
#include <scc_ast2ir.h>
|
||||
#include <scc_ast_def.h>
|
||||
#include <scc_hir_builder.h>
|
||||
#include <scc_hir_def.h>
|
||||
|
||||
static scc_hir_type_ref_t parse_base_type(scc_ast2ir_ctx_t *ctx,
|
||||
const scc_ast_type_t *ast_type) {
|
||||
|
||||
@@ -8,6 +8,7 @@
|
||||
typedef usize scc_cfg_id_t;
|
||||
typedef scc_cfg_id_t scc_cfg_bblock_id_t;
|
||||
typedef scc_cfg_id_t scc_cfg_func_id_t;
|
||||
typedef scc_cfg_id_t scc_cfg_symbol_id_t;
|
||||
|
||||
typedef SCC_VEC(scc_cfg_bblock_id_t) scc_cfg_bblock_id_vec_t;
|
||||
typedef SCC_VEC(void *) scc_cfg_value_vec_t;
|
||||
@@ -22,7 +23,7 @@ struct scc_cfg_bblock {
|
||||
scc_cfg_bblock_id_vec_t preds; // 前驱
|
||||
scc_cfg_bblock_id_vec_t succs; // 后继
|
||||
scc_cfg_value_vec_t values; // using cast
|
||||
void *attribute;
|
||||
void *meta;
|
||||
};
|
||||
|
||||
struct scc_cfg_func {
|
||||
@@ -33,7 +34,7 @@ struct scc_cfg_func {
|
||||
scc_hashtable_t bblock_map; // id -> index
|
||||
|
||||
scc_cfg_bblock_id_t entry_bblock_id; // maybe it will always 0
|
||||
void *attribute;
|
||||
void *meta;
|
||||
};
|
||||
|
||||
typedef enum {
|
||||
@@ -42,10 +43,16 @@ typedef enum {
|
||||
SCC_CFG_SYMBOL_KIND_EXTERN,
|
||||
} scc_cfg_symbol_kind_t;
|
||||
|
||||
typedef enum {
|
||||
SCC_CFG_SYMBOL_LINK_GLOABL,
|
||||
SCC_CFG_SYMBOL_LINK_LOCAL,
|
||||
} scc_cfg_symbol_linkage_t;
|
||||
|
||||
struct scc_cfg_symbol {
|
||||
const char *name;
|
||||
scc_cfg_symbol_kind_t kind;
|
||||
void *attribute;
|
||||
scc_cfg_symbol_linkage_t linkage;
|
||||
void *meta;
|
||||
};
|
||||
|
||||
typedef SCC_VEC(scc_cfg_bblock_t) scc_cfg_bblock_vec_t;
|
||||
@@ -74,9 +81,11 @@ scc_cfg_bblock_id_t scc_cfg_module_add_bblock(scc_cfg_module_t *module,
|
||||
const scc_cfg_bblock_t *bblock);
|
||||
scc_cfg_bblock_t *scc_cfg_module_unsafe_get_bblock(scc_cfg_module_t *module,
|
||||
scc_cfg_bblock_id_t id);
|
||||
void scc_cfg_module_add_symbol(scc_cfg_module_t *module,
|
||||
scc_cfg_symbol_id_t scc_cfg_module_add_symbol(scc_cfg_module_t *module,
|
||||
const scc_cfg_symbol_t *symbol);
|
||||
scc_cfg_id_t scc_cfg_module_lookup_symbol(const scc_cfg_module_t *module,
|
||||
scc_cfg_symbol_t *scc_cfg_module_unsafe_get_symbol(scc_cfg_module_t *module,
|
||||
scc_cfg_symbol_id_t id);
|
||||
scc_cfg_symbol_id_t scc_cfg_module_lookup_symbol(const scc_cfg_module_t *module,
|
||||
const char *name);
|
||||
|
||||
static inline const scc_cfg_symbol_t *
|
||||
|
||||
@@ -40,8 +40,10 @@ scc_cfg_func_t *scc_cfg_module_unsafe_get_func(scc_cfg_module_t *module,
|
||||
|
||||
scc_cfg_bblock_id_t scc_cfg_module_add_bblock(scc_cfg_module_t *module,
|
||||
const scc_cfg_bblock_t *bblock) {
|
||||
scc_cfg_func_id_t id = scc_vec_size(module->bblocks);
|
||||
scc_vec_push(module->bblocks, *bblock);
|
||||
scc_cfg_bblock_id_t id = scc_vec_size(module->bblocks);
|
||||
scc_cfg_bblock_t bblock_ = *bblock;
|
||||
bblock_.id = id;
|
||||
scc_vec_push(module->bblocks, bblock_);
|
||||
Assert(id != SCC_CFG_ID_nullptr);
|
||||
return id;
|
||||
}
|
||||
@@ -57,11 +59,30 @@ scc_cfg_bblock_t *scc_cfg_module_unsafe_get_bblock(scc_cfg_module_t *module,
|
||||
return &scc_vec_at(module->bblocks, id);
|
||||
}
|
||||
|
||||
void scc_cfg_module_add_symbol(scc_cfg_module_t *module,
|
||||
const scc_cfg_symbol_t *symbol) {}
|
||||
scc_cfg_id_t scc_cfg_module_lookup_symbol(const scc_cfg_module_t *module,
|
||||
scc_cfg_symbol_id_t scc_cfg_module_add_symbol(scc_cfg_module_t *module,
|
||||
const scc_cfg_symbol_t *symbol) {
|
||||
scc_cfg_func_id_t id = scc_vec_size(module->symbols);
|
||||
scc_vec_push(module->symbols, *symbol);
|
||||
Assert(id != SCC_CFG_ID_nullptr);
|
||||
scc_hashtable_set(&module->symbol_map, symbol->name, (void *)(usize)id);
|
||||
return id;
|
||||
}
|
||||
|
||||
scc_cfg_symbol_t *scc_cfg_module_unsafe_get_symbol(scc_cfg_module_t *module,
|
||||
scc_cfg_symbol_id_t id) {
|
||||
if (id == SCC_CFG_ID_nullptr) {
|
||||
Panic("nullptr symbol id");
|
||||
}
|
||||
if (id >= scc_vec_size(module->bblocks)) {
|
||||
Panic("invalid symbol id");
|
||||
}
|
||||
return &scc_vec_at(module->symbols, id);
|
||||
}
|
||||
|
||||
scc_cfg_symbol_id_t scc_cfg_module_lookup_symbol(const scc_cfg_module_t *module,
|
||||
const char *name) {
|
||||
return 0;
|
||||
return (scc_cfg_symbol_id_t)(usize)scc_hashtable_get(&module->symbol_map,
|
||||
name);
|
||||
}
|
||||
|
||||
void scc_cfg_bblock_add_pred(scc_cfg_bblock_t *bb,
|
||||
|
||||
@@ -1,8 +1,8 @@
|
||||
#ifndef __SCC_HIR_H__
|
||||
#define __SCC_HIR_H__
|
||||
|
||||
#include "hir_def.h"
|
||||
#include "hir_prog.h"
|
||||
#include "scc_hir_def.h"
|
||||
#include "scc_hir_prog.h"
|
||||
|
||||
void scc_hir_type_init(scc_hir_type_t *in, scc_hir_type_tag_t tag);
|
||||
void scc_hir_bblock_init(scc_hir_bblock_t *in, const char *label);
|
||||
|
||||
@@ -1,8 +1,8 @@
|
||||
#ifndef __SCC_HIR_BUILDER_H__
|
||||
#define __SCC_HIR_BUILDER_H__
|
||||
|
||||
#include "hir_prog.h"
|
||||
#include "scc_hir.h"
|
||||
#include "scc_hir_prog.h"
|
||||
|
||||
typedef struct scc_hir_builder scc_hir_builder_t;
|
||||
|
||||
@@ -246,10 +246,9 @@ typedef struct scc_hir_func_meta {
|
||||
} scc_hir_func_meta_t;
|
||||
|
||||
#define SCC_HIR_BBLOCK_VALUES(bblock) \
|
||||
(*(scc_hir_value_ref_vec_t *)&((bblock).values))
|
||||
((scc_hir_value_ref_vec_t *)&((bblock)->values))
|
||||
|
||||
#define SCC_HIR_BBLOCK_META(bblock) \
|
||||
((scc_hir_bblock_meta_t *)((bblock).attribute))
|
||||
#define SCC_HIR_FUNC_META(func) ((scc_hir_func_meta_t *)((func).attribute))
|
||||
#define SCC_HIR_BBLOCK_META(bblock) ((scc_hir_bblock_meta_t *)((bblock)->meta))
|
||||
#define SCC_HIR_FUNC_META(func) ((scc_hir_func_meta_t *)((func)->meta))
|
||||
|
||||
#endif /* __SCC_HIR_DEF_H__ */
|
||||
@@ -1,7 +1,7 @@
|
||||
#ifndef __SCC_HIR_DUMP_H__
|
||||
#define __SCC_HIR_DUMP_H__
|
||||
|
||||
#include "hir_prog.h"
|
||||
#include "scc_hir_prog.h"
|
||||
#include <scc_tree_dump.h>
|
||||
|
||||
typedef struct {
|
||||
@@ -9,7 +9,7 @@ typedef struct {
|
||||
scc_tree_dump_t *dump_ctx;
|
||||
} scc_hir_dump_t;
|
||||
|
||||
void scc_hir_dump_ctx_init(scc_hir_dump_t *ctx, scc_tree_dump_t *tree_dump,
|
||||
void scc_hir_dump_init(scc_hir_dump_t *ctx, scc_tree_dump_t *tree_dump,
|
||||
scc_hir_cprog_t *cprog);
|
||||
void scc_hir_dump_value(scc_hir_dump_t *ctx, scc_hir_value_ref_t node_ref);
|
||||
void scc_hir_dump_type(scc_hir_dump_t *ctx, scc_hir_type_ref_t type_ref);
|
||||
@@ -1,7 +1,7 @@
|
||||
#ifndef __SCC_HIR_MODULE_H__
|
||||
#define __SCC_HIR_MODULE_H__
|
||||
|
||||
#include "hir_def.h"
|
||||
#include "scc_hir_def.h"
|
||||
#include <scc_cfg.h>
|
||||
#include <scc_hashtable.h>
|
||||
|
||||
@@ -1,8 +1,8 @@
|
||||
#ifndef __SCC_HIR_PROG_H__
|
||||
#define __SCC_HIR_PROG_H__
|
||||
|
||||
#include "hir_def.h"
|
||||
#include "hir_module.h"
|
||||
#include "scc_hir_def.h"
|
||||
#include "scc_hir_module.h"
|
||||
|
||||
typedef struct scc_hir_cprog {
|
||||
scc_hir_module_t module;
|
||||
@@ -54,7 +54,7 @@ void scc_hir_func_init(scc_hir_func_t *in, const char *name) {
|
||||
Assert(in != nullptr);
|
||||
Assert(name != nullptr);
|
||||
in->name = name;
|
||||
scc_hir_func_meta_t *meta = SCC_HIR_FUNC_META(*in);
|
||||
scc_hir_func_meta_t *meta = SCC_HIR_FUNC_META(in);
|
||||
meta->type = SCC_HIR_REF_nullptr;
|
||||
scc_vec_init(in->bblocks);
|
||||
scc_vec_init(meta->params);
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
#include <hir_builder.h>
|
||||
#include <hir_def.h>
|
||||
#include <hir_prog.h>
|
||||
#include <scc_hir_builder.h>
|
||||
#include <scc_hir_def.h>
|
||||
#include <scc_hir_prog.h>
|
||||
|
||||
#define GET_MODULE(builder) (&(builder->cprog->module))
|
||||
|
||||
@@ -175,7 +175,7 @@ scc_hir_func_ref_t scc_hir_builder_func(scc_hir_builder_t *builder,
|
||||
scc_hir_func_t func;
|
||||
scc_hir_func_meta_t *meta = scc_malloc(sizeof(scc_hir_func_meta_t));
|
||||
Assert(meta != nullptr);
|
||||
func.attribute = meta;
|
||||
func.meta = meta;
|
||||
scc_hir_func_init(&func, name);
|
||||
meta->type = type_ref;
|
||||
|
||||
@@ -197,8 +197,8 @@ scc_hir_bblock_ref_t scc_hir_builder_bblock(scc_hir_builder_t *builder,
|
||||
}
|
||||
// scc_hir_bblock_meta_t bblock_meta = {0};
|
||||
// FIXME 当前没有bblock meta
|
||||
bblock.attribute = nullptr;
|
||||
scc_vec_init(SCC_HIR_BBLOCK_VALUES(bblock));
|
||||
bblock.meta = nullptr;
|
||||
scc_vec_init(*SCC_HIR_BBLOCK_VALUES(&bblock));
|
||||
|
||||
scc_hir_bblock_ref_t bblock_ref =
|
||||
scc_hir_module_add_bblock(GET_MODULE(builder), &bblock);
|
||||
@@ -298,7 +298,7 @@ void scc_hir_builder_begin_func(scc_hir_builder_t *builder,
|
||||
SCC_HIR_BUILDER_BEGIN_BORROW(
|
||||
builder, func_type,
|
||||
scc_hir_module_get_type(GET_MODULE(builder),
|
||||
SCC_HIR_FUNC_META(*func_ptr)->type));
|
||||
SCC_HIR_FUNC_META(func_ptr)->type));
|
||||
|
||||
if (func_type == nullptr || func_type->tag != SCC_HIR_TYPE_FUNC) {
|
||||
LOG_ERROR("Invalid function type");
|
||||
@@ -313,7 +313,7 @@ void scc_hir_builder_begin_func(scc_hir_builder_t *builder,
|
||||
return;
|
||||
}
|
||||
if (scc_vec_size(func_ptr->bblocks) != 0 ||
|
||||
scc_vec_size(SCC_HIR_FUNC_META(*func_ptr)->params) != 0) {
|
||||
scc_vec_size(SCC_HIR_FUNC_META(func_ptr)->params) != 0) {
|
||||
LOG_FATAL("Multiple function definitions");
|
||||
SCC_HIR_BUILDER_END_BORROW(builder); // func_type
|
||||
SCC_HIR_BUILDER_END_BORROW(builder); // func_ptr
|
||||
@@ -381,7 +381,7 @@ void scc_hir_builder_add_instr(scc_hir_builder_t *builder,
|
||||
scc_hir_module_get_bblock(GET_MODULE(builder),
|
||||
builder->current_bblock));
|
||||
if (current_bblock) {
|
||||
scc_vec_push(SCC_HIR_BBLOCK_VALUES(*current_bblock), instr);
|
||||
scc_vec_push(*SCC_HIR_BBLOCK_VALUES(current_bblock), instr);
|
||||
} else {
|
||||
LOG_ERROR("Current basic block is not set");
|
||||
}
|
||||
@@ -628,7 +628,7 @@ scc_hir_value_ref_t scc_hir_builder_call(scc_hir_builder_t *builder,
|
||||
SCC_HIR_BUILDER_BEGIN_BORROW(
|
||||
builder, func_type,
|
||||
scc_hir_module_get_type(GET_MODULE(builder),
|
||||
SCC_HIR_FUNC_META(*callee_func)->type));
|
||||
SCC_HIR_FUNC_META(callee_func)->type));
|
||||
if (func_type && func_type->tag == SCC_HIR_TYPE_FUNC) {
|
||||
call_node.type = func_type->data.function.ret_type;
|
||||
}
|
||||
@@ -1,5 +1,5 @@
|
||||
#include <hir_dump.h>
|
||||
#include <hir_prog.h>
|
||||
#include <scc_hir_dump.h>
|
||||
#include <scc_hir_prog.h>
|
||||
#include <scc_tree_dump.h>
|
||||
|
||||
#define GET_MODULE(ctx) (&(ctx->cprog->module))
|
||||
@@ -192,10 +192,10 @@ static void dump_ret_node(scc_hir_dump_t *ctx, const scc_hir_value_t *value) {
|
||||
dump_child_node_ref(ctx, value->data.ret.ret_val, true);
|
||||
}
|
||||
|
||||
void scc_hir_dump_ctx_init(scc_hir_dump_t *ctx, scc_tree_dump_t *td,
|
||||
void scc_hir_dump_init(scc_hir_dump_t *ctx, scc_tree_dump_t *td,
|
||||
scc_hir_cprog_t *cprog) {
|
||||
ctx->cprog = cprog;
|
||||
ctx->dump_ctx = td;
|
||||
ctx->cprog = cprog;
|
||||
}
|
||||
|
||||
void scc_hir_dump_value(scc_hir_dump_t *ctx, scc_hir_value_ref_t value_ref) {
|
||||
@@ -327,10 +327,11 @@ void scc_hir_dump_bblock(scc_hir_dump_t *ctx, scc_hir_bblock_ref_t bblock_ref) {
|
||||
bblock->name ? bblock->name : "<unnamed>");
|
||||
scc_tree_dump_append(ctx->dump_ctx, "\n");
|
||||
|
||||
for (usize i = 0; i < scc_vec_size(SCC_HIR_BBLOCK_VALUES(*bblock)); i++) {
|
||||
cbool is_last = (i + 1 == scc_vec_size(SCC_HIR_BBLOCK_VALUES(*bblock)));
|
||||
scc_hir_value_ref_vec_t *values = SCC_HIR_BBLOCK_VALUES(bblock);
|
||||
scc_vec_foreach(*values, i) {
|
||||
cbool is_last = (i + 1 == scc_vec_size(*values));
|
||||
scc_tree_dump_push(ctx->dump_ctx, is_last);
|
||||
scc_hir_dump_value(ctx, scc_vec_at(SCC_HIR_BBLOCK_VALUES(*bblock), i));
|
||||
scc_hir_dump_value(ctx, scc_vec_at(*values, i));
|
||||
scc_tree_dump_pop(ctx->dump_ctx);
|
||||
}
|
||||
}
|
||||
@@ -347,17 +348,16 @@ void scc_hir_dump_func(scc_hir_dump_t *ctx, scc_hir_func_ref_t func_ref) {
|
||||
func->name ? func->name : "<unnamed>");
|
||||
scc_tree_dump_append(ctx->dump_ctx, "\n");
|
||||
|
||||
if (SCC_HIR_FUNC_META(*func)->type) {
|
||||
if (SCC_HIR_FUNC_META(func)->type) {
|
||||
scc_tree_dump_push(ctx->dump_ctx, false);
|
||||
scc_hir_dump_type(ctx, SCC_HIR_FUNC_META(*func)->type);
|
||||
scc_hir_dump_type(ctx, SCC_HIR_FUNC_META(func)->type);
|
||||
scc_tree_dump_pop(ctx->dump_ctx);
|
||||
}
|
||||
for (usize i = 0; i < scc_vec_size(SCC_HIR_FUNC_META(*func)->params); i++) {
|
||||
for (usize i = 0; i < scc_vec_size(SCC_HIR_FUNC_META(func)->params); i++) {
|
||||
cbool is_last =
|
||||
(i + 1 == scc_vec_size(SCC_HIR_FUNC_META(*func)->params));
|
||||
(i + 1 == scc_vec_size(SCC_HIR_FUNC_META(func)->params));
|
||||
scc_tree_dump_push(ctx->dump_ctx, is_last);
|
||||
scc_hir_dump_value(ctx,
|
||||
scc_vec_at(SCC_HIR_FUNC_META(*func)->params, i));
|
||||
scc_hir_dump_value(ctx, scc_vec_at(SCC_HIR_FUNC_META(func)->params, i));
|
||||
scc_tree_dump_pop(ctx->dump_ctx);
|
||||
}
|
||||
for (usize i = 0; i < scc_vec_size(func->bblocks); i++) {
|
||||
@@ -630,11 +630,11 @@ void scc_hir_dump_bblock_linear(scc_hir_dump_t *ctx,
|
||||
else
|
||||
scc_tree_dump_append_fmt(ctx->dump_ctx, "%%L%d <unnamed>:", bblock_ref);
|
||||
|
||||
for (usize i = 0; i < scc_vec_size(SCC_HIR_BBLOCK_VALUES(*bblock)); i++) {
|
||||
scc_hir_value_ref_vec_t *values = SCC_HIR_BBLOCK_VALUES(bblock);
|
||||
for (usize i = 0; i < scc_vec_size(*values); i++) {
|
||||
scc_tree_dump_begin_line(ctx->dump_ctx);
|
||||
scc_tree_dump_append(ctx->dump_ctx, " ");
|
||||
scc_hir_dump_value_linear(
|
||||
ctx, scc_vec_at(SCC_HIR_BBLOCK_VALUES(*bblock), i));
|
||||
scc_hir_dump_value_linear(ctx, scc_vec_at(*values, i));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -650,14 +650,14 @@ void scc_hir_dump_func_linear(scc_hir_dump_t *ctx, scc_hir_func_ref_t func_ref,
|
||||
(func->name && func->name[0]) ? func->name
|
||||
: "<unnamed>");
|
||||
|
||||
if (scc_vec_size(SCC_HIR_FUNC_META(*func)->params) > 0) {
|
||||
if (scc_vec_size(SCC_HIR_FUNC_META(func)->params) > 0) {
|
||||
scc_tree_dump_append(ctx->dump_ctx, "(");
|
||||
for (usize i = 0; i < scc_vec_size(SCC_HIR_FUNC_META(*func)->params);
|
||||
for (usize i = 0; i < scc_vec_size(SCC_HIR_FUNC_META(func)->params);
|
||||
i++) {
|
||||
if (i > 0)
|
||||
scc_tree_dump_append(ctx->dump_ctx, ", ");
|
||||
scc_hir_value_ref_t param_ref =
|
||||
scc_vec_at(SCC_HIR_FUNC_META(*func)->params, i);
|
||||
scc_vec_at(SCC_HIR_FUNC_META(func)->params, i);
|
||||
scc_hir_value_t *param_node =
|
||||
scc_hir_module_get_value(GET_MODULE(ctx), param_ref);
|
||||
scc_tree_dump_append(ctx->dump_ctx, "%");
|
||||
@@ -673,7 +673,7 @@ void scc_hir_dump_func_linear(scc_hir_dump_t *ctx, scc_hir_func_ref_t func_ref,
|
||||
}
|
||||
|
||||
scc_tree_dump_append(ctx->dump_ctx, ": ");
|
||||
scc_hir_dump_type_linear(ctx, SCC_HIR_FUNC_META(*func)->type);
|
||||
scc_hir_dump_type_linear(ctx, SCC_HIR_FUNC_META(func)->type);
|
||||
|
||||
if (is_decl) {
|
||||
scc_tree_dump_append(ctx->dump_ctx, ";");
|
||||
@@ -1,4 +1,4 @@
|
||||
#include <hir_module.h>
|
||||
#include <scc_hir_module.h>
|
||||
|
||||
void scc_hir_module_init(scc_hir_module_t *ctx) {
|
||||
scc_cfg_module_init(&ctx->cfg_module);
|
||||
@@ -89,7 +89,7 @@ scc_hir_bblock_ref_t scc_hir_module_add_bblock(scc_hir_module_t *ctx,
|
||||
scc_hir_func_ref_t scc_hir_module_add_func(scc_hir_module_t *ctx,
|
||||
const scc_hir_func_t *func) {
|
||||
scc_hir_func_ref_t ref = scc_cfg_module_add_func(&ctx->cfg_module, func);
|
||||
scc_vec_push(ctx->funcs_meta, func->attribute);
|
||||
scc_vec_push(ctx->funcs_meta, func->meta);
|
||||
return ref;
|
||||
}
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
#include <hir_prog.h>
|
||||
#include <scc_hir_prog.h>
|
||||
|
||||
void scc_hir_cprog_init(scc_hir_cprog_t *in) {
|
||||
scc_vec_init(in->func_decls);
|
||||
14
libs/ir/lir/cbuild.toml
Normal file
14
libs/ir/lir/cbuild.toml
Normal file
@@ -0,0 +1,14 @@
|
||||
[package]
|
||||
name = "scc_lir"
|
||||
version = "0.1.0"
|
||||
authors = []
|
||||
description = ""
|
||||
|
||||
dependencies = [
|
||||
{ name = "scc_cfg", path = "../cfg" },
|
||||
{ name = "scc_hir", path = "../hir" },
|
||||
{ name = "scc_utils", path = "../../../runtime/scc_utils" },
|
||||
{ name = "tree_dump", path = "../../tree_dump" },
|
||||
]
|
||||
# features = {}
|
||||
# default_features = []
|
||||
10
libs/ir/lir/include/scc_hir2lir.h
Normal file
10
libs/ir/lir/include/scc_hir2lir.h
Normal file
@@ -0,0 +1,10 @@
|
||||
#ifndef __SCC_IR2LIR_H__
|
||||
#define __SCC_IR2LIR_H__
|
||||
|
||||
#include "scc_lir.h"
|
||||
#include "scc_lir_module.h"
|
||||
#include <scc_hir.h>
|
||||
|
||||
void scc_hir2lir(scc_lir_module_t *module, const scc_hir_cprog_t *cprog);
|
||||
|
||||
#endif /* __SCC_IR2LIR_H__ */
|
||||
275
libs/ir/lir/include/scc_lir.h
Normal file
275
libs/ir/lir/include/scc_lir.h
Normal file
@@ -0,0 +1,275 @@
|
||||
/**
|
||||
* @file scc_lir.h
|
||||
* @brief 低层中间表示 (Low-Level IR) - 实用精简版
|
||||
*
|
||||
* 设计原则:
|
||||
* - 依赖 scc_core.h 和 scc_utils.h (仅使用哈希表)
|
||||
* - 保留所有核心功能:复杂地址、并行复制、VA 支持、符号引用
|
||||
* - 基本块使用稳定 ID,通过哈希表快速查找
|
||||
* - 预留属性位掩码 (attr),未来可扩展 align、packed、weak 等
|
||||
* - 无字符串池、无类型缓存,保持简单
|
||||
*/
|
||||
|
||||
#ifndef __SCC_LIR_H__
|
||||
#define __SCC_LIR_H__
|
||||
|
||||
#include <scc_ap.h>
|
||||
#include <scc_cfg.h>
|
||||
#include <scc_core.h>
|
||||
#include <scc_hashtable.h>
|
||||
#include <scc_pos.h>
|
||||
|
||||
typedef enum scc_lir_attr {
|
||||
SCC_LIR_ATTR_NONE = 0,
|
||||
SCC_LIR_ATTR_STATIC = 1 << 0, // 内部链接
|
||||
SCC_LIR_ATTR_WEAK = 1 << 1, // 弱符号
|
||||
SCC_LIR_ATTR_VIS_HIDDEN = 1 << 2, // 符号可见性 hidden
|
||||
SCC_LIR_ATTR_NOINLINE = 1 << 3,
|
||||
SCC_LIR_ATTR_ALWAYSINLINE = 1 << 4,
|
||||
// 对齐值可编码在 attr 的高位,例如:
|
||||
// #define SCC_LIR_ATTR_ALIGN(n) (((n) << 8) | SCC_LIR_ATTR_ALIGN_FLAG)
|
||||
} scc_lir_attr_t;
|
||||
|
||||
typedef enum {
|
||||
SCC_LIR_INSTR_KIND_NONE, // 无操作数
|
||||
SCC_LIR_INSTR_KIND_VREG, // 虚拟寄存器
|
||||
SCC_LIR_INSTR_KIND_PREG, // 物理寄存器 (后端定义编号)
|
||||
SCC_LIR_INSTR_KIND_IMM, // 整数立即数
|
||||
SCC_LIR_INSTR_KIND_FIMM, // 浮点立即数
|
||||
SCC_LIR_INSTR_KIND_SYMBOL, // 全局符号 (函数名、全局变量、字符串常量)
|
||||
SCC_LIR_INSTR_KIND_ADDR // 复杂地址表达式 (base + index*scale + offset)
|
||||
} scc_lir_instr_kind_t;
|
||||
|
||||
/**
|
||||
* @brief 复杂地址表达式
|
||||
* base + index * scale + offset
|
||||
*/
|
||||
typedef struct scc_lir_addr {
|
||||
int base; // 基址寄存器,-1 表示无
|
||||
int index; // 索引寄存器,-1 表示无
|
||||
int scale; // 比例因子 (1, 2, 4, 8)
|
||||
i64 offset; // 常量偏移
|
||||
} scc_lir_addr_t;
|
||||
|
||||
typedef struct scc_lir_instr {
|
||||
scc_lir_instr_kind_t kind;
|
||||
union {
|
||||
unsigned int reg; // VREG 或 PREG 索引
|
||||
scc_ap_t imm; // 整型立即数
|
||||
f64 fimm; // 浮点立即数
|
||||
const char *symbol; // 符号名 (生命周期由前端管理)
|
||||
scc_lir_addr_t addr; // 复杂地址
|
||||
} data;
|
||||
} scc_lir_val_t;
|
||||
|
||||
/* 构造宏 */
|
||||
#define SCC_LIR_NONE() ((scc_lir_val_t){.kind = SCC_LIR_INSTR_KIND_NONE})
|
||||
#define SCC_LIR_VREG(n) \
|
||||
((scc_lir_val_t){.kind = SCC_LIR_INSTR_KIND_VREG, .data.reg = (n)})
|
||||
#define SCC_LIR_PREG(r) \
|
||||
((scc_lir_val_t){.kind = SCC_LIR_INSTR_KIND_PREG, .data.reg = (r)})
|
||||
#define SCC_LIR_IMM(v) \
|
||||
((scc_lir_val_t){.kind = SCC_LIR_INSTR_KIND_IMM, .data.imm = (v)})
|
||||
#define SCC_LIR_FIMM(v) \
|
||||
((scc_lir_val_t){.kind = SCC_LIR_INSTR_KIND_FIMM, .data.fimm = (v)})
|
||||
#define SCC_LIR_SYMBOL(s) \
|
||||
((scc_lir_val_t){.kind = SCC_LIR_INSTR_KIND_SYMBOL, .data.symbol = (s)})
|
||||
#define SCC_LIR_ADDR(b, i, s, o) \
|
||||
((scc_lir_val_t){.kind = SCC_LIR_INSTR_KIND_ADDR, \
|
||||
.data.addr = {b, i, s, o}})
|
||||
|
||||
#define SCC_LIR_SIZE_8 1
|
||||
#define SCC_LIR_SIZE_16 2
|
||||
#define SCC_LIR_SIZE_32 4
|
||||
#define SCC_LIR_SIZE_64 8
|
||||
|
||||
typedef enum {
|
||||
SCC_LIR_EXT_NONE,
|
||||
SCC_LIR_EXT_SEXT,
|
||||
SCC_LIR_EXT_ZEXT,
|
||||
SCC_LIR_EXT_FLOAT
|
||||
} scc_lir_ext_t;
|
||||
|
||||
typedef enum {
|
||||
/* 数据移动 */
|
||||
SCC_LIR_MOV,
|
||||
SCC_LIR_LOAD,
|
||||
SCC_LIR_LOAD_ADDR,
|
||||
SCC_LIR_STORE,
|
||||
SCC_LIR_STORE_ADDR,
|
||||
SCC_LIR_LEA,
|
||||
|
||||
/* 整数算术 */
|
||||
SCC_LIR_ADD,
|
||||
SCC_LIR_SUB,
|
||||
SCC_LIR_MUL,
|
||||
SCC_LIR_DIV_S,
|
||||
SCC_LIR_DIV_U,
|
||||
SCC_LIR_REM_S,
|
||||
SCC_LIR_REM_U,
|
||||
SCC_LIR_AND,
|
||||
SCC_LIR_OR,
|
||||
SCC_LIR_XOR,
|
||||
SCC_LIR_SHL,
|
||||
SCC_LIR_SHR,
|
||||
SCC_LIR_SAR,
|
||||
SCC_LIR_NEG,
|
||||
SCC_LIR_NOT,
|
||||
|
||||
/* 浮点算术 */
|
||||
SCC_LIR_FADD,
|
||||
SCC_LIR_FSUB,
|
||||
SCC_LIR_FMUL,
|
||||
SCC_LIR_FDIV,
|
||||
SCC_LIR_FNEG,
|
||||
SCC_LIR_FCVT,
|
||||
|
||||
/* 比较 */
|
||||
SCC_LIR_CMP,
|
||||
|
||||
/* 控制流 */
|
||||
SCC_LIR_BR,
|
||||
SCC_LIR_JMP,
|
||||
SCC_LIR_JMP_INDIRECT,
|
||||
SCC_LIR_CALL,
|
||||
SCC_LIR_CALL_INDIRECT,
|
||||
SCC_LIR_RET,
|
||||
|
||||
/* 并行复制 */
|
||||
SCC_LIR_PARALLEL_COPY,
|
||||
|
||||
/* 可变参数 */
|
||||
SCC_LIR_VA_START,
|
||||
SCC_LIR_VA_ARG,
|
||||
SCC_LIR_VA_END,
|
||||
SCC_LIR_VA_COPY,
|
||||
|
||||
/* 栈管理 */
|
||||
SCC_LIR_ALLOCA,
|
||||
|
||||
SCC_LIR_NOP
|
||||
} scc_lir_op_t;
|
||||
|
||||
typedef enum {
|
||||
SCC_LIR_COND_EQ,
|
||||
SCC_LIR_COND_NE,
|
||||
SCC_LIR_COND_SLT,
|
||||
SCC_LIR_COND_SLE,
|
||||
SCC_LIR_COND_SGT,
|
||||
SCC_LIR_COND_SGE,
|
||||
SCC_LIR_COND_ULT,
|
||||
SCC_LIR_COND_ULE,
|
||||
SCC_LIR_COND_UGT,
|
||||
SCC_LIR_COND_UGE,
|
||||
SCC_LIR_COND_FEQ,
|
||||
SCC_LIR_COND_FNE,
|
||||
SCC_LIR_COND_FLT,
|
||||
SCC_LIR_COND_FLE,
|
||||
SCC_LIR_COND_FGT,
|
||||
SCC_LIR_COND_FGE
|
||||
} scc_lir_cond_t;
|
||||
|
||||
typedef scc_cfg_bblock_id_t scc_lir_bblock_id_t;
|
||||
typedef struct scc_lir_ins {
|
||||
scc_lir_op_t op;
|
||||
u8 size;
|
||||
scc_lir_ext_t ext;
|
||||
scc_lir_val_t to;
|
||||
scc_lir_val_t arg0;
|
||||
scc_lir_val_t arg1;
|
||||
/// @brief debug 信息
|
||||
scc_pos_t src_loc;
|
||||
|
||||
union {
|
||||
scc_lir_cond_t cond;
|
||||
|
||||
struct scc_lir_br {
|
||||
scc_lir_bblock_id_t true_target;
|
||||
scc_lir_bblock_id_t false_target;
|
||||
} br;
|
||||
|
||||
scc_lir_bblock_id_t jmp_target;
|
||||
|
||||
struct scc_lir_call {
|
||||
const char *callee;
|
||||
scc_lir_val_t *args;
|
||||
int arg_count;
|
||||
scc_lir_val_t ret_vreg;
|
||||
u64 clobber_mask;
|
||||
} call;
|
||||
|
||||
struct scc_lir_call_indirect {
|
||||
scc_lir_val_t target;
|
||||
scc_lir_val_t *args;
|
||||
int arg_count;
|
||||
scc_lir_val_t ret_vreg;
|
||||
u64 clobber_mask;
|
||||
} call_indirect;
|
||||
|
||||
scc_lir_val_t ret_val;
|
||||
|
||||
struct scc_lir_parallel_copy {
|
||||
scc_lir_val_t *dests;
|
||||
scc_lir_val_t *srcs;
|
||||
int num_copies;
|
||||
} parallel_copy;
|
||||
|
||||
struct scc_lir_alloca {
|
||||
int size_bytes;
|
||||
int align_bytes;
|
||||
} alloca;
|
||||
|
||||
struct {
|
||||
scc_lir_val_t ap;
|
||||
scc_lir_val_t last;
|
||||
} va_start;
|
||||
|
||||
struct {
|
||||
scc_lir_val_t ap;
|
||||
int type_size;
|
||||
int type_align;
|
||||
char is_float;
|
||||
scc_lir_val_t to;
|
||||
} va_arg;
|
||||
|
||||
struct {
|
||||
scc_lir_val_t ap;
|
||||
} va_end;
|
||||
|
||||
struct {
|
||||
scc_lir_val_t dest;
|
||||
scc_lir_val_t src;
|
||||
} va_copy;
|
||||
} metadata;
|
||||
} scc_lir_instr_t;
|
||||
|
||||
typedef SCC_VEC(scc_lir_instr_t) scc_lir_instr_vec_t;
|
||||
typedef scc_cfg_bblock_t scc_lir_bblock_t;
|
||||
#define SCC_LIR_BBLOCK_VALUES(bblock) \
|
||||
((scc_lir_instr_vec_t *)&((bblock)->values))
|
||||
|
||||
typedef scc_cfg_func_t scc_lir_func_t;
|
||||
typedef struct scc_lir_func_meta {
|
||||
int vregs_count;
|
||||
int frame_size;
|
||||
scc_lir_attr_t attr;
|
||||
} scc_lir_func_meta_t;
|
||||
#define SCC_LIR_FUNC_META(func) ((scc_lir_func_meta_t *)(func)->meta)
|
||||
|
||||
typedef scc_cfg_symbol_id_t scc_lir_symbol_id_t;
|
||||
typedef scc_cfg_symbol_t scc_lir_symbol_t;
|
||||
typedef struct scc_lir_symbol_meta {
|
||||
union {
|
||||
struct {
|
||||
scc_lir_func_t *func; // 指向函数体(若为定义)
|
||||
} func;
|
||||
struct {
|
||||
u8 *init_data; // 初始化数据(若为 NULL 则零初始化)
|
||||
usize size; // 数据大小(字节)
|
||||
int align; // 对齐要求
|
||||
} data;
|
||||
};
|
||||
} scc_lir_symbol_meta_t;
|
||||
#define SCC_LIR_SYMBOL_META(symbol) ((scc_lir_symbol_meta_t *)(symbol)->meta)
|
||||
|
||||
#endif /* __SCC_LIR_H__ */
|
||||
25
libs/ir/lir/include/scc_lir_dump.h
Normal file
25
libs/ir/lir/include/scc_lir_dump.h
Normal file
@@ -0,0 +1,25 @@
|
||||
#ifndef __SCC_LIR_DUMP_H__
|
||||
#define __SCC_LIR_DUMP_H__
|
||||
|
||||
#include "scc_lir.h"
|
||||
#include "scc_lir_module.h"
|
||||
#include <scc_tree_dump.h>
|
||||
|
||||
typedef struct {
|
||||
scc_tree_dump_t *dump_ctx;
|
||||
scc_lir_module_t *module;
|
||||
} scc_lir_dump_ctx_t;
|
||||
|
||||
static inline void scc_lir_dump_init(scc_lir_dump_ctx_t *ctx,
|
||||
scc_tree_dump_t *dump_ctx,
|
||||
scc_lir_module_t *module) {
|
||||
Assert(ctx != nullptr && dump_ctx != nullptr && module != nullptr);
|
||||
ctx->dump_ctx = dump_ctx;
|
||||
ctx->module = module;
|
||||
}
|
||||
void scc_lir_dump_ins(scc_lir_dump_ctx_t *ctx, const scc_lir_instr_t *ins);
|
||||
void scc_lir_dump_bblock(scc_lir_dump_ctx_t *ctx, const scc_lir_bblock_t *bb);
|
||||
void scc_lir_dump_func(scc_lir_dump_ctx_t *ctx, const scc_lir_func_t *func);
|
||||
void scc_lir_dump_module(scc_lir_dump_ctx_t *ctx);
|
||||
|
||||
#endif /* __SCC_LIR_DUMP_H__ */
|
||||
59
libs/ir/lir/include/scc_lir_module.h
Normal file
59
libs/ir/lir/include/scc_lir_module.h
Normal file
@@ -0,0 +1,59 @@
|
||||
/**
|
||||
* @file scc_lir_module.h
|
||||
* @brief LIR 模块:管理函数定义、全局符号和声明
|
||||
*/
|
||||
|
||||
#ifndef __SCC_LIR_MODULE_H__
|
||||
#define __SCC_LIR_MODULE_H__
|
||||
|
||||
#include "scc_lir.h"
|
||||
#include <scc_cfg.h>
|
||||
#include <scc_core.h>
|
||||
#include <scc_hashtable.h>
|
||||
|
||||
typedef SCC_VEC(scc_lir_func_meta_t *) scc_lir_func_meta_vec_t;
|
||||
typedef SCC_VEC(scc_lir_symbol_meta_t *) scc_lir_symbol_meta_vec_t;
|
||||
typedef struct scc_lir_module {
|
||||
scc_cfg_module_t cfg_module;
|
||||
scc_lir_func_meta_vec_t func_metas;
|
||||
scc_lir_symbol_meta_vec_t symbol_metas;
|
||||
} scc_lir_module_t;
|
||||
|
||||
/**
|
||||
* @brief 初始化 LIR 模块
|
||||
*/
|
||||
void scc_lir_module_init(scc_lir_module_t *module);
|
||||
|
||||
/**
|
||||
* @brief 销毁 LIR 模块,释放所有函数和符号资源
|
||||
*/
|
||||
void scc_lir_module_drop(scc_lir_module_t *module);
|
||||
|
||||
/**
|
||||
* @brief 添加一个函数声明(外部或未定义)
|
||||
* @param mod 模块
|
||||
* @param name 函数名
|
||||
* @param attr 属性
|
||||
* @return 符号指针
|
||||
*/
|
||||
scc_lir_symbol_id_t scc_lir_module_add_func_decl(scc_lir_module_t *module,
|
||||
const char *name);
|
||||
|
||||
/**
|
||||
* @brief 添加一个全局数据符号(定义或外部)
|
||||
* @param mod 模块
|
||||
* @param name 符号名
|
||||
* @param kind 种类 (DATA 或 EXTERN)
|
||||
* @param init_data 初始化数据 (若为 DATA 定义;若为 NULL 则零初始化)
|
||||
* @param size 数据大小 (若为 EXTERN 可为 0)
|
||||
* @param align 对齐要求
|
||||
* @param attr 属性
|
||||
* @return 符号指针
|
||||
*/
|
||||
scc_lir_symbol_id_t scc_lir_module_add_data(scc_lir_module_t *module,
|
||||
const char *name,
|
||||
scc_cfg_symbol_kind_t kind,
|
||||
const u8 *init_data, usize size,
|
||||
u32 align);
|
||||
|
||||
#endif /* __SCC_LIR_MODULE_H__ */
|
||||
544
libs/ir/lir/src/scc_hir2lir.c
Normal file
544
libs/ir/lir/src/scc_hir2lir.c
Normal file
@@ -0,0 +1,544 @@
|
||||
/**
|
||||
* @file scc_hir2lir.c
|
||||
* @brief 将高级 IR (scc_ir) 降级为低层 LIR (scc_lir)
|
||||
*/
|
||||
|
||||
#include <scc_hashtable.h>
|
||||
#include <scc_hir2lir.h>
|
||||
|
||||
/* ---------- 转换上下文 ---------- */
|
||||
typedef struct {
|
||||
scc_hir_module_t *hir_module;
|
||||
scc_lir_module_t *lir_module;
|
||||
|
||||
scc_lir_func_t *current_func;
|
||||
scc_lir_instr_vec_t instrs;
|
||||
|
||||
scc_hashtable_t bb_map; // ir_bblock_ref_t -> scc_lir_bblock_id_t
|
||||
scc_hashtable_t value_to_vreg; // ir_value_ref_t -> unsigned int vreg
|
||||
scc_hashtable_t func_decl_map; // ir_func_ref_t -> const char* (用于调用)
|
||||
} ir2lir_ctx_t;
|
||||
|
||||
static void ir2lir_ctx_init(ir2lir_ctx_t *ctx, scc_lir_module_t *lir_module,
|
||||
scc_hir_module_t *hir_module) {
|
||||
ctx->hir_module = hir_module;
|
||||
ctx->lir_module = lir_module;
|
||||
scc_hashtable_usize_init(&ctx->bb_map);
|
||||
scc_hashtable_usize_init(&ctx->value_to_vreg);
|
||||
scc_hashtable_usize_init(&ctx->func_decl_map);
|
||||
}
|
||||
|
||||
static void ir2lir_ctx_drop(ir2lir_ctx_t *ctx) {
|
||||
scc_hashtable_drop(&ctx->bb_map);
|
||||
scc_hashtable_drop(&ctx->value_to_vreg);
|
||||
scc_hashtable_drop(&ctx->func_decl_map);
|
||||
}
|
||||
|
||||
static scc_lir_val_t ir_value_to_lir_operand(ir2lir_ctx_t *ctx,
|
||||
scc_hir_value_ref_t val_ref);
|
||||
static void translate_hir_value(ir2lir_ctx_t *ctx, scc_hir_value_t *value,
|
||||
scc_hir_value_ref_t value_ref);
|
||||
|
||||
static void ir_type_to_lir_size_ext(scc_hir_type_t *type, u8 *out_size,
|
||||
scc_lir_ext_t *out_ext) {
|
||||
switch (type->tag) {
|
||||
case SCC_HIR_TYPE_i8:
|
||||
*out_size = SCC_LIR_SIZE_8;
|
||||
*out_ext = SCC_LIR_EXT_SEXT;
|
||||
break;
|
||||
case SCC_HIR_TYPE_u8:
|
||||
*out_size = SCC_LIR_SIZE_8;
|
||||
*out_ext = SCC_LIR_EXT_ZEXT;
|
||||
break;
|
||||
case SCC_HIR_TYPE_i16:
|
||||
*out_size = SCC_LIR_SIZE_16;
|
||||
*out_ext = SCC_LIR_EXT_SEXT;
|
||||
break;
|
||||
case SCC_HIR_TYPE_u16:
|
||||
*out_size = SCC_LIR_SIZE_16;
|
||||
*out_ext = SCC_LIR_EXT_ZEXT;
|
||||
break;
|
||||
case SCC_HIR_TYPE_i32:
|
||||
*out_size = SCC_LIR_SIZE_32;
|
||||
*out_ext = SCC_LIR_EXT_SEXT;
|
||||
break;
|
||||
case SCC_HIR_TYPE_u32:
|
||||
*out_size = SCC_LIR_SIZE_32;
|
||||
*out_ext = SCC_LIR_EXT_ZEXT;
|
||||
break;
|
||||
case SCC_HIR_TYPE_i64:
|
||||
*out_size = SCC_LIR_SIZE_64;
|
||||
*out_ext = SCC_LIR_EXT_SEXT;
|
||||
break;
|
||||
case SCC_HIR_TYPE_u64:
|
||||
*out_size = SCC_LIR_SIZE_64;
|
||||
*out_ext = SCC_LIR_EXT_ZEXT;
|
||||
break;
|
||||
case SCC_HIR_TYPE_f32:
|
||||
*out_size = SCC_LIR_SIZE_32;
|
||||
*out_ext = SCC_LIR_EXT_FLOAT;
|
||||
break;
|
||||
case SCC_HIR_TYPE_f64:
|
||||
*out_size = SCC_LIR_SIZE_64;
|
||||
*out_ext = SCC_LIR_EXT_FLOAT;
|
||||
break;
|
||||
case SCC_HIR_TYPE_PTR:
|
||||
*out_size = SCC_LIR_SIZE_64;
|
||||
*out_ext = SCC_LIR_EXT_NONE;
|
||||
break;
|
||||
case SCC_HIR_TYPE_void:
|
||||
*out_size = 0;
|
||||
*out_ext = SCC_LIR_EXT_NONE;
|
||||
break;
|
||||
default:
|
||||
Panic("unsupported IR type in lowering");
|
||||
}
|
||||
}
|
||||
|
||||
static inline void scc_lir_builder_add_instr(ir2lir_ctx_t *ctx,
|
||||
scc_lir_instr_t *instr) {
|
||||
scc_vec_push(ctx->instrs, *instr);
|
||||
}
|
||||
|
||||
// 获取或创建 IR 值对应的虚拟寄存器
|
||||
static int get_vreg_for_value(ir2lir_ctx_t *ctx, scc_hir_value_ref_t val_ref) {
|
||||
void *found =
|
||||
scc_hashtable_get(&ctx->value_to_vreg, (void *)(usize)val_ref);
|
||||
if (found)
|
||||
return (int)(usize)found;
|
||||
|
||||
int vreg = SCC_LIR_FUNC_META(ctx->current_func)->vregs_count++;
|
||||
scc_hashtable_set(&ctx->value_to_vreg, (void *)(usize)val_ref,
|
||||
(void *)(usize)vreg);
|
||||
return vreg;
|
||||
}
|
||||
|
||||
// 获取 LIR 操作数:若为常量/全局符号则直接构造,否则返回对应 vreg
|
||||
static scc_lir_val_t ir_value_to_lir_operand(ir2lir_ctx_t *ctx,
|
||||
scc_hir_value_ref_t val_ref) {
|
||||
scc_hir_value_t *val = scc_hir_module_get_value(ctx->hir_module, val_ref);
|
||||
Assert(val != nullptr);
|
||||
|
||||
switch (val->tag) {
|
||||
case SCC_HIR_VALUE_TAG_INTEGER: {
|
||||
return SCC_LIR_IMM(val->data.integer);
|
||||
}
|
||||
case SCC_HIR_VALUE_TAG_DECIMAL: {
|
||||
TODO();
|
||||
}
|
||||
case SCC_HIR_VALUE_TAG_GLOBAL_ALLOC: {
|
||||
// 全局变量地址作为符号
|
||||
scc_hir_value_t *global_val = scc_hir_module_get_value(
|
||||
ctx->hir_module, val->data.global_alloc.value);
|
||||
return SCC_LIR_SYMBOL(global_val->name);
|
||||
}
|
||||
case SCC_HIR_VALUE_TAG_FUNC_ARG_REF: {
|
||||
// 函数参数:预先已分配 vreg
|
||||
unsigned int vreg = get_vreg_for_value(ctx, val_ref);
|
||||
return SCC_LIR_VREG(vreg);
|
||||
}
|
||||
case SCC_HIR_VALUE_TAG_NULLPTR: {
|
||||
return SCC_LIR_IMM(0);
|
||||
}
|
||||
default: {
|
||||
// 其他所有产生值的指令:确保其指令已生成,然后返回 vreg
|
||||
// 注意:这里可能会递归调用 translate_ir_value,因此需要先检查是否已翻译
|
||||
void *found =
|
||||
scc_hashtable_get(&ctx->value_to_vreg, (void *)(uintptr_t)val_ref);
|
||||
if (!found) {
|
||||
translate_hir_value(ctx, val, val_ref);
|
||||
}
|
||||
unsigned int vreg = (unsigned int)(uintptr_t)scc_hashtable_get(
|
||||
&ctx->value_to_vreg, (void *)(uintptr_t)val_ref);
|
||||
return SCC_LIR_VREG(vreg);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
static scc_lir_cond_t map_cmp_cond(scc_hir_op_type_t op, cbool is_float) {
|
||||
if (is_float) {
|
||||
switch (op) {
|
||||
case SCC_HIR_OP_EQ:
|
||||
return SCC_LIR_COND_FEQ;
|
||||
case SCC_HIR_OP_NEQ:
|
||||
return SCC_LIR_COND_FNE;
|
||||
case SCC_HIR_OP_LT:
|
||||
return SCC_LIR_COND_FLT;
|
||||
case SCC_HIR_OP_LE:
|
||||
return SCC_LIR_COND_FLE;
|
||||
case SCC_HIR_OP_GT:
|
||||
return SCC_LIR_COND_FGT;
|
||||
case SCC_HIR_OP_GE:
|
||||
return SCC_LIR_COND_FGE;
|
||||
default:
|
||||
Panic("invalid float cmp");
|
||||
}
|
||||
} else {
|
||||
// 默认为有符号比较 (无符号需额外处理类型)
|
||||
switch (op) {
|
||||
case SCC_HIR_OP_EQ:
|
||||
return SCC_LIR_COND_EQ;
|
||||
case SCC_HIR_OP_NEQ:
|
||||
return SCC_LIR_COND_NE;
|
||||
case SCC_HIR_OP_LT:
|
||||
return SCC_LIR_COND_SLT;
|
||||
case SCC_HIR_OP_LE:
|
||||
return SCC_LIR_COND_SLE;
|
||||
case SCC_HIR_OP_GT:
|
||||
return SCC_LIR_COND_SGT;
|
||||
case SCC_HIR_OP_GE:
|
||||
return SCC_LIR_COND_SGE;
|
||||
default:
|
||||
Panic("invalid int cmp");
|
||||
}
|
||||
}
|
||||
return SCC_LIR_COND_EQ;
|
||||
}
|
||||
|
||||
static scc_lir_op_t map_binop(scc_hir_op_type_t op, cbool is_float) {
|
||||
if (is_float) {
|
||||
switch (op) {
|
||||
case SCC_HIR_OP_ADD:
|
||||
return SCC_LIR_FADD;
|
||||
case SCC_HIR_OP_SUB:
|
||||
return SCC_LIR_FSUB;
|
||||
case SCC_HIR_OP_MUL:
|
||||
return SCC_LIR_FMUL;
|
||||
case SCC_HIR_OP_DIV:
|
||||
return SCC_LIR_FDIV;
|
||||
default:
|
||||
Panic("unsupported float binop");
|
||||
}
|
||||
} else {
|
||||
switch (op) {
|
||||
case SCC_HIR_OP_ADD:
|
||||
return SCC_LIR_ADD;
|
||||
case SCC_HIR_OP_SUB:
|
||||
return SCC_LIR_SUB;
|
||||
case SCC_HIR_OP_MUL:
|
||||
return SCC_LIR_MUL;
|
||||
case SCC_HIR_OP_DIV:
|
||||
return SCC_LIR_DIV_S;
|
||||
case SCC_HIR_OP_MOD:
|
||||
return SCC_LIR_REM_S;
|
||||
case SCC_HIR_OP_AND:
|
||||
return SCC_LIR_AND;
|
||||
case SCC_HIR_OP_OR:
|
||||
return SCC_LIR_OR;
|
||||
case SCC_HIR_OP_XOR:
|
||||
return SCC_LIR_XOR;
|
||||
case SCC_HIR_OP_SHL:
|
||||
return SCC_LIR_SHL;
|
||||
case SCC_HIR_OP_SHR:
|
||||
return SCC_LIR_SHR;
|
||||
case SCC_HIR_OP_SAR:
|
||||
return SCC_LIR_SAR;
|
||||
default:
|
||||
return map_cmp_cond(op, is_float);
|
||||
}
|
||||
}
|
||||
return SCC_LIR_NOP;
|
||||
}
|
||||
|
||||
static void translate_hir_value(ir2lir_ctx_t *ctx, scc_hir_value_t *value,
|
||||
scc_hir_value_ref_t value_ref) {
|
||||
// 防止重复翻译
|
||||
if (scc_hashtable_get(&ctx->value_to_vreg, (void *)(uintptr_t)value_ref))
|
||||
return;
|
||||
|
||||
scc_hir_type_t *ty = scc_hir_module_get_type(ctx->hir_module, value->type);
|
||||
u8 size = 0;
|
||||
scc_lir_ext_t ext = SCC_LIR_EXT_NONE;
|
||||
if (ty != nullptr) {
|
||||
ir_type_to_lir_size_ext(ty, &size, &ext);
|
||||
}
|
||||
bool is_float = (ext == SCC_LIR_EXT_FLOAT);
|
||||
|
||||
// 为当前指令分配目标虚拟寄存器
|
||||
unsigned int dst_vreg = get_vreg_for_value(ctx, value_ref);
|
||||
|
||||
switch (value->tag) {
|
||||
case SCC_HIR_VALUE_TAG_OP: {
|
||||
scc_lir_val_t lhs = ir_value_to_lir_operand(ctx, value->data.op.lhs);
|
||||
scc_lir_val_t rhs = ir_value_to_lir_operand(ctx, value->data.op.rhs);
|
||||
scc_lir_op_t op = map_binop(value->data.op.op, is_float);
|
||||
|
||||
scc_lir_instr_t instr = {.op = op,
|
||||
.size = size,
|
||||
.ext = ext,
|
||||
.to = SCC_LIR_VREG(dst_vreg),
|
||||
.arg0 = lhs,
|
||||
.arg1 = rhs};
|
||||
scc_lir_builder_add_instr(ctx, &instr);
|
||||
break;
|
||||
}
|
||||
case SCC_HIR_VALUE_TAG_LOAD: {
|
||||
scc_lir_val_t addr =
|
||||
ir_value_to_lir_operand(ctx, value->data.load.target);
|
||||
scc_lir_instr_t instr = {.op = SCC_LIR_LOAD,
|
||||
.size = size,
|
||||
.ext = ext,
|
||||
.to = SCC_LIR_VREG(dst_vreg),
|
||||
.arg0 = addr};
|
||||
scc_lir_builder_add_instr(ctx, &instr);
|
||||
break;
|
||||
}
|
||||
case SCC_HIR_VALUE_TAG_STORE: {
|
||||
scc_lir_val_t data =
|
||||
ir_value_to_lir_operand(ctx, value->data.store.value);
|
||||
scc_lir_val_t addr =
|
||||
ir_value_to_lir_operand(ctx, value->data.store.target);
|
||||
scc_lir_instr_t instr = {
|
||||
.op = SCC_LIR_STORE, .size = size, .arg0 = data, .arg1 = addr};
|
||||
scc_lir_builder_add_instr(ctx, &instr);
|
||||
break;
|
||||
}
|
||||
case SCC_HIR_VALUE_TAG_GET_ELEM_PTR: {
|
||||
// 将指针运算转换为 LEA
|
||||
scc_lir_val_t base =
|
||||
ir_value_to_lir_operand(ctx, value->data.get_elem_ptr.src_addr);
|
||||
scc_lir_val_t index = SCC_LIR_IMM(0);
|
||||
if (value->data.get_elem_ptr.index)
|
||||
ir_value_to_lir_operand(ctx, value->data.get_elem_ptr.index);
|
||||
|
||||
// 计算元素大小
|
||||
scc_hir_type_t *ptr_ty = scc_hir_module_get_type_by_value(
|
||||
ctx->hir_module, value->data.get_elem_ptr.src_addr);
|
||||
scc_hir_type_t *elem_ty = nullptr;
|
||||
if (ptr_ty->tag == SCC_HIR_TYPE_PTR) {
|
||||
elem_ty = scc_hir_module_get_type(ctx->hir_module,
|
||||
ptr_ty->data.pointer.base);
|
||||
} else if (ptr_ty->tag == SCC_HIR_TYPE_ARRAY) {
|
||||
elem_ty = scc_hir_module_get_type(ctx->hir_module,
|
||||
ptr_ty->data.array.base);
|
||||
}
|
||||
Assert(elem_ty != nullptr);
|
||||
int elem_size = 0;
|
||||
// 简化:仅处理基本类型
|
||||
if (elem_ty->tag == SCC_HIR_TYPE_i32 ||
|
||||
elem_ty->tag == SCC_HIR_TYPE_u32)
|
||||
elem_size = 4;
|
||||
else if (elem_ty->tag == SCC_HIR_TYPE_i64 ||
|
||||
elem_ty->tag == SCC_HIR_TYPE_u64)
|
||||
elem_size = 8;
|
||||
else if (elem_ty->tag == SCC_HIR_TYPE_i8 ||
|
||||
elem_ty->tag == SCC_HIR_TYPE_u8)
|
||||
elem_size = 1;
|
||||
else if (elem_ty->tag == SCC_HIR_TYPE_ARRAY)
|
||||
elem_size = 8; // FIXME ptr size
|
||||
else
|
||||
Panic("unsupported element type for getelemptr");
|
||||
|
||||
// 构造地址表达式:base + index * elem_size
|
||||
scc_lir_val_t addr =
|
||||
SCC_LIR_ADDR(dst_vreg, -1, index.data.reg, elem_size);
|
||||
// 更简单的做法:使用 LEA 指令,参数为 base 和 index,由后端展开
|
||||
scc_lir_instr_t instr = {.op = SCC_LIR_LEA,
|
||||
.size = SCC_LIR_SIZE_64,
|
||||
.to = SCC_LIR_VREG(dst_vreg),
|
||||
.arg0 = base,
|
||||
.arg1 = index};
|
||||
// 实际需要将 index * elem_size 的信息编码,这里简化,假设后端处理
|
||||
scc_lir_builder_add_instr(ctx, &instr);
|
||||
break;
|
||||
}
|
||||
case SCC_HIR_VALUE_TAG_ALLOC: {
|
||||
// alloca 指令:分配栈空间
|
||||
scc_hir_type_t *alloc_ty =
|
||||
scc_hir_module_get_type(ctx->hir_module, ty->data.pointer.base);
|
||||
// 计算大小和对齐(简化)
|
||||
int alloc_size = 8; // 默认
|
||||
if (alloc_ty->tag == SCC_HIR_TYPE_i32)
|
||||
alloc_size = 4;
|
||||
scc_lir_instr_t instr = {.op = SCC_LIR_ALLOCA,
|
||||
.size = SCC_LIR_SIZE_64,
|
||||
.to = SCC_LIR_VREG(dst_vreg),
|
||||
.metadata.alloca = {alloc_size, alloc_size}};
|
||||
scc_lir_builder_add_instr(ctx, &instr);
|
||||
break;
|
||||
}
|
||||
case SCC_HIR_VALUE_TAG_CALL: {
|
||||
scc_hir_func_t *callee =
|
||||
scc_hir_module_get_func(ctx->hir_module, value->data.call.callee);
|
||||
int arg_count = scc_vec_size(value->data.call.args);
|
||||
scc_lir_val_t *lir_args = scc_malloc(sizeof(scc_lir_val_t) * arg_count);
|
||||
for (int i = 0; i < arg_count; i++) {
|
||||
scc_hir_value_ref_t arg_ref = scc_vec_at(value->data.call.args, i);
|
||||
lir_args[i] = ir_value_to_lir_operand(ctx, arg_ref);
|
||||
}
|
||||
|
||||
scc_lir_instr_t instr = {
|
||||
.op = SCC_LIR_CALL,
|
||||
.size = size,
|
||||
.to = SCC_LIR_VREG(dst_vreg),
|
||||
.metadata.call = {.callee = callee->name,
|
||||
.args = lir_args,
|
||||
.arg_count = arg_count,
|
||||
.ret_vreg = SCC_LIR_VREG(dst_vreg),
|
||||
.clobber_mask = 0}};
|
||||
scc_lir_builder_add_instr(ctx, &instr);
|
||||
// 注意:lir_args 内存将随指令结构体复制到基本块向量中,但
|
||||
// metadata.call.args 指针仍指向 malloc 内存,
|
||||
// 这会导致内存泄漏。实际实现中应使用 arena
|
||||
// 分配或随函数释放。此处为示例简化。
|
||||
break;
|
||||
}
|
||||
case SCC_HIR_VALUE_TAG_BRANCH: {
|
||||
scc_lir_val_t cond =
|
||||
ir_value_to_lir_operand(ctx, value->data.branch.cond);
|
||||
scc_lir_bblock_id_t true_bb =
|
||||
(scc_lir_bblock_id_t)(uintptr_t)scc_hashtable_get(
|
||||
&ctx->bb_map,
|
||||
(void *)(uintptr_t)value->data.branch.true_bblock);
|
||||
scc_lir_bblock_id_t false_bb =
|
||||
(scc_lir_bblock_id_t)(uintptr_t)scc_hashtable_get(
|
||||
&ctx->bb_map,
|
||||
(void *)(uintptr_t)value->data.branch.false_bblock);
|
||||
scc_lir_instr_t instr = {
|
||||
.op = SCC_LIR_BR, .arg0 = cond, .metadata.br = {true_bb, false_bb}};
|
||||
scc_lir_builder_add_instr(ctx, &instr);
|
||||
break;
|
||||
}
|
||||
case SCC_HIR_VALUE_TAG_JUMP: {
|
||||
scc_lir_bblock_id_t target =
|
||||
(scc_lir_bblock_id_t)(usize)scc_hashtable_get(
|
||||
&ctx->bb_map, (void *)(usize)value->data.jump.target_bblock);
|
||||
scc_lir_instr_t instr = {.op = SCC_LIR_JMP,
|
||||
.metadata.jmp_target = target};
|
||||
scc_lir_builder_add_instr(ctx, &instr);
|
||||
break;
|
||||
}
|
||||
case SCC_HIR_VALUE_TAG_RET: {
|
||||
scc_lir_val_t ret_val;
|
||||
if (value->data.ret.ret_val != SCC_HIR_REF_nullptr) {
|
||||
ret_val = ir_value_to_lir_operand(ctx, value->data.ret.ret_val);
|
||||
} else {
|
||||
ret_val = SCC_LIR_NONE();
|
||||
}
|
||||
scc_lir_instr_t instr = {.op = SCC_LIR_RET,
|
||||
.metadata.ret_val = ret_val};
|
||||
scc_lir_builder_add_instr(ctx, &instr);
|
||||
break;
|
||||
}
|
||||
case SCC_HIR_VALUE_TAG_CONV: {
|
||||
// 类型转换:使用 MOV 指令配合扩展/截断
|
||||
scc_lir_val_t src =
|
||||
ir_value_to_lir_operand(ctx, value->data.conv.operand);
|
||||
scc_lir_ext_t conv_ext = SCC_LIR_EXT_NONE;
|
||||
if (value->data.conv.conv_type == CONV_SEXT)
|
||||
conv_ext = SCC_LIR_EXT_SEXT;
|
||||
else if (value->data.conv.conv_type == CONV_ZEXT)
|
||||
conv_ext = SCC_LIR_EXT_ZEXT;
|
||||
// TRUNC 用 NONE 即可(MOV 截断)
|
||||
scc_lir_instr_t instr = {.op = SCC_LIR_MOV,
|
||||
.size = size,
|
||||
.ext = conv_ext,
|
||||
.to = SCC_LIR_VREG(dst_vreg),
|
||||
.arg0 = src};
|
||||
scc_lir_builder_add_instr(ctx, &instr);
|
||||
break;
|
||||
}
|
||||
default:
|
||||
// 不生成指令的节点(如 CONST_INT, FUNC_ARG_REF)不会调用本函数
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
static void translate_func(ir2lir_ctx_t *ctx, scc_hir_func_t *ir_func) {
|
||||
scc_hir_func_meta_t *func_meta = SCC_HIR_FUNC_META(ir_func);
|
||||
|
||||
scc_lir_func_meta_t *lir_func_meta =
|
||||
scc_malloc(sizeof(scc_lir_func_meta_t));
|
||||
Assert(lir_func_meta != nullptr);
|
||||
*lir_func_meta = (scc_lir_func_meta_t){
|
||||
.attr = SCC_LIR_ATTR_NONE,
|
||||
.frame_size = 0,
|
||||
.vregs_count = 0,
|
||||
};
|
||||
scc_vec_push(ctx->lir_module->func_metas, lir_func_meta);
|
||||
ctx->current_func = ir_func;
|
||||
ir_func->meta = lir_func_meta;
|
||||
|
||||
// 预先为所有参数分配虚拟寄存器
|
||||
scc_vec_foreach(func_meta->params, i) {
|
||||
scc_hir_value_ref_t param_ref = scc_vec_at(func_meta->params, i);
|
||||
get_vreg_for_value(ctx, param_ref);
|
||||
}
|
||||
|
||||
scc_vec_foreach(ir_func->bblocks, i) {
|
||||
scc_hir_bblock_ref_t bb_ref = scc_vec_at(ir_func->bblocks, i);
|
||||
scc_hir_bblock_t *bb =
|
||||
scc_hir_module_get_bblock(ctx->hir_module, bb_ref);
|
||||
|
||||
scc_vec_init(ctx->instrs);
|
||||
|
||||
// 翻译指令
|
||||
scc_vec_foreach(bb->values, j) {
|
||||
scc_hir_value_ref_t val_ref =
|
||||
scc_vec_at(*SCC_HIR_BBLOCK_VALUES(bb), j);
|
||||
scc_hir_value_t *val =
|
||||
scc_hir_module_get_value(ctx->hir_module, val_ref);
|
||||
translate_hir_value(ctx, val, val_ref);
|
||||
}
|
||||
|
||||
bb->values = *(scc_cfg_value_vec_t *)&ctx->instrs;
|
||||
// MOVE
|
||||
scc_vec_init(ctx->instrs);
|
||||
}
|
||||
|
||||
// 清理本次函数翻译的临时映射
|
||||
scc_hashtable_drop(&ctx->bb_map);
|
||||
scc_hashtable_drop(&ctx->value_to_vreg);
|
||||
}
|
||||
|
||||
void scc_hir2lir(scc_lir_module_t *module, const scc_hir_cprog_t *cprog) {
|
||||
Assert(module != nullptr && cprog != nullptr);
|
||||
|
||||
// FIXME
|
||||
module->cfg_module = cprog->module.cfg_module;
|
||||
|
||||
ir2lir_ctx_t ctx;
|
||||
ir2lir_ctx_init(&ctx, module, &cprog->module);
|
||||
|
||||
// 1. 处理全局变量
|
||||
for (usize i = 0; i < scc_vec_size(cprog->global_vals); i++) {
|
||||
scc_hir_value_ref_t gv_ref = scc_vec_at(cprog->global_vals, i);
|
||||
scc_hir_value_t *galloc =
|
||||
scc_hir_module_get_value(&cprog->module, gv_ref);
|
||||
Assert(galloc->tag == SCC_HIR_VALUE_TAG_GLOBAL_ALLOC);
|
||||
scc_hir_value_t *val = scc_hir_module_get_value(
|
||||
&cprog->module, galloc->data.global_alloc.value);
|
||||
scc_hir_buffer_t *data = &val->data.const_array.fields;
|
||||
scc_lir_symbol_id_t id = scc_lir_module_add_data(
|
||||
module, galloc->name, SCC_CFG_SYMBOL_KIND_DATA,
|
||||
scc_vec_unsafe_get_data(*data), scc_vec_size(*data), 0);
|
||||
Assert(id != SCC_CFG_ID_nullptr);
|
||||
}
|
||||
|
||||
// 2. 处理函数声明 (外部函数)
|
||||
for (usize i = 0; i < scc_vec_size(cprog->func_decls); i++) {
|
||||
scc_hir_func_ref_t func_ref = scc_vec_at(cprog->func_decls, i);
|
||||
scc_hir_func_t *func =
|
||||
scc_hir_module_get_func(&cprog->module, func_ref);
|
||||
if (!func)
|
||||
continue;
|
||||
scc_lir_symbol_id_t id =
|
||||
scc_lir_module_add_func_decl(module, func->name);
|
||||
Assert(id != SCC_CFG_ID_nullptr);
|
||||
// 记录映射,供调用时使用
|
||||
scc_hashtable_set(&ctx.func_decl_map, (void *)(uintptr_t)func_ref,
|
||||
(void *)func->name);
|
||||
}
|
||||
|
||||
// 3. 处理函数定义
|
||||
for (usize i = 0; i < scc_vec_size(cprog->func_defs); i++) {
|
||||
scc_hir_func_ref_t func_ref = scc_vec_at(cprog->func_defs, i);
|
||||
scc_hir_func_t *func =
|
||||
scc_hir_module_get_func(&cprog->module, func_ref);
|
||||
if (!func)
|
||||
continue;
|
||||
translate_func(&ctx, func);
|
||||
}
|
||||
|
||||
ir2lir_ctx_drop(&ctx);
|
||||
}
|
||||
459
libs/ir/lir/src/scc_lir_dump.c
Normal file
459
libs/ir/lir/src/scc_lir_dump.c
Normal file
@@ -0,0 +1,459 @@
|
||||
/**
|
||||
* @file scc_lir_dump.c
|
||||
* @brief LIR 文本 dump 实现
|
||||
*/
|
||||
|
||||
#include "scc_lir_dump.h"
|
||||
#include <scc_core.h>
|
||||
|
||||
static const char *op_to_string(scc_lir_op_t op) {
|
||||
switch (op) {
|
||||
case SCC_LIR_MOV:
|
||||
return "mov";
|
||||
case SCC_LIR_LOAD:
|
||||
return "load";
|
||||
case SCC_LIR_LOAD_ADDR:
|
||||
return "load.addr";
|
||||
case SCC_LIR_STORE:
|
||||
return "store";
|
||||
case SCC_LIR_STORE_ADDR:
|
||||
return "store.addr";
|
||||
case SCC_LIR_LEA:
|
||||
return "lea";
|
||||
case SCC_LIR_ADD:
|
||||
return "add";
|
||||
case SCC_LIR_SUB:
|
||||
return "sub";
|
||||
case SCC_LIR_MUL:
|
||||
return "mul";
|
||||
case SCC_LIR_DIV_S:
|
||||
return "div.s";
|
||||
case SCC_LIR_DIV_U:
|
||||
return "div.u";
|
||||
case SCC_LIR_REM_S:
|
||||
return "rem.s";
|
||||
case SCC_LIR_REM_U:
|
||||
return "rem.u";
|
||||
case SCC_LIR_AND:
|
||||
return "and";
|
||||
case SCC_LIR_OR:
|
||||
return "or";
|
||||
case SCC_LIR_XOR:
|
||||
return "xor";
|
||||
case SCC_LIR_SHL:
|
||||
return "shl";
|
||||
case SCC_LIR_SHR:
|
||||
return "shr";
|
||||
case SCC_LIR_SAR:
|
||||
return "sar";
|
||||
case SCC_LIR_NEG:
|
||||
return "neg";
|
||||
case SCC_LIR_NOT:
|
||||
return "not";
|
||||
case SCC_LIR_FADD:
|
||||
return "fadd";
|
||||
case SCC_LIR_FSUB:
|
||||
return "fsub";
|
||||
case SCC_LIR_FMUL:
|
||||
return "fmul";
|
||||
case SCC_LIR_FDIV:
|
||||
return "fdiv";
|
||||
case SCC_LIR_FNEG:
|
||||
return "fneg";
|
||||
case SCC_LIR_FCVT:
|
||||
return "fcvt";
|
||||
case SCC_LIR_CMP:
|
||||
return "cmp";
|
||||
case SCC_LIR_BR:
|
||||
return "br";
|
||||
case SCC_LIR_JMP:
|
||||
return "jmp";
|
||||
case SCC_LIR_JMP_INDIRECT:
|
||||
return "jmp.indirect";
|
||||
case SCC_LIR_CALL:
|
||||
return "call";
|
||||
case SCC_LIR_CALL_INDIRECT:
|
||||
return "call.indirect";
|
||||
case SCC_LIR_RET:
|
||||
return "ret";
|
||||
case SCC_LIR_PARALLEL_COPY:
|
||||
return "parallel_copy";
|
||||
case SCC_LIR_VA_START:
|
||||
return "va_start";
|
||||
case SCC_LIR_VA_ARG:
|
||||
return "va_arg";
|
||||
case SCC_LIR_VA_END:
|
||||
return "va_end";
|
||||
case SCC_LIR_VA_COPY:
|
||||
return "va_copy";
|
||||
case SCC_LIR_ALLOCA:
|
||||
return "alloca";
|
||||
case SCC_LIR_NOP:
|
||||
return "nop";
|
||||
default:
|
||||
return "???";
|
||||
}
|
||||
}
|
||||
|
||||
static const char *cond_to_string(scc_lir_cond_t cond) {
|
||||
switch (cond) {
|
||||
case SCC_LIR_COND_EQ:
|
||||
return "eq";
|
||||
case SCC_LIR_COND_NE:
|
||||
return "ne";
|
||||
case SCC_LIR_COND_SLT:
|
||||
return "slt";
|
||||
case SCC_LIR_COND_SLE:
|
||||
return "sle";
|
||||
case SCC_LIR_COND_SGT:
|
||||
return "sgt";
|
||||
case SCC_LIR_COND_SGE:
|
||||
return "sge";
|
||||
case SCC_LIR_COND_ULT:
|
||||
return "ult";
|
||||
case SCC_LIR_COND_ULE:
|
||||
return "ule";
|
||||
case SCC_LIR_COND_UGT:
|
||||
return "ugt";
|
||||
case SCC_LIR_COND_UGE:
|
||||
return "uge";
|
||||
case SCC_LIR_COND_FEQ:
|
||||
return "feq";
|
||||
case SCC_LIR_COND_FNE:
|
||||
return "fne";
|
||||
case SCC_LIR_COND_FLT:
|
||||
return "flt";
|
||||
case SCC_LIR_COND_FLE:
|
||||
return "fle";
|
||||
case SCC_LIR_COND_FGT:
|
||||
return "fgt";
|
||||
case SCC_LIR_COND_FGE:
|
||||
return "fge";
|
||||
default:
|
||||
return "???";
|
||||
}
|
||||
}
|
||||
|
||||
static void dump_operand(scc_lir_dump_ctx_t *ctx, const scc_lir_val_t *op) {
|
||||
scc_tree_dump_t *td = ctx->dump_ctx;
|
||||
|
||||
switch (op->kind) {
|
||||
case SCC_LIR_INSTR_KIND_NONE:
|
||||
scc_tree_dump_append(td, "_");
|
||||
break;
|
||||
case SCC_LIR_INSTR_KIND_VREG:
|
||||
scc_tree_dump_append_fmt(td, "%%%u", op->data.reg);
|
||||
break;
|
||||
case SCC_LIR_INSTR_KIND_PREG:
|
||||
scc_tree_dump_append_fmt(td, "Phy%u", op->data.reg);
|
||||
break;
|
||||
case SCC_LIR_INSTR_KIND_IMM:
|
||||
// TODO hack ap
|
||||
scc_tree_dump_append_fmt(td, "%lld", op->data.imm.data.digit);
|
||||
break;
|
||||
case SCC_LIR_INSTR_KIND_FIMM:
|
||||
TODO();
|
||||
scc_tree_dump_append_fmt(td, "%lf", op->data.fimm);
|
||||
break;
|
||||
case SCC_LIR_INSTR_KIND_SYMBOL:
|
||||
scc_tree_dump_append_fmt(td, "@%s",
|
||||
op->data.symbol ? op->data.symbol : "<null>");
|
||||
break;
|
||||
case SCC_LIR_INSTR_KIND_ADDR: {
|
||||
const scc_lir_addr_t *addr = &op->data.addr;
|
||||
scc_tree_dump_append(td, "[");
|
||||
if (addr->base != -1) {
|
||||
scc_tree_dump_append_fmt(td, "%%%d", addr->base);
|
||||
}
|
||||
if (addr->index != -1) {
|
||||
scc_tree_dump_append_fmt(td, " + %%%d * %d", addr->index,
|
||||
addr->scale);
|
||||
}
|
||||
if (addr->offset != 0) {
|
||||
if (addr->offset > 0)
|
||||
scc_tree_dump_append_fmt(td, " + %lld",
|
||||
(long long)addr->offset);
|
||||
else
|
||||
scc_tree_dump_append_fmt(td, " - %lld",
|
||||
-(long long)addr->offset);
|
||||
}
|
||||
if (addr->base == -1 && addr->index == -1 && addr->offset == 0)
|
||||
scc_tree_dump_append(td, "0");
|
||||
scc_tree_dump_append(td, "]");
|
||||
break;
|
||||
}
|
||||
default:
|
||||
scc_tree_dump_append(td, "<?>");
|
||||
}
|
||||
}
|
||||
|
||||
static void dump_size_ext(scc_lir_dump_ctx_t *ctx, u8 size, scc_lir_ext_t ext) {
|
||||
scc_tree_dump_t *td = ctx->dump_ctx;
|
||||
const char *size_str = "";
|
||||
if (size == SCC_LIR_SIZE_8)
|
||||
size_str = "8";
|
||||
else if (size == SCC_LIR_SIZE_16)
|
||||
size_str = "16";
|
||||
else if (size == SCC_LIR_SIZE_32)
|
||||
size_str = "32";
|
||||
else if (size == SCC_LIR_SIZE_64)
|
||||
size_str = "64";
|
||||
|
||||
const char *ext_str = "";
|
||||
if (ext == SCC_LIR_EXT_SEXT)
|
||||
ext_str = ".s";
|
||||
else if (ext == SCC_LIR_EXT_ZEXT)
|
||||
ext_str = ".u";
|
||||
else if (ext == SCC_LIR_EXT_FLOAT)
|
||||
ext_str = ".f";
|
||||
|
||||
if (size_str[0] || ext_str[0]) {
|
||||
scc_tree_dump_append_fmt(td, ".%s%s", size_str, ext_str);
|
||||
}
|
||||
}
|
||||
|
||||
void scc_lir_dump_ins(scc_lir_dump_ctx_t *ctx, const scc_lir_instr_t *ins) {
|
||||
Assert(ctx != nullptr && ins != nullptr);
|
||||
scc_tree_dump_t *td = ctx->dump_ctx;
|
||||
|
||||
scc_tree_dump_begin_line(td);
|
||||
scc_tree_dump_append(td, " ");
|
||||
// 输出操作码(带节点颜色)
|
||||
scc_tree_dump_node(td, "%s", op_to_string(ins->op));
|
||||
|
||||
// 输出宽度和扩展标志
|
||||
dump_size_ext(ctx, ins->size, ins->ext);
|
||||
scc_tree_dump_append(td, " ");
|
||||
|
||||
switch (ins->op) {
|
||||
case SCC_LIR_MOV:
|
||||
case SCC_LIR_LOAD:
|
||||
case SCC_LIR_LOAD_ADDR:
|
||||
dump_operand(ctx, &ins->to);
|
||||
scc_tree_dump_append(td, " <- ");
|
||||
dump_operand(ctx, &ins->arg0);
|
||||
break;
|
||||
case SCC_LIR_LEA:
|
||||
case SCC_LIR_NEG:
|
||||
case SCC_LIR_NOT:
|
||||
case SCC_LIR_FNEG:
|
||||
case SCC_LIR_FCVT:
|
||||
case SCC_LIR_ALLOCA:
|
||||
dump_operand(ctx, &ins->to);
|
||||
if (ins->op != SCC_LIR_ALLOCA) {
|
||||
scc_tree_dump_append(td, ", ");
|
||||
dump_operand(ctx, &ins->arg0);
|
||||
// alloca 额外信息在 metadata 中,但通常只关注 to
|
||||
}
|
||||
break;
|
||||
|
||||
case SCC_LIR_STORE:
|
||||
case SCC_LIR_STORE_ADDR:
|
||||
dump_operand(ctx, &ins->arg0);
|
||||
scc_tree_dump_append(td, " -> ");
|
||||
dump_operand(ctx, &ins->arg1);
|
||||
break;
|
||||
|
||||
case SCC_LIR_ADD:
|
||||
case SCC_LIR_SUB:
|
||||
case SCC_LIR_MUL:
|
||||
case SCC_LIR_DIV_S:
|
||||
case SCC_LIR_DIV_U:
|
||||
case SCC_LIR_REM_S:
|
||||
case SCC_LIR_REM_U:
|
||||
case SCC_LIR_AND:
|
||||
case SCC_LIR_OR:
|
||||
case SCC_LIR_XOR:
|
||||
case SCC_LIR_SHL:
|
||||
case SCC_LIR_SHR:
|
||||
case SCC_LIR_SAR:
|
||||
case SCC_LIR_FADD:
|
||||
case SCC_LIR_FSUB:
|
||||
case SCC_LIR_FMUL:
|
||||
case SCC_LIR_FDIV:
|
||||
dump_operand(ctx, &ins->to);
|
||||
scc_tree_dump_append(td, ", ");
|
||||
dump_operand(ctx, &ins->arg0);
|
||||
scc_tree_dump_append(td, ", ");
|
||||
dump_operand(ctx, &ins->arg1);
|
||||
break;
|
||||
|
||||
case SCC_LIR_CMP:
|
||||
dump_operand(ctx, &ins->to);
|
||||
scc_tree_dump_append_fmt(td, ", %s, ",
|
||||
cond_to_string(ins->metadata.cond));
|
||||
dump_operand(ctx, &ins->arg0);
|
||||
scc_tree_dump_append(td, ", ");
|
||||
dump_operand(ctx, &ins->arg1);
|
||||
break;
|
||||
|
||||
case SCC_LIR_BR:
|
||||
dump_operand(ctx, &ins->arg0);
|
||||
scc_tree_dump_append_fmt(td, ", BB#%zu, BB#%zu",
|
||||
ins->metadata.br.true_target,
|
||||
ins->metadata.br.false_target);
|
||||
break;
|
||||
|
||||
case SCC_LIR_JMP:
|
||||
scc_tree_dump_append_fmt(td, "BB#%zu", ins->metadata.jmp_target);
|
||||
break;
|
||||
|
||||
case SCC_LIR_JMP_INDIRECT:
|
||||
dump_operand(ctx, &ins->arg0);
|
||||
break;
|
||||
|
||||
case SCC_LIR_CALL: {
|
||||
const struct scc_lir_call *c = &ins->metadata.call;
|
||||
if (c->ret_vreg.kind != SCC_LIR_INSTR_KIND_NONE) {
|
||||
dump_operand(ctx, &c->ret_vreg);
|
||||
scc_tree_dump_append(td, " = ");
|
||||
}
|
||||
scc_tree_dump_append_fmt(td, "call @%s(",
|
||||
c->callee ? c->callee : "<null>");
|
||||
for (u8 i = 0; i < c->arg_count; i++) {
|
||||
if (i > 0)
|
||||
scc_tree_dump_append(td, ", ");
|
||||
dump_operand(ctx, &c->args[i]);
|
||||
}
|
||||
scc_tree_dump_append_fmt(td, ") clobber=0x%llx",
|
||||
(unsigned long long)c->clobber_mask);
|
||||
break;
|
||||
}
|
||||
|
||||
case SCC_LIR_CALL_INDIRECT: {
|
||||
const struct scc_lir_call_indirect *c = &ins->metadata.call_indirect;
|
||||
if (c->ret_vreg.kind != SCC_LIR_INSTR_KIND_NONE) {
|
||||
dump_operand(ctx, &c->ret_vreg);
|
||||
scc_tree_dump_append(td, " = ");
|
||||
}
|
||||
scc_tree_dump_append(td, "call ");
|
||||
dump_operand(ctx, &c->target);
|
||||
scc_tree_dump_append(td, "(");
|
||||
for (u8 i = 0; i < c->arg_count; i++) {
|
||||
if (i > 0)
|
||||
scc_tree_dump_append(td, ", ");
|
||||
dump_operand(ctx, &c->args[i]);
|
||||
}
|
||||
scc_tree_dump_append_fmt(td, ") clobber=0x%llx",
|
||||
(unsigned long long)c->clobber_mask);
|
||||
break;
|
||||
}
|
||||
|
||||
case SCC_LIR_RET:
|
||||
if (ins->metadata.ret_val.kind != SCC_LIR_INSTR_KIND_NONE) {
|
||||
dump_operand(ctx, &ins->metadata.ret_val);
|
||||
}
|
||||
break;
|
||||
|
||||
case SCC_LIR_PARALLEL_COPY: {
|
||||
const struct scc_lir_parallel_copy *pc = &ins->metadata.parallel_copy;
|
||||
scc_tree_dump_append(td, "[");
|
||||
for (u8 i = 0; i < pc->num_copies; i++) {
|
||||
if (i > 0)
|
||||
scc_tree_dump_append(td, ", ");
|
||||
dump_operand(ctx, &pc->dests[i]);
|
||||
scc_tree_dump_append(td, " <- ");
|
||||
dump_operand(ctx, &pc->srcs[i]);
|
||||
}
|
||||
scc_tree_dump_append(td, "]");
|
||||
break;
|
||||
}
|
||||
|
||||
case SCC_LIR_VA_START:
|
||||
dump_operand(ctx, &ins->metadata.va_start.ap);
|
||||
scc_tree_dump_append(td, ", ");
|
||||
dump_operand(ctx, &ins->metadata.va_start.last);
|
||||
break;
|
||||
|
||||
case SCC_LIR_VA_ARG:
|
||||
dump_operand(ctx, &ins->metadata.va_arg.to);
|
||||
scc_tree_dump_append(td, " = va_arg ");
|
||||
dump_operand(ctx, &ins->metadata.va_arg.ap);
|
||||
scc_tree_dump_append_fmt(
|
||||
td, ", size=%u, align=%u, float=%d", ins->metadata.va_arg.type_size,
|
||||
ins->metadata.va_arg.type_align, ins->metadata.va_arg.is_float);
|
||||
break;
|
||||
|
||||
case SCC_LIR_VA_END:
|
||||
dump_operand(ctx, &ins->metadata.va_end.ap);
|
||||
break;
|
||||
|
||||
case SCC_LIR_VA_COPY:
|
||||
dump_operand(ctx, &ins->metadata.va_copy.dest);
|
||||
scc_tree_dump_append(td, ", ");
|
||||
dump_operand(ctx, &ins->metadata.va_copy.src);
|
||||
break;
|
||||
|
||||
case SCC_LIR_NOP:
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
void scc_lir_dump_bblock(scc_lir_dump_ctx_t *ctx, const scc_lir_bblock_t *bb) {
|
||||
Assert(ctx != nullptr && bb != nullptr);
|
||||
scc_tree_dump_t *td = ctx->dump_ctx;
|
||||
|
||||
// 基本块头部
|
||||
scc_tree_dump_begin_line(td);
|
||||
scc_tree_dump_node(td, "BB%zu", bb->id);
|
||||
if (bb->name) {
|
||||
scc_tree_dump_append_fmt(td, " (%s)", bb->name);
|
||||
}
|
||||
scc_tree_dump_append(td, ":");
|
||||
|
||||
// 输出每条指令
|
||||
scc_lir_instr_vec_t *instrs = SCC_LIR_BBLOCK_VALUES(bb);
|
||||
scc_vec_foreach(*instrs, i) {
|
||||
const scc_lir_instr_t *ins = &scc_vec_at(*instrs, i);
|
||||
scc_lir_dump_ins(ctx, ins);
|
||||
}
|
||||
}
|
||||
|
||||
void scc_lir_dump_func(scc_lir_dump_ctx_t *ctx, const scc_lir_func_t *func) {
|
||||
Assert(ctx != nullptr && func != nullptr);
|
||||
scc_tree_dump_t *td = ctx->dump_ctx;
|
||||
|
||||
scc_lir_func_meta_t *meta = SCC_LIR_FUNC_META(func);
|
||||
// 函数头部
|
||||
scc_tree_dump_begin_line(td);
|
||||
scc_tree_dump_node(td, "func @%s", func->name ? func->name : "<anon>");
|
||||
scc_tree_dump_append_fmt(td, " (vregs: %u, frame: %d)", meta->vregs_count,
|
||||
meta->frame_size);
|
||||
if (meta->attr != SCC_LIR_ATTR_NONE) {
|
||||
scc_tree_dump_append(td, " [attr:");
|
||||
if (meta->attr & SCC_LIR_ATTR_STATIC)
|
||||
scc_tree_dump_append(td, " static");
|
||||
if (meta->attr & SCC_LIR_ATTR_WEAK)
|
||||
scc_tree_dump_append(td, " weak");
|
||||
scc_tree_dump_append(td, " ]");
|
||||
}
|
||||
scc_tree_dump_append(td, " {");
|
||||
|
||||
// 输出所有基本块
|
||||
for (usize i = 0; i < scc_vec_size(func->bblocks); ++i) {
|
||||
scc_cfg_bblock_id_t id = scc_vec_at(func->bblocks, i);
|
||||
const scc_cfg_bblock_t *bb =
|
||||
scc_cfg_module_unsafe_get_bblock(&ctx->module->cfg_module, id);
|
||||
scc_lir_dump_bblock(ctx, bb);
|
||||
}
|
||||
|
||||
scc_tree_dump_begin_line(td);
|
||||
scc_tree_dump_append(td, "}");
|
||||
}
|
||||
|
||||
void scc_lir_dump_module(scc_lir_dump_ctx_t *ctx) {
|
||||
scc_vec_foreach(ctx->module->cfg_module.symbols, i) {
|
||||
// FIXME 0 is null
|
||||
if (i == 0)
|
||||
continue;
|
||||
scc_lir_symbol_t *sym = &scc_vec_at(ctx->module->cfg_module.symbols, i);
|
||||
scc_tree_dump_begin_line(ctx->dump_ctx);
|
||||
scc_tree_dump_node(ctx->dump_ctx, "symbol");
|
||||
scc_tree_dump_append_fmt(ctx->dump_ctx, " %s", sym->name);
|
||||
}
|
||||
scc_vec_foreach(ctx->module->cfg_module.funcs, i) {
|
||||
if (i == 0)
|
||||
continue;
|
||||
scc_lir_dump_func(ctx, &scc_vec_at(ctx->module->cfg_module.funcs, i));
|
||||
}
|
||||
}
|
||||
79
libs/ir/lir/src/scc_lir_module.c
Normal file
79
libs/ir/lir/src/scc_lir_module.c
Normal file
@@ -0,0 +1,79 @@
|
||||
#include <scc_lir_module.h>
|
||||
|
||||
void scc_lir_module_init(scc_lir_module_t *module) {
|
||||
// FIXME
|
||||
// module->module
|
||||
|
||||
scc_vec_init(module->func_metas);
|
||||
scc_vec_init(module->symbol_metas);
|
||||
}
|
||||
|
||||
void scc_lir_module_drop(scc_lir_module_t *module) {
|
||||
scc_vec_free(module->func_metas);
|
||||
scc_vec_free(module->symbol_metas);
|
||||
}
|
||||
|
||||
scc_lir_symbol_id_t scc_lir_module_add_func_decl(scc_lir_module_t *module,
|
||||
const char *name) {
|
||||
if (!module || !name)
|
||||
return SCC_CFG_ID_nullptr;
|
||||
|
||||
scc_lir_symbol_meta_t *meta = scc_malloc(sizeof(scc_lir_symbol_meta_t));
|
||||
Assert(meta != nullptr);
|
||||
scc_lir_symbol_t sym = {.name = name,
|
||||
.kind = SCC_CFG_SYMBOL_KIND_FUNC,
|
||||
.linkage = SCC_CFG_SYMBOL_LINK_GLOABL,
|
||||
.meta = meta};
|
||||
meta->func.func = nullptr;
|
||||
|
||||
scc_vec_push(module->symbol_metas, meta);
|
||||
scc_cfg_symbol_id_t id =
|
||||
scc_cfg_module_add_symbol(&module->cfg_module, &sym);
|
||||
if (id == SCC_CFG_ID_nullptr) {
|
||||
Panic("scc_lir: add func decl '%s' failed", name);
|
||||
}
|
||||
return id;
|
||||
}
|
||||
|
||||
scc_lir_symbol_id_t scc_lir_module_add_data(scc_lir_module_t *module,
|
||||
const char *name,
|
||||
scc_cfg_symbol_kind_t kind,
|
||||
const u8 *init_data, usize size,
|
||||
u32 align) {
|
||||
if (!module || !name)
|
||||
return SCC_CFG_ID_nullptr;
|
||||
if (kind != SCC_CFG_SYMBOL_KIND_DATA && kind != SCC_CFG_SYMBOL_KIND_EXTERN)
|
||||
return SCC_CFG_ID_nullptr;
|
||||
|
||||
scc_lir_symbol_meta_t *meta = scc_malloc(sizeof(scc_lir_symbol_meta_t));
|
||||
Assert(meta != nullptr);
|
||||
scc_lir_symbol_t sym = {.name = name, .kind = kind, .meta = meta};
|
||||
*meta = (scc_lir_symbol_meta_t){
|
||||
.data = {.init_data = nullptr, .size = size, .align = align},
|
||||
};
|
||||
|
||||
/* 对于数据定义,复制初始化数据(若有) */
|
||||
if (kind == SCC_CFG_SYMBOL_KIND_DATA && init_data && size > 0) {
|
||||
meta->data.init_data = scc_malloc(size);
|
||||
if (!meta->data.init_data) {
|
||||
LOG_FATAL("out of memory for data symbol '%s'", name);
|
||||
}
|
||||
scc_memcpy(meta->data.init_data, init_data, size);
|
||||
} else if (kind == SCC_CFG_SYMBOL_KIND_DATA && !init_data) {
|
||||
/* 零初始化:分配并清零 */
|
||||
meta->data.init_data = scc_calloc(1, size);
|
||||
if (!meta->data.init_data) {
|
||||
LOG_FATAL("out of memory for data symbol '%s'", name);
|
||||
}
|
||||
}
|
||||
|
||||
scc_vec_push(module->symbol_metas, meta);
|
||||
scc_cfg_symbol_id_t id =
|
||||
scc_cfg_module_add_symbol(&module->cfg_module, &sym);
|
||||
if (id = SCC_CFG_ID_nullptr) {
|
||||
/* 冲突时释放已分配的数据 */
|
||||
scc_free(meta->data.init_data);
|
||||
return SCC_CFG_ID_nullptr;
|
||||
}
|
||||
return id;
|
||||
}
|
||||
File diff suppressed because it is too large
Load Diff
50
src/main.c
50
src/main.c
@@ -3,12 +3,12 @@
|
||||
#include <scc_parser.h>
|
||||
#include <scc_pproc.h>
|
||||
|
||||
#include <ast_dump.h>
|
||||
#include <hir_dump.h>
|
||||
#include <scc_ast2ir.h>
|
||||
#include <scc_ast_dump.h>
|
||||
#include <scc_hir2lir.h>
|
||||
#include <scc_hir_dump.h>
|
||||
#include <scc_lir_dump.h>
|
||||
|
||||
// #include <scc_ir2lir.h>
|
||||
// #include <scc_lir_dump.h>
|
||||
// #include <scc_ir2mcode.h>
|
||||
// #include <sccf2pe.h>
|
||||
|
||||
@@ -231,7 +231,7 @@ sstream_drop:
|
||||
} else {
|
||||
scc_tree_dump_init(&tree_dump, false);
|
||||
}
|
||||
scc_hir_dump_ctx_init(&ir_dump_ctx, &tree_dump, &cprog);
|
||||
scc_hir_dump_init(&ir_dump_ctx, &tree_dump, &cprog);
|
||||
// scc_ir_dump_cprog(&ir_dump_ctx);
|
||||
scc_hir_dump_cprog_linear(&ir_dump_ctx);
|
||||
|
||||
@@ -241,28 +241,26 @@ sstream_drop:
|
||||
return 0;
|
||||
}
|
||||
|
||||
// scc_lir_builder_t lir_builder;
|
||||
// scc_lir_module_t lir_module;
|
||||
// scc_lir_module_init(&lir_module);
|
||||
// scc_lir_builder_init(&lir_builder, &lir_module);
|
||||
// scc_ir2lir(&lir_builder, &cprog);
|
||||
// if (config.emit_lir) {
|
||||
// scc_lir_dump_ctx_t lir_dump_ctx;
|
||||
// scc_tree_dump_t tree_dump;
|
||||
// if (fp == nullptr) {
|
||||
// scc_tree_dump_init(&tree_dump, true);
|
||||
// } else {
|
||||
// scc_tree_dump_init(&tree_dump, false);
|
||||
// }
|
||||
// scc_lir_dump_init(&lir_dump_ctx, &tree_dump);
|
||||
// // scc_ir_dump_cprog(&ir_dump_ctx);
|
||||
// scc_lir_dump_module(&lir_dump_ctx, &lir_module);
|
||||
scc_lir_module_t lir_module;
|
||||
scc_lir_module_init(&lir_module);
|
||||
scc_hir2lir(&lir_module, &cprog);
|
||||
if (config.emit_lir) {
|
||||
scc_lir_dump_ctx_t lir_dump_ctx;
|
||||
scc_tree_dump_t tree_dump;
|
||||
if (fp == nullptr) {
|
||||
scc_tree_dump_init(&tree_dump, true);
|
||||
} else {
|
||||
scc_tree_dump_init(&tree_dump, false);
|
||||
}
|
||||
scc_lir_dump_init(&lir_dump_ctx, &tree_dump, &lir_module);
|
||||
// scc_ir_dump_cprog(&ir_dump_ctx);
|
||||
scc_lir_dump_module(&lir_dump_ctx);
|
||||
|
||||
// scc_tree_dump_flush(&tree_dump, tree_dump_output,
|
||||
// fp == nullptr ? scc_stdout : fp);
|
||||
// scc_tree_dump_drop(&tree_dump);
|
||||
// return 0;
|
||||
// }
|
||||
scc_tree_dump_flush(&tree_dump, tree_dump_output,
|
||||
fp == nullptr ? scc_stdout : fp);
|
||||
scc_tree_dump_drop(&tree_dump);
|
||||
return 0;
|
||||
}
|
||||
|
||||
// scc_ir2mcode_ctx_t ir2mcode_ctx;
|
||||
// sccf_builder_t sccf_builder;
|
||||
|
||||
Reference in New Issue
Block a user