Search in sources :

Example 16 with IntermediateEdge

use of org.candle.decompiler.intermediate.graph.edge.IntermediateEdge in project candle-decompiler by bradsdavis.

the class IntermediateGraphTransformer method getIntermediateGraphContext.

public IntermediateGraphContext getIntermediateGraphContext() {
    Map<InstructionHandle, AbstractIntermediate> nodeMapping = new HashMap<InstructionHandle, AbstractIntermediate>();
    ListenableDirectedGraph<AbstractIntermediate, IntermediateEdge> intermediateGraph = new ListenableDirectedGraph<AbstractIntermediate, IntermediateEdge>(IntermediateEdge.class);
    // walk the instruction graph and generate the intermediate graph.  start by walking and adding all vertices.  Then, add the connections between the vertices.
    for (InstructionHandle ih : igc.getGraph().vertexSet()) {
        if (igc.hasIntermediate(ih)) {
            AbstractIntermediate vertex = igc.getIntermediateFromInstruction(ih);
            intermediateGraph.addVertex(vertex);
            nodeMapping.put(ih, vertex);
        }
    }
    // now, add the links.
    for (InstructionHandle ih : igc.getGraph().vertexSet()) {
        AbstractIntermediate nodeIntermediate = nodeMapping.get(ih);
        if (nodeIntermediate == null) {
            LOG.warn("This shouldn't be...");
            continue;
        }
        List<InstructionHandle> predecessors = igc.getPredecessors(ih);
        List<InstructionHandle> successors = igc.getSuccessors(ih);
        for (InstructionHandle predecessor : predecessors) {
            // find it's AbstractIntermediate.
            AbstractIntermediate predIntermediate = nodeMapping.get(predecessor);
            if (predIntermediate == null) {
                // then something is wrong.
                LOG.warn("This shouldn't be...");
                continue;
            }
            IntermediateEdge insEdge = igc.getGraph().getEdge(predecessor, ih);
            // add an edge to the intermediate graph.
            if (intermediateGraph.containsEdge(predIntermediate, nodeIntermediate)) {
                continue;
            }
            intermediateGraph.addEdge(predIntermediate, nodeIntermediate, (IntermediateEdge) insEdge.clone());
        }
        for (InstructionHandle successor : successors) {
            // find it's AbstractIntermediate.
            AbstractIntermediate successorIntermediate = nodeMapping.get(successor);
            if (successorIntermediate == null) {
                LOG.warn("This shouldn't be...");
                continue;
            }
            if (intermediateGraph.containsEdge(nodeIntermediate, successorIntermediate)) {
                continue;
            }
            IntermediateEdge insEdge = igc.getGraph().getEdge(ih, successor);
            // add an edge to the intermediate graph.
            intermediateGraph.addEdge(nodeIntermediate, successorIntermediate, (IntermediateEdge) insEdge.clone());
        }
    }
    return new IntermediateGraphContext(intermediateGraph);
}
Also used : AbstractIntermediate(org.candle.decompiler.intermediate.code.AbstractIntermediate) HashMap(java.util.HashMap) IntermediateGraphContext(org.candle.decompiler.intermediate.graph.context.IntermediateGraphContext) ListenableDirectedGraph(org.jgrapht.graph.ListenableDirectedGraph) IntermediateEdge(org.candle.decompiler.intermediate.graph.edge.IntermediateEdge) InstructionHandle(org.apache.bcel.generic.InstructionHandle)

Example 17 with IntermediateEdge

use of org.candle.decompiler.intermediate.graph.edge.IntermediateEdge in project candle-decompiler by bradsdavis.

the class InstructionGraphFactory method process.

public InstructionGraphContext process() {
    ListenableDirectedGraph<InstructionHandle, IntermediateEdge> instructionHandleGraph = new ListenableDirectedGraph<InstructionHandle, IntermediateEdge>(IntermediateEdge.class);
    InstructionGraphContext igc = new InstructionGraphContext(instructionHandleGraph);
    for (InstructionHandle instructionHandle : instructionList.getInstructionHandles()) {
        InstructionHandle iv = instructionHandle;
        instructionHandleGraph.addVertex(iv);
    }
    Iterator<InstructionHandle> iter = instructionList.iterator();
    while (iter.hasNext()) {
        InstructionHandle ih = iter.next();
        InstructionHandle sourceVertex = igc.getPositionMap().get(ih.getPosition());
        if (ih.getInstruction() instanceof Select) {
            for (InstructionHandle targetVertex : ((Select) ih.getInstruction()).getTargets()) {
                instructionHandleGraph.addEdge(sourceVertex, targetVertex);
            }
            InstructionHandle targetVertex = ((Select) ih.getInstruction()).getTarget();
            if (targetVertex != null) {
                instructionHandleGraph.addEdge(sourceVertex, targetVertex);
            }
        } else if (ih instanceof BranchHandle) {
            // if this is an unconditional branch, only add the branch between the instruction and it's target.
            InstructionHandle targetVertex = igc.getPositionMap().get(((BranchHandle) ih).getTarget().getPosition());
            instructionHandleGraph.addEdge(sourceVertex, targetVertex);
        }
        if (!(ih.getInstruction() instanceof UnconditionalBranch)) {
            if (ih.getNext() != null) {
                InstructionHandle targetVertex = igc.getPositionMap().get((ih).getNext().getPosition());
                instructionHandleGraph.addEdge(sourceVertex, targetVertex);
            }
        }
    }
    /*
		//HAPPY PATH
		Set<InstructionHandle> happy = new HashSet<InstructionHandle>();
		InstructionHandle root = createVertex(instructionList.getStart());
		BreadthFirstIterator<InstructionHandle, IntermediateEdge> bfi = new BreadthFirstIterator<InstructionHandle, IntermediateEdge>(instructionHandleGraph, root);
		while(bfi.hasNext()) {
			happy.add(bfi.next());
		}
		
		InstructionTryCatch itc = new InstructionTryCatch(igc, this.exceptions);
		
		
		NamedDirectedSubgraph happyPath = new NamedDirectedSubgraph("Main", instructionHandleGraph, happy, null);
		
		
		for(CodeExceptionGen ceg : exceptions) {
			Set<InstructionHandle> cegG = new HashSet<InstructionHandle>();
			System.out.println("X"+ceg.getStartPC() + " -> "+ceg.getEndPC()+ ", "+ceg.getHandlerPC());
			
			for(InstructionHandle iv : instructionHandleGraph.vertexSet()) {
				
				if(iv.getPosition() <= ceg.getEndPC().getPosition() && iv.getPosition() >= ceg.getStartPC().getPosition()) {
					System.out.println("Position : "+iv.getPosition() );
					cegG.add(iv);
				}
			}
			
			subs.add(new NamedDirectedSubgraph("ExceptionHandler"+ceg.getCatchType().toString(), instructionHandleGraph, cegG, null));
		}
		
		*/
    return igc;
}
Also used : UnconditionalBranch(org.apache.bcel.generic.UnconditionalBranch) Select(org.apache.bcel.generic.Select) BranchHandle(org.apache.bcel.generic.BranchHandle) ListenableDirectedGraph(org.jgrapht.graph.ListenableDirectedGraph) IntermediateEdge(org.candle.decompiler.intermediate.graph.edge.IntermediateEdge) InstructionHandle(org.apache.bcel.generic.InstructionHandle)

Example 18 with IntermediateEdge

use of org.candle.decompiler.intermediate.graph.edge.IntermediateEdge in project candle-decompiler by bradsdavis.

the class ExceptionEdgeEnhancer method process.

@Override
public void process() {
    Set<Integer> positions = new HashSet<Integer>();
    for (CodeExceptionGen ceg : exceptions) {
        int t1 = ceg.getStartPC().getPosition();
        int t2 = ceg.getHandlerPC().getPosition();
        positions.add(t1);
        positions.add(t2);
    }
    Map<Integer, InstructionHandle> ivc = new HashMap<Integer, InstructionHandle>();
    for (InstructionHandle iv : igc.getGraph().vertexSet()) {
        if (positions.contains(iv.getPosition())) {
            ivc.put(iv.getPosition(), iv);
        }
    }
    // now, we will map the CEG.
    for (CodeExceptionGen ceg : exceptions) {
        int t1 = ceg.getStartPC().getPosition();
        int t2 = ceg.getHandlerPC().getPosition();
        InstructionHandle source = ivc.get(t1);
        InstructionHandle target = ivc.get(t2);
        IntermediateEdge ie = new IntermediateEdge();
        addExceptionHandle(ie, ceg);
        ie.setType(EdgeType.EXCEPTION);
        igc.getGraph().addEdge(source, target, ie);
    }
}
Also used : HashMap(java.util.HashMap) CodeExceptionGen(org.apache.bcel.generic.CodeExceptionGen) IntermediateEdge(org.candle.decompiler.intermediate.graph.edge.IntermediateEdge) InstructionHandle(org.apache.bcel.generic.InstructionHandle) HashSet(java.util.HashSet)

Example 19 with IntermediateEdge

use of org.candle.decompiler.intermediate.graph.edge.IntermediateEdge in project candle-decompiler by bradsdavis.

the class LoopHeader method splitLoopHeader.

public void splitLoopHeader(InstructionHandle ih) {
    DuplicateHandle duplicate = new DuplicateHandle(ih);
    System.out.println(igc.getGraph().addVertex(duplicate));
    System.out.println(duplicate);
    List<InstructionHandle> preds = Graphs.predecessorListOf(igc.getGraph(), ih);
    for (InstructionHandle pred : preds) {
        IntermediateEdge ie = igc.getGraph().getEdge(pred, ih);
        IntermediateEdge in = igc.getGraph().addEdge(pred, duplicate);
        in.setType(ie.getType());
        // now remove the original edge.
        igc.getGraph().removeEdge(ie);
    }
    // add edge from duplicate to new.
    igc.getGraph().addEdge(duplicate, ih);
}
Also used : DuplicateHandle(org.apache.bcel.generic.DuplicateHandle) IntermediateEdge(org.candle.decompiler.intermediate.graph.edge.IntermediateEdge) InstructionHandle(org.apache.bcel.generic.InstructionHandle)

Aggregations

IntermediateEdge (org.candle.decompiler.intermediate.graph.edge.IntermediateEdge)19 InstructionHandle (org.apache.bcel.generic.InstructionHandle)11 AbstractIntermediate (org.candle.decompiler.intermediate.code.AbstractIntermediate)6 File (java.io.File)3 FileWriter (java.io.FileWriter)3 IOException (java.io.IOException)3 Writer (java.io.Writer)3 TreeSet (java.util.TreeSet)3 BranchHandle (org.apache.bcel.generic.BranchHandle)3 InstructionEdgeAttributeProvider (org.candle.decompiler.instruction.graph.edge.InstructionEdgeAttributeProvider)3 IntermediateEdgeProvider (org.candle.decompiler.intermediate.graph.edge.IntermediateEdgeProvider)3 DOTExporter (org.jgrapht.ext.DOTExporter)3 HashMap (java.util.HashMap)2 HashSet (java.util.HashSet)2 InstructionLabelProvider (org.candle.decompiler.instruction.graph.vertex.InstructionLabelProvider)2 GoToIntermediate (org.candle.decompiler.intermediate.code.GoToIntermediate)2 StatementIntermediate (org.candle.decompiler.intermediate.code.StatementIntermediate)2 ListenableDirectedGraph (org.jgrapht.graph.ListenableDirectedGraph)2 BreadthFirstIterator (org.jgrapht.traverse.BreadthFirstIterator)2 CodeExceptionGen (org.apache.bcel.generic.CodeExceptionGen)1