Search in sources :

Example 1 with ListenableDirectedGraph

use of org.jgrapht.graph.ListenableDirectedGraph 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 2 with ListenableDirectedGraph

use of org.jgrapht.graph.ListenableDirectedGraph 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)

Aggregations

InstructionHandle (org.apache.bcel.generic.InstructionHandle)2 IntermediateEdge (org.candle.decompiler.intermediate.graph.edge.IntermediateEdge)2 ListenableDirectedGraph (org.jgrapht.graph.ListenableDirectedGraph)2 HashMap (java.util.HashMap)1 BranchHandle (org.apache.bcel.generic.BranchHandle)1 Select (org.apache.bcel.generic.Select)1 UnconditionalBranch (org.apache.bcel.generic.UnconditionalBranch)1 AbstractIntermediate (org.candle.decompiler.intermediate.code.AbstractIntermediate)1 IntermediateGraphContext (org.candle.decompiler.intermediate.graph.context.IntermediateGraphContext)1