-
Notifications
You must be signed in to change notification settings - Fork 0
/
Parser.h
186 lines (149 loc) · 5.68 KB
/
Parser.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
//
// Created by Taemin Park on 1/14/16.
//
#ifndef ADV_COMPILER_PARSER_H
#define ADV_COMPILER_PARSER_H
//#define CODEBUFSIZE 128 //128 * uint32 = 4096
//#define NUMOFREGS 32
#include <array>
#include <vector>
#include <stack>
#include <map>
#include <algorithm>
#include "Scanner.h"
#include "GraphDrawer.h"
#include "SSABuilder.h"
#include "BasicBlock.h"
#include "CSETracker.h"
using namespace std;
class Parser {
public:
Parser();
~Parser(){
Scanner *scanner = Scanner::instance();
delete scanner;
GraphDrawer *graphDrawer = GraphDrawer::instance();
delete graphDrawer;
_parser = 0;
};
static Parser* instance();
RC openFile(const std::string &folder, const std::string &sourceFileName, const std::string &sourceFileFormat); RC closeFile();
void startParse();
void printIRCodes(string functionName,vector<shared_ptr<IRFormat>> codes);
void printSymbolTable();
void printBlock();
void createControlFlowGraph(const string &graphFolder,const string &sourceFileName,const string version);
void createDominantGraph(const string &graphFolder,const string &sourceFileName);
int getNumOfVarInFunction(string functionName){
SymTable table = symTableList.at(functionName);
return table.getNumOfVar();
};
int getNumOfParamInFunction(string functionName){
SymTable table = symTableList.at(functionName);
return table.getNumOfParam();
};
string getCodeString(string functionName, shared_ptr<IRFormat> code);
std::unordered_map<std::string,vector<shared_ptr<BasicBlock>>> getFuncList(){return functionList;};
SymTable getSymTable(string functionName){
return symTableList.at(functionName);
};
void setNumOfVirtualRegs(string functionName, int numOfVRegs)
{ symTableList.at(functionName).setNumOfVirtualRegs(numOfVRegs);};
int getNumOfVirtualRegs(string functionName){return symTableList.at(functionName).getNumOfVirtualRegs();};
void insertRegUsed(string functionName, int arg){
if(arg < NUM_OF_DATA_REGS)
arg = arg + REG_DATA;
else
arg = arg - NUM_OF_DATA_REGS + REG_VIRTUAL;
SymTable *symTable = &symTableList.at(functionName);
symTable->regUsedList.push_back(arg);
};
shared_ptr<Symbol> symTableLookup(string scope,std::string symbol,SymType symType);
int newInstruction(){return IRpc++;};
int newBasicBlock(){return numOfBlock++;};
void updateBlockInfo(string functionName, shared_ptr<BasicBlock> block)
{
vector<shared_ptr<BasicBlock>> *blockList = &functionList.at(functionName);
blockList->push_back(block);
}
private:
string fileName;
//Error message
void Error(std::string nonTerminal, std::initializer_list<std::string> missingTokens);
//Next token for a explicit token
void Next();
//Non terminals
void computation();
void funcBody();
vector<string> formalParam();
void funcDecl();
void varDecl();
shared_ptr<Symbol> typeDecl();
void statSequence();
void statement();
void returnStatement();
void whileStatement();
void ifStatement();
Result funcCall();
Result assignment();
Result relation();
Result expression();
Result term();
Result factor();
Result designator();
IROP relOp();
//Intermediate Code emittion
Result emitIntermediate(IROP irOp,vector<Result> x);
void emitOrUpdatePhi(string x, Result defined);
void CondJF(Result &x);
void UnCJF(Result &x);
void Fixup(unsigned long loc);
void FixLink(unsigned long loc);
void predefinedFunc();
Result getAddressInStack(int location);
int IRpc;
int loopDepth;
std::vector<shared_ptr<IRFormat>> IRCodes; //index is correspond to line Num
std::stack<std::string> scopeStack;
std::unordered_map<int,stack<int>> dominatedByInfo;
std::unordered_map<std::string,SymTable> symTableList;
//std::vector<BasicBlock> basicBlockList;
std::unordered_map<std::string,vector<shared_ptr<BasicBlock>>> functionList;
shared_ptr<BasicBlock> currentBlock;
unordered_map<int,shared_ptr<BasicBlock>> instructionBlockPair;
bool finalizeAndStartNewBlock(BlockKind newBlockKind, bool isCurrentCond, bool directFlowExist, bool dominate);
void updatePhiInBB(int modifiedBlockNum, vector<shared_ptr<IRFormat>> codes);
void insertBasicBlock(shared_ptr<BasicBlock> block);
void updateBlockForDT(int dominatingBlockNum);
shared_ptr<BasicBlock> getBlockFromNum(int blockNum);
bool isDominate(int dominatingBlockNum, int dominatedBlockNum);
int numOfBlock;
void addFuncSymbol(SymType symType, std::string symbolName, unsigned long numOfParam);
void addVarSymbol(std::string symbol, SymType symType, std::vector<int> arrayCapacity);
void addParamSymbol(std::string symbol, size_t numOfParam, int index);
int addSymInTable(){return numOfSym++;}; //Fixme: fake implementation
void symbolTableUpdate(string var,shared_ptr<Symbol> varSym);
void defChangedToPhi(string x,shared_ptr<IRFormat> phiCode, vector<shared_ptr<IRFormat>> irCodes);
//SSA
SSABuilder ssaBuilder;
//CSE
CSETracker cseTracker;
void cseForLoad(int dominatingBlockNum);
void cseForWhileInst(int dominatingBlockNum);
/*
//Code emit related
void PutF1(int op, int a, int b, int c);
void Load(Result &x);
int AllocateReg();
void DeAllocateReg(int regNum);
void Compute(TokenType computeToken, Result &x, Result &y);
*/
TokenType scannerSym;
//std::array<int32_t, CODEBUFSIZE> buf;
//std::array<bool,NUMOFREGS> regsBusy;
//int pc;
// int globalBase;
int numOfSym;
static Parser *_parser;
};
#endif //ADV_COMPILER_PARSER_H