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
.compare(1, std::string::npos
, pattern
.c_str()) == 0)
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
, nullptr, 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
.compare(pos
, 2, "!=") == 0)
128 return match_attr(attributes
, match_expr
.substr(0, pos
), match_expr
.substr(pos
+2), '!');
129 if (match_expr
.compare(pos
, 2, "<=") == 0)
130 return match_attr(attributes
, match_expr
.substr(0, pos
), match_expr
.substr(pos
+2), '[');
131 if (match_expr
.compare(pos
, 2, ">=") == 0)
132 return match_attr(attributes
, match_expr
.substr(0, pos
), match_expr
.substr(pos
+2), ']');
133 return match_attr(attributes
, match_expr
.substr(0, pos
), match_expr
.substr(pos
+1), match_expr
[pos
]);
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
: design
->modules())
157 if (lhs
.selected_whole_module(mod
->name
))
159 if (!lhs
.selected_module(mod
->name
)) {
160 new_sel
.selected_modules
.insert(mod
->name
);
164 for (auto wire
: mod
->wires())
165 if (!lhs
.selected_member(mod
->name
, wire
->name
))
166 new_sel
.selected_members
[mod
->name
].insert(wire
->name
);
167 for (auto &it
: mod
->memories
)
168 if (!lhs
.selected_member(mod
->name
, it
.first
))
169 new_sel
.selected_members
[mod
->name
].insert(it
.first
);
170 for (auto cell
: mod
->cells())
171 if (!lhs
.selected_member(mod
->name
, cell
->name
))
172 new_sel
.selected_members
[mod
->name
].insert(cell
->name
);
173 for (auto &it
: mod
->processes
)
174 if (!lhs
.selected_member(mod
->name
, it
.first
))
175 new_sel
.selected_members
[mod
->name
].insert(it
.first
);
178 lhs
.selected_modules
.swap(new_sel
.selected_modules
);
179 lhs
.selected_members
.swap(new_sel
.selected_members
);
182 static int my_xorshift32_rng() {
183 static uint32_t x32
= 314159265;
187 return x32
& 0x0fffffff;
190 static void select_op_random(RTLIL::Design
*design
, RTLIL::Selection
&lhs
, int count
)
192 vector
<pair
<IdString
, IdString
>> objects
;
194 for (auto mod
: design
->modules())
196 if (!lhs
.selected_module(mod
->name
))
199 for (auto cell
: mod
->cells()) {
200 if (lhs
.selected_member(mod
->name
, cell
->name
))
201 objects
.push_back(make_pair(mod
->name
, cell
->name
));
204 for (auto wire
: mod
->wires()) {
205 if (lhs
.selected_member(mod
->name
, wire
->name
))
206 objects
.push_back(make_pair(mod
->name
, wire
->name
));
210 lhs
= RTLIL::Selection(false);
212 while (!objects
.empty() && count
-- > 0)
214 int idx
= my_xorshift32_rng() % GetSize(objects
);
215 lhs
.selected_members
[objects
[idx
].first
].insert(objects
[idx
].second
);
216 objects
[idx
] = objects
.back();
220 lhs
.optimize(design
);
223 static void select_op_submod(RTLIL::Design
*design
, RTLIL::Selection
&lhs
)
225 for (auto mod
: design
->modules())
227 if (lhs
.selected_whole_module(mod
->name
))
229 for (auto cell
: mod
->cells())
231 if (design
->module(cell
->type
) == nullptr)
233 lhs
.selected_modules
.insert(cell
->type
);
239 static void select_op_cells_to_modules(RTLIL::Design
*design
, RTLIL::Selection
&lhs
)
241 RTLIL::Selection
new_sel(false);
242 for (auto mod
: design
->modules())
243 if (lhs
.selected_module(mod
->name
))
244 for (auto cell
: mod
->cells())
245 if (lhs
.selected_member(mod
->name
, cell
->name
) && (design
->module(cell
->type
) != nullptr))
246 new_sel
.selected_modules
.insert(cell
->type
);
250 static void select_op_module_to_cells(RTLIL::Design
*design
, RTLIL::Selection
&lhs
)
252 RTLIL::Selection
new_sel(false);
253 for (auto mod
: design
->modules())
254 for (auto cell
: mod
->cells())
255 if ((design
->module(cell
->type
) != nullptr) && lhs
.selected_whole_module(cell
->type
))
256 new_sel
.selected_members
[mod
->name
].insert(cell
->name
);
260 static void select_op_fullmod(RTLIL::Design
*design
, RTLIL::Selection
&lhs
)
262 lhs
.optimize(design
);
263 for (auto &it
: lhs
.selected_members
)
264 lhs
.selected_modules
.insert(it
.first
);
265 lhs
.selected_members
.clear();
268 static void select_op_alias(RTLIL::Design
*design
, RTLIL::Selection
&lhs
)
270 for (auto mod
: design
->modules())
272 if (lhs
.selected_whole_module(mod
->name
))
274 if (!lhs
.selected_module(mod
->name
))
278 SigPool selected_bits
;
280 for (auto wire
: mod
->wires())
281 if (lhs
.selected_member(mod
->name
, wire
->name
))
282 selected_bits
.add(sigmap(wire
));
284 for (auto wire
: mod
->wires())
285 if (!lhs
.selected_member(mod
->name
, wire
->name
) && selected_bits
.check_any(sigmap(wire
)))
286 lhs
.selected_members
[mod
->name
].insert(wire
->name
);
290 static void select_op_union(RTLIL::Design
*, RTLIL::Selection
&lhs
, const RTLIL::Selection
&rhs
)
292 if (rhs
.full_selection
) {
293 lhs
.full_selection
= true;
294 lhs
.selected_modules
.clear();
295 lhs
.selected_members
.clear();
299 if (lhs
.full_selection
)
302 for (auto &it
: rhs
.selected_members
)
303 for (auto &it2
: it
.second
)
304 lhs
.selected_members
[it
.first
].insert(it2
);
306 for (auto &it
: rhs
.selected_modules
) {
307 lhs
.selected_modules
.insert(it
);
308 lhs
.selected_members
.erase(it
);
312 static void select_op_diff(RTLIL::Design
*design
, RTLIL::Selection
&lhs
, const RTLIL::Selection
&rhs
)
314 if (rhs
.full_selection
) {
315 lhs
.full_selection
= false;
316 lhs
.selected_modules
.clear();
317 lhs
.selected_members
.clear();
321 if (lhs
.full_selection
) {
322 if (!rhs
.full_selection
&& rhs
.selected_modules
.size() == 0 && rhs
.selected_members
.size() == 0)
324 lhs
.full_selection
= false;
325 for (auto mod
: design
->modules())
326 lhs
.selected_modules
.insert(mod
->name
);
329 for (auto &it
: rhs
.selected_modules
) {
330 lhs
.selected_modules
.erase(it
);
331 lhs
.selected_members
.erase(it
);
334 for (auto &it
: rhs
.selected_members
)
336 if (design
->module(it
.first
) == nullptr)
339 RTLIL::Module
*mod
= design
->module(it
.first
);
341 if (lhs
.selected_modules
.count(mod
->name
) > 0)
343 for (auto wire
: mod
->wires())
344 lhs
.selected_members
[mod
->name
].insert(wire
->name
);
345 for (auto &it
: mod
->memories
)
346 lhs
.selected_members
[mod
->name
].insert(it
.first
);
347 for (auto cell
: mod
->cells())
348 lhs
.selected_members
[mod
->name
].insert(cell
->name
);
349 for (auto &it
: mod
->processes
)
350 lhs
.selected_members
[mod
->name
].insert(it
.first
);
351 lhs
.selected_modules
.erase(mod
->name
);
354 if (lhs
.selected_members
.count(mod
->name
) == 0)
357 for (auto &it2
: it
.second
)
358 lhs
.selected_members
[mod
->name
].erase(it2
);
362 static void select_op_intersect(RTLIL::Design
*design
, RTLIL::Selection
&lhs
, const RTLIL::Selection
&rhs
)
364 if (rhs
.full_selection
)
367 if (lhs
.full_selection
) {
368 lhs
.full_selection
= false;
369 for (auto mod
: design
->modules())
370 lhs
.selected_modules
.insert(mod
->name
);
373 std::vector
<RTLIL::IdString
> del_list
;
375 for (auto &it
: lhs
.selected_modules
)
376 if (rhs
.selected_modules
.count(it
) == 0) {
377 if (rhs
.selected_members
.count(it
) > 0)
378 for (auto &it2
: rhs
.selected_members
.at(it
))
379 lhs
.selected_members
[it
].insert(it2
);
380 del_list
.push_back(it
);
382 for (auto &it
: del_list
)
383 lhs
.selected_modules
.erase(it
);
386 for (auto &it
: lhs
.selected_members
) {
387 if (rhs
.selected_modules
.count(it
.first
) > 0)
389 if (rhs
.selected_members
.count(it
.first
) == 0) {
390 del_list
.push_back(it
.first
);
393 std::vector
<RTLIL::IdString
> del_list2
;
394 for (auto &it2
: it
.second
)
395 if (rhs
.selected_members
.at(it
.first
).count(it2
) == 0)
396 del_list2
.push_back(it2
);
397 for (auto &it2
: del_list2
)
398 it
.second
.erase(it2
);
399 if (it
.second
.size() == 0)
400 del_list
.push_back(it
.first
);
402 for (auto &it
: del_list
)
403 lhs
.selected_members
.erase(it
);
407 struct expand_rule_t
{
409 std::set
<RTLIL::IdString
> cell_types
, port_names
;
413 static int parse_comma_list(std::set
<RTLIL::IdString
> &tokens
, std::string str
, size_t pos
, std::string stopchar
)
417 size_t endpos
= str
.find_first_of(stopchar
, pos
);
418 if (endpos
== std::string::npos
)
421 tokens
.insert(RTLIL::escape_id(str
.substr(pos
, endpos
-pos
)));
423 if (pos
== str
.size() || str
[pos
] != ',')
429 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
)
432 bool is_input
, is_output
;
433 for (auto mod
: design
->modules())
435 if (lhs
.selected_whole_module(mod
->name
) || !lhs
.selected_module(mod
->name
))
438 std::set
<RTLIL::Wire
*> selected_wires
;
439 auto selected_members
= lhs
.selected_members
[mod
->name
];
441 for (auto wire
: mod
->wires())
442 if (lhs
.selected_member(mod
->name
, wire
->name
) && limits
.count(wire
->name
) == 0)
443 selected_wires
.insert(wire
);
445 for (auto &conn
: mod
->connections())
447 std::vector
<RTLIL::SigBit
> conn_lhs
= conn
.first
.to_sigbit_vector();
448 std::vector
<RTLIL::SigBit
> conn_rhs
= conn
.second
.to_sigbit_vector();
450 for (size_t i
= 0; i
< conn_lhs
.size(); i
++) {
451 if (conn_lhs
[i
].wire
== nullptr || conn_rhs
[i
].wire
== nullptr)
453 if (mode
!= 'i' && selected_wires
.count(conn_rhs
[i
].wire
) && selected_members
.count(conn_lhs
[i
].wire
->name
) == 0)
454 lhs
.selected_members
[mod
->name
].insert(conn_lhs
[i
].wire
->name
), sel_objects
++, max_objects
--;
455 if (mode
!= 'o' && selected_wires
.count(conn_lhs
[i
].wire
) && selected_members
.count(conn_rhs
[i
].wire
->name
) == 0)
456 lhs
.selected_members
[mod
->name
].insert(conn_rhs
[i
].wire
->name
), sel_objects
++, max_objects
--;
460 for (auto cell
: mod
->cells())
461 for (auto &conn
: cell
->connections())
463 char last_mode
= '-';
464 if (eval_only
&& !yosys_celltypes
.cell_evaluable(cell
->type
))
466 for (auto &rule
: rules
) {
467 last_mode
= rule
.mode
;
468 if (rule
.cell_types
.size() > 0 && rule
.cell_types
.count(cell
->type
) == 0)
470 if (rule
.port_names
.size() > 0 && rule
.port_names
.count(conn
.first
) == 0)
472 if (rule
.mode
== '+')
477 if (last_mode
== '+')
480 is_input
= mode
== 'x' || ct
.cell_input(cell
->type
, conn
.first
);
481 is_output
= mode
== 'x' || ct
.cell_output(cell
->type
, conn
.first
);
482 for (auto &chunk
: conn
.second
.chunks())
483 if (chunk
.wire
!= nullptr) {
484 if (max_objects
!= 0 && selected_wires
.count(chunk
.wire
) > 0 && selected_members
.count(cell
->name
) == 0)
485 if (mode
== 'x' || (mode
== 'i' && is_output
) || (mode
== 'o' && is_input
))
486 lhs
.selected_members
[mod
->name
].insert(cell
->name
), sel_objects
++, max_objects
--;
487 if (max_objects
!= 0 && selected_members
.count(cell
->name
) > 0 && limits
.count(cell
->name
) == 0 && selected_members
.count(chunk
.wire
->name
) == 0)
488 if (mode
== 'x' || (mode
== 'i' && is_input
) || (mode
== 'o' && is_output
))
489 lhs
.selected_members
[mod
->name
].insert(chunk
.wire
->name
), sel_objects
++, max_objects
--;
498 static void select_op_expand(RTLIL::Design
*design
, std::string arg
, char mode
, bool eval_only
)
500 int pos
= (mode
== 'x' ? 2 : 3) + (eval_only
? 1 : 0);
501 int levels
= 1, rem_objects
= -1;
502 std::vector
<expand_rule_t
> rules
;
503 std::set
<RTLIL::IdString
> limits
;
510 if (pos
< int(arg
.size()) && arg
[pos
] == '*') {
514 if (pos
< int(arg
.size()) && '0' <= arg
[pos
] && arg
[pos
] <= '9') {
515 size_t endpos
= arg
.find_first_not_of("0123456789", pos
);
516 if (endpos
== std::string::npos
)
518 levels
= atoi(arg
.substr(pos
, endpos
-pos
).c_str());
522 if (pos
< int(arg
.size()) && arg
[pos
] == '.') {
523 size_t endpos
= arg
.find_first_not_of("0123456789", ++pos
);
524 if (endpos
== std::string::npos
)
526 if (int(endpos
) > pos
)
527 rem_objects
= atoi(arg
.substr(pos
, endpos
-pos
).c_str());
531 while (pos
< int(arg
.size())) {
532 if (arg
[pos
] != ':' || pos
+1 == int(arg
.size()))
533 log_cmd_error("Syntax error in expand operator '%s'.\n", arg
.c_str());
535 if (arg
[pos
] == '+' || arg
[pos
] == '-') {
537 rule
.mode
= arg
[pos
++];
538 pos
= parse_comma_list(rule
.cell_types
, arg
, pos
, "[:");
539 if (pos
< int(arg
.size()) && arg
[pos
] == '[') {
540 pos
= parse_comma_list(rule
.port_names
, arg
, pos
+1, "]:");
541 if (pos
< int(arg
.size()) && arg
[pos
] == ']')
544 rules
.push_back(rule
);
546 size_t endpos
= arg
.find(':', pos
);
547 if (endpos
== std::string::npos
)
549 if (int(endpos
) > pos
) {
550 std::string str
= arg
.substr(pos
, endpos
-pos
);
552 str
= RTLIL::escape_id(str
.substr(1));
553 if (design
->selection_vars
.count(str
) > 0) {
554 for (auto i1
: design
->selection_vars
.at(str
).selected_members
)
555 for (auto i2
: i1
.second
)
558 log_cmd_error("Selection %s is not defined!\n", RTLIL::unescape_id(str
).c_str());
560 limits
.insert(RTLIL::escape_id(str
));
567 log("expand by %d levels (max. %d objects):\n", levels
, rem_objects
);
568 for (auto &rule
: rules
) {
569 log(" rule (%c):\n", rule
.mode
);
570 if (rule
.cell_types
.size() > 0) {
572 for (auto &it
: rule
.cell_types
)
573 log(" %s", it
.c_str());
576 if (rule
.port_names
.size() > 0) {
578 for (auto &it
: rule
.port_names
)
579 log(" %s", it
.c_str());
583 if (limits
.size() > 0) {
585 for (auto &it
: limits
)
586 log(" %s", it
.c_str());
591 while (levels
-- > 0 && rem_objects
!= 0) {
592 int num_objects
= select_op_expand(design
, work_stack
.back(), rules
, limits
, rem_objects
, mode
, ct
, eval_only
);
593 if (num_objects
== 0)
595 rem_objects
-= num_objects
;
598 if (rem_objects
== 0)
599 log_warning("reached configured limit at `%s'.\n", arg
.c_str());
602 static void select_filter_active_mod(RTLIL::Design
*design
, RTLIL::Selection
&sel
)
604 if (design
->selected_active_module
.empty())
607 if (sel
.full_selection
) {
608 sel
.full_selection
= false;
609 sel
.selected_modules
.clear();
610 sel
.selected_members
.clear();
611 sel
.selected_modules
.insert(design
->selected_active_module
);
615 std::vector
<RTLIL::IdString
> del_list
;
616 for (auto mod_name
: sel
.selected_modules
)
617 if (mod_name
!= design
->selected_active_module
)
618 del_list
.push_back(mod_name
);
619 for (auto &it
: sel
.selected_members
)
620 if (it
.first
!= design
->selected_active_module
)
621 del_list
.push_back(it
.first
);
622 for (auto mod_name
: del_list
) {
623 sel
.selected_modules
.erase(mod_name
);
624 sel
.selected_members
.erase(mod_name
);
628 static void select_stmt(RTLIL::Design
*design
, std::string arg
, bool disable_empty_warning
= false)
630 std::string arg_mod
, arg_memb
;
631 std::unordered_map
<std::string
, bool> arg_mod_found
;
632 std::unordered_map
<std::string
, bool> arg_memb_found
;
634 auto isprefixed
= [](const string
&s
) {
635 return GetSize(s
) >= 2 && ((s
[0] >= 'a' && s
[0] <= 'z') || (s
[0] >= 'A' && s
[0] <= 'Z')) && s
[1] == ':';
643 if (design
->selection_stack
.size() > 0)
644 work_stack
.push_back(design
->selection_stack
.back());
647 while (work_stack
.size() > 1) {
648 select_op_union(design
, work_stack
.front(), work_stack
.back());
649 work_stack
.pop_back();
653 if (work_stack
.size() < 1)
654 log_cmd_error("Must have at least one element on the stack for operator %%n.\n");
655 select_op_neg(design
, work_stack
[work_stack
.size()-1]);
658 if (work_stack
.size() < 2)
659 log_cmd_error("Must have at least two elements on the stack for operator %%u.\n");
660 select_op_union(design
, work_stack
[work_stack
.size()-2], work_stack
[work_stack
.size()-1]);
661 work_stack
.pop_back();
664 if (work_stack
.size() < 2)
665 log_cmd_error("Must have at least two elements on the stack for operator %%d.\n");
666 select_op_diff(design
, work_stack
[work_stack
.size()-2], work_stack
[work_stack
.size()-1]);
667 work_stack
.pop_back();
670 if (work_stack
.size() < 2)
671 log_cmd_error("Must have at least two elements on the stack for operator %%D.\n");
672 select_op_diff(design
, work_stack
[work_stack
.size()-1], work_stack
[work_stack
.size()-2]);
673 work_stack
[work_stack
.size()-2] = work_stack
[work_stack
.size()-1];
674 work_stack
.pop_back();
677 if (work_stack
.size() < 2)
678 log_cmd_error("Must have at least two elements on the stack for operator %%i.\n");
679 select_op_intersect(design
, work_stack
[work_stack
.size()-2], work_stack
[work_stack
.size()-1]);
680 work_stack
.pop_back();
682 if (arg
.size() >= 2 && arg
[0] == '%' && arg
[1] == 'R') {
683 if (work_stack
.size() < 1)
684 log_cmd_error("Must have at least one element on the stack for operator %%R.\n");
685 int count
= arg
.size() > 2 ? atoi(arg
.c_str() + 2) : 1;
686 select_op_random(design
, work_stack
[work_stack
.size()-1], count
);
689 if (work_stack
.size() < 1)
690 log_cmd_error("Must have at least one element on the stack for operator %%s.\n");
691 select_op_submod(design
, work_stack
[work_stack
.size()-1]);
694 if (work_stack
.size() < 1)
695 log_cmd_error("Must have at least one element on the stack for operator %%M.\n");
696 select_op_cells_to_modules(design
, work_stack
[work_stack
.size()-1]);
699 if (work_stack
.size() < 1)
700 log_cmd_error("Must have at least one element on the stack for operator %%C.\n");
701 select_op_module_to_cells(design
, work_stack
[work_stack
.size()-1]);
704 if (work_stack
.size() < 1)
705 log_cmd_error("Must have at least one element on the stack for operator %%c.\n");
706 work_stack
.push_back(work_stack
.back());
709 if (work_stack
.size() < 1)
710 log_cmd_error("Must have at least one element on the stack for operator %%m.\n");
711 select_op_fullmod(design
, work_stack
[work_stack
.size()-1]);
714 if (work_stack
.size() < 1)
715 log_cmd_error("Must have at least one element on the stack for operator %%a.\n");
716 select_op_alias(design
, work_stack
[work_stack
.size()-1]);
718 if (arg
== "%x" || (arg
.size() > 2 && arg
.compare(0, 2, "%x") == 0 && (arg
[2] == ':' || arg
[2] == '*' || arg
[2] == '.' || ('0' <= arg
[2] && arg
[2] <= '9')))) {
719 if (work_stack
.size() < 1)
720 log_cmd_error("Must have at least one element on the stack for operator %%x.\n");
721 select_op_expand(design
, arg
, 'x', false);
723 if (arg
== "%ci" || (arg
.size() > 3 && arg
.compare(0, 3, "%ci") == 0 && (arg
[3] == ':' || arg
[3] == '*' || arg
[3] == '.' || ('0' <= arg
[3] && arg
[3] <= '9')))) {
724 if (work_stack
.size() < 1)
725 log_cmd_error("Must have at least one element on the stack for operator %%ci.\n");
726 select_op_expand(design
, arg
, 'i', false);
728 if (arg
== "%co" || (arg
.size() > 3 && arg
.compare(0, 3, "%co") == 0 && (arg
[3] == ':' || arg
[3] == '*' || arg
[3] == '.' || ('0' <= arg
[3] && arg
[3] <= '9')))) {
729 if (work_stack
.size() < 1)
730 log_cmd_error("Must have at least one element on the stack for operator %%co.\n");
731 select_op_expand(design
, arg
, 'o', false);
733 if (arg
== "%xe" || (arg
.size() > 3 && arg
.compare(0, 3, "%xe") == 0 && (arg
[3] == ':' || arg
[3] == '*' || arg
[3] == '.' || ('0' <= arg
[3] && arg
[3] <= '9')))) {
734 if (work_stack
.size() < 1)
735 log_cmd_error("Must have at least one element on the stack for operator %%xe.\n");
736 select_op_expand(design
, arg
, 'x', true);
738 if (arg
== "%cie" || (arg
.size() > 4 && arg
.compare(0, 4, "%cie") == 0 && (arg
[4] == ':' || arg
[4] == '*' || arg
[4] == '.' || ('0' <= arg
[4] && arg
[4] <= '9')))) {
739 if (work_stack
.size() < 1)
740 log_cmd_error("Must have at least one element on the stack for operator %%cie.\n");
741 select_op_expand(design
, arg
, 'i', true);
743 if (arg
== "%coe" || (arg
.size() > 4 && arg
.compare(0, 4, "%coe") == 0 && (arg
[4] == ':' || arg
[4] == '*' || arg
[4] == '.' || ('0' <= arg
[4] && arg
[4] <= '9')))) {
744 if (work_stack
.size() < 1)
745 log_cmd_error("Must have at least one element on the stack for operator %%coe.\n");
746 select_op_expand(design
, arg
, 'o', true);
748 log_cmd_error("Unknown selection operator '%s'.\n", arg
.c_str());
749 if (work_stack
.size() >= 1)
750 select_filter_active_mod(design
, work_stack
.back());
755 std::string set_name
= RTLIL::escape_id(arg
.substr(1));
756 if (design
->selection_vars
.count(set_name
) > 0)
757 work_stack
.push_back(design
->selection_vars
[set_name
]);
759 log_cmd_error("Selection @%s is not defined!\n", RTLIL::unescape_id(set_name
).c_str());
760 select_filter_active_mod(design
, work_stack
.back());
764 bool select_blackboxes
= false;
765 if (arg
.substr(0, 1) == "=") {
767 select_blackboxes
= true;
770 if (!design
->selected_active_module
.empty()) {
771 arg_mod
= design
->selected_active_module
;
773 if (!isprefixed(arg_memb
))
774 arg_memb_found
[arg_memb
] = false;
776 if (isprefixed(arg
) && arg
[0] >= 'a' && arg
[0] <= 'z') {
777 arg_mod
= "*", arg_memb
= arg
;
779 size_t pos
= arg
.find('/');
780 if (pos
== std::string::npos
) {
782 if (!isprefixed(arg_mod
))
783 arg_mod_found
[arg_mod
] = false;
785 arg_mod
= arg
.substr(0, pos
);
786 if (!isprefixed(arg_mod
))
787 arg_mod_found
[arg_mod
] = false;
788 arg_memb
= arg
.substr(pos
+1);
789 if (!isprefixed(arg_memb
))
790 arg_memb_found
[arg_memb
] = false;
794 work_stack
.push_back(RTLIL::Selection());
795 RTLIL::Selection
&sel
= work_stack
.back();
797 if (arg
== "*" && arg_mod
== "*" && select_blackboxes
) {
798 select_filter_active_mod(design
, work_stack
.back());
802 sel
.full_selection
= false;
803 for (auto mod
: design
->modules())
805 if (!select_blackboxes
&& mod
->get_blackbox_attribute())
808 if (arg_mod
.compare(0, 2, "A:") == 0) {
809 if (!match_attr(mod
->attributes
, arg_mod
.substr(2)))
812 if (arg_mod
.compare(0, 2, "N:") == 0) {
813 if (!match_ids(mod
->name
, arg_mod
.substr(2)))
816 if (!match_ids(mod
->name
, arg_mod
))
819 arg_mod_found
[arg_mod
] = true;
821 if (arg_memb
== "") {
822 sel
.selected_modules
.insert(mod
->name
);
826 if (arg_memb
.compare(0, 2, "w:") == 0) {
827 for (auto wire
: mod
->wires())
828 if (match_ids(wire
->name
, arg_memb
.substr(2)))
829 sel
.selected_members
[mod
->name
].insert(wire
->name
);
831 if (arg_memb
.compare(0, 2, "i:") == 0) {
832 for (auto wire
: mod
->wires())
833 if (wire
->port_input
&& match_ids(wire
->name
, arg_memb
.substr(2)))
834 sel
.selected_members
[mod
->name
].insert(wire
->name
);
836 if (arg_memb
.compare(0, 2, "o:") == 0) {
837 for (auto wire
: mod
->wires())
838 if (wire
->port_output
&& match_ids(wire
->name
, arg_memb
.substr(2)))
839 sel
.selected_members
[mod
->name
].insert(wire
->name
);
841 if (arg_memb
.compare(0, 2, "x:") == 0) {
842 for (auto wire
: mod
->wires())
843 if ((wire
->port_input
|| wire
->port_output
) && match_ids(wire
->name
, arg_memb
.substr(2)))
844 sel
.selected_members
[mod
->name
].insert(wire
->name
);
846 if (arg_memb
.compare(0, 2, "s:") == 0) {
847 size_t delim
= arg_memb
.substr(2).find(':');
848 if (delim
== std::string::npos
) {
849 int width
= atoi(arg_memb
.substr(2).c_str());
850 for (auto wire
: mod
->wires())
851 if (wire
->width
== width
)
852 sel
.selected_members
[mod
->name
].insert(wire
->name
);
854 std::string min_str
= arg_memb
.substr(2, delim
);
855 std::string max_str
= arg_memb
.substr(2+delim
+1);
856 int min_width
= min_str
.empty() ? 0 : atoi(min_str
.c_str());
857 int max_width
= max_str
.empty() ? -1 : atoi(max_str
.c_str());
858 for (auto wire
: mod
->wires())
859 if (min_width
<= wire
->width
&& (wire
->width
<= max_width
|| max_width
== -1))
860 sel
.selected_members
[mod
->name
].insert(wire
->name
);
863 if (arg_memb
.compare(0, 2, "m:") == 0) {
864 for (auto &it
: mod
->memories
)
865 if (match_ids(it
.first
, arg_memb
.substr(2)))
866 sel
.selected_members
[mod
->name
].insert(it
.first
);
868 if (arg_memb
.compare(0, 2, "c:") == 0) {
869 for (auto cell
: mod
->cells())
870 if (match_ids(cell
->name
, arg_memb
.substr(2)))
871 sel
.selected_members
[mod
->name
].insert(cell
->name
);
873 if (arg_memb
.compare(0, 2, "t:") == 0) {
874 for (auto cell
: mod
->cells())
875 if (match_ids(cell
->type
, arg_memb
.substr(2)))
876 sel
.selected_members
[mod
->name
].insert(cell
->name
);
878 if (arg_memb
.compare(0, 2, "p:") == 0) {
879 for (auto &it
: mod
->processes
)
880 if (match_ids(it
.first
, arg_memb
.substr(2)))
881 sel
.selected_members
[mod
->name
].insert(it
.first
);
883 if (arg_memb
.compare(0, 2, "a:") == 0) {
884 for (auto wire
: mod
->wires())
885 if (match_attr(wire
->attributes
, arg_memb
.substr(2)))
886 sel
.selected_members
[mod
->name
].insert(wire
->name
);
887 for (auto &it
: mod
->memories
)
888 if (match_attr(it
.second
->attributes
, arg_memb
.substr(2)))
889 sel
.selected_members
[mod
->name
].insert(it
.first
);
890 for (auto cell
: mod
->cells())
891 if (match_attr(cell
->attributes
, arg_memb
.substr(2)))
892 sel
.selected_members
[mod
->name
].insert(cell
->name
);
893 for (auto &it
: mod
->processes
)
894 if (match_attr(it
.second
->attributes
, arg_memb
.substr(2)))
895 sel
.selected_members
[mod
->name
].insert(it
.first
);
897 if (arg_memb
.compare(0, 2, "r:") == 0) {
898 for (auto cell
: mod
->cells())
899 if (match_attr(cell
->parameters
, arg_memb
.substr(2)))
900 sel
.selected_members
[mod
->name
].insert(cell
->name
);
902 std::string orig_arg_memb
= arg_memb
;
903 if (arg_memb
.compare(0, 2, "n:") == 0)
904 arg_memb
= arg_memb
.substr(2);
905 for (auto wire
: mod
->wires())
906 if (match_ids(wire
->name
, arg_memb
)) {
907 sel
.selected_members
[mod
->name
].insert(wire
->name
);
908 arg_memb_found
[orig_arg_memb
] = true;
910 for (auto &it
: mod
->memories
)
911 if (match_ids(it
.first
, arg_memb
)) {
912 sel
.selected_members
[mod
->name
].insert(it
.first
);
913 arg_memb_found
[orig_arg_memb
] = true;
915 for (auto cell
: mod
->cells())
916 if (match_ids(cell
->name
, arg_memb
)) {
917 sel
.selected_members
[mod
->name
].insert(cell
->name
);
918 arg_memb_found
[orig_arg_memb
] = true;
920 for (auto &it
: mod
->processes
)
921 if (match_ids(it
.first
, arg_memb
)) {
922 sel
.selected_members
[mod
->name
].insert(it
.first
);
923 arg_memb_found
[orig_arg_memb
] = true;
928 select_filter_active_mod(design
, work_stack
.back());
930 for (auto &it
: arg_mod_found
) {
931 if (it
.second
== false && !disable_empty_warning
) {
932 log_warning("Selection \"%s\" did not match any module.\n", it
.first
.c_str());
935 for (auto &it
: arg_memb_found
) {
936 if (it
.second
== false && !disable_empty_warning
) {
937 log_warning("Selection \"%s\" did not match any object.\n", it
.first
.c_str());
942 static std::string
describe_selection_for_assert(RTLIL::Design
*design
, RTLIL::Selection
*sel
)
944 std::string desc
= "Selection contains:\n";
945 for (auto mod
: design
->modules())
947 if (sel
->selected_module(mod
->name
)) {
948 for (auto wire
: mod
->wires())
949 if (sel
->selected_member(mod
->name
, wire
->name
))
950 desc
+= stringf("%s/%s\n", id2cstr(mod
->name
), id2cstr(wire
->name
));
951 for (auto &it
: mod
->memories
)
952 if (sel
->selected_member(mod
->name
, it
.first
))
953 desc
+= stringf("%s/%s\n", id2cstr(mod
->name
), id2cstr(it
.first
));
954 for (auto cell
: mod
->cells())
955 if (sel
->selected_member(mod
->name
, cell
->name
))
956 desc
+= stringf("%s/%s\n", id2cstr(mod
->name
), id2cstr(cell
->name
));
957 for (auto &it
: mod
->processes
)
958 if (sel
->selected_member(mod
->name
, it
.first
))
959 desc
+= stringf("%s/%s\n", id2cstr(mod
->name
), id2cstr(it
.first
));
965 PRIVATE_NAMESPACE_END
966 YOSYS_NAMESPACE_BEGIN
968 // used in kernel/register.cc and maybe other locations, extern decl. in register.h
969 void handle_extra_select_args(Pass
*pass
, vector
<string
> args
, size_t argidx
, size_t args_size
, RTLIL::Design
*design
)
972 for (; argidx
< args_size
; argidx
++) {
973 if (args
[argidx
].compare(0, 1, "-") == 0) {
975 pass
->cmd_error(args
, argidx
, "Unexpected option in selection arguments.");
977 log_cmd_error("Unexpected option in selection arguments.");
979 select_stmt(design
, args
[argidx
]);
981 while (work_stack
.size() > 1) {
982 select_op_union(design
, work_stack
.front(), work_stack
.back());
983 work_stack
.pop_back();
985 if (work_stack
.empty())
986 design
->selection_stack
.push_back(RTLIL::Selection(false));
988 design
->selection_stack
.push_back(work_stack
.back());
991 // extern decl. in register.h
992 RTLIL::Selection
eval_select_args(const vector
<string
> &args
, RTLIL::Design
*design
)
995 for (auto &arg
: args
)
996 select_stmt(design
, arg
);
997 while (work_stack
.size() > 1) {
998 select_op_union(design
, work_stack
.front(), work_stack
.back());
999 work_stack
.pop_back();
1001 if (work_stack
.empty())
1002 return RTLIL::Selection(false);
1003 return work_stack
.back();
1006 // extern decl. in register.h
1007 void eval_select_op(vector
<RTLIL::Selection
> &work
, const string
&op
, RTLIL::Design
*design
)
1009 work_stack
.swap(work
);
1010 select_stmt(design
, op
);
1011 work_stack
.swap(work
);
1015 PRIVATE_NAMESPACE_BEGIN
1017 struct SelectPass
: public Pass
{
1018 SelectPass() : Pass("select", "modify and view the list of selected objects") { }
1019 void help() YS_OVERRIDE
1021 // |---v---|---v---|---v---|---v---|---v---|---v---|---v---|---v---|---v---|---v---|
1023 log(" select [ -add | -del | -set <name> ] {-read <filename> | <selection>}\n");
1024 log(" select [ <assert_option> ] {-read <filename> | <selection>}\n");
1025 log(" select [ -list | -write <filename> | -count | -clear ]\n");
1026 log(" select -module <modname>\n");
1028 log("Most commands use the list of currently selected objects to determine which part\n");
1029 log("of the design to operate on. This command can be used to modify and view this\n");
1030 log("list of selected objects.\n");
1032 log("Note that many commands support an optional [selection] argument that can be\n");
1033 log("used to override the global selection for the command. The syntax of this\n");
1034 log("optional argument is identical to the syntax of the <selection> argument\n");
1035 log("described here.\n");
1037 log(" -add, -del\n");
1038 log(" add or remove the given objects to the current selection.\n");
1039 log(" without this options the current selection is replaced.\n");
1041 log(" -set <name>\n");
1042 log(" do not modify the current selection. instead save the new selection\n");
1043 log(" under the given name (see @<name> below). to save the current selection,\n");
1044 log(" use \"select -set <name> %%\"\n");
1046 log(" -assert-none\n");
1047 log(" do not modify the current selection. instead assert that the given\n");
1048 log(" selection is empty. i.e. produce an error if any object matching the\n");
1049 log(" selection is found.\n");
1051 log(" -assert-any\n");
1052 log(" do not modify the current selection. instead assert that the given\n");
1053 log(" selection is non-empty. i.e. produce an error if no object matching\n");
1054 log(" the selection is found.\n");
1056 log(" -assert-count N\n");
1057 log(" do not modify the current selection. instead assert that the given\n");
1058 log(" selection contains exactly N objects.\n");
1060 log(" -assert-max N\n");
1061 log(" do not modify the current selection. instead assert that the given\n");
1062 log(" selection contains less than or exactly N objects.\n");
1064 log(" -assert-min N\n");
1065 log(" do not modify the current selection. instead assert that the given\n");
1066 log(" selection contains at least N objects.\n");
1069 log(" list all objects in the current selection\n");
1071 log(" -write <filename>\n");
1072 log(" like -list but write the output to the specified file\n");
1074 log(" -read <filename>\n");
1075 log(" read the specified file (written by -write)\n");
1078 log(" count all objects in the current selection\n");
1081 log(" clear the current selection. this effectively selects the whole\n");
1082 log(" design. it also resets the selected module (see -module). use the\n");
1083 log(" command 'select *' to select everything but stay in the current module.\n");
1086 log(" create an empty selection. the current module is unchanged.\n");
1088 log(" -module <modname>\n");
1089 log(" limit the current scope to the specified module.\n");
1090 log(" the difference between this and simply selecting the module\n");
1091 log(" is that all object names are interpreted relative to this\n");
1092 log(" module after this command until the selection is cleared again.\n");
1094 log("When this command is called without an argument, the current selection\n");
1095 log("is displayed in a compact form (i.e. only the module name when a whole module\n");
1096 log("is selected).\n");
1098 log("The <selection> argument itself is a series of commands for a simple stack\n");
1099 log("machine. Each element on the stack represents a set of selected objects.\n");
1100 log("After this commands have been executed, the union of all remaining sets\n");
1101 log("on the stack is computed and used as selection for the command.\n");
1103 log("Pushing (selecting) object when not in -module mode:\n");
1105 log(" <mod_pattern>\n");
1106 log(" select the specified module(s)\n");
1108 log(" <mod_pattern>/<obj_pattern>\n");
1109 log(" select the specified object(s) from the module(s)\n");
1111 log("Pushing (selecting) object when in -module mode:\n");
1113 log(" <obj_pattern>\n");
1114 log(" select the specified object(s) from the current module\n");
1116 log("Prefix the following patterns with '=' if the pattern should match black-/\n");
1117 log("white-box modules and their contents.\n");
1119 log("A <mod_pattern> can be a module name, wildcard expression (*, ?, [..])\n");
1120 log("matching module names, or one of the following:\n");
1122 log(" A:<pattern>, A:<pattern>=<pattern>\n");
1123 log(" all modules with an attribute matching the given pattern\n");
1124 log(" in addition to = also <, <=, >=, and > are supported\n");
1126 log(" N:<pattern>\n");
1127 log(" all modules with a name matching the given pattern\n");
1128 log(" (i.e. 'N:' is optional as it is the default matching rule)\n");
1130 log("An <obj_pattern> can be an object name, wildcard expression, or one of\n");
1131 log("the following:\n");
1133 log(" w:<pattern>\n");
1134 log(" all wires with a name matching the given wildcard pattern\n");
1136 log(" i:<pattern>, o:<pattern>, x:<pattern>\n");
1137 log(" all inputs (i:), outputs (o:) or any ports (x:) with matching names\n");
1139 log(" s:<size>, s:<min>:<max>\n");
1140 log(" all wires with a matching width\n");
1142 log(" m:<pattern>\n");
1143 log(" all memories with a name matching the given pattern\n");
1145 log(" c:<pattern>\n");
1146 log(" all cells with a name matching the given pattern\n");
1148 log(" t:<pattern>\n");
1149 log(" all cells with a type matching the given pattern\n");
1151 log(" p:<pattern>\n");
1152 log(" all processes with a name matching the given pattern\n");
1154 log(" a:<pattern>\n");
1155 log(" all objects with an attribute name matching the given pattern\n");
1157 log(" a:<pattern>=<pattern>\n");
1158 log(" all objects with a matching attribute name-value-pair.\n");
1159 log(" in addition to = also <, <=, >=, and > are supported\n");
1161 log(" r:<pattern>, r:<pattern>=<pattern>\n");
1162 log(" cells with matching parameters. also with <, <=, >= and >.\n");
1164 log(" n:<pattern>\n");
1165 log(" all objects with a name matching the given pattern\n");
1166 log(" (i.e. 'n:' is optional as it is the default matching rule)\n");
1169 log(" push the selection saved prior with 'select -set <name> ...'\n");
1171 log("The following actions can be performed on the top sets on the stack:\n");
1174 log(" push a copy of the current selection to the stack\n");
1177 log(" replace the stack with a union of all elements on it\n");
1180 log(" replace top set with its invert\n");
1183 log(" replace the two top sets on the stack with their union\n");
1186 log(" replace the two top sets on the stack with their intersection\n");
1189 log(" pop the top set from the stack and subtract it from the new top\n");
1192 log(" like %%d but swap the roles of two top sets on the stack\n");
1195 log(" create a copy of the top set from the stack and push it\n");
1197 log(" %%x[<num1>|*][.<num2>][:<rule>[:<rule>..]]\n");
1198 log(" expand top set <num1> num times according to the specified rules.\n");
1199 log(" (i.e. select all cells connected to selected wires and select all\n");
1200 log(" wires connected to selected cells) The rules specify which cell\n");
1201 log(" ports to use for this. the syntax for a rule is a '-' for exclusion\n");
1202 log(" and a '+' for inclusion, followed by an optional comma separated\n");
1203 log(" list of cell types followed by an optional comma separated list of\n");
1204 log(" cell ports in square brackets. a rule can also be just a cell or wire\n");
1205 log(" name that limits the expansion (is included but does not go beyond).\n");
1206 log(" select at most <num2> objects. a warning message is printed when this\n");
1207 log(" limit is reached. When '*' is used instead of <num1> then the process\n");
1208 log(" is repeated until no further object are selected.\n");
1210 log(" %%ci[<num1>|*][.<num2>][:<rule>[:<rule>..]]\n");
1211 log(" %%co[<num1>|*][.<num2>][:<rule>[:<rule>..]]\n");
1212 log(" similar to %%x, but only select input (%%ci) or output cones (%%co)\n");
1214 log(" %%xe[...] %%cie[...] %%coe\n");
1215 log(" like %%x, %%ci, and %%co but only consider combinatorial cells\n");
1218 log(" expand top set by selecting all wires that are (at least in part)\n");
1219 log(" aliases for selected wires.\n");
1222 log(" expand top set by adding all modules that implement cells in selected\n");
1226 log(" expand top set by selecting all modules that contain selected objects\n");
1229 log(" select modules that implement selected cells\n");
1232 log(" select cells that implement selected modules\n");
1234 log(" %%R[<num>]\n");
1235 log(" select <num> random objects from top selection (default 1)\n");
1237 log("Example: the following command selects all wires that are connected to a\n");
1238 log("'GATE' input of a 'SWITCH' cell:\n");
1240 log(" select */t:SWITCH %%x:+[GATE] */t:SWITCH %%d\n");
1243 void execute(std::vector
<std::string
> args
, RTLIL::Design
*design
) YS_OVERRIDE
1245 bool add_mode
= false;
1246 bool del_mode
= false;
1247 bool clear_mode
= false;
1248 bool none_mode
= false;
1249 bool list_mode
= false;
1250 bool count_mode
= false;
1251 bool got_module
= false;
1252 bool assert_none
= false;
1253 bool assert_any
= false;
1254 int assert_count
= -1;
1255 int assert_max
= -1;
1256 int assert_min
= -1;
1257 std::string write_file
, read_file
;
1258 std::string set_name
, sel_str
;
1263 for (argidx
= 1; argidx
< args
.size(); argidx
++)
1265 std::string arg
= args
[argidx
];
1266 if (arg
== "-add") {
1270 if (arg
== "-del") {
1274 if (arg
== "-assert-none") {
1278 if (arg
== "-assert-any") {
1282 if (arg
== "-assert-count" && argidx
+1 < args
.size()) {
1283 assert_count
= atoi(args
[++argidx
].c_str());
1286 if (arg
== "-assert-max" && argidx
+1 < args
.size()) {
1287 assert_max
= atoi(args
[++argidx
].c_str());
1290 if (arg
== "-assert-min" && argidx
+1 < args
.size()) {
1291 assert_min
= atoi(args
[++argidx
].c_str());
1294 if (arg
== "-clear") {
1298 if (arg
== "-none") {
1302 if (arg
== "-list") {
1306 if (arg
== "-write" && argidx
+1 < args
.size()) {
1307 write_file
= args
[++argidx
];
1310 if (arg
== "-read" && argidx
+1 < args
.size()) {
1311 read_file
= args
[++argidx
];
1314 if (arg
== "-count") {
1318 if (arg
== "-module" && argidx
+1 < args
.size()) {
1319 RTLIL::IdString mod_name
= RTLIL::escape_id(args
[++argidx
]);
1320 if (design
->module(mod_name
) == nullptr)
1321 log_cmd_error("No such module: %s\n", id2cstr(mod_name
));
1322 design
->selected_active_module
= mod_name
.str();
1326 if (arg
== "-set" && argidx
+1 < args
.size()) {
1327 set_name
= RTLIL::escape_id(args
[++argidx
]);
1330 if (arg
.size() > 0 && arg
[0] == '-')
1331 log_cmd_error("Unknown option %s.\n", arg
.c_str());
1332 bool disable_empty_warning
= count_mode
|| assert_none
|| assert_any
|| (assert_count
!= -1) || (assert_max
!= -1) || (assert_min
!= -1);
1333 select_stmt(design
, arg
, disable_empty_warning
);
1334 sel_str
+= " " + arg
;
1337 if (!read_file
.empty())
1339 if (!sel_str
.empty())
1340 log_cmd_error("Option -read can not be combined with a selection expression.\n");
1342 std::ifstream
f(read_file
);
1343 yosys_input_files
.insert(read_file
);
1345 log_error("Can't open '%s' for reading: %s\n", read_file
.c_str(), strerror(errno
));
1347 RTLIL::Selection
sel(false);
1350 while (std::getline(f
, line
)) {
1351 size_t slash_pos
= line
.find('/');
1352 if (slash_pos
== string::npos
) {
1353 log_warning("Ignoring line without slash in 'select -read': %s\n", line
.c_str());
1356 IdString mod_name
= RTLIL::escape_id(line
.substr(0, slash_pos
));
1357 IdString obj_name
= RTLIL::escape_id(line
.substr(slash_pos
+1));
1358 sel
.selected_members
[mod_name
].insert(obj_name
);
1361 select_filter_active_mod(design
, sel
);
1362 sel
.optimize(design
);
1363 work_stack
.push_back(sel
);
1366 if (clear_mode
&& args
.size() != 2)
1367 log_cmd_error("Option -clear can not be combined with any other options.\n");
1369 if (none_mode
&& args
.size() != 2)
1370 log_cmd_error("Option -none can not be combined with any other options.\n");
1372 if (add_mode
+ del_mode
+ assert_none
+ assert_any
+ (assert_count
>= 0) + (assert_max
>= 0) + (assert_min
>= 0) > 1)
1373 log_cmd_error("Options -add, -del, -assert-none, -assert-any, assert-count, -assert-max or -assert-min can not be combined.\n");
1375 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))
1376 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");
1378 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))
1379 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");
1381 if (work_stack
.size() == 0 && got_module
) {
1382 RTLIL::Selection sel
;
1383 select_filter_active_mod(design
, sel
);
1384 work_stack
.push_back(sel
);
1387 while (work_stack
.size() > 1) {
1388 select_op_union(design
, work_stack
.front(), work_stack
.back());
1389 work_stack
.pop_back();
1392 log_assert(design
->selection_stack
.size() > 0);
1395 design
->selection_stack
.back() = RTLIL::Selection(true);
1396 design
->selected_active_module
= std::string();
1401 design
->selection_stack
.back() = RTLIL::Selection(false);
1405 if (list_mode
|| count_mode
|| !write_file
.empty())
1407 #define LOG_OBJECT(...) { if (list_mode) log(__VA_ARGS__); if (f != nullptr) fprintf(f, __VA_ARGS__); total_count++; }
1408 int total_count
= 0;
1410 if (!write_file
.empty()) {
1411 f
= fopen(write_file
.c_str(), "w");
1412 yosys_output_files
.insert(write_file
);
1414 log_error("Can't open '%s' for writing: %s\n", write_file
.c_str(), strerror(errno
));
1416 RTLIL::Selection
*sel
= &design
->selection_stack
.back();
1417 if (work_stack
.size() > 0)
1418 sel
= &work_stack
.back();
1419 sel
->optimize(design
);
1420 for (auto mod
: design
->modules())
1422 if (sel
->selected_whole_module(mod
->name
) && list_mode
)
1423 log("%s\n", id2cstr(mod
->name
));
1424 if (sel
->selected_module(mod
->name
)) {
1425 for (auto wire
: mod
->wires())
1426 if (sel
->selected_member(mod
->name
, wire
->name
))
1427 LOG_OBJECT("%s/%s\n", id2cstr(mod
->name
), id2cstr(wire
->name
))
1428 for (auto &it
: mod
->memories
)
1429 if (sel
->selected_member(mod
->name
, it
.first
))
1430 LOG_OBJECT("%s/%s\n", id2cstr(mod
->name
), id2cstr(it
.first
))
1431 for (auto cell
: mod
->cells())
1432 if (sel
->selected_member(mod
->name
, cell
->name
))
1433 LOG_OBJECT("%s/%s\n", id2cstr(mod
->name
), id2cstr(cell
->name
))
1434 for (auto &it
: mod
->processes
)
1435 if (sel
->selected_member(mod
->name
, it
.first
))
1436 LOG_OBJECT("%s/%s\n", id2cstr(mod
->name
), id2cstr(it
.first
))
1440 log("%d objects.\n", total_count
);
1449 if (work_stack
.size() == 0)
1450 log_cmd_error("Nothing to add to selection.\n");
1451 select_op_union(design
, design
->selection_stack
.back(), work_stack
.back());
1452 design
->selection_stack
.back().optimize(design
);
1458 if (work_stack
.size() == 0)
1459 log_cmd_error("Nothing to delete from selection.\n");
1460 select_op_diff(design
, design
->selection_stack
.back(), work_stack
.back());
1461 design
->selection_stack
.back().optimize(design
);
1467 if (work_stack
.size() == 0)
1468 log_cmd_error("No selection to check.\n");
1469 work_stack
.back().optimize(design
);
1470 if (!work_stack
.back().empty())
1472 RTLIL::Selection
*sel
= &work_stack
.back();
1473 sel
->optimize(design
);
1474 std::string desc
= describe_selection_for_assert(design
, sel
);
1475 log_error("Assertion failed: selection is not empty:%s\n%s", sel_str
.c_str(), desc
.c_str());
1482 if (work_stack
.size() == 0)
1483 log_cmd_error("No selection to check.\n");
1484 work_stack
.back().optimize(design
);
1485 if (work_stack
.back().empty())
1487 RTLIL::Selection
*sel
= &work_stack
.back();
1488 sel
->optimize(design
);
1489 std::string desc
= describe_selection_for_assert(design
, sel
);
1490 log_error("Assertion failed: selection is empty:%s\n%s", sel_str
.c_str(), desc
.c_str());
1495 if (assert_count
>= 0 || assert_max
>= 0 || assert_min
>= 0)
1497 int total_count
= 0;
1498 if (work_stack
.size() == 0)
1499 log_cmd_error("No selection to check.\n");
1500 RTLIL::Selection
*sel
= &work_stack
.back();
1501 sel
->optimize(design
);
1502 for (auto mod
: design
->modules())
1503 if (sel
->selected_module(mod
->name
)) {
1504 for (auto wire
: mod
->wires())
1505 if (sel
->selected_member(mod
->name
, wire
->name
))
1507 for (auto &it
: mod
->memories
)
1508 if (sel
->selected_member(mod
->name
, it
.first
))
1510 for (auto cell
: mod
->cells())
1511 if (sel
->selected_member(mod
->name
, cell
->name
))
1513 for (auto &it
: mod
->processes
)
1514 if (sel
->selected_member(mod
->name
, it
.first
))
1517 if (assert_count
>= 0 && assert_count
!= total_count
)
1519 std::string desc
= describe_selection_for_assert(design
, sel
);
1520 log_error("Assertion failed: selection contains %d elements instead of the asserted %d:%s\n%s",
1521 total_count
, assert_count
, sel_str
.c_str(), desc
.c_str());
1523 if (assert_max
>= 0 && assert_max
< total_count
)
1525 std::string desc
= describe_selection_for_assert(design
, sel
);
1526 log_error("Assertion failed: selection contains %d elements, more than the maximum number %d:%s\n%s",
1527 total_count
, assert_max
, sel_str
.c_str(), desc
.c_str());
1529 if (assert_min
>= 0 && assert_min
> total_count
)
1531 std::string desc
= describe_selection_for_assert(design
, sel
);
1532 log_error("Assertion failed: selection contains %d elements, less than the minimum number %d:%s\n%s",
1533 total_count
, assert_min
, sel_str
.c_str(), desc
.c_str());
1538 if (!set_name
.empty())
1540 if (work_stack
.size() == 0)
1541 design
->selection_vars
[set_name
] = RTLIL::Selection(false);
1543 design
->selection_vars
[set_name
] = work_stack
.back();
1547 if (work_stack
.size() == 0) {
1548 RTLIL::Selection
&sel
= design
->selection_stack
.back();
1549 if (sel
.full_selection
)
1551 for (auto &it
: sel
.selected_modules
)
1552 log("%s\n", id2cstr(it
));
1553 for (auto &it
: sel
.selected_members
)
1554 for (auto &it2
: it
.second
)
1555 log("%s/%s\n", id2cstr(it
.first
), id2cstr(it2
));
1559 design
->selection_stack
.back() = work_stack
.back();
1560 design
->selection_stack
.back().optimize(design
);
1564 struct CdPass
: public Pass
{
1565 CdPass() : Pass("cd", "a shortcut for 'select -module <name>'") { }
1566 void help() YS_OVERRIDE
1568 // |---v---|---v---|---v---|---v---|---v---|---v---|---v---|---v---|---v---|---v---|
1570 log(" cd <modname>\n");
1572 log("This is just a shortcut for 'select -module <modname>'.\n");
1575 log(" cd <cellname>\n");
1577 log("When no module with the specified name is found, but there is a cell\n");
1578 log("with the specified name in the current module, then this is equivalent\n");
1579 log("to 'cd <celltype>'.\n");
1583 log("Remove trailing substrings that start with '.' in current module name until\n");
1584 log("the name of a module in the current design is generated, then switch to that\n");
1585 log("module. Otherwise clear the current selection.\n");
1589 log("This is just a shortcut for 'select -clear'.\n");
1592 void execute(std::vector
<std::string
> args
, RTLIL::Design
*design
) YS_OVERRIDE
1594 if (args
.size() != 1 && args
.size() != 2)
1595 log_cmd_error("Invalid number of arguments.\n");
1597 if (args
.size() == 1 || args
[1] == "/") {
1598 design
->selection_stack
.back() = RTLIL::Selection(true);
1599 design
->selected_active_module
= std::string();
1603 if (args
[1] == "..")
1605 string modname
= design
->selected_active_module
;
1607 design
->selection_stack
.back() = RTLIL::Selection(true);
1608 design
->selected_active_module
= std::string();
1612 size_t pos
= modname
.rfind('.');
1614 if (pos
== string::npos
)
1617 modname
= modname
.substr(0, pos
);
1618 Module
*mod
= design
->module(modname
);
1623 design
->selected_active_module
= modname
;
1624 design
->selection_stack
.back() = RTLIL::Selection();
1625 select_filter_active_mod(design
, design
->selection_stack
.back());
1626 design
->selection_stack
.back().optimize(design
);
1633 std::string modname
= RTLIL::escape_id(args
[1]);
1635 if (design
->module(modname
) == nullptr && !design
->selected_active_module
.empty()) {
1636 RTLIL::Module
*module
= design
->module(design
->selected_active_module
);
1637 if (module
!= nullptr && module
->cell(modname
) != nullptr)
1638 modname
= module
->cell(modname
)->type
.str();
1641 if (design
->module(modname
) != nullptr) {
1642 design
->selected_active_module
= modname
;
1643 design
->selection_stack
.back() = RTLIL::Selection();
1644 select_filter_active_mod(design
, design
->selection_stack
.back());
1645 design
->selection_stack
.back().optimize(design
);
1649 log_cmd_error("No such module `%s' found!\n", RTLIL::unescape_id(modname
).c_str());
1653 template<typename T
>
1654 static void log_matches(const char *title
, Module
*module
, T list
)
1656 std::vector
<IdString
> matches
;
1658 for (auto &it
: list
)
1659 if (module
->selected(it
.second
))
1660 matches
.push_back(it
.first
);
1662 if (!matches
.empty()) {
1663 log("\n%d %s:\n", int(matches
.size()), title
);
1664 std::sort(matches
.begin(), matches
.end(), RTLIL::sort_by_id_str());
1665 for (auto id
: matches
)
1666 log(" %s\n", RTLIL::id2cstr(id
));
1670 struct LsPass
: public Pass
{
1671 LsPass() : Pass("ls", "list modules or objects in modules") { }
1672 void help() YS_OVERRIDE
1674 // |---v---|---v---|---v---|---v---|---v---|---v---|---v---|---v---|---v---|---v---|
1676 log(" ls [selection]\n");
1678 log("When no active module is selected, this prints a list of modules.\n");
1680 log("When an active module is selected, this prints a list of objects in the module.\n");
1683 void execute(std::vector
<std::string
> args
, RTLIL::Design
*design
) YS_OVERRIDE
1686 extra_args(args
, argidx
, design
);
1688 if (design
->selected_active_module
.empty())
1690 std::vector
<IdString
> matches
;
1692 for (auto mod
: design
->selected_modules())
1693 matches
.push_back(mod
->name
);
1695 if (!matches
.empty()) {
1696 log("\n%d %s:\n", int(matches
.size()), "modules");
1697 std::sort(matches
.begin(), matches
.end(), RTLIL::sort_by_id_str());
1698 for (auto id
: matches
)
1699 log(" %s%s\n", log_id(id
), design
->selected_whole_module(design
->module(id
)) ? "" : "*");
1703 if (design
->module(design
->selected_active_module
) != nullptr)
1705 RTLIL::Module
*module
= design
->module(design
->selected_active_module
);
1706 log_matches("wires", module
, module
->wires_
);
1707 log_matches("memories", module
, module
->memories
);
1708 log_matches("cells", module
, module
->cells_
);
1709 log_matches("processes", module
, module
->processes
);
1714 PRIVATE_NAMESPACE_END