2 * yosys -- Yosys Open SYnthesis Suite
4 * Copyright (C) 2012 Clifford Wolf <clifford@clifford.at>
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.
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.
20 #include "kernel/yosys.h"
21 #include "kernel/celltypes.h"
22 #include "kernel/sigtools.h"
27 PRIVATE_NAMESPACE_BEGIN
31 static std::vector
<RTLIL::Selection
> work_stack
;
33 static bool match_ids(RTLIL::IdString id
, std::string pattern
)
37 if (id
.size() > 0 && id
[0] == '\\' && id
.substr(1) == pattern
)
39 if (patmatch(pattern
.c_str(), id
.c_str()))
41 if (id
.size() > 0 && id
[0] == '\\' && patmatch(pattern
.c_str(), id
.substr(1).c_str()))
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
, '$');
52 static bool match_attr_val(const RTLIL::Const
&value
, std::string pattern
, char match_op
)
57 if ((value
.flags
& RTLIL::CONST_FLAG_STRING
) == 0)
59 RTLIL::SigSpec sig_value
;
61 if (!RTLIL::SigSpec::parse(sig_value
, NULL
, pattern
))
64 RTLIL::Const pattern_value
= sig_value
.as_const();
67 return value
== pattern_value
;
69 return value
!= pattern_value
;
71 return value
.as_int() < pattern_value
.as_int();
73 return value
.as_int() > pattern_value
.as_int();
75 return value
.as_int() <= pattern_value
.as_int();
77 return value
.as_int() >= pattern_value
.as_int();
81 std::string value_str
= value
.decode_string();
84 if (patmatch(pattern
.c_str(), value
.decode_string().c_str()))
88 return value_str
== pattern
;
90 return value_str
!= pattern
;
92 return value_str
< pattern
;
94 return value_str
> pattern
;
96 return value_str
<= pattern
;
98 return value_str
>= pattern
;
104 static bool match_attr(const dict
<RTLIL::IdString
, RTLIL::Const
> &attributes
, std::string name_pat
, std::string value_pat
, char match_op
)
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
))
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
))
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
))
116 if (attributes
.count("\\" + name_pat
) && match_attr_val(attributes
.at("\\" + name_pat
), value_pat
, match_op
))
122 static bool match_attr(const dict
<RTLIL::IdString
, RTLIL::Const
> &attributes
, std::string match_expr
)
124 size_t pos
= match_expr
.find_first_of("<!=>");
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
]);
136 return match_attr(attributes
, match_expr
, std::string(), 0);
139 static void select_op_neg(RTLIL::Design
*design
, RTLIL::Selection
&lhs
)
141 if (lhs
.full_selection
) {
142 lhs
.full_selection
= false;
143 lhs
.selected_modules
.clear();
144 lhs
.selected_members
.clear();
148 if (lhs
.selected_modules
.size() == 0 && lhs
.selected_members
.size() == 0) {
149 lhs
.full_selection
= true;
153 RTLIL::Selection
new_sel(false);
155 for (auto &mod_it
: design
->modules_
)
157 if (lhs
.selected_whole_module(mod_it
.first
))
159 if (!lhs
.selected_module(mod_it
.first
)) {
160 new_sel
.selected_modules
.insert(mod_it
.first
);
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
);
179 lhs
.selected_modules
.swap(new_sel
.selected_modules
);
180 lhs
.selected_members
.swap(new_sel
.selected_members
);
183 static int my_xorshift32_rng() {
184 static uint32_t x32
= 314159265;
188 return x32
& 0x0fffffff;
191 static void select_op_random(RTLIL::Design
*design
, RTLIL::Selection
&lhs
, int count
)
193 vector
<pair
<IdString
, IdString
>> objects
;
195 for (auto mod
: design
->modules())
197 if (!lhs
.selected_module(mod
->name
))
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
));
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
));
211 lhs
= RTLIL::Selection(false);
213 while (!objects
.empty() && count
-- > 0)
215 int idx
= my_xorshift32_rng() % GetSize(objects
);
216 lhs
.selected_members
[objects
[idx
].first
].insert(objects
[idx
].second
);
217 objects
[idx
] = objects
.back();
221 lhs
.optimize(design
);
224 static void select_op_submod(RTLIL::Design
*design
, RTLIL::Selection
&lhs
)
226 for (auto &mod_it
: design
->modules_
)
228 if (lhs
.selected_whole_module(mod_it
.first
))
230 for (auto &cell_it
: mod_it
.second
->cells_
)
232 if (design
->modules_
.count(cell_it
.second
->type
) == 0)
234 lhs
.selected_modules
.insert(cell_it
.second
->type
);
240 static void select_op_cells_to_modules(RTLIL::Design
*design
, RTLIL::Selection
&lhs
)
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
);
251 static void select_op_module_to_cells(RTLIL::Design
*design
, RTLIL::Selection
&lhs
)
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
);
261 static void select_op_fullmod(RTLIL::Design
*design
, RTLIL::Selection
&lhs
)
263 lhs
.optimize(design
);
264 for (auto &it
: lhs
.selected_members
)
265 lhs
.selected_modules
.insert(it
.first
);
266 lhs
.selected_members
.clear();
269 static void select_op_alias(RTLIL::Design
*design
, RTLIL::Selection
&lhs
)
271 for (auto &mod_it
: design
->modules_
)
273 if (lhs
.selected_whole_module(mod_it
.first
))
275 if (!lhs
.selected_module(mod_it
.first
))
278 SigMap
sigmap(mod_it
.second
);
279 SigPool selected_bits
;
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
));
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
);
291 static void select_op_union(RTLIL::Design
*, RTLIL::Selection
&lhs
, const RTLIL::Selection
&rhs
)
293 if (rhs
.full_selection
) {
294 lhs
.full_selection
= true;
295 lhs
.selected_modules
.clear();
296 lhs
.selected_members
.clear();
300 if (lhs
.full_selection
)
303 for (auto &it
: rhs
.selected_members
)
304 for (auto &it2
: it
.second
)
305 lhs
.selected_members
[it
.first
].insert(it2
);
307 for (auto &it
: rhs
.selected_modules
) {
308 lhs
.selected_modules
.insert(it
);
309 lhs
.selected_members
.erase(it
);
313 static void select_op_diff(RTLIL::Design
*design
, RTLIL::Selection
&lhs
, const RTLIL::Selection
&rhs
)
315 if (rhs
.full_selection
) {
316 lhs
.full_selection
= false;
317 lhs
.selected_modules
.clear();
318 lhs
.selected_members
.clear();
322 if (lhs
.full_selection
) {
323 if (!rhs
.full_selection
&& rhs
.selected_modules
.size() == 0 && rhs
.selected_members
.size() == 0)
325 lhs
.full_selection
= false;
326 for (auto &it
: design
->modules_
)
327 lhs
.selected_modules
.insert(it
.first
);
330 for (auto &it
: rhs
.selected_modules
) {
331 lhs
.selected_modules
.erase(it
);
332 lhs
.selected_members
.erase(it
);
335 for (auto &it
: rhs
.selected_members
)
337 if (design
->modules_
.count(it
.first
) == 0)
340 RTLIL::Module
*mod
= design
->modules_
[it
.first
];
342 if (lhs
.selected_modules
.count(mod
->name
) > 0)
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
);
355 if (lhs
.selected_members
.count(mod
->name
) == 0)
358 for (auto &it2
: it
.second
)
359 lhs
.selected_members
[mod
->name
].erase(it2
);
363 static void select_op_intersect(RTLIL::Design
*design
, RTLIL::Selection
&lhs
, const RTLIL::Selection
&rhs
)
365 if (rhs
.full_selection
)
368 if (lhs
.full_selection
) {
369 lhs
.full_selection
= false;
370 for (auto &it
: design
->modules_
)
371 lhs
.selected_modules
.insert(it
.first
);
374 std::vector
<RTLIL::IdString
> del_list
;
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
);
383 for (auto &it
: del_list
)
384 lhs
.selected_modules
.erase(it
);
387 for (auto &it
: lhs
.selected_members
) {
388 if (rhs
.selected_modules
.count(it
.first
) > 0)
390 if (rhs
.selected_members
.count(it
.first
) == 0) {
391 del_list
.push_back(it
.first
);
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
);
403 for (auto &it
: del_list
)
404 lhs
.selected_members
.erase(it
);
408 struct expand_rule_t
{
410 std::set
<RTLIL::IdString
> cell_types
, port_names
;
414 static int parse_comma_list(std::set
<RTLIL::IdString
> &tokens
, std::string str
, size_t pos
, std::string stopchar
)
418 size_t endpos
= str
.find_first_of(stopchar
, pos
);
419 if (endpos
== std::string::npos
)
422 tokens
.insert(RTLIL::escape_id(str
.substr(pos
, endpos
-pos
)));
424 if (pos
== str
.size() || str
[pos
] != ',')
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
)
433 bool is_input
, is_output
;
434 for (auto &mod_it
: design
->modules_
)
436 if (lhs
.selected_whole_module(mod_it
.first
) || !lhs
.selected_module(mod_it
.first
))
439 RTLIL::Module
*mod
= mod_it
.second
;
440 std::set
<RTLIL::Wire
*> selected_wires
;
441 auto selected_members
= lhs
.selected_members
[mod
->name
];
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
);
447 for (auto &conn
: mod
->connections())
449 std::vector
<RTLIL::SigBit
> conn_lhs
= conn
.first
.to_sigbit_vector();
450 std::vector
<RTLIL::SigBit
> conn_rhs
= conn
.second
.to_sigbit_vector();
452 for (size_t i
= 0; i
< conn_lhs
.size(); i
++) {
453 if (conn_lhs
[i
].wire
== NULL
|| conn_rhs
[i
].wire
== NULL
)
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
--;
462 for (auto &cell
: mod
->cells_
)
463 for (auto &conn
: cell
.second
->connections())
465 char last_mode
= '-';
466 if (eval_only
&& !yosys_celltypes
.cell_evaluable(cell
.second
->type
))
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)
472 if (rule
.port_names
.size() > 0 && rule
.port_names
.count(conn
.first
) == 0)
474 if (rule
.mode
== '+')
479 if (last_mode
== '+')
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
--;
500 static void select_op_expand(RTLIL::Design
*design
, std::string arg
, char mode
, bool eval_only
)
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
;
512 if (pos
< int(arg
.size()) && arg
[pos
] == '*') {
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
)
520 levels
= atoi(arg
.substr(pos
, endpos
-pos
).c_str());
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
)
528 if (int(endpos
) > pos
)
529 rem_objects
= atoi(arg
.substr(pos
, endpos
-pos
).c_str());
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());
537 if (arg
[pos
] == '+' || arg
[pos
] == '-') {
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
] == ']')
546 rules
.push_back(rule
);
548 size_t endpos
= arg
.find(':', pos
);
549 if (endpos
== std::string::npos
)
551 if (int(endpos
) > pos
) {
552 std::string str
= arg
.substr(pos
, endpos
-pos
);
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
)
560 log_cmd_error("Selection %s is not defined!\n", RTLIL::unescape_id(str
).c_str());
562 limits
.insert(RTLIL::escape_id(str
));
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) {
574 for (auto &it
: rule
.cell_types
)
575 log(" %s", it
.c_str());
578 if (rule
.port_names
.size() > 0) {
580 for (auto &it
: rule
.port_names
)
581 log(" %s", it
.c_str());
585 if (limits
.size() > 0) {
587 for (auto &it
: limits
)
588 log(" %s", it
.c_str());
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)
597 rem_objects
-= num_objects
;
600 if (rem_objects
== 0)
601 log_warning("reached configured limit at `%s'.\n", arg
.c_str());
604 static void select_filter_active_mod(RTLIL::Design
*design
, RTLIL::Selection
&sel
)
606 if (design
->selected_active_module
.empty())
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
);
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
);
630 static void select_stmt(RTLIL::Design
*design
, std::string arg
)
632 std::string arg_mod
, arg_memb
;
639 if (design
->selection_stack
.size() > 0)
640 work_stack
.push_back(design
->selection_stack
.back());
643 while (work_stack
.size() > 1) {
644 select_op_union(design
, work_stack
.front(), work_stack
.back());
645 work_stack
.pop_back();
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]);
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();
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();
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();
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();
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
);
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]);
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]);
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]);
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());
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]);
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]);
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);
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);
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);
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);
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);
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);
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());
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
]);
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());
760 if (!design
->selected_active_module
.empty()) {
761 arg_mod
= design
->selected_active_module
;
764 size_t pos
= arg
.find('/');
765 if (pos
== std::string::npos
) {
766 if (arg
.find(':') == std::string::npos
|| arg
.substr(0, 1) == "A")
769 arg_mod
= "*", arg_memb
= arg
;
771 arg_mod
= arg
.substr(0, pos
);
772 arg_memb
= arg
.substr(pos
+1);
776 work_stack
.push_back(RTLIL::Selection());
777 RTLIL::Selection
&sel
= work_stack
.back();
779 if (arg
== "*" && arg_mod
== "*") {
780 select_filter_active_mod(design
, work_stack
.back());
784 sel
.full_selection
= false;
785 for (auto &mod_it
: design
->modules_
)
787 if (arg_mod
.substr(0, 2) == "A:") {
788 if (!match_attr(mod_it
.second
->attributes
, arg_mod
.substr(2)))
791 if (!match_ids(mod_it
.first
, arg_mod
))
794 if (arg_memb
== "") {
795 sel
.selected_modules
.insert(mod_it
.first
);
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
);
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
);
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
);
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
);
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
);
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
);
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
);
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
);
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
);
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
);
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
);
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
);
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
);
893 select_filter_active_mod(design
, work_stack
.back());
896 PRIVATE_NAMESPACE_END
897 YOSYS_NAMESPACE_BEGIN
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
)
903 for (; argidx
< args_size
; argidx
++) {
904 if (args
[argidx
].substr(0, 1) == "-") {
906 pass
->cmd_error(args
, argidx
, "Unexpected option in selection arguments.");
908 log_cmd_error("Unexpected option in selection arguments.");
910 select_stmt(design
, args
[argidx
]);
912 while (work_stack
.size() > 1) {
913 select_op_union(design
, work_stack
.front(), work_stack
.back());
914 work_stack
.pop_back();
916 if (work_stack
.empty())
917 design
->selection_stack
.push_back(RTLIL::Selection(false));
919 design
->selection_stack
.push_back(work_stack
.back());
922 // extern decl. in register.h
923 RTLIL::Selection
eval_select_args(const vector
<string
> &args
, RTLIL::Design
*design
)
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();
932 if (work_stack
.empty())
933 return RTLIL::Selection(false);
934 return work_stack
.back();
937 // extern decl. in register.h
938 void eval_select_op(vector
<RTLIL::Selection
> &work
, const string
&op
, RTLIL::Design
*design
)
940 work_stack
.swap(work
);
941 select_stmt(design
, op
);
942 work_stack
.swap(work
);
946 PRIVATE_NAMESPACE_BEGIN
948 struct SelectPass
: public Pass
{
949 SelectPass() : Pass("select", "modify and view the list of selected objects") { }
952 // |---v---|---v---|---v---|---v---|---v---|---v---|---v---|---v---|---v---|---v---|
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");
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");
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");
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");
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");
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");
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");
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");
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");
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");
1000 log(" list all objects in the current selection\n");
1002 log(" -write <filename>\n");
1003 log(" like -list but write the output to the specified file\n");
1005 log(" -read <filename>\n");
1006 log(" read the specified file (written by -write)\n");
1009 log(" count all objects in the current selection\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");
1017 log(" create an empty selection. the current module is unchanged.\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");
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");
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");
1034 log("Pushing (selecting) object when not in -module mode:\n");
1036 log(" <mod_pattern>\n");
1037 log(" select the specified module(s)\n");
1039 log(" <mod_pattern>/<obj_pattern>\n");
1040 log(" select the specified object(s) from the module(s)\n");
1042 log("Pushing (selecting) object when in -module mode:\n");
1044 log(" <obj_pattern>\n");
1045 log(" select the specified object(s) from the current module\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");
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");
1054 log("An <obj_pattern> can be an object name, wildcard expression, or one of\n");
1055 log("the following:\n");
1057 log(" w:<pattern>\n");
1058 log(" all wires with a name matching the given wildcard pattern\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");
1063 log(" s:<size>, s:<min>:<max>\n");
1064 log(" all wires with a matching width\n");
1066 log(" m:<pattern>\n");
1067 log(" all memories with a name matching the given pattern\n");
1069 log(" c:<pattern>\n");
1070 log(" all cells with a name matching the given pattern\n");
1072 log(" t:<pattern>\n");
1073 log(" all cells with a type matching the given pattern\n");
1075 log(" p:<pattern>\n");
1076 log(" all processes with a name matching the given pattern\n");
1078 log(" a:<pattern>\n");
1079 log(" all objects with an attribute name matching the given pattern\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");
1085 log(" r:<pattern>, r:<pattern>=<pattern>\n");
1086 log(" cells with matching parameters. also with <, <=, >= and >.\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");
1093 log(" push the selection saved prior with 'select -set <name> ...'\n");
1095 log("The following actions can be performed on the top sets on the stack:\n");
1098 log(" push a copy of the current selection to the stack\n");
1101 log(" replace the stack with a union of all elements on it\n");
1104 log(" replace top set with its invert\n");
1107 log(" replace the two top sets on the stack with their union\n");
1110 log(" replace the two top sets on the stack with their intersection\n");
1113 log(" pop the top set from the stack and subtract it from the new top\n");
1116 log(" like %%d but swap the roles of two top sets on the stack\n");
1119 log(" create a copy of the top set from the stack and push it\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");
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");
1138 log(" %%xe[...] %%cie[...] %%coe\n");
1139 log(" like %%x, %%ci, and %%co but only consider combinatorial cells\n");
1142 log(" expand top set by selecting all wires that are (at least in part)\n");
1143 log(" aliases for selected wires.\n");
1146 log(" expand top set by adding all modules that implement cells in selected\n");
1150 log(" expand top set by selecting all modules that contain selected objects\n");
1153 log(" select modules that implement selected cells\n");
1156 log(" select cells that implement selected modules\n");
1158 log(" %%R[<num>]\n");
1159 log(" select <num> random objects from top selection (default 1)\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");
1164 log(" select */t:SWITCH %%x:+[GATE] */t:SWITCH %%d\n");
1167 virtual void execute(std::vector
<std::string
> args
, RTLIL::Design
*design
)
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
;
1187 for (argidx
= 1; argidx
< args
.size(); argidx
++)
1189 std::string arg
= args
[argidx
];
1190 if (arg
== "-add") {
1194 if (arg
== "-del") {
1198 if (arg
== "-assert-none") {
1202 if (arg
== "-assert-any") {
1206 if (arg
== "-assert-count" && argidx
+1 < args
.size()) {
1207 assert_count
= atoi(args
[++argidx
].c_str());
1210 if (arg
== "-assert-max" && argidx
+1 < args
.size()) {
1211 assert_max
= atoi(args
[++argidx
].c_str());
1214 if (arg
== "-assert-min" && argidx
+1 < args
.size()) {
1215 assert_min
= atoi(args
[++argidx
].c_str());
1218 if (arg
== "-clear") {
1222 if (arg
== "-none") {
1226 if (arg
== "-list") {
1230 if (arg
== "-write" && argidx
+1 < args
.size()) {
1231 write_file
= args
[++argidx
];
1234 if (arg
== "-read" && argidx
+1 < args
.size()) {
1235 read_file
= args
[++argidx
];
1238 if (arg
== "-count") {
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();
1250 if (arg
== "-set" && argidx
+1 < args
.size()) {
1251 set_name
= RTLIL::escape_id(args
[++argidx
]);
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
;
1260 if (!read_file
.empty())
1262 if (!sel_str
.empty())
1263 log_cmd_error("Option -read can not be combined with a selection expression.\n");
1265 std::ifstream
f(read_file
);
1267 log_error("Can't open '%s' for reading: %s\n", read_file
.c_str(), strerror(errno
));
1269 RTLIL::Selection
sel(false);
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());
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
);
1283 select_filter_active_mod(design
, sel
);
1284 sel
.optimize(design
);
1285 work_stack
.push_back(sel
);
1288 if (clear_mode
&& args
.size() != 2)
1289 log_cmd_error("Option -clear can not be combined with any other options.\n");
1291 if (none_mode
&& args
.size() != 2)
1292 log_cmd_error("Option -none can not be combined with any other options.\n");
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");
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");
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");
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
);
1309 while (work_stack
.size() > 1) {
1310 select_op_union(design
, work_stack
.front(), work_stack
.back());
1311 work_stack
.pop_back();
1314 log_assert(design
->selection_stack
.size() > 0);
1317 design
->selection_stack
.back() = RTLIL::Selection(true);
1318 design
->selected_active_module
= std::string();
1323 design
->selection_stack
.back() = RTLIL::Selection(false);
1327 if (list_mode
|| count_mode
|| !write_file
.empty())
1329 #define LOG_OBJECT(...) { if (list_mode) log(__VA_ARGS__); if (f != NULL) fprintf(f, __VA_ARGS__); total_count++; }
1330 int total_count
= 0;
1332 if (!write_file
.empty()) {
1333 f
= fopen(write_file
.c_str(), "w");
1335 log_error("Can't open '%s' for writing: %s\n", write_file
.c_str(), strerror(errno
));
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_
)
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
))
1361 log("%d objects.\n", total_count
);
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
);
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
);
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());
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());
1406 if (assert_count
>= 0 || assert_max
>= 0 || assert_min
>= 0)
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
))
1418 for (auto &it
: mod_it
.second
->memories
)
1419 if (sel
->selected_member(mod_it
.first
, it
.first
))
1421 for (auto &it
: mod_it
.second
->cells_
)
1422 if (sel
->selected_member(mod_it
.first
, it
.first
))
1424 for (auto &it
: mod_it
.second
->processes
)
1425 if (sel
->selected_member(mod_it
.first
, it
.first
))
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());
1440 if (!set_name
.empty())
1442 if (work_stack
.size() == 0)
1443 design
->selection_vars
[set_name
] = RTLIL::Selection(false);
1445 design
->selection_vars
[set_name
] = work_stack
.back();
1449 if (work_stack
.size() == 0) {
1450 RTLIL::Selection
&sel
= design
->selection_stack
.back();
1451 if (sel
.full_selection
)
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
));
1461 design
->selection_stack
.back() = work_stack
.back();
1462 design
->selection_stack
.back().optimize(design
);
1466 struct CdPass
: public Pass
{
1467 CdPass() : Pass("cd", "a shortcut for 'select -module <name>'") { }
1470 // |---v---|---v---|---v---|---v---|---v---|---v---|---v---|---v---|---v---|---v---|
1472 log(" cd <modname>\n");
1474 log("This is just a shortcut for 'select -module <modname>'.\n");
1477 log(" cd <cellname>\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");
1485 log("This is just a shortcut for 'select -clear'.\n");
1488 virtual void execute(std::vector
<std::string
> args
, RTLIL::Design
*design
)
1490 if (args
.size() != 2)
1491 log_cmd_error("Invalid number of arguments.\n");
1493 if (args
[1] == "..") {
1494 design
->selection_stack
.back() = RTLIL::Selection(true);
1495 design
->selected_active_module
= std::string();
1499 std::string modname
= RTLIL::escape_id(args
[1]);
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();
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
);
1517 log_cmd_error("No such module `%s' found!\n", RTLIL::unescape_id(modname
).c_str());
1521 template<typename T
>
1522 static void log_matches(const char *title
, Module
*module
, T list
)
1524 std::vector
<IdString
> matches
;
1526 for (auto &it
: list
)
1527 if (module
->selected(it
.second
))
1528 matches
.push_back(it
.first
);
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
));
1538 struct LsPass
: public Pass
{
1539 LsPass() : Pass("ls", "list modules or objects in modules") { }
1542 // |---v---|---v---|---v---|---v---|---v---|---v---|---v---|---v---|---v---|---v---|
1544 log(" ls [selection]\n");
1546 log("When no active module is selected, this prints a list of modules.\n");
1548 log("When an active module is selected, this prints a list of objects in the module.\n");
1551 virtual void execute(std::vector
<std::string
> args
, RTLIL::Design
*design
)
1554 extra_args(args
, argidx
, design
);
1556 if (design
->selected_active_module
.empty())
1558 std::vector
<IdString
> matches
;
1560 for (auto mod
: design
->selected_modules())
1561 matches
.push_back(mod
->name
);
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
)) ? "" : "*");
1571 if (design
->module(design
->selected_active_module
) != nullptr)
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
);
1582 PRIVATE_NAMESPACE_END