yosys/kernel/sigtools.h

348 lines
7.3 KiB
C
Raw Normal View History

2013-01-05 04:13:26 -06:00
/*
* yosys -- Yosys Open SYnthesis Suite
*
* Copyright (C) 2012 Claire Xenia Wolf <claire@yosyshq.com>
2015-07-02 04:14:30 -05:00
*
2013-01-05 04:13:26 -06:00
* Permission to use, copy, modify, and/or distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
2015-07-02 04:14:30 -05:00
*
2013-01-05 04:13:26 -06:00
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*
*/
#ifndef SIGTOOLS_H
#define SIGTOOLS_H
#include "kernel/yosys.h"
YOSYS_NAMESPACE_BEGIN
2013-01-05 04:13:26 -06:00
struct SigPool
{
struct bitDef_t : public std::pair<RTLIL::Wire*, int> {
bitDef_t() : std::pair<RTLIL::Wire*, int>(NULL, 0) { }
bitDef_t(const RTLIL::SigBit &bit) : std::pair<RTLIL::Wire*, int>(bit.wire, bit.offset) { }
unsigned int hash() const { return first->name.hash() + second; }
};
pool<bitDef_t> bits;
2013-01-05 04:13:26 -06:00
void clear()
{
bits.clear();
}
void add(const RTLIL::SigSpec &sig)
2013-01-05 04:13:26 -06:00
{
for (auto &bit : sig)
if (bit.wire != NULL)
bits.insert(bit);
2013-01-05 04:13:26 -06:00
}
void add(const SigPool &other)
{
for (auto &bit : other.bits)
bits.insert(bit);
}
void del(const RTLIL::SigSpec &sig)
2013-01-05 04:13:26 -06:00
{
for (auto &bit : sig)
if (bit.wire != NULL)
bits.erase(bit);
2013-01-05 04:13:26 -06:00
}
void del(const SigPool &other)
{
for (auto &bit : other.bits)
2013-08-06 08:04:24 -05:00
bits.erase(bit);
2013-01-05 04:13:26 -06:00
}
void expand(const RTLIL::SigSpec &from, const RTLIL::SigSpec &to)
2013-01-05 04:13:26 -06:00
{
log_assert(GetSize(from) == GetSize(to));
for (int i = 0; i < GetSize(from); i++) {
bitDef_t bit_from(from[i]), bit_to(to[i]);
if (bit_from.first != NULL && bit_to.first != NULL && bits.count(bit_from) > 0)
2013-01-05 04:13:26 -06:00
bits.insert(bit_to);
}
}
RTLIL::SigSpec extract(const RTLIL::SigSpec &sig) const
2013-01-05 04:13:26 -06:00
{
RTLIL::SigSpec result;
for (auto &bit : sig)
if (bit.wire != NULL && bits.count(bit))
result.append(bit);
2013-01-05 04:13:26 -06:00
return result;
}
RTLIL::SigSpec remove(const RTLIL::SigSpec &sig) const
2013-01-05 04:13:26 -06:00
{
RTLIL::SigSpec result;
for (auto &bit : sig)
if (bit.wire != NULL && bits.count(bit) == 0)
result.append(bit);
2013-01-05 04:13:26 -06:00
return result;
}
bool check(const RTLIL::SigBit &bit) const
2014-07-27 08:38:02 -05:00
{
return bit.wire != NULL && bits.count(bit);
}
bool check_any(const RTLIL::SigSpec &sig) const
2013-01-05 04:13:26 -06:00
{
for (auto &bit : sig)
if (bit.wire != NULL && bits.count(bit))
2013-01-05 04:13:26 -06:00
return true;
return false;
}
bool check_all(const RTLIL::SigSpec &sig) const
2013-01-05 04:13:26 -06:00
{
for (auto &bit : sig)
if (bit.wire != NULL && bits.count(bit) == 0)
2013-01-05 04:13:26 -06:00
return false;
return true;
}
RTLIL::SigSpec export_one() const
{
for (auto &bit : bits)
return RTLIL::SigSpec(bit.first, bit.second);
return RTLIL::SigSpec();
}
RTLIL::SigSpec export_all() const
{
pool<RTLIL::SigBit> sig;
for (auto &bit : bits)
sig.insert(RTLIL::SigBit(bit.first, bit.second));
return sig;
}
2015-01-31 06:58:04 -06:00
size_t size() const
{
return bits.size();
}
2013-01-05 04:13:26 -06:00
};
2019-09-13 18:30:44 -05:00
template <typename T, class Compare = void>
2013-01-05 04:13:26 -06:00
struct SigSet
{
2019-09-13 13:13:57 -05:00
static_assert(!std::is_same<Compare,void>::value, "Default value for `Compare' class not found for SigSet<T>. Please specify.");
struct bitDef_t : public std::pair<RTLIL::Wire*, int> {
bitDef_t() : std::pair<RTLIL::Wire*, int>(NULL, 0) { }
bitDef_t(const RTLIL::SigBit &bit) : std::pair<RTLIL::Wire*, int>(bit.wire, bit.offset) { }
unsigned int hash() const { return first->name.hash() + second; }
};
dict<bitDef_t, std::set<T, Compare>> bits;
2013-01-05 04:13:26 -06:00
void clear()
{
bits.clear();
}
void insert(const RTLIL::SigSpec &sig, T data)
2013-01-05 04:13:26 -06:00
{
for (const auto &bit : sig)
if (bit.wire != NULL)
bits[bit].insert(data);
2013-01-05 04:13:26 -06:00
}
void insert(const RTLIL::SigSpec& sig, const std::set<T> &data)
{
for (const auto &bit : sig)
if (bit.wire != NULL)
bits[bit].insert(data.begin(), data.end());
}
void erase(const RTLIL::SigSpec& sig)
2013-01-05 04:13:26 -06:00
{
for (const auto &bit : sig)
if (bit.wire != NULL)
bits[bit].clear();
2013-01-05 04:13:26 -06:00
}
void erase(const RTLIL::SigSpec &sig, T data)
2013-01-05 04:13:26 -06:00
{
for (const auto &bit : sig)
if (bit.wire != NULL)
bits[bit].erase(data);
2013-01-05 04:13:26 -06:00
}
void erase(const RTLIL::SigSpec &sig, const std::set<T> &data)
{
for (const auto &bit : sig)
if (bit.wire != NULL)
bits[bit].erase(data.begin(), data.end());
}
void find(const RTLIL::SigSpec &sig, std::set<T> &result)
2014-12-28 21:06:52 -06:00
{
for (const auto &bit : sig)
2014-12-28 21:06:52 -06:00
if (bit.wire != NULL) {
auto &data = bits[bit];
result.insert(data.begin(), data.end());
}
}
void find(const RTLIL::SigSpec &sig, pool<T> &result)
2013-01-05 04:13:26 -06:00
{
for (const auto &bit : sig)
if (bit.wire != NULL) {
auto &data = bits[bit];
result.insert(data.begin(), data.end());
}
2013-01-05 04:13:26 -06:00
}
std::set<T> find(const RTLIL::SigSpec &sig)
2013-01-05 04:13:26 -06:00
{
std::set<T> result;
find(sig, result);
return result;
}
bool has(const RTLIL::SigSpec &sig)
{
for (auto &bit : sig)
if (bit.wire != NULL && bits.count(bit))
return true;
return false;
}
2013-01-05 04:13:26 -06:00
};
2019-09-13 13:13:57 -05:00
template<typename T>
class SigSet<T, typename std::enable_if<!std::is_pointer<T>::value>::type> : public SigSet<T, std::less<T>> {};
template<typename T>
using sort_by_name_id_guard = typename std::enable_if<std::is_same<T,RTLIL::Cell*>::value>::type;
template<typename T>
class SigSet<T, sort_by_name_id_guard<T>> : public SigSet<T, RTLIL::sort_by_name_id<typename std::remove_pointer<T>::type>> {};
2015-10-27 09:04:47 -05:00
struct SigMap
{
mfp<SigBit> database;
SigMap(RTLIL::Module *module = NULL)
{
if (module != NULL)
set(module);
}
void swap(SigMap &other)
{
database.swap(other.database);
}
void clear()
{
database.clear();
}
void set(RTLIL::Module *module)
{
2016-02-01 03:10:20 -06:00
int bitcount = 0;
for (auto &it : module->connections())
bitcount += it.first.size();
database.clear();
database.reserve(bitcount);
2015-10-27 09:04:47 -05:00
for (auto &it : module->connections())
add(it.first, it.second);
}
void add(const RTLIL::SigSpec& from, const RTLIL::SigSpec& to)
2015-10-27 09:04:47 -05:00
{
log_assert(GetSize(from) == GetSize(to));
for (int i = 0; i < GetSize(from); i++)
{
2015-10-28 05:21:55 -05:00
int bfi = database.lookup(from[i]);
int bti = database.lookup(to[i]);
const RTLIL::SigBit &bf = database[bfi];
const RTLIL::SigBit &bt = database[bti];
2015-10-27 09:04:47 -05:00
2015-10-27 18:39:53 -05:00
if (bf.wire || bt.wire)
{
2015-10-28 05:21:55 -05:00
database.imerge(bfi, bti);
2015-10-27 18:39:53 -05:00
if (bf.wire == nullptr)
2015-10-28 05:21:55 -05:00
database.ipromote(bfi);
2015-10-27 18:39:53 -05:00
if (bt.wire == nullptr)
2015-10-28 05:21:55 -05:00
database.ipromote(bti);
2015-10-27 18:39:53 -05:00
}
2015-10-27 09:04:47 -05:00
}
}
void add(const RTLIL::SigBit &bit)
2015-10-27 09:04:47 -05:00
{
const auto &b = database.find(bit);
if (b.wire != nullptr)
database.promote(bit);
2015-10-27 09:04:47 -05:00
}
void add(const RTLIL::SigSpec &sig)
{
for (const auto &bit : sig)
add(bit);
}
inline void add(Wire *wire) { return add(RTLIL::SigSpec(wire)); }
2015-10-27 09:04:47 -05:00
void apply(RTLIL::SigBit &bit) const
{
bit = database.find(bit);
}
void apply(RTLIL::SigSpec &sig) const
{
for (auto &bit : sig)
apply(bit);
}
RTLIL::SigBit operator()(RTLIL::SigBit bit) const
{
apply(bit);
return bit;
}
RTLIL::SigSpec operator()(RTLIL::SigSpec sig) const
{
apply(sig);
return sig;
}
RTLIL::SigSpec operator()(RTLIL::Wire *wire) const
{
SigSpec sig(wire);
apply(sig);
return sig;
}
2015-11-30 12:43:52 -06:00
RTLIL::SigSpec allbits() const
{
RTLIL::SigSpec sig;
for (const auto &bit : database)
2015-11-30 12:43:52 -06:00
if (bit.wire != nullptr)
sig.append(bit);
return sig;
}
2015-10-27 09:04:47 -05:00
};
2013-01-05 04:13:26 -06:00
YOSYS_NAMESPACE_END
2013-01-05 04:13:26 -06:00
#endif /* SIGTOOLS_H */