From 7bf471eec3d35936cc0e5d56b680614d6ebdd173 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?David=20Srbeck=C3=BD?= Date: Sat, 26 Jan 2008 12:22:34 +0000 Subject: [PATCH] Some set operations that will be need for 'if' simplification --- src/ControlFlow/Node.cs | 52 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 52 insertions(+) diff --git a/src/ControlFlow/Node.cs b/src/ControlFlow/Node.cs index d65b6b601..2f9d9aba2 100644 --- a/src/ControlFlow/Node.cs +++ b/src/ControlFlow/Node.cs @@ -13,6 +13,24 @@ namespace Decompiler.ControlFlow } } + public void RemoveRange(IEnumerable items) + { + foreach(Node item in items) { + this.Remove(item); + } + } + + public static NodeCollection Intersection(NodeCollection collectionA, NodeCollection collectionB) + { + NodeCollection result = new NodeCollection(); + foreach(Node a in collectionA) { + if (collectionB.Contains(a)) { + result.Add(a); + } + } + return result; + } + protected override void InsertItem(int index, Node item) { if (!this.Contains(item)) { @@ -106,6 +124,23 @@ namespace Decompiler.ControlFlow this.id = NextNodeID++; } + NodeCollection GetReachableNodes() + { + NodeCollection accumulator = new NodeCollection(); + AddReachableNode(accumulator, this); + return accumulator; + } + + static void AddReachableNode(NodeCollection accumulator, Node node) + { + if (!accumulator.Contains(node)) { + accumulator.Add(node); + foreach(Node successor in node.Successors) { + AddReachableNode(successor, accumulator); + } + } + } + void GetBasicBlockSuccessors(NodeCollection accumulator) { BasicBlock me = this as BasicBlock; @@ -164,6 +199,23 @@ namespace Decompiler.ControlFlow this.childs = this.Childs[0].Childs; this.UpdateParentOfChilds(); } + OptimizeIf(); + } + + public void OptimizeIf() + { + Node conditionNode = this.HeadChild; + // Find conditionNode (the start) + while(true) { + if (conditionNode is BasicBlock && conditionNode.Successors.Count == 2) { + break; // Found + } else if (conditionNode.Successors == 1) { + conditionNode = conditionNode.Successors[0]; + continue; // Next + } else { + return; + } + } } void UpdateParentOfChilds()