Project

General

Profile

Statistics
| Branch: | Tag: | Revision:

lustrec / src / main_lustre_compiler.ml @ 7dedc5f0

History | View | Annotate | Download (15.3 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 6aeb3388 xthirioux
      eprintf "Parsing error: %a%a@."
79 90028770 ploc
	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 6aeb3388 xthirioux
   (* checking defined types are compatible with declared types*)
97
    Typing.check_typedef_compat header;
98 7ecdb0aa xthirioux
99
    (* checking type compatibility with computed types*)
100 59294251 ploc
    Typing.check_env_compat header declared_types_env computed_types_env;
101
    Typing.uneval_prog_generics prog;
102
    
103 7ecdb0aa xthirioux
    (* checking clocks compatibility with computed clocks*)
104
    Clock_calculus.check_env_compat header declared_clocks_env computed_clocks_env;
105 2cf39a8e xthirioux
    Clock_calculus.uneval_prog_generics prog;
106
107
    (* checking stateless status compatibility *)
108
    Stateless.check_compat header
109 59294251 ploc
110
    with Sys_error _ -> ( 
111
      (* Printing lusi file is necessary *)
112 9aaee7f9 xthirioux
      Log.report ~level:1 
113 59294251 ploc
	(fun fmt -> 
114
	  fprintf fmt 
115
	    ".. generating lustre interface file %s@," lusi_name);
116
      let lusi_out = open_out lusi_name in
117
      let lusi_fmt = formatter_of_out_channel lusi_out in
118
      Typing.uneval_prog_generics prog;
119
      Clock_calculus.uneval_prog_generics prog;
120
      Printers.pp_lusi_header lusi_fmt source_name prog
121
    )
122
    | (Types.Error (loc,err)) as exc ->
123
      eprintf "Type mismatch between computed type and declared type in lustre interface file: %a@."
124
	Types.pp_error err;
125
      raise exc
126
    | Clocks.Error (loc, err) as exc ->
127
      eprintf "Clock mismatch between computed clock and declared clock in lustre interface file: %a@."
128
	Clocks.pp_error err;
129
      raise exc
130
    | Stateless.Error (loc, err) as exc ->
131
      eprintf "Stateless status mismatch between defined status and declared status in lustre interface file: %a@."
132
	Stateless.pp_error err;
133
      raise exc
134 7dedc5f0 xthirioux
135
let extract_interface prog =
136
 List.fold_right
137
   (fun decl header ->
138
    match decl.top_decl_desc with
139
    | Node nd        -> { decl with top_decl_desc = ImportedNode (Corelang.get_node_interface nd) } :: header 
140
    | ImportedNode _ -> header
141
    | Consts _
142
    | Type _
143
    | Open _         -> decl :: header)
144
   prog []
145
146
let write_compiled_header (header : top_decl list) basename extension =
147
  let target_name = basename^extension in
148
  let outchan = open_out_bin target_name in
149
  begin
150
    Marshal.to_channel outchan header [];
151
    close_out outchan
152
  end
153
154
let read_compiled_header basename extension =
155
  let source_name = basename^extension in
156
  let inchan = open_in_bin source_name in
157
  let header = (Marshal.from_channel inchan : top_decl list) in
158
  begin
159
    close_in inchan;
160
    header
161
  end
162
163
let compile_header basename extension =
164
   let header_name = basename^extension in
165
   let header = load_lusi true header_name in
166
   begin
167
     ignore (check_lusi header);
168
     write_compiled_header header basename (extension^"c")
169
   end
170
171
let compile_prog basename extension =
172
173 0cbf0839 ploc
let rec compile basename extension =
174 59294251 ploc
175 f22632aa ploc
  (* Loading the input file *)
176 0cbf0839 ploc
  let source_name = basename^extension in
177
  Location.input_name := source_name;
178
  let lexbuf = Lexing.from_channel (open_in source_name) in
179
  Location.init lexbuf source_name;
180 59294251 ploc
181 0cbf0839 ploc
  (* Parsing *)
182 9aaee7f9 xthirioux
  Log.report ~level:1 
183 c00d0b42 ploc
    (fun fmt -> fprintf fmt "@[<v>.. parsing file %s@," source_name);
184 0cbf0839 ploc
  let prog =
185
    try
186
      Parse.prog Parser_lustre.prog Lexer_lustre.token lexbuf
187 6affc9f5 xthirioux
    with
188
    | (Lexer_lustre.Error err) | (Parse.Syntax_err err) as exc -> 
189 0cbf0839 ploc
      Parse.report_error err;
190 6affc9f5 xthirioux
      raise exc
191 d3e4c22f xthirioux
    | Corelang.Error (loc, err) as exc ->
192 90028770 ploc
      eprintf "Parsing error %a%a@."
193
	Corelang.pp_error err
194
	Location.pp_loc loc;
195 0cbf0839 ploc
      raise exc
196
  in
197 59294251 ploc
198 f22632aa ploc
  (* Extracting dependencies *)
199 9aaee7f9 xthirioux
  Log.report ~level:1 (fun fmt -> fprintf fmt "@[<v 2>.. extracting dependencies@,");
200 f22632aa ploc
  let dependencies = 
201 0cbf0839 ploc
    List.fold_right 
202 0038002e ploc
      (fun d accu -> match d.top_decl_desc with 
203
      | Open (local, s) -> (s, local)::accu 
204 f22632aa ploc
      | _ -> accu) 
205 0cbf0839 ploc
      prog [] 
206
  in
207 c00d0b42 ploc
  let dependencies, type_env, clock_env =
208
    List.fold_left (fun (compilation_dep, type_env, clock_env) (s, local) -> 
209 f22632aa ploc
      try
210 c00d0b42 ploc
	let basename = (if local then s else Version.prefix ^ "/include/lustrec/" ^ s ) ^ ".lusi" in 
211 9aaee7f9 xthirioux
	Log.report ~level:1 (fun fmt -> fprintf fmt "@[<v 0>Library %s@," basename);
212 59294251 ploc
	let comp_dep, lusi_type_env, lusi_clock_env = check_lusi (load_lusi false basename) in 
213 9aaee7f9 xthirioux
	Log.report ~level:1 (fun fmt -> fprintf fmt "@]@ ");
214 45c0d258 ploc
	
215 59294251 ploc
	(s, local, comp_dep)::compilation_dep,
216
	Env.overwrite type_env lusi_type_env,
217
	Env.overwrite clock_env lusi_clock_env      
218 f22632aa ploc
      with Sys_error msg -> (
219 90028770 ploc
	eprintf "Failure: impossible to load library %s.@.%s@." s msg;
220 f22632aa ploc
	exit 1
221
      )
222 c00d0b42 ploc
    )  ([], Basic_library.type_env, Basic_library.clock_env) dependencies
223 f22632aa ploc
  in
224 9aaee7f9 xthirioux
  Log.report ~level:1 (fun fmt -> fprintf fmt "@]@ ");
225 59294251 ploc
  
226 0cbf0839 ploc
  (* Sorting nodes *)
227
  let prog = SortProg.sort prog in
228 7ecdb0aa xthirioux
229 0cbf0839 ploc
  (* Typing *)
230 f22632aa ploc
  let computed_types_env = type_decls type_env prog in
231 0cbf0839 ploc
  
232
  (* Clock calculus *)
233 f22632aa ploc
  let computed_clocks_env = clock_decls clock_env prog in
234 0cbf0839 ploc
235 2cf39a8e xthirioux
  (* Checking stateless/stateful status *)
236
  check_stateless_decls prog;
237
238 c02d255e ploc
  (* Perform global inlining *)
239
  let prog =
240
    if !Options.global_inline && 
241
      (match !Options.main_node with | "" -> false | _ -> true) then
242 2842f7ca ploc
      Inliner.global_inline basename prog type_env clock_env
243 c02d255e ploc
    else
244
      prog
245
  in
246
247 0cbf0839 ploc
  (* Delay calculus *)
248 f22632aa ploc
  (*
249
    if(!Options.delay_calculus)
250
    then
251 0cbf0839 ploc
    begin
252 9aaee7f9 xthirioux
    Log.report ~level:1 (fun fmt -> fprintf fmt ".. initialisation analysis@?");
253 f22632aa ploc
    try
254
    Delay_calculus.delay_prog Basic_library.delay_env prog
255
    with (Delay.Error (loc,err)) as exc ->
256
    Location.print loc;
257
    eprintf "%a" Delay.pp_error err;
258
    Utils.track_exception ();
259
    raise exc
260 0cbf0839 ploc
    end;
261 f22632aa ploc
  *)
262 0cbf0839 ploc
  (*
263
    eprintf "Causality analysis@.@?";
264 f22632aa ploc
    (* Causality analysis *)
265 0cbf0839 ploc
    begin
266
    try
267
    Causality.check_causal_prog prog
268
    with (Causality.Cycle v) as exc ->
269
    Causality.pp_error err_formatter v;
270
    raise exc
271
    end;
272
  *)
273 8b3afe43 xthirioux
274 59294251 ploc
  (* Compatibility with Lusi *)
275 8b3afe43 xthirioux
  (* Checking the existence of a lusi (Lustre Interface file) *)
276
  let lusi_name = basename ^ ".lusi" in
277 59294251 ploc
  load_n_check_lusi source_name lusi_name prog computed_types_env computed_clocks_env;
278 8b3afe43 xthirioux
279 0cbf0839 ploc
  (* Computes and stores generic calls for each node,
280
     only useful for ANSI C90 compliant generic node compilation *)
281
  if !Options.ansi then Causality.NodeDep.compute_generic_calls prog;
282
  (*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;*)
283
284
  (* Normalization phase *)
285 9aaee7f9 xthirioux
  Log.report ~level:1 (fun fmt -> fprintf fmt ".. normalization@,");
286 49ddf66d ploc
  (* Special treatment of arrows in lustre backend. We want to keep them *)
287
  if !Options.output = "lustre" then
288
    Normalization.unfold_arrow_active := false;
289 59294251 ploc
  let prog = Normalization.normalize_prog prog in
290 9aaee7f9 xthirioux
  Log.report ~level:2 (fun fmt -> fprintf fmt "@[<v 2>@ %a@]@," Printers.pp_prog prog);
291 59294251 ploc
292 0cbf0839 ploc
  (* Checking array accesses *)
293
  if !Options.check then
294
    begin
295 9aaee7f9 xthirioux
      Log.report ~level:1 (fun fmt -> fprintf fmt ".. array access checks@,");
296 59294251 ploc
      Access.check_prog prog;
297 0cbf0839 ploc
    end;
298
299 9aaee7f9 xthirioux
  (* Computation of node equation scheduling. It also breaks dependency cycles
300
     and warns about unused input or memory variables *)
301
  Log.report ~level:1 (fun fmt -> fprintf fmt ".. scheduling@,");
302
  let prog, node_schs = Scheduling.schedule_prog prog in
303 01f1a1f4 xthirioux
  Log.report ~level:3 (fun fmt -> fprintf fmt "@[<v 2>@ %a@]@," Scheduling.pp_warning_unused node_schs);
304
  Log.report ~level:3 (fun fmt -> fprintf fmt "@[<v 2>@ %a@]@," Scheduling.pp_schedule node_schs);
305
  Log.report ~level:3 (fun fmt -> fprintf fmt "@[<v 2>@ %a@]@," Scheduling.pp_fanin_table node_schs);
306
  Log.report ~level:3 (fun fmt -> fprintf fmt "@[<v 2>@ %a@]@," Printers.pp_prog prog);
307 59294251 ploc
308
 (* Optimization of prog: 
309
    - Unfold consts 
310
    - eliminate trivial expressions
311
 *)
312
  let prog = 
313
    if !Options.optimization >= 2 then 
314
      Optimize_prog.prog_unfold_consts prog 
315
    else
316
      prog
317
  in
318
319 0cbf0839 ploc
  (* DFS with modular code generation *)
320 9aaee7f9 xthirioux
  Log.report ~level:1 (fun fmt -> fprintf fmt ".. machines generation@,");
321 59294251 ploc
  let machine_code = Machine_code.translate_prog prog node_schs in
322
323
  (* Optimize machine code *)
324
  let machine_code = 
325 d52e7821 xthirioux
    if !Options.optimization >= 3 && !Options.output <> "horn" then
326 01f1a1f4 xthirioux
      begin
327
	Log.report ~level:1 (fun fmt -> fprintf fmt ".. machines optimization@,");
328
	Optimize_machine.machines_reuse_variables machine_code node_schs
329
      end
330 59294251 ploc
    else
331
      machine_code
332 6aeb3388 xthirioux
 in  
333
  let machine_code = 
334
    if !Options.optimization >= 3 && !Options.output <> "horn" then
335
      begin
336
	Log.report ~level:1 (fun fmt -> fprintf fmt ".. machines optimization@,");
337
	Optimize_machine.machines_fusion machine_code
338
      end
339
    else
340
      machine_code
341 01f1a1f4 xthirioux
 in
342
  Log.report ~level:3 (fun fmt -> fprintf fmt "@[<v 2>@ %a@]@,"
343
  (Utils.fprintf_list ~sep:"@ " Machine_code.pp_machine)
344
  machine_code);
345
346 9bdfc99f xthirioux
  (* Creating destination directory if needed *)
347
  if not (Sys.file_exists !Options.dest_dir) then (
348 9aaee7f9 xthirioux
    Log.report ~level:1 (fun fmt -> fprintf fmt ".. creating destination directory@,");
349 9bdfc99f xthirioux
    Unix.mkdir !Options.dest_dir (Unix.stat ".").Unix.st_perm
350
  );
351
  if (Unix.stat !Options.dest_dir).Unix.st_kind <> Unix.S_DIR then (
352 90028770 ploc
    eprintf "Failure: destination %s is not a directory.@.@." !Options.dest_dir;
353 9bdfc99f xthirioux
    exit 1
354
  );
355 0cbf0839 ploc
  (* Printing code *)
356 8b3afe43 xthirioux
  let basename    =  Filename.basename basename in
357 c518d082 xthirioux
  let destname = !Options.dest_dir ^ "/" ^ basename in
358 f6923c9e ploc
  let _ = match !Options.output with
359
      "C" -> 
360
	begin
361 8b3afe43 xthirioux
	  let header_file = destname ^ ".h" in (* Could be changed *)
362 e95470b3 ploc
	  let source_lib_file = destname ^ ".c" in (* Could be changed *)
363
	  let source_main_file = destname ^ "_main.c" in (* Could be changed *)
364 8b3afe43 xthirioux
	  let makefile_file = destname ^ ".makefile" in (* Could be changed *)
365 e95470b3 ploc
	  (* let spec_file_opt = if !Options.c_spec then  *)
366
	  (*     ( *)
367
	  (* 	let spec_file = basename ^ "_spec.c" in *)
368 9aaee7f9 xthirioux
	  (* 	Log.report ~level:1 (fun fmt -> fprintf fmt ".. opening files %s, %s and %s@," header_file source_file spec_file); *)
369 e95470b3 ploc
	  (* 	Some spec_file  *)
370
	  (*     ) else ( *)
371 9aaee7f9 xthirioux
	  (* 	Log.report ~level:1 (fun fmt -> fprintf fmt ".. opening files %s and %s@," header_file source_file); *)
372 e95470b3 ploc
	  (* 	None  *)
373
	  (*      ) *)
374
	  (* in  *)
375 9aaee7f9 xthirioux
	  Log.report ~level:1 (fun fmt -> fprintf fmt ".. C code generation@,");
376 e95470b3 ploc
	  C_backend.translate_to_c 
377
	    header_file source_lib_file source_main_file makefile_file
378
	    basename prog machine_code dependencies
379 f6923c9e ploc
	end
380 8b3afe43 xthirioux
    | "java" ->
381
      begin
382
	failwith "Sorry, but not yet supported !"
383 0cbf0839 ploc
    (*let source_file = basename ^ ".java" in
384 9aaee7f9 xthirioux
      Log.report ~level:1 (fun fmt -> fprintf fmt ".. opening file %s@,@?" source_file);
385 f22632aa ploc
      let source_out = open_out source_file in
386
      let source_fmt = formatter_of_out_channel source_out in
387 9aaee7f9 xthirioux
      Log.report ~level:1 (fun fmt -> fprintf fmt ".. java code generation@,@?");
388 f22632aa ploc
      Java_backend.translate_to_java source_fmt basename normalized_prog machine_code;*)
389 8b3afe43 xthirioux
      end
390
    | "horn" ->
391
      begin
392 2842f7ca ploc
	let source_file = destname ^ ".smt2" in (* Could be changed *)
393 8b3afe43 xthirioux
	let source_out = open_out source_file in
394
	let fmt = formatter_of_out_channel source_out in
395 36454535 ploc
	Horn_backend.translate fmt basename prog machine_code;
396
	(* Tracability file if option is activated *)
397
	if !Options.horntraces then (
398
	let traces_file = destname ^ ".traces" in (* Could be changed *)
399
	let traces_out = open_out traces_file in
400
	let fmt = formatter_of_out_channel traces_out in
401
	Horn_backend.traces_file fmt basename prog machine_code	  
402
	)
403 8b3afe43 xthirioux
      end
404 49ddf66d ploc
    | "lustre" -> 
405 59294251 ploc
      begin
406
	let source_file = destname ^ ".lustrec.lus" in (* Could be changed *)
407
	let source_out = open_out source_file in
408
	let fmt = formatter_of_out_channel source_out in
409 49ddf66d ploc
	Printers.pp_prog fmt prog;
410 59294251 ploc
(*	Lustre_backend.translate fmt basename normalized_prog machine_code *)
411
	()
412 49ddf66d ploc
      end
413 59294251 ploc
414 4f3cc9f3 ploc
    | _ -> assert false
415 f6923c9e ploc
  in
416 9aaee7f9 xthirioux
  Log.report ~level:1 (fun fmt -> fprintf fmt ".. done !@ @]@.");
417 0cbf0839 ploc
  (* We stop the process here *)
418
  exit 0
419
  
420
let anonymous filename =
421
  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
422
  if ok_ext then
423
    let basename = Filename.chop_suffix filename ext in
424
    compile basename ext
425
  else
426 c518d082 xthirioux
    raise (Arg.Bad ("Can only compile *.lusi, *.lus or *.ec files"))
427 0cbf0839 ploc
428
let _ =
429
  Corelang.add_internal_funs ();
430
  try
431
    Printexc.record_backtrace true;
432
    Arg.parse Options.options anonymous usage
433
  with
434
  | Parse.Syntax_err _ | Lexer_lustre.Error _ 
435
  | Types.Error (_,_) | Clocks.Error (_,_)
436
  | Corelang.Error _ (*| Task_set.Error _*) 
437
  | Causality.Cycle _ -> exit 1
438
  | exc -> (Utils.track_exception (); raise exc)
439
440
(* Local Variables: *)
441
(* compile-command:"make -C .." *)
442
(* End: *)