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