-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathJmmOptimizer.java
97 lines (73 loc) · 4.23 KB
/
JmmOptimizer.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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
package pt.up.fe.comp.ollir;
import pt.up.fe.comp.jmm.analysis.JmmSemanticsResult;
import pt.up.fe.comp.jmm.ollir.JmmOptimization;
import pt.up.fe.comp.jmm.ollir.OllirResult;
import pt.up.fe.comp.ollir.optimization.optimizers.ConstFoldingVisitor;
import pt.up.fe.comp.ollir.optimization.optimizers.ConstPropagationParam;
import pt.up.fe.comp.ollir.optimization.optimizers.ConstPropagationVisitor;
import pt.up.fe.comp.ollir.optimization.LocalVariableOptimization;
import pt.up.fe.comp.ollir.optimization.optimizers.DeadCodeEliminationVisitor;
import pt.up.fe.comp.ollir.optimization.optimizers.*;
import java.util.Collections;
public class JmmOptimizer implements JmmOptimization {
// before OLLIR
public JmmSemanticsResult optimize(JmmSemanticsResult semanticsResult) {
boolean optimize = semanticsResult.getConfig().get("optimize") != null
&& semanticsResult.getConfig().get("optimize").equals("true");
if (!optimize) return semanticsResult;
boolean debug = semanticsResult.getConfig().get("debug") != null && semanticsResult.getConfig().get("debug").equals("true");
System.out.println("Performing optimizations before OLLIR ...");
// duplicate condition of while loops in order to check if they can be promoted to do-while loops at compile-time
WhileConditionDuplicatorVisitor whileConditionDuplicatorVisitor = new WhileConditionDuplicatorVisitor();
whileConditionDuplicatorVisitor.visit(semanticsResult.getRootNode());
boolean hasChanges = true;
while (hasChanges) {
ConstPropagationVisitor constPropagationVisitor = new ConstPropagationVisitor();
if (debug) {
System.out.println("Performing constant propagation ...");
}
hasChanges = constPropagationVisitor.visit(semanticsResult.getRootNode(), new ConstPropagationParam());
ConstFoldingVisitor constFoldingVisitor = new ConstFoldingVisitor();
if (debug) {
System.out.println("Performing constant folding ...");
}
hasChanges = constFoldingVisitor.visit(semanticsResult.getRootNode()) || hasChanges;
DeadCodeEliminationVisitor deadCodeEliminationVisitor = new DeadCodeEliminationVisitor();
if (debug) {
System.out.println("Performing dead code elimination ...");
}
hasChanges = deadCodeEliminationVisitor.visit(semanticsResult.getRootNode()) || hasChanges;
}
// remove duplicated while condition and annotate with do-while true/false at compile-time
DoWhileAnnotatorVisitor doWhileAnnotatorVisitor = new DoWhileAnnotatorVisitor();
doWhileAnnotatorVisitor.visit(semanticsResult.getRootNode());
if (debug) {
System.out.println("OPTIMIZED ANNOTATED AST : \n" + semanticsResult.getRootNode().toTree());
}
return semanticsResult;
}
@Override
public OllirResult toOllir(JmmSemanticsResult semanticsResult) {
boolean optimize = semanticsResult.getConfig().get("optimize") != null
&& semanticsResult.getConfig().get("optimize").equals("true");
System.out.println("Generating OLLIR code ...");
var ollirGenerator = new OllirGenerator(semanticsResult.getSymbolTable(), optimize);
ollirGenerator.visit(semanticsResult.getRootNode());
var ollirCode = ollirGenerator.getCode();
if (semanticsResult.getConfig().get("debug") != null && semanticsResult.getConfig().get("debug").equals("true")) {
System.out.println("OLLIR CODE : \n" + ollirCode);
}
return new OllirResult(semanticsResult, ollirCode, Collections.emptyList());
}
@Override
public OllirResult optimize(OllirResult ollirResult) {
String localVariableAllocation = ollirResult.getConfig().get("registerAllocation");
int localVariableNum = localVariableAllocation == null? -1 : Integer.parseInt(localVariableAllocation);
if (localVariableNum > -1) {
System.out.println("Performing register allocation ...");
LocalVariableOptimization optimization = new LocalVariableOptimization(ollirResult);
optimization.optimize(localVariableNum);
}
return ollirResult;
}
}