Skip to content

Commit ec0e7e8

Browse files
opt1
1 parent 5a64502 commit ec0e7e8

21 files changed

+2559
-85
lines changed

Block.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -255,3 +255,7 @@ ostream& operator<<(ostream& out, Block b) {
255255
return out;
256256
}
257257

258+
void Block::blockOptimize() {
259+
BlockOptimization bop(activeOut);
260+
v = bop.propagationInBlock(v);
261+
}

Block.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
#include"MidCode.h"
55
#include"DagMap.h"
66
#include"DeadCodeEliminator.h"
7+
#include"BlockOptimization.h"
78
using namespace std;
89

910
class Block {
@@ -31,4 +32,5 @@ class Block {
3132
static set<int> setDifference(set<int> a, set<int> b);
3233
void DAGoptimize();
3334
void eliminateDeadCode();
35+
void blockOptimize();
3436
};

BlockOptimization.cpp

Lines changed: 230 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,230 @@
1+
#include"BlockOptimization.h"
2+
BlockOptimization::BlockOptimization(set<int>&_activeOut) {
3+
activeOut = _activeOut;
4+
}
5+
vector<MidCode>BlockOptimization::propagationInBlock(vector<MidCode>& v) {
6+
map<int, Item>substitution;
7+
vector<MidCode>res;
8+
vector<MidCode>resEnd;
9+
for (int i = 0; i < v.size(); i++) {
10+
MidCode c = v[i];
11+
if (c.labelNo != -1) {
12+
label = c.labelNo;
13+
}
14+
switch (c.op) {
15+
//只使用op1,且只会出现在最后
16+
case MIDPUSH:
17+
case MIDRET:
18+
case MIDBNZ:
19+
case MIDBZ:
20+
{
21+
MidCode tmp = c;
22+
if (c.operand1 != -1 && !c.isImmediate1 &&
23+
substitution.find(c.operand1) != substitution.end()) {
24+
tmp.operand1 = substitution[c.operand1].id;
25+
tmp.isImmediate1 = substitution[c.operand1].isImmediate;
26+
}
27+
resEnd.push_back(tmp);
28+
break;
29+
}
30+
//只使用op1
31+
case MIDPRINTINT:
32+
case MIDPRINTCHAR:
33+
{
34+
MidCode tmp = c;
35+
if (c.operand1 != -1 && !c.isImmediate1 &&
36+
substitution.find(c.operand1) != substitution.end()) {
37+
tmp.operand1 = substitution[c.operand1].id;
38+
tmp.isImmediate1 = substitution[c.operand1].isImmediate;
39+
}
40+
res.push_back(tmp);
41+
break;
42+
}
43+
case MIDADD:
44+
case MIDSUB:
45+
case MIDMULT:
46+
case MIDDIV:
47+
case MIDLSS:
48+
case MIDLEQ:
49+
case MIDGRE:
50+
case MIDGEQ:
51+
case MIDEQL:
52+
case MIDNEQ:
53+
{
54+
MidCode tmp = c;
55+
if (c.operand1 != -1 && !c.isImmediate1 &&
56+
substitution.find(c.operand1) != substitution.end()) {
57+
tmp.operand1 = substitution[c.operand1].id;
58+
tmp.isImmediate1 = substitution[c.operand1].isImmediate;
59+
}
60+
if (c.operand2 != -1 && !c.isImmediate2 &&
61+
substitution.find(c.operand2) != substitution.end()) {
62+
tmp.operand2 = substitution[c.operand2].id;
63+
tmp.isImmediate2 = substitution[c.operand2].isImmediate;
64+
}
65+
set<int>del;
66+
for (map<int, Item>::iterator itr = substitution.begin();
67+
itr != substitution.end(); itr++) {
68+
if (!itr->second.isImmediate && itr->second.id == c.target) {
69+
MidCode tmp2;
70+
tmp2.op = MIDASSIGN; tmp2.target = itr->first;
71+
tmp2.operand1 = itr->second.id; tmp2.isImmediate1 = itr->second.isImmediate;
72+
tmp2.operand2 = -1; tmp2.isImmediate2 = false;
73+
tmp2.labelNo = -1;
74+
res.push_back(tmp2);
75+
del.insert(itr->first);
76+
}
77+
}
78+
for (int j : del) {
79+
substitution.erase(j);
80+
}
81+
res.push_back(tmp);
82+
break;
83+
}
84+
case MIDNEGATE:
85+
{
86+
MidCode tmp = c;
87+
if (c.operand1 != -1 && !c.isImmediate1 &&
88+
substitution.find(c.operand1) != substitution.end()) {
89+
tmp.operand1 = substitution[c.operand1].id;
90+
tmp.isImmediate1 = substitution[c.operand1].isImmediate;
91+
}
92+
set<int>del;
93+
for (map<int, Item>::iterator itr = substitution.begin();
94+
itr != substitution.end(); itr++) {
95+
if (!itr->second.isImmediate && itr->second.id == c.target) {
96+
MidCode tmp2;
97+
tmp2.op = MIDASSIGN; tmp2.target = itr->first;
98+
tmp2.operand1 = itr->second.id; tmp2.isImmediate1 = itr->second.isImmediate;
99+
tmp2.operand2 = -1; tmp2.isImmediate2 = false;
100+
tmp2.labelNo = -1;
101+
res.push_back(tmp2);
102+
del.insert(itr->first);
103+
}
104+
}
105+
for (int j : del) {
106+
substitution.erase(j);
107+
}
108+
res.push_back(tmp);
109+
break;
110+
}
111+
case MIDASSIGN:
112+
{
113+
if (!c.isImmediate1 && c.operand1 == -1) {
114+
//ret
115+
res.push_back(c);
116+
break;
117+
}
118+
Item tmp;
119+
tmp.id= c.operand1;
120+
tmp.isImmediate = c.isImmediate1;
121+
substitution[c.target] = tmp;
122+
break;
123+
}
124+
case MIDARRAYGET:
125+
{
126+
MidCode tmp = c;
127+
if (c.operand2 != -1 && !c.isImmediate2 &&
128+
substitution.find(c.operand2) != substitution.end()) {
129+
tmp.operand2 = substitution[c.operand2].id;
130+
tmp.isImmediate2 = substitution[c.operand2].isImmediate;
131+
}
132+
set<int>del;
133+
for (map<int, Item>::iterator itr = substitution.begin();
134+
itr != substitution.end(); itr++) {
135+
if (!itr->second.isImmediate && itr->second.id == c.target) {
136+
MidCode tmp2;
137+
tmp2.op = MIDASSIGN; tmp2.target = itr->first;
138+
tmp2.operand1 = itr->second.id; tmp2.isImmediate1 = itr->second.isImmediate;
139+
tmp2.operand2 = -1; tmp2.isImmediate2 = false;
140+
tmp2.labelNo = -1;
141+
res.push_back(tmp2);
142+
del.insert(itr->first);
143+
}
144+
}
145+
for (int j : del) {
146+
substitution.erase(j);
147+
}
148+
res.push_back(tmp);
149+
break;
150+
}
151+
case MIDARRAYWRITE:
152+
{
153+
MidCode tmp = c;
154+
if (c.operand1 != -1 && !c.isImmediate1 &&
155+
substitution.find(c.operand1) != substitution.end()) {
156+
tmp.operand1 = substitution[c.operand1].id;
157+
tmp.isImmediate1 = substitution[c.operand1].isImmediate;
158+
}
159+
if (c.operand2 != -1 && !c.isImmediate2 &&
160+
substitution.find(c.operand2) != substitution.end()) {
161+
tmp.operand2 = substitution[c.operand2].id;
162+
tmp.isImmediate2 = substitution[c.operand2].isImmediate;
163+
}
164+
res.push_back(tmp);
165+
break;
166+
}
167+
case MIDREADINTEGER:
168+
case MIDREADCHAR:
169+
{
170+
MidCode tmp=c;
171+
set<int>del;
172+
for (map<int, Item>::iterator itr = substitution.begin();
173+
itr != substitution.end(); itr++) {
174+
if (!itr->second.isImmediate && itr->second.id == c.target) {
175+
MidCode tmp2;
176+
tmp2.op = MIDASSIGN; tmp2.target = itr->first;
177+
tmp2.operand1 = itr->second.id; tmp2.isImmediate1 = itr->second.isImmediate;
178+
tmp2.operand2 = -1; tmp2.isImmediate2 = false;
179+
tmp2.labelNo = -1;
180+
res.push_back(tmp2);
181+
del.insert(itr->first);
182+
}
183+
}
184+
for (int j : del) {
185+
substitution.erase(j);
186+
}
187+
res.push_back(tmp);
188+
break;
189+
}
190+
case MIDFUNC:
191+
case MIDPARA:
192+
case MIDNOP:
193+
res.push_back(c);
194+
break;
195+
case MIDGOTO:
196+
case MIDCALL:
197+
resEnd.push_back(c);
198+
break;
199+
case MIDPRINTSTRING:
200+
res.push_back(c);
201+
}
202+
203+
}
204+
//todo implement
205+
for (auto& i : substitution) {
206+
if (activeOut.find(i.first) != activeOut.end()) {
207+
MidCode tmp2;
208+
tmp2.op = MIDASSIGN; tmp2.target = i.first;
209+
tmp2.operand1 = i.second.id; tmp2.isImmediate1 = i.second.isImmediate;
210+
tmp2.operand2 = -1; tmp2.isImmediate2 = false;
211+
tmp2.labelNo = -1;
212+
res.push_back(tmp2);
213+
}
214+
}
215+
res.insert(res.end(), resEnd.begin(), resEnd.end());
216+
if (label != -1) {
217+
if (res.size() != 0) {
218+
res[0].labelNo = label;
219+
}
220+
else {
221+
MidCode tmp;
222+
tmp.op = MIDNOP;
223+
tmp.target = tmp.operand1 = tmp.operand2 = -1;
224+
tmp.isImmediate1 = tmp.isImmediate2 = false;
225+
tmp.labelNo=label;
226+
res.push_back(tmp);
227+
}
228+
}
229+
return res;
230+
}

BlockOptimization.h

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
#pragma once
2+
#include"MidCode.h"
3+
struct Item {
4+
int id;
5+
bool isImmediate=false;
6+
};
7+
class BlockOptimization {
8+
public:
9+
BlockOptimization(set<int>&activeOut);
10+
vector<MidCode>propagationInBlock(vector<MidCode>& v);
11+
private:
12+
set<int>activeOut;
13+
int label = -1;
14+
};

C0Compiler.vcxproj

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'" Label="Configuration">
2929
<ConfigurationType>Application</ConfigurationType>
3030
<UseDebugLibraries>true</UseDebugLibraries>
31-
<PlatformToolset>v142</PlatformToolset>
31+
<PlatformToolset>ClangCL</PlatformToolset>
3232
<CharacterSet>MultiByte</CharacterSet>
3333
</PropertyGroup>
3434
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'" Label="Configuration">
@@ -135,6 +135,7 @@
135135
</ItemGroup>
136136
<ItemGroup>
137137
<ClCompile Include="Block.cpp" />
138+
<ClCompile Include="BlockOptimization.cpp" />
138139
<ClCompile Include="DagMap.cpp" />
139140
<ClCompile Include="DeadCodeEliminator.cpp" />
140141
<ClCompile Include="FaultHandler.cpp" />
@@ -151,6 +152,7 @@
151152
</ItemGroup>
152153
<ItemGroup>
153154
<ClInclude Include="Block.h" />
155+
<ClInclude Include="BlockOptimization.h" />
154156
<ClInclude Include="DagMap.h" />
155157
<ClInclude Include="DeadCodeEliminator.h" />
156158
<ClInclude Include="FaultHandler.h" />

C0Compiler.vcxproj.filters

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,9 @@
101101
<ClCompile Include="DeadCodeEliminator.cpp">
102102
<Filter>源文件\BackEnd</Filter>
103103
</ClCompile>
104+
<ClCompile Include="BlockOptimization.cpp">
105+
<Filter>源文件\BackEnd</Filter>
106+
</ClCompile>
104107
</ItemGroup>
105108
<ItemGroup>
106109
<ClInclude Include="LexicalAnalyzer.h">
@@ -145,6 +148,9 @@
145148
<ClInclude Include="DeadCodeEliminator.h">
146149
<Filter>头文件\BackEnd</Filter>
147150
</ClInclude>
151+
<ClInclude Include="BlockOptimization.h">
152+
<Filter>头文件\BackEnd</Filter>
153+
</ClInclude>
148154
</ItemGroup>
149155
<ItemGroup>
150156
<Image Include="Annotation 2019-09-26 123820.jpg">

DagMap.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ void DagMap::handleMidCode(MidCode c){
9090
//只能出现在最前部,不会影响DAG
9191
beginning.push_back(c);
9292
break;
93-
//只能出现在最后,不会影响DAG
93+
//只能出现在最后,不会影响DAG
9494
case MIDGOTO:
9595
case MIDCALL:
9696
case MIDREADCHAR:

DeadCodeEliminator.cpp

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,9 @@ vector<MidCode> DeadCodeEliminator::eliminateDeadCode(vector<MidCode>& v) {
99
set<int>localActive=activeOut;
1010
for (int line = v.size() - 1; line >= 0; line--) {
1111
MidCode i = v[line];
12+
if (i.labelNo != -1) {
13+
label = i.labelNo;
14+
}
1215
//三地址运算
1316
if (i.op == MIDADD || i.op == MIDSUB || i.op == MIDMULT
1417
|| i.op == MIDDIV || i.op == MIDLSS || i.op == MIDLEQ
@@ -70,5 +73,18 @@ vector<MidCode> DeadCodeEliminator::eliminateDeadCode(vector<MidCode>& v) {
7073
res.push_back(v[i]);
7174
}
7275
}
76+
if (label != -1) {
77+
if (res.size() != 0) {
78+
res[0].labelNo = label;
79+
}
80+
else {
81+
MidCode tmp;
82+
tmp.op = MIDNOP;
83+
tmp.target = tmp.operand1 = tmp.operand2 = -1;
84+
tmp.isImmediate1 = tmp.isImmediate2 = false;
85+
tmp.labelNo = label;
86+
res.push_back(tmp);
87+
}
88+
}
7389
return res;
7490
}

DeadCodeEliminator.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
#pragma once
22
#include<set>
3-
#include"Midcode.h"
3+
#include"MidCode.h"
44
using namespace std;
55
class DeadCodeEliminator {
66
public:
@@ -9,4 +9,5 @@ class DeadCodeEliminator {
99
private:
1010
set<int>activeOut;
1111
vector<int>del;
12+
int label=-1;
1213
};

0 commit comments

Comments
 (0)