Ejemplo n.º 1
0
 private void DFSBuild(DFSTNode currentNode)
 {
     dummyVar0 = this.traversedNodes.Add(currentNode);
     dummyVar1 = this.currentPath.Add(currentNode);
     V_0       = LogicalFlowUtilities.GetTraversableSuccessors(currentNode.get_Construct()).GetEnumerator();
     try
     {
         while (V_0.MoveNext())
         {
             V_1 = V_0.get_Current();
             if (!this.constructToNodeMap.TryGetValue(V_1, out V_2))
             {
                 continue;
             }
             if (this.traversedNodes.Contains(V_2))
             {
                 if (!this.currentPath.Contains(V_2))
                 {
                     if (!this.IsAncestor(V_2, currentNode))
                     {
                         dummyVar10 = V_2.get_CrossEdgePredecessors().Add(currentNode);
                         dummyVar11 = currentNode.get_CrossEdgeSuccessors().Add(V_2);
                         dummyVar12 = this.theTree.get_CrossEdges().Add(new DFSTEdge(currentNode, V_2));
                     }
                     else
                     {
                         dummyVar7 = V_2.get_ForwardEdgePredecessors().Add(currentNode);
                         dummyVar8 = currentNode.get_ForwardEdgeSucessors().Add(V_2);
                         dummyVar9 = this.theTree.get_ForwardEdges().Add(new DFSTEdge(currentNode, V_2));
                     }
                 }
                 else
                 {
                     dummyVar4 = V_2.get_BackEdgePredecessors().Add(currentNode);
                     dummyVar5 = currentNode.get_BackEdgeSuccessors().Add(V_2);
                     dummyVar6 = this.theTree.get_BackEdges().Add(new DFSTEdge(currentNode, V_2));
                 }
             }
             else
             {
                 V_2.set_Predecessor(currentNode);
                 dummyVar2 = currentNode.get_TreeEdgeSuccessors().Add(V_2);
                 dummyVar3 = this.theTree.get_TreeEdges().Add(new DFSTEdge(currentNode, V_2));
                 this.DFSBuild(V_2);
             }
         }
     }
     finally
     {
         if (V_0 != null)
         {
             V_0.Dispose();
         }
     }
     dummyVar13 = this.currentPath.Remove(currentNode);
     this.theTree.get_ReversePostOrder().Add(currentNode);
     return;
 }
Ejemplo n.º 2
0
        /// <summary>
        /// Recursively traverses the nodes and adds them to the tree. Also determines the type of each edge.
        /// </summary>
        /// <param name="currentNode"></param>
        private void DFSBuild(DFSTNode currentNode)
        {
            traversedNodes.Add(currentNode);

            //The current path is the set of all nodes on the DFS tree path from the root to the current node.
            currentPath.Add(currentNode);

            foreach (ISingleEntrySubGraph successorConstruct in LogicalFlowUtilities.GetTraversableSuccessors(currentNode.Construct))
            {
                DFSTNode successor;
                if (!constructToNodeMap.TryGetValue(successorConstruct, out successor))
                {
                    //Special case for interval constructs
                    continue;
                }

                if (!traversedNodes.Contains(successor))
                {
                    //If the successor is not traversed then the edge between the two nodes is a tree edge.
                    successor.Predecessor = currentNode;
                    currentNode.TreeEdgeSuccessors.Add(successor);
                    theTree.TreeEdges.Add(new DFSTEdge(currentNode, successor));

                    //We continue the build from this successor.
                    DFSBuild(successor);
                }
                else if (currentPath.Contains(successor))
                {
                    //If the successor is traversed and is on the current path then the edge between the nodes is a back edge.
                    successor.BackEdgePredecessors.Add(currentNode);
                    currentNode.BackEdgeSuccessors.Add(successor);
                    theTree.BackEdges.Add(new DFSTEdge(currentNode, successor));
                }
                else if (IsAncestor(successor, currentNode))
                {
                    //If the successor is traversed and the current node is its ancestor, then the edge is a forward edge.
                    successor.ForwardEdgePredecessors.Add(currentNode);
                    currentNode.ForwardEdgeSucessors.Add(successor);
                    theTree.ForwardEdges.Add(new DFSTEdge(currentNode, successor));
                }
                else
                {
                    //Otherwise the edge between the nodes is a cross edge.
                    successor.CrossEdgePredecessors.Add(currentNode);
                    currentNode.CrossEdgeSuccessors.Add(successor);
                    theTree.CrossEdges.Add(new DFSTEdge(currentNode, successor));
                }
            }

            currentPath.Remove(currentNode);

            //Adding the nodes in post order.
            theTree.ReversePostOrder.Add(currentNode);
        }
        private HashSet <ILogicalConstruct> GetLogicalConstructsInRange(BlockRange blockRange, out ILogicalConstruct theCommonParent)
        {
            HashSet <ILogicalConstruct>    children      = new HashSet <ILogicalConstruct>();
            HashSet <ISingleEntrySubGraph> blocksParents = new HashSet <ISingleEntrySubGraph>();

            int rangeBegin = blockRange.Start.First.Offset;
            int rangeEnd   = blockRange.End.First.Offset;

            for (int i = 0; i < context.CFG.Blocks.Length; i++)
            {
                InstructionBlock currentBlock = context.CFG.Blocks[i];
                if (currentBlock.First.Offset >= rangeBegin && currentBlock.First.Offset <= rangeEnd)
                {
                    CFGBlockLogicalConstruct[] cfgConstructs = context.CFGBlockToLogicalConstructMap[currentBlock];

                    for (int j = 0; j < cfgConstructs.Length; j++)
                    {
                        blocksParents.Add((ILogicalConstruct)cfgConstructs[j].Parent);
                        children.Add(cfgConstructs[j]);
                    }
                }
            }

            if (blocksParents.Count == 1)
            {
                theCommonParent = (ILogicalConstruct)blocksParents.ToArray <ISingleEntrySubGraph>()[0];

                return(children);
            }
            //TODO: CCheck whether the parent logical construct of of each CFGBlockLogicalConstruct that belongs to the exception handling block,
            //(i.e. each member of blocksParents) contains as children ONLY blocks that belong to the exception handling block (i.e. blocks that are in blockRange).

            theCommonParent = (ILogicalConstruct)LogicalFlowUtilities.FindFirstCommonParent(blocksParents);

            HashSet <ILogicalConstruct> result = new HashSet <ILogicalConstruct>();

            foreach (ILogicalConstruct child in children)
            {
                ILogicalConstruct desiredNode;
                LogicalFlowUtilities.TryGetParentConstructWithGivenParent(child, theCommonParent, out desiredNode);
                result.Add(desiredNode);
            }

            if (theCommonParent is ExceptionHandlingLogicalConstruct)
            {
                result.Clear();
                result.Add(theCommonParent);
                theCommonParent = theCommonParent.Parent as ILogicalConstruct;
            }

            return(result);
        }
 private HashSet <ILogicalConstruct> GetLogicalConstructsInRange(BlockRange blockRange, out ILogicalConstruct theCommonParent)
 {
     V_0 = new HashSet <ILogicalConstruct>();
     V_1 = new HashSet <ISingleEntrySubGraph>();
     V_2 = blockRange.Start.get_First().get_Offset();
     V_3 = blockRange.End.get_First().get_Offset();
     V_5 = 0;
     while (V_5 < (int)this.context.get_CFG().get_Blocks().Length)
     {
         V_6 = this.context.get_CFG().get_Blocks()[V_5];
         if (V_6.get_First().get_Offset() >= V_2 && V_6.get_First().get_Offset() <= V_3)
         {
             V_7 = this.context.get_CFGBlockToLogicalConstructMap().get_Item(V_6);
             V_8 = 0;
             while (V_8 < (int)V_7.Length)
             {
                 dummyVar0 = V_1.Add((ILogicalConstruct)V_7[V_8].get_Parent());
                 dummyVar1 = V_0.Add(V_7[V_8]);
                 V_8       = V_8 + 1;
             }
         }
         V_5 = V_5 + 1;
     }
     if (V_1.get_Count() == 1)
     {
         theCommonParent = (ILogicalConstruct)V_1.ToArray <ISingleEntrySubGraph>()[0];
         return(V_0);
     }
     theCommonParent = (ILogicalConstruct)LogicalFlowUtilities.FindFirstCommonParent(V_1);
     V_4             = new HashSet <ILogicalConstruct>();
     V_9             = V_0.GetEnumerator();
     try
     {
         while (V_9.MoveNext())
         {
             dummyVar2 = LogicalFlowUtilities.TryGetParentConstructWithGivenParent(V_9.get_Current(), theCommonParent, out V_10);
             dummyVar3 = V_4.Add(V_10);
         }
     }
     finally
     {
         ((IDisposable)V_9).Dispose();
     }
     if (theCommonParent as ExceptionHandlingLogicalConstruct != null)
     {
         V_4.Clear();
         dummyVar4       = V_4.Add(theCommonParent);
         theCommonParent = theCommonParent.get_Parent() as ILogicalConstruct;
     }
     return(V_4);
 }
Ejemplo n.º 5
0
        private void CreateSwitchConstruct(CFGBlockLogicalConstruct switchBlock, ILogicalConstruct parentConstruct,
                                           SwitchData switchData, DominatorTree dominatorTree)
        {
            List <KeyValuePair <CFGBlockLogicalConstruct, List <int> > >    cfgSuccessorToLabelsMap           = GetOrderedCFGSuccessorToLabelsMap(switchData);
            Dictionary <ILogicalConstruct, HashSet <ISingleEntrySubGraph> > validCaseEntryToDominatedNodesMap = GetValidCases(dominatorTree, switchBlock);

            List <CaseLogicalConstruct> orderedCaseConstructs = new List <CaseLogicalConstruct>();
            PairList <List <int>, CFGBlockLogicalConstruct> labelsToCFGSuccessorsList = new PairList <List <int>, CFGBlockLogicalConstruct>();

            foreach (KeyValuePair <CFGBlockLogicalConstruct, List <int> > cfgSuccessorToLabelsPair in cfgSuccessorToLabelsMap)
            {
                ILogicalConstruct successor;
                HashSet <ISingleEntrySubGraph> dominatedNodes;
                if (LogicalFlowUtilities.TryGetParentConstructWithGivenParent(cfgSuccessorToLabelsPair.Key, parentConstruct, out successor) &&
                    validCaseEntryToDominatedNodesMap.TryGetValue(successor, out dominatedNodes))
                {
                    CaseLogicalConstruct newCaseConstruct = new CaseLogicalConstruct(successor);
                    newCaseConstruct.CaseNumbers.AddRange(cfgSuccessorToLabelsPair.Value);
                    newCaseConstruct.Body.UnionWith(dominatedNodes.Cast <ILogicalConstruct>());
                    newCaseConstruct.AttachCaseConstructToGraph();
                    orderedCaseConstructs.Add(newCaseConstruct);
                }
                else
                {
                    labelsToCFGSuccessorsList.Add(cfgSuccessorToLabelsPair.Value, cfgSuccessorToLabelsPair.Key);
                }
            }

            CaseLogicalConstruct           defaultCase         = null;
            CFGBlockLogicalConstruct       defaultCFGSuccessor = GetCFGLogicalConstructFromBlock(switchData.DefaultCase);
            ILogicalConstruct              defaultSuccessor;
            HashSet <ISingleEntrySubGraph> defaultCaseNodes;

            if (LogicalFlowUtilities.TryGetParentConstructWithGivenParent(defaultCFGSuccessor, parentConstruct, out defaultSuccessor) &&
                validCaseEntryToDominatedNodesMap.TryGetValue(defaultSuccessor, out defaultCaseNodes))
            {
                defaultCase = new CaseLogicalConstruct(defaultSuccessor);
                if (HasSuccessors(defaultCaseNodes))
                {
                    defaultCase.Body.UnionWith(defaultCaseNodes.Cast <ILogicalConstruct>());
                }
                defaultCase.AttachCaseConstructToGraph();
            }

            SwitchLogicalConstruct theSwitch = SwitchLogicalConstruct.GroupInSwitchConstruct(switchBlock, orderedCaseConstructs, labelsToCFGSuccessorsList, defaultCase, defaultCFGSuccessor);

            UpdateDominatorTree(dominatorTree, theSwitch);
        }
Ejemplo n.º 6
0
 private void CreateSwitchConstruct(CFGBlockLogicalConstruct switchBlock, ILogicalConstruct parentConstruct, SwitchData switchData, DominatorTree dominatorTree)
 {
     stackVariable2 = this.GetOrderedCFGSuccessorToLabelsMap(switchData);
     V_0            = this.GetValidCases(dominatorTree, switchBlock);
     V_1            = new List <CaseLogicalConstruct>();
     V_2            = new PairList <List <int>, CFGBlockLogicalConstruct>();
     V_8            = stackVariable2.GetEnumerator();
     try
     {
         while (V_8.MoveNext())
         {
             V_9 = V_8.get_Current();
             if (!LogicalFlowUtilities.TryGetParentConstructWithGivenParent(V_9.get_Key(), parentConstruct, out V_10) || !V_0.TryGetValue(V_10, out V_11))
             {
                 V_2.Add(V_9.get_Value(), V_9.get_Key());
             }
             else
             {
                 V_12 = new CaseLogicalConstruct(V_10);
                 V_12.get_CaseNumbers().AddRange(V_9.get_Value());
                 V_12.get_Body().UnionWith(V_11.Cast <ILogicalConstruct>());
                 V_12.AttachCaseConstructToGraph();
                 V_1.Add(V_12);
             }
         }
     }
     finally
     {
         ((IDisposable)V_8).Dispose();
     }
     V_3 = null;
     V_4 = this.GetCFGLogicalConstructFromBlock(switchData.get_DefaultCase());
     if (LogicalFlowUtilities.TryGetParentConstructWithGivenParent(V_4, parentConstruct, out V_5) && V_0.TryGetValue(V_5, out V_6))
     {
         V_3 = new CaseLogicalConstruct(V_5);
         if (this.HasSuccessors(V_6))
         {
             V_3.get_Body().UnionWith(V_6.Cast <ILogicalConstruct>());
         }
         V_3.AttachCaseConstructToGraph();
     }
     V_7 = SwitchLogicalConstruct.GroupInSwitchConstruct(switchBlock, V_1, V_2, V_3, V_4);
     this.UpdateDominatorTree(dominatorTree, V_7);
     return;
 }