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