-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstmt.c
384 lines (356 loc) · 9 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
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
#include "stmt.h"
#include "decl.h"
#include "expr.h"
#include <stdio.h>
#include <stdlib.h>
#include "scope.h"
#include "register.h"
extern int local_no;
extern int ctl_no;
struct stmt *stmt_create(stmt_kind_t kind, struct decl *d, struct expr *init_expr, struct expr *e, struct expr *next_expr, struct stmt *body, struct stmt *else_body, int line, struct stmt *next) {
struct stmt *s = (struct stmt *)malloc(sizeof(struct stmt));
if(!s) {
fprintf(stdout, "malloc fails!\n");
exit(EXIT_FAILURE);
}
s->kind = kind;
s->decl = d;
s->init_expr = init_expr;
s->expr = e;
s->next_expr = next_expr;
s->body = body;
s->else_body = else_body;
s->line = line;
s->next = next;
return s;
}
void stmt_print(struct stmt *s, int indent1, int indent2, int curly_newline) {
if(!s) return;
switch(s->kind) {
case STMT_DECL:
decl_print(s->decl, indent1);
break;
case STMT_EXPR:
indent_process(indent2);
expr_print(s->expr);
printf(";\n");
break;
case STMT_PRINT:
indent_process(indent2);
printf("print");
if(s->expr) {
printf(" ");
expr_print(s->expr);
}
printf(";\n");
break;
case STMT_RETURN:
indent_process(indent2);
printf("return");
if(s->expr) {
printf(" ");
expr_print(s->expr);
}
printf(";\n");
break;
case STMT_IF_ELSE:
indent_process(indent2);
printf("if(");
expr_print(s->expr);
printf(")");
if(s->body->kind == STMT_BLOCK) {
printf(" ");
stmt_print(s->body, 0, indent2, 0);
printf(" ");
} else {
printf("\n");
stmt_print(s->body, indent1 + 4, indent2 + 4, 1);
}
if(s->else_body) {
if(s->body->kind == STMT_BLOCK) {
printf("else");
printf(" ");
stmt_print(s->else_body, 0, indent2, 1);
} else {
indent_process(indent2);
printf("else");
printf("\n");
stmt_print(s->else_body, indent1 + 4, indent2 + 4, 1);
}
} else {
if(s->body->kind == STMT_BLOCK)
printf("\n");
}
break;
case STMT_FOR:
indent_process(indent2);
printf("for(");
expr_print(s->init_expr);
printf(";");
expr_print(s->expr);
printf(";");
expr_print(s->next_expr);
printf(")");
if(s->body->kind == STMT_BLOCK) {
printf(" ");
stmt_print(s->body, 0, indent2, 1);
} else {
printf("\n");
stmt_print(s->body, indent1 + 4, indent2 + 4, 1);
}
break;
case STMT_BLOCK:
indent_process(indent1);
printf("{\n");
stmt_print(s->body, indent2 + 4, indent2 + 4, 1);
indent_process(indent2);
printf("}");
if(curly_newline)
printf("\n");
break;
}
stmt_print(s->next, indent2, indent2, 1);
return;
}
void indent_process(int indent) {
/* indent process */
if(indent > 0) {
char *spaces = (char *)malloc((indent+1) * sizeof(char));
if(!spaces) {
fprintf(stdout, "malloc fails!\n");
exit(EXIT_FAILURE);
}
int i;
for(i = 0; i < indent; i++) {
spaces[i] = ' ';
}
spaces[i] = '\0';
printf("%s", spaces);
free(spaces);
}
}
void stmt_resolve(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_PRINT:
expr_resolve(s->expr);
break;
case STMT_RETURN:
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_BLOCK:
scope_enter();
stmt_resolve(s->body);
scope_exit();
break;
}
stmt_resolve(s->next);
return;
}
void stmt_typecheck(struct stmt *s, const char *func_name) {
if(!s) return;
struct type *t, *func_return;
switch(s->kind) {
case STMT_DECL:
decl_typecheck(s->decl);
break;
case STMT_EXPR:
type_free(expr_typecheck(s->expr, 0, 0));
break;
case STMT_PRINT:
type_free(expr_typecheck(s->expr, 0, 0));
expr_print_typecheck(s->expr);
break;
case STMT_RETURN:
t = expr_typecheck(s->expr, 0, 0);
//return wrong number
func_return = scope_lookup(func_name, s->line, 0)->type->subtype;
if(!t) { //return;
if(func_return->kind != TYPE_VOID) {
fprintf(stdout, "type error (line %d): function (%s) returns a wrong type (void), and should return type ", s->line, func_name);
type_print(func_return);
printf("!\n");
type_error_count += 1;
}
} else {
if(!type_equals(t, func_return)) {
fprintf(stdout, "type error (line %d): function (%s) returns a wrong type (", s->line, func_name);
type_print(t);
printf("), and should return type ");
type_print(func_return);
printf("!\n");
type_error_count += 1;
}
}
type_free(t);
break;
case STMT_IF_ELSE:
t = expr_typecheck(s->expr, 0, 0);
if(t->kind != TYPE_BOOLEAN) {
fprintf(stdout, "type error (line %d): the expr (", s->expr->line);
expr_print(s->expr);
printf(") of if_stmt has wrong type (");
type_print(t);
printf("), and should be boolean!\n");
type_error_count += 1;
}
type_free(t);
stmt_typecheck(s->body, func_name);
stmt_typecheck(s->else_body, func_name);
break;
case STMT_FOR:
type_free(expr_typecheck(s->init_expr, 0, 0));
t = expr_typecheck(s->expr, 0, 0);
if(t && t->kind != TYPE_BOOLEAN) {
fprintf(stdout, "type error (line %d): the expr (", s->expr->line);
expr_print(s->expr);
printf(") of for_stmt has wrong type (");
type_print(t);
printf("), and should be boolean!\n");
type_error_count += 1;
}
type_free(t);
type_free(expr_typecheck(s->next_expr, 0, 0));
stmt_typecheck(s->body, func_name);
break;
case STMT_BLOCK:
stmt_typecheck(s->body, func_name);
break;
}
stmt_typecheck(s->next, func_name);
}
void stmt_codegen(struct stmt *s, FILE *f) {
if(!s) {
fprintf(f, "\tnop\n");
return;
}
register_freeall();
int old_ctl_no;
switch(s->kind) {
case STMT_DECL:
decl_codegen(s->decl, f);
break;
case STMT_EXPR:
expr_codegen(s->expr, f);
break;
case STMT_PRINT:
stmt_print_codegen(s->expr, f);
break;
case STMT_RETURN:
expr_codegen(s->expr, f);
if(s->expr) {
fprintf(f, "\tmovq\t%%%s, %%rax\n", register_name(s->expr->reg));
}
/* the postamble. Without postamble here would result the stmts after return still get executed. */
fprintf(f, "\tpopq\t%%r15\n");
fprintf(f, "\tpopq\t%%r14\n");
fprintf(f, "\tpopq\t%%r13\n");
fprintf(f, "\tpopq\t%%r12\n");
fprintf(f, "\tpopq\t%%rbx\n");
fprintf(f, "\tmovq\t%%rbp, %%rsp\n");
fprintf(f, "\tpopq\t%%rbp\n");
fprintf(f, "\tret\n");
break;
case STMT_IF_ELSE:
expr_codegen(s->expr, f);
old_ctl_no = ctl_no;
ctl_no += 2;
fprintf(f, "\tcmpq\t$0, %%%s\n", register_name(s->expr->reg));
fprintf(f, "\tje\t.L%d\n", old_ctl_no);
stmt_codegen(s->body, f);
fprintf(f, "\tjmp\t.L%d\n", old_ctl_no+1);
fprintf(f, ".L%d:\n", old_ctl_no);
stmt_codegen(s->else_body, f);
fprintf(f, ".L%d:\n", old_ctl_no+1);
break;
case STMT_FOR:
expr_codegen(s->init_expr, f);
old_ctl_no = ctl_no;
ctl_no += 2;
fprintf(f, ".L%d:\n", old_ctl_no);
if(s->expr) {
expr_codegen(s->expr, f);
fprintf(f, "\tcmpq\t$0, %%%s\n", register_name(s->expr->reg));
fprintf(f, "\tje\t.L%d\n", old_ctl_no+1);
}
stmt_codegen(s->body, f);
expr_codegen(s->next_expr, f);
fprintf(f, "\tjmp\t.L%d\n", old_ctl_no);
fprintf(f, ".L%d:\n", old_ctl_no+1);
break;
case STMT_BLOCK:
stmt_codegen(s->body, f);
break;
}
stmt_codegen(s->next, f);
}
void stmt_print_codegen(struct expr *e, FILE *f) {
if(!e) return;
int n = expr_count_item(e);
int i;
struct expr *arg;
for(i = 1; i <= n; i++) {
arg = expr_get_item(e, n, i);
struct type *t = expr_typecheck(arg, 0, 1);
expr_codegen(arg, f);
switch(t->kind) {
//print integer
case TYPE_INTEGER:
fprintf(f, "\tmovq\t%%%s, %%rdi\n", register_name(arg->reg));
fprintf(f, "\tpushq\t%%r10\n");
fprintf(f, "\tpushq\t%%r11\n");
fprintf(f, "\tcall\tprint_integer\n");
fprintf(f, "\tpopq\t%%r11\n");
fprintf(f, "\tpopq\t%%r10\n");
break;
//print char
case TYPE_CHARACTER:
fprintf(f, "\tmovq\t%%%s, %%rdi\n", register_name(arg->reg));
fprintf(f, "\tpushq\t%%r10\n");
fprintf(f, "\tpushq\t%%r11\n");
fprintf(f, "\tcall\tprint_character\n");
fprintf(f, "\tpopq\t%%r11\n");
fprintf(f, "\tpopq\t%%r10\n");
break;
//print string
case TYPE_STRING:
fprintf(f, "\tmovq\t%%%s, %%rdi\n", register_name(arg->reg));
fprintf(f, "\tpushq\t%%r10\n");
fprintf(f, "\tpushq\t%%r11\n");
fprintf(f, "\tcall\tprint_string\n");
fprintf(f, "\tpopq\t%%r11\n");
fprintf(f, "\tpopq\t%%r10\n");
break;
//print boolean
case TYPE_BOOLEAN:
fprintf(f, "\tmovq\t%%%s, %%rdi\n", register_name(arg->reg));
fprintf(f, "\tpushq\t%%r10\n");
fprintf(f, "\tpushq\t%%r11\n");
fprintf(f, "\tcall\tprint_boolean\n");
fprintf(f, "\tpopq\t%%r11\n");
fprintf(f, "\tpopq\t%%r10\n");
break;
default:
break;
}
register_free(arg->reg);
type_free(t);
}
}