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:1 (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 >= 4 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 >= 2 && !Options.output <> "horn" then
|
218
|
begin
|
219
|
Log.report ~level:1 (fun fmt -> fprintf fmt ".. machines optimization (phase 1)@,");
|
220
|
Optimize_machine.machines_unfold (Corelang.get_consts prog) node_schs machine_code
|
221
|
end
|
222
|
else
|
223
|
machine_code
|
224
|
in
|
225
|
(* Optimize machine code *)
|
226
|
let machine_code =
|
227
|
if !Options.optimization >= 3 && !Options.output <> "horn" then
|
228
|
begin
|
229
|
Log.report ~level:1 (fun fmt -> fprintf fmt ".. machines optimization (phase 2)@,");
|
230
|
Optimize_machine.machines_fusion (Optimize_machine.machines_reuse_variables machine_code node_schs)
|
231
|
end
|
232
|
else
|
233
|
machine_code
|
234
|
in
|
235
|
Log.report ~level:3 (fun fmt -> fprintf fmt "@[<v 2>@ %a@]@,"
|
236
|
(Utils.fprintf_list ~sep:"@ " Machine_code.pp_machine)
|
237
|
machine_code);
|
238
|
|
239
|
(* Printing code *)
|
240
|
let basename = Filename.basename basename in
|
241
|
let destname = !Options.dest_dir ^ "/" ^ basename in
|
242
|
let _ = match !Options.output with
|
243
|
"C" ->
|
244
|
begin
|
245
|
let alloc_header_file = destname ^ "_alloc.h" in (* Could be changed *)
|
246
|
let source_lib_file = destname ^ ".c" in (* Could be changed *)
|
247
|
let source_main_file = destname ^ "_main.c" in (* Could be changed *)
|
248
|
let makefile_file = destname ^ ".makefile" in (* Could be changed *)
|
249
|
Log.report ~level:1 (fun fmt -> fprintf fmt ".. C code generation@,");
|
250
|
C_backend.translate_to_c
|
251
|
alloc_header_file source_lib_file source_main_file makefile_file
|
252
|
basename prog machine_code dependencies
|
253
|
end
|
254
|
| "java" ->
|
255
|
begin
|
256
|
failwith "Sorry, but not yet supported !"
|
257
|
(*let source_file = basename ^ ".java" in
|
258
|
Log.report ~level:1 (fun fmt -> fprintf fmt ".. opening file %s@,@?" source_file);
|
259
|
let source_out = open_out source_file in
|
260
|
let source_fmt = formatter_of_out_channel source_out in
|
261
|
Log.report ~level:1 (fun fmt -> fprintf fmt ".. java code generation@,@?");
|
262
|
Java_backend.translate_to_java source_fmt basename normalized_prog machine_code;*)
|
263
|
end
|
264
|
| "horn" ->
|
265
|
begin
|
266
|
let source_file = destname ^ ".smt2" in (* Could be changed *)
|
267
|
let source_out = open_out source_file in
|
268
|
let fmt = formatter_of_out_channel source_out in
|
269
|
Horn_backend.translate fmt basename prog machine_code;
|
270
|
(* Tracability file if option is activated *)
|
271
|
if !Options.horntraces then (
|
272
|
let traces_file = destname ^ ".traces" in (* Could be changed *)
|
273
|
let traces_out = open_out traces_file in
|
274
|
let fmt = formatter_of_out_channel traces_out in
|
275
|
Horn_backend.traces_file fmt basename prog machine_code
|
276
|
)
|
277
|
end
|
278
|
| "lustre" ->
|
279
|
begin
|
280
|
let source_file = destname ^ ".lustrec.lus" in (* Could be changed *)
|
281
|
let source_out = open_out source_file in
|
282
|
let fmt = formatter_of_out_channel source_out in
|
283
|
Printers.pp_prog fmt prog;
|
284
|
(* Lustre_backend.translate fmt basename normalized_prog machine_code *)
|
285
|
()
|
286
|
end
|
287
|
|
288
|
| _ -> assert false
|
289
|
in
|
290
|
begin
|
291
|
Log.report ~level:1 (fun fmt -> fprintf fmt ".. done !@ @]@.");
|
292
|
(* We stop the process here *)
|
293
|
exit 0
|
294
|
end
|
295
|
|
296
|
let compile basename extension =
|
297
|
match extension with
|
298
|
| ".lusi" -> compile_header basename extension
|
299
|
| ".lus" -> compile_source basename extension
|
300
|
| _ -> assert false
|
301
|
|
302
|
let anonymous filename =
|
303
|
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
|
304
|
if ok_ext then
|
305
|
let basename = Filename.chop_suffix (Filename.basename filename) ext in
|
306
|
compile basename ext
|
307
|
else
|
308
|
raise (Arg.Bad ("Can only compile *.lusi, *.lus or *.ec files"))
|
309
|
|
310
|
let _ =
|
311
|
Corelang.add_internal_funs ();
|
312
|
try
|
313
|
Printexc.record_backtrace true;
|
314
|
Arg.parse Options.options anonymous usage
|
315
|
with
|
316
|
| Parse.Syntax_err _ | Lexer_lustre.Error _
|
317
|
| Types.Error (_,_) | Clocks.Error (_,_)
|
318
|
| Corelang.Error _ (*| Task_set.Error _*)
|
319
|
| Causality.Cycle _ -> exit 1
|
320
|
| Sys_error msg -> (eprintf "Failure: %s@." msg)
|
321
|
| exc -> (Utils.track_exception (); raise exc)
|
322
|
|
323
|
(* Local Variables: *)
|
324
|
(* compile-command:"make -C .." *)
|
325
|
(* End: *)
|