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