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

formalsec / smtml / 548

10 Jun 2026 07:16AM UTC coverage: 46.175% (+0.5%) from 45.654%
548

push

github

filipeom
Add expr printer, fix expr parser, and remove mutable state in parser

10 of 10 new or added lines in 2 files covered. (100.0%)

49 existing lines in 2 files now uncovered.

2348 of 5085 relevant lines covered (46.18%)

40.47 hits per line

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

50.1
/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,967✔
33
    | Val v1, Val v2 -> Value.equal v1 v2
1,012✔
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
427✔
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) ->
122✔
42
      Ty.equal t1 t2 && Ty.Binop.equal op1 op2 && phys_equal e1 e2
107✔
43
      && phys_equal e3 e4
99✔
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✔
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,232✔
66

67
  let hash (e : expr) : int =
68
    match e with
7,735✔
69
    | Val v -> Value.hash v
3,535✔
70
    | Ptr { base; offset } -> combine (Bitvector.hash base) offset.tag
21✔
71
    | Symbol s -> Symbol.hash s
1,330✔
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,475✔
81
      let h = Ty.hash ty in
82
      let h = combine h (Ty.Binop.hash op) in
1,475✔
83
      let h = combine h e1.tag in
1,475✔
84
      combine h e2.tag
1,475✔
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]
251✔
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,678✔
130

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

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

135
let symbol s = make (Symbol s)
871✔
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
10✔
185
  | hd :: tl ->
19✔
186
    get_symbols_aux (fun acc -> get_symbols_aux_loop k acc tl) acc hd
19✔
187

188
and get_symbols_aux k acc (hte : t) =
189
  match view hte with
19✔
190
  | Val _ -> k acc
3✔
191
  | Ptr { offset; _ } -> get_symbols_aux k acc offset
×
192
  | Symbol s -> k (s :: acc)
9✔
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
3✔
203

204
let rec pp_with ~printer fmt (hte : t) =
205
  match view hte with
6✔
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
4✔
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) ->
2✔
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
2✔
250

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

253
module Printer = struct
254
  let pp_symbols fmt syms =
255
    Fmt.list ~sep:Fmt.cut
2✔
256
      (fun fmt sym ->
257
        let t = Symbol.type_of sym in
4✔
258
        Fmt.pf fmt "(let-const %a %a)" Symbol.pp sym Ty.pp t )
4✔
259
      fmt syms
260

261
  let pp_expr fmt e =
262
    let syms = get_symbols [ e ] in
1✔
263
    if List.length syms > 0 then Fmt.pf fmt "@[<v>%a@]@\n" pp_symbols syms;
1✔
264
    pp_safe fmt e
1✔
265

266
  let pp_query fmt es =
267
    let pp_asserts fmt es =
1✔
268
      Fmt.list ~sep:Fmt.cut
1✔
269
        (fun fmt e -> Fmt.pf fmt "(assert @[<h 2>%a@])" pp_safe e)
1✔
270
        fmt es
271
    in
272
    let syms = get_symbols es in
273
    if List.length syms > 0 then Fmt.pf fmt "@[<v>%a@]@\n" pp_symbols syms;
1✔
274
    if List.length es > 0 then Fmt.pf fmt "@[<v>%a@]@\n" pp_asserts es;
1✔
275
    Fmt.string fmt "(check-sat)"
1✔
276
end
277

278
let pp_smtml = Printer.pp_query
279

280
let to_string e = Fmt.str "%a" pp e
×
281

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

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

286
let list l = make (List l)
5✔
287

288
let app symbol args = make (App (symbol, args))
20✔
289

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

292
let let_in vars body = binder Let_in vars body
8✔
293

294
let forall vars body = binder Forall vars body
2✔
295

296
let exists vars body = binder Exists vars body
1✔
297

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

300
let negate_relop (hte : t) : t =
301
  let e =
6✔
302
    match view hte with
303
    | Relop (ty, Eq, e1, e2) -> Relop (ty, Ne, e1, e2)
4✔
304
    | Relop (ty, Ne, e1, e2) -> Relop (ty, Eq, e1, e2)
×
305
    | Relop (ty, Lt, e1, e2) -> Relop (ty, Le, e2, e1)
2✔
306
    | Relop (ty, LtU, e1, e2) -> Relop (ty, LeU, e2, e1)
×
307
    | Relop (ty, Le, e1, e2) -> Relop (ty, Lt, e2, e1)
×
308
    | Relop (ty, LeU, e1, e2) -> Relop (ty, LtU, e2, e1)
×
309
    | _ -> Fmt.failwith "negate_relop: not a relop."
×
310
  in
311
  make e
312

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

330
let raw_binop ty op hte1 hte2 = make (Binop (ty, op, hte1, hte2)) [@@inline]
784✔
331

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

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

410
let triop ty op e1 e2 e3 =
411
  match (op, view e1, view e2, view e3) with
31✔
412
  | Ty.Triop.Ite, Val True, _, _ -> e2
1✔
413
  | Ite, Val False, _, _ -> e3
1✔
414
  | Ite, _, _, _ when equal e2 e3 -> e2
1✔
415
  | op, Val v1, Val v2, Val v3 -> value (Eval.triop ty op v1 v2 v3)
4✔
416
  | Ite, _, Triop (_, Ite, c2, r1, r2), Triop (_, Ite, _, _, _) ->
×
417
    let else_ = raw_triop ty Ite e1 r2 e3 in
418
    let cond = binop Ty_bool And e1 c2 in
×
419
    raw_triop ty Ite cond r1 else_
×
420
  | _ -> raw_triop ty op e1 e2 e3
24✔
421

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

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

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

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

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

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

544
let naryop ty op hexps =
545
  (* Get list of value or exit early *)
546
  let rec extract_values acc = function
8✔
547
    | [] -> Some (List.rev acc)
7✔
548
    | hexp :: remaining ->
19✔
549
      begin match view hexp with
550
      | Val v -> extract_values (v :: acc) remaining
18✔
551
      | _ -> None
1✔
552
      end
553
  in
554

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

575
let[@inline] raw_extract (hte : t) ~(high : int) ~(low : int) : t =
576
  make (Extract (hte, high, low))
13✔
577

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

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

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

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

650
module Cache = Hashtbl.Make (struct
651
  type nonrec t = t
652

653
  let hash = hash
654

655
  let equal = equal
656
end)
657

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

675
module Bool = struct
676
  open Ty
677

678
  let of_val = function
679
    | Val True -> Some true
×
680
    | Val False -> Some false
×
681
    | _ -> None
126✔
682

683
  let true_ = value True
50✔
684

685
  let false_ = value False
50✔
686

687
  let to_val b = if b then true_ else false_
×
688

689
  let v b = to_val b [@@inline]
×
690

691
  let not b =
692
    let bexpr = view b in
×
693
    match of_val bexpr with
×
694
    | Some b -> to_val (not b)
×
695
    | None -> (
×
696
      match bexpr with
697
      | Unop (Ty_bool, Not, cond) -> cond
×
698
      | _ -> unop Ty_bool Not b )
×
699

700
  let equal b1 b2 =
701
    match (view b1, view b2) with
×
702
    | Val True, Val True | Val False, Val False -> true_
×
703
    | _ -> relop Ty_bool Eq b1 b2
×
704

705
  let distinct b1 b2 =
706
    match (view b1, view b2) with
×
707
    | Val True, Val False | Val False, Val True -> true_
×
708
    | _ -> relop Ty_bool Ne b1 b2
×
709

710
  let and_ b1 b2 =
711
    match (of_val (view b1), of_val (view b2)) with
63✔
712
    | Some true, _ -> b2
×
713
    | _, Some true -> b1
×
714
    | Some false, _ | _, Some false -> false_
×
715
    | _ -> binop Ty_bool And b1 b2
63✔
716

717
  let or_ b1 b2 =
718
    match (of_val (view b1), of_val (view b2)) with
×
719
    | Some false, _ -> b2
×
720
    | _, Some false -> b1
×
721
    | Some true, _ | _, Some true -> true_
×
722
    | _ -> binop Ty_bool Or b1 b2
×
723

724
  let implies a b = binop Ty_bool Implies a b
×
725

726
  let ite c r1 r2 = triop Ty_bool Ite c r1 r2
×
727
end
728

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

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

806
module Set = struct
807
  include Set.Make (Key)
808

809
  type key = Key.t
810

811
  let hash = Hashtbl.hash
812

813
  let to_list s = elements s
2✔
814

815
  let pp fmt v =
816
    Fmt.pf fmt "@[<hov 1>%a@]"
×
817
      (Fmt.iter iter ~sep:(fun fmt () -> Fmt.pf fmt "@;") pp)
×
818
      v
819

820
  let get_symbols (set : t) =
821
    fold (fun x acc -> get_symbols_aux Fun.id acc x) set []
×
822
    |> List.sort_uniq Symbol.compare
×
823

824
  let map f set =
825
    fold
×
826
      (fun elt set ->
827
        let elt = f elt in
×
828
        add elt set )
×
829
      set empty
830

831
  let inline_symbol_values symbol_map set =
832
    map (inline_symbol_values symbol_map) set
×
833
end
834

835
let rec split_conjunctions (e : t) : Set.t =
836
  match view e with
×
837
  | Binop (Ty_bool, And, e1, e2) ->
×
838
    let s1 = split_conjunctions e1 in
839
    let s2 = split_conjunctions e2 in
×
840
    Set.union s1 s2
×
841
  | _ -> Set.singleton e
×
842

843
let rec split_disjunctions (e : t) : Set.t =
844
  match view e with
×
845
  | Binop (Ty_bool, Or, e1, e2) ->
×
846
    let s1 = split_disjunctions e1 in
847
    let s2 = split_disjunctions e2 in
×
848
    Set.union s1 s2
×
849
  | _ -> 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