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