forked from openrs2/openrs2
parent
8ac16d722b
commit
5b032c980f
@ -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<BinaryExpr.Operator> SHIFT_OPS = ImmutableSet.of( |
||||
BinaryExpr.Operator.SIGNED_RIGHT_SHIFT, |
||||
BinaryExpr.Operator.UNSIGNED_RIGHT_SHIFT |
||||
); |
||||
|
||||
private static final ImmutableSet<BinaryExpr.Operator> 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)); |
||||
}); |
||||
} |
||||
} |
@ -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 |
||||
) |
||||
} |
||||
} |
Loading…
Reference in new issue