-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathMidCodeContainer.cpp
75 lines (66 loc) · 1.68 KB
/
MidCodeContainer.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
#include"MidCodeContainer.h"
int MidCodeContainer::midCodeInsert(MidCodeOp op, int target,
int operand1,bool isImmediate1,
int operand2, bool isImmediate2,
int label=-1 ) {
MidCode tmp;
tmp.op = op;
tmp.target = target;
tmp.operand1 = operand1;
tmp.operand2 = operand2;
tmp.isImmediate1 = isImmediate1;
tmp.isImmediate2 = isImmediate2;
tmp.labelNo = label;
int no = v.size();
v.push_back(tmp);
return no;
}
void MidCodeContainer::midCodeInsert(vector<MidCode>& tmp) {
for (MidCode& i : tmp) {
v.push_back(i);
}
}
ostream& operator<<(ostream& out, MidCodeContainer c) {
MidCode::table->getSubSymbolTableByName(c.functionName)->dumpMidCode(out);
for (MidCode& code : c.v) {
out << code;
}
return out;
}
int MidCodeContainer::getIndex() {
return v.size();
}
vector<MidCode>::iterator MidCodeContainer::getIterator(int index) {
return v.begin() + index;
}
void MidCodeContainer::erase(int start,int end) {
v.erase(v.begin() + start, v.begin() + end);
}
void MidCodeContainer::removeNops() {
for (int i = 0; i < v.size(); i++) {
if (v[i].op == MIDNOP) {
if (i + 1 >= v.size()||v[i+1].op==MIDFUNC) {
continue;
}
else if (v[i + 1].labelNo != MIDNOLABEL) {
int oldLabel = v[i].labelNo;
int newLabel = v[i + 1].labelNo;
for (int j = 0; j < v.size(); j++) {
if (v[j].op == MIDGOTO && v[j].operand1 == oldLabel) {
v[j].operand1 = newLabel;
}
else if ((v[j].op == MIDBNZ || v[j].op == MIDBZ) && v[j].operand2 == oldLabel) {
v[j].operand2 =newLabel;
}
}
v.erase(v.begin() + i, v.begin() + i + 1);
i--;
}
else {
v[i + 1].labelNo = v[i].labelNo;
v.erase(v.begin() + i, v.begin() + i + 1);
i--;
}
}
}
}