forked from TuringKi/chibicc-riscv64
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.c
129 lines (102 loc) · 2.5 KB
/
main.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
#include "chibicc.h"
char *base_file;
StringArray include_paths;
static char *opt_o;
static bool opt_E;
static char *input_path;
static void usage(int status) {
fprintf(stderr, "chibicc [ -o <path> ] <file>\n");
exit(status);
}
static bool take_arg(char *arg) {
char *x[] = {"-o", "-I"};
for (int i = 0; i < sizeof(x) / sizeof(*x); i++) {
if (!strcmp(arg, x[i])) {
return true;
}
}
return false;
}
static void add_default_include_paths(char *argv0) {
strarray_push(&include_paths, format("%s/include", dirname(strdup(argv0))));
strarray_push(&include_paths, "/usr/local/include");
strarray_push(&include_paths, "/usr/include");
}
static void parse_args(int argc, char **argv) {
for (int i = 1; i < argc; i++) {
if (take_arg(argv[i])) {
if (!argv[++i]) {
usage(1);
}
}
}
for (int i = 1; i < argc; i++) {
if (!strcmp(argv[i], "--help"))
usage(0);
if (!strcmp(argv[i], "-o")) {
if (!argv[++i])
usage(1);
opt_o = argv[i];
continue;
}
if (!strncmp(argv[i], "-o", 2)) {
opt_o = argv[i] + 2;
continue;
}
if (!strcmp(argv[i], "-E")) {
opt_E = true;
continue;
}
if (!strncmp(argv[i], "-I", 2)) {
strarray_push(&include_paths, argv[i] + 2);
continue;
}
if (argv[i][0] == '-' && argv[i][1] != '\0')
error("unknown argument: %s", argv[i]);
input_path = argv[i];
}
if (!input_path)
error("no input files");
}
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 void print_tokens(Token *tok) {
FILE *out = open_file(opt_o ? opt_o : "-");
int line = 1;
for (; tok->kind != TK_EOF; tok = tok->next) {
if (line > 1 && tok->at_bol) {
fprintf(out, "\n");
}
if (tok->has_space && !tok->at_bol) {
fprintf(out, " ");
}
fprintf(out, "%.*s", tok->len, tok->loc);
line++;
}
fprintf(out, "\n");
}
int main(int argc, char **argv) {
parse_args(argc, argv);
add_default_include_paths(argv[0]);
// Tokenize and parse.
Token *tok = tokenize_file(input_path);
if (!tok) {
error("%s: %s", base_file, strerror(errno));
}
tok = preprocess(tok);
if (opt_E) {
print_tokens(tok);
return 0;
}
Obj *prog = parse(tok);
// Traverse the AST to emit assembly.
FILE *out = open_file(opt_o);
codegen(prog, out);
return 0;
}