Project

General

Profile

Statistics
| Branch: | Tag: | Revision:

lustrec / src / clocks.ml @ e6b644f4

History | View | Annotate | Download (14 KB)

1 a2d97a3e ploc
(********************************************************************)
2
(*                                                                  *)
3
(*  The LustreC compiler toolset   /  The LustreC Development Team  *)
4
(*  Copyright 2012 -    --   ONERA - CNRS - INPT - LIFL             *)
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
(*  This file was originally from the Prelude compiler              *)
11
(*                                                                  *) 
12
(********************************************************************)
13 22fe1c93 ploc
14
(** Clocks definitions and a few utility functions on clocks. *)
15
open Utils
16
open Format
17
18 45f0f48d xthirioux
(* (\* Clock type sets, for subtyping. *\) *)
19
(* type clock_set = *)
20
(*     CSet_all *)
21
(*   | CSet_pck of int*rat *)
22 22fe1c93 ploc
23
(* Clock carriers basically correspond to the "c" in "x when c" *)
24
type carrier_desc =
25
  | Carry_const of ident
26
  | Carry_name
27
  | Carry_var
28
  | Carry_link of carrier_expr
29
30
(* Carriers are scoped, to detect clock extrusion. In other words, we
31
   check the scope of a clock carrier before generalizing it. *)
32
and carrier_expr =
33
    {mutable carrier_desc: carrier_desc;
34
     mutable carrier_scoped: bool;
35
     carrier_id: int}
36 333e3a25 ploc
     
37 22fe1c93 ploc
type clock_expr =
38
    {mutable cdesc: clock_desc;
39
     mutable cscoped: bool;
40
     cid: int}
41
42
(* pck stands for periodic clock. Easier not to separate pck from other clocks *)
43
and clock_desc =
44
  | Carrow of clock_expr * clock_expr
45
  | Ctuple of clock_expr list
46
  | Con of clock_expr * carrier_expr * ident
47 45f0f48d xthirioux
  (* | Pck_up of clock_expr * int *)
48
  (* | Pck_down of clock_expr * int *)
49
  (* | Pck_phase of clock_expr * rat *)
50
  (* | Pck_const of int * rat *)
51
  | Cvar (* of clock_set *) (* Monomorphic clock variable *)
52
  | Cunivar (* of clock_set *) (* Polymorphic clock variable *)
53 22fe1c93 ploc
  | Clink of clock_expr (* During unification, make links instead of substitutions *)
54
  | Ccarrying of carrier_expr * clock_expr
55
56
type error =
57
  | Clock_clash of clock_expr * clock_expr
58 45f0f48d xthirioux
  (* | Not_pck *)
59
  (* | Clock_set_mismatch of clock_expr * clock_set *)
60 22fe1c93 ploc
  | Cannot_be_polymorphic of clock_expr
61
  | Invalid_imported_clock of clock_expr
62
  | Invalid_const of clock_expr
63
  | Factor_zero
64
  | Carrier_mismatch of carrier_expr * carrier_expr
65
  | Carrier_extrusion of clock_expr * carrier_expr
66
  | Clock_extrusion of clock_expr * clock_expr
67
68
exception Unify of clock_expr * clock_expr
69
exception Mismatch of carrier_expr * carrier_expr
70
exception Scope_carrier of carrier_expr
71
exception Scope_clock of clock_expr
72
exception Error of Location.t * error
73
74 fc886259 xthirioux
let rec print_carrier fmt cr =
75
 (* (if cr.carrier_scoped then fprintf fmt "[%t]" else fprintf fmt "%t") (fun fmt -> *)
76
  match cr.carrier_desc with
77
  | Carry_const id -> fprintf fmt "%s" id
78
  | Carry_name ->
79
      fprintf fmt "_%s" (name_of_carrier cr.carrier_id)
80
  | Carry_var ->
81
    fprintf fmt "'%s" (name_of_carrier cr.carrier_id)
82
  | Carry_link cr' ->
83
    print_carrier fmt cr'
84
85
(* Simple pretty-printing, performs no simplifications. Linear
86
   complexity. For debug mainly. *)
87
let rec print_ck_long fmt ck =
88
  match ck.cdesc with
89
  | Carrow (ck1,ck2) ->
90 e39f5319 xthirioux
      fprintf fmt "%a -> %a" print_ck_long ck1 print_ck_long ck2
91 fc886259 xthirioux
  | Ctuple cklist ->
92
    fprintf fmt "(%a)"
93
      (fprintf_list ~sep:" * " print_ck_long) cklist
94
  | Con (ck,c,l) ->
95
    fprintf fmt "%a on %s(%a)" print_ck_long ck l print_carrier c
96 45f0f48d xthirioux
  | Cvar -> fprintf fmt "'_%i" ck.cid 
97
  | Cunivar -> fprintf fmt "'%i" ck.cid 
98 fc886259 xthirioux
  | Clink ck' ->
99
    fprintf fmt "link %a" print_ck_long ck'
100
  | Ccarrying (cr,ck') ->
101
    fprintf fmt "(%a:%a)" print_carrier cr print_ck_long ck'
102
103 22fe1c93 ploc
let new_id = ref (-1)
104
105 04a63d25 xthirioux
let rec bottom =
106
  { cdesc = Clink bottom; cid = -666; cscoped = false }
107
108 22fe1c93 ploc
let new_ck desc scoped =
109
  incr new_id; {cdesc=desc; cid = !new_id; cscoped = scoped}
110
111 45f0f48d xthirioux
let new_var scoped = new_ck Cvar scoped
112 22fe1c93 ploc
113 45f0f48d xthirioux
let new_univar () = new_ck Cunivar false
114 22fe1c93 ploc
115
let new_carrier_id = ref (-1)
116
117
let new_carrier desc scoped =
118
  incr new_carrier_id; {carrier_desc = desc;
119
                        carrier_id = !new_carrier_id;
120
                        carrier_scoped = scoped}
121
122
let new_carrier_name () =
123
  new_carrier Carry_name true
124
125
let rec repr =
126
  function
127
      {cdesc=Clink ck'} ->
128
        repr ck'
129
    | ck -> ck
130
131
let rec carrier_repr =
132
  function {carrier_desc = Carry_link cr'} -> carrier_repr cr'
133
    | cr -> cr
134
135
136 8f1c7e91 xthirioux
let get_carrier_name ck =
137
 match (repr ck).cdesc with
138
 | Ccarrying (cr, _) -> Some cr
139
 | _                 -> None
140
141 fc886259 xthirioux
let rename_carrier_static rename cr =
142
  match (carrier_repr cr).carrier_desc with
143
  | Carry_const id -> { cr with carrier_desc = Carry_const (rename id) }
144
  | _              -> (Format.eprintf "internal error: Clocks.rename_carrier_static %a@." print_carrier cr; assert false)
145
146
let rec rename_static rename ck =
147
 match (repr ck).cdesc with
148
 | Ccarrying (cr, ck') -> { ck with cdesc = Ccarrying (rename_carrier_static rename cr, rename_static rename ck') }
149
 | Con (ck', cr, l)    -> { ck with cdesc = Con (rename_static rename ck', rename_carrier_static rename cr, l) }
150
 | _                   -> ck
151
152 22fe1c93 ploc
let uncarrier ck =
153
 match ck.cdesc with
154 8f1c7e91 xthirioux
 | Ccarrying (_, ck') -> ck'
155
 | _                  -> ck
156 22fe1c93 ploc
157
(* Removes all links in a clock. Only used for clocks
158
   simplification though. *)
159 45f0f48d xthirioux
let rec simplify ck =
160 22fe1c93 ploc
  match ck.cdesc with
161
  | Carrow (ck1,ck2) ->
162 45f0f48d xthirioux
      new_ck (Carrow (simplify ck1, simplify ck2)) ck.cscoped
163 22fe1c93 ploc
  | Ctuple cl ->
164 45f0f48d xthirioux
      new_ck (Ctuple (List.map simplify cl)) ck.cscoped
165 22fe1c93 ploc
  | Con (ck', c, l) ->
166 45f0f48d xthirioux
      new_ck (Con (simplify ck', c, l)) ck.cscoped
167
  | Cvar | Cunivar -> ck
168
  | Clink ck' -> simplify ck'
169
  | Ccarrying (cr,ck') -> new_ck (Ccarrying (cr, simplify ck')) ck.cscoped
170 22fe1c93 ploc
171
(** Splits ck into the [lhs,rhs] of an arrow clock. Expects an arrow clock
172
    (ensured by language syntax) *)
173
let split_arrow ck =
174
  match (repr ck).cdesc with
175
  | Carrow (cin,cout) -> cin,cout
176
  | _ -> failwith "Internal error: not an arrow clock"
177
178
(** Returns the clock corresponding to a clock list. *)
179
let clock_of_clock_list ckl =
180
  if (List.length ckl) > 1 then
181
    new_ck (Ctuple ckl) true
182
  else
183
    List.hd ckl
184
185
let clock_list_of_clock ck =
186
 match (repr ck).cdesc with
187
 | Ctuple cl -> cl
188
 | _         -> [ck]
189
190 6fdfb60b xthirioux
let clock_on ck cr l =
191
 clock_of_clock_list (List.map (fun ck -> new_ck (Con (ck,cr,l)) true) (clock_list_of_clock ck))
192
193 54d032f5 xthirioux
let clock_current ck =
194
 clock_of_clock_list (List.map (fun ck -> match (repr ck).cdesc with Con(ck',_,_) -> ck' | _ -> assert false) (clock_list_of_clock ck))
195
196 22fe1c93 ploc
let clock_of_impnode_clock ck =
197
  let ck = repr ck in
198
  match ck.cdesc with
199 45f0f48d xthirioux
  | Carrow _ | Clink _ | Cvar | Cunivar ->
200 22fe1c93 ploc
      failwith "internal error clock_of_impnode_clock"
201
  | Ctuple cklist -> List.hd cklist
202 45f0f48d xthirioux
  | Con (_,_,_) 
203
  | Ccarrying (_,_) -> ck
204
205 22fe1c93 ploc
206
(** [is_polymorphic ck] returns true if [ck] is polymorphic. *)
207
let rec is_polymorphic ck =
208
  match ck.cdesc with
209 45f0f48d xthirioux
  | Cvar -> false
210 22fe1c93 ploc
  | Carrow (ck1,ck2) -> (is_polymorphic ck1) || (is_polymorphic ck2)
211
  | Ctuple ckl -> List.exists (fun c -> is_polymorphic c) ckl
212
  | Con (ck',_,_) -> is_polymorphic ck'
213 45f0f48d xthirioux
  | Cunivar  -> true
214 22fe1c93 ploc
  | Clink ck' -> is_polymorphic ck'
215
  | Ccarrying (_,ck') -> is_polymorphic ck'
216
217
(** [constrained_vars_of_clock ck] returns the clock variables subject
218
    to sub-typing constraints appearing in clock [ck]. Removes duplicates *)
219
(* Used mainly for debug, non-linear complexity. *)
220
let rec constrained_vars_of_clock ck =
221
  let rec aux vars ck =
222
    match ck.cdesc with
223 45f0f48d xthirioux
    | Cvar -> vars
224 22fe1c93 ploc
    | Carrow (ck1,ck2) ->
225
        let l = aux vars ck1 in
226
        aux l ck2
227
    | Ctuple ckl ->
228
        List.fold_left
229
          (fun acc ck' -> aux acc ck') 
230
          vars ckl
231
    | Con (ck',_,_) -> aux vars ck'
232 45f0f48d xthirioux
    | Cunivar -> vars
233 22fe1c93 ploc
    | Clink ck' -> aux vars ck'
234
    | Ccarrying (_,ck') -> aux vars ck'
235
  in
236
  aux [] ck
237
238 b1a97ade xthirioux
let eq_carrier cr1 cr2 =
239
  match (carrier_repr cr1).carrier_desc, (carrier_repr cr2).carrier_desc with
240
 | Carry_const id1, Carry_const id2 -> id1 = id2
241
 | _                                -> cr1.carrier_id = cr2.carrier_id
242
243 919292ca xthirioux
let eq_clock ck1 ck2 =
244
 (repr ck1).cid = (repr ck2).cid
245
246 8f89eba8 xthirioux
(* Returns the clock root of a clock *)
247
let rec root ck =
248 919292ca xthirioux
  let ck = repr ck in
249
  match ck.cdesc with
250 6fdfb60b xthirioux
  | Ctuple (ck'::_)
251
  | Con (ck',_,_) | Clink ck' | Ccarrying (_,ck') -> root ck'
252 45f0f48d xthirioux
  | Cvar | Cunivar -> ck
253 6fdfb60b xthirioux
  | Carrow _ | Ctuple _ -> failwith "Internal error root"
254 22fe1c93 ploc
255 8f89eba8 xthirioux
(* Returns the branch of clock [ck] in its clock tree *)
256
let rec branch ck =
257
  let rec branch ck acc =
258
    match (repr ck).cdesc with
259
    | Ccarrying (_, ck) -> branch ck acc
260
    | Con (ck, cr, l)   -> branch ck ((cr, l) :: acc)
261 6fdfb60b xthirioux
    | Ctuple (ck::_)    -> branch ck acc
262 919292ca xthirioux
    | Ctuple _
263 8f89eba8 xthirioux
    | Carrow _          -> assert false
264
    | _                 -> acc
265
  in branch ck [];;
266
267 54d032f5 xthirioux
let clock_of_root_branch r br =
268
 List.fold_left (fun ck (cr,l) -> new_ck (Con (ck, cr, l)) true) r br
269
270
(* Computes the (longest) common prefix of two branches *)
271
let rec common_prefix br1 br2 =
272
 match br1, br2 with
273
 | []          , _
274
 | _           , []           -> []
275
 | (cr1,l1)::q1, (cr2,l2)::q2 ->
276
   if eq_carrier cr1 cr2 && (l1 = l2)
277
   then (cr1, l1) :: common_prefix q1 q2
278
   else []
279
280 8f89eba8 xthirioux
(* Tests whether clock branches [br1] nd [br2] are statically disjoint *)
281
let rec disjoint_branches br1 br2 =
282
 match br1, br2 with
283
 | []          , _
284
 | _           , []           -> false
285 b1a97ade xthirioux
 | (cr1,l1)::q1, (cr2,l2)::q2 -> eq_carrier cr1 cr2 && ((l1 <> l2) || disjoint_branches q1 q2);;
286 8f89eba8 xthirioux
287
(* Disjunction relation between variables based upon their static clocks. *)
288
let disjoint ck1 ck2 =
289 919292ca xthirioux
  eq_clock (root ck1) (root ck2) && disjoint_branches (branch ck1) (branch ck2)
290 8f89eba8 xthirioux
291 22fe1c93 ploc
let print_cvar fmt cvar =
292
  match cvar.cdesc with
293 45f0f48d xthirioux
  | Cvar ->
294 22fe1c93 ploc
 (*
295
      if cvar.cscoped
296
      then
297 45f0f48d xthirioux
	fprintf fmt "[_%s]"
298 22fe1c93 ploc
	  (name_of_type cvar.cid)
299
      else
300
 *)
301 45f0f48d xthirioux
	fprintf fmt "_%s"
302 22fe1c93 ploc
	  (name_of_type cvar.cid)
303 45f0f48d xthirioux
  | Cunivar ->
304 22fe1c93 ploc
 (*
305
      if cvar.cscoped
306
      then
307 45f0f48d xthirioux
	fprintf fmt "['%s]"
308 22fe1c93 ploc
	  (name_of_type cvar.cid)
309
      else
310
 *)
311 45f0f48d xthirioux
	fprintf fmt "'%s"
312 22fe1c93 ploc
	  (name_of_type cvar.cid)
313
  | _ -> failwith "Internal error print_cvar"
314
315
(* Nice pretty-printing. Simplifies expressions before printing them. Non-linear
316
   complexity. *)
317
let print_ck fmt ck =
318
  let rec aux fmt ck =
319
    match ck.cdesc with
320
    | Carrow (ck1,ck2) ->
321 e39f5319 xthirioux
      fprintf fmt "%a -> %a" aux ck1 aux ck2
322 22fe1c93 ploc
    | Ctuple cklist ->
323
      fprintf fmt "(%a)" 
324
	(fprintf_list ~sep:" * " aux) cklist
325
    | Con (ck,c,l) ->
326
      fprintf fmt "%a on %s(%a)" aux ck l print_carrier c
327 45f0f48d xthirioux
    | Cvar ->
328 22fe1c93 ploc
(*
329
      if ck.cscoped
330
      then
331 52cfee34 xthirioux
        fprintf fmt "[_%s]" (name_of_type ck.cid)
332 22fe1c93 ploc
      else
333
*)
334 52cfee34 xthirioux
	fprintf fmt "_%s" (name_of_type ck.cid)
335 45f0f48d xthirioux
    | Cunivar ->
336 22fe1c93 ploc
(*
337
      if ck.cscoped
338
      then
339
        fprintf fmt "['%s]" (name_of_type ck.cid)
340
      else
341
*)
342
        fprintf fmt "'%s" (name_of_type ck.cid)
343
    | Clink ck' ->
344
        aux fmt ck'
345
    | Ccarrying (cr,ck') ->
346
      fprintf fmt "(%a:%a)" print_carrier cr aux ck'
347
  in
348
  let cvars = constrained_vars_of_clock ck in
349
  aux fmt ck;
350
  if cvars <> [] then
351
    fprintf fmt " (where %a)"
352
      (fprintf_list ~sep:", " print_cvar) cvars
353
354 8f1c7e91 xthirioux
(* prints only the Con components of a clock, useful for printing nodes *)
355
let rec print_ck_suffix fmt ck =
356
  match ck.cdesc with
357
  | Carrow _
358
  | Ctuple _
359 45f0f48d xthirioux
  | Cvar 
360
  | Cunivar    -> ()
361 8f1c7e91 xthirioux
  | Con (ck,c,l) ->
362 629392e1 ploc
     if !Options.kind2_print then
363
       print_ck_suffix fmt ck
364
     else
365
       fprintf fmt "%a when %s(%a)" print_ck_suffix ck l print_carrier c
366 8f1c7e91 xthirioux
  | Clink ck' ->
367
    print_ck_suffix fmt ck'
368
  | Ccarrying (cr,ck') ->
369
    fprintf fmt "%a" print_ck_suffix ck'
370 45f0f48d xthirioux
371 8f1c7e91 xthirioux
372 22fe1c93 ploc
let pp_error fmt = function
373
  | Clock_clash (ck1,ck2) ->
374
      reset_names ();
375
      fprintf fmt "Expected clock %a, got clock %a@."
376
      print_ck ck1
377
      print_ck ck2
378
  | Carrier_mismatch (cr1, cr2) ->
379
     fprintf fmt "Name clash. Expected clock %a, got clock %a@."
380
       print_carrier cr1
381
       print_carrier cr2
382
  | Cannot_be_polymorphic ck ->
383
      reset_names ();
384
    fprintf fmt "The main node cannot have a polymorphic clock: %a@."
385
      print_ck ck
386
  | Invalid_imported_clock ck ->
387
      reset_names ();
388
    fprintf fmt "Not a valid imported node clock: %a@."
389
      print_ck ck
390
  | Invalid_const ck ->
391
      reset_names ();
392
    fprintf fmt "Clock %a is not a valid periodic clock@."
393
      print_ck ck;
394
  | Factor_zero ->
395
    fprintf fmt "Cannot apply clock transformation with factor 0@."
396
  | Carrier_extrusion (ck,cr) ->
397
    fprintf fmt "This node has clock@.%a@.It is invalid as %a escapes its scope@."
398
      print_ck ck
399
      print_carrier cr
400
  | Clock_extrusion (ck_node,ck) ->
401
    fprintf fmt "This node has clock@.%a@.It is invalid as %a escapes its scope@."
402
      print_ck ck_node
403
      print_ck ck
404
405 d4807c3d xthirioux
let const_of_carrier cr =
406 45f0f48d xthirioux
  match (carrier_repr cr).carrier_desc with
407
  | Carry_const id -> id
408
  | Carry_name
409
  | Carry_var
410
  | Carry_link _ -> (Format.eprintf "internal error: const_of_carrier %a@." print_carrier cr; assert false) (* TODO check this Xavier *)
411 d4807c3d xthirioux
 
412 8f1c7e91 xthirioux
let uneval const cr =
413 89b9e25c xthirioux
  (*Format.printf "Clocks.uneval %s %a@." const print_carrier cr;*)
414 8f1c7e91 xthirioux
  let cr = carrier_repr cr in
415
  match cr.carrier_desc with
416
  | Carry_var -> cr.carrier_desc <- Carry_const const
417 d4807c3d xthirioux
  | Carry_name -> cr.carrier_desc <- Carry_const const
418 8f1c7e91 xthirioux
  | _         -> assert false
419 22fe1c93 ploc
420 333e3a25 ploc
421
(* Used in rename functions in Corelang. We have to propagate the renaming to
422
   ids of variables clocking the signals *)
423
424
(* Carrier are not renamed. They corresponds to enumerated type constants *)
425
(*
426
let rec rename_carrier f c =
427
  { c with carrier_desc = rename_carrier_desc fvar c.carrier_desc }
428
and rename_carrier_desc f 
429
let re = rename_carrier f
430
  match cd with
431
  | Carry_const id -> Carry_const (f id)
432
  | Carry_link ce -> Carry_link (re ce)
433
  | _ -> cd
434
*)
435
436
     
437
let rec rename_clock_expr fvar c =
438
  { c with cdesc = rename_clock_desc fvar c.cdesc }
439
and rename_clock_desc fvar cd =
440
  let re = rename_clock_expr fvar in
441
  match cd with
442
  | Carrow (c1, c2) -> Carrow (re c1, re c2)
443
  | Ctuple cl -> Ctuple (List.map re cl)
444
  | Con (c1, car, id) -> Con (re c1, car, fvar id)
445
  | Cvar 
446
  | Cunivar -> cd
447
  | Clink c -> Clink (re c)
448
  | Ccarrying (car, c) -> Ccarrying (car, re c)
449
    
450 22fe1c93 ploc
(* Local Variables: *)
451
(* compile-command:"make -C .." *)
452
(* End: *)