Merge branch 'master' into wandwor
[yosys.git] / passes / techmap / attrmap.cc
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/yosys.h"
21 #include "kernel/sigtools.h"
22
23 USING_YOSYS_NAMESPACE
24 PRIVATE_NAMESPACE_BEGIN
25
26 Const make_value(string &value)
27 {
28 if (GetSize(value) >= 2 && value.front() == '"' && value.back() == '"')
29 return Const(value.substr(1, GetSize(value)-2));
30
31 SigSpec sig;
32 SigSpec::parse(sig, nullptr, value);
33 return sig.as_const();
34 }
35
36 bool string_compare_nocase(const string &str1, const string &str2)
37 {
38 if (str1.size() != str2.size())
39 return false;
40
41 for (size_t i = 0; i < str1.size(); i++)
42 {
43 char ch1 = str1[i], ch2 = str2[i];
44 if ('a' <= ch1 && ch1 <= 'z')
45 ch1 -= 'a' - 'A';
46 if ('a' <= ch2 && ch2 <= 'z')
47 ch2 -= 'a' - 'A';
48 if (ch1 != ch2)
49 return false;
50 }
51
52 return true;
53 }
54
55 bool match_name(string &name, IdString &id, bool ignore_case=false)
56 {
57 string str1 = RTLIL::escape_id(name);
58 string str2 = id.str();
59
60 if (ignore_case)
61 return string_compare_nocase(str1, str2);
62
63 return str1 == str2;
64 }
65
66 bool match_value(string &value, Const &val, bool ignore_case=false)
67 {
68 if (ignore_case && ((val.flags & RTLIL::CONST_FLAG_STRING) != 0) && GetSize(value) && value.front() == '"' && value.back() == '"') {
69 string str1 = value.substr(1, GetSize(value)-2);
70 string str2 = val.decode_string();
71 return string_compare_nocase(str1, str2);
72 }
73
74 return make_value(value) == val;
75 }
76
77 struct AttrmapAction {
78 virtual ~AttrmapAction() { }
79 virtual bool apply(IdString &id, Const &val) = 0;
80 };
81
82 struct AttrmapTocase : AttrmapAction {
83 string name;
84 bool apply(IdString &id, Const&) YS_OVERRIDE {
85 if (match_name(name, id, true))
86 id = RTLIL::escape_id(name);
87 return true;
88 }
89 };
90
91 struct AttrmapRename : AttrmapAction {
92 string old_name, new_name;
93 bool apply(IdString &id, Const&) YS_OVERRIDE {
94 if (match_name(old_name, id))
95 id = RTLIL::escape_id(new_name);
96 return true;
97 }
98 };
99
100 struct AttrmapMap : AttrmapAction {
101 bool imap;
102 string old_name, new_name;
103 string old_value, new_value;
104 bool apply(IdString &id, Const &val) YS_OVERRIDE {
105 if (match_name(old_name, id) && match_value(old_value, val, true)) {
106 id = RTLIL::escape_id(new_name);
107 val = make_value(new_value);
108 }
109 return true;
110 }
111 };
112
113 struct AttrmapRemove : AttrmapAction {
114 bool has_value;
115 string name, value;
116 bool apply(IdString &id, Const &val) YS_OVERRIDE {
117 return !(match_name(name, id) && (!has_value || match_value(value, val)));
118 }
119 };
120
121 void attrmap_apply(string objname, vector<std::unique_ptr<AttrmapAction>> &actions, dict<RTLIL::IdString, RTLIL::Const> &attributes)
122 {
123 dict<RTLIL::IdString, RTLIL::Const> new_attributes;
124
125 for (auto attr : attributes)
126 {
127 auto new_attr = attr;
128 for (auto &action : actions)
129 if (!action->apply(new_attr.first, new_attr.second))
130 goto delete_this_attr;
131
132 if (new_attr != attr)
133 log("Changed attribute on %s: %s=%s -> %s=%s\n", objname.c_str(),
134 log_id(attr.first), log_const(attr.second), log_id(new_attr.first), log_const(new_attr.second));
135
136 new_attributes[new_attr.first] = new_attr.second;
137
138 if (0)
139 delete_this_attr:
140 log("Removed attribute on %s: %s=%s\n", objname.c_str(), log_id(attr.first), log_const(attr.second));
141 }
142
143 attributes.swap(new_attributes);
144 }
145
146 struct AttrmapPass : public Pass {
147 AttrmapPass() : Pass("attrmap", "renaming attributes") { }
148 void help() YS_OVERRIDE
149 {
150 // |---v---|---v---|---v---|---v---|---v---|---v---|---v---|---v---|---v---|---v---|
151 log("\n");
152 log(" attrmap [options] [selection]\n");
153 log("\n");
154 log("This command renames attributes and/or mapps key/value pairs to\n");
155 log("other key/value pairs.\n");
156 log("\n");
157 log(" -tocase <name>\n");
158 log(" Match attribute names case-insensitively and set it to the specified\n");
159 log(" name.\n");
160 log("\n");
161 log(" -rename <old_name> <new_name>\n");
162 log(" Rename attributes as specified\n");
163 log("\n");
164 log(" -map <old_name>=<old_value> <new_name>=<new_value>\n");
165 log(" Map key/value pairs as indicated.\n");
166 log("\n");
167 log(" -imap <old_name>=<old_value> <new_name>=<new_value>\n");
168 log(" Like -map, but use case-insensitive match for <old_value> when\n");
169 log(" it is a string value.\n");
170 log("\n");
171 log(" -remove <name>=<value>\n");
172 log(" Remove attributes matching this pattern.\n");
173 log("\n");
174 log(" -modattr\n");
175 log(" Operate on module attributes instead of attributes on wires and cells.\n");
176 log("\n");
177 log("For example, mapping Xilinx-style \"keep\" attributes to Yosys-style:\n");
178 log("\n");
179 log(" attrmap -tocase keep -imap keep=\"true\" keep=1 \\\n");
180 log(" -imap keep=\"false\" keep=0 -remove keep=0\n");
181 log("\n");
182 }
183 void execute(std::vector<std::string> args, RTLIL::Design *design) YS_OVERRIDE
184 {
185 log_header(design, "Executing ATTRMAP pass (move or copy attributes).\n");
186
187 bool modattr_mode = false;
188 vector<std::unique_ptr<AttrmapAction>> actions;
189
190 size_t argidx;
191 for (argidx = 1; argidx < args.size(); argidx++)
192 {
193 std::string arg = args[argidx];
194 if (arg == "-tocase" && argidx+1 < args.size()) {
195 auto action = new AttrmapTocase;
196 action->name = args[++argidx];
197 actions.push_back(std::unique_ptr<AttrmapAction>(action));
198 continue;
199 }
200 if (arg == "-rename" && argidx+2 < args.size()) {
201 auto action = new AttrmapRename;
202 action->old_name = args[++argidx];
203 action->new_name = args[++argidx];
204 actions.push_back(std::unique_ptr<AttrmapAction>(action));
205 continue;
206 }
207 if ((arg == "-map" || arg == "-imap") && argidx+2 < args.size()) {
208 string arg1 = args[++argidx];
209 string arg2 = args[++argidx];
210 string val1, val2;
211 size_t p = arg1.find("=");
212 if (p != string::npos) {
213 val1 = arg1.substr(p+1);
214 arg1 = arg1.substr(0, p);
215 }
216 p = arg2.find("=");
217 if (p != string::npos) {
218 val2 = arg2.substr(p+1);
219 arg2 = arg2.substr(0, p);
220 }
221 auto action = new AttrmapMap;
222 action->imap = (arg == "-map");
223 action->old_name = arg1;
224 action->new_name = arg2;
225 action->old_value = val1;
226 action->new_value = val2;
227 actions.push_back(std::unique_ptr<AttrmapAction>(action));
228 continue;
229 }
230 if (arg == "-remove" && argidx+1 < args.size()) {
231 string arg1 = args[++argidx], val1;
232 size_t p = arg1.find("=");
233 if (p != string::npos) {
234 val1 = arg1.substr(p+1);
235 arg1 = arg1.substr(0, p);
236 }
237 auto action = new AttrmapRemove;
238 action->name = arg1;
239 action->has_value = (p != string::npos);
240 action->value = val1;
241 actions.push_back(std::unique_ptr<AttrmapAction>(action));
242 continue;
243 }
244 if (arg == "-modattr") {
245 modattr_mode = true;
246 continue;
247 }
248 break;
249 }
250 extra_args(args, argidx, design);
251
252 if (modattr_mode)
253 {
254 for (auto module : design->selected_whole_modules())
255 attrmap_apply(stringf("%s", log_id(module)), actions, module->attributes);
256 }
257 else
258 {
259 for (auto module : design->selected_modules())
260 {
261 for (auto wire : module->selected_wires())
262 attrmap_apply(stringf("%s.%s", log_id(module), log_id(wire)), actions, wire->attributes);
263
264 for (auto cell : module->selected_cells())
265 attrmap_apply(stringf("%s.%s", log_id(module), log_id(cell)), actions, cell->attributes);
266 }
267 }
268 }
269 } AttrmapPass;
270
271 PRIVATE_NAMESPACE_END