Exemplo n.º 1
0
        void CreateEdge(ControlFlowNode source, ControlFlowNode destination)
        {
            ControlFlowEdge edge = new ControlFlowEdge(source, destination, JumpType.Normal);

            source.Outgoing.Add(edge);
            destination.Incoming.Add(edge);
        }
Exemplo n.º 2
0
 /// <summary>
 /// Creates a new instance of the <see cref="Enumerator"/> structure.
 /// </summary>
 /// <param name="collection">The collection to enumerate.</param>
 public Enumerator(AdjacencyCollection <TContents> collection)
 {
     _groupEnumerator = collection._neighbours.GetEnumerator();
     _itemIterator    = default;
     _hasItemIterator = false;
     _current         = null;
 }
Exemplo n.º 3
0
        /// <summary>
        /// Creates and adds a edge to the provided node.
        /// </summary>
        /// <param name="neighbour">The new neighbouring node.</param>
        /// <returns>The created edge.</returns>
        public ControlFlowEdge <TContents> Add(ControlFlowNode <TContents> neighbour)
        {
            var edge = new ControlFlowEdge <TContents>(Owner, neighbour, EdgeType);

            Add(edge);
            return(edge);
        }
Exemplo n.º 4
0
        ControlFlowGraph BuildGraph(IList <ILNode> nodes, ILLabel entryLabel)
        {
            int index = 0;
            List <ControlFlowNode> cfNodes    = new List <ControlFlowNode>();
            ControlFlowNode        entryPoint = new ControlFlowNode(index++, 0, ControlFlowNodeType.EntryPoint);

            cfNodes.Add(entryPoint);
            ControlFlowNode regularExit = new ControlFlowNode(index++, -1, ControlFlowNodeType.RegularExit);

            cfNodes.Add(regularExit);

            // Create graph nodes
            labelToCfNode = new Dictionary <ILLabel, ControlFlowNode>();
            Dictionary <ILNode, ControlFlowNode> astNodeToCfNode = new Dictionary <ILNode, ControlFlowNode>();

            foreach (ILBasicBlock node in nodes)
            {
                ControlFlowNode cfNode = new ControlFlowNode(index++, -1, ControlFlowNodeType.Normal);
                cfNodes.Add(cfNode);
                astNodeToCfNode[node] = cfNode;
                cfNode.UserData       = node;

                // Find all contained labels
                foreach (ILLabel label in node.GetSelfAndChildrenRecursive <ILLabel>())
                {
                    labelToCfNode[label] = cfNode;
                }
            }

            // Entry endge
            ControlFlowNode entryNode = labelToCfNode[entryLabel];
            ControlFlowEdge entryEdge = new ControlFlowEdge(entryPoint, entryNode, JumpType.Normal);

            entryPoint.Outgoing.Add(entryEdge);
            entryNode.Incoming.Add(entryEdge);

            // Create edges
            foreach (ILBasicBlock node in nodes)
            {
                ControlFlowNode source = astNodeToCfNode[node];

                // Find all branches
                foreach (ILLabel target in node.GetSelfAndChildrenRecursive <ILExpression>(e => e.IsBranch()).SelectMany(e => e.GetBranchTargets()))
                {
                    ControlFlowNode destination;

                    // Labels which are out of out scope will not be in the collection
                    // Insert self edge only if we are sure we are a loop
                    if (labelToCfNode.TryGetValue(target, out destination) && (destination != source || target == node.Body.FirstOrDefault()))
                    {
                        ControlFlowEdge edge = new ControlFlowEdge(source, destination, JumpType.Normal);
                        source.Outgoing.Add(edge);
                        destination.Incoming.Add(edge);
                    }
                }
            }

            return(new ControlFlowGraph(cfNodes.ToArray()));
        }
Exemplo n.º 5
0
 /// <summary>
 /// Adds an edge to the adjacency collection.
 /// </summary>
 /// <param name="edge">The edge to add.</param>
 /// <returns>The edge that was added.</returns>
 /// <exception cref="ArgumentException">
 /// Occurs when the provided edge cannot be added to this collection because of an invalid source node or edge type.
 /// </exception>
 public ControlFlowEdge <TContents> Add(ControlFlowEdge <TContents> edge)
 {
     AssertEdgeValidity(Owner, EdgeType, edge);
     GetEdges(edge.Target).Add(edge);
     edge.Target.IncomingEdges.Add(edge);
     _count++;
     return(edge);
 }
Exemplo n.º 6
0
        /// <inheritdoc />
        public bool Remove(ControlFlowEdge <TContents> edge)
        {
            bool result = GetEdges(edge.Target).Remove(edge);

            if (result)
            {
                _count--;
                edge.Target.IncomingEdges.Remove(edge);
            }

            return(result);
        }
Exemplo n.º 7
0
        internal static void AssertEdgeValidity(
            ControlFlowNode <TContents> owner, ControlFlowEdge <TContents> item, ControlFlowEdgeType type)
        {
            if (item.Type != type)
            {
                throw new ArgumentException(
                          $"Cannot add an edge of type {item.Type} to a collection of type {type}.");
            }

            if (item.Origin != owner)
            {
                throw new ArgumentException("Cannot add an edge originating from a different node.");
            }
        }
Exemplo n.º 8
0
            /// <inheritdoc />
            public bool MoveNext()
            {
                while (true)
                {
                    if (!_hasItemIterator)
                    {
                        if (!_groupEnumerator.MoveNext())
                        {
                            return(false);
                        }

                        _itemIterator    = _groupEnumerator.Current.Value.GetEnumerator();
                        _hasItemIterator = true;
                    }

                    if (_itemIterator.MoveNext())
                    {
                        _current = _itemIterator.Current;
                        return(true);
                    }

                    _hasItemIterator = false;
                }
            }
Exemplo n.º 9
0
 /// <inheritdoc />
 void ICollection <ControlFlowEdge <TContents> > .Add(ControlFlowEdge <TContents> edge)
 {
     Add(edge);
 }
Exemplo n.º 10
0
 /// <inheritdoc />
 public bool Contains(ControlFlowEdge <TContents> item)
 {
     return(GetEdges(item.Target).Contains(item));
 }
 public bool AddEigenEdge(ControlFlowEdge e)
 {
     var existing = Vedge(e.Source, e.Target);
     if (existing != null)
     {
         _eigenEdges.Contains(existing).AssertFalse();
         return false;
     }
     else
     {
         _vertices.Contains(e.Source).AssertTrue();
         _vertices.Contains(e.Target).AssertTrue();
         _eigenEdges.Add(e);
         OnEdgeAdded(e);
         return true;
     }
 }