Project

General

Profile

Download (14.1 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
195
      | Con(ck',_,_) -> ck'
196
      | _ -> Format.eprintf "internal error: Clocks.clock_current %a@." print_ck_long (repr ck);
197
        assert false) (clock_list_of_clock ck))
198

    
199
let clock_of_impnode_clock ck =
200
  let ck = repr ck in
201
  match ck.cdesc with
202
  | Carrow _ | Clink _ | Cvar | Cunivar ->
203
      failwith "internal error clock_of_impnode_clock"
204
  | Ctuple cklist -> List.hd cklist
205
  | Con (_,_,_) 
206
  | Ccarrying (_,_) -> ck
207

    
208

    
209
(** [is_polymorphic ck] returns true if [ck] is polymorphic. *)
210
let rec is_polymorphic ck =
211
  match ck.cdesc with
212
  | Cvar -> false
213
  | Carrow (ck1,ck2) -> (is_polymorphic ck1) || (is_polymorphic ck2)
214
  | Ctuple ckl -> List.exists (fun c -> is_polymorphic c) ckl
215
  | Con (ck',_,_) -> is_polymorphic ck'
216
  | Cunivar  -> true
217
  | Clink ck' -> is_polymorphic ck'
218
  | Ccarrying (_,ck') -> is_polymorphic ck'
219

    
220
(** [constrained_vars_of_clock ck] returns the clock variables subject
221
    to sub-typing constraints appearing in clock [ck]. Removes duplicates *)
222
(* Used mainly for debug, non-linear complexity. *)
223
let constrained_vars_of_clock ck =
224
  let rec aux vars ck =
225
    match ck.cdesc with
226
    | Cvar -> vars
227
    | Carrow (ck1,ck2) ->
228
        let l = aux vars ck1 in
229
        aux l ck2
230
    | Ctuple ckl ->
231
        List.fold_left
232
          (fun acc ck' -> aux acc ck') 
233
          vars ckl
234
    | Con (ck',_,_) -> aux vars ck'
235
    | Cunivar -> vars
236
    | Clink ck' -> aux vars ck'
237
    | Ccarrying (_,ck') -> aux vars ck'
238
  in
239
  aux [] ck
240

    
241
let eq_carrier cr1 cr2 =
242
  match (carrier_repr cr1).carrier_desc, (carrier_repr cr2).carrier_desc with
243
 | Carry_const id1, Carry_const id2 -> id1 = id2
244
 | _                                -> cr1.carrier_id = cr2.carrier_id
245

    
246
let eq_clock ck1 ck2 =
247
 (repr ck1).cid = (repr ck2).cid
248

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

    
258
(* Returns the branch of clock [ck] in its clock tree *)
259
let branch ck =
260
  let rec branch ck acc =
261
    match (repr ck).cdesc with
262
    | Ccarrying (_, ck) -> branch ck acc
263
    | Con (ck, cr, l)   -> branch ck ((cr, l) :: acc)
264
    | Ctuple (ck::_)     -> branch ck acc
265
    | Ctuple _
266
    | Carrow _          -> assert false
267
    | _                 -> acc
268
  in branch ck [];;
269

    
270
let clock_of_root_branch r br =
271
 List.fold_left (fun ck (cr,l) -> new_ck (Con (ck, cr, l)) true) r br
272

    
273
(* Computes the (longest) common prefix of two branches *)
274
let rec common_prefix br1 br2 =
275
 match br1, br2 with
276
 | []          , _
277
 | _           , []           -> []
278
 | (cr1,l1)::q1, (cr2,l2)::q2 ->
279
   if eq_carrier cr1 cr2 && (l1 = l2)
280
   then (cr1, l1) :: common_prefix q1 q2
281
   else []
282

    
283
(* Tests whether clock branches [br1] nd [br2] are statically disjoint *)
284
let rec disjoint_branches br1 br2 =
285
 match br1, br2 with
286
 | []          , _
287
 | _           , []           -> false
288
 | (cr1,l1)::q1, (cr2,l2)::q2 -> eq_carrier cr1 cr2 && ((l1 <> l2) || disjoint_branches q1 q2);;
289

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

    
294
let print_cvar fmt cvar =
295
  match cvar.cdesc with
296
  | Cvar ->
297
 (*
298
      if cvar.cscoped
299
      then
300
	fprintf fmt "[_%s]"
301
	  (name_of_type cvar.cid)
302
      else
303
 *)
304
	fprintf fmt "_%s"
305
	  (name_of_type cvar.cid)
306
  | Cunivar ->
307
 (*
308
      if cvar.cscoped
309
      then
310
	fprintf fmt "['%s]"
311
	  (name_of_type cvar.cid)
312
      else
313
 *)
314
	fprintf fmt "'%s"
315
	  (name_of_type cvar.cid)
316
  | _ -> failwith "Internal error print_cvar"
317

    
318
(* Nice pretty-printing. Simplifies expressions before printing them. Non-linear
319
   complexity. *)
320
let print_ck fmt ck =
321
  let rec aux fmt ck =
322
    match ck.cdesc with
323
    | Carrow (ck1,ck2) ->
324
      fprintf fmt "%a -> %a" aux ck1 aux ck2
325
    | Ctuple cklist ->
326
      fprintf fmt "(%a)" 
327
	(fprintf_list ~sep:" * " aux) cklist
328
    | Con (ck,c,l) ->
329
      fprintf fmt "%a on %s(%a)" aux ck l print_carrier c
330
    | Cvar ->
331
(*
332
      if ck.cscoped
333
      then
334
        fprintf fmt "[_%s]" (name_of_type ck.cid)
335
      else
336
*)
337
	fprintf fmt "_%s" (name_of_type ck.cid)
338
    | Cunivar ->
339
(*
340
      if ck.cscoped
341
      then
342
        fprintf fmt "['%s]" (name_of_type ck.cid)
343
      else
344
*)
345
        fprintf fmt "'%s" (name_of_type ck.cid)
346
    | Clink ck' ->
347
        aux fmt ck'
348
    | Ccarrying (cr,ck') ->
349
      fprintf fmt "(%a:%a)" print_carrier cr aux ck'
350
  in
351
  let cvars = constrained_vars_of_clock ck in
352
  aux fmt ck;
353
  if cvars <> [] then
354
    fprintf fmt " (where %a)"
355
      (fprintf_list ~sep:", " print_cvar) cvars
356

    
357
(* prints only the Con components of a clock, useful for printing nodes *)
358
let rec print_ck_suffix fmt ck =
359
  match ck.cdesc with
360
  | Carrow _
361
  | Ctuple _
362
  | Cvar 
363
  | Cunivar    -> ()
364
  | Con (ck,c,l) ->
365
     if !Options.kind2_print then
366
       print_ck_suffix fmt ck
367
     else
368
       fprintf fmt "%a when %s(%a)" print_ck_suffix ck l print_carrier c
369
  | Clink ck' ->
370
    print_ck_suffix fmt ck'
371
  | Ccarrying (_, ck') ->
372
    fprintf fmt "%a" print_ck_suffix ck'
373

    
374

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

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

    
423

    
424
(* Used in rename functions in Corelang. We have to propagate the renaming to
425
   ids of variables clocking the signals *)
426

    
427
(* Carrier are not renamed. They corresponds to enumerated type constants *)
428
(*
429
let rec rename_carrier f c =
430
  { c with carrier_desc = rename_carrier_desc fvar c.carrier_desc }
431
and rename_carrier_desc f 
432
let re = rename_carrier f
433
  match cd with
434
  | Carry_const id -> Carry_const (f id)
435
  | Carry_link ce -> Carry_link (re ce)
436
  | _ -> cd
437
*)
438

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