json: remove the 32-bit parameter special case

Before, the rules for encoding parameters in JSON were as follows:

- if the parameter is not a string:

  - if it is exactly 32 bits long and there are no z or x bits, emit it
    as an int
  - otherwise, emit it as a string made of 0/1/x/z characters

- if the parameter is a string:

  - if it contains only 0/1/x/z characters, append a space at the end
    to distinguish it from a non-string
  - otherwise, emit it directly

However, this caused a problem in the json11 parser used in nextpnr:
yosys emits unsigned ints, and nextpnr parses them as signed, using
the value of INT_MIN for values that overflow the signed int range.
This caused destruction of LUT5 initialization values.  Since both
nextpnr and yosys parser can also accept 32-bit parameters in the
same encoding as other widths, let's just remove that special case.
The old behavior is still left behind a `-compat-int` flag, in case
someone relies on it.
This commit is contained in:
Marcin Kościelnicki 2020-02-01 10:21:19 +01:00 committed by Marcelina Kościelnicka
parent a1c840ca5d
commit 00fba62711
1 changed files with 28 additions and 10 deletions

View File

@ -33,6 +33,7 @@ struct JsonWriter
std::ostream &f; std::ostream &f;
bool use_selection; bool use_selection;
bool aig_mode; bool aig_mode;
bool compat_int_mode;
Design *design; Design *design;
Module *module; Module *module;
@ -42,8 +43,9 @@ struct JsonWriter
dict<SigBit, string> sigids; dict<SigBit, string> sigids;
pool<Aig> aig_models; pool<Aig> aig_models;
JsonWriter(std::ostream &f, bool use_selection, bool aig_mode) : JsonWriter(std::ostream &f, bool use_selection, bool aig_mode, bool compat_int_mode) :
f(f), use_selection(use_selection), aig_mode(aig_mode) { } f(f), use_selection(use_selection), aig_mode(aig_mode),
compat_int_mode(compat_int_mode) { }
string get_string(string str) string get_string(string str)
{ {
@ -102,8 +104,7 @@ struct JsonWriter
if (state < 2) if (state < 2)
str += " "; str += " ";
f << get_string(str); f << get_string(str);
} else } else if (compat_int_mode && GetSize(value) == 32 && value.is_fully_def()) {
if (GetSize(value) == 32 && value.is_fully_def()) {
if ((value.flags & RTLIL::ConstFlags::CONST_FLAG_SIGNED) != 0) if ((value.flags & RTLIL::ConstFlags::CONST_FLAG_SIGNED) != 0)
f << stringf("%d", value.as_int()); f << stringf("%d", value.as_int());
else else
@ -294,6 +295,10 @@ struct JsonBackend : public Backend {
log(" -aig\n"); log(" -aig\n");
log(" include AIG models for the different gate types\n"); log(" include AIG models for the different gate types\n");
log("\n"); log("\n");
log(" -compat-int\n");
log(" emit 32-bit fully-defined parameter values directly\n");
log(" as JSON numbers (for compatibility with old parsers)\n");
log("\n");
log("\n"); log("\n");
log("The general syntax of the JSON output created by this command is as follows:\n"); log("The general syntax of the JSON output created by this command is as follows:\n");
log("\n"); log("\n");
@ -368,10 +373,9 @@ struct JsonBackend : public Backend {
log("connected to a constant driver are denoted as string \"0\", \"1\", \"x\", or\n"); log("connected to a constant driver are denoted as string \"0\", \"1\", \"x\", or\n");
log("\"z\" instead of a number.\n"); log("\"z\" instead of a number.\n");
log("\n"); log("\n");
log("Numeric 32-bit parameter and attribute values are written as decimal values.\n"); log("Bit vectors (including integers) are written as string holding the binary");
log("Bit verctors of different sizes, or ones containing 'x' or 'z' bits, are written\n"); log("representation of the value. Strings are written as strings, with an appended");
log("as string holding the binary representation of the value. Strings are written\n"); log("blank in cases of strings of the form /[01xz]* */.\n");
log("as strings, with an appended blank in cases of strings of the form /[01xz]* */.\n");
log("\n"); log("\n");
log("For example the following Verilog code:\n"); log("For example the following Verilog code:\n");
log("\n"); log("\n");
@ -495,6 +499,7 @@ struct JsonBackend : public Backend {
void execute(std::ostream *&f, std::string filename, std::vector<std::string> args, RTLIL::Design *design) YS_OVERRIDE void execute(std::ostream *&f, std::string filename, std::vector<std::string> args, RTLIL::Design *design) YS_OVERRIDE
{ {
bool aig_mode = false; bool aig_mode = false;
bool compat_int_mode = false;
size_t argidx; size_t argidx;
for (argidx = 1; argidx < args.size(); argidx++) for (argidx = 1; argidx < args.size(); argidx++)
@ -503,13 +508,17 @@ struct JsonBackend : public Backend {
aig_mode = true; aig_mode = true;
continue; continue;
} }
if (args[argidx] == "-compat-int") {
compat_int_mode = true;
continue;
}
break; break;
} }
extra_args(f, filename, args, argidx); extra_args(f, filename, args, argidx);
log_header(design, "Executing JSON backend.\n"); log_header(design, "Executing JSON backend.\n");
JsonWriter json_writer(*f, false, aig_mode); JsonWriter json_writer(*f, false, aig_mode, compat_int_mode);
json_writer.write_design(design); json_writer.write_design(design);
} }
} JsonBackend; } JsonBackend;
@ -530,6 +539,10 @@ struct JsonPass : public Pass {
log(" -aig\n"); log(" -aig\n");
log(" also include AIG models for the different gate types\n"); log(" also include AIG models for the different gate types\n");
log("\n"); log("\n");
log(" -compat-int\n");
log(" emit 32-bit fully-defined parameter values directly\n");
log(" as JSON numbers (for compatibility with old parsers)\n");
log("\n");
log("See 'help write_json' for a description of the JSON format used.\n"); log("See 'help write_json' for a description of the JSON format used.\n");
log("\n"); log("\n");
} }
@ -537,6 +550,7 @@ struct JsonPass : public Pass {
{ {
std::string filename; std::string filename;
bool aig_mode = false; bool aig_mode = false;
bool compat_int_mode = false;
size_t argidx; size_t argidx;
for (argidx = 1; argidx < args.size(); argidx++) for (argidx = 1; argidx < args.size(); argidx++)
@ -549,6 +563,10 @@ struct JsonPass : public Pass {
aig_mode = true; aig_mode = true;
continue; continue;
} }
if (args[argidx] == "-compat-int") {
compat_int_mode = true;
continue;
}
break; break;
} }
extra_args(args, argidx, design); extra_args(args, argidx, design);
@ -569,7 +587,7 @@ struct JsonPass : public Pass {
f = &buf; f = &buf;
} }
JsonWriter json_writer(*f, true, aig_mode); JsonWriter json_writer(*f, true, aig_mode, compat_int_mode);
json_writer.write_design(design); json_writer.write_design(design);
if (!filename.empty()) { if (!filename.empty()) {