blob: ac9b7ff7acdeea2d3c1ae3c1809c867a63218163 (
plain)
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
|
/*
* Code formatter project
* CS 4481
*/
package submit.ast;
import submit.MIPSResult;
import submit.RegisterAllocator;
import submit.SymbolTable;
/**
*
* @author edwajohn
*/
public class Mutable implements Expression {
private final String id;
private final Expression index;
public Mutable(String id, Expression index) {
this.id = id;
this.index = index;
}
@Override
public void toCminus(StringBuilder builder, String prefix) {
builder.append(id);
if (index != null) {
builder.append("[");
index.toCminus(builder, prefix);
builder.append("]");
}
}
public MIPSResult toMIPS(StringBuilder code, StringBuilder data,
SymbolTable symbolTable,
RegisterAllocator regAllocator) {
String stackReg = regAllocator.getAny();
code.append(String.format("li %s %d\n", stackReg, symbolTable.offsetOf(id)))
.append(String.format("add %s %s $sp\n", stackReg, stackReg));
return MIPSResult.createAddressResult(stackReg, VarType.INT);
}
}
|