-
Notifications
You must be signed in to change notification settings - Fork 0
/
blif.cc
261 lines (222 loc) · 8.05 KB
/
blif.cc
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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
#include "blif.h"
#include <cassert>
#include <algorithm>
#include <iostream>
#include <iterator>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
#include "error.h"
#include "tokenizer.h"
#include "truthtable.h"
namespace blifverifier {
using std::istream;
using std::istringstream;
using std::ostream;
using std::string;
using std::vector;
using std::unordered_set;
BLIF::BLIF(istream&& input)
: BLIF(input) {}
BLIF::BLIF(istream& input)
: mNextLiteralIndex(0) {
if (!input) {
throw BadInputStreamError();
}
bool read_model = false;
bool read_inputs = false;
bool read_outputs = false;
// Need this to handle validation -- outputs are a special case but we don't
// know it when reading their truth table. Once we're done reading the file
// we can discard this, however.
unordered_set<int> outputs;
Tokenizer::LineTokenReader reader(input);
auto tokens = reader.readLine();
while (reader.isGood() && tokens[0] != TOKENS::END) {
auto tok = tokens.begin();
auto section = *tok++;
// Allow at most one model name.
if (section == TOKENS::MODEL) {
if (read_model) {
throw DuplicateBlockError(reader.getRawLineNumber(), "model");
}
read_model = true;
// De-tokenize the model name (since they can have spaces)
while (tok != tokens.end()) {
mModel += *tok++ + " ";
}
}
// Allow at most one inputs section.
else if (section == TOKENS::INPUTS) {
if (read_inputs) {
throw DuplicateBlockError(reader.getRawLineNumber(), "inputs");
}
read_inputs = true;
while (tok != tokens.end()) {
mPrimaryInputs.push_back(*tok);
mTruthTables[registerLiteral(*tok)] = TruthTable();
++tok;
}
}
// Allow at most one outputs section.
else if (section == TOKENS::OUTPUTS) {
if (read_outputs) {
throw DuplicateBlockError(reader.getRawLineNumber(), "outputs");
}
read_outputs = true;
while (tok != tokens.end()) {
mPrimaryOutputs.push_back(*tok);
outputs.insert(registerLiteral(*tok));
++tok;
}
}
// All names sections must be unique, and all outputs need one.
else if (section == TOKENS::NAMES) {
// Verify we are ready for these and the uniqueness of the name.
if (!read_inputs) {
throw NamesBlockBeforeHeadersError(reader.getRawLineNumber(), "inputs");
} else if (!read_outputs) {
throw NamesBlockBeforeHeadersError(reader.getRawLineNumber(),
"outputs");
} else if (!read_model) {
throw NamesBlockBeforeHeadersError(reader.getRawLineNumber(), "model");
}
int name = registerLiteral(*(tokens.end() - 1));
if (mTruthTables.find(name) != mTruthTables.end()) {
throw DuplicateTruthTableError(reader.getRawLineNumber(),
*(tokens.end() - 1));
}
auto kind = (outputs.find(name) != outputs.end() ?
TruthTable::TTKind::OUTPUT :
TruthTable::TTKind::NORMAL);
vector<int> literals;
while (tok != tokens.end() - 1) {
literals.push_back(registerLiteral(*tok++));
}
mTruthTables[name] = TruthTable(reader, std::move(literals), kind);
// TODO: would be nice to verify the consistency of the input cover, or
// provide an option to if it's algorithmically expensive.
// Last remaining valid token.
} else {
if (section != TOKENS::END) {
throw UnrecognizedSectionError(reader.getRawLineNumber(),
section.c_str());
}
}
tokens = reader.readLine();
}
// Verify the BLIF's validity for, eg, undefined names.
// Ensure all dependencies of this truth table are in the map.
for (const auto& tt : mTruthTables) {
auto inputs = tt.second;
for (const auto& input : tt.second.getInputs()) {
if (mTruthTables.find(input) == mTruthTables.end()) {
throw MissingLogicDependencyError(mLiteralsReverse[tt.first].c_str(),
mLiteralsReverse[input].c_str());
}
}
}
// Ensure all primary outputs are defined.
for (const auto& po : mPrimaryOutputs) {
if (mTruthTables.find(registerLiteral(po)) == mTruthTables.end()) {
throw UndefinedPrimaryOutputError(po.c_str());
}
}
}
bool BLIF::triviallyNotEquivalent(const BLIF& other, ostream& warn) const {
if (other.mPrimaryInputs.size() != mPrimaryInputs.size()) {
warn << "WARNING: Circuits not equivalent; different number of inputs.\n";
return true;
}
if (other.mPrimaryInputs != mPrimaryInputs) {
warn << "WARNING: Circuits not equivalent; input names do not match.\n";
return true;
}
if (other.mPrimaryOutputs.size() != mPrimaryOutputs.size()) {
warn << "WARNING: Circuits not equivalent; different number of outputs.\n";
return true;
}
if (other.mPrimaryOutputs != mPrimaryOutputs) {
warn << "WARNING: Circuits not equivalent; output names do not match.\n";
return true;
}
return false;
}
const std::vector<std::string>& BLIF::getPrimaryInputs() const {
return mPrimaryInputs;
}
const std::vector<std::string>& BLIF::getPrimaryOutputs() const {
return mPrimaryOutputs;
}
int BLIF::registerLiteral(const string& lit) {
if (mLiterals.find(lit) == mLiterals.end()) {
mLiterals[lit] = mNextLiteralIndex;
mLiteralsReverse[mNextLiteralIndex] = lit;
++mNextLiteralIndex;
}
return mLiterals[lit];
}
void BLIF::writeEvaluator(std::ostream& output, const string& fxn_name) const {
output << "void " << fxn_name << "(size_t inputs[numInputs],"
<< " size_t outputs[numOutputs]) {\n";
// Generate node names for each gate
std::unordered_map<int, string> nicknames;
for (const auto& gate : mLiteralsReverse) {
nicknames[gate.first] = "node" + std::to_string(gate.first); // keys only
}
// Special case primary inputs and outputs
for (decltype(mPrimaryInputs)::size_type i = 0; i < mPrimaryInputs.size(); ++i) {
assert(mLiterals.find(mPrimaryInputs[i]) != mLiterals.end());
int key = mLiterals.find(mPrimaryInputs[i])->second;
nicknames[key] = "inputs[" + std::to_string(i) + "]";
}
for (decltype(mPrimaryOutputs)::size_type i = 0; i < mPrimaryOutputs.size(); ++i) {
assert(mLiterals.find(mPrimaryOutputs[i]) != mLiterals.end());
int key = mLiterals.find(mPrimaryOutputs[i])->second;
nicknames[key] = "outputs[" + std::to_string(i) + "]";
}
// We need to write out the assignments in topologically sorted order to
// ensure that all dependencies are met. BLIF does not require the circuit
// be so sorted.
std::unordered_set<int> ordered;
for (const auto& gate : mTruthTables) {
if (ordered.find(gate.first) == ordered.end()) {
std::stack<int> todo;
std::unordered_set<int> visited;
todo.push(gate.first);
while (!todo.empty()) {
int top_name = todo.top();
todo.pop();
if (ordered.find(top_name) == ordered.end()) {
// Mark this node as visit started to catch cycles
visited.insert(top_name);
bool ready = true;
const TruthTable& top = mTruthTables.find(top_name)->second;
for (const auto& dependency : top.getInputs()) {
if (ordered.find(dependency) == ordered.end()) {
if (ready) {
todo.push(top_name);
ready = false;
}
// If we attempt to visit a node we've already visited, and
// it has not been ordered, then there is a cycle.
if (visited.find(dependency) == visited.end()) {
todo.push(dependency);
} else {
throw CircularDependencyError(mLiteralsReverse.find(top_name)->second,
mLiteralsReverse.find(dependency)->second);
}
}
}
if (ready) {
ordered.insert(top_name);
top.generateCode(top_name, output, nicknames);
}
}
}
}
}
output << "}\n";
}
}