lustrec / src / main_lustre_compiler.ml @ bc7b6c62
History | View | Annotate | Download (11.7 KB)
1 | 70e1006b | xthirioux | (********************************************************************) |
---|---|---|---|
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 Log |
||
14 | |||
15 | open LustreSpec |
||
16 | open Compiler_common |
||
17 | |||
18 | let usage = "Usage: lustrec [options] <source-file>" |
||
19 | |||
20 | let extensions = [".ec"; ".lus"; ".lusi"] |
||
21 | |||
22 | (* print a .lusi header file from a source prog *) |
||
23 | let print_lusi prog basename extension = |
||
24 | let header = Lusic.extract_header basename prog in |
||
25 | let header_name = basename ^ extension in |
||
26 | let h_out = open_out header_name in |
||
27 | let h_fmt = formatter_of_out_channel h_out in |
||
28 | begin |
||
29 | Printers.pp_lusi_header h_fmt basename header; |
||
30 | close_out h_out |
||
31 | end |
||
32 | |||
33 | (* compile a .lusi header file *) |
||
34 | let compile_header basename extension = |
||
35 | 3f823d04 | xthirioux | let destname = !Options.dest_dir ^ "/" ^ basename in |
36 | let header_name = basename ^ extension in |
||
37 | let lusic_ext = extension ^ "c" in |
||
38 | begin |
||
39 | Log.report ~level:1 (fun fmt -> fprintf fmt "@[<v>"); |
||
40 | let header = parse_header true header_name in |
||
41 | ignore (check_top_decls header); |
||
42 | create_dest_dir (); |
||
43 | Log.report ~level:1 (fun fmt -> fprintf fmt ".. generating compiled header file %sc@," header_name); |
||
44 | Lusic.write_lusic true header destname lusic_ext; |
||
45 | Lusic.print_lusic_to_h destname lusic_ext; |
||
46 | Log.report ~level:1 (fun fmt -> fprintf fmt ".. done !@ @]@.") |
||
47 | end |
||
48 | 70e1006b | xthirioux | |
49 | (* check whether a source file has a compiled header, |
||
50 | if not, generate the compiled header *) |
||
51 | let compile_source_to_header prog computed_types_env computed_clocks_env basename extension = |
||
52 | 3f823d04 | xthirioux | let destname = !Options.dest_dir ^ "/" ^ basename in |
53 | 70e1006b | xthirioux | let lusic_ext = extension ^ "c" in |
54 | 3f823d04 | xthirioux | let header_name = destname ^ lusic_ext in |
55 | 70e1006b | xthirioux | begin |
56 | if not (Sys.file_exists header_name) then |
||
57 | begin |
||
58 | Log.report ~level:1 (fun fmt -> fprintf fmt ".. generating compiled header file %s@," header_name); |
||
59 | 3f823d04 | xthirioux | Lusic.write_lusic false (Lusic.extract_header basename prog) destname lusic_ext; |
60 | Lusic.print_lusic_to_h destname lusic_ext |
||
61 | 70e1006b | xthirioux | end |
62 | else |
||
63 | 3f823d04 | xthirioux | let lusic = Lusic.read_lusic destname lusic_ext in |
64 | 70e1006b | xthirioux | if not lusic.Lusic.from_lusi then |
65 | begin |
||
66 | Log.report ~level:1 (fun fmt -> fprintf fmt ".. generating compiled header file %s@," header_name); |
||
67 | 3f823d04 | xthirioux | Lusic.write_lusic false (Lusic.extract_header basename prog) destname lusic_ext; |
68 | 70e1006b | xthirioux | (*List.iter (fun top_decl -> Format.eprintf "lusic: %a@." Printers.pp_decl top_decl) lusic.Lusic.contents;*) |
69 | 3f823d04 | xthirioux | Lusic.print_lusic_to_h destname lusic_ext |
70 | 70e1006b | xthirioux | end |
71 | else |
||
72 | begin |
||
73 | Log.report ~level:1 (fun fmt -> fprintf fmt ".. loading compiled header file %s@," header_name); |
||
74 | let header = lusic.Lusic.contents in |
||
75 | let (declared_types_env, declared_clocks_env) = get_envs_from_top_decls header in |
||
76 | check_compatibility |
||
77 | (prog, computed_types_env, computed_clocks_env) |
||
78 | (header, declared_types_env, declared_clocks_env) |
||
79 | end |
||
80 | end |
||
81 | |||
82 | (* compile a .lus source file *) |
||
83 | let rec compile_source basename extension = |
||
84 | let source_name = basename ^ extension in |
||
85 | |||
86 | Log.report ~level:1 (fun fmt -> fprintf fmt "@[<v>"); |
||
87 | |||
88 | let prog = parse_source source_name in |
||
89 | |||
90 | (* Extracting dependencies *) |
||
91 | let dependencies, type_env, clock_env = import_dependencies prog in |
||
92 | bc7b6c62 | xthirioux | |
93 | (* Removing automata *) |
||
94 | (*let prog = Automata.expand_decls prog in*) |
||
95 | |||
96 | (*Printers.pp_prog Format.std_formatter prog;*) |
||
97 | 70e1006b | xthirioux | (* Sorting nodes *) |
98 | let prog = SortProg.sort prog in |
||
99 | |||
100 | (* Typing *) |
||
101 | let computed_types_env = type_decls type_env prog in |
||
102 | |||
103 | (* Clock calculus *) |
||
104 | let computed_clocks_env = clock_decls clock_env prog in |
||
105 | |||
106 | (* Checking stateless/stateful status *) |
||
107 | check_stateless_decls prog; |
||
108 | |||
109 | (* Generating a .lusi header file only *) |
||
110 | if !Options.lusi then |
||
111 | begin |
||
112 | let lusi_ext = extension ^ "i" in |
||
113 | Log.report ~level:1 (fun fmt -> fprintf fmt ".. generating interface file %s@," (basename ^ lusi_ext)); |
||
114 | print_lusi prog basename lusi_ext; |
||
115 | Log.report ~level:1 (fun fmt -> fprintf fmt ".. done !@ @]@."); |
||
116 | exit 0 |
||
117 | end; |
||
118 | |||
119 | (* Perform global inlining *) |
||
120 | let prog = |
||
121 | if !Options.global_inline && |
||
122 | (match !Options.main_node with | "" -> false | _ -> true) then |
||
123 | Inliner.global_inline basename prog type_env clock_env |
||
124 | else |
||
125 | prog |
||
126 | in |
||
127 | |||
128 | (* Delay calculus *) |
||
129 | (* |
||
130 | if(!Options.delay_calculus) |
||
131 | then |
||
132 | begin |
||
133 | Log.report ~level:1 (fun fmt -> fprintf fmt ".. initialisation analysis@?"); |
||
134 | try |
||
135 | Delay_calculus.delay_prog Basic_library.delay_env prog |
||
136 | with (Delay.Error (loc,err)) as exc -> |
||
137 | Location.print loc; |
||
138 | eprintf "%a" Delay.pp_error err; |
||
139 | Utils.track_exception (); |
||
140 | raise exc |
||
141 | end; |
||
142 | *) |
||
143 | (* |
||
144 | eprintf "Causality analysis@.@?"; |
||
145 | (* Causality analysis *) |
||
146 | begin |
||
147 | try |
||
148 | Causality.check_causal_prog prog |
||
149 | with (Causality.Cycle v) as exc -> |
||
150 | Causality.pp_error err_formatter v; |
||
151 | raise exc |
||
152 | end; |
||
153 | *) |
||
154 | |||
155 | (* Creating destination directory if needed *) |
||
156 | create_dest_dir (); |
||
157 | |||
158 | (* Compatibility with Lusi *) |
||
159 | (* Checking the existence of a lusi (Lustre Interface file) *) |
||
160 | let extension = ".lusi" in |
||
161 | compile_source_to_header prog computed_types_env computed_clocks_env basename extension; |
||
162 | |||
163 | Typing.uneval_prog_generics prog; |
||
164 | Clock_calculus.uneval_prog_generics prog; |
||
165 | |||
166 | (* Computes and stores generic calls for each node, |
||
167 | only useful for ANSI C90 compliant generic node compilation *) |
||
168 | if !Options.ansi then Causality.NodeDep.compute_generic_calls prog; |
||
169 | (*Hashtbl.iter (fun id td -> match td.Corelang.top_decl_desc with Corelang.Node nd -> Format.eprintf "%s calls %a" id Causality.NodeDep.pp_generic_calls nd | _ -> ()) Corelang.node_table;*) |
||
170 | |||
171 | (* Normalization phase *) |
||
172 | Log.report ~level:1 (fun fmt -> fprintf fmt ".. normalization@,"); |
||
173 | (* Special treatment of arrows in lustre backend. We want to keep them *) |
||
174 | if !Options.output = "lustre" then |
||
175 | Normalization.unfold_arrow_active := false; |
||
176 | let prog = Normalization.normalize_prog prog in |
||
177 | Log.report ~level:2 (fun fmt -> fprintf fmt "@[<v 2>@ %a@]@," Printers.pp_prog prog); |
||
178 | |||
179 | (* Checking array accesses *) |
||
180 | if !Options.check then |
||
181 | begin |
||
182 | Log.report ~level:1 (fun fmt -> fprintf fmt ".. array access checks@,"); |
||
183 | Access.check_prog prog; |
||
184 | end; |
||
185 | |||
186 | (* Computation of node equation scheduling. It also breaks dependency cycles |
||
187 | and warns about unused input or memory variables *) |
||
188 | Log.report ~level:1 (fun fmt -> fprintf fmt ".. scheduling@,"); |
||
189 | let prog, node_schs = Scheduling.schedule_prog prog in |
||
190 | Log.report ~level:3 (fun fmt -> fprintf fmt "@[<v 2>@ %a@]@," Scheduling.pp_warning_unused node_schs); |
||
191 | Log.report ~level:3 (fun fmt -> fprintf fmt "@[<v 2>@ %a@]@," Scheduling.pp_schedule node_schs); |
||
192 | Log.report ~level:3 (fun fmt -> fprintf fmt "@[<v 2>@ %a@]@," Scheduling.pp_fanin_table node_schs); |
||
193 | Log.report ~level:3 (fun fmt -> fprintf fmt "@[<v 2>@ %a@]@," Printers.pp_prog prog); |
||
194 | |||
195 | (* Optimization of prog: |
||
196 | - Unfold consts |
||
197 | - eliminate trivial expressions |
||
198 | *) |
||
199 | let prog = |
||
200 | if !Options.optimization >= 2 then |
||
201 | Optimize_prog.prog_unfold_consts prog |
||
202 | else |
||
203 | prog |
||
204 | in |
||
205 | |||
206 | (* DFS with modular code generation *) |
||
207 | Log.report ~level:1 (fun fmt -> fprintf fmt ".. machines generation@,"); |
||
208 | let machine_code = Machine_code.translate_prog prog node_schs in |
||
209 | |||
210 | (* Optimize machine code *) |
||
211 | let machine_code = |
||
212 | if !Options.optimization >= 3 && !Options.output <> "horn" then |
||
213 | begin |
||
214 | Log.report ~level:1 (fun fmt -> fprintf fmt ".. machines optimization@,"); |
||
215 | Optimize_machine.machines_reuse_variables machine_code node_schs |
||
216 | end |
||
217 | else |
||
218 | machine_code |
||
219 | in |
||
220 | let machine_code = |
||
221 | if !Options.optimization >= 3 && !Options.output <> "horn" then |
||
222 | begin |
||
223 | Optimize_machine.machines_fusion machine_code |
||
224 | end |
||
225 | else |
||
226 | machine_code |
||
227 | in |
||
228 | Log.report ~level:3 (fun fmt -> fprintf fmt "@[<v 2>@ %a@]@," |
||
229 | (Utils.fprintf_list ~sep:"@ " Machine_code.pp_machine) |
||
230 | machine_code); |
||
231 | |||
232 | (* Printing code *) |
||
233 | let basename = Filename.basename basename in |
||
234 | let destname = !Options.dest_dir ^ "/" ^ basename in |
||
235 | let _ = match !Options.output with |
||
236 | "C" -> |
||
237 | begin |
||
238 | let alloc_header_file = destname ^ "_alloc.h" in (* Could be changed *) |
||
239 | let source_lib_file = destname ^ ".c" in (* Could be changed *) |
||
240 | let source_main_file = destname ^ "_main.c" in (* Could be changed *) |
||
241 | let makefile_file = destname ^ ".makefile" in (* Could be changed *) |
||
242 | Log.report ~level:1 (fun fmt -> fprintf fmt ".. C code generation@,"); |
||
243 | C_backend.translate_to_c |
||
244 | alloc_header_file source_lib_file source_main_file makefile_file |
||
245 | basename prog machine_code dependencies |
||
246 | end |
||
247 | | "java" -> |
||
248 | begin |
||
249 | failwith "Sorry, but not yet supported !" |
||
250 | (*let source_file = basename ^ ".java" in |
||
251 | Log.report ~level:1 (fun fmt -> fprintf fmt ".. opening file %s@,@?" source_file); |
||
252 | let source_out = open_out source_file in |
||
253 | let source_fmt = formatter_of_out_channel source_out in |
||
254 | Log.report ~level:1 (fun fmt -> fprintf fmt ".. java code generation@,@?"); |
||
255 | Java_backend.translate_to_java source_fmt basename normalized_prog machine_code;*) |
||
256 | end |
||
257 | | "horn" -> |
||
258 | begin |
||
259 | let source_file = destname ^ ".smt2" in (* Could be changed *) |
||
260 | let source_out = open_out source_file in |
||
261 | let fmt = formatter_of_out_channel source_out in |
||
262 | Horn_backend.translate fmt basename prog machine_code; |
||
263 | (* Tracability file if option is activated *) |
||
264 | if !Options.horntraces then ( |
||
265 | let traces_file = destname ^ ".traces" in (* Could be changed *) |
||
266 | let traces_out = open_out traces_file in |
||
267 | let fmt = formatter_of_out_channel traces_out in |
||
268 | Horn_backend.traces_file fmt basename prog machine_code |
||
269 | ) |
||
270 | end |
||
271 | | "lustre" -> |
||
272 | begin |
||
273 | let source_file = destname ^ ".lustrec.lus" in (* Could be changed *) |
||
274 | let source_out = open_out source_file in |
||
275 | let fmt = formatter_of_out_channel source_out in |
||
276 | Printers.pp_prog fmt prog; |
||
277 | (* Lustre_backend.translate fmt basename normalized_prog machine_code *) |
||
278 | () |
||
279 | end |
||
280 | |||
281 | | _ -> assert false |
||
282 | in |
||
283 | begin |
||
284 | Log.report ~level:1 (fun fmt -> fprintf fmt ".. done !@ @]@."); |
||
285 | (* We stop the process here *) |
||
286 | exit 0 |
||
287 | end |
||
288 | |||
289 | let compile basename extension = |
||
290 | match extension with |
||
291 | | ".lusi" -> compile_header basename extension |
||
292 | | ".lus" -> compile_source basename extension |
||
293 | | _ -> assert false |
||
294 | |||
295 | let anonymous filename = |
||
296 | let ok_ext, ext = List.fold_left (fun (ok, ext) ext' -> if not ok && Filename.check_suffix filename ext' then true, ext' else ok, ext) (false, "") extensions in |
||
297 | if ok_ext then |
||
298 | 3f823d04 | xthirioux | let basename = Filename.chop_suffix (Filename.basename filename) ext in |
299 | 70e1006b | xthirioux | compile basename ext |
300 | else |
||
301 | raise (Arg.Bad ("Can only compile *.lusi, *.lus or *.ec files")) |
||
302 | |||
303 | let _ = |
||
304 | Corelang.add_internal_funs (); |
||
305 | try |
||
306 | Printexc.record_backtrace true; |
||
307 | Arg.parse Options.options anonymous usage |
||
308 | with |
||
309 | | Parse.Syntax_err _ | Lexer_lustre.Error _ |
||
310 | | Types.Error (_,_) | Clocks.Error (_,_) |
||
311 | | Corelang.Error _ (*| Task_set.Error _*) |
||
312 | | Causality.Cycle _ -> exit 1 |
||
313 | | Sys_error msg -> (eprintf "Failure: %s@." msg) |
||
314 | | exc -> (Utils.track_exception (); raise exc) |
||
315 | |||
316 | (* Local Variables: *) |
||
317 | (* compile-command:"make -C .." *) |
||
318 | (* End: *) |