Project

General

Profile

Statistics
| Branch: | Tag: | Revision:

lustrec / src / normalization.ml @ 14ebde97

History | View | Annotate | Download (15 KB)

1
(* ----------------------------------------------------------------------------
2
 * SchedMCore - A MultiCore Scheduling Framework
3
 * Copyright (C) 2009-2013, ONERA, Toulouse, FRANCE - LIFL, Lille, FRANCE
4
 * Copyright (C) 2012-2013, INPT, Toulouse, FRANCE
5
 *
6
 * This file is part of Prelude
7
 *
8
 * Prelude is free software; you can redistribute it and/or
9
 * modify it under the terms of the GNU Lesser General Public License
10
 * as published by the Free Software Foundation ; either version 2 of
11
 * the License, or (at your option) any later version.
12
 *
13
 * Prelude is distributed in the hope that it will be useful, but
14
 * WITHOUT ANY WARRANTY ; without even the implied warranty of
15
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16
 * Lesser General Public License for more details.
17
 *
18
 * You should have received a copy of the GNU Lesser General Public
19
 * License along with this program ; if not, write to the Free Software
20
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
21
 * USA
22
 *---------------------------------------------------------------------------- *)
23

    
24
(* This module is used for the lustre to C compiler *)
25

    
26

    
27
open Utils
28
open LustreSpec
29
open Corelang
30
(* open Clocks *)
31
open Format
32

    
33
let expr_true loc ck =
34
{ expr_tag = Utils.new_tag ();
35
  expr_desc = Expr_const (Const_tag tag_true);
36
  expr_type = Type_predef.type_bool;
37
  expr_clock = ck;
38
  expr_delay = Delay.new_var ();
39
  expr_annot = None;
40
  expr_loc = loc }
41

    
42
let expr_false loc ck =
43
{ expr_tag = Utils.new_tag ();
44
  expr_desc = Expr_const (Const_tag tag_false);
45
  expr_type = Type_predef.type_bool;
46
  expr_clock = ck;
47
  expr_delay = Delay.new_var ();
48
  expr_annot = None;
49
  expr_loc = loc }
50

    
51
let expr_once loc ck =
52
 { expr_tag = Utils.new_tag ();
53
  expr_desc = Expr_arrow (expr_true loc ck, expr_false loc ck);
54
  expr_type = Type_predef.type_bool;
55
  expr_clock = ck;
56
  expr_delay = Delay.new_var ();
57
  expr_annot = None;
58
  expr_loc = loc }
59

    
60
let is_expr_once =
61
  let dummy_expr_once = expr_once Location.dummy_loc (Clocks.new_var true) in
62
  fun expr -> Corelang.is_eq_expr expr dummy_expr_once
63

    
64
let unfold_arrow expr =
65
 match expr.expr_desc with
66
 | Expr_arrow (e1, e2) ->
67
    let loc = expr.expr_loc in
68
    let ck = expr.expr_clock in
69
    { expr with expr_desc = Expr_ite (expr_once loc ck, e1, e2) }
70
 | _                   -> assert false
71

    
72
let cpt_fresh = ref 0
73

    
74
(* Generate a new local [node] variable *)
75
let mk_fresh_var node loc ty ck =
76
  let vars = node_vars node in
77
  let rec aux () =
78
  incr cpt_fresh;
79
  let s = Printf.sprintf "__%s_%d" node.node_id !cpt_fresh in
80
  if List.exists (fun v -> v.var_id = s) vars then aux () else
81
  {
82
    var_id = s;
83
    var_dec_type = dummy_type_dec;
84
    var_dec_clock = dummy_clock_dec;
85
    var_dec_const = false;
86
    var_type = ty;
87
    var_clock = ck;
88
    var_loc = loc
89
  }
90
  in aux ()
91

    
92
(* Generate a new ident expression from a declared variable *)
93
let mk_ident_expr v =
94
  { expr_tag = new_tag ();
95
    expr_desc = Expr_ident v.var_id;
96
    expr_type = v.var_type;
97
    expr_clock = v.var_clock;
98
    expr_delay = Delay.new_var ();
99
    expr_annot = None;
100
    expr_loc = v.var_loc }
101

    
102
(* Get the equation in [defs] with [expr] as rhs, if any *)
103
let get_expr_alias defs expr =
104
 try Some (List.find (fun eq -> is_eq_expr eq.eq_rhs expr) defs)
105
 with
106
   Not_found -> None
107

    
108
(* Replace [expr] with (tuple of) [locals] *)
109
let replace_expr locals expr =
110
 match locals with
111
 | []  -> assert false
112
 | [v] -> { expr with
113
   expr_tag = Utils.new_tag ();
114
   expr_desc = Expr_ident v.var_id }
115
 | _   -> { expr with
116
   expr_tag = Utils.new_tag ();
117
   expr_desc = Expr_tuple (List.map mk_ident_expr locals) }
118

    
119
let unfold_offsets e offsets =
120
  let add_offset e d =
121
(*Format.eprintf "add_offset %a %a@." Dimension.pp_dimension (Types.array_type_dimension e.expr_type) Dimension.pp_dimension d;*)
122
    { e with
123
      expr_tag = Utils.new_tag ();
124
      expr_loc = d.Dimension.dim_loc;
125
      expr_type = Types.array_element_type e.expr_type;
126
      expr_desc = Expr_access (e, d) } in
127
 List.fold_left add_offset e offsets
128

    
129
(* Create an alias for [expr], if none exists yet *)
130
let mk_expr_alias node (defs, vars) expr =
131
  match get_expr_alias defs expr with
132
  | Some eq ->
133
    let aliases = List.map (fun id -> List.find (fun v -> v.var_id = id) vars) eq.eq_lhs in
134
    (defs, vars), replace_expr aliases expr
135
  | None    ->
136
    let new_aliases =
137
      List.map2
138
	(mk_fresh_var node expr.expr_loc)
139
	(Types.type_list_of_type expr.expr_type)
140
	(Clocks.clock_list_of_clock expr.expr_clock) in
141
    let new_def =
142
      mkeq expr.expr_loc (List.map (fun v -> v.var_id) new_aliases, expr)
143
    in (new_def::defs, new_aliases@vars), replace_expr new_aliases expr
144

    
145
(* Create an alias for [expr], if [opt] *)
146
let mk_expr_alias_opt opt node defvars expr =
147
 if opt
148
 then
149
   mk_expr_alias node defvars expr
150
 else
151
   defvars, expr
152

    
153
(* Create a (normalized) expression from [ref_e], 
154
   replacing description with [norm_d],
155
   taking propagated [offsets] into account 
156
   in order to change expression type *)
157
let mk_norm_expr offsets ref_e norm_d =
158
  let drop_array_type ty =
159
    Types.map_tuple_type Types.array_element_type ty in
160
  { ref_e with
161
    expr_desc = norm_d;
162
    expr_type = Utils.repeat (List.length offsets) drop_array_type ref_e.expr_type }
163

    
164
(* normalize_<foo> : defs * used vars -> <foo> -> (updated defs * updated vars) * normalized <foo> *)
165
let rec normalize_list alias node offsets norm_element defvars elist =
166
  List.fold_right
167
    (fun t (defvars, qlist) ->
168
      let defvars, norm_t = norm_element alias node offsets defvars t in
169
      (defvars, norm_t :: qlist)
170
    ) elist (defvars, [])
171

    
172
let rec normalize_expr ?(alias=true) node offsets defvars expr =
173
(*  Format.eprintf "normalize %B %a [%a]@." alias Printers.pp_expr expr (Utils.fprintf_list ~sep:"," Dimension.pp_dimension) offsets;*)
174
  match expr.expr_desc with
175
  | Expr_const _ 
176
  | Expr_ident _ -> defvars, unfold_offsets expr offsets
177
  | Expr_array elist ->
178
    let defvars, norm_elist = normalize_list alias node offsets (fun _ -> normalize_array_expr ~alias:true) defvars elist in
179
    let norm_expr = mk_norm_expr offsets expr (Expr_array norm_elist) in
180
    mk_expr_alias_opt alias node defvars norm_expr
181
  | Expr_power (e1, d) when offsets = [] ->
182
    let defvars, norm_e1 = normalize_expr node offsets defvars e1 in
183
    let norm_expr = mk_norm_expr offsets expr (Expr_power (norm_e1, d)) in
184
    mk_expr_alias_opt alias node defvars norm_expr
185
  | Expr_power (e1, d) ->
186
    normalize_expr ~alias:alias node (List.tl offsets) defvars e1
187
  | Expr_access (e1, d) ->
188
    normalize_expr ~alias:alias node (d::offsets) defvars e1
189
  | Expr_tuple elist -> 
190
    let defvars, norm_elist =
191
      normalize_list alias node offsets (fun alias -> normalize_expr ~alias:alias) defvars elist in
192
    defvars, mk_norm_expr offsets expr (Expr_tuple norm_elist)
193
  | Expr_appl (id, args, None) when Basic_library.is_internal_fun id && Types.is_array_type expr.expr_type ->
194
    let defvars, norm_args = normalize_list alias node offsets (fun _ -> normalize_array_expr ~alias:true) defvars (expr_list_of_expr args) in
195
    defvars, mk_norm_expr offsets expr (Expr_appl (id, expr_of_expr_list args.expr_loc norm_args, None))
196
  | Expr_appl (id, args, None) when Basic_library.is_internal_fun id ->
197
    let defvars, norm_args = normalize_expr ~alias:true node offsets defvars args in
198
    defvars, mk_norm_expr offsets expr (Expr_appl (id, norm_args, None))
199
  | Expr_appl (id, args, r) ->
200
    let defvars, norm_args = normalize_expr node [] defvars args in
201
    let norm_expr = mk_norm_expr [] expr (Expr_appl (id, norm_args, r)) in
202
    if offsets <> []
203
    then
204
      let defvars, norm_expr = normalize_expr node [] defvars norm_expr in
205
      normalize_expr ~alias:alias node offsets defvars norm_expr
206
    else
207
      mk_expr_alias_opt (alias && not (Basic_library.is_internal_fun id)) node defvars norm_expr
208
  | Expr_arrow (e1,e2) when not (is_expr_once expr) -> (* Here we differ from Colaco paper: arrows are pushed to the top *)
209
    normalize_expr ~alias:alias node offsets defvars (unfold_arrow expr)
210
  | Expr_arrow (e1,e2) ->
211
    let defvars, norm_e1 = normalize_expr node offsets defvars e1 in
212
    let defvars, norm_e2 = normalize_expr node offsets defvars e2 in
213
    let norm_expr = mk_norm_expr offsets expr (Expr_arrow (norm_e1, norm_e2)) in
214
    mk_expr_alias_opt alias node defvars norm_expr
215
  | Expr_pre e ->
216
    let defvars, norm_e = normalize_expr node offsets defvars e in
217
    let norm_expr = mk_norm_expr offsets expr (Expr_pre norm_e) in
218
    mk_expr_alias_opt alias node defvars norm_expr
219
  | Expr_fby (e1, e2) ->
220
    let defvars, norm_e1 = normalize_expr node offsets defvars e1 in
221
    let defvars, norm_e2 = normalize_expr node offsets defvars e2 in
222
    let norm_expr = mk_norm_expr offsets expr (Expr_fby (norm_e1, norm_e2)) in
223
    mk_expr_alias_opt alias node defvars norm_expr
224
  | Expr_when (e, c, l) ->
225
    let defvars, norm_e = normalize_expr node offsets defvars e in
226
    defvars, mk_norm_expr offsets expr (Expr_when (norm_e, c, l))
227
  | Expr_ite (c, t, e) ->
228
    let defvars, norm_c = normalize_guard node defvars c in
229
    let defvars, norm_t = normalize_cond_expr  node offsets defvars t in
230
    let defvars, norm_e = normalize_cond_expr  node offsets defvars e in
231
    let norm_expr = mk_norm_expr offsets expr (Expr_ite (norm_c, norm_t, norm_e)) in
232
    mk_expr_alias_opt alias node defvars norm_expr
233
  | Expr_merge (c, hl) ->
234
    let defvars, norm_hl = normalize_branches node offsets defvars hl in
235
    let norm_expr = mk_norm_expr offsets expr (Expr_merge (c, norm_hl)) in
236
    mk_expr_alias_opt alias node defvars norm_expr
237
  | Expr_uclock _
238
  | Expr_dclock _ 
239
  | Expr_phclock _ -> assert false (* Not handled yet *)
240
(* Creates a conditional with a merge construct, which is more lazy *)
241
(*
242
let norm_conditional_as_merge alias node norm_expr offsets defvars expr =
243
 match expr.expr_desc with
244
 | Expr_ite (c, t, e) ->
245
   let defvars, norm_t = norm_expr (alias node offsets defvars t in
246
 | _ -> assert false
247
*)
248
and normalize_branches node offsets defvars hl =
249
 List.fold_right
250
   (fun (t, h) (defvars, norm_q) ->
251
     let (defvars, norm_h) = normalize_cond_expr node offsets defvars h in
252
     defvars, (t, norm_h) :: norm_q
253
   )
254
   hl (defvars, [])
255

    
256
and normalize_array_expr ?(alias=true) node offsets defvars expr =
257
(*  Format.eprintf "normalize_array %B %a [%a]@." alias Printers.pp_expr expr (Utils.fprintf_list ~sep:"," Dimension.pp_dimension) offsets;*)
258
  match expr.expr_desc with
259
  | Expr_power (e1, d) when offsets = [] ->
260
    let defvars, norm_e1 = normalize_expr node offsets defvars e1 in
261
    defvars, mk_norm_expr offsets expr (Expr_power (norm_e1, d))
262
  | Expr_power (e1, d) ->
263
    normalize_array_expr ~alias:alias node (List.tl offsets) defvars e1
264
  | Expr_access (e1, d) -> normalize_array_expr ~alias:alias node (d::offsets) defvars e1
265
  | Expr_array elist when offsets = [] ->
266
    let defvars, norm_elist = normalize_list alias node offsets (fun _ -> normalize_array_expr ~alias:true) defvars elist in
267
    defvars, mk_norm_expr offsets expr (Expr_array norm_elist)
268
  | Expr_appl (id, args, None) when Basic_library.is_internal_fun id ->
269
    let defvars, norm_args = normalize_list alias node offsets (fun _ -> normalize_array_expr ~alias:true) defvars (expr_list_of_expr args) in
270
    defvars, mk_norm_expr offsets expr (Expr_appl (id, expr_of_expr_list args.expr_loc norm_args, None))
271
  |  _ -> normalize_expr ~alias:alias node offsets defvars expr
272

    
273
and normalize_cond_expr ?(alias=true) node offsets defvars expr =
274
  (*Format.eprintf "normalize_cond %B %a [%a]@." alias Printers.pp_expr expr (Utils.fprintf_list ~sep:"," Dimension.pp_dimension) offsets;*)
275
  match expr.expr_desc with
276
  | Expr_access (e1, d) ->
277
    normalize_cond_expr ~alias:alias node (d::offsets) defvars e1
278
  | Expr_ite (c, t, e) ->
279
    let defvars, norm_c = normalize_guard node defvars c in
280
    let defvars, norm_t = normalize_cond_expr node offsets defvars t in
281
    let defvars, norm_e = normalize_cond_expr node offsets defvars e in
282
    defvars, mk_norm_expr offsets expr (Expr_ite (norm_c, norm_t, norm_e))
283
  | Expr_merge (c, hl) ->
284
    let defvars, norm_hl = normalize_branches node offsets defvars hl in
285
    defvars, mk_norm_expr offsets expr (Expr_merge (c, norm_hl))
286
  | _ -> normalize_expr ~alias:alias node offsets defvars expr
287

    
288
and normalize_guard node defvars expr =
289
  match expr.expr_desc with
290
  | Expr_ident _ -> defvars, expr
291
  | _ ->
292
    let defvars, norm_expr = normalize_expr node [] defvars expr in
293
    mk_expr_alias_opt true node defvars norm_expr
294

    
295
(* outputs cannot be memories as well. If so, introduce new local variable.
296
*)
297
let decouple_outputs node defvars eq =
298
  let rec fold_lhs defvars lhs tys cks =
299
   match lhs, tys, cks with
300
   | [], [], []          -> defvars, []
301
   | v::qv, t::qt, c::qc -> let (defs_q, vars_q), lhs_q = fold_lhs defvars qv qt qc in
302
			    if List.exists (fun o -> o.var_id = v) node.node_outputs
303
			    then
304
			      let newvar = mk_fresh_var node eq.eq_loc t c in
305
			      let neweq  = mkeq eq.eq_loc ([v], mk_ident_expr newvar) in
306
			      (neweq :: defs_q, newvar :: vars_q), newvar.var_id :: lhs_q
307
			    else
308
			      (defs_q, vars_q), v::lhs_q
309
   | _                   -> assert false in
310
  let defvars', lhs' =
311
    fold_lhs
312
      defvars
313
      eq.eq_lhs
314
      (Types.type_list_of_type eq.eq_rhs.expr_type)
315
      (Clocks.clock_list_of_clock eq.eq_rhs.expr_clock) in
316
  defvars', {eq with eq_lhs = lhs' }
317

    
318
let rec normalize_eq node defvars eq = 
319
  match eq.eq_rhs.expr_desc with
320
  | Expr_pre _
321
  | Expr_fby _  ->
322
    let (defvars', eq') = decouple_outputs node defvars eq in
323
    let (defs', vars'), norm_rhs = normalize_expr ~alias:false node [] defvars' eq'.eq_rhs in
324
    let norm_eq = { eq' with eq_rhs = norm_rhs } in
325
    (norm_eq::defs', vars')
326
  | Expr_array _ ->
327
    let (defs', vars'), norm_rhs = normalize_array_expr ~alias:false node [] defvars eq.eq_rhs in
328
    let norm_eq = { eq with eq_rhs = norm_rhs } in
329
    (norm_eq::defs', vars')
330
  | Expr_appl (id, _, None) when Basic_library.is_internal_fun id && Types.is_array_type eq.eq_rhs.expr_type ->
331
    let (defs', vars'), norm_rhs = normalize_array_expr ~alias:false node [] defvars eq.eq_rhs in
332
    let norm_eq = { eq with eq_rhs = norm_rhs } in
333
    (norm_eq::defs', vars')
334
  | Expr_appl _ ->
335
    let (defs', vars'), norm_rhs = normalize_expr ~alias:false node [] defvars eq.eq_rhs in
336
    let norm_eq = { eq with eq_rhs = norm_rhs } in
337
    (norm_eq::defs', vars')
338
  | _ ->
339
    let (defs', vars'), norm_rhs = normalize_cond_expr ~alias:false node [] defvars eq.eq_rhs in
340
    let norm_eq = { eq with eq_rhs = norm_rhs } in
341
    norm_eq::defs', vars'
342

    
343
let normalize_node node = 
344
  cpt_fresh := 0;
345
  let inputs_outputs = node.node_inputs@node.node_outputs in
346
  let is_local v =
347
    List.for_all ((!=) v) inputs_outputs in
348
  let defs, vars = 
349
    List.fold_left (normalize_eq node) ([], inputs_outputs@node.node_locals) node.node_eqs in
350
  let new_locals = List.filter is_local vars in
351
  let node =
352
  { node with node_locals = new_locals; node_eqs = defs }
353
  in ((*Printers.pp_node Format.err_formatter node;*) node)
354

    
355
let normalize_decl decl =
356
  match decl.top_decl_desc with
357
  | Node nd ->
358
    {decl with top_decl_desc = Node (normalize_node nd)}
359
  | Open _ | ImportedNode _ | Consts _ -> decl
360
  
361
let normalize_prog decls = 
362
  List.map normalize_decl decls
363

    
364
(* Local Variables: *)
365
(* compile-command:"make -C .." *)
366
(* End: *)