-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCompiler.java
executable file
·67 lines (59 loc) · 2.34 KB
/
Compiler.java
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
import antlr.*;
import assembly.ASMModule;
import Utils.*;
import AST.*;
import IR.IRprogram;
import java.io.BufferedWriter;
import java.io.FileInputStream;
import java.io.FileWriter;
import java.io.IOException;
import back.*;
import org.antlr.v4.runtime.*;
import org.antlr.v4.runtime.tree.*;
import front.*;
public class Compiler {
public static void writeToFile(String fileName, String content) {
try (BufferedWriter writer = new BufferedWriter(new FileWriter(fileName))) {
writer.write(content);
} catch (IOException e) {
e.printStackTrace();
}
}
public static void main(String[] args) throws Exception {
try {
// CharStream input = CharStreams.fromStream(new FileInputStream("1.cpp"));
CharStream input = CharStreams.fromStream(System.in);
//
MxLexer lexer = new MxLexer(input);
lexer.removeErrorListeners();
lexer.addErrorListener(new MxErrorListener());
CommonTokenStream tokens = new CommonTokenStream(lexer);
MxParser parser = new MxParser(tokens);
parser.removeErrorListeners();
parser.addErrorListener(new MxErrorListener());
ParseTree tree = parser.program();
ASTbuilder astBuilder = new ASTbuilder();
ProgramNode ast = (ProgramNode) astBuilder.visit(tree);
GlobalScope globalScope = new GlobalScope();
new SymbolCollector(globalScope).visit(ast);
new SemanticChecker(globalScope).visit(ast);
IRprogram irprogram = new IRprogram();
new IRBuilder(globalScope, irprogram).visit(ast);
new CFGBuilder(irprogram);
new Mem2Reg(irprogram).work();
// String content1 = irprogram.toString();
// writeToFile("1.ll", content1);
ASMModule asmModule = new ASMModule();
new InstSelector(asmModule).visit(irprogram);
// String content_1 = asmModule.toString();
// writeToFile("1.shit", content_1);
// new RegAllocator(asmModule).work();
new modernAllocator(asmModule).work();
String content = asmModule.toString();
// writeToFile("1.s", content);
System.out.print(content);
} catch (Throwable gb) {
System.out.print(gb.toString());
}
}
}