- 将IR构建器初始化函数修改为接受cprog参数 - 添加scc_ast2ir_ctx_drop函数用于资源清理 - 更新类型标识符命名规范,从大写改为小写形式 - 替换scc_ir_ctx_get_*函数调用为scc_ir_module_get_*函数 - 移除对ir_builtin.h的依赖,改用ir_builder.h中的构建器函数 - 为整数常量创建添加专门的构建器辅助函数 fix(ir): 重构IR上下文和模块管理结构 - 将原有的scc_ir_cprog_ctx_t拆分为scc_ir_module_t和scc_ir_ctx_t - 添加scc_ir_module_t结构用于统一管理IR对象存储 - 更新IR类型枚举名称格式,从SCC_IR_TYPE_XXX改为SCC_IR_TYPE_xxx - 添加整数、无符号整数和浮点数常量联合体定义 - 移除ir_base.h和ir_builtin.h头文件,整合到scc_ir.h中 feat(ir_builder): 添加类型构建器函数和常量创建功能 - 为各种基础类型添加scc_ir_builder_type_*内联函数 - 实现scc_ir_builder_const_int函数用于创建整数常量 - 修改构建器初始化函数签名以接受cprog参数 - 更新构建器内部结构,使用指向cprog的指针而非嵌入式结构
111 lines
3.5 KiB
C
111 lines
3.5 KiB
C
#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, const char *name) {
|
|
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_ir_cprog_t cprog;
|
|
scc_ir_cprog_init(&cprog);
|
|
scc_ast2ir_ctx_init(&ast2ir_ctx, scc_win_x64_type_abi, &cprog);
|
|
scc_ast2ir_translation_unit(&ast2ir_ctx, tu);
|
|
scc_ast2ir_ctx_drop(&ast2ir_ctx);
|
|
|
|
scc_ir2mcode_ctx_t ir2mcode_ctx;
|
|
sccf_builder_t sccf_builder;
|
|
scc_ir2mcode_init(&ir2mcode_ctx, &cprog, &sccf_builder,
|
|
SCC_MCODE_ARCH_AMD64);
|
|
scc_ir2mcode(&ir2mcode_ctx);
|
|
scc_ir2mcode_drop(&ir2mcode_ctx);
|
|
|
|
const sccf_t *sccf = sccf_builder_to_sccf(&sccf_builder);
|
|
scc_pe_builder_t pe_builder;
|
|
sccf2pe(&pe_builder, sccf);
|
|
|
|
char fname[1024] = {0};
|
|
scc_snprintf(fname, sizeof(fname), "%s%s%s", __FILE__, "/../../", name);
|
|
scc_pe_dump_to_file(&pe_builder, fname);
|
|
}
|
|
|
|
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, "02_decl_expr.exe");
|
|
test_example("int main(void) {\n"
|
|
" int a;\n"
|
|
" a = 1;\n"
|
|
" if (a) {\n"
|
|
" a = 1;\n"
|
|
" } else {\n"
|
|
" a = 2;\n"
|
|
" }\n"
|
|
" return a;\n"
|
|
"}\n",
|
|
true, "04_if.exe");
|
|
test_example("int main() {\n"
|
|
" int i = 0;\n"
|
|
" while (i < 10) i = i + 1;\n"
|
|
" return i;\n"
|
|
"}\n",
|
|
true, "07_while.exe");
|
|
test_example("int add(int a, int b) {\n"
|
|
" return a + b;\n"
|
|
"}\n"
|
|
"\n"
|
|
"int main(void) {\n"
|
|
" return add(1, 2);\n"
|
|
"}\n",
|
|
true, "10_main.exe");
|
|
test_example("int factorial(int);\n"
|
|
"\n"
|
|
"int main() {\n"
|
|
" int num = 5;\n"
|
|
" int result = factorial(num);\n"
|
|
" // printf(\"%d\", result);\n"
|
|
" return result;\n"
|
|
"}\n"
|
|
"\n"
|
|
"int factorial(int num) {\n"
|
|
" if (num == 0) {\n"
|
|
" return 1;\n"
|
|
" } else {\n"
|
|
" return num * factorial(num - 1);\n"
|
|
" }\n"
|
|
"}\n",
|
|
true, "11_recursion.exe");
|
|
|
|
return 0;
|
|
}
|