-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathMiniJavaCompiler.java
148 lines (122 loc) · 4.6 KB
/
MiniJavaCompiler.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
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
138
139
140
141
142
143
144
145
146
147
148
//Mark Klara
//CS 1622 - Project 3
//MiniJavaCompiler.java
import java_cup.runtime.*;
import java.io.*;
import syntaxtree.*;
import visitor.*;
import symboltable.*;
import IR.*;
import helper.*;
import linker.*;
import codegen.*;
import backpatching.*;
import regalloc.*;
import java.util.Set;
import java.util.List;
import java.util.Hashtable;
import java.util.HashMap;
import regalloc.flowgraph.*;
import regalloc.graph.*;
import java.util.BitSet;
public class MiniJavaCompiler
{
public static void main(String[] args)
{
if(args.length != 1)
{
System.out.println("Usage: java MiniJavaCompiler file.java");
System.exit(1);
}
Symbol parse_tree = null;
try
{
Reader reader = new java.io.InputStreamReader(new java.io.FileInputStream(args[0]), "UTF-8");
Parser parser = new Parser(new Lexer(reader));
parse_tree = parser.parse();
Program program = ((Program) parse_tree.value);
if(program != null)
{
if(parser.errorDetected == false)
{
//Visit the tree starting at root (program) to build the symbol table
BuildSymbolTableVisitor bstVisitor = new BuildSymbolTableVisitor();
bstVisitor.visit(program);
//Get a copy of the symboltable
Scope symbolTable = bstVisitor.getFirstScope();
if(symbolTable == null) //Check for error making symbol table
{
System.err.println("Oh no... the symbol table is null!");
return;
}
//Visit the tree starting at the root to check for undefined variables
UndefinedVariableVisitor undefinedVisitor = new UndefinedVariableVisitor(symbolTable);
undefinedVisitor.visit(program);
//Visit the tree starting at the root to check for type errors
TypeCheckingVisitor typeCheckVisitor = new TypeCheckingVisitor(symbolTable);
typeCheckVisitor.visit(program);
//If any errors occured, do not generate IR
if(bstVisitor.errorDetected || undefinedVisitor.errorDetected || typeCheckVisitor.errorDetected)
{
return;
}
//Generate IR
IRVisitor intermediateVisitor = new IRVisitor(symbolTable);
intermediateVisitor.visit(program);
List<Quadruple> IRList = intermediateVisitor.getIR();
Hashtable<Quadruple, List<Label>> labels = intermediateVisitor.getLabels();
HashMap<String, String> workList = intermediateVisitor.getWorkList();
//Temporary reg allocation
RegisterAllocator allocator = new RegisterAllocator();
SymbolTable symTable = (SymbolTable)symbolTable;
Hashtable <String, ClassSymbolTable> classes = symTable.getClasses();
List<String> keys = Helper.keysToSortedList(classes.keySet());;
for(int i = 0; i < keys.size(); i++) //Iterate over each class
{
ClassSymbolTable classSymTable = classes.get(keys.get(i));
classSymTable.calculateVarOffsets(); //Store variable offsets in the symbol table
Hashtable<String, MethodSymbolTable> methods = classSymTable.getMethods();
List<String> methodKeys = Helper.keysToSortedList(methods.keySet());
for(int j = 0; j < methodKeys.size(); j++)
{
MethodSymbolTable methSymTable = methods.get(methodKeys.get(j));
methSymTable.assignRegisters(allocator); //Temporary allocation to all method locals
}
}
//End temp reg allocation
//Backpatch the IR to resolve labels in jumps to methods
BackPatcher backPatch = new BackPatcher(IRList, workList);
backPatch.patch();
//Allocate Registers - Not complete (Inteferences are buggy and no coloring/allocation yet)
AssemFlowGraph asmFG = new AssemFlowGraph(IRList,labels);
List<List<Node>> func = asmFG.buildCFG();
for (int i = 0; i < func.size(); i++)
{
Liveness liv = new Liveness(func.get(i));
liv.calculateLive();
List<BitSet> liveOut = liv.getLiveOut();
InterferenceGraph iG = new InterferenceGraph(func.get(i),liveOut, liv.getAllVariables());
//iG.buildInterferenceGraph();
}
//Create output file
String fileName = args[0].substring(0, args[0].lastIndexOf(".")) + ".asm";
//Write MIPS
CodeGenerator gen = new CodeGenerator(IRList, labels, allocator, symTable, fileName);
gen.generateMIPS();
//Link runtime.asm file
Linker linker = new Linker("linker/runtime.asm", fileName);
linker.link();
}
}
}
catch (java.io.IOException e)
{
System.err.println("Unable to open file: " + args[0]);
}
catch (Exception e)
{
e.printStackTrace(System.err);
}
}
}