2 * yosys -- Yosys Open SYnthesis Suite
4 * Copyright (C) 2012 Clifford Wolf <clifford@clifford.at>
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.
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.
20 #include "kernel/yosys.h"
29 enum State
: unsigned char {
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)
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
49 enum ConstFlags
: unsigned char {
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
67 struct SigSpecIterator
;
68 struct SigSpecConstIterator
;
75 typedef std::pair
<SigSpec
, SigSpec
> SigSig
;
79 // the global id string cache
81 static struct destruct_guard_t
{
82 bool ok
; // POD, will be initialized to zero
83 destruct_guard_t() { ok
= true; }
84 ~destruct_guard_t() { ok
= false; }
87 static std::vector
<int> global_refcount_storage_
;
88 static std::vector
<char*> global_id_storage_
;
89 static dict
<char*, int, hash_cstr_ops
> global_id_index_
;
90 static std::vector
<int> global_free_idx_list_
;
92 static inline int get_reference(int idx
)
94 global_refcount_storage_
.at(idx
)++;
98 static inline int get_reference(const char *p
)
100 log_assert(destruct_guard
.ok
);
103 log_assert(p
[1] != 0);
104 log_assert(p
[0] == '$' || p
[0] == '\\');
107 auto it
= global_id_index_
.find((char*)p
);
108 if (it
!= global_id_index_
.end()) {
109 global_refcount_storage_
.at(it
->second
)++;
113 if (global_free_idx_list_
.empty()) {
114 log_assert(global_id_storage_
.size() < 0x40000000);
115 global_free_idx_list_
.push_back(global_id_storage_
.size());
116 global_id_storage_
.push_back(nullptr);
117 global_refcount_storage_
.push_back(0);
120 int idx
= global_free_idx_list_
.back();
121 global_free_idx_list_
.pop_back();
122 global_id_storage_
.at(idx
) = strdup(p
);
123 global_id_index_
[global_id_storage_
.at(idx
)] = idx
;
124 global_refcount_storage_
.at(idx
)++;
126 // Avoid Create->Delete->Create pattern
127 static IdString last_created_id
;
128 put_reference(last_created_id
.index_
);
129 last_created_id
.index_
= idx
;
130 get_reference(last_created_id
.index_
);
133 log("#X# New IdString '%s' with index %d.\n", p
, idx
);
134 log_backtrace("-X- ", yosys_xtrace
-1);
140 static inline void put_reference(int idx
)
142 // put_reference() may be called from destructors after the destructor of
143 // global_refcount_storage_ has been run. in this case we simply do nothing.
144 if (!destruct_guard
.ok
)
147 log_assert(global_refcount_storage_
.at(idx
) > 0);
149 if (--global_refcount_storage_
.at(idx
) != 0)
153 log("#X# Removed IdString '%s' with index %d.\n", global_id_storage_
.at(idx
), idx
);
154 log_backtrace("-X- ", yosys_xtrace
-1);
157 global_id_index_
.erase(global_id_storage_
.at(idx
));
158 free(global_id_storage_
.at(idx
));
159 global_id_storage_
.at(idx
) = nullptr;
160 global_free_idx_list_
.push_back(idx
);
163 // the actual IdString object is just is a single int
167 IdString() : index_(get_reference("")) { }
168 IdString(const char *str
) : index_(get_reference(str
)) { }
169 IdString(const IdString
&str
) : index_(get_reference(str
.index_
)) { }
170 IdString(const std::string
&str
) : index_(get_reference(str
.c_str())) { }
171 ~IdString() { put_reference(index_
); }
173 void operator=(const IdString
&rhs
) {
174 put_reference(index_
);
175 index_
= get_reference(rhs
.index_
);
178 void operator=(const char *rhs
) {
183 void operator=(const std::string
&rhs
) {
188 const char *c_str() const {
189 return global_id_storage_
.at(index_
);
192 std::string
str() const {
193 return std::string(global_id_storage_
.at(index_
));
196 bool operator<(const IdString
&rhs
) const {
197 return index_
< rhs
.index_
;
200 bool operator==(const IdString
&rhs
) const { return index_
== rhs
.index_
; }
201 bool operator!=(const IdString
&rhs
) const { return index_
!= rhs
.index_
; }
203 // The methods below are just convenience functions for better compatibility with std::string.
205 bool operator==(const std::string
&rhs
) const { return str() == rhs
; }
206 bool operator!=(const std::string
&rhs
) const { return str() != rhs
; }
208 bool operator==(const char *rhs
) const { return strcmp(c_str(), rhs
) == 0; }
209 bool operator!=(const char *rhs
) const { return strcmp(c_str(), rhs
) != 0; }
211 char operator[](size_t i
) const {
212 const char *p
= c_str();
213 for (; i
!= 0; i
--, p
++)
218 std::string
substr(size_t pos
= 0, size_t len
= std::string::npos
) const {
219 if (len
== std::string::npos
|| len
>= strlen(c_str() + pos
))
220 return std::string(c_str() + pos
);
222 return std::string(c_str() + pos
, len
);
225 size_t size() const {
230 return c_str()[0] == 0;
237 unsigned int hash() const {
241 // The following is a helper key_compare class. Instead of for example std::set<Cell*>
242 // use std::set<Cell*, IdString::compare_ptr_by_name<Cell>> if the order of cells in the
243 // set has an influence on the algorithm.
245 template<typename T
> struct compare_ptr_by_name
{
246 bool operator()(const T
*a
, const T
*b
) const {
247 return (a
== nullptr || b
== nullptr) ? (a
< b
) : (a
->name
< b
->name
);
251 // often one needs to check if a given IdString is part of a list (for example a list
252 // of cell types). the following functions helps with that.
254 template<typename T
, typename
... Args
>
255 bool in(T first
, Args
... rest
) const {
256 return in(first
) || in(rest
...);
259 bool in(IdString rhs
) const { return *this == rhs
; }
260 bool in(const char *rhs
) const { return *this == rhs
; }
261 bool in(const std::string
&rhs
) const { return *this == rhs
; }
262 bool in(const pool
<IdString
> &rhs
) const { return rhs
.count(*this) != 0; }
265 static inline std::string
escape_id(std::string str
) {
266 if (str
.size() > 0 && str
[0] != '\\' && str
[0] != '$')
271 static inline std::string
unescape_id(std::string str
) {
276 if (str
[1] == '$' || str
[1] == '\\')
278 if (str
[1] >= '0' && str
[1] <= '9')
280 return str
.substr(1);
283 static inline std::string
unescape_id(RTLIL::IdString str
) {
284 return unescape_id(str
.str());
287 static inline const char *id2cstr(const RTLIL::IdString
&str
) {
291 template <typename T
> struct sort_by_name_id
{
292 bool operator()(T
*a
, T
*b
) const {
293 return a
->name
< b
->name
;
297 template <typename T
> struct sort_by_name_str
{
298 bool operator()(T
*a
, T
*b
) const {
299 return strcmp(a
->name
.c_str(), b
->name
.c_str()) < 0;
303 struct sort_by_id_str
{
304 bool operator()(RTLIL::IdString a
, RTLIL::IdString b
) const {
305 return strcmp(a
.c_str(), b
.c_str()) < 0;
309 // see calc.cc for the implementation of this functions
310 RTLIL::Const
const_not (const RTLIL::Const
&arg1
, const RTLIL::Const
&arg2
, bool signed1
, bool signed2
, int result_len
);
311 RTLIL::Const
const_and (const RTLIL::Const
&arg1
, const RTLIL::Const
&arg2
, bool signed1
, bool signed2
, int result_len
);
312 RTLIL::Const
const_or (const RTLIL::Const
&arg1
, const RTLIL::Const
&arg2
, bool signed1
, bool signed2
, int result_len
);
313 RTLIL::Const
const_xor (const RTLIL::Const
&arg1
, const RTLIL::Const
&arg2
, bool signed1
, bool signed2
, int result_len
);
314 RTLIL::Const
const_xnor (const RTLIL::Const
&arg1
, const RTLIL::Const
&arg2
, bool signed1
, bool signed2
, int result_len
);
316 RTLIL::Const
const_reduce_and (const RTLIL::Const
&arg1
, const RTLIL::Const
&arg2
, bool signed1
, bool signed2
, int result_len
);
317 RTLIL::Const
const_reduce_or (const RTLIL::Const
&arg1
, const RTLIL::Const
&arg2
, bool signed1
, bool signed2
, int result_len
);
318 RTLIL::Const
const_reduce_xor (const RTLIL::Const
&arg1
, const RTLIL::Const
&arg2
, bool signed1
, bool signed2
, int result_len
);
319 RTLIL::Const
const_reduce_xnor (const RTLIL::Const
&arg1
, const RTLIL::Const
&arg2
, bool signed1
, bool signed2
, int result_len
);
320 RTLIL::Const
const_reduce_bool (const RTLIL::Const
&arg1
, const RTLIL::Const
&arg2
, bool signed1
, bool signed2
, int result_len
);
322 RTLIL::Const
const_logic_not (const RTLIL::Const
&arg1
, const RTLIL::Const
&arg2
, bool signed1
, bool signed2
, int result_len
);
323 RTLIL::Const
const_logic_and (const RTLIL::Const
&arg1
, const RTLIL::Const
&arg2
, bool signed1
, bool signed2
, int result_len
);
324 RTLIL::Const
const_logic_or (const RTLIL::Const
&arg1
, const RTLIL::Const
&arg2
, bool signed1
, bool signed2
, int result_len
);
326 RTLIL::Const
const_shl (const RTLIL::Const
&arg1
, const RTLIL::Const
&arg2
, bool signed1
, bool signed2
, int result_len
);
327 RTLIL::Const
const_shr (const RTLIL::Const
&arg1
, const RTLIL::Const
&arg2
, bool signed1
, bool signed2
, int result_len
);
328 RTLIL::Const
const_sshl (const RTLIL::Const
&arg1
, const RTLIL::Const
&arg2
, bool signed1
, bool signed2
, int result_len
);
329 RTLIL::Const
const_sshr (const RTLIL::Const
&arg1
, const RTLIL::Const
&arg2
, bool signed1
, bool signed2
, int result_len
);
330 RTLIL::Const
const_shift (const RTLIL::Const
&arg1
, const RTLIL::Const
&arg2
, bool signed1
, bool signed2
, int result_len
);
331 RTLIL::Const
const_shiftx (const RTLIL::Const
&arg1
, const RTLIL::Const
&arg2
, bool signed1
, bool signed2
, int result_len
);
333 RTLIL::Const
const_lt (const RTLIL::Const
&arg1
, const RTLIL::Const
&arg2
, bool signed1
, bool signed2
, int result_len
);
334 RTLIL::Const
const_le (const RTLIL::Const
&arg1
, const RTLIL::Const
&arg2
, bool signed1
, bool signed2
, int result_len
);
335 RTLIL::Const
const_eq (const RTLIL::Const
&arg1
, const RTLIL::Const
&arg2
, bool signed1
, bool signed2
, int result_len
);
336 RTLIL::Const
const_ne (const RTLIL::Const
&arg1
, const RTLIL::Const
&arg2
, bool signed1
, bool signed2
, int result_len
);
337 RTLIL::Const
const_eqx (const RTLIL::Const
&arg1
, const RTLIL::Const
&arg2
, bool signed1
, bool signed2
, int result_len
);
338 RTLIL::Const
const_nex (const RTLIL::Const
&arg1
, const RTLIL::Const
&arg2
, bool signed1
, bool signed2
, int result_len
);
339 RTLIL::Const
const_ge (const RTLIL::Const
&arg1
, const RTLIL::Const
&arg2
, bool signed1
, bool signed2
, int result_len
);
340 RTLIL::Const
const_gt (const RTLIL::Const
&arg1
, const RTLIL::Const
&arg2
, bool signed1
, bool signed2
, int result_len
);
342 RTLIL::Const
const_add (const RTLIL::Const
&arg1
, const RTLIL::Const
&arg2
, bool signed1
, bool signed2
, int result_len
);
343 RTLIL::Const
const_sub (const RTLIL::Const
&arg1
, const RTLIL::Const
&arg2
, bool signed1
, bool signed2
, int result_len
);
344 RTLIL::Const
const_mul (const RTLIL::Const
&arg1
, const RTLIL::Const
&arg2
, bool signed1
, bool signed2
, int result_len
);
345 RTLIL::Const
const_div (const RTLIL::Const
&arg1
, const RTLIL::Const
&arg2
, bool signed1
, bool signed2
, int result_len
);
346 RTLIL::Const
const_mod (const RTLIL::Const
&arg1
, const RTLIL::Const
&arg2
, bool signed1
, bool signed2
, int result_len
);
347 RTLIL::Const
const_pow (const RTLIL::Const
&arg1
, const RTLIL::Const
&arg2
, bool signed1
, bool signed2
, int result_len
);
349 RTLIL::Const
const_pos (const RTLIL::Const
&arg1
, const RTLIL::Const
&arg2
, bool signed1
, bool signed2
, int result_len
);
350 RTLIL::Const
const_neg (const RTLIL::Const
&arg1
, const RTLIL::Const
&arg2
, bool signed1
, bool signed2
, int result_len
);
353 // This iterator-range-pair is used for Design::modules(), Module::wires() and Module::cells().
354 // It maintains a reference counter that is used to make sure that the container is not modified while being iterated over.
359 typename dict
<RTLIL::IdString
, T
>::iterator it
;
360 dict
<RTLIL::IdString
, T
> *list_p
;
363 ObjIterator() : list_p(nullptr), refcount_p(nullptr) {
366 ObjIterator(decltype(list_p
) list_p
, int *refcount_p
) : list_p(list_p
), refcount_p(refcount_p
) {
367 if (list_p
->empty()) {
368 this->list_p
= nullptr;
369 this->refcount_p
= nullptr;
371 it
= list_p
->begin();
376 ObjIterator(const RTLIL::ObjIterator
<T
> &other
) {
378 list_p
= other
.list_p
;
379 refcount_p
= other
.refcount_p
;
384 ObjIterator
&operator=(const RTLIL::ObjIterator
<T
> &other
) {
388 list_p
= other
.list_p
;
389 refcount_p
= other
.refcount_p
;
400 inline T
operator*() const {
401 log_assert(list_p
!= nullptr);
405 inline bool operator!=(const RTLIL::ObjIterator
<T
> &other
) const {
406 if (list_p
== nullptr || other
.list_p
== nullptr)
407 return list_p
!= other
.list_p
;
408 return it
!= other
.it
;
411 inline void operator++() {
412 log_assert(list_p
!= nullptr);
413 if (++it
== list_p
->end()) {
416 refcount_p
= nullptr;
424 dict
<RTLIL::IdString
, T
> *list_p
;
427 ObjRange(decltype(list_p
) list_p
, int *refcount_p
) : list_p(list_p
), refcount_p(refcount_p
) { }
428 RTLIL::ObjIterator
<T
> begin() { return RTLIL::ObjIterator
<T
>(list_p
, refcount_p
); }
429 RTLIL::ObjIterator
<T
> end() { return RTLIL::ObjIterator
<T
>(); }
431 size_t size() const {
432 return list_p
->size();
435 operator pool
<T
>() const {
437 for (auto &it
: *list_p
)
438 result
.insert(it
.second
);
442 operator std::vector
<T
>() const {
443 std::vector
<T
> result
;
444 result
.reserve(list_p
->size());
445 for (auto &it
: *list_p
)
446 result
.push_back(it
.second
);
450 pool
<T
> to_pool() const { return *this; }
451 std::vector
<T
> to_vector() const { return *this; }
458 std::vector
<RTLIL::State
> bits
;
461 Const(std::string str
);
462 Const(int val
, int width
= 32);
463 Const(RTLIL::State bit
, int width
= 1);
464 Const(const std::vector
<RTLIL::State
> &bits
) : bits(bits
) { flags
= CONST_FLAG_NONE
; }
465 Const(const std::vector
<bool> &bits
);
467 bool operator <(const RTLIL::Const
&other
) const;
468 bool operator ==(const RTLIL::Const
&other
) const;
469 bool operator !=(const RTLIL::Const
&other
) const;
471 bool as_bool() const;
472 int as_int(bool is_signed
= false) const;
473 std::string
as_string() const;
474 static Const
from_string(std::string str
);
476 std::string
decode_string() const;
478 inline int size() const { return bits
.size(); }
479 inline RTLIL::State
&operator[](int index
) { return bits
.at(index
); }
480 inline const RTLIL::State
&operator[](int index
) const { return bits
.at(index
); }
482 inline RTLIL::Const
extract(int offset
, int len
= 1, RTLIL::State padding
= RTLIL::State::S0
) const {
484 ret
.bits
.reserve(len
);
485 for (int i
= offset
; i
< offset
+ len
; i
++)
486 ret
.bits
.push_back(i
< GetSize(bits
) ? bits
[i
] : padding
);
490 inline unsigned int hash() const {
491 unsigned int h
= mkhash_init
;
498 struct RTLIL::AttrObject
500 dict
<RTLIL::IdString
, RTLIL::Const
> attributes
;
502 void set_bool_attribute(RTLIL::IdString id
);
503 bool get_bool_attribute(RTLIL::IdString id
) const;
504 void set_strpool_attribute(RTLIL::IdString id
, const pool
<string
> &data
);
505 void add_strpool_attribute(RTLIL::IdString id
, const pool
<string
> &data
);
506 pool
<string
> get_strpool_attribute(RTLIL::IdString id
) const;
509 struct RTLIL::SigChunk
512 std::vector
<RTLIL::State
> data
; // only used if wire == NULL, LSB at index 0
516 SigChunk(const RTLIL::Const
&value
);
517 SigChunk(RTLIL::Wire
*wire
);
518 SigChunk(RTLIL::Wire
*wire
, int offset
, int width
= 1);
519 SigChunk(const std::string
&str
);
520 SigChunk(int val
, int width
= 32);
521 SigChunk(RTLIL::State bit
, int width
= 1);
522 SigChunk(RTLIL::SigBit bit
);
524 RTLIL::SigChunk
extract(int offset
, int length
) const;
526 bool operator <(const RTLIL::SigChunk
&other
) const;
527 bool operator ==(const RTLIL::SigChunk
&other
) const;
528 bool operator !=(const RTLIL::SigChunk
&other
) const;
535 RTLIL::State data
; // used if wire == NULL
536 int offset
; // used if wire != NULL
540 SigBit(RTLIL::State bit
);
542 SigBit(RTLIL::Wire
*wire
);
543 SigBit(RTLIL::Wire
*wire
, int offset
);
544 SigBit(const RTLIL::SigChunk
&chunk
);
545 SigBit(const RTLIL::SigChunk
&chunk
, int index
);
546 SigBit(const RTLIL::SigSpec
&sig
);
548 bool operator <(const RTLIL::SigBit
&other
) const;
549 bool operator ==(const RTLIL::SigBit
&other
) const;
550 bool operator !=(const RTLIL::SigBit
&other
) const;
551 unsigned int hash() const;
554 struct RTLIL::SigSpecIterator
: public std::iterator
<std::input_iterator_tag
, RTLIL::SigSpec
>
556 RTLIL::SigSpec
*sig_p
;
559 inline RTLIL::SigBit
&operator*() const;
560 inline bool operator!=(const RTLIL::SigSpecIterator
&other
) const { return index
!= other
.index
; }
561 inline bool operator==(const RTLIL::SigSpecIterator
&other
) const { return index
== other
.index
; }
562 inline void operator++() { index
++; }
565 struct RTLIL::SigSpecConstIterator
: public std::iterator
<std::input_iterator_tag
, RTLIL::SigSpec
>
567 const RTLIL::SigSpec
*sig_p
;
570 inline const RTLIL::SigBit
&operator*() const;
571 inline bool operator!=(const RTLIL::SigSpecConstIterator
&other
) const { return index
!= other
.index
; }
572 inline bool operator==(const RTLIL::SigSpecIterator
&other
) const { return index
== other
.index
; }
573 inline void operator++() { index
++; }
576 struct RTLIL::SigSpec
581 std::vector
<RTLIL::SigChunk
> chunks_
; // LSB at index 0
582 std::vector
<RTLIL::SigBit
> bits_
; // LSB at index 0
586 void updhash() const;
588 inline bool packed() const {
589 return bits_
.empty();
592 inline void inline_unpack() const {
593 if (!chunks_
.empty())
599 SigSpec(const RTLIL::SigSpec
&other
);
600 SigSpec(std::initializer_list
<RTLIL::SigSpec
> parts
);
601 const RTLIL::SigSpec
&operator=(const RTLIL::SigSpec
&other
);
603 SigSpec(const RTLIL::Const
&value
);
604 SigSpec(const RTLIL::SigChunk
&chunk
);
605 SigSpec(RTLIL::Wire
*wire
);
606 SigSpec(RTLIL::Wire
*wire
, int offset
, int width
= 1);
607 SigSpec(const std::string
&str
);
608 SigSpec(int val
, int width
= 32);
609 SigSpec(RTLIL::State bit
, int width
= 1);
610 SigSpec(RTLIL::SigBit bit
, int width
= 1);
611 SigSpec(std::vector
<RTLIL::SigChunk
> chunks
);
612 SigSpec(std::vector
<RTLIL::SigBit
> bits
);
613 SigSpec(pool
<RTLIL::SigBit
> bits
);
614 SigSpec(std::set
<RTLIL::SigBit
> bits
);
617 SigSpec(RTLIL::SigSpec
&&other
) {
618 width_
= other
.width_
;
620 chunks_
= std::move(other
.chunks_
);
621 bits_
= std::move(other
.bits_
);
624 const RTLIL::SigSpec
&operator=(RTLIL::SigSpec
&&other
) {
625 width_
= other
.width_
;
627 chunks_
= std::move(other
.chunks_
);
628 bits_
= std::move(other
.bits_
);
632 size_t get_hash() const {
637 inline const std::vector
<RTLIL::SigChunk
> &chunks() const { pack(); return chunks_
; }
638 inline const std::vector
<RTLIL::SigBit
> &bits() const { inline_unpack(); return bits_
; }
640 inline int size() const { return width_
; }
641 inline bool empty() const { return width_
== 0; }
643 inline RTLIL::SigBit
&operator[](int index
) { inline_unpack(); return bits_
.at(index
); }
644 inline const RTLIL::SigBit
&operator[](int index
) const { inline_unpack(); return bits_
.at(index
); }
646 inline RTLIL::SigSpecIterator
begin() { RTLIL::SigSpecIterator it
; it
.sig_p
= this; it
.index
= 0; return it
; }
647 inline RTLIL::SigSpecIterator
end() { RTLIL::SigSpecIterator it
; it
.sig_p
= this; it
.index
= width_
; return it
; }
649 inline RTLIL::SigSpecConstIterator
begin() const { RTLIL::SigSpecConstIterator it
; it
.sig_p
= this; it
.index
= 0; return it
; }
650 inline RTLIL::SigSpecConstIterator
end() const { RTLIL::SigSpecConstIterator it
; it
.sig_p
= this; it
.index
= width_
; return it
; }
653 void sort_and_unify();
655 void replace(const RTLIL::SigSpec
&pattern
, const RTLIL::SigSpec
&with
);
656 void replace(const RTLIL::SigSpec
&pattern
, const RTLIL::SigSpec
&with
, RTLIL::SigSpec
*other
) const;
658 void replace(const dict
<RTLIL::SigBit
, RTLIL::SigBit
> &rules
);
659 void replace(const dict
<RTLIL::SigBit
, RTLIL::SigBit
> &rules
, RTLIL::SigSpec
*other
) const;
661 void replace(const std::map
<RTLIL::SigBit
, RTLIL::SigBit
> &rules
);
662 void replace(const std::map
<RTLIL::SigBit
, RTLIL::SigBit
> &rules
, RTLIL::SigSpec
*other
) const;
664 void replace(int offset
, const RTLIL::SigSpec
&with
);
666 void remove(const RTLIL::SigSpec
&pattern
);
667 void remove(const RTLIL::SigSpec
&pattern
, RTLIL::SigSpec
*other
) const;
668 void remove2(const RTLIL::SigSpec
&pattern
, RTLIL::SigSpec
*other
);
670 void remove(const pool
<RTLIL::SigBit
> &pattern
);
671 void remove(const pool
<RTLIL::SigBit
> &pattern
, RTLIL::SigSpec
*other
) const;
672 void remove2(const pool
<RTLIL::SigBit
> &pattern
, RTLIL::SigSpec
*other
);
673 void remove2(const std::set
<RTLIL::SigBit
> &pattern
, RTLIL::SigSpec
*other
);
675 void remove(int offset
, int length
= 1);
678 RTLIL::SigSpec
extract(const RTLIL::SigSpec
&pattern
, const RTLIL::SigSpec
*other
= NULL
) const;
679 RTLIL::SigSpec
extract(const pool
<RTLIL::SigBit
> &pattern
, const RTLIL::SigSpec
*other
= NULL
) const;
680 RTLIL::SigSpec
extract(int offset
, int length
= 1) const;
682 void append(const RTLIL::SigSpec
&signal
);
683 void append_bit(const RTLIL::SigBit
&bit
);
685 void extend_u0(int width
, bool is_signed
= false);
687 RTLIL::SigSpec
repeat(int num
) const;
689 bool operator <(const RTLIL::SigSpec
&other
) const;
690 bool operator ==(const RTLIL::SigSpec
&other
) const;
691 inline bool operator !=(const RTLIL::SigSpec
&other
) const { return !(*this == other
); }
693 bool is_wire() const;
694 bool is_chunk() const;
695 inline bool is_bit() const { return width_
== 1; }
697 bool is_fully_const() const;
698 bool is_fully_zero() const;
699 bool is_fully_def() const;
700 bool is_fully_undef() const;
701 bool has_const() const;
702 bool has_marked_bits() const;
704 bool as_bool() const;
705 int as_int(bool is_signed
= false) const;
706 std::string
as_string() const;
707 RTLIL::Const
as_const() const;
708 RTLIL::Wire
*as_wire() const;
709 RTLIL::SigChunk
as_chunk() const;
710 RTLIL::SigBit
as_bit() const;
712 bool match(std::string pattern
) const;
714 std::set
<RTLIL::SigBit
> to_sigbit_set() const;
715 pool
<RTLIL::SigBit
> to_sigbit_pool() const;
716 std::vector
<RTLIL::SigBit
> to_sigbit_vector() const;
717 std::map
<RTLIL::SigBit
, RTLIL::SigBit
> to_sigbit_map(const RTLIL::SigSpec
&other
) const;
718 dict
<RTLIL::SigBit
, RTLIL::SigBit
> to_sigbit_dict(const RTLIL::SigSpec
&other
) const;
720 static bool parse(RTLIL::SigSpec
&sig
, RTLIL::Module
*module
, std::string str
);
721 static bool parse_sel(RTLIL::SigSpec
&sig
, RTLIL::Design
*design
, RTLIL::Module
*module
, std::string str
);
722 static bool parse_rhs(const RTLIL::SigSpec
&lhs
, RTLIL::SigSpec
&sig
, RTLIL::Module
*module
, std::string str
);
724 operator std::vector
<RTLIL::SigChunk
>() const { return chunks(); }
725 operator std::vector
<RTLIL::SigBit
>() const { return bits(); }
727 unsigned int hash() const { if (!hash_
) updhash(); return hash_
; };
732 void check() const { }
736 struct RTLIL::Selection
739 pool
<RTLIL::IdString
> selected_modules
;
740 dict
<RTLIL::IdString
, pool
<RTLIL::IdString
>> selected_members
;
742 Selection(bool full
= true) : full_selection(full
) { }
744 bool selected_module(RTLIL::IdString mod_name
) const;
745 bool selected_whole_module(RTLIL::IdString mod_name
) const;
746 bool selected_member(RTLIL::IdString mod_name
, RTLIL::IdString memb_name
) const;
747 void optimize(RTLIL::Design
*design
);
749 template<typename T1
> void select(T1
*module
) {
750 if (!full_selection
&& selected_modules
.count(module
->name
) == 0) {
751 selected_modules
.insert(module
->name
);
752 selected_members
.erase(module
->name
);
756 template<typename T1
, typename T2
> void select(T1
*module
, T2
*member
) {
757 if (!full_selection
&& selected_modules
.count(module
->name
) == 0)
758 selected_members
[module
->name
].insert(member
->name
);
762 return !full_selection
&& selected_modules
.empty() && selected_members
.empty();
766 struct RTLIL::Monitor
768 unsigned int hashidx_
;
769 unsigned int hash() const { return hashidx_
; }
772 static unsigned int hashidx_count
= 123456789;
773 hashidx_count
= mkhash_xorshift(hashidx_count
);
774 hashidx_
= hashidx_count
;
777 virtual ~Monitor() { }
778 virtual void notify_module_add(RTLIL::Module
*) { }
779 virtual void notify_module_del(RTLIL::Module
*) { }
780 virtual void notify_connect(RTLIL::Cell
*, const RTLIL::IdString
&, const RTLIL::SigSpec
&, RTLIL::SigSpec
&) { }
781 virtual void notify_connect(RTLIL::Module
*, const RTLIL::SigSig
&) { }
782 virtual void notify_connect(RTLIL::Module
*, const std::vector
<RTLIL::SigSig
>&) { }
783 virtual void notify_blackout(RTLIL::Module
*) { }
788 unsigned int hashidx_
;
789 unsigned int hash() const { return hashidx_
; }
791 pool
<RTLIL::Monitor
*> monitors
;
792 dict
<std::string
, std::string
> scratchpad
;
794 int refcount_modules_
;
795 dict
<RTLIL::IdString
, RTLIL::Module
*> modules_
;
796 std::vector
<AST::AstNode
*> verilog_packages
, verilog_globals
;
797 dict
<std::string
, std::pair
<std::string
, bool>> verilog_defines
;
799 std::vector
<RTLIL::Selection
> selection_stack
;
800 dict
<RTLIL::IdString
, RTLIL::Selection
> selection_vars
;
801 std::string selected_active_module
;
806 RTLIL::ObjRange
<RTLIL::Module
*> modules();
807 RTLIL::Module
*module(RTLIL::IdString name
);
808 RTLIL::Module
*top_module();
810 bool has(RTLIL::IdString id
) const {
811 return modules_
.count(id
) != 0;
814 void add(RTLIL::Module
*module
);
815 RTLIL::Module
*addModule(RTLIL::IdString name
);
816 void remove(RTLIL::Module
*module
);
817 void rename(RTLIL::Module
*module
, RTLIL::IdString new_name
);
819 void scratchpad_unset(std::string varname
);
821 void scratchpad_set_int(std::string varname
, int value
);
822 void scratchpad_set_bool(std::string varname
, bool value
);
823 void scratchpad_set_string(std::string varname
, std::string value
);
825 int scratchpad_get_int(std::string varname
, int default_value
= 0) const;
826 bool scratchpad_get_bool(std::string varname
, bool default_value
= false) const;
827 std::string
scratchpad_get_string(std::string varname
, std::string default_value
= std::string()) const;
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;
837 bool selected_module(RTLIL::Module
*mod
) const;
838 bool selected_whole_module(RTLIL::Module
*mod
) const;
840 RTLIL::Selection
&selection() {
841 return selection_stack
.back();
844 const RTLIL::Selection
&selection() const {
845 return selection_stack
.back();
848 bool full_selection() const {
849 return selection_stack
.back().full_selection
;
852 template<typename T1
> bool selected(T1
*module
) const {
853 return selected_module(module
->name
);
856 template<typename T1
, typename T2
> bool selected(T1
*module
, T2
*member
) const {
857 return selected_member(module
->name
, member
->name
);
860 template<typename T1
, typename T2
> void select(T1
*module
, T2
*member
) {
861 if (selection_stack
.size() > 0) {
862 RTLIL::Selection
&sel
= selection_stack
.back();
863 sel
.select(module
, member
);
867 std::vector
<RTLIL::Module
*> selected_modules() const;
868 std::vector
<RTLIL::Module
*> selected_whole_modules() const;
869 std::vector
<RTLIL::Module
*> selected_whole_modules_warn() const;
872 struct RTLIL::Module
: public RTLIL::AttrObject
874 unsigned int hashidx_
;
875 unsigned int hash() const { return hashidx_
; }
878 void add(RTLIL::Wire
*wire
);
879 void add(RTLIL::Cell
*cell
);
882 RTLIL::Design
*design
;
883 pool
<RTLIL::Monitor
*> monitors
;
888 dict
<RTLIL::IdString
, RTLIL::Wire
*> wires_
;
889 dict
<RTLIL::IdString
, RTLIL::Cell
*> cells_
;
890 std::vector
<RTLIL::SigSig
> connections_
;
892 RTLIL::IdString name
;
893 pool
<RTLIL::IdString
> avail_parameters
;
894 dict
<RTLIL::IdString
, RTLIL::Memory
*> memories
;
895 dict
<RTLIL::IdString
, RTLIL::Process
*> processes
;
899 virtual RTLIL::IdString
derive(RTLIL::Design
*design
, dict
<RTLIL::IdString
, RTLIL::Const
> parameters
);
900 virtual size_t count_id(RTLIL::IdString id
);
903 virtual void check();
904 virtual void optimize();
906 void connect(const RTLIL::SigSig
&conn
);
907 void connect(const RTLIL::SigSpec
&lhs
, const RTLIL::SigSpec
&rhs
);
908 void new_connections(const std::vector
<RTLIL::SigSig
> &new_conn
);
909 const std::vector
<RTLIL::SigSig
> &connections() const;
911 std::vector
<RTLIL::IdString
> ports
;
914 template<typename T
> void rewrite_sigspecs(T functor
);
915 void cloneInto(RTLIL::Module
*new_mod
) const;
916 virtual RTLIL::Module
*clone() const;
918 bool has_memories() const;
919 bool has_processes() const;
921 bool has_memories_warn() const;
922 bool has_processes_warn() const;
924 std::vector
<RTLIL::Wire
*> selected_wires() const;
925 std::vector
<RTLIL::Cell
*> selected_cells() const;
927 template<typename T
> bool selected(T
*member
) const {
928 return design
->selected_member(name
, member
->name
);
931 RTLIL::Wire
* wire(RTLIL::IdString id
) { return wires_
.count(id
) ? wires_
.at(id
) : nullptr; }
932 RTLIL::Cell
* cell(RTLIL::IdString id
) { return cells_
.count(id
) ? cells_
.at(id
) : nullptr; }
934 RTLIL::ObjRange
<RTLIL::Wire
*> wires() { return RTLIL::ObjRange
<RTLIL::Wire
*>(&wires_
, &refcount_wires_
); }
935 RTLIL::ObjRange
<RTLIL::Cell
*> cells() { return RTLIL::ObjRange
<RTLIL::Cell
*>(&cells_
, &refcount_cells_
); }
937 // Removing wires is expensive. If you have to remove wires, remove them all at once.
938 void remove(const pool
<RTLIL::Wire
*> &wires
);
939 void remove(RTLIL::Cell
*cell
);
941 void rename(RTLIL::Wire
*wire
, RTLIL::IdString new_name
);
942 void rename(RTLIL::Cell
*cell
, RTLIL::IdString new_name
);
943 void rename(RTLIL::IdString old_name
, RTLIL::IdString new_name
);
945 void swap_names(RTLIL::Wire
*w1
, RTLIL::Wire
*w2
);
946 void swap_names(RTLIL::Cell
*c1
, RTLIL::Cell
*c2
);
948 RTLIL::IdString
uniquify(RTLIL::IdString name
);
949 RTLIL::IdString
uniquify(RTLIL::IdString name
, int &index
);
951 RTLIL::Wire
*addWire(RTLIL::IdString name
, int width
= 1);
952 RTLIL::Wire
*addWire(RTLIL::IdString name
, const RTLIL::Wire
*other
);
954 RTLIL::Cell
*addCell(RTLIL::IdString name
, RTLIL::IdString type
);
955 RTLIL::Cell
*addCell(RTLIL::IdString name
, const RTLIL::Cell
*other
);
957 // The add* methods create a cell and return the created cell. All signals must exist in advance.
959 RTLIL::Cell
* addNot (RTLIL::IdString name
, RTLIL::SigSpec sig_a
, RTLIL::SigSpec sig_y
, bool is_signed
= false);
960 RTLIL::Cell
* addPos (RTLIL::IdString name
, RTLIL::SigSpec sig_a
, RTLIL::SigSpec sig_y
, bool is_signed
= false);
961 RTLIL::Cell
* addNeg (RTLIL::IdString name
, RTLIL::SigSpec sig_a
, RTLIL::SigSpec sig_y
, bool is_signed
= false);
963 RTLIL::Cell
* addAnd (RTLIL::IdString name
, RTLIL::SigSpec sig_a
, RTLIL::SigSpec sig_b
, RTLIL::SigSpec sig_y
, bool is_signed
= false);
964 RTLIL::Cell
* addOr (RTLIL::IdString name
, RTLIL::SigSpec sig_a
, RTLIL::SigSpec sig_b
, RTLIL::SigSpec sig_y
, bool is_signed
= false);
965 RTLIL::Cell
* addXor (RTLIL::IdString name
, RTLIL::SigSpec sig_a
, RTLIL::SigSpec sig_b
, RTLIL::SigSpec sig_y
, bool is_signed
= false);
966 RTLIL::Cell
* addXnor (RTLIL::IdString name
, RTLIL::SigSpec sig_a
, RTLIL::SigSpec sig_b
, RTLIL::SigSpec sig_y
, bool is_signed
= false);
968 RTLIL::Cell
* addReduceAnd (RTLIL::IdString name
, RTLIL::SigSpec sig_a
, RTLIL::SigSpec sig_y
, bool is_signed
= false);
969 RTLIL::Cell
* addReduceOr (RTLIL::IdString name
, RTLIL::SigSpec sig_a
, RTLIL::SigSpec sig_y
, bool is_signed
= false);
970 RTLIL::Cell
* addReduceXor (RTLIL::IdString name
, RTLIL::SigSpec sig_a
, RTLIL::SigSpec sig_y
, bool is_signed
= false);
971 RTLIL::Cell
* addReduceXnor (RTLIL::IdString name
, RTLIL::SigSpec sig_a
, RTLIL::SigSpec sig_y
, bool is_signed
= false);
972 RTLIL::Cell
* addReduceBool (RTLIL::IdString name
, RTLIL::SigSpec sig_a
, RTLIL::SigSpec sig_y
, bool is_signed
= false);
974 RTLIL::Cell
* addShl (RTLIL::IdString name
, RTLIL::SigSpec sig_a
, RTLIL::SigSpec sig_b
, RTLIL::SigSpec sig_y
, bool is_signed
= false);
975 RTLIL::Cell
* addShr (RTLIL::IdString name
, RTLIL::SigSpec sig_a
, RTLIL::SigSpec sig_b
, RTLIL::SigSpec sig_y
, bool is_signed
= false);
976 RTLIL::Cell
* addSshl (RTLIL::IdString name
, RTLIL::SigSpec sig_a
, RTLIL::SigSpec sig_b
, RTLIL::SigSpec sig_y
, bool is_signed
= false);
977 RTLIL::Cell
* addSshr (RTLIL::IdString name
, RTLIL::SigSpec sig_a
, RTLIL::SigSpec sig_b
, RTLIL::SigSpec sig_y
, bool is_signed
= false);
978 RTLIL::Cell
* addShift (RTLIL::IdString name
, RTLIL::SigSpec sig_a
, RTLIL::SigSpec sig_b
, RTLIL::SigSpec sig_y
, bool is_signed
= false);
979 RTLIL::Cell
* addShiftx (RTLIL::IdString name
, RTLIL::SigSpec sig_a
, RTLIL::SigSpec sig_b
, RTLIL::SigSpec sig_y
, bool is_signed
= false);
981 RTLIL::Cell
* addLt (RTLIL::IdString name
, RTLIL::SigSpec sig_a
, RTLIL::SigSpec sig_b
, RTLIL::SigSpec sig_y
, bool is_signed
= false);
982 RTLIL::Cell
* addLe (RTLIL::IdString name
, RTLIL::SigSpec sig_a
, RTLIL::SigSpec sig_b
, RTLIL::SigSpec sig_y
, bool is_signed
= false);
983 RTLIL::Cell
* addEq (RTLIL::IdString name
, RTLIL::SigSpec sig_a
, RTLIL::SigSpec sig_b
, RTLIL::SigSpec sig_y
, bool is_signed
= false);
984 RTLIL::Cell
* addNe (RTLIL::IdString name
, RTLIL::SigSpec sig_a
, RTLIL::SigSpec sig_b
, RTLIL::SigSpec sig_y
, bool is_signed
= false);
985 RTLIL::Cell
* addEqx (RTLIL::IdString name
, RTLIL::SigSpec sig_a
, RTLIL::SigSpec sig_b
, RTLIL::SigSpec sig_y
, bool is_signed
= false);
986 RTLIL::Cell
* addNex (RTLIL::IdString name
, RTLIL::SigSpec sig_a
, RTLIL::SigSpec sig_b
, RTLIL::SigSpec sig_y
, bool is_signed
= false);
987 RTLIL::Cell
* addGe (RTLIL::IdString name
, RTLIL::SigSpec sig_a
, RTLIL::SigSpec sig_b
, RTLIL::SigSpec sig_y
, bool is_signed
= false);
988 RTLIL::Cell
* addGt (RTLIL::IdString name
, RTLIL::SigSpec sig_a
, RTLIL::SigSpec sig_b
, RTLIL::SigSpec sig_y
, bool is_signed
= false);
990 RTLIL::Cell
* addAdd (RTLIL::IdString name
, RTLIL::SigSpec sig_a
, RTLIL::SigSpec sig_b
, RTLIL::SigSpec sig_y
, bool is_signed
= false);
991 RTLIL::Cell
* addSub (RTLIL::IdString name
, RTLIL::SigSpec sig_a
, RTLIL::SigSpec sig_b
, RTLIL::SigSpec sig_y
, bool is_signed
= false);
992 RTLIL::Cell
* addMul (RTLIL::IdString name
, RTLIL::SigSpec sig_a
, RTLIL::SigSpec sig_b
, RTLIL::SigSpec sig_y
, bool is_signed
= false);
993 RTLIL::Cell
* addDiv (RTLIL::IdString name
, RTLIL::SigSpec sig_a
, RTLIL::SigSpec sig_b
, RTLIL::SigSpec sig_y
, bool is_signed
= false);
994 RTLIL::Cell
* addMod (RTLIL::IdString name
, RTLIL::SigSpec sig_a
, RTLIL::SigSpec sig_b
, RTLIL::SigSpec sig_y
, bool is_signed
= false);
995 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);
997 RTLIL::Cell
* addLogicNot (RTLIL::IdString name
, RTLIL::SigSpec sig_a
, RTLIL::SigSpec sig_y
, bool is_signed
= false);
998 RTLIL::Cell
* addLogicAnd (RTLIL::IdString name
, RTLIL::SigSpec sig_a
, RTLIL::SigSpec sig_b
, RTLIL::SigSpec sig_y
, bool is_signed
= false);
999 RTLIL::Cell
* addLogicOr (RTLIL::IdString name
, RTLIL::SigSpec sig_a
, RTLIL::SigSpec sig_b
, RTLIL::SigSpec sig_y
, bool is_signed
= false);
1001 RTLIL::Cell
* addMux (RTLIL::IdString name
, RTLIL::SigSpec sig_a
, RTLIL::SigSpec sig_b
, RTLIL::SigSpec sig_s
, RTLIL::SigSpec sig_y
);
1002 RTLIL::Cell
* addPmux (RTLIL::IdString name
, RTLIL::SigSpec sig_a
, RTLIL::SigSpec sig_b
, RTLIL::SigSpec sig_s
, RTLIL::SigSpec sig_y
);
1004 RTLIL::Cell
* addSlice (RTLIL::IdString name
, RTLIL::SigSpec sig_a
, RTLIL::SigSpec sig_y
, RTLIL::Const offset
);
1005 RTLIL::Cell
* addConcat (RTLIL::IdString name
, RTLIL::SigSpec sig_a
, RTLIL::SigSpec sig_b
, RTLIL::SigSpec sig_y
);
1006 RTLIL::Cell
* addLut (RTLIL::IdString name
, RTLIL::SigSpec sig_a
, RTLIL::SigSpec sig_y
, RTLIL::Const lut
);
1007 RTLIL::Cell
* addTribuf (RTLIL::IdString name
, RTLIL::SigSpec sig_a
, RTLIL::SigSpec sig_en
, RTLIL::SigSpec sig_y
);
1008 RTLIL::Cell
* addAssert (RTLIL::IdString name
, RTLIL::SigSpec sig_a
, RTLIL::SigSpec sig_en
);
1009 RTLIL::Cell
* addAssume (RTLIL::IdString name
, RTLIL::SigSpec sig_a
, RTLIL::SigSpec sig_en
);
1010 RTLIL::Cell
* addLive (RTLIL::IdString name
, RTLIL::SigSpec sig_a
, RTLIL::SigSpec sig_en
);
1011 RTLIL::Cell
* addFair (RTLIL::IdString name
, RTLIL::SigSpec sig_a
, RTLIL::SigSpec sig_en
);
1012 RTLIL::Cell
* addCover (RTLIL::IdString name
, RTLIL::SigSpec sig_a
, RTLIL::SigSpec sig_en
);
1013 RTLIL::Cell
* addEquiv (RTLIL::IdString name
, RTLIL::SigSpec sig_a
, RTLIL::SigSpec sig_b
, RTLIL::SigSpec sig_y
);
1015 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);
1016 RTLIL::Cell
* addFf (RTLIL::IdString name
, RTLIL::SigSpec sig_d
, RTLIL::SigSpec sig_q
);
1017 RTLIL::Cell
* addDff (RTLIL::IdString name
, RTLIL::SigSpec sig_clk
, RTLIL::SigSpec sig_d
, RTLIL::SigSpec sig_q
, bool clk_polarity
= true);
1018 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);
1019 RTLIL::Cell
* addDffsr (RTLIL::IdString name
, RTLIL::SigSpec sig_clk
, RTLIL::SigSpec sig_set
, RTLIL::SigSpec sig_clr
,
1020 RTLIL::SigSpec sig_d
, RTLIL::SigSpec sig_q
, bool clk_polarity
= true, bool set_polarity
= true, bool clr_polarity
= true);
1021 RTLIL::Cell
* addAdff (RTLIL::IdString name
, RTLIL::SigSpec sig_clk
, RTLIL::SigSpec sig_arst
, RTLIL::SigSpec sig_d
, RTLIL::SigSpec sig_q
,
1022 RTLIL::Const arst_value
, bool clk_polarity
= true, bool arst_polarity
= true);
1023 RTLIL::Cell
* addDlatch (RTLIL::IdString name
, RTLIL::SigSpec sig_en
, RTLIL::SigSpec sig_d
, RTLIL::SigSpec sig_q
, bool en_polarity
= true);
1024 RTLIL::Cell
* addDlatchsr (RTLIL::IdString name
, RTLIL::SigSpec sig_en
, RTLIL::SigSpec sig_set
, RTLIL::SigSpec sig_clr
,
1025 RTLIL::SigSpec sig_d
, RTLIL::SigSpec sig_q
, bool en_polarity
= true, bool set_polarity
= true, bool clr_polarity
= true);
1027 RTLIL::Cell
* addBufGate (RTLIL::IdString name
, RTLIL::SigBit sig_a
, RTLIL::SigBit sig_y
);
1028 RTLIL::Cell
* addNotGate (RTLIL::IdString name
, RTLIL::SigBit sig_a
, RTLIL::SigBit sig_y
);
1029 RTLIL::Cell
* addAndGate (RTLIL::IdString name
, RTLIL::SigBit sig_a
, RTLIL::SigBit sig_b
, RTLIL::SigBit sig_y
);
1030 RTLIL::Cell
* addNandGate (RTLIL::IdString name
, RTLIL::SigBit sig_a
, RTLIL::SigBit sig_b
, RTLIL::SigBit sig_y
);
1031 RTLIL::Cell
* addOrGate (RTLIL::IdString name
, RTLIL::SigBit sig_a
, RTLIL::SigBit sig_b
, RTLIL::SigBit sig_y
);
1032 RTLIL::Cell
* addNorGate (RTLIL::IdString name
, RTLIL::SigBit sig_a
, RTLIL::SigBit sig_b
, RTLIL::SigBit sig_y
);
1033 RTLIL::Cell
* addXorGate (RTLIL::IdString name
, RTLIL::SigBit sig_a
, RTLIL::SigBit sig_b
, RTLIL::SigBit sig_y
);
1034 RTLIL::Cell
* addXnorGate (RTLIL::IdString name
, RTLIL::SigBit sig_a
, RTLIL::SigBit sig_b
, RTLIL::SigBit sig_y
);
1035 RTLIL::Cell
* addMuxGate (RTLIL::IdString name
, RTLIL::SigBit sig_a
, RTLIL::SigBit sig_b
, RTLIL::SigBit sig_s
, RTLIL::SigBit sig_y
);
1036 RTLIL::Cell
* addAoi3Gate (RTLIL::IdString name
, RTLIL::SigBit sig_a
, RTLIL::SigBit sig_b
, RTLIL::SigBit sig_c
, RTLIL::SigBit sig_y
);
1037 RTLIL::Cell
* addOai3Gate (RTLIL::IdString name
, RTLIL::SigBit sig_a
, RTLIL::SigBit sig_b
, RTLIL::SigBit sig_c
, RTLIL::SigBit sig_y
);
1038 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
);
1039 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
);
1041 RTLIL::Cell
* addFfGate (RTLIL::IdString name
, RTLIL::SigSpec sig_d
, RTLIL::SigSpec sig_q
);
1042 RTLIL::Cell
* addDffGate (RTLIL::IdString name
, RTLIL::SigSpec sig_clk
, RTLIL::SigSpec sig_d
, RTLIL::SigSpec sig_q
, bool clk_polarity
= true);
1043 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);
1044 RTLIL::Cell
* addDffsrGate (RTLIL::IdString name
, RTLIL::SigSpec sig_clk
, RTLIL::SigSpec sig_set
, RTLIL::SigSpec sig_clr
,
1045 RTLIL::SigSpec sig_d
, RTLIL::SigSpec sig_q
, bool clk_polarity
= true, bool set_polarity
= true, bool clr_polarity
= true);
1046 RTLIL::Cell
* addAdffGate (RTLIL::IdString name
, RTLIL::SigSpec sig_clk
, RTLIL::SigSpec sig_arst
, RTLIL::SigSpec sig_d
, RTLIL::SigSpec sig_q
,
1047 bool arst_value
= false, bool clk_polarity
= true, bool arst_polarity
= true);
1048 RTLIL::Cell
* addDlatchGate (RTLIL::IdString name
, RTLIL::SigSpec sig_en
, RTLIL::SigSpec sig_d
, RTLIL::SigSpec sig_q
, bool en_polarity
= true);
1049 RTLIL::Cell
* addDlatchsrGate (RTLIL::IdString name
, RTLIL::SigSpec sig_en
, RTLIL::SigSpec sig_set
, RTLIL::SigSpec sig_clr
,
1050 RTLIL::SigSpec sig_d
, RTLIL::SigSpec sig_q
, bool en_polarity
= true, bool set_polarity
= true, bool clr_polarity
= true);
1052 // The methods without the add* prefix create a cell and an output signal. They return the newly created output signal.
1054 RTLIL::SigSpec
Not (RTLIL::IdString name
, RTLIL::SigSpec sig_a
, bool is_signed
= false);
1055 RTLIL::SigSpec
Pos (RTLIL::IdString name
, RTLIL::SigSpec sig_a
, bool is_signed
= false);
1056 RTLIL::SigSpec
Bu0 (RTLIL::IdString name
, RTLIL::SigSpec sig_a
, bool is_signed
= false);
1057 RTLIL::SigSpec
Neg (RTLIL::IdString name
, RTLIL::SigSpec sig_a
, bool is_signed
= false);
1059 RTLIL::SigSpec
And (RTLIL::IdString name
, RTLIL::SigSpec sig_a
, RTLIL::SigSpec sig_b
, bool is_signed
= false);
1060 RTLIL::SigSpec
Or (RTLIL::IdString name
, RTLIL::SigSpec sig_a
, RTLIL::SigSpec sig_b
, bool is_signed
= false);
1061 RTLIL::SigSpec
Xor (RTLIL::IdString name
, RTLIL::SigSpec sig_a
, RTLIL::SigSpec sig_b
, bool is_signed
= false);
1062 RTLIL::SigSpec
Xnor (RTLIL::IdString name
, RTLIL::SigSpec sig_a
, RTLIL::SigSpec sig_b
, bool is_signed
= false);
1064 RTLIL::SigSpec
ReduceAnd (RTLIL::IdString name
, RTLIL::SigSpec sig_a
, bool is_signed
= false);
1065 RTLIL::SigSpec
ReduceOr (RTLIL::IdString name
, RTLIL::SigSpec sig_a
, bool is_signed
= false);
1066 RTLIL::SigSpec
ReduceXor (RTLIL::IdString name
, RTLIL::SigSpec sig_a
, bool is_signed
= false);
1067 RTLIL::SigSpec
ReduceXnor (RTLIL::IdString name
, RTLIL::SigSpec sig_a
, bool is_signed
= false);
1068 RTLIL::SigSpec
ReduceBool (RTLIL::IdString name
, RTLIL::SigSpec sig_a
, bool is_signed
= false);
1070 RTLIL::SigSpec
Shl (RTLIL::IdString name
, RTLIL::SigSpec sig_a
, RTLIL::SigSpec sig_b
, bool is_signed
= false);
1071 RTLIL::SigSpec
Shr (RTLIL::IdString name
, RTLIL::SigSpec sig_a
, RTLIL::SigSpec sig_b
, bool is_signed
= false);
1072 RTLIL::SigSpec
Sshl (RTLIL::IdString name
, RTLIL::SigSpec sig_a
, RTLIL::SigSpec sig_b
, bool is_signed
= false);
1073 RTLIL::SigSpec
Sshr (RTLIL::IdString name
, RTLIL::SigSpec sig_a
, RTLIL::SigSpec sig_b
, bool is_signed
= false);
1074 RTLIL::SigSpec
Shift (RTLIL::IdString name
, RTLIL::SigSpec sig_a
, RTLIL::SigSpec sig_b
, bool is_signed
= false);
1075 RTLIL::SigSpec
Shiftx (RTLIL::IdString name
, RTLIL::SigSpec sig_a
, RTLIL::SigSpec sig_b
, bool is_signed
= false);
1077 RTLIL::SigSpec
Lt (RTLIL::IdString name
, RTLIL::SigSpec sig_a
, RTLIL::SigSpec sig_b
, bool is_signed
= false);
1078 RTLIL::SigSpec
Le (RTLIL::IdString name
, RTLIL::SigSpec sig_a
, RTLIL::SigSpec sig_b
, bool is_signed
= false);
1079 RTLIL::SigSpec
Eq (RTLIL::IdString name
, RTLIL::SigSpec sig_a
, RTLIL::SigSpec sig_b
, bool is_signed
= false);
1080 RTLIL::SigSpec
Ne (RTLIL::IdString name
, RTLIL::SigSpec sig_a
, RTLIL::SigSpec sig_b
, bool is_signed
= false);
1081 RTLIL::SigSpec
Eqx (RTLIL::IdString name
, RTLIL::SigSpec sig_a
, RTLIL::SigSpec sig_b
, bool is_signed
= false);
1082 RTLIL::SigSpec
Nex (RTLIL::IdString name
, RTLIL::SigSpec sig_a
, RTLIL::SigSpec sig_b
, bool is_signed
= false);
1083 RTLIL::SigSpec
Ge (RTLIL::IdString name
, RTLIL::SigSpec sig_a
, RTLIL::SigSpec sig_b
, bool is_signed
= false);
1084 RTLIL::SigSpec
Gt (RTLIL::IdString name
, RTLIL::SigSpec sig_a
, RTLIL::SigSpec sig_b
, bool is_signed
= false);
1086 RTLIL::SigSpec
Add (RTLIL::IdString name
, RTLIL::SigSpec sig_a
, RTLIL::SigSpec sig_b
, bool is_signed
= false);
1087 RTLIL::SigSpec
Sub (RTLIL::IdString name
, RTLIL::SigSpec sig_a
, RTLIL::SigSpec sig_b
, bool is_signed
= false);
1088 RTLIL::SigSpec
Mul (RTLIL::IdString name
, RTLIL::SigSpec sig_a
, RTLIL::SigSpec sig_b
, bool is_signed
= false);
1089 RTLIL::SigSpec
Div (RTLIL::IdString name
, RTLIL::SigSpec sig_a
, RTLIL::SigSpec sig_b
, bool is_signed
= false);
1090 RTLIL::SigSpec
Mod (RTLIL::IdString name
, RTLIL::SigSpec sig_a
, RTLIL::SigSpec sig_b
, bool is_signed
= false);
1091 RTLIL::SigSpec
Pow (RTLIL::IdString name
, RTLIL::SigSpec sig_a
, RTLIL::SigSpec sig_b
, bool a_signed
= false, bool b_signed
= false);
1093 RTLIL::SigSpec
LogicNot (RTLIL::IdString name
, RTLIL::SigSpec sig_a
, bool is_signed
= false);
1094 RTLIL::SigSpec
LogicAnd (RTLIL::IdString name
, RTLIL::SigSpec sig_a
, RTLIL::SigSpec sig_b
, bool is_signed
= false);
1095 RTLIL::SigSpec
LogicOr (RTLIL::IdString name
, RTLIL::SigSpec sig_a
, RTLIL::SigSpec sig_b
, bool is_signed
= false);
1097 RTLIL::SigSpec
Mux (RTLIL::IdString name
, RTLIL::SigSpec sig_a
, RTLIL::SigSpec sig_b
, RTLIL::SigSpec sig_s
);
1098 RTLIL::SigSpec
Pmux (RTLIL::IdString name
, RTLIL::SigSpec sig_a
, RTLIL::SigSpec sig_b
, RTLIL::SigSpec sig_s
);
1100 RTLIL::SigBit
BufGate (RTLIL::IdString name
, RTLIL::SigBit sig_a
);
1101 RTLIL::SigBit
NotGate (RTLIL::IdString name
, RTLIL::SigBit sig_a
);
1102 RTLIL::SigBit
AndGate (RTLIL::IdString name
, RTLIL::SigBit sig_a
, RTLIL::SigBit sig_b
);
1103 RTLIL::SigBit
NandGate (RTLIL::IdString name
, RTLIL::SigBit sig_a
, RTLIL::SigBit sig_b
);
1104 RTLIL::SigBit
OrGate (RTLIL::IdString name
, RTLIL::SigBit sig_a
, RTLIL::SigBit sig_b
);
1105 RTLIL::SigBit
NorGate (RTLIL::IdString name
, RTLIL::SigBit sig_a
, RTLIL::SigBit sig_b
);
1106 RTLIL::SigBit
XorGate (RTLIL::IdString name
, RTLIL::SigBit sig_a
, RTLIL::SigBit sig_b
);
1107 RTLIL::SigBit
XnorGate (RTLIL::IdString name
, RTLIL::SigBit sig_a
, RTLIL::SigBit sig_b
);
1108 RTLIL::SigBit
MuxGate (RTLIL::IdString name
, RTLIL::SigBit sig_a
, RTLIL::SigBit sig_b
, RTLIL::SigBit sig_s
);
1109 RTLIL::SigBit
Aoi3Gate (RTLIL::IdString name
, RTLIL::SigBit sig_a
, RTLIL::SigBit sig_b
, RTLIL::SigBit sig_c
);
1110 RTLIL::SigBit
Oai3Gate (RTLIL::IdString name
, RTLIL::SigBit sig_a
, RTLIL::SigBit sig_b
, RTLIL::SigBit sig_c
);
1111 RTLIL::SigBit
Aoi4Gate (RTLIL::IdString name
, RTLIL::SigBit sig_a
, RTLIL::SigBit sig_b
, RTLIL::SigBit sig_c
, RTLIL::SigBit sig_d
);
1112 RTLIL::SigBit
Oai4Gate (RTLIL::IdString name
, RTLIL::SigBit sig_a
, RTLIL::SigBit sig_b
, RTLIL::SigBit sig_c
, RTLIL::SigBit sig_d
);
1114 RTLIL::SigSpec
Anyconst (RTLIL::IdString name
, int width
= 1);
1115 RTLIL::SigSpec
Anyseq (RTLIL::IdString name
, int width
= 1);
1116 RTLIL::SigSpec
Initstate (RTLIL::IdString name
);
1119 struct RTLIL::Wire
: public RTLIL::AttrObject
1121 unsigned int hashidx_
;
1122 unsigned int hash() const { return hashidx_
; }
1125 // use module->addWire() and module->remove() to create or destroy wires
1126 friend struct RTLIL::Module
;
1131 // do not simply copy wires
1132 Wire(RTLIL::Wire
&other
) = delete;
1133 void operator=(RTLIL::Wire
&other
) = delete;
1135 RTLIL::Module
*module
;
1136 RTLIL::IdString name
;
1137 int width
, start_offset
, port_id
;
1138 bool port_input
, port_output
, upto
;
1141 struct RTLIL::Memory
: public RTLIL::AttrObject
1143 unsigned int hashidx_
;
1144 unsigned int hash() const { return hashidx_
; }
1148 RTLIL::IdString name
;
1149 int width
, start_offset
, size
;
1152 struct RTLIL::Cell
: public RTLIL::AttrObject
1154 unsigned int hashidx_
;
1155 unsigned int hash() const { return hashidx_
; }
1158 // use module->addCell() and module->remove() to create or destroy cells
1159 friend struct RTLIL::Module
;
1163 // do not simply copy cells
1164 Cell(RTLIL::Cell
&other
) = delete;
1165 void operator=(RTLIL::Cell
&other
) = delete;
1167 RTLIL::Module
*module
;
1168 RTLIL::IdString name
;
1169 RTLIL::IdString type
;
1170 dict
<RTLIL::IdString
, RTLIL::SigSpec
> connections_
;
1171 dict
<RTLIL::IdString
, RTLIL::Const
> parameters
;
1173 // access cell ports
1174 bool hasPort(RTLIL::IdString portname
) const;
1175 void unsetPort(RTLIL::IdString portname
);
1176 void setPort(RTLIL::IdString portname
, RTLIL::SigSpec signal
);
1177 const RTLIL::SigSpec
&getPort(RTLIL::IdString portname
) const;
1178 const dict
<RTLIL::IdString
, RTLIL::SigSpec
> &connections() const;
1180 // information about cell ports
1182 bool input(RTLIL::IdString portname
) const;
1183 bool output(RTLIL::IdString portname
) const;
1185 // access cell parameters
1186 bool hasParam(RTLIL::IdString paramname
) const;
1187 void unsetParam(RTLIL::IdString paramname
);
1188 void setParam(RTLIL::IdString paramname
, RTLIL::Const value
);
1189 const RTLIL::Const
&getParam(RTLIL::IdString paramname
) const;
1193 void fixup_parameters(bool set_a_signed
= false, bool set_b_signed
= false);
1195 bool has_keep_attr() const {
1196 return get_bool_attribute("\\keep") || (module
&& module
->design
&& module
->design
->module(type
) &&
1197 module
->design
->module(type
)->get_bool_attribute("\\keep"));
1200 template<typename T
> void rewrite_sigspecs(T functor
);
1203 struct RTLIL::CaseRule
1205 std::vector
<RTLIL::SigSpec
> compare
;
1206 std::vector
<RTLIL::SigSig
> actions
;
1207 std::vector
<RTLIL::SwitchRule
*> switches
;
1212 template<typename T
> void rewrite_sigspecs(T functor
);
1213 RTLIL::CaseRule
*clone() const;
1216 struct RTLIL::SwitchRule
: public RTLIL::AttrObject
1218 RTLIL::SigSpec signal
;
1219 std::vector
<RTLIL::CaseRule
*> cases
;
1223 template<typename T
> void rewrite_sigspecs(T functor
);
1224 RTLIL::SwitchRule
*clone() const;
1227 struct RTLIL::SyncRule
1229 RTLIL::SyncType type
;
1230 RTLIL::SigSpec signal
;
1231 std::vector
<RTLIL::SigSig
> actions
;
1233 template<typename T
> void rewrite_sigspecs(T functor
);
1234 RTLIL::SyncRule
*clone() const;
1237 struct RTLIL::Process
: public RTLIL::AttrObject
1239 RTLIL::IdString name
;
1240 RTLIL::CaseRule root_case
;
1241 std::vector
<RTLIL::SyncRule
*> syncs
;
1245 template<typename T
> void rewrite_sigspecs(T functor
);
1246 RTLIL::Process
*clone() const;
1250 inline RTLIL::SigBit::SigBit() : wire(NULL
), data(RTLIL::State::S0
) { }
1251 inline RTLIL::SigBit::SigBit(RTLIL::State bit
) : wire(NULL
), data(bit
) { }
1252 inline RTLIL::SigBit::SigBit(bool bit
) : wire(NULL
), data(bit
? RTLIL::S1
: RTLIL::S0
) { }
1253 inline RTLIL::SigBit::SigBit(RTLIL::Wire
*wire
) : wire(wire
), offset(0) { log_assert(wire
&& wire
->width
== 1); }
1254 inline RTLIL::SigBit::SigBit(RTLIL::Wire
*wire
, int offset
) : wire(wire
), offset(offset
) { log_assert(wire
!= nullptr); }
1255 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]; }
1256 inline RTLIL::SigBit::SigBit(const RTLIL::SigChunk
&chunk
, int index
) : wire(chunk
.wire
) { if (wire
) offset
= chunk
.offset
+ index
; else data
= chunk
.data
[index
]; }
1258 inline bool RTLIL::SigBit::operator<(const RTLIL::SigBit
&other
) const {
1259 if (wire
== other
.wire
)
1260 return wire
? (offset
< other
.offset
) : (data
< other
.data
);
1261 if (wire
!= nullptr && other
.wire
!= nullptr)
1262 return wire
->name
< other
.wire
->name
;
1263 return wire
< other
.wire
;
1266 inline bool RTLIL::SigBit::operator==(const RTLIL::SigBit
&other
) const {
1267 return (wire
== other
.wire
) && (wire
? (offset
== other
.offset
) : (data
== other
.data
));
1270 inline bool RTLIL::SigBit::operator!=(const RTLIL::SigBit
&other
) const {
1271 return (wire
!= other
.wire
) || (wire
? (offset
!= other
.offset
) : (data
!= other
.data
));
1274 inline unsigned int RTLIL::SigBit::hash() const {
1276 return mkhash_add(wire
->name
.hash(), offset
);
1280 inline RTLIL::SigBit
&RTLIL::SigSpecIterator::operator*() const {
1281 return (*sig_p
)[index
];
1284 inline const RTLIL::SigBit
&RTLIL::SigSpecConstIterator::operator*() const {
1285 return (*sig_p
)[index
];
1288 inline RTLIL::SigBit::SigBit(const RTLIL::SigSpec
&sig
) {
1289 log_assert(sig
.size() == 1 && sig
.chunks().size() == 1);
1290 *this = SigBit(sig
.chunks().front());
1293 template<typename T
>
1294 void RTLIL::Module::rewrite_sigspecs(T functor
)
1296 for (auto &it
: cells_
)
1297 it
.second
->rewrite_sigspecs(functor
);
1298 for (auto &it
: processes
)
1299 it
.second
->rewrite_sigspecs(functor
);
1300 for (auto &it
: connections_
) {
1306 template<typename T
>
1307 void RTLIL::Cell::rewrite_sigspecs(T functor
) {
1308 for (auto &it
: connections_
)
1312 template<typename T
>
1313 void RTLIL::CaseRule::rewrite_sigspecs(T functor
) {
1314 for (auto &it
: compare
)
1316 for (auto &it
: actions
) {
1320 for (auto it
: switches
)
1321 it
->rewrite_sigspecs(functor
);
1324 template<typename T
>
1325 void RTLIL::SwitchRule::rewrite_sigspecs(T functor
)
1328 for (auto it
: cases
)
1329 it
->rewrite_sigspecs(functor
);
1332 template<typename T
>
1333 void RTLIL::SyncRule::rewrite_sigspecs(T functor
)
1336 for (auto &it
: actions
) {
1342 template<typename T
>
1343 void RTLIL::Process::rewrite_sigspecs(T functor
)
1345 root_case
.rewrite_sigspecs(functor
);
1346 for (auto it
: syncs
)
1347 it
->rewrite_sigspecs(functor
);