Project

General

Profile

Statistics
| Branch: | Tag: | Revision:

lustrec / src / inliner.ml @ 01d48bb0

History | View | Annotate | Download (15.4 KB)

1 b38ffff3 ploc
(********************************************************************)
2
(*                                                                  *)
3
(*  The LustreC compiler toolset   /  The LustreC Development Team  *)
4
(*  Copyright 2012 -    --   ONERA - CNRS - INPT                    *)
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
(********************************************************************)
11
12 274c72ec ploc
open LustreSpec
13
open Corelang
14 6394042a ploc
open Utils
15
16
(* Local annotations are declared with the following key /inlining/: true *)
17
let keyword = ["inlining"]
18
19
let is_inline_expr expr = 
20
match expr.expr_annot with
21
| Some ann -> 
22
  List.exists (fun (key, value) -> key = keyword) ann.annots
23
| None -> false
24 274c72ec ploc
25
let check_node_name id = (fun t -> 
26
  match t.top_decl_desc with 
27
  | Node nd -> nd.node_id = id 
28
  | _ -> false) 
29
30 b3f91fdb xthirioux
let is_node_var node v =
31
 try
32
   ignore (Corelang.get_node_var v node); true
33
 with Not_found -> false
34 274c72ec ploc
35 36454535 ploc
let rename_expr rename expr = expr_replace_var rename expr
36 b3f91fdb xthirioux
37 36454535 ploc
let rename_eq rename eq = 
38
  { eq with
39
    eq_lhs = List.map rename eq.eq_lhs; 
40
    eq_rhs = rename_expr rename eq.eq_rhs
41
  }
42 01d48bb0 xthirioux
(*
43
let get_static_inputs input_arg_list =
44
 List.fold_right (fun (vdecl, arg) res ->
45
   if vdecl.var_dec_const
46
   then (vdecl.var_id, Corelang.dimension_of_expr arg) :: res
47
   else res)
48
   input_arg_list []
49
50
let get_carrier_inputs input_arg_list =
51
 List.fold_right (fun (vdecl, arg) res ->
52
   if Corelang.is_clock_dec_type vdecl.var_dec_type.ty_dec_desc
53
   then (vdecl.var_id, ident_of_expr arg) :: res
54
   else res)
55
   input_arg_list []
56
*)
57 274c72ec ploc
(* 
58 b3f91fdb xthirioux
    expr, locals', eqs = inline_call id args' reset locals node nodes
59 274c72ec ploc
60
We select the called node equations and variables.
61
   renamed_inputs = args
62
   renamed_eqs
63
64
the resulting expression is tuple_of_renamed_outputs
65
   
66
TODO: convert the specification/annotation/assert and inject them
67
TODO: deal with reset
68
*)
69 b3f91fdb xthirioux
let inline_call node orig_expr args reset locals caller =
70 274c72ec ploc
  let loc = orig_expr.expr_loc in
71
  let uid = orig_expr.expr_tag in
72 3f823d04 xthirioux
  let rename v =
73 b3f91fdb xthirioux
    if v = tag_true || v = tag_false || not (is_node_var node v) then v else
74
      Corelang.mk_new_node_name caller (Format.sprintf "%s_%i_%s" node.node_id uid v)
75 274c72ec ploc
  in
76 01d48bb0 xthirioux
  let eqs' = List.map (rename_eq rename) (get_node_eqs node) in
77
  let input_arg_list = List.combine node.node_inputs (Corelang.expr_list_of_expr args) in
78
  let static_inputs, dynamic_inputs = List.partition (fun (vdecl, arg) -> vdecl.var_dec_const) input_arg_list in
79
  let static_inputs = List.map (fun (vdecl, arg) -> vdecl, Corelang.dimension_of_expr arg) static_inputs in
80
  let carrier_inputs, other_inputs = List.partition (fun (vdecl, arg) -> Corelang.is_clock_dec_type vdecl.var_dec_type.ty_dec_desc) dynamic_inputs in
81
  let carrier_inputs = List.map (fun (vdecl, arg) -> vdecl, Corelang.ident_of_expr arg) carrier_inputs in
82 b3f91fdb xthirioux
  let rename_static v =
83
    try
84 01d48bb0 xthirioux
      snd (List.find (fun (vdecl, _) -> v = vdecl.var_id) static_inputs)
85
    with Not_found -> Dimension.mkdim_ident loc v in
86 b3f91fdb xthirioux
  let rename_carrier v =
87
    try
88 01d48bb0 xthirioux
      snd (List.find (fun (vdecl, _) -> v = vdecl.var_id) carrier_inputs)
89 b3f91fdb xthirioux
    with Not_found -> v in
90
  let rename_var v =
91 01d48bb0 xthirioux
    let vdecl =
92
      Corelang.mkvar_decl v.var_loc
93
	(rename v.var_id,
94
	 { v.var_dec_type  with ty_dec_desc = Corelang.rename_static rename_static v.var_dec_type.ty_dec_desc },
95
	 { v.var_dec_clock with ck_dec_desc = Corelang.rename_carrier rename_carrier v.var_dec_clock.ck_dec_desc },
96
	 v.var_dec_const,
97
	 Utils.option_map (rename_expr rename) v.var_dec_value) in
98
    begin
99
(*
100
      (try
101
	 Format.eprintf "Inliner.inline_call unify %a %a@." Types.print_ty vdecl.var_type Dimension.pp_dimension (List.assoc v.var_id static_inputs);
102
	 Typing.unify vdecl.var_type (Type_predef.type_static (List.assoc v.var_id static_inputs) (Types.new_var ()))
103
       with Not_found -> ());
104
      (try
105
Clock_calculus.unify vdecl.var_clock (Clock_predef.ck_carrier (List.assoc v.var_id carrier_inputs) (Clocks.new_var true))
106
       with Not_found -> ());
107
(*Format.eprintf "Inliner.inline_call res=%a@." Printers.pp_var vdecl;*)
108
*)
109
      vdecl
110
    end
111
    (*Format.eprintf "Inliner.rename_var %a@." Printers.pp_var v;*)
112
  in
113
  let inputs' = List.map (fun (vdecl, _) -> rename_var vdecl) dynamic_inputs in
114 274c72ec ploc
  let outputs' = List.map rename_var node.node_outputs in
115 01d48bb0 xthirioux
  let locals' =
116
      (List.map (fun (vdecl, arg) -> let vdecl' = rename_var vdecl in { vdecl' with var_dec_value = Some (Corelang.expr_of_dimension arg) }) static_inputs)
117
    @ (List.map rename_var node.node_locals) 
118
in
119 274c72ec ploc
  (* checking we are at the appropriate (early) step: node_checks and
120
     node_gencalls should be empty (not yet assigned) *)
121
  assert (node.node_checks = []);
122
  assert (node.node_gencalls = []);
123
124
  (* Bug included: todo deal with reset *)
125
  assert (reset = None);
126
127 01d48bb0 xthirioux
  let assign_inputs = mkeq loc (List.map (fun v -> v.var_id) inputs', expr_of_expr_list args.expr_loc (List.map snd dynamic_inputs)) in
128 b3f91fdb xthirioux
  let expr = expr_of_expr_list loc (List.map expr_of_vdecl outputs')
129 274c72ec ploc
  in
130 36454535 ploc
  let asserts' = (* We rename variables in assert expressions *)
131
    List.map 
132
      (fun a -> 
133
	{a with assert_expr = 
134
	    let expr = a.assert_expr in
135
	    rename_expr rename expr
136
      })
137
      node.node_asserts 
138
  in
139
  expr, 
140
  inputs'@outputs'@locals'@locals, 
141 2ac56807 xthirioux
  assign_inputs::eqs',
142 36454535 ploc
  asserts'
143 274c72ec ploc
144
145
146 b3f91fdb xthirioux
let inline_table = Hashtbl.create 23
147 274c72ec ploc
148
(* 
149 b3f91fdb xthirioux
   new_expr, new_locals, new_eqs = inline_expr expr locals node nodes
150 274c72ec ploc
   
151
   Each occurence of a node in nodes in the expr should be replaced by fresh
152
   variables and the code of called node instance added to new_eqs
153
154
*)
155 b3f91fdb xthirioux
let rec inline_expr ?(selection_on_annotation=false) expr locals node nodes =
156 6394042a ploc
  let inline_expr = inline_expr ~selection_on_annotation:selection_on_annotation in
157
  let inline_node = inline_node ~selection_on_annotation:selection_on_annotation in
158 274c72ec ploc
  let inline_tuple el = 
159 36454535 ploc
    List.fold_right (fun e (el_tail, locals, eqs, asserts) -> 
160 b3f91fdb xthirioux
      let e', locals', eqs', asserts' = inline_expr e locals node nodes in
161 36454535 ploc
      e'::el_tail, locals', eqs'@eqs, asserts@asserts'
162
    ) el ([], locals, [], [])
163 274c72ec ploc
  in
164
  let inline_pair e1 e2 = 
165 36454535 ploc
    let el', l', eqs', asserts' = inline_tuple [e1;e2] in
166 274c72ec ploc
    match el' with
167 36454535 ploc
    | [e1'; e2'] -> e1', e2', l', eqs', asserts'
168 274c72ec ploc
    | _ -> assert false
169
  in
170
  let inline_triple e1 e2 e3 = 
171 36454535 ploc
    let el', l', eqs', asserts' = inline_tuple [e1;e2;e3] in
172 274c72ec ploc
    match el' with
173 36454535 ploc
    | [e1'; e2'; e3'] -> e1', e2', e3', l', eqs', asserts'
174 274c72ec ploc
    | _ -> assert false
175
  in
176 6394042a ploc
  
177 274c72ec ploc
  match expr.expr_desc with
178
  | Expr_appl (id, args, reset) ->
179 b3f91fdb xthirioux
    let args', locals', eqs', asserts' = inline_expr args locals node nodes in 
180 6394042a ploc
    if List.exists (check_node_name id) nodes && (* the current node call is provided
181
						    as arguments nodes *)
182
      (not selection_on_annotation || is_inline_expr expr) (* and if selection on annotation is activated, 
183
							      it is explicitely inlined here *)
184
    then 
185 274c72ec ploc
      (* The node should be inlined *)
186 b3f91fdb xthirioux
      (* let _ =     Format.eprintf "Inlining call to %s@." id in *)
187
      let called = try List.find (check_node_name id) nodes 
188 274c72ec ploc
	with Not_found -> (assert false) in
189 b3f91fdb xthirioux
      let called = node_of_top called in
190
      let called' = inline_node called nodes in
191 36454535 ploc
      let expr, locals', eqs'', asserts'' = 
192 b3f91fdb xthirioux
	inline_call called' expr args' reset locals' node in
193 36454535 ploc
      expr, locals', eqs'@eqs'', asserts'@asserts''
194 274c72ec ploc
    else 
195
      (* let _ =     Format.eprintf "Not inlining call to %s@." id in *)
196 36454535 ploc
      { expr with expr_desc = Expr_appl(id, args', reset)}, 
197
      locals', 
198
      eqs', 
199
      asserts'
200 274c72ec ploc
201
  (* For other cases, we just keep the structure, but convert sub-expressions *)
202
  | Expr_const _ 
203 36454535 ploc
  | Expr_ident _ -> expr, locals, [], []
204 274c72ec ploc
  | Expr_tuple el -> 
205 36454535 ploc
    let el', l', eqs', asserts' = inline_tuple el in
206
    { expr with expr_desc = Expr_tuple el' }, l', eqs', asserts'
207 274c72ec ploc
  | Expr_ite (g, t, e) ->
208 36454535 ploc
    let g', t', e', l', eqs', asserts' = inline_triple g t e in
209
    { expr with expr_desc = Expr_ite (g', t', e') }, l', eqs', asserts'
210 274c72ec ploc
  | Expr_arrow (e1, e2) ->
211 36454535 ploc
    let e1', e2', l', eqs', asserts' = inline_pair e1 e2 in
212
    { expr with expr_desc = Expr_arrow (e1', e2') } , l', eqs', asserts'
213 274c72ec ploc
  | Expr_fby (e1, e2) ->
214 36454535 ploc
    let e1', e2', l', eqs', asserts' = inline_pair e1 e2 in
215
    { expr with expr_desc = Expr_fby (e1', e2') }, l', eqs', asserts'
216 274c72ec ploc
  | Expr_array el ->
217 36454535 ploc
    let el', l', eqs', asserts' = inline_tuple el in
218
    { expr with expr_desc = Expr_array el' }, l', eqs', asserts'
219 274c72ec ploc
  | Expr_access (e, dim) ->
220 b3f91fdb xthirioux
    let e', l', eqs', asserts' = inline_expr e locals node nodes in 
221 36454535 ploc
    { expr with expr_desc = Expr_access (e', dim) }, l', eqs', asserts'
222 274c72ec ploc
  | Expr_power (e, dim) ->
223 b3f91fdb xthirioux
    let e', l', eqs', asserts' = inline_expr e locals node nodes in 
224 36454535 ploc
    { expr with expr_desc = Expr_power (e', dim) }, l', eqs', asserts'
225 274c72ec ploc
  | Expr_pre e ->
226 b3f91fdb xthirioux
    let e', l', eqs', asserts' = inline_expr e locals node nodes in 
227 36454535 ploc
    { expr with expr_desc = Expr_pre e' }, l', eqs', asserts'
228 274c72ec ploc
  | Expr_when (e, id, label) ->
229 b3f91fdb xthirioux
    let e', l', eqs', asserts' = inline_expr e locals node nodes in 
230 36454535 ploc
    { expr with expr_desc = Expr_when (e', id, label) }, l', eqs', asserts'
231 274c72ec ploc
  | Expr_merge (id, branches) ->
232 36454535 ploc
    let el, l', eqs', asserts' = inline_tuple (List.map snd branches) in
233 274c72ec ploc
    let branches' = List.map2 (fun (label, _) v -> label, v) branches el in
234 36454535 ploc
    { expr with expr_desc = Expr_merge (id, branches') }, l', eqs', asserts'
235 b3f91fdb xthirioux
236
and inline_node ?(selection_on_annotation=false) node nodes =
237 01d48bb0 xthirioux
  try copy_node (Hashtbl.find inline_table node.node_id)
238 b3f91fdb xthirioux
  with Not_found ->
239 6394042a ploc
  let inline_expr = inline_expr ~selection_on_annotation:selection_on_annotation in
240 36454535 ploc
  let new_locals, eqs, asserts = 
241
    List.fold_left (fun (locals, eqs, asserts) eq ->
242
      let eq_rhs', locals', new_eqs', asserts' = 
243 b3f91fdb xthirioux
	inline_expr eq.eq_rhs locals node nodes 
244 274c72ec ploc
      in
245 36454535 ploc
      locals', { eq with eq_rhs = eq_rhs' }::new_eqs'@eqs, asserts'@asserts
246 b3f91fdb xthirioux
    ) (node.node_locals, [], node.node_asserts) (get_node_eqs node)
247 274c72ec ploc
  in
248 b3f91fdb xthirioux
  let inlined = 
249
  { node with
250 274c72ec ploc
    node_locals = new_locals;
251 1eda3e78 xthirioux
    node_stmts = List.map (fun eq -> Eq eq) eqs;
252 36454535 ploc
    node_asserts = asserts;
253 274c72ec ploc
  }
254 b3f91fdb xthirioux
  in
255
  begin
256
    (*Format.eprintf "inline node:<< %a@.>>@." Printers.pp_node inlined;*)
257
    Hashtbl.add inline_table node.node_id inlined;
258
    inlined
259
  end
260 274c72ec ploc
261
let inline_all_calls node nodes =
262
  let nd = match node.top_decl_desc with Node nd -> nd | _ -> assert false in
263
  { node with top_decl_desc = Node (inline_node nd nodes) }
264
    
265
266
267
268
269 2842f7ca ploc
let witness filename main_name orig inlined type_env clock_env =
270 274c72ec ploc
  let loc = Location.dummy_loc in
271
  let rename_local_node nodes prefix id =
272
    if List.exists (check_node_name id) nodes then
273
      prefix ^ id 
274
    else
275
      id
276
  in
277
  let main_orig_node = match (List.find (check_node_name main_name) orig).top_decl_desc with
278
  Node nd -> nd | _ -> assert false in
279
  
280
  let orig_rename = rename_local_node orig "orig_" in
281
  let inlined_rename = rename_local_node inlined "inlined_" in
282
  let identity = (fun x -> x) in
283
  let is_node top = match top.top_decl_desc with Node _ -> true | _ -> false in
284
  let orig = rename_prog orig_rename identity identity orig in
285
  let inlined = rename_prog inlined_rename identity identity inlined in
286
  let nodes_origs, others = List.partition is_node orig in
287
  let nodes_inlined, _ = List.partition is_node inlined in
288
289 9f8c4c1d ploc
  (* One ok_i boolean variable  per output var *)
290
  let nb_outputs = List.length main_orig_node.node_outputs in
291 01d48bb0 xthirioux
  let ok_ident = "OK" in
292 9f8c4c1d ploc
  let ok_i = List.map (fun id ->
293
    mkvar_decl 
294
      loc 
295 01d48bb0 xthirioux
      (Format.sprintf "%s_%i" ok_ident id,
296 9f8c4c1d ploc
       {ty_dec_desc=Tydec_bool; ty_dec_loc=loc},
297
       {ck_dec_desc=Ckdec_any; ck_dec_loc=loc},
298 01d48bb0 xthirioux
       false,
299
       None)
300 9f8c4c1d ploc
  ) (Utils.enumerate nb_outputs) 
301
  in
302
303
  (* OK = ok_1 and ok_2 and ... ok_n-1 *)
304 274c72ec ploc
  let ok_output = mkvar_decl 
305
    loc 
306
    (ok_ident,
307
     {ty_dec_desc=Tydec_bool; ty_dec_loc=loc},
308
     {ck_dec_desc=Ckdec_any; ck_dec_loc=loc},
309 01d48bb0 xthirioux
     false,
310
     None)
311 274c72ec ploc
  in
312 9f8c4c1d ploc
  let main_ok_expr =
313
    let mkv x = mkexpr loc (Expr_ident x) in
314
    match ok_i with
315
    | [] -> assert false
316
    | [x] -> mkv x.var_id 
317
    | hd::tl -> 
318
      List.fold_left (fun accu elem -> 
319
	mkpredef_call loc "&&" [mkv elem.var_id; accu]
320
      ) (mkv hd.var_id) tl
321
  in
322
323
  (* Building main node *)
324
325 1eda3e78 xthirioux
  let ok_i_eq =
326
    { eq_loc = loc;
327
      eq_lhs = List.map (fun v -> v.var_id) ok_i;
328
      eq_rhs = 
329
	let inputs = expr_of_expr_list  loc (List.map (fun v -> mkexpr loc (Expr_ident v.var_id)) main_orig_node.node_inputs) in
330
	let call_orig = 
331
	  mkexpr loc (Expr_appl ("orig_" ^ main_name, inputs, None)) in
332
	let call_inlined = 
333
	  mkexpr loc (Expr_appl ("inlined_" ^ main_name, inputs, None)) in
334
	let args = mkexpr loc (Expr_tuple [call_orig; call_inlined]) in 
335
	mkexpr loc (Expr_appl ("=", args, None))
336
    } in
337
  let ok_eq =
338
    { eq_loc = loc;
339
      eq_lhs = [ok_ident];
340
      eq_rhs = main_ok_expr;
341
    } in
342 274c72ec ploc
  let main_node = {
343
    node_id = "check";
344
    node_type = Types.new_var ();
345
    node_clock = Clocks.new_var true;
346
    node_inputs = main_orig_node.node_inputs;
347
    node_outputs = [ok_output];
348 3f823d04 xthirioux
    node_locals = ok_i;
349 274c72ec ploc
    node_gencalls = [];
350
    node_checks = [];
351
    node_asserts = [];
352 1eda3e78 xthirioux
    node_stmts = [Eq ok_i_eq; Eq ok_eq];
353 316af809 ploc
    node_dec_stateless = false;
354 d3e4c22f xthirioux
    node_stateless = None;
355 274c72ec ploc
    node_spec = Some 
356
      {requires = []; 
357 0038002e ploc
       ensures = [mkeexpr loc (mkexpr loc (Expr_ident ok_ident))];
358
       behaviors = [];
359
       spec_loc = loc
360 274c72ec ploc
      };
361 0038002e ploc
    node_annot = [];
362 274c72ec ploc
  }
363
  in
364 70e1006b xthirioux
  let main = [{ top_decl_desc = Node main_node; top_decl_loc = loc; top_decl_owner = filename; top_decl_itf = false }] in
365 274c72ec ploc
  let new_prog = others@nodes_origs@nodes_inlined@main in
366 2842f7ca ploc
  let witness_file = (Options.get_witness_dir filename) ^ "/" ^ "inliner_witness.lus" in
367 274c72ec ploc
  let witness_out = open_out witness_file in
368
  let witness_fmt = Format.formatter_of_out_channel witness_out in
369 01d48bb0 xthirioux
  begin
370
    List.iter (fun vdecl -> Typing.try_unify Type_predef.type_bool vdecl.var_type vdecl.var_loc) (ok_output::ok_i);
371
    Format.fprintf witness_fmt
372
      "(* Generated lustre file to check validity of inlining process *)@.";
373
    Printers.pp_prog witness_fmt new_prog;
374
    Format.fprintf witness_fmt "@.";
375
    ()
376
  end (* xx *)
377 274c72ec ploc
378 2842f7ca ploc
let global_inline basename prog type_env clock_env =
379 274c72ec ploc
  (* We select the main node desc *)
380
  let main_node, other_nodes, other_tops = 
381 01d48bb0 xthirioux
    List.fold_right
382
      (fun top (main_opt, nodes, others) -> 
383 274c72ec ploc
	match top.top_decl_desc with 
384
	| Node nd when nd.node_id = !Options.main_node -> 
385
	  Some top, nodes, others
386
	| Node _ -> main_opt, top::nodes, others
387
	| _ -> main_opt, nodes, top::others) 
388 01d48bb0 xthirioux
      prog (None, [], []) 
389 274c72ec ploc
  in
390 01d48bb0 xthirioux
391 274c72ec ploc
  (* Recursively each call of a node in the top node is replaced *)
392
  let main_node = Utils.desome main_node in
393
  let main_node' = inline_all_calls main_node other_nodes in
394 01d48bb0 xthirioux
  let res = List.map (fun top -> if check_node_name !Options.main_node top then main_node' else top) prog in
395 274c72ec ploc
  res
396 9f8c4c1d ploc
397 6394042a ploc
let local_inline basename prog type_env clock_env =
398
  let local_anns = Annotations.get_expr_annotations keyword in
399
  if local_anns != [] then (
400
    let nodes_with_anns = List.fold_left (fun accu (k, _) -> ISet.add k accu) ISet.empty local_anns in
401
    ISet.iter (fun node_id -> Format.eprintf "Node %s has local expression annotations@." node_id) nodes_with_anns;
402
    List.fold_right (fun top accu -> 
403
      ( match top.top_decl_desc with
404
      | Node nd when ISet.mem nd.node_id nodes_with_anns ->
405
	{ top with top_decl_desc = Node (inline_node ~selection_on_annotation:true nd prog) }
406
      | _ -> top
407
      )::accu) prog []
408
    
409
)
410
 else
411
  prog
412
413
414 9f8c4c1d ploc
(* Local Variables: *)
415
(* compile-command:"make -C .." *)
416
(* End: *)