-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
133 lines (112 loc) · 4.93 KB
/
main.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
#include <iostream>
#include "CodeGeneration.h"
using namespace std;
int main() {
string folder = "/home/tmpark/ClionProjects/Adv_Compiler/test/";
string graphFolder = "/home/tmpark/ClionProjects/Adv_Compiler/graph/";
string binaryFolder = "/home/tmpark/ClionProjects/Adv_Compiler/binary/";
string simulator = "DLX";
string sourceFileFormat = ".txt";
string graphFileName = "graph.vcg";
string xvcg = "xvcg -font rk24";
RC rc = -1;
#if NO_PARSE
Scanner *scanner = Scanner :: instance();
rc = scanner->openFile(folder + sourceFileName);
if(rc == -1)
return 0;
while(scanner->GetSym() != eofToken);
scanner->closeFile();
#else
//shift test
/*
int a = 12431;
int b = a >> 1;
int aRest = a & 1;
int c = -a;
int cRest = c & 1;
int d = c >> 1;
cout << a << "\t" << b << "\t"<< aRest <<"\t"<< c << "\t" << d<< "\t" << cRest << endl;*/
//Issue
//Test4 not matched number of parameters, wrong array indexing -> intentional error
//Test13 function is changed to procedure (Michael's mistake)
//Test15 var name should be allowed
//Test17,18,19->Copy Propagation Test
//Test26 -> CSE Test
//1~35
//9:inner if
//10:inner while(unlimited iteration)
//11:inner if-while and vice versa(unlimited iteration)
//1,5,7,8,9,10,11,12,14,17,19,21,22,23,24,25,26,27,28,29,30,31 : No func Call
for(int i = 1 ; i < 35 ; i++)
{
//if(i == 4)
// continue;
string testNum = to_string(i);
size_t numOfChar = testNum.size();
if(numOfChar == 1)
{
testNum = "00" + testNum;
}
else if(numOfChar == 2)
{
testNum = "0" + testNum;
}
string sourceFileName = "test" + testNum;
if(i == 32)
sourceFileName = "cell";
else if(i == 33)
sourceFileName = "factorial";
/**************************************************Parser***********************************************************/
Parser *parser = Parser :: instance();
rc = parser->openFile(folder,sourceFileName,sourceFileFormat);
if(rc == -1)
return 0;
parser->startParse();
/*************************************************Draw CFG,DT**********************************************************/
//parser->printBlock();
parser->createControlFlowGraph(graphFolder+sourceFileName + "/" + "CFG_Original" + "/",sourceFileName,"Original");
parser->createDominantGraph(graphFolder+sourceFileName + "/" + "DT" + "/",sourceFileName);
/**********************************************Register Allocation for each function***********************************/
std::unordered_map<std::string,vector<shared_ptr<BasicBlock>>> functionList = parser->getFuncList();
int numOfGlobalVar = parser->getNumOfVarInFunction(GLOBAL_SCOPE_NAME);
vector<shared_ptr<RegAllocation>> regAllocList;
vector<string> functionNameList;
for(auto function : functionList)
{
int numOfVar = parser->getNumOfVarInFunction(function.first);
if(function.first != GLOBAL_SCOPE_NAME)
numOfVar = numOfVar + numOfGlobalVar;
int numOfParam = parser->getNumOfParamInFunction(function.first);
shared_ptr<RegAllocation> regAlloc(new RegAllocation(function.first, function.second, numOfVar + numOfParam));
regAlloc->doRegAllocation();
regAllocList.push_back(regAlloc);
functionNameList.push_back(function.first);
}
/**********************************************Draw CFG again after register allocation********************************/
parser->createControlFlowGraph(graphFolder+sourceFileName + "/" + "CFG_RegAlloc" + "/",sourceFileName,"RegAlloc");
/**********************************************Draw Interference Graph*************************************************/
int index = 0;
for(auto regAlloc : regAllocList)
{
regAlloc->createInterferenceGraph(graphFolder+sourceFileName + "/" + "IG" + "/",sourceFileName,functionNameList.at(index));
regAlloc.reset();
index++;
}
/**********************************************Code Generation*********************************************************/
CodeGeneration codeGeneration(functionList);
codeGeneration.doCodeGen();
codeGeneration.writeOutCode(binaryFolder+sourceFileName + "/",sourceFileName);
string executable = binaryFolder + sourceFileName + "/" + sourceFileName + ".out";
string execCommand = "java -classpath " + binaryFolder + " " + simulator + " " + executable;
//cout << sourceFileName << "'s output: " <<endl;
//system(execCommand.c_str());
//cout << endl;
//parser->printIRCodes(parser->IRCodes); //Debug
//parser->printSymbolTable(); //Debug
parser->closeFile();
delete parser;
}
#endif
return 0;
}