Project

General

Profile

Download (34.7 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 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
(***********************************************************)
185
(* Fast access to nodes, by name *)
186
let (node_table : (ident, top_decl) Hashtbl.t) = Hashtbl.create 30
187
let consts_table = Hashtbl.create 30
188

    
189
let print_node_table fmt () =
190
  begin
191
    Format.fprintf fmt "{ /* node table */@.";
192
    Hashtbl.iter (fun id nd ->
193
      Format.fprintf fmt "%s |-> %a"
194
	id
195
	Printers.pp_short_decl nd
196
    ) node_table;
197
    Format.fprintf fmt "}@."
198
  end
199

    
200
let print_consts_table fmt () =
201
  begin
202
    Format.fprintf fmt "{ /* consts table */@.";
203
    Hashtbl.iter (fun id const ->
204
      Format.fprintf fmt "%s |-> %a"
205
	id
206
	Printers.pp_const_decl (const_of_top const)
207
    ) consts_table;
208
    Format.fprintf fmt "}@."
209
  end
210

    
211
let node_name td =
212
    match td.top_decl_desc with 
213
    | Node nd         -> nd.node_id
214
    | ImportedNode nd -> nd.nodei_id
215
    | _ -> assert false
216

    
217
let is_generic_node td =
218
  match td.top_decl_desc with 
219
  | Node nd         -> List.exists (fun v -> v.var_dec_const) nd.node_inputs
220
  | ImportedNode nd -> List.exists (fun v -> v.var_dec_const) nd.nodei_inputs
221
  | _ -> assert false
222

    
223
let node_inputs td =
224
  match td.top_decl_desc with 
225
  | Node nd         -> nd.node_inputs
226
  | ImportedNode nd -> nd.nodei_inputs
227
  | _ -> assert false
228

    
229
let node_from_name id =
230
  try
231
    Hashtbl.find node_table id
232
  with Not_found -> (Format.eprintf "Unable to find any node named %s@ @?" id;
233
		     assert false)
234

    
235
let is_imported_node td =
236
  match td.top_decl_desc with 
237
  | Node nd         -> false
238
  | ImportedNode nd -> true
239
  | _ -> assert false
240

    
241

    
242
(* alias and type definition table *)
243

    
244
(*let mktop = mktop_decl Location.dummy_loc Version.include_path false *)
245

    
246
let mktop = mktop_decl Location.dummy_loc !Options.include_dir false
247

    
248
let top_int_type = mktop (TypeDef {tydef_id = "int"; tydef_desc = Tydec_int})
249
let top_bool_type = mktop (TypeDef {tydef_id = "bool"; tydef_desc = Tydec_bool})
250
(* let top_float_type = mktop (TypeDef {tydef_id = "float"; tydef_desc = Tydec_float}) *)
251
let top_real_type = mktop (TypeDef {tydef_id = "real"; tydef_desc = Tydec_real})
252

    
253
let type_table =
254
  Utils.create_hashtable 20 [
255
    Tydec_int  , top_int_type;
256
    Tydec_bool , top_bool_type;
257
    (* Tydec_float, top_float_type; *)
258
    Tydec_real , top_real_type
259
  ]
260

    
261
let print_type_table fmt () =
262
  begin
263
    Format.fprintf fmt "{ /* type table */@.";
264
    Hashtbl.iter (fun tydec tdef ->
265
      Format.fprintf fmt "%a |-> %a"
266
	Printers.pp_var_type_dec_desc tydec
267
	Printers.pp_typedef (typedef_of_top tdef)
268
    ) type_table;
269
    Format.fprintf fmt "}@."
270
  end
271

    
272
let rec is_user_type typ =
273
  match typ with
274
  | Tydec_int | Tydec_bool | Tydec_real 
275
  (* | Tydec_float *) | Tydec_any | Tydec_const _ -> false
276
  | Tydec_clock typ' -> is_user_type typ'
277
  | _ -> true
278

    
279
let get_repr_type typ =
280
  let typ_def = (typedef_of_top (Hashtbl.find type_table typ)).tydef_desc in
281
  if is_user_type typ_def then typ else typ_def
282

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

    
307
let tag_true = "true"
308
let tag_false = "false"
309
let tag_default = "default"
310

    
311
let const_is_bool c =
312
 match c with
313
 | Const_tag t -> t = tag_true || t = tag_false
314
 | _           -> false
315

    
316
(* Computes the negation of a boolean constant *)
317
let const_negation c =
318
  assert (const_is_bool c);
319
  match c with
320
  | Const_tag t when t = tag_true  -> Const_tag tag_false
321
  | _                              -> Const_tag tag_true
322

    
323
let const_or c1 c2 =
324
  assert (const_is_bool c1 && const_is_bool c2);
325
  match c1, c2 with
326
  | Const_tag t1, _            when t1 = tag_true -> c1
327
  | _           , Const_tag t2 when t2 = tag_true -> c2
328
  | _                                             -> Const_tag tag_false
329

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

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

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

    
350
(* To guarantee uniqueness of tags in enum types *)
351
let tag_table =
352
  Utils.create_hashtable 20 [
353
   tag_true, top_bool_type;
354
   tag_false, top_bool_type
355
  ]
356

    
357
(* To guarantee uniqueness of fields in struct types *)
358
let field_table =
359
  Utils.create_hashtable 20 [
360
  ]
361

    
362
let get_enum_type_tags cty =
363
(*Format.eprintf "get_enum_type_tags %a@." Printers.pp_var_type_dec_desc cty;*)
364
 match cty with
365
 | Tydec_bool    -> [tag_true; tag_false]
366
 | Tydec_const _ -> (match (typedef_of_top (Hashtbl.find type_table cty)).tydef_desc with
367
                     | Tydec_enum tl -> tl
368
                     | _             -> assert false)
369
 | _            -> assert false
370

    
371
let get_struct_type_fields cty =
372
 match cty with
373
 | Tydec_const _ -> (match (typedef_of_top (Hashtbl.find type_table cty)).tydef_desc with
374
                     | Tydec_struct fl -> fl
375
                     | _               -> assert false)
376
 | _            -> assert false
377

    
378
let const_of_bool b =
379
 Const_tag (if b then tag_true else tag_false)
380

    
381
(* let get_const c = snd (Hashtbl.find consts_table c) *)
382

    
383
let ident_of_expr expr =
384
 match expr.expr_desc with
385
 | Expr_ident id -> id
386
 | _             -> assert false
387

    
388
(* Generate a new ident expression from a declared variable *)
389
let expr_of_vdecl v =
390
  { expr_tag = Utils.new_tag ();
391
    expr_desc = Expr_ident v.var_id;
392
    expr_type = v.var_type;
393
    expr_clock = v.var_clock;
394
    expr_delay = Delay.new_var ();
395
    expr_annot = None;
396
    expr_loc = v.var_loc }
397

    
398
(* Caution, returns an untyped and unclocked expression *)
399
let expr_of_ident id loc =
400
  {expr_tag = Utils.new_tag ();
401
   expr_desc = Expr_ident id;
402
   expr_type = Types.new_var ();
403
   expr_clock = Clocks.new_var true;
404
   expr_delay = Delay.new_var ();
405
   expr_loc = loc;
406
   expr_annot = None}
407

    
408
let is_tuple_expr expr =
409
 match expr.expr_desc with
410
  | Expr_tuple _ -> true
411
  | _            -> false
412

    
413
let expr_list_of_expr expr =
414
  match expr.expr_desc with
415
  | Expr_tuple elist -> elist
416
  | _                -> [expr]
417

    
418
let expr_of_expr_list loc elist =
419
 match elist with
420
 | [t]  -> { t with expr_loc = loc }
421
 | t::_ ->
422
    let tlist = List.map (fun e -> e.expr_type) elist in
423
    let clist = List.map (fun e -> e.expr_clock) elist in
424
    { t with expr_desc = Expr_tuple elist;
425
	     expr_type = Type_predef.type_tuple tlist;
426
	     expr_clock = Clock_predef.ck_tuple clist;
427
	     expr_tag = Utils.new_tag ();
428
	     expr_loc = loc }
429
 | _    -> assert false
430

    
431
let call_of_expr expr =
432
 match expr.expr_desc with
433
 | Expr_appl (f, args, r) -> (f, expr_list_of_expr args, r)
434
 | _                      -> assert false
435

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

    
454
let dimension_of_const loc const =
455
 match const with
456
 | Const_int i                                    -> mkdim_int loc i
457
 | Const_tag t when t = tag_true || t = tag_false -> mkdim_bool loc (t = tag_true)
458
 | _                                              -> raise InvalidDimension
459

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

    
474

    
475
let sort_handlers hl =
476
 List.sort (fun (t, _) (t', _) -> compare t t') hl
477

    
478
let rec is_eq_expr e1 e2 = match e1.expr_desc, e2.expr_desc with
479
  | Expr_const c1, Expr_const c2 -> c1 = c2
480
  | Expr_ident i1, Expr_ident i2 -> i1 = i2
481
  | Expr_array el1, Expr_array el2 
482
  | Expr_tuple el1, Expr_tuple el2 -> 
483
    List.length el1 = List.length el2 && List.for_all2 is_eq_expr el1 el2 
484
  | Expr_arrow (e1, e2), Expr_arrow (e1', e2') -> is_eq_expr e1 e1' && is_eq_expr e2 e2'
485
  | Expr_fby (e1,e2), Expr_fby (e1',e2') -> is_eq_expr e1 e1' && is_eq_expr e2 e2'
486
  | 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
487
  (* | Expr_concat (e1,e2), Expr_concat (e1',e2') -> is_eq_expr e1 e1' && is_eq_expr e2 e2' *)
488
  (* | Expr_tail e, Expr_tail e' -> is_eq_expr e e' *)
489
  | Expr_pre e, Expr_pre e' -> is_eq_expr e e'
490
  | Expr_when (e, i, l), Expr_when (e', i', l') -> l=l' && i=i' && is_eq_expr e e'
491
  | 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')
492
  | Expr_appl (i, e, r), Expr_appl (i', e', r') -> i=i' && r=r' && is_eq_expr e e'
493
  | Expr_power (e1, i1), Expr_power (e2, i2)
494
  | Expr_access (e1, i1), Expr_access (e2, i2) -> is_eq_expr e1 e2 && is_eq_expr (expr_of_dimension i1) (expr_of_dimension i2)
495
  | _ -> false
496

    
497
let get_node_vars nd =
498
  nd.node_inputs @ nd.node_locals @ nd.node_outputs
499

    
500
let mk_new_node_name nd id =
501
  let used_vars = get_node_vars nd in
502
  let used v = List.exists (fun vdecl -> vdecl.var_id = v) used_vars in
503
  mk_new_name used id
504

    
505
let get_var id var_list =
506
  List.find (fun v -> v.var_id = id) var_list
507

    
508
let get_node_var id node =
509
  get_var id (get_node_vars node)
510

    
511
let get_node_eqs =
512
  let get_eqs stmts =
513
    List.fold_right
514
      (fun stmt res ->
515
	match stmt with
516
	| Eq eq -> eq :: res
517
	| Aut _ -> assert false)
518
      stmts
519
      [] in
520
  let table_eqs = Hashtbl.create 23 in
521
  (fun nd ->
522
    try
523
      let (old, res) = Hashtbl.find table_eqs nd.node_id
524
      in if old == nd.node_stmts then res else raise Not_found
525
    with Not_found -> 
526
      let res = get_eqs nd.node_stmts in
527
      begin
528
	Hashtbl.replace table_eqs nd.node_id (nd.node_stmts, res);
529
	res
530
      end)
531

    
532
let get_node_eq id node =
533
 List.find (fun eq -> List.mem id eq.eq_lhs) (get_node_eqs node)
534

    
535
let get_nodes prog = 
536
  List.fold_left (
537
    fun nodes decl ->
538
      match decl.top_decl_desc with
539
	| Node _ -> decl::nodes
540
	| Const _ | ImportedNode _ | Open _ | TypeDef _ -> nodes  
541
  ) [] prog
542

    
543
let get_imported_nodes prog = 
544
  List.fold_left (
545
    fun nodes decl ->
546
      match decl.top_decl_desc with
547
	| ImportedNode _ -> decl::nodes
548
	| Const _ | Node _ | Open _ | TypeDef _-> nodes  
549
  ) [] prog
550

    
551
let get_consts prog = 
552
  List.fold_right (
553
    fun decl consts ->
554
      match decl.top_decl_desc with
555
	| Const _ -> decl::consts
556
	| Node _ | ImportedNode _ | Open _ | TypeDef _ -> consts  
557
  ) prog []
558

    
559
let get_typedefs prog = 
560
  List.fold_right (
561
    fun decl types ->
562
      match decl.top_decl_desc with
563
	| TypeDef _ -> decl::types
564
	| Node _ | ImportedNode _ | Open _ | Const _ -> types  
565
  ) prog []
566

    
567
let get_dependencies prog =
568
  List.fold_right (
569
    fun decl deps ->
570
      match decl.top_decl_desc with
571
	| Open _ -> decl::deps
572
	| Node _ | ImportedNode _ | TypeDef _ | Const _ -> deps  
573
  ) prog []
574

    
575
let get_node_interface nd =
576
 {nodei_id = nd.node_id;
577
  nodei_type = nd.node_type;
578
  nodei_clock = nd.node_clock;
579
  nodei_inputs = nd.node_inputs;
580
  nodei_outputs = nd.node_outputs;
581
  nodei_stateless = nd.node_dec_stateless;
582
  nodei_spec = nd.node_spec;
583
  nodei_prototype = None;
584
  nodei_in_lib = [];
585
 }
586

    
587
(************************************************************************)
588
(*        Renaming                                                      *)
589

    
590
let rec rename_static rename cty =
591
 match cty with
592
 | Tydec_array (d, cty') -> Tydec_array (Dimension.expr_replace_expr rename d, rename_static rename cty')
593
 | Tydec_clock cty       -> Tydec_clock (rename_static rename cty)
594
 | Tydec_struct fl       -> Tydec_struct (List.map (fun (f, cty) -> f, rename_static rename cty) fl)
595
 | _                      -> cty
596

    
597
let rec rename_carrier rename cck =
598
 match cck with
599
 | Ckdec_bool cl -> Ckdec_bool (List.map (fun (c, l) -> rename c, l) cl)
600
 | _             -> cck
601

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

    
604
(* applies the renaming function [fvar] to all variables of expression [expr] *)
605
 let rec expr_replace_var fvar expr =
606
  { expr with expr_desc = expr_desc_replace_var fvar expr.expr_desc }
607

    
608
 and expr_desc_replace_var fvar expr_desc =
609
   match expr_desc with
610
   | Expr_const _ -> expr_desc
611
   | Expr_ident i -> Expr_ident (fvar i)
612
   | Expr_array el -> Expr_array (List.map (expr_replace_var fvar) el)
613
   | Expr_access (e1, d) -> Expr_access (expr_replace_var fvar e1, d)
614
   | Expr_power (e1, d) -> Expr_power (expr_replace_var fvar e1, d)
615
   | Expr_tuple el -> Expr_tuple (List.map (expr_replace_var fvar) el)
616
   | Expr_ite (c, t, e) -> Expr_ite (expr_replace_var fvar c, expr_replace_var fvar t, expr_replace_var fvar e)
617
   | Expr_arrow (e1, e2)-> Expr_arrow (expr_replace_var fvar e1, expr_replace_var fvar e2) 
618
   | Expr_fby (e1, e2) -> Expr_fby (expr_replace_var fvar e1, expr_replace_var fvar e2)
619
   | Expr_pre e' -> Expr_pre (expr_replace_var fvar e')
620
   | Expr_when (e', i, l)-> Expr_when (expr_replace_var fvar e', fvar i, l)
621
   | Expr_merge (i, hl) -> Expr_merge (fvar i, List.map (fun (t, h) -> (t, expr_replace_var fvar h)) hl)
622
   | Expr_appl (i, e', i') -> Expr_appl (i, expr_replace_var fvar e', Utils.option_map (expr_replace_var fvar) i')
623

    
624
(* Applies the renaming function [fvar] to every rhs
625
   only when the corresponding lhs satisfies predicate [pvar] *)
626
 let eq_replace_rhs_var pvar fvar eq =
627
   let pvar l = List.exists pvar l in
628
   let rec replace lhs rhs =
629
     { rhs with expr_desc =
630
     match lhs with
631
     | []  -> assert false
632
     | [_] -> if pvar lhs then expr_desc_replace_var fvar rhs.expr_desc else rhs.expr_desc
633
     | _   ->
634
       (match rhs.expr_desc with
635
       | Expr_tuple tl ->
636
	 Expr_tuple (List.map2 (fun v e -> replace [v] e) lhs tl)
637
       | Expr_appl (f, arg, None) when Basic_library.is_expr_internal_fun rhs ->
638
	 let args = expr_list_of_expr arg in
639
	 Expr_appl (f, expr_of_expr_list arg.expr_loc (List.map (replace lhs) args), None)
640
       | Expr_array _
641
       | Expr_access _
642
       | Expr_power _
643
       | Expr_const _
644
       | Expr_ident _
645
       | Expr_appl _   ->
646
	 if pvar lhs
647
	 then expr_desc_replace_var fvar rhs.expr_desc
648
	 else rhs.expr_desc
649
       | Expr_ite (c, t, e)   -> Expr_ite (replace lhs c, replace lhs t, replace lhs e)
650
       | Expr_arrow (e1, e2)  -> Expr_arrow (replace lhs e1, replace lhs e2) 
651
       | Expr_fby (e1, e2)    -> Expr_fby (replace lhs e1, replace lhs e2)
652
       | Expr_pre e'          -> Expr_pre (replace lhs e')
653
       | Expr_when (e', i, l) -> let i' = if pvar lhs then fvar i else i
654
				 in Expr_when (replace lhs e', i', l)
655
       | Expr_merge (i, hl)   -> let i' = if pvar lhs then fvar i else i
656
				 in Expr_merge (i', List.map (fun (t, h) -> (t, replace lhs h)) hl)
657
       )
658
     }
659
   in { eq with eq_rhs = replace eq.eq_lhs eq.eq_rhs }
660

    
661

    
662
 let rec rename_expr  f_node f_var f_const expr =
663
   { expr with expr_desc = rename_expr_desc f_node f_var f_const expr.expr_desc }
664
 and rename_expr_desc f_node f_var f_const expr_desc =
665
   let re = rename_expr  f_node f_var f_const in
666
   match expr_desc with
667
   | Expr_const _ -> expr_desc
668
   | Expr_ident i -> Expr_ident (f_var i)
669
   | Expr_array el -> Expr_array (List.map re el)
670
   | Expr_access (e1, d) -> Expr_access (re e1, d)
671
   | Expr_power (e1, d) -> Expr_power (re e1, d)
672
   | Expr_tuple el -> Expr_tuple (List.map re el)
673
   | Expr_ite (c, t, e) -> Expr_ite (re c, re t, re e)
674
   | Expr_arrow (e1, e2)-> Expr_arrow (re e1, re e2) 
675
   | Expr_fby (e1, e2) -> Expr_fby (re e1, re e2)
676
   | Expr_pre e' -> Expr_pre (re e')
677
   | Expr_when (e', i, l)-> Expr_when (re e', f_var i, l)
678
   | Expr_merge (i, hl) -> 
679
     Expr_merge (f_var i, List.map (fun (t, h) -> (t, re h)) hl)
680
   | Expr_appl (i, e', i') -> 
681
     Expr_appl (f_node i, re e', Utils.option_map re i')
682
  
683
 let rename_node_annot f_node f_var f_const expr  =
684
   expr
685
 (* TODO assert false *)
686

    
687
 let rename_expr_annot f_node f_var f_const annot =
688
   annot
689
 (* TODO assert false *)
690

    
691
let rename_node f_node f_var f_const nd =
692
  let rename_var v = { v with var_id = f_var v.var_id } in
693
  let rename_eq eq = { eq with
694
      eq_lhs = List.map f_var eq.eq_lhs; 
695
      eq_rhs = rename_expr f_node f_var f_const eq.eq_rhs
696
    } 
697
  in
698
  let inputs = List.map rename_var nd.node_inputs in
699
  let outputs = List.map rename_var nd.node_outputs in
700
  let locals = List.map rename_var nd.node_locals in
701
  let gen_calls = List.map (rename_expr f_node f_var f_const) nd.node_gencalls in
702
  let node_checks = List.map (Dimension.expr_replace_var f_var)  nd.node_checks in
703
  let node_asserts = List.map 
704
    (fun a -> 
705
      {a with assert_expr = 
706
	  let expr = a.assert_expr in
707
	  rename_expr f_node f_var f_const expr})
708
    nd.node_asserts
709
  in
710
  let node_stmts = List.map (fun eq -> Eq (rename_eq eq)) (get_node_eqs nd) in
711
  let spec = 
712
    Utils.option_map 
713
      (fun s -> rename_node_annot f_node f_var f_const s) 
714
      nd.node_spec 
715
  in
716
  let annot =
717
    List.map 
718
      (fun s -> rename_expr_annot f_node f_var f_const s) 
719
      nd.node_annot
720
  in
721
  {
722
    node_id = f_node nd.node_id;
723
    node_type = nd.node_type;
724
    node_clock = nd.node_clock;
725
    node_inputs = inputs;
726
    node_outputs = outputs;
727
    node_locals = locals;
728
    node_gencalls = gen_calls;
729
    node_checks = node_checks;
730
    node_asserts = node_asserts;
731
    node_stmts = node_stmts;
732
    node_dec_stateless = nd.node_dec_stateless;
733
    node_stateless = nd.node_stateless;
734
    node_spec = spec;
735
    node_annot = annot;
736
  }
737

    
738

    
739
let rename_const f_const c =
740
  { c with const_id = f_const c.const_id }
741

    
742
let rename_typedef f_var t =
743
  match t.tydef_desc with
744
  | Tydec_enum tags -> { t with tydef_desc = Tydec_enum (List.map f_var tags) }
745
  | _               -> t
746

    
747
let rename_prog f_node f_var f_const prog =
748
  List.rev (
749
    List.fold_left (fun accu top ->
750
      (match top.top_decl_desc with
751
      | Node nd -> 
752
	 { top with top_decl_desc = Node (rename_node f_node f_var f_const nd) }
753
      | Const c -> 
754
	 { top with top_decl_desc = Const (rename_const f_const c) }
755
      | TypeDef tdef ->
756
	 { top with top_decl_desc = TypeDef (rename_typedef f_var tdef) }
757
      | ImportedNode _
758
      | Open _       -> top)
759
      ::accu
760
) [] prog
761
		   )
762

    
763
(**********************************************************************)
764
(* Pretty printers *)
765

    
766
let pp_decl_type fmt tdecl =
767
  match tdecl.top_decl_desc with
768
  | Node nd ->
769
    fprintf fmt "%s: " nd.node_id;
770
    Utils.reset_names ();
771
    fprintf fmt "%a@ " Types.print_ty nd.node_type
772
  | ImportedNode ind ->
773
    fprintf fmt "%s: " ind.nodei_id;
774
    Utils.reset_names ();
775
    fprintf fmt "%a@ " Types.print_ty ind.nodei_type
776
  | Const _ | Open _ | TypeDef _ -> ()
777

    
778
let pp_prog_type fmt tdecl_list =
779
  Utils.fprintf_list ~sep:"" pp_decl_type fmt tdecl_list
780

    
781
let pp_decl_clock fmt cdecl =
782
  match cdecl.top_decl_desc with
783
  | Node nd ->
784
    fprintf fmt "%s: " nd.node_id;
785
    Utils.reset_names ();
786
    fprintf fmt "%a@ " Clocks.print_ck nd.node_clock
787
  | ImportedNode ind ->
788
    fprintf fmt "%s: " ind.nodei_id;
789
    Utils.reset_names ();
790
    fprintf fmt "%a@ " Clocks.print_ck ind.nodei_clock
791
  | Const _ | Open _ | TypeDef _ -> ()
792

    
793
let pp_prog_clock fmt prog =
794
  Utils.fprintf_list ~sep:"" pp_decl_clock fmt prog
795

    
796
let pp_error fmt = function
797
    Main_not_found ->
798
      fprintf fmt "Could not find the definition of main node %s.@."
799
	!Global.main_node
800
  | Main_wrong_kind ->
801
    fprintf fmt
802
      "Node %s does not correspond to a valid main node definition.@." 
803
      !Global.main_node 
804
  | No_main_specified ->
805
    fprintf fmt "No main node specified (use -node option)@."
806
  | Unbound_symbol sym ->
807
    fprintf fmt
808
      "%s is undefined.@."
809
      sym
810
  | Already_bound_symbol sym -> 
811
    fprintf fmt
812
      "%s is already defined.@."
813
      sym
814
  | Unknown_library sym ->
815
    fprintf fmt
816
      "impossible to load library %s.lusic.@.Please compile the corresponding interface or source file.@."
817
      sym
818
  | Wrong_number sym ->
819
    fprintf fmt
820
      "library %s.lusic has a different version number and may crash compiler.@.Please recompile the corresponding interface or source file.@."
821
      sym
822

    
823
(* filling node table with internal functions *)
824
let vdecls_of_typ_ck cpt ty =
825
  let loc = Location.dummy_loc in
826
  List.map
827
    (fun _ -> incr cpt;
828
              let name = sprintf "_var_%d" !cpt in
829
              mkvar_decl loc (name, mktyp loc Tydec_any, mkclock loc Ckdec_any, false, None))
830
    (Types.type_list_of_type ty)
831

    
832
let mk_internal_node id =
833
  let spec = None in
834
  let ty = Env.lookup_value Basic_library.type_env id in
835
  let ck = Env.lookup_value Basic_library.clock_env id in
836
  let (tin, tout) = Types.split_arrow ty in
837
  (*eprintf "internal fun %s: %d -> %d@." id (List.length (Types.type_list_of_type tin)) (List.length (Types.type_list_of_type tout));*)
838
  let cpt = ref (-1) in
839
  mktop
840
    (ImportedNode
841
       {nodei_id = id;
842
	nodei_type = ty;
843
	nodei_clock = ck;
844
	nodei_inputs = vdecls_of_typ_ck cpt tin;
845
	nodei_outputs = vdecls_of_typ_ck cpt tout;
846
	nodei_stateless = Types.get_static_value ty <> None;
847
	nodei_spec = spec;
848
	nodei_prototype = None;
849
       	nodei_in_lib = [];
850
       })
851

    
852
let add_internal_funs () =
853
  List.iter
854
    (fun id -> let nd = mk_internal_node id in Hashtbl.add node_table id nd)
855
    Basic_library.internal_funs
856

    
857

    
858

    
859
(* Replace any occurence of a var in vars_to_replace by its associated
860
   expression in defs until e does not contain any such variables *)
861
let rec substitute_expr vars_to_replace defs e =
862
  let se = substitute_expr vars_to_replace defs in
863
  { e with expr_desc = 
864
      let ed = e.expr_desc in
865
      match ed with
866
      | Expr_const _ -> ed
867
      | Expr_array el -> Expr_array (List.map se el)
868
      | Expr_access (e1, d) -> Expr_access (se e1, d)
869
      | Expr_power (e1, d) -> Expr_power (se e1, d)
870
      | Expr_tuple el -> Expr_tuple (List.map se el)
871
      | Expr_ite (c, t, e) -> Expr_ite (se c, se t, se e)
872
      | Expr_arrow (e1, e2)-> Expr_arrow (se e1, se e2) 
873
      | Expr_fby (e1, e2) -> Expr_fby (se e1, se e2)
874
      | Expr_pre e' -> Expr_pre (se e')
875
      | Expr_when (e', i, l)-> Expr_when (se e', i, l)
876
      | Expr_merge (i, hl) -> Expr_merge (i, List.map (fun (t, h) -> (t, se h)) hl)
877
      | Expr_appl (i, e', i') -> Expr_appl (i, se e', i')
878
      | Expr_ident i -> 
879
	if List.exists (fun v -> v.var_id = i) vars_to_replace then (
880
	  let eq_i eq = eq.eq_lhs = [i] in
881
	  if List.exists eq_i defs then
882
	    let sub = List.find eq_i defs in
883
	    let sub' = se sub.eq_rhs in
884
	    sub'.expr_desc
885
	  else 
886
	    assert false
887
	)
888
	else
889
	  ed
890

    
891
  }
892
(* FAUT IL RETIRER ?
893
  
894
 let rec expr_to_eexpr  expr =
895
   { eexpr_tag = expr.expr_tag;
896
     eexpr_desc = expr_desc_to_eexpr_desc expr.expr_desc;
897
     eexpr_type = expr.expr_type;
898
     eexpr_clock = expr.expr_clock;
899
     eexpr_loc = expr.expr_loc
900
   }
901
 and expr_desc_to_eexpr_desc expr_desc =
902
   let conv = expr_to_eexpr in
903
   match expr_desc with
904
   | Expr_const c -> EExpr_const (match c with
905
     | Const_int x -> EConst_int x 
906
     | Const_real x -> EConst_real x 
907
     | Const_float x -> EConst_float x 
908
     | Const_tag x -> EConst_tag x 
909
     | _ -> assert false
910

    
911
   )
912
   | Expr_ident i -> EExpr_ident i
913
   | Expr_tuple el -> EExpr_tuple (List.map conv el)
914

    
915
   | Expr_arrow (e1, e2)-> EExpr_arrow (conv e1, conv e2) 
916
   | Expr_fby (e1, e2) -> EExpr_fby (conv e1, conv e2)
917
   | Expr_pre e' -> EExpr_pre (conv e')
918
   | Expr_appl (i, e', i') -> 
919
     EExpr_appl 
920
       (i, conv e', match i' with None -> None | Some(id, _) -> Some id)
921

    
922
   | Expr_when _
923
   | Expr_merge _ -> assert false
924
   | Expr_array _ 
925
   | Expr_access _ 
926
   | Expr_power _  -> assert false
927
   | Expr_ite (c, t, e) -> assert false 
928
   | _ -> assert false
929

    
930
     *)
931
let rec get_expr_calls nodes e =
932
  let get_calls = get_expr_calls nodes in
933
  match e.expr_desc with
934
  | Expr_const _ 
935
   | Expr_ident _ -> Utils.ISet.empty
936
   | Expr_tuple el
937
   | Expr_array el -> List.fold_left (fun accu e -> Utils.ISet.union accu (get_calls e)) Utils.ISet.empty el
938
   | Expr_pre e1 
939
   | Expr_when (e1, _, _) 
940
   | Expr_access (e1, _) 
941
   | Expr_power (e1, _) -> get_calls e1
942
   | Expr_ite (c, t, e) -> Utils.ISet.union (Utils.ISet.union (get_calls c) (get_calls t)) (get_calls e) 
943
   | Expr_arrow (e1, e2) 
944
   | Expr_fby (e1, e2) -> Utils.ISet.union (get_calls e1) (get_calls e2)
945
   | Expr_merge (_, hl) -> List.fold_left (fun accu (_, h) -> Utils.ISet.union accu (get_calls h)) Utils.ISet.empty  hl
946
   | Expr_appl (i, e', i') -> 
947
     if Basic_library.is_expr_internal_fun e then 
948
       (get_calls e') 
949
     else
950
       let calls =  Utils.ISet.add i (get_calls e') in
951
       let test = (fun n -> match n.top_decl_desc with Node nd -> nd.node_id = i | _ -> false) in
952
       if List.exists test nodes then
953
	 match (List.find test nodes).top_decl_desc with
954
	 | Node nd -> Utils.ISet.union (get_node_calls nodes nd) calls
955
	 | _ -> assert false
956
       else 
957
	 calls
958

    
959
and get_eq_calls nodes eq =
960
  get_expr_calls nodes eq.eq_rhs
961
and get_node_calls nodes node =
962
  List.fold_left (fun accu eq -> Utils.ISet.union (get_eq_calls nodes eq) accu) Utils.ISet.empty (get_node_eqs node)
963

    
964
let rec get_expr_vars vars e =
965
  get_expr_desc_vars vars e.expr_desc
966
and get_expr_desc_vars vars expr_desc =
967
  match expr_desc with
968
  | Expr_const _ -> vars
969
  | Expr_ident x -> Utils.ISet.add x vars
970
  | Expr_tuple el
971
  | Expr_array el -> List.fold_left get_expr_vars vars el
972
  | Expr_pre e1 -> get_expr_vars vars e1
973
  | Expr_when (e1, c, _) -> get_expr_vars (Utils.ISet.add c vars) e1 
974
  | Expr_access (e1, d) 
975
  | Expr_power (e1, d)   -> List.fold_left get_expr_vars vars [e1; expr_of_dimension d]
976
  | Expr_ite (c, t, e) -> List.fold_left get_expr_vars vars [c; t; e]
977
  | Expr_arrow (e1, e2) 
978
  | Expr_fby (e1, e2) -> List.fold_left get_expr_vars vars [e1; e2]
979
  | Expr_merge (c, hl) -> List.fold_left (fun vars (_, h) -> get_expr_vars vars h) (Utils.ISet.add c vars) hl
980
  | Expr_appl (_, arg, None)   -> get_expr_vars vars arg
981
  | Expr_appl (_, arg, Some r) -> List.fold_left get_expr_vars vars [arg; r]
982

    
983

    
984
let rec expr_has_arrows e =
985
  expr_desc_has_arrows e.expr_desc
986
and expr_desc_has_arrows expr_desc =
987
  match expr_desc with
988
  | Expr_const _ 
989
  | Expr_ident _ -> false
990
  | Expr_tuple el
991
  | Expr_array el -> List.exists expr_has_arrows el
992
  | Expr_pre e1 
993
  | Expr_when (e1, _, _) 
994
  | Expr_access (e1, _) 
995
  | Expr_power (e1, _) -> expr_has_arrows e1
996
  | Expr_ite (c, t, e) -> List.exists expr_has_arrows [c; t; e]
997
  | Expr_arrow (e1, e2) 
998
  | Expr_fby (e1, e2) -> true
999
  | Expr_merge (_, hl) -> List.exists (fun (_, h) -> expr_has_arrows h) hl
1000
  | Expr_appl (i, e', i') -> expr_has_arrows e'
1001

    
1002
and eq_has_arrows eq =
1003
  expr_has_arrows eq.eq_rhs
1004
and node_has_arrows node =
1005
  List.exists (fun eq -> eq_has_arrows eq) (get_node_eqs node)
1006

    
1007

    
1008
let copy_var_decl vdecl =
1009
  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)
1010

    
1011
let copy_const cdecl =
1012
  { cdecl with const_type = Types.new_var () }
1013

    
1014
let copy_node nd =
1015
  { nd with
1016
    node_type     = Types.new_var ();
1017
    node_clock    = Clocks.new_var true;
1018
    node_inputs   = List.map copy_var_decl nd.node_inputs;
1019
    node_outputs  = List.map copy_var_decl nd.node_outputs;
1020
    node_locals   = List.map copy_var_decl nd.node_locals;
1021
    node_gencalls = [];
1022
    node_checks   = [];
1023
    node_stateless = None;
1024
  }
1025

    
1026
let copy_top top =
1027
  match top.top_decl_desc with
1028
  | Node nd -> { top with top_decl_desc = Node (copy_node nd)  }
1029
  | Const c -> { top with top_decl_desc = Const (copy_const c) }
1030
  | _       -> top
1031

    
1032
let copy_prog top_list =
1033
  List.map copy_top top_list
1034

    
1035
(* Local Variables: *)
1036
(* compile-command:"make -C .." *)
1037
(* End: *)
(13-13/51)