-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.c
More file actions
executable file
·28 lines (23 loc) · 738 Bytes
/
main.c
File metadata and controls
executable file
·28 lines (23 loc) · 738 Bytes
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
#include "y3c.h"
static int align_to(int n, int align) {
return (n + align - 1) / align * align;
}
int main(int argc, char **argv) {
if (argc != 2)
error("%s: invalid number of arguments.", argv[0]);
// Tokenize and parse.
Token *tok = tokenize(argv[1]);
Function *prog = parse(tok);
// Assign offsets to local variables.
for (Function *fn = prog; fn; fn = fn->next) {
int offset = 32; // 32 for callee-saved registers
for (Var *var = fn->locals; var; var = var->next) {
offset += var->ty->size;
var->offset = offset;
}
fn->stack_size = align_to(offset, 16);
}
// Traverse the AST to emit assembly.
codegen(prog);
return 0;
}