1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
(* SPDX-License-Identifier: MIT *)
(* Copyright (C) 2023-2024 formalsec *)
(* Written by the Smtml programmers *)
type _ cast =
| C8 : int cast
| C32 : int32 cast
| C64 : int64 cast
type t =
| Ty_app
| Ty_bitv of int
| Ty_bool
| Ty_fp of int
| Ty_int
| Ty_list
| Ty_none
| Ty_real
| Ty_str
| Ty_unit
| Ty_regexp
let discr = function
| Ty_app -> 0
| Ty_bool -> 1
| Ty_int -> 2
| Ty_list -> 3
| Ty_none -> 4
| Ty_real -> 5
| Ty_str -> 6
| Ty_unit -> 7
| Ty_regexp -> 8
| Ty_bitv n -> 9 + n
| Ty_fp n -> 10 + n
let compare t1 t2 = compare (discr t1) (discr t2)
let equal t1 t2 = compare t1 t2 = 0
let pp fmt = function
| Ty_int -> Fmt.string fmt "int"
| Ty_real -> Fmt.string fmt "real"
| Ty_bool -> Fmt.string fmt "bool"
| Ty_str -> Fmt.string fmt "str"
| Ty_bitv n -> Fmt.pf fmt "i%d" n
| Ty_fp n -> Fmt.pf fmt "f%d" n
| Ty_list -> Fmt.string fmt "list"
| Ty_app -> Fmt.string fmt "app"
| Ty_unit -> Fmt.string fmt "unit"
| Ty_none -> Fmt.string fmt "none"
| Ty_regexp -> Fmt.string fmt "regexp"
let string_of_type (ty : t) : string = Fmt.str "%a" pp ty
let of_string = function
| "int" -> Ok Ty_int
| "real" -> Ok Ty_real
| "bool" -> Ok Ty_bool
| "str" -> Ok Ty_str
| "list" -> Ok Ty_list
| "app" -> Ok Ty_app
| "unit" -> Ok Ty_unit
| "none" -> Ok Ty_none
| "regexp" -> Ok Ty_regexp
| s ->
if String.starts_with ~prefix:"i" s then begin
let s = String.sub s 1 (String.length s - 1) in
match int_of_string s with
| None -> Error (Fmt.str "can not parse type %s" s)
| Some n when n < 0 ->
Error (Fmt.str "size of bitvectors must be a positive integer")
| Some n -> Ok (Ty_bitv n)
end
else if String.starts_with ~prefix:"f" s then begin
let s = String.sub s 1 (String.length s - 1) in
match int_of_string s with
| None -> Error (Fmt.str "can not parse type %s" s)
| Some n when n < 0 ->
Error (Fmt.str "size of fp must be a positive integer")
| Some n -> Ok (Ty_fp n)
end
else Error (Fmt.str "can not parse type %s" s)
let size (ty : t) : int =
match ty with
| Ty_bitv n | Ty_fp n -> n / 8
| Ty_int | Ty_bool -> 4
| Ty_real | Ty_str | Ty_list | Ty_app | Ty_unit | Ty_none | Ty_regexp ->
assert false
module Unop = struct
type t =
| Neg
| Not
| Clz
| Ctz
(* Float *)
| Abs
| Sqrt
| Is_nan
| Ceil
| Floor
| Trunc
| Nearest
| Head
| Tail
| Reverse
| Length
(* String *)
| Trim
(* RegExp *)
| Regexp_star
| Regexp_loop of (int * int)
| Regexp_plus
| Regexp_opt
| Regexp_comp
let equal o1 o2 =
match (o1, o2) with
| Neg, Neg
| Not, Not
| Clz, Clz
| Ctz, Ctz
| Abs, Abs
| Sqrt, Sqrt
| Is_nan, Is_nan
| Ceil, Ceil
| Floor, Floor
| Trunc, Trunc
| Nearest, Nearest
| Head, Head
| Tail, Tail
| Reverse, Reverse
| Length, Length
| Trim, Trim
| Regexp_star, Regexp_star
| Regexp_loop _, Regexp_loop _
| Regexp_plus, Regexp_plus
| Regexp_opt, Regexp_opt
| Regexp_comp, Regexp_comp ->
true
| ( ( Neg | Not | Clz | Ctz | Abs | Sqrt | Is_nan | Ceil | Floor | Trunc
| Nearest | Head | Tail | Reverse | Length | Trim | Regexp_star
| Regexp_loop _ | Regexp_plus | Regexp_opt | Regexp_comp )
, _ ) ->
false
let pp fmt = function
| Neg -> Fmt.string fmt "neg"
| Not -> Fmt.string fmt "not"
| Clz -> Fmt.string fmt "clz"
| Ctz -> Fmt.string fmt "ctz"
| Abs -> Fmt.string fmt "abs"
| Sqrt -> Fmt.string fmt "sqrt"
| Is_nan -> Fmt.string fmt "is_nan"
| Ceil -> Fmt.string fmt "ceil"
| Floor -> Fmt.string fmt "floor"
| Trunc -> Fmt.string fmt "trunc"
| Nearest -> Fmt.string fmt "nearest"
| Head -> Fmt.string fmt "head"
| Tail -> Fmt.string fmt "tail"
| Reverse -> Fmt.string fmt "reverse"
| Length -> Fmt.string fmt "length"
| Trim -> Fmt.string fmt "trim"
| Regexp_star -> Fmt.string fmt "*"
| Regexp_loop _ -> Fmt.string fmt "loop"
| Regexp_plus -> Fmt.string fmt "+"
| Regexp_opt -> Fmt.string fmt "opt"
| Regexp_comp -> Fmt.string fmt "comp"
end
module Binop = struct
type t =
| Add
| Sub
| Mul
| Div
| DivU
| Rem
| RemU
| Shl
| ShrA
| ShrL
| And
| Or
| Xor
| Pow
| Min
| Max
| Rotl
| Rotr
| At
| List_cons
| List_append
(* String *)
| String_prefix
| String_suffix
| String_contains
| String_last_index
| String_in_re
(* Regexp *)
| Regexp_range
let equal o1 o2 =
match (o1, o2) with
| Add, Add
| Sub, Sub
| Mul, Mul
| Div, Div
| DivU, DivU
| Rem, Rem
| RemU, RemU
| Shl, Shl
| ShrA, ShrA
| ShrL, ShrL
| And, And
| Or, Or
| Xor, Xor
| Pow, Pow
| Min, Min
| Max, Max
| Rotl, Rotl
| Rotr, Rotr
| At, At
| List_cons, List_cons
| List_append, List_append
| String_prefix, String_prefix
| String_suffix, String_suffix
| String_contains, String_contains
| String_last_index, String_last_index
| String_in_re, String_in_re
| Regexp_range, Regexp_range ->
true
| ( ( Add | Sub | Mul | Div | DivU | Rem | RemU | Shl | ShrA | ShrL | And
| Or | Xor | Pow | Min | Max | Rotl | Rotr | At | List_cons
| List_append | String_prefix | String_suffix | String_contains
| String_last_index | String_in_re | Regexp_range )
, _ ) ->
false
let pp fmt = function
| Add -> Fmt.string fmt "add"
| Sub -> Fmt.string fmt "sub"
| Mul -> Fmt.string fmt "mul"
| Div -> Fmt.string fmt "div"
| DivU -> Fmt.string fmt "div_u"
| Rem -> Fmt.string fmt "rem"
| RemU -> Fmt.string fmt "rem_u"
| Shl -> Fmt.string fmt "shl"
| ShrA -> Fmt.string fmt "shr"
| ShrL -> Fmt.string fmt "shr_u"
| And -> Fmt.string fmt "and"
| Or -> Fmt.string fmt "or"
| Xor -> Fmt.string fmt "xor"
| Pow -> Fmt.string fmt "pow"
| Min -> Fmt.string fmt "min"
| Max -> Fmt.string fmt "max"
| Rotl -> Fmt.string fmt "rotl"
| Rotr -> Fmt.string fmt "rotr"
| At -> Fmt.string fmt "at"
| List_cons -> Fmt.string fmt "cons"
| List_append -> Fmt.string fmt "append"
| String_prefix -> Fmt.string fmt "prefixof"
| String_suffix -> Fmt.string fmt "suffixof"
| String_contains -> Fmt.string fmt "contains"
| String_last_index -> Fmt.string fmt "last_indexof"
| String_in_re -> Fmt.string fmt "in_re"
| Regexp_range -> Fmt.string fmt "range"
end
module Relop = struct
type t =
| Eq
| Ne
| Lt
| LtU
| Gt
| GtU
| Le
| LeU
| Ge
| GeU
let equal op1 op2 =
match (op1, op2) with
| Eq, Eq
| Ne, Ne
| Lt, Lt
| LtU, LtU
| Gt, Gt
| GtU, GtU
| Le, Le
| LeU, LeU
| Ge, Ge
| GeU, GeU ->
true
| (Eq | Ne | Lt | LtU | Gt | GtU | Le | LeU | Ge | GeU), _ -> false
let pp fmt = function
| Eq -> Fmt.string fmt "eq"
| Ne -> Fmt.string fmt "ne"
| Lt -> Fmt.string fmt "lt"
| LtU -> Fmt.string fmt "lt_u"
| Gt -> Fmt.string fmt "gt"
| GtU -> Fmt.string fmt "gt_u"
| Le -> Fmt.string fmt "le"
| LeU -> Fmt.string fmt "le_u"
| Ge -> Fmt.string fmt "ge"
| GeU -> Fmt.string fmt "ge_u"
end
module Triop = struct
type t =
| Ite
| List_set
(* String *)
| String_extract
| String_replace
| String_index
let equal op1 op2 =
match (op1, op2) with
| Ite, Ite
| List_set, List_set
| String_extract, String_extract
| String_replace, String_replace
| String_index, String_index ->
true
| (Ite | List_set | String_extract | String_replace | String_index), _ ->
false
let pp fmt = function
| Ite -> Fmt.string fmt "ite"
| String_extract -> Fmt.string fmt "substr"
| String_replace -> Fmt.string fmt "replace"
| String_index -> Fmt.string fmt "indexof"
| List_set -> Fmt.string fmt "set"
end
module Cvtop = struct
type t =
| ToString
| OfString
| ToBool
| OfBool
| Reinterpret_int
| Reinterpret_float
| DemoteF64
| PromoteF32
| ConvertSI32
| ConvertUI32
| ConvertSI64
| ConvertUI64
| TruncSF32
| TruncUF32
| TruncSF64
| TruncUF64
| WrapI64
| Sign_extend of int
| Zero_extend of int
(* String *)
| String_to_code
| String_from_code
| String_to_int
| String_from_int
| String_to_float
| String_to_re
let equal op1 op2 =
match (op1, op2) with
| ToString, ToString
| OfString, OfString
| ToBool, ToBool
| OfBool, OfBool
| Reinterpret_int, Reinterpret_int
| Reinterpret_float, Reinterpret_float
| DemoteF64, DemoteF64
| PromoteF32, PromoteF32
| ConvertSI32, ConvertSI32
| ConvertUI32, ConvertUI32
| ConvertSI64, ConvertSI64
| ConvertUI64, ConvertUI64
| TruncSF32, TruncSF32
| TruncUF32, TruncUF32
| TruncSF64, TruncSF64
| TruncUF64, TruncUF64
| WrapI64, WrapI64
| String_to_code, String_to_code
| String_from_code, String_from_code
| String_to_int, String_to_int
| String_from_int, String_from_int
| String_to_float, String_to_float
| String_to_re, String_to_re ->
true
| Sign_extend x1, Sign_extend x2 | Zero_extend x1, Zero_extend x2 -> x1 = x2
| ( ( ToString | OfString | ToBool | OfBool | Reinterpret_int
| Reinterpret_float | DemoteF64 | PromoteF32 | ConvertSI32 | ConvertUI32
| ConvertSI64 | ConvertUI64 | TruncSF32 | TruncUF32 | TruncSF64
| TruncUF64 | WrapI64 | Sign_extend _ | Zero_extend _ | String_to_code
| String_from_code | String_to_int | String_from_int | String_to_float
| String_to_re )
, _ ) ->
false
let pp fmt = function
| ToString -> Fmt.string fmt "to_string"
| OfString -> Fmt.string fmt "of_string"
| ToBool -> Fmt.string fmt "to_bool"
| OfBool -> Fmt.string fmt "of_bool"
| Reinterpret_int -> Fmt.string fmt "reinterpret_int"
| Reinterpret_float -> Fmt.string fmt "reinterpret_float"
| DemoteF64 -> Fmt.string fmt "demote_f64"
| PromoteF32 -> Fmt.string fmt "promote_f32"
| ConvertSI32 -> Fmt.string fmt "convert_i32_s"
| ConvertUI32 -> Fmt.string fmt "convert_i32_u"
| ConvertSI64 -> Fmt.string fmt "convert_i64_s"
| ConvertUI64 -> Fmt.string fmt "convert_i64_u"
| TruncSF32 -> Fmt.string fmt "trunc_f32_s"
| TruncUF32 -> Fmt.string fmt "trunc_f32_u"
| TruncSF64 -> Fmt.string fmt "trunc_f64_s"
| TruncUF64 -> Fmt.string fmt "trunc_f64_u"
| WrapI64 -> Fmt.string fmt "wrap_i64"
| Sign_extend sz -> Fmt.pf fmt "extend_i%d_s" sz
| Zero_extend sz -> Fmt.pf fmt "extend_i%d_u" sz
| String_to_code -> Fmt.string fmt "to_code"
| String_from_code -> Fmt.string fmt "from_code"
| String_to_int -> Fmt.string fmt "to_int"
| String_from_int -> Fmt.string fmt "from_int"
| String_to_float -> Fmt.string fmt "to_float"
| String_to_re -> Fmt.string fmt "to_re"
end
module Naryop = struct
type t =
| Logand
| Logor
| Concat
| Regexp_union
let equal op1 op2 =
match (op1, op2) with
| Logand, Logand
| Logor, Logor
| Concat, Concat
| Regexp_union, Regexp_union ->
true
| (Logand | Logor | Concat | Regexp_union), _ -> false
let pp fmt = function
| Logand -> Fmt.string fmt "and"
| Logor -> Fmt.string fmt "or"
| Concat -> Fmt.string fmt "++"
| Regexp_union -> Fmt.string fmt "union"
end