Project

General

Profile

Statistics
| Branch: | Tag: | Revision:

lustrec / src / main_lustre_compiler.ml @ e41592cf

History | View | Annotate | Download (13.3 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
(* compile a .lus source file *)
89
let rec compile_source dirname basename extension =
90

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

    
93
  (* Parsing source *)
94
  let prog = parse_source (dirname ^ "/" ^ basename ^ extension) in
95

    
96
  (* Removing automata *)
97
  let prog = Automata.expand_decls prog in
98

    
99
  (* Importing source *)
100
  let _ = Modules.load_program ISet.empty prog in
101

    
102
  (* Extracting dependencies *)
103
  let dependencies, type_env, clock_env = import_dependencies prog in
104

    
105
  (* Sorting nodes *)
106
  let prog = SortProg.sort prog in
107

    
108
  (* Perform inlining before any analysis *)
109
  let orig, prog =
110
    if !Options.global_inline && !Options.main_node <> "" then
111
      (if !Options.witnesses then prog else []),
112
      Inliner.global_inline basename prog type_env clock_env
113
    else (* if !Option.has_local_inline *)
114
      [],
115
      Inliner.local_inline basename prog type_env clock_env
116
  in
117

    
118
  (* Checking stateless/stateful status *)
119
  check_stateless_decls prog;
120

    
121
  (* Typing *)
122
  let computed_types_env = type_decls type_env prog in
123

    
124
  (* Clock calculus *)
125
  let computed_clocks_env = clock_decls clock_env prog in
126

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

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

    
153
  (* Creating destination directory if needed *)
154
  create_dest_dir ();
155

    
156
  (* Compatibility with Lusi *)
157
  (* Checking the existence of a lusi (Lustre Interface file) *)
158
  match !Options.output with
159
    "C" ->
160
      begin
161
        let extension = ".lusi" in
162
        compile_source_to_header prog computed_types_env computed_clocks_env dirname basename extension;
163
      end
164
   |_ -> ();
165

    
166
  Typing.uneval_prog_generics prog;
167
  Clock_calculus.uneval_prog_generics prog;
168

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

    
184
(*Format.eprintf "Inliner.global_inline<<@.%a@.>>@." Printers.pp_prog prog;*)
185
  (* Computes and stores generic calls for each node,
186
     only useful for ANSI C90 compliant generic node compilation *)
187
  if !Options.ansi then Causality.NodeDep.compute_generic_calls prog;
188
  (*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;*)
189

    
190
  (* Normalization phase *)
191
  Log.report ~level:1 (fun fmt -> fprintf fmt ".. normalization@,");
192
  (* Special treatment of arrows in lustre backend. We want to keep them *)
193
  if !Options.output = "lustre" then
194
    Normalization.unfold_arrow_active := false;
195
  let prog = Normalization.normalize_prog prog in
196

    
197
  Log.report ~level:2 (fun fmt -> fprintf fmt "@[<v 2>@ %a@]@," Printers.pp_prog prog);
198
  (* Checking array accesses *)
199
  if !Options.check then
200
    begin
201
      Log.report ~level:1 (fun fmt -> fprintf fmt ".. array access checks@,");
202
      Access.check_prog prog;
203
    end;
204

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

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

    
231
  (* Optimize machine code *)
232
  let machine_code =
233
    if !Options.optimization >= 4 && !Options.output <> "horn" then
234
      begin
235
	Log.report ~level:1 (fun fmt -> fprintf fmt ".. machines optimization (phase 3)@,");
236
	Optimize_machine.machines_cse machine_code
237
      end
238
    else
239
      machine_code
240
  in
241

    
242
  Log.report ~level:2 (fun fmt -> fprintf fmt "@[<v 2>@ %a@]@,"
243
  (Utils.fprintf_list ~sep:"@ " Machine_code.pp_machine)
244
  machine_code);
245

    
246
  (* Optimize machine code *)
247
  let machine_code =
248
    if !Options.optimization >= 2 && !Options.output <> "horn" then
249
      begin
250
	Log.report ~level:1 (fun fmt -> fprintf fmt ".. machines optimization (phase 1)@,");
251
	Optimize_machine.machines_unfold (Corelang.get_consts prog) node_schs machine_code
252
      end
253
    else
254
      machine_code
255
  in
256
  (* Optimize machine code *)
257
  let machine_code =
258
    if !Options.optimization >= 3 && !Options.output <> "horn" then
259
      begin
260
	Log.report ~level:1 (fun fmt -> fprintf fmt ".. machines optimization (phase 2)@,");
261
	Optimize_machine.machines_fusion (Optimize_machine.machines_reuse_variables machine_code node_schs)
262
      end
263
    else
264
      machine_code
265
  in
266

    
267

    
268
  Log.report ~level:3 (fun fmt -> fprintf fmt "@[<v 2>@ %a@]@,"
269
  (Utils.fprintf_list ~sep:"@ " Machine_code.pp_machine)
270
  machine_code);
271

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

    
323
    | _ -> assert false
324
  in
325
  begin
326
    Log.report ~level:1 (fun fmt -> fprintf fmt "@.. done @ @]@.");
327
  (* We stop the process here *)
328
    exit 0
329
  end
330

    
331
let compile dirname basename extension =
332
  match extension with
333
  | ".lusi"  -> compile_header dirname basename extension
334
  | ".lus"   -> compile_source dirname basename extension
335
  | _        -> assert false
336

    
337
let anonymous filename =
338
  let ok_ext, ext = List.fold_left
339
    (fun (ok, ext) ext' ->
340
      if not ok && Filename.check_suffix filename ext' then
341
	true, ext'
342
      else
343
	ok, ext)
344
    (false, "") extensions in
345
  if ok_ext then
346
    let dirname = Filename.dirname filename in
347
    let basename = Filename.chop_suffix (Filename.basename filename) ext in
348
    compile dirname basename ext
349
  else
350
    raise (Arg.Bad ("Can only compile *.lusi, *.lus or *.ec files"))
351

    
352
let _ =
353
  Corelang.add_internal_funs ();
354
  try
355
    Printexc.record_backtrace true;
356
    Arg.parse Options.options anonymous usage
357
  with
358
  | Parse.Syntax_err _ | Lexer_lustre.Error _
359
  | Types.Error (_,_) | Clocks.Error (_,_)
360
  | Corelang.Error _ (*| Task_set.Error _*)
361
  | Causality.Cycle _ -> exit 1
362
  | Sys_error msg -> (eprintf "Failure: %s@." msg)
363
  | exc -> (Utils.track_exception (); raise exc)
364

    
365
(* Local Variables: *)
366
(* compile-command:"make -C .." *)
367
(* End: *)