-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path9cc.c
48 lines (37 loc) · 977 Bytes
/
9cc.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
#include "9cc.h"
int main(int argc, char **argv) {
if (argc != 2) {
fprintf(stderr, "引数の個数が正しくありません\n");
return 1;
}
user_input = argv[1];
// tokenize
tokenize();
// token to AST
program();
printf(".intel_syntax noprefix\n");
{ // for the definition of global functions
printf(".globl ");
for (int i = 0; code[i]; i++) {
if (i > 0) printf(", ");
printf("%.*s", code[i]->len, code[i]->name);
}
printf("\n\n");
}
for (int i = 0; code[i]; i++) {
printf("%.*s:\n", code[i]->len, code[i]->name);
// prologue
// for variable
printf(" push rbp\n");
printf(" mov rbp, rsp\n");
printf(" sub rsp, %d\n", locals[0]->offset);
gen(code[i]);
printf(" pop rax\n"); // set on rax
// epilogue
// rax is still valid without dealing with it.
printf(" mov rsp, rbp\n");
printf(" pop rbp\n");
printf(" ret\n\n");
}
return 0;
}