yosys-master
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
setattr.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 struct setunset_t
28 {
31  bool unset;
32 
33  setunset_t(std::string unset_name) : name(RTLIL::escape_id(unset_name)), value(), unset(true) { }
34 
35  setunset_t(std::string set_name, std::vector<std::string> args, size_t &argidx) : name(RTLIL::escape_id(set_name)), value(), unset(false)
36  {
37  if (!args[argidx].empty() && args[argidx][0] == '"') {
38  std::string str = args[argidx++].substr(1);
39  while (str.size() != 0 && str[str.size()-1] != '"' && argidx < args.size())
40  str += args[argidx++];
41  if (str.size() != 0 && str[str.size()-1] == '"')
42  str = str.substr(0, str.size()-1);
43  value = RTLIL::Const(str);
44  } else {
45  RTLIL::SigSpec sig_value;
46  if (!RTLIL::SigSpec::parse(sig_value, NULL, args[argidx++]))
47  log_cmd_error("Can't decode value '%s'!\n", args[argidx-1].c_str());
48  value = sig_value.as_const();
49  }
50  }
51 };
52 
53 static void do_setunset(std::map<RTLIL::IdString, RTLIL::Const> &attrs, std::vector<setunset_t> &list)
54 {
55  for (auto &item : list)
56  if (item.unset)
57  attrs.erase(item.name);
58  else
59  attrs[item.name] = item.value;
60 }
61 
62 struct SetattrPass : public Pass {
63  SetattrPass() : Pass("setattr", "set/unset attributes on objects") { }
64  virtual void help()
65  {
66  // |---v---|---v---|---v---|---v---|---v---|---v---|---v---|---v---|---v---|---v---|
67  log("\n");
68  log(" setattr [ -mod ] [ -set name value | -unset name ]... [selection]\n");
69  log("\n");
70  log("Set/unset the given attributes on the selected objects. String values must be\n");
71  log("passed in double quotes (\").\n");
72  log("\n");
73  log("When called with -mod, this command will set and unset attributes on modules\n");
74  log("instead of objects within modules.\n");
75  log("\n");
76  }
77  virtual void execute(std::vector<std::string> args, RTLIL::Design *design)
78  {
79  std::vector<setunset_t> setunset_list;
80  bool flag_mod = false;
81 
82  size_t argidx;
83  for (argidx = 1; argidx < args.size(); argidx++)
84  {
85  std::string arg = args[argidx];
86  if (arg == "-set" && argidx+2 < args.size()) {
87  argidx += 2;
88  setunset_list.push_back(setunset_t(args[argidx-1], args, argidx));
89  argidx--;
90  continue;
91  }
92  if (arg == "-unset" && argidx+1 < args.size()) {
93  setunset_list.push_back(setunset_t(args[++argidx]));
94  continue;
95  }
96  if (arg == "-mod") {
97  flag_mod = true;
98  continue;
99  }
100  break;
101  }
102  extra_args(args, argidx, design);
103 
104  for (auto &mod : design->modules_)
105  {
106  RTLIL::Module *module = mod.second;
107 
108  if (flag_mod) {
109  if (design->selected_whole_module(module->name))
110  do_setunset(module->attributes, setunset_list);
111  continue;
112  }
113 
114  if (!design->selected(module))
115  continue;
116 
117  for (auto &it : module->wires_)
118  if (design->selected(module, it.second))
119  do_setunset(it.second->attributes, setunset_list);
120 
121  for (auto &it : module->memories)
122  if (design->selected(module, it.second))
123  do_setunset(it.second->attributes, setunset_list);
124 
125  for (auto &it : module->cells_)
126  if (design->selected(module, it.second))
127  do_setunset(it.second->attributes, setunset_list);
128 
129  for (auto &it : module->processes)
130  if (design->selected(module, it.second))
131  do_setunset(it.second->attributes, setunset_list);
132  }
133  }
134 } SetattrPass;
135 
136 struct SetparamPass : public Pass {
137  SetparamPass() : Pass("setparam", "set/unset parameters on objects") { }
138  virtual void help()
139  {
140  // |---v---|---v---|---v---|---v---|---v---|---v---|---v---|---v---|---v---|---v---|
141  log("\n");
142  log(" setparam [ -set name value | -unset name ]... [selection]\n");
143  log("\n");
144  log("Set/unset the given parameters on the selected cells. String values must be\n");
145  log("passed in double quotes (\").\n");
146  log("\n");
147  }
148  virtual void execute(std::vector<std::string> args, RTLIL::Design *design)
149  {
150  std::vector<setunset_t> setunset_list;
151 
152  size_t argidx;
153  for (argidx = 1; argidx < args.size(); argidx++)
154  {
155  std::string arg = args[argidx];
156  if (arg == "-set" && argidx+2 < args.size()) {
157  argidx += 2;
158  setunset_list.push_back(setunset_t(args[argidx-1], args, argidx));
159  argidx--;
160  continue;
161  }
162  if (arg == "-unset" && argidx+1 < args.size()) {
163  setunset_list.push_back(setunset_t(args[++argidx]));
164  continue;
165  }
166  break;
167  }
168  extra_args(args, argidx, design);
169 
170  for (auto &mod : design->modules_)
171  {
172  RTLIL::Module *module = mod.second;
173 
174  if (!design->selected(module))
175  continue;
176 
177  for (auto &it : module->cells_)
178  if (design->selected(module, it.second))
179  do_setunset(it.second->parameters, setunset_list);
180  }
181  }
182 } SetparamPass;
183 
bool selected(T1 *module) const
Definition: rtlil.h:551
RTLIL::Const as_const() const
Definition: rtlil.cc:2857
std::map< RTLIL::IdString, RTLIL::Wire * > wires_
Definition: rtlil.h:595
virtual void execute(std::vector< std::string > args, RTLIL::Design *design)
Definition: setattr.cc:77
std::map< RTLIL::IdString, RTLIL::Memory * > memories
Definition: rtlil.h:601
static void do_setunset(std::map< RTLIL::IdString, RTLIL::Const > &attrs, std::vector< setunset_t > &list)
Definition: setattr.cc:53
RTLIL::Module * module
Definition: abc.cc:94
SetattrPass SetattrPass
static std::string escape_id(std::string str)
Definition: rtlil.h:251
setunset_t(std::string unset_name)
Definition: setattr.cc:33
#define PRIVATE_NAMESPACE_BEGIN
Definition: yosys.h:97
RTLIL::IdString name
Definition: rtlil.h:599
SetparamPass SetparamPass
bool selected_whole_module(RTLIL::IdString mod_name) const
Definition: rtlil.cc:388
#define PRIVATE_NAMESPACE_END
Definition: yosys.h:98
Definition: register.h:27
void log_cmd_error(const char *format,...)
Definition: log.cc:211
bool unset
Definition: setattr.cc:31
std::map< RTLIL::IdString, RTLIL::Process * > processes
Definition: rtlil.h:602
virtual void help()
Definition: setattr.cc:138
#define USING_YOSYS_NAMESPACE
Definition: yosys.h:102
std::map< RTLIL::IdString, RTLIL::Module * > modules_
Definition: rtlil.h:507
#define NULL
std::map< RTLIL::IdString, RTLIL::Cell * > cells_
Definition: rtlil.h:596
SetattrPass()
Definition: setattr.cc:63
virtual void execute(std::vector< std::string > args, RTLIL::Design *design)
Definition: setattr.cc:148
void log(const char *format,...)
Definition: log.cc:180
setunset_t(std::string set_name, std::vector< std::string > args, size_t &argidx)
Definition: setattr.cc:35
RTLIL::IdString name
Definition: setattr.cc:29
void extra_args(std::vector< std::string > args, size_t argidx, RTLIL::Design *design, bool select=true)
Definition: register.cc:128
RTLIL::Const value
Definition: setattr.cc:30
static bool parse(RTLIL::SigSpec &sig, RTLIL::Module *module, std::string str)
Definition: rtlil.cc:2972
virtual void help()
Definition: setattr.cc:64