f92a87dbbe7a4465b258f803e27f81406612d73c
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 void find(RTLIL::SigSpec sig
, pool
<T
> &result
)
200 for (auto &bit
: sig
)
201 if (bit
.wire
!= NULL
) {
202 auto &data
= bits
[bit
];
203 result
.insert(data
.begin(), data
.end());
207 std::set
<T
> find(RTLIL::SigSpec sig
)
214 bool has(RTLIL::SigSpec sig
)
216 for (auto &bit
: sig
)
217 if (bit
.wire
!= NULL
&& bits
.count(bit
))
225 struct bitDef_t
: public std::pair
<RTLIL::Wire
*, int> {
226 bitDef_t() : std::pair
<RTLIL::Wire
*, int>(NULL
, 0) { }
227 bitDef_t(const RTLIL::SigBit
&bit
) : std::pair
<RTLIL::Wire
*, int>(bit
.wire
, bit
.offset
) { }
228 unsigned int hash() const { return first
->name
.hash() + second
; }
231 struct shared_bit_data_t
{
232 RTLIL::SigBit map_to
;
233 std::set
<bitDef_t
> bits
;
236 dict
<bitDef_t
, shared_bit_data_t
*> bits
;
238 SigMap(RTLIL::Module
*module
= NULL
)
244 SigMap(const SigMap
&other
)
249 const SigMap
&operator=(const SigMap
&other
)
255 void copy(const SigMap
&other
)
258 for (auto &bit
: other
.bits
) {
259 bits
[bit
.first
] = new shared_bit_data_t
;
260 bits
[bit
.first
]->map_to
= bit
.second
->map_to
;
261 bits
[bit
.first
]->bits
= bit
.second
->bits
;
265 void swap(SigMap
&other
)
267 bits
.swap(other
.bits
);
277 std::set
<shared_bit_data_t
*> all_bd_ptr
;
278 for (auto &it
: bits
)
279 all_bd_ptr
.insert(it
.second
);
280 for (auto bd_ptr
: all_bd_ptr
)
285 void set(RTLIL::Module
*module
)
288 for (auto &it
: module
->connections())
289 add(it
.first
, it
.second
);
292 // internal helper function
293 void register_bit(const RTLIL::SigBit
&bit
)
295 if (bit
.wire
&& bits
.count(bit
) == 0) {
296 shared_bit_data_t
*bd
= new shared_bit_data_t
;
298 bd
->bits
.insert(bit
);
303 // internal helper function
304 void unregister_bit(const RTLIL::SigBit
&bit
)
306 if (bit
.wire
&& bits
.count(bit
) > 0) {
307 shared_bit_data_t
*bd
= bits
[bit
];
309 if (bd
->bits
.size() == 0)
315 // internal helper function
316 void merge_bit(const RTLIL::SigBit
&bit1
, const RTLIL::SigBit
&bit2
)
318 log_assert(bit1
.wire
!= NULL
&& bit2
.wire
!= NULL
);
320 shared_bit_data_t
*bd1
= bits
[bit1
];
321 shared_bit_data_t
*bd2
= bits
[bit2
];
322 log_assert(bd1
!= NULL
&& bd2
!= NULL
);
327 if (bd1
->bits
.size() < bd2
->bits
.size())
329 for (auto &bit
: bd1
->bits
)
331 bd2
->bits
.insert(bd1
->bits
.begin(), bd1
->bits
.end());
336 bd1
->map_to
= bd2
->map_to
;
337 for (auto &bit
: bd2
->bits
)
339 bd1
->bits
.insert(bd2
->bits
.begin(), bd2
->bits
.end());
344 // internal helper function
345 void set_bit(const RTLIL::SigBit
&bit1
, const RTLIL::SigBit
&bit2
)
347 log_assert(bit1
.wire
!= NULL
);
348 log_assert(bits
.count(bit1
) > 0);
349 bits
[bit1
]->map_to
= bit2
;
352 // internal helper function
353 void map_bit(RTLIL::SigBit
&bit
) const
355 if (bit
.wire
&& bits
.count(bit
) > 0)
356 bit
= bits
.at(bit
)->map_to
;
359 void add(RTLIL::SigSpec from
, RTLIL::SigSpec to
)
361 log_assert(GetSize(from
) == GetSize(to
));
363 for (int i
= 0; i
< GetSize(from
); i
++)
365 RTLIL::SigBit
&bf
= from
[i
];
366 RTLIL::SigBit
&bt
= to
[i
];
381 void add(RTLIL::SigSpec sig
)
383 for (auto &bit
: sig
) {
389 void del(RTLIL::SigSpec sig
)
391 for (auto &bit
: sig
)
395 void apply(RTLIL::SigBit
&bit
) const
400 void apply(RTLIL::SigSpec
&sig
) const
402 for (auto &bit
: sig
)
406 RTLIL::SigBit
operator()(RTLIL::SigBit bit
) const
412 RTLIL::SigSpec
operator()(RTLIL::SigSpec sig
) const
418 RTLIL::SigSpec
operator()(RTLIL::Wire
*wire
) const
420 RTLIL::SigSpec
sig(wire
);
428 #endif /* SIGTOOLS_H */