Пример #1
0
        // Finds back path from source to target, true if it is.
        public bool FindBackwardPath(CFGNode source, CFGNode target)
        {
            List <Edge <CFGNode> > incoming_edges = null;

            if (Tree.ContainsVertex(source) && !Tree.IsInEdgesEmpty(source))
            {
                incoming_edges = Tree.InEdges(source).ToList();
            }
            else
            {
                return(false);
            }

            while (incoming_edges.Count() > 0)
            {
                var edge = incoming_edges.First();
                if (edge.Source.Equals(target))
                {
                    return(true);
                }
                else
                {
                    incoming_edges = Tree.InEdges(edge.Source).ToList();
                }
            }

            return(false);
        }
Пример #2
0
 private void AcceptChild(CFGNode child)
 {
     if (child.ParentsNodes.Contains(this))
     {
         return;
     }
     child.ParentsNodes.Add(this);
 }
Пример #3
0
 public bool MoveBack()
 {
     if (_path.Count == 0)
     {
         return(false);
     }
     _current = _path.Pop();
     return(true);
 }
Пример #4
0
        public List <CFGNode> findBetween(CFGNode from, CFGNode to)
        {
            visitedNodes = new List <CFGNode>();
            visitedNodes.Add(to);

            backDFS(from);

            return(visitedNodes);
        }
Пример #5
0
        // Constructor from list of blocks
        public CFGraph(List <IBaseBlock> blocks)
        {
            Blocks    = blocks;
            EdgeTypes = new EdgeTypes();

            // First step - construct
            List <CFGNode> cfg_nodes = new List <CFGNode>(blocks.Count);

            for (int i = 0; i < blocks.Count; i++)
            {
                cfg_nodes.Add(new CFGNode(blocks[i]));
            }

            // Second step - make connections
            for (int i = 0; i < cfg_nodes.Count; i++)
            {
                var lastOp = cfg_nodes[i].Value.Enumerate().Last();
                if (i != cfg_nodes.Count - 1 &&
                    lastOp.Operation != LinearRepr.Values.Operation.Goto)
                {
                    cfg_nodes[i].SetDirectChild(cfg_nodes[i + 1]);
                }

                if (lastOp.Operation == LinearRepr.Values.Operation.Goto ||
                    lastOp.Operation == LinearRepr.Values.Operation.CondGoto)
                {
                    var     gotoBlock = blocks.FirstOrDefault(b => b.Enumerate().First().Label.Equals(lastOp.Destination));
                    CFGNode goto_node = cfg_nodes.Find(el => el.Value == gotoBlock);
                    cfg_nodes[i].SetGotoChild(goto_node);
                }
            }

            // Make IList and add as vertexes
            var base_cfg_nodes = cfg_nodes as IList <CFGNode> ?? cfg_nodes.ToList();

            graph.AddVertexRange(base_cfg_nodes);


            // Add edges now
            foreach (var node in base_cfg_nodes)
            {
                if (node.directChild != null)
                {
                    graph.AddEdge(new Edge <CFGNode>(node, node.directChild));
                }

                if (node.gotoNode != null)
                {
                    graph.AddEdge(new Edge <CFGNode>(node, node.gotoNode));
                }
            }

            ClassificateEdges();
            buildDominatorTree();

            naturalCycleGraph = new NaturalCycleGraph(this);
        }
Пример #6
0
        private CFGNode Move(CFGNode next)
        {
            if (next == null)
            {
                return(null);
            }

            _path.Push(_current);
            return(_current = next);
        }
Пример #7
0
        private void backDFS(CFGNode currentNode)
        {
            if (!visitedNodes.Contains(currentNode))
            {
                visitedNodes.Add(currentNode);
            }

            getNodes(currentNode).ToList()
            .Where(node => !visitedNodes.Contains(node)).ToList()
            .ForEach(node => backDFS(node));
        }
Пример #8
0
        // Build tree
        private void BuildTree(CFGNode node, ref int currentNumber)
        {
            if (node == null)
            {
                return;
            }
            visited.Add(node);
            Numbers[node] = currentNumber;

            if (node.directChild == null && node.gotoNode == null)
            {
                return;
            }

            var children = new List <CFGNode>();

            if (node.directChild != null)
            {
                children.Add(node.directChild);
            }
            if (node.gotoNode != null)
            {
                children.Add(node.gotoNode);
            }


            if (!Tree.Vertices.Contains(node))
            {
                Tree.AddVertex(node);
            }
            foreach (var child in children)
            {
                if (!visited.Contains(child))
                {
                    if (!Tree.Vertices.Contains(child))
                    {
                        Tree.AddVertex(child);
                    }
                    Tree.AddEdge(new Edge <CFGNode>(node, child));

                    currentNumber += 1;
                    //Console.WriteLine(currentNumber);
                    BuildTree(child, ref currentNumber);
                }
            }
        }
Пример #9
0
        public CFGraph(List <IBaseBlock> blocks)
        {
            this.Blocks = blocks;
            List <CFGNode> cfg_nodes = new List <CFGNode>(blocks.Count);

            for (int i = 0; i < blocks.Count; i++)
            {
                cfg_nodes.Add(new CFGNode(blocks[i]));
            }

            for (int i = 0; i < cfg_nodes.Count; i++)
            {
                if (i != cfg_nodes.Count - 1)
                {
                    cfg_nodes[i].SetDirectChild(cfg_nodes[i + 1]);
                }

                var lastOp = cfg_nodes[i].Value.Enumerate().Last();
                if (lastOp.Operation == LinearRepr.Values.Operation.Goto)
                {
                    var     gotoBlock = blocks.First(b => b.Enumerate().First().Label.Equals(lastOp.Destination));
                    CFGNode goto_node = cfg_nodes.Find(el => el.Value == gotoBlock);
                    cfg_nodes[i].SetGotoChild(goto_node);
                }
            }

            var base_cfg_nodes = cfg_nodes as IList <CFGNode> ?? cfg_nodes.ToList();

            graph.AddVertexRange(base_cfg_nodes);

            foreach (var node in base_cfg_nodes)
            {
                if (node.directChild != null)
                {
                    graph.AddEdge(new Edge <CFGNode>(node, node.directChild));
                }

                if (node.gotoNode != null)
                {
                    graph.AddEdge(new Edge <CFGNode>(node, node.gotoNode));
                }
            }
        }
Пример #10
0
        public static CFGraph Build(List <IBaseBlock> blocks)
        {
            CFGNode root        = new CFGNode(blocks.First());
            var     currentNode = root;

            foreach (var block in blocks.Skip(1))
            {
                var directChild = new CFGNode(block);
                currentNode.SetDirectChild(directChild);

                var lastOp = currentNode.Value.Enumerate().Last();
                if (lastOp.IsGoto())
                {
                    var gotoBlock = blocks.First(b => b.Enumerate().First().Label.Equals(lastOp.Destination));
                    var gotoChild = new CFGNode(gotoBlock);
                    currentNode.SetGotoChild(gotoChild);
                }
                currentNode = directChild;
            }

            var graph = new CFGraph(blocks);

            return(graph);
        }
Пример #11
0
 public void Reset()
 {
     _current = _root;
     _path.Clear();
 }
Пример #12
0
 public CFGLookup(CFGNode root)
 {
     _root    = root;
     _current = root;
 }
Пример #13
0
 private int getNodeIndex(CFGNode node)
 {
     return(graph.Vertices.ToList().IndexOf(node));
 }
Пример #14
0
 public void SetGotoChild(CFGNode child)
 {
     gotoNode = child;
     AcceptChild(child);
 }
Пример #15
0
 public void SetDirectChild(CFGNode child)
 {
     directChild = child;
     AcceptChild(child);
 }
Пример #16
0
 private IEnumerable <CFGNode> getNodes(CFGNode node)
 {
     return(cfGraph.graph.Edges
            .Where(edge => edge.Target == node)
            .Select(edge => edge.Source));
 }
Пример #17
0
 private bool isDominate(CFGNode from, CFGNode to)
 {
     return(dominatorTree.isDominate(from, to));
 }