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