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:
142
src/config.h
Normal file
142
src/config.h
Normal file
@@ -0,0 +1,142 @@
|
||||
#ifndef __SCC_CONFIG_H__
|
||||
#define __SCC_CONFIG_H__
|
||||
|
||||
#include <argparse.h>
|
||||
|
||||
typedef struct {
|
||||
const char *input_file;
|
||||
const char *output_file;
|
||||
int verbose;
|
||||
scc_argparse_list_t include_paths;
|
||||
scc_argparse_list_t define_macros;
|
||||
cbool emit_lex;
|
||||
cbool emit_pp;
|
||||
cbool emit_ast;
|
||||
cbool emit_ir;
|
||||
} scc_config_t;
|
||||
|
||||
static void setup_argparse(scc_argparse_t *argparse, scc_config_t *config,
|
||||
scc_argparse_lang_t lang) {
|
||||
enum {
|
||||
SCC_HINT_PROG_NAME,
|
||||
SCC_HINT_DESCRIPTION,
|
||||
SCC_HINT_OUTPUT_FILE,
|
||||
SCC_HINT_INPUT_FILE,
|
||||
SCC_HINT_INCLUDE_PATH,
|
||||
SCC_HINT_DEFINED_MACRO,
|
||||
SCC_HINT_VERBOSE,
|
||||
|
||||
SCC_HINT_EMIT_LEX,
|
||||
SCC_HINT_EMIT_PP,
|
||||
SCC_HINT_EMIT_AST,
|
||||
SCC_HINT_EMIT_IR,
|
||||
};
|
||||
static const char *scc_hints_en[] = {
|
||||
[SCC_HINT_PROG_NAME] = "scc",
|
||||
[SCC_HINT_DESCRIPTION] = "A simple C compiler",
|
||||
[SCC_HINT_OUTPUT_FILE] =
|
||||
"Output file (`-` means standard output stream file)",
|
||||
[SCC_HINT_INPUT_FILE] = "Input source file",
|
||||
[SCC_HINT_INCLUDE_PATH] = "Add directory to the include search paths",
|
||||
[SCC_HINT_DEFINED_MACRO] = "Define a macro",
|
||||
[SCC_HINT_VERBOSE] = "Increase verbosity (can be used multiple times)",
|
||||
[SCC_HINT_EMIT_LEX] = "Generate lexer sources tokens and exit",
|
||||
[SCC_HINT_EMIT_PP] = "Generate preprocessed tokens and exit",
|
||||
[SCC_HINT_EMIT_AST] = "Generate AST and exit",
|
||||
[SCC_HINT_EMIT_IR] = "Generate IR and exit",
|
||||
};
|
||||
static const char *scc_hints_zh[] = {
|
||||
[SCC_HINT_PROG_NAME] = "scc",
|
||||
[SCC_HINT_DESCRIPTION] = "一个简单的C编译器",
|
||||
[SCC_HINT_OUTPUT_FILE] = "输出文件(`-`表示标准输出流文件)",
|
||||
[SCC_HINT_INPUT_FILE] = "输入源文件",
|
||||
[SCC_HINT_INCLUDE_PATH] = "添加系统头文件到搜索路径",
|
||||
[SCC_HINT_DEFINED_MACRO] = "定义宏",
|
||||
[SCC_HINT_VERBOSE] = "增加详细输出(可多次使用)",
|
||||
[SCC_HINT_EMIT_LEX] = "生成`源代码的词法单元`并退出",
|
||||
[SCC_HINT_EMIT_PP] = "生成`预处理后的词法单元`并退出",
|
||||
[SCC_HINT_EMIT_AST] = "生成`抽象语法树`并退出",
|
||||
[SCC_HINT_EMIT_IR] = "生成`中间代码`并退出",
|
||||
};
|
||||
|
||||
const char **scc_hints;
|
||||
switch (lang) {
|
||||
case SCC_ARGPARSE_LANG_EN:
|
||||
scc_hints = scc_hints_en;
|
||||
break;
|
||||
case SCC_ARGPARSE_LANG_ZH:
|
||||
scc_hints = scc_hints_zh;
|
||||
break;
|
||||
default:
|
||||
scc_hints = scc_hints_en;
|
||||
break;
|
||||
}
|
||||
|
||||
scc_argparse_init(argparse, scc_hints[SCC_HINT_PROG_NAME],
|
||||
scc_hints[SCC_HINT_DESCRIPTION]);
|
||||
argparse->lang = lang;
|
||||
scc_argparse_cmd_t *root = scc_argparse_get_root(argparse);
|
||||
|
||||
// -o, --output
|
||||
scc_argparse_opt_t opt_output;
|
||||
scc_argparse_opt_init(&opt_output, 'o', "output",
|
||||
scc_hints[SCC_HINT_OUTPUT_FILE]);
|
||||
scc_argparse_spec_setup_string(&opt_output.spec, &(config->output_file));
|
||||
scc_argparse_cmd_add_opt(root, &opt_output);
|
||||
|
||||
// input file (必需)
|
||||
scc_argparse_arg_t arg_input;
|
||||
scc_argparse_arg_init(&arg_input, "input", scc_hints[SCC_HINT_INPUT_FILE]);
|
||||
scc_argparse_spec_setup_string(&arg_input.spec, &(config->input_file));
|
||||
scc_argparse_spec_set_required(&arg_input.spec, true);
|
||||
scc_argparse_cmd_add_arg(root, &arg_input);
|
||||
|
||||
// -I, --include (添加额外的系统头文件搜索路径)
|
||||
scc_argparse_opt_t opt_include;
|
||||
scc_argparse_opt_init(&opt_include, 'I', "include",
|
||||
scc_hints[SCC_HINT_INCLUDE_PATH]);
|
||||
scc_argparse_spec_setup_list(&opt_include.spec, &(config->include_paths));
|
||||
scc_argparse_cmd_add_opt(root, &opt_include);
|
||||
|
||||
// -D, --define (定义宏)
|
||||
scc_argparse_opt_t opt_define;
|
||||
scc_argparse_opt_init(&opt_define, 'D', "define",
|
||||
scc_hints[SCC_HINT_DEFINED_MACRO]);
|
||||
scc_argparse_spec_setup_list(&opt_define.spec, &(config->define_macros));
|
||||
scc_argparse_cmd_add_opt(root, &opt_define);
|
||||
|
||||
// -v, --verbose (计数)
|
||||
scc_argparse_opt_t opt_verbose;
|
||||
scc_argparse_opt_init(&opt_verbose, 'V', "verbose",
|
||||
scc_hints[SCC_HINT_VERBOSE]);
|
||||
scc_argparse_spec_setup_count(&opt_verbose.spec, &(config->verbose));
|
||||
scc_argparse_cmd_add_opt(root, &opt_verbose);
|
||||
|
||||
// --emit-lex
|
||||
scc_argparse_opt_t opt_lex;
|
||||
scc_argparse_opt_init(&opt_lex, 0, "emit-lex",
|
||||
scc_hints[SCC_HINT_EMIT_LEX]);
|
||||
scc_argparse_spec_setup_bool(&opt_lex.spec, &(config->emit_lex));
|
||||
scc_argparse_cmd_add_opt(root, &opt_lex);
|
||||
|
||||
// --emit-pp
|
||||
scc_argparse_opt_t opt_pp;
|
||||
scc_argparse_opt_init(&opt_pp, 0, "emit-pp", scc_hints[SCC_HINT_EMIT_PP]);
|
||||
scc_argparse_spec_setup_bool(&opt_pp.spec, &(config->emit_pp));
|
||||
scc_argparse_cmd_add_opt(root, &opt_pp);
|
||||
|
||||
// -T, --emit-ast
|
||||
scc_argparse_opt_t opt_ast;
|
||||
scc_argparse_opt_init(&opt_ast, 'T', "emit-ast",
|
||||
scc_hints[SCC_HINT_EMIT_AST]);
|
||||
scc_argparse_spec_setup_bool(&opt_ast.spec, &(config->emit_ast));
|
||||
scc_argparse_cmd_add_opt(root, &opt_ast);
|
||||
|
||||
// -R, --emit-ir
|
||||
scc_argparse_opt_t opt_ir;
|
||||
scc_argparse_opt_init(&opt_ir, 'R', "emit-ir", scc_hints[SCC_HINT_EMIT_IR]);
|
||||
scc_argparse_spec_setup_bool(&opt_ir.spec, &(config->emit_ir));
|
||||
scc_argparse_cmd_add_opt(root, &opt_ir);
|
||||
}
|
||||
|
||||
#endif /* __SCC_CONFIG_H___ */
|
||||
137
src/main.c
137
src/main.c
@@ -7,130 +7,7 @@
|
||||
#include <scc_ast2ir.h>
|
||||
#include <scc_parser.h>
|
||||
|
||||
typedef struct {
|
||||
const char *input_file;
|
||||
const char *output_file;
|
||||
int verbose;
|
||||
scc_argparse_list_t include_paths;
|
||||
cbool emit_lex;
|
||||
cbool emit_pp;
|
||||
cbool emit_ast;
|
||||
cbool emit_ir;
|
||||
} scc_config_t;
|
||||
|
||||
static void setup_argparse(scc_argparse_t *argparse, scc_config_t *config,
|
||||
scc_argparse_lang_t lang) {
|
||||
enum {
|
||||
SCC_HINT_PROG_NAME,
|
||||
SCC_HINT_DESCRIPTION,
|
||||
SCC_HINT_OUTPUT_FILE,
|
||||
SCC_HINT_INPUT_FILE,
|
||||
SCC_HINT_INCLUDE_PATH,
|
||||
SCC_HINT_VERBOSE,
|
||||
|
||||
SCC_HINT_EMIT_LEX,
|
||||
SCC_HINT_EMIT_PP,
|
||||
SCC_HINT_EMIT_AST,
|
||||
SCC_HINT_EMIT_IR,
|
||||
};
|
||||
static const char *scc_hints_en[] = {
|
||||
[SCC_HINT_PROG_NAME] = "scc",
|
||||
[SCC_HINT_DESCRIPTION] = "A simple C compiler",
|
||||
[SCC_HINT_OUTPUT_FILE] =
|
||||
"Output file (`-` means standard output stream file)",
|
||||
[SCC_HINT_INPUT_FILE] = "Input source file",
|
||||
[SCC_HINT_INCLUDE_PATH] = "Add directory to the include search paths",
|
||||
[SCC_HINT_VERBOSE] = "Increase verbosity (can be used multiple times)",
|
||||
[SCC_HINT_EMIT_LEX] = "Generate lexer sources tokens and exit",
|
||||
[SCC_HINT_EMIT_PP] = "Generate preprocessed tokens and exit",
|
||||
[SCC_HINT_EMIT_AST] = "Generate AST and exit",
|
||||
[SCC_HINT_EMIT_IR] = "Generate IR and exit",
|
||||
};
|
||||
static const char *scc_hints_zh[] = {
|
||||
[SCC_HINT_PROG_NAME] = "scc",
|
||||
[SCC_HINT_DESCRIPTION] = "一个简单的C编译器",
|
||||
[SCC_HINT_OUTPUT_FILE] = "输出文件(`-`表示标准输出流文件)",
|
||||
[SCC_HINT_INPUT_FILE] = "输入源文件",
|
||||
[SCC_HINT_INCLUDE_PATH] = "添加系统头文件到搜索路径",
|
||||
[SCC_HINT_VERBOSE] = "增加详细输出(可多次使用)",
|
||||
[SCC_HINT_EMIT_LEX] = "生成`源代码的词法单元`并退出",
|
||||
[SCC_HINT_EMIT_PP] = "生成`预处理后的词法单元`并退出",
|
||||
[SCC_HINT_EMIT_AST] = "生成`抽象语法树`并退出",
|
||||
[SCC_HINT_EMIT_IR] = "生成`中间代码`并退出",
|
||||
};
|
||||
|
||||
const char **scc_hints;
|
||||
switch (lang) {
|
||||
case SCC_ARGPARSE_LANG_EN:
|
||||
scc_hints = scc_hints_en;
|
||||
break;
|
||||
case SCC_ARGPARSE_LANG_ZH:
|
||||
scc_hints = scc_hints_zh;
|
||||
break;
|
||||
default:
|
||||
scc_hints = scc_hints_en;
|
||||
break;
|
||||
}
|
||||
|
||||
scc_argparse_init(argparse, scc_hints[SCC_HINT_PROG_NAME],
|
||||
scc_hints[SCC_HINT_DESCRIPTION]);
|
||||
argparse->lang = lang;
|
||||
scc_argparse_cmd_t *root = scc_argparse_get_root(argparse);
|
||||
|
||||
// -o, --output
|
||||
scc_argparse_opt_t opt_output;
|
||||
scc_argparse_opt_init(&opt_output, 'o', "output",
|
||||
scc_hints[SCC_HINT_OUTPUT_FILE]);
|
||||
scc_argparse_spec_setup_string(&opt_output.spec, &(config->output_file));
|
||||
scc_argparse_cmd_add_opt(root, &opt_output);
|
||||
|
||||
// input file (必需)
|
||||
scc_argparse_arg_t arg_input;
|
||||
scc_argparse_arg_init(&arg_input, "input", scc_hints[SCC_HINT_INPUT_FILE]);
|
||||
scc_argparse_spec_setup_string(&arg_input.spec, &(config->input_file));
|
||||
scc_argparse_spec_set_required(&arg_input.spec, true);
|
||||
scc_argparse_cmd_add_arg(root, &arg_input);
|
||||
|
||||
// -I, --include (添加额外的系统头文件搜索路径)
|
||||
scc_argparse_opt_t opt_include;
|
||||
scc_argparse_opt_init(&opt_include, 'I', "include",
|
||||
scc_hints[SCC_HINT_INCLUDE_PATH]);
|
||||
scc_argparse_spec_setup_list(&opt_include.spec, &(config->include_paths));
|
||||
scc_argparse_cmd_add_opt(root, &opt_include);
|
||||
|
||||
// -v, --verbose (计数)
|
||||
scc_argparse_opt_t opt_verbose;
|
||||
scc_argparse_opt_init(&opt_verbose, 'V', "verbose",
|
||||
scc_hints[SCC_HINT_VERBOSE]);
|
||||
scc_argparse_spec_setup_count(&opt_verbose.spec, &(config->verbose));
|
||||
scc_argparse_cmd_add_opt(root, &opt_verbose);
|
||||
|
||||
// --emit-lex
|
||||
scc_argparse_opt_t opt_lex;
|
||||
scc_argparse_opt_init(&opt_lex, 0, "emit-lex",
|
||||
scc_hints[SCC_HINT_EMIT_LEX]);
|
||||
scc_argparse_spec_setup_bool(&opt_lex.spec, &(config->emit_lex));
|
||||
scc_argparse_cmd_add_opt(root, &opt_lex);
|
||||
|
||||
// --emit-pp
|
||||
scc_argparse_opt_t opt_pp;
|
||||
scc_argparse_opt_init(&opt_pp, 0, "emit-pp", scc_hints[SCC_HINT_EMIT_PP]);
|
||||
scc_argparse_spec_setup_bool(&opt_pp.spec, &(config->emit_pp));
|
||||
scc_argparse_cmd_add_opt(root, &opt_pp);
|
||||
|
||||
// -T, --emit-ast
|
||||
scc_argparse_opt_t opt_ast;
|
||||
scc_argparse_opt_init(&opt_ast, 'T', "emit-ast",
|
||||
scc_hints[SCC_HINT_EMIT_AST]);
|
||||
scc_argparse_spec_setup_bool(&opt_ast.spec, &(config->emit_ast));
|
||||
scc_argparse_cmd_add_opt(root, &opt_ast);
|
||||
|
||||
// -R, --emit-ir
|
||||
scc_argparse_opt_t opt_ir;
|
||||
scc_argparse_opt_init(&opt_ir, 'R', "emit-ir", scc_hints[SCC_HINT_EMIT_IR]);
|
||||
scc_argparse_spec_setup_bool(&opt_ir.spec, &(config->emit_ir));
|
||||
scc_argparse_cmd_add_opt(root, &opt_ir);
|
||||
}
|
||||
#include "config.h"
|
||||
|
||||
#ifdef _WIN32
|
||||
#define WIN32_LEAN_AND_MEAN
|
||||
@@ -332,11 +209,10 @@ sstream_drop:
|
||||
return 0;
|
||||
}
|
||||
|
||||
scc_ir_builder_t ir_builder;
|
||||
scc_ir_builder_init(&ir_builder);
|
||||
scc_ast2ir_ctx_t ast2ir_ctx;
|
||||
#include <abi/win_x64_type_abi.h>
|
||||
scc_ast2ir_translation_unit(&ir_builder, translation_unit,
|
||||
scc_win_x64_type_abi);
|
||||
scc_ast2ir_ctx_init(&ast2ir_ctx, scc_win_x64_type_abi);
|
||||
scc_ast2ir_translation_unit(&ast2ir_ctx, translation_unit);
|
||||
|
||||
if (config.emit_ir) {
|
||||
scc_ir_dump_ctx_t ir_dump_ctx;
|
||||
@@ -348,8 +224,9 @@ sstream_drop:
|
||||
scc_tree_dump_ctx_init(&tree_dump, false, (void *)scc_fprintf,
|
||||
(void *)fp);
|
||||
}
|
||||
scc_ir_dump_ctx_init(&ir_dump_ctx, &tree_dump, &ir_builder.cprog,
|
||||
&ir_builder.ctx);
|
||||
scc_ir_dump_ctx_init(&ir_dump_ctx, &tree_dump,
|
||||
&ast2ir_ctx.builder.cprog,
|
||||
&ast2ir_ctx.builder.ctx);
|
||||
// scc_ir_dump_cprog(&ir_dump_ctx);
|
||||
scc_ir_dump_cprog_linear(&ir_dump_ctx);
|
||||
scc_tree_dump_ctx_drop(&tree_dump);
|
||||
|
||||
Reference in New Issue
Block a user