Search in sources :

Example 36 with InstructionHandle

use of org.apache.bcel.generic.InstructionHandle in project candle-decompiler by bradsdavis.

the class IntermediateGraphFactory method visitMultiBranchIntermediate.

@Override
public void visitMultiBranchIntermediate(MultiBranchIntermediate line) {
    Select select = (Select) line.getInstruction().getInstruction();
    Set<Case> cases = new HashSet<Case>();
    InstructionHandle[] handles = select.getTargets();
    int[] matches = select.getMatchs();
    for (int i = 0, j = handles.length; i < j; i++) {
        InstructionHandle ih = handles[i];
        int match = matches[i];
        Resolved resolved = new Resolved(line.getInstruction(), BasicType.INT, "" + match);
        Case caseEntry = new Case(line.getInstruction(), ih, resolved);
        cases.add(caseEntry);
    }
    if (select.getTarget() != null) {
        DefaultCase defaultCase = new DefaultCase(line.getInstruction(), select.getTarget());
        line.setDefaultCase(defaultCase);
    }
    //now, create the graph.
    line.setCases(cases);
    //now, create the graph.
    igc.getGraph().addVertex(line);
    if (line.getDefaultCase() != null) {
        CaseIntermediate si = new CaseIntermediate(line.getInstruction(), line.getDefaultCase());
        igc.getGraph().addVertex(si);
        //add an edge.
        igc.getGraph().addEdge(line, si);
        //add edge from outcome to edge.
        LOG.debug(si);
        AbstractIntermediate target = ilc.getNext(line.getDefaultCase().getTarget().getPosition());
        LOG.debug("TargeT:" + target);
        igc.getGraph().addVertex(target);
        igc.getGraph().addEdge(si, target);
    }
    for (Case caseVal : line.getCases()) {
        CaseIntermediate si = new CaseIntermediate(line.getInstruction(), caseVal);
        igc.getGraph().addVertex(si);
        //add an edge.
        igc.getGraph().addEdge(line, si);
        //add edge from outcome to edge.
        AbstractIntermediate target = ilc.getNext(caseVal.getTarget().getPosition());
        igc.getGraph().addVertex(target);
        igc.getGraph().addEdge(si, target);
    }
}
Also used : AbstractIntermediate(org.candle.decompiler.intermediate.code.AbstractIntermediate) DefaultCase(org.candle.decompiler.intermediate.expression.DefaultCase) Select(org.apache.bcel.generic.Select) CaseIntermediate(org.candle.decompiler.intermediate.code.CaseIntermediate) Resolved(org.candle.decompiler.intermediate.expression.Resolved) InstructionHandle(org.apache.bcel.generic.InstructionHandle) HashSet(java.util.HashSet) Case(org.candle.decompiler.intermediate.expression.Case) DefaultCase(org.candle.decompiler.intermediate.expression.DefaultCase)

Example 37 with InstructionHandle

use of org.apache.bcel.generic.InstructionHandle in project candle-decompiler by bradsdavis.

the class IntermediateLineContext method getNext.

public AbstractIntermediate getNext(AbstractIntermediate ai) {
    if (ai instanceof GoToIntermediate) {
        BranchHandle bh = (BranchHandle) ai.getInstruction();
        Integer position = bh.getTarget().getPosition();
        return getNext(position);
    }
    //otherwise, get it from the next position.
    InstructionHandle next = ai.getInstruction().getNext();
    //return either null, if next is null (last next).  Otherwise, send next position.
    return next == null ? null : getNext(next.getPosition());
}
Also used : GoToIntermediate(org.candle.decompiler.intermediate.code.GoToIntermediate) BranchHandle(org.apache.bcel.generic.BranchHandle) InstructionHandle(org.apache.bcel.generic.InstructionHandle)

Example 38 with InstructionHandle

use of org.apache.bcel.generic.InstructionHandle in project candle-decompiler by bradsdavis.

the class RetractDuplicateFinally method processTry.

protected void processTry(TryIntermediate tryIntermediate, FinallyIntermediate finallyIntermediate, Set<Integer> offsets) {
    //ok, now let's handle the try...
    InstructionHandle end = tryIntermediate.getBlockRange().getEnd();
    //next should be GOTO.
    AbstractIntermediate tryEndNode = igc.findNextNode(end);
    AbstractIntermediate gotoIntermediate = null;
    //check to see if this is loop...
    if (tryEndNode instanceof StatementIntermediate) {
        LOG.debug("Position: " + tryEndNode.getInstruction().getPosition() + " Value: " + tryEndNode);
        gotoIntermediate = igc.getSingleSuccessor(tryEndNode);
    } else if (tryEndNode instanceof BooleanBranchIntermediate) {
        BooleanBranchIntermediate bbi = (BooleanBranchIntermediate) tryEndNode;
        //find higher target...
        AbstractIntermediate trueTarget = igc.getTrueTarget(bbi);
        AbstractIntermediate falseTarget = igc.getFalseTarget(bbi);
        int trueTargetPosition = trueTarget.getInstruction().getPosition();
        int falseTargetPosition = falseTarget.getInstruction().getPosition();
        gotoIntermediate = (trueTargetPosition > falseTargetPosition) ? trueTarget : falseTarget;
    }
    //validate it is a GOTO.
    if (!(gotoIntermediate instanceof GoToIntermediate)) {
        LOG.warn("Expected GOTO.  But this isn't: " + gotoIntermediate);
    } else {
        AbstractIntermediate tryFinallyFirst = igc.getSingleSuccessor(gotoIntermediate);
        eliminateNode(tryFinallyFirst, offsets);
        //now, eliminate the GOTO.
        igc.getGraph().removeVertex(gotoIntermediate);
        igc.getGraph().addEdge(tryIntermediate, finallyIntermediate);
    }
}
Also used : AbstractIntermediate(org.candle.decompiler.intermediate.code.AbstractIntermediate) GoToIntermediate(org.candle.decompiler.intermediate.code.GoToIntermediate) StatementIntermediate(org.candle.decompiler.intermediate.code.StatementIntermediate) BooleanBranchIntermediate(org.candle.decompiler.intermediate.code.BooleanBranchIntermediate) InstructionHandle(org.apache.bcel.generic.InstructionHandle)

Example 39 with InstructionHandle

use of org.apache.bcel.generic.InstructionHandle in project candle-decompiler by bradsdavis.

the class RetractDuplicateFinally method eliminateNode.

protected void eliminateNode(AbstractIntermediate first, Set<Integer> offsets) {
    InstructionHandle ih = first.getInstruction();
    int position = first.getInstruction().getPosition();
    for (Integer offset : offsets) {
        int target = position + offset;
        ih = igc.getInstructionHandle(target);
        if (ih == null) {
            LOG.warn("Not found expected InstructionHandle: " + (position + offset));
            continue;
        }
        //loop through...
        AbstractIntermediate ai = igc.findNextNode(ih);
        if (ai.getInstruction().getPosition() == target) {
            igc.getGraph().removeVertex(ai);
        } else {
            LOG.warn("Did not find vertex: " + target);
        }
    }
}
Also used : AbstractIntermediate(org.candle.decompiler.intermediate.code.AbstractIntermediate) InstructionHandle(org.apache.bcel.generic.InstructionHandle)

Example 40 with InstructionHandle

use of org.apache.bcel.generic.InstructionHandle in project candle-decompiler by bradsdavis.

the class RetractDuplicateFinally method visitFinallyIntermediate.

@Override
public void visitFinallyIntermediate(FinallyIntermediate line) {
    //TODO: Remove the highest finally statements first; won't fit this visitor
    //pattern.
    //get the bounds of the finally... associate the try.
    //finally is part of the try if the try + catch bounds has:
    //Try[0,13]
    //Catch[19,50]
    //Catch[56,65]
    //Finally | Handler[56, 65]| Handler[0, 42] |  | Range[66, 76]
    //in the case above, finally should match because lower bound == finally lower handler bound.
    //and upper bound of Catch matches upper bound of Finally Handler.
    //first, match the Try block that applies...
    //get lowest bound.
    InstructionHandle min = getLowestBound(line.getCodeExceptions());
    InstructionHandle max = getHighestBound(line.getCodeExceptions());
    LOG.debug("Finally Range: " + min.getPosition() + " -> " + max.getPosition());
    TryIntermediate matched = matchTryBlock(min, max);
    if (matched != null) {
        final Set<Integer> offsets = collectOffsets(line);
        //ok, now we need to eliminate finally blocks from this try and all of the catches.  thanks Java!
        List<CatchIntermediate> catchClauses = igc.getCatchClauses(matched);
        //for each catch clause...
        for (CatchIntermediate catchClause : catchClauses) {
            processCatch(catchClause, line, offsets);
        }
        processTry(matched, line, offsets);
    }
    //now, add the edge between end of FINALLY and the next statement.
    InstructionHandle finallyEnd = line.getBlockRange().getEnd();
    //get the next.. then search for next node in graph.
    AbstractIntermediate finallyLast = igc.findNextNode(finallyEnd);
    AbstractIntermediate afterFinally = igc.findNextNode(finallyEnd.getNext());
    igc.redirectPredecessors(finallyLast, afterFinally);
    igc.getGraph().removeVertex(finallyLast);
}
Also used : AbstractIntermediate(org.candle.decompiler.intermediate.code.AbstractIntermediate) CatchIntermediate(org.candle.decompiler.intermediate.code.CatchIntermediate) TryIntermediate(org.candle.decompiler.intermediate.code.TryIntermediate) InstructionHandle(org.apache.bcel.generic.InstructionHandle)

Aggregations

InstructionHandle (org.apache.bcel.generic.InstructionHandle)103 InstructionList (org.apache.bcel.generic.InstructionList)26 MethodInfo (com.jopdesign.common.MethodInfo)20 CallString (com.jopdesign.common.code.CallString)20 Instruction (org.apache.bcel.generic.Instruction)13 MethodCode (com.jopdesign.common.MethodCode)12 ContextMap (com.jopdesign.dfa.framework.ContextMap)11 IntermediateEdge (org.candle.decompiler.intermediate.graph.edge.IntermediateEdge)11 Context (com.jopdesign.dfa.framework.Context)10 InvokeInstruction (org.apache.bcel.generic.InvokeInstruction)10 Iterator (java.util.Iterator)9 BranchInstruction (org.apache.bcel.generic.BranchInstruction)9 FieldInstruction (org.apache.bcel.generic.FieldInstruction)9 AbstractIntermediate (org.candle.decompiler.intermediate.code.AbstractIntermediate)9 ReturnInstruction (org.apache.bcel.generic.ReturnInstruction)8 InstructionFinder (org.apache.bcel.util.InstructionFinder)8 ClassInfo (com.jopdesign.common.ClassInfo)7 HashMap (java.util.HashMap)7 HashSet (java.util.HashSet)7 LinkedList (java.util.LinkedList)7