lustrec / src / corelang.ml @ 6394042a
History | View | Annotate | Download (32.4 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 Format |
13 |
open LustreSpec |
14 |
open Dimension |
15 |
|
16 |
|
17 |
exception Error of Location.t * error |
18 |
|
19 |
module VDeclModule = |
20 |
struct (* Node module *) |
21 |
type t = var_decl |
22 |
let compare v1 v2 = compare v1.var_id v2.var_id |
23 |
end |
24 |
|
25 |
module VMap = Map.Make(VDeclModule) |
26 |
|
27 |
module VSet = Set.Make(VDeclModule) |
28 |
|
29 |
let dummy_type_dec = {ty_dec_desc=Tydec_any; ty_dec_loc=Location.dummy_loc} |
30 |
|
31 |
let dummy_clock_dec = {ck_dec_desc=Ckdec_any; ck_dec_loc=Location.dummy_loc} |
32 |
|
33 |
|
34 |
|
35 |
(************************************************************) |
36 |
(* *) |
37 |
|
38 |
let mktyp loc d = |
39 |
{ ty_dec_desc = d; ty_dec_loc = loc } |
40 |
|
41 |
let mkclock loc d = |
42 |
{ ck_dec_desc = d; ck_dec_loc = loc } |
43 |
|
44 |
let mkvar_decl loc ?(orig=false) (id, ty_dec, ck_dec, is_const) = |
45 |
{ var_id = id; |
46 |
var_orig = orig; |
47 |
var_dec_type = ty_dec; |
48 |
var_dec_clock = ck_dec; |
49 |
var_dec_const = is_const; |
50 |
var_type = Types.new_var (); |
51 |
var_clock = Clocks.new_var true; |
52 |
var_loc = loc } |
53 |
|
54 |
let mkexpr loc d = |
55 |
{ expr_tag = Utils.new_tag (); |
56 |
expr_desc = d; |
57 |
expr_type = Types.new_var (); |
58 |
expr_clock = Clocks.new_var true; |
59 |
expr_delay = Delay.new_var (); |
60 |
expr_annot = None; |
61 |
expr_loc = loc } |
62 |
|
63 |
let var_decl_of_const c = |
64 |
{ var_id = c.const_id; |
65 |
var_orig = true; |
66 |
var_dec_type = { ty_dec_loc = c.const_loc; ty_dec_desc = Tydec_any }; |
67 |
var_dec_clock = { ck_dec_loc = c.const_loc; ck_dec_desc = Ckdec_any }; |
68 |
var_dec_const = true; |
69 |
var_type = c.const_type; |
70 |
var_clock = Clocks.new_var false; |
71 |
var_loc = c.const_loc } |
72 |
|
73 |
let mk_new_name used id = |
74 |
let rec new_name name cpt = |
75 |
if used name |
76 |
then new_name (sprintf "_%s_%i" id cpt) (cpt+1) |
77 |
else name |
78 |
in new_name id 1 |
79 |
|
80 |
let mkeq loc (lhs, rhs) = |
81 |
{ eq_lhs = lhs; |
82 |
eq_rhs = rhs; |
83 |
eq_loc = loc } |
84 |
|
85 |
let mkassert loc expr = |
86 |
{ assert_loc = loc; |
87 |
assert_expr = expr |
88 |
} |
89 |
|
90 |
let mktop_decl loc own itf d = |
91 |
{ top_decl_desc = d; top_decl_loc = loc; top_decl_owner = own; top_decl_itf = itf } |
92 |
|
93 |
let mkpredef_call loc funname args = |
94 |
mkexpr loc (Expr_appl (funname, mkexpr loc (Expr_tuple args), None)) |
95 |
|
96 |
|
97 |
let const_of_top top_decl = |
98 |
match top_decl.top_decl_desc with |
99 |
| Const c -> c |
100 |
| _ -> assert false |
101 |
|
102 |
let node_of_top top_decl = |
103 |
match top_decl.top_decl_desc with |
104 |
| Node nd -> nd |
105 |
| _ -> assert false |
106 |
|
107 |
let imported_node_of_top top_decl = |
108 |
match top_decl.top_decl_desc with |
109 |
| ImportedNode ind -> ind |
110 |
| _ -> assert false |
111 |
|
112 |
let typedef_of_top top_decl = |
113 |
match top_decl.top_decl_desc with |
114 |
| TypeDef tdef -> tdef |
115 |
| _ -> assert false |
116 |
|
117 |
let dependency_of_top top_decl = |
118 |
match top_decl.top_decl_desc with |
119 |
| Open (local, dep) -> (local, dep) |
120 |
| _ -> assert false |
121 |
|
122 |
let consts_of_enum_type top_decl = |
123 |
match top_decl.top_decl_desc with |
124 |
| TypeDef tdef -> |
125 |
(match tdef.tydef_desc with |
126 |
| Tydec_enum tags -> List.map (fun tag -> let cdecl = { const_id = tag; const_loc = top_decl.top_decl_loc; const_value = Const_tag tag; const_type = Type_predef.type_const tdef.tydef_id } in { top_decl with top_decl_desc = Const cdecl }) tags |
127 |
| _ -> []) |
128 |
| _ -> assert false |
129 |
|
130 |
(************************************************************) |
131 |
(* Eexpr functions *) |
132 |
(************************************************************) |
133 |
|
134 |
let merge_node_annot ann1 ann2 = |
135 |
{ requires = ann1.requires @ ann2.requires; |
136 |
ensures = ann1.ensures @ ann2.ensures; |
137 |
behaviors = ann1.behaviors @ ann2.behaviors; |
138 |
spec_loc = ann1.spec_loc |
139 |
} |
140 |
|
141 |
let mkeexpr loc expr = |
142 |
{ eexpr_tag = Utils.new_tag (); |
143 |
eexpr_qfexpr = expr; |
144 |
eexpr_quantifiers = []; |
145 |
eexpr_type = Types.new_var (); |
146 |
eexpr_clock = Clocks.new_var true; |
147 |
eexpr_normalized = None; |
148 |
eexpr_loc = loc } |
149 |
|
150 |
let extend_eexpr q e = { e with eexpr_quantifiers = q@e.eexpr_quantifiers } |
151 |
|
152 |
(* |
153 |
let mkepredef_call loc funname args = |
154 |
mkeexpr loc (EExpr_appl (funname, mkeexpr loc (EExpr_tuple args), None)) |
155 |
|
156 |
let mkepredef_unary_call loc funname arg = |
157 |
mkeexpr loc (EExpr_appl (funname, arg, None)) |
158 |
*) |
159 |
|
160 |
let merge_expr_annot ann1 ann2 = |
161 |
match ann1, ann2 with |
162 |
| None, None -> assert false |
163 |
| Some _, None -> ann1 |
164 |
| None, Some _ -> ann2 |
165 |
| Some ann1, Some ann2 -> Some { |
166 |
annots = ann1.annots @ ann2.annots; |
167 |
annot_loc = ann1.annot_loc |
168 |
} |
169 |
|
170 |
let update_expr_annot node_id e annot = |
171 |
List.iter (fun (key, _) -> |
172 |
Annotations.add_expr_ann node_id e.expr_tag key |
173 |
) annot.annots; |
174 |
{ e with expr_annot = merge_expr_annot e.expr_annot (Some annot) } |
175 |
|
176 |
|
177 |
(***********************************************************) |
178 |
(* Fast access to nodes, by name *) |
179 |
let (node_table : (ident, top_decl) Hashtbl.t) = Hashtbl.create 30 |
180 |
let consts_table = Hashtbl.create 30 |
181 |
|
182 |
let print_node_table fmt () = |
183 |
begin |
184 |
Format.fprintf fmt "{ /* node table */@."; |
185 |
Hashtbl.iter (fun id nd -> |
186 |
Format.fprintf fmt "%s |-> %a" |
187 |
id |
188 |
Printers.pp_short_decl nd |
189 |
) node_table; |
190 |
Format.fprintf fmt "}@." |
191 |
end |
192 |
|
193 |
let print_consts_table fmt () = |
194 |
begin |
195 |
Format.fprintf fmt "{ /* consts table */@."; |
196 |
Hashtbl.iter (fun id const -> |
197 |
Format.fprintf fmt "%s |-> %a" |
198 |
id |
199 |
Printers.pp_const_decl (const_of_top const) |
200 |
) consts_table; |
201 |
Format.fprintf fmt "}@." |
202 |
end |
203 |
|
204 |
let node_name td = |
205 |
match td.top_decl_desc with |
206 |
| Node nd -> nd.node_id |
207 |
| ImportedNode nd -> nd.nodei_id |
208 |
| _ -> assert false |
209 |
|
210 |
let is_generic_node td = |
211 |
match td.top_decl_desc with |
212 |
| Node nd -> List.exists (fun v -> v.var_dec_const) nd.node_inputs |
213 |
| ImportedNode nd -> List.exists (fun v -> v.var_dec_const) nd.nodei_inputs |
214 |
| _ -> assert false |
215 |
|
216 |
let node_inputs td = |
217 |
match td.top_decl_desc with |
218 |
| Node nd -> nd.node_inputs |
219 |
| ImportedNode nd -> nd.nodei_inputs |
220 |
| _ -> assert false |
221 |
|
222 |
let node_from_name id = |
223 |
try |
224 |
Hashtbl.find node_table id |
225 |
with Not_found -> (Format.eprintf "Unable to find any node named %s@ @?" id; |
226 |
assert false) |
227 |
|
228 |
let is_imported_node td = |
229 |
match td.top_decl_desc with |
230 |
| Node nd -> false |
231 |
| ImportedNode nd -> true |
232 |
| _ -> assert false |
233 |
|
234 |
|
235 |
(* alias and type definition table *) |
236 |
|
237 |
let mktop = mktop_decl Location.dummy_loc Version.include_path false |
238 |
|
239 |
let top_int_type = mktop (TypeDef {tydef_id = "int"; tydef_desc = Tydec_int}) |
240 |
let top_bool_type = mktop (TypeDef {tydef_id = "bool"; tydef_desc = Tydec_bool}) |
241 |
let top_float_type = mktop (TypeDef {tydef_id = "float"; tydef_desc = Tydec_float}) |
242 |
let top_real_type = mktop (TypeDef {tydef_id = "real"; tydef_desc = Tydec_real}) |
243 |
|
244 |
let type_table = |
245 |
Utils.create_hashtable 20 [ |
246 |
Tydec_int , top_int_type; |
247 |
Tydec_bool , top_bool_type; |
248 |
Tydec_float, top_float_type; |
249 |
Tydec_real , top_real_type |
250 |
] |
251 |
|
252 |
let print_type_table fmt () = |
253 |
begin |
254 |
Format.fprintf fmt "{ /* type table */@."; |
255 |
Hashtbl.iter (fun tydec tdef -> |
256 |
Format.fprintf fmt "%a |-> %a" |
257 |
Printers.pp_var_type_dec_desc tydec |
258 |
Printers.pp_typedef (typedef_of_top tdef) |
259 |
) type_table; |
260 |
Format.fprintf fmt "}@." |
261 |
end |
262 |
|
263 |
let rec is_user_type typ = |
264 |
match typ with |
265 |
| Tydec_int | Tydec_bool | Tydec_real |
266 |
| Tydec_float | Tydec_any | Tydec_const _ -> false |
267 |
| Tydec_clock typ' -> is_user_type typ' |
268 |
| _ -> true |
269 |
|
270 |
let get_repr_type typ = |
271 |
let typ_def = (typedef_of_top (Hashtbl.find type_table typ)).tydef_desc in |
272 |
if is_user_type typ_def then typ else typ_def |
273 |
|
274 |
let rec coretype_equal ty1 ty2 = |
275 |
let res = |
276 |
match ty1, ty2 with |
277 |
| Tydec_any , _ |
278 |
| _ , Tydec_any -> assert false |
279 |
| Tydec_const _ , Tydec_const _ -> get_repr_type ty1 = get_repr_type ty2 |
280 |
| Tydec_const _ , _ -> let ty1' = (typedef_of_top (Hashtbl.find type_table ty1)).tydef_desc |
281 |
in (not (is_user_type ty1')) && coretype_equal ty1' ty2 |
282 |
| _ , Tydec_const _ -> coretype_equal ty2 ty1 |
283 |
| Tydec_int , Tydec_int |
284 |
| Tydec_real , Tydec_real |
285 |
| Tydec_float , Tydec_float |
286 |
| Tydec_bool , Tydec_bool -> true |
287 |
| Tydec_clock ty1 , Tydec_clock ty2 -> coretype_equal ty1 ty2 |
288 |
| Tydec_array (d1,ty1), Tydec_array (d2, ty2) -> Dimension.is_eq_dimension d1 d2 && coretype_equal ty1 ty2 |
289 |
| Tydec_enum tl1 , Tydec_enum tl2 -> List.sort compare tl1 = List.sort compare tl2 |
290 |
| Tydec_struct fl1 , Tydec_struct fl2 -> |
291 |
List.length fl1 = List.length fl2 |
292 |
&& List.for_all2 (fun (f1, t1) (f2, t2) -> f1 = f2 && coretype_equal t1 t2) |
293 |
(List.sort (fun (f1,_) (f2,_) -> compare f1 f2) fl1) |
294 |
(List.sort (fun (f1,_) (f2,_) -> compare f1 f2) fl2) |
295 |
| _ -> false |
296 |
in ((*Format.eprintf "coretype_equal %a %a = %B@." Printers.pp_var_type_dec_desc ty1 Printers.pp_var_type_dec_desc ty2 res;*) res) |
297 |
|
298 |
let tag_true = "true" |
299 |
let tag_false = "false" |
300 |
|
301 |
let const_is_bool c = |
302 |
match c with |
303 |
| Const_tag t -> t = tag_true || t = tag_false |
304 |
| _ -> false |
305 |
|
306 |
(* Computes the negation of a boolean constant *) |
307 |
let const_negation c = |
308 |
assert (const_is_bool c); |
309 |
match c with |
310 |
| Const_tag t when t = tag_true -> Const_tag tag_false |
311 |
| _ -> Const_tag tag_true |
312 |
|
313 |
let const_or c1 c2 = |
314 |
assert (const_is_bool c1 && const_is_bool c2); |
315 |
match c1, c2 with |
316 |
| Const_tag t1, _ when t1 = tag_true -> c1 |
317 |
| _ , Const_tag t2 when t2 = tag_true -> c2 |
318 |
| _ -> Const_tag tag_false |
319 |
|
320 |
let const_and c1 c2 = |
321 |
assert (const_is_bool c1 && const_is_bool c2); |
322 |
match c1, c2 with |
323 |
| Const_tag t1, _ when t1 = tag_false -> c1 |
324 |
| _ , Const_tag t2 when t2 = tag_false -> c2 |
325 |
| _ -> Const_tag tag_true |
326 |
|
327 |
let const_xor c1 c2 = |
328 |
assert (const_is_bool c1 && const_is_bool c2); |
329 |
match c1, c2 with |
330 |
| Const_tag t1, Const_tag t2 when t1 <> t2 -> Const_tag tag_true |
331 |
| _ -> Const_tag tag_false |
332 |
|
333 |
let const_impl c1 c2 = |
334 |
assert (const_is_bool c1 && const_is_bool c2); |
335 |
match c1, c2 with |
336 |
| Const_tag t1, _ when t1 = tag_false -> Const_tag tag_true |
337 |
| _ , Const_tag t2 when t2 = tag_true -> Const_tag tag_true |
338 |
| _ -> Const_tag tag_false |
339 |
|
340 |
(* To guarantee uniqueness of tags in enum types *) |
341 |
let tag_table = |
342 |
Utils.create_hashtable 20 [ |
343 |
tag_true, top_bool_type; |
344 |
tag_false, top_bool_type |
345 |
] |
346 |
|
347 |
(* To guarantee uniqueness of fields in struct types *) |
348 |
let field_table = |
349 |
Utils.create_hashtable 20 [ |
350 |
] |
351 |
|
352 |
let get_enum_type_tags cty = |
353 |
(*Format.eprintf "get_enum_type_tags %a@." Printers.pp_var_type_dec_desc cty;*) |
354 |
match cty with |
355 |
| Tydec_bool -> [tag_true; tag_false] |
356 |
| Tydec_const _ -> (match (typedef_of_top (Hashtbl.find type_table cty)).tydef_desc with |
357 |
| Tydec_enum tl -> tl |
358 |
| _ -> assert false) |
359 |
| _ -> assert false |
360 |
|
361 |
let get_struct_type_fields cty = |
362 |
match cty with |
363 |
| Tydec_const _ -> (match (typedef_of_top (Hashtbl.find type_table cty)).tydef_desc with |
364 |
| Tydec_struct fl -> fl |
365 |
| _ -> assert false) |
366 |
| _ -> assert false |
367 |
|
368 |
let const_of_bool b = |
369 |
Const_tag (if b then tag_true else tag_false) |
370 |
|
371 |
(* let get_const c = snd (Hashtbl.find consts_table c) *) |
372 |
|
373 |
let ident_of_expr expr = |
374 |
match expr.expr_desc with |
375 |
| Expr_ident id -> id |
376 |
| _ -> assert false |
377 |
|
378 |
(* Caution, returns an untyped and unclocked expression *) |
379 |
let expr_of_ident id loc = |
380 |
{expr_tag = Utils.new_tag (); |
381 |
expr_desc = Expr_ident id; |
382 |
expr_type = Types.new_var (); |
383 |
expr_clock = Clocks.new_var true; |
384 |
expr_delay = Delay.new_var (); |
385 |
expr_loc = loc; |
386 |
expr_annot = None} |
387 |
|
388 |
let is_tuple_expr expr = |
389 |
match expr.expr_desc with |
390 |
| Expr_tuple _ -> true |
391 |
| _ -> false |
392 |
|
393 |
let expr_list_of_expr expr = |
394 |
match expr.expr_desc with |
395 |
| Expr_tuple elist -> elist |
396 |
| _ -> [expr] |
397 |
|
398 |
let expr_of_expr_list loc elist = |
399 |
match elist with |
400 |
| [t] -> { t with expr_loc = loc } |
401 |
| t::_ -> |
402 |
let tlist = List.map (fun e -> e.expr_type) elist in |
403 |
let clist = List.map (fun e -> e.expr_clock) elist in |
404 |
{ t with expr_desc = Expr_tuple elist; |
405 |
expr_type = Type_predef.type_tuple tlist; |
406 |
expr_clock = Clock_predef.ck_tuple clist; |
407 |
expr_tag = Utils.new_tag (); |
408 |
expr_loc = loc } |
409 |
| _ -> assert false |
410 |
|
411 |
let call_of_expr expr = |
412 |
match expr.expr_desc with |
413 |
| Expr_appl (f, args, r) -> (f, expr_list_of_expr args, r) |
414 |
| _ -> assert false |
415 |
|
416 |
(* Conversion from dimension expr to standard expr, for the purpose of printing, typing, etc... *) |
417 |
let rec expr_of_dimension dim = |
418 |
match dim.dim_desc with |
419 |
| Dbool b -> |
420 |
mkexpr dim.dim_loc (Expr_const (const_of_bool b)) |
421 |
| Dint i -> |
422 |
mkexpr dim.dim_loc (Expr_const (Const_int i)) |
423 |
| Dident id -> |
424 |
mkexpr dim.dim_loc (Expr_ident id) |
425 |
| Dite (c, t, e) -> |
426 |
mkexpr dim.dim_loc (Expr_ite (expr_of_dimension c, expr_of_dimension t, expr_of_dimension e)) |
427 |
| Dappl (id, args) -> |
428 |
mkexpr dim.dim_loc (Expr_appl (id, expr_of_expr_list dim.dim_loc (List.map expr_of_dimension args), None)) |
429 |
| Dlink dim' -> expr_of_dimension dim' |
430 |
| Dvar |
431 |
| Dunivar -> (Format.eprintf "internal error: expr_of_dimension %a@." Dimension.pp_dimension dim; |
432 |
assert false) |
433 |
|
434 |
let dimension_of_const loc const = |
435 |
match const with |
436 |
| Const_int i -> mkdim_int loc i |
437 |
| Const_tag t when t = tag_true || t = tag_false -> mkdim_bool loc (t = tag_true) |
438 |
| _ -> raise InvalidDimension |
439 |
|
440 |
(* Conversion from standard expr to dimension expr, for the purpose of injecting static call arguments |
441 |
into dimension expressions *) |
442 |
let rec dimension_of_expr expr = |
443 |
match expr.expr_desc with |
444 |
| Expr_const c -> dimension_of_const expr.expr_loc c |
445 |
| Expr_ident id -> mkdim_ident expr.expr_loc id |
446 |
| Expr_appl (f, args, None) when Basic_library.is_internal_fun f -> |
447 |
let k = Types.get_static_value (Env.lookup_value Basic_library.type_env f) in |
448 |
if k = None then raise InvalidDimension; |
449 |
mkdim_appl expr.expr_loc f (List.map dimension_of_expr (expr_list_of_expr args)) |
450 |
| Expr_ite (i, t, e) -> |
451 |
mkdim_ite expr.expr_loc (dimension_of_expr i) (dimension_of_expr t) (dimension_of_expr e) |
452 |
| _ -> raise InvalidDimension (* not a simple dimension expression *) |
453 |
|
454 |
|
455 |
let sort_handlers hl = |
456 |
List.sort (fun (t, _) (t', _) -> compare t t') hl |
457 |
|
458 |
let rec is_eq_expr e1 e2 = match e1.expr_desc, e2.expr_desc with |
459 |
| Expr_const c1, Expr_const c2 -> c1 = c2 |
460 |
| Expr_ident i1, Expr_ident i2 -> i1 = i2 |
461 |
| Expr_array el1, Expr_array el2 |
462 |
| Expr_tuple el1, Expr_tuple el2 -> |
463 |
List.length el1 = List.length el2 && List.for_all2 is_eq_expr el1 el2 |
464 |
| Expr_arrow (e1, e2), Expr_arrow (e1', e2') -> is_eq_expr e1 e1' && is_eq_expr e2 e2' |
465 |
| Expr_fby (e1,e2), Expr_fby (e1',e2') -> is_eq_expr e1 e1' && is_eq_expr e2 e2' |
466 |
| Expr_ite (i1, t1, e1), Expr_ite (i2, t2, e2) -> is_eq_expr i1 i2 && is_eq_expr t1 t2 && is_eq_expr e1 e2 |
467 |
(* | Expr_concat (e1,e2), Expr_concat (e1',e2') -> is_eq_expr e1 e1' && is_eq_expr e2 e2' *) |
468 |
(* | Expr_tail e, Expr_tail e' -> is_eq_expr e e' *) |
469 |
| Expr_pre e, Expr_pre e' -> is_eq_expr e e' |
470 |
| Expr_when (e, i, l), Expr_when (e', i', l') -> l=l' && i=i' && is_eq_expr e e' |
471 |
| Expr_merge(i, hl), Expr_merge(i', hl') -> i=i' && List.for_all2 (fun (t, h) (t', h') -> t=t' && is_eq_expr h h') (sort_handlers hl) (sort_handlers hl') |
472 |
| Expr_appl (i, e, r), Expr_appl (i', e', r') -> i=i' && r=r' && is_eq_expr e e' |
473 |
| Expr_power (e1, i1), Expr_power (e2, i2) |
474 |
| Expr_access (e1, i1), Expr_access (e2, i2) -> is_eq_expr e1 e2 && is_eq_expr (expr_of_dimension i1) (expr_of_dimension i2) |
475 |
| _ -> false |
476 |
|
477 |
let get_node_vars nd = |
478 |
nd.node_inputs @ nd.node_locals @ nd.node_outputs |
479 |
|
480 |
let get_var id var_list = |
481 |
List.find (fun v -> v.var_id = id) var_list |
482 |
|
483 |
let get_node_var id node = |
484 |
get_var id (get_node_vars node) |
485 |
|
486 |
let get_node_eqs = |
487 |
let get_eqs stmts = |
488 |
List.fold_right |
489 |
(fun stmt res -> |
490 |
match stmt with |
491 |
| Eq eq -> eq :: res |
492 |
| Aut _ -> assert false) |
493 |
stmts |
494 |
[] in |
495 |
let table_eqs = Hashtbl.create 23 in |
496 |
(fun nd -> |
497 |
try |
498 |
let (old, res) = Hashtbl.find table_eqs nd.node_id |
499 |
in if old == nd.node_stmts then res else raise Not_found |
500 |
with Not_found -> |
501 |
let res = get_eqs nd.node_stmts in |
502 |
begin |
503 |
Hashtbl.replace table_eqs nd.node_id (nd.node_stmts, res); |
504 |
res |
505 |
end) |
506 |
|
507 |
let get_node_eq id node = |
508 |
List.find (fun eq -> List.mem id eq.eq_lhs) (get_node_eqs node) |
509 |
|
510 |
let get_nodes prog = |
511 |
List.fold_left ( |
512 |
fun nodes decl -> |
513 |
match decl.top_decl_desc with |
514 |
| Node _ -> decl::nodes |
515 |
| Const _ | ImportedNode _ | Open _ | TypeDef _ -> nodes |
516 |
) [] prog |
517 |
|
518 |
let get_imported_nodes prog = |
519 |
List.fold_left ( |
520 |
fun nodes decl -> |
521 |
match decl.top_decl_desc with |
522 |
| ImportedNode _ -> decl::nodes |
523 |
| Const _ | Node _ | Open _ | TypeDef _-> nodes |
524 |
) [] prog |
525 |
|
526 |
let get_consts prog = |
527 |
List.fold_right ( |
528 |
fun decl consts -> |
529 |
match decl.top_decl_desc with |
530 |
| Const _ -> decl::consts |
531 |
| Node _ | ImportedNode _ | Open _ | TypeDef _ -> consts |
532 |
) prog [] |
533 |
|
534 |
let get_typedefs prog = |
535 |
List.fold_right ( |
536 |
fun decl types -> |
537 |
match decl.top_decl_desc with |
538 |
| TypeDef _ -> decl::types |
539 |
| Node _ | ImportedNode _ | Open _ | Const _ -> types |
540 |
) prog [] |
541 |
|
542 |
let get_dependencies prog = |
543 |
List.fold_right ( |
544 |
fun decl deps -> |
545 |
match decl.top_decl_desc with |
546 |
| Open _ -> decl::deps |
547 |
| Node _ | ImportedNode _ | TypeDef _ | Const _ -> deps |
548 |
) prog [] |
549 |
|
550 |
let get_node_interface nd = |
551 |
{nodei_id = nd.node_id; |
552 |
nodei_type = nd.node_type; |
553 |
nodei_clock = nd.node_clock; |
554 |
nodei_inputs = nd.node_inputs; |
555 |
nodei_outputs = nd.node_outputs; |
556 |
nodei_stateless = nd.node_dec_stateless; |
557 |
nodei_spec = nd.node_spec; |
558 |
nodei_prototype = None; |
559 |
nodei_in_lib = None; |
560 |
} |
561 |
|
562 |
(************************************************************************) |
563 |
(* Renaming *) |
564 |
|
565 |
(* applies the renaming function [fvar] to all variables of expression [expr] *) |
566 |
let rec expr_replace_var fvar expr = |
567 |
{ expr with expr_desc = expr_desc_replace_var fvar expr.expr_desc } |
568 |
|
569 |
and expr_desc_replace_var fvar expr_desc = |
570 |
match expr_desc with |
571 |
| Expr_const _ -> expr_desc |
572 |
| Expr_ident i -> Expr_ident (fvar i) |
573 |
| Expr_array el -> Expr_array (List.map (expr_replace_var fvar) el) |
574 |
| Expr_access (e1, d) -> Expr_access (expr_replace_var fvar e1, d) |
575 |
| Expr_power (e1, d) -> Expr_power (expr_replace_var fvar e1, d) |
576 |
| Expr_tuple el -> Expr_tuple (List.map (expr_replace_var fvar) el) |
577 |
| Expr_ite (c, t, e) -> Expr_ite (expr_replace_var fvar c, expr_replace_var fvar t, expr_replace_var fvar e) |
578 |
| Expr_arrow (e1, e2)-> Expr_arrow (expr_replace_var fvar e1, expr_replace_var fvar e2) |
579 |
| Expr_fby (e1, e2) -> Expr_fby (expr_replace_var fvar e1, expr_replace_var fvar e2) |
580 |
| Expr_pre e' -> Expr_pre (expr_replace_var fvar e') |
581 |
| Expr_when (e', i, l)-> Expr_when (expr_replace_var fvar e', fvar i, l) |
582 |
| Expr_merge (i, hl) -> Expr_merge (fvar i, List.map (fun (t, h) -> (t, expr_replace_var fvar h)) hl) |
583 |
| Expr_appl (i, e', i') -> Expr_appl (i, expr_replace_var fvar e', Utils.option_map (expr_replace_var fvar) i') |
584 |
|
585 |
(* Applies the renaming function [fvar] to every rhs |
586 |
only when the corresponding lhs satisfies predicate [pvar] *) |
587 |
let eq_replace_rhs_var pvar fvar eq = |
588 |
let pvar l = List.exists pvar l in |
589 |
let rec replace lhs rhs = |
590 |
{ rhs with expr_desc = replace_desc lhs rhs.expr_desc } |
591 |
and replace_desc lhs rhs_desc = |
592 |
match lhs with |
593 |
| [] -> assert false |
594 |
| [_] -> if pvar lhs then expr_desc_replace_var fvar rhs_desc else rhs_desc |
595 |
| _ -> |
596 |
(match rhs_desc with |
597 |
| Expr_tuple tl -> |
598 |
Expr_tuple (List.map2 (fun v e -> replace [v] e) lhs tl) |
599 |
| Expr_appl (f, arg, None) when Basic_library.is_internal_fun f -> |
600 |
let args = expr_list_of_expr arg in |
601 |
Expr_appl (f, expr_of_expr_list arg.expr_loc (List.map (replace lhs) args), None) |
602 |
| Expr_array _ |
603 |
| Expr_access _ |
604 |
| Expr_power _ |
605 |
| Expr_const _ |
606 |
| Expr_ident _ |
607 |
| Expr_appl _ -> |
608 |
if pvar lhs |
609 |
then expr_desc_replace_var fvar rhs_desc |
610 |
else rhs_desc |
611 |
| Expr_ite (c, t, e) -> Expr_ite (replace lhs c, replace lhs t, replace lhs e) |
612 |
| Expr_arrow (e1, e2) -> Expr_arrow (replace lhs e1, replace lhs e2) |
613 |
| Expr_fby (e1, e2) -> Expr_fby (replace lhs e1, replace lhs e2) |
614 |
| Expr_pre e' -> Expr_pre (replace lhs e') |
615 |
| Expr_when (e', i, l) -> let i' = if pvar lhs then fvar i else i |
616 |
in Expr_when (replace lhs e', i', l) |
617 |
| Expr_merge (i, hl) -> let i' = if pvar lhs then fvar i else i |
618 |
in Expr_merge (i', List.map (fun (t, h) -> (t, replace lhs h)) hl) |
619 |
) |
620 |
in { eq with eq_rhs = replace eq.eq_lhs eq.eq_rhs } |
621 |
|
622 |
|
623 |
let rec rename_expr f_node f_var f_const expr = |
624 |
{ expr with expr_desc = rename_expr_desc f_node f_var f_const expr.expr_desc } |
625 |
and rename_expr_desc f_node f_var f_const expr_desc = |
626 |
let re = rename_expr f_node f_var f_const in |
627 |
match expr_desc with |
628 |
| Expr_const _ -> expr_desc |
629 |
| Expr_ident i -> Expr_ident (f_var i) |
630 |
| Expr_array el -> Expr_array (List.map re el) |
631 |
| Expr_access (e1, d) -> Expr_access (re e1, d) |
632 |
| Expr_power (e1, d) -> Expr_power (re e1, d) |
633 |
| Expr_tuple el -> Expr_tuple (List.map re el) |
634 |
| Expr_ite (c, t, e) -> Expr_ite (re c, re t, re e) |
635 |
| Expr_arrow (e1, e2)-> Expr_arrow (re e1, re e2) |
636 |
| Expr_fby (e1, e2) -> Expr_fby (re e1, re e2) |
637 |
| Expr_pre e' -> Expr_pre (re e') |
638 |
| Expr_when (e', i, l)-> Expr_when (re e', f_var i, l) |
639 |
| Expr_merge (i, hl) -> |
640 |
Expr_merge (f_var i, List.map (fun (t, h) -> (t, re h)) hl) |
641 |
| Expr_appl (i, e', i') -> |
642 |
Expr_appl (f_node i, re e', Utils.option_map re i') |
643 |
|
644 |
let rename_node_annot f_node f_var f_const expr = |
645 |
expr |
646 |
(* TODO assert false *) |
647 |
|
648 |
let rename_expr_annot f_node f_var f_const annot = |
649 |
annot |
650 |
(* TODO assert false *) |
651 |
|
652 |
let rename_node f_node f_var f_const nd = |
653 |
let rename_var v = { v with var_id = f_var v.var_id } in |
654 |
let rename_eq eq = { eq with |
655 |
eq_lhs = List.map f_var eq.eq_lhs; |
656 |
eq_rhs = rename_expr f_node f_var f_const eq.eq_rhs |
657 |
} |
658 |
in |
659 |
let inputs = List.map rename_var nd.node_inputs in |
660 |
let outputs = List.map rename_var nd.node_outputs in |
661 |
let locals = List.map rename_var nd.node_locals in |
662 |
let gen_calls = List.map (rename_expr f_node f_var f_const) nd.node_gencalls in |
663 |
let node_checks = List.map (Dimension.expr_replace_var f_var) nd.node_checks in |
664 |
let node_asserts = List.map |
665 |
(fun a -> |
666 |
{a with assert_expr = |
667 |
let expr = a.assert_expr in |
668 |
rename_expr f_node f_var f_const expr}) |
669 |
nd.node_asserts |
670 |
in |
671 |
let node_stmts = List.map (fun eq -> Eq (rename_eq eq)) (get_node_eqs nd) in |
672 |
let spec = |
673 |
Utils.option_map |
674 |
(fun s -> rename_node_annot f_node f_var f_const s) |
675 |
nd.node_spec |
676 |
in |
677 |
let annot = |
678 |
List.map |
679 |
(fun s -> rename_expr_annot f_node f_var f_const s) |
680 |
nd.node_annot |
681 |
in |
682 |
{ |
683 |
node_id = f_node nd.node_id; |
684 |
node_type = nd.node_type; |
685 |
node_clock = nd.node_clock; |
686 |
node_inputs = inputs; |
687 |
node_outputs = outputs; |
688 |
node_locals = locals; |
689 |
node_gencalls = gen_calls; |
690 |
node_checks = node_checks; |
691 |
node_asserts = node_asserts; |
692 |
node_stmts = node_stmts; |
693 |
node_dec_stateless = nd.node_dec_stateless; |
694 |
node_stateless = nd.node_stateless; |
695 |
node_spec = spec; |
696 |
node_annot = annot; |
697 |
} |
698 |
|
699 |
|
700 |
let rename_const f_const c = |
701 |
{ c with const_id = f_const c.const_id } |
702 |
|
703 |
let rename_typedef f_var t = |
704 |
match t.tydef_desc with |
705 |
| Tydec_enum tags -> { t with tydef_desc = Tydec_enum (List.map f_var tags) } |
706 |
| _ -> t |
707 |
|
708 |
let rename_prog f_node f_var f_const prog = |
709 |
List.rev ( |
710 |
List.fold_left (fun accu top -> |
711 |
(match top.top_decl_desc with |
712 |
| Node nd -> |
713 |
{ top with top_decl_desc = Node (rename_node f_node f_var f_const nd) } |
714 |
| Const c -> |
715 |
{ top with top_decl_desc = Const (rename_const f_const c) } |
716 |
| TypeDef tdef -> |
717 |
{ top with top_decl_desc = TypeDef (rename_typedef f_var tdef) } |
718 |
| ImportedNode _ |
719 |
| Open _ -> top) |
720 |
::accu |
721 |
) [] prog |
722 |
) |
723 |
|
724 |
(**********************************************************************) |
725 |
(* Pretty printers *) |
726 |
|
727 |
let pp_decl_type fmt tdecl = |
728 |
match tdecl.top_decl_desc with |
729 |
| Node nd -> |
730 |
fprintf fmt "%s: " nd.node_id; |
731 |
Utils.reset_names (); |
732 |
fprintf fmt "%a@ " Types.print_ty nd.node_type |
733 |
| ImportedNode ind -> |
734 |
fprintf fmt "%s: " ind.nodei_id; |
735 |
Utils.reset_names (); |
736 |
fprintf fmt "%a@ " Types.print_ty ind.nodei_type |
737 |
| Const _ | Open _ | TypeDef _ -> () |
738 |
|
739 |
let pp_prog_type fmt tdecl_list = |
740 |
Utils.fprintf_list ~sep:"" pp_decl_type fmt tdecl_list |
741 |
|
742 |
let pp_decl_clock fmt cdecl = |
743 |
match cdecl.top_decl_desc with |
744 |
| Node nd -> |
745 |
fprintf fmt "%s: " nd.node_id; |
746 |
Utils.reset_names (); |
747 |
fprintf fmt "%a@ " Clocks.print_ck nd.node_clock |
748 |
| ImportedNode ind -> |
749 |
fprintf fmt "%s: " ind.nodei_id; |
750 |
Utils.reset_names (); |
751 |
fprintf fmt "%a@ " Clocks.print_ck ind.nodei_clock |
752 |
| Const _ | Open _ | TypeDef _ -> () |
753 |
|
754 |
let pp_prog_clock fmt prog = |
755 |
Utils.fprintf_list ~sep:"" pp_decl_clock fmt prog |
756 |
|
757 |
let pp_error fmt = function |
758 |
Main_not_found -> |
759 |
fprintf fmt "Cannot compile node %s: could not find the node definition.@." |
760 |
!Options.main_node |
761 |
| Main_wrong_kind -> |
762 |
fprintf fmt |
763 |
"Name %s does not correspond to a (non-imported) node definition.@." |
764 |
!Options.main_node |
765 |
| No_main_specified -> |
766 |
fprintf fmt "No main node specified@." |
767 |
| Unbound_symbol sym -> |
768 |
fprintf fmt |
769 |
"%s is undefined.@." |
770 |
sym |
771 |
| Already_bound_symbol sym -> |
772 |
fprintf fmt |
773 |
"%s is already defined.@." |
774 |
sym |
775 |
| Unknown_library sym -> |
776 |
fprintf fmt |
777 |
"impossible to load library %s.lusic@.Please compile the corresponding interface or source file.@." |
778 |
sym |
779 |
|
780 |
(* filling node table with internal functions *) |
781 |
let vdecls_of_typ_ck cpt ty = |
782 |
let loc = Location.dummy_loc in |
783 |
List.map |
784 |
(fun _ -> incr cpt; |
785 |
let name = sprintf "_var_%d" !cpt in |
786 |
mkvar_decl loc (name, mktyp loc Tydec_any, mkclock loc Ckdec_any, false)) |
787 |
(Types.type_list_of_type ty) |
788 |
|
789 |
let mk_internal_node id = |
790 |
let spec = None in |
791 |
let ty = Env.lookup_value Basic_library.type_env id in |
792 |
let ck = Env.lookup_value Basic_library.clock_env id in |
793 |
let (tin, tout) = Types.split_arrow ty in |
794 |
(*eprintf "internal fun %s: %d -> %d@." id (List.length (Types.type_list_of_type tin)) (List.length (Types.type_list_of_type tout));*) |
795 |
let cpt = ref (-1) in |
796 |
mktop |
797 |
(ImportedNode |
798 |
{nodei_id = id; |
799 |
nodei_type = ty; |
800 |
nodei_clock = ck; |
801 |
nodei_inputs = vdecls_of_typ_ck cpt tin; |
802 |
nodei_outputs = vdecls_of_typ_ck cpt tout; |
803 |
nodei_stateless = Types.get_static_value ty <> None; |
804 |
nodei_spec = spec; |
805 |
nodei_prototype = None; |
806 |
nodei_in_lib = None; |
807 |
}) |
808 |
|
809 |
let add_internal_funs () = |
810 |
List.iter |
811 |
(fun id -> let nd = mk_internal_node id in Hashtbl.add node_table id nd) |
812 |
Basic_library.internal_funs |
813 |
|
814 |
|
815 |
|
816 |
(* Replace any occurence of a var in vars_to_replace by its associated |
817 |
expression in defs until e does not contain any such variables *) |
818 |
let rec substitute_expr vars_to_replace defs e = |
819 |
let se = substitute_expr vars_to_replace defs in |
820 |
{ e with expr_desc = |
821 |
let ed = e.expr_desc in |
822 |
match ed with |
823 |
| Expr_const _ -> ed |
824 |
| Expr_array el -> Expr_array (List.map se el) |
825 |
| Expr_access (e1, d) -> Expr_access (se e1, d) |
826 |
| Expr_power (e1, d) -> Expr_power (se e1, d) |
827 |
| Expr_tuple el -> Expr_tuple (List.map se el) |
828 |
| Expr_ite (c, t, e) -> Expr_ite (se c, se t, se e) |
829 |
| Expr_arrow (e1, e2)-> Expr_arrow (se e1, se e2) |
830 |
| Expr_fby (e1, e2) -> Expr_fby (se e1, se e2) |
831 |
| Expr_pre e' -> Expr_pre (se e') |
832 |
| Expr_when (e', i, l)-> Expr_when (se e', i, l) |
833 |
| Expr_merge (i, hl) -> Expr_merge (i, List.map (fun (t, h) -> (t, se h)) hl) |
834 |
| Expr_appl (i, e', i') -> Expr_appl (i, se e', i') |
835 |
| Expr_ident i -> |
836 |
if List.exists (fun v -> v.var_id = i) vars_to_replace then ( |
837 |
let eq_i eq = eq.eq_lhs = [i] in |
838 |
if List.exists eq_i defs then |
839 |
let sub = List.find eq_i defs in |
840 |
let sub' = se sub.eq_rhs in |
841 |
sub'.expr_desc |
842 |
else |
843 |
assert false |
844 |
) |
845 |
else |
846 |
ed |
847 |
|
848 |
} |
849 |
(* FAUT IL RETIRER ? |
850 |
|
851 |
let rec expr_to_eexpr expr = |
852 |
{ eexpr_tag = expr.expr_tag; |
853 |
eexpr_desc = expr_desc_to_eexpr_desc expr.expr_desc; |
854 |
eexpr_type = expr.expr_type; |
855 |
eexpr_clock = expr.expr_clock; |
856 |
eexpr_loc = expr.expr_loc |
857 |
} |
858 |
and expr_desc_to_eexpr_desc expr_desc = |
859 |
let conv = expr_to_eexpr in |
860 |
match expr_desc with |
861 |
| Expr_const c -> EExpr_const (match c with |
862 |
| Const_int x -> EConst_int x |
863 |
| Const_real x -> EConst_real x |
864 |
| Const_float x -> EConst_float x |
865 |
| Const_tag x -> EConst_tag x |
866 |
| _ -> assert false |
867 |
|
868 |
) |
869 |
| Expr_ident i -> EExpr_ident i |
870 |
| Expr_tuple el -> EExpr_tuple (List.map conv el) |
871 |
|
872 |
| Expr_arrow (e1, e2)-> EExpr_arrow (conv e1, conv e2) |
873 |
| Expr_fby (e1, e2) -> EExpr_fby (conv e1, conv e2) |
874 |
| Expr_pre e' -> EExpr_pre (conv e') |
875 |
| Expr_appl (i, e', i') -> |
876 |
EExpr_appl |
877 |
(i, conv e', match i' with None -> None | Some(id, _) -> Some id) |
878 |
|
879 |
| Expr_when _ |
880 |
| Expr_merge _ -> assert false |
881 |
| Expr_array _ |
882 |
| Expr_access _ |
883 |
| Expr_power _ -> assert false |
884 |
| Expr_ite (c, t, e) -> assert false |
885 |
| _ -> assert false |
886 |
|
887 |
*) |
888 |
let rec get_expr_calls nodes e = |
889 |
get_calls_expr_desc nodes e.expr_desc |
890 |
and get_calls_expr_desc nodes expr_desc = |
891 |
let get_calls = get_expr_calls nodes in |
892 |
match expr_desc with |
893 |
| Expr_const _ |
894 |
| Expr_ident _ -> Utils.ISet.empty |
895 |
| Expr_tuple el |
896 |
| Expr_array el -> List.fold_left (fun accu e -> Utils.ISet.union accu (get_calls e)) Utils.ISet.empty el |
897 |
| Expr_pre e1 |
898 |
| Expr_when (e1, _, _) |
899 |
| Expr_access (e1, _) |
900 |
| Expr_power (e1, _) -> get_calls e1 |
901 |
| Expr_ite (c, t, e) -> Utils.ISet.union (Utils.ISet.union (get_calls c) (get_calls t)) (get_calls e) |
902 |
| Expr_arrow (e1, e2) |
903 |
| Expr_fby (e1, e2) -> Utils.ISet.union (get_calls e1) (get_calls e2) |
904 |
| Expr_merge (_, hl) -> List.fold_left (fun accu (_, h) -> Utils.ISet.union accu (get_calls h)) Utils.ISet.empty hl |
905 |
| Expr_appl (i, e', i') -> |
906 |
if Basic_library.is_internal_fun i then |
907 |
(get_calls e') |
908 |
else |
909 |
let calls = Utils.ISet.add i (get_calls e') in |
910 |
let test = (fun n -> match n.top_decl_desc with Node nd -> nd.node_id = i | _ -> false) in |
911 |
if List.exists test nodes then |
912 |
match (List.find test nodes).top_decl_desc with |
913 |
| Node nd -> Utils.ISet.union (get_node_calls nodes nd) calls |
914 |
| _ -> assert false |
915 |
else |
916 |
calls |
917 |
|
918 |
and get_eq_calls nodes eq = |
919 |
get_expr_calls nodes eq.eq_rhs |
920 |
and get_node_calls nodes node = |
921 |
List.fold_left (fun accu eq -> Utils.ISet.union (get_eq_calls nodes eq) accu) Utils.ISet.empty (get_node_eqs node) |
922 |
|
923 |
let rec get_expr_vars vars e = |
924 |
get_expr_desc_vars vars e.expr_desc |
925 |
and get_expr_desc_vars vars expr_desc = |
926 |
match expr_desc with |
927 |
| Expr_const _ -> vars |
928 |
| Expr_ident x -> Utils.ISet.add x vars |
929 |
| Expr_tuple el |
930 |
| Expr_array el -> List.fold_left get_expr_vars vars el |
931 |
| Expr_pre e1 -> get_expr_vars vars e1 |
932 |
| Expr_when (e1, c, _) -> get_expr_vars (Utils.ISet.add c vars) e1 |
933 |
| Expr_access (e1, d) |
934 |
| Expr_power (e1, d) -> List.fold_left get_expr_vars vars [e1; expr_of_dimension d] |
935 |
| Expr_ite (c, t, e) -> List.fold_left get_expr_vars vars [c; t; e] |
936 |
| Expr_arrow (e1, e2) |
937 |
| Expr_fby (e1, e2) -> List.fold_left get_expr_vars vars [e1; e2] |
938 |
| Expr_merge (c, hl) -> List.fold_left (fun vars (_, h) -> get_expr_vars vars h) (Utils.ISet.add c vars) hl |
939 |
| Expr_appl (_, arg, None) -> get_expr_vars vars arg |
940 |
| Expr_appl (_, arg, Some r) -> List.fold_left get_expr_vars vars [arg; r] |
941 |
|
942 |
|
943 |
let rec expr_has_arrows e = |
944 |
expr_desc_has_arrows e.expr_desc |
945 |
and expr_desc_has_arrows expr_desc = |
946 |
match expr_desc with |
947 |
| Expr_const _ |
948 |
| Expr_ident _ -> false |
949 |
| Expr_tuple el |
950 |
| Expr_array el -> List.exists expr_has_arrows el |
951 |
| Expr_pre e1 |
952 |
| Expr_when (e1, _, _) |
953 |
| Expr_access (e1, _) |
954 |
| Expr_power (e1, _) -> expr_has_arrows e1 |
955 |
| Expr_ite (c, t, e) -> List.exists expr_has_arrows [c; t; e] |
956 |
| Expr_arrow (e1, e2) |
957 |
| Expr_fby (e1, e2) -> true |
958 |
| Expr_merge (_, hl) -> List.exists (fun (_, h) -> expr_has_arrows h) hl |
959 |
| Expr_appl (i, e', i') -> expr_has_arrows e' |
960 |
|
961 |
and eq_has_arrows eq = |
962 |
expr_has_arrows eq.eq_rhs |
963 |
and node_has_arrows node = |
964 |
List.exists (fun eq -> eq_has_arrows eq) (get_node_eqs node) |
965 |
|
966 |
(* Local Variables: *) |
967 |
(* compile-command:"make -C .." *) |
968 |
(* End: *) |