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