Merge pull request #23 from hansiglaser/master
[yosys.git] / passes / cmds / select.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/register.h"
21 #include "kernel/celltypes.h"
22 #include "kernel/log.h"
23 #include <string.h>
24 #include <fnmatch.h>
25
26 using RTLIL::id2cstr;
27
28 static std::vector<RTLIL::Selection> work_stack;
29
30 static bool match_ids(RTLIL::IdString id, std::string pattern)
31 {
32 if (id == pattern)
33 return true;
34 if (id.size() > 0 && id[0] == '\\' && id.substr(1) == pattern)
35 return true;
36 if (!fnmatch(pattern.c_str(), id.c_str(), 0))
37 return true;
38 if (id.size() > 0 && id[0] == '\\' && !fnmatch(pattern.c_str(), id.substr(1).c_str(), 0))
39 return true;
40 if (id.size() > 0 && id[0] == '$' && pattern.size() > 0 && pattern[0] == '$') {
41 const char *p = id.c_str();
42 const char *q = strrchr(p, '$');
43 if (pattern == q)
44 return true;
45 }
46 return false;
47 }
48
49 static bool match_attr_val(const RTLIL::Const &value, std::string pattern, char match_op)
50 {
51 if (match_op == 0)
52 return true;
53
54 if ((value.flags & RTLIL::CONST_FLAG_STRING) == 0)
55 {
56 RTLIL::SigSpec sig_value;
57
58 if (!RTLIL::SigSpec::parse(sig_value, NULL, pattern))
59 return false;
60
61 RTLIL::Const pattern_value = sig_value.as_const();
62
63 if (match_op == '=')
64 return value == pattern_value;
65 if (match_op == '<')
66 return value.as_int() < pattern_value.as_int();
67 if (match_op == '>')
68 return value.as_int() > pattern_value.as_int();
69 if (match_op == '[')
70 return value.as_int() <= pattern_value.as_int();
71 if (match_op == ']')
72 return value.as_int() >= pattern_value.as_int();
73 }
74 else
75 {
76 std::string value_str = value.decode_string();
77
78 if (match_op == '=')
79 if (!fnmatch(pattern.c_str(), value.decode_string().c_str(), FNM_NOESCAPE))
80 return true;
81
82 if (match_op == '=')
83 return value_str == pattern;
84 if (match_op == '<')
85 return value_str < pattern;
86 if (match_op == '>')
87 return value_str > pattern;
88 if (match_op == '[')
89 return value_str <= pattern;
90 if (match_op == ']')
91 return value_str >= pattern;
92 }
93
94 log_abort();
95 }
96
97 static bool match_attr(const std::map<RTLIL::IdString, RTLIL::Const> &attributes, std::string name_pat, std::string value_pat, char match_op)
98 {
99 if (name_pat.find('*') != std::string::npos || name_pat.find('?') != std::string::npos || name_pat.find('[') != std::string::npos) {
100 for (auto &it : attributes) {
101 if (!fnmatch(name_pat.c_str(), it.first.c_str(), FNM_NOESCAPE) && match_attr_val(it.second, value_pat, match_op))
102 return true;
103 if (it.first.size() > 0 && it.first[0] == '\\' && !fnmatch(name_pat.c_str(), it.first.substr(1).c_str(), FNM_NOESCAPE) && match_attr_val(it.second, value_pat, match_op))
104 return true;
105 }
106 } else {
107 if (name_pat.size() > 0 && (name_pat[0] == '\\' || name_pat[0] == '$') && attributes.count(name_pat) && match_attr_val(attributes.at(name_pat), value_pat, match_op))
108 return true;
109 if (attributes.count("\\" + name_pat) && match_attr_val(attributes.at("\\" + name_pat), value_pat, match_op))
110 return true;
111 }
112 return false;
113 }
114
115 static bool match_attr(const std::map<RTLIL::IdString, RTLIL::Const> &attributes, std::string match_expr)
116 {
117 size_t pos = match_expr.find_first_of("<=>");
118
119 if (pos != std::string::npos) {
120 if (match_expr.substr(pos, 2) == "<=")
121 return match_attr(attributes, match_expr.substr(0, pos), match_expr.substr(pos+2), '[');
122 if (match_expr.substr(pos, 2) == ">=")
123 return match_attr(attributes, match_expr.substr(0, pos), match_expr.substr(pos+2), ']');
124 return match_attr(attributes, match_expr.substr(0, pos), match_expr.substr(pos+1), match_expr[pos]);
125 }
126
127 return match_attr(attributes, match_expr, std::string(), 0);
128 }
129
130 static void select_op_neg(RTLIL::Design *design, RTLIL::Selection &lhs)
131 {
132 if (lhs.full_selection) {
133 lhs.full_selection = false;
134 lhs.selected_modules.clear();
135 lhs.selected_members.clear();
136 return;
137 }
138
139 if (lhs.selected_modules.size() == 0 && lhs.selected_members.size() == 0) {
140 lhs.full_selection = true;
141 return;
142 }
143
144 RTLIL::Selection new_sel(false);
145
146 for (auto &mod_it : design->modules)
147 {
148 if (lhs.selected_whole_module(mod_it.first))
149 continue;
150 if (!lhs.selected_module(mod_it.first)) {
151 new_sel.selected_modules.insert(mod_it.first);
152 continue;
153 }
154
155 RTLIL::Module *mod = mod_it.second;
156 for (auto &it : mod->wires)
157 if (!lhs.selected_member(mod_it.first, it.first))
158 new_sel.selected_members[mod->name].insert(it.first);
159 for (auto &it : mod->memories)
160 if (!lhs.selected_member(mod_it.first, it.first))
161 new_sel.selected_members[mod->name].insert(it.first);
162 for (auto &it : mod->cells)
163 if (!lhs.selected_member(mod_it.first, it.first))
164 new_sel.selected_members[mod->name].insert(it.first);
165 for (auto &it : mod->processes)
166 if (!lhs.selected_member(mod_it.first, it.first))
167 new_sel.selected_members[mod->name].insert(it.first);
168 }
169
170 lhs.selected_modules.swap(new_sel.selected_modules);
171 lhs.selected_members.swap(new_sel.selected_members);
172 }
173
174 static void select_op_submod(RTLIL::Design *design, RTLIL::Selection &lhs)
175 {
176 for (auto &mod_it : design->modules)
177 {
178 if (lhs.selected_whole_module(mod_it.first))
179 {
180 for (auto &cell_it : mod_it.second->cells)
181 {
182 if (design->modules.count(cell_it.second->type) == 0)
183 continue;
184 lhs.selected_modules.insert(cell_it.second->type);
185 }
186 }
187 }
188 }
189
190 static void select_op_union(RTLIL::Design*, RTLIL::Selection &lhs, const RTLIL::Selection &rhs)
191 {
192 if (rhs.full_selection) {
193 lhs.full_selection = true;
194 lhs.selected_modules.clear();
195 lhs.selected_members.clear();
196 return;
197 }
198
199 if (lhs.full_selection)
200 return;
201
202 for (auto &it : rhs.selected_members)
203 for (auto &it2 : it.second)
204 lhs.selected_members[it.first].insert(it2);
205
206 for (auto &it : rhs.selected_modules) {
207 lhs.selected_modules.insert(it);
208 lhs.selected_members.erase(it);
209 }
210 }
211
212 static void select_op_diff(RTLIL::Design *design, RTLIL::Selection &lhs, const RTLIL::Selection &rhs)
213 {
214 if (rhs.full_selection) {
215 lhs.full_selection = false;
216 lhs.selected_modules.clear();
217 lhs.selected_members.clear();
218 return;
219 }
220
221 if (lhs.full_selection) {
222 if (!rhs.full_selection && rhs.selected_modules.size() == 0 && rhs.selected_members.size() == 0)
223 return;
224 lhs.full_selection = false;
225 for (auto &it : design->modules)
226 lhs.selected_modules.insert(it.first);
227 }
228
229 for (auto &it : rhs.selected_modules) {
230 lhs.selected_modules.erase(it);
231 lhs.selected_members.erase(it);
232 }
233
234 for (auto &it : rhs.selected_members)
235 {
236 if (design->modules.count(it.first) == 0)
237 continue;
238
239 RTLIL::Module *mod = design->modules[it.first];
240
241 if (lhs.selected_modules.count(mod->name) > 0)
242 {
243 for (auto &it : mod->wires)
244 lhs.selected_members[mod->name].insert(it.first);
245 for (auto &it : mod->memories)
246 lhs.selected_members[mod->name].insert(it.first);
247 for (auto &it : mod->cells)
248 lhs.selected_members[mod->name].insert(it.first);
249 for (auto &it : mod->processes)
250 lhs.selected_members[mod->name].insert(it.first);
251 lhs.selected_modules.erase(mod->name);
252 }
253
254 if (lhs.selected_members.count(mod->name) == 0)
255 continue;
256
257 for (auto &it2 : it.second)
258 lhs.selected_members[mod->name].erase(it2);
259 }
260 }
261
262 static void select_op_intersect(RTLIL::Design *design, RTLIL::Selection &lhs, const RTLIL::Selection &rhs)
263 {
264 if (rhs.full_selection)
265 return;
266
267 if (lhs.full_selection) {
268 lhs.full_selection = false;
269 for (auto &it : design->modules)
270 lhs.selected_modules.insert(it.first);
271 }
272
273 std::vector<RTLIL::IdString> del_list;
274
275 for (auto &it : lhs.selected_modules)
276 if (rhs.selected_modules.count(it) == 0) {
277 if (rhs.selected_members.count(it) > 0)
278 for (auto &it2 : rhs.selected_members.at(it))
279 lhs.selected_members[it].insert(it2);
280 del_list.push_back(it);
281 }
282 for (auto &it : del_list)
283 lhs.selected_modules.erase(it);
284
285 del_list.clear();
286 for (auto &it : lhs.selected_members) {
287 if (rhs.selected_modules.count(it.first) > 0)
288 continue;
289 if (rhs.selected_members.count(it.first) == 0) {
290 del_list.push_back(it.first);
291 continue;
292 }
293 std::vector<RTLIL::IdString> del_list2;
294 for (auto &it2 : it.second)
295 if (rhs.selected_members.at(it.first).count(it2) == 0)
296 del_list2.push_back(it2);
297 for (auto &it2 : del_list2)
298 it.second.erase(it2);
299 if (it.second.size() == 0)
300 del_list.push_back(it.first);
301 }
302 for (auto &it : del_list)
303 lhs.selected_members.erase(it);
304 }
305
306 namespace {
307 struct expand_rule_t {
308 char mode;
309 std::set<RTLIL::IdString> cell_types, port_names;
310 };
311 }
312
313 static int parse_comma_list(std::set<RTLIL::IdString> &tokens, std::string str, size_t pos, std::string stopchar)
314 {
315 stopchar += ',';
316 while (1) {
317 size_t endpos = str.find_first_of(stopchar, pos);
318 if (endpos == std::string::npos)
319 endpos = str.size();
320 if (endpos != pos)
321 tokens.insert(RTLIL::escape_id(str.substr(pos, endpos-pos)));
322 pos = endpos;
323 if (pos == str.size() || str[pos] != ',')
324 return pos;
325 pos++;
326 }
327 }
328
329 static int select_op_expand(RTLIL::Design *design, RTLIL::Selection &lhs, std::vector<expand_rule_t> &rules, std::set<RTLIL::IdString> &limits, int max_objects, char mode, CellTypes &ct)
330 {
331 int sel_objects = 0;
332 bool is_input, is_output;
333 for (auto &mod_it : design->modules)
334 {
335 if (lhs.selected_whole_module(mod_it.first) || !lhs.selected_module(mod_it.first))
336 continue;
337
338 RTLIL::Module *mod = mod_it.second;
339 std::set<RTLIL::Wire*> selected_wires;
340
341 for (auto &it : mod->wires)
342 if (lhs.selected_member(mod_it.first, it.first) && limits.count(it.first) == 0)
343 selected_wires.insert(it.second);
344
345 for (auto &conn : mod->connections)
346 {
347 std::vector<RTLIL::SigBit> conn_lhs = conn.first.to_sigbit_vector();
348 std::vector<RTLIL::SigBit> conn_rhs = conn.second.to_sigbit_vector();
349
350 for (size_t i = 0; i < conn_lhs.size(); i++) {
351 if (conn_lhs[i].wire == NULL || conn_rhs[i].wire == NULL)
352 continue;
353 if (mode != 'i' && selected_wires.count(conn_rhs[i].wire) && lhs.selected_members[mod->name].count(conn_lhs[i].wire->name) == 0)
354 lhs.selected_members[mod->name].insert(conn_lhs[i].wire->name), sel_objects++, max_objects--;
355 if (mode != 'o' && selected_wires.count(conn_lhs[i].wire) && lhs.selected_members[mod->name].count(conn_rhs[i].wire->name) == 0)
356 lhs.selected_members[mod->name].insert(conn_rhs[i].wire->name), sel_objects++, max_objects--;
357 }
358 }
359
360 for (auto &cell : mod->cells)
361 for (auto &conn : cell.second->connections)
362 {
363 char last_mode = '-';
364 for (auto &rule : rules) {
365 last_mode = rule.mode;
366 if (rule.cell_types.size() > 0 && rule.cell_types.count(cell.second->type) == 0)
367 continue;
368 if (rule.port_names.size() > 0 && rule.port_names.count(conn.first) == 0)
369 continue;
370 if (rule.mode == '+')
371 goto include_match;
372 else
373 goto exclude_match;
374 }
375 if (last_mode == '+')
376 goto exclude_match;
377 include_match:
378 is_input = mode == 'x' || ct.cell_input(cell.second->type, conn.first);
379 is_output = mode == 'x' || ct.cell_output(cell.second->type, conn.first);
380 for (auto &chunk : conn.second.chunks)
381 if (chunk.wire != NULL) {
382 if (max_objects != 0 && selected_wires.count(chunk.wire) > 0 && lhs.selected_members[mod->name].count(cell.first) == 0)
383 if (mode == 'x' || (mode == 'i' && is_output) || (mode == 'o' && is_input))
384 lhs.selected_members[mod->name].insert(cell.first), sel_objects++, max_objects--;
385 if (max_objects != 0 && lhs.selected_members[mod->name].count(cell.first) > 0 && limits.count(cell.first) == 0 && lhs.selected_members[mod->name].count(chunk.wire->name) == 0)
386 if (mode == 'x' || (mode == 'i' && is_input) || (mode == 'o' && is_output))
387 lhs.selected_members[mod->name].insert(chunk.wire->name), sel_objects++, max_objects--;
388 }
389 exclude_match:;
390 }
391 }
392
393 return sel_objects;
394 }
395
396 static void select_op_expand(RTLIL::Design *design, std::string arg, char mode)
397 {
398 int pos = mode == 'x' ? 2 : 3, levels = 1, rem_objects = -1;
399 std::vector<expand_rule_t> rules;
400 std::set<RTLIL::IdString> limits;
401
402 CellTypes ct;
403
404 if (mode != 'x')
405 ct.setup(design);
406
407 if (pos < int(arg.size()) && arg[pos] == '*') {
408 levels = 1000000;
409 pos++;
410 } else
411 if (pos < int(arg.size()) && '0' <= arg[pos] && arg[pos] <= '9') {
412 size_t endpos = arg.find_first_not_of("0123456789", pos);
413 if (endpos == std::string::npos)
414 endpos = arg.size();
415 levels = atoi(arg.substr(pos, endpos-pos).c_str());
416 pos = endpos;
417 }
418
419 if (pos < int(arg.size()) && arg[pos] == '.') {
420 size_t endpos = arg.find_first_not_of("0123456789", ++pos);
421 if (endpos == std::string::npos)
422 endpos = arg.size();
423 if (int(endpos) > pos)
424 rem_objects = atoi(arg.substr(pos, endpos-pos).c_str());
425 pos = endpos;
426 }
427
428 while (pos < int(arg.size())) {
429 if (arg[pos] != ':' || pos+1 == int(arg.size()))
430 log_cmd_error("Syntax error in expand operator '%s'.\n", arg.c_str());
431 pos++;
432 if (arg[pos] == '+' || arg[pos] == '-') {
433 expand_rule_t rule;
434 rule.mode = arg[pos++];
435 pos = parse_comma_list(rule.cell_types, arg, pos, "[:");
436 if (pos < int(arg.size()) && arg[pos] == '[') {
437 pos = parse_comma_list(rule.port_names, arg, pos+1, "]:");
438 if (pos < int(arg.size()) && arg[pos] == ']')
439 pos++;
440 }
441 rules.push_back(rule);
442 } else {
443 size_t endpos = arg.find(':', pos);
444 if (endpos == std::string::npos)
445 endpos = arg.size();
446 if (int(endpos) > pos) {
447 std::string str = arg.substr(pos, endpos-pos);
448 if (str[0] == '@') {
449 str = RTLIL::escape_id(str.substr(1));
450 if (design->selection_vars.count(str) > 0) {
451 for (auto i1 : design->selection_vars.at(str).selected_members)
452 for (auto i2 : i1.second)
453 limits.insert(i2);
454 } else
455 log_cmd_error("Selection %s is not defined!\n", RTLIL::id2cstr(str));
456 } else
457 limits.insert(RTLIL::escape_id(str));
458 }
459 pos = endpos;
460 }
461 }
462
463 #if 0
464 log("expand by %d levels (max. %d objects):\n", levels, rem_objects);
465 for (auto &rule : rules) {
466 log(" rule (%c):\n", rule.mode);
467 if (rule.cell_types.size() > 0) {
468 log(" cell types:");
469 for (auto &it : rule.cell_types)
470 log(" %s", it.c_str());
471 log("\n");
472 }
473 if (rule.port_names.size() > 0) {
474 log(" port names:");
475 for (auto &it : rule.port_names)
476 log(" %s", it.c_str());
477 log("\n");
478 }
479 }
480 if (limits.size() > 0) {
481 log(" limits:");
482 for (auto &it : limits)
483 log(" %s", it.c_str());
484 log("\n");
485 }
486 #endif
487
488 while (levels-- > 0 && rem_objects != 0) {
489 int num_objects = select_op_expand(design, work_stack.back(), rules, limits, rem_objects, mode, ct);
490 if (num_objects == 0)
491 break;
492 rem_objects -= num_objects;
493 }
494
495 if (rem_objects == 0)
496 log("Warning: reached configured limit at `%s'.\n", arg.c_str());
497 }
498
499 static void select_filter_active_mod(RTLIL::Design *design, RTLIL::Selection &sel)
500 {
501 if (design->selected_active_module.empty())
502 return;
503
504 if (sel.full_selection) {
505 sel.full_selection = false;
506 sel.selected_modules.clear();
507 sel.selected_members.clear();
508 sel.selected_modules.insert(design->selected_active_module);
509 return;
510 }
511
512 std::vector<std::string> del_list;
513 for (auto mod_name : sel.selected_modules)
514 if (mod_name != design->selected_active_module)
515 del_list.push_back(mod_name);
516 for (auto &it : sel.selected_members)
517 if (it.first != design->selected_active_module)
518 del_list.push_back(it.first);
519 for (auto mod_name : del_list) {
520 sel.selected_modules.erase(mod_name);
521 sel.selected_members.erase(mod_name);
522 }
523 }
524
525 static void select_stmt(RTLIL::Design *design, std::string arg)
526 {
527 std::string arg_mod, arg_memb;
528
529 if (arg.size() == 0)
530 return;
531
532 if (arg[0] == '%') {
533 if (arg == "%") {
534 if (design->selection_stack.size() > 0)
535 work_stack.push_back(design->selection_stack.back());
536 } else
537 if (arg == "%%") {
538 while (work_stack.size() > 1) {
539 select_op_union(design, work_stack.front(), work_stack.back());
540 work_stack.pop_back();
541 }
542 } else
543 if (arg == "%n") {
544 if (work_stack.size() < 1)
545 log_cmd_error("Must have at least one element on the stack for operator %%n.\n");
546 select_op_neg(design, work_stack[work_stack.size()-1]);
547 } else
548 if (arg == "%u") {
549 if (work_stack.size() < 2)
550 log_cmd_error("Must have at least two elements on the stack for operator %%u.\n");
551 select_op_union(design, work_stack[work_stack.size()-2], work_stack[work_stack.size()-1]);
552 work_stack.pop_back();
553 } else
554 if (arg == "%d") {
555 if (work_stack.size() < 2)
556 log_cmd_error("Must have at least two elements on the stack for operator %%d.\n");
557 select_op_diff(design, work_stack[work_stack.size()-2], work_stack[work_stack.size()-1]);
558 work_stack.pop_back();
559 } else
560 if (arg == "%i") {
561 if (work_stack.size() < 2)
562 log_cmd_error("Must have at least two elements on the stack for operator %%i.\n");
563 select_op_intersect(design, work_stack[work_stack.size()-2], work_stack[work_stack.size()-1]);
564 work_stack.pop_back();
565 } else
566 if (arg == "%s") {
567 if (work_stack.size() < 1)
568 log_cmd_error("Must have at least one element on the stack for operator %%s.\n");
569 select_op_submod(design, work_stack[work_stack.size()-1]);
570 } else
571 if (arg == "%x" || (arg.size() > 2 && arg.substr(0, 2) == "%x" && (arg[2] == ':' || arg[2] == '*' || arg[2] == '.' || ('0' <= arg[2] && arg[2] <= '9')))) {
572 if (work_stack.size() < 1)
573 log_cmd_error("Must have at least one element on the stack for operator %%x.\n");
574 select_op_expand(design, arg, 'x');
575 } else
576 if (arg == "%ci" || (arg.size() > 3 && arg.substr(0, 3) == "%ci" && (arg[3] == ':' || arg[3] == '*' || arg[3] == '.' || ('0' <= arg[3] && arg[3] <= '9')))) {
577 if (work_stack.size() < 1)
578 log_cmd_error("Must have at least one element on the stack for operator %%ci.\n");
579 select_op_expand(design, arg, 'i');
580 } else
581 if (arg == "%co" || (arg.size() > 3 && arg.substr(0, 3) == "%co" && (arg[3] == ':' || arg[3] == '*' || arg[3] == '.' || ('0' <= arg[3] && arg[3] <= '9')))) {
582 if (work_stack.size() < 1)
583 log_cmd_error("Must have at least one element on the stack for operator %%co.\n");
584 select_op_expand(design, arg, 'o');
585 } else
586 log_cmd_error("Unknown selection operator '%s'.\n", arg.c_str());
587 if (work_stack.size() >= 1)
588 select_filter_active_mod(design, work_stack.back());
589 return;
590 }
591
592 if (arg[0] == '@') {
593 std::string set_name = RTLIL::escape_id(arg.substr(1));
594 if (design->selection_vars.count(set_name) > 0)
595 work_stack.push_back(design->selection_vars[set_name]);
596 else
597 log_cmd_error("Selection @%s is not defined!\n", RTLIL::id2cstr(set_name));
598 select_filter_active_mod(design, work_stack.back());
599 return;
600 }
601
602 if (!design->selected_active_module.empty()) {
603 arg_mod = design->selected_active_module;
604 arg_memb = arg;
605 } else {
606 size_t pos = arg.find('/');
607 if (pos == std::string::npos) {
608 if (arg.find(':') == std::string::npos || arg.substr(0, 1) == "A")
609 arg_mod = arg;
610 else
611 arg_mod = "*", arg_memb = arg;
612 } else {
613 arg_mod = arg.substr(0, pos);
614 arg_memb = arg.substr(pos+1);
615 }
616 }
617
618 work_stack.push_back(RTLIL::Selection());
619 RTLIL::Selection &sel = work_stack.back();
620
621 if (arg == "*" && arg_mod == "*") {
622 select_filter_active_mod(design, work_stack.back());
623 return;
624 }
625
626 sel.full_selection = false;
627 for (auto &mod_it : design->modules)
628 {
629 if (arg_mod.substr(0, 2) == "A:") {
630 if (!match_attr(mod_it.second->attributes, arg_mod.substr(2)))
631 continue;
632 } else
633 if (!match_ids(mod_it.first, arg_mod))
634 continue;
635
636 if (arg_memb == "") {
637 sel.selected_modules.insert(mod_it.first);
638 continue;
639 }
640
641 RTLIL::Module *mod = mod_it.second;
642 if (arg_memb.substr(0, 2) == "w:") {
643 for (auto &it : mod->wires)
644 if (match_ids(it.first, arg_memb.substr(2)))
645 sel.selected_members[mod->name].insert(it.first);
646 } else
647 if (arg_memb.substr(0, 2) == "m:") {
648 for (auto &it : mod->memories)
649 if (match_ids(it.first, arg_memb.substr(2)))
650 sel.selected_members[mod->name].insert(it.first);
651 } else
652 if (arg_memb.substr(0, 2) == "c:") {
653 for (auto &it : mod->cells)
654 if (match_ids(it.first, arg_memb.substr(2)))
655 sel.selected_members[mod->name].insert(it.first);
656 } else
657 if (arg_memb.substr(0, 2) == "t:") {
658 for (auto &it : mod->cells)
659 if (match_ids(it.second->type, arg_memb.substr(2)))
660 sel.selected_members[mod->name].insert(it.first);
661 } else
662 if (arg_memb.substr(0, 2) == "p:") {
663 for (auto &it : mod->processes)
664 if (match_ids(it.first, arg_memb.substr(2)))
665 sel.selected_members[mod->name].insert(it.first);
666 } else
667 if (arg_memb.substr(0, 2) == "a:") {
668 for (auto &it : mod->wires)
669 if (match_attr(it.second->attributes, arg_memb.substr(2)))
670 sel.selected_members[mod->name].insert(it.first);
671 for (auto &it : mod->memories)
672 if (match_attr(it.second->attributes, arg_memb.substr(2)))
673 sel.selected_members[mod->name].insert(it.first);
674 for (auto &it : mod->cells)
675 if (match_attr(it.second->attributes, arg_memb.substr(2)))
676 sel.selected_members[mod->name].insert(it.first);
677 for (auto &it : mod->processes)
678 if (match_attr(it.second->attributes, arg_memb.substr(2)))
679 sel.selected_members[mod->name].insert(it.first);
680 } else
681 if (arg_memb.substr(0, 2) == "r:") {
682 for (auto &it : mod->cells)
683 if (match_attr(it.second->parameters, arg_memb.substr(2)))
684 sel.selected_members[mod->name].insert(it.first);
685 } else {
686 if (arg_memb.substr(0, 2) == "n:")
687 arg_memb = arg_memb.substr(2);
688 for (auto &it : mod->wires)
689 if (match_ids(it.first, arg_memb))
690 sel.selected_members[mod->name].insert(it.first);
691 for (auto &it : mod->memories)
692 if (match_ids(it.first, arg_memb))
693 sel.selected_members[mod->name].insert(it.first);
694 for (auto &it : mod->cells)
695 if (match_ids(it.first, arg_memb))
696 sel.selected_members[mod->name].insert(it.first);
697 for (auto &it : mod->processes)
698 if (match_ids(it.first, arg_memb))
699 sel.selected_members[mod->name].insert(it.first);
700 }
701 }
702
703 select_filter_active_mod(design, work_stack.back());
704 }
705
706 // used in kernel/register.cc and maybe other locations, extern decl. in register.h
707 void handle_extra_select_args(Pass *pass, std::vector<std::string> args, size_t argidx, size_t args_size, RTLIL::Design *design)
708 {
709 work_stack.clear();
710 for (; argidx < args_size; argidx++) {
711 if (args[argidx].substr(0, 1) == "-") {
712 if (pass != NULL)
713 pass->cmd_error(args, argidx, "Unexpected option in selection arguments.");
714 else
715 log_cmd_error("Unexpected option in selection arguments.");
716 }
717 select_stmt(design, args[argidx]);
718 }
719 while (work_stack.size() > 1) {
720 select_op_union(design, work_stack.front(), work_stack.back());
721 work_stack.pop_back();
722 }
723 if (work_stack.size() > 0)
724 design->selection_stack.push_back(work_stack.back());
725 else
726 design->selection_stack.push_back(RTLIL::Selection(false));
727 }
728
729 struct SelectPass : public Pass {
730 SelectPass() : Pass("select", "modify and view the list of selected objects") { }
731 virtual void help()
732 {
733 // |---v---|---v---|---v---|---v---|---v---|---v---|---v---|---v---|---v---|---v---|
734 log("\n");
735 log(" select [ -add | -del | -set <name> ] <selection>\n");
736 log(" select [ -assert-none | -assert-any ] <selection>\n");
737 log(" select [ -list | -write <filename> | -count | -clear ]\n");
738 log(" select -module <modname>\n");
739 log("\n");
740 log("Most commands use the list of currently selected objects to determine which part\n");
741 log("of the design to operate on. This command can be used to modify and view this\n");
742 log("list of selected objects.\n");
743 log("\n");
744 log("Note that many commands support an optional [selection] argument that can be\n");
745 log("used to override the global selection for the command. The syntax of this\n");
746 log("optional argument is identical to the syntax of the <selection> argument\n");
747 log("described here.\n");
748 log("\n");
749 log(" -add, -del\n");
750 log(" add or remove the given objects to the current selection.\n");
751 log(" without this options the current selection is replaced.\n");
752 log("\n");
753 log(" -set <name>\n");
754 log(" do not modify the current selection. instead save the new selection\n");
755 log(" under the given name (see @<name> below). to save the current selection,\n");
756 log(" use \"select -set <name> %%\"\n");
757 log("\n");
758 log(" -assert-none\n");
759 log(" do not modify the current selection. instead assert that the given\n");
760 log(" selection is empty. i.e. produce an error if any object matching the\n");
761 log(" selection is found.\n");
762 log("\n");
763 log(" -assert-any\n");
764 log(" do not modify the current selection. instead assert that the given\n");
765 log(" selection is non-empty. i.e. produce an error if no object matching\n");
766 log(" the selection is found.\n");
767 log("\n");
768 log(" -list\n");
769 log(" list all objects in the current selection\n");
770 log("\n");
771 log(" -write <filename>\n");
772 log(" like -list but write the output to the specified file\n");
773 log("\n");
774 log(" -count\n");
775 log(" count all objects in the current selection\n");
776 log("\n");
777 log(" -clear\n");
778 log(" clear the current selection. this effectively selects the whole\n");
779 log(" design. it also resets the selected module (see -module). use the\n");
780 log(" command 'select *' to select everything but stay in the current module.\n");
781 log("\n");
782 log(" -none\n");
783 log(" create an empty selection. the current module is unchanged.\n");
784 log("\n");
785 log(" -module <modname>\n");
786 log(" limit the current scope to the specified module.\n");
787 log(" the difference between this and simply selecting the module\n");
788 log(" is that all object names are interpreted relative to this\n");
789 log(" module after this command until the selection is cleared again.\n");
790 log("\n");
791 log("When this command is called without an argument, the current selection\n");
792 log("is displayed in a compact form (i.e. only the module name when a whole module\n");
793 log("is selected).\n");
794 log("\n");
795 log("The <selection> argument itself is a series of commands for a simple stack\n");
796 log("machine. Each element on the stack represents a set of selected objects.\n");
797 log("After this commands have been executed, the union of all remaining sets\n");
798 log("on the stack is computed and used as selection for the command.\n");
799 log("\n");
800 log("Pushing (selecting) object when not in -module mode:\n");
801 log("\n");
802 log(" <mod_pattern>\n");
803 log(" select the specified module(s)\n");
804 log("\n");
805 log(" <mod_pattern>/<obj_pattern>\n");
806 log(" select the specified object(s) from the module(s)\n");
807 log("\n");
808 log("Pushing (selecting) object when in -module mode:\n");
809 log("\n");
810 log(" <obj_pattern>\n");
811 log(" select the specified object(s) from the current module\n");
812 log("\n");
813 log("A <mod_pattern> can be a module name, wildcard expression (*, ?, [..])\n");
814 log("matching module names, or one of the following:\n");
815 log("\n");
816 log(" A:<pattern>, A:<pattern>=<pattern>\n");
817 log(" all modules with an attribute matching the given pattern\n");
818 log(" in addition to = also <, <=, >=, and > are supported\n");
819 log("\n");
820 log("An <obj_pattern> can be an object name, wildcard expression, or one of\n");
821 log("the following:\n");
822 log("\n");
823 log(" w:<pattern>\n");
824 log(" all wires with a name matching the given wildcard pattern\n");
825 log("\n");
826 log(" m:<pattern>\n");
827 log(" all memories with a name matching the given pattern\n");
828 log("\n");
829 log(" c:<pattern>\n");
830 log(" all cells with a name matching the given pattern\n");
831 log("\n");
832 log(" t:<pattern>\n");
833 log(" all cells with a type matching the given pattern\n");
834 log("\n");
835 log(" p:<pattern>\n");
836 log(" all processes with a name matching the given pattern\n");
837 log("\n");
838 log(" a:<pattern>\n");
839 log(" all objects with an attribute name matching the given pattern\n");
840 log("\n");
841 log(" a:<pattern>=<pattern>\n");
842 log(" all objects with a matching attribute name-value-pair.\n");
843 log(" in addition to = also <, <=, >=, and > are supported\n");
844 log("\n");
845 log(" r:<pattern>, r:<pattern>=<pattern>\n");
846 log(" cells with matching parameters. also with <, <=, >= and >.\n");
847 log("\n");
848 log(" n:<pattern>\n");
849 log(" all objects with a name matching the given pattern\n");
850 log(" (i.e. 'n:' is optional as it is the default matching rule)\n");
851 log("\n");
852 log(" @<name>\n");
853 log(" push the selection saved prior with 'select -set <name> ...'\n");
854 log("\n");
855 log("The following actions can be performed on the top sets on the stack:\n");
856 log("\n");
857 log(" %%\n");
858 log(" push a copy of the current selection to the stack\n");
859 log("\n");
860 log(" %%%%\n");
861 log(" replace the stack with a union of all elements on it\n");
862 log("\n");
863 log(" %%n\n");
864 log(" replace top set with its invert\n");
865 log("\n");
866 log(" %%u\n");
867 log(" replace the two top sets on the stack with their union\n");
868 log("\n");
869 log(" %%i\n");
870 log(" replace the two top sets on the stack with their intersection\n");
871 log("\n");
872 log(" %%d\n");
873 log(" pop the top set from the stack and subtract it from the new top\n");
874 log("\n");
875 log(" %%x[<num1>|*][.<num2>][:<rule>[:<rule>..]]\n");
876 log(" expand top set <num1> num times according to the specified rules.\n");
877 log(" (i.e. select all cells connected to selected wires and select all\n");
878 log(" wires connected to selected cells) The rules specify which cell\n");
879 log(" ports to use for this. the syntax for a rule is a '-' for exclusion\n");
880 log(" and a '+' for inclusion, followed by an optional comma seperated\n");
881 log(" list of cell types followed by an optional comma separated list of\n");
882 log(" cell ports in square brackets. a rule can also be just a cell or wire\n");
883 log(" name that limits the expansion (is included but does not go beyond).\n");
884 log(" select at most <num2> objects. a warning message is printed when this\n");
885 log(" limit is reached. When '*' is used instead of <num1> then the process\n");
886 log(" is repeated until no further object are selected.\n");
887 log("\n");
888 log(" %%ci[<num1>|*][.<num2>][:<rule>[:<rule>..]]\n");
889 log(" %%co[<num1>|*][.<num2>][:<rule>[:<rule>..]]\n");
890 log(" simmilar to %%x, but only select input (%%ci) or output cones (%%co)\n");
891 log("\n");
892 log(" %%s\n");
893 log(" expand top set by adding all modules of instantiated cells in selected\n");
894 log(" modules\n");
895 log("\n");
896 log("Example: the following command selects all wires that are connected to a\n");
897 log("'GATE' input of a 'SWITCH' cell:\n");
898 log("\n");
899 log(" select */t:SWITCH %%x:+[GATE] */t:SWITCH %%d\n");
900 log("\n");
901 }
902 virtual void execute(std::vector<std::string> args, RTLIL::Design *design)
903 {
904 bool add_mode = false;
905 bool del_mode = false;
906 bool clear_mode = false;
907 bool none_mode = false;
908 bool list_mode = false;
909 bool count_mode = false;
910 bool got_module = false;
911 bool assert_none = false;
912 bool assert_any = false;
913 std::string write_file;
914 std::string set_name;
915 std::string sel_str;
916
917 work_stack.clear();
918
919 size_t argidx;
920 for (argidx = 1; argidx < args.size(); argidx++)
921 {
922 std::string arg = args[argidx];
923 if (arg == "-add") {
924 add_mode = true;
925 continue;
926 }
927 if (arg == "-del") {
928 del_mode = true;
929 continue;
930 }
931 if (arg == "-assert-none") {
932 assert_none = true;
933 continue;
934 }
935 if (arg == "-assert-any") {
936 assert_any = true;
937 continue;
938 }
939 if (arg == "-clear") {
940 clear_mode = true;
941 continue;
942 }
943 if (arg == "-none") {
944 none_mode = true;
945 continue;
946 }
947 if (arg == "-list") {
948 list_mode = true;
949 continue;
950 }
951 if (arg == "-write" && argidx+1 < args.size()) {
952 write_file = args[++argidx];
953 continue;
954 }
955 if (arg == "-count") {
956 count_mode = true;
957 continue;
958 }
959 if (arg == "-module" && argidx+1 < args.size()) {
960 RTLIL::IdString mod_name = RTLIL::escape_id(args[++argidx]);
961 if (design->modules.count(mod_name) == 0)
962 log_cmd_error("No such module: %s\n", id2cstr(mod_name));
963 design->selected_active_module = mod_name;
964 got_module = true;
965 continue;
966 }
967 if (arg == "-set" && argidx+1 < args.size()) {
968 set_name = RTLIL::escape_id(args[++argidx]);
969 continue;
970 }
971 if (arg.size() > 0 && arg[0] == '-')
972 log_cmd_error("Unkown option %s.\n", arg.c_str());
973 select_stmt(design, arg);
974 sel_str += " " + arg;
975 }
976
977 if (clear_mode && args.size() != 2)
978 log_cmd_error("Option -clear can not be combined with any other options.\n");
979
980 if (none_mode && args.size() != 2)
981 log_cmd_error("Option -none can not be combined with any other options.\n");
982
983 if (add_mode + del_mode + assert_none + assert_any > 1)
984 log_cmd_error("Options -add, -del, -assert-none or -assert-any can not be combined.\n");
985
986 if ((list_mode || !write_file.empty() || count_mode) && (add_mode || del_mode || assert_none || assert_any))
987 log_cmd_error("Options -list, -write and -count can not be combined with -add, -del, -assert-none or -assert-any.\n");
988
989 if (!set_name.empty() && (list_mode || !write_file.empty() || count_mode || add_mode || del_mode || assert_none || assert_any))
990 log_cmd_error("Option -set can not be combined with -list, -write, -count, -add, -del, -assert-none or -assert-any.\n");
991
992 if (work_stack.size() == 0 && got_module) {
993 RTLIL::Selection sel;
994 select_filter_active_mod(design, sel);
995 work_stack.push_back(sel);
996 }
997
998 while (work_stack.size() > 1) {
999 select_op_union(design, work_stack.front(), work_stack.back());
1000 work_stack.pop_back();
1001 }
1002
1003 assert(design->selection_stack.size() > 0);
1004
1005 if (clear_mode) {
1006 design->selection_stack.back() = RTLIL::Selection(true);
1007 design->selected_active_module = std::string();
1008 return;
1009 }
1010
1011 if (none_mode) {
1012 design->selection_stack.back() = RTLIL::Selection(false);
1013 return;
1014 }
1015
1016 if (list_mode || count_mode || !write_file.empty())
1017 {
1018 #define LOG_OBJECT(...) do { if (list_mode) log(__VA_ARGS__); if (f != NULL) fprintf(f, __VA_ARGS__); total_count++; } while (0)
1019 int total_count = 0;
1020 FILE *f = NULL;
1021 if (!write_file.empty()) {
1022 f = fopen(write_file.c_str(), "w");
1023 if (f == NULL)
1024 log_error("Can't open '%s' for writing: %s\n", write_file.c_str(), strerror(errno));
1025 }
1026 RTLIL::Selection *sel = &design->selection_stack.back();
1027 if (work_stack.size() > 0)
1028 sel = &work_stack.back();
1029 sel->optimize(design);
1030 for (auto mod_it : design->modules)
1031 {
1032 if (sel->selected_whole_module(mod_it.first) && list_mode)
1033 log("%s\n", id2cstr(mod_it.first));
1034 if (sel->selected_module(mod_it.first)) {
1035 for (auto &it : mod_it.second->wires)
1036 if (sel->selected_member(mod_it.first, it.first))
1037 LOG_OBJECT("%s/%s\n", id2cstr(mod_it.first), id2cstr(it.first));
1038 for (auto &it : mod_it.second->memories)
1039 if (sel->selected_member(mod_it.first, it.first))
1040 LOG_OBJECT("%s/%s\n", id2cstr(mod_it.first), id2cstr(it.first));
1041 for (auto &it : mod_it.second->cells)
1042 if (sel->selected_member(mod_it.first, it.first))
1043 LOG_OBJECT("%s/%s\n", id2cstr(mod_it.first), id2cstr(it.first));
1044 for (auto &it : mod_it.second->processes)
1045 if (sel->selected_member(mod_it.first, it.first))
1046 LOG_OBJECT("%s/%s\n", id2cstr(mod_it.first), id2cstr(it.first));
1047 }
1048 }
1049 if (count_mode)
1050 log("%d objects.\n", total_count);
1051 if (f != NULL)
1052 fclose(f);
1053 #undef LOG_OBJECT
1054 return;
1055 }
1056
1057 if (add_mode)
1058 {
1059 if (work_stack.size() == 0)
1060 log_cmd_error("Nothing to add to selection.\n");
1061 select_op_union(design, design->selection_stack.back(), work_stack.back());
1062 design->selection_stack.back().optimize(design);
1063 return;
1064 }
1065
1066 if (del_mode)
1067 {
1068 if (work_stack.size() == 0)
1069 log_cmd_error("Nothing to delete from selection.\n");
1070 select_op_diff(design, design->selection_stack.back(), work_stack.back());
1071 design->selection_stack.back().optimize(design);
1072 return;
1073 }
1074
1075 if (assert_none)
1076 {
1077 if (work_stack.size() == 0)
1078 log_cmd_error("No selection to check.\n");
1079 if (!work_stack.back().empty())
1080 log_error("Assertation failed: selection is not empty:%s\n", sel_str.c_str());
1081 return;
1082 }
1083
1084 if (assert_any)
1085 {
1086 if (work_stack.size() == 0)
1087 log_cmd_error("No selection to check.\n");
1088 if (work_stack.back().empty())
1089 log_error("Assertation failed: selection is empty:%s\n", sel_str.c_str());
1090 return;
1091 }
1092
1093 if (!set_name.empty())
1094 {
1095 if (work_stack.size() == 0)
1096 design->selection_vars[set_name] = RTLIL::Selection(false);
1097 else
1098 design->selection_vars[set_name] = work_stack.back();
1099 return;
1100 }
1101
1102 if (work_stack.size() == 0) {
1103 RTLIL::Selection &sel = design->selection_stack.back();
1104 if (sel.full_selection)
1105 log("*\n");
1106 for (auto &it : sel.selected_modules)
1107 log("%s\n", id2cstr(it));
1108 for (auto &it : sel.selected_members)
1109 for (auto &it2 : it.second)
1110 log("%s/%s\n", id2cstr(it.first), id2cstr(it2));
1111 return;
1112 }
1113
1114 design->selection_stack.back() = work_stack.back();
1115 design->selection_stack.back().optimize(design);
1116 }
1117 } SelectPass;
1118
1119 struct CdPass : public Pass {
1120 CdPass() : Pass("cd", "a shortcut for 'select -module <name>'") { }
1121 virtual void help()
1122 {
1123 // |---v---|---v---|---v---|---v---|---v---|---v---|---v---|---v---|---v---|---v---|
1124 log("\n");
1125 log(" cd <modname>\n");
1126 log("\n");
1127 log("This is just a shortcut for 'select -module <modname>'.\n");
1128 log("\n");
1129 log("\n");
1130 log(" cd <cellname>\n");
1131 log("\n");
1132 log("When no module with the specified name is found, but there is a cell\n");
1133 log("with the specified name in the current module, then this is equivialent\n");
1134 log("to 'cd <celltype>'.\n");
1135 log("\n");
1136 log(" cd ..\n");
1137 log("\n");
1138 log("This is just a shortcut for 'select -clear'.\n");
1139 log("\n");
1140 }
1141 virtual void execute(std::vector<std::string> args, RTLIL::Design *design)
1142 {
1143 if (args.size() != 2)
1144 log_cmd_error("Invalid number of arguments.\n");
1145
1146 if (args[1] == "..") {
1147 design->selection_stack.back() = RTLIL::Selection(true);
1148 design->selected_active_module = std::string();
1149 return;
1150 }
1151
1152 std::string modname = RTLIL::escape_id(args[1]);
1153
1154 if (design->modules.count(modname) == 0 && !design->selected_active_module.empty()) {
1155 RTLIL::Module *module = NULL;
1156 if (design->modules.count(design->selected_active_module) > 0)
1157 module = design->modules.at(design->selected_active_module);
1158 if (module != NULL && module->cells.count(modname) > 0)
1159 modname = module->cells.at(modname)->type;
1160 }
1161
1162 if (design->modules.count(modname) > 0) {
1163 design->selected_active_module = modname;
1164 design->selection_stack.back() = RTLIL::Selection();
1165 select_filter_active_mod(design, design->selection_stack.back());
1166 design->selection_stack.back().optimize(design);
1167 return;
1168 }
1169
1170 log_cmd_error("No such module `%s' found!\n", RTLIL::id2cstr(modname));
1171 }
1172 } CdPass;
1173
1174 template<typename T>
1175 static int log_matches(const char *title, std::string pattern, T list)
1176 {
1177 std::vector<std::string> matches;
1178
1179 for (auto &it : list)
1180 if (pattern.empty() || match_ids(it.first, pattern))
1181 matches.push_back(it.first);
1182
1183 if (matches.empty())
1184 return 0;
1185
1186 log("\n%d %s:\n", int(matches.size()), title);
1187 for (auto &id : matches)
1188 log(" %s\n", RTLIL::id2cstr(id));
1189 return matches.size();
1190 }
1191
1192 struct LsPass : public Pass {
1193 LsPass() : Pass("ls", "list modules or objects in modules") { }
1194 virtual void help()
1195 {
1196 // |---v---|---v---|---v---|---v---|---v---|---v---|---v---|---v---|---v---|---v---|
1197 log("\n");
1198 log(" ls [pattern]\n");
1199 log("\n");
1200 log("When no active module is selected, this prints a list of all modules.\n");
1201 log("\n");
1202 log("When an active module is selected, this prints a list of objects in the module.\n");
1203 log("\n");
1204 log("If a pattern is given, the objects matching the pattern are printed\n");
1205 log("\n");
1206 log("Note that this command does not use the selection mechanism and always operates\n");
1207 log("on the whole design or whole active module. Use 'select -list' to show a list\n");
1208 log("of currently selected objects.\n");
1209 log("\n");
1210 }
1211 virtual void execute(std::vector<std::string> args, RTLIL::Design *design)
1212 {
1213 std::string pattern;
1214 int counter = 0;
1215
1216 if (args.size() != 1 && args.size() != 2)
1217 log_cmd_error("Invalid number of arguments.\n");
1218 if (args.size() == 2)
1219 pattern = args.at(1);
1220
1221 if (design->selected_active_module.empty())
1222 {
1223 counter += log_matches("modules", pattern, design->modules);
1224 }
1225 else
1226 if (design->modules.count(design->selected_active_module) > 0)
1227 {
1228 RTLIL::Module *module = design->modules.at(design->selected_active_module);
1229 counter += log_matches("wires", pattern, module->wires);
1230 counter += log_matches("memories", pattern, module->memories);
1231 counter += log_matches("cells", pattern, module->cells);
1232 counter += log_matches("processes", pattern, module->processes);
1233 }
1234
1235 // log("\nfound %d item%s.\n", counter, counter == 1 ? "" : "s");
1236 }
1237 } LsPass;
1238