Project

General

Profile

Statistics
| Branch: | Tag: | Revision:

lustrec / src / plugins / salsa / salsaDatatypes.ml @ ae7d913d

History | View | Annotate | Download (13.1 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 type VALUE =
17
sig
18
  type t
19
  val union: t -> t -> t
20
  val pp: Format.formatter -> t -> unit
21
    val leq: t -> t -> bool
22
end
23
  
24
module Ranges = 
25
  functor (Value: VALUE)  ->
26
struct
27
  module Value = Value
28
  type t = Value.t
29
  type r_t = (LT.ident, Value.t) Hashtbl.t
30

    
31
  let empty: r_t = Hashtbl.create 13
32

    
33
  (* Look for def of node i with inputs living in vtl_ranges, reinforce ranges
34
     to bound vdl: each output of node i *)
35
  let add_call ranges vdl id vtl_ranges = ranges (* TODO assert false.  On est
36
  						    pas obligé de faire
37
  						    qqchose. On peut supposer
38
  						    que les ranges sont donnés
39
  						    pour chaque noeud *)
40

    
41

    
42
  let pp fmt r =
43
    if Hashtbl.length r = 0 then
44
      Format.fprintf fmt "empty"
45
    else
46
      pp_hash ~sep:";" (fun k v fmt -> Format.fprintf fmt "%s -> %a" k Value.pp v) fmt r
47
  let pp_val = Value.pp
48

    
49
  let add_def ranges name r = 
50
    (* Format.eprintf "%s: declare %a@."  *)
51
    (* 	  x.LT.var_id *)
52
    (* 	  Value.pp r ; *)
53
	
54
    let fresh = Hashtbl.copy ranges in
55
    Hashtbl.add fresh name r; fresh
56

    
57
  let enlarge ranges name r =
58
    let fresh = Hashtbl.copy ranges in
59
    if Hashtbl.mem fresh name then
60
      Hashtbl.replace fresh name (Value.union r (Hashtbl.find fresh name))
61
    else
62
      Hashtbl.add fresh name r; 
63
    fresh
64
    
65

    
66
  (* Compute a join per variable *)  
67
  let merge ranges1 ranges2 = 
68
    (* Format.eprintf "Mergeing rangesint %a with %a@." pp ranges1 pp ranges2; *)
69
    let ranges = Hashtbl.copy ranges1 in
70
    Hashtbl.iter (fun k v -> 
71
      if Hashtbl.mem ranges k then (
72
	(* Format.eprintf "%s: %a union %a = %a@."  *)
73
	(*   k *)
74
	(*   Value.pp v  *)
75
	(*   Value.pp (Hashtbl.find ranges k) *)
76
	(*   Value.pp (Value.union v (Hashtbl.find ranges k)); *)
77
      Hashtbl.replace ranges k (Value.union v (Hashtbl.find ranges k))
78
    )
79
      else
80
	 Hashtbl.add ranges k v
81
    ) ranges2;
82
    (* Format.eprintf "Merge result %a@." pp ranges; *)
83
    ranges
84

    
85

    
86
  let to_abstract_env ranges =
87
    Hashtbl.fold 
88
      (fun id value accu -> (id,value)::accu) 
89
      ranges
90
      [] 
91
end
92

    
93
module FloatIntSalsa = 
94
struct
95
  type t = ST.abstractValue
96

    
97
  let pp fmt ((f,r):t) =
98
    let fs, rs = (Salsa.Float.Domain.print (f,r)) in
99
    Format.fprintf fmt "%s + %s" fs rs 
100
(*    match f, r with
101
    | ST.I(a,b), ST.J(c,d) ->
102
      Format.fprintf fmt "[%f, %f] + [%s, %s]" a b (Num.string_of_num c) (Num.string_of_num d)
103
    | ST.I(a,b), ST.JInfty ->  Format.fprintf fmt "[%f, %f] + oo" a b 
104
    | ST.Empty, _ -> Format.fprintf fmt "???"
105

    
106
    | _ -> assert false
107
*)
108
  let union v1 v2 = Salsa.Float.Domain.join v1 v2
109
(*    match v1, v2 with
110
    |(ST.I(x1, x2), ST.J(y1, y2)), (ST.I(x1', x2'), ST.J(y1', y2')) ->
111
      ST.(I(min x1 x1', max x2 x2'), J(min y1 y1', max y2 y2'))
112
    | _ -> Format.eprintf "%a cup %a failed@.@?" pp v1 pp v2; assert false 
113
*)
114
  let inject cst = match cst with  
115
    | LT.Const_int(i)  -> Salsa.Builder.mk_cst (Salsa.Float.Domain.inject_float (float_of_int i))
116
    | LT.Const_real (c,e,s) -> (* TODO: this is incorrect. We should rather
117
				  compute the error associated to the float *)
118
       let f = float_of_string s  in
119
       Salsa.Builder.mk_cst (Salsa.Float.Domain.inject_float f)
120
       
121
       (* let r = Salsa.Prelude.r_of_f_aux r in *)
122
       (* Salsa.Builder.mk_cst (Float.Domain.nnew r r) *)
123
	 
124
      (* let r = float_of_string s  in *)
125
      (* if r = 0. then *)
126
      (* 	Salsa.Builder.mk_cst (ST.R(-. min_float, min_float),Float.ulp (ST.R(-. min_float, min_float))) *)
127
      (* else *)
128
      (* 	Salsa.Builder.mk_cst (ST.I(r*.(1.-.epsilon_float),r*.(1.+.epsilon_float)),Float.ulp (ST.I(r,r))) *)
129
    | _ -> assert false
130

    
131
  let leq = Salsa.Float.feSseq
132
end
133

    
134
module RangesInt = Ranges (FloatIntSalsa)
135

    
136
module Vars = 
137
struct
138
  module VarSet = Set.Make (struct type t = LT.var_decl let compare x y = compare x.LT.var_id y.LT.var_id end)
139
  let real_vars vs = VarSet.filter (fun v -> Types.is_real_type v.LT.var_type) vs
140
  let of_list = List.fold_left (fun s e -> VarSet.add e s) VarSet.empty 
141

    
142
  include VarSet 
143

    
144
  let remove_list (set:t) (v_list: elt list) : t = List.fold_right VarSet.remove v_list set
145
  let pp fmt vs = Utils.fprintf_list ~sep:", " Printers.pp_var fmt (VarSet.elements vs)
146
end
147

    
148

    
149

    
150

    
151

    
152

    
153

    
154

    
155

    
156

    
157
(*************************************************************************************)
158
(*                 Converting values back and forth                                  *)
159
(*************************************************************************************)
160

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

    
205
    | MT.Fun(f,_)   -> raise (Salsa.Prelude.Error 
206
				("Unhandled function "^f^" in conversion to salsa expression"))
207
    
208
    | MT.Array(_) 
209
    | MT.Access(_)
210
    | MT.Power(_)   -> raise (Salsa.Prelude.Error ("Unhandled construct in conversion to salsa expression"))
211
  in
212
  (* if debug then *)
213
  (*   Format.eprintf "value_t2salsa_expr: %a -> %a@ " *)
214
  (*     MC.pp_val vt *)
215
  (*     (fun fmt x -> Format.fprintf fmt "%s" (Salsa.Print.printExpression x)) res; *)
216
  res
217

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

    
221
(* let is_local_var vars_env v = *)
222
(*   try *)
223
(*   (VarEnv.find v vars_env).is_local *)
224
(*   with Not_found -> Format.eprintf "Impossible to find var %s@.@?" v; assert false *)
225

    
226
let get_var vars_env v =
227
try
228
  VarEnv.find v vars_env
229
with Not_found -> Format.eprintf "Impossible to find var %s in var env %a@ " v
230
  (Utils.fprintf_list ~sep:", " (fun fmt (id, _) -> Format.pp_print_string fmt id)) (VarEnv.bindings vars_env) 
231
  ; assert false
232

    
233
let compute_vars_env m =
234
  let env = VarEnv.empty in
235
  let env = 
236
    List.fold_left 
237
      (fun accu v -> VarEnv.add v.LT.var_id {vdecl = v; is_local = false; } accu) 
238
      env 
239
      m.MT.mmemory
240
  in
241
  let env = 
242
    List.fold_left (
243
      fun accu v -> VarEnv.add v.LT.var_id {vdecl = v; is_local = true; } accu
244
    )
245
      env
246
      MC.(m.MT.mstep.MT.step_inputs@m.MT.mstep.MT.step_outputs@m.MT.mstep.MT.step_locals)
247
  in
248
env
249

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

    
311

    
312

    
313
let rec get_salsa_free_vars vars_env constEnv absenv e =
314
  let f = get_salsa_free_vars vars_env constEnv absenv in
315
  match e with
316
  | ST.Id (id, _) -> 
317
    if not (List.mem_assoc id absenv) && not (List.mem_assoc id constEnv) then
318
      Vars.singleton ((try VarEnv.find id vars_env with Not_found -> assert false).vdecl) 
319
    else
320
      Vars.empty
321
  | ST.Plus(x, y, _)  
322
  | ST.Minus(x, y, _)
323
  | ST.Times(x, y, _)
324
  | ST.Div(x, y, _)
325
  | ST.IntOfBool(ST.Eq(x, y, _),_) 
326
  | ST.IntOfBool(ST.Lt(x,y,_),_)   
327
  | ST.IntOfBool(ST.Gt(x,y,_),_)   
328
  | ST.IntOfBool(ST.Lte(x,y,_),_)  
329
  | ST.IntOfBool(ST.Gte(x,y,_),_)  
330
    -> Vars.union (f x) (f y)
331
  | ST.Uminus(x,_)    -> f x
332
  | ST.Cst _ -> Vars.empty
333
  | _ -> assert false
334

    
335

    
336
module FormalEnv =
337
struct
338
  type fe_t = (LT.ident, (int * MT.value_t)) Hashtbl.t
339
  let cpt = ref 0
340

    
341
  exception NoDefinition of LT.var_decl
342
  (* Returns the expression associated to v in env *)
343
  let get_def (env: fe_t) v = 
344
    try 
345
      snd (Hashtbl.find env v.LT.var_id) 
346
    with Not_found -> raise (NoDefinition v)
347

    
348
  let fold f = Hashtbl.fold (fun k (_,v) accu -> f k v accu)
349
      
350
  let to_salsa constEnv formalEnv = 
351
    fold (fun id expr accu ->
352
      (id, value_t2salsa_expr constEnv expr)::accu
353
    ) formalEnv [] 
354

    
355
  let def constEnv vars_env (env: fe_t) d expr = 
356
    incr cpt;
357
    let fresh = Hashtbl.copy env in
358
    let expr_salsa = value_t2salsa_expr constEnv expr in
359
    let salsa_env = to_salsa constEnv env in
360
    let expr_salsa, _ = Salsa.Rewrite.substVars expr_salsa salsa_env 0 in
361
    let expr_salsa = Salsa.Analyzer.evalPartExpr expr_salsa salsa_env ([] (* no blacklisted vars *)) ([] (*no arrays *)) in
362
    let expr_lustrec = salsa_expr2value_t vars_env [] expr_salsa in
363
    Hashtbl.add fresh d.LT.var_id (!cpt, expr_lustrec); fresh
364

    
365
  let empty (): fe_t = Hashtbl.create 13
366

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

    
369

    
370
  let get_sort_fun env =
371
    let order = Hashtbl.fold (fun k (cpt, _) accu -> (k,cpt)::accu) env [] in
372
    fun v1 v2 -> 
373
      if List.mem_assoc v1.LT.var_id order && List.mem_assoc v2.LT.var_id order then
374
	if (List.assoc v1.LT.var_id order) <= (List.assoc v2.LT.var_id order) then 
375
	  -1
376
	else 
377
	  1
378
      else
379
	assert false
380
end
381

    
382
     
383
(* Local Variables: *)
384
(* compile-command:"make -C ../../.." *)
385
(* End: *)