Project

General

Profile

Download (13.9 KB) Statistics
| Branch: | Tag: | Revision:
1
(********************************************************************)
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

    
14
(** Clocks definitions and a few utility functions on clocks. *)
15
open Utils
16
open Format
17

    
18
(* (\* Clock type sets, for subtyping. *\) *)
19
(* type clock_set = *)
20
(*     CSet_all *)
21
(*   | CSet_pck of int*rat *)
22

    
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
     
37
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
  (* | 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
  | 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
  (* | Not_pck *)
59
  (* | Clock_set_mismatch of clock_expr * clock_set *)
60
  | 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
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
      fprintf fmt "%a -> %a" print_ck_long ck1 print_ck_long ck2
91
  | 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
  | Cvar -> fprintf fmt "'_%i" ck.cid 
97
  | Cunivar -> fprintf fmt "'%i" ck.cid 
98
  | 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
let new_id = ref (-1)
104

    
105
let rec bottom =
106
  { cdesc = Clink bottom; cid = -666; cscoped = false }
107

    
108
let new_ck desc scoped =
109
  incr new_id; {cdesc=desc; cid = !new_id; cscoped = scoped}
110

    
111
let new_var scoped = new_ck Cvar scoped
112

    
113
let new_univar () = new_ck Cunivar false
114

    
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
let get_carrier_name ck =
137
 match (repr ck).cdesc with
138
 | Ccarrying (cr, _) -> Some cr
139
 | _                 -> None
140

    
141
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
let uncarrier ck =
153
 match ck.cdesc with
154
 | Ccarrying (_, ck') -> ck'
155
 | _                  -> ck
156

    
157
(* Removes all links in a clock. Only used for clocks
158
   simplification though. *)
159
let rec simplify ck =
160
  match ck.cdesc with
161
  | Carrow (ck1,ck2) ->
162
      new_ck (Carrow (simplify ck1, simplify ck2)) ck.cscoped
163
  | Ctuple cl ->
164
      new_ck (Ctuple (List.map simplify cl)) ck.cscoped
165
  | Con (ck', c, l) ->
166
      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

    
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
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
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
let clock_of_impnode_clock ck =
197
  let ck = repr ck in
198
  match ck.cdesc with
199
  | Carrow _ | Clink _ | Cvar | Cunivar ->
200
      failwith "internal error clock_of_impnode_clock"
201
  | Ctuple cklist -> List.hd cklist
202
  | Con (_,_,_) 
203
  | Ccarrying (_,_) -> ck
204

    
205

    
206
(** [is_polymorphic ck] returns true if [ck] is polymorphic. *)
207
let rec is_polymorphic ck =
208
  match ck.cdesc with
209
  | Cvar -> false
210
  | 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
  | Cunivar  -> true
214
  | 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
    | Cvar -> vars
224
    | 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
    | Cunivar -> vars
233
    | Clink ck' -> aux vars ck'
234
    | Ccarrying (_,ck') -> aux vars ck'
235
  in
236
  aux [] ck
237

    
238
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
let eq_clock ck1 ck2 =
244
 (repr ck1).cid = (repr ck2).cid
245

    
246
(* Returns the clock root of a clock *)
247
let rec root ck =
248
  let ck = repr ck in
249
  match ck.cdesc with
250
  | Ctuple (ck'::_)
251
  | Con (ck',_,_) | Clink ck' | Ccarrying (_,ck') -> root ck'
252
  | Cvar | Cunivar -> ck
253
  | Carrow _ | Ctuple _ -> failwith "Internal error root"
254

    
255
(* 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
    | Ctuple (ck::_)    -> branch ck acc
262
    | Ctuple _
263
    | Carrow _          -> assert false
264
    | _                 -> acc
265
  in branch ck [];;
266

    
267
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
(* 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
 | (cr1,l1)::q1, (cr2,l2)::q2 -> eq_carrier cr1 cr2 && ((l1 <> l2) || disjoint_branches q1 q2);;
286

    
287
(* Disjunction relation between variables based upon their static clocks. *)
288
let disjoint ck1 ck2 =
289
  eq_clock (root ck1) (root ck2) && disjoint_branches (branch ck1) (branch ck2)
290

    
291
let print_cvar fmt cvar =
292
  match cvar.cdesc with
293
  | Cvar ->
294
 (*
295
      if cvar.cscoped
296
      then
297
	fprintf fmt "[_%s]"
298
	  (name_of_type cvar.cid)
299
      else
300
 *)
301
	fprintf fmt "_%s"
302
	  (name_of_type cvar.cid)
303
  | Cunivar ->
304
 (*
305
      if cvar.cscoped
306
      then
307
	fprintf fmt "['%s]"
308
	  (name_of_type cvar.cid)
309
      else
310
 *)
311
	fprintf fmt "'%s"
312
	  (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
      fprintf fmt "%a -> %a" aux ck1 aux ck2
322
    | 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
    | Cvar ->
328
(*
329
      if ck.cscoped
330
      then
331
        fprintf fmt "[_%s]" (name_of_type ck.cid)
332
      else
333
*)
334
	fprintf fmt "_%s" (name_of_type ck.cid)
335
    | Cunivar ->
336
(*
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
(* 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
  | Cvar 
360
  | Cunivar    -> ()
361
  | Con (ck,c,l) ->
362
    fprintf fmt "%a when %s(%a)" print_ck_suffix ck l print_carrier c
363
  | Clink ck' ->
364
    print_ck_suffix fmt ck'
365
  | Ccarrying (cr,ck') ->
366
    fprintf fmt "%a" print_ck_suffix ck'
367

    
368

    
369
let pp_error fmt = function
370
  | Clock_clash (ck1,ck2) ->
371
      reset_names ();
372
      fprintf fmt "Expected clock %a, got clock %a@."
373
      print_ck ck1
374
      print_ck ck2
375
  | Carrier_mismatch (cr1, cr2) ->
376
     fprintf fmt "Name clash. Expected clock %a, got clock %a@."
377
       print_carrier cr1
378
       print_carrier cr2
379
  | Cannot_be_polymorphic ck ->
380
      reset_names ();
381
    fprintf fmt "The main node cannot have a polymorphic clock: %a@."
382
      print_ck ck
383
  | Invalid_imported_clock ck ->
384
      reset_names ();
385
    fprintf fmt "Not a valid imported node clock: %a@."
386
      print_ck ck
387
  | Invalid_const ck ->
388
      reset_names ();
389
    fprintf fmt "Clock %a is not a valid periodic clock@."
390
      print_ck ck;
391
  | Factor_zero ->
392
    fprintf fmt "Cannot apply clock transformation with factor 0@."
393
  | Carrier_extrusion (ck,cr) ->
394
    fprintf fmt "This node has clock@.%a@.It is invalid as %a escapes its scope@."
395
      print_ck ck
396
      print_carrier cr
397
  | Clock_extrusion (ck_node,ck) ->
398
    fprintf fmt "This node has clock@.%a@.It is invalid as %a escapes its scope@."
399
      print_ck ck_node
400
      print_ck ck
401

    
402
let const_of_carrier cr =
403
  match (carrier_repr cr).carrier_desc with
404
  | Carry_const id -> id
405
  | Carry_name
406
  | Carry_var
407
  | Carry_link _ -> (Format.eprintf "internal error: const_of_carrier %a@." print_carrier cr; assert false) (* TODO check this Xavier *)
408
 
409
let uneval const cr =
410
  (*Format.printf "Clocks.uneval %s %a@." const print_carrier cr;*)
411
  let cr = carrier_repr cr in
412
  match cr.carrier_desc with
413
  | Carry_var -> cr.carrier_desc <- Carry_const const
414
  | Carry_name -> cr.carrier_desc <- Carry_const const
415
  | _         -> assert false
416

    
417

    
418
(* Used in rename functions in Corelang. We have to propagate the renaming to
419
   ids of variables clocking the signals *)
420

    
421
(* Carrier are not renamed. They corresponds to enumerated type constants *)
422
(*
423
let rec rename_carrier f c =
424
  { c with carrier_desc = rename_carrier_desc fvar c.carrier_desc }
425
and rename_carrier_desc f 
426
let re = rename_carrier f
427
  match cd with
428
  | Carry_const id -> Carry_const (f id)
429
  | Carry_link ce -> Carry_link (re ce)
430
  | _ -> cd
431
*)
432

    
433
     
434
let rec rename_clock_expr fvar c =
435
  { c with cdesc = rename_clock_desc fvar c.cdesc }
436
and rename_clock_desc fvar cd =
437
  let re = rename_clock_expr fvar in
438
  match cd with
439
  | Carrow (c1, c2) -> Carrow (re c1, re c2)
440
  | Ctuple cl -> Ctuple (List.map re cl)
441
  | Con (c1, car, id) -> Con (re c1, car, fvar id)
442
  | Cvar 
443
  | Cunivar -> cd
444
  | Clink c -> Clink (re c)
445
  | Ccarrying (car, c) -> Ccarrying (car, re c)
446
    
447
(* Local Variables: *)
448
(* compile-command:"make -C .." *)
449
(* End: *)
(13-13/66)