- 将 MIR 中的 SCC_MIR_OP_MEM 替换为更精确的 SCC_MIR_OP_STACK_SLOT 和 SCC_MIR_OP_STACK_OFFSET 类型 - 在 x86_64 指令选择中更新相应的内存操作数处理逻辑 - 修改寄存器分配器中的栈槽操作数类型检查 - 更新 IR 转机器码过程中的内存操作数转换 refactor(hir): 使用 tree_dump_node 输出函数节点 - 将 hir_dump 中的函数名输出从 append 改为 node 类型 refactor(frame-layout): 重构栈帧布局传递实现结构 - 引入函数指针实现方式替代直接函数实现 - 将栈帧分配功能集成到 MIR 传递流程中 - 移除独立的 frame_layout 实现文件 refactor(prolog-epilog): 添加函数序言/尾声传递框架 - 为 Windows x64 平台初始化序言/尾声生成器 - 在 MIR 传递阶段添加序言/尾声处理步骤 refactor(win64): 更新 Windows x64 目标平台接口 - 重命名寄存器分配填充函数为 scc_win_pc_x64_reg_alloc_fill - 添加栈帧分配和序言/尾声初始化函数 - 修正参数传递中的栈槽操作数类型 refactor(dump): 改进 MIR 输出格式 - 将基本块显示改为分支节点类型 - 更新操作数类型的显示处理 chore: 添加 x86 编码相关数据结构定义 - 新增 scc_x86_encode.h 头文件包含内存操作数和指令编码接口定义
358 lines
11 KiB
C
358 lines
11 KiB
C
#include <argparse.h>
|
|
#include <scc_lexer.h>
|
|
#include <scc_parser.h>
|
|
#include <scc_pproc.h>
|
|
|
|
#include <scc_ast2ir.h>
|
|
#include <scc_ast_dump.h>
|
|
#include <scc_hir2lir.h>
|
|
#include <scc_hir_dump.h>
|
|
#include <scc_lir2mir.h>
|
|
#include <scc_lir_dump.h>
|
|
#include <scc_mir_dump.h>
|
|
|
|
#include <scc_mir_pass.h>
|
|
|
|
#include <scc_ir2mcode.h>
|
|
#include <scc_ir2sccf.h>
|
|
#include <sccf2pe.h>
|
|
|
|
#include "config.h"
|
|
void init_platform(void);
|
|
#define GET_VALID_FP(fp) (fp == nullptr ? scc_stdout : fp)
|
|
|
|
static void print_ring(scc_lexer_tok_ring_t *ring, int verbose) {
|
|
scc_lexer_tok_t tok = {0};
|
|
int ret = 0;
|
|
while (1) {
|
|
scc_ring_next_consume(*ring, tok, ret);
|
|
if (ret == false || tok.type == SCC_TOK_EOF) {
|
|
break;
|
|
}
|
|
if (verbose == 0) {
|
|
scc_printf("%s ", scc_get_tok_name(tok.type));
|
|
} else if (verbose >= 1) {
|
|
scc_printf(
|
|
"token [%-8s] `%s` at %s:%d:%d\n", scc_get_tok_name(tok.type),
|
|
tok.type != SCC_TOK_ENDLINE ? scc_str_as_cstr(&tok.lexeme)
|
|
: "\\n",
|
|
tok.loc.name, tok.loc.line, tok.loc.col);
|
|
}
|
|
scc_lexer_tok_drop(&tok);
|
|
}
|
|
}
|
|
|
|
static void print_file(scc_lexer_tok_ring_t *ring, scc_file_t fp) {
|
|
scc_lexer_tok_t tok = {0};
|
|
int ret = 0;
|
|
|
|
while (1) {
|
|
scc_ring_next_consume(*ring, tok, ret);
|
|
if (ret == false || tok.type == SCC_TOK_EOF) {
|
|
break;
|
|
}
|
|
if (fp == scc_stdout) {
|
|
scc_printf("%s", scc_str_as_cstr(&tok.lexeme));
|
|
} else {
|
|
usize ret = scc_fwrite(fp, scc_str_as_cstr(&tok.lexeme),
|
|
scc_str_len(&tok.lexeme));
|
|
if (ret != scc_str_len(&tok.lexeme)) {
|
|
LOG_FATAL("Failed to write to file");
|
|
}
|
|
}
|
|
scc_lexer_tok_drop(&tok);
|
|
}
|
|
scc_fclose(fp);
|
|
}
|
|
|
|
static void tree_dump_output(const char *str, usize len, void *user) {
|
|
scc_fprintf(user, "%.*s", (int)len, str);
|
|
}
|
|
static inline int mir_dump(scc_file_t fp, scc_mir_module_t *mir_module) {
|
|
scc_tree_dump_t tree_dump;
|
|
scc_mir_dump_ctx_t mir_dump_ctx;
|
|
if (fp == nullptr) {
|
|
scc_tree_dump_init(&tree_dump, true);
|
|
} else {
|
|
scc_tree_dump_init(&tree_dump, false);
|
|
}
|
|
scc_mir_dump_init(&mir_dump_ctx, &tree_dump, mir_module);
|
|
scc_mir_dump_module(&mir_dump_ctx);
|
|
|
|
scc_tree_dump_flush(&tree_dump, tree_dump_output, GET_VALID_FP(fp));
|
|
scc_tree_dump_drop(&tree_dump);
|
|
return 0;
|
|
}
|
|
|
|
int main(int argc, const char **argv, const char **envp) {
|
|
init_platform();
|
|
|
|
#ifndef SCC_DEFAULT_ARGPARSE_LANG
|
|
#define SCC_DEFAULT_ARGPARSE_LANG SCC_ARGPARSE_LANG_ZH
|
|
#endif
|
|
|
|
scc_argparse_lang_t argparse_lang = SCC_DEFAULT_ARGPARSE_LANG;
|
|
for (const char **env = envp; *env != nullptr; env++) {
|
|
const char *env_str = *env;
|
|
if (scc_strcmp(env_str, "LANG=zh_CN.UTF-8") == 0) {
|
|
argparse_lang = SCC_ARGPARSE_LANG_ZH;
|
|
}
|
|
}
|
|
|
|
scc_config_t config = {
|
|
.input_file = nullptr,
|
|
.verbose = 0,
|
|
.output_file = nullptr,
|
|
.entry_point_symbol = nullptr,
|
|
.emit_stage = SCC_EMIT_STAGE_DEFAULT,
|
|
.target_description = "x86_64-pc-windows-msvc",
|
|
};
|
|
scc_vec_init(config.include_paths);
|
|
|
|
scc_argparse_t argparse;
|
|
setup_argparse(&argparse, &config, argparse_lang);
|
|
int ret = scc_argparse_parse(&argparse, argc, argv);
|
|
if (ret != 0) {
|
|
scc_argparse_drop(&argparse);
|
|
return 0;
|
|
}
|
|
scc_argparse_drop(&argparse);
|
|
|
|
scc_file_t fp = nullptr;
|
|
if (config.output_file) {
|
|
cbool is_stdout = scc_strcmp(config.output_file, "-") == 0;
|
|
if (!is_stdout) {
|
|
fp = scc_fopen(config.output_file, SCC_FILE_WRITE);
|
|
if (fp == nullptr) {
|
|
LOG_FATAL("Failed to open file %s", config.output_file);
|
|
return 1;
|
|
}
|
|
} else {
|
|
fp = scc_stdout;
|
|
}
|
|
}
|
|
|
|
int error_code = 0;
|
|
cbool need_end = false;
|
|
scc_sstream_t sstream;
|
|
error_code = scc_sstream_init(&sstream, config.input_file, 1024);
|
|
if (error_code) {
|
|
goto sstream_drop;
|
|
need_end = true;
|
|
}
|
|
|
|
scc_lexer_t lexer;
|
|
scc_lexer_init(&lexer, scc_sstream_to_ring(&sstream));
|
|
if (config.emit_stage == SCC_EMIT_STAGE_LEX) {
|
|
scc_lexer_tok_ring_t *tok_ring =
|
|
scc_lexer_to_ring(&lexer, 8, fp == nullptr ? false : true);
|
|
if (fp == nullptr) {
|
|
print_ring(tok_ring, config.verbose);
|
|
} else {
|
|
print_file(tok_ring, fp);
|
|
}
|
|
need_end = true;
|
|
goto lexer_drop;
|
|
}
|
|
|
|
scc_pproc_t pproc;
|
|
scc_pproc_init(&pproc, scc_lexer_to_ring(&lexer, 8, true));
|
|
// FIXME maybe using config to got download path and add include path
|
|
scc_pproc_add_include_path_cstr(&pproc, "./.scc_include");
|
|
scc_vec_foreach(config.include_paths, i) {
|
|
scc_pproc_add_include_path_cstr(&pproc,
|
|
scc_vec_at(config.include_paths, i));
|
|
}
|
|
scc_lexer_tok_vec_t pproc_tok_vec;
|
|
scc_vec_init(pproc_tok_vec);
|
|
scc_lexer_tok_t tok = {
|
|
.lexeme = scc_str_from_cstr("1"),
|
|
.type = SCC_TOK_INT_LITERAL,
|
|
.loc.name = "<internal>",
|
|
.loc.line = 0,
|
|
.loc.col = 0,
|
|
.loc.offset = 0,
|
|
};
|
|
scc_vec_push(pproc_tok_vec, tok);
|
|
scc_str_t pproc_predefined_macros[] = {
|
|
scc_str_from_cstr("__SCC__"),
|
|
scc_str_from_cstr("_WIN64"),
|
|
scc_str_from_cstr("_WIN32"),
|
|
scc_str_from_cstr("__x86_64__"),
|
|
};
|
|
for (usize i = 0; i < SCC_ARRLEN(pproc_predefined_macros); i += 1) {
|
|
scc_vec_init(pproc_tok_vec);
|
|
scc_lexer_tok_t coped_tok = scc_lexer_tok_copy(&tok);
|
|
scc_vec_push(pproc_tok_vec, coped_tok);
|
|
scc_pproc_add_object_macro(&pproc.macro_table,
|
|
&pproc_predefined_macros[i], &pproc_tok_vec);
|
|
}
|
|
if (config.emit_stage == SCC_EMIT_STAGE_PP) {
|
|
scc_lexer_tok_ring_t *tok_ring =
|
|
scc_pproc_to_ring(&pproc, 8, true, true);
|
|
if (fp == nullptr) {
|
|
print_ring(tok_ring, config.verbose);
|
|
} else {
|
|
print_file(tok_ring, fp);
|
|
}
|
|
need_end = true;
|
|
goto pproc_drop;
|
|
}
|
|
|
|
scc_lexer_tok_ring_t *tok_ring =
|
|
scc_pproc_to_ring(&pproc, 1024, false, false);
|
|
scc_parser_t parser;
|
|
scc_ast_ctx_t ast_ctx;
|
|
scc_ast_ctx_init(&ast_ctx);
|
|
scc_sema_ctx_t sema_ctx;
|
|
scc_sema_init(&sema_ctx, &ast_ctx);
|
|
scc_parser_init(&parser, tok_ring, &ast_ctx, &sema_ctx);
|
|
scc_ast_translation_unit_t *translation_unit =
|
|
scc_parse_translation_unit(&parser);
|
|
if (parser.errcode != 0) {
|
|
return parser.errcode;
|
|
}
|
|
|
|
scc_sema_drop(&sema_ctx);
|
|
scc_parser_drop(&parser);
|
|
pproc_drop:
|
|
scc_pproc_drop(&pproc);
|
|
lexer_drop:
|
|
scc_lexer_drop(&lexer);
|
|
sstream_drop:
|
|
scc_sstream_drop(&sstream);
|
|
if (error_code || need_end) {
|
|
return error_code;
|
|
}
|
|
|
|
if (config.emit_stage == SCC_EMIT_STAGE_AST) {
|
|
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_ast_dump_node(&tree_dump, (scc_ast_node_t *)translation_unit);
|
|
scc_tree_dump_flush(&tree_dump, tree_dump_output, GET_VALID_FP(fp));
|
|
scc_tree_dump_drop(&tree_dump);
|
|
return 0;
|
|
}
|
|
|
|
scc_ast2ir_ctx_t ast2ir_ctx;
|
|
#include <target/scc_abi_win_x64_pc.h>
|
|
scc_hir_cprog_t cprog;
|
|
scc_hir_cprog_init(&cprog);
|
|
scc_ast2ir_ctx_init(&ast2ir_ctx, &scc_ast_abi_impl, &ast_ctx, &cprog);
|
|
scc_ast2ir_run(&ast2ir_ctx, translation_unit);
|
|
scc_ast2ir_ctx_drop(&ast2ir_ctx);
|
|
|
|
if (config.emit_stage == SCC_EMIT_STAGE_HIR) {
|
|
scc_hir_dump_t ir_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_hir_dump_init(&ir_dump_ctx, &tree_dump, &cprog);
|
|
// scc_ir_dump_cprog(&ir_dump_ctx);
|
|
scc_hir_dump_cprog_linear(&ir_dump_ctx);
|
|
|
|
scc_tree_dump_flush(&tree_dump, tree_dump_output, GET_VALID_FP(fp));
|
|
scc_tree_dump_drop(&tree_dump);
|
|
return 0;
|
|
}
|
|
|
|
scc_lir_module_t lir_module;
|
|
scc_lir_module_init(&lir_module);
|
|
scc_hir2lir(&lir_module, &cprog);
|
|
if (config.emit_stage == SCC_EMIT_STAGE_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, GET_VALID_FP(fp));
|
|
scc_tree_dump_drop(&tree_dump);
|
|
return 0;
|
|
}
|
|
|
|
scc_mir_module_t mir_module;
|
|
scc_mir_module_init(&mir_module);
|
|
scc_lir2mir(&mir_module, &lir_module);
|
|
|
|
switch (config.emit_stage) {
|
|
case SCC_EMIT_STAGE_MIR:
|
|
return mir_dump(fp, &mir_module);
|
|
case SCC_EMIT_STAGE_MIR_PASS_REG_ALLOC:
|
|
scc_mir_pass(&mir_module, SCC_MIR_STAGE_REGALLOC);
|
|
return mir_dump(fp, &mir_module);
|
|
case SCC_EMIT_STAGE_MIR_PASS_FLAME_LAYOUT:
|
|
scc_mir_pass(&mir_module, SCC_MIR_STAGE_FRAME_LAYOUT);
|
|
return mir_dump(fp, &mir_module);
|
|
case SCC_EMIT_STAGE_MIR_PASS_PROLOG_EPILOG:
|
|
scc_mir_pass(&mir_module, SCC_MIR_STAGE_PROLOGUE_EPILOGUE);
|
|
return mir_dump(fp, &mir_module);
|
|
default:
|
|
scc_mir_pass(&mir_module, SCC_MIR_STAGE_ANY);
|
|
break;
|
|
}
|
|
|
|
if (config.emit_stage == SCC_EMIT_STAGE_FLATBIN) {
|
|
scc_mcode_t mcode = {0};
|
|
scc_mcode_init(&mcode, SCC_MCODE_ARCH_X86_64);
|
|
scc_ir2mcode(&mcode, &mir_module);
|
|
if (fp == nullptr) {
|
|
LOG_WARN("emit flatbin can't write to stdout");
|
|
return 0;
|
|
}
|
|
usize ret = scc_fwrite(fp, scc_vec_unsafe_get_data(mcode.mcode),
|
|
scc_vec_size(mcode.mcode));
|
|
if (ret != scc_vec_size(mcode.mcode)) {
|
|
LOG_ERROR("write flatbin failed, write %zu but need write %zu\n",
|
|
ret, scc_vec_size(mcode.mcode));
|
|
return 1;
|
|
}
|
|
return 0;
|
|
}
|
|
|
|
sccf_builder_t sccf_builder = {0};
|
|
sccf_builder_init(&sccf_builder);
|
|
scc_ir2sccf(&sccf_builder, &mir_module);
|
|
sccf_builder_set_entry_symbol_name(&sccf_builder,
|
|
config.entry_point_symbol);
|
|
const sccf_t *sccf = sccf_builder_to_sccf(&sccf_builder);
|
|
if (config.emit_stage == SCC_EMIT_STAGE_SCCF) {
|
|
sccf_buffer_t buffer;
|
|
scc_vec_init(buffer);
|
|
sccf_builder_to_buffer(&sccf_builder, &buffer);
|
|
if (fp == nullptr) {
|
|
scc_printf("output exe at %s\n", config.output_file);
|
|
} else {
|
|
sccf_builder_to_file(&sccf_builder, config.output_file);
|
|
}
|
|
return 0;
|
|
}
|
|
|
|
if (config.emit_stage == SCC_EMIT_STAGE_DEFAULT ||
|
|
config.emit_stage == SCC_EMIT_STAGE_TARGET) {
|
|
scc_pe_builder_t pe_builder;
|
|
sccf2pe(&pe_builder, sccf);
|
|
if (fp == nullptr) {
|
|
scc_printf("output exe at %s\n", config.output_file);
|
|
} else {
|
|
scc_pe_dump_to_file(&pe_builder, config.output_file);
|
|
}
|
|
return 0;
|
|
}
|
|
|
|
Panic("unknown emit stage");
|
|
return 1;
|
|
}
|