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

View File

@@ -1,69 +0,0 @@
#include <sccf.h>
#include <sccf2pe.h>
#include <sccf_builder.h>
#include <stdio.h>
int main() {
char data[] = "Hello, World from SCC PE Builder!\n\0";
/* clang-format off */
char code[] = {
// sub rsp, 0x28 ; 为函数调用分配栈空间
0x48, 0x83, 0xEC, 0x28,
// lea rcx, [rip + data_offset] ; 将字符串地址加载到RCX第一个参数
0x48, 0x8D, 0x0D, 0x00, 0x00, 0x00, 0x00,
// call qword ptr [rip + puts_iat] ; 通过IAT调用puts
0xFF, 0x15, 0x00, 0x00, 0x00, 0x00,
// add rsp, 0x28 ; 恢复栈空间
0x48, 0x83, 0xC4, 0x28,
// xor eax, eax ; 设置返回值为0
0x33, 0xC0,
// ret ; 返回
0xC3,
};
/* clang-format on */
sccf_builder_t builder;
sccf_builder_init(&builder);
sccf_sect_data_t text_section = {
.data = (u8 *)code, .size = sizeof(code), .cap = sizeof(code)};
sccf_sect_data_t data_section = {
.data = (u8 *)data, .size = sizeof(data), .cap = sizeof(data)};
sccf_builder_add_text_section(&builder, &text_section);
sccf_builder_add_data_section(&builder, &data_section);
usize str_idx =
sccf_builder_add_symbol(&builder, "str_data",
&(sccf_sym_t){
.sccf_sect_offset = 0,
.sccf_sect_type = SCCF_SECT_DATA,
.sccf_sym_bind = SCCF_SYM_BIND_GLOBAL,
.sccf_sym_size = sizeof(data),
.sccf_sym_type = SCCF_SYM_TYPE_DATA,
.sccf_sym_vis = SCCF_SYM_VIS_DEFAULT,
});
usize puts_idx =
sccf_builder_add_symbol(&builder, "puts",
&(sccf_sym_t){
.sccf_sect_offset = 0,
.sccf_sect_type = SCCF_SECT_NONE,
.sccf_sym_bind = SCCF_SYM_BIND_GLOBAL,
.sccf_sym_size = 8,
.sccf_sym_type = SCCF_SYM_TYPE_EXTERN,
.sccf_sym_vis = SCCF_SYM_VIS_DEFAULT,
});
sccf_builder_add_reloc(&builder, (sccf_reloc_t){.addend = 4,
.offset = 7,
.sect_type = SCCF_SECT_CODE,
.sym_idx = str_idx,
.type = SCCF_RELOC_REL});
sccf_builder_add_reloc(&builder, (sccf_reloc_t){.addend = 4,
.offset = 13,
.sect_type = SCCF_SECT_CODE,
.sym_idx = puts_idx,
.type = SCCF_RELOC_REL});
const sccf_t *sccf = sccf_builder_to_sccf(&builder);
scc_pe_builder_t pe_builder;
sccf2pe(&pe_builder, sccf);
scc_pe_dump_to_file(&pe_builder, __FILE__ "/../../test.exe");
}

View File

@@ -64,7 +64,7 @@ static void pe_idata_lib_init(pe_idata_lib_ctx_t *ctx) {
(scc_hashtable_hash_func_t)scc_strhash32,
(scc_hashtable_equal_func_t)scc_strcmp);
scc_vec_init(ctx->idata_libs);
load_from_def(ctx, "./.dll_def", "ucrtbase.dll");
load_from_def(ctx, __FILE__ "/../../.dll_def", "ucrtbase.dll");
}
static cbool pe_idata_get(pe_idata_lib_ctx_t *ctx, const char *name) {
@@ -102,24 +102,22 @@ void sccf2pe(scc_pe_builder_t *builder, const sccf_t *sccf) {
sccf_sym_vec_t symtab;
scc_vec_init(symtab);
sccf_sect_data_t *text_data = null;
scc_pe_reserve_header(builder, 3);
scc_pe_section_range code_range = {0};
scc_pe_section_range data_range = {0};
scc_pe_section_range idata_range = {0};
sccf_sect_data_t *code_data = null;
sccf_sect_data_t *data_data = null;
int num_of_section = 1;
scc_vec_foreach(sccf->sect_headers, i) {
sccf_sect_header_t *sect_header = &scc_vec_at(sccf->sect_headers, i);
sccf_sect_data_t *sect_data = &scc_vec_at(sccf->sect_datas, i);
if (sect_header->sccf_sect_type == SCCF_SECT_CODE) {
text_data = sect_data;
code_range = scc_pe_reserve_text_section_header(
builder, scc_vec_size(*sect_data));
if (scc_vec_size(*sect_data) != 0) {
code_data = sect_data;
num_of_section += 1;
}
} else if (sect_header->sccf_sect_type == SCCF_SECT_DATA) {
data_range = scc_pe_reserve_data_section_header(
builder, scc_vec_size(*sect_data));
if (scc_vec_size(*sect_data) != 0) {
data_data = sect_data;
num_of_section += 1;
}
} else if (sect_header->sccf_sect_type == SCCF_SECT_STRTAB) {
scc_vec_unsafe_from_buffer(
strtab, (char *)scc_vec_unsafe_get_data(*sect_data),
@@ -135,6 +133,21 @@ void sccf2pe(scc_pe_builder_t *builder, const sccf_t *sccf) {
}
}
scc_pe_reserve_header(builder, num_of_section);
scc_pe_section_range code_range = {0};
scc_pe_section_range data_range = {0};
scc_pe_section_range idata_range = {0};
if (code_data) {
code_range = scc_pe_reserve_text_section_header(
builder, scc_vec_size(*code_data));
}
if (data_data) {
data_range = scc_pe_reserve_data_section_header(
builder, scc_vec_size(*data_data));
}
pe_idata_lib_ctx_t idata_lib_ctx;
pe_idata_lib_init(&idata_lib_ctx);
scc_vec_foreach(symtab, i) {
@@ -204,24 +217,21 @@ void sccf2pe(scc_pe_builder_t *builder, const sccf_t *sccf) {
}
Assert(reloc->sect_type == SCCF_SECT_CODE);
rva -= code_range.virual_address + reloc->offset + reloc->addend;
Assert(text_data != null);
Assert(code_data != null);
// FIXME 需要确保宿主机与目标机器大小端一致
*(u32 *)(scc_vec_unsafe_get_data(*text_data) + reloc->offset) = rva;
*(u32 *)(scc_vec_unsafe_get_data(*code_data) + reloc->offset) = rva;
}
scc_pe_write_header(builder, &config);
scc_vec_foreach(sccf->sect_headers, i) {
sccf_sect_header_t *sect_header = &scc_vec_at(sccf->sect_headers, i);
sccf_sect_data_t *sect_data = &scc_vec_at(sccf->sect_datas, i);
if (sect_header->sccf_sect_type == SCCF_SECT_CODE) {
scc_pe_write_section(builder, &code_range,
(u8 *)scc_vec_unsafe_get_data(*sect_data),
scc_vec_size(*sect_data));
} else if (sect_header->sccf_sect_type == SCCF_SECT_DATA) {
scc_pe_write_section(builder, &data_range,
(u8 *)scc_vec_unsafe_get_data(*sect_data),
scc_vec_size(*sect_data));
}
if (code_data != null) {
scc_pe_write_section(builder, &code_range,
(u8 *)scc_vec_unsafe_get_data(*code_data),
scc_vec_size(*code_data));
}
if (data_data != null) {
scc_pe_write_section(builder, &data_range,
(u8 *)scc_vec_unsafe_get_data(*data_data),
scc_vec_size(*data_data));
}
scc_pe_write_section(builder, &idata_range,
scc_vec_unsafe_get_data(idata_buffer),