feat(ast): 添加汇编器模块并改进AST定义和IR转换

- 在README.md中添加asm汇编器模块说明
- 更新ast_def.h中的枚举注释,添加sema相关信息以明确语义分析作用域
- 重命名函数参数param_types为params,使命名更清晰
- 移除call表达式中的_target字段,简化结构
- 为member、identifier等字段添加///< fill by sema注释说明填充时机
- 为jump语句添加_target字段用于语义分析
- 更新所有AST初始化函数,接受位置信息参数以改进错误定位
- 修复alignof表达式的类型应为ALIGN_OF而非SIZE_OF的问题
- 重构ast2ir.h,引入scc_ast2ir_ctx_t上下文结构体统一管理转换状态
- 添加符号表、节点到IR映射等必要的转换上下文信息
This commit is contained in:
zzy
2026-03-17 20:29:40 +08:00
parent cabd1710ed
commit 2e5e98868d
29 changed files with 1289 additions and 1000 deletions

View File

@@ -69,6 +69,66 @@ scc_pe_section_range scc_pe_reserve_section_header(scc_pe_builder_t *builder,
u32 virtual_size,
u32 data_size);
static inline scc_pe_section_range
scc_pe_reserve_text_section_header(scc_pe_builder_t *builder, u32 data_size) {
return scc_pe_reserve_section_header(
builder, (BYTE *)".text\0\0",
IMAGE_SCN_CNT_CODE | IMAGE_SCN_MEM_EXECUTE | IMAGE_SCN_MEM_READ,
data_size, data_size);
}
static inline scc_pe_section_range
scc_pe_reserve_data_section_header(scc_pe_builder_t *builder, u32 data_size) {
return scc_pe_reserve_section_header(builder, (BYTE *)".data\0\0",
IMAGE_SCN_CNT_INITIALIZED_DATA |
IMAGE_SCN_MEM_READ |
IMAGE_SCN_MEM_WRITE,
data_size, data_size);
}
static inline scc_pe_section_range
scc_pe_reserve_rdata_section_header(scc_pe_builder_t *builder, u32 data_size) {
return scc_pe_reserve_section_header(builder, (BYTE *)".rdata\0",
IMAGE_SCN_CNT_INITIALIZED_DATA |
IMAGE_SCN_MEM_READ,
data_size, data_size);
}
static inline scc_pe_section_range
scc_pe_reserve_bss_section_header(scc_pe_builder_t *builder, u32 data_size) {
return scc_pe_reserve_section_header(builder, (BYTE *)".bss\0\0\0",
IMAGE_SCN_CNT_UNINITIALIZED_DATA |
IMAGE_SCN_MEM_READ |
IMAGE_SCN_MEM_WRITE,
data_size, 0);
}
static inline scc_pe_section_range
scc_pe_reserve_idata_section_header(scc_pe_builder_t *builder, u32 data_size) {
scc_pe_section_range range = scc_pe_reserve_section_header(
builder, (BYTE *)".idata\0",
IMAGE_SCN_CNT_INITIALIZED_DATA | IMAGE_SCN_MEM_READ |
IMAGE_SCN_MEM_WRITE,
data_size, data_size);
scc_vec_at(builder->image_data_directory_vec,
IMAGE_DIRECTORY_ENTRY_IMPORT) = (IMAGE_DATA_DIRECTORY){
.VirtualAddress = range.virual_address, .Size = range.virual_size};
return range;
}
static inline scc_pe_section_range
scc_pe_reserve_reloc_section_header(scc_pe_builder_t *builder, u32 data_size) {
scc_pe_section_range range = scc_pe_reserve_section_header(
builder, (BYTE *)".reloc\0\0",
IMAGE_SCN_CNT_INITIALIZED_DATA | IMAGE_SCN_MEM_READ |
IMAGE_SCN_MEM_DISCARDABLE,
data_size, data_size);
scc_vec_at(builder->image_data_directory_vec,
IMAGE_DIRECTORY_ENTRY_BASERELOC) = (IMAGE_DATA_DIRECTORY){
.VirtualAddress = range.virual_address, .Size = range.virual_size};
return range;
}
void scc_pe_write_header(scc_pe_builder_t *builder, scc_pe_config_t *config);
void scc_pe_write_section(scc_pe_builder_t *builder,

View File

@@ -15,6 +15,9 @@
*
* @bug 可以有更好的方式解决这个问题
*/
#ifdef DUMMYUNIONNAME
#undef DUMMYUNIONNAME
#endif
#define DUMMYUNIONNAME DUMMYUNIONNAME
// 基本类型定义

View File

@@ -10,33 +10,21 @@ typedef struct {
scc_pe_buffer_t data; ///< 具体数据
scc_hashtable_t str_map; ///< 符号名称映射到data的idx
u32 section_offset; ///< 在idata中的偏移
} scc_winpe_hnt_builder_t;
typedef struct {
u32 offset; ///< 相对代码段的偏移地址
i32 addend; ///< 可选的偏移量的附加值 可选默认为0
u8 size; ///< 引用的地址的大小
const char *library_name; ///< 库名称 eg. "Kernel32.dll"
const char *symbol_name; ///< 导入dll的符号名称 eg. "CreateFileW"
u16 ordinal; ///< 符号的序号 eg. 可选
} scc_winpe_extern_t;
typedef SCC_VEC(scc_winpe_extern_t) scc_winpe_extern_vec_t;
} scc_pe_hnt_builder_t;
typedef SCC_VEC(const char *) scc_winpe_name_vec_t;
typedef struct {
const char *name;
scc_winpe_name_vec_t symbol_names;
} scc_winpe_idata_lib_t;
typedef SCC_VEC(scc_winpe_idata_lib_t) scc_winpe_idata_lib_vec_t;
typedef SCC_VEC(scc_winpe_idata_lib_t) scc_pe_idata_lib_vec_t;
typedef struct {
scc_pe_buffer_t buffer; ///< 导入表数据
scc_winpe_extern_vec_t externs;
scc_hashtable_t externs_set;
scc_winpe_hnt_builder_t hnt_builder;
scc_pe_hnt_builder_t hnt_builder;
scc_hashtable_t iat_map; ///< 符号名称映射到idata的虚拟镜像地址
scc_winpe_idata_lib_vec_t idata_libs;
} scc_winpe_idata_builder_t;
scc_pe_idata_lib_vec_t idata_libs;
} scc_pe_idata_builder_t;
/**
* @brief PE格式导入表构建器初始化
@@ -45,8 +33,8 @@ typedef struct {
* @param builder
* @param idata_libs
*/
void scc_pe_idata_builder_init(scc_winpe_idata_builder_t *builder,
scc_winpe_idata_lib_vec_t *idata_libs);
void scc_pe_idata_builder_init(scc_pe_idata_builder_t *builder,
scc_pe_idata_lib_vec_t *idata_libs);
/**
* @brief 获取导入表占据的大小
@@ -55,7 +43,7 @@ void scc_pe_idata_builder_init(scc_winpe_idata_builder_t *builder,
* @param builder
* @return u32
*/
u32 scc_pe_reserve_idata(scc_winpe_idata_builder_t *builder);
u32 scc_pe_reserve_idata(scc_pe_idata_builder_t *builder);
/**
* @brief 构造导入表
@@ -64,13 +52,12 @@ u32 scc_pe_reserve_idata(scc_winpe_idata_builder_t *builder);
* @param builder
* @param idata_range
*/
scc_pe_buffer_t scc_pe_construct_idata(scc_winpe_idata_builder_t *builder,
scc_pe_buffer_t scc_pe_construct_idata(scc_pe_idata_builder_t *builder,
scc_pe_section_range *idata_range);
// RVA Relative Virtual Address
static inline u64
scc_pe_idata_get_symbol_rva(scc_winpe_idata_builder_t *builder,
const char *symbol_name) {
static inline u64 scc_pe_idata_get_symbol_rva(scc_pe_idata_builder_t *builder,
const char *symbol_name) {
return (u64)scc_hashtable_get(&builder->iat_map, symbol_name);
}

View File

@@ -0,0 +1,45 @@
#ifndef __SCC_PE_RELOC_H__
#define __SCC_PE_RELOC_H__
#include "scc_pe_def.h"
typedef struct {
u32 offset; ///< 相对代码段的偏移地址
i32 addend; ///< 可选的偏移量的附加值 可选默认为0
u8 size; ///< 引用的地址的大小
const char *library_name; ///< 库名称 eg. "Kernel32.dll"
const char *symbol_name; ///< 导入dll的符号名称 eg. "CreateFileW"
u16 ordinal; ///< 符号的序号 eg. 可选
} scc_pe_extern_t;
typedef SCC_VEC(scc_pe_extern_t) scc_pe_extern_vec_t;
typedef struct {
scc_pe_extern_vec_t externs;
scc_hashtable_t externs_set;
} scc_pe_reloc_builder_t;
void scc_pe_reloc_builder_init(scc_pe_reloc_builder_t *builder);
// void scc_pe_reloc_add_item(scc_pe_reloc_builder_t *builder, );
/**
* @brief
* @warning
*
* @param builder
* @return u32
*/
u32 scc_pe_reserve_reloc(scc_pe_reloc_builder_t *builder);
/**
* @brief
* @warning
*
* @param builder
* @param reloc_range
*/
scc_pe_buffer_t scc_pe_construct_reloc(scc_pe_reloc_builder_t *builder,
scc_pe_section_range *reloc_range);
#endif /* __SCC_PE_RELOC_H__ */

View File

@@ -13,9 +13,9 @@ image_import_descriptor_init(u32 import_lookup_table_rva, u32 name_rva,
return iid;
};
static void scc_winpe_hnt_builder_push(scc_winpe_hnt_builder_t *builder,
static void scc_winpe_hnt_builder_push(scc_pe_hnt_builder_t *builder,
const char *name, u16 hint);
static void scc_winpe_hnt_builder_init(scc_winpe_hnt_builder_t *builder) {
static void scc_winpe_hnt_builder_init(scc_pe_hnt_builder_t *builder) {
scc_vec_init(builder->data);
builder->section_offset = 0;
scc_hashtable_init(&builder->str_map,
@@ -23,7 +23,7 @@ static void scc_winpe_hnt_builder_init(scc_winpe_hnt_builder_t *builder) {
(scc_hashtable_equal_func_t)scc_strcmp);
}
static void scc_winpe_hnt_builder_push(scc_winpe_hnt_builder_t *builder,
static void scc_winpe_hnt_builder_push(scc_pe_hnt_builder_t *builder,
const char *name, u16 hint) {
///< 可选哈希表去重
if (scc_hashtable_get(&builder->str_map, name)) {
@@ -49,13 +49,13 @@ static void scc_winpe_hnt_builder_push(scc_winpe_hnt_builder_t *builder,
Assert(scc_vec_size(builder->data) % 2 == 0);
}
static u64 scc_winpe_hnt_get_offset(scc_winpe_hnt_builder_t *builder,
static u64 scc_winpe_hnt_get_offset(scc_pe_hnt_builder_t *builder,
const char *name) {
return (u64)scc_hashtable_get(&builder->str_map, name);
}
void scc_pe_idata_builder_init(scc_winpe_idata_builder_t *builder,
scc_winpe_idata_lib_vec_t *idata_libs) {
void scc_pe_idata_builder_init(scc_pe_idata_builder_t *builder,
scc_pe_idata_lib_vec_t *idata_libs) {
scc_vec_init(builder->buffer);
builder->idata_libs = *idata_libs;
@@ -66,7 +66,7 @@ void scc_pe_idata_builder_init(scc_winpe_idata_builder_t *builder,
(scc_hashtable_equal_func_t)scc_strcmp);
}
u32 scc_pe_reserve_idata(scc_winpe_idata_builder_t *builder) {
u32 scc_pe_reserve_idata(scc_pe_idata_builder_t *builder) {
u32 idata_size = (scc_vec_size(builder->idata_libs) + 1) *
sizeof(IMAGE_IMPORT_DESCRIPTOR);
@@ -89,7 +89,7 @@ u32 scc_pe_reserve_idata(scc_winpe_idata_builder_t *builder) {
return idata_size;
}
scc_pe_buffer_t scc_pe_construct_idata(scc_winpe_idata_builder_t *builder,
scc_pe_buffer_t scc_pe_construct_idata(scc_pe_idata_builder_t *builder,
scc_pe_section_range *idata_range) {
u32 idata_rva = idata_range->virual_address;
u32 hnt_offset = builder->hnt_builder.section_offset;

View File

View File

@@ -24,19 +24,14 @@ int main() {
scc_pe_builder_t builder = {0};
scc_pe_builder_init(&builder, true, 4096, 512);
scc_pe_reserve_header(&builder, 3);
scc_pe_section_range code_range = scc_pe_reserve_section_header(
&builder, (BYTE *)".text\0\0",
IMAGE_SCN_CNT_CODE | IMAGE_SCN_MEM_READ | IMAGE_SCN_MEM_EXECUTE,
sizeof(code), sizeof(code));
scc_pe_section_range code_range =
scc_pe_reserve_text_section_header(&builder, sizeof(code));
scc_pe_section_range data_range = scc_pe_reserve_section_header(
&builder, (BYTE *)".data\0\0",
IMAGE_SCN_CNT_INITIALIZED_DATA | IMAGE_SCN_MEM_READ |
IMAGE_SCN_MEM_WRITE,
sizeof(data), sizeof(data));
scc_pe_section_range data_range =
scc_pe_reserve_data_section_header(&builder, sizeof(data));
scc_winpe_idata_builder_t idata_builder;
scc_winpe_idata_lib_vec_t idata_libs;
scc_pe_idata_builder_t idata_builder;
scc_pe_idata_lib_vec_t idata_libs;
scc_vec_init(idata_libs);
scc_winpe_idata_lib_t ucrtbase;
ucrtbase.name = "ucrtbase.dll";
@@ -45,14 +40,8 @@ int main() {
scc_vec_push(idata_libs, ucrtbase);
scc_pe_idata_builder_init(&idata_builder, &idata_libs);
u32 idata_size = scc_pe_reserve_idata(&idata_builder);
scc_pe_section_range idata_range = scc_pe_reserve_section_header(
&builder, (BYTE *)".idata\0",
IMAGE_SCN_CNT_INITIALIZED_DATA | IMAGE_SCN_MEM_READ |
IMAGE_SCN_MEM_WRITE,
idata_size, idata_size);
scc_vec_at(builder.image_data_directory_vec, IMAGE_DIRECTORY_ENTRY_IMPORT) =
(IMAGE_DATA_DIRECTORY){.VirtualAddress = idata_range.virual_address,
.Size = idata_range.virual_size};
scc_pe_section_range idata_range =
scc_pe_reserve_idata_section_header(&builder, idata_size);
scc_pe_buffer_t idata_buffer =
scc_pe_construct_idata(&idata_builder, &idata_range);
@@ -97,5 +86,5 @@ int main() {
scc_pe_write_section(&builder, &idata_range,
scc_vec_unsafe_get_data(idata_buffer),
scc_vec_size(idata_buffer));
scc_pe_dump_to_file(&builder, "hello_world.exe");
scc_pe_dump_to_file(&builder, __FILE__ "/../pe_write_idata.exe");
}

View File

@@ -0,0 +1,2 @@
int main(void) { return 0; }

View File

@@ -1,10 +0,0 @@
#include <stdio.h>
void test_example() {
printf("Test passed!\n");
}
int main() {
test_example();
return 0;
}