Skip to content

Commit 22ed29d

Browse files
final
1 parent 444988e commit 22ed29d

9 files changed

+66
-1920
lines changed

FlowGraph.cpp

+7-2
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,20 @@
11
#include"FlowGraph.h"
2-
2+
vector<Block*>FlowGraph::allGraph;
33
FlowGraph::FlowGraph(MidCodeContainer& c) {
44
map<int, Block*>codeToBlock;
55
map<int, Block*>labelToBlock;
66

77
functionName = c.functionName;
88
functionId = MidCode::table->getSymbolByName("", functionName)->id;
99
Block* block = new Block(functionId);
10+
allGraph.push_back(block);
1011
for (int i = 0; i < c.v.size(); i++) {
1112
if (c.v[i].labelNo != MIDNOLABEL||
1213
(i != 0 && ( c.v[i - 1].op == MIDBNZ ||c.v[i - 1].op == MIDBZ||c.v[i-1].op==MIDCALL
1314
|| c.v[i - 1].op == MIDREADINTEGER|| c.v[i - 1].op == MIDREADCHAR))) {
1415
Block* oldBlock = block;
1516
block = new Block(functionId);
17+
allGraph.push_back(block);
1618
graph.push_back(oldBlock);
1719
if (!(i != 0 && (c.v[i - 1].op == MIDGOTO || c.v[i - 1].op == MIDRET))) {
1820
addLink(oldBlock, block);
@@ -22,6 +24,7 @@ FlowGraph::FlowGraph(MidCodeContainer& c) {
2224
Block* oldBlock = block;
2325
block = new Block(functionId);
2426
graph.push_back(oldBlock);
27+
allGraph.push_back(block);
2528
}
2629
block->insert(c.v[i]);
2730
codeToBlock[i] = block;
@@ -43,7 +46,9 @@ FlowGraph::FlowGraph(MidCodeContainer& c) {
4346
}
4447
}
4548
}
46-
49+
FlowGraph::~FlowGraph() {
50+
51+
}
4752

4853
void FlowGraph::addLink(Block* from, Block* to) {
4954
if (from == NULL || to == NULL) {

FlowGraph.h

+2-1
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,15 @@
77
using namespace std;
88
class FlowGraph {
99
public:
10+
~FlowGraph();
1011
set<int>globalVariable;
1112
set<int>tmpVariable;
1213
set<int>allVariable;
1314
string functionName;
1415
int functionId;
1516
vector<Block*>graph;
1617
vector<vector<int>>conflictEdges;
17-
18+
static vector<Block*>allGraph;
1819
FlowGraph(MidCodeContainer& container);
1920
friend ostream& operator<<(ostream& out, FlowGraph& f);
2021
void optimize();

MidCodeFrameWork.cpp

+5
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,11 @@
33
MidCodeFramework::MidCodeFramework(MipsTranslator& _mips):mips(_mips) {
44
}
55

6+
MidCodeFramework::~MidCodeFramework(){
7+
for (Block* b : FlowGraph::allGraph) {
8+
delete b;
9+
}
10+
}
611
void MidCodeFramework::functionStart(string name) {
712
container = MidCodeContainer();
813
container.functionName = name;

MidCodeFramework.h

+1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ struct ReturnBundle;
77
class MidCodeFramework {
88
public :
99
MidCodeFramework(MipsTranslator& _mips);
10+
~MidCodeFramework();
1011
vector<MidCodeContainer>functionContainer;
1112
MidCodeContainer container;
1213
void functionStart(string name);

README.md

+38-2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,39 @@
11
# C0_Compiler
2-
Project of 'Compiler Technology' Course of SCSE,BUAA
3-
没完工呢
2+
Final Project of ' Design of Compiler Technology' Course of SCSE,BUAA
3+
### Overview
4+
This is the repository for the project of "Design of Compiler Technology".This project contains a simple compiler which can translate a 'C0' language to MIPS assembly language.
5+
### About the 'C0 language'
6+
C0 language is a slightly modified and simplified High-level programming language based on C language. The grammar of this 'C0' language is LL(1) grammar.<br>
7+
The Grammar of this C0 language is shown bellow.
8+
[!](Annotation 2019-09-26 123820.jpg)
9+
### About the simple compiler
10+
This compiler contains a 'frontend' and a 'backend.<br>
11+
In the front end we implemented Lexical analysis,Grammar analysis ( recursive-descend method), and the fault handling. The front end can translate the original C0 language to midcode.
12+
The backend translates midcode to the target assembly language : mips. The following optimization is Implemented:
13+
14+
- Active variables analysis
15+
- automatic inline function
16+
- DAG optimization (elimination of shared expression)
17+
- Elimination of dead code
18+
- substitution for assign instruction
19+
- constant proporgation and substitution
20+
- peephole optimization
21+
- Simple calculation in compilation phase
22+
- graph coloring algorithm for distribution of global register
23+
- distribution of temporary register based on active variables analysis
24+
### Development Environment
25+
Editor:VS2019<br>
26+
Complier: clang++ 8.0 msvc g++7.2.0<br>
27+
Debugger: gdb debugger of msvc
28+
### How to use this compiler
29+
In command line:
30+
```
31+
usage: C0compiler.exe sourcefile
32+
[-o outputfile]
33+
[-d debugInformation]
34+
[-rdi recursiveDescendInformationFile]
35+
[-h] help document
36+
[-opt] turn on the optimization
37+
```
38+
if no argument is passed to the complier,the program will be run under HomeWork Acceptance Test Mode. Under such circumstance,the source file must be "testfile.txt" and the output file must be "mips.txt"
39+

0 commit comments

Comments
 (0)