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
use crate::ast::IntoAst;
use crate::ast::{Ast, Bool, Int, binop, unop};
use crate::{Context, Sort, Symbol};
use std::ffi::CString;
use z3_sys::*;
/// [`Ast`] node representing a bitvector value.
pub struct BV {
pub(crate) ctx: Context,
pub(crate) z3_ast: Z3_ast,
}
macro_rules! bv_overflow_check_signed {
(
$(
$( #[ $attr:meta ] )* $f:ident ( $z3fn:ident ) ;
)*
) => {
$(
$( #[ $attr ] )*
pub fn $f(&self, other: &BV, b: bool) -> Bool {
unsafe {
Ast::wrap(&self.ctx, {
$z3fn(self.ctx.z3_ctx.0, self.z3_ast, other.z3_ast, b).unwrap()
})
}
}
)*
};
}
impl BV {
pub fn from_str(sz: u32, value: &str) -> Option<BV> {
let ctx = &Context::thread_local();
let sort = Sort::bitvector(sz);
let ast = unsafe {
let bv_cstring = CString::new(value).unwrap();
Z3_mk_numeral(ctx.z3_ctx.0, bv_cstring.as_ptr(), sort.z3_sort)?
};
Some(unsafe { Self::wrap(ctx, ast) })
}
/// Create a BV from an array of bits.
///
/// # Examples
/// ```
/// # use z3::{ast::{Ast, BV}, Config, Context, Solver};
/// // 0b00000010
/// let bv = BV::from_bits(&[false, true, false, false, false, false, false, false]).unwrap();
/// let bv_none = BV::from_bits(&[]);
/// assert_eq!(bv, 2);
/// assert_eq!(bv_none, None);
/// ```
pub fn from_bits(bits: &[bool]) -> Option<BV> {
let ctx = &Context::thread_local();
let ast = unsafe { Z3_mk_bv_numeral(ctx.z3_ctx.0, bits.len() as u32, bits.as_ptr())? };
Some(unsafe { Self::wrap(ctx, ast) })
}
pub fn new_const<S: Into<Symbol>>(name: S, sz: u32) -> BV {
let ctx = &Context::thread_local();
let sort = Sort::bitvector(sz);
unsafe {
Self::wrap(ctx, {
Z3_mk_const(ctx.z3_ctx.0, name.into().as_z3_symbol(), sort.z3_sort).unwrap()
})
}
}
pub fn fresh_const(prefix: &str, sz: u32) -> BV {
let ctx = &Context::thread_local();
let sort = Sort::bitvector(sz);
unsafe {
Self::wrap(ctx, {
let pp = CString::new(prefix).unwrap();
let p = pp.as_ptr();
Z3_mk_fresh_const(ctx.z3_ctx.0, p, sort.z3_sort).unwrap()
})
}
}
pub fn from_i64(i: i64, sz: u32) -> BV {
let ctx = &Context::thread_local();
let sort = Sort::bitvector(sz);
unsafe { Self::wrap(ctx, Z3_mk_int64(ctx.z3_ctx.0, i, sort.z3_sort).unwrap()) }
}
pub fn from_u64(u: u64, sz: u32) -> BV {
let ctx = &Context::thread_local();
let sort = Sort::bitvector(sz);
unsafe {
Self::wrap(
ctx,
Z3_mk_unsigned_int64(ctx.z3_ctx.0, u, sort.z3_sort).unwrap(),
)
}
}
pub fn as_i64(&self) -> Option<i64> {
unsafe {
let mut tmp: ::std::os::raw::c_longlong = 0;
if Z3_get_numeral_int64(self.ctx.z3_ctx.0, self.z3_ast, &mut tmp) {
Some(tmp)
} else {
None
}
}
}
pub fn as_u64(&self) -> Option<u64> {
unsafe {
let mut tmp: ::std::os::raw::c_ulonglong = 0;
if Z3_get_numeral_uint64(self.ctx.z3_ctx.0, self.z3_ast, &mut tmp) {
Some(tmp)
} else {
None
}
}
}
/// Create a bit vector from an integer.
///
/// The bit vector will have width `sz`.
///
/// # Examples
/// ```
/// # use z3::{ast, Config, Context, SatResult, Solver};
/// # use z3::ast::Ast;
/// # let solver = Solver::new();
/// let i = ast::Int::new_const("x");
/// solver.assert(&i.eq(&ast::Int::from_i64(-3)));
///
/// let x = ast::BV::from_int(&i, 64);
/// assert_eq!(64, x.get_size());
///
/// assert_eq!(solver.check(), SatResult::Sat);
/// let model = solver.get_model().unwrap();
///
/// assert_eq!(-3, model.eval(&x.to_int(true), true).unwrap().as_i64().expect("as_i64() shouldn't fail"));
/// ```
pub fn from_int(ast: &Int, sz: u32) -> BV {
unsafe {
Self::wrap(
&ast.ctx,
Z3_mk_int2bv(ast.ctx.z3_ctx.0, sz, ast.z3_ast).unwrap(),
)
}
}
/// Create an integer from a bitvector.
/// This is just a convenience wrapper around
/// [`Int::from_bv()`]; see notes there.
pub fn to_int(&self, signed: bool) -> Int {
Int::from_bv(self, signed)
}
/// Get the size of the bitvector (in bits)
pub fn get_size(&self) -> u32 {
let sort = self.get_sort();
unsafe { Z3_get_bv_sort_size(self.ctx.z3_ctx.0, sort.z3_sort) }
}
// Bitwise ops
unop! {
/// Bitwise negation
bvnot(Z3_mk_bvnot, Self);
/// Two's complement negation
bvneg(Z3_mk_bvneg, Self);
/// Conjunction of all the bits in the vector. Returns a BV with size (bitwidth) 1.
bvredand(Z3_mk_bvredand, Self);
/// Disjunction of all the bits in the vector. Returns a BV with size (bitwidth) 1.
bvredor(Z3_mk_bvredor, Self);
}
binop! {
/// Bitwise and
bvand(Z3_mk_bvand, Self);
/// Bitwise or
bvor(Z3_mk_bvor, Self);
/// Bitwise exclusive-or
bvxor(Z3_mk_bvxor, Self);
/// Bitwise nand
bvnand(Z3_mk_bvnand, Self);
/// Bitwise nor
bvnor(Z3_mk_bvnor, Self);
/// Bitwise xnor
bvxnor(Z3_mk_bvxnor, Self);
}
// Arithmetic ops
binop! {
/// Addition
bvadd(Z3_mk_bvadd, Self);
/// Subtraction
bvsub(Z3_mk_bvsub, Self);
/// Multiplication
bvmul(Z3_mk_bvmul, Self);
/// Unsigned division
bvudiv(Z3_mk_bvudiv, Self);
/// Signed division
bvsdiv(Z3_mk_bvsdiv, Self);
/// Unsigned remainder
bvurem(Z3_mk_bvurem, Self);
/// Signed remainder (sign follows dividend)
bvsrem(Z3_mk_bvsrem, Self);
/// Signed remainder (sign follows divisor)
bvsmod(Z3_mk_bvsmod, Self);
}
// Comparison ops
binop! {
/// Unsigned less than
bvult(Z3_mk_bvult, Bool);
/// Signed less than
bvslt(Z3_mk_bvslt, Bool);
/// Unsigned less than or equal
bvule(Z3_mk_bvule, Bool);
/// Signed less than or equal
bvsle(Z3_mk_bvsle, Bool);
/// Unsigned greater or equal
bvuge(Z3_mk_bvuge, Bool);
/// Signed greater or equal
bvsge(Z3_mk_bvsge, Bool);
/// Unsigned greater than
bvugt(Z3_mk_bvugt, Bool);
/// Signed greater than
bvsgt(Z3_mk_bvsgt, Bool);
}
// Shift ops
binop! {
/// Shift left
bvshl(Z3_mk_bvshl, Self);
/// Logical shift right (add zeroes in the high bits)
bvlshr(Z3_mk_bvlshr, Self);
/// Arithmetic shift right (sign-extend in the high bits)
bvashr(Z3_mk_bvashr, Self);
/// Rotate left
bvrotl(Z3_mk_ext_rotate_left, Self);
/// Rotate right
bvrotr(Z3_mk_ext_rotate_right, Self);
}
binop! {
/// Concatenate two bitvectors
concat(Z3_mk_concat, Self);
}
// overflow checks
unop! {
/// Check if negation overflows
bvneg_no_overflow(Z3_mk_bvneg_no_overflow, Bool);
}
bv_overflow_check_signed! {
/// Check if addition overflows
bvadd_no_overflow(Z3_mk_bvadd_no_overflow);
/// Check if subtraction underflows
bvsub_no_underflow(Z3_mk_bvsub_no_underflow);
/// Check if multiplication overflows
bvmul_no_overflow(Z3_mk_bvmul_no_overflow);
}
binop! {
/// Check if addition underflows
bvadd_no_underflow(Z3_mk_bvadd_no_underflow, Bool);
/// Check if subtraction overflows
bvsub_no_overflow(Z3_mk_bvsub_no_overflow, Bool);
/// Check if signed division overflows
bvsdiv_no_overflow(Z3_mk_bvsdiv_no_overflow, Bool);
/// Check if multiplication underflows
bvmul_no_underflow(Z3_mk_bvmul_no_underflow, Bool);
}
/// Extract the bits `high` down to `low` from the bitvector.
/// Returns a bitvector of size `n`, where `n = high - low + 1`.
pub fn extract(&self, high: u32, low: u32) -> Self {
unsafe {
Self::wrap(&self.ctx, {
Z3_mk_extract(self.ctx.z3_ctx.0, high, low, self.z3_ast).unwrap()
})
}
}
/// Sign-extend the bitvector to size `m+i`, where `m` is the original size of the bitvector.
/// That is, `i` bits will be added.
pub fn sign_ext(&self, i: u32) -> Self {
unsafe {
Self::wrap(&self.ctx, {
Z3_mk_sign_ext(self.ctx.z3_ctx.0, i, self.z3_ast).unwrap()
})
}
}
/// Zero-extend the bitvector to size `m+i`, where `m` is the original size of the bitvector.
/// That is, `i` bits will be added.
pub fn zero_ext(&self, i: u32) -> Self {
unsafe {
Self::wrap(&self.ctx, {
Z3_mk_zero_ext(self.ctx.z3_ctx.0, i, self.z3_ast).unwrap()
})
}
}
}
macro_rules! into_bv {
($t:ty) => {
impl IntoAst<BV> for $t {
fn into_ast(self, a: &BV) -> BV {
BV::from_u64(self as u64, a.get_size())
}
}
};
}
macro_rules! into_bv_signed {
($t:ty) => {
impl IntoAst<BV> for $t {
fn into_ast(self, a: &BV) -> BV {
BV::from_i64(self as i64, a.get_size())
}
}
};
}
into_bv!(u8);
into_bv!(u16);
into_bv!(u32);
into_bv!(u64);
into_bv_signed!(i8);
into_bv_signed!(i16);
into_bv_signed!(i32);
into_bv_signed!(i64);