This repository has been archived by the owner on Jun 28, 2024. It is now read-only.
generated from dthain/compilerbook-starter-code
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstmt.c
361 lines (333 loc) · 6.63 KB
/
stmt.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
#include <stdlib.h>
#include <stdio.h>
#include "type.h"
#include "expr.h"
#include "decl.h"
#include "stmt.h"
#include "scope.h"
#include "scratch.h"
#include "label.h"
struct stmt* stmt_create(
stmt_t kind,
struct decl* decl,
struct expr* init_expr,
struct expr* expr,
struct expr* next_expr,
struct stmt* body,
struct stmt* else_body,
struct stmt* next
)
{
struct stmt* s = malloc(sizeof(*s));
s->kind = kind;
s->decl = decl;
s->init_expr = init_expr;
s->expr = expr;
s->next_expr = next_expr;
s->body = body;
s->else_body = else_body;
s->next = next;
return s;
}
struct stmt* stmt_create_empty(stmt_t kind)
{
return stmt_create(
kind,
NULL,
NULL,
NULL,
NULL,
NULL,
NULL,
NULL
);
}
/* Print a statement */
void stmt_print_indent(int indent)
{
for (int i = 0; i < indent; i++)
printf("\t");
}
void stmt_print_if_else(const struct stmt* s, int indent)
{
stmt_print_indent(indent);
printf("if (");
expr_print(s->expr);
printf(")\n");
if (s->body)
{
if (s->body->kind == STMT_BLOCK)
stmt_print(s->body, indent);
else
stmt_print(s->body, indent + 1);
}
if (s->else_body)
{
stmt_print_indent(indent);
printf("else\n");
if (s->else_body->kind == STMT_BLOCK)
stmt_print(s->else_body, indent);
else
stmt_print(s->else_body, indent + 1);
}
}
void stmt_print_for(const struct stmt* s, int indent)
{
stmt_print_indent(indent);
printf("for (");
if (s->init_expr)
{
expr_print(s->init_expr);
printf("; ");
}
else printf(";");
if (s->next_expr)
{
expr_print(s->expr);
printf("; ");
}
else printf(";");
if (s->next_expr)
expr_print(s->next_expr);
printf(")\n");
if (s->body->kind == STMT_BLOCK)
stmt_print(s->body, indent);
else
stmt_print(s->body, indent + 1);
}
void stmt_print(const struct stmt* s, int indent)
{
switch (s->kind)
{
case STMT_DECL:
decl_print(s->decl, indent);
break;
case STMT_EXPR:
stmt_print_indent(indent);
expr_print(s->expr);
printf(";\n");
break;
case STMT_IF_ELSE:
stmt_print_if_else(s, indent);
break;
case STMT_FOR:
stmt_print_for(s, indent);
break;
case STMT_PRINT:
stmt_print_indent(indent);
printf("print ");
expr_print(s->expr);
printf(";\n");
break;
case STMT_RETURN:
stmt_print_indent(indent);
printf("return");
if (s->expr)
{
printf(" ");
expr_print(s->expr);
}
printf(";\n");
break;
case STMT_BLOCK:
stmt_print_indent(indent);
printf("{\n");
stmt_print(s->body, indent + 1);
stmt_print_indent(indent);
printf("}\n");
break;
}
if (s->next)
stmt_print(s->next, indent);
}
void stmt_resolve(const struct stmt* s)
{
if (!s) return;
switch (s->kind)
{
case STMT_DECL:
decl_resolve(s->decl);
break;
case STMT_EXPR:
expr_resolve(s->expr);
break;
case STMT_IF_ELSE:
expr_resolve(s->expr);
stmt_resolve(s->body);
stmt_resolve(s->else_body);
break;
case STMT_FOR:
expr_resolve(s->init_expr);
expr_resolve(s->expr);
expr_resolve(s->next_expr);
stmt_resolve(s->body);
break;
case STMT_PRINT:
expr_resolve(s->expr);
break;
case STMT_RETURN:
expr_resolve(s->expr);
break;
case STMT_BLOCK:
scope_enter();
stmt_resolve(s->body);
scope_exit();
break;
}
stmt_resolve(s->next);
}
void stmt_typecheck(const struct stmt* s)
{
if (!s) return;
extern struct type* rtype;
struct type* expr_type;
switch (s->kind)
{
case STMT_DECL:
decl_typecheck(s->decl);
break;
case STMT_IF_ELSE:
case STMT_FOR:
expr_typecheck(s->init_expr);
struct type* cond_type = expr_typecheck(s->expr);
if (cond_type->kind != TYPE_BOOLEAN)
{
printf("Type Error | condition of if/for statement cannot be ");
type_print(cond_type);
printf(" (");
expr_print(s->expr);
printf(")\n");
type_errors++;
}
expr_typecheck(s->next_expr);
stmt_typecheck(s->body);
stmt_typecheck(s->else_body);
break;
case STMT_BLOCK:
stmt_typecheck(s->body);
break;
case STMT_EXPR:
case STMT_PRINT:
expr_typecheck(s->expr);
break;
case STMT_RETURN:
expr_type = expr_typecheck(s->expr);
if (!(rtype->kind == TYPE_VOID && expr_type == NULL) &&
!type_equals(expr_type, rtype))
{
printf("Type Error | return type mismatch, expected ");
type_print(rtype);
printf(", got");
if (s->expr)
{
printf(" ");
expr_print(s->expr);
printf(" (");
type_print(expr_typecheck(s->expr));
printf(")\n");
}
else
printf(" nothing\n");
type_errors++;
}
break;
}
stmt_typecheck(s->next);
}
/** Generates code for print statement */
void print_codegen(const struct stmt* s)
{
struct expr* e = s->expr;
while (e)
{
expr_codegen(e->left);
switch (e->kind)
{
case EXPR_INTEGER_LITERAL:
printf(".data\n");
printf(".formatter_d: .string \"%%d\"\n");
printf("movq $%d, %%rsi\n", e->integer_literal);
printf("leaq .formatter_d(%%rip), %%rdi\n");
break;
default:
// TODO: Others printf formats
break;
}
}
}
void stmt_codegen(const struct stmt* s)
{
if (!s) return;
int top, end; // Labels
extern const char* cur_func;
switch (s->kind)
{
case STMT_DECL:
decl_codegen(s->decl);
break;
case STMT_EXPR:
if (s->expr)
{
expr_codegen(s->expr);
scratch_free(s->expr->reg);
}
break;
case STMT_IF_ELSE:
top = label_create();
end = label_create();
printf("# if-else condition\n");
if (s->expr)
{
expr_codegen(s->expr);
printf("cmp $0, %s\n", scratch_name(s->expr->reg));
scratch_free(s->expr->reg);
printf("je %s\n", label_name(top)); // To false
}
printf("# if-else body\n");
stmt_codegen(s->body);
printf("jmp %s\n", label_name(end)); // To end
printf("# if-else else body\n");
printf("%s:\n", label_name(top)); // False label
stmt_codegen(s->else_body);
printf("%s:\n", label_name(end)); // End label
break;
case STMT_FOR:
top = label_create();
end = label_create();
printf("# for-loop init expr\n");
expr_codegen(s->init_expr);
printf("%s:\n", label_name(top)); // Loop top
printf("# for-loop expr\n");
if (s->expr)
{
expr_codegen(s->expr);
printf("cmp $0 %s\n", scratch_name(s->expr->reg));
scratch_free(s->expr->reg);
printf("je %s\n", label_name(end));
}
printf("# for-loop body\n");
stmt_codegen(s->body);
printf("# for-loop next expr\n");
expr_codegen(s->next_expr);
printf("jmp %s:\n", label_name(top));
printf("%s:\n", label_name(end)); // Loop end
break;
case STMT_PRINT:
// TODO: Implement register calling
// TODO: Calling runtime library
break;
case STMT_RETURN:
if (s->expr)
{
expr_codegen(s->expr);
printf("movq %s, %%rax\n", scratch_name(s->expr->reg));
scratch_free(s->expr->reg);
}
printf("jmp .%s_epilogue\n", cur_func);
break;
case STMT_BLOCK:
stmt_codegen(s->body);
break;
}
stmt_codegen(s->next);
}