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
//! Operator kinds.
//!
//! The semantics of every operator are total and follow SMT-LIB QF_BV; see
//! `docs/design.md` ยง3.2. Shift and rotate counts have the width of the shifted value and are
//! read as unsigned.
/// Unary operators. All map a `W`-bit operand to a `W`-bit result.
#[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Debug)]
#[non_exhaustive]
pub enum UnOp {
/// Bitwise complement.
Not,
/// Two's-complement negation.
Neg,
/// Number of set bits.
Popcnt,
/// Number of leading zero bits; `clz(0) = W`.
Clz,
/// Number of trailing zero bits; `ctz(0) = W`.
Ctz,
/// Byte reversal. Only defined when `W % 8 == 0` (a typing rule, not partiality).
Bswap,
/// Bit reversal.
BitRev,
}
impl UnOp {
/// Every unary operator.
pub const ALL: [UnOp; 7] = [
UnOp::Not,
UnOp::Neg,
UnOp::Popcnt,
UnOp::Clz,
UnOp::Ctz,
UnOp::Bswap,
UnOp::BitRev,
];
/// The operator's name in the expression syntax.
pub const fn name(self) -> &'static str {
match self {
UnOp::Not => "not",
UnOp::Neg => "neg",
UnOp::Popcnt => "popcnt",
UnOp::Clz => "clz",
UnOp::Ctz => "ctz",
UnOp::Bswap => "bswap",
UnOp::BitRev => "bitrev",
}
}
}
/// Binary operators. All map two `W`-bit operands to a `W`-bit result.
#[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Debug)]
#[non_exhaustive]
pub enum BinOp {
/// Addition modulo `2^W`.
Add,
/// Subtraction modulo `2^W`.
Sub,
/// Multiplication modulo `2^W`.
Mul,
/// High `W` bits of the `2W`-bit unsigned product.
UMulHi,
/// High `W` bits of the `2W`-bit signed product.
SMulHi,
/// Unsigned division; `udiv(x, 0) = ones`.
UDiv,
/// Unsigned remainder; `urem(x, 0) = x`.
URem,
/// Signed truncating division (`bvsdiv`); `sdiv(x, 0) = (x <s 0 ? 1 : -1)`.
SDiv,
/// Signed remainder (`bvsrem`), sign follows the dividend; `srem(x, 0) = x`.
SRem,
/// Bitwise and.
And,
/// Bitwise or.
Or,
/// Bitwise exclusive or.
Xor,
/// Left shift; a count `>= W` gives 0.
Shl,
/// Logical right shift; a count `>= W` gives 0.
LShr,
/// Arithmetic right shift; a count `>= W` gives the sign fill.
AShr,
/// Rotate left by `count mod W`.
RotL,
/// Rotate right by `count mod W`.
RotR,
/// Parallel bit deposit of the first operand under the mask in the second.
Pdep,
/// Parallel bit extract of the first operand under the mask in the second.
Pext,
}
impl BinOp {
/// Every binary operator.
pub const ALL: [BinOp; 19] = [
BinOp::Add,
BinOp::Sub,
BinOp::Mul,
BinOp::UMulHi,
BinOp::SMulHi,
BinOp::UDiv,
BinOp::URem,
BinOp::SDiv,
BinOp::SRem,
BinOp::And,
BinOp::Or,
BinOp::Xor,
BinOp::Shl,
BinOp::LShr,
BinOp::AShr,
BinOp::RotL,
BinOp::RotR,
BinOp::Pdep,
BinOp::Pext,
];
/// The operator's name in the expression syntax.
pub const fn name(self) -> &'static str {
match self {
BinOp::Add => "add",
BinOp::Sub => "sub",
BinOp::Mul => "mul",
BinOp::UMulHi => "umulhi",
BinOp::SMulHi => "smulhi",
BinOp::UDiv => "udiv",
BinOp::URem => "urem",
BinOp::SDiv => "sdiv",
BinOp::SRem => "srem",
BinOp::And => "and",
BinOp::Or => "or",
BinOp::Xor => "xor",
BinOp::Shl => "shl",
BinOp::LShr => "lshr",
BinOp::AShr => "ashr",
BinOp::RotL => "rotl",
BinOp::RotR => "rotr",
BinOp::Pdep => "pdep",
BinOp::Pext => "pext",
}
}
/// Whether the operands may be swapped without changing the result.
pub const fn is_commutative(self) -> bool {
matches!(
self,
BinOp::Add
| BinOp::Mul
| BinOp::UMulHi
| BinOp::SMulHi
| BinOp::And
| BinOp::Or
| BinOp::Xor
)
}
}
/// The comparison predicates stored in expressions. Each maps two `W`-bit operands to a 1-bit
/// result. `ugt`, `uge`, `sgt` and `sge` are spelled by swapping operands; see [`CmpOpExt`].
#[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Debug)]
#[non_exhaustive]
pub enum CmpOp {
/// Equal.
Eq,
/// Not equal.
Ne,
/// Unsigned less than.
Ult,
/// Unsigned less than or equal.
Ule,
/// Signed less than.
Slt,
/// Signed less than or equal.
Sle,
}
impl CmpOp {
/// Every stored predicate.
pub const ALL: [CmpOp; 6] = [
CmpOp::Eq,
CmpOp::Ne,
CmpOp::Ult,
CmpOp::Ule,
CmpOp::Slt,
CmpOp::Sle,
];
/// Whether the operands may be swapped without changing the result.
pub const fn is_commutative(self) -> bool {
matches!(self, CmpOp::Eq | CmpOp::Ne)
}
/// The logical negation of this predicate over the same operand order, as an extended
/// predicate (for example `!(a <u b)` is `a >=u b`).
pub const fn negated(self) -> CmpOpExt {
match self {
CmpOp::Eq => CmpOpExt::Ne,
CmpOp::Ne => CmpOpExt::Eq,
CmpOp::Ult => CmpOpExt::Uge,
CmpOp::Ule => CmpOpExt::Ugt,
CmpOp::Slt => CmpOpExt::Sge,
CmpOp::Sle => CmpOpExt::Sgt,
}
}
}
/// Every comparison predicate, including the operand-swapped forms.
#[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Debug)]
#[non_exhaustive]
pub enum CmpOpExt {
/// Equal.
Eq,
/// Not equal.
Ne,
/// Unsigned less than.
Ult,
/// Unsigned less than or equal.
Ule,
/// Unsigned greater than.
Ugt,
/// Unsigned greater than or equal.
Uge,
/// Signed less than.
Slt,
/// Signed less than or equal.
Sle,
/// Signed greater than.
Sgt,
/// Signed greater than or equal.
Sge,
}
impl CmpOpExt {
/// Every predicate.
pub const ALL: [CmpOpExt; 10] = [
CmpOpExt::Eq,
CmpOpExt::Ne,
CmpOpExt::Ult,
CmpOpExt::Ule,
CmpOpExt::Ugt,
CmpOpExt::Uge,
CmpOpExt::Slt,
CmpOpExt::Sle,
CmpOpExt::Sgt,
CmpOpExt::Sge,
];
/// The stored predicate and whether the operands must be swapped to express `self`.
pub const fn canonical(self) -> (CmpOp, bool) {
match self {
CmpOpExt::Eq => (CmpOp::Eq, false),
CmpOpExt::Ne => (CmpOp::Ne, false),
CmpOpExt::Ult => (CmpOp::Ult, false),
CmpOpExt::Ule => (CmpOp::Ule, false),
CmpOpExt::Ugt => (CmpOp::Ult, true),
CmpOpExt::Uge => (CmpOp::Ule, true),
CmpOpExt::Slt => (CmpOp::Slt, false),
CmpOpExt::Sle => (CmpOp::Sle, false),
CmpOpExt::Sgt => (CmpOp::Slt, true),
CmpOpExt::Sge => (CmpOp::Sle, true),
}
}
/// The predicate's spelling in the expression syntax.
pub const fn symbol(self) -> &'static str {
match self {
CmpOpExt::Eq => "==",
CmpOpExt::Ne => "!=",
CmpOpExt::Ult => "<u",
CmpOpExt::Ule => "<=u",
CmpOpExt::Ugt => ">u",
CmpOpExt::Uge => ">=u",
CmpOpExt::Slt => "<s",
CmpOpExt::Sle => "<=s",
CmpOpExt::Sgt => ">s",
CmpOpExt::Sge => ">=s",
}
}
}
impl From<CmpOp> for CmpOpExt {
fn from(op: CmpOp) -> Self {
match op {
CmpOp::Eq => CmpOpExt::Eq,
CmpOp::Ne => CmpOpExt::Ne,
CmpOp::Ult => CmpOpExt::Ult,
CmpOp::Ule => CmpOpExt::Ule,
CmpOp::Slt => CmpOpExt::Slt,
CmpOp::Sle => CmpOpExt::Sle,
}
}
}