-
Notifications
You must be signed in to change notification settings - Fork 18
/
aigtoblif.c
411 lines (361 loc) · 9.5 KB
/
aigtoblif.c
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
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
/***************************************************************************
Copyright (c) 2006-2011, Armin Biere, Johannes Kepler University.
Copyright (c) 2006, Marc Herbstritt, University of Freiburg
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to
deal in the Software without restriction, including without limitation the
rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
sell copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
IN THE SOFTWARE.
***************************************************************************/
#include "aiger.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>
#include <ctype.h>
static FILE *file;
static aiger *mgr;
static int count;
static int verbose;
static char buffer[20];
static const char *
on (unsigned i)
{
assert (mgr && i < mgr->num_outputs);
if (mgr->outputs[i].name)
return mgr->outputs[i].name;
sprintf (buffer, "o%u", i);
return buffer;
}
static void
ps (const char *str)
{
fputs (str, file);
}
static int
isblifsymchar (int ch)
{
if (isspace (ch)) return 0;
if (ch == '.') return 0;
if (ch == '\\') return 0;
if (!isprint (ch)) return 0;
return 1;
}
static void
print_mangled (const char * name, FILE * file)
{
const char * p;
char ch;
for (p = name; (ch = *p); p++)
if (isblifsymchar (ch))
fputc (ch, file);
else
fprintf (file, "\\%0X", ch);
}
static int require_const0;
static int require_const1;
static void
pl (unsigned lit)
{
const char *name;
char ch;
int i;
if (lit == 0) {
ps ("c0"); require_const0 = 1;
} else if (lit == 1) {
ps ("c1"); require_const1 = 1;
} else if ((lit & 1))
putc ('!', file), pl (lit - 1);
else if ((name = aiger_get_symbol (mgr, lit)))
{
print_mangled (name, file);
}
else
{
if (aiger_is_input (mgr, lit))
ch = 'i';
else if (aiger_is_latch (mgr, lit))
ch = 'l';
else
{
assert (aiger_is_and (mgr, lit));
ch = 'a';
}
for (i = 0; i <= count; i++)
fputc (ch, file);
fprintf (file, "%u", lit);
}
}
static int
count_ch_prefix (const char *str, char ch)
{
const char *p;
assert (ch);
for (p = str; *p == ch; p++)
;
if (*p && !isdigit (*p))
return 0;
return p - str;
}
static void
setupcount (void)
{
const char *symbol;
unsigned i;
int tmp;
count = 0;
for (i = 1; i <= mgr->maxvar; i++)
{
symbol = aiger_get_symbol (mgr, 2 * i);
if (!symbol)
continue;
if ((tmp = count_ch_prefix (symbol, 'i')) > count)
count = tmp;
if ((tmp = count_ch_prefix (symbol, 'l')) > count)
count = tmp;
if ((tmp = count_ch_prefix (symbol, 'o')) > count)
count = tmp;
if ((tmp = count_ch_prefix (symbol, 'a')) > count)
count = tmp;
}
}
int
main (int argc, char **argv)
{
unsigned i, j, latch_helper_cnt;
const char *src, *dst, *error;
int *latch_helper = 0;
int res, strip, ag;
require_const0 = 0;
require_const1 = 0;
latch_helper_cnt = 0;
src = dst = 0;
strip = 0;
res = 0;
ag = 0;
for (i = 1; i < argc; i++)
{
if (!strcmp (argv[i], "-h"))
{
fprintf (stderr,
"usage: aigtoblif [-p <prefix>][-h][-s][src [dst]]\n");
exit (0);
}
if (!strcmp (argv[i], "-s"))
strip = 1;
else if (!strcmp (argv[i], "-v"))
verbose++;
else if (argv[i][0] == '-')
{
fprintf (stderr, "=[aigtoblif] invalid option '%s'\n", argv[i]);
exit (1);
}
else if (!src)
src = argv[i];
else if (!dst)
dst = argv[i];
else
{
fprintf (stderr, "=[aigtoblif] too many files\n");
exit (1);
}
}
mgr = aiger_init ();
if (src)
error = aiger_open_and_read_from_file (mgr, src);
else
error = aiger_read_from_file (mgr, stdin);
if (error)
{
fprintf (stderr, "=[aigtoblif] %s\n", error);
res = 1;
}
else
{
if (dst)
{
if (!(file = fopen (dst, "w")))
{
fprintf (stderr, "=[aigtoblif] failed to write to '%s'\n", dst);
exit (1);
}
}
else
file = stdout;
if (strip)
aiger_strip_symbols_and_comments (mgr);
else
setupcount ();
ps (".model "), ps (src ? src : "stdin"), ps ("\n");
fputs (".inputs ", file);
for (i = 0; i < mgr->num_inputs; i++)
{
pl (mgr->inputs[i].lit), ps (" ");
if ((i + 1) % 10 == 0 && (i < (mgr->num_inputs - 1)))
ps ("\\\n");
}
ps ("\n");
fputs (".outputs ", file);
for (i = 0; i < mgr->num_outputs; i++)
{
if (verbose > 1)
fprintf (stderr,
"=[aigtoblif] output '%d' "
"has symbol '%s' with AIGER index %d\n",
(i + 1), on (i), mgr->outputs[i].lit);
ps (on (i)), ps (" ");
if ((i + 1) % 10 == 0 && (i < (mgr->num_outputs - 1)))
ps ("\\\n");
}
ps ("\n");
/* this is a non-efficient hack for assuring that BLIF-inverters
* are only inserted once even when multiple latches have the same
* next-state-function!
* latch_helper[i] stores the i-th AIG-index for which a INV has to
* be inserted. checking duplicates is simply done by comparing
* a new potential INV with all other INV that must already be inserted!
*/
latch_helper = calloc (mgr->num_latches, sizeof (latch_helper[0]));
for (i = 0; i < mgr->num_latches; i++)
{
latch_helper[i] = 0;
if (mgr->latches[i].next == aiger_false)
{
require_const0 = 1;
}
if (mgr->latches[i].next == aiger_true)
{
require_const1 = 1;
}
/* this case normally makes no sense, but you never know ... */
if (mgr->latches[i].next == aiger_false ||
mgr->latches[i].next == aiger_true)
{
ps (".latch "),
(mgr->latches[i].next == aiger_false) ? ps ("c0") : ps ("c1"),
ps (" "), pl (mgr->latches[i].lit), ps (" 0\n");
}
/* this should be the general case! */
else
{
if (!aiger_sign (mgr->latches[i].next))
{
ps (".latch "), pl (mgr->latches[i].next), ps (" "),
pl (mgr->latches[i].lit), ps (" 0\n");
}
else
{
/* add prefix 'n' to inverted AIG nodes.
* corresponding inverters are inserted below!
*/
ps (".latch n"), pl (aiger_strip (mgr->latches[i].next)),
ps (" "), pl (mgr->latches[i].lit), ps (" 0\n");
int already_done = 0;
for (j = 0; j < latch_helper_cnt && !already_done; j++)
{
if (mgr->latches[latch_helper[j]].next ==
mgr->latches[i].next)
{
already_done = 1;
}
}
if (!already_done)
{
latch_helper[latch_helper_cnt] = i;
latch_helper_cnt++;
}
}
}
}
for (i = 0; i < mgr->num_ands; i++)
{
aiger_and *n = mgr->ands + i;
unsigned rhs0 = n->rhs0;
unsigned rhs1 = n->rhs1;
ps (".names "), pl (aiger_strip (rhs0)), ps (" "),
pl (aiger_strip (rhs1)), ps (" "), pl (n->lhs), ps ("\n");
aiger_sign (rhs0) ? ps ("0") : ps ("1");
aiger_sign (rhs1) ? ps ("0") : ps ("1");
ps (" 1\n");
}
/* for those outputs having an inverted AIG node, insert an INV,
* otherwise just a BUF
*/
for (i = 0; i < mgr->num_outputs; i++)
{
if (verbose > 1)
fprintf (stderr,
"=[aigtoblif] output '%d' "
"has symbol '%s' with AIGER index %d\n",
(i + 1), on (i), mgr->outputs[i].lit);
/* this case normally makes no sense, but you never know ... */
if (mgr->outputs[i].lit == aiger_false ||
mgr->outputs[i].lit == aiger_true)
{
ps (".names ");
((mgr->outputs[i].lit ==
aiger_false) ? ps ("c0 ") : ps ("c1 ")),
ps (on (i)), ps ("\n"), ps ("1 1\n");
(mgr->outputs[i].lit == aiger_false) ? (require_const0 =
1) : (require_const1 =
1);
}
/* this should be the general case! */
else if (aiger_sign (mgr->outputs[i].lit))
{
ps (".names ");
pl (aiger_strip (mgr->outputs[i].lit));
ps (" ");
ps (on (i));
ps ("\n");
ps ("0 1\n");
}
else
{
ps (".names "), pl (aiger_strip (mgr->outputs[i].lit)),
ps (" "), ps (on (i)), ps ("\n"), ps ("1 1\n");
}
}
/* for those latches having an inverted AIG node as next-state-function,
* insert an INV. these latches were already saved in latch_helper!
*/
for (i = 0; i < latch_helper_cnt; i++)
{
unsigned l = latch_helper[i];
if (mgr->latches[l].next != aiger_false &&
mgr->latches[l].next != aiger_true)
{
assert (aiger_sign (mgr->latches[l].next));
ps (".names "), pl (aiger_strip (mgr->latches[l].next)),
ps (" n"), pl (aiger_strip (mgr->latches[l].next)),
ps ("\n"), ps ("0 1\n");
}
}
/* insert constants when necessary */
if (require_const0)
{
ps (".names c0\n");
}
if (require_const1)
{
ps (".names c1\n"), ps ("1\n");
}
fputs (".end\n", file);
/* close file */
if (dst)
fclose (file);
}
aiger_reset (mgr);
free (latch_helper);
return res;
}