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);
}
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;
}
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);
}
}
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);
}
Aggregations