summaryrefslogtreecommitdiff
path: root/main.c
diff options
context:
space:
mode:
Diffstat (limited to 'main.c')
-rw-r--r--main.c72
1 files changed, 72 insertions, 0 deletions
diff --git a/main.c b/main.c
new file mode 100644
index 0000000..af10c1d
--- /dev/null
+++ b/main.c
@@ -0,0 +1,72 @@
+#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;
+
+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.
+ typegen(prog, output_buf);
+ 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) {
+ assert(argc == 2);
+ base_file = argv[1];
+ cc1(argv[1]);
+ return 0;
+}
generated by cgit on debian on lair
contact matthew@masot.net with questions or feedback