Skip to content

Commit dab7905

Browse files
committed
write_json: add option to include $scopeinfo cells
1 parent 17a53b8 commit dab7905

File tree

2 files changed

+60
-5
lines changed

2 files changed

+60
-5
lines changed

backends/json/json.cc

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ struct JsonWriter
3434
bool use_selection;
3535
bool aig_mode;
3636
bool compat_int_mode;
37+
bool scopeinfo_mode;
3738

3839
Design *design;
3940
Module *module;
@@ -43,9 +44,9 @@ struct JsonWriter
4344
dict<SigBit, string> sigids;
4445
pool<Aig> aig_models;
4546

46-
JsonWriter(std::ostream &f, bool use_selection, bool aig_mode, bool compat_int_mode) :
47+
JsonWriter(std::ostream &f, bool use_selection, bool aig_mode, bool compat_int_mode, bool scopeinfo_mode) :
4748
f(f), use_selection(use_selection), aig_mode(aig_mode),
48-
compat_int_mode(compat_int_mode) { }
49+
compat_int_mode(compat_int_mode), scopeinfo_mode(scopeinfo_mode) { }
4950

5051
string get_string(string str)
5152
{
@@ -194,7 +195,7 @@ struct JsonWriter
194195
continue;
195196
// Eventually we will want to emit $scopeinfo, but currently this
196197
// will break JSON netlist consumers like nextpnr
197-
if (c->type == ID($scopeinfo))
198+
if (!scopeinfo_mode && c->type == ID($scopeinfo))
198199
continue;
199200
f << stringf("%s\n", first ? "" : ",");
200201
f << stringf(" %s: {\n", get_name(c->name).c_str());
@@ -356,6 +357,9 @@ struct JsonBackend : public Backend {
356357
log(" -selected\n");
357358
log(" output only select module\n");
358359
log("\n");
360+
log(" -scopeinfo\n");
361+
log(" include $scopeinfo cells in the output\n");
362+
log("\n");
359363
log("\n");
360364
log("The general syntax of the JSON output created by this command is as follows:\n");
361365
log("\n");
@@ -601,6 +605,7 @@ struct JsonBackend : public Backend {
601605
bool aig_mode = false;
602606
bool compat_int_mode = false;
603607
bool use_selection = false;
608+
bool scopeinfo_mode = false;
604609

605610
size_t argidx;
606611
for (argidx = 1; argidx < args.size(); argidx++)
@@ -617,13 +622,17 @@ struct JsonBackend : public Backend {
617622
use_selection = true;
618623
continue;
619624
}
625+
if (args[argidx] == "-scopeinfo") {
626+
scopeinfo_mode = true;
627+
continue;
628+
}
620629
break;
621630
}
622631
extra_args(f, filename, args, argidx);
623632

624633
log_header(design, "Executing JSON backend.\n");
625634

626-
JsonWriter json_writer(*f, use_selection, aig_mode, compat_int_mode);
635+
JsonWriter json_writer(*f, use_selection, aig_mode, compat_int_mode, scopeinfo_mode);
627636
json_writer.write_design(design);
628637
}
629638
} JsonBackend;
@@ -648,6 +657,9 @@ struct JsonPass : public Pass {
648657
log(" emit 32-bit or smaller fully-defined parameter values directly\n");
649658
log(" as JSON numbers (for compatibility with old parsers)\n");
650659
log("\n");
660+
log(" -scopeinfo\n");
661+
log(" include $scopeinfo cells in the output\n");
662+
log("\n");
651663
log("See 'help write_json' for a description of the JSON format used.\n");
652664
log("\n");
653665
}
@@ -656,6 +668,7 @@ struct JsonPass : public Pass {
656668
std::string filename;
657669
bool aig_mode = false;
658670
bool compat_int_mode = false;
671+
bool scopeinfo_mode = false;
659672

660673
size_t argidx;
661674
for (argidx = 1; argidx < args.size(); argidx++)
@@ -672,6 +685,10 @@ struct JsonPass : public Pass {
672685
compat_int_mode = true;
673686
continue;
674687
}
688+
if (args[argidx] == "-scopeinfo") {
689+
scopeinfo_mode = true;
690+
continue;
691+
}
675692
break;
676693
}
677694
extra_args(args, argidx, design);
@@ -693,7 +710,7 @@ struct JsonPass : public Pass {
693710
f = &buf;
694711
}
695712

696-
JsonWriter json_writer(*f, true, aig_mode, compat_int_mode);
713+
JsonWriter json_writer(*f, true, aig_mode, compat_int_mode, scopeinfo_mode);
697714
json_writer.write_design(design);
698715

699716
if (!empty) {

tests/various/json_scopeinfo.ys

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
read_verilog <<EOT
2+
module top(input in, output out);
3+
wire [1:0] w1, w2;
4+
f1_test u1 (.in(in), .out(w1[0]));
5+
f2_test u2 (.in(w1), .out(w2));
6+
f3_test u3 (.in(w2[0]), .out(out));
7+
endmodule
8+
9+
module f1_test(input in, output out);
10+
assign out = in;
11+
endmodule
12+
13+
module f2_test(input [1:0] in, output [1:0] out);
14+
assign out[0] = in[0];
15+
assign out[1] = in[1];
16+
endmodule
17+
18+
module f3_test(input in, output [1:0] out);
19+
assign out[0] = in;
20+
assign out[1] = in;
21+
endmodule
22+
EOT
23+
24+
hierarchy -top top
25+
flatten -scopename
26+
prep
27+
28+
write_json json_scopeinfo.out
29+
!grep -qvF '$scopeinfo' json_scopeinfo.out
30+
31+
write_json -scopeinfo json_scopeinfo.out
32+
!grep -qF '$scopeinfo' json_scopeinfo.out
33+
34+
json -o json_scopeinfo.out
35+
!grep -qvF '$scopeinfo' json_scopeinfo.out
36+
37+
json -scopeinfo -o json_scopeinfo.out
38+
!grep -qF '$scopeinfo' json_scopeinfo.out

0 commit comments

Comments
 (0)