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/ast2ir/include/scc_ast2ir_log.h
Normal file
26
libs/ast2ir/include/scc_ast2ir_log.h
Normal file
@@ -0,0 +1,26 @@
|
||||
#ifndef __SCC_AST2IR_LOG_H__
|
||||
#define __SCC_AST2IR_LOG_H__
|
||||
|
||||
typedef struct logger logger_t;
|
||||
extern logger_t __scc_ast2ir_log;
|
||||
extern logger_t __scc_ast2ir_user;
|
||||
|
||||
#define SCC_LOG_HANDLER &__scc_ast2ir_user
|
||||
#define LOG_DEFAULT_HANDLER &__scc_ast2ir_log
|
||||
#include <scc_log.h>
|
||||
|
||||
#ifdef __SCC_AST2IR_LOG_IMPL__
|
||||
logger_t __scc_ast2ir_log = {
|
||||
.name = "ast2ir",
|
||||
.level = LOG_LEVEL_ALL,
|
||||
.handler = log_default_handler,
|
||||
};
|
||||
|
||||
logger_t __scc_ast2ir_user = {
|
||||
.name = "ast2ir",
|
||||
.level = LOG_LEVEL_ALL,
|
||||
.user_handler = scc_log_handler,
|
||||
};
|
||||
#endif
|
||||
|
||||
#endif
|
||||
@@ -1,3 +1,6 @@
|
||||
#define __SCC_AST2IR_LOG_IMPL__
|
||||
#include <scc_ast2ir_log.h>
|
||||
|
||||
#include <scc_ast2ir.h>
|
||||
#include <scc_ast_def.h>
|
||||
#include <scc_ast_utils.h>
|
||||
@@ -192,6 +195,7 @@ void scc_ast2ir_stmt(scc_ast2ir_ctx_t *ctx, const scc_ast_stmt_t *stmt) {
|
||||
return;
|
||||
}
|
||||
|
||||
LOG_DEBUG("stmt type=%d", stmt->base.type);
|
||||
switch (stmt->base.type) {
|
||||
case SCC_AST_STMT_COMPOUND: {
|
||||
scc_vec_foreach(stmt->compound.block_items, i) {
|
||||
@@ -542,6 +546,7 @@ void scc_ast2ir_decl(scc_ast2ir_ctx_t *ctx, const scc_ast_decl_t *decl,
|
||||
break;
|
||||
}
|
||||
case SCC_AST_DECL_FUNC: {
|
||||
LOG_DEBUG("function '%s'", decl->name);
|
||||
scc_hir_type_ref_t func_type_ref =
|
||||
scc_ast2ir_type(ctx, decl->func.type);
|
||||
scc_hir_func_ref_t func_ref =
|
||||
@@ -674,6 +679,8 @@ void scc_ast2ir_run(scc_ast2ir_ctx_t *ctx,
|
||||
const scc_ast_translation_unit_t *tu) {
|
||||
Assert(ctx != nullptr && tu != nullptr);
|
||||
|
||||
LOG_INFO("translating %zu declaration(s) to HIR",
|
||||
scc_vec_size(tu->declarations));
|
||||
scc_vec_foreach(tu->declarations, i) {
|
||||
scc_ast_decl_t *decl = scc_vec_at(tu->declarations, i);
|
||||
scc_ast2ir_decl(ctx, decl, true);
|
||||
|
||||
@@ -1,3 +1,5 @@
|
||||
#include <scc_ast2ir_log.h>
|
||||
|
||||
#include <scc_ast2ir.h>
|
||||
#include <scc_ast_def.h>
|
||||
#include <scc_ast_utils.h>
|
||||
@@ -158,9 +160,11 @@ scc_hir_value_ref_t scc_ast2ir_expr(scc_ast2ir_ctx_t *ctx,
|
||||
return 0;
|
||||
}
|
||||
|
||||
LOG_DEBUG("expr type=%d is_lvalue=%d", expr->base.type, (int)is_lvalue);
|
||||
cbool is_assign = true;
|
||||
switch (expr->base.type) {
|
||||
case SCC_AST_EXPR_BINARY: {
|
||||
LOG_DEBUG(" binary op=%d", expr->binary.op);
|
||||
scc_ast_expr_t tmp_expr;
|
||||
scc_hir_value_ref_t lhs = SCC_HIR_REF_nullptr,
|
||||
rhs = SCC_HIR_REF_nullptr;
|
||||
@@ -365,6 +369,7 @@ scc_hir_value_ref_t scc_ast2ir_expr(scc_ast2ir_ctx_t *ctx,
|
||||
return scc_hir_builder_binop(&ctx->builder, op, lhs, rhs);
|
||||
} break;
|
||||
case SCC_AST_EXPR_UNARY: {
|
||||
LOG_DEBUG(" unary op=%d", expr->unary.op);
|
||||
if (expr->unary.op == SCC_AST_OP_ADDRESS_OF) {
|
||||
return scc_ast2ir_expr(ctx, expr->unary.operand, true);
|
||||
} else if (expr->unary.op == SCC_AST_OP_INDIRECTION) {
|
||||
@@ -508,6 +513,7 @@ scc_hir_value_ref_t scc_ast2ir_expr(scc_ast2ir_ctx_t *ctx,
|
||||
UNREACHABLE();
|
||||
} break;
|
||||
case SCC_AST_EXPR_COND: {
|
||||
LOG_DEBUG(" ternary conditional");
|
||||
scc_hir_type_ref_t true_type = SCC_HIR_REF_nullptr;
|
||||
scc_hir_type_ref_t false_type = SCC_HIR_REF_nullptr;
|
||||
|
||||
@@ -559,7 +565,10 @@ scc_hir_value_ref_t scc_ast2ir_expr(scc_ast2ir_ctx_t *ctx,
|
||||
return scc_hir_builder_load(&ctx->builder, result_slot);
|
||||
} break;
|
||||
case SCC_AST_EXPR_CALL: {
|
||||
// 转换参数
|
||||
LOG_DEBUG(" call '%s'",
|
||||
expr->call.callee->identifier._target
|
||||
? expr->call.callee->identifier._target->name
|
||||
: "?");
|
||||
scc_hir_value_ref_vec_t args;
|
||||
scc_vec_init(args);
|
||||
|
||||
@@ -640,7 +649,7 @@ scc_hir_value_ref_t scc_ast2ir_expr(scc_ast2ir_ctx_t *ctx,
|
||||
return node;
|
||||
}
|
||||
case SCC_AST_EXPR_ARRAY_SUBSCRIPT: {
|
||||
// 1. 计算数组/指针基址(右值,得到地址)
|
||||
LOG_DEBUG(" array subscript");
|
||||
scc_hir_value_ref_t base_ptr =
|
||||
scc_ast2ir_expr(ctx, expr->subscript.array, true);
|
||||
// 2. 计算下标值
|
||||
@@ -670,7 +679,7 @@ scc_hir_value_ref_t scc_ast2ir_expr(scc_ast2ir_ctx_t *ctx,
|
||||
}
|
||||
}
|
||||
case SCC_AST_EXPR_MEMBER: {
|
||||
// 1. 获取基对象的左值(地址)
|
||||
LOG_DEBUG(" member access field_idx=%zu", expr->member._target_idx);
|
||||
scc_hir_value_ref_t base_ptr =
|
||||
scc_ast2ir_expr(ctx, expr->member.base, true);
|
||||
// 2. 通过偏移生成字段地址
|
||||
@@ -688,7 +697,7 @@ scc_hir_value_ref_t scc_ast2ir_expr(scc_ast2ir_ctx_t *ctx,
|
||||
return scc_hir_builder_load(&ctx->builder, field_ptr);
|
||||
}
|
||||
case SCC_AST_EXPR_PTR_MEMBER: {
|
||||
// 1. 计算指针值(obj->field 等价于 (*obj).field)
|
||||
LOG_DEBUG(" ptr member access");
|
||||
scc_hir_value_ref_t obj_ptr =
|
||||
scc_ast2ir_expr(ctx, expr->member.base, false);
|
||||
// 2. 解引用得到对象地址,再访问成员
|
||||
|
||||
Reference in New Issue
Block a user