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