From 13e91a5366e18d6a9be7482f8b5a0f3b318f288b Mon Sep 17 00:00:00 2001 From: Jamie Hanlon Date: Tue, 14 Mar 2023 18:02:57 +0000 Subject: [PATCH] Dot output WIP --- {source => include/slang}/text/FormatBuffer.h | 0 source/ast/Bitstream.cpp | 2 +- source/ast/EvalContext.cpp | 3 +-- source/ast/Symbol.cpp | 3 +-- source/ast/types/TypePrinter.cpp | 3 +-- source/diagnostics/TextDiagnosticClient.cpp | 3 +-- source/numeric/ConstantValue.cpp | 2 +- source/text/Json.cpp | 2 +- tools/netlist/include/DirectedGraph.h | 5 +++++ tools/netlist/netlist.cpp | 19 +++++++++++++++++++ 10 files changed, 31 insertions(+), 11 deletions(-) rename {source => include/slang}/text/FormatBuffer.h (100%) diff --git a/source/text/FormatBuffer.h b/include/slang/text/FormatBuffer.h similarity index 100% rename from source/text/FormatBuffer.h rename to include/slang/text/FormatBuffer.h diff --git a/source/ast/Bitstream.cpp b/source/ast/Bitstream.cpp index dc3c4843c..91ed9c1f4 100644 --- a/source/ast/Bitstream.cpp +++ b/source/ast/Bitstream.cpp @@ -7,7 +7,6 @@ //------------------------------------------------------------------------------ #include "slang/ast/Bitstream.h" -#include "../text/FormatBuffer.h" #include #include "slang/ast/Compilation.h" @@ -18,6 +17,7 @@ #include "slang/diagnostics/ConstEvalDiags.h" #include "slang/diagnostics/ExpressionsDiags.h" #include "slang/diagnostics/NumericDiags.h" +#include "slang/text/FormatBuffer.h" namespace slang::ast { diff --git a/source/ast/EvalContext.cpp b/source/ast/EvalContext.cpp index 3246fec06..ee5fbeae1 100644 --- a/source/ast/EvalContext.cpp +++ b/source/ast/EvalContext.cpp @@ -7,14 +7,13 @@ //------------------------------------------------------------------------------ #include "slang/ast/EvalContext.h" -#include "../text/FormatBuffer.h" - #include "slang/ast/ASTContext.h" #include "slang/ast/Compilation.h" #include "slang/ast/symbols/SubroutineSymbols.h" #include "slang/ast/symbols/VariableSymbols.h" #include "slang/ast/types/Type.h" #include "slang/diagnostics/ConstEvalDiags.h" +#include "slang/text/FormatBuffer.h" namespace slang::ast { diff --git a/source/ast/Symbol.cpp b/source/ast/Symbol.cpp index d668dec68..0157456a7 100644 --- a/source/ast/Symbol.cpp +++ b/source/ast/Symbol.cpp @@ -7,8 +7,6 @@ //------------------------------------------------------------------------------ #include "slang/ast/Symbol.h" -#include "../text/FormatBuffer.h" - #include "slang/ast/ASTVisitor.h" #include "slang/ast/Compilation.h" #include "slang/ast/Definition.h" @@ -16,6 +14,7 @@ #include "slang/ast/symbols/MemberSymbols.h" #include "slang/ast/types/Type.h" #include "slang/syntax/AllSyntax.h" +#include "slang/text/FormatBuffer.h" #include "slang/text/SourceManager.h" #include "slang/util/StackContainer.h" diff --git a/source/ast/types/TypePrinter.cpp b/source/ast/types/TypePrinter.cpp index 545db64d8..a5bc4916d 100644 --- a/source/ast/types/TypePrinter.cpp +++ b/source/ast/types/TypePrinter.cpp @@ -7,9 +7,8 @@ //------------------------------------------------------------------------------ #include "slang/ast/types/TypePrinter.h" -#include "../../text/FormatBuffer.h" - #include "slang/ast/ASTVisitor.h" +#include "slang/text/FormatBuffer.h" namespace slang::ast { diff --git a/source/diagnostics/TextDiagnosticClient.cpp b/source/diagnostics/TextDiagnosticClient.cpp index 8a68cd796..748e1d981 100644 --- a/source/diagnostics/TextDiagnosticClient.cpp +++ b/source/diagnostics/TextDiagnosticClient.cpp @@ -7,10 +7,9 @@ //------------------------------------------------------------------------------ #include "slang/diagnostics/TextDiagnosticClient.h" -#include "../text/FormatBuffer.h" - #include "slang/text/CharInfo.h" #include "slang/text/SourceManager.h" +#include "slang/text/FormatBuffer.h" namespace slang { diff --git a/source/numeric/ConstantValue.cpp b/source/numeric/ConstantValue.cpp index 8011c04f7..81409ec7a 100644 --- a/source/numeric/ConstantValue.cpp +++ b/source/numeric/ConstantValue.cpp @@ -7,10 +7,10 @@ //------------------------------------------------------------------------------ #include "slang/numeric/ConstantValue.h" -#include "../text/FormatBuffer.h" #include #include "slang/util/Hash.h" +#include "slang/text/FormatBuffer.h" namespace slang { diff --git a/source/text/Json.cpp b/source/text/Json.cpp index 26626db70..9bfa54406 100644 --- a/source/text/Json.cpp +++ b/source/text/Json.cpp @@ -7,9 +7,9 @@ //------------------------------------------------------------------------------ #include "slang/text/Json.h" -#include "FormatBuffer.h" #include +#include "slang/text/FormatBuffer.h" #include "slang/util/SmallVector.h" #include "slang/util/String.h" diff --git a/tools/netlist/include/DirectedGraph.h b/tools/netlist/include/DirectedGraph.h index 364b977f2..576040478 100644 --- a/tools/netlist/include/DirectedGraph.h +++ b/tools/netlist/include/DirectedGraph.h @@ -168,6 +168,11 @@ class DirectedGraph { DirectedGraph() = default; + const_iterator begin() const { return nodes.begin(); } + const_iterator end() const { return nodes.end(); } + iterator begin() { return nodes.begin(); } + iterator end() { return nodes.end(); } + node_descriptor findNode(const NodeType &nodeToFind) const { auto it = std::find_if(nodes.begin(), nodes.end(), [&nodeToFind](const std::unique_ptr &node) { diff --git a/tools/netlist/netlist.cpp b/tools/netlist/netlist.cpp index f7830bbba..7cdfbbecf 100644 --- a/tools/netlist/netlist.cpp +++ b/tools/netlist/netlist.cpp @@ -25,6 +25,7 @@ #include "slang/util/TimeTrace.h" #include "slang/util/Version.h" #include "slang/util/Util.h" +#include "slang/text/FormatBuffer.h" #include "DirectedGraph.h" @@ -93,6 +94,7 @@ class NetlistNode : public Node { selectors.emplace_back(std::make_unique(name)); } void setName(string_view name) { name = name; } + string_view getName() { return name; } private: string_view name; std::vector> selectors; @@ -337,6 +339,18 @@ void printJson(Compilation& compilation, const std::string& fileName, writeToFile(fileName, writer.view()); } +void printDOT(const Netlist &netlist, const std::string &fileName) { + slang::FormatBuffer buffer; + buffer.append("digraph {\n"); + for (auto &node : netlist) { + for (auto &edge : node->getEdges()) { + buffer.format(" {} -> {}", node->getName(), edge->getTargetNode()); + } + } + buffer.append("}\n"); + writeToFile(fileName, buffer.str()); +} + template void writeToFile(Stream& os, string_view fileName, String contents) { os.write(contents.data(), contents.size()); @@ -378,6 +392,11 @@ int main(int argc, char** argv) { "given hierarchical paths", ""); + std::optional netlistDotFile; + driver.cmdLine.add("--netlist-dot", netlistDotFile, + "Dump the netlist in DOT format to the specified file, or '-' for stdout", "", + /* isFileName */ true); + if (!driver.parseCommandLine(argc, argv)) { return 1; }