feat(ir2mcode): 添加IR到机器码转换模块并更新依赖配置
- 新增ir2mcode库用于将IR转换为机器码 - 添加sccf2target依赖以支持目标平台转换 - 在ast库中添加scc_pos依赖支持位置信息 - 更新cbuild.toml配置文件添加新库依赖 - 实现AMD64架构代码生成功能 - 添加寄存器分配器实现栈和寄存器位置管理 - 支持基本的算术运算和内存访问操作 - 添加PE格式目标文件生成支持
This commit is contained in:
59
libs/ir2mcode/tests/test_run.c
Normal file
59
libs/ir2mcode/tests/test_run.c
Normal file
@@ -0,0 +1,59 @@
|
||||
#include <scc_ast2ir.h>
|
||||
#include <scc_ir2mcode.h>
|
||||
#include <scc_lexer.h>
|
||||
#include <scc_parser.h>
|
||||
#include <sccf2pe.h>
|
||||
|
||||
#include <stdio.h>
|
||||
|
||||
void test_example(const char *input, cbool need_sema) {
|
||||
int res = 0;
|
||||
scc_sstream_t mem_stream;
|
||||
res = scc_sstream_init_by_buffer(&mem_stream, input, scc_strlen(input),
|
||||
false, 16);
|
||||
Assert(res == 0);
|
||||
|
||||
scc_lexer_t lexer;
|
||||
scc_lexer_init(&lexer, scc_sstream_to_ring(&mem_stream));
|
||||
|
||||
scc_lexer_tok_ring_t *tok_ring = scc_lexer_to_ring(&lexer, 64, false);
|
||||
|
||||
scc_parser_t parser;
|
||||
if (need_sema) {
|
||||
scc_sema_callbacks_t sema_callbacks;
|
||||
scc_sema_init(&sema_callbacks);
|
||||
scc_parser_init(&parser, tok_ring, &sema_callbacks);
|
||||
} else {
|
||||
scc_parser_init(&parser, tok_ring, null);
|
||||
}
|
||||
|
||||
scc_ast_translation_unit_t *tu = scc_parse_translation_unit(&parser);
|
||||
|
||||
scc_ast2ir_ctx_t ast2ir_ctx;
|
||||
#include <abi/win_x64_type_abi.h>
|
||||
scc_ast2ir_ctx_init(&ast2ir_ctx, scc_win_x64_type_abi);
|
||||
scc_ast2ir_translation_unit(&ast2ir_ctx, tu);
|
||||
|
||||
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);
|
||||
scc_pe_dump_to_file(&pe_builder, __FILE__ "/../../test.exe");
|
||||
}
|
||||
|
||||
int main() {
|
||||
test_example("int main() {\n"
|
||||
" int a;\n"
|
||||
" int b;\n"
|
||||
" a = 1 + 2 * 3;\n"
|
||||
" b = 7;\n"
|
||||
" a = a - b + 1;\n"
|
||||
" return a;\n"
|
||||
"}\n",
|
||||
true);
|
||||
return 0;
|
||||
}
|
||||
Reference in New Issue
Block a user