-
Notifications
You must be signed in to change notification settings - Fork 0
/
ast.c
executable file
·644 lines (539 loc) · 17.6 KB
/
ast.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
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
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
#include <stdio.h>
#include <stdlib.h>
#include "ast.h"
#include "y.tab.h"
#include "utils.h"
const char *type_name(enum value_type t) {
switch (t) {
case INTEGER: return "int";
case BOOLEAN: return "bool";
case FLOAT: return "float";
case ERROR: return "error";
default: return "not-a-type";
}
}
struct expr* bool_lit(int v) {
struct expr* r = malloc(sizeof(struct expr));
r->type = BOOL_LIT;
r->value = v;
return r;
}
struct expr* literal(int v) {
struct expr* r = malloc(sizeof(struct expr));
r->type = LITERAL;
r->value = v;
return r;
}
struct expr* decimal(float v) {
struct expr* r = malloc(sizeof(struct expr));
r->type = DECIMAL;
r->value_dec = v;
return r;
}
struct expr* variable(size_t id) {
struct expr* r = malloc(sizeof(struct expr));
r->type = VARIABLE;
r->id = id;
return r;
}
struct expr* binop(struct expr *lhs, int op, struct expr *rhs) {
struct expr* r = malloc(sizeof(struct expr));
r->type = BIN_OP;
r->binop.lhs = lhs;
r->binop.op = op;
r->binop.rhs = rhs;
return r;
}
void print_expr(struct expr *expr) {
switch (expr->type) {
case BOOL_LIT:
printf("%s", expr->value ? "true" : "false");
break;
case LITERAL:
printf("%d", expr->value);
break;
case DECIMAL:
printf("%f", expr->value_dec * 1.0);
break;
case VARIABLE:
printf("%s", string_int_rev(&global_ids, expr->id));
break;
case BIN_OP:
printf("(");
print_expr(expr->binop.lhs);
switch (expr->binop.op) {
case EQ: printf(" == "); break;
case NE: printf(" != "); break;
case GE: printf(" >= "); break;
case LE: printf(" <= "); break;
default: printf(" %c ", expr->binop.op); break;
}
print_expr(expr->binop.rhs);
printf(")");
break;
}
}
static void print_indent(int indent) {
while (indent--) {
printf(" ");
}
}
void print_stmt(struct stmt *stmt, int indent) {
switch (stmt->type) {
case STMT_SEQ:
print_stmt(stmt->seq.fst, indent);
print_stmt(stmt->seq.snd, indent);
break;
case STMT_ASSIGN:
print_indent(indent);
printf("%s = ", string_int_rev(&global_ids, stmt->assign.id));
print_expr(stmt->assign.expr);
printf(";\n");
break;
case STMT_PRINT:
print_indent(indent);
printf("print ");
print_expr(stmt->print.expr);
printf(";\n");
break;
case STMT_WHILE:
print_indent(indent);
printf("while (");
print_expr(stmt->while_.cond);
printf(") {\n");
print_stmt(stmt->while_.body, indent + 1);
print_indent(indent);
printf("}\n");
break;
case STMT_DOWHILE:
print_indent(indent);
printf("do {\n");
print_stmt(stmt->dowhile.body, indent + 1);
print_indent(indent);
printf("} while (");
print_expr(stmt->dowhile.cond);
printf(");\n");
break;
case STMT_FOR:
print_indent(indent);
printf("for (");
print_stmt(stmt->for_.init, indent + 1);
print_indent(indent);
printf(";\n");
print_expr(stmt->for_.cond);
printf(";\n");
print_stmt(stmt->for_.incr, indent + 1);
print_indent(indent);
printf(") {\n");
print_stmt(stmt->for_.body, indent + 1);
print_indent(indent);
printf("}\n");
break;
case STMT_IF:
print_indent(indent);
printf("if (");
print_expr(stmt->ifelse.cond);
printf(") {\n");
print_stmt(stmt->ifelse.if_body, indent + 1);
if (stmt->ifelse.else_body) {
print_indent(indent);
printf("} else {\n");
print_stmt(stmt->ifelse.else_body, indent + 1);
}
print_indent(indent);
printf("}\n");
break;
}
}
void emit_stack_machine(struct expr *expr) {
switch (expr->type) {
case BOOL_LIT:
printf(expr->value ? "load_true\n" : "load_false\n");
break;
case LITERAL:
printf("load_imm %d\n", expr->value);
break;
case DECIMAL:
printf("load_fmm %f\n", expr->value_dec * 1.0);
break;
case VARIABLE:
printf("load_mem %zu # %s\n", expr->id, string_int_rev(&global_ids, expr->id));
break;
case BIN_OP:
emit_stack_machine(expr->binop.lhs);
emit_stack_machine(expr->binop.rhs);
switch (expr->binop.op) {
case '+': printf("add\n"); break;
case '-': printf("sub\n"); break;
case '*': printf("mul\n"); break;
case '/': printf("div\n"); break;
case '%': printf("mod\n"); break;
case AND: printf("and\n"); break;
case OR: printf("or\n"); break;
case EQ: printf("eq\n"); break;
case NE: printf("ne\n"); break;
case GE: printf("ge\n"); break;
case LE: printf("le\n"); break;
case '>': printf("gt\n"); break;
case '<': printf("lt\n"); break;
}
break;
}
}
static int next_reg = 0;
static int gen_reg() {
return next_reg++;
}
int emit_reg_machine(struct expr *expr) {
int result_reg = gen_reg();
switch (expr->type) {
case BOOL_LIT:
printf("r%d = %d\n", result_reg, expr->value);
break;
case LITERAL:
printf("r%d = %d\n", result_reg, expr->value);
break;
case DECIMAL:
printf("r%d = %f\n", result_reg, expr->value_dec * 1.0);
break;
case VARIABLE:
printf("r%d = load %zu # %s\n", result_reg, expr->id, string_int_rev(&global_ids, expr->id));
break;
case BIN_OP: {
int lhs = emit_reg_machine(expr->binop.lhs);
int rhs = emit_reg_machine(expr->binop.rhs);
switch (expr->binop.op) {
case '+': printf("r%d = add r%d, r%d\n", result_reg, lhs, rhs); break;
case '-': printf("r%d = sub r%d, r%d\n", result_reg, lhs, rhs); break;
case '*': printf("r%d = mul r%d, r%d\n", result_reg, lhs, rhs); break;
case '/': printf("r%d = div r%d, r%d\n", result_reg, lhs, rhs); break;
case '%': printf("r%d = mod r%d, r%d\n", result_reg, lhs, rhs); break;
case AND: printf("r%d = and r%d, r%d\n", result_reg, lhs, rhs); break;
case OR: printf("r%d = or r%d, r%d\n", result_reg, lhs, rhs); break;
case EQ: printf("r%d = eq r%d, r%d\n", result_reg, lhs, rhs); break;
case NE: printf("r%d = ne r%d, r%d\n", result_reg, lhs, rhs); break;
case GE: printf("r%d = ge r%d, r%d\n", result_reg, lhs, rhs); break;
case LE: printf("r%d = le r%d, r%d\n", result_reg, lhs, rhs); break;
case '>': printf("r%d = gt r%d, r%d\n", result_reg, lhs, rhs); break;
case '<': printf("r%d = lt r%d, r%d\n", result_reg, lhs, rhs); break;
}
break;
}
}
return result_reg;
}
enum value_type check_types(struct expr *expr) {
switch (expr->type) {
case BOOL_LIT:
return BOOLEAN;
case LITERAL:
return INTEGER;
case DECIMAL:
return FLOAT;
case VARIABLE: {
LLVMValueRef ptr = vector_get(&global_types, expr->id);
LLVMTypeRef t = LLVMGetElementType(LLVMTypeOf(ptr));
return LLVMGetIntTypeWidth(t) == 1 ? BOOLEAN : INTEGER;
}
case BIN_OP: {
enum value_type lhs = check_types(expr->binop.lhs);
enum value_type rhs = check_types(expr->binop.rhs);
switch (expr->binop.op) {
case '+':
case '-':
case '*':
case '/':
case '%':
if (lhs == INTEGER && rhs == INTEGER)
return INTEGER;
else if (lhs == FLOAT && rhs == FLOAT)
return FLOAT;
else
return ERROR;
case AND:
case OR:
if (lhs == INTEGER && rhs == INTEGER)
return INTEGER;
else if (lhs == BOOLEAN && rhs == BOOLEAN)
return BOOLEAN;
else
return ERROR;
case EQ:
case NE:
if (lhs == rhs && lhs != ERROR)
return BOOLEAN;
else
return ERROR;
case GE:
case LE:
case '>':
case '<':
if (lhs == INTEGER && rhs == INTEGER)
return BOOLEAN;
else if (lhs == FLOAT && rhs == FLOAT)
return BOOLEAN;
else
return ERROR;
}
default:
return ERROR;
}
}
}
void free_expr(struct expr *expr) {
switch (expr->type) {
case BOOL_LIT:
case LITERAL:
case VARIABLE:
case DECIMAL:
free(expr);
break;
case BIN_OP:
free_expr(expr->binop.lhs);
free_expr(expr->binop.rhs);
free(expr);
break;
}
}
struct stmt* make_seq(struct stmt *fst, struct stmt *snd) {
struct stmt* r = malloc(sizeof(struct stmt));
r->type = STMT_SEQ;
r->seq.fst = fst;
r->seq.snd = snd;
return r;
}
struct stmt* make_assign(size_t id, struct expr *e) {
struct stmt* r = malloc(sizeof(struct stmt));
r->type = STMT_ASSIGN;
r->assign.id = id;
r->assign.expr = e;
return r;
}
struct stmt* make_while(struct expr *e, struct stmt *body) {
struct stmt* r = malloc(sizeof(struct stmt));
r->type = STMT_WHILE;
r->while_.cond = e;
r->while_.body = body;
return r;
}
struct stmt* make_dowhile(struct stmt *body, struct expr *e) {
struct stmt* r = malloc(sizeof(struct stmt));
r->type = STMT_DOWHILE;
r->dowhile.cond = e;
r->dowhile.body = body;
return r;
}
struct stmt* make_for(struct stmt *init, struct expr *cond, struct stmt *incr, struct stmt *body) {
struct stmt* r = malloc(sizeof(struct stmt));
r->type = STMT_FOR;
r->for_.init = init;
r->for_.cond = cond;
r->for_.incr = incr;
r->for_.body = body;
return r;
}
struct stmt* make_ifelse(struct expr *e, struct stmt *if_body, struct stmt *else_body) {
struct stmt* r = malloc(sizeof(struct stmt));
r->type = STMT_IF;
r->ifelse.cond = e;
r->ifelse.if_body = if_body;
r->ifelse.else_body = else_body;
return r;
}
struct stmt* make_if(struct expr *e, struct stmt *body) {
return make_ifelse(e, body, NULL);
}
struct stmt* make_print(struct expr *e) {
struct stmt* r = malloc(sizeof(struct stmt));
r->type = STMT_PRINT;
r->print.expr = e;
return r;
}
void free_stmt(struct stmt *stmt) {
switch (stmt->type) {
case STMT_SEQ:
free_stmt(stmt->seq.fst);
free_stmt(stmt->seq.snd);
break;
case STMT_ASSIGN:
free_expr(stmt->assign.expr);
break;
case STMT_PRINT:
free_expr(stmt->print.expr);
break;
case STMT_WHILE:
free_expr(stmt->while_.cond);
free_stmt(stmt->while_.body);
break;
case STMT_DOWHILE:
free_expr(stmt->dowhile.cond);
free_stmt(stmt->dowhile.body);
break;
case STMT_FOR:
free_stmt(stmt->for_.init);
free_expr(stmt->for_.cond);
free_stmt(stmt->for_.body);
free_stmt(stmt->for_.incr);
break;
case STMT_IF:
free_expr(stmt->ifelse.cond);
free_stmt(stmt->ifelse.if_body);
if (stmt->ifelse.else_body)
free_stmt(stmt->ifelse.else_body);
break;
}
free(stmt);
}
int valid_stmt(struct stmt *stmt) {
switch (stmt->type) {
case STMT_SEQ:
return valid_stmt(stmt->seq.fst) && valid_stmt(stmt->seq.snd);
case STMT_ASSIGN:
// should the language/compiler forbid accessing uninitialized variables?
// maybe also warn about dead assignments?
return check_types(stmt->assign.expr) != ERROR;
case STMT_PRINT:
return check_types(stmt->print.expr) != ERROR;
case STMT_WHILE:
return check_types(stmt->while_.cond) == BOOLEAN && valid_stmt(stmt->while_.body);
case STMT_DOWHILE:
return check_types(stmt->dowhile.cond) == BOOLEAN && valid_stmt(stmt->dowhile.body);
case STMT_FOR:
return check_types(stmt->for_.cond) == BOOLEAN
&& valid_stmt(stmt->for_.init)
&& valid_stmt(stmt->for_.body)
&& valid_stmt(stmt->for_.incr);
case STMT_IF:
return
check_types(stmt->ifelse.cond) == BOOLEAN &&
valid_stmt(stmt->ifelse.if_body) &&
(stmt->ifelse.else_body == NULL || valid_stmt(stmt->ifelse.else_body));
default:
return 0;
}
}
LLVMValueRef codegen_expr(struct expr *expr, LLVMModuleRef module, LLVMBuilderRef builder) {
switch (expr->type) {
case BOOL_LIT:
return LLVMConstInt(LLVMInt1Type(), expr->value, 0);
case LITERAL:
return LLVMConstInt(LLVMInt32Type(), expr->value, 0);
case DECIMAL:
return LLVMConstReal(LLVMFloatType(), expr->value_dec * 1.0);
case VARIABLE:
return LLVMBuildLoad(builder, vector_get(&global_types, expr->id), "loadtmp");
case BIN_OP: {
LLVMValueRef lhs = codegen_expr(expr->binop.lhs, module, builder);
LLVMValueRef rhs = codegen_expr(expr->binop.rhs, module, builder);
switch (expr->binop.op) {
case '+': return LLVMBuildAdd(builder, lhs, rhs, "addtmp");
case '-': return LLVMBuildSub(builder, lhs, rhs, "subtmp");
case '*': return LLVMBuildMul(builder, lhs, rhs, "multmp");
case '/': return LLVMBuildSDiv(builder, lhs, rhs, "divtmp");
case '%': return LLVMBuildURem(builder, lhs, rhs, "modtmp");
case AND: return LLVMBuildAnd(builder, lhs, rhs, "andtmp");
case OR: return LLVMBuildOr(builder, lhs, rhs, "ortmp");
case EQ: return LLVMBuildICmp(builder, LLVMIntEQ, lhs, rhs, "eqtmp");
case NE: return LLVMBuildICmp(builder, LLVMIntNE, lhs, rhs, "netmp");
case GE: return LLVMBuildICmp(builder, LLVMIntSGE, lhs, rhs, "getmp");
case LE: return LLVMBuildICmp(builder, LLVMIntSLE, lhs, rhs, "letmp");
case '>': return LLVMBuildICmp(builder, LLVMIntSGT, lhs, rhs, "gttmp");
case '<': return LLVMBuildICmp(builder, LLVMIntSLT, lhs, rhs, "lttmp");
}
}
}
return NULL;
}
void codegen_stmt(struct stmt *stmt, LLVMModuleRef module, LLVMBuilderRef builder) {
switch (stmt->type) {
case STMT_SEQ: {
codegen_stmt(stmt->seq.fst, module, builder);
codegen_stmt(stmt->seq.snd, module, builder);
break;
}
case STMT_ASSIGN: {
LLVMValueRef expr = codegen_expr(stmt->assign.expr, module, builder);
LLVMBuildStore(builder, expr, vector_get(&global_types, stmt->assign.id));
break;
}
case STMT_PRINT: {
enum value_type arg_type = check_types(stmt->print.expr);
LLVMValueRef print_fn;
if(arg_type == BOOLEAN) {
print_fn = LLVMGetNamedFunction(module, "print_i1");
} else if(arg_type == INTEGER) {
print_fn = LLVMGetNamedFunction(module, "print_i32");
} else {
print_fn = LLVMGetNamedFunction(module, "print_f32");
}
LLVMValueRef args[] = { codegen_expr(stmt->print.expr, module, builder) };
LLVMBuildCall(builder, print_fn, args, 1, "");
break;
}
case STMT_WHILE: {
LLVMValueRef func = LLVMGetBasicBlockParent(LLVMGetInsertBlock(builder));
LLVMBasicBlockRef cond_bb = LLVMAppendBasicBlock(func, "cond");
LLVMBasicBlockRef body_bb = LLVMAppendBasicBlock(func, "body");
LLVMBasicBlockRef cont_bb = LLVMAppendBasicBlock(func, "cont");
LLVMBuildBr(builder, cond_bb);
LLVMPositionBuilderAtEnd(builder, cond_bb);
LLVMValueRef cond = codegen_expr(stmt->while_.cond, module, builder);
LLVMBuildCondBr(builder, cond, body_bb, cont_bb);
LLVMPositionBuilderAtEnd(builder, body_bb);
codegen_stmt(stmt->while_.body, module, builder);
LLVMBuildBr(builder, cond_bb);
LLVMPositionBuilderAtEnd(builder, cont_bb);
break;
}
case STMT_FOR: {
LLVMValueRef func = LLVMGetBasicBlockParent(LLVMGetInsertBlock(builder));
LLVMBasicBlockRef cond_bb = LLVMAppendBasicBlock(func, "cond");
LLVMBasicBlockRef body_bb = LLVMAppendBasicBlock(func, "body");
LLVMBasicBlockRef cont_bb = LLVMAppendBasicBlock(func, "cont");
codegen_stmt(stmt->for_.init, module, builder);
LLVMBuildBr(builder, cond_bb);
LLVMPositionBuilderAtEnd(builder, cond_bb);
LLVMValueRef cond = codegen_expr(stmt->for_.cond, module, builder);
LLVMBuildCondBr(builder, cond, body_bb, cont_bb);
LLVMPositionBuilderAtEnd(builder, body_bb);
codegen_stmt(stmt->for_.body, module, builder);
codegen_stmt(stmt->for_.incr, module, builder);
LLVMBuildBr(builder, cond_bb);
LLVMPositionBuilderAtEnd(builder, cont_bb);
break;
}
case STMT_DOWHILE: {
LLVMValueRef func = LLVMGetBasicBlockParent(LLVMGetInsertBlock(builder));
LLVMBasicBlockRef cond_bb = LLVMAppendBasicBlock(func, "cond");
LLVMBasicBlockRef body_bb = LLVMAppendBasicBlock(func, "body");
LLVMBasicBlockRef cont_bb = LLVMAppendBasicBlock(func, "cont");
LLVMBuildBr(builder, cond_bb);
LLVMPositionBuilderAtEnd(builder, cond_bb);
LLVMValueRef cond = codegen_expr(stmt->dowhile.cond, module, builder);
LLVMBuildCondBr(builder, cond, body_bb, cont_bb);
LLVMPositionBuilderAtEnd(builder, body_bb);
codegen_stmt(stmt->dowhile.body, module, builder);
LLVMBuildBr(builder, cond_bb);
LLVMPositionBuilderAtEnd(builder, cont_bb);
break;
}
case STMT_IF: {
LLVMValueRef func = LLVMGetBasicBlockParent(LLVMGetInsertBlock(builder));
LLVMBasicBlockRef body_bb = LLVMAppendBasicBlock(func, "body");
LLVMBasicBlockRef else_bb = LLVMAppendBasicBlock(func, "else");
LLVMBasicBlockRef cont_bb = LLVMAppendBasicBlock(func, "cont");
LLVMValueRef cond = codegen_expr(stmt->ifelse.cond, module, builder);
LLVMBuildCondBr(builder, cond, body_bb, else_bb);
LLVMPositionBuilderAtEnd(builder, body_bb);
codegen_stmt(stmt->ifelse.if_body, module, builder);
LLVMBuildBr(builder, cont_bb);
LLVMPositionBuilderAtEnd(builder, else_bb);
if (stmt->ifelse.else_body) {
codegen_stmt(stmt->ifelse.else_body, module, builder);
}
LLVMBuildBr(builder, cont_bb);
LLVMPositionBuilderAtEnd(builder, cont_bb);
break;
}
}
}