Project

General

Profile

Statistics
| Branch: | Tag: | Revision:

lustrec / src / main_lustre_compiler.ml @ b38ffff3

History | View | Annotate | Download (13.9 KB)

1 b38ffff3 ploc
(********************************************************************)
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 0cbf0839 ploc
12
open Format
13
open Log
14
15 0038002e ploc
open LustreSpec
16
17 0cbf0839 ploc
let usage = "Usage: lustrec [options] <source-file>"
18
19 c518d082 xthirioux
let extensions = [".ec"; ".lus"; ".lusi"]
20 0cbf0839 ploc
21 d3e4c22f xthirioux
let check_stateless_decls decls =
22 9aaee7f9 xthirioux
  Log.report ~level:1 (fun fmt -> fprintf fmt ".. checking stateless/stateful status@ ");
23 d3e4c22f xthirioux
  try
24 5538b7ac xthirioux
    Stateless.check_prog decls
25
  with (Stateless.Error (loc, err)) as exc ->
26 90028770 ploc
    eprintf "Stateless status error %a%a@."
27
      Stateless.pp_error err
28
      Location.pp_loc loc;
29 d3e4c22f xthirioux
    raise exc
30
31 f22632aa ploc
let type_decls env decls =  
32 9aaee7f9 xthirioux
  Log.report ~level:1 (fun fmt -> fprintf fmt ".. typing@ ");
33 f22632aa ploc
  let new_env = 
34
    begin
35
      try
36
	Typing.type_prog env decls
37
      with (Types.Error (loc,err)) as exc ->
38 90028770 ploc
	eprintf "Typing error %a%a@."
39
	  Types.pp_error err
40
	  Location.pp_loc loc;
41 f22632aa ploc
	raise exc
42
    end 
43
  in
44
  if !Options.print_types then
45 9aaee7f9 xthirioux
    Log.report ~level:1 (fun fmt -> fprintf fmt "@[<v 2>  %a@]@ " Corelang.pp_prog_type decls);
46 f22632aa ploc
  new_env
47
      
48
let clock_decls env decls = 
49 9aaee7f9 xthirioux
  Log.report ~level:1 (fun fmt -> fprintf fmt ".. clock calculus@ ");
50 f22632aa ploc
  let new_env =
51
    begin
52
      try
53
	Clock_calculus.clock_prog env decls
54
      with (Clocks.Error (loc,err)) as exc ->
55 90028770 ploc
	eprintf "Clock calculus error %a%a@." Clocks.pp_error err Location.pp_loc loc;
56 f22632aa ploc
	raise exc
57
    end
58
  in
59
  if !Options.print_clocks then
60 9aaee7f9 xthirioux
    Log.report ~level:1 (fun fmt -> fprintf fmt "@[<v 2>  %a@]@ " Corelang.pp_prog_clock decls);
61 f22632aa ploc
  new_env
62
63 6affc9f5 xthirioux
(* Loading Lusi file and filling type tables with parsed
64 f22632aa ploc
   functions/nodes *)
65 2ea1e4a6 xthirioux
let load_lusi own filename =
66 f22632aa ploc
  Location.input_name := filename;
67
  let lexbuf = Lexing.from_channel (open_in filename) in
68
  Location.init lexbuf filename;
69
  (* Parsing *)
70 9aaee7f9 xthirioux
  Log.report ~level:1 (fun fmt -> fprintf fmt ".. parsing header file %s@ " filename);
71 c00d0b42 ploc
    try
72
      Parse.header own Parser_lustre.header Lexer_lustre.token lexbuf
73
    with
74
    | (Lexer_lustre.Error err) | (Parse.Syntax_err err) as exc -> 
75
      Parse.report_error err;
76
      raise exc
77
    | Corelang.Error (loc, err) as exc -> (
78 90028770 ploc
      eprintf "Parsing error %a%a@."
79
	Corelang.pp_error err
80
	Location.pp_loc loc;
81 c00d0b42 ploc
      raise exc
82
    )
83
84 8b3afe43 xthirioux
85
let check_lusi header =
86 f22632aa ploc
  let new_tenv = type_decls Basic_library.type_env header in   (* Typing *)
87 c518d082 xthirioux
  let new_cenv = clock_decls Basic_library.clock_env header in   (* Clock calculus *)
88 f22632aa ploc
  header, new_tenv, new_cenv
89 c00d0b42 ploc
90 59294251 ploc
let load_n_check_lusi source_name lusi_name prog computed_types_env computed_clocks_env= 
91
  try 
92
    let _ = open_in lusi_name in
93
    let header = load_lusi true lusi_name in
94
    let _, declared_types_env, declared_clocks_env = check_lusi header in
95 7ecdb0aa xthirioux
        
96
97
    (* checking type compatibility with computed types*)
98 59294251 ploc
    Typing.check_env_compat header declared_types_env computed_types_env;
99
    Typing.uneval_prog_generics prog;
100
    
101 7ecdb0aa xthirioux
    (* checking clocks compatibility with computed clocks*)
102
    Clock_calculus.check_env_compat header declared_clocks_env computed_clocks_env;
103 2cf39a8e xthirioux
    Clock_calculus.uneval_prog_generics prog;
104
105
    (* checking stateless status compatibility *)
106
    Stateless.check_compat header
107 59294251 ploc
108
    with Sys_error _ -> ( 
109
      (* Printing lusi file is necessary *)
110 9aaee7f9 xthirioux
      Log.report ~level:1 
111 59294251 ploc
	(fun fmt -> 
112
	  fprintf fmt 
113
	    ".. generating lustre interface file %s@," lusi_name);
114
      let lusi_out = open_out lusi_name in
115
      let lusi_fmt = formatter_of_out_channel lusi_out in
116
      Typing.uneval_prog_generics prog;
117
      Clock_calculus.uneval_prog_generics prog;
118
      Printers.pp_lusi_header lusi_fmt source_name prog
119
    )
120
    | (Types.Error (loc,err)) as exc ->
121
      eprintf "Type mismatch between computed type and declared type in lustre interface file: %a@."
122
	Types.pp_error err;
123
      raise exc
124
    | Clocks.Error (loc, err) as exc ->
125
      eprintf "Clock mismatch between computed clock and declared clock in lustre interface file: %a@."
126
	Clocks.pp_error err;
127
      raise exc
128
    | Stateless.Error (loc, err) as exc ->
129
      eprintf "Stateless status mismatch between defined status and declared status in lustre interface file: %a@."
130
	Stateless.pp_error err;
131
      raise exc
132 f22632aa ploc
    
133 0cbf0839 ploc
let rec compile basename extension =
134 59294251 ploc
135 f22632aa ploc
  (* Loading the input file *)
136 0cbf0839 ploc
  let source_name = basename^extension in
137
  Location.input_name := source_name;
138
  let lexbuf = Lexing.from_channel (open_in source_name) in
139
  Location.init lexbuf source_name;
140 59294251 ploc
141 0cbf0839 ploc
  (* Parsing *)
142 9aaee7f9 xthirioux
  Log.report ~level:1 
143 c00d0b42 ploc
    (fun fmt -> fprintf fmt "@[<v>.. parsing file %s@," source_name);
144 0cbf0839 ploc
  let prog =
145
    try
146
      Parse.prog Parser_lustre.prog Lexer_lustre.token lexbuf
147 6affc9f5 xthirioux
    with
148
    | (Lexer_lustre.Error err) | (Parse.Syntax_err err) as exc -> 
149 0cbf0839 ploc
      Parse.report_error err;
150 6affc9f5 xthirioux
      raise exc
151 d3e4c22f xthirioux
    | Corelang.Error (loc, err) as exc ->
152 90028770 ploc
      eprintf "Parsing error %a%a@."
153
	Corelang.pp_error err
154
	Location.pp_loc loc;
155 0cbf0839 ploc
      raise exc
156
  in
157 59294251 ploc
158 f22632aa ploc
  (* Extracting dependencies *)
159 9aaee7f9 xthirioux
  Log.report ~level:1 (fun fmt -> fprintf fmt "@[<v 2>.. extracting dependencies@,");
160 f22632aa ploc
  let dependencies = 
161 0cbf0839 ploc
    List.fold_right 
162 0038002e ploc
      (fun d accu -> match d.top_decl_desc with 
163
      | Open (local, s) -> (s, local)::accu 
164 f22632aa ploc
      | _ -> accu) 
165 0cbf0839 ploc
      prog [] 
166
  in
167 c00d0b42 ploc
  let dependencies, type_env, clock_env =
168
    List.fold_left (fun (compilation_dep, type_env, clock_env) (s, local) -> 
169 f22632aa ploc
      try
170 c00d0b42 ploc
	let basename = (if local then s else Version.prefix ^ "/include/lustrec/" ^ s ) ^ ".lusi" in 
171 9aaee7f9 xthirioux
	Log.report ~level:1 (fun fmt -> fprintf fmt "@[<v 0>Library %s@," basename);
172 59294251 ploc
	let comp_dep, lusi_type_env, lusi_clock_env = check_lusi (load_lusi false basename) in 
173 9aaee7f9 xthirioux
	Log.report ~level:1 (fun fmt -> fprintf fmt "@]@ ");
174 45c0d258 ploc
	
175 59294251 ploc
	(s, local, comp_dep)::compilation_dep,
176
	Env.overwrite type_env lusi_type_env,
177
	Env.overwrite clock_env lusi_clock_env      
178 f22632aa ploc
      with Sys_error msg -> (
179 90028770 ploc
	eprintf "Failure: impossible to load library %s.@.%s@." s msg;
180 f22632aa ploc
	exit 1
181
      )
182 c00d0b42 ploc
    )  ([], Basic_library.type_env, Basic_library.clock_env) dependencies
183 f22632aa ploc
  in
184 9aaee7f9 xthirioux
  Log.report ~level:1 (fun fmt -> fprintf fmt "@]@ ");
185 59294251 ploc
  
186 0cbf0839 ploc
  (* Sorting nodes *)
187
  let prog = SortProg.sort prog in
188 7ecdb0aa xthirioux
189 0cbf0839 ploc
  (* Typing *)
190 f22632aa ploc
  let computed_types_env = type_decls type_env prog in
191 0cbf0839 ploc
  
192
  (* Clock calculus *)
193 f22632aa ploc
  let computed_clocks_env = clock_decls clock_env prog in
194 0cbf0839 ploc
195 2cf39a8e xthirioux
  (* Checking stateless/stateful status *)
196
  check_stateless_decls prog;
197
198 c02d255e ploc
  (* Perform global inlining *)
199
  let prog =
200
    if !Options.global_inline && 
201
      (match !Options.main_node with | "" -> false | _ -> true) then
202 2842f7ca ploc
      Inliner.global_inline basename prog type_env clock_env
203 c02d255e ploc
    else
204
      prog
205
  in
206
207 0cbf0839 ploc
  (* Delay calculus *)
208 f22632aa ploc
  (*
209
    if(!Options.delay_calculus)
210
    then
211 0cbf0839 ploc
    begin
212 9aaee7f9 xthirioux
    Log.report ~level:1 (fun fmt -> fprintf fmt ".. initialisation analysis@?");
213 f22632aa ploc
    try
214
    Delay_calculus.delay_prog Basic_library.delay_env prog
215
    with (Delay.Error (loc,err)) as exc ->
216
    Location.print loc;
217
    eprintf "%a" Delay.pp_error err;
218
    Utils.track_exception ();
219
    raise exc
220 0cbf0839 ploc
    end;
221 f22632aa ploc
  *)
222 0cbf0839 ploc
  (*
223
    eprintf "Causality analysis@.@?";
224 f22632aa ploc
    (* Causality analysis *)
225 0cbf0839 ploc
    begin
226
    try
227
    Causality.check_causal_prog prog
228
    with (Causality.Cycle v) as exc ->
229
    Causality.pp_error err_formatter v;
230
    raise exc
231
    end;
232
  *)
233 8b3afe43 xthirioux
234 59294251 ploc
  (* Compatibility with Lusi *)
235 8b3afe43 xthirioux
  (* Checking the existence of a lusi (Lustre Interface file) *)
236
  let lusi_name = basename ^ ".lusi" in
237 59294251 ploc
  load_n_check_lusi source_name lusi_name prog computed_types_env computed_clocks_env;
238 8b3afe43 xthirioux
239 0cbf0839 ploc
  (* Computes and stores generic calls for each node,
240
     only useful for ANSI C90 compliant generic node compilation *)
241
  if !Options.ansi then Causality.NodeDep.compute_generic_calls prog;
242
  (*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;*)
243
244
  (* Normalization phase *)
245 9aaee7f9 xthirioux
  Log.report ~level:1 (fun fmt -> fprintf fmt ".. normalization@,");
246 49ddf66d ploc
  (* Special treatment of arrows in lustre backend. We want to keep them *)
247
  if !Options.output = "lustre" then
248
    Normalization.unfold_arrow_active := false;
249 59294251 ploc
  let prog = Normalization.normalize_prog prog in
250 9aaee7f9 xthirioux
  Log.report ~level:2 (fun fmt -> fprintf fmt "@[<v 2>@ %a@]@," Printers.pp_prog prog);
251 59294251 ploc
252 0cbf0839 ploc
  (* Checking array accesses *)
253
  if !Options.check then
254
    begin
255 9aaee7f9 xthirioux
      Log.report ~level:1 (fun fmt -> fprintf fmt ".. array access checks@,");
256 59294251 ploc
      Access.check_prog prog;
257 0cbf0839 ploc
    end;
258
259 9aaee7f9 xthirioux
  (* Computation of node equation scheduling. It also breaks dependency cycles
260
     and warns about unused input or memory variables *)
261
  Log.report ~level:1 (fun fmt -> fprintf fmt ".. scheduling@,");
262
  let prog, node_schs = Scheduling.schedule_prog prog in
263
  Log.report ~level:1 (fun fmt -> fprintf fmt "@[<v 2>@ %a@]@," Scheduling.pp_warning_unused node_schs);
264 d96d54ac xthirioux
  Log.report ~level:2 (fun fmt -> fprintf fmt "@[<v 2>@ %a@]@," Scheduling.pp_schedule node_schs);
265
  Log.report ~level:2 (fun fmt -> fprintf fmt "@[<v 2>@ %a@]@," Scheduling.pp_fanin_table node_schs);
266 9aaee7f9 xthirioux
  Log.report ~level:2 (fun fmt -> fprintf fmt "@[<v 2>@ %a@]@," Printers.pp_prog prog);
267 59294251 ploc
268
 (* Optimization of prog: 
269
    - Unfold consts 
270
    - eliminate trivial expressions
271
 *)
272
  let prog = 
273
    if !Options.optimization >= 2 then 
274
      Optimize_prog.prog_unfold_consts prog 
275
    else
276
      prog
277
  in
278
279 0cbf0839 ploc
  (* DFS with modular code generation *)
280 9aaee7f9 xthirioux
  Log.report ~level:1 (fun fmt -> fprintf fmt ".. machines generation@,");
281 59294251 ploc
  let machine_code = Machine_code.translate_prog prog node_schs in
282 7cd31331 xthirioux
 (* experimental
283
  Log.report ~level:1 (fun fmt -> fprintf fmt ".. machines optimization@,");
284
  let machine_code = Machine_code.prog_reuse_var machine_code node_schs in
285
  *)
286 9aaee7f9 xthirioux
  Log.report ~level:2 (fun fmt -> fprintf fmt "@[<v 2>@ %a@]@,"
287 0cbf0839 ploc
    (Utils.fprintf_list ~sep:"@ " Machine_code.pp_machine)
288
    machine_code);
289 59294251 ploc
290 7cd31331 xthirioux
 
291 59294251 ploc
  (* Optimize machine code *)
292
  let machine_code = 
293
    if !Options.optimization >= 2 then
294
      Optimize_machine.optimize_machines machine_code
295
    else
296
      machine_code
297
  in
298 9bdfc99f xthirioux
  
299
  (* Creating destination directory if needed *)
300
  if not (Sys.file_exists !Options.dest_dir) then (
301 9aaee7f9 xthirioux
    Log.report ~level:1 (fun fmt -> fprintf fmt ".. creating destination directory@,");
302 9bdfc99f xthirioux
    Unix.mkdir !Options.dest_dir (Unix.stat ".").Unix.st_perm
303
  );
304
  if (Unix.stat !Options.dest_dir).Unix.st_kind <> Unix.S_DIR then (
305 90028770 ploc
    eprintf "Failure: destination %s is not a directory.@.@." !Options.dest_dir;
306 9bdfc99f xthirioux
    exit 1
307
  );
308 0cbf0839 ploc
  (* Printing code *)
309 8b3afe43 xthirioux
  let basename    =  Filename.basename basename in
310 c518d082 xthirioux
  let destname = !Options.dest_dir ^ "/" ^ basename in
311 f6923c9e ploc
  let _ = match !Options.output with
312
      "C" -> 
313
	begin
314 8b3afe43 xthirioux
	  let header_file = destname ^ ".h" in (* Could be changed *)
315 e95470b3 ploc
	  let source_lib_file = destname ^ ".c" in (* Could be changed *)
316
	  let source_main_file = destname ^ "_main.c" in (* Could be changed *)
317 8b3afe43 xthirioux
	  let makefile_file = destname ^ ".makefile" in (* Could be changed *)
318 e95470b3 ploc
	  (* let spec_file_opt = if !Options.c_spec then  *)
319
	  (*     ( *)
320
	  (* 	let spec_file = basename ^ "_spec.c" in *)
321 9aaee7f9 xthirioux
	  (* 	Log.report ~level:1 (fun fmt -> fprintf fmt ".. opening files %s, %s and %s@," header_file source_file spec_file); *)
322 e95470b3 ploc
	  (* 	Some spec_file  *)
323
	  (*     ) else ( *)
324 9aaee7f9 xthirioux
	  (* 	Log.report ~level:1 (fun fmt -> fprintf fmt ".. opening files %s and %s@," header_file source_file); *)
325 e95470b3 ploc
	  (* 	None  *)
326
	  (*      ) *)
327
	  (* in  *)
328 9aaee7f9 xthirioux
	  Log.report ~level:1 (fun fmt -> fprintf fmt ".. C code generation@,");
329 e95470b3 ploc
	  C_backend.translate_to_c 
330
	    header_file source_lib_file source_main_file makefile_file
331
	    basename prog machine_code dependencies
332 f6923c9e ploc
	end
333 8b3afe43 xthirioux
    | "java" ->
334
      begin
335
	failwith "Sorry, but not yet supported !"
336 0cbf0839 ploc
    (*let source_file = basename ^ ".java" in
337 9aaee7f9 xthirioux
      Log.report ~level:1 (fun fmt -> fprintf fmt ".. opening file %s@,@?" source_file);
338 f22632aa ploc
      let source_out = open_out source_file in
339
      let source_fmt = formatter_of_out_channel source_out in
340 9aaee7f9 xthirioux
      Log.report ~level:1 (fun fmt -> fprintf fmt ".. java code generation@,@?");
341 f22632aa ploc
      Java_backend.translate_to_java source_fmt basename normalized_prog machine_code;*)
342 8b3afe43 xthirioux
      end
343
    | "horn" ->
344
      begin
345 2842f7ca ploc
	let source_file = destname ^ ".smt2" in (* Could be changed *)
346 8b3afe43 xthirioux
	let source_out = open_out source_file in
347
	let fmt = formatter_of_out_channel source_out in
348 36454535 ploc
	Horn_backend.translate fmt basename prog machine_code;
349
	(* Tracability file if option is activated *)
350
	if !Options.horntraces then (
351
	let traces_file = destname ^ ".traces" in (* Could be changed *)
352
	let traces_out = open_out traces_file in
353
	let fmt = formatter_of_out_channel traces_out in
354
	Horn_backend.traces_file fmt basename prog machine_code	  
355
	)
356 8b3afe43 xthirioux
      end
357 49ddf66d ploc
    | "lustre" -> 
358 59294251 ploc
      begin
359
	let source_file = destname ^ ".lustrec.lus" in (* Could be changed *)
360
	let source_out = open_out source_file in
361
	let fmt = formatter_of_out_channel source_out in
362 49ddf66d ploc
	Printers.pp_prog fmt prog;
363 59294251 ploc
(*	Lustre_backend.translate fmt basename normalized_prog machine_code *)
364
	()
365 49ddf66d ploc
      end
366 59294251 ploc
367 4f3cc9f3 ploc
    | _ -> assert false
368 f6923c9e ploc
  in
369 9aaee7f9 xthirioux
  Log.report ~level:1 (fun fmt -> fprintf fmt ".. done !@ @]@.");
370 0cbf0839 ploc
  (* We stop the process here *)
371
  exit 0
372
  
373
let anonymous filename =
374
  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
375
  if ok_ext then
376
    let basename = Filename.chop_suffix filename ext in
377
    compile basename ext
378
  else
379 c518d082 xthirioux
    raise (Arg.Bad ("Can only compile *.lusi, *.lus or *.ec files"))
380 0cbf0839 ploc
381
let _ =
382
  Corelang.add_internal_funs ();
383
  try
384
    Printexc.record_backtrace true;
385
    Arg.parse Options.options anonymous usage
386
  with
387
  | Parse.Syntax_err _ | Lexer_lustre.Error _ 
388
  | Types.Error (_,_) | Clocks.Error (_,_)
389
  | Corelang.Error _ (*| Task_set.Error _*) 
390
  | Causality.Cycle _ -> exit 1
391
  | exc -> (Utils.track_exception (); raise exc)
392
393
(* Local Variables: *)
394
(* compile-command:"make -C .." *)
395
(* End: *)