-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathIRFunction.java
More file actions
70 lines (64 loc) · 2.4 KB
/
IRFunction.java
File metadata and controls
70 lines (64 loc) · 2.4 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
import java.util.List;
public class IRFunction extends Function implements IRInstruction {
Function func;
TempAllocator allocator;
List<IRInstruction> instList;
public IRFunction(Function func, List<IRInstruction> instList, TempAllocator allocator) {
super(func.decl, func.body);
this.func = func;
this.instList = instList;
this.allocator = allocator;
}
public String toString() {
StringBuilder sb = new StringBuilder();
sb.append("FUNC " + func.decl.id.name);
sb.append(" (");
if (func.decl.params != null) {
for (FormalParameter p: func.decl.params.params) {
sb.append(p.type.toString());
}
}
sb.append(")");
sb.append(func.decl.type.toString());
sb.append("\n{");
for (TempVar t: allocator.temps) {
sb.append("\n TEMP " + t.number + ":" + t.type.toString() + ";");
}
IRInstruction lastIR = null;
for (IRInstruction ir: instList) {
if (ir instanceof IRLabel) {
sb.append("\n" + ir.toString());
} else {
sb.append("\n " + ir.toString());
}
lastIR = ir;
}
if (func.decl.type instanceof VoidType && (lastIR == null || !(lastIR instanceof IRReturnInstruction))) {
sb.append("\n " + new IRReturnInstruction(null));
}
sb.append("\n}");
return sb.toString();
}
@Override
public String toBytecodeString() {
StringBuilder sb = new StringBuilder();
sb.append(".method public static ");
sb.append(func.functionSignature());
sb.append("\n .limit stack 16");
sb.append(String.format("\n .limit locals %d", allocator.temps.size()));
IRInstruction lastIR = null;
for (IRInstruction ir: instList) {
if (ir instanceof IRLabel) {
sb.append("\n" + ir.toBytecodeString());
} else {
sb.append("\n " + ir.toBytecodeString());
}
lastIR = ir;
}
if (func.decl.type instanceof VoidType && (lastIR == null || !(lastIR instanceof IRReturnInstruction))) {
sb.append("\n " + (new IRReturnInstruction(null)).toBytecodeString());
}
sb.append("\n.end method");
return sb.toString();
}
}