Skip to content

Commit 623aff5

Browse files
committed
Added support for Array Creation and .length in MIPS
1 parent 691f8ec commit 623aff5

1 file changed

Lines changed: 203 additions & 4 deletions

File tree

‎codegen/CodeGenerator.java‎

Lines changed: 203 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,14 @@ else if(q instanceof ConditionalJumpIR)
105105
{
106106
handleConditionalJump(q, bw);
107107
}
108+
else if(q instanceof NewArrayIR)
109+
{
110+
handleNewArray(q, bw);
111+
}
112+
else if(q instanceof LengthIR)
113+
{
114+
handleArrayLength(q, bw);
115+
}
108116

109117
//Print any labels after
110118
if(labelList != null)
@@ -131,6 +139,183 @@ else if(q instanceof ConditionalJumpIR)
131139
}
132140
}
133141

142+
private void handleArrayLength(Quadruple instruction, BufferedWriter bw)
143+
{
144+
try
145+
{
146+
Variable arg1 = (Variable)instruction.getArg1(); //Holds the address
147+
Variable result = (Variable)instruction.getResult();
148+
String temp;
149+
150+
if(result.getType().equals("temporary"))
151+
{
152+
if(arg1.getType().equals("temporary"))
153+
{
154+
temp = "lw " + allocator.allocateReg(result.getName()) + ", 0(" + allocator.allocateReg(arg1.getName()) + ")\n";
155+
}
156+
else //Variable
157+
{
158+
if(arg1.getOffset() == -1)
159+
{
160+
temp = "lw " + allocator.allocateReg(result.getName()) + ", 0(" + arg1.getRegister() + ")\n";
161+
}
162+
else //Class variable
163+
{
164+
//Todo
165+
temp = "";
166+
}
167+
}
168+
}
169+
else //Variable arg1
170+
{
171+
if(result.getOffset() == -1)
172+
{
173+
if(arg1.getType().equals("temporary"))
174+
{
175+
temp = "lw " + result.getRegister() + ", 0(" + allocator.allocateReg(arg1.getName()) + ")\n";
176+
}
177+
else //Variable
178+
{
179+
if(arg1.getOffset() == -1)
180+
{
181+
temp = "lw " + result.getRegister() + ", 0(" + arg1.getRegister() + ")\n";
182+
}
183+
else //Class variable
184+
{
185+
//Todo
186+
temp = "";
187+
}
188+
}
189+
}
190+
else //Class variable
191+
{
192+
//Todo
193+
temp = "";
194+
}
195+
}
196+
197+
bw.write(temp, 0, temp.length());
198+
}
199+
catch(IOException e)
200+
{
201+
e.printStackTrace();
202+
}
203+
}
204+
205+
private void handleNewArray(Quadruple instruction, BufferedWriter bw)
206+
{
207+
try
208+
{
209+
//Notes:
210+
// $a0 - Holds the number of bytes
211+
// $v0 - On return, holds the memory address (with address 0 holding the 4 byte length)
212+
213+
String type = (String)instruction.getArg1();
214+
Variable arg2 = (Variable)instruction.getArg2();
215+
Variable result = (Variable)instruction.getResult();
216+
String temp;
217+
218+
//Handle arg2 -- The number of elements/size
219+
if(arg2.getType().equals("constant"))
220+
{
221+
temp = "li $a0, " + arg2.getName() + "\n";
222+
}
223+
else if(arg2.getType().equals("temporary"))
224+
{
225+
temp = "move $a0, " + allocator.allocateReg(arg2.getName()) + "\n";
226+
}
227+
else //Variable arg1
228+
{
229+
if(arg2.getOffset() == -1)
230+
{
231+
temp = "move $a0, " + arg2.getRegister() + "\n";
232+
}
233+
else //Class variable
234+
{
235+
//Todo
236+
temp = "";
237+
}
238+
}
239+
240+
bw.write(temp, 0, temp.length());
241+
242+
if(type.equals("int")) //Only "int" for MiniJava
243+
{
244+
//Shift left by 2 (multiply by 4 bytes) for each "int" in size
245+
temp = "sll $a0, $a0, 2\n";
246+
bw.write(temp, 0, temp.length());
247+
}
248+
249+
//Store $ra on stack
250+
temp = "addi $sp, $sp, -20\n"; //Make enough space on stack to save all reg
251+
bw.write(temp, 0, temp.length());
252+
temp = "sw $ra, 16($sp)\n";
253+
bw.write(temp, 0, temp.length());
254+
255+
//Store $a0
256+
temp = "sw $a0, 12($sp)\n";
257+
bw.write(temp, 0, temp.length());
258+
259+
//Store $t0-$t1 on the stack
260+
for(int i = 0; i < 2; i++)
261+
{
262+
temp = "sw $t" + i + ", " + (8 - (4*i)) + "($sp)\n";
263+
bw.write(temp, 0, temp.length());
264+
}
265+
266+
//Store $v0 on the stack
267+
temp = "sw $v0, 0($sp)\n";
268+
bw.write(temp, 0, temp.length());
269+
270+
//Call the function of "_new_array"
271+
temp = "jal _new_array\n";
272+
bw.write(temp, 0, temp.length());
273+
274+
//Restore $t0-$t1 from the stack
275+
for(int i = 1; i >= 0; i--)
276+
{
277+
temp = "lw $t" + i + ", " + (8 - (4*i)) + "($sp)\n";
278+
bw.write(temp, 0, temp.length());
279+
}
280+
281+
//Restore $a0 from the stack
282+
temp = "lw $a0, 12($sp)\n";
283+
bw.write(temp, 0, temp.length());
284+
285+
if(result.getType().equals("temporary"))
286+
{
287+
temp = "move " + allocator.allocateReg(result.getName()) + ", $v0\n";
288+
}
289+
else //Variable
290+
{
291+
if(result.getOffset() == -1)
292+
{
293+
temp = "move " + result.getRegister() + ", $v0\n";
294+
}
295+
else //Class variable
296+
{
297+
//Todo
298+
}
299+
}
300+
301+
bw.write(temp, 0, temp.length());
302+
303+
//Restore $v0
304+
temp = "lw $v0, 0($sp)\n";
305+
bw.write(temp, 0, temp.length());
306+
307+
//Restore $ra from the stack
308+
temp = "lw $ra, 16($sp)\n";
309+
bw.write(temp, 0, temp.length());
310+
temp = "addi $sp, $sp, 20\n"; //Cleanup space on stack from all saved reg
311+
bw.write(temp, 0, temp.length());
312+
}
313+
catch(IOException e)
314+
{
315+
e.printStackTrace();
316+
}
317+
}
318+
134319
private void handleConditionalJump(Quadruple instruction, BufferedWriter bw)
135320
{
136321
try
@@ -614,11 +799,18 @@ private void functionCall(List<Quadruple>IRList, int index, BufferedWriter bw)
614799
}
615800

616801
//Store $ra on stack
617-
String temp = "addi $sp, $sp, -68\n"; //Make enough space on stack to save all reg
802+
String temp = "addi $sp, $sp, -100\n"; //Make enough space on stack to save all reg
618803
bw.write(temp, 0, temp.length());
619-
temp = "sw $ra, 64($sp)\n";
804+
temp = "sw $ra, 96($sp)\n";
620805
bw.write(temp, 0, temp.length());
621806

807+
//Store $s0-$s7 on stack
808+
for(int i = 0; i < 8; i++)
809+
{
810+
temp = "sw $s" + i + ", " + (92 - (4*i)) + "($sp)\n";
811+
bw.write(temp, 0, temp.length());
812+
}
813+
622814
//Store $a0 - $a3 on stack
623815
for(int i = 0; i < 4; i++)
624816
{
@@ -721,6 +913,13 @@ else if(arg1.getRegister().equals("$a3"))
721913
bw.write(temp, 0, temp.length());
722914
}
723915

916+
//Restore $s0-$s7 on the stack
917+
for(int i = 7; i >= 0; i--)
918+
{
919+
temp = "lw $s" + i + ", " + (92 - (4*i)) + "($sp)\n";
920+
bw.write(temp, 0, temp.length());
921+
}
922+
724923
//Move return value into the result register
725924
if(!function.equals("_system_out_println")) //println is the only 'void' function in minijava
726925
{
@@ -753,9 +952,9 @@ else if(arg1.getRegister().equals("$a3"))
753952
}
754953

755954
//Restore $ra from the stack
756-
temp = "lw $ra, 64($sp)\n";
955+
temp = "lw $ra, 96($sp)\n";
757956
bw.write(temp, 0, temp.length());
758-
temp = "addi $sp, $sp, 68\n"; //Cleanup space on stack from all saved reg
957+
temp = "addi $sp, $sp, 100\n"; //Cleanup space on stack from all saved reg
759958
bw.write(temp, 0, temp.length());
760959
}
761960
catch (IOException e)

0 commit comments

Comments
 (0)