- 在argparse库中将所有null指针常量替换为nullptr - 更新头文件和源文件中的指针初始化和比较操作 - 修改测试文件中的相关断言检查 - 更新AST定义文件中的注释说明
258 lines
7.8 KiB
C
258 lines
7.8 KiB
C
#include <argparse.h>
|
|
#include <scc_lexer.h>
|
|
#include <scc_parser.h>
|
|
#include <scc_pproc.h>
|
|
|
|
#include <ast_dump.h>
|
|
#include <ir_dump.h>
|
|
#include <scc_ast2ir.h>
|
|
#include <scc_ir2mcode.h>
|
|
#include <sccf2pe.h>
|
|
|
|
#include "config.h"
|
|
|
|
static void print_ring(scc_lexer_tok_ring_t *ring, int verbose) {
|
|
scc_lexer_tok_t tok = {0};
|
|
int ret = 0;
|
|
while (1) {
|
|
scc_ring_next_consume(*ring, tok, ret);
|
|
if (ret == false || tok.type == SCC_TOK_EOF) {
|
|
break;
|
|
}
|
|
if (verbose == 0) {
|
|
scc_printf("%s ", scc_get_tok_name(tok.type));
|
|
} else if (verbose >= 1) {
|
|
scc_printf(
|
|
"token [%-8s] `%s` at %s:%d:%d\n", scc_get_tok_name(tok.type),
|
|
tok.type != SCC_TOK_ENDLINE ? scc_str_as_cstr(&tok.lexeme)
|
|
: "\\n",
|
|
tok.loc.name, tok.loc.line, tok.loc.col);
|
|
}
|
|
scc_lexer_tok_drop(&tok);
|
|
}
|
|
}
|
|
|
|
static void print_file(scc_lexer_tok_ring_t *ring, scc_file_t fp) {
|
|
scc_lexer_tok_t tok = {0};
|
|
int ret = 0;
|
|
|
|
while (1) {
|
|
scc_ring_next_consume(*ring, tok, ret);
|
|
if (ret == false || tok.type == SCC_TOK_EOF) {
|
|
break;
|
|
}
|
|
if (fp == scc_stdout) {
|
|
scc_printf("%s", scc_str_as_cstr(&tok.lexeme));
|
|
} else {
|
|
usize ret = scc_fwrite(fp, scc_str_as_cstr(&tok.lexeme),
|
|
scc_str_len(&tok.lexeme));
|
|
if (ret != scc_str_len(&tok.lexeme)) {
|
|
LOG_FATAL("Failed to write to file");
|
|
}
|
|
}
|
|
scc_lexer_tok_drop(&tok);
|
|
}
|
|
scc_fclose(fp);
|
|
}
|
|
|
|
static void tree_dump_output(const char *str, usize len, void *user) {
|
|
scc_fprintf(user, "%.*s", (int)len, str);
|
|
}
|
|
|
|
void init_platform(void);
|
|
|
|
int main(int argc, const char **argv, const char **envp) {
|
|
init_platform();
|
|
|
|
#ifndef SCC_DEFAULT_ARGPARSE_LANG
|
|
#define SCC_DEFAULT_ARGPARSE_LANG SCC_ARGPARSE_LANG_ZH
|
|
#endif
|
|
|
|
scc_argparse_lang_t argparse_lang = SCC_DEFAULT_ARGPARSE_LANG;
|
|
for (const char **env = envp; *env != nullptr; env++) {
|
|
const char *env_str = *env;
|
|
if (scc_strcmp(env_str, "LANG=zh_CN.UTF-8") == 0) {
|
|
argparse_lang = SCC_ARGPARSE_LANG_ZH;
|
|
}
|
|
}
|
|
|
|
scc_config_t config = {
|
|
.input_file = nullptr,
|
|
.verbose = 0,
|
|
.output_file = nullptr,
|
|
.emit_ast = false,
|
|
.emit_ir = false,
|
|
.target_description = "x86_64-pc-windows-msvc",
|
|
};
|
|
scc_vec_init(config.include_paths);
|
|
|
|
scc_argparse_t argparse;
|
|
setup_argparse(&argparse, &config, argparse_lang);
|
|
int ret = scc_argparse_parse(&argparse, argc, argv);
|
|
if (ret != 0) {
|
|
scc_argparse_drop(&argparse);
|
|
return 0;
|
|
}
|
|
scc_argparse_drop(&argparse);
|
|
|
|
scc_file_t fp = nullptr;
|
|
if (config.output_file) {
|
|
cbool is_stdout = scc_strcmp(config.output_file, "-") == 0;
|
|
if (!is_stdout) {
|
|
fp = scc_fopen(config.output_file, SCC_FILE_WRITE);
|
|
if (fp == nullptr) {
|
|
LOG_FATAL("Failed to open file %s", config.output_file);
|
|
return 1;
|
|
}
|
|
} else {
|
|
fp = scc_stdout;
|
|
}
|
|
}
|
|
|
|
int error_code = 0;
|
|
cbool need_end = false;
|
|
scc_sstream_t sstream;
|
|
error_code = scc_sstream_init(&sstream, config.input_file, 1024);
|
|
if (error_code) {
|
|
goto sstream_drop;
|
|
need_end = true;
|
|
}
|
|
|
|
scc_lexer_t lexer;
|
|
scc_lexer_init(&lexer, scc_sstream_to_ring(&sstream));
|
|
if (config.emit_lex) {
|
|
scc_lexer_tok_ring_t *tok_ring =
|
|
scc_lexer_to_ring(&lexer, 8, fp == nullptr ? false : true);
|
|
if (fp == nullptr) {
|
|
print_ring(tok_ring, config.verbose);
|
|
} else {
|
|
print_file(tok_ring, fp);
|
|
}
|
|
need_end = true;
|
|
goto lexer_drop;
|
|
}
|
|
|
|
scc_pproc_t pproc;
|
|
scc_pproc_init(&pproc, scc_lexer_to_ring(&lexer, 8, true));
|
|
// FIXME maybe using config to got download path and add include path
|
|
scc_pproc_add_include_path_cstr(&pproc, "./.scc_include");
|
|
scc_vec_foreach(config.include_paths, i) {
|
|
scc_pproc_add_include_path_cstr(&pproc,
|
|
scc_vec_at(config.include_paths, i));
|
|
}
|
|
scc_lexer_tok_vec_t pproc_tok_vec;
|
|
scc_vec_init(pproc_tok_vec);
|
|
scc_lexer_tok_t tok = {
|
|
.lexeme = scc_str_from_cstr("1"),
|
|
.type = SCC_TOK_INT_LITERAL,
|
|
.loc.name = "<internal>",
|
|
.loc.line = 0,
|
|
.loc.col = 0,
|
|
.loc.offset = 0,
|
|
};
|
|
scc_vec_push(pproc_tok_vec, tok);
|
|
scc_str_t pproc_predefined_macros[] = {
|
|
scc_str_from_cstr("__SCC__"),
|
|
scc_str_from_cstr("_WIN64"),
|
|
scc_str_from_cstr("__x86_64__"),
|
|
};
|
|
for (usize i = 0; i < SCC_ARRLEN(pproc_predefined_macros); i += 1) {
|
|
scc_vec_init(pproc_tok_vec);
|
|
scc_lexer_tok_t coped_tok = scc_lexer_tok_copy(&tok);
|
|
scc_vec_push(pproc_tok_vec, coped_tok);
|
|
scc_pproc_add_object_macro(&pproc.macro_table,
|
|
&pproc_predefined_macros[i], &pproc_tok_vec);
|
|
}
|
|
if (config.emit_pp) {
|
|
scc_lexer_tok_ring_t *tok_ring =
|
|
scc_pproc_to_ring(&pproc, 8, true, true);
|
|
if (fp == nullptr) {
|
|
print_ring(tok_ring, config.verbose);
|
|
} else {
|
|
print_file(tok_ring, fp);
|
|
}
|
|
need_end = true;
|
|
goto pproc_drop;
|
|
}
|
|
|
|
scc_lexer_tok_ring_t *tok_ring =
|
|
scc_pproc_to_ring(&pproc, 1024, false, false);
|
|
scc_parser_t parser;
|
|
scc_sema_callbacks_t sema_callbacks;
|
|
scc_sema_init(&sema_callbacks);
|
|
scc_parser_init(&parser, tok_ring, &sema_callbacks);
|
|
scc_ast_translation_unit_t *translation_unit =
|
|
scc_parse_translation_unit(&parser);
|
|
|
|
scc_sema_drop(&sema_callbacks);
|
|
scc_parser_drop(&parser);
|
|
pproc_drop:
|
|
scc_pproc_drop(&pproc);
|
|
lexer_drop:
|
|
scc_lexer_drop(&lexer);
|
|
sstream_drop:
|
|
scc_sstream_drop(&sstream);
|
|
if (error_code || need_end) {
|
|
return error_code;
|
|
}
|
|
|
|
if (config.emit_ast) {
|
|
scc_tree_dump_t tree_dump;
|
|
if (fp == nullptr) {
|
|
scc_tree_dump_init(&tree_dump, true);
|
|
} else {
|
|
scc_tree_dump_init(&tree_dump, false);
|
|
}
|
|
scc_ast_dump_node(&tree_dump, (scc_ast_node_t *)translation_unit);
|
|
scc_tree_dump_flush(&tree_dump, tree_dump_output,
|
|
fp == nullptr ? scc_stdout : fp);
|
|
scc_tree_dump_drop(&tree_dump);
|
|
return 0;
|
|
}
|
|
|
|
scc_ast2ir_ctx_t ast2ir_ctx;
|
|
#include <abi/win_x64_type_abi.h>
|
|
scc_ir_cprog_t cprog;
|
|
scc_ir_cprog_init(&cprog);
|
|
scc_ast2ir_ctx_init(&ast2ir_ctx, scc_win_x64_type_abi, &cprog);
|
|
scc_ast2ir_translation_unit(&ast2ir_ctx, translation_unit);
|
|
scc_ast2ir_ctx_drop(&ast2ir_ctx);
|
|
|
|
if (config.emit_ir) {
|
|
scc_ir_dump_ctx_t ir_dump_ctx;
|
|
scc_tree_dump_t tree_dump;
|
|
if (fp == nullptr) {
|
|
scc_tree_dump_init(&tree_dump, true);
|
|
} else {
|
|
scc_tree_dump_init(&tree_dump, false);
|
|
}
|
|
scc_ir_dump_ctx_init(&ir_dump_ctx, &tree_dump, &cprog);
|
|
// scc_ir_dump_cprog(&ir_dump_ctx);
|
|
scc_ir_dump_cprog_linear(&ir_dump_ctx);
|
|
|
|
scc_tree_dump_flush(&tree_dump, tree_dump_output,
|
|
fp == nullptr ? scc_stdout : fp);
|
|
scc_tree_dump_drop(&tree_dump);
|
|
return 0;
|
|
}
|
|
|
|
scc_ir2mcode_ctx_t ir2mcode_ctx;
|
|
sccf_builder_t sccf_builder;
|
|
scc_ir2mcode_init(&ir2mcode_ctx, &cprog, &sccf_builder,
|
|
SCC_MCODE_ARCH_AMD64);
|
|
scc_ir2mcode(&ir2mcode_ctx);
|
|
scc_ir2mcode_drop(&ir2mcode_ctx);
|
|
|
|
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);
|
|
}
|
|
|
|
return 0;
|
|
}
|