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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
|
int atoi();
void* calloc();
void exit();
int getchar();
int isalnum();
int isalpha();
int isdigit();
int isspace();
void* malloc();
void* memcpy();
void* memset();
int printf();
int putchar();
int sprintf();
int strcmp();
char* strstr();
long strtol();
int fatal_error(char* msg) {
printf("%s", msg);
exit(1);
return 0;
}
int read_all(char* buf) {
int c;
int n = 0;
while ((c = getchar()) != -1) {
buf[n] = c;
n += 1;
}
return n;
}
#define TK_EOF 0
#define TK_K_BREAK 1
#define TK_K_CHAR 2
#define TK_K_CONTINUE 3
#define TK_K_ELSE 4
#define TK_K_FOR 5
#define TK_K_IF 6
#define TK_K_INT 7
#define TK_K_LONG 8
#define TK_K_RETURN 9
#define TK_K_SIZEOF 10
#define TK_K_STRUCT 11
#define TK_K_VOID 12
#define TK_IDENT 13
#define TK_L_INT 14
#define TK_L_STR 15 // TODO
#define TK_PAREN_L 16
#define TK_PAREN_R 17
#define TK_BRACE_L 18
#define TK_BRACE_R 19
#define TK_BRACKET_L 20
#define TK_BRACKET_R 21
#define TK_SEMICOLON 22
typedef struct Token {
int kind;
char* value;
int len;
} TOKEN;
TOKEN* tokenize(char* src, int len) {
TOKEN* tokens = calloc(1024*1024, sizeof(TOKEN));
TOKEN* tok = tokens;
int pos = 0;
while (pos < len) {
char c = src[pos];
if (c == '(') {
pos += 1;
tok->kind = TK_PAREN_L;
tok->value = src + pos - 1;
tok->len = 1;
tok += 1;
} else if (c == ')') {
pos += 1;
tok->kind = TK_PAREN_R;
tok->value = src + pos - 1;
tok->len = 1;
tok += 1;
} else if (c == '{') {
pos += 1;
tok->kind = TK_BRACE_L;
tok->value = src + pos - 1;
tok->len = 1;
tok += 1;
} else if (c == '}') {
pos += 1;
tok->kind = TK_BRACE_R;
tok->value = src + pos - 1;
tok->len = 1;
tok += 1;
} else if (c == '[') {
pos += 1;
tok->kind = TK_BRACKET_L;
tok->value = src + pos - 1;
tok->len = 1;
tok += 1;
} else if (c == ']') {
pos += 1;
tok->kind = TK_BRACKET_R;
tok->value = src + pos - 1;
tok->len = 1;
tok += 1;
} else if (c == ';') {
pos += 1;
tok->kind = TK_SEMICOLON;
tok->value = src + pos - 1;
tok->len = 1;
tok += 1;
} else if (isdigit(c)) {
int start = pos;
while (isdigit(src[pos])) {
pos += 1;
}
tok->kind = TK_L_INT;
tok->value = src + start;
tok->len = pos - start;
tok += 1;
} else if (isalpha(c)) {
int start = pos;
while (isalnum(src[pos])) {
pos += 1;
}
int kind;
if (strstr(src + start, "break") == src + start) {
kind = TK_K_BREAK;
} else if (strstr(src + start, "char") == src + start) {
kind = TK_K_CHAR;
} else if (strstr(src + start, "continue") == src + start) {
kind = TK_K_CONTINUE;
} else if (strstr(src + start, "else") == src + start) {
kind = TK_K_ELSE;
} else if (strstr(src + start, "for") == src + start) {
kind = TK_K_FOR;
} else if (strstr(src + start, "if") == src + start) {
kind = TK_K_IF;
} else if (strstr(src + start, "int") == src + start) {
kind = TK_K_INT;
} else if (strstr(src + start, "long") == src + start) {
kind = TK_K_LONG;
} else if (strstr(src + start, "return") == src + start) {
kind = TK_K_RETURN;
} else if (strstr(src + start, "sizeof") == src + start) {
kind = TK_K_SIZEOF;
} else if (strstr(src + start, "struct") == src + start) {
kind = TK_K_STRUCT;
} else {
kind = TK_IDENT;
}
tok->kind = kind;
tok->value = src + start;
tok->len = pos - start;
tok += 1;
} else if (isspace(c)) {
pos += 1;
} else {
fatal_error("unknown token");
}
}
return tokens;
}
#define AST_UNKNOWN 0
#define AST_PROGRAM 1
#define AST_FUNC_DECL 2
#define AST_FUNC_DEF 3
#define AST_TYPE 4
#define AST_BLOCK 5
#define AST_RETURN_STMT 6
#define AST_INT_LIT_EXPR 7
typedef struct AstNode {
int kind;
struct AstNode* next;
struct AstNode* last;
TOKEN* func_name;
struct AstNode* func_body;
struct AstNode* expr1;
int int_value;
} AST;
typedef struct Parser {
TOKEN* tokens;
int pos;
} PARSER;
PARSER* parser_new(TOKEN* tokens) {
PARSER* p = calloc(1, sizeof(PARSER));
p->tokens = tokens;
return p;
}
TOKEN* current_token(PARSER* p) {
return p->tokens + p->pos;
}
void next(PARSER* p) {
p->pos += 1;
}
int eof(PARSER* p) {
return current_token(p)->kind != TK_EOF;
}
TOKEN* expect(PARSER*p, int expected) {
TOKEN* t = current_token(p);
if (t->kind == expected) {
next(p);
return p->tokens + p->pos - 1;
} else {
char buf[1024];
sprintf(buf, "expected %d, but got %d", expected, t->kind);
fatal_error(buf);
}
}
AST* parse_expr(PARSER* p);
AST* parse_primitive_expr(PARSER* p) {
TOKEN* t = current_token(p);
if (t->kind == TK_L_INT) {
next(p);
AST* e = calloc(1, sizeof(AST));
e->kind = AST_INT_LIT_EXPR;
char buf[1024];
memcpy(buf, t->value, t->len);
buf[t->len] = 0;
e->int_value = atoi(buf);
return e;
} else {
fatal_error("parse_expr");
}
}
AST* parse_expr(PARSER* p) {
return parse_primitive_expr(p);
}
AST* parse_return_stmt(PARSER* p) {
expect(p, TK_K_RETURN);
AST* expr = parse_expr(p);
expect(p, TK_SEMICOLON);
AST* ret = calloc(1, sizeof(AST));
ret->kind = AST_RETURN_STMT;
ret->expr1 = expr;
return ret;
}
AST* parse_stmt(PARSER* p) {
TOKEN* t = current_token(p);
if (t->kind == TK_K_RETURN) {
return parse_return_stmt(p);
} else {
fatal_error("parse_stmt");
}
}
AST* parse_block_stmt(PARSER* p) {
AST* list = calloc(1, sizeof(AST));
list->kind = AST_BLOCK;
list->last = list;
expect(p, TK_BRACE_L);
while (current_token(p)->kind != TK_BRACE_R) {
AST* n = parse_stmt(p);
list->last->next = n;
list->last = n;
}
expect(p, TK_BRACE_R);
return list;
}
AST* parse_func_decl_or_def(PARSER* p) {
TOKEN* t = current_token(p);
if (t->kind == TK_K_INT) {
next(p);
TOKEN* name = expect(p, TK_IDENT);
expect(p, TK_PAREN_L);
expect(p, TK_PAREN_R);
AST* body = parse_block_stmt(p);
AST* func = calloc(1, sizeof(AST));
func->kind = AST_FUNC_DEF;
func->func_name = name;
func->func_body = body;
return func;
} else {
fatal_error("parse_func_decl_or_def: expect type");
}
}
AST* parse_toplevel(PARSER* p) {
return parse_func_decl_or_def(p);
}
AST* parse(PARSER* p) {
AST* list = calloc(1, sizeof(AST));
list->kind = AST_PROGRAM;
list->last = list;
while (eof(p)) {
AST* n = parse_toplevel(p);
list->last->next = n;
list->last = n;
}
return list;
}
typedef struct CodeGenerator {
} CODE_GENERATOR;
CODE_GENERATOR* codegen_new() {
CODE_GENERATOR* g = calloc(1, sizeof(CODE_GENERATOR));
return g;
}
void assert_ast_kind(AST* ast, int kind) {
if (ast->kind != kind) {
char buf[1024];
sprintf(buf, "invalid ast kind: expected %d, but got %d", kind, ast->kind);
fatal_error(buf);
}
}
void gen_int_lit_expr(CODE_GENERATOR* g, AST* ast) {
assert_ast_kind(ast, AST_INT_LIT_EXPR);
printf(" mov rax, %d\n", ast->int_value);
}
void gen_primitive_expr(CODE_GENERATOR* g, AST* ast) {
gen_int_lit_expr(g, ast);
}
void gen_expr(CODE_GENERATOR* g, AST* ast) {
gen_primitive_expr(g, ast);
}
void gen_return_stmt(CODE_GENERATOR* g, AST* ast) {
assert_ast_kind(ast, AST_RETURN_STMT);
gen_expr(g, ast->expr1);
printf(" ret\n");
}
void gen_block_stmt(CODE_GENERATOR* g, AST* ast) {
assert_ast_kind(ast, AST_BLOCK);
gen_return_stmt(g, ast->next);
}
void gen_stmt(CODE_GENERATOR* g, AST* ast) {
gen_block_stmt(g, ast);
}
void gen_func(CODE_GENERATOR* g, AST* ast) {
assert_ast_kind(ast, AST_FUNC_DEF);
gen_stmt(g, ast->func_body);
printf(" ret\n");
}
void gen(CODE_GENERATOR* g, AST* ast) {
assert_ast_kind(ast, AST_PROGRAM);
printf(".intel_syntax noprefix\n");
printf(".globl main\n");
printf("main:\n");
gen_func(g, ast->next);
}
int main() {
char source[1024*1024];
memset(source, 0, sizeof(source));
int source_len = read_all(source);
TOKEN* tokens = tokenize(source, source_len);
// for (int i = 0; tokens[i].kind != TK_EOF; i++) {
// for (int j = 0; j < tokens[i].len; j++) {
// putchar(tokens[i].value[j]);
// }
// printf("\n");
// }
PARSER* parser = parser_new(tokens);
AST* ast = parse(parser);
CODE_GENERATOR* code_generator = codegen_new();
gen(code_generator, ast);
return 0;
}
|