Project

General

Profile

Statistics
| Branch: | Tag: | Revision:

lustrec / src / corelang.ml @ 6aeb3388

History | View | Annotate | Download (27 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

    
32

    
33
let dummy_clock_dec = {ck_dec_desc=Ckdec_any; ck_dec_loc=Location.dummy_loc}
34

    
35

    
36

    
37
(************************************************************)
38
(* *)
39

    
40
let mktyp loc d =
41
  { ty_dec_desc = d; ty_dec_loc = loc }
42

    
43
let mkclock loc d =
44
  { ck_dec_desc = d; ck_dec_loc = loc }
45

    
46
let mkvar_decl loc (id, ty_dec, ck_dec, is_const) =
47
  { var_id = id;
48
    var_dec_type = ty_dec;
49
    var_dec_clock = ck_dec;
50
    var_dec_const = is_const;
51
    var_type = Types.new_var ();
52
    var_clock = Clocks.new_var true;
53
    var_loc = loc }
54

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

    
64
let var_decl_of_const c =
65
  { var_id = c.const_id;
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 vdecl_list id =
74
  let rec new_name name cpt =
75
    if List.exists (fun v -> v.var_id = name) vdecl_list
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 d =
91
  { top_decl_desc = d; top_decl_loc = loc }
92

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

    
96
(************************************************************)
97
(*   Eexpr functions *)
98
(************************************************************)
99

    
100
let merge_node_annot ann1 ann2 =
101
  { requires = ann1.requires @ ann2.requires;
102
    ensures = ann1.ensures @ ann2.ensures;
103
    behaviors = ann1.behaviors @ ann2.behaviors;
104
    spec_loc = ann1.spec_loc
105
  }
106

    
107
let mkeexpr loc expr =
108
  { eexpr_tag = Utils.new_tag ();
109
    eexpr_qfexpr = expr;
110
    eexpr_quantifiers = [];
111
    eexpr_type = Types.new_var ();
112
    eexpr_clock = Clocks.new_var true;
113
    eexpr_normalized = None;
114
    eexpr_loc = loc }
115

    
116
let extend_eexpr q e = { e with eexpr_quantifiers = q@e.eexpr_quantifiers }
117

    
118
(*
119
let mkepredef_call loc funname args =
120
  mkeexpr loc (EExpr_appl (funname, mkeexpr loc (EExpr_tuple args), None))
121

    
122
let mkepredef_unary_call loc funname arg =
123
  mkeexpr loc (EExpr_appl (funname, arg, None))
124
*)
125

    
126
let merge_expr_annot ann1 ann2 =
127
  match ann1, ann2 with
128
    | None, None -> assert false
129
    | Some _, None -> ann1
130
    | None, Some _ -> ann2
131
    | Some ann1, Some ann2 -> Some {
132
      annots = ann1.annots @ ann2.annots;
133
      annot_loc = ann1.annot_loc
134
    }
135

    
136
let update_expr_annot e annot =
137
  { e with expr_annot = merge_expr_annot e.expr_annot (Some annot) }
138

    
139

    
140
(***********************************************************)
141
(* Fast access to nodes, by name *)
142
let (node_table : (ident, top_decl) Hashtbl.t) = Hashtbl.create 30
143
let consts_table = Hashtbl.create 30
144

    
145
let node_name td =
146
    match td.top_decl_desc with 
147
    | Node nd         -> nd.node_id
148
    | ImportedNode nd -> nd.nodei_id
149
    | _ -> assert false
150

    
151
let is_generic_node td =
152
  match td.top_decl_desc with 
153
  | Node nd         -> List.exists (fun v -> v.var_dec_const) nd.node_inputs
154
  | ImportedNode nd -> List.exists (fun v -> v.var_dec_const) nd.nodei_inputs
155
  | _ -> assert false
156

    
157
let node_inputs td =
158
  match td.top_decl_desc with 
159
  | Node nd         -> nd.node_inputs
160
  | ImportedNode nd -> nd.nodei_inputs
161
  | _ -> assert false
162

    
163
let node_from_name id =
164
  try
165
    Hashtbl.find node_table id
166
  with Not_found -> (Format.eprintf "Unable to find any node named %s@ @?" id;
167
		     assert false)
168

    
169
let is_imported_node td =
170
  match td.top_decl_desc with 
171
  | Node nd         -> false
172
  | ImportedNode nd -> true
173
  | _ -> assert false
174

    
175

    
176
(* alias and type definition table *)
177
let type_table =
178
  Utils.create_hashtable 20 [
179
    Tydec_int  , Tydec_int;
180
    Tydec_bool , Tydec_bool;
181
    Tydec_float, Tydec_float;
182
    Tydec_real , Tydec_real
183
  ]
184

    
185
let rec is_user_type typ =
186
  match typ with
187
  | Tydec_int | Tydec_bool | Tydec_real 
188
  | Tydec_float | Tydec_any | Tydec_const _ -> false
189
  | Tydec_clock typ' -> is_user_type typ'
190
  | _ -> true
191

    
192
let get_repr_type typ =
193
  let typ_def = Hashtbl.find type_table typ in
194
  if is_user_type typ_def then typ else typ_def
195

    
196
let rec coretype_equal ty1 ty2 =
197
  let res = 
198
  match ty1, ty2 with
199
  | Tydec_any       , _
200
  | _               , Tydec_any        -> assert false
201
  | Tydec_const _   , Tydec_const _    -> get_repr_type ty1 = get_repr_type ty2
202
  | Tydec_const _   , _                -> let ty1' = Hashtbl.find type_table ty1
203
					  in (not (is_user_type ty1')) && coretype_equal ty1' ty2
204
  | _               , Tydec_const _    -> coretype_equal ty2 ty1
205
  | Tydec_int       , Tydec_int
206
  | Tydec_real      , Tydec_real
207
  | Tydec_float     , Tydec_float
208
  | Tydec_bool      , Tydec_bool       -> true
209
  | Tydec_clock ty1 , Tydec_clock ty2  -> coretype_equal ty1 ty2
210
  | Tydec_enum tl1  , Tydec_enum tl2   -> List.sort compare tl1 = List.sort compare tl2
211
  | Tydec_struct fl1, Tydec_struct fl2 ->
212
       List.length fl1 = List.length fl2
213
    && List.for_all2 (fun (f1, t1) (f2, t2) -> f1 = f2 && coretype_equal t1 t2)
214
      (List.sort (fun (f1,_) (f2,_) -> compare f1 f2) fl1)
215
      (List.sort (fun (f1,_) (f2,_) -> compare f1 f2) fl2)
216
  | _                                  -> false
217
  in ((*Format.eprint "coretype_equal %a %a = %B@." Printers.pp_var_type_dec_desc ty1 Printers.pp_var_type_dec_desc ty2 res;*) res)
218

    
219
let tag_true = "true"
220
let tag_false = "false"
221

    
222
let const_is_bool c =
223
 match c with
224
 | Const_tag t -> t = tag_true || t = tag_false
225
 | _           -> false
226

    
227
(* Computes the negation of a boolean constant *)
228
let const_negation c =
229
  assert (const_is_bool c);
230
  match c with
231
  | Const_tag t when t = tag_true  -> Const_tag tag_false
232
  | _                              -> Const_tag tag_true
233

    
234
let const_or c1 c2 =
235
  assert (const_is_bool c1 && const_is_bool c2);
236
  match c1, c2 with
237
  | Const_tag t1, _            when t1 = tag_true -> c1
238
  | _           , Const_tag t2 when t2 = tag_true -> c2
239
  | _                                             -> Const_tag tag_false
240

    
241
let const_and c1 c2 =
242
  assert (const_is_bool c1 && const_is_bool c2);
243
  match c1, c2 with
244
  | Const_tag t1, _            when t1 = tag_false -> c1
245
  | _           , Const_tag t2 when t2 = tag_false -> c2
246
  | _                                              -> Const_tag tag_true
247

    
248
let const_xor c1 c2 =
249
  assert (const_is_bool c1 && const_is_bool c2);
250
   match c1, c2 with
251
  | Const_tag t1, Const_tag t2 when t1 <> t2  -> Const_tag tag_true
252
  | _                                         -> Const_tag tag_false
253

    
254
let const_impl c1 c2 =
255
  assert (const_is_bool c1 && const_is_bool c2);
256
  match c1, c2 with
257
  | Const_tag t1, _ when t1 = tag_false           -> Const_tag tag_true
258
  | _           , Const_tag t2 when t2 = tag_true -> Const_tag tag_true
259
  | _                                             -> Const_tag tag_false
260

    
261
(* To guarantee uniqueness of tags in enum types *)
262
let tag_table =
263
  Utils.create_hashtable 20 [
264
   tag_true, Tydec_bool;
265
   tag_false, Tydec_bool
266
  ]
267

    
268
(* To guarantee uniqueness of fields in struct types *)
269
let field_table =
270
  Utils.create_hashtable 20 [
271
  ]
272

    
273
let get_enum_type_tags cty =
274
 match cty with
275
 | Tydec_bool    -> [tag_true; tag_false]
276
 | Tydec_const _ -> (match Hashtbl.find type_table cty with
277
                     | Tydec_enum tl -> tl
278
                     | _             -> assert false)
279
 | _            -> assert false
280

    
281
let get_struct_type_fields cty =
282
 match cty with
283
 | Tydec_const _ -> (match Hashtbl.find type_table cty with
284
                     | Tydec_struct fl -> fl
285
                     | _               -> assert false)
286
 | _            -> assert false
287

    
288
let const_of_bool b =
289
 Const_tag (if b then tag_true else tag_false)
290

    
291
(* let get_const c = snd (Hashtbl.find consts_table c) *)
292

    
293
let ident_of_expr expr =
294
 match expr.expr_desc with
295
 | Expr_ident id -> id
296
 | _             -> assert false
297

    
298
(* Caution, returns an untyped and unclocked expression *)
299
let expr_of_ident id loc =
300
  {expr_tag = Utils.new_tag ();
301
   expr_desc = Expr_ident id;
302
   expr_type = Types.new_var ();
303
   expr_clock = Clocks.new_var true;
304
   expr_delay = Delay.new_var ();
305
   expr_loc = loc;
306
   expr_annot = None}
307

    
308
let is_tuple_expr expr =
309
 match expr.expr_desc with
310
  | Expr_tuple _ -> true
311
  | _            -> false
312

    
313
let expr_list_of_expr expr =
314
  match expr.expr_desc with
315
  | Expr_tuple elist -> elist
316
  | _                -> [expr]
317

    
318
let expr_of_expr_list loc elist =
319
 match elist with
320
 | [t]  -> { t with expr_loc = loc }
321
 | t::_ -> { t with expr_desc = Expr_tuple elist; expr_loc = loc }
322
 | _    -> assert false
323

    
324
let call_of_expr expr =
325
 match expr.expr_desc with
326
 | Expr_appl (f, args, r) -> (f, expr_list_of_expr args, r)
327
 | _                      -> assert false
328

    
329
(* Conversion from dimension expr to standard expr, for the purpose of printing, typing, etc... *)
330
let rec expr_of_dimension dim =
331
 match dim.dim_desc with
332
 | Dbool b        ->
333
     mkexpr dim.dim_loc (Expr_const (const_of_bool b))
334
 | Dint i         ->
335
     mkexpr dim.dim_loc (Expr_const (Const_int i))
336
 | Dident id      ->
337
     mkexpr dim.dim_loc (Expr_ident id)
338
 | Dite (c, t, e) ->
339
     mkexpr dim.dim_loc (Expr_ite (expr_of_dimension c, expr_of_dimension t, expr_of_dimension e))
340
 | Dappl (id, args) ->
341
     mkexpr dim.dim_loc (Expr_appl (id, expr_of_expr_list dim.dim_loc (List.map expr_of_dimension args), None))
342
 | Dlink dim'       -> expr_of_dimension dim'
343
 | Dvar
344
 | Dunivar          -> (Format.eprintf "internal error: expr_of_dimension %a@." Dimension.pp_dimension dim;
345
			assert false)
346

    
347
let dimension_of_const loc const =
348
 match const with
349
 | Const_int i                                    -> mkdim_int loc i
350
 | Const_tag t when t = tag_true || t = tag_false -> mkdim_bool loc (t = tag_true)
351
 | _                                              -> raise InvalidDimension
352

    
353
(* Conversion from standard expr to dimension expr, for the purpose of injecting static call arguments 
354
   into dimension expressions *)
355
let rec dimension_of_expr expr =
356
  match expr.expr_desc with
357
  | Expr_const c  -> dimension_of_const expr.expr_loc c
358
  | Expr_ident id -> mkdim_ident expr.expr_loc id
359
  | Expr_appl (f, args, None) when Basic_library.is_internal_fun f ->
360
      let k = Types.get_static_value (Env.lookup_value Basic_library.type_env f) in
361
      if k = None then raise InvalidDimension;
362
      mkdim_appl expr.expr_loc f (List.map dimension_of_expr (expr_list_of_expr args))
363
  | Expr_ite (i, t, e)        ->
364
      mkdim_ite expr.expr_loc (dimension_of_expr i) (dimension_of_expr t) (dimension_of_expr e)
365
  | _ -> raise InvalidDimension (* not a simple dimension expression *)
366

    
367

    
368
let sort_handlers hl =
369
 List.sort (fun (t, _) (t', _) -> compare t t') hl
370

    
371
let rec is_eq_expr e1 e2 = match e1.expr_desc, e2.expr_desc with
372
  | Expr_const c1, Expr_const c2 -> c1 = c2
373
  | Expr_ident i1, Expr_ident i2 -> i1 = i2
374
  | Expr_array el1, Expr_array el2 
375
  | Expr_tuple el1, Expr_tuple el2 -> 
376
    List.length el1 = List.length el2 && List.for_all2 is_eq_expr el1 el2 
377
  | Expr_arrow (e1, e2), Expr_arrow (e1', e2') -> is_eq_expr e1 e1' && is_eq_expr e2 e2'
378
  | Expr_fby (e1,e2), Expr_fby (e1',e2') -> is_eq_expr e1 e1' && is_eq_expr e2 e2'
379
  | 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
380
  (* | Expr_concat (e1,e2), Expr_concat (e1',e2') -> is_eq_expr e1 e1' && is_eq_expr e2 e2' *)
381
  (* | Expr_tail e, Expr_tail e' -> is_eq_expr e e' *)
382
  | Expr_pre e, Expr_pre e' -> is_eq_expr e e'
383
  | Expr_when (e, i, l), Expr_when (e', i', l') -> l=l' && i=i' && is_eq_expr e e'
384
  | 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')
385
  | Expr_appl (i, e, r), Expr_appl (i', e', r') -> i=i' && r=r' && is_eq_expr e e'
386
  | Expr_power (e1, i1), Expr_power (e2, i2)
387
  | Expr_access (e1, i1), Expr_access (e2, i2) -> is_eq_expr e1 e2 && is_eq_expr (expr_of_dimension i1) (expr_of_dimension i2)
388
  | _ -> false
389

    
390
let get_node_vars nd =
391
  nd.node_inputs @ nd.node_locals @ nd.node_outputs
392

    
393
let get_var id var_list =
394
 List.find (fun v -> v.var_id = id) var_list
395

    
396
let get_node_var id node = get_var id (get_node_vars node)
397

    
398
let get_node_eq id node =
399
 List.find (fun eq -> List.mem id eq.eq_lhs) node.node_eqs
400

    
401
let get_nodes prog = 
402
  List.fold_left (
403
    fun nodes decl ->
404
      match decl.top_decl_desc with
405
	| Node nd -> nd::nodes
406
	| Consts _ | ImportedNode _ | Open _ | Type _ -> nodes  
407
  ) [] prog
408

    
409
let get_consts prog = 
410
  List.fold_left (
411
    fun consts decl ->
412
      match decl.top_decl_desc with
413
	| Consts clist -> clist@consts
414
	| Node _ | ImportedNode _ | Open _ | Type _ -> consts  
415
  ) [] prog
416

    
417
let get_types prog = 
418
  List.fold_left (
419
    fun types decl ->
420
      match decl.top_decl_desc with
421
	| Type typ -> typ::types
422
	| Node _ | ImportedNode _ | Open _ | Consts _ -> types  
423
  ) [] prog
424

    
425
(************************************************************************)
426
(*        Renaming                                                      *)
427

    
428
(* applies the renaming function [fvar] to all variables of expression [expr] *)
429
 let rec expr_replace_var fvar expr =
430
  { expr with expr_desc = expr_desc_replace_var fvar expr.expr_desc }
431

    
432
 and expr_desc_replace_var fvar expr_desc =
433
   match expr_desc with
434
   | Expr_const _ -> expr_desc
435
   | Expr_ident i -> Expr_ident (fvar i)
436
   | Expr_array el -> Expr_array (List.map (expr_replace_var fvar) el)
437
   | Expr_access (e1, d) -> Expr_access (expr_replace_var fvar e1, d)
438
   | Expr_power (e1, d) -> Expr_power (expr_replace_var fvar e1, d)
439
   | Expr_tuple el -> Expr_tuple (List.map (expr_replace_var fvar) el)
440
   | Expr_ite (c, t, e) -> Expr_ite (expr_replace_var fvar c, expr_replace_var fvar t, expr_replace_var fvar e)
441
   | Expr_arrow (e1, e2)-> Expr_arrow (expr_replace_var fvar e1, expr_replace_var fvar e2) 
442
   | Expr_fby (e1, e2) -> Expr_fby (expr_replace_var fvar e1, expr_replace_var fvar e2)
443
   | Expr_pre e' -> Expr_pre (expr_replace_var fvar e')
444
   | Expr_when (e', i, l)-> Expr_when (expr_replace_var fvar e', fvar i, l)
445
   | Expr_merge (i, hl) -> Expr_merge (fvar i, List.map (fun (t, h) -> (t, expr_replace_var fvar h)) hl)
446
   | Expr_appl (i, e', i') -> Expr_appl (i, expr_replace_var fvar e', Utils.option_map (fun (x, l) -> fvar x, l) i')
447

    
448
(* Applies the renaming function [fvar] to every rhs
449
   only when the corresponding lhs satisfies predicate [pvar] *)
450
 let eq_replace_rhs_var pvar fvar eq =
451
   let pvar l = List.exists pvar l in
452
   let rec replace lhs rhs =
453
     { rhs with expr_desc = replace_desc lhs rhs.expr_desc }
454
   and replace_desc lhs rhs_desc =
455
     match lhs with
456
     | []  -> assert false
457
     | [_] -> if pvar lhs then expr_desc_replace_var fvar rhs_desc else rhs_desc
458
     | _   ->
459
       (match rhs_desc with
460
       | Expr_tuple tl ->
461
	 Expr_tuple (List.map2 (fun v e -> replace [v] e) lhs tl)
462
       | Expr_appl (f, arg, None) when Basic_library.is_internal_fun f ->
463
	 let args = expr_list_of_expr arg in
464
	 Expr_appl (f, expr_of_expr_list arg.expr_loc (List.map (replace lhs) args), None)
465
       | Expr_array _
466
       | Expr_access _
467
       | Expr_power _
468
       | Expr_const _
469
       | Expr_ident _
470
       | Expr_appl _   ->
471
	 if pvar lhs
472
	 then expr_desc_replace_var fvar rhs_desc
473
	 else rhs_desc
474
       | Expr_ite (c, t, e)   -> Expr_ite (replace lhs c, replace lhs t, replace lhs e)
475
       | Expr_arrow (e1, e2)  -> Expr_arrow (replace lhs e1, replace lhs e2) 
476
       | Expr_fby (e1, e2)    -> Expr_fby (replace lhs e1, replace lhs e2)
477
       | Expr_pre e'          -> Expr_pre (replace lhs e')
478
       | Expr_when (e', i, l) -> let i' = if pvar lhs then fvar i else i
479
				 in Expr_when (replace lhs e', i', l)
480
       | Expr_merge (i, hl)   -> let i' = if pvar lhs then fvar i else i
481
				 in Expr_merge (i', List.map (fun (t, h) -> (t, replace lhs h)) hl)
482
       )
483
   in { eq with eq_rhs = replace eq.eq_lhs eq.eq_rhs }
484

    
485

    
486
 let rec rename_expr  f_node f_var f_const expr =
487
   { expr with expr_desc = rename_expr_desc f_node f_var f_const expr.expr_desc }
488
 and rename_expr_desc f_node f_var f_const expr_desc =
489
   let re = rename_expr  f_node f_var f_const in
490
   match expr_desc with
491
   | Expr_const _ -> expr_desc
492
   | Expr_ident i -> Expr_ident (f_var i)
493
   | Expr_array el -> Expr_array (List.map re el)
494
   | Expr_access (e1, d) -> Expr_access (re e1, d)
495
   | Expr_power (e1, d) -> Expr_power (re e1, d)
496
   | Expr_tuple el -> Expr_tuple (List.map re el)
497
   | Expr_ite (c, t, e) -> Expr_ite (re c, re t, re e)
498
   | Expr_arrow (e1, e2)-> Expr_arrow (re e1, re e2) 
499
   | Expr_fby (e1, e2) -> Expr_fby (re e1, re e2)
500
   | Expr_pre e' -> Expr_pre (re e')
501
   | Expr_when (e', i, l)-> Expr_when (re e', f_var i, l)
502
   | Expr_merge (i, hl) -> 
503
     Expr_merge (f_var i, List.map (fun (t, h) -> (t, re h)) hl)
504
   | Expr_appl (i, e', i') -> 
505
     Expr_appl (f_node i, re e', Utils.option_map (fun (x, l) -> f_var x, l) i')
506
  
507
 let rename_node_annot f_node f_var f_const expr  =
508
   expr
509
 (* TODO assert false *)
510

    
511
 let rename_expr_annot f_node f_var f_const annot =
512
   annot
513
 (* TODO assert false *)
514

    
515
let rename_node f_node f_var f_const nd =
516
  let rename_var v = { v with var_id = f_var v.var_id } in
517
  let rename_eq eq = { eq with
518
      eq_lhs = List.map f_var eq.eq_lhs; 
519
      eq_rhs = rename_expr f_node f_var f_const eq.eq_rhs
520
    } 
521
  in
522
  let inputs = List.map rename_var nd.node_inputs in
523
  let outputs = List.map rename_var nd.node_outputs in
524
  let locals = List.map rename_var nd.node_locals in
525
  let gen_calls = List.map (rename_expr f_node f_var f_const) nd.node_gencalls in
526
  let node_checks = List.map (Dimension.expr_replace_var f_var)  nd.node_checks in
527
  let node_asserts = List.map 
528
    (fun a -> 
529
      {a with assert_expr = 
530
	  let expr = a.assert_expr in
531
	  rename_expr f_node f_var f_const expr})
532
    nd.node_asserts
533
  in
534
  let eqs = List.map rename_eq nd.node_eqs in
535
  let spec = 
536
    Utils.option_map 
537
      (fun s -> rename_node_annot f_node f_var f_const s) 
538
      nd.node_spec 
539
  in
540
  let annot =
541
    List.map 
542
      (fun s -> rename_expr_annot f_node f_var f_const s) 
543
      nd.node_annot
544
  in
545
  {
546
    node_id = f_node nd.node_id;
547
    node_type = nd.node_type;
548
    node_clock = nd.node_clock;
549
    node_inputs = inputs;
550
    node_outputs = outputs;
551
    node_locals = locals;
552
    node_gencalls = gen_calls;
553
    node_checks = node_checks;
554
    node_asserts = node_asserts;
555
    node_eqs = eqs;
556
    node_dec_stateless = nd.node_dec_stateless;
557
    node_stateless = nd.node_stateless;
558
    node_spec = spec;
559
    node_annot = annot;
560
  }
561

    
562

    
563
let rename_const f_const c =
564
  { c with const_id = f_const c.const_id }
565
    
566
let rename_prog f_node f_var f_const prog =
567
  List.rev (
568
    List.fold_left (fun accu top ->
569
      (match top.top_decl_desc with
570
      | Node nd -> 
571
	{ top with top_decl_desc = Node (rename_node f_node f_var f_const nd) }
572
      | Consts c -> 
573
	{ top with top_decl_desc = Consts (List.map (rename_const f_const) c) }
574
      | ImportedNode _
575
      | Open _
576
      | Type _ -> top)
577
      ::accu
578
) [] prog
579
  )
580

    
581
(**********************************************************************)
582
(* Pretty printers *)
583

    
584
let pp_decl_type fmt tdecl =
585
  match tdecl.top_decl_desc with
586
  | Node nd ->
587
    fprintf fmt "%s: " nd.node_id;
588
    Utils.reset_names ();
589
    fprintf fmt "%a@ " Types.print_ty nd.node_type
590
  | ImportedNode ind ->
591
    fprintf fmt "%s: " ind.nodei_id;
592
    Utils.reset_names ();
593
    fprintf fmt "%a@ " Types.print_ty ind.nodei_type
594
  | Consts _ | Open _ | Type _ -> ()
595

    
596
let pp_prog_type fmt tdecl_list =
597
  Utils.fprintf_list ~sep:"" pp_decl_type fmt tdecl_list
598

    
599
let pp_decl_clock fmt cdecl =
600
  match cdecl.top_decl_desc with
601
  | Node nd ->
602
    fprintf fmt "%s: " nd.node_id;
603
    Utils.reset_names ();
604
    fprintf fmt "%a@ " Clocks.print_ck nd.node_clock
605
  | ImportedNode ind ->
606
    fprintf fmt "%s: " ind.nodei_id;
607
    Utils.reset_names ();
608
    fprintf fmt "%a@ " Clocks.print_ck ind.nodei_clock
609
  | Consts _ | Open _ | Type _ -> ()
610

    
611
let pp_prog_clock fmt prog =
612
  Utils.fprintf_list ~sep:"" pp_decl_clock fmt prog
613

    
614
let pp_error fmt = function
615
    Main_not_found ->
616
      fprintf fmt "Cannot compile node %s: could not find the node definition.@."
617
	!Options.main_node
618
  | Main_wrong_kind ->
619
    fprintf fmt
620
      "Name %s does not correspond to a (non-imported) node definition.@." 
621
      !Options.main_node
622
  | No_main_specified ->
623
    fprintf fmt "No main node specified@."
624
  | Unbound_symbol sym ->
625
    fprintf fmt
626
      "%s is undefined.@."
627
      sym
628
  | Already_bound_symbol sym -> 
629
    fprintf fmt
630
      "%s is already defined.@."
631
      sym
632

    
633
(* filling node table with internal functions *)
634
let vdecls_of_typ_ck cpt ty =
635
  let loc = Location.dummy_loc in
636
  List.map
637
    (fun _ -> incr cpt;
638
              let name = sprintf "_var_%d" !cpt in
639
              mkvar_decl loc (name, mktyp loc Tydec_any, mkclock loc Ckdec_any, false))
640
    (Types.type_list_of_type ty)
641

    
642
let mk_internal_node id =
643
  let spec = None in
644
  let ty = Env.lookup_value Basic_library.type_env id in
645
  let ck = Env.lookup_value Basic_library.clock_env id in
646
  let (tin, tout) = Types.split_arrow ty in
647
  (*eprintf "internal fun %s: %d -> %d@." id (List.length (Types.type_list_of_type tin)) (List.length (Types.type_list_of_type tout));*)
648
  let cpt = ref (-1) in
649
  mktop_decl Location.dummy_loc
650
    (ImportedNode
651
       {nodei_id = id;
652
	nodei_type = ty;
653
	nodei_clock = ck;
654
	nodei_inputs = vdecls_of_typ_ck cpt tin;
655
	nodei_outputs = vdecls_of_typ_ck cpt tout;
656
	nodei_stateless = Types.get_static_value ty <> None;
657
	nodei_spec = spec;
658
	nodei_prototype = None;
659
       	nodei_in_lib = None;
660
       })
661

    
662
let add_internal_funs () =
663
  List.iter
664
    (fun id -> let nd = mk_internal_node id in Hashtbl.add node_table id nd)
665
    Basic_library.internal_funs
666

    
667

    
668

    
669
(* Replace any occurence of a var in vars_to_replace by its associated
670
   expression in defs until e does not contain any such variables *)
671
let rec substitute_expr vars_to_replace defs e =
672
  let se = substitute_expr vars_to_replace defs in
673
  { e with expr_desc = 
674
      let ed = e.expr_desc in
675
      match ed with
676
      | Expr_const _ -> ed
677
      | Expr_array el -> Expr_array (List.map se el)
678
      | Expr_access (e1, d) -> Expr_access (se e1, d)
679
      | Expr_power (e1, d) -> Expr_power (se e1, d)
680
      | Expr_tuple el -> Expr_tuple (List.map se el)
681
      | Expr_ite (c, t, e) -> Expr_ite (se c, se t, se e)
682
      | Expr_arrow (e1, e2)-> Expr_arrow (se e1, se e2) 
683
      | Expr_fby (e1, e2) -> Expr_fby (se e1, se e2)
684
      | Expr_pre e' -> Expr_pre (se e')
685
      | Expr_when (e', i, l)-> Expr_when (se e', i, l)
686
      | Expr_merge (i, hl) -> Expr_merge (i, List.map (fun (t, h) -> (t, se h)) hl)
687
      | Expr_appl (i, e', i') -> Expr_appl (i, se e', i')
688
      | Expr_ident i -> 
689
	if List.exists (fun v -> v.var_id = i) vars_to_replace then (
690
	  let eq_i eq = eq.eq_lhs = [i] in
691
	  if List.exists eq_i defs then
692
	    let sub = List.find eq_i defs in
693
	    let sub' = se sub.eq_rhs in
694
	    sub'.expr_desc
695
	  else 
696
	    assert false
697
	)
698
	else
699
	  ed
700

    
701
  }
702
(* FAUT IL RETIRER ?
703
  
704
 let rec expr_to_eexpr  expr =
705
   { eexpr_tag = expr.expr_tag;
706
     eexpr_desc = expr_desc_to_eexpr_desc expr.expr_desc;
707
     eexpr_type = expr.expr_type;
708
     eexpr_clock = expr.expr_clock;
709
     eexpr_loc = expr.expr_loc
710
   }
711
 and expr_desc_to_eexpr_desc expr_desc =
712
   let conv = expr_to_eexpr in
713
   match expr_desc with
714
   | Expr_const c -> EExpr_const (match c with
715
     | Const_int x -> EConst_int x 
716
     | Const_real x -> EConst_real x 
717
     | Const_float x -> EConst_float x 
718
     | Const_tag x -> EConst_tag x 
719
     | _ -> assert false
720

    
721
   )
722
   | Expr_ident i -> EExpr_ident i
723
   | Expr_tuple el -> EExpr_tuple (List.map conv el)
724

    
725
   | Expr_arrow (e1, e2)-> EExpr_arrow (conv e1, conv e2) 
726
   | Expr_fby (e1, e2) -> EExpr_fby (conv e1, conv e2)
727
   | Expr_pre e' -> EExpr_pre (conv e')
728
   | Expr_appl (i, e', i') -> 
729
     EExpr_appl 
730
       (i, conv e', match i' with None -> None | Some(id, _) -> Some id)
731

    
732
   | Expr_when _
733
   | Expr_merge _ -> assert false
734
   | Expr_array _ 
735
   | Expr_access _ 
736
   | Expr_power _  -> assert false
737
   | Expr_ite (c, t, e) -> assert false 
738
   | _ -> assert false
739

    
740
     *)
741
let rec get_expr_calls nodes e =
742
  get_calls_expr_desc nodes e.expr_desc
743
and get_calls_expr_desc nodes expr_desc =
744
  let get_calls = get_expr_calls nodes in
745
  match expr_desc with
746
  | Expr_const _ 
747
   | Expr_ident _ -> Utils.ISet.empty
748
   | Expr_tuple el
749
   | Expr_array el -> List.fold_left (fun accu e -> Utils.ISet.union accu (get_calls e)) Utils.ISet.empty el
750
   | Expr_pre e1 
751
   | Expr_when (e1, _, _) 
752
   | Expr_access (e1, _) 
753
   | Expr_power (e1, _) -> get_calls e1
754
   | Expr_ite (c, t, e) -> Utils.ISet.union (Utils.ISet.union (get_calls c) (get_calls t)) (get_calls e) 
755
   | Expr_arrow (e1, e2) 
756
   | Expr_fby (e1, e2) -> Utils.ISet.union (get_calls e1) (get_calls e2)
757
   | Expr_merge (_, hl) -> List.fold_left (fun accu (_, h) -> Utils.ISet.union accu (get_calls h)) Utils.ISet.empty  hl
758
   | Expr_appl (i, e', i') -> 
759
     if Basic_library.is_internal_fun i then 
760
       (get_calls e') 
761
     else
762
       let calls =  Utils.ISet.add i (get_calls e') in
763
       let test = (fun n -> match n.top_decl_desc with Node nd -> nd.node_id = i | _ -> false) in
764
       if List.exists test nodes then
765
	 match (List.find test nodes).top_decl_desc with
766
	 | Node nd -> Utils.ISet.union (get_node_calls nodes nd) calls
767
	 | _ -> assert false
768
       else 
769
	 calls
770

    
771
and get_eq_calls nodes eq =
772
  get_expr_calls nodes eq.eq_rhs
773
and get_node_calls nodes node =
774
  List.fold_left (fun accu eq -> Utils.ISet.union (get_eq_calls nodes eq) accu) Utils.ISet.empty node.node_eqs
775

    
776

    
777
let rec expr_has_arrows e =
778
  expr_desc_has_arrows e.expr_desc
779
and expr_desc_has_arrows expr_desc =
780
  match expr_desc with
781
  | Expr_const _ 
782
  | Expr_ident _ -> false
783
  | Expr_tuple el
784
  | Expr_array el -> List.exists expr_has_arrows el
785
  | Expr_pre e1 
786
  | Expr_when (e1, _, _) 
787
  | Expr_access (e1, _) 
788
  | Expr_power (e1, _) -> expr_has_arrows e1
789
  | Expr_ite (c, t, e) -> List.exists expr_has_arrows [c; t; e]
790
  | Expr_arrow (e1, e2) 
791
  | Expr_fby (e1, e2) -> true
792
  | Expr_merge (_, hl) -> List.exists (fun (_, h) -> expr_has_arrows h) hl
793
  | Expr_appl (i, e', i') -> expr_has_arrows e'
794

    
795
and eq_has_arrows eq =
796
  expr_has_arrows eq.eq_rhs
797
and node_has_arrows node =
798
  List.exists (fun eq -> eq_has_arrows eq) node.node_eqs
799

    
800
(* Local Variables: *)
801
(* compile-command:"make -C .." *)
802
(* End: *)