46 [[nodiscard]] std::string withCircuitModuleNames(std::string json)
48 auto design = SILICON::yosys::Json::parse(json);
49 auto& modules = design.at(
"modules");
50 std::unordered_set<std::string> reserved;
51 std::map<std::string, std::string> replacements;
52 for (
const auto& [name, _module] : modules.items()) {
53 reserved.insert(name);
56 std::string candidate = name;
57 for (
char& c : candidate)
58 if (!((c >=
'a' && c <=
'z') || (c >=
'A' && c <=
'Z') || (c >=
'0' && c <=
'9')
61 if (candidate.empty())
63 if (candidate.front() >=
'0' && candidate.front() <=
'9')
64 candidate.insert(0,
"module_");
65 replacements.emplace(name, std::move(candidate));
67 if (replacements.empty())
71 for (
auto& [_name, replacement] : replacements) {
72 const auto base = replacement;
73 for (std::size_t suffix = 2; reserved.contains(replacement); ++suffix)
74 replacement = std::format(
"{}_{}", base, suffix);
75 reserved.insert(replacement);
77 for (
auto& module : modules)
78 for (
auto& cell : module.at(
"cells"))
79 if (
const auto replacement =
80 replacements.find(cell.at(
"type").get<std::string>());
81 replacement != replacements.end())
82 cell[
"type"] = replacement->second;
84 auto renamed = SILICON::yosys::Json::object();
85 for (
auto& [name, module] : modules.items()) {
86 const auto replacement = replacements.find(name);
87 renamed[replacement == replacements.end() ? name : replacement->second] =
90 modules = std::move(renamed);
95 prepareCircuitToVerilog(
const Document& source,
96 const std::span<const Document> projectDocuments,
101 if (source.getType() != DocumentType::Circuit || !slug)
102 throw std::invalid_argument(
"Only circuits can be converted to Verilog");
105 registry, &resolver);
106 for (
const auto& [component, vertex] : circuit.getComponentToVertex()) {
107 const auto rom = std::dynamic_pointer_cast<SILICON::core::ROM>(
108 circuit.getComponentByVertexId(vertex));
111 const auto slug = rom->getPropertyValue<std::string>(
"binaryContents");
112 std::shared_ptr<const std::string> snapshot;
115 for (
const auto& document : projectDocuments)
116 if (document.getPath() == path)
117 snapshot = std::make_shared<const std::string>(document.getContents());
119 rom->refreshBinaryContents(std::move(snapshot));
127 circuit = std::move(circuit)](
const std::span<const std::string> selected) {
128 if (!selected.empty())
129 throw std::invalid_argument(
130 "Circuit-to-Verilog conversion does not accept selections");
131 std::vector<SemanticDocument> documents;
137 .activatePath = path};
143 prepareVerilogToCircuits(
const Document& source,
144 const std::span<const Document> projectDocuments)
146 if (source.getType() != DocumentType::Verilog)
147 throw std::invalid_argument(
"Only Verilog files can be converted to circuits");
149 std::vector<SILICON::core::SourceFile> sources;
150 for (
const auto& document : projectDocuments) {
151 if (document.getType() == DocumentType::Verilog)
153 {.path = document.getPath(), .contents = document.getContents()});
162 if (design.modules.
modules().empty())
163 throw std::runtime_error(
"Verilog source must declare at least one module");
165 const auto allModules = design.modules.
modules();
168 std::vector<ConversionChoice> choices;
169 choices.reserve(allModules.size());
170 for (
const auto& module : allModules) {
171 choices.push_back({.id = module,
177 .choices = std::move(choices),
179 [design = std::move(design)](
const std::span<const std::string> selected) {
180 if (selected.empty())
181 throw std::invalid_argument(
"At least one module must be selected");
183 const std::vector<std::string> roots(selected.begin(), selected.end());
186 std::vector<SemanticDocument> generated;
187 generated.reserve(exportOrder.size());
188 for (
const auto& module : exportOrder) {
190 throw std::runtime_error(std::format(
191 "Verilog module '{}' cannot be used as a project circuit name",
195 for (
const auto& [component, vertex] : circuit.getComponentToVertex()) {
196 const auto rom = std::dynamic_pointer_cast<SILICON::core::ROM>(
197 circuit.getComponentByVertexId(vertex));
200 const auto slug = rom->getPropertyValue<std::string>(
"binaryContents");
201 const auto contents = rom->binaryContentsSnapshot();
202 if (!slug || !contents)
203 throw std::runtime_error(
"Imported ROM has no binary contents");
205 const auto existing = std::ranges::find_if(generated, [&](
const SemanticDocument& item) {
return item.
path == path; });
206 if (existing == generated.end())
207 generated.push_back({.path = path, .payload =
BinarySource{*contents}});
208 else if (std::get<BinarySource>(existing->payload).contents != *contents)
209 throw std::runtime_error(
"Conflicting ROM binary document names");
211 generated.push_back({
214 .payload = std::move(circuit),
219 .documents = std::move(generated),
221 DocumentType::Circuit, roots.front()),
231 const std::span<const Document> projectDocuments,
235 if (source.getType() == DocumentType::Circuit && target == DocumentType::Verilog)
236 return prepareCircuitToVerilog(source, projectDocuments, registry, resolver);
237 if (source.getType() == DocumentType::Verilog && target == DocumentType::Circuit)
238 return prepareVerilogToCircuits(source, projectDocuments);
239 throw std::invalid_argument(
"The selected document has no registered conversion");