Search in sources :

Example 6 with PhiListAttr

use of jadx.core.dex.attributes.nodes.PhiListAttr in project jadx by skylot.

the class SSATransform method addPhi.

public static PhiInsn addPhi(MethodNode mth, BlockNode block, int regNum) {
    PhiListAttr phiList = block.get(AType.PHI_LIST);
    if (phiList == null) {
        phiList = new PhiListAttr();
        block.addAttr(phiList);
    }
    int size = block.getPredecessors().size();
    if (mth.getEnterBlock() == block) {
        RegisterArg thisArg = mth.getThisArg();
        if (thisArg != null && thisArg.getRegNum() == regNum) {
            size++;
        } else {
            for (RegisterArg arg : mth.getArgRegs()) {
                if (arg.getRegNum() == regNum) {
                    size++;
                    break;
                }
            }
        }
    }
    PhiInsn phiInsn = new PhiInsn(regNum, size);
    phiList.getList().add(phiInsn);
    phiInsn.setOffset(block.getStartOffset());
    block.getInstructions().add(0, phiInsn);
    return phiInsn;
}
Also used : RegisterArg(jadx.core.dex.instructions.args.RegisterArg) PhiInsn(jadx.core.dex.instructions.PhiInsn) PhiListAttr(jadx.core.dex.attributes.nodes.PhiListAttr)

Example 7 with PhiListAttr

use of jadx.core.dex.attributes.nodes.PhiListAttr in project jadx by skylot.

the class SSATransform method fixUselessPhi.

private static boolean fixUselessPhi(MethodNode mth) {
    boolean changed = false;
    List<PhiInsn> insnToRemove = new ArrayList<>();
    for (SSAVar var : mth.getSVars()) {
        // phi result not used
        if (var.getUseCount() == 0) {
            InsnNode assignInsn = var.getAssign().getParentInsn();
            if (assignInsn != null && assignInsn.getType() == InsnType.PHI) {
                insnToRemove.add((PhiInsn) assignInsn);
                changed = true;
            }
        }
    }
    for (BlockNode block : mth.getBasicBlocks()) {
        PhiListAttr phiList = block.get(AType.PHI_LIST);
        if (phiList == null) {
            continue;
        }
        Iterator<PhiInsn> it = phiList.getList().iterator();
        while (it.hasNext()) {
            PhiInsn phi = it.next();
            if (fixPhiWithSameArgs(mth, block, phi)) {
                it.remove();
                changed = true;
            }
        }
    }
    removePhiList(mth, insnToRemove);
    return changed;
}
Also used : BlockNode(jadx.core.dex.nodes.BlockNode) InsnNode(jadx.core.dex.nodes.InsnNode) PhiInsn(jadx.core.dex.instructions.PhiInsn) SSAVar(jadx.core.dex.instructions.args.SSAVar) PhiListAttr(jadx.core.dex.attributes.nodes.PhiListAttr) ArrayList(java.util.ArrayList)

Example 8 with PhiListAttr

use of jadx.core.dex.attributes.nodes.PhiListAttr in project jadx by skylot.

the class SSATransform method renameVarsInBlock.

private static void renameVarsInBlock(MethodNode mth, RenameState state) {
    BlockNode block = state.getBlock();
    for (InsnNode insn : block.getInstructions()) {
        if (insn.getType() != InsnType.PHI) {
            for (InsnArg arg : insn.getArguments()) {
                if (!arg.isRegister()) {
                    continue;
                }
                RegisterArg reg = (RegisterArg) arg;
                int regNum = reg.getRegNum();
                SSAVar var = state.getVar(regNum);
                if (var == null) {
                    // TODO: in most cases issue in incorrectly attached exception handlers
                    mth.addWarnComment("Not initialized variable reg: " + regNum + ", insn: " + insn + ", block:" + block);
                    var = state.startVar(reg);
                }
                var.use(reg);
            }
        }
        RegisterArg result = insn.getResult();
        if (result != null) {
            state.startVar(result);
        }
    }
    for (BlockNode s : block.getSuccessors()) {
        PhiListAttr phiList = s.get(AType.PHI_LIST);
        if (phiList == null) {
            continue;
        }
        for (PhiInsn phiInsn : phiList.getList()) {
            bindPhiArg(state, phiInsn);
        }
    }
}
Also used : BlockNode(jadx.core.dex.nodes.BlockNode) InsnNode(jadx.core.dex.nodes.InsnNode) RegisterArg(jadx.core.dex.instructions.args.RegisterArg) SSAVar(jadx.core.dex.instructions.args.SSAVar) PhiInsn(jadx.core.dex.instructions.PhiInsn) InsnArg(jadx.core.dex.instructions.args.InsnArg) PhiListAttr(jadx.core.dex.attributes.nodes.PhiListAttr)

Example 9 with PhiListAttr

use of jadx.core.dex.attributes.nodes.PhiListAttr in project jadx by skylot.

the class SSATransform method removeBlockerInsns.

private static boolean removeBlockerInsns(MethodNode mth) {
    boolean removed = false;
    for (BlockNode block : mth.getBasicBlocks()) {
        PhiListAttr phiList = block.get(AType.PHI_LIST);
        if (phiList == null) {
            continue;
        }
        // check if args must be removed
        for (PhiInsn phi : phiList.getList()) {
            for (int i = 0; i < phi.getArgsCount(); i++) {
                RegisterArg arg = phi.getArg(i);
                InsnNode parentInsn = arg.getAssignInsn();
                if (parentInsn != null && parentInsn.contains(AFlag.REMOVE)) {
                    phi.removeArg(arg);
                    InsnRemover.remove(mth, block, parentInsn);
                    removed = true;
                }
            }
        }
    }
    return removed;
}
Also used : BlockNode(jadx.core.dex.nodes.BlockNode) InsnNode(jadx.core.dex.nodes.InsnNode) RegisterArg(jadx.core.dex.instructions.args.RegisterArg) PhiInsn(jadx.core.dex.instructions.PhiInsn) PhiListAttr(jadx.core.dex.attributes.nodes.PhiListAttr)

Example 10 with PhiListAttr

use of jadx.core.dex.attributes.nodes.PhiListAttr in project jadx by skylot.

the class SSATransform method initPhiInEnterBlock.

private static void initPhiInEnterBlock(SSAVar[] vars, BlockNode enterBlock) {
    PhiListAttr phiList = enterBlock.get(AType.PHI_LIST);
    if (phiList != null) {
        for (PhiInsn phiInsn : phiList.getList()) {
            int regNum = phiInsn.getResult().getRegNum();
            SSAVar var = vars[regNum];
            if (var == null) {
                continue;
            }
            RegisterArg arg = phiInsn.bindArg(enterBlock);
            var.use(arg);
            var.setUsedInPhi(phiInsn);
        }
    }
}
Also used : RegisterArg(jadx.core.dex.instructions.args.RegisterArg) PhiInsn(jadx.core.dex.instructions.PhiInsn) SSAVar(jadx.core.dex.instructions.args.SSAVar) PhiListAttr(jadx.core.dex.attributes.nodes.PhiListAttr)

Aggregations

PhiListAttr (jadx.core.dex.attributes.nodes.PhiListAttr)11 PhiInsn (jadx.core.dex.instructions.PhiInsn)11 BlockNode (jadx.core.dex.nodes.BlockNode)9 RegisterArg (jadx.core.dex.instructions.args.RegisterArg)8 SSAVar (jadx.core.dex.instructions.args.SSAVar)7 InsnNode (jadx.core.dex.nodes.InsnNode)6 InsnArg (jadx.core.dex.instructions.args.InsnArg)5 JadxRuntimeException (jadx.core.utils.exceptions.JadxRuntimeException)3 ArrayList (java.util.ArrayList)3