Merge pull request #1510 from pumbor/master
[yosys.git] / kernel / rtlil.cc
1 /*
2 * yosys -- Yosys Open SYnthesis Suite
3 *
4 * Copyright (C) 2012 Clifford Wolf <clifford@clifford.at>
5 *
6 * Permission to use, copy, modify, and/or distribute this software for any
7 * purpose with or without fee is hereby granted, provided that the above
8 * copyright notice and this permission notice appear in all copies.
9 *
10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 *
18 */
19
20 #include "kernel/yosys.h"
21 #include "kernel/macc.h"
22 #include "kernel/celltypes.h"
23 #include "frontends/verilog/verilog_frontend.h"
24 #include "backends/ilang/ilang_backend.h"
25
26 #include <string.h>
27 #include <algorithm>
28
29 YOSYS_NAMESPACE_BEGIN
30
31 RTLIL::IdString::destruct_guard_t RTLIL::IdString::destruct_guard;
32 std::vector<char*> RTLIL::IdString::global_id_storage_;
33 dict<char*, int, hash_cstr_ops> RTLIL::IdString::global_id_index_;
34 #ifndef YOSYS_NO_IDS_REFCNT
35 std::vector<int> RTLIL::IdString::global_refcount_storage_;
36 std::vector<int> RTLIL::IdString::global_free_idx_list_;
37 #endif
38 #ifdef YOSYS_USE_STICKY_IDS
39 int RTLIL::IdString::last_created_idx_[8];
40 int RTLIL::IdString::last_created_idx_ptr_;
41 #endif
42
43 IdString RTLIL::ID::A;
44 IdString RTLIL::ID::B;
45 IdString RTLIL::ID::Y;
46 IdString RTLIL::ID::keep;
47 IdString RTLIL::ID::whitebox;
48 IdString RTLIL::ID::blackbox;
49 dict<std::string, std::string> RTLIL::constpad;
50
51 RTLIL::Const::Const()
52 {
53 flags = RTLIL::CONST_FLAG_NONE;
54 }
55
56 RTLIL::Const::Const(std::string str)
57 {
58 flags = RTLIL::CONST_FLAG_STRING;
59 for (int i = str.size()-1; i >= 0; i--) {
60 unsigned char ch = str[i];
61 for (int j = 0; j < 8; j++) {
62 bits.push_back((ch & 1) != 0 ? State::S1 : State::S0);
63 ch = ch >> 1;
64 }
65 }
66 }
67
68 RTLIL::Const::Const(int val, int width)
69 {
70 flags = RTLIL::CONST_FLAG_NONE;
71 for (int i = 0; i < width; i++) {
72 bits.push_back((val & 1) != 0 ? State::S1 : State::S0);
73 val = val >> 1;
74 }
75 }
76
77 RTLIL::Const::Const(RTLIL::State bit, int width)
78 {
79 flags = RTLIL::CONST_FLAG_NONE;
80 for (int i = 0; i < width; i++)
81 bits.push_back(bit);
82 }
83
84 RTLIL::Const::Const(const std::vector<bool> &bits)
85 {
86 flags = RTLIL::CONST_FLAG_NONE;
87 for (auto b : bits)
88 this->bits.push_back(b ? State::S1 : State::S0);
89 }
90
91 RTLIL::Const::Const(const RTLIL::Const &c)
92 {
93 flags = c.flags;
94 for (auto b : c.bits)
95 this->bits.push_back(b);
96 }
97
98 bool RTLIL::Const::operator <(const RTLIL::Const &other) const
99 {
100 if (bits.size() != other.bits.size())
101 return bits.size() < other.bits.size();
102 for (size_t i = 0; i < bits.size(); i++)
103 if (bits[i] != other.bits[i])
104 return bits[i] < other.bits[i];
105 return false;
106 }
107
108 bool RTLIL::Const::operator ==(const RTLIL::Const &other) const
109 {
110 return bits == other.bits;
111 }
112
113 bool RTLIL::Const::operator !=(const RTLIL::Const &other) const
114 {
115 return bits != other.bits;
116 }
117
118 bool RTLIL::Const::as_bool() const
119 {
120 for (size_t i = 0; i < bits.size(); i++)
121 if (bits[i] == State::S1)
122 return true;
123 return false;
124 }
125
126 int RTLIL::Const::as_int(bool is_signed) const
127 {
128 int32_t ret = 0;
129 for (size_t i = 0; i < bits.size() && i < 32; i++)
130 if (bits[i] == State::S1)
131 ret |= 1 << i;
132 if (is_signed && bits.back() == State::S1)
133 for (size_t i = bits.size(); i < 32; i++)
134 ret |= 1 << i;
135 return ret;
136 }
137
138 std::string RTLIL::Const::as_string() const
139 {
140 std::string ret;
141 for (size_t i = bits.size(); i > 0; i--)
142 switch (bits[i-1]) {
143 case S0: ret += "0"; break;
144 case S1: ret += "1"; break;
145 case Sx: ret += "x"; break;
146 case Sz: ret += "z"; break;
147 case Sa: ret += "-"; break;
148 case Sm: ret += "m"; break;
149 }
150 return ret;
151 }
152
153 RTLIL::Const RTLIL::Const::from_string(std::string str)
154 {
155 Const c;
156 for (auto it = str.rbegin(); it != str.rend(); it++)
157 switch (*it) {
158 case '0': c.bits.push_back(State::S0); break;
159 case '1': c.bits.push_back(State::S1); break;
160 case 'x': c.bits.push_back(State::Sx); break;
161 case 'z': c.bits.push_back(State::Sz); break;
162 case 'm': c.bits.push_back(State::Sm); break;
163 default: c.bits.push_back(State::Sa);
164 }
165 return c;
166 }
167
168 std::string RTLIL::Const::decode_string() const
169 {
170 std::string string;
171 std::vector<char> string_chars;
172 for (int i = 0; i < int (bits.size()); i += 8) {
173 char ch = 0;
174 for (int j = 0; j < 8 && i + j < int (bits.size()); j++)
175 if (bits[i + j] == RTLIL::State::S1)
176 ch |= 1 << j;
177 if (ch != 0)
178 string_chars.push_back(ch);
179 }
180 for (int i = int (string_chars.size()) - 1; i >= 0; i--)
181 string += string_chars[i];
182 return string;
183 }
184
185 bool RTLIL::Const::is_fully_zero() const
186 {
187 cover("kernel.rtlil.const.is_fully_zero");
188
189 for (auto bit : bits)
190 if (bit != RTLIL::State::S0)
191 return false;
192
193 return true;
194 }
195
196 bool RTLIL::Const::is_fully_ones() const
197 {
198 cover("kernel.rtlil.const.is_fully_ones");
199
200 for (auto bit : bits)
201 if (bit != RTLIL::State::S1)
202 return false;
203
204 return true;
205 }
206
207 bool RTLIL::Const::is_fully_def() const
208 {
209 cover("kernel.rtlil.const.is_fully_def");
210
211 for (auto bit : bits)
212 if (bit != RTLIL::State::S0 && bit != RTLIL::State::S1)
213 return false;
214
215 return true;
216 }
217
218 bool RTLIL::Const::is_fully_undef() const
219 {
220 cover("kernel.rtlil.const.is_fully_undef");
221
222 for (auto bit : bits)
223 if (bit != RTLIL::State::Sx && bit != RTLIL::State::Sz)
224 return false;
225
226 return true;
227 }
228
229 void RTLIL::AttrObject::set_bool_attribute(RTLIL::IdString id, bool value)
230 {
231 if (value)
232 attributes[id] = RTLIL::Const(1);
233 else {
234 const auto it = attributes.find(id);
235 if (it != attributes.end())
236 attributes.erase(it);
237 }
238 }
239
240 bool RTLIL::AttrObject::get_bool_attribute(RTLIL::IdString id) const
241 {
242 const auto it = attributes.find(id);
243 if (it == attributes.end())
244 return false;
245 return it->second.as_bool();
246 }
247
248 void RTLIL::AttrObject::set_strpool_attribute(RTLIL::IdString id, const pool<string> &data)
249 {
250 string attrval;
251 for (auto &s : data) {
252 if (!attrval.empty())
253 attrval += "|";
254 attrval += s;
255 }
256 attributes[id] = RTLIL::Const(attrval);
257 }
258
259 void RTLIL::AttrObject::add_strpool_attribute(RTLIL::IdString id, const pool<string> &data)
260 {
261 pool<string> union_data = get_strpool_attribute(id);
262 union_data.insert(data.begin(), data.end());
263 if (!union_data.empty())
264 set_strpool_attribute(id, union_data);
265 }
266
267 pool<string> RTLIL::AttrObject::get_strpool_attribute(RTLIL::IdString id) const
268 {
269 pool<string> data;
270 if (attributes.count(id) != 0)
271 for (auto s : split_tokens(attributes.at(id).decode_string(), "|"))
272 data.insert(s);
273 return data;
274 }
275
276 void RTLIL::AttrObject::set_src_attribute(const std::string &src)
277 {
278 if (src.empty())
279 attributes.erase(ID(src));
280 else
281 attributes[ID(src)] = src;
282 }
283
284 std::string RTLIL::AttrObject::get_src_attribute() const
285 {
286 std::string src;
287 if (attributes.count(ID(src)))
288 src = attributes.at(ID(src)).decode_string();
289 return src;
290 }
291
292 bool RTLIL::Selection::selected_module(RTLIL::IdString mod_name) const
293 {
294 if (full_selection)
295 return true;
296 if (selected_modules.count(mod_name) > 0)
297 return true;
298 if (selected_members.count(mod_name) > 0)
299 return true;
300 return false;
301 }
302
303 bool RTLIL::Selection::selected_whole_module(RTLIL::IdString mod_name) const
304 {
305 if (full_selection)
306 return true;
307 if (selected_modules.count(mod_name) > 0)
308 return true;
309 return false;
310 }
311
312 bool RTLIL::Selection::selected_member(RTLIL::IdString mod_name, RTLIL::IdString memb_name) const
313 {
314 if (full_selection)
315 return true;
316 if (selected_modules.count(mod_name) > 0)
317 return true;
318 if (selected_members.count(mod_name) > 0)
319 if (selected_members.at(mod_name).count(memb_name) > 0)
320 return true;
321 return false;
322 }
323
324 void RTLIL::Selection::optimize(RTLIL::Design *design)
325 {
326 if (full_selection) {
327 selected_modules.clear();
328 selected_members.clear();
329 return;
330 }
331
332 std::vector<RTLIL::IdString> del_list, add_list;
333
334 del_list.clear();
335 for (auto mod_name : selected_modules) {
336 if (design->modules_.count(mod_name) == 0)
337 del_list.push_back(mod_name);
338 selected_members.erase(mod_name);
339 }
340 for (auto mod_name : del_list)
341 selected_modules.erase(mod_name);
342
343 del_list.clear();
344 for (auto &it : selected_members)
345 if (design->modules_.count(it.first) == 0)
346 del_list.push_back(it.first);
347 for (auto mod_name : del_list)
348 selected_members.erase(mod_name);
349
350 for (auto &it : selected_members) {
351 del_list.clear();
352 for (auto memb_name : it.second)
353 if (design->modules_[it.first]->count_id(memb_name) == 0)
354 del_list.push_back(memb_name);
355 for (auto memb_name : del_list)
356 it.second.erase(memb_name);
357 }
358
359 del_list.clear();
360 add_list.clear();
361 for (auto &it : selected_members)
362 if (it.second.size() == 0)
363 del_list.push_back(it.first);
364 else if (it.second.size() == design->modules_[it.first]->wires_.size() + design->modules_[it.first]->memories.size() +
365 design->modules_[it.first]->cells_.size() + design->modules_[it.first]->processes.size())
366 add_list.push_back(it.first);
367 for (auto mod_name : del_list)
368 selected_members.erase(mod_name);
369 for (auto mod_name : add_list) {
370 selected_members.erase(mod_name);
371 selected_modules.insert(mod_name);
372 }
373
374 if (selected_modules.size() == design->modules_.size()) {
375 full_selection = true;
376 selected_modules.clear();
377 selected_members.clear();
378 }
379 }
380
381 RTLIL::Design::Design()
382 {
383 static unsigned int hashidx_count = 123456789;
384 hashidx_count = mkhash_xorshift(hashidx_count);
385 hashidx_ = hashidx_count;
386
387 refcount_modules_ = 0;
388 selection_stack.push_back(RTLIL::Selection());
389
390 #ifdef WITH_PYTHON
391 RTLIL::Design::get_all_designs()->insert(std::pair<unsigned int, RTLIL::Design*>(hashidx_, this));
392 #endif
393 }
394
395 RTLIL::Design::~Design()
396 {
397 for (auto it = modules_.begin(); it != modules_.end(); ++it)
398 delete it->second;
399 for (auto n : verilog_packages)
400 delete n;
401 for (auto n : verilog_globals)
402 delete n;
403 #ifdef WITH_PYTHON
404 RTLIL::Design::get_all_designs()->erase(hashidx_);
405 #endif
406 }
407
408 #ifdef WITH_PYTHON
409 static std::map<unsigned int, RTLIL::Design*> all_designs;
410 std::map<unsigned int, RTLIL::Design*> *RTLIL::Design::get_all_designs(void)
411 {
412 return &all_designs;
413 }
414 #endif
415
416 RTLIL::ObjRange<RTLIL::Module*> RTLIL::Design::modules()
417 {
418 return RTLIL::ObjRange<RTLIL::Module*>(&modules_, &refcount_modules_);
419 }
420
421 RTLIL::Module *RTLIL::Design::module(RTLIL::IdString name)
422 {
423 return modules_.count(name) ? modules_.at(name) : NULL;
424 }
425
426 RTLIL::Module *RTLIL::Design::top_module()
427 {
428 RTLIL::Module *module = nullptr;
429 int module_count = 0;
430
431 for (auto mod : selected_modules()) {
432 if (mod->get_bool_attribute(ID(top)))
433 return mod;
434 module_count++;
435 module = mod;
436 }
437
438 return module_count == 1 ? module : nullptr;
439 }
440
441 void RTLIL::Design::add(RTLIL::Module *module)
442 {
443 log_assert(modules_.count(module->name) == 0);
444 log_assert(refcount_modules_ == 0);
445 modules_[module->name] = module;
446 module->design = this;
447
448 for (auto mon : monitors)
449 mon->notify_module_add(module);
450
451 if (yosys_xtrace) {
452 log("#X# New Module: %s\n", log_id(module));
453 log_backtrace("-X- ", yosys_xtrace-1);
454 }
455 }
456
457 RTLIL::Module *RTLIL::Design::addModule(RTLIL::IdString name)
458 {
459 log_assert(modules_.count(name) == 0);
460 log_assert(refcount_modules_ == 0);
461
462 RTLIL::Module *module = new RTLIL::Module;
463 modules_[name] = module;
464 module->design = this;
465 module->name = name;
466
467 for (auto mon : monitors)
468 mon->notify_module_add(module);
469
470 if (yosys_xtrace) {
471 log("#X# New Module: %s\n", log_id(module));
472 log_backtrace("-X- ", yosys_xtrace-1);
473 }
474
475 return module;
476 }
477
478 void RTLIL::Design::scratchpad_unset(std::string varname)
479 {
480 scratchpad.erase(varname);
481 }
482
483 void RTLIL::Design::scratchpad_set_int(std::string varname, int value)
484 {
485 scratchpad[varname] = stringf("%d", value);
486 }
487
488 void RTLIL::Design::scratchpad_set_bool(std::string varname, bool value)
489 {
490 scratchpad[varname] = value ? "true" : "false";
491 }
492
493 void RTLIL::Design::scratchpad_set_string(std::string varname, std::string value)
494 {
495 scratchpad[varname] = value;
496 }
497
498 int RTLIL::Design::scratchpad_get_int(std::string varname, int default_value) const
499 {
500 if (scratchpad.count(varname) == 0)
501 return default_value;
502
503 std::string str = scratchpad.at(varname);
504
505 if (str == "0" || str == "false")
506 return 0;
507
508 if (str == "1" || str == "true")
509 return 1;
510
511 char *endptr = nullptr;
512 long int parsed_value = strtol(str.c_str(), &endptr, 10);
513 return *endptr ? default_value : parsed_value;
514 }
515
516 bool RTLIL::Design::scratchpad_get_bool(std::string varname, bool default_value) const
517 {
518 if (scratchpad.count(varname) == 0)
519 return default_value;
520
521 std::string str = scratchpad.at(varname);
522
523 if (str == "0" || str == "false")
524 return false;
525
526 if (str == "1" || str == "true")
527 return true;
528
529 return default_value;
530 }
531
532 std::string RTLIL::Design::scratchpad_get_string(std::string varname, std::string default_value) const
533 {
534 if (scratchpad.count(varname) == 0)
535 return default_value;
536 return scratchpad.at(varname);
537 }
538
539 void RTLIL::Design::remove(RTLIL::Module *module)
540 {
541 for (auto mon : monitors)
542 mon->notify_module_del(module);
543
544 if (yosys_xtrace) {
545 log("#X# Remove Module: %s\n", log_id(module));
546 log_backtrace("-X- ", yosys_xtrace-1);
547 }
548
549 log_assert(modules_.at(module->name) == module);
550 modules_.erase(module->name);
551 delete module;
552 }
553
554 void RTLIL::Design::rename(RTLIL::Module *module, RTLIL::IdString new_name)
555 {
556 modules_.erase(module->name);
557 module->name = new_name;
558 add(module);
559 }
560
561 void RTLIL::Design::sort()
562 {
563 scratchpad.sort();
564 modules_.sort(sort_by_id_str());
565 for (auto &it : modules_)
566 it.second->sort();
567 }
568
569 void RTLIL::Design::check()
570 {
571 #ifndef NDEBUG
572 for (auto &it : modules_) {
573 log_assert(this == it.second->design);
574 log_assert(it.first == it.second->name);
575 log_assert(!it.first.empty());
576 it.second->check();
577 }
578 #endif
579 }
580
581 void RTLIL::Design::optimize()
582 {
583 for (auto &it : modules_)
584 it.second->optimize();
585 for (auto &it : selection_stack)
586 it.optimize(this);
587 for (auto &it : selection_vars)
588 it.second.optimize(this);
589 }
590
591 bool RTLIL::Design::selected_module(RTLIL::IdString mod_name) const
592 {
593 if (!selected_active_module.empty() && mod_name != selected_active_module)
594 return false;
595 if (selection_stack.size() == 0)
596 return true;
597 return selection_stack.back().selected_module(mod_name);
598 }
599
600 bool RTLIL::Design::selected_whole_module(RTLIL::IdString mod_name) const
601 {
602 if (!selected_active_module.empty() && mod_name != selected_active_module)
603 return false;
604 if (selection_stack.size() == 0)
605 return true;
606 return selection_stack.back().selected_whole_module(mod_name);
607 }
608
609 bool RTLIL::Design::selected_member(RTLIL::IdString mod_name, RTLIL::IdString memb_name) const
610 {
611 if (!selected_active_module.empty() && mod_name != selected_active_module)
612 return false;
613 if (selection_stack.size() == 0)
614 return true;
615 return selection_stack.back().selected_member(mod_name, memb_name);
616 }
617
618 bool RTLIL::Design::selected_module(RTLIL::Module *mod) const
619 {
620 return selected_module(mod->name);
621 }
622
623 bool RTLIL::Design::selected_whole_module(RTLIL::Module *mod) const
624 {
625 return selected_whole_module(mod->name);
626 }
627
628 std::vector<RTLIL::Module*> RTLIL::Design::selected_modules() const
629 {
630 std::vector<RTLIL::Module*> result;
631 result.reserve(modules_.size());
632 for (auto &it : modules_)
633 if (selected_module(it.first) && !it.second->get_blackbox_attribute())
634 result.push_back(it.second);
635 return result;
636 }
637
638 std::vector<RTLIL::Module*> RTLIL::Design::selected_whole_modules() const
639 {
640 std::vector<RTLIL::Module*> result;
641 result.reserve(modules_.size());
642 for (auto &it : modules_)
643 if (selected_whole_module(it.first) && !it.second->get_blackbox_attribute())
644 result.push_back(it.second);
645 return result;
646 }
647
648 std::vector<RTLIL::Module*> RTLIL::Design::selected_whole_modules_warn() const
649 {
650 std::vector<RTLIL::Module*> result;
651 result.reserve(modules_.size());
652 for (auto &it : modules_)
653 if (it.second->get_blackbox_attribute())
654 continue;
655 else if (selected_whole_module(it.first))
656 result.push_back(it.second);
657 else if (selected_module(it.first))
658 log_warning("Ignoring partially selected module %s.\n", log_id(it.first));
659 return result;
660 }
661
662 RTLIL::Module::Module()
663 {
664 static unsigned int hashidx_count = 123456789;
665 hashidx_count = mkhash_xorshift(hashidx_count);
666 hashidx_ = hashidx_count;
667
668 design = nullptr;
669 refcount_wires_ = 0;
670 refcount_cells_ = 0;
671
672 #ifdef WITH_PYTHON
673 RTLIL::Module::get_all_modules()->insert(std::pair<unsigned int, RTLIL::Module*>(hashidx_, this));
674 #endif
675 }
676
677 RTLIL::Module::~Module()
678 {
679 for (auto it = wires_.begin(); it != wires_.end(); ++it)
680 delete it->second;
681 for (auto it = memories.begin(); it != memories.end(); ++it)
682 delete it->second;
683 for (auto it = cells_.begin(); it != cells_.end(); ++it)
684 delete it->second;
685 for (auto it = processes.begin(); it != processes.end(); ++it)
686 delete it->second;
687 #ifdef WITH_PYTHON
688 RTLIL::Module::get_all_modules()->erase(hashidx_);
689 #endif
690 }
691
692 #ifdef WITH_PYTHON
693 static std::map<unsigned int, RTLIL::Module*> all_modules;
694 std::map<unsigned int, RTLIL::Module*> *RTLIL::Module::get_all_modules(void)
695 {
696 return &all_modules;
697 }
698 #endif
699
700 void RTLIL::Module::makeblackbox()
701 {
702 pool<RTLIL::Wire*> delwires;
703
704 for (auto it = wires_.begin(); it != wires_.end(); ++it)
705 if (!it->second->port_input && !it->second->port_output)
706 delwires.insert(it->second);
707
708 for (auto it = memories.begin(); it != memories.end(); ++it)
709 delete it->second;
710 memories.clear();
711
712 for (auto it = cells_.begin(); it != cells_.end(); ++it)
713 delete it->second;
714 cells_.clear();
715
716 for (auto it = processes.begin(); it != processes.end(); ++it)
717 delete it->second;
718 processes.clear();
719
720 remove(delwires);
721 set_bool_attribute(ID::blackbox);
722 }
723
724 void RTLIL::Module::reprocess_module(RTLIL::Design *, dict<RTLIL::IdString, RTLIL::Module *>)
725 {
726 log_error("Cannot reprocess_module module `%s' !\n", id2cstr(name));
727 }
728
729 RTLIL::IdString RTLIL::Module::derive(RTLIL::Design*, dict<RTLIL::IdString, RTLIL::Const>, bool mayfail)
730 {
731 if (mayfail)
732 return RTLIL::IdString();
733 log_error("Module `%s' is used with parameters but is not parametric!\n", id2cstr(name));
734 }
735
736
737 RTLIL::IdString RTLIL::Module::derive(RTLIL::Design*, dict<RTLIL::IdString, RTLIL::Const>, dict<RTLIL::IdString, RTLIL::Module*>, dict<RTLIL::IdString, RTLIL::IdString>, bool mayfail)
738 {
739 if (mayfail)
740 return RTLIL::IdString();
741 log_error("Module `%s' is used with parameters but is not parametric!\n", id2cstr(name));
742 }
743
744 size_t RTLIL::Module::count_id(RTLIL::IdString id)
745 {
746 return wires_.count(id) + memories.count(id) + cells_.count(id) + processes.count(id);
747 }
748
749 #ifndef NDEBUG
750 namespace {
751 struct InternalCellChecker
752 {
753 RTLIL::Module *module;
754 RTLIL::Cell *cell;
755 pool<RTLIL::IdString> expected_params, expected_ports;
756
757 InternalCellChecker(RTLIL::Module *module, RTLIL::Cell *cell) : module(module), cell(cell) { }
758
759 void error(int linenr)
760 {
761 std::stringstream buf;
762 ILANG_BACKEND::dump_cell(buf, " ", cell);
763
764 log_error("Found error in internal cell %s%s%s (%s) at %s:%d:\n%s",
765 module ? module->name.c_str() : "", module ? "." : "",
766 cell->name.c_str(), cell->type.c_str(), __FILE__, linenr, buf.str().c_str());
767 }
768
769 int param(RTLIL::IdString name)
770 {
771 if (cell->parameters.count(name) == 0)
772 error(__LINE__);
773 expected_params.insert(name);
774 return cell->parameters.at(name).as_int();
775 }
776
777 int param_bool(RTLIL::IdString name)
778 {
779 int v = param(name);
780 if (cell->parameters.at(name).bits.size() > 32)
781 error(__LINE__);
782 if (v != 0 && v != 1)
783 error(__LINE__);
784 return v;
785 }
786
787 int param_bool(RTLIL::IdString name, bool expected)
788 {
789 int v = param_bool(name);
790 if (v != expected)
791 error(__LINE__);
792 return v;
793 }
794
795 void param_bits(RTLIL::IdString name, int width)
796 {
797 param(name);
798 if (int(cell->parameters.at(name).bits.size()) != width)
799 error(__LINE__);
800 }
801
802 void port(RTLIL::IdString name, int width)
803 {
804 if (!cell->hasPort(name))
805 error(__LINE__);
806 if (cell->getPort(name).size() != width)
807 error(__LINE__);
808 expected_ports.insert(name);
809 }
810
811 void check_expected(bool check_matched_sign = true)
812 {
813 for (auto &para : cell->parameters)
814 if (expected_params.count(para.first) == 0)
815 error(__LINE__);
816 for (auto &conn : cell->connections())
817 if (expected_ports.count(conn.first) == 0)
818 error(__LINE__);
819
820 if (expected_params.count(ID(A_SIGNED)) != 0 && expected_params.count(ID(B_SIGNED)) && check_matched_sign) {
821 bool a_is_signed = param(ID(A_SIGNED)) != 0;
822 bool b_is_signed = param(ID(B_SIGNED)) != 0;
823 if (a_is_signed != b_is_signed)
824 error(__LINE__);
825 }
826 }
827
828 void check_gate(const char *ports)
829 {
830 if (cell->parameters.size() != 0)
831 error(__LINE__);
832
833 for (const char *p = ports; *p; p++) {
834 char portname[3] = { '\\', *p, 0 };
835 if (!cell->hasPort(portname))
836 error(__LINE__);
837 if (cell->getPort(portname).size() != 1)
838 error(__LINE__);
839 }
840
841 for (auto &conn : cell->connections()) {
842 if (conn.first.size() != 2 || conn.first[0] != '\\')
843 error(__LINE__);
844 if (strchr(ports, conn.first[1]) == NULL)
845 error(__LINE__);
846 }
847 }
848
849 void check()
850 {
851 if (!cell->type.begins_with("$") || cell->type.begins_with("$__") || cell->type.begins_with("$paramod") || cell->type.begins_with("$fmcombine") ||
852 cell->type.begins_with("$verific$") || cell->type.begins_with("$array:") || cell->type.begins_with("$extern:"))
853 return;
854
855 if (cell->type.in(ID($not), ID($pos), ID($neg))) {
856 param_bool(ID(A_SIGNED));
857 port(ID::A, param(ID(A_WIDTH)));
858 port(ID::Y, param(ID(Y_WIDTH)));
859 check_expected();
860 return;
861 }
862
863 if (cell->type.in(ID($and), ID($or), ID($xor), ID($xnor))) {
864 param_bool(ID(A_SIGNED));
865 param_bool(ID(B_SIGNED));
866 port(ID::A, param(ID(A_WIDTH)));
867 port(ID::B, param(ID(B_WIDTH)));
868 port(ID::Y, param(ID(Y_WIDTH)));
869 check_expected();
870 return;
871 }
872
873 if (cell->type.in(ID($reduce_and), ID($reduce_or), ID($reduce_xor), ID($reduce_xnor), ID($reduce_bool))) {
874 param_bool(ID(A_SIGNED));
875 port(ID::A, param(ID(A_WIDTH)));
876 port(ID::Y, param(ID(Y_WIDTH)));
877 check_expected();
878 return;
879 }
880
881 if (cell->type.in(ID($shl), ID($shr), ID($sshl), ID($sshr))) {
882 param_bool(ID(A_SIGNED));
883 param_bool(ID(B_SIGNED), /*expected=*/false);
884 port(ID::A, param(ID(A_WIDTH)));
885 port(ID::B, param(ID(B_WIDTH)));
886 port(ID::Y, param(ID(Y_WIDTH)));
887 check_expected(/*check_matched_sign=*/false);
888 return;
889 }
890
891 if (cell->type.in(ID($shift), ID($shiftx))) {
892 param_bool(ID(A_SIGNED));
893 param_bool(ID(B_SIGNED));
894 port(ID::A, param(ID(A_WIDTH)));
895 port(ID::B, param(ID(B_WIDTH)));
896 port(ID::Y, param(ID(Y_WIDTH)));
897 check_expected(/*check_matched_sign=*/false);
898 return;
899 }
900
901 if (cell->type.in(ID($lt), ID($le), ID($eq), ID($ne), ID($eqx), ID($nex), ID($ge), ID($gt))) {
902 param_bool(ID(A_SIGNED));
903 param_bool(ID(B_SIGNED));
904 port(ID::A, param(ID(A_WIDTH)));
905 port(ID::B, param(ID(B_WIDTH)));
906 port(ID::Y, param(ID(Y_WIDTH)));
907 check_expected();
908 return;
909 }
910
911 if (cell->type.in(ID($add), ID($sub), ID($mul), ID($div), ID($mod), ID($pow))) {
912 param_bool(ID(A_SIGNED));
913 param_bool(ID(B_SIGNED));
914 port(ID::A, param(ID(A_WIDTH)));
915 port(ID::B, param(ID(B_WIDTH)));
916 port(ID::Y, param(ID(Y_WIDTH)));
917 check_expected(cell->type != ID($pow));
918 return;
919 }
920
921 if (cell->type == ID($fa)) {
922 port(ID::A, param(ID(WIDTH)));
923 port(ID::B, param(ID(WIDTH)));
924 port(ID(C), param(ID(WIDTH)));
925 port(ID(X), param(ID(WIDTH)));
926 port(ID::Y, param(ID(WIDTH)));
927 check_expected();
928 return;
929 }
930
931 if (cell->type == ID($lcu)) {
932 port(ID(P), param(ID(WIDTH)));
933 port(ID(G), param(ID(WIDTH)));
934 port(ID(CI), 1);
935 port(ID(CO), param(ID(WIDTH)));
936 check_expected();
937 return;
938 }
939
940 if (cell->type == ID($alu)) {
941 param_bool(ID(A_SIGNED));
942 param_bool(ID(B_SIGNED));
943 port(ID::A, param(ID(A_WIDTH)));
944 port(ID::B, param(ID(B_WIDTH)));
945 port(ID(CI), 1);
946 port(ID(BI), 1);
947 port(ID(X), param(ID(Y_WIDTH)));
948 port(ID::Y, param(ID(Y_WIDTH)));
949 port(ID(CO), param(ID(Y_WIDTH)));
950 check_expected();
951 return;
952 }
953
954 if (cell->type == ID($macc)) {
955 param(ID(CONFIG));
956 param(ID(CONFIG_WIDTH));
957 port(ID::A, param(ID(A_WIDTH)));
958 port(ID::B, param(ID(B_WIDTH)));
959 port(ID::Y, param(ID(Y_WIDTH)));
960 check_expected();
961 Macc().from_cell(cell);
962 return;
963 }
964
965 if (cell->type == ID($logic_not)) {
966 param_bool(ID(A_SIGNED));
967 port(ID::A, param(ID(A_WIDTH)));
968 port(ID::Y, param(ID(Y_WIDTH)));
969 check_expected();
970 return;
971 }
972
973 if (cell->type.in(ID($logic_and), ID($logic_or))) {
974 param_bool(ID(A_SIGNED));
975 param_bool(ID(B_SIGNED));
976 port(ID::A, param(ID(A_WIDTH)));
977 port(ID::B, param(ID(B_WIDTH)));
978 port(ID::Y, param(ID(Y_WIDTH)));
979 check_expected(/*check_matched_sign=*/false);
980 return;
981 }
982
983 if (cell->type == ID($slice)) {
984 param(ID(OFFSET));
985 port(ID::A, param(ID(A_WIDTH)));
986 port(ID::Y, param(ID(Y_WIDTH)));
987 if (param(ID(OFFSET)) + param(ID(Y_WIDTH)) > param(ID(A_WIDTH)))
988 error(__LINE__);
989 check_expected();
990 return;
991 }
992
993 if (cell->type == ID($concat)) {
994 port(ID::A, param(ID(A_WIDTH)));
995 port(ID::B, param(ID(B_WIDTH)));
996 port(ID::Y, param(ID(A_WIDTH)) + param(ID(B_WIDTH)));
997 check_expected();
998 return;
999 }
1000
1001 if (cell->type == ID($mux)) {
1002 port(ID::A, param(ID(WIDTH)));
1003 port(ID::B, param(ID(WIDTH)));
1004 port(ID(S), 1);
1005 port(ID::Y, param(ID(WIDTH)));
1006 check_expected();
1007 return;
1008 }
1009
1010 if (cell->type == ID($pmux)) {
1011 port(ID::A, param(ID(WIDTH)));
1012 port(ID::B, param(ID(WIDTH)) * param(ID(S_WIDTH)));
1013 port(ID(S), param(ID(S_WIDTH)));
1014 port(ID::Y, param(ID(WIDTH)));
1015 check_expected();
1016 return;
1017 }
1018
1019 if (cell->type == ID($lut)) {
1020 param(ID(LUT));
1021 port(ID::A, param(ID(WIDTH)));
1022 port(ID::Y, 1);
1023 check_expected();
1024 return;
1025 }
1026
1027 if (cell->type == ID($sop)) {
1028 param(ID(DEPTH));
1029 param(ID(TABLE));
1030 port(ID::A, param(ID(WIDTH)));
1031 port(ID::Y, 1);
1032 check_expected();
1033 return;
1034 }
1035
1036 if (cell->type == ID($sr)) {
1037 param_bool(ID(SET_POLARITY));
1038 param_bool(ID(CLR_POLARITY));
1039 port(ID(SET), param(ID(WIDTH)));
1040 port(ID(CLR), param(ID(WIDTH)));
1041 port(ID(Q), param(ID(WIDTH)));
1042 check_expected();
1043 return;
1044 }
1045
1046 if (cell->type == ID($ff)) {
1047 port(ID(D), param(ID(WIDTH)));
1048 port(ID(Q), param(ID(WIDTH)));
1049 check_expected();
1050 return;
1051 }
1052
1053 if (cell->type == ID($dff)) {
1054 param_bool(ID(CLK_POLARITY));
1055 port(ID(CLK), 1);
1056 port(ID(D), param(ID(WIDTH)));
1057 port(ID(Q), param(ID(WIDTH)));
1058 check_expected();
1059 return;
1060 }
1061
1062 if (cell->type == ID($dffe)) {
1063 param_bool(ID(CLK_POLARITY));
1064 param_bool(ID(EN_POLARITY));
1065 port(ID(CLK), 1);
1066 port(ID(EN), 1);
1067 port(ID(D), param(ID(WIDTH)));
1068 port(ID(Q), param(ID(WIDTH)));
1069 check_expected();
1070 return;
1071 }
1072
1073 if (cell->type == ID($dffsr)) {
1074 param_bool(ID(CLK_POLARITY));
1075 param_bool(ID(SET_POLARITY));
1076 param_bool(ID(CLR_POLARITY));
1077 port(ID(CLK), 1);
1078 port(ID(SET), param(ID(WIDTH)));
1079 port(ID(CLR), param(ID(WIDTH)));
1080 port(ID(D), param(ID(WIDTH)));
1081 port(ID(Q), param(ID(WIDTH)));
1082 check_expected();
1083 return;
1084 }
1085
1086 if (cell->type == ID($adff)) {
1087 param_bool(ID(CLK_POLARITY));
1088 param_bool(ID(ARST_POLARITY));
1089 param_bits(ID(ARST_VALUE), param(ID(WIDTH)));
1090 port(ID(CLK), 1);
1091 port(ID(ARST), 1);
1092 port(ID(D), param(ID(WIDTH)));
1093 port(ID(Q), param(ID(WIDTH)));
1094 check_expected();
1095 return;
1096 }
1097
1098 if (cell->type == ID($dlatch)) {
1099 param_bool(ID(EN_POLARITY));
1100 port(ID(EN), 1);
1101 port(ID(D), param(ID(WIDTH)));
1102 port(ID(Q), param(ID(WIDTH)));
1103 check_expected();
1104 return;
1105 }
1106
1107 if (cell->type == ID($dlatchsr)) {
1108 param_bool(ID(EN_POLARITY));
1109 param_bool(ID(SET_POLARITY));
1110 param_bool(ID(CLR_POLARITY));
1111 port(ID(EN), 1);
1112 port(ID(SET), param(ID(WIDTH)));
1113 port(ID(CLR), param(ID(WIDTH)));
1114 port(ID(D), param(ID(WIDTH)));
1115 port(ID(Q), param(ID(WIDTH)));
1116 check_expected();
1117 return;
1118 }
1119
1120 if (cell->type == ID($fsm)) {
1121 param(ID(NAME));
1122 param_bool(ID(CLK_POLARITY));
1123 param_bool(ID(ARST_POLARITY));
1124 param(ID(STATE_BITS));
1125 param(ID(STATE_NUM));
1126 param(ID(STATE_NUM_LOG2));
1127 param(ID(STATE_RST));
1128 param_bits(ID(STATE_TABLE), param(ID(STATE_BITS)) * param(ID(STATE_NUM)));
1129 param(ID(TRANS_NUM));
1130 param_bits(ID(TRANS_TABLE), param(ID(TRANS_NUM)) * (2*param(ID(STATE_NUM_LOG2)) + param(ID(CTRL_IN_WIDTH)) + param(ID(CTRL_OUT_WIDTH))));
1131 port(ID(CLK), 1);
1132 port(ID(ARST), 1);
1133 port(ID(CTRL_IN), param(ID(CTRL_IN_WIDTH)));
1134 port(ID(CTRL_OUT), param(ID(CTRL_OUT_WIDTH)));
1135 check_expected();
1136 return;
1137 }
1138
1139 if (cell->type == ID($memrd)) {
1140 param(ID(MEMID));
1141 param_bool(ID(CLK_ENABLE));
1142 param_bool(ID(CLK_POLARITY));
1143 param_bool(ID(TRANSPARENT));
1144 port(ID(CLK), 1);
1145 port(ID(EN), 1);
1146 port(ID(ADDR), param(ID(ABITS)));
1147 port(ID(DATA), param(ID(WIDTH)));
1148 check_expected();
1149 return;
1150 }
1151
1152 if (cell->type == ID($memwr)) {
1153 param(ID(MEMID));
1154 param_bool(ID(CLK_ENABLE));
1155 param_bool(ID(CLK_POLARITY));
1156 param(ID(PRIORITY));
1157 port(ID(CLK), 1);
1158 port(ID(EN), param(ID(WIDTH)));
1159 port(ID(ADDR), param(ID(ABITS)));
1160 port(ID(DATA), param(ID(WIDTH)));
1161 check_expected();
1162 return;
1163 }
1164
1165 if (cell->type == ID($meminit)) {
1166 param(ID(MEMID));
1167 param(ID(PRIORITY));
1168 port(ID(ADDR), param(ID(ABITS)));
1169 port(ID(DATA), param(ID(WIDTH)) * param(ID(WORDS)));
1170 check_expected();
1171 return;
1172 }
1173
1174 if (cell->type == ID($mem)) {
1175 param(ID(MEMID));
1176 param(ID(SIZE));
1177 param(ID(OFFSET));
1178 param(ID(INIT));
1179 param_bits(ID(RD_CLK_ENABLE), max(1, param(ID(RD_PORTS))));
1180 param_bits(ID(RD_CLK_POLARITY), max(1, param(ID(RD_PORTS))));
1181 param_bits(ID(RD_TRANSPARENT), max(1, param(ID(RD_PORTS))));
1182 param_bits(ID(WR_CLK_ENABLE), max(1, param(ID(WR_PORTS))));
1183 param_bits(ID(WR_CLK_POLARITY), max(1, param(ID(WR_PORTS))));
1184 port(ID(RD_CLK), param(ID(RD_PORTS)));
1185 port(ID(RD_EN), param(ID(RD_PORTS)));
1186 port(ID(RD_ADDR), param(ID(RD_PORTS)) * param(ID(ABITS)));
1187 port(ID(RD_DATA), param(ID(RD_PORTS)) * param(ID(WIDTH)));
1188 port(ID(WR_CLK), param(ID(WR_PORTS)));
1189 port(ID(WR_EN), param(ID(WR_PORTS)) * param(ID(WIDTH)));
1190 port(ID(WR_ADDR), param(ID(WR_PORTS)) * param(ID(ABITS)));
1191 port(ID(WR_DATA), param(ID(WR_PORTS)) * param(ID(WIDTH)));
1192 check_expected();
1193 return;
1194 }
1195
1196 if (cell->type == ID($tribuf)) {
1197 port(ID::A, param(ID(WIDTH)));
1198 port(ID::Y, param(ID(WIDTH)));
1199 port(ID(EN), 1);
1200 check_expected();
1201 return;
1202 }
1203
1204 if (cell->type.in(ID($assert), ID($assume), ID($live), ID($fair), ID($cover))) {
1205 port(ID::A, 1);
1206 port(ID(EN), 1);
1207 check_expected();
1208 return;
1209 }
1210
1211 if (cell->type == ID($initstate)) {
1212 port(ID::Y, 1);
1213 check_expected();
1214 return;
1215 }
1216
1217 if (cell->type.in(ID($anyconst), ID($anyseq), ID($allconst), ID($allseq))) {
1218 port(ID::Y, param(ID(WIDTH)));
1219 check_expected();
1220 return;
1221 }
1222
1223 if (cell->type == ID($equiv)) {
1224 port(ID::A, 1);
1225 port(ID::B, 1);
1226 port(ID::Y, 1);
1227 check_expected();
1228 return;
1229 }
1230
1231 if (cell->type.in(ID($specify2), ID($specify3))) {
1232 param_bool(ID(FULL));
1233 param_bool(ID(SRC_DST_PEN));
1234 param_bool(ID(SRC_DST_POL));
1235 param(ID(T_RISE_MIN));
1236 param(ID(T_RISE_TYP));
1237 param(ID(T_RISE_MAX));
1238 param(ID(T_FALL_MIN));
1239 param(ID(T_FALL_TYP));
1240 param(ID(T_FALL_MAX));
1241 port(ID(EN), 1);
1242 port(ID(SRC), param(ID(SRC_WIDTH)));
1243 port(ID(DST), param(ID(DST_WIDTH)));
1244 if (cell->type == ID($specify3)) {
1245 param_bool(ID(EDGE_EN));
1246 param_bool(ID(EDGE_POL));
1247 param_bool(ID(DAT_DST_PEN));
1248 param_bool(ID(DAT_DST_POL));
1249 port(ID(DAT), param(ID(DST_WIDTH)));
1250 }
1251 check_expected();
1252 return;
1253 }
1254
1255 if (cell->type == ID($specrule)) {
1256 param(ID(TYPE));
1257 param_bool(ID(SRC_PEN));
1258 param_bool(ID(SRC_POL));
1259 param_bool(ID(DST_PEN));
1260 param_bool(ID(DST_POL));
1261 param(ID(T_LIMIT));
1262 param(ID(T_LIMIT2));
1263 port(ID(SRC_EN), 1);
1264 port(ID(DST_EN), 1);
1265 port(ID(SRC), param(ID(SRC_WIDTH)));
1266 port(ID(DST), param(ID(DST_WIDTH)));
1267 check_expected();
1268 return;
1269 }
1270
1271 if (cell->type == ID($_BUF_)) { check_gate("AY"); return; }
1272 if (cell->type == ID($_NOT_)) { check_gate("AY"); return; }
1273 if (cell->type == ID($_AND_)) { check_gate("ABY"); return; }
1274 if (cell->type == ID($_NAND_)) { check_gate("ABY"); return; }
1275 if (cell->type == ID($_OR_)) { check_gate("ABY"); return; }
1276 if (cell->type == ID($_NOR_)) { check_gate("ABY"); return; }
1277 if (cell->type == ID($_XOR_)) { check_gate("ABY"); return; }
1278 if (cell->type == ID($_XNOR_)) { check_gate("ABY"); return; }
1279 if (cell->type == ID($_ANDNOT_)) { check_gate("ABY"); return; }
1280 if (cell->type == ID($_ORNOT_)) { check_gate("ABY"); return; }
1281 if (cell->type == ID($_MUX_)) { check_gate("ABSY"); return; }
1282 if (cell->type == ID($_NMUX_)) { check_gate("ABSY"); return; }
1283 if (cell->type == ID($_AOI3_)) { check_gate("ABCY"); return; }
1284 if (cell->type == ID($_OAI3_)) { check_gate("ABCY"); return; }
1285 if (cell->type == ID($_AOI4_)) { check_gate("ABCDY"); return; }
1286 if (cell->type == ID($_OAI4_)) { check_gate("ABCDY"); return; }
1287
1288 if (cell->type == ID($_TBUF_)) { check_gate("AYE"); return; }
1289
1290 if (cell->type == ID($_MUX4_)) { check_gate("ABCDSTY"); return; }
1291 if (cell->type == ID($_MUX8_)) { check_gate("ABCDEFGHSTUY"); return; }
1292 if (cell->type == ID($_MUX16_)) { check_gate("ABCDEFGHIJKLMNOPSTUVY"); return; }
1293
1294 if (cell->type == ID($_SR_NN_)) { check_gate("SRQ"); return; }
1295 if (cell->type == ID($_SR_NP_)) { check_gate("SRQ"); return; }
1296 if (cell->type == ID($_SR_PN_)) { check_gate("SRQ"); return; }
1297 if (cell->type == ID($_SR_PP_)) { check_gate("SRQ"); return; }
1298
1299 if (cell->type == ID($_FF_)) { check_gate("DQ"); return; }
1300 if (cell->type == ID($_DFF_N_)) { check_gate("DQC"); return; }
1301 if (cell->type == ID($_DFF_P_)) { check_gate("DQC"); return; }
1302
1303 if (cell->type == ID($_DFFE_NN_)) { check_gate("DQCE"); return; }
1304 if (cell->type == ID($_DFFE_NP_)) { check_gate("DQCE"); return; }
1305 if (cell->type == ID($_DFFE_PN_)) { check_gate("DQCE"); return; }
1306 if (cell->type == ID($_DFFE_PP_)) { check_gate("DQCE"); return; }
1307
1308 if (cell->type == ID($_DFF_NN0_)) { check_gate("DQCR"); return; }
1309 if (cell->type == ID($_DFF_NN1_)) { check_gate("DQCR"); return; }
1310 if (cell->type == ID($_DFF_NP0_)) { check_gate("DQCR"); return; }
1311 if (cell->type == ID($_DFF_NP1_)) { check_gate("DQCR"); return; }
1312 if (cell->type == ID($_DFF_PN0_)) { check_gate("DQCR"); return; }
1313 if (cell->type == ID($_DFF_PN1_)) { check_gate("DQCR"); return; }
1314 if (cell->type == ID($_DFF_PP0_)) { check_gate("DQCR"); return; }
1315 if (cell->type == ID($_DFF_PP1_)) { check_gate("DQCR"); return; }
1316
1317 if (cell->type == ID($_DFFSR_NNN_)) { check_gate("CSRDQ"); return; }
1318 if (cell->type == ID($_DFFSR_NNP_)) { check_gate("CSRDQ"); return; }
1319 if (cell->type == ID($_DFFSR_NPN_)) { check_gate("CSRDQ"); return; }
1320 if (cell->type == ID($_DFFSR_NPP_)) { check_gate("CSRDQ"); return; }
1321 if (cell->type == ID($_DFFSR_PNN_)) { check_gate("CSRDQ"); return; }
1322 if (cell->type == ID($_DFFSR_PNP_)) { check_gate("CSRDQ"); return; }
1323 if (cell->type == ID($_DFFSR_PPN_)) { check_gate("CSRDQ"); return; }
1324 if (cell->type == ID($_DFFSR_PPP_)) { check_gate("CSRDQ"); return; }
1325
1326 if (cell->type == ID($_DLATCH_N_)) { check_gate("EDQ"); return; }
1327 if (cell->type == ID($_DLATCH_P_)) { check_gate("EDQ"); return; }
1328
1329 if (cell->type == ID($_DLATCHSR_NNN_)) { check_gate("ESRDQ"); return; }
1330 if (cell->type == ID($_DLATCHSR_NNP_)) { check_gate("ESRDQ"); return; }
1331 if (cell->type == ID($_DLATCHSR_NPN_)) { check_gate("ESRDQ"); return; }
1332 if (cell->type == ID($_DLATCHSR_NPP_)) { check_gate("ESRDQ"); return; }
1333 if (cell->type == ID($_DLATCHSR_PNN_)) { check_gate("ESRDQ"); return; }
1334 if (cell->type == ID($_DLATCHSR_PNP_)) { check_gate("ESRDQ"); return; }
1335 if (cell->type == ID($_DLATCHSR_PPN_)) { check_gate("ESRDQ"); return; }
1336 if (cell->type == ID($_DLATCHSR_PPP_)) { check_gate("ESRDQ"); return; }
1337
1338 error(__LINE__);
1339 }
1340 };
1341 }
1342 #endif
1343
1344 void RTLIL::Module::sort()
1345 {
1346 wires_.sort(sort_by_id_str());
1347 cells_.sort(sort_by_id_str());
1348 avail_parameters.sort(sort_by_id_str());
1349 memories.sort(sort_by_id_str());
1350 processes.sort(sort_by_id_str());
1351 for (auto &it : cells_)
1352 it.second->sort();
1353 for (auto &it : wires_)
1354 it.second->attributes.sort(sort_by_id_str());
1355 for (auto &it : memories)
1356 it.second->attributes.sort(sort_by_id_str());
1357 }
1358
1359 void RTLIL::Module::check()
1360 {
1361 #ifndef NDEBUG
1362 std::vector<bool> ports_declared;
1363 for (auto &it : wires_) {
1364 log_assert(this == it.second->module);
1365 log_assert(it.first == it.second->name);
1366 log_assert(!it.first.empty());
1367 log_assert(it.second->width >= 0);
1368 log_assert(it.second->port_id >= 0);
1369 for (auto &it2 : it.second->attributes)
1370 log_assert(!it2.first.empty());
1371 if (it.second->port_id) {
1372 log_assert(GetSize(ports) >= it.second->port_id);
1373 log_assert(ports.at(it.second->port_id-1) == it.first);
1374 log_assert(it.second->port_input || it.second->port_output);
1375 if (GetSize(ports_declared) < it.second->port_id)
1376 ports_declared.resize(it.second->port_id);
1377 log_assert(ports_declared[it.second->port_id-1] == false);
1378 ports_declared[it.second->port_id-1] = true;
1379 } else
1380 log_assert(!it.second->port_input && !it.second->port_output);
1381 }
1382 for (auto port_declared : ports_declared)
1383 log_assert(port_declared == true);
1384 log_assert(GetSize(ports) == GetSize(ports_declared));
1385
1386 for (auto &it : memories) {
1387 log_assert(it.first == it.second->name);
1388 log_assert(!it.first.empty());
1389 log_assert(it.second->width >= 0);
1390 log_assert(it.second->size >= 0);
1391 for (auto &it2 : it.second->attributes)
1392 log_assert(!it2.first.empty());
1393 }
1394
1395 for (auto &it : cells_) {
1396 log_assert(this == it.second->module);
1397 log_assert(it.first == it.second->name);
1398 log_assert(!it.first.empty());
1399 log_assert(!it.second->type.empty());
1400 for (auto &it2 : it.second->connections()) {
1401 log_assert(!it2.first.empty());
1402 it2.second.check();
1403 }
1404 for (auto &it2 : it.second->attributes)
1405 log_assert(!it2.first.empty());
1406 for (auto &it2 : it.second->parameters)
1407 log_assert(!it2.first.empty());
1408 InternalCellChecker checker(this, it.second);
1409 checker.check();
1410 }
1411
1412 for (auto &it : processes) {
1413 log_assert(it.first == it.second->name);
1414 log_assert(!it.first.empty());
1415 log_assert(it.second->root_case.compare.empty());
1416 std::vector<CaseRule*> all_cases = {&it.second->root_case};
1417 for (size_t i = 0; i < all_cases.size(); i++) {
1418 for (auto &switch_it : all_cases[i]->switches) {
1419 for (auto &case_it : switch_it->cases) {
1420 for (auto &compare_it : case_it->compare) {
1421 log_assert(switch_it->signal.size() == compare_it.size());
1422 }
1423 all_cases.push_back(case_it);
1424 }
1425 }
1426 }
1427 for (auto &sync_it : it.second->syncs) {
1428 switch (sync_it->type) {
1429 case SyncType::ST0:
1430 case SyncType::ST1:
1431 case SyncType::STp:
1432 case SyncType::STn:
1433 case SyncType::STe:
1434 log_assert(!sync_it->signal.empty());
1435 break;
1436 case SyncType::STa:
1437 case SyncType::STg:
1438 case SyncType::STi:
1439 log_assert(sync_it->signal.empty());
1440 break;
1441 }
1442 }
1443 }
1444
1445 for (auto &it : connections_) {
1446 log_assert(it.first.size() == it.second.size());
1447 log_assert(!it.first.has_const());
1448 it.first.check();
1449 it.second.check();
1450 }
1451
1452 for (auto &it : attributes)
1453 log_assert(!it.first.empty());
1454 #endif
1455 }
1456
1457 void RTLIL::Module::optimize()
1458 {
1459 }
1460
1461 void RTLIL::Module::cloneInto(RTLIL::Module *new_mod) const
1462 {
1463 log_assert(new_mod->refcount_wires_ == 0);
1464 log_assert(new_mod->refcount_cells_ == 0);
1465
1466 new_mod->avail_parameters = avail_parameters;
1467
1468 for (auto &conn : connections_)
1469 new_mod->connect(conn);
1470
1471 for (auto &attr : attributes)
1472 new_mod->attributes[attr.first] = attr.second;
1473
1474 for (auto &it : wires_)
1475 new_mod->addWire(it.first, it.second);
1476
1477 for (auto &it : memories)
1478 new_mod->memories[it.first] = new RTLIL::Memory(*it.second);
1479
1480 for (auto &it : cells_)
1481 new_mod->addCell(it.first, it.second);
1482
1483 for (auto &it : processes)
1484 new_mod->processes[it.first] = it.second->clone();
1485
1486 struct RewriteSigSpecWorker
1487 {
1488 RTLIL::Module *mod;
1489 void operator()(RTLIL::SigSpec &sig)
1490 {
1491 std::vector<RTLIL::SigChunk> chunks = sig.chunks();
1492 for (auto &c : chunks)
1493 if (c.wire != NULL)
1494 c.wire = mod->wires_.at(c.wire->name);
1495 sig = chunks;
1496 }
1497 };
1498
1499 RewriteSigSpecWorker rewriteSigSpecWorker;
1500 rewriteSigSpecWorker.mod = new_mod;
1501 new_mod->rewrite_sigspecs(rewriteSigSpecWorker);
1502 new_mod->fixup_ports();
1503 }
1504
1505 RTLIL::Module *RTLIL::Module::clone() const
1506 {
1507 RTLIL::Module *new_mod = new RTLIL::Module;
1508 new_mod->name = name;
1509 cloneInto(new_mod);
1510 return new_mod;
1511 }
1512
1513 bool RTLIL::Module::has_memories() const
1514 {
1515 return !memories.empty();
1516 }
1517
1518 bool RTLIL::Module::has_processes() const
1519 {
1520 return !processes.empty();
1521 }
1522
1523 bool RTLIL::Module::has_memories_warn() const
1524 {
1525 if (!memories.empty())
1526 log_warning("Ignoring module %s because it contains memories (run 'memory' command first).\n", log_id(this));
1527 return !memories.empty();
1528 }
1529
1530 bool RTLIL::Module::has_processes_warn() const
1531 {
1532 if (!processes.empty())
1533 log_warning("Ignoring module %s because it contains processes (run 'proc' command first).\n", log_id(this));
1534 return !processes.empty();
1535 }
1536
1537 std::vector<RTLIL::Wire*> RTLIL::Module::selected_wires() const
1538 {
1539 std::vector<RTLIL::Wire*> result;
1540 result.reserve(wires_.size());
1541 for (auto &it : wires_)
1542 if (design->selected(this, it.second))
1543 result.push_back(it.second);
1544 return result;
1545 }
1546
1547 std::vector<RTLIL::Cell*> RTLIL::Module::selected_cells() const
1548 {
1549 std::vector<RTLIL::Cell*> result;
1550 result.reserve(cells_.size());
1551 for (auto &it : cells_)
1552 if (design->selected(this, it.second))
1553 result.push_back(it.second);
1554 return result;
1555 }
1556
1557 void RTLIL::Module::add(RTLIL::Wire *wire)
1558 {
1559 log_assert(!wire->name.empty());
1560 log_assert(count_id(wire->name) == 0);
1561 log_assert(refcount_wires_ == 0);
1562 wires_[wire->name] = wire;
1563 wire->module = this;
1564 }
1565
1566 void RTLIL::Module::add(RTLIL::Cell *cell)
1567 {
1568 log_assert(!cell->name.empty());
1569 log_assert(count_id(cell->name) == 0);
1570 log_assert(refcount_cells_ == 0);
1571 cells_[cell->name] = cell;
1572 cell->module = this;
1573 }
1574
1575 void RTLIL::Module::remove(const pool<RTLIL::Wire*> &wires)
1576 {
1577 log_assert(refcount_wires_ == 0);
1578
1579 struct DeleteWireWorker
1580 {
1581 RTLIL::Module *module;
1582 const pool<RTLIL::Wire*> *wires_p;
1583
1584 void operator()(RTLIL::SigSpec &sig) {
1585 std::vector<RTLIL::SigChunk> chunks = sig;
1586 for (auto &c : chunks)
1587 if (c.wire != NULL && wires_p->count(c.wire)) {
1588 c.wire = module->addWire(NEW_ID, c.width);
1589 c.offset = 0;
1590 }
1591 sig = chunks;
1592 }
1593
1594 void operator()(RTLIL::SigSpec &lhs, RTLIL::SigSpec &rhs) {
1595 log_assert(GetSize(lhs) == GetSize(rhs));
1596 RTLIL::SigSpec new_lhs, new_rhs;
1597 for (int i = 0; i < GetSize(lhs); i++) {
1598 RTLIL::SigBit lhs_bit = lhs[i];
1599 if (lhs_bit.wire != nullptr && wires_p->count(lhs_bit.wire))
1600 continue;
1601 RTLIL::SigBit rhs_bit = rhs[i];
1602 if (rhs_bit.wire != nullptr && wires_p->count(rhs_bit.wire))
1603 continue;
1604 new_lhs.append(lhs_bit);
1605 new_rhs.append(rhs_bit);
1606 }
1607 lhs = new_lhs;
1608 rhs = new_rhs;
1609 }
1610 };
1611
1612 DeleteWireWorker delete_wire_worker;
1613 delete_wire_worker.module = this;
1614 delete_wire_worker.wires_p = &wires;
1615 rewrite_sigspecs2(delete_wire_worker);
1616
1617 for (auto &it : wires) {
1618 log_assert(wires_.count(it->name) != 0);
1619 wires_.erase(it->name);
1620 delete it;
1621 }
1622 }
1623
1624 void RTLIL::Module::remove(RTLIL::Cell *cell)
1625 {
1626 while (!cell->connections_.empty())
1627 cell->unsetPort(cell->connections_.begin()->first);
1628
1629 log_assert(cells_.count(cell->name) != 0);
1630 log_assert(refcount_cells_ == 0);
1631 cells_.erase(cell->name);
1632 delete cell;
1633 }
1634
1635 void RTLIL::Module::rename(RTLIL::Wire *wire, RTLIL::IdString new_name)
1636 {
1637 log_assert(wires_[wire->name] == wire);
1638 log_assert(refcount_wires_ == 0);
1639 wires_.erase(wire->name);
1640 wire->name = new_name;
1641 add(wire);
1642 }
1643
1644 void RTLIL::Module::rename(RTLIL::Cell *cell, RTLIL::IdString new_name)
1645 {
1646 log_assert(cells_[cell->name] == cell);
1647 log_assert(refcount_wires_ == 0);
1648 cells_.erase(cell->name);
1649 cell->name = new_name;
1650 add(cell);
1651 }
1652
1653 void RTLIL::Module::rename(RTLIL::IdString old_name, RTLIL::IdString new_name)
1654 {
1655 log_assert(count_id(old_name) != 0);
1656 if (wires_.count(old_name))
1657 rename(wires_.at(old_name), new_name);
1658 else if (cells_.count(old_name))
1659 rename(cells_.at(old_name), new_name);
1660 else
1661 log_abort();
1662 }
1663
1664 void RTLIL::Module::swap_names(RTLIL::Wire *w1, RTLIL::Wire *w2)
1665 {
1666 log_assert(wires_[w1->name] == w1);
1667 log_assert(wires_[w2->name] == w2);
1668 log_assert(refcount_wires_ == 0);
1669
1670 wires_.erase(w1->name);
1671 wires_.erase(w2->name);
1672
1673 std::swap(w1->name, w2->name);
1674
1675 wires_[w1->name] = w1;
1676 wires_[w2->name] = w2;
1677 }
1678
1679 void RTLIL::Module::swap_names(RTLIL::Cell *c1, RTLIL::Cell *c2)
1680 {
1681 log_assert(cells_[c1->name] == c1);
1682 log_assert(cells_[c2->name] == c2);
1683 log_assert(refcount_cells_ == 0);
1684
1685 cells_.erase(c1->name);
1686 cells_.erase(c2->name);
1687
1688 std::swap(c1->name, c2->name);
1689
1690 cells_[c1->name] = c1;
1691 cells_[c2->name] = c2;
1692 }
1693
1694 RTLIL::IdString RTLIL::Module::uniquify(RTLIL::IdString name)
1695 {
1696 int index = 0;
1697 return uniquify(name, index);
1698 }
1699
1700 RTLIL::IdString RTLIL::Module::uniquify(RTLIL::IdString name, int &index)
1701 {
1702 if (index == 0) {
1703 if (count_id(name) == 0)
1704 return name;
1705 index++;
1706 }
1707
1708 while (1) {
1709 RTLIL::IdString new_name = stringf("%s_%d", name.c_str(), index);
1710 if (count_id(new_name) == 0)
1711 return new_name;
1712 index++;
1713 }
1714 }
1715
1716 static bool fixup_ports_compare(const RTLIL::Wire *a, const RTLIL::Wire *b)
1717 {
1718 if (a->port_id && !b->port_id)
1719 return true;
1720 if (!a->port_id && b->port_id)
1721 return false;
1722
1723 if (a->port_id == b->port_id)
1724 return a->name < b->name;
1725 return a->port_id < b->port_id;
1726 }
1727
1728 void RTLIL::Module::connect(const RTLIL::SigSig &conn)
1729 {
1730 for (auto mon : monitors)
1731 mon->notify_connect(this, conn);
1732
1733 if (design)
1734 for (auto mon : design->monitors)
1735 mon->notify_connect(this, conn);
1736
1737 // ignore all attempts to assign constants to other constants
1738 if (conn.first.has_const()) {
1739 RTLIL::SigSig new_conn;
1740 for (int i = 0; i < GetSize(conn.first); i++)
1741 if (conn.first[i].wire) {
1742 new_conn.first.append(conn.first[i]);
1743 new_conn.second.append(conn.second[i]);
1744 }
1745 if (GetSize(new_conn.first))
1746 connect(new_conn);
1747 return;
1748 }
1749
1750 if (yosys_xtrace) {
1751 log("#X# Connect (SigSig) in %s: %s = %s (%d bits)\n", log_id(this), log_signal(conn.first), log_signal(conn.second), GetSize(conn.first));
1752 log_backtrace("-X- ", yosys_xtrace-1);
1753 }
1754
1755 log_assert(GetSize(conn.first) == GetSize(conn.second));
1756 connections_.push_back(conn);
1757 }
1758
1759 void RTLIL::Module::connect(const RTLIL::SigSpec &lhs, const RTLIL::SigSpec &rhs)
1760 {
1761 connect(RTLIL::SigSig(lhs, rhs));
1762 }
1763
1764 void RTLIL::Module::new_connections(const std::vector<RTLIL::SigSig> &new_conn)
1765 {
1766 for (auto mon : monitors)
1767 mon->notify_connect(this, new_conn);
1768
1769 if (design)
1770 for (auto mon : design->monitors)
1771 mon->notify_connect(this, new_conn);
1772
1773 if (yosys_xtrace) {
1774 log("#X# New connections vector in %s:\n", log_id(this));
1775 for (auto &conn: new_conn)
1776 log("#X# %s = %s (%d bits)\n", log_signal(conn.first), log_signal(conn.second), GetSize(conn.first));
1777 log_backtrace("-X- ", yosys_xtrace-1);
1778 }
1779
1780 connections_ = new_conn;
1781 }
1782
1783 const std::vector<RTLIL::SigSig> &RTLIL::Module::connections() const
1784 {
1785 return connections_;
1786 }
1787
1788 void RTLIL::Module::fixup_ports()
1789 {
1790 std::vector<RTLIL::Wire*> all_ports;
1791
1792 for (auto &w : wires_)
1793 if (w.second->port_input || w.second->port_output)
1794 all_ports.push_back(w.second);
1795 else
1796 w.second->port_id = 0;
1797
1798 std::sort(all_ports.begin(), all_ports.end(), fixup_ports_compare);
1799
1800 ports.clear();
1801 for (size_t i = 0; i < all_ports.size(); i++) {
1802 ports.push_back(all_ports[i]->name);
1803 all_ports[i]->port_id = i+1;
1804 }
1805 }
1806
1807 RTLIL::Wire *RTLIL::Module::addWire(RTLIL::IdString name, int width)
1808 {
1809 RTLIL::Wire *wire = new RTLIL::Wire;
1810 wire->name = name;
1811 wire->width = width;
1812 add(wire);
1813 return wire;
1814 }
1815
1816 RTLIL::Wire *RTLIL::Module::addWire(RTLIL::IdString name, const RTLIL::Wire *other)
1817 {
1818 RTLIL::Wire *wire = addWire(name);
1819 wire->width = other->width;
1820 wire->start_offset = other->start_offset;
1821 wire->port_id = other->port_id;
1822 wire->port_input = other->port_input;
1823 wire->port_output = other->port_output;
1824 wire->upto = other->upto;
1825 wire->attributes = other->attributes;
1826 return wire;
1827 }
1828
1829 RTLIL::Cell *RTLIL::Module::addCell(RTLIL::IdString name, RTLIL::IdString type)
1830 {
1831 RTLIL::Cell *cell = new RTLIL::Cell;
1832 cell->name = name;
1833 cell->type = type;
1834 add(cell);
1835 return cell;
1836 }
1837
1838 RTLIL::Cell *RTLIL::Module::addCell(RTLIL::IdString name, const RTLIL::Cell *other)
1839 {
1840 RTLIL::Cell *cell = addCell(name, other->type);
1841 cell->connections_ = other->connections_;
1842 cell->parameters = other->parameters;
1843 cell->attributes = other->attributes;
1844 return cell;
1845 }
1846
1847 #define DEF_METHOD(_func, _y_size, _type) \
1848 RTLIL::Cell* RTLIL::Module::add ## _func(RTLIL::IdString name, RTLIL::SigSpec sig_a, RTLIL::SigSpec sig_y, bool is_signed, const std::string &src) { \
1849 RTLIL::Cell *cell = addCell(name, _type); \
1850 cell->parameters[ID(A_SIGNED)] = is_signed; \
1851 cell->parameters[ID(A_WIDTH)] = sig_a.size(); \
1852 cell->parameters[ID(Y_WIDTH)] = sig_y.size(); \
1853 cell->setPort(ID::A, sig_a); \
1854 cell->setPort(ID::Y, sig_y); \
1855 cell->set_src_attribute(src); \
1856 return cell; \
1857 } \
1858 RTLIL::SigSpec RTLIL::Module::_func(RTLIL::IdString name, RTLIL::SigSpec sig_a, bool is_signed, const std::string &src) { \
1859 RTLIL::SigSpec sig_y = addWire(NEW_ID, _y_size); \
1860 add ## _func(name, sig_a, sig_y, is_signed, src); \
1861 return sig_y; \
1862 }
1863 DEF_METHOD(Not, sig_a.size(), ID($not))
1864 DEF_METHOD(Pos, sig_a.size(), ID($pos))
1865 DEF_METHOD(Neg, sig_a.size(), ID($neg))
1866 DEF_METHOD(ReduceAnd, 1, ID($reduce_and))
1867 DEF_METHOD(ReduceOr, 1, ID($reduce_or))
1868 DEF_METHOD(ReduceXor, 1, ID($reduce_xor))
1869 DEF_METHOD(ReduceXnor, 1, ID($reduce_xnor))
1870 DEF_METHOD(ReduceBool, 1, ID($reduce_bool))
1871 DEF_METHOD(LogicNot, 1, ID($logic_not))
1872 #undef DEF_METHOD
1873
1874 #define DEF_METHOD(_func, _y_size, _type) \
1875 RTLIL::Cell* RTLIL::Module::add ## _func(RTLIL::IdString name, RTLIL::SigSpec sig_a, RTLIL::SigSpec sig_b, RTLIL::SigSpec sig_y, bool is_signed, const std::string &src) { \
1876 RTLIL::Cell *cell = addCell(name, _type); \
1877 cell->parameters[ID(A_SIGNED)] = is_signed; \
1878 cell->parameters[ID(B_SIGNED)] = is_signed; \
1879 cell->parameters[ID(A_WIDTH)] = sig_a.size(); \
1880 cell->parameters[ID(B_WIDTH)] = sig_b.size(); \
1881 cell->parameters[ID(Y_WIDTH)] = sig_y.size(); \
1882 cell->setPort(ID::A, sig_a); \
1883 cell->setPort(ID::B, sig_b); \
1884 cell->setPort(ID::Y, sig_y); \
1885 cell->set_src_attribute(src); \
1886 return cell; \
1887 } \
1888 RTLIL::SigSpec RTLIL::Module::_func(RTLIL::IdString name, RTLIL::SigSpec sig_a, RTLIL::SigSpec sig_b, bool is_signed, const std::string &src) { \
1889 RTLIL::SigSpec sig_y = addWire(NEW_ID, _y_size); \
1890 add ## _func(name, sig_a, sig_b, sig_y, is_signed, src); \
1891 return sig_y; \
1892 }
1893 DEF_METHOD(And, max(sig_a.size(), sig_b.size()), ID($and))
1894 DEF_METHOD(Or, max(sig_a.size(), sig_b.size()), ID($or))
1895 DEF_METHOD(Xor, max(sig_a.size(), sig_b.size()), ID($xor))
1896 DEF_METHOD(Xnor, max(sig_a.size(), sig_b.size()), ID($xnor))
1897 DEF_METHOD(Shift, sig_a.size(), ID($shift))
1898 DEF_METHOD(Shiftx, sig_a.size(), ID($shiftx))
1899 DEF_METHOD(Lt, 1, ID($lt))
1900 DEF_METHOD(Le, 1, ID($le))
1901 DEF_METHOD(Eq, 1, ID($eq))
1902 DEF_METHOD(Ne, 1, ID($ne))
1903 DEF_METHOD(Eqx, 1, ID($eqx))
1904 DEF_METHOD(Nex, 1, ID($nex))
1905 DEF_METHOD(Ge, 1, ID($ge))
1906 DEF_METHOD(Gt, 1, ID($gt))
1907 DEF_METHOD(Add, max(sig_a.size(), sig_b.size()), ID($add))
1908 DEF_METHOD(Sub, max(sig_a.size(), sig_b.size()), ID($sub))
1909 DEF_METHOD(Mul, max(sig_a.size(), sig_b.size()), ID($mul))
1910 DEF_METHOD(Div, max(sig_a.size(), sig_b.size()), ID($div))
1911 DEF_METHOD(Mod, max(sig_a.size(), sig_b.size()), ID($mod))
1912 DEF_METHOD(LogicAnd, 1, ID($logic_and))
1913 DEF_METHOD(LogicOr, 1, ID($logic_or))
1914 #undef DEF_METHOD
1915
1916 #define DEF_METHOD(_func, _y_size, _type) \
1917 RTLIL::Cell* RTLIL::Module::add ## _func(RTLIL::IdString name, RTLIL::SigSpec sig_a, RTLIL::SigSpec sig_b, RTLIL::SigSpec sig_y, bool is_signed, const std::string &src) { \
1918 RTLIL::Cell *cell = addCell(name, _type); \
1919 cell->parameters[ID(A_SIGNED)] = is_signed; \
1920 cell->parameters[ID(B_SIGNED)] = false; \
1921 cell->parameters[ID(A_WIDTH)] = sig_a.size(); \
1922 cell->parameters[ID(B_WIDTH)] = sig_b.size(); \
1923 cell->parameters[ID(Y_WIDTH)] = sig_y.size(); \
1924 cell->setPort(ID::A, sig_a); \
1925 cell->setPort(ID::B, sig_b); \
1926 cell->setPort(ID::Y, sig_y); \
1927 cell->set_src_attribute(src); \
1928 return cell; \
1929 } \
1930 RTLIL::SigSpec RTLIL::Module::_func(RTLIL::IdString name, RTLIL::SigSpec sig_a, RTLIL::SigSpec sig_b, bool is_signed, const std::string &src) { \
1931 RTLIL::SigSpec sig_y = addWire(NEW_ID, _y_size); \
1932 add ## _func(name, sig_a, sig_b, sig_y, is_signed, src); \
1933 return sig_y; \
1934 }
1935 DEF_METHOD(Shl, sig_a.size(), ID($shl))
1936 DEF_METHOD(Shr, sig_a.size(), ID($shr))
1937 DEF_METHOD(Sshl, sig_a.size(), ID($sshl))
1938 DEF_METHOD(Sshr, sig_a.size(), ID($sshr))
1939 #undef DEF_METHOD
1940
1941 #define DEF_METHOD(_func, _type, _pmux) \
1942 RTLIL::Cell* RTLIL::Module::add ## _func(RTLIL::IdString name, RTLIL::SigSpec sig_a, RTLIL::SigSpec sig_b, RTLIL::SigSpec sig_s, RTLIL::SigSpec sig_y, const std::string &src) { \
1943 RTLIL::Cell *cell = addCell(name, _type); \
1944 cell->parameters[ID(WIDTH)] = sig_a.size(); \
1945 if (_pmux) cell->parameters[ID(S_WIDTH)] = sig_s.size(); \
1946 cell->setPort(ID::A, sig_a); \
1947 cell->setPort(ID::B, sig_b); \
1948 cell->setPort(ID(S), sig_s); \
1949 cell->setPort(ID::Y, sig_y); \
1950 cell->set_src_attribute(src); \
1951 return cell; \
1952 } \
1953 RTLIL::SigSpec RTLIL::Module::_func(RTLIL::IdString name, RTLIL::SigSpec sig_a, RTLIL::SigSpec sig_b, RTLIL::SigSpec sig_s, const std::string &src) { \
1954 RTLIL::SigSpec sig_y = addWire(NEW_ID, sig_a.size()); \
1955 add ## _func(name, sig_a, sig_b, sig_s, sig_y, src); \
1956 return sig_y; \
1957 }
1958 DEF_METHOD(Mux, ID($mux), 0)
1959 DEF_METHOD(Pmux, ID($pmux), 1)
1960 #undef DEF_METHOD
1961
1962 #define DEF_METHOD_2(_func, _type, _P1, _P2) \
1963 RTLIL::Cell* RTLIL::Module::add ## _func(RTLIL::IdString name, RTLIL::SigBit sig1, RTLIL::SigBit sig2, const std::string &src) { \
1964 RTLIL::Cell *cell = addCell(name, _type); \
1965 cell->setPort("\\" #_P1, sig1); \
1966 cell->setPort("\\" #_P2, sig2); \
1967 cell->set_src_attribute(src); \
1968 return cell; \
1969 } \
1970 RTLIL::SigBit RTLIL::Module::_func(RTLIL::IdString name, RTLIL::SigBit sig1, const std::string &src) { \
1971 RTLIL::SigBit sig2 = addWire(NEW_ID); \
1972 add ## _func(name, sig1, sig2, src); \
1973 return sig2; \
1974 }
1975 #define DEF_METHOD_3(_func, _type, _P1, _P2, _P3) \
1976 RTLIL::Cell* RTLIL::Module::add ## _func(RTLIL::IdString name, RTLIL::SigBit sig1, RTLIL::SigBit sig2, RTLIL::SigBit sig3, const std::string &src) { \
1977 RTLIL::Cell *cell = addCell(name, _type); \
1978 cell->setPort("\\" #_P1, sig1); \
1979 cell->setPort("\\" #_P2, sig2); \
1980 cell->setPort("\\" #_P3, sig3); \
1981 cell->set_src_attribute(src); \
1982 return cell; \
1983 } \
1984 RTLIL::SigBit RTLIL::Module::_func(RTLIL::IdString name, RTLIL::SigBit sig1, RTLIL::SigBit sig2, const std::string &src) { \
1985 RTLIL::SigBit sig3 = addWire(NEW_ID); \
1986 add ## _func(name, sig1, sig2, sig3, src); \
1987 return sig3; \
1988 }
1989 #define DEF_METHOD_4(_func, _type, _P1, _P2, _P3, _P4) \
1990 RTLIL::Cell* RTLIL::Module::add ## _func(RTLIL::IdString name, RTLIL::SigBit sig1, RTLIL::SigBit sig2, RTLIL::SigBit sig3, RTLIL::SigBit sig4, const std::string &src) { \
1991 RTLIL::Cell *cell = addCell(name, _type); \
1992 cell->setPort("\\" #_P1, sig1); \
1993 cell->setPort("\\" #_P2, sig2); \
1994 cell->setPort("\\" #_P3, sig3); \
1995 cell->setPort("\\" #_P4, sig4); \
1996 cell->set_src_attribute(src); \
1997 return cell; \
1998 } \
1999 RTLIL::SigBit RTLIL::Module::_func(RTLIL::IdString name, RTLIL::SigBit sig1, RTLIL::SigBit sig2, RTLIL::SigBit sig3, const std::string &src) { \
2000 RTLIL::SigBit sig4 = addWire(NEW_ID); \
2001 add ## _func(name, sig1, sig2, sig3, sig4, src); \
2002 return sig4; \
2003 }
2004 #define DEF_METHOD_5(_func, _type, _P1, _P2, _P3, _P4, _P5) \
2005 RTLIL::Cell* RTLIL::Module::add ## _func(RTLIL::IdString name, RTLIL::SigBit sig1, RTLIL::SigBit sig2, RTLIL::SigBit sig3, RTLIL::SigBit sig4, RTLIL::SigBit sig5, const std::string &src) { \
2006 RTLIL::Cell *cell = addCell(name, _type); \
2007 cell->setPort("\\" #_P1, sig1); \
2008 cell->setPort("\\" #_P2, sig2); \
2009 cell->setPort("\\" #_P3, sig3); \
2010 cell->setPort("\\" #_P4, sig4); \
2011 cell->setPort("\\" #_P5, sig5); \
2012 cell->set_src_attribute(src); \
2013 return cell; \
2014 } \
2015 RTLIL::SigBit RTLIL::Module::_func(RTLIL::IdString name, RTLIL::SigBit sig1, RTLIL::SigBit sig2, RTLIL::SigBit sig3, RTLIL::SigBit sig4, const std::string &src) { \
2016 RTLIL::SigBit sig5 = addWire(NEW_ID); \
2017 add ## _func(name, sig1, sig2, sig3, sig4, sig5, src); \
2018 return sig5; \
2019 }
2020 DEF_METHOD_2(BufGate, ID($_BUF_), A, Y)
2021 DEF_METHOD_2(NotGate, ID($_NOT_), A, Y)
2022 DEF_METHOD_3(AndGate, ID($_AND_), A, B, Y)
2023 DEF_METHOD_3(NandGate, ID($_NAND_), A, B, Y)
2024 DEF_METHOD_3(OrGate, ID($_OR_), A, B, Y)
2025 DEF_METHOD_3(NorGate, ID($_NOR_), A, B, Y)
2026 DEF_METHOD_3(XorGate, ID($_XOR_), A, B, Y)
2027 DEF_METHOD_3(XnorGate, ID($_XNOR_), A, B, Y)
2028 DEF_METHOD_3(AndnotGate, ID($_ANDNOT_), A, B, Y)
2029 DEF_METHOD_3(OrnotGate, ID($_ORNOT_), A, B, Y)
2030 DEF_METHOD_4(MuxGate, ID($_MUX_), A, B, S, Y)
2031 DEF_METHOD_4(NmuxGate, ID($_NMUX_), A, B, S, Y)
2032 DEF_METHOD_4(Aoi3Gate, ID($_AOI3_), A, B, C, Y)
2033 DEF_METHOD_4(Oai3Gate, ID($_OAI3_), A, B, C, Y)
2034 DEF_METHOD_5(Aoi4Gate, ID($_AOI4_), A, B, C, D, Y)
2035 DEF_METHOD_5(Oai4Gate, ID($_OAI4_), A, B, C, D, Y)
2036 #undef DEF_METHOD_2
2037 #undef DEF_METHOD_3
2038 #undef DEF_METHOD_4
2039 #undef DEF_METHOD_5
2040
2041 RTLIL::Cell* RTLIL::Module::addPow(RTLIL::IdString name, RTLIL::SigSpec sig_a, RTLIL::SigSpec sig_b, RTLIL::SigSpec sig_y, bool a_signed, bool b_signed, const std::string &src)
2042 {
2043 RTLIL::Cell *cell = addCell(name, ID($pow));
2044 cell->parameters[ID(A_SIGNED)] = a_signed;
2045 cell->parameters[ID(B_SIGNED)] = b_signed;
2046 cell->parameters[ID(A_WIDTH)] = sig_a.size();
2047 cell->parameters[ID(B_WIDTH)] = sig_b.size();
2048 cell->parameters[ID(Y_WIDTH)] = sig_y.size();
2049 cell->setPort(ID::A, sig_a);
2050 cell->setPort(ID::B, sig_b);
2051 cell->setPort(ID::Y, sig_y);
2052 cell->set_src_attribute(src);
2053 return cell;
2054 }
2055
2056 RTLIL::Cell* RTLIL::Module::addSlice(RTLIL::IdString name, RTLIL::SigSpec sig_a, RTLIL::SigSpec sig_y, RTLIL::Const offset, const std::string &src)
2057 {
2058 RTLIL::Cell *cell = addCell(name, ID($slice));
2059 cell->parameters[ID(A_WIDTH)] = sig_a.size();
2060 cell->parameters[ID(Y_WIDTH)] = sig_y.size();
2061 cell->parameters[ID(OFFSET)] = offset;
2062 cell->setPort(ID::A, sig_a);
2063 cell->setPort(ID::Y, sig_y);
2064 cell->set_src_attribute(src);
2065 return cell;
2066 }
2067
2068 RTLIL::Cell* RTLIL::Module::addConcat(RTLIL::IdString name, RTLIL::SigSpec sig_a, RTLIL::SigSpec sig_b, RTLIL::SigSpec sig_y, const std::string &src)
2069 {
2070 RTLIL::Cell *cell = addCell(name, ID($concat));
2071 cell->parameters[ID(A_WIDTH)] = sig_a.size();
2072 cell->parameters[ID(B_WIDTH)] = sig_b.size();
2073 cell->setPort(ID::A, sig_a);
2074 cell->setPort(ID::B, sig_b);
2075 cell->setPort(ID::Y, sig_y);
2076 cell->set_src_attribute(src);
2077 return cell;
2078 }
2079
2080 RTLIL::Cell* RTLIL::Module::addLut(RTLIL::IdString name, RTLIL::SigSpec sig_a, RTLIL::SigSpec sig_y, RTLIL::Const lut, const std::string &src)
2081 {
2082 RTLIL::Cell *cell = addCell(name, ID($lut));
2083 cell->parameters[ID(LUT)] = lut;
2084 cell->parameters[ID(WIDTH)] = sig_a.size();
2085 cell->setPort(ID::A, sig_a);
2086 cell->setPort(ID::Y, sig_y);
2087 cell->set_src_attribute(src);
2088 return cell;
2089 }
2090
2091 RTLIL::Cell* RTLIL::Module::addTribuf(RTLIL::IdString name, RTLIL::SigSpec sig_a, RTLIL::SigSpec sig_en, RTLIL::SigSpec sig_y, const std::string &src)
2092 {
2093 RTLIL::Cell *cell = addCell(name, ID($tribuf));
2094 cell->parameters[ID(WIDTH)] = sig_a.size();
2095 cell->setPort(ID::A, sig_a);
2096 cell->setPort(ID(EN), sig_en);
2097 cell->setPort(ID::Y, sig_y);
2098 cell->set_src_attribute(src);
2099 return cell;
2100 }
2101
2102 RTLIL::Cell* RTLIL::Module::addAssert(RTLIL::IdString name, RTLIL::SigSpec sig_a, RTLIL::SigSpec sig_en, const std::string &src)
2103 {
2104 RTLIL::Cell *cell = addCell(name, ID($assert));
2105 cell->setPort(ID::A, sig_a);
2106 cell->setPort(ID(EN), sig_en);
2107 cell->set_src_attribute(src);
2108 return cell;
2109 }
2110
2111 RTLIL::Cell* RTLIL::Module::addAssume(RTLIL::IdString name, RTLIL::SigSpec sig_a, RTLIL::SigSpec sig_en, const std::string &src)
2112 {
2113 RTLIL::Cell *cell = addCell(name, ID($assume));
2114 cell->setPort(ID::A, sig_a);
2115 cell->setPort(ID(EN), sig_en);
2116 cell->set_src_attribute(src);
2117 return cell;
2118 }
2119
2120 RTLIL::Cell* RTLIL::Module::addLive(RTLIL::IdString name, RTLIL::SigSpec sig_a, RTLIL::SigSpec sig_en, const std::string &src)
2121 {
2122 RTLIL::Cell *cell = addCell(name, ID($live));
2123 cell->setPort(ID::A, sig_a);
2124 cell->setPort(ID(EN), sig_en);
2125 cell->set_src_attribute(src);
2126 return cell;
2127 }
2128
2129 RTLIL::Cell* RTLIL::Module::addFair(RTLIL::IdString name, RTLIL::SigSpec sig_a, RTLIL::SigSpec sig_en, const std::string &src)
2130 {
2131 RTLIL::Cell *cell = addCell(name, ID($fair));
2132 cell->setPort(ID::A, sig_a);
2133 cell->setPort(ID(EN), sig_en);
2134 cell->set_src_attribute(src);
2135 return cell;
2136 }
2137
2138 RTLIL::Cell* RTLIL::Module::addCover(RTLIL::IdString name, RTLIL::SigSpec sig_a, RTLIL::SigSpec sig_en, const std::string &src)
2139 {
2140 RTLIL::Cell *cell = addCell(name, ID($cover));
2141 cell->setPort(ID::A, sig_a);
2142 cell->setPort(ID(EN), sig_en);
2143 cell->set_src_attribute(src);
2144 return cell;
2145 }
2146
2147 RTLIL::Cell* RTLIL::Module::addEquiv(RTLIL::IdString name, RTLIL::SigSpec sig_a, RTLIL::SigSpec sig_b, RTLIL::SigSpec sig_y, const std::string &src)
2148 {
2149 RTLIL::Cell *cell = addCell(name, ID($equiv));
2150 cell->setPort(ID::A, sig_a);
2151 cell->setPort(ID::B, sig_b);
2152 cell->setPort(ID::Y, sig_y);
2153 cell->set_src_attribute(src);
2154 return cell;
2155 }
2156
2157 RTLIL::Cell* RTLIL::Module::addSr(RTLIL::IdString name, RTLIL::SigSpec sig_set, RTLIL::SigSpec sig_clr, RTLIL::SigSpec sig_q, bool set_polarity, bool clr_polarity, const std::string &src)
2158 {
2159 RTLIL::Cell *cell = addCell(name, ID($sr));
2160 cell->parameters[ID(SET_POLARITY)] = set_polarity;
2161 cell->parameters[ID(CLR_POLARITY)] = clr_polarity;
2162 cell->parameters[ID(WIDTH)] = sig_q.size();
2163 cell->setPort(ID(SET), sig_set);
2164 cell->setPort(ID(CLR), sig_clr);
2165 cell->setPort(ID(Q), sig_q);
2166 cell->set_src_attribute(src);
2167 return cell;
2168 }
2169
2170 RTLIL::Cell* RTLIL::Module::addFf(RTLIL::IdString name, RTLIL::SigSpec sig_d, RTLIL::SigSpec sig_q, const std::string &src)
2171 {
2172 RTLIL::Cell *cell = addCell(name, ID($ff));
2173 cell->parameters[ID(WIDTH)] = sig_q.size();
2174 cell->setPort(ID(D), sig_d);
2175 cell->setPort(ID(Q), sig_q);
2176 cell->set_src_attribute(src);
2177 return cell;
2178 }
2179
2180 RTLIL::Cell* RTLIL::Module::addDff(RTLIL::IdString name, RTLIL::SigSpec sig_clk, RTLIL::SigSpec sig_d, RTLIL::SigSpec sig_q, bool clk_polarity, const std::string &src)
2181 {
2182 RTLIL::Cell *cell = addCell(name, ID($dff));
2183 cell->parameters[ID(CLK_POLARITY)] = clk_polarity;
2184 cell->parameters[ID(WIDTH)] = sig_q.size();
2185 cell->setPort(ID(CLK), sig_clk);
2186 cell->setPort(ID(D), sig_d);
2187 cell->setPort(ID(Q), sig_q);
2188 cell->set_src_attribute(src);
2189 return cell;
2190 }
2191
2192 RTLIL::Cell* RTLIL::Module::addDffe(RTLIL::IdString name, RTLIL::SigSpec sig_clk, RTLIL::SigSpec sig_en, RTLIL::SigSpec sig_d, RTLIL::SigSpec sig_q, bool clk_polarity, bool en_polarity, const std::string &src)
2193 {
2194 RTLIL::Cell *cell = addCell(name, ID($dffe));
2195 cell->parameters[ID(CLK_POLARITY)] = clk_polarity;
2196 cell->parameters[ID(EN_POLARITY)] = en_polarity;
2197 cell->parameters[ID(WIDTH)] = sig_q.size();
2198 cell->setPort(ID(CLK), sig_clk);
2199 cell->setPort(ID(EN), sig_en);
2200 cell->setPort(ID(D), sig_d);
2201 cell->setPort(ID(Q), sig_q);
2202 cell->set_src_attribute(src);
2203 return cell;
2204 }
2205
2206 RTLIL::Cell* RTLIL::Module::addDffsr(RTLIL::IdString name, RTLIL::SigSpec sig_clk, RTLIL::SigSpec sig_set, RTLIL::SigSpec sig_clr,
2207 RTLIL::SigSpec sig_d, RTLIL::SigSpec sig_q, bool clk_polarity, bool set_polarity, bool clr_polarity, const std::string &src)
2208 {
2209 RTLIL::Cell *cell = addCell(name, ID($dffsr));
2210 cell->parameters[ID(CLK_POLARITY)] = clk_polarity;
2211 cell->parameters[ID(SET_POLARITY)] = set_polarity;
2212 cell->parameters[ID(CLR_POLARITY)] = clr_polarity;
2213 cell->parameters[ID(WIDTH)] = sig_q.size();
2214 cell->setPort(ID(CLK), sig_clk);
2215 cell->setPort(ID(SET), sig_set);
2216 cell->setPort(ID(CLR), sig_clr);
2217 cell->setPort(ID(D), sig_d);
2218 cell->setPort(ID(Q), sig_q);
2219 cell->set_src_attribute(src);
2220 return cell;
2221 }
2222
2223 RTLIL::Cell* RTLIL::Module::addAdff(RTLIL::IdString name, RTLIL::SigSpec sig_clk, RTLIL::SigSpec sig_arst, RTLIL::SigSpec sig_d, RTLIL::SigSpec sig_q,
2224 RTLIL::Const arst_value, bool clk_polarity, bool arst_polarity, const std::string &src)
2225 {
2226 RTLIL::Cell *cell = addCell(name, ID($adff));
2227 cell->parameters[ID(CLK_POLARITY)] = clk_polarity;
2228 cell->parameters[ID(ARST_POLARITY)] = arst_polarity;
2229 cell->parameters[ID(ARST_VALUE)] = arst_value;
2230 cell->parameters[ID(WIDTH)] = sig_q.size();
2231 cell->setPort(ID(CLK), sig_clk);
2232 cell->setPort(ID(ARST), sig_arst);
2233 cell->setPort(ID(D), sig_d);
2234 cell->setPort(ID(Q), sig_q);
2235 cell->set_src_attribute(src);
2236 return cell;
2237 }
2238
2239 RTLIL::Cell* RTLIL::Module::addDlatch(RTLIL::IdString name, RTLIL::SigSpec sig_en, RTLIL::SigSpec sig_d, RTLIL::SigSpec sig_q, bool en_polarity, const std::string &src)
2240 {
2241 RTLIL::Cell *cell = addCell(name, ID($dlatch));
2242 cell->parameters[ID(EN_POLARITY)] = en_polarity;
2243 cell->parameters[ID(WIDTH)] = sig_q.size();
2244 cell->setPort(ID(EN), sig_en);
2245 cell->setPort(ID(D), sig_d);
2246 cell->setPort(ID(Q), sig_q);
2247 cell->set_src_attribute(src);
2248 return cell;
2249 }
2250
2251 RTLIL::Cell* RTLIL::Module::addDlatchsr(RTLIL::IdString name, RTLIL::SigSpec sig_en, RTLIL::SigSpec sig_set, RTLIL::SigSpec sig_clr,
2252 RTLIL::SigSpec sig_d, RTLIL::SigSpec sig_q, bool en_polarity, bool set_polarity, bool clr_polarity, const std::string &src)
2253 {
2254 RTLIL::Cell *cell = addCell(name, ID($dlatchsr));
2255 cell->parameters[ID(EN_POLARITY)] = en_polarity;
2256 cell->parameters[ID(SET_POLARITY)] = set_polarity;
2257 cell->parameters[ID(CLR_POLARITY)] = clr_polarity;
2258 cell->parameters[ID(WIDTH)] = sig_q.size();
2259 cell->setPort(ID(EN), sig_en);
2260 cell->setPort(ID(SET), sig_set);
2261 cell->setPort(ID(CLR), sig_clr);
2262 cell->setPort(ID(D), sig_d);
2263 cell->setPort(ID(Q), sig_q);
2264 cell->set_src_attribute(src);
2265 return cell;
2266 }
2267
2268 RTLIL::Cell* RTLIL::Module::addFfGate(RTLIL::IdString name, RTLIL::SigSpec sig_d, RTLIL::SigSpec sig_q, const std::string &src)
2269 {
2270 RTLIL::Cell *cell = addCell(name, ID($_FF_));
2271 cell->setPort(ID(D), sig_d);
2272 cell->setPort(ID(Q), sig_q);
2273 cell->set_src_attribute(src);
2274 return cell;
2275 }
2276
2277 RTLIL::Cell* RTLIL::Module::addDffGate(RTLIL::IdString name, RTLIL::SigSpec sig_clk, RTLIL::SigSpec sig_d, RTLIL::SigSpec sig_q, bool clk_polarity, const std::string &src)
2278 {
2279 RTLIL::Cell *cell = addCell(name, stringf("$_DFF_%c_", clk_polarity ? 'P' : 'N'));
2280 cell->setPort(ID(C), sig_clk);
2281 cell->setPort(ID(D), sig_d);
2282 cell->setPort(ID(Q), sig_q);
2283 cell->set_src_attribute(src);
2284 return cell;
2285 }
2286
2287 RTLIL::Cell* RTLIL::Module::addDffeGate(RTLIL::IdString name, RTLIL::SigSpec sig_clk, RTLIL::SigSpec sig_en, RTLIL::SigSpec sig_d, RTLIL::SigSpec sig_q, bool clk_polarity, bool en_polarity, const std::string &src)
2288 {
2289 RTLIL::Cell *cell = addCell(name, stringf("$_DFFE_%c%c_", clk_polarity ? 'P' : 'N', en_polarity ? 'P' : 'N'));
2290 cell->setPort(ID(C), sig_clk);
2291 cell->setPort(ID(E), sig_en);
2292 cell->setPort(ID(D), sig_d);
2293 cell->setPort(ID(Q), sig_q);
2294 cell->set_src_attribute(src);
2295 return cell;
2296 }
2297
2298 RTLIL::Cell* RTLIL::Module::addDffsrGate(RTLIL::IdString name, RTLIL::SigSpec sig_clk, RTLIL::SigSpec sig_set, RTLIL::SigSpec sig_clr,
2299 RTLIL::SigSpec sig_d, RTLIL::SigSpec sig_q, bool clk_polarity, bool set_polarity, bool clr_polarity, const std::string &src)
2300 {
2301 RTLIL::Cell *cell = addCell(name, stringf("$_DFFSR_%c%c%c_", clk_polarity ? 'P' : 'N', set_polarity ? 'P' : 'N', clr_polarity ? 'P' : 'N'));
2302 cell->setPort(ID(C), sig_clk);
2303 cell->setPort(ID(S), sig_set);
2304 cell->setPort(ID(R), sig_clr);
2305 cell->setPort(ID(D), sig_d);
2306 cell->setPort(ID(Q), sig_q);
2307 cell->set_src_attribute(src);
2308 return cell;
2309 }
2310
2311 RTLIL::Cell* RTLIL::Module::addAdffGate(RTLIL::IdString name, RTLIL::SigSpec sig_clk, RTLIL::SigSpec sig_arst, RTLIL::SigSpec sig_d, RTLIL::SigSpec sig_q,
2312 bool arst_value, bool clk_polarity, bool arst_polarity, const std::string &src)
2313 {
2314 RTLIL::Cell *cell = addCell(name, stringf("$_DFF_%c%c%c_", clk_polarity ? 'P' : 'N', arst_polarity ? 'P' : 'N', arst_value ? '1' : '0'));
2315 cell->setPort(ID(C), sig_clk);
2316 cell->setPort(ID(R), sig_arst);
2317 cell->setPort(ID(D), sig_d);
2318 cell->setPort(ID(Q), sig_q);
2319 cell->set_src_attribute(src);
2320 return cell;
2321 }
2322
2323 RTLIL::Cell* RTLIL::Module::addDlatchGate(RTLIL::IdString name, RTLIL::SigSpec sig_en, RTLIL::SigSpec sig_d, RTLIL::SigSpec sig_q, bool en_polarity, const std::string &src)
2324 {
2325 RTLIL::Cell *cell = addCell(name, stringf("$_DLATCH_%c_", en_polarity ? 'P' : 'N'));
2326 cell->setPort(ID(E), sig_en);
2327 cell->setPort(ID(D), sig_d);
2328 cell->setPort(ID(Q), sig_q);
2329 cell->set_src_attribute(src);
2330 return cell;
2331 }
2332
2333 RTLIL::Cell* RTLIL::Module::addDlatchsrGate(RTLIL::IdString name, RTLIL::SigSpec sig_en, RTLIL::SigSpec sig_set, RTLIL::SigSpec sig_clr,
2334 RTLIL::SigSpec sig_d, RTLIL::SigSpec sig_q, bool en_polarity, bool set_polarity, bool clr_polarity, const std::string &src)
2335 {
2336 RTLIL::Cell *cell = addCell(name, stringf("$_DLATCHSR_%c%c%c_", en_polarity ? 'P' : 'N', set_polarity ? 'P' : 'N', clr_polarity ? 'P' : 'N'));
2337 cell->setPort(ID(E), sig_en);
2338 cell->setPort(ID(S), sig_set);
2339 cell->setPort(ID(R), sig_clr);
2340 cell->setPort(ID(D), sig_d);
2341 cell->setPort(ID(Q), sig_q);
2342 cell->set_src_attribute(src);
2343 return cell;
2344 }
2345
2346 RTLIL::SigSpec RTLIL::Module::Anyconst(RTLIL::IdString name, int width, const std::string &src)
2347 {
2348 RTLIL::SigSpec sig = addWire(NEW_ID, width);
2349 Cell *cell = addCell(name, ID($anyconst));
2350 cell->setParam(ID(WIDTH), width);
2351 cell->setPort(ID::Y, sig);
2352 cell->set_src_attribute(src);
2353 return sig;
2354 }
2355
2356 RTLIL::SigSpec RTLIL::Module::Anyseq(RTLIL::IdString name, int width, const std::string &src)
2357 {
2358 RTLIL::SigSpec sig = addWire(NEW_ID, width);
2359 Cell *cell = addCell(name, ID($anyseq));
2360 cell->setParam(ID(WIDTH), width);
2361 cell->setPort(ID::Y, sig);
2362 cell->set_src_attribute(src);
2363 return sig;
2364 }
2365
2366 RTLIL::SigSpec RTLIL::Module::Allconst(RTLIL::IdString name, int width, const std::string &src)
2367 {
2368 RTLIL::SigSpec sig = addWire(NEW_ID, width);
2369 Cell *cell = addCell(name, ID($allconst));
2370 cell->setParam(ID(WIDTH), width);
2371 cell->setPort(ID::Y, sig);
2372 cell->set_src_attribute(src);
2373 return sig;
2374 }
2375
2376 RTLIL::SigSpec RTLIL::Module::Allseq(RTLIL::IdString name, int width, const std::string &src)
2377 {
2378 RTLIL::SigSpec sig = addWire(NEW_ID, width);
2379 Cell *cell = addCell(name, ID($allseq));
2380 cell->setParam(ID(WIDTH), width);
2381 cell->setPort(ID::Y, sig);
2382 cell->set_src_attribute(src);
2383 return sig;
2384 }
2385
2386 RTLIL::SigSpec RTLIL::Module::Initstate(RTLIL::IdString name, const std::string &src)
2387 {
2388 RTLIL::SigSpec sig = addWire(NEW_ID);
2389 Cell *cell = addCell(name, ID($initstate));
2390 cell->setPort(ID::Y, sig);
2391 cell->set_src_attribute(src);
2392 return sig;
2393 }
2394
2395 RTLIL::Wire::Wire()
2396 {
2397 static unsigned int hashidx_count = 123456789;
2398 hashidx_count = mkhash_xorshift(hashidx_count);
2399 hashidx_ = hashidx_count;
2400
2401 module = nullptr;
2402 width = 1;
2403 start_offset = 0;
2404 port_id = 0;
2405 port_input = false;
2406 port_output = false;
2407 upto = false;
2408
2409 #ifdef WITH_PYTHON
2410 RTLIL::Wire::get_all_wires()->insert(std::pair<unsigned int, RTLIL::Wire*>(hashidx_, this));
2411 #endif
2412 }
2413
2414 RTLIL::Wire::~Wire()
2415 {
2416 #ifdef WITH_PYTHON
2417 RTLIL::Wire::get_all_wires()->erase(hashidx_);
2418 #endif
2419 }
2420
2421 #ifdef WITH_PYTHON
2422 static std::map<unsigned int, RTLIL::Wire*> all_wires;
2423 std::map<unsigned int, RTLIL::Wire*> *RTLIL::Wire::get_all_wires(void)
2424 {
2425 return &all_wires;
2426 }
2427 #endif
2428
2429 RTLIL::Memory::Memory()
2430 {
2431 static unsigned int hashidx_count = 123456789;
2432 hashidx_count = mkhash_xorshift(hashidx_count);
2433 hashidx_ = hashidx_count;
2434
2435 width = 1;
2436 start_offset = 0;
2437 size = 0;
2438 #ifdef WITH_PYTHON
2439 RTLIL::Memory::get_all_memorys()->insert(std::pair<unsigned int, RTLIL::Memory*>(hashidx_, this));
2440 #endif
2441 }
2442
2443 RTLIL::Cell::Cell() : module(nullptr)
2444 {
2445 static unsigned int hashidx_count = 123456789;
2446 hashidx_count = mkhash_xorshift(hashidx_count);
2447 hashidx_ = hashidx_count;
2448
2449 // log("#memtrace# %p\n", this);
2450 memhasher();
2451
2452 #ifdef WITH_PYTHON
2453 RTLIL::Cell::get_all_cells()->insert(std::pair<unsigned int, RTLIL::Cell*>(hashidx_, this));
2454 #endif
2455 }
2456
2457 RTLIL::Cell::~Cell()
2458 {
2459 #ifdef WITH_PYTHON
2460 RTLIL::Cell::get_all_cells()->erase(hashidx_);
2461 #endif
2462 }
2463
2464 #ifdef WITH_PYTHON
2465 static std::map<unsigned int, RTLIL::Cell*> all_cells;
2466 std::map<unsigned int, RTLIL::Cell*> *RTLIL::Cell::get_all_cells(void)
2467 {
2468 return &all_cells;
2469 }
2470 #endif
2471
2472 bool RTLIL::Cell::hasPort(RTLIL::IdString portname) const
2473 {
2474 return connections_.count(portname) != 0;
2475 }
2476
2477 void RTLIL::Cell::unsetPort(RTLIL::IdString portname)
2478 {
2479 RTLIL::SigSpec signal;
2480 auto conn_it = connections_.find(portname);
2481
2482 if (conn_it != connections_.end())
2483 {
2484 for (auto mon : module->monitors)
2485 mon->notify_connect(this, conn_it->first, conn_it->second, signal);
2486
2487 if (module->design)
2488 for (auto mon : module->design->monitors)
2489 mon->notify_connect(this, conn_it->first, conn_it->second, signal);
2490
2491 if (yosys_xtrace) {
2492 log("#X# Unconnect %s.%s.%s\n", log_id(this->module), log_id(this), log_id(portname));
2493 log_backtrace("-X- ", yosys_xtrace-1);
2494 }
2495
2496 connections_.erase(conn_it);
2497 }
2498 }
2499
2500 void RTLIL::Cell::setPort(RTLIL::IdString portname, RTLIL::SigSpec signal)
2501 {
2502 auto conn_it = connections_.find(portname);
2503
2504 if (conn_it == connections_.end()) {
2505 connections_[portname] = RTLIL::SigSpec();
2506 conn_it = connections_.find(portname);
2507 log_assert(conn_it != connections_.end());
2508 } else
2509 if (conn_it->second == signal)
2510 return;
2511
2512 for (auto mon : module->monitors)
2513 mon->notify_connect(this, conn_it->first, conn_it->second, signal);
2514
2515 if (module->design)
2516 for (auto mon : module->design->monitors)
2517 mon->notify_connect(this, conn_it->first, conn_it->second, signal);
2518
2519 if (yosys_xtrace) {
2520 log("#X# Connect %s.%s.%s = %s (%d)\n", log_id(this->module), log_id(this), log_id(portname), log_signal(signal), GetSize(signal));
2521 log_backtrace("-X- ", yosys_xtrace-1);
2522 }
2523
2524 conn_it->second = signal;
2525 }
2526
2527 const RTLIL::SigSpec &RTLIL::Cell::getPort(RTLIL::IdString portname) const
2528 {
2529 return connections_.at(portname);
2530 }
2531
2532 const dict<RTLIL::IdString, RTLIL::SigSpec> &RTLIL::Cell::connections() const
2533 {
2534 return connections_;
2535 }
2536
2537 bool RTLIL::Cell::known() const
2538 {
2539 if (yosys_celltypes.cell_known(type))
2540 return true;
2541 if (module && module->design && module->design->module(type))
2542 return true;
2543 return false;
2544 }
2545
2546 bool RTLIL::Cell::input(RTLIL::IdString portname) const
2547 {
2548 if (yosys_celltypes.cell_known(type))
2549 return yosys_celltypes.cell_input(type, portname);
2550 if (module && module->design) {
2551 RTLIL::Module *m = module->design->module(type);
2552 RTLIL::Wire *w = m ? m->wire(portname) : nullptr;
2553 return w && w->port_input;
2554 }
2555 return false;
2556 }
2557
2558 bool RTLIL::Cell::output(RTLIL::IdString portname) const
2559 {
2560 if (yosys_celltypes.cell_known(type))
2561 return yosys_celltypes.cell_output(type, portname);
2562 if (module && module->design) {
2563 RTLIL::Module *m = module->design->module(type);
2564 RTLIL::Wire *w = m ? m->wire(portname) : nullptr;
2565 return w && w->port_output;
2566 }
2567 return false;
2568 }
2569
2570 bool RTLIL::Cell::hasParam(RTLIL::IdString paramname) const
2571 {
2572 return parameters.count(paramname) != 0;
2573 }
2574
2575 void RTLIL::Cell::unsetParam(RTLIL::IdString paramname)
2576 {
2577 parameters.erase(paramname);
2578 }
2579
2580 void RTLIL::Cell::setParam(RTLIL::IdString paramname, RTLIL::Const value)
2581 {
2582 parameters[paramname] = value;
2583 }
2584
2585 const RTLIL::Const &RTLIL::Cell::getParam(RTLIL::IdString paramname) const
2586 {
2587 return parameters.at(paramname);
2588 }
2589
2590 void RTLIL::Cell::sort()
2591 {
2592 connections_.sort(sort_by_id_str());
2593 parameters.sort(sort_by_id_str());
2594 attributes.sort(sort_by_id_str());
2595 }
2596
2597 void RTLIL::Cell::check()
2598 {
2599 #ifndef NDEBUG
2600 InternalCellChecker checker(NULL, this);
2601 checker.check();
2602 #endif
2603 }
2604
2605 void RTLIL::Cell::fixup_parameters(bool set_a_signed, bool set_b_signed)
2606 {
2607 if (!type.begins_with("$") || type.begins_with("$_") || type.begins_with("$paramod") || type.begins_with("$fmcombine") ||
2608 type.begins_with("$verific$") || type.begins_with("$array:") || type.begins_with("$extern:"))
2609 return;
2610
2611 if (type == ID($mux) || type == ID($pmux)) {
2612 parameters[ID(WIDTH)] = GetSize(connections_[ID::Y]);
2613 if (type == ID($pmux))
2614 parameters[ID(S_WIDTH)] = GetSize(connections_[ID(S)]);
2615 check();
2616 return;
2617 }
2618
2619 if (type == ID($lut) || type == ID($sop)) {
2620 parameters[ID(WIDTH)] = GetSize(connections_[ID::A]);
2621 return;
2622 }
2623
2624 if (type == ID($fa)) {
2625 parameters[ID(WIDTH)] = GetSize(connections_[ID::Y]);
2626 return;
2627 }
2628
2629 if (type == ID($lcu)) {
2630 parameters[ID(WIDTH)] = GetSize(connections_[ID(CO)]);
2631 return;
2632 }
2633
2634 bool signedness_ab = !type.in(ID($slice), ID($concat), ID($macc));
2635
2636 if (connections_.count(ID::A)) {
2637 if (signedness_ab) {
2638 if (set_a_signed)
2639 parameters[ID(A_SIGNED)] = true;
2640 else if (parameters.count(ID(A_SIGNED)) == 0)
2641 parameters[ID(A_SIGNED)] = false;
2642 }
2643 parameters[ID(A_WIDTH)] = GetSize(connections_[ID::A]);
2644 }
2645
2646 if (connections_.count(ID::B)) {
2647 if (signedness_ab) {
2648 if (set_b_signed)
2649 parameters[ID(B_SIGNED)] = true;
2650 else if (parameters.count(ID(B_SIGNED)) == 0)
2651 parameters[ID(B_SIGNED)] = false;
2652 }
2653 parameters[ID(B_WIDTH)] = GetSize(connections_[ID::B]);
2654 }
2655
2656 if (connections_.count(ID::Y))
2657 parameters[ID(Y_WIDTH)] = GetSize(connections_[ID::Y]);
2658
2659 if (connections_.count(ID(Q)))
2660 parameters[ID(WIDTH)] = GetSize(connections_[ID(Q)]);
2661
2662 check();
2663 }
2664
2665 RTLIL::SigChunk::SigChunk()
2666 {
2667 wire = NULL;
2668 width = 0;
2669 offset = 0;
2670 }
2671
2672 RTLIL::SigChunk::SigChunk(const RTLIL::Const &value)
2673 {
2674 wire = NULL;
2675 data = value.bits;
2676 width = GetSize(data);
2677 offset = 0;
2678 }
2679
2680 RTLIL::SigChunk::SigChunk(RTLIL::Wire *wire)
2681 {
2682 log_assert(wire != nullptr);
2683 this->wire = wire;
2684 this->width = wire->width;
2685 this->offset = 0;
2686 }
2687
2688 RTLIL::SigChunk::SigChunk(RTLIL::Wire *wire, int offset, int width)
2689 {
2690 log_assert(wire != nullptr);
2691 this->wire = wire;
2692 this->width = width;
2693 this->offset = offset;
2694 }
2695
2696 RTLIL::SigChunk::SigChunk(const std::string &str)
2697 {
2698 wire = NULL;
2699 data = RTLIL::Const(str).bits;
2700 width = GetSize(data);
2701 offset = 0;
2702 }
2703
2704 RTLIL::SigChunk::SigChunk(int val, int width)
2705 {
2706 wire = NULL;
2707 data = RTLIL::Const(val, width).bits;
2708 this->width = GetSize(data);
2709 offset = 0;
2710 }
2711
2712 RTLIL::SigChunk::SigChunk(RTLIL::State bit, int width)
2713 {
2714 wire = NULL;
2715 data = RTLIL::Const(bit, width).bits;
2716 this->width = GetSize(data);
2717 offset = 0;
2718 }
2719
2720 RTLIL::SigChunk::SigChunk(RTLIL::SigBit bit)
2721 {
2722 wire = bit.wire;
2723 offset = 0;
2724 if (wire == NULL)
2725 data = RTLIL::Const(bit.data).bits;
2726 else
2727 offset = bit.offset;
2728 width = 1;
2729 }
2730
2731 RTLIL::SigChunk::SigChunk(const RTLIL::SigChunk &sigchunk) : data(sigchunk.data)
2732 {
2733 wire = sigchunk.wire;
2734 data = sigchunk.data;
2735 width = sigchunk.width;
2736 offset = sigchunk.offset;
2737 }
2738
2739 RTLIL::SigChunk RTLIL::SigChunk::extract(int offset, int length) const
2740 {
2741 RTLIL::SigChunk ret;
2742 if (wire) {
2743 ret.wire = wire;
2744 ret.offset = this->offset + offset;
2745 ret.width = length;
2746 } else {
2747 for (int i = 0; i < length; i++)
2748 ret.data.push_back(data[offset+i]);
2749 ret.width = length;
2750 }
2751 return ret;
2752 }
2753
2754 bool RTLIL::SigChunk::operator <(const RTLIL::SigChunk &other) const
2755 {
2756 if (wire && other.wire)
2757 if (wire->name != other.wire->name)
2758 return wire->name < other.wire->name;
2759
2760 if (wire != other.wire)
2761 return wire < other.wire;
2762
2763 if (offset != other.offset)
2764 return offset < other.offset;
2765
2766 if (width != other.width)
2767 return width < other.width;
2768
2769 return data < other.data;
2770 }
2771
2772 bool RTLIL::SigChunk::operator ==(const RTLIL::SigChunk &other) const
2773 {
2774 return wire == other.wire && width == other.width && offset == other.offset && data == other.data;
2775 }
2776
2777 bool RTLIL::SigChunk::operator !=(const RTLIL::SigChunk &other) const
2778 {
2779 if (*this == other)
2780 return false;
2781 return true;
2782 }
2783
2784 RTLIL::SigSpec::SigSpec()
2785 {
2786 width_ = 0;
2787 hash_ = 0;
2788 }
2789
2790 RTLIL::SigSpec::SigSpec(const RTLIL::SigSpec &other)
2791 {
2792 *this = other;
2793 }
2794
2795 RTLIL::SigSpec::SigSpec(std::initializer_list<RTLIL::SigSpec> parts)
2796 {
2797 cover("kernel.rtlil.sigspec.init.list");
2798
2799 width_ = 0;
2800 hash_ = 0;
2801
2802 std::vector<RTLIL::SigSpec> parts_vec(parts.begin(), parts.end());
2803 for (auto it = parts_vec.rbegin(); it != parts_vec.rend(); it++)
2804 append(*it);
2805 }
2806
2807 const RTLIL::SigSpec &RTLIL::SigSpec::operator=(const RTLIL::SigSpec &other)
2808 {
2809 cover("kernel.rtlil.sigspec.assign");
2810
2811 width_ = other.width_;
2812 hash_ = other.hash_;
2813 chunks_ = other.chunks_;
2814 bits_.clear();
2815
2816 if (!other.bits_.empty())
2817 {
2818 RTLIL::SigChunk *last = NULL;
2819 int last_end_offset = 0;
2820
2821 for (auto &bit : other.bits_) {
2822 if (last && bit.wire == last->wire) {
2823 if (bit.wire == NULL) {
2824 last->data.push_back(bit.data);
2825 last->width++;
2826 continue;
2827 } else if (last_end_offset == bit.offset) {
2828 last_end_offset++;
2829 last->width++;
2830 continue;
2831 }
2832 }
2833 chunks_.push_back(bit);
2834 last = &chunks_.back();
2835 last_end_offset = bit.offset + 1;
2836 }
2837
2838 check();
2839 }
2840
2841 return *this;
2842 }
2843
2844 RTLIL::SigSpec::SigSpec(const RTLIL::Const &value)
2845 {
2846 cover("kernel.rtlil.sigspec.init.const");
2847
2848 chunks_.push_back(RTLIL::SigChunk(value));
2849 width_ = chunks_.back().width;
2850 hash_ = 0;
2851 check();
2852 }
2853
2854 RTLIL::SigSpec::SigSpec(const RTLIL::SigChunk &chunk)
2855 {
2856 cover("kernel.rtlil.sigspec.init.chunk");
2857
2858 chunks_.push_back(chunk);
2859 width_ = chunks_.back().width;
2860 hash_ = 0;
2861 check();
2862 }
2863
2864 RTLIL::SigSpec::SigSpec(RTLIL::Wire *wire)
2865 {
2866 cover("kernel.rtlil.sigspec.init.wire");
2867
2868 chunks_.push_back(RTLIL::SigChunk(wire));
2869 width_ = chunks_.back().width;
2870 hash_ = 0;
2871 check();
2872 }
2873
2874 RTLIL::SigSpec::SigSpec(RTLIL::Wire *wire, int offset, int width)
2875 {
2876 cover("kernel.rtlil.sigspec.init.wire_part");
2877
2878 chunks_.push_back(RTLIL::SigChunk(wire, offset, width));
2879 width_ = chunks_.back().width;
2880 hash_ = 0;
2881 check();
2882 }
2883
2884 RTLIL::SigSpec::SigSpec(const std::string &str)
2885 {
2886 cover("kernel.rtlil.sigspec.init.str");
2887
2888 chunks_.push_back(RTLIL::SigChunk(str));
2889 width_ = chunks_.back().width;
2890 hash_ = 0;
2891 check();
2892 }
2893
2894 RTLIL::SigSpec::SigSpec(int val, int width)
2895 {
2896 cover("kernel.rtlil.sigspec.init.int");
2897
2898 chunks_.push_back(RTLIL::SigChunk(val, width));
2899 width_ = width;
2900 hash_ = 0;
2901 check();
2902 }
2903
2904 RTLIL::SigSpec::SigSpec(RTLIL::State bit, int width)
2905 {
2906 cover("kernel.rtlil.sigspec.init.state");
2907
2908 chunks_.push_back(RTLIL::SigChunk(bit, width));
2909 width_ = width;
2910 hash_ = 0;
2911 check();
2912 }
2913
2914 RTLIL::SigSpec::SigSpec(RTLIL::SigBit bit, int width)
2915 {
2916 cover("kernel.rtlil.sigspec.init.bit");
2917
2918 if (bit.wire == NULL)
2919 chunks_.push_back(RTLIL::SigChunk(bit.data, width));
2920 else
2921 for (int i = 0; i < width; i++)
2922 chunks_.push_back(bit);
2923 width_ = width;
2924 hash_ = 0;
2925 check();
2926 }
2927
2928 RTLIL::SigSpec::SigSpec(std::vector<RTLIL::SigChunk> chunks)
2929 {
2930 cover("kernel.rtlil.sigspec.init.stdvec_chunks");
2931
2932 width_ = 0;
2933 hash_ = 0;
2934 for (auto &c : chunks)
2935 append(c);
2936 check();
2937 }
2938
2939 RTLIL::SigSpec::SigSpec(std::vector<RTLIL::SigBit> bits)
2940 {
2941 cover("kernel.rtlil.sigspec.init.stdvec_bits");
2942
2943 width_ = 0;
2944 hash_ = 0;
2945 for (auto &bit : bits)
2946 append_bit(bit);
2947 check();
2948 }
2949
2950 RTLIL::SigSpec::SigSpec(pool<RTLIL::SigBit> bits)
2951 {
2952 cover("kernel.rtlil.sigspec.init.pool_bits");
2953
2954 width_ = 0;
2955 hash_ = 0;
2956 for (auto &bit : bits)
2957 append_bit(bit);
2958 check();
2959 }
2960
2961 RTLIL::SigSpec::SigSpec(std::set<RTLIL::SigBit> bits)
2962 {
2963 cover("kernel.rtlil.sigspec.init.stdset_bits");
2964
2965 width_ = 0;
2966 hash_ = 0;
2967 for (auto &bit : bits)
2968 append_bit(bit);
2969 check();
2970 }
2971
2972 RTLIL::SigSpec::SigSpec(bool bit)
2973 {
2974 cover("kernel.rtlil.sigspec.init.bool");
2975
2976 width_ = 0;
2977 hash_ = 0;
2978 append_bit(bit);
2979 check();
2980 }
2981
2982 void RTLIL::SigSpec::pack() const
2983 {
2984 RTLIL::SigSpec *that = (RTLIL::SigSpec*)this;
2985
2986 if (that->bits_.empty())
2987 return;
2988
2989 cover("kernel.rtlil.sigspec.convert.pack");
2990 log_assert(that->chunks_.empty());
2991
2992 std::vector<RTLIL::SigBit> old_bits;
2993 old_bits.swap(that->bits_);
2994
2995 RTLIL::SigChunk *last = NULL;
2996 int last_end_offset = 0;
2997
2998 for (auto &bit : old_bits) {
2999 if (last && bit.wire == last->wire) {
3000 if (bit.wire == NULL) {
3001 last->data.push_back(bit.data);
3002 last->width++;
3003 continue;
3004 } else if (last_end_offset == bit.offset) {
3005 last_end_offset++;
3006 last->width++;
3007 continue;
3008 }
3009 }
3010 that->chunks_.push_back(bit);
3011 last = &that->chunks_.back();
3012 last_end_offset = bit.offset + 1;
3013 }
3014
3015 check();
3016 }
3017
3018 void RTLIL::SigSpec::unpack() const
3019 {
3020 RTLIL::SigSpec *that = (RTLIL::SigSpec*)this;
3021
3022 if (that->chunks_.empty())
3023 return;
3024
3025 cover("kernel.rtlil.sigspec.convert.unpack");
3026 log_assert(that->bits_.empty());
3027
3028 that->bits_.reserve(that->width_);
3029 for (auto &c : that->chunks_)
3030 for (int i = 0; i < c.width; i++)
3031 that->bits_.push_back(RTLIL::SigBit(c, i));
3032
3033 that->chunks_.clear();
3034 that->hash_ = 0;
3035 }
3036
3037 void RTLIL::SigSpec::updhash() const
3038 {
3039 RTLIL::SigSpec *that = (RTLIL::SigSpec*)this;
3040
3041 if (that->hash_ != 0)
3042 return;
3043
3044 cover("kernel.rtlil.sigspec.hash");
3045 that->pack();
3046
3047 that->hash_ = mkhash_init;
3048 for (auto &c : that->chunks_)
3049 if (c.wire == NULL) {
3050 for (auto &v : c.data)
3051 that->hash_ = mkhash(that->hash_, v);
3052 } else {
3053 that->hash_ = mkhash(that->hash_, c.wire->name.index_);
3054 that->hash_ = mkhash(that->hash_, c.offset);
3055 that->hash_ = mkhash(that->hash_, c.width);
3056 }
3057
3058 if (that->hash_ == 0)
3059 that->hash_ = 1;
3060 }
3061
3062 void RTLIL::SigSpec::sort()
3063 {
3064 unpack();
3065 cover("kernel.rtlil.sigspec.sort");
3066 std::sort(bits_.begin(), bits_.end());
3067 }
3068
3069 void RTLIL::SigSpec::sort_and_unify()
3070 {
3071 unpack();
3072 cover("kernel.rtlil.sigspec.sort_and_unify");
3073
3074 // A copy of the bits vector is used to prevent duplicating the logic from
3075 // SigSpec::SigSpec(std::vector<SigBit>). This incurrs an extra copy but
3076 // that isn't showing up as significant in profiles.
3077 std::vector<SigBit> unique_bits = bits_;
3078 std::sort(unique_bits.begin(), unique_bits.end());
3079 auto last = std::unique(unique_bits.begin(), unique_bits.end());
3080 unique_bits.erase(last, unique_bits.end());
3081
3082 *this = unique_bits;
3083 }
3084
3085 void RTLIL::SigSpec::replace(const RTLIL::SigSpec &pattern, const RTLIL::SigSpec &with)
3086 {
3087 replace(pattern, with, this);
3088 }
3089
3090 void RTLIL::SigSpec::replace(const RTLIL::SigSpec &pattern, const RTLIL::SigSpec &with, RTLIL::SigSpec *other) const
3091 {
3092 log_assert(other != NULL);
3093 log_assert(width_ == other->width_);
3094 log_assert(pattern.width_ == with.width_);
3095
3096 pattern.unpack();
3097 with.unpack();
3098 unpack();
3099 other->unpack();
3100
3101 for (int i = 0; i < GetSize(pattern.bits_); i++) {
3102 if (pattern.bits_[i].wire != NULL) {
3103 for (int j = 0; j < GetSize(bits_); j++) {
3104 if (bits_[j] == pattern.bits_[i]) {
3105 other->bits_[j] = with.bits_[i];
3106 }
3107 }
3108 }
3109 }
3110
3111 other->check();
3112 }
3113
3114 void RTLIL::SigSpec::replace(const dict<RTLIL::SigBit, RTLIL::SigBit> &rules)
3115 {
3116 replace(rules, this);
3117 }
3118
3119 void RTLIL::SigSpec::replace(const dict<RTLIL::SigBit, RTLIL::SigBit> &rules, RTLIL::SigSpec *other) const
3120 {
3121 cover("kernel.rtlil.sigspec.replace_dict");
3122
3123 log_assert(other != NULL);
3124 log_assert(width_ == other->width_);
3125
3126 if (rules.empty()) return;
3127 unpack();
3128 other->unpack();
3129
3130 for (int i = 0; i < GetSize(bits_); i++) {
3131 auto it = rules.find(bits_[i]);
3132 if (it != rules.end())
3133 other->bits_[i] = it->second;
3134 }
3135
3136 other->check();
3137 }
3138
3139 void RTLIL::SigSpec::replace(const std::map<RTLIL::SigBit, RTLIL::SigBit> &rules)
3140 {
3141 replace(rules, this);
3142 }
3143
3144 void RTLIL::SigSpec::replace(const std::map<RTLIL::SigBit, RTLIL::SigBit> &rules, RTLIL::SigSpec *other) const
3145 {
3146 cover("kernel.rtlil.sigspec.replace_map");
3147
3148 log_assert(other != NULL);
3149 log_assert(width_ == other->width_);
3150
3151 if (rules.empty()) return;
3152 unpack();
3153 other->unpack();
3154
3155 for (int i = 0; i < GetSize(bits_); i++) {
3156 auto it = rules.find(bits_[i]);
3157 if (it != rules.end())
3158 other->bits_[i] = it->second;
3159 }
3160
3161 other->check();
3162 }
3163
3164 void RTLIL::SigSpec::remove(const RTLIL::SigSpec &pattern)
3165 {
3166 remove2(pattern, NULL);
3167 }
3168
3169 void RTLIL::SigSpec::remove(const RTLIL::SigSpec &pattern, RTLIL::SigSpec *other) const
3170 {
3171 RTLIL::SigSpec tmp = *this;
3172 tmp.remove2(pattern, other);
3173 }
3174
3175 void RTLIL::SigSpec::remove2(const RTLIL::SigSpec &pattern, RTLIL::SigSpec *other)
3176 {
3177 if (other)
3178 cover("kernel.rtlil.sigspec.remove_other");
3179 else
3180 cover("kernel.rtlil.sigspec.remove");
3181
3182 unpack();
3183 if (other != NULL) {
3184 log_assert(width_ == other->width_);
3185 other->unpack();
3186 }
3187
3188 for (int i = GetSize(bits_) - 1; i >= 0; i--)
3189 {
3190 if (bits_[i].wire == NULL) continue;
3191
3192 for (auto &pattern_chunk : pattern.chunks())
3193 if (bits_[i].wire == pattern_chunk.wire &&
3194 bits_[i].offset >= pattern_chunk.offset &&
3195 bits_[i].offset < pattern_chunk.offset + pattern_chunk.width) {
3196 bits_.erase(bits_.begin() + i);
3197 width_--;
3198 if (other != NULL) {
3199 other->bits_.erase(other->bits_.begin() + i);
3200 other->width_--;
3201 }
3202 break;
3203 }
3204 }
3205
3206 check();
3207 }
3208
3209 void RTLIL::SigSpec::remove(const pool<RTLIL::SigBit> &pattern)
3210 {
3211 remove2(pattern, NULL);
3212 }
3213
3214 void RTLIL::SigSpec::remove(const pool<RTLIL::SigBit> &pattern, RTLIL::SigSpec *other) const
3215 {
3216 RTLIL::SigSpec tmp = *this;
3217 tmp.remove2(pattern, other);
3218 }
3219
3220 void RTLIL::SigSpec::remove2(const pool<RTLIL::SigBit> &pattern, RTLIL::SigSpec *other)
3221 {
3222 if (other)
3223 cover("kernel.rtlil.sigspec.remove_other");
3224 else
3225 cover("kernel.rtlil.sigspec.remove");
3226
3227 unpack();
3228
3229 if (other != NULL) {
3230 log_assert(width_ == other->width_);
3231 other->unpack();
3232 }
3233
3234 for (int i = GetSize(bits_) - 1; i >= 0; i--) {
3235 if (bits_[i].wire != NULL && pattern.count(bits_[i])) {
3236 bits_.erase(bits_.begin() + i);
3237 width_--;
3238 if (other != NULL) {
3239 other->bits_.erase(other->bits_.begin() + i);
3240 other->width_--;
3241 }
3242 }
3243 }
3244
3245 check();
3246 }
3247
3248 void RTLIL::SigSpec::remove2(const std::set<RTLIL::SigBit> &pattern, RTLIL::SigSpec *other)
3249 {
3250 if (other)
3251 cover("kernel.rtlil.sigspec.remove_other");
3252 else
3253 cover("kernel.rtlil.sigspec.remove");
3254
3255 unpack();
3256
3257 if (other != NULL) {
3258 log_assert(width_ == other->width_);
3259 other->unpack();
3260 }
3261
3262 for (int i = GetSize(bits_) - 1; i >= 0; i--) {
3263 if (bits_[i].wire != NULL && pattern.count(bits_[i])) {
3264 bits_.erase(bits_.begin() + i);
3265 width_--;
3266 if (other != NULL) {
3267 other->bits_.erase(other->bits_.begin() + i);
3268 other->width_--;
3269 }
3270 }
3271 }
3272
3273 check();
3274 }
3275
3276 RTLIL::SigSpec RTLIL::SigSpec::extract(const RTLIL::SigSpec &pattern, const RTLIL::SigSpec *other) const
3277 {
3278 if (other)
3279 cover("kernel.rtlil.sigspec.extract_other");
3280 else
3281 cover("kernel.rtlil.sigspec.extract");
3282
3283 log_assert(other == NULL || width_ == other->width_);
3284
3285 RTLIL::SigSpec ret;
3286 std::vector<RTLIL::SigBit> bits_match = to_sigbit_vector();
3287
3288 for (auto& pattern_chunk : pattern.chunks()) {
3289 if (other) {
3290 std::vector<RTLIL::SigBit> bits_other = other->to_sigbit_vector();
3291 for (int i = 0; i < width_; i++)
3292 if (bits_match[i].wire &&
3293 bits_match[i].wire == pattern_chunk.wire &&
3294 bits_match[i].offset >= pattern_chunk.offset &&
3295 bits_match[i].offset < pattern_chunk.offset + pattern_chunk.width)
3296 ret.append_bit(bits_other[i]);
3297 } else {
3298 for (int i = 0; i < width_; i++)
3299 if (bits_match[i].wire &&
3300 bits_match[i].wire == pattern_chunk.wire &&
3301 bits_match[i].offset >= pattern_chunk.offset &&
3302 bits_match[i].offset < pattern_chunk.offset + pattern_chunk.width)
3303 ret.append_bit(bits_match[i]);
3304 }
3305 }
3306
3307 ret.check();
3308 return ret;
3309 }
3310
3311 RTLIL::SigSpec RTLIL::SigSpec::extract(const pool<RTLIL::SigBit> &pattern, const RTLIL::SigSpec *other) const
3312 {
3313 if (other)
3314 cover("kernel.rtlil.sigspec.extract_other");
3315 else
3316 cover("kernel.rtlil.sigspec.extract");
3317
3318 log_assert(other == NULL || width_ == other->width_);
3319
3320 std::vector<RTLIL::SigBit> bits_match = to_sigbit_vector();
3321 RTLIL::SigSpec ret;
3322
3323 if (other) {
3324 std::vector<RTLIL::SigBit> bits_other = other->to_sigbit_vector();
3325 for (int i = 0; i < width_; i++)
3326 if (bits_match[i].wire && pattern.count(bits_match[i]))
3327 ret.append_bit(bits_other[i]);
3328 } else {
3329 for (int i = 0; i < width_; i++)
3330 if (bits_match[i].wire && pattern.count(bits_match[i]))
3331 ret.append_bit(bits_match[i]);
3332 }
3333
3334 ret.check();
3335 return ret;
3336 }
3337
3338 void RTLIL::SigSpec::replace(int offset, const RTLIL::SigSpec &with)
3339 {
3340 cover("kernel.rtlil.sigspec.replace_pos");
3341
3342 unpack();
3343 with.unpack();
3344
3345 log_assert(offset >= 0);
3346 log_assert(with.width_ >= 0);
3347 log_assert(offset+with.width_ <= width_);
3348
3349 for (int i = 0; i < with.width_; i++)
3350 bits_.at(offset + i) = with.bits_.at(i);
3351
3352 check();
3353 }
3354
3355 void RTLIL::SigSpec::remove_const()
3356 {
3357 if (packed())
3358 {
3359 cover("kernel.rtlil.sigspec.remove_const.packed");
3360
3361 std::vector<RTLIL::SigChunk> new_chunks;
3362 new_chunks.reserve(GetSize(chunks_));
3363
3364 width_ = 0;
3365 for (auto &chunk : chunks_)
3366 if (chunk.wire != NULL) {
3367 new_chunks.push_back(chunk);
3368 width_ += chunk.width;
3369 }
3370
3371 chunks_.swap(new_chunks);
3372 }
3373 else
3374 {
3375 cover("kernel.rtlil.sigspec.remove_const.unpacked");
3376
3377 std::vector<RTLIL::SigBit> new_bits;
3378 new_bits.reserve(width_);
3379
3380 for (auto &bit : bits_)
3381 if (bit.wire != NULL)
3382 new_bits.push_back(bit);
3383
3384 bits_.swap(new_bits);
3385 width_ = bits_.size();
3386 }
3387
3388 check();
3389 }
3390
3391 void RTLIL::SigSpec::remove(int offset, int length)
3392 {
3393 cover("kernel.rtlil.sigspec.remove_pos");
3394
3395 unpack();
3396
3397 log_assert(offset >= 0);
3398 log_assert(length >= 0);
3399 log_assert(offset + length <= width_);
3400
3401 bits_.erase(bits_.begin() + offset, bits_.begin() + offset + length);
3402 width_ = bits_.size();
3403
3404 check();
3405 }
3406
3407 RTLIL::SigSpec RTLIL::SigSpec::extract(int offset, int length) const
3408 {
3409 unpack();
3410 cover("kernel.rtlil.sigspec.extract_pos");
3411 return std::vector<RTLIL::SigBit>(bits_.begin() + offset, bits_.begin() + offset + length);
3412 }
3413
3414 void RTLIL::SigSpec::append(const RTLIL::SigSpec &signal)
3415 {
3416 if (signal.width_ == 0)
3417 return;
3418
3419 if (width_ == 0) {
3420 *this = signal;
3421 return;
3422 }
3423
3424 cover("kernel.rtlil.sigspec.append");
3425
3426 if (packed() != signal.packed()) {
3427 pack();
3428 signal.pack();
3429 }
3430
3431 if (packed())
3432 for (auto &other_c : signal.chunks_)
3433 {
3434 auto &my_last_c = chunks_.back();
3435 if (my_last_c.wire == NULL && other_c.wire == NULL) {
3436 auto &this_data = my_last_c.data;
3437 auto &other_data = other_c.data;
3438 this_data.insert(this_data.end(), other_data.begin(), other_data.end());
3439 my_last_c.width += other_c.width;
3440 } else
3441 if (my_last_c.wire == other_c.wire && my_last_c.offset + my_last_c.width == other_c.offset) {
3442 my_last_c.width += other_c.width;
3443 } else
3444 chunks_.push_back(other_c);
3445 }
3446 else
3447 bits_.insert(bits_.end(), signal.bits_.begin(), signal.bits_.end());
3448
3449 width_ += signal.width_;
3450 check();
3451 }
3452
3453 void RTLIL::SigSpec::append_bit(const RTLIL::SigBit &bit)
3454 {
3455 if (packed())
3456 {
3457 cover("kernel.rtlil.sigspec.append_bit.packed");
3458
3459 if (chunks_.size() == 0)
3460 chunks_.push_back(bit);
3461 else
3462 if (bit.wire == NULL)
3463 if (chunks_.back().wire == NULL) {
3464 chunks_.back().data.push_back(bit.data);
3465 chunks_.back().width++;
3466 } else
3467 chunks_.push_back(bit);
3468 else
3469 if (chunks_.back().wire == bit.wire && chunks_.back().offset + chunks_.back().width == bit.offset)
3470 chunks_.back().width++;
3471 else
3472 chunks_.push_back(bit);
3473 }
3474 else
3475 {
3476 cover("kernel.rtlil.sigspec.append_bit.unpacked");
3477 bits_.push_back(bit);
3478 }
3479
3480 width_++;
3481 check();
3482 }
3483
3484 void RTLIL::SigSpec::extend_u0(int width, bool is_signed)
3485 {
3486 cover("kernel.rtlil.sigspec.extend_u0");
3487
3488 pack();
3489
3490 if (width_ > width)
3491 remove(width, width_ - width);
3492
3493 if (width_ < width) {
3494 RTLIL::SigBit padding = width_ > 0 ? (*this)[width_ - 1] : RTLIL::State::Sx;
3495 if (!is_signed)
3496 padding = RTLIL::State::S0;
3497 while (width_ < width)
3498 append(padding);
3499 }
3500
3501 }
3502
3503 RTLIL::SigSpec RTLIL::SigSpec::repeat(int num) const
3504 {
3505 cover("kernel.rtlil.sigspec.repeat");
3506
3507 RTLIL::SigSpec sig;
3508 for (int i = 0; i < num; i++)
3509 sig.append(*this);
3510 return sig;
3511 }
3512
3513 #ifndef NDEBUG
3514 void RTLIL::SigSpec::check() const
3515 {
3516 if (width_ > 64)
3517 {
3518 cover("kernel.rtlil.sigspec.check.skip");
3519 }
3520 else if (packed())
3521 {
3522 cover("kernel.rtlil.sigspec.check.packed");
3523
3524 int w = 0;
3525 for (size_t i = 0; i < chunks_.size(); i++) {
3526 const RTLIL::SigChunk chunk = chunks_[i];
3527 if (chunk.wire == NULL) {
3528 if (i > 0)
3529 log_assert(chunks_[i-1].wire != NULL);
3530 log_assert(chunk.offset == 0);
3531 log_assert(chunk.data.size() == (size_t)chunk.width);
3532 } else {
3533 if (i > 0 && chunks_[i-1].wire == chunk.wire)
3534 log_assert(chunk.offset != chunks_[i-1].offset + chunks_[i-1].width);
3535 log_assert(chunk.offset >= 0);
3536 log_assert(chunk.width >= 0);
3537 log_assert(chunk.offset + chunk.width <= chunk.wire->width);
3538 log_assert(chunk.data.size() == 0);
3539 }
3540 w += chunk.width;
3541 }
3542 log_assert(w == width_);
3543 log_assert(bits_.empty());
3544 }
3545 else
3546 {
3547 cover("kernel.rtlil.sigspec.check.unpacked");
3548
3549 log_assert(width_ == GetSize(bits_));
3550 log_assert(chunks_.empty());
3551 }
3552 }
3553 #endif
3554
3555 bool RTLIL::SigSpec::operator <(const RTLIL::SigSpec &other) const
3556 {
3557 cover("kernel.rtlil.sigspec.comp_lt");
3558
3559 if (this == &other)
3560 return false;
3561
3562 if (width_ != other.width_)
3563 return width_ < other.width_;
3564
3565 pack();
3566 other.pack();
3567
3568 if (chunks_.size() != other.chunks_.size())
3569 return chunks_.size() < other.chunks_.size();
3570
3571 updhash();
3572 other.updhash();
3573
3574 if (hash_ != other.hash_)
3575 return hash_ < other.hash_;
3576
3577 for (size_t i = 0; i < chunks_.size(); i++)
3578 if (chunks_[i] != other.chunks_[i]) {
3579 cover("kernel.rtlil.sigspec.comp_lt.hash_collision");
3580 return chunks_[i] < other.chunks_[i];
3581 }
3582
3583 cover("kernel.rtlil.sigspec.comp_lt.equal");
3584 return false;
3585 }
3586
3587 bool RTLIL::SigSpec::operator ==(const RTLIL::SigSpec &other) const
3588 {
3589 cover("kernel.rtlil.sigspec.comp_eq");
3590
3591 if (this == &other)
3592 return true;
3593
3594 if (width_ != other.width_)
3595 return false;
3596
3597 // Without this, SigSpec() == SigSpec(State::S0, 0) will fail
3598 // since the RHS will contain one SigChunk of width 0 causing
3599 // the size check below to fail
3600 if (width_ == 0)
3601 return true;
3602
3603 pack();
3604 other.pack();
3605
3606 if (chunks_.size() != other.chunks_.size())
3607 return false;
3608
3609 updhash();
3610 other.updhash();
3611
3612 if (hash_ != other.hash_)
3613 return false;
3614
3615 for (size_t i = 0; i < chunks_.size(); i++)
3616 if (chunks_[i] != other.chunks_[i]) {
3617 cover("kernel.rtlil.sigspec.comp_eq.hash_collision");
3618 return false;
3619 }
3620
3621 cover("kernel.rtlil.sigspec.comp_eq.equal");
3622 return true;
3623 }
3624
3625 bool RTLIL::SigSpec::is_wire() const
3626 {
3627 cover("kernel.rtlil.sigspec.is_wire");
3628
3629 pack();
3630 return GetSize(chunks_) == 1 && chunks_[0].wire && chunks_[0].wire->width == width_;
3631 }
3632
3633 bool RTLIL::SigSpec::is_chunk() const
3634 {
3635 cover("kernel.rtlil.sigspec.is_chunk");
3636
3637 pack();
3638 return GetSize(chunks_) == 1;
3639 }
3640
3641 bool RTLIL::SigSpec::is_fully_const() const
3642 {
3643 cover("kernel.rtlil.sigspec.is_fully_const");
3644
3645 pack();
3646 for (auto it = chunks_.begin(); it != chunks_.end(); it++)
3647 if (it->width > 0 && it->wire != NULL)
3648 return false;
3649 return true;
3650 }
3651
3652 bool RTLIL::SigSpec::is_fully_zero() const
3653 {
3654 cover("kernel.rtlil.sigspec.is_fully_zero");
3655
3656 pack();
3657 for (auto it = chunks_.begin(); it != chunks_.end(); it++) {
3658 if (it->width > 0 && it->wire != NULL)
3659 return false;
3660 for (size_t i = 0; i < it->data.size(); i++)
3661 if (it->data[i] != RTLIL::State::S0)
3662 return false;
3663 }
3664 return true;
3665 }
3666
3667 bool RTLIL::SigSpec::is_fully_ones() const
3668 {
3669 cover("kernel.rtlil.sigspec.is_fully_ones");
3670
3671 pack();
3672 for (auto it = chunks_.begin(); it != chunks_.end(); it++) {
3673 if (it->width > 0 && it->wire != NULL)
3674 return false;
3675 for (size_t i = 0; i < it->data.size(); i++)
3676 if (it->data[i] != RTLIL::State::S1)
3677 return false;
3678 }
3679 return true;
3680 }
3681
3682 bool RTLIL::SigSpec::is_fully_def() const
3683 {
3684 cover("kernel.rtlil.sigspec.is_fully_def");
3685
3686 pack();
3687 for (auto it = chunks_.begin(); it != chunks_.end(); it++) {
3688 if (it->width > 0 && it->wire != NULL)
3689 return false;
3690 for (size_t i = 0; i < it->data.size(); i++)
3691 if (it->data[i] != RTLIL::State::S0 && it->data[i] != RTLIL::State::S1)
3692 return false;
3693 }
3694 return true;
3695 }
3696
3697 bool RTLIL::SigSpec::is_fully_undef() const
3698 {
3699 cover("kernel.rtlil.sigspec.is_fully_undef");
3700
3701 pack();
3702 for (auto it = chunks_.begin(); it != chunks_.end(); it++) {
3703 if (it->width > 0 && it->wire != NULL)
3704 return false;
3705 for (size_t i = 0; i < it->data.size(); i++)
3706 if (it->data[i] != RTLIL::State::Sx && it->data[i] != RTLIL::State::Sz)
3707 return false;
3708 }
3709 return true;
3710 }
3711
3712 bool RTLIL::SigSpec::has_const() const
3713 {
3714 cover("kernel.rtlil.sigspec.has_const");
3715
3716 pack();
3717 for (auto it = chunks_.begin(); it != chunks_.end(); it++)
3718 if (it->width > 0 && it->wire == NULL)
3719 return true;
3720 return false;
3721 }
3722
3723 bool RTLIL::SigSpec::has_marked_bits() const
3724 {
3725 cover("kernel.rtlil.sigspec.has_marked_bits");
3726
3727 pack();
3728 for (auto it = chunks_.begin(); it != chunks_.end(); it++)
3729 if (it->width > 0 && it->wire == NULL) {
3730 for (size_t i = 0; i < it->data.size(); i++)
3731 if (it->data[i] == RTLIL::State::Sm)
3732 return true;
3733 }
3734 return false;
3735 }
3736
3737 bool RTLIL::SigSpec::as_bool() const
3738 {
3739 cover("kernel.rtlil.sigspec.as_bool");
3740
3741 pack();
3742 log_assert(is_fully_const() && GetSize(chunks_) <= 1);
3743 if (width_)
3744 return RTLIL::Const(chunks_[0].data).as_bool();
3745 return false;
3746 }
3747
3748 int RTLIL::SigSpec::as_int(bool is_signed) const
3749 {
3750 cover("kernel.rtlil.sigspec.as_int");
3751
3752 pack();
3753 log_assert(is_fully_const() && GetSize(chunks_) <= 1);
3754 if (width_)
3755 return RTLIL::Const(chunks_[0].data).as_int(is_signed);
3756 return 0;
3757 }
3758
3759 std::string RTLIL::SigSpec::as_string() const
3760 {
3761 cover("kernel.rtlil.sigspec.as_string");
3762
3763 pack();
3764 std::string str;
3765 for (size_t i = chunks_.size(); i > 0; i--) {
3766 const RTLIL::SigChunk &chunk = chunks_[i-1];
3767 if (chunk.wire != NULL)
3768 for (int j = 0; j < chunk.width; j++)
3769 str += "?";
3770 else
3771 str += RTLIL::Const(chunk.data).as_string();
3772 }
3773 return str;
3774 }
3775
3776 RTLIL::Const RTLIL::SigSpec::as_const() const
3777 {
3778 cover("kernel.rtlil.sigspec.as_const");
3779
3780 pack();
3781 log_assert(is_fully_const() && GetSize(chunks_) <= 1);
3782 if (width_)
3783 return chunks_[0].data;
3784 return RTLIL::Const();
3785 }
3786
3787 RTLIL::Wire *RTLIL::SigSpec::as_wire() const
3788 {
3789 cover("kernel.rtlil.sigspec.as_wire");
3790
3791 pack();
3792 log_assert(is_wire());
3793 return chunks_[0].wire;
3794 }
3795
3796 RTLIL::SigChunk RTLIL::SigSpec::as_chunk() const
3797 {
3798 cover("kernel.rtlil.sigspec.as_chunk");
3799
3800 pack();
3801 log_assert(is_chunk());
3802 return chunks_[0];
3803 }
3804
3805 RTLIL::SigBit RTLIL::SigSpec::as_bit() const
3806 {
3807 cover("kernel.rtlil.sigspec.as_bit");
3808
3809 log_assert(width_ == 1);
3810 if (packed())
3811 return RTLIL::SigBit(*chunks_.begin());
3812 else
3813 return bits_[0];
3814 }
3815
3816 bool RTLIL::SigSpec::match(std::string pattern) const
3817 {
3818 cover("kernel.rtlil.sigspec.match");
3819
3820 pack();
3821 std::string str = as_string();
3822 log_assert(pattern.size() == str.size());
3823
3824 for (size_t i = 0; i < pattern.size(); i++) {
3825 if (pattern[i] == ' ')
3826 continue;
3827 if (pattern[i] == '*') {
3828 if (str[i] != 'z' && str[i] != 'x')
3829 return false;
3830 continue;
3831 }
3832 if (pattern[i] != str[i])
3833 return false;
3834 }
3835
3836 return true;
3837 }
3838
3839 std::set<RTLIL::SigBit> RTLIL::SigSpec::to_sigbit_set() const
3840 {
3841 cover("kernel.rtlil.sigspec.to_sigbit_set");
3842
3843 pack();
3844 std::set<RTLIL::SigBit> sigbits;
3845 for (auto &c : chunks_)
3846 for (int i = 0; i < c.width; i++)
3847 sigbits.insert(RTLIL::SigBit(c, i));
3848 return sigbits;
3849 }
3850
3851 pool<RTLIL::SigBit> RTLIL::SigSpec::to_sigbit_pool() const
3852 {
3853 cover("kernel.rtlil.sigspec.to_sigbit_pool");
3854
3855 pack();
3856 pool<RTLIL::SigBit> sigbits;
3857 for (auto &c : chunks_)
3858 for (int i = 0; i < c.width; i++)
3859 sigbits.insert(RTLIL::SigBit(c, i));
3860 return sigbits;
3861 }
3862
3863 std::vector<RTLIL::SigBit> RTLIL::SigSpec::to_sigbit_vector() const
3864 {
3865 cover("kernel.rtlil.sigspec.to_sigbit_vector");
3866
3867 unpack();
3868 return bits_;
3869 }
3870
3871 std::map<RTLIL::SigBit, RTLIL::SigBit> RTLIL::SigSpec::to_sigbit_map(const RTLIL::SigSpec &other) const
3872 {
3873 cover("kernel.rtlil.sigspec.to_sigbit_map");
3874
3875 unpack();
3876 other.unpack();
3877
3878 log_assert(width_ == other.width_);
3879
3880 std::map<RTLIL::SigBit, RTLIL::SigBit> new_map;
3881 for (int i = 0; i < width_; i++)
3882 new_map[bits_[i]] = other.bits_[i];
3883
3884 return new_map;
3885 }
3886
3887 dict<RTLIL::SigBit, RTLIL::SigBit> RTLIL::SigSpec::to_sigbit_dict(const RTLIL::SigSpec &other) const
3888 {
3889 cover("kernel.rtlil.sigspec.to_sigbit_dict");
3890
3891 unpack();
3892 other.unpack();
3893
3894 log_assert(width_ == other.width_);
3895
3896 dict<RTLIL::SigBit, RTLIL::SigBit> new_map;
3897 for (int i = 0; i < width_; i++)
3898 new_map[bits_[i]] = other.bits_[i];
3899
3900 return new_map;
3901 }
3902
3903 static void sigspec_parse_split(std::vector<std::string> &tokens, const std::string &text, char sep)
3904 {
3905 size_t start = 0, end = 0;
3906 while ((end = text.find(sep, start)) != std::string::npos) {
3907 tokens.push_back(text.substr(start, end - start));
3908 start = end + 1;
3909 }
3910 tokens.push_back(text.substr(start));
3911 }
3912
3913 static int sigspec_parse_get_dummy_line_num()
3914 {
3915 return 0;
3916 }
3917
3918 bool RTLIL::SigSpec::parse(RTLIL::SigSpec &sig, RTLIL::Module *module, std::string str)
3919 {
3920 cover("kernel.rtlil.sigspec.parse");
3921
3922 AST::current_filename = "input";
3923 AST::use_internal_line_num();
3924 AST::set_line_num(0);
3925
3926 std::vector<std::string> tokens;
3927 sigspec_parse_split(tokens, str, ',');
3928
3929 sig = RTLIL::SigSpec();
3930 for (int tokidx = int(tokens.size())-1; tokidx >= 0; tokidx--)
3931 {
3932 std::string netname = tokens[tokidx];
3933 std::string indices;
3934
3935 if (netname.size() == 0)
3936 continue;
3937
3938 if (('0' <= netname[0] && netname[0] <= '9') || netname[0] == '\'') {
3939 cover("kernel.rtlil.sigspec.parse.const");
3940 AST::get_line_num = sigspec_parse_get_dummy_line_num;
3941 AST::AstNode *ast = VERILOG_FRONTEND::const2ast(netname);
3942 if (ast == NULL)
3943 return false;
3944 sig.append(RTLIL::Const(ast->bits));
3945 delete ast;
3946 continue;
3947 }
3948
3949 if (module == NULL)
3950 return false;
3951
3952 cover("kernel.rtlil.sigspec.parse.net");
3953
3954 if (netname[0] != '$' && netname[0] != '\\')
3955 netname = "\\" + netname;
3956
3957 if (module->wires_.count(netname) == 0) {
3958 size_t indices_pos = netname.size()-1;
3959 if (indices_pos > 2 && netname[indices_pos] == ']')
3960 {
3961 indices_pos--;
3962 while (indices_pos > 0 && ('0' <= netname[indices_pos] && netname[indices_pos] <= '9')) indices_pos--;
3963 if (indices_pos > 0 && netname[indices_pos] == ':') {
3964 indices_pos--;
3965 while (indices_pos > 0 && ('0' <= netname[indices_pos] && netname[indices_pos] <= '9')) indices_pos--;
3966 }
3967 if (indices_pos > 0 && netname[indices_pos] == '[') {
3968 indices = netname.substr(indices_pos);
3969 netname = netname.substr(0, indices_pos);
3970 }
3971 }
3972 }
3973
3974 if (module->wires_.count(netname) == 0)
3975 return false;
3976
3977 RTLIL::Wire *wire = module->wires_.at(netname);
3978 if (!indices.empty()) {
3979 std::vector<std::string> index_tokens;
3980 sigspec_parse_split(index_tokens, indices.substr(1, indices.size()-2), ':');
3981 if (index_tokens.size() == 1) {
3982 cover("kernel.rtlil.sigspec.parse.bit_sel");
3983 int a = atoi(index_tokens.at(0).c_str());
3984 if (a < 0 || a >= wire->width)
3985 return false;
3986 sig.append(RTLIL::SigSpec(wire, a));
3987 } else {
3988 cover("kernel.rtlil.sigspec.parse.part_sel");
3989 int a = atoi(index_tokens.at(0).c_str());
3990 int b = atoi(index_tokens.at(1).c_str());
3991 if (a > b) {
3992 int tmp = a;
3993 a = b, b = tmp;
3994 }
3995 if (a < 0 || a >= wire->width)
3996 return false;
3997 if (b < 0 || b >= wire->width)
3998 return false;
3999 sig.append(RTLIL::SigSpec(wire, a, b-a+1));
4000 }
4001 } else
4002 sig.append(wire);
4003 }
4004
4005 return true;
4006 }
4007
4008 bool RTLIL::SigSpec::parse_sel(RTLIL::SigSpec &sig, RTLIL::Design *design, RTLIL::Module *module, std::string str)
4009 {
4010 if (str.empty() || str[0] != '@')
4011 return parse(sig, module, str);
4012
4013 cover("kernel.rtlil.sigspec.parse.sel");
4014
4015 str = RTLIL::escape_id(str.substr(1));
4016 if (design->selection_vars.count(str) == 0)
4017 return false;
4018
4019 sig = RTLIL::SigSpec();
4020 RTLIL::Selection &sel = design->selection_vars.at(str);
4021 for (auto &it : module->wires_)
4022 if (sel.selected_member(module->name, it.first))
4023 sig.append(it.second);
4024
4025 return true;
4026 }
4027
4028 bool RTLIL::SigSpec::parse_rhs(const RTLIL::SigSpec &lhs, RTLIL::SigSpec &sig, RTLIL::Module *module, std::string str)
4029 {
4030 if (str == "0") {
4031 cover("kernel.rtlil.sigspec.parse.rhs_zeros");
4032 sig = RTLIL::SigSpec(RTLIL::State::S0, lhs.width_);
4033 return true;
4034 }
4035
4036 if (str == "~0") {
4037 cover("kernel.rtlil.sigspec.parse.rhs_ones");
4038 sig = RTLIL::SigSpec(RTLIL::State::S1, lhs.width_);
4039 return true;
4040 }
4041
4042 if (lhs.chunks_.size() == 1) {
4043 char *p = (char*)str.c_str(), *endptr;
4044 long int val = strtol(p, &endptr, 10);
4045 if (endptr && endptr != p && *endptr == 0) {
4046 sig = RTLIL::SigSpec(val, lhs.width_);
4047 cover("kernel.rtlil.sigspec.parse.rhs_dec");
4048 return true;
4049 }
4050 }
4051
4052 return parse(sig, module, str);
4053 }
4054
4055 RTLIL::CaseRule::~CaseRule()
4056 {
4057 for (auto it = switches.begin(); it != switches.end(); it++)
4058 delete *it;
4059 }
4060
4061 bool RTLIL::CaseRule::empty() const
4062 {
4063 return actions.empty() && switches.empty();
4064 }
4065
4066 RTLIL::CaseRule *RTLIL::CaseRule::clone() const
4067 {
4068 RTLIL::CaseRule *new_caserule = new RTLIL::CaseRule;
4069 new_caserule->compare = compare;
4070 new_caserule->actions = actions;
4071 for (auto &it : switches)
4072 new_caserule->switches.push_back(it->clone());
4073 return new_caserule;
4074 }
4075
4076 RTLIL::SwitchRule::~SwitchRule()
4077 {
4078 for (auto it = cases.begin(); it != cases.end(); it++)
4079 delete *it;
4080 }
4081
4082 bool RTLIL::SwitchRule::empty() const
4083 {
4084 return cases.empty();
4085 }
4086
4087 RTLIL::SwitchRule *RTLIL::SwitchRule::clone() const
4088 {
4089 RTLIL::SwitchRule *new_switchrule = new RTLIL::SwitchRule;
4090 new_switchrule->signal = signal;
4091 new_switchrule->attributes = attributes;
4092 for (auto &it : cases)
4093 new_switchrule->cases.push_back(it->clone());
4094 return new_switchrule;
4095
4096 }
4097
4098 RTLIL::SyncRule *RTLIL::SyncRule::clone() const
4099 {
4100 RTLIL::SyncRule *new_syncrule = new RTLIL::SyncRule;
4101 new_syncrule->type = type;
4102 new_syncrule->signal = signal;
4103 new_syncrule->actions = actions;
4104 return new_syncrule;
4105 }
4106
4107 RTLIL::Process::~Process()
4108 {
4109 for (auto it = syncs.begin(); it != syncs.end(); it++)
4110 delete *it;
4111 }
4112
4113 RTLIL::Process *RTLIL::Process::clone() const
4114 {
4115 RTLIL::Process *new_proc = new RTLIL::Process;
4116
4117 new_proc->name = name;
4118 new_proc->attributes = attributes;
4119
4120 RTLIL::CaseRule *rc_ptr = root_case.clone();
4121 new_proc->root_case = *rc_ptr;
4122 rc_ptr->switches.clear();
4123 delete rc_ptr;
4124
4125 for (auto &it : syncs)
4126 new_proc->syncs.push_back(it->clone());
4127
4128 return new_proc;
4129 }
4130
4131 #ifdef WITH_PYTHON
4132 RTLIL::Memory::~Memory()
4133 {
4134 RTLIL::Memory::get_all_memorys()->erase(hashidx_);
4135 }
4136 static std::map<unsigned int, RTLIL::Memory*> all_memorys;
4137 std::map<unsigned int, RTLIL::Memory*> *RTLIL::Memory::get_all_memorys(void)
4138 {
4139 return &all_memorys;
4140 }
4141 #endif
4142 YOSYS_NAMESPACE_END