Convert some deob-ast methods to extension methods

bzip2
Graham 5 years ago
parent 5487a74eb8
commit 0a988584b7
  1. 16
      deob-ast/src/main/java/dev/openrs2/deob/ast/transform/AddSubTransformer.kt
  2. 16
      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. 26
      deob-ast/src/main/java/dev/openrs2/deob/ast/transform/IdentityTransformer.kt
  5. 38
      deob-ast/src/main/java/dev/openrs2/deob/ast/transform/IfElseTransformer.kt
  6. 10
      deob-ast/src/main/java/dev/openrs2/deob/ast/transform/IncrementTransformer.kt

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

@ -8,7 +8,7 @@ import dev.openrs2.deob.ast.util.walk
class BinaryExprOrderTransformer : Transformer() {
override fun transform(unit: CompilationUnit) {
unit.walk { expr: BinaryExpr ->
val op = flip(expr.operator) ?: return@walk
val op = expr.operator.flip() ?: return@walk
val type = expr.calculateResolvedType()
if (op == BinaryExpr.Operator.PLUS && type.isString()) {
@ -25,13 +25,12 @@ class BinaryExprOrderTransformer : Transformer() {
}
}
companion object {
private fun flip(op: BinaryExpr.Operator): BinaryExpr.Operator? {
return when (op) {
BinaryExpr.Operator.PLUS, BinaryExpr.Operator.MULTIPLY -> op
BinaryExpr.Operator.EQUALS, BinaryExpr.Operator.NOT_EQUALS -> op
BinaryExpr.Operator.BINARY_AND, BinaryExpr.Operator.BINARY_OR -> op
BinaryExpr.Operator.XOR, BinaryExpr.Operator.OR, BinaryExpr.Operator.AND -> op
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
@ -39,5 +38,4 @@ class BinaryExprOrderTransformer : Transformer() {
else -> null
}
}
}
}

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

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

@ -14,11 +14,11 @@ class IfElseTransformer : Transformer() {
stmt.elseStmt.ifPresent { elseStmt: Statement ->
val condition = stmt.condition
val thenStmt = stmt.thenStmt
if (isIf(thenStmt) && !isIf(elseStmt)) {
if (thenStmt.isIf() && !elseStmt.isIf()) {
stmt.condition = condition.not()
stmt.thenStmt = elseStmt.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
* if/else branches, as it'll introduce another level of
@ -45,8 +45,8 @@ class IfElseTransformer : Transformer() {
unit.walk { stmt: IfStmt ->
stmt.elseStmt.ifPresent { elseStmt ->
if (isIf(elseStmt)) {
stmt.setElseStmt(getIf(elseStmt))
if (elseStmt.isIf()) {
stmt.setElseStmt(elseStmt.getIf())
}
}
}
@ -97,7 +97,7 @@ class IfElseTransformer : Transformer() {
}
val thenStmt = ifStmt.thenStmt
if (!isTailThrowOrReturn(thenStmt)) {
if (!thenStmt.isTailThrowOrReturn()) {
return@ifPresent
}
@ -112,23 +112,22 @@ class IfElseTransformer : Transformer() {
}
}
companion object {
private fun isIf(stmt: Statement): Boolean {
private fun Statement.isIf(): Boolean {
return when {
stmt.isIfStmt -> true
stmt.isBlockStmt -> {
val stmts = stmt.asBlockStmt().statements
isIfStmt -> true
isBlockStmt -> {
val stmts = asBlockStmt().statements
stmts.size == 1 && stmts[0].isIfStmt
}
else -> false
}
}
private fun getIf(stmt: Statement): Statement {
if (stmt.isIfStmt) {
return stmt.clone()
} else if (stmt.isBlockStmt) {
val stmts = stmt.asBlockStmt().statements
private fun Statement.getIf(): Statement {
if (isIfStmt) {
return clone()
} else if (isBlockStmt) {
val stmts = asBlockStmt().statements
if (stmts.size == 1) {
val head = stmts[0]
if (head.isIfStmt) {
@ -139,11 +138,11 @@ class IfElseTransformer : Transformer() {
throw IllegalArgumentException()
}
private fun isTailThrowOrReturn(stmt: Statement): Boolean {
return if (stmt.isThrowStmt || stmt.isReturnStmt) {
private fun Statement.isTailThrowOrReturn(): Boolean {
return if (isThrowStmt || isReturnStmt) {
true
} else if (stmt.isBlockStmt) {
val stmts = stmt.asBlockStmt().statements
} else if (isBlockStmt) {
val stmts = asBlockStmt().statements
if (stmts.isEmpty()) {
return false
}
@ -154,5 +153,4 @@ class IfElseTransformer : Transformer() {
false
}
}
}
}

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

Loading…
Cancel
Save