• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

formalsec / smtml / 542

22 May 2026 07:45AM UTC coverage: 48.103% (-0.1%) from 48.2%
542

push

github

filipeom
[z3] add implement `Re.allchar` and `Re.diff` using native API

Since these aren't yet available in the API we can use the native
function and leverage `Obj.magic` to implement `Re.allchar` and
`Re.diff`.

2434 of 5060 relevant lines covered (48.1%)

40.89 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

47.27
/src/smtml/expr.ml
1
(* SPDX-License-Identifier: MIT *)
2
(* Copyright (C) 2023-2026 formalsec *)
3
(* Written by the Smtml programmers *)
4

5
type t = expr Hc.hash_consed
6

7
and expr =
8
  | Val of Value.t
9
  | Ptr of
10
      { base : Bitvector.t
11
      ; offset : t
12
      }
13
  | Symbol of Symbol.t
14
  | List of t list
15
  | App of Symbol.t * t list
16
  | Unop of Ty.t * Ty.Unop.t * t
17
  | Binop of Ty.t * Ty.Binop.t * t * t
18
  | Triop of Ty.t * Ty.Triop.t * t * t * t
19
  | Relop of Ty.t * Ty.Relop.t * t * t
20
  | Cvtop of Ty.t * Ty.Cvtop.t * t
21
  | Naryop of Ty.t * Ty.Naryop.t * t list
22
  | Extract of t * int * int
23
  | Concat of t * t
24
  | Binder of Binder.t * t list * t
25

26
module Expr = struct
27
  type t = expr
28

29
  let list_eq (l1 : 'a list) (l2 : 'a list) : bool = List.equal phys_equal l1 l2
6✔
30

31
  let equal (e1 : expr) (e2 : expr) : bool =
32
    match (e1, e2) with
1,931✔
33
    | Val v1, Val v2 -> Value.equal v1 v2
998✔
34
    | Ptr { base = b1; offset = o1 }, Ptr { base = b2; offset = o2 } ->
4✔
35
      Bitvector.equal b1 b2 && phys_equal o1 o2
4✔
36
    | Symbol s1, Symbol s2 -> Symbol.equal s1 s2
419✔
37
    | List l1, List l2 -> list_eq l1 l2
4✔
38
    | App (s1, l1), App (s2, l2) -> Symbol.equal s1 s2 && list_eq l1 l2
×
39
    | Unop (t1, op1, e1), Unop (t2, op2, e2) ->
39✔
40
      Ty.equal t1 t2 && Ty.Unop.equal op1 op2 && phys_equal e1 e2
39✔
41
    | Binop (t1, op1, e1, e3), Binop (t2, op2, e2, e4) ->
117✔
42
      Ty.equal t1 t2 && Ty.Binop.equal op1 op2 && phys_equal e1 e2
102✔
43
      && phys_equal e3 e4
94✔
44
    | Relop (t1, op1, e1, e3), Relop (t2, op2, e2, e4) ->
59✔
45
      Ty.equal t1 t2 && Ty.Relop.equal op1 op2 && phys_equal e1 e2
48✔
46
      && phys_equal e3 e4
40✔
47
    | Triop (t1, op1, e1, e3, e5), Triop (t2, op2, e2, e4, e6) ->
4✔
48
      Ty.equal t1 t2 && Ty.Triop.equal op1 op2 && phys_equal e1 e2
4✔
49
      && phys_equal e3 e4 && phys_equal e5 e6
4✔
50
    | Cvtop (t1, op1, e1), Cvtop (t2, op2, e2) ->
25✔
51
      Ty.equal t1 t2 && Ty.Cvtop.equal op1 op2 && phys_equal e1 e2
25✔
52
    | Naryop (t1, op1, l1), Naryop (t2, op2, l2) ->
2✔
53
      Ty.equal t1 t2 && Ty.Naryop.equal op1 op2 && list_eq l1 l2
2✔
54
    | Extract (e1, h1, l1), Extract (e2, h2, l2) ->
×
55
      phys_equal e1 e2 && h1 = h2 && l1 = l2
×
56
    | Concat (e1, e3), Concat (e2, e4) -> phys_equal e1 e2 && phys_equal e3 e4
×
57
    | Binder (binder1, vars1, e1), Binder (binder2, vars2, e2) ->
×
58
      Binder.equal binder1 binder2 && list_eq vars1 vars2 && phys_equal e1 e2
×
59
    | ( ( Val _ | Ptr _ | Symbol _ | List _ | App _ | Unop _ | Binop _ | Triop _
×
60
        | Relop _ | Cvtop _ | Naryop _ | Extract _ | Concat _ | Binder _ )
×
61
      , _ ) ->
62
      false
63

64
  (* Optimized mixer (DJB2 variant). Inlines to simple arithmetic. *)
65
  let[@inline] combine h v = (h * 33) + v
8,228✔
66

67
  let hash (e : expr) : int =
68
    match e with
7,737✔
69
    | Val v -> Value.hash v
3,545✔
70
    | Ptr { base; offset } -> combine (Bitvector.hash base) offset.tag
22✔
71
    | Symbol s -> Symbol.hash s
1,322✔
72
    | List l -> List.fold_left (fun acc x -> combine acc x.Hc.tag) 0 l
16✔
73
    | App (s, es) ->
40✔
74
      let h_s = Symbol.hash s in
75
      List.fold_left (fun acc x -> combine acc x.Hc.tag) h_s es
40✔
76
    | Unop (ty, op, e) ->
161✔
77
      let h1 = Ty.hash ty in
78
      let h2 = combine h1 (Ty.Unop.hash op) in
161✔
79
      combine h2 e.tag
161✔
80
    | Binop (ty, op, e1, e2) ->
1,472✔
81
      let h = Ty.hash ty in
82
      let h = combine h (Ty.Binop.hash op) in
1,472✔
83
      let h = combine h e1.tag in
1,472✔
84
      combine h e2.tag
1,472✔
85
    | Triop (ty, op, e1, e2, e3) ->
44✔
86
      let h = Ty.hash ty in
87
      let h = combine h (Ty.Triop.hash op) in
44✔
88
      let h = combine h e1.tag in
44✔
89
      let h = combine h e2.tag in
44✔
90
      combine h e3.tag
44✔
91
    | Relop (ty, op, e1, e2) ->
962✔
92
      let h = Ty.hash ty in
93
      let h = combine h (Ty.Relop.hash op) in
962✔
94
      let h = combine h e1.tag in
962✔
95
      combine h e2.tag
962✔
96
    | Cvtop (ty, op, e) ->
77✔
97
      let h = Ty.hash ty in
98
      let h = combine h (Ty.Cvtop.hash op) in
77✔
99
      combine h e.tag
77✔
100
    | Naryop (ty, op, es) ->
14✔
101
      let h = Ty.hash ty in
102
      let h = combine h (Ty.Naryop.hash op) in
14✔
103
      List.fold_left (fun acc x -> combine acc x.Hc.tag) h es
14✔
104
    | Extract (e, hi, lo) ->
26✔
105
      let h = e.tag in
106
      let h = combine h hi in
107
      combine h lo
26✔
108
    | Concat (e1, e2) -> combine e1.tag e2.tag
8✔
109
    | Binder (b, vars, e) ->
28✔
110
      let h = Binder.hash b in
111
      let h_vars = List.fold_left (fun acc x -> combine acc x.Hc.tag) h vars in
28✔
112
      combine h_vars e.tag
28✔
113
end
114

115
module Hc = Hc.Make_strong [@inlined hint] (Expr)
116

117
let equal (hte1 : t) (hte2 : t) = phys_equal hte1 hte2 [@@inline]
250✔
118

119
let hash (hte : t) = hte.tag [@@inline]
10✔
120

121
module Key = struct
122
  type nonrec t = t
123

124
  let to_int hte = hash hte
6✔
125

126
  let compare x y = compare (to_int x) (to_int y)
3✔
127
end
128

129
let[@inline] make e = Hc.hashcons e
4,666✔
130

131
let[@inline] view (hte : t) = hte.node
7,468✔
132

133
let[@inline] compare (hte1 : t) (hte2 : t) = compare hte1.tag hte2.tag
×
134

135
let symbol s = make (Symbol s)
863✔
136

137
(** The return type of an expression *)
138
let rec ty (hte : t) : Ty.t =
139
  match view hte with
310✔
140
  | Val x -> Value.type_of x
58✔
141
  | Ptr _ -> Ty_bitv 32
×
142
  | Symbol x -> Symbol.type_of x
174✔
143
  | List _ -> Ty_list
×
144
  | App (sym, _) ->
×
145
    begin match sym.ty with Ty_none -> Ty_app | ty -> ty
×
146
    end
147
  | Triop (_, Ite, _, hte1, hte2) ->
×
148
    let ty1 = ty hte1 in
149
    assert (
×
150
      let ty2 = ty hte2 in
151
      Ty.equal ty1 ty2 );
×
152
    ty1
153
  | Cvtop (_, (Zero_extend m | Sign_extend m), hte) -> (
×
154
    match ty hte with Ty_bitv n -> Ty_bitv (n + m) | _ -> assert false )
1✔
155
  | Unop (ty, _, _)
21✔
156
  | Binop (ty, _, _, _)
35✔
157
  | Triop (ty, _, _, _, _)
×
158
  | Relop (ty, _, _, _)
11✔
159
  | Cvtop (ty, _, _)
2✔
160
  | Naryop (ty, _, _) ->
1✔
161
    ty
162
  | Extract (_, h, l) -> Ty_bitv (h - l + 1)
7✔
163
  | Concat (e1, e2) -> (
×
164
    match (ty e1, ty e2) with
×
165
    | Ty_bitv n1, Ty_bitv n2 -> Ty_bitv (n1 + n2)
×
166
    | t1, t2 ->
×
167
      Fmt.failwith "Invalid concat of (%a) with (%a)" Ty.pp t1 Ty.pp t2 )
168
  | Binder (_, _, e) -> ty e
×
169

170
let rec is_symbolic (v : t) : bool =
171
  match view v with
×
172
  | Val _ -> false
×
173
  | Symbol _ -> true
×
174
  | Ptr { offset; _ } -> is_symbolic offset
×
175
  | Unop (_, _, v) | Cvtop (_, _, v) | Extract (v, _, _) | Binder (_, _, v) ->
×
176
    is_symbolic v
177
  | Binop (_, _, v1, v2) | Relop (_, _, v1, v2) | Concat (v1, v2) ->
×
178
    is_symbolic v1 || is_symbolic v2
×
179
  | Triop (_, _, v1, v2, v3) ->
×
180
    is_symbolic v1 || is_symbolic v2 || is_symbolic v3
×
181
  | List vs | App (_, vs) | Naryop (_, _, vs) -> List.exists is_symbolic vs
×
182

183
let rec get_symbols_aux_loop k acc = function
184
  | [] -> k acc
6✔
185
  | hd :: tl ->
13✔
186
    get_symbols_aux (fun acc -> get_symbols_aux_loop k acc tl) acc hd
13✔
187

188
and get_symbols_aux k acc (hte : t) =
189
  match view hte with
13✔
190
  | Val _ -> k acc
3✔
191
  | Ptr { offset; _ } -> get_symbols_aux k acc offset
×
192
  | Symbol s -> k (s :: acc)
5✔
193
  | List es | App (_, es) | Naryop (_, _, es) -> get_symbols_aux_loop k acc es
×
194
  | Unop (_, _, e) | Cvtop (_, _, e) | Extract (e, _, _) ->
×
195
    get_symbols_aux k acc e
196
  | Binop (_, _, e1, e2) | Relop (_, _, e1, e2) | Concat (e1, e2) ->
×
197
    get_symbols_aux_loop k acc [ e1; e2 ]
198
  | Triop (_, _, e1, e2, e3) -> get_symbols_aux_loop k acc [ e1; e2; e3 ]
×
199
  | Binder (_, vars, e) -> get_symbols_aux_loop k acc (e :: vars)
×
200

201
let get_symbols (hte : t list) =
202
  get_symbols_aux_loop Fun.id [] hte |> List.sort_uniq Symbol.compare
1✔
203

204
let rec pp_with ~printer fmt (hte : t) =
205
  match view hte with
×
206
  | Val v -> Value.pp_with ~printer fmt v
×
207
  | Ptr { base; offset } ->
×
208
    Fmt.pf fmt "(Ptr %a %a)"
209
      (Bitvector.pp_with ~printer)
210
      base (pp_with ~printer) offset
211
  | Symbol s -> Fmt.pf fmt "@[<hov 1>%a@]" Symbol.pp s
×
212
  | List v ->
×
213
    Fmt.pf fmt "@[<hov 1>[%a]@]" (Fmt.list ~sep:Fmt.comma (pp_with ~printer)) v
×
214
  | App (s, v) ->
×
215
    Fmt.pf fmt "@[<hov 1>(%a@ %a)@]" Symbol.pp s
216
      (Fmt.list ~sep:Fmt.comma (pp_with ~printer))
×
217
      v
218
  | Unop (ty, op, e) ->
×
219
    Fmt.pf fmt "@[<hov 1>(%a.%a@ %a)@]" Ty.pp ty Ty.Unop.pp op
220
      (pp_with ~printer) e
221
  | Binop (ty, op, e1, e2) ->
×
222
    Fmt.pf fmt "@[<hov 1>(%a.%a@ %a@ %a)@]" Ty.pp ty Ty.Binop.pp op
223
      (pp_with ~printer) e1 (pp_with ~printer) e2
224
  | Triop (ty, op, e1, e2, e3) ->
×
225
    Fmt.pf fmt "@[<hov 1>(%a.%a@ %a@ %a@ %a)@]" Ty.pp ty Ty.Triop.pp op
226
      (pp_with ~printer) e1 (pp_with ~printer) e2 (pp_with ~printer) e3
227
  | Relop (ty, op, e1, e2) ->
×
228
    Fmt.pf fmt "@[<hov 1>(%a.%a@ %a@ %a)@]" Ty.pp ty Ty.Relop.pp op
229
      (pp_with ~printer) e1 (pp_with ~printer) e2
230
  | Cvtop (ty, op, e) ->
×
231
    Fmt.pf fmt "@[<hov 1>(%a.%a@ %a)@]" Ty.pp ty Ty.Cvtop.pp op
232
      (pp_with ~printer) e
233
  | Naryop (ty, op, es) ->
×
234
    Fmt.pf fmt "@[<hov 1>(%a.%a@ %a)@]" Ty.pp ty Ty.Naryop.pp op
235
      (Fmt.list ~sep:Fmt.sp (pp_with ~printer))
×
236
      es
237
  | Extract (e, h, l) ->
×
238
    Fmt.pf fmt "@[<hov 1>(extract@ %a@ %d@ %d)@]" (pp_with ~printer) e l h
239
  | Concat (e1, e2) ->
×
240
    Fmt.pf fmt "@[<hov 1>(++@ %a@ %a)@]" (pp_with ~printer) e1
241
      (pp_with ~printer) e2
242
  | Binder (b, vars, e) ->
×
243
    Fmt.pf fmt "@[<hov 1>(%a@ (%a)@ %a)@]" Binder.pp b
244
      (Fmt.list ~sep:Fmt.sp (pp_with ~printer))
×
245
      vars (pp_with ~printer) e
246

247
let pp fmt e = pp_with ~printer:Without_type fmt e
×
248

249
let pp_safe fmt e = pp_with ~printer:With_type_and_hexa_float fmt e
×
250

251
let pp_list fmt (es : t list) = Fmt.hovbox (Fmt.list ~sep:Fmt.comma pp) fmt es
×
252

253
let pp_smtml fmt (es : t list) : unit =
254
  let pp_symbols fmt syms =
×
255
    Fmt.list ~sep:Fmt.cut
×
256
      (fun fmt sym ->
257
        let t = Symbol.type_of sym in
×
258
        Fmt.pf fmt "(let-const %a %a)" Symbol.pp sym Ty.pp t )
×
259
      fmt syms
260
  in
261
  let pp_asserts fmt es =
262
    Fmt.list ~sep:Fmt.cut
×
263
      (fun fmt e -> Fmt.pf fmt "(assert @[<h 2>%a@])" pp_safe e)
×
264
      fmt es
265
  in
266
  let syms = get_symbols es in
267
  if List.length syms > 0 then Fmt.pf fmt "@[<v>%a@]@\n" pp_symbols syms;
×
268
  if List.length es > 0 then Fmt.pf fmt "@[<v>%a@]@\n" pp_asserts es;
×
269
  Fmt.string fmt "(check-sat)"
×
270

271
let to_string e = Fmt.str "%a" pp e
×
272

273
let value (v : Value.t) : t = make (Val v) [@@inline]
2,262✔
274

275
let ptr base offset = make (Ptr { base = Bitvector.of_int32 base; offset })
7✔
276

277
let list l = make (List l)
5✔
278

279
let app symbol args = make (App (symbol, args))
20✔
280

281
let[@inline] binder bt vars expr = make (Binder (bt, vars, expr))
14✔
282

283
let let_in vars body = binder Let_in vars body
8✔
284

285
let forall vars body = binder Forall vars body
2✔
286

287
let exists vars body = binder Exists vars body
1✔
288

289
let raw_unop ty op hte = make (Unop (ty, op, hte)) [@@inline]
100✔
290

291
let negate_relop (hte : t) : t =
292
  let e =
6✔
293
    match view hte with
294
    | Relop (ty, Eq, e1, e2) -> Relop (ty, Ne, e1, e2)
4✔
295
    | Relop (ty, Ne, e1, e2) -> Relop (ty, Eq, e1, e2)
×
296
    | Relop (ty, Lt, e1, e2) -> Relop (ty, Le, e2, e1)
2✔
297
    | Relop (ty, LtU, e1, e2) -> Relop (ty, LeU, e2, e1)
×
298
    | Relop (ty, Le, e1, e2) -> Relop (ty, Lt, e2, e1)
×
299
    | Relop (ty, LeU, e1, e2) -> Relop (ty, LtU, e2, e1)
×
300
    | _ -> Fmt.failwith "negate_relop: not a relop."
×
301
  in
302
  make e
303

304
let rec unop ty op hte =
305
  match (op, view hte) with
116✔
306
  | Ty.Unop.(Regexp_loop _ | Regexp_star), _ -> raw_unop ty op hte
×
307
  | _, Val v -> value (Eval.unop ty op v)
27✔
308
  | Not, Unop (_, Not, hte') -> hte'
2✔
309
  | Neg, Unop (_, Neg, hte') -> hte'
1✔
310
  | Not, Binop (inner_ty, Or, a, b) ->
3✔
311
    make (Binop (inner_ty, And, unop ty Not a, unop ty Not b))
3✔
312
  | Not, Relop (Ty_fp _, _, _, _) -> raw_unop ty op hte
2✔
313
  | Not, Relop (_, _, _, _) -> negate_relop hte
6✔
314
  | Trim, Cvtop (Ty_real, ToString, _) -> hte
×
315
  | Head, List (hd :: _) -> hd
1✔
316
  | Tail, List (_ :: tl) -> make (List tl)
1✔
317
  | Reverse, List es -> make (List (List.rev es))
2✔
318
  | Length, List es -> value (Int (List.length es))
1✔
319
  | _ -> raw_unop ty op hte
70✔
320

321
let raw_binop ty op hte1 hte2 = make (Binop (ty, op, hte1, hte2)) [@@inline]
780✔
322

323
let rec binop ty op hte1 hte2 =
324
  match (op, view hte1, view hte2) with
803✔
325
  | Ty.Binop.(String_in_re | Regexp_range), _, _ -> raw_binop ty op hte1 hte2
×
326
  | op, Val v1, Val v2 -> value (Eval.binop ty op v1 v2)
70✔
327
  | Sub, Ptr { base = b1; offset = os1 }, Ptr { base = b2; offset = os2 } ->
1✔
328
    if Bitvector.equal b1 b2 then binop ty Sub os1 os2
1✔
329
    else raw_binop ty op hte1 hte2
×
330
  | Add, Ptr { base; offset }, _ ->
2✔
331
    let m = Bitvector.numbits base in
332
    make (Ptr { base; offset = binop (Ty_bitv m) Add offset hte2 })
2✔
333
  | Sub, Ptr { base; offset }, _ ->
1✔
334
    let m = Bitvector.numbits base in
335
    make (Ptr { base; offset = binop (Ty_bitv m) Sub offset hte2 })
1✔
336
  | Rem, Ptr { base; offset }, _ ->
1✔
337
    let m = Bitvector.numbits base in
338
    let rhs = value (Bitv base) in
1✔
339
    let addr = binop (Ty_bitv m) Add rhs offset in
1✔
340
    binop ty Rem addr hte2
1✔
341
  | Add, _, Ptr { base; offset } ->
1✔
342
    let m = Bitvector.numbits base in
343
    make (Ptr { base; offset = binop (Ty_bitv m) Add offset hte1 })
1✔
344
  | Sub, _, Ptr { base; offset } ->
×
345
    let m = Bitvector.numbits base in
346
    let base = value (Bitv base) in
×
347
    binop ty Sub hte1 (binop (Ty_bitv m) Add base offset)
×
348
  | (Add | Or), Val (Bitv bv), _ when Bitvector.eqz bv -> hte2
×
349
  | (And | Div | DivU | Mul | Rem | RemU), Val (Bitv bv), _
×
350
    when Bitvector.eqz bv ->
4✔
351
    hte1
1✔
352
  | (Add | Or), _, Val (Bitv bv) when Bitvector.eqz bv -> hte1
×
353
  | Add, _, Val (Real -0.) -> hte1
×
354
  | Add, Val (Real -0.), _ -> hte2
×
355
  | Add, _, Val (Num (F32 -0l)) -> hte1
1✔
356
  | Add, Val (Num (F32 -0l)), _ -> hte2
×
357
  | Add, _, Val (Num (F64 -0L)) -> hte1
×
358
  | Add, Val (Num (F64 -0L)), _ -> hte2
×
359
  | (And | Mul), _, Val (Bitv bv) when Bitvector.eqz bv -> hte2
1✔
360
  | And, Val True, _ -> hte2
×
361
  | And, _, Val True -> hte1
1✔
362
  | And, Val False, _ -> hte1
1✔
363
  | And, _, Val False -> hte2
×
364
  | Or, Val True, _ -> hte1
×
365
  | Or, _, Val True -> hte2
1✔
366
  | Or, Val False, _ -> hte2
×
367
  | Or, _, Val False -> hte1
1✔
368
  | Add, Binop (ty, Add, x, { node = Val v1; _ }), Val v2 ->
1✔
369
    let v = value (Eval.binop ty Add v1 v2) in
1✔
370
    raw_binop ty Add x v
1✔
371
  | Sub, Binop (ty, Sub, x, { node = Val v1; _ }), Val v2 ->
1✔
372
    let v = value (Eval.binop ty Add v1 v2) in
1✔
373
    raw_binop ty Sub x v
1✔
374
  | Mul, Val (Bitv bv), _ when Bitvector.eq_one bv -> hte2
×
375
  | Mul, _, Val (Bitv bv) when Bitvector.eq_one bv -> hte1
×
376
  | Mul, Binop (ty, Mul, x, { node = Val v1; _ }), Val v2 ->
1✔
377
    let v = value (Eval.binop ty Mul v1 v2) in
1✔
378
    raw_binop ty Mul x v
1✔
379
  | Add, Val v1, Binop (ty, Add, x, { node = Val v2; _ }) ->
1✔
380
    let v = value (Eval.binop ty Add v1 v2) in
1✔
381
    raw_binop ty Add v x
1✔
382
  | Mul, Val v1, Binop (ty, Mul, x, { node = Val v2; _ }) ->
1✔
383
    let v = value (Eval.binop ty Mul v1 v2) in
1✔
384
    raw_binop ty Mul v x
1✔
385
  | At, List es, Val (Int n) ->
1✔
386
    (* TODO: use another datastructure? *)
387
    begin match List.nth_opt es n with None -> assert false | Some v -> v
1✔
388
    end
389
  | (String_contains | String_prefix | String_suffix), _, Val (Str "") ->
1✔
390
    value True
391
  | List_cons, _, List es -> make (List (hte1 :: es))
1✔
392
  | List_append, List _, (List [] | Val (List [])) -> hte1
×
393
  | List_append, (List [] | Val (List [])), List _ -> hte2
×
394
  | List_append, List l0, Val (List l1) -> make (List (l0 @ List.map value l1))
1✔
395
  | List_append, Val (List l0), List l1 -> make (List (List.map value l0 @ l1))
×
396
  | List_append, List l0, List l1 -> make (List (l0 @ l1))
×
397
  | _ -> raw_binop ty op hte1 hte2
707✔
398

399
let raw_triop ty op e1 e2 e3 = make (Triop (ty, op, e1, e2, e3)) [@@inline]
24✔
400

401
let triop ty op e1 e2 e3 =
402
  match (op, view e1, view e2, view e3) with
31✔
403
  | Ty.Triop.Ite, Val True, _, _ -> e2
1✔
404
  | Ite, Val False, _, _ -> e3
1✔
405
  | Ite, _, _, _ when equal e2 e3 -> e2
1✔
406
  | op, Val v1, Val v2, Val v3 -> value (Eval.triop ty op v1 v2 v3)
4✔
407
  | Ite, _, Triop (_, Ite, c2, r1, r2), Triop (_, Ite, _, _, _) ->
×
408
    let else_ = raw_triop ty Ite e1 r2 e3 in
409
    let cond = binop Ty_bool And e1 c2 in
×
410
    raw_triop ty Ite cond r1 else_
×
411
  | _ -> raw_triop ty op e1 e2 e3
24✔
412

413
let raw_relop ty op hte1 hte2 = make (Relop (ty, op, hte1, hte2)) [@@inline]
495✔
414

415
let rec relop ty (op : Ty.Relop.t) hte1 hte2 =
416
  let both_phys_eq = phys_equal hte1 hte2 in
514✔
417
  let can_be_shortcuted =
514✔
418
    match ty with
419
    | Ty.Ty_bool | Ty_bitv _ | Ty_int | Ty_unit -> both_phys_eq
×
420
    | Ty_fp _ | Ty_app | Ty_list | Ty_real | Ty_regexp | Ty_roundingMode
×
421
    | Ty_none | Ty_str ->
×
422
      false
423
  in
424
  match (op, view hte1, view hte2) with
514✔
425
  | (Eq | Le | LeU), _, _ when can_be_shortcuted -> value True
6✔
426
  | (Ne | Lt | LtU), _, _ when can_be_shortcuted -> value False
6✔
427
  | op, Val v1, Val v2 -> value (if Eval.relop ty op v1 v2 then True else False)
21✔
428
  | Ne, Val (Real v), _ | Ne, _, Val (Real v) ->
×
429
    if Float.is_nan v || Float.is_infinite v then value True
×
430
    else if both_phys_eq then value False
×
431
    else raw_relop ty op hte1 hte2
×
432
  | _, Val (Real v), _ | _, _, Val (Real v) ->
×
433
    if Float.is_nan v || Float.is_infinite v then value False
×
434
    else
435
      (* TODO: it is possible to add a shortcut when `both_phys_eq` *)
436
      raw_relop ty op hte1 hte2
4✔
437
  | Eq, _, Val Nothing | Eq, Val Nothing, _ -> value False
×
438
  | Ne, _, Val Nothing | Ne, Val Nothing, _ -> value True
×
439
  | Eq, _, Val (App (`Op "symbol", [ Str _ ]))
×
440
  | Eq, Val (App (`Op "symbol", [ Str _ ])), _ ->
×
441
    value False
442
  | Ne, _, Val (App (`Op "symbol", [ Str _ ]))
×
443
  | Ne, Val (App (`Op "symbol", [ Str _ ])), _ ->
×
444
    value True
445
  | ( Eq
×
446
    , Symbol ({ ty = Ty_fp prec1; _ } as s1)
447
    , Symbol ({ ty = Ty_fp prec2; _ } as s2) )
448
    when both_phys_eq || (prec1 = prec2 && Symbol.equal s1 s2) ->
×
449
    raw_unop Ty_bool Not (raw_unop (Ty_fp prec1) Is_nan hte1)
×
450
  | Eq, Ptr { base = b1; offset = os1 }, Ptr { base = b2; offset = os2 } ->
1✔
451
    if both_phys_eq then value True
×
452
    else if Bitvector.equal b1 b2 then relop Ty_bool Eq os1 os2
×
453
    else value False
1✔
454
  | Ne, Ptr { base = b1; offset = os1 }, Ptr { base = b2; offset = os2 } ->
1✔
455
    if both_phys_eq then value False
×
456
    else if Bitvector.equal b1 b2 then relop Ty_bool Ne os1 os2
×
457
    else value True
1✔
458
  | ( (LtU | LeU)
1✔
459
    , Ptr { base = b1; offset = os1 }
460
    , Ptr { base = b2; offset = os2 } ) ->
461
    if both_phys_eq then value True
×
462
    else if Bitvector.equal b1 b2 then relop ty op os1 os2
×
463
    else
464
      let b1 = Value.Bitv b1 in
2✔
465
      let b2 = Value.Bitv b2 in
466
      value (if Eval.relop ty op b1 b2 then True else False)
1✔
467
  | ( op
2✔
468
    , Val (Bitv _ as n)
469
    , Ptr { base; offset = { node = Val (Bitv _ as o); _ } } ) ->
470
    let base = Eval.binop (Ty_bitv 32) Add (Bitv base) o in
471
    value (if Eval.relop ty op n base then True else False)
×
472
  | op, Ptr { base; offset = { node = Val (Bitv _ as o); _ } }, Val (Bitv _ as n)
2✔
473
    ->
474
    let base = Eval.binop (Ty_bitv 32) Add (Bitv base) o in
475
    value (if Eval.relop ty op base n then True else False)
×
476
  | op, List l1, List l2 -> relop_list op l1 l2
×
477
  | _, _, _ -> raw_relop ty op hte1 hte2
415✔
478

479
and relop_list op l1 l2 =
480
  match (op, l1, l2) with
×
481
  | Eq, [], [] -> value True
×
482
  | Eq, _, [] | Eq, [], _ -> value False
×
483
  | Eq, l1, l2 ->
×
484
    if not (List.compare_lengths l1 l2 = 0) then value False
×
485
    else
486
      List.fold_left2
×
487
        (fun acc a b ->
488
          binop Ty_bool And acc
×
489
          @@
490
          match (ty a, ty b) with
×
491
          | Ty_real, Ty_real -> relop Ty_real Eq a b
×
492
          | _ -> relop Ty_bool Eq a b )
×
493
        (value True) l1 l2
×
494
  | Ne, _, _ -> unop Ty_bool Not @@ relop_list Eq l1 l2
×
495
  | (Lt | LtU | Le | LeU), _, _ -> assert false
496

497
let raw_cvtop ty op hte = make (Cvtop (ty, op, hte)) [@@inline]
51✔
498

499
let rec cvtop theory op hte =
500
  match (op, view hte) with
75✔
501
  | Ty.Cvtop.String_to_re, _ -> raw_cvtop theory op hte
×
502
  | _, Val v -> value (Eval.cvtop theory op v)
32✔
503
  | ToBool, Cvtop (_, OfBool, hte) -> hte
1✔
504
  | OfBool, Cvtop (_, ToBool, hte) -> hte
×
505
  | String_to_float, Cvtop (Ty_real, ToString, hte) -> hte
×
506
  | Reinterpret_float, Cvtop (Ty_real, Reinterpret_int, e1) -> e1
×
507
  | Reinterpret_float, Cvtop (Ty_fp n, Reinterpret_int, e) ->
×
508
    assert (match theory with Ty_bitv m -> n = m | _ -> false);
×
509
    e
510
  | Reinterpret_int, Cvtop (Ty_int, Reinterpret_float, e1) -> e1
×
511
  | Reinterpret_int, Cvtop (Ty_bitv m, Reinterpret_float, e) ->
9✔
512
    assert (match theory with Ty_fp n -> n = m | _ -> false);
×
513
    e
514
  | Zero_extend n, Ptr { base; offset } ->
1✔
515
    let offset = cvtop theory op offset in
516
    make (Ptr { base = Bitvector.zero_extend n base; offset })
1✔
517
  | WrapI64, Ptr { base; offset } ->
1✔
518
    let offset = cvtop theory op offset in
519
    make (Ptr { base = Bitvector.extract base ~high:31 ~low:0; offset })
1✔
520
  | WrapI64, Cvtop (Ty_bitv 64, Zero_extend 32, hte) ->
×
521
    assert (Ty.equal theory (ty hte) && Ty.equal theory (Ty_bitv 32));
×
522
    hte
523
  | ToString, Cvtop (_, OfString, e1) -> e1
1✔
524
  | OfString, Cvtop (_, ToString, e1) -> e1
×
525
  | PromoteF32, Cvtop (_, DemoteF64, e1) -> e1
×
526
  | DemoteF64, Cvtop (_, PromoteF32, e1) -> e1
×
527
  | Zero_extend 0, _ -> hte
1✔
528
  | Sign_extend 0, _ -> hte
×
529
  | String_from_code, Cvtop (_, String_to_code, e1) -> e1
3✔
530
  | String_to_code, Cvtop (_, String_from_code, e1) -> e1
1✔
531
  | _ -> raw_cvtop theory op hte
25✔
532

533
let raw_naryop ty op es = make (Naryop (ty, op, es)) [@@inline]
8✔
534

535
let naryop ty op hexps =
536
  (* Get list of value or exit early *)
537
  let rec extract_values acc = function
8✔
538
    | [] -> Some (List.rev acc)
7✔
539
    | hexp :: remaining ->
19✔
540
      begin match view hexp with
541
      | Val v -> extract_values (v :: acc) remaining
18✔
542
      | _ -> None
1✔
543
      end
544
  in
545

546
  match extract_values [] hexps with
547
  | Some vlist -> value (Eval.naryop ty op vlist)
7✔
548
  | None ->
1✔
549
    begin match (ty, op) with
550
    | Ty_str, Concat ->
×
551
      let rec concat_exprs acc = function
552
        | [] -> List.rev acc
×
553
        | hexp :: remainig ->
×
554
          let acc =
555
            match view hexp with
556
            | Naryop (Ty_str, Concat, inner_hexps) ->
×
557
              List.rev_append inner_hexps acc
×
558
            | _ -> hexp :: acc
×
559
          in
560
          concat_exprs acc remainig
561
      in
562
      raw_naryop ty op (concat_exprs [] hexps)
×
563
    | _ -> raw_naryop ty op hexps
1✔
564
    end
565

566
let[@inline] raw_extract (hte : t) ~(high : int) ~(low : int) : t =
567
  make (Extract (hte, high, low))
13✔
568

569
let extract (hte : t) ~(high : int) ~(low : int) : t =
570
  match (view hte, high, low) with
36✔
571
  | Val (Bitv bv), high, low -> value (Bitv (Bitvector.extract bv ~high ~low))
26✔
572
  | ( Cvtop
2✔
573
        ( _
574
        , (Zero_extend 24 | Sign_extend 24)
1✔
575
        , ({ node = Symbol { ty = Ty_bitv 8; _ }; _ } as sym) )
576
    , 7
577
    , 0 ) ->
578
    sym
579
  | Concat (_, e), h, l when l = 0 && Ty.bitsize (ty e) = h - l + 1 -> e
2✔
580
  | Concat (e, _), 63, 32 when Ty.bitsize (ty e) = 32 -> e
×
581
  | _ ->
6✔
582
    if high - low + 1 = Ty.bitsize (ty hte) then hte
×
583
    else raw_extract hte ~high ~low
6✔
584

585
let raw_concat (msb : t) (lsb : t) : t = make (Concat (msb, lsb)) [@@inline]
4✔
586

587
(* TODO: don't rebuild so many values it generates unecessary hc lookups *)
588
let rec concat (msb : t) (lsb : t) : t =
589
  match (view msb, view lsb) with
7✔
590
  | Val (Bitv a), Val (Bitv b) -> value (Bitv (Bitvector.concat a b))
1✔
591
  | Val (Bitv _), Concat (({ node = Val (Bitv _); _ } as b), se) ->
×
592
    raw_concat (concat msb b) se
×
593
  | Extract (s1, h, m1), Extract (s2, m2, l) when equal s1 s2 && m1 = m2 + 1 ->
3✔
594
    if h - l + 1 = Ty.bitsize (ty s1) then s1 else raw_extract s1 ~high:h ~low:l
1✔
595
  | Extract (_, _, _), Concat (({ node = Extract (_, _, _); _ } as e2), e3) ->
×
596
    raw_concat (concat msb e2) e3
×
597
  | _ -> raw_concat msb lsb
3✔
598

599
let rec simplify_expr ?(in_relop = false) (hte : t) : t =
4✔
600
  match view hte with
16✔
601
  | Val _ | Symbol _ -> hte
4✔
602
  | Ptr { base; offset } ->
×
603
    let offset = simplify_expr ~in_relop offset in
604
    if not in_relop then make (Ptr { base; offset })
×
605
    else binop (Ty_bitv 32) Add (value (Bitv base)) offset
×
606
  | List es -> make @@ List (List.map (simplify_expr ~in_relop) es)
×
607
  | App (x, es) -> make @@ App (x, List.map (simplify_expr ~in_relop) es)
×
608
  | Unop (ty, op, e) ->
×
609
    let e = simplify_expr ~in_relop e in
610
    unop ty op e
×
611
  | Binop (ty, op, e1, e2) ->
6✔
612
    let e1 = simplify_expr ~in_relop e1 in
613
    let e2 = simplify_expr ~in_relop e2 in
6✔
614
    binop ty op e1 e2
6✔
615
  | Relop (ty, op, e1, e2) ->
×
616
    let e1 = simplify_expr ~in_relop:true e1 in
617
    let e2 = simplify_expr ~in_relop:true e2 in
×
618
    relop ty op e1 e2
×
619
  | Triop (ty, op, c, e1, e2) ->
×
620
    let c = simplify_expr ~in_relop c in
621
    let e1 = simplify_expr ~in_relop e1 in
×
622
    let e2 = simplify_expr ~in_relop e2 in
×
623
    triop ty op c e1 e2
×
624
  | Cvtop (ty, op, e) ->
×
625
    let e = simplify_expr ~in_relop e in
626
    cvtop ty op e
×
627
  | Naryop (ty, op, es) ->
×
628
    let es = List.map (simplify_expr ~in_relop) es in
629
    naryop ty op es
×
630
  | Extract (s, high, low) ->
×
631
    let s = simplify_expr ~in_relop s in
632
    extract s ~high ~low
×
633
  | Concat (e1, e2) ->
×
634
    let msb = simplify_expr ~in_relop e1 in
635
    let lsb = simplify_expr ~in_relop e2 in
×
636
    concat msb lsb
×
637
  | Binder _ ->
×
638
    (* Not simplifying anything atm *)
639
    hte
640

641
module Cache = Hashtbl.Make (struct
642
  type nonrec t = t
643

644
  let hash = hash
645

646
  let equal = equal
647
end)
648

649
let simplify =
650
  (* TODO: it may make sense to share the cache with simplify_expr ? *)
651
  let cache = Cache.create 512 in
652
  fun e ->
50✔
653
    match Cache.find_opt cache e with
2✔
654
    | Some simplified -> simplified
×
655
    | None ->
2✔
656
      let rec loop x =
657
        let x' = simplify_expr x in
4✔
658
        if equal x x' then begin
2✔
659
          Cache.add cache e x';
660
          x'
2✔
661
        end
662
        else loop x'
2✔
663
      in
664
      loop e
665

666
module Bool = struct
667
  open Ty
668

669
  let of_val = function
670
    | Val True -> Some true
×
671
    | Val False -> Some false
×
672
    | _ -> None
126✔
673

674
  let true_ = value True
50✔
675

676
  let false_ = value False
50✔
677

678
  let to_val b = if b then true_ else false_
×
679

680
  let v b = to_val b [@@inline]
×
681

682
  let not b =
683
    let bexpr = view b in
×
684
    match of_val bexpr with
×
685
    | Some b -> to_val (not b)
×
686
    | None -> (
×
687
      match bexpr with
688
      | Unop (Ty_bool, Not, cond) -> cond
×
689
      | _ -> unop Ty_bool Not b )
×
690

691
  let equal b1 b2 =
692
    match (view b1, view b2) with
×
693
    | Val True, Val True | Val False, Val False -> true_
×
694
    | _ -> relop Ty_bool Eq b1 b2
×
695

696
  let distinct b1 b2 =
697
    match (view b1, view b2) with
×
698
    | Val True, Val False | Val False, Val True -> true_
×
699
    | _ -> relop Ty_bool Ne b1 b2
×
700

701
  let and_ b1 b2 =
702
    match (of_val (view b1), of_val (view b2)) with
63✔
703
    | Some true, _ -> b2
×
704
    | _, Some true -> b1
×
705
    | Some false, _ | _, Some false -> false_
×
706
    | _ -> binop Ty_bool And b1 b2
63✔
707

708
  let or_ b1 b2 =
709
    match (of_val (view b1), of_val (view b2)) with
×
710
    | Some false, _ -> b2
×
711
    | _, Some false -> b1
×
712
    | Some true, _ | _, Some true -> true_
×
713
    | _ -> binop Ty_bool Or b1 b2
×
714

715
  let implies a b = binop Ty_bool Implies a b
×
716

717
  let ite c r1 r2 = triop Ty_bool Ite c r1 r2
×
718
end
719

720
module Smtlib = struct
721
  let rec pp fmt (hte : t) =
722
    match view hte with
13✔
723
    | Val v -> Value.Smtlib.pp fmt v
3✔
724
    | Ptr _ -> assert false
725
    | Symbol s -> Fmt.pf fmt "@[<hov 1>%a@]" Symbol.pp s
5✔
726
    | List _ -> assert false
727
    | App _ -> assert false
728
    | Unop (ty, op, e) ->
×
729
      Fmt.pf fmt "@[<hov 1>(%a@ %a)@]" Ty.Smtlib.pp_unop (ty, op) pp e
730
    | Binop (ty, op, e1, e2) ->
2✔
731
      Fmt.pf fmt "@[<hov 1>(%a@ %a@ %a)@]" Ty.Smtlib.pp_binop (ty, op) pp e1 pp
732
        e2
733
    | Triop _ -> assert false
734
    | Relop (ty, op, e1, e2) ->
3✔
735
      Fmt.pf fmt "@[<hov 1>(%a@ %a@ %a)@]" Ty.Smtlib.pp_relop (ty, op) pp e1 pp
736
        e2
737
    | Cvtop _ -> assert false
738
    | Naryop _ -> assert false
739
    | Extract _ -> assert false
740
    | Concat _ -> assert false
741
    | Binder _ -> assert false
742
end
743

744
let inline_symbol_values map e =
745
  let rec aux e =
2✔
746
    match view e with
2✔
747
    | Val _ -> e
×
748
    | Symbol symbol ->
2✔
749
      begin match Symbol.Map.find_opt symbol map with
750
      | None -> e
1✔
751
      | Some v -> value v
1✔
752
      end
753
    | Ptr e ->
×
754
      let offset = aux e.offset in
755
      make @@ Ptr { e with offset }
×
756
    | List vs ->
×
757
      let vs = List.map aux vs in
758
      list vs
×
759
    | App (x, vs) ->
×
760
      let vs = List.map aux vs in
761
      app x vs
×
762
    | Unop (ty, op, v) ->
×
763
      let v = aux v in
764
      unop ty op v
×
765
    | Binop (ty, op, v1, v2) ->
×
766
      let v1 = aux v1 in
767
      let v2 = aux v2 in
×
768
      binop ty op v1 v2
×
769
    | Triop (ty, op, v1, v2, v3) ->
×
770
      let v1 = aux v1 in
771
      let v2 = aux v2 in
×
772
      let v3 = aux v3 in
×
773
      triop ty op v1 v2 v3
×
774
    | Cvtop (ty, op, v) ->
×
775
      let v = aux v in
776
      cvtop ty op v
×
777
    | Relop (ty, op, v1, v2) ->
×
778
      let v1 = aux v1 in
779
      let v2 = aux v2 in
×
780
      relop ty op v1 v2
×
781
    | Naryop (ty, op, vs) ->
×
782
      let vs = List.map aux vs in
783
      naryop ty op vs
×
784
    | Extract (e, high, low) ->
×
785
      let e = aux e in
786
      extract e ~high ~low
×
787
    | Concat (e1, e2) ->
×
788
      let e1 = aux e1 in
789
      let e2 = aux e2 in
×
790
      concat e1 e2
×
791
    | Binder (b, vars, e) ->
×
792
      let e = aux e in
793
      binder b vars e
×
794
  in
795
  aux e
796

797
module Set = struct
798
  include Set.Make (Key)
799

800
  type key = Key.t
801

802
  let hash = Hashtbl.hash
803

804
  let to_list s = elements s
2✔
805

806
  let pp fmt v =
807
    Fmt.pf fmt "@[<hov 1>%a@]"
×
808
      (Fmt.iter iter ~sep:(fun fmt () -> Fmt.pf fmt "@;") pp)
×
809
      v
810

811
  let get_symbols (set : t) =
812
    fold (fun x acc -> get_symbols_aux Fun.id acc x) set []
×
813
    |> List.sort_uniq Symbol.compare
×
814

815
  let map f set =
816
    fold
×
817
      (fun elt set ->
818
        let elt = f elt in
×
819
        add elt set )
×
820
      set empty
821

822
  let inline_symbol_values symbol_map set =
823
    map (inline_symbol_values symbol_map) set
×
824
end
825

826
let rec split_conjunctions (e : t) : Set.t =
827
  match view e with
×
828
  | Binop (Ty_bool, And, e1, e2) ->
×
829
    let s1 = split_conjunctions e1 in
830
    let s2 = split_conjunctions e2 in
×
831
    Set.union s1 s2
×
832
  | _ -> Set.singleton e
×
833

834
let rec split_disjunctions (e : t) : Set.t =
835
  match view e with
×
836
  | Binop (Ty_bool, Or, e1, e2) ->
×
837
    let s1 = split_disjunctions e1 in
838
    let s2 = split_disjunctions e2 in
×
839
    Set.union s1 s2
×
840
  | _ -> Set.singleton e
×
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc