Project

General

Profile

Statistics
| Branch: | Tag: | Revision:

lustrec / src / corelang.ml @ 95944ba1

History | View | Annotate | Download (42.2 KB)

1 a2d97a3e ploc
(********************************************************************)
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 22fe1c93 ploc
open Format
13 8446bf03 ploc
open Lustre_types
14
open Machine_code_types
15 7d640c88 ploc
(*open Dimension*)
16 22fe1c93 ploc
17
18 e7cc5186 ploc
exception Error of Location.t * Error.error_kind
19 22fe1c93 ploc
20
module VDeclModule =
21
struct (* Node module *)
22
  type t = var_decl
23 bb2ca5f4 xthirioux
  let compare v1 v2 = compare v1.var_id v2.var_id
24 22fe1c93 ploc
end
25
26
module VMap = Map.Make(VDeclModule)
27
28 c35de73b ploc
module VSet: sig
29
  include Set.S
30
  val pp: Format.formatter -> t -> unit 
31
end with type elt = var_decl =
32
  struct
33
    include Set.Make(VDeclModule)
34
    let pp fmt s =
35
      Format.fprintf fmt "{@[%a}@]" (Utils.fprintf_list ~sep:",@ " Printers.pp_var) (elements s)  
36
  end
37 01c7d5e1 ploc
let dummy_type_dec = {ty_dec_desc=Tydec_any; ty_dec_loc=Location.dummy_loc}
38
39
let dummy_clock_dec = {ck_dec_desc=Ckdec_any; ck_dec_loc=Location.dummy_loc}
40
41
42
43 22fe1c93 ploc
(************************************************************)
44
(* *)
45
46
let mktyp loc d =
47
  { ty_dec_desc = d; ty_dec_loc = loc }
48
49
let mkclock loc d =
50
  { ck_dec_desc = d; ck_dec_loc = loc }
51
52 66359a5e ploc
let mkvar_decl loc ?(orig=false) (id, ty_dec, ck_dec, is_const, value, parentid) =
53 ec433d69 xthirioux
  assert (value = None || is_const);
54 22fe1c93 ploc
  { var_id = id;
55 54d032f5 xthirioux
    var_orig = orig;
56 22fe1c93 ploc
    var_dec_type = ty_dec;
57
    var_dec_clock = ck_dec;
58
    var_dec_const = is_const;
59 ec433d69 xthirioux
    var_dec_value = value;
60 66359a5e ploc
    var_parent_nodeid = parentid;
61 22fe1c93 ploc
    var_type = Types.new_var ();
62
    var_clock = Clocks.new_var true;
63
    var_loc = loc }
64
65 2863281f ploc
let dummy_var_decl name typ =
66
  {
67
    var_id = name;
68
    var_orig = false;
69
    var_dec_type = dummy_type_dec;
70
    var_dec_clock = dummy_clock_dec;
71
    var_dec_const = false;
72
    var_dec_value = None;
73
    var_parent_nodeid = None;
74
    var_type =  typ;
75
    var_clock = Clocks.new_ck Clocks.Cvar true;
76
    var_loc = Location.dummy_loc
77
  }
78
79 22fe1c93 ploc
let mkexpr loc d =
80
  { expr_tag = Utils.new_tag ();
81
    expr_desc = d;
82
    expr_type = Types.new_var ();
83
    expr_clock = Clocks.new_var true;
84
    expr_delay = Delay.new_var ();
85
    expr_annot = None;
86
    expr_loc = loc }
87
88 66359a5e ploc
let var_decl_of_const ?(parentid=None) c =
89 22fe1c93 ploc
  { var_id = c.const_id;
90 54d032f5 xthirioux
    var_orig = true;
91 22fe1c93 ploc
    var_dec_type = { ty_dec_loc = c.const_loc; ty_dec_desc = Tydec_any };
92
    var_dec_clock = { ck_dec_loc = c.const_loc; ck_dec_desc = Ckdec_any };
93
    var_dec_const = true;
94 ec433d69 xthirioux
    var_dec_value = None;
95 66359a5e ploc
    var_parent_nodeid = parentid;
96 22fe1c93 ploc
    var_type = c.const_type;
97
    var_clock = Clocks.new_var false;
98
    var_loc = c.const_loc }
99
100 b08ffca7 xthirioux
let mk_new_name used id =
101 22fe1c93 ploc
  let rec new_name name cpt =
102 b08ffca7 xthirioux
    if used name
103 22fe1c93 ploc
    then new_name (sprintf "_%s_%i" id cpt) (cpt+1)
104
    else name
105
  in new_name id 1
106
107
let mkeq loc (lhs, rhs) =
108
  { eq_lhs = lhs;
109
    eq_rhs = rhs;
110
    eq_loc = loc }
111
112
let mkassert loc expr =
113
  { assert_loc = loc;
114
    assert_expr = expr
115
  }
116
117 ef34b4ae xthirioux
let mktop_decl loc own itf d =
118
  { top_decl_desc = d; top_decl_loc = loc; top_decl_owner = own; top_decl_itf = itf }
119 22fe1c93 ploc
120
let mkpredef_call loc funname args =
121
  mkexpr loc (Expr_appl (funname, mkexpr loc (Expr_tuple args), None))
122
123 ec433d69 xthirioux
let is_clock_dec_type cty =
124
  match cty with
125
  | Tydec_clock _ -> true
126
  | _             -> false
127 ef34b4ae xthirioux
128
let const_of_top top_decl =
129
  match top_decl.top_decl_desc with
130
  | Const c -> c
131
  | _ -> assert false
132
133
let node_of_top top_decl =
134
  match top_decl.top_decl_desc with
135
  | Node nd -> nd
136 2475c9e8 ploc
  | _ -> raise Not_found
137 ef34b4ae xthirioux
138
let imported_node_of_top top_decl =
139
  match top_decl.top_decl_desc with
140
  | ImportedNode ind -> ind
141
  | _ -> assert false
142
143
let typedef_of_top top_decl =
144
  match top_decl.top_decl_desc with
145
  | TypeDef tdef -> tdef
146
  | _ -> assert false
147
148
let dependency_of_top top_decl =
149
  match top_decl.top_decl_desc with
150
  | Open (local, dep) -> (local, dep)
151
  | _ -> assert false
152
153
let consts_of_enum_type top_decl =
154
  match top_decl.top_decl_desc with
155
  | TypeDef tdef ->
156
    (match tdef.tydef_desc with
157 333e3a25 ploc
    | Tydec_enum tags ->
158
       List.map
159
	 (fun tag ->
160
	   let cdecl = {
161
	     const_id = tag;
162
	     const_loc = top_decl.top_decl_loc;
163
	     const_value = Const_tag tag;
164
	     const_type = Type_predef.type_const tdef.tydef_id
165
	   } in
166
	   { top_decl with top_decl_desc = Const cdecl })
167
	 tags
168 ef34b4ae xthirioux
     | _               -> [])
169
  | _ -> assert false
170
171 01c7d5e1 ploc
(************************************************************)
172
(*   Eexpr functions *)
173
(************************************************************)
174
175 778c80fd ploc
176
let empty_contract =
177
  {
178 f9f06e7d ploc
    consts = []; locals = []; stmts = []; assume = []; guarantees = []; modes = []; imports = []; spec_loc = Location.dummy_loc;
179 778c80fd ploc
  }
180 f9f06e7d ploc
181
(* For const declaration we do as for regular lustre node.
182
But for local flows we registered the variable and the lustre flow definition *)
183 778c80fd ploc
let mk_contract_var id is_const type_opt expr loc =
184
  let typ = match type_opt with None -> mktyp loc Tydec_any | Some t -> t in
185
  if is_const then
186 f9f06e7d ploc
  let v = mkvar_decl loc (id, typ, mkclock loc Ckdec_any, is_const, Some expr, None) in
187 778c80fd ploc
  { empty_contract with consts = [v]; spec_loc = loc; }
188
  else
189 f9f06e7d ploc
    let v = mkvar_decl loc (id, typ, mkclock loc Ckdec_any, is_const, None, None) in
190
    let eq = mkeq loc ([id], expr) in 
191
    { empty_contract with locals = [v]; stmts = [Eq eq]; spec_loc = loc; }
192 778c80fd ploc
193
let mk_contract_guarantees eexpr =
194
  { empty_contract with guarantees = [eexpr]; spec_loc = eexpr.eexpr_loc }
195
196
let mk_contract_assume eexpr =
197
  { empty_contract with assume = [eexpr]; spec_loc = eexpr.eexpr_loc }
198
199
let mk_contract_mode id rl el loc =
200
  { empty_contract with modes = [{ mode_id = id; require = rl; ensure = el; mode_loc = loc; }]; spec_loc = loc }
201
202
let mk_contract_import id ins outs loc =
203
  { empty_contract with imports = [{import_nodeid = id; inputs = ins; outputs = outs; import_loc = loc; }]; spec_loc = loc }
204
205
    
206
let merge_contracts ann1 ann2 = (* keeping the first item loc *)
207
  { consts = ann1.consts @ ann2.consts;
208
    locals = ann1.locals @ ann2.locals;
209 f9f06e7d ploc
    stmts = ann1.stmts @ ann2.stmts;
210 778c80fd ploc
    assume = ann1.assume @ ann2.assume;
211
    guarantees = ann1.guarantees @ ann2.guarantees;
212
    modes = ann1.modes @ ann2.modes;
213
    imports = ann1.imports @ ann2.imports;
214 01c7d5e1 ploc
    spec_loc = ann1.spec_loc
215
  }
216
217
let mkeexpr loc expr =
218
  { eexpr_tag = Utils.new_tag ();
219
    eexpr_qfexpr = expr;
220
    eexpr_quantifiers = [];
221
    eexpr_type = Types.new_var ();
222
    eexpr_clock = Clocks.new_var true;
223
    eexpr_normalized = None;
224
    eexpr_loc = loc }
225
226
let extend_eexpr q e = { e with eexpr_quantifiers = q@e.eexpr_quantifiers }
227
228
(*
229
let mkepredef_call loc funname args =
230
  mkeexpr loc (EExpr_appl (funname, mkeexpr loc (EExpr_tuple args), None))
231
232
let mkepredef_unary_call loc funname arg =
233
  mkeexpr loc (EExpr_appl (funname, arg, None))
234
*)
235
236
let merge_expr_annot ann1 ann2 =
237
  match ann1, ann2 with
238
    | None, None -> assert false
239
    | Some _, None -> ann1
240
    | None, Some _ -> ann2
241
    | Some ann1, Some ann2 -> Some {
242
      annots = ann1.annots @ ann2.annots;
243
      annot_loc = ann1.annot_loc
244
    }
245
246 566dbf49 ploc
let update_expr_annot node_id e annot =
247
  List.iter (fun (key, _) -> 
248
    Annotations.add_expr_ann node_id e.expr_tag key
249
  ) annot.annots;
250 264a4844 ploc
  e.expr_annot <- merge_expr_annot e.expr_annot (Some annot);
251
  e
252 01c7d5e1 ploc
253
254 1b683c9a ploc
let mkinstr ?lustre_expr ?lustre_eq i =
255 3ca27bc7 ploc
  {
256
    instr_desc = i;
257 1bff14ac ploc
    (* lustre_expr = lustre_expr; *)
258
    lustre_eq = lustre_eq;
259 3ca27bc7 ploc
  }
260
261
let get_instr_desc i = i.instr_desc
262
let update_instr_desc i id = { i with instr_desc = id }
263
264 22fe1c93 ploc
(***********************************************************)
265
(* Fast access to nodes, by name *)
266
let (node_table : (ident, top_decl) Hashtbl.t) = Hashtbl.create 30
267
let consts_table = Hashtbl.create 30
268
269 ef34b4ae xthirioux
let print_node_table fmt () =
270
  begin
271
    Format.fprintf fmt "{ /* node table */@.";
272
    Hashtbl.iter (fun id nd ->
273
      Format.fprintf fmt "%s |-> %a"
274
	id
275
	Printers.pp_short_decl nd
276
    ) node_table;
277
    Format.fprintf fmt "}@."
278
  end
279
280
let print_consts_table fmt () =
281
  begin
282
    Format.fprintf fmt "{ /* consts table */@.";
283
    Hashtbl.iter (fun id const ->
284
      Format.fprintf fmt "%s |-> %a"
285
	id
286
	Printers.pp_const_decl (const_of_top const)
287
    ) consts_table;
288
    Format.fprintf fmt "}@."
289
  end
290
291 22fe1c93 ploc
let node_name td =
292
    match td.top_decl_desc with 
293
    | Node nd         -> nd.node_id
294
    | ImportedNode nd -> nd.nodei_id
295
    | _ -> assert false
296
297
let is_generic_node td =
298
  match td.top_decl_desc with 
299
  | Node nd         -> List.exists (fun v -> v.var_dec_const) nd.node_inputs
300
  | ImportedNode nd -> List.exists (fun v -> v.var_dec_const) nd.nodei_inputs
301
  | _ -> assert false
302
303
let node_inputs td =
304
  match td.top_decl_desc with 
305
  | Node nd         -> nd.node_inputs
306
  | ImportedNode nd -> nd.nodei_inputs
307
  | _ -> assert false
308
309
let node_from_name id =
310 95944ba1 ploc
      Hashtbl.find node_table id
311
  (* with Not_found -> (Format.eprintf "Unable to find any node named %s@ @?" id;
312
   *       	     assert false) *)
313
314
let update_node id top =
315
  Hashtbl.replace node_table id top
316 22fe1c93 ploc
317
let is_imported_node td =
318
  match td.top_decl_desc with 
319
  | Node nd         -> false
320
  | ImportedNode nd -> true
321
  | _ -> assert false
322
323 52cfee34 xthirioux
324 22fe1c93 ploc
(* alias and type definition table *)
325 ef34b4ae xthirioux
326 990210f3 ploc
let mktop = mktop_decl Location.dummy_loc !Options.dest_dir false
327 1e48ef45 ploc
328
let top_int_type = mktop (TypeDef {tydef_id = "int"; tydef_desc = Tydec_int})
329
let top_bool_type = mktop (TypeDef {tydef_id = "bool"; tydef_desc = Tydec_bool})
330 04a63d25 xthirioux
(* let top_float_type = mktop (TypeDef {tydef_id = "float"; tydef_desc = Tydec_float}) *)
331 1e48ef45 ploc
let top_real_type = mktop (TypeDef {tydef_id = "real"; tydef_desc = Tydec_real})
332 ef34b4ae xthirioux
333 22fe1c93 ploc
let type_table =
334
  Utils.create_hashtable 20 [
335 ef34b4ae xthirioux
    Tydec_int  , top_int_type;
336
    Tydec_bool , top_bool_type;
337 04a63d25 xthirioux
    (* Tydec_float, top_float_type; *)
338 ef34b4ae xthirioux
    Tydec_real , top_real_type
339 22fe1c93 ploc
  ]
340
341 ef34b4ae xthirioux
let print_type_table fmt () =
342
  begin
343
    Format.fprintf fmt "{ /* type table */@.";
344
    Hashtbl.iter (fun tydec tdef ->
345
      Format.fprintf fmt "%a |-> %a"
346
	Printers.pp_var_type_dec_desc tydec
347
	Printers.pp_typedef (typedef_of_top tdef)
348
    ) type_table;
349
    Format.fprintf fmt "}@."
350
  end
351
352 22fe1c93 ploc
let rec is_user_type typ =
353
  match typ with
354
  | Tydec_int | Tydec_bool | Tydec_real 
355 04a63d25 xthirioux
  (* | Tydec_float *) | Tydec_any | Tydec_const _ -> false
356 22fe1c93 ploc
  | Tydec_clock typ' -> is_user_type typ'
357
  | _ -> true
358
359
let get_repr_type typ =
360 ef34b4ae xthirioux
  let typ_def = (typedef_of_top (Hashtbl.find type_table typ)).tydef_desc in
361 22fe1c93 ploc
  if is_user_type typ_def then typ else typ_def
362
363 b1655a21 xthirioux
let rec coretype_equal ty1 ty2 =
364 ef34b4ae xthirioux
  let res =
365 b1655a21 xthirioux
  match ty1, ty2 with
366 ed81df06 xthirioux
  | Tydec_any           , _
367
  | _                   , Tydec_any             -> assert false
368
  | Tydec_const _       , Tydec_const _         -> get_repr_type ty1 = get_repr_type ty2
369 ef34b4ae xthirioux
  | Tydec_const _       , _                     -> let ty1' = (typedef_of_top (Hashtbl.find type_table ty1)).tydef_desc
370 ed81df06 xthirioux
	       					   in (not (is_user_type ty1')) && coretype_equal ty1' ty2
371
  | _                   , Tydec_const _         -> coretype_equal ty2 ty1
372
  | Tydec_int           , Tydec_int
373
  | Tydec_real          , Tydec_real
374 04a63d25 xthirioux
  (* | Tydec_float         , Tydec_float *)
375 ed81df06 xthirioux
  | Tydec_bool          , Tydec_bool            -> true
376
  | Tydec_clock ty1     , Tydec_clock ty2       -> coretype_equal ty1 ty2
377
  | Tydec_array (d1,ty1), Tydec_array (d2, ty2) -> Dimension.is_eq_dimension d1 d2 && coretype_equal ty1 ty2
378
  | Tydec_enum tl1      , Tydec_enum tl2        -> List.sort compare tl1 = List.sort compare tl2
379
  | Tydec_struct fl1    , Tydec_struct fl2      ->
380 b1655a21 xthirioux
       List.length fl1 = List.length fl2
381
    && List.for_all2 (fun (f1, t1) (f2, t2) -> f1 = f2 && coretype_equal t1 t2)
382
      (List.sort (fun (f1,_) (f2,_) -> compare f1 f2) fl1)
383
      (List.sort (fun (f1,_) (f2,_) -> compare f1 f2) fl2)
384
  | _                                  -> false
385 ef34b4ae xthirioux
  in ((*Format.eprintf "coretype_equal %a %a = %B@." Printers.pp_var_type_dec_desc ty1 Printers.pp_var_type_dec_desc ty2 res;*) res)
386 b1655a21 xthirioux
387 22fe1c93 ploc
let tag_true = "true"
388
let tag_false = "false"
389 77a61575 xthirioux
let tag_default = "default"
390 22fe1c93 ploc
391
let const_is_bool c =
392
 match c with
393
 | Const_tag t -> t = tag_true || t = tag_false
394
 | _           -> false
395
396
(* Computes the negation of a boolean constant *)
397
let const_negation c =
398
  assert (const_is_bool c);
399
  match c with
400
  | Const_tag t when t = tag_true  -> Const_tag tag_false
401
  | _                              -> Const_tag tag_true
402
403
let const_or c1 c2 =
404
  assert (const_is_bool c1 && const_is_bool c2);
405
  match c1, c2 with
406
  | Const_tag t1, _            when t1 = tag_true -> c1
407
  | _           , Const_tag t2 when t2 = tag_true -> c2
408
  | _                                             -> Const_tag tag_false
409
410
let const_and c1 c2 =
411
  assert (const_is_bool c1 && const_is_bool c2);
412
  match c1, c2 with
413
  | Const_tag t1, _            when t1 = tag_false -> c1
414
  | _           , Const_tag t2 when t2 = tag_false -> c2
415
  | _                                              -> Const_tag tag_true
416
417
let const_xor c1 c2 =
418
  assert (const_is_bool c1 && const_is_bool c2);
419
   match c1, c2 with
420
  | Const_tag t1, Const_tag t2 when t1 <> t2  -> Const_tag tag_true
421
  | _                                         -> Const_tag tag_false
422
423
let const_impl c1 c2 =
424
  assert (const_is_bool c1 && const_is_bool c2);
425
  match c1, c2 with
426
  | Const_tag t1, _ when t1 = tag_false           -> Const_tag tag_true
427
  | _           , Const_tag t2 when t2 = tag_true -> Const_tag tag_true
428
  | _                                             -> Const_tag tag_false
429
430
(* To guarantee uniqueness of tags in enum types *)
431
let tag_table =
432
  Utils.create_hashtable 20 [
433 ef34b4ae xthirioux
   tag_true, top_bool_type;
434
   tag_false, top_bool_type
435 22fe1c93 ploc
  ]
436
437 fa090c4e xthirioux
(* To guarantee uniqueness of fields in struct types *)
438
let field_table =
439
  Utils.create_hashtable 20 [
440
  ]
441
442 22fe1c93 ploc
let get_enum_type_tags cty =
443 ef34b4ae xthirioux
(*Format.eprintf "get_enum_type_tags %a@." Printers.pp_var_type_dec_desc cty;*)
444 22fe1c93 ploc
 match cty with
445
 | Tydec_bool    -> [tag_true; tag_false]
446 ef34b4ae xthirioux
 | Tydec_const _ -> (match (typedef_of_top (Hashtbl.find type_table cty)).tydef_desc with
447 22fe1c93 ploc
                     | Tydec_enum tl -> tl
448
                     | _             -> assert false)
449
 | _            -> assert false
450 6a6abd76 xthirioux
451 fa090c4e xthirioux
let get_struct_type_fields cty =
452
 match cty with
453 ef34b4ae xthirioux
 | Tydec_const _ -> (match (typedef_of_top (Hashtbl.find type_table cty)).tydef_desc with
454 fa090c4e xthirioux
                     | Tydec_struct fl -> fl
455
                     | _               -> assert false)
456
 | _            -> assert false
457 6a6abd76 xthirioux
458 22fe1c93 ploc
let const_of_bool b =
459
 Const_tag (if b then tag_true else tag_false)
460
461
(* let get_const c = snd (Hashtbl.find consts_table c) *)
462
463 add75bcb xthirioux
let ident_of_expr expr =
464
 match expr.expr_desc with
465
 | Expr_ident id -> id
466
 | _             -> assert false
467
468 fc886259 xthirioux
(* Generate a new ident expression from a declared variable *)
469
let expr_of_vdecl v =
470
  { expr_tag = Utils.new_tag ();
471
    expr_desc = Expr_ident v.var_id;
472
    expr_type = v.var_type;
473
    expr_clock = v.var_clock;
474
    expr_delay = Delay.new_var ();
475
    expr_annot = None;
476
    expr_loc = v.var_loc }
477
478 22fe1c93 ploc
(* Caution, returns an untyped and unclocked expression *)
479
let expr_of_ident id loc =
480
  {expr_tag = Utils.new_tag ();
481
   expr_desc = Expr_ident id;
482
   expr_type = Types.new_var ();
483
   expr_clock = Clocks.new_var true;
484
   expr_delay = Delay.new_var ();
485
   expr_loc = loc;
486
   expr_annot = None}
487
488 b616fe7a xthirioux
let is_tuple_expr expr =
489
 match expr.expr_desc with
490
  | Expr_tuple _ -> true
491
  | _            -> false
492
493 22fe1c93 ploc
let expr_list_of_expr expr =
494
  match expr.expr_desc with
495 bb2ca5f4 xthirioux
  | Expr_tuple elist -> elist
496
  | _                -> [expr]
497 22fe1c93 ploc
498
let expr_of_expr_list loc elist =
499
 match elist with
500
 | [t]  -> { t with expr_loc = loc }
501 70df2f87 xthirioux
 | t::_ ->
502
    let tlist = List.map (fun e -> e.expr_type) elist in
503
    let clist = List.map (fun e -> e.expr_clock) elist in
504
    { t with expr_desc = Expr_tuple elist;
505
	     expr_type = Type_predef.type_tuple tlist;
506
	     expr_clock = Clock_predef.ck_tuple clist;
507
	     expr_tag = Utils.new_tag ();
508
	     expr_loc = loc }
509 22fe1c93 ploc
 | _    -> assert false
510
511
let call_of_expr expr =
512
 match expr.expr_desc with
513
 | Expr_appl (f, args, r) -> (f, expr_list_of_expr args, r)
514
 | _                      -> assert false
515
516 7d640c88 ploc
    
517 22fe1c93 ploc
(* Conversion from dimension expr to standard expr, for the purpose of printing, typing, etc... *)
518
let rec expr_of_dimension dim =
519 7d640c88 ploc
  let open Dimension in
520
  match dim.dim_desc with
521 22fe1c93 ploc
 | Dbool b        ->
522
     mkexpr dim.dim_loc (Expr_const (const_of_bool b))
523
 | Dint i         ->
524
     mkexpr dim.dim_loc (Expr_const (Const_int i))
525
 | Dident id      ->
526
     mkexpr dim.dim_loc (Expr_ident id)
527
 | Dite (c, t, e) ->
528
     mkexpr dim.dim_loc (Expr_ite (expr_of_dimension c, expr_of_dimension t, expr_of_dimension e))
529
 | Dappl (id, args) ->
530
     mkexpr dim.dim_loc (Expr_appl (id, expr_of_expr_list dim.dim_loc (List.map expr_of_dimension args), None))
531
 | Dlink dim'       -> expr_of_dimension dim'
532
 | Dvar
533 fc886259 xthirioux
 | Dunivar          -> (Format.eprintf "internal error: Corelang.expr_of_dimension %a@." Dimension.pp_dimension dim;
534 22fe1c93 ploc
			assert false)
535
536
let dimension_of_const loc const =
537 7d640c88 ploc
  let open Dimension in
538 22fe1c93 ploc
 match const with
539
 | Const_int i                                    -> mkdim_int loc i
540
 | Const_tag t when t = tag_true || t = tag_false -> mkdim_bool loc (t = tag_true)
541
 | _                                              -> raise InvalidDimension
542
543
(* Conversion from standard expr to dimension expr, for the purpose of injecting static call arguments 
544
   into dimension expressions *)
545
let rec dimension_of_expr expr =
546 7d640c88 ploc
  let open Dimension in
547 22fe1c93 ploc
  match expr.expr_desc with
548
  | Expr_const c  -> dimension_of_const expr.expr_loc c
549
  | Expr_ident id -> mkdim_ident expr.expr_loc id
550 04a63d25 xthirioux
  | Expr_appl (f, args, None) when Basic_library.is_expr_internal_fun expr ->
551 22fe1c93 ploc
      let k = Types.get_static_value (Env.lookup_value Basic_library.type_env f) in
552
      if k = None then raise InvalidDimension;
553
      mkdim_appl expr.expr_loc f (List.map dimension_of_expr (expr_list_of_expr args))
554
  | Expr_ite (i, t, e)        ->
555
      mkdim_ite expr.expr_loc (dimension_of_expr i) (dimension_of_expr t) (dimension_of_expr e)
556
  | _ -> raise InvalidDimension (* not a simple dimension expression *)
557
558
559
let sort_handlers hl =
560
 List.sort (fun (t, _) (t', _) -> compare t t') hl
561
562 e49b6d55 xavier.thirioux
let num_10 = Num.num_of_int 10
563
  
564
let rec is_eq_const c1 c2 =
565
  match c1, c2 with
566
  | Const_real (n1, i1, _), Const_real (n2, i2, _)
567
    -> Num.(let n1 = n1 // (num_10 **/ (num_of_int i1)) in
568
	    let n2 = n2 // (num_10 **/ (num_of_int i2)) in
569
	    eq_num n1 n2)
570
  | Const_struct lcl1, Const_struct lcl2
571
    -> List.length lcl1 = List.length lcl2
572
    && List.for_all2 (fun (l1, c1) (l2, c2) -> l1 = l2 && is_eq_const c1 c2) lcl1 lcl2
573
  | _  -> c1 = c2
574
575 22fe1c93 ploc
let rec is_eq_expr e1 e2 = match e1.expr_desc, e2.expr_desc with
576 e49b6d55 xavier.thirioux
  | Expr_const c1, Expr_const c2 -> is_eq_const c1 c2
577 22fe1c93 ploc
  | Expr_ident i1, Expr_ident i2 -> i1 = i2
578
  | Expr_array el1, Expr_array el2 
579
  | Expr_tuple el1, Expr_tuple el2 -> 
580
    List.length el1 = List.length el2 && List.for_all2 is_eq_expr el1 el2 
581
  | Expr_arrow (e1, e2), Expr_arrow (e1', e2') -> is_eq_expr e1 e1' && is_eq_expr e2 e2'
582
  | Expr_fby (e1,e2), Expr_fby (e1',e2') -> is_eq_expr e1 e1' && is_eq_expr e2 e2'
583
  | 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
584
  (* | Expr_concat (e1,e2), Expr_concat (e1',e2') -> is_eq_expr e1 e1' && is_eq_expr e2 e2' *)
585
  (* | Expr_tail e, Expr_tail e' -> is_eq_expr e e' *)
586
  | Expr_pre e, Expr_pre e' -> is_eq_expr e e'
587
  | Expr_when (e, i, l), Expr_when (e', i', l') -> l=l' && i=i' && is_eq_expr e e'
588
  | 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')
589
  | Expr_appl (i, e, r), Expr_appl (i', e', r') -> i=i' && r=r' && is_eq_expr e e'
590
  | Expr_power (e1, i1), Expr_power (e2, i2)
591
  | Expr_access (e1, i1), Expr_access (e2, i2) -> is_eq_expr e1 e2 && is_eq_expr (expr_of_dimension i1) (expr_of_dimension i2)
592
  | _ -> false
593
594 01c7d5e1 ploc
let get_node_vars nd =
595 22fe1c93 ploc
  nd.node_inputs @ nd.node_locals @ nd.node_outputs
596
597 fc886259 xthirioux
let mk_new_node_name nd id =
598
  let used_vars = get_node_vars nd in
599
  let used v = List.exists (fun vdecl -> vdecl.var_id = v) used_vars in
600
  mk_new_name used id
601
602 01c7d5e1 ploc
let get_var id var_list =
603 04a63d25 xthirioux
  List.find (fun v -> v.var_id = id) var_list
604 22fe1c93 ploc
605 df39e35a xthirioux
let get_node_var id node =
606 76c7023b ploc
  try
607
    get_var id (get_node_vars node)
608
  with Not_found -> begin
609 63f10e14 ploc
    (* Format.eprintf "Unable to find variable %s in node %s@.@?" id node.node_id; *)
610 76c7023b ploc
    raise Not_found
611
  end
612 333e3a25 ploc
613
614 b08ffca7 xthirioux
let get_node_eqs =
615
  let get_eqs stmts =
616
    List.fold_right
617 333e3a25 ploc
      (fun stmt (res_eq, res_aut) ->
618 b08ffca7 xthirioux
	match stmt with
619 333e3a25 ploc
	| Eq eq -> eq :: res_eq, res_aut
620
	| Aut aut -> res_eq, aut::res_aut)
621 b08ffca7 xthirioux
      stmts
622 333e3a25 ploc
      ([], []) in
623 b08ffca7 xthirioux
  let table_eqs = Hashtbl.create 23 in
624
  (fun nd ->
625
    try
626
      let (old, res) = Hashtbl.find table_eqs nd.node_id
627
      in if old == nd.node_stmts then res else raise Not_found
628
    with Not_found -> 
629
      let res = get_eqs nd.node_stmts in
630
      begin
631
	Hashtbl.replace table_eqs nd.node_id (nd.node_stmts, res);
632
	res
633
      end)
634
635 01c7d5e1 ploc
let get_node_eq id node =
636 333e3a25 ploc
  let eqs, auts = get_node_eqs node in
637
  try
638
    List.find (fun eq -> List.mem id eq.eq_lhs) eqs
639
  with
640
    Not_found -> (* Shall be defined in automata auts *) raise Not_found
641
      
642 22fe1c93 ploc
let get_nodes prog = 
643
  List.fold_left (
644
    fun nodes decl ->
645
      match decl.top_decl_desc with
646 ef34b4ae xthirioux
	| Node _ -> decl::nodes
647 19a1e66b ploc
	| Const _ | ImportedNode _ | Include _ | Open _ | TypeDef _ -> nodes  
648 22fe1c93 ploc
  ) [] prog
649
650 ef34b4ae xthirioux
let get_imported_nodes prog = 
651 c1adf235 ploc
  List.fold_left (
652 ef34b4ae xthirioux
    fun nodes decl ->
653 c1adf235 ploc
      match decl.top_decl_desc with
654 ef34b4ae xthirioux
	| ImportedNode _ -> decl::nodes
655 19a1e66b ploc
	| Const _ | Node _ | Include _ | Open _ | TypeDef _-> nodes  
656 c1adf235 ploc
  ) [] prog
657 22fe1c93 ploc
658 ef34b4ae xthirioux
let get_consts prog = 
659
  List.fold_right (
660
    fun decl consts ->
661 b1655a21 xthirioux
      match decl.top_decl_desc with
662 ef34b4ae xthirioux
	| Const _ -> decl::consts
663 19a1e66b ploc
	| Node _ | ImportedNode _ | Include _ | Open _ | TypeDef _ -> consts  
664 ef34b4ae xthirioux
  ) prog []
665
666
let get_typedefs prog = 
667
  List.fold_right (
668
    fun decl types ->
669
      match decl.top_decl_desc with
670
	| TypeDef _ -> decl::types
671 19a1e66b ploc
	| Node _ | ImportedNode _ | Include _ | Open _ | Const _ -> types  
672 ef34b4ae xthirioux
  ) prog []
673
674
let get_dependencies prog =
675
  List.fold_right (
676
    fun decl deps ->
677
      match decl.top_decl_desc with
678
	| Open _ -> decl::deps
679 19a1e66b ploc
	| Node _ | ImportedNode _ | TypeDef _ | Include _ | Const _ -> deps  
680 ef34b4ae xthirioux
  ) prog []
681 e2380d4d ploc
682 ed81df06 xthirioux
let get_node_interface nd =
683
 {nodei_id = nd.node_id;
684
  nodei_type = nd.node_type;
685
  nodei_clock = nd.node_clock;
686
  nodei_inputs = nd.node_inputs;
687
  nodei_outputs = nd.node_outputs;
688
  nodei_stateless = nd.node_dec_stateless;
689
  nodei_spec = nd.node_spec;
690 66359a5e ploc
  (* nodei_annot = nd.node_annot; *)
691 ed81df06 xthirioux
  nodei_prototype = None;
692 04a63d25 xthirioux
  nodei_in_lib = [];
693 ed81df06 xthirioux
 }
694
695 e2380d4d ploc
(************************************************************************)
696
(*        Renaming                                                      *)
697
698 ec433d69 xthirioux
let rec rename_static rename cty =
699
 match cty with
700
 | Tydec_array (d, cty') -> Tydec_array (Dimension.expr_replace_expr rename d, rename_static rename cty')
701
 | Tydec_clock cty       -> Tydec_clock (rename_static rename cty)
702
 | Tydec_struct fl       -> Tydec_struct (List.map (fun (f, cty) -> f, rename_static rename cty) fl)
703
 | _                      -> cty
704
705
let rec rename_carrier rename cck =
706
 match cck with
707
 | Ckdec_bool cl -> Ckdec_bool (List.map (fun (c, l) -> rename c, l) cl)
708
 | _             -> cck
709
710 333e3a25 ploc
 (*Format.eprintf "Types.rename_static %a = %a@." print_ty ty print_ty res; res*)
711 ec433d69 xthirioux
712
(* applies the renaming function [fvar] to all variables of expression [expr] *)
713 333e3a25 ploc
 (* let rec expr_replace_var fvar expr = *)
714
 (*  { expr with expr_desc = expr_desc_replace_var fvar expr.expr_desc } *)
715 ec433d69 xthirioux
716 333e3a25 ploc
 (* and expr_desc_replace_var fvar expr_desc = *)
717
 (*   match expr_desc with *)
718
 (*   | Expr_const _ -> expr_desc *)
719
 (*   | Expr_ident i -> Expr_ident (fvar i) *)
720
 (*   | Expr_array el -> Expr_array (List.map (expr_replace_var fvar) el) *)
721
 (*   | Expr_access (e1, d) -> Expr_access (expr_replace_var fvar e1, d) *)
722
 (*   | Expr_power (e1, d) -> Expr_power (expr_replace_var fvar e1, d) *)
723
 (*   | Expr_tuple el -> Expr_tuple (List.map (expr_replace_var fvar) el) *)
724
 (*   | Expr_ite (c, t, e) -> Expr_ite (expr_replace_var fvar c, expr_replace_var fvar t, expr_replace_var fvar e) *)
725
 (*   | Expr_arrow (e1, e2)-> Expr_arrow (expr_replace_var fvar e1, expr_replace_var fvar e2)  *)
726
 (*   | Expr_fby (e1, e2) -> Expr_fby (expr_replace_var fvar e1, expr_replace_var fvar e2) *)
727
 (*   | Expr_pre e' -> Expr_pre (expr_replace_var fvar e') *)
728
 (*   | Expr_when (e', i, l)-> Expr_when (expr_replace_var fvar e', fvar i, l) *)
729
 (*   | Expr_merge (i, hl) -> Expr_merge (fvar i, List.map (fun (t, h) -> (t, expr_replace_var fvar h)) hl) *)
730
 (*   | Expr_appl (i, e', i') -> Expr_appl (i, expr_replace_var fvar e', Utils.option_map (expr_replace_var fvar) i') *)
731
732
733
734
 let rec rename_expr  f_node f_var expr =
735
   { expr with expr_desc = rename_expr_desc f_node f_var expr.expr_desc }
736
 and rename_expr_desc f_node f_var expr_desc =
737
   let re = rename_expr  f_node f_var in
738 ec433d69 xthirioux
   match expr_desc with
739
   | Expr_const _ -> expr_desc
740
   | Expr_ident i -> Expr_ident (f_var i)
741
   | Expr_array el -> Expr_array (List.map re el)
742
   | Expr_access (e1, d) -> Expr_access (re e1, d)
743
   | Expr_power (e1, d) -> Expr_power (re e1, d)
744
   | Expr_tuple el -> Expr_tuple (List.map re el)
745
   | Expr_ite (c, t, e) -> Expr_ite (re c, re t, re e)
746
   | Expr_arrow (e1, e2)-> Expr_arrow (re e1, re e2) 
747
   | Expr_fby (e1, e2) -> Expr_fby (re e1, re e2)
748
   | Expr_pre e' -> Expr_pre (re e')
749
   | Expr_when (e', i, l)-> Expr_when (re e', f_var i, l)
750
   | Expr_merge (i, hl) -> 
751
     Expr_merge (f_var i, List.map (fun (t, h) -> (t, re h)) hl)
752
   | Expr_appl (i, e', i') -> 
753
     Expr_appl (f_node i, re e', Utils.option_map re i')
754 333e3a25 ploc
755
 let rename_dec_type f_node f_var t = assert false (*
756
						     Types.rename_dim_type (Dimension.rename f_node f_var) t*)
757
758
 let rename_dec_clock f_node f_var c = assert false (* 
759
					  Clocks.rename_clock_expr f_var c*)
760
   
761
 let rename_var f_node f_var v = {
762
   v with
763
     var_id = f_var v.var_id;
764
     var_dec_type = rename_dec_type f_node f_var v.var_type;
765
     var_dec_clock = rename_dec_clock f_node f_var v.var_clock
766
 } 
767
768
 let rename_vars f_node f_var = List.map (rename_var f_node f_var) 
769
770
 let rec rename_eq f_node f_var eq = { eq with
771
   eq_lhs = List.map f_var eq.eq_lhs; 
772
   eq_rhs = rename_expr f_node f_var eq.eq_rhs
773
 } 
774
 and rename_handler f_node f_var  h = {h with
775
   hand_state = f_var h.hand_state;
776
   hand_unless = List.map (
777
     fun (l,e,b,id) -> l, rename_expr f_node f_var e, b, f_var id
778
   ) h.hand_unless;
779
   hand_until = List.map (
780
     fun (l,e,b,id) -> l, rename_expr f_node f_var e, b, f_var id
781
   ) h.hand_until;
782
   hand_locals = rename_vars f_node f_var h.hand_locals;
783
   hand_stmts = rename_stmts f_node f_var h.hand_stmts;
784
   hand_annots = rename_annots f_node f_var h.hand_annots;
785
   
786
 } 
787
 and rename_aut f_node f_var  aut = { aut with
788
   aut_id = f_var aut.aut_id;
789
   aut_handlers = List.map (rename_handler f_node f_var) aut.aut_handlers;
790
 }
791
 and rename_stmts f_node f_var stmts = List.map (fun stmt -> match stmt with
792
   | Eq eq -> Eq (rename_eq f_node f_var eq)
793
   | Aut at -> Aut (rename_aut f_node f_var at))
794
   stmts
795
 and rename_annotl f_node f_var  annots = 
796
   List.map 
797
     (fun (key, value) -> key, rename_eexpr f_node f_var value) 
798
     annots
799
 and rename_annot f_node f_var annot =
800
   { annot with annots = rename_annotl f_node f_var annot.annots }
801
 and rename_annots f_node f_var annots =
802
   List.map (rename_annot f_node f_var) annots
803
and rename_eexpr f_node f_var ee =
804
   { ee with
805
     eexpr_tag = Utils.new_tag ();
806
     eexpr_qfexpr = rename_expr f_node f_var ee.eexpr_qfexpr;
807
     eexpr_quantifiers = List.map (fun (typ,vdecls) -> typ, rename_vars f_node f_var vdecls) ee.eexpr_quantifiers;
808
     eexpr_normalized = Utils.option_map 
809
       (fun (vdecl, eqs, vdecls) ->
810
	 rename_var f_node f_var vdecl,
811
	 List.map (rename_eq f_node f_var) eqs,
812
	 rename_vars f_node f_var vdecls
813
       ) ee.eexpr_normalized;
814
     
815
   }
816
 
817
     
818
     
819
   
820
 let rename_node f_node f_var nd =
821
   let rename_var = rename_var f_node f_var in
822
   let rename_expr = rename_expr f_node f_var in
823
   let rename_stmts = rename_stmts f_node f_var in
824
   let inputs = List.map rename_var nd.node_inputs in
825
   let outputs = List.map rename_var nd.node_outputs in
826
   let locals = List.map rename_var nd.node_locals in
827
   let gen_calls = List.map rename_expr nd.node_gencalls in
828
   let node_checks = List.map (Dimension.rename f_node f_var)  nd.node_checks in
829
   let node_asserts = List.map 
830
     (fun a -> 
831
       {a with assert_expr = 
832
	   let expr = a.assert_expr in
833
	   rename_expr expr})
834
     nd.node_asserts
835
   in
836
   let node_stmts = rename_stmts nd.node_stmts
837
838
     
839
   in
840
   let spec = 
841
     Utils.option_map 
842
       (fun s -> assert false; (*rename_node_annot f_node f_var s*) ) (* TODO: implement! *) 
843
       nd.node_spec 
844
   in
845
   let annot = rename_annots f_node f_var nd.node_annot in
846
   {
847
     node_id = f_node nd.node_id;
848
     node_type = nd.node_type;
849
     node_clock = nd.node_clock;
850
     node_inputs = inputs;
851
     node_outputs = outputs;
852
     node_locals = locals;
853
     node_gencalls = gen_calls;
854
     node_checks = node_checks;
855
     node_asserts = node_asserts;
856
     node_stmts = node_stmts;
857
     node_dec_stateless = nd.node_dec_stateless;
858
     node_stateless = nd.node_stateless;
859
     node_spec = spec;
860
     node_annot = annot;
861
   }
862 ec433d69 xthirioux
863
864
let rename_const f_const c =
865
  { c with const_id = f_const c.const_id }
866
867
let rename_typedef f_var t =
868
  match t.tydef_desc with
869
  | Tydec_enum tags -> { t with tydef_desc = Tydec_enum (List.map f_var tags) }
870
  | _               -> t
871
872
let rename_prog f_node f_var f_const prog =
873
  List.rev (
874
    List.fold_left (fun accu top ->
875
      (match top.top_decl_desc with
876
      | Node nd -> 
877 333e3a25 ploc
	 { top with top_decl_desc = Node (rename_node f_node f_var nd) }
878 ec433d69 xthirioux
      | Const c -> 
879
	 { top with top_decl_desc = Const (rename_const f_const c) }
880
      | TypeDef tdef ->
881
	 { top with top_decl_desc = TypeDef (rename_typedef f_var tdef) }
882
      | ImportedNode _
883 19a1e66b ploc
        | Include _ | Open _       -> top)
884 ec433d69 xthirioux
      ::accu
885
) [] prog
886
		   )
887
888 333e3a25 ploc
(* Applies the renaming function [fvar] to every rhs
889
   only when the corresponding lhs satisfies predicate [pvar] *)
890
 let eq_replace_rhs_var pvar fvar eq =
891
   let pvar l = List.exists pvar l in
892
   let rec replace lhs rhs =
893
     { rhs with expr_desc =
894
     match lhs with
895
     | []  -> assert false
896
     | [_] -> if pvar lhs then rename_expr_desc (fun x -> x) fvar rhs.expr_desc else rhs.expr_desc
897
     | _   ->
898
       (match rhs.expr_desc with
899
       | Expr_tuple tl ->
900
	 Expr_tuple (List.map2 (fun v e -> replace [v] e) lhs tl)
901
       | Expr_appl (f, arg, None) when Basic_library.is_expr_internal_fun rhs ->
902
	 let args = expr_list_of_expr arg in
903
	 Expr_appl (f, expr_of_expr_list arg.expr_loc (List.map (replace lhs) args), None)
904
       | Expr_array _
905
       | Expr_access _
906
       | Expr_power _
907
       | Expr_const _
908
       | Expr_ident _
909
       | Expr_appl _   ->
910
	 if pvar lhs
911
	 then rename_expr_desc (fun x -> x) fvar rhs.expr_desc
912
	 else rhs.expr_desc
913
       | Expr_ite (c, t, e)   -> Expr_ite (replace lhs c, replace lhs t, replace lhs e)
914
       | Expr_arrow (e1, e2)  -> Expr_arrow (replace lhs e1, replace lhs e2) 
915
       | Expr_fby (e1, e2)    -> Expr_fby (replace lhs e1, replace lhs e2)
916
       | Expr_pre e'          -> Expr_pre (replace lhs e')
917
       | Expr_when (e', i, l) -> let i' = if pvar lhs then fvar i else i
918
				 in Expr_when (replace lhs e', i', l)
919
       | Expr_merge (i, hl)   -> let i' = if pvar lhs then fvar i else i
920
				 in Expr_merge (i', List.map (fun (t, h) -> (t, replace lhs h)) hl)
921
       )
922
     }
923
   in { eq with eq_rhs = replace eq.eq_lhs eq.eq_rhs }
924
925
    
926 ec433d69 xthirioux
(**********************************************************************)
927
(* Pretty printers *)
928
929
let pp_decl_type fmt tdecl =
930
  match tdecl.top_decl_desc with
931
  | Node nd ->
932
    fprintf fmt "%s: " nd.node_id;
933
    Utils.reset_names ();
934
    fprintf fmt "%a@ " Types.print_ty nd.node_type
935
  | ImportedNode ind ->
936
    fprintf fmt "%s: " ind.nodei_id;
937
    Utils.reset_names ();
938
    fprintf fmt "%a@ " Types.print_ty ind.nodei_type
939 19a1e66b ploc
  | Const _ | Include _ | Open _ | TypeDef _ -> ()
940 ec433d69 xthirioux
941
let pp_prog_type fmt tdecl_list =
942
  Utils.fprintf_list ~sep:"" pp_decl_type fmt tdecl_list
943
944
let pp_decl_clock fmt cdecl =
945
  match cdecl.top_decl_desc with
946
  | Node nd ->
947
    fprintf fmt "%s: " nd.node_id;
948
    Utils.reset_names ();
949
    fprintf fmt "%a@ " Clocks.print_ck nd.node_clock
950
  | ImportedNode ind ->
951
    fprintf fmt "%s: " ind.nodei_id;
952
    Utils.reset_names ();
953
    fprintf fmt "%a@ " Clocks.print_ck ind.nodei_clock
954 19a1e66b ploc
  | Const _ | Include _ | Open _ | TypeDef _ -> ()
955 ec433d69 xthirioux
956
let pp_prog_clock fmt prog =
957
  Utils.fprintf_list ~sep:"" pp_decl_clock fmt prog
958
959
960
(* filling node table with internal functions *)
961
let vdecls_of_typ_ck cpt ty =
962
  let loc = Location.dummy_loc in
963
  List.map
964
    (fun _ -> incr cpt;
965
              let name = sprintf "_var_%d" !cpt in
966 66359a5e ploc
              mkvar_decl loc (name, mktyp loc Tydec_any, mkclock loc Ckdec_any, false, None, None))
967 ec433d69 xthirioux
    (Types.type_list_of_type ty)
968
969
let mk_internal_node id =
970
  let spec = None in
971
  let ty = Env.lookup_value Basic_library.type_env id in
972
  let ck = Env.lookup_value Basic_library.clock_env id in
973
  let (tin, tout) = Types.split_arrow ty in
974
  (*eprintf "internal fun %s: %d -> %d@." id (List.length (Types.type_list_of_type tin)) (List.length (Types.type_list_of_type tout));*)
975
  let cpt = ref (-1) in
976
  mktop
977
    (ImportedNode
978
       {nodei_id = id;
979
	nodei_type = ty;
980
	nodei_clock = ck;
981
	nodei_inputs = vdecls_of_typ_ck cpt tin;
982
	nodei_outputs = vdecls_of_typ_ck cpt tout;
983
	nodei_stateless = Types.get_static_value ty <> None;
984
	nodei_spec = spec;
985 66359a5e ploc
	(* nodei_annot = []; *)
986 ec433d69 xthirioux
	nodei_prototype = None;
987 04a63d25 xthirioux
       	nodei_in_lib = [];
988 ec433d69 xthirioux
       })
989
990
let add_internal_funs () =
991
  List.iter
992
    (fun id -> let nd = mk_internal_node id in Hashtbl.add node_table id nd)
993
    Basic_library.internal_funs
994
995
996
997
(* Replace any occurence of a var in vars_to_replace by its associated
998
   expression in defs until e does not contain any such variables *)
999
let rec substitute_expr vars_to_replace defs e =
1000
  let se = substitute_expr vars_to_replace defs in
1001
  { e with expr_desc = 
1002
      let ed = e.expr_desc in
1003
      match ed with
1004
      | Expr_const _ -> ed
1005
      | Expr_array el -> Expr_array (List.map se el)
1006
      | Expr_access (e1, d) -> Expr_access (se e1, d)
1007
      | Expr_power (e1, d) -> Expr_power (se e1, d)
1008
      | Expr_tuple el -> Expr_tuple (List.map se el)
1009
      | Expr_ite (c, t, e) -> Expr_ite (se c, se t, se e)
1010
      | Expr_arrow (e1, e2)-> Expr_arrow (se e1, se e2) 
1011
      | Expr_fby (e1, e2) -> Expr_fby (se e1, se e2)
1012
      | Expr_pre e' -> Expr_pre (se e')
1013
      | Expr_when (e', i, l)-> Expr_when (se e', i, l)
1014
      | Expr_merge (i, hl) -> Expr_merge (i, List.map (fun (t, h) -> (t, se h)) hl)
1015
      | Expr_appl (i, e', i') -> Expr_appl (i, se e', i')
1016
      | Expr_ident i -> 
1017
	if List.exists (fun v -> v.var_id = i) vars_to_replace then (
1018
	  let eq_i eq = eq.eq_lhs = [i] in
1019
	  if List.exists eq_i defs then
1020
	    let sub = List.find eq_i defs in
1021
	    let sub' = se sub.eq_rhs in
1022
	    sub'.expr_desc
1023
	  else 
1024
	    assert false
1025
	)
1026
	else
1027
	  ed
1028
1029
  }
1030
  
1031
 let rec expr_to_eexpr  expr =
1032
   { eexpr_tag = expr.expr_tag;
1033 3e1d20e0 ploc
     eexpr_qfexpr = expr;
1034
     eexpr_quantifiers = [];
1035 ec433d69 xthirioux
     eexpr_type = expr.expr_type;
1036
     eexpr_clock = expr.expr_clock;
1037 3e1d20e0 ploc
     eexpr_loc = expr.expr_loc;
1038
     eexpr_normalized = None
1039 ec433d69 xthirioux
   }
1040 3e1d20e0 ploc
 (* and expr_desc_to_eexpr_desc expr_desc = *)
1041
 (*   let conv = expr_to_eexpr in *)
1042
 (*   match expr_desc with *)
1043
 (*   | Expr_const c -> EExpr_const (match c with *)
1044
 (*     | Const_int x -> EConst_int x  *)
1045
 (*     | Const_real x -> EConst_real x  *)
1046
 (*     | Const_float x -> EConst_float x  *)
1047
 (*     | Const_tag x -> EConst_tag x  *)
1048
 (*     | _ -> assert false *)
1049
1050
 (*   ) *)
1051
 (*   | Expr_ident i -> EExpr_ident i *)
1052
 (*   | Expr_tuple el -> EExpr_tuple (List.map conv el) *)
1053
1054
 (*   | Expr_arrow (e1, e2)-> EExpr_arrow (conv e1, conv e2)  *)
1055
 (*   | Expr_fby (e1, e2) -> EExpr_fby (conv e1, conv e2) *)
1056
 (*   | Expr_pre e' -> EExpr_pre (conv e') *)
1057
 (*   | Expr_appl (i, e', i') ->  *)
1058
 (*     EExpr_appl  *)
1059
 (*       (i, conv e', match i' with None -> None | Some(id, _) -> Some id) *)
1060
1061
 (*   | Expr_when _ *)
1062
 (*   | Expr_merge _ -> assert false *)
1063
 (*   | Expr_array _  *)
1064
 (*   | Expr_access _  *)
1065
 (*   | Expr_power _  -> assert false *)
1066
 (*   | Expr_ite (c, t, e) -> assert false  *)
1067
 (*   | _ -> assert false *)
1068
      
1069
     
1070 ec433d69 xthirioux
let rec get_expr_calls nodes e =
1071
  let get_calls = get_expr_calls nodes in
1072 04a63d25 xthirioux
  match e.expr_desc with
1073 ec433d69 xthirioux
  | Expr_const _ 
1074
   | Expr_ident _ -> Utils.ISet.empty
1075
   | Expr_tuple el
1076
   | Expr_array el -> List.fold_left (fun accu e -> Utils.ISet.union accu (get_calls e)) Utils.ISet.empty el
1077
   | Expr_pre e1 
1078
   | Expr_when (e1, _, _) 
1079
   | Expr_access (e1, _) 
1080
   | Expr_power (e1, _) -> get_calls e1
1081
   | Expr_ite (c, t, e) -> Utils.ISet.union (Utils.ISet.union (get_calls c) (get_calls t)) (get_calls e) 
1082
   | Expr_arrow (e1, e2) 
1083
   | Expr_fby (e1, e2) -> Utils.ISet.union (get_calls e1) (get_calls e2)
1084
   | Expr_merge (_, hl) -> List.fold_left (fun accu (_, h) -> Utils.ISet.union accu (get_calls h)) Utils.ISet.empty  hl
1085
   | Expr_appl (i, e', i') -> 
1086 04a63d25 xthirioux
     if Basic_library.is_expr_internal_fun e then 
1087 ec433d69 xthirioux
       (get_calls e') 
1088
     else
1089
       let calls =  Utils.ISet.add i (get_calls e') in
1090
       let test = (fun n -> match n.top_decl_desc with Node nd -> nd.node_id = i | _ -> false) in
1091
       if List.exists test nodes then
1092
	 match (List.find test nodes).top_decl_desc with
1093
	 | Node nd -> Utils.ISet.union (get_node_calls nodes nd) calls
1094
	 | _ -> assert false
1095
       else 
1096
	 calls
1097
1098
and get_eq_calls nodes eq =
1099
  get_expr_calls nodes eq.eq_rhs
1100 333e3a25 ploc
and get_aut_handler_calls nodes h =
1101
  List.fold_left (fun accu stmt -> match stmt with
1102
  | Eq eq -> Utils.ISet.union (get_eq_calls nodes eq) accu
1103
  | Aut aut' ->  Utils.ISet.union (get_aut_calls nodes aut') accu
1104
  ) Utils.ISet.empty h.hand_stmts 
1105
and get_aut_calls nodes aut =
1106
  List.fold_left (fun accu h -> Utils.ISet.union (get_aut_handler_calls nodes h) accu)
1107
    Utils.ISet.empty aut.aut_handlers
1108 ec433d69 xthirioux
and get_node_calls nodes node =
1109 333e3a25 ploc
  let eqs, auts = get_node_eqs node in
1110
  let aut_calls =
1111
    List.fold_left
1112
      (fun accu aut -> Utils.ISet.union (get_aut_calls nodes aut) accu)
1113
      Utils.ISet.empty auts
1114
  in
1115
  List.fold_left
1116
    (fun accu eq -> Utils.ISet.union (get_eq_calls nodes eq) accu)
1117
    aut_calls eqs
1118 ec433d69 xthirioux
1119 a6df3992 Ploc
let get_expr_vars e =
1120
  let rec get_expr_vars vars e =
1121
    get_expr_desc_vars vars e.expr_desc
1122
  and get_expr_desc_vars vars expr_desc =
1123 a85ca7df ploc
    (*Format.eprintf "get_expr_desc_vars expr=%a@." Printers.pp_expr (mkexpr Location.dummy_loc expr_desc);*)
1124 ec433d69 xthirioux
  match expr_desc with
1125
  | Expr_const _ -> vars
1126 a85ca7df ploc
  | Expr_ident x -> Utils.ISet.add x vars
1127 ec433d69 xthirioux
  | Expr_tuple el
1128
  | Expr_array el -> List.fold_left get_expr_vars vars el
1129
  | Expr_pre e1 -> get_expr_vars vars e1
1130
  | Expr_when (e1, c, _) -> get_expr_vars (Utils.ISet.add c vars) e1 
1131
  | Expr_access (e1, d) 
1132
  | Expr_power (e1, d)   -> List.fold_left get_expr_vars vars [e1; expr_of_dimension d]
1133
  | Expr_ite (c, t, e) -> List.fold_left get_expr_vars vars [c; t; e]
1134
  | Expr_arrow (e1, e2) 
1135
  | Expr_fby (e1, e2) -> List.fold_left get_expr_vars vars [e1; e2]
1136
  | Expr_merge (c, hl) -> List.fold_left (fun vars (_, h) -> get_expr_vars vars h) (Utils.ISet.add c vars) hl
1137
  | Expr_appl (_, arg, None)   -> get_expr_vars vars arg
1138
  | Expr_appl (_, arg, Some r) -> List.fold_left get_expr_vars vars [arg; r]
1139 a6df3992 Ploc
  in
1140
  get_expr_vars Utils.ISet.empty e 
1141 ec433d69 xthirioux
1142
let rec expr_has_arrows e =
1143
  expr_desc_has_arrows e.expr_desc
1144
and expr_desc_has_arrows expr_desc =
1145
  match expr_desc with
1146
  | Expr_const _ 
1147
  | Expr_ident _ -> false
1148
  | Expr_tuple el
1149
  | Expr_array el -> List.exists expr_has_arrows el
1150
  | Expr_pre e1 
1151
  | Expr_when (e1, _, _) 
1152
  | Expr_access (e1, _) 
1153
  | Expr_power (e1, _) -> expr_has_arrows e1
1154
  | Expr_ite (c, t, e) -> List.exists expr_has_arrows [c; t; e]
1155
  | Expr_arrow (e1, e2) 
1156
  | Expr_fby (e1, e2) -> true
1157
  | Expr_merge (_, hl) -> List.exists (fun (_, h) -> expr_has_arrows h) hl
1158
  | Expr_appl (i, e', i') -> expr_has_arrows e'
1159
1160
and eq_has_arrows eq =
1161
  expr_has_arrows eq.eq_rhs
1162 333e3a25 ploc
and aut_has_arrows aut = List.exists (fun h -> List.exists (fun stmt -> match stmt with Eq eq -> eq_has_arrows eq | Aut aut' -> aut_has_arrows aut') h.hand_stmts ) aut.aut_handlers 
1163 ec433d69 xthirioux
and node_has_arrows node =
1164 333e3a25 ploc
  let eqs, auts = get_node_eqs node in
1165
  List.exists (fun eq -> eq_has_arrows eq) eqs || List.exists (fun aut -> aut_has_arrows aut) auts
1166
1167 ec433d69 xthirioux
1168 01c7d5e1 ploc
1169 ec433d69 xthirioux
let copy_var_decl vdecl =
1170 66359a5e ploc
  mkvar_decl vdecl.var_loc ~orig:vdecl.var_orig (vdecl.var_id, vdecl.var_dec_type, vdecl.var_dec_clock, vdecl.var_dec_const, vdecl.var_dec_value, vdecl.var_parent_nodeid)
1171 ec433d69 xthirioux
1172
let copy_const cdecl =
1173
  { cdecl with const_type = Types.new_var () }
1174
1175
let copy_node nd =
1176
  { nd with
1177
    node_type     = Types.new_var ();
1178
    node_clock    = Clocks.new_var true;
1179
    node_inputs   = List.map copy_var_decl nd.node_inputs;
1180
    node_outputs  = List.map copy_var_decl nd.node_outputs;
1181
    node_locals   = List.map copy_var_decl nd.node_locals;
1182
    node_gencalls = [];
1183
    node_checks   = [];
1184
    node_stateless = None;
1185
  }
1186
1187
let copy_top top =
1188
  match top.top_decl_desc with
1189
  | Node nd -> { top with top_decl_desc = Node (copy_node nd)  }
1190
  | Const c -> { top with top_decl_desc = Const (copy_const c) }
1191
  | _       -> top
1192
1193
let copy_prog top_list =
1194
  List.map copy_top top_list
1195
1196 e7cc5186 ploc
1197
let rec expr_contains_expr expr_tag expr  =
1198
  let search = expr_contains_expr expr_tag in
1199
  expr.expr_tag = expr_tag ||
1200
      (
1201
	match expr.expr_desc with
1202
	| Expr_const _ -> false
1203
	| Expr_array el -> List.exists search el
1204
	| Expr_access (e1, _) 
1205
	| Expr_power (e1, _) -> search e1
1206
	| Expr_tuple el -> List.exists search el
1207
	| Expr_ite (c, t, e) -> List.exists search [c;t;e]
1208
	| Expr_arrow (e1, e2)
1209
	| Expr_fby (e1, e2) -> List.exists search [e1; e2]
1210
	| Expr_pre e' 
1211
	| Expr_when (e', _, _) -> search e'
1212
	| Expr_merge (_, hl) -> List.exists (fun (_, h) -> search h) hl
1213
	| Expr_appl (_, e', None) -> search e' 
1214
	| Expr_appl (_, e', Some e'') -> List.exists search [e'; e''] 
1215
	| Expr_ident _ -> false
1216
      )
1217
1218 2863281f ploc
1219
1220 7065d912 ploc
(* Generate a new local [node] variable *)
1221
let cpt_fresh = ref 0
1222
1223
let reset_cpt_fresh () =
1224
    cpt_fresh := 0
1225
    
1226 95944ba1 ploc
let mk_fresh_var (parentid, ctx_env) loc ty ck =
1227 7065d912 ploc
  let rec aux () =
1228
  incr cpt_fresh;
1229 95944ba1 ploc
  let s = Printf.sprintf "__%s_%d" parentid !cpt_fresh in
1230
  if List.exists (fun v -> v.var_id = s) ctx_env then aux () else
1231 7065d912 ploc
  {
1232
    var_id = s;
1233
    var_orig = false;
1234
    var_dec_type = dummy_type_dec;
1235
    var_dec_clock = dummy_clock_dec;
1236
    var_dec_const = false;
1237
    var_dec_value = None;
1238 95944ba1 ploc
    var_parent_nodeid = Some parentid;
1239 7065d912 ploc
    var_type = ty;
1240
    var_clock = ck;
1241
    var_loc = loc
1242
  }
1243
  in aux ()
1244 2863281f ploc
1245 eb9a8c3c ploc
1246
let find_eq xl eqs =
1247
  let rec aux accu eqs =
1248
    match eqs with
1249
	| [] ->
1250
	  begin
1251
	    Format.eprintf "Looking for variables %a in the following equations@.%a@."
1252
	      (Utils.fprintf_list ~sep:" , " (fun fmt v -> Format.fprintf fmt "%s" v)) xl
1253
	      Printers.pp_node_eqs eqs;
1254
	    assert false
1255
	  end
1256
	| hd::tl ->
1257
	  if List.exists (fun x -> List.mem x hd.eq_lhs) xl then hd, accu@tl else aux (hd::accu) tl
1258
    in
1259
    aux [] eqs
1260
1261 22fe1c93 ploc
(* Local Variables: *)
1262
(* compile-command:"make -C .." *)
1263
(* End: *)