feat(parser): 使用静态数组初始化测试向量

- 将多个测试用例中的 `scc_vec_unsafe_from_array` 替换为
  `scc_vec_unsafe_from_static_array` 以提高性能
- 此更改影响了 `test_parser_unit` 和 `test_parser_type` 函数中的多个位置

feat(sccf): 添加SCC格式支持和相关工具

- 创建新的 sccf 库用于处理 SCCF (SCC Format) 文件格式
- 实现了基本的文件格式定义,包括头部、段表、符号表等结构
- 添加了构建器和链接器的基本框架
- 包含格式化工具的初始实现

refactor(main): 修复输出文件检查逻辑

- 修正主函数中输出文件检查条件,确保在 fp 为 null 时正确处理
- 更新输出消息显示逻辑以匹配文件操作状态
This commit is contained in:
zzy
2026-03-16 11:02:32 +08:00
parent 45c59e0b65
commit cabd1710ed
12 changed files with 614 additions and 27 deletions

8
libs/sccf/src/main.c Normal file
View File

@@ -0,0 +1,8 @@
#include <sccf_builder.h>
int main(void) {
sccf_builder_t sccf_builder;
sccf_builder_init(&sccf_builder);
sccf_builder_to_file(&sccf_builder, "test.o");
return 0;
}

View File

@@ -0,0 +1,51 @@
#include <sccf_builder.h>
void sccf_builder_init(sccf_builder_t *builder) {
builder->aligned = 64;
sccf_init(&builder->sccf);
scc_strpool_init(&builder->strpool);
scc_hashtable_init(&builder->str2offset,
(scc_hashtable_hash_func_t)scc_strhash32,
(scc_hashtable_equal_func_t)scc_strcmp);
scc_vec_init(builder->relocs);
scc_vec_init(builder->syms);
}
void sccf_builder_add_section(sccf_builder_t *builder,
sccf_sect_header_t *sect_header,
sccf_sect_data_t *sect_data) {
Assert((usize)(sect_header->size) == scc_vec_size(*sect_data));
builder->sccf.header.sect_header_num += 1;
scc_vec_push(builder->sccf.sect_headers, *sect_header);
scc_vec_push(builder->sccf.sect_datas, *sect_data);
}
void sccf_builder_to_buffer(sccf_builder_t *builder, sccf_buffer_t *buffer) {
Assert(builder != null && buffer != null);
// TODO symtab strtab reloc
// sccf_sect_header_t symtab_header;
// sccf_sect_data_t symtab_data;
// sccf_builder_add_section(builder, &symtab_header, &symtab_data);
sccf_write(&builder->sccf, buffer);
}
void sccf_builder_to_file(sccf_builder_t *builder, const char *file_path) {
Assert(builder != null && file_path != null);
scc_file_t fp = scc_fopen(file_path, SCC_FILE_WRITE);
if (fp == null) {
LOG_ERROR("file can't open %s", file_path);
return;
}
sccf_buffer_t buffer;
scc_vec_init(buffer);
sccf_builder_to_buffer(builder, &buffer);
usize write_size =
scc_fwrite(fp, scc_vec_unsafe_get_data(buffer), scc_vec_size(buffer));
if (write_size != scc_vec_size(buffer)) {
LOG_ERROR("file write failed expect write %zu but got %zu",
scc_vec_size(buffer), write_size);
}
scc_vec_free(buffer);
scc_fclose(fp);
}