Use > and >= in for loops that decrement instead of increment

Signed-off-by: Graham <gpe@openrs2.dev>
bzip2
Graham 4 years ago
parent ba67f32d42
commit 29b63b613a
  1. 15
      deob-ast/src/main/java/dev/openrs2/deob/ast/transform/BinaryExprOrderTransformer.kt
  2. 29
      deob-ast/src/main/java/dev/openrs2/deob/ast/transform/ForLoopConditionTransformer.kt
  3. 14
      deob-ast/src/main/java/dev/openrs2/deob/ast/util/ExprUtils.kt

@ -4,6 +4,7 @@ import com.github.javaparser.ast.CompilationUnit
import com.github.javaparser.ast.expr.BinaryExpr
import dev.openrs2.deob.ast.Library
import dev.openrs2.deob.ast.LibraryGroup
import dev.openrs2.deob.ast.util.flip
import dev.openrs2.deob.ast.util.isString
import dev.openrs2.deob.ast.util.walk
import javax.inject.Singleton
@ -28,18 +29,4 @@ class BinaryExprOrderTransformer : Transformer() {
}
}
}
private fun BinaryExpr.Operator.flip(): BinaryExpr.Operator? {
return when (this) {
BinaryExpr.Operator.PLUS, BinaryExpr.Operator.MULTIPLY -> this
BinaryExpr.Operator.EQUALS, BinaryExpr.Operator.NOT_EQUALS -> this
BinaryExpr.Operator.BINARY_AND, BinaryExpr.Operator.BINARY_OR -> this
BinaryExpr.Operator.XOR, BinaryExpr.Operator.OR, BinaryExpr.Operator.AND -> this
BinaryExpr.Operator.GREATER -> BinaryExpr.Operator.LESS
BinaryExpr.Operator.GREATER_EQUALS -> BinaryExpr.Operator.LESS_EQUALS
BinaryExpr.Operator.LESS -> BinaryExpr.Operator.GREATER
BinaryExpr.Operator.LESS_EQUALS -> BinaryExpr.Operator.GREATER_EQUALS
else -> null
}
}
}

@ -1,10 +1,18 @@
package dev.openrs2.deob.ast.transform
import com.github.javaparser.ast.CompilationUnit
import com.github.javaparser.ast.expr.AssignExpr
import com.github.javaparser.ast.expr.BinaryExpr
import com.github.javaparser.ast.expr.Expression
import com.github.javaparser.ast.expr.UnaryExpr
import com.github.javaparser.ast.expr.UnaryExpr.Operator.POSTFIX_DECREMENT
import com.github.javaparser.ast.expr.UnaryExpr.Operator.POSTFIX_INCREMENT
import com.github.javaparser.ast.expr.UnaryExpr.Operator.PREFIX_DECREMENT
import com.github.javaparser.ast.expr.UnaryExpr.Operator.PREFIX_INCREMENT
import com.github.javaparser.ast.stmt.ForStmt
import dev.openrs2.deob.ast.Library
import dev.openrs2.deob.ast.LibraryGroup
import dev.openrs2.deob.ast.util.flip
import dev.openrs2.deob.ast.util.hasSideEffects
import dev.openrs2.deob.ast.util.walk
import javax.inject.Singleton
@ -13,6 +21,8 @@ import javax.inject.Singleton
class ForLoopConditionTransformer : Transformer() {
override fun transformUnit(group: LibraryGroup, library: Library, unit: CompilationUnit) {
unit.walk { stmt: ForStmt ->
val updatedExprs = stmt.update.mapNotNull { it.getUpdatedExpr() }
stmt.compare.ifPresent { compare ->
if (!compare.isBinaryExpr) {
return@ifPresent
@ -23,14 +33,23 @@ class ForLoopConditionTransformer : Transformer() {
return@ifPresent
}
val flipped = when (expr.operator) {
BinaryExpr.Operator.GREATER -> BinaryExpr.Operator.LESS
BinaryExpr.Operator.GREATER_EQUALS -> BinaryExpr.Operator.LESS_EQUALS
else -> return@ifPresent
}
val flipped = expr.operator.flip() ?: return@ifPresent
if (expr.left !in updatedExprs && expr.right in updatedExprs) {
stmt.setCompare(BinaryExpr(expr.right, expr.left, flipped))
}
}
}
}
private fun Expression.getUpdatedExpr(): Expression? {
return when (this) {
is UnaryExpr -> when (operator) {
PREFIX_INCREMENT, PREFIX_DECREMENT, POSTFIX_INCREMENT, POSTFIX_DECREMENT -> expression
else -> null
}
is AssignExpr -> target
else -> null
}
}
}

@ -122,3 +122,17 @@ fun Expression.hasSideEffects(): Boolean {
// TODO(gpe): more cases
return true
}
fun BinaryExpr.Operator.flip(): BinaryExpr.Operator? {
return when (this) {
BinaryExpr.Operator.PLUS, BinaryExpr.Operator.MULTIPLY -> this
BinaryExpr.Operator.EQUALS, BinaryExpr.Operator.NOT_EQUALS -> this
BinaryExpr.Operator.BINARY_AND, BinaryExpr.Operator.BINARY_OR -> this
BinaryExpr.Operator.XOR, BinaryExpr.Operator.OR, BinaryExpr.Operator.AND -> this
BinaryExpr.Operator.GREATER -> BinaryExpr.Operator.LESS
BinaryExpr.Operator.GREATER_EQUALS -> BinaryExpr.Operator.LESS_EQUALS
BinaryExpr.Operator.LESS -> BinaryExpr.Operator.GREATER
BinaryExpr.Operator.LESS_EQUALS -> BinaryExpr.Operator.GREATER_EQUALS
else -> null
}
}

Loading…
Cancel
Save