Project

General

Profile

Statistics
| Branch: | Tag: | Revision:

lustrec / src / corelang.ml @ df39e35a

History | View | Annotate | Download (32.4 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) =
45
  { var_id = id;
46
    var_orig = orig;
47
    var_dec_type = ty_dec;
48
    var_dec_clock = ck_dec;
49
    var_dec_const = is_const;
50
    var_type = Types.new_var ();
51
    var_clock = Clocks.new_var true;
52
    var_loc = loc }
53

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

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

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

    
80
let mkeq loc (lhs, rhs) =
81
  { eq_lhs = lhs;
82
    eq_rhs = rhs;
83
    eq_loc = loc }
84

    
85
let mkassert loc expr =
86
  { assert_loc = loc;
87
    assert_expr = expr
88
  }
89

    
90
let mktop_decl loc own itf d =
91
  { top_decl_desc = d; top_decl_loc = loc; top_decl_owner = own; top_decl_itf = itf }
92

    
93
let mkpredef_call loc funname args =
94
  mkexpr loc (Expr_appl (funname, mkexpr loc (Expr_tuple args), None))
95

    
96

    
97
let const_of_top top_decl =
98
  match top_decl.top_decl_desc with
99
  | Const c -> c
100
  | _ -> assert false
101

    
102
let node_of_top top_decl =
103
  match top_decl.top_decl_desc with
104
  | Node nd -> nd
105
  | _ -> assert false
106

    
107
let imported_node_of_top top_decl =
108
  match top_decl.top_decl_desc with
109
  | ImportedNode ind -> ind
110
  | _ -> assert false
111

    
112
let typedef_of_top top_decl =
113
  match top_decl.top_decl_desc with
114
  | TypeDef tdef -> tdef
115
  | _ -> assert false
116

    
117
let dependency_of_top top_decl =
118
  match top_decl.top_decl_desc with
119
  | Open (local, dep) -> (local, dep)
120
  | _ -> assert false
121

    
122
let consts_of_enum_type top_decl =
123
  match top_decl.top_decl_desc with
124
  | TypeDef tdef ->
125
    (match tdef.tydef_desc with
126
     | 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
127
     | _               -> [])
128
  | _ -> assert false
129

    
130
(************************************************************)
131
(*   Eexpr functions *)
132
(************************************************************)
133

    
134
let merge_node_annot ann1 ann2 =
135
  { requires = ann1.requires @ ann2.requires;
136
    ensures = ann1.ensures @ ann2.ensures;
137
    behaviors = ann1.behaviors @ ann2.behaviors;
138
    spec_loc = ann1.spec_loc
139
  }
140

    
141
let mkeexpr loc expr =
142
  { eexpr_tag = Utils.new_tag ();
143
    eexpr_qfexpr = expr;
144
    eexpr_quantifiers = [];
145
    eexpr_type = Types.new_var ();
146
    eexpr_clock = Clocks.new_var true;
147
    eexpr_normalized = None;
148
    eexpr_loc = loc }
149

    
150
let extend_eexpr q e = { e with eexpr_quantifiers = q@e.eexpr_quantifiers }
151

    
152
(*
153
let mkepredef_call loc funname args =
154
  mkeexpr loc (EExpr_appl (funname, mkeexpr loc (EExpr_tuple args), None))
155

    
156
let mkepredef_unary_call loc funname arg =
157
  mkeexpr loc (EExpr_appl (funname, arg, None))
158
*)
159

    
160
let merge_expr_annot ann1 ann2 =
161
  match ann1, ann2 with
162
    | None, None -> assert false
163
    | Some _, None -> ann1
164
    | None, Some _ -> ann2
165
    | Some ann1, Some ann2 -> Some {
166
      annots = ann1.annots @ ann2.annots;
167
      annot_loc = ann1.annot_loc
168
    }
169

    
170
let update_expr_annot e annot =
171
  { e with expr_annot = merge_expr_annot e.expr_annot (Some annot) }
172

    
173

    
174
(***********************************************************)
175
(* Fast access to nodes, by name *)
176
let (node_table : (ident, top_decl) Hashtbl.t) = Hashtbl.create 30
177
let consts_table = Hashtbl.create 30
178

    
179
let print_node_table fmt () =
180
  begin
181
    Format.fprintf fmt "{ /* node table */@.";
182
    Hashtbl.iter (fun id nd ->
183
      Format.fprintf fmt "%s |-> %a"
184
	id
185
	Printers.pp_short_decl nd
186
    ) node_table;
187
    Format.fprintf fmt "}@."
188
  end
189

    
190
let print_consts_table fmt () =
191
  begin
192
    Format.fprintf fmt "{ /* consts table */@.";
193
    Hashtbl.iter (fun id const ->
194
      Format.fprintf fmt "%s |-> %a"
195
	id
196
	Printers.pp_const_decl (const_of_top const)
197
    ) consts_table;
198
    Format.fprintf fmt "}@."
199
  end
200

    
201
let node_name td =
202
    match td.top_decl_desc with 
203
    | Node nd         -> nd.node_id
204
    | ImportedNode nd -> nd.nodei_id
205
    | _ -> assert false
206

    
207
let is_generic_node td =
208
  match td.top_decl_desc with 
209
  | Node nd         -> List.exists (fun v -> v.var_dec_const) nd.node_inputs
210
  | ImportedNode nd -> List.exists (fun v -> v.var_dec_const) nd.nodei_inputs
211
  | _ -> assert false
212

    
213
let node_inputs td =
214
  match td.top_decl_desc with 
215
  | Node nd         -> nd.node_inputs
216
  | ImportedNode nd -> nd.nodei_inputs
217
  | _ -> assert false
218

    
219
let node_from_name id =
220
  try
221
    Hashtbl.find node_table id
222
  with Not_found -> (Format.eprintf "Unable to find any node named %s@ @?" id;
223
		     assert false)
224

    
225
let is_imported_node td =
226
  match td.top_decl_desc with 
227
  | Node nd         -> false
228
  | ImportedNode nd -> true
229
  | _ -> assert false
230

    
231

    
232
(* alias and type definition table *)
233

    
234
let top_int_type = mktop_decl Location.dummy_loc Version.prefix false (TypeDef {tydef_id = "int"; tydef_desc = Tydec_int})
235
let top_bool_type = mktop_decl Location.dummy_loc Version.prefix false (TypeDef {tydef_id = "bool"; tydef_desc = Tydec_bool})
236
let top_float_type = mktop_decl Location.dummy_loc Version.prefix false (TypeDef {tydef_id = "float"; tydef_desc = Tydec_float})
237
let top_real_type = mktop_decl Location.dummy_loc Version.prefix false (TypeDef {tydef_id = "real"; tydef_desc = Tydec_real})
238

    
239
let type_table =
240
  Utils.create_hashtable 20 [
241
    Tydec_int  , top_int_type;
242
    Tydec_bool , top_bool_type;
243
    Tydec_float, top_float_type;
244
    Tydec_real , top_real_type
245
  ]
246

    
247
let print_type_table fmt () =
248
  begin
249
    Format.fprintf fmt "{ /* type table */@.";
250
    Hashtbl.iter (fun tydec tdef ->
251
      Format.fprintf fmt "%a |-> %a"
252
	Printers.pp_var_type_dec_desc tydec
253
	Printers.pp_typedef (typedef_of_top tdef)
254
    ) type_table;
255
    Format.fprintf fmt "}@."
256
  end
257

    
258
let rec is_user_type typ =
259
  match typ with
260
  | Tydec_int | Tydec_bool | Tydec_real 
261
  | Tydec_float | Tydec_any | Tydec_const _ -> false
262
  | Tydec_clock typ' -> is_user_type typ'
263
  | _ -> true
264

    
265
let get_repr_type typ =
266
  let typ_def = (typedef_of_top (Hashtbl.find type_table typ)).tydef_desc in
267
  if is_user_type typ_def then typ else typ_def
268

    
269
let rec coretype_equal ty1 ty2 =
270
  let res =
271
  match ty1, ty2 with
272
  | Tydec_any           , _
273
  | _                   , Tydec_any             -> assert false
274
  | Tydec_const _       , Tydec_const _         -> get_repr_type ty1 = get_repr_type ty2
275
  | Tydec_const _       , _                     -> let ty1' = (typedef_of_top (Hashtbl.find type_table ty1)).tydef_desc
276
	       					   in (not (is_user_type ty1')) && coretype_equal ty1' ty2
277
  | _                   , Tydec_const _         -> coretype_equal ty2 ty1
278
  | Tydec_int           , Tydec_int
279
  | Tydec_real          , Tydec_real
280
  | Tydec_float         , Tydec_float
281
  | Tydec_bool          , Tydec_bool            -> true
282
  | Tydec_clock ty1     , Tydec_clock ty2       -> coretype_equal ty1 ty2
283
  | Tydec_array (d1,ty1), Tydec_array (d2, ty2) -> Dimension.is_eq_dimension d1 d2 && coretype_equal ty1 ty2
284
  | Tydec_enum tl1      , Tydec_enum tl2        -> List.sort compare tl1 = List.sort compare tl2
285
  | Tydec_struct fl1    , Tydec_struct fl2      ->
286
       List.length fl1 = List.length fl2
287
    && List.for_all2 (fun (f1, t1) (f2, t2) -> f1 = f2 && coretype_equal t1 t2)
288
      (List.sort (fun (f1,_) (f2,_) -> compare f1 f2) fl1)
289
      (List.sort (fun (f1,_) (f2,_) -> compare f1 f2) fl2)
290
  | _                                  -> false
291
  in ((*Format.eprintf "coretype_equal %a %a = %B@." Printers.pp_var_type_dec_desc ty1 Printers.pp_var_type_dec_desc ty2 res;*) res)
292

    
293
let tag_true = "true"
294
let tag_false = "false"
295

    
296
let const_is_bool c =
297
 match c with
298
 | Const_tag t -> t = tag_true || t = tag_false
299
 | _           -> false
300

    
301
(* Computes the negation of a boolean constant *)
302
let const_negation c =
303
  assert (const_is_bool c);
304
  match c with
305
  | Const_tag t when t = tag_true  -> Const_tag tag_false
306
  | _                              -> Const_tag tag_true
307

    
308
let const_or c1 c2 =
309
  assert (const_is_bool c1 && const_is_bool c2);
310
  match c1, c2 with
311
  | Const_tag t1, _            when t1 = tag_true -> c1
312
  | _           , Const_tag t2 when t2 = tag_true -> c2
313
  | _                                             -> Const_tag tag_false
314

    
315
let const_and c1 c2 =
316
  assert (const_is_bool c1 && const_is_bool c2);
317
  match c1, c2 with
318
  | Const_tag t1, _            when t1 = tag_false -> c1
319
  | _           , Const_tag t2 when t2 = tag_false -> c2
320
  | _                                              -> Const_tag tag_true
321

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

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

    
335
(* To guarantee uniqueness of tags in enum types *)
336
let tag_table =
337
  Utils.create_hashtable 20 [
338
   tag_true, top_bool_type;
339
   tag_false, top_bool_type
340
  ]
341

    
342
(* To guarantee uniqueness of fields in struct types *)
343
let field_table =
344
  Utils.create_hashtable 20 [
345
  ]
346

    
347
let get_enum_type_tags cty =
348
(*Format.eprintf "get_enum_type_tags %a@." Printers.pp_var_type_dec_desc cty;*)
349
 match cty with
350
 | Tydec_bool    -> [tag_true; tag_false]
351
 | Tydec_const _ -> (match (typedef_of_top (Hashtbl.find type_table cty)).tydef_desc with
352
                     | Tydec_enum tl -> tl
353
                     | _             -> assert false)
354
 | _            -> assert false
355

    
356
let get_struct_type_fields cty =
357
 match cty with
358
 | Tydec_const _ -> (match (typedef_of_top (Hashtbl.find type_table cty)).tydef_desc with
359
                     | Tydec_struct fl -> fl
360
                     | _               -> assert false)
361
 | _            -> assert false
362

    
363
let const_of_bool b =
364
 Const_tag (if b then tag_true else tag_false)
365

    
366
(* let get_const c = snd (Hashtbl.find consts_table c) *)
367

    
368
let ident_of_expr expr =
369
 match expr.expr_desc with
370
 | Expr_ident id -> id
371
 | _             -> assert false
372

    
373
(* Caution, returns an untyped and unclocked expression *)
374
let expr_of_ident id loc =
375
  {expr_tag = Utils.new_tag ();
376
   expr_desc = Expr_ident id;
377
   expr_type = Types.new_var ();
378
   expr_clock = Clocks.new_var true;
379
   expr_delay = Delay.new_var ();
380
   expr_loc = loc;
381
   expr_annot = None}
382

    
383
let is_tuple_expr expr =
384
 match expr.expr_desc with
385
  | Expr_tuple _ -> true
386
  | _            -> false
387

    
388
let expr_list_of_expr expr =
389
  match expr.expr_desc with
390
  | Expr_tuple elist -> elist
391
  | _                -> [expr]
392

    
393
let expr_of_expr_list loc elist =
394
 match elist with
395
 | [t]  -> { t with expr_loc = loc }
396
 | t::_ ->
397
    let tlist = List.map (fun e -> e.expr_type) elist in
398
    let clist = List.map (fun e -> e.expr_clock) elist in
399
    { t with expr_desc = Expr_tuple elist;
400
	     expr_type = Type_predef.type_tuple tlist;
401
	     expr_clock = Clock_predef.ck_tuple clist;
402
	     expr_tag = Utils.new_tag ();
403
	     expr_loc = loc }
404
 | _    -> assert false
405

    
406
let call_of_expr expr =
407
 match expr.expr_desc with
408
 | Expr_appl (f, args, r) -> (f, expr_list_of_expr args, r)
409
 | _                      -> assert false
410

    
411
(* Conversion from dimension expr to standard expr, for the purpose of printing, typing, etc... *)
412
let rec expr_of_dimension dim =
413
 match dim.dim_desc with
414
 | Dbool b        ->
415
     mkexpr dim.dim_loc (Expr_const (const_of_bool b))
416
 | Dint i         ->
417
     mkexpr dim.dim_loc (Expr_const (Const_int i))
418
 | Dident id      ->
419
     mkexpr dim.dim_loc (Expr_ident id)
420
 | Dite (c, t, e) ->
421
     mkexpr dim.dim_loc (Expr_ite (expr_of_dimension c, expr_of_dimension t, expr_of_dimension e))
422
 | Dappl (id, args) ->
423
     mkexpr dim.dim_loc (Expr_appl (id, expr_of_expr_list dim.dim_loc (List.map expr_of_dimension args), None))
424
 | Dlink dim'       -> expr_of_dimension dim'
425
 | Dvar
426
 | Dunivar          -> (Format.eprintf "internal error: expr_of_dimension %a@." Dimension.pp_dimension dim;
427
			assert false)
428

    
429
let dimension_of_const loc const =
430
 match const with
431
 | Const_int i                                    -> mkdim_int loc i
432
 | Const_tag t when t = tag_true || t = tag_false -> mkdim_bool loc (t = tag_true)
433
 | _                                              -> raise InvalidDimension
434

    
435
(* Conversion from standard expr to dimension expr, for the purpose of injecting static call arguments 
436
   into dimension expressions *)
437
let rec dimension_of_expr expr =
438
  match expr.expr_desc with
439
  | Expr_const c  -> dimension_of_const expr.expr_loc c
440
  | Expr_ident id -> mkdim_ident expr.expr_loc id
441
  | Expr_appl (f, args, None) when Basic_library.is_internal_fun f ->
442
      let k = Types.get_static_value (Env.lookup_value Basic_library.type_env f) in
443
      if k = None then raise InvalidDimension;
444
      mkdim_appl expr.expr_loc f (List.map dimension_of_expr (expr_list_of_expr args))
445
  | Expr_ite (i, t, e)        ->
446
      mkdim_ite expr.expr_loc (dimension_of_expr i) (dimension_of_expr t) (dimension_of_expr e)
447
  | _ -> raise InvalidDimension (* not a simple dimension expression *)
448

    
449

    
450
let sort_handlers hl =
451
 List.sort (fun (t, _) (t', _) -> compare t t') hl
452

    
453
let rec is_eq_expr e1 e2 = match e1.expr_desc, e2.expr_desc with
454
  | Expr_const c1, Expr_const c2 -> c1 = c2
455
  | Expr_ident i1, Expr_ident i2 -> i1 = i2
456
  | Expr_array el1, Expr_array el2 
457
  | Expr_tuple el1, Expr_tuple el2 -> 
458
    List.length el1 = List.length el2 && List.for_all2 is_eq_expr el1 el2 
459
  | Expr_arrow (e1, e2), Expr_arrow (e1', e2') -> is_eq_expr e1 e1' && is_eq_expr e2 e2'
460
  | Expr_fby (e1,e2), Expr_fby (e1',e2') -> is_eq_expr e1 e1' && is_eq_expr e2 e2'
461
  | 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
462
  (* | Expr_concat (e1,e2), Expr_concat (e1',e2') -> is_eq_expr e1 e1' && is_eq_expr e2 e2' *)
463
  (* | Expr_tail e, Expr_tail e' -> is_eq_expr e e' *)
464
  | Expr_pre e, Expr_pre e' -> is_eq_expr e e'
465
  | Expr_when (e, i, l), Expr_when (e', i', l') -> l=l' && i=i' && is_eq_expr e e'
466
  | 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')
467
  | Expr_appl (i, e, r), Expr_appl (i', e', r') -> i=i' && r=r' && is_eq_expr e e'
468
  | Expr_power (e1, i1), Expr_power (e2, i2)
469
  | Expr_access (e1, i1), Expr_access (e2, i2) -> is_eq_expr e1 e2 && is_eq_expr (expr_of_dimension i1) (expr_of_dimension i2)
470
  | _ -> false
471

    
472
let get_node_vars nd =
473
  nd.node_inputs @ nd.node_locals @ nd.node_outputs
474

    
475
let get_var id var_list =
476
 List.find (fun v -> v.var_id = id) var_list
477

    
478
let get_node_var id node =
479
  get_var id (get_node_vars node)
480

    
481
let get_node_eqs =
482
  let get_eqs stmts =
483
    List.fold_right
484
      (fun stmt res ->
485
	match stmt with
486
	| Eq eq -> eq :: res
487
	| Aut _ -> assert false)
488
      stmts
489
      [] in
490
  let table_eqs = Hashtbl.create 23 in
491
  (fun nd ->
492
    try
493
      let (old, res) = Hashtbl.find table_eqs nd.node_id
494
      in if old == nd.node_stmts then res else raise Not_found
495
    with Not_found -> 
496
      let res = get_eqs nd.node_stmts in
497
      begin
498
	Hashtbl.replace table_eqs nd.node_id (nd.node_stmts, res);
499
	res
500
      end)
501

    
502
let get_node_eq id node =
503
 List.find (fun eq -> List.mem id eq.eq_lhs) (get_node_eqs node)
504

    
505
let get_nodes prog = 
506
  List.fold_left (
507
    fun nodes decl ->
508
      match decl.top_decl_desc with
509
	| Node _ -> decl::nodes
510
	| Const _ | ImportedNode _ | Open _ | TypeDef _ -> nodes  
511
  ) [] prog
512

    
513
let get_imported_nodes prog = 
514
  List.fold_left (
515
    fun nodes decl ->
516
      match decl.top_decl_desc with
517
	| ImportedNode _ -> decl::nodes
518
	| Const _ | Node _ | Open _ | TypeDef _-> nodes  
519
  ) [] prog
520

    
521
let get_consts prog = 
522
  List.fold_right (
523
    fun decl consts ->
524
      match decl.top_decl_desc with
525
	| Const _ -> decl::consts
526
	| Node _ | ImportedNode _ | Open _ | TypeDef _ -> consts  
527
  ) prog []
528

    
529
let get_typedefs prog = 
530
  List.fold_right (
531
    fun decl types ->
532
      match decl.top_decl_desc with
533
	| TypeDef _ -> decl::types
534
	| Node _ | ImportedNode _ | Open _ | Const _ -> types  
535
  ) prog []
536

    
537
let get_dependencies prog =
538
  List.fold_right (
539
    fun decl deps ->
540
      match decl.top_decl_desc with
541
	| Open _ -> decl::deps
542
	| Node _ | ImportedNode _ | TypeDef _ | Const _ -> deps  
543
  ) prog []
544

    
545
let get_node_interface nd =
546
 {nodei_id = nd.node_id;
547
  nodei_type = nd.node_type;
548
  nodei_clock = nd.node_clock;
549
  nodei_inputs = nd.node_inputs;
550
  nodei_outputs = nd.node_outputs;
551
  nodei_stateless = nd.node_dec_stateless;
552
  nodei_spec = nd.node_spec;
553
  nodei_prototype = None;
554
  nodei_in_lib = None;
555
 }
556

    
557
(************************************************************************)
558
(*        Renaming                                                      *)
559

    
560
(* applies the renaming function [fvar] to all variables of expression [expr] *)
561
 let rec expr_replace_var fvar expr =
562
  { expr with expr_desc = expr_desc_replace_var fvar expr.expr_desc }
563

    
564
 and expr_desc_replace_var fvar expr_desc =
565
   match expr_desc with
566
   | Expr_const _ -> expr_desc
567
   | Expr_ident i -> Expr_ident (fvar i)
568
   | Expr_array el -> Expr_array (List.map (expr_replace_var fvar) el)
569
   | Expr_access (e1, d) -> Expr_access (expr_replace_var fvar e1, d)
570
   | Expr_power (e1, d) -> Expr_power (expr_replace_var fvar e1, d)
571
   | Expr_tuple el -> Expr_tuple (List.map (expr_replace_var fvar) el)
572
   | Expr_ite (c, t, e) -> Expr_ite (expr_replace_var fvar c, expr_replace_var fvar t, expr_replace_var fvar e)
573
   | Expr_arrow (e1, e2)-> Expr_arrow (expr_replace_var fvar e1, expr_replace_var fvar e2) 
574
   | Expr_fby (e1, e2) -> Expr_fby (expr_replace_var fvar e1, expr_replace_var fvar e2)
575
   | Expr_pre e' -> Expr_pre (expr_replace_var fvar e')
576
   | Expr_when (e', i, l)-> Expr_when (expr_replace_var fvar e', fvar i, l)
577
   | Expr_merge (i, hl) -> Expr_merge (fvar i, List.map (fun (t, h) -> (t, expr_replace_var fvar h)) hl)
578
   | Expr_appl (i, e', i') -> Expr_appl (i, expr_replace_var fvar e', Utils.option_map (expr_replace_var fvar) i')
579

    
580
(* Applies the renaming function [fvar] to every rhs
581
   only when the corresponding lhs satisfies predicate [pvar] *)
582
 let eq_replace_rhs_var pvar fvar eq =
583
   let pvar l = List.exists pvar l in
584
   let rec replace lhs rhs =
585
     { rhs with expr_desc = replace_desc lhs rhs.expr_desc }
586
   and replace_desc lhs rhs_desc =
587
     match lhs with
588
     | []  -> assert false
589
     | [_] -> if pvar lhs then expr_desc_replace_var fvar rhs_desc else rhs_desc
590
     | _   ->
591
       (match rhs_desc with
592
       | Expr_tuple tl ->
593
	 Expr_tuple (List.map2 (fun v e -> replace [v] e) lhs tl)
594
       | Expr_appl (f, arg, None) when Basic_library.is_internal_fun f ->
595
	 let args = expr_list_of_expr arg in
596
	 Expr_appl (f, expr_of_expr_list arg.expr_loc (List.map (replace lhs) args), None)
597
       | Expr_array _
598
       | Expr_access _
599
       | Expr_power _
600
       | Expr_const _
601
       | Expr_ident _
602
       | Expr_appl _   ->
603
	 if pvar lhs
604
	 then expr_desc_replace_var fvar rhs_desc
605
	 else rhs_desc
606
       | Expr_ite (c, t, e)   -> Expr_ite (replace lhs c, replace lhs t, replace lhs e)
607
       | Expr_arrow (e1, e2)  -> Expr_arrow (replace lhs e1, replace lhs e2) 
608
       | Expr_fby (e1, e2)    -> Expr_fby (replace lhs e1, replace lhs e2)
609
       | Expr_pre e'          -> Expr_pre (replace lhs e')
610
       | Expr_when (e', i, l) -> let i' = if pvar lhs then fvar i else i
611
				 in Expr_when (replace lhs e', i', l)
612
       | Expr_merge (i, hl)   -> let i' = if pvar lhs then fvar i else i
613
				 in Expr_merge (i', List.map (fun (t, h) -> (t, replace lhs h)) hl)
614
       )
615
   in { eq with eq_rhs = replace eq.eq_lhs eq.eq_rhs }
616

    
617

    
618
 let rec rename_expr  f_node f_var f_const expr =
619
   { expr with expr_desc = rename_expr_desc f_node f_var f_const expr.expr_desc }
620
 and rename_expr_desc f_node f_var f_const expr_desc =
621
   let re = rename_expr  f_node f_var f_const in
622
   match expr_desc with
623
   | Expr_const _ -> expr_desc
624
   | Expr_ident i -> Expr_ident (f_var i)
625
   | Expr_array el -> Expr_array (List.map re el)
626
   | Expr_access (e1, d) -> Expr_access (re e1, d)
627
   | Expr_power (e1, d) -> Expr_power (re e1, d)
628
   | Expr_tuple el -> Expr_tuple (List.map re el)
629
   | Expr_ite (c, t, e) -> Expr_ite (re c, re t, re e)
630
   | Expr_arrow (e1, e2)-> Expr_arrow (re e1, re e2) 
631
   | Expr_fby (e1, e2) -> Expr_fby (re e1, re e2)
632
   | Expr_pre e' -> Expr_pre (re e')
633
   | Expr_when (e', i, l)-> Expr_when (re e', f_var i, l)
634
   | Expr_merge (i, hl) -> 
635
     Expr_merge (f_var i, List.map (fun (t, h) -> (t, re h)) hl)
636
   | Expr_appl (i, e', i') -> 
637
     Expr_appl (f_node i, re e', Utils.option_map re i')
638
  
639
 let rename_node_annot f_node f_var f_const expr  =
640
   expr
641
 (* TODO assert false *)
642

    
643
 let rename_expr_annot f_node f_var f_const annot =
644
   annot
645
 (* TODO assert false *)
646

    
647
let rename_node f_node f_var f_const nd =
648
  let rename_var v = { v with var_id = f_var v.var_id } in
649
  let rename_eq eq = { eq with
650
      eq_lhs = List.map f_var eq.eq_lhs; 
651
      eq_rhs = rename_expr f_node f_var f_const eq.eq_rhs
652
    } 
653
  in
654
  let inputs = List.map rename_var nd.node_inputs in
655
  let outputs = List.map rename_var nd.node_outputs in
656
  let locals = List.map rename_var nd.node_locals in
657
  let gen_calls = List.map (rename_expr f_node f_var f_const) nd.node_gencalls in
658
  let node_checks = List.map (Dimension.expr_replace_var f_var)  nd.node_checks in
659
  let node_asserts = List.map 
660
    (fun a -> 
661
      {a with assert_expr = 
662
	  let expr = a.assert_expr in
663
	  rename_expr f_node f_var f_const expr})
664
    nd.node_asserts
665
  in
666
  let node_stmts = List.map (fun eq -> Eq (rename_eq eq)) (get_node_eqs nd) in
667
  let spec = 
668
    Utils.option_map 
669
      (fun s -> rename_node_annot f_node f_var f_const s) 
670
      nd.node_spec 
671
  in
672
  let annot =
673
    List.map 
674
      (fun s -> rename_expr_annot f_node f_var f_const s) 
675
      nd.node_annot
676
  in
677
  {
678
    node_id = f_node nd.node_id;
679
    node_type = nd.node_type;
680
    node_clock = nd.node_clock;
681
    node_inputs = inputs;
682
    node_outputs = outputs;
683
    node_locals = locals;
684
    node_gencalls = gen_calls;
685
    node_checks = node_checks;
686
    node_asserts = node_asserts;
687
    node_stmts = node_stmts;
688
    node_dec_stateless = nd.node_dec_stateless;
689
    node_stateless = nd.node_stateless;
690
    node_spec = spec;
691
    node_annot = annot;
692
  }
693

    
694

    
695
let rename_const f_const c =
696
  { c with const_id = f_const c.const_id }
697

    
698
let rename_typedef f_var t =
699
  match t.tydef_desc with
700
  | Tydec_enum tags -> { t with tydef_desc = Tydec_enum (List.map f_var tags) }
701
  | _               -> t
702

    
703
let rename_prog f_node f_var f_const prog =
704
  List.rev (
705
    List.fold_left (fun accu top ->
706
      (match top.top_decl_desc with
707
      | Node nd -> 
708
	 { top with top_decl_desc = Node (rename_node f_node f_var f_const nd) }
709
      | Const c -> 
710
	 { top with top_decl_desc = Const (rename_const f_const c) }
711
      | TypeDef tdef ->
712
	 { top with top_decl_desc = TypeDef (rename_typedef f_var tdef) }
713
      | ImportedNode _
714
      | Open _       -> top)
715
      ::accu
716
) [] prog
717
		   )
718

    
719
(**********************************************************************)
720
(* Pretty printers *)
721

    
722
let pp_decl_type fmt tdecl =
723
  match tdecl.top_decl_desc with
724
  | Node nd ->
725
    fprintf fmt "%s: " nd.node_id;
726
    Utils.reset_names ();
727
    fprintf fmt "%a@ " Types.print_ty nd.node_type
728
  | ImportedNode ind ->
729
    fprintf fmt "%s: " ind.nodei_id;
730
    Utils.reset_names ();
731
    fprintf fmt "%a@ " Types.print_ty ind.nodei_type
732
  | Const _ | Open _ | TypeDef _ -> ()
733

    
734
let pp_prog_type fmt tdecl_list =
735
  Utils.fprintf_list ~sep:"" pp_decl_type fmt tdecl_list
736

    
737
let pp_decl_clock fmt cdecl =
738
  match cdecl.top_decl_desc with
739
  | Node nd ->
740
    fprintf fmt "%s: " nd.node_id;
741
    Utils.reset_names ();
742
    fprintf fmt "%a@ " Clocks.print_ck nd.node_clock
743
  | ImportedNode ind ->
744
    fprintf fmt "%s: " ind.nodei_id;
745
    Utils.reset_names ();
746
    fprintf fmt "%a@ " Clocks.print_ck ind.nodei_clock
747
  | Const _ | Open _ | TypeDef _ -> ()
748

    
749
let pp_prog_clock fmt prog =
750
  Utils.fprintf_list ~sep:"" pp_decl_clock fmt prog
751

    
752
let pp_error fmt = function
753
    Main_not_found ->
754
      fprintf fmt "Cannot compile node %s: could not find the node definition.@."
755
	!Options.main_node
756
  | Main_wrong_kind ->
757
    fprintf fmt
758
      "Name %s does not correspond to a (non-imported) node definition.@." 
759
      !Options.main_node
760
  | No_main_specified ->
761
    fprintf fmt "No main node specified@."
762
  | Unbound_symbol sym ->
763
    fprintf fmt
764
      "%s is undefined.@."
765
      sym
766
  | Already_bound_symbol sym -> 
767
    fprintf fmt
768
      "%s is already defined.@."
769
      sym
770
  | Unknown_library sym ->
771
    fprintf fmt
772
      "impossible to load library %s.@."
773
      sym
774

    
775
(* filling node table with internal functions *)
776
let vdecls_of_typ_ck cpt ty =
777
  let loc = Location.dummy_loc in
778
  List.map
779
    (fun _ -> incr cpt;
780
              let name = sprintf "_var_%d" !cpt in
781
              mkvar_decl loc (name, mktyp loc Tydec_any, mkclock loc Ckdec_any, false))
782
    (Types.type_list_of_type ty)
783

    
784
let mk_internal_node id =
785
  let spec = None in
786
  let ty = Env.lookup_value Basic_library.type_env id in
787
  let ck = Env.lookup_value Basic_library.clock_env id in
788
  let (tin, tout) = Types.split_arrow ty in
789
  (*eprintf "internal fun %s: %d -> %d@." id (List.length (Types.type_list_of_type tin)) (List.length (Types.type_list_of_type tout));*)
790
  let cpt = ref (-1) in
791
  mktop_decl Location.dummy_loc Version.prefix false
792
    (ImportedNode
793
       {nodei_id = id;
794
	nodei_type = ty;
795
	nodei_clock = ck;
796
	nodei_inputs = vdecls_of_typ_ck cpt tin;
797
	nodei_outputs = vdecls_of_typ_ck cpt tout;
798
	nodei_stateless = Types.get_static_value ty <> None;
799
	nodei_spec = spec;
800
	nodei_prototype = None;
801
       	nodei_in_lib = None;
802
       })
803

    
804
let add_internal_funs () =
805
  List.iter
806
    (fun id -> let nd = mk_internal_node id in Hashtbl.add node_table id nd)
807
    Basic_library.internal_funs
808

    
809

    
810

    
811
(* Replace any occurence of a var in vars_to_replace by its associated
812
   expression in defs until e does not contain any such variables *)
813
let rec substitute_expr vars_to_replace defs e =
814
  let se = substitute_expr vars_to_replace defs in
815
  { e with expr_desc = 
816
      let ed = e.expr_desc in
817
      match ed with
818
      | Expr_const _ -> ed
819
      | Expr_array el -> Expr_array (List.map se el)
820
      | Expr_access (e1, d) -> Expr_access (se e1, d)
821
      | Expr_power (e1, d) -> Expr_power (se e1, d)
822
      | Expr_tuple el -> Expr_tuple (List.map se el)
823
      | Expr_ite (c, t, e) -> Expr_ite (se c, se t, se e)
824
      | Expr_arrow (e1, e2)-> Expr_arrow (se e1, se e2) 
825
      | Expr_fby (e1, e2) -> Expr_fby (se e1, se e2)
826
      | Expr_pre e' -> Expr_pre (se e')
827
      | Expr_when (e', i, l)-> Expr_when (se e', i, l)
828
      | Expr_merge (i, hl) -> Expr_merge (i, List.map (fun (t, h) -> (t, se h)) hl)
829
      | Expr_appl (i, e', i') -> Expr_appl (i, se e', i')
830
      | Expr_ident i -> 
831
	if List.exists (fun v -> v.var_id = i) vars_to_replace then (
832
	  let eq_i eq = eq.eq_lhs = [i] in
833
	  if List.exists eq_i defs then
834
	    let sub = List.find eq_i defs in
835
	    let sub' = se sub.eq_rhs in
836
	    sub'.expr_desc
837
	  else 
838
	    assert false
839
	)
840
	else
841
	  ed
842

    
843
  }
844
(* FAUT IL RETIRER ?
845
  
846
 let rec expr_to_eexpr  expr =
847
   { eexpr_tag = expr.expr_tag;
848
     eexpr_desc = expr_desc_to_eexpr_desc expr.expr_desc;
849
     eexpr_type = expr.expr_type;
850
     eexpr_clock = expr.expr_clock;
851
     eexpr_loc = expr.expr_loc
852
   }
853
 and expr_desc_to_eexpr_desc expr_desc =
854
   let conv = expr_to_eexpr in
855
   match expr_desc with
856
   | Expr_const c -> EExpr_const (match c with
857
     | Const_int x -> EConst_int x 
858
     | Const_real x -> EConst_real x 
859
     | Const_float x -> EConst_float x 
860
     | Const_tag x -> EConst_tag x 
861
     | _ -> assert false
862

    
863
   )
864
   | Expr_ident i -> EExpr_ident i
865
   | Expr_tuple el -> EExpr_tuple (List.map conv el)
866

    
867
   | Expr_arrow (e1, e2)-> EExpr_arrow (conv e1, conv e2) 
868
   | Expr_fby (e1, e2) -> EExpr_fby (conv e1, conv e2)
869
   | Expr_pre e' -> EExpr_pre (conv e')
870
   | Expr_appl (i, e', i') -> 
871
     EExpr_appl 
872
       (i, conv e', match i' with None -> None | Some(id, _) -> Some id)
873

    
874
   | Expr_when _
875
   | Expr_merge _ -> assert false
876
   | Expr_array _ 
877
   | Expr_access _ 
878
   | Expr_power _  -> assert false
879
   | Expr_ite (c, t, e) -> assert false 
880
   | _ -> assert false
881

    
882
     *)
883
let rec get_expr_calls nodes e =
884
  get_calls_expr_desc nodes e.expr_desc
885
and get_calls_expr_desc nodes expr_desc =
886
  let get_calls = get_expr_calls nodes in
887
  match expr_desc with
888
  | Expr_const _ 
889
   | Expr_ident _ -> Utils.ISet.empty
890
   | Expr_tuple el
891
   | Expr_array el -> List.fold_left (fun accu e -> Utils.ISet.union accu (get_calls e)) Utils.ISet.empty el
892
   | Expr_pre e1 
893
   | Expr_when (e1, _, _) 
894
   | Expr_access (e1, _) 
895
   | Expr_power (e1, _) -> get_calls e1
896
   | Expr_ite (c, t, e) -> Utils.ISet.union (Utils.ISet.union (get_calls c) (get_calls t)) (get_calls e) 
897
   | Expr_arrow (e1, e2) 
898
   | Expr_fby (e1, e2) -> Utils.ISet.union (get_calls e1) (get_calls e2)
899
   | Expr_merge (_, hl) -> List.fold_left (fun accu (_, h) -> Utils.ISet.union accu (get_calls h)) Utils.ISet.empty  hl
900
   | Expr_appl (i, e', i') -> 
901
     if Basic_library.is_internal_fun i then 
902
       (get_calls e') 
903
     else
904
       let calls =  Utils.ISet.add i (get_calls e') in
905
       let test = (fun n -> match n.top_decl_desc with Node nd -> nd.node_id = i | _ -> false) in
906
       if List.exists test nodes then
907
	 match (List.find test nodes).top_decl_desc with
908
	 | Node nd -> Utils.ISet.union (get_node_calls nodes nd) calls
909
	 | _ -> assert false
910
       else 
911
	 calls
912

    
913
and get_eq_calls nodes eq =
914
  get_expr_calls nodes eq.eq_rhs
915
and get_node_calls nodes node =
916
  List.fold_left (fun accu eq -> Utils.ISet.union (get_eq_calls nodes eq) accu) Utils.ISet.empty (get_node_eqs node)
917

    
918
let rec get_expr_vars vars e =
919
  get_expr_desc_vars vars e.expr_desc
920
and get_expr_desc_vars vars expr_desc =
921
  match expr_desc with
922
  | Expr_const _ -> vars
923
  | Expr_ident x -> Utils.ISet.add x vars
924
  | Expr_tuple el
925
  | Expr_array el -> List.fold_left get_expr_vars vars el
926
  | Expr_pre e1 -> get_expr_vars vars e1
927
  | Expr_when (e1, c, _) -> get_expr_vars (Utils.ISet.add c vars) e1 
928
  | Expr_access (e1, d) 
929
  | Expr_power (e1, d)   -> List.fold_left get_expr_vars vars [e1; expr_of_dimension d]
930
  | Expr_ite (c, t, e) -> List.fold_left get_expr_vars vars [c; t; e]
931
  | Expr_arrow (e1, e2) 
932
  | Expr_fby (e1, e2) -> List.fold_left get_expr_vars vars [e1; e2]
933
  | Expr_merge (c, hl) -> List.fold_left (fun vars (_, h) -> get_expr_vars vars h) (Utils.ISet.add c vars) hl
934
  | Expr_appl (_, arg, None)   -> get_expr_vars vars arg
935
  | Expr_appl (_, arg, Some r) -> List.fold_left get_expr_vars vars [arg; r]
936

    
937

    
938
let rec expr_has_arrows e =
939
  expr_desc_has_arrows e.expr_desc
940
and expr_desc_has_arrows expr_desc =
941
  match expr_desc with
942
  | Expr_const _ 
943
  | Expr_ident _ -> false
944
  | Expr_tuple el
945
  | Expr_array el -> List.exists expr_has_arrows el
946
  | Expr_pre e1 
947
  | Expr_when (e1, _, _) 
948
  | Expr_access (e1, _) 
949
  | Expr_power (e1, _) -> expr_has_arrows e1
950
  | Expr_ite (c, t, e) -> List.exists expr_has_arrows [c; t; e]
951
  | Expr_arrow (e1, e2) 
952
  | Expr_fby (e1, e2) -> true
953
  | Expr_merge (_, hl) -> List.exists (fun (_, h) -> expr_has_arrows h) hl
954
  | Expr_appl (i, e', i') -> expr_has_arrows e'
955

    
956
and eq_has_arrows eq =
957
  expr_has_arrows eq.eq_rhs
958
and node_has_arrows node =
959
  List.exists (fun eq -> eq_has_arrows eq) (get_node_eqs node)
960

    
961
(* Local Variables: *)
962
(* compile-command:"make -C .." *)
963
(* End: *)