Project

General

Profile

Statistics
| Branch: | Tag: | Revision:

lustrec / src / main_lustre_compiler.ml @ 15003796

History | View | Annotate | Download (14.1 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

    
90
(* compile a .lus source file *)
91
let rec compile_source dirname basename extension =
92

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

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

    
98
  (* Removing automata *)
99
  let prog = Automata.expand_decls prog in
100

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

    
104
  (* Extracting dependencies *)
105
  let dependencies, type_env, clock_env = import_dependencies prog in
106

    
107
  (* Sorting nodes *)
108
  let prog = SortProg.sort prog in
109

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

    
120
  (* Checking stateless/stateful status *)
121
  check_stateless_decls prog;
122

    
123
  (* Typing *)
124
  let computed_types_env = type_decls type_env prog in
125

    
126
  (* Clock calculus *)
127
  let computed_clocks_env = clock_decls clock_env prog in
128

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

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

    
155
  (* Creating destination directory if needed *)
156
  create_dest_dir ();
157

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

    
168
  Typing.uneval_prog_generics prog;
169
  Clock_calculus.uneval_prog_generics prog;
170

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

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

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

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

    
207
  (* Apply transformation according to annotations: for the moment, linearization only *)
208
  let prog = 
209
    List.map (
210
      fun top -> 
211
	match top.top_decl_desc with
212
	| Node nd -> 
213
	  let nd = 
214
	    List.fold_left ( 
215
     	      fun nd annots ->
216
		List.fold_left (
217
		  fun nd (keyl, value) ->
218
		    match keyl, value with
219
	  	    | ["linearize"], _ ->  (* This option is activated only for lustre output *)
220
		      if !Options.output = "lustre" then
221
			Linearize.node nd value
222
		      else 
223
			nd
224
	  	    | _ -> nd (* do nothing *)
225
		) nd annots.annots
226
	    ) nd nd.node_annot
227
	  in
228
	  { top with top_decl_desc = Node nd }
229
	| _ -> top
230
    ) prog
231
  in
232

    
233

    
234
 (* Optimization of prog:
235
    - Unfold consts
236
    - eliminate trivial expressions
237
 *)
238
  let prog =
239
    if !Options.optimization >= 5 then
240
      begin
241
	Log.report ~level:1 (fun fmt -> fprintf fmt ".. constants elimination@,");
242
	Optimize_prog.prog_unfold_consts prog
243
      end
244
    else
245
      prog
246
  in
247

    
248

    
249
  let machine_code =
250
    if !Options.output = "lustre" then 
251
      None
252
    else begin
253

    
254
  (* Computation of node equation scheduling. It also breaks dependency cycles
255
     and warns about unused input or memory variables *)
256
  Log.report ~level:1 (fun fmt -> fprintf fmt ".. scheduling@,");
257
  let prog, node_schs = Scheduling.schedule_prog prog in
258
  Log.report ~level:1 (fun fmt -> fprintf fmt "@[<v 2>@ %a@]@," Scheduling.pp_warning_unused node_schs);
259
  Log.report ~level:3 (fun fmt -> fprintf fmt "@[<v 2>@ %a@]@," Scheduling.pp_schedule node_schs);
260
  Log.report ~level:3 (fun fmt -> fprintf fmt "@[<v 2>@ %a@]@," Scheduling.pp_fanin_table node_schs);
261
  Log.report ~level:3 (fun fmt -> fprintf fmt "@[<v 2>@ %a@]@," Printers.pp_prog prog);
262
      
263
  (* DFS with modular code generation *)
264
  Log.report ~level:1 (fun fmt -> fprintf fmt ".. machines generation@,");
265
  let machine_code = Machine_code.translate_prog prog node_schs in
266

    
267
  (* Optimize machine code *)
268
  let machine_code =
269
    if !Options.optimization >= 4 && !Options.output <> "horn" then
270
      begin
271
	Log.report ~level:1 (fun fmt -> fprintf fmt ".. machines optimization (phase 3)@,");
272
	Optimize_machine.machines_cse machine_code
273
      end
274
    else
275
      machine_code
276
  in
277

    
278
  Log.report ~level:2 (fun fmt -> fprintf fmt "@[<v 2>@ %a@]@,"
279
  (Utils.fprintf_list ~sep:"@ " Machine_code.pp_machine)
280
  machine_code);
281

    
282
  (* Optimize machine code *)
283
  let machine_code =
284
    if !Options.optimization >= 2 && !Options.output <> "horn" then
285
      begin
286
	Log.report ~level:1 (fun fmt -> fprintf fmt ".. machines optimization (phase 1)@,");
287
	Optimize_machine.machines_unfold (Corelang.get_consts prog) node_schs machine_code
288
      end
289
    else
290
      machine_code
291
  in
292
  (* Optimize machine code *)
293
  let machine_code =
294
    if !Options.optimization >= 3 && !Options.output <> "horn" then
295
      begin
296
	Log.report ~level:1 (fun fmt -> fprintf fmt ".. machines optimization (phase 2)@,");
297
	Optimize_machine.machines_fusion (Optimize_machine.machines_reuse_variables machine_code node_schs)
298
      end
299
    else
300
      machine_code
301
  in
302

    
303

    
304
  Log.report ~level:3 (fun fmt -> fprintf fmt "@[<v 2>@ %a@]@,"
305
  (Utils.fprintf_list ~sep:"@ " Machine_code.pp_machine)
306
  machine_code);
307

    
308
  Some machine_code 
309
    end
310
  in
311
  
312
  (* Printing code *)
313
  let basename    =  Filename.basename basename in
314
  let destname = !Options.dest_dir ^ "/" ^ basename in
315
  let _ = match !Options.output, machine_code with
316
    | "C", Some machine_code ->
317
      begin
318
	  let alloc_header_file = destname ^ "_alloc.h" in (* Could be changed *)
319
	  let source_lib_file = destname ^ ".c" in (* Could be changed *)
320
	  let source_main_file = destname ^ "_main.c" in (* Could be changed *)
321
	  let makefile_file = destname ^ ".makefile" in (* Could be changed *)
322
	  Log.report ~level:1 (fun fmt -> fprintf fmt ".. C code generation@,");
323
	  C_backend.translate_to_c
324
	    alloc_header_file source_lib_file source_main_file makefile_file
325
	    basename prog machine_code dependencies
326
	end
327
    | "java", Some machine_code ->
328
      begin
329
	failwith "Sorry, but not yet supported !"
330
    (*let source_file = basename ^ ".java" in
331
      Log.report ~level:1 (fun fmt -> fprintf fmt ".. opening file %s@,@?" source_file);
332
      let source_out = open_out source_file in
333
      let source_fmt = formatter_of_out_channel source_out in
334
      Log.report ~level:1 (fun fmt -> fprintf fmt ".. java code generation@,@?");
335
      Java_backend.translate_to_java source_fmt basename normalized_prog machine_code;*)
336
      end
337
    | "horn", Some machine_code ->
338
       begin
339
	let source_file = destname ^ ".smt2" in (* Could be changed *)
340
	let source_out = open_out source_file in
341
	let fmt = formatter_of_out_channel source_out in
342
	Log.report ~level:1 (fun fmt -> fprintf fmt ".. hornification@,");
343
        Horn_backend.translate fmt basename prog machine_code;
344
	(* Tracability file if option is activated *)
345
	if !Options.traces then (
346
	let traces_file = destname ^ ".traces.xml" in (* Could be changed *)
347
	let traces_out = open_out traces_file in
348
	let fmt = formatter_of_out_channel traces_out in
349
        Log.report ~level:1 (fun fmt -> fprintf fmt ".. tracing info@,");
350
	Horn_backend.traces_file fmt basename prog machine_code;
351
	)
352
      end
353
    | "lustre", None ->
354
      begin
355
	let source_file = destname ^ ".lustrec.lus" in (* Could be changed *)
356
	let source_out = open_out source_file in
357
	let fmt = formatter_of_out_channel source_out in
358
	Printers.pp_prog fmt prog;
359
(*	Lustre_backend.translate fmt basename normalized_prog machine_code *)
360
	()
361
      end
362

    
363
    | _ -> assert false
364
  in
365
  begin
366
    Log.report ~level:1 (fun fmt -> fprintf fmt ".. done @ @]@.");
367
  (* We stop the process here *)
368
    exit 0
369
  end
370

    
371
let compile dirname basename extension =
372
  match extension with
373
  | ".lusi"  -> compile_header dirname basename extension
374
  | ".lus"   -> compile_source dirname basename extension
375
  | _        -> assert false
376

    
377
let anonymous filename =
378
  let ok_ext, ext = List.fold_left
379
    (fun (ok, ext) ext' ->
380
      if not ok && Filename.check_suffix filename ext' then
381
	true, ext'
382
      else
383
	ok, ext)
384
    (false, "") extensions in
385
  if ok_ext then
386
    let dirname = Filename.dirname filename in
387
    let basename = Filename.chop_suffix (Filename.basename filename) ext in
388
    compile dirname basename ext
389
  else
390
    raise (Arg.Bad ("Can only compile *.lusi, *.lus or *.ec files"))
391

    
392
let _ =
393
  Corelang.add_internal_funs ();
394
  try
395
    Printexc.record_backtrace true;
396
    Arg.parse Options.options anonymous usage
397
  with
398
  | Parse.Syntax_err _ | Lexer_lustre.Error _
399
  | Types.Error (_,_) | Clocks.Error (_,_)
400
  | Corelang.Error _ (*| Task_set.Error _*)
401
  | Causality.Cycle _ -> exit 1
402
  | Sys_error msg -> (eprintf "Failure: %s@." msg)
403
  | exc -> (Utils.track_exception (); raise exc)
404

    
405
(* Local Variables: *)
406
(* compile-command:"make -C .." *)
407
(* End: *)