-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbitfunc_cy.pyx
1159 lines (899 loc) · 37.3 KB
/
bitfunc_cy.pyx
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
# -*- mode: python -*-
#cython: embedsignature=True, language_level=2
# Copyright 2012 Sandia Corporation. Under the terms of Contract
# DE-AC04-94AL85000, there is a non-exclusive license for use of this work by or
# on behalf of the U.S. Government. Export of this program may require a license
# from the United States Government.
cimport _bitfunc_cy as bf
from libc.stdint cimport *
from libc.stdio cimport stdout
import functools
import warnings
# @memoized
# def fibonacci(n):
# "Return the nth fibonacci number."
# if n in (0, 1):
# return n
# return fibonacci(n-1) + fibonacci(n-2)
# print fibonacci(12)
cdef class literal:
cdef bf.literal l
cdef readonly bfman _bfman
def __cinit__(self, bf.literal l, bfman b):
self.l = l
self._bfman = b
def __reduce__(self):
return (literal, (self.l,))
def __iter__(self):
yield self
cpdef bint isTrue(self):
"""Whether this is the unique TRUE literal"""
return self.l == bf.LITERAL_TRUE
cpdef bint isFalse(self):
"""Whether this is the unique FALSE literal"""
return self.l == bf.LITERAL_FALSE
def __invert__(self):
return self.__neg__()
def asInt(self): return self.as_int()
def as_int(self):
if self.isTrue():
return 1
elif self.isFalse():
return 0
else:
raise ValueError('symbolic literal cannot be coerced to int')
int = property(as_int)
def asBool(self): return self.as_bool()
def as_bool(self):
if self.isTrue():
return True
elif self.isFalse():
return False
else:
raise ValueError('symbolic literal cannot be coerced to bool')
bool = property(as_bool)
def asBitvec(self): return self.as_bitvec()
def as_bitvec(self):
return bitvec.fromLiteral(self)
bitvec = property(as_bitvec)
cpdef dimacs(self):
if not self.isSymbolic():
raise ValueError, 'concrete literals have no dimacs representation'
else:
return self.l
def __neg__(self):
return literal(-self.l, self._bfman)
def __abs__(self):
if self.l < 0:
return literal(-self.l, self._bfman)
else:
return self
def _apply_binop(self, op, other):
# Treat bools and 0 or 1 as literal
if not isinstance(other, literal):
if isinstance(other, bool) or \
(isinstance(other, int) and (other == 0 or other == 1)):
other = literal.fromBool(self._bfman, bool(other))
if isinstance(other, literal):
return op(self, other)
return NotImplemented
def __and__(self, other):
return self._apply_binop(self._bfman.newAnd, other)
def __or__(self, other):
return self._apply_binop(self._bfman.newOr, other)
def __xor__(self, other):
return self._apply_binop(self._bfman.newXor, other)
def eq(self, literal other):
"""Test that this is the same literal as the other"""
return self.l == other.l
def hash(self):
return int(self.l)
def __eq__(self, other):
return self._apply_binop(self._bfman.newEqual, other)
def __ne__(self, other):
x = self._apply_binop(self._bfman.newEqual, other)
if x == NotImplemented:
return x
return -x
def __hash__(self):
return self.l
def __str__(self):
s = str(self.l)
if self.isTrue():
s = 'TRUE'
elif self.isFalse():
s = 'FALSE'
return 'literal(' + s + ')'
def __repr__(self):
return 'literal(' + str(self.l) + ')'
@classmethod
def fromBool(cls, bfman man, b):
"""create a literal representing the truth value of the given Boolean"""
if b:
return man.LIT_TRUE
else:
return man.LIT_FALSE
def asBit(self):
"""returns a single character. 1 if the literal is true, 0 if it's false,
and ? otherwise."""
if self.isTrue():
return '1'
elif self.isFalse():
return '0'
else:
return '?'
def isSymbolic(self):
"""A literal is symbolic if it is not concrete"""
return not self.isConcrete()
def isConcrete(self):
"""self.isTrue() or self.isFalse()"""
return self.isTrue() or self.isFalse()
STATUS_MUST_BE_TRUE = bf.STATUS_MUST_BE_TRUE
STATUS_MUST_BE_FALSE = bf.STATUS_MUST_BE_FALSE
STATUS_TRUE_OR_FALSE = bf.STATUS_TRUE_OR_FALSE
STATUS_NOT_TRUE_NOR_FALSE = bf.STATUS_NOT_TRUE_NOR_FALSE
STATUS_UNKNOWN = bf.STATUS_UNKNOWN
mbool_false = 0
mbool_true = 1
mbool_unknown = bf.unknown
AIG_MODE = bf.AIG_MODE
cdef class bfresult:
cdef s
def __init__(self, s):
self.s = s
def __repr__(self):
return self.s
BF_SAT = bfresult('BF_SAT')
BF_UNSAT = bfresult('BF_UNSAT')
BF_UNKNOWN = bfresult('BF_UNKNOWN')
def wrapbfresult(bf.bfresult x):
if x == bf.BF_SAT: return BF_SAT
elif x == bf.BF_UNSAT: return BF_UNSAT
elif x == bf.BF_UNKNOWN: return BF_UNKNOWN
cdef class bitvec:
cdef bf.bitvec *v
cdef readonly bfman _bfman
def __dealloc__(self):
bf.CONSUME_BITVEC(bf.bfvRelease(self.v))
def __reduce__(self):
return (vAlloc, (self._bfman, self.v.size), list(self))
def __setstate__(self, lits):
self.v.size = len(lits)
for x in range(len(lits)):
self[x] = lits[x]
cpdef push(self, literal data):
"""Appends the literal at the MSB position of this bitvec"""
bf.bfvPush(self.v, data.l)
cpdef push_back(self, literal data):
"""Appends the literal at the MSB position of this bitvec"""
bf.bfvPush(self.v, data.l)
cpdef vPush(self, literal data):
bf.bfvPush(self.v, data.l)
def __getitem__(self, size_t index):
if not index < self.v.size:
raise IndexError, 'bitvec index out of range'
return literal(self.v.data[index], self._bfman)
def __setitem__(self, size_t index, literal value):
if not index < self.v.size:
raise IndexError, 'bitvec index out of range'
self.v.data[index] = value.l
def __getslice__(self, size_t i, size_t j):
return self._bfman.vExtract(self, i, j-i)
def __len__(self):
return self.v.size
def eq(self, bitvec other):
"""Test whether this bitvec is structurally identical to other"""
if len(self) == len(other):
for i in xrange(len(self)):
if not self[i].eq(other[i]):
return False
return True
else:
return False
def hash(self):
return hash(tuple(list(self)))
def __iter__(self):
for i in xrange(self.v.size):
yield self[i]
def __richcmp__(self, other, int op):
if isinstance(other, (int, long)):
other = self._bfman.vConstant(len(self), other)
if isinstance(other, bitvec):
if op == 2: # eq
if len(self) == len(other):
ret = self._bfman.LIT_TRUE
for i in xrange(len(self)):
ret = ret & (self[i] == other[i])
return ret
else:
return self._bfman.LIT_FALSE
elif op == 3: # ne
if len(self) != len(other):
return self._bfman.LIT_TRUE
else:
ret = self._bfman.LIT_FALSE
for i in xrange(len(self)):
ret = ret | (self[i] != other[i])
return ret
elif op == 0 and len(self) == len(other): # <
return self._bfman.vSlt(self, other)
elif op == 4 and len(self) == len(other): # >
return self._bfman.vSgt(self, other)
elif op == 1 and len(self) == len(other): # <=
return self._bfman.vSlte(self, other)
elif op == 5 and len(self) == len(other): # >=
return self._bfman.vSgte(self, other)
raise NotImplementedError
def _binop_apply(self, other, function):
if isinstance(other, bitvec):
return function(self, other)
elif isinstance(other, (int, long)):
return function(self, self._bfman.vUconstant(len(self), other))
else:
raise ValueError, "other neither bitvec nor int"
def __add__(self, other):
return self._binop_apply(other, self._bfman.vAdd0)
def __sub__(self, other):
return self._binop_apply(other, self._bfman.vSubtract)
def __mul__(self, other):
return self._binop_apply(other, self._bfman.vMul)
def __floordiv__(self, other):
return self._binop_apply(other, self._bfman.vSdiv)
def __mod__(self, other):
return self._binop_apply(other, self._bfman.vSrem)
def __lshift__(self, other):
return self._binop_apply(other, self._bfman.vLshift)
def __rshift__(self, other):
"""Arithmetic right shift (see also SHR)"""
if isinstance(other, bitvec):
return self._bfman.vRshift(self, other, self[-1])
elif isinstance(other, (int, long)):
return self._bfman.vRshift(self, self._bfman.vConstant(len(self), other), self[-1])
else:
raise ValueError, "other neither bitvec nor int"
def SHR(self, uint32_t dist):
"""Logical right shift (see also __rshift__)"""
return self._bfman.vRLshift(self, dist)
def __and__(self, other):
if isinstance(self, (int, long)): #reverse?!
self, other = other, self
return self._binop_apply(other, self._bfman.vAnd)
def __or__(self, other):
if isinstance(self, (int, long)): #reverse?!
self, other = other, self
return self._binop_apply(other, self._bfman.vOr)
def __xor__(self, other):
if isinstance(self, (int, long)): #reverse?!
self, other = other, self
return self._binop_apply(other, self._bfman.vXor)
def __neg__(self):
return self._bfman.vNegate(self)
def __abs__(self):
"""Sets the MSB to 0"""
b = self._bfman.vDup(self)
b[len(b)-1] = self._bfman.LIT_FALSE
return b
def __hex__(self):
if self.isConcrete():
return str(self)
else:
raise ValueError, 'bit vector is symbolic, cannot convert to hex'
def __invert__(self):
return self._bfman.vInvert(self)
def isConcrete(self):
return not self.isSymbolic()
def isSymbolic(self):
"""Test if any literal in the bit vector is symbolic"""
for i in xrange(0, len(self)):
if not self[i].isConcrete():
return True
return False
cpdef elseIfFalse(self, c, ifFalse):
"""Whenever c is true, evaluates to this bitvec. Otherwise, evaluates to
ifFalse"""
if isinstance(ifFalse, (int, long)):
ifTrue = self._bfman.vConstant(len(self), ifFalse)
return self._bfman.vSelect(c, self, ifFalse)
cpdef elseIfTrue(self, c, ifTrue):
"""Whenever c is false, evaluates to this bitvec. Otherwise, evaluates to
ifTrue"""
if isinstance(ifTrue, (int, long)):
ifTrue = self._bfman.vConstant(len(self), ifTrue)
return self._bfman.vSelect(c, ifTrue, self)
@classmethod
def zero(cls, bfman b, uint32_t sz):
return wrapbv(b, bf.bfvZero(sz))
@classmethod
def ones(cls, bfman b, uint32_t sz):
return wrapbv(b, bf.bfvOnes(sz))
@classmethod
def one(cls, bfman b, uint32_t sz):
return wrapbv(b, bf.bfvOne(sz))
def asInt(self): return self.as_int()
def as_int(self):
# uses the definition of 2s complement
if not self.isConcrete():
raise ValueError('symbolic bitvecs cannot be coerced to int')
m = len(self)
ret = -self[m-1].int*(2**(m-1))
for i in xrange(m-1):
ret += self[i].int*(2**i)
return ret
int = property(as_int)
def asUint(self): return self.asUint()
def as_uint(self):
if not self.isConcrete():
raise ValueError('symbolic bitvecs cannot be coerced to uint')
ret = 0
for i in xrange(len(self)):
ret = ret | (self[i].int << i)
return ret
uint = property(as_uint)
def asBin(self): return self.as_bin()
def as_bin(self):
return bin(self.uint)
bin = property(as_bin)
def asBitvec(self): return self.as_bitvec()
def as_bitvec(self):
return self
bitvec = property(as_bitvec)
def __str__(self):
"""
Printing for symbolic and concrete bit vectors.
"""
if self.isSymbolic():
return self.bitstr()
else:
return hex(self.uint)
def __repr__(self):
if self.isSymbolic():
ret = '['
lits = []
for i in xrange(0, len(self)):
lits.append(self[i])
ret += ', '.join(map(str, lits))
ret += ']'
return ret
else:
return hex(self.uint)
def bitstr(self):
return ''.join(reversed([self[i].asBit() for i in xrange(len(self))]))
@classmethod
def fromList(cls, bfman b, l):
"""Input: a list of literals or Boolean-interpretable objects.
Returns bv where bv[i] = l[i]"""
ret = vAlloc(b, len(l))
for x in l:
if not isinstance(x, literal):
x = literal.fromBool(b, x)
ret.vPush(x)
return ret
@classmethod
def fromLiteral(cls, bfman b, l):
b = vAlloc(b, 1)
b.vPush(l)
return b
@classmethod
def alloc(cls, bfman b, n):
return vAlloc(b, n)
cpdef bitvec vAlloc(bfman b, uint32_t initialCapacity):
return wrapbv(b, bf.bfvAlloc(initialCapacity))
cdef inline makestr(char *s):
if s is NULL:
return None
else:
return s
cdef inline char *fromstr(s):
if s is None:
return NULL
else:
return s
cdef class bfman:
"""The bitfunc manager.
This is the first class you'll need to use to do anything useful in
bitfunc. It manages the construction and description of constraints for a
particular instance. It also lets you solve() that instance.
Once the instance is solved, the *FromCounterExample family of methods allows
you to extract the satisfying assignment in terms of bitfunc objects."""
cdef bf.bfman *b
cdef readonly literal LIT_TRUE
cdef readonly literal LIT_FALSE
def __cinit__(self):
self.b = bf.bfInit(bf.AIG_MODE)
self.LIT_TRUE = literal(bf.LITERAL_TRUE, self)
self.LIT_FALSE = literal(bf.LITERAL_FALSE, self)
def __reduce__(self):
state = {}
state['numVars'] = self.b.numVars
state['assumps'] = wrapbv(self, bf.bfvHold(self.b.assumps))
ins = [(s.lit,makestr(s.name)) for s in self.b.aig.inputs[:self.b.aig.num_inputs]]
outs = [(s.lit,makestr(s.name)) for s in self.b.aig.outputs[:self.b.aig.num_outputs]]
latches = [(s.lit,s.next,makestr(s.name)) for s in self.b.aig.latches[:self.b.aig.num_latches]]
ands = [(a.lhs,a.rhs0,a.rhs1) for a in self.b.aig.ands[:self.b.aig.num_ands]]
state['aig'] = (ins, outs, latches, ands)
return (bfman, (), state)
def __setstate__(self, state):
bf.bfSetNumVars(self.b, state['numVars'])
ins, outs, latches, ands = state['aig']
for lit,name in ins:
bf.aiger_add_input(self.b.aig, lit, fromstr(name))
for lit,next,name in latches:
bf.aiger_add_latch(self.b.aig, lit, next, fromstr(name))
for lit,name in outs:
bf.aiger_add_output(self.b.aig, lit, fromstr(name))
for lhs,rhs0,rhs1 in ands:
bf.aiger_add_and(self.b.aig, lhs, rhs0, rhs1)
bf.bfvPushAssumption(self.b, (<bitvec>state['assumps']).v)
def __dealloc__(self):
bf.bfDestroy(self.b)
cpdef enableDebug(self, char *category, int level):
bf.bfEnableDebug(self.b, category, level)
cpdef printCNF(self, char *filename):
"""Prints the current CNF to the given filename"""
bf.bfPrintCNF(self.b, filename)
cpdef printAIG(self):
"""Prints the AIG to stdout"""
bf.bfPrintAIG(self.b, bf.stdout)
cpdef bfresult pushAssumption(self, literal p):
"""Add a literal as an assumption, in LIFO order."""
return wrapbfresult(bf.bfPushAssumption(self.b, p.l))
cpdef popAssumptions(self, unsigned num):
"""popAssumptions(num). Pops num assumptions in LIFO order."""
bf.bfPopAssumptions(self.b, num)
cpdef configureFuncsat(self): bf.bfConfigureFuncsat(self.b)
cpdef configurePicosat(self): bf.bfConfigurePicosat(self.b)
cpdef configurePicosatIncremental(self): bf.bfConfigurePicosatIncremental(self.b)
cpdef configureLingeling(self): bf.bfConfigureLingeling(self.b)
cpdef configurePlingeling(self): bf.bfConfigurePlingeling(self.b)
cpdef configurePrecosat(self): bf.bfConfigurePrecosat(self.b)
cpdef configurePicosatReduce(self): bf.bfConfigurePicosatReduce(self.b)
cpdef configureLingelingReduce(self): bf.bfConfigureLingelingReduce(self.b)
cpdef configureBFSolve(self):
"""Will run the command 'bfsolve' in the path"""
bf.bfConfigureExternal(self.b)
cpdef bfresult solve(self):
"""solve(). Assuming the assumptions are true, find a satisfying assignment
for every variable"""
return wrapbfresult(bf.bfSolve(self.b))
cpdef reset(self):
"""you shouldn't need to call this, just make a new bfman"""
bf.bfReset(self.b)
cpdef literal lFromCounterExample(self, literal l):
"""lFromCounterExample(l). Get the assignment for literal l"""
return literal(bf.bflFromCounterExample(self.b, l.l), self)
cpdef bitvec vFromCounterExample(self, bitvec b):
"""vFromCounterExample(b). Get the assignment for bitvec b"""
return wrapbv(self, bf.bfvFromCounterExample(self.b, b.v))
cpdef memory mFromCounterExample(self, memory m):
"""mFromCounterExample(m). Get the assignment for memory m"""
return wrapmem(self, bf.bfmFromCounterExample(self.b, m.m))
cpdef fromCounterExample(self, x):
"""Given a literal, bitvec, or memory, return the counterexample value from
the model"""
if isinstance(x, literal):
return self.lFromCounterExample(x)
elif isinstance(x, bitvec):
return self.vFromCounterExample(x)
elif isinstance(x, memory):
return self.mFromCounterExample(x)
raise ValueError, 'value not a literal, bitvec, or memory'
cpdef literal newVar(self):
return literal(bf.bfNewVar(self.b), self)
cpdef literal newAnd(self, literal a, literal b):
"""Returns a literal that is the logical and of a and b"""
return literal(bf.bfNewAnd(self.b, a.l, b.l), self)
cpdef literal newOr(self, literal a, literal b):
"""Returns a literal that is the logical or of a and b"""
return literal(bf.bfNewOr(self.b, a.l, b.l), self)
cpdef literal newXor(self, literal a, literal b):
"""Returns a literal that is the logical xor of a and b"""
return literal(bf.bfNewXor(self.b, a.l, b.l), self)
cpdef literal newNand(self, literal a, literal b):
"""Returns a literal that is the logical nand of a and b"""
return literal(bf.bfNewNand(self.b, a.l, b.l), self)
cpdef literal newNor(self, literal a, literal b):
"""Returns a literal that is the logical nor of a and b"""
return literal(bf.bfNewNor(self.b, a.l, b.l), self)
cpdef literal newEqual(self, literal a, literal b):
"""Returns a literal that is the logical equal of a and b"""
return literal(bf.bfNewEqual(self.b, a.l, b.l), self)
cpdef literal newImplies(self, literal a, literal b):
"""Returns a literal that is the logical implies of a and b"""
return literal(bf.bfNewImplies(self.b, a.l, b.l), self)
cpdef literal newIff(self, literal a, literal b):
"""Returns a literal that is the logical iff of a and b"""
return literal(bf.bfNewIff(self.b, a.l, b.l), self)
cpdef lIsTrue(self, literal l):
"""deprecated"""
warnings.warn('use l.isTrue()', DeprecationWarning)
return l.isTrue()
cpdef lIsFalse(self, literal l):
"""deprecated"""
warnings.warn('use l.isFalse()', DeprecationWarning)
return l.isFalse()
cpdef lAnd(self, literal a, literal b, literal o):
bf.bfAnd(self.b, a.l, b.l, o.l)
cpdef lOr(self, literal a, literal b, literal o):
bf.bfOr(self.b, a.l, b.l, o.l)
cpdef lXor(self, literal a, literal b, literal o):
bf.bfXor(self.b, a.l, b.l, o.l)
cpdef lNand(self, literal a, literal b, literal o):
bf.bfNand(self.b, a.l, b.l, o.l)
cpdef lNor(self, literal a, literal b, literal o):
bf.bfNor(self.b, a.l, b.l, o.l)
cpdef lImplies(self, literal a, literal b, literal o):
bf.bfImplies(self.b, a.l, b.l, o.l)
cpdef lEqual(self, literal a, literal b, literal o):
bf.bfEqual(self.b, a.l, b.l, o.l)
cpdef literal bigAnd(self, bitvec bv):
"""Returns a literal equal to the logical and of all the literals in bv"""
return literal(bf.bfBigAnd(self.b, bv.v), self)
cpdef literal bigOr(self, bitvec bv):
"""Returns a literal equal to the logical or of all the literals in bv"""
return literal(bf.bfBigOr(self.b, bv.v), self)
cpdef literal bigXor(self, bitvec bv):
"""Returns a literal equal to the logical or of all the literals in bv"""
return literal(bf.bfBigXor(self.b, bv.v), self)
cpdef literal newSelect(self, literal a, literal b, literal c):
"""Same as lSelect"""
return literal(bf.bfNewSelect(self.b, a.l, b.l, c.l), self)
cpdef literal lSelect(self, literal c, literal ifTrue, literal ifFalse):
"""Returns a literal that is ifTrue if c is true, and ifFalse otherwise"""
return self.newSelect(c, ifTrue, ifFalse)
cpdef setEqual(self, literal a, literal b):
"""Constraint a and be to equal each other in every assignment"""
bf.bfSetEqual(self.b, a.l, b.l)
cpdef bitvec vAnd(self, bitvec x, bitvec y):
"""Returns b = x&y"""
return wrapbv(self, bf.bfvAnd(self.b, x.v, y.v))
cpdef bitvec vOr(self, bitvec x, bitvec y):
"""Returns b = x|y"""
return wrapbv(self, bf.bfvOr(self.b, x.v, y.v))
cpdef bitvec vXor(self, bitvec x, bitvec y):
"""Returns b = x^y"""
return wrapbv(self, bf.bfvXor(self.b, x.v, y.v))
cpdef bitvec vNegate(self, bitvec x):
"""Returns b = -x"""
return wrapbv(self, bf.bfvNegate(self.b, x.v))
cpdef bitvec vInvert(self, bitvec x):
"""Returns b = ~x"""
return wrapbv(self, bf.bfvInvert(self.b, x.v))
cpdef bitvec vSelect(self, literal t, bitvec thn, bitvec els):
"""Returns a bitvec that is thn whenever t is true, els otherwise"""
return wrapbv(self, bf.bfvSelect(self.b, t.l, thn.v, els.v))
cpdef bitvec vZextend(self, uint32_t new_width, bitvec v):
"""Returns a zero-extended bitvec"""
return wrapbv(self, bf.bfvZextend(self.b, new_width, v.v))
cpdef bitvec vSextend(self, uint32_t new_width, bitvec v):
"""Returns a sign-extended bitvec"""
return wrapbv(self, bf.bfvSextend(self.b, new_width, v.v))
cpdef bitvec vTruncate(self, uint32_t new_width, bitvec v):
"""Returns a new bitvec truncated to new_width"""
return wrapbv(self, bf.bfvTruncate(self.b, new_width, v.v))
cpdef vAdd(self, bitvec x, bitvec y, literal cin):
"""Returns a bitvec x+y+c (cin is the carry in bit)"""
cdef bf.literal cout
res = wrapbv(self, bf.bfvAdd(self.b, x.v, y.v, cin.l, &cout))
return (res, literal(cout, self))
cpdef bitvec vAdd0(self, bitvec x, bitvec y):
"""Returns a bitvec x+y"""
return wrapbv(self, bf.bfvAdd0(self.b, x.v, y.v))
cpdef bitvec vSubtract(self, bitvec x, bitvec y):
"""Returns a bitvec x-y"""
return wrapbv(self, bf.bfvSubtract(self.b, x.v, y.v))
cpdef literal vUlt(self, x, y):
"""Returns a literal that is true exactly when x<y (unsigned comparison)"""
if not isinstance(x, bitvec) and not isinstance(y, bitvec):
raise ValueError, 'need to be passed at least one bit vector'
if isinstance(x, (int, long)):
x = self.vUconstant(len(y), x)
if isinstance(y, (int, long)):
y = self.vUconstant(len(x), y)
return self.vUlt_bitvecs(x, y)
cpdef literal vUlte(self, x, y):
"""Returns a literal that is true exactly when x<=y (unsigned comparison)"""
if not isinstance(x, bitvec) and not isinstance(y, bitvec):
raise ValueError, 'need to be passed at least one bit vector'
if isinstance(x, (int, long)):
x = self.vUconstant(len(y), x)
if isinstance(y, (int, long)):
y = self.vUconstant(len(x), y)
return self.vUlte_bitvecs(x, y)
cdef literal vUlt_bitvecs(self, bitvec x, bitvec y):
return literal(bf.bfvUlt(self.b, x.v, y.v), self)
cdef literal vUlte_bitvecs(self, bitvec x, bitvec y):
return literal(bf.bfvUlte(self.b, x.v, y.v), self)
cpdef vUgt(self, x, y):
"""Returns a literal that is true exactly when x>y (unsigned comparison)"""
return self.vUlt(y, x)
cpdef vUgte(self, x, y):
"""Returns a literal that is true exactly when x>=y (unsigned comparison)"""
return -self.vUlt(x, y)
cpdef literal vSlt(self, bitvec x, bitvec y):
"""Returns a literal that is true exactly when x<y (signed comparison)"""
return literal(bf.bfvSlt(self.b, x.v, y.v), self)
cpdef literal vSlte(self, bitvec x, bitvec y):
"""Returns a literal that is true exactly when x<=y (signed comparison)"""
return literal(bf.bfvSlte(self.b, x.v, y.v), self)
def vSgt(self, x, y):
"""Returns a literal that is true exactly when x>y (signed comparison)"""
return self.vSlt(y, x)
def vSgte(self, x, y):
"""Returns a literal that is true exactly when x>=y (signed comparison)"""
return -self.vSlt(x, y)
cpdef literal vSltZero(self, bitvec x):
"""Returns a literal that is true exactly when x<0 (signed comparison)"""
return literal(bf.bfvSltZero(self.b, x.v), self)
cpdef literal vSgtZero(self, bitvec x):
"""Returns a literal that is true exactly when x>0 (signed comparison)"""
return literal(bf.bfvSgtZero(self.b, x.v), self)
cpdef literal vSgteZero(self, bitvec x):
"""Returns a literal that is true exactly when x>=0 (signed comparison)"""
return literal(bf.bfvSgteZero(self.b, x.v), self)
cpdef literal vEqual(self, bitvec x, bitvec y):
"""Returns a literal that is true exactly when x==y"""
return literal(bf.bfvEqual(self.b, x.v, y.v), self)
cpdef bf.mbool vSame(self, bitvec x, bitvec y):
return bf.bfvSame(x.v, y.v)
cpdef bitvec vConstant(self, uint32_t width, object value):
"""vConstant(width, value)"""
cdef unsigned int i
cdef bitvec result = bitvec.alloc(self, width)
for i in range(0, width):
if value % 2:
result.vPush(self.LIT_TRUE)
else:
result.vPush(self.LIT_FALSE)
value /= 2
return result
cpdef bitvec vSconstant(self, uint32_t width, object value):
warnings.warn('use vConstant(width, value)', DeprecationWarning)
return self.vConstant(width, value)
cpdef bitvec vUconstant(self, uint32_t width, object value):
warnings.warn('use vConstant(width, value)', DeprecationWarning)
return self.vConstant(width, value)
cpdef bitvec vLshift(self, bitvec val, bitvec dist):
return wrapbv(self, bf.bfvLshift(self.b, val.v, dist.v))
cpdef bitvec vLshiftConst(self, bitvec val, uint32_t dist):
return wrapbv(self, bf.bfvLshiftConst(self.b, val.v, dist))
cpdef bitvec vRshift(self, bitvec val, bitvec dist, literal fill):
return wrapbv(self, bf.bfvRshift(self.b, val.v, dist.v, fill.l))
cpdef bitvec vRshiftConst(self, bitvec val, uint32_t dist, literal fill):
return wrapbv(self, bf.bfvRshiftConst(self.b, val.v, dist, fill.l))
cpdef bitvec vRLshift(self, bitvec val, uint32_t dist):
"""Logical right shift"""
return self.vRshiftConst(val, dist, self.LIT_FALSE)
cpdef bitvec vMul(self, bitvec x, bitvec y):
return wrapbv(self, bf.bfvMul(self.b, x.v, y.v))
cpdef vQuotRem(self, bitvec x, bitvec y):
"""vQuotRem(x, y). Returns a pair (quotient, remainder)."""
cdef bf.bitvec *quot = NULL
cdef bf.bitvec *rem = NULL
bf.bfvQuotRem(self.b, x.v, y.v, ", &rem)
return (wrapbv(self, quot), wrapbv(self, rem))
cpdef bitvec vSdiv(self, bitvec x, bitvec y):
return wrapbv(self, bf.bfvSdiv(self.b, x.v, y.v))
cpdef bitvec vSrem(self, bitvec x, bitvec y):
return wrapbv(self, bf.bfvSrem(self.b, x.v, y.v))
cpdef vCopy(self, bitvec dst, bitvec src):
bf.bfvCopy(self.b, dst.v, src.v)
cpdef bitvec vDup(self, bitvec b):
return wrapbv(self, bf.bfvDup(self.b, b.v))
cpdef bitvec vConcat(self, bitvec x, bitvec y):
"""vConcat(x, y). Returns a bit vector of x and y concatenated together"""
return wrapbv(self, bf.bfvConcat(self.b, x.v, y.v))
cpdef bitvec vConcat3(self, bitvec x, bitvec y, bitvec z):
return wrapbv(self, bf.bfvConcat3(self.b, x.v, y.v, z.v))
cpdef bitvec vExtract(self, bitvec x, uint32_t begin, uint32_t length):
return wrapbv(self, bf.bfvExtract(self.b, x.v, begin, length))
cpdef bitvec vReverse(self, bitvec x):
"""Reverse the bits in x"""
return wrapbv(self, bf.bfvReverse(self.b, x.v))
cpdef bitvec vInit(self, uint32_t width):
"""Creates a new, unknown bit vector."""
return wrapbv(self, bf.bfvInit(self.b, width))
cpdef unsigned getNumVars(self):
"""Returns the number of variables in the underlying SAT
instance"""
return bf.bfNumVars(self.b)
cpdef setNumVars(self, unsigned num):
bf.bfSetNumVars(self.b, num)
cpdef lSet(self, literal a):
"""Constrain a literal to be true in every assignment"""
bf.bfSet(self.b, a.l)
cpdef bf.mbool lGet(self, literal a):
return literal(bf.bfGet(self.b, a.l), self)
cpdef bf.bfstatus checkStatus(self, literal a) except? bf.STATUS_UNKNOWN:
return bf.bfCheckStatus(self.b, a.l)
cpdef bf.mbool mayBeTrue(self, literal x):
"""mustBeTrue(x). Returns mbool_true if there is an assignment that can
make literal true; mbool_false othrewise (and mbool_unknown if the solver
cannot determine the answer)."""
return bf.bfMayBeTrue(self.b, x.l)
cpdef bf.mbool mayBeFalse(self, literal x):
"""mustBeTrue(x). Returns mbool_true if there is an assignment that can
make literal false; mbool_false othrewise (and mbool_unknown if the solver
cannot determine the answer)."""
return bf.bfMayBeFalse(self.b, x.l)
cpdef bf.mbool mustBeTrue(self, literal x):
"""mustBeTrue(x). Returns mbool_true if there is no assignment that can
make literal false; mbool_false othrewise (and mbool_unknown if the solver
cannot determine the answer)."""
return bf.bfMustBeTrue(self.b, x.l)
cpdef bf.mbool mustBeFalse(self, literal x):
"""mustBeTrue(x). Returns mbool_true if there is no assignment that can
make literal true; mbool_false othrewise (and mbool_unknown if the solver
cannot determine the answer)."""
return bf.bfMustBeFalse(self.b, x.l)
cpdef vGet(self, bitvec bv):
cdef unsigned int shift
cdef bf.bitvec *shifted
cdef bf.bitvec *truncated
if len(bv) > 64:
r = 0L
for shift in range(0, len(bv), 64):
shifted = bf.bfvRshiftConst(self.b, bv.v, shift, bf.LITERAL_FALSE)
truncated = bf.bfvTruncate(self.b, 64, shifted)
val = bf.bfvGet(self.b, truncated)
r |= val << shift
return r
else:
return bf.bfvGet(self.b, bv.v)
def zero(self, uint32_t size):
return bitvec.zero(self, size)
cpdef bitvec pCOPY(self, bitvec input0):
return wrapbv(self, bf.pCOPY(self.b, input0.v))
cpdef bitvec pINT_ADD(self, bitvec input0, bitvec input1):
return wrapbv(self, bf.pINT_ADD(self.b, input0.v, input1.v))
cpdef bitvec pINT_SUB(self, bitvec input0, bitvec input1):
return wrapbv(self, bf.pINT_SUB(self.b, input0.v, input1.v))
cpdef bitvec pINT_MULT(self, bitvec input0, bitvec input1):
return wrapbv(self, bf.pINT_MULT(self.b, input0.v, input1.v))
cpdef bitvec pINT_DIV(self, bitvec input0, bitvec input1):
return wrapbv(self, bf.pINT_DIV(self.b, input0.v, input1.v))
cpdef bitvec pINT_REM(self, bitvec input0, bitvec input1):
return wrapbv(self, bf.pINT_REM(self.b, input0.v, input1.v))
cpdef bitvec pINT_SDIV(self, bitvec input0, bitvec input1):
return wrapbv(self, bf.pINT_SDIV(self.b, input0.v, input1.v))
cpdef bitvec pINT_SREM(self, bitvec input0, bitvec input1):
return wrapbv(self, bf.pINT_SREM(self.b, input0.v, input1.v))
cpdef bitvec pINT_OR(self, bitvec input0, bitvec input1):
return wrapbv(self, bf.pINT_OR(self.b, input0.v, input1.v))
cpdef bitvec pINT_XOR(self, bitvec input0, bitvec input1):
return wrapbv(self, bf.pINT_XOR(self.b, input0.v, input1.v))
cpdef bitvec pINT_AND(self, bitvec input0, bitvec input1):
return wrapbv(self, bf.pINT_AND(self.b, input0.v, input1.v))
cpdef bitvec pINT_LEFT(self, bitvec input0, bitvec input1):
return wrapbv(self, bf.pINT_LEFT(self.b, input0.v, input1.v))
cpdef bitvec pINT_RIGHT(self, bitvec input0, bitvec input1):
return wrapbv(self, bf.pINT_RIGHT(self.b, input0.v, input1.v))
cpdef bitvec pINT_SRIGHT(self, bitvec input0, bitvec input1):
return wrapbv(self, bf.pINT_SRIGHT(self.b, input0.v, input1.v))
cpdef bitvec pINT_EQUAL(self, bitvec input0, bitvec input1):
return wrapbv(self, bf.pINT_EQUAL(self.b, input0.v, input1.v))
cpdef bitvec pINT_NOTEQUAL(self, bitvec input0, bitvec input1):
return wrapbv(self, bf.pINT_NOTEQUAL(self.b, input0.v, input1.v))
cpdef bitvec pINT_LESS(self, bitvec input0, bitvec input1):
return wrapbv(self, bf.pINT_LESS(self.b, input0.v, input1.v))
cpdef bitvec pINT_LESSEQUAL(self, bitvec input0, bitvec input1):
return wrapbv(self, bf.pINT_LESSEQUAL(self.b, input0.v, input1.v))
cpdef bitvec pINT_CARRY(self, bitvec input0, bitvec input1):
return wrapbv(self, bf.pINT_CARRY(self.b, input0.v, input1.v))