// 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); }
private void AcceptChild(CFGNode child) { if (child.ParentsNodes.Contains(this)) { return; } child.ParentsNodes.Add(this); }
public bool MoveBack() { if (_path.Count == 0) { return(false); } _current = _path.Pop(); return(true); }
public List <CFGNode> findBetween(CFGNode from, CFGNode to) { visitedNodes = new List <CFGNode>(); visitedNodes.Add(to); backDFS(from); return(visitedNodes); }
// 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); }
private CFGNode Move(CFGNode next) { if (next == null) { return(null); } _path.Push(_current); return(_current = next); }
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)); }
// 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); } } }
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)); } } }
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); }
public void Reset() { _current = _root; _path.Clear(); }
public CFGLookup(CFGNode root) { _root = root; _current = root; }
private int getNodeIndex(CFGNode node) { return(graph.Vertices.ToList().IndexOf(node)); }
public void SetGotoChild(CFGNode child) { gotoNode = child; AcceptChild(child); }
public void SetDirectChild(CFGNode child) { directChild = child; AcceptChild(child); }
private IEnumerable <CFGNode> getNodes(CFGNode node) { return(cfGraph.graph.Edges .Where(edge => edge.Target == node) .Select(edge => edge.Source)); }
private bool isDominate(CFGNode from, CFGNode to) { return(dominatorTree.isDominate(from, to)); }