Project

General

Profile

Statistics
| Branch: | Tag: | Revision:

lustrec / src / corelang.ml @ b38ffff3

History | View | Annotate | Download (25.6 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 tag_true = "true"
197
let tag_false = "false"
198

    
199
let const_is_bool c =
200
 match c with
201
 | Const_tag t -> t = tag_true || t = tag_false
202
 | _           -> false
203

    
204
(* Computes the negation of a boolean constant *)
205
let const_negation c =
206
  assert (const_is_bool c);
207
  match c with
208
  | Const_tag t when t = tag_true  -> Const_tag tag_false
209
  | _                              -> Const_tag tag_true
210

    
211
let const_or c1 c2 =
212
  assert (const_is_bool c1 && const_is_bool c2);
213
  match c1, c2 with
214
  | Const_tag t1, _            when t1 = tag_true -> c1
215
  | _           , Const_tag t2 when t2 = tag_true -> c2
216
  | _                                             -> Const_tag tag_false
217

    
218
let const_and c1 c2 =
219
  assert (const_is_bool c1 && const_is_bool c2);
220
  match c1, c2 with
221
  | Const_tag t1, _            when t1 = tag_false -> c1
222
  | _           , Const_tag t2 when t2 = tag_false -> c2
223
  | _                                              -> Const_tag tag_true
224

    
225
let const_xor c1 c2 =
226
  assert (const_is_bool c1 && const_is_bool c2);
227
   match c1, c2 with
228
  | Const_tag t1, Const_tag t2 when t1 <> t2  -> Const_tag tag_true
229
  | _                                         -> Const_tag tag_false
230

    
231
let const_impl c1 c2 =
232
  assert (const_is_bool c1 && const_is_bool c2);
233
  match c1, c2 with
234
  | Const_tag t1, _ when t1 = tag_false           -> Const_tag tag_true
235
  | _           , Const_tag t2 when t2 = tag_true -> Const_tag tag_true
236
  | _                                             -> Const_tag tag_false
237

    
238
(* To guarantee uniqueness of tags in enum types *)
239
let tag_table =
240
  Utils.create_hashtable 20 [
241
   tag_true, Tydec_bool;
242
   tag_false, Tydec_bool
243
  ]
244

    
245
(* To guarantee uniqueness of fields in struct types *)
246
let field_table =
247
  Utils.create_hashtable 20 [
248
  ]
249

    
250
let get_enum_type_tags cty =
251
 match cty with
252
 | Tydec_bool    -> [tag_true; tag_false]
253
 | Tydec_const _ -> (match Hashtbl.find type_table cty with
254
                     | Tydec_enum tl -> tl
255
                     | _             -> assert false)
256
 | _            -> assert false
257

    
258
let get_struct_type_fields cty =
259
 match cty with
260
 | Tydec_const _ -> (match Hashtbl.find type_table cty with
261
                     | Tydec_struct fl -> fl
262
                     | _               -> assert false)
263
 | _            -> assert false
264

    
265
let const_of_bool b =
266
 Const_tag (if b then tag_true else tag_false)
267

    
268
(* let get_const c = snd (Hashtbl.find consts_table c) *)
269

    
270
let ident_of_expr expr =
271
 match expr.expr_desc with
272
 | Expr_ident id -> id
273
 | _             -> assert false
274

    
275
(* Caution, returns an untyped and unclocked expression *)
276
let expr_of_ident id loc =
277
  {expr_tag = Utils.new_tag ();
278
   expr_desc = Expr_ident id;
279
   expr_type = Types.new_var ();
280
   expr_clock = Clocks.new_var true;
281
   expr_delay = Delay.new_var ();
282
   expr_loc = loc;
283
   expr_annot = None}
284

    
285
let is_tuple_expr expr =
286
 match expr.expr_desc with
287
  | Expr_tuple _ -> true
288
  | _            -> false
289

    
290
let expr_list_of_expr expr =
291
  match expr.expr_desc with
292
  | Expr_tuple elist -> elist
293
  | _                -> [expr]
294

    
295
let expr_of_expr_list loc elist =
296
 match elist with
297
 | [t]  -> { t with expr_loc = loc }
298
 | t::_ -> { t with expr_desc = Expr_tuple elist; expr_loc = loc }
299
 | _    -> assert false
300

    
301
let call_of_expr expr =
302
 match expr.expr_desc with
303
 | Expr_appl (f, args, r) -> (f, expr_list_of_expr args, r)
304
 | _                      -> assert false
305

    
306
(* Conversion from dimension expr to standard expr, for the purpose of printing, typing, etc... *)
307
let rec expr_of_dimension dim =
308
 match dim.dim_desc with
309
 | Dbool b        ->
310
     mkexpr dim.dim_loc (Expr_const (const_of_bool b))
311
 | Dint i         ->
312
     mkexpr dim.dim_loc (Expr_const (Const_int i))
313
 | Dident id      ->
314
     mkexpr dim.dim_loc (Expr_ident id)
315
 | Dite (c, t, e) ->
316
     mkexpr dim.dim_loc (Expr_ite (expr_of_dimension c, expr_of_dimension t, expr_of_dimension e))
317
 | Dappl (id, args) ->
318
     mkexpr dim.dim_loc (Expr_appl (id, expr_of_expr_list dim.dim_loc (List.map expr_of_dimension args), None))
319
 | Dlink dim'       -> expr_of_dimension dim'
320
 | Dvar
321
 | Dunivar          -> (Format.eprintf "internal error: expr_of_dimension %a@." Dimension.pp_dimension dim;
322
			assert false)
323

    
324
let dimension_of_const loc const =
325
 match const with
326
 | Const_int i                                    -> mkdim_int loc i
327
 | Const_tag t when t = tag_true || t = tag_false -> mkdim_bool loc (t = tag_true)
328
 | _                                              -> raise InvalidDimension
329

    
330
(* Conversion from standard expr to dimension expr, for the purpose of injecting static call arguments 
331
   into dimension expressions *)
332
let rec dimension_of_expr expr =
333
  match expr.expr_desc with
334
  | Expr_const c  -> dimension_of_const expr.expr_loc c
335
  | Expr_ident id -> mkdim_ident expr.expr_loc id
336
  | Expr_appl (f, args, None) when Basic_library.is_internal_fun f ->
337
      let k = Types.get_static_value (Env.lookup_value Basic_library.type_env f) in
338
      if k = None then raise InvalidDimension;
339
      mkdim_appl expr.expr_loc f (List.map dimension_of_expr (expr_list_of_expr args))
340
  | Expr_ite (i, t, e)        ->
341
      mkdim_ite expr.expr_loc (dimension_of_expr i) (dimension_of_expr t) (dimension_of_expr e)
342
  | _ -> raise InvalidDimension (* not a simple dimension expression *)
343

    
344

    
345
let sort_handlers hl =
346
 List.sort (fun (t, _) (t', _) -> compare t t') hl
347

    
348
let rec is_eq_expr e1 e2 = match e1.expr_desc, e2.expr_desc with
349
  | Expr_const c1, Expr_const c2 -> c1 = c2
350
  | Expr_ident i1, Expr_ident i2 -> i1 = i2
351
  | Expr_array el1, Expr_array el2 
352
  | Expr_tuple el1, Expr_tuple el2 -> 
353
    List.length el1 = List.length el2 && List.for_all2 is_eq_expr el1 el2 
354
  | Expr_arrow (e1, e2), Expr_arrow (e1', e2') -> is_eq_expr e1 e1' && is_eq_expr e2 e2'
355
  | Expr_fby (e1,e2), Expr_fby (e1',e2') -> is_eq_expr e1 e1' && is_eq_expr e2 e2'
356
  | 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
357
  (* | Expr_concat (e1,e2), Expr_concat (e1',e2') -> is_eq_expr e1 e1' && is_eq_expr e2 e2' *)
358
  (* | Expr_tail e, Expr_tail e' -> is_eq_expr e e' *)
359
  | Expr_pre e, Expr_pre e' -> is_eq_expr e e'
360
  | Expr_when (e, i, l), Expr_when (e', i', l') -> l=l' && i=i' && is_eq_expr e e'
361
  | 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')
362
  | Expr_appl (i, e, r), Expr_appl (i', e', r') -> i=i' && r=r' && is_eq_expr e e'
363
  | Expr_power (e1, i1), Expr_power (e2, i2)
364
  | Expr_access (e1, i1), Expr_access (e2, i2) -> is_eq_expr e1 e2 && is_eq_expr (expr_of_dimension i1) (expr_of_dimension i2)
365
  | _ -> false
366

    
367
let get_node_vars nd =
368
  nd.node_inputs @ nd.node_locals @ nd.node_outputs
369

    
370
let get_var id var_list =
371
 List.find (fun v -> v.var_id = id) var_list
372

    
373
let get_node_var id node = get_var id (get_node_vars node)
374

    
375
let get_node_eq id node =
376
 List.find (fun eq -> List.mem id eq.eq_lhs) node.node_eqs
377

    
378
let get_nodes prog = 
379
  List.fold_left (
380
    fun nodes decl ->
381
      match decl.top_decl_desc with
382
	| Node nd -> nd::nodes
383
	| Consts _ | ImportedNode _ | Open _ -> nodes  
384
  ) [] prog
385

    
386
let get_consts prog = 
387
  List.fold_left (
388
    fun consts decl ->
389
      match decl.top_decl_desc with
390
	| Consts clist -> clist@consts
391
	| Node _ | ImportedNode _ | Open _ -> consts  
392
  ) [] prog
393

    
394

    
395

    
396
(************************************************************************)
397
(*        Renaming                                                      *)
398

    
399
(* applies the renaming function [fvar] to all variables of expression [expr] *)
400
 let rec expr_replace_var fvar expr =
401
  { expr with expr_desc = expr_desc_replace_var fvar expr.expr_desc }
402

    
403
 and expr_desc_replace_var fvar expr_desc =
404
   match expr_desc with
405
   | Expr_const _ -> expr_desc
406
   | Expr_ident i -> Expr_ident (fvar i)
407
   | Expr_array el -> Expr_array (List.map (expr_replace_var fvar) el)
408
   | Expr_access (e1, d) -> Expr_access (expr_replace_var fvar e1, d)
409
   | Expr_power (e1, d) -> Expr_power (expr_replace_var fvar e1, d)
410
   | Expr_tuple el -> Expr_tuple (List.map (expr_replace_var fvar) el)
411
   | Expr_ite (c, t, e) -> Expr_ite (expr_replace_var fvar c, expr_replace_var fvar t, expr_replace_var fvar e)
412
   | Expr_arrow (e1, e2)-> Expr_arrow (expr_replace_var fvar e1, expr_replace_var fvar e2) 
413
   | Expr_fby (e1, e2) -> Expr_fby (expr_replace_var fvar e1, expr_replace_var fvar e2)
414
   | Expr_pre e' -> Expr_pre (expr_replace_var fvar e')
415
   | Expr_when (e', i, l)-> Expr_when (expr_replace_var fvar e', fvar i, l)
416
   | Expr_merge (i, hl) -> Expr_merge (fvar i, List.map (fun (t, h) -> (t, expr_replace_var fvar h)) hl)
417
   | Expr_appl (i, e', i') -> Expr_appl (i, expr_replace_var fvar e', Utils.option_map (fun (x, l) -> fvar x, l) i')
418

    
419
(* Applies the renaming function [fvar] to every rhs
420
   only when the corresponding lhs satisfies predicate [pvar] *)
421
 let eq_replace_rhs_var pvar fvar eq =
422
   let pvar l = List.exists pvar l in
423
   let rec replace lhs rhs =
424
     { rhs with expr_desc = replace_desc lhs rhs.expr_desc }
425
   and replace_desc lhs rhs_desc =
426
     match lhs with
427
     | []  -> assert false
428
     | [_] -> if pvar lhs then expr_desc_replace_var fvar rhs_desc else rhs_desc
429
     | _   ->
430
       (match rhs_desc with
431
       | Expr_tuple tl ->
432
	 Expr_tuple (List.map2 (fun v e -> replace [v] e) lhs tl)
433
       | Expr_appl (f, arg, None) when Basic_library.is_internal_fun f ->
434
	 let args = expr_list_of_expr arg in
435
	 Expr_appl (f, expr_of_expr_list arg.expr_loc (List.map (replace lhs) args), None)
436
       | Expr_array _
437
       | Expr_access _
438
       | Expr_power _
439
       | Expr_const _
440
       | Expr_ident _
441
       | Expr_appl _   ->
442
	 if pvar lhs
443
	 then expr_desc_replace_var fvar rhs_desc
444
	 else rhs_desc
445
       | Expr_ite (c, t, e)   -> Expr_ite (replace lhs c, replace lhs t, replace lhs e)
446
       | Expr_arrow (e1, e2)  -> Expr_arrow (replace lhs e1, replace lhs e2) 
447
       | Expr_fby (e1, e2)    -> Expr_fby (replace lhs e1, replace lhs e2)
448
       | Expr_pre e'          -> Expr_pre (replace lhs e')
449
       | Expr_when (e', i, l) -> let i' = if pvar lhs then fvar i else i
450
				 in Expr_when (replace lhs e', i', l)
451
       | Expr_merge (i, hl)   -> let i' = if pvar lhs then fvar i else i
452
				 in Expr_merge (i', List.map (fun (t, h) -> (t, replace lhs h)) hl)
453
       )
454
   in { eq with eq_rhs = replace eq.eq_lhs eq.eq_rhs }
455

    
456

    
457
 let rec rename_expr  f_node f_var f_const expr =
458
   { expr with expr_desc = rename_expr_desc f_node f_var f_const expr.expr_desc }
459
 and rename_expr_desc f_node f_var f_const expr_desc =
460
   let re = rename_expr  f_node f_var f_const in
461
   match expr_desc with
462
   | Expr_const _ -> expr_desc
463
   | Expr_ident i -> Expr_ident (f_var i)
464
   | Expr_array el -> Expr_array (List.map re el)
465
   | Expr_access (e1, d) -> Expr_access (re e1, d)
466
   | Expr_power (e1, d) -> Expr_power (re e1, d)
467
   | Expr_tuple el -> Expr_tuple (List.map re el)
468
   | Expr_ite (c, t, e) -> Expr_ite (re c, re t, re e)
469
   | Expr_arrow (e1, e2)-> Expr_arrow (re e1, re e2) 
470
   | Expr_fby (e1, e2) -> Expr_fby (re e1, re e2)
471
   | Expr_pre e' -> Expr_pre (re e')
472
   | Expr_when (e', i, l)-> Expr_when (re e', f_var i, l)
473
   | Expr_merge (i, hl) -> 
474
     Expr_merge (f_var i, List.map (fun (t, h) -> (t, re h)) hl)
475
   | Expr_appl (i, e', i') -> 
476
     Expr_appl (f_node i, re e', Utils.option_map (fun (x, l) -> f_var x, l) i')
477
  
478
 let rename_node_annot f_node f_var f_const expr  =
479
   expr
480
 (* TODO assert false *)
481

    
482
 let rename_expr_annot f_node f_var f_const annot =
483
   annot
484
 (* TODO assert false *)
485

    
486
let rename_node f_node f_var f_const nd =
487
  let rename_var v = { v with var_id = f_var v.var_id } in
488
  let rename_eq eq = { eq with
489
      eq_lhs = List.map f_var eq.eq_lhs; 
490
      eq_rhs = rename_expr f_node f_var f_const eq.eq_rhs
491
    } 
492
  in
493
  let inputs = List.map rename_var nd.node_inputs in
494
  let outputs = List.map rename_var nd.node_outputs in
495
  let locals = List.map rename_var nd.node_locals in
496
  let gen_calls = List.map (rename_expr f_node f_var f_const) nd.node_gencalls in
497
  let node_checks = List.map (Dimension.expr_replace_var f_var)  nd.node_checks in
498
  let node_asserts = List.map 
499
    (fun a -> 
500
      {a with assert_expr = 
501
	  let expr = a.assert_expr in
502
	  rename_expr f_node f_var f_const expr})
503
    nd.node_asserts
504
  in
505
  let eqs = List.map rename_eq nd.node_eqs in
506
  let spec = 
507
    Utils.option_map 
508
      (fun s -> rename_node_annot f_node f_var f_const s) 
509
      nd.node_spec 
510
  in
511
  let annot =
512
    List.map 
513
      (fun s -> rename_expr_annot f_node f_var f_const s) 
514
      nd.node_annot
515
  in
516
  {
517
    node_id = f_node nd.node_id;
518
    node_type = nd.node_type;
519
    node_clock = nd.node_clock;
520
    node_inputs = inputs;
521
    node_outputs = outputs;
522
    node_locals = locals;
523
    node_gencalls = gen_calls;
524
    node_checks = node_checks;
525
    node_asserts = node_asserts;
526
    node_eqs = eqs;
527
    node_dec_stateless = nd.node_dec_stateless;
528
    node_stateless = nd.node_stateless;
529
    node_spec = spec;
530
    node_annot = annot;
531
  }
532

    
533

    
534
let rename_const f_const c =
535
  { c with const_id = f_const c.const_id }
536
    
537
let rename_prog f_node f_var f_const prog =
538
  List.rev (
539
    List.fold_left (fun accu top ->
540
      (match top.top_decl_desc with
541
      | Node nd -> 
542
	{ top with top_decl_desc = Node (rename_node f_node f_var f_const nd) }
543
      | Consts c -> 
544
	{ top with top_decl_desc = Consts (List.map (rename_const f_const) c) }
545
      | ImportedNode _
546
      | Open _ -> top)
547
      ::accu
548
) [] prog
549
  )
550

    
551
(**********************************************************************)
552
(* Pretty printers *)
553

    
554
let pp_decl_type fmt tdecl =
555
  match tdecl.top_decl_desc with
556
  | Node nd ->
557
    fprintf fmt "%s: " nd.node_id;
558
    Utils.reset_names ();
559
    fprintf fmt "%a@ " Types.print_ty nd.node_type
560
  | ImportedNode ind ->
561
    fprintf fmt "%s: " ind.nodei_id;
562
    Utils.reset_names ();
563
    fprintf fmt "%a@ " Types.print_ty ind.nodei_type
564
  | Consts _ | Open _ -> ()
565

    
566
let pp_prog_type fmt tdecl_list =
567
  Utils.fprintf_list ~sep:"" pp_decl_type fmt tdecl_list
568

    
569
let pp_decl_clock fmt cdecl =
570
  match cdecl.top_decl_desc with
571
  | Node nd ->
572
    fprintf fmt "%s: " nd.node_id;
573
    Utils.reset_names ();
574
    fprintf fmt "%a@ " Clocks.print_ck nd.node_clock
575
  | ImportedNode ind ->
576
    fprintf fmt "%s: " ind.nodei_id;
577
    Utils.reset_names ();
578
    fprintf fmt "%a@ " Clocks.print_ck ind.nodei_clock
579
  | Consts _ | Open _ -> ()
580

    
581
let pp_prog_clock fmt prog =
582
  Utils.fprintf_list ~sep:"" pp_decl_clock fmt prog
583

    
584
let pp_error fmt = function
585
    Main_not_found ->
586
      fprintf fmt "Cannot compile node %s: could not find the node definition.@."
587
	!Options.main_node
588
  | Main_wrong_kind ->
589
    fprintf fmt
590
      "Name %s does not correspond to a (non-imported) node definition.@." 
591
      !Options.main_node
592
  | No_main_specified ->
593
    fprintf fmt "No main node specified@."
594
  | Unbound_symbol sym ->
595
    fprintf fmt
596
      "%s is undefined.@."
597
      sym
598
  | Already_bound_symbol sym -> 
599
    fprintf fmt
600
      "%s is already defined.@."
601
      sym
602

    
603
(* filling node table with internal functions *)
604
let vdecls_of_typ_ck cpt ty =
605
  let loc = Location.dummy_loc in
606
  List.map
607
    (fun _ -> incr cpt;
608
              let name = sprintf "_var_%d" !cpt in
609
              mkvar_decl loc (name, mktyp loc Tydec_any, mkclock loc Ckdec_any, false))
610
    (Types.type_list_of_type ty)
611

    
612
let mk_internal_node id =
613
  let spec = None in
614
  let ty = Env.lookup_value Basic_library.type_env id in
615
  let ck = Env.lookup_value Basic_library.clock_env id in
616
  let (tin, tout) = Types.split_arrow ty in
617
  (*eprintf "internal fun %s: %d -> %d@." id (List.length (Types.type_list_of_type tin)) (List.length (Types.type_list_of_type tout));*)
618
  let cpt = ref (-1) in
619
  mktop_decl Location.dummy_loc
620
    (ImportedNode
621
       {nodei_id = id;
622
	nodei_type = ty;
623
	nodei_clock = ck;
624
	nodei_inputs = vdecls_of_typ_ck cpt tin;
625
	nodei_outputs = vdecls_of_typ_ck cpt tout;
626
	nodei_stateless = Types.get_static_value ty <> None;
627
	nodei_spec = spec;
628
	nodei_prototype = None;
629
       	nodei_in_lib = None;
630
       })
631

    
632
let add_internal_funs () =
633
  List.iter
634
    (fun id -> let nd = mk_internal_node id in Hashtbl.add node_table id nd)
635
    Basic_library.internal_funs
636

    
637

    
638

    
639
(* Replace any occurence of a var in vars_to_replace by its associated
640
   expression in defs until e does not contain any such variables *)
641
let rec substitute_expr vars_to_replace defs e =
642
  let se = substitute_expr vars_to_replace defs in
643
  { e with expr_desc = 
644
      let ed = e.expr_desc in
645
      match ed with
646
      | Expr_const _ -> ed
647
      | Expr_array el -> Expr_array (List.map se el)
648
      | Expr_access (e1, d) -> Expr_access (se e1, d)
649
      | Expr_power (e1, d) -> Expr_power (se e1, d)
650
      | Expr_tuple el -> Expr_tuple (List.map se el)
651
      | Expr_ite (c, t, e) -> Expr_ite (se c, se t, se e)
652
      | Expr_arrow (e1, e2)-> Expr_arrow (se e1, se e2) 
653
      | Expr_fby (e1, e2) -> Expr_fby (se e1, se e2)
654
      | Expr_pre e' -> Expr_pre (se e')
655
      | Expr_when (e', i, l)-> Expr_when (se e', i, l)
656
      | Expr_merge (i, hl) -> Expr_merge (i, List.map (fun (t, h) -> (t, se h)) hl)
657
      | Expr_appl (i, e', i') -> Expr_appl (i, se e', i')
658
      | Expr_ident i -> 
659
	if List.exists (fun v -> v.var_id = i) vars_to_replace then (
660
	  let eq_i eq = eq.eq_lhs = [i] in
661
	  if List.exists eq_i defs then
662
	    let sub = List.find eq_i defs in
663
	    let sub' = se sub.eq_rhs in
664
	    sub'.expr_desc
665
	  else 
666
	    assert false
667
	)
668
	else
669
	  ed
670

    
671
  }
672
(* FAUT IL RETIRER ?
673
  
674
 let rec expr_to_eexpr  expr =
675
   { eexpr_tag = expr.expr_tag;
676
     eexpr_desc = expr_desc_to_eexpr_desc expr.expr_desc;
677
     eexpr_type = expr.expr_type;
678
     eexpr_clock = expr.expr_clock;
679
     eexpr_loc = expr.expr_loc
680
   }
681
 and expr_desc_to_eexpr_desc expr_desc =
682
   let conv = expr_to_eexpr in
683
   match expr_desc with
684
   | Expr_const c -> EExpr_const (match c with
685
     | Const_int x -> EConst_int x 
686
     | Const_real x -> EConst_real x 
687
     | Const_float x -> EConst_float x 
688
     | Const_tag x -> EConst_tag x 
689
     | _ -> assert false
690

    
691
   )
692
   | Expr_ident i -> EExpr_ident i
693
   | Expr_tuple el -> EExpr_tuple (List.map conv el)
694

    
695
   | Expr_arrow (e1, e2)-> EExpr_arrow (conv e1, conv e2) 
696
   | Expr_fby (e1, e2) -> EExpr_fby (conv e1, conv e2)
697
   | Expr_pre e' -> EExpr_pre (conv e')
698
   | Expr_appl (i, e', i') -> 
699
     EExpr_appl 
700
       (i, conv e', match i' with None -> None | Some(id, _) -> Some id)
701

    
702
   | Expr_when _
703
   | Expr_merge _ -> assert false
704
   | Expr_array _ 
705
   | Expr_access _ 
706
   | Expr_power _  -> assert false
707
   | Expr_ite (c, t, e) -> assert false 
708
   | _ -> assert false
709

    
710
     *)
711
let rec get_expr_calls nodes e =
712
  get_calls_expr_desc nodes e.expr_desc
713
and get_calls_expr_desc nodes expr_desc =
714
  let get_calls = get_expr_calls nodes in
715
  match expr_desc with
716
  | Expr_const _ 
717
   | Expr_ident _ -> Utils.ISet.empty
718
   | Expr_tuple el
719
   | Expr_array el -> List.fold_left (fun accu e -> Utils.ISet.union accu (get_calls e)) Utils.ISet.empty el
720
   | Expr_pre e1 
721
   | Expr_when (e1, _, _) 
722
   | Expr_access (e1, _) 
723
   | Expr_power (e1, _) -> get_calls e1
724
   | Expr_ite (c, t, e) -> Utils.ISet.union (Utils.ISet.union (get_calls c) (get_calls t)) (get_calls e) 
725
   | Expr_arrow (e1, e2) 
726
   | Expr_fby (e1, e2) -> Utils.ISet.union (get_calls e1) (get_calls e2)
727
   | Expr_merge (_, hl) -> List.fold_left (fun accu (_, h) -> Utils.ISet.union accu (get_calls h)) Utils.ISet.empty  hl
728
   | Expr_appl (i, e', i') -> 
729
     if Basic_library.is_internal_fun i then 
730
       (get_calls e') 
731
     else
732
       let calls =  Utils.ISet.add i (get_calls e') in
733
       let test = (fun n -> match n.top_decl_desc with Node nd -> nd.node_id = i | _ -> false) in
734
       if List.exists test nodes then
735
	 match (List.find test nodes).top_decl_desc with
736
	 | Node nd -> Utils.ISet.union (get_node_calls nodes nd) calls
737
	 | _ -> assert false
738
       else 
739
	 calls
740

    
741
and get_eq_calls nodes eq =
742
  get_expr_calls nodes eq.eq_rhs
743
and get_node_calls nodes node =
744
  List.fold_left (fun accu eq -> Utils.ISet.union (get_eq_calls nodes eq) accu) Utils.ISet.empty node.node_eqs
745

    
746

    
747
let rec expr_has_arrows e =
748
  expr_desc_has_arrows e.expr_desc
749
and expr_desc_has_arrows expr_desc =
750
  match expr_desc with
751
  | Expr_const _ 
752
  | Expr_ident _ -> false
753
  | Expr_tuple el
754
  | Expr_array el -> List.exists expr_has_arrows el
755
  | Expr_pre e1 
756
  | Expr_when (e1, _, _) 
757
  | Expr_access (e1, _) 
758
  | Expr_power (e1, _) -> expr_has_arrows e1
759
  | Expr_ite (c, t, e) -> List.exists expr_has_arrows [c; t; e]
760
  | Expr_arrow (e1, e2) 
761
  | Expr_fby (e1, e2) -> true
762
  | Expr_merge (_, hl) -> List.exists (fun (_, h) -> expr_has_arrows h) hl
763
  | Expr_appl (i, e', i') -> expr_has_arrows e'
764

    
765
and eq_has_arrows eq =
766
  expr_has_arrows eq.eq_rhs
767
and node_has_arrows node =
768
  List.exists (fun eq -> eq_has_arrows eq) node.node_eqs
769

    
770
(* Local Variables: *)
771
(* compile-command:"make -C .." *)
772
(* End: *)