slang-netlist  0.11.0
Loading...
Searching...
No Matches
NetlistDot.hpp
Go to the documentation of this file.
1#pragma once
2
5
7
8#include <unordered_set>
9
10namespace slang::netlist {
11
13struct NetlistDot {
14
16 static auto render(NetlistGraph const &netlist, FormatBuffer &buffer) {
17 renderImpl(netlist, buffer, nullptr);
18 }
19
23 static auto render(NetlistGraph const &netlist, FormatBuffer &buffer,
24 std::unordered_set<NetlistNode const *> const &nodes) {
25 renderImpl(netlist, buffer, &nodes);
26 }
27
28private:
29 static void writeNode(FormatBuffer &buffer, NetlistNode const &node) {
30 switch (node.kind) {
31 case NodeKind::Port: {
32 auto const &portNode = node.as<Port>();
33 buffer.format(" N{} [label=\"{} port {}\"]\n", node.ID,
34 toString(portNode.direction), portNode.name);
35 break;
36 }
37 case NodeKind::Variable: {
38 auto const &varNode = node.as<Variable>();
39 buffer.format(" N{} [label=\"Variable {}\"]\n", node.ID, varNode.name);
40 break;
41 }
43 buffer.format(" N{} [label=\"Assignment\"]\n", node.ID);
44 break;
45 }
46 case NodeKind::Case: {
47 buffer.format(" N{} [label=\"Case\"]\n", node.ID);
48 break;
49 }
51 buffer.format(" N{} [label=\"Conditional\"]\n", node.ID);
52 break;
53 }
54 case NodeKind::Merge: {
55 buffer.format(" N{} [label=\"Merge\"]\n", node.ID);
56 break;
57 }
58 case NodeKind::State: {
59 auto const &state = node.as<State>();
60 buffer.format(" N{} [label=\"{} {}\"]\n", node.ID, state.name,
61 toString(state.bounds));
62 break;
63 }
64 case NodeKind::Constant: {
65 auto const &constNode = node.as<Constant>();
66 buffer.format(" N{} [label=\"Const {}\"]\n", node.ID,
67 constNode.value.toString());
68 break;
69 }
70 default:
71 SLANG_UNREACHABLE;
72 }
73 }
74
75 static void
76 renderImpl(NetlistGraph const &netlist, FormatBuffer &buffer,
77 std::unordered_set<NetlistNode const *> const *filter) {
78 auto included = [&](NetlistNode const &node) {
79 return filter == nullptr || filter->count(&node) != 0;
80 };
81
82 buffer.append("digraph {\n");
83 buffer.append(" node [shape=record];\n");
84 for (auto const &node : netlist) {
85 if (!included(*node)) {
86 continue;
87 }
88 writeNode(buffer, *node);
89 }
90 for (auto const &node : netlist) {
91 if (!included(*node)) {
92 continue;
93 }
94 for (auto const &edge : node->getOutEdges()) {
95 if (edge->disabled) {
96 continue;
97 }
98 if (!included(edge->getTargetNode())) {
99 continue;
100 }
101 if (edge->symbol != nullptr && !edge->symbol->empty()) {
102 buffer.format(" N{} -> N{} [label=\"{}{}\"]\n", node->ID,
103 edge->getTargetNode().ID, edge->symbol->name,
104 toString(edge->bounds));
105 } else {
106 buffer.format(" N{} -> N{}\n", node->ID, edge->getTargetNode().ID);
107 }
108 }
109 }
110 buffer.append("}\n");
111 }
112};
113
114} // namespace slang::netlist
Definition FormatBuffer.hpp:13
void format(fmt::format_string< Args... > fmt, Args &&...args)
Append the result of formatting fmt with args.
Definition FormatBuffer.hpp:23
Represent the netlist connectivity of an elaborated design.
Definition NetlistGraph.hpp:44
Definition NetlistNode.hpp:33
NodeKind kind
Definition NetlistNode.hpp:38
size_t ID
Definition NetlistNode.hpp:37
auto as() -> T &
Definition NetlistNode.hpp:45
auto getOutEdges() const -> const OutEdgeListType &
Definition DirectedGraph.hpp:256
Definition NetlistNode.hpp:71
Definition FormatBuffer.hpp:9
@ Case
Definition NetlistNode.hpp:25
@ State
Definition NetlistNode.hpp:27
@ Variable
Definition NetlistNode.hpp:22
@ Port
Definition NetlistNode.hpp:21
@ Merge
Definition NetlistNode.hpp:26
@ Conditional
Definition NetlistNode.hpp:24
@ Constant
Definition NetlistNode.hpp:28
@ Assignment
Definition NetlistNode.hpp:23
A utility class for rendering a netlist graph in DOT format.
Definition NetlistDot.hpp:13
static auto render(NetlistGraph const &netlist, FormatBuffer &buffer)
Render the whole netlist graph.
Definition NetlistDot.hpp:16
static auto render(NetlistGraph const &netlist, FormatBuffer &buffer, std::unordered_set< NetlistNode const * > const &nodes)
Definition NetlistDot.hpp:23