예제 #1
0
 public GraphVizGraph ExportGraph()
 {
     GraphVizGraph graph = new GraphVizGraph();
     foreach (ControlFlowNode node in nodes) {
         graph.AddNode(new GraphVizNode(node.BlockIndex) { label = node.ToString(), shape = "box" });
     }
     foreach (ControlFlowNode node in nodes) {
         foreach (ControlFlowEdge edge in node.Outgoing) {
             GraphVizEdge e = new GraphVizEdge(edge.Source.BlockIndex, edge.Target.BlockIndex);
             switch (edge.Type) {
                 case JumpType.Normal:
                     break;
                 case JumpType.LeaveTry:
                 case JumpType.EndFinally:
                     e.color = "red";
                     break;
                 default:
                     e.color = "gray";
                     //e.constraint = false;
                     break;
             }
             graph.AddEdge(e);
         }
         if (node.ImmediateDominator != null) {
             graph.AddEdge(new GraphVizEdge(node.ImmediateDominator.BlockIndex, node.BlockIndex) { color = "green", constraint = false });
         }
     }
     return graph;
 }
예제 #2
0
        /// <summary>
        /// Exports the CFG. This method is intended to help debugging issues related to definite assignment.
        /// </summary>
        public GraphVizGraph ExportGraph()
        {
            GraphVizGraph g = new GraphVizGraph();

            g.Title = "DefiniteAssignment - " + variableName;
            for (int i = 0; i < allNodes.Count; i++)
            {
                string name = "#" + i + " = " + allNodes[i].NodeStatus.ToString() + Environment.NewLine;
                switch (allNodes[i].Type)
                {
                case ControlFlowNodeType.StartNode:
                case ControlFlowNodeType.BetweenStatements:
                    name += allNodes[i].NextStatement.ToString();
                    break;

                case ControlFlowNodeType.EndNode:
                    name += "End of " + allNodes[i].PreviousStatement.ToString();
                    break;

                case ControlFlowNodeType.LoopCondition:
                    name += "Condition in " + allNodes[i].NextStatement.ToString();
                    break;

                default:
                    name += allNodes[i].Type.ToString();
                    break;
                }
                g.AddNode(new GraphVizNode(i)
                {
                    label = name
                });
                foreach (ControlFlowEdge edge in allNodes[i].Outgoing)
                {
                    GraphVizEdge ge = new GraphVizEdge(i, ((DefiniteAssignmentNode)edge.To).Index);
                    if (edgeStatus.Count > 0)
                    {
                        ge.label = edgeStatus[edge].ToString();
                    }
                    if (edge.IsLeavingTryFinally)
                    {
                        ge.style = "dashed";
                    }
                    switch (edge.Type)
                    {
                    case ControlFlowEdgeType.ConditionTrue:
                        ge.color = "green";
                        break;

                    case ControlFlowEdgeType.ConditionFalse:
                        ge.color = "red";
                        break;

                    case ControlFlowEdgeType.Jump:
                        ge.color = "blue";
                        break;
                    }
                    g.AddEdge(ge);
                }
            }
            return(g);
        }
예제 #3
0
 public GraphVizGraph ExportVariableGraph(Func<SsaVariable, string> labelProvider = null)
 {
     if (labelProvider == null)
         labelProvider = v => v.ToString();
     GraphVizGraph graph = new GraphVizGraph();
     foreach (SsaVariable v in this.AllVariables) {
         graph.AddNode(new GraphVizNode(v.Name) { label = labelProvider(v) });
     }
     int instructionIndex = 0;
     foreach (SsaBlock block in this.Blocks) {
         foreach (SsaInstruction inst in block.Instructions) {
             if (inst.Operands.Length == 0 && inst.Target == null)
                 continue;
             string id = "instruction" + (++instructionIndex);
             graph.AddNode(new GraphVizNode(id) { label = inst.ToString(), shape = "box" });
             foreach (SsaVariable op in inst.Operands)
                 graph.AddEdge(new GraphVizEdge(op.Name, id));
             if (inst.Target != null)
                 graph.AddEdge(new GraphVizEdge(id, inst.Target.Name));
         }
     }
     return graph;
 }
예제 #4
0
 public GraphVizGraph ExportBlockGraph(Func<SsaBlock, string> labelProvider = null)
 {
     if (labelProvider == null)
         labelProvider = b => b.ToString();
     GraphVizGraph graph = new GraphVizGraph();
     foreach (SsaBlock block in this.Blocks) {
         graph.AddNode(new GraphVizNode(block.BlockIndex) { label = labelProvider(block), shape = "box" });
     }
     foreach (SsaBlock block in this.Blocks) {
         foreach (SsaBlock s in block.Successors) {
             graph.AddEdge(new GraphVizEdge(block.BlockIndex, s.BlockIndex));
         }
     }
     return graph;
 }
예제 #5
0
        /// <summary>
        /// Debugging helper that exports a control flow graph.
        /// </summary>
        public static GraphVizGraph ExportGraph(IList <ControlFlowNode> nodes)
        {
            GraphVizGraph g = new GraphVizGraph();

            GraphVizNode[] n = new GraphVizNode[nodes.Count];
            Dictionary <ControlFlowNode, int> dict = new Dictionary <ControlFlowNode, int>();

            for (int i = 0; i < n.Length; i++)
            {
                dict.Add(nodes[i], i);
                n[i] = new GraphVizNode(i);
                string name = "#" + i + " = ";
                switch (nodes[i].Type)
                {
                case ControlFlowNodeType.StartNode:
                case ControlFlowNodeType.BetweenStatements:
                    name += nodes[i].NextStatement.DebugToString();
                    break;

                case ControlFlowNodeType.EndNode:
                    name += "End of " + nodes[i].PreviousStatement.DebugToString();
                    break;

                case ControlFlowNodeType.LoopCondition:
                    name += "Condition in " + nodes[i].NextStatement.DebugToString();
                    break;

                default:
                    name += "?";
                    break;
                }
                n[i].label = name;
                g.AddNode(n[i]);
            }
            for (int i = 0; i < n.Length; i++)
            {
                foreach (ControlFlowEdge edge in nodes[i].Outgoing)
                {
                    GraphVizEdge ge = new GraphVizEdge(i, dict[edge.To]);
                    if (edge.IsLeavingTryFinally)
                    {
                        ge.style = "dashed";
                    }
                    switch (edge.Type)
                    {
                    case ControlFlowEdgeType.ConditionTrue:
                        ge.color = "green";
                        break;

                    case ControlFlowEdgeType.ConditionFalse:
                        ge.color = "red";
                        break;

                    case ControlFlowEdgeType.Jump:
                        ge.color = "blue";
                        break;
                    }
                    g.AddEdge(ge);
                }
            }
            return(g);
        }