Fix handling of some formal cells in btor back-end
[yosys.git] / backends / json / json.cc
index 1da23bb7d035c50538e4f170a48250419a2584e1..42eedc606273c15ea477b7c6269b6dff00af0515 100644 (file)
@@ -1,7 +1,7 @@
 /*
  *  yosys -- Yosys Open SYnthesis Suite
  *
- *  Copyright (C) 2012  Clifford Wolf <clifford@clifford.at>
+ *  Copyright (C) 2012  Claire Xenia Wolf <claire@yosyshq.com>
  *
  *  Permission to use, copy, modify, and/or distribute this software for any
  *  purpose with or without fee is hereby granted, provided that the above
@@ -52,8 +52,23 @@ struct JsonWriter
                string newstr = "\"";
                for (char c : str) {
                        if (c == '\\')
+                               newstr += "\\\\";
+                       else if (c == '"')
+                               newstr += "\\\"";
+                       else if (c == '\b')
+                               newstr += "\\b";
+                       else if (c == '\f')
+                               newstr += "\\f";
+                       else if (c == '\n')
+                               newstr += "\\n";
+                       else if (c == '\r')
+                               newstr += "\\r";
+                       else if (c == '\t')
+                               newstr += "\\t";
+                       else if (c < 0x20)
+                               newstr += stringf("\\u%04X", c);
+                       else
                                newstr += c;
-                       newstr += c;
                }
                return newstr + "\"";
        }
@@ -135,12 +150,22 @@ struct JsonWriter
                // reserve 0 and 1 to avoid confusion with "0" and "1"
                sigidcounter = 2;
 
+               if (module->has_processes()) {
+                       log_error("Module %s contains processes, which are not supported by JSON backend (run `proc` first).\n", log_id(module));
+               }
+
                f << stringf("    %s: {\n", get_name(module->name).c_str());
 
                f << stringf("      \"attributes\": {");
                write_parameters(module->attributes, /*for_module=*/true);
                f << stringf("\n      },\n");
 
+               if (module->parameter_default_values.size()) {
+                       f << stringf("      \"parameter_default_values\": {");
+                       write_parameters(module->parameter_default_values, /*for_module=*/true);
+                       f << stringf("\n      },\n");
+               }
+
                f << stringf("      \"ports\": {");
                bool first = true;
                for (auto n : module->ports) {
@@ -154,6 +179,8 @@ struct JsonWriter
                                f << stringf("          \"offset\": %d,\n", w->start_offset);
                        if (w->upto)
                                f << stringf("          \"upto\": 1,\n");
+                       if (w->is_signed)
+                               f << stringf("          \"signed\": %d,\n", w->is_signed);
                        f << stringf("          \"bits\": %s\n", get_bits(w).c_str());
                        f << stringf("        }");
                        first = false;
@@ -208,6 +235,27 @@ struct JsonWriter
                }
                f << stringf("\n      },\n");
 
+               if (!module->memories.empty()) {
+                       f << stringf("      \"memories\": {");
+                       first = true;
+                       for (auto &it : module->memories) {
+                               if (use_selection && !module->selected(it.second))
+                                       continue;
+                               f << stringf("%s\n", first ? "" : ",");
+                               f << stringf("        %s: {\n", get_name(it.second->name).c_str());
+                               f << stringf("          \"hide_name\": %s,\n", it.second->name[0] == '$' ? "1" : "0");
+                               f << stringf("          \"attributes\": {");
+                               write_parameters(it.second->attributes);
+                               f << stringf("\n          },\n");
+                               f << stringf("          \"width\": %d,\n", it.second->width);
+                               f << stringf("          \"start_offset\": %d,\n", it.second->start_offset);
+                               f << stringf("          \"size\": %d\n", it.second->size);
+                               f << stringf("        }");
+                               first = false;
+                       }
+                       f << stringf("\n      },\n");
+               }
+
                f << stringf("      \"netnames\": {");
                first = true;
                for (auto w : module->wires()) {
@@ -221,6 +269,8 @@ struct JsonWriter
                                f << stringf("          \"offset\": %d,\n", w->start_offset);
                        if (w->upto)
                                f << stringf("          \"upto\": 1,\n");
+                       if (w->is_signed)
+                               f << stringf("          \"signed\": %d,\n", w->is_signed);
                        f << stringf("          \"attributes\": {");
                        write_parameters(w->attributes);
                        f << stringf("\n          }\n");
@@ -284,7 +334,7 @@ struct JsonWriter
 
 struct JsonBackend : public Backend {
        JsonBackend() : Backend("json", "write design to a JSON file") { }
-       void help() YS_OVERRIDE
+       void help() override
        {
                //   |---v---|---v---|---v---|---v---|---v---|---v---|---v---|---v---|---v---|---v---|
                log("\n");
@@ -310,6 +360,10 @@ struct JsonBackend : public Backend {
                log("            <attribute_name>: <attribute_value>,\n");
                log("            ...\n");
                log("          },\n");
+               log("          \"parameter_default_values\": {\n");
+               log("            <parameter_name>: <parameter_value>,\n");
+               log("            ...\n");
+               log("          },\n");
                log("          \"ports\": {\n");
                log("            <port_name>: <port_details>,\n");
                log("            ...\n");
@@ -318,6 +372,10 @@ struct JsonBackend : public Backend {
                log("            <cell_name>: <cell_details>,\n");
                log("            ...\n");
                log("          },\n");
+               log("          \"memories\": {\n");
+               log("            <memory_name>: <memory_details>,\n");
+               log("            ...\n");
+               log("          },\n");
                log("          \"netnames\": {\n");
                log("            <net_name>: <net_details>,\n");
                log("            ...\n");
@@ -365,6 +423,19 @@ struct JsonBackend : public Backend {
                log("      },\n");
                log("    }\n");
                log("\n");
+               log("And <memory_details> is:\n");
+               log("\n");
+               log("    {\n");
+               log("      \"hide_name\": <1 | 0>,\n");
+               log("      \"attributes\": {\n");
+               log("        <attribute_name>: <attribute_value>,\n");
+               log("        ...\n");
+               log("      },\n");
+               log("      \"width\": <memory width>\n");
+               log("      \"start_offset\": <the lowest valid memory address>\n");
+               log("      \"size\": <memory size>\n");
+               log("    }\n");
+               log("\n");
                log("And <net_details> is:\n");
                log("\n");
                log("    {\n");
@@ -516,7 +587,7 @@ struct JsonBackend : public Backend {
                log("format. A program processing this format must ignore all unknown fields.\n");
                log("\n");
        }
-       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) override
        {
                bool aig_mode = false;
                bool compat_int_mode = false;
@@ -545,7 +616,7 @@ struct JsonBackend : public Backend {
 
 struct JsonPass : public Pass {
        JsonPass() : Pass("json", "write design in JSON format") { }
-       void help() YS_OVERRIDE
+       void help() override
        {
                //   |---v---|---v---|---v---|---v---|---v---|---v---|---v---|---v---|---v---|---v---|
                log("\n");
@@ -566,7 +637,7 @@ struct JsonPass : public Pass {
                log("See 'help write_json' for a description of the JSON format used.\n");
                log("\n");
        }
-       void execute(std::vector<std::string> args, RTLIL::Design *design) YS_OVERRIDE
+       void execute(std::vector<std::string> args, RTLIL::Design *design) override
        {
                std::string filename;
                bool aig_mode = false;