Search in sources :

Example 26 with Stmt

use of org.mapleir.ir.code.Stmt in project maple-ir by LLVM-but-worse.

the class LocalsPool method remap.

public static void remap(ControlFlowGraph cfg, Map<? extends Local, ? extends Local> remap) {
    for (BasicBlock b : cfg.vertices()) {
        for (Stmt stmt : b) {
            if (stmt.getOpcode() == Opcode.LOCAL_STORE) {
                VarExpr v = ((CopyVarStmt) stmt).getVariable();
                Local l = v.getLocal();
                if (remap.containsKey(l)) {
                    Local l2 = remap.get(l);
                    v.setLocal(l2);
                }
            }
            for (Expr s : stmt.enumerateOnlyChildren()) {
                if (s.getOpcode() == Opcode.LOCAL_LOAD) {
                    VarExpr v = (VarExpr) s;
                    Local l = v.getLocal();
                    if (remap.containsKey(l)) {
                        v.setLocal(remap.get(l));
                    }
                }
            }
        }
    }
}
Also used : VarExpr(org.mapleir.ir.code.expr.VarExpr) Expr(org.mapleir.ir.code.Expr) CopyVarStmt(org.mapleir.ir.code.stmt.copy.CopyVarStmt) BasicBlock(org.mapleir.ir.cfg.BasicBlock) VarExpr(org.mapleir.ir.code.expr.VarExpr) VersionedLocal(org.mapleir.ir.locals.impl.VersionedLocal) BasicLocal(org.mapleir.ir.locals.impl.BasicLocal) Stmt(org.mapleir.ir.code.Stmt) CopyVarStmt(org.mapleir.ir.code.stmt.copy.CopyVarStmt) AbstractCopyStmt(org.mapleir.ir.code.stmt.copy.AbstractCopyStmt)

Example 27 with Stmt

use of org.mapleir.ir.code.Stmt in project maple-ir by LLVM-but-worse.

the class ConcreteStaticInvocationPass method accept.

@Override
public int accept(AnalysisContext cxt, IPass prev, List<IPass> completed) {
    int fixed = 0;
    InvocationResolver resolver = cxt.getInvocationResolver();
    for (ClassNode cn : cxt.getApplication().iterate()) {
        for (MethodNode mn : cn.methods) {
            ControlFlowGraph cfg = cxt.getIRCache().getFor(mn);
            for (BasicBlock b : cfg.vertices()) {
                for (Stmt stmt : b) {
                    for (Expr e : stmt.enumerateOnlyChildren()) {
                        if (e.getOpcode() == Opcode.INVOKE) {
                            InvocationExpr invoke = (InvocationExpr) e;
                            if (invoke.isStatic()) {
                                MethodNode invoked = resolver.resolveStaticCall(invoke.getOwner(), invoke.getName(), invoke.getDesc());
                                if (invoked != null) {
                                    if (!invoked.owner.name.equals(invoke.getOwner())) {
                                        invoke.setOwner(invoked.owner.name);
                                        fixed++;
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
    }
    System.out.printf("  corrected %d dodgy static calls.%n", fixed);
    return fixed;
}
Also used : ClassNode(org.objectweb.asm.tree.ClassNode) MethodNode(org.objectweb.asm.tree.MethodNode) InvocationExpr(org.mapleir.ir.code.expr.invoke.InvocationExpr) Expr(org.mapleir.ir.code.Expr) ControlFlowGraph(org.mapleir.ir.cfg.ControlFlowGraph) InvocationResolver(org.mapleir.app.service.InvocationResolver) BasicBlock(org.mapleir.ir.cfg.BasicBlock) InvocationExpr(org.mapleir.ir.code.expr.invoke.InvocationExpr) Stmt(org.mapleir.ir.code.Stmt)

Example 28 with Stmt

use of org.mapleir.ir.code.Stmt 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 29 with Stmt

use of org.mapleir.ir.code.Stmt in project maple-ir by LLVM-but-worse.

the class DeadCodeEliminationPass method process.

public void process(ControlFlowGraph cfg) {
    LocalsPool lp = cfg.getLocals();
    boolean c;
    do {
        c = false;
        SimpleDfs<BasicBlock> dfs = new SimpleDfs<>(cfg, cfg.getEntries().iterator().next(), SimpleDfs.PRE);
        List<BasicBlock> pre = dfs.getPreOrder();
        for (BasicBlock b : new HashSet<>(cfg.vertices())) {
            if (!pre.contains(b)) {
                // System.out.println("proc1: " + b);
                for (FlowEdge<BasicBlock> fe : new HashSet<>(cfg.getEdges(b))) {
                    cfg.exciseEdge(fe);
                }
                // System.out.println("removed: ");
                for (Stmt stmt : b) {
                    // System.out.println(" " + (b.indexOf(stmt)) + ". " + stmt);
                    if (stmt instanceof AbstractCopyStmt) {
                        AbstractCopyStmt copy = (AbstractCopyStmt) stmt;
                        lp.defs.remove(copy.getVariable().getLocal());
                    // System.out.println("  kill1 " + copy.getVariable().getLocal());
                    }
                    for (Expr e : stmt.enumerateOnlyChildren()) {
                        if (e.getOpcode() == Opcode.LOCAL_LOAD) {
                            VarExpr v = (VarExpr) e;
                            lp.uses.get(v.getLocal()).remove(v);
                        // System.out.println("  kill2 " + v.getLocal());
                        }
                    }
                }
                cfg.removeVertex(b);
                deadBlocks++;
                c = true;
            } else {
                // System.out.println("proc2: " + b);
                UnconditionalJumpEdge<BasicBlock> uncond = null;
                for (FlowEdge<BasicBlock> fe : cfg.getEdges(b)) {
                    if (fe.getType() == FlowEdges.UNCOND) {
                        uncond = (UnconditionalJumpEdge<BasicBlock>) fe;
                    }
                }
                if (uncond != null) {
                    BasicBlock dst = uncond.dst();
                    List<BasicBlock> verts = new ArrayList<>(cfg.vertices());
                    if (verts.indexOf(b) + 1 == verts.indexOf(dst)) {
                        ImmediateEdge<BasicBlock> im = new ImmediateEdge<>(b, dst);
                        cfg.exciseEdge(uncond);
                        cfg.addEdge(b, im);
                        Stmt stmt = b.remove(b.size() - 1);
                        if (stmt.getOpcode() != Opcode.UNCOND_JUMP) {
                            throw new IllegalStateException(b + " : " + stmt);
                        }
                        immediateJumps++;
                        c = true;
                    }
                }
                // if(cfg.getMethod().toString().equals("cf.k(IIIIII)V")) {}
                Iterator<Stmt> it = b.iterator();
                while (it.hasNext()) {
                    Stmt stmt = it.next();
                    if (stmt.getOpcode() == Opcode.LOCAL_STORE) {
                        AbstractCopyStmt copy = (AbstractCopyStmt) stmt;
                        if (copy.isSynthetic()) {
                            continue;
                        }
                        Local l = copy.getVariable().getLocal();
                        LocalsPool pool = cfg.getLocals();
                        // System.out.println("copy: "+ copy);
                        if (!ConstraintUtil.isUncopyable(copy.getExpression()) && pool.uses.get(l).size() == 0) {
                            for (Expr e : copy.getExpression().enumerateWithSelf()) {
                                if (e.getOpcode() == Opcode.LOCAL_LOAD) {
                                    VarExpr v = (VarExpr) e;
                                    Local l2 = v.getLocal();
                                    pool.uses.remove(l2);
                                }
                            }
                            pool.uses.remove(l);
                            pool.defs.remove(l);
                            it.remove();
                            deadLocals++;
                            c = true;
                        }
                    } else if (stmt.getOpcode() == Opcode.NOP) {
                        it.remove();
                        c = true;
                    }
                }
            }
        }
    // for now
    } while (c);
}
Also used : ImmediateEdge(org.mapleir.flowgraph.edges.ImmediateEdge) BasicBlock(org.mapleir.ir.cfg.BasicBlock) ArrayList(java.util.ArrayList) Local(org.mapleir.ir.locals.Local) SimpleDfs(org.mapleir.stdlib.collections.graph.algorithms.SimpleDfs) Stmt(org.mapleir.ir.code.Stmt) AbstractCopyStmt(org.mapleir.ir.code.stmt.copy.AbstractCopyStmt) LocalsPool(org.mapleir.ir.locals.LocalsPool) VarExpr(org.mapleir.ir.code.expr.VarExpr) Expr(org.mapleir.ir.code.Expr) AbstractCopyStmt(org.mapleir.ir.code.stmt.copy.AbstractCopyStmt) VarExpr(org.mapleir.ir.code.expr.VarExpr) HashSet(java.util.HashSet)

Example 30 with Stmt

use of org.mapleir.ir.code.Stmt in project maple-ir by LLVM-but-worse.

the class LiftConstructorCallsPass method split.

private void split(ControlFlowGraph cfg, BasicBlock b, Stmt at) {
    BasicBlock newBlock = new BasicBlock(cfg, cfg.vertices().size() + 1, new LabelNode());
    cfg.addVertex(newBlock);
    System.out.println(ControlFlowGraph.printBlock(b));
    System.out.println("  to " + at);
    int index = b.indexOf(at) + 1;
    int size = b.size();
    for (int i = index; i < size; i++) {
        Stmt stmt = b.remove(index);
        stmt.setBlock(newBlock);
        newBlock.add(stmt);
    }
}
Also used : LabelNode(org.objectweb.asm.tree.LabelNode) BasicBlock(org.mapleir.ir.cfg.BasicBlock) Stmt(org.mapleir.ir.code.Stmt)

Aggregations

Stmt (org.mapleir.ir.code.Stmt)53 BasicBlock (org.mapleir.ir.cfg.BasicBlock)35 Expr (org.mapleir.ir.code.Expr)29 AbstractCopyStmt (org.mapleir.ir.code.stmt.copy.AbstractCopyStmt)28 VarExpr (org.mapleir.ir.code.expr.VarExpr)26 CopyPhiStmt (org.mapleir.ir.code.stmt.copy.CopyPhiStmt)25 CopyVarStmt (org.mapleir.ir.code.stmt.copy.CopyVarStmt)24 Local (org.mapleir.ir.locals.Local)21 VersionedLocal (org.mapleir.ir.locals.impl.VersionedLocal)19 PhiExpr (org.mapleir.ir.code.expr.PhiExpr)15 ConditionalJumpStmt (org.mapleir.ir.code.stmt.ConditionalJumpStmt)13 UnconditionalJumpStmt (org.mapleir.ir.code.stmt.UnconditionalJumpStmt)13 ThrowStmt (org.mapleir.ir.code.stmt.ThrowStmt)12 PopStmt (org.mapleir.ir.code.stmt.PopStmt)11 SwitchStmt (org.mapleir.ir.code.stmt.SwitchStmt)11 HashSet (java.util.HashSet)10 ControlFlowGraph (org.mapleir.ir.cfg.ControlFlowGraph)9 ConstantExpr (org.mapleir.ir.code.expr.ConstantExpr)8 InvocationExpr (org.mapleir.ir.code.expr.invoke.InvocationExpr)8 BasicLocal (org.mapleir.ir.locals.impl.BasicLocal)7