Project

General

Profile

Statistics
| Branch: | Tag: | Revision:

lustrec / src / normalization.ml @ af5af1e8

History | View | Annotate | Download (16.5 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 unfold_arrow_active = ref true 
73
let cpt_fresh = ref 0
74

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

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

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

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

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

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

    
146
(* Create an alias for [expr], if [expr] is not already an alias (i.e. an ident)
147
   and [opt] is true *)
148
let mk_expr_alias_opt opt node defvars expr =
149
  match expr.expr_desc with
150
  | Expr_ident alias ->
151
    defvars, expr
152
  | _                -> 
153
    if opt
154
    then
155
      mk_expr_alias node defvars expr
156
    else
157
      defvars, expr
158

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

    
170
(* normalize_<foo> : defs * used vars -> <foo> -> (updated defs * updated vars) * normalized <foo> *)
171
let rec normalize_list alias node offsets norm_element defvars elist =
172
  List.fold_right
173
    (fun t (defvars, qlist) ->
174
      let defvars, norm_t = norm_element alias node offsets defvars t in
175
      (defvars, norm_t :: qlist)
176
    ) elist (defvars, [])
177

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

    
270
and normalize_array_expr ?(alias=true) node offsets defvars expr =
271
(*  Format.eprintf "normalize_array %B %a [%a]@." alias Printers.pp_expr expr (Utils.fprintf_list ~sep:"," Dimension.pp_dimension) offsets;*)
272
  match expr.expr_desc with
273
  | Expr_power (e1, d) when offsets = [] ->
274
    let defvars, norm_e1 = normalize_expr node offsets defvars e1 in
275
    defvars, mk_norm_expr offsets expr (Expr_power (norm_e1, d))
276
  | Expr_power (e1, d) ->
277
    normalize_array_expr ~alias:alias node (List.tl offsets) defvars e1
278
  | Expr_access (e1, d) -> normalize_array_expr ~alias:alias node (d::offsets) defvars e1
279
  | Expr_array elist when offsets = [] ->
280
    let defvars, norm_elist = normalize_list alias node offsets (fun _ -> normalize_array_expr ~alias:true) defvars elist in
281
    defvars, mk_norm_expr offsets expr (Expr_array norm_elist)
282
  | Expr_appl (id, args, None) when Basic_library.is_internal_fun id ->
283
    let defvars, norm_args = normalize_list alias node offsets (fun _ -> normalize_array_expr ~alias:true) defvars (expr_list_of_expr args) in
284
    defvars, mk_norm_expr offsets expr (Expr_appl (id, expr_of_expr_list args.expr_loc norm_args, None))
285
  |  _ -> normalize_expr ~alias:alias node offsets defvars expr
286

    
287
and normalize_cond_expr ?(alias=true) node offsets defvars expr =
288
  (*Format.eprintf "normalize_cond %B %a [%a]@." alias Printers.pp_expr expr (Utils.fprintf_list ~sep:"," Dimension.pp_dimension) offsets;*)
289
  match expr.expr_desc with
290
  | Expr_access (e1, d) ->
291
    normalize_cond_expr ~alias:alias node (d::offsets) defvars e1
292
  | Expr_ite (c, t, e) ->
293
    let defvars, norm_c = normalize_guard node defvars c in
294
    let defvars, norm_t = normalize_cond_expr node offsets defvars t in
295
    let defvars, norm_e = normalize_cond_expr node offsets defvars e in
296
    defvars, mk_norm_expr offsets expr (Expr_ite (norm_c, norm_t, norm_e))
297
  | Expr_merge (c, hl) ->
298
    let defvars, norm_hl = normalize_branches node offsets defvars hl in
299
    defvars, mk_norm_expr offsets expr (Expr_merge (c, norm_hl))
300
  | _ -> normalize_expr ~alias:alias node offsets defvars expr
301

    
302
and normalize_guard node defvars expr =
303
  let defvars, norm_expr = normalize_expr node [] defvars expr in
304
  mk_expr_alias_opt true node defvars norm_expr
305

    
306
(* outputs cannot be memories as well. If so, introduce new local variable.
307
*)
308
let decouple_outputs node defvars eq =
309
  let rec fold_lhs defvars lhs tys cks =
310
   match lhs, tys, cks with
311
   | [], [], []          -> defvars, []
312
   | v::qv, t::qt, c::qc -> let (defs_q, vars_q), lhs_q = fold_lhs defvars qv qt qc in
313
			    if List.exists (fun o -> o.var_id = v) node.node_outputs
314
			    then
315
			      let newvar = mk_fresh_var node eq.eq_loc t c in
316
			      let neweq  = mkeq eq.eq_loc ([v], mk_ident_expr newvar) in
317
			      (neweq :: defs_q, newvar :: vars_q), newvar.var_id :: lhs_q
318
			    else
319
			      (defs_q, vars_q), v::lhs_q
320
   | _                   -> assert false in
321
  let defvars', lhs' =
322
    fold_lhs
323
      defvars
324
      eq.eq_lhs
325
      (Types.type_list_of_type eq.eq_rhs.expr_type)
326
      (Clocks.clock_list_of_clock eq.eq_rhs.expr_clock) in
327
  defvars', {eq with eq_lhs = lhs' }
328

    
329
let rec normalize_eq node defvars eq = 
330
  match eq.eq_rhs.expr_desc with
331
  | Expr_pre _
332
  | Expr_fby _  ->
333
    let (defvars', eq') = decouple_outputs node defvars eq in
334
    let (defs', vars'), norm_rhs = normalize_expr ~alias:false node [] defvars' eq'.eq_rhs in
335
    let norm_eq = { eq' with eq_rhs = norm_rhs } in
336
    (norm_eq::defs', vars')
337
  | Expr_array _ ->
338
    let (defs', vars'), norm_rhs = normalize_array_expr ~alias:false node [] defvars eq.eq_rhs in
339
    let norm_eq = { eq with eq_rhs = norm_rhs } in
340
    (norm_eq::defs', vars')
341
  | Expr_appl (id, _, None) when Basic_library.is_internal_fun id && Types.is_array_type eq.eq_rhs.expr_type ->
342
    let (defs', vars'), norm_rhs = normalize_array_expr ~alias:false node [] defvars eq.eq_rhs in
343
    let norm_eq = { eq with eq_rhs = norm_rhs } in
344
    (norm_eq::defs', vars')
345
  | Expr_appl _ ->
346
    let (defs', vars'), norm_rhs = normalize_expr ~alias:false node [] defvars eq.eq_rhs in
347
    let norm_eq = { eq with eq_rhs = norm_rhs } in
348
    (norm_eq::defs', vars')
349
  | _ ->
350
    let (defs', vars'), norm_rhs = normalize_cond_expr ~alias:false node [] defvars eq.eq_rhs in
351
    let norm_eq = { eq with eq_rhs = norm_rhs } in
352
    norm_eq::defs', vars'
353

    
354
(** normalize_node node returns a normalized node, 
355
    ie. 
356
    - updated locals
357
    - new equations
358
    - 
359
*)
360
let normalize_node node = 
361
  cpt_fresh := 0;
362
  let inputs_outputs = node.node_inputs@node.node_outputs in
363
  let is_local v =
364
    List.for_all ((!=) v) inputs_outputs in
365
  let orig_vars = inputs_outputs@node.node_locals in
366
  let defs, vars = 
367
    List.fold_left (normalize_eq node) ([], orig_vars) node.node_eqs in
368
  (* Normalize the asserts *)
369
  let vars, assert_defs, asserts = 
370
    List.fold_left (
371
    fun (vars, def_accu, assert_accu) assert_ ->
372
      let assert_expr = assert_.assert_expr in
373
      let (defs, vars'), expr = 
374
	normalize_expr 
375
	  ~alias:false 
376
	  node 
377
	  [] (* empty offset for arrays *)
378
	  ([], vars) (* defvar only contains vars *)
379
	  assert_expr
380
      in
381
      vars', defs@def_accu, {assert_ with assert_expr = expr}::assert_accu
382
    ) (vars, [], []) node.node_asserts in
383
  let new_locals = List.filter is_local vars in
384
  (* Compute tracebaility info: 
385
     - gather newly bound variables
386
     - compute the associated expression without aliases     
387
  *)
388
  let diff_vars = List.filter (fun v -> not (List.mem v node.node_locals) ) new_locals in
389
  let norm_traceability = {
390
    annots = 
391
      List.map 
392
	(fun v -> 
393
	  let expr = substitute_expr diff_vars defs (
394
	    let eq = List.find (fun eq -> eq.eq_lhs = [v.var_id]) defs in
395
	    eq.eq_rhs) in 
396
	  let pair = mkeexpr expr.expr_loc (mkexpr expr.expr_loc (Expr_tuple [expr_of_ident v.var_id expr.expr_loc; expr])) in 
397
	  ["horn_backend";"trace"], pair 
398
    ) 
399
    diff_vars ;
400
    annot_loc = Location.dummy_loc
401
  }
402

    
403
  in
404
  let node =
405
  { node with 
406
    node_locals = new_locals; 
407
    node_eqs = defs @ assert_defs;
408
    node_asserts = asserts;
409
    node_annot = norm_traceability::node.node_annot;
410
  }
411
  in ((*Printers.pp_node Format.err_formatter node;*) node)
412

    
413
let normalize_decl decl =
414
  match decl.top_decl_desc with
415
  | Node nd ->
416
    {decl with top_decl_desc = Node (normalize_node nd)}
417
  | Open _ | ImportedNode _ | Consts _ -> decl
418
  
419
let normalize_prog decls = 
420
  List.map normalize_decl decls
421

    
422
(* Local Variables: *)
423
(* compile-command:"make -C .." *)
424
(* End: *)