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