Skip to content

Commit 03f9da1

Browse files
release Version
1 parent 606ef13 commit 03f9da1

18 files changed

+1766
-1539
lines changed

C0Compiler.rar

-286 KB
Binary file not shown.

C0Compiler.vcxproj

+1
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,7 @@
165165
<ClInclude Include="MidCodeContainer.h" />
166166
<ClInclude Include="MidCodeFramework.h" />
167167
<ClInclude Include="MipsTranslator.h" />
168+
<ClInclude Include="OptimizeSwitch.h" />
168169
<ClInclude Include="PeepHoleOptimization.h" />
169170
<ClInclude Include="SubSymbolTable.h" />
170171
<ClInclude Include="SymbolTable.h" />

C0Compiler.vcxproj.filters

+3
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,9 @@
157157
<ClInclude Include="PeepHoleOptimization.h">
158158
<Filter>头文件\BackEnd</Filter>
159159
</ClInclude>
160+
<ClInclude Include="OptimizeSwitch.h">
161+
<Filter>头文件</Filter>
162+
</ClInclude>
160163
</ItemGroup>
161164
<ItemGroup>
162165
<Image Include="Annotation 2019-09-26 123820.jpg">

DagMap.cpp

+21-2
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,12 @@ void DagMap::handleMidCode(MidCode c){
135135
varToNode[c.target] = node;
136136
node->possibleNames.insert(c.target);
137137
assigned.insert(c.target);
138+
if (c.target > 0) {
139+
SymbolEntry* entry = MidCode::table->getSymbolById(c.target);
140+
if (entry->scope == "") {
141+
mustOut.insert(c.target);
142+
}
143+
}
138144
break;
139145
}
140146
case MIDNEGATE:
@@ -153,6 +159,12 @@ void DagMap::handleMidCode(MidCode c){
153159
varToNode[c.target] = node;
154160
node->possibleNames.insert(c.target);
155161
assigned.insert(c.target);
162+
if (c.target > 0) {
163+
SymbolEntry* entry = MidCode::table->getSymbolById(c.target);
164+
if (entry->scope == "") {
165+
mustOut.insert(c.target);
166+
}
167+
}
156168
break;
157169
}
158170
case MIDASSIGN:
@@ -177,6 +189,12 @@ void DagMap::handleMidCode(MidCode c){
177189
varToNode[c.target] = node1;
178190
node1->possibleNames.insert(c.target);
179191
assigned.insert(c.target);
192+
if (c.target > 0) {
193+
SymbolEntry* entry = MidCode::table->getSymbolById(c.target);
194+
if (entry->scope == "") {
195+
mustOut.insert(c.target);
196+
}
197+
}
180198
break;
181199
}
182200
case MIDPRINTCHAR:
@@ -297,8 +315,9 @@ vector<MidCode> DagMap::dumpMidCode() {
297315
//开始确定导出顺序
298316
vector<DagNode*>q;
299317
int i = 0;
300-
while (i < nodes.size()) {
301-
DagNode* tmp=nullptr;
318+
DagNode* tmp = nullptr;
319+
while (i < nodes.size() ) {
320+
tmp = nullptr;
302321
for (int j = 0; j < nodes.size(); j++) {
303322
if (nodes[j]->dumped) { continue; }
304323
else if (nodes[j]->isLeaf) {

FaultHandler.cpp

+4-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,10 @@
22

33
FaultHandler::FaultHandler(string filename) {
44
debug = false;
5-
fout.open(filename,ios_base::out|ios_base::trunc);
5+
if (filename == "") {
6+
filename = "/dev/null";
7+
}
8+
fout.open(filename, ios_base::out | ios_base::trunc);
69
messages[LEXICALERROR] = "lexical error";
710
messages[REDEFINED] = "redefined symbol";
811
messages[UNDEFINED] = "undifined symbol";

FaultHandler.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ class FaultHandler {
3636
void test();
3737

3838
private:
39-
ofstream fout;
39+
fstream fout;
4040
bool debug=false;
4141
map<FaultType, string>messages;
4242
};

FlowGraph.cpp

+17-10
Original file line numberDiff line numberDiff line change
@@ -54,19 +54,26 @@ void FlowGraph::addLink(Block* from, Block* to) {
5454
}
5555

5656
void FlowGraph::optimize() {
57+
if (dagMapSwitch) {
58+
activeVariableAnalyze();
59+
DAGoptimize();
60+
}
5761

58-
activeVariableAnalyze();
59-
DAGoptimize();
60-
61-
activeVariableAnalyze();
62-
blockOptimize();
62+
if (propagationSwitch) {
63+
activeVariableAnalyze();
64+
blockOptimize();
65+
}
6366

64-
activeVariableAnalyze();
65-
eliminateDeadCode();
67+
if (deadCodeEliminateSwitch) {
68+
activeVariableAnalyze();
69+
eliminateDeadCode();
70+
}
6671

67-
activeVariableAnalyze();
68-
activeVariablePerLine();
69-
peepholeOptimize();
72+
if (PeepHoleSwitch) {
73+
activeVariableAnalyze();
74+
activeVariablePerLine();
75+
peepholeOptimize();
76+
}
7077

7178
activeVariableAnalyze();
7279
variableSummary();

FlowGraph.h

+1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
#include<set>
44
#include"Block.h"
55
#include"MidCodeContainer.h"
6+
#include"OptimizeSwitch.h"
67
using namespace std;
78
class FlowGraph {
89
public:

GrammarAnalyzer.cpp

+6-3
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@
33
/*构造器函数*/
44
GrammarAnalyzer::GrammarAnalyzer(FaultHandler& _f, SymbolTable& _s, LexicalAnalyzer& _l,MidCodeFramework& _raw,string file)
55
:f(_f),table(_s),raw(_raw), lex(_l) {
6+
if (file == "") {
7+
file = "/dev/null";
8+
}
69
out.open(file,ios_base::trunc|ios_base::out);
710
currentScope="";
811
}
@@ -749,7 +752,7 @@ void GrammarAnalyzer::parameterList(SymbolEntry* entry) {
749752

750753
/*<复合语句>*/
751754
void GrammarAnalyzer::compoundSentence(){
752-
inlineable = true;
755+
inlineable = inlineSwitch;
753756
if (currentScope == "main") {
754757
inlineable = false;
755758
}
@@ -840,7 +843,7 @@ ReturnBundle GrammarAnalyzer::factor() {
840843
res.isChar = true;
841844
}
842845
if (!error) {
843-
if (entry->type == TYPECHARCONST || entry->type == TYPEINTCONST) {
846+
if (constantSubstitutionSwitch&&(entry->type == TYPECHARCONST || entry->type == TYPEINTCONST)) {
844847
res.id = entry->initValue;
845848
res.isImmediate = true;
846849
}
@@ -1845,7 +1848,7 @@ void GrammarAnalyzer::loopSentence() {
18451848

18461849

18471850
if (entry2 != NULL&&entry3!=NULL) {
1848-
if (entry3->type == TYPECHARCONST || entry3->type == TYPEINTCONST) {
1851+
if (constantSubstitutionSwitch&&(entry3->type == TYPECHARCONST || entry3->type == TYPEINTCONST)) {
18491852
raw.midCodeInsert(op, entry2->id,
18501853
entry3->initValue, true, step, true, MIDNOLABEL);
18511854
}

GrammarAnalyzer.h

+1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
#include"SymbolTable.h"
66
#include"MidCode.h"
77
#include"MidCodeFramework.h"
8+
#include"OptimizeSwitch.h"
89
using namespace std;
910
struct ReturnBundle {
1011
bool isChar=false;

MidCodeFrameWork.cpp

+3-1
Original file line numberDiff line numberDiff line change
@@ -79,9 +79,11 @@ void MidCodeFramework::dumpNewMidCode(ostream&out) {
7979
MidCode::table->getSubSymbolTableByName("")->dumpMidCode(out);
8080
for (FlowGraph& g : graph) {
8181
string functionName = MidCode::table->getSymbolById(g.functionId)->name;
82-
MidCode::table->getSubSymbolTableByName(functionName)->dumpMidCode(out);
82+
8383
for (Block* b : g.graph) {
8484
for (MidCode& c : b->v) {
85+
if(c.op==MIDFUNC)
86+
MidCode::table->getSubSymbolTableByName(functionName)->dumpMidCode(out);
8587
out << c;
8688
}
8789
}

MipsTranslator.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@ void MipsTranslator::SregisterAlloc() {
136136
//若没有找到满足条件的,暂时是从头找一个
137137
//此处如何选择可进行优化
138138
int chosen = *(var.begin());
139-
cout << "remove var No." << chosen << endl;
139+
//cout << "remove var No." << chosen << endl;
140140
for (int j : m[chosen]) {
141141
m[j].erase(chosen);
142142
}

OptimizeSwitch.h

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
#pragma once
2+
extern bool constantSubstitutionSwitch;
3+
extern bool inlineSwitch;
4+
extern bool propagationSwitch;
5+
extern bool dagMapSwitch;
6+
extern bool deadCodeEliminateSwitch;
7+
extern bool PeepHoleSwitch;

0 commit comments

Comments
 (0)