Merge branch 'yosys-0.5-vtr' of https://github.com/eddiehung/yosys into eddiehung-vtr
[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.substr(1) == pattern)
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.substr(pos, 2) == "!=")
128 return match_attr(attributes, match_expr.substr(0, pos), match_expr.substr(pos+2), '!');
129 if (match_expr.substr(pos, 2) == "<=")
130 return match_attr(attributes, match_expr.substr(0, pos), match_expr.substr(pos+2), '[');
131 if (match_expr.substr(pos, 2) == ">=")
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 %%M.\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.substr(0, 2) == "%x" && (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.substr(0, 3) == "%ci" && (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.substr(0, 3) == "%co" && (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.substr(0, 3) == "%xe" && (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.substr(0, 4) == "%cie" && (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.substr(0, 4) == "%coe" && (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 size_t pos = arg.find('/');
765 if (pos == std::string::npos) {
766 if (arg.find(':') == std::string::npos || arg.substr(0, 1) == "A")
767 arg_mod = arg;
768 else
769 arg_mod = "*", arg_memb = arg;
770 } else {
771 arg_mod = arg.substr(0, pos);
772 arg_memb = arg.substr(pos+1);
773 }
774 }
775
776 work_stack.push_back(RTLIL::Selection());
777 RTLIL::Selection &sel = work_stack.back();
778
779 if (arg == "*" && arg_mod == "*") {
780 select_filter_active_mod(design, work_stack.back());
781 return;
782 }
783
784 sel.full_selection = false;
785 for (auto &mod_it : design->modules_)
786 {
787 if (arg_mod.substr(0, 2) == "A:") {
788 if (!match_attr(mod_it.second->attributes, arg_mod.substr(2)))
789 continue;
790 } else
791 if (!match_ids(mod_it.first, arg_mod))
792 continue;
793
794 if (arg_memb == "") {
795 sel.selected_modules.insert(mod_it.first);
796 continue;
797 }
798
799 RTLIL::Module *mod = mod_it.second;
800 if (arg_memb.substr(0, 2) == "w:") {
801 for (auto &it : mod->wires_)
802 if (match_ids(it.first, arg_memb.substr(2)))
803 sel.selected_members[mod->name].insert(it.first);
804 } else
805 if (arg_memb.substr(0, 2) == "i:") {
806 for (auto &it : mod->wires_)
807 if (it.second->port_input && match_ids(it.first, arg_memb.substr(2)))
808 sel.selected_members[mod->name].insert(it.first);
809 } else
810 if (arg_memb.substr(0, 2) == "o:") {
811 for (auto &it : mod->wires_)
812 if (it.second->port_output && match_ids(it.first, arg_memb.substr(2)))
813 sel.selected_members[mod->name].insert(it.first);
814 } else
815 if (arg_memb.substr(0, 2) == "x:") {
816 for (auto &it : mod->wires_)
817 if ((it.second->port_input || it.second->port_output) && match_ids(it.first, arg_memb.substr(2)))
818 sel.selected_members[mod->name].insert(it.first);
819 } else
820 if (arg_memb.substr(0, 2) == "s:") {
821 size_t delim = arg_memb.substr(2).find(':');
822 if (delim == std::string::npos) {
823 int width = atoi(arg_memb.substr(2).c_str());
824 for (auto &it : mod->wires_)
825 if (it.second->width == width)
826 sel.selected_members[mod->name].insert(it.first);
827 } else {
828 std::string min_str = arg_memb.substr(2, delim);
829 std::string max_str = arg_memb.substr(2+delim+1);
830 int min_width = min_str.empty() ? 0 : atoi(min_str.c_str());
831 int max_width = max_str.empty() ? -1 : atoi(max_str.c_str());
832 for (auto &it : mod->wires_)
833 if (min_width <= it.second->width && (it.second->width <= max_width || max_width == -1))
834 sel.selected_members[mod->name].insert(it.first);
835 }
836 } else
837 if (arg_memb.substr(0, 2) == "m:") {
838 for (auto &it : mod->memories)
839 if (match_ids(it.first, arg_memb.substr(2)))
840 sel.selected_members[mod->name].insert(it.first);
841 } else
842 if (arg_memb.substr(0, 2) == "c:") {
843 for (auto &it : mod->cells_)
844 if (match_ids(it.first, arg_memb.substr(2)))
845 sel.selected_members[mod->name].insert(it.first);
846 } else
847 if (arg_memb.substr(0, 2) == "t:") {
848 for (auto &it : mod->cells_)
849 if (match_ids(it.second->type, arg_memb.substr(2)))
850 sel.selected_members[mod->name].insert(it.first);
851 } else
852 if (arg_memb.substr(0, 2) == "p:") {
853 for (auto &it : mod->processes)
854 if (match_ids(it.first, arg_memb.substr(2)))
855 sel.selected_members[mod->name].insert(it.first);
856 } else
857 if (arg_memb.substr(0, 2) == "a:") {
858 for (auto &it : mod->wires_)
859 if (match_attr(it.second->attributes, arg_memb.substr(2)))
860 sel.selected_members[mod->name].insert(it.first);
861 for (auto &it : mod->memories)
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->cells_)
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->processes)
868 if (match_attr(it.second->attributes, arg_memb.substr(2)))
869 sel.selected_members[mod->name].insert(it.first);
870 } else
871 if (arg_memb.substr(0, 2) == "r:") {
872 for (auto &it : mod->cells_)
873 if (match_attr(it.second->parameters, arg_memb.substr(2)))
874 sel.selected_members[mod->name].insert(it.first);
875 } else {
876 if (arg_memb.substr(0, 2) == "n:")
877 arg_memb = arg_memb.substr(2);
878 for (auto &it : mod->wires_)
879 if (match_ids(it.first, arg_memb))
880 sel.selected_members[mod->name].insert(it.first);
881 for (auto &it : mod->memories)
882 if (match_ids(it.first, arg_memb))
883 sel.selected_members[mod->name].insert(it.first);
884 for (auto &it : mod->cells_)
885 if (match_ids(it.first, arg_memb))
886 sel.selected_members[mod->name].insert(it.first);
887 for (auto &it : mod->processes)
888 if (match_ids(it.first, arg_memb))
889 sel.selected_members[mod->name].insert(it.first);
890 }
891 }
892
893 select_filter_active_mod(design, work_stack.back());
894 }
895
896 PRIVATE_NAMESPACE_END
897 YOSYS_NAMESPACE_BEGIN
898
899 // used in kernel/register.cc and maybe other locations, extern decl. in register.h
900 void handle_extra_select_args(Pass *pass, vector<string> args, size_t argidx, size_t args_size, RTLIL::Design *design)
901 {
902 work_stack.clear();
903 for (; argidx < args_size; argidx++) {
904 if (args[argidx].substr(0, 1) == "-") {
905 if (pass != NULL)
906 pass->cmd_error(args, argidx, "Unexpected option in selection arguments.");
907 else
908 log_cmd_error("Unexpected option in selection arguments.");
909 }
910 select_stmt(design, args[argidx]);
911 }
912 while (work_stack.size() > 1) {
913 select_op_union(design, work_stack.front(), work_stack.back());
914 work_stack.pop_back();
915 }
916 if (work_stack.empty())
917 design->selection_stack.push_back(RTLIL::Selection(false));
918 else
919 design->selection_stack.push_back(work_stack.back());
920 }
921
922 // extern decl. in register.h
923 RTLIL::Selection eval_select_args(const vector<string> &args, RTLIL::Design *design)
924 {
925 work_stack.clear();
926 for (auto &arg : args)
927 select_stmt(design, arg);
928 while (work_stack.size() > 1) {
929 select_op_union(design, work_stack.front(), work_stack.back());
930 work_stack.pop_back();
931 }
932 if (work_stack.empty())
933 return RTLIL::Selection(false);
934 return work_stack.back();
935 }
936
937 // extern decl. in register.h
938 void eval_select_op(vector<RTLIL::Selection> &work, const string &op, RTLIL::Design *design)
939 {
940 work_stack.swap(work);
941 select_stmt(design, op);
942 work_stack.swap(work);
943 }
944
945 YOSYS_NAMESPACE_END
946 PRIVATE_NAMESPACE_BEGIN
947
948 struct SelectPass : public Pass {
949 SelectPass() : Pass("select", "modify and view the list of selected objects") { }
950 virtual void help()
951 {
952 // |---v---|---v---|---v---|---v---|---v---|---v---|---v---|---v---|---v---|---v---|
953 log("\n");
954 log(" select [ -add | -del | -set <name> ] {-read <filename> | <selection>}\n");
955 log(" select [ <assert_option> ] {-read <filename> | <selection>}\n");
956 log(" select [ -list | -write <filename> | -count | -clear ]\n");
957 log(" select -module <modname>\n");
958 log("\n");
959 log("Most commands use the list of currently selected objects to determine which part\n");
960 log("of the design to operate on. This command can be used to modify and view this\n");
961 log("list of selected objects.\n");
962 log("\n");
963 log("Note that many commands support an optional [selection] argument that can be\n");
964 log("used to override the global selection for the command. The syntax of this\n");
965 log("optional argument is identical to the syntax of the <selection> argument\n");
966 log("described here.\n");
967 log("\n");
968 log(" -add, -del\n");
969 log(" add or remove the given objects to the current selection.\n");
970 log(" without this options the current selection is replaced.\n");
971 log("\n");
972 log(" -set <name>\n");
973 log(" do not modify the current selection. instead save the new selection\n");
974 log(" under the given name (see @<name> below). to save the current selection,\n");
975 log(" use \"select -set <name> %%\"\n");
976 log("\n");
977 log(" -assert-none\n");
978 log(" do not modify the current selection. instead assert that the given\n");
979 log(" selection is empty. i.e. produce an error if any object matching the\n");
980 log(" selection is found.\n");
981 log("\n");
982 log(" -assert-any\n");
983 log(" do not modify the current selection. instead assert that the given\n");
984 log(" selection is non-empty. i.e. produce an error if no object matching\n");
985 log(" the selection is found.\n");
986 log("\n");
987 log(" -assert-count N\n");
988 log(" do not modify the current selection. instead assert that the given\n");
989 log(" selection contains exactly N objects.\n");
990 log("\n");
991 log(" -assert-max N\n");
992 log(" do not modify the current selection. instead assert that the given\n");
993 log(" selection contains less than or exactly N objects.\n");
994 log("\n");
995 log(" -assert-min N\n");
996 log(" do not modify the current selection. instead assert that the given\n");
997 log(" selection contains at least N objects.\n");
998 log("\n");
999 log(" -list\n");
1000 log(" list all objects in the current selection\n");
1001 log("\n");
1002 log(" -write <filename>\n");
1003 log(" like -list but write the output to the specified file\n");
1004 log("\n");
1005 log(" -read <filename>\n");
1006 log(" read the specified file (written by -write)\n");
1007 log("\n");
1008 log(" -count\n");
1009 log(" count all objects in the current selection\n");
1010 log("\n");
1011 log(" -clear\n");
1012 log(" clear the current selection. this effectively selects the whole\n");
1013 log(" design. it also resets the selected module (see -module). use the\n");
1014 log(" command 'select *' to select everything but stay in the current module.\n");
1015 log("\n");
1016 log(" -none\n");
1017 log(" create an empty selection. the current module is unchanged.\n");
1018 log("\n");
1019 log(" -module <modname>\n");
1020 log(" limit the current scope to the specified module.\n");
1021 log(" the difference between this and simply selecting the module\n");
1022 log(" is that all object names are interpreted relative to this\n");
1023 log(" module after this command until the selection is cleared again.\n");
1024 log("\n");
1025 log("When this command is called without an argument, the current selection\n");
1026 log("is displayed in a compact form (i.e. only the module name when a whole module\n");
1027 log("is selected).\n");
1028 log("\n");
1029 log("The <selection> argument itself is a series of commands for a simple stack\n");
1030 log("machine. Each element on the stack represents a set of selected objects.\n");
1031 log("After this commands have been executed, the union of all remaining sets\n");
1032 log("on the stack is computed and used as selection for the command.\n");
1033 log("\n");
1034 log("Pushing (selecting) object when not in -module mode:\n");
1035 log("\n");
1036 log(" <mod_pattern>\n");
1037 log(" select the specified module(s)\n");
1038 log("\n");
1039 log(" <mod_pattern>/<obj_pattern>\n");
1040 log(" select the specified object(s) from the module(s)\n");
1041 log("\n");
1042 log("Pushing (selecting) object when in -module mode:\n");
1043 log("\n");
1044 log(" <obj_pattern>\n");
1045 log(" select the specified object(s) from the current module\n");
1046 log("\n");
1047 log("A <mod_pattern> can be a module name, wildcard expression (*, ?, [..])\n");
1048 log("matching module names, or one of the following:\n");
1049 log("\n");
1050 log(" A:<pattern>, A:<pattern>=<pattern>\n");
1051 log(" all modules with an attribute matching the given pattern\n");
1052 log(" in addition to = also <, <=, >=, and > are supported\n");
1053 log("\n");
1054 log("An <obj_pattern> can be an object name, wildcard expression, or one of\n");
1055 log("the following:\n");
1056 log("\n");
1057 log(" w:<pattern>\n");
1058 log(" all wires with a name matching the given wildcard pattern\n");
1059 log("\n");
1060 log(" i:<pattern>, o:<pattern>, x:<pattern>\n");
1061 log(" all inputs (i:), outputs (o:) or any ports (x:) with matching names\n");
1062 log("\n");
1063 log(" s:<size>, s:<min>:<max>\n");
1064 log(" all wires with a matching width\n");
1065 log("\n");
1066 log(" m:<pattern>\n");
1067 log(" all memories with a name matching the given pattern\n");
1068 log("\n");
1069 log(" c:<pattern>\n");
1070 log(" all cells with a name matching the given pattern\n");
1071 log("\n");
1072 log(" t:<pattern>\n");
1073 log(" all cells with a type matching the given pattern\n");
1074 log("\n");
1075 log(" p:<pattern>\n");
1076 log(" all processes with a name matching the given pattern\n");
1077 log("\n");
1078 log(" a:<pattern>\n");
1079 log(" all objects with an attribute name matching the given pattern\n");
1080 log("\n");
1081 log(" a:<pattern>=<pattern>\n");
1082 log(" all objects with a matching attribute name-value-pair.\n");
1083 log(" in addition to = also <, <=, >=, and > are supported\n");
1084 log("\n");
1085 log(" r:<pattern>, r:<pattern>=<pattern>\n");
1086 log(" cells with matching parameters. also with <, <=, >= and >.\n");
1087 log("\n");
1088 log(" n:<pattern>\n");
1089 log(" all objects with a name matching the given pattern\n");
1090 log(" (i.e. 'n:' is optional as it is the default matching rule)\n");
1091 log("\n");
1092 log(" @<name>\n");
1093 log(" push the selection saved prior with 'select -set <name> ...'\n");
1094 log("\n");
1095 log("The following actions can be performed on the top sets on the stack:\n");
1096 log("\n");
1097 log(" %%\n");
1098 log(" push a copy of the current selection to the stack\n");
1099 log("\n");
1100 log(" %%%%\n");
1101 log(" replace the stack with a union of all elements on it\n");
1102 log("\n");
1103 log(" %%n\n");
1104 log(" replace top set with its invert\n");
1105 log("\n");
1106 log(" %%u\n");
1107 log(" replace the two top sets on the stack with their union\n");
1108 log("\n");
1109 log(" %%i\n");
1110 log(" replace the two top sets on the stack with their intersection\n");
1111 log("\n");
1112 log(" %%d\n");
1113 log(" pop the top set from the stack and subtract it from the new top\n");
1114 log("\n");
1115 log(" %%D\n");
1116 log(" like %%d but swap the roles of two top sets on the stack\n");
1117 log("\n");
1118 log(" %%c\n");
1119 log(" create a copy of the top set from the stack and push it\n");
1120 log("\n");
1121 log(" %%x[<num1>|*][.<num2>][:<rule>[:<rule>..]]\n");
1122 log(" expand top set <num1> num times according to the specified rules.\n");
1123 log(" (i.e. select all cells connected to selected wires and select all\n");
1124 log(" wires connected to selected cells) The rules specify which cell\n");
1125 log(" ports to use for this. the syntax for a rule is a '-' for exclusion\n");
1126 log(" and a '+' for inclusion, followed by an optional comma separated\n");
1127 log(" list of cell types followed by an optional comma separated list of\n");
1128 log(" cell ports in square brackets. a rule can also be just a cell or wire\n");
1129 log(" name that limits the expansion (is included but does not go beyond).\n");
1130 log(" select at most <num2> objects. a warning message is printed when this\n");
1131 log(" limit is reached. When '*' is used instead of <num1> then the process\n");
1132 log(" is repeated until no further object are selected.\n");
1133 log("\n");
1134 log(" %%ci[<num1>|*][.<num2>][:<rule>[:<rule>..]]\n");
1135 log(" %%co[<num1>|*][.<num2>][:<rule>[:<rule>..]]\n");
1136 log(" similar to %%x, but only select input (%%ci) or output cones (%%co)\n");
1137 log("\n");
1138 log(" %%xe[...] %%cie[...] %%coe\n");
1139 log(" like %%x, %%ci, and %%co but only consider combinatorial cells\n");
1140 log("\n");
1141 log(" %%a\n");
1142 log(" expand top set by selecting all wires that are (at least in part)\n");
1143 log(" aliases for selected wires.\n");
1144 log("\n");
1145 log(" %%s\n");
1146 log(" expand top set by adding all modules that implement cells in selected\n");
1147 log(" modules\n");
1148 log("\n");
1149 log(" %%m\n");
1150 log(" expand top set by selecting all modules that contain selected objects\n");
1151 log("\n");
1152 log(" %%M\n");
1153 log(" select modules that implement selected cells\n");
1154 log("\n");
1155 log(" %%C\n");
1156 log(" select cells that implement selected modules\n");
1157 log("\n");
1158 log(" %%R[<num>]\n");
1159 log(" select <num> random objects from top selection (default 1)\n");
1160 log("\n");
1161 log("Example: the following command selects all wires that are connected to a\n");
1162 log("'GATE' input of a 'SWITCH' cell:\n");
1163 log("\n");
1164 log(" select */t:SWITCH %%x:+[GATE] */t:SWITCH %%d\n");
1165 log("\n");
1166 }
1167 virtual void execute(std::vector<std::string> args, RTLIL::Design *design)
1168 {
1169 bool add_mode = false;
1170 bool del_mode = false;
1171 bool clear_mode = false;
1172 bool none_mode = false;
1173 bool list_mode = false;
1174 bool count_mode = false;
1175 bool got_module = false;
1176 bool assert_none = false;
1177 bool assert_any = false;
1178 int assert_count = -1;
1179 int assert_max = -1;
1180 int assert_min = -1;
1181 std::string write_file, read_file;
1182 std::string set_name, sel_str;
1183
1184 work_stack.clear();
1185
1186 size_t argidx;
1187 for (argidx = 1; argidx < args.size(); argidx++)
1188 {
1189 std::string arg = args[argidx];
1190 if (arg == "-add") {
1191 add_mode = true;
1192 continue;
1193 }
1194 if (arg == "-del") {
1195 del_mode = true;
1196 continue;
1197 }
1198 if (arg == "-assert-none") {
1199 assert_none = true;
1200 continue;
1201 }
1202 if (arg == "-assert-any") {
1203 assert_any = true;
1204 continue;
1205 }
1206 if (arg == "-assert-count" && argidx+1 < args.size()) {
1207 assert_count = atoi(args[++argidx].c_str());
1208 continue;
1209 }
1210 if (arg == "-assert-max" && argidx+1 < args.size()) {
1211 assert_max = atoi(args[++argidx].c_str());
1212 continue;
1213 }
1214 if (arg == "-assert-min" && argidx+1 < args.size()) {
1215 assert_min = atoi(args[++argidx].c_str());
1216 continue;
1217 }
1218 if (arg == "-clear") {
1219 clear_mode = true;
1220 continue;
1221 }
1222 if (arg == "-none") {
1223 none_mode = true;
1224 continue;
1225 }
1226 if (arg == "-list") {
1227 list_mode = true;
1228 continue;
1229 }
1230 if (arg == "-write" && argidx+1 < args.size()) {
1231 write_file = args[++argidx];
1232 continue;
1233 }
1234 if (arg == "-read" && argidx+1 < args.size()) {
1235 read_file = args[++argidx];
1236 continue;
1237 }
1238 if (arg == "-count") {
1239 count_mode = true;
1240 continue;
1241 }
1242 if (arg == "-module" && argidx+1 < args.size()) {
1243 RTLIL::IdString mod_name = RTLIL::escape_id(args[++argidx]);
1244 if (design->modules_.count(mod_name) == 0)
1245 log_cmd_error("No such module: %s\n", id2cstr(mod_name));
1246 design->selected_active_module = mod_name.str();
1247 got_module = true;
1248 continue;
1249 }
1250 if (arg == "-set" && argidx+1 < args.size()) {
1251 set_name = RTLIL::escape_id(args[++argidx]);
1252 continue;
1253 }
1254 if (arg.size() > 0 && arg[0] == '-')
1255 log_cmd_error("Unknown option %s.\n", arg.c_str());
1256 select_stmt(design, arg);
1257 sel_str += " " + arg;
1258 }
1259
1260 if (!read_file.empty())
1261 {
1262 if (!sel_str.empty())
1263 log_cmd_error("Option -read can not be combined with a selection expression.\n");
1264
1265 std::ifstream f(read_file);
1266 if (f.fail())
1267 log_error("Can't open '%s' for reading: %s\n", read_file.c_str(), strerror(errno));
1268
1269 RTLIL::Selection sel(false);
1270 string line;
1271
1272 while (std::getline(f, line)) {
1273 size_t slash_pos = line.find('/');
1274 if (slash_pos == string::npos) {
1275 log_warning("Ignoring line without slash in 'select -read': %s\n", line.c_str());
1276 continue;
1277 }
1278 IdString mod_name = RTLIL::escape_id(line.substr(0, slash_pos));
1279 IdString obj_name = RTLIL::escape_id(line.substr(slash_pos+1));
1280 sel.selected_members[mod_name].insert(obj_name);
1281 }
1282
1283 select_filter_active_mod(design, sel);
1284 sel.optimize(design);
1285 work_stack.push_back(sel);
1286 }
1287
1288 if (clear_mode && args.size() != 2)
1289 log_cmd_error("Option -clear can not be combined with any other options.\n");
1290
1291 if (none_mode && args.size() != 2)
1292 log_cmd_error("Option -none can not be combined with any other options.\n");
1293
1294 if (add_mode + del_mode + assert_none + assert_any + (assert_count >= 0) + (assert_max >= 0) + (assert_min >= 0) > 1)
1295 log_cmd_error("Options -add, -del, -assert-none, -assert-any, assert-count, -assert-max or -assert-min can not be combined.\n");
1296
1297 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))
1298 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");
1299
1300 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))
1301 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");
1302
1303 if (work_stack.size() == 0 && got_module) {
1304 RTLIL::Selection sel;
1305 select_filter_active_mod(design, sel);
1306 work_stack.push_back(sel);
1307 }
1308
1309 while (work_stack.size() > 1) {
1310 select_op_union(design, work_stack.front(), work_stack.back());
1311 work_stack.pop_back();
1312 }
1313
1314 log_assert(design->selection_stack.size() > 0);
1315
1316 if (clear_mode) {
1317 design->selection_stack.back() = RTLIL::Selection(true);
1318 design->selected_active_module = std::string();
1319 return;
1320 }
1321
1322 if (none_mode) {
1323 design->selection_stack.back() = RTLIL::Selection(false);
1324 return;
1325 }
1326
1327 if (list_mode || count_mode || !write_file.empty())
1328 {
1329 #define LOG_OBJECT(...) { if (list_mode) log(__VA_ARGS__); if (f != NULL) fprintf(f, __VA_ARGS__); total_count++; }
1330 int total_count = 0;
1331 FILE *f = NULL;
1332 if (!write_file.empty()) {
1333 f = fopen(write_file.c_str(), "w");
1334 if (f == NULL)
1335 log_error("Can't open '%s' for writing: %s\n", write_file.c_str(), strerror(errno));
1336 }
1337 RTLIL::Selection *sel = &design->selection_stack.back();
1338 if (work_stack.size() > 0)
1339 sel = &work_stack.back();
1340 sel->optimize(design);
1341 for (auto mod_it : design->modules_)
1342 {
1343 if (sel->selected_whole_module(mod_it.first) && list_mode)
1344 log("%s\n", id2cstr(mod_it.first));
1345 if (sel->selected_module(mod_it.first)) {
1346 for (auto &it : mod_it.second->wires_)
1347 if (sel->selected_member(mod_it.first, it.first))
1348 LOG_OBJECT("%s/%s\n", id2cstr(mod_it.first), id2cstr(it.first))
1349 for (auto &it : mod_it.second->memories)
1350 if (sel->selected_member(mod_it.first, it.first))
1351 LOG_OBJECT("%s/%s\n", id2cstr(mod_it.first), id2cstr(it.first))
1352 for (auto &it : mod_it.second->cells_)
1353 if (sel->selected_member(mod_it.first, it.first))
1354 LOG_OBJECT("%s/%s\n", id2cstr(mod_it.first), id2cstr(it.first))
1355 for (auto &it : mod_it.second->processes)
1356 if (sel->selected_member(mod_it.first, it.first))
1357 LOG_OBJECT("%s/%s\n", id2cstr(mod_it.first), id2cstr(it.first))
1358 }
1359 }
1360 if (count_mode)
1361 log("%d objects.\n", total_count);
1362 if (f != NULL)
1363 fclose(f);
1364 #undef LOG_OBJECT
1365 return;
1366 }
1367
1368 if (add_mode)
1369 {
1370 if (work_stack.size() == 0)
1371 log_cmd_error("Nothing to add to selection.\n");
1372 select_op_union(design, design->selection_stack.back(), work_stack.back());
1373 design->selection_stack.back().optimize(design);
1374 return;
1375 }
1376
1377 if (del_mode)
1378 {
1379 if (work_stack.size() == 0)
1380 log_cmd_error("Nothing to delete from selection.\n");
1381 select_op_diff(design, design->selection_stack.back(), work_stack.back());
1382 design->selection_stack.back().optimize(design);
1383 return;
1384 }
1385
1386 if (assert_none)
1387 {
1388 if (work_stack.size() == 0)
1389 log_cmd_error("No selection to check.\n");
1390 work_stack.back().optimize(design);
1391 if (!work_stack.back().empty())
1392 log_error("Assertion failed: selection is not empty:%s\n", sel_str.c_str());
1393 return;
1394 }
1395
1396 if (assert_any)
1397 {
1398 if (work_stack.size() == 0)
1399 log_cmd_error("No selection to check.\n");
1400 work_stack.back().optimize(design);
1401 if (work_stack.back().empty())
1402 log_error("Assertion failed: selection is empty:%s\n", sel_str.c_str());
1403 return;
1404 }
1405
1406 if (assert_count >= 0 || assert_max >= 0 || assert_min >= 0)
1407 {
1408 int total_count = 0;
1409 if (work_stack.size() == 0)
1410 log_cmd_error("No selection to check.\n");
1411 RTLIL::Selection *sel = &work_stack.back();
1412 sel->optimize(design);
1413 for (auto mod_it : design->modules_)
1414 if (sel->selected_module(mod_it.first)) {
1415 for (auto &it : mod_it.second->wires_)
1416 if (sel->selected_member(mod_it.first, it.first))
1417 total_count++;
1418 for (auto &it : mod_it.second->memories)
1419 if (sel->selected_member(mod_it.first, it.first))
1420 total_count++;
1421 for (auto &it : mod_it.second->cells_)
1422 if (sel->selected_member(mod_it.first, it.first))
1423 total_count++;
1424 for (auto &it : mod_it.second->processes)
1425 if (sel->selected_member(mod_it.first, it.first))
1426 total_count++;
1427 }
1428 if (assert_count >= 0 && assert_count != total_count)
1429 log_error("Assertion failed: selection contains %d elements instead of the asserted %d:%s\n",
1430 total_count, assert_count, sel_str.c_str());
1431 if (assert_max >= 0 && assert_max < total_count)
1432 log_error("Assertion failed: selection contains %d elements, more than the maximum number %d:%s\n",
1433 total_count, assert_max, sel_str.c_str());
1434 if (assert_min >= 0 && assert_min > total_count)
1435 log_error("Assertion failed: selection contains %d elements, less than the minimum number %d:%s\n",
1436 total_count, assert_min, sel_str.c_str());
1437 return;
1438 }
1439
1440 if (!set_name.empty())
1441 {
1442 if (work_stack.size() == 0)
1443 design->selection_vars[set_name] = RTLIL::Selection(false);
1444 else
1445 design->selection_vars[set_name] = work_stack.back();
1446 return;
1447 }
1448
1449 if (work_stack.size() == 0) {
1450 RTLIL::Selection &sel = design->selection_stack.back();
1451 if (sel.full_selection)
1452 log("*\n");
1453 for (auto &it : sel.selected_modules)
1454 log("%s\n", id2cstr(it));
1455 for (auto &it : sel.selected_members)
1456 for (auto &it2 : it.second)
1457 log("%s/%s\n", id2cstr(it.first), id2cstr(it2));
1458 return;
1459 }
1460
1461 design->selection_stack.back() = work_stack.back();
1462 design->selection_stack.back().optimize(design);
1463 }
1464 } SelectPass;
1465
1466 struct CdPass : public Pass {
1467 CdPass() : Pass("cd", "a shortcut for 'select -module <name>'") { }
1468 virtual void help()
1469 {
1470 // |---v---|---v---|---v---|---v---|---v---|---v---|---v---|---v---|---v---|---v---|
1471 log("\n");
1472 log(" cd <modname>\n");
1473 log("\n");
1474 log("This is just a shortcut for 'select -module <modname>'.\n");
1475 log("\n");
1476 log("\n");
1477 log(" cd <cellname>\n");
1478 log("\n");
1479 log("When no module with the specified name is found, but there is a cell\n");
1480 log("with the specified name in the current module, then this is equivalent\n");
1481 log("to 'cd <celltype>'.\n");
1482 log("\n");
1483 log(" cd ..\n");
1484 log("\n");
1485 log("This is just a shortcut for 'select -clear'.\n");
1486 log("\n");
1487 }
1488 virtual void execute(std::vector<std::string> args, RTLIL::Design *design)
1489 {
1490 if (args.size() != 2)
1491 log_cmd_error("Invalid number of arguments.\n");
1492
1493 if (args[1] == "..") {
1494 design->selection_stack.back() = RTLIL::Selection(true);
1495 design->selected_active_module = std::string();
1496 return;
1497 }
1498
1499 std::string modname = RTLIL::escape_id(args[1]);
1500
1501 if (design->modules_.count(modname) == 0 && !design->selected_active_module.empty()) {
1502 RTLIL::Module *module = NULL;
1503 if (design->modules_.count(design->selected_active_module) > 0)
1504 module = design->modules_.at(design->selected_active_module);
1505 if (module != NULL && module->cells_.count(modname) > 0)
1506 modname = module->cells_.at(modname)->type.str();
1507 }
1508
1509 if (design->modules_.count(modname) > 0) {
1510 design->selected_active_module = modname;
1511 design->selection_stack.back() = RTLIL::Selection();
1512 select_filter_active_mod(design, design->selection_stack.back());
1513 design->selection_stack.back().optimize(design);
1514 return;
1515 }
1516
1517 log_cmd_error("No such module `%s' found!\n", RTLIL::unescape_id(modname).c_str());
1518 }
1519 } CdPass;
1520
1521 template<typename T>
1522 static void log_matches(const char *title, Module *module, T list)
1523 {
1524 std::vector<IdString> matches;
1525
1526 for (auto &it : list)
1527 if (module->selected(it.second))
1528 matches.push_back(it.first);
1529
1530 if (!matches.empty()) {
1531 log("\n%d %s:\n", int(matches.size()), title);
1532 std::sort(matches.begin(), matches.end(), RTLIL::sort_by_id_str());
1533 for (auto id : matches)
1534 log(" %s\n", RTLIL::id2cstr(id));
1535 }
1536 }
1537
1538 struct LsPass : public Pass {
1539 LsPass() : Pass("ls", "list modules or objects in modules") { }
1540 virtual void help()
1541 {
1542 // |---v---|---v---|---v---|---v---|---v---|---v---|---v---|---v---|---v---|---v---|
1543 log("\n");
1544 log(" ls [selection]\n");
1545 log("\n");
1546 log("When no active module is selected, this prints a list of modules.\n");
1547 log("\n");
1548 log("When an active module is selected, this prints a list of objects in the module.\n");
1549 log("\n");
1550 }
1551 virtual void execute(std::vector<std::string> args, RTLIL::Design *design)
1552 {
1553 size_t argidx = 1;
1554 extra_args(args, argidx, design);
1555
1556 if (design->selected_active_module.empty())
1557 {
1558 std::vector<IdString> matches;
1559
1560 for (auto mod : design->selected_modules())
1561 matches.push_back(mod->name);
1562
1563 if (!matches.empty()) {
1564 log("\n%d %s:\n", int(matches.size()), "modules");
1565 std::sort(matches.begin(), matches.end(), RTLIL::sort_by_id_str());
1566 for (auto id : matches)
1567 log(" %s%s\n", log_id(id), design->selected_whole_module(design->module(id)) ? "" : "*");
1568 }
1569 }
1570 else
1571 if (design->module(design->selected_active_module) != nullptr)
1572 {
1573 RTLIL::Module *module = design->module(design->selected_active_module);
1574 log_matches("wires", module, module->wires_);
1575 log_matches("memories", module, module->memories);
1576 log_matches("cells", module, module->cells_);
1577 log_matches("processes", module, module->processes);
1578 }
1579 }
1580 } LsPass;
1581
1582 PRIVATE_NAMESPACE_END