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
open Definitions
open Qed.Logic
open Lang
type cst =
| C_str of string
| W_str of int64 list
module STR =
struct
type t = cst
let compare = Stdlib.compare
let pretty fmt = function
| C_str s -> Format.fprintf fmt "%S" s
| W_str _ -> Format.fprintf fmt "\"L<...>\""
let hash (c:t) = FCHashtbl.hash c land 0xFFFF
end
let pretty = STR.pretty
let cluster () =
Definitions.cluster ~id:"cstring" ~title:"String Literals" ()
module LIT = WpContext.Generator(STR)
(struct
type key = cst
type data = int * F.term
let name = "Cstring.Literals"
let hid = Hashtbl.create 31
let rec lookup id =
if id=0 || Hashtbl.mem hid id
then lookup (succ id)
else (Hashtbl.add hid id () ; id)
let export_literal prefix lfun str =
let chars = ref [] in
let array = F.e_fun lfun [] in
let n = String.length str in
for i = 0 to n do
let a = F.e_get array (F.e_int i) in
let c =
if i = n
then F.e_zero
else F.e_int (int_of_char str.[i])
in
chars := (F.p_equal a c) :: !chars ;
done ;
define_lemma {
l_name = prefix ^ "_literal" ;
l_cluster = cluster () ;
l_kind = Admit ;
l_forall = [] ;
l_triggers = [] ;
l_lemma = F.p_conj (List.rev !chars) ;
}
let compile s =
let id = lookup (STR.hash s) in
let lfun = Lang.generated_f ~result:(Array(Int,Int)) "Lit_%04X" id in
let prefix = Lang.Fun.debug lfun in
define_symbol {
d_lfun = lfun ;
d_cluster = cluster () ;
d_types = 0 ;
d_params = [] ;
d_definition = Logic (Array(Int,Int)) ;
} ;
if Wp_parameters.Literals.get () then
begin match s with
| C_str str -> export_literal prefix lfun str
| W_str _ ->
Wp_parameters.warning ~current:false ~once:true
"Content of wide string literals not exported."
end ;
id , F.e_fun lfun []
end)
let str_id s = fst (LIT.get s)
let str_val s = snd (LIT.get s)
let str_len s n = match s with
| C_str s -> F.p_equal n (F.e_int (String.length s))
| W_str _ -> F.p_lt F.e_zero n
let char_at s k = F.e_get (str_val s) k