feat(ir2mcode): 添加IR到机器码转换模块并更新依赖配置

- 新增ir2mcode库用于将IR转换为机器码
- 添加sccf2target依赖以支持目标平台转换
- 在ast库中添加scc_pos依赖支持位置信息
- 更新cbuild.toml配置文件添加新库依赖
- 实现AMD64架构代码生成功能
- 添加寄存器分配器实现栈和寄存器位置管理
- 支持基本的算术运算和内存访问操作
- 添加PE格式目标文件生成支持
This commit is contained in:
zzy
2026-03-20 14:12:25 +08:00
parent 02a6c684f1
commit de6f5d510a
19 changed files with 4046 additions and 290 deletions

11
src/init.c Normal file
View File

@@ -0,0 +1,11 @@
#ifdef _WIN32
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
#endif
void init_platform(void) {
#ifdef _WIN32
SetConsoleOutputCP(CP_UTF8);
SetConsoleCP(CP_UTF8);
#endif
}

View File

@@ -1,19 +1,16 @@
#include <argparse.h>
#include <scc_lexer.h>
#include <scc_parser.h>
#include <scc_pproc.h>
#include <ast_dump.h>
#include <ir_dump.h>
#include <scc_ast2ir.h>
#include <scc_parser.h>
#include <scc_ir2mcode.h>
#include <sccf2pe.h>
#include "config.h"
#ifdef _WIN32
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
#endif
static void print_ring(scc_lexer_tok_ring_t *ring, int verbose) {
scc_lexer_tok_t tok = {0};
int ret = 0;
@@ -58,11 +55,10 @@ static void print_file(scc_lexer_tok_ring_t *ring, scc_file_t fp) {
scc_fclose(fp);
}
void init_platform(void);
int main(int argc, const char **argv, const char **envp) {
#ifdef _WIN32
SetConsoleOutputCP(CP_UTF8);
SetConsoleCP(CP_UTF8);
#endif
init_platform();
#ifndef SCC_DEFAULT_ARGPARSE_LANG
#define SCC_DEFAULT_ARGPARSE_LANG SCC_ARGPARSE_LANG_ZH
@@ -233,10 +229,19 @@ sstream_drop:
return 0;
}
scc_ir2mcode_ctx_t mcode_ctx;
scc_ir2mcode_init(&mcode_ctx, &ast2ir_ctx.builder.cprog,
&ast2ir_ctx.builder.ctx, SCC_MCODE_ARCH_AMD64);
scc_ir2mcode(&mcode_ctx);
const sccf_t *sccf = sccf_builder_to_sccf(&mcode_ctx.builder);
scc_pe_builder_t pe_builder;
sccf2pe(&pe_builder, sccf);
if (fp == null) {
scc_printf("output exe at %s\n", config.output_file);
} else {
scc_printf("output exe at %s\n", config.output_file);
scc_pe_dump_to_file(&pe_builder, config.output_file);
}
return 0;