2 * yosys -- Yosys Open SYnthesis Suite
4 * Copyright (C) 2012 Aki "lethalbit" Van Ness <aki@yosyshq.com> <aki@lethalbit.net>
6 * Permission to use, copy, modify, and/or distribute this software for any
7 * purpose with or without fee is hereby granted, provided that the above
8 * copyright notice and this permission notice appear in all copies.
10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
20 #include "kernel/rtlil.h"
21 #include "kernel/register.h"
22 #include "kernel/sigtools.h"
23 #include "kernel/celltypes.h"
24 #include "kernel/cellaigs.h"
25 #include "kernel/log.h"
28 #include <unordered_map>
32 PRIVATE_NAMESPACE_BEGIN
41 // XXX(aki): TODO: this needs to be updated to us
42 // dict<T, V> and then coalesce_cells needs to be updated
43 // but for now for the PoC this looks to be sufficient
44 std::unordered_map
<std::string
, std::vector
<Cell
*>> _cells
{};
46 bool _include_connections
;
47 bool _include_attributes
;
48 bool _include_properties
;
50 string
escape_string(string str
) {
53 auto itr
= str
.begin();
55 for(; itr
!= str
.end(); ++itr
) {
100 // XXX(aki): I know this is far from ideal but i'm out of spoons and cant focus so
101 // it'll have to do for now,
102 void coalesce_cells(Module
* mod
)
104 for (auto cell
: mod
->cells()) {
105 const auto cell_type
= escape_string(RTLIL::unescape_id(cell
->type
));
107 if (_cells
.find(cell_type
) == _cells
.end())
108 _cells
.emplace(cell_type
, std::vector
<Cell
*>());
110 _cells
.at(cell_type
).push_back(cell
);
114 // XXX(aki): this is a lazy way to do this i know,,,
115 std::string
gen_indent(const uint16_t level
)
118 for (uint16_t i
= 0; i
<= level
; ++i
)
126 JnyWriter(std::ostream
&f
, bool use_selection
, bool connections
, bool attributes
, bool properties
) noexcept
:
127 f(f
), _use_selection(use_selection
),
128 _include_connections(connections
), _include_attributes(attributes
), _include_properties(properties
)
131 void write_metadata(Design
*design
, uint16_t indent_level
= 0)
133 log_assert(design
!= nullptr);
138 f
<< stringf(" \"generator\": \"%s\",\n", escape_string(yosys_version_str
).c_str());
139 // XXX(aki): Replace this with a proper version info eventually:tm:
140 f
<< " \"version\": \"0.0.0\",\n";
142 f
<< " \"features\": [";
145 if (_include_connections
) {
147 f
<< "\"connections\"";
150 if (_include_attributes
) {
154 f
<< "\"attributes\"";
157 if (_include_properties
) {
161 f
<< "\"properties\"";
166 f
<< " \"modules\": [\n";
169 for (auto mod
: _use_selection
? design
->selected_modules() : design
->modules()) {
172 write_module(mod
, indent_level
+ 2);
181 void write_sigspec(const RTLIL::SigSpec
& sig
, uint16_t indent_level
= 0) {
182 const auto _indent
= gen_indent(indent_level
);
184 f
<< _indent
<< " {\n";
185 f
<< _indent
<< " \"width\": \"" << sig
.size() << "\",\n";
186 f
<< _indent
<< " \"type\": \"";
190 } else if (sig
.is_chunk()) {
192 } else if (sig
.is_bit()) {
199 f
<< _indent
<< " \"const\": ";
200 if (sig
.has_const()) {
208 f
<< _indent
<< " }";
211 void write_mod_conn(const std::pair
<RTLIL::SigSpec
, RTLIL::SigSpec
>& conn
, uint16_t indent_level
= 0) {
212 const auto _indent
= gen_indent(indent_level
);
213 f
<< _indent
<< " {\n";
214 f
<< _indent
<< " \"signals\": [\n";
216 write_sigspec(conn
.first
, indent_level
+ 2);
218 write_sigspec(conn
.second
, indent_level
+ 2);
221 f
<< _indent
<< " ]\n";
222 f
<< _indent
<< " }";
225 void write_cell_conn(const std::pair
<RTLIL::IdString
, RTLIL::SigSpec
>& sig
, uint16_t indent_level
= 0) {
226 const auto _indent
= gen_indent(indent_level
);
227 f
<< _indent
<< " {\n";
228 f
<< _indent
<< " \"name\": \"" << escape_string(RTLIL::unescape_id(sig
.first
)) << "\",\n";
229 f
<< _indent
<< " \"signals\": [\n";
231 write_sigspec(sig
.second
, indent_level
+ 2);
234 f
<< _indent
<< " ]\n";
235 f
<< _indent
<< " }";
238 void write_module(Module
* mod
, uint16_t indent_level
= 0) {
239 log_assert(mod
!= nullptr);
243 const auto _indent
= gen_indent(indent_level
);
245 f
<< _indent
<< "{\n";
246 f
<< stringf(" %s\"name\": \"%s\",\n", _indent
.c_str(), escape_string(RTLIL::unescape_id(mod
->name
)).c_str());
247 f
<< _indent
<< " \"cell_sorts\": [\n";
249 bool first_sort
{true};
250 for (auto& sort
: _cells
) {
253 write_cell_sort(sort
, indent_level
+ 2);
258 f
<< _indent
<< " ]";
259 if (_include_connections
) {
260 f
<< ",\n" << _indent
<< " \"connections\": [\n";
262 bool first_conn
{true};
263 for (const auto& conn
: mod
->connections()) {
267 write_mod_conn(conn
, indent_level
+ 2);
272 f
<< _indent
<< " ]";
274 if (_include_attributes
) {
275 f
<< ",\n" << _indent
<< " \"attributes\": {\n";
277 write_prams(mod
->attributes
, indent_level
+ 2);
280 f
<< _indent
<< " }";
282 f
<< "\n" << _indent
<< "}";
285 void write_cell_ports(RTLIL::Cell
* port_cell
, uint64_t indent_level
= 0) {
286 const auto _indent
= gen_indent(indent_level
);
288 bool first_port
{true};
289 for (auto con
: port_cell
->connections()) {
293 f
<< _indent
<< " {\n";
294 f
<< stringf(" %s\"name\": \"%s\",\n", _indent
.c_str(), escape_string(RTLIL::unescape_id(con
.first
)).c_str());
295 f
<< _indent
<< " \"direction\": \"";
296 if (port_cell
->input(con
.first
))
298 if (port_cell
->input(con
.first
))
301 if (con
.second
.size() == 1)
302 f
<< _indent
<< " \"range\": [0, 0]\n";
304 f
<< stringf(" %s\"range\": [%d, %d]\n", _indent
.c_str(), con
.second
.size(), 0);
305 f
<< _indent
<< " }";
313 void write_cell_sort(std::pair
<const std::string
, std::vector
<Cell
*>>& sort
, uint16_t indent_level
= 0) {
314 const auto port_cell
= sort
.second
.front();
315 const auto _indent
= gen_indent(indent_level
);
317 f
<< _indent
<< "{\n";
318 f
<< stringf(" %s\"type\": %s,\n", _indent
.c_str(), sort
.first
.c_str());
319 f
<< _indent
<< " \"ports\": [\n";
321 write_cell_ports(port_cell
, indent_level
+ 2);
323 f
<< _indent
<< " ],\n" << _indent
<< " \"cells\": [\n";
325 bool first_cell
{true};
326 for (auto& cell
: sort
.second
) {
330 write_cell(cell
, indent_level
+ 2);
336 f
<< _indent
<< " ]\n";
340 void write_param_val(const Const
& v
) {
341 if ((v
.flags
& RTLIL::ConstFlags::CONST_FLAG_STRING
) == RTLIL::ConstFlags::CONST_FLAG_STRING
) {
342 const auto str
= v
.decode_string();
344 // XXX(aki): TODO, uh, yeah
346 f
<< "\"" << escape_string(str
) << "\"";
347 } else if ((v
.flags
& RTLIL::ConstFlags::CONST_FLAG_SIGNED
) == RTLIL::ConstFlags::CONST_FLAG_SIGNED
) {
348 f
<< stringf("\"%dsd %d\"", v
.size(), v
.as_int(true));
349 } else if ((v
.flags
& RTLIL::ConstFlags::CONST_FLAG_REAL
) == RTLIL::ConstFlags::CONST_FLAG_REAL
) {
352 f
<< "\"" << escape_string(v
.as_string()) << "\"";
356 void write_prams(dict
<RTLIL::IdString
, RTLIL::Const
>& params
, uint16_t indent_level
= 0) {
357 const auto _indent
= gen_indent(indent_level
);
359 bool first_param
{true};
360 for (auto& param
: params
) {
363 const auto param_val
= param
.second
;
364 if (!param_val
.empty()) {
365 f
<< stringf(" %s\"%s\": ", _indent
.c_str(), escape_string(RTLIL::unescape_id(param
.first
)).c_str());
366 write_param_val(param_val
);
368 f
<< stringf(" %s\"%s\": true", _indent
.c_str(), escape_string(RTLIL::unescape_id(param
.first
)).c_str());
375 void write_cell(Cell
* cell
, uint16_t indent_level
= 0) {
376 const auto _indent
= gen_indent(indent_level
);
377 log_assert(cell
!= nullptr);
379 f
<< _indent
<< " {\n";
380 f
<< stringf(" %s\"name\": \"%s\"", _indent
.c_str(), escape_string(RTLIL::unescape_id(cell
->name
)).c_str());
382 if (_include_connections
) {
383 f
<< ",\n" << _indent
<< " \"connections\": [\n";
385 bool first_conn
{true};
386 for (const auto& conn
: cell
->connections()) {
390 write_cell_conn(conn
, indent_level
+ 2);
396 f
<< _indent
<< " ]";
399 if (_include_attributes
) {
400 f
<< ",\n" << _indent
<< " \"attributes\": {\n";
402 write_prams(cell
->attributes
, indent_level
+ 2);
405 f
<< _indent
<< " }";
408 if (_include_properties
) {
409 f
<< ",\n" << _indent
<< " \"parameters\": {\n";
411 write_prams(cell
->parameters
, indent_level
+ 2);
414 f
<< _indent
<< " }";
417 f
<< "\n" << _indent
<< " }";
421 struct JnyBackend
: public Backend
{
422 JnyBackend() : Backend("jny", "generate design metadata") { }
423 void help() override
{
424 // XXX(aki): TODO: explicitly document the JSON schema
425 // |---v---|---v---|---v---|---v---|---v---|---v---|---v---|---v---|---v---|---v---|
427 log(" jny [options] [selection]\n");
429 log(" -connections\n");
430 log(" Include connection information in the netlist output.\n");
432 log(" -attributes\n");
433 log(" Include attributed information in the netlist output.\n");
435 log(" -properties\n");
436 log(" Include property information in the netlist output.\n");
438 log("Write a JSON metadata for the current design\n");
443 void execute(std::ostream
*&f
, std::string filename
, std::vector
<std::string
> args
, RTLIL::Design
*design
) override
{
445 bool connections
{false};
446 bool attributes
{false};
447 bool properties
{false};
450 for (; argidx
< args
.size(); argidx
++) {
451 if (args
[argidx
] == "-connections") {
456 if (args
[argidx
] == "-attributes") {
461 if (args
[argidx
] == "-properties") {
468 extra_args(f
, filename
, args
, argidx
);
470 log_header(design
, "Executing jny backend.\n");
472 JnyWriter
jny_writer(*f
, false, connections
, attributes
, properties
);
473 jny_writer
.write_metadata(design
);
479 struct JnyPass
: public Pass
{
480 JnyPass() : Pass("jny", "write design and metadata") { }
482 void help() override
{
483 // |---v---|---v---|---v---|---v---|---v---|---v---|---v---|---v---|---v---|---v---|
485 log(" jny [options] [selection]\n");
487 log("Write a JSON netlist metadata for the current design\n");
489 log(" -o <filename>\n");
490 log(" write to the specified file.\n");
492 log(" -connections\n");
493 log(" Include connection information in the netlist output.\n");
495 log(" -attributes\n");
496 log(" Include attributed information in the netlist output.\n");
498 log(" -properties\n");
499 log(" Include property information in the netlist output.\n");
501 log("See 'help write_jny' for a description of the JSON format used.\n");
504 void execute(std::vector
<std::string
> args
, RTLIL::Design
*design
) override
{
505 std::string filename
{};
507 bool connections
{false};
508 bool attributes
{false};
509 bool properties
{false};
512 for (; argidx
< args
.size(); argidx
++) {
513 if (args
[argidx
] == "-o" && argidx
+1 < args
.size()) {
514 filename
= args
[++argidx
];
518 if (args
[argidx
] == "-connections") {
523 if (args
[argidx
] == "-attributes") {
528 if (args
[argidx
] == "-properties") {
535 extra_args(args
, argidx
, design
);
538 std::stringstream buf
;
540 if (!filename
.empty()) {
541 rewrite_filename(filename
);
542 std::ofstream
*ff
= new std::ofstream
;
543 ff
->open(filename
.c_str(), std::ofstream::trunc
);
546 log_error("Can't open file `%s' for writing: %s\n", filename
.c_str(), strerror(errno
));
554 JnyWriter
jny_writer(*f
, false, connections
, attributes
, properties
);
555 jny_writer
.write_metadata(design
);
557 if (!filename
.empty()) {
560 log("%s", buf
.str().c_str());
566 PRIVATE_NAMESPACE_END