Skip to content

Commit 5aa6cbd

Browse files
committed
Removed unused DataSeg code && fixed dependency on labels for method termination
1 parent 31e8346 commit 5aa6cbd

4 files changed

Lines changed: 19 additions & 94 deletions

File tree

‎MiniJavaCompiler.java‎

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,6 @@ public static void main(String[] args)
7474

7575
List<Quadruple> IRList = intermediateVisitor.getIR();
7676
Hashtable<Quadruple, List<Label>> labels = intermediateVisitor.getLabels();
77-
List<Variable> varList = intermediateVisitor.getVars();
7877
HashMap<String, String> workList = intermediateVisitor.getWorkList();
7978

8079
//Backpatch the IR to resolve labels in jumps to methods
@@ -112,7 +111,7 @@ public static void main(String[] args)
112111
String fileName = args[0].substring(0, args[0].lastIndexOf(".")) + ".asm";
113112

114113
//Write MIPS
115-
CodeGenerator gen = new CodeGenerator(IRList, labels, varList, fileName);
114+
CodeGenerator gen = new CodeGenerator(IRList, labels, fileName);
116115
gen.generateMIPS();
117116

118117
//Link runtime.asm file

‎backpatching/BackPatcher.java‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,10 @@ public void patch()
3232
{
3333
q.setArg1("_system_out_println");
3434
}
35+
else if(methodName.equals("System.exit"))
36+
{
37+
q.setArg1("_system_exit");
38+
}
3539
else
3640
{
3741
q.setArg1(workList.get(methodName));

‎codegen/CodeGenerator.java‎

Lines changed: 13 additions & 83 deletions
Original file line numberDiff line numberDiff line change
@@ -18,15 +18,13 @@ public class CodeGenerator
1818
private String output;
1919
private List<Quadruple> IRList;
2020
private Hashtable<Quadruple, List<Label>> labels;
21-
private List<Variable> varList;
2221
private RegisterAllocator allocator;
2322

24-
public CodeGenerator(List<Quadruple> list, Hashtable<Quadruple, List<Label>> label, List<Variable> vars, String fileName)
23+
public CodeGenerator(List<Quadruple> list, Hashtable<Quadruple, List<Label>> label, String fileName)
2524
{
2625
IRList = list;
2726
output = fileName;
2827
labels = label;
29-
varList = vars;
3028
allocator = new RegisterAllocator();
3129
}
3230

@@ -39,10 +37,6 @@ public void generateMIPS()
3937
FileWriter fw = new FileWriter(output);
4038
BufferedWriter bw = new BufferedWriter(fw);
4139

42-
//Allocated Variables
43-
bw.write(".data\n");
44-
generateDataSeg(varList, bw);
45-
4640
bw.write(".text\n", 0, 6);
4741
bw.write("main:\n", 0, 6);
4842

@@ -60,22 +54,6 @@ public void generateMIPS()
6054
{
6155
if(l.printBefore == true)
6256
{
63-
//Print system exit at the end of main
64-
if(l.toString().equals("L1:"))
65-
{
66-
String temp = "jal _system_exit\n";
67-
bw.write(temp, 0, temp.length());
68-
printedExit = true;
69-
}
70-
else //Print jr return jump before the label
71-
{
72-
if(!l.toString().equals("L0:"))
73-
{
74-
String temp = "jr $ra\n";
75-
bw.write(temp, 0, temp.length());
76-
}
77-
}
78-
7957
//Print label
8058
String temp = l.toString() + "\n";
8159
bw.write(temp, 0, temp.length());
@@ -111,22 +89,6 @@ else if(q instanceof UnaryAssignmentIR)
11189
{
11290
if(l.printBefore == false)
11391
{
114-
//Print system exit at the end of main
115-
if(l.toString().equals("L1:"))
116-
{
117-
String temp = "jal _system_exit\n";
118-
bw.write(temp, 0, temp.length());
119-
printedExit = true;
120-
}
121-
else //Print jr return jump before the label
122-
{
123-
if(!l.toString().equals("L0:"))
124-
{
125-
String temp = "jr $ra\n";
126-
bw.write(temp, 0, temp.length());
127-
}
128-
}
129-
13092
//Print label
13193
String temp = l.toString() + "\n";
13294
bw.write(temp, 0, temp.length());
@@ -135,18 +97,6 @@ else if(q instanceof UnaryAssignmentIR)
13597
}
13698
}
13799

138-
//Print the closing exit/return
139-
if(printedExit == true)
140-
{
141-
String temp = "jr $ra\n";
142-
bw.write(temp, 0, temp.length());
143-
}
144-
else
145-
{
146-
String temp = "jal _system_exit\n";
147-
bw.write(temp, 0, temp.length());
148-
}
149-
150100
//Close output file resources
151101
if(bw != null)
152102
bw.close();
@@ -157,36 +107,6 @@ else if(q instanceof UnaryAssignmentIR)
157107
}
158108
}
159109

160-
private void generateDataSeg(List<Variable> varList, BufferedWriter bw)
161-
{
162-
for(int i = 0; i < varList.size(); i++)
163-
{
164-
try
165-
{
166-
String name = varList.get(i).getName();
167-
String type = varList.get(i).getType();
168-
String value = "";
169-
170-
if(type.equals("int") || type.equals("boolean"))
171-
{
172-
type = ".word";
173-
value = "0"; //Default value of 0d or false
174-
}
175-
else //Don't handle objects or int[] yet
176-
{
177-
return;
178-
}
179-
180-
String data = name + ": " + type + " " + value + "\n";
181-
bw.write(data, 0, data.length());
182-
}
183-
catch (IOException e)
184-
{
185-
e.printStackTrace();
186-
}
187-
}
188-
}
189-
190110
private void handleCopy(Quadruple instruction, BufferedWriter bw)
191111
{
192112
try
@@ -331,6 +251,9 @@ else if(arg1.getType().equals("temporary"))
331251
}
332252

333253
bw.write(temp, 0, temp.length());
254+
255+
temp = "jr $ra\n";
256+
bw.write(temp, 0, temp.length());
334257
}
335258
catch(IOException e)
336259
{
@@ -544,6 +467,15 @@ private void functionCall(List<Quadruple>IRList, int index, BufferedWriter bw)
544467
{
545468
CallIR instruction = (CallIR)IRList.get(index);
546469
int paramCount = Integer.parseInt((String)instruction.getArg2());
470+
String function = (String)instruction.getArg1();
471+
472+
//Handle System.exit (no need to worry about params nor registers)
473+
if(function.equals("_system_exit"))
474+
{
475+
String temp = "jal " + function + "\n";
476+
bw.write(temp, 0, temp.length());
477+
return;
478+
}
547479

548480
//Store $ra on stack
549481
String temp = "addi $sp, $sp, -68\n"; //Make enough space on stack to save all reg
@@ -606,8 +538,6 @@ else if(argType.equals("temporary"))
606538
}
607539

608540
//Jump to the function
609-
String function = (String)instruction.getArg1();
610-
611541
temp = "jal " + function + "\n";
612542
bw.write(temp, 0, temp.length());
613543

‎visitor/IRVisitor.java‎

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -20,14 +20,12 @@ public class IRVisitor implements Visitor
2020
private int blockNumber;
2121
private List<Quadruple> IRList;
2222
private Hashtable<Quadruple, List<Label>> labels;
23-
private List<Variable> varList;
2423
private HashMap<String, String> workList; //A mapping of method calls to labels... i.e. 'foo()' --> 'L1:"
2524

2625
public IRVisitor(Scope symbolTable)
2726
{
2827
labels = new Hashtable<Quadruple, List<Label>>();
2928
IRList = new ArrayList<Quadruple>();
30-
varList = new ArrayList<Variable>();
3129
workList = new HashMap<String, String>();
3230
currentScope = symbolTable;
3331
blockNumber = 0;
@@ -47,11 +45,6 @@ public List<Quadruple> getIR()
4745
{
4846
return IRList;
4947
}
50-
51-
public List<Variable> getVars()
52-
{
53-
return varList;
54-
}
5548

5649
//Helper function to add a new Label to a certain IR
5750
public String addLabel(Quadruple q, boolean printBefore)
@@ -118,6 +111,7 @@ public void visit(MainClass n)
118111

119112
String labelName = addLabel(IRList.get(0), true);
120113

114+
IRList.add(new CallIR("System.exit", "0", null));
121115
workList.put("main", labelName);
122116

123117
currentScope = currentScope.exitScope(); //Exit "main" method
@@ -306,7 +300,6 @@ public void visit(Assign n)
306300
{
307301
n.i.accept(this);
308302
n.e.accept(this);
309-
varList.add(currentScope.lookupVariable(n.i.toString()));
310303
IRList.add(new CopyIR(n.e.generateTAC(), currentScope.lookupVariable(n.i.toString())));
311304
}
312305

@@ -317,7 +310,6 @@ public void visit(ArrayAssign n)
317310
n.i.accept(this);
318311
n.e1.accept(this);
319312
n.e2.accept(this);
320-
varList.add(currentScope.lookupVariable(n.i.toString()));
321313
IRList.add(new IndexedAssignmentIR2(n.e2.generateTAC(), n.e1.generateTAC(), currentScope.lookupVariable(n.i.toString())));
322314
}
323315

0 commit comments

Comments
 (0)