-
Notifications
You must be signed in to change notification settings - Fork 0
/
zcc.c
133 lines (106 loc) · 2.84 KB
/
zcc.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
130
131
132
133
/***
* ZCC - a study purpose compiler for C programming language
* By keren. naeioi@hotmail.com
*/
#include "zcc.h"
#include <stdio.h>
#include <string.h>
#include <assert.h>
#include <stdlib.h>
/* =---- Others ----= */
void init(int argc, char **argv) {
lex_init();
}
void test_lex(const char *filename) {
lex_load_file(filename);
while(1) {
int n = lex_next();
if(n != 0) return;
lex_print_token(&lex_token);
}
}
void test_binary(char *filename) {
lex_load_file(filename);
lex_next();
prs_init();
prs_expr();
}
void test_stmt(char *filename) {
lex_load_file(filename);
lex_next();
prs_init();
prs_stmt();
}
void test_decl(char *filename) {
lex_load_file(filename);
lex_next();
prs_init();
prs_decl();
}
void test_decls(char *filename) {
lex_load_file(filename);
lex_next();
prs_init();
prs_decls();
}
void test_ir(char *filename) {
lex_load_file(filename);
lex_next();
prs_init();
sym_init();
prs_decls();
fprintf(stderr, "Parse done.\n");
list_st *funcs = context.funcs;
/* start from 1 to skip wrap function */
int i = 1;
fprintf(stderr, "Read %d funcs.\n", funcs->len);
for(; i < funcs->len; i++) {
printf("\n");
gen_print_func_ir(funcs->elems[i]);
}
}
void test_asm(char *filename) {
lex_load_file(filename);
lex_next();
prs_init();
sym_init();
prs_decls();
fprintf(stderr, "Parse done.\n");
list_st *funcs = context.funcs;
fprintf(stderr, "Read %d funcs.\n", funcs->len);
gen_assembly(stdout);
}
void to_asm(char *infile, char *outfile) {
lex_load_file(infile);
lex_next();
prs_init();
sym_init();
//fprintf(stderr, "sym_init\n");
prs_decls();
fprintf(stderr, "Parse done.\n");
list_st *funcs = context.funcs;
fprintf(stderr, "Read %d funcs.\n", funcs->len);
FILE *fp = fopen(outfile, "w");
gen_assembly(fp);
}
int main(int argc, char **argv)
{
init(argc, argv);
if(argc == 3 && strcmp("-l", argv[1]) == 0)
test_lex(argv[2]);
if(argc == 3 && strcmp("-b", argv[1]) == 0)
test_binary(argv[2]);
if(argc == 3 && strcmp("-s", argv[1]) == 0)
test_stmt(argv[2]);
if(argc == 3 && strcmp("-d", argv[1]) == 0)
test_decl(argv[2]);
if(argc == 3 && strcmp("-ds", argv[1]) == 0)
test_decls(argv[2]);
if(argc == 3 && strcmp("-ir", argv[1]) == 0)
test_ir(argv[2]);
if(argc == 3 && strcmp("-a" , argv[1]) == 0)
test_asm(argv[2]);
if(argc == 5 && strcmp("-a" , argv[1]) == 0 && strcmp("-o", argv[3]) == 0) {
to_asm(argv[2], argv[4]);
}
}