Search in sources :

Example 1 with Value

use of com.googlecode.dex2jar.ir.expr.Value in project dex2jar by pxb1988.

the class DeadCodeTransformer method transform.

@Override
public void transform(IrMethod method) {
    Cfg.createCFG(method);
    Cfg.dfsVisit(method, null);
    if (method.traps != null) {
        for (Iterator<Trap> it = method.traps.iterator(); it.hasNext(); ) {
            Trap t = it.next();
            boolean allNotThrow = true;
            for (Stmt p = t.start; p != t.end; p = p.getNext()) {
                if (p.visited && Cfg.isThrow(p)) {
                    allNotThrow = false;
                    break;
                }
            }
            if (allNotThrow) {
                it.remove();
                continue;
            }
            boolean allNotVisited = true;
            boolean allVisited = true;
            for (LabelStmt labelStmt : t.handlers) {
                if (labelStmt.visited) {
                    allNotVisited = false;
                } else {
                    allVisited = false;
                }
            }
            if (allNotVisited) {
                it.remove();
            } else {
                // keep start and end
                t.start.visited = true;
                t.end.visited = true;
                if (!allVisited) {
                    // part visited
                    List<String> types = new ArrayList<>(t.handlers.length);
                    List<LabelStmt> labelStmts = new ArrayList<>(t.handlers.length);
                    for (int i = 0; i < t.handlers.length; i++) {
                        labelStmts.add(t.handlers[i]);
                        types.add(t.types[i]);
                    }
                    t.handlers = labelStmts.toArray(new LabelStmt[labelStmts.size()]);
                    t.types = types.toArray(new String[types.size()]);
                }
            }
        }
    }
    Set<Local> definedLocals = new HashSet<>();
    for (Iterator<Stmt> it = method.stmts.iterator(); it.hasNext(); ) {
        Stmt p = it.next();
        if (!p.visited) {
            it.remove();
            continue;
        }
        if (p.st == Stmt.ST.ASSIGN || p.st == Stmt.ST.IDENTITY) {
            if (p.getOp1().vt == Value.VT.LOCAL) {
                definedLocals.add((Local) p.getOp1());
            }
        }
    }
    if (method.phiLabels != null) {
        for (Iterator<LabelStmt> it = method.phiLabels.iterator(); it.hasNext(); ) {
            LabelStmt labelStmt = it.next();
            if (!labelStmt.visited) {
                it.remove();
                continue;
            }
            if (labelStmt.phis != null) {
                for (AssignStmt phi : labelStmt.phis) {
                    definedLocals.add((Local) phi.getOp1());
                }
            }
        }
    }
    method.locals.clear();
    method.locals.addAll(definedLocals);
    Set<Value> tmp = new HashSet<>();
    if (method.phiLabels != null) {
        for (Iterator<LabelStmt> it = method.phiLabels.iterator(); it.hasNext(); ) {
            LabelStmt labelStmt = it.next();
            if (labelStmt.phis != null) {
                for (AssignStmt phi : labelStmt.phis) {
                    PhiExpr phiExpr = (PhiExpr) phi.getOp2();
                    boolean needRebuild = false;
                    for (Value v : phiExpr.getOps()) {
                        if (!definedLocals.contains(v)) {
                            needRebuild = true;
                            break;
                        }
                    }
                    if (needRebuild) {
                        for (Value v : phiExpr.getOps()) {
                            if (definedLocals.contains(v)) {
                                tmp.add(v);
                            }
                        }
                        phiExpr.setOps(tmp.toArray(new Value[tmp.size()]));
                        tmp.clear();
                    }
                }
            }
        }
    }
}
Also used : LabelStmt(com.googlecode.dex2jar.ir.stmt.LabelStmt) AssignStmt(com.googlecode.dex2jar.ir.stmt.AssignStmt) Local(com.googlecode.dex2jar.ir.expr.Local) Trap(com.googlecode.dex2jar.ir.Trap) LabelStmt(com.googlecode.dex2jar.ir.stmt.LabelStmt) AssignStmt(com.googlecode.dex2jar.ir.stmt.AssignStmt) Stmt(com.googlecode.dex2jar.ir.stmt.Stmt) PhiExpr(com.googlecode.dex2jar.ir.expr.PhiExpr) Value(com.googlecode.dex2jar.ir.expr.Value)

Example 2 with Value

use of com.googlecode.dex2jar.ir.expr.Value in project dex2jar by pxb1988.

the class FixVar method transform.

@Override
public void transform(IrMethod irMethod) {
    int i = 0;
    for (LocalVar var : irMethod.vars) {
        if (var.reg.trim().vt != VT.LOCAL) {
            if (var.reg.trim().vt == VT.CONSTANT) {
                Local n = new Local(i++);
                Value old = var.reg.trim();
                irMethod.stmts.insertBefore(var.start, Stmts.nAssign(n, old));
                var.reg = n;
                irMethod.locals.add(n);
            } else {
            // throw new DexExcpeption("not support");
            }
        }
    }
}
Also used : Value(com.googlecode.dex2jar.ir.expr.Value) Local(com.googlecode.dex2jar.ir.expr.Local) LocalVar(com.googlecode.dex2jar.ir.LocalVar)

Example 3 with Value

use of com.googlecode.dex2jar.ir.expr.Value in project dex2jar by pxb1988.

the class Ir2JRegAssignTransformer method genGraph.

private Reg[] genGraph(IrMethod method, final Reg[] regs) {
    Reg[] args;
    if (method.isStatic) {
        args = new Reg[method.args.length];
    } else {
        args = new Reg[method.args.length + 1];
    }
    Set<Stmt> tos = new HashSet<>();
    for (Stmt stmt : method.stmts) {
        if (stmt.st == ST.ASSIGN || stmt.st == ST.IDENTITY) {
            if (stmt.getOp1().vt == VT.LOCAL) {
                Local left = (Local) stmt.getOp1();
                Value op2 = stmt.getOp2();
                int idx = left._ls_index;
                Reg leftReg = regs[idx];
                // a new local can't effect next value live in next frame
                Cfg.collectTos(stmt, tos);
                for (Stmt next : tos) {
                    SimpleLiveValue[] frame = (SimpleLiveValue[]) next.frame;
                    if (frame == null) {
                        continue;
                    }
                    for (int i = 0; i < frame.length; i++) {
                        if (i == idx) {
                            continue;
                        }
                        SimpleLiveValue v = frame[i];
                        if (v != null && v.used) {
                            Reg rightReg = regs[i];
                            leftReg.excludes.add(rightReg);
                            rightReg.excludes.add(leftReg);
                        }
                    }
                }
                tos.clear();
                // Preferred same reg can save load-store
                if (op2.vt == VT.LOCAL) {
                    Reg rightReg = regs[((Local) op2)._ls_index];
                    leftReg.prefers.add(rightReg);
                    rightReg.prefers.add(leftReg);
                }
                // record @this @parameter_x
                if (op2.vt == VT.THIS_REF) {
                    args[0] = leftReg;
                } else if (op2.vt == VT.PARAMETER_REF) {
                    RefExpr refExpr = (RefExpr) op2;
                    if (method.isStatic) {
                        args[refExpr.parameterIndex] = leftReg;
                    } else {
                        args[refExpr.parameterIndex + 1] = leftReg;
                    }
                }
            }
        }
    }
    // remove the link between itself
    for (Reg reg : regs) {
        reg.excludes.remove(reg);
        reg.prefers.remove(reg);
    }
    return args;
}
Also used : SimpleLiveValue(com.googlecode.dex2jar.ir.ts.an.SimpleLiveValue) Value(com.googlecode.dex2jar.ir.expr.Value) SimpleLiveValue(com.googlecode.dex2jar.ir.ts.an.SimpleLiveValue) Local(com.googlecode.dex2jar.ir.expr.Local) RefExpr(com.googlecode.dex2jar.ir.expr.RefExpr) Stmt(com.googlecode.dex2jar.ir.stmt.Stmt)

Example 4 with Value

use of com.googlecode.dex2jar.ir.expr.Value in project dex2jar by pxb1988.

the class StmtSearcher method travel.

public void travel(Stmt stmt) {
    switch(stmt.et) {
        case E0:
            break;
        case E1:
            travel(stmt.getOp());
            break;
        case E2:
            travel(stmt.getOp1());
            travel(stmt.getOp2());
            break;
        case En:
            Value[] ops = stmt.getOps();
            for (Value op : ops) {
                travel(op);
            }
            break;
    }
}
Also used : Value(com.googlecode.dex2jar.ir.expr.Value)

Example 5 with Value

use of com.googlecode.dex2jar.ir.expr.Value in project dex2jar by pxb1988.

the class AggTransformer method localCanExecFirst.

/**
     * dfs searching, if local is appear before first location-insensitive value, throws SUCCESS, or throws FAIL
     */
private static void localCanExecFirst(Local local, Value op) throws MergeResult {
    switch(op.et) {
        case E0:
            if (local.vt == Value.VT.LOCAL) {
                if (op == local) {
                    throw SUCCESS;
                }
            }
            break;
        case E1:
            localCanExecFirst(local, op.getOp());
            break;
        case E2:
            localCanExecFirst(local, op.getOp1());
            localCanExecFirst(local, op.getOp2());
            break;
        case En:
            for (Value v : op.getOps()) {
                localCanExecFirst(local, v);
            }
    }
    boolean shouldExclude = false;
    if (op.vt == Value.VT.INVOKE_STATIC) {
        InvokeExpr ie = (InvokeExpr) op;
        if (ie.name.equals("valueOf") && ie.owner.startsWith("Ljava/lang/") && ie.args.length == 1 && ie.args[0].length() == 1) {
            shouldExclude = true;
        }
    }
    if (!isLocationInsensitive(op.vt) && !shouldExclude) {
        // this is the first insensitive Value
        throw FAIL;
    }
}
Also used : InvokeExpr(com.googlecode.dex2jar.ir.expr.InvokeExpr) Value(com.googlecode.dex2jar.ir.expr.Value)

Aggregations

Value (com.googlecode.dex2jar.ir.expr.Value)32 Local (com.googlecode.dex2jar.ir.expr.Local)19 AssignStmt (com.googlecode.dex2jar.ir.stmt.AssignStmt)11 LabelStmt (com.googlecode.dex2jar.ir.stmt.LabelStmt)9 Stmt (com.googlecode.dex2jar.ir.stmt.Stmt)8 Constant (com.googlecode.dex2jar.ir.expr.Constant)5 Exprs.nArrayValue (com.googlecode.dex2jar.ir.expr.Exprs.nArrayValue)4 ArrayList (java.util.ArrayList)4 AnalyzeValue (com.googlecode.dex2jar.ir.ts.an.AnalyzeValue)3 PhiExpr (com.googlecode.dex2jar.ir.expr.PhiExpr)2 TravelCallBack (com.googlecode.dex2jar.ir.ts.Cfg.TravelCallBack)2 DexLabel (com.googlecode.d2j.DexLabel)1 DexType (com.googlecode.d2j.DexType)1 Field (com.googlecode.d2j.Field)1 Method (com.googlecode.d2j.Method)1 DvmInterpreter (com.googlecode.d2j.node.analysis.DvmInterpreter)1 Op (com.googlecode.d2j.reader.Op)1 IrMethod (com.googlecode.dex2jar.ir.IrMethod)1 LocalVar (com.googlecode.dex2jar.ir.LocalVar)1 StmtSearcher (com.googlecode.dex2jar.ir.StmtSearcher)1