-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathsymtab.c
240 lines (196 loc) · 5.15 KB
/
symtab.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
/*build symbol tables
* and function tables
*/
#include "globals.h"
#include "symtab.h"
/* tables store different symbol tables,
* incluidng global, local and param.
*/
SymbolTable* tables = NULL;
/* funs store all function symbols */
FunSymbol* funs = NULL;
/* temporary table used to allocate small symtabs for Compound & Param */
SymbolTable* CompoundST;
SymbolTable* ParamST;
/* the hash function */
int hash ( char * key )
{
int temp = 0;
int i = 0;
while (key[i] != '\0')
{ temp = ((temp << SHIFT) + key[i]) % SIZE;
++i;
}
return temp;
}
/* Note: we insert for input()&output() here*/
void initTable()
{
CompoundST = newSymbolTable(LOCAL);
ParamST = newSymbolTable(PARAM);
tables = newSymbolTable(GLOBAL);
insert_fun("input", ParamST, 0, TYPE_INTEGER);
ParamST = newSymbolTable(PARAM);
pushTable(ParamST);
insert_var("i", PARAM, ParamST->size++, TYPE_INTEGER);
popTable();
insert_fun("output", ParamST, ParamST->size, TYPE_VOID);
ParamST = newSymbolTable(PARAM);
}
/* create a new symbol table of certain scope */
SymbolTable* newSymbolTable(Scope scope)
{
int i;
SymbolTable* st = (SymbolTable*)malloc(sizeof(SymbolTable));
ASSERT(st != NULL){
fprintf(stderr, "Failed to malloc for symbal table.\n");
}
st->scope = scope;
st->size = 0;
st->next = NULL;
st->varList = NULL;
for(i = 0;i<SIZE;i++)
st->hashTable[i] = NULL;
return st;
}
/* manipulate the symbol table stack*/
SymbolTable* topTable()
{
return tables;
}
SymbolTable* popTable()
{
ASSERT(tables != NULL){
fprintf(stderr, "Pop an empty table list.\n");
}
SymbolTable* st = tables;
tables = tables->next;
return st;
}
void pushTable(SymbolTable* st)
{
ASSERT(st != NULL){
fprintf(stderr, "Push an null table.\n");
}
st->next = tables;
tables = st;
}
/* look up for variables in top symbol table*/
VarSymbol* lookup_var_top(char* name)
{
if(tables == NULL)
return NULL;
VarSymbol* l;
int h = hash(name);
for(l = tables->hashTable[h]; l!=NULL; l=l->next){
if(strcmp(l->name, name) == 0 )
break;
}
return l;
}
/* lookup for all tables in the stack */
VarSymbol* lookup_var (char * name)
{
if(tables == NULL)
return NULL;
int h = hash(name);
SymbolTable* st;
VarSymbol* l;
for(st = tables; st!=NULL; st=st->next) /* iteration of all symbol tables in stack */
{
for(l = st->hashTable[h]; l!=NULL; l=l->next) /* iteration of all linkedlist in a symboltable */
{
if(strcmp(l->name, name)==0)
return l;
}
}
return NULL; /* may be NULL */
}
/* look up for a function symbol*/
FunSymbol* lookup_fun(char * name)
{
if(funs == NULL)
return NULL;
FunSymbol* fs;
for(fs=funs;fs!=NULL; fs = fs->next)
{
if(strcmp(fs->name, name)==0)
break;
}
return fs;
}
/* Always insert var into the top symbol table*/
int insert_var(char * name, Scope scope, int offset, ExpType type)
{
VarSymbol* l, *tmp;
int h = hash(name);
/*Check duplication*/
if(tables == NULL){
l = NULL;
}
else{
l = tables->hashTable[h];
while ((l != NULL) && (strcmp(name,l->name) != 0))
l = l->next;
}
if (l != NULL){
fprintf(stderr, "Duplicate declarations of variable: %s.\n", name);
return 1;
}
l = (VarSymbol*) malloc(sizeof(VarSymbol));
ASSERT(l != NULL){
fprintf(stderr, "Failed to malloc for VarSymbol.\n" );
}
l->name = strdup(name);
l->scope = scope;
l->type = type;
l->offset = offset;
l->next = tables->hashTable[h];
tables->hashTable[h] = l;
l->next_FIFO = NULL;
if(tables->varList == NULL){ /*First insertion*/
tables->varList = l;
}
else{
for(tmp=tables->varList; tmp->next_FIFO != NULL; tmp = tmp->next_FIFO);
tmp->next_FIFO = l;
}
return 0;
}
int insert_fun(char* name, SymbolTable* st, int num, ExpType type)
{
FunSymbol* fs;
/*Check duplication*/
if(lookup_fun(name) != NULL){
fprintf(stderr, "Duplicate declarations of function: %s\n", name);
return 1;
}
fs = (FunSymbol*)malloc(sizeof(FunSymbol));
ASSERT(fs != NULL){
fprintf(stderr, "Failed to malloc for FunSymbol.\n");
}
fs->name = strdup(name);
fs->type = type;
fs->paramNum = num;
fs->symbolTable = st;
fs->next = funs;
funs = fs;
return 0;
}
void printSymTab(SymbolTable* st)
{
int i;
fprintf(listing,"Variable Name Offset\n");
fprintf(listing,"------------- ------\n");
VarSymbol* vs = NULL;
for (i=0;i<SIZE;++i)
{
for(vs = st->hashTable[i]; vs != NULL; vs=vs->next)
{
fprintf(listing, "%-14s", vs->name);
fprintf(listing, "%-8d", vs->offset);
fprintf(listing, "\n");
}
}
fprintf(listing, "\n");
}