Source file formatcil.ml
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
open Cil
module E = Errormsg
module H = Hashtbl
let noMemoize = ref false
let expMemoTable :
(string, (((string * formatArg) list -> exp) *
(exp -> formatArg list option))) H.t = H.create 23
let typeMemoTable :
(string, (((string * formatArg) list -> typ) *
(typ -> formatArg list option))) H.t = H.create 23
let lvalMemoTable :
(string, (((string * formatArg) list -> lval) *
(lval -> formatArg list option))) H.t = H.create 23
let instrMemoTable :
(string, ((location -> (string * formatArg) list -> instr) *
(instr -> formatArg list option))) H.t = H.create 23
let stmtMemoTable :
(string, ((string -> typ -> varinfo) ->
location ->
(string * formatArg) list -> stmt)) H.t = H.create 23
let stmtsMemoTable :
(string, ((string -> typ -> varinfo) ->
location ->
(string * formatArg) list -> stmt list)) H.t = H.create 23
let doParse (prog: string)
(theParser: (Lexing.lexbuf -> Formatparse.token)
-> Lexing.lexbuf -> 'a)
(memoTable: (string, 'a) H.t) : 'a =
try
if !noMemoize then raise Not_found else
H.find memoTable prog
with Not_found -> begin
let lexbuf = Formatlex.init ~prog:prog in
try
Formatparse.initialize Formatlex.initial lexbuf;
let res = theParser Formatlex.initial lexbuf in
H.add memoTable prog res;
Formatlex.finish ();
res
with Parsing.Parse_error -> begin
Formatlex.finish ();
E.s (E.error "Parsing error: %s" prog)
end
| e -> begin
ignore (E.log "Caught %s while parsing\n" (Printexc.to_string e));
Formatlex.finish ();
raise e
end
end
let cExp (prog: string) : (string * formatArg) list -> exp =
let cf = doParse prog Formatparse.expression expMemoTable in
(fst cf)
let cLval (prog: string) : (string * formatArg) list -> lval =
let cf = doParse prog Formatparse.lval lvalMemoTable in
(fst cf)
let cType (prog: string) : (string * formatArg) list -> typ =
let cf = doParse prog Formatparse.typename typeMemoTable in
(fst cf)
let cInstr (prog: string) : location -> (string * formatArg) list -> instr =
let cf = doParse prog Formatparse.instr instrMemoTable in
(fst cf)
let cStmt (prog: string) : (string -> typ -> varinfo) ->
location -> (string * formatArg) list -> stmt =
let cf = doParse prog Formatparse.stmt stmtMemoTable in
cf
let cStmts (prog: string) :
(string -> typ -> varinfo) ->
location -> (string * formatArg) list -> stmt list =
let cf = doParse prog Formatparse.stmt_list stmtsMemoTable in
cf
let dExp (prog: string) : exp -> formatArg list option =
let df = doParse prog Formatparse.expression expMemoTable in
(snd df)
let dLval (prog: string) : lval -> formatArg list option =
let df = doParse prog Formatparse.lval lvalMemoTable in
(snd df)
let dType (prog: string) : typ -> formatArg list option =
let df = doParse prog Formatparse.typename typeMemoTable in
(snd df)
let dInstr (prog: string) : instr -> formatArg list option =
let df = doParse prog Formatparse.instr instrMemoTable in
(snd df)
let test () =
let func = emptyFunction "test_formatcil" in
let res = makeLocalVar func "res" (TPtr(intType, [])) in
let fptr = makeLocalVar func "fptr"
(TPtr(TFun(intType, None, false, []), [])) in
let makeInstr () =
Call(Some (var res),
Lval (Mem (CastE(TPtr(TFun(TPtr(intType, []),
Some [ ("", intType, []);
("a2", TPtr(intType, []), []);
("a3", TPtr(TPtr(intType, []),
[]), []) ],
false, []), []),
Lval (var fptr))),
NoOffset),
[ ], locUnknown, locUnknown)
in
let times = 100000 in
Stats.time "make instruction regular"
(fun _ -> for _ = 0 to times do ignore (makeInstr ()) done)
();
noMemoize := true;
Stats.time "make instruction interpreted"
(fun _ -> for _ = 0 to times do
let _ =
cInstr "%v:res = (* ((int * (*)(int, int * a2, int * * a3))%v:fptr))();"
locUnknown [ ("res", Fv res);
("fptr", Fv fptr) ]
in
()
done)
();
noMemoize := false;
Stats.time "make instruction interpreted memoized"
(fun _ -> for _ = 0 to times do
let _ =
cInstr "%v:res = (* ((int * (*)(int, int * a2, int * * a3))%v:fptr))();"
locUnknown [ ("res", Fv res); ("fptr", Fv fptr) ]
in
()
done)
();
let partInstr =
cInstr "%v:res = (* ((int * (*)(int, int * a2, int * * a3))%v:fptr))();" in
Stats.time "make instruction interpreted partial"
(fun _ -> for _ = 0 to times do
let _ =
partInstr
locUnknown [ ("res", Fv res); ("fptr", Fv fptr) ]
in
()
done)
();
()