-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathA5_42_translator.cpp
More file actions
395 lines (368 loc) · 12.7 KB
/
A5_42_translator.cpp
File metadata and controls
395 lines (368 loc) · 12.7 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
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
#include "A5_42_translator.h"
// Global Variables
vector<Quad *> quadArray; // Quad Array
SymbolTable *currentTable, *globalTable, *parentTable; // Symbol Tables
Symbol *currentSymbol; // Current Symbol
SymbolType::typeEnum currentType; // Current Type
int tableCount, temporaryCount; // Counts of number of tables and number of temps generated
// Implementation of symbol type class
SymbolType::SymbolType(typeEnum type, SymbolType *arrayType, int width) : type(type), width(width), arrayType(arrayType) {}
// Implementation of sizes for symbol types
int SymbolType::getSize()
{
if (type == CHAR)
return 1;
else if (type == INT || type == POINTER)
return 4;
else if (type == ARRAY)
return width * (arrayType->getSize());
else
return 0;
}
// Function to print symbol type
string SymbolType::toString()
{
if(this->type == SymbolType::VOID)
return "void";
else if(this->type == SymbolType::CHAR)
return "char";
else if(this->type == SymbolType::INT)
return "int";
else if(this->type == SymbolType::POINTER)
return "ptr(" + this->arrayType->toString() + ")";
else if(this->type == SymbolType::FUNCTION)
return "function";
else if(this->type == SymbolType::ARRAY)
return "array(" + to_string(this->width) + ", " + this->arrayType->toString() + ")";
else if(this->type == SymbolType::BLOCK)
return "block";
}
// Implementation of symbol table class
SymbolTable::SymbolTable(string name, SymbolTable *parent) : name(name), parent(parent) {}
Symbol *SymbolTable::lookup(string name)
{
// If the symbol is present in the current table, return it
auto it = (this)->symbols.find(name);
if (it != (this)->symbols.end())
return &(it->second);
// If the symbol is not present in the current table, check the parent table
Symbol *ret_ptr = nullptr;
if (this->parent != NULL)
ret_ptr = this->parent->lookup(name);
// if the symbol is not present in the parent table, insert it in the current table and return
if (this == currentTable && !ret_ptr)
{
this->symbols.insert({name, *(new Symbol(name))});
return &((this)->symbols.find(name)->second);
}
return ret_ptr;
}
// Update the symbol table and its children with offsets
void SymbolTable::update()
{
vector<SymbolTable *> visited; // vector to keep track of children tables to visit
int offset;
for (auto &map_entry : (this)->symbols) // for all symbols in the table
{
if (map_entry.first == (this->symbols).begin()->first) // if the symbol is the first one in the table then set offset of it to 0
{
map_entry.second.offset = 0;
offset = map_entry.second.size;
}
else // else update the offset of the symbol and update the offset by adding the symbols width
{
map_entry.second.offset = offset;
offset += map_entry.second.size;
}
if (map_entry.second.nestedTable) // remember children table
{
visited.push_back(map_entry.second.nestedTable);
}
}
for (auto &table : visited) // update children table
{
table->update();
}
}
// Function to print the symbol table and its children
void SymbolTable::print()
{
// pretty print
cout << string(115, '-') << endl;
cout << "Table Name: " << this->name <<"\t\t Parent Name: "<< ((this->parent)?this->parent->name:"None") << endl;
cout << string(115, '-') << endl;
cout << setw(20) << "Name" << setw(20) << "Type" << setw(20) << "Initial Value" << setw(20) << "Offset" << setw(20) << "Size" << setw(20) << "Child" << "\n" << endl;
// cout<<"Name\t Type\t InitialValue\t Offset\t Size\n";
vector<SymbolTable *> tovisit;
// print all the symbols in the table
for (auto &map_entry : (this)->symbols)
{
cout << setw(20) << map_entry.first;
fflush(stdout);
cout << setw(20) << (map_entry.second.isFunction ? "function" : map_entry.second.type->toString());
cout << setw(20) << map_entry.second.initialValue << setw(20) << map_entry.second.offset << setw(20) << map_entry.second.size;
cout << setw(20) << (map_entry.second.nestedTable ? map_entry.second.nestedTable->name : "NULL") << endl;
// remember to print nested tables later
if (map_entry.second.nestedTable)
{
tovisit.push_back(map_entry.second.nestedTable);
}
}
cout << string(115, '-') << endl;
cout << "\n" << endl;
// print nested tables
for (auto &table : tovisit)
{
table->print();
}
}
// Implementation of symbol class
Symbol::Symbol(string name, SymbolType::typeEnum type, string init) : name(name), type(new SymbolType(type)), offset(0), nestedTable(NULL), initialValue(init), isFunction(false)
{
size = this->type->getSize();
}
// update type of the symbol
Symbol *Symbol::update(SymbolType *type)
{
this->type = type;
size = this->type->getSize();
return this;
}
// convert the present symbol to different type, return old symbol if conversion not possible
Symbol *Symbol::convert(SymbolType::typeEnum type_)
{
// if current type is int
if ((this->type)->type == SymbolType::typeEnum::INT)
{
if (type_ == SymbolType::typeEnum::CHAR)
{
// generate symbol of new type
Symbol *fin_ = gentemp(type_);
emit("=", fin_->name, "INT_TO_Char(" + this->name + ")");
return fin_;
}
// reutrn orignal symbol if the final type is not char
return this;
}
// if the current type si char
else if ((this->type)->type == SymbolType::typeEnum::CHAR)
{
// if the target type is int
if (type_ == SymbolType::typeEnum::INT)
{
// generate symbol of new type
Symbol *fin_ = gentemp(type_);
emit("=", fin_->name, "Char_TO_Int(" + this->name + ")");
return fin_;
}
return this;
}
return this;
}
// Implementation of quad class
Quad::Quad(string result, string arg1, string op, string arg2) : result(result), op(op), arg1(arg1), arg2(arg2) {}
Quad::Quad(string result, int arg1, string op, string arg2) : result(result), op(op), arg1(toString(arg1)), arg2(arg2) {}
// print the quad
void Quad::print()
{
// if binary operations
auto binary_print = [this]()
{
cout << "\t" << this->result << " = " << this->arg1 << " " << this->op << " " << this->arg2 << endl;
};
// if relational operators
auto relation_print = [this]()
{
cout << "\tif " << this->arg1 << " " << this->op << " " << this->arg2 << " goto " << this->result << endl;
};
auto shift_print = [this]()
{
cout << "\t" << this->result << " " << this->op[0] << " " << this->op[1] << this->arg1 << endl;
};
/* we define the printing format for all operators */
if (this->op == "=")
{
cout << "\t" << this->result << " = " << this->arg1 << endl;
}
else if (this->op == "goto")
{
cout << "\tgoto " << this->result << endl;
}
else if (this->op == "return")
{
cout << "\treturn " << this->result << endl;
}
else if (this->op == "call")
{
cout << "\t" << this->result << " = call " << this->arg1 << ", " << this->arg2 << endl;
}
else if (this->op == "param")
{
cout << "\t" << "param " << this->result << endl;
}
else if (this->op == "label")
{
cout << this->result << endl;
}
else if (this->op == "=[]")
{
cout << "\t" << this->result << " = " << this->arg1 << "[" << this->arg2 << "]" << endl;
}
else if (this->op == "[]=")
{
cout << "\t" << this->result << "[" << this->arg1 << "] = " << this->arg2 << endl;
}
else if (this->op == "+" or this->op == "-" or this->op == "*" or this->op == "/" or this->op == "%" or this->op == "|" or this->op == "^" or this->op == "&")
{
binary_print();
}
else if (this->op == "==" or this->op == "!=" or this->op == "<" or this->op == ">" or this->op == "<=" or this->op == ">=")
{
relation_print();
}
else if (this->op == "=&" or this->op == "=*")
{
shift_print();
}
else if(this->op == "*=")
{
cout << "\t" << "*" << this->result << " = " << this->arg1 << endl;
}
else
{
// if none of the above operators
cout << this->op << this->arg1 << this->arg2 << this->result << endl;
cout << "INVALID OPERATOR\n";
}
}
// Implementation of emit funtions
void emit(string op, string result, string arg1, string arg2)
{
Quad *q = new Quad(result, arg1, op, arg2);
quadArray.push_back(q);
}
void emit(string op, string result, int arg1, string arg2)
{
Quad *q = new Quad(result, arg1, op, arg2);
quadArray.push_back(q);
}
// Implementation of backpatching functions
void backpatch(list<int> list_, int addr)
{
// for all the addresses in the list, add the target address
for (auto &i : list_)
{
quadArray[i-1]->result = toString(addr);
}
}
list<int> makeList(int base)
{
// returns list with the base address as its only value
return {base};
}
list<int> merge(list<int> first, list<int> second)
{
// merge two lists
list<int> ret = first;
ret.merge(second);
return ret;
}
// Implementation of Expression class functions
void Expression::toInt()
{
// if the expression type is boolean
if (this->type == Expression::typeEnum::BOOLEAN)
{
// generate symbol of new type and do backpatching and other required operations
this->symbol = gentemp(SymbolType::typeEnum::INT);
backpatch(this->trueList, static_cast<int>(quadArray.size()+1)); // update the true list
emit("=", this->symbol->name, "true"); // emit the quad
emit("goto", toString(static_cast<int>(quadArray.size() + 2))); // emit the goto quad
backpatch(this->falseList, static_cast<int>(quadArray.size()+1)); // update the false list
emit("=", this->symbol->name, "false");
}
}
void Expression::toBool()
{
// if the expression type is non boolean
if (this->type == Expression::typeEnum::NONBOOLEAN)
{
// generate symbol of new type and do backpatching and other required operations
this->falseList = makeList(static_cast<int>(quadArray.size()+1)); // update the falselist
emit("==", "", this->symbol->name, "0"); // emit general goto statements
this->trueList = makeList(static_cast<int>(quadArray.size()+1)); // update the truelist
emit("goto", "");
}
}
// Implementation of other helper functions
int nextInstruction()
{
// returns the next instruction number
return quadArray.size() + 1;
}
// generates temporary of given type with given value s
Symbol *gentemp(SymbolType::typeEnum type, string s)
{
Symbol *temp = new Symbol("t" + toString(temporaryCount++), type, s);
currentTable->symbols.insert({temp->name, *temp});
return temp;
}
// change current table to specified table
void changeTable(SymbolTable *table)
{
currentTable = table;
}
// code to check if a and b are of the same type, promotes to the higher type if feasible and if that makes the type of both the same
bool typeCheck(Symbol *&a, Symbol *&b)
{
// lambda function to check if a and b are of the same type
std::function<bool(SymbolType *, SymbolType *)> type_comp = [&](SymbolType *first, SymbolType *second) -> bool
{
if (!first and !second)
return true;
else if (!first or !second or first->type != second->type)
return false;
else
return type_comp(first->arrayType, second->arrayType);
};
// if the types are same return true
if(type_comp(a->type, b->type))
return true;
else if(a->type->type == SymbolType::INT or b->type->type == SymbolType::INT) {
a = a->convert(SymbolType::INT);
b = b->convert(SymbolType::INT);
return true;
}
// return false if not possible to cast safelt to same type
else {
return false;
}
}
// Implementation of utility functions
// overloaded toString function to maintain semantic consistency
// convert int to string
string toString(int i)
{
return to_string(i);
}
// converts char to string
string toString(char c)
{
return string(1, c);
}
int main() {
// initialization of global variables
tableCount = 0;
temporaryCount = 0;
globalTable = new SymbolTable("global");
currentTable = globalTable;
cout << left; // left allign
yyparse();
globalTable->update();
globalTable->print();
int ins = 1;
for(auto it : quadArray) {
cout<<setw(4)<<ins++<<": "; it->print();
}
return 0;
}