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