Search in sources :

Example 1 with Operator

use of org.mapleir.ir.code.expr.ArithmeticExpr.Operator in project maple-ir by LLVM-but-worse.

the class ConstantExpressionReorderPass method transform.

private int transform(ControlFlowGraph ir) {
    int i = 0;
    for (BasicBlock b : ir.vertices()) {
        for (Stmt stmt : b) {
            if (stmt.getOpcode() == COND_JUMP) {
                ConditionalJumpStmt cjs = (ConditionalJumpStmt) stmt;
                Expr r = cjs.getRight();
                Expr l = cjs.getLeft();
                ComparisonType type = cjs.getComparisonType();
                if (type == ComparisonType.EQ || type == ComparisonType.NE) {
                    if (shouldReorder(r, l)) {
                        cjs.setRight(null);
                        cjs.setLeft(null);
                        cjs.setLeft(r);
                        cjs.setRight(l);
                        i++;
                    }
                }
            }
            for (Expr e : stmt.enumerateOnlyChildren()) {
                if (e.getOpcode() == ARITHMETIC) {
                    ArithmeticExpr arith = (ArithmeticExpr) e;
                    Expr r = arith.getRight();
                    Expr l = arith.getLeft();
                    Operator op = arith.getOperator();
                    if (!op.doesOrderMatter()) {
                        if (shouldReorder(r, l)) {
                            arith.setRight(null);
                            arith.setLeft(null);
                            arith.setLeft(r);
                            arith.setRight(l);
                            i++;
                        }
                    }
                }
            }
        }
    }
    return i;
}
Also used : ConditionalJumpStmt(org.mapleir.ir.code.stmt.ConditionalJumpStmt) Operator(org.mapleir.ir.code.expr.ArithmeticExpr.Operator) ComparisonType(org.mapleir.ir.code.stmt.ConditionalJumpStmt.ComparisonType) ArithmeticExpr(org.mapleir.ir.code.expr.ArithmeticExpr) Expr(org.mapleir.ir.code.Expr) ArithmeticExpr(org.mapleir.ir.code.expr.ArithmeticExpr) BasicBlock(org.mapleir.ir.cfg.BasicBlock) Stmt(org.mapleir.ir.code.Stmt) ConditionalJumpStmt(org.mapleir.ir.code.stmt.ConditionalJumpStmt)

Example 2 with Operator

use of org.mapleir.ir.code.expr.ArithmeticExpr.Operator in project maple-ir by LLVM-but-worse.

the class FieldRSADecryptionPass method bcheck1.

static boolean bcheck1(Expr e) {
    if (e.getOpcode() == ARITHMETIC) {
        ArithmeticExpr ar = (ArithmeticExpr) e;
        Operator op = ar.getOperator();
        return op != Operator.MUL && op != Operator.ADD;
    } else {
        return true;
    }
}
Also used : Operator(org.mapleir.ir.code.expr.ArithmeticExpr.Operator) ArithmeticExpr(org.mapleir.ir.code.expr.ArithmeticExpr)

Example 3 with Operator

use of org.mapleir.ir.code.expr.ArithmeticExpr.Operator in project maple-ir by LLVM-but-worse.

the class ExpressionEvaluator method reassociate.

private ArithmeticExpr reassociate(LocalsPool pool, ArithmeticExpr ae) {
    ArithmeticExpr leftAe = (ArithmeticExpr) ae.getLeft();
    Operator operatorA = leftAe.getOperator();
    Operator operatorB = ae.getOperator();
    Expr r1 = eval(pool, leftAe.getRight());
    Expr r2 = eval(pool, ae.getRight());
    if (r1 != null && r2 != null) {
        ConstantExpr cr1 = (ConstantExpr) r1;
        ConstantExpr cr2 = (ConstantExpr) r2;
        int sign = 0;
        if ((operatorA == MUL && operatorB == MUL)) {
            sign = 1;
        } else if (operatorA == ADD && (operatorB == ADD || operatorB == SUB)) {
            // what about overflow?? integers mod 2^32 forms a group over addition...should be ok?
            sign = 1;
        } else if (operatorA == SUB && (operatorB == ADD || operatorB == SUB)) {
            sign = -1;
        }
        if (sign != 0) {
            ConstantExpr cr1r2 = eval(pool, new ArithmeticExpr(sign > 0 ? cr2 : new NegationExpr(cr2), cr1, operatorB));
            Object associated = cr1r2.getConstant();
            return new ArithmeticExpr(new ConstantExpr(associated, cr1r2.getType()), leftAe.getLeft().copy(), operatorA);
        }
    }
    return null;
}
Also used : Operator(org.mapleir.ir.code.expr.ArithmeticExpr.Operator) ArithmeticExpr(org.mapleir.ir.code.expr.ArithmeticExpr) VarExpr(org.mapleir.ir.code.expr.VarExpr) Expr(org.mapleir.ir.code.Expr) ConstantExpr(org.mapleir.ir.code.expr.ConstantExpr) CastExpr(org.mapleir.ir.code.expr.CastExpr) ComparisonExpr(org.mapleir.ir.code.expr.ComparisonExpr) NegationExpr(org.mapleir.ir.code.expr.NegationExpr) ArithmeticExpr(org.mapleir.ir.code.expr.ArithmeticExpr) ConstantExpr(org.mapleir.ir.code.expr.ConstantExpr) NegationExpr(org.mapleir.ir.code.expr.NegationExpr)

Aggregations

ArithmeticExpr (org.mapleir.ir.code.expr.ArithmeticExpr)3 Operator (org.mapleir.ir.code.expr.ArithmeticExpr.Operator)3 Expr (org.mapleir.ir.code.Expr)2 BasicBlock (org.mapleir.ir.cfg.BasicBlock)1 Stmt (org.mapleir.ir.code.Stmt)1 CastExpr (org.mapleir.ir.code.expr.CastExpr)1 ComparisonExpr (org.mapleir.ir.code.expr.ComparisonExpr)1 ConstantExpr (org.mapleir.ir.code.expr.ConstantExpr)1 NegationExpr (org.mapleir.ir.code.expr.NegationExpr)1 VarExpr (org.mapleir.ir.code.expr.VarExpr)1 ConditionalJumpStmt (org.mapleir.ir.code.stmt.ConditionalJumpStmt)1 ComparisonType (org.mapleir.ir.code.stmt.ConditionalJumpStmt.ComparisonType)1