Transform `-x + y` to `y - x` unless both have side effects

bzip2
Graham 5 years ago
parent fcc053270d
commit 14354a22f3
  1. 6
      deob-ast/src/main/java/dev/openrs2/deob/ast/transform/AddSubTransformer.java
  2. 18
      deob-ast/src/main/java/dev/openrs2/deob/ast/util/ExprUtils.java

@ -38,9 +38,11 @@ public final class AddSubTransformer extends Transformer {
/* x + -y => x - y */ /* x + -y => x - y */
expr.setOperator(BinaryExpr.Operator.MINUS); expr.setOperator(BinaryExpr.Operator.MINUS);
expr.setRight(ExprUtils.negate(right)); expr.setRight(ExprUtils.negate(right));
} else if (op == BinaryExpr.Operator.PLUS && isNegative(left)) { } else if (op == BinaryExpr.Operator.PLUS && isNegative(left) && !(ExprUtils.hasSideEffects(left) && ExprUtils.hasSideEffects(right))) {
/* -x + y => y - x */ /* -x + y => y - x */
// TODO(gpe): check for side effects before applying this transform expr.setOperator(BinaryExpr.Operator.MINUS);
expr.setLeft(right.clone());
expr.setRight(ExprUtils.negate(left));
} else if (op == BinaryExpr.Operator.MINUS && isNegative(right)) { } else if (op == BinaryExpr.Operator.MINUS && isNegative(right)) {
/* x - -y => x + y */ /* x - -y => x + y */
expr.setOperator(BinaryExpr.Operator.PLUS); expr.setOperator(BinaryExpr.Operator.PLUS);

@ -71,6 +71,24 @@ public final class ExprUtils {
return new UnaryExpr(expr.clone(), UnaryExpr.Operator.LOGICAL_COMPLEMENT); return new UnaryExpr(expr.clone(), UnaryExpr.Operator.LOGICAL_COMPLEMENT);
} }
public static boolean hasSideEffects(Expression expr) {
if (expr.isLiteralExpr() || expr.isNameExpr() | expr.isFieldAccessExpr()) {
return false;
} else if (expr.isEnclosedExpr()) {
return hasSideEffects(expr.asEnclosedExpr().getInner());
} else if (expr.isUnaryExpr()) {
return hasSideEffects(expr.asUnaryExpr().getExpression());
} else if (expr.isBinaryExpr()) {
var binary = expr.asBinaryExpr();
return hasSideEffects(binary.getLeft()) || hasSideEffects(binary.getRight());
} else if (expr.isArrayAccessExpr()) {
var access = expr.asArrayAccessExpr();
return hasSideEffects(access.getName()) || hasSideEffects(access.getIndex());
}
// TODO(gpe): more cases
return true;
}
private ExprUtils() { private ExprUtils() {
/* empty */ /* empty */
} }

Loading…
Cancel
Save