lustrec / src / main_lustre_compiler.ml @ 6affc9f5
History | View | Annotate | Download (11.6 KB)
1 |
(* ---------------------------------------------------------------------------- |
---|---|
2 |
* SchedMCore - A MultiCore Scheduling Framework |
3 |
* Copyright (C) 2009-2013, ONERA, Toulouse, FRANCE - LIFL, Lille, FRANCE |
4 |
* Copyright (C) 2012-2013, INPT, Toulouse, FRANCE |
5 |
* |
6 |
* This file is part of Prelude |
7 |
* |
8 |
* Prelude is free software; you can redistribute it and/or |
9 |
* modify it under the terms of the GNU Lesser General Public License |
10 |
* as published by the Free Software Foundation ; either version 2 of |
11 |
* the License, or (at your option) any later version. |
12 |
* |
13 |
* Prelude is distributed in the hope that it will be useful, but |
14 |
* WITHOUT ANY WARRANTY ; without even the implied warranty of |
15 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
16 |
* Lesser General Public License for more details. |
17 |
* |
18 |
* You should have received a copy of the GNU Lesser General Public |
19 |
* License along with this program ; if not, write to the Free Software |
20 |
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 |
21 |
* USA |
22 |
*---------------------------------------------------------------------------- *) |
23 |
|
24 |
(* This module is used for the lustre to C compiler *) |
25 |
|
26 |
open Format |
27 |
open Log |
28 |
|
29 |
let usage = "Usage: lustrec [options] <source-file>" |
30 |
|
31 |
let extensions = [".ec"; ".lus"; ".lusi"] |
32 |
|
33 |
let type_decls env decls = |
34 |
report ~level:1 (fun fmt -> fprintf fmt ".. typing@,@?"); |
35 |
let new_env = |
36 |
begin |
37 |
try |
38 |
Typing.type_prog env decls |
39 |
with (Types.Error (loc,err)) as exc -> |
40 |
Format.eprintf "Typing error at loc %a: %a@]@." |
41 |
Location.pp_loc loc |
42 |
Types.pp_error err; |
43 |
raise exc |
44 |
end |
45 |
in |
46 |
if !Options.print_types then |
47 |
report ~level:1 (fun fmt -> fprintf fmt "@[<v 2>@ %a@]@,@?" Corelang.pp_prog_type decls); |
48 |
new_env |
49 |
|
50 |
let clock_decls env decls = |
51 |
report ~level:1 (fun fmt -> fprintf fmt ".. clock calculus@,@?"); |
52 |
let new_env = |
53 |
begin |
54 |
try |
55 |
Clock_calculus.clock_prog env decls |
56 |
with (Clocks.Error (loc,err)) as exc -> |
57 |
Location.print loc; |
58 |
eprintf "Clock calculus error at loc %a: %a@]@." Location.pp_loc loc Clocks.pp_error err; |
59 |
raise exc |
60 |
end |
61 |
in |
62 |
if !Options.print_clocks then |
63 |
report ~level:1 (fun fmt -> fprintf fmt "@[<v 2>@ %a@]@,@?" Corelang.pp_prog_clock decls); |
64 |
new_env |
65 |
|
66 |
(* Loading Lusi file and filling type tables with parsed |
67 |
functions/nodes *) |
68 |
let load_lusi filename = |
69 |
Location.input_name := filename; |
70 |
let lexbuf = Lexing.from_channel (open_in filename) in |
71 |
Location.init lexbuf filename; |
72 |
(* Parsing *) |
73 |
report ~level:1 (fun fmt -> fprintf fmt "@[<v>.. parsing header file %s@,@?" filename); |
74 |
try |
75 |
Parse.prog Parser_lustre.header Lexer_lustre.token lexbuf |
76 |
with (Lexer_lustre.Error err) | (Parse.Syntax_err err) as exc -> |
77 |
Parse.report_error err; |
78 |
raise exc |
79 |
|
80 |
let check_lusi header = |
81 |
let new_tenv = type_decls Basic_library.type_env header in (* Typing *) |
82 |
let new_cenv = clock_decls Basic_library.clock_env header in (* Clock calculus *) |
83 |
header, new_tenv, new_cenv |
84 |
|
85 |
let rec compile basename extension = |
86 |
(* Loading the input file *) |
87 |
let source_name = basename^extension in |
88 |
Location.input_name := source_name; |
89 |
let lexbuf = Lexing.from_channel (open_in source_name) in |
90 |
Location.init lexbuf source_name; |
91 |
(* Parsing *) |
92 |
report ~level:1 |
93 |
(fun fmt -> fprintf fmt "@[<v>.. parsing file %s@,@?" source_name); |
94 |
let prog = |
95 |
try |
96 |
Parse.prog Parser_lustre.prog Lexer_lustre.token lexbuf |
97 |
with |
98 |
| (Lexer_lustre.Error err) | (Parse.Syntax_err err) as exc -> |
99 |
Parse.report_error err; |
100 |
raise exc |
101 |
| Corelang.Error (err, loc) as exc -> |
102 |
Format.eprintf "Parsing error at loc %a: %a@]@." |
103 |
Location.pp_loc loc |
104 |
Corelang.pp_error err; |
105 |
raise exc |
106 |
in |
107 |
(* Extracting dependencies *) |
108 |
report ~level:1 (fun fmt -> fprintf fmt ".. extracting dependencies@,@?"); |
109 |
let dependencies = |
110 |
List.fold_right |
111 |
(fun d accu -> match d.Corelang.top_decl_desc with |
112 |
| Corelang.Open s -> s::accu |
113 |
| _ -> accu) |
114 |
prog [] |
115 |
in |
116 |
let type_env, clock_env = |
117 |
List.fold_left (fun (type_env, clock_env) s -> |
118 |
try |
119 |
let basename = s ^ ".lusi" in |
120 |
report ~level:1 (fun fmt -> fprintf fmt "@[<v 2>Library %s@ " s); |
121 |
let _, lusi_type_env, lusi_clock_env = check_lusi (load_lusi basename) in |
122 |
report ~level:1 (fun fmt -> fprintf fmt "@]@,@?"); |
123 |
Env.overwrite type_env lusi_type_env, |
124 |
Env.overwrite clock_env lusi_clock_env |
125 |
with Sys_error msg -> ( |
126 |
Format.eprintf "Failure: impossible to load library %s.@.%s@." s msg; |
127 |
exit 1 |
128 |
) |
129 |
) (Basic_library.type_env, Basic_library.clock_env) dependencies |
130 |
in |
131 |
|
132 |
(* Unfold consts *) |
133 |
(*let prog = Corelang.prog_unfold_consts prog in*) |
134 |
|
135 |
(* Sorting nodes *) |
136 |
let prog = SortProg.sort prog in |
137 |
|
138 |
(* Typing *) |
139 |
let computed_types_env = type_decls type_env prog in |
140 |
|
141 |
(* Clock calculus *) |
142 |
let computed_clocks_env = clock_decls clock_env prog in |
143 |
|
144 |
(* Perform global inlining *) |
145 |
let prog = |
146 |
if !Options.global_inline && |
147 |
(match !Options.main_node with | "" -> false | _ -> true) then |
148 |
Inliner.global_inline basename prog type_env clock_env |
149 |
else |
150 |
prog |
151 |
in |
152 |
|
153 |
(* Delay calculus *) |
154 |
(* |
155 |
if(!Options.delay_calculus) |
156 |
then |
157 |
begin |
158 |
report ~level:1 (fun fmt -> fprintf fmt ".. initialisation analysis@?"); |
159 |
try |
160 |
Delay_calculus.delay_prog Basic_library.delay_env prog |
161 |
with (Delay.Error (loc,err)) as exc -> |
162 |
Location.print loc; |
163 |
eprintf "%a" Delay.pp_error err; |
164 |
Utils.track_exception (); |
165 |
raise exc |
166 |
end; |
167 |
*) |
168 |
(* |
169 |
eprintf "Causality analysis@.@?"; |
170 |
(* Causality analysis *) |
171 |
begin |
172 |
try |
173 |
Causality.check_causal_prog prog |
174 |
with (Causality.Cycle v) as exc -> |
175 |
Causality.pp_error err_formatter v; |
176 |
raise exc |
177 |
end; |
178 |
*) |
179 |
|
180 |
(* Checking the existence of a lusi (Lustre Interface file) *) |
181 |
let lusi_name = basename ^ ".lusi" in |
182 |
let _ = |
183 |
try |
184 |
let _ = open_in lusi_name in |
185 |
let header = load_lusi lusi_name in |
186 |
let _, declared_types_env, declared_clocks_env = check_lusi header in |
187 |
(* checking type compatibility with computed types*) |
188 |
Typing.check_env_compat header declared_types_env computed_types_env; |
189 |
(* checking clocks compatibility with computed clocks*) |
190 |
Clock_calculus.check_env_compat header declared_clocks_env computed_clocks_env; |
191 |
Typing.uneval_prog_generics prog |
192 |
with Sys_error _ -> ( |
193 |
(* Printing lusi file is necessary *) |
194 |
report ~level:1 |
195 |
(fun fmt -> |
196 |
fprintf fmt |
197 |
".. generating lustre interface file %s@,@?" lusi_name); |
198 |
let lusi_out = open_out lusi_name in |
199 |
let lusi_fmt = formatter_of_out_channel lusi_out in |
200 |
Typing.uneval_prog_generics prog; |
201 |
Clock_calculus.uneval_prog_generics prog; |
202 |
Printers.pp_lusi_header lusi_fmt source_name prog |
203 |
) |
204 |
| (Types.Error (loc,err)) as exc -> |
205 |
Format.eprintf "Type mismatch between computed type and declared type in lustre interface file: %a@]@." |
206 |
Types.pp_error err; |
207 |
raise exc |
208 |
| Clocks.Error (loc, err) as exc -> |
209 |
Format.eprintf "Clock mismatch between computed clock and declared clock in lustre interface file: %a@]@." |
210 |
Clocks.pp_error err; |
211 |
raise exc |
212 |
in |
213 |
|
214 |
(* Computes and stores generic calls for each node, |
215 |
only useful for ANSI C90 compliant generic node compilation *) |
216 |
if !Options.ansi then Causality.NodeDep.compute_generic_calls prog; |
217 |
(*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;*) |
218 |
|
219 |
(* Normalization phase *) |
220 |
report ~level:1 (fun fmt -> fprintf fmt ".. normalization@,@?"); |
221 |
let normalized_prog = Normalization.normalize_prog prog in |
222 |
(*Typing.uneval_prog_generics normalized_prog;*) |
223 |
report ~level:2 (fun fmt -> fprintf fmt "@[<v 2>@ %a@]@,@?" Printers.pp_prog normalized_prog); |
224 |
(* Checking array accesses *) |
225 |
if !Options.check then |
226 |
begin |
227 |
report ~level:1 (fun fmt -> fprintf fmt ".. array access checks@,@?"); |
228 |
Access.check_prog normalized_prog; |
229 |
end; |
230 |
|
231 |
(* DFS with modular code generation *) |
232 |
report ~level:1 (fun fmt -> fprintf fmt ".. machines generation@,@?"); |
233 |
let machine_code = Machine_code.translate_prog normalized_prog in |
234 |
report ~level:2 (fun fmt -> fprintf fmt "@[<v 2>@ %a@]@,@?" |
235 |
(Utils.fprintf_list ~sep:"@ " Machine_code.pp_machine) |
236 |
machine_code); |
237 |
|
238 |
(* Creating destination directory if needed *) |
239 |
if not (Sys.file_exists !Options.dest_dir) then ( |
240 |
report ~level:1 (fun fmt -> fprintf fmt ".. creating destination directory@,@?"); |
241 |
Unix.mkdir !Options.dest_dir (Unix.stat ".").Unix.st_perm |
242 |
); |
243 |
if (Unix.stat !Options.dest_dir).Unix.st_kind <> Unix.S_DIR then ( |
244 |
Format.eprintf "Failure: destination %s is not a directory.@.@." !Options.dest_dir; |
245 |
exit 1 |
246 |
); |
247 |
(* Printing code *) |
248 |
let basename = Filename.basename basename in |
249 |
let destname = !Options.dest_dir ^ "/" ^ basename in |
250 |
let _ = match !Options.output with |
251 |
"C" -> |
252 |
begin |
253 |
let header_file = destname ^ ".h" in (* Could be changed *) |
254 |
let source_file = destname ^ ".c" in (* Could be changed *) |
255 |
let makefile_file = destname ^ ".makefile" in (* Could be changed *) |
256 |
let spec_file_opt = if !Options.c_spec then |
257 |
( |
258 |
let spec_file = basename ^ "_spec.c" in |
259 |
report ~level:1 (fun fmt -> fprintf fmt ".. opening files %s, %s and %s@,@?" header_file source_file spec_file); |
260 |
Some spec_file |
261 |
) else ( |
262 |
report ~level:1 (fun fmt -> fprintf fmt ".. opening files %s and %s@,@?" header_file source_file); |
263 |
None |
264 |
) |
265 |
in |
266 |
let header_out = open_out header_file in |
267 |
let header_fmt = formatter_of_out_channel header_out in |
268 |
let source_out = open_out source_file in |
269 |
let source_fmt = formatter_of_out_channel source_out in |
270 |
let makefile_out = open_out makefile_file in |
271 |
let makefile_fmt = formatter_of_out_channel makefile_out in |
272 |
let spec_fmt_opt = match spec_file_opt with |
273 |
None -> None |
274 |
| Some f -> Some (formatter_of_out_channel (open_out f)) |
275 |
in |
276 |
report ~level:1 (fun fmt -> fprintf fmt ".. C code generation@,@?"); |
277 |
C_backend.translate_to_c header_fmt source_fmt makefile_fmt spec_fmt_opt basename normalized_prog machine_code dependencies |
278 |
end |
279 |
| "java" -> |
280 |
begin |
281 |
failwith "Sorry, but not yet supported !" |
282 |
(*let source_file = basename ^ ".java" in |
283 |
report ~level:1 (fun fmt -> fprintf fmt ".. opening file %s@,@?" source_file); |
284 |
let source_out = open_out source_file in |
285 |
let source_fmt = formatter_of_out_channel source_out in |
286 |
report ~level:1 (fun fmt -> fprintf fmt ".. java code generation@,@?"); |
287 |
Java_backend.translate_to_java source_fmt basename normalized_prog machine_code;*) |
288 |
end |
289 |
| "horn" -> |
290 |
begin |
291 |
let source_file = destname ^ ".smt2" in (* Could be changed *) |
292 |
let source_out = open_out source_file in |
293 |
let fmt = formatter_of_out_channel source_out in |
294 |
Horn_backend.translate fmt basename normalized_prog machine_code |
295 |
end |
296 |
| _ -> assert false |
297 |
in |
298 |
report ~level:1 (fun fmt -> fprintf fmt ".. done !@ @]@."); |
299 |
(* We stop the process here *) |
300 |
exit 0 |
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 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 |
| exc -> (Utils.track_exception (); raise exc) |
321 |
|
322 |
(* Local Variables: *) |
323 |
(* compile-command:"make -C .." *) |
324 |
(* End: *) |