Merge branch 'master' into clk2ff-better-names
[yosys.git] / backends / json / json.cc
1 /*
2 * yosys -- Yosys Open SYnthesis Suite
3 *
4 * Copyright (C) 2012 Claire Xenia Wolf <claire@yosyshq.com>
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 if (module->has_processes()) {
139 log_error("Module %s contains processes, which are not supported by JSON backend (run `proc` first).\n", log_id(module));
140 }
141
142 f << stringf(" %s: {\n", get_name(module->name).c_str());
143
144 f << stringf(" \"attributes\": {");
145 write_parameters(module->attributes, /*for_module=*/true);
146 f << stringf("\n },\n");
147
148 if (module->parameter_default_values.size()) {
149 f << stringf(" \"parameter_default_values\": {");
150 write_parameters(module->parameter_default_values, /*for_module=*/true);
151 f << stringf("\n },\n");
152 }
153
154 f << stringf(" \"ports\": {");
155 bool first = true;
156 for (auto n : module->ports) {
157 Wire *w = module->wire(n);
158 if (use_selection && !module->selected(w))
159 continue;
160 f << stringf("%s\n", first ? "" : ",");
161 f << stringf(" %s: {\n", get_name(n).c_str());
162 f << stringf(" \"direction\": \"%s\",\n", w->port_input ? w->port_output ? "inout" : "input" : "output");
163 if (w->start_offset)
164 f << stringf(" \"offset\": %d,\n", w->start_offset);
165 if (w->upto)
166 f << stringf(" \"upto\": 1,\n");
167 if (w->is_signed)
168 f << stringf(" \"signed\": %d,\n", w->is_signed);
169 f << stringf(" \"bits\": %s\n", get_bits(w).c_str());
170 f << stringf(" }");
171 first = false;
172 }
173 f << stringf("\n },\n");
174
175 f << stringf(" \"cells\": {");
176 first = true;
177 for (auto c : module->cells()) {
178 if (use_selection && !module->selected(c))
179 continue;
180 f << stringf("%s\n", first ? "" : ",");
181 f << stringf(" %s: {\n", get_name(c->name).c_str());
182 f << stringf(" \"hide_name\": %s,\n", c->name[0] == '$' ? "1" : "0");
183 f << stringf(" \"type\": %s,\n", get_name(c->type).c_str());
184 if (aig_mode) {
185 Aig aig(c);
186 if (!aig.name.empty()) {
187 f << stringf(" \"model\": \"%s\",\n", aig.name.c_str());
188 aig_models.insert(aig);
189 }
190 }
191 f << stringf(" \"parameters\": {");
192 write_parameters(c->parameters);
193 f << stringf("\n },\n");
194 f << stringf(" \"attributes\": {");
195 write_parameters(c->attributes);
196 f << stringf("\n },\n");
197 if (c->known()) {
198 f << stringf(" \"port_directions\": {");
199 bool first2 = true;
200 for (auto &conn : c->connections()) {
201 string direction = "output";
202 if (c->input(conn.first))
203 direction = c->output(conn.first) ? "inout" : "input";
204 f << stringf("%s\n", first2 ? "" : ",");
205 f << stringf(" %s: \"%s\"", get_name(conn.first).c_str(), direction.c_str());
206 first2 = false;
207 }
208 f << stringf("\n },\n");
209 }
210 f << stringf(" \"connections\": {");
211 bool first2 = true;
212 for (auto &conn : c->connections()) {
213 f << stringf("%s\n", first2 ? "" : ",");
214 f << stringf(" %s: %s", get_name(conn.first).c_str(), get_bits(conn.second).c_str());
215 first2 = false;
216 }
217 f << stringf("\n }\n");
218 f << stringf(" }");
219 first = false;
220 }
221 f << stringf("\n },\n");
222
223 if (!module->memories.empty()) {
224 f << stringf(" \"memories\": {");
225 first = true;
226 for (auto &it : module->memories) {
227 if (use_selection && !module->selected(it.second))
228 continue;
229 f << stringf("%s\n", first ? "" : ",");
230 f << stringf(" %s: {\n", get_name(it.second->name).c_str());
231 f << stringf(" \"hide_name\": %s,\n", it.second->name[0] == '$' ? "1" : "0");
232 f << stringf(" \"attributes\": {");
233 write_parameters(it.second->attributes);
234 f << stringf("\n },\n");
235 f << stringf(" \"width\": %d,\n", it.second->width);
236 f << stringf(" \"start_offset\": %d,\n", it.second->start_offset);
237 f << stringf(" \"size\": %d\n", it.second->size);
238 f << stringf(" }");
239 first = false;
240 }
241 f << stringf("\n },\n");
242 }
243
244 f << stringf(" \"netnames\": {");
245 first = true;
246 for (auto w : module->wires()) {
247 if (use_selection && !module->selected(w))
248 continue;
249 f << stringf("%s\n", first ? "" : ",");
250 f << stringf(" %s: {\n", get_name(w->name).c_str());
251 f << stringf(" \"hide_name\": %s,\n", w->name[0] == '$' ? "1" : "0");
252 f << stringf(" \"bits\": %s,\n", get_bits(w).c_str());
253 if (w->start_offset)
254 f << stringf(" \"offset\": %d,\n", w->start_offset);
255 if (w->upto)
256 f << stringf(" \"upto\": 1,\n");
257 if (w->is_signed)
258 f << stringf(" \"signed\": %d,\n", w->is_signed);
259 f << stringf(" \"attributes\": {");
260 write_parameters(w->attributes);
261 f << stringf("\n }\n");
262 f << stringf(" }");
263 first = false;
264 }
265 f << stringf("\n }\n");
266
267 f << stringf(" }");
268 }
269
270 void write_design(Design *design_)
271 {
272 design = design_;
273 design->sort();
274
275 f << stringf("{\n");
276 f << stringf(" \"creator\": %s,\n", get_string(yosys_version_str).c_str());
277 f << stringf(" \"modules\": {\n");
278 vector<Module*> modules = use_selection ? design->selected_modules() : design->modules();
279 bool first_module = true;
280 for (auto mod : modules) {
281 if (!first_module)
282 f << stringf(",\n");
283 write_module(mod);
284 first_module = false;
285 }
286 f << stringf("\n }");
287 if (!aig_models.empty()) {
288 f << stringf(",\n \"models\": {\n");
289 bool first_model = true;
290 for (auto &aig : aig_models) {
291 if (!first_model)
292 f << stringf(",\n");
293 f << stringf(" \"%s\": [\n", aig.name.c_str());
294 int node_idx = 0;
295 for (auto &node : aig.nodes) {
296 if (node_idx != 0)
297 f << stringf(",\n");
298 f << stringf(" /* %3d */ [ ", node_idx);
299 if (node.portbit >= 0)
300 f << stringf("\"%sport\", \"%s\", %d", node.inverter ? "n" : "",
301 log_id(node.portname), node.portbit);
302 else if (node.left_parent < 0 && node.right_parent < 0)
303 f << stringf("\"%s\"", node.inverter ? "true" : "false");
304 else
305 f << stringf("\"%s\", %d, %d", node.inverter ? "nand" : "and", node.left_parent, node.right_parent);
306 for (auto &op : node.outports)
307 f << stringf(", \"%s\", %d", log_id(op.first), op.second);
308 f << stringf(" ]");
309 node_idx++;
310 }
311 f << stringf("\n ]");
312 first_model = false;
313 }
314 f << stringf("\n }");
315 }
316 f << stringf("\n}\n");
317 }
318 };
319
320 struct JsonBackend : public Backend {
321 JsonBackend() : Backend("json", "write design to a JSON file") { }
322 void help() override
323 {
324 // |---v---|---v---|---v---|---v---|---v---|---v---|---v---|---v---|---v---|---v---|
325 log("\n");
326 log(" write_json [options] [filename]\n");
327 log("\n");
328 log("Write a JSON netlist of the current design.\n");
329 log("\n");
330 log(" -aig\n");
331 log(" include AIG models for the different gate types\n");
332 log("\n");
333 log(" -compat-int\n");
334 log(" emit 32-bit or smaller fully-defined parameter values directly\n");
335 log(" as JSON numbers (for compatibility with old parsers)\n");
336 log("\n");
337 log("\n");
338 log("The general syntax of the JSON output created by this command is as follows:\n");
339 log("\n");
340 log(" {\n");
341 log(" \"creator\": \"Yosys <version info>\",\n");
342 log(" \"modules\": {\n");
343 log(" <module_name>: {\n");
344 log(" \"attributes\": {\n");
345 log(" <attribute_name>: <attribute_value>,\n");
346 log(" ...\n");
347 log(" },\n");
348 log(" \"parameter_default_values\": {\n");
349 log(" <parameter_name>: <parameter_value>,\n");
350 log(" ...\n");
351 log(" },\n");
352 log(" \"ports\": {\n");
353 log(" <port_name>: <port_details>,\n");
354 log(" ...\n");
355 log(" },\n");
356 log(" \"cells\": {\n");
357 log(" <cell_name>: <cell_details>,\n");
358 log(" ...\n");
359 log(" },\n");
360 log(" \"memories\": {\n");
361 log(" <memory_name>: <memory_details>,\n");
362 log(" ...\n");
363 log(" },\n");
364 log(" \"netnames\": {\n");
365 log(" <net_name>: <net_details>,\n");
366 log(" ...\n");
367 log(" }\n");
368 log(" }\n");
369 log(" },\n");
370 log(" \"models\": {\n");
371 log(" ...\n");
372 log(" },\n");
373 log(" }\n");
374 log("\n");
375 log("Where <port_details> is:\n");
376 log("\n");
377 log(" {\n");
378 log(" \"direction\": <\"input\" | \"output\" | \"inout\">,\n");
379 log(" \"bits\": <bit_vector>\n");
380 log(" \"offset\": <the lowest bit index in use, if non-0>\n");
381 log(" \"upto\": <1 if the port bit indexing is MSB-first>\n");
382 log(" }\n");
383 log("\n");
384 log("The \"offset\" and \"upto\" fields are skipped if their value would be 0.");
385 log("They don't affect connection semantics, and are only used to preserve original");
386 log("HDL bit indexing.");
387 log("And <cell_details> is:\n");
388 log("\n");
389 log(" {\n");
390 log(" \"hide_name\": <1 | 0>,\n");
391 log(" \"type\": <cell_type>,\n");
392 log(" \"model\": <AIG model name, if -aig option used>,\n");
393 log(" \"parameters\": {\n");
394 log(" <parameter_name>: <parameter_value>,\n");
395 log(" ...\n");
396 log(" },\n");
397 log(" \"attributes\": {\n");
398 log(" <attribute_name>: <attribute_value>,\n");
399 log(" ...\n");
400 log(" },\n");
401 log(" \"port_directions\": {\n");
402 log(" <port_name>: <\"input\" | \"output\" | \"inout\">,\n");
403 log(" ...\n");
404 log(" },\n");
405 log(" \"connections\": {\n");
406 log(" <port_name>: <bit_vector>,\n");
407 log(" ...\n");
408 log(" },\n");
409 log(" }\n");
410 log("\n");
411 log("And <memory_details> is:\n");
412 log("\n");
413 log(" {\n");
414 log(" \"hide_name\": <1 | 0>,\n");
415 log(" \"attributes\": {\n");
416 log(" <attribute_name>: <attribute_value>,\n");
417 log(" ...\n");
418 log(" },\n");
419 log(" \"width\": <memory width>\n");
420 log(" \"start_offset\": <the lowest valid memory address>\n");
421 log(" \"size\": <memory size>\n");
422 log(" }\n");
423 log("\n");
424 log("And <net_details> is:\n");
425 log("\n");
426 log(" {\n");
427 log(" \"hide_name\": <1 | 0>,\n");
428 log(" \"bits\": <bit_vector>\n");
429 log(" \"offset\": <the lowest bit index in use, if non-0>\n");
430 log(" \"upto\": <1 if the port bit indexing is MSB-first>\n");
431 log(" }\n");
432 log("\n");
433 log("The \"hide_name\" fields are set to 1 when the name of this cell or net is\n");
434 log("automatically created and is likely not of interest for a regular user.\n");
435 log("\n");
436 log("The \"port_directions\" section is only included for cells for which the\n");
437 log("interface is known.\n");
438 log("\n");
439 log("Module and cell ports and nets can be single bit wide or vectors of multiple\n");
440 log("bits. Each individual signal bit is assigned a unique integer. The <bit_vector>\n");
441 log("values referenced above are vectors of this integers. Signal bits that are\n");
442 log("connected to a constant driver are denoted as string \"0\", \"1\", \"x\", or\n");
443 log("\"z\" instead of a number.\n");
444 log("\n");
445 log("Bit vectors (including integers) are written as string holding the binary");
446 log("representation of the value. Strings are written as strings, with an appended");
447 log("blank in cases of strings of the form /[01xz]* */.\n");
448 log("\n");
449 log("For example the following Verilog code:\n");
450 log("\n");
451 log(" module test(input x, y);\n");
452 log(" (* keep *) foo #(.P(42), .Q(1337))\n");
453 log(" foo_inst (.A({x, y}), .B({y, x}), .C({4'd10, {4{x}}}));\n");
454 log(" endmodule\n");
455 log("\n");
456 log("Translates to the following JSON output:\n");
457 log("\n");
458
459 log(" {\n");
460 log(" \"creator\": \"Yosys 0.9+2406 (git sha1 fb1168d8, clang 9.0.1 -fPIC -Os)\",\n");
461 log(" \"modules\": {\n");
462 log(" \"test\": {\n");
463 log(" \"attributes\": {\n");
464 log(" \"cells_not_processed\": \"00000000000000000000000000000001\",\n");
465 log(" \"src\": \"test.v:1.1-4.10\"\n");
466 log(" },\n");
467 log(" \"ports\": {\n");
468 log(" \"x\": {\n");
469 log(" \"direction\": \"input\",\n");
470 log(" \"bits\": [ 2 ]\n");
471 log(" },\n");
472 log(" \"y\": {\n");
473 log(" \"direction\": \"input\",\n");
474 log(" \"bits\": [ 3 ]\n");
475 log(" }\n");
476 log(" },\n");
477 log(" \"cells\": {\n");
478 log(" \"foo_inst\": {\n");
479 log(" \"hide_name\": 0,\n");
480 log(" \"type\": \"foo\",\n");
481 log(" \"parameters\": {\n");
482 log(" \"P\": \"00000000000000000000000000101010\",\n");
483 log(" \"Q\": \"00000000000000000000010100111001\"\n");
484 log(" },\n");
485 log(" \"attributes\": {\n");
486 log(" \"keep\": \"00000000000000000000000000000001\",\n");
487 log(" \"module_not_derived\": \"00000000000000000000000000000001\",\n");
488 log(" \"src\": \"test.v:3.1-3.55\"\n");
489 log(" },\n");
490 log(" \"connections\": {\n");
491 log(" \"A\": [ 3, 2 ],\n");
492 log(" \"B\": [ 2, 3 ],\n");
493 log(" \"C\": [ 2, 2, 2, 2, \"0\", \"1\", \"0\", \"1\" ]\n");
494 log(" }\n");
495 log(" }\n");
496 log(" },\n");
497 log(" \"netnames\": {\n");
498 log(" \"x\": {\n");
499 log(" \"hide_name\": 0,\n");
500 log(" \"bits\": [ 2 ],\n");
501 log(" \"attributes\": {\n");
502 log(" \"src\": \"test.v:1.19-1.20\"\n");
503 log(" }\n");
504 log(" },\n");
505 log(" \"y\": {\n");
506 log(" \"hide_name\": 0,\n");
507 log(" \"bits\": [ 3 ],\n");
508 log(" \"attributes\": {\n");
509 log(" \"src\": \"test.v:1.22-1.23\"\n");
510 log(" }\n");
511 log(" }\n");
512 log(" }\n");
513 log(" }\n");
514 log(" }\n");
515 log(" }\n");
516 log("\n");
517 log("The models are given as And-Inverter-Graphs (AIGs) in the following form:\n");
518 log("\n");
519 log(" \"models\": {\n");
520 log(" <model_name>: [\n");
521 log(" /* 0 */ [ <node-spec> ],\n");
522 log(" /* 1 */ [ <node-spec> ],\n");
523 log(" /* 2 */ [ <node-spec> ],\n");
524 log(" ...\n");
525 log(" ],\n");
526 log(" ...\n");
527 log(" },\n");
528 log("\n");
529 log("The following node-types may be used:\n");
530 log("\n");
531 log(" [ \"port\", <portname>, <bitindex>, <out-list> ]\n");
532 log(" - the value of the specified input port bit\n");
533 log("\n");
534 log(" [ \"nport\", <portname>, <bitindex>, <out-list> ]\n");
535 log(" - the inverted value of the specified input port bit\n");
536 log("\n");
537 log(" [ \"and\", <node-index>, <node-index>, <out-list> ]\n");
538 log(" - the ANDed value of the specified nodes\n");
539 log("\n");
540 log(" [ \"nand\", <node-index>, <node-index>, <out-list> ]\n");
541 log(" - the inverted ANDed value of the specified nodes\n");
542 log("\n");
543 log(" [ \"true\", <out-list> ]\n");
544 log(" - the constant value 1\n");
545 log("\n");
546 log(" [ \"false\", <out-list> ]\n");
547 log(" - the constant value 0\n");
548 log("\n");
549 log("All nodes appear in topological order. I.e. only nodes with smaller indices\n");
550 log("are referenced by \"and\" and \"nand\" nodes.\n");
551 log("\n");
552 log("The optional <out-list> at the end of a node specification is a list of\n");
553 log("output portname and bitindex pairs, specifying the outputs driven by this node.\n");
554 log("\n");
555 log("For example, the following is the model for a 3-input 3-output $reduce_and cell\n");
556 log("inferred by the following code:\n");
557 log("\n");
558 log(" module test(input [2:0] in, output [2:0] out);\n");
559 log(" assign in = &out;\n");
560 log(" endmodule\n");
561 log("\n");
562 log(" \"$reduce_and:3U:3\": [\n");
563 log(" /* 0 */ [ \"port\", \"A\", 0 ],\n");
564 log(" /* 1 */ [ \"port\", \"A\", 1 ],\n");
565 log(" /* 2 */ [ \"and\", 0, 1 ],\n");
566 log(" /* 3 */ [ \"port\", \"A\", 2 ],\n");
567 log(" /* 4 */ [ \"and\", 2, 3, \"Y\", 0 ],\n");
568 log(" /* 5 */ [ \"false\", \"Y\", 1, \"Y\", 2 ]\n");
569 log(" ]\n");
570 log("\n");
571 log("Future version of Yosys might add support for additional fields in the JSON\n");
572 log("format. A program processing this format must ignore all unknown fields.\n");
573 log("\n");
574 }
575 void execute(std::ostream *&f, std::string filename, std::vector<std::string> args, RTLIL::Design *design) override
576 {
577 bool aig_mode = false;
578 bool compat_int_mode = false;
579
580 size_t argidx;
581 for (argidx = 1; argidx < args.size(); argidx++)
582 {
583 if (args[argidx] == "-aig") {
584 aig_mode = true;
585 continue;
586 }
587 if (args[argidx] == "-compat-int") {
588 compat_int_mode = true;
589 continue;
590 }
591 break;
592 }
593 extra_args(f, filename, args, argidx);
594
595 log_header(design, "Executing JSON backend.\n");
596
597 JsonWriter json_writer(*f, false, aig_mode, compat_int_mode);
598 json_writer.write_design(design);
599 }
600 } JsonBackend;
601
602 struct JsonPass : public Pass {
603 JsonPass() : Pass("json", "write design in JSON format") { }
604 void help() override
605 {
606 // |---v---|---v---|---v---|---v---|---v---|---v---|---v---|---v---|---v---|---v---|
607 log("\n");
608 log(" json [options] [selection]\n");
609 log("\n");
610 log("Write a JSON netlist of all selected objects.\n");
611 log("\n");
612 log(" -o <filename>\n");
613 log(" write to the specified file.\n");
614 log("\n");
615 log(" -aig\n");
616 log(" also include AIG models for the different gate types\n");
617 log("\n");
618 log(" -compat-int\n");
619 log(" emit 32-bit or smaller fully-defined parameter values directly\n");
620 log(" as JSON numbers (for compatibility with old parsers)\n");
621 log("\n");
622 log("See 'help write_json' for a description of the JSON format used.\n");
623 log("\n");
624 }
625 void execute(std::vector<std::string> args, RTLIL::Design *design) override
626 {
627 std::string filename;
628 bool aig_mode = false;
629 bool compat_int_mode = false;
630
631 size_t argidx;
632 for (argidx = 1; argidx < args.size(); argidx++)
633 {
634 if (args[argidx] == "-o" && argidx+1 < args.size()) {
635 filename = args[++argidx];
636 continue;
637 }
638 if (args[argidx] == "-aig") {
639 aig_mode = true;
640 continue;
641 }
642 if (args[argidx] == "-compat-int") {
643 compat_int_mode = true;
644 continue;
645 }
646 break;
647 }
648 extra_args(args, argidx, design);
649
650 std::ostream *f;
651 std::stringstream buf;
652
653 if (!filename.empty()) {
654 rewrite_filename(filename);
655 std::ofstream *ff = new std::ofstream;
656 ff->open(filename.c_str(), std::ofstream::trunc);
657 if (ff->fail()) {
658 delete ff;
659 log_error("Can't open file `%s' for writing: %s\n", filename.c_str(), strerror(errno));
660 }
661 f = ff;
662 } else {
663 f = &buf;
664 }
665
666 JsonWriter json_writer(*f, true, aig_mode, compat_int_mode);
667 json_writer.write_design(design);
668
669 if (!filename.empty()) {
670 delete f;
671 } else {
672 log("%s", buf.str().c_str());
673 }
674 }
675 } JsonPass;
676
677 PRIVATE_NAMESPACE_END