mirror of
https://github.com/Eaglercraft-TeaVM-Fork/eagler-teavm.git
synced 2024-12-22 16:14:10 -08:00
Fixes issues in if statement AST optimizer
This commit is contained in:
parent
16d11909ea
commit
692bfdd731
|
@ -301,94 +301,101 @@ class OptimizingVisitor implements StatementVisitor, ExprVisitor {
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
private void eliminateRedundantBreaks(List<Statement> statements, Set<IdentifiedStatement> currentExits,
|
private void eliminateRedundantBreaks(List<Statement> statements, IdentifiedStatement exit) {
|
||||||
Set<IdentifiedStatement> exits) {
|
|
||||||
if (statements.isEmpty()) {
|
if (statements.isEmpty()) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
Statement last = statements.get(statements.size() - 1);
|
Statement last = statements.get(statements.size() - 1);
|
||||||
if (last instanceof BreakStatement) {
|
if (last instanceof BreakStatement && exit != null) {
|
||||||
IdentifiedStatement target = ((BreakStatement)last).getTarget();
|
IdentifiedStatement target = ((BreakStatement)last).getTarget();
|
||||||
if (exits.contains(target)) {
|
if (exit == target) {
|
||||||
statements.remove(statements.size() - 1);
|
statements.remove(statements.size() - 1);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (statements.isEmpty()) {
|
if (statements.isEmpty()) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
Set<IdentifiedStatement> nestedExits = new HashSet<>();
|
|
||||||
nestedExits.addAll(currentExits);
|
|
||||||
for (int i = 0; i < statements.size(); ++i) {
|
for (int i = 0; i < statements.size(); ++i) {
|
||||||
if (i == statements.size() - 1) {
|
|
||||||
nestedExits.addAll(exits);
|
|
||||||
}
|
|
||||||
Statement stmt = statements.get(i);
|
Statement stmt = statements.get(i);
|
||||||
if (stmt instanceof ConditionalStatement) {
|
if (stmt instanceof ConditionalStatement) {
|
||||||
ConditionalStatement cond = (ConditionalStatement)stmt;
|
ConditionalStatement cond = (ConditionalStatement)stmt;
|
||||||
last = cond.getConsequent().get(cond.getConsequent().size() - 1);
|
check_conditional: {
|
||||||
if (last instanceof BreakStatement) {
|
last = cond.getConsequent().isEmpty() ? null :
|
||||||
BreakStatement breakStmt = (BreakStatement)last;
|
cond.getConsequent().get(cond.getConsequent().size() - 1);
|
||||||
if (currentExits.contains(breakStmt.getTarget()) || exits.contains(breakStmt.getTarget())) {
|
if (last instanceof BreakStatement) {
|
||||||
int refs = referencedStatements.get(breakStmt.getTarget());
|
BreakStatement breakStmt = (BreakStatement)last;
|
||||||
referencedStatements.put(breakStmt.getTarget(), refs - 1);
|
if (exit != null && exit == breakStmt.getTarget()) {
|
||||||
cond.getConsequent().remove(cond.getConsequent().size() - 1);
|
int refs = referencedStatements.get(breakStmt.getTarget());
|
||||||
List<Statement> remaining = statements.subList(i + 1, statements.size());
|
referencedStatements.put(breakStmt.getTarget(), refs - 1);
|
||||||
cond.getAlternative().addAll(remaining);
|
cond.getConsequent().remove(cond.getConsequent().size() - 1);
|
||||||
remaining.clear();
|
List<Statement> remaining = statements.subList(i + 1, statements.size());
|
||||||
visit(cond);
|
cond.getAlternative().addAll(remaining);
|
||||||
if (resultStmt == cond) {
|
remaining.clear();
|
||||||
eliminateRedundantBreaks(cond.getConsequent(),
|
break check_conditional;
|
||||||
Collections.<IdentifiedStatement>emptySet(), nestedExits);
|
}
|
||||||
eliminateRedundantBreaks(cond.getAlternative(),
|
}
|
||||||
Collections.<IdentifiedStatement>emptySet(), nestedExits);
|
last = cond.getAlternative().isEmpty() ? null :
|
||||||
} else {
|
cond.getAlternative().get(cond.getAlternative().size() - 1);
|
||||||
statements.set(i, resultStmt);
|
if (last instanceof BreakStatement) {
|
||||||
|
BreakStatement breakStmt = (BreakStatement)last;
|
||||||
|
if (exit != null && exit == breakStmt.getTarget()) {
|
||||||
|
int refs = referencedStatements.get(breakStmt.getTarget());
|
||||||
|
referencedStatements.put(breakStmt.getTarget(), refs - 1);
|
||||||
|
cond.getAlternative().remove(cond.getConsequent().size() - 1);
|
||||||
|
List<Statement> remaining = statements.subList(i + 1, statements.size());
|
||||||
|
cond.getConsequent().addAll(remaining);
|
||||||
|
remaining.clear();
|
||||||
|
break check_conditional;
|
||||||
}
|
}
|
||||||
break;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
last = cond.getAlternative().isEmpty() ? null :
|
if (i == statements.size() - 1) {
|
||||||
cond.getAlternative().get(cond.getAlternative().size() - 1);
|
eliminateRedundantBreaks(cond.getConsequent(), exit);
|
||||||
if (last instanceof BreakStatement) {
|
eliminateRedundantBreaks(cond.getAlternative(), exit);
|
||||||
BreakStatement breakStmt = (BreakStatement)last;
|
}
|
||||||
if (currentExits.contains(breakStmt.getTarget()) || exits.contains(breakStmt.getTarget())) {
|
normalizeConditional(cond);
|
||||||
int refs = referencedStatements.get(breakStmt.getTarget());
|
if (cond.getConsequent().size() == 1 && cond.getConsequent().get(0) instanceof ConditionalStatement) {
|
||||||
referencedStatements.put(breakStmt.getTarget(), refs - 1);
|
ConditionalStatement innerCond = (ConditionalStatement)cond.getConsequent().get(0);
|
||||||
cond.getAlternative().remove(cond.getConsequent().size() - 1);
|
if (innerCond.getAlternative().isEmpty()) {
|
||||||
List<Statement> remaining = statements.subList(i + 1, statements.size());
|
if (cond.getAlternative().isEmpty()) {
|
||||||
cond.getConsequent().addAll(remaining);
|
cond.getConsequent().clear();
|
||||||
remaining.clear();
|
cond.getConsequent().addAll(innerCond.getConsequent());
|
||||||
visit(cond);
|
cond.setCondition(Expr.binary(BinaryOperation.AND, cond.getCondition(),
|
||||||
if (resultStmt == cond) {
|
innerCond.getCondition()));
|
||||||
eliminateRedundantBreaks(cond.getConsequent(),
|
} else {
|
||||||
Collections.<IdentifiedStatement>emptySet(), nestedExits);
|
cond.setCondition(ExprOptimizer.invert(cond.getCondition()));
|
||||||
eliminateRedundantBreaks(cond.getAlternative(),
|
cond.getConsequent().clear();
|
||||||
Collections.<IdentifiedStatement>emptySet(), nestedExits);
|
cond.getConsequent().addAll(cond.getAlternative());
|
||||||
|
cond.getAlternative().clear();
|
||||||
|
cond.getAlternative().add(innerCond);
|
||||||
}
|
}
|
||||||
statements.set(i, resultStmt);
|
--i;
|
||||||
break;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} else if (stmt instanceof BlockStatement) {
|
} else if (stmt instanceof BlockStatement) {
|
||||||
BlockStatement nestedBlock = (BlockStatement)stmt;
|
BlockStatement nestedBlock = (BlockStatement)stmt;
|
||||||
eliminateRedundantBreaks(nestedBlock.getBody(),
|
eliminateRedundantBreaks(nestedBlock.getBody(), nestedBlock);
|
||||||
Collections.<IdentifiedStatement>singleton(nestedBlock), nestedExits);
|
|
||||||
} else if (stmt instanceof WhileStatement) {
|
} else if (stmt instanceof WhileStatement) {
|
||||||
WhileStatement whileStmt = (WhileStatement)stmt;
|
WhileStatement whileStmt = (WhileStatement)stmt;
|
||||||
eliminateRedundantBreaks(whileStmt.getBody(), Collections.<IdentifiedStatement>emptySet(),
|
eliminateRedundantBreaks(whileStmt.getBody(), null);
|
||||||
Collections.<IdentifiedStatement>emptySet());
|
|
||||||
} else if (stmt instanceof SwitchStatement) {
|
} else if (stmt instanceof SwitchStatement) {
|
||||||
SwitchStatement switchStmt = (SwitchStatement)stmt;
|
SwitchStatement switchStmt = (SwitchStatement)stmt;
|
||||||
for (SwitchClause clause : switchStmt.getClauses()) {
|
for (SwitchClause clause : switchStmt.getClauses()) {
|
||||||
eliminateRedundantBreaks(clause.getBody(), Collections.<IdentifiedStatement>emptySet(),
|
eliminateRedundantBreaks(clause.getBody(), null);
|
||||||
Collections.<IdentifiedStatement>emptySet());
|
|
||||||
}
|
}
|
||||||
eliminateRedundantBreaks(switchStmt.getDefaultClause(), null,
|
eliminateRedundantBreaks(switchStmt.getDefaultClause(), null);
|
||||||
Collections.<IdentifiedStatement>emptySet());
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private void normalizeConditional(ConditionalStatement stmt) {
|
||||||
|
if (stmt.getConsequent().isEmpty()) {
|
||||||
|
stmt.getConsequent().addAll(stmt.getAlternative());
|
||||||
|
stmt.getAlternative().clear();
|
||||||
|
stmt.setCondition(ExprOptimizer.invert(stmt.getCondition()));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void visit(SequentialStatement statement) {
|
public void visit(SequentialStatement statement) {
|
||||||
List<Statement> statements = processSequence(statement.getSequence(), false);
|
List<Statement> statements = processSequence(statement.getSequence(), false);
|
||||||
|
@ -453,8 +460,7 @@ class OptimizingVisitor implements StatementVisitor, ExprVisitor {
|
||||||
@Override
|
@Override
|
||||||
public void visit(BlockStatement statement) {
|
public void visit(BlockStatement statement) {
|
||||||
List<Statement> statements = processSequence(statement.getBody(), false);
|
List<Statement> statements = processSequence(statement.getBody(), false);
|
||||||
eliminateRedundantBreaks(statements, Collections.<IdentifiedStatement>singleton(statement),
|
eliminateRedundantBreaks(statements, statement);
|
||||||
Collections.<IdentifiedStatement>emptySet());
|
|
||||||
if (referencedStatements.get(statement).equals(0)) {
|
if (referencedStatements.get(statement).equals(0)) {
|
||||||
SequentialStatement result = new SequentialStatement();
|
SequentialStatement result = new SequentialStatement();
|
||||||
result.getSequence().addAll(statements);
|
result.getSequence().addAll(statements);
|
||||||
|
|
|
@ -385,17 +385,26 @@ public class Renderer implements ExprVisitor, StatementVisitor {
|
||||||
@Override
|
@Override
|
||||||
public void visit(ConditionalStatement statement) {
|
public void visit(ConditionalStatement statement) {
|
||||||
try {
|
try {
|
||||||
writer.append("if").ws().append("(");
|
while (true) {
|
||||||
statement.getCondition().acceptVisitor(this);
|
writer.append("if").ws().append("(");
|
||||||
writer.append(")").ws().append("{").softNewLine().indent();
|
statement.getCondition().acceptVisitor(this);
|
||||||
for (Statement part : statement.getConsequent()) {
|
writer.append(")").ws().append("{").softNewLine().indent();
|
||||||
part.acceptVisitor(this);
|
for (Statement part : statement.getConsequent()) {
|
||||||
}
|
|
||||||
if (!statement.getAlternative().isEmpty()) {
|
|
||||||
writer.outdent().append("}").ws().append("else").ws().append("{").indent().softNewLine();
|
|
||||||
for (Statement part : statement.getAlternative()) {
|
|
||||||
part.acceptVisitor(this);
|
part.acceptVisitor(this);
|
||||||
}
|
}
|
||||||
|
if (!statement.getAlternative().isEmpty()) {
|
||||||
|
writer.outdent().append("}").ws().append("else").ws();
|
||||||
|
if (statement.getAlternative().size() == 1 &&
|
||||||
|
statement.getAlternative().get(0) instanceof ConditionalStatement) {
|
||||||
|
statement = (ConditionalStatement)statement.getAlternative().get(0);
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
writer.append("{").indent().softNewLine();
|
||||||
|
for (Statement part : statement.getAlternative()) {
|
||||||
|
part.acceptVisitor(this);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
break;
|
||||||
}
|
}
|
||||||
writer.outdent().append("}").softNewLine();
|
writer.outdent().append("}").softNewLine();
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
|
|
Loading…
Reference in New Issue
Block a user