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

formalsec / smtml / 529

27 Apr 2026 11:42AM UTC coverage: 48.244% (-0.05%) from 48.293%
529

push

github

filipeom
Disable implicit transitive deps

0 of 3 new or added lines in 1 file covered. (0.0%)

2 existing lines in 1 file now uncovered.

2417 of 5010 relevant lines covered (48.24%)

41.17 hits per line

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

46.78
/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,924✔
33
    | Val v1, Val v2 -> Value.equal v1 v2
1,000✔
34
    | Ptr { base = b1; offset = o1 }, Ptr { base = b2; offset = o2 } ->
5✔
35
      Bitvector.equal b1 b2 && phys_equal o1 o2
5✔
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) ->
41✔
40
      Ty.equal t1 t2 && Ty.Unop.equal op1 op2 && phys_equal e1 e2
41✔
41
    | Binop (t1, op1, e1, e3), Binop (t2, op2, e2, e4) ->
118✔
42
      Ty.equal t1 t2 && Ty.Binop.equal op1 op2 && phys_equal e1 e2
104✔
43
      && phys_equal e3 e4
96✔
44
    | Relop (t1, op1, e1, e3), Relop (t2, op2, e2, e4) ->
56✔
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) ->
23✔
51
      Ty.equal t1 t2 && Ty.Cvtop.equal op1 op2 && phys_equal e1 e2
23✔
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✔
UNCOV
54
    | Extract (e1, h1, l1), Extract (e2, h2, l2) ->
×
UNCOV
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,189✔
66

67
  let hash (e : expr) : int =
68
    match e with
7,714✔
69
    | Val v -> Value.hash v
3,541✔
70
    | Ptr { base; offset } -> combine (Bitvector.hash base) offset.tag
21✔
71
    | Symbol s -> Symbol.hash s
1,318✔
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) ->
159✔
77
      let h1 = Ty.hash ty in
78
      let h2 = combine h1 (Ty.Unop.hash op) in
159✔
79
      combine h2 e.tag
159✔
80
    | Binop (ty, op, e1, e2) ->
1,462✔
81
      let h = Ty.hash ty in
82
      let h = combine h (Ty.Binop.hash op) in
1,462✔
83
      let h = combine h e1.tag in
1,462✔
84
      combine h e2.tag
1,462✔
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) ->
75✔
97
      let h = Ty.hash ty in
98
      let h = combine h (Ty.Cvtop.hash op) in
75✔
99
      combine h e.tag
75✔
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,656✔
130

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

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

135
let symbol s = make (Symbol s)
861✔
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 acc (hte : t) =
184
  match view hte with
13✔
185
  | Val _ -> acc
3✔
186
  | Ptr { offset; _ } -> get_symbols_aux acc offset
×
187
  | Symbol s -> s :: acc
5✔
188
  | List es | App (_, es) | Naryop (_, _, es) ->
×
189
    List.fold_left get_symbols_aux acc es
190
  | Unop (_, _, e) | Cvtop (_, _, e) | Extract (e, _, _) ->
×
191
    get_symbols_aux acc e
192
  | Binop (_, _, e1, e2) | Relop (_, _, e1, e2) | Concat (e1, e2) ->
×
193
    let acc = get_symbols_aux acc e1 in
194
    get_symbols_aux acc e2
5✔
195
  | Triop (_, _, e1, e2, e3) ->
×
196
    let acc = get_symbols_aux acc e1 in
197
    let acc = get_symbols_aux acc e2 in
×
198
    get_symbols_aux acc e3
×
199
  | Binder (_, vars, e) ->
×
200
    let acc = List.fold_left get_symbols_aux acc vars in
201
    get_symbols_aux acc e
×
202

203
let get_symbols (hte : t list) =
204
  List.fold_left get_symbols_aux [] hte |> List.sort_uniq Symbol.compare
1✔
205

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

249
let pp fmt e = pp_with ~printer:Without_type fmt e
×
250

251
let pp_safe fmt e = pp_with ~printer:With_type_and_hexa_float fmt e
×
252

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

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

273
let to_string e = Fmt.str "%a" pp e
×
274

275
let value (v : Value.t) : t = make (Val v) [@@inline]
2,260✔
276

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

279
let list l = make (List l)
5✔
280

281
let app symbol args = make (App (symbol, args))
20✔
282

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

285
let let_in vars body = binder Let_in vars body
8✔
286

287
let forall vars body = binder Forall vars body
2✔
288

289
let exists vars body = binder Exists vars body
1✔
290

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

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

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

323
let raw_binop ty op hte1 hte2 = make (Binop (ty, op, hte1, hte2)) [@@inline]
776✔
324

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

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

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

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

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

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

499
let raw_cvtop ty op hte = make (Cvtop (ty, op, hte)) [@@inline]
49✔
500

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

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

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

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

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

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

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

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

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

643
module Cache = Hashtbl.Make (struct
644
  type nonrec t = t
645

646
  let hash = hash
647

648
  let equal = equal
649
end)
650

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

668
module Bool = struct
669
  open Ty
670

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

676
  let true_ = value True
50✔
677

678
  let false_ = value False
50✔
679

680
  let to_val b = if b then true_ else false_
×
681

682
  let v b = to_val b [@@inline]
×
683

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

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

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

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

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

717
  let implies a b = binop Ty_bool Implies a b
×
718

719
  let ite c r1 r2 = triop Ty_bool Ite c r1 r2
×
720
end
721

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

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

799
module Set = struct
800
  include Set.Make (Key)
801

802
  type key = Key.t
803

804
  let hash = Hashtbl.hash
805

806
  let to_list s = elements s
2✔
807

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

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

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

824
  let inline_symbol_values symbol_map set =
825
    map (inline_symbol_values symbol_map) set
×
826
end
827

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

836
let rec split_disjunctions (e : t) : Set.t =
837
  match view e with
×
838
  | Binop (Ty_bool, Or, e1, e2) ->
×
839
    let s1 = split_disjunctions e1 in
840
    let s2 = split_disjunctions e2 in
×
841
    Set.union s1 s2
×
842
  | _ -> 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