Project

General

Profile

Statistics
| Branch: | Tag: | Revision:

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

History | View | Annotate | Download (12.7 KB)

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

    
7
let debug = ref false
8

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

    
14

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

    
21
  let empty: r_t = Hashtbl.create 13
22

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

    
31

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

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

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

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

    
71
end
72

    
73
module FloatIntSalsa = 
74
struct
75
  type t = ST.abstractValue
76

    
77
  let pp fmt (f,r) =
78
    let fs, rs = (Salsa.Float.Domain.print (f,r)) in
79
    Format.fprintf fmt "%s + %s" fs rs 
80
(*    match f, r with
81
    | ST.I(a,b), ST.J(c,d) ->
82
      Format.fprintf fmt "[%f, %f] + [%s, %s]" a b (Num.string_of_num c) (Num.string_of_num d)
83
    | ST.I(a,b), ST.JInfty ->  Format.fprintf fmt "[%f, %f] + oo" a b 
84
    | ST.Empty, _ -> Format.fprintf fmt "???"
85

    
86
    | _ -> assert false
87
*)
88
  let union v1 v2 = Salsa.Float.Domain.join v1 v2
89
(*    match v1, v2 with
90
    |(ST.I(x1, x2), ST.J(y1, y2)), (ST.I(x1', x2'), ST.J(y1', y2')) ->
91
      ST.(I(min x1 x1', max x2 x2'), J(min y1 y1', max y2 y2'))
92
    | _ -> Format.eprintf "%a cup %a failed@.@?" pp v1 pp v2; assert false 
93
*)
94
  let inject cst = match cst with (* ATTENTION ATTENTION !!!!! Remettre les Num !!!! *) 
95
    | LT.Const_int(i)  -> Salsa.Builder.mk_cst (ST.R(float_of_int i (*Num.num_of_int i*), []), ST.R(float_of_int i (*Num.num_of_int i*), []))
96
    | LT.Const_real (c,e,s) -> (* TODO: this is incorrect. We should rather
97
				  compute the error associated to the float *)
98
       let r = float_of_string s  in
99
       let r = Salsa.Prelude.r_of_f_aux r in
100
       Salsa.Builder.mk_cst (Float.Domain.nnew r r)
101
	 
102
      (* let r = float_of_string s  in *)
103
      (* if r = 0. then *)
104
      (* 	Salsa.Builder.mk_cst (ST.R(-. min_float, min_float),Float.ulp (ST.R(-. min_float, min_float))) *)
105
      (* else *)
106
      (* 	Salsa.Builder.mk_cst (ST.I(r*.(1.-.epsilon_float),r*.(1.+.epsilon_float)),Float.ulp (ST.I(r,r))) *)
107
    | _ -> assert false
108
end
109

    
110
module RangesInt = Ranges (FloatIntSalsa)
111

    
112
module Vars = 
113
struct
114
  module VarSet = Set.Make (struct type t = LT.var_decl let compare x y = compare x.LT.var_id y.LT.var_id end)
115
  let real_vars vs = VarSet.filter (fun v -> Types.is_real_type v.LT.var_type) vs
116
  let of_list = List.fold_left (fun s e -> VarSet.add e s) VarSet.empty 
117

    
118
  include VarSet 
119

    
120
  let remove_list (set:t) (v_list: elt list) : t = List.fold_right VarSet.remove v_list set
121
  let pp fmt vs = Utils.fprintf_list ~sep:", " Printers.pp_var fmt (VarSet.elements vs)
122
end
123

    
124

    
125

    
126

    
127

    
128

    
129

    
130

    
131

    
132

    
133
(*************************************************************************************)
134
(*                 Converting values back and forth                                  *)
135
(*************************************************************************************)
136

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

    
182
    | MT.Fun(f,_)   -> raise (Salsa.Prelude.Error 
183
				("Unhandled function "^f^" in conversion to salsa expression"))
184
    
185
    | MT.Array(_) 
186
    | MT.Access(_)
187
    | MT.Power(_)   -> raise (Salsa.Prelude.Error ("Unhandled construct in conversion to salsa expression"))
188
  in
189
  (* if debug then *)
190
  (*   Format.eprintf "value_t2salsa_expr: %a -> %a@ " *)
191
  (*     MC.pp_val vt *)
192
  (*     (fun fmt x -> Format.fprintf fmt "%s" (Salsa.Print.printExpression x)) res; *)
193
  res
194

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

    
198
(* let is_local_var vars_env v = *)
199
(*   try *)
200
(*   (VarEnv.find v vars_env).is_local *)
201
(*   with Not_found -> Format.eprintf "Impossible to find var %s@.@?" v; assert false *)
202

    
203
let get_var vars_env v =
204
try
205
  VarEnv.find v vars_env
206
  with Not_found -> Format.eprintf "Impossible to find var %s@.@?" v; assert false
207

    
208
let compute_vars_env m =
209
  let env = VarEnv.empty in
210
  let env = 
211
    List.fold_left 
212
      (fun accu v -> VarEnv.add v.LT.var_id {vdecl = v; is_local = false; } accu) 
213
      env 
214
      m.MT.mmemory
215
  in
216
  let env = 
217
    List.fold_left (
218
      fun accu v -> VarEnv.add v.LT.var_id {vdecl = v; is_local = true; } accu
219
    )
220
      env
221
      MC.(m.MT.mstep.MT.step_inputs@m.MT.mstep.MT.step_outputs@m.MT.mstep.MT.step_locals)
222
  in
223
env
224

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

    
288

    
289

    
290
let rec get_salsa_free_vars vars_env constEnv absenv e =
291
  let f = get_salsa_free_vars vars_env constEnv absenv in
292
  match e with
293
  | ST.Id (id, _) -> 
294
    if not (List.mem_assoc id absenv) && not (List.mem_assoc id constEnv) then
295
      Vars.singleton ((try VarEnv.find id vars_env with Not_found -> assert false).vdecl) 
296
    else
297
      Vars.empty
298
  | ST.Plus(x, y, _)  
299
  | ST.Minus(x, y, _)
300
  | ST.Times(x, y, _)
301
  | ST.Div(x, y, _)
302
  | ST.IntOfBool(ST.Eq(x, y, _),_) 
303
  | ST.IntOfBool(ST.Lt(x,y,_),_)   
304
  | ST.IntOfBool(ST.Gt(x,y,_),_)   
305
  | ST.IntOfBool(ST.Lte(x,y,_),_)  
306
  | ST.IntOfBool(ST.Gte(x,y,_),_)  
307
    -> Vars.union (f x) (f y)
308
  | ST.Uminus(x,_)    -> f x
309
  | ST.Cst _ -> Vars.empty
310
  | _ -> assert false
311

    
312

    
313
module FormalEnv =
314
struct
315
  type fe_t = (LT.ident, (int * MT.value_t)) Hashtbl.t
316
  let cpt = ref 0
317

    
318
  exception NoDefinition of LT.var_decl
319
  (* Returns the expression associated to v in env *)
320
  let get_def (env: fe_t) v = 
321
    try 
322
      snd (Hashtbl.find env v.LT.var_id) 
323
    with Not_found -> raise (NoDefinition v)
324

    
325
  let fold f = Hashtbl.fold (fun k (_,v) accu -> f k v accu)
326
      
327
  let to_salsa constEnv formalEnv = 
328
    fold (fun id expr accu ->
329
      (id, value_t2salsa_expr constEnv expr)::accu
330
    ) formalEnv [] 
331

    
332
  let def constEnv vars_env (env: fe_t) d expr = 
333
    incr cpt;
334
    let fresh = Hashtbl.copy env in
335
    let expr_salsa = value_t2salsa_expr constEnv expr in
336
    let salsa_env = to_salsa constEnv env in
337
    let expr_salsa, _ = Salsa.Rewrite.substVars expr_salsa salsa_env 0 in
338
    let expr_salsa = Salsa.Analyzer.evalPartExpr expr_salsa salsa_env ([] (* no blacklisted vars *)) ([] (*no arrays *)) in
339
    let expr_lustrec = salsa_expr2value_t vars_env [] expr_salsa in
340
    Hashtbl.add fresh d.LT.var_id (!cpt, expr_lustrec); fresh
341

    
342
  let empty (): fe_t = Hashtbl.create 13
343

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

    
346

    
347
  let get_sort_fun env =
348
    let order = Hashtbl.fold (fun k (cpt, _) accu -> (k,cpt)::accu) env [] in
349
    fun v1 v2 -> 
350
      if List.mem_assoc v1.LT.var_id order && List.mem_assoc v2.LT.var_id order then
351
	if (List.assoc v1.LT.var_id order) <= (List.assoc v2.LT.var_id order) then 
352
	  -1
353
	else 
354
	  1
355
      else
356
	assert false
357
end
358

    
359
     
360
(* Local Variables: *)
361
(* compile-command:"make -C ../../.." *)
362
(* End: *)