From a7ac256c47edc6b017d58f6e5af31aaabea1f77f Mon Sep 17 00:00:00 2001 From: Alexey Andreev Date: Tue, 21 Nov 2017 22:40:22 +0300 Subject: [PATCH] Fix bug in AST optimization --- .../ast/optimization/OptimizingVisitor.java | 20 ++++++++++++------- 1 file changed, 13 insertions(+), 7 deletions(-) diff --git a/core/src/main/java/org/teavm/ast/optimization/OptimizingVisitor.java b/core/src/main/java/org/teavm/ast/optimization/OptimizingVisitor.java index 7b589bda8..a85fb960a 100644 --- a/core/src/main/java/org/teavm/ast/optimization/OptimizingVisitor.java +++ b/core/src/main/java/org/teavm/ast/optimization/OptimizingVisitor.java @@ -119,15 +119,21 @@ class OptimizingVisitor implements StatementVisitor, ExprVisitor { default: break; } - expr.getSecondOperand().acceptVisitor(this); - Expr b = resultExpr; - if (b instanceof ConstantExpr && expr.getOperation() == BinaryOperation.SUBTRACT) { - if (tryMakePositive((ConstantExpr) b)) { - expr.setOperation(BinaryOperation.ADD); + Expr b = expr.getSecondOperand(); + Expr a = expr.getFirstOperand(); + + if (a instanceof VariableExpr || a instanceof ConstantExpr) { + b.acceptVisitor(this); + b = resultExpr; + if (b instanceof ConstantExpr && expr.getOperation() == BinaryOperation.SUBTRACT) { + if (tryMakePositive((ConstantExpr) b)) { + expr.setOperation(BinaryOperation.ADD); + } } + a.acceptVisitor(this); + a = resultExpr; } - expr.getFirstOperand().acceptVisitor(this); - Expr a = resultExpr; + Expr p = a; Expr q = b; boolean invert = false;