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:
@@ -43,14 +43,14 @@ static inline void scc_mcode_drop(scc_mcode_t *mcode) {
|
||||
* 避免拷贝大块代码数据。
|
||||
*/
|
||||
static inline void scc_mcode_adopt_buf(scc_mcode_t *mcode,
|
||||
scc_mcode_buff_t *buf) {
|
||||
scc_mcode_buff_t *buf) {
|
||||
scc_vec_free(mcode->code);
|
||||
mcode->code.size = buf->size;
|
||||
mcode->code.cap = buf->cap;
|
||||
mcode->code.data = buf->data;
|
||||
buf->size = 0;
|
||||
buf->cap = 0;
|
||||
buf->data = NULL;
|
||||
buf->data = nullptr;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -61,14 +61,14 @@ static inline void scc_mcode_adopt_buf(scc_mcode_t *mcode,
|
||||
* 避免拷贝大块代码数据。
|
||||
*/
|
||||
static inline void scc_mcode_disown_buf(scc_mcode_t *mcode,
|
||||
scc_mcode_buff_t *out) {
|
||||
scc_mcode_buff_t *out) {
|
||||
scc_vec_free(*out);
|
||||
out->size = mcode->code.size;
|
||||
out->cap = mcode->code.cap;
|
||||
out->data = mcode->code.data;
|
||||
mcode->code.size = 0;
|
||||
mcode->code.cap = 0;
|
||||
mcode->code.data = NULL;
|
||||
mcode->code.data = nullptr;
|
||||
}
|
||||
|
||||
static inline void scc_mcode_add_u8(scc_mcode_t *mcode, u8 data) {
|
||||
|
||||
26
libs/mcode/include/scc_mcode_log.h
Normal file
26
libs/mcode/include/scc_mcode_log.h
Normal file
@@ -0,0 +1,26 @@
|
||||
#ifndef __SCC_MCODE_LOG_H__
|
||||
#define __SCC_MCODE_LOG_H__
|
||||
|
||||
typedef struct logger logger_t;
|
||||
extern logger_t __scc_mcode_log;
|
||||
extern logger_t __scc_mcode_user;
|
||||
|
||||
#define SCC_LOG_HANDLER &__scc_mcode_user
|
||||
#define LOG_DEFAULT_HANDLER &__scc_mcode_log
|
||||
#include <scc_log.h>
|
||||
|
||||
#ifdef __SCC_MCODE_LOG_IMPL__
|
||||
logger_t __scc_mcode_log = {
|
||||
.name = "mcode",
|
||||
.level = LOG_LEVEL_ALL,
|
||||
.handler = log_default_handler,
|
||||
};
|
||||
|
||||
logger_t __scc_mcode_user = {
|
||||
.name = "mcode",
|
||||
.level = LOG_LEVEL_ALL,
|
||||
.user_handler = scc_log_handler,
|
||||
};
|
||||
#endif
|
||||
|
||||
#endif
|
||||
2
libs/mcode/src/scc_mcode.c
Normal file
2
libs/mcode/src/scc_mcode.c
Normal file
@@ -0,0 +1,2 @@
|
||||
#define __SCC_MCODE_LOG_IMPL__
|
||||
#include <scc_mcode_log.h>
|
||||
@@ -1,14 +1,9 @@
|
||||
#include <scc_mcode_log.h>
|
||||
|
||||
#include <x86/scc_x86_encode.h>
|
||||
#include <x86/scc_x86_iform.h>
|
||||
#include <x86/scc_x86_reg.h>
|
||||
|
||||
#if 1
|
||||
#ifdef LOG_INFO
|
||||
#undef LOG_INFO
|
||||
#endif
|
||||
#define LOG_INFO(...)
|
||||
#endif
|
||||
|
||||
/* ---------- 内部辅助 ---------- */
|
||||
static inline void emit_u8(scc_mcode_t *m, uint8_t v) {
|
||||
scc_mcode_add_u8(m, v);
|
||||
@@ -167,7 +162,7 @@ static void emit_rex(scc_mcode_t *m, const scc_x86_encoding_t *enc,
|
||||
if (b != SCC_X86_REG_INVALID && reg_rex_bit(b))
|
||||
rex |= 1;
|
||||
if (rex != 0x40) {
|
||||
LOG_INFO("[REX] emit 0x%02x", (uint8_t)rex);
|
||||
LOG_TRACE("[REX] emit 0x%02x", (uint8_t)rex);
|
||||
emit_u8(m, (uint8_t)rex);
|
||||
}
|
||||
}
|
||||
@@ -249,7 +244,7 @@ static void emit_immediate(scc_mcode_t *m, const scc_x86_encoding_t *enc,
|
||||
else if (!scc_strcmp(oc2, "q"))
|
||||
imm_size = 8;
|
||||
|
||||
LOG_INFO("[IMM] val=%lld size=%d", imm_val, imm_size);
|
||||
LOG_TRACE("[IMM] val=%lld size=%d", imm_val, imm_size);
|
||||
switch (imm_size) {
|
||||
case 1:
|
||||
emit_u8(m, (uint8_t)imm_val);
|
||||
@@ -287,8 +282,8 @@ static void emit_modrm_sib_disp(scc_mcode_t *m,
|
||||
for (int i = 0; i < num_ops; i++) {
|
||||
const char *tname = tmpl[i].name;
|
||||
if (ops[i].kind == SCC_X86_OPR_REG) {
|
||||
LOG_INFO("[OPD] %d: REG kind, name=\"%s\" reg=%d", i, tname,
|
||||
ops[i].reg);
|
||||
LOG_TRACE("[OPD] %d: REG kind, name=\"%s\" reg=%d", i, tname,
|
||||
ops[i].reg);
|
||||
if (scc_strcmp(tname, "REG0") == 0)
|
||||
reg_r = ops[i].reg;
|
||||
else if (scc_strcmp(tname, "REG1") == 0)
|
||||
@@ -307,8 +302,8 @@ static void emit_modrm_sib_disp(scc_mcode_t *m,
|
||||
imm_idx = i;
|
||||
}
|
||||
}
|
||||
LOG_INFO("[MODRM] reg_r=%d (ord=%d) reg_b=%d (ord=%d)", reg_r,
|
||||
scc_reg_ordinal(reg_r), reg_b, scc_reg_ordinal(reg_b));
|
||||
LOG_TRACE("[MODRM] reg_r=%d (ord=%d) reg_b=%d (ord=%d)", reg_r,
|
||||
scc_reg_ordinal(reg_r), reg_b, scc_reg_ordinal(reg_b));
|
||||
|
||||
uint8_t modrm = 0;
|
||||
|
||||
@@ -426,8 +421,8 @@ static void emit_modrm_sib_disp(scc_mcode_t *m,
|
||||
reg_b == SCC_X86_REG_INVALID) {
|
||||
modrm = 0xC0 | (reg_low3(reg_r) & 7);
|
||||
emit_u8(m, modrm);
|
||||
LOG_INFO("[MODRM] single reg operand treated as rm, emit 0x%02x",
|
||||
modrm);
|
||||
LOG_TRACE("[MODRM] single reg operand treated as rm, emit 0x%02x",
|
||||
modrm);
|
||||
} else {
|
||||
modrm = 0xC0;
|
||||
if (enc->modrm_reg_fix >= 0)
|
||||
@@ -449,7 +444,7 @@ static void emit_modrm_sib_disp(scc_mcode_t *m,
|
||||
}
|
||||
}
|
||||
|
||||
LOG_INFO("[MODRM] emit byte 0x%02x", modrm);
|
||||
LOG_TRACE("[MODRM] emit byte 0x%02x", modrm);
|
||||
|
||||
// 立即数在 ModRM 后发射
|
||||
if (imm_idx >= 0) {
|
||||
@@ -469,7 +464,7 @@ int scc_x86_encode_inst(scc_mcode_t *mcode, scc_x86_iform_t iform,
|
||||
int num_ops = info->num_explicit_ops;
|
||||
const scc_x86_encoding_t *enc = &info->encode;
|
||||
const scc_x86_operand_t *tmpl = info->ops;
|
||||
LOG_INFO("[ENCODE] %s", info->iform_name);
|
||||
LOG_TRACE("[ENCODE] %s", info->iform_name);
|
||||
|
||||
scc_x86_reg_t reg_field = SCC_X86_REG_INVALID;
|
||||
scc_x86_reg_t rm_field = SCC_X86_REG_INVALID;
|
||||
@@ -521,7 +516,7 @@ int scc_x86_encode_inst(scc_mcode_t *mcode, scc_x86_iform_t iform,
|
||||
}
|
||||
|
||||
int op_width = infer_operand_width(info, ops);
|
||||
LOG_INFO("[OPWIDTH] %d bits", op_width);
|
||||
LOG_TRACE("[OPWIDTH] %d bits", op_width);
|
||||
|
||||
if (!enc->has_modrm) {
|
||||
rm_field = reg_field;
|
||||
|
||||
Reference in New Issue
Block a user