Merge pull request #1974 from YosysHQ/eddie/abc9_disable_mfs
[yosys.git] / backends / json / json.cc
1 /*
2 * yosys -- Yosys Open SYnthesis Suite
3 *
4 * Copyright (C) 2012 Clifford Wolf <clifford@clifford.at>
5 *
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.
9 *
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.
17 *
18 */
19
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"
26 #include <string>
27
28 USING_YOSYS_NAMESPACE
29 PRIVATE_NAMESPACE_BEGIN
30
31 struct JsonWriter
32 {
33 std::ostream &f;
34 bool use_selection;
35 bool aig_mode;
36 bool compat_int_mode;
37
38 Design *design;
39 Module *module;
40
41 SigMap sigmap;
42 int sigidcounter;
43 dict<SigBit, string> sigids;
44 pool<Aig> aig_models;
45
46 JsonWriter(std::ostream &f, bool use_selection, bool aig_mode, bool compat_int_mode) :
47 f(f), use_selection(use_selection), aig_mode(aig_mode),
48 compat_int_mode(compat_int_mode) { }
49
50 string get_string(string str)
51 {
52 string newstr = "\"";
53 for (char c : str) {
54 if (c == '\\')
55 newstr += c;
56 newstr += c;
57 }
58 return newstr + "\"";
59 }
60
61 string get_name(IdString name)
62 {
63 return get_string(RTLIL::unescape_id(name));
64 }
65
66 string get_bits(SigSpec sig)
67 {
68 bool first = true;
69 string str = "[";
70 for (auto bit : sigmap(sig)) {
71 str += first ? " " : ", ";
72 first = false;
73 if (sigids.count(bit) == 0) {
74 string &s = sigids[bit];
75 if (bit.wire == nullptr) {
76 if (bit == State::S0) s = "\"0\"";
77 else if (bit == State::S1) s = "\"1\"";
78 else if (bit == State::Sz) s = "\"z\"";
79 else s = "\"x\"";
80 } else
81 s = stringf("%d", sigidcounter++);
82 }
83 str += sigids[bit];
84 }
85 return str + " ]";
86 }
87
88 void write_parameter_value(const Const &value)
89 {
90 if ((value.flags & RTLIL::ConstFlags::CONST_FLAG_STRING) != 0) {
91 string str = value.decode_string();
92 int state = 0;
93 for (char c : str) {
94 if (state == 0) {
95 if (c == '0' || c == '1' || c == 'x' || c == 'z')
96 state = 0;
97 else if (c == ' ')
98 state = 1;
99 else
100 state = 2;
101 } else if (state == 1 && c != ' ')
102 state = 2;
103 }
104 if (state < 2)
105 str += " ";
106 f << get_string(str);
107 } else if (compat_int_mode && GetSize(value) <= 32 && value.is_fully_def()) {
108 if ((value.flags & RTLIL::ConstFlags::CONST_FLAG_SIGNED) != 0)
109 f << stringf("%d", value.as_int());
110 else
111 f << stringf("%u", value.as_int());
112 } else {
113 f << get_string(value.as_string());
114 }
115 }
116
117 void write_parameters(const dict<IdString, Const> &parameters, bool for_module=false)
118 {
119 bool first = true;
120 for (auto &param : parameters) {
121 f << stringf("%s\n", first ? "" : ",");
122 f << stringf(" %s%s: ", for_module ? "" : " ", get_name(param.first).c_str());
123 write_parameter_value(param.second);
124 first = false;
125 }
126 }
127
128 void write_module(Module *module_)
129 {
130 module = module_;
131 log_assert(module->design == design);
132 sigmap.set(module);
133 sigids.clear();
134
135 // reserve 0 and 1 to avoid confusion with "0" and "1"
136 sigidcounter = 2;
137
138 f << stringf(" %s: {\n", get_name(module->name).c_str());
139
140 f << stringf(" \"attributes\": {");
141 write_parameters(module->attributes, /*for_module=*/true);
142 f << stringf("\n },\n");
143
144 if (module->parameter_default_values.size()) {
145 f << stringf(" \"parameter_default_values\": {");
146 write_parameters(module->parameter_default_values, /*for_module=*/true);
147 f << stringf("\n },\n");
148 }
149
150 f << stringf(" \"ports\": {");
151 bool first = true;
152 for (auto n : module->ports) {
153 Wire *w = module->wire(n);
154 if (use_selection && !module->selected(w))
155 continue;
156 f << stringf("%s\n", first ? "" : ",");
157 f << stringf(" %s: {\n", get_name(n).c_str());
158 f << stringf(" \"direction\": \"%s\",\n", w->port_input ? w->port_output ? "inout" : "input" : "output");
159 if (w->start_offset)
160 f << stringf(" \"offset\": %d,\n", w->start_offset);
161 if (w->upto)
162 f << stringf(" \"upto\": 1,\n");
163 f << stringf(" \"bits\": %s\n", get_bits(w).c_str());
164 f << stringf(" }");
165 first = false;
166 }
167 f << stringf("\n },\n");
168
169 f << stringf(" \"cells\": {");
170 first = true;
171 for (auto c : module->cells()) {
172 if (use_selection && !module->selected(c))
173 continue;
174 f << stringf("%s\n", first ? "" : ",");
175 f << stringf(" %s: {\n", get_name(c->name).c_str());
176 f << stringf(" \"hide_name\": %s,\n", c->name[0] == '$' ? "1" : "0");
177 f << stringf(" \"type\": %s,\n", get_name(c->type).c_str());
178 if (aig_mode) {
179 Aig aig(c);
180 if (!aig.name.empty()) {
181 f << stringf(" \"model\": \"%s\",\n", aig.name.c_str());
182 aig_models.insert(aig);
183 }
184 }
185 f << stringf(" \"parameters\": {");
186 write_parameters(c->parameters);
187 f << stringf("\n },\n");
188 f << stringf(" \"attributes\": {");
189 write_parameters(c->attributes);
190 f << stringf("\n },\n");
191 if (c->known()) {
192 f << stringf(" \"port_directions\": {");
193 bool first2 = true;
194 for (auto &conn : c->connections()) {
195 string direction = "output";
196 if (c->input(conn.first))
197 direction = c->output(conn.first) ? "inout" : "input";
198 f << stringf("%s\n", first2 ? "" : ",");
199 f << stringf(" %s: \"%s\"", get_name(conn.first).c_str(), direction.c_str());
200 first2 = false;
201 }
202 f << stringf("\n },\n");
203 }
204 f << stringf(" \"connections\": {");
205 bool first2 = true;
206 for (auto &conn : c->connections()) {
207 f << stringf("%s\n", first2 ? "" : ",");
208 f << stringf(" %s: %s", get_name(conn.first).c_str(), get_bits(conn.second).c_str());
209 first2 = false;
210 }
211 f << stringf("\n }\n");
212 f << stringf(" }");
213 first = false;
214 }
215 f << stringf("\n },\n");
216
217 f << stringf(" \"netnames\": {");
218 first = true;
219 for (auto w : module->wires()) {
220 if (use_selection && !module->selected(w))
221 continue;
222 f << stringf("%s\n", first ? "" : ",");
223 f << stringf(" %s: {\n", get_name(w->name).c_str());
224 f << stringf(" \"hide_name\": %s,\n", w->name[0] == '$' ? "1" : "0");
225 f << stringf(" \"bits\": %s,\n", get_bits(w).c_str());
226 if (w->start_offset)
227 f << stringf(" \"offset\": %d,\n", w->start_offset);
228 if (w->upto)
229 f << stringf(" \"upto\": 1,\n");
230 f << stringf(" \"attributes\": {");
231 write_parameters(w->attributes);
232 f << stringf("\n }\n");
233 f << stringf(" }");
234 first = false;
235 }
236 f << stringf("\n }\n");
237
238 f << stringf(" }");
239 }
240
241 void write_design(Design *design_)
242 {
243 design = design_;
244 design->sort();
245
246 f << stringf("{\n");
247 f << stringf(" \"creator\": %s,\n", get_string(yosys_version_str).c_str());
248 f << stringf(" \"modules\": {\n");
249 vector<Module*> modules = use_selection ? design->selected_modules() : design->modules();
250 bool first_module = true;
251 for (auto mod : modules) {
252 if (!first_module)
253 f << stringf(",\n");
254 write_module(mod);
255 first_module = false;
256 }
257 f << stringf("\n }");
258 if (!aig_models.empty()) {
259 f << stringf(",\n \"models\": {\n");
260 bool first_model = true;
261 for (auto &aig : aig_models) {
262 if (!first_model)
263 f << stringf(",\n");
264 f << stringf(" \"%s\": [\n", aig.name.c_str());
265 int node_idx = 0;
266 for (auto &node : aig.nodes) {
267 if (node_idx != 0)
268 f << stringf(",\n");
269 f << stringf(" /* %3d */ [ ", node_idx);
270 if (node.portbit >= 0)
271 f << stringf("\"%sport\", \"%s\", %d", node.inverter ? "n" : "",
272 log_id(node.portname), node.portbit);
273 else if (node.left_parent < 0 && node.right_parent < 0)
274 f << stringf("\"%s\"", node.inverter ? "true" : "false");
275 else
276 f << stringf("\"%s\", %d, %d", node.inverter ? "nand" : "and", node.left_parent, node.right_parent);
277 for (auto &op : node.outports)
278 f << stringf(", \"%s\", %d", log_id(op.first), op.second);
279 f << stringf(" ]");
280 node_idx++;
281 }
282 f << stringf("\n ]");
283 first_model = false;
284 }
285 f << stringf("\n }");
286 }
287 f << stringf("\n}\n");
288 }
289 };
290
291 struct JsonBackend : public Backend {
292 JsonBackend() : Backend("json", "write design to a JSON file") { }
293 void help() YS_OVERRIDE
294 {
295 // |---v---|---v---|---v---|---v---|---v---|---v---|---v---|---v---|---v---|---v---|
296 log("\n");
297 log(" write_json [options] [filename]\n");
298 log("\n");
299 log("Write a JSON netlist of the current design.\n");
300 log("\n");
301 log(" -aig\n");
302 log(" include AIG models for the different gate types\n");
303 log("\n");
304 log(" -compat-int\n");
305 log(" emit 32-bit or smaller fully-defined parameter values directly\n");
306 log(" as JSON numbers (for compatibility with old parsers)\n");
307 log("\n");
308 log("\n");
309 log("The general syntax of the JSON output created by this command is as follows:\n");
310 log("\n");
311 log(" {\n");
312 log(" \"creator\": \"Yosys <version info>\",\n");
313 log(" \"modules\": {\n");
314 log(" <module_name>: {\n");
315 log(" \"attributes\": {\n");
316 log(" <attribute_name>: <attribute_value>,\n");
317 log(" ...\n");
318 log(" },\n");
319 log(" \"parameter_default_values\": {\n");
320 log(" <parameter_name>: <parameter_value>,\n");
321 log(" ...\n");
322 log(" },\n");
323 log(" \"ports\": {\n");
324 log(" <port_name>: <port_details>,\n");
325 log(" ...\n");
326 log(" },\n");
327 log(" \"cells\": {\n");
328 log(" <cell_name>: <cell_details>,\n");
329 log(" ...\n");
330 log(" },\n");
331 log(" \"netnames\": {\n");
332 log(" <net_name>: <net_details>,\n");
333 log(" ...\n");
334 log(" }\n");
335 log(" }\n");
336 log(" },\n");
337 log(" \"models\": {\n");
338 log(" ...\n");
339 log(" },\n");
340 log(" }\n");
341 log("\n");
342 log("Where <port_details> is:\n");
343 log("\n");
344 log(" {\n");
345 log(" \"direction\": <\"input\" | \"output\" | \"inout\">,\n");
346 log(" \"bits\": <bit_vector>\n");
347 log(" \"offset\": <the lowest bit index in use, if non-0>\n");
348 log(" \"upto\": <1 if the port bit indexing is MSB-first>\n");
349 log(" }\n");
350 log("\n");
351 log("The \"offset\" and \"upto\" fields are skipped if their value would be 0.");
352 log("They don't affect connection semantics, and are only used to preserve original");
353 log("HDL bit indexing.");
354 log("And <cell_details> is:\n");
355 log("\n");
356 log(" {\n");
357 log(" \"hide_name\": <1 | 0>,\n");
358 log(" \"type\": <cell_type>,\n");
359 log(" \"model\": <AIG model name, if -aig option used>,\n");
360 log(" \"parameters\": {\n");
361 log(" <parameter_name>: <parameter_value>,\n");
362 log(" ...\n");
363 log(" },\n");
364 log(" \"attributes\": {\n");
365 log(" <attribute_name>: <attribute_value>,\n");
366 log(" ...\n");
367 log(" },\n");
368 log(" \"port_directions\": {\n");
369 log(" <port_name>: <\"input\" | \"output\" | \"inout\">,\n");
370 log(" ...\n");
371 log(" },\n");
372 log(" \"connections\": {\n");
373 log(" <port_name>: <bit_vector>,\n");
374 log(" ...\n");
375 log(" },\n");
376 log(" }\n");
377 log("\n");
378 log("And <net_details> is:\n");
379 log("\n");
380 log(" {\n");
381 log(" \"hide_name\": <1 | 0>,\n");
382 log(" \"bits\": <bit_vector>\n");
383 log(" \"offset\": <the lowest bit index in use, if non-0>\n");
384 log(" \"upto\": <1 if the port bit indexing is MSB-first>\n");
385 log(" }\n");
386 log("\n");
387 log("The \"hide_name\" fields are set to 1 when the name of this cell or net is\n");
388 log("automatically created and is likely not of interest for a regular user.\n");
389 log("\n");
390 log("The \"port_directions\" section is only included for cells for which the\n");
391 log("interface is known.\n");
392 log("\n");
393 log("Module and cell ports and nets can be single bit wide or vectors of multiple\n");
394 log("bits. Each individual signal bit is assigned a unique integer. The <bit_vector>\n");
395 log("values referenced above are vectors of this integers. Signal bits that are\n");
396 log("connected to a constant driver are denoted as string \"0\", \"1\", \"x\", or\n");
397 log("\"z\" instead of a number.\n");
398 log("\n");
399 log("Bit vectors (including integers) are written as string holding the binary");
400 log("representation of the value. Strings are written as strings, with an appended");
401 log("blank in cases of strings of the form /[01xz]* */.\n");
402 log("\n");
403 log("For example the following Verilog code:\n");
404 log("\n");
405 log(" module test(input x, y);\n");
406 log(" (* keep *) foo #(.P(42), .Q(1337))\n");
407 log(" foo_inst (.A({x, y}), .B({y, x}), .C({4'd10, {4{x}}}));\n");
408 log(" endmodule\n");
409 log("\n");
410 log("Translates to the following JSON output:\n");
411 log("\n");
412
413 log(" {\n");
414 log(" \"creator\": \"Yosys 0.9+2406 (git sha1 fb1168d8, clang 9.0.1 -fPIC -Os)\",\n");
415 log(" \"modules\": {\n");
416 log(" \"test\": {\n");
417 log(" \"attributes\": {\n");
418 log(" \"cells_not_processed\": \"00000000000000000000000000000001\",\n");
419 log(" \"src\": \"test.v:1.1-4.10\"\n");
420 log(" },\n");
421 log(" \"ports\": {\n");
422 log(" \"x\": {\n");
423 log(" \"direction\": \"input\",\n");
424 log(" \"bits\": [ 2 ]\n");
425 log(" },\n");
426 log(" \"y\": {\n");
427 log(" \"direction\": \"input\",\n");
428 log(" \"bits\": [ 3 ]\n");
429 log(" }\n");
430 log(" },\n");
431 log(" \"cells\": {\n");
432 log(" \"foo_inst\": {\n");
433 log(" \"hide_name\": 0,\n");
434 log(" \"type\": \"foo\",\n");
435 log(" \"parameters\": {\n");
436 log(" \"P\": \"00000000000000000000000000101010\",\n");
437 log(" \"Q\": \"00000000000000000000010100111001\"\n");
438 log(" },\n");
439 log(" \"attributes\": {\n");
440 log(" \"keep\": \"00000000000000000000000000000001\",\n");
441 log(" \"module_not_derived\": \"00000000000000000000000000000001\",\n");
442 log(" \"src\": \"test.v:3.1-3.55\"\n");
443 log(" },\n");
444 log(" \"connections\": {\n");
445 log(" \"A\": [ 3, 2 ],\n");
446 log(" \"B\": [ 2, 3 ],\n");
447 log(" \"C\": [ 2, 2, 2, 2, \"0\", \"1\", \"0\", \"1\" ]\n");
448 log(" }\n");
449 log(" }\n");
450 log(" },\n");
451 log(" \"netnames\": {\n");
452 log(" \"x\": {\n");
453 log(" \"hide_name\": 0,\n");
454 log(" \"bits\": [ 2 ],\n");
455 log(" \"attributes\": {\n");
456 log(" \"src\": \"test.v:1.19-1.20\"\n");
457 log(" }\n");
458 log(" },\n");
459 log(" \"y\": {\n");
460 log(" \"hide_name\": 0,\n");
461 log(" \"bits\": [ 3 ],\n");
462 log(" \"attributes\": {\n");
463 log(" \"src\": \"test.v:1.22-1.23\"\n");
464 log(" }\n");
465 log(" }\n");
466 log(" }\n");
467 log(" }\n");
468 log(" }\n");
469 log(" }\n");
470 log("\n");
471 log("The models are given as And-Inverter-Graphs (AIGs) in the following form:\n");
472 log("\n");
473 log(" \"models\": {\n");
474 log(" <model_name>: [\n");
475 log(" /* 0 */ [ <node-spec> ],\n");
476 log(" /* 1 */ [ <node-spec> ],\n");
477 log(" /* 2 */ [ <node-spec> ],\n");
478 log(" ...\n");
479 log(" ],\n");
480 log(" ...\n");
481 log(" },\n");
482 log("\n");
483 log("The following node-types may be used:\n");
484 log("\n");
485 log(" [ \"port\", <portname>, <bitindex>, <out-list> ]\n");
486 log(" - the value of the specified input port bit\n");
487 log("\n");
488 log(" [ \"nport\", <portname>, <bitindex>, <out-list> ]\n");
489 log(" - the inverted value of the specified input port bit\n");
490 log("\n");
491 log(" [ \"and\", <node-index>, <node-index>, <out-list> ]\n");
492 log(" - the ANDed value of the specified nodes\n");
493 log("\n");
494 log(" [ \"nand\", <node-index>, <node-index>, <out-list> ]\n");
495 log(" - the inverted ANDed value of the specified nodes\n");
496 log("\n");
497 log(" [ \"true\", <out-list> ]\n");
498 log(" - the constant value 1\n");
499 log("\n");
500 log(" [ \"false\", <out-list> ]\n");
501 log(" - the constant value 0\n");
502 log("\n");
503 log("All nodes appear in topological order. I.e. only nodes with smaller indices\n");
504 log("are referenced by \"and\" and \"nand\" nodes.\n");
505 log("\n");
506 log("The optional <out-list> at the end of a node specification is a list of\n");
507 log("output portname and bitindex pairs, specifying the outputs driven by this node.\n");
508 log("\n");
509 log("For example, the following is the model for a 3-input 3-output $reduce_and cell\n");
510 log("inferred by the following code:\n");
511 log("\n");
512 log(" module test(input [2:0] in, output [2:0] out);\n");
513 log(" assign in = &out;\n");
514 log(" endmodule\n");
515 log("\n");
516 log(" \"$reduce_and:3U:3\": [\n");
517 log(" /* 0 */ [ \"port\", \"A\", 0 ],\n");
518 log(" /* 1 */ [ \"port\", \"A\", 1 ],\n");
519 log(" /* 2 */ [ \"and\", 0, 1 ],\n");
520 log(" /* 3 */ [ \"port\", \"A\", 2 ],\n");
521 log(" /* 4 */ [ \"and\", 2, 3, \"Y\", 0 ],\n");
522 log(" /* 5 */ [ \"false\", \"Y\", 1, \"Y\", 2 ]\n");
523 log(" ]\n");
524 log("\n");
525 log("Future version of Yosys might add support for additional fields in the JSON\n");
526 log("format. A program processing this format must ignore all unknown fields.\n");
527 log("\n");
528 }
529 void execute(std::ostream *&f, std::string filename, std::vector<std::string> args, RTLIL::Design *design) YS_OVERRIDE
530 {
531 bool aig_mode = false;
532 bool compat_int_mode = false;
533
534 size_t argidx;
535 for (argidx = 1; argidx < args.size(); argidx++)
536 {
537 if (args[argidx] == "-aig") {
538 aig_mode = true;
539 continue;
540 }
541 if (args[argidx] == "-compat-int") {
542 compat_int_mode = true;
543 continue;
544 }
545 break;
546 }
547 extra_args(f, filename, args, argidx);
548
549 log_header(design, "Executing JSON backend.\n");
550
551 JsonWriter json_writer(*f, false, aig_mode, compat_int_mode);
552 json_writer.write_design(design);
553 }
554 } JsonBackend;
555
556 struct JsonPass : public Pass {
557 JsonPass() : Pass("json", "write design in JSON format") { }
558 void help() YS_OVERRIDE
559 {
560 // |---v---|---v---|---v---|---v---|---v---|---v---|---v---|---v---|---v---|---v---|
561 log("\n");
562 log(" json [options] [selection]\n");
563 log("\n");
564 log("Write a JSON netlist of all selected objects.\n");
565 log("\n");
566 log(" -o <filename>\n");
567 log(" write to the specified file.\n");
568 log("\n");
569 log(" -aig\n");
570 log(" also include AIG models for the different gate types\n");
571 log("\n");
572 log(" -compat-int\n");
573 log(" emit 32-bit or smaller fully-defined parameter values directly\n");
574 log(" as JSON numbers (for compatibility with old parsers)\n");
575 log("\n");
576 log("See 'help write_json' for a description of the JSON format used.\n");
577 log("\n");
578 }
579 void execute(std::vector<std::string> args, RTLIL::Design *design) YS_OVERRIDE
580 {
581 std::string filename;
582 bool aig_mode = false;
583 bool compat_int_mode = false;
584
585 size_t argidx;
586 for (argidx = 1; argidx < args.size(); argidx++)
587 {
588 if (args[argidx] == "-o" && argidx+1 < args.size()) {
589 filename = args[++argidx];
590 continue;
591 }
592 if (args[argidx] == "-aig") {
593 aig_mode = true;
594 continue;
595 }
596 if (args[argidx] == "-compat-int") {
597 compat_int_mode = true;
598 continue;
599 }
600 break;
601 }
602 extra_args(args, argidx, design);
603
604 std::ostream *f;
605 std::stringstream buf;
606
607 if (!filename.empty()) {
608 rewrite_filename(filename);
609 std::ofstream *ff = new std::ofstream;
610 ff->open(filename.c_str(), std::ofstream::trunc);
611 if (ff->fail()) {
612 delete ff;
613 log_error("Can't open file `%s' for writing: %s\n", filename.c_str(), strerror(errno));
614 }
615 f = ff;
616 } else {
617 f = &buf;
618 }
619
620 JsonWriter json_writer(*f, true, aig_mode, compat_int_mode);
621 json_writer.write_design(design);
622
623 if (!filename.empty()) {
624 delete f;
625 } else {
626 log("%s", buf.str().c_str());
627 }
628 }
629 } JsonPass;
630
631 PRIVATE_NAMESPACE_END