yosys-master
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
rename.cc
Go to the documentation of this file.
1 /*
2  * yosys -- Yosys Open SYnthesis Suite
3  *
4  * Copyright (C) 2012 Clifford Wolf <clifford@clifford.at>
5  *
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.
9  *
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.
17  *
18  */
19 
20 #include "kernel/register.h"
21 #include "kernel/rtlil.h"
22 #include "kernel/log.h"
23 
26 
27 static void rename_in_module(RTLIL::Module *module, std::string from_name, std::string to_name)
28 {
29  from_name = RTLIL::escape_id(from_name);
30  to_name = RTLIL::escape_id(to_name);
31 
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());
34 
35  for (auto &it : module->wires_)
36  if (it.first == from_name) {
37  log("Renaming wire %s to %s in module %s.\n", log_id(it.second), log_id(to_name), log_id(module));
38  module->rename(it.second, to_name);
39  if (it.second->port_id)
40  module->fixup_ports();
41  return;
42  }
43 
44  for (auto &it : module->cells_)
45  if (it.first == from_name) {
46  log("Renaming cell %s to %s in module %s.\n", log_id(it.second), log_id(to_name), log_id(module));
47  module->rename(it.second, to_name);
48  return;
49  }
50 
51  log_cmd_error("Object `%s' not found!\n", from_name.c_str());
52 }
53 
54 struct RenamePass : public Pass {
55  RenamePass() : Pass("rename", "rename object in the design") { }
56  virtual void help()
57  {
58  // |---v---|---v---|---v---|---v---|---v---|---v---|---v---|---v---|---v---|---v---|
59  log("\n");
60  log(" rename old_name new_name\n");
61  log("\n");
62  log("Rename the specified object. Note that selection patterns are not supported\n");
63  log("by this command.\n");
64  log("\n");
65  log("\n");
66  log(" rename -enumerate [-pattern <pattern>] [selection]\n");
67  log("\n");
68  log("Assign short auto-generated names to all selected wires and cells with private\n");
69  log("names. The -pattern option can be used to set the pattern for the new names.\n");
70  log("The character %% in the pattern is replaced with a integer number. The default\n");
71  log("pattern is '_%%_'.\n");
72  log("\n");
73  log(" rename -hide [selection]\n");
74  log("\n");
75  log("Assign private names (the ones with $-prefix) to all selected wires and cells\n");
76  log("with public names. This ignores all selected ports.\n");
77  log("\n");
78  }
79  virtual void execute(std::vector<std::string> args, RTLIL::Design *design)
80  {
81  std::string pattern_prefix = "_", pattern_suffix = "_";
82  bool flag_enumerate = false;
83  bool flag_hide = false;
84  bool got_mode = false;
85 
86  size_t argidx;
87  for (argidx = 1; argidx < args.size(); argidx++)
88  {
89  std::string arg = args[argidx];
90  if (arg == "-enumerate" && !got_mode) {
91  flag_enumerate = true;
92  got_mode = true;
93  continue;
94  }
95  if (arg == "-hide" && !got_mode) {
96  flag_hide = true;
97  got_mode = true;
98  continue;
99  }
100  if (arg == "-pattern" && argidx+1 < args.size() && args[argidx+1].find('%') != std::string::npos) {
101  int pos = args[++argidx].find('%');
102  pattern_prefix = args[argidx].substr(0, pos);
103  pattern_suffix = args[argidx].substr(pos+1);
104  continue;
105  }
106  break;
107  }
108 
109  if (flag_enumerate)
110  {
111  extra_args(args, argidx, design);
112 
113  for (auto &mod : design->modules_)
114  {
115  int counter = 0;
116 
117  RTLIL::Module *module = mod.second;
118  if (!design->selected(module))
119  continue;
120 
121  std::map<RTLIL::IdString, RTLIL::Wire*> new_wires;
122  for (auto &it : module->wires_) {
123  if (it.first[0] == '$' && design->selected(module, it.second))
124  do it.second->name = stringf("\\%s%d%s", pattern_prefix.c_str(), counter++, pattern_suffix.c_str());
125  while (module->count_id(it.second->name) > 0);
126  new_wires[it.second->name] = it.second;
127  }
128  module->wires_.swap(new_wires);
129  module->fixup_ports();
130 
131  std::map<RTLIL::IdString, RTLIL::Cell*> new_cells;
132  for (auto &it : module->cells_) {
133  if (it.first[0] == '$' && design->selected(module, it.second))
134  do it.second->name = stringf("\\%s%d%s", pattern_prefix.c_str(), counter++, pattern_suffix.c_str());
135  while (module->count_id(it.second->name) > 0);
136  new_cells[it.second->name] = it.second;
137  }
138  module->cells_.swap(new_cells);
139  }
140  }
141  else
142  if (flag_hide)
143  {
144  extra_args(args, argidx, design);
145 
146  for (auto &mod : design->modules_)
147  {
148  RTLIL::Module *module = mod.second;
149  if (!design->selected(module))
150  continue;
151 
152  std::map<RTLIL::IdString, RTLIL::Wire*> new_wires;
153  for (auto &it : module->wires_) {
154  if (design->selected(module, it.second))
155  if (it.first[0] == '\\' && it.second->port_id == 0)
156  it.second->name = NEW_ID;
157  new_wires[it.second->name] = it.second;
158  }
159  module->wires_.swap(new_wires);
160  module->fixup_ports();
161 
162  std::map<RTLIL::IdString, RTLIL::Cell*> new_cells;
163  for (auto &it : module->cells_) {
164  if (design->selected(module, it.second))
165  if (it.first[0] == '\\')
166  it.second->name = NEW_ID;
167  new_cells[it.second->name] = it.second;
168  }
169  module->cells_.swap(new_cells);
170  }
171  }
172  else
173  {
174  if (argidx+2 != args.size())
175  log_cmd_error("Invalid number of arguments!\n");
176 
177  std::string from_name = args[argidx++];
178  std::string to_name = args[argidx++];
179 
180  if (!design->selected_active_module.empty())
181  {
182  if (design->modules_.count(design->selected_active_module) > 0)
183  rename_in_module(design->modules_.at(design->selected_active_module), from_name, to_name);
184  }
185  else
186  {
187  for (auto &mod : design->modules_) {
188  if (mod.first == from_name || RTLIL::unescape_id(mod.first) == from_name) {
189  to_name = RTLIL::escape_id(to_name);
190  log("Renaming module %s to %s.\n", mod.first.c_str(), to_name.c_str());
191  RTLIL::Module *module = mod.second;
192  design->modules_.erase(module->name);
193  module->name = to_name;
194  design->modules_[module->name] = module;
195  goto rename_ok;
196  }
197  }
198 
199  log_cmd_error("Object `%s' not found!\n", from_name.c_str());
200  rename_ok:;
201  }
202  }
203  }
204 } RenamePass;
205 
const char * c_str() const
Definition: rtlil.h:178
bool selected(T1 *module) const
Definition: rtlil.h:551
std::string stringf(const char *fmt,...)
Definition: yosys.cc:58
RenamePass RenamePass
std::map< RTLIL::IdString, RTLIL::Wire * > wires_
Definition: rtlil.h:595
static std::string unescape_id(std::string str)
Definition: rtlil.h:257
virtual void help()
Definition: rename.cc:56
RTLIL::Module * module
Definition: abc.cc:94
virtual void execute(std::vector< std::string > args, RTLIL::Design *design)
Definition: rename.cc:79
static std::string escape_id(std::string str)
Definition: rtlil.h:251
#define PRIVATE_NAMESPACE_BEGIN
Definition: yosys.h:97
void fixup_ports()
Definition: rtlil.cc:1312
RTLIL::IdString name
Definition: rtlil.h:599
#define NEW_ID
Definition: yosys.h:166
USING_YOSYS_NAMESPACE static PRIVATE_NAMESPACE_BEGIN void rename_in_module(RTLIL::Module *module, std::string from_name, std::string to_name)
Definition: rename.cc:27
#define PRIVATE_NAMESPACE_END
Definition: yosys.h:98
Definition: register.h:27
void log_cmd_error(const char *format,...)
Definition: log.cc:211
#define USING_YOSYS_NAMESPACE
Definition: yosys.h:102
virtual size_t count_id(RTLIL::IdString id)
Definition: rtlil.cc:472
std::map< RTLIL::IdString, RTLIL::Module * > modules_
Definition: rtlil.h:507
std::map< RTLIL::IdString, RTLIL::Cell * > cells_
Definition: rtlil.h:596
void log(const char *format,...)
Definition: log.cc:180
std::string selected_active_module
Definition: rtlil.h:511
void extra_args(std::vector< std::string > args, size_t argidx, RTLIL::Design *design, bool select=true)
Definition: register.cc:128
const char * log_id(RTLIL::IdString str)
Definition: log.cc:283
RenamePass()
Definition: rename.cc:55
void rename(RTLIL::Wire *wire, RTLIL::IdString new_name)
Definition: rtlil.cc:1185