-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.c
77 lines (64 loc) · 1.59 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
#include "9cc.h"
int pos = 0;
Node *code[100];
LVar *locals;
unsigned int jump_count = 0;
int check(int line, int expected, int actual)
{
if (expected == actual)
return 0;
fprintf(stderr, "%d: %d expected, but got %d\n", line, expected, actual);
exit(1);
}
void runtest()
{
Vector *vec = new_vector();
check(__LINE__, 0, vec->len);
for (int i = 0; i < 100; i++)
vec_push(vec, (void *)i);
check(__LINE__, 100, vec->len);
check(__LINE__, 0, (int)vec->data[0]);
check(__LINE__, 50, (int)vec->data[50]);
check(__LINE__, 99, (int)vec->data[99]);
printf("OK\n");
}
int main(int argc, char **argv)
{
if (argc != 2)
{
fprintf(stderr, "Incorrect number of arguments\n");
return 1;
}
if (strcmp(argv[1], "-test") == 0)
{
runtest();
return 0;
}
Vector *tokens = new_vector(); // Initialize a token vector
LVar *lvar = malloc(sizeof(LVar)); // Initialize locals
lvar->offset = 0;
lvar->name = "INIT";
locals = lvar;
// Tokenize input and parse
tokenize(argv[1], tokens);
program(tokens);
// Output the first part of assembly
printf(".intel_syntax noprefix\n");
printf(".global main\n");
printf("main:\n");
// Prolog
// Get enough space for 26 variables
printf(" push rbp\n");
printf(" mov rbp, rsp\n");
printf(" sub rsp, 208\n");
for (int i = 0; code[i]; i++)
{
gen(code[i]);
printf(" pop rax\n");
}
// Epilog
printf(" mov rsp, rbp\n");
printf(" pop rbp\n");
printf(" ret\n");
return 0;
}