-
Notifications
You must be signed in to change notification settings - Fork 0
/
symtable.cpp
345 lines (308 loc) · 7.44 KB
/
symtable.cpp
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
#include "symtable.h"
BaseType::BaseType(){}
BaseType::BaseType(string name){
this->name = name;
this->type = 0;
pab = "base";
}
void BaseType::print(){
cout<<name;
}
bool BaseType::equal(Type* type2,int rec){
if(type2->pab=="base"){
if(rec==0){
if((this->name=="int" || this->name=="float") && (type2->name=="int" || type2->name=="float")){
return true;
}
}
return (this->name==type2->name);
}
return false;
}
int BaseType::size(){
if(this->equal(Type_int)){return 4;}
else if(this->equal(Type_void)){return 0;}
else {
return gst->symlist.find(this->name)->second->size;
}
}
Type* BaseType::Dereference(){
return NULL;
}
Type* PointerType::Dereference(){
if(this->type->pab=="base" && this->type->name=="void"){
return NULL;
}
return this->type;
}
Type* ArrayType::Dereference(){
return this->type;
}
PointerType::PointerType(){}
PointerType::PointerType(Type* type){
this->type = type;
pab = "pointer";
}
void PointerType::print(){
cout<<"*";
this->type->print();
}
bool PointerType::equal(Type* type2,int rec){
if(type2->pab=="base"){
return false;
}
if(type2->pab=="pointer"){
return this->type->equal(type2->type,rec+1);
}
if(type2->pab=="array"){
if(rec==0){
return this->type->equal(type2->type,1);
}
else{
return false;
}
// return this->type->equal(type->type2)
}
return false;
}
int PointerType::size(){
return 4;
}
ArrayType::ArrayType(){}
ArrayType::ArrayType(int dimension, Type* type){
this->dimension = dimension;
this->type = type;
pab = "array";
}
void ArrayType::print(){
cout<<"array("<<dimension<<", ";
type->print();
cout<<")";
}
bool ArrayType::equal(Type* type2, int rec){
if(type2->pab=="base"){
return false;
}
if(type2->pab=="pointer"){
return false;
}
if(type2->pab=="array"){
return (this->dimension==type2->dimension) && this->type->equal(type2->type,rec+1);
}
return false;
}
int ArrayType::size(){
return this->dimension * this->type->size();
}
// Type* getType(Type* type1, Type* type2){
// if(type1->0 && type2->0){
// if(type1->name=="int" && type2->name=="int"){
// return new Type*("int");
// }
// if(type1->name=="float" || type2->name=="float"){
// return new Type*("float");
// }
// return new Type(type1->string);
// }
// if
// }
Record_global::Record_global(){}
Record_global::Record_global(
string symbol_name,
sf_enum sf,
Type* type,
int size,
SymTable_local* symtable
){
this->symbol_name=symbol_name;
this->sf = sf;
this->type=type;
this->size=size;
this->symtable=symtable;
}
void Record_global::print(){
cout<<id<<"\t"<<symbol_name<<"\t"<<sf<<"\t";cout<<size<<"\t";type->print();cout<<endl;
cout<<endl;
cout<<"Symbol table for "<<symbol_name<<endl;
symtable->print();
cout<<endl<<endl;
}
Record_local::Record_local(){}
Record_local::Record_local(
string symbol_name,
// pv_enum pv,
bool is_param,
Type* type,
int size,
int offset
){
this->symbol_name=symbol_name;
this->is_param = is_param;
// this->pv = pv;
this->type=type;
this->size=size;
this->offset=offset;
}
void Record_local::print(){
cout<<id<<"\t"<<symbol_name<<"\t"<<is_param<<"\t";cout<<size<<"\t"<<offset<<"\t";type->print();cout<<endl;
}
vector<Record_local*> Record_global::params(){
return this->symtable->params;
}
SymTable_global::SymTable_global(){
id=0;
}
void SymTable_global::add(string record_name,Record_global* record){
record->id=id;
id++;
this->symlist.insert(std::pair<string,Record_global*>(record_name,record));
}
void SymTable_global::print(){
std::map<string,Record_global*>::iterator it = symlist.begin();
cout<<"Printing Global Table"<<endl;
cout<<"id\tname\tSF\tsize\ttype"<<endl;
vector<Record_global*> rc;
for (it=symlist.begin(); it!=symlist.end(); ++it){
// it->second->print();
rc.push_back(it->second);
}
for(int i=0;i<rc.size();i++){
for(int j=0;j<rc.size();j++){
if(rc[i]->id < rc[j]->id){
Record_global* r;
r=rc[i];
rc[i]=rc[j];
rc[j]=r;
}
}
}
for(int i=0;i<rc.size();i++){
rc[i]->print();
}
}
SymTable_local::SymTable_local(){
id=0;
this->locals_size=0;
}
void SymTable_local::add(string record_name,Record_local* record){
record->id=id;
id++;
if(record->is_param){
this->params.push_back(record);
}
this->symlist.insert(std::pair<string,Record_local*>(record_name,record));
if(!record->is_param)
this->locals_size = record->offset;
}
// void SymTable_local::getLocalSize(){
// for (it=symlist.begin(); it!=symlist.end(); ++it){
// if(it->second)
// rc.push_back(it->second);
// }
// }
void SymTable_local::print(){
std::map<string,Record_local*>::iterator it = symlist.begin();
vector<Record_local*> rc;
cout<<"id\tname\tSF\tsize\toffset\ttype"<<endl;
for (it=symlist.begin(); it!=symlist.end(); ++it){
rc.push_back(it->second);
}
for(int i=0;i<rc.size();i++){
for(int j=0;j<rc.size();j++){
if(rc[i]->id < rc[j]->id){
Record_local* r;
r=rc[i];
rc[i]=rc[j];
rc[j]=r;
}
}
}
for(int i=0;i<rc.size();i++){
rc[i]->print();
}
}
int SymTable_local::params_size(){
int res=0;
for(int i=0;i<params.size();i++){
res += params[i]->size;
}
return res;
}
void SymTable_local::populate_offset(int num){
std::map<string,Record_local*>::iterator it = symlist.begin();
for (it=symlist.begin(); it!=symlist.end(); ++it){
it->second->offset+=num;
}
}
// int SymTable_local::locals_size(){
// };
SymTable_global* gst = new SymTable_global();
SymTable_local* lst=0;
Type* type;
int width;
Type* old_type;
int old_width;
Record_global* grc;
Record_global* grc_temp;
Record_local* lrc;
Type* current_return_type;
string symbol_name;
int struct_width =0;
bool is_param = true;
int offset;
int size_ebp=4;
bool is_intconst;
int val;
int symtree=1;
Type* Type_float = new BaseType("float");
Type* Type_int = new BaseType("int");int line=1;
Type* Type_void = new BaseType("void");
Type* Type_voidstar = new PointerType(new BaseType("void"));
int return_size;
string fun_name;
bool debugger = 0;
Record_global* is_global(string id,sf_enum sf){
if(gst->symlist.find(id)!=gst->symlist.end()){
if(gst->symlist.find(id)->second->sf==sf)
return gst->symlist.find(id)->second;
}
return NULL;
}
vector<StringObject> global_strings;
// int fun(int a[10]){
// for(int i=0;i<15;i++){
// cout<<a[i]<<endl;
// }
// }
// int main(){
// Type* INT = new BaseType("int");
// Type* FLOAT = new BaseType("float");
// Type* STRUCT = new BaseType(("struct"));
// Type* INTSTAR = new PointerType(INT);
// Type* FLOATSTAR = new PointerType(FLOAT);
// Type* STRUCTSTAR = new PointerType(STRUCT);
// Type* INTARRAY = new ArrayType(10,INT);
// Type* INTARRAYARRAY = new ArrayType(10,INTARRAY);
// Type* INTAARRAY = new ArrayType(5,INTARRAY);
// INTAARRAY->print();
// INTARRAYARRAY->print();
// Type* INTSTARSTAR = new PointerType(INTSTAR);
// cout<<"INT INT"<<INTAARRAY->equal(INTSTARSTAR,0)<<endl;
// cout<<"INT INT"<<INTAARRAY->equal(INTARRAYARRAY,0)<<endl;
// cout<<"INT FLOAT"<<INTARRAY->equal(INTSTAR,0)<<endl;
// cout<<"INT FLOAT"<<INTSTAR->equal(INTARRAY)<<endl;
// // int x[5][10];
// // int a[15];
// // for(int i=0;i<15;i++){
// // a[i]=i;
// // }
// // int *b;
// // int **d;
// // fun(a);
// int (*a)[10];
// int b[10][10];
// int c[10][10];
// a=b;
// b=c;
// // fun(a,a,7,d);
// }