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: {
|
||||||
|
last = cond.getConsequent().isEmpty() ? null :
|
||||||
|
cond.getConsequent().get(cond.getConsequent().size() - 1);
|
||||||
if (last instanceof BreakStatement) {
|
if (last instanceof BreakStatement) {
|
||||||
BreakStatement breakStmt = (BreakStatement)last;
|
BreakStatement breakStmt = (BreakStatement)last;
|
||||||
if (currentExits.contains(breakStmt.getTarget()) || exits.contains(breakStmt.getTarget())) {
|
if (exit != null && exit == breakStmt.getTarget()) {
|
||||||
int refs = referencedStatements.get(breakStmt.getTarget());
|
int refs = referencedStatements.get(breakStmt.getTarget());
|
||||||
referencedStatements.put(breakStmt.getTarget(), refs - 1);
|
referencedStatements.put(breakStmt.getTarget(), refs - 1);
|
||||||
cond.getConsequent().remove(cond.getConsequent().size() - 1);
|
cond.getConsequent().remove(cond.getConsequent().size() - 1);
|
||||||
List<Statement> remaining = statements.subList(i + 1, statements.size());
|
List<Statement> remaining = statements.subList(i + 1, statements.size());
|
||||||
cond.getAlternative().addAll(remaining);
|
cond.getAlternative().addAll(remaining);
|
||||||
remaining.clear();
|
remaining.clear();
|
||||||
visit(cond);
|
break check_conditional;
|
||||||
if (resultStmt == cond) {
|
|
||||||
eliminateRedundantBreaks(cond.getConsequent(),
|
|
||||||
Collections.<IdentifiedStatement>emptySet(), nestedExits);
|
|
||||||
eliminateRedundantBreaks(cond.getAlternative(),
|
|
||||||
Collections.<IdentifiedStatement>emptySet(), nestedExits);
|
|
||||||
} else {
|
|
||||||
statements.set(i, resultStmt);
|
|
||||||
}
|
|
||||||
break;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
last = cond.getAlternative().isEmpty() ? null :
|
last = cond.getAlternative().isEmpty() ? null :
|
||||||
cond.getAlternative().get(cond.getAlternative().size() - 1);
|
cond.getAlternative().get(cond.getAlternative().size() - 1);
|
||||||
if (last instanceof BreakStatement) {
|
if (last instanceof BreakStatement) {
|
||||||
BreakStatement breakStmt = (BreakStatement)last;
|
BreakStatement breakStmt = (BreakStatement)last;
|
||||||
if (currentExits.contains(breakStmt.getTarget()) || exits.contains(breakStmt.getTarget())) {
|
if (exit != null && exit == breakStmt.getTarget()) {
|
||||||
int refs = referencedStatements.get(breakStmt.getTarget());
|
int refs = referencedStatements.get(breakStmt.getTarget());
|
||||||
referencedStatements.put(breakStmt.getTarget(), refs - 1);
|
referencedStatements.put(breakStmt.getTarget(), refs - 1);
|
||||||
cond.getAlternative().remove(cond.getConsequent().size() - 1);
|
cond.getAlternative().remove(cond.getConsequent().size() - 1);
|
||||||
List<Statement> remaining = statements.subList(i + 1, statements.size());
|
List<Statement> remaining = statements.subList(i + 1, statements.size());
|
||||||
cond.getConsequent().addAll(remaining);
|
cond.getConsequent().addAll(remaining);
|
||||||
remaining.clear();
|
remaining.clear();
|
||||||
visit(cond);
|
break check_conditional;
|
||||||
if (resultStmt == cond) {
|
|
||||||
eliminateRedundantBreaks(cond.getConsequent(),
|
|
||||||
Collections.<IdentifiedStatement>emptySet(), nestedExits);
|
|
||||||
eliminateRedundantBreaks(cond.getAlternative(),
|
|
||||||
Collections.<IdentifiedStatement>emptySet(), nestedExits);
|
|
||||||
}
|
}
|
||||||
statements.set(i, resultStmt);
|
}
|
||||||
break;
|
}
|
||||||
|
if (i == statements.size() - 1) {
|
||||||
|
eliminateRedundantBreaks(cond.getConsequent(), exit);
|
||||||
|
eliminateRedundantBreaks(cond.getAlternative(), exit);
|
||||||
|
}
|
||||||
|
normalizeConditional(cond);
|
||||||
|
if (cond.getConsequent().size() == 1 && cond.getConsequent().get(0) instanceof ConditionalStatement) {
|
||||||
|
ConditionalStatement innerCond = (ConditionalStatement)cond.getConsequent().get(0);
|
||||||
|
if (innerCond.getAlternative().isEmpty()) {
|
||||||
|
if (cond.getAlternative().isEmpty()) {
|
||||||
|
cond.getConsequent().clear();
|
||||||
|
cond.getConsequent().addAll(innerCond.getConsequent());
|
||||||
|
cond.setCondition(Expr.binary(BinaryOperation.AND, cond.getCondition(),
|
||||||
|
innerCond.getCondition()));
|
||||||
|
} else {
|
||||||
|
cond.setCondition(ExprOptimizer.invert(cond.getCondition()));
|
||||||
|
cond.getConsequent().clear();
|
||||||
|
cond.getConsequent().addAll(cond.getAlternative());
|
||||||
|
cond.getAlternative().clear();
|
||||||
|
cond.getAlternative().add(innerCond);
|
||||||
|
}
|
||||||
|
--i;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} 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,6 +385,7 @@ public class Renderer implements ExprVisitor, StatementVisitor {
|
||||||
@Override
|
@Override
|
||||||
public void visit(ConditionalStatement statement) {
|
public void visit(ConditionalStatement statement) {
|
||||||
try {
|
try {
|
||||||
|
while (true) {
|
||||||
writer.append("if").ws().append("(");
|
writer.append("if").ws().append("(");
|
||||||
statement.getCondition().acceptVisitor(this);
|
statement.getCondition().acceptVisitor(this);
|
||||||
writer.append(")").ws().append("{").softNewLine().indent();
|
writer.append(")").ws().append("{").softNewLine().indent();
|
||||||
|
@ -392,11 +393,19 @@ public class Renderer implements ExprVisitor, StatementVisitor {
|
||||||
part.acceptVisitor(this);
|
part.acceptVisitor(this);
|
||||||
}
|
}
|
||||||
if (!statement.getAlternative().isEmpty()) {
|
if (!statement.getAlternative().isEmpty()) {
|
||||||
writer.outdent().append("}").ws().append("else").ws().append("{").indent().softNewLine();
|
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()) {
|
for (Statement part : statement.getAlternative()) {
|
||||||
part.acceptVisitor(this);
|
part.acceptVisitor(this);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
break;
|
||||||
|
}
|
||||||
writer.outdent().append("}").softNewLine();
|
writer.outdent().append("}").softNewLine();
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
throw new RenderingException("IO error occured", e);
|
throw new RenderingException("IO error occured", e);
|
||||||
|
|
Loading…
Reference in New Issue
Block a user