Returns back old variable substitution

This commit is contained in:
konsoletyper 2014-01-31 22:25:24 +04:00
parent ce0859beac
commit 4c03e7d3cb

View File

@ -28,10 +28,6 @@ class OptimizingVisitor implements StatementVisitor, ExprVisitor {
private ReadWriteStatsBuilder stats; private ReadWriteStatsBuilder stats;
Map<IdentifiedStatement, Integer> referencedStatements = new HashMap<>(); Map<IdentifiedStatement, Integer> referencedStatements = new HashMap<>();
private List<Statement> resultSequence; private List<Statement> resultSequence;
private int[] variableDecl;
private List<Boolean> invocations;
private int lastMethodCall;
private boolean hasMethodCall;
public OptimizingVisitor(ReadWriteStatsBuilder stats) { public OptimizingVisitor(ReadWriteStatsBuilder stats) {
this.stats = stats; this.stats = stats;
@ -122,27 +118,22 @@ class OptimizingVisitor implements StatementVisitor, ExprVisitor {
if (stats.reads[index] != 1 || stats.writes[index] != 1) { if (stats.reads[index] != 1 || stats.writes[index] != 1) {
return; return;
} }
int declIndex = variableDecl[index]; if (resultSequence.isEmpty()) {
if (declIndex < 0 || declIndex >= lastMethodCall) {
return; return;
} }
if (invocations.get(declIndex)) { Statement last = resultSequence.get(resultSequence.size() - 1);
for (int i = lastMethodCall - 1; i > declIndex; --i) { if (!(last instanceof AssignmentStatement)) {
if (invocations.get(i)) {
return; return;
} }
AssignmentStatement assignment = (AssignmentStatement)last;
if (!(assignment.getLeftValue() instanceof VariableExpr)) {
return;
} }
VariableExpr var = (VariableExpr)assignment.getLeftValue();
if (var.getIndex() == index) {
resultSequence.remove(resultSequence.size() - 1);
assignment.getRightValue().acceptVisitor(this);
} }
for (int i = declIndex; i < lastMethodCall; ++i) {
if (invocations.get(i)) {
lastMethodCall = i;
break;
}
}
AssignmentStatement decl = (AssignmentStatement)resultSequence.get(declIndex);
resultSequence.set(declIndex, null);
variableDecl[index] = -1;
resultExpr = decl.getRightValue();
} }
@Override @Override
@ -166,7 +157,6 @@ class OptimizingVisitor implements StatementVisitor, ExprVisitor {
@Override @Override
public void visit(InvocationExpr expr) { public void visit(InvocationExpr expr) {
hasMethodCall = true;
Expr[] args = new Expr[expr.getArguments().size()]; Expr[] args = new Expr[expr.getArguments().size()];
for (int i = expr.getArguments().size() - 1; i >= 0; --i) { for (int i = expr.getArguments().size() - 1; i >= 0; --i) {
expr.getArguments().get(i).acceptVisitor(this); expr.getArguments().get(i).acceptVisitor(this);
@ -276,9 +266,6 @@ class OptimizingVisitor implements StatementVisitor, ExprVisitor {
if (!(statement.getLeftValue() instanceof VariableExpr)) { if (!(statement.getLeftValue() instanceof VariableExpr)) {
statement.getLeftValue().acceptVisitor(this); statement.getLeftValue().acceptVisitor(this);
left = resultExpr; left = resultExpr;
} else {
VariableExpr var = (VariableExpr)statement.getLeftValue();
variableDecl[var.getIndex()] = resultSequence.size();
} }
statement.setLeftValue(left); statement.setLeftValue(left);
statement.setRightValue(right); statement.setRightValue(right);
@ -287,14 +274,8 @@ class OptimizingVisitor implements StatementVisitor, ExprVisitor {
} }
private List<Statement> processSequence(List<Statement> statements) { private List<Statement> processSequence(List<Statement> statements) {
boolean hasMethodCallBackup = hasMethodCall;
List<Statement> backup = resultSequence; List<Statement> backup = resultSequence;
int[] variableDeclBackup = variableDecl;
List<Boolean> invocationsBackup = invocations;
invocations = new ArrayList<>();
resultSequence = new ArrayList<>(); resultSequence = new ArrayList<>();
variableDecl = new int[stats.reads.length];
Arrays.fill(variableDecl, -1);
processSequenceImpl(statements); processSequenceImpl(statements);
List<Statement> result = new ArrayList<>(); List<Statement> result = new ArrayList<>();
for (Statement part : resultSequence) { for (Statement part : resultSequence) {
@ -303,9 +284,6 @@ class OptimizingVisitor implements StatementVisitor, ExprVisitor {
} }
} }
resultSequence = backup; resultSequence = backup;
variableDecl = variableDeclBackup;
invocations = invocationsBackup;
hasMethodCall = hasMethodCallBackup;
return result; return result;
} }
@ -318,10 +296,7 @@ class OptimizingVisitor implements StatementVisitor, ExprVisitor {
} }
continue; continue;
} }
hasMethodCall = false;
lastMethodCall = resultSequence.size();
part.acceptVisitor(this); part.acceptVisitor(this);
invocations.add(hasMethodCall);
part = resultStmt; part = resultStmt;
if (part instanceof SequentialStatement) { if (part instanceof SequentialStatement) {
if (!processSequenceImpl(((SequentialStatement)part).getSequence())) { if (!processSequenceImpl(((SequentialStatement)part).getSequence())) {
@ -464,7 +439,6 @@ class OptimizingVisitor implements StatementVisitor, ExprVisitor {
statement.getAlternative().clear(); statement.getAlternative().clear();
statement.getAlternative().addAll(alternative); statement.getAlternative().addAll(alternative);
resultStmt = statement; resultStmt = statement;
hasMethodCall = true;
} }
@Override @Override
@ -480,7 +454,6 @@ class OptimizingVisitor implements StatementVisitor, ExprVisitor {
statement.getDefaultClause().clear(); statement.getDefaultClause().clear();
statement.getDefaultClause().addAll(newDefault); statement.getDefaultClause().addAll(newDefault);
resultStmt = statement; resultStmt = statement;
hasMethodCall = true;
} }
@Override @Override
@ -519,7 +492,6 @@ class OptimizingVisitor implements StatementVisitor, ExprVisitor {
break; break;
} }
resultStmt = statement; resultStmt = statement;
hasMethodCall = true;
} }
@Override @Override
@ -544,13 +516,11 @@ class OptimizingVisitor implements StatementVisitor, ExprVisitor {
@Override @Override
public void visit(BreakStatement statement) { public void visit(BreakStatement statement) {
resultStmt = statement; resultStmt = statement;
hasMethodCall = true;
} }
@Override @Override
public void visit(ContinueStatement statement) { public void visit(ContinueStatement statement) {
resultStmt = statement; resultStmt = statement;
hasMethodCall = true;
} }
@Override @Override
@ -560,7 +530,6 @@ class OptimizingVisitor implements StatementVisitor, ExprVisitor {
statement.setResult(resultExpr); statement.setResult(resultExpr);
} }
resultStmt = statement; resultStmt = statement;
hasMethodCall = true;
} }
@Override @Override
@ -568,7 +537,6 @@ class OptimizingVisitor implements StatementVisitor, ExprVisitor {
statement.getException().acceptVisitor(this); statement.getException().acceptVisitor(this);
statement.setException(resultExpr); statement.setException(resultExpr);
resultStmt = statement; resultStmt = statement;
hasMethodCall = true;
} }
@Override @Override