2 * yosys -- Yosys Open SYnthesis Suite
4 * Copyright (C) 2012 Clifford Wolf <clifford@clifford.at>
6 * Permission to use, copy, modify, and/or distribute this software for any
7 * purpose with or without fee is hereby granted, provided that the above
8 * copyright notice and this permission notice appear in all copies.
10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
20 #include "kernel/register.h"
21 #include "kernel/rtlil.h"
22 #include "kernel/log.h"
25 PRIVATE_NAMESPACE_BEGIN
27 static void rename_in_module(RTLIL::Module
*module
, std::string from_name
, std::string to_name
)
29 from_name
= RTLIL::escape_id(from_name
);
30 to_name
= RTLIL::escape_id(to_name
);
32 if (module
->count_id(to_name
))
33 log_cmd_error("There is already an object `%s' in module `%s'.\n", to_name
.c_str(), module
->name
.c_str());
35 for (auto &it
: module
->wires_
)
36 if (it
.first
== from_name
) {
38 log("Renaming wire %s to %s in module %s.\n", log_id(w
), log_id(to_name
), log_id(module
));
39 module
->rename(w
, to_name
);
41 module
->fixup_ports();
45 for (auto &it
: module
->cells_
)
46 if (it
.first
== from_name
) {
47 log("Renaming cell %s to %s in module %s.\n", log_id(it
.second
), log_id(to_name
), log_id(module
));
48 module
->rename(it
.second
, to_name
);
52 log_cmd_error("Object `%s' not found!\n", from_name
.c_str());
55 struct RenamePass
: public Pass
{
56 RenamePass() : Pass("rename", "rename object in the design") { }
59 // |---v---|---v---|---v---|---v---|---v---|---v---|---v---|---v---|---v---|---v---|
61 log(" rename old_name new_name\n");
63 log("Rename the specified object. Note that selection patterns are not supported\n");
64 log("by this command.\n");
67 log(" rename -enumerate [-pattern <pattern>] [selection]\n");
69 log("Assign short auto-generated names to all selected wires and cells with private\n");
70 log("names. The -pattern option can be used to set the pattern for the new names.\n");
71 log("The character %% in the pattern is replaced with a integer number. The default\n");
72 log("pattern is '_%%_'.\n");
74 log(" rename -hide [selection]\n");
76 log("Assign private names (the ones with $-prefix) to all selected wires and cells\n");
77 log("with public names. This ignores all selected ports.\n");
79 log(" rename -top new_name\n");
81 log("Rename top module.\n");
84 virtual void execute(std::vector
<std::string
> args
, RTLIL::Design
*design
)
86 std::string pattern_prefix
= "_", pattern_suffix
= "_";
87 bool flag_enumerate
= false;
88 bool flag_hide
= false;
89 bool flag_top
= false;
90 bool got_mode
= false;
93 for (argidx
= 1; argidx
< args
.size(); argidx
++)
95 std::string arg
= args
[argidx
];
96 if (arg
== "-enumerate" && !got_mode
) {
97 flag_enumerate
= true;
101 if (arg
== "-hide" && !got_mode
) {
106 if (arg
== "-top" && !got_mode
) {
111 if (arg
== "-pattern" && argidx
+1 < args
.size() && args
[argidx
+1].find('%') != std::string::npos
) {
112 int pos
= args
[++argidx
].find('%');
113 pattern_prefix
= args
[argidx
].substr(0, pos
);
114 pattern_suffix
= args
[argidx
].substr(pos
+1);
122 extra_args(args
, argidx
, design
);
124 for (auto &mod
: design
->modules_
)
128 RTLIL::Module
*module
= mod
.second
;
129 if (!design
->selected(module
))
132 dict
<RTLIL::IdString
, RTLIL::Wire
*> new_wires
;
133 for (auto &it
: module
->wires_
) {
134 if (it
.first
[0] == '$' && design
->selected(module
, it
.second
))
135 do it
.second
->name
= stringf("\\%s%d%s", pattern_prefix
.c_str(), counter
++, pattern_suffix
.c_str());
136 while (module
->count_id(it
.second
->name
) > 0);
137 new_wires
[it
.second
->name
] = it
.second
;
139 module
->wires_
.swap(new_wires
);
140 module
->fixup_ports();
142 dict
<RTLIL::IdString
, RTLIL::Cell
*> new_cells
;
143 for (auto &it
: module
->cells_
) {
144 if (it
.first
[0] == '$' && design
->selected(module
, it
.second
))
145 do it
.second
->name
= stringf("\\%s%d%s", pattern_prefix
.c_str(), counter
++, pattern_suffix
.c_str());
146 while (module
->count_id(it
.second
->name
) > 0);
147 new_cells
[it
.second
->name
] = it
.second
;
149 module
->cells_
.swap(new_cells
);
155 extra_args(args
, argidx
, design
);
157 for (auto &mod
: design
->modules_
)
159 RTLIL::Module
*module
= mod
.second
;
160 if (!design
->selected(module
))
163 dict
<RTLIL::IdString
, RTLIL::Wire
*> new_wires
;
164 for (auto &it
: module
->wires_
) {
165 if (design
->selected(module
, it
.second
))
166 if (it
.first
[0] == '\\' && it
.second
->port_id
== 0)
167 it
.second
->name
= NEW_ID
;
168 new_wires
[it
.second
->name
] = it
.second
;
170 module
->wires_
.swap(new_wires
);
171 module
->fixup_ports();
173 dict
<RTLIL::IdString
, RTLIL::Cell
*> new_cells
;
174 for (auto &it
: module
->cells_
) {
175 if (design
->selected(module
, it
.second
))
176 if (it
.first
[0] == '\\')
177 it
.second
->name
= NEW_ID
;
178 new_cells
[it
.second
->name
] = it
.second
;
180 module
->cells_
.swap(new_cells
);
186 if (argidx
+1 != args
.size())
187 log_cmd_error("Invalid number of arguments!\n");
189 IdString new_name
= RTLIL::escape_id(args
[argidx
]);
190 RTLIL::Module
*module
= design
->top_module();
193 log_cmd_error("No top module found!\n");
195 log("Renaming module %s to %s.\n", log_id(module
), log_id(new_name
));
196 design
->rename(module
, new_name
);
200 if (argidx
+2 != args
.size())
201 log_cmd_error("Invalid number of arguments!\n");
203 std::string from_name
= args
[argidx
++];
204 std::string to_name
= args
[argidx
++];
206 if (!design
->selected_active_module
.empty())
208 if (design
->modules_
.count(design
->selected_active_module
) > 0)
209 rename_in_module(design
->modules_
.at(design
->selected_active_module
), from_name
, to_name
);
213 for (auto &mod
: design
->modules_
) {
214 if (mod
.first
== from_name
|| RTLIL::unescape_id(mod
.first
) == from_name
) {
215 to_name
= RTLIL::escape_id(to_name
);
216 log("Renaming module %s to %s.\n", mod
.first
.c_str(), to_name
.c_str());
217 RTLIL::Module
*module
= mod
.second
;
218 design
->modules_
.erase(module
->name
);
219 module
->name
= to_name
;
220 design
->modules_
[module
->name
] = module
;
225 log_cmd_error("Object `%s' not found!\n", from_name
.c_str());
232 PRIVATE_NAMESPACE_END