-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathsyntax_tree.cpp
195 lines (172 loc) · 4.85 KB
/
syntax_tree.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
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
#include "syntax_tree.h"
int TreeNode::fornum;
int TreeNode::ifnum;
int TreeNode::genName;
int TreeNode::arrindex;
int TreeNode::registerFingerPrint;
int TreeNode::insideStmtBlocks;
int TreeNode::insideFor;
int TreeNode::arrsize;
int TreeNode::registerNum;
vector<code> TreeNode::Codes;
vector<code> TreeNode::Defs;
vector<string> TreeNode::args;
vector<string> TreeNode::paras;
stack<string> TreeNode::labelForContinue;
stack<string> TreeNode::labelForBreak;
bool TreeNode::insideFunction;
bool TreeNode::getPointer;
bool TreeNode::EnableCodeGen;
bool TreeNode::optimize;
string TreeNode::arrid;
string TreeNode::structType;
set<string> TreeNode::freeRegister;
unordered_set<string> TreeNode::usedFunctionName;
unordered_set<string> TreeNode::usedStructType;
unordered_set<string> TreeNode::structTypes;
unordered_map<string,unordered_map<string,int>> TreeNode::structTable;
void TreeNode::init() {
fornum=0;
ifnum=0;
genName=0;
insideStmtBlocks = 0;
insideFor = 0;
EnableCodeGen = true;
getPointer = false;
insideFunction = false;
arrsize = 0;
arrindex = 0;
registerNum = 0;
registerFingerPrint=0;
}
string TreeNode::allocateRegister(const string prefix) {
string ret;
if(prefix=="r_"){ //default
if(freeRegister.size()>0){
ret=*freeRegister.begin();
freeRegister.erase(freeRegister.begin());
}else{
ret = "%" + prefix + to_string(registerNum++);
}
}else{
ret = "%" + prefix +"oneshot";
}
ret=ret+".fp"+to_string(registerFingerPrint);
registerFingerPrint++;
return ret;
}
void TreeNode::freReg(string id) {
if(id.size()>3 && id.substr(0,3)=="%r_") {
size_t ed= id.find_last_of(".");
freeRegister.insert(id.substr(0,ed));
}
}
void TreeNode::CodePrint() {
init();
optimize=false;
this->Codegen();
for(int i=0; i<Defs.size();i++){
Defs[i].print();
Defs[i].printline();
}
for (int i = 0; i < Codes.size(); i++) {
Codes[i].print();
Codes[i].printline();
}
}
int TreeNode::SearchIdType(const string &id, string &type) const {
const TreeNode *treeNode = this;
while (treeNode) {
if (treeNode->className != "STMTBLOCK" && treeNode->className != "PROGRAM") {
treeNode = treeNode->parent;
continue;
}
if (treeNode->GetIdType(id, type) == 0) {
return 0;
} else {
treeNode = treeNode->parent;
}
}
return -1;
}
int TreeNode::GetIdType(const string &id, string &type) const {
if (symbolTable.count(id) == 0) {
return -1;
}
type = symbolTable.at(id).type;
return 0;
}
int TreeNode::GetIdPointer(const string &id, string &MemPtr) const {
if (symbolTable.count(id) == 0) {
return -1;
}
MemPtr.clear();
IdInfo tmp = symbolTable.at(id);
if (className == "STMTBLOCK") {
MemPtr.push_back('%');
} else if (className == "PROGRAM") {
MemPtr.push_back('@');
}
MemPtr.append("ptr_");
MemPtr.append(id);
MemPtr.append(".line" + to_string(tmp.lineno));
MemPtr.append(tmp.suffix);
return 0;
}
int TreeNode::SearchIdPointer(const string &id, string &MemPtr) const {
const TreeNode *treeNode = this;
while (treeNode) {
if (treeNode->className != "STMTBLOCK" && treeNode->className != "PROGRAM") {
treeNode = treeNode->parent;
continue;
}
if (treeNode->GetIdPointer(id, MemPtr) == 0) {
return 0;
} else {
treeNode = treeNode->parent;
}
}
return -1;
}
int TreeNode::saveIdtoTable(const string & id, const string & type, string & MemPtr) {
string n;
return saveIdtoTable(id, type, n, MemPtr);
}
int TreeNode::saveIdtoTable(const string & id, const string & type) {
string tmp;
string n;
return saveIdtoTable(id, type, n, tmp);
}
int TreeNode::saveIdtoTable(const string & id, const string & type, const string & suffix, string &MemPtr) {
TreeNode *treeNode = (TreeNode *) this;
while (treeNode && treeNode->className != "STMTBLOCK" && treeNode->className != "PROGRAM") {
treeNode = treeNode->parent;
}
if (treeNode->GetIdPointer(id, MemPtr) == 0) {
err("find duplicate definition :" + id);
return -1;
}
else {
IdInfo tmp;
tmp.lineno = lineCount;
tmp.type = type;
tmp.suffix = suffix;
treeNode->symbolTable[id] = tmp;
return treeNode->GetIdPointer(id, MemPtr);
}
}
void printTree(TreeNode *t, int level) {
int i = 0;
if (level == 0) {
printf("\n");
printf("syntax tree:\n");
printf("\n");
}
for (; i < level; i++) {
printf("--");
}
printf("%s\n", t->content.c_str());
for (i = 0; i < t->childrenSize; i++) {
printTree(t->children[i], level + 1);
}
}