Project

General

Profile

Statistics
| Branch: | Tag: | Revision:

lustrec / src / corelang.ml @ 8446bf03

History | View | Annotate | Download (39 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 Lustre_types
14
open Machine_code_types
15
(*open Dimension*)
16

    
17

    
18
exception Error of Location.t * Error.error_kind
19

    
20
module VDeclModule =
21
struct (* Node module *)
22
  type t = var_decl
23
  let compare v1 v2 = compare v1.var_id v2.var_id
24
end
25

    
26
module VMap = Map.Make(VDeclModule)
27

    
28
module VSet = Set.Make(VDeclModule)
29

    
30
let dummy_type_dec = {ty_dec_desc=Tydec_any; ty_dec_loc=Location.dummy_loc}
31

    
32
let dummy_clock_dec = {ck_dec_desc=Ckdec_any; ck_dec_loc=Location.dummy_loc}
33

    
34

    
35

    
36
(************************************************************)
37
(* *)
38

    
39
let mktyp loc d =
40
  { ty_dec_desc = d; ty_dec_loc = loc }
41

    
42
let mkclock loc d =
43
  { ck_dec_desc = d; ck_dec_loc = loc }
44

    
45
let mkvar_decl loc ?(orig=false) (id, ty_dec, ck_dec, is_const, value, parentid) =
46
  assert (value = None || is_const);
47
  { var_id = id;
48
    var_orig = orig;
49
    var_dec_type = ty_dec;
50
    var_dec_clock = ck_dec;
51
    var_dec_const = is_const;
52
    var_dec_value = value;
53
    var_parent_nodeid = parentid;
54
    var_type = Types.new_var ();
55
    var_clock = Clocks.new_var true;
56
    var_loc = loc }
57

    
58
let mkexpr loc d =
59
  { expr_tag = Utils.new_tag ();
60
    expr_desc = d;
61
    expr_type = Types.new_var ();
62
    expr_clock = Clocks.new_var true;
63
    expr_delay = Delay.new_var ();
64
    expr_annot = None;
65
    expr_loc = loc }
66

    
67
let var_decl_of_const ?(parentid=None) c =
68
  { var_id = c.const_id;
69
    var_orig = true;
70
    var_dec_type = { ty_dec_loc = c.const_loc; ty_dec_desc = Tydec_any };
71
    var_dec_clock = { ck_dec_loc = c.const_loc; ck_dec_desc = Ckdec_any };
72
    var_dec_const = true;
73
    var_dec_value = None;
74
    var_parent_nodeid = parentid;
75
    var_type = c.const_type;
76
    var_clock = Clocks.new_var false;
77
    var_loc = c.const_loc }
78

    
79
let mk_new_name used id =
80
  let rec new_name name cpt =
81
    if used name
82
    then new_name (sprintf "_%s_%i" id cpt) (cpt+1)
83
    else name
84
  in new_name id 1
85

    
86
let mkeq loc (lhs, rhs) =
87
  { eq_lhs = lhs;
88
    eq_rhs = rhs;
89
    eq_loc = loc }
90

    
91
let mkassert loc expr =
92
  { assert_loc = loc;
93
    assert_expr = expr
94
  }
95

    
96
let mktop_decl loc own itf d =
97
  { top_decl_desc = d; top_decl_loc = loc; top_decl_owner = own; top_decl_itf = itf }
98

    
99
let mkpredef_call loc funname args =
100
  mkexpr loc (Expr_appl (funname, mkexpr loc (Expr_tuple args), None))
101

    
102
let is_clock_dec_type cty =
103
  match cty with
104
  | Tydec_clock _ -> true
105
  | _             -> false
106

    
107
let const_of_top top_decl =
108
  match top_decl.top_decl_desc with
109
  | Const c -> c
110
  | _ -> assert false
111

    
112
let node_of_top top_decl =
113
  match top_decl.top_decl_desc with
114
  | Node nd -> nd
115
  | _ -> raise Not_found
116

    
117
let imported_node_of_top top_decl =
118
  match top_decl.top_decl_desc with
119
  | ImportedNode ind -> ind
120
  | _ -> assert false
121

    
122
let typedef_of_top top_decl =
123
  match top_decl.top_decl_desc with
124
  | TypeDef tdef -> tdef
125
  | _ -> assert false
126

    
127
let dependency_of_top top_decl =
128
  match top_decl.top_decl_desc with
129
  | Open (local, dep) -> (local, dep)
130
  | _ -> assert false
131

    
132
let consts_of_enum_type top_decl =
133
  match top_decl.top_decl_desc with
134
  | TypeDef tdef ->
135
    (match tdef.tydef_desc with
136
    | Tydec_enum tags ->
137
       List.map
138
	 (fun tag ->
139
	   let cdecl = {
140
	     const_id = tag;
141
	     const_loc = top_decl.top_decl_loc;
142
	     const_value = Const_tag tag;
143
	     const_type = Type_predef.type_const tdef.tydef_id
144
	   } in
145
	   { top_decl with top_decl_desc = Const cdecl })
146
	 tags
147
     | _               -> [])
148
  | _ -> assert false
149

    
150
(************************************************************)
151
(*   Eexpr functions *)
152
(************************************************************)
153

    
154
let merge_node_annot ann1 ann2 =
155
  { requires = ann1.requires @ ann2.requires;
156
    ensures = ann1.ensures @ ann2.ensures;
157
    behaviors = ann1.behaviors @ ann2.behaviors;
158
    spec_loc = ann1.spec_loc
159
  }
160

    
161
let mkeexpr loc expr =
162
  { eexpr_tag = Utils.new_tag ();
163
    eexpr_qfexpr = expr;
164
    eexpr_quantifiers = [];
165
    eexpr_type = Types.new_var ();
166
    eexpr_clock = Clocks.new_var true;
167
    eexpr_normalized = None;
168
    eexpr_loc = loc }
169

    
170
let extend_eexpr q e = { e with eexpr_quantifiers = q@e.eexpr_quantifiers }
171

    
172
(*
173
let mkepredef_call loc funname args =
174
  mkeexpr loc (EExpr_appl (funname, mkeexpr loc (EExpr_tuple args), None))
175

    
176
let mkepredef_unary_call loc funname arg =
177
  mkeexpr loc (EExpr_appl (funname, arg, None))
178
*)
179

    
180
let merge_expr_annot ann1 ann2 =
181
  match ann1, ann2 with
182
    | None, None -> assert false
183
    | Some _, None -> ann1
184
    | None, Some _ -> ann2
185
    | Some ann1, Some ann2 -> Some {
186
      annots = ann1.annots @ ann2.annots;
187
      annot_loc = ann1.annot_loc
188
    }
189

    
190
let update_expr_annot node_id e annot =
191
  List.iter (fun (key, _) -> 
192
    Annotations.add_expr_ann node_id e.expr_tag key
193
  ) annot.annots;
194
  e.expr_annot <- merge_expr_annot e.expr_annot (Some annot);
195
  e
196

    
197

    
198
let mkinstr ?lustre_expr ?lustre_eq i =
199
  {
200
    instr_desc = i;
201
    (* lustre_expr = lustre_expr; *)
202
    lustre_eq = lustre_eq;
203
  }
204

    
205
let get_instr_desc i = i.instr_desc
206
let update_instr_desc i id = { i with instr_desc = id }
207

    
208
(***********************************************************)
209
(* Fast access to nodes, by name *)
210
let (node_table : (ident, top_decl) Hashtbl.t) = Hashtbl.create 30
211
let consts_table = Hashtbl.create 30
212

    
213
let print_node_table fmt () =
214
  begin
215
    Format.fprintf fmt "{ /* node table */@.";
216
    Hashtbl.iter (fun id nd ->
217
      Format.fprintf fmt "%s |-> %a"
218
	id
219
	Printers.pp_short_decl nd
220
    ) node_table;
221
    Format.fprintf fmt "}@."
222
  end
223

    
224
let print_consts_table fmt () =
225
  begin
226
    Format.fprintf fmt "{ /* consts table */@.";
227
    Hashtbl.iter (fun id const ->
228
      Format.fprintf fmt "%s |-> %a"
229
	id
230
	Printers.pp_const_decl (const_of_top const)
231
    ) consts_table;
232
    Format.fprintf fmt "}@."
233
  end
234

    
235
let node_name td =
236
    match td.top_decl_desc with 
237
    | Node nd         -> nd.node_id
238
    | ImportedNode nd -> nd.nodei_id
239
    | _ -> assert false
240

    
241
let is_generic_node td =
242
  match td.top_decl_desc with 
243
  | Node nd         -> List.exists (fun v -> v.var_dec_const) nd.node_inputs
244
  | ImportedNode nd -> List.exists (fun v -> v.var_dec_const) nd.nodei_inputs
245
  | _ -> assert false
246

    
247
let node_inputs td =
248
  match td.top_decl_desc with 
249
  | Node nd         -> nd.node_inputs
250
  | ImportedNode nd -> nd.nodei_inputs
251
  | _ -> assert false
252

    
253
let node_from_name id =
254
  try
255
    Hashtbl.find node_table id
256
  with Not_found -> (Format.eprintf "Unable to find any node named %s@ @?" id;
257
		     assert false)
258

    
259
let is_imported_node td =
260
  match td.top_decl_desc with 
261
  | Node nd         -> false
262
  | ImportedNode nd -> true
263
  | _ -> assert false
264

    
265

    
266
(* alias and type definition table *)
267

    
268
let mktop = mktop_decl Location.dummy_loc !Options.dest_dir false
269

    
270
let top_int_type = mktop (TypeDef {tydef_id = "int"; tydef_desc = Tydec_int})
271
let top_bool_type = mktop (TypeDef {tydef_id = "bool"; tydef_desc = Tydec_bool})
272
(* let top_float_type = mktop (TypeDef {tydef_id = "float"; tydef_desc = Tydec_float}) *)
273
let top_real_type = mktop (TypeDef {tydef_id = "real"; tydef_desc = Tydec_real})
274

    
275
let type_table =
276
  Utils.create_hashtable 20 [
277
    Tydec_int  , top_int_type;
278
    Tydec_bool , top_bool_type;
279
    (* Tydec_float, top_float_type; *)
280
    Tydec_real , top_real_type
281
  ]
282

    
283
let print_type_table fmt () =
284
  begin
285
    Format.fprintf fmt "{ /* type table */@.";
286
    Hashtbl.iter (fun tydec tdef ->
287
      Format.fprintf fmt "%a |-> %a"
288
	Printers.pp_var_type_dec_desc tydec
289
	Printers.pp_typedef (typedef_of_top tdef)
290
    ) type_table;
291
    Format.fprintf fmt "}@."
292
  end
293

    
294
let rec is_user_type typ =
295
  match typ with
296
  | Tydec_int | Tydec_bool | Tydec_real 
297
  (* | Tydec_float *) | Tydec_any | Tydec_const _ -> false
298
  | Tydec_clock typ' -> is_user_type typ'
299
  | _ -> true
300

    
301
let get_repr_type typ =
302
  let typ_def = (typedef_of_top (Hashtbl.find type_table typ)).tydef_desc in
303
  if is_user_type typ_def then typ else typ_def
304

    
305
let rec coretype_equal ty1 ty2 =
306
  let res =
307
  match ty1, ty2 with
308
  | Tydec_any           , _
309
  | _                   , Tydec_any             -> assert false
310
  | Tydec_const _       , Tydec_const _         -> get_repr_type ty1 = get_repr_type ty2
311
  | Tydec_const _       , _                     -> let ty1' = (typedef_of_top (Hashtbl.find type_table ty1)).tydef_desc
312
	       					   in (not (is_user_type ty1')) && coretype_equal ty1' ty2
313
  | _                   , Tydec_const _         -> coretype_equal ty2 ty1
314
  | Tydec_int           , Tydec_int
315
  | Tydec_real          , Tydec_real
316
  (* | Tydec_float         , Tydec_float *)
317
  | Tydec_bool          , Tydec_bool            -> true
318
  | Tydec_clock ty1     , Tydec_clock ty2       -> coretype_equal ty1 ty2
319
  | Tydec_array (d1,ty1), Tydec_array (d2, ty2) -> Dimension.is_eq_dimension d1 d2 && coretype_equal ty1 ty2
320
  | Tydec_enum tl1      , Tydec_enum tl2        -> List.sort compare tl1 = List.sort compare tl2
321
  | Tydec_struct fl1    , Tydec_struct fl2      ->
322
       List.length fl1 = List.length fl2
323
    && List.for_all2 (fun (f1, t1) (f2, t2) -> f1 = f2 && coretype_equal t1 t2)
324
      (List.sort (fun (f1,_) (f2,_) -> compare f1 f2) fl1)
325
      (List.sort (fun (f1,_) (f2,_) -> compare f1 f2) fl2)
326
  | _                                  -> false
327
  in ((*Format.eprintf "coretype_equal %a %a = %B@." Printers.pp_var_type_dec_desc ty1 Printers.pp_var_type_dec_desc ty2 res;*) res)
328

    
329
let tag_true = "true"
330
let tag_false = "false"
331
let tag_default = "default"
332

    
333
let const_is_bool c =
334
 match c with
335
 | Const_tag t -> t = tag_true || t = tag_false
336
 | _           -> false
337

    
338
(* Computes the negation of a boolean constant *)
339
let const_negation c =
340
  assert (const_is_bool c);
341
  match c with
342
  | Const_tag t when t = tag_true  -> Const_tag tag_false
343
  | _                              -> Const_tag tag_true
344

    
345
let const_or c1 c2 =
346
  assert (const_is_bool c1 && const_is_bool c2);
347
  match c1, c2 with
348
  | Const_tag t1, _            when t1 = tag_true -> c1
349
  | _           , Const_tag t2 when t2 = tag_true -> c2
350
  | _                                             -> Const_tag tag_false
351

    
352
let const_and c1 c2 =
353
  assert (const_is_bool c1 && const_is_bool c2);
354
  match c1, c2 with
355
  | Const_tag t1, _            when t1 = tag_false -> c1
356
  | _           , Const_tag t2 when t2 = tag_false -> c2
357
  | _                                              -> Const_tag tag_true
358

    
359
let const_xor c1 c2 =
360
  assert (const_is_bool c1 && const_is_bool c2);
361
   match c1, c2 with
362
  | Const_tag t1, Const_tag t2 when t1 <> t2  -> Const_tag tag_true
363
  | _                                         -> Const_tag tag_false
364

    
365
let const_impl c1 c2 =
366
  assert (const_is_bool c1 && const_is_bool c2);
367
  match c1, c2 with
368
  | Const_tag t1, _ when t1 = tag_false           -> Const_tag tag_true
369
  | _           , Const_tag t2 when t2 = tag_true -> Const_tag tag_true
370
  | _                                             -> Const_tag tag_false
371

    
372
(* To guarantee uniqueness of tags in enum types *)
373
let tag_table =
374
  Utils.create_hashtable 20 [
375
   tag_true, top_bool_type;
376
   tag_false, top_bool_type
377
  ]
378

    
379
(* To guarantee uniqueness of fields in struct types *)
380
let field_table =
381
  Utils.create_hashtable 20 [
382
  ]
383

    
384
let get_enum_type_tags cty =
385
(*Format.eprintf "get_enum_type_tags %a@." Printers.pp_var_type_dec_desc cty;*)
386
 match cty with
387
 | Tydec_bool    -> [tag_true; tag_false]
388
 | Tydec_const _ -> (match (typedef_of_top (Hashtbl.find type_table cty)).tydef_desc with
389
                     | Tydec_enum tl -> tl
390
                     | _             -> assert false)
391
 | _            -> assert false
392

    
393
let get_struct_type_fields cty =
394
 match cty with
395
 | Tydec_const _ -> (match (typedef_of_top (Hashtbl.find type_table cty)).tydef_desc with
396
                     | Tydec_struct fl -> fl
397
                     | _               -> assert false)
398
 | _            -> assert false
399

    
400
let const_of_bool b =
401
 Const_tag (if b then tag_true else tag_false)
402

    
403
(* let get_const c = snd (Hashtbl.find consts_table c) *)
404

    
405
let ident_of_expr expr =
406
 match expr.expr_desc with
407
 | Expr_ident id -> id
408
 | _             -> assert false
409

    
410
(* Generate a new ident expression from a declared variable *)
411
let expr_of_vdecl v =
412
  { expr_tag = Utils.new_tag ();
413
    expr_desc = Expr_ident v.var_id;
414
    expr_type = v.var_type;
415
    expr_clock = v.var_clock;
416
    expr_delay = Delay.new_var ();
417
    expr_annot = None;
418
    expr_loc = v.var_loc }
419

    
420
(* Caution, returns an untyped and unclocked expression *)
421
let expr_of_ident id loc =
422
  {expr_tag = Utils.new_tag ();
423
   expr_desc = Expr_ident id;
424
   expr_type = Types.new_var ();
425
   expr_clock = Clocks.new_var true;
426
   expr_delay = Delay.new_var ();
427
   expr_loc = loc;
428
   expr_annot = None}
429

    
430
let is_tuple_expr expr =
431
 match expr.expr_desc with
432
  | Expr_tuple _ -> true
433
  | _            -> false
434

    
435
let expr_list_of_expr expr =
436
  match expr.expr_desc with
437
  | Expr_tuple elist -> elist
438
  | _                -> [expr]
439

    
440
let expr_of_expr_list loc elist =
441
 match elist with
442
 | [t]  -> { t with expr_loc = loc }
443
 | t::_ ->
444
    let tlist = List.map (fun e -> e.expr_type) elist in
445
    let clist = List.map (fun e -> e.expr_clock) elist in
446
    { t with expr_desc = Expr_tuple elist;
447
	     expr_type = Type_predef.type_tuple tlist;
448
	     expr_clock = Clock_predef.ck_tuple clist;
449
	     expr_tag = Utils.new_tag ();
450
	     expr_loc = loc }
451
 | _    -> assert false
452

    
453
let call_of_expr expr =
454
 match expr.expr_desc with
455
 | Expr_appl (f, args, r) -> (f, expr_list_of_expr args, r)
456
 | _                      -> assert false
457

    
458
    
459
(* Conversion from dimension expr to standard expr, for the purpose of printing, typing, etc... *)
460
let rec expr_of_dimension dim =
461
  let open Dimension in
462
  match dim.dim_desc with
463
 | Dbool b        ->
464
     mkexpr dim.dim_loc (Expr_const (const_of_bool b))
465
 | Dint i         ->
466
     mkexpr dim.dim_loc (Expr_const (Const_int i))
467
 | Dident id      ->
468
     mkexpr dim.dim_loc (Expr_ident id)
469
 | Dite (c, t, e) ->
470
     mkexpr dim.dim_loc (Expr_ite (expr_of_dimension c, expr_of_dimension t, expr_of_dimension e))
471
 | Dappl (id, args) ->
472
     mkexpr dim.dim_loc (Expr_appl (id, expr_of_expr_list dim.dim_loc (List.map expr_of_dimension args), None))
473
 | Dlink dim'       -> expr_of_dimension dim'
474
 | Dvar
475
 | Dunivar          -> (Format.eprintf "internal error: Corelang.expr_of_dimension %a@." Dimension.pp_dimension dim;
476
			assert false)
477

    
478
let dimension_of_const loc const =
479
  let open Dimension in
480
 match const with
481
 | Const_int i                                    -> mkdim_int loc i
482
 | Const_tag t when t = tag_true || t = tag_false -> mkdim_bool loc (t = tag_true)
483
 | _                                              -> raise InvalidDimension
484

    
485
(* Conversion from standard expr to dimension expr, for the purpose of injecting static call arguments 
486
   into dimension expressions *)
487
let rec dimension_of_expr expr =
488
  let open Dimension in
489
  match expr.expr_desc with
490
  | Expr_const c  -> dimension_of_const expr.expr_loc c
491
  | Expr_ident id -> mkdim_ident expr.expr_loc id
492
  | Expr_appl (f, args, None) when Basic_library.is_expr_internal_fun expr ->
493
      let k = Types.get_static_value (Env.lookup_value Basic_library.type_env f) in
494
      if k = None then raise InvalidDimension;
495
      mkdim_appl expr.expr_loc f (List.map dimension_of_expr (expr_list_of_expr args))
496
  | Expr_ite (i, t, e)        ->
497
      mkdim_ite expr.expr_loc (dimension_of_expr i) (dimension_of_expr t) (dimension_of_expr e)
498
  | _ -> raise InvalidDimension (* not a simple dimension expression *)
499

    
500

    
501
let sort_handlers hl =
502
 List.sort (fun (t, _) (t', _) -> compare t t') hl
503

    
504
let num_10 = Num.num_of_int 10
505
  
506
let rec is_eq_const c1 c2 =
507
  match c1, c2 with
508
  | Const_real (n1, i1, _), Const_real (n2, i2, _)
509
    -> Num.(let n1 = n1 // (num_10 **/ (num_of_int i1)) in
510
	    let n2 = n2 // (num_10 **/ (num_of_int i2)) in
511
	    eq_num n1 n2)
512
  | Const_struct lcl1, Const_struct lcl2
513
    -> List.length lcl1 = List.length lcl2
514
    && List.for_all2 (fun (l1, c1) (l2, c2) -> l1 = l2 && is_eq_const c1 c2) lcl1 lcl2
515
  | _  -> c1 = c2
516

    
517
let rec is_eq_expr e1 e2 = match e1.expr_desc, e2.expr_desc with
518
  | Expr_const c1, Expr_const c2 -> is_eq_const c1 c2
519
  | Expr_ident i1, Expr_ident i2 -> i1 = i2
520
  | Expr_array el1, Expr_array el2 
521
  | Expr_tuple el1, Expr_tuple el2 -> 
522
    List.length el1 = List.length el2 && List.for_all2 is_eq_expr el1 el2 
523
  | Expr_arrow (e1, e2), Expr_arrow (e1', e2') -> is_eq_expr e1 e1' && is_eq_expr e2 e2'
524
  | Expr_fby (e1,e2), Expr_fby (e1',e2') -> is_eq_expr e1 e1' && is_eq_expr e2 e2'
525
  | 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
526
  (* | Expr_concat (e1,e2), Expr_concat (e1',e2') -> is_eq_expr e1 e1' && is_eq_expr e2 e2' *)
527
  (* | Expr_tail e, Expr_tail e' -> is_eq_expr e e' *)
528
  | Expr_pre e, Expr_pre e' -> is_eq_expr e e'
529
  | Expr_when (e, i, l), Expr_when (e', i', l') -> l=l' && i=i' && is_eq_expr e e'
530
  | 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')
531
  | Expr_appl (i, e, r), Expr_appl (i', e', r') -> i=i' && r=r' && is_eq_expr e e'
532
  | Expr_power (e1, i1), Expr_power (e2, i2)
533
  | Expr_access (e1, i1), Expr_access (e2, i2) -> is_eq_expr e1 e2 && is_eq_expr (expr_of_dimension i1) (expr_of_dimension i2)
534
  | _ -> false
535

    
536
let get_node_vars nd =
537
  nd.node_inputs @ nd.node_locals @ nd.node_outputs
538

    
539
let mk_new_node_name nd id =
540
  let used_vars = get_node_vars nd in
541
  let used v = List.exists (fun vdecl -> vdecl.var_id = v) used_vars in
542
  mk_new_name used id
543

    
544
let get_var id var_list =
545
  List.find (fun v -> v.var_id = id) var_list
546

    
547
let get_node_var id node =
548
  try
549
    get_var id (get_node_vars node)
550
  with Not_found -> begin
551
    (* Format.eprintf "Unable to find variable %s in node %s@.@?" id node.node_id; *)
552
    raise Not_found
553
  end
554

    
555

    
556
let get_node_eqs =
557
  let get_eqs stmts =
558
    List.fold_right
559
      (fun stmt (res_eq, res_aut) ->
560
	match stmt with
561
	| Eq eq -> eq :: res_eq, res_aut
562
	| Aut aut -> res_eq, aut::res_aut)
563
      stmts
564
      ([], []) in
565
  let table_eqs = Hashtbl.create 23 in
566
  (fun nd ->
567
    try
568
      let (old, res) = Hashtbl.find table_eqs nd.node_id
569
      in if old == nd.node_stmts then res else raise Not_found
570
    with Not_found -> 
571
      let res = get_eqs nd.node_stmts in
572
      begin
573
	Hashtbl.replace table_eqs nd.node_id (nd.node_stmts, res);
574
	res
575
      end)
576

    
577
let get_node_eq id node =
578
  let eqs, auts = get_node_eqs node in
579
  try
580
    List.find (fun eq -> List.mem id eq.eq_lhs) eqs
581
  with
582
    Not_found -> (* Shall be defined in automata auts *) raise Not_found
583
      
584
let get_nodes prog = 
585
  List.fold_left (
586
    fun nodes decl ->
587
      match decl.top_decl_desc with
588
	| Node _ -> decl::nodes
589
	| Const _ | ImportedNode _ | Open _ | TypeDef _ -> nodes  
590
  ) [] prog
591

    
592
let get_imported_nodes prog = 
593
  List.fold_left (
594
    fun nodes decl ->
595
      match decl.top_decl_desc with
596
	| ImportedNode _ -> decl::nodes
597
	| Const _ | Node _ | Open _ | TypeDef _-> nodes  
598
  ) [] prog
599

    
600
let get_consts prog = 
601
  List.fold_right (
602
    fun decl consts ->
603
      match decl.top_decl_desc with
604
	| Const _ -> decl::consts
605
	| Node _ | ImportedNode _ | Open _ | TypeDef _ -> consts  
606
  ) prog []
607

    
608
let get_typedefs prog = 
609
  List.fold_right (
610
    fun decl types ->
611
      match decl.top_decl_desc with
612
	| TypeDef _ -> decl::types
613
	| Node _ | ImportedNode _ | Open _ | Const _ -> types  
614
  ) prog []
615

    
616
let get_dependencies prog =
617
  List.fold_right (
618
    fun decl deps ->
619
      match decl.top_decl_desc with
620
	| Open _ -> decl::deps
621
	| Node _ | ImportedNode _ | TypeDef _ | Const _ -> deps  
622
  ) prog []
623

    
624
let get_node_interface nd =
625
 {nodei_id = nd.node_id;
626
  nodei_type = nd.node_type;
627
  nodei_clock = nd.node_clock;
628
  nodei_inputs = nd.node_inputs;
629
  nodei_outputs = nd.node_outputs;
630
  nodei_stateless = nd.node_dec_stateless;
631
  nodei_spec = nd.node_spec;
632
  (* nodei_annot = nd.node_annot; *)
633
  nodei_prototype = None;
634
  nodei_in_lib = [];
635
 }
636

    
637
(************************************************************************)
638
(*        Renaming                                                      *)
639

    
640
let rec rename_static rename cty =
641
 match cty with
642
 | Tydec_array (d, cty') -> Tydec_array (Dimension.expr_replace_expr rename d, rename_static rename cty')
643
 | Tydec_clock cty       -> Tydec_clock (rename_static rename cty)
644
 | Tydec_struct fl       -> Tydec_struct (List.map (fun (f, cty) -> f, rename_static rename cty) fl)
645
 | _                      -> cty
646

    
647
let rec rename_carrier rename cck =
648
 match cck with
649
 | Ckdec_bool cl -> Ckdec_bool (List.map (fun (c, l) -> rename c, l) cl)
650
 | _             -> cck
651

    
652
 (*Format.eprintf "Types.rename_static %a = %a@." print_ty ty print_ty res; res*)
653

    
654
(* applies the renaming function [fvar] to all variables of expression [expr] *)
655
 (* let rec expr_replace_var fvar expr = *)
656
 (*  { expr with expr_desc = expr_desc_replace_var fvar expr.expr_desc } *)
657

    
658
 (* and expr_desc_replace_var fvar expr_desc = *)
659
 (*   match expr_desc with *)
660
 (*   | Expr_const _ -> expr_desc *)
661
 (*   | Expr_ident i -> Expr_ident (fvar i) *)
662
 (*   | Expr_array el -> Expr_array (List.map (expr_replace_var fvar) el) *)
663
 (*   | Expr_access (e1, d) -> Expr_access (expr_replace_var fvar e1, d) *)
664
 (*   | Expr_power (e1, d) -> Expr_power (expr_replace_var fvar e1, d) *)
665
 (*   | Expr_tuple el -> Expr_tuple (List.map (expr_replace_var fvar) el) *)
666
 (*   | Expr_ite (c, t, e) -> Expr_ite (expr_replace_var fvar c, expr_replace_var fvar t, expr_replace_var fvar e) *)
667
 (*   | Expr_arrow (e1, e2)-> Expr_arrow (expr_replace_var fvar e1, expr_replace_var fvar e2)  *)
668
 (*   | Expr_fby (e1, e2) -> Expr_fby (expr_replace_var fvar e1, expr_replace_var fvar e2) *)
669
 (*   | Expr_pre e' -> Expr_pre (expr_replace_var fvar e') *)
670
 (*   | Expr_when (e', i, l)-> Expr_when (expr_replace_var fvar e', fvar i, l) *)
671
 (*   | Expr_merge (i, hl) -> Expr_merge (fvar i, List.map (fun (t, h) -> (t, expr_replace_var fvar h)) hl) *)
672
 (*   | Expr_appl (i, e', i') -> Expr_appl (i, expr_replace_var fvar e', Utils.option_map (expr_replace_var fvar) i') *)
673

    
674

    
675

    
676
 let rec rename_expr  f_node f_var expr =
677
   { expr with expr_desc = rename_expr_desc f_node f_var expr.expr_desc }
678
 and rename_expr_desc f_node f_var expr_desc =
679
   let re = rename_expr  f_node f_var in
680
   match expr_desc with
681
   | Expr_const _ -> expr_desc
682
   | Expr_ident i -> Expr_ident (f_var i)
683
   | Expr_array el -> Expr_array (List.map re el)
684
   | Expr_access (e1, d) -> Expr_access (re e1, d)
685
   | Expr_power (e1, d) -> Expr_power (re e1, d)
686
   | Expr_tuple el -> Expr_tuple (List.map re el)
687
   | Expr_ite (c, t, e) -> Expr_ite (re c, re t, re e)
688
   | Expr_arrow (e1, e2)-> Expr_arrow (re e1, re e2) 
689
   | Expr_fby (e1, e2) -> Expr_fby (re e1, re e2)
690
   | Expr_pre e' -> Expr_pre (re e')
691
   | Expr_when (e', i, l)-> Expr_when (re e', f_var i, l)
692
   | Expr_merge (i, hl) -> 
693
     Expr_merge (f_var i, List.map (fun (t, h) -> (t, re h)) hl)
694
   | Expr_appl (i, e', i') -> 
695
     Expr_appl (f_node i, re e', Utils.option_map re i')
696

    
697
 let rename_dec_type f_node f_var t = assert false (*
698
						     Types.rename_dim_type (Dimension.rename f_node f_var) t*)
699

    
700
 let rename_dec_clock f_node f_var c = assert false (* 
701
					  Clocks.rename_clock_expr f_var c*)
702
   
703
 let rename_var f_node f_var v = {
704
   v with
705
     var_id = f_var v.var_id;
706
     var_dec_type = rename_dec_type f_node f_var v.var_type;
707
     var_dec_clock = rename_dec_clock f_node f_var v.var_clock
708
 } 
709

    
710
 let rename_vars f_node f_var = List.map (rename_var f_node f_var) 
711

    
712
 let rec rename_eq f_node f_var eq = { eq with
713
   eq_lhs = List.map f_var eq.eq_lhs; 
714
   eq_rhs = rename_expr f_node f_var eq.eq_rhs
715
 } 
716
 and rename_handler f_node f_var  h = {h with
717
   hand_state = f_var h.hand_state;
718
   hand_unless = List.map (
719
     fun (l,e,b,id) -> l, rename_expr f_node f_var e, b, f_var id
720
   ) h.hand_unless;
721
   hand_until = List.map (
722
     fun (l,e,b,id) -> l, rename_expr f_node f_var e, b, f_var id
723
   ) h.hand_until;
724
   hand_locals = rename_vars f_node f_var h.hand_locals;
725
   hand_stmts = rename_stmts f_node f_var h.hand_stmts;
726
   hand_annots = rename_annots f_node f_var h.hand_annots;
727
   
728
 } 
729
 and rename_aut f_node f_var  aut = { aut with
730
   aut_id = f_var aut.aut_id;
731
   aut_handlers = List.map (rename_handler f_node f_var) aut.aut_handlers;
732
 }
733
 and rename_stmts f_node f_var stmts = List.map (fun stmt -> match stmt with
734
   | Eq eq -> Eq (rename_eq f_node f_var eq)
735
   | Aut at -> Aut (rename_aut f_node f_var at))
736
   stmts
737
 and rename_annotl f_node f_var  annots = 
738
   List.map 
739
     (fun (key, value) -> key, rename_eexpr f_node f_var value) 
740
     annots
741
 and rename_annot f_node f_var annot =
742
   { annot with annots = rename_annotl f_node f_var annot.annots }
743
 and rename_annots f_node f_var annots =
744
   List.map (rename_annot f_node f_var) annots
745
and rename_eexpr f_node f_var ee =
746
   { ee with
747
     eexpr_tag = Utils.new_tag ();
748
     eexpr_qfexpr = rename_expr f_node f_var ee.eexpr_qfexpr;
749
     eexpr_quantifiers = List.map (fun (typ,vdecls) -> typ, rename_vars f_node f_var vdecls) ee.eexpr_quantifiers;
750
     eexpr_normalized = Utils.option_map 
751
       (fun (vdecl, eqs, vdecls) ->
752
	 rename_var f_node f_var vdecl,
753
	 List.map (rename_eq f_node f_var) eqs,
754
	 rename_vars f_node f_var vdecls
755
       ) ee.eexpr_normalized;
756
     
757
   }
758
 
759
     
760
     
761
   
762
 let rename_node f_node f_var nd =
763
   let rename_var = rename_var f_node f_var in
764
   let rename_expr = rename_expr f_node f_var in
765
   let rename_stmts = rename_stmts f_node f_var in
766
   let inputs = List.map rename_var nd.node_inputs in
767
   let outputs = List.map rename_var nd.node_outputs in
768
   let locals = List.map rename_var nd.node_locals in
769
   let gen_calls = List.map rename_expr nd.node_gencalls in
770
   let node_checks = List.map (Dimension.rename f_node f_var)  nd.node_checks in
771
   let node_asserts = List.map 
772
     (fun a -> 
773
       {a with assert_expr = 
774
	   let expr = a.assert_expr in
775
	   rename_expr expr})
776
     nd.node_asserts
777
   in
778
   let node_stmts = rename_stmts nd.node_stmts
779

    
780
     
781
   in
782
   let spec = 
783
     Utils.option_map 
784
       (fun s -> assert false; (*rename_node_annot f_node f_var s*) ) (* TODO: implement! *) 
785
       nd.node_spec 
786
   in
787
   let annot = rename_annots f_node f_var nd.node_annot in
788
   {
789
     node_id = f_node nd.node_id;
790
     node_type = nd.node_type;
791
     node_clock = nd.node_clock;
792
     node_inputs = inputs;
793
     node_outputs = outputs;
794
     node_locals = locals;
795
     node_gencalls = gen_calls;
796
     node_checks = node_checks;
797
     node_asserts = node_asserts;
798
     node_stmts = node_stmts;
799
     node_dec_stateless = nd.node_dec_stateless;
800
     node_stateless = nd.node_stateless;
801
     node_spec = spec;
802
     node_annot = annot;
803
   }
804

    
805

    
806
let rename_const f_const c =
807
  { c with const_id = f_const c.const_id }
808

    
809
let rename_typedef f_var t =
810
  match t.tydef_desc with
811
  | Tydec_enum tags -> { t with tydef_desc = Tydec_enum (List.map f_var tags) }
812
  | _               -> t
813

    
814
let rename_prog f_node f_var f_const prog =
815
  List.rev (
816
    List.fold_left (fun accu top ->
817
      (match top.top_decl_desc with
818
      | Node nd -> 
819
	 { top with top_decl_desc = Node (rename_node f_node f_var nd) }
820
      | Const c -> 
821
	 { top with top_decl_desc = Const (rename_const f_const c) }
822
      | TypeDef tdef ->
823
	 { top with top_decl_desc = TypeDef (rename_typedef f_var tdef) }
824
      | ImportedNode _
825
      | Open _       -> top)
826
      ::accu
827
) [] prog
828
		   )
829

    
830
(* Applies the renaming function [fvar] to every rhs
831
   only when the corresponding lhs satisfies predicate [pvar] *)
832
 let eq_replace_rhs_var pvar fvar eq =
833
   let pvar l = List.exists pvar l in
834
   let rec replace lhs rhs =
835
     { rhs with expr_desc =
836
     match lhs with
837
     | []  -> assert false
838
     | [_] -> if pvar lhs then rename_expr_desc (fun x -> x) fvar rhs.expr_desc else rhs.expr_desc
839
     | _   ->
840
       (match rhs.expr_desc with
841
       | Expr_tuple tl ->
842
	 Expr_tuple (List.map2 (fun v e -> replace [v] e) lhs tl)
843
       | Expr_appl (f, arg, None) when Basic_library.is_expr_internal_fun rhs ->
844
	 let args = expr_list_of_expr arg in
845
	 Expr_appl (f, expr_of_expr_list arg.expr_loc (List.map (replace lhs) args), None)
846
       | Expr_array _
847
       | Expr_access _
848
       | Expr_power _
849
       | Expr_const _
850
       | Expr_ident _
851
       | Expr_appl _   ->
852
	 if pvar lhs
853
	 then rename_expr_desc (fun x -> x) fvar rhs.expr_desc
854
	 else rhs.expr_desc
855
       | Expr_ite (c, t, e)   -> Expr_ite (replace lhs c, replace lhs t, replace lhs e)
856
       | Expr_arrow (e1, e2)  -> Expr_arrow (replace lhs e1, replace lhs e2) 
857
       | Expr_fby (e1, e2)    -> Expr_fby (replace lhs e1, replace lhs e2)
858
       | Expr_pre e'          -> Expr_pre (replace lhs e')
859
       | Expr_when (e', i, l) -> let i' = if pvar lhs then fvar i else i
860
				 in Expr_when (replace lhs e', i', l)
861
       | Expr_merge (i, hl)   -> let i' = if pvar lhs then fvar i else i
862
				 in Expr_merge (i', List.map (fun (t, h) -> (t, replace lhs h)) hl)
863
       )
864
     }
865
   in { eq with eq_rhs = replace eq.eq_lhs eq.eq_rhs }
866

    
867
    
868
(**********************************************************************)
869
(* Pretty printers *)
870

    
871
let pp_decl_type fmt tdecl =
872
  match tdecl.top_decl_desc with
873
  | Node nd ->
874
    fprintf fmt "%s: " nd.node_id;
875
    Utils.reset_names ();
876
    fprintf fmt "%a@ " Types.print_ty nd.node_type
877
  | ImportedNode ind ->
878
    fprintf fmt "%s: " ind.nodei_id;
879
    Utils.reset_names ();
880
    fprintf fmt "%a@ " Types.print_ty ind.nodei_type
881
  | Const _ | Open _ | TypeDef _ -> ()
882

    
883
let pp_prog_type fmt tdecl_list =
884
  Utils.fprintf_list ~sep:"" pp_decl_type fmt tdecl_list
885

    
886
let pp_decl_clock fmt cdecl =
887
  match cdecl.top_decl_desc with
888
  | Node nd ->
889
    fprintf fmt "%s: " nd.node_id;
890
    Utils.reset_names ();
891
    fprintf fmt "%a@ " Clocks.print_ck nd.node_clock
892
  | ImportedNode ind ->
893
    fprintf fmt "%s: " ind.nodei_id;
894
    Utils.reset_names ();
895
    fprintf fmt "%a@ " Clocks.print_ck ind.nodei_clock
896
  | Const _ | Open _ | TypeDef _ -> ()
897

    
898
let pp_prog_clock fmt prog =
899
  Utils.fprintf_list ~sep:"" pp_decl_clock fmt prog
900

    
901

    
902
(* filling node table with internal functions *)
903
let vdecls_of_typ_ck cpt ty =
904
  let loc = Location.dummy_loc in
905
  List.map
906
    (fun _ -> incr cpt;
907
              let name = sprintf "_var_%d" !cpt in
908
              mkvar_decl loc (name, mktyp loc Tydec_any, mkclock loc Ckdec_any, false, None, None))
909
    (Types.type_list_of_type ty)
910

    
911
let mk_internal_node id =
912
  let spec = None in
913
  let ty = Env.lookup_value Basic_library.type_env id in
914
  let ck = Env.lookup_value Basic_library.clock_env id in
915
  let (tin, tout) = Types.split_arrow ty in
916
  (*eprintf "internal fun %s: %d -> %d@." id (List.length (Types.type_list_of_type tin)) (List.length (Types.type_list_of_type tout));*)
917
  let cpt = ref (-1) in
918
  mktop
919
    (ImportedNode
920
       {nodei_id = id;
921
	nodei_type = ty;
922
	nodei_clock = ck;
923
	nodei_inputs = vdecls_of_typ_ck cpt tin;
924
	nodei_outputs = vdecls_of_typ_ck cpt tout;
925
	nodei_stateless = Types.get_static_value ty <> None;
926
	nodei_spec = spec;
927
	(* nodei_annot = []; *)
928
	nodei_prototype = None;
929
       	nodei_in_lib = [];
930
       })
931

    
932
let add_internal_funs () =
933
  List.iter
934
    (fun id -> let nd = mk_internal_node id in Hashtbl.add node_table id nd)
935
    Basic_library.internal_funs
936

    
937

    
938

    
939
(* Replace any occurence of a var in vars_to_replace by its associated
940
   expression in defs until e does not contain any such variables *)
941
let rec substitute_expr vars_to_replace defs e =
942
  let se = substitute_expr vars_to_replace defs in
943
  { e with expr_desc = 
944
      let ed = e.expr_desc in
945
      match ed with
946
      | Expr_const _ -> ed
947
      | Expr_array el -> Expr_array (List.map se el)
948
      | Expr_access (e1, d) -> Expr_access (se e1, d)
949
      | Expr_power (e1, d) -> Expr_power (se e1, d)
950
      | Expr_tuple el -> Expr_tuple (List.map se el)
951
      | Expr_ite (c, t, e) -> Expr_ite (se c, se t, se e)
952
      | Expr_arrow (e1, e2)-> Expr_arrow (se e1, se e2) 
953
      | Expr_fby (e1, e2) -> Expr_fby (se e1, se e2)
954
      | Expr_pre e' -> Expr_pre (se e')
955
      | Expr_when (e', i, l)-> Expr_when (se e', i, l)
956
      | Expr_merge (i, hl) -> Expr_merge (i, List.map (fun (t, h) -> (t, se h)) hl)
957
      | Expr_appl (i, e', i') -> Expr_appl (i, se e', i')
958
      | Expr_ident i -> 
959
	if List.exists (fun v -> v.var_id = i) vars_to_replace then (
960
	  let eq_i eq = eq.eq_lhs = [i] in
961
	  if List.exists eq_i defs then
962
	    let sub = List.find eq_i defs in
963
	    let sub' = se sub.eq_rhs in
964
	    sub'.expr_desc
965
	  else 
966
	    assert false
967
	)
968
	else
969
	  ed
970

    
971
  }
972
  
973
 let rec expr_to_eexpr  expr =
974
   { eexpr_tag = expr.expr_tag;
975
     eexpr_qfexpr = expr;
976
     eexpr_quantifiers = [];
977
     eexpr_type = expr.expr_type;
978
     eexpr_clock = expr.expr_clock;
979
     eexpr_loc = expr.expr_loc;
980
     eexpr_normalized = None
981
   }
982
 (* and expr_desc_to_eexpr_desc expr_desc = *)
983
 (*   let conv = expr_to_eexpr in *)
984
 (*   match expr_desc with *)
985
 (*   | Expr_const c -> EExpr_const (match c with *)
986
 (*     | Const_int x -> EConst_int x  *)
987
 (*     | Const_real x -> EConst_real x  *)
988
 (*     | Const_float x -> EConst_float x  *)
989
 (*     | Const_tag x -> EConst_tag x  *)
990
 (*     | _ -> assert false *)
991

    
992
 (*   ) *)
993
 (*   | Expr_ident i -> EExpr_ident i *)
994
 (*   | Expr_tuple el -> EExpr_tuple (List.map conv el) *)
995

    
996
 (*   | Expr_arrow (e1, e2)-> EExpr_arrow (conv e1, conv e2)  *)
997
 (*   | Expr_fby (e1, e2) -> EExpr_fby (conv e1, conv e2) *)
998
 (*   | Expr_pre e' -> EExpr_pre (conv e') *)
999
 (*   | Expr_appl (i, e', i') ->  *)
1000
 (*     EExpr_appl  *)
1001
 (*       (i, conv e', match i' with None -> None | Some(id, _) -> Some id) *)
1002

    
1003
 (*   | Expr_when _ *)
1004
 (*   | Expr_merge _ -> assert false *)
1005
 (*   | Expr_array _  *)
1006
 (*   | Expr_access _  *)
1007
 (*   | Expr_power _  -> assert false *)
1008
 (*   | Expr_ite (c, t, e) -> assert false  *)
1009
 (*   | _ -> assert false *)
1010
      
1011
     
1012
let rec get_expr_calls nodes e =
1013
  let get_calls = get_expr_calls nodes in
1014
  match e.expr_desc with
1015
  | Expr_const _ 
1016
   | Expr_ident _ -> Utils.ISet.empty
1017
   | Expr_tuple el
1018
   | Expr_array el -> List.fold_left (fun accu e -> Utils.ISet.union accu (get_calls e)) Utils.ISet.empty el
1019
   | Expr_pre e1 
1020
   | Expr_when (e1, _, _) 
1021
   | Expr_access (e1, _) 
1022
   | Expr_power (e1, _) -> get_calls e1
1023
   | Expr_ite (c, t, e) -> Utils.ISet.union (Utils.ISet.union (get_calls c) (get_calls t)) (get_calls e) 
1024
   | Expr_arrow (e1, e2) 
1025
   | Expr_fby (e1, e2) -> Utils.ISet.union (get_calls e1) (get_calls e2)
1026
   | Expr_merge (_, hl) -> List.fold_left (fun accu (_, h) -> Utils.ISet.union accu (get_calls h)) Utils.ISet.empty  hl
1027
   | Expr_appl (i, e', i') -> 
1028
     if Basic_library.is_expr_internal_fun e then 
1029
       (get_calls e') 
1030
     else
1031
       let calls =  Utils.ISet.add i (get_calls e') in
1032
       let test = (fun n -> match n.top_decl_desc with Node nd -> nd.node_id = i | _ -> false) in
1033
       if List.exists test nodes then
1034
	 match (List.find test nodes).top_decl_desc with
1035
	 | Node nd -> Utils.ISet.union (get_node_calls nodes nd) calls
1036
	 | _ -> assert false
1037
       else 
1038
	 calls
1039

    
1040
and get_eq_calls nodes eq =
1041
  get_expr_calls nodes eq.eq_rhs
1042
and get_aut_handler_calls nodes h =
1043
  List.fold_left (fun accu stmt -> match stmt with
1044
  | Eq eq -> Utils.ISet.union (get_eq_calls nodes eq) accu
1045
  | Aut aut' ->  Utils.ISet.union (get_aut_calls nodes aut') accu
1046
  ) Utils.ISet.empty h.hand_stmts 
1047
and get_aut_calls nodes aut =
1048
  List.fold_left (fun accu h -> Utils.ISet.union (get_aut_handler_calls nodes h) accu)
1049
    Utils.ISet.empty aut.aut_handlers
1050
and get_node_calls nodes node =
1051
  let eqs, auts = get_node_eqs node in
1052
  let aut_calls =
1053
    List.fold_left
1054
      (fun accu aut -> Utils.ISet.union (get_aut_calls nodes aut) accu)
1055
      Utils.ISet.empty auts
1056
  in
1057
  List.fold_left
1058
    (fun accu eq -> Utils.ISet.union (get_eq_calls nodes eq) accu)
1059
    aut_calls eqs
1060

    
1061
let get_expr_vars e =
1062
  let rec get_expr_vars vars e =
1063
    get_expr_desc_vars vars e.expr_desc
1064
  and get_expr_desc_vars vars expr_desc =
1065
    (*Format.eprintf "get_expr_desc_vars expr=%a@." Printers.pp_expr (mkexpr Location.dummy_loc expr_desc);*)
1066
  match expr_desc with
1067
  | Expr_const _ -> vars
1068
  | Expr_ident x -> Utils.ISet.add x vars
1069
  | Expr_tuple el
1070
  | Expr_array el -> List.fold_left get_expr_vars vars el
1071
  | Expr_pre e1 -> get_expr_vars vars e1
1072
  | Expr_when (e1, c, _) -> get_expr_vars (Utils.ISet.add c vars) e1 
1073
  | Expr_access (e1, d) 
1074
  | Expr_power (e1, d)   -> List.fold_left get_expr_vars vars [e1; expr_of_dimension d]
1075
  | Expr_ite (c, t, e) -> List.fold_left get_expr_vars vars [c; t; e]
1076
  | Expr_arrow (e1, e2) 
1077
  | Expr_fby (e1, e2) -> List.fold_left get_expr_vars vars [e1; e2]
1078
  | Expr_merge (c, hl) -> List.fold_left (fun vars (_, h) -> get_expr_vars vars h) (Utils.ISet.add c vars) hl
1079
  | Expr_appl (_, arg, None)   -> get_expr_vars vars arg
1080
  | Expr_appl (_, arg, Some r) -> List.fold_left get_expr_vars vars [arg; r]
1081
  in
1082
  get_expr_vars Utils.ISet.empty e 
1083

    
1084
let rec expr_has_arrows e =
1085
  expr_desc_has_arrows e.expr_desc
1086
and expr_desc_has_arrows expr_desc =
1087
  match expr_desc with
1088
  | Expr_const _ 
1089
  | Expr_ident _ -> false
1090
  | Expr_tuple el
1091
  | Expr_array el -> List.exists expr_has_arrows el
1092
  | Expr_pre e1 
1093
  | Expr_when (e1, _, _) 
1094
  | Expr_access (e1, _) 
1095
  | Expr_power (e1, _) -> expr_has_arrows e1
1096
  | Expr_ite (c, t, e) -> List.exists expr_has_arrows [c; t; e]
1097
  | Expr_arrow (e1, e2) 
1098
  | Expr_fby (e1, e2) -> true
1099
  | Expr_merge (_, hl) -> List.exists (fun (_, h) -> expr_has_arrows h) hl
1100
  | Expr_appl (i, e', i') -> expr_has_arrows e'
1101

    
1102
and eq_has_arrows eq =
1103
  expr_has_arrows eq.eq_rhs
1104
and aut_has_arrows aut = List.exists (fun h -> List.exists (fun stmt -> match stmt with Eq eq -> eq_has_arrows eq | Aut aut' -> aut_has_arrows aut') h.hand_stmts ) aut.aut_handlers 
1105
and node_has_arrows node =
1106
  let eqs, auts = get_node_eqs node in
1107
  List.exists (fun eq -> eq_has_arrows eq) eqs || List.exists (fun aut -> aut_has_arrows aut) auts
1108

    
1109

    
1110

    
1111
let copy_var_decl vdecl =
1112
  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, vdecl.var_parent_nodeid)
1113

    
1114
let copy_const cdecl =
1115
  { cdecl with const_type = Types.new_var () }
1116

    
1117
let copy_node nd =
1118
  { nd with
1119
    node_type     = Types.new_var ();
1120
    node_clock    = Clocks.new_var true;
1121
    node_inputs   = List.map copy_var_decl nd.node_inputs;
1122
    node_outputs  = List.map copy_var_decl nd.node_outputs;
1123
    node_locals   = List.map copy_var_decl nd.node_locals;
1124
    node_gencalls = [];
1125
    node_checks   = [];
1126
    node_stateless = None;
1127
  }
1128

    
1129
let copy_top top =
1130
  match top.top_decl_desc with
1131
  | Node nd -> { top with top_decl_desc = Node (copy_node nd)  }
1132
  | Const c -> { top with top_decl_desc = Const (copy_const c) }
1133
  | _       -> top
1134

    
1135
let copy_prog top_list =
1136
  List.map copy_top top_list
1137

    
1138

    
1139
let rec expr_contains_expr expr_tag expr  =
1140
  let search = expr_contains_expr expr_tag in
1141
  expr.expr_tag = expr_tag ||
1142
      (
1143
	match expr.expr_desc with
1144
	| Expr_const _ -> false
1145
	| Expr_array el -> List.exists search el
1146
	| Expr_access (e1, _) 
1147
	| Expr_power (e1, _) -> search e1
1148
	| Expr_tuple el -> List.exists search el
1149
	| Expr_ite (c, t, e) -> List.exists search [c;t;e]
1150
	| Expr_arrow (e1, e2)
1151
	| Expr_fby (e1, e2) -> List.exists search [e1; e2]
1152
	| Expr_pre e' 
1153
	| Expr_when (e', _, _) -> search e'
1154
	| Expr_merge (_, hl) -> List.exists (fun (_, h) -> search h) hl
1155
	| Expr_appl (_, e', None) -> search e' 
1156
	| Expr_appl (_, e', Some e'') -> List.exists search [e'; e''] 
1157
	| Expr_ident _ -> false
1158
      )
1159

    
1160
(* Local Variables: *)
1161
(* compile-command:"make -C .." *)
1162
(* End: *)