Project

General

Profile

Download (13.4 KB) Statistics
| Branch: | Tag: | Revision:
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
  Log.report ~level:4 (fun fmt -> fprintf fmt "After automata expansion:@.@[<v 2>@ %a@]@," Printers.pp_prog prog);
102

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

    
106
  (* Extracting dependencies *)
107
  let dependencies, type_env, clock_env = import_dependencies prog in
108

    
109
  (* Sorting nodes *)
110
  let prog = SortProg.sort prog in
111

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

    
122
  (* Checking stateless/stateful status *)
123
  check_stateless_decls prog;
124

    
125
  (* Typing *)
126
  let computed_types_env = type_decls type_env prog in
127

    
128
  (* Clock calculus *)
129
  let computed_clocks_env = clock_decls clock_env prog in
130

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

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

    
157
  (* Creating destination directory if needed *)
158
  create_dest_dir ();
159

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

    
170
  Typing.uneval_prog_generics prog;
171
  Clock_calculus.uneval_prog_generics prog;
172

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

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

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

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

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

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

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

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

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

    
271
  if !Options.optimization >= 2 then
272
    begin
273
      Log.report ~level:3 (fun fmt -> fprintf fmt "@[<v 2>@ %a@]@,"
274
	(Utils.fprintf_list ~sep:"@ " Machine_code.pp_machine)
275
	machine_code);
276
    end;
277

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

    
329
    | _ -> assert false
330
  in
331
  begin
332
    Log.report ~level:1 (fun fmt -> fprintf fmt ".. done @ @]@.");
333
  (* We stop the process here *)
334
    exit 0
335
  end
336

    
337
let compile dirname basename extension =
338
  match extension with
339
  | ".lusi"  -> compile_header dirname basename extension
340
  | ".lus"   -> compile_source dirname basename extension
341
  | _        -> assert false
342

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

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

    
371
(* Local Variables: *)
372
(* compile-command:"make -C .." *)
373
(* End: *)
(31-31/50)