Search in sources :

Example 11 with PhiInsn

use of jadx.core.dex.instructions.PhiInsn in project jadx by skylot.

the class RegisterArg method getPhiAssignInsn.

public InsnNode getPhiAssignInsn() {
    PhiInsn usePhi = sVar.getUsedInPhi();
    if (usePhi != null) {
        return usePhi;
    }
    InsnNode parent = sVar.getAssign().getParentInsn();
    if (parent != null && parent.getType() == InsnType.PHI) {
        return parent;
    }
    return null;
}
Also used : InsnNode(jadx.core.dex.nodes.InsnNode) PhiInsn(jadx.core.dex.instructions.PhiInsn)

Example 12 with PhiInsn

use of jadx.core.dex.instructions.PhiInsn in project jadx by skylot.

the class PhiListAttr method toString.

@Override
public String toString() {
    StringBuilder sb = new StringBuilder();
    sb.append("PHI: ");
    for (PhiInsn phiInsn : list) {
        sb.append('r').append(phiInsn.getResult().getRegNum()).append(" ");
    }
    return sb.toString();
}
Also used : PhiInsn(jadx.core.dex.instructions.PhiInsn)

Example 13 with PhiInsn

use of jadx.core.dex.instructions.PhiInsn in project jadx by skylot.

the class TypeInference method visit.

@Override
public void visit(MethodNode mth) throws JadxException {
    if (mth.isNoCode()) {
        return;
    }
    DexNode dex = mth.dex();
    for (SSAVar var : mth.getSVars()) {
        // inference variable type
        ArgType type = processType(dex, var);
        if (type == null) {
            type = ArgType.UNKNOWN;
        }
        var.setType(type);
        // search variable name
        String name = processVarName(var);
        var.setName(name);
    }
    // fix type for vars used only in Phi nodes
    for (SSAVar sVar : mth.getSVars()) {
        PhiInsn phi = sVar.getUsedInPhi();
        if (phi != null) {
            processPhiNode(phi);
        }
    }
}
Also used : ArgType(jadx.core.dex.instructions.args.ArgType) SSAVar(jadx.core.dex.instructions.args.SSAVar) PhiInsn(jadx.core.dex.instructions.PhiInsn) DexNode(jadx.core.dex.nodes.DexNode)

Example 14 with PhiInsn

use of jadx.core.dex.instructions.PhiInsn in project jadx by skylot.

the class EliminatePhiNodes method replaceMerge.

/**
	 * Replace 'MERGE' with 'PHI' insn.
	 */
private void replaceMerge(MethodNode mth, BlockNode block, InsnNode insn) {
    if (insn.getArgsCount() != 2) {
        throw new JadxRuntimeException("Unexpected count of arguments in merge insn: " + insn);
    }
    RegisterArg oldArg = (RegisterArg) insn.getArg(1);
    RegisterArg newArg = (RegisterArg) insn.getArg(0);
    int newRegNum = newArg.getRegNum();
    if (oldArg.getRegNum() == newRegNum) {
        throw new JadxRuntimeException("Unexpected register number in merge insn: " + insn);
    }
    SSAVar oldSVar = oldArg.getSVar();
    RegisterArg assignArg = oldSVar.getAssign();
    InsnNode assignParentInsn = assignArg.getParentInsn();
    BlockNode assignBlock = BlockUtils.getBlockByInsn(mth, assignParentInsn);
    if (assignBlock == null) {
        throw new JadxRuntimeException("Unknown assign block for " + assignParentInsn);
    }
    BlockNode assignPred = null;
    for (BlockNode pred : block.getPredecessors()) {
        if (BlockUtils.isPathExists(assignBlock, pred)) {
            assignPred = pred;
            break;
        }
    }
    if (assignPred == null) {
        throw new JadxRuntimeException("Assign predecessor not found for " + assignBlock + " from " + block);
    }
    // all checks passed
    RegisterArg newAssignArg = oldArg.duplicate(newRegNum, null);
    SSAVar newSVar = mth.makeNewSVar(newRegNum, mth.getNextSVarVersion(newRegNum), newAssignArg);
    newSVar.setName(oldSVar.getName());
    newSVar.setType(assignArg.getType());
    if (assignParentInsn != null) {
        assignParentInsn.setResult(newAssignArg);
    }
    for (RegisterArg useArg : oldSVar.getUseList()) {
        RegisterArg newUseArg = useArg.duplicate(newRegNum, newSVar);
        InsnNode parentInsn = useArg.getParentInsn();
        if (parentInsn != null) {
            newSVar.use(newUseArg);
            parentInsn.replaceArg(useArg, newUseArg);
        }
    }
    block.getInstructions().remove(0);
    PhiInsn phiInsn = SSATransform.addPhi(mth, block, newRegNum);
    phiInsn.setResult(insn.getResult());
    phiInsn.bindArg(newAssignArg.duplicate(), assignPred);
    phiInsn.bindArg(newArg.duplicate(), BlockUtils.selectOtherSafe(assignPred, block.getPredecessors()));
}
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) JadxRuntimeException(jadx.core.utils.exceptions.JadxRuntimeException)

Example 15 with PhiInsn

use of jadx.core.dex.instructions.PhiInsn 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

PhiInsn (jadx.core.dex.instructions.PhiInsn)16 RegisterArg (jadx.core.dex.instructions.args.RegisterArg)11 InsnNode (jadx.core.dex.nodes.InsnNode)10 PhiListAttr (jadx.core.dex.attributes.nodes.PhiListAttr)8 BlockNode (jadx.core.dex.nodes.BlockNode)8 SSAVar (jadx.core.dex.instructions.args.SSAVar)7 InsnArg (jadx.core.dex.instructions.args.InsnArg)4 JadxRuntimeException (jadx.core.utils.exceptions.JadxRuntimeException)3 ArrayList (java.util.ArrayList)2 ArgType (jadx.core.dex.instructions.args.ArgType)1 TernaryInsn (jadx.core.dex.instructions.mods.TernaryInsn)1 DexNode (jadx.core.dex.nodes.DexNode)1 IContainer (jadx.core.dex.nodes.IContainer)1 ForLoop (jadx.core.dex.regions.loops.ForLoop)1 LoopType (jadx.core.dex.regions.loops.LoopType)1 HashMap (java.util.HashMap)1 LinkedList (java.util.LinkedList)1 Map (java.util.Map)1