Skip to content

Commit 9fc7058

Browse files
committed
Implemented data segment draft and updated caller saved regs
1 parent 523bfaa commit 9fc7058

1 file changed

Lines changed: 91 additions & 6 deletions

File tree

‎codegen/CodeGenerator.java‎

Lines changed: 91 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,18 +7,24 @@
77

88
import java.io.*;
99
import IR.*;
10+
import helper.Label;
1011
import symboltable.Variable;
1112
import java.util.List;
13+
import java.util.Hashtable;
1214

1315
public class CodeGenerator
1416
{
1517
private String output;
1618
private List<Quadruple> IRList;
19+
private Hashtable<Quadruple, List<Label>> labels;
20+
private List<Variable> varList;
1721

18-
public CodeGenerator(List<Quadruple> list, String fileName)
22+
public CodeGenerator(List<Quadruple> list, Hashtable<Quadruple, List<Label>> label, List<Variable> vars, String fileName)
1923
{
2024
IRList = list;
2125
output = fileName;
26+
labels = label;
27+
varList = vars;
2228
}
2329

2430
public void generateMIPS()
@@ -28,6 +34,13 @@ public void generateMIPS()
2834
FileWriter fw = new FileWriter(output);
2935
BufferedWriter bw = new BufferedWriter(fw);
3036

37+
//Allocated Variables
38+
bw.write(".data\n");
39+
generateDataSeg(varList, bw);
40+
41+
bw.write(".text\n", 0, 6);
42+
bw.write("main:\n", 0, 6);
43+
3144
//Iterate through the IR instructions
3245
for(int i = 0; i < IRList.size(); i++)
3346
{
@@ -49,6 +62,36 @@ public void generateMIPS()
4962
}
5063
}
5164

65+
private void generateDataSeg(List<Variable> varList, BufferedWriter bw)
66+
{
67+
for(int i = 0; i < varList.size(); i++)
68+
{
69+
try
70+
{
71+
String name = varList.get(i).getName();
72+
String type = varList.get(i).getType();
73+
String value = "";
74+
75+
if(type.equals("int") || type.equals("boolean"))
76+
{
77+
type = ".word";
78+
value = "0"; //Default value of 0d or false
79+
}
80+
else //Don't handle objects or int[] yet
81+
{
82+
return;
83+
}
84+
85+
String data = name + ": " + type + " " + value + "\n";
86+
bw.write(data, 0, data.length());
87+
}
88+
catch (IOException e)
89+
{
90+
e.printStackTrace();
91+
}
92+
}
93+
}
94+
5295
private void functionCall(List<Quadruple>IRList, int index, BufferedWriter bw)
5396
{
5497
try
@@ -57,11 +100,32 @@ private void functionCall(List<Quadruple>IRList, int index, BufferedWriter bw)
57100
int paramCount = Integer.parseInt((String)instruction.getArg2());
58101

59102
//Store $ra on stack
60-
String temp = "addi $sp, $sp, -4\n";
103+
String temp = "addi $sp, $sp, -68\n"; //Make enough space on stack to save all reg
61104
bw.write(temp, 0, temp.length());
62-
temp = "sw $ra, ($sp)\n";
105+
temp = "sw $ra, 64($sp)\n";
63106
bw.write(temp, 0, temp.length());
64107

108+
//Store $a0 - $a3 on stack
109+
for(int i = 0; i < 4; i++)
110+
{
111+
temp = "sw $a" + i + ", " + (60 - (4*i)) + "($sp)\n";
112+
bw.write(temp, 0, temp.length());
113+
}
114+
115+
//Store $t0-$t9 on the stack
116+
for(int i = 0; i < 10; i++)
117+
{
118+
temp = "sw $t" + i + ", " + (44 - (4*i)) + "($sp)\n";
119+
bw.write(temp, 0, temp.length());
120+
}
121+
122+
//Store $v0-$v1 on the stack
123+
for(int i = 0; i < 2; i++)
124+
{
125+
temp = "sw $v" + i + ", " + (4 - (4*i)) + "($sp)\n";
126+
bw.write(temp, 0, temp.length());
127+
}
128+
65129
//Store the (up to 4) params in register
66130
if(paramCount > 4)
67131
{
@@ -92,10 +156,31 @@ private void functionCall(List<Quadruple>IRList, int index, BufferedWriter bw)
92156
temp = "jal " + function + "\n";
93157
bw.write(temp, 0, temp.length());
94158

95-
//Restore $ra from stack
96-
temp = "lw $ra, ($sp)\n";
159+
//Restore $v0-$v1 from the stack
160+
for(int i = 1; i >= 0; i--)
161+
{
162+
temp = "lw $v" + i + ", " + (4 - (4*i)) + "($sp)\n";
163+
bw.write(temp, 0, temp.length());
164+
}
165+
166+
//Restore $t0-$t9 from the stack
167+
for(int i = 9; i >= 0; i--)
168+
{
169+
temp = "lw $t" + i + ", " + (44 - (4*i)) + "($sp)\n";
170+
bw.write(temp, 0, temp.length());
171+
}
172+
173+
//Restore $a0-$a3 on the stack
174+
for(int i = 3; i >= 0; i--)
175+
{
176+
temp = "lw $a" + i + ", " + (60 - (4*i)) + "($sp)\n";
177+
bw.write(temp, 0, temp.length());
178+
}
179+
180+
//Restore $ra from the stack
181+
temp = "lw $ra, 64($sp)\n";
97182
bw.write(temp, 0, temp.length());
98-
temp = "addi $sp, $sp, 4\n";
183+
temp = "addi $sp, $sp, 68\n"; //Cleanup space on stack from all saved reg
99184
bw.write(temp, 0, temp.length());
100185
}
101186
catch (IOException e)

0 commit comments

Comments
 (0)