-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLocalVariableOptimization.java
68 lines (55 loc) · 2.61 KB
/
LocalVariableOptimization.java
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
package pt.up.fe.comp.ollir.optimization;
import org.specs.comp.ollir.ClassUnit;
import org.specs.comp.ollir.Method;
import org.specs.comp.ollir.OllirErrorException;
import pt.up.fe.comp.jmm.jasmin.JasminBackender;
import pt.up.fe.comp.jmm.ollir.OllirResult;
import pt.up.fe.comp.jmm.report.Report;
import pt.up.fe.comp.jmm.report.ReportType;
import pt.up.fe.comp.jmm.report.Stage;
public class LocalVariableOptimization {
private final OllirResult ollirResult;
private final ClassUnit unit;
private final boolean debug;
public LocalVariableOptimization(OllirResult ollirResult) {
this.ollirResult = ollirResult;
this.unit = ollirResult.getOllirClass();
this.debug = ollirResult.getConfig().get("debug") != null && ollirResult.getConfig().get("debug").equals("true");
}
public void optimize(int localVariableNum) {
try {
unit.checkMethodLabels();
} catch (OllirErrorException e) {
e.printStackTrace();
return;
}
unit.buildCFGs();
unit.buildVarTables();
int maxLocalsNeeded = -1;
for (Method method : unit.getMethods()) {
if (this.debug) {
System.out.println(method.isConstructMethod() ? "{Constructor}" : method.getMethodName() + ":");
}
LivenessAnalyser analyser = new LivenessAnalyser(method);
analyser.analyse(this.debug);
LocalVariableInterferenceGraph varGraph = new LocalVariableInterferenceGraph(analyser.getInAlive(), analyser.getOutAlive(), analyser.getDefined(), method);
AllocateVariablesRes allocateVariablesRes = varGraph.allocateLocalVariables(localVariableNum);
var updatedVarTable = allocateVariablesRes.getUpdateVarTable();
int localVariableNumUsed = allocateVariablesRes.getLocalVariableNum();
maxLocalsNeeded = Math.max(maxLocalsNeeded, localVariableNumUsed);
for (String varName : updatedVarTable.keySet()) {
method.getVarTable().put(varName, updatedVarTable.get(varName));
}
if (this.debug) {
System.out.println("Used " + JasminBackender.calculateLimitLocals(method) + " register(s).\n");
}
}
// add report only if -r=n > 0 and n is insufficient
if (localVariableNum > 0 && maxLocalsNeeded > localVariableNum) {
ollirResult.getReports().add(new Report(
ReportType.ERROR, Stage.OPTIMIZATION,
-1, -1,
"Insufficient -r=" + localVariableNum + ". Needs -r=" + maxLocalsNeeded + "."));
}
}
}