-
Notifications
You must be signed in to change notification settings - Fork 0
/
sym.c
448 lines (400 loc) · 11.8 KB
/
sym.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
/* sym.c */
#include "zcc.h"
#include <memory.h>
#include <stdlib.h>
#include <stdio.h>
#include <assert.h>
context_st context;
/* =---- Symbol manager ----= */
type_st* type_int;
type_st* type_int_ptr;
type_st* type_char;
type_st* type_char_ptr;
func_st* wrap_func;
static list_st *disposed_temp_vars;
int sym_init() {
/* initialize context */
context.funcs = make_list();
context.func = wrap_func = sym_make_func("(top)", NULL); /* virtual wrap function */
context.scope = sym_make_scope();
context.types = make_list();
context.cstr = make_list();
/* add native types */
type_int = malloc(sizeof(int));
type_int->bytes = sizeof(int);
type_int->native = 1;
type_int->ref = NULL;
type_int->name = "int";
sym_add_type(type_int);
type_int_ptr = malloc(sizeof(int*));
type_int_ptr->bytes = sizeof(int*);
type_int_ptr->native = 1;
type_int_ptr->ref = type_int;
type_int_ptr->name = NULL;
sym_add_type(type_int_ptr);
type_char = malloc(sizeof(char));
type_char->bytes = sizeof(char);
type_char->native = 1;
type_char->ref = NULL;
type_char->name = "char";
sym_add_type(type_char);
type_char_ptr = malloc(sizeof(char*));
type_char_ptr->bytes = sizeof(char*);
type_char_ptr->native = 1;
type_char_ptr->ref = type_char;
type_char_ptr->name = NULL;
sym_add_type(type_char_ptr);
disposed_temp_vars = NULL;
return 0;
}
void sym_add_type(type_st *type) {
list_append(context.types, type);
}
scope_st* sym_make_scope() {
scope_st *scope = malloc(sizeof(scope_st));
scope->up = NULL;
scope->vars = make_list();
scope->forB = scope->forE = scope->forC = NULL;
return scope;
}
void sym_push_scope(scope_st *scope) {
scope->up = context.scope;
scope->forB = scope->up->forB;
scope->forC = scope->up->forC;
scope->forE = scope->up->forE;
context.scope = scope;
}
void sym_pop_scope() {
assert(context.scope != NULL);
context.scope = context.scope->up;
}
scope_st* sym_mnp_scope() {
scope_st *scope = sym_make_scope();
sym_push_scope(scope);
return scope;
}
var_st* sym_make_var(char* name, type_st* type) {
if(sym_redefined_var(name)) {
fprintf(stderr, "Fatal: var[name=%s] redefine.\n", name);
fexit("");
return NULL;
}
var_st *var = malloc(sizeof(var_st));
var->name = dup_str(name);
var->type = type;
var->value = NULL;
var->ispar = 0;
var->lvalue = 1;
var->irname = NULL;
var->addr = 0;
sym_add_var(var);
return var;
}
/* add to scope */
var_st* sym_add_var(var_st *var) {
func_st *func = context.func;
scope_st *scope = context.scope;
list_st *vars = scope->vars;
list_append(vars, var);
var->where = STACK_VAR;
var->ref = 0;
/* allocate space for local variable in stack frame */
if(func != wrap_func) {
var->addr = func->rbytes;
func->rbytes += 8; /* max size for var is 8 bytes */
}
return var;
}
var_st* sym_find_var(char *name) {
scope_st *scope = context.scope;
list_st *vars;
int i;
while(scope) {
vars = scope->vars;
assert(vars);
for(i = 0; i < vars->len; i++) {
if(strcmp(name, ((var_st*)vars->elems[i])->name) == 0)
return vars->elems[i];
}
scope = scope->up;
}
return NULL;
}
func_st* sym_find_func(char *name) {
list_st *funcs = context.funcs;
int i;
for(i = 0; i < funcs->len; i++)
if(strcmp(name, ((func_st*)funcs->elems[i])->name) == 0)
return funcs->elems[i];
return NULL;
}
var_st* sym_make_temp_var(type_st *type) {
var_st *var;
func_st *func = context.func;
assert(func != wrap_func);
if(!list_isempty(disposed_temp_vars)) {
var = list_pop(disposed_temp_vars);
}
else {
var = malloc(sizeof(var_st));
var->irname = NULL;
/* allocate space in stack frame */
var->where = STACK_VAR;
var->ref = 0;
var->addr = func->rbytes;
func->rbytes += 8; /* always align to 8 bytes */
/* dont add to scope */
}
var->name = NULL;
var->type = type;
var->value = NULL;
var->ispar = 0;
var->lvalue = 0;
return var;
}
/* TODO: explain ref */
var_st* sym_make_temp_lvar(type_st *type, int ref) {
var_st *var = sym_make_temp_var(type);
var->lvalue = 1;
var->ref = ref;
return var;
}
void sym_dispose_temp_var(var_st *var) {
if(var->lvalue == 0 && var->value == NULL) {
var->value = NULL;
list_append(disposed_temp_vars, var);
}
}
var_st* sym_make_imm(token_st* tk) {
var_st *var = malloc(sizeof(var_st));
var->irname = NULL;
var->addr = -1;
/* support only int for now */
if(tk->tk_class == TK_CONST_INT) {
var->type = type_int;
var->name = NULL;
var->value = dup_value(tk);
var->ispar = 0;
var->lvalue = 0;
}
else if(tk->tk_class == TK_CONST_STRING) {
var->type = type_char_ptr;
var->name = NULL;
var->value = dup_value(tk);
//fprintf(stderr, "var->value->s=%s\n", var->value->s);
var->ispar = 0;
var->lvalue = 0;
list_append(context.cstr, var);
}
else {
fexit("Unsupported const type");
}
return var;
}
int sym_redefined_var(char *name) {
scope_st *scope = context.scope;
list_st *vars = scope->vars;
int i;
assert(vars);
for(i = 0; i < vars->len; i++) {
if(strcmp(name, ((var_st*)vars->elems[i])->name) == 0)
return 1;
}
return 0;
}
/* make par and add to scope */
var_st* sym_make_par(char* name, type_st* type) {
static func_st *func = NULL;
static int par_ind;
if(context.func != func) {
func = context.func;
par_ind = 0;
}
else par_ind++;
list_st *pars = func->pars;
if(func->declared) {
/* check declaration consistency */
var_st *var = pars->elems[par_ind];
assert(par_ind < pars->len);
assert(var->type == type);
sym_add_var(var);
return var;
}
else {
/* add new parameter to list */
if(sym_redefined_var(name)) {
fprintf(stderr, "Fatal: var[name=%s] redefine.\n", name);
fexit("");
return NULL;
}
var_st *var = malloc(sizeof(var_st));
var->name = dup_str(name);
var->type = type;
var->value = NULL;
var->ispar = pars->len + 1;
var->lvalue = 1;
var->irname = NULL;
list_append(pars, var);
/* add to scope. Note that sym_add_var() will allocate space in stack frame.
* this is intended for it's easier to manipulte linear stack-placed args
* (rather than part in register and part on stack)
*/
sym_add_var(var);
return var;
}
}
/* make var and set it as context.func */
func_st* sym_make_func(char* name, type_st* rtype) {
func_st *func = sym_find_func(name);
if(func) {
assert(rtype == func->rtype);
if(disposed_temp_vars) free(disposed_temp_vars);
disposed_temp_vars = make_list();
context.func = func;
return func;
}
func = malloc(sizeof(func_st));
func->name = dup_str(name);
func->rtype = rtype;
func->pars = make_list();
func->insts = make_list();
func->ret = NULL;
func->rbytes = 0;
func->declared = 0;
func->defined = 0;
assert(context.func == NULL || strcmp(context.func->name, "(top)") == 0); /* dont allow nested function */
context.func = func;
list_append(context.funcs, func); /* should append early to allow recursive */
if(disposed_temp_vars) free(disposed_temp_vars);
disposed_temp_vars = make_list();
/* reserve space in stack frame for return value
* this simplify IR_RETURN. (i.e. not need to pass argument by IR_RETURN)
*/
func->ret = rtype ? sym_make_temp_var(rtype) : NULL;
return func;
}
/* workaround for call to printf when pre-declaration is not implemented */
func_st* sym_make_temp_func(char* name, type_st* rtype) {
func_st *func = malloc(sizeof(func_st));
func->name = dup_str(name);
func->rtype = rtype;
func->pars = NULL;
func->insts = NULL;
func->ret = NULL;
func->rbytes = 0;
func->declared = 0;
func->defined = 0;
return func;
}
int sym_hasid(token_st *tk) {
/* TODO */
return 1;
}
int sym_hastype(token_st *tk) {
/* TODO */
if(tk->tk_class == TK_INT) return 1;
return 0;
}
int sym_islabel(token_st* tk) {
/* TODO */
return 0;
}
int sym_is_pointer(var_st* v) {
return v->type->ref != NULL;
}
int sym_is_full_pars(func_st* f) {
list_st *pars = f->pars;
int i, yes = 1;
for(i = 0; i < pars->len; i++)
if(((var_st*)pars->elems[i])->name == NULL) {
yes = 0;
break;
}
return yes;
}
label_st* sym_make_label() {
label_st *label = malloc(sizeof(label_st));
label->func = context.func;
label->chain = NULL;
label->irname = NULL;
return label;
}
var_st* sym_deref(var_st *v) {
var_st *r;
if(v->ref == 0) {
r = malloc(sizeof(var_st));
*r = *v;
r->type = r->type->ref;
r->lvalue = 1;
r->ref = 1;
}
else {
/* case study:
* v is of type T*, v->ref == 1
* then v stores pointer to a pointer
* i.e. v --> &T --> T
* to derefence v, allocate a temp var r to store &T
* retrieve &T by assigning v to r
* then set r->ref = 1 so that r is of type T
*/
r = sym_make_temp_var(v->type);
gen_emit(IR_ASSIGN, r, v);
r->type = r->type->ref;
r->lvalue = 1;
r->ref = 1;
}
return r;
}
type_st* pointer_of(type_st* type) {
list_st *types = context.types;
int i = 0;
for(; i < types->len; i++) {
if(((type_st*)types->elems[i])->ref == type)
return types->elems[i];
}
/* pointer type not found. create one */
type_st *ntype = malloc(sizeof(type_st));
ntype->bytes = 8;
ntype->native = 0;
ntype->ref = type;
ntype->name = NULL;
sym_add_type(ntype);
return ntype;
}
type_st* binop_res_type(ir_op_en op, type_st *x, type_st *y) {
if(x == type_int) {
if(y == type_int)
return type_int;
else if(y == type_char)
return type_int;
else if(y == type_int_ptr && (op == IR_ADD || op == IR_SUB))
return type_int_ptr;
else
assert("Type error");
}
else if(x == type_int_ptr) {
if(y == type_int && (op == IR_ADD || op == IR_SUB))
return type_int_ptr;
else if(y == type_int_ptr && op == IR_SUB)
return type_int;
}
else if(x == type_char) {
if(y == type_int)
return type_int;
else if(y == type_char)
return type_char;
else if(y == type_char_ptr && (op == IR_ADD || op == IR_SUB))
return type_int_ptr;
else
assert("Type error");
}
else if(x == type_char_ptr) {
if(y == type_int && (op == IR_ADD || op == IR_SUB))
return type_char_ptr;
else if(y == type_char_ptr && op == IR_SUB)
return type_int;
}
else
fexit("Not supported types");
return NULL;
}