1 |
0cbf0839
|
ploc
|
(* ----------------------------------------------------------------------------
|
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 |
c518d082
|
xthirioux
|
let extensions = [".ec"; ".lus"; ".lusi"]
|
32 |
0cbf0839
|
ploc
|
|
33 |
f22632aa
|
ploc
|
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 filing 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 |
8b3afe43
|
xthirioux
|
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 |
f22632aa
|
ploc
|
let new_tenv = type_decls Basic_library.type_env header in (* Typing *)
|
82 |
c518d082
|
xthirioux
|
let new_cenv = clock_decls Basic_library.clock_env header in (* Clock calculus *)
|
83 |
f22632aa
|
ploc
|
header, new_tenv, new_cenv
|
84 |
|
|
|
85 |
0cbf0839
|
ploc
|
let rec compile basename extension =
|
86 |
f22632aa
|
ploc
|
(* Loading the input file *)
|
87 |
0cbf0839
|
ploc
|
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 |
f22632aa
|
ploc
|
report ~level:1
|
93 |
|
|
(fun fmt -> fprintf fmt "@[<v>.. parsing file %s@,@?" source_name);
|
94 |
0cbf0839
|
ploc
|
let prog =
|
95 |
|
|
try
|
96 |
|
|
Parse.prog Parser_lustre.prog Lexer_lustre.token lexbuf
|
97 |
|
|
with (Lexer_lustre.Error err) | (Parse.Syntax_err err) as exc ->
|
98 |
|
|
Parse.report_error err;
|
99 |
|
|
raise exc
|
100 |
|
|
in
|
101 |
f22632aa
|
ploc
|
(* Extracting dependencies *)
|
102 |
|
|
report ~level:1 (fun fmt -> fprintf fmt ".. extracting dependencies@,@?");
|
103 |
|
|
let dependencies =
|
104 |
0cbf0839
|
ploc
|
List.fold_right
|
105 |
f22632aa
|
ploc
|
(fun d accu -> match d.Corelang.top_decl_desc with
|
106 |
|
|
| Corelang.Open s -> s::accu
|
107 |
|
|
| _ -> accu)
|
108 |
0cbf0839
|
ploc
|
prog []
|
109 |
|
|
in
|
110 |
f22632aa
|
ploc
|
let type_env, clock_env =
|
111 |
|
|
List.fold_left (fun (type_env, clock_env) s ->
|
112 |
|
|
try
|
113 |
|
|
let basename = s ^ ".lusi" in
|
114 |
|
|
report ~level:1 (fun fmt -> fprintf fmt "@[<v 2>Library %s@ " s);
|
115 |
8b3afe43
|
xthirioux
|
let _, lusi_type_env, lusi_clock_env = check_lusi (load_lusi basename) in
|
116 |
f22632aa
|
ploc
|
report ~level:1 (fun fmt -> fprintf fmt "@]@,@?");
|
117 |
|
|
Env.overwrite type_env lusi_type_env,
|
118 |
|
|
Env.overwrite clock_env lusi_clock_env
|
119 |
|
|
with Sys_error msg -> (
|
120 |
|
|
Format.eprintf "Failure: impossible to load library %s.@.%s@." s msg;
|
121 |
|
|
exit 1
|
122 |
|
|
)
|
123 |
|
|
) (Basic_library.type_env, Basic_library.clock_env) dependencies
|
124 |
|
|
in
|
125 |
|
|
|
126 |
0cbf0839
|
ploc
|
(* Unfold consts *)
|
127 |
|
|
(*let prog = Corelang.prog_unfold_consts prog in*)
|
128 |
|
|
|
129 |
|
|
(* Sorting nodes *)
|
130 |
|
|
let prog = SortProg.sort prog in
|
131 |
f22632aa
|
ploc
|
|
132 |
0cbf0839
|
ploc
|
(* Typing *)
|
133 |
f22632aa
|
ploc
|
let computed_types_env = type_decls type_env prog in
|
134 |
0cbf0839
|
ploc
|
|
135 |
|
|
(* Clock calculus *)
|
136 |
f22632aa
|
ploc
|
let computed_clocks_env = clock_decls clock_env prog in
|
137 |
0cbf0839
|
ploc
|
|
138 |
c02d255e
|
ploc
|
(* Perform global inlining *)
|
139 |
|
|
let prog =
|
140 |
|
|
if !Options.global_inline &&
|
141 |
|
|
(match !Options.main_node with | "" -> false | _ -> true) then
|
142 |
2842f7ca
|
ploc
|
Inliner.global_inline basename prog type_env clock_env
|
143 |
c02d255e
|
ploc
|
else
|
144 |
|
|
prog
|
145 |
|
|
in
|
146 |
|
|
|
147 |
0cbf0839
|
ploc
|
(* Delay calculus *)
|
148 |
f22632aa
|
ploc
|
(*
|
149 |
|
|
if(!Options.delay_calculus)
|
150 |
|
|
then
|
151 |
0cbf0839
|
ploc
|
begin
|
152 |
f22632aa
|
ploc
|
report ~level:1 (fun fmt -> fprintf fmt ".. initialisation analysis@?");
|
153 |
|
|
try
|
154 |
|
|
Delay_calculus.delay_prog Basic_library.delay_env prog
|
155 |
|
|
with (Delay.Error (loc,err)) as exc ->
|
156 |
|
|
Location.print loc;
|
157 |
|
|
eprintf "%a" Delay.pp_error err;
|
158 |
|
|
Utils.track_exception ();
|
159 |
|
|
raise exc
|
160 |
0cbf0839
|
ploc
|
end;
|
161 |
f22632aa
|
ploc
|
*)
|
162 |
0cbf0839
|
ploc
|
(*
|
163 |
|
|
eprintf "Causality analysis@.@?";
|
164 |
f22632aa
|
ploc
|
(* Causality analysis *)
|
165 |
0cbf0839
|
ploc
|
begin
|
166 |
|
|
try
|
167 |
|
|
Causality.check_causal_prog prog
|
168 |
|
|
with (Causality.Cycle v) as exc ->
|
169 |
|
|
Causality.pp_error err_formatter v;
|
170 |
|
|
raise exc
|
171 |
|
|
end;
|
172 |
|
|
*)
|
173 |
8b3afe43
|
xthirioux
|
|
174 |
|
|
(* Checking the existence of a lusi (Lustre Interface file) *)
|
175 |
|
|
let lusi_name = basename ^ ".lusi" in
|
176 |
|
|
let _ =
|
177 |
|
|
try
|
178 |
|
|
let _ = open_in lusi_name in
|
179 |
|
|
let header = load_lusi lusi_name in
|
180 |
|
|
let _, declared_types_env, declared_clocks_env = check_lusi header in
|
181 |
|
|
(* checking type compatibility with computed types*)
|
182 |
|
|
Typing.check_env_compat header declared_types_env computed_types_env;
|
183 |
|
|
(* checking clocks compatibilty with computed clocks*)
|
184 |
|
|
Clock_calculus.check_env_compat header declared_clocks_env computed_clocks_env;
|
185 |
|
|
Typing.uneval_prog_generics prog
|
186 |
|
|
with Sys_error _ -> (
|
187 |
|
|
(* Printing lusi file is necessary *)
|
188 |
|
|
report ~level:1
|
189 |
|
|
(fun fmt ->
|
190 |
|
|
fprintf fmt
|
191 |
|
|
".. generating lustre interface file %s@,@?" lusi_name);
|
192 |
|
|
let lusi_out = open_out lusi_name in
|
193 |
|
|
let lusi_fmt = formatter_of_out_channel lusi_out in
|
194 |
|
|
Typing.uneval_prog_generics prog;
|
195 |
c518d082
|
xthirioux
|
Clock_calculus.uneval_prog_generics prog;
|
196 |
8b3afe43
|
xthirioux
|
Printers.pp_lusi_header lusi_fmt source_name prog
|
197 |
|
|
)
|
198 |
|
|
| (Types.Error (loc,err)) as exc ->
|
199 |
|
|
Format.eprintf "Type mismatch between computed type and declared type in lustre interface file: %a@]@."
|
200 |
|
|
Types.pp_error err;
|
201 |
|
|
raise exc
|
202 |
c518d082
|
xthirioux
|
| Clocks.Error (loc, err) as exc ->
|
203 |
|
|
Format.eprintf "Clock mismatch between computed clock and declared clock in lustre interface file: %a@]@."
|
204 |
|
|
Clocks.pp_error err;
|
205 |
|
|
raise exc
|
206 |
8b3afe43
|
xthirioux
|
in
|
207 |
|
|
|
208 |
0cbf0839
|
ploc
|
(* Computes and stores generic calls for each node,
|
209 |
|
|
only useful for ANSI C90 compliant generic node compilation *)
|
210 |
|
|
if !Options.ansi then Causality.NodeDep.compute_generic_calls prog;
|
211 |
|
|
(*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;*)
|
212 |
|
|
|
213 |
|
|
(* Normalization phase *)
|
214 |
|
|
report ~level:1 (fun fmt -> fprintf fmt ".. normalization@,@?");
|
215 |
|
|
let normalized_prog = Normalization.normalize_prog prog in
|
216 |
8b3afe43
|
xthirioux
|
(*Typing.uneval_prog_generics normalized_prog;*)
|
217 |
0cbf0839
|
ploc
|
report ~level:2 (fun fmt -> fprintf fmt "@[<v 2>@ %a@]@,@?" Printers.pp_prog normalized_prog);
|
218 |
|
|
(* Checking array accesses *)
|
219 |
|
|
if !Options.check then
|
220 |
|
|
begin
|
221 |
|
|
report ~level:1 (fun fmt -> fprintf fmt ".. array access checks@,@?");
|
222 |
|
|
Access.check_prog normalized_prog;
|
223 |
|
|
end;
|
224 |
|
|
|
225 |
|
|
(* DFS with modular code generation *)
|
226 |
|
|
report ~level:1 (fun fmt -> fprintf fmt ".. machines generation@,@?");
|
227 |
|
|
let machine_code = Machine_code.translate_prog normalized_prog in
|
228 |
|
|
report ~level:2 (fun fmt -> fprintf fmt "@[<v 2>@ %a@]@,@?"
|
229 |
|
|
(Utils.fprintf_list ~sep:"@ " Machine_code.pp_machine)
|
230 |
|
|
machine_code);
|
231 |
|
|
|
232 |
|
|
(* Printing code *)
|
233 |
8b3afe43
|
xthirioux
|
let basename = Filename.basename basename in
|
234 |
c518d082
|
xthirioux
|
let destname = !Options.dest_dir ^ "/" ^ basename in
|
235 |
f6923c9e
|
ploc
|
let _ = match !Options.output with
|
236 |
|
|
"C" ->
|
237 |
|
|
begin
|
238 |
8b3afe43
|
xthirioux
|
let header_file = destname ^ ".h" in (* Could be changed *)
|
239 |
|
|
let source_file = destname ^ ".c" in (* Could be changed *)
|
240 |
|
|
let makefile_file = destname ^ ".makefile" in (* Could be changed *)
|
241 |
f6923c9e
|
ploc
|
let spec_file_opt = if !Options.c_spec then
|
242 |
|
|
(
|
243 |
|
|
let spec_file = basename ^ "_spec.c" in
|
244 |
|
|
report ~level:1 (fun fmt -> fprintf fmt ".. opening files %s, %s and %s@,@?" header_file source_file spec_file);
|
245 |
|
|
Some spec_file
|
246 |
|
|
) else (
|
247 |
|
|
report ~level:1 (fun fmt -> fprintf fmt ".. opening files %s and %s@,@?" header_file source_file);
|
248 |
|
|
None
|
249 |
|
|
)
|
250 |
|
|
in
|
251 |
|
|
let header_out = open_out header_file in
|
252 |
|
|
let header_fmt = formatter_of_out_channel header_out in
|
253 |
|
|
let source_out = open_out source_file in
|
254 |
|
|
let source_fmt = formatter_of_out_channel source_out in
|
255 |
|
|
let makefile_out = open_out makefile_file in
|
256 |
|
|
let makefile_fmt = formatter_of_out_channel makefile_out in
|
257 |
|
|
let spec_fmt_opt = match spec_file_opt with
|
258 |
|
|
None -> None
|
259 |
|
|
| Some f -> Some (formatter_of_out_channel (open_out f))
|
260 |
|
|
in
|
261 |
|
|
report ~level:1 (fun fmt -> fprintf fmt ".. C code generation@,@?");
|
262 |
2842f7ca
|
ploc
|
C_backend.translate_to_c header_fmt source_fmt makefile_fmt spec_fmt_opt basename normalized_prog machine_code dependencies
|
263 |
f6923c9e
|
ploc
|
end
|
264 |
8b3afe43
|
xthirioux
|
| "java" ->
|
265 |
|
|
begin
|
266 |
|
|
failwith "Sorry, but not yet supported !"
|
267 |
0cbf0839
|
ploc
|
(*let source_file = basename ^ ".java" in
|
268 |
f22632aa
|
ploc
|
report ~level:1 (fun fmt -> fprintf fmt ".. opening file %s@,@?" source_file);
|
269 |
|
|
let source_out = open_out source_file in
|
270 |
|
|
let source_fmt = formatter_of_out_channel source_out in
|
271 |
|
|
report ~level:1 (fun fmt -> fprintf fmt ".. java code generation@,@?");
|
272 |
|
|
Java_backend.translate_to_java source_fmt basename normalized_prog machine_code;*)
|
273 |
8b3afe43
|
xthirioux
|
end
|
274 |
|
|
| "horn" ->
|
275 |
|
|
begin
|
276 |
2842f7ca
|
ploc
|
let source_file = destname ^ ".smt2" in (* Could be changed *)
|
277 |
8b3afe43
|
xthirioux
|
let source_out = open_out source_file in
|
278 |
|
|
let fmt = formatter_of_out_channel source_out in
|
279 |
|
|
Horn_backend.translate fmt basename normalized_prog machine_code
|
280 |
|
|
end
|
281 |
4f3cc9f3
|
ploc
|
| _ -> assert false
|
282 |
f6923c9e
|
ploc
|
in
|
283 |
0cbf0839
|
ploc
|
report ~level:1 (fun fmt -> fprintf fmt ".. done !@ @]@.");
|
284 |
|
|
(* We stop the process here *)
|
285 |
|
|
exit 0
|
286 |
|
|
|
287 |
|
|
let anonymous filename =
|
288 |
|
|
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
|
289 |
|
|
if ok_ext then
|
290 |
|
|
let basename = Filename.chop_suffix filename ext in
|
291 |
|
|
compile basename ext
|
292 |
|
|
else
|
293 |
c518d082
|
xthirioux
|
raise (Arg.Bad ("Can only compile *.lusi, *.lus or *.ec files"))
|
294 |
0cbf0839
|
ploc
|
|
295 |
|
|
let _ =
|
296 |
|
|
Corelang.add_internal_funs ();
|
297 |
|
|
try
|
298 |
|
|
Printexc.record_backtrace true;
|
299 |
|
|
Arg.parse Options.options anonymous usage
|
300 |
|
|
with
|
301 |
|
|
| Parse.Syntax_err _ | Lexer_lustre.Error _
|
302 |
|
|
| Types.Error (_,_) | Clocks.Error (_,_)
|
303 |
|
|
| Corelang.Error _ (*| Task_set.Error _*)
|
304 |
|
|
| Causality.Cycle _ -> exit 1
|
305 |
|
|
| exc -> (Utils.track_exception (); raise exc)
|
306 |
|
|
|
307 |
|
|
(* Local Variables: *)
|
308 |
|
|
(* compile-command:"make -C .." *)
|
309 |
|
|
(* End: *)
|