-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCompiler.java
More file actions
137 lines (126 loc) · 5.19 KB
/
Compiler.java
File metadata and controls
137 lines (126 loc) · 5.19 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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
import exception.NoMatchToken;
import exception.ParseError;
import exception.ParseOutOfBound;
import ir.IntermediateInstruction;
import ir.LoadArrayValueIr;
import ir.PassParamIr;
import ir.StorePointerValueIr;
import lex.Lexer;
import mips.Annotation;
import mips.MipsCode;
import mips.TargetCodeContainer;
import parse.Parser;
import symbol.FunctionLocalSymbolTables;
import symbol.GlobalSymbolTable;
import symbol.SymbolTable;
import symbol.SymbolTableItem;
import symbol.type.Symbol;
import token.Token;
import treeNode.CompUnit;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
public class Compiler {
public static void main(String[] args) throws IOException,
NoMatchToken, ParseError, ParseOutOfBound {
Lexer lexer = new Lexer();
List<Token> tokens = lexer.tokenization();
Parser parser = new Parser(tokens);
CompUnit res = parser.parseCompUnit();
List<IntermediateInstruction> ir = new ArrayList<>();
if (res != null) {
ir = res.generateIr(0);
}
outputIr(ir);
toMips(ir);
outputMips(TargetCodeContainer.codes);
System.out.println("Global SymbolTable:");
outputSymbolTable(GlobalSymbolTable.symbolTable);
for (String i : FunctionLocalSymbolTables.allLocalSymbolTables.keySet()) {
SymbolTable symbolTable = FunctionLocalSymbolTables.allLocalSymbolTables.get(i);
System.out.println(i + " SymbolTable:");
outputSymbolTable(symbolTable);
}
}
public static void outputSymbolTable(SymbolTable symbolTable) {
List<SymbolTableItem> items = symbolTable.getItems();
for (int i = 0; i < items.size(); i++) {
System.out.println("type: " + items.get(i).getSymbolType() + " name: "
+ items.get(i).getName() + " headAddr: " + items.get(i).getAddr()
+ " size: " + items.get(i).getSize());
}
}
public static void toMips(List<IntermediateInstruction> ir) {
// TargetCodeContainer.codes.add(new Add("$0", "$sp", "$fp"));
// TargetCodeContainer.codes.add(new Add("$0", "$fp", "$s0"));
int pos = 1;
for (IntermediateInstruction i : ir) {
try {
if (i instanceof LoadArrayValueIr) {
TargetCodeContainer.codes.add((new Annotation("begin loadArrayValue")));
} else if (i instanceof StorePointerValueIr) {
TargetCodeContainer.codes.add((new Annotation("begin StorePointerValue")));
} else if (i instanceof PassParamIr) {
TargetCodeContainer.codes.add((new Annotation("begin PassParam")));
}
List<MipsCode> list = i.toMips();
if (list != null) {
TargetCodeContainer.codes.addAll(list);
}
if (i instanceof LoadArrayValueIr) {
TargetCodeContainer.codes.add((new Annotation("END loadArrayValue")));
} else if (i instanceof StorePointerValueIr) {
TargetCodeContainer.codes.add((new Annotation("END StorePointerValue")));
} else if (i instanceof PassParamIr) {
TargetCodeContainer.codes.add((new Annotation("End PassParam")));
}
} catch (NullPointerException e) {
e.printStackTrace();
System.out.println(i.toString());
System.out.println(pos);
break;
}
// if (i instanceof LoadArrayValueIr) {
// TargetCodeContainer.codes.add((new Annotation("begin loadArrayValue")));
// } else if (i instanceof StorePointerValueIr) {
// TargetCodeContainer.codes.add((new Annotation("begin StorePointerValue")));
// }
// List<MipsCode> list = i.toMips();
// if (list != null) {
// TargetCodeContainer.codes.addAll(list);
// }
// if (i instanceof LoadArrayValueIr) {
// TargetCodeContainer.codes.add((new Annotation("END loadArrayValue")));
// } else if (i instanceof StorePointerValueIr) {
// TargetCodeContainer.codes.add((new Annotation("END StorePointerValue")));
// }
pos++;
}
}
public static void outputMips(List<MipsCode> codes) throws IOException {
File file = new File("mips.txt");
FileWriter writer = new FileWriter(file);
for (MipsCode i : codes) {
writer.write(i.toString());
writer.write("\n");
}
writer.flush();
}
public static void outputIr(List<IntermediateInstruction> ir) throws IOException {
File file = new File("ir.txt");
FileWriter writer = new FileWriter(file);
for (IntermediateInstruction i : ir) {
writer.write(i.toString());
writer.write("\n");
}
writer.flush();
}
public static void output(String str) throws IOException {
File file = new File("output.txt");
FileWriter writer = new FileWriter(file);
writer.write(str);
writer.flush();
}
}