Skip to content

Commit 03f9da1

Browse files
committedDec 21, 2019
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;

‎debug.txt

+1,054-946
Large diffs are not rendered by default.

‎main.cpp

+100-22
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,115 @@
11
#include"main.h"
22
using namespace std;
33

4-
int main() {
5-
fstream f;
6-
f.open("debug.txt", ios_base::trunc | ios_base::out);
7-
8-
FaultHandler faultHandler("error.txt");
9-
faultHandler.debugOn();
4+
bool constantSubstitutionSwitch=false;
5+
bool inlineSwitch=false;
6+
bool propagationSwitch=false;
7+
bool dagMapSwitch=false;
8+
bool deadCodeEliminateSwitch=false;
9+
bool PeepHoleSwitch=false;
1010

11-
LexicalAnalyzer lexicalAnalyzer(faultHandler);
12-
lexicalAnalyzer.readAll("testfile.txt");
13-
lexicalAnalyzer.getNextSym();
11+
int main(int argc, char* argv[]) {
12+
string inputFile = "testfile.txt";
13+
string outputFile = "mips.txt";
14+
string errorFile = "";
15+
string debugFile = "";
16+
string recursiveDescendInformationFile = "";
17+
cout << "Final project of 'Complier Design' Course(Autumn2019),SCSE BUAA" << endl;
18+
19+
/*添加命令行参数支持与学校验收测试的支持*/
20+
if (argc==1) {
21+
//如果是学校验收的话无参数
22+
cout << "HomeWork acceptance Mode" << endl;
23+
errorFile = "error.txt";
24+
debugFile = "debug.txt";
25+
recursiveDescendInformationFile = "output.txt";
26+
//grammarAnalyzer.homeworkOn(true,true);
27+
constantSubstitutionSwitch = true;
28+
inlineSwitch = true;
29+
propagationSwitch = true;
30+
dagMapSwitch = true;
31+
deadCodeEliminateSwitch = true;
32+
PeepHoleSwitch = true;
33+
}
34+
else {
35+
//使用了命令行参数
36+
inputFile = argv[1];
37+
for (int i = 2; i < argc; i++) {
38+
string header = argv[i];
39+
if (header == "-opt") {
40+
constantSubstitutionSwitch = true;
41+
inlineSwitch = true;
42+
propagationSwitch = true;
43+
dagMapSwitch = true;
44+
deadCodeEliminateSwitch = true;
45+
PeepHoleSwitch = true;
46+
continue;
47+
}
1448

49+
if (i + 1 >= argc) {
50+
cout << "invalid parameter for operand " << header << endl;
51+
exit(0);
52+
}
53+
string para = argv[i + 1];
54+
if (header == "-o") {
55+
outputFile = para;
56+
}
57+
else if (header == "-d") {
58+
debugFile = para;
59+
}
60+
else if (header == "-rdi") {
61+
recursiveDescendInformationFile = "output.txt";
62+
}
63+
else if (header == "-h") {
64+
/*准备说明文档*/
65+
}
66+
else {
67+
cout << "invalid operand " << header << endl;
68+
exit(0);
69+
}
70+
i++;
71+
}
72+
}
73+
/*实例化各个组件===========================*/
74+
//符号表
1575
SymbolTable symbolTable;
1676
MidCode::table = &symbolTable;
1777
SubSymbolTable::table = &symbolTable;
18-
19-
MipsTranslator mips("mips.txt");
20-
78+
//错误处理
79+
FaultHandler faultHandler(errorFile);
80+
faultHandler.debugOn();
81+
//词法分析
82+
LexicalAnalyzer lexicalAnalyzer(faultHandler);
83+
lexicalAnalyzer.readAll(inputFile);
84+
lexicalAnalyzer.getNextSym();
85+
//目标代码生成
86+
MipsTranslator mips(outputFile);
87+
//编译器框架
2188
MidCodeFramework frame(mips);
22-
GrammarAnalyzer grammarAnalyzer(faultHandler,symbolTable,lexicalAnalyzer,frame,"output.txt");
23-
//grammarAnalyzer.homeworkOn(true,true);
24-
89+
//语法分析
90+
GrammarAnalyzer grammarAnalyzer(faultHandler,symbolTable,lexicalAnalyzer,
91+
frame,recursiveDescendInformationFile);
92+
/*开始编译*/
93+
fstream f;
94+
if (debugFile != "") {
95+
f.open(debugFile, ios_base::trunc | ios_base::out);
96+
}
2597
grammarAnalyzer.programme();
2698
frame.optimize();
27-
f << frame;
28-
f << endl << endl;
29-
frame.dumpNewMidCode(f);
30-
f << endl << endl;
31-
f << symbolTable;
32-
f << endl << endl;
99+
if (debugFile != "") {
100+
f << "BEFORE BACKEND OPTIMIZATION" << endl;
101+
f << frame;
102+
f << endl << endl;
103+
}
104+
if (debugFile!= "") {
105+
f << "BEFORE BACKEND OPTIMIZATION" << endl;
106+
frame.dumpNewMidCode(f);
107+
f << endl << endl;
108+
f << "SYMBOLTABLE" << endl;
109+
f << symbolTable;
110+
f << endl << endl;
111+
}
33112
frame.generateMips();
34-
35113
system("pause");
36114

37115
return 0;

‎main.h

+2-1
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,5 @@
1010
#include"MidCodeContainer.h"
1111
#include"Block.h"
1212
#include"FlowGraph.h"
13-
#include"MipsTranslator.h"
13+
#include"MipsTranslator.h"
14+
#include"OptimizeSwitch.h"

‎mips.txt

+448-444
Large diffs are not rendered by default.

‎testfile.txt

+96-107
Original file line numberDiff line numberDiff line change
@@ -1,129 +1,118 @@
1-
const int MAX_NUM = 128 ;
1+
int line;
2+
int chess[64];
3+
int count_chess,count_hanno,chess_record_num,hanno_record_num;
4+
int hanno_flag;
25

3-
int factorial(int n){
4-
if(n<=1) return (1);
5-
6-
return(n*factorial(n-1)) ;
6+
int hannuo(int n, char one, char two, char three)
7+
{
8+
if(hanno_flag == 1){
9+
return (0);
10+
}
11+
if (n == 1){
12+
count_hanno = count_hanno + 1;
13+
}
14+
else
15+
{
16+
hannuo(n - 1, one, three, two);
17+
count_hanno = count_hanno + 1;
18+
if(count_hanno == hanno_record_num){
19+
printf("Step ",hanno_record_num);
20+
printf("from ",one);
21+
printf("to ",three);
22+
hanno_flag = 1;
23+
}
24+
hannuo(n - 1, two, one, three);
25+
}
26+
return (0);
727
}
828

9-
int mod(int x, int y){
10-
x=x-x/y*y;
11-
12-
return (x) ;
29+
int Fibonacci(int n){
30+
if(n < 1){
31+
return (0);
32+
}else{
33+
if (n == 1){
34+
return (1);
35+
}else{
36+
return (Fibonacci(n-1)+Fibonacci(n-2));
37+
}
38+
}
1339
}
1440

15-
void swap(int x, int y){
16-
int temp;
1741

18-
printf("x = ", x) ;
19-
printf("y = ", y) ;
20-
temp = x;
21-
x=y;
22-
y=temp;
23-
printf("SWAP x = ", x) ;
24-
printf("SWAP y = ", y) ;
25-
}
42+
int Get_reminder(int n){
43+
int base;
44+
base = 1 ;
45+
if(n < 10){
46+
return (n);
47+
}
48+
while(n > base){
49+
base = base * 10;
50+
}
51+
base = base / 10;
52+
while(n > base){
53+
n = n - base;
54+
}
55+
return (Get_reminder(n));
2656

27-
int full_num(int n, int j, int a){
28-
return (n*100+j*10+a) ;
2957
}
3058

31-
int flower_num(int n, int j, int a){
32-
return (n*n*n+j*j*j+a*a*a) ;
59+
int Factorial(int i){
60+
if(i > 1){
61+
return (i * Factorial(i-1));
62+
}
63+
else{
64+
return (1);
65+
}
3366
}
3467

35-
void complete_flower_num()
36-
{
37-
int k[128];
38-
int i,j,n,s,x1,y;
39-
int m,k2,h,leap,x2;
40-
int a,b,c ;
41-
42-
43-
for(j=2;j< MAX_NUM ;j=j+1)
44-
{
45-
n = -1;
46-
s = j;
47-
for(i=1; i<j; i=i+1)
48-
{
49-
x1 = (j/i) * i ;
50-
if( mod(j,i) == 0 )
51-
{
52-
n = n + 1;
53-
s = s - i;
54-
if (n >= 128)
55-
printf("OVERFLOW!") ;
56-
else
57-
k[n] = i;
58-
}
68+
int Sum(int n){
69+
if(n < 1){
70+
return (n);
71+
}else{
72+
return (n + Sum(n - 1));
5973
}
74+
}
6075

61-
if(s==0)
62-
{
63-
printf("complete number: ",j);
64-
for(i=0;i<=n;i=i+1)
65-
printf(" ",k[i]);
66-
printf(" ") ;
76+
int Sum_Factorial(int n){
77+
if (n == 1){
78+
return (1);
79+
}else{
80+
return (Factorial(n)+Sum_Factorial(n - 1));
6781
}
68-
}
69-
70-
printf("---------------------------------------------------------------");
71-
printf("'water flower'number is:");
72-
y = 0 ;
73-
for(i=100;i<100+MAX_NUM;i=i+1){
74-
n=i/100;
75-
j=mod(i/10,10);
76-
a=mod(i,10);
77-
if(full_num(n,j,a)==flower_num(n, j, a)){
78-
k[y] = i ;
79-
y = y + 1 ;
80-
}
81-
}
82-
for(i = 0 ; i<y ; i=i+1){
83-
printf(" ", k[i]) ;
84-
}
85-
printf(" ") ;
86-
87-
88-
89-
printf("---------------------------------------------------------------");
90-
h = 0 ;
91-
leap = 1 ;
92-
for(m = 2 ; m <= MAX_NUM ; m=m+1)
93-
{
94-
k2 = m / 2;
95-
for(i=2; i<=k2; i=i+1){
96-
x2 = (m/i)*i ;
97-
if( mod(m,i) == 0)
98-
{
99-
leap=0;
100-
}
101-
}
102-
if(leap == 1)
103-
{
104-
printf(" ",m);
105-
h = h +1;
106-
107-
x2 = (h/10)*10 ;
108-
if( x2 == h)
109-
printf(" ");
110-
}
111-
leap=1;
112-
}
113-
114-
printf("The total is ",h);
115-
11682
}
11783

118-
84+
int Permutation(int m,int n){
85+
if(m == 0){
86+
return (1);
87+
}else{
88+
if (n == 0){
89+
return (1);
90+
}else{
91+
return (Permutation(m-1,n)+ Permutation(m,n-1));
92+
}
93+
}
94+
}
11995
void main()
12096
{
121-
int n ;
97+
int hanno_num;
98+
scanf(line);
99+
scanf(hanno_num);
100+
scanf(hanno_record_num);
101+
102+
count_chess = 0;
103+
count_hanno = 0;
104+
hanno_flag = 0;
105+
106+
hannuo(hanno_num,'A','B','C');
107+
printf( count_hanno);
108+
109+
printf(Fibonacci(4));
110+
printf(Factorial(5));
111+
printf(Sum(10));
122112

123-
n = factorial(5) ;
124-
printf("5 != ", n) ;
113+
printf(Sum_Factorial(5));
125114

126-
swap(5, 10 ) ;
115+
printf(Get_reminder(3464));
127116

128-
complete_flower_num() ;
117+
printf(Permutation(8,5));
129118
}

0 commit comments

Comments
 (0)
Please sign in to comment.