Added "dump" command (part ilang backend)
[yosys.git] / backends / ilang / ilang_backend.cc
index 8dcdb12881e0c99484ccfb3fa2f9449dc3e35d81..9c74e61ca9da5025c1aa2c7086f58916dc040d22 100644 (file)
@@ -27,6 +27,8 @@
 #include "kernel/log.h"
 #include <string>
 #include <assert.h>
+#include <string.h>
+#include <errno.h>
 
 using namespace ILANG_BACKEND;
 
@@ -257,7 +259,7 @@ void ILANG_BACKEND::dump_conn(FILE *f, std::string indent, const RTLIL::SigSpec
        fprintf(f, "\n");
 }
 
-void ILANG_BACKEND::dump_module(FILE *f, std::string indent, const RTLIL::Module *module)
+void ILANG_BACKEND::dump_module(FILE *f, std::string indent, const RTLIL::Module *module, const RTLIL::Design *design, bool only_selected)
 {
        for (auto it = module->attributes.begin(); it != module->attributes.end(); it++) {
                fprintf(f, "%s" "attribute %s ", indent.c_str(), it->first.c_str());
@@ -268,29 +270,63 @@ void ILANG_BACKEND::dump_module(FILE *f, std::string indent, const RTLIL::Module
        fprintf(f, "%s" "module %s\n", indent.c_str(), module->name.c_str());
 
        for (auto it = module->wires.begin(); it != module->wires.end(); it++)
-               dump_wire(f, indent + "  ", it->second);
+               if (!only_selected || design->selected(module, it->second)) {
+                       if (only_selected)
+                               fprintf(f, "\n");
+                       dump_wire(f, indent + "  ", it->second);
+               }
 
        for (auto it = module->memories.begin(); it != module->memories.end(); it++)
-               dump_memory(f, indent + "  ", it->second);
+               if (!only_selected || design->selected(module, it->second)) {
+                       if (only_selected)
+                               fprintf(f, "\n");
+                       dump_memory(f, indent + "  ", it->second);
+               }
 
        for (auto it = module->cells.begin(); it != module->cells.end(); it++)
-               dump_cell(f, indent + "  ", it->second);
+               if (!only_selected || design->selected(module, it->second)) {
+                       if (only_selected)
+                               fprintf(f, "\n");
+                       dump_cell(f, indent + "  ", it->second);
+               }
 
        for (auto it = module->processes.begin(); it != module->processes.end(); it++)
-               dump_proc(f, indent + "  ", it->second);
+               if (!only_selected || design->selected(module, it->second)) {
+                       if (only_selected)
+                               fprintf(f, "\n");
+                       dump_proc(f, indent + "  ", it->second);
+               }
 
-       for (auto it = module->connections.begin(); it != module->connections.end(); it++)
-               dump_conn(f, indent + "  ", it->first, it->second);
+       bool first_conn_line = true;
+       for (auto it = module->connections.begin(); it != module->connections.end(); it++) {
+               bool show_conn = !only_selected;
+               if (only_selected) {
+                       RTLIL::SigSpec sigs = it->first;
+                       sigs.append(it->second);
+                       for (auto &c : sigs.chunks) {
+                               if (c.wire == NULL || !design->selected(module, c.wire))
+                                       continue;
+                               show_conn = true;
+                       }
+               }
+               if (show_conn) {
+                       if (only_selected && first_conn_line)
+                               fprintf(f, "\n");
+                       dump_conn(f, indent + "  ", it->first, it->second);
+                       first_conn_line = false;
+               }
+       }
 
        fprintf(f, "%s" "end\n", indent.c_str());
 }
 
-void ILANG_BACKEND::dump_design(FILE *f, const RTLIL::Design *design)
+void ILANG_BACKEND::dump_design(FILE *f, const RTLIL::Design *design, bool only_selected)
 {
        for (auto it = design->modules.begin(); it != design->modules.end(); it++) {
-               if (it != design->modules.begin())
+               if (it != design->modules.begin() || only_selected)
                        fprintf(f, "\n");
-               dump_module(f, "", it->second);
+               if (!only_selected || design->selected(it->second))
+                       dump_module(f, "", it->second, design, only_selected);
        }
 }
 
@@ -310,7 +346,61 @@ struct IlangBackend : public Backend {
                log_header("Executing ILANG backend.\n");
                extra_args(f, filename, args, 1);
                log("Output filename: %s\n", filename.c_str());
-               ILANG_BACKEND::dump_design(f, design);
+               ILANG_BACKEND::dump_design(f, design, false);
        }
 } IlangBackend;
 
+struct DumpPass : public Pass {
+       DumpPass() : Pass("dump", "print parts of the design in ilang format") { }
+       virtual void help()
+       {
+               //   |---v---|---v---|---v---|---v---|---v---|---v---|---v---|---v---|---v---|---v---|
+               log("\n");
+               log("    dump [options] [selection]\n");
+               log("\n");
+               log("Write the selected parts of the design to the console or specified file in\n");
+               log("ilang format.\n");
+               log("\n");
+               log("    -outfile <filename>\n");
+               log("        Write to the specified file.\n");
+               log("\n");
+       }
+       virtual void execute(std::vector<std::string> args, RTLIL::Design *design)
+       {
+               std::string filename;
+
+               size_t argidx;
+               for (argidx = 1; argidx < args.size(); argidx++)
+               {
+                       std::string arg = args[argidx];
+                       if (arg == "-outfile" && argidx+1 < args.size()) {
+                               filename = args[++argidx];
+                               continue;
+                       }
+                       break;
+               }
+               extra_args(args, argidx, design);
+
+               FILE *f = NULL;
+               char *buf_ptr;
+               size_t buf_size;
+
+               if (!filename.empty()) {
+                       f = fopen(filename.c_str(), "w");
+                       if (f == NULL)
+                               log_error("Can't open file `%s' for writing: %s\n", filename.c_str(), strerror(errno));
+               } else {
+                       f = open_memstream(&buf_ptr, &buf_size);
+               }
+
+               ILANG_BACKEND::dump_design(f, design, true);
+
+               fclose(f);
+
+               if (filename.empty()) {
+                       log("%s", buf_ptr);
+                       free(buf_ptr);
+               }
+       }
+} DumpPass;