Project

General

Profile

Download (48.1 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
module VDeclModule =
19
struct (* Node module *)
20
  type t = var_decl
21
  let compare v1 v2 = compare v1.var_id v2.var_id
22
end
23

    
24
module VMap = Map.Make(VDeclModule)
25

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

    
42
let dummy_clock_dec = {ck_dec_desc=Ckdec_any; ck_dec_loc=Location.dummy_loc}
43

    
44

    
45

    
46
(************************************************************)
47
(* *)
48

    
49
let mktyp loc d =
50
  { ty_dec_desc = d; ty_dec_loc = loc }
51

    
52
let mkclock loc d =
53
  { ck_dec_desc = d; ck_dec_loc = loc }
54

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

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

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

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

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

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

    
115
let mkassert loc expr =
116
  { assert_loc = loc;
117
    assert_expr = expr
118
  }
119

    
120
let mktop_decl loc own itf d =
121
  { top_decl_desc = d; top_decl_loc = loc; top_decl_owner = own; top_decl_itf = itf }
122

    
123
let mkpredef_call loc funname args =
124
  mkexpr loc (Expr_appl (funname, mkexpr loc (Expr_tuple args), None))
125

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

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

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

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

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

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

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

    
174
(************************************************************)
175
(*   Eexpr functions *)
176
(************************************************************)
177

    
178

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

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

    
200
let mk_contract_assume ?(name="") eexpr =
201
  { empty_contract with assume = [eexpr_add_name eexpr name]; spec_loc = eexpr.eexpr_loc }
202

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

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

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

    
221
let mkeexpr loc expr =
222
  { eexpr_tag = Utils.new_tag ();
223
    eexpr_qfexpr = expr;
224
    eexpr_quantifiers = [];
225
    eexpr_name = None;
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
      
316
let update_node id top =
317
  Hashtbl.replace node_table id top
318

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

    
325
let is_node_contract nd =
326
  match nd.node_spec with
327
  | Some (Contract _) -> true
328
  | _ -> false
329
  
330
let get_node_contract nd =
331
  match nd.node_spec with
332
  | Some (Contract c) -> c
333
  | _ -> assert false
334
  
335
let is_contract td =
336
  match td.top_decl_desc with 
337
  | Node nd -> is_node_contract nd
338
  | _ -> false
339

    
340
(* alias and type definition table *)
341

    
342
let mktop = mktop_decl Location.dummy_loc !Options.dest_dir false
343

    
344
let top_int_type = mktop (TypeDef {tydef_id = "int"; tydef_desc = Tydec_int})
345
let top_bool_type = mktop (TypeDef {tydef_id = "bool"; tydef_desc = Tydec_bool})
346
(* let top_float_type = mktop (TypeDef {tydef_id = "float"; tydef_desc = Tydec_float}) *)
347
let top_real_type = mktop (TypeDef {tydef_id = "real"; tydef_desc = Tydec_real})
348

    
349
let type_table =
350
  Utils.create_hashtable 20 [
351
    Tydec_int  , top_int_type;
352
    Tydec_bool , top_bool_type;
353
    (* Tydec_float, top_float_type; *)
354
    Tydec_real , top_real_type
355
  ]
356

    
357
let print_type_table fmt () =
358
  begin
359
    Format.fprintf fmt "{ /* type table */@.";
360
    Hashtbl.iter (fun tydec tdef ->
361
      Format.fprintf fmt "%a |-> %a"
362
	Printers.pp_var_type_dec_desc tydec
363
	Printers.pp_typedef (typedef_of_top tdef)
364
    ) type_table;
365
    Format.fprintf fmt "}@."
366
  end
367

    
368
let rec is_user_type typ =
369
  match typ with
370
  | Tydec_int | Tydec_bool | Tydec_real 
371
  (* | Tydec_float *) | Tydec_any | Tydec_const _ -> false
372
  | Tydec_clock typ' -> is_user_type typ'
373
  | _ -> true
374

    
375
let get_repr_type typ =
376
  let typ_def = (typedef_of_top (Hashtbl.find type_table typ)).tydef_desc in
377
  if is_user_type typ_def then typ else typ_def
378

    
379
let rec coretype_equal ty1 ty2 =
380
  let res =
381
  match ty1, ty2 with
382
  | Tydec_any           , _
383
  | _                   , Tydec_any             -> assert false
384
  | Tydec_const _       , Tydec_const _         -> get_repr_type ty1 = get_repr_type ty2
385
  | Tydec_const _       , _                     -> let ty1' = (typedef_of_top (Hashtbl.find type_table ty1)).tydef_desc
386
	       					   in (not (is_user_type ty1')) && coretype_equal ty1' ty2
387
  | _                   , Tydec_const _         -> coretype_equal ty2 ty1
388
  | Tydec_int           , Tydec_int
389
  | Tydec_real          , Tydec_real
390
  (* | Tydec_float         , Tydec_float *)
391
  | Tydec_bool          , Tydec_bool            -> true
392
  | Tydec_clock ty1     , Tydec_clock ty2       -> coretype_equal ty1 ty2
393
  | Tydec_array (d1,ty1), Tydec_array (d2, ty2) -> Dimension.is_eq_dimension d1 d2 && coretype_equal ty1 ty2
394
  | Tydec_enum tl1      , Tydec_enum tl2        -> List.sort compare tl1 = List.sort compare tl2
395
  | Tydec_struct fl1    , Tydec_struct fl2      ->
396
       List.length fl1 = List.length fl2
397
    && List.for_all2 (fun (f1, t1) (f2, t2) -> f1 = f2 && coretype_equal t1 t2)
398
      (List.sort (fun (f1,_) (f2,_) -> compare f1 f2) fl1)
399
      (List.sort (fun (f1,_) (f2,_) -> compare f1 f2) fl2)
400
  | _                                  -> false
401
  in ((*Format.eprintf "coretype_equal %a %a = %B@." Printers.pp_var_type_dec_desc ty1 Printers.pp_var_type_dec_desc ty2 res;*) res)
402

    
403
let tag_default = "default"
404

    
405
let const_is_bool c =
406
 match c with
407
 | Const_tag t -> t = tag_true || t = tag_false
408
 | _           -> false
409

    
410
(* Computes the negation of a boolean constant *)
411
let const_negation c =
412
  assert (const_is_bool c);
413
  match c with
414
  | Const_tag t when t = tag_true  -> Const_tag tag_false
415
  | _                              -> Const_tag tag_true
416

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

    
424
let const_and c1 c2 =
425
  assert (const_is_bool c1 && const_is_bool c2);
426
  match c1, c2 with
427
  | Const_tag t1, _            when t1 = tag_false -> c1
428
  | _           , Const_tag t2 when t2 = tag_false -> c2
429
  | _                                              -> Const_tag tag_true
430

    
431
let const_xor c1 c2 =
432
  assert (const_is_bool c1 && const_is_bool c2);
433
   match c1, c2 with
434
  | Const_tag t1, Const_tag t2 when t1 <> t2  -> Const_tag tag_true
435
  | _                                         -> Const_tag tag_false
436

    
437
let const_impl c1 c2 =
438
  assert (const_is_bool c1 && const_is_bool c2);
439
  match c1, c2 with
440
  | Const_tag t1, _ when t1 = tag_false           -> Const_tag tag_true
441
  | _           , Const_tag t2 when t2 = tag_true -> Const_tag tag_true
442
  | _                                             -> Const_tag tag_false
443

    
444
(* To guarantee uniqueness of tags in enum types *)
445
let tag_table =
446
  Utils.create_hashtable 20 [
447
   tag_true, top_bool_type;
448
   tag_false, top_bool_type
449
  ]
450

    
451
(* To guarantee uniqueness of fields in struct types *)
452
let field_table =
453
  Utils.create_hashtable 20 [
454
  ]
455

    
456
let get_enum_type_tags cty =
457
(*Format.eprintf "get_enum_type_tags %a@." Printers.pp_var_type_dec_desc cty;*)
458
 match cty with
459
 | Tydec_bool    -> [tag_true; tag_false]
460
 | Tydec_const _ -> (match (typedef_of_top (Hashtbl.find type_table cty)).tydef_desc with
461
                     | Tydec_enum tl -> tl
462
                     | _             -> assert false)
463
 | _            -> assert false
464

    
465
let get_struct_type_fields cty =
466
 match cty with
467
 | Tydec_const _ -> (match (typedef_of_top (Hashtbl.find type_table cty)).tydef_desc with
468
                     | Tydec_struct fl -> fl
469
                     | _               -> assert false)
470
 | _            -> assert false
471

    
472
let const_of_bool b =
473
 Const_tag (if b then tag_true else tag_false)
474

    
475
(* let get_const c = snd (Hashtbl.find consts_table c) *)
476

    
477
let ident_of_expr expr =
478
 match expr.expr_desc with
479
 | Expr_ident id -> id
480
 | _             -> assert false
481

    
482
(* Generate a new ident expression from a declared variable *)
483
let expr_of_vdecl v =
484
  { expr_tag = Utils.new_tag ();
485
    expr_desc = Expr_ident v.var_id;
486
    expr_type = v.var_type;
487
    expr_clock = v.var_clock;
488
    expr_delay = Delay.new_var ();
489
    expr_annot = None;
490
    expr_loc = v.var_loc }
491

    
492
(* Caution, returns an untyped and unclocked expression *)
493
let expr_of_ident id loc =
494
  {expr_tag = Utils.new_tag ();
495
   expr_desc = Expr_ident id;
496
   expr_type = Types.new_var ();
497
   expr_clock = Clocks.new_var true;
498
   expr_delay = Delay.new_var ();
499
   expr_loc = loc;
500
   expr_annot = None}
501

    
502
let is_tuple_expr expr =
503
 match expr.expr_desc with
504
  | Expr_tuple _ -> true
505
  | _            -> false
506

    
507
let expr_list_of_expr expr =
508
  match expr.expr_desc with
509
  | Expr_tuple elist -> elist
510
  | _                -> [expr]
511

    
512
let expr_of_expr_list loc elist =
513
 match elist with
514
 | [t]  -> { t with expr_loc = loc }
515
 | t::_ ->
516
    let tlist = List.map (fun e -> e.expr_type) elist in
517
    let clist = List.map (fun e -> e.expr_clock) elist in
518
    { t with expr_desc = Expr_tuple elist;
519
	     expr_type = Type_predef.type_tuple tlist;
520
	     expr_clock = Clock_predef.ck_tuple clist;
521
	     expr_tag = Utils.new_tag ();
522
	     expr_loc = loc }
523
 | _    -> assert false
524

    
525
let call_of_expr expr =
526
 match expr.expr_desc with
527
 | Expr_appl (f, args, r) -> (f, expr_list_of_expr args, r)
528
 | _                      -> assert false
529

    
530
    
531
(* Conversion from dimension expr to standard expr, for the purpose of printing, typing, etc... *)
532
let rec expr_of_dimension dim =
533
  let open Dimension in
534
  let expr =
535
  match dim.dim_desc with
536
 | Dbool b        ->
537
     mkexpr dim.dim_loc (Expr_const (const_of_bool b))
538
 | Dint i         ->
539
     mkexpr dim.dim_loc (Expr_const (Const_int i))
540
 | Dident id      ->
541
     mkexpr dim.dim_loc (Expr_ident id)
542
 | Dite (c, t, e) ->
543
     mkexpr dim.dim_loc (Expr_ite (expr_of_dimension c, expr_of_dimension t, expr_of_dimension e))
544
 | Dappl (id, args) ->
545
     mkexpr dim.dim_loc (Expr_appl (id, expr_of_expr_list dim.dim_loc (List.map expr_of_dimension args), None))
546
 | Dlink dim'       -> expr_of_dimension dim'
547
 | Dvar
548
 | Dunivar          -> (Format.eprintf "internal error: Corelang.expr_of_dimension %a@." Dimension.pp_dimension dim;
549
			assert false)
550
  in
551
  { expr
552
  with
553
    expr_type = Types.new_ty Types.type_int;
554
  }
555
  
556
let dimension_of_const loc const =
557
  let open Dimension in
558
 match const with
559
 | Const_int i                                    -> mkdim_int loc i
560
 | Const_tag t when t = tag_true || t = tag_false -> mkdim_bool loc (t = tag_true)
561
 | _                                              -> raise InvalidDimension
562

    
563
(* Conversion from standard expr to dimension expr, for the purpose of injecting static call arguments 
564
   into dimension expressions *)
565
let rec dimension_of_expr expr =
566
  let open Dimension in
567
  match expr.expr_desc with
568
  | Expr_const c  -> dimension_of_const expr.expr_loc c
569
  | Expr_ident id -> mkdim_ident expr.expr_loc id
570
  | Expr_appl (f, args, None) when Basic_library.is_expr_internal_fun expr ->
571
      let k = Types.get_static_value (Env.lookup_value Basic_library.type_env f) in
572
      if k = None then raise InvalidDimension;
573
      mkdim_appl expr.expr_loc f (List.map dimension_of_expr (expr_list_of_expr args))
574
  | Expr_ite (i, t, e)        ->
575
      mkdim_ite expr.expr_loc (dimension_of_expr i) (dimension_of_expr t) (dimension_of_expr e)
576
  | _ -> raise InvalidDimension (* not a simple dimension expression *)
577

    
578

    
579
let sort_handlers hl =
580
 List.sort (fun (t, _) (t', _) -> compare t t') hl
581

    
582
  
583
let rec is_eq_const c1 c2 =
584
  match c1, c2 with
585
  | Const_real r1, Const_real r2
586
    -> Real.eq r1 r1 
587
  | Const_struct lcl1, Const_struct lcl2
588
    -> List.length lcl1 = List.length lcl2
589
    && List.for_all2 (fun (l1, c1) (l2, c2) -> l1 = l2 && is_eq_const c1 c2) lcl1 lcl2
590
  | _  -> c1 = c2
591

    
592
let rec is_eq_expr e1 e2 = match e1.expr_desc, e2.expr_desc with
593
  | Expr_const c1, Expr_const c2 -> is_eq_const c1 c2
594
  | Expr_ident i1, Expr_ident i2 -> i1 = i2
595
  | Expr_array el1, Expr_array el2 
596
  | Expr_tuple el1, Expr_tuple el2 -> 
597
    List.length el1 = List.length el2 && List.for_all2 is_eq_expr el1 el2 
598
  | Expr_arrow (e1, e2), Expr_arrow (e1', e2') -> is_eq_expr e1 e1' && is_eq_expr e2 e2'
599
  | Expr_fby (e1,e2), Expr_fby (e1',e2') -> is_eq_expr e1 e1' && is_eq_expr e2 e2'
600
  | 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
601
  (* | Expr_concat (e1,e2), Expr_concat (e1',e2') -> is_eq_expr e1 e1' && is_eq_expr e2 e2' *)
602
  (* | Expr_tail e, Expr_tail e' -> is_eq_expr e e' *)
603
  | Expr_pre e, Expr_pre e' -> is_eq_expr e e'
604
  | Expr_when (e, i, l), Expr_when (e', i', l') -> l=l' && i=i' && is_eq_expr e e'
605
  | 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')
606
  | Expr_appl (i, e, r), Expr_appl (i', e', r') -> i=i' && r=r' && is_eq_expr e e'
607
  | Expr_power (e1, i1), Expr_power (e2, i2)
608
  | Expr_access (e1, i1), Expr_access (e2, i2) -> is_eq_expr e1 e2 && is_eq_expr (expr_of_dimension i1) (expr_of_dimension i2)
609
  | _ -> false
610

    
611
let get_node_vars nd =
612
  nd.node_inputs @ nd.node_locals @ nd.node_outputs
613

    
614
let mk_new_node_name nd id =
615
  let used_vars = get_node_vars nd in
616
  let used v = List.exists (fun vdecl -> vdecl.var_id = v) used_vars in
617
  mk_new_name used id
618

    
619
let get_var id var_list =
620
  List.find (fun v -> v.var_id = id) var_list
621

    
622
let get_node_var id node =
623
  try
624
    get_var id (get_node_vars node)
625
  with Not_found -> begin
626
    (* Format.eprintf "Unable to find variable %s in node %s@.@?" id node.node_id; *)
627
    raise Not_found
628
  end
629

    
630

    
631
let get_node_eqs =
632
  let get_eqs stmts =
633
    List.fold_right
634
      (fun stmt (res_eq, res_aut) ->
635
	match stmt with
636
	| Eq eq -> eq :: res_eq, res_aut
637
	| Aut aut -> res_eq, aut::res_aut)
638
      stmts
639
      ([], []) in
640
  let table_eqs = Hashtbl.create 23 in
641
  (fun nd ->
642
    try
643
      let (old, res) = Hashtbl.find table_eqs nd.node_id
644
      in if old == nd.node_stmts then res else raise Not_found
645
    with Not_found -> 
646
      let res = get_eqs nd.node_stmts in
647
      begin
648
	Hashtbl.replace table_eqs nd.node_id (nd.node_stmts, res);
649
	res
650
      end)
651

    
652
let get_node_eq id node =
653
  let eqs, auts = get_node_eqs node in
654
  try
655
    List.find (fun eq -> List.mem id eq.eq_lhs) eqs
656
  with
657
    Not_found -> (* Shall be defined in automata auts *) raise Not_found
658
      
659
let get_nodes prog = 
660
  List.fold_left (
661
    fun nodes decl ->
662
      match decl.top_decl_desc with
663
	| Node _ -> decl::nodes
664
	| Const _ | ImportedNode _ | Include _ | Open _ | TypeDef _ -> nodes  
665
  ) [] prog
666

    
667
let get_imported_nodes prog = 
668
  List.fold_left (
669
    fun nodes decl ->
670
      match decl.top_decl_desc with
671
	| ImportedNode _ -> decl::nodes
672
	| Const _ | Node _ | Include _ | Open _ | TypeDef _-> nodes  
673
  ) [] prog
674

    
675
let get_consts prog = 
676
  List.fold_right (
677
    fun decl consts ->
678
      match decl.top_decl_desc with
679
	| Const _ -> decl::consts
680
	| Node _ | ImportedNode _ | Include _ | Open _ | TypeDef _ -> consts  
681
  ) prog []
682

    
683
let get_typedefs prog = 
684
  List.fold_right (
685
    fun decl types ->
686
      match decl.top_decl_desc with
687
	| TypeDef _ -> decl::types
688
	| Node _ | ImportedNode _ | Include _ | Open _ | Const _ -> types  
689
  ) prog []
690

    
691
let get_dependencies prog =
692
  List.fold_right (
693
    fun decl deps ->
694
      match decl.top_decl_desc with
695
	| Open _ -> decl::deps
696
	| Node _ | ImportedNode _ | TypeDef _ | Include _ | Const _ -> deps  
697
  ) prog []
698

    
699
let get_node_interface nd =
700
 {nodei_id = nd.node_id;
701
  nodei_type = nd.node_type;
702
  nodei_clock = nd.node_clock;
703
  nodei_inputs = nd.node_inputs;
704
  nodei_outputs = nd.node_outputs;
705
  nodei_stateless = nd.node_dec_stateless;
706
  nodei_spec = nd.node_spec;
707
  (* nodei_annot = nd.node_annot; *)
708
  nodei_prototype = None;
709
  nodei_in_lib = [];
710
 }
711

    
712
(************************************************************************)
713
(*        Renaming / Copying                                                      *)
714

    
715
let copy_var_decl vdecl =
716
  mkvar_decl
717
    vdecl.var_loc
718
    ~orig:vdecl.var_orig
719
    (
720
      vdecl.var_id,
721
      vdecl.var_dec_type,
722
      vdecl.var_dec_clock,
723
      vdecl.var_dec_const,
724
      vdecl.var_dec_value,
725
      vdecl.var_parent_nodeid
726
    )
727

    
728
let copy_const cdecl =
729
  { cdecl with const_type = Types.new_var () }
730

    
731
let copy_node nd =
732
  { nd with
733
    node_type     = Types.new_var ();
734
    node_clock    = Clocks.new_var true;
735
    node_inputs   = List.map copy_var_decl nd.node_inputs;
736
    node_outputs  = List.map copy_var_decl nd.node_outputs;
737
    node_locals   = List.map copy_var_decl nd.node_locals;
738
    node_gencalls = [];
739
    node_checks   = [];
740
    node_stateless = None;
741
  }
742

    
743
let copy_top top =
744
  match top.top_decl_desc with
745
  | Node nd -> { top with top_decl_desc = Node (copy_node nd)  }
746
  | Const c -> { top with top_decl_desc = Const (copy_const c) }
747
  | _       -> top
748

    
749
let copy_prog top_list =
750
  List.map copy_top top_list
751

    
752
  
753
let rec rename_static rename cty =
754
 match cty with
755
 | Tydec_array (d, cty') -> Tydec_array (Dimension.expr_replace_expr rename d, rename_static rename cty')
756
 | Tydec_clock cty       -> Tydec_clock (rename_static rename cty)
757
 | Tydec_struct fl       -> Tydec_struct (List.map (fun (f, cty) -> f, rename_static rename cty) fl)
758
 | _                      -> cty
759

    
760
let rec rename_carrier rename cck =
761
 match cck with
762
 | Ckdec_bool cl -> Ckdec_bool (List.map (fun (c, l) -> rename c, l) cl)
763
 | _             -> cck
764

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

    
767
(* applies the renaming function [fvar] to all variables of expression [expr] *)
768
 (* let rec expr_replace_var fvar expr = *)
769
 (*  { expr with expr_desc = expr_desc_replace_var fvar expr.expr_desc } *)
770

    
771
 (* and expr_desc_replace_var fvar expr_desc = *)
772
 (*   match expr_desc with *)
773
 (*   | Expr_const _ -> expr_desc *)
774
 (*   | Expr_ident i -> Expr_ident (fvar i) *)
775
 (*   | Expr_array el -> Expr_array (List.map (expr_replace_var fvar) el) *)
776
 (*   | Expr_access (e1, d) -> Expr_access (expr_replace_var fvar e1, d) *)
777
 (*   | Expr_power (e1, d) -> Expr_power (expr_replace_var fvar e1, d) *)
778
 (*   | Expr_tuple el -> Expr_tuple (List.map (expr_replace_var fvar) el) *)
779
 (*   | Expr_ite (c, t, e) -> Expr_ite (expr_replace_var fvar c, expr_replace_var fvar t, expr_replace_var fvar e) *)
780
 (*   | Expr_arrow (e1, e2)-> Expr_arrow (expr_replace_var fvar e1, expr_replace_var fvar e2)  *)
781
 (*   | Expr_fby (e1, e2) -> Expr_fby (expr_replace_var fvar e1, expr_replace_var fvar e2) *)
782
 (*   | Expr_pre e' -> Expr_pre (expr_replace_var fvar e') *)
783
 (*   | Expr_when (e', i, l)-> Expr_when (expr_replace_var fvar e', fvar i, l) *)
784
 (*   | Expr_merge (i, hl) -> Expr_merge (fvar i, List.map (fun (t, h) -> (t, expr_replace_var fvar h)) hl) *)
785
 (*   | Expr_appl (i, e', i') -> Expr_appl (i, expr_replace_var fvar e', Utils.option_map (expr_replace_var fvar) i') *)
786

    
787

    
788

    
789
 let rec rename_expr  f_node f_var expr =
790
   { expr with expr_desc = rename_expr_desc f_node f_var expr.expr_desc }
791
 and rename_expr_desc f_node f_var expr_desc =
792
   let re = rename_expr  f_node f_var in
793
   match expr_desc with
794
   | Expr_const _ -> expr_desc
795
   | Expr_ident i -> Expr_ident (f_var i)
796
   | Expr_array el -> Expr_array (List.map re el)
797
   | Expr_access (e1, d) -> Expr_access (re e1, d)
798
   | Expr_power (e1, d) -> Expr_power (re e1, d)
799
   | Expr_tuple el -> Expr_tuple (List.map re el)
800
   | Expr_ite (c, t, e) -> Expr_ite (re c, re t, re e)
801
   | Expr_arrow (e1, e2)-> Expr_arrow (re e1, re e2) 
802
   | Expr_fby (e1, e2) -> Expr_fby (re e1, re e2)
803
   | Expr_pre e' -> Expr_pre (re e')
804
   | Expr_when (e', i, l)-> Expr_when (re e', f_var i, l)
805
   | Expr_merge (i, hl) -> 
806
     Expr_merge (f_var i, List.map (fun (t, h) -> (t, re h)) hl)
807
   | Expr_appl (i, e', i') -> 
808
     Expr_appl (f_node i, re e', Utils.option_map re i')
809
   
810
 let rename_var f_node f_var v = {
811
     (copy_var_decl v) with
812
     var_id = f_var v.var_id;
813
     var_type = v.var_type;
814
     var_clock = v.var_clock;
815
 } 
816

    
817
 let rename_vars f_node f_var = List.map (rename_var f_node f_var) 
818

    
819
 let rec rename_eq f_node f_var eq = { eq with
820
   eq_lhs = List.map f_var eq.eq_lhs; 
821
   eq_rhs = rename_expr f_node f_var eq.eq_rhs
822
 } 
823
 and rename_handler f_node f_var  h = {h with
824
   hand_state = f_var h.hand_state;
825
   hand_unless = List.map (
826
     fun (l,e,b,id) -> l, rename_expr f_node f_var e, b, f_var id
827
   ) h.hand_unless;
828
   hand_until = List.map (
829
     fun (l,e,b,id) -> l, rename_expr f_node f_var e, b, f_var id
830
   ) h.hand_until;
831
   hand_locals = rename_vars f_node f_var h.hand_locals;
832
   hand_stmts = rename_stmts f_node f_var h.hand_stmts;
833
   hand_annots = rename_annots f_node f_var h.hand_annots;
834
   
835
 } 
836
 and rename_aut f_node f_var  aut = { aut with
837
   aut_id = f_var aut.aut_id;
838
   aut_handlers = List.map (rename_handler f_node f_var) aut.aut_handlers;
839
 }
840
 and rename_stmts f_node f_var stmts = List.map (fun stmt -> match stmt with
841
   | Eq eq -> Eq (rename_eq f_node f_var eq)
842
   | Aut at -> Aut (rename_aut f_node f_var at))
843
   stmts
844
 and rename_annotl f_node f_var  annots = 
845
   List.map 
846
     (fun (key, value) -> key, rename_eexpr f_node f_var value) 
847
     annots
848
 and rename_annot f_node f_var annot =
849
   { annot with annots = rename_annotl f_node f_var annot.annots }
850
 and rename_annots f_node f_var annots =
851
   List.map (rename_annot f_node f_var) annots
852
and rename_eexpr f_node f_var ee =
853
   { ee with
854
     eexpr_tag = Utils.new_tag ();
855
     eexpr_qfexpr = rename_expr f_node f_var ee.eexpr_qfexpr;
856
     eexpr_quantifiers = List.map (fun (typ,vdecls) -> typ, rename_vars f_node f_var vdecls) ee.eexpr_quantifiers;
857
   }
858
and rename_mode f_node f_var m =
859
  let rename_ee = rename_eexpr f_node f_var in
860
  {
861
    m with
862
    require = List.map rename_ee m.require;
863
    ensure = List.map rename_ee m.ensure
864
  }
865
     
866
 let rename_import f_node f_var imp =
867
   let rename_expr = rename_expr f_node f_var in
868
   {
869
     imp with
870
     import_nodeid = f_node imp.import_nodeid;
871
     inputs = rename_expr imp.inputs;
872
     outputs =  rename_expr imp.outputs;
873
   }
874
   
875
 let rename_node f_node f_var nd =
876
   let f_var x = (* checking that this is actually a local variable *)
877
     if List.exists (fun v -> v.var_id = x) (get_node_vars nd) then
878
       f_var x
879
     else
880
       x
881
   in
882
   let rename_var = rename_var f_node f_var in
883
   let rename_vars = List.map rename_var in
884
   let rename_expr = rename_expr f_node f_var in
885
   let rename_eexpr = rename_eexpr f_node f_var in
886
   let rename_stmts = rename_stmts f_node f_var in
887
   let inputs = rename_vars nd.node_inputs in
888
   let outputs = rename_vars nd.node_outputs in
889
   let locals = rename_vars nd.node_locals in
890
   let gen_calls = List.map rename_expr nd.node_gencalls in
891
   let node_checks = List.map (Dimension.rename f_node f_var)  nd.node_checks in
892
   let node_asserts = List.map 
893
     (fun a -> 
894
       {a with assert_expr = 
895
	   let expr = a.assert_expr in
896
	   rename_expr expr})
897
     nd.node_asserts
898
   in
899
   let node_stmts = rename_stmts nd.node_stmts
900

    
901
     
902
   in
903
   let spec = 
904
     Utils.option_map 
905
       (fun s -> match s with
906
                   NodeSpec id -> NodeSpec (f_node id)
907
                 | Contract c -> Contract {
908
                     c with
909
                     consts = rename_vars c.consts;
910
                     locals = rename_vars c.locals;
911
                     stmts = rename_stmts c.stmts;
912
                     assume = List.map rename_eexpr c.assume;
913
                     guarantees = List.map rename_eexpr c.guarantees;
914
                     modes = List.map (rename_mode f_node f_var) c.modes;
915
                     imports = List.map (rename_import f_node f_var) c.imports;
916
                   }
917
       )
918
       nd.node_spec 
919
   in
920
   let annot = rename_annots f_node f_var nd.node_annot in
921
   {
922
     node_id = f_node nd.node_id;
923
     node_type = nd.node_type;
924
     node_clock = nd.node_clock;
925
     node_inputs = inputs;
926
     node_outputs = outputs;
927
     node_locals = locals;
928
     node_gencalls = gen_calls;
929
     node_checks = node_checks;
930
     node_asserts = node_asserts;
931
     node_stmts = node_stmts;
932
     node_dec_stateless = nd.node_dec_stateless;
933
     node_stateless = nd.node_stateless;
934
     node_spec = spec;
935
     node_annot = annot;
936
     node_iscontract = nd.node_iscontract;
937
   }
938

    
939

    
940
let rename_const f_const c =
941
  { c with const_id = f_const c.const_id }
942

    
943
let rename_typedef f_var t =
944
  match t.tydef_desc with
945
  | Tydec_enum tags -> { t with tydef_desc = Tydec_enum (List.map f_var tags) }
946
  | _               -> t
947

    
948
let rename_prog f_node f_var f_const prog =
949
  List.rev (
950
    List.fold_left (fun accu top ->
951
      (match top.top_decl_desc with
952
      | Node nd -> 
953
	 { top with top_decl_desc = Node (rename_node f_node f_var nd) }
954
      | Const c -> 
955
	 { top with top_decl_desc = Const (rename_const f_const c) }
956
      | TypeDef tdef ->
957
	 { top with top_decl_desc = TypeDef (rename_typedef f_var tdef) }
958
      | ImportedNode _
959
        | Include _ | Open _       -> top)
960
      ::accu
961
) [] prog
962
		   )
963

    
964
(* Applies the renaming function [fvar] to every rhs
965
   only when the corresponding lhs satisfies predicate [pvar] *)
966
 let eq_replace_rhs_var pvar fvar eq =
967
   let pvar l = List.exists pvar l in
968
   let rec replace lhs rhs =
969
     { rhs with expr_desc =
970
     match lhs with
971
     | []  -> assert false
972
     | [_] -> if pvar lhs then rename_expr_desc (fun x -> x) fvar rhs.expr_desc else rhs.expr_desc
973
     | _   ->
974
       (match rhs.expr_desc with
975
       | Expr_tuple tl ->
976
	 Expr_tuple (List.map2 (fun v e -> replace [v] e) lhs tl)
977
       | Expr_appl (f, arg, None) when Basic_library.is_expr_internal_fun rhs ->
978
	 let args = expr_list_of_expr arg in
979
	 Expr_appl (f, expr_of_expr_list arg.expr_loc (List.map (replace lhs) args), None)
980
       | Expr_array _
981
       | Expr_access _
982
       | Expr_power _
983
       | Expr_const _
984
       | Expr_ident _
985
       | Expr_appl _   ->
986
	 if pvar lhs
987
	 then rename_expr_desc (fun x -> x) fvar rhs.expr_desc
988
	 else rhs.expr_desc
989
       | Expr_ite (c, t, e)   -> Expr_ite (replace lhs c, replace lhs t, replace lhs e)
990
       | Expr_arrow (e1, e2)  -> Expr_arrow (replace lhs e1, replace lhs e2) 
991
       | Expr_fby (e1, e2)    -> Expr_fby (replace lhs e1, replace lhs e2)
992
       | Expr_pre e'          -> Expr_pre (replace lhs e')
993
       | Expr_when (e', i, l) -> let i' = if pvar lhs then fvar i else i
994
				 in Expr_when (replace lhs e', i', l)
995
       | Expr_merge (i, hl)   -> let i' = if pvar lhs then fvar i else i
996
				 in Expr_merge (i', List.map (fun (t, h) -> (t, replace lhs h)) hl)
997
       )
998
     }
999
   in { eq with eq_rhs = replace eq.eq_lhs eq.eq_rhs }
1000

    
1001
    
1002
(**********************************************************************)
1003
(* Pretty printers *)
1004

    
1005
let pp_decl_type fmt tdecl =
1006
  match tdecl.top_decl_desc with
1007
  | Node nd ->
1008
    fprintf fmt "%s: " nd.node_id;
1009
    Utils.reset_names ();
1010
    fprintf fmt "%a@ " Types.print_ty nd.node_type
1011
  | ImportedNode ind ->
1012
    fprintf fmt "%s: " ind.nodei_id;
1013
    Utils.reset_names ();
1014
    fprintf fmt "%a@ " Types.print_ty ind.nodei_type
1015
  | Const _ | Include _ | Open _ | TypeDef _ -> ()
1016

    
1017
let pp_prog_type fmt tdecl_list =
1018
  Utils.fprintf_list ~sep:"" pp_decl_type fmt tdecl_list
1019

    
1020
let pp_decl_clock fmt cdecl =
1021
  match cdecl.top_decl_desc with
1022
  | Node nd ->
1023
    fprintf fmt "%s: " nd.node_id;
1024
    Utils.reset_names ();
1025
    fprintf fmt "%a@ " Clocks.print_ck nd.node_clock
1026
  | ImportedNode ind ->
1027
    fprintf fmt "%s: " ind.nodei_id;
1028
    Utils.reset_names ();
1029
    fprintf fmt "%a@ " Clocks.print_ck ind.nodei_clock
1030
  | Const _ | Include _ | Open _ | TypeDef _ -> ()
1031

    
1032
let pp_prog_clock fmt prog =
1033
  Utils.fprintf_list ~sep:"" pp_decl_clock fmt prog
1034

    
1035

    
1036
(* filling node table with internal functions *)
1037
let vdecls_of_typ_ck cpt ty =
1038
  let loc = Location.dummy_loc in
1039
  List.map
1040
    (fun _ -> incr cpt;
1041
              let name = sprintf "_var_%d" !cpt in
1042
              mkvar_decl loc (name, mktyp loc Tydec_any, mkclock loc Ckdec_any, false, None, None))
1043
    (Types.type_list_of_type ty)
1044

    
1045
let mk_internal_node id =
1046
  let spec = None in
1047
  let ty = Env.lookup_value Basic_library.type_env id in
1048
  let ck = Env.lookup_value Basic_library.clock_env id in
1049
  let (tin, tout) = Types.split_arrow ty in
1050
  (*eprintf "internal fun %s: %d -> %d@." id (List.length (Types.type_list_of_type tin)) (List.length (Types.type_list_of_type tout));*)
1051
  let cpt = ref (-1) in
1052
  mktop
1053
    (ImportedNode
1054
       {nodei_id = id;
1055
	nodei_type = ty;
1056
	nodei_clock = ck;
1057
	nodei_inputs = vdecls_of_typ_ck cpt tin;
1058
	nodei_outputs = vdecls_of_typ_ck cpt tout;
1059
	nodei_stateless = Types.get_static_value ty <> None;
1060
	nodei_spec = spec;
1061
	(* nodei_annot = []; *)
1062
	nodei_prototype = None;
1063
       	nodei_in_lib = [];
1064
       })
1065

    
1066
let add_internal_funs () =
1067
  List.iter
1068
    (fun id -> let nd = mk_internal_node id in Hashtbl.add node_table id nd)
1069
    Basic_library.internal_funs
1070

    
1071

    
1072

    
1073
(* Replace any occurence of a var in vars_to_replace by its associated
1074
   expression in defs until e does not contain any such variables *)
1075
let rec substitute_expr vars_to_replace defs e =
1076
  let se = substitute_expr vars_to_replace defs in
1077
  { e with expr_desc = 
1078
      let ed = e.expr_desc in
1079
      match ed with
1080
      | Expr_const _ -> ed
1081
      | Expr_array el -> Expr_array (List.map se el)
1082
      | Expr_access (e1, d) -> Expr_access (se e1, d)
1083
      | Expr_power (e1, d) -> Expr_power (se e1, d)
1084
      | Expr_tuple el -> Expr_tuple (List.map se el)
1085
      | Expr_ite (c, t, e) -> Expr_ite (se c, se t, se e)
1086
      | Expr_arrow (e1, e2)-> Expr_arrow (se e1, se e2) 
1087
      | Expr_fby (e1, e2) -> Expr_fby (se e1, se e2)
1088
      | Expr_pre e' -> Expr_pre (se e')
1089
      | Expr_when (e', i, l)-> Expr_when (se e', i, l)
1090
      | Expr_merge (i, hl) -> Expr_merge (i, List.map (fun (t, h) -> (t, se h)) hl)
1091
      | Expr_appl (i, e', i') -> Expr_appl (i, se e', i')
1092
      | Expr_ident i -> 
1093
	if List.exists (fun v -> v.var_id = i) vars_to_replace then (
1094
	  let eq_i eq = eq.eq_lhs = [i] in
1095
	  if List.exists eq_i defs then
1096
	    let sub = List.find eq_i defs in
1097
	    let sub' = se sub.eq_rhs in
1098
	    sub'.expr_desc
1099
	  else 
1100
	    assert false
1101
	)
1102
	else
1103
	  ed
1104

    
1105
  }
1106
  
1107
 let rec expr_to_eexpr  expr =
1108
   { eexpr_tag = expr.expr_tag;
1109
     eexpr_qfexpr = expr;
1110
     eexpr_quantifiers = [];
1111
     eexpr_name = None;
1112
     eexpr_type = expr.expr_type;
1113
     eexpr_clock = expr.expr_clock;
1114
     eexpr_loc = expr.expr_loc;
1115
     (*eexpr_normalized = None*)
1116
   }
1117
 (* and expr_desc_to_eexpr_desc expr_desc = *)
1118
 (*   let conv = expr_to_eexpr in *)
1119
 (*   match expr_desc with *)
1120
 (*   | Expr_const c -> EExpr_const (match c with *)
1121
 (*     | Const_int x -> EConst_int x  *)
1122
 (*     | Const_real x -> EConst_real x  *)
1123
 (*     | Const_float x -> EConst_float x  *)
1124
 (*     | Const_tag x -> EConst_tag x  *)
1125
 (*     | _ -> assert false *)
1126

    
1127
 (*   ) *)
1128
 (*   | Expr_ident i -> EExpr_ident i *)
1129
 (*   | Expr_tuple el -> EExpr_tuple (List.map conv el) *)
1130

    
1131
 (*   | Expr_arrow (e1, e2)-> EExpr_arrow (conv e1, conv e2)  *)
1132
 (*   | Expr_fby (e1, e2) -> EExpr_fby (conv e1, conv e2) *)
1133
 (*   | Expr_pre e' -> EExpr_pre (conv e') *)
1134
 (*   | Expr_appl (i, e', i') ->  *)
1135
 (*     EExpr_appl  *)
1136
 (*       (i, conv e', match i' with None -> None | Some(id, _) -> Some id) *)
1137

    
1138
 (*   | Expr_when _ *)
1139
 (*   | Expr_merge _ -> assert false *)
1140
 (*   | Expr_array _  *)
1141
 (*   | Expr_access _  *)
1142
 (*   | Expr_power _  -> assert false *)
1143
 (*   | Expr_ite (c, t, e) -> assert false  *)
1144
 (*   | _ -> assert false *)
1145
      
1146
     
1147
let rec get_expr_calls nodes e =
1148
  let get_calls = get_expr_calls nodes in
1149
  match e.expr_desc with
1150
  | Expr_const _ 
1151
   | Expr_ident _ -> Utils.ISet.empty
1152
   | Expr_tuple el
1153
   | Expr_array el -> List.fold_left (fun accu e -> Utils.ISet.union accu (get_calls e)) Utils.ISet.empty el
1154
   | Expr_pre e1 
1155
   | Expr_when (e1, _, _) 
1156
   | Expr_access (e1, _) 
1157
   | Expr_power (e1, _) -> get_calls e1
1158
   | Expr_ite (c, t, e) -> Utils.ISet.union (Utils.ISet.union (get_calls c) (get_calls t)) (get_calls e) 
1159
   | Expr_arrow (e1, e2) 
1160
   | Expr_fby (e1, e2) -> Utils.ISet.union (get_calls e1) (get_calls e2)
1161
   | Expr_merge (_, hl) -> List.fold_left (fun accu (_, h) -> Utils.ISet.union accu (get_calls h)) Utils.ISet.empty  hl
1162
   | Expr_appl (i, e', i') -> 
1163
     if Basic_library.is_expr_internal_fun e then 
1164
       (get_calls e') 
1165
     else
1166
       let calls =  Utils.ISet.add i (get_calls e') in
1167
       let test = (fun n -> match n.top_decl_desc with Node nd -> nd.node_id = i | _ -> false) in
1168
       if List.exists test nodes then
1169
	 match (List.find test nodes).top_decl_desc with
1170
	 | Node nd -> Utils.ISet.union (get_node_calls nodes nd) calls
1171
	 | _ -> assert false
1172
       else 
1173
	 calls
1174

    
1175
and get_eq_calls nodes eq =
1176
  get_expr_calls nodes eq.eq_rhs
1177
and get_aut_handler_calls nodes h =
1178
  List.fold_left (fun accu stmt -> match stmt with
1179
  | Eq eq -> Utils.ISet.union (get_eq_calls nodes eq) accu
1180
  | Aut aut' ->  Utils.ISet.union (get_aut_calls nodes aut') accu
1181
  ) Utils.ISet.empty h.hand_stmts 
1182
and get_aut_calls nodes aut =
1183
  List.fold_left (fun accu h -> Utils.ISet.union (get_aut_handler_calls nodes h) accu)
1184
    Utils.ISet.empty aut.aut_handlers
1185
and get_node_calls nodes node =
1186
  let eqs, auts = get_node_eqs node in
1187
  let aut_calls =
1188
    List.fold_left
1189
      (fun accu aut -> Utils.ISet.union (get_aut_calls nodes aut) accu)
1190
      Utils.ISet.empty auts
1191
  in
1192
  List.fold_left
1193
    (fun accu eq -> Utils.ISet.union (get_eq_calls nodes eq) accu)
1194
    aut_calls eqs
1195

    
1196
let get_expr_vars e =
1197
  let rec get_expr_vars vars e =
1198
    get_expr_desc_vars vars e.expr_desc
1199
  and get_expr_desc_vars vars expr_desc =
1200
    (*Format.eprintf "get_expr_desc_vars expr=%a@." Printers.pp_expr (mkexpr Location.dummy_loc expr_desc);*)
1201
  match expr_desc with
1202
  | Expr_const _ -> vars
1203
  | Expr_ident x -> Utils.ISet.add x vars
1204
  | Expr_tuple el
1205
  | Expr_array el -> List.fold_left get_expr_vars vars el
1206
  | Expr_pre e1 -> get_expr_vars vars e1
1207
  | Expr_when (e1, c, _) -> get_expr_vars (Utils.ISet.add c vars) e1 
1208
  | Expr_access (e1, d) 
1209
  | Expr_power (e1, d)   -> List.fold_left get_expr_vars vars [e1; expr_of_dimension d]
1210
  | Expr_ite (c, t, e) -> List.fold_left get_expr_vars vars [c; t; e]
1211
  | Expr_arrow (e1, e2) 
1212
  | Expr_fby (e1, e2) -> List.fold_left get_expr_vars vars [e1; e2]
1213
  | Expr_merge (c, hl) -> List.fold_left (fun vars (_, h) -> get_expr_vars vars h) (Utils.ISet.add c vars) hl
1214
  | Expr_appl (_, arg, None)   -> get_expr_vars vars arg
1215
  | Expr_appl (_, arg, Some r) -> List.fold_left get_expr_vars vars [arg; r]
1216
  in
1217
  get_expr_vars Utils.ISet.empty e 
1218

    
1219
let rec expr_has_arrows e =
1220
  expr_desc_has_arrows e.expr_desc
1221
and expr_desc_has_arrows expr_desc =
1222
  match expr_desc with
1223
  | Expr_const _ 
1224
  | Expr_ident _ -> false
1225
  | Expr_tuple el
1226
  | Expr_array el -> List.exists expr_has_arrows el
1227
  | Expr_pre e1 
1228
  | Expr_when (e1, _, _) 
1229
  | Expr_access (e1, _) 
1230
  | Expr_power (e1, _) -> expr_has_arrows e1
1231
  | Expr_ite (c, t, e) -> List.exists expr_has_arrows [c; t; e]
1232
  | Expr_arrow (e1, e2) 
1233
  | Expr_fby (e1, e2) -> true
1234
  | Expr_merge (_, hl) -> List.exists (fun (_, h) -> expr_has_arrows h) hl
1235
  | Expr_appl (i, e', i') -> expr_has_arrows e'
1236

    
1237
and eq_has_arrows eq =
1238
  expr_has_arrows eq.eq_rhs
1239
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 
1240
and node_has_arrows node =
1241
  let eqs, auts = get_node_eqs node in
1242
  List.exists (fun eq -> eq_has_arrows eq) eqs || List.exists (fun aut -> aut_has_arrows aut) auts
1243

    
1244

    
1245

    
1246

    
1247

    
1248
let rec expr_contains_expr expr_tag expr  =
1249
  let search = expr_contains_expr expr_tag in
1250
  expr.expr_tag = expr_tag ||
1251
      (
1252
	match expr.expr_desc with
1253
	| Expr_const _ -> false
1254
	| Expr_array el -> List.exists search el
1255
	| Expr_access (e1, _) 
1256
	| Expr_power (e1, _) -> search e1
1257
	| Expr_tuple el -> List.exists search el
1258
	| Expr_ite (c, t, e) -> List.exists search [c;t;e]
1259
	| Expr_arrow (e1, e2)
1260
	| Expr_fby (e1, e2) -> List.exists search [e1; e2]
1261
	| Expr_pre e' 
1262
	| Expr_when (e', _, _) -> search e'
1263
	| Expr_merge (_, hl) -> List.exists (fun (_, h) -> search h) hl
1264
	| Expr_appl (_, e', None) -> search e' 
1265
	| Expr_appl (_, e', Some e'') -> List.exists search [e'; e''] 
1266
	| Expr_ident _ -> false
1267
      )
1268

    
1269

    
1270

    
1271
(* Generate a new local [node] variable *)
1272
let cpt_fresh = ref 0
1273

    
1274
let reset_cpt_fresh () =
1275
    cpt_fresh := 0
1276
    
1277
let mk_fresh_var (parentid, ctx_env) loc ty ck =
1278
  let rec aux () =
1279
  incr cpt_fresh;
1280
  let s = Printf.sprintf "__%s_%d" parentid !cpt_fresh in
1281
  if List.exists (fun v -> v.var_id = s) ctx_env then aux () else
1282
  {
1283
    var_id = s;
1284
    var_orig = false;
1285
    var_dec_type = dummy_type_dec;
1286
    var_dec_clock = dummy_clock_dec;
1287
    var_dec_const = false;
1288
    var_dec_value = None;
1289
    var_parent_nodeid = Some parentid;
1290
    var_type = ty;
1291
    var_clock = ck;
1292
    var_loc = loc
1293
  }
1294
  in aux ()
1295

    
1296

    
1297
let find_eq xl eqs =
1298
  let rec aux accu eqs =
1299
    match eqs with
1300
	| [] ->
1301
	  begin
1302
	    Format.eprintf "Looking for variables %a in the following equations@.%a@."
1303
	      (Utils.fprintf_list ~sep:" , " (fun fmt v -> Format.fprintf fmt "%s" v)) xl
1304
	      Printers.pp_node_eqs eqs;
1305
	    assert false
1306
	  end
1307
	| hd::tl ->
1308
	  if List.exists (fun x -> List.mem x hd.eq_lhs) xl then hd, accu@tl else aux (hd::accu) tl
1309
    in
1310
    aux [] eqs
1311

    
1312
       
1313
let get_node name prog =
1314
  let node_opt = List.fold_left
1315
    (fun res top -> 
1316
      match res, top.top_decl_desc with
1317
      | Some _, _ -> res
1318
      | None, Node nd -> 
1319
	(* Format.eprintf "Checking node %s = %s: %b@." nd.node_id name (nd.node_id = name); *)
1320
	if nd.node_id = name then Some nd else res
1321
      | _ -> None) 
1322
    None prog 
1323
  in
1324
  try 
1325
    Utils.desome node_opt
1326
  with Utils.DeSome -> raise Not_found
1327

    
1328
(* Pushing negations in expression. Subtitute operators whenever possible *)
1329
let rec push_negations ?(neg=false) e =
1330
  let res =
1331
    let pn = push_negations in
1332
    let map desc =
1333
      (* Keeping clock and type info *)
1334
      let new_e = mkexpr e.expr_loc desc in
1335
      {
1336
        new_e
1337
      with
1338
        expr_type = e.expr_type;
1339
        expr_clock = e.expr_clock
1340
      }
1341
    in
1342
    match e.expr_desc with
1343
    | Expr_ite (g,t,e) ->
1344
       if neg then
1345
         map (Expr_ite(pn g, pn e, pn t))
1346
       else
1347
         map (Expr_ite(pn g, pn t, pn e)) 
1348
    | Expr_tuple t ->
1349
       map (Expr_tuple (List.map (pn ~neg) t))
1350
    | Expr_arrow (e1, e2) ->
1351
       map (Expr_arrow (pn ~neg e1, pn ~neg e2)) 
1352
    | Expr_fby (e1, e2) ->
1353
       map (Expr_fby (pn ~neg e1, pn ~neg e2))
1354
    | Expr_pre e ->
1355
       map (Expr_pre (pn ~neg e))
1356
    | Expr_appl (op, e', None) when op = "not" ->
1357
       if neg then
1358
         push_negations ~neg:false e'
1359
       else
1360
         push_negations ~neg:true e'
1361
    | Expr_appl (op, e', None) when List.mem op (Basic_library.bool_funs @ Basic_library.rel_funs) -> (
1362
      match op with
1363
      | "&&" -> map (Expr_appl((if neg then "||" else op), pn ~neg e', None))
1364
      | "||" -> map (Expr_appl((if neg then "&&" else op), pn ~neg e', None))
1365
      (* TODO xor/equi/impl *)
1366
      | "<" -> map (Expr_appl((if neg then ">=" else op), pn e', None))
1367
      | ">" -> map (Expr_appl((if neg then "<=" else op), pn e', None))
1368
      | "<=" -> map (Expr_appl((if neg then ">" else op), pn e', None))
1369
      | ">=" -> map (Expr_appl((if neg then "<" else op), pn e', None))
1370
      | "!=" -> map (Expr_appl((if neg then "=" else op), pn e', None))
1371
      | "=" -> map (Expr_appl((if neg then "!=" else op), pn e', None))
1372
             
1373
      | _ -> assert false                     
1374
    )
1375
    | Expr_const c -> if neg then map (Expr_const (const_negation c)) else e
1376
    | Expr_ident _ -> 
1377
       if neg then
1378
                         mkpredef_call e.expr_loc "not" [e]
1379
                       else
1380
                         e
1381
    | Expr_appl (op,_,_) -> 
1382
       if neg then
1383
         mkpredef_call e.expr_loc "not" [e]
1384
       else
1385
         e
1386
    | _ -> assert false (* no array, array access, power or merge/when *)
1387
  in
1388
  res
1389

    
1390
let rec add_pre_expr vars e =
1391
  let ap = add_pre_expr vars in
1392
  let desc =
1393
    match e.expr_desc with
1394
    | Expr_ite (g,t,e) ->
1395
       Expr_ite (ap g, ap t,ap e)
1396
    | Expr_tuple t ->
1397
       Expr_tuple (List.map ap t)
1398
    | Expr_arrow (e1, e2) ->
1399
       Expr_arrow (ap e1, ap e2) 
1400
    | Expr_fby (e1, e2) ->
1401
       Expr_fby (ap e1, ap e2)
1402
    | Expr_pre e ->
1403
       Expr_pre (ap e)
1404
    | Expr_appl (op, e, opt) ->
1405
       Expr_appl (op, ap e, opt)
1406
    | Expr_const _ -> e.expr_desc 
1407
    | Expr_ident id ->
1408
       if List.mem id vars then
1409
         Expr_pre e
1410
       else
1411
         e.expr_desc
1412
    | _ -> assert false (* no array, array access, power or merge/when yet *)
1413
  in
1414
  let new_e = mkexpr e.expr_loc desc in
1415
  { new_e with
1416
    expr_type = e.expr_type;
1417
    expr_clock = e.expr_clock
1418
  }
1419

    
1420

    
1421
        
1422
let mk_eq l e1 e2 =
1423
  mkpredef_call l "=" [e1; e2]
1424

    
1425

    
1426
let rec partial_eval e =
1427
  let pa = partial_eval in
1428
  let edesc =
1429
    match e.expr_desc with
1430
    | Expr_const _ -> e.expr_desc 
1431
    | Expr_ident id -> e.expr_desc
1432
    | Expr_ite (g,t,e) -> (
1433
       let g, t, e = pa g, pa t, pa e in
1434
       match g.expr_desc with
1435
       | Expr_const (Const_tag tag) when (tag = tag_true) -> t.expr_desc
1436
       | Expr_const (Const_tag tag) when (tag = tag_false) -> e.expr_desc
1437
       | _ -> Expr_ite (g, t, e)
1438
    )
1439
    | Expr_tuple t ->
1440
       Expr_tuple (List.map pa t)
1441
    | Expr_arrow (e1, e2) ->
1442
       Expr_arrow (pa e1, pa e2) 
1443
    | Expr_fby (e1, e2) ->
1444
       Expr_fby (pa e1, pa e2)
1445
    | Expr_pre e ->
1446
       Expr_pre (pa e)
1447
    | Expr_appl (op, args, opt) ->
1448
       let args = pa args in
1449
       if Basic_library.is_expr_internal_fun e then
1450
         Basic_library.partial_eval op args opt
1451
       else
1452
         Expr_appl (op, args, opt)
1453
    | Expr_array el ->
1454
       Expr_array (List.map pa el)
1455
    | Expr_access (e, d) ->
1456
       Expr_access (pa e, d)
1457
    | Expr_power (e, d) ->
1458
       Expr_power (pa e, d)
1459
    | Expr_when (e, id, l) ->
1460
       Expr_when (pa e, id, l)
1461
    | Expr_merge (id, gl) -> 
1462
       Expr_merge(id, List.map (fun (l, e) -> l, pa e) gl)
1463
  in
1464
  { e with expr_desc = edesc }
1465

    
1466
    (* Local Variables: *)
1467
    (* compile-command:"make -C .." *)
1468
    (* End: *)
(16-16/69)