lustrec / src / normalization.ml @ b1655a21
History  View  Annotate  Download (16.3 KB)
1 
(********************************************************************) 

2 
(* *) 
3 
(* The LustreC compiler toolset / The LustreC Development Team *) 
4 
(* Copyright 2012   ONERA  CNRS  INPT *) 
5 
(* *) 
6 
(* LustreC is free software, distributed WITHOUT ANY WARRANTY *) 
7 
(* under the terms of the GNU Lesser General Public License *) 
8 
(* version 2.1. *) 
9 
(* *) 
10 
(********************************************************************) 
11  
12 
open Utils 
13 
open LustreSpec 
14 
open Corelang 
15 
open Format 
16  
17 
let expr_true loc ck = 
18 
{ expr_tag = Utils.new_tag (); 
19 
expr_desc = Expr_const (Const_tag tag_true); 
20 
expr_type = Type_predef.type_bool; 
21 
expr_clock = ck; 
22 
expr_delay = Delay.new_var (); 
23 
expr_annot = None; 
24 
expr_loc = loc } 
25  
26 
let expr_false loc ck = 
27 
{ expr_tag = Utils.new_tag (); 
28 
expr_desc = Expr_const (Const_tag tag_false); 
29 
expr_type = Type_predef.type_bool; 
30 
expr_clock = ck; 
31 
expr_delay = Delay.new_var (); 
32 
expr_annot = None; 
33 
expr_loc = loc } 
34  
35 
let expr_once loc ck = 
36 
{ expr_tag = Utils.new_tag (); 
37 
expr_desc = Expr_arrow (expr_true loc ck, expr_false loc ck); 
38 
expr_type = Type_predef.type_bool; 
39 
expr_clock = ck; 
40 
expr_delay = Delay.new_var (); 
41 
expr_annot = None; 
42 
expr_loc = loc } 
43  
44 
let is_expr_once = 
45 
let dummy_expr_once = expr_once Location.dummy_loc (Clocks.new_var true) in 
46 
fun expr > Corelang.is_eq_expr expr dummy_expr_once 
47  
48 
let unfold_arrow expr = 
49 
match expr.expr_desc with 
50 
 Expr_arrow (e1, e2) > 
51 
let loc = expr.expr_loc in 
52 
let ck = List.hd (Clocks.clock_list_of_clock expr.expr_clock) in 
53 
{ expr with expr_desc = Expr_ite (expr_once loc ck, e1, e2) } 
54 
 _ > assert false 
55  
56 
let unfold_arrow_active = ref true 
57 
let cpt_fresh = ref 0 
58  
59 
(* Generate a new local [node] variable *) 
60 
let mk_fresh_var node loc ty ck = 
61 
let vars = get_node_vars node in 
62 
let rec aux () = 
63 
incr cpt_fresh; 
64 
let s = Printf.sprintf "__%s_%d" node.node_id !cpt_fresh in 
65 
if List.exists (fun v > v.var_id = s) vars then aux () else 
66 
{ 
67 
var_id = s; 
68 
var_dec_type = dummy_type_dec; 
69 
var_dec_clock = dummy_clock_dec; 
70 
var_dec_const = false; 
71 
var_type = ty; 
72 
var_clock = ck; 
73 
var_loc = loc 
74 
} 
75 
in aux () 
76  
77 
(* Generate a new ident expression from a declared variable *) 
78 
let mk_ident_expr v = 
79 
{ expr_tag = new_tag (); 
80 
expr_desc = Expr_ident v.var_id; 
81 
expr_type = v.var_type; 
82 
expr_clock = v.var_clock; 
83 
expr_delay = Delay.new_var (); 
84 
expr_annot = None; 
85 
expr_loc = v.var_loc } 
86  
87 
(* Get the equation in [defs] with [expr] as rhs, if any *) 
88 
let get_expr_alias defs expr = 
89 
try Some (List.find (fun eq > is_eq_expr eq.eq_rhs expr) defs) 
90 
with 
91 
Not_found > None 
92  
93 
(* Replace [expr] with (tuple of) [locals] *) 
94 
let replace_expr locals expr = 
95 
match locals with 
96 
 [] > assert false 
97 
 [v] > { expr with 
98 
expr_tag = Utils.new_tag (); 
99 
expr_desc = Expr_ident v.var_id } 
100 
 _ > { expr with 
101 
expr_tag = Utils.new_tag (); 
102 
expr_desc = Expr_tuple (List.map mk_ident_expr locals) } 
103  
104 
let unfold_offsets e offsets = 
105 
let add_offset e d = 
106 
(*Format.eprintf "add_offset %a %a@." Dimension.pp_dimension (Types.array_type_dimension e.expr_type) Dimension.pp_dimension d;*) 
107 
{ e with 
108 
expr_tag = Utils.new_tag (); 
109 
expr_loc = d.Dimension.dim_loc; 
110 
expr_type = Types.array_element_type e.expr_type; 
111 
expr_desc = Expr_access (e, d) } in 
112 
List.fold_left add_offset e offsets 
113  
114 
(* Create an alias for [expr], if none exists yet *) 
115 
let mk_expr_alias node (defs, vars) expr = 
116 
(*Format.eprintf "mk_expr_alias %a %a %a@." Printers.pp_expr expr Types.print_ty expr.expr_type Clocks.print_ck expr.expr_clock;*) 
117 
match get_expr_alias defs expr with 
118 
 Some eq > 
119 
let aliases = List.map (fun id > List.find (fun v > v.var_id = id) vars) eq.eq_lhs in 
120 
(defs, vars), replace_expr aliases expr 
121 
 None > 
122 
let new_aliases = 
123 
List.map2 
124 
(mk_fresh_var node expr.expr_loc) 
125 
(Types.type_list_of_type expr.expr_type) 
126 
(Clocks.clock_list_of_clock expr.expr_clock) in 
127 
let new_def = 
128 
mkeq expr.expr_loc (List.map (fun v > v.var_id) new_aliases, expr) 
129 
in (new_def::defs, new_aliases@vars), replace_expr new_aliases expr 
130  
131 
(* Create an alias for [expr], if [expr] is not already an alias (i.e. an ident) 
132 
and [opt] is true *) 
133 
let mk_expr_alias_opt opt node defvars expr = 
134 
match expr.expr_desc with 
135 
 Expr_ident alias > 
136 
defvars, expr 
137 
 _ > 
138 
if opt 
139 
then 
140 
mk_expr_alias node defvars expr 
141 
else 
142 
defvars, expr 
143  
144 
(* Create a (normalized) expression from [ref_e], 
145 
replacing description with [norm_d], 
146 
taking propagated [offsets] into account 
147 
in order to change expression type *) 
148 
let mk_norm_expr offsets ref_e norm_d = 
149 
let drop_array_type ty = 
150 
Types.map_tuple_type Types.array_element_type ty in 
151 
{ ref_e with 
152 
expr_desc = norm_d; 
153 
expr_type = Utils.repeat (List.length offsets) drop_array_type ref_e.expr_type } 
154  
155 
(* normalize_<foo> : defs * used vars > <foo> > (updated defs * updated vars) * normalized <foo> *) 
156 
let rec normalize_list alias node offsets norm_element defvars elist = 
157 
List.fold_right 
158 
(fun t (defvars, qlist) > 
159 
let defvars, norm_t = norm_element alias node offsets defvars t in 
160 
(defvars, norm_t :: qlist) 
161 
) elist (defvars, []) 
162  
163 
let rec normalize_expr ?(alias=true) node offsets defvars expr = 
164 
(* Format.eprintf "normalize %B %a [%a]@." alias Printers.pp_expr expr (Utils.fprintf_list ~sep:"," Dimension.pp_dimension) offsets;*) 
165 
match expr.expr_desc with 
166 
 Expr_const _ 
167 
 Expr_ident _ > defvars, unfold_offsets expr offsets 
168 
 Expr_array elist > 
169 
let defvars, norm_elist = normalize_list alias node offsets (fun _ > normalize_array_expr ~alias:true) defvars elist in 
170 
let norm_expr = mk_norm_expr offsets expr (Expr_array norm_elist) in 
171 
mk_expr_alias_opt alias node defvars norm_expr 
172 
 Expr_power (e1, d) when offsets = [] > 
173 
let defvars, norm_e1 = normalize_expr node offsets defvars e1 in 
174 
let norm_expr = mk_norm_expr offsets expr (Expr_power (norm_e1, d)) in 
175 
mk_expr_alias_opt alias node defvars norm_expr 
176 
 Expr_power (e1, d) > 
177 
normalize_expr ~alias:alias node (List.tl offsets) defvars e1 
178 
 Expr_access (e1, d) > 
179 
normalize_expr ~alias:alias node (d::offsets) defvars e1 
180 
 Expr_tuple elist > 
181 
let defvars, norm_elist = 
182 
normalize_list alias node offsets (fun alias > normalize_expr ~alias:alias) defvars elist in 
183 
defvars, mk_norm_expr offsets expr (Expr_tuple norm_elist) 
184 
 Expr_appl (id, args, None) 
185 
when Basic_library.is_internal_fun id 
186 
&& Types.is_array_type expr.expr_type > 
187 
let defvars, norm_args = 
188 
normalize_list 
189 
alias 
190 
node 
191 
offsets 
192 
(fun _ > normalize_array_expr ~alias:true) 
193 
defvars 
194 
(expr_list_of_expr args) 
195 
in 
196 
defvars, mk_norm_expr offsets expr (Expr_appl (id, expr_of_expr_list args.expr_loc norm_args, None)) 
197 
 Expr_appl (id, args, None) when Basic_library.is_internal_fun id > 
198 
let defvars, norm_args = normalize_expr ~alias:true node offsets defvars args in 
199 
defvars, mk_norm_expr offsets expr (Expr_appl (id, norm_args, None)) 
200 
 Expr_appl (id, args, r) > 
201 
let defvars, norm_args = normalize_expr node [] defvars args in 
202 
let norm_expr = mk_norm_expr [] expr (Expr_appl (id, norm_args, r)) in 
203 
if offsets <> [] 
204 
then 
205 
let defvars, norm_expr = normalize_expr node [] defvars norm_expr in 
206 
normalize_expr ~alias:alias node offsets defvars norm_expr 
207 
else 
208 
mk_expr_alias_opt (alias && not (Basic_library.is_internal_fun id)) node defvars norm_expr 
209 
 Expr_arrow (e1,e2) when !unfold_arrow_active && not (is_expr_once expr) > (* Here we differ from Colaco paper: arrows are pushed to the top *) 
210 
normalize_expr ~alias:alias node offsets defvars (unfold_arrow expr) 
211 
 Expr_arrow (e1,e2) > 
212 
let defvars, norm_e1 = normalize_expr node offsets defvars e1 in 
213 
let defvars, norm_e2 = normalize_expr node offsets defvars e2 in 
214 
let norm_expr = mk_norm_expr offsets expr (Expr_arrow (norm_e1, norm_e2)) in 
215 
mk_expr_alias_opt alias node defvars norm_expr 
216 
 Expr_pre e > 
217 
let defvars, norm_e = normalize_expr node offsets defvars e in 
218 
let norm_expr = mk_norm_expr offsets expr (Expr_pre norm_e) in 
219 
mk_expr_alias_opt alias node defvars norm_expr 
220 
 Expr_fby (e1, e2) > 
221 
let defvars, norm_e1 = normalize_expr node offsets defvars e1 in 
222 
let defvars, norm_e2 = normalize_expr node offsets defvars e2 in 
223 
let norm_expr = mk_norm_expr offsets expr (Expr_fby (norm_e1, norm_e2)) in 
224 
mk_expr_alias_opt alias node defvars norm_expr 
225 
 Expr_when (e, c, l) > 
226 
let defvars, norm_e = normalize_expr node offsets defvars e in 
227 
defvars, mk_norm_expr offsets expr (Expr_when (norm_e, c, l)) 
228 
 Expr_ite (c, t, e) > 
229 
let defvars, norm_c = normalize_guard node defvars c in 
230 
let defvars, norm_t = normalize_cond_expr node offsets defvars t in 
231 
let defvars, norm_e = normalize_cond_expr node offsets defvars e in 
232 
let norm_expr = mk_norm_expr offsets expr (Expr_ite (norm_c, norm_t, norm_e)) in 
233 
mk_expr_alias_opt alias node defvars norm_expr 
234 
 Expr_merge (c, hl) > 
235 
let defvars, norm_hl = normalize_branches node offsets defvars hl in 
236 
let norm_expr = mk_norm_expr offsets expr (Expr_merge (c, norm_hl)) in 
237 
mk_expr_alias_opt alias node defvars norm_expr 
238 

239 
(* Creates a conditional with a merge construct, which is more lazy *) 
240 
(* 
241 
let norm_conditional_as_merge alias node norm_expr offsets defvars expr = 
242 
match expr.expr_desc with 
243 
 Expr_ite (c, t, e) > 
244 
let defvars, norm_t = norm_expr (alias node offsets defvars t in 
245 
 _ > assert false 
246 
*) 
247 
and normalize_branches node offsets defvars hl = 
248 
List.fold_right 
249 
(fun (t, h) (defvars, norm_q) > 
250 
let (defvars, norm_h) = normalize_cond_expr node offsets defvars h in 
251 
defvars, (t, norm_h) :: norm_q 
252 
) 
253 
hl (defvars, []) 
254  
255 
and normalize_array_expr ?(alias=true) node offsets defvars expr = 
256 
(* Format.eprintf "normalize_array %B %a [%a]@." alias Printers.pp_expr expr (Utils.fprintf_list ~sep:"," Dimension.pp_dimension) offsets;*) 
257 
match expr.expr_desc with 
258 
 Expr_power (e1, d) when offsets = [] > 
259 
let defvars, norm_e1 = normalize_expr node offsets defvars e1 in 
260 
defvars, mk_norm_expr offsets expr (Expr_power (norm_e1, d)) 
261 
 Expr_power (e1, d) > 
262 
normalize_array_expr ~alias:alias node (List.tl offsets) defvars e1 
263 
 Expr_access (e1, d) > normalize_array_expr ~alias:alias node (d::offsets) defvars e1 
264 
 Expr_array elist when offsets = [] > 
265 
let defvars, norm_elist = normalize_list alias node offsets (fun _ > normalize_array_expr ~alias:true) defvars elist in 
266 
defvars, mk_norm_expr offsets expr (Expr_array norm_elist) 
267 
 Expr_appl (id, args, None) when Basic_library.is_internal_fun id > 
268 
let defvars, norm_args = normalize_list alias node offsets (fun _ > normalize_array_expr ~alias:true) defvars (expr_list_of_expr args) in 
269 
defvars, mk_norm_expr offsets expr (Expr_appl (id, expr_of_expr_list args.expr_loc norm_args, None)) 
270 
 _ > normalize_expr ~alias:alias node offsets defvars expr 
271  
272 
and normalize_cond_expr ?(alias=true) node offsets defvars expr = 
273 
(*Format.eprintf "normalize_cond %B %a [%a]@." alias Printers.pp_expr expr (Utils.fprintf_list ~sep:"," Dimension.pp_dimension) offsets;*) 
274 
match expr.expr_desc with 
275 
 Expr_access (e1, d) > 
276 
normalize_cond_expr ~alias:alias node (d::offsets) defvars e1 
277 
 Expr_ite (c, t, e) > 
278 
let defvars, norm_c = normalize_guard node defvars c in 
279 
let defvars, norm_t = normalize_cond_expr node offsets defvars t in 
280 
let defvars, norm_e = normalize_cond_expr node offsets defvars e in 
281 
defvars, mk_norm_expr offsets expr (Expr_ite (norm_c, norm_t, norm_e)) 
282 
 Expr_merge (c, hl) > 
283 
let defvars, norm_hl = normalize_branches node offsets defvars hl in 
284 
defvars, mk_norm_expr offsets expr (Expr_merge (c, norm_hl)) 
285 
 _ > normalize_expr ~alias:alias node offsets defvars expr 
286  
287 
and normalize_guard node defvars expr = 
288 
let defvars, norm_expr = normalize_expr node [] defvars expr in 
289 
mk_expr_alias_opt true node defvars norm_expr 
290  
291 
(* outputs cannot be memories as well. If so, introduce new local variable. 
292 
*) 
293 
let decouple_outputs node defvars eq = 
294 
let rec fold_lhs defvars lhs tys cks = 
295 
match lhs, tys, cks with 
296 
 [], [], [] > defvars, [] 
297 
 v::qv, t::qt, c::qc > let (defs_q, vars_q), lhs_q = fold_lhs defvars qv qt qc in 
298 
if List.exists (fun o > o.var_id = v) node.node_outputs 
299 
then 
300 
let newvar = mk_fresh_var node eq.eq_loc t c in 
301 
let neweq = mkeq eq.eq_loc ([v], mk_ident_expr newvar) in 
302 
(neweq :: defs_q, newvar :: vars_q), newvar.var_id :: lhs_q 
303 
else 
304 
(defs_q, vars_q), v::lhs_q 
305 
 _ > assert false in 
306 
let defvars', lhs' = 
307 
fold_lhs 
308 
defvars 
309 
eq.eq_lhs 
310 
(Types.type_list_of_type eq.eq_rhs.expr_type) 
311 
(Clocks.clock_list_of_clock eq.eq_rhs.expr_clock) in 
312 
defvars', {eq with eq_lhs = lhs' } 
313  
314 
let rec normalize_eq node defvars eq = 
315 
match eq.eq_rhs.expr_desc with 
316 
 Expr_pre _ 
317 
 Expr_fby _ > 
318 
let (defvars', eq') = decouple_outputs node defvars eq in 
319 
let (defs', vars'), norm_rhs = normalize_expr ~alias:false node [] defvars' eq'.eq_rhs in 
320 
let norm_eq = { eq' with eq_rhs = norm_rhs } in 
321 
(norm_eq::defs', vars') 
322 
 Expr_array _ > 
323 
let (defs', vars'), norm_rhs = normalize_array_expr ~alias:false node [] defvars eq.eq_rhs in 
324 
let norm_eq = { eq with eq_rhs = norm_rhs } in 
325 
(norm_eq::defs', vars') 
326 
 Expr_appl (id, _, None) when Basic_library.is_internal_fun id && Types.is_array_type eq.eq_rhs.expr_type > 
327 
let (defs', vars'), norm_rhs = normalize_array_expr ~alias:false node [] defvars eq.eq_rhs in 
328 
let norm_eq = { eq with eq_rhs = norm_rhs } in 
329 
(norm_eq::defs', vars') 
330 
 Expr_appl _ > 
331 
let (defs', vars'), norm_rhs = normalize_expr ~alias:false node [] defvars eq.eq_rhs in 
332 
let norm_eq = { eq with eq_rhs = norm_rhs } in 
333 
(norm_eq::defs', vars') 
334 
 _ > 
335 
let (defs', vars'), norm_rhs = normalize_cond_expr ~alias:false node [] defvars eq.eq_rhs in 
336 
let norm_eq = { eq with eq_rhs = norm_rhs } in 
337 
norm_eq::defs', vars' 
338  
339 
(** normalize_node node returns a normalized node, 
340 
ie. 
341 
 updated locals 
342 
 new equations 
343 
 
344 
*) 
345 
let normalize_node node = 
346 
cpt_fresh := 0; 
347 
let inputs_outputs = node.node_inputs@node.node_outputs in 
348 
let is_local v = 
349 
List.for_all ((!=) v) inputs_outputs in 
350 
let orig_vars = inputs_outputs@node.node_locals in 
351 
let defs, vars = 
352 
List.fold_left (normalize_eq node) ([], orig_vars) node.node_eqs in 
353 
(* Normalize the asserts *) 
354 
let vars, assert_defs, asserts = 
355 
List.fold_left ( 
356 
fun (vars, def_accu, assert_accu) assert_ > 
357 
let assert_expr = assert_.assert_expr in 
358 
let (defs, vars'), expr = 
359 
normalize_expr 
360 
~alias:false 
361 
node 
362 
[] (* empty offset for arrays *) 
363 
([], vars) (* defvar only contains vars *) 
364 
assert_expr 
365 
in 
366 
vars', defs@def_accu, {assert_ with assert_expr = expr}::assert_accu 
367 
) (vars, [], []) node.node_asserts in 
368 
let new_locals = List.filter is_local vars in 
369 
(* Compute traceability info: 
370 
 gather newly bound variables 
371 
 compute the associated expression without aliases 
372 
*) 
373 
let diff_vars = List.filter (fun v > not (List.mem v node.node_locals) ) new_locals in 
374 
let norm_traceability = { 
375 
annots = 
376 
List.map 
377 
(fun v > 
378 
let expr = substitute_expr diff_vars defs ( 
379 
let eq = try 
380 
List.find (fun eq > eq.eq_lhs = [v.var_id]) defs 
381 
with Not_found > (Format.eprintf "var not found %s@." v.var_id; assert false) in 
382 
eq.eq_rhs) in 
383 
let pair = mkeexpr expr.expr_loc (mkexpr expr.expr_loc (Expr_tuple [expr_of_ident v.var_id expr.expr_loc; expr])) in 
384 
["horn_backend";"trace"], pair 
385 
) 
386 
diff_vars ; 
387 
annot_loc = Location.dummy_loc 
388 
} 
389  
390 
in 
391 
let node = 
392 
{ node with 
393 
node_locals = new_locals; 
394 
node_eqs = defs @ assert_defs; 
395 
node_asserts = asserts; 
396 
node_annot = norm_traceability::node.node_annot; 
397 
} 
398 
in ((*Printers.pp_node Format.err_formatter node;*) node) 
399  
400 
let normalize_decl decl = 
401 
match decl.top_decl_desc with 
402 
 Node nd > 
403 
{decl with top_decl_desc = Node (normalize_node nd)} 
404 
 Open _  ImportedNode _  Consts _  Type _ > decl 
405 

406 
let normalize_prog decls = 
407 
List.map normalize_decl decls 
408  
409 
(* Local Variables: *) 
410 
(* compilecommand:"make C .." *) 
411 
(* End: *) 