Skip to content

Commit 7161f17

Browse files
author
Daniel Kroening
authored
Merge pull request #1879 from diffblue/smt2-frontend
An SMT2 frontend plus solver binary
2 parents d83fa17 + ab68848 commit 7161f17

25 files changed

+1720
-232
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,8 @@ src/goto-instrument/goto-instrument.exe
104104
src/jbmc/jbmc
105105
src/musketeer/musketeer
106106
src/musketeer/musketeer.exe
107+
src/solvers/smt2/smt2_solver
108+
src/solvers/smt2/smt2_solver.exe
107109
src/symex/symex
108110
src/symex/symex.exe
109111
src/goto-diff/goto-diff

regression/Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ DIRS = cbmc \
1111
strings-smoke-tests \
1212
cbmc-cover \
1313
goto-instrument-typedef \
14+
smt2_solver \
1415
strings \
1516
invariants \
1617
goto-diff \

regression/smt2_solver/Makefile

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
default: tests.log
2+
3+
test:
4+
@../test.pl -p -c ../../../src/solvers/smt2_solver
5+
6+
tests.log: ../test.pl
7+
@../test.pl -p -c ../../../src/solvers/smt2_solver
8+
9+
show:
10+
@for dir in *; do \
11+
if [ -d "$$dir" ]; then \
12+
vim -o "$$dir/*.c" "$$dir/*.out"; \
13+
fi; \
14+
done;
15+
16+
clean:
17+
find -name '*.out' -execdir $(RM) '{}' \;
18+
$(RM) tests.log
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
(set-logic QF_BV)
2+
3+
; From https://rise4fun.com/z3/tutorialcontent/guide
4+
5+
; Basic Bitvector Arithmetic
6+
(define-fun b01 () Bool (= (bvadd #x07 #x03) #x0a)) ; addition
7+
(define-fun b02 () Bool (= (bvsub #x07 #x03) #x04)) ; subtraction
8+
(define-fun b03 () Bool (= (bvneg #x07) #xf9)) ; unary minus
9+
(define-fun b04 () Bool (= (bvmul #x07 #x03) #x15)) ; multiplication
10+
(define-fun b05 () Bool (= (bvurem #x07 #x03) #x01)) ; unsigned remainder
11+
(define-fun b06 () Bool (= (bvsrem #x07 #x03) #x01)) ; signed remainder
12+
(define-fun b07 () Bool (= (bvsmod #x07 #x03) #x01)) ; signed modulo
13+
(define-fun b08 () Bool (= (bvshl #x07 #x03) #x38)) ; shift left
14+
(define-fun b09 () Bool (= (bvlshr #xf0 #x03) #x1e)) ; unsigned (logical) shift right
15+
(define-fun b10 () Bool (= (bvashr #xf0 #x03) #xfe)) ; signed (arithmetical) shift right#x0a
16+
17+
; Bitwise Operations
18+
19+
(define-fun w1 () Bool (= (bvor #x6 #x3) #x7)) ; bitwise or
20+
(define-fun w2 () Bool (= (bvand #x6 #x3) #x2)) ; bitwise and
21+
(define-fun w3 () Bool (= (bvnot #x6) #x9)) ; bitwise not
22+
(define-fun w4 () Bool (= (bvnand #x6 #x3) #xd)) ; bitwise nand
23+
(define-fun w5 () Bool (= (bvnor #x6 #x3) #x8)) ; bitwise nor
24+
(define-fun w6 () Bool (= (bvxnor #x6 #x3) #xa)) ; bitwise xnor
25+
26+
; We can prove a bitwise version of deMorgan's law
27+
28+
(declare-const x (_ BitVec 64))
29+
(declare-const y (_ BitVec 64))
30+
(define-fun d01 () Bool (= (bvand (bvnot x) (bvnot y)) (bvnot (bvor x y))))
31+
32+
; There is a fast way to check that fixed size numbers are powers of two
33+
34+
(define-fun is-power-of-two ((x (_ BitVec 4))) Bool
35+
(= #x0 (bvand x (bvsub x #x1))))
36+
(declare-const a (_ BitVec 4))
37+
(define-fun power-test () Bool
38+
(= (is-power-of-two a)
39+
(or (= a #x0)
40+
(= a #x1)
41+
(= a #x2)
42+
(= a #x4)
43+
(= a #x8))))
44+
45+
; Predicates over Bitvectors
46+
47+
(define-fun p1 () Bool (= (bvule #x0a #xf0) true)) ; unsigned less or equal
48+
(define-fun p2 () Bool (= (bvult #x0a #xf0) true)) ; unsigned less than
49+
(define-fun p3 () Bool (= (bvuge #x0a #xf0) false)) ; unsigned greater or equal
50+
(define-fun p4 () Bool (= (bvugt #x0a #xf0) false)) ; unsigned greater than
51+
(define-fun p5 () Bool (= (bvsle #x0a #xf0) false)) ; signed less or equal
52+
(define-fun p6 () Bool (= (bvslt #x0a #xf0) false)) ; signed less than
53+
(define-fun p7 () Bool (= (bvsge #x0a #xf0) true)) ; signed greater or equal
54+
(define-fun p8 () Bool (= (bvsgt #x0a #xf0) true)) ; signed greater than
55+
56+
; all must be true
57+
58+
(assert (not (and
59+
b01 b02 b03 b04 b05 b06 b07 b08 b09 b10
60+
d01
61+
power-test
62+
p1 p2 p3 p4 p5 p6 p7 p8)))
63+
64+
(check-sat)
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
CORE
2+
basic-bv1.smt2
3+
4+
^EXIT=0$
5+
^SIGNAL=0$
6+
^unsat$
7+
--
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
(set-logic QF_BV)
2+
3+
; try 'let' on bitvectors
4+
5+
(define-fun x () (_ BitVec 4) #x0)
6+
7+
; very simple
8+
(define-fun let0 () Bool (= (let ((x #x0)) #x1) #x1))
9+
10+
; let hides the function 'x'
11+
(define-fun let1 () Bool (= (let ((x #x1)) x) #x1))
12+
13+
; the binding isn't visible immediately
14+
(define-fun let2 () Bool (= (let ((x x)) x) #x0))
15+
16+
; parallel let
17+
(define-fun let3 () Bool (= (let ((x #x1) (y x)) y) #x0))
18+
19+
; limited scope
20+
(define-fun let4 () Bool (and (= (let ((x #x1)) x) #x1) (= x #x0)))
21+
22+
; all must be true
23+
24+
(assert (not (and
25+
let0
26+
let1
27+
let2
28+
let3
29+
let4
30+
)))
31+
32+
(check-sat)
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
CORE
2+
let-with-bv1.smt2
3+
4+
^EXIT=0$
5+
^SIGNAL=0$
6+
^unsat$
7+
--

regression/smt2_solver/let1/let1.smt2

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
(set-logic QF_BV)
2+
3+
(define-fun x () Bool false)
4+
5+
; very simple
6+
(define-fun let0 () Bool (let ((x false)) true))
7+
8+
; let hides the function 'x'
9+
(define-fun let1 () Bool (let ((x true)) x))
10+
11+
; the binding isn't visible immediately
12+
(define-fun let2 () Bool (not (let ((x x)) x)))
13+
14+
; parallel let
15+
(define-fun let3 () Bool (let ((x true) (y x)) (not y)))
16+
17+
; limited scope
18+
(define-fun let4 () Bool (and (let ((x true)) x) (not x)))
19+
20+
; all must be true
21+
22+
(assert (not (and
23+
let0
24+
let1
25+
let2
26+
let3
27+
let4
28+
)))
29+
30+
(check-sat)

regression/smt2_solver/let1/test.desc

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
CORE
2+
let1.smt2
3+
4+
^EXIT=0$
5+
^SIGNAL=0$
6+
^unsat$
7+
--

src/ansi-c/expr2c.cpp

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -944,6 +944,26 @@ std::string expr2ct::convert_with(
944944
return dest;
945945
}
946946

947+
std::string expr2ct::convert_let(
948+
const let_exprt &src,
949+
unsigned precedence)
950+
{
951+
if(src.operands().size()<3)
952+
return convert_norep(src, precedence);
953+
954+
unsigned p0;
955+
std::string op0=convert_with_precedence(src.op0(), p0);
956+
957+
std::string dest="LET ";
958+
dest+=convert(src.symbol());
959+
dest+='=';
960+
dest+=convert(src.value());
961+
dest+=" IN ";
962+
dest+=convert(src.where());
963+
964+
return dest;
965+
}
966+
947967
std::string expr2ct::convert_update(
948968
const exprt &src,
949969
unsigned precedence)
@@ -3936,6 +3956,9 @@ std::string expr2ct::convert_with_precedence(
39363956
else if(src.id()==ID_sizeof)
39373957
return convert_sizeof(src, precedence);
39383958

3959+
else if(src.id()==ID_let)
3960+
return convert_let(to_let_expr(src), precedence=16);
3961+
39393962
else if(src.id()==ID_type)
39403963
return convert(src.type());
39413964

0 commit comments

Comments
 (0)