From 5b032c980fed525f6baad4d607ac85181810eadb Mon Sep 17 00:00:00 2001 From: Graham Date: Thu, 26 Dec 2019 19:45:57 +0000 Subject: [PATCH] Convert BitMaskTransformer to Kotlin --- .../ast/transform/BitMaskTransformer.java | 79 ------------------- .../deob/ast/transform/BitMaskTransformer.kt | 68 ++++++++++++++++ 2 files changed, 68 insertions(+), 79 deletions(-) delete mode 100644 deob-ast/src/main/java/dev/openrs2/deob/ast/transform/BitMaskTransformer.java create mode 100644 deob-ast/src/main/java/dev/openrs2/deob/ast/transform/BitMaskTransformer.kt diff --git a/deob-ast/src/main/java/dev/openrs2/deob/ast/transform/BitMaskTransformer.java b/deob-ast/src/main/java/dev/openrs2/deob/ast/transform/BitMaskTransformer.java deleted file mode 100644 index ad404472..00000000 --- a/deob-ast/src/main/java/dev/openrs2/deob/ast/transform/BitMaskTransformer.java +++ /dev/null @@ -1,79 +0,0 @@ -package dev.openrs2.deob.ast.transform; - -import com.github.javaparser.ast.CompilationUnit; -import com.github.javaparser.ast.Node; -import com.github.javaparser.ast.expr.BinaryExpr; -import com.github.javaparser.ast.expr.IntegerLiteralExpr; -import com.google.common.collect.ImmutableSet; -import dev.openrs2.deob.ast.util.ExprUtils; -import dev.openrs2.deob.ast.util.NodeUtils; - -public final class BitMaskTransformer extends Transformer { - private static final ImmutableSet SHIFT_OPS = ImmutableSet.of( - BinaryExpr.Operator.SIGNED_RIGHT_SHIFT, - BinaryExpr.Operator.UNSIGNED_RIGHT_SHIFT - ); - - private static final ImmutableSet BITWISE_OPS = ImmutableSet.of( - BinaryExpr.Operator.BINARY_AND, - BinaryExpr.Operator.BINARY_OR, - BinaryExpr.Operator.XOR - ); - - @Override - public void transform(CompilationUnit unit) { - NodeUtils.walk(unit, Node.TreeTraversal.POSTORDER, BinaryExpr.class, expr -> { - var shiftOp = expr.getOperator(); - var left = expr.getLeft(); - var shamtExpr = expr.getRight(); - - if (!SHIFT_OPS.contains(shiftOp) || !left.isBinaryExpr() || !shamtExpr.isIntegerLiteralExpr()) { - return; - } - - var bitwiseExpr = left.asBinaryExpr(); - var bitwiseOp = bitwiseExpr.getOperator(); - var argExpr = bitwiseExpr.getLeft(); - var maskExpr = bitwiseExpr.getRight(); - - if (!BITWISE_OPS.contains(bitwiseOp) || !ExprUtils.isIntegerOrLongLiteral(maskExpr)) { - return; - } - - var shamt = shamtExpr.asIntegerLiteralExpr().asInt(); - if (maskExpr.isIntegerLiteralExpr()) { - var mask = maskExpr.asIntegerLiteralExpr().asInt(); - - switch (shiftOp) { - case SIGNED_RIGHT_SHIFT: - mask >>= shamt; - break; - case UNSIGNED_RIGHT_SHIFT: - mask >>>= shamt; - break; - default: - throw new IllegalStateException(); - } - - maskExpr = new IntegerLiteralExpr(mask); - } else { - var mask = maskExpr.asLongLiteralExpr().asLong(); - - switch (shiftOp) { - case SIGNED_RIGHT_SHIFT: - mask >>= shamt; - break; - case UNSIGNED_RIGHT_SHIFT: - mask >>>= shamt; - break; - default: - throw new IllegalStateException(); - } - - maskExpr = ExprUtils.createLong(mask); - } - - expr.replace(new BinaryExpr(new BinaryExpr(argExpr.clone(), shamtExpr.clone(), shiftOp), maskExpr, bitwiseOp)); - }); - } -} diff --git a/deob-ast/src/main/java/dev/openrs2/deob/ast/transform/BitMaskTransformer.kt b/deob-ast/src/main/java/dev/openrs2/deob/ast/transform/BitMaskTransformer.kt new file mode 100644 index 00000000..85173dfa --- /dev/null +++ b/deob-ast/src/main/java/dev/openrs2/deob/ast/transform/BitMaskTransformer.kt @@ -0,0 +1,68 @@ +package dev.openrs2.deob.ast.transform + +import com.github.javaparser.ast.CompilationUnit +import com.github.javaparser.ast.Node +import com.github.javaparser.ast.expr.BinaryExpr +import com.github.javaparser.ast.expr.IntegerLiteralExpr +import dev.openrs2.deob.ast.util.ExprUtils +import dev.openrs2.deob.ast.util.NodeUtils + +class BitMaskTransformer : Transformer() { + override fun transform(unit: CompilationUnit) { + NodeUtils.walk(unit, Node.TreeTraversal.POSTORDER, BinaryExpr::class.java) { expr -> + val shiftOp = expr.operator + val left = expr.left + val shamtExpr = expr.right + + if (!SHIFT_OPS.contains(shiftOp) || !left.isBinaryExpr || !shamtExpr.isIntegerLiteralExpr) { + return@walk + } + + val bitwiseExpr = left.asBinaryExpr() + val bitwiseOp = bitwiseExpr.operator + val argExpr = bitwiseExpr.left + var maskExpr = bitwiseExpr.right + + if (!BITWISE_OPS.contains(bitwiseOp) || !ExprUtils.isIntegerOrLongLiteral(maskExpr)) { + return@walk + } + + val shamt = shamtExpr.asIntegerLiteralExpr().asInt() + if (maskExpr.isIntegerLiteralExpr) { + var mask = maskExpr.asIntegerLiteralExpr().asInt() + + mask = when (shiftOp) { + BinaryExpr.Operator.SIGNED_RIGHT_SHIFT -> mask shr shamt + BinaryExpr.Operator.UNSIGNED_RIGHT_SHIFT -> mask ushr shamt + else -> throw IllegalStateException() + } + + maskExpr = IntegerLiteralExpr(mask) + } else { + var mask = maskExpr.asLongLiteralExpr().asLong() + + mask = when (shiftOp) { + BinaryExpr.Operator.SIGNED_RIGHT_SHIFT -> mask shr shamt + BinaryExpr.Operator.UNSIGNED_RIGHT_SHIFT -> mask ushr shamt + else -> throw IllegalStateException() + } + + maskExpr = ExprUtils.createLong(mask) + } + + expr.replace(BinaryExpr(BinaryExpr(argExpr.clone(), shamtExpr.clone(), shiftOp), maskExpr, bitwiseOp)) + } + } + + companion object { + private val SHIFT_OPS = setOf( + BinaryExpr.Operator.SIGNED_RIGHT_SHIFT, + BinaryExpr.Operator.UNSIGNED_RIGHT_SHIFT + ) + private val BITWISE_OPS = setOf( + BinaryExpr.Operator.BINARY_AND, + BinaryExpr.Operator.BINARY_OR, + BinaryExpr.Operator.XOR + ) + } +}