Convert some deob-ast methods to extension methods

master
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
}
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 {
return when {
expr.isUnaryExpr -> expr.asUnaryExpr().operator == UnaryExpr.Operator.MINUS
expr.isIntegerLiteralExpr -> expr.asIntegerLiteralExpr().asInt() < 0
expr.isLongLiteralExpr -> expr.asLongLiteralExpr().asLong() < 0
else -> false
}
private fun Expression.isNegative(): Boolean {
return when {
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,19 +25,17 @@ 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
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
}
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
}
}
}

@ -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 {
return when {
expr.isIntegerLiteralExpr -> expr.asIntegerLiteralExpr().asNumber() == 0
expr.isLongLiteralExpr -> expr.asLongLiteralExpr().asNumber() == 0L
else -> false
}
private fun Expression.isZero(): Boolean {
return when {
isIntegerLiteralExpr -> asIntegerLiteralExpr().asNumber() == 0
isLongLiteralExpr -> asLongLiteralExpr().asNumber() == 0L
else -> false
}
}
private fun isOne(expr: Expression): Boolean {
return when {
expr.isIntegerLiteralExpr -> expr.asIntegerLiteralExpr().asNumber() == 1
expr.isLongLiteralExpr -> expr.asLongLiteralExpr().asNumber() == 1L
else -> false
}
private fun Expression.isOne(): Boolean {
return when {
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,47 +112,45 @@ class IfElseTransformer : Transformer() {
}
}
companion object {
private fun isIf(stmt: Statement): Boolean {
return when {
stmt.isIfStmt -> true
stmt.isBlockStmt -> {
val stmts = stmt.asBlockStmt().statements
stmts.size == 1 && stmts[0].isIfStmt
}
else -> false
private fun Statement.isIf(): Boolean {
return when {
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
if (stmts.size == 1) {
val head = stmts[0]
if (head.isIfStmt) {
return head.clone()
}
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) {
return head.clone()
}
}
throw IllegalArgumentException()
}
throw IllegalArgumentException()
}
private fun isTailThrowOrReturn(stmt: Statement): Boolean {
return if (stmt.isThrowStmt || stmt.isReturnStmt) {
true
} else if (stmt.isBlockStmt) {
val stmts = stmt.asBlockStmt().statements
if (stmts.isEmpty()) {
return false
}
val tail = stmts[stmts.size - 1]
tail.isThrowStmt || tail.isReturnStmt
} else {
false
private fun Statement.isTailThrowOrReturn(): Boolean {
return if (isThrowStmt || isReturnStmt) {
true
} else if (isBlockStmt) {
val stmts = asBlockStmt().statements
if (stmts.isEmpty()) {
return 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()
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) {
UnaryExpr.Operator.PREFIX_INCREMENT -> UnaryExpr.Operator.POSTFIX_INCREMENT
UnaryExpr.Operator.PREFIX_DECREMENT -> UnaryExpr.Operator.POSTFIX_DECREMENT
else -> 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 -> this
}
}

Loading…
Cancel
Save