-
Notifications
You must be signed in to change notification settings - Fork 17
/
ast.cc
447 lines (372 loc) · 8.61 KB
/
ast.cc
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
/******************************************************************************
Copyright (c) 1992, 1995, 1996 Xerox Corporation. All rights reserved.
Portions of this code were written by Stephen White, aka ghond.
Use and copying of this software and preparation of derivative works based
upon this software are permitted. Any distribution of this software or
derivative works must comply with all applicable United States export
control laws. This software is made available AS IS, and Xerox Corporation
makes no warranty about the software, its performance or its conformity to
any specification. Any person obtaining a copy of this software is requested
to send their name and post office or electronic mail address to:
Pavel Curtis
Xerox PARC
3333 Coyote Hill Rd.
Palo Alto, CA 94304
*****************************************************************************/
#include "my-string.h"
#include "ast.h"
#include "config.h"
#include "list.h"
#include "log.h"
#include "program.h"
#include "structures.h"
#include "sym_table.h"
#include "storage.h"
#include "utils.h"
struct entry {
void *ptr;
Memory_Type type;
};
static int pool_size, next_pool_slot;
static struct entry *pool;
void
begin_code_allocation()
{
pool_size = 10;
next_pool_slot = 0;
pool = (struct entry *)mymalloc(pool_size * sizeof(struct entry), M_AST_POOL);
}
void
end_code_allocation(int aborted)
{
if (aborted) {
int i;
for (i = 0; i < next_pool_slot; i++) {
if (pool[i].ptr != 0)
myfree(pool[i].ptr, pool[i].type);
}
}
myfree(pool, M_AST_POOL);
}
static void *
allocate(int size, Memory_Type type)
{
if (next_pool_slot >= pool_size) { /* enlarge the pool */
struct entry *new_pool;
int i;
pool_size *= 2;
new_pool = (struct entry *)mymalloc(pool_size * sizeof(struct entry), M_AST_POOL);
for (i = 0; i < next_pool_slot; i++) {
new_pool[i] = pool[i];
}
myfree(pool, M_AST_POOL);
pool = new_pool;
}
pool[next_pool_slot].type = type;
return pool[next_pool_slot++].ptr = mymalloc(size, type);
}
static void
deallocate(void *ptr)
{
int i;
for (i = 0; i < next_pool_slot; i++) {
if (ptr == pool[i].ptr) {
myfree(ptr, pool[i].type);
pool[i].ptr = 0;
return;
}
}
errlog("DEALLOCATE: Unknown pointer deallocated\n");
}
char *
alloc_string(const char *buffer)
{
char *string = (char *)allocate(strlen(buffer) + 1, M_STRING);
strcpy(string, buffer);
return string;
}
void
dealloc_string(char *str)
{
deallocate(str);
}
double *
alloc_float(double value)
{
double *d = (double *)allocate(sizeof(double), M_FLOAT);
*d = value;
return d;
}
void
dealloc_node(void *node)
{
deallocate(node);
}
Stmt *
alloc_stmt(enum Stmt_Kind kind)
{
Stmt *result = (Stmt *)allocate(sizeof(Stmt), M_AST);
result->kind = kind;
result->next = 0;
return result;
}
Cond_Arm *
alloc_cond_arm(Expr * condition, Stmt * stmt)
{
Cond_Arm *result = (Cond_Arm *)allocate(sizeof(Cond_Arm), M_AST);
result->condition = condition;
result->stmt = stmt;
result->next = 0;
return result;
}
Except_Arm *
alloc_except(int id, Arg_List * codes, Stmt * stmt)
{
Except_Arm *result = (Except_Arm *)allocate(sizeof(Except_Arm), M_AST);
result->id = id;
result->codes = codes;
result->stmt = stmt;
result->label = 0;
result->next = 0;
return result;
}
Expr *
alloc_expr(enum Expr_Kind kind)
{
Expr *result = (Expr *)allocate(sizeof(Expr), M_AST);
result->kind = kind;
return result;
}
Expr *
alloc_var(var_type type)
{
Expr *result = (Expr *)alloc_expr(EXPR_VAR);
result->e.var.type = type;
return result;
}
Expr *
alloc_binary(enum Expr_Kind kind, Expr * lhs, Expr * rhs)
{
Expr *result = (Expr *)alloc_expr(kind);
result->e.bin.lhs = lhs;
result->e.bin.rhs = rhs;
return result;
}
Expr *
alloc_verb(Expr * obj, Expr * verb, Arg_List * args)
{
Expr *result = (Expr *)alloc_expr(EXPR_VERB);
result->e.verb.obj = obj;
result->e.verb.verb = verb;
result->e.verb.args = args;
return result;
}
Map_List *
alloc_map_list(Expr * key, Expr * value)
{
Map_List *result = (Map_List *)allocate(sizeof(Map_List), M_AST);
result->key = key;
result->value = value;
result->next = 0;
return result;
}
Arg_List *
alloc_arg_list(enum Arg_Kind kind, Expr * expr)
{
Arg_List *result = (Arg_List *)allocate(sizeof(Arg_List), M_AST);
result->kind = kind;
result->expr = expr;
result->next = 0;
return result;
}
Scatter *
alloc_scatter(enum Scatter_Kind kind, int id, Expr * expr)
{
Scatter *sc = (Scatter *)allocate(sizeof(Scatter), M_AST);
sc->kind = kind;
sc->id = id;
sc->expr = expr;
sc->next = 0;
sc->label = sc->next_label = 0;
return sc;
}
static void free_expr(Expr *);
static void
free_map_list(Map_List * map)
{
Map_List *m, *next_m;
for (m = map; m; m = next_m) {
next_m = m->next;
free_expr(m->key);
free_expr(m->value);
myfree(m, M_AST);
}
}
static void
free_arg_list(Arg_List * args)
{
Arg_List *arg, *next_arg;
for (arg = args; arg; arg = next_arg) {
next_arg = arg->next;
free_expr(arg->expr);
myfree(arg, M_AST);
}
}
static void
free_scatter(Scatter * sc)
{
Scatter *next_sc;
for (; sc; sc = next_sc) {
next_sc = sc->next;
if (sc->expr)
free_expr(sc->expr);
myfree(sc, M_AST);
}
}
static void
free_expr(Expr * expr)
{
switch (expr->kind) {
case EXPR_VAR:
free_var(expr->e.var);
break;
case EXPR_ID:
case EXPR_FIRST:
case EXPR_LAST:
/* Do nothing. */
break;
case EXPR_PROP:
case EXPR_INDEX:
case EXPR_PLUS:
case EXPR_MINUS:
case EXPR_TIMES:
case EXPR_DIVIDE:
case EXPR_MOD:
case EXPR_AND:
case EXPR_OR:
case EXPR_EQ:
case EXPR_NE:
case EXPR_LT:
case EXPR_LE:
case EXPR_GT:
case EXPR_GE:
case EXPR_IN:
case EXPR_ASGN:
case EXPR_EXP:
case EXPR_BITOR:
case EXPR_BITAND:
case EXPR_BITXOR:
case EXPR_BITSHL:
case EXPR_BITSHR:
free_expr(expr->e.bin.lhs);
free_expr(expr->e.bin.rhs);
break;
case EXPR_COND:
free_expr(expr->e.cond.condition);
free_expr(expr->e.cond.consequent);
free_expr(expr->e.cond.alternate);
break;
case EXPR_VERB:
free_expr(expr->e.verb.obj);
free_expr(expr->e.verb.verb);
free_arg_list(expr->e.verb.args);
break;
case EXPR_RANGE:
free_expr(expr->e.range.base);
free_expr(expr->e.range.from);
free_expr(expr->e.range.to);
break;
case EXPR_CALL:
free_arg_list(expr->e.call.args);
break;
case EXPR_NEGATE:
case EXPR_NOT:
case EXPR_COMPLEMENT:
free_expr(expr->e.expr);
break;
case EXPR_MAP:
free_map_list(expr->e.map);
break;
case EXPR_LIST:
free_arg_list(expr->e.list);
break;
case EXPR_CATCH:
free_expr(expr->e._catch._try);
free_arg_list(expr->e._catch.codes);
if (expr->e._catch.except)
free_expr(expr->e._catch.except);
break;
case EXPR_SCATTER:
free_scatter(expr->e.scatter);
break;
default:
errlog("FREE_EXPR: Unknown Expr_Kind: %d\n", expr->kind);
break;
}
myfree(expr, M_AST);
}
void
free_stmt(Stmt * stmt)
{
Stmt *next_stmt;
Cond_Arm *arm, *next_arm;
Except_Arm *except, *next_e;
for (; stmt; stmt = next_stmt) {
next_stmt = stmt->next;
switch (stmt->kind) {
case STMT_COND:
for (arm = stmt->s.cond.arms; arm; arm = next_arm) {
next_arm = arm->next;
free_expr(arm->condition);
free_stmt(arm->stmt);
myfree(arm, M_AST);
}
if (stmt->s.cond.otherwise)
free_stmt(stmt->s.cond.otherwise);
break;
case STMT_LIST:
free_expr(stmt->s.list.expr);
free_stmt(stmt->s.list.body);
break;
case STMT_RANGE:
free_expr(stmt->s.range.from);
free_expr(stmt->s.range.to);
free_stmt(stmt->s.range.body);
break;
case STMT_WHILE:
free_expr(stmt->s.loop.condition);
free_stmt(stmt->s.loop.body);
break;
case STMT_FORK:
free_expr(stmt->s.fork.time);
free_stmt(stmt->s.fork.body);
break;
case STMT_EXPR:
case STMT_RETURN:
if (stmt->s.expr)
free_expr(stmt->s.expr);
break;
case STMT_TRY_EXCEPT:
free_stmt(stmt->s._catch.body);
for (except = stmt->s._catch.excepts; except; except = next_e) {
next_e = except->next;
free_arg_list(except->codes);
free_stmt(except->stmt);
myfree(except, M_AST);
}
break;
case STMT_TRY_FINALLY:
free_stmt(stmt->s.finally.body);
free_stmt(stmt->s.finally.handler);
break;
case STMT_BREAK:
case STMT_CONTINUE:
break; /* Nothing extra to free */
default:
errlog("FREE_STMT: unknown Stmt_Kind: %d\n", stmt->kind);
break;
}
myfree(stmt, M_AST);
}
}