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