From dab7905cbe7f1679a86c2ee0daaac642ec4887c0 Mon Sep 17 00:00:00 2001 From: "N. Engelhardt" Date: Wed, 8 Jan 2025 13:04:14 +0100 Subject: [PATCH 1/2] write_json: add option to include $scopeinfo cells --- backends/json/json.cc | 27 ++++++++++++++++++----- tests/various/json_scopeinfo.ys | 38 +++++++++++++++++++++++++++++++++ 2 files changed, 60 insertions(+), 5 deletions(-) create mode 100644 tests/various/json_scopeinfo.ys diff --git a/backends/json/json.cc b/backends/json/json.cc index 287c01ead..197223c63 100644 --- a/backends/json/json.cc +++ b/backends/json/json.cc @@ -34,6 +34,7 @@ struct JsonWriter bool use_selection; bool aig_mode; bool compat_int_mode; + bool scopeinfo_mode; Design *design; Module *module; @@ -43,9 +44,9 @@ struct JsonWriter dict sigids; pool aig_models; - JsonWriter(std::ostream &f, bool use_selection, bool aig_mode, bool compat_int_mode) : + JsonWriter(std::ostream &f, bool use_selection, bool aig_mode, bool compat_int_mode, bool scopeinfo_mode) : f(f), use_selection(use_selection), aig_mode(aig_mode), - compat_int_mode(compat_int_mode) { } + compat_int_mode(compat_int_mode), scopeinfo_mode(scopeinfo_mode) { } string get_string(string str) { @@ -194,7 +195,7 @@ struct JsonWriter continue; // Eventually we will want to emit $scopeinfo, but currently this // will break JSON netlist consumers like nextpnr - if (c->type == ID($scopeinfo)) + if (!scopeinfo_mode && c->type == ID($scopeinfo)) continue; f << stringf("%s\n", first ? "" : ","); f << stringf(" %s: {\n", get_name(c->name).c_str()); @@ -356,6 +357,9 @@ struct JsonBackend : public Backend { log(" -selected\n"); log(" output only select module\n"); log("\n"); + log(" -scopeinfo\n"); + log(" include $scopeinfo cells in the output\n"); + log("\n"); log("\n"); log("The general syntax of the JSON output created by this command is as follows:\n"); log("\n"); @@ -601,6 +605,7 @@ struct JsonBackend : public Backend { bool aig_mode = false; bool compat_int_mode = false; bool use_selection = false; + bool scopeinfo_mode = false; size_t argidx; for (argidx = 1; argidx < args.size(); argidx++) @@ -617,13 +622,17 @@ struct JsonBackend : public Backend { use_selection = true; continue; } + if (args[argidx] == "-scopeinfo") { + scopeinfo_mode = true; + continue; + } break; } extra_args(f, filename, args, argidx); log_header(design, "Executing JSON backend.\n"); - JsonWriter json_writer(*f, use_selection, aig_mode, compat_int_mode); + JsonWriter json_writer(*f, use_selection, aig_mode, compat_int_mode, scopeinfo_mode); json_writer.write_design(design); } } JsonBackend; @@ -648,6 +657,9 @@ struct JsonPass : public Pass { log(" emit 32-bit or smaller fully-defined parameter values directly\n"); log(" as JSON numbers (for compatibility with old parsers)\n"); log("\n"); + log(" -scopeinfo\n"); + log(" include $scopeinfo cells in the output\n"); + log("\n"); log("See 'help write_json' for a description of the JSON format used.\n"); log("\n"); } @@ -656,6 +668,7 @@ struct JsonPass : public Pass { std::string filename; bool aig_mode = false; bool compat_int_mode = false; + bool scopeinfo_mode = false; size_t argidx; for (argidx = 1; argidx < args.size(); argidx++) @@ -672,6 +685,10 @@ struct JsonPass : public Pass { compat_int_mode = true; continue; } + if (args[argidx] == "-scopeinfo") { + scopeinfo_mode = true; + continue; + } break; } extra_args(args, argidx, design); @@ -693,7 +710,7 @@ struct JsonPass : public Pass { f = &buf; } - JsonWriter json_writer(*f, true, aig_mode, compat_int_mode); + JsonWriter json_writer(*f, true, aig_mode, compat_int_mode, scopeinfo_mode); json_writer.write_design(design); if (!empty) { diff --git a/tests/various/json_scopeinfo.ys b/tests/various/json_scopeinfo.ys new file mode 100644 index 000000000..50219f16e --- /dev/null +++ b/tests/various/json_scopeinfo.ys @@ -0,0 +1,38 @@ +read_verilog < Date: Wed, 8 Jan 2025 14:46:47 +0100 Subject: [PATCH 2/2] emit $scopeinfo cells by default --- backends/json/json.cc | 22 ++++++++++------------ tests/various/json_scopeinfo.ys | 12 ++++++------ 2 files changed, 16 insertions(+), 18 deletions(-) diff --git a/backends/json/json.cc b/backends/json/json.cc index 197223c63..20d42f626 100644 --- a/backends/json/json.cc +++ b/backends/json/json.cc @@ -193,8 +193,6 @@ struct JsonWriter for (auto c : module->cells()) { if (use_selection && !module->selected(c)) continue; - // Eventually we will want to emit $scopeinfo, but currently this - // will break JSON netlist consumers like nextpnr if (!scopeinfo_mode && c->type == ID($scopeinfo)) continue; f << stringf("%s\n", first ? "" : ","); @@ -357,8 +355,8 @@ struct JsonBackend : public Backend { log(" -selected\n"); log(" output only select module\n"); log("\n"); - log(" -scopeinfo\n"); - log(" include $scopeinfo cells in the output\n"); + log(" -noscopeinfo\n"); + log(" don't include $scopeinfo cells in the output\n"); log("\n"); log("\n"); log("The general syntax of the JSON output created by this command is as follows:\n"); @@ -605,7 +603,7 @@ struct JsonBackend : public Backend { bool aig_mode = false; bool compat_int_mode = false; bool use_selection = false; - bool scopeinfo_mode = false; + bool scopeinfo_mode = true; size_t argidx; for (argidx = 1; argidx < args.size(); argidx++) @@ -622,8 +620,8 @@ struct JsonBackend : public Backend { use_selection = true; continue; } - if (args[argidx] == "-scopeinfo") { - scopeinfo_mode = true; + if (args[argidx] == "-noscopeinfo") { + scopeinfo_mode = false; continue; } break; @@ -657,8 +655,8 @@ struct JsonPass : public Pass { log(" emit 32-bit or smaller fully-defined parameter values directly\n"); log(" as JSON numbers (for compatibility with old parsers)\n"); log("\n"); - log(" -scopeinfo\n"); - log(" include $scopeinfo cells in the output\n"); + log(" -noscopeinfo\n"); + log(" don't include $scopeinfo cells in the output\n"); log("\n"); log("See 'help write_json' for a description of the JSON format used.\n"); log("\n"); @@ -668,7 +666,7 @@ struct JsonPass : public Pass { std::string filename; bool aig_mode = false; bool compat_int_mode = false; - bool scopeinfo_mode = false; + bool scopeinfo_mode = true; size_t argidx; for (argidx = 1; argidx < args.size(); argidx++) @@ -685,8 +683,8 @@ struct JsonPass : public Pass { compat_int_mode = true; continue; } - if (args[argidx] == "-scopeinfo") { - scopeinfo_mode = true; + if (args[argidx] == "-noscopeinfo") { + scopeinfo_mode = false; continue; } break; diff --git a/tests/various/json_scopeinfo.ys b/tests/various/json_scopeinfo.ys index 50219f16e..a5adbde10 100644 --- a/tests/various/json_scopeinfo.ys +++ b/tests/various/json_scopeinfo.ys @@ -26,13 +26,13 @@ flatten -scopename prep write_json json_scopeinfo.out -!grep -qvF '$scopeinfo' json_scopeinfo.out - -write_json -scopeinfo json_scopeinfo.out !grep -qF '$scopeinfo' json_scopeinfo.out +write_json -noscopeinfo json_scopeinfo.out +!grep -qvF '$scopeinfo' json_scopeinfo.out + json -o json_scopeinfo.out -!grep -qvF '$scopeinfo' json_scopeinfo.out - -json -scopeinfo -o json_scopeinfo.out !grep -qF '$scopeinfo' json_scopeinfo.out + +json -noscopeinfo -o json_scopeinfo.out +!grep -qvF '$scopeinfo' json_scopeinfo.out