Merge pull request #1347 from mmicko/fix_select_error_msg
[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/yosys.h"
21 #include "kernel/celltypes.h"
22 #include "kernel/sigtools.h"
23 #include <string.h>
24 #include <errno.h>
25
26 USING_YOSYS_NAMESPACE
27 PRIVATE_NAMESPACE_BEGIN
28
29 using RTLIL::id2cstr;
30
31 static std::vector<RTLIL::Selection> work_stack;
32
33 static bool match_ids(RTLIL::IdString id, std::string pattern)
34 {
35 if (id == pattern)
36 return true;
37 if (id.size() > 0 && id[0] == '\\' && id.compare(1, std::string::npos, pattern.c_str()) == 0)
38 return true;
39 if (patmatch(pattern.c_str(), id.c_str()))
40 return true;
41 if (id.size() > 0 && id[0] == '\\' && patmatch(pattern.c_str(), id.substr(1).c_str()))
42 return true;
43 if (id.size() > 0 && id[0] == '$' && pattern.size() > 0 && pattern[0] == '$') {
44 const char *p = id.c_str();
45 const char *q = strrchr(p, '$');
46 if (pattern == q)
47 return true;
48 }
49 return false;
50 }
51
52 static bool match_attr_val(const RTLIL::Const &value, std::string pattern, char match_op)
53 {
54 if (match_op == 0)
55 return true;
56
57 if ((value.flags & RTLIL::CONST_FLAG_STRING) == 0)
58 {
59 RTLIL::SigSpec sig_value;
60
61 if (!RTLIL::SigSpec::parse(sig_value, NULL, pattern))
62 return false;
63
64 RTLIL::Const pattern_value = sig_value.as_const();
65
66 if (match_op == '=')
67 return value == pattern_value;
68 if (match_op == '!')
69 return value != pattern_value;
70 if (match_op == '<')
71 return value.as_int() < pattern_value.as_int();
72 if (match_op == '>')
73 return value.as_int() > pattern_value.as_int();
74 if (match_op == '[')
75 return value.as_int() <= pattern_value.as_int();
76 if (match_op == ']')
77 return value.as_int() >= pattern_value.as_int();
78 }
79 else
80 {
81 std::string value_str = value.decode_string();
82
83 if (match_op == '=')
84 if (patmatch(pattern.c_str(), value.decode_string().c_str()))
85 return true;
86
87 if (match_op == '=')
88 return value_str == pattern;
89 if (match_op == '!')
90 return value_str != pattern;
91 if (match_op == '<')
92 return value_str < pattern;
93 if (match_op == '>')
94 return value_str > pattern;
95 if (match_op == '[')
96 return value_str <= pattern;
97 if (match_op == ']')
98 return value_str >= pattern;
99 }
100
101 log_abort();
102 }
103
104 static bool match_attr(const dict<RTLIL::IdString, RTLIL::Const> &attributes, std::string name_pat, std::string value_pat, char match_op)
105 {
106 if (name_pat.find('*') != std::string::npos || name_pat.find('?') != std::string::npos || name_pat.find('[') != std::string::npos) {
107 for (auto &it : attributes) {
108 if (patmatch(name_pat.c_str(), it.first.c_str()) && match_attr_val(it.second, value_pat, match_op))
109 return true;
110 if (it.first.size() > 0 && it.first[0] == '\\' && patmatch(name_pat.c_str(), it.first.substr(1).c_str()) && match_attr_val(it.second, value_pat, match_op))
111 return true;
112 }
113 } else {
114 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))
115 return true;
116 if (attributes.count("\\" + name_pat) && match_attr_val(attributes.at("\\" + name_pat), value_pat, match_op))
117 return true;
118 }
119 return false;
120 }
121
122 static bool match_attr(const dict<RTLIL::IdString, RTLIL::Const> &attributes, std::string match_expr)
123 {
124 size_t pos = match_expr.find_first_of("<!=>");
125
126 if (pos != std::string::npos) {
127 if (match_expr.compare(pos, 2, "!=") == 0)
128 return match_attr(attributes, match_expr.substr(0, pos), match_expr.substr(pos+2), '!');
129 if (match_expr.compare(pos, 2, "<=") == 0)
130 return match_attr(attributes, match_expr.substr(0, pos), match_expr.substr(pos+2), '[');
131 if (match_expr.compare(pos, 2, ">=") == 0)
132 return match_attr(attributes, match_expr.substr(0, pos), match_expr.substr(pos+2), ']');
133 return match_attr(attributes, match_expr.substr(0, pos), match_expr.substr(pos+1), match_expr[pos]);
134 }
135
136 return match_attr(attributes, match_expr, std::string(), 0);
137 }
138
139 static void select_op_neg(RTLIL::Design *design, RTLIL::Selection &lhs)
140 {
141 if (lhs.full_selection) {
142 lhs.full_selection = false;
143 lhs.selected_modules.clear();
144 lhs.selected_members.clear();
145 return;
146 }
147
148 if (lhs.selected_modules.size() == 0 && lhs.selected_members.size() == 0) {
149 lhs.full_selection = true;
150 return;
151 }
152
153 RTLIL::Selection new_sel(false);
154
155 for (auto &mod_it : design->modules_)
156 {
157 if (lhs.selected_whole_module(mod_it.first))
158 continue;
159 if (!lhs.selected_module(mod_it.first)) {
160 new_sel.selected_modules.insert(mod_it.first);
161 continue;
162 }
163
164 RTLIL::Module *mod = mod_it.second;
165 for (auto &it : mod->wires_)
166 if (!lhs.selected_member(mod_it.first, it.first))
167 new_sel.selected_members[mod->name].insert(it.first);
168 for (auto &it : mod->memories)
169 if (!lhs.selected_member(mod_it.first, it.first))
170 new_sel.selected_members[mod->name].insert(it.first);
171 for (auto &it : mod->cells_)
172 if (!lhs.selected_member(mod_it.first, it.first))
173 new_sel.selected_members[mod->name].insert(it.first);
174 for (auto &it : mod->processes)
175 if (!lhs.selected_member(mod_it.first, it.first))
176 new_sel.selected_members[mod->name].insert(it.first);
177 }
178
179 lhs.selected_modules.swap(new_sel.selected_modules);
180 lhs.selected_members.swap(new_sel.selected_members);
181 }
182
183 static int my_xorshift32_rng() {
184 static uint32_t x32 = 314159265;
185 x32 ^= x32 << 13;
186 x32 ^= x32 >> 17;
187 x32 ^= x32 << 5;
188 return x32 & 0x0fffffff;
189 }
190
191 static void select_op_random(RTLIL::Design *design, RTLIL::Selection &lhs, int count)
192 {
193 vector<pair<IdString, IdString>> objects;
194
195 for (auto mod : design->modules())
196 {
197 if (!lhs.selected_module(mod->name))
198 continue;
199
200 for (auto cell : mod->cells()) {
201 if (lhs.selected_member(mod->name, cell->name))
202 objects.push_back(make_pair(mod->name, cell->name));
203 }
204
205 for (auto wire : mod->wires()) {
206 if (lhs.selected_member(mod->name, wire->name))
207 objects.push_back(make_pair(mod->name, wire->name));
208 }
209 }
210
211 lhs = RTLIL::Selection(false);
212
213 while (!objects.empty() && count-- > 0)
214 {
215 int idx = my_xorshift32_rng() % GetSize(objects);
216 lhs.selected_members[objects[idx].first].insert(objects[idx].second);
217 objects[idx] = objects.back();
218 objects.pop_back();
219 }
220
221 lhs.optimize(design);
222 }
223
224 static void select_op_submod(RTLIL::Design *design, RTLIL::Selection &lhs)
225 {
226 for (auto &mod_it : design->modules_)
227 {
228 if (lhs.selected_whole_module(mod_it.first))
229 {
230 for (auto &cell_it : mod_it.second->cells_)
231 {
232 if (design->modules_.count(cell_it.second->type) == 0)
233 continue;
234 lhs.selected_modules.insert(cell_it.second->type);
235 }
236 }
237 }
238 }
239
240 static void select_op_cells_to_modules(RTLIL::Design *design, RTLIL::Selection &lhs)
241 {
242 RTLIL::Selection new_sel(false);
243 for (auto &mod_it : design->modules_)
244 if (lhs.selected_module(mod_it.first))
245 for (auto &cell_it : mod_it.second->cells_)
246 if (lhs.selected_member(mod_it.first, cell_it.first) && design->modules_.count(cell_it.second->type))
247 new_sel.selected_modules.insert(cell_it.second->type);
248 lhs = new_sel;
249 }
250
251 static void select_op_module_to_cells(RTLIL::Design *design, RTLIL::Selection &lhs)
252 {
253 RTLIL::Selection new_sel(false);
254 for (auto &mod_it : design->modules_)
255 for (auto &cell_it : mod_it.second->cells_)
256 if (design->modules_.count(cell_it.second->type) && lhs.selected_whole_module(cell_it.second->type))
257 new_sel.selected_members[mod_it.first].insert(cell_it.first);
258 lhs = new_sel;
259 }
260
261 static void select_op_fullmod(RTLIL::Design *design, RTLIL::Selection &lhs)
262 {
263 lhs.optimize(design);
264 for (auto &it : lhs.selected_members)
265 lhs.selected_modules.insert(it.first);
266 lhs.selected_members.clear();
267 }
268
269 static void select_op_alias(RTLIL::Design *design, RTLIL::Selection &lhs)
270 {
271 for (auto &mod_it : design->modules_)
272 {
273 if (lhs.selected_whole_module(mod_it.first))
274 continue;
275 if (!lhs.selected_module(mod_it.first))
276 continue;
277
278 SigMap sigmap(mod_it.second);
279 SigPool selected_bits;
280
281 for (auto &it : mod_it.second->wires_)
282 if (lhs.selected_member(mod_it.first, it.first))
283 selected_bits.add(sigmap(it.second));
284
285 for (auto &it : mod_it.second->wires_)
286 if (!lhs.selected_member(mod_it.first, it.first) && selected_bits.check_any(sigmap(it.second)))
287 lhs.selected_members[mod_it.first].insert(it.first);
288 }
289 }
290
291 static void select_op_union(RTLIL::Design*, RTLIL::Selection &lhs, const RTLIL::Selection &rhs)
292 {
293 if (rhs.full_selection) {
294 lhs.full_selection = true;
295 lhs.selected_modules.clear();
296 lhs.selected_members.clear();
297 return;
298 }
299
300 if (lhs.full_selection)
301 return;
302
303 for (auto &it : rhs.selected_members)
304 for (auto &it2 : it.second)
305 lhs.selected_members[it.first].insert(it2);
306
307 for (auto &it : rhs.selected_modules) {
308 lhs.selected_modules.insert(it);
309 lhs.selected_members.erase(it);
310 }
311 }
312
313 static void select_op_diff(RTLIL::Design *design, RTLIL::Selection &lhs, const RTLIL::Selection &rhs)
314 {
315 if (rhs.full_selection) {
316 lhs.full_selection = false;
317 lhs.selected_modules.clear();
318 lhs.selected_members.clear();
319 return;
320 }
321
322 if (lhs.full_selection) {
323 if (!rhs.full_selection && rhs.selected_modules.size() == 0 && rhs.selected_members.size() == 0)
324 return;
325 lhs.full_selection = false;
326 for (auto &it : design->modules_)
327 lhs.selected_modules.insert(it.first);
328 }
329
330 for (auto &it : rhs.selected_modules) {
331 lhs.selected_modules.erase(it);
332 lhs.selected_members.erase(it);
333 }
334
335 for (auto &it : rhs.selected_members)
336 {
337 if (design->modules_.count(it.first) == 0)
338 continue;
339
340 RTLIL::Module *mod = design->modules_[it.first];
341
342 if (lhs.selected_modules.count(mod->name) > 0)
343 {
344 for (auto &it : mod->wires_)
345 lhs.selected_members[mod->name].insert(it.first);
346 for (auto &it : mod->memories)
347 lhs.selected_members[mod->name].insert(it.first);
348 for (auto &it : mod->cells_)
349 lhs.selected_members[mod->name].insert(it.first);
350 for (auto &it : mod->processes)
351 lhs.selected_members[mod->name].insert(it.first);
352 lhs.selected_modules.erase(mod->name);
353 }
354
355 if (lhs.selected_members.count(mod->name) == 0)
356 continue;
357
358 for (auto &it2 : it.second)
359 lhs.selected_members[mod->name].erase(it2);
360 }
361 }
362
363 static void select_op_intersect(RTLIL::Design *design, RTLIL::Selection &lhs, const RTLIL::Selection &rhs)
364 {
365 if (rhs.full_selection)
366 return;
367
368 if (lhs.full_selection) {
369 lhs.full_selection = false;
370 for (auto &it : design->modules_)
371 lhs.selected_modules.insert(it.first);
372 }
373
374 std::vector<RTLIL::IdString> del_list;
375
376 for (auto &it : lhs.selected_modules)
377 if (rhs.selected_modules.count(it) == 0) {
378 if (rhs.selected_members.count(it) > 0)
379 for (auto &it2 : rhs.selected_members.at(it))
380 lhs.selected_members[it].insert(it2);
381 del_list.push_back(it);
382 }
383 for (auto &it : del_list)
384 lhs.selected_modules.erase(it);
385
386 del_list.clear();
387 for (auto &it : lhs.selected_members) {
388 if (rhs.selected_modules.count(it.first) > 0)
389 continue;
390 if (rhs.selected_members.count(it.first) == 0) {
391 del_list.push_back(it.first);
392 continue;
393 }
394 std::vector<RTLIL::IdString> del_list2;
395 for (auto &it2 : it.second)
396 if (rhs.selected_members.at(it.first).count(it2) == 0)
397 del_list2.push_back(it2);
398 for (auto &it2 : del_list2)
399 it.second.erase(it2);
400 if (it.second.size() == 0)
401 del_list.push_back(it.first);
402 }
403 for (auto &it : del_list)
404 lhs.selected_members.erase(it);
405 }
406
407 namespace {
408 struct expand_rule_t {
409 char mode;
410 std::set<RTLIL::IdString> cell_types, port_names;
411 };
412 }
413
414 static int parse_comma_list(std::set<RTLIL::IdString> &tokens, std::string str, size_t pos, std::string stopchar)
415 {
416 stopchar += ',';
417 while (1) {
418 size_t endpos = str.find_first_of(stopchar, pos);
419 if (endpos == std::string::npos)
420 endpos = str.size();
421 if (endpos != pos)
422 tokens.insert(RTLIL::escape_id(str.substr(pos, endpos-pos)));
423 pos = endpos;
424 if (pos == str.size() || str[pos] != ',')
425 return pos;
426 pos++;
427 }
428 }
429
430 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, bool eval_only)
431 {
432 int sel_objects = 0;
433 bool is_input, is_output;
434 for (auto &mod_it : design->modules_)
435 {
436 if (lhs.selected_whole_module(mod_it.first) || !lhs.selected_module(mod_it.first))
437 continue;
438
439 RTLIL::Module *mod = mod_it.second;
440 std::set<RTLIL::Wire*> selected_wires;
441 auto selected_members = lhs.selected_members[mod->name];
442
443 for (auto &it : mod->wires_)
444 if (lhs.selected_member(mod_it.first, it.first) && limits.count(it.first) == 0)
445 selected_wires.insert(it.second);
446
447 for (auto &conn : mod->connections())
448 {
449 std::vector<RTLIL::SigBit> conn_lhs = conn.first.to_sigbit_vector();
450 std::vector<RTLIL::SigBit> conn_rhs = conn.second.to_sigbit_vector();
451
452 for (size_t i = 0; i < conn_lhs.size(); i++) {
453 if (conn_lhs[i].wire == NULL || conn_rhs[i].wire == NULL)
454 continue;
455 if (mode != 'i' && selected_wires.count(conn_rhs[i].wire) && selected_members.count(conn_lhs[i].wire->name) == 0)
456 lhs.selected_members[mod->name].insert(conn_lhs[i].wire->name), sel_objects++, max_objects--;
457 if (mode != 'o' && selected_wires.count(conn_lhs[i].wire) && selected_members.count(conn_rhs[i].wire->name) == 0)
458 lhs.selected_members[mod->name].insert(conn_rhs[i].wire->name), sel_objects++, max_objects--;
459 }
460 }
461
462 for (auto &cell : mod->cells_)
463 for (auto &conn : cell.second->connections())
464 {
465 char last_mode = '-';
466 if (eval_only && !yosys_celltypes.cell_evaluable(cell.second->type))
467 goto exclude_match;
468 for (auto &rule : rules) {
469 last_mode = rule.mode;
470 if (rule.cell_types.size() > 0 && rule.cell_types.count(cell.second->type) == 0)
471 continue;
472 if (rule.port_names.size() > 0 && rule.port_names.count(conn.first) == 0)
473 continue;
474 if (rule.mode == '+')
475 goto include_match;
476 else
477 goto exclude_match;
478 }
479 if (last_mode == '+')
480 goto exclude_match;
481 include_match:
482 is_input = mode == 'x' || ct.cell_input(cell.second->type, conn.first);
483 is_output = mode == 'x' || ct.cell_output(cell.second->type, conn.first);
484 for (auto &chunk : conn.second.chunks())
485 if (chunk.wire != NULL) {
486 if (max_objects != 0 && selected_wires.count(chunk.wire) > 0 && selected_members.count(cell.first) == 0)
487 if (mode == 'x' || (mode == 'i' && is_output) || (mode == 'o' && is_input))
488 lhs.selected_members[mod->name].insert(cell.first), sel_objects++, max_objects--;
489 if (max_objects != 0 && selected_members.count(cell.first) > 0 && limits.count(cell.first) == 0 && selected_members.count(chunk.wire->name) == 0)
490 if (mode == 'x' || (mode == 'i' && is_input) || (mode == 'o' && is_output))
491 lhs.selected_members[mod->name].insert(chunk.wire->name), sel_objects++, max_objects--;
492 }
493 exclude_match:;
494 }
495 }
496
497 return sel_objects;
498 }
499
500 static void select_op_expand(RTLIL::Design *design, std::string arg, char mode, bool eval_only)
501 {
502 int pos = (mode == 'x' ? 2 : 3) + (eval_only ? 1 : 0);
503 int levels = 1, rem_objects = -1;
504 std::vector<expand_rule_t> rules;
505 std::set<RTLIL::IdString> limits;
506
507 CellTypes ct;
508
509 if (mode != 'x')
510 ct.setup(design);
511
512 if (pos < int(arg.size()) && arg[pos] == '*') {
513 levels = 1000000;
514 pos++;
515 } else
516 if (pos < int(arg.size()) && '0' <= arg[pos] && arg[pos] <= '9') {
517 size_t endpos = arg.find_first_not_of("0123456789", pos);
518 if (endpos == std::string::npos)
519 endpos = arg.size();
520 levels = atoi(arg.substr(pos, endpos-pos).c_str());
521 pos = endpos;
522 }
523
524 if (pos < int(arg.size()) && arg[pos] == '.') {
525 size_t endpos = arg.find_first_not_of("0123456789", ++pos);
526 if (endpos == std::string::npos)
527 endpos = arg.size();
528 if (int(endpos) > pos)
529 rem_objects = atoi(arg.substr(pos, endpos-pos).c_str());
530 pos = endpos;
531 }
532
533 while (pos < int(arg.size())) {
534 if (arg[pos] != ':' || pos+1 == int(arg.size()))
535 log_cmd_error("Syntax error in expand operator '%s'.\n", arg.c_str());
536 pos++;
537 if (arg[pos] == '+' || arg[pos] == '-') {
538 expand_rule_t rule;
539 rule.mode = arg[pos++];
540 pos = parse_comma_list(rule.cell_types, arg, pos, "[:");
541 if (pos < int(arg.size()) && arg[pos] == '[') {
542 pos = parse_comma_list(rule.port_names, arg, pos+1, "]:");
543 if (pos < int(arg.size()) && arg[pos] == ']')
544 pos++;
545 }
546 rules.push_back(rule);
547 } else {
548 size_t endpos = arg.find(':', pos);
549 if (endpos == std::string::npos)
550 endpos = arg.size();
551 if (int(endpos) > pos) {
552 std::string str = arg.substr(pos, endpos-pos);
553 if (str[0] == '@') {
554 str = RTLIL::escape_id(str.substr(1));
555 if (design->selection_vars.count(str) > 0) {
556 for (auto i1 : design->selection_vars.at(str).selected_members)
557 for (auto i2 : i1.second)
558 limits.insert(i2);
559 } else
560 log_cmd_error("Selection %s is not defined!\n", RTLIL::unescape_id(str).c_str());
561 } else
562 limits.insert(RTLIL::escape_id(str));
563 }
564 pos = endpos;
565 }
566 }
567
568 #if 0
569 log("expand by %d levels (max. %d objects):\n", levels, rem_objects);
570 for (auto &rule : rules) {
571 log(" rule (%c):\n", rule.mode);
572 if (rule.cell_types.size() > 0) {
573 log(" cell types:");
574 for (auto &it : rule.cell_types)
575 log(" %s", it.c_str());
576 log("\n");
577 }
578 if (rule.port_names.size() > 0) {
579 log(" port names:");
580 for (auto &it : rule.port_names)
581 log(" %s", it.c_str());
582 log("\n");
583 }
584 }
585 if (limits.size() > 0) {
586 log(" limits:");
587 for (auto &it : limits)
588 log(" %s", it.c_str());
589 log("\n");
590 }
591 #endif
592
593 while (levels-- > 0 && rem_objects != 0) {
594 int num_objects = select_op_expand(design, work_stack.back(), rules, limits, rem_objects, mode, ct, eval_only);
595 if (num_objects == 0)
596 break;
597 rem_objects -= num_objects;
598 }
599
600 if (rem_objects == 0)
601 log_warning("reached configured limit at `%s'.\n", arg.c_str());
602 }
603
604 static void select_filter_active_mod(RTLIL::Design *design, RTLIL::Selection &sel)
605 {
606 if (design->selected_active_module.empty())
607 return;
608
609 if (sel.full_selection) {
610 sel.full_selection = false;
611 sel.selected_modules.clear();
612 sel.selected_members.clear();
613 sel.selected_modules.insert(design->selected_active_module);
614 return;
615 }
616
617 std::vector<RTLIL::IdString> del_list;
618 for (auto mod_name : sel.selected_modules)
619 if (mod_name != design->selected_active_module)
620 del_list.push_back(mod_name);
621 for (auto &it : sel.selected_members)
622 if (it.first != design->selected_active_module)
623 del_list.push_back(it.first);
624 for (auto mod_name : del_list) {
625 sel.selected_modules.erase(mod_name);
626 sel.selected_members.erase(mod_name);
627 }
628 }
629
630 static void select_stmt(RTLIL::Design *design, std::string arg)
631 {
632 std::string arg_mod, arg_memb;
633
634 if (arg.size() == 0)
635 return;
636
637 if (arg[0] == '%') {
638 if (arg == "%") {
639 if (design->selection_stack.size() > 0)
640 work_stack.push_back(design->selection_stack.back());
641 } else
642 if (arg == "%%") {
643 while (work_stack.size() > 1) {
644 select_op_union(design, work_stack.front(), work_stack.back());
645 work_stack.pop_back();
646 }
647 } else
648 if (arg == "%n") {
649 if (work_stack.size() < 1)
650 log_cmd_error("Must have at least one element on the stack for operator %%n.\n");
651 select_op_neg(design, work_stack[work_stack.size()-1]);
652 } else
653 if (arg == "%u") {
654 if (work_stack.size() < 2)
655 log_cmd_error("Must have at least two elements on the stack for operator %%u.\n");
656 select_op_union(design, work_stack[work_stack.size()-2], work_stack[work_stack.size()-1]);
657 work_stack.pop_back();
658 } else
659 if (arg == "%d") {
660 if (work_stack.size() < 2)
661 log_cmd_error("Must have at least two elements on the stack for operator %%d.\n");
662 select_op_diff(design, work_stack[work_stack.size()-2], work_stack[work_stack.size()-1]);
663 work_stack.pop_back();
664 } else
665 if (arg == "%D") {
666 if (work_stack.size() < 2)
667 log_cmd_error("Must have at least two elements on the stack for operator %%D.\n");
668 select_op_diff(design, work_stack[work_stack.size()-1], work_stack[work_stack.size()-2]);
669 work_stack[work_stack.size()-2] = work_stack[work_stack.size()-1];
670 work_stack.pop_back();
671 } else
672 if (arg == "%i") {
673 if (work_stack.size() < 2)
674 log_cmd_error("Must have at least two elements on the stack for operator %%i.\n");
675 select_op_intersect(design, work_stack[work_stack.size()-2], work_stack[work_stack.size()-1]);
676 work_stack.pop_back();
677 } else
678 if (arg.size() >= 2 && arg[0] == '%' && arg[1] == 'R') {
679 if (work_stack.size() < 1)
680 log_cmd_error("Must have at least one element on the stack for operator %%R.\n");
681 int count = arg.size() > 2 ? atoi(arg.c_str() + 2) : 1;
682 select_op_random(design, work_stack[work_stack.size()-1], count);
683 } else
684 if (arg == "%s") {
685 if (work_stack.size() < 1)
686 log_cmd_error("Must have at least one element on the stack for operator %%s.\n");
687 select_op_submod(design, work_stack[work_stack.size()-1]);
688 } else
689 if (arg == "%M") {
690 if (work_stack.size() < 1)
691 log_cmd_error("Must have at least one element on the stack for operator %%M.\n");
692 select_op_cells_to_modules(design, work_stack[work_stack.size()-1]);
693 } else
694 if (arg == "%C") {
695 if (work_stack.size() < 1)
696 log_cmd_error("Must have at least one element on the stack for operator %%C.\n");
697 select_op_module_to_cells(design, work_stack[work_stack.size()-1]);
698 } else
699 if (arg == "%c") {
700 if (work_stack.size() < 1)
701 log_cmd_error("Must have at least one element on the stack for operator %%c.\n");
702 work_stack.push_back(work_stack.back());
703 } else
704 if (arg == "%m") {
705 if (work_stack.size() < 1)
706 log_cmd_error("Must have at least one element on the stack for operator %%m.\n");
707 select_op_fullmod(design, work_stack[work_stack.size()-1]);
708 } else
709 if (arg == "%a") {
710 if (work_stack.size() < 1)
711 log_cmd_error("Must have at least one element on the stack for operator %%a.\n");
712 select_op_alias(design, work_stack[work_stack.size()-1]);
713 } else
714 if (arg == "%x" || (arg.size() > 2 && arg.compare(0, 2, "%x") == 0 && (arg[2] == ':' || arg[2] == '*' || arg[2] == '.' || ('0' <= arg[2] && arg[2] <= '9')))) {
715 if (work_stack.size() < 1)
716 log_cmd_error("Must have at least one element on the stack for operator %%x.\n");
717 select_op_expand(design, arg, 'x', false);
718 } else
719 if (arg == "%ci" || (arg.size() > 3 && arg.compare(0, 3, "%ci") == 0 && (arg[3] == ':' || arg[3] == '*' || arg[3] == '.' || ('0' <= arg[3] && arg[3] <= '9')))) {
720 if (work_stack.size() < 1)
721 log_cmd_error("Must have at least one element on the stack for operator %%ci.\n");
722 select_op_expand(design, arg, 'i', false);
723 } else
724 if (arg == "%co" || (arg.size() > 3 && arg.compare(0, 3, "%co") == 0 && (arg[3] == ':' || arg[3] == '*' || arg[3] == '.' || ('0' <= arg[3] && arg[3] <= '9')))) {
725 if (work_stack.size() < 1)
726 log_cmd_error("Must have at least one element on the stack for operator %%co.\n");
727 select_op_expand(design, arg, 'o', false);
728 } else
729 if (arg == "%xe" || (arg.size() > 3 && arg.compare(0, 3, "%xe") == 0 && (arg[3] == ':' || arg[3] == '*' || arg[3] == '.' || ('0' <= arg[3] && arg[3] <= '9')))) {
730 if (work_stack.size() < 1)
731 log_cmd_error("Must have at least one element on the stack for operator %%xe.\n");
732 select_op_expand(design, arg, 'x', true);
733 } else
734 if (arg == "%cie" || (arg.size() > 4 && arg.compare(0, 4, "%cie") == 0 && (arg[4] == ':' || arg[4] == '*' || arg[4] == '.' || ('0' <= arg[4] && arg[4] <= '9')))) {
735 if (work_stack.size() < 1)
736 log_cmd_error("Must have at least one element on the stack for operator %%cie.\n");
737 select_op_expand(design, arg, 'i', true);
738 } else
739 if (arg == "%coe" || (arg.size() > 4 && arg.compare(0, 4, "%coe") == 0 && (arg[4] == ':' || arg[4] == '*' || arg[4] == '.' || ('0' <= arg[4] && arg[4] <= '9')))) {
740 if (work_stack.size() < 1)
741 log_cmd_error("Must have at least one element on the stack for operator %%coe.\n");
742 select_op_expand(design, arg, 'o', true);
743 } else
744 log_cmd_error("Unknown selection operator '%s'.\n", arg.c_str());
745 if (work_stack.size() >= 1)
746 select_filter_active_mod(design, work_stack.back());
747 return;
748 }
749
750 if (arg[0] == '@') {
751 std::string set_name = RTLIL::escape_id(arg.substr(1));
752 if (design->selection_vars.count(set_name) > 0)
753 work_stack.push_back(design->selection_vars[set_name]);
754 else
755 log_cmd_error("Selection @%s is not defined!\n", RTLIL::unescape_id(set_name).c_str());
756 select_filter_active_mod(design, work_stack.back());
757 return;
758 }
759
760 if (!design->selected_active_module.empty()) {
761 arg_mod = design->selected_active_module;
762 arg_memb = arg;
763 } else
764 if (GetSize(arg) >= 2 && arg[0] >= 'a' && arg[0] <= 'z' && arg[1] == ':') {
765 arg_mod = "*", arg_memb = arg;
766 } else {
767 size_t pos = arg.find('/');
768 if (pos == std::string::npos) {
769 if (arg.find(':') == std::string::npos || arg.compare(0, 1, "A") == 0)
770 arg_mod = arg;
771 else
772 arg_mod = "*", arg_memb = arg;
773 } else {
774 arg_mod = arg.substr(0, pos);
775 arg_memb = arg.substr(pos+1);
776 }
777 }
778
779 work_stack.push_back(RTLIL::Selection());
780 RTLIL::Selection &sel = work_stack.back();
781
782 if (arg == "*" && arg_mod == "*") {
783 select_filter_active_mod(design, work_stack.back());
784 return;
785 }
786
787 sel.full_selection = false;
788 for (auto &mod_it : design->modules_)
789 {
790 if (arg_mod.compare(0, 2, "A:") == 0) {
791 if (!match_attr(mod_it.second->attributes, arg_mod.substr(2)))
792 continue;
793 } else
794 if (!match_ids(mod_it.first, arg_mod))
795 continue;
796
797 if (arg_memb == "") {
798 sel.selected_modules.insert(mod_it.first);
799 continue;
800 }
801
802 RTLIL::Module *mod = mod_it.second;
803 if (arg_memb.compare(0, 2, "w:") == 0) {
804 for (auto &it : mod->wires_)
805 if (match_ids(it.first, arg_memb.substr(2)))
806 sel.selected_members[mod->name].insert(it.first);
807 } else
808 if (arg_memb.compare(0, 2, "i:") == 0) {
809 for (auto &it : mod->wires_)
810 if (it.second->port_input && match_ids(it.first, arg_memb.substr(2)))
811 sel.selected_members[mod->name].insert(it.first);
812 } else
813 if (arg_memb.compare(0, 2, "o:") == 0) {
814 for (auto &it : mod->wires_)
815 if (it.second->port_output && match_ids(it.first, arg_memb.substr(2)))
816 sel.selected_members[mod->name].insert(it.first);
817 } else
818 if (arg_memb.compare(0, 2, "x:") == 0) {
819 for (auto &it : mod->wires_)
820 if ((it.second->port_input || it.second->port_output) && match_ids(it.first, arg_memb.substr(2)))
821 sel.selected_members[mod->name].insert(it.first);
822 } else
823 if (arg_memb.compare(0, 2, "s:") == 0) {
824 size_t delim = arg_memb.substr(2).find(':');
825 if (delim == std::string::npos) {
826 int width = atoi(arg_memb.substr(2).c_str());
827 for (auto &it : mod->wires_)
828 if (it.second->width == width)
829 sel.selected_members[mod->name].insert(it.first);
830 } else {
831 std::string min_str = arg_memb.substr(2, delim);
832 std::string max_str = arg_memb.substr(2+delim+1);
833 int min_width = min_str.empty() ? 0 : atoi(min_str.c_str());
834 int max_width = max_str.empty() ? -1 : atoi(max_str.c_str());
835 for (auto &it : mod->wires_)
836 if (min_width <= it.second->width && (it.second->width <= max_width || max_width == -1))
837 sel.selected_members[mod->name].insert(it.first);
838 }
839 } else
840 if (arg_memb.compare(0, 2, "m:") == 0) {
841 for (auto &it : mod->memories)
842 if (match_ids(it.first, arg_memb.substr(2)))
843 sel.selected_members[mod->name].insert(it.first);
844 } else
845 if (arg_memb.compare(0, 2, "c:") ==0) {
846 for (auto &it : mod->cells_)
847 if (match_ids(it.first, arg_memb.substr(2)))
848 sel.selected_members[mod->name].insert(it.first);
849 } else
850 if (arg_memb.compare(0, 2, "t:") == 0) {
851 for (auto &it : mod->cells_)
852 if (match_ids(it.second->type, arg_memb.substr(2)))
853 sel.selected_members[mod->name].insert(it.first);
854 } else
855 if (arg_memb.compare(0, 2, "p:") == 0) {
856 for (auto &it : mod->processes)
857 if (match_ids(it.first, arg_memb.substr(2)))
858 sel.selected_members[mod->name].insert(it.first);
859 } else
860 if (arg_memb.compare(0, 2, "a:") == 0) {
861 for (auto &it : mod->wires_)
862 if (match_attr(it.second->attributes, arg_memb.substr(2)))
863 sel.selected_members[mod->name].insert(it.first);
864 for (auto &it : mod->memories)
865 if (match_attr(it.second->attributes, arg_memb.substr(2)))
866 sel.selected_members[mod->name].insert(it.first);
867 for (auto &it : mod->cells_)
868 if (match_attr(it.second->attributes, arg_memb.substr(2)))
869 sel.selected_members[mod->name].insert(it.first);
870 for (auto &it : mod->processes)
871 if (match_attr(it.second->attributes, arg_memb.substr(2)))
872 sel.selected_members[mod->name].insert(it.first);
873 } else
874 if (arg_memb.compare(0, 2, "r:") == 0) {
875 for (auto &it : mod->cells_)
876 if (match_attr(it.second->parameters, arg_memb.substr(2)))
877 sel.selected_members[mod->name].insert(it.first);
878 } else {
879 if (arg_memb.compare(0, 2, "n:") == 0)
880 arg_memb = arg_memb.substr(2);
881 for (auto &it : mod->wires_)
882 if (match_ids(it.first, arg_memb))
883 sel.selected_members[mod->name].insert(it.first);
884 for (auto &it : mod->memories)
885 if (match_ids(it.first, arg_memb))
886 sel.selected_members[mod->name].insert(it.first);
887 for (auto &it : mod->cells_)
888 if (match_ids(it.first, arg_memb))
889 sel.selected_members[mod->name].insert(it.first);
890 for (auto &it : mod->processes)
891 if (match_ids(it.first, arg_memb))
892 sel.selected_members[mod->name].insert(it.first);
893 }
894 }
895
896 select_filter_active_mod(design, work_stack.back());
897 }
898
899 static std::string describe_selection_for_assert(RTLIL::Design *design, RTLIL::Selection *sel)
900 {
901 std::string desc = "Selection contains:\n";
902 for (auto mod_it : design->modules_)
903 {
904 if (sel->selected_module(mod_it.first)) {
905 for (auto &it : mod_it.second->wires_)
906 if (sel->selected_member(mod_it.first, it.first))
907 desc += stringf("%s/%s\n", id2cstr(mod_it.first), id2cstr(it.first));
908 for (auto &it : mod_it.second->memories)
909 if (sel->selected_member(mod_it.first, it.first))
910 desc += stringf("%s/%s\n", id2cstr(mod_it.first), id2cstr(it.first));
911 for (auto &it : mod_it.second->cells_)
912 if (sel->selected_member(mod_it.first, it.first))
913 desc += stringf("%s/%s\n", id2cstr(mod_it.first), id2cstr(it.first));
914 for (auto &it : mod_it.second->processes)
915 if (sel->selected_member(mod_it.first, it.first))
916 desc += stringf("%s/%s\n", id2cstr(mod_it.first), id2cstr(it.first));
917 }
918 }
919 return desc;
920 }
921
922 PRIVATE_NAMESPACE_END
923 YOSYS_NAMESPACE_BEGIN
924
925 // used in kernel/register.cc and maybe other locations, extern decl. in register.h
926 void handle_extra_select_args(Pass *pass, vector<string> args, size_t argidx, size_t args_size, RTLIL::Design *design)
927 {
928 work_stack.clear();
929 for (; argidx < args_size; argidx++) {
930 if (args[argidx].compare(0, 1, "-") == 0) {
931 if (pass != NULL)
932 pass->cmd_error(args, argidx, "Unexpected option in selection arguments.");
933 else
934 log_cmd_error("Unexpected option in selection arguments.");
935 }
936 select_stmt(design, args[argidx]);
937 }
938 while (work_stack.size() > 1) {
939 select_op_union(design, work_stack.front(), work_stack.back());
940 work_stack.pop_back();
941 }
942 if (work_stack.empty())
943 design->selection_stack.push_back(RTLIL::Selection(false));
944 else
945 design->selection_stack.push_back(work_stack.back());
946 }
947
948 // extern decl. in register.h
949 RTLIL::Selection eval_select_args(const vector<string> &args, RTLIL::Design *design)
950 {
951 work_stack.clear();
952 for (auto &arg : args)
953 select_stmt(design, arg);
954 while (work_stack.size() > 1) {
955 select_op_union(design, work_stack.front(), work_stack.back());
956 work_stack.pop_back();
957 }
958 if (work_stack.empty())
959 return RTLIL::Selection(false);
960 return work_stack.back();
961 }
962
963 // extern decl. in register.h
964 void eval_select_op(vector<RTLIL::Selection> &work, const string &op, RTLIL::Design *design)
965 {
966 work_stack.swap(work);
967 select_stmt(design, op);
968 work_stack.swap(work);
969 }
970
971 YOSYS_NAMESPACE_END
972 PRIVATE_NAMESPACE_BEGIN
973
974 struct SelectPass : public Pass {
975 SelectPass() : Pass("select", "modify and view the list of selected objects") { }
976 void help() YS_OVERRIDE
977 {
978 // |---v---|---v---|---v---|---v---|---v---|---v---|---v---|---v---|---v---|---v---|
979 log("\n");
980 log(" select [ -add | -del | -set <name> ] {-read <filename> | <selection>}\n");
981 log(" select [ <assert_option> ] {-read <filename> | <selection>}\n");
982 log(" select [ -list | -write <filename> | -count | -clear ]\n");
983 log(" select -module <modname>\n");
984 log("\n");
985 log("Most commands use the list of currently selected objects to determine which part\n");
986 log("of the design to operate on. This command can be used to modify and view this\n");
987 log("list of selected objects.\n");
988 log("\n");
989 log("Note that many commands support an optional [selection] argument that can be\n");
990 log("used to override the global selection for the command. The syntax of this\n");
991 log("optional argument is identical to the syntax of the <selection> argument\n");
992 log("described here.\n");
993 log("\n");
994 log(" -add, -del\n");
995 log(" add or remove the given objects to the current selection.\n");
996 log(" without this options the current selection is replaced.\n");
997 log("\n");
998 log(" -set <name>\n");
999 log(" do not modify the current selection. instead save the new selection\n");
1000 log(" under the given name (see @<name> below). to save the current selection,\n");
1001 log(" use \"select -set <name> %%\"\n");
1002 log("\n");
1003 log(" -assert-none\n");
1004 log(" do not modify the current selection. instead assert that the given\n");
1005 log(" selection is empty. i.e. produce an error if any object matching the\n");
1006 log(" selection is found.\n");
1007 log("\n");
1008 log(" -assert-any\n");
1009 log(" do not modify the current selection. instead assert that the given\n");
1010 log(" selection is non-empty. i.e. produce an error if no object matching\n");
1011 log(" the selection is found.\n");
1012 log("\n");
1013 log(" -assert-count N\n");
1014 log(" do not modify the current selection. instead assert that the given\n");
1015 log(" selection contains exactly N objects.\n");
1016 log("\n");
1017 log(" -assert-max N\n");
1018 log(" do not modify the current selection. instead assert that the given\n");
1019 log(" selection contains less than or exactly N objects.\n");
1020 log("\n");
1021 log(" -assert-min N\n");
1022 log(" do not modify the current selection. instead assert that the given\n");
1023 log(" selection contains at least N objects.\n");
1024 log("\n");
1025 log(" -list\n");
1026 log(" list all objects in the current selection\n");
1027 log("\n");
1028 log(" -write <filename>\n");
1029 log(" like -list but write the output to the specified file\n");
1030 log("\n");
1031 log(" -read <filename>\n");
1032 log(" read the specified file (written by -write)\n");
1033 log("\n");
1034 log(" -count\n");
1035 log(" count all objects in the current selection\n");
1036 log("\n");
1037 log(" -clear\n");
1038 log(" clear the current selection. this effectively selects the whole\n");
1039 log(" design. it also resets the selected module (see -module). use the\n");
1040 log(" command 'select *' to select everything but stay in the current module.\n");
1041 log("\n");
1042 log(" -none\n");
1043 log(" create an empty selection. the current module is unchanged.\n");
1044 log("\n");
1045 log(" -module <modname>\n");
1046 log(" limit the current scope to the specified module.\n");
1047 log(" the difference between this and simply selecting the module\n");
1048 log(" is that all object names are interpreted relative to this\n");
1049 log(" module after this command until the selection is cleared again.\n");
1050 log("\n");
1051 log("When this command is called without an argument, the current selection\n");
1052 log("is displayed in a compact form (i.e. only the module name when a whole module\n");
1053 log("is selected).\n");
1054 log("\n");
1055 log("The <selection> argument itself is a series of commands for a simple stack\n");
1056 log("machine. Each element on the stack represents a set of selected objects.\n");
1057 log("After this commands have been executed, the union of all remaining sets\n");
1058 log("on the stack is computed and used as selection for the command.\n");
1059 log("\n");
1060 log("Pushing (selecting) object when not in -module mode:\n");
1061 log("\n");
1062 log(" <mod_pattern>\n");
1063 log(" select the specified module(s)\n");
1064 log("\n");
1065 log(" <mod_pattern>/<obj_pattern>\n");
1066 log(" select the specified object(s) from the module(s)\n");
1067 log("\n");
1068 log("Pushing (selecting) object when in -module mode:\n");
1069 log("\n");
1070 log(" <obj_pattern>\n");
1071 log(" select the specified object(s) from the current module\n");
1072 log("\n");
1073 log("A <mod_pattern> can be a module name, wildcard expression (*, ?, [..])\n");
1074 log("matching module names, or one of the following:\n");
1075 log("\n");
1076 log(" A:<pattern>, A:<pattern>=<pattern>\n");
1077 log(" all modules with an attribute matching the given pattern\n");
1078 log(" in addition to = also <, <=, >=, and > are supported\n");
1079 log("\n");
1080 log("An <obj_pattern> can be an object name, wildcard expression, or one of\n");
1081 log("the following:\n");
1082 log("\n");
1083 log(" w:<pattern>\n");
1084 log(" all wires with a name matching the given wildcard pattern\n");
1085 log("\n");
1086 log(" i:<pattern>, o:<pattern>, x:<pattern>\n");
1087 log(" all inputs (i:), outputs (o:) or any ports (x:) with matching names\n");
1088 log("\n");
1089 log(" s:<size>, s:<min>:<max>\n");
1090 log(" all wires with a matching width\n");
1091 log("\n");
1092 log(" m:<pattern>\n");
1093 log(" all memories with a name matching the given pattern\n");
1094 log("\n");
1095 log(" c:<pattern>\n");
1096 log(" all cells with a name matching the given pattern\n");
1097 log("\n");
1098 log(" t:<pattern>\n");
1099 log(" all cells with a type matching the given pattern\n");
1100 log("\n");
1101 log(" p:<pattern>\n");
1102 log(" all processes with a name matching the given pattern\n");
1103 log("\n");
1104 log(" a:<pattern>\n");
1105 log(" all objects with an attribute name matching the given pattern\n");
1106 log("\n");
1107 log(" a:<pattern>=<pattern>\n");
1108 log(" all objects with a matching attribute name-value-pair.\n");
1109 log(" in addition to = also <, <=, >=, and > are supported\n");
1110 log("\n");
1111 log(" r:<pattern>, r:<pattern>=<pattern>\n");
1112 log(" cells with matching parameters. also with <, <=, >= and >.\n");
1113 log("\n");
1114 log(" n:<pattern>\n");
1115 log(" all objects with a name matching the given pattern\n");
1116 log(" (i.e. 'n:' is optional as it is the default matching rule)\n");
1117 log("\n");
1118 log(" @<name>\n");
1119 log(" push the selection saved prior with 'select -set <name> ...'\n");
1120 log("\n");
1121 log("The following actions can be performed on the top sets on the stack:\n");
1122 log("\n");
1123 log(" %%\n");
1124 log(" push a copy of the current selection to the stack\n");
1125 log("\n");
1126 log(" %%%%\n");
1127 log(" replace the stack with a union of all elements on it\n");
1128 log("\n");
1129 log(" %%n\n");
1130 log(" replace top set with its invert\n");
1131 log("\n");
1132 log(" %%u\n");
1133 log(" replace the two top sets on the stack with their union\n");
1134 log("\n");
1135 log(" %%i\n");
1136 log(" replace the two top sets on the stack with their intersection\n");
1137 log("\n");
1138 log(" %%d\n");
1139 log(" pop the top set from the stack and subtract it from the new top\n");
1140 log("\n");
1141 log(" %%D\n");
1142 log(" like %%d but swap the roles of two top sets on the stack\n");
1143 log("\n");
1144 log(" %%c\n");
1145 log(" create a copy of the top set from the stack and push it\n");
1146 log("\n");
1147 log(" %%x[<num1>|*][.<num2>][:<rule>[:<rule>..]]\n");
1148 log(" expand top set <num1> num times according to the specified rules.\n");
1149 log(" (i.e. select all cells connected to selected wires and select all\n");
1150 log(" wires connected to selected cells) The rules specify which cell\n");
1151 log(" ports to use for this. the syntax for a rule is a '-' for exclusion\n");
1152 log(" and a '+' for inclusion, followed by an optional comma separated\n");
1153 log(" list of cell types followed by an optional comma separated list of\n");
1154 log(" cell ports in square brackets. a rule can also be just a cell or wire\n");
1155 log(" name that limits the expansion (is included but does not go beyond).\n");
1156 log(" select at most <num2> objects. a warning message is printed when this\n");
1157 log(" limit is reached. When '*' is used instead of <num1> then the process\n");
1158 log(" is repeated until no further object are selected.\n");
1159 log("\n");
1160 log(" %%ci[<num1>|*][.<num2>][:<rule>[:<rule>..]]\n");
1161 log(" %%co[<num1>|*][.<num2>][:<rule>[:<rule>..]]\n");
1162 log(" similar to %%x, but only select input (%%ci) or output cones (%%co)\n");
1163 log("\n");
1164 log(" %%xe[...] %%cie[...] %%coe\n");
1165 log(" like %%x, %%ci, and %%co but only consider combinatorial cells\n");
1166 log("\n");
1167 log(" %%a\n");
1168 log(" expand top set by selecting all wires that are (at least in part)\n");
1169 log(" aliases for selected wires.\n");
1170 log("\n");
1171 log(" %%s\n");
1172 log(" expand top set by adding all modules that implement cells in selected\n");
1173 log(" modules\n");
1174 log("\n");
1175 log(" %%m\n");
1176 log(" expand top set by selecting all modules that contain selected objects\n");
1177 log("\n");
1178 log(" %%M\n");
1179 log(" select modules that implement selected cells\n");
1180 log("\n");
1181 log(" %%C\n");
1182 log(" select cells that implement selected modules\n");
1183 log("\n");
1184 log(" %%R[<num>]\n");
1185 log(" select <num> random objects from top selection (default 1)\n");
1186 log("\n");
1187 log("Example: the following command selects all wires that are connected to a\n");
1188 log("'GATE' input of a 'SWITCH' cell:\n");
1189 log("\n");
1190 log(" select */t:SWITCH %%x:+[GATE] */t:SWITCH %%d\n");
1191 log("\n");
1192 }
1193 void execute(std::vector<std::string> args, RTLIL::Design *design) YS_OVERRIDE
1194 {
1195 bool add_mode = false;
1196 bool del_mode = false;
1197 bool clear_mode = false;
1198 bool none_mode = false;
1199 bool list_mode = false;
1200 bool count_mode = false;
1201 bool got_module = false;
1202 bool assert_none = false;
1203 bool assert_any = false;
1204 int assert_count = -1;
1205 int assert_max = -1;
1206 int assert_min = -1;
1207 std::string write_file, read_file;
1208 std::string set_name, sel_str;
1209
1210 work_stack.clear();
1211
1212 size_t argidx;
1213 for (argidx = 1; argidx < args.size(); argidx++)
1214 {
1215 std::string arg = args[argidx];
1216 if (arg == "-add") {
1217 add_mode = true;
1218 continue;
1219 }
1220 if (arg == "-del") {
1221 del_mode = true;
1222 continue;
1223 }
1224 if (arg == "-assert-none") {
1225 assert_none = true;
1226 continue;
1227 }
1228 if (arg == "-assert-any") {
1229 assert_any = true;
1230 continue;
1231 }
1232 if (arg == "-assert-count" && argidx+1 < args.size()) {
1233 assert_count = atoi(args[++argidx].c_str());
1234 continue;
1235 }
1236 if (arg == "-assert-max" && argidx+1 < args.size()) {
1237 assert_max = atoi(args[++argidx].c_str());
1238 continue;
1239 }
1240 if (arg == "-assert-min" && argidx+1 < args.size()) {
1241 assert_min = atoi(args[++argidx].c_str());
1242 continue;
1243 }
1244 if (arg == "-clear") {
1245 clear_mode = true;
1246 continue;
1247 }
1248 if (arg == "-none") {
1249 none_mode = true;
1250 continue;
1251 }
1252 if (arg == "-list") {
1253 list_mode = true;
1254 continue;
1255 }
1256 if (arg == "-write" && argidx+1 < args.size()) {
1257 write_file = args[++argidx];
1258 continue;
1259 }
1260 if (arg == "-read" && argidx+1 < args.size()) {
1261 read_file = args[++argidx];
1262 continue;
1263 }
1264 if (arg == "-count") {
1265 count_mode = true;
1266 continue;
1267 }
1268 if (arg == "-module" && argidx+1 < args.size()) {
1269 RTLIL::IdString mod_name = RTLIL::escape_id(args[++argidx]);
1270 if (design->modules_.count(mod_name) == 0)
1271 log_cmd_error("No such module: %s\n", id2cstr(mod_name));
1272 design->selected_active_module = mod_name.str();
1273 got_module = true;
1274 continue;
1275 }
1276 if (arg == "-set" && argidx+1 < args.size()) {
1277 set_name = RTLIL::escape_id(args[++argidx]);
1278 continue;
1279 }
1280 if (arg.size() > 0 && arg[0] == '-')
1281 log_cmd_error("Unknown option %s.\n", arg.c_str());
1282 select_stmt(design, arg);
1283 sel_str += " " + arg;
1284 }
1285
1286 if (!read_file.empty())
1287 {
1288 if (!sel_str.empty())
1289 log_cmd_error("Option -read can not be combined with a selection expression.\n");
1290
1291 std::ifstream f(read_file);
1292 yosys_input_files.insert(read_file);
1293 if (f.fail())
1294 log_error("Can't open '%s' for reading: %s\n", read_file.c_str(), strerror(errno));
1295
1296 RTLIL::Selection sel(false);
1297 string line;
1298
1299 while (std::getline(f, line)) {
1300 size_t slash_pos = line.find('/');
1301 if (slash_pos == string::npos) {
1302 log_warning("Ignoring line without slash in 'select -read': %s\n", line.c_str());
1303 continue;
1304 }
1305 IdString mod_name = RTLIL::escape_id(line.substr(0, slash_pos));
1306 IdString obj_name = RTLIL::escape_id(line.substr(slash_pos+1));
1307 sel.selected_members[mod_name].insert(obj_name);
1308 }
1309
1310 select_filter_active_mod(design, sel);
1311 sel.optimize(design);
1312 work_stack.push_back(sel);
1313 }
1314
1315 if (clear_mode && args.size() != 2)
1316 log_cmd_error("Option -clear can not be combined with any other options.\n");
1317
1318 if (none_mode && args.size() != 2)
1319 log_cmd_error("Option -none can not be combined with any other options.\n");
1320
1321 if (add_mode + del_mode + assert_none + assert_any + (assert_count >= 0) + (assert_max >= 0) + (assert_min >= 0) > 1)
1322 log_cmd_error("Options -add, -del, -assert-none, -assert-any, assert-count, -assert-max or -assert-min can not be combined.\n");
1323
1324 if ((list_mode || !write_file.empty() || count_mode) && (add_mode || del_mode || assert_none || assert_any || assert_count >= 0 || assert_max >= 0 || assert_min >= 0))
1325 log_cmd_error("Options -list, -write and -count can not be combined with -add, -del, -assert-none, -assert-any, assert-count, -assert-max, or -assert-min.\n");
1326
1327 if (!set_name.empty() && (list_mode || !write_file.empty() || count_mode || add_mode || del_mode || assert_none || assert_any || assert_count >= 0 || assert_max >= 0 || assert_min >= 0))
1328 log_cmd_error("Option -set can not be combined with -list, -write, -count, -add, -del, -assert-none, -assert-any, -assert-count, -assert-max, or -assert-min.\n");
1329
1330 if (work_stack.size() == 0 && got_module) {
1331 RTLIL::Selection sel;
1332 select_filter_active_mod(design, sel);
1333 work_stack.push_back(sel);
1334 }
1335
1336 while (work_stack.size() > 1) {
1337 select_op_union(design, work_stack.front(), work_stack.back());
1338 work_stack.pop_back();
1339 }
1340
1341 log_assert(design->selection_stack.size() > 0);
1342
1343 if (clear_mode) {
1344 design->selection_stack.back() = RTLIL::Selection(true);
1345 design->selected_active_module = std::string();
1346 return;
1347 }
1348
1349 if (none_mode) {
1350 design->selection_stack.back() = RTLIL::Selection(false);
1351 return;
1352 }
1353
1354 if (list_mode || count_mode || !write_file.empty())
1355 {
1356 #define LOG_OBJECT(...) { if (list_mode) log(__VA_ARGS__); if (f != NULL) fprintf(f, __VA_ARGS__); total_count++; }
1357 int total_count = 0;
1358 FILE *f = NULL;
1359 if (!write_file.empty()) {
1360 f = fopen(write_file.c_str(), "w");
1361 yosys_output_files.insert(write_file);
1362 if (f == NULL)
1363 log_error("Can't open '%s' for writing: %s\n", write_file.c_str(), strerror(errno));
1364 }
1365 RTLIL::Selection *sel = &design->selection_stack.back();
1366 if (work_stack.size() > 0)
1367 sel = &work_stack.back();
1368 sel->optimize(design);
1369 for (auto mod_it : design->modules_)
1370 {
1371 if (sel->selected_whole_module(mod_it.first) && list_mode)
1372 log("%s\n", id2cstr(mod_it.first));
1373 if (sel->selected_module(mod_it.first)) {
1374 for (auto &it : mod_it.second->wires_)
1375 if (sel->selected_member(mod_it.first, it.first))
1376 LOG_OBJECT("%s/%s\n", id2cstr(mod_it.first), id2cstr(it.first))
1377 for (auto &it : mod_it.second->memories)
1378 if (sel->selected_member(mod_it.first, it.first))
1379 LOG_OBJECT("%s/%s\n", id2cstr(mod_it.first), id2cstr(it.first))
1380 for (auto &it : mod_it.second->cells_)
1381 if (sel->selected_member(mod_it.first, it.first))
1382 LOG_OBJECT("%s/%s\n", id2cstr(mod_it.first), id2cstr(it.first))
1383 for (auto &it : mod_it.second->processes)
1384 if (sel->selected_member(mod_it.first, it.first))
1385 LOG_OBJECT("%s/%s\n", id2cstr(mod_it.first), id2cstr(it.first))
1386 }
1387 }
1388 if (count_mode)
1389 log("%d objects.\n", total_count);
1390 if (f != NULL)
1391 fclose(f);
1392 #undef LOG_OBJECT
1393 return;
1394 }
1395
1396 if (add_mode)
1397 {
1398 if (work_stack.size() == 0)
1399 log_cmd_error("Nothing to add to selection.\n");
1400 select_op_union(design, design->selection_stack.back(), work_stack.back());
1401 design->selection_stack.back().optimize(design);
1402 return;
1403 }
1404
1405 if (del_mode)
1406 {
1407 if (work_stack.size() == 0)
1408 log_cmd_error("Nothing to delete from selection.\n");
1409 select_op_diff(design, design->selection_stack.back(), work_stack.back());
1410 design->selection_stack.back().optimize(design);
1411 return;
1412 }
1413
1414 if (assert_none)
1415 {
1416 if (work_stack.size() == 0)
1417 log_cmd_error("No selection to check.\n");
1418 work_stack.back().optimize(design);
1419 if (!work_stack.back().empty())
1420 {
1421 RTLIL::Selection *sel = &work_stack.back();
1422 sel->optimize(design);
1423 std::string desc = describe_selection_for_assert(design, sel);
1424 log_error("Assertion failed: selection is not empty:%s\n%s", sel_str.c_str(), desc.c_str());
1425 }
1426 return;
1427 }
1428
1429 if (assert_any)
1430 {
1431 if (work_stack.size() == 0)
1432 log_cmd_error("No selection to check.\n");
1433 work_stack.back().optimize(design);
1434 if (work_stack.back().empty())
1435 {
1436 RTLIL::Selection *sel = &work_stack.back();
1437 sel->optimize(design);
1438 std::string desc = describe_selection_for_assert(design, sel);
1439 log_error("Assertion failed: selection is empty:%s\n%s", sel_str.c_str(), desc.c_str());
1440 }
1441 return;
1442 }
1443
1444 if (assert_count >= 0 || assert_max >= 0 || assert_min >= 0)
1445 {
1446 int total_count = 0;
1447 if (work_stack.size() == 0)
1448 log_cmd_error("No selection to check.\n");
1449 RTLIL::Selection *sel = &work_stack.back();
1450 sel->optimize(design);
1451 for (auto mod_it : design->modules_)
1452 if (sel->selected_module(mod_it.first)) {
1453 for (auto &it : mod_it.second->wires_)
1454 if (sel->selected_member(mod_it.first, it.first))
1455 total_count++;
1456 for (auto &it : mod_it.second->memories)
1457 if (sel->selected_member(mod_it.first, it.first))
1458 total_count++;
1459 for (auto &it : mod_it.second->cells_)
1460 if (sel->selected_member(mod_it.first, it.first))
1461 total_count++;
1462 for (auto &it : mod_it.second->processes)
1463 if (sel->selected_member(mod_it.first, it.first))
1464 total_count++;
1465 }
1466 if (assert_count >= 0 && assert_count != total_count)
1467 {
1468 std::string desc = describe_selection_for_assert(design, sel);
1469 log_error("Assertion failed: selection contains %d elements instead of the asserted %d:%s\n%s",
1470 total_count, assert_count, sel_str.c_str(), desc.c_str());
1471 }
1472 if (assert_max >= 0 && assert_max < total_count)
1473 {
1474 std::string desc = describe_selection_for_assert(design, sel);
1475 log_error("Assertion failed: selection contains %d elements, more than the maximum number %d:%s\n%s",
1476 total_count, assert_max, sel_str.c_str(), desc.c_str());
1477 }
1478 if (assert_min >= 0 && assert_min > total_count)
1479 {
1480 std::string desc = describe_selection_for_assert(design, sel);
1481 log_error("Assertion failed: selection contains %d elements, less than the minimum number %d:%s\n%s",
1482 total_count, assert_min, sel_str.c_str(), desc.c_str());
1483 }
1484 return;
1485 }
1486
1487 if (!set_name.empty())
1488 {
1489 if (work_stack.size() == 0)
1490 design->selection_vars[set_name] = RTLIL::Selection(false);
1491 else
1492 design->selection_vars[set_name] = work_stack.back();
1493 return;
1494 }
1495
1496 if (work_stack.size() == 0) {
1497 RTLIL::Selection &sel = design->selection_stack.back();
1498 if (sel.full_selection)
1499 log("*\n");
1500 for (auto &it : sel.selected_modules)
1501 log("%s\n", id2cstr(it));
1502 for (auto &it : sel.selected_members)
1503 for (auto &it2 : it.second)
1504 log("%s/%s\n", id2cstr(it.first), id2cstr(it2));
1505 return;
1506 }
1507
1508 design->selection_stack.back() = work_stack.back();
1509 design->selection_stack.back().optimize(design);
1510 }
1511 } SelectPass;
1512
1513 struct CdPass : public Pass {
1514 CdPass() : Pass("cd", "a shortcut for 'select -module <name>'") { }
1515 void help() YS_OVERRIDE
1516 {
1517 // |---v---|---v---|---v---|---v---|---v---|---v---|---v---|---v---|---v---|---v---|
1518 log("\n");
1519 log(" cd <modname>\n");
1520 log("\n");
1521 log("This is just a shortcut for 'select -module <modname>'.\n");
1522 log("\n");
1523 log("\n");
1524 log(" cd <cellname>\n");
1525 log("\n");
1526 log("When no module with the specified name is found, but there is a cell\n");
1527 log("with the specified name in the current module, then this is equivalent\n");
1528 log("to 'cd <celltype>'.\n");
1529 log("\n");
1530 log(" cd ..\n");
1531 log("\n");
1532 log("Remove trailing substrings that start with '.' in current module name until\n");
1533 log("the name of a module in the current design is generated, then switch to that\n");
1534 log("module. Otherwise clear the current selection.\n");
1535 log("\n");
1536 log(" cd\n");
1537 log("\n");
1538 log("This is just a shortcut for 'select -clear'.\n");
1539 log("\n");
1540 }
1541 void execute(std::vector<std::string> args, RTLIL::Design *design) YS_OVERRIDE
1542 {
1543 if (args.size() != 1 && args.size() != 2)
1544 log_cmd_error("Invalid number of arguments.\n");
1545
1546 if (args.size() == 1 || args[1] == "/") {
1547 design->selection_stack.back() = RTLIL::Selection(true);
1548 design->selected_active_module = std::string();
1549 return;
1550 }
1551
1552 if (args[1] == "..")
1553 {
1554 string modname = design->selected_active_module;
1555
1556 design->selection_stack.back() = RTLIL::Selection(true);
1557 design->selected_active_module = std::string();
1558
1559 while (1)
1560 {
1561 size_t pos = modname.rfind('.');
1562
1563 if (pos == string::npos)
1564 break;
1565
1566 modname = modname.substr(0, pos);
1567 Module *mod = design->module(modname);
1568
1569 if (mod == nullptr)
1570 continue;
1571
1572 design->selected_active_module = modname;
1573 design->selection_stack.back() = RTLIL::Selection();
1574 select_filter_active_mod(design, design->selection_stack.back());
1575 design->selection_stack.back().optimize(design);
1576 return;
1577 }
1578
1579 return;
1580 }
1581
1582 std::string modname = RTLIL::escape_id(args[1]);
1583
1584 if (design->modules_.count(modname) == 0 && !design->selected_active_module.empty()) {
1585 RTLIL::Module *module = NULL;
1586 if (design->modules_.count(design->selected_active_module) > 0)
1587 module = design->modules_.at(design->selected_active_module);
1588 if (module != NULL && module->cells_.count(modname) > 0)
1589 modname = module->cells_.at(modname)->type.str();
1590 }
1591
1592 if (design->modules_.count(modname) > 0) {
1593 design->selected_active_module = modname;
1594 design->selection_stack.back() = RTLIL::Selection();
1595 select_filter_active_mod(design, design->selection_stack.back());
1596 design->selection_stack.back().optimize(design);
1597 return;
1598 }
1599
1600 log_cmd_error("No such module `%s' found!\n", RTLIL::unescape_id(modname).c_str());
1601 }
1602 } CdPass;
1603
1604 template<typename T>
1605 static void log_matches(const char *title, Module *module, T list)
1606 {
1607 std::vector<IdString> matches;
1608
1609 for (auto &it : list)
1610 if (module->selected(it.second))
1611 matches.push_back(it.first);
1612
1613 if (!matches.empty()) {
1614 log("\n%d %s:\n", int(matches.size()), title);
1615 std::sort(matches.begin(), matches.end(), RTLIL::sort_by_id_str());
1616 for (auto id : matches)
1617 log(" %s\n", RTLIL::id2cstr(id));
1618 }
1619 }
1620
1621 struct LsPass : public Pass {
1622 LsPass() : Pass("ls", "list modules or objects in modules") { }
1623 void help() YS_OVERRIDE
1624 {
1625 // |---v---|---v---|---v---|---v---|---v---|---v---|---v---|---v---|---v---|---v---|
1626 log("\n");
1627 log(" ls [selection]\n");
1628 log("\n");
1629 log("When no active module is selected, this prints a list of modules.\n");
1630 log("\n");
1631 log("When an active module is selected, this prints a list of objects in the module.\n");
1632 log("\n");
1633 }
1634 void execute(std::vector<std::string> args, RTLIL::Design *design) YS_OVERRIDE
1635 {
1636 size_t argidx = 1;
1637 extra_args(args, argidx, design);
1638
1639 if (design->selected_active_module.empty())
1640 {
1641 std::vector<IdString> matches;
1642
1643 for (auto mod : design->selected_modules())
1644 matches.push_back(mod->name);
1645
1646 if (!matches.empty()) {
1647 log("\n%d %s:\n", int(matches.size()), "modules");
1648 std::sort(matches.begin(), matches.end(), RTLIL::sort_by_id_str());
1649 for (auto id : matches)
1650 log(" %s%s\n", log_id(id), design->selected_whole_module(design->module(id)) ? "" : "*");
1651 }
1652 }
1653 else
1654 if (design->module(design->selected_active_module) != nullptr)
1655 {
1656 RTLIL::Module *module = design->module(design->selected_active_module);
1657 log_matches("wires", module, module->wires_);
1658 log_matches("memories", module, module->memories);
1659 log_matches("cells", module, module->cells_);
1660 log_matches("processes", module, module->processes);
1661 }
1662 }
1663 } LsPass;
1664
1665 PRIVATE_NAMESPACE_END