-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathCompiler.java
More file actions
110 lines (96 loc) · 3.86 KB
/
Compiler.java
File metadata and controls
110 lines (96 loc) · 3.86 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
import SymTable.SymItem;
import SymTable.SymbolTable;
import back.MipsGenerator;
import front.ASD.CompUnit;
import front.ErrorRecorder;
import front.LexicalAnalyser;
import front.Parser;
import SymTable.SymLinker;
import mid.DataFlower;
import mid.MidCodeList;
import mid.VarConfliction;
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.HashMap;
public class Compiler {
public static int debugging = 2;
private static final String inputFilePath = "testfile.txt";
public static boolean branch_opt = true;
private static String readFile() throws IOException {
InputStream is = new FileInputStream(inputFilePath);
BufferedReader reader = new BufferedReader(new InputStreamReader(is));
StringBuilder buffer = new StringBuilder();
String line = reader.readLine();
while (line != null) {
buffer.append(line).append("\n");
line = reader.readLine();
}
reader.close();
is.close();
return buffer.toString();
}
public static void main(String[] args) throws IOException {
front.Compiler.branch_opt = branch_opt;
LexicalAnalyser analyser = new LexicalAnalyser();
analyser.analyze(readFile());
if (debugging == 1) {
System.out.print(analyser.result());
}
// parsing the source code
Parser parser = new Parser(analyser.getTokenList());
if (!parser.analyze()){
System.out.println("Error parsing your testfile");
ErrorRecorder.PrintErrorRecord();
return;
}
CompUnit unit = parser.getASDTree();
if (debugging == 1) {
unit.printTestInfo();
}
// linking
SymLinker symLinker = new SymLinker(unit);
symLinker.link();
if (!ErrorRecorder.withoutError()) {
System.out.println("Error linking your testfile");
ErrorRecorder.PrintErrorRecord();
return;
}
//generate mid_code
HashMap<String, ArrayList<SymItem>> funcTables = symLinker.getFuncTable();
System.out.println("finish parsing your code, start generating mid-code...");
MidCodeList midCodeList = new MidCodeList(symLinker.node2tableItem, funcTables);
unit.gen_mid(midCodeList);
midCodeList.printCode("testfile1_19375341_孙泽一_优化前中间代码.txt");
SymbolTable global_table = symLinker.getBlockLoc2table().get("<0,0>");
midCodeList.addTmp(funcTables, global_table);
// midCodeList.remove_useless_code();
for (int i = 0; i < 20; i++) {
DataFlower.refresh(midCodeList);
// mid_code_optimization
midCodeList.remove_redundant_arith();
midCodeList.remove_redundant_compare();
midCodeList.arith_to_assign();
midCodeList.remove_redundant_assign();
midCodeList.remove_redundant_tmp();
midCodeList.remove_redundant_jump();
// data_flow_analysis
DataFlower.define_point_ranking(midCodeList);
DataFlower.divide_base_block(midCodeList);
// DataFlower.printBlockInfo("block_info_before.txt");
DataFlower.remove_redundant();
DataFlower.const_broadcast();
// DataFlower.printBlockInfo("block_info_after.txt");
}
VarConfliction.get_func_vars(midCodeList);
midCodeList.printCode("testfile1_19375341_孙泽一_优化后中间代码.txt");
// generate mips_code
System.out.println("finish generating mid code, start generating mips-code...");
MipsGenerator mips = new MipsGenerator(midCodeList.midCodes, midCodeList.strCons, funcTables, global_table);
mips.translate();
mips.toFile();
}
}