Skip to content

Commit

Permalink
Start coloring CFG nodes. Start using CreateNodeFromBasicBlock.
Browse files Browse the repository at this point in the history
  • Loading branch information
allisterb committed Jul 28, 2023
1 parent 1305516 commit 6700bb5
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 35 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -68,38 +68,19 @@ public static void AnalyzeControlFlow(string cfgFile, Configuration config, Sema
{
var bb = cfg.Blocks[j];
if (bb.Kind == BasicBlockKind.Exit) continue;

var nid = method.Identifier + "::" + method.Type + "_" + bb.Ordinal.ToString();
var node = graph.FindNode(nid);
if (node is null)
{
node = new Node(nid);
var src = bb.Operations.Select(o => o?.Syntax.ToString() ?? "").JoinWith(Environment.NewLine) + Environment.NewLine + (bb.BranchValue?.Syntax.ToString() ?? "") ;
node.LabelText =
bb.Kind == BasicBlockKind.Entry ? method.Identifier + "::" + method.Type + Environment.NewLine + src : src;
graph.AddNode(node);
}

var node = CreateNodeFromBasicBlock(bb, method.Identifier, method.Type, graph);
if (bb.ConditionalSuccessor is not null && bb.ConditionalSuccessor.Destination.Kind != BasicBlockKind.Exit)
{
var csnid = method.Identifier + "::" + method.Type + "_" + bb.ConditionalSuccessor.Destination.Ordinal.ToString();
var csnode = graph.FindNode(csnid);
if (csnode is null)
{
csnode = new Node(csnid);
csnode.LabelText = bb.ConditionalSuccessor.Destination.Operations.Select(o => o?.Syntax.ToString() ?? "").JoinWith(Environment.NewLine) + Environment.NewLine + (bb.ConditionalSuccessor.Destination.BranchValue?.Syntax.ToString() ?? "");
graph.AddNode(csnode);
}

var csnode = CreateNodeFromBasicBlock(bb.ConditionalSuccessor.Destination, method.Identifier, method.Type, graph);
if (bb.ConditionalSuccessor.Source != bb)
{
var cssnode = CreateNodeFromBasicBlock(bb.ConditionalSuccessor.Source, method.Identifier, method.Type, graph);
graph.AddEdge(nid, cssnode.Id);
graph.AddEdge(cssnode.Id, csnid);
graph.AddEdge(node.Id, cssnode.Id);
graph.AddEdge(cssnode.Id, csnode.Id);
}
else
{
graph.AddEdge(nid, csnid);
graph.AddEdge(node.Id, csnode.Id);
}
}

Expand All @@ -111,18 +92,19 @@ public static void AnalyzeControlFlow(string cfgFile, Configuration config, Sema
{
ftnode = new Node(ftnid);
ftnode.LabelText = bb.FallThroughSuccessor.Destination.Operations.Select(o => o?.Syntax.ToString() ?? "").JoinWith(Environment.NewLine) + Environment.NewLine + (bb.FallThroughSuccessor.Destination.BranchValue?.Syntax.ToString() ?? "");
ftnode.Kind = bb.FallThroughSuccessor.Destination.Kind == BasicBlockKind.Entry ? "entry" : bb.FallThroughSuccessor.Destination.ConditionalSuccessor is not null ? "branch" : "block";
graph.AddNode(ftnode);
}

if (bb.FallThroughSuccessor.Source != bb)
{
var ftsnode = CreateNodeFromBasicBlock(bb.FallThroughSuccessor.Source, method.Identifier, method.Type, graph);
graph.AddEdge(nid, ftsnode.Id);
graph.AddEdge(node.Id, ftsnode.Id);
graph.AddEdge(ftsnode.Id, ftnid);
}
else
{
graph.AddEdge(nid, ftnid);
graph.AddEdge(node.Id, ftnid);
}
}

Expand Down Expand Up @@ -159,6 +141,7 @@ protected static Node CreateNodeFromBasicBlock(BasicBlock bb, string methodIdent
var src = bb.Operations.Select(o => o?.Syntax.ToString() ?? "").JoinWith(Environment.NewLine) + Environment.NewLine + (bb.BranchValue?.Syntax.ToString() ?? "");
node.LabelText =
bb.Kind == BasicBlockKind.Entry ? methodIdentifier + "::" + methodType + Environment.NewLine + src : src;
node.Kind = bb.Kind == BasicBlockKind.Entry ? "entry" : bb.BranchValue is not null && bb.ConditionalSuccessor is not null ? "branch" : "block";
graph.AddNode(node);
}
return node;
Expand Down
2 changes: 1 addition & 1 deletion src/Stratis.CodeAnalysis.Cs/Stratis.CodeAnalysis.Cs/Gui.cs
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ public static void SendGuiMessage(string cfgfile, Compilation c, string document
EditorEntryAssembly = EntryAssembly?.FullName ?? "(none)",
AssemblyName = c.AssemblyName,
Document = document,
Nodes = graph.Nodes.Select(n => new NodeData() { Id = n.Id, Label = n.LabelText }).ToArray(),
Nodes = graph.Nodes.Select(n => new NodeData() { Id = n.Id, Label = n.LabelText, Kind = n.Kind }).ToArray(),
Edges = graph.Edges.Select(e => new EdgeData() { SourceId = e.Source, TargetId = e.Target, Label = e.LabelText}).ToArray()
};
if (GuiProcessRunning() && !pipeClient.IsConnected)
Expand Down
30 changes: 23 additions & 7 deletions src/Stratis.DevEx.Drawing/VisJS/VisJS.cs
Original file line number Diff line number Diff line change
Expand Up @@ -83,13 +83,17 @@ public static Network Draw(Graph graph, string width="100%", string height="100%
Font = new NetworkFont() { Face = "monospace", Align = "left" },
Shape = nodeshape,
Size = nodesize,
Color = node.Attr.FillColor != Color.Transparent || node.Attr.Color != Color.White ?
new NetworkColor()
{
Background = node.Attr.FillColor.ToString().Trim('"')
} : null
}); ;

Color = graph.Kind switch
{
"cg" => node.Attr.FillColor != Color.Transparent || node.Attr.Color != Color.White ?
new NetworkColor()
{
Background = node.Attr.FillColor.ToString().Trim('"')
} : null,
"cfg" => GetCFGNodeColor(node),
_ => null
}
});
}

foreach(var edge in graph.Edges)
Expand All @@ -107,6 +111,18 @@ public static Network Draw(Graph graph, string width="100%", string height="100%
return network;
}

protected static NetworkColor GetCFGNodeColor(Node node)
{
return new NetworkColor()
{
Background = (node.Kind) switch

Check failure on line 118 in src/Stratis.DevEx.Drawing/VisJS/VisJS.cs

View workflow job for this annotation

GitHub Actions / build (Debug, 3.1.x)

'Node' does not contain a definition for 'Kind' and no accessible extension method 'Kind' accepting a first argument of type 'Node' could be found (are you missing a using directive or an assembly reference?)

Check failure on line 118 in src/Stratis.DevEx.Drawing/VisJS/VisJS.cs

View workflow job for this annotation

GitHub Actions / build (Debug, 3.1.x)

'Node' does not contain a definition for 'Kind' and no accessible extension method 'Kind' accepting a first argument of type 'Node' could be found (are you missing a using directive or an assembly reference?)
{
"entry" => "LightYellow",
"branch" => "Blue",
_ => "White"
}
};
}
#endregion
}
}
2 changes: 1 addition & 1 deletion src/Stratis.DevEx.Gui/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ public static Graph CreateGraph(ControlFlowGraphMessage m)
{
if (graph.FindNode(node.Id) is null)
{
graph.AddNode(new Node(node.Id) { LabelText = node.Label });
graph.AddNode(new Node(node.Id) { LabelText = node.Label, Kind = node.Kind });
}
}
foreach (var edge in m.Edges)
Expand Down

0 comments on commit 6700bb5

Please sign in to comment.