1
|
module LT = LustreSpec
|
2
|
module MC = Machine_code
|
3
|
module ST = Salsa.Types
|
4
|
module Float = Salsa.Float
|
5
|
|
6
|
let debug = ref false
|
7
|
|
8
|
let pp_hash ~sep f fmt r =
|
9
|
Format.fprintf fmt "[@[<v>";
|
10
|
Hashtbl.iter (fun k v -> Format.fprintf fmt "%t%s@ " (f k v) sep) r;
|
11
|
Format.fprintf fmt "]@]";
|
12
|
|
13
|
module FormalEnv =
|
14
|
struct
|
15
|
type fe_t = (LT.ident, (int * LT.value_t)) Hashtbl.t
|
16
|
let cpt = ref 0
|
17
|
|
18
|
exception NoDefinition of LT.var_decl
|
19
|
(* Returns the expression associated to v in env *)
|
20
|
let get_def (env: fe_t) v =
|
21
|
try
|
22
|
snd (Hashtbl.find env v.LT.var_id)
|
23
|
with Not_found -> raise (NoDefinition v)
|
24
|
|
25
|
let def (env: fe_t) d expr =
|
26
|
incr cpt;
|
27
|
let fresh = Hashtbl.copy env in
|
28
|
Hashtbl.add fresh d.LT.var_id (!cpt, expr); fresh
|
29
|
|
30
|
let empty (): fe_t = Hashtbl.create 13
|
31
|
|
32
|
let pp fmt env = pp_hash ~sep:";" (fun k (_,v) fmt -> Format.fprintf fmt "%s -> %a" k MC.pp_val v) fmt env
|
33
|
|
34
|
let fold f = Hashtbl.fold (fun k (_,v) accu -> f k v accu)
|
35
|
|
36
|
let get_sort_fun env =
|
37
|
let order = Hashtbl.fold (fun k (cpt, _) accu -> (k,cpt)::accu) env [] in
|
38
|
fun v1 v2 ->
|
39
|
if List.mem_assoc v1.LT.var_id order && List.mem_assoc v2.LT.var_id order then
|
40
|
if (List.assoc v1.LT.var_id order) <= (List.assoc v2.LT.var_id order) then
|
41
|
-1
|
42
|
else
|
43
|
1
|
44
|
else
|
45
|
assert false
|
46
|
|
47
|
end
|
48
|
|
49
|
module Ranges =
|
50
|
functor (Value: sig type t val union: t -> t -> t val pp: Format.formatter -> t -> unit end) ->
|
51
|
struct
|
52
|
type t = Value.t
|
53
|
type r_t = (LT.ident, Value.t) Hashtbl.t
|
54
|
|
55
|
let empty: r_t = Hashtbl.create 13
|
56
|
|
57
|
(* Look for def of node i with inputs living in vtl_ranges, reinforce ranges
|
58
|
to bound vdl: each output of node i *)
|
59
|
let add_call ranges vdl id vtl_ranges = ranges (* TODO assert false. On est
|
60
|
pas obligé de faire
|
61
|
qqchose. On peut supposer
|
62
|
que les ranges sont donnés
|
63
|
pour chaque noeud *)
|
64
|
|
65
|
|
66
|
let pp = pp_hash ~sep:";" (fun k v fmt -> Format.fprintf fmt "%s -> %a" k Value.pp v)
|
67
|
let pp_val = Value.pp
|
68
|
|
69
|
let add_def ranges name r =
|
70
|
(* Format.eprintf "%s: declare %a@." *)
|
71
|
(* x.LT.var_id *)
|
72
|
(* Value.pp r ; *)
|
73
|
|
74
|
let fresh = Hashtbl.copy ranges in
|
75
|
Hashtbl.add fresh name r; fresh
|
76
|
|
77
|
let enlarge ranges name r =
|
78
|
let fresh = Hashtbl.copy ranges in
|
79
|
if Hashtbl.mem fresh name then
|
80
|
Hashtbl.replace fresh name (Value.union r (Hashtbl.find fresh name))
|
81
|
else
|
82
|
Hashtbl.add fresh name r;
|
83
|
fresh
|
84
|
|
85
|
|
86
|
(* Compute a join per variable *)
|
87
|
let merge ranges1 ranges2 =
|
88
|
(* Format.eprintf "Mergeing rangesint %a with %a@." pp ranges1 pp ranges2; *)
|
89
|
let ranges = Hashtbl.copy ranges1 in
|
90
|
Hashtbl.iter (fun k v ->
|
91
|
if Hashtbl.mem ranges k then (
|
92
|
(* Format.eprintf "%s: %a union %a = %a@." *)
|
93
|
(* k *)
|
94
|
(* Value.pp v *)
|
95
|
(* Value.pp (Hashtbl.find ranges k) *)
|
96
|
(* Value.pp (Value.union v (Hashtbl.find ranges k)); *)
|
97
|
Hashtbl.replace ranges k (Value.union v (Hashtbl.find ranges k))
|
98
|
)
|
99
|
else
|
100
|
Hashtbl.add ranges k v
|
101
|
) ranges2;
|
102
|
(* Format.eprintf "Merge result %a@." pp ranges; *)
|
103
|
ranges
|
104
|
|
105
|
end
|
106
|
|
107
|
module FloatIntSalsa =
|
108
|
struct
|
109
|
type t = ST.abstractValue
|
110
|
|
111
|
let pp fmt (f,r) =
|
112
|
let fs, rs = (Salsa.Float.Domain.print (f,r)) in
|
113
|
Format.fprintf fmt "%s + %s" fs rs
|
114
|
(* match f, r with
|
115
|
| ST.I(a,b), ST.J(c,d) ->
|
116
|
Format.fprintf fmt "[%f, %f] + [%s, %s]" a b (Num.string_of_num c) (Num.string_of_num d)
|
117
|
| ST.I(a,b), ST.JInfty -> Format.fprintf fmt "[%f, %f] + oo" a b
|
118
|
| ST.Empty, _ -> Format.fprintf fmt "???"
|
119
|
|
120
|
| _ -> assert false
|
121
|
*)
|
122
|
let union v1 v2 = Salsa.Float.Domain.join v1 v2
|
123
|
(* match v1, v2 with
|
124
|
|(ST.I(x1, x2), ST.J(y1, y2)), (ST.I(x1', x2'), ST.J(y1', y2')) ->
|
125
|
ST.(I(min x1 x1', max x2 x2'), J(min y1 y1', max y2 y2'))
|
126
|
| _ -> Format.eprintf "%a cup %a failed@.@?" pp v1 pp v2; assert false
|
127
|
*)
|
128
|
let inject cst = match cst with
|
129
|
| LT.Const_int(i) -> Salsa.Builder.mk_cst (ST.R(Num.num_of_int i, []), ST.R(Num.num_of_int i, []))
|
130
|
| LT.Const_real (c,e,s) -> (* TODO: this is incorrect. We should rather
|
131
|
compute the error associated to the float *)
|
132
|
|
133
|
let r = float_of_string s in
|
134
|
let r = Salsa.Prelude.r_of_f_aux r in
|
135
|
Salsa.Builder.mk_cst (Float.Domain.nnew r r)
|
136
|
|
137
|
(* let r = float_of_string s in *)
|
138
|
(* if r = 0. then *)
|
139
|
(* Salsa.Builder.mk_cst (ST.R(-. min_float, min_float),Float.ulp (ST.R(-. min_float, min_float))) *)
|
140
|
(* else *)
|
141
|
(* Salsa.Builder.mk_cst (ST.I(r*.(1.-.epsilon_float),r*.(1.+.epsilon_float)),Float.ulp (ST.I(r,r))) *)
|
142
|
| _ -> assert false
|
143
|
end
|
144
|
|
145
|
module RangesInt = Ranges (FloatIntSalsa)
|
146
|
|
147
|
module Vars =
|
148
|
struct
|
149
|
module VarSet = Set.Make (struct type t = LT.var_decl let compare x y = compare x.LT.var_id y.LT.var_id end)
|
150
|
let real_vars vs = VarSet.filter (fun v -> Types.is_real_type v.LT.var_type) vs
|
151
|
let of_list = List.fold_left (fun s e -> VarSet.add e s) VarSet.empty
|
152
|
|
153
|
include VarSet
|
154
|
|
155
|
let remove_list (set:t) (v_list: elt list) : t = List.fold_right VarSet.remove v_list set
|
156
|
let pp fmt vs = Utils.fprintf_list ~sep:", " Printers.pp_var fmt (VarSet.elements vs)
|
157
|
end
|
158
|
|
159
|
|
160
|
|
161
|
|
162
|
|
163
|
|
164
|
|
165
|
|
166
|
|
167
|
|
168
|
(*************************************************************************************)
|
169
|
(* Converting values back and forth *)
|
170
|
(*************************************************************************************)
|
171
|
|
172
|
let rec value_t2salsa_expr constEnv vt =
|
173
|
let value_t2salsa_expr = value_t2salsa_expr constEnv in
|
174
|
let res =
|
175
|
match vt.LT.value_desc with
|
176
|
(* | LT.Cst(LT.Const_tag(t) as c) -> *)
|
177
|
(* Format.eprintf "v2s: cst tag@."; *)
|
178
|
(* if List.mem_assoc t constEnv then ( *)
|
179
|
(* Format.eprintf "trouvé la constante %s: %a@ " t Printers.pp_const c; *)
|
180
|
(* FloatIntSalsa.inject (List.assoc t constEnv) *)
|
181
|
(* ) *)
|
182
|
(* else ( *)
|
183
|
(* Format.eprintf "Const tag %s unhandled@.@?" t ; *)
|
184
|
(* raise (Salsa.Prelude.Error ("Entschuldigung6, constant tag not yet implemented")) *)
|
185
|
(* ) *)
|
186
|
| LT.Cst(cst) -> (* Format.eprintf "v2s: cst tag 2: %a@." Printers.pp_const cst; *)FloatIntSalsa.inject cst
|
187
|
| LT.LocalVar(v)
|
188
|
| LT.StateVar(v) -> (* Format.eprintf "v2s: var %s@." v.LT.var_id; *)
|
189
|
let sel_fun = (fun (vname, _) -> v.LT.var_id = vname) in
|
190
|
if List.exists sel_fun constEnv then
|
191
|
let _, cst = List.find sel_fun constEnv in
|
192
|
FloatIntSalsa.inject cst
|
193
|
else
|
194
|
let id = v.LT.var_id in
|
195
|
Salsa.Builder.mk_id id
|
196
|
| LT.Fun(binop, [x;y]) -> let salsaX = value_t2salsa_expr x in
|
197
|
let salsaY = value_t2salsa_expr y in
|
198
|
let op = (
|
199
|
let pred f x y = Salsa.Builder.mk_int_of_bool (f x y) in
|
200
|
match binop with
|
201
|
| "+" -> Salsa.Builder.mk_plus
|
202
|
| "-" -> Salsa.Builder.mk_minus
|
203
|
| "*" -> Salsa.Builder.mk_times
|
204
|
| "/" -> Salsa.Builder.mk_div
|
205
|
| "=" -> pred Salsa.Builder.mk_eq
|
206
|
| "<" -> pred Salsa.Builder.mk_lt
|
207
|
| ">" -> pred Salsa.Builder.mk_gt
|
208
|
| "<=" -> pred Salsa.Builder.mk_lte
|
209
|
| ">=" -> pred Salsa.Builder.mk_gte
|
210
|
| _ -> assert false
|
211
|
)
|
212
|
in
|
213
|
op salsaX salsaY
|
214
|
| LT.Fun(unop, [x]) -> let salsaX = value_t2salsa_expr x in
|
215
|
Salsa.Builder.mk_uminus salsaX
|
216
|
|
217
|
| LT.Fun(f,_) -> raise (Salsa.Prelude.Error
|
218
|
("Unhandled function "^f^" in conversion to salsa expression"))
|
219
|
|
220
|
| LT.Array(_)
|
221
|
| LT.Access(_)
|
222
|
| LT.Power(_) -> raise (Salsa.Prelude.Error ("Unhandled construct in conversion to salsa expression"))
|
223
|
in
|
224
|
(* if debug then *)
|
225
|
(* Format.eprintf "value_t2salsa_expr: %a -> %a@ " *)
|
226
|
(* MC.pp_val vt *)
|
227
|
(* (fun fmt x -> Format.fprintf fmt "%s" (Salsa.Print.printExpression x)) res; *)
|
228
|
res
|
229
|
|
230
|
type var_decl = { vdecl: LT.var_decl; is_local: bool }
|
231
|
module VarEnv = Map.Make (struct type t = LT.ident let compare = compare end )
|
232
|
|
233
|
(* let is_local_var vars_env v = *)
|
234
|
(* try *)
|
235
|
(* (VarEnv.find v vars_env).is_local *)
|
236
|
(* with Not_found -> Format.eprintf "Impossible to find var %s@.@?" v; assert false *)
|
237
|
|
238
|
let get_var vars_env v =
|
239
|
try
|
240
|
VarEnv.find v vars_env
|
241
|
with Not_found -> Format.eprintf "Impossible to find var %s@.@?" v; assert false
|
242
|
|
243
|
let compute_vars_env m =
|
244
|
let env = VarEnv.empty in
|
245
|
let env =
|
246
|
List.fold_left
|
247
|
(fun accu v -> VarEnv.add v.LT.var_id {vdecl = v; is_local = false; } accu)
|
248
|
env
|
249
|
m.MC.mmemory
|
250
|
in
|
251
|
let env =
|
252
|
List.fold_left (
|
253
|
fun accu v -> VarEnv.add v.LT.var_id {vdecl = v; is_local = true; } accu
|
254
|
)
|
255
|
env
|
256
|
MC.(m.mstep.step_inputs@m.mstep.step_outputs@m.mstep.step_locals)
|
257
|
in
|
258
|
env
|
259
|
|
260
|
let rec salsa_expr2value_t vars_env cst_env e =
|
261
|
let salsa_expr2value_t = salsa_expr2value_t vars_env cst_env in
|
262
|
let binop op e1 e2 t =
|
263
|
let x = salsa_expr2value_t e1 in
|
264
|
let y = salsa_expr2value_t e2 in
|
265
|
MC.mk_val (LT.Fun (op, [x;y])) t
|
266
|
in
|
267
|
match e with
|
268
|
ST.Cst((ST.R(c,_),_),_) -> (* We project ranges into constants. We
|
269
|
forget about errors and provide the
|
270
|
mean/middle value of the interval
|
271
|
*)
|
272
|
let new_float = Num.float_of_num c in
|
273
|
(* let new_float = *)
|
274
|
(* if f1 = f2 then *)
|
275
|
(* f1 *)
|
276
|
(* else *)
|
277
|
(* (f1 +. f2) /. 2.0 *)
|
278
|
(* in *)
|
279
|
(* Log.report ~level:3 *)
|
280
|
(* (fun fmt -> Format.fprintf fmt "projecting [%.45f, %.45f] -> %.45f@ " f1 f2 new_float); *)
|
281
|
let cst =
|
282
|
let s =
|
283
|
if new_float = 0. then "0." else
|
284
|
(* We have to convert it into our format: int * int * real *)
|
285
|
let _ = Format.flush_str_formatter () in
|
286
|
Format.fprintf Format.str_formatter "%.50f" new_float;
|
287
|
Format.flush_str_formatter ()
|
288
|
in
|
289
|
Parser_lustre.signed_const Lexer_lustre.token (Lexing.from_string s)
|
290
|
in
|
291
|
MC.mk_val (LT.Cst(cst)) Type_predef.type_real
|
292
|
| ST.Id(id, _) ->
|
293
|
(* Format.eprintf "Looking for id=%s@.@?" id; *)
|
294
|
if List.mem_assoc id cst_env then (
|
295
|
let cst = List.assoc id cst_env in
|
296
|
(* Format.eprintf "Found cst = %a@.@?" Printers.pp_const cst; *)
|
297
|
MC.mk_val (LT.Cst cst) Type_predef.type_real
|
298
|
)
|
299
|
else
|
300
|
(* if is_const salsa_label then *)
|
301
|
(* MC.Cst(LT.Const_tag(get_const salsa_label)) *)
|
302
|
(* else *)
|
303
|
let var_id = try get_var vars_env id with Not_found -> assert false in
|
304
|
if var_id.is_local then
|
305
|
MC.mk_val (LT.LocalVar(var_id.vdecl)) var_id.vdecl.LT.var_type
|
306
|
else
|
307
|
MC.mk_val (LT.StateVar(var_id.vdecl)) var_id.vdecl.LT.var_type
|
308
|
| ST.Plus(x, y, _) -> binop "+" x y Type_predef.type_real
|
309
|
| ST.Minus(x, y, _) -> binop "-" x y Type_predef.type_real
|
310
|
| ST.Times(x, y, _) -> binop "*" x y Type_predef.type_real
|
311
|
| ST.Div(x, y, _) -> binop "/" x y Type_predef.type_real
|
312
|
| ST.Uminus(x,_) -> let x = salsa_expr2value_t x in
|
313
|
MC.mk_val (LT.Fun("uminus",[x])) Type_predef.type_real
|
314
|
| ST.IntOfBool(ST.Eq(x, y, _),_) -> binop "=" x y Type_predef.type_bool
|
315
|
| ST.IntOfBool(ST.Lt(x,y,_),_) -> binop "<" x y Type_predef.type_bool
|
316
|
| ST.IntOfBool(ST.Gt(x,y,_),_) -> binop ">" x y Type_predef.type_bool
|
317
|
| ST.IntOfBool(ST.Lte(x,y,_),_) -> binop "<=" x y Type_predef.type_bool
|
318
|
| ST.IntOfBool(ST.Gte(x,y,_),_) -> binop ">=" x y Type_predef.type_bool
|
319
|
| _ -> raise (Salsa.Prelude.Error "Entschuldigung, salsaExpr2value_t case not yet implemented")
|
320
|
|
321
|
|
322
|
let rec get_salsa_free_vars vars_env constEnv absenv e =
|
323
|
let f = get_salsa_free_vars vars_env constEnv absenv in
|
324
|
match e with
|
325
|
| ST.Id (id, _) ->
|
326
|
if not (List.mem_assoc id absenv) && not (List.mem_assoc id constEnv) then
|
327
|
Vars.singleton ((try VarEnv.find id vars_env with Not_found -> assert false).vdecl)
|
328
|
else
|
329
|
Vars.empty
|
330
|
| ST.Plus(x, y, _)
|
331
|
| ST.Minus(x, y, _)
|
332
|
| ST.Times(x, y, _)
|
333
|
| ST.Div(x, y, _)
|
334
|
| ST.IntOfBool(ST.Eq(x, y, _),_)
|
335
|
| ST.IntOfBool(ST.Lt(x,y,_),_)
|
336
|
| ST.IntOfBool(ST.Gt(x,y,_),_)
|
337
|
| ST.IntOfBool(ST.Lte(x,y,_),_)
|
338
|
| ST.IntOfBool(ST.Gte(x,y,_),_)
|
339
|
-> Vars.union (f x) (f y)
|
340
|
| ST.Uminus(x,_) -> f x
|
341
|
| ST.Cst _ -> Vars.empty
|
342
|
| _ -> assert false
|
343
|
|
344
|
(* Local Variables: *)
|
345
|
(* compile-command:"make -C ../../.." *)
|
346
|
(* End: *)
|