refactor(log): 统一日志系统并添加链接器实现
- 为所有模块添加统一的 scc_*_log.h 日志头文件,删除旧的 lexer_log.h/c - 将运行时日志从 scc_utils 迁移到 scc_core 目录,统一日志管理 - 在解析器表达式/语句/类型解析中添加 LOG_TRACE 调试日志 - 实现 SCCF 链接器 (sccf_linker) 支持多目标文件链接 - 重构 CLI 主程序 (main.c/config.c),新增 cmd_log.h 调试支持 - 优化 x86 指令编码操作数对齐检查 - 修复预处理器的指令处理和宏展开逻辑
This commit is contained in:
26
libs/ir/hir/include/scc_hir_log.h
Normal file
26
libs/ir/hir/include/scc_hir_log.h
Normal file
@@ -0,0 +1,26 @@
|
||||
#ifndef __SCC_HIR_LOG_H__
|
||||
#define __SCC_HIR_LOG_H__
|
||||
|
||||
typedef struct logger logger_t;
|
||||
extern logger_t __scc_hir_log;
|
||||
extern logger_t __scc_hir_user;
|
||||
|
||||
#define SCC_LOG_HANDLER &__scc_hir_user
|
||||
#define LOG_DEFAULT_HANDLER &__scc_hir_log
|
||||
#include <scc_log.h>
|
||||
|
||||
#ifdef __SCC_HIR_LOG_IMPL__
|
||||
logger_t __scc_hir_log = {
|
||||
.name = "hir",
|
||||
.level = LOG_LEVEL_ALL,
|
||||
.handler = log_default_handler,
|
||||
};
|
||||
|
||||
logger_t __scc_hir_user = {
|
||||
.name = "hir",
|
||||
.level = LOG_LEVEL_ALL,
|
||||
.user_handler = scc_log_handler,
|
||||
};
|
||||
#endif
|
||||
|
||||
#endif
|
||||
@@ -1,3 +1,6 @@
|
||||
#define __SCC_HIR_LOG_IMPL__
|
||||
#include <scc_hir_log.h>
|
||||
|
||||
#include <scc_hir.h>
|
||||
|
||||
void scc_hir_type_init(scc_hir_type_t *in, scc_hir_type_tag_t tag) {
|
||||
|
||||
@@ -1,3 +1,5 @@
|
||||
#include <scc_hir_log.h>
|
||||
|
||||
#include <scc_hir_builder.h>
|
||||
#include <scc_hir_def.h>
|
||||
#include <scc_hir_prog.h>
|
||||
|
||||
@@ -1,3 +1,5 @@
|
||||
#include <scc_hir_log.h>
|
||||
|
||||
#include <scc_hir_dump.h>
|
||||
#include <scc_hir_prog.h>
|
||||
#include <scc_tree_dump.h>
|
||||
|
||||
26
libs/ir/lir/include/scc_lir_log.h
Normal file
26
libs/ir/lir/include/scc_lir_log.h
Normal file
@@ -0,0 +1,26 @@
|
||||
#ifndef __SCC_LIR_LOG_H__
|
||||
#define __SCC_LIR_LOG_H__
|
||||
|
||||
typedef struct logger logger_t;
|
||||
extern logger_t __scc_lir_log;
|
||||
extern logger_t __scc_lir_user;
|
||||
|
||||
#define SCC_LOG_HANDLER &__scc_lir_user
|
||||
#define LOG_DEFAULT_HANDLER &__scc_lir_log
|
||||
#include <scc_log.h>
|
||||
|
||||
#ifdef __SCC_LIR_LOG_IMPL__
|
||||
logger_t __scc_lir_log = {
|
||||
.name = "lir",
|
||||
.level = LOG_LEVEL_ALL,
|
||||
.handler = log_default_handler,
|
||||
};
|
||||
|
||||
logger_t __scc_lir_user = {
|
||||
.name = "lir",
|
||||
.level = LOG_LEVEL_ALL,
|
||||
.user_handler = scc_log_handler,
|
||||
};
|
||||
#endif
|
||||
|
||||
#endif
|
||||
@@ -2,6 +2,7 @@
|
||||
* @file scc_hir2lir.c
|
||||
* @brief 将高级 IR (scc_ir) 降级为低层 LIR (scc_lir)
|
||||
*/
|
||||
#include <scc_lir_log.h>
|
||||
|
||||
#include <scc_hashtable.h>
|
||||
#include <scc_hir2lir.h>
|
||||
@@ -323,6 +324,7 @@ static void translate_hir_value(ir2lir_ctx_t *ctx, scc_hir_value_t *value,
|
||||
if (scc_hashtable_get(&ctx->value_to_vreg, (void *)(uintptr_t)value_ref))
|
||||
return;
|
||||
|
||||
LOG_TRACE("translate val_ref=%zu tag=%d", (size_t)value_ref, value->tag);
|
||||
u8 size_bits = 0;
|
||||
scc_lir_ext_t ext = SCC_LIR_EXT_NONE;
|
||||
ir_type_to_lir_size_ext(ctx->hir_module, value->type, &size_bits, &ext);
|
||||
@@ -643,6 +645,9 @@ static void translate_hir_value(ir2lir_ctx_t *ctx, scc_hir_value_t *value,
|
||||
|
||||
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);
|
||||
LOG_DEBUG("translate func '%s' (%zu bblocks)",
|
||||
ir_func->name ? ir_func->name : "?",
|
||||
scc_vec_size(ir_func->bblocks));
|
||||
|
||||
scc_lir_func_meta_t *lir_func_meta =
|
||||
scc_malloc(sizeof(scc_lir_func_meta_t));
|
||||
@@ -689,6 +694,8 @@ static void translate_func(ir2lir_ctx_t *ctx, scc_hir_func_t *ir_func) {
|
||||
}
|
||||
|
||||
void scc_hir2lir(scc_lir_module_t *module, scc_hir_cprog_t *cprog) {
|
||||
LOG_INFO("HIR -> LIR lowering start (%zu funcs)",
|
||||
scc_vec_size(cprog->func_defs));
|
||||
Assert(module != nullptr && cprog != nullptr);
|
||||
|
||||
// FIXME
|
||||
|
||||
2
libs/ir/lir/src/scc_lir.c
Normal file
2
libs/ir/lir/src/scc_lir.c
Normal file
@@ -0,0 +1,2 @@
|
||||
#define __SCC_LIR_LOG_IMPL__
|
||||
#include <scc_lir_log.h>
|
||||
@@ -1,3 +1,5 @@
|
||||
#include <scc_lir_log.h>
|
||||
|
||||
#include <scc_lir_module.h>
|
||||
|
||||
void scc_lir_module_init(scc_lir_module_t *lir_module) {
|
||||
@@ -13,7 +15,8 @@ void scc_lir_module_drop(scc_lir_module_t *lir_module) {
|
||||
scc_lir_symbol_meta_t *meta = lir_module->symbol_metas.data[i];
|
||||
usize cfg_idx = i + 1; /* cfg_module.symbols[0] is null sentinel */
|
||||
if (cfg_idx < scc_vec_size(lir_module->cfg_module.symbols)) {
|
||||
scc_cfg_symbol_t *sym = &lir_module->cfg_module.symbols.data[cfg_idx];
|
||||
scc_cfg_symbol_t *sym =
|
||||
&lir_module->cfg_module.symbols.data[cfg_idx];
|
||||
if (sym->kind == SCC_CFG_SYMBOL_KIND_DATA && meta->data.init_data) {
|
||||
scc_free(meta->data.init_data);
|
||||
}
|
||||
|
||||
26
libs/ir/mir/include/scc_mir_log.h
Normal file
26
libs/ir/mir/include/scc_mir_log.h
Normal file
@@ -0,0 +1,26 @@
|
||||
#ifndef __SCC_MIR_LOG_H__
|
||||
#define __SCC_MIR_LOG_H__
|
||||
|
||||
typedef struct logger logger_t;
|
||||
extern logger_t __scc_mir_log;
|
||||
extern logger_t __scc_mir_user;
|
||||
|
||||
#define SCC_LOG_HANDLER &__scc_mir_user
|
||||
#define LOG_DEFAULT_HANDLER &__scc_mir_log
|
||||
#include <scc_log.h>
|
||||
|
||||
#ifdef __SCC_MIR_LOG_IMPL__
|
||||
logger_t __scc_mir_log = {
|
||||
.name = "mir",
|
||||
.level = LOG_LEVEL_ALL,
|
||||
.handler = log_default_handler,
|
||||
};
|
||||
|
||||
logger_t __scc_mir_user = {
|
||||
.name = "mir",
|
||||
.level = LOG_LEVEL_ALL,
|
||||
.user_handler = scc_log_handler,
|
||||
};
|
||||
#endif
|
||||
|
||||
#endif
|
||||
@@ -1,3 +1,5 @@
|
||||
#include <scc_mir_log.h>
|
||||
|
||||
#include <arch/scc_x86_mir.h>
|
||||
|
||||
static inline scc_x86_iform_t
|
||||
|
||||
@@ -1,9 +1,14 @@
|
||||
#include <scc_mir_log.h>
|
||||
|
||||
#include <arch/scc_x86_isel.h>
|
||||
#include <scc_lir2mir.h>
|
||||
#include <scc_mir_module.h>
|
||||
#include <target/scc_win64.h>
|
||||
|
||||
void scc_lir2mir(scc_mir_module_t *mir_module,
|
||||
const scc_lir_module_t *lir_module) {
|
||||
LOG_INFO("LIR -> MIR conversion (%zu funcs)",
|
||||
scc_vec_size(lir_module->cfg_module.funcs));
|
||||
// Move
|
||||
mir_module->cfg_module = lir_module->cfg_module;
|
||||
|
||||
|
||||
@@ -1,3 +1,6 @@
|
||||
#define __SCC_MIR_LOG_IMPL__
|
||||
#include <scc_mir_log.h>
|
||||
|
||||
#include <scc_mir.h>
|
||||
|
||||
void scc_mir_func_meta_init(scc_mir_func_meta_t *func_meta) {
|
||||
|
||||
@@ -1,3 +1,5 @@
|
||||
#include <scc_mir_log.h>
|
||||
|
||||
#include <arch/scc_x86_mir.h>
|
||||
#include <arch/scc_x86_reg_alloc.h>
|
||||
#include <target/scc_win64.h>
|
||||
@@ -11,17 +13,23 @@
|
||||
|
||||
void scc_frame_layout(scc_frame_layout_t *ctx, scc_mir_module_t *module) {
|
||||
Assert(ctx && ctx->impl_fn && module);
|
||||
LOG_DEBUG(" frame layout for %zu funcs",
|
||||
scc_vec_size(module->cfg_module.funcs));
|
||||
scc_vec_foreach(module->cfg_module.funcs, i) {
|
||||
if (i == 0)
|
||||
continue;
|
||||
scc_mir_func_t *func = &scc_vec_at(module->cfg_module.funcs, i);
|
||||
ctx->offset = SCC_MIR_FUNC_META(func)->frame_size;
|
||||
LOG_TRACE(" func[%zu] frame_size=%zu", (size_t)i,
|
||||
(size_t)ctx->offset);
|
||||
ctx->impl_fn(ctx, module, func);
|
||||
}
|
||||
}
|
||||
|
||||
void scc_prolog_epilog(scc_prolog_epilog_t *ctx, scc_mir_module_t *module) {
|
||||
Assert(ctx && ctx->epilog && ctx->prolog && module);
|
||||
LOG_DEBUG(" prolog/epilog for %zu funcs",
|
||||
scc_vec_size(module->cfg_module.funcs));
|
||||
scc_vec_foreach(module->cfg_module.funcs, i) {
|
||||
if (i == 0)
|
||||
continue;
|
||||
@@ -58,29 +66,38 @@ void scc_prolog_epilog(scc_prolog_epilog_t *ctx, scc_mir_module_t *module) {
|
||||
}
|
||||
|
||||
void scc_mir_pass(scc_mir_module_t *mir_module, scc_mir_pass_stage_t stage) {
|
||||
LOG_INFO("running MIR passes (stage=%d, %zu funcs)", (int)stage,
|
||||
scc_vec_size(mir_module->cfg_module.funcs));
|
||||
|
||||
LOG_DEBUG(" pass 1/3: register allocation");
|
||||
scc_reg_alloc_ctx_t reg_alloc_ctx = {.func = nullptr, .ops = {0}};
|
||||
scc_reg_alloc_fill_arch_x86(®_alloc_ctx.ops);
|
||||
scc_win_pc_x64_reg_alloc_fill(®_alloc_ctx.ops);
|
||||
|
||||
scc_reg_alloc(®_alloc_ctx, mir_module);
|
||||
if (stage == SCC_MIR_STAGE_REGALLOC) {
|
||||
LOG_DEBUG(" stop after regalloc");
|
||||
return;
|
||||
}
|
||||
|
||||
void scc_x86_peephole_optimize(scc_mir_module_t * module);
|
||||
// scc_x86_peephole_optimize(mir_module);
|
||||
|
||||
LOG_DEBUG(" pass 2/3: frame layout");
|
||||
scc_frame_layout_t frame_layout_ctx = {0};
|
||||
scc_win_pc_x64_frame_layout_init(&frame_layout_ctx);
|
||||
scc_frame_layout(&frame_layout_ctx, mir_module);
|
||||
if (stage == SCC_MIR_STAGE_FRAME_LAYOUT) {
|
||||
LOG_DEBUG(" stop after frame layout");
|
||||
return;
|
||||
}
|
||||
|
||||
LOG_DEBUG(" pass 3/3: prolog/epilog");
|
||||
scc_prolog_epilog_t prolog_epilog_ctx = {0};
|
||||
scc_win_pc_x64_prolog_epilog_init(&prolog_epilog_ctx);
|
||||
scc_prolog_epilog(&prolog_epilog_ctx, mir_module);
|
||||
if (stage == SCC_MIR_STAGE_PROLOGUE_EPILOGUE) {
|
||||
LOG_DEBUG(" stop after prolog/epilog");
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user