lustrec / src / compiler_stages.ml @ 684d39e7
History | View | Annotate | Download (11.6 KB)
1 |
open Format |
---|---|
2 |
open Utils |
3 |
open Compiler_common |
4 |
open Lustre_types |
5 |
|
6 |
exception StopPhase1 of program_t |
7 |
|
8 |
let dynamic_checks () = |
9 |
match !Options.output, !Options.spec with |
10 |
| "C", "C" -> true |
11 |
| _ -> false |
12 |
|
13 |
|
14 |
(* check whether a source file has a compiled header, if not, generate the |
15 |
compiled header *) |
16 |
let compile_source_to_header prog computed_types_env computed_clocks_env dirname basename extension = |
17 |
let destname = !Options.dest_dir ^ "/" ^ basename in |
18 |
let lusic_ext = ".lusic" in |
19 |
let header_name = destname ^ lusic_ext in |
20 |
begin |
21 |
if (* Generating the lusic file *) |
22 |
extension = ".lusi" (* because input is a lusi *) |
23 |
|| (extension = ".lus" && |
24 |
not (Sys.file_exists header_name)) |
25 |
(* or because it is a lus but not lusic exists *) |
26 |
|| (let lusic = Lusic.read_lusic destname lusic_ext in |
27 |
not lusic.Lusic.from_lusi) |
28 |
(* or the lusic exists but is not generated from a lusi, hence it |
29 |
has te be regenerated *) |
30 |
then |
31 |
begin |
32 |
Log.report ~level:1 (fun fmt -> fprintf fmt ".. generating compiled header file %s@," header_name); |
33 |
Lusic.write_lusic |
34 |
(extension = ".lusi") (* is it a lusi file ? *) |
35 |
(if extension = ".lusi" then prog else Lusic.extract_header dirname basename prog) |
36 |
destname |
37 |
lusic_ext; |
38 |
let _ = |
39 |
match !Options.output with |
40 |
| "C" -> C_backend_lusic.print_lusic_to_h destname lusic_ext |
41 |
| _ -> () |
42 |
in |
43 |
() |
44 |
end |
45 |
else (* Lusic exists and is usable. Checking compatibility *) |
46 |
begin |
47 |
Log.report ~level:1 (fun fmt -> fprintf fmt ".. loading compiled header file %s@," header_name); |
48 |
let lusic = Lusic.read_lusic destname lusic_ext in |
49 |
Lusic.check_obsolete lusic destname; |
50 |
let header = lusic.Lusic.contents in |
51 |
let (declared_types_env, declared_clocks_env) = Modules.get_envs_from_top_decls header in |
52 |
check_compatibility |
53 |
(prog, computed_types_env, computed_clocks_env) |
54 |
(header, declared_types_env, declared_clocks_env) |
55 |
end |
56 |
end |
57 |
|
58 |
|
59 |
(* From prog to prog *) |
60 |
let stage1 params prog dirname basename extension = |
61 |
(* Updating parent node information for variables *) |
62 |
Compiler_common.update_vdecl_parents_prog prog; |
63 |
|
64 |
(* Removing automata *) |
65 |
let prog = expand_automata prog in |
66 |
Log.report ~level:4 (fun fmt -> fprintf fmt ".. after automata expansion:@, @[<v 2>@,%a@]@ " Printers.pp_prog prog); |
67 |
|
68 |
(* Importing source *) |
69 |
let prog, dependencies, (typ_env, clk_env) = Modules.load ~is_header:(extension = ".lusi") prog in |
70 |
|
71 |
(* Registering types and clocks for future checks *) |
72 |
Global.type_env := Env.overwrite !Global.type_env typ_env; |
73 |
Global.clock_env := Env.overwrite !Global.clock_env clk_env; |
74 |
|
75 |
(* (\* Extracting dependencies (and updating Global.(type_env/clock_env) *\) |
76 |
* let dependencies = import_dependencies prog in *) |
77 |
|
78 |
(* Sorting nodes *) |
79 |
let prog = SortProg.sort prog in |
80 |
|
81 |
(* Perform inlining before any analysis *) |
82 |
let orig, prog = |
83 |
if !Options.global_inline && !Options.main_node <> "" then |
84 |
(if !Options.witnesses then prog else []), |
85 |
Inliner.global_inline basename prog |
86 |
else (* if !Option.has_local_inline *) |
87 |
[], |
88 |
Inliner.local_inline prog (* type_env clock_env *) |
89 |
in |
90 |
|
91 |
(* Checking stateless/stateful status *) |
92 |
if Plugins.check_force_stateful () then |
93 |
force_stateful_decls prog |
94 |
else |
95 |
check_stateless_decls prog; |
96 |
|
97 |
(* Typing *) |
98 |
Global.type_env := type_decls !Global.type_env prog; |
99 |
|
100 |
(* Clock calculus *) |
101 |
Global.clock_env := clock_decls !Global.clock_env prog; |
102 |
|
103 |
(* Registering and checking machine types *) |
104 |
if Machine_types.is_active then Machine_types.load prog; |
105 |
|
106 |
|
107 |
(* Generating a .lusi header file only *) |
108 |
if !Options.lusi then |
109 |
(* We stop here the processing and produce the current prog. It will be |
110 |
exported as a lusi *) |
111 |
raise (StopPhase1 prog); |
112 |
|
113 |
(* Optimization of prog: |
114 |
- Unfold consts |
115 |
- eliminate trivial expressions |
116 |
*) |
117 |
(* |
118 |
let prog = |
119 |
if !Options.const_unfold || !Options.optimization >= 5 then |
120 |
begin |
121 |
Log.report ~level:1 (fun fmt -> fprintf fmt ".. eliminating constants and aliases@,"); |
122 |
Optimize_prog.prog_unfold_consts prog |
123 |
end |
124 |
else |
125 |
prog |
126 |
in |
127 |
*) |
128 |
(* Delay calculus *) |
129 |
(* TO BE DONE LATER (Xavier) |
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 |
(* Creating destination directory if needed *) |
145 |
create_dest_dir (); |
146 |
|
147 |
Typing.uneval_prog_generics prog; |
148 |
Clock_calculus.uneval_prog_generics prog; |
149 |
|
150 |
|
151 |
(* Disabling witness option. Could but reactivated later |
152 |
if !Options.global_inline && !Options.main_node <> "" && !Options.witnesses then |
153 |
begin |
154 |
let orig = Corelang.copy_prog orig in |
155 |
Log.report ~level:1 (fun fmt -> fprintf fmt ".. generating witness file@,"); |
156 |
check_stateless_decls orig; |
157 |
let _ = Typing.type_prog type_env orig in |
158 |
let _ = Clock_calculus.clock_prog clock_env orig in |
159 |
Typing.uneval_prog_generics orig; |
160 |
Clock_calculus.uneval_prog_generics orig; |
161 |
Inliner.witness |
162 |
basename |
163 |
!Options.main_node |
164 |
orig prog type_env clock_env |
165 |
end; |
166 |
*) |
167 |
|
168 |
(* Computes and stores generic calls for each node, |
169 |
only useful for ANSI C90 compliant generic node compilation *) |
170 |
if !Options.ansi then Causality.NodeDep.compute_generic_calls prog; |
171 |
(*Hashtbl.iter (fun id td -> match td.Corelang.top_decl_desc with |
172 |
Corelang.Node nd -> Format.eprintf "%s calls %a" id |
173 |
Causality.NodeDep.pp_generic_calls nd | _ -> ()) Corelang.node_table;*) |
174 |
|
175 |
(* If some backend involving dynamic checks are active, then node annotations become runtime checks *) |
176 |
let prog = |
177 |
if dynamic_checks () then |
178 |
Spec.enforce_spec_prog prog |
179 |
else |
180 |
prog |
181 |
in |
182 |
|
183 |
|
184 |
(* (\* Registering and checking machine types *\) *) |
185 |
(* Machine_types.load prog; *) |
186 |
|
187 |
(* Normalization phase *) |
188 |
Log.report ~level:1 (fun fmt -> fprintf fmt ".. normalization@,"); |
189 |
let prog = Normalization.normalize_prog params prog in |
190 |
Log.report ~level:2 (fun fmt -> fprintf fmt "@[<v 2>@ %a@]@," Printers.pp_prog prog); |
191 |
|
192 |
(* Compatibility with Lusi *) |
193 |
(* If compiling a lusi, generate the lusic. If this is a lus file, Check the existence of a lusi (Lustre Interface file) *) |
194 |
compile_source_to_header |
195 |
prog !Global.type_env !Global.clock_env dirname basename extension; |
196 |
|
197 |
let prog = |
198 |
if !Options.mpfr |
199 |
then |
200 |
begin |
201 |
Log.report ~level:1 (fun fmt -> fprintf fmt ".. targetting MPFR library@,"); |
202 |
Mpfr.inject_prog prog |
203 |
end |
204 |
else |
205 |
begin |
206 |
Log.report ~level:1 (fun fmt -> fprintf fmt ".. keeping floating-point numbers@,"); |
207 |
prog |
208 |
end in |
209 |
Log.report ~level:2 (fun fmt -> fprintf fmt "@[<v 2>@ %a@]@," Printers.pp_prog prog); |
210 |
|
211 |
(* Checking array accesses *) |
212 |
if !Options.check then |
213 |
begin |
214 |
Log.report ~level:1 (fun fmt -> fprintf fmt ".. checking array accesses@,"); |
215 |
Access.check_prog prog; |
216 |
end; |
217 |
|
218 |
|
219 |
let prog = SortProg.sort_nodes_locals prog in |
220 |
|
221 |
prog, dependencies |
222 |
|
223 |
|
224 |
(* from source to machine code, with optimization *) |
225 |
let stage2 prog = |
226 |
(* Computation of node equation scheduling. It also breaks dependency cycles |
227 |
and warns about unused input or memory variables *) |
228 |
Log.report ~level:1 (fun fmt -> fprintf fmt ".. @[<v 2>scheduling@ "); |
229 |
let prog, node_schs = |
230 |
try |
231 |
Scheduling.schedule_prog prog |
232 |
with Causality.Error _ -> (* Error is not kept. It is recomputed in a more |
233 |
systemtic way in AlgebraicLoop module *) |
234 |
AlgebraicLoop.analyze prog |
235 |
in |
236 |
Log.report ~level:1 (fun fmt -> fprintf fmt "%a" Scheduling.pp_warning_unused node_schs); |
237 |
Log.report ~level:3 (fun fmt -> fprintf fmt "@[<v 2>@ %a@]@," Scheduling.pp_schedule node_schs); |
238 |
Log.report ~level:3 (fun fmt -> fprintf fmt "@[<v 2>@ %a@]@," Scheduling.pp_fanin_table node_schs); |
239 |
Log.report ~level:5 (fun fmt -> fprintf fmt "@[<v 2>@ %a@]@," Scheduling.pp_dep_graph node_schs); |
240 |
Log.report ~level:3 (fun fmt -> fprintf fmt "@[<v 2>@ %a@]@," Printers.pp_prog prog); |
241 |
Log.report ~level:1 (fun fmt -> fprintf fmt "@]@ "); |
242 |
|
243 |
(* TODO Salsa optimize prog: |
244 |
- emits warning for programs with pre inside expressions |
245 |
- make sure each node arguments and memory is bounded by a local annotation |
246 |
- introduce fresh local variables for each real pure subexpression |
247 |
*) |
248 |
(* DFS with modular code generation *) |
249 |
Log.report ~level:1 (fun fmt -> fprintf fmt ".. machines generation@,"); |
250 |
let machine_code = Machine_code.translate_prog prog node_schs in |
251 |
|
252 |
Log.report ~level:3 (fun fmt -> fprintf fmt ".. generated machines (unoptimized):@ %a@ " Machine_code_common.pp_machines machine_code); |
253 |
|
254 |
(* Optimize machine code *) |
255 |
Optimize_machine.optimize prog node_schs machine_code |
256 |
|
257 |
|
258 |
(* printing code *) |
259 |
let stage3 prog machine_code dependencies basename extension = |
260 |
let basename = Filename.basename basename in |
261 |
match !Options.output, extension with |
262 |
"C", ".lus" -> |
263 |
begin |
264 |
Log.report ~level:1 (fun fmt -> fprintf fmt ".. C code generation@,"); |
265 |
C_backend.translate_to_c |
266 |
(* alloc_header_file source_lib_file source_main_file makefile_file *) |
267 |
basename prog machine_code dependencies |
268 |
end |
269 |
| "C", _ -> |
270 |
begin |
271 |
Log.report ~level:1 (fun fmt -> fprintf fmt ".. no C code generation for lusi@,"); |
272 |
end |
273 |
| "java", _ -> |
274 |
begin |
275 |
(Format.eprintf "internal error: sorry, but not yet supported !"; assert false) |
276 |
(*let source_file = basename ^ ".java" in |
277 |
Log.report ~level:1 (fun fmt -> fprintf fmt ".. opening file %s@,@?" source_file); |
278 |
let source_out = open_out source_file in |
279 |
let source_fmt = formatter_of_out_channel source_out in |
280 |
Log.report ~level:1 (fun fmt -> fprintf fmt ".. java code generation@,@?"); |
281 |
Java_backend.translate_to_java source_fmt basename normalized_prog machine_code;*) |
282 |
end |
283 |
| "horn", _ -> |
284 |
begin |
285 |
let destname = !Options.dest_dir ^ "/" ^ basename in |
286 |
let source_file = destname ^ ".smt2" in (* Could be changed *) |
287 |
let source_out = open_out source_file in |
288 |
let fmt = formatter_of_out_channel source_out in |
289 |
Log.report ~level:1 (fun fmt -> fprintf fmt ".. hornification@,"); |
290 |
Horn_backend.translate fmt basename prog (Machine_code_common.arrow_machine::machine_code); |
291 |
(* Tracability file if option is activated *) |
292 |
if !Options.traces then ( |
293 |
let traces_file = destname ^ ".traces.xml" in (* Could be changed *) |
294 |
let traces_out = open_out traces_file in |
295 |
let fmt = formatter_of_out_channel traces_out in |
296 |
Log.report ~level:1 (fun fmt -> fprintf fmt ".. tracing info@,"); |
297 |
Horn_backend_traces.traces_file fmt basename prog machine_code; |
298 |
) |
299 |
end |
300 |
| "lustre", _ -> |
301 |
begin |
302 |
let destname = !Options.dest_dir ^ "/" ^ basename in |
303 |
let source_file = destname ^ ".lustrec" ^ extension in (* Could be changed *) |
304 |
Log.report ~level:1 (fun fmt -> fprintf fmt ".. exporting processed file as %s@," source_file); |
305 |
let source_out = open_out source_file in |
306 |
let fmt = formatter_of_out_channel source_out in |
307 |
Printers.pp_prog fmt prog; |
308 |
Format.fprintf fmt "@.@?"; |
309 |
(* Lustre_backend.translate fmt basename normalized_prog machine_code *) |
310 |
() |
311 |
end |
312 |
| "emf", _ -> |
313 |
begin |
314 |
let destname = !Options.dest_dir ^ "/" ^ basename in |
315 |
let source_file = destname ^ ".emf" in (* Could be changed *) |
316 |
let source_out = open_out source_file in |
317 |
let fmt = formatter_of_out_channel source_out in |
318 |
EMF_backend.translate fmt basename prog machine_code; |
319 |
() |
320 |
end |
321 |
|
322 |
| _ -> assert false |