Convert some deob-ast methods to extension methods

bzip2
Graham 5 years ago
parent 5487a74eb8
commit 0a988584b7
  1. 20
      deob-ast/src/main/java/dev/openrs2/deob/ast/transform/AddSubTransformer.kt
  2. 26
      deob-ast/src/main/java/dev/openrs2/deob/ast/transform/BinaryExprOrderTransformer.kt
  3. 22
      deob-ast/src/main/java/dev/openrs2/deob/ast/transform/ComplementTransformer.kt
  4. 36
      deob-ast/src/main/java/dev/openrs2/deob/ast/transform/IdentityTransformer.kt
  5. 74
      deob-ast/src/main/java/dev/openrs2/deob/ast/transform/IfElseTransformer.kt
  6. 14
      deob-ast/src/main/java/dev/openrs2/deob/ast/transform/IncrementTransformer.kt

@ -21,11 +21,11 @@ class AddSubTransformer : Transformer() {
return@walk return@walk
} }
if (op == BinaryExpr.Operator.PLUS && isNegative(right)) { if (op == BinaryExpr.Operator.PLUS && right.isNegative()) {
// x + -y => x - y // x + -y => x - y
expr.operator = BinaryExpr.Operator.MINUS expr.operator = BinaryExpr.Operator.MINUS
expr.right = right.negate() expr.right = right.negate()
} else if (op == BinaryExpr.Operator.PLUS && isNegative(left)) { } else if (op == BinaryExpr.Operator.PLUS && left.isNegative()) {
if (expr.hasSideEffects()) { if (expr.hasSideEffects()) {
return@walk return@walk
} }
@ -34,7 +34,7 @@ class AddSubTransformer : Transformer() {
expr.operator = BinaryExpr.Operator.MINUS expr.operator = BinaryExpr.Operator.MINUS
expr.left = right.clone() expr.left = right.clone()
expr.right = left.negate() expr.right = left.negate()
} else if (op == BinaryExpr.Operator.MINUS && isNegative(right)) { } else if (op == BinaryExpr.Operator.MINUS && right.isNegative()) {
// x - -y => x + y // x - -y => x + y
expr.operator = BinaryExpr.Operator.PLUS expr.operator = BinaryExpr.Operator.PLUS
expr.right = right.negate() expr.right = right.negate()
@ -42,14 +42,12 @@ class AddSubTransformer : Transformer() {
} }
} }
companion object { private fun Expression.isNegative(): Boolean {
private fun isNegative(expr: Expression): Boolean { return when {
return when { isUnaryExpr -> asUnaryExpr().operator == UnaryExpr.Operator.MINUS
expr.isUnaryExpr -> expr.asUnaryExpr().operator == UnaryExpr.Operator.MINUS isIntegerLiteralExpr -> asIntegerLiteralExpr().asInt() < 0
expr.isIntegerLiteralExpr -> expr.asIntegerLiteralExpr().asInt() < 0 isLongLiteralExpr -> asLongLiteralExpr().asLong() < 0
expr.isLongLiteralExpr -> expr.asLongLiteralExpr().asLong() < 0 else -> false
else -> false
}
} }
} }
} }

@ -8,7 +8,7 @@ import dev.openrs2.deob.ast.util.walk
class BinaryExprOrderTransformer : Transformer() { class BinaryExprOrderTransformer : Transformer() {
override fun transform(unit: CompilationUnit) { override fun transform(unit: CompilationUnit) {
unit.walk { expr: BinaryExpr -> unit.walk { expr: BinaryExpr ->
val op = flip(expr.operator) ?: return@walk val op = expr.operator.flip() ?: return@walk
val type = expr.calculateResolvedType() val type = expr.calculateResolvedType()
if (op == BinaryExpr.Operator.PLUS && type.isString()) { if (op == BinaryExpr.Operator.PLUS && type.isString()) {
@ -25,19 +25,17 @@ class BinaryExprOrderTransformer : Transformer() {
} }
} }
companion object { private fun BinaryExpr.Operator.flip(): BinaryExpr.Operator? {
private fun flip(op: BinaryExpr.Operator): BinaryExpr.Operator? { return when (this) {
return when (op) { BinaryExpr.Operator.PLUS, BinaryExpr.Operator.MULTIPLY -> this
BinaryExpr.Operator.PLUS, BinaryExpr.Operator.MULTIPLY -> op BinaryExpr.Operator.EQUALS, BinaryExpr.Operator.NOT_EQUALS -> this
BinaryExpr.Operator.EQUALS, BinaryExpr.Operator.NOT_EQUALS -> op BinaryExpr.Operator.BINARY_AND, BinaryExpr.Operator.BINARY_OR -> this
BinaryExpr.Operator.BINARY_AND, BinaryExpr.Operator.BINARY_OR -> op BinaryExpr.Operator.XOR, BinaryExpr.Operator.OR, BinaryExpr.Operator.AND -> this
BinaryExpr.Operator.XOR, BinaryExpr.Operator.OR, BinaryExpr.Operator.AND -> op BinaryExpr.Operator.GREATER -> BinaryExpr.Operator.LESS
BinaryExpr.Operator.GREATER -> BinaryExpr.Operator.LESS BinaryExpr.Operator.GREATER_EQUALS -> BinaryExpr.Operator.LESS_EQUALS
BinaryExpr.Operator.GREATER_EQUALS -> BinaryExpr.Operator.LESS_EQUALS BinaryExpr.Operator.LESS -> BinaryExpr.Operator.GREATER
BinaryExpr.Operator.LESS -> BinaryExpr.Operator.GREATER BinaryExpr.Operator.LESS_EQUALS -> BinaryExpr.Operator.GREATER_EQUALS
BinaryExpr.Operator.LESS_EQUALS -> BinaryExpr.Operator.GREATER_EQUALS else -> null
else -> null
}
} }
} }
} }

@ -18,21 +18,21 @@ class ComplementTransformer : Transformer() {
val right = expr.right val right = expr.right
val bothLiteral = left.isIntegerOrLongLiteral() && right.isIntegerOrLongLiteral() val bothLiteral = left.isIntegerOrLongLiteral() && right.isIntegerOrLongLiteral()
if (isComplementOrLiteral(left) && isComplementOrLiteral(right) && !bothLiteral) { if (left.isComplementOrLiteral() && right.isComplementOrLiteral() && !bothLiteral) {
expr.operator = op expr.operator = op
expr.left = complement(left) expr.left = left.complement()
expr.right = complement(right) expr.right = right.complement()
} }
} }
} }
companion object { companion object {
private fun isComplement(expr: Expression): Boolean { private fun Expression.isComplement(): Boolean {
return expr.isUnaryExpr && expr.asUnaryExpr().operator == UnaryExpr.Operator.BITWISE_COMPLEMENT return isUnaryExpr && asUnaryExpr().operator == UnaryExpr.Operator.BITWISE_COMPLEMENT
} }
private fun isComplementOrLiteral(expr: Expression): Boolean { private fun Expression.isComplementOrLiteral(): Boolean {
return isComplement(expr) || expr.isIntegerOrLongLiteral() return isComplement() || isIntegerOrLongLiteral()
} }
private fun complement(op: BinaryExpr.Operator): BinaryExpr.Operator? { private fun complement(op: BinaryExpr.Operator): BinaryExpr.Operator? {
@ -46,11 +46,11 @@ class ComplementTransformer : Transformer() {
} }
} }
private fun complement(expr: Expression): Expression { private fun Expression.complement(): Expression {
return when { return when {
expr.isUnaryExpr -> expr.asUnaryExpr().expression isUnaryExpr -> asUnaryExpr().expression
expr.isIntegerLiteralExpr -> IntegerLiteralExpr(expr.asIntegerLiteralExpr().asInt().inv()) isIntegerLiteralExpr -> IntegerLiteralExpr(asIntegerLiteralExpr().asInt().inv())
expr.isLongLiteralExpr -> createLong(expr.asLongLiteralExpr().asLong().inv()) isLongLiteralExpr -> createLong(asLongLiteralExpr().asLong().inv())
else -> throw IllegalArgumentException() else -> throw IllegalArgumentException()
} }
} }

@ -12,28 +12,28 @@ class IdentityTransformer : Transformer() {
@Suppress("NON_EXHAUSTIVE_WHEN") @Suppress("NON_EXHAUSTIVE_WHEN")
when (expr.operator) { when (expr.operator) {
BinaryExpr.Operator.PLUS -> { BinaryExpr.Operator.PLUS -> {
if (isZero(expr.left)) { if (expr.left.isZero()) {
// 0 + x => x // 0 + x => x
expr.replace(expr.right) expr.replace(expr.right)
} else if (isZero(expr.right)) { } else if (expr.right.isZero()) {
// x + 0 => x // x + 0 => x
expr.replace(expr.left) expr.replace(expr.left)
} }
} }
BinaryExpr.Operator.MINUS -> { BinaryExpr.Operator.MINUS -> {
if (isZero(expr.left)) { if (expr.left.isZero()) {
// 0 - x => -x // 0 - x => -x
expr.replace(UnaryExpr(expr.right, UnaryExpr.Operator.MINUS)) expr.replace(UnaryExpr(expr.right, UnaryExpr.Operator.MINUS))
} else if (isZero(expr.right)) { } else if (expr.right.isZero()) {
// x - 0 => x // x - 0 => x
expr.replace(expr.left) expr.replace(expr.left)
} }
} }
BinaryExpr.Operator.MULTIPLY -> { BinaryExpr.Operator.MULTIPLY -> {
if (isOne(expr.left)) { if (expr.left.isOne()) {
// 1 * x => x // 1 * x => x
expr.replace(expr.right) expr.replace(expr.right)
} else if (isOne(expr.right)) { } else if (expr.right.isOne()) {
// x * 1 => x // x * 1 => x
expr.replace(expr.left) expr.replace(expr.left)
} }
@ -42,21 +42,19 @@ class IdentityTransformer : Transformer() {
} }
} }
companion object { private fun Expression.isZero(): Boolean {
private fun isZero(expr: Expression): Boolean { return when {
return when { isIntegerLiteralExpr -> asIntegerLiteralExpr().asNumber() == 0
expr.isIntegerLiteralExpr -> expr.asIntegerLiteralExpr().asNumber() == 0 isLongLiteralExpr -> asLongLiteralExpr().asNumber() == 0L
expr.isLongLiteralExpr -> expr.asLongLiteralExpr().asNumber() == 0L else -> false
else -> false
}
} }
}
private fun isOne(expr: Expression): Boolean { private fun Expression.isOne(): Boolean {
return when { return when {
expr.isIntegerLiteralExpr -> expr.asIntegerLiteralExpr().asNumber() == 1 isIntegerLiteralExpr -> asIntegerLiteralExpr().asNumber() == 1
expr.isLongLiteralExpr -> expr.asLongLiteralExpr().asNumber() == 1L isLongLiteralExpr -> asLongLiteralExpr().asNumber() == 1L
else -> false else -> false
}
} }
} }
} }

@ -14,11 +14,11 @@ class IfElseTransformer : Transformer() {
stmt.elseStmt.ifPresent { elseStmt: Statement -> stmt.elseStmt.ifPresent { elseStmt: Statement ->
val condition = stmt.condition val condition = stmt.condition
val thenStmt = stmt.thenStmt val thenStmt = stmt.thenStmt
if (isIf(thenStmt) && !isIf(elseStmt)) { if (thenStmt.isIf() && !elseStmt.isIf()) {
stmt.condition = condition.not() stmt.condition = condition.not()
stmt.thenStmt = elseStmt.clone() stmt.thenStmt = elseStmt.clone()
stmt.setElseStmt(thenStmt.clone()) stmt.setElseStmt(thenStmt.clone())
} else if (!isIf(thenStmt) && isIf(elseStmt)) { } else if (!thenStmt.isIf() && elseStmt.isIf()) {
/* /*
* Don't consider any more conditions for swapping the * Don't consider any more conditions for swapping the
* if/else branches, as it'll introduce another level of * if/else branches, as it'll introduce another level of
@ -45,8 +45,8 @@ class IfElseTransformer : Transformer() {
unit.walk { stmt: IfStmt -> unit.walk { stmt: IfStmt ->
stmt.elseStmt.ifPresent { elseStmt -> stmt.elseStmt.ifPresent { elseStmt ->
if (isIf(elseStmt)) { if (elseStmt.isIf()) {
stmt.setElseStmt(getIf(elseStmt)) stmt.setElseStmt(elseStmt.getIf())
} }
} }
} }
@ -97,7 +97,7 @@ class IfElseTransformer : Transformer() {
} }
val thenStmt = ifStmt.thenStmt val thenStmt = ifStmt.thenStmt
if (!isTailThrowOrReturn(thenStmt)) { if (!thenStmt.isTailThrowOrReturn()) {
return@ifPresent return@ifPresent
} }
@ -112,47 +112,45 @@ class IfElseTransformer : Transformer() {
} }
} }
companion object { private fun Statement.isIf(): Boolean {
private fun isIf(stmt: Statement): Boolean { return when {
return when { isIfStmt -> true
stmt.isIfStmt -> true isBlockStmt -> {
stmt.isBlockStmt -> { val stmts = asBlockStmt().statements
val stmts = stmt.asBlockStmt().statements stmts.size == 1 && stmts[0].isIfStmt
stmts.size == 1 && stmts[0].isIfStmt
}
else -> false
} }
else -> false
} }
}
private fun getIf(stmt: Statement): Statement { private fun Statement.getIf(): Statement {
if (stmt.isIfStmt) { if (isIfStmt) {
return stmt.clone() return clone()
} else if (stmt.isBlockStmt) { } else if (isBlockStmt) {
val stmts = stmt.asBlockStmt().statements val stmts = asBlockStmt().statements
if (stmts.size == 1) { if (stmts.size == 1) {
val head = stmts[0] val head = stmts[0]
if (head.isIfStmt) { if (head.isIfStmt) {
return head.clone() return head.clone()
}
} }
} }
throw IllegalArgumentException()
} }
throw IllegalArgumentException()
}
private fun isTailThrowOrReturn(stmt: Statement): Boolean { private fun Statement.isTailThrowOrReturn(): Boolean {
return if (stmt.isThrowStmt || stmt.isReturnStmt) { return if (isThrowStmt || isReturnStmt) {
true true
} else if (stmt.isBlockStmt) { } else if (isBlockStmt) {
val stmts = stmt.asBlockStmt().statements val stmts = asBlockStmt().statements
if (stmts.isEmpty()) { if (stmts.isEmpty()) {
return false return false
}
val tail = stmts[stmts.size - 1]
tail.isThrowStmt || tail.isReturnStmt
} else {
false
} }
val tail = stmts[stmts.size - 1]
tail.isThrowStmt || tail.isReturnStmt
} else {
false
} }
} }
} }

@ -14,7 +14,7 @@ class IncrementTransformer : Transformer() {
} }
val unaryExpr = stmt.expression.asUnaryExpr() val unaryExpr = stmt.expression.asUnaryExpr()
unaryExpr.operator = prefixToPostfix(unaryExpr.operator) unaryExpr.operator = unaryExpr.operator.toPostfix()
} }
unit.walk { stmt: ForStmt -> unit.walk { stmt: ForStmt ->
@ -24,16 +24,14 @@ class IncrementTransformer : Transformer() {
} }
val unaryExpr = expr.asUnaryExpr() val unaryExpr = expr.asUnaryExpr()
unaryExpr.operator = prefixToPostfix(unaryExpr.operator) unaryExpr.operator = unaryExpr.operator.toPostfix()
} }
} }
} }
companion object { private fun UnaryExpr.Operator.toPostfix() = when (this) {
private fun prefixToPostfix(operator: UnaryExpr.Operator) = when (operator) { UnaryExpr.Operator.PREFIX_INCREMENT -> UnaryExpr.Operator.POSTFIX_INCREMENT
UnaryExpr.Operator.PREFIX_INCREMENT -> UnaryExpr.Operator.POSTFIX_INCREMENT UnaryExpr.Operator.PREFIX_DECREMENT -> UnaryExpr.Operator.POSTFIX_DECREMENT
UnaryExpr.Operator.PREFIX_DECREMENT -> UnaryExpr.Operator.POSTFIX_DECREMENT else -> this
else -> operator
}
} }
} }

Loading…
Cancel
Save