Skip to content

Commit 3bf8522

Browse files
临时AC
1 parent c4d0505 commit 3bf8522

22 files changed

+1150
-146
lines changed

Block.cpp

+70-8
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
#include"Block.h"
1+
#include"Block.h"
22
int Block::count = 0;
33

44
Block::Block() {
@@ -17,10 +17,11 @@ void Block::addNext(Block* b) {
1717
next.push_back(b);
1818
}
1919

20+
/*扫描这个基本块中的use def集合*/
2021
void Block::useDefScan() {
2122
for (int ii = v.size() - 1; ii >= 0;ii--) {
2223
MidCode i = v[ii];
23-
//正常三地址运算,注意立即数问题
24+
//正常三地址运算,注意立即数问题
2425
if (i.op == MIDADD || i.op == MIDSUB || i.op == MIDMULT
2526
|| i.op == MIDDIV || i.op == MIDLSS || i.op == MIDLEQ
2627
|| i.op == MIDGRE || i.op == MIDGEQ || i.op == MIDEQL
@@ -36,15 +37,15 @@ void Block::useDefScan() {
3637
use.insert(i.operand2);
3738
}
3839
}
39-
//只使用operand1
40+
//只使用operand1
4041
else if (i.op == MIDPUSH || i.op == MIDRET||i.op==MIDBNZ||i.op==MIDBZ
4142
||i.op==MIDPRINTINT||i.op==MIDPRINTCHAR) {
42-
if (!i.isImmediate1) {
43+
if (!i.isImmediate1&&i.operand1!=-1) {
4344
def.erase(i.operand1);
4445
use.insert(i.operand1);
4546
}
4647
}
47-
//使用operand1并返回值,其中assign需要考虑排除-1
48+
//使用operand1并返回值,其中assign需要考虑排除-1
4849
else if (i.op == MIDNEGATE || i.op == MIDASSIGN) {
4950
use.erase(i.target);
5051
def.insert(i.target);
@@ -54,7 +55,7 @@ void Block::useDefScan() {
5455
}
5556

5657
}
57-
//数组写操作只会使用2个操作数,但是不会对target造成影响,因为数组地址没变
58+
//数组写操作只会使用2个操作数,但是不会对target造成影响,因为数组地址没变
5859
else if (i.op == MIDARRAYWRITE) {
5960
if (!i.isImmediate1) {
6061
def.erase(i.operand1);
@@ -64,7 +65,7 @@ void Block::useDefScan() {
6465
def.erase(i.operand2);
6566
use.insert(i.operand2);
6667
}
67-
}//只使用target
68+
}//只使用target
6869
else if (i.op == MIDREADCHAR || i.op == MIDREADINTEGER) {
6970
use.erase(i.target);
7071
def.insert(i.target);
@@ -73,7 +74,6 @@ void Block::useDefScan() {
7374
}
7475
}
7576

76-
7777
bool Block::activeVariableAnalyzeEpoch() {
7878
int oldsize = activeIn.size();
7979
for (Block* i : next) {
@@ -85,6 +85,68 @@ bool Block::activeVariableAnalyzeEpoch() {
8585

8686
}
8787

88+
vector<vector<int>>Block::conflictEdgeAnalyze(){
89+
/*冲突边的构建原则是,只要在变量定义处活跃的变量全都算是冲突*/
90+
vector<vector<int>>res;
91+
set<int>localActive;
92+
localActive = activeOut;
93+
for (int line = v.size() - 1; line >= 0; line--) {
94+
MidCode i = v[line];
95+
//正常三地址运算
96+
if (i.op == MIDADD || i.op == MIDSUB || i.op == MIDMULT
97+
|| i.op == MIDDIV || i.op == MIDLSS || i.op == MIDLEQ
98+
|| i.op == MIDGRE || i.op == MIDGEQ || i.op == MIDEQL
99+
|| i.op == MIDNEQ || i.op == MIDARRAYGET) {
100+
localActive.erase(i.target);
101+
if (!i.isImmediate1) {
102+
localActive.insert(i.operand1);
103+
}
104+
if (!i.isImmediate2) {
105+
localActive.insert(i.operand2);
106+
}
107+
}
108+
//只使用operand1
109+
else if (i.op == MIDPUSH || i.op == MIDRET || i.op == MIDBNZ || i.op == MIDBZ
110+
|| i.op == MIDPRINTINT || i.op == MIDPRINTCHAR) {
111+
if (!i.isImmediate1&&i.operand1!=-1) {
112+
localActive.insert(i.operand1);
113+
}
114+
}
115+
//使用operand1并返回值,其中assign需要考虑排除-1
116+
else if (i.op == MIDNEGATE || i.op == MIDASSIGN) {
117+
localActive.erase(i.target);
118+
if (!i.isImmediate1 && i.operand1 != -1) {
119+
localActive.insert(i.operand1);
120+
}
121+
}
122+
//数组写操作只会使用2个操作数,但是不会对target造成影响,因为数组地址没变
123+
else if (i.op == MIDARRAYWRITE) {
124+
if (!i.isImmediate1) {
125+
localActive.insert(i.operand1);
126+
}
127+
if (!i.isImmediate2) {
128+
localActive.insert(i.operand2);
129+
}
130+
}
131+
//只有target
132+
else if (i.op == MIDREADCHAR || i.op == MIDREADINTEGER) {
133+
localActive.erase(i.target);
134+
}
135+
//现在找有target的计算冲突边
136+
if (i.op == MIDADD || i.op == MIDSUB || i.op == MIDMULT
137+
|| i.op == MIDDIV || i.op == MIDLSS || i.op == MIDLEQ
138+
|| i.op == MIDGRE || i.op == MIDGEQ || i.op == MIDEQL
139+
|| i.op == MIDNEQ || i.op == MIDARRAYGET ||
140+
i.op == MIDNEGATE || i.op == MIDASSIGN ||
141+
i.op == MIDREADCHAR || i.op == MIDREADINTEGER) {
142+
for (int j : localActive) {
143+
res.push_back({ i.target,j });
144+
}
145+
}
146+
}
147+
return res;
148+
}
149+
88150
set<int>Block::setUnion(set<int> a, set<int> b) {
89151
set<int>res;
90152
for (int i : a) {

Block.h

+6-5
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
#pragma once
1+
#pragma once
22
#include<vector>
33
#include<set>
44
#include"MidCode.h"
@@ -7,8 +7,8 @@ using namespace std;
77
class Block {
88
public:
99
static int count;
10-
int id;
11-
int functionId;
10+
int id;//每个block有唯一确定的id
11+
int functionId;//所属函数id
1212
vector<Block*>prev;
1313
vector<Block*>next;
1414
set<int>def;
@@ -23,9 +23,10 @@ class Block {
2323
void addNext(Block* b);
2424
void useDefScan();
2525
bool activeVariableAnalyzeEpoch();
26+
vector<vector<int>>conflictEdgeAnalyze();
2627
friend ostream& operator<<(ostream& out, Block b);
27-
set<int> setUnion(set<int> a, set<int> b);
28-
set<int> setDifference(set<int> a, set<int> b);
28+
static set<int> setUnion(set<int> a, set<int> b);
29+
static set<int> setDifference(set<int> a, set<int> b);
2930

3031

3132
};

C0Compiler.vcxproj

+4-1
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,7 @@
128128
<Text Include="error.txt" />
129129
<Text Include="grammar.txt" />
130130
<Text Include="grammarRegroup.txt" />
131+
<Text Include="mips.txt" />
131132
<Text Include="testfile.txt" />
132133
<Text Include="output.txt" />
133134
<Text Include="teststorage.txt" />
@@ -140,7 +141,8 @@
140141
<ClCompile Include="LexicalAnalyzer.cpp" />
141142
<ClCompile Include="main.cpp" />
142143
<ClCompile Include="MidCodeContainer.cpp" />
143-
<ClCompile Include="MideCode.cpp" />
144+
<ClCompile Include="MidCode.cpp" />
145+
<ClCompile Include="MipsGenerator.cpp" />
144146
<ClCompile Include="SubSymbolTable.cpp" />
145147
<ClCompile Include="SymbolTable.cpp" />
146148
</ItemGroup>
@@ -153,6 +155,7 @@
153155
<ClInclude Include="main.h" />
154156
<ClInclude Include="MidCode.h" />
155157
<ClInclude Include="MidCodeContainer.h" />
158+
<ClInclude Include="MipsGenerator.h" />
156159
<ClInclude Include="SubSymbolTable.h" />
157160
<ClInclude Include="SymbolTable.h" />
158161
</ItemGroup>

C0Compiler.vcxproj.filters

+12-3
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,9 @@
5454
<Text Include="debug.txt">
5555
<Filter>资源文件</Filter>
5656
</Text>
57+
<Text Include="mips.txt">
58+
<Filter>资源文件</Filter>
59+
</Text>
5760
</ItemGroup>
5861
<ItemGroup>
5962
<ClCompile Include="main.cpp">
@@ -68,9 +71,6 @@
6871
<ClCompile Include="GrammarAnalyzer.cpp">
6972
<Filter>源文件\FrontEnd</Filter>
7073
</ClCompile>
71-
<ClCompile Include="MideCode.cpp">
72-
<Filter>源文件\Midcode&amp;Symbol</Filter>
73-
</ClCompile>
7474
<ClCompile Include="MidCodeContainer.cpp">
7575
<Filter>源文件\Midcode&amp;Symbol</Filter>
7676
</ClCompile>
@@ -86,6 +86,12 @@
8686
<ClCompile Include="SubSymbolTable.cpp">
8787
<Filter>源文件\Midcode&amp;Symbol</Filter>
8888
</ClCompile>
89+
<ClCompile Include="MipsGenerator.cpp">
90+
<Filter>源文件\BackEnd</Filter>
91+
</ClCompile>
92+
<ClCompile Include="MidCode.cpp">
93+
<Filter>源文件\Midcode&amp;Symbol</Filter>
94+
</ClCompile>
8995
</ItemGroup>
9096
<ItemGroup>
9197
<ClInclude Include="LexicalAnalyzer.h">
@@ -118,6 +124,9 @@
118124
<ClInclude Include="SubSymbolTable.h">
119125
<Filter>头文件\Midcode&amp;Symbol</Filter>
120126
</ClInclude>
127+
<ClInclude Include="MipsGenerator.h">
128+
<Filter>头文件\BackEnd</Filter>
129+
</ClInclude>
121130
</ItemGroup>
122131
<ItemGroup>
123132
<Image Include="Annotation 2019-09-26 123820.jpg">

0 commit comments

Comments
 (0)