Project

General

Profile

Statistics
| Branch: | Tag: | Revision:

lustrec / src / corelang.ml @ 1bff14ac

History | View | Annotate | Download (35.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 LustreSpec
14
open Dimension
15

    
16

    
17
exception Error of Location.t * error
18

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

    
25
module VMap = Map.Make(VDeclModule)
26

    
27
module VSet = Set.Make(VDeclModule)
28

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

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

    
33

    
34

    
35
(************************************************************)
36
(* *)
37

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

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

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

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

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

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

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

    
88
let mkassert loc expr =
89
  { assert_loc = loc;
90
    assert_expr = expr
91
  }
92

    
93
let mktop_decl loc own itf d =
94
  { top_decl_desc = d; top_decl_loc = loc; top_decl_owner = own; top_decl_itf = itf }
95

    
96
let mkpredef_call loc funname args =
97
  mkexpr loc (Expr_appl (funname, mkexpr loc (Expr_tuple args), None))
98

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

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

    
109
let node_of_top top_decl =
110
  match top_decl.top_decl_desc with
111
  | Node nd -> nd
112
  | _ -> assert false
113

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

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

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

    
129
let consts_of_enum_type top_decl =
130
  match top_decl.top_decl_desc with
131
  | TypeDef tdef ->
132
    (match tdef.tydef_desc with
133
     | Tydec_enum tags -> List.map (fun tag -> let cdecl = { const_id = tag; const_loc = top_decl.top_decl_loc; const_value = Const_tag tag; const_type = Type_predef.type_const tdef.tydef_id } in { top_decl with top_decl_desc = Const cdecl }) tags
134
     | _               -> [])
135
  | _ -> assert false
136

    
137
(************************************************************)
138
(*   Eexpr functions *)
139
(************************************************************)
140

    
141
let merge_node_annot ann1 ann2 =
142
  { requires = ann1.requires @ ann2.requires;
143
    ensures = ann1.ensures @ ann2.ensures;
144
    behaviors = ann1.behaviors @ ann2.behaviors;
145
    spec_loc = ann1.spec_loc
146
  }
147

    
148
let mkeexpr loc expr =
149
  { eexpr_tag = Utils.new_tag ();
150
    eexpr_qfexpr = expr;
151
    eexpr_quantifiers = [];
152
    eexpr_type = Types.new_var ();
153
    eexpr_clock = Clocks.new_var true;
154
    eexpr_normalized = None;
155
    eexpr_loc = loc }
156

    
157
let extend_eexpr q e = { e with eexpr_quantifiers = q@e.eexpr_quantifiers }
158

    
159
(*
160
let mkepredef_call loc funname args =
161
  mkeexpr loc (EExpr_appl (funname, mkeexpr loc (EExpr_tuple args), None))
162

    
163
let mkepredef_unary_call loc funname arg =
164
  mkeexpr loc (EExpr_appl (funname, arg, None))
165
*)
166

    
167
let merge_expr_annot ann1 ann2 =
168
  match ann1, ann2 with
169
    | None, None -> assert false
170
    | Some _, None -> ann1
171
    | None, Some _ -> ann2
172
    | Some ann1, Some ann2 -> Some {
173
      annots = ann1.annots @ ann2.annots;
174
      annot_loc = ann1.annot_loc
175
    }
176

    
177
let update_expr_annot node_id e annot =
178
  List.iter (fun (key, _) -> 
179
    Annotations.add_expr_ann node_id e.expr_tag key
180
  ) annot.annots;
181
  { e with expr_annot = merge_expr_annot e.expr_annot (Some annot) }
182

    
183

    
184
let mkinstr ?(lustre_expr=None) ?(lustre_eq=None) i =
185
  {
186
    instr_desc = i;
187
    (* lustre_expr = lustre_expr; *)
188
    lustre_eq = lustre_eq;
189
  }
190

    
191
let get_instr_desc i = i.instr_desc
192
let update_instr_desc i id = { i with instr_desc = id }
193

    
194
(***********************************************************)
195
(* Fast access to nodes, by name *)
196
let (node_table : (ident, top_decl) Hashtbl.t) = Hashtbl.create 30
197
let consts_table = Hashtbl.create 30
198

    
199
let print_node_table fmt () =
200
  begin
201
    Format.fprintf fmt "{ /* node table */@.";
202
    Hashtbl.iter (fun id nd ->
203
      Format.fprintf fmt "%s |-> %a"
204
	id
205
	Printers.pp_short_decl nd
206
    ) node_table;
207
    Format.fprintf fmt "}@."
208
  end
209

    
210
let print_consts_table fmt () =
211
  begin
212
    Format.fprintf fmt "{ /* consts table */@.";
213
    Hashtbl.iter (fun id const ->
214
      Format.fprintf fmt "%s |-> %a"
215
	id
216
	Printers.pp_const_decl (const_of_top const)
217
    ) consts_table;
218
    Format.fprintf fmt "}@."
219
  end
220

    
221
let node_name td =
222
    match td.top_decl_desc with 
223
    | Node nd         -> nd.node_id
224
    | ImportedNode nd -> nd.nodei_id
225
    | _ -> assert false
226

    
227
let is_generic_node td =
228
  match td.top_decl_desc with 
229
  | Node nd         -> List.exists (fun v -> v.var_dec_const) nd.node_inputs
230
  | ImportedNode nd -> List.exists (fun v -> v.var_dec_const) nd.nodei_inputs
231
  | _ -> assert false
232

    
233
let node_inputs td =
234
  match td.top_decl_desc with 
235
  | Node nd         -> nd.node_inputs
236
  | ImportedNode nd -> nd.nodei_inputs
237
  | _ -> assert false
238

    
239
let node_from_name id =
240
  try
241
    Hashtbl.find node_table id
242
  with Not_found -> (Format.eprintf "Unable to find any node named %s@ @?" id;
243
		     assert false)
244

    
245
let is_imported_node td =
246
  match td.top_decl_desc with 
247
  | Node nd         -> false
248
  | ImportedNode nd -> true
249
  | _ -> assert false
250

    
251

    
252
(* alias and type definition table *)
253

    
254
let mktop = mktop_decl Location.dummy_loc !Options.dest_dir false
255

    
256
let top_int_type = mktop (TypeDef {tydef_id = "int"; tydef_desc = Tydec_int})
257
let top_bool_type = mktop (TypeDef {tydef_id = "bool"; tydef_desc = Tydec_bool})
258
(* let top_float_type = mktop (TypeDef {tydef_id = "float"; tydef_desc = Tydec_float}) *)
259
let top_real_type = mktop (TypeDef {tydef_id = "real"; tydef_desc = Tydec_real})
260

    
261
let type_table =
262
  Utils.create_hashtable 20 [
263
    Tydec_int  , top_int_type;
264
    Tydec_bool , top_bool_type;
265
    (* Tydec_float, top_float_type; *)
266
    Tydec_real , top_real_type
267
  ]
268

    
269
let print_type_table fmt () =
270
  begin
271
    Format.fprintf fmt "{ /* type table */@.";
272
    Hashtbl.iter (fun tydec tdef ->
273
      Format.fprintf fmt "%a |-> %a"
274
	Printers.pp_var_type_dec_desc tydec
275
	Printers.pp_typedef (typedef_of_top tdef)
276
    ) type_table;
277
    Format.fprintf fmt "}@."
278
  end
279

    
280
let rec is_user_type typ =
281
  match typ with
282
  | Tydec_int | Tydec_bool | Tydec_real 
283
  (* | Tydec_float *) | Tydec_any | Tydec_const _ -> false
284
  | Tydec_clock typ' -> is_user_type typ'
285
  | _ -> true
286

    
287
let get_repr_type typ =
288
  let typ_def = (typedef_of_top (Hashtbl.find type_table typ)).tydef_desc in
289
  if is_user_type typ_def then typ else typ_def
290

    
291
let rec coretype_equal ty1 ty2 =
292
  let res =
293
  match ty1, ty2 with
294
  | Tydec_any           , _
295
  | _                   , Tydec_any             -> assert false
296
  | Tydec_const _       , Tydec_const _         -> get_repr_type ty1 = get_repr_type ty2
297
  | Tydec_const _       , _                     -> let ty1' = (typedef_of_top (Hashtbl.find type_table ty1)).tydef_desc
298
	       					   in (not (is_user_type ty1')) && coretype_equal ty1' ty2
299
  | _                   , Tydec_const _         -> coretype_equal ty2 ty1
300
  | Tydec_int           , Tydec_int
301
  | Tydec_real          , Tydec_real
302
  (* | Tydec_float         , Tydec_float *)
303
  | Tydec_bool          , Tydec_bool            -> true
304
  | Tydec_clock ty1     , Tydec_clock ty2       -> coretype_equal ty1 ty2
305
  | Tydec_array (d1,ty1), Tydec_array (d2, ty2) -> Dimension.is_eq_dimension d1 d2 && coretype_equal ty1 ty2
306
  | Tydec_enum tl1      , Tydec_enum tl2        -> List.sort compare tl1 = List.sort compare tl2
307
  | Tydec_struct fl1    , Tydec_struct fl2      ->
308
       List.length fl1 = List.length fl2
309
    && List.for_all2 (fun (f1, t1) (f2, t2) -> f1 = f2 && coretype_equal t1 t2)
310
      (List.sort (fun (f1,_) (f2,_) -> compare f1 f2) fl1)
311
      (List.sort (fun (f1,_) (f2,_) -> compare f1 f2) fl2)
312
  | _                                  -> false
313
  in ((*Format.eprintf "coretype_equal %a %a = %B@." Printers.pp_var_type_dec_desc ty1 Printers.pp_var_type_dec_desc ty2 res;*) res)
314

    
315
let tag_true = "true"
316
let tag_false = "false"
317
let tag_default = "default"
318

    
319
let const_is_bool c =
320
 match c with
321
 | Const_tag t -> t = tag_true || t = tag_false
322
 | _           -> false
323

    
324
(* Computes the negation of a boolean constant *)
325
let const_negation c =
326
  assert (const_is_bool c);
327
  match c with
328
  | Const_tag t when t = tag_true  -> Const_tag tag_false
329
  | _                              -> Const_tag tag_true
330

    
331
let const_or c1 c2 =
332
  assert (const_is_bool c1 && const_is_bool c2);
333
  match c1, c2 with
334
  | Const_tag t1, _            when t1 = tag_true -> c1
335
  | _           , Const_tag t2 when t2 = tag_true -> c2
336
  | _                                             -> Const_tag tag_false
337

    
338
let const_and c1 c2 =
339
  assert (const_is_bool c1 && const_is_bool c2);
340
  match c1, c2 with
341
  | Const_tag t1, _            when t1 = tag_false -> c1
342
  | _           , Const_tag t2 when t2 = tag_false -> c2
343
  | _                                              -> Const_tag tag_true
344

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

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

    
358
(* To guarantee uniqueness of tags in enum types *)
359
let tag_table =
360
  Utils.create_hashtable 20 [
361
   tag_true, top_bool_type;
362
   tag_false, top_bool_type
363
  ]
364

    
365
(* To guarantee uniqueness of fields in struct types *)
366
let field_table =
367
  Utils.create_hashtable 20 [
368
  ]
369

    
370
let get_enum_type_tags cty =
371
(*Format.eprintf "get_enum_type_tags %a@." Printers.pp_var_type_dec_desc cty;*)
372
 match cty with
373
 | Tydec_bool    -> [tag_true; tag_false]
374
 | Tydec_const _ -> (match (typedef_of_top (Hashtbl.find type_table cty)).tydef_desc with
375
                     | Tydec_enum tl -> tl
376
                     | _             -> assert false)
377
 | _            -> assert false
378

    
379
let get_struct_type_fields cty =
380
 match cty with
381
 | Tydec_const _ -> (match (typedef_of_top (Hashtbl.find type_table cty)).tydef_desc with
382
                     | Tydec_struct fl -> fl
383
                     | _               -> assert false)
384
 | _            -> assert false
385

    
386
let const_of_bool b =
387
 Const_tag (if b then tag_true else tag_false)
388

    
389
(* let get_const c = snd (Hashtbl.find consts_table c) *)
390

    
391
let ident_of_expr expr =
392
 match expr.expr_desc with
393
 | Expr_ident id -> id
394
 | _             -> assert false
395

    
396
(* Generate a new ident expression from a declared variable *)
397
let expr_of_vdecl v =
398
  { expr_tag = Utils.new_tag ();
399
    expr_desc = Expr_ident v.var_id;
400
    expr_type = v.var_type;
401
    expr_clock = v.var_clock;
402
    expr_delay = Delay.new_var ();
403
    expr_annot = None;
404
    expr_loc = v.var_loc }
405

    
406
(* Caution, returns an untyped and unclocked expression *)
407
let expr_of_ident id loc =
408
  {expr_tag = Utils.new_tag ();
409
   expr_desc = Expr_ident id;
410
   expr_type = Types.new_var ();
411
   expr_clock = Clocks.new_var true;
412
   expr_delay = Delay.new_var ();
413
   expr_loc = loc;
414
   expr_annot = None}
415

    
416
let is_tuple_expr expr =
417
 match expr.expr_desc with
418
  | Expr_tuple _ -> true
419
  | _            -> false
420

    
421
let expr_list_of_expr expr =
422
  match expr.expr_desc with
423
  | Expr_tuple elist -> elist
424
  | _                -> [expr]
425

    
426
let expr_of_expr_list loc elist =
427
 match elist with
428
 | [t]  -> { t with expr_loc = loc }
429
 | t::_ ->
430
    let tlist = List.map (fun e -> e.expr_type) elist in
431
    let clist = List.map (fun e -> e.expr_clock) elist in
432
    { t with expr_desc = Expr_tuple elist;
433
	     expr_type = Type_predef.type_tuple tlist;
434
	     expr_clock = Clock_predef.ck_tuple clist;
435
	     expr_tag = Utils.new_tag ();
436
	     expr_loc = loc }
437
 | _    -> assert false
438

    
439
let call_of_expr expr =
440
 match expr.expr_desc with
441
 | Expr_appl (f, args, r) -> (f, expr_list_of_expr args, r)
442
 | _                      -> assert false
443

    
444
(* Conversion from dimension expr to standard expr, for the purpose of printing, typing, etc... *)
445
let rec expr_of_dimension dim =
446
 match dim.dim_desc with
447
 | Dbool b        ->
448
     mkexpr dim.dim_loc (Expr_const (const_of_bool b))
449
 | Dint i         ->
450
     mkexpr dim.dim_loc (Expr_const (Const_int i))
451
 | Dident id      ->
452
     mkexpr dim.dim_loc (Expr_ident id)
453
 | Dite (c, t, e) ->
454
     mkexpr dim.dim_loc (Expr_ite (expr_of_dimension c, expr_of_dimension t, expr_of_dimension e))
455
 | Dappl (id, args) ->
456
     mkexpr dim.dim_loc (Expr_appl (id, expr_of_expr_list dim.dim_loc (List.map expr_of_dimension args), None))
457
 | Dlink dim'       -> expr_of_dimension dim'
458
 | Dvar
459
 | Dunivar          -> (Format.eprintf "internal error: Corelang.expr_of_dimension %a@." Dimension.pp_dimension dim;
460
			assert false)
461

    
462
let dimension_of_const loc const =
463
 match const with
464
 | Const_int i                                    -> mkdim_int loc i
465
 | Const_tag t when t = tag_true || t = tag_false -> mkdim_bool loc (t = tag_true)
466
 | _                                              -> raise InvalidDimension
467

    
468
(* Conversion from standard expr to dimension expr, for the purpose of injecting static call arguments 
469
   into dimension expressions *)
470
let rec dimension_of_expr expr =
471
  match expr.expr_desc with
472
  | Expr_const c  -> dimension_of_const expr.expr_loc c
473
  | Expr_ident id -> mkdim_ident expr.expr_loc id
474
  | Expr_appl (f, args, None) when Basic_library.is_expr_internal_fun expr ->
475
      let k = Types.get_static_value (Env.lookup_value Basic_library.type_env f) in
476
      if k = None then raise InvalidDimension;
477
      mkdim_appl expr.expr_loc f (List.map dimension_of_expr (expr_list_of_expr args))
478
  | Expr_ite (i, t, e)        ->
479
      mkdim_ite expr.expr_loc (dimension_of_expr i) (dimension_of_expr t) (dimension_of_expr e)
480
  | _ -> raise InvalidDimension (* not a simple dimension expression *)
481

    
482

    
483
let sort_handlers hl =
484
 List.sort (fun (t, _) (t', _) -> compare t t') hl
485

    
486
let num_10 = Num.num_of_int 10
487
  
488
let rec is_eq_const c1 c2 =
489
  match c1, c2 with
490
  | Const_real (n1, i1, _), Const_real (n2, i2, _)
491
    -> Num.(let n1 = n1 // (num_10 **/ (num_of_int i1)) in
492
	    let n2 = n2 // (num_10 **/ (num_of_int i2)) in
493
	    eq_num n1 n2)
494
  | Const_struct lcl1, Const_struct lcl2
495
    -> List.length lcl1 = List.length lcl2
496
    && List.for_all2 (fun (l1, c1) (l2, c2) -> l1 = l2 && is_eq_const c1 c2) lcl1 lcl2
497
  | _  -> c1 = c2
498

    
499
let rec is_eq_expr e1 e2 = match e1.expr_desc, e2.expr_desc with
500
  | Expr_const c1, Expr_const c2 -> is_eq_const c1 c2
501
  | Expr_ident i1, Expr_ident i2 -> i1 = i2
502
  | Expr_array el1, Expr_array el2 
503
  | Expr_tuple el1, Expr_tuple el2 -> 
504
    List.length el1 = List.length el2 && List.for_all2 is_eq_expr el1 el2 
505
  | Expr_arrow (e1, e2), Expr_arrow (e1', e2') -> is_eq_expr e1 e1' && is_eq_expr e2 e2'
506
  | Expr_fby (e1,e2), Expr_fby (e1',e2') -> is_eq_expr e1 e1' && is_eq_expr e2 e2'
507
  | 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
508
  (* | Expr_concat (e1,e2), Expr_concat (e1',e2') -> is_eq_expr e1 e1' && is_eq_expr e2 e2' *)
509
  (* | Expr_tail e, Expr_tail e' -> is_eq_expr e e' *)
510
  | Expr_pre e, Expr_pre e' -> is_eq_expr e e'
511
  | Expr_when (e, i, l), Expr_when (e', i', l') -> l=l' && i=i' && is_eq_expr e e'
512
  | 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')
513
  | Expr_appl (i, e, r), Expr_appl (i', e', r') -> i=i' && r=r' && is_eq_expr e e'
514
  | Expr_power (e1, i1), Expr_power (e2, i2)
515
  | Expr_access (e1, i1), Expr_access (e2, i2) -> is_eq_expr e1 e2 && is_eq_expr (expr_of_dimension i1) (expr_of_dimension i2)
516
  | _ -> false
517

    
518
let get_node_vars nd =
519
  nd.node_inputs @ nd.node_locals @ nd.node_outputs
520

    
521
let mk_new_node_name nd id =
522
  let used_vars = get_node_vars nd in
523
  let used v = List.exists (fun vdecl -> vdecl.var_id = v) used_vars in
524
  mk_new_name used id
525

    
526
let get_var id var_list =
527
  List.find (fun v -> v.var_id = id) var_list
528

    
529
let get_node_var id node =
530
  try
531
    get_var id (get_node_vars node)
532
  with Not_found -> begin
533
    (* Format.eprintf "Unable to find variable %s in node %s@.@?" id node.node_id; *)
534
    raise Not_found
535
  end
536
    
537
let get_node_eqs =
538
  let get_eqs stmts =
539
    List.fold_right
540
      (fun stmt res ->
541
	match stmt with
542
	| Eq eq -> eq :: res
543
	| Aut _ -> assert false)
544
      stmts
545
      [] in
546
  let table_eqs = Hashtbl.create 23 in
547
  (fun nd ->
548
    try
549
      let (old, res) = Hashtbl.find table_eqs nd.node_id
550
      in if old == nd.node_stmts then res else raise Not_found
551
    with Not_found -> 
552
      let res = get_eqs nd.node_stmts in
553
      begin
554
	Hashtbl.replace table_eqs nd.node_id (nd.node_stmts, res);
555
	res
556
      end)
557

    
558
let get_node_eq id node =
559
 List.find (fun eq -> List.mem id eq.eq_lhs) (get_node_eqs node)
560

    
561
let get_nodes prog = 
562
  List.fold_left (
563
    fun nodes decl ->
564
      match decl.top_decl_desc with
565
	| Node _ -> decl::nodes
566
	| Const _ | ImportedNode _ | Open _ | TypeDef _ -> nodes  
567
  ) [] prog
568

    
569
let get_imported_nodes prog = 
570
  List.fold_left (
571
    fun nodes decl ->
572
      match decl.top_decl_desc with
573
	| ImportedNode _ -> decl::nodes
574
	| Const _ | Node _ | Open _ | TypeDef _-> nodes  
575
  ) [] prog
576

    
577
let get_consts prog = 
578
  List.fold_right (
579
    fun decl consts ->
580
      match decl.top_decl_desc with
581
	| Const _ -> decl::consts
582
	| Node _ | ImportedNode _ | Open _ | TypeDef _ -> consts  
583
  ) prog []
584

    
585
let get_typedefs prog = 
586
  List.fold_right (
587
    fun decl types ->
588
      match decl.top_decl_desc with
589
	| TypeDef _ -> decl::types
590
	| Node _ | ImportedNode _ | Open _ | Const _ -> types  
591
  ) prog []
592

    
593
let get_dependencies prog =
594
  List.fold_right (
595
    fun decl deps ->
596
      match decl.top_decl_desc with
597
	| Open _ -> decl::deps
598
	| Node _ | ImportedNode _ | TypeDef _ | Const _ -> deps  
599
  ) prog []
600

    
601
let get_node_interface nd =
602
 {nodei_id = nd.node_id;
603
  nodei_type = nd.node_type;
604
  nodei_clock = nd.node_clock;
605
  nodei_inputs = nd.node_inputs;
606
  nodei_outputs = nd.node_outputs;
607
  nodei_stateless = nd.node_dec_stateless;
608
  nodei_spec = nd.node_spec;
609
  nodei_prototype = None;
610
  nodei_in_lib = [];
611
 }
612

    
613
(************************************************************************)
614
(*        Renaming                                                      *)
615

    
616
let rec rename_static rename cty =
617
 match cty with
618
 | Tydec_array (d, cty') -> Tydec_array (Dimension.expr_replace_expr rename d, rename_static rename cty')
619
 | Tydec_clock cty       -> Tydec_clock (rename_static rename cty)
620
 | Tydec_struct fl       -> Tydec_struct (List.map (fun (f, cty) -> f, rename_static rename cty) fl)
621
 | _                      -> cty
622

    
623
let rec rename_carrier rename cck =
624
 match cck with
625
 | Ckdec_bool cl -> Ckdec_bool (List.map (fun (c, l) -> rename c, l) cl)
626
 | _             -> cck
627

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

    
630
(* applies the renaming function [fvar] to all variables of expression [expr] *)
631
 let rec expr_replace_var fvar expr =
632
  { expr with expr_desc = expr_desc_replace_var fvar expr.expr_desc }
633

    
634
 and expr_desc_replace_var fvar expr_desc =
635
   match expr_desc with
636
   | Expr_const _ -> expr_desc
637
   | Expr_ident i -> Expr_ident (fvar i)
638
   | Expr_array el -> Expr_array (List.map (expr_replace_var fvar) el)
639
   | Expr_access (e1, d) -> Expr_access (expr_replace_var fvar e1, d)
640
   | Expr_power (e1, d) -> Expr_power (expr_replace_var fvar e1, d)
641
   | Expr_tuple el -> Expr_tuple (List.map (expr_replace_var fvar) el)
642
   | Expr_ite (c, t, e) -> Expr_ite (expr_replace_var fvar c, expr_replace_var fvar t, expr_replace_var fvar e)
643
   | Expr_arrow (e1, e2)-> Expr_arrow (expr_replace_var fvar e1, expr_replace_var fvar e2) 
644
   | Expr_fby (e1, e2) -> Expr_fby (expr_replace_var fvar e1, expr_replace_var fvar e2)
645
   | Expr_pre e' -> Expr_pre (expr_replace_var fvar e')
646
   | Expr_when (e', i, l)-> Expr_when (expr_replace_var fvar e', fvar i, l)
647
   | Expr_merge (i, hl) -> Expr_merge (fvar i, List.map (fun (t, h) -> (t, expr_replace_var fvar h)) hl)
648
   | Expr_appl (i, e', i') -> Expr_appl (i, expr_replace_var fvar e', Utils.option_map (expr_replace_var fvar) i')
649

    
650
(* Applies the renaming function [fvar] to every rhs
651
   only when the corresponding lhs satisfies predicate [pvar] *)
652
 let eq_replace_rhs_var pvar fvar eq =
653
   let pvar l = List.exists pvar l in
654
   let rec replace lhs rhs =
655
     { rhs with expr_desc =
656
     match lhs with
657
     | []  -> assert false
658
     | [_] -> if pvar lhs then expr_desc_replace_var fvar rhs.expr_desc else rhs.expr_desc
659
     | _   ->
660
       (match rhs.expr_desc with
661
       | Expr_tuple tl ->
662
	 Expr_tuple (List.map2 (fun v e -> replace [v] e) lhs tl)
663
       | Expr_appl (f, arg, None) when Basic_library.is_expr_internal_fun rhs ->
664
	 let args = expr_list_of_expr arg in
665
	 Expr_appl (f, expr_of_expr_list arg.expr_loc (List.map (replace lhs) args), None)
666
       | Expr_array _
667
       | Expr_access _
668
       | Expr_power _
669
       | Expr_const _
670
       | Expr_ident _
671
       | Expr_appl _   ->
672
	 if pvar lhs
673
	 then expr_desc_replace_var fvar rhs.expr_desc
674
	 else rhs.expr_desc
675
       | Expr_ite (c, t, e)   -> Expr_ite (replace lhs c, replace lhs t, replace lhs e)
676
       | Expr_arrow (e1, e2)  -> Expr_arrow (replace lhs e1, replace lhs e2) 
677
       | Expr_fby (e1, e2)    -> Expr_fby (replace lhs e1, replace lhs e2)
678
       | Expr_pre e'          -> Expr_pre (replace lhs e')
679
       | Expr_when (e', i, l) -> let i' = if pvar lhs then fvar i else i
680
				 in Expr_when (replace lhs e', i', l)
681
       | Expr_merge (i, hl)   -> let i' = if pvar lhs then fvar i else i
682
				 in Expr_merge (i', List.map (fun (t, h) -> (t, replace lhs h)) hl)
683
       )
684
     }
685
   in { eq with eq_rhs = replace eq.eq_lhs eq.eq_rhs }
686

    
687

    
688
 let rec rename_expr  f_node f_var f_const expr =
689
   { expr with expr_desc = rename_expr_desc f_node f_var f_const expr.expr_desc }
690
 and rename_expr_desc f_node f_var f_const expr_desc =
691
   let re = rename_expr  f_node f_var f_const in
692
   match expr_desc with
693
   | Expr_const _ -> expr_desc
694
   | Expr_ident i -> Expr_ident (f_var i)
695
   | Expr_array el -> Expr_array (List.map re el)
696
   | Expr_access (e1, d) -> Expr_access (re e1, d)
697
   | Expr_power (e1, d) -> Expr_power (re e1, d)
698
   | Expr_tuple el -> Expr_tuple (List.map re el)
699
   | Expr_ite (c, t, e) -> Expr_ite (re c, re t, re e)
700
   | Expr_arrow (e1, e2)-> Expr_arrow (re e1, re e2) 
701
   | Expr_fby (e1, e2) -> Expr_fby (re e1, re e2)
702
   | Expr_pre e' -> Expr_pre (re e')
703
   | Expr_when (e', i, l)-> Expr_when (re e', f_var i, l)
704
   | Expr_merge (i, hl) -> 
705
     Expr_merge (f_var i, List.map (fun (t, h) -> (t, re h)) hl)
706
   | Expr_appl (i, e', i') -> 
707
     Expr_appl (f_node i, re e', Utils.option_map re i')
708
  
709
 let rename_node_annot f_node f_var f_const expr  =
710
   expr
711
 (* TODO assert false *)
712

    
713
 let rename_expr_annot f_node f_var f_const annot =
714
   annot
715
 (* TODO assert false *)
716

    
717
let rename_node f_node f_var f_const nd =
718
  let rename_var v = { v with var_id = f_var v.var_id } in
719
  let rename_eq eq = { eq with
720
      eq_lhs = List.map f_var eq.eq_lhs; 
721
      eq_rhs = rename_expr f_node f_var f_const eq.eq_rhs
722
    } 
723
  in
724
  let inputs = List.map rename_var nd.node_inputs in
725
  let outputs = List.map rename_var nd.node_outputs in
726
  let locals = List.map rename_var nd.node_locals in
727
  let gen_calls = List.map (rename_expr f_node f_var f_const) nd.node_gencalls in
728
  let node_checks = List.map (Dimension.expr_replace_var f_var)  nd.node_checks in
729
  let node_asserts = List.map 
730
    (fun a -> 
731
      {a with assert_expr = 
732
	  let expr = a.assert_expr in
733
	  rename_expr f_node f_var f_const expr})
734
    nd.node_asserts
735
  in
736
  let node_stmts = List.map (fun eq -> Eq (rename_eq eq)) (get_node_eqs nd) in
737
  let spec = 
738
    Utils.option_map 
739
      (fun s -> rename_node_annot f_node f_var f_const s) 
740
      nd.node_spec 
741
  in
742
  let annot =
743
    List.map 
744
      (fun s -> rename_expr_annot f_node f_var f_const s) 
745
      nd.node_annot
746
  in
747
  {
748
    node_id = f_node nd.node_id;
749
    node_type = nd.node_type;
750
    node_clock = nd.node_clock;
751
    node_inputs = inputs;
752
    node_outputs = outputs;
753
    node_locals = locals;
754
    node_gencalls = gen_calls;
755
    node_checks = node_checks;
756
    node_asserts = node_asserts;
757
    node_stmts = node_stmts;
758
    node_dec_stateless = nd.node_dec_stateless;
759
    node_stateless = nd.node_stateless;
760
    node_spec = spec;
761
    node_annot = annot;
762
  }
763

    
764

    
765
let rename_const f_const c =
766
  { c with const_id = f_const c.const_id }
767

    
768
let rename_typedef f_var t =
769
  match t.tydef_desc with
770
  | Tydec_enum tags -> { t with tydef_desc = Tydec_enum (List.map f_var tags) }
771
  | _               -> t
772

    
773
let rename_prog f_node f_var f_const prog =
774
  List.rev (
775
    List.fold_left (fun accu top ->
776
      (match top.top_decl_desc with
777
      | Node nd -> 
778
	 { top with top_decl_desc = Node (rename_node f_node f_var f_const nd) }
779
      | Const c -> 
780
	 { top with top_decl_desc = Const (rename_const f_const c) }
781
      | TypeDef tdef ->
782
	 { top with top_decl_desc = TypeDef (rename_typedef f_var tdef) }
783
      | ImportedNode _
784
      | Open _       -> top)
785
      ::accu
786
) [] prog
787
		   )
788

    
789
(**********************************************************************)
790
(* Pretty printers *)
791

    
792
let pp_decl_type fmt tdecl =
793
  match tdecl.top_decl_desc with
794
  | Node nd ->
795
    fprintf fmt "%s: " nd.node_id;
796
    Utils.reset_names ();
797
    fprintf fmt "%a@ " Types.print_ty nd.node_type
798
  | ImportedNode ind ->
799
    fprintf fmt "%s: " ind.nodei_id;
800
    Utils.reset_names ();
801
    fprintf fmt "%a@ " Types.print_ty ind.nodei_type
802
  | Const _ | Open _ | TypeDef _ -> ()
803

    
804
let pp_prog_type fmt tdecl_list =
805
  Utils.fprintf_list ~sep:"" pp_decl_type fmt tdecl_list
806

    
807
let pp_decl_clock fmt cdecl =
808
  match cdecl.top_decl_desc with
809
  | Node nd ->
810
    fprintf fmt "%s: " nd.node_id;
811
    Utils.reset_names ();
812
    fprintf fmt "%a@ " Clocks.print_ck nd.node_clock
813
  | ImportedNode ind ->
814
    fprintf fmt "%s: " ind.nodei_id;
815
    Utils.reset_names ();
816
    fprintf fmt "%a@ " Clocks.print_ck ind.nodei_clock
817
  | Const _ | Open _ | TypeDef _ -> ()
818

    
819
let pp_prog_clock fmt prog =
820
  Utils.fprintf_list ~sep:"" pp_decl_clock fmt prog
821

    
822
let pp_error fmt = function
823
    Main_not_found ->
824
      fprintf fmt "Could not find the definition of main node %s.@."
825
	!Global.main_node
826
  | Main_wrong_kind ->
827
    fprintf fmt
828
      "Node %s does not correspond to a valid main node definition.@." 
829
      !Global.main_node 
830
  | No_main_specified ->
831
    fprintf fmt "No main node specified (use -node option)@."
832
  | Unbound_symbol sym ->
833
    fprintf fmt
834
      "%s is undefined.@."
835
      sym
836
  | Already_bound_symbol sym -> 
837
    fprintf fmt
838
      "%s is already defined.@."
839
      sym
840
  | Unknown_library sym ->
841
    fprintf fmt
842
      "impossible to load library %s.lusic.@.Please compile the corresponding interface or source file.@."
843
      sym
844
  | Wrong_number sym ->
845
    fprintf fmt
846
      "library %s.lusic has a different version number and may crash compiler.@.Please recompile the corresponding interface or source file.@."
847
      sym
848

    
849
(* filling node table with internal functions *)
850
let vdecls_of_typ_ck cpt ty =
851
  let loc = Location.dummy_loc in
852
  List.map
853
    (fun _ -> incr cpt;
854
              let name = sprintf "_var_%d" !cpt in
855
              mkvar_decl loc (name, mktyp loc Tydec_any, mkclock loc Ckdec_any, false, None))
856
    (Types.type_list_of_type ty)
857

    
858
let mk_internal_node id =
859
  let spec = None in
860
  let ty = Env.lookup_value Basic_library.type_env id in
861
  let ck = Env.lookup_value Basic_library.clock_env id in
862
  let (tin, tout) = Types.split_arrow ty in
863
  (*eprintf "internal fun %s: %d -> %d@." id (List.length (Types.type_list_of_type tin)) (List.length (Types.type_list_of_type tout));*)
864
  let cpt = ref (-1) in
865
  mktop
866
    (ImportedNode
867
       {nodei_id = id;
868
	nodei_type = ty;
869
	nodei_clock = ck;
870
	nodei_inputs = vdecls_of_typ_ck cpt tin;
871
	nodei_outputs = vdecls_of_typ_ck cpt tout;
872
	nodei_stateless = Types.get_static_value ty <> None;
873
	nodei_spec = spec;
874
	nodei_prototype = None;
875
       	nodei_in_lib = [];
876
       })
877

    
878
let add_internal_funs () =
879
  List.iter
880
    (fun id -> let nd = mk_internal_node id in Hashtbl.add node_table id nd)
881
    Basic_library.internal_funs
882

    
883

    
884

    
885
(* Replace any occurence of a var in vars_to_replace by its associated
886
   expression in defs until e does not contain any such variables *)
887
let rec substitute_expr vars_to_replace defs e =
888
  let se = substitute_expr vars_to_replace defs in
889
  { e with expr_desc = 
890
      let ed = e.expr_desc in
891
      match ed with
892
      | Expr_const _ -> ed
893
      | Expr_array el -> Expr_array (List.map se el)
894
      | Expr_access (e1, d) -> Expr_access (se e1, d)
895
      | Expr_power (e1, d) -> Expr_power (se e1, d)
896
      | Expr_tuple el -> Expr_tuple (List.map se el)
897
      | Expr_ite (c, t, e) -> Expr_ite (se c, se t, se e)
898
      | Expr_arrow (e1, e2)-> Expr_arrow (se e1, se e2) 
899
      | Expr_fby (e1, e2) -> Expr_fby (se e1, se e2)
900
      | Expr_pre e' -> Expr_pre (se e')
901
      | Expr_when (e', i, l)-> Expr_when (se e', i, l)
902
      | Expr_merge (i, hl) -> Expr_merge (i, List.map (fun (t, h) -> (t, se h)) hl)
903
      | Expr_appl (i, e', i') -> Expr_appl (i, se e', i')
904
      | Expr_ident i -> 
905
	if List.exists (fun v -> v.var_id = i) vars_to_replace then (
906
	  let eq_i eq = eq.eq_lhs = [i] in
907
	  if List.exists eq_i defs then
908
	    let sub = List.find eq_i defs in
909
	    let sub' = se sub.eq_rhs in
910
	    sub'.expr_desc
911
	  else 
912
	    assert false
913
	)
914
	else
915
	  ed
916

    
917
  }
918
(* FAUT IL RETIRER ?
919
  
920
 let rec expr_to_eexpr  expr =
921
   { eexpr_tag = expr.expr_tag;
922
     eexpr_desc = expr_desc_to_eexpr_desc expr.expr_desc;
923
     eexpr_type = expr.expr_type;
924
     eexpr_clock = expr.expr_clock;
925
     eexpr_loc = expr.expr_loc
926
   }
927
 and expr_desc_to_eexpr_desc expr_desc =
928
   let conv = expr_to_eexpr in
929
   match expr_desc with
930
   | Expr_const c -> EExpr_const (match c with
931
     | Const_int x -> EConst_int x 
932
     | Const_real x -> EConst_real x 
933
     | Const_float x -> EConst_float x 
934
     | Const_tag x -> EConst_tag x 
935
     | _ -> assert false
936

    
937
   )
938
   | Expr_ident i -> EExpr_ident i
939
   | Expr_tuple el -> EExpr_tuple (List.map conv el)
940

    
941
   | Expr_arrow (e1, e2)-> EExpr_arrow (conv e1, conv e2) 
942
   | Expr_fby (e1, e2) -> EExpr_fby (conv e1, conv e2)
943
   | Expr_pre e' -> EExpr_pre (conv e')
944
   | Expr_appl (i, e', i') -> 
945
     EExpr_appl 
946
       (i, conv e', match i' with None -> None | Some(id, _) -> Some id)
947

    
948
   | Expr_when _
949
   | Expr_merge _ -> assert false
950
   | Expr_array _ 
951
   | Expr_access _ 
952
   | Expr_power _  -> assert false
953
   | Expr_ite (c, t, e) -> assert false 
954
   | _ -> assert false
955

    
956
     *)
957
let rec get_expr_calls nodes e =
958
  let get_calls = get_expr_calls nodes in
959
  match e.expr_desc with
960
  | Expr_const _ 
961
   | Expr_ident _ -> Utils.ISet.empty
962
   | Expr_tuple el
963
   | Expr_array el -> List.fold_left (fun accu e -> Utils.ISet.union accu (get_calls e)) Utils.ISet.empty el
964
   | Expr_pre e1 
965
   | Expr_when (e1, _, _) 
966
   | Expr_access (e1, _) 
967
   | Expr_power (e1, _) -> get_calls e1
968
   | Expr_ite (c, t, e) -> Utils.ISet.union (Utils.ISet.union (get_calls c) (get_calls t)) (get_calls e) 
969
   | Expr_arrow (e1, e2) 
970
   | Expr_fby (e1, e2) -> Utils.ISet.union (get_calls e1) (get_calls e2)
971
   | Expr_merge (_, hl) -> List.fold_left (fun accu (_, h) -> Utils.ISet.union accu (get_calls h)) Utils.ISet.empty  hl
972
   | Expr_appl (i, e', i') -> 
973
     if Basic_library.is_expr_internal_fun e then 
974
       (get_calls e') 
975
     else
976
       let calls =  Utils.ISet.add i (get_calls e') in
977
       let test = (fun n -> match n.top_decl_desc with Node nd -> nd.node_id = i | _ -> false) in
978
       if List.exists test nodes then
979
	 match (List.find test nodes).top_decl_desc with
980
	 | Node nd -> Utils.ISet.union (get_node_calls nodes nd) calls
981
	 | _ -> assert false
982
       else 
983
	 calls
984

    
985
and get_eq_calls nodes eq =
986
  get_expr_calls nodes eq.eq_rhs
987
and get_node_calls nodes node =
988
  List.fold_left (fun accu eq -> Utils.ISet.union (get_eq_calls nodes eq) accu) Utils.ISet.empty (get_node_eqs node)
989

    
990
let get_expr_vars e =
991
  let rec get_expr_vars vars e =
992
    get_expr_desc_vars vars e.expr_desc
993
  and get_expr_desc_vars vars expr_desc =
994
    Format.eprintf "get_expr_desc_vars expr=%a@." Printers.pp_expr (mkexpr Location.dummy_loc expr_desc);
995
  match expr_desc with
996
  | Expr_const _ -> vars
997
  | Expr_ident x -> Format.eprintf "%s is an ident!@." x; Utils.ISet.add x vars
998
  | Expr_tuple el
999
  | Expr_array el -> List.fold_left get_expr_vars vars el
1000
  | Expr_pre e1 -> get_expr_vars vars e1
1001
  | Expr_when (e1, c, _) -> get_expr_vars (Utils.ISet.add c vars) e1 
1002
  | Expr_access (e1, d) 
1003
  | Expr_power (e1, d)   -> List.fold_left get_expr_vars vars [e1; expr_of_dimension d]
1004
  | Expr_ite (c, t, e) -> List.fold_left get_expr_vars vars [c; t; e]
1005
  | Expr_arrow (e1, e2) 
1006
  | Expr_fby (e1, e2) -> List.fold_left get_expr_vars vars [e1; e2]
1007
  | Expr_merge (c, hl) -> List.fold_left (fun vars (_, h) -> get_expr_vars vars h) (Utils.ISet.add c vars) hl
1008
  | Expr_appl (_, arg, None)   -> get_expr_vars vars arg
1009
  | Expr_appl (_, arg, Some r) -> List.fold_left get_expr_vars vars [arg; r]
1010
  in
1011
  get_expr_vars Utils.ISet.empty e 
1012

    
1013
let rec expr_has_arrows e =
1014
  expr_desc_has_arrows e.expr_desc
1015
and expr_desc_has_arrows expr_desc =
1016
  match expr_desc with
1017
  | Expr_const _ 
1018
  | Expr_ident _ -> false
1019
  | Expr_tuple el
1020
  | Expr_array el -> List.exists expr_has_arrows el
1021
  | Expr_pre e1 
1022
  | Expr_when (e1, _, _) 
1023
  | Expr_access (e1, _) 
1024
  | Expr_power (e1, _) -> expr_has_arrows e1
1025
  | Expr_ite (c, t, e) -> List.exists expr_has_arrows [c; t; e]
1026
  | Expr_arrow (e1, e2) 
1027
  | Expr_fby (e1, e2) -> true
1028
  | Expr_merge (_, hl) -> List.exists (fun (_, h) -> expr_has_arrows h) hl
1029
  | Expr_appl (i, e', i') -> expr_has_arrows e'
1030

    
1031
and eq_has_arrows eq =
1032
  expr_has_arrows eq.eq_rhs
1033
and node_has_arrows node =
1034
  List.exists (fun eq -> eq_has_arrows eq) (get_node_eqs node)
1035

    
1036

    
1037
let copy_var_decl vdecl =
1038
  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)
1039

    
1040
let copy_const cdecl =
1041
  { cdecl with const_type = Types.new_var () }
1042

    
1043
let copy_node nd =
1044
  { nd with
1045
    node_type     = Types.new_var ();
1046
    node_clock    = Clocks.new_var true;
1047
    node_inputs   = List.map copy_var_decl nd.node_inputs;
1048
    node_outputs  = List.map copy_var_decl nd.node_outputs;
1049
    node_locals   = List.map copy_var_decl nd.node_locals;
1050
    node_gencalls = [];
1051
    node_checks   = [];
1052
    node_stateless = None;
1053
  }
1054

    
1055
let copy_top top =
1056
  match top.top_decl_desc with
1057
  | Node nd -> { top with top_decl_desc = Node (copy_node nd)  }
1058
  | Const c -> { top with top_decl_desc = Const (copy_const c) }
1059
  | _       -> top
1060

    
1061
let copy_prog top_list =
1062
  List.map copy_top top_list
1063

    
1064
let functional_backend () = 
1065
  match !Options.output with
1066
  | "horn" | "lustre" | "acsl" -> true
1067
  | _ -> false
1068

    
1069
(* Local Variables: *)
1070
(* compile-command:"make -C .." *)
1071
(* End: *)