Add "paramap" pass
authorClifford Wolf <clifford@clifford.at>
Wed, 28 Aug 2019 08:03:27 +0000 (10:03 +0200)
committerClifford Wolf <clifford@clifford.at>
Wed, 28 Aug 2019 08:03:27 +0000 (10:03 +0200)
Signed-off-by: Clifford Wolf <clifford@clifford.at>
passes/techmap/attrmap.cc

index a38638e0b6e5afdd598853172af43024b1afb528..3a2835733e59417bdef4f40c9cf55e1f6990a5f9 100644 (file)
@@ -143,6 +143,82 @@ void attrmap_apply(string objname, vector<std::unique_ptr<AttrmapAction>> &actio
        attributes.swap(new_attributes);
 }
 
+void log_attrmap_paramap_options()
+{
+       log("    -tocase <name>\n");
+       log("        Match attribute names case-insensitively and set it to the specified\n");
+       log("        name.\n");
+       log("\n");
+       log("    -rename <old_name> <new_name>\n");
+       log("        Rename attributes as specified\n");
+       log("\n");
+       log("    -map <old_name>=<old_value> <new_name>=<new_value>\n");
+       log("        Map key/value pairs as indicated.\n");
+       log("\n");
+       log("    -imap <old_name>=<old_value> <new_name>=<new_value>\n");
+       log("        Like -map, but use case-insensitive match for <old_value> when\n");
+       log("        it is a string value.\n");
+       log("\n");
+       log("    -remove <name>=<value>\n");
+       log("        Remove attributes matching this pattern.\n");
+}
+
+bool parse_attrmap_paramap_options(size_t &argidx, std::vector<std::string> &args, vector<std::unique_ptr<AttrmapAction>> &actions)
+{
+       std::string arg = args[argidx];
+       if (arg == "-tocase" && argidx+1 < args.size()) {
+               auto action = new AttrmapTocase;
+               action->name = args[++argidx];
+               actions.push_back(std::unique_ptr<AttrmapAction>(action));
+               return true;
+       }
+       if (arg == "-rename" && argidx+2 < args.size()) {
+               auto action = new AttrmapRename;
+               action->old_name = args[++argidx];
+               action->new_name = args[++argidx];
+               actions.push_back(std::unique_ptr<AttrmapAction>(action));
+               return true;
+       }
+       if ((arg == "-map" || arg == "-imap") && argidx+2 < args.size()) {
+               string arg1 = args[++argidx];
+               string arg2 = args[++argidx];
+               string val1, val2;
+               size_t p = arg1.find("=");
+               if (p != string::npos) {
+                       val1 = arg1.substr(p+1);
+                       arg1 = arg1.substr(0, p);
+               }
+               p = arg2.find("=");
+               if (p != string::npos) {
+                       val2 = arg2.substr(p+1);
+                       arg2 = arg2.substr(0, p);
+               }
+               auto action = new AttrmapMap;
+               action->imap = (arg == "-map");
+               action->old_name = arg1;
+               action->new_name = arg2;
+               action->old_value = val1;
+               action->new_value = val2;
+               actions.push_back(std::unique_ptr<AttrmapAction>(action));
+               return true;
+       }
+       if (arg == "-remove" && argidx+1 < args.size()) {
+               string arg1 = args[++argidx], val1;
+               size_t p = arg1.find("=");
+               if (p != string::npos) {
+                       val1 = arg1.substr(p+1);
+                       arg1 = arg1.substr(0, p);
+               }
+               auto action = new AttrmapRemove;
+               action->name = arg1;
+               action->has_value = (p != string::npos);
+               action->value = val1;
+               actions.push_back(std::unique_ptr<AttrmapAction>(action));
+               return true;
+       }
+       return false;
+}
+
 struct AttrmapPass : public Pass {
        AttrmapPass() : Pass("attrmap", "renaming attributes") { }
        void help() YS_OVERRIDE
@@ -154,22 +230,7 @@ struct AttrmapPass : public Pass {
                log("This command renames attributes and/or mapps key/value pairs to\n");
                log("other key/value pairs.\n");
                log("\n");
-               log("    -tocase <name>\n");
-               log("        Match attribute names case-insensitively and set it to the specified\n");
-               log("        name.\n");
-               log("\n");
-               log("    -rename <old_name> <new_name>\n");
-               log("        Rename attributes as specified\n");
-               log("\n");
-               log("    -map <old_name>=<old_value> <new_name>=<new_value>\n");
-               log("        Map key/value pairs as indicated.\n");
-               log("\n");
-               log("    -imap <old_name>=<old_value> <new_name>=<new_value>\n");
-               log("        Like -map, but use case-insensitive match for <old_value> when\n");
-               log("        it is a string value.\n");
-               log("\n");
-               log("    -remove <name>=<value>\n");
-               log("        Remove attributes matching this pattern.\n");
+               log_attrmap_paramap_options();
                log("\n");
                log("    -modattr\n");
                log("        Operate on module attributes instead of attributes on wires and cells.\n");
@@ -190,58 +251,9 @@ struct AttrmapPass : public Pass {
                size_t argidx;
                for (argidx = 1; argidx < args.size(); argidx++)
                {
-                       std::string arg = args[argidx];
-                       if (arg == "-tocase" && argidx+1 < args.size()) {
-                               auto action = new AttrmapTocase;
-                               action->name = args[++argidx];
-                               actions.push_back(std::unique_ptr<AttrmapAction>(action));
-                               continue;
-                       }
-                       if (arg == "-rename" && argidx+2 < args.size()) {
-                               auto action = new AttrmapRename;
-                               action->old_name = args[++argidx];
-                               action->new_name = args[++argidx];
-                               actions.push_back(std::unique_ptr<AttrmapAction>(action));
+                       if (parse_attrmap_paramap_options(argidx, args, actions))
                                continue;
-                       }
-                       if ((arg == "-map" || arg == "-imap") && argidx+2 < args.size()) {
-                               string arg1 = args[++argidx];
-                               string arg2 = args[++argidx];
-                               string val1, val2;
-                               size_t p = arg1.find("=");
-                               if (p != string::npos) {
-                                       val1 = arg1.substr(p+1);
-                                       arg1 = arg1.substr(0, p);
-                               }
-                               p = arg2.find("=");
-                               if (p != string::npos) {
-                                       val2 = arg2.substr(p+1);
-                                       arg2 = arg2.substr(0, p);
-                               }
-                               auto action = new AttrmapMap;
-                               action->imap = (arg == "-map");
-                               action->old_name = arg1;
-                               action->new_name = arg2;
-                               action->old_value = val1;
-                               action->new_value = val2;
-                               actions.push_back(std::unique_ptr<AttrmapAction>(action));
-                               continue;
-                       }
-                       if (arg == "-remove" && argidx+1 < args.size()) {
-                               string arg1 = args[++argidx], val1;
-                               size_t p = arg1.find("=");
-                               if (p != string::npos) {
-                                       val1 = arg1.substr(p+1);
-                                       arg1 = arg1.substr(0, p);
-                               }
-                               auto action = new AttrmapRemove;
-                               action->name = arg1;
-                               action->has_value = (p != string::npos);
-                               action->value = val1;
-                               actions.push_back(std::unique_ptr<AttrmapAction>(action));
-                               continue;
-                       }
-                       if (arg == "-modattr") {
+                       if (args[argidx] == "-modattr") {
                                modattr_mode = true;
                                continue;
                        }
@@ -287,4 +299,43 @@ struct AttrmapPass : public Pass {
        }
 } AttrmapPass;
 
+struct ParamapPass : public Pass {
+       ParamapPass() : Pass("paramap", "renaming cell parameters") { }
+       void help() YS_OVERRIDE
+       {
+               //   |---v---|---v---|---v---|---v---|---v---|---v---|---v---|---v---|---v---|---v---|
+               log("\n");
+               log("    paramap [options] [selection]\n");
+               log("\n");
+               log("This command renames cell parameters and/or mapps key/value pairs to\n");
+               log("other key/value pairs.\n");
+               log("\n");
+               log_attrmap_paramap_options();
+               log("\n");
+               log("For example, mapping Diamond-style ECP5 \"init\" attributes to Yosys-style:\n");
+               log("\n");
+               log("    paramap -tocase INIT t:LUT4\n");
+               log("\n");
+       }
+       void execute(std::vector<std::string> args, RTLIL::Design *design) YS_OVERRIDE
+       {
+               log_header(design, "Executing PARAMAP pass (move or copy cell parameters).\n");
+
+               vector<std::unique_ptr<AttrmapAction>> actions;
+
+               size_t argidx;
+               for (argidx = 1; argidx < args.size(); argidx++)
+               {
+                       if (parse_attrmap_paramap_options(argidx, args, actions))
+                               continue;
+                       break;
+               }
+               extra_args(args, argidx, design);
+
+               for (auto module : design->selected_modules())
+               for (auto cell : module->selected_cells())
+                       attrmap_apply(stringf("%s.%s", log_id(module), log_id(cell)), actions, cell->parameters);
+       }
+} ParamapPass;
+
 PRIVATE_NAMESPACE_END