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