-
Notifications
You must be signed in to change notification settings - Fork 29
/
Copy pathblimc.c
677 lines (612 loc) · 18.2 KB
/
blimc.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
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
/*-------------------------------------------------------------------------*/
/* Copyright 2010-2020 Armin Biere Johannes Kepler University Linz Austria */
/*-------------------------------------------------------------------------*/
#include "lglib.h"
#include "aiger.h"
#include <assert.h>
#include <ctype.h>
#include <limits.h>
#include <signal.h>
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct Clause { int * lits; } Clause;
static aiger * model;
static unsigned num_bad;
static aiger_symbol * badsyms;
static FILE *outfile, *msgfile, *errfile, *apitrace;
static int verbose, xstim, plain, nowitness, noclone;
static LGL * lgl, * clone;
static int * coi, opt = 3;
static int cloned;
static Clause * clauses;
static int nclauses, szclauses;
static int * lits;
static int nlits, szlits;
static int maxvar;
static int * litmap;
static int k;
static size_t maxbytes, curbytes;
static int catchedsig;
static void (*sig_int_handler)(int);
static void (*sig_segv_handler)(int);
static void (*sig_abrt_handler)(int);
static void (*sig_term_handler)(int);
static void resetsighandlers (void) {
(void) signal (SIGINT, sig_int_handler);
(void) signal (SIGSEGV, sig_segv_handler);
(void) signal (SIGABRT, sig_abrt_handler);
(void) signal (SIGTERM, sig_term_handler);
}
static void msg (int level, const char * fmt, ...) {
va_list ap;
if (verbose < level) return;
fprintf (msgfile, "c [blimc] %.2f ", lglprocesstime ());
va_start (ap, fmt);
vfprintf (msgfile, fmt, ap);
va_end (ap);
fputc ('\n', msgfile);
fflush (msgfile);
}
static void caughtsigmsg (int sig) {
if (!verbose) return;
fprintf (errfile, "c [blimc]\nc [blimc] CAUGHT SIGNAL %d\nc [blimc]\n", sig);
fflush (errfile);
}
static void stats (void) {
if (verbose) {
if (clone) lglstats (clone);
if (lgl) lglstats (lgl);
}
msg (1, "reached k = %d", k);
msg (1, "cloned %d solvers", cloned);
msg (1, "max %.1f MB", maxbytes/(double)(1<<20));
}
static void catchsig (int sig) {
if (!catchedsig) {
fputs ("s UNKNOWN\n", errfile);
fflush (errfile);
catchedsig = 1;
caughtsigmsg (sig);
if (verbose) stats (), caughtsigmsg (sig);
}
resetsighandlers ();
if (!getenv ("LGLNABORT")) raise (sig); else exit (1);
}
static void setsighandlers (void) {
sig_int_handler = signal (SIGINT, catchsig);
sig_segv_handler = signal (SIGSEGV, catchsig);
sig_abrt_handler = signal (SIGABRT, catchsig);
sig_term_handler = signal (SIGTERM, catchsig);
}
static void die (const char * fmt, ...) {
va_list ap;
fputs ("*** blimc: ", errfile);
va_start (ap, fmt);
vfprintf (errfile, fmt, ap);
va_end (ap);
fputc ('\n', errfile);
fflush (errfile);
exit (1);
}
static void * new (void * state, size_t bytes) {
char * res;
(void) state;
res = malloc (bytes);
if (!res) die ("out of memory");
curbytes += bytes;
if (curbytes > maxbytes) maxbytes = curbytes;
return res;
}
static void del (void * state, void * ptr, size_t bytes) {
(void) state;
assert (curbytes >= bytes);
curbytes -= bytes;
free (ptr);
}
static void * rsz (void * state, void * ptr, size_t o, size_t n) {
char * res;
(void) state;
assert (curbytes >= o);
curbytes -= o;
res = realloc (ptr, n);
if (!res) die ("out of memory");
curbytes += n;
if (curbytes > maxbytes) maxbytes = curbytes;
return res;
}
static int prepilit (unsigned ulit) {
int res;
assert (aiger_lit2var (ulit) <= model->maxvar);
assert (coi[aiger_lit2var (ulit)]);
assert (ulit <= 2*model->maxvar + 1);
res = (ulit>>1) + 1;
if (ulit & 1) res = -res;
return res;
}
static unsigned aiginput (unsigned idx) {
assert (idx < model->num_inputs);
return model->inputs[idx].lit;
}
static int prepinput (unsigned idx) { return prepilit (aiginput (idx)); }
static unsigned aiglatch (unsigned idx) {
assert (idx < model->num_latches);
return model->latches[idx].lit;
}
static unsigned aigreset (unsigned idx) {
assert (idx < model->num_latches);
return model->latches[idx].reset;
}
static int preplatch (unsigned idx) { return prepilit (aiglatch (idx)); }
static unsigned aignext (unsigned idx) {
assert (idx < model->num_latches);
return model->latches[idx].next;
}
static int prepnext (unsigned idx) { return prepilit (aignext (idx)); }
static unsigned aigbad (unsigned idx) {
assert (idx < num_bad);
return badsyms[idx].lit;
}
static int prepbad (unsigned idx) { return prepilit (aigbad (idx)); }
static int preplhs (unsigned idx) {
assert (idx < model->num_ands);
return prepilit (model->ands[idx].lhs);
}
static int prepleft (unsigned idx) {
assert (idx < model->num_ands);
return prepilit (model->ands[idx].rhs0);
}
static int prepright (unsigned idx) {
assert (idx < model->num_ands);
return prepilit (model->ands[idx].rhs1);
}
static void unit (int ilit) { lgladd (lgl, ilit); lgladd (lgl, 0); }
static void binary (int a, int b) {
lgladd (lgl, a);
lgladd (lgl, b);
lgladd (lgl, 0);
}
static void ternary (int a, int b, int c) {
lgladd (lgl, a);
lgladd (lgl, b);
lgladd (lgl, c);
lgladd (lgl, 0);
}
static int ulitincoi (unsigned ulit) {
unsigned idx;
assert (ulit <= 2*model->maxvar + 1);
idx = aiger_lit2var (ulit);
assert (idx <= model->maxvar);
return coi[idx];
}
static void prepfreeze (void) {
unsigned i;
msg (2, "freeze");
for (i = 0; i < model->num_inputs; i++)
if (!nowitness && ulitincoi (aiginput (i)))
lglfreeze (lgl, prepinput (i));
for (i = 0; i < model->num_latches; i++) {
if (!ulitincoi (aiglatch (i))) continue;
lglfreeze (lgl, preplatch (i));
lglfreeze (lgl, prepnext (i));
}
for (i = 0; i < num_bad; i++) {
assert (ulitincoi (badsyms[i].lit));
lglfreeze (lgl, prepbad (i));
}
if (ulitincoi (0)) lglfreeze (lgl, 1);
}
static void and (int lhs, int rhs0, int rhs1) {
binary (-lhs, rhs0);
binary (-lhs, rhs1);
ternary (-rhs0, -rhs1, lhs);
}
static void logic (void) {
unsigned i;
msg (2, "logic");
if (ulitincoi (0)) unit (-1);
for (i = 0; i < model->num_ands; i++)
if (ulitincoi (model->ands[i].lhs))
and (preplhs (i), prepleft (i), prepright (i));
}
static const char * usage =
"usage: blimc [-h][-v][-x][-n][-p][-O[0123]][--no-clone][<maxk>][<aiger>]\n"
;
static int isnumstr (const char * str) {
const char * p = str;
int ch;
if (!isdigit (*p++)) return 0;
while (isdigit ((ch = *p))) p++;
return !ch;
}
static double percent (double a, double b) {
return b ? 100.0 * (a / b) : 0.0;
}
static void coimsg (const char * name, unsigned remaining, unsigned all) {
msg (1, "%-9s in COI: %10u = %3.0f%% out of %u",
name, remaining, percent (remaining, all), all);
}
static void travcoi (void) {
unsigned next, top, size, latches, inputs, ands, constants;
unsigned lit, * stack, idx, stripped;
aiger_symbol * s;
aiger_and * a;
size = model->maxvar + 1;
stack = new (0, size * sizeof *stack);
latches = inputs = ands = constants = 0;
assert (num_bad == 1);
lit = badsyms[0].lit;
idx = aiger_lit2var (lit);
coi[idx] = top = next = 1;
for (;;) {
assert (idx <= model->maxvar);
assert (coi[idx]);
stripped = aiger_strip (lit);
if (aiger_is_input (model, stripped)) inputs++;
else if ((s = aiger_is_latch (model, stripped))) {
latches++;
lit = s->next;
idx = aiger_lit2var (lit);
assert (idx <= model->maxvar);
if (!coi[idx]) stack[top++] = lit, coi[idx] = top;
} else if ((a = aiger_is_and (model, stripped))) {
ands++;
lit = a->rhs0;
idx = aiger_lit2var (lit);
assert (idx <= model->maxvar);
if (!coi[idx]) stack[top++] = lit, coi[idx] = top;
lit = a->rhs1;
idx = aiger_lit2var (lit);
assert (idx <= model->maxvar);
if (!coi[idx]) stack[top++] = lit, coi[idx] = top;
} else assert (!stripped), constants++;
if (next == top) break;
lit = stack[next++];
}
del (0, stack, size * sizeof * stack);
coimsg ("literals", top, model->maxvar);
coimsg ("inputs", inputs, model->num_inputs);
coimsg ("latches", latches, model->num_latches);
coimsg ("constants", constants, 1);
coimsg ("ands", ands, model->num_ands);
}
static void newlgl (int setapitrace) {
const char * apitracename = getenv ("BLIMCLGLAPITRACE");
lgl = lglminit (0,new,rsz,del);
if (setapitrace && apitracename) {
assert (!apitrace);
apitrace = fopen (apitracename, "w");
if (!apitrace)
die ("can write to API trace file '%s'", apitracename);
lglwtrapi (lgl, apitrace);
}
lglsetout (lgl, msgfile);
if (verbose >= 2) lglsetopt (lgl, "verbose", verbose - 1);
if (plain) lglsetopt (lgl, "plain", 1);
}
static void extract (void * dummy, int lit) {
size_t oldbytes, newbytes;
Clause * c;
int i;
(void) dummy;
if (lit) {
if (nlits == szlits) {
oldbytes = szlits * sizeof *lits;
szlits = szlits ? 2*szlits : 1;
newbytes = szlits * sizeof *lits;
lits = rsz (0, lits, oldbytes, newbytes);
}
lits[nlits++] = lit;
} else {
if (nclauses == szclauses) {
oldbytes = szclauses * sizeof *clauses;
szclauses = szclauses ? 2*szclauses : 1;
newbytes = szclauses * sizeof *clauses;
clauses = rsz (0, clauses, oldbytes, newbytes);
}
c = clauses + nclauses++;
newbytes = (nlits + 1) * sizeof *c->lits;
c->lits = new (0, newbytes);
for (i = 0; i < nlits; i++) c->lits[i] = lits[i];
c->lits[i] = 0;
nlits = 0;
}
}
static int mapuntimedlit (int lit) {
int idx = abs (lit), res;
assert (1 <= idx && idx <= (int) model->maxvar + 1);
if (!(res = litmap[idx])) litmap[idx] = res = ++maxvar;
if (lit < 0) res = -res;
return res;
}
static void mapcnf (void) {
int * p, lit;
Clause * c;
for (c = clauses; c < clauses + nclauses; c++)
for (p = c->lits; (lit = *p); p++)
*p = mapuntimedlit (lit);
}
static int mainilit (unsigned ulit) {
int tmp = prepilit (ulit), idx = abs (tmp), res;
assert (1 <= idx && idx <= (int) model->maxvar+1);
res = litmap[idx];
if (tmp < 0) res = -res;
return res;
}
static int shift (int ilit, int time) {
int idx = abs (ilit), res;
assert (1 <= idx && idx <= maxvar);
res = idx + time * maxvar;
if (ilit < 0) res = -res;
return res;
}
static void equiv (int a, int b) { binary (-a, b); binary (a, -b); }
static void shiftcnf (int time) {
int * p, lit, prev;
unsigned i;
Clause * c;
assert (0 <= time);
if (time > 0) {
for (i = 0; i < model->num_latches; i++) {
if (!ulitincoi (aiglatch (i))) continue;
prev = shift (mainilit (aignext (i)), time-1);
lit = shift (mainilit (aiglatch (i)), time);
equiv (prev, lit);
lglmelt (lgl, prev);
}
}
for (c = clauses; c < clauses + nclauses; c++) {
for (p = c->lits; (lit = *p); p++) lgladd (lgl, shift (lit, time));
lgladd (lgl, 0);
}
for (i = 0; i < model->num_latches; i++)
if (ulitincoi (aiglatch (i)))
lglfreeze (lgl, shift (mainilit (aignext (i)), time));
}
static void bad (LGL * whichlgl, int time) {
lglassume (whichlgl, shift (mainilit (aigbad (0)), time));
}
static void init (void) {
size_t bytes;
unsigned i;
int lit;
bytes = (model->maxvar + 2) * sizeof *litmap;
litmap = new (0, bytes);
memset (litmap, 0, bytes);
assert (!maxvar);
maxvar = 0; // true lit
for (i = 0; i < model->num_latches; i++) {
if (!ulitincoi (aiglatch (i))) continue;
lit = ++maxvar;
litmap[preplatch (i)] = lit;
}
for (i = 0; i < model->num_inputs; i++) {
if (!ulitincoi (aiginput (i))) continue;
lit = ++maxvar;
litmap[prepinput (i)] = lit;
}
mapcnf ();
for (i = 0; i < model->num_latches; i++)
if (ulitincoi (aiglatch (i)))
(void) mapuntimedlit (prepnext (i));
msg (1, "mapped %d variables", maxvar);
#ifndef NDEBUG
for (i = 0; i < num_bad; i++)
if (ulitincoi (aigbad (i)))
assert (litmap[abs (prepbad (i))]);
#endif
for (i = 0; i < model->num_latches; i++) {
if (!ulitincoi (aiglatch (i))) continue;
if (aiglatch (i) == aigreset (i)) continue;
if (aiger_false == aigreset (i)) unit (-mainilit (aiglatch (i)));
if (aiger_true == aigreset (i)) unit (mainilit (aiglatch (i)));
}
}
static int length (const int * p) {
const int * q;
for (q = p; *q; q++)
;
return q - p;
}
int main (int argc, char ** argv) {
int res, i, lit, maxk, val;
unsigned j, init0, init1, initx;
const char * iname, * err;
size_t coibytes;
char prefix[80];
#ifndef NDEBUG
int fres;
#endif
LGL * tmp;
char vch;
outfile = stdout, msgfile = errfile = stderr;
iname = 0;
maxk = 0;
for (i = 1; i < argc; i++) {
if (!strcmp (argv[i], "-h")) { fputs (usage, outfile); exit (0);}
else if (!strcmp (argv[i], "-v")) verbose++;
else if (!strcmp (argv[i], "-x")) xstim = 1;
else if (!strcmp (argv[i], "-n")) nowitness = 1;
else if (!strcmp (argv[i], "-p")) plain = 1;
else if (!strcmp (argv[i], "-O")) opt = 1;
else if (!strcmp (argv[i], "-O0")) opt = 0;
else if (!strcmp (argv[i], "-O1")) opt = 1;
else if (!strcmp (argv[i], "-O2")) opt = 2;
else if (!strcmp (argv[i], "-O3")) opt = 3;
else if (!strcmp (argv[i], "--no-clone")) noclone = 1;
else if (argv[i][0] == '-')
die ("invalid command line option '%s'", argv[i]);
else if (isnumstr (argv[i])) maxk = atoi (argv[i]);
else if (iname)
die ("two files specified '%s' and '%s'", iname, argv[i]);
else iname = argv[i];
}
if (verbose)
lglbnr ("BLIMC Bounded Lingeling Model Checker",
"c [blimc] ", msgfile);
setsighandlers ();
msg (1, "reading %s", iname ? iname : "<stdin>");
model = aiger_init ();
if (iname) err = aiger_open_and_read_from_file (model, iname);
else err = aiger_read_from_file (model, stdin);
if (err) die ("parse error in '%s' at %s",
iname ? iname : "<stdin>", err);
msg (1, "MILOA %u %u %u %u %u",
model->maxvar,
model->num_inputs,
model->num_latches,
model->num_outputs,
model->num_ands);
msg (1, "BCJK %u %u %u %u %u",
model->num_bad,
model->num_constraints,
model->num_justice,
model->num_fairness);
if (!model->num_outputs && !model->num_bad)
die ("model contains no output nor bad state property");
if (model->num_bad > 1)
die ("can not handle multiple bad state properties");
if (!model->num_bad && model->num_outputs > 1)
die ("can not handle multiple outputs (without bad state property)");
if (model->num_constraints > 0)
die ("can not handle environment constraints");
if (model->num_justice)
msg (1, "ignoring %d justice properties", model->num_justice);
if (model->num_fairness)
msg (1, "ignoring %d fairness constraints", model->num_fairness);
if (model->num_bad) num_bad = model->num_bad, badsyms = model->bad;
else num_bad = model->num_outputs, badsyms = model->outputs;
assert (num_bad == 1);
init0 = init1 = initx = 0;
for (j = 0; j < model->num_latches; j++) {
unsigned rst = aigreset (j);
if (rst == aiger_false) init0++;
if (rst == aiger_true) init1++;
if (rst == aiglatch (j)) initx++;
}
if (model->num_latches) {
msg (1, "%u latches initialized to 0", init0);
msg (1, "%u latches initialized to 1", init1);
msg (1, "%u latches uninitialized", initx);
} else msg (1, "no latches, so purely combinational");
coibytes = (model->maxvar+1) * sizeof *coi;
coi = new (0, coibytes);
memset (coi, 0, coibytes);
travcoi ();
newlgl (0);
lglsetprefix (lgl, "c [lglopt] ");
logic ();
prepfreeze ();
msg (1, "encoded");
(void) lglsimp (lgl, opt);
msg (1, "simplified");
if (lglfixed (lgl, prepbad (0)) < 0) {
res = 20;
fprintf (outfile, "0\nb0\n.\n");
} else {
res = 0;
lgltravall (lgl, 0, extract);
msg (1, "extracted");
if (verbose >= 1) lglstats (lgl);
tmp = lgl, lgl = 0;
lglrelease (tmp);
newlgl (1);
lglsetopt (lgl, "flipping", 0);
lglsetopt (lgl, "boost", 0);
lglsetopt (lgl, "simpdelay", 100);
lglsetprefix (lgl, "c [lgl0] ");
init ();
msg (1, "maxk %d", maxk);
for (k = 0; k <= maxk; k++) {
msg (1, "bound %d", k);
sprintf (prefix, "c [lgl%d] ", k);
lglsetprefix (lgl, prefix);
shiftcnf (k);
bad (lgl, k);
if (!noclone) lglsetopt (lgl, "clim", 1000);
res = lglsat (lgl);
if (!res) {
assert (!clone);
assert (!noclone);
clone = lglclone (lgl);
sprintf (prefix, "c [lgl%dclone%d] ", k, cloned++);
lglsetprefix (clone, prefix);
lglfixate (clone);
lglmeltall (clone);
#if 0
if (cloned & (cloned-1)) res = 0;
else
#endif
res = lglsimp (clone, 1);
if (!res) {
lglsetopt (clone, "clim", -1);
res = lglsat (clone);
assert (res == 10 || res == 20);
}
if (verbose >= 3) lglstats (clone);
tmp = clone, clone = 0;
#ifndef NDEBUG
fres =
#endif
lglunclone (lgl, tmp);
assert (fres == res);
lglrelease (tmp);
}
assert (res);
if (res == 10) break;
assert (res == 20);
printf ("u%d\n", k);
fflush (stdout);
if (!model->num_latches) break;
if (k < maxk && !((k + 1) & k)) (void) lglsimp (lgl, 0);
}
if (res == 10) {
fprintf (outfile, "1\nb0\n");
if (!nowitness) {
for (i = 0; i < (int) model->num_latches; i++) {
if (ulitincoi (aiglatch (i))) {
lit = shift (mainilit (aiglatch (i)), 0);
val = lglderef (lgl, lit);
} else val = 0;
if (val > 0) vch = '1';
else if (!val && xstim) vch = 'x';
else vch = '0';
fputc (vch, stdout);
}
fputc ('\n', stdout);
for (i = 0; i <= k; i++) {
for (j = 0; j < model->num_inputs; j++) {
if (ulitincoi (aiginput (j))) {
lit = shift (mainilit (aiginput (j)), i);
val = lglderef (lgl, lit);
} else val = 0;
if (val > 0) vch = '1';
else if (!val && xstim) vch = 'x';
else vch = '0';
fputc (vch, stdout);
}
fputc ('\n', stdout);
}
}
printf (".\n");
} else if (res == 20 && !model->num_latches)
fprintf (outfile, "0\nb0\n.\n");
else fprintf (outfile, "2\nb0\n.\n");
del (0, litmap, (model->maxvar + 2) * sizeof *litmap);
}
fflush (outfile);
del (0, coi, (model->maxvar+1) * sizeof *coi);
aiger_reset (model);
for (i = 0; i < nclauses; i++)
del (0, clauses[i].lits,
(length (clauses[i].lits) + 1) * sizeof *clauses[i].lits);
del (0, clauses, szclauses * sizeof *clauses);
del (0, lits, szlits * sizeof *lits);
resetsighandlers ();
stats ();
lglrelease (lgl);
if (apitrace) fflush (apitrace), fclose (apitrace);
msg (1, "exit %d", res);
return res;
}