Repository navigation
Expand file tree
/
Copy pathMiniJavaCompiler.java
More file actions
138 lines (116 loc) · 3.75 KB
/
Copy pathMiniJavaCompiler.java
File metadata and controls
138 lines (116 loc) · 3.75 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
138
//Mark Klara
//mak241@pitt.edu
//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 java.util.List;
import java.util.Hashtable;
import java.util.HashMap;
import regalloc.flowgraph.*;
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();
//Backpatch the IR to resolve labels in jumps to methods
BackPatcher backPatch = new BackPatcher(IRList, workList);
backPatch.patch();
//Print IR
for(int i = 0; i < IRList.size(); i++)
{
if(labels.containsKey(IRList.get(i)))
{
List<Label> temp = labels.get(IRList.get(i));
for(int j = 0; j < temp.size(); j++)
{
if(temp.get(j).printBefore)
System.out.println(temp.get(j).toString());
}
}
System.out.println("\t" + IRList.get(i).toString());
if(labels.containsKey(IRList.get(i)))
{
List<Label> temp = labels.get(IRList.get(i));
for(int j = 0; j < temp.size(); j++)
{
if(!temp.get(j).printBefore)
System.out.println(temp.get(j).toString());
}
}
}
//Create output file
String fileName = args[0].substring(0, args[0].lastIndexOf(".")) + ".asm";
//Write MIPS
CodeGenerator gen = new CodeGenerator(IRList, labels, fileName);
gen.generateMIPS();
//System.out.println("\n Testing Graph");
//AssemFlowGraph asmFG = new AssemFlowGraph(IRList,labels);
//asmFG.buildCFG();
//asmFG.printGraph();
//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);
}
}
}