Project

General

Profile

Statistics
| Branch: | Tag: | Revision:

lustrec / src / corelang.ml @ 66359a5e

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 LustreSpec
14
(*open Dimension*)
15

    
16

    
17
exception Error of Location.t * Error.error_kind
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, parentid) =
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_parent_nodeid = parentid;
53
    var_type = Types.new_var ();
54
    var_clock = Clocks.new_var true;
55
    var_loc = loc }
56

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    
196

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

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

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

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

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

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

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

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

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

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

    
264

    
265
(* alias and type definition table *)
266

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    
499

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

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

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

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

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

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

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

    
554

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

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

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

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

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

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

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

    
636
(************************************************************************)
637
(*        Renaming                                                      *)
638

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

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

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

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

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

    
673

    
674

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

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

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

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

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

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

    
804

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

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

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

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

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

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

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

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

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

    
900

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

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

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

    
936

    
937

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

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

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

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

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

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

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

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

    
1101
and eq_has_arrows eq =
1102
  expr_has_arrows eq.eq_rhs
1103
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 
1104
and node_has_arrows node =
1105
  let eqs, auts = get_node_eqs node in
1106
  List.exists (fun eq -> eq_has_arrows eq) eqs || List.exists (fun aut -> aut_has_arrows aut) auts
1107

    
1108

    
1109

    
1110
let copy_var_decl vdecl =
1111
  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)
1112

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

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

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

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

    
1137

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

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