-
Notifications
You must be signed in to change notification settings - Fork 0
/
ast.c
389 lines (363 loc) · 6.43 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
#include <stdlib.h>
#include <string.h>
#include "ast.h"
#include "hash_table.h"
#define NEW(t) (malloc(sizeof(struct t)))
struct prog *prog_make(struct decl *ast)
{
struct prog *p = NEW(prog);
p->ast = ast;
p->strings = hash_table_create(0, 0);
p->symbols = NULL;
return p;
}
void prog_add_string(struct prog *prog, const char *string)
{
static int count = 0;
int *ip = malloc(sizeof(int));
*ip = ++count;
hash_table_insert(prog->strings, string, ip, NULL);
}
void prog_free(struct prog **pp)
{
if (!pp || !(*pp)) {
return;
}
struct prog *p = *pp;
decl_free(&p->ast);
hash_table_delete(p->strings);
symbol_free(&p->symbols);
free(p);
*pp = 0;
}
const char *reg_to_s(enum reg reg)
{
switch (reg) {
case REG_EAX:
return "%eax";
case REG_EBX:
return "%ebx";
case REG_ECX:
return "%ecx";
case REG_EDX:
return "%edx";
case REG_ESI:
return "%esi";
case REG_EDI:
return "%edi";
default:
return NULL;
}
}
struct decl *decl_make(char *name, struct type *type, struct expr *value, struct stmt *code)
{
struct decl *d = NEW(decl);
d->name = name;
d->type = type;
d->value = value;
d->code = code;
d->symbol = NULL;
d->next = NULL;
d->num_locals = 0;
d->regs = 0;
return d;
}
void decl_free(struct decl **dp)
{
if (!dp || !(*dp)) {
return;
}
struct decl *d = *dp;
free(d->name);
type_free(&d->type);
expr_free(&d->value);
stmt_free(&d->code);
decl_free(&d->next);
free(d);
*dp = 0;
}
const char *type_kind_to_s(enum type_kind kind)
{
switch (kind) {
case TYPE_CHAR:
return "char";
case TYPE_INT:
return "int";
case TYPE_BOOLEAN:
return "boolean";
case TYPE_STRING:
return "string";
case TYPE_VOID:
return "void";
default:
return NULL;
}
}
struct type *type_make(enum type_kind kind, struct param *params, struct type *rtype)
{
struct type *t = NEW(type);
t->kind = kind;
t->params = params;
t->rtype = rtype;
return t;
}
void type_free(struct type **tp)
{
if (!tp || !(*tp)) {
return;
}
struct type *t = *tp;
param_free(&t->params);
type_free(&t->rtype);
free(t);
*tp = 0;
}
const char *expr_kind_to_s(enum expr_kind kind)
{
switch (kind) {
case EXPR_LT:
return "<";
case EXPR_LE:
return "<=";
case EXPR_EQ:
return "==";
case EXPR_NE:
return "!=";
case EXPR_GT:
return ">";
case EXPR_GE:
return ">=";
case EXPR_NOT:
return "!";
case EXPR_ADD:
case EXPR_POS:
return "+";
case EXPR_SUB:
case EXPR_NEG:
return "-";
case EXPR_MUL:
return "*";
case EXPR_DIV:
return "/";
case EXPR_MOD:
return "%";
case EXPR_ASSIGN:
return "=";
case EXPR_PRE_INCR:
case EXPR_POST_INCR:
return "++";
case EXPR_PRE_DECR:
case EXPR_POST_DECR:
return "--";
case EXPR_OR:
return "||";
case EXPR_AND:
return "&&";
case EXPR_POW:
return "^";
default:
return NULL;
}
}
struct expr *expr_make(enum expr_kind kind, struct expr *left, struct expr *right, char *name, int constant)
{
struct expr *e = NEW(expr);
e->kind = kind;
e->left = left;
e->right = right;
e->name = name; /* consider copying this */
e->constant = constant;
e->symbol = NULL;
e->reg = 0;
return e;
}
struct expr *expr_copy(struct expr *e)
{
if (!e) {
return NULL;
}
struct expr *copy = expr_make(e->kind, NULL, NULL, NULL, e->constant);
if (e->name) {
copy->name = malloc(strlen(e->name) + 1);
strcpy(copy->name, e->name);
}
copy->left = expr_copy(e->left);
copy->right = expr_copy(e->right);
copy->symbol = e->symbol;
return copy;
}
void expr_free(struct expr **ep)
{
if (!ep || !(*ep)) {
return;
}
struct expr *e = *ep;
expr_free(&e->left);
expr_free(&e->right);
free(e->name);
free(e);
*ep = 0;
}
enum type_kind expr_to_type_kind(struct expr *e)
{
if (!e) {
return TYPE_UNKNOWN;
}
switch (e->kind) {
case EXPR_LT:
case EXPR_LE:
case EXPR_EQ:
case EXPR_NE:
case EXPR_GT:
case EXPR_GE:
case EXPR_NOT:
case EXPR_AND:
case EXPR_OR:
case EXPR_BOOLEAN:
return TYPE_BOOLEAN;
case EXPR_ADD:
case EXPR_SUB:
case EXPR_MUL:
case EXPR_DIV:
case EXPR_POS:
case EXPR_NEG:
case EXPR_MOD:
case EXPR_PRE_INCR:
case EXPR_PRE_DECR:
case EXPR_POST_INCR:
case EXPR_POST_DECR:
case EXPR_POW:
case EXPR_INT:
return TYPE_INT;
case EXPR_CHAR:
return TYPE_CHAR;
case EXPR_STRING:
return TYPE_STRING;
case EXPR_ARG:
return expr_to_type_kind(e->left);
case EXPR_ASSIGN:
case EXPR_NAME:
return e->symbol->type->kind;
case EXPR_CALL:
return e->symbol->type->rtype->kind;
default:
return TYPE_UNKNOWN;
}
}
int expr_is_const(struct expr *e)
{
if (!e) {
return 1;
}
switch (e->kind) {
case EXPR_INT:
case EXPR_BOOLEAN:
case EXPR_CHAR:
case EXPR_STRING:
return 1;
case EXPR_ASSIGN:
case EXPR_NAME:
case EXPR_CALL:
return 0;
default:
return expr_is_const(e->left) && expr_is_const(e->right);
}
}
int expr_has_effects(struct expr *e)
{
if (!e) {
return 0;
}
switch (e->kind) {
case EXPR_INT:
case EXPR_BOOLEAN:
case EXPR_CHAR:
case EXPR_STRING:
return 0;
case EXPR_ASSIGN:
case EXPR_CALL:
case EXPR_PRE_INCR:
case EXPR_PRE_DECR:
case EXPR_POST_INCR:
case EXPR_POST_DECR:
return 1;
default:
return expr_has_effects(e->left) || expr_has_effects(e->right);
}
}
struct stmt *stmt_make(enum stmt_kind kind, struct decl *decl, struct expr *expr, struct stmt *body, struct stmt *ebody)
{
struct stmt *s = NEW(stmt);
s->kind = kind;
s->decl = decl;
s->expr = expr;
s->body = body;
s->ebody = ebody;
s->next = NULL;
return s;
}
void stmt_free(struct stmt **sp)
{
if (!sp || !(*sp)) {
return;
}
struct stmt *s = *sp;
decl_free(&s->decl);
expr_free(&s->expr);
stmt_free(&s->body);
stmt_free(&s->ebody);
stmt_free(&s->next);
free(s);
*sp = 0;
}
struct symbol *symbol_make(enum symbol_kind kind, struct type *type, char *name, struct prog *prog)
{
struct symbol *s = NEW(symbol);
s->kind = kind;
s->which = 0;
s->type = type;
s->name = name;
s->offset = 0;
s->init = 0;
s->value = NULL;
s->num_reads = 0;
s->num_writes = 0;
if (prog) {
s->next = prog->symbols;
prog->symbols = s;
}
return s;
}
void symbol_free(struct symbol **sp)
{
if (!sp || !(*sp)) {
return;
}
struct symbol *s = *sp;
type_free(&s->type);
free(s->name);
expr_free(&s->value);
free(s->next);
free(s);
*sp = 0;
}
struct param * param_make(char *name, struct type *type, struct param *next)
{
struct param *p = NEW(param);
p->name = name;
p->type = type;
p->next = next;
return p;
}
void param_free(struct param **pp)
{
if (!pp || !(*pp)) {
return;
}
struct param *p = *pp;
free(p->name);
type_free(&p->type);
param_free(&p->next);
free(p);
*pp = 0;
}