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