qbfsat: Simplify solution format and replace `SigBit::str()` with `log_signal()`.
[yosys.git] / kernel / rtlil.h
1 /* -*- c++ -*-
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
22 #ifndef RTLIL_H
23 #define RTLIL_H
24
25 YOSYS_NAMESPACE_BEGIN
26
27 namespace RTLIL
28 {
29 enum State : unsigned char {
30 S0 = 0,
31 S1 = 1,
32 Sx = 2, // undefined value or conflict
33 Sz = 3, // high-impedance / not-connected
34 Sa = 4, // don't care (used only in cases)
35 Sm = 5 // marker (used internally by some passes)
36 };
37
38 enum SyncType : unsigned char {
39 ST0 = 0, // level sensitive: 0
40 ST1 = 1, // level sensitive: 1
41 STp = 2, // edge sensitive: posedge
42 STn = 3, // edge sensitive: negedge
43 STe = 4, // edge sensitive: both edges
44 STa = 5, // always active
45 STg = 6, // global clock
46 STi = 7 // init
47 };
48
49 enum ConstFlags : unsigned char {
50 CONST_FLAG_NONE = 0,
51 CONST_FLAG_STRING = 1,
52 CONST_FLAG_SIGNED = 2, // only used for parameters
53 CONST_FLAG_REAL = 4 // only used for parameters
54 };
55
56 struct Const;
57 struct AttrObject;
58 struct Selection;
59 struct Monitor;
60 struct Design;
61 struct Module;
62 struct Wire;
63 struct Memory;
64 struct Cell;
65 struct SigChunk;
66 struct SigBit;
67 struct SigSpecIterator;
68 struct SigSpecConstIterator;
69 struct SigSpec;
70 struct CaseRule;
71 struct SwitchRule;
72 struct SyncRule;
73 struct Process;
74
75 typedef std::pair<SigSpec, SigSpec> SigSig;
76
77 struct IdString
78 {
79 #undef YOSYS_XTRACE_GET_PUT
80 #undef YOSYS_SORT_ID_FREE_LIST
81 #undef YOSYS_USE_STICKY_IDS
82 #undef YOSYS_NO_IDS_REFCNT
83
84 // the global id string cache
85
86 static struct destruct_guard_t {
87 bool ok; // POD, will be initialized to zero
88 destruct_guard_t() { ok = true; }
89 ~destruct_guard_t() { ok = false; }
90 } destruct_guard;
91
92 static std::vector<char*> global_id_storage_;
93 static dict<char*, int, hash_cstr_ops> global_id_index_;
94 #ifndef YOSYS_NO_IDS_REFCNT
95 static std::vector<int> global_refcount_storage_;
96 static std::vector<int> global_free_idx_list_;
97 #endif
98
99 #ifdef YOSYS_USE_STICKY_IDS
100 static int last_created_idx_ptr_;
101 static int last_created_idx_[8];
102 #endif
103
104 static inline void xtrace_db_dump()
105 {
106 #ifdef YOSYS_XTRACE_GET_PUT
107 for (int idx = 0; idx < GetSize(global_id_storage_); idx++)
108 {
109 if (global_id_storage_.at(idx) == nullptr)
110 log("#X# DB-DUMP index %d: FREE\n", idx);
111 else
112 log("#X# DB-DUMP index %d: '%s' (ref %d)\n", idx, global_id_storage_.at(idx), global_refcount_storage_.at(idx));
113 }
114 #endif
115 }
116
117 static inline void checkpoint()
118 {
119 #ifdef YOSYS_USE_STICKY_IDS
120 last_created_idx_ptr_ = 0;
121 for (int i = 0; i < 8; i++) {
122 if (last_created_idx_[i])
123 put_reference(last_created_idx_[i]);
124 last_created_idx_[i] = 0;
125 }
126 #endif
127 #ifdef YOSYS_SORT_ID_FREE_LIST
128 std::sort(global_free_idx_list_.begin(), global_free_idx_list_.end(), std::greater<int>());
129 #endif
130 }
131
132 static inline int get_reference(int idx)
133 {
134 if (idx) {
135 #ifndef YOSYS_NO_IDS_REFCNT
136 global_refcount_storage_[idx]++;
137 #endif
138 #ifdef YOSYS_XTRACE_GET_PUT
139 if (yosys_xtrace)
140 log("#X# GET-BY-INDEX '%s' (index %d, refcount %d)\n", global_id_storage_.at(idx), idx, global_refcount_storage_.at(idx));
141 #endif
142 }
143 return idx;
144 }
145
146 static int get_reference(const char *p)
147 {
148 log_assert(destruct_guard.ok);
149
150 if (!p[0])
151 return 0;
152
153 auto it = global_id_index_.find((char*)p);
154 if (it != global_id_index_.end()) {
155 #ifndef YOSYS_NO_IDS_REFCNT
156 global_refcount_storage_.at(it->second)++;
157 #endif
158 #ifdef YOSYS_XTRACE_GET_PUT
159 if (yosys_xtrace)
160 log("#X# GET-BY-NAME '%s' (index %d, refcount %d)\n", global_id_storage_.at(it->second), it->second, global_refcount_storage_.at(it->second));
161 #endif
162 return it->second;
163 }
164
165 log_assert(p[0] == '$' || p[0] == '\\');
166 log_assert(p[1] != 0);
167 for (const char *c = p; *c; c++)
168 log_assert((unsigned)*c > (unsigned)' ');
169
170 #ifndef YOSYS_NO_IDS_REFCNT
171 if (global_free_idx_list_.empty()) {
172 if (global_id_storage_.empty()) {
173 global_refcount_storage_.push_back(0);
174 global_id_storage_.push_back((char*)"");
175 global_id_index_[global_id_storage_.back()] = 0;
176 }
177 log_assert(global_id_storage_.size() < 0x40000000);
178 global_free_idx_list_.push_back(global_id_storage_.size());
179 global_id_storage_.push_back(nullptr);
180 global_refcount_storage_.push_back(0);
181 }
182
183 int idx = global_free_idx_list_.back();
184 global_free_idx_list_.pop_back();
185 global_id_storage_.at(idx) = strdup(p);
186 global_id_index_[global_id_storage_.at(idx)] = idx;
187 global_refcount_storage_.at(idx)++;
188 #else
189 if (global_id_storage_.empty()) {
190 global_id_storage_.push_back((char*)"");
191 global_id_index_[global_id_storage_.back()] = 0;
192 }
193 int idx = global_id_storage_.size();
194 global_id_storage_.push_back(strdup(p));
195 global_id_index_[global_id_storage_.back()] = idx;
196 #endif
197
198 if (yosys_xtrace) {
199 log("#X# New IdString '%s' with index %d.\n", p, idx);
200 log_backtrace("-X- ", yosys_xtrace-1);
201 }
202
203 #ifdef YOSYS_XTRACE_GET_PUT
204 if (yosys_xtrace)
205 log("#X# GET-BY-NAME '%s' (index %d, refcount %d)\n", global_id_storage_.at(idx), idx, global_refcount_storage_.at(idx));
206 #endif
207
208 #ifdef YOSYS_USE_STICKY_IDS
209 // Avoid Create->Delete->Create pattern
210 if (last_created_idx_[last_created_idx_ptr_])
211 put_reference(last_created_idx_[last_created_idx_ptr_]);
212 last_created_idx_[last_created_idx_ptr_] = idx;
213 get_reference(last_created_idx_[last_created_idx_ptr_]);
214 last_created_idx_ptr_ = (last_created_idx_ptr_ + 1) & 7;
215 #endif
216
217 return idx;
218 }
219
220 #ifndef YOSYS_NO_IDS_REFCNT
221 static inline void put_reference(int idx)
222 {
223 // put_reference() may be called from destructors after the destructor of
224 // global_refcount_storage_ has been run. in this case we simply do nothing.
225 if (!destruct_guard.ok || !idx)
226 return;
227
228 #ifdef YOSYS_XTRACE_GET_PUT
229 if (yosys_xtrace) {
230 log("#X# PUT '%s' (index %d, refcount %d)\n", global_id_storage_.at(idx), idx, global_refcount_storage_.at(idx));
231 }
232 #endif
233
234 int &refcount = global_refcount_storage_[idx];
235
236 if (--refcount > 0)
237 return;
238
239 log_assert(refcount == 0);
240 free_reference(idx);
241 }
242 static inline void free_reference(int idx)
243 {
244 if (yosys_xtrace) {
245 log("#X# Removed IdString '%s' with index %d.\n", global_id_storage_.at(idx), idx);
246 log_backtrace("-X- ", yosys_xtrace-1);
247 }
248
249 global_id_index_.erase(global_id_storage_.at(idx));
250 free(global_id_storage_.at(idx));
251 global_id_storage_.at(idx) = nullptr;
252 global_free_idx_list_.push_back(idx);
253 }
254 #else
255 static inline void put_reference(int) { }
256 #endif
257
258 // the actual IdString object is just is a single int
259
260 int index_;
261
262 inline IdString() : index_(0) { }
263 inline IdString(const char *str) : index_(get_reference(str)) { }
264 inline IdString(const IdString &str) : index_(get_reference(str.index_)) { }
265 inline IdString(IdString &&str) : index_(str.index_) { str.index_ = 0; }
266 inline IdString(const std::string &str) : index_(get_reference(str.c_str())) { }
267 inline ~IdString() { put_reference(index_); }
268
269 inline void operator=(const IdString &rhs) {
270 put_reference(index_);
271 index_ = get_reference(rhs.index_);
272 }
273
274 inline void operator=(const char *rhs) {
275 IdString id(rhs);
276 *this = id;
277 }
278
279 inline void operator=(const std::string &rhs) {
280 IdString id(rhs);
281 *this = id;
282 }
283
284 inline const char *c_str() const {
285 return global_id_storage_.at(index_);
286 }
287
288 inline std::string str() const {
289 return std::string(global_id_storage_.at(index_));
290 }
291
292 inline bool operator<(const IdString &rhs) const {
293 return index_ < rhs.index_;
294 }
295
296 inline bool operator==(const IdString &rhs) const { return index_ == rhs.index_; }
297 inline bool operator!=(const IdString &rhs) const { return index_ != rhs.index_; }
298
299 // The methods below are just convenience functions for better compatibility with std::string.
300
301 bool operator==(const std::string &rhs) const { return c_str() == rhs; }
302 bool operator!=(const std::string &rhs) const { return c_str() != rhs; }
303
304 bool operator==(const char *rhs) const { return strcmp(c_str(), rhs) == 0; }
305 bool operator!=(const char *rhs) const { return strcmp(c_str(), rhs) != 0; }
306
307 char operator[](size_t i) const {
308 const char *p = c_str();
309 for (; i != 0; i--, p++)
310 log_assert(*p != 0);
311 return *p;
312 }
313
314 std::string substr(size_t pos = 0, size_t len = std::string::npos) const {
315 if (len == std::string::npos || len >= strlen(c_str() + pos))
316 return std::string(c_str() + pos);
317 else
318 return std::string(c_str() + pos, len);
319 }
320
321 int compare(size_t pos, size_t len, const char* s) const {
322 return strncmp(c_str()+pos, s, len);
323 }
324
325 bool begins_with(const char* prefix) const {
326 size_t len = strlen(prefix);
327 if (size() < len) return false;
328 return compare(0, len, prefix) == 0;
329 }
330
331 bool ends_with(const char* suffix) const {
332 size_t len = strlen(suffix);
333 if (size() < len) return false;
334 return compare(size()-len, len, suffix) == 0;
335 }
336
337 size_t size() const {
338 return strlen(c_str());
339 }
340
341 bool empty() const {
342 return c_str()[0] == 0;
343 }
344
345 void clear() {
346 *this = IdString();
347 }
348
349 unsigned int hash() const {
350 return index_;
351 }
352
353 // The following is a helper key_compare class. Instead of for example std::set<Cell*>
354 // use std::set<Cell*, IdString::compare_ptr_by_name<Cell>> if the order of cells in the
355 // set has an influence on the algorithm.
356
357 template<typename T> struct compare_ptr_by_name {
358 bool operator()(const T *a, const T *b) const {
359 return (a == nullptr || b == nullptr) ? (a < b) : (a->name < b->name);
360 }
361 };
362
363 // often one needs to check if a given IdString is part of a list (for example a list
364 // of cell types). the following functions helps with that.
365
366 template<typename... Args>
367 bool in(Args... args) const {
368 // Credit: https://articles.emptycrate.com/2016/05/14/folds_in_cpp11_ish.html
369 bool result = false;
370 (void) std::initializer_list<int>{ (result = result || in(args), 0)... };
371 return result;
372 }
373
374 bool in(const IdString &rhs) const { return *this == rhs; }
375 bool in(const char *rhs) const { return *this == rhs; }
376 bool in(const std::string &rhs) const { return *this == rhs; }
377 bool in(const pool<IdString> &rhs) const { return rhs.count(*this) != 0; }
378 };
379
380 namespace ID {
381 #define X(_id) extern IdString _id;
382 #include "kernel/constids.inc"
383 #undef X
384 };
385
386 extern dict<std::string, std::string> constpad;
387
388 const pool<IdString> &builtin_ff_cell_types();
389
390 static inline std::string escape_id(const std::string &str) {
391 if (str.size() > 0 && str[0] != '\\' && str[0] != '$')
392 return "\\" + str;
393 return str;
394 }
395
396 static inline std::string unescape_id(const std::string &str) {
397 if (str.size() < 2)
398 return str;
399 if (str[0] != '\\')
400 return str;
401 if (str[1] == '$' || str[1] == '\\')
402 return str;
403 if (str[1] >= '0' && str[1] <= '9')
404 return str;
405 return str.substr(1);
406 }
407
408 static inline std::string unescape_id(RTLIL::IdString str) {
409 return unescape_id(str.str());
410 }
411
412 static inline const char *id2cstr(RTLIL::IdString str) {
413 return log_id(str);
414 }
415
416 template <typename T> struct sort_by_name_id {
417 bool operator()(T *a, T *b) const {
418 return a->name < b->name;
419 }
420 };
421
422 template <typename T> struct sort_by_name_str {
423 bool operator()(T *a, T *b) const {
424 return strcmp(a->name.c_str(), b->name.c_str()) < 0;
425 }
426 };
427
428 struct sort_by_id_str {
429 bool operator()(RTLIL::IdString a, RTLIL::IdString b) const {
430 return strcmp(a.c_str(), b.c_str()) < 0;
431 }
432 };
433
434 // see calc.cc for the implementation of this functions
435 RTLIL::Const const_not (const RTLIL::Const &arg1, const RTLIL::Const &arg2, bool signed1, bool signed2, int result_len);
436 RTLIL::Const const_and (const RTLIL::Const &arg1, const RTLIL::Const &arg2, bool signed1, bool signed2, int result_len);
437 RTLIL::Const const_or (const RTLIL::Const &arg1, const RTLIL::Const &arg2, bool signed1, bool signed2, int result_len);
438 RTLIL::Const const_xor (const RTLIL::Const &arg1, const RTLIL::Const &arg2, bool signed1, bool signed2, int result_len);
439 RTLIL::Const const_xnor (const RTLIL::Const &arg1, const RTLIL::Const &arg2, bool signed1, bool signed2, int result_len);
440
441 RTLIL::Const const_reduce_and (const RTLIL::Const &arg1, const RTLIL::Const &arg2, bool signed1, bool signed2, int result_len);
442 RTLIL::Const const_reduce_or (const RTLIL::Const &arg1, const RTLIL::Const &arg2, bool signed1, bool signed2, int result_len);
443 RTLIL::Const const_reduce_xor (const RTLIL::Const &arg1, const RTLIL::Const &arg2, bool signed1, bool signed2, int result_len);
444 RTLIL::Const const_reduce_xnor (const RTLIL::Const &arg1, const RTLIL::Const &arg2, bool signed1, bool signed2, int result_len);
445 RTLIL::Const const_reduce_bool (const RTLIL::Const &arg1, const RTLIL::Const &arg2, bool signed1, bool signed2, int result_len);
446
447 RTLIL::Const const_logic_not (const RTLIL::Const &arg1, const RTLIL::Const &arg2, bool signed1, bool signed2, int result_len);
448 RTLIL::Const const_logic_and (const RTLIL::Const &arg1, const RTLIL::Const &arg2, bool signed1, bool signed2, int result_len);
449 RTLIL::Const const_logic_or (const RTLIL::Const &arg1, const RTLIL::Const &arg2, bool signed1, bool signed2, int result_len);
450
451 RTLIL::Const const_shl (const RTLIL::Const &arg1, const RTLIL::Const &arg2, bool signed1, bool signed2, int result_len);
452 RTLIL::Const const_shr (const RTLIL::Const &arg1, const RTLIL::Const &arg2, bool signed1, bool signed2, int result_len);
453 RTLIL::Const const_sshl (const RTLIL::Const &arg1, const RTLIL::Const &arg2, bool signed1, bool signed2, int result_len);
454 RTLIL::Const const_sshr (const RTLIL::Const &arg1, const RTLIL::Const &arg2, bool signed1, bool signed2, int result_len);
455 RTLIL::Const const_shift (const RTLIL::Const &arg1, const RTLIL::Const &arg2, bool signed1, bool signed2, int result_len);
456 RTLIL::Const const_shiftx (const RTLIL::Const &arg1, const RTLIL::Const &arg2, bool signed1, bool signed2, int result_len);
457
458 RTLIL::Const const_lt (const RTLIL::Const &arg1, const RTLIL::Const &arg2, bool signed1, bool signed2, int result_len);
459 RTLIL::Const const_le (const RTLIL::Const &arg1, const RTLIL::Const &arg2, bool signed1, bool signed2, int result_len);
460 RTLIL::Const const_eq (const RTLIL::Const &arg1, const RTLIL::Const &arg2, bool signed1, bool signed2, int result_len);
461 RTLIL::Const const_ne (const RTLIL::Const &arg1, const RTLIL::Const &arg2, bool signed1, bool signed2, int result_len);
462 RTLIL::Const const_eqx (const RTLIL::Const &arg1, const RTLIL::Const &arg2, bool signed1, bool signed2, int result_len);
463 RTLIL::Const const_nex (const RTLIL::Const &arg1, const RTLIL::Const &arg2, bool signed1, bool signed2, int result_len);
464 RTLIL::Const const_ge (const RTLIL::Const &arg1, const RTLIL::Const &arg2, bool signed1, bool signed2, int result_len);
465 RTLIL::Const const_gt (const RTLIL::Const &arg1, const RTLIL::Const &arg2, bool signed1, bool signed2, int result_len);
466
467 RTLIL::Const const_add (const RTLIL::Const &arg1, const RTLIL::Const &arg2, bool signed1, bool signed2, int result_len);
468 RTLIL::Const const_sub (const RTLIL::Const &arg1, const RTLIL::Const &arg2, bool signed1, bool signed2, int result_len);
469 RTLIL::Const const_mul (const RTLIL::Const &arg1, const RTLIL::Const &arg2, bool signed1, bool signed2, int result_len);
470 RTLIL::Const const_div (const RTLIL::Const &arg1, const RTLIL::Const &arg2, bool signed1, bool signed2, int result_len);
471 RTLIL::Const const_divfloor (const RTLIL::Const &arg1, const RTLIL::Const &arg2, bool signed1, bool signed2, int result_len);
472 RTLIL::Const const_modfloor (const RTLIL::Const &arg1, const RTLIL::Const &arg2, bool signed1, bool signed2, int result_len);
473 RTLIL::Const const_mod (const RTLIL::Const &arg1, const RTLIL::Const &arg2, bool signed1, bool signed2, int result_len);
474 RTLIL::Const const_pow (const RTLIL::Const &arg1, const RTLIL::Const &arg2, bool signed1, bool signed2, int result_len);
475
476 RTLIL::Const const_pos (const RTLIL::Const &arg1, const RTLIL::Const &arg2, bool signed1, bool signed2, int result_len);
477 RTLIL::Const const_neg (const RTLIL::Const &arg1, const RTLIL::Const &arg2, bool signed1, bool signed2, int result_len);
478
479
480 // This iterator-range-pair is used for Design::modules(), Module::wires() and Module::cells().
481 // It maintains a reference counter that is used to make sure that the container is not modified while being iterated over.
482
483 template<typename T>
484 struct ObjIterator {
485 using iterator_category = std::forward_iterator_tag;
486 using value_type = T;
487 using difference_type = ptrdiff_t;
488 using pointer = T*;
489 using reference = T&;
490 typename dict<RTLIL::IdString, T>::iterator it;
491 dict<RTLIL::IdString, T> *list_p;
492 int *refcount_p;
493
494 ObjIterator() : list_p(nullptr), refcount_p(nullptr) {
495 }
496
497 ObjIterator(decltype(list_p) list_p, int *refcount_p) : list_p(list_p), refcount_p(refcount_p) {
498 if (list_p->empty()) {
499 this->list_p = nullptr;
500 this->refcount_p = nullptr;
501 } else {
502 it = list_p->begin();
503 (*refcount_p)++;
504 }
505 }
506
507 ObjIterator(const RTLIL::ObjIterator<T> &other) {
508 it = other.it;
509 list_p = other.list_p;
510 refcount_p = other.refcount_p;
511 if (refcount_p)
512 (*refcount_p)++;
513 }
514
515 ObjIterator &operator=(const RTLIL::ObjIterator<T> &other) {
516 if (refcount_p)
517 (*refcount_p)--;
518 it = other.it;
519 list_p = other.list_p;
520 refcount_p = other.refcount_p;
521 if (refcount_p)
522 (*refcount_p)++;
523 return *this;
524 }
525
526 ~ObjIterator() {
527 if (refcount_p)
528 (*refcount_p)--;
529 }
530
531 inline T operator*() const {
532 log_assert(list_p != nullptr);
533 return it->second;
534 }
535
536 inline bool operator!=(const RTLIL::ObjIterator<T> &other) const {
537 if (list_p == nullptr || other.list_p == nullptr)
538 return list_p != other.list_p;
539 return it != other.it;
540 }
541
542
543 inline bool operator==(const RTLIL::ObjIterator<T> &other) const {
544 return !(*this != other);
545 }
546
547 inline ObjIterator<T>& operator++() {
548 log_assert(list_p != nullptr);
549 if (++it == list_p->end()) {
550 (*refcount_p)--;
551 list_p = nullptr;
552 refcount_p = nullptr;
553 }
554 return *this;
555 }
556
557 inline ObjIterator<T>& operator+=(int amt) {
558 log_assert(list_p != nullptr);
559 it += amt;
560 if (it == list_p->end()) {
561 (*refcount_p)--;
562 list_p = nullptr;
563 refcount_p = nullptr;
564 }
565 return *this;
566 }
567
568 inline ObjIterator<T> operator+(int amt) {
569 log_assert(list_p != nullptr);
570 ObjIterator<T> new_obj(*this);
571 new_obj.it += amt;
572 if (new_obj.it == list_p->end()) {
573 (*(new_obj.refcount_p))--;
574 new_obj.list_p = nullptr;
575 new_obj.refcount_p = nullptr;
576 }
577 return new_obj;
578 }
579
580 inline const ObjIterator<T> operator++(int) {
581 ObjIterator<T> result(*this);
582 ++(*this);
583 return result;
584 }
585 };
586
587 template<typename T>
588 struct ObjRange
589 {
590 dict<RTLIL::IdString, T> *list_p;
591 int *refcount_p;
592
593 ObjRange(decltype(list_p) list_p, int *refcount_p) : list_p(list_p), refcount_p(refcount_p) { }
594 RTLIL::ObjIterator<T> begin() { return RTLIL::ObjIterator<T>(list_p, refcount_p); }
595 RTLIL::ObjIterator<T> end() { return RTLIL::ObjIterator<T>(); }
596
597 size_t size() const {
598 return list_p->size();
599 }
600
601 operator pool<T>() const {
602 pool<T> result;
603 for (auto &it : *list_p)
604 result.insert(it.second);
605 return result;
606 }
607
608 operator std::vector<T>() const {
609 std::vector<T> result;
610 result.reserve(list_p->size());
611 for (auto &it : *list_p)
612 result.push_back(it.second);
613 return result;
614 }
615
616 pool<T> to_pool() const { return *this; }
617 std::vector<T> to_vector() const { return *this; }
618 };
619 };
620
621 struct RTLIL::Const
622 {
623 int flags;
624 std::vector<RTLIL::State> bits;
625
626 Const();
627 Const(std::string str);
628 Const(int val, int width = 32);
629 Const(RTLIL::State bit, int width = 1);
630 Const(const std::vector<RTLIL::State> &bits) : bits(bits) { flags = CONST_FLAG_NONE; }
631 Const(const std::vector<bool> &bits);
632 Const(const RTLIL::Const &c);
633 RTLIL::Const &operator =(const RTLIL::Const &other) = default;
634
635 bool operator <(const RTLIL::Const &other) const;
636 bool operator ==(const RTLIL::Const &other) const;
637 bool operator !=(const RTLIL::Const &other) const;
638
639 bool as_bool() const;
640 int as_int(bool is_signed = false) const;
641 std::string as_string() const;
642 static Const from_string(const std::string &str);
643
644 std::string decode_string() const;
645
646 inline int size() const { return bits.size(); }
647 inline bool empty() const { return bits.empty(); }
648 inline RTLIL::State &operator[](int index) { return bits.at(index); }
649 inline const RTLIL::State &operator[](int index) const { return bits.at(index); }
650 inline decltype(bits)::iterator begin() { return bits.begin(); }
651 inline decltype(bits)::iterator end() { return bits.end(); }
652
653 bool is_fully_zero() const;
654 bool is_fully_ones() const;
655 bool is_fully_def() const;
656 bool is_fully_undef() const;
657
658 inline RTLIL::Const extract(int offset, int len = 1, RTLIL::State padding = RTLIL::State::S0) const {
659 RTLIL::Const ret;
660 ret.bits.reserve(len);
661 for (int i = offset; i < offset + len; i++)
662 ret.bits.push_back(i < GetSize(bits) ? bits[i] : padding);
663 return ret;
664 }
665
666 void extu(int width) {
667 bits.resize(width, RTLIL::State::S0);
668 }
669
670 void exts(int width) {
671 bits.resize(width, bits.empty() ? RTLIL::State::Sx : bits.back());
672 }
673
674 inline unsigned int hash() const {
675 unsigned int h = mkhash_init;
676 for (auto b : bits)
677 mkhash(h, b);
678 return h;
679 }
680 };
681
682 struct RTLIL::AttrObject
683 {
684 dict<RTLIL::IdString, RTLIL::Const> attributes;
685
686 bool has_attribute(RTLIL::IdString id) const;
687
688 void set_bool_attribute(RTLIL::IdString id, bool value=true);
689 bool get_bool_attribute(RTLIL::IdString id) const;
690
691 bool get_blackbox_attribute(bool ignore_wb=false) const {
692 return get_bool_attribute(ID::blackbox) || (!ignore_wb && get_bool_attribute(ID::whitebox));
693 }
694
695 void set_string_attribute(RTLIL::IdString id, string value);
696 string get_string_attribute(RTLIL::IdString id) const;
697
698 void set_strpool_attribute(RTLIL::IdString id, const pool<string> &data);
699 void add_strpool_attribute(RTLIL::IdString id, const pool<string> &data);
700 pool<string> get_strpool_attribute(RTLIL::IdString id) const;
701
702 void set_src_attribute(const std::string &src) {
703 set_string_attribute(ID::src, src);
704 }
705 std::string get_src_attribute() const {
706 return get_string_attribute(ID::src);
707 }
708
709 void set_hdlname_attribute(const vector<string> &hierarchy);
710 vector<string> get_hdlname_attribute() const;
711 };
712
713 struct RTLIL::SigChunk
714 {
715 RTLIL::Wire *wire;
716 std::vector<RTLIL::State> data; // only used if wire == NULL, LSB at index 0
717 int width, offset;
718
719 SigChunk();
720 SigChunk(const RTLIL::Const &value);
721 SigChunk(RTLIL::Wire *wire);
722 SigChunk(RTLIL::Wire *wire, int offset, int width = 1);
723 SigChunk(const std::string &str);
724 SigChunk(int val, int width = 32);
725 SigChunk(RTLIL::State bit, int width = 1);
726 SigChunk(const RTLIL::SigBit &bit);
727 SigChunk(const RTLIL::SigChunk &sigchunk);
728 RTLIL::SigChunk &operator =(const RTLIL::SigChunk &other) = default;
729
730 RTLIL::SigChunk extract(int offset, int length) const;
731 inline int size() const { return width; }
732
733 bool operator <(const RTLIL::SigChunk &other) const;
734 bool operator ==(const RTLIL::SigChunk &other) const;
735 bool operator !=(const RTLIL::SigChunk &other) const;
736 };
737
738 struct RTLIL::SigBit
739 {
740 RTLIL::Wire *wire;
741 union {
742 RTLIL::State data; // used if wire == NULL
743 int offset; // used if wire != NULL
744 };
745
746 SigBit();
747 SigBit(RTLIL::State bit);
748 SigBit(bool bit);
749 SigBit(RTLIL::Wire *wire);
750 SigBit(RTLIL::Wire *wire, int offset);
751 SigBit(const RTLIL::SigChunk &chunk);
752 SigBit(const RTLIL::SigChunk &chunk, int index);
753 SigBit(const RTLIL::SigSpec &sig);
754 SigBit(const RTLIL::SigBit &sigbit) = default;
755 RTLIL::SigBit &operator =(const RTLIL::SigBit &other) = default;
756
757 bool operator <(const RTLIL::SigBit &other) const;
758 bool operator ==(const RTLIL::SigBit &other) const;
759 bool operator !=(const RTLIL::SigBit &other) const;
760 unsigned int hash() const;
761 };
762
763 struct RTLIL::SigSpecIterator : public std::iterator<std::input_iterator_tag, RTLIL::SigSpec>
764 {
765 RTLIL::SigSpec *sig_p;
766 int index;
767
768 inline RTLIL::SigBit &operator*() const;
769 inline bool operator!=(const RTLIL::SigSpecIterator &other) const { return index != other.index; }
770 inline bool operator==(const RTLIL::SigSpecIterator &other) const { return index == other.index; }
771 inline void operator++() { index++; }
772 };
773
774 struct RTLIL::SigSpecConstIterator : public std::iterator<std::input_iterator_tag, RTLIL::SigSpec>
775 {
776 const RTLIL::SigSpec *sig_p;
777 int index;
778
779 inline const RTLIL::SigBit &operator*() const;
780 inline bool operator!=(const RTLIL::SigSpecConstIterator &other) const { return index != other.index; }
781 inline bool operator==(const RTLIL::SigSpecIterator &other) const { return index == other.index; }
782 inline void operator++() { index++; }
783 };
784
785 struct RTLIL::SigSpec
786 {
787 private:
788 int width_;
789 unsigned long hash_;
790 std::vector<RTLIL::SigChunk> chunks_; // LSB at index 0
791 std::vector<RTLIL::SigBit> bits_; // LSB at index 0
792
793 void pack() const;
794 void unpack() const;
795 void updhash() const;
796
797 inline bool packed() const {
798 return bits_.empty();
799 }
800
801 inline void inline_unpack() const {
802 if (!chunks_.empty())
803 unpack();
804 }
805
806 // Only used by Module::remove(const pool<Wire*> &wires)
807 // but cannot be more specific as it isn't yet declared
808 friend struct RTLIL::Module;
809
810 public:
811 SigSpec();
812 SigSpec(const RTLIL::SigSpec &other);
813 SigSpec(std::initializer_list<RTLIL::SigSpec> parts);
814 RTLIL::SigSpec &operator=(const RTLIL::SigSpec &other);
815
816 SigSpec(const RTLIL::Const &value);
817 SigSpec(const RTLIL::SigChunk &chunk);
818 SigSpec(RTLIL::Wire *wire);
819 SigSpec(RTLIL::Wire *wire, int offset, int width = 1);
820 SigSpec(const std::string &str);
821 SigSpec(int val, int width = 32);
822 SigSpec(RTLIL::State bit, int width = 1);
823 SigSpec(const RTLIL::SigBit &bit, int width = 1);
824 SigSpec(const std::vector<RTLIL::SigChunk> &chunks);
825 SigSpec(const std::vector<RTLIL::SigBit> &bits);
826 SigSpec(const pool<RTLIL::SigBit> &bits);
827 SigSpec(const std::set<RTLIL::SigBit> &bits);
828 SigSpec(bool bit);
829
830 SigSpec(RTLIL::SigSpec &&other) {
831 width_ = other.width_;
832 hash_ = other.hash_;
833 chunks_ = std::move(other.chunks_);
834 bits_ = std::move(other.bits_);
835 }
836
837 const RTLIL::SigSpec &operator=(RTLIL::SigSpec &&other) {
838 width_ = other.width_;
839 hash_ = other.hash_;
840 chunks_ = std::move(other.chunks_);
841 bits_ = std::move(other.bits_);
842 return *this;
843 }
844
845 size_t get_hash() const {
846 if (!hash_) hash();
847 return hash_;
848 }
849
850 inline const std::vector<RTLIL::SigChunk> &chunks() const { pack(); return chunks_; }
851 inline const std::vector<RTLIL::SigBit> &bits() const { inline_unpack(); return bits_; }
852
853 inline int size() const { return width_; }
854 inline bool empty() const { return width_ == 0; }
855
856 inline RTLIL::SigBit &operator[](int index) { inline_unpack(); return bits_.at(index); }
857 inline const RTLIL::SigBit &operator[](int index) const { inline_unpack(); return bits_.at(index); }
858
859 inline RTLIL::SigSpecIterator begin() { RTLIL::SigSpecIterator it; it.sig_p = this; it.index = 0; return it; }
860 inline RTLIL::SigSpecIterator end() { RTLIL::SigSpecIterator it; it.sig_p = this; it.index = width_; return it; }
861
862 inline RTLIL::SigSpecConstIterator begin() const { RTLIL::SigSpecConstIterator it; it.sig_p = this; it.index = 0; return it; }
863 inline RTLIL::SigSpecConstIterator end() const { RTLIL::SigSpecConstIterator it; it.sig_p = this; it.index = width_; return it; }
864
865 void sort();
866 void sort_and_unify();
867
868 void replace(const RTLIL::SigSpec &pattern, const RTLIL::SigSpec &with);
869 void replace(const RTLIL::SigSpec &pattern, const RTLIL::SigSpec &with, RTLIL::SigSpec *other) const;
870
871 void replace(const dict<RTLIL::SigBit, RTLIL::SigBit> &rules);
872 void replace(const dict<RTLIL::SigBit, RTLIL::SigBit> &rules, RTLIL::SigSpec *other) const;
873
874 void replace(const std::map<RTLIL::SigBit, RTLIL::SigBit> &rules);
875 void replace(const std::map<RTLIL::SigBit, RTLIL::SigBit> &rules, RTLIL::SigSpec *other) const;
876
877 void replace(int offset, const RTLIL::SigSpec &with);
878
879 void remove(const RTLIL::SigSpec &pattern);
880 void remove(const RTLIL::SigSpec &pattern, RTLIL::SigSpec *other) const;
881 void remove2(const RTLIL::SigSpec &pattern, RTLIL::SigSpec *other);
882
883 void remove(const pool<RTLIL::SigBit> &pattern);
884 void remove(const pool<RTLIL::SigBit> &pattern, RTLIL::SigSpec *other) const;
885 void remove2(const pool<RTLIL::SigBit> &pattern, RTLIL::SigSpec *other);
886 void remove2(const std::set<RTLIL::SigBit> &pattern, RTLIL::SigSpec *other);
887
888 void remove(int offset, int length = 1);
889 void remove_const();
890
891 RTLIL::SigSpec extract(const RTLIL::SigSpec &pattern, const RTLIL::SigSpec *other = NULL) const;
892 RTLIL::SigSpec extract(const pool<RTLIL::SigBit> &pattern, const RTLIL::SigSpec *other = NULL) const;
893 RTLIL::SigSpec extract(int offset, int length = 1) const;
894 RTLIL::SigSpec extract_end(int offset) const { return extract(offset, width_ - offset); }
895
896 void append(const RTLIL::SigSpec &signal);
897 inline void append(Wire *wire) { append(RTLIL::SigSpec(wire)); }
898 inline void append(const RTLIL::SigChunk &chunk) { append(RTLIL::SigSpec(chunk)); }
899 inline void append(const RTLIL::Const &const_) { append(RTLIL::SigSpec(const_)); }
900
901 void append(const RTLIL::SigBit &bit);
902 inline void append(RTLIL::State state) { append(RTLIL::SigBit(state)); }
903 inline void append(bool bool_) { append(RTLIL::SigBit(bool_)); }
904
905 void extend_u0(int width, bool is_signed = false);
906
907 RTLIL::SigSpec repeat(int num) const;
908
909 void reverse() { inline_unpack(); std::reverse(bits_.begin(), bits_.end()); }
910
911 bool operator <(const RTLIL::SigSpec &other) const;
912 bool operator ==(const RTLIL::SigSpec &other) const;
913 inline bool operator !=(const RTLIL::SigSpec &other) const { return !(*this == other); }
914
915 bool is_wire() const;
916 bool is_chunk() const;
917 inline bool is_bit() const { return width_ == 1; }
918
919 bool is_fully_const() const;
920 bool is_fully_zero() const;
921 bool is_fully_ones() const;
922 bool is_fully_def() const;
923 bool is_fully_undef() const;
924 bool has_const() const;
925 bool has_marked_bits() const;
926
927 bool as_bool() const;
928 int as_int(bool is_signed = false) const;
929 std::string as_string() const;
930 RTLIL::Const as_const() const;
931 RTLIL::Wire *as_wire() const;
932 RTLIL::SigChunk as_chunk() const;
933 RTLIL::SigBit as_bit() const;
934
935 bool match(const char* pattern) const;
936
937 std::set<RTLIL::SigBit> to_sigbit_set() const;
938 pool<RTLIL::SigBit> to_sigbit_pool() const;
939 std::vector<RTLIL::SigBit> to_sigbit_vector() const;
940 std::map<RTLIL::SigBit, RTLIL::SigBit> to_sigbit_map(const RTLIL::SigSpec &other) const;
941 dict<RTLIL::SigBit, RTLIL::SigBit> to_sigbit_dict(const RTLIL::SigSpec &other) const;
942
943 static bool parse(RTLIL::SigSpec &sig, RTLIL::Module *module, std::string str);
944 static bool parse_sel(RTLIL::SigSpec &sig, RTLIL::Design *design, RTLIL::Module *module, std::string str);
945 static bool parse_rhs(const RTLIL::SigSpec &lhs, RTLIL::SigSpec &sig, RTLIL::Module *module, std::string str);
946
947 operator std::vector<RTLIL::SigChunk>() const { return chunks(); }
948 operator std::vector<RTLIL::SigBit>() const { return bits(); }
949 const RTLIL::SigBit &at(int offset, const RTLIL::SigBit &defval) { return offset < width_ ? (*this)[offset] : defval; }
950
951 unsigned int hash() const { if (!hash_) updhash(); return hash_; };
952
953 #ifndef NDEBUG
954 void check() const;
955 #else
956 void check() const { }
957 #endif
958 };
959
960 struct RTLIL::Selection
961 {
962 bool full_selection;
963 pool<RTLIL::IdString> selected_modules;
964 dict<RTLIL::IdString, pool<RTLIL::IdString>> selected_members;
965
966 Selection(bool full = true) : full_selection(full) { }
967
968 bool selected_module(RTLIL::IdString mod_name) const;
969 bool selected_whole_module(RTLIL::IdString mod_name) const;
970 bool selected_member(RTLIL::IdString mod_name, RTLIL::IdString memb_name) const;
971 void optimize(RTLIL::Design *design);
972
973 template<typename T1> void select(T1 *module) {
974 if (!full_selection && selected_modules.count(module->name) == 0) {
975 selected_modules.insert(module->name);
976 selected_members.erase(module->name);
977 }
978 }
979
980 template<typename T1, typename T2> void select(T1 *module, T2 *member) {
981 if (!full_selection && selected_modules.count(module->name) == 0)
982 selected_members[module->name].insert(member->name);
983 }
984
985 bool empty() const {
986 return !full_selection && selected_modules.empty() && selected_members.empty();
987 }
988 };
989
990 struct RTLIL::Monitor
991 {
992 unsigned int hashidx_;
993 unsigned int hash() const { return hashidx_; }
994
995 Monitor() {
996 static unsigned int hashidx_count = 123456789;
997 hashidx_count = mkhash_xorshift(hashidx_count);
998 hashidx_ = hashidx_count;
999 }
1000
1001 virtual ~Monitor() { }
1002 virtual void notify_module_add(RTLIL::Module*) { }
1003 virtual void notify_module_del(RTLIL::Module*) { }
1004 virtual void notify_connect(RTLIL::Cell*, const RTLIL::IdString&, const RTLIL::SigSpec&, const RTLIL::SigSpec&) { }
1005 virtual void notify_connect(RTLIL::Module*, const RTLIL::SigSig&) { }
1006 virtual void notify_connect(RTLIL::Module*, const std::vector<RTLIL::SigSig>&) { }
1007 virtual void notify_blackout(RTLIL::Module*) { }
1008 };
1009
1010 // Forward declaration; defined in preproc.h.
1011 struct define_map_t;
1012
1013 struct RTLIL::Design
1014 {
1015 unsigned int hashidx_;
1016 unsigned int hash() const { return hashidx_; }
1017
1018 pool<RTLIL::Monitor*> monitors;
1019 dict<std::string, std::string> scratchpad;
1020
1021 int refcount_modules_;
1022 dict<RTLIL::IdString, RTLIL::Module*> modules_;
1023 std::vector<AST::AstNode*> verilog_packages, verilog_globals;
1024 std::unique_ptr<define_map_t> verilog_defines;
1025
1026 std::vector<RTLIL::Selection> selection_stack;
1027 dict<RTLIL::IdString, RTLIL::Selection> selection_vars;
1028 std::string selected_active_module;
1029
1030 Design();
1031 ~Design();
1032
1033 RTLIL::ObjRange<RTLIL::Module*> modules();
1034 RTLIL::Module *module(RTLIL::IdString name);
1035 RTLIL::Module *top_module();
1036
1037 bool has(RTLIL::IdString id) const {
1038 return modules_.count(id) != 0;
1039 }
1040
1041 void add(RTLIL::Module *module);
1042 RTLIL::Module *addModule(RTLIL::IdString name);
1043 void remove(RTLIL::Module *module);
1044 void rename(RTLIL::Module *module, RTLIL::IdString new_name);
1045
1046 void scratchpad_unset(const std::string &varname);
1047
1048 void scratchpad_set_int(const std::string &varname, int value);
1049 void scratchpad_set_bool(const std::string &varname, bool value);
1050 void scratchpad_set_string(const std::string &varname, std::string value);
1051
1052 int scratchpad_get_int(const std::string &varname, int default_value = 0) const;
1053 bool scratchpad_get_bool(const std::string &varname, bool default_value = false) const;
1054 std::string scratchpad_get_string(const std::string &varname, const std::string &default_value = std::string()) const;
1055
1056 void sort();
1057 void check();
1058 void optimize();
1059
1060 bool selected_module(RTLIL::IdString mod_name) const;
1061 bool selected_whole_module(RTLIL::IdString mod_name) const;
1062 bool selected_member(RTLIL::IdString mod_name, RTLIL::IdString memb_name) const;
1063
1064 bool selected_module(RTLIL::Module *mod) const;
1065 bool selected_whole_module(RTLIL::Module *mod) const;
1066
1067 RTLIL::Selection &selection() {
1068 return selection_stack.back();
1069 }
1070
1071 const RTLIL::Selection &selection() const {
1072 return selection_stack.back();
1073 }
1074
1075 bool full_selection() const {
1076 return selection_stack.back().full_selection;
1077 }
1078
1079 template<typename T1> bool selected(T1 *module) const {
1080 return selected_module(module->name);
1081 }
1082
1083 template<typename T1, typename T2> bool selected(T1 *module, T2 *member) const {
1084 return selected_member(module->name, member->name);
1085 }
1086
1087 template<typename T1> void select(T1 *module) {
1088 if (selection_stack.size() > 0) {
1089 RTLIL::Selection &sel = selection_stack.back();
1090 sel.select(module);
1091 }
1092 }
1093
1094 template<typename T1, typename T2> void select(T1 *module, T2 *member) {
1095 if (selection_stack.size() > 0) {
1096 RTLIL::Selection &sel = selection_stack.back();
1097 sel.select(module, member);
1098 }
1099 }
1100
1101
1102 std::vector<RTLIL::Module*> selected_modules() const;
1103 std::vector<RTLIL::Module*> selected_whole_modules() const;
1104 std::vector<RTLIL::Module*> selected_whole_modules_warn() const;
1105 #ifdef WITH_PYTHON
1106 static std::map<unsigned int, RTLIL::Design*> *get_all_designs(void);
1107 #endif
1108 };
1109
1110 struct RTLIL::Module : public RTLIL::AttrObject
1111 {
1112 unsigned int hashidx_;
1113 unsigned int hash() const { return hashidx_; }
1114
1115 protected:
1116 void add(RTLIL::Wire *wire);
1117 void add(RTLIL::Cell *cell);
1118
1119 public:
1120 RTLIL::Design *design;
1121 pool<RTLIL::Monitor*> monitors;
1122
1123 int refcount_wires_;
1124 int refcount_cells_;
1125
1126 dict<RTLIL::IdString, RTLIL::Wire*> wires_;
1127 dict<RTLIL::IdString, RTLIL::Cell*> cells_;
1128 std::vector<RTLIL::SigSig> connections_;
1129
1130 RTLIL::IdString name;
1131 idict<RTLIL::IdString> avail_parameters;
1132 dict<RTLIL::IdString, RTLIL::Const> parameter_default_values;
1133 dict<RTLIL::IdString, RTLIL::Memory*> memories;
1134 dict<RTLIL::IdString, RTLIL::Process*> processes;
1135
1136 Module();
1137 virtual ~Module();
1138 virtual RTLIL::IdString derive(RTLIL::Design *design, const dict<RTLIL::IdString, RTLIL::Const> &parameters, bool mayfail = false);
1139 virtual RTLIL::IdString derive(RTLIL::Design *design, const dict<RTLIL::IdString, RTLIL::Const> &parameters, const dict<RTLIL::IdString, RTLIL::Module*> &interfaces, const dict<RTLIL::IdString, RTLIL::IdString> &modports, bool mayfail = false);
1140 virtual size_t count_id(RTLIL::IdString id);
1141 virtual void reprocess_module(RTLIL::Design *design, const dict<RTLIL::IdString, RTLIL::Module *> &local_interfaces);
1142
1143 virtual void sort();
1144 virtual void check();
1145 virtual void optimize();
1146 virtual void makeblackbox();
1147
1148 void connect(const RTLIL::SigSig &conn);
1149 void connect(const RTLIL::SigSpec &lhs, const RTLIL::SigSpec &rhs);
1150 void new_connections(const std::vector<RTLIL::SigSig> &new_conn);
1151 const std::vector<RTLIL::SigSig> &connections() const;
1152
1153 std::vector<RTLIL::IdString> ports;
1154 void fixup_ports();
1155
1156 template<typename T> void rewrite_sigspecs(T &functor);
1157 template<typename T> void rewrite_sigspecs2(T &functor);
1158 void cloneInto(RTLIL::Module *new_mod) const;
1159 virtual RTLIL::Module *clone() const;
1160
1161 bool has_memories() const;
1162 bool has_processes() const;
1163
1164 bool has_memories_warn() const;
1165 bool has_processes_warn() const;
1166
1167 std::vector<RTLIL::Wire*> selected_wires() const;
1168 std::vector<RTLIL::Cell*> selected_cells() const;
1169
1170 template<typename T> bool selected(T *member) const {
1171 return design->selected_member(name, member->name);
1172 }
1173
1174 RTLIL::Wire* wire(RTLIL::IdString id) {
1175 auto it = wires_.find(id);
1176 return it == wires_.end() ? nullptr : it->second;
1177 }
1178 RTLIL::Cell* cell(RTLIL::IdString id) {
1179 auto it = cells_.find(id);
1180 return it == cells_.end() ? nullptr : it->second;
1181 }
1182
1183 RTLIL::ObjRange<RTLIL::Wire*> wires() { return RTLIL::ObjRange<RTLIL::Wire*>(&wires_, &refcount_wires_); }
1184 RTLIL::ObjRange<RTLIL::Cell*> cells() { return RTLIL::ObjRange<RTLIL::Cell*>(&cells_, &refcount_cells_); }
1185
1186 // Removing wires is expensive. If you have to remove wires, remove them all at once.
1187 void remove(const pool<RTLIL::Wire*> &wires);
1188 void remove(RTLIL::Cell *cell);
1189
1190 void rename(RTLIL::Wire *wire, RTLIL::IdString new_name);
1191 void rename(RTLIL::Cell *cell, RTLIL::IdString new_name);
1192 void rename(RTLIL::IdString old_name, RTLIL::IdString new_name);
1193
1194 void swap_names(RTLIL::Wire *w1, RTLIL::Wire *w2);
1195 void swap_names(RTLIL::Cell *c1, RTLIL::Cell *c2);
1196
1197 RTLIL::IdString uniquify(RTLIL::IdString name);
1198 RTLIL::IdString uniquify(RTLIL::IdString name, int &index);
1199
1200 RTLIL::Wire *addWire(RTLIL::IdString name, int width = 1);
1201 RTLIL::Wire *addWire(RTLIL::IdString name, const RTLIL::Wire *other);
1202
1203 RTLIL::Cell *addCell(RTLIL::IdString name, RTLIL::IdString type);
1204 RTLIL::Cell *addCell(RTLIL::IdString name, const RTLIL::Cell *other);
1205
1206 RTLIL::Memory *addMemory(RTLIL::IdString name, const RTLIL::Memory *other);
1207
1208 RTLIL::Process *addProcess(RTLIL::IdString name, const RTLIL::Process *other);
1209
1210 // The add* methods create a cell and return the created cell. All signals must exist in advance.
1211
1212 RTLIL::Cell* addNot (RTLIL::IdString name, const RTLIL::SigSpec &sig_a, const RTLIL::SigSpec &sig_y, bool is_signed = false, const std::string &src = "");
1213 RTLIL::Cell* addPos (RTLIL::IdString name, const RTLIL::SigSpec &sig_a, const RTLIL::SigSpec &sig_y, bool is_signed = false, const std::string &src = "");
1214 RTLIL::Cell* addNeg (RTLIL::IdString name, const RTLIL::SigSpec &sig_a, const RTLIL::SigSpec &sig_y, bool is_signed = false, const std::string &src = "");
1215
1216 RTLIL::Cell* addAnd (RTLIL::IdString name, const RTLIL::SigSpec &sig_a, const RTLIL::SigSpec &sig_b, const RTLIL::SigSpec &sig_y, bool is_signed = false, const std::string &src = "");
1217 RTLIL::Cell* addOr (RTLIL::IdString name, const RTLIL::SigSpec &sig_a, const RTLIL::SigSpec &sig_b, const RTLIL::SigSpec &sig_y, bool is_signed = false, const std::string &src = "");
1218 RTLIL::Cell* addXor (RTLIL::IdString name, const RTLIL::SigSpec &sig_a, const RTLIL::SigSpec &sig_b, const RTLIL::SigSpec &sig_y, bool is_signed = false, const std::string &src = "");
1219 RTLIL::Cell* addXnor (RTLIL::IdString name, const RTLIL::SigSpec &sig_a, const RTLIL::SigSpec &sig_b, const RTLIL::SigSpec &sig_y, bool is_signed = false, const std::string &src = "");
1220
1221 RTLIL::Cell* addReduceAnd (RTLIL::IdString name, const RTLIL::SigSpec &sig_a, const RTLIL::SigSpec &sig_y, bool is_signed = false, const std::string &src = "");
1222 RTLIL::Cell* addReduceOr (RTLIL::IdString name, const RTLIL::SigSpec &sig_a, const RTLIL::SigSpec &sig_y, bool is_signed = false, const std::string &src = "");
1223 RTLIL::Cell* addReduceXor (RTLIL::IdString name, const RTLIL::SigSpec &sig_a, const RTLIL::SigSpec &sig_y, bool is_signed = false, const std::string &src = "");
1224 RTLIL::Cell* addReduceXnor (RTLIL::IdString name, const RTLIL::SigSpec &sig_a, const RTLIL::SigSpec &sig_y, bool is_signed = false, const std::string &src = "");
1225 RTLIL::Cell* addReduceBool (RTLIL::IdString name, const RTLIL::SigSpec &sig_a, const RTLIL::SigSpec &sig_y, bool is_signed = false, const std::string &src = "");
1226
1227 RTLIL::Cell* addShl (RTLIL::IdString name, const RTLIL::SigSpec &sig_a, const RTLIL::SigSpec &sig_b, const RTLIL::SigSpec &sig_y, bool is_signed = false, const std::string &src = "");
1228 RTLIL::Cell* addShr (RTLIL::IdString name, const RTLIL::SigSpec &sig_a, const RTLIL::SigSpec &sig_b, const RTLIL::SigSpec &sig_y, bool is_signed = false, const std::string &src = "");
1229 RTLIL::Cell* addSshl (RTLIL::IdString name, const RTLIL::SigSpec &sig_a, const RTLIL::SigSpec &sig_b, const RTLIL::SigSpec &sig_y, bool is_signed = false, const std::string &src = "");
1230 RTLIL::Cell* addSshr (RTLIL::IdString name, const RTLIL::SigSpec &sig_a, const RTLIL::SigSpec &sig_b, const RTLIL::SigSpec &sig_y, bool is_signed = false, const std::string &src = "");
1231 RTLIL::Cell* addShift (RTLIL::IdString name, const RTLIL::SigSpec &sig_a, const RTLIL::SigSpec &sig_b, const RTLIL::SigSpec &sig_y, bool is_signed = false, const std::string &src = "");
1232 RTLIL::Cell* addShiftx (RTLIL::IdString name, const RTLIL::SigSpec &sig_a, const RTLIL::SigSpec &sig_b, const RTLIL::SigSpec &sig_y, bool is_signed = false, const std::string &src = "");
1233
1234 RTLIL::Cell* addLt (RTLIL::IdString name, const RTLIL::SigSpec &sig_a, const RTLIL::SigSpec &sig_b, const RTLIL::SigSpec &sig_y, bool is_signed = false, const std::string &src = "");
1235 RTLIL::Cell* addLe (RTLIL::IdString name, const RTLIL::SigSpec &sig_a, const RTLIL::SigSpec &sig_b, const RTLIL::SigSpec &sig_y, bool is_signed = false, const std::string &src = "");
1236 RTLIL::Cell* addEq (RTLIL::IdString name, const RTLIL::SigSpec &sig_a, const RTLIL::SigSpec &sig_b, const RTLIL::SigSpec &sig_y, bool is_signed = false, const std::string &src = "");
1237 RTLIL::Cell* addNe (RTLIL::IdString name, const RTLIL::SigSpec &sig_a, const RTLIL::SigSpec &sig_b, const RTLIL::SigSpec &sig_y, bool is_signed = false, const std::string &src = "");
1238 RTLIL::Cell* addEqx (RTLIL::IdString name, const RTLIL::SigSpec &sig_a, const RTLIL::SigSpec &sig_b, const RTLIL::SigSpec &sig_y, bool is_signed = false, const std::string &src = "");
1239 RTLIL::Cell* addNex (RTLIL::IdString name, const RTLIL::SigSpec &sig_a, const RTLIL::SigSpec &sig_b, const RTLIL::SigSpec &sig_y, bool is_signed = false, const std::string &src = "");
1240 RTLIL::Cell* addGe (RTLIL::IdString name, const RTLIL::SigSpec &sig_a, const RTLIL::SigSpec &sig_b, const RTLIL::SigSpec &sig_y, bool is_signed = false, const std::string &src = "");
1241 RTLIL::Cell* addGt (RTLIL::IdString name, const RTLIL::SigSpec &sig_a, const RTLIL::SigSpec &sig_b, const RTLIL::SigSpec &sig_y, bool is_signed = false, const std::string &src = "");
1242
1243 RTLIL::Cell* addAdd (RTLIL::IdString name, const RTLIL::SigSpec &sig_a, const RTLIL::SigSpec &sig_b, const RTLIL::SigSpec &sig_y, bool is_signed = false, const std::string &src = "");
1244 RTLIL::Cell* addSub (RTLIL::IdString name, const RTLIL::SigSpec &sig_a, const RTLIL::SigSpec &sig_b, const RTLIL::SigSpec &sig_y, bool is_signed = false, const std::string &src = "");
1245 RTLIL::Cell* addMul (RTLIL::IdString name, const RTLIL::SigSpec &sig_a, const RTLIL::SigSpec &sig_b, const RTLIL::SigSpec &sig_y, bool is_signed = false, const std::string &src = "");
1246 // truncating division
1247 RTLIL::Cell* addDiv (RTLIL::IdString name, const RTLIL::SigSpec &sig_a, const RTLIL::SigSpec &sig_b, const RTLIL::SigSpec &sig_y, bool is_signed = false, const std::string &src = "");
1248 // truncating modulo
1249 RTLIL::Cell* addMod (RTLIL::IdString name, const RTLIL::SigSpec &sig_a, const RTLIL::SigSpec &sig_b, const RTLIL::SigSpec &sig_y, bool is_signed = false, const std::string &src = "");
1250 RTLIL::Cell* addDivFloor (RTLIL::IdString name, const RTLIL::SigSpec &sig_a, const RTLIL::SigSpec &sig_b, const RTLIL::SigSpec &sig_y, bool is_signed = false, const std::string &src = "");
1251 RTLIL::Cell* addModFloor (RTLIL::IdString name, const RTLIL::SigSpec &sig_a, const RTLIL::SigSpec &sig_b, const RTLIL::SigSpec &sig_y, bool is_signed = false, const std::string &src = "");
1252 RTLIL::Cell* addPow (RTLIL::IdString name, const RTLIL::SigSpec &sig_a, const RTLIL::SigSpec &sig_b, const RTLIL::SigSpec &sig_y, bool a_signed = false, bool b_signed = false, const std::string &src = "");
1253
1254 RTLIL::Cell* addLogicNot (RTLIL::IdString name, const RTLIL::SigSpec &sig_a, const RTLIL::SigSpec &sig_y, bool is_signed = false, const std::string &src = "");
1255 RTLIL::Cell* addLogicAnd (RTLIL::IdString name, const RTLIL::SigSpec &sig_a, const RTLIL::SigSpec &sig_b, const RTLIL::SigSpec &sig_y, bool is_signed = false, const std::string &src = "");
1256 RTLIL::Cell* addLogicOr (RTLIL::IdString name, const RTLIL::SigSpec &sig_a, const RTLIL::SigSpec &sig_b, const RTLIL::SigSpec &sig_y, bool is_signed = false, const std::string &src = "");
1257
1258 RTLIL::Cell* addMux (RTLIL::IdString name, const RTLIL::SigSpec &sig_a, const RTLIL::SigSpec &sig_b, const RTLIL::SigSpec &sig_s, const RTLIL::SigSpec &sig_y, const std::string &src = "");
1259 RTLIL::Cell* addPmux (RTLIL::IdString name, const RTLIL::SigSpec &sig_a, const RTLIL::SigSpec &sig_b, const RTLIL::SigSpec &sig_s, const RTLIL::SigSpec &sig_y, const std::string &src = "");
1260
1261 RTLIL::Cell* addSlice (RTLIL::IdString name, const RTLIL::SigSpec &sig_a, const RTLIL::SigSpec &sig_y, RTLIL::Const offset, const std::string &src = "");
1262 RTLIL::Cell* addConcat (RTLIL::IdString name, const RTLIL::SigSpec &sig_a, const RTLIL::SigSpec &sig_b, const RTLIL::SigSpec &sig_y, const std::string &src = "");
1263 RTLIL::Cell* addLut (RTLIL::IdString name, const RTLIL::SigSpec &sig_a, const RTLIL::SigSpec &sig_y, RTLIL::Const lut, const std::string &src = "");
1264 RTLIL::Cell* addTribuf (RTLIL::IdString name, const RTLIL::SigSpec &sig_a, const RTLIL::SigSpec &sig_en, const RTLIL::SigSpec &sig_y, const std::string &src = "");
1265 RTLIL::Cell* addAssert (RTLIL::IdString name, const RTLIL::SigSpec &sig_a, const RTLIL::SigSpec &sig_en, const std::string &src = "");
1266 RTLIL::Cell* addAssume (RTLIL::IdString name, const RTLIL::SigSpec &sig_a, const RTLIL::SigSpec &sig_en, const std::string &src = "");
1267 RTLIL::Cell* addLive (RTLIL::IdString name, const RTLIL::SigSpec &sig_a, const RTLIL::SigSpec &sig_en, const std::string &src = "");
1268 RTLIL::Cell* addFair (RTLIL::IdString name, const RTLIL::SigSpec &sig_a, const RTLIL::SigSpec &sig_en, const std::string &src = "");
1269 RTLIL::Cell* addCover (RTLIL::IdString name, const RTLIL::SigSpec &sig_a, const RTLIL::SigSpec &sig_en, const std::string &src = "");
1270 RTLIL::Cell* addEquiv (RTLIL::IdString name, const RTLIL::SigSpec &sig_a, const RTLIL::SigSpec &sig_b, const RTLIL::SigSpec &sig_y, const std::string &src = "");
1271
1272 RTLIL::Cell* addSr (RTLIL::IdString name, const RTLIL::SigSpec &sig_set, const RTLIL::SigSpec &sig_clr, const RTLIL::SigSpec &sig_q, bool set_polarity = true, bool clr_polarity = true, const std::string &src = "");
1273 RTLIL::Cell* addFf (RTLIL::IdString name, const RTLIL::SigSpec &sig_d, const RTLIL::SigSpec &sig_q, const std::string &src = "");
1274 RTLIL::Cell* addDff (RTLIL::IdString name, const RTLIL::SigSpec &sig_clk, const RTLIL::SigSpec &sig_d, const RTLIL::SigSpec &sig_q, bool clk_polarity = true, const std::string &src = "");
1275 RTLIL::Cell* addDffe (RTLIL::IdString name, const RTLIL::SigSpec &sig_clk, const RTLIL::SigSpec &sig_en, const RTLIL::SigSpec &sig_d, const RTLIL::SigSpec &sig_q, bool clk_polarity = true, bool en_polarity = true, const std::string &src = "");
1276 RTLIL::Cell* addDffsr (RTLIL::IdString name, const RTLIL::SigSpec &sig_clk, const RTLIL::SigSpec &sig_set, const RTLIL::SigSpec &sig_clr, RTLIL::SigSpec sig_d, const RTLIL::SigSpec &sig_q, bool clk_polarity = true, bool set_polarity = true, bool clr_polarity = true, const std::string &src = "");
1277 RTLIL::Cell* addAdff (RTLIL::IdString name, const RTLIL::SigSpec &sig_clk, const RTLIL::SigSpec &sig_arst, const RTLIL::SigSpec &sig_d, const RTLIL::SigSpec &sig_q, RTLIL::Const arst_value, bool clk_polarity = true, bool arst_polarity = true, const std::string &src = "");
1278 RTLIL::Cell* addDlatch (RTLIL::IdString name, const RTLIL::SigSpec &sig_en, const RTLIL::SigSpec &sig_d, const RTLIL::SigSpec &sig_q, bool en_polarity = true, const std::string &src = "");
1279 RTLIL::Cell* addDlatchsr (RTLIL::IdString name, const RTLIL::SigSpec &sig_en, const RTLIL::SigSpec &sig_set, const RTLIL::SigSpec &sig_clr, RTLIL::SigSpec sig_d, const RTLIL::SigSpec &sig_q, bool en_polarity = true, bool set_polarity = true, bool clr_polarity = true, const std::string &src = "");
1280
1281 RTLIL::Cell* addBufGate (RTLIL::IdString name, const RTLIL::SigBit &sig_a, const RTLIL::SigBit &sig_y, const std::string &src = "");
1282 RTLIL::Cell* addNotGate (RTLIL::IdString name, const RTLIL::SigBit &sig_a, const RTLIL::SigBit &sig_y, const std::string &src = "");
1283 RTLIL::Cell* addAndGate (RTLIL::IdString name, const RTLIL::SigBit &sig_a, const RTLIL::SigBit &sig_b, const RTLIL::SigBit &sig_y, const std::string &src = "");
1284 RTLIL::Cell* addNandGate (RTLIL::IdString name, const RTLIL::SigBit &sig_a, const RTLIL::SigBit &sig_b, const RTLIL::SigBit &sig_y, const std::string &src = "");
1285 RTLIL::Cell* addOrGate (RTLIL::IdString name, const RTLIL::SigBit &sig_a, const RTLIL::SigBit &sig_b, const RTLIL::SigBit &sig_y, const std::string &src = "");
1286 RTLIL::Cell* addNorGate (RTLIL::IdString name, const RTLIL::SigBit &sig_a, const RTLIL::SigBit &sig_b, const RTLIL::SigBit &sig_y, const std::string &src = "");
1287 RTLIL::Cell* addXorGate (RTLIL::IdString name, const RTLIL::SigBit &sig_a, const RTLIL::SigBit &sig_b, const RTLIL::SigBit &sig_y, const std::string &src = "");
1288 RTLIL::Cell* addXnorGate (RTLIL::IdString name, const RTLIL::SigBit &sig_a, const RTLIL::SigBit &sig_b, const RTLIL::SigBit &sig_y, const std::string &src = "");
1289 RTLIL::Cell* addAndnotGate (RTLIL::IdString name, const RTLIL::SigBit &sig_a, const RTLIL::SigBit &sig_b, const RTLIL::SigBit &sig_y, const std::string &src = "");
1290 RTLIL::Cell* addOrnotGate (RTLIL::IdString name, const RTLIL::SigBit &sig_a, const RTLIL::SigBit &sig_b, const RTLIL::SigBit &sig_y, const std::string &src = "");
1291 RTLIL::Cell* addMuxGate (RTLIL::IdString name, const RTLIL::SigBit &sig_a, const RTLIL::SigBit &sig_b, const RTLIL::SigBit &sig_s, const RTLIL::SigBit &sig_y, const std::string &src = "");
1292 RTLIL::Cell* addNmuxGate (RTLIL::IdString name, const RTLIL::SigBit &sig_a, const RTLIL::SigBit &sig_b, const RTLIL::SigBit &sig_s, const RTLIL::SigBit &sig_y, const std::string &src = "");
1293 RTLIL::Cell* addAoi3Gate (RTLIL::IdString name, const RTLIL::SigBit &sig_a, const RTLIL::SigBit &sig_b, const RTLIL::SigBit &sig_c, const RTLIL::SigBit &sig_y, const std::string &src = "");
1294 RTLIL::Cell* addOai3Gate (RTLIL::IdString name, const RTLIL::SigBit &sig_a, const RTLIL::SigBit &sig_b, const RTLIL::SigBit &sig_c, const RTLIL::SigBit &sig_y, const std::string &src = "");
1295 RTLIL::Cell* addAoi4Gate (RTLIL::IdString name, const RTLIL::SigBit &sig_a, const RTLIL::SigBit &sig_b, const RTLIL::SigBit &sig_c, const RTLIL::SigBit &sig_d, const RTLIL::SigBit &sig_y, const std::string &src = "");
1296 RTLIL::Cell* addOai4Gate (RTLIL::IdString name, const RTLIL::SigBit &sig_a, const RTLIL::SigBit &sig_b, const RTLIL::SigBit &sig_c, const RTLIL::SigBit &sig_d, const RTLIL::SigBit &sig_y, const std::string &src = "");
1297
1298 RTLIL::Cell* addFfGate (RTLIL::IdString name, const RTLIL::SigSpec &sig_d, const RTLIL::SigSpec &sig_q, const std::string &src = "");
1299 RTLIL::Cell* addDffGate (RTLIL::IdString name, const RTLIL::SigSpec &sig_clk, const RTLIL::SigSpec &sig_d, const RTLIL::SigSpec &sig_q, bool clk_polarity = true, const std::string &src = "");
1300 RTLIL::Cell* addDffeGate (RTLIL::IdString name, const RTLIL::SigSpec &sig_clk, const RTLIL::SigSpec &sig_en, const RTLIL::SigSpec &sig_d, const RTLIL::SigSpec &sig_q, bool clk_polarity = true, bool en_polarity = true, const std::string &src = "");
1301 RTLIL::Cell* addDffsrGate (RTLIL::IdString name, const RTLIL::SigSpec &sig_clk, const RTLIL::SigSpec &sig_set, const RTLIL::SigSpec &sig_clr,
1302 RTLIL::SigSpec sig_d, const RTLIL::SigSpec &sig_q, bool clk_polarity = true, bool set_polarity = true, bool clr_polarity = true, const std::string &src = "");
1303 RTLIL::Cell* addAdffGate (RTLIL::IdString name, const RTLIL::SigSpec &sig_clk, const RTLIL::SigSpec &sig_arst, const RTLIL::SigSpec &sig_d, const RTLIL::SigSpec &sig_q,
1304 bool arst_value = false, bool clk_polarity = true, bool arst_polarity = true, const std::string &src = "");
1305 RTLIL::Cell* addDlatchGate (RTLIL::IdString name, const RTLIL::SigSpec &sig_en, const RTLIL::SigSpec &sig_d, const RTLIL::SigSpec &sig_q, bool en_polarity = true, const std::string &src = "");
1306 RTLIL::Cell* addDlatchsrGate (RTLIL::IdString name, const RTLIL::SigSpec &sig_en, const RTLIL::SigSpec &sig_set, const RTLIL::SigSpec &sig_clr,
1307 RTLIL::SigSpec sig_d, const RTLIL::SigSpec &sig_q, bool en_polarity = true, bool set_polarity = true, bool clr_polarity = true, const std::string &src = "");
1308
1309 // The methods without the add* prefix create a cell and an output signal. They return the newly created output signal.
1310
1311 RTLIL::SigSpec Not (RTLIL::IdString name, const RTLIL::SigSpec &sig_a, bool is_signed = false, const std::string &src = "");
1312 RTLIL::SigSpec Pos (RTLIL::IdString name, const RTLIL::SigSpec &sig_a, bool is_signed = false, const std::string &src = "");
1313 RTLIL::SigSpec Bu0 (RTLIL::IdString name, const RTLIL::SigSpec &sig_a, bool is_signed = false, const std::string &src = "");
1314 RTLIL::SigSpec Neg (RTLIL::IdString name, const RTLIL::SigSpec &sig_a, bool is_signed = false, const std::string &src = "");
1315
1316 RTLIL::SigSpec And (RTLIL::IdString name, const RTLIL::SigSpec &sig_a, const RTLIL::SigSpec &sig_b, bool is_signed = false, const std::string &src = "");
1317 RTLIL::SigSpec Or (RTLIL::IdString name, const RTLIL::SigSpec &sig_a, const RTLIL::SigSpec &sig_b, bool is_signed = false, const std::string &src = "");
1318 RTLIL::SigSpec Xor (RTLIL::IdString name, const RTLIL::SigSpec &sig_a, const RTLIL::SigSpec &sig_b, bool is_signed = false, const std::string &src = "");
1319 RTLIL::SigSpec Xnor (RTLIL::IdString name, const RTLIL::SigSpec &sig_a, const RTLIL::SigSpec &sig_b, bool is_signed = false, const std::string &src = "");
1320
1321 RTLIL::SigSpec ReduceAnd (RTLIL::IdString name, const RTLIL::SigSpec &sig_a, bool is_signed = false, const std::string &src = "");
1322 RTLIL::SigSpec ReduceOr (RTLIL::IdString name, const RTLIL::SigSpec &sig_a, bool is_signed = false, const std::string &src = "");
1323 RTLIL::SigSpec ReduceXor (RTLIL::IdString name, const RTLIL::SigSpec &sig_a, bool is_signed = false, const std::string &src = "");
1324 RTLIL::SigSpec ReduceXnor (RTLIL::IdString name, const RTLIL::SigSpec &sig_a, bool is_signed = false, const std::string &src = "");
1325 RTLIL::SigSpec ReduceBool (RTLIL::IdString name, const RTLIL::SigSpec &sig_a, bool is_signed = false, const std::string &src = "");
1326
1327 RTLIL::SigSpec Shl (RTLIL::IdString name, const RTLIL::SigSpec &sig_a, const RTLIL::SigSpec &sig_b, bool is_signed = false, const std::string &src = "");
1328 RTLIL::SigSpec Shr (RTLIL::IdString name, const RTLIL::SigSpec &sig_a, const RTLIL::SigSpec &sig_b, bool is_signed = false, const std::string &src = "");
1329 RTLIL::SigSpec Sshl (RTLIL::IdString name, const RTLIL::SigSpec &sig_a, const RTLIL::SigSpec &sig_b, bool is_signed = false, const std::string &src = "");
1330 RTLIL::SigSpec Sshr (RTLIL::IdString name, const RTLIL::SigSpec &sig_a, const RTLIL::SigSpec &sig_b, bool is_signed = false, const std::string &src = "");
1331 RTLIL::SigSpec Shift (RTLIL::IdString name, const RTLIL::SigSpec &sig_a, const RTLIL::SigSpec &sig_b, bool is_signed = false, const std::string &src = "");
1332 RTLIL::SigSpec Shiftx (RTLIL::IdString name, const RTLIL::SigSpec &sig_a, const RTLIL::SigSpec &sig_b, bool is_signed = false, const std::string &src = "");
1333
1334 RTLIL::SigSpec Lt (RTLIL::IdString name, const RTLIL::SigSpec &sig_a, const RTLIL::SigSpec &sig_b, bool is_signed = false, const std::string &src = "");
1335 RTLIL::SigSpec Le (RTLIL::IdString name, const RTLIL::SigSpec &sig_a, const RTLIL::SigSpec &sig_b, bool is_signed = false, const std::string &src = "");
1336 RTLIL::SigSpec Eq (RTLIL::IdString name, const RTLIL::SigSpec &sig_a, const RTLIL::SigSpec &sig_b, bool is_signed = false, const std::string &src = "");
1337 RTLIL::SigSpec Ne (RTLIL::IdString name, const RTLIL::SigSpec &sig_a, const RTLIL::SigSpec &sig_b, bool is_signed = false, const std::string &src = "");
1338 RTLIL::SigSpec Eqx (RTLIL::IdString name, const RTLIL::SigSpec &sig_a, const RTLIL::SigSpec &sig_b, bool is_signed = false, const std::string &src = "");
1339 RTLIL::SigSpec Nex (RTLIL::IdString name, const RTLIL::SigSpec &sig_a, const RTLIL::SigSpec &sig_b, bool is_signed = false, const std::string &src = "");
1340 RTLIL::SigSpec Ge (RTLIL::IdString name, const RTLIL::SigSpec &sig_a, const RTLIL::SigSpec &sig_b, bool is_signed = false, const std::string &src = "");
1341 RTLIL::SigSpec Gt (RTLIL::IdString name, const RTLIL::SigSpec &sig_a, const RTLIL::SigSpec &sig_b, bool is_signed = false, const std::string &src = "");
1342
1343 RTLIL::SigSpec Add (RTLIL::IdString name, const RTLIL::SigSpec &sig_a, const RTLIL::SigSpec &sig_b, bool is_signed = false, const std::string &src = "");
1344 RTLIL::SigSpec Sub (RTLIL::IdString name, const RTLIL::SigSpec &sig_a, const RTLIL::SigSpec &sig_b, bool is_signed = false, const std::string &src = "");
1345 RTLIL::SigSpec Mul (RTLIL::IdString name, const RTLIL::SigSpec &sig_a, const RTLIL::SigSpec &sig_b, bool is_signed = false, const std::string &src = "");
1346 // truncating division
1347 RTLIL::SigSpec Div (RTLIL::IdString name, const RTLIL::SigSpec &sig_a, const RTLIL::SigSpec &sig_b, bool is_signed = false, const std::string &src = "");
1348 // truncating modulo
1349 RTLIL::SigSpec Mod (RTLIL::IdString name, const RTLIL::SigSpec &sig_a, const RTLIL::SigSpec &sig_b, bool is_signed = false, const std::string &src = "");
1350 RTLIL::SigSpec DivFloor (RTLIL::IdString name, const RTLIL::SigSpec &sig_a, const RTLIL::SigSpec &sig_b, bool is_signed = false, const std::string &src = "");
1351 RTLIL::SigSpec ModFloor (RTLIL::IdString name, const RTLIL::SigSpec &sig_a, const RTLIL::SigSpec &sig_b, bool is_signed = false, const std::string &src = "");
1352 RTLIL::SigSpec Pow (RTLIL::IdString name, const RTLIL::SigSpec &sig_a, const RTLIL::SigSpec &sig_b, bool a_signed = false, bool b_signed = false, const std::string &src = "");
1353
1354 RTLIL::SigSpec LogicNot (RTLIL::IdString name, const RTLIL::SigSpec &sig_a, bool is_signed = false, const std::string &src = "");
1355 RTLIL::SigSpec LogicAnd (RTLIL::IdString name, const RTLIL::SigSpec &sig_a, const RTLIL::SigSpec &sig_b, bool is_signed = false, const std::string &src = "");
1356 RTLIL::SigSpec LogicOr (RTLIL::IdString name, const RTLIL::SigSpec &sig_a, const RTLIL::SigSpec &sig_b, bool is_signed = false, const std::string &src = "");
1357
1358 RTLIL::SigSpec Mux (RTLIL::IdString name, const RTLIL::SigSpec &sig_a, const RTLIL::SigSpec &sig_b, const RTLIL::SigSpec &sig_s, const std::string &src = "");
1359 RTLIL::SigSpec Pmux (RTLIL::IdString name, const RTLIL::SigSpec &sig_a, const RTLIL::SigSpec &sig_b, const RTLIL::SigSpec &sig_s, const std::string &src = "");
1360
1361 RTLIL::SigBit BufGate (RTLIL::IdString name, const RTLIL::SigBit &sig_a, const std::string &src = "");
1362 RTLIL::SigBit NotGate (RTLIL::IdString name, const RTLIL::SigBit &sig_a, const std::string &src = "");
1363 RTLIL::SigBit AndGate (RTLIL::IdString name, const RTLIL::SigBit &sig_a, const RTLIL::SigBit &sig_b, const std::string &src = "");
1364 RTLIL::SigBit NandGate (RTLIL::IdString name, const RTLIL::SigBit &sig_a, const RTLIL::SigBit &sig_b, const std::string &src = "");
1365 RTLIL::SigBit OrGate (RTLIL::IdString name, const RTLIL::SigBit &sig_a, const RTLIL::SigBit &sig_b, const std::string &src = "");
1366 RTLIL::SigBit NorGate (RTLIL::IdString name, const RTLIL::SigBit &sig_a, const RTLIL::SigBit &sig_b, const std::string &src = "");
1367 RTLIL::SigBit XorGate (RTLIL::IdString name, const RTLIL::SigBit &sig_a, const RTLIL::SigBit &sig_b, const std::string &src = "");
1368 RTLIL::SigBit XnorGate (RTLIL::IdString name, const RTLIL::SigBit &sig_a, const RTLIL::SigBit &sig_b, const std::string &src = "");
1369 RTLIL::SigBit AndnotGate (RTLIL::IdString name, const RTLIL::SigBit &sig_a, const RTLIL::SigBit &sig_b, const std::string &src = "");
1370 RTLIL::SigBit OrnotGate (RTLIL::IdString name, const RTLIL::SigBit &sig_a, const RTLIL::SigBit &sig_b, const std::string &src = "");
1371 RTLIL::SigBit MuxGate (RTLIL::IdString name, const RTLIL::SigBit &sig_a, const RTLIL::SigBit &sig_b, const RTLIL::SigBit &sig_s, const std::string &src = "");
1372 RTLIL::SigBit NmuxGate (RTLIL::IdString name, const RTLIL::SigBit &sig_a, const RTLIL::SigBit &sig_b, const RTLIL::SigBit &sig_s, const std::string &src = "");
1373 RTLIL::SigBit Aoi3Gate (RTLIL::IdString name, const RTLIL::SigBit &sig_a, const RTLIL::SigBit &sig_b, const RTLIL::SigBit &sig_c, const std::string &src = "");
1374 RTLIL::SigBit Oai3Gate (RTLIL::IdString name, const RTLIL::SigBit &sig_a, const RTLIL::SigBit &sig_b, const RTLIL::SigBit &sig_c, const std::string &src = "");
1375 RTLIL::SigBit Aoi4Gate (RTLIL::IdString name, const RTLIL::SigBit &sig_a, const RTLIL::SigBit &sig_b, const RTLIL::SigBit &sig_c, const RTLIL::SigBit &sig_d, const std::string &src = "");
1376 RTLIL::SigBit Oai4Gate (RTLIL::IdString name, const RTLIL::SigBit &sig_a, const RTLIL::SigBit &sig_b, const RTLIL::SigBit &sig_c, const RTLIL::SigBit &sig_d, const std::string &src = "");
1377
1378 RTLIL::SigSpec Anyconst (RTLIL::IdString name, int width = 1, const std::string &src = "");
1379 RTLIL::SigSpec Anyseq (RTLIL::IdString name, int width = 1, const std::string &src = "");
1380 RTLIL::SigSpec Allconst (RTLIL::IdString name, int width = 1, const std::string &src = "");
1381 RTLIL::SigSpec Allseq (RTLIL::IdString name, int width = 1, const std::string &src = "");
1382 RTLIL::SigSpec Initstate (RTLIL::IdString name, const std::string &src = "");
1383
1384 #ifdef WITH_PYTHON
1385 static std::map<unsigned int, RTLIL::Module*> *get_all_modules(void);
1386 #endif
1387 };
1388
1389 struct RTLIL::Wire : public RTLIL::AttrObject
1390 {
1391 unsigned int hashidx_;
1392 unsigned int hash() const { return hashidx_; }
1393
1394 protected:
1395 // use module->addWire() and module->remove() to create or destroy wires
1396 friend struct RTLIL::Module;
1397 Wire();
1398 ~Wire();
1399
1400 public:
1401 // do not simply copy wires
1402 Wire(RTLIL::Wire &other) = delete;
1403 void operator=(RTLIL::Wire &other) = delete;
1404
1405 RTLIL::Module *module;
1406 RTLIL::IdString name;
1407 int width, start_offset, port_id;
1408 bool port_input, port_output, upto, is_signed;
1409
1410 #ifdef WITH_PYTHON
1411 static std::map<unsigned int, RTLIL::Wire*> *get_all_wires(void);
1412 #endif
1413 };
1414
1415 struct RTLIL::Memory : public RTLIL::AttrObject
1416 {
1417 unsigned int hashidx_;
1418 unsigned int hash() const { return hashidx_; }
1419
1420 Memory();
1421
1422 RTLIL::IdString name;
1423 int width, start_offset, size;
1424 #ifdef WITH_PYTHON
1425 ~Memory();
1426 static std::map<unsigned int, RTLIL::Memory*> *get_all_memorys(void);
1427 #endif
1428 };
1429
1430 struct RTLIL::Cell : public RTLIL::AttrObject
1431 {
1432 unsigned int hashidx_;
1433 unsigned int hash() const { return hashidx_; }
1434
1435 protected:
1436 // use module->addCell() and module->remove() to create or destroy cells
1437 friend struct RTLIL::Module;
1438 Cell();
1439 ~Cell();
1440
1441 public:
1442 // do not simply copy cells
1443 Cell(RTLIL::Cell &other) = delete;
1444 void operator=(RTLIL::Cell &other) = delete;
1445
1446 RTLIL::Module *module;
1447 RTLIL::IdString name;
1448 RTLIL::IdString type;
1449 dict<RTLIL::IdString, RTLIL::SigSpec> connections_;
1450 dict<RTLIL::IdString, RTLIL::Const> parameters;
1451
1452 // access cell ports
1453 bool hasPort(RTLIL::IdString portname) const;
1454 void unsetPort(RTLIL::IdString portname);
1455 void setPort(RTLIL::IdString portname, RTLIL::SigSpec signal);
1456 const RTLIL::SigSpec &getPort(RTLIL::IdString portname) const;
1457 const dict<RTLIL::IdString, RTLIL::SigSpec> &connections() const;
1458
1459 // information about cell ports
1460 bool known() const;
1461 bool input(RTLIL::IdString portname) const;
1462 bool output(RTLIL::IdString portname) const;
1463
1464 // access cell parameters
1465 bool hasParam(RTLIL::IdString paramname) const;
1466 void unsetParam(RTLIL::IdString paramname);
1467 void setParam(RTLIL::IdString paramname, RTLIL::Const value);
1468 const RTLIL::Const &getParam(RTLIL::IdString paramname) const;
1469
1470 void sort();
1471 void check();
1472 void fixup_parameters(bool set_a_signed = false, bool set_b_signed = false);
1473
1474 bool has_keep_attr() const {
1475 return get_bool_attribute(ID::keep) || (module && module->design && module->design->module(type) &&
1476 module->design->module(type)->get_bool_attribute(ID::keep));
1477 }
1478
1479 template<typename T> void rewrite_sigspecs(T &functor);
1480 template<typename T> void rewrite_sigspecs2(T &functor);
1481
1482 #ifdef WITH_PYTHON
1483 static std::map<unsigned int, RTLIL::Cell*> *get_all_cells(void);
1484 #endif
1485 };
1486
1487 struct RTLIL::CaseRule : public RTLIL::AttrObject
1488 {
1489 std::vector<RTLIL::SigSpec> compare;
1490 std::vector<RTLIL::SigSig> actions;
1491 std::vector<RTLIL::SwitchRule*> switches;
1492
1493 ~CaseRule();
1494 void optimize();
1495
1496 bool empty() const;
1497
1498 template<typename T> void rewrite_sigspecs(T &functor);
1499 template<typename T> void rewrite_sigspecs2(T &functor);
1500 RTLIL::CaseRule *clone() const;
1501 };
1502
1503 struct RTLIL::SwitchRule : public RTLIL::AttrObject
1504 {
1505 RTLIL::SigSpec signal;
1506 std::vector<RTLIL::CaseRule*> cases;
1507
1508 ~SwitchRule();
1509
1510 bool empty() const;
1511
1512 template<typename T> void rewrite_sigspecs(T &functor);
1513 template<typename T> void rewrite_sigspecs2(T &functor);
1514 RTLIL::SwitchRule *clone() const;
1515 };
1516
1517 struct RTLIL::SyncRule
1518 {
1519 RTLIL::SyncType type;
1520 RTLIL::SigSpec signal;
1521 std::vector<RTLIL::SigSig> actions;
1522
1523 template<typename T> void rewrite_sigspecs(T &functor);
1524 template<typename T> void rewrite_sigspecs2(T &functor);
1525 RTLIL::SyncRule *clone() const;
1526 };
1527
1528 struct RTLIL::Process : public RTLIL::AttrObject
1529 {
1530 RTLIL::IdString name;
1531 RTLIL::CaseRule root_case;
1532 std::vector<RTLIL::SyncRule*> syncs;
1533
1534 ~Process();
1535
1536 template<typename T> void rewrite_sigspecs(T &functor);
1537 template<typename T> void rewrite_sigspecs2(T &functor);
1538 RTLIL::Process *clone() const;
1539 };
1540
1541
1542 inline RTLIL::SigBit::SigBit() : wire(NULL), data(RTLIL::State::S0) { }
1543 inline RTLIL::SigBit::SigBit(RTLIL::State bit) : wire(NULL), data(bit) { }
1544 inline RTLIL::SigBit::SigBit(bool bit) : wire(NULL), data(bit ? State::S1 : State::S0) { }
1545 inline RTLIL::SigBit::SigBit(RTLIL::Wire *wire) : wire(wire), offset(0) { log_assert(wire && wire->width == 1); }
1546 inline RTLIL::SigBit::SigBit(RTLIL::Wire *wire, int offset) : wire(wire), offset(offset) { log_assert(wire != nullptr); }
1547 inline RTLIL::SigBit::SigBit(const RTLIL::SigChunk &chunk) : wire(chunk.wire) { log_assert(chunk.width == 1); if (wire) offset = chunk.offset; else data = chunk.data[0]; }
1548 inline RTLIL::SigBit::SigBit(const RTLIL::SigChunk &chunk, int index) : wire(chunk.wire) { if (wire) offset = chunk.offset + index; else data = chunk.data[index]; }
1549
1550 inline bool RTLIL::SigBit::operator<(const RTLIL::SigBit &other) const {
1551 if (wire == other.wire)
1552 return wire ? (offset < other.offset) : (data < other.data);
1553 if (wire != nullptr && other.wire != nullptr)
1554 return wire->name < other.wire->name;
1555 return (wire != nullptr) < (other.wire != nullptr);
1556 }
1557
1558 inline bool RTLIL::SigBit::operator==(const RTLIL::SigBit &other) const {
1559 return (wire == other.wire) && (wire ? (offset == other.offset) : (data == other.data));
1560 }
1561
1562 inline bool RTLIL::SigBit::operator!=(const RTLIL::SigBit &other) const {
1563 return (wire != other.wire) || (wire ? (offset != other.offset) : (data != other.data));
1564 }
1565
1566 inline unsigned int RTLIL::SigBit::hash() const {
1567 if (wire)
1568 return mkhash_add(wire->name.hash(), offset);
1569 return data;
1570 }
1571
1572 inline RTLIL::SigBit &RTLIL::SigSpecIterator::operator*() const {
1573 return (*sig_p)[index];
1574 }
1575
1576 inline const RTLIL::SigBit &RTLIL::SigSpecConstIterator::operator*() const {
1577 return (*sig_p)[index];
1578 }
1579
1580 inline RTLIL::SigBit::SigBit(const RTLIL::SigSpec &sig) {
1581 log_assert(sig.size() == 1 && sig.chunks().size() == 1);
1582 *this = SigBit(sig.chunks().front());
1583 }
1584
1585 template<typename T>
1586 void RTLIL::Module::rewrite_sigspecs(T &functor)
1587 {
1588 for (auto &it : cells_)
1589 it.second->rewrite_sigspecs(functor);
1590 for (auto &it : processes)
1591 it.second->rewrite_sigspecs(functor);
1592 for (auto &it : connections_) {
1593 functor(it.first);
1594 functor(it.second);
1595 }
1596 }
1597
1598 template<typename T>
1599 void RTLIL::Module::rewrite_sigspecs2(T &functor)
1600 {
1601 for (auto &it : cells_)
1602 it.second->rewrite_sigspecs2(functor);
1603 for (auto &it : processes)
1604 it.second->rewrite_sigspecs2(functor);
1605 for (auto &it : connections_) {
1606 functor(it.first, it.second);
1607 }
1608 }
1609
1610 template<typename T>
1611 void RTLIL::Cell::rewrite_sigspecs(T &functor) {
1612 for (auto &it : connections_)
1613 functor(it.second);
1614 }
1615
1616 template<typename T>
1617 void RTLIL::Cell::rewrite_sigspecs2(T &functor) {
1618 for (auto &it : connections_)
1619 functor(it.second);
1620 }
1621
1622 template<typename T>
1623 void RTLIL::CaseRule::rewrite_sigspecs(T &functor) {
1624 for (auto &it : compare)
1625 functor(it);
1626 for (auto &it : actions) {
1627 functor(it.first);
1628 functor(it.second);
1629 }
1630 for (auto it : switches)
1631 it->rewrite_sigspecs(functor);
1632 }
1633
1634 template<typename T>
1635 void RTLIL::CaseRule::rewrite_sigspecs2(T &functor) {
1636 for (auto &it : compare)
1637 functor(it);
1638 for (auto &it : actions) {
1639 functor(it.first, it.second);
1640 }
1641 for (auto it : switches)
1642 it->rewrite_sigspecs2(functor);
1643 }
1644
1645 template<typename T>
1646 void RTLIL::SwitchRule::rewrite_sigspecs(T &functor)
1647 {
1648 functor(signal);
1649 for (auto it : cases)
1650 it->rewrite_sigspecs(functor);
1651 }
1652
1653 template<typename T>
1654 void RTLIL::SwitchRule::rewrite_sigspecs2(T &functor)
1655 {
1656 functor(signal);
1657 for (auto it : cases)
1658 it->rewrite_sigspecs2(functor);
1659 }
1660
1661 template<typename T>
1662 void RTLIL::SyncRule::rewrite_sigspecs(T &functor)
1663 {
1664 functor(signal);
1665 for (auto &it : actions) {
1666 functor(it.first);
1667 functor(it.second);
1668 }
1669 }
1670
1671 template<typename T>
1672 void RTLIL::SyncRule::rewrite_sigspecs2(T &functor)
1673 {
1674 functor(signal);
1675 for (auto &it : actions) {
1676 functor(it.first, it.second);
1677 }
1678 }
1679
1680 template<typename T>
1681 void RTLIL::Process::rewrite_sigspecs(T &functor)
1682 {
1683 root_case.rewrite_sigspecs(functor);
1684 for (auto it : syncs)
1685 it->rewrite_sigspecs(functor);
1686 }
1687
1688 template<typename T>
1689 void RTLIL::Process::rewrite_sigspecs2(T &functor)
1690 {
1691 root_case.rewrite_sigspecs2(functor);
1692 for (auto it : syncs)
1693 it->rewrite_sigspecs2(functor);
1694 }
1695
1696 YOSYS_NAMESPACE_END
1697
1698 #endif