#include "chibicc.h" typedef enum { FILE_NONE, FILE_C, FILE_ASM, FILE_OBJ, FILE_AR, FILE_DSO, } FileType; char *base_file; StringArray include_paths; bool opt_fcommon = false; bool opt_fpic; bool opt_line_numbers; bool opt_type_builtins; static FILE *open_file(char *path) { if (!path || strcmp(path, "-") == 0) return stdout; FILE *out = fopen(path, "w"); if (!out) error("cannot open output file: %s: %s", path, strerror(errno)); return out; } static Token *must_tokenize_file(char *path) { Token *tok = tokenize_file(path); if (!tok) error("%s: %s", path, strerror(errno)); return tok; } static Token *append_tokens(Token *tok1, Token *tok2) { if (!tok1 || tok1->kind == TK_EOF) return tok2; Token *t = tok1; while (t->next->kind != TK_EOF) t = t->next; t->next = tok2; return tok1; } static void cc1(char *base_file) { Token *tok = NULL; // Tokenize and parse. Token *tok2 = must_tokenize_file(base_file); tok = append_tokens(tok, tok2); tok = preprocess(tok); Obj *prog = parse(tok); // Open a temporary output buffer. char *buf; size_t buflen; FILE *output_buf = open_memstream(&buf, &buflen); // Traverse the AST to emit assembly. codegen(prog, output_buf); fclose(output_buf); // Write the asembly text to a file. FILE *out = open_file("/dev/stdout"); fwrite(buf, buflen, 1, out); fclose(out); } int main(int argc, char **argv) { for (int i = 1; i < argc; i++) { if (!strcmp(argv[i], "--line-numbers")) { opt_line_numbers = 1; } else if (!strcmp(argv[i], "--type-builtins")) { opt_type_builtins = 1; } else { assert(!base_file); base_file = argv[i]; } } cc1(base_file); return 0; }