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