Project

General

Profile

Statistics
| Branch: | Tag: | Revision:

lustrec / src / main_lustre_compiler.ml @ cf9cc6f9

History | View | Annotate | Download (13.7 KB)

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 dirname basename extension =
25
  let header = Lusic.extract_header dirname basename prog in
26
  let header_name = dirname ^ "/" ^ 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
    Typing.uneval_prog_generics header;
31
    Clock_calculus.uneval_prog_generics header;
32
    Printers.pp_lusi_header h_fmt basename header;
33
    close_out h_out
34
  end
35

    
36
(* compile a .lusi header file *)
37
let compile_header dirname  basename extension =
38
  let destname = !Options.dest_dir ^ "/" ^ basename in
39
  let header_name = basename ^ extension in
40
  let lusic_ext = extension ^ "c" in
41
  begin
42
    Log.report ~level:1 (fun fmt -> fprintf fmt "@[<v>");
43
    let header = parse_header true (dirname ^ "/" ^ header_name) in
44
    ignore (Modules.load_header ISet.empty header);
45
    ignore (check_top_decls header);
46
    create_dest_dir ();
47
    Log.report ~level:1
48
      (fun fmt -> fprintf fmt ".. generating compiled header file %sc@," (destname ^ extension));
49
    Lusic.write_lusic true header destname lusic_ext;
50
    Lusic.print_lusic_to_h destname lusic_ext;
51
    Log.report ~level:1 (fun fmt -> fprintf fmt ".. done !@ @]@.")
52
  end
53

    
54
(* check whether a source file has a compiled header,
55
   if not, generate the compiled header *)
56
let compile_source_to_header prog computed_types_env computed_clocks_env dirname basename extension =
57
  let destname = !Options.dest_dir ^ "/" ^ basename in
58
  let lusic_ext = extension ^ "c" in
59
  let header_name = destname ^ lusic_ext in
60
  begin
61
    if not (Sys.file_exists header_name) then
62
      begin
63
	Log.report ~level:1 (fun fmt -> fprintf fmt ".. generating compiled header file %s@," header_name);
64
	Lusic.write_lusic false (Lusic.extract_header dirname basename prog) destname lusic_ext;
65
	Lusic.print_lusic_to_h destname lusic_ext
66
      end
67
    else
68
      let lusic = Lusic.read_lusic destname lusic_ext in
69
      if not lusic.Lusic.from_lusi then
70
	begin
71
	  Log.report ~level:1 (fun fmt -> fprintf fmt ".. generating compiled header file %s@," header_name);
72
       	  Lusic.write_lusic false (Lusic.extract_header dirname basename prog) destname lusic_ext;
73
	  (*List.iter (fun top_decl -> Format.eprintf "lusic: %a@." Printers.pp_decl top_decl) lusic.Lusic.contents;*)
74
	  Lusic.print_lusic_to_h destname lusic_ext
75
	end
76
      else
77
	begin
78
	  Log.report ~level:1 (fun fmt -> fprintf fmt ".. loading compiled header file %s@," header_name);
79
	  Modules.check_dependency lusic destname;
80
	  let header = lusic.Lusic.contents in
81
	  let (declared_types_env, declared_clocks_env) = get_envs_from_top_decls header in
82
	  check_compatibility
83
	    (prog, computed_types_env, computed_clocks_env)
84
	    (header, declared_types_env, declared_clocks_env)
85
	end
86
  end
87

    
88

    
89
let functional_backend () = 
90
  match !Options.output with
91
  | "horn" | "lustre" | "acsl" -> true
92
  | _ -> false
93

    
94
(* compile a .lus source file *)
95
let rec compile_source dirname basename extension =
96

    
97
  Log.report ~level:1 (fun fmt -> fprintf fmt "@[<v>");
98

    
99
  (* Parsing source *)
100
  let prog = parse_source (dirname ^ "/" ^ basename ^ extension) in
101

    
102
  (* Removing automata *)
103
  let prog = Automata.expand_decls prog in
104

    
105
  (* Importing source *)
106
  let _ = Modules.load_program ISet.empty prog in
107

    
108
  (* Extracting dependencies *)
109
  let dependencies, type_env, clock_env = import_dependencies prog in
110

    
111
  (* Sorting nodes *)
112
  let prog = SortProg.sort prog in
113

    
114
  (* Perform inlining before any analysis *)
115
  let orig, prog =
116
    if !Options.global_inline && !Options.main_node <> "" then
117
      (if !Options.witnesses then prog else []),
118
      Inliner.global_inline basename prog type_env clock_env
119
    else (* if !Option.has_local_inline *)
120
      [],
121
      Inliner.local_inline basename prog type_env clock_env
122
  in
123

    
124
  (* Checking stateless/stateful status *)
125
  check_stateless_decls prog;
126

    
127
  (* Typing *)
128
  let computed_types_env = type_decls type_env prog in
129

    
130
  (* Clock calculus *)
131
  let computed_clocks_env = clock_decls clock_env prog in
132

    
133
  (* Generating a .lusi header file only *)
134
  if !Options.lusi then
135
    begin
136
      let lusi_ext = extension ^ "i" in
137
      Log.report ~level:1 (fun fmt -> fprintf fmt ".. generating interface file %s@," (dirname ^ "/" ^ basename ^ lusi_ext));
138
      print_lusi prog dirname basename lusi_ext;
139
      Log.report ~level:1 (fun fmt -> fprintf fmt ".. done !@ @]@.");
140
      exit 0
141
    end;
142

    
143
  (* Delay calculus *)
144
  (* TO BE DONE LATER (Xavier)
145
    if(!Options.delay_calculus)
146
    then
147
    begin
148
    Log.report ~level:1 (fun fmt -> fprintf fmt ".. initialisation analysis@?");
149
    try
150
    Delay_calculus.delay_prog Basic_library.delay_env prog
151
    with (Delay.Error (loc,err)) as exc ->
152
    Location.print loc;
153
    eprintf "%a" Delay.pp_error err;
154
    Utils.track_exception ();
155
    raise exc
156
    end;
157
  *)
158

    
159
  (* Creating destination directory if needed *)
160
  create_dest_dir ();
161

    
162
  (* Compatibility with Lusi *)
163
  (* Checking the existence of a lusi (Lustre Interface file) *)
164
  (match !Options.output with
165
    "C" ->
166
      begin
167
        let extension = ".lusi" in
168
        compile_source_to_header prog computed_types_env computed_clocks_env dirname basename extension;
169
      end
170
   |_ -> ());
171

    
172
  Typing.uneval_prog_generics prog;
173
  Clock_calculus.uneval_prog_generics prog;
174

    
175
  if !Options.global_inline && !Options.main_node <> "" && !Options.witnesses then
176
    begin
177
      let orig = Corelang.copy_prog orig in
178
      Log.report ~level:1 (fun fmt -> fprintf fmt ".. generating witness file@,");
179
      check_stateless_decls orig;
180
      let _ = Typing.type_prog type_env orig in
181
      let _ = Clock_calculus.clock_prog clock_env orig in
182
      Typing.uneval_prog_generics orig;
183
      Clock_calculus.uneval_prog_generics orig;
184
      Inliner.witness
185
	basename
186
	!Options.main_node
187
	orig prog type_env clock_env
188
    end;
189

    
190
(*Format.eprintf "Inliner.global_inline<<@.%a@.>>@." Printers.pp_prog prog;*)
191
  (* Computes and stores generic calls for each node,
192
     only useful for ANSI C90 compliant generic node compilation *)
193
  if !Options.ansi then Causality.NodeDep.compute_generic_calls prog;
194
  (*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;*)
195

    
196
  (* Normalization phase *)
197
  Log.report ~level:1 (fun fmt -> fprintf fmt ".. normalization@,");
198
  (* Special treatment of arrows in lustre backend. We want to keep them *)
199
  if !Options.output = "lustre" then
200
    Normalization.unfold_arrow_active := false;
201
  let prog = Normalization.normalize_prog prog in
202

    
203
  Log.report ~level:2 (fun fmt -> fprintf fmt "@[<v 2>@ %a@]@," Printers.pp_prog prog);
204
  (* Checking array accesses *)
205
  if !Options.check then
206
    begin
207
      Log.report ~level:1 (fun fmt -> fprintf fmt ".. array access checks@,");
208
      Access.check_prog prog;
209
    end;
210

    
211
  (* Computation of node equation scheduling. It also breaks dependency cycles
212
     and warns about unused input or memory variables *)
213
  Log.report ~level:1 (fun fmt -> fprintf fmt ".. scheduling@,");
214
  let prog, node_schs = Scheduling.schedule_prog prog in
215
  Log.report ~level:1 (fun fmt -> fprintf fmt "@[<v 2>@ %a@]@," Scheduling.pp_warning_unused node_schs);
216
  Log.report ~level:3 (fun fmt -> fprintf fmt "@[<v 2>@ %a@]@," Scheduling.pp_schedule node_schs);
217
  Log.report ~level:3 (fun fmt -> fprintf fmt "@[<v 2>@ %a@]@," Scheduling.pp_fanin_table node_schs);
218
  Log.report ~level:3 (fun fmt -> fprintf fmt "@[<v 2>@ %a@]@," Printers.pp_prog prog);
219

    
220
 (* Optimization of prog:
221
    - Unfold consts
222
    - eliminate trivial expressions
223
 *)
224
  let prog =
225
    if !Options.optimization >= 5 then
226
      begin
227
	Log.report ~level:1 (fun fmt -> fprintf fmt ".. constants elimination@,");
228
	Optimize_prog.prog_unfold_consts prog
229
      end
230
    else
231
      prog
232
  in
233
  (* DFS with modular code generation *)
234
  Log.report ~level:1 (fun fmt -> fprintf fmt ".. machines generation@,");
235
  let machine_code = Machine_code.translate_prog prog node_schs in
236

    
237
  Log.report ~level:2 (fun fmt -> fprintf fmt "@[<v 2>@ %a@]@,"
238
  (Utils.fprintf_list ~sep:"@ " Machine_code.pp_machine)
239
  machine_code);
240

    
241
  (* Optimize machine code *)
242
  let machine_code =
243
    if !Options.optimization >= 4 (* && !Options.output <> "horn" *) then
244
      begin
245
	Log.report ~level:1 
246
	  (fun fmt -> fprintf fmt ".. machines optimization: sub-expression elimination@,");
247
	Optimize_machine.machines_cse machine_code
248
      end
249
    else
250
      machine_code
251
  in
252

    
253
  (* Optimize machine code *)
254
  let machine_code =
255
    if !Options.optimization >= 2 (* && !Options.output <> "horn" *) then
256
      begin
257
	Log.report ~level:1 
258
	  (fun fmt -> fprintf fmt 
259
	    ".. machines optimization: const. inlining (partial eval. with const)@,");
260
	Optimize_machine.machines_unfold (Corelang.get_consts prog) node_schs machine_code
261
      end
262
    else
263
      machine_code
264
  in
265
  (* Optimize machine code *)
266
  let machine_code = (* TODO reactivate. I disabled it because output variables were removed *)
267
    if false && !Options.optimization >= 3 && not (functional_backend ()) then
268
      begin
269
	Log.report ~level:1 
270
	  (fun fmt -> fprintf fmt ".. machines optimization: minimize heap alloc by reusing vars@,");
271
	Optimize_machine.machines_fusion 
272
	  (Optimize_machine.machines_reuse_variables machine_code node_schs)
273
      end
274
    else
275
      machine_code
276
  in
277

    
278
  if !Options.optimization >= 2 then
279
    begin
280
      Log.report ~level:3 (fun fmt -> fprintf fmt "@[<v 2>@ %a@]@,"
281
	(Utils.fprintf_list ~sep:"@ " Machine_code.pp_machine)
282
	machine_code);
283
    end;
284

    
285
  (* Printing code *)
286
  let basename    =  Filename.basename basename in
287
  let destname = !Options.dest_dir ^ "/" ^ basename in
288
  let _ = match !Options.output with
289
    | "C" ->
290
      begin
291
	  let alloc_header_file = destname ^ "_alloc.h" in (* Could be changed *)
292
	  let source_lib_file = destname ^ ".c" in (* Could be changed *)
293
	  let source_main_file = destname ^ "_main.c" in (* Could be changed *)
294
	  let makefile_file = destname ^ ".makefile" in (* Could be changed *)
295
	  Log.report ~level:1 (fun fmt -> fprintf fmt ".. C code generation@,");
296
	  C_backend.translate_to_c
297
	    alloc_header_file source_lib_file source_main_file makefile_file
298
	    basename prog machine_code dependencies
299
	end
300
    | "java" ->
301
      begin
302
	failwith "Sorry, but not yet supported !"
303
    (*let source_file = basename ^ ".java" in
304
      Log.report ~level:1 (fun fmt -> fprintf fmt ".. opening file %s@,@?" source_file);
305
      let source_out = open_out source_file in
306
      let source_fmt = formatter_of_out_channel source_out in
307
      Log.report ~level:1 (fun fmt -> fprintf fmt ".. java code generation@,@?");
308
      Java_backend.translate_to_java source_fmt basename normalized_prog machine_code;*)
309
      end
310
    | "horn" ->
311
      begin
312
	let source_file = destname ^ ".smt2" in (* Could be changed *)
313
	let source_out = open_out source_file in
314
	let fmt = formatter_of_out_channel source_out in
315
	Log.report ~level:1 (fun fmt -> fprintf fmt ".. hornification@,");
316
        Horn_backend.translate fmt basename prog (Machine_code.arrow_machine::machine_code);
317
	(* Tracability file if option is activated *)
318
	if !Options.traces then (
319
	let traces_file = destname ^ ".traces.xml" in (* Could be changed *)
320
	let traces_out = open_out traces_file in
321
	let fmt = formatter_of_out_channel traces_out in
322
        Log.report ~level:1 (fun fmt -> fprintf fmt ".. tracing info@,");
323
	Horn_backend_traces.traces_file fmt basename prog machine_code;
324
	)
325
      end
326
    | "lustre" ->
327
      begin
328
	let source_file = destname ^ ".lustrec.lus" in (* Could be changed *)
329
	let source_out = open_out source_file in
330
	let fmt = formatter_of_out_channel source_out in
331
	Printers.pp_prog fmt prog;
332
(*	Lustre_backend.translate fmt basename normalized_prog machine_code *)
333
	()
334
      end
335

    
336
    | _ -> assert false
337
  in
338
  begin
339
    Log.report ~level:1 (fun fmt -> fprintf fmt ".. done @ @]@.");
340
  (* We stop the process here *)
341
    exit 0
342
  end
343

    
344
let compile dirname basename extension =
345
  match extension with
346
  | ".lusi"  -> compile_header dirname basename extension
347
  | ".lus"   -> compile_source dirname basename extension
348
  | _        -> assert false
349

    
350
let anonymous filename =
351
  let ok_ext, ext = List.fold_left
352
    (fun (ok, ext) ext' ->
353
      if not ok && Filename.check_suffix filename ext' then
354
	true, ext'
355
      else
356
	ok, ext)
357
    (false, "") extensions in
358
  if ok_ext then
359
    let dirname = Filename.dirname filename in
360
    let basename = Filename.chop_suffix (Filename.basename filename) ext in
361
    compile dirname basename ext
362
  else
363
    raise (Arg.Bad ("Can only compile *.lusi, *.lus or *.ec files"))
364

    
365
let _ =
366
  Corelang.add_internal_funs ();
367
  try
368
    Printexc.record_backtrace true;
369
    Arg.parse Options.options anonymous usage
370
  with
371
  | Parse.Syntax_err _ | Lexer_lustre.Error _
372
  | Types.Error (_,_) | Clocks.Error (_,_)
373
  | Corelang.Error _ (*| Task_set.Error _*)
374
  | Causality.Cycle _ -> exit 1
375
  | Sys_error msg -> (eprintf "Failure: %s@." msg)
376
  | exc -> (Utils.track_exception (); raise exc)
377

    
378
(* Local Variables: *)
379
(* compile-command:"make -C .." *)
380
(* End: *)