25#include <unordered_map>
28#include <boost/graph/adjacency_list.hpp>
30#include <nlohmann/json.hpp>
44using Json = nlohmann::ordered_json;
70 boost::adjacency_list<boost::setS, boost::vecS, boost::directedS, VertexProperty>;
72 using Vertex = boost::graph_traits<Graph>::vertex_descriptor;
84 void addModule(std::string_view moduleName);
95 void addDependency(std::string_view from, std::string_view to);
130 [[nodiscard]]
bool containsModule(std::string_view moduleName)
const;
141 [[nodiscard]] std::optional<Vertex>
findVertex(std::string_view moduleName)
const;
179 [[nodiscard]]
Json bits(
const core::Bus& bus, std::string_view nullValue =
"x")
const;
188 std::size_t expectedWidth)
const;
194 [[nodiscard]]
static Json concatenate(
const std::vector<Json>& vectors);
197 [[nodiscard]]
static std::string
parameter(std::uint64_t value, std::size_t width = 32);
203 void addCell(std::string_view suffix, std::string_view type,
Json parameters,
204 Json portDirections,
Json connections,
Json attributes = Json::object());
207 void addPort(std::string name, std::string_view direction,
const core::Bus& bus);
211 const std::vector<core::Bus>& outputs,
232 std::string_view moduleName);
245 std::optional<std::string_view> moduleName = std::nullopt);
Represents a digital circuit as a directed graph of components.
Base class for all digital circuit components.
SerializationContext(Impl &impl)
Module-instantiation dependency graph for a Yosys JSON design.
std::vector< std::string > ModuleNameList
List of module names.
NameToVertex nameToVertex
Name-to-vertex index kept in sync with graph.
void rebuildNameIndex()
Rebuilds nameToVertex from the current graph.
Graph graph
The dependency graph itself.
bool containsModule(std::string_view moduleName) const
Checks whether a module vertex is present in the graph.
boost::adjacency_list< boost::setS, boost::vecS, boost::directedS, VertexProperty > Graph
Boost graph type: set out-edges, vector vertices, directed.
ModuleNameList dependencyOrder(const ModuleNameList &roots) const
Returns the unique dependency closure of roots in dependency-first order.
std::unordered_map< std::string, Vertex, core::TransparentStringHash, core::TransparentStringEqual > NameToVertex
O(1) index mapping a module name to its graph vertex.
const Graph & boost() const
Read-only access to the underlying Boost graph.
void addDependency(std::string_view from, std::string_view to)
Records that from instantiates to.
ModuleNameList dependenciesOf(std::string_view moduleName) const
Returns the sorted, unique modules moduleName instantiates.
Vertex ensureVertex(std::string_view moduleName)
Ensures a module vertex exists in this graph (O(1)).
boost::graph_traits< Graph >::vertex_descriptor Vertex
Boost vertex descriptor (an integer index under vecS).
void addModule(std::string_view moduleName)
Ensures a module vertex exists, creating it if necessary.
ModuleNameList modules() const
Returns every declared module name.
std::optional< Vertex > findVertex(std::string_view moduleName) const
void addPort(std::string name, std::string_view direction, const core::Bus &bus)
Register a named top-level input or output port.
void addSubcircuitInstance(std::string_view slug, const std::vector< core::Bus > &inputs, const std::vector< core::Bus > &outputs, const core::CircuitResolver *resolver)
Emit a hierarchical instance and recursively serialize its definition.
SerializationContext(Impl &impl)
Json bits(const core::Bus &bus, std::string_view nullValue="x") const
Convert a Silicon bus to a Yosys least-significant-bit-first vector.
Json allocateBits(std::size_t width)
Allocate module-local temporary signal bits.
friend std::string serialize(const core::Circuit &circuit, std::string_view moduleName)
Serialize a complete Silicon circuit as Yosys write_json-compatible JSON.
static std::string parameter(std::uint64_t value, std::size_t width=32)
Encode an unsigned Yosys parameter as a fixed-width binary string.
std::string componentIdentifier() const
Stable name of the component being serialized within its module.
Json inputBits(const core::Component &component, std::size_t index, std::size_t expectedWidth) const
Encode a component input, applying its declared unconnected-input default.
void addCell(std::string_view suffix, std::string_view type, Json parameters, Json portDirections, Json connections, Json attributes=Json::object())
Emit one native Yosys cell for the current Silicon component.
static Json concatenate(const std::vector< Json > &vectors)
Concatenate bit vectors in their existing LSB-first order.
Circuit deserialize(const std::string_view json, const std::optional< std::string_view > moduleName)
Deserialize one supported Yosys write_json module into a Silicon circuit.
ModuleDependencyGraph moduleDependencyGraph(const std::string_view json)
Build a module dependency graph from a Yosys write_json document.
std::string serialize(const Circuit &circuit, const std::string_view moduleName)
Serialize a complete Silicon circuit as Yosys write_json-compatible JSON.
nlohmann::ordered_json Json
Transparent equality functor for string-like keys.
Transparent hash functor for string-like keys.
Per-vertex payload holding the declared module name.