2014-07-27 07:50:25 -05:00
|
|
|
/*
|
|
|
|
* yosys -- Yosys Open SYnthesis Suite
|
|
|
|
*
|
2021-06-07 17:39:36 -05:00
|
|
|
* Copyright (C) 2012 Claire Xenia Wolf <claire@yosyshq.com>
|
2015-07-02 04:14:30 -05:00
|
|
|
*
|
2014-07-27 07:50:25 -05: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
|
|
|
*
|
2014-07-27 07:50:25 -05: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.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2014-08-16 17:56:47 -05:00
|
|
|
// This file contains various c++ utility routines and helper classes that
|
|
|
|
// do not depend on any other components of yosys (except stuff like log_*).
|
|
|
|
|
|
|
|
#include "kernel/yosys.h"
|
|
|
|
|
|
|
|
#ifndef UTILS_H
|
|
|
|
#define UTILS_H
|
|
|
|
|
2014-09-27 09:17:53 -05:00
|
|
|
YOSYS_NAMESPACE_BEGIN
|
|
|
|
|
2014-08-16 17:56:47 -05:00
|
|
|
// ------------------------------------------------
|
|
|
|
// A map-like container, but you can save and restore the state
|
|
|
|
// ------------------------------------------------
|
|
|
|
|
2023-10-05 17:24:26 -05:00
|
|
|
template<typename Key, typename T, typename OPS = hash_ops<Key>>
|
|
|
|
struct stackmap
|
|
|
|
{
|
|
|
|
private:
|
|
|
|
std::vector<dict<Key, T*, OPS>> backup_state;
|
2014-12-28 12:24:24 -06:00
|
|
|
dict<Key, T, OPS> current_state;
|
2014-08-16 17:56:47 -05:00
|
|
|
static T empty_tuple;
|
|
|
|
|
2023-10-05 17:24:26 -05:00
|
|
|
public:
|
|
|
|
stackmap() { }
|
|
|
|
stackmap(const dict<Key, T, OPS> &other) : current_state(other) { }
|
2014-08-16 17:56:47 -05:00
|
|
|
|
2023-10-05 17:24:26 -05:00
|
|
|
template<typename Other>
|
|
|
|
void operator=(const Other &other)
|
2014-08-16 17:56:47 -05:00
|
|
|
{
|
2023-10-05 17:24:26 -05:00
|
|
|
for (auto &it : current_state)
|
2014-08-16 17:56:47 -05:00
|
|
|
if (!backup_state.empty() && backup_state.back().count(it.first) == 0)
|
|
|
|
backup_state.back()[it.first] = new T(it.second);
|
|
|
|
current_state.clear();
|
|
|
|
|
2023-10-05 17:24:26 -05:00
|
|
|
for (auto &it : other)
|
2014-08-16 17:56:47 -05:00
|
|
|
set(it.first, it.second);
|
|
|
|
}
|
|
|
|
|
2023-10-05 17:24:26 -05:00
|
|
|
bool has(const Key &k)
|
|
|
|
{
|
|
|
|
return current_state.count(k) != 0;
|
|
|
|
}
|
2023-09-20 17:49:05 -05:00
|
|
|
|
2014-08-16 17:56:47 -05:00
|
|
|
void set(const Key &k, const T &v)
|
|
|
|
{
|
|
|
|
if (!backup_state.empty() && backup_state.back().count(k) == 0)
|
|
|
|
backup_state.back()[k] = current_state.count(k) ? new T(current_state.at(k)) : nullptr;
|
|
|
|
current_state[k] = v;
|
|
|
|
}
|
|
|
|
|
|
|
|
void unset(const Key &k)
|
|
|
|
{
|
|
|
|
if (!backup_state.empty() && backup_state.back().count(k) == 0)
|
|
|
|
backup_state.back()[k] = current_state.count(k) ? new T(current_state.at(k)) : nullptr;
|
|
|
|
current_state.erase(k);
|
|
|
|
}
|
|
|
|
|
|
|
|
const T &get(const Key &k)
|
|
|
|
{
|
|
|
|
if (current_state.count(k) == 0)
|
|
|
|
return empty_tuple;
|
|
|
|
return current_state.at(k);
|
|
|
|
}
|
|
|
|
|
|
|
|
void reset(const Key &k)
|
|
|
|
{
|
2023-10-05 17:24:26 -05:00
|
|
|
for (int i = GetSize(backup_state)-1; i >= 0; i--)
|
2014-08-16 17:56:47 -05:00
|
|
|
if (backup_state[i].count(k) != 0) {
|
|
|
|
if (backup_state[i].at(k) == nullptr)
|
|
|
|
current_state.erase(k);
|
|
|
|
else
|
|
|
|
current_state[k] = *backup_state[i].at(k);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
current_state.erase(k);
|
|
|
|
}
|
|
|
|
|
2023-10-05 17:24:26 -05:00
|
|
|
const dict<Key, T, OPS> &stdmap()
|
|
|
|
{
|
|
|
|
return current_state;
|
|
|
|
}
|
2014-08-16 17:56:47 -05:00
|
|
|
|
2023-10-05 17:24:26 -05:00
|
|
|
void save()
|
|
|
|
{
|
|
|
|
backup_state.resize(backup_state.size()+1);
|
|
|
|
}
|
2014-08-16 17:56:47 -05:00
|
|
|
|
|
|
|
void restore()
|
|
|
|
{
|
|
|
|
log_assert(!backup_state.empty());
|
2023-10-05 17:24:26 -05:00
|
|
|
for (auto &it : backup_state.back())
|
2014-08-16 17:56:47 -05:00
|
|
|
if (it.second != nullptr) {
|
|
|
|
current_state[it.first] = *it.second;
|
|
|
|
delete it.second;
|
|
|
|
} else
|
|
|
|
current_state.erase(it.first);
|
|
|
|
backup_state.pop_back();
|
|
|
|
}
|
|
|
|
|
|
|
|
~stackmap()
|
|
|
|
{
|
|
|
|
while (!backup_state.empty())
|
|
|
|
restore();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2023-10-05 17:26:29 -05:00
|
|
|
|
2014-08-16 17:56:47 -05:00
|
|
|
// ------------------------------------------------
|
|
|
|
// A simple class for topological sorting
|
|
|
|
// ------------------------------------------------
|
2014-07-27 07:50:25 -05:00
|
|
|
|
2023-09-20 17:49:05 -05:00
|
|
|
template <typename T, typename C = std::less<T>, typename OPS = hash_ops<T>> class TopoSort
|
2014-07-27 07:50:25 -05:00
|
|
|
{
|
2023-09-20 17:49:05 -05:00
|
|
|
public:
|
|
|
|
// We use this ordering of the edges in the adjacency matrix for
|
|
|
|
// exact compatibility with an older implementation.
|
|
|
|
struct IndirectCmp {
|
|
|
|
IndirectCmp(const std::vector<T> &nodes) : nodes_(nodes) {}
|
|
|
|
bool operator()(int a, int b) const
|
|
|
|
{
|
|
|
|
log_assert(static_cast<size_t>(a) < nodes_.size());
|
|
|
|
log_assert(static_cast<size_t>(b) < nodes_.size());
|
|
|
|
return node_cmp_(nodes_[a], nodes_[b]);
|
|
|
|
}
|
|
|
|
const C node_cmp_;
|
|
|
|
const std::vector<T> &nodes_;
|
|
|
|
};
|
|
|
|
|
|
|
|
bool analyze_loops;
|
|
|
|
std::map<T, int, C> node_to_index;
|
|
|
|
std::vector<std::set<int, IndirectCmp>> edges;
|
2014-07-27 07:50:25 -05:00
|
|
|
std::vector<T> sorted;
|
2023-09-20 17:49:05 -05:00
|
|
|
std::set<std::set<T, C>> loops;
|
2014-07-27 07:50:25 -05:00
|
|
|
|
2023-09-20 17:49:05 -05:00
|
|
|
TopoSort() : indirect_cmp(nodes)
|
2014-07-27 07:50:25 -05:00
|
|
|
{
|
|
|
|
analyze_loops = true;
|
|
|
|
found_loops = false;
|
|
|
|
}
|
|
|
|
|
2023-09-20 17:49:05 -05:00
|
|
|
int node(T n)
|
|
|
|
{
|
|
|
|
auto it = node_to_index.find(n);
|
|
|
|
if (it == node_to_index.end()) {
|
|
|
|
int index = static_cast<size_t>(nodes.size());
|
|
|
|
node_to_index[n] = index;
|
|
|
|
nodes.push_back(n);
|
|
|
|
edges.push_back(std::set<int, IndirectCmp>(indirect_cmp));
|
|
|
|
return index;
|
|
|
|
}
|
|
|
|
return it->second;
|
|
|
|
}
|
|
|
|
|
|
|
|
void edge(int l_index, int r_index) { edges[r_index].insert(l_index); }
|
|
|
|
|
|
|
|
void edge(T left, T right) { edge(node(left), node(right)); }
|
|
|
|
|
|
|
|
bool has_edges(const T &node)
|
|
|
|
{
|
|
|
|
auto it = node_to_index.find(node);
|
|
|
|
return it == node_to_index.end() || !edges[it->second].empty();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool sort()
|
2014-07-27 07:50:25 -05:00
|
|
|
{
|
2023-09-20 17:49:05 -05:00
|
|
|
log_assert(GetSize(node_to_index) == GetSize(edges));
|
|
|
|
log_assert(GetSize(nodes) == GetSize(edges));
|
|
|
|
|
|
|
|
loops.clear();
|
|
|
|
sorted.clear();
|
|
|
|
found_loops = false;
|
|
|
|
|
|
|
|
std::vector<bool> marked_cells(edges.size(), false);
|
|
|
|
std::vector<bool> active_cells(edges.size(), false);
|
|
|
|
std::vector<int> active_stack;
|
|
|
|
|
|
|
|
marked_cells.reserve(edges.size());
|
|
|
|
sorted.reserve(edges.size());
|
|
|
|
|
|
|
|
for (const auto &it : node_to_index)
|
|
|
|
sort_worker(it.second, marked_cells, active_cells, active_stack);
|
|
|
|
|
|
|
|
log_assert(GetSize(sorted) == GetSize(nodes));
|
|
|
|
|
|
|
|
return !found_loops;
|
2014-07-27 07:50:25 -05:00
|
|
|
}
|
|
|
|
|
2023-09-20 17:49:05 -05:00
|
|
|
// Build the more expensive representation of edges for
|
|
|
|
// a few passes that use it directly.
|
|
|
|
std::map<T, std::set<T, C>, C> get_database()
|
2014-07-27 07:50:25 -05:00
|
|
|
{
|
2023-09-20 17:49:05 -05:00
|
|
|
std::map<T, std::set<T, C>, C> database;
|
|
|
|
for (size_t i = 0; i < nodes.size(); ++i) {
|
|
|
|
std::set<T, C> converted_edge_set;
|
|
|
|
for (int other_node : edges[i]) {
|
|
|
|
converted_edge_set.insert(nodes[other_node]);
|
|
|
|
}
|
|
|
|
database.emplace(nodes[i], converted_edge_set);
|
|
|
|
}
|
|
|
|
return database;
|
2014-07-27 07:50:25 -05:00
|
|
|
}
|
|
|
|
|
2023-09-20 17:49:05 -05:00
|
|
|
private:
|
|
|
|
bool found_loops;
|
|
|
|
std::vector<T> nodes;
|
|
|
|
const IndirectCmp indirect_cmp;
|
2023-09-20 18:20:08 -05:00
|
|
|
|
2023-09-20 17:49:05 -05:00
|
|
|
void sort_worker(const int root_index, std::vector<bool> &marked_cells, std::vector<bool> &active_cells, std::vector<int> &active_stack)
|
2014-07-27 07:50:25 -05:00
|
|
|
{
|
2023-09-20 17:49:05 -05:00
|
|
|
if (active_cells[root_index]) {
|
2014-09-21 05:57:33 -05:00
|
|
|
found_loops = true;
|
2014-07-27 07:50:25 -05:00
|
|
|
if (analyze_loops) {
|
2014-11-07 08:21:03 -06:00
|
|
|
std::set<T, C> loop;
|
2023-09-20 17:49:05 -05:00
|
|
|
for (int i = GetSize(active_stack) - 1; i >= 0; i--) {
|
|
|
|
const int index = active_stack[i];
|
|
|
|
loop.insert(nodes[index]);
|
|
|
|
if (index == root_index)
|
2014-07-27 07:50:25 -05:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
loops.insert(loop);
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2023-09-20 17:49:05 -05:00
|
|
|
if (marked_cells[root_index])
|
2014-07-27 07:50:25 -05:00
|
|
|
return;
|
|
|
|
|
2023-09-20 17:49:05 -05:00
|
|
|
if (!edges[root_index].empty()) {
|
2014-07-27 07:50:25 -05:00
|
|
|
if (analyze_loops)
|
2023-09-20 17:49:05 -05:00
|
|
|
active_stack.push_back(root_index);
|
|
|
|
active_cells[root_index] = true;
|
2014-07-27 07:50:25 -05:00
|
|
|
|
2023-09-20 17:49:05 -05:00
|
|
|
for (int left_n : edges[root_index])
|
2014-07-27 07:50:25 -05:00
|
|
|
sort_worker(left_n, marked_cells, active_cells, active_stack);
|
|
|
|
|
|
|
|
if (analyze_loops)
|
|
|
|
active_stack.pop_back();
|
2023-09-20 17:49:05 -05:00
|
|
|
active_cells[root_index] = false;
|
2014-07-27 07:50:25 -05:00
|
|
|
}
|
2015-07-02 04:14:30 -05:00
|
|
|
|
2023-09-20 17:49:05 -05:00
|
|
|
marked_cells[root_index] = true;
|
|
|
|
sorted.push_back(nodes[root_index]);
|
2014-07-27 07:50:25 -05:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2014-09-27 09:17:53 -05:00
|
|
|
YOSYS_NAMESPACE_END
|
|
|
|
|
2014-07-27 07:50:25 -05:00
|
|
|
#endif
|