-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathsyn2equation.ml
480 lines (443 loc) · 15.1 KB
/
syn2equation.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
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
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
(** Generating equations from abstract syntax tree *)
(* This file is part of the Interproc analyzer, released under GPL license.
Please read the COPYING file packaged in the distribution.
Copyright (C) Mathias Argoud, Gaël Lalire, Bertrand Jeannet 2007.
*)
open Spl_syn
open Format
(* ********************************************************************** *)
(** {2 Useful Information for generating equations} *)
(* ********************************************************************** *)
(* ---------------------------------------------------------------------- *)
(** {3 Utility functions} *)
(* ---------------------------------------------------------------------- *)
(** Last element of a list *)
let rec last_of_list = function
| [] -> failwith ""
| [x] -> x
| x::l -> last_of_list l
(** Exit point of a block *)
let exit_of_block block =
if block.instrs=[] then
block.bpoint
else begin
let instr = last_of_list block.instrs in
instr.ipoint
end
(** Extract an array of variables from variable declaration list *)
let convert (lvartyp:(var*typ) list) : var array
=
Array.of_list
(List.map (fun (var,typ) -> var) lvartyp)
(** Add to an environment a list of variables *)
let add_env
(env:Apron.Environment.t)
(lvartyp:(var*typ) list)
:
Apron.Environment.t
=
let (lint,lreal) =
List.fold_left
(begin fun (lint,lreal) (var,typ) ->
if typ = INT then
(var::lint,lreal)
else
(lint,var::lreal)
end)
([],[])
lvartyp
in
Apron.Environment.add env
(Array.of_list lint)
(Array.of_list lreal)
(* ---------------------------------------------------------------------- *)
(** {3 Building preprocessed information} *)
(* ---------------------------------------------------------------------- *)
(** Build a [Equation.procinfo] object from [Spl_syn.procedure]. *)
let make_procinfo (proc:procedure) : Equation.procinfo
=
let (pcode:block) = proc.pcode in
let pstart = pcode.bpoint in
let pexit = exit_of_block pcode in
let pinput = convert proc.Spl_syn.pinput in
let poutput = convert proc.Spl_syn.poutput in
let plocal = convert proc.Spl_syn.plocal in
let penv = Apron.Environment.make [||] [||] in
let penv = add_env penv proc.Spl_syn.pinput in
let penv = add_env penv proc.Spl_syn.poutput in
let penv = add_env penv proc.Spl_syn.plocal in
let poutput_tmp =
Array.mapi
(begin fun i var ->
Apron.Var.of_string (Format.sprintf "_%%out%i%%_" i)
end)
poutput
in
{
Equation.pname = proc.pname;
Equation.pstart = pstart;
Equation.pexit = pexit;
Equation.pinput = pinput;
Equation.poutput = poutput;
Equation.plocal = plocal;
Equation.penv = penv;
Equation.poutput_tmp = poutput_tmp;
}
(** Build a [Equation.info] object from [Spl_syn.program]. *)
let make_info (prog:program) : Equation.info
=
let procinfo = Hashhe.create 3 in
List.iter
(begin fun proc ->
let info = make_procinfo proc in
Hashhe.add procinfo proc.pname info
end)
prog.procedures
;
let callret = DHashhe.create 3 in
List.iter
(begin fun proc ->
Spl_syn.iter_eltinstr
(begin fun (bpoint,instr) ->
begin match instr.instruction with
| CALL _ -> DHashhe.add callret bpoint instr.ipoint
| _ -> ()
end
end)
proc.pcode
end)
prog.procedures
;
let pointenv = Hashhe.create 3 in
List.iter
(begin fun proc ->
let pinfo = Hashhe.find procinfo proc.pname in
let env = pinfo.Equation.penv in
Spl_syn.iter_instr
(begin fun (point,instr) ->
if not (Hashhe.mem pointenv point) then
Hashhe.add pointenv point env;
if not (Hashhe.mem pointenv instr.ipoint) then
Hashhe.add pointenv instr.ipoint env;
end)
proc.pcode
end)
prog.procedures
;
{
Equation.procinfo = procinfo;
Equation.callret = callret;
Equation.pointenv = pointenv;
Equation.counter = 0;
}
(* ********************************************************************** *)
(** {2 Translating expressions} *)
(* ********************************************************************** *)
let negate_texpr (texpr:Apron.Texpr1.t) : Apron.Texpr1.t
=
let expr = Apron.Texpr1.to_expr texpr in
let nexpr = match expr with
| Apron.Texpr1.Unop(Apron.Texpr1.Neg,e,typ,round) ->
e
| _ ->
Apron.Texpr1.Unop(
Apron.Texpr1.Neg, expr,
Apron.Texpr1.Real, Apron.Texpr1.Rnd
)
in
let env = Apron.Texpr1.get_env texpr in
Apron.Texpr1.of_expr env nexpr
let negate_tcons (tcons:Apron.Tcons1.t) : Apron.Tcons1.t
=
let texpr = Apron.Tcons1.get_texpr1 tcons in
let (ntyp,ntexpr) = match Apron.Tcons1.get_typ tcons with
| Apron.Tcons1.EQ -> (Apron.Tcons1.DISEQ,texpr)
| Apron.Tcons1.DISEQ -> (Apron.Tcons1.EQ,texpr)
| Apron.Tcons1.SUPEQ -> (Apron.Tcons1.SUP, negate_texpr texpr)
| Apron.Tcons1.SUP -> (Apron.Tcons1.SUPEQ, negate_texpr texpr)
| Apron.Tcons1.EQMOD _ -> failwith "EQMOD not supported now"
in
Apron.Tcons1.make ntexpr ntyp
let tcons_of_cons env (cons:Spl_syn.cons) : Apron.Tcons1.t
=
let (expr1,typ,expr2) = cons in
let (typ,expr) = match typ with
| EQ ->
(Apron.Tcons1.EQ,
Apron.Texpr1.Binop(Apron.Texpr1.Sub,expr1,expr2,Apron.Texpr1.Real, Apron.Texpr1.Rnd))
| NEQ ->
(Apron.Tcons1.DISEQ,
Apron.Texpr1.Binop(Apron.Texpr1.Sub,expr1,expr2,Apron.Texpr1.Real, Apron.Texpr1.Rnd))
| GEQ ->
(Apron.Tcons1.SUPEQ,
Apron.Texpr1.Binop(Apron.Texpr1.Sub,expr1,expr2,Apron.Texpr1.Real, Apron.Texpr1.Rnd))
| GT ->
(Apron.Tcons1.SUP,
Apron.Texpr1.Binop(Apron.Texpr1.Sub,expr1,expr2,Apron.Texpr1.Real, Apron.Texpr1.Rnd))
| LEQ ->
(Apron.Tcons1.SUPEQ,
Apron.Texpr1.Binop(Apron.Texpr1.Sub,expr2,expr1,Apron.Texpr1.Real, Apron.Texpr1.Rnd))
| LT ->
(Apron.Tcons1.SUP,
Apron.Texpr1.Binop(Apron.Texpr1.Sub,expr2,expr1,Apron.Texpr1.Real, Apron.Texpr1.Rnd))
in
Apron.Tcons1.make (Apron.Texpr1.of_expr env expr) typ
let rec push_not (bexpr:Spl_syn.bexpr) : Spl_syn.bexpr
=
match bexpr with
| TRUE | FALSE | BRANDOM | CONS _ ->
bexpr
| NOT(e) ->
begin match e with
| TRUE -> FALSE
| FALSE -> TRUE
| BRANDOM -> BRANDOM
| CONS(cons) -> e
| AND(e1,e2) -> OR(push_not (NOT e1), push_not (NOT e2))
| OR(e1,e2) -> AND(push_not (NOT e1), push_not (NOT e2))
| NOT(e) -> push_not e
end
| AND(e1,e2) -> AND(push_not e1, push_not e2)
| OR(e1,e2) -> OR(push_not e1, push_not e2)
let boolexpr0_of_bexpr env (bexpr:Spl_syn.bexpr)
:
Apron.Tcons1.t array Boolexpr.t
=
let cand t1 t2 = Boolexpr.make_conjunction (Array.append t1 t2) in
let rec translate bexpr =
match bexpr with
| TRUE | BRANDOM -> Boolexpr.make_cst true
| FALSE -> Boolexpr.make_cst false
| CONS(cons) ->
let tcons = tcons_of_cons env cons in
Boolexpr.make_conjunction [|tcons|]
| AND(e1,e2) ->
Boolexpr.make_and ~cand
(translate e1) (translate e2)
| OR(e1,e2) ->
Boolexpr.make_or (translate e1) (translate e2)
| NOT(e) ->
begin match e with
| FALSE | BRANDOM -> Boolexpr.make_cst true
| TRUE -> Boolexpr.make_cst false
| CONS(cons) ->
let tcons = tcons_of_cons env cons in
let tcons = negate_tcons tcons in
Boolexpr.make_conjunction [|tcons|]
| AND(e1,e2) ->
Boolexpr.make_or (translate (NOT e1)) (translate (NOT e2))
| OR(e1,e2) ->
Boolexpr.make_and ~cand
(translate (NOT e1)) (translate (NOT e2))
| NOT(e) -> translate e
end
in
translate bexpr
let boolexpr_of_bexpr env (bexpr:Spl_syn.bexpr)
:
Apron.Tcons1.earray Boolexpr.t
=
let bexpr0 = boolexpr0_of_bexpr env bexpr in
Boolexpr.map
(begin fun tcons ->
assert(tcons<>[||]);
let res = Apron.Tcons1.array_make env (Array.length tcons) in
Array.iteri
(fun i cons -> Apron.Tcons1.array_set res i cons)
tcons;
res
end)
bexpr0
(* ********************************************************************** *)
(** {2 Forward equations} *)
(* ********************************************************************** *)
module Forward = struct
let make (prog:Spl_syn.program) : Equation.graph =
let info = make_info prog in
let graph = Equation.create 3 info in
let rec iter_block (procinfo:Equation.procinfo) (block:block) : unit
=
let env = procinfo.Equation.penv in
ignore begin
List.fold_left
(begin fun point instr ->
begin match instr.instruction with
| SKIP ->
let transfer = Equation.Condition(Boolexpr.TRUE) in
Equation.add_equation graph [|point|] transfer instr.ipoint;
| HALT
| FAIL ->
(* We still put a dummy equation *)
let transfer = Equation.Condition(Boolexpr.DISJ([])) in
Equation.add_equation graph [|point|] transfer instr.ipoint;
()
| ASSUME(bexpr) ->
let cond = boolexpr_of_bexpr env bexpr in
let transfer = Equation.Condition(cond) in
Equation.add_equation graph [|point|] transfer instr.ipoint;
| ASSIGN(var,iexpr) ->
let (texpr:Apron.Texpr1.t) =
Apron.Texpr1.of_expr env iexpr
in
let transfer = Equation.Tassign(var,texpr) in
Equation.add_equation graph [|point|] transfer instr.ipoint;
| CALL(pout,name,pin) ->
let callee = Hashhe.find info.Equation.procinfo name in
let pin = Array.of_list pin in
let pout = Array.of_list pout in
let calltransfer = Equation.Call(procinfo,callee,pin,pout) in
let rettransfer = Equation.Return(procinfo,callee, pin, pout) in
Equation.add_equation graph
[|point|] calltransfer callee.Equation.pstart;
Equation.add_equation graph
[|point; callee.Equation.pexit|] rettransfer instr.ipoint;
| IF(bexpr,block) ->
let cond = boolexpr_of_bexpr env bexpr in
let condnot = boolexpr_of_bexpr env (NOT bexpr) in
let condtransfer = Equation.Condition(cond) in
let condnottransfer = Equation.Condition(condnot) in
Equation.add_equation graph
[|point|] condtransfer block.bpoint;
Equation.add_equation graph
[|exit_of_block block|] (Equation.Condition(Boolexpr.make_cst true)) instr.ipoint;
Equation.add_equation graph
[|point|] condnottransfer instr.ipoint;
iter_block procinfo block
| IFELSE(bexpr,block1,block2) ->
let cond = boolexpr_of_bexpr env bexpr in
let condnot = boolexpr_of_bexpr env (NOT bexpr) in
let condtransfer = Equation.Condition(cond) in
let condnottransfer = Equation.Condition(condnot) in
Equation.add_equation graph
[|point|] condtransfer block1.bpoint;
Equation.add_equation graph
[|exit_of_block block1|] (Equation.Condition(Boolexpr.make_cst true)) instr.ipoint;
Equation.add_equation graph
[|point|] condnottransfer block2.bpoint;
Equation.add_equation graph
[|exit_of_block block2|] (Equation.Condition(Boolexpr.make_cst true)) instr.ipoint;
iter_block procinfo block1;
iter_block procinfo block2
| LOOP(bexpr,block) ->
let cond = boolexpr_of_bexpr env bexpr in
let condnot = boolexpr_of_bexpr env (NOT bexpr) in
let condtransfer = Equation.Condition(cond) in
let condnottransfer = Equation.Condition(condnot) in
Equation.add_equation graph
[|point|] condtransfer block.bpoint;
Equation.add_equation graph
[|exit_of_block block|] (Equation.Condition(Boolexpr.make_cst true)) point;
Equation.add_equation graph
[|point|] condnottransfer instr.ipoint;
iter_block procinfo block
end;
instr.ipoint
end)
block.bpoint
block.instrs
end
in
List.iter
(begin fun procedure ->
let procinfo = Hashhe.find info.Equation.procinfo procedure.pname in
iter_block procinfo procedure.pcode;
end)
prog.procedures;
graph
end
(* ********************************************************************** *)
(** {2 Backward equations} *)
(* ********************************************************************** *)
module Backward = struct
let make (prog:Spl_syn.program) : Equation.graph =
let info = make_info prog in
let graph = Equation.create 3 info in
let rec iter_block (procinfo:Equation.procinfo) (block:block) : unit
=
let env = procinfo.Equation.penv in
ignore begin
List.fold_left
(begin fun point instr ->
begin match instr.instruction with
| SKIP ->
let transfer = Equation.Condition(Boolexpr.make_cst true) in
Equation.add_equation graph [|instr.ipoint|] transfer point;
| HALT
| FAIL ->
(* We still put a dummy equation *)
let transfer = Equation.Condition(Boolexpr.make_cst false) in
Equation.add_equation graph [|instr.ipoint|] transfer point;
| ASSUME(bexpr) ->
let cond = boolexpr_of_bexpr env bexpr in
let transfer = Equation.Condition(cond) in
Equation.add_equation graph [|instr.ipoint|] transfer point;
| ASSIGN(var,iexpr) ->
let (texpr:Apron.Texpr1.t) =
Apron.Texpr1.of_expr env iexpr
in
let transfer = Equation.Tassign(var,texpr) in
Equation.add_equation graph [|instr.ipoint|] transfer point;
| CALL(pout,name,pin) ->
let callee = Hashhe.find info.Equation.procinfo name in
let pin = Array.of_list pin in
let pout = Array.of_list pout in
let calltransfer = Equation.Call(procinfo,callee,pin,pout) in
let rettransfer = Equation.Return(procinfo,callee,pin,pout) in
Equation.add_equation graph
[|callee.Equation.pstart|] calltransfer point;
Equation.add_equation graph
[|instr.ipoint|] rettransfer callee.Equation.pexit;
| IF(bexpr,block) ->
let cond = boolexpr_of_bexpr env bexpr in
let condnot = boolexpr_of_bexpr env (NOT bexpr) in
let condtransfer = Equation.Condition(cond) in
let condnottransfer = Equation.Condition(condnot) in
Equation.add_equation graph
[|block.bpoint|] condtransfer point;
Equation.add_equation graph
[|instr.ipoint|] (Equation.Condition(Boolexpr.make_cst true)) (exit_of_block block);
Equation.add_equation graph
[|instr.ipoint|] condnottransfer point;
iter_block procinfo block
| IFELSE(bexpr,block1,block2) ->
let cond = boolexpr_of_bexpr env bexpr in
let condnot = boolexpr_of_bexpr env (NOT bexpr) in
let condtransfer = Equation.Condition(cond) in
let condnottransfer = Equation.Condition(condnot) in
Equation.add_equation graph
[|block1.bpoint|] condtransfer point;
Equation.add_equation graph
[|instr.ipoint|] (Equation.Condition(Boolexpr.make_cst true)) (exit_of_block block1);
Equation.add_equation graph
[|block2.bpoint|] condnottransfer point;
Equation.add_equation graph
[|instr.ipoint|] (Equation.Condition(Boolexpr.make_cst true)) (exit_of_block block2);
iter_block procinfo block1;
iter_block procinfo block2
| LOOP(bexpr,block) ->
let cond = boolexpr_of_bexpr env bexpr in
let condnot = boolexpr_of_bexpr env (NOT bexpr) in
let condtransfer = Equation.Condition(cond) in
let condnottransfer = Equation.Condition(condnot) in
Equation.add_equation graph
[|block.bpoint|] condtransfer point;
Equation.add_equation graph
[|point|] (Equation.Condition(Boolexpr.make_cst true)) (exit_of_block block);
Equation.add_equation graph
[|instr.ipoint|] condnottransfer point;
iter_block procinfo block
end;
instr.ipoint
end)
block.bpoint
block.instrs
end
in
List.iter
(begin fun procedure ->
let procinfo = Hashhe.find info.Equation.procinfo procedure.pname in
iter_block procinfo procedure.pcode;
end)
prog.procedures;
graph
end