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