-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathglobalsymboltable.c
341 lines (314 loc) · 8.23 KB
/
globalsymboltable.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
#include "globalsymboltable.h"
//Functions For Global Symbol Table
int currentBinding = 0;
/*
Add A new Variable to symbol Table
*/
struct Gsymbol * installVariable(char * name,struct typeTable * typeTableTuple, int arraySize){
struct Gsymbol *variable;
variable = malloc(sizeof(struct Gsymbol));
if(variable == NULL){
outOfMemory();
}
variable->name = malloc(strlen(name) + 1);
strcpy(variable->name,name);
variable->size = arraySize;
variable->next = NULL;
variable->type = typeTableTuple;
variable->binding = currentBinding;
currentBinding += arraySize;
if(!GsymbolHead){
GsymbolHead = variable;
GsymbolTail = variable;
}
else{
GsymbolTail->next = variable;
GsymbolTail = variable;
}
return variable;
}
/*
Add A new symbol if its type table entry is unknowns
Currently not used
*/
struct Gsymbol * installVariableRaw(char * name,char * type, int arraySize){
struct typeTable * typeTableTuple = getTypeTuple(type);
installVariable(name,typeTableTuple,arraySize);
}
/*
Install multiple symbols at a time
eg
int a,b,c;
varListContains list of variable names a,b,c
typeTableTupe contains the type table entry for 'int'
*/
void installAllVariables(struct Gsymbol * idList,struct typeTable * typeTableTuple){
struct Gsymbol * head;
head = idList;
while(head){
//printf("HELLO\n");
head->type = typeTableTuple;
head = head->next;
}
}
/*
To print all declared symbols (For Debugging)
*/
void printAllVariables(){
struct Gsymbol * head;
head = GsymbolHead;
while(head){
printf("\nName : %s \nSize : %d \nType : %s\nBinding : %d\n",head->name,head->size,head->type->name,head->binding);
if(head->binding == -11){
struct Lsymbol *start;
start = head->lsymbols;
printf("\n\tLocal Symbols\n");
while(start){
printf("\n\tName : %s \n\tSize : %d \n\tType : %s\n\tBinding : %d\n",start->name,start->size,start->type->name,start->binding);
start = start->next;
}
printf("\n\tArguments\n");
struct argList *begin;
begin = head->args;
while(begin){
printf("\n\tName : %s \n\tType : %s\n",begin->name,begin->type->name);
begin = begin->next;
}
}
head = head->next;
}
}
/*
Search Variable Binding From Symbol Table if only variable name is known
Shift = Index (for Array) or 0 (for other variables)
*/
int findVariableBinding(char * variableName, int shift){
struct Gsymbol * variable = getVariable(variableName);
if(variable && (shift < variable->size)){
return variable->binding + (shift*variable->type->size);
}
else{
yyerror("Index Out of Range");
return -1;
}
}
/*
Get Variable Binding If Symbol Table Entry is already Known
Shift = Index (for Array) or 0 (for other variables)
*/
int getVariableBinding(struct Gsymbol * variable, int shift){
if(variable && (shift < variable->size)){
return variable->binding + (shift*variable->type->size);
}
else{
return -1;
}
}
int getLocalVariableBinding(char * name,struct Lsymbol * variable){
while(variable){
if(strcmp(variable->name,name) == 0){
return variable->binding;
}
variable = variable->next;
}
return 0;
}
struct Lsymbol * getLocalTableEntry(char * name,struct Lsymbol * variable){
while(variable){
if(strcmp(variable->name,name) == 0){
return variable;
}
variable = variable->next;
}
return NULL;
}
/*
Get Variable's entry From Symbol Table
*/
struct Gsymbol * getVariable(char * variableName){
struct Gsymbol * variable;
variable = GsymbolHead;
while(variable){
if(strcmp(variable->name,variableName) == 0){
return variable;
}
variable = variable->next;
}
return NULL;
}
/*
Create a new variable pointer for the given type
*/
struct variablePointer * newVariablePointer(char * type){
struct variablePointer *varPointer;
varPointer = malloc(sizeof(struct variablePointer));
varPointer->type = malloc(strlen(type)+1);
strcpy(varPointer->type,type);
if(strcmp(type,"integer") == 0){
varPointer->intPointer = NULL;
}
else if(strcmp(type,"char") == 0){
varPointer->charPointer = NULL;
}
else if(strcmp(type,"bool") == 0){
varPointer->booleanPointer = NULL;
}
return varPointer;
}
/*
To store value the variable
Inputs : pointer to variable binding , pointer to the value to be copied to the variable{ both of type variablePointer }
*/
void writeToVariable(struct variablePointer *varPointer,struct variablePointer *writeBufferPointer){
if(strcmp(varPointer->type,"integer") == 0){
*varPointer->intPointer = *writeBufferPointer->intPointer;
}
else if(strcmp(varPointer->type,"char") == 0){
*varPointer->charPointer = *writeBufferPointer->charPointer;
}
else if(strcmp(varPointer->type,"boolean") == 0){
*varPointer->booleanPointer = *writeBufferPointer->booleanPointer;
}
}
struct argList * saveFarg(char * name,struct typeTable * typeTableTuple){
struct argList *arg;
arg = malloc(sizeof(struct argList));
if(arg == NULL){
outOfMemory();
}
arg->name = malloc(strlen(name) + 1);
strcpy(arg->name,name);
arg->next = NULL;
arg->type = typeTableTuple;
arg->isRef= 0;
return arg;
}
struct Gsymbol * installFunction(char * name,struct argList * args,struct typeTable * typeTableTuple, struct Lsymbol * lsymbols){
struct Gsymbol *function;
function = malloc(sizeof(struct Gsymbol));
if(function == NULL){
outOfMemory();
}
function->name = malloc(strlen(name) + 1);
strcpy(function->name,name);
function->size = 0;
function->next = NULL;
function->binding = -11;
function->lsymbols = lsymbols;
function->args = args;
function->type = typeTableTuple;
if(!GsymbolHead){
GsymbolHead = function;
GsymbolTail = function;
}
else{
GsymbolTail->next = function;
GsymbolTail = function;
}
return function;
}
struct Lsymbol * installLocalVariable(char * name,struct typeTable * typeTableTuple, int arraySize){
struct Lsymbol *variable;
variable = malloc(sizeof(struct Lsymbol));
if(variable == NULL){
outOfMemory();
}
variable->name = malloc(strlen(name) + 1);
strcpy(variable->name,name);
variable->size = arraySize;
variable->next = NULL;
variable->type = typeTableTuple;
return variable;
}
struct Lsymbol * installAllLocalVariables(struct Lsymbol * idList,struct typeTable * typeTableTuple){
struct Lsymbol * head;
head = idList;
while(head){
head->type = typeTableTuple;
head = head->next;
}
return idList;
}
struct Gsymbol * getSymbolTableTuple(char * name){
struct Gsymbol *head;
head = GsymbolHead;
while(head){
if(strcmp(head->name,name) == 0){
return head;
}
head = head->next;
}
return NULL;
}
void writeBindingsAndArgs(struct Lsymbol * localVar,struct argList *argument){
struct Lsymbol * head;
head = localVar;
int curBinding = 1;
while(head->next){
head->binding = curBinding++;
head = head->next;
}
head->binding = curBinding;
curBinding = -2;
struct argList * argHead;
struct Lsymbol * argLocalSymbol;
argHead = argument;
while(argHead){
argLocalSymbol = installLocalVariable(argHead->name,argHead->type,1);
argLocalSymbol->binding = --curBinding;
head->next = argLocalSymbol;
head = head->next;
argHead = argHead->next;
}
}
int isCorrectDeclaration(struct argList * argument,struct argList * actualArgument){
struct argList *head;
struct argList *actualHead;
head = argument;
actualHead = actualArgument;
while(head && actualHead){
if((strcmp(head->name,actualHead->name)!=0)||(head->type != actualHead->type)){
break;
}
if(head->isRef != actualHead->isRef){
break;
}
head = head->next;
actualHead = actualHead->next;
}
if(head || actualHead)
return 0;
return 1;
}
int checkMultipleDeclaration(){
struct Gsymbol * head1;
struct Gsymbol * head2;
head1 = GsymbolHead;
head2 = GsymbolHead->next;
while(head1){
while(head2){
if(strcmp(head1->name,head2->name) == 0){
printf("Error : Multiple declarations for %s\n",head1->name);
return 1;
}
head2 = head2->next;
}
head1 = head1->next;
}
return 0;
}
int checkMultipleLocalDeclaration(struct Lsymbol * head1){
struct Lsymbol * head2;
head2 = head1->next;
while(head1){
while(head2){
if(strcmp(head1->name,head2->name) == 0){
printf("Error : Multiple declarations for %s\n",head1->name);
return 1;
}
head2 = head2->next;
}
head1 = head1->next;
}
return 0;
}