Project

General

Profile

Statistics
| Branch: | Tag: | Revision:

lustrec / src / corelang.ml @ 70be4acf

History | View | Annotate | Download (41.8 KB)

1
(********************************************************************)
2
(*                                                                  *)
3
(*  The LustreC compiler toolset   /  The LustreC Development Team  *)
4
(*  Copyright 2012 -    --   ONERA - CNRS - INPT                    *)
5
(*                                                                  *)
6
(*  LustreC is free software, distributed WITHOUT ANY WARRANTY      *)
7
(*  under the terms of the GNU Lesser General Public License        *)
8
(*  version 2.1.                                                    *)
9
(*                                                                  *)
10
(********************************************************************)
11

    
12
open Format
13
open 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
end with type elt = var_decl =
32
  struct
33
    include Set.Make(VDeclModule)
34
    let pp fmt s =
35
      Format.fprintf fmt "{@[%a}@]" (Utils.fprintf_list ~sep:",@ " Printers.pp_var) (elements s)  
36
  end
37
let dummy_type_dec = {ty_dec_desc=Tydec_any; ty_dec_loc=Location.dummy_loc}
38

    
39
let dummy_clock_dec = {ck_dec_desc=Ckdec_any; ck_dec_loc=Location.dummy_loc}
40

    
41

    
42

    
43
(************************************************************)
44
(* *)
45

    
46
let mktyp loc d =
47
  { ty_dec_desc = d; ty_dec_loc = loc }
48

    
49
let mkclock loc d =
50
  { ck_dec_desc = d; ck_dec_loc = loc }
51

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

    
65
let dummy_var_decl name typ =
66
  {
67
    var_id = name;
68
    var_orig = false;
69
    var_dec_type = dummy_type_dec;
70
    var_dec_clock = dummy_clock_dec;
71
    var_dec_const = false;
72
    var_dec_value = None;
73
    var_parent_nodeid = None;
74
    var_type =  typ;
75
    var_clock = Clocks.new_ck Clocks.Cvar true;
76
    var_loc = Location.dummy_loc
77
  }
78

    
79
let mkexpr loc d =
80
  { expr_tag = Utils.new_tag ();
81
    expr_desc = d;
82
    expr_type = Types.new_var ();
83
    expr_clock = Clocks.new_var true;
84
    expr_delay = Delay.new_var ();
85
    expr_annot = None;
86
    expr_loc = loc }
87

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

    
100
let mk_new_name used id =
101
  let rec new_name name cpt =
102
    if used name
103
    then new_name (sprintf "_%s_%i" id cpt) (cpt+1)
104
    else name
105
  in new_name id 1
106

    
107
let mkeq loc (lhs, rhs) =
108
  { eq_lhs = lhs;
109
    eq_rhs = rhs;
110
    eq_loc = loc }
111

    
112
let mkassert loc expr =
113
  { assert_loc = loc;
114
    assert_expr = expr
115
  }
116

    
117
let mktop_decl loc own itf d =
118
  { top_decl_desc = d; top_decl_loc = loc; top_decl_owner = own; top_decl_itf = itf }
119

    
120
let mkpredef_call loc funname args =
121
  mkexpr loc (Expr_appl (funname, mkexpr loc (Expr_tuple args), None))
122

    
123
let is_clock_dec_type cty =
124
  match cty with
125
  | Tydec_clock _ -> true
126
  | _             -> false
127

    
128
let const_of_top top_decl =
129
  match top_decl.top_decl_desc with
130
  | Const c -> c
131
  | _ -> assert false
132

    
133
let node_of_top top_decl =
134
  match top_decl.top_decl_desc with
135
  | Node nd -> nd
136
  | _ -> raise Not_found
137

    
138
let imported_node_of_top top_decl =
139
  match top_decl.top_decl_desc with
140
  | ImportedNode ind -> ind
141
  | _ -> assert false
142

    
143
let typedef_of_top top_decl =
144
  match top_decl.top_decl_desc with
145
  | TypeDef tdef -> tdef
146
  | _ -> assert false
147

    
148
let dependency_of_top top_decl =
149
  match top_decl.top_decl_desc with
150
  | Open (local, dep) -> (local, dep)
151
  | _ -> assert false
152

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

    
171
(************************************************************)
172
(*   Eexpr functions *)
173
(************************************************************)
174

    
175

    
176
let empty_contract =
177
  {
178
    consts = []; locals = []; assume = []; guarantees = []; modes = []; imports = []; spec_loc = Location.dummy_loc;
179
  }
180
    
181
let mk_contract_var id is_const type_opt expr loc =
182
  let typ = match type_opt with None -> mktyp loc Tydec_any | Some t -> t in
183
  let v = mkvar_decl loc (id, typ, mkclock loc Ckdec_any, is_const, Some expr, None) in
184
  if is_const then
185
  { empty_contract with consts = [v]; spec_loc = loc; }
186
  else
187
    { empty_contract with locals = [v]; spec_loc = loc; }
188

    
189
let mk_contract_guarantees eexpr =
190
  { empty_contract with guarantees = [eexpr]; spec_loc = eexpr.eexpr_loc }
191

    
192
let mk_contract_assume eexpr =
193
  { empty_contract with assume = [eexpr]; spec_loc = eexpr.eexpr_loc }
194

    
195
let mk_contract_mode id rl el loc =
196
  { empty_contract with modes = [{ mode_id = id; require = rl; ensure = el; mode_loc = loc; }]; spec_loc = loc }
197

    
198
let mk_contract_import id ins outs loc =
199
  { empty_contract with imports = [{import_nodeid = id; inputs = ins; outputs = outs; import_loc = loc; }]; spec_loc = loc }
200

    
201
    
202
let merge_contracts ann1 ann2 = (* keeping the first item loc *)
203
  { consts = ann1.consts @ ann2.consts;
204
    locals = ann1.locals @ ann2.locals;
205
    assume = ann1.assume @ ann2.assume;
206
    guarantees = ann1.guarantees @ ann2.guarantees;
207
    modes = ann1.modes @ ann2.modes;
208
    imports = ann1.imports @ ann2.imports;
209
    spec_loc = ann1.spec_loc
210
  }
211

    
212
let mkeexpr loc expr =
213
  { eexpr_tag = Utils.new_tag ();
214
    eexpr_qfexpr = expr;
215
    eexpr_quantifiers = [];
216
    eexpr_type = Types.new_var ();
217
    eexpr_clock = Clocks.new_var true;
218
    eexpr_normalized = None;
219
    eexpr_loc = loc }
220

    
221
let extend_eexpr q e = { e with eexpr_quantifiers = q@e.eexpr_quantifiers }
222

    
223
(*
224
let mkepredef_call loc funname args =
225
  mkeexpr loc (EExpr_appl (funname, mkeexpr loc (EExpr_tuple args), None))
226

    
227
let mkepredef_unary_call loc funname arg =
228
  mkeexpr loc (EExpr_appl (funname, arg, None))
229
*)
230

    
231
let merge_expr_annot ann1 ann2 =
232
  match ann1, ann2 with
233
    | None, None -> assert false
234
    | Some _, None -> ann1
235
    | None, Some _ -> ann2
236
    | Some ann1, Some ann2 -> Some {
237
      annots = ann1.annots @ ann2.annots;
238
      annot_loc = ann1.annot_loc
239
    }
240

    
241
let update_expr_annot node_id e annot =
242
  List.iter (fun (key, _) -> 
243
    Annotations.add_expr_ann node_id e.expr_tag key
244
  ) annot.annots;
245
  e.expr_annot <- merge_expr_annot e.expr_annot (Some annot);
246
  e
247

    
248

    
249
let mkinstr ?lustre_expr ?lustre_eq i =
250
  {
251
    instr_desc = i;
252
    (* lustre_expr = lustre_expr; *)
253
    lustre_eq = lustre_eq;
254
  }
255

    
256
let get_instr_desc i = i.instr_desc
257
let update_instr_desc i id = { i with instr_desc = id }
258

    
259
(***********************************************************)
260
(* Fast access to nodes, by name *)
261
let (node_table : (ident, top_decl) Hashtbl.t) = Hashtbl.create 30
262
let consts_table = Hashtbl.create 30
263

    
264
let print_node_table fmt () =
265
  begin
266
    Format.fprintf fmt "{ /* node table */@.";
267
    Hashtbl.iter (fun id nd ->
268
      Format.fprintf fmt "%s |-> %a"
269
	id
270
	Printers.pp_short_decl nd
271
    ) node_table;
272
    Format.fprintf fmt "}@."
273
  end
274

    
275
let print_consts_table fmt () =
276
  begin
277
    Format.fprintf fmt "{ /* consts table */@.";
278
    Hashtbl.iter (fun id const ->
279
      Format.fprintf fmt "%s |-> %a"
280
	id
281
	Printers.pp_const_decl (const_of_top const)
282
    ) consts_table;
283
    Format.fprintf fmt "}@."
284
  end
285

    
286
let node_name td =
287
    match td.top_decl_desc with 
288
    | Node nd         -> nd.node_id
289
    | ImportedNode nd -> nd.nodei_id
290
    | _ -> assert false
291

    
292
let is_generic_node td =
293
  match td.top_decl_desc with 
294
  | Node nd         -> List.exists (fun v -> v.var_dec_const) nd.node_inputs
295
  | ImportedNode nd -> List.exists (fun v -> v.var_dec_const) nd.nodei_inputs
296
  | _ -> assert false
297

    
298
let node_inputs td =
299
  match td.top_decl_desc with 
300
  | Node nd         -> nd.node_inputs
301
  | ImportedNode nd -> nd.nodei_inputs
302
  | _ -> assert false
303

    
304
let node_from_name id =
305
  try
306
    Hashtbl.find node_table id
307
  with Not_found -> (Format.eprintf "Unable to find any node named %s@ @?" id;
308
		     assert false)
309

    
310
let is_imported_node td =
311
  match td.top_decl_desc with 
312
  | Node nd         -> false
313
  | ImportedNode nd -> true
314
  | _ -> assert false
315

    
316

    
317
(* alias and type definition table *)
318

    
319
let mktop = mktop_decl Location.dummy_loc !Options.dest_dir false
320

    
321
let top_int_type = mktop (TypeDef {tydef_id = "int"; tydef_desc = Tydec_int})
322
let top_bool_type = mktop (TypeDef {tydef_id = "bool"; tydef_desc = Tydec_bool})
323
(* let top_float_type = mktop (TypeDef {tydef_id = "float"; tydef_desc = Tydec_float}) *)
324
let top_real_type = mktop (TypeDef {tydef_id = "real"; tydef_desc = Tydec_real})
325

    
326
let type_table =
327
  Utils.create_hashtable 20 [
328
    Tydec_int  , top_int_type;
329
    Tydec_bool , top_bool_type;
330
    (* Tydec_float, top_float_type; *)
331
    Tydec_real , top_real_type
332
  ]
333

    
334
let print_type_table fmt () =
335
  begin
336
    Format.fprintf fmt "{ /* type table */@.";
337
    Hashtbl.iter (fun tydec tdef ->
338
      Format.fprintf fmt "%a |-> %a"
339
	Printers.pp_var_type_dec_desc tydec
340
	Printers.pp_typedef (typedef_of_top tdef)
341
    ) type_table;
342
    Format.fprintf fmt "}@."
343
  end
344

    
345
let rec is_user_type typ =
346
  match typ with
347
  | Tydec_int | Tydec_bool | Tydec_real 
348
  (* | Tydec_float *) | Tydec_any | Tydec_const _ -> false
349
  | Tydec_clock typ' -> is_user_type typ'
350
  | _ -> true
351

    
352
let get_repr_type typ =
353
  let typ_def = (typedef_of_top (Hashtbl.find type_table typ)).tydef_desc in
354
  if is_user_type typ_def then typ else typ_def
355

    
356
let rec coretype_equal ty1 ty2 =
357
  let res =
358
  match ty1, ty2 with
359
  | Tydec_any           , _
360
  | _                   , Tydec_any             -> assert false
361
  | Tydec_const _       , Tydec_const _         -> get_repr_type ty1 = get_repr_type ty2
362
  | Tydec_const _       , _                     -> let ty1' = (typedef_of_top (Hashtbl.find type_table ty1)).tydef_desc
363
	       					   in (not (is_user_type ty1')) && coretype_equal ty1' ty2
364
  | _                   , Tydec_const _         -> coretype_equal ty2 ty1
365
  | Tydec_int           , Tydec_int
366
  | Tydec_real          , Tydec_real
367
  (* | Tydec_float         , Tydec_float *)
368
  | Tydec_bool          , Tydec_bool            -> true
369
  | Tydec_clock ty1     , Tydec_clock ty2       -> coretype_equal ty1 ty2
370
  | Tydec_array (d1,ty1), Tydec_array (d2, ty2) -> Dimension.is_eq_dimension d1 d2 && coretype_equal ty1 ty2
371
  | Tydec_enum tl1      , Tydec_enum tl2        -> List.sort compare tl1 = List.sort compare tl2
372
  | Tydec_struct fl1    , Tydec_struct fl2      ->
373
       List.length fl1 = List.length fl2
374
    && List.for_all2 (fun (f1, t1) (f2, t2) -> f1 = f2 && coretype_equal t1 t2)
375
      (List.sort (fun (f1,_) (f2,_) -> compare f1 f2) fl1)
376
      (List.sort (fun (f1,_) (f2,_) -> compare f1 f2) fl2)
377
  | _                                  -> false
378
  in ((*Format.eprintf "coretype_equal %a %a = %B@." Printers.pp_var_type_dec_desc ty1 Printers.pp_var_type_dec_desc ty2 res;*) res)
379

    
380
let tag_true = "true"
381
let tag_false = "false"
382
let tag_default = "default"
383

    
384
let const_is_bool c =
385
 match c with
386
 | Const_tag t -> t = tag_true || t = tag_false
387
 | _           -> false
388

    
389
(* Computes the negation of a boolean constant *)
390
let const_negation c =
391
  assert (const_is_bool c);
392
  match c with
393
  | Const_tag t when t = tag_true  -> Const_tag tag_false
394
  | _                              -> Const_tag tag_true
395

    
396
let const_or c1 c2 =
397
  assert (const_is_bool c1 && const_is_bool c2);
398
  match c1, c2 with
399
  | Const_tag t1, _            when t1 = tag_true -> c1
400
  | _           , Const_tag t2 when t2 = tag_true -> c2
401
  | _                                             -> Const_tag tag_false
402

    
403
let const_and c1 c2 =
404
  assert (const_is_bool c1 && const_is_bool c2);
405
  match c1, c2 with
406
  | Const_tag t1, _            when t1 = tag_false -> c1
407
  | _           , Const_tag t2 when t2 = tag_false -> c2
408
  | _                                              -> Const_tag tag_true
409

    
410
let const_xor c1 c2 =
411
  assert (const_is_bool c1 && const_is_bool c2);
412
   match c1, c2 with
413
  | Const_tag t1, Const_tag t2 when t1 <> t2  -> Const_tag tag_true
414
  | _                                         -> Const_tag tag_false
415

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

    
423
(* To guarantee uniqueness of tags in enum types *)
424
let tag_table =
425
  Utils.create_hashtable 20 [
426
   tag_true, top_bool_type;
427
   tag_false, top_bool_type
428
  ]
429

    
430
(* To guarantee uniqueness of fields in struct types *)
431
let field_table =
432
  Utils.create_hashtable 20 [
433
  ]
434

    
435
let get_enum_type_tags cty =
436
(*Format.eprintf "get_enum_type_tags %a@." Printers.pp_var_type_dec_desc cty;*)
437
 match cty with
438
 | Tydec_bool    -> [tag_true; tag_false]
439
 | Tydec_const _ -> (match (typedef_of_top (Hashtbl.find type_table cty)).tydef_desc with
440
                     | Tydec_enum tl -> tl
441
                     | _             -> assert false)
442
 | _            -> assert false
443

    
444
let get_struct_type_fields cty =
445
 match cty with
446
 | Tydec_const _ -> (match (typedef_of_top (Hashtbl.find type_table cty)).tydef_desc with
447
                     | Tydec_struct fl -> fl
448
                     | _               -> assert false)
449
 | _            -> assert false
450

    
451
let const_of_bool b =
452
 Const_tag (if b then tag_true else tag_false)
453

    
454
(* let get_const c = snd (Hashtbl.find consts_table c) *)
455

    
456
let ident_of_expr expr =
457
 match expr.expr_desc with
458
 | Expr_ident id -> id
459
 | _             -> assert false
460

    
461
(* Generate a new ident expression from a declared variable *)
462
let expr_of_vdecl v =
463
  { expr_tag = Utils.new_tag ();
464
    expr_desc = Expr_ident v.var_id;
465
    expr_type = v.var_type;
466
    expr_clock = v.var_clock;
467
    expr_delay = Delay.new_var ();
468
    expr_annot = None;
469
    expr_loc = v.var_loc }
470

    
471
(* Caution, returns an untyped and unclocked expression *)
472
let expr_of_ident id loc =
473
  {expr_tag = Utils.new_tag ();
474
   expr_desc = Expr_ident id;
475
   expr_type = Types.new_var ();
476
   expr_clock = Clocks.new_var true;
477
   expr_delay = Delay.new_var ();
478
   expr_loc = loc;
479
   expr_annot = None}
480

    
481
let is_tuple_expr expr =
482
 match expr.expr_desc with
483
  | Expr_tuple _ -> true
484
  | _            -> false
485

    
486
let expr_list_of_expr expr =
487
  match expr.expr_desc with
488
  | Expr_tuple elist -> elist
489
  | _                -> [expr]
490

    
491
let expr_of_expr_list loc elist =
492
 match elist with
493
 | [t]  -> { t with expr_loc = loc }
494
 | t::_ ->
495
    let tlist = List.map (fun e -> e.expr_type) elist in
496
    let clist = List.map (fun e -> e.expr_clock) elist in
497
    { t with expr_desc = Expr_tuple elist;
498
	     expr_type = Type_predef.type_tuple tlist;
499
	     expr_clock = Clock_predef.ck_tuple clist;
500
	     expr_tag = Utils.new_tag ();
501
	     expr_loc = loc }
502
 | _    -> assert false
503

    
504
let call_of_expr expr =
505
 match expr.expr_desc with
506
 | Expr_appl (f, args, r) -> (f, expr_list_of_expr args, r)
507
 | _                      -> assert false
508

    
509
    
510
(* Conversion from dimension expr to standard expr, for the purpose of printing, typing, etc... *)
511
let rec expr_of_dimension dim =
512
  let open Dimension in
513
  match dim.dim_desc with
514
 | Dbool b        ->
515
     mkexpr dim.dim_loc (Expr_const (const_of_bool b))
516
 | Dint i         ->
517
     mkexpr dim.dim_loc (Expr_const (Const_int i))
518
 | Dident id      ->
519
     mkexpr dim.dim_loc (Expr_ident id)
520
 | Dite (c, t, e) ->
521
     mkexpr dim.dim_loc (Expr_ite (expr_of_dimension c, expr_of_dimension t, expr_of_dimension e))
522
 | Dappl (id, args) ->
523
     mkexpr dim.dim_loc (Expr_appl (id, expr_of_expr_list dim.dim_loc (List.map expr_of_dimension args), None))
524
 | Dlink dim'       -> expr_of_dimension dim'
525
 | Dvar
526
 | Dunivar          -> (Format.eprintf "internal error: Corelang.expr_of_dimension %a@." Dimension.pp_dimension dim;
527
			assert false)
528

    
529
let dimension_of_const loc const =
530
  let open Dimension in
531
 match const with
532
 | Const_int i                                    -> mkdim_int loc i
533
 | Const_tag t when t = tag_true || t = tag_false -> mkdim_bool loc (t = tag_true)
534
 | _                                              -> raise InvalidDimension
535

    
536
(* Conversion from standard expr to dimension expr, for the purpose of injecting static call arguments 
537
   into dimension expressions *)
538
let rec dimension_of_expr expr =
539
  let open Dimension in
540
  match expr.expr_desc with
541
  | Expr_const c  -> dimension_of_const expr.expr_loc c
542
  | Expr_ident id -> mkdim_ident expr.expr_loc id
543
  | Expr_appl (f, args, None) when Basic_library.is_expr_internal_fun expr ->
544
      let k = Types.get_static_value (Env.lookup_value Basic_library.type_env f) in
545
      if k = None then raise InvalidDimension;
546
      mkdim_appl expr.expr_loc f (List.map dimension_of_expr (expr_list_of_expr args))
547
  | Expr_ite (i, t, e)        ->
548
      mkdim_ite expr.expr_loc (dimension_of_expr i) (dimension_of_expr t) (dimension_of_expr e)
549
  | _ -> raise InvalidDimension (* not a simple dimension expression *)
550

    
551

    
552
let sort_handlers hl =
553
 List.sort (fun (t, _) (t', _) -> compare t t') hl
554

    
555
let num_10 = Num.num_of_int 10
556

    
557
let cst_real_to_num n i =
558
  Num.(n // (num_10 **/ (num_of_int i)))
559

    
560
let rec is_eq_const c1 c2 =
561
  match c1, c2 with
562
  | Const_real (n1, i1, _), Const_real (n2, i2, _)
563
    -> let n1 = cst_real_to_num n1 i1 in
564
       let n2 = cst_real_to_num n2 i2 in
565
	    Num.eq_num n1 n2
566
  | Const_struct lcl1, Const_struct lcl2
567
    -> List.length lcl1 = List.length lcl2
568
    && List.for_all2 (fun (l1, c1) (l2, c2) -> l1 = l2 && is_eq_const c1 c2) lcl1 lcl2
569
  | _  -> c1 = c2
570

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

    
590
let get_node_vars nd =
591
  nd.node_inputs @ nd.node_locals @ nd.node_outputs
592

    
593
let mk_new_node_name nd id =
594
  let used_vars = get_node_vars nd in
595
  let used v = List.exists (fun vdecl -> vdecl.var_id = v) used_vars in
596
  mk_new_name used id
597

    
598
let get_var id var_list =
599
  List.find (fun v -> v.var_id = id) var_list
600

    
601
let get_node_var id node =
602
  try
603
    get_var id (get_node_vars node)
604
  with Not_found -> begin
605
    (* Format.eprintf "Unable to find variable %s in node %s@.@?" id node.node_id; *)
606
    raise Not_found
607
  end
608

    
609

    
610
let get_node_eqs =
611
  let get_eqs stmts =
612
    List.fold_right
613
      (fun stmt (res_eq, res_aut) ->
614
	match stmt with
615
	| Eq eq -> eq :: res_eq, res_aut
616
	| Aut aut -> res_eq, aut::res_aut)
617
      stmts
618
      ([], []) in
619
  let table_eqs = Hashtbl.create 23 in
620
  (fun nd ->
621
    try
622
      let (old, res) = Hashtbl.find table_eqs nd.node_id
623
      in if old == nd.node_stmts then res else raise Not_found
624
    with Not_found -> 
625
      let res = get_eqs nd.node_stmts in
626
      begin
627
	Hashtbl.replace table_eqs nd.node_id (nd.node_stmts, res);
628
	res
629
      end)
630

    
631
let get_node_eq id node =
632
  let eqs, auts = get_node_eqs node in
633
  try
634
    List.find (fun eq -> List.mem id eq.eq_lhs) eqs
635
  with
636
    Not_found -> (* Shall be defined in automata auts *) raise Not_found
637
      
638
let get_nodes prog = 
639
  List.fold_left (
640
    fun nodes decl ->
641
      match decl.top_decl_desc with
642
	| Node _ -> decl::nodes
643
	| Const _ | ImportedNode _ | Open _ | TypeDef _ -> nodes  
644
  ) [] prog
645

    
646
let get_imported_nodes prog = 
647
  List.fold_left (
648
    fun nodes decl ->
649
      match decl.top_decl_desc with
650
	| ImportedNode _ -> decl::nodes
651
	| Const _ | Node _ | Open _ | TypeDef _-> nodes  
652
  ) [] prog
653

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

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

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

    
678
let get_node_interface nd =
679
 {nodei_id = nd.node_id;
680
  nodei_type = nd.node_type;
681
  nodei_clock = nd.node_clock;
682
  nodei_inputs = nd.node_inputs;
683
  nodei_outputs = nd.node_outputs;
684
  nodei_stateless = nd.node_dec_stateless;
685
  nodei_spec = nd.node_spec;
686
  (* nodei_annot = nd.node_annot; *)
687
  nodei_prototype = None;
688
  nodei_in_lib = [];
689
 }
690

    
691
(************************************************************************)
692
(*        Renaming                                                      *)
693

    
694
let rec rename_static rename cty =
695
 match cty with
696
 | Tydec_array (d, cty') -> Tydec_array (Dimension.expr_replace_expr rename d, rename_static rename cty')
697
 | Tydec_clock cty       -> Tydec_clock (rename_static rename cty)
698
 | Tydec_struct fl       -> Tydec_struct (List.map (fun (f, cty) -> f, rename_static rename cty) fl)
699
 | _                      -> cty
700

    
701
let rec rename_carrier rename cck =
702
 match cck with
703
 | Ckdec_bool cl -> Ckdec_bool (List.map (fun (c, l) -> rename c, l) cl)
704
 | _             -> cck
705

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

    
708
(* applies the renaming function [fvar] to all variables of expression [expr] *)
709
 (* let rec expr_replace_var fvar expr = *)
710
 (*  { expr with expr_desc = expr_desc_replace_var fvar expr.expr_desc } *)
711

    
712
 (* and expr_desc_replace_var fvar expr_desc = *)
713
 (*   match expr_desc with *)
714
 (*   | Expr_const _ -> expr_desc *)
715
 (*   | Expr_ident i -> Expr_ident (fvar i) *)
716
 (*   | Expr_array el -> Expr_array (List.map (expr_replace_var fvar) el) *)
717
 (*   | Expr_access (e1, d) -> Expr_access (expr_replace_var fvar e1, d) *)
718
 (*   | Expr_power (e1, d) -> Expr_power (expr_replace_var fvar e1, d) *)
719
 (*   | Expr_tuple el -> Expr_tuple (List.map (expr_replace_var fvar) el) *)
720
 (*   | Expr_ite (c, t, e) -> Expr_ite (expr_replace_var fvar c, expr_replace_var fvar t, expr_replace_var fvar e) *)
721
 (*   | Expr_arrow (e1, e2)-> Expr_arrow (expr_replace_var fvar e1, expr_replace_var fvar e2)  *)
722
 (*   | Expr_fby (e1, e2) -> Expr_fby (expr_replace_var fvar e1, expr_replace_var fvar e2) *)
723
 (*   | Expr_pre e' -> Expr_pre (expr_replace_var fvar e') *)
724
 (*   | Expr_when (e', i, l)-> Expr_when (expr_replace_var fvar e', fvar i, l) *)
725
 (*   | Expr_merge (i, hl) -> Expr_merge (fvar i, List.map (fun (t, h) -> (t, expr_replace_var fvar h)) hl) *)
726
 (*   | Expr_appl (i, e', i') -> Expr_appl (i, expr_replace_var fvar e', Utils.option_map (expr_replace_var fvar) i') *)
727

    
728

    
729

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

    
751
 let rename_dec_type f_node f_var t = assert false (*
752
						     Types.rename_dim_type (Dimension.rename f_node f_var) t*)
753

    
754
 let rename_dec_clock f_node f_var c = assert false (* 
755
					  Clocks.rename_clock_expr f_var c*)
756
   
757
 let rename_var f_node f_var v = {
758
   v with
759
     var_id = f_var v.var_id;
760
     var_dec_type = rename_dec_type f_node f_var v.var_type;
761
     var_dec_clock = rename_dec_clock f_node f_var v.var_clock
762
 } 
763

    
764
 let rename_vars f_node f_var = List.map (rename_var f_node f_var) 
765

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

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

    
859

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

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

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

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

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

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

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

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

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

    
955

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

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

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

    
991

    
992

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

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

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

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

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

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

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

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

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

    
1163

    
1164

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

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

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

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

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

    
1192

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

    
1214

    
1215

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

    
1219
let reset_cpt_fresh () =
1220
    cpt_fresh := 0
1221
    
1222
let mk_fresh_var node loc ty ck =
1223
  let vars = get_node_vars node in
1224
  let rec aux () =
1225
  incr cpt_fresh;
1226
  let s = Printf.sprintf "__%s_%d" node.node_id !cpt_fresh in
1227
  if List.exists (fun v -> v.var_id = s) vars 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 node.node_id;
1236
    var_type = ty;
1237
    var_clock = ck;
1238
    var_loc = loc
1239
  }
1240
  in aux ()
1241

    
1242
       
1243
let get_node name prog =
1244
  let node_opt = List.fold_left
1245
    (fun res top -> 
1246
      match res, top.top_decl_desc with
1247
      | Some _, _ -> res
1248
      | None, Node nd -> 
1249
	(* Format.eprintf "Checking node %s = %s: %b@." nd.node_id name (nd.node_id = name); *)
1250
	if nd.node_id = name then Some nd else res
1251
      | _ -> None) 
1252
    None prog 
1253
  in
1254
  try 
1255
    Utils.desome node_opt
1256
  with Utils.DeSome -> raise Not_found
1257

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