-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathass6_13CS10060_translator.h
444 lines (362 loc) · 9.93 KB
/
ass6_13CS10060_translator.h
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
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
#ifndef ASS6_13CS10060_TRANSLATOR_H_INCLUDED
#define ASS6_13CS10060_TRANSLATOR_H_INCLUDED
#include <string>
#include <vector>
#include <iostream>
#include <fstream>
#include <unordered_map>
#include <map>
#include <iomanip>
#include <cstdlib>
#include <cstdio>
using namespace std;
typedef enum {
/* Arithmetic Operators */
PLUS = 1,
MINUS,
MULT,
DIV,
MOD,
/* Shift Operators And Bitwise Operators */
SLL,
SRA, //Signed shift right
SRL,
BitwiseAND,
BitwiseOR,
BitwiseXOR,
/* Logical Operators */
LESS,
LEQ,
GRT,
GEQ,
AND,
OR,
LogicalEQ,
NotEQ,
/* Unary operators */
UMINUS,
UPLUS,
NEGATION,
COMPLEMENT,
/* Copy operator */
COPY,
/* Jump and branch operators */
OP_GOTO,
IFGOTO,
IFFalseGOTO,
IFLessGOTO,
IFGrtGOTO,
IFLessEqGOTO,
IFGrtEqGOTO,
IFLogEqGOTO,
IFNotEqGOTO,
/* Procedural Operators */
CALL,
PARAM,
OP_RETURN,
/* Array operators */
ASSIGNARR,
ARRASSIGN,
/* Pointer Access operators */
ASSIGNADDRESS,
ASSIGNVALUE,
VALUEASSIGN,
/* Type conversion Operators */
Int2Dbl,
Dbl2Int,
Char2Int,
Int2Char,
COND,
NOP,
__SLABEL,
__ELABEL
} opcodeType;
typedef enum{
TEMPORARY = 1,
LOCAL,
RETURNVALUE,
PARAMETER,
GLOBAL,
FUNCTION,
BLOCK,
CONSTANT_TEMP,
UNKNOWN
} symbolCategory;
struct Value{
enum {INT_TAG,DOUBLE_TAG,CHAR_TAG,STRING_TAG,NO_DEF} tag;
int intvalue;
double doublevalue;
char charvalue;
string strlabel;
Value();
Value(int value);
Value(double value);
Value(char value);
Value(string strlabel);
string getString();
};
class SymbolTableRow;
class StringTable
{
map<string,int> strtable;
static int count;
public:
string lookup(string key);
void print(ostream& out);
string getStringsASM();
};
class LabelTable{
map<int,int> table;
int labelcount;
public:
LabelTable();
string lookup(int index);
bool find(int index);
void print(ostream& out);
int getcount();
};
class QuadOperand{
public:
enum {SYMBOLINK=1, VALUE,NA} tag;
Value v;
SymbolTableRow* symrow;
QuadOperand();
QuadOperand(SymbolTableRow*);
QuadOperand(Value);
QuadOperand(int);
int isConstant();
int getIntegerConstValue();
string getString();
};
class SymbolTable;
class Quad;
class Type;
class ParameterList;
class Quad {
public:
opcodeType opcode;
/**
* result : The argument on rhs of equal in all instruction where applicable
* The target address in case of goto opcode
* The parameter in case of param opcode
*
* operand1 : First Operand wherever applicable else NULL
* operand2 : Second Operand wherever applicable else NULL
*
*/
QuadOperand result,operand1,operand2;
/**
*
*/
Quad();
Quad(opcodeType opcode, QuadOperand result=QuadOperand(),QuadOperand operand1=QuadOperand(),
QuadOperand operand2=QuadOperand());
/**
* Function to update the target for a goto instructions(assuming it
* is called for the correct quad(ie with opcode of jump types)
*/
void updateTarget(string result);
void print(ostream& out);
};
class QuadArray{
vector<Quad> _quadarray;
int nextinstruction;
public:
QuadArray();
//void emit(opcodeType opcode,SymbolTableRow* result=NULL,SymbolTableRow* arg1=NULL,SymbolTableRow* arg2=NULL);
//void emit(opcodeType opcode,SymbolTableRow* result,SymbolTableRow* arg1,Value constant);
//void emit(opcodeType opcode,SymbolTableRow* result,Value constant);
//void emit(opcodeType opcode,Value constant);
void emit(opcodeType opcode,QuadOperand result,QuadOperand operand1=QuadOperand(),QuadOperand operand2=QuadOperand());
void emit(opcodeType);
int getNextIndex();
void backpatch(vector<int>& listinstr,int index);
void updateLabelTable();
Quad& get(int i);
void print(ostream&);
void propagateSome();
};
class Type{
public:
string name;
Type();
Type(string name);
virtual ~Type();
virtual int getSize();
virtual void print(std::ostream &out);
virtual int getIdentity();
virtual string getname();
virtual bool isScalarType();
virtual bool isIntegerType();
virtual bool isArithmeticType();
};
class Basic_Type:public Type{
public:
int size;
Basic_Type(string name,int size=0);
int getSize();
void print(ostream&);
int getIdentity();
string getname();
bool isScalarType();
bool isIntegerType();
bool isArithmeticType();
};
class Pointer;
class Array:public Type{
public:
int size;
Type *elemType;
Array(int size,Type *elemType);
int getSize();
void print(ostream&);
string getname();
static Type* makeArray(Type*,int size=1);
Pointer* getEquivalentPointer();
};
class Function:public Type{
public:
Type* retType;
vector<Type*> paramList;
int n;
Function(Type* retType,vector<Type*> paramlist,int n);
int getSize();
void print(ostream&);
int getNoOfParameters();
Type* getReturnType();
string getname();
bool isScalarType();
bool isIntegerType();
bool isArithmeticType();
static void updateParameterList(Type *t,ParameterList* plist=NULL);
static Type* makeFunction(Type* t);
};
class Pointer:public Type{
public:
int size;
Type* pointeeType;
Pointer(Type* pointee);
int getSize();
void print(ostream&);
int getIdentity();
string getname();
Type* getPointeeType();
static Type* makePointer(Type *);
};
class SymbolTableRow{
public:
string name;
Type* type;
symbolCategory categoryOfSymbol;
Value initialValue;
int size;
int offset;
SymbolTable* nestedTable;
static const string RETNAME;
SymbolTableRow(string name,Type *type=NULL,symbolCategory categoryOfSymbol=UNKNOWN,
Value initialValue=Value(),int size=-1,int offset=-1,SymbolTable* nestedTable=NULL);
void print(ostream&);
};
class SymbolTable{
unordered_map<string,SymbolTableRow*> table;
vector<string> insertionOrder;
typedef pair<string,SymbolTableRow*> tablepair;
static int tempNo;
static int blockNo;
static int paramNo;
public:
static const int GLOBALLEVEL = 0;
static const int FUNCTIONLEVEL = 1;
static const int BLOCKLEVEL = 2;
string name;
SymbolTable* parent;
int level;
SymbolTable(string name,SymbolTable* parent,int level);
SymbolTableRow* gentemp(Type* type,Value initialValue=Value(),symbolCategory categoryOfSymbol=UNKNOWN);
SymbolTableRow* lookup(string name);
static SymbolTable* getNewBlockTable();
void insert(SymbolTableRow* symrow);
SymbolTableRow* insertDummyParam(Type * t);
void updateOffset();
string getDataSegment();
void print(ostream&);
int setARoffsets(int);
friend string transferParameters(SymbolTableRow* srow);
};
class Expression{
public:
Expression();
SymbolTableRow* loc;
vector<int> truelist;
vector<int> falselist;
Type* type;
SymbolTableRow* array;
SymbolTableRow* offset;
void swapLists();
/**
*
* Promotes the value to the corresponding int value
* Converts bool to int and promotes char to int
* Leaves other types unchanged
*/
Expression convertToInt();
/**
* Converts expression to bool if it is of type convertible to bool
*/
void convertToBool();
/**
* Converts expression from bool to int if it is of type int
*/
void convertBoolToInt();
};
class ListType{
public:
vector<int> nextlist;
ListType();
};
class ArgList{
public:
vector<Expression*> args;
ArgList();
};
class ParameterList{
public:
vector<Type*> plist;
};
vector<int> merge(const vector<int> list1,const vector<int> list2);
vector<Type*> merge(const vector<Type*> list1,const vector<Type*> list2);
vector<int> makelist(int addr);
vector<Type*> makelist(Type* type);
int isCallable(Type *t);
Expression* callFunction(SymbolTableRow* f,ArgList *args);
/**
* This function checks if the two types are same or can be promoted to same
* using implicit promotion(and promotes if possible) and returns 1 else return 0
*/
int typecheck(Expression* e1, opcodeType opcode);
int typecheck(Expression* e1, Expression* e2,opcodeType opcode);
int typecheck(Expression* e1, Expression* e2,Expression* e3,opcodeType opcode);
int typecheck(Type* t, Expression *e );
/**Global Functions*/
string getGlobalVarString(SymbolTableRow* srow);
string getIncludeString();
string getRegisterApt(string type,int no);
string makeFunctionPrologue(SymbolTableRow* srow);
string makeFunctionEpilogue(SymbolTableRow* srow);
string getAddressExpression(SymbolTableRow* srow);
string generateCallStack(Quad q,int i,int *size);
string getASMforQuad(Quad& q,int *factive,int i);
void printASMCode(ostream& out,string filename);
const int SIZEOFINT = 4;
const int SIZEOFCHAR = 1;
const int SIZEOFDOUBLE = 8;
const int SIZEOFPOINTER = 8;
const int REGPARAMETERS = 6;
//extern const Basic_Type *INT_TYPE;
static Basic_Type* INT_TYPE = new Basic_Type("int",SIZEOFINT);
static Basic_Type *DOUBLE_TYPE = new Basic_Type("double",SIZEOFDOUBLE);
static Basic_Type *CHAR_TYPE = new Basic_Type("char",SIZEOFCHAR);
static Basic_Type *VOID_TYPE = new Basic_Type("void");
static Basic_Type *BOOL_TYPE = new Basic_Type("bool");
static Pointer *STRING_TYPE = new Pointer(CHAR_TYPE);
#endif // ASS6_13CS10060_TRANSLATOR_H_INCLUDED