Skip to content

Commit 7e3990b

Browse files
Merge pull request #4837 from YosysHQ/json_scopinfo_opt
write_json: add option to include $scopeinfo cells
2 parents 828ccfa + 77b2844 commit 7e3990b

File tree

2 files changed

+60
-7
lines changed

2 files changed

+60
-7
lines changed

backends/json/json.cc

Lines changed: 22 additions & 7 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
{
@@ -192,9 +193,7 @@ struct JsonWriter
192193
for (auto c : module->cells()) {
193194
if (use_selection && !module->selected(c))
194195
continue;
195-
// Eventually we will want to emit $scopeinfo, but currently this
196-
// will break JSON netlist consumers like nextpnr
197-
if (c->type == ID($scopeinfo))
196+
if (!scopeinfo_mode && c->type == ID($scopeinfo))
198197
continue;
199198
f << stringf("%s\n", first ? "" : ",");
200199
f << stringf(" %s: {\n", get_name(c->name).c_str());
@@ -356,6 +355,9 @@ struct JsonBackend : public Backend {
356355
log(" -selected\n");
357356
log(" output only select module\n");
358357
log("\n");
358+
log(" -noscopeinfo\n");
359+
log(" don't include $scopeinfo cells in the output\n");
360+
log("\n");
359361
log("\n");
360362
log("The general syntax of the JSON output created by this command is as follows:\n");
361363
log("\n");
@@ -601,6 +603,7 @@ struct JsonBackend : public Backend {
601603
bool aig_mode = false;
602604
bool compat_int_mode = false;
603605
bool use_selection = false;
606+
bool scopeinfo_mode = true;
604607

605608
size_t argidx;
606609
for (argidx = 1; argidx < args.size(); argidx++)
@@ -617,13 +620,17 @@ struct JsonBackend : public Backend {
617620
use_selection = true;
618621
continue;
619622
}
623+
if (args[argidx] == "-noscopeinfo") {
624+
scopeinfo_mode = false;
625+
continue;
626+
}
620627
break;
621628
}
622629
extra_args(f, filename, args, argidx);
623630

624631
log_header(design, "Executing JSON backend.\n");
625632

626-
JsonWriter json_writer(*f, use_selection, aig_mode, compat_int_mode);
633+
JsonWriter json_writer(*f, use_selection, aig_mode, compat_int_mode, scopeinfo_mode);
627634
json_writer.write_design(design);
628635
}
629636
} JsonBackend;
@@ -648,6 +655,9 @@ struct JsonPass : public Pass {
648655
log(" emit 32-bit or smaller fully-defined parameter values directly\n");
649656
log(" as JSON numbers (for compatibility with old parsers)\n");
650657
log("\n");
658+
log(" -noscopeinfo\n");
659+
log(" don't include $scopeinfo cells in the output\n");
660+
log("\n");
651661
log("See 'help write_json' for a description of the JSON format used.\n");
652662
log("\n");
653663
}
@@ -656,6 +666,7 @@ struct JsonPass : public Pass {
656666
std::string filename;
657667
bool aig_mode = false;
658668
bool compat_int_mode = false;
669+
bool scopeinfo_mode = true;
659670

660671
size_t argidx;
661672
for (argidx = 1; argidx < args.size(); argidx++)
@@ -672,6 +683,10 @@ struct JsonPass : public Pass {
672683
compat_int_mode = true;
673684
continue;
674685
}
686+
if (args[argidx] == "-noscopeinfo") {
687+
scopeinfo_mode = false;
688+
continue;
689+
}
675690
break;
676691
}
677692
extra_args(args, argidx, design);
@@ -693,7 +708,7 @@ struct JsonPass : public Pass {
693708
f = &buf;
694709
}
695710

696-
JsonWriter json_writer(*f, true, aig_mode, compat_int_mode);
711+
JsonWriter json_writer(*f, true, aig_mode, compat_int_mode, scopeinfo_mode);
697712
json_writer.write_design(design);
698713

699714
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 -qF '$scopeinfo' json_scopeinfo.out
30+
31+
write_json -noscopeinfo json_scopeinfo.out
32+
!grep -qvF '$scopeinfo' json_scopeinfo.out
33+
34+
json -o json_scopeinfo.out
35+
!grep -qF '$scopeinfo' json_scopeinfo.out
36+
37+
json -noscopeinfo -o json_scopeinfo.out
38+
!grep -qvF '$scopeinfo' json_scopeinfo.out

0 commit comments

Comments
 (0)