Project

General

Profile

Download (42.2 KB) Statistics
| Branch: | Tag: | Revision:
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: sig
29
  include Set.S
30
  val pp: Format.formatter -> t -> unit
31
  val get: ident -> t -> elt
32
end with type elt = var_decl =
33
  struct
34
    include Set.Make(VDeclModule)
35
    let pp fmt s =
36
      Format.fprintf fmt "{@[%a}@]" (Utils.fprintf_list ~sep:",@ " Printers.pp_var) (elements s)
37
    (* Strangley the find_first function of Set.Make is incorrect (at
38
       the current time of writting this comment. Had to switch to
39
       lists *)
40
    let get id s = List.find (fun v -> v.var_id = id) (elements s)
41
  end
42
let dummy_type_dec = {ty_dec_desc=Tydec_any; ty_dec_loc=Location.dummy_loc}
43

    
44
let dummy_clock_dec = {ck_dec_desc=Ckdec_any; ck_dec_loc=Location.dummy_loc}
45

    
46

    
47

    
48
(************************************************************)
49
(* *)
50

    
51
let mktyp loc d =
52
  { ty_dec_desc = d; ty_dec_loc = loc }
53

    
54
let mkclock loc d =
55
  { ck_dec_desc = d; ck_dec_loc = loc }
56

    
57
let mkvar_decl loc ?(orig=false) (id, ty_dec, ck_dec, is_const, value, parentid) =
58
  assert (value = None || is_const);
59
  { var_id = id;
60
    var_orig = orig;
61
    var_dec_type = ty_dec;
62
    var_dec_clock = ck_dec;
63
    var_dec_const = is_const;
64
    var_dec_value = value;
65
    var_parent_nodeid = parentid;
66
    var_type = Types.new_var ();
67
    var_clock = Clocks.new_var true;
68
    var_loc = loc }
69

    
70
let dummy_var_decl name typ =
71
  {
72
    var_id = name;
73
    var_orig = false;
74
    var_dec_type = dummy_type_dec;
75
    var_dec_clock = dummy_clock_dec;
76
    var_dec_const = false;
77
    var_dec_value = None;
78
    var_parent_nodeid = None;
79
    var_type =  typ;
80
    var_clock = Clocks.new_ck Clocks.Cvar true;
81
    var_loc = Location.dummy_loc
82
  }
83

    
84
let mkexpr loc d =
85
  { expr_tag = Utils.new_tag ();
86
    expr_desc = d;
87
    expr_type = Types.new_var ();
88
    expr_clock = Clocks.new_var true;
89
    expr_delay = Delay.new_var ();
90
    expr_annot = None;
91
    expr_loc = loc }
92

    
93
let var_decl_of_const ?(parentid=None) c =
94
  { var_id = c.const_id;
95
    var_orig = true;
96
    var_dec_type = { ty_dec_loc = c.const_loc; ty_dec_desc = Tydec_any };
97
    var_dec_clock = { ck_dec_loc = c.const_loc; ck_dec_desc = Ckdec_any };
98
    var_dec_const = true;
99
    var_dec_value = None;
100
    var_parent_nodeid = parentid;
101
    var_type = c.const_type;
102
    var_clock = Clocks.new_var false;
103
    var_loc = c.const_loc }
104

    
105
let mk_new_name used id =
106
  let rec new_name name cpt =
107
    if used name
108
    then new_name (sprintf "_%s_%i" id cpt) (cpt+1)
109
    else name
110
  in new_name id 1
111

    
112
let mkeq loc (lhs, rhs) =
113
  { eq_lhs = lhs;
114
    eq_rhs = rhs;
115
    eq_loc = loc }
116

    
117
let mkassert loc expr =
118
  { assert_loc = loc;
119
    assert_expr = expr
120
  }
121

    
122
let mktop_decl loc own itf d =
123
  { top_decl_desc = d; top_decl_loc = loc; top_decl_owner = own; top_decl_itf = itf }
124

    
125
let mkpredef_call loc funname args =
126
  mkexpr loc (Expr_appl (funname, mkexpr loc (Expr_tuple args), None))
127

    
128
let is_clock_dec_type cty =
129
  match cty with
130
  | Tydec_clock _ -> true
131
  | _             -> false
132

    
133
let const_of_top top_decl =
134
  match top_decl.top_decl_desc with
135
  | Const c -> c
136
  | _ -> assert false
137

    
138
let node_of_top top_decl =
139
  match top_decl.top_decl_desc with
140
  | Node nd -> nd
141
  | _ -> raise Not_found
142

    
143
let imported_node_of_top top_decl =
144
  match top_decl.top_decl_desc with
145
  | ImportedNode ind -> ind
146
  | _ -> assert false
147

    
148
let typedef_of_top top_decl =
149
  match top_decl.top_decl_desc with
150
  | TypeDef tdef -> tdef
151
  | _ -> assert false
152

    
153
let dependency_of_top top_decl =
154
  match top_decl.top_decl_desc with
155
  | Open (local, dep) -> (local, dep)
156
  | _ -> assert false
157

    
158
let consts_of_enum_type top_decl =
159
  match top_decl.top_decl_desc with
160
  | TypeDef tdef ->
161
    (match tdef.tydef_desc with
162
    | Tydec_enum tags ->
163
       List.map
164
	 (fun tag ->
165
	   let cdecl = {
166
	     const_id = tag;
167
	     const_loc = top_decl.top_decl_loc;
168
	     const_value = Const_tag tag;
169
	     const_type = Type_predef.type_const tdef.tydef_id
170
	   } in
171
	   { top_decl with top_decl_desc = Const cdecl })
172
	 tags
173
     | _               -> [])
174
  | _ -> assert false
175

    
176
(************************************************************)
177
(*   Eexpr functions *)
178
(************************************************************)
179

    
180

    
181
let empty_contract =
182
  {
183
    consts = []; locals = []; stmts = []; assume = []; guarantees = []; modes = []; imports = []; spec_loc = Location.dummy_loc;
184
  }
185

    
186
(* For const declaration we do as for regular lustre node.
187
But for local flows we registered the variable and the lustre flow definition *)
188
let mk_contract_var id is_const type_opt expr loc =
189
  let typ = match type_opt with None -> mktyp loc Tydec_any | Some t -> t in
190
  if is_const then
191
  let v = mkvar_decl loc (id, typ, mkclock loc Ckdec_any, is_const, Some expr, None) in
192
  { empty_contract with consts = [v]; spec_loc = loc; }
193
  else
194
    let v = mkvar_decl loc (id, typ, mkclock loc Ckdec_any, is_const, None, None) in
195
    let eq = mkeq loc ([id], expr) in 
196
    { empty_contract with locals = [v]; stmts = [Eq eq]; spec_loc = loc; }
197

    
198
let mk_contract_guarantees eexpr =
199
  { empty_contract with guarantees = [eexpr]; spec_loc = eexpr.eexpr_loc }
200

    
201
let mk_contract_assume eexpr =
202
  { empty_contract with assume = [eexpr]; spec_loc = eexpr.eexpr_loc }
203

    
204
let mk_contract_mode id rl el loc =
205
  { empty_contract with modes = [{ mode_id = id; require = rl; ensure = el; mode_loc = loc; }]; spec_loc = loc }
206

    
207
let mk_contract_import id ins outs loc =
208
  { empty_contract with imports = [{import_nodeid = id; inputs = ins; outputs = outs; import_loc = loc; }]; spec_loc = loc }
209

    
210
    
211
let merge_contracts ann1 ann2 = (* keeping the first item loc *)
212
  { consts = ann1.consts @ ann2.consts;
213
    locals = ann1.locals @ ann2.locals;
214
    stmts = ann1.stmts @ ann2.stmts;
215
    assume = ann1.assume @ ann2.assume;
216
    guarantees = ann1.guarantees @ ann2.guarantees;
217
    modes = ann1.modes @ ann2.modes;
218
    imports = ann1.imports @ ann2.imports;
219
    spec_loc = ann1.spec_loc
220
  }
221

    
222
let mkeexpr loc expr =
223
  { eexpr_tag = Utils.new_tag ();
224
    eexpr_qfexpr = expr;
225
    eexpr_quantifiers = [];
226
    eexpr_type = Types.new_var ();
227
    eexpr_clock = Clocks.new_var true;
228
    eexpr_loc = loc }
229

    
230
let extend_eexpr q e = { e with eexpr_quantifiers = q@e.eexpr_quantifiers }
231

    
232
(*
233
let mkepredef_call loc funname args =
234
  mkeexpr loc (EExpr_appl (funname, mkeexpr loc (EExpr_tuple args), None))
235

    
236
let mkepredef_unary_call loc funname arg =
237
  mkeexpr loc (EExpr_appl (funname, arg, None))
238
*)
239

    
240
let merge_expr_annot ann1 ann2 =
241
  match ann1, ann2 with
242
    | None, None -> assert false
243
    | Some _, None -> ann1
244
    | None, Some _ -> ann2
245
    | Some ann1, Some ann2 -> Some {
246
      annots = ann1.annots @ ann2.annots;
247
      annot_loc = ann1.annot_loc
248
    }
249

    
250
let update_expr_annot node_id e annot =
251
  List.iter (fun (key, _) -> 
252
    Annotations.add_expr_ann node_id e.expr_tag key
253
  ) annot.annots;
254
  e.expr_annot <- merge_expr_annot e.expr_annot (Some annot);
255
  e
256

    
257

    
258
let mkinstr ?lustre_expr ?lustre_eq i =
259
  {
260
    instr_desc = i;
261
    (* lustre_expr = lustre_expr; *)
262
    lustre_eq = lustre_eq;
263
  }
264

    
265
let get_instr_desc i = i.instr_desc
266
let update_instr_desc i id = { i with instr_desc = id }
267

    
268
(***********************************************************)
269
(* Fast access to nodes, by name *)
270
let (node_table : (ident, top_decl) Hashtbl.t) = Hashtbl.create 30
271
let consts_table = Hashtbl.create 30
272

    
273
let print_node_table fmt () =
274
  begin
275
    Format.fprintf fmt "{ /* node table */@.";
276
    Hashtbl.iter (fun id nd ->
277
      Format.fprintf fmt "%s |-> %a"
278
	id
279
	Printers.pp_short_decl nd
280
    ) node_table;
281
    Format.fprintf fmt "}@."
282
  end
283

    
284
let print_consts_table fmt () =
285
  begin
286
    Format.fprintf fmt "{ /* consts table */@.";
287
    Hashtbl.iter (fun id const ->
288
      Format.fprintf fmt "%s |-> %a"
289
	id
290
	Printers.pp_const_decl (const_of_top const)
291
    ) consts_table;
292
    Format.fprintf fmt "}@."
293
  end
294

    
295
let node_name td =
296
    match td.top_decl_desc with 
297
    | Node nd         -> nd.node_id
298
    | ImportedNode nd -> nd.nodei_id
299
    | _ -> assert false
300

    
301
let is_generic_node td =
302
  match td.top_decl_desc with 
303
  | Node nd         -> List.exists (fun v -> v.var_dec_const) nd.node_inputs
304
  | ImportedNode nd -> List.exists (fun v -> v.var_dec_const) nd.nodei_inputs
305
  | _ -> assert false
306

    
307
let node_inputs td =
308
  match td.top_decl_desc with 
309
  | Node nd         -> nd.node_inputs
310
  | ImportedNode nd -> nd.nodei_inputs
311
  | _ -> assert false
312

    
313
let node_from_name id =
314
      Hashtbl.find node_table id
315
  (* with Not_found -> (Format.eprintf "Unable to find any node named %s@ @?" id;
316
   *       	     assert false) *)
317

    
318
let update_node id top =
319
  Hashtbl.replace node_table id top
320

    
321
let is_imported_node td =
322
  match td.top_decl_desc with 
323
  | Node nd         -> false
324
  | ImportedNode nd -> true
325
  | _ -> assert false
326

    
327

    
328
(* alias and type definition table *)
329

    
330
let mktop = mktop_decl Location.dummy_loc !Options.dest_dir false
331

    
332
let top_int_type = mktop (TypeDef {tydef_id = "int"; tydef_desc = Tydec_int})
333
let top_bool_type = mktop (TypeDef {tydef_id = "bool"; tydef_desc = Tydec_bool})
334
(* let top_float_type = mktop (TypeDef {tydef_id = "float"; tydef_desc = Tydec_float}) *)
335
let top_real_type = mktop (TypeDef {tydef_id = "real"; tydef_desc = Tydec_real})
336

    
337
let type_table =
338
  Utils.create_hashtable 20 [
339
    Tydec_int  , top_int_type;
340
    Tydec_bool , top_bool_type;
341
    (* Tydec_float, top_float_type; *)
342
    Tydec_real , top_real_type
343
  ]
344

    
345
let print_type_table fmt () =
346
  begin
347
    Format.fprintf fmt "{ /* type table */@.";
348
    Hashtbl.iter (fun tydec tdef ->
349
      Format.fprintf fmt "%a |-> %a"
350
	Printers.pp_var_type_dec_desc tydec
351
	Printers.pp_typedef (typedef_of_top tdef)
352
    ) type_table;
353
    Format.fprintf fmt "}@."
354
  end
355

    
356
let rec is_user_type typ =
357
  match typ with
358
  | Tydec_int | Tydec_bool | Tydec_real 
359
  (* | Tydec_float *) | Tydec_any | Tydec_const _ -> false
360
  | Tydec_clock typ' -> is_user_type typ'
361
  | _ -> true
362

    
363
let get_repr_type typ =
364
  let typ_def = (typedef_of_top (Hashtbl.find type_table typ)).tydef_desc in
365
  if is_user_type typ_def then typ else typ_def
366

    
367
let rec coretype_equal ty1 ty2 =
368
  let res =
369
  match ty1, ty2 with
370
  | Tydec_any           , _
371
  | _                   , Tydec_any             -> assert false
372
  | Tydec_const _       , Tydec_const _         -> get_repr_type ty1 = get_repr_type ty2
373
  | Tydec_const _       , _                     -> let ty1' = (typedef_of_top (Hashtbl.find type_table ty1)).tydef_desc
374
	       					   in (not (is_user_type ty1')) && coretype_equal ty1' ty2
375
  | _                   , Tydec_const _         -> coretype_equal ty2 ty1
376
  | Tydec_int           , Tydec_int
377
  | Tydec_real          , Tydec_real
378
  (* | Tydec_float         , Tydec_float *)
379
  | Tydec_bool          , Tydec_bool            -> true
380
  | Tydec_clock ty1     , Tydec_clock ty2       -> coretype_equal ty1 ty2
381
  | Tydec_array (d1,ty1), Tydec_array (d2, ty2) -> Dimension.is_eq_dimension d1 d2 && coretype_equal ty1 ty2
382
  | Tydec_enum tl1      , Tydec_enum tl2        -> List.sort compare tl1 = List.sort compare tl2
383
  | Tydec_struct fl1    , Tydec_struct fl2      ->
384
       List.length fl1 = List.length fl2
385
    && List.for_all2 (fun (f1, t1) (f2, t2) -> f1 = f2 && coretype_equal t1 t2)
386
      (List.sort (fun (f1,_) (f2,_) -> compare f1 f2) fl1)
387
      (List.sort (fun (f1,_) (f2,_) -> compare f1 f2) fl2)
388
  | _                                  -> false
389
  in ((*Format.eprintf "coretype_equal %a %a = %B@." Printers.pp_var_type_dec_desc ty1 Printers.pp_var_type_dec_desc ty2 res;*) res)
390

    
391
let tag_true = "true"
392
let tag_false = "false"
393
let tag_default = "default"
394

    
395
let const_is_bool c =
396
 match c with
397
 | Const_tag t -> t = tag_true || t = tag_false
398
 | _           -> false
399

    
400
(* Computes the negation of a boolean constant *)
401
let const_negation c =
402
  assert (const_is_bool c);
403
  match c with
404
  | Const_tag t when t = tag_true  -> Const_tag tag_false
405
  | _                              -> Const_tag tag_true
406

    
407
let const_or c1 c2 =
408
  assert (const_is_bool c1 && const_is_bool c2);
409
  match c1, c2 with
410
  | Const_tag t1, _            when t1 = tag_true -> c1
411
  | _           , Const_tag t2 when t2 = tag_true -> c2
412
  | _                                             -> Const_tag tag_false
413

    
414
let const_and c1 c2 =
415
  assert (const_is_bool c1 && const_is_bool c2);
416
  match c1, c2 with
417
  | Const_tag t1, _            when t1 = tag_false -> c1
418
  | _           , Const_tag t2 when t2 = tag_false -> c2
419
  | _                                              -> Const_tag tag_true
420

    
421
let const_xor c1 c2 =
422
  assert (const_is_bool c1 && const_is_bool c2);
423
   match c1, c2 with
424
  | Const_tag t1, Const_tag t2 when t1 <> t2  -> Const_tag tag_true
425
  | _                                         -> Const_tag tag_false
426

    
427
let const_impl c1 c2 =
428
  assert (const_is_bool c1 && const_is_bool c2);
429
  match c1, c2 with
430
  | Const_tag t1, _ when t1 = tag_false           -> Const_tag tag_true
431
  | _           , Const_tag t2 when t2 = tag_true -> Const_tag tag_true
432
  | _                                             -> Const_tag tag_false
433

    
434
(* To guarantee uniqueness of tags in enum types *)
435
let tag_table =
436
  Utils.create_hashtable 20 [
437
   tag_true, top_bool_type;
438
   tag_false, top_bool_type
439
  ]
440

    
441
(* To guarantee uniqueness of fields in struct types *)
442
let field_table =
443
  Utils.create_hashtable 20 [
444
  ]
445

    
446
let get_enum_type_tags cty =
447
(*Format.eprintf "get_enum_type_tags %a@." Printers.pp_var_type_dec_desc cty;*)
448
 match cty with
449
 | Tydec_bool    -> [tag_true; tag_false]
450
 | Tydec_const _ -> (match (typedef_of_top (Hashtbl.find type_table cty)).tydef_desc with
451
                     | Tydec_enum tl -> tl
452
                     | _             -> assert false)
453
 | _            -> assert false
454

    
455
let get_struct_type_fields cty =
456
 match cty with
457
 | Tydec_const _ -> (match (typedef_of_top (Hashtbl.find type_table cty)).tydef_desc with
458
                     | Tydec_struct fl -> fl
459
                     | _               -> assert false)
460
 | _            -> assert false
461

    
462
let const_of_bool b =
463
 Const_tag (if b then tag_true else tag_false)
464

    
465
(* let get_const c = snd (Hashtbl.find consts_table c) *)
466

    
467
let ident_of_expr expr =
468
 match expr.expr_desc with
469
 | Expr_ident id -> id
470
 | _             -> assert false
471

    
472
(* Generate a new ident expression from a declared variable *)
473
let expr_of_vdecl v =
474
  { expr_tag = Utils.new_tag ();
475
    expr_desc = Expr_ident v.var_id;
476
    expr_type = v.var_type;
477
    expr_clock = v.var_clock;
478
    expr_delay = Delay.new_var ();
479
    expr_annot = None;
480
    expr_loc = v.var_loc }
481

    
482
(* Caution, returns an untyped and unclocked expression *)
483
let expr_of_ident id loc =
484
  {expr_tag = Utils.new_tag ();
485
   expr_desc = Expr_ident id;
486
   expr_type = Types.new_var ();
487
   expr_clock = Clocks.new_var true;
488
   expr_delay = Delay.new_var ();
489
   expr_loc = loc;
490
   expr_annot = None}
491

    
492
let is_tuple_expr expr =
493
 match expr.expr_desc with
494
  | Expr_tuple _ -> true
495
  | _            -> false
496

    
497
let expr_list_of_expr expr =
498
  match expr.expr_desc with
499
  | Expr_tuple elist -> elist
500
  | _                -> [expr]
501

    
502
let expr_of_expr_list loc elist =
503
 match elist with
504
 | [t]  -> { t with expr_loc = loc }
505
 | t::_ ->
506
    let tlist = List.map (fun e -> e.expr_type) elist in
507
    let clist = List.map (fun e -> e.expr_clock) elist in
508
    { t with expr_desc = Expr_tuple elist;
509
	     expr_type = Type_predef.type_tuple tlist;
510
	     expr_clock = Clock_predef.ck_tuple clist;
511
	     expr_tag = Utils.new_tag ();
512
	     expr_loc = loc }
513
 | _    -> assert false
514

    
515
let call_of_expr expr =
516
 match expr.expr_desc with
517
 | Expr_appl (f, args, r) -> (f, expr_list_of_expr args, r)
518
 | _                      -> assert false
519

    
520
    
521
(* Conversion from dimension expr to standard expr, for the purpose of printing, typing, etc... *)
522
let rec expr_of_dimension dim =
523
  let open Dimension in
524
  match dim.dim_desc with
525
 | Dbool b        ->
526
     mkexpr dim.dim_loc (Expr_const (const_of_bool b))
527
 | Dint i         ->
528
     mkexpr dim.dim_loc (Expr_const (Const_int i))
529
 | Dident id      ->
530
     mkexpr dim.dim_loc (Expr_ident id)
531
 | Dite (c, t, e) ->
532
     mkexpr dim.dim_loc (Expr_ite (expr_of_dimension c, expr_of_dimension t, expr_of_dimension e))
533
 | Dappl (id, args) ->
534
     mkexpr dim.dim_loc (Expr_appl (id, expr_of_expr_list dim.dim_loc (List.map expr_of_dimension args), None))
535
 | Dlink dim'       -> expr_of_dimension dim'
536
 | Dvar
537
 | Dunivar          -> (Format.eprintf "internal error: Corelang.expr_of_dimension %a@." Dimension.pp_dimension dim;
538
			assert false)
539

    
540
let dimension_of_const loc const =
541
  let open Dimension in
542
 match const with
543
 | Const_int i                                    -> mkdim_int loc i
544
 | Const_tag t when t = tag_true || t = tag_false -> mkdim_bool loc (t = tag_true)
545
 | _                                              -> raise InvalidDimension
546

    
547
(* Conversion from standard expr to dimension expr, for the purpose of injecting static call arguments 
548
   into dimension expressions *)
549
let rec dimension_of_expr expr =
550
  let open Dimension in
551
  match expr.expr_desc with
552
  | Expr_const c  -> dimension_of_const expr.expr_loc c
553
  | Expr_ident id -> mkdim_ident expr.expr_loc id
554
  | Expr_appl (f, args, None) when Basic_library.is_expr_internal_fun expr ->
555
      let k = Types.get_static_value (Env.lookup_value Basic_library.type_env f) in
556
      if k = None then raise InvalidDimension;
557
      mkdim_appl expr.expr_loc f (List.map dimension_of_expr (expr_list_of_expr args))
558
  | Expr_ite (i, t, e)        ->
559
      mkdim_ite expr.expr_loc (dimension_of_expr i) (dimension_of_expr t) (dimension_of_expr e)
560
  | _ -> raise InvalidDimension (* not a simple dimension expression *)
561

    
562

    
563
let sort_handlers hl =
564
 List.sort (fun (t, _) (t', _) -> compare t t') hl
565

    
566
let num_10 = Num.num_of_int 10
567
  
568
let rec is_eq_const c1 c2 =
569
  match c1, c2 with
570
  | Const_real (n1, i1, _), Const_real (n2, i2, _)
571
    -> Num.(let n1 = n1 // (num_10 **/ (num_of_int i1)) in
572
	    let n2 = n2 // (num_10 **/ (num_of_int i2)) in
573
	    eq_num n1 n2)
574
  | Const_struct lcl1, Const_struct lcl2
575
    -> List.length lcl1 = List.length lcl2
576
    && List.for_all2 (fun (l1, c1) (l2, c2) -> l1 = l2 && is_eq_const c1 c2) lcl1 lcl2
577
  | _  -> c1 = c2
578

    
579
let rec is_eq_expr e1 e2 = match e1.expr_desc, e2.expr_desc with
580
  | Expr_const c1, Expr_const c2 -> is_eq_const c1 c2
581
  | Expr_ident i1, Expr_ident i2 -> i1 = i2
582
  | Expr_array el1, Expr_array el2 
583
  | Expr_tuple el1, Expr_tuple el2 -> 
584
    List.length el1 = List.length el2 && List.for_all2 is_eq_expr el1 el2 
585
  | Expr_arrow (e1, e2), Expr_arrow (e1', e2') -> is_eq_expr e1 e1' && is_eq_expr e2 e2'
586
  | Expr_fby (e1,e2), Expr_fby (e1',e2') -> is_eq_expr e1 e1' && is_eq_expr e2 e2'
587
  | 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
588
  (* | Expr_concat (e1,e2), Expr_concat (e1',e2') -> is_eq_expr e1 e1' && is_eq_expr e2 e2' *)
589
  (* | Expr_tail e, Expr_tail e' -> is_eq_expr e e' *)
590
  | Expr_pre e, Expr_pre e' -> is_eq_expr e e'
591
  | Expr_when (e, i, l), Expr_when (e', i', l') -> l=l' && i=i' && is_eq_expr e e'
592
  | 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')
593
  | Expr_appl (i, e, r), Expr_appl (i', e', r') -> i=i' && r=r' && is_eq_expr e e'
594
  | Expr_power (e1, i1), Expr_power (e2, i2)
595
  | Expr_access (e1, i1), Expr_access (e2, i2) -> is_eq_expr e1 e2 && is_eq_expr (expr_of_dimension i1) (expr_of_dimension i2)
596
  | _ -> false
597

    
598
let get_node_vars nd =
599
  nd.node_inputs @ nd.node_locals @ nd.node_outputs
600

    
601
let mk_new_node_name nd id =
602
  let used_vars = get_node_vars nd in
603
  let used v = List.exists (fun vdecl -> vdecl.var_id = v) used_vars in
604
  mk_new_name used id
605

    
606
let get_var id var_list =
607
  List.find (fun v -> v.var_id = id) var_list
608

    
609
let get_node_var id node =
610
  try
611
    get_var id (get_node_vars node)
612
  with Not_found -> begin
613
    (* Format.eprintf "Unable to find variable %s in node %s@.@?" id node.node_id; *)
614
    raise Not_found
615
  end
616

    
617

    
618
let get_node_eqs =
619
  let get_eqs stmts =
620
    List.fold_right
621
      (fun stmt (res_eq, res_aut) ->
622
	match stmt with
623
	| Eq eq -> eq :: res_eq, res_aut
624
	| Aut aut -> res_eq, aut::res_aut)
625
      stmts
626
      ([], []) in
627
  let table_eqs = Hashtbl.create 23 in
628
  (fun nd ->
629
    try
630
      let (old, res) = Hashtbl.find table_eqs nd.node_id
631
      in if old == nd.node_stmts then res else raise Not_found
632
    with Not_found -> 
633
      let res = get_eqs nd.node_stmts in
634
      begin
635
	Hashtbl.replace table_eqs nd.node_id (nd.node_stmts, res);
636
	res
637
      end)
638

    
639
let get_node_eq id node =
640
  let eqs, auts = get_node_eqs node in
641
  try
642
    List.find (fun eq -> List.mem id eq.eq_lhs) eqs
643
  with
644
    Not_found -> (* Shall be defined in automata auts *) raise Not_found
645
      
646
let get_nodes prog = 
647
  List.fold_left (
648
    fun nodes decl ->
649
      match decl.top_decl_desc with
650
	| Node _ -> decl::nodes
651
	| Const _ | ImportedNode _ | Include _ | Open _ | TypeDef _ -> nodes  
652
  ) [] prog
653

    
654
let get_imported_nodes prog = 
655
  List.fold_left (
656
    fun nodes decl ->
657
      match decl.top_decl_desc with
658
	| ImportedNode _ -> decl::nodes
659
	| Const _ | Node _ | Include _ | Open _ | TypeDef _-> nodes  
660
  ) [] prog
661

    
662
let get_consts prog = 
663
  List.fold_right (
664
    fun decl consts ->
665
      match decl.top_decl_desc with
666
	| Const _ -> decl::consts
667
	| Node _ | ImportedNode _ | Include _ | Open _ | TypeDef _ -> consts  
668
  ) prog []
669

    
670
let get_typedefs prog = 
671
  List.fold_right (
672
    fun decl types ->
673
      match decl.top_decl_desc with
674
	| TypeDef _ -> decl::types
675
	| Node _ | ImportedNode _ | Include _ | Open _ | Const _ -> types  
676
  ) prog []
677

    
678
let get_dependencies prog =
679
  List.fold_right (
680
    fun decl deps ->
681
      match decl.top_decl_desc with
682
	| Open _ -> decl::deps
683
	| Node _ | ImportedNode _ | TypeDef _ | Include _ | Const _ -> deps  
684
  ) prog []
685

    
686
let get_node_interface nd =
687
 {nodei_id = nd.node_id;
688
  nodei_type = nd.node_type;
689
  nodei_clock = nd.node_clock;
690
  nodei_inputs = nd.node_inputs;
691
  nodei_outputs = nd.node_outputs;
692
  nodei_stateless = nd.node_dec_stateless;
693
  nodei_spec = nd.node_spec;
694
  (* nodei_annot = nd.node_annot; *)
695
  nodei_prototype = None;
696
  nodei_in_lib = [];
697
 }
698

    
699
(************************************************************************)
700
(*        Renaming                                                      *)
701

    
702
let rec rename_static rename cty =
703
 match cty with
704
 | Tydec_array (d, cty') -> Tydec_array (Dimension.expr_replace_expr rename d, rename_static rename cty')
705
 | Tydec_clock cty       -> Tydec_clock (rename_static rename cty)
706
 | Tydec_struct fl       -> Tydec_struct (List.map (fun (f, cty) -> f, rename_static rename cty) fl)
707
 | _                      -> cty
708

    
709
let rec rename_carrier rename cck =
710
 match cck with
711
 | Ckdec_bool cl -> Ckdec_bool (List.map (fun (c, l) -> rename c, l) cl)
712
 | _             -> cck
713

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

    
716
(* applies the renaming function [fvar] to all variables of expression [expr] *)
717
 (* let rec expr_replace_var fvar expr = *)
718
 (*  { expr with expr_desc = expr_desc_replace_var fvar expr.expr_desc } *)
719

    
720
 (* and expr_desc_replace_var fvar expr_desc = *)
721
 (*   match expr_desc with *)
722
 (*   | Expr_const _ -> expr_desc *)
723
 (*   | Expr_ident i -> Expr_ident (fvar i) *)
724
 (*   | Expr_array el -> Expr_array (List.map (expr_replace_var fvar) el) *)
725
 (*   | Expr_access (e1, d) -> Expr_access (expr_replace_var fvar e1, d) *)
726
 (*   | Expr_power (e1, d) -> Expr_power (expr_replace_var fvar e1, d) *)
727
 (*   | Expr_tuple el -> Expr_tuple (List.map (expr_replace_var fvar) el) *)
728
 (*   | Expr_ite (c, t, e) -> Expr_ite (expr_replace_var fvar c, expr_replace_var fvar t, expr_replace_var fvar e) *)
729
 (*   | Expr_arrow (e1, e2)-> Expr_arrow (expr_replace_var fvar e1, expr_replace_var fvar e2)  *)
730
 (*   | Expr_fby (e1, e2) -> Expr_fby (expr_replace_var fvar e1, expr_replace_var fvar e2) *)
731
 (*   | Expr_pre e' -> Expr_pre (expr_replace_var fvar e') *)
732
 (*   | Expr_when (e', i, l)-> Expr_when (expr_replace_var fvar e', fvar i, l) *)
733
 (*   | Expr_merge (i, hl) -> Expr_merge (fvar i, List.map (fun (t, h) -> (t, expr_replace_var fvar h)) hl) *)
734
 (*   | Expr_appl (i, e', i') -> Expr_appl (i, expr_replace_var fvar e', Utils.option_map (expr_replace_var fvar) i') *)
735

    
736

    
737

    
738
 let rec rename_expr  f_node f_var expr =
739
   { expr with expr_desc = rename_expr_desc f_node f_var expr.expr_desc }
740
 and rename_expr_desc f_node f_var expr_desc =
741
   let re = rename_expr  f_node f_var in
742
   match expr_desc with
743
   | Expr_const _ -> expr_desc
744
   | Expr_ident i -> Expr_ident (f_var i)
745
   | Expr_array el -> Expr_array (List.map re el)
746
   | Expr_access (e1, d) -> Expr_access (re e1, d)
747
   | Expr_power (e1, d) -> Expr_power (re e1, d)
748
   | Expr_tuple el -> Expr_tuple (List.map re el)
749
   | Expr_ite (c, t, e) -> Expr_ite (re c, re t, re e)
750
   | Expr_arrow (e1, e2)-> Expr_arrow (re e1, re e2) 
751
   | Expr_fby (e1, e2) -> Expr_fby (re e1, re e2)
752
   | Expr_pre e' -> Expr_pre (re e')
753
   | Expr_when (e', i, l)-> Expr_when (re e', f_var i, l)
754
   | Expr_merge (i, hl) -> 
755
     Expr_merge (f_var i, List.map (fun (t, h) -> (t, re h)) hl)
756
   | Expr_appl (i, e', i') -> 
757
     Expr_appl (f_node i, re e', Utils.option_map re i')
758

    
759
 let rename_dec_type f_node f_var t = assert false (*
760
						     Types.rename_dim_type (Dimension.rename f_node f_var) t*)
761

    
762
 let rename_dec_clock f_node f_var c = assert false (* 
763
					  Clocks.rename_clock_expr f_var c*)
764
   
765
 let rename_var f_node f_var v = {
766
   v with
767
     var_id = f_var v.var_id;
768
     var_dec_type = rename_dec_type f_node f_var v.var_type;
769
     var_dec_clock = rename_dec_clock f_node f_var v.var_clock
770
 } 
771

    
772
 let rename_vars f_node f_var = List.map (rename_var f_node f_var) 
773

    
774
 let rec rename_eq f_node f_var eq = { eq with
775
   eq_lhs = List.map f_var eq.eq_lhs; 
776
   eq_rhs = rename_expr f_node f_var eq.eq_rhs
777
 } 
778
 and rename_handler f_node f_var  h = {h with
779
   hand_state = f_var h.hand_state;
780
   hand_unless = List.map (
781
     fun (l,e,b,id) -> l, rename_expr f_node f_var e, b, f_var id
782
   ) h.hand_unless;
783
   hand_until = List.map (
784
     fun (l,e,b,id) -> l, rename_expr f_node f_var e, b, f_var id
785
   ) h.hand_until;
786
   hand_locals = rename_vars f_node f_var h.hand_locals;
787
   hand_stmts = rename_stmts f_node f_var h.hand_stmts;
788
   hand_annots = rename_annots f_node f_var h.hand_annots;
789
   
790
 } 
791
 and rename_aut f_node f_var  aut = { aut with
792
   aut_id = f_var aut.aut_id;
793
   aut_handlers = List.map (rename_handler f_node f_var) aut.aut_handlers;
794
 }
795
 and rename_stmts f_node f_var stmts = List.map (fun stmt -> match stmt with
796
   | Eq eq -> Eq (rename_eq f_node f_var eq)
797
   | Aut at -> Aut (rename_aut f_node f_var at))
798
   stmts
799
 and rename_annotl f_node f_var  annots = 
800
   List.map 
801
     (fun (key, value) -> key, rename_eexpr f_node f_var value) 
802
     annots
803
 and rename_annot f_node f_var annot =
804
   { annot with annots = rename_annotl f_node f_var annot.annots }
805
 and rename_annots f_node f_var annots =
806
   List.map (rename_annot f_node f_var) annots
807
and rename_eexpr f_node f_var ee =
808
   { ee with
809
     eexpr_tag = Utils.new_tag ();
810
     eexpr_qfexpr = rename_expr f_node f_var ee.eexpr_qfexpr;
811
     eexpr_quantifiers = List.map (fun (typ,vdecls) -> typ, rename_vars f_node f_var vdecls) ee.eexpr_quantifiers;
812
   }
813
 
814
     
815
     
816
   
817
 let rename_node f_node f_var nd =
818
   let rename_var = rename_var f_node f_var in
819
   let rename_expr = rename_expr f_node f_var in
820
   let rename_stmts = rename_stmts f_node f_var in
821
   let inputs = List.map rename_var nd.node_inputs in
822
   let outputs = List.map rename_var nd.node_outputs in
823
   let locals = List.map rename_var nd.node_locals in
824
   let gen_calls = List.map rename_expr nd.node_gencalls in
825
   let node_checks = List.map (Dimension.rename f_node f_var)  nd.node_checks in
826
   let node_asserts = List.map 
827
     (fun a -> 
828
       {a with assert_expr = 
829
	   let expr = a.assert_expr in
830
	   rename_expr expr})
831
     nd.node_asserts
832
   in
833
   let node_stmts = rename_stmts nd.node_stmts
834

    
835
     
836
   in
837
   let spec = 
838
     Utils.option_map 
839
       (fun s -> assert false; (*rename_node_annot f_node f_var s*) ) (* TODO: implement! *) 
840
       nd.node_spec 
841
   in
842
   let annot = rename_annots f_node f_var nd.node_annot in
843
   {
844
     node_id = f_node nd.node_id;
845
     node_type = nd.node_type;
846
     node_clock = nd.node_clock;
847
     node_inputs = inputs;
848
     node_outputs = outputs;
849
     node_locals = locals;
850
     node_gencalls = gen_calls;
851
     node_checks = node_checks;
852
     node_asserts = node_asserts;
853
     node_stmts = node_stmts;
854
     node_dec_stateless = nd.node_dec_stateless;
855
     node_stateless = nd.node_stateless;
856
     node_spec = spec;
857
     node_annot = annot;
858
   }
859

    
860

    
861
let rename_const f_const c =
862
  { c with const_id = f_const c.const_id }
863

    
864
let rename_typedef f_var t =
865
  match t.tydef_desc with
866
  | Tydec_enum tags -> { t with tydef_desc = Tydec_enum (List.map f_var tags) }
867
  | _               -> t
868

    
869
let rename_prog f_node f_var f_const prog =
870
  List.rev (
871
    List.fold_left (fun accu top ->
872
      (match top.top_decl_desc with
873
      | Node nd -> 
874
	 { top with top_decl_desc = Node (rename_node f_node f_var nd) }
875
      | Const c -> 
876
	 { top with top_decl_desc = Const (rename_const f_const c) }
877
      | TypeDef tdef ->
878
	 { top with top_decl_desc = TypeDef (rename_typedef f_var tdef) }
879
      | ImportedNode _
880
        | Include _ | Open _       -> top)
881
      ::accu
882
) [] prog
883
		   )
884

    
885
(* Applies the renaming function [fvar] to every rhs
886
   only when the corresponding lhs satisfies predicate [pvar] *)
887
 let eq_replace_rhs_var pvar fvar eq =
888
   let pvar l = List.exists pvar l in
889
   let rec replace lhs rhs =
890
     { rhs with expr_desc =
891
     match lhs with
892
     | []  -> assert false
893
     | [_] -> if pvar lhs then rename_expr_desc (fun x -> x) fvar rhs.expr_desc else rhs.expr_desc
894
     | _   ->
895
       (match rhs.expr_desc with
896
       | Expr_tuple tl ->
897
	 Expr_tuple (List.map2 (fun v e -> replace [v] e) lhs tl)
898
       | Expr_appl (f, arg, None) when Basic_library.is_expr_internal_fun rhs ->
899
	 let args = expr_list_of_expr arg in
900
	 Expr_appl (f, expr_of_expr_list arg.expr_loc (List.map (replace lhs) args), None)
901
       | Expr_array _
902
       | Expr_access _
903
       | Expr_power _
904
       | Expr_const _
905
       | Expr_ident _
906
       | Expr_appl _   ->
907
	 if pvar lhs
908
	 then rename_expr_desc (fun x -> x) fvar rhs.expr_desc
909
	 else rhs.expr_desc
910
       | Expr_ite (c, t, e)   -> Expr_ite (replace lhs c, replace lhs t, replace lhs e)
911
       | Expr_arrow (e1, e2)  -> Expr_arrow (replace lhs e1, replace lhs e2) 
912
       | Expr_fby (e1, e2)    -> Expr_fby (replace lhs e1, replace lhs e2)
913
       | Expr_pre e'          -> Expr_pre (replace lhs e')
914
       | Expr_when (e', i, l) -> let i' = if pvar lhs then fvar i else i
915
				 in Expr_when (replace lhs e', i', l)
916
       | Expr_merge (i, hl)   -> let i' = if pvar lhs then fvar i else i
917
				 in Expr_merge (i', List.map (fun (t, h) -> (t, replace lhs h)) hl)
918
       )
919
     }
920
   in { eq with eq_rhs = replace eq.eq_lhs eq.eq_rhs }
921

    
922
    
923
(**********************************************************************)
924
(* Pretty printers *)
925

    
926
let pp_decl_type fmt tdecl =
927
  match tdecl.top_decl_desc with
928
  | Node nd ->
929
    fprintf fmt "%s: " nd.node_id;
930
    Utils.reset_names ();
931
    fprintf fmt "%a@ " Types.print_ty nd.node_type
932
  | ImportedNode ind ->
933
    fprintf fmt "%s: " ind.nodei_id;
934
    Utils.reset_names ();
935
    fprintf fmt "%a@ " Types.print_ty ind.nodei_type
936
  | Const _ | Include _ | Open _ | TypeDef _ -> ()
937

    
938
let pp_prog_type fmt tdecl_list =
939
  Utils.fprintf_list ~sep:"" pp_decl_type fmt tdecl_list
940

    
941
let pp_decl_clock fmt cdecl =
942
  match cdecl.top_decl_desc with
943
  | Node nd ->
944
    fprintf fmt "%s: " nd.node_id;
945
    Utils.reset_names ();
946
    fprintf fmt "%a@ " Clocks.print_ck nd.node_clock
947
  | ImportedNode ind ->
948
    fprintf fmt "%s: " ind.nodei_id;
949
    Utils.reset_names ();
950
    fprintf fmt "%a@ " Clocks.print_ck ind.nodei_clock
951
  | Const _ | Include _ | Open _ | TypeDef _ -> ()
952

    
953
let pp_prog_clock fmt prog =
954
  Utils.fprintf_list ~sep:"" pp_decl_clock fmt prog
955

    
956

    
957
(* filling node table with internal functions *)
958
let vdecls_of_typ_ck cpt ty =
959
  let loc = Location.dummy_loc in
960
  List.map
961
    (fun _ -> incr cpt;
962
              let name = sprintf "_var_%d" !cpt in
963
              mkvar_decl loc (name, mktyp loc Tydec_any, mkclock loc Ckdec_any, false, None, None))
964
    (Types.type_list_of_type ty)
965

    
966
let mk_internal_node id =
967
  let spec = None in
968
  let ty = Env.lookup_value Basic_library.type_env id in
969
  let ck = Env.lookup_value Basic_library.clock_env id in
970
  let (tin, tout) = Types.split_arrow ty in
971
  (*eprintf "internal fun %s: %d -> %d@." id (List.length (Types.type_list_of_type tin)) (List.length (Types.type_list_of_type tout));*)
972
  let cpt = ref (-1) in
973
  mktop
974
    (ImportedNode
975
       {nodei_id = id;
976
	nodei_type = ty;
977
	nodei_clock = ck;
978
	nodei_inputs = vdecls_of_typ_ck cpt tin;
979
	nodei_outputs = vdecls_of_typ_ck cpt tout;
980
	nodei_stateless = Types.get_static_value ty <> None;
981
	nodei_spec = spec;
982
	(* nodei_annot = []; *)
983
	nodei_prototype = None;
984
       	nodei_in_lib = [];
985
       })
986

    
987
let add_internal_funs () =
988
  List.iter
989
    (fun id -> let nd = mk_internal_node id in Hashtbl.add node_table id nd)
990
    Basic_library.internal_funs
991

    
992

    
993

    
994
(* Replace any occurence of a var in vars_to_replace by its associated
995
   expression in defs until e does not contain any such variables *)
996
let rec substitute_expr vars_to_replace defs e =
997
  let se = substitute_expr vars_to_replace defs in
998
  { e with expr_desc = 
999
      let ed = e.expr_desc in
1000
      match ed with
1001
      | Expr_const _ -> ed
1002
      | Expr_array el -> Expr_array (List.map se el)
1003
      | Expr_access (e1, d) -> Expr_access (se e1, d)
1004
      | Expr_power (e1, d) -> Expr_power (se e1, d)
1005
      | Expr_tuple el -> Expr_tuple (List.map se el)
1006
      | Expr_ite (c, t, e) -> Expr_ite (se c, se t, se e)
1007
      | Expr_arrow (e1, e2)-> Expr_arrow (se e1, se e2) 
1008
      | Expr_fby (e1, e2) -> Expr_fby (se e1, se e2)
1009
      | Expr_pre e' -> Expr_pre (se e')
1010
      | Expr_when (e', i, l)-> Expr_when (se e', i, l)
1011
      | Expr_merge (i, hl) -> Expr_merge (i, List.map (fun (t, h) -> (t, se h)) hl)
1012
      | Expr_appl (i, e', i') -> Expr_appl (i, se e', i')
1013
      | Expr_ident i -> 
1014
	if List.exists (fun v -> v.var_id = i) vars_to_replace then (
1015
	  let eq_i eq = eq.eq_lhs = [i] in
1016
	  if List.exists eq_i defs then
1017
	    let sub = List.find eq_i defs in
1018
	    let sub' = se sub.eq_rhs in
1019
	    sub'.expr_desc
1020
	  else 
1021
	    assert false
1022
	)
1023
	else
1024
	  ed
1025

    
1026
  }
1027
  
1028
 let rec expr_to_eexpr  expr =
1029
   { eexpr_tag = expr.expr_tag;
1030
     eexpr_qfexpr = expr;
1031
     eexpr_quantifiers = [];
1032
     eexpr_type = expr.expr_type;
1033
     eexpr_clock = expr.expr_clock;
1034
     eexpr_loc = expr.expr_loc;
1035
     (*eexpr_normalized = None*)
1036
   }
1037
 (* and expr_desc_to_eexpr_desc expr_desc = *)
1038
 (*   let conv = expr_to_eexpr in *)
1039
 (*   match expr_desc with *)
1040
 (*   | Expr_const c -> EExpr_const (match c with *)
1041
 (*     | Const_int x -> EConst_int x  *)
1042
 (*     | Const_real x -> EConst_real x  *)
1043
 (*     | Const_float x -> EConst_float x  *)
1044
 (*     | Const_tag x -> EConst_tag x  *)
1045
 (*     | _ -> assert false *)
1046

    
1047
 (*   ) *)
1048
 (*   | Expr_ident i -> EExpr_ident i *)
1049
 (*   | Expr_tuple el -> EExpr_tuple (List.map conv el) *)
1050

    
1051
 (*   | Expr_arrow (e1, e2)-> EExpr_arrow (conv e1, conv e2)  *)
1052
 (*   | Expr_fby (e1, e2) -> EExpr_fby (conv e1, conv e2) *)
1053
 (*   | Expr_pre e' -> EExpr_pre (conv e') *)
1054
 (*   | Expr_appl (i, e', i') ->  *)
1055
 (*     EExpr_appl  *)
1056
 (*       (i, conv e', match i' with None -> None | Some(id, _) -> Some id) *)
1057

    
1058
 (*   | Expr_when _ *)
1059
 (*   | Expr_merge _ -> assert false *)
1060
 (*   | Expr_array _  *)
1061
 (*   | Expr_access _  *)
1062
 (*   | Expr_power _  -> assert false *)
1063
 (*   | Expr_ite (c, t, e) -> assert false  *)
1064
 (*   | _ -> assert false *)
1065
      
1066
     
1067
let rec get_expr_calls nodes e =
1068
  let get_calls = get_expr_calls nodes in
1069
  match e.expr_desc with
1070
  | Expr_const _ 
1071
   | Expr_ident _ -> Utils.ISet.empty
1072
   | Expr_tuple el
1073
   | Expr_array el -> List.fold_left (fun accu e -> Utils.ISet.union accu (get_calls e)) Utils.ISet.empty el
1074
   | Expr_pre e1 
1075
   | Expr_when (e1, _, _) 
1076
   | Expr_access (e1, _) 
1077
   | Expr_power (e1, _) -> get_calls e1
1078
   | Expr_ite (c, t, e) -> Utils.ISet.union (Utils.ISet.union (get_calls c) (get_calls t)) (get_calls e) 
1079
   | Expr_arrow (e1, e2) 
1080
   | Expr_fby (e1, e2) -> Utils.ISet.union (get_calls e1) (get_calls e2)
1081
   | Expr_merge (_, hl) -> List.fold_left (fun accu (_, h) -> Utils.ISet.union accu (get_calls h)) Utils.ISet.empty  hl
1082
   | Expr_appl (i, e', i') -> 
1083
     if Basic_library.is_expr_internal_fun e then 
1084
       (get_calls e') 
1085
     else
1086
       let calls =  Utils.ISet.add i (get_calls e') in
1087
       let test = (fun n -> match n.top_decl_desc with Node nd -> nd.node_id = i | _ -> false) in
1088
       if List.exists test nodes then
1089
	 match (List.find test nodes).top_decl_desc with
1090
	 | Node nd -> Utils.ISet.union (get_node_calls nodes nd) calls
1091
	 | _ -> assert false
1092
       else 
1093
	 calls
1094

    
1095
and get_eq_calls nodes eq =
1096
  get_expr_calls nodes eq.eq_rhs
1097
and get_aut_handler_calls nodes h =
1098
  List.fold_left (fun accu stmt -> match stmt with
1099
  | Eq eq -> Utils.ISet.union (get_eq_calls nodes eq) accu
1100
  | Aut aut' ->  Utils.ISet.union (get_aut_calls nodes aut') accu
1101
  ) Utils.ISet.empty h.hand_stmts 
1102
and get_aut_calls nodes aut =
1103
  List.fold_left (fun accu h -> Utils.ISet.union (get_aut_handler_calls nodes h) accu)
1104
    Utils.ISet.empty aut.aut_handlers
1105
and get_node_calls nodes node =
1106
  let eqs, auts = get_node_eqs node in
1107
  let aut_calls =
1108
    List.fold_left
1109
      (fun accu aut -> Utils.ISet.union (get_aut_calls nodes aut) accu)
1110
      Utils.ISet.empty auts
1111
  in
1112
  List.fold_left
1113
    (fun accu eq -> Utils.ISet.union (get_eq_calls nodes eq) accu)
1114
    aut_calls eqs
1115

    
1116
let get_expr_vars e =
1117
  let rec get_expr_vars vars e =
1118
    get_expr_desc_vars vars e.expr_desc
1119
  and get_expr_desc_vars vars expr_desc =
1120
    (*Format.eprintf "get_expr_desc_vars expr=%a@." Printers.pp_expr (mkexpr Location.dummy_loc expr_desc);*)
1121
  match expr_desc with
1122
  | Expr_const _ -> vars
1123
  | Expr_ident x -> Utils.ISet.add x vars
1124
  | Expr_tuple el
1125
  | Expr_array el -> List.fold_left get_expr_vars vars el
1126
  | Expr_pre e1 -> get_expr_vars vars e1
1127
  | Expr_when (e1, c, _) -> get_expr_vars (Utils.ISet.add c vars) e1 
1128
  | Expr_access (e1, d) 
1129
  | Expr_power (e1, d)   -> List.fold_left get_expr_vars vars [e1; expr_of_dimension d]
1130
  | Expr_ite (c, t, e) -> List.fold_left get_expr_vars vars [c; t; e]
1131
  | Expr_arrow (e1, e2) 
1132
  | Expr_fby (e1, e2) -> List.fold_left get_expr_vars vars [e1; e2]
1133
  | Expr_merge (c, hl) -> List.fold_left (fun vars (_, h) -> get_expr_vars vars h) (Utils.ISet.add c vars) hl
1134
  | Expr_appl (_, arg, None)   -> get_expr_vars vars arg
1135
  | Expr_appl (_, arg, Some r) -> List.fold_left get_expr_vars vars [arg; r]
1136
  in
1137
  get_expr_vars Utils.ISet.empty e 
1138

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

    
1157
and eq_has_arrows eq =
1158
  expr_has_arrows eq.eq_rhs
1159
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 
1160
and node_has_arrows node =
1161
  let eqs, auts = get_node_eqs node in
1162
  List.exists (fun eq -> eq_has_arrows eq) eqs || List.exists (fun aut -> aut_has_arrows aut) auts
1163

    
1164

    
1165

    
1166
let copy_var_decl vdecl =
1167
  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)
1168

    
1169
let copy_const cdecl =
1170
  { cdecl with const_type = Types.new_var () }
1171

    
1172
let copy_node nd =
1173
  { nd with
1174
    node_type     = Types.new_var ();
1175
    node_clock    = Clocks.new_var true;
1176
    node_inputs   = List.map copy_var_decl nd.node_inputs;
1177
    node_outputs  = List.map copy_var_decl nd.node_outputs;
1178
    node_locals   = List.map copy_var_decl nd.node_locals;
1179
    node_gencalls = [];
1180
    node_checks   = [];
1181
    node_stateless = None;
1182
  }
1183

    
1184
let copy_top top =
1185
  match top.top_decl_desc with
1186
  | Node nd -> { top with top_decl_desc = Node (copy_node nd)  }
1187
  | Const c -> { top with top_decl_desc = Const (copy_const c) }
1188
  | _       -> top
1189

    
1190
let copy_prog top_list =
1191
  List.map copy_top top_list
1192

    
1193

    
1194
let rec expr_contains_expr expr_tag expr  =
1195
  let search = expr_contains_expr expr_tag in
1196
  expr.expr_tag = expr_tag ||
1197
      (
1198
	match expr.expr_desc with
1199
	| Expr_const _ -> false
1200
	| Expr_array el -> List.exists search el
1201
	| Expr_access (e1, _) 
1202
	| Expr_power (e1, _) -> search e1
1203
	| Expr_tuple el -> List.exists search el
1204
	| Expr_ite (c, t, e) -> List.exists search [c;t;e]
1205
	| Expr_arrow (e1, e2)
1206
	| Expr_fby (e1, e2) -> List.exists search [e1; e2]
1207
	| Expr_pre e' 
1208
	| Expr_when (e', _, _) -> search e'
1209
	| Expr_merge (_, hl) -> List.exists (fun (_, h) -> search h) hl
1210
	| Expr_appl (_, e', None) -> search e' 
1211
	| Expr_appl (_, e', Some e'') -> List.exists search [e'; e''] 
1212
	| Expr_ident _ -> false
1213
      )
1214

    
1215

    
1216

    
1217
(* Generate a new local [node] variable *)
1218
let cpt_fresh = ref 0
1219

    
1220
let reset_cpt_fresh () =
1221
    cpt_fresh := 0
1222
    
1223
let mk_fresh_var (parentid, ctx_env) loc ty ck =
1224
  let rec aux () =
1225
  incr cpt_fresh;
1226
  let s = Printf.sprintf "__%s_%d" parentid !cpt_fresh in
1227
  if List.exists (fun v -> v.var_id = s) ctx_env then aux () else
1228
  {
1229
    var_id = s;
1230
    var_orig = false;
1231
    var_dec_type = dummy_type_dec;
1232
    var_dec_clock = dummy_clock_dec;
1233
    var_dec_const = false;
1234
    var_dec_value = None;
1235
    var_parent_nodeid = Some parentid;
1236
    var_type = ty;
1237
    var_clock = ck;
1238
    var_loc = loc
1239
  }
1240
  in aux ()
1241

    
1242

    
1243
let find_eq xl eqs =
1244
  let rec aux accu eqs =
1245
    match eqs with
1246
	| [] ->
1247
	  begin
1248
	    Format.eprintf "Looking for variables %a in the following equations@.%a@."
1249
	      (Utils.fprintf_list ~sep:" , " (fun fmt v -> Format.fprintf fmt "%s" v)) xl
1250
	      Printers.pp_node_eqs eqs;
1251
	    assert false
1252
	  end
1253
	| hd::tl ->
1254
	  if List.exists (fun x -> List.mem x hd.eq_lhs) xl then hd, accu@tl else aux (hd::accu) tl
1255
    in
1256
    aux [] eqs
1257

    
1258
(* Local Variables: *)
1259
(* compile-command:"make -C .." *)
1260
(* End: *)
(16-16/66)