-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.c
88 lines (67 loc) · 1.78 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
#include "alloycc.h"
bool opt_E;
char **include_paths;
static char *input_file;
static void usage(void) {
fprintf(stderr, "alloycc [ -I<path> ] <file>\n");
exit(1);
}
static void parse_args(int argc, char **argv) {
include_paths = malloc(sizeof(char *) * argc);
int npaths = 0;
for (int i = 1; i < argc; i++) {
if (!strcmp(argv[i], "--help"))
usage();
if (!strncmp(argv[i], "-I", 2)) {
include_paths[npaths++] = argv[i] + 2;
continue;
}
if (!strcmp(argv[i], "-E")) {
opt_E = true;
continue;
}
if (argv[i][0] == '-' && argv[i][1] != '\0')
error("unknown argument: %s", argv[i]);
input_file = argv[i];
}
include_paths[npaths] = NULL;
if (!input_file)
error("no input files");
}
static void print_tokens(Token *tok) {
int line = 1;
for (; tok->kind != TK_EOF; tok = tok->next) {
if (line > 1 && tok->at_bol)
printf("\n");
if (tok->has_space && !tok->at_bol)
printf(" ");
printf("%.*s", tok->len, tok->str);
line++;
}
printf("\n");
}
int main(int argc, char **argv) {
parse_args(argc, argv);
Token *tok = tokenize_file(input_file);
if (!tok)
error("%s: %s", input_file, strerror(errno));
tok = preprocess(tok);
if (opt_E) {
print_tokens(tok);
exit(0);
}
Program *prog = parse(tok);
for (Function *fn = prog->fns; fn; fn = fn->next) {
// first 32 bytes are reserved for callee saved resigisters
// additional 96 bytes can be used for variadic vars (if requried)
int offset = fn->is_variadic? 128 : 32;
for (Var *var = fn->locals; var; var = var->next) {
offset = align_to(offset, var->align);
offset += size_of(var->ty);
var->offset = offset;
}
fn->stack_size = align_to(offset, 16);
}
codegen(prog);
return 0;
}