forked from cdkersey/chdl
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathnodeimpl.cpp
142 lines (110 loc) · 3.13 KB
/
nodeimpl.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
#include <unordered_map>
#include <map>
#include <set>
#include <iostream>
#include "nodeimpl.h"
#include "litimpl.h"
#include "lit.h"
#include "reset.h"
#include "cdomain.h"
using namespace std;
using namespace chdl;
vector<nodeimpl*> chdl::nodes;
typedef unordered_map<nodeid_t, set<node*> > node_dir_t;
// The node directory keeps track of every node. This is used by optimizations
// to perform transforms requiring changing, e.g. node IDs.
node_dir_t &node_dir() {
static auto nd = new unordered_map<nodeid_t, set<node*> >();
return *nd;
}
// Node dir pair
pair<nodeid_t, node*> ndp(nodeid_t i, node *p) {
return pair<nodeid_t, node*>(i, p);
}
// Node dir erase
void nde(nodeid_t i, node *p) {
node_dir()[i].erase(p);
if (node_dir()[i].empty()) node_dir().erase(i);
}
nodeid_t chdl::nodecount() { return nodes.size() - 1; }
static void clear_nodes() {
node_dir().clear();
nodes.clear();
}
CHDL_REGISTER_RESET(clear_nodes);
bool litimpl::eval(cdomain_handle_t) { return val; }
void litimpl::print(ostream &out) {
if (undef) out << " litX " << id << endl;
else out << " lit" << val << ' ' << id << endl;
}
void litimpl::print_vl(ostream &out) {
out << " assign __x" << id << " = " << val << ';' << endl;
}
node::node(): idx(nodes.size()) {
new litimpl();
check();
node_dir()[idx].insert(this);
}
node::node(nodeid_t i): idx(i) { check(); node_dir()[idx].insert(this); }
node::node(const node &r): idx(r.idx){ check(); node_dir()[idx].insert(this); }
node::~node() { nde(idx, this); }
const node &node::operator=(const node &r) const {
nodeid_t from(idx), to(r.idx);
if (from != NO_NODE && from != to) {
for (auto x : node_dir()[from]) {
node_dir()[to].insert(x);
x->idx = to;
}
node_dir().erase(from);
}
check();
return *this;
}
node &node::operator=(const node &r) {
*(const node *)this = r;
return *this;
}
void node::change_net(nodeid_t i) {
nde(idx, this);
node_dir()[i].insert(this);
idx = i;
check();
}
void chdl::permute_nodes(map<nodeid_t, nodeid_t> x) {
size_t new_nodes_sz=0;
for (auto i = x.begin(); i != x.end(); ++i)
if (i->second + 1 > new_nodes_sz) new_nodes_sz = i->second + 1;
vector<nodeimpl *> new_nodes(new_nodes_sz);
for (size_t i = 0; i < nodes.size(); ++i) {
node n(i);
if (x.find(i) != x.end()) {
// This assignment, because of the way nodes work, causes all nodes with
// index i to point to the new index.
n = node(x[i]);
new_nodes[x[i]] = nodes[i];
// Set the ID.
new_nodes[x[i]]->id = x[i];
} else {
// It's not in the mapping; the nodeimpl can be freed, and the
// corresponding node objects made to point at NO_NODE.
n = node(NO_NODE);
delete nodes[i];
}
}
nodes = new_nodes;
}
void chdl::get_dead_nodes(std::set<nodeid_t> &s) {
s.clear();
for (nodeid_t i = 0; i < nodes.size(); ++i)
if (!node_dir().count(i) || node_dir()[i].size() == 0) s.insert(i);
#if 0
unsigned long n(0);
for (auto &x : node_dir()) {
if (x.second.empty()) {
s.insert(x.first);
++n;
}
}
#endif
cout << "Found " << s.size() << " dead nodes." << endl;
}