-
Notifications
You must be signed in to change notification settings - Fork 0
/
tracecheck.c
3838 lines (3068 loc) · 102 KB
/
tracecheck.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
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*------------------------------------------------------------------------*/
/* Copyright (c) 2005 - 2015 Armin Biere, Johannes Kepler University. */
/* Copyright (c) 2015 - 2016 Matthias Schlaipfer, TU Wien. */
/*------------------------------------------------------------------------*/
/*-----------------------------------------------------------------------*\
* Documentation and terminology used in the implementation can be found in
* the seperate documentation file 'README.tracecheck'.
*
* Armin Biere, JKU Linz, 2005.
\*-----------------------------------------------------------------------*/
/*-----------------------------------------------------------------------*\
* TODO:
*
* 1. allow multiple occurrences of the same literal and the same
* antecedent by making the error that currently is reported into a
* warning
*
* 2. interleave collection of literals and resolution checking to be able
* to delete collected literals early
*------------------------------------------------------------------------*/
#include "config.h"
/*------------------------------------------------------------------------*/
#include <stdio.h>
#include <assert.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <limits.h>
#include <stdarg.h>
#include <time.h>
/*------------------------------------------------------------------------*/
#include "bftime.h"
#include "bfio.h"
#include "bfmem.h"
/*------------------------------------------------------------------------*/
#include "simpaig.h"
#include "aiger.h"
/*------------------------------------------------------------------------*/
#define VPFX "c " /* verbose output prefix string */
#define EPFX "*** tracecheck: " /* error message prefix string */
/*------------------------------------------------------------------------*/
#define DONE INT_MIN /* scanner done token */
#define ERROR INT_MAX /* scanner error token */
/*------------------------------------------------------------------------*/
/* The scanner returns integer tokens, which either denote an error,
* represented by 'ERROR', the end of the token stream, represented by
* 'DONE' or a legal integer. Thus the maximal index, for both clauses and
* literals is given by the remaining range of integers. In essence, we
* only waste one legal index, e.g. 2^32-1 on a 32-bit machine.
*/
#define MAX_IDX (INT_MAX - 1)
/*------------------------------------------------------------------------*/
/* Lisp-like list operations. See also the constructor 'cons' below.
*/
#define car(l) ((l)->head)
#define cdr(l) ((l)->tail)
/*------------------------------------------------------------------------*/
/* Macro for declaring an integer stack. The corresponding operations on
* the stack are defined by the macro 'DefineIntStackFunctions'.
*/
#define DeclareIntStack(name) \
static int * name; \
static int size_ ## name; \
static int count_ ## name
/*------------------------------------------------------------------------*/
/* Macro for declaring an aig component storage. The corresponding operations on
* the stack are defined by the macro 'DefineAIGStoreFunctions'.
*/
#define DeclareAIGComponentStorage(name) \
static simpaig ** name; \
static int count_ ## name
/*------------------------------------------------------------------------*/
typedef struct Clause Clause;
typedef struct Cell Cell; /* list node */
typedef struct Literal Literal; /* signed */
/*------------------------------------------------------------------------*/
enum Boole {
FALSE = -1,
UNKNOWN = 0,
TRUE = 1,
};
/*------------------------------------------------------------------------*/
enum Format {
ASCII_FORMAT,
BINARY_FORMAT,
COMPRESSED_FORMAT
};
typedef enum Format Format;
/*------------------------------------------------------------------------*/
struct Clause {
Clause *next_in_order; /* links relevant clauses */
Clause *dfs_tree_parent; /* graph parent in DFS tree */
int *labels; /* zero terminated list */
int *literals; /* zero terminated list */
int *antecedents; /* zero terminated list */
int idx; /* "<0"=original, ">0"=derived */
int newidx; /* "<0"=original, ">0"=derived */
int mark; /* graph traversal */
simpaig *itp; /* partial interpolant */
unsigned lineno: 28; /* where the definition started */
unsigned partition : 3;
unsigned split: 1;
#ifndef NDEBUG
unsigned resolved:1; /* 1 iff already resolved */
#endif
};
/*------------------------------------------------------------------------*/
/* List node for occurrence lists of literals.
*/
struct Cell {
void *head;
Cell *tail;
};
/*------------------------------------------------------------------------*/
// Note: bitfields provide no memory savings
// sizeof(Literal) = 16 (64-bit) best case
// Data structure alignment to multiples of 16 bytes according to Wiki:
// "x86 CPUs do require the data to be 128-bit (16-byte) aligned"
// A pointer uses 8 bytes, so we need to use a second chunk of 8 bytes in any
// case. Whether that is through padding or data doesn't make a difference, so
// we use mark (4) + label (2) + partition (2) = 8 bytes.
struct Literal {
Cell *clauses; /* occurrence lists in chain */
// local and reset
int mark;
short label;
simpaig *aig;
unsigned idx;
// global for whole proof
short partition;
};
/*------------------------------------------------------------------------*/
/**** START OF STATIC VARIABLES *******************************************/
/*------------------------------------------------------------------------*/
static int static_variables_are_dirty;
/*------------------------------------------------------------------------*/
/* literals[-max_lit_idx,max_lit_idx]
*/
static Literal *literals;
static int max_lit_idx;
static int first_defined_lit_idx;
/*------------------------------------------------------------------------*/
static Cell *free_cells; /* list of recycled cells */
static Cell *cells;
static int size_cells;
/*------------------------------------------------------------------------*/
#ifndef NDEBUG
static int debug;
#endif
/*------------------------------------------------------------------------*/
static FILE *input;
static const char *input_name;
static int close_input;
static int current_lineno;
static int last_token_lineno; /* fix for more specific diagnosis */
/*------------------------------------------------------------------------*/
static Format format;
/*------------------------------------------------------------------------*/
static BooleforceInputBuffer buffer;
static int previous_char; /* implements ungetc */
/*------------------------------------------------------------------------*/
static FILE *output;
static int close_output;
static int verbose; /* verbose level */
/*------------------------------------------------------------------------*/
static FILE * bintrace;
static FILE * ebintrace;
static FILE * restrace;
static FILE * rpttrace;
static FILE * etrace;
static FILE * stats_file;
static FILE * interpolant;
static FILE * dotgraph;
/*------------------------------------------------------------------------*/
static char * original_cnf_file_name;
static int original_variables;
static int original_clauses;
/*------------------------------------------------------------------------*/
/* If this flag is set, for instance through the command line option
* '--linear', then the linearization of the antecedents in the individual
* chains is skipped. It is assumed that the antecedents of all chains in
* the trace can already be resolved in the given order with regular input
* resolution (trivial resolution in [1]). Since linearization is currently
* the most expensive phase, enabling this flag can speed up the checker
* quite a bit. Experimentally we observerd speedups of 4.
*/
static int assume_already_linearized;
/*------------------------------------------------------------------------*/
static int lax;
/*------------------------------------------------------------------------*/
/* head of list (Clause.next_in_order)
*/
static Clause *first_in_order;
/* clauses[1,size_clauses[
*/
static Clause **clauses;
static int size_clauses;
static int min_derived_idx;
static int max_original_idx;
static int num_original_clauses;
static int num_original_literals;
static int num_derived_clauses;
static int num_derived_literals;
/*------------------------------------------------------------------------*/
/* Interpolation
*/
static int itp_system_strength;
static int partition_split;
static int empty_cls_idx; // get final itp at this index
static simpaigmgr *mgr;
static simpaig *final_itp;
static simpaig **aigs;
static aiger *output_aig;
enum Label {
UNDEF = -1,
BOT = 0,
A = 1,
B = 2,
AB = 3,
};
#ifndef NDEBUG
static simpaig *partition_a;
static simpaig *partition_b;
#endif
#ifndef NDEBUG
/* Stuff for checking invariant of interpolant */
#define SANITY_AIG "/tmp/sanity.aig"
#define SANITY_CNF "/tmp/sanity.cnf"
static char* aigtocnf_path;
static char* aigtocnf_call;
static char* picosat_path;
static char* picosat_call;
#endif
DeclareAIGComponentStorage (circuit_components_m);
DeclareAIGComponentStorage (circuit_components_itp);
/*------------------------------------------------------------------------*/
static char *outbuffer;
static unsigned size_outbuffer;
/*------------------------------------------------------------------------*/
DeclareIntStack (resolvent);
DeclareIntStack (stack);
DeclareIntStack (roots);
DeclareIntStack (trail);
DeclareIntStack (labels);
/*------------------------------------------------------------------------*/
static int conflict_occurred;
/*------------------------------------------------------------------------*/
/* statistics
*/
static int num_resolutions; /* number resolution steps */
static int num_antecedents;
static int max_antecedents;
static int num_empty_clauses;
static int init_max_cls_idx;
static int num_split_clauses;
static int num_derived_clauses_before_split;
static int num_antecedents_before_split;
static int num_derived_clauses_after_split;
static int num_antecedents_after;
static int max_cls_idx; // helper to determine correctly the # of split clauses
static int num_splits;
static int num_merge_literals;
static int num_merge_literals_after;
static int gates;
static int ands;
/*------------------------------------------------------------------------*/
static double entered_time;
/*------------------------------------------------------------------------*/
/***** END OF STATIC VARIABLES ********************************************/
/*------------------------------------------------------------------------*/
#define DefineIntStackFunctionsNoPop(name) \
static void \
enlarge_ ## name (void) \
{ \
int new_size_ ## name = size_ ## name ? 2 * size_ ## name : 1; \
BOOLEFORCE_ENLARGE_ARRAY (name, size_ ## name, new_size_ ## name); \
size_ ## name = new_size_ ## name; \
} \
\
inline static void \
push_ ## name (int n) \
{ \
if (size_ ## name == count_ ## name) \
enlarge_ ## name (); \
\
name[count_ ## name++] = n; \
} \
\
static void \
release_ ## name (void) \
{ \
BOOLEFORCE_DELETE_ARRAY (name, size_ ## name); \
name = 0; \
size_ ## name = 0; \
count_ ## name = 0; \
}
#define DefineIntStackFunctions(name) \
\
DefineIntStackFunctionsNoPop(name) \
\
inline static int \
pop_ ## name (void) \
{ \
int res; \
assert (count_ ## name > 0); \
res = name[--count_ ## name]; \
return res; \
}
/*------------------------------------------------------------------------*/
/* *INDENT-OFF* */
DefineIntStackFunctions(stack)
DefineIntStackFunctions(resolvent)
DefineIntStackFunctionsNoPop(roots)
DefineIntStackFunctions(trail);
DefineIntStackFunctionsNoPop(labels);
/* *INDENT-ON* */
/*------------------------------------------------------------------------*/
/*------------------------------------------------------------------------*/
/* Build circuit like a tree, so that it has less levels (maybe better for AIG
* reductions)
*/
#define DefineAIGStoreFunctions(name) \
static simpaig* \
build_circuit_rec_ ## name (int level, \
simpaig* (*aig_connective) (simpaigmgr*, simpaig*, simpaig*), \
simpaig* base) \
{ \
simpaig *c1, *c2; \
if (level == 1) \
{ \
if (count_circuit_components_ ## name > 0) \
return circuit_components_ ## name[--count_circuit_components_ ## name]; \
else \
return base; \
} \
c1 = build_circuit_rec_ ## name (level >> 1, aig_connective, base); \
c2 = build_circuit_rec_ ## name (level >> 1, aig_connective, base); \
return aig_connective (mgr, c1, c2); \
}
/*------------------------------------------------------------------------*/
/* *INDENT-OFF* */
DefineAIGStoreFunctions(m);
DefineAIGStoreFunctions(itp);
/* *INDENT-ON* */
/*------------------------------------------------------------------------*/
static void
LOG (const char *fmt, ...)
{
va_list ap;
fputs (VPFX, output);
va_start (ap, fmt);
vfprintf (output, fmt, ap);
va_end (ap);
fputc ('\n', output);
}
/*------------------------------------------------------------------------*/
/* Lisp's 'cons' operator for generated a linked list node.
*/
inline static Cell *
cons (Cell * tail, void *head)
{
Cell *res;
assert (free_cells);
res = free_cells;
free_cells = cdr (res);
res->head = head;
res->tail = tail;
return res;
}
/*------------------------------------------------------------------------*/
#define CONS(tail,head) \
do { (tail) = cons ((tail), (head)); } while (0)
/*------------------------------------------------------------------------*/
/* Recycle all cells reachable from 'root'.
*/
static void
recycle_cells (Cell * root)
{
Cell *p, *tail;
assert (root);
for (p = root; (tail = cdr (p)); p = tail)
;
p->tail = free_cells;
free_cells = root;
}
/*------------------------------------------------------------------------*/
#define RECYCLE_CELLS(root) \
do { \
if (!(root)) \
break; \
recycle_cells (root); \
(root) = 0; \
} while(0)
/*------------------------------------------------------------------------*/
static void
recycle_cell (Cell * cell)
{
cell->tail = free_cells;
free_cells = cell;
}
/*------------------------------------------------------------------------*/
#if 0
inline static int
length_gt_than (Cell * root, int n)
{
Cell *p;
int i;
assert (n >= 0);
i = 0;
for (p = root; p; p = cdr (p))
if (++i > n)
return 1;
return 0;
}
#endif
/*------------------------------------------------------------------------*/
static size_t
length_ints (int *a)
{
size_t res;
int *p;
assert (a);
res = 0;
for (p = a; *p; p++)
res++;
return res;
}
/*------------------------------------------------------------------------*/
// TODO: num_derived not correct as it is used with splits as well
inline static Clause *
add_clause (int idx, int *literals, int *antecedents, int lineno)
{
int new_size, llen, alen;
Clause *res, **p;
size_t res_bytes;
assert (idx);
assert (idx > 0);
if(idx > max_cls_idx)
max_cls_idx = idx;
if (literals && !literals[0])
num_empty_clauses++;
llen = literals ? length_ints (literals) : 0;
alen = antecedents ? length_ints (antecedents) : 0;
res_bytes = sizeof (*res);
res = booleforce_new (res_bytes);
res->idx = idx;
res->lineno = lineno;
assert (!res->mark);
res->literals = literals;
res->antecedents = antecedents;
while (idx >= size_clauses) {
if (!(new_size = 2 * size_clauses))
new_size = 1;
BOOLEFORCE_ENLARGE_ARRAY (clauses, size_clauses, new_size);
size_clauses = new_size;
}
p = clauses + idx;
if (antecedents) {
num_derived_clauses++;
num_antecedents += alen;
if (literals)
num_derived_literals += llen;
if (!min_derived_idx || idx < min_derived_idx)
min_derived_idx = idx;
} else {
num_original_clauses++;
if (idx > max_original_idx)
max_original_idx = idx;
if (literals)
num_original_literals += llen;
}
assert (!*p);
*p = res;
return res;
}
/*------------------------------------------------------------------------*/
static void
delete_clause (Clause * clause)
{
assert (clause);
if (clause->literals)
booleforce_delete_ints (clause->literals);
if (clause->antecedents)
booleforce_delete_ints (clause->antecedents);
if (clause->labels)
booleforce_delete_ints (clause->labels);
booleforce_delete (clause, sizeof (*clause));
}
/*------------------------------------------------------------------------*/
static void
print_zero_terminated_array (int *a, FILE * file)
{
int *p, i;
for (p = a; (i = *p); p++)
fprintf (file, "%d ", i);
fputc ('0', file);
}
/*------------------------------------------------------------------------*/
static void
print_clause (Clause * clause, int print_literals, FILE * file)
{
assert (clause);
fprintf (file, "%d ", clause->idx);
if ((print_literals || !clause->antecedents) && clause->literals)
print_zero_terminated_array (clause->literals, file);
else
fputc ('*', file);
if (clause->antecedents) {
fputc (' ', file);
print_zero_terminated_array (clause->antecedents, file);
} else
fputs (" 0", file);
fputc ('\n', file);
}
/*------------------------------------------------------------------------*/
static void
print (FILE * file)
{
Clause *clause;
int i;
for (i = 1; i < size_clauses; i++)
if ((clause = clauses[i]))
print_clause (clause, 1, file);
}
/*------------------------------------------------------------------------*/
inline static int
next_char (void)
{
int ch;
if (previous_char != EOF) {
ch = previous_char;
previous_char = EOF;
} else
ch = next_char_from_buffer (buffer);
if (ch == '\n')
current_lineno++;
return ch;
}
/*------------------------------------------------------------------------*/
inline static void
put_back_char (int ch)
{
assert (previous_char == EOF);
previous_char = ch;
if (ch == '\n') {
assert (current_lineno > 1);
current_lineno--;
}
}
/*------------------------------------------------------------------------*/
/* Unfortunately we have three kind of error codes. In one case, my
* preferred version, the error code of a function is actually a success
* code: the function returns zero iff it was not successful. This is the
* convention used for 'parse_error' and 'check_error'. However, the OS
* view is different and the main function has to return zero iff it was
* successful. Accordingly we use this convention for 'option_error'. A
* parse error is signalled by using a specific non zero integer, that can
* not represent a legal number in the input.
*/
#define check_error !option_error
#define parse_error !scan_error
/*------------------------------------------------------------------------*/
static int
scan_error (const char *fmt, ...)
{
va_list ap;
fprintf (output, "%s:%d: ", input_name, last_token_lineno);
va_start (ap, fmt);
vfprintf (output, fmt, ap);
va_end (ap);
fputc ('\n', output);
return ERROR;
}
/*------------------------------------------------------------------------*/
static int
option_error (const char *fmt, ...)
{
va_list ap;
fputs (EPFX, output);
va_start (ap, fmt);
vfprintf (output, fmt, ap);
va_end (ap);
fputc ('\n', output);
return 1;
}
/*------------------------------------------------------------------------*/
inline static int
next_non_white_space_char_ignoring_comments (void)
{
int ch;
SKIP_WHITE_SPACE_AND_COMMENTS:
ch = next_char ();
if (isspace (ch))
goto SKIP_WHITE_SPACE_AND_COMMENTS;
if (ch == 'c') {
while ((ch = next_char ()) != '\n' && ch != EOF)
;
if (ch == '\n')
goto SKIP_WHITE_SPACE_AND_COMMENTS;
}
return ch;
}
/*------------------------------------------------------------------------*/
inline static int
next_int (void)
{
int ch, res, sign, digit;
ch = next_non_white_space_char_ignoring_comments ();
if (ch == EOF)
return DONE;
last_token_lineno = current_lineno;
sign = 1;
if (ch == '-') {
sign = -1;
ch = next_char ();
}
if (!isdigit (ch)) {
if (isprint (ch))
return scan_error ("expected digit or '-' but got '%c'", ch);
else
return scan_error ("expected digit or '-' but got 0x%x", ch);
}
res = ch - '0';
ch = next_char ();
while (isdigit (ch)) {
if (res > (MAX_IDX / 10))
NUMBER_TOO_LARGE:
return scan_error ("number too large");
res *= 10;
digit = ch - '0';
if (res > MAX_IDX - digit)
goto NUMBER_TOO_LARGE;
res += digit;
ch = next_char ();
}
if (ch != EOF && !isspace (ch))
return scan_error ("expected EOF or white space after number");
res *= sign;
return res;
}
/*------------------------------------------------------------------------*/
inline static int
is_valid_clause_idx (int idx)
{
if (idx <= 0)
return 0;
return idx < size_clauses;
}
/*------------------------------------------------------------------------*/
inline static Clause *
idx2clause (int idx)
{
if (!is_valid_clause_idx (idx))
return 0;
return clauses[idx];
}
/*------------------------------------------------------------------------*/
static int
parse_zero_terminated_list_of_numbers (int only_positive_indices)
{
int n;
assert (!count_stack);
NEXT_NUMBER:
n = next_int ();
if (n == DONE)
return parse_error ("no zero before EOF");
if (n == ERROR)
return 0;
if (!n)
return 1;
if (only_positive_indices && n < 0)
return parse_error ("expected positive number");
push_stack (n);
goto NEXT_NUMBER;
}
/*------------------------------------------------------------------------*/
static int *
copy_ints (int * start, int count)
{
int *res, i, tmp;
BOOLEFORCE_NEW_ARRAY (res, count + 1);
for (i = 0; i < count; i++) {
tmp = start[i];
assert (tmp);
res[i] = tmp;
}
res[count] = 0;
return res;
}
/*------------------------------------------------------------------------*/
static int *
copy_stack (void)
{
int * res;
res = copy_ints (stack, count_stack);
count_stack = 0;
return res;
}
/*------------------------------------------------------------------------*/
static int *
copy_labels (void)
{
int * res;
res = copy_ints (labels, count_labels);
count_labels = 0;
return res;
}
/*------------------------------------------------------------------------*/
static int
parse_ascii (void)
{
int ch, idx, idx_lineno, *literals, *antecedents, *p, lit;
Clause *other;
NEXT_CLAUSE:
idx = next_int ();
if (idx == ERROR)
return 0;
if (idx == DONE)
return 1;
if (idx < 0)
return parse_error ("negative clause index");
if (!idx)
return parse_error ("zero clause index");
if ((other = idx2clause (idx)))
return parse_error ("clause %d already defined at line %d",
idx, other->lineno);
idx_lineno = last_token_lineno;
ch = next_non_white_space_char_ignoring_comments ();
if (ch != '*') {
put_back_char (ch);
if (!parse_zero_terminated_list_of_numbers (0))
return 0;
literals = copy_stack ();
} else
literals = 0;
if (!parse_zero_terminated_list_of_numbers (1)) {
BOOLEFORCE_DELETE_ARRAY (literals, length_ints (literals) + 1);
return 0;
}
// Note: delete parsed literals if there are antecedents
// We have to enter collect_literals for labelling, so we only support '*'
// literal lists for internal vertices
// TODO Maybe allow labelling through input file, then this could be changed
if (count_stack) {
if (literals)
BOOLEFORCE_DELETE_ARRAY (literals, length_ints (literals) + 1);
literals = 0;
antecedents = copy_stack ();
} else
antecedents = 0;
if (!literals && !antecedents)
return parse_error ("original clause without literals");
if (original_cnf_file_name) {
if (antecedents && idx <= original_clauses)
return parse_error ("derived clause index %d too small", idx);
if (!antecedents && idx > original_clauses)
return parse_error ("original clause index %d too large", idx);
if (literals) {
for (p = literals; (lit = *p); p++)
if (abs (lit) > original_variables)
return parse_error ("literal %d too large", lit);
}
}
(void) add_clause (idx, literals, antecedents, idx_lineno);
goto NEXT_CLAUSE;
}
/*------------------------------------------------------------------------*/
static const char *
format2str (Format f)
{
switch (f) {
case ASCII_FORMAT:
return "ascii";
case BINARY_FORMAT: