Project

General

Profile

Statistics
| Branch: | Tag: | Revision:

lustrec / src / plugins / salsa / salsaDatatypes.ml @ 151117f7

History | View | Annotate | Download (13 KB)

1
module LT = Lustre_types
2
module MT = Machine_code_types
3
module MC = Machine_code_common
4
module ST = Salsa.Types
5

    
6
let debug = ref false
7

    
8
let _ = Salsa.Prelude.sliceSize := 5 
9
  
10
let pp_hash ~sep f fmt r = 
11
  Format.fprintf fmt "[@[<v>";
12
  Hashtbl.iter (fun k v -> Format.fprintf fmt "%t%s@ " (f k v) sep) r;
13
  Format.fprintf fmt "]@]";
14

    
15

    
16
module Ranges = 
17
  functor (Value: sig type t val union: t -> t -> t val pp: Format.formatter -> t -> unit end)  ->
18
struct
19
  type t = Value.t
20
  type r_t = (LT.ident, Value.t) Hashtbl.t
21

    
22
  let empty: r_t = Hashtbl.create 13
23

    
24
  (* Look for def of node i with inputs living in vtl_ranges, reinforce ranges
25
     to bound vdl: each output of node i *)
26
  let add_call ranges vdl id vtl_ranges = ranges (* TODO assert false.  On est
27
  						    pas obligé de faire
28
  						    qqchose. On peut supposer
29
  						    que les ranges sont donnés
30
  						    pour chaque noeud *)
31

    
32

    
33
  let pp = pp_hash ~sep:";" (fun k v fmt -> Format.fprintf fmt "%s -> %a" k Value.pp v) 
34
  let pp_val = Value.pp
35

    
36
  let add_def ranges name r = 
37
    (* Format.eprintf "%s: declare %a@."  *)
38
    (* 	  x.LT.var_id *)
39
    (* 	  Value.pp r ; *)
40
	
41
    let fresh = Hashtbl.copy ranges in
42
    Hashtbl.add fresh name r; fresh
43

    
44
  let enlarge ranges name r =
45
    let fresh = Hashtbl.copy ranges in
46
    if Hashtbl.mem fresh name then
47
      Hashtbl.replace fresh name (Value.union r (Hashtbl.find fresh name))
48
    else
49
      Hashtbl.add fresh name r; 
50
    fresh
51
    
52

    
53
  (* Compute a join per variable *)  
54
  let merge ranges1 ranges2 = 
55
    (* Format.eprintf "Mergeing rangesint %a with %a@." pp ranges1 pp ranges2; *)
56
    let ranges = Hashtbl.copy ranges1 in
57
    Hashtbl.iter (fun k v -> 
58
      if Hashtbl.mem ranges k then (
59
	(* Format.eprintf "%s: %a union %a = %a@."  *)
60
	(*   k *)
61
	(*   Value.pp v  *)
62
	(*   Value.pp (Hashtbl.find ranges k) *)
63
	(*   Value.pp (Value.union v (Hashtbl.find ranges k)); *)
64
      Hashtbl.replace ranges k (Value.union v (Hashtbl.find ranges k))
65
    )
66
      else
67
	 Hashtbl.add ranges k v
68
    ) ranges2;
69
    (* Format.eprintf "Merge result %a@." pp ranges; *)
70
    ranges
71

    
72

    
73
  let to_abstract_env ranges =
74
    Hashtbl.fold 
75
      (fun id value accu -> (id,value)::accu) 
76
      ranges
77
      [] 
78
end
79

    
80
module FloatIntSalsa = 
81
struct
82
  type t = ST.abstractValue
83

    
84
  let pp fmt ((f,r):t) =
85
    let fs, rs = (Salsa.Float.Domain.print (f,r)) in
86
    Format.fprintf fmt "%s + %s" fs rs 
87
(*    match f, r with
88
    | ST.I(a,b), ST.J(c,d) ->
89
      Format.fprintf fmt "[%f, %f] + [%s, %s]" a b (Num.string_of_num c) (Num.string_of_num d)
90
    | ST.I(a,b), ST.JInfty ->  Format.fprintf fmt "[%f, %f] + oo" a b 
91
    | ST.Empty, _ -> Format.fprintf fmt "???"
92

    
93
    | _ -> assert false
94
*)
95
  let union v1 v2 = Salsa.Float.Domain.join v1 v2
96
(*    match v1, v2 with
97
    |(ST.I(x1, x2), ST.J(y1, y2)), (ST.I(x1', x2'), ST.J(y1', y2')) ->
98
      ST.(I(min x1 x1', max x2 x2'), J(min y1 y1', max y2 y2'))
99
    | _ -> Format.eprintf "%a cup %a failed@.@?" pp v1 pp v2; assert false 
100
*)
101
  let inject cst = match cst with  
102
    | LT.Const_int(i)  -> Salsa.Builder.mk_cst (Salsa.Float.Domain.inject_float (float_of_int i))
103
    | LT.Const_real (c,e,s) -> (* TODO: this is incorrect. We should rather
104
				  compute the error associated to the float *)
105
       let f = float_of_string s  in
106
       Salsa.Builder.mk_cst (Salsa.Float.Domain.inject_float f)
107
       
108
       (* let r = Salsa.Prelude.r_of_f_aux r in *)
109
       (* Salsa.Builder.mk_cst (Float.Domain.nnew r r) *)
110
	 
111
      (* let r = float_of_string s  in *)
112
      (* if r = 0. then *)
113
      (* 	Salsa.Builder.mk_cst (ST.R(-. min_float, min_float),Float.ulp (ST.R(-. min_float, min_float))) *)
114
      (* else *)
115
      (* 	Salsa.Builder.mk_cst (ST.I(r*.(1.-.epsilon_float),r*.(1.+.epsilon_float)),Float.ulp (ST.I(r,r))) *)
116
    | _ -> assert false
117
end
118

    
119
module RangesInt = Ranges (FloatIntSalsa)
120

    
121
module Vars = 
122
struct
123
  module VarSet = Set.Make (struct type t = LT.var_decl let compare x y = compare x.LT.var_id y.LT.var_id end)
124
  let real_vars vs = VarSet.filter (fun v -> Types.is_real_type v.LT.var_type) vs
125
  let of_list = List.fold_left (fun s e -> VarSet.add e s) VarSet.empty 
126

    
127
  include VarSet 
128

    
129
  let remove_list (set:t) (v_list: elt list) : t = List.fold_right VarSet.remove v_list set
130
  let pp fmt vs = Utils.fprintf_list ~sep:", " Printers.pp_var fmt (VarSet.elements vs)
131
end
132

    
133

    
134

    
135

    
136

    
137

    
138

    
139

    
140

    
141

    
142
(*************************************************************************************)
143
(*                 Converting values back and forth                                  *)
144
(*************************************************************************************)
145

    
146
let rec value_t2salsa_expr constEnv vt = 
147
  let value_t2salsa_expr = value_t2salsa_expr constEnv in
148
  let res = 
149
    match vt.MT.value_desc with
150
    (* | LT.Cst(LT.Const_tag(t) as c)   ->  *)
151
    (*   Format.eprintf "v2s: cst tag@."; *)
152
    (*   if List.mem_assoc t constEnv then ( *)
153
    (* 	Format.eprintf "trouvé la constante %s: %a@ " t Printers.pp_const c; *)
154
    (* 	FloatIntSalsa.inject (List.assoc t constEnv) *)
155
    (*   ) *)
156
    (*   else (     *)
157
    (* 	Format.eprintf "Const tag %s unhandled@.@?" t ; *)
158
    (* 	raise (Salsa.Prelude.Error ("Entschuldigung6, constant tag not yet implemented")) *)
159
    (*   ) *)
160
    | MT.Cst(cst)                ->        (* Format.eprintf "v2s: cst tag 2: %a@." Printers.pp_const cst;  *)FloatIntSalsa.inject cst
161
    | MT.LocalVar(v)            
162
    | MT.StateVar(v)            ->       (* Format.eprintf "v2s: var %s@." v.LT.var_id; *) 
163
      let sel_fun = (fun (vname, _) -> v.LT.var_id = vname) in
164
      if List.exists sel_fun  constEnv then
165
	let _, cst = List.find sel_fun constEnv in
166
	FloatIntSalsa.inject cst
167
      else
168
	let id = v.LT.var_id in
169
				   Salsa.Builder.mk_id id
170
    | MT.Fun(binop, [x;y])      -> let salsaX = value_t2salsa_expr x in
171
				   let salsaY = value_t2salsa_expr y in
172
				   let op = (
173
				     let pred f x y = Salsa.Builder.mk_int_of_bool (f x y) in
174
				     match binop with
175
				     | "+" -> Salsa.Builder.mk_plus
176
				     | "-" -> Salsa.Builder.mk_minus
177
				     | "*" -> Salsa.Builder.mk_times
178
				     | "/" -> Salsa.Builder.mk_div
179
				     | "=" -> pred Salsa.Builder.mk_eq
180
				     | "<" -> pred Salsa.Builder.mk_lt
181
				     | ">" -> pred Salsa.Builder.mk_gt
182
				     | "<=" -> pred Salsa.Builder.mk_lte
183
				     | ">=" -> pred Salsa.Builder.mk_gte
184
				     | _ -> assert false
185
				   )
186
				   in
187
				   op salsaX salsaY 
188
    | MT.Fun(unop, [x])         -> let salsaX = value_t2salsa_expr x in
189
				   Salsa.Builder.mk_uminus salsaX
190

    
191
    | MT.Fun(f,_)   -> raise (Salsa.Prelude.Error 
192
				("Unhandled function "^f^" in conversion to salsa expression"))
193
    
194
    | MT.Array(_) 
195
    | MT.Access(_)
196
    | MT.Power(_)   -> raise (Salsa.Prelude.Error ("Unhandled construct in conversion to salsa expression"))
197
  in
198
  (* if debug then *)
199
  (*   Format.eprintf "value_t2salsa_expr: %a -> %a@ " *)
200
  (*     MC.pp_val vt *)
201
  (*     (fun fmt x -> Format.fprintf fmt "%s" (Salsa.Print.printExpression x)) res; *)
202
  res
203

    
204
type var_decl = { vdecl: LT.var_decl; is_local: bool }
205
module VarEnv = Map.Make (struct type t = LT.ident let compare = compare end )
206

    
207
(* let is_local_var vars_env v = *)
208
(*   try *)
209
(*   (VarEnv.find v vars_env).is_local *)
210
(*   with Not_found -> Format.eprintf "Impossible to find var %s@.@?" v; assert false *)
211

    
212
let get_var vars_env v =
213
try
214
  VarEnv.find v vars_env
215
with Not_found -> Format.eprintf "Impossible to find var %s in var env %a@.@?" v
216
  (Utils.fprintf_list ~sep:", " (fun fmt (id, _) -> Format.pp_print_string fmt id)) (VarEnv.bindings vars_env) 
217
  ; assert false
218

    
219
let compute_vars_env m =
220
  let env = VarEnv.empty in
221
  let env = 
222
    List.fold_left 
223
      (fun accu v -> VarEnv.add v.LT.var_id {vdecl = v; is_local = false; } accu) 
224
      env 
225
      m.MT.mmemory
226
  in
227
  let env = 
228
    List.fold_left (
229
      fun accu v -> VarEnv.add v.LT.var_id {vdecl = v; is_local = true; } accu
230
    )
231
      env
232
      MC.(m.MT.mstep.MT.step_inputs@m.MT.mstep.MT.step_outputs@m.MT.mstep.MT.step_locals)
233
  in
234
env
235

    
236
let rec salsa_expr2value_t vars_env cst_env e  = 
237
  (* let e =   Float.evalPartExpr e [] [] in *)
238
  let salsa_expr2value_t = salsa_expr2value_t vars_env cst_env in
239
  let binop op e1 e2 t = 
240
    let x = salsa_expr2value_t e1 in
241
    let y = salsa_expr2value_t e2 in                    
242
    MC.mk_val (MT.Fun (op, [x;y])) t
243
  in
244
  match e with
245
    ST.Cst(abs_val,_)     -> (* We project ranges into constants. We
246
					forget about errors and provide the
247
					mean/middle value of the interval
248
			      *)
249
    let new_float = Salsa.Float.Domain.to_float abs_val in
250
      (* let  new_float = Salsa.NumMartel.float_of_num c in *)
251
      (* let new_float =  *)
252
      (* 	if f1 = f2 then *)
253
      (* 	  f1 *)
254
      (* 	else *)
255
      (* 	  (f1 +. f2) /. 2.0  *)
256
      (* in *)
257
      (* Log.report ~level:3 *)
258
      (* 	(fun fmt -> Format.fprintf fmt  "projecting [%.45f, %.45f] -> %.45f@ " f1 f2 new_float); *)
259
      let cst =  
260
	let s = 
261
	  if new_float = 0. then "0." else
262
	    (* We have to convert it into our format: int * int * real *)
263
	    (* string_of_float new_float *) 
264
	    let _ = Format.flush_str_formatter () in
265
	    Format.fprintf Format.str_formatter "%.11f" new_float;
266
	    Format.flush_str_formatter ()  
267
	in
268
	Parser_lustre.signed_const Lexer_lustre.token (Lexing.from_string s) 
269
      in
270
      MC.mk_val (MT.Cst(cst)) Type_predef.type_real
271
  | ST.Id(id, _)          -> 
272
    (* Format.eprintf "Looking for id=%s@.@?" id; *)
273
     if List.mem_assoc id cst_env then (
274
       let cst = List.assoc id cst_env in
275
      (* Format.eprintf "Found cst = %a@.@?" Printers.pp_const cst; *)
276
       MC.mk_val (MT.Cst cst) Type_predef.type_real
277
     )
278
     else
279
      (* if is_const salsa_label then *)
280
      (*   MC.Cst(LT.Const_tag(get_const salsa_label)) *)
281
      (* else *) 
282
       let var_id = try get_var vars_env id with Not_found -> assert false in
283
       if var_id.is_local then
284
	 MC.mk_val (MT.LocalVar(var_id.vdecl)) var_id.vdecl.LT.var_type
285
       else
286
	 MC.mk_val (MT.StateVar(var_id.vdecl)) var_id.vdecl.LT.var_type
287
  | ST.Plus(x, y, _)               -> binop "+" x y Type_predef.type_real
288
  | ST.Minus(x, y, _)              -> binop "-" x y Type_predef.type_real
289
  | ST.Times(x, y, _)              -> binop "*" x y Type_predef.type_real
290
  | ST.Div(x, y, _)                -> binop "/" x y Type_predef.type_real
291
  | ST.Uminus(x,_)                 -> let x = salsa_expr2value_t x in
292
				      MC.mk_val (MT.Fun("uminus",[x])) Type_predef.type_real
293
  | ST.IntOfBool(ST.Eq(x, y, _),_) -> binop "=" x y Type_predef.type_bool
294
  | ST.IntOfBool(ST.Lt(x,y,_),_)   -> binop "<" x y Type_predef.type_bool
295
  | ST.IntOfBool(ST.Gt(x,y,_),_)   -> binop ">" x y Type_predef.type_bool
296
  | ST.IntOfBool(ST.Lte(x,y,_),_)  -> binop "<=" x y Type_predef.type_bool
297
  | ST.IntOfBool(ST.Gte(x,y,_),_)  -> binop ">=" x y Type_predef.type_bool
298
  | _      -> raise (Salsa.Prelude.Error "Entschuldigung, salsaExpr2value_t case not yet implemented")
299

    
300

    
301

    
302
let rec get_salsa_free_vars vars_env constEnv absenv e =
303
  let f = get_salsa_free_vars vars_env constEnv absenv in
304
  match e with
305
  | ST.Id (id, _) -> 
306
    if not (List.mem_assoc id absenv) && not (List.mem_assoc id constEnv) then
307
      Vars.singleton ((try VarEnv.find id vars_env with Not_found -> assert false).vdecl) 
308
    else
309
      Vars.empty
310
  | ST.Plus(x, y, _)  
311
  | ST.Minus(x, y, _)
312
  | ST.Times(x, y, _)
313
  | ST.Div(x, y, _)
314
  | ST.IntOfBool(ST.Eq(x, y, _),_) 
315
  | ST.IntOfBool(ST.Lt(x,y,_),_)   
316
  | ST.IntOfBool(ST.Gt(x,y,_),_)   
317
  | ST.IntOfBool(ST.Lte(x,y,_),_)  
318
  | ST.IntOfBool(ST.Gte(x,y,_),_)  
319
    -> Vars.union (f x) (f y)
320
  | ST.Uminus(x,_)    -> f x
321
  | ST.Cst _ -> Vars.empty
322
  | _ -> assert false
323

    
324

    
325
module FormalEnv =
326
struct
327
  type fe_t = (LT.ident, (int * MT.value_t)) Hashtbl.t
328
  let cpt = ref 0
329

    
330
  exception NoDefinition of LT.var_decl
331
  (* Returns the expression associated to v in env *)
332
  let get_def (env: fe_t) v = 
333
    try 
334
      snd (Hashtbl.find env v.LT.var_id) 
335
    with Not_found -> raise (NoDefinition v)
336

    
337
  let fold f = Hashtbl.fold (fun k (_,v) accu -> f k v accu)
338
      
339
  let to_salsa constEnv formalEnv = 
340
    fold (fun id expr accu ->
341
      (id, value_t2salsa_expr constEnv expr)::accu
342
    ) formalEnv [] 
343

    
344
  let def constEnv vars_env (env: fe_t) d expr = 
345
    incr cpt;
346
    let fresh = Hashtbl.copy env in
347
    let expr_salsa = value_t2salsa_expr constEnv expr in
348
    let salsa_env = to_salsa constEnv env in
349
    let expr_salsa, _ = Salsa.Rewrite.substVars expr_salsa salsa_env 0 in
350
    let expr_salsa = Salsa.Analyzer.evalPartExpr expr_salsa salsa_env ([] (* no blacklisted vars *)) ([] (*no arrays *)) in
351
    let expr_lustrec = salsa_expr2value_t vars_env [] expr_salsa in
352
    Hashtbl.add fresh d.LT.var_id (!cpt, expr_lustrec); fresh
353

    
354
  let empty (): fe_t = Hashtbl.create 13
355

    
356
  let pp fmt env = pp_hash ~sep:";" (fun k (_,v) fmt -> Format.fprintf fmt "%s -> %a" k MC.pp_val v) fmt env
357

    
358

    
359
  let get_sort_fun env =
360
    let order = Hashtbl.fold (fun k (cpt, _) accu -> (k,cpt)::accu) env [] in
361
    fun v1 v2 -> 
362
      if List.mem_assoc v1.LT.var_id order && List.mem_assoc v2.LT.var_id order then
363
	if (List.assoc v1.LT.var_id order) <= (List.assoc v2.LT.var_id order) then 
364
	  -1
365
	else 
366
	  1
367
      else
368
	assert false
369
end
370

    
371
     
372
(* Local Variables: *)
373
(* compile-command:"make -C ../../.." *)
374
(* End: *)