Project

General

Profile

Statistics
| Branch: | Tag: | Revision:

lustrec / src / main_lustre_compiler.ml @ 3f823d04

History | View | Annotate | Download (11.5 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 LustreSpec
16
open Compiler_common
17

    
18
let usage = "Usage: lustrec [options] <source-file>"
19

    
20
let extensions = [".ec"; ".lus"; ".lusi"]
21

    
22
(* print a .lusi header file from a source prog *)
23
let print_lusi prog basename extension =
24
  let header = Lusic.extract_header basename prog in
25
  let header_name = basename ^ extension in
26
  let h_out = open_out header_name in
27
  let h_fmt = formatter_of_out_channel h_out in
28
  begin
29
    Printers.pp_lusi_header h_fmt basename header;
30
    close_out h_out
31
  end
32

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

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

    
82
(* compile a .lus source file *)
83
let rec compile_source basename extension =
84
  let source_name = basename ^ extension in
85

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

    
88
  let prog = parse_source source_name in
89

    
90
  (* Extracting dependencies *)
91
  let dependencies, type_env, clock_env = import_dependencies prog in
92
 
93
  (* Sorting nodes *)
94
  let prog = SortProg.sort prog in
95

    
96
  (* Typing *)
97
  let computed_types_env = type_decls type_env prog in
98
  
99
  (* Clock calculus *)
100
  let computed_clocks_env = clock_decls clock_env prog in
101

    
102
  (* Checking stateless/stateful status *)
103
  check_stateless_decls prog;
104

    
105
  (* Generating a .lusi header file only *)
106
  if !Options.lusi then
107
    begin
108
      let lusi_ext = extension ^ "i" in
109
      Log.report ~level:1 (fun fmt -> fprintf fmt ".. generating interface file %s@," (basename ^ lusi_ext));
110
      print_lusi prog basename lusi_ext;
111
      Log.report ~level:1 (fun fmt -> fprintf fmt ".. done !@ @]@.");
112
      exit 0
113
    end;
114

    
115
  (* Perform global inlining *)
116
  let prog =
117
    if !Options.global_inline && 
118
      (match !Options.main_node with | "" -> false | _ -> true) then
119
      Inliner.global_inline basename prog type_env clock_env
120
    else
121
      prog
122
  in
123

    
124
  (* Delay calculus *)
125
  (*
126
    if(!Options.delay_calculus)
127
    then
128
    begin
129
    Log.report ~level:1 (fun fmt -> fprintf fmt ".. initialisation analysis@?");
130
    try
131
    Delay_calculus.delay_prog Basic_library.delay_env prog
132
    with (Delay.Error (loc,err)) as exc ->
133
    Location.print loc;
134
    eprintf "%a" Delay.pp_error err;
135
    Utils.track_exception ();
136
    raise exc
137
    end;
138
  *)
139
  (*
140
    eprintf "Causality analysis@.@?";
141
    (* Causality analysis *)
142
    begin
143
    try
144
    Causality.check_causal_prog prog
145
    with (Causality.Cycle v) as exc ->
146
    Causality.pp_error err_formatter v;
147
    raise exc
148
    end;
149
  *)
150

    
151
  (* Creating destination directory if needed *)
152
  create_dest_dir ();
153

    
154
  (* Compatibility with Lusi *)
155
  (* Checking the existence of a lusi (Lustre Interface file) *)
156
  let extension = ".lusi" in
157
  compile_source_to_header prog computed_types_env computed_clocks_env basename extension;
158

    
159
  Typing.uneval_prog_generics prog;
160
  Clock_calculus.uneval_prog_generics prog;
161

    
162
  (* Computes and stores generic calls for each node,
163
     only useful for ANSI C90 compliant generic node compilation *)
164
  if !Options.ansi then Causality.NodeDep.compute_generic_calls prog;
165
  (*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;*)
166

    
167
  (* Normalization phase *)
168
  Log.report ~level:1 (fun fmt -> fprintf fmt ".. normalization@,");
169
  (* Special treatment of arrows in lustre backend. We want to keep them *)
170
  if !Options.output = "lustre" then
171
    Normalization.unfold_arrow_active := false;
172
  let prog = Normalization.normalize_prog prog in
173
  Log.report ~level:2 (fun fmt -> fprintf fmt "@[<v 2>@ %a@]@," Printers.pp_prog prog);
174

    
175
  (* Checking array accesses *)
176
  if !Options.check then
177
    begin
178
      Log.report ~level:1 (fun fmt -> fprintf fmt ".. array access checks@,");
179
      Access.check_prog prog;
180
    end;
181

    
182
  (* Computation of node equation scheduling. It also breaks dependency cycles
183
     and warns about unused input or memory variables *)
184
  Log.report ~level:1 (fun fmt -> fprintf fmt ".. scheduling@,");
185
  let prog, node_schs = Scheduling.schedule_prog prog in
186
  Log.report ~level:3 (fun fmt -> fprintf fmt "@[<v 2>@ %a@]@," Scheduling.pp_warning_unused node_schs);
187
  Log.report ~level:3 (fun fmt -> fprintf fmt "@[<v 2>@ %a@]@," Scheduling.pp_schedule node_schs);
188
  Log.report ~level:3 (fun fmt -> fprintf fmt "@[<v 2>@ %a@]@," Scheduling.pp_fanin_table node_schs);
189
  Log.report ~level:3 (fun fmt -> fprintf fmt "@[<v 2>@ %a@]@," Printers.pp_prog prog);
190

    
191
 (* Optimization of prog: 
192
    - Unfold consts 
193
    - eliminate trivial expressions
194
 *)
195
  let prog = 
196
    if !Options.optimization >= 2 then 
197
      Optimize_prog.prog_unfold_consts prog 
198
    else
199
      prog
200
  in
201

    
202
  (* DFS with modular code generation *)
203
  Log.report ~level:1 (fun fmt -> fprintf fmt ".. machines generation@,");
204
  let machine_code = Machine_code.translate_prog prog node_schs in
205

    
206
  (* Optimize machine code *)
207
  let machine_code = 
208
    if !Options.optimization >= 3 && !Options.output <> "horn" then
209
      begin
210
	Log.report ~level:1 (fun fmt -> fprintf fmt ".. machines optimization@,");
211
	Optimize_machine.machines_reuse_variables machine_code node_schs
212
      end
213
    else
214
      machine_code
215
 in  
216
  let machine_code = 
217
    if !Options.optimization >= 3 && !Options.output <> "horn" then
218
      begin
219
	Optimize_machine.machines_fusion machine_code
220
      end
221
    else
222
      machine_code
223
 in
224
  Log.report ~level:3 (fun fmt -> fprintf fmt "@[<v 2>@ %a@]@,"
225
  (Utils.fprintf_list ~sep:"@ " Machine_code.pp_machine)
226
  machine_code);
227

    
228
  (* Printing code *)
229
  let basename    =  Filename.basename basename in
230
  let destname = !Options.dest_dir ^ "/" ^ basename in
231
  let _ = match !Options.output with
232
      "C" -> 
233
	begin
234
	  let alloc_header_file = destname ^ "_alloc.h" in (* Could be changed *)
235
	  let source_lib_file = destname ^ ".c" in (* Could be changed *)
236
	  let source_main_file = destname ^ "_main.c" in (* Could be changed *)
237
	  let makefile_file = destname ^ ".makefile" in (* Could be changed *)
238
	  Log.report ~level:1 (fun fmt -> fprintf fmt ".. C code generation@,");
239
	  C_backend.translate_to_c 
240
	    alloc_header_file source_lib_file source_main_file makefile_file
241
	    basename prog machine_code dependencies
242
	end
243
    | "java" ->
244
      begin
245
	failwith "Sorry, but not yet supported !"
246
    (*let source_file = basename ^ ".java" in
247
      Log.report ~level:1 (fun fmt -> fprintf fmt ".. opening file %s@,@?" source_file);
248
      let source_out = open_out source_file in
249
      let source_fmt = formatter_of_out_channel source_out in
250
      Log.report ~level:1 (fun fmt -> fprintf fmt ".. java code generation@,@?");
251
      Java_backend.translate_to_java source_fmt basename normalized_prog machine_code;*)
252
      end
253
    | "horn" ->
254
      begin
255
	let source_file = destname ^ ".smt2" in (* Could be changed *)
256
	let source_out = open_out source_file in
257
	let fmt = formatter_of_out_channel source_out in
258
	Horn_backend.translate fmt basename prog machine_code;
259
	(* Tracability file if option is activated *)
260
	if !Options.horntraces then (
261
	let traces_file = destname ^ ".traces" in (* Could be changed *)
262
	let traces_out = open_out traces_file in
263
	let fmt = formatter_of_out_channel traces_out in
264
	Horn_backend.traces_file fmt basename prog machine_code	  
265
	)
266
      end
267
    | "lustre" -> 
268
      begin
269
	let source_file = destname ^ ".lustrec.lus" in (* Could be changed *)
270
	let source_out = open_out source_file in
271
	let fmt = formatter_of_out_channel source_out in
272
	Printers.pp_prog fmt prog;
273
(*	Lustre_backend.translate fmt basename normalized_prog machine_code *)
274
	()
275
      end
276

    
277
    | _ -> assert false
278
  in
279
  begin
280
    Log.report ~level:1 (fun fmt -> fprintf fmt ".. done !@ @]@.");
281
  (* We stop the process here *)
282
    exit 0
283
  end
284

    
285
let compile basename extension =
286
  match extension with
287
  | ".lusi"  -> compile_header basename extension
288
  | ".lus"   -> compile_source basename extension
289
  | _        -> assert false
290

    
291
let anonymous filename =
292
  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
293
  if ok_ext then
294
    let basename = Filename.chop_suffix (Filename.basename filename) ext in
295
    compile basename ext
296
  else
297
    raise (Arg.Bad ("Can only compile *.lusi, *.lus or *.ec files"))
298

    
299
let _ =
300
  Corelang.add_internal_funs ();
301
  try
302
    Printexc.record_backtrace true;
303
    Arg.parse Options.options anonymous usage
304
  with
305
  | Parse.Syntax_err _ | Lexer_lustre.Error _ 
306
  | Types.Error (_,_) | Clocks.Error (_,_)
307
  | Corelang.Error _ (*| Task_set.Error _*) 
308
  | Causality.Cycle _ -> exit 1
309
  | Sys_error msg -> (eprintf "Failure: %s@." msg)
310
  | exc -> (Utils.track_exception (); raise exc)
311

    
312
(* Local Variables: *)
313
(* compile-command:"make -C .." *)
314
(* End: *)