feat(argparse): 添加枚举类型选项支持
添加了对命令行参数枚举类型的支持,允许用户从预定义的选项中进行选择, 并在解析时进行验证。同时修复了空值检查问题。 refactor(mir): 重构pass管理系统 将原有的pass管理器系统简化为直接的pass执行函数,移除了复杂的 依赖管理和流水线构建机制,使系统更加简洁明了。 chore(ir): 添加Windows x64 ABI实现 实现了Windows x64平台的ABI约定,包括参数传递、寄存器使用和 栈帧布局的处理逻辑。 feat(config): 统一编译阶段控制配置 将多个独立的布尔类型编译阶段控制改为统一的枚举类型,便于 管理和扩展新的编译阶段。
This commit is contained in:
175
src/config.c
Normal file
175
src/config.c
Normal file
@@ -0,0 +1,175 @@
|
||||
#include "config.h"
|
||||
|
||||
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_TARGET_DESC,
|
||||
SCC_HINT_VERBOSE,
|
||||
|
||||
SCC_HINT_ENTRY_POINT_SYMBOL,
|
||||
|
||||
SCC_HINT_EMIT_STAGE,
|
||||
|
||||
SCC_HINT_EMIT_LEX,
|
||||
SCC_HINT_EMIT_PP,
|
||||
SCC_HINT_EMIT_AST,
|
||||
SCC_HINT_EMIT_HIR,
|
||||
SCC_HINT_EMIT_LIR,
|
||||
SCC_HINT_EMIT_MIR,
|
||||
|
||||
SCC_HINT_EMIT_FLATBIN,
|
||||
SCC_HINT_EMIT_SCCF,
|
||||
SCC_HINT_EMIT_TARGET,
|
||||
};
|
||||
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_TARGET_DESC] =
|
||||
"Target description(eg. x86_64-pc-windows-msvc)",
|
||||
[SCC_HINT_VERBOSE] = "Increase verbosity (can be used multiple times)",
|
||||
|
||||
[SCC_HINT_ENTRY_POINT_SYMBOL] = "Entry point symbol name",
|
||||
|
||||
[SCC_HINT_EMIT_STAGE] = "Generate intermediate code and exit",
|
||||
|
||||
[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_HIR] = "Generate High-level IR and exit",
|
||||
[SCC_HINT_EMIT_LIR] = "Generate Low-level IR and exit",
|
||||
[SCC_HINT_EMIT_MIR] = "Generate Machine IR and exit",
|
||||
|
||||
[SCC_HINT_EMIT_FLATBIN] = "Generate flat binary and exit",
|
||||
[SCC_HINT_EMIT_SCCF] = "Generate SCCF and exit",
|
||||
[SCC_HINT_EMIT_TARGET] = "Generate target description 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_TARGET_DESC] = "目标机器描述(eg. x86_64-pc-windows-msvc)",
|
||||
[SCC_HINT_VERBOSE] = "增加详细输出(可多次使用)",
|
||||
|
||||
[SCC_HINT_ENTRY_POINT_SYMBOL] = "入口点符号名称",
|
||||
|
||||
[SCC_HINT_EMIT_STAGE] = "指定生成代码的阶段并退出",
|
||||
|
||||
[SCC_HINT_EMIT_LEX] = "生成`源代码的词法单元`并退出",
|
||||
[SCC_HINT_EMIT_PP] = "生成`预处理后的词法单元`并退出",
|
||||
[SCC_HINT_EMIT_AST] = "生成`抽象语法树`并退出",
|
||||
[SCC_HINT_EMIT_HIR] = "生成`高级中间代码`并退出",
|
||||
[SCC_HINT_EMIT_LIR] = "生成`低级中间代码`并退出",
|
||||
[SCC_HINT_EMIT_MIR] = "生成`机器中间代码`并退出",
|
||||
|
||||
[SCC_HINT_EMIT_FLATBIN] = "生成`flat binary`并退出",
|
||||
[SCC_HINT_EMIT_SCCF] = "生成`SCCF`并退出",
|
||||
[SCC_HINT_EMIT_TARGET] = "生成`目标代码`并退出",
|
||||
};
|
||||
|
||||
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);
|
||||
|
||||
// --entry-point-symbol (设置入口点符号名称)
|
||||
scc_argparse_opt_t opt_entry_point_symbol;
|
||||
scc_argparse_opt_init(&opt_entry_point_symbol, 0, "entry-point-symbol",
|
||||
scc_hints[SCC_HINT_ENTRY_POINT_SYMBOL]);
|
||||
scc_argparse_spec_setup_string(&opt_entry_point_symbol.spec,
|
||||
&(config->entry_point_symbol));
|
||||
scc_argparse_cmd_add_opt(root, &opt_entry_point_symbol);
|
||||
|
||||
// --target
|
||||
scc_argparse_opt_t opt_target;
|
||||
scc_argparse_opt_init(&opt_target, 0, "target",
|
||||
scc_hints[SCC_HINT_TARGET_DESC]);
|
||||
scc_argparse_spec_setup_string(&opt_target.spec,
|
||||
&(config->target_description));
|
||||
scc_argparse_cmd_add_opt(root, &opt_target);
|
||||
|
||||
// -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);
|
||||
|
||||
scc_argparse_opt_t opt_emit;
|
||||
scc_argparse_opt_init(&opt_emit, 0, "emit", scc_hints[SCC_HINT_EMIT_STAGE]);
|
||||
static const char *emit_stages[] = {
|
||||
[SCC_EMIT_STAGE_LEX] = "lex",
|
||||
[SCC_EMIT_STAGE_PP] = "pp",
|
||||
|
||||
[SCC_EMIT_STAGE_AST] = "ast", // -T
|
||||
[SCC_EMIT_STAGE_HIR] = "hir", // -H
|
||||
[SCC_EMIT_STAGE_LIR] = "lir", // -L
|
||||
[SCC_EMIT_STAGE_MIR] = "mir", // -M
|
||||
|
||||
[SCC_EMIT_STAGE_MIR_PASS_REG_ALLOC] = "",
|
||||
[SCC_EMIT_STAGE_MIR_PASS_FLAME_LAYOUT] = "",
|
||||
[SCC_EMIT_STAGE_MIR_PASS_PROLOG_EPILOG] = "",
|
||||
[SCC_EMIT_STAGE_FLATBIN] = "flatbin",
|
||||
[SCC_EMIT_STAGE_SCCF] = "sccf",
|
||||
[SCC_EMIT_STAGE_TARGET] = "target",
|
||||
};
|
||||
scc_argparse_spec_setup_choices(&opt_emit.spec, emit_stages,
|
||||
SCC_ARRLEN(emit_stages),
|
||||
(int *)&(config->emit_stage));
|
||||
scc_argparse_cmd_add_opt(root, &opt_emit);
|
||||
}
|
||||
238
src/config.h
238
src/config.h
@@ -3,6 +3,25 @@
|
||||
|
||||
#include <argparse.h>
|
||||
|
||||
typedef enum scc_emit_stage {
|
||||
SCC_EMIT_STAGE_LEX,
|
||||
SCC_EMIT_STAGE_PP,
|
||||
SCC_EMIT_STAGE_AST,
|
||||
SCC_EMIT_STAGE_HIR,
|
||||
SCC_EMIT_STAGE_LIR,
|
||||
SCC_EMIT_STAGE_MIR,
|
||||
|
||||
SCC_EMIT_STAGE_MIR_PASS_REG_ALLOC,
|
||||
SCC_EMIT_STAGE_MIR_PASS_FLAME_LAYOUT,
|
||||
SCC_EMIT_STAGE_MIR_PASS_PROLOG_EPILOG,
|
||||
|
||||
SCC_EMIT_STAGE_FLATBIN,
|
||||
SCC_EMIT_STAGE_SCCF,
|
||||
SCC_EMIT_STAGE_TARGET,
|
||||
|
||||
SCC_EMIT_STAGE_DEFAULT,
|
||||
} scc_emit_stage_t;
|
||||
|
||||
typedef struct {
|
||||
const char *input_file;
|
||||
const char *output_file;
|
||||
@@ -11,224 +30,11 @@ typedef struct {
|
||||
scc_argparse_list_t include_paths;
|
||||
scc_argparse_list_t define_macros;
|
||||
const char *entry_point_symbol;
|
||||
cbool emit_lex;
|
||||
cbool emit_pp;
|
||||
cbool emit_ast;
|
||||
cbool emit_hir;
|
||||
cbool emit_lir;
|
||||
cbool emit_mir;
|
||||
|
||||
cbool emit_flatbin;
|
||||
cbool emit_sccf;
|
||||
cbool emit_target;
|
||||
cbool emit_mir_pass_reg_alloc;
|
||||
cbool emit_mir_pass_stack_layout;
|
||||
cbool emit_mir_pass_prolog_epilog;
|
||||
scc_emit_stage_t emit_stage;
|
||||
} 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_TARGET_DESC,
|
||||
SCC_HINT_VERBOSE,
|
||||
|
||||
SCC_HINT_ENTRY_POINT_SYMBOL,
|
||||
|
||||
SCC_HINT_EMIT_LEX,
|
||||
SCC_HINT_EMIT_PP,
|
||||
SCC_HINT_EMIT_AST,
|
||||
SCC_HINT_EMIT_HIR,
|
||||
SCC_HINT_EMIT_LIR,
|
||||
SCC_HINT_EMIT_MIR,
|
||||
|
||||
SCC_HINT_EMIT_FLATBIN,
|
||||
SCC_HINT_EMIT_SCCF,
|
||||
SCC_HINT_EMIT_TARGET,
|
||||
};
|
||||
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_TARGET_DESC] =
|
||||
"Target description(eg. x86_64-pc-windows-msvc)",
|
||||
[SCC_HINT_VERBOSE] = "Increase verbosity (can be used multiple times)",
|
||||
|
||||
[SCC_HINT_ENTRY_POINT_SYMBOL] = "Entry point symbol name",
|
||||
|
||||
[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_HIR] = "Generate High-level IR and exit",
|
||||
[SCC_HINT_EMIT_LIR] = "Generate Low-level IR and exit",
|
||||
[SCC_HINT_EMIT_MIR] = "Generate Machine IR and exit",
|
||||
|
||||
[SCC_HINT_EMIT_FLATBIN] = "Generate flat binary and exit",
|
||||
[SCC_HINT_EMIT_SCCF] = "Generate SCCF and exit",
|
||||
[SCC_HINT_EMIT_TARGET] = "Generate target description 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_TARGET_DESC] = "目标机器描述(eg. x86_64-pc-windows-msvc)",
|
||||
[SCC_HINT_VERBOSE] = "增加详细输出(可多次使用)",
|
||||
|
||||
[SCC_HINT_ENTRY_POINT_SYMBOL] = "入口点符号名称",
|
||||
|
||||
[SCC_HINT_EMIT_LEX] = "生成`源代码的词法单元`并退出",
|
||||
[SCC_HINT_EMIT_PP] = "生成`预处理后的词法单元`并退出",
|
||||
[SCC_HINT_EMIT_AST] = "生成`抽象语法树`并退出",
|
||||
[SCC_HINT_EMIT_HIR] = "生成`高级中间代码`并退出",
|
||||
[SCC_HINT_EMIT_LIR] = "生成`低级中间代码`并退出",
|
||||
[SCC_HINT_EMIT_MIR] = "生成`机器中间代码`并退出",
|
||||
|
||||
[SCC_HINT_EMIT_FLATBIN] = "生成`flat binary`并退出",
|
||||
[SCC_HINT_EMIT_SCCF] = "生成`SCCF`并退出",
|
||||
[SCC_HINT_EMIT_TARGET] = "生成`目标代码`并退出",
|
||||
};
|
||||
|
||||
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);
|
||||
|
||||
// --entry-point-symbol (设置入口点符号名称)
|
||||
scc_argparse_opt_t opt_entry_point_symbol;
|
||||
scc_argparse_opt_init(&opt_entry_point_symbol, 0, "entry-point-symbol",
|
||||
scc_hints[SCC_HINT_ENTRY_POINT_SYMBOL]);
|
||||
scc_argparse_spec_setup_string(&opt_entry_point_symbol.spec,
|
||||
&(config->entry_point_symbol));
|
||||
scc_argparse_cmd_add_opt(root, &opt_entry_point_symbol);
|
||||
|
||||
// --target
|
||||
scc_argparse_opt_t opt_target;
|
||||
scc_argparse_opt_init(&opt_target, 0, "target",
|
||||
scc_hints[SCC_HINT_TARGET_DESC]);
|
||||
scc_argparse_spec_setup_string(&opt_target.spec,
|
||||
&(config->target_description));
|
||||
scc_argparse_cmd_add_opt(root, &opt_target);
|
||||
|
||||
// -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);
|
||||
|
||||
// -H, --emit-hir
|
||||
scc_argparse_opt_t opt_hir;
|
||||
scc_argparse_opt_init(&opt_hir, 'H', "emit-hir",
|
||||
scc_hints[SCC_HINT_EMIT_HIR]);
|
||||
scc_argparse_spec_setup_bool(&opt_hir.spec, &(config->emit_hir));
|
||||
scc_argparse_cmd_add_opt(root, &opt_hir);
|
||||
|
||||
// -L, --emit-lir
|
||||
scc_argparse_opt_t opt_lir;
|
||||
scc_argparse_opt_init(&opt_lir, 'L', "emit-lir",
|
||||
scc_hints[SCC_HINT_EMIT_LIR]);
|
||||
scc_argparse_spec_setup_bool(&opt_lir.spec, &(config->emit_lir));
|
||||
scc_argparse_cmd_add_opt(root, &opt_lir);
|
||||
|
||||
// -M, --emit-mir
|
||||
scc_argparse_opt_t opt_mir;
|
||||
scc_argparse_opt_init(&opt_mir, 'M', "emit-mir",
|
||||
scc_hints[SCC_HINT_EMIT_MIR]);
|
||||
scc_argparse_spec_setup_bool(&opt_mir.spec, &(config->emit_mir));
|
||||
scc_argparse_cmd_add_opt(root, &opt_mir);
|
||||
|
||||
// --emit-flatbin
|
||||
scc_argparse_opt_t opt_flatbin;
|
||||
scc_argparse_opt_init(&opt_flatbin, 0, "emit-flatbin",
|
||||
scc_hints[SCC_HINT_EMIT_FLATBIN]);
|
||||
scc_argparse_spec_setup_bool(&opt_flatbin.spec, &(config->emit_flatbin));
|
||||
scc_argparse_cmd_add_opt(root, &opt_flatbin);
|
||||
// --emit-sccf
|
||||
scc_argparse_opt_t opt_sccf;
|
||||
scc_argparse_opt_init(&opt_sccf, 0, "emit-sccf",
|
||||
scc_hints[SCC_HINT_EMIT_SCCF]);
|
||||
scc_argparse_spec_setup_bool(&opt_sccf.spec, &(config->emit_sccf));
|
||||
scc_argparse_cmd_add_opt(root, &opt_sccf);
|
||||
// --emit-target
|
||||
scc_argparse_opt_t opt_emit_target;
|
||||
scc_argparse_opt_init(&opt_emit_target, 0, "emit-target",
|
||||
scc_hints[SCC_HINT_EMIT_TARGET]);
|
||||
scc_argparse_spec_setup_bool(&opt_emit_target.spec, &(config->emit_target));
|
||||
scc_argparse_cmd_add_opt(root, &opt_emit_target);
|
||||
}
|
||||
void setup_argparse(scc_argparse_t *argparse, scc_config_t *config,
|
||||
scc_argparse_lang_t lang);
|
||||
|
||||
#endif /* __SCC_CONFIG_H___ */
|
||||
|
||||
48
src/main.c
48
src/main.c
@@ -89,8 +89,7 @@ int main(int argc, const char **argv, const char **envp) {
|
||||
.verbose = 0,
|
||||
.output_file = nullptr,
|
||||
.entry_point_symbol = nullptr,
|
||||
.emit_ast = false,
|
||||
.emit_hir = false,
|
||||
.emit_stage = SCC_EMIT_STAGE_DEFAULT,
|
||||
.target_description = "x86_64-pc-windows-msvc",
|
||||
};
|
||||
scc_vec_init(config.include_paths);
|
||||
@@ -129,7 +128,7 @@ int main(int argc, const char **argv, const char **envp) {
|
||||
|
||||
scc_lexer_t lexer;
|
||||
scc_lexer_init(&lexer, scc_sstream_to_ring(&sstream));
|
||||
if (config.emit_lex) {
|
||||
if (config.emit_stage == SCC_EMIT_STAGE_LEX) {
|
||||
scc_lexer_tok_ring_t *tok_ring =
|
||||
scc_lexer_to_ring(&lexer, 8, fp == nullptr ? false : true);
|
||||
if (fp == nullptr) {
|
||||
@@ -173,7 +172,7 @@ int main(int argc, const char **argv, const char **envp) {
|
||||
scc_pproc_add_object_macro(&pproc.macro_table,
|
||||
&pproc_predefined_macros[i], &pproc_tok_vec);
|
||||
}
|
||||
if (config.emit_pp) {
|
||||
if (config.emit_stage == SCC_EMIT_STAGE_PP) {
|
||||
scc_lexer_tok_ring_t *tok_ring =
|
||||
scc_pproc_to_ring(&pproc, 8, true, true);
|
||||
if (fp == nullptr) {
|
||||
@@ -211,7 +210,7 @@ sstream_drop:
|
||||
return error_code;
|
||||
}
|
||||
|
||||
if (config.emit_ast) {
|
||||
if (config.emit_stage == SCC_EMIT_STAGE_AST) {
|
||||
scc_tree_dump_t tree_dump;
|
||||
if (fp == nullptr) {
|
||||
scc_tree_dump_init(&tree_dump, true);
|
||||
@@ -232,7 +231,7 @@ sstream_drop:
|
||||
scc_ast2ir_run(&ast2ir_ctx, translation_unit);
|
||||
scc_ast2ir_ctx_drop(&ast2ir_ctx);
|
||||
|
||||
if (config.emit_hir) {
|
||||
if (config.emit_stage == SCC_EMIT_STAGE_HIR) {
|
||||
scc_hir_dump_t ir_dump_ctx;
|
||||
scc_tree_dump_t tree_dump;
|
||||
if (fp == nullptr) {
|
||||
@@ -252,7 +251,7 @@ sstream_drop:
|
||||
scc_lir_module_t lir_module;
|
||||
scc_lir_module_init(&lir_module);
|
||||
scc_hir2lir(&lir_module, &cprog);
|
||||
if (config.emit_lir) {
|
||||
if (config.emit_stage == SCC_EMIT_STAGE_LIR) {
|
||||
scc_lir_dump_ctx_t lir_dump_ctx;
|
||||
scc_tree_dump_t tree_dump;
|
||||
if (fp == nullptr) {
|
||||
@@ -272,7 +271,7 @@ sstream_drop:
|
||||
scc_mir_module_t mir_module;
|
||||
scc_mir_module_init(&mir_module);
|
||||
scc_lir2mir(&mir_module, &lir_module);
|
||||
if (config.emit_mir) {
|
||||
if (config.emit_stage == SCC_EMIT_STAGE_MIR) {
|
||||
scc_mir_dump_ctx_t mir_dump_ctx;
|
||||
scc_tree_dump_t tree_dump;
|
||||
if (fp == nullptr) {
|
||||
@@ -288,7 +287,7 @@ sstream_drop:
|
||||
return 0;
|
||||
}
|
||||
|
||||
if (config.emit_flatbin) {
|
||||
if (config.emit_stage == SCC_EMIT_STAGE_FLATBIN) {
|
||||
scc_mcode_t mcode = {0};
|
||||
scc_mcode_init(&mcode, SCC_MCODE_ARCH_X86_64);
|
||||
scc_ir2mcode(&mcode, &mir_module);
|
||||
@@ -311,18 +310,31 @@ sstream_drop:
|
||||
scc_ir2sccf(&sccf_builder, &mir_module);
|
||||
sccf_builder_set_entry_symbol_name(&sccf_builder,
|
||||
config.entry_point_symbol);
|
||||
if (config.emit_sccf) {
|
||||
const sccf_t *sccf = sccf_builder_to_sccf(&sccf_builder);
|
||||
if (config.emit_stage == SCC_EMIT_STAGE_SCCF) {
|
||||
sccf_buffer_t buffer;
|
||||
scc_vec_init(buffer);
|
||||
sccf_builder_to_buffer(&sccf_builder, &buffer);
|
||||
if (fp == nullptr) {
|
||||
scc_printf("output exe at %s\n", config.output_file);
|
||||
} else {
|
||||
sccf_builder_to_file(&sccf_builder, config.output_file);
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
const sccf_t *sccf = sccf_builder_to_sccf(&sccf_builder);
|
||||
scc_pe_builder_t pe_builder;
|
||||
sccf2pe(&pe_builder, sccf);
|
||||
if (fp == nullptr) {
|
||||
scc_printf("output exe at %s\n", config.output_file);
|
||||
} else {
|
||||
scc_pe_dump_to_file(&pe_builder, config.output_file);
|
||||
if (config.emit_stage == SCC_EMIT_STAGE_DEFAULT ||
|
||||
config.emit_stage == SCC_EMIT_STAGE_TARGET) {
|
||||
scc_pe_builder_t pe_builder;
|
||||
sccf2pe(&pe_builder, sccf);
|
||||
if (fp == nullptr) {
|
||||
scc_printf("output exe at %s\n", config.output_file);
|
||||
} else {
|
||||
scc_pe_dump_to_file(&pe_builder, config.output_file);
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
return 0;
|
||||
Panic("unknown emit stage");
|
||||
return 1;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user