forked from sahilpparmar/emmCompiler
-
Notifications
You must be signed in to change notification settings - Fork 0
/
InterCode.h
293 lines (232 loc) · 8.41 KB
/
InterCode.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
/* Each InterCode class will store single three address Instruction in Quadruple form
* opnds_ array is for three operands
* OPNTYPE gives which type of instruction is this
* subCode_ is to distinguish expr type operand
*
* InterCodesClass - vector to store all InterCodes in a program
* BasicBlock - Divide InterCodesvec into basicblocks
* BasicBlocksClass - Vector of all basic blocks within one function reachable from one another
* BasicBlocksContainer- GLobal BasicBlocks structure - vector of all basicblocksclass
*/
#ifndef INTERCODE
#define INTERCODE
#include "STEClasses.h"
#include "Value.h"
#include "ParserUtil.h"
#include "all.h"
#include "Ast.h"
#include <map>
#include <utility>
#include <set>
#define TAB_SPACE 8
using namespace std;
class InterCode {
public:
enum OPNTYPE {CALL, FPARAM, APARAM, RETURN, EXPR, LABEL, GOTO, IFREL, ENTER, LEAVE, PRINT};
InterCode (OPNTYPE op, OpNode::OpCode subopc = OpNode::OpCode::INVALID,
void *a = NULL, void *b = NULL, void *c = NULL) {
optype_ = op;
subCode_ = subopc;
opnds_[0] = a;
opnds_[1] = b;
opnds_[2] = c;
}
void print(ostream &os);
void** get3Operands() { return opnds_; }
void set3Operands(void *opnd[3]) { *opnds_ = opnd; }
OPNTYPE getOPNType() { return optype_; }
OpNode::OpCode getsubCode() { return subCode_; }
void setSubCode(OpNode::OpCode opCode) { subCode_ = opCode; }
bool xchgSubcode();
string getLabel();
protected:
void *opnds_[3];
OpNode::OpCode subCode_;
OPNTYPE optype_;
};
/* Stores vector of all three address instructions
* Will be used for breaking basic blocks based on OPNTYPE label
*/
class InterCodesClass {
public:
InterCodesClass() {}
virtual ~InterCodesClass() {}
void print (ostream &os);
void addCode (InterCode *code);
void addCode (InterCodesClass* code);
void addCode (InterCode::OPNTYPE op, void *a = NULL,
void *b = NULL, void *c = NULL,
OpNode::OpCode subopc = OpNode::OpCode::INVALID);
vector<InterCode*>* getICodeVector() {
return &InterCodeVector;
}
void setICodeVector(vector <InterCode*> *tempVector) {
InterCodeVector = *tempVector;
}
void ifThenElseOpt(int *isOptimized);
void removeContLabelGoto(int *isOptimized);
void createLabelDUChain();
void insertMap(string str, InterCode* ic);
void ioptimize () {
int isOptimized = 0;
do {
isOptimized = 0;
ifThenElseOpt(&isOptimized);
removeContLabelGoto(&isOptimized);
} while (isOptimized);
createLabelDUChain();
//printMap();
}
void printMap();
protected:
vector <InterCode*> InterCodeVector;
map <string, vector <InterCode*>*> labelUsageMap;
};
class LabelClass {
static long labelCount;
static map <string, InterCode*> label_interCode_map;
public:
friend class InterCodesClass;
static void insertInMap(string str, InterCode* ic) {
label_interCode_map.insert(pair<string, InterCode*>(str, ic));
}
long getLabelCount() { return labelCount; }
void setLabelCount (long c) { labelCount = c; }
static InterCode* assignLabel (string name="") {
InterCode* ic = new InterCode (InterCode::OPNTYPE::LABEL, OpNode::OpCode::INVALID,
(void *)(labelCount), (void *)(new string(name)));
string str;
if (name.length() == 0) {
str = "L";
str += itoa(labelCount);
} else {
str = name;
}
insertInMap(str, ic);
labelCount++;
return ic;
}
};
class BasicBlock {
public :
BasicBlock (string label) {
blocklabel.assign(label);
}
vector<InterCode*>* getICodeVector() {
return &InterCodeVector;
}
void setICodeVector(vector<InterCode*>* vec) {
InterCodeVector.erase(InterCodeVector.begin(), InterCodeVector.end());
InterCodeVector.assign(vec->begin(), vec->end());
}
void addNextBlock(string nextlabel) {
vector<string>::iterator it = NextBlockLabels.begin();
for(; it != NextBlockLabels.end(); ++it) {
if (nextlabel.compare(*it) == 0)
return;
}
NextBlockLabels.push_back(nextlabel);
}
void addPrevBlock(string prevlabel) {
vector<string>::iterator it = PrevBlockLabels.begin();
for(; it != PrevBlockLabels.end(); ++it) {
if (prevlabel.compare(*it) == 0)
return;
}
PrevBlockLabels.push_back(prevlabel);
}
void setBlockLabel(string s) {
blocklabel.assign(s);
}
string getBlockLabel() {
return blocklabel;
}
void addCode (InterCode* cs) {
if (cs) InterCodeVector.push_back (cs);
}
vector <string>* getPrevBlockLabels() {
return &PrevBlockLabels;
}
void print(ostream &os);
void constantFolding(int *isOptimized);
void constantPropogation(int *isOptimized);
void redundantGotoRemoval(int *isOptimized);
void zeroRemoval(int *isOptimized);
set <string> EndLiveVars, StartLiveVars;
private :
string blocklabel;
vector <InterCode*> InterCodeVector;
vector <string> NextBlockLabels, PrevBlockLabels;
};
class BasicBlocksClass {
private:
vector <BasicBlock*> bbVector;
map <string, BasicBlock*> label_block_map;
public:
vector<string> ordervec;
map <string, BasicBlock*>* getLabelMap() {
return &label_block_map;
}
void setVector (vector <BasicBlock *> &vec) {
bbVector = vec;
}
vector<BasicBlock*>* getVector() {
return &bbVector;
}
vector<BasicBlock*> getBasicBlock() {
return bbVector;
}
BasicBlock* getBlockWithLabel (string);
void createBlocks (InterCodesClass* ic);
void blockOptimize () {
vector <BasicBlock*>::iterator it;
int isOptimized = 0;
do {
isOptimized = 0;
for (it = bbVector.begin(); it != bbVector.end(); ++it) {
(*it)->constantFolding (&isOptimized);
(*it)->constantPropogation (&isOptimized);
(*it)->redundantGotoRemoval(&isOptimized);
(*it)->zeroRemoval(&isOptimized);
}
} while (isOptimized);
}
void liveVariableAnalysis();
void commonSubExprElimination();
void print(ostream &os) {
vector <BasicBlock*>::iterator it = bbVector.begin();
for (; it != bbVector.end(); ++it) {
(*it)->print(os);
}
}
};
class BasicBlocksContainer {
private:
set <string> bbUsedContainers;
map <string, BasicBlocksClass*> bbContainer;
public:
void createBlockStruct (InterCodesClass* ic);
void insertInUsedList (string str) {
bbUsedContainers.insert(str);
}
set <string>* getUsedContainers() {
return &bbUsedContainers;
}
void printUsedContainers() {
set <string>::iterator it;
cout << "\n Called Blocks :";
for (it = bbUsedContainers.begin(); it != bbUsedContainers.end(); ++it) {
//no need of live var analysis for global
cout << (*it) << "\t";
}
cout << endl;
}
map <string, BasicBlocksClass*>* getContainer() {
return &bbContainer;
}
void removeBlocks();
BasicBlocksClass* insertInContainer (string);
void optimize();
void print(ostream &os);
};
#endif