-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsymtab.cpp
More file actions
353 lines (323 loc) · 8.61 KB
/
symtab.cpp
File metadata and controls
353 lines (323 loc) · 8.61 KB
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
#include <string>
#include <iostream>
#include <bits/stdc++.h>
#pragma once
#define SIZE 523
#define SHIFT 4
using namespace std;
enum TypeID{VarType, FuncType};
enum DataType{Int, IntPointer, Void};
typedef struct LineListRec{
int lineno;
LineListRec* next;
}*LineList;
typedef struct BucketListRec{
string id;
TypeID type_id;
string scope;
DataType data_type;
LineList lines;
BucketListRec* next;
int mem_loc;
int mem_pos;
int var_amount;
vector<string> variables;
bool value_in_register;
bool is_parameter;
string loc_register;
}*BucketList;
static BucketList hashTable[SIZE];
int hashTab(string key){
int temp = 0;
int i = 0;
for(i = 0; i < key.length(); i++)
{
temp = ((temp << SHIFT) + (int)key[i]) % SIZE;
++i;
}
return temp;
}
void insertAllocMemScope(string scope, int mem_loc){
int pos = hashTab(scope+" ");
BucketList l = hashTable[pos];
while(l!=NULL){
if(scope.compare(l->id)==0) break;
l = l->next;
}
l->mem_loc = l->mem_loc + mem_loc;
}
void insertVarInScope(string scope, string id){
int pos = hashTab(scope+" ");
BucketList l = hashTable[pos];
while(l!=NULL){
if(scope.compare(l->id)==0) break;
l = l->next;
}
l->var_amount = l->var_amount + 1;
l->variables.push_back(id);
}
bool insertSymTab(string id, TypeID type_id, string scope, DataType data_type, int lineno, int mem_loc, bool is_param){
int pos = hashTab(id+scope);
BucketList l = hashTable[pos];
while(l!=NULL){
if(id.compare(l->id) == 0 && scope.compare(l->scope) == 0 ) return false;
l = l->next;
}
BucketList newElm = (BucketList) malloc(sizeof(struct BucketListRec));
newElm->id = id;
newElm->type_id = type_id;
newElm->scope = scope;
newElm->data_type = data_type;
newElm->lines = (LineList) malloc(sizeof(struct LineListRec));
newElm->lines->lineno = lineno;
newElm->lines->next = NULL;
newElm->next = hashTable[pos];
newElm->mem_loc = mem_loc;
newElm->mem_pos = -1;
newElm->var_amount = 0;
newElm->loc_register = " ";
newElm->value_in_register = false;
newElm->is_parameter = is_param;
if(scope!=" "){
insertAllocMemScope(scope, mem_loc);
}
else if(mem_loc!=0){
insertAllocMemScope("GLOBAL", mem_loc);
}
hashTable[pos] = newElm;
return true;
}
bool existID(string id, string scope){
int pos = hashTab(id+scope);
BucketList l = hashTable[pos];
while(l!=NULL){
if(id.compare(l->id) == 0 && scope.compare(l->scope) == 0 ) return true;
l = l->next;
}
pos = hashTab(id+" ");
l = hashTable[pos];
while(l!=NULL){
if(id.compare(l->id) == 0) return true;
l = l->next;
}
return false;
}
bool existIdEveryScope(string id){
for(int i = 0; i < SIZE; i++){
BucketList l = hashTable[i];
while(l!=NULL){
if(l->id.compare(id) == 0) return true;
l = l->next;
}
}
return false;
}
string showLines(LineList l){
string ret = "";
while(l!=NULL){
ret = ret + to_string(l->lineno);
if(l->next!=NULL)
ret = ret + " ";
l = l ->next;
}
return ret;
}
string stringDataType(DataType t){
if(t == Int) return "int";
else if(t== IntPointer) return "int*";
else return "void";
}
string stringTypeID(TypeID t){
if(t == VarType) return "var";
else return "func";
}
string vectorString(vector<string> vectorString){
string ret = "";
for(int i=0;i<vectorString.size(); i++){
if(i==0)
ret = vectorString[i];
else
ret = ret + ", " + vectorString[i];
}
return ret;
}
static string symbTabString = "";
string formatTabCeil(string info, int index){
int sizes[9] = {13, 15, 13, 16, 14, 40, 15, 15, 15};
if(index<9){
while(info.size()<sizes[index]){
info = info + " ";
}
}
return info;
}
void showSymbTab(){
symbTabString = "";
bool hasMoreThanOne;
for(int i = 0; i < SIZE; i++){
BucketList l = hashTable[i];
while(l!=NULL){
symbTabString = symbTabString + formatTabCeil("ID: "+l->id, 0);
symbTabString = symbTabString + formatTabCeil("SCOPE: "+l->scope, 1);
if(l->is_parameter){
symbTabString = symbTabString + formatTabCeil("PARAM: true", 2);
}
else{
symbTabString = symbTabString + formatTabCeil("PARAM: false", 2);
}
symbTabString = symbTabString + formatTabCeil("DATA TYPE: "+stringDataType(l->data_type), 3);
symbTabString = symbTabString + formatTabCeil("TYPE ID: "+stringTypeID(l->type_id), 4);
symbTabString = symbTabString + formatTabCeil("LINES: "+showLines(l->lines), 5);
symbTabString = symbTabString + formatTabCeil("MEMLOC: "+to_string(l->mem_loc), 6);
symbTabString = symbTabString + formatTabCeil("MEMPOS: "+to_string(l->mem_pos), 7);
symbTabString = symbTabString + formatTabCeil("VARAMOUNT: "+to_string(l->var_amount), 8);
if(l->value_in_register && stringTypeID(l->type_id)=="var")
symbTabString = symbTabString + formatTabCeil("REG: "+l->loc_register, 9);
else if(stringTypeID(l->type_id)=="var")
symbTabString = symbTabString + formatTabCeil("REG: mem["+l->loc_register+"]", 9);
if(stringTypeID(l->type_id)=="func")
symbTabString = symbTabString + formatTabCeil("VARS: ["+vectorString(l->variables)+"]", 9);
symbTabString = symbTabString + "\n";
l = l->next;
}
}
}
bool checkMain(){
for(int i = 0; i < SIZE; i++){
BucketList l = hashTable[i];
while(l!=NULL){
if(l->id.compare("main") == 0 && l->type_id == FuncType) return true;
l = l->next;
}
}
return false;
}
DataType getDataType(string id, string scope){
int pos = hashTab(id+scope);
BucketList l = hashTable[pos];
while(l->id != id) l = l ->next;
return l->data_type;
}
TypeID getTypeID(string id, string scope){
int pos = hashTab(id+scope);
BucketList l = hashTable[pos];
while(l!=NULL){
if(id.compare(l->id) == 0 && scope.compare(l->scope) == 0 ) return l->type_id;
l = l->next;
}
pos = hashTab(id+" ");
l = hashTable[pos];
while(l!=NULL){
if(id.compare(l->id) == 0) return l->type_id;
l = l->next;
}
}
bool variableIsArray(string id, string scope){
int pos = hashTab(id+scope);
BucketList l = hashTable[pos];
while(l!=NULL){
if(id.compare(l->id) == 0 && scope.compare(l->scope) == 0 ){
if(l->data_type==IntPointer)
return true;
else
return false;
}
l = l->next;
}
pos = hashTab(id+" ");
l = hashTable[pos];
while(l!=NULL){
if(id.compare(l->id) == 0){
if(l->data_type==IntPointer)
return true;
else
return false;
}
l = l->next;
}
return false;
}
bool checkVoid(treeNode *tree){
if(tree->nodeKind == CallK){
if(getDataType(tree->name," ") == Void) return true;
}
return false;
}
bool checkAtr(treeNode *tree){
if(tree->nodeKind == OpK){
if(tree->name.compare("=") == 0) return true;
}
return false;
}
void insertLineID(string id, string scope, int lineno){
int pos = hashTab(id+scope);
BucketList l = hashTable[pos];
while(l!=NULL){
if(id.compare(l->id) == 0 && scope.compare(l->scope) == 0 ) break;
l = l->next;
}
if(l!=NULL){
LineList newLine = (LineList) malloc(sizeof(struct LineListRec));
newLine->lineno = lineno;
newLine->next = l->lines;
l->lines = newLine;
}
pos = hashTab(id+" ");
l = hashTable[pos];
while(l!=NULL){
if(id.compare(l->id) == 0 && scope.compare(l->scope) == 0 ) break;
l = l->next;
}
if(l!=NULL){
LineList newLine = (LineList) malloc(sizeof(struct LineListRec));
newLine->lineno = lineno;
newLine->next = l->lines;
l->lines = newLine;
}
}
void insertLineIDGlobal(string id, int lineno){
int pos = hashTab(id+" ");
BucketList l = hashTable[pos];
while(l!=NULL){
if(id.compare(l->id) == 0) break;
l = l->next;
}
if(l!=NULL){
LineList newLine = (LineList) malloc(sizeof(struct LineListRec));
newLine->lineno = lineno;
newLine->next = l->lines;
l->lines = newLine;
}
}
string getDataTypeElement(string id, string scope){
int pos = hashTab(id+scope);
BucketList l = hashTable[pos];
while(l!=NULL){
if(id.compare(l->id) == 0 && scope.compare(l->scope) == 0 ){
break;
}
l = l->next;
}
return stringTypeID(l->type_id);
}
BucketList getBucketElement(string id, string scope){
int pos = hashTab(id+scope);
BucketList l = hashTable[pos];
while(l!=NULL){
if(id.compare(l->id) == 0 && scope.compare(l->scope) == 0 ){
return l;
}
l = l->next;
}
pos = hashTab(id+" ");
l = hashTable[pos];
string globalScope = " ";
while(l!=NULL){
if(id.compare(l->id) == 0 && globalScope.compare(l->scope)==0 ){
return l;
}
l = l->next;
}
cout << "elemento não encontrado" << " "+ id + " " << scope << endl;
return l;
}