lustrec / src / machine_code.ml @ 70be4acf
History  View  Annotate  Download (14.7 KB)
1 
(********************************************************************) 

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 
open Lustre_types 
13 
open Machine_code_types 
14 
open Machine_code_common 
15 
open Corelang 
16 
open Clocks 
17 
open Causality 
18 

19 
exception NormalizationError 
20  
21  
22 

23 
(* translate_<foo> : node > context > <foo> > machine code/expression *) 
24 
(* the context contains m : state aka memory variables *) 
25 
(* si : initialization instructions *) 
26 
(* j : node aka machine instances *) 
27 
(* d : local variables *) 
28 
(* s : step instructions *) 
29 
let translate_ident node (m, si, j, d, s) id = 
30 
(* Format.eprintf "trnaslating ident: %s@." id; *) 
31 
try (* id is a node var *) 
32 
let var_id = get_node_var id node in 
33 
mk_val (Var var_id) var_id.var_type 
34 
with Not_found > 
35 
try (* id is a constant *) 
36 
let vdecl = (Corelang.var_decl_of_const (const_of_top (Hashtbl.find Corelang.consts_table id))) in 
37 
mk_val (Var vdecl) vdecl.var_type 
38 
with Not_found > 
39 
(* id is a tag *) 
40 
(* DONE construire une liste des enum declarés et alors chercher dedans la liste 
41 
qui contient id *) 
42 
try 
43 
let typ = (typedef_of_top (Hashtbl.find Corelang.tag_table id)).tydef_id in 
44 
mk_val (Cst (Const_tag id)) (Type_predef.type_const typ) 
45 
with Not_found > (Format.eprintf "internal error: Machine_code.translate_ident %s" id; 
46 
assert false) 
47  
48 
let rec control_on_clock node ((m, si, j, d, s) as args) ck inst = 
49 
match (Clocks.repr ck).cdesc with 
50 
 Con (ck1, cr, l) > 
51 
let id = Clocks.const_of_carrier cr in 
52 
control_on_clock node args ck1 (mkinstr 
53 
(* TODO il faudrait prendre le lustre 
54 
associé à instr et rajouter print_ck_suffix 
55 
ck) de clocks.ml *) 
56 
(MBranch (translate_ident node args id, 
57 
[l, [inst]] ))) 
58 
 _ > inst 
59  
60  
61 
(* specialize predefined (polymorphic) operators 
62 
wrt their instances, so that the C semantics 
63 
is preserved *) 
64 
let specialize_to_c expr = 
65 
match expr.expr_desc with 
66 
 Expr_appl (id, e, r) > 
67 
if List.exists (fun e > Types.is_bool_type e.expr_type) (expr_list_of_expr e) 
68 
then let id = 
69 
match id with 
70 
 "=" > "equi" 
71 
 "!=" > "xor" 
72 
 _ > id in 
73 
{ expr with expr_desc = Expr_appl (id, e, r) } 
74 
else expr 
75 
 _ > expr 
76  
77 
let specialize_op expr = 
78 
match !Options.output with 
79 
 "C" > specialize_to_c expr 
80 
 _ > expr 
81  
82 
let rec translate_expr node ((m, si, j, d, s) as args) expr = 
83 
let expr = specialize_op expr in 
84 
let value_desc = 
85 
match expr.expr_desc with 
86 
 Expr_const v > Cst v 
87 
 Expr_ident x > (translate_ident node args x).value_desc 
88 
 Expr_array el > Array (List.map (translate_expr node args) el) 
89 
 Expr_access (t, i) > Access (translate_expr node args t, translate_expr node args (expr_of_dimension i)) 
90 
 Expr_power (e, n) > Power (translate_expr node args e, translate_expr node args (expr_of_dimension n)) 
91 
 Expr_tuple _ 
92 
 Expr_arrow _ 
93 
 Expr_fby _ 
94 
 Expr_pre _ > (Printers.pp_expr Format.err_formatter expr; Format.pp_print_flush Format.err_formatter (); raise NormalizationError) 
95 
 Expr_when (e1, _, _) > (translate_expr node args e1).value_desc 
96 
 Expr_merge (x, _) > raise NormalizationError 
97 
 Expr_appl (id, e, _) when Basic_library.is_expr_internal_fun expr > 
98 
let nd = node_from_name id in 
99 
Fun (node_name nd, List.map (translate_expr node args) (expr_list_of_expr e)) 
100 
 Expr_ite (g,t,e) > ( 
101 
(* special treatment depending on the active backend. For horn backend, ite 
102 
are preserved in expression. While they are removed for C or Java 
103 
backends. *) 
104 
match !Options.output with 
105 
 "horn" > 
106 
Fun ("ite", [translate_expr node args g; translate_expr node args t; translate_expr node args e]) 
107 
 "C"  "java"  _ > 
108 
(Format.eprintf "Normalization error for backend %s: %a@." 
109 
!Options.output 
110 
Printers.pp_expr expr; 
111 
raise NormalizationError) 
112 
) 
113 
 _ > raise NormalizationError 
114 
in 
115 
mk_val value_desc expr.expr_type 
116  
117 
let translate_guard node args expr = 
118 
match expr.expr_desc with 
119 
 Expr_ident x > translate_ident node args x 
120 
 _ > (Format.eprintf "internal error: translate_guard %s %a@." node.node_id Printers.pp_expr expr;assert false) 
121  
122 
let rec translate_act node ((m, si, j, d, s) as args) (y, expr) = 
123 
let eq = Corelang.mkeq Location.dummy_loc ([y.var_id], expr) in 
124 
match expr.expr_desc with 
125 
 Expr_ite (c, t, e) > let g = translate_guard node args c in 
126 
mk_conditional ?lustre_eq:(Some eq) g 
127 
[translate_act node args (y, t)] 
128 
[translate_act node args (y, e)] 
129 
 Expr_merge (x, hl) > mkinstr ?lustre_eq:(Some eq) (MBranch (translate_ident node args x, 
130 
List.map (fun (t, h) > t, [translate_act node args (y, h)]) hl)) 
131 
 _ > mkinstr ?lustre_eq:(Some eq) (MLocalAssign (y, translate_expr node args expr)) 
132  
133 
let reset_instance node args i r c = 
134 
match r with 
135 
 None > [] 
136 
 Some r > let g = translate_guard node args r in 
137 
[control_on_clock node args c (mk_conditional g [mkinstr (MReset i)] [mkinstr (MNoReset i)])] 
138  
139 
let translate_eq node ((m, si, j, d, s) as args) eq = 
140 
(* Format.eprintf "translate_eq %a with clock %a@." Printers.pp_node_eq eq Clocks.print_ck eq.eq_rhs.expr_clock; *) 
141 
match eq.eq_lhs, eq.eq_rhs.expr_desc with 
142 
 [x], Expr_arrow (e1, e2) > 
143 
let var_x = get_node_var x node in 
144 
let o = new_instance node Arrow.arrow_top_decl eq.eq_rhs.expr_tag in 
145 
let c1 = translate_expr node args e1 in 
146 
let c2 = translate_expr node args e2 in 
147 
(m, 
148 
mkinstr (MReset o) :: si, 
149 
Utils.IMap.add o (Arrow.arrow_top_decl, []) j, 
150 
d, 
151 
(control_on_clock node args eq.eq_rhs.expr_clock (mkinstr ?lustre_eq:(Some eq) (MStep ([var_x], o, [c1;c2])))) :: s) 
152 
 [x], Expr_pre e1 when VSet.mem (get_node_var x node) d > 
153 
let var_x = get_node_var x node in 
154 
(VSet.add var_x m, 
155 
si, 
156 
j, 
157 
d, 
158 
control_on_clock node args eq.eq_rhs.expr_clock (mkinstr ?lustre_eq:(Some eq) (MStateAssign (var_x, translate_expr node args e1))) :: s) 
159 
 [x], Expr_fby (e1, e2) when VSet.mem (get_node_var x node) d > 
160 
let var_x = get_node_var x node in 
161 
(VSet.add var_x m, 
162 
mkinstr ?lustre_eq:(Some eq) (MStateAssign (var_x, translate_expr node args e1)) :: si, 
163 
j, 
164 
d, 
165 
control_on_clock node args eq.eq_rhs.expr_clock (mkinstr ?lustre_eq:(Some eq) (MStateAssign (var_x, translate_expr node args e2))) :: s) 
166  
167 
 p , Expr_appl (f, arg, r) when not (Basic_library.is_expr_internal_fun eq.eq_rhs) > 
168 
let var_p = List.map (fun v > get_node_var v node) p in 
169 
let el = expr_list_of_expr arg in 
170 
let vl = List.map (translate_expr node args) el in 
171 
let node_f = node_from_name f in 
172 
let call_f = 
173 
node_f, 
174 
NodeDep.filter_static_inputs (node_inputs node_f) el in 
175 
let o = new_instance node node_f eq.eq_rhs.expr_tag in 
176 
let env_cks = List.fold_right (fun arg cks > arg.expr_clock :: cks) el [eq.eq_rhs.expr_clock] in 
177 
let call_ck = Clock_calculus.compute_root_clock (Clock_predef.ck_tuple env_cks) in 
178 
(*Clocks.new_var true in 
179 
Clock_calculus.unify_imported_clock (Some call_ck) eq.eq_rhs.expr_clock eq.eq_rhs.expr_loc; 
180 
Format.eprintf "call %a: %a: %a@," Printers.pp_expr eq.eq_rhs Clocks.print_ck (Clock_predef.ck_tuple env_cks) Clocks.print_ck call_ck;*) 
181 
(m, 
182 
(if Stateless.check_node node_f then si else mkinstr (MReset o) :: si), 
183 
Utils.IMap.add o call_f j, 
184 
d, 
185 
(if Stateless.check_node node_f 
186 
then [] 
187 
else reset_instance node args o r call_ck) @ 
188 
(control_on_clock node args call_ck (mkinstr ?lustre_eq:(Some eq) (MStep (var_p, o, vl)))) :: s) 
189 
(* 
190 
(* special treatment depending on the active backend. For horn backend, x = ite (g,t,e) 
191 
are preserved. While they are replaced as if g then x = t else x = e in C or Java 
192 
backends. *) 
193 
 [x], Expr_ite (c, t, e) 
194 
when (match !Options.output with  "horn" > true  "C"  "java"  _ > false) 
195 
> 
196 
let var_x = get_node_var x node in 
197 
(m, 
198 
si, 
199 
j, 
200 
d, 
201 
(control_on_clock node args eq.eq_rhs.expr_clock 
202 
(MLocalAssign (var_x, translate_expr node args eq.eq_rhs))::s) 
203 
) 
204  
205 
*) 
206 
 [x], _ > ( 
207 
let var_x = get_node_var x node in 
208 
(m, si, j, d, 
209 
control_on_clock 
210 
node 
211 
args 
212 
eq.eq_rhs.expr_clock 
213 
(translate_act node args (var_x, eq.eq_rhs)) :: s 
214 
) 
215 
) 
216 
 _ > 
217 
begin 
218 
Format.eprintf "internal error: Machine_code.translate_eq %a@?" Printers.pp_node_eq eq; 
219 
assert false 
220 
end 
221  
222 
let find_eq xl eqs = 
223 
let rec aux accu eqs = 
224 
match eqs with 
225 
 [] > 
226 
begin 
227 
Format.eprintf "Looking for variables %a in the following equations@.%a@." 
228 
(Utils.fprintf_list ~sep:" , " (fun fmt v > Format.fprintf fmt "%s" v)) xl 
229 
Printers.pp_node_eqs eqs; 
230 
assert false 
231 
end 
232 
 hd::tl > 
233 
if List.exists (fun x > List.mem x hd.eq_lhs) xl then hd, accu@tl else aux (hd::accu) tl 
234 
in 
235 
aux [] eqs 
236  
237 
(* Sort the set of equations of node [nd] according 
238 
to the computed schedule [sch] 
239 
*) 
240 
let sort_equations_from_schedule nd sch = 
241 
(* Format.eprintf "%s schedule: %a@." *) 
242 
(* nd.node_id *) 
243 
(* (Utils.fprintf_list ~sep:" ; " Scheduling.pp_eq_schedule) sch; *) 
244 
let eqs, auts = get_node_eqs nd in 
245 
assert (auts = []); (* Automata should be expanded by now *) 
246 
let split_eqs = Splitting.tuple_split_eq_list eqs in 
247 
let eqs_rev, remainder = 
248 
List.fold_left 
249 
(fun (accu, node_eqs_remainder) vl > 
250 
if List.exists (fun eq > List.exists (fun v > List.mem v eq.eq_lhs) vl) accu 
251 
then 
252 
(accu, node_eqs_remainder) 
253 
else 
254 
let eq_v, remainder = find_eq vl node_eqs_remainder in 
255 
eq_v::accu, remainder 
256 
) 
257 
([], split_eqs) 
258 
sch 
259 
in 
260 
begin 
261 
if List.length remainder > 0 then ( 
262 
let eqs, auts = get_node_eqs nd in 
263 
assert (auts = []); (* Automata should be expanded by now *) 
264 
Format.eprintf "Equations not used are@.%a@.Full equation set is:@.%a@.@?" 
265 
Printers.pp_node_eqs remainder 
266 
Printers.pp_node_eqs eqs; 
267 
assert false); 
268 
List.rev eqs_rev 
269 
end 
270  
271 
let constant_equations nd = 
272 
List.fold_right (fun vdecl eqs > 
273 
if vdecl.var_dec_const 
274 
then 
275 
{ eq_lhs = [vdecl.var_id]; 
276 
eq_rhs = Utils.desome vdecl.var_dec_value; 
277 
eq_loc = vdecl.var_loc 
278 
} :: eqs 
279 
else eqs) 
280 
nd.node_locals [] 
281  
282 
let translate_eqs node args eqs = 
283 
List.fold_right (fun eq args > translate_eq node args eq) eqs args;; 
284  
285 
let translate_decl nd sch = 
286 
(*Log.report ~level:1 (fun fmt > Printers.pp_node fmt nd);*) 
287  
288 
let sorted_eqs = sort_equations_from_schedule nd sch in 
289 
let constant_eqs = constant_equations nd in 
290  
291 
(* In case of non functional backend (eg. C), additional local variables have 
292 
to be declared for each assert *) 
293 
let new_locals, assert_instrs, nd_node_asserts = 
294 
let exprl = List.map (fun assert_ > assert_.assert_expr ) nd.node_asserts in 
295 
if Backends.is_functional () then 
296 
[], [], exprl 
297 
else (* Each assert(e) is associated to a fresh variable v and declared as 
298 
v=e; assert (v); *) 
299 
let _, vars, eql, assertl = 
300 
List.fold_left (fun (i, vars, eqlist, assertlist) expr > 
301 
let loc = expr.expr_loc in 
302 
let var_id = nd.node_id ^ "_assert_" ^ string_of_int i in 
303 
let assert_var = 
304 
mkvar_decl 
305 
loc 
306 
~orig:false (* fresh var *) 
307 
(var_id, 
308 
mktyp loc Tydec_bool, 
309 
mkclock loc Ckdec_any, 
310 
false, (* not a constant *) 
311 
None, (* no default value *) 
312 
Some nd.node_id 
313 
) 
314 
in 
315 
assert_var.var_type < Type_predef.type_bool (* Types.new_ty (Types.Tbool) *); 
316 
let eq = mkeq loc ([var_id], expr) in 
317 
(i+1, assert_var::vars, eq::eqlist, {expr with expr_desc = Expr_ident var_id}::assertlist) 
318 
) (1, [], [], []) exprl 
319 
in 
320 
vars, eql, assertl 
321 
in 
322 
let locals_list = nd.node_locals @ new_locals in 
323  
324 
let nd = { nd with node_locals = locals_list } in 
325 
let init_args = VSet.empty, [], Utils.IMap.empty, List.fold_right (fun l > VSet.add l) locals_list VSet.empty, [] in 
326 
(* memories, init instructions, node calls, local variables (including memories), step instrs *) 
327 
let m0, init0, j0, locals0, s0 = translate_eqs nd init_args constant_eqs in 
328 
assert (VSet.is_empty m0); 
329 
assert (init0 = []); 
330 
assert (Utils.IMap.is_empty j0); 
331 
let m, init, j, locals, s as context_with_asserts = translate_eqs nd (m0, init0, j0, locals0, []) (assert_instrs@sorted_eqs) in 
332 
let mmap = Utils.IMap.fold (fun i n res > (i, n)::res) j [] in 
333 
{ 
334 
mname = nd; 
335 
mmemory = VSet.elements m; 
336 
mcalls = mmap; 
337 
minstances = List.filter (fun (_, (n,_)) > not (Stateless.check_node n)) mmap; 
338 
minit = init; 
339 
mconst = s0; 
340 
mstatic = List.filter (fun v > v.var_dec_const) nd.node_inputs; 
341 
mstep = { 
342 
step_inputs = nd.node_inputs; 
343 
step_outputs = nd.node_outputs; 
344 
step_locals = VSet.elements (VSet.diff locals m); 
345 
step_checks = List.map (fun d > d.Dimension.dim_loc, translate_expr nd init_args (expr_of_dimension d)) nd.node_checks; 
346 
step_instrs = ( 
347 
(* special treatment depending on the active backend. For horn backend, 
348 
common branches are not merged while they are in C or Java 
349 
backends. *) 
350 
(*match !Options.output with 
351 
 "horn" > s 
352 
 "C"  "java"  _ >*) 
353 
if !Backends.join_guards then 
354 
join_guards_list s 
355 
else 
356 
s 
357 
); 
358 
step_asserts = List.map (translate_expr nd context_with_asserts) nd_node_asserts; 
359 
}; 
360 
mspec = nd.node_spec; 
361 
mannot = nd.node_annot; 
362 
} 
363  
364 
(** takes the global declarations and the scheduling associated to each node *) 
365 
let translate_prog decls node_schs = 
366 
let nodes = get_nodes decls in 
367 
List.map 
368 
(fun decl > 
369 
let node = node_of_top decl in 
370 
let sch = (Utils.IMap.find node.node_id node_schs).Scheduling.schedule in 
371 
translate_decl node sch 
372 
) nodes 
373  
374  
375  
376 
(* Local Variables: *) 
377 
(* compilecommand:"make C .." *) 
378 
(* End: *) 