lustrec / src / backends / C / c_backend_header.ml @ 86ae18b7
History | View | Annotate | Download (16.9 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 Format |
13 |
open LustreSpec |
14 |
open Corelang |
15 |
open Machine_code |
16 |
open C_backend_common |
17 |
|
18 |
(********************************************************************************************) |
19 |
(* Header Printing functions *) |
20 |
(********************************************************************************************) |
21 |
|
22 |
|
23 |
module type MODIFIERS_HDR = |
24 |
sig |
25 |
val print_machine_decl_prefix: Format.formatter -> Machine_code.machine_t -> unit |
26 |
end |
27 |
|
28 |
module EmptyMod = |
29 |
struct |
30 |
let print_machine_decl_prefix = fun fmt x -> () |
31 |
end |
32 |
|
33 |
module Main = functor (Mod: MODIFIERS_HDR) -> |
34 |
struct |
35 |
|
36 |
let print_import_standard fmt = |
37 |
begin |
38 |
if !Options.mpfr then |
39 |
begin |
40 |
fprintf fmt "#include <mpfr.h>@." |
41 |
end; |
42 |
(*fprintf fmt "#include \"%s/arrow.h\"@.@." Version.include_path*) |
43 |
fprintf fmt "#include \"%s/arrow.h\"@.@." !Options.include_dir |
44 |
end |
45 |
|
46 |
let rec print_static_val pp_var fmt v = |
47 |
match v.value_desc with |
48 |
| Cst c -> pp_c_const fmt c |
49 |
| LocalVar v -> pp_var fmt v |
50 |
| Fun (n, vl) -> Basic_library.pp_c n (print_static_val pp_var) fmt vl |
51 |
| _ -> (Format.eprintf "Internal error: C_backend_header.print_static_val"; assert false) |
52 |
|
53 |
let print_constant_decl (m, attr, inst) pp_var fmt v = |
54 |
Format.fprintf fmt "%s %a = %a" |
55 |
attr |
56 |
(pp_c_type (Format.sprintf "%s ## %s" inst v.var_id)) v.var_type |
57 |
(print_static_val pp_var) (Machine_code.get_const_assign m v) |
58 |
|
59 |
let print_static_constant_decl (m, attr, inst) fmt const_locals = |
60 |
let pp_var fmt v = |
61 |
if List.mem v const_locals |
62 |
then |
63 |
Format.fprintf fmt "%s ## %s" inst v.var_id |
64 |
else |
65 |
Format.fprintf fmt "%s" v.var_id in |
66 |
Format.fprintf fmt "%a%t" |
67 |
(Utils.fprintf_list ~sep:";\\@," (print_constant_decl (m, attr, inst) pp_var)) const_locals |
68 |
(Utils.pp_final_char_if_non_empty ";\\@," const_locals) |
69 |
|
70 |
let print_static_declare_instance (m, attr, inst) const_locals fmt (i, (n, static)) = |
71 |
let pp_var fmt v = |
72 |
if List.mem v const_locals |
73 |
then |
74 |
Format.fprintf fmt "%s ## %s" inst v.var_id |
75 |
else |
76 |
Format.fprintf fmt "%s" v.var_id in |
77 |
let values = List.map (Machine_code.value_of_dimension m) static in |
78 |
fprintf fmt "%a(%s, %a%t%s)" |
79 |
pp_machine_static_declare_name (node_name n) |
80 |
attr |
81 |
(Utils.fprintf_list ~sep:", " (print_static_val pp_var)) values |
82 |
(Utils.pp_final_char_if_non_empty ", " static) |
83 |
i |
84 |
|
85 |
let print_static_declare_macro fmt (m, attr, inst) = |
86 |
let const_locals = List.filter (fun vdecl -> vdecl.var_dec_const) m.mstep.step_locals in |
87 |
let array_mem = List.filter (fun v -> Types.is_array_type v.var_type) m.mmemory in |
88 |
fprintf fmt "@[<v 2>#define %a(%s, %a%t%s)\\@,%a%s %a %s;\\@,%a%t%a;@,@]" |
89 |
pp_machine_static_declare_name m.mname.node_id |
90 |
attr |
91 |
(Utils.fprintf_list ~sep:", " (pp_c_var_read m)) m.mstatic |
92 |
(Utils.pp_final_char_if_non_empty ", " m.mstatic) |
93 |
inst |
94 |
(* constants *) |
95 |
(print_static_constant_decl (m, attr, inst)) const_locals |
96 |
attr |
97 |
pp_machine_memtype_name m.mname.node_id |
98 |
inst |
99 |
(Utils.fprintf_list ~sep:";\\@," (pp_c_decl_local_var m)) array_mem |
100 |
(Utils.pp_final_char_if_non_empty ";\\@," array_mem) |
101 |
(Utils.fprintf_list ~sep:";\\@," |
102 |
(fun fmt (i',m') -> |
103 |
let path = sprintf "%s ## _%s" inst i' in |
104 |
fprintf fmt "%a" |
105 |
(print_static_declare_instance (m, attr, inst) const_locals) (path, m') |
106 |
)) m.minstances |
107 |
|
108 |
|
109 |
let print_static_link_instance fmt (i, (m, _)) = |
110 |
fprintf fmt "%a(%s)" pp_machine_static_link_name (node_name m) i |
111 |
|
112 |
(* Allocation of a node struct: |
113 |
- if node memory is an array/matrix/etc, we cast it to a pointer (see pp_registers_struct) |
114 |
*) |
115 |
let print_static_link_macro fmt (m, attr, inst) = |
116 |
let array_mem = List.filter (fun v -> Types.is_array_type v.var_type) m.mmemory in |
117 |
fprintf fmt "@[<v>@[<v 2>#define %a(%s) do {\\@,%a%t%a;\\@]@,} while (0)@.@]" |
118 |
pp_machine_static_link_name m.mname.node_id |
119 |
inst |
120 |
(Utils.fprintf_list ~sep:";\\@," |
121 |
(fun fmt v -> |
122 |
fprintf fmt "%s._reg.%s = (%a*) &%s" |
123 |
inst |
124 |
v.var_id |
125 |
(fun fmt v -> pp_c_type "" fmt (Types.array_base_type v.var_type)) v |
126 |
v.var_id |
127 |
)) array_mem |
128 |
(Utils.pp_final_char_if_non_empty ";\\@," array_mem) |
129 |
(Utils.fprintf_list ~sep:";\\@," |
130 |
(fun fmt (i',m') -> |
131 |
let path = sprintf "%s ## _%s" inst i' in |
132 |
fprintf fmt "%a;\\@,%s.%s = &%s" |
133 |
print_static_link_instance (path,m') |
134 |
inst |
135 |
i' |
136 |
path |
137 |
)) m.minstances |
138 |
|
139 |
let print_static_alloc_macro fmt (m, attr, inst) = |
140 |
fprintf fmt "@[<v>@[<v 2>#define %a(%s, %a%t%s)\\@,%a(%s, %a%t%s);\\@,%a(%s);@]@,@]@." |
141 |
pp_machine_static_alloc_name m.mname.node_id |
142 |
attr |
143 |
(Utils.fprintf_list ~sep:", " (pp_c_var_read m)) m.mstatic |
144 |
(Utils.pp_final_char_if_non_empty ", " m.mstatic) |
145 |
inst |
146 |
pp_machine_static_declare_name m.mname.node_id |
147 |
attr |
148 |
(Utils.fprintf_list ~sep:", " (pp_c_var_read m)) m.mstatic |
149 |
(Utils.pp_final_char_if_non_empty ", " m.mstatic) |
150 |
inst |
151 |
pp_machine_static_link_name m.mname.node_id |
152 |
inst |
153 |
|
154 |
let print_machine_decl fmt m = |
155 |
begin |
156 |
Mod.print_machine_decl_prefix fmt m; |
157 |
if fst (get_stateless_status m) then |
158 |
begin |
159 |
fprintf fmt "extern %a;@.@." |
160 |
print_stateless_prototype |
161 |
(m.mname.node_id, m.mstep.step_inputs, m.mstep.step_outputs) |
162 |
end |
163 |
else |
164 |
begin |
165 |
(* Static allocation *) |
166 |
if !Options.static_mem |
167 |
then |
168 |
begin |
169 |
let inst = mk_instance m in |
170 |
let attr = mk_attribute m in |
171 |
fprintf fmt "%a@.%a@.%a@." |
172 |
print_static_declare_macro (m, attr, inst) |
173 |
print_static_link_macro (m, attr, inst) |
174 |
print_static_alloc_macro (m, attr, inst) |
175 |
end |
176 |
else |
177 |
begin |
178 |
(* Dynamic allocation *) |
179 |
fprintf fmt "extern %a;@.@." |
180 |
print_alloc_prototype (m.mname.node_id, m.mstatic) |
181 |
end; |
182 |
let self = mk_self m in |
183 |
fprintf fmt "extern %a;@.@." |
184 |
(print_reset_prototype self) (m.mname.node_id, m.mstatic); |
185 |
|
186 |
fprintf fmt "extern %a;@.@." |
187 |
(print_step_prototype self) |
188 |
(m.mname.node_id, m.mstep.step_inputs, m.mstep.step_outputs); |
189 |
|
190 |
if !Options.mpfr then |
191 |
begin |
192 |
fprintf fmt "extern %a;@.@." |
193 |
(print_init_prototype self) (m.mname.node_id, m.mstatic); |
194 |
|
195 |
fprintf fmt "extern %a;@.@." |
196 |
(print_clear_prototype self) (m.mname.node_id, m.mstatic); |
197 |
end |
198 |
end |
199 |
end |
200 |
|
201 |
let print_machine_alloc_decl fmt m = |
202 |
Mod.print_machine_decl_prefix fmt m; |
203 |
if fst (get_stateless_status m) then |
204 |
begin |
205 |
end |
206 |
else |
207 |
begin |
208 |
if !Options.static_mem |
209 |
then |
210 |
begin |
211 |
(* Static allocation *) |
212 |
let inst = mk_instance m in |
213 |
let attr = mk_attribute m in |
214 |
fprintf fmt "%a@.%a@.%a@." |
215 |
print_static_declare_macro (m, attr, inst) |
216 |
print_static_link_macro (m, attr, inst) |
217 |
print_static_alloc_macro (m, attr, inst) |
218 |
end |
219 |
else |
220 |
begin |
221 |
(* Dynamic allocation *) |
222 |
fprintf fmt "extern %a;@." |
223 |
print_alloc_prototype (m.mname.node_id, m.mstatic) |
224 |
end |
225 |
end |
226 |
|
227 |
let print_machine_decl_from_header fmt inode = |
228 |
(*Mod.print_machine_decl_prefix fmt m;*) |
229 |
if inode.nodei_prototype = Some "C" then |
230 |
if inode.nodei_stateless then |
231 |
begin |
232 |
fprintf fmt "extern %a;@.@." |
233 |
print_stateless_C_prototype |
234 |
(inode.nodei_id, inode.nodei_inputs, inode.nodei_outputs) |
235 |
end |
236 |
else (Format.eprintf "internal error: print_machine_decl_from_header"; assert false) |
237 |
else |
238 |
if inode.nodei_stateless then |
239 |
begin |
240 |
fprintf fmt "extern %a;@.@." |
241 |
print_stateless_prototype |
242 |
(inode.nodei_id, inode.nodei_inputs, inode.nodei_outputs) |
243 |
end |
244 |
else |
245 |
begin |
246 |
let static_inputs = List.filter (fun v -> v.var_dec_const) inode.nodei_inputs in |
247 |
let used name = |
248 |
(List.exists (fun v -> v.var_id = name) inode.nodei_inputs) |
249 |
|| (List.exists (fun v -> v.var_id = name) inode.nodei_outputs) in |
250 |
let self = mk_new_name used "self" in |
251 |
fprintf fmt "extern %a;@.@." |
252 |
(print_reset_prototype self) (inode.nodei_id, static_inputs); |
253 |
|
254 |
fprintf fmt "extern %a;@.@." |
255 |
(print_init_prototype self) (inode.nodei_id, static_inputs); |
256 |
|
257 |
fprintf fmt "extern %a;@.@." |
258 |
(print_clear_prototype self) (inode.nodei_id, static_inputs); |
259 |
|
260 |
fprintf fmt "extern %a;@.@." |
261 |
(print_step_prototype self) |
262 |
(inode.nodei_id, inode.nodei_inputs, inode.nodei_outputs) |
263 |
end |
264 |
|
265 |
let print_const_decl fmt cdecl = |
266 |
if !Options.mpfr && Types.is_real_type (Types.array_base_type cdecl.const_type) |
267 |
then |
268 |
fprintf fmt "extern %a;@." |
269 |
(pp_c_type cdecl.const_id) (Types.dynamic_type cdecl.const_type) |
270 |
else |
271 |
fprintf fmt "extern %a;@." |
272 |
(pp_c_type cdecl.const_id) cdecl.const_type |
273 |
|
274 |
let rec pp_c_struct_type_field filename cpt fmt (label, tdesc) = |
275 |
fprintf fmt "%a;" (pp_c_type_decl filename cpt label) tdesc |
276 |
and pp_c_type_decl filename cpt var fmt tdecl = |
277 |
match tdecl with |
278 |
| Tydec_any -> assert false |
279 |
| Tydec_int -> fprintf fmt "int %s" var |
280 |
| Tydec_real when !Options.mpfr |
281 |
-> fprintf fmt "%s %s" Mpfr.mpfr_t var |
282 |
| Tydec_real -> fprintf fmt "double %s" var |
283 |
(* | Tydec_float -> fprintf fmt "float %s" var *) |
284 |
| Tydec_bool -> fprintf fmt "_Bool %s" var |
285 |
| Tydec_clock ty -> pp_c_type_decl filename cpt var fmt ty |
286 |
| Tydec_const c -> fprintf fmt "%s %s" c var |
287 |
| Tydec_array (d, ty) -> fprintf fmt "%a[%a]" (pp_c_type_decl filename cpt var) ty pp_c_dimension d |
288 |
| Tydec_enum tl -> |
289 |
begin |
290 |
incr cpt; |
291 |
fprintf fmt "enum _enum_%s_%d { %a } %s" filename !cpt (Utils.fprintf_list ~sep:", " pp_print_string) tl var |
292 |
end |
293 |
| Tydec_struct fl -> |
294 |
begin |
295 |
incr cpt; |
296 |
fprintf fmt "struct _struct_%s_%d { %a } %s" filename !cpt (Utils.fprintf_list ~sep:" " (pp_c_struct_type_field filename cpt)) fl var |
297 |
end |
298 |
|
299 |
let print_type_definitions fmt filename = |
300 |
let cpt_type = ref 0 in |
301 |
Hashtbl.iter (fun typ decl -> |
302 |
match typ with |
303 |
| Tydec_const var -> |
304 |
(match decl.top_decl_desc with |
305 |
| TypeDef tdef -> |
306 |
fprintf fmt "typedef %a;@.@." |
307 |
(pp_c_type_decl filename cpt_type var) tdef.tydef_desc |
308 |
| _ -> assert false) |
309 |
| _ -> ()) type_table |
310 |
|
311 |
let reset_type_definitions, print_type_definition_from_header = |
312 |
let cpt_type =ref 0 in |
313 |
((fun () -> cpt_type := 0), |
314 |
(fun fmt typ filename -> |
315 |
fprintf fmt "typedef %a;@.@." |
316 |
(pp_c_type_decl filename cpt_type typ.tydef_id) typ.tydef_desc)) |
317 |
|
318 |
(********************************************************************************************) |
319 |
(* MAIN Header Printing functions *) |
320 |
(********************************************************************************************) |
321 |
let print_header header_fmt basename prog machines dependencies = |
322 |
(* Include once: start *) |
323 |
let baseNAME = file_to_module_name basename in |
324 |
begin |
325 |
(* Print the version number and the supported C standard (C90 or C99) *) |
326 |
print_version header_fmt; |
327 |
fprintf header_fmt "#ifndef _%s@.#define _%s@." baseNAME baseNAME; |
328 |
pp_print_newline header_fmt (); |
329 |
fprintf header_fmt "/* Imports standard library */@."; |
330 |
(* imports standard library definitions (arrow) *) |
331 |
print_import_standard header_fmt; |
332 |
pp_print_newline header_fmt (); |
333 |
(* imports dependencies *) |
334 |
fprintf header_fmt "/* Import dependencies */@."; |
335 |
fprintf header_fmt "@[<v>"; |
336 |
List.iter (print_import_prototype header_fmt) dependencies; |
337 |
fprintf header_fmt "@]@."; |
338 |
fprintf header_fmt "/* Types definitions */@."; |
339 |
(* Print the type definitions from the type table *) |
340 |
print_type_definitions header_fmt basename; |
341 |
pp_print_newline header_fmt (); |
342 |
(* Print the global constant declarations. *) |
343 |
fprintf header_fmt "/* Global constant (declarations, definitions are in C file) */@."; |
344 |
List.iter (fun c -> print_const_decl header_fmt (const_of_top c)) (get_consts prog); |
345 |
pp_print_newline header_fmt (); |
346 |
if !Options.mpfr then |
347 |
begin |
348 |
fprintf header_fmt "/* Global initialization declaration */@."; |
349 |
fprintf header_fmt "extern %a;@.@." |
350 |
print_global_init_prototype baseNAME; |
351 |
|
352 |
fprintf header_fmt "/* Global clear declaration */@."; |
353 |
fprintf header_fmt "extern %a;@.@." |
354 |
print_global_clear_prototype baseNAME; |
355 |
end; |
356 |
(* Print the struct declarations of all machines. *) |
357 |
fprintf header_fmt "/* Structs declarations */@."; |
358 |
List.iter (print_machine_struct header_fmt) machines; |
359 |
pp_print_newline header_fmt (); |
360 |
(* Print the prototypes of all machines *) |
361 |
fprintf header_fmt "/* Nodes declarations */@."; |
362 |
List.iter (print_machine_decl header_fmt) machines; |
363 |
pp_print_newline header_fmt (); |
364 |
(* Include once: end *) |
365 |
fprintf header_fmt "#endif@."; |
366 |
pp_print_newline header_fmt () |
367 |
end |
368 |
|
369 |
let print_alloc_header header_fmt basename prog machines dependencies = |
370 |
(* Include once: start *) |
371 |
let baseNAME = file_to_module_name basename in |
372 |
begin |
373 |
(* Print the svn version number and the supported C standard (C90 or C99) *) |
374 |
print_version header_fmt; |
375 |
fprintf header_fmt "#ifndef _%s_alloc@.#define _%s_alloc@." baseNAME baseNAME; |
376 |
pp_print_newline header_fmt (); |
377 |
(* Import the header *) |
378 |
fprintf header_fmt "/* Import header from %s */@." basename; |
379 |
fprintf header_fmt "@[<v>"; |
380 |
print_import_prototype header_fmt (Dep (true, basename, [], true (* assuming it is staful *) )); |
381 |
fprintf header_fmt "@]@."; |
382 |
fprintf header_fmt "/* Import dependencies */@."; |
383 |
fprintf header_fmt "@[<v>"; |
384 |
List.iter (print_import_alloc_prototype header_fmt) dependencies; |
385 |
fprintf header_fmt "@]@."; |
386 |
(* Print the struct definitions of all machines. *) |
387 |
fprintf header_fmt "/* Struct definitions */@."; |
388 |
List.iter (print_machine_struct header_fmt) machines; |
389 |
pp_print_newline header_fmt (); |
390 |
(* Print the prototypes of all machines *) |
391 |
fprintf header_fmt "/* Node allocation function/macro prototypes */@."; |
392 |
List.iter (print_machine_alloc_decl header_fmt) machines; |
393 |
pp_print_newline header_fmt (); |
394 |
(* Include once: end *) |
395 |
fprintf header_fmt "#endif@."; |
396 |
pp_print_newline header_fmt () |
397 |
end |
398 |
|
399 |
(* Function called when compiling a lusi file and generating the associated C |
400 |
header. *) |
401 |
let print_header_from_header header_fmt basename header = |
402 |
(* Include once: start *) |
403 |
let baseNAME = file_to_module_name basename in |
404 |
let types = get_typedefs header in |
405 |
let consts = get_consts header in |
406 |
let nodes = get_imported_nodes header in |
407 |
let dependencies = get_dependencies header in |
408 |
begin |
409 |
(* Print the version number and the supported C standard (C90 or C99) *) |
410 |
print_version header_fmt; |
411 |
fprintf header_fmt "#ifndef _%s@.#define _%s@." baseNAME baseNAME; |
412 |
pp_print_newline header_fmt (); |
413 |
fprintf header_fmt "/* Imports standard library */@."; |
414 |
(* imports standard library definitions (arrow) *) |
415 |
print_import_standard header_fmt; |
416 |
pp_print_newline header_fmt (); |
417 |
(* imports dependencies *) |
418 |
fprintf header_fmt "/* Import dependencies */@."; |
419 |
fprintf header_fmt "@[<v>"; |
420 |
List.iter |
421 |
(fun dep -> |
422 |
let (local, s) = dependency_of_top dep in |
423 |
print_import_prototype header_fmt (Dep (local, s, [], true (* assuming it is stateful *)))) |
424 |
dependencies; |
425 |
fprintf header_fmt "@]@."; |
426 |
fprintf header_fmt "/* Types definitions */@."; |
427 |
(* Print the type definitions from the type table *) |
428 |
reset_type_definitions (); |
429 |
List.iter (fun typ -> print_type_definition_from_header header_fmt (typedef_of_top typ) basename) types; |
430 |
pp_print_newline header_fmt (); |
431 |
(* Print the global constant declarations. *) |
432 |
fprintf header_fmt "/* Global constant (declarations, definitions are in C file) */@."; |
433 |
List.iter (fun c -> print_const_decl header_fmt (const_of_top c)) consts; |
434 |
pp_print_newline header_fmt (); |
435 |
if !Options.mpfr then |
436 |
begin |
437 |
fprintf header_fmt "/* Global initialization declaration */@."; |
438 |
fprintf header_fmt "extern %a;@.@." |
439 |
print_global_init_prototype baseNAME; |
440 |
|
441 |
fprintf header_fmt "/* Global clear declaration */@."; |
442 |
fprintf header_fmt "extern %a;@.@." |
443 |
print_global_clear_prototype baseNAME; |
444 |
end; |
445 |
(* Print the struct declarations of all machines. *) |
446 |
fprintf header_fmt "/* Structs declarations */@."; |
447 |
List.iter (fun node -> print_machine_struct_from_header header_fmt (imported_node_of_top node)) nodes; |
448 |
pp_print_newline header_fmt (); |
449 |
(* Print the prototypes of all machines *) |
450 |
fprintf header_fmt "/* Nodes declarations */@."; |
451 |
List.iter (fun node -> print_machine_decl_from_header header_fmt (imported_node_of_top node)) nodes; |
452 |
pp_print_newline header_fmt (); |
453 |
(* Include once: end *) |
454 |
fprintf header_fmt "#endif@."; |
455 |
pp_print_newline header_fmt () |
456 |
end |
457 |
|
458 |
end |
459 |
(* Local Variables: *) |
460 |
(* compile-command:"make -C ../../.." *) |
461 |
(* End: *) |