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.
23 #include "kernel/yosys.h"
29 struct bitDef_t
: public std::pair
<RTLIL::Wire
*, int> {
30 bitDef_t() : std::pair
<RTLIL::Wire
*, int>(NULL
, 0) { }
31 bitDef_t(const RTLIL::SigBit
&bit
) : std::pair
<RTLIL::Wire
*, int>(bit
.wire
, bit
.offset
) { }
32 unsigned int hash() const { return first
->name
.hash() + second
; }
42 void add(RTLIL::SigSpec sig
)
49 void add(const SigPool
&other
)
51 for (auto &bit
: other
.bits
)
55 void del(RTLIL::SigSpec sig
)
62 void del(const SigPool
&other
)
64 for (auto &bit
: other
.bits
)
68 void expand(RTLIL::SigSpec from
, RTLIL::SigSpec to
)
70 log_assert(GetSize(from
) == GetSize(to
));
71 for (int i
= 0; i
< GetSize(from
); i
++) {
72 bitDef_t
bit_from(from
[i
]), bit_to(to
[i
]);
73 if (bit_from
.first
!= NULL
&& bit_to
.first
!= NULL
&& bits
.count(bit_from
) > 0)
78 RTLIL::SigSpec
extract(RTLIL::SigSpec sig
)
80 RTLIL::SigSpec result
;
82 if (bit
.wire
!= NULL
&& bits
.count(bit
))
83 result
.append_bit(bit
);
87 RTLIL::SigSpec
remove(RTLIL::SigSpec sig
)
89 RTLIL::SigSpec result
;
91 if (bit
.wire
!= NULL
&& bits
.count(bit
) == 0)
96 bool check(RTLIL::SigBit bit
)
98 return bit
.wire
!= NULL
&& bits
.count(bit
);
101 bool check_any(RTLIL::SigSpec sig
)
103 for (auto &bit
: sig
)
104 if (bit
.wire
!= NULL
&& bits
.count(bit
))
109 bool check_all(RTLIL::SigSpec sig
)
111 for (auto &bit
: sig
)
112 if (bit
.wire
!= NULL
&& bits
.count(bit
) == 0)
117 RTLIL::SigSpec
export_one()
119 for (auto &bit
: bits
)
120 return RTLIL::SigSpec(bit
.first
, bit
.second
);
121 return RTLIL::SigSpec();
124 RTLIL::SigSpec
export_all()
126 pool
<RTLIL::SigBit
> sig
;
127 for (auto &bit
: bits
)
128 sig
.insert(RTLIL::SigBit(bit
.first
, bit
.second
));
138 template <typename T
, class Compare
= std::less
<T
>>
141 struct bitDef_t
: public std::pair
<RTLIL::Wire
*, int> {
142 bitDef_t() : std::pair
<RTLIL::Wire
*, int>(NULL
, 0) { }
143 bitDef_t(const RTLIL::SigBit
&bit
) : std::pair
<RTLIL::Wire
*, int>(bit
.wire
, bit
.offset
) { }
144 unsigned int hash() const { return first
->name
.hash() + second
; }
147 dict
<bitDef_t
, std::set
<T
, Compare
>> bits
;
154 void insert(RTLIL::SigSpec sig
, T data
)
156 for (auto &bit
: sig
)
157 if (bit
.wire
!= NULL
)
158 bits
[bit
].insert(data
);
161 void insert(RTLIL::SigSpec sig
, const std::set
<T
> &data
)
163 for (auto &bit
: sig
)
164 if (bit
.wire
!= NULL
)
165 bits
[bit
].insert(data
.begin(), data
.end());
168 void erase(RTLIL::SigSpec sig
)
170 for (auto &bit
: sig
)
171 if (bit
.wire
!= NULL
)
175 void erase(RTLIL::SigSpec sig
, T data
)
177 for (auto &bit
: sig
)
178 if (bit
.wire
!= NULL
)
179 bits
[bit
].erase(data
);
182 void erase(RTLIL::SigSpec sig
, const std::set
<T
> &data
)
184 for (auto &bit
: sig
)
185 if (bit
.wire
!= NULL
)
186 bits
[bit
].erase(data
.begin(), data
.end());
189 void find(RTLIL::SigSpec sig
, std::set
<T
> &result
)
191 for (auto &bit
: sig
)
192 if (bit
.wire
!= NULL
) {
193 auto &data
= bits
[bit
];
194 result
.insert(data
.begin(), data
.end());
198 std::set
<T
> find(RTLIL::SigSpec sig
)
205 bool has(RTLIL::SigSpec sig
)
207 for (auto &bit
: sig
)
208 if (bit
.wire
!= NULL
&& bits
.count(bit
))
216 struct bitDef_t
: public std::pair
<RTLIL::Wire
*, int> {
217 bitDef_t() : std::pair
<RTLIL::Wire
*, int>(NULL
, 0) { }
218 bitDef_t(const RTLIL::SigBit
&bit
) : std::pair
<RTLIL::Wire
*, int>(bit
.wire
, bit
.offset
) { }
219 unsigned int hash() const { return first
->name
.hash() + second
; }
222 struct shared_bit_data_t
{
223 RTLIL::SigBit map_to
;
224 std::set
<bitDef_t
> bits
;
227 dict
<bitDef_t
, shared_bit_data_t
*> bits
;
229 SigMap(RTLIL::Module
*module
= NULL
)
235 SigMap(const SigMap
&other
)
240 const SigMap
&operator=(const SigMap
&other
)
246 void copy(const SigMap
&other
)
249 for (auto &bit
: other
.bits
) {
250 bits
[bit
.first
] = new shared_bit_data_t
;
251 bits
[bit
.first
]->map_to
= bit
.second
->map_to
;
252 bits
[bit
.first
]->bits
= bit
.second
->bits
;
256 void swap(SigMap
&other
)
258 bits
.swap(other
.bits
);
268 std::set
<shared_bit_data_t
*> all_bd_ptr
;
269 for (auto &it
: bits
)
270 all_bd_ptr
.insert(it
.second
);
271 for (auto bd_ptr
: all_bd_ptr
)
276 void set(RTLIL::Module
*module
)
279 for (auto &it
: module
->connections())
280 add(it
.first
, it
.second
);
283 // internal helper function
284 void register_bit(const RTLIL::SigBit
&bit
)
286 if (bit
.wire
&& bits
.count(bit
) == 0) {
287 shared_bit_data_t
*bd
= new shared_bit_data_t
;
289 bd
->bits
.insert(bit
);
294 // internal helper function
295 void unregister_bit(const RTLIL::SigBit
&bit
)
297 if (bit
.wire
&& bits
.count(bit
) > 0) {
298 shared_bit_data_t
*bd
= bits
[bit
];
300 if (bd
->bits
.size() == 0)
306 // internal helper function
307 void merge_bit(const RTLIL::SigBit
&bit1
, const RTLIL::SigBit
&bit2
)
309 log_assert(bit1
.wire
!= NULL
&& bit2
.wire
!= NULL
);
311 shared_bit_data_t
*bd1
= bits
[bit1
];
312 shared_bit_data_t
*bd2
= bits
[bit2
];
313 log_assert(bd1
!= NULL
&& bd2
!= NULL
);
318 if (bd1
->bits
.size() < bd2
->bits
.size())
320 for (auto &bit
: bd1
->bits
)
322 bd2
->bits
.insert(bd1
->bits
.begin(), bd1
->bits
.end());
327 bd1
->map_to
= bd2
->map_to
;
328 for (auto &bit
: bd2
->bits
)
330 bd1
->bits
.insert(bd2
->bits
.begin(), bd2
->bits
.end());
335 // internal helper function
336 void set_bit(const RTLIL::SigBit
&bit1
, const RTLIL::SigBit
&bit2
)
338 log_assert(bit1
.wire
!= NULL
);
339 log_assert(bits
.count(bit1
) > 0);
340 bits
[bit1
]->map_to
= bit2
;
343 // internal helper function
344 void map_bit(RTLIL::SigBit
&bit
) const
346 if (bit
.wire
&& bits
.count(bit
) > 0)
347 bit
= bits
.at(bit
)->map_to
;
350 void add(RTLIL::SigSpec from
, RTLIL::SigSpec to
)
352 log_assert(GetSize(from
) == GetSize(to
));
354 for (int i
= 0; i
< GetSize(from
); i
++)
356 RTLIL::SigBit
&bf
= from
[i
];
357 RTLIL::SigBit
&bt
= to
[i
];
372 void add(RTLIL::SigSpec sig
)
374 for (auto &bit
: sig
) {
380 void del(RTLIL::SigSpec sig
)
382 for (auto &bit
: sig
)
386 void apply(RTLIL::SigBit
&bit
) const
391 void apply(RTLIL::SigSpec
&sig
) const
393 for (auto &bit
: sig
)
397 RTLIL::SigBit
operator()(RTLIL::SigBit bit
) const
403 RTLIL::SigSpec
operator()(RTLIL::SigSpec sig
) const
409 RTLIL::SigSpec
operator()(RTLIL::Wire
*wire
) const
411 RTLIL::SigSpec
sig(wire
);
419 #endif /* SIGTOOLS_H */