Added "rename -top new_name"
authorClifford Wolf <clifford@clifford.at>
Wed, 17 Jun 2015 07:38:56 +0000 (09:38 +0200)
committerClifford Wolf <clifford@clifford.at>
Wed, 17 Jun 2015 07:38:56 +0000 (09:38 +0200)
kernel/rtlil.cc
kernel/rtlil.h
passes/cmds/rename.cc

index 5deef8507246e5a0b4aeb5e6b26da361260ea275..7cd2dd4bbd8a0fa0a60d8362ddd2ce7eaadef599 100644 (file)
@@ -316,6 +316,21 @@ RTLIL::Module *RTLIL::Design::module(RTLIL::IdString name)
        return modules_.count(name) ? modules_.at(name) : NULL;
 }
 
+RTLIL::Module *RTLIL::Design::top_module()
+{
+       RTLIL::Module *module = nullptr;
+       int module_count = 0;
+
+       for (auto mod : selected_modules()) {
+               if (mod->get_bool_attribute("\\top"))
+                       return mod;
+               module_count++;
+               module = mod;
+       }
+
+       return module_count == 1 ? module : nullptr;
+}
+
 void RTLIL::Design::add(RTLIL::Module *module)
 {
        log_assert(modules_.count(module->name) == 0);
index e9deb1d5ffec858a825930d57d236585f484f6c9..b6248c4c0d5b930578d83151d962b34953d47933 100644 (file)
@@ -800,6 +800,7 @@ struct RTLIL::Design
 
        RTLIL::ObjRange<RTLIL::Module*> modules();
        RTLIL::Module *module(RTLIL::IdString name);
+       RTLIL::Module *top_module();
 
        bool has(RTLIL::IdString id) const {
                return modules_.count(id) != 0;
index 17d803e96ca298615a5e3abb4d1ce30809287741..d1671317597fe1a5defa9330624b84cada126cc5 100644 (file)
@@ -76,12 +76,17 @@ struct RenamePass : public Pass {
                log("Assign private names (the ones with $-prefix) to all selected wires and cells\n");
                log("with public names. This ignores all selected ports.\n");
                log("\n");
+               log("    rename -top new_name\n");
+               log("\n");
+               log("Rename top module.\n");
+               log("\n");
        }
        virtual void execute(std::vector<std::string> args, RTLIL::Design *design)
        {
                std::string pattern_prefix = "_", pattern_suffix = "_";
                bool flag_enumerate = false;
                bool flag_hide = false;
+               bool flag_top = false;
                bool got_mode = false;
 
                size_t argidx;
@@ -98,6 +103,11 @@ struct RenamePass : public Pass {
                                got_mode = true;
                                continue;
                        }
+                       if (arg == "-top" && !got_mode) {
+                               flag_top = true;
+                               got_mode = true;
+                               continue;
+                       }
                        if (arg == "-pattern" && argidx+1 < args.size() && args[argidx+1].find('%') != std::string::npos) {
                                int pos = args[++argidx].find('%');
                                pattern_prefix = args[argidx].substr(0, pos);
@@ -171,6 +181,23 @@ struct RenamePass : public Pass {
                        }
                }
                else
+               if (flag_top)
+               {
+                       if (argidx+1 != args.size())
+                               log_cmd_error("Invalid number of arguments!\n");
+
+                       IdString new_name = RTLIL::escape_id(args[argidx]);
+                       RTLIL::Module *module = design->top_module();
+
+                       if (module == NULL)
+                               log_cmd_error("No top module found!\n");
+
+                       log("Renaming module %s to %s.\n", log_id(module), log_id(new_name));
+                       design->modules_.erase(module->name);
+                       module->name = new_name;
+                       design->modules_[module->name] = module;
+               }
+               else
                {
                        if (argidx+2 != args.size())
                                log_cmd_error("Invalid number of arguments!\n");