Project

General

Profile

Revision 70e1006b src/main_lustre_compiler.ml

View differences:

src/main_lustre_compiler.ml
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

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

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

  
21
let check_stateless_decls decls =
22
  Log.report ~level:1 (fun fmt -> fprintf fmt ".. checking stateless/stateful status@ ");
23
  try
24
    Stateless.check_prog decls
25
  with (Stateless.Error (loc, err)) as exc ->
26
    eprintf "Stateless status error %a%a@."
27
      Stateless.pp_error err
28
      Location.pp_loc loc;
29
    raise exc
30

  
31
let type_decls env decls =  
32
  Log.report ~level:1 (fun fmt -> fprintf fmt ".. typing@ ");
33
  let new_env = 
34
    begin
35
      try
36
	Typing.type_prog env decls
37
      with (Types.Error (loc,err)) as exc ->
38
	eprintf "Typing error %a%a@."
39
	  Types.pp_error err
40
	  Location.pp_loc loc;
41
	raise exc
42
    end 
43
  in
44
  if !Options.print_types then
45
    Log.report ~level:1 (fun fmt -> fprintf fmt "@[<v 2>  %a@]@ " Corelang.pp_prog_type decls);
46
  new_env
47
      
48
let clock_decls env decls = 
49
  Log.report ~level:1 (fun fmt -> fprintf fmt ".. clock calculus@ ");
50
  let new_env =
51
    begin
52
      try
53
	Clock_calculus.clock_prog env decls
54
      with (Clocks.Error (loc,err)) as exc ->
55
	eprintf "Clock calculus error %a%a@." Clocks.pp_error err Location.pp_loc loc;
56
	raise exc
57
    end
58
  in
59
  if !Options.print_clocks then
60
    Log.report ~level:1 (fun fmt -> fprintf fmt "@[<v 2>  %a@]@ " Corelang.pp_prog_clock decls);
61
  new_env
62

  
63
(* Loading Lusi file and filling type tables with parsed
64
   functions/nodes *)
65
let load_lusi own filename =
66
  Location.input_name := filename;
67
  let lexbuf = Lexing.from_channel (open_in filename) in
68
  Location.init lexbuf filename;
69
  (* Parsing *)
70
  Log.report ~level:1 (fun fmt -> fprintf fmt ".. parsing header file %s@ " filename);
71
    try
72
      Parse.header own Parser_lustre.header Lexer_lustre.token lexbuf
73
    with
74
    | (Lexer_lustre.Error err) | (Parse.Syntax_err err) as exc -> 
75
      Parse.report_error err;
76
      raise exc
77
    | Corelang.Error (loc, err) as exc -> (
78
      eprintf "Parsing error: %a%a@."
79
	Corelang.pp_error err
80
	Location.pp_loc loc;
81
      raise exc
82
    )
83

  
84

  
85
let check_lusi header =
86
  let new_tenv = type_decls Basic_library.type_env header in   (* Typing *)
87
  let new_cenv = clock_decls Basic_library.clock_env header in   (* Clock calculus *)
88
  header, new_tenv, new_cenv
89

  
90
let load_n_check_lusi source_name lusi_name prog computed_types_env computed_clocks_env= 
91
  try 
92
    let _ = open_in lusi_name in
93
    let header = load_lusi true lusi_name in
94
    let _, declared_types_env, declared_clocks_env = check_lusi header in
95
        
96
   (* checking defined types are compatible with declared types*)
97
    Typing.check_typedef_compat header;
98

  
99
    (* checking type compatibility with computed types*)
100
    Typing.check_env_compat header declared_types_env computed_types_env;
101
    Typing.uneval_prog_generics prog;
102
    
103
    (* checking clocks compatibility with computed clocks*)
104
    Clock_calculus.check_env_compat header declared_clocks_env computed_clocks_env;
105
    Clock_calculus.uneval_prog_generics prog;
106

  
107
    (* checking stateless status compatibility *)
108
    Stateless.check_compat header
109

  
110
    with Sys_error _ -> ( 
111
      (* Printing lusi file is necessary *)
112
      Log.report ~level:1 
113
	(fun fmt -> 
114
	  fprintf fmt 
115
	    ".. generating lustre interface file %s@," lusi_name);
116
      let lusi_out = open_out lusi_name in
117
      let lusi_fmt = formatter_of_out_channel lusi_out in
118
      Typing.uneval_prog_generics prog;
119
      Clock_calculus.uneval_prog_generics prog;
120
      Printers.pp_lusi_header lusi_fmt source_name prog
121
    )
122
    | (Types.Error (loc,err)) as exc ->
123
      eprintf "Type mismatch between computed type and declared type in lustre interface file: %a@."
124
	Types.pp_error err;
125
      raise exc
126
    | Clocks.Error (loc, err) as exc ->
127
      eprintf "Clock mismatch between computed clock and declared clock in lustre interface file: %a@."
128
	Clocks.pp_error err;
129
      raise exc
130
    | Stateless.Error (loc, err) as exc ->
131
      eprintf "Stateless status mismatch between defined status and declared status in lustre interface file: %a@."
132
	Stateless.pp_error err;
133
      raise exc
134

  
135
let extract_interface prog =
136
 List.fold_right
137
   (fun decl header ->
138
    match decl.top_decl_desc with
139
    | Node nd        -> { decl with top_decl_desc = ImportedNode (Corelang.get_node_interface nd) } :: header 
140
    | ImportedNode _ -> header
141
    | Consts _
142
    | Type _
143
    | Open _         -> decl :: header)
144
   prog []
145

  
146
let write_compiled_header (header : top_decl list) basename extension =
147
  let target_name = basename^extension in
148
  let outchan = open_out_bin target_name in
149
  begin
150
    Marshal.to_channel outchan header [];
151
    close_out outchan
152
  end
153

  
154
let read_compiled_header basename extension =
155
  let source_name = basename^extension in
156
  let inchan = open_in_bin source_name in
157
  let header = (Marshal.from_channel inchan : top_decl list) in
158
  begin
159
    close_in inchan;
160
    header
161
  end
162

  
163
let compile_header basename extension =
164
   let header_name = basename^extension in
165
   let header = load_lusi true header_name in
166
   begin
167
     ignore (check_lusi header);
168
     write_compiled_header header basename (extension^"c")
169
   end
170

  
171
let compile_prog basename extension =
172
 ()
173

  
174
let rec compile basename extension =
175

  
176
  (* Loading the input file *)
177
  let source_name = basename^extension in
178
  Location.input_name := source_name;
179
  let lexbuf = Lexing.from_channel (open_in source_name) in
180
  Location.init lexbuf source_name;
181

  
182
  (* Parsing *)
183
  Log.report ~level:1 
184
    (fun fmt -> fprintf fmt "@[<v>.. parsing file %s@," source_name);
185
  let prog =
186
    try
187
      Parse.prog Parser_lustre.prog Lexer_lustre.token lexbuf
188
    with
189
    | (Lexer_lustre.Error err) | (Parse.Syntax_err err) as exc -> 
190
      Parse.report_error err;
191
      raise exc
192
    | Corelang.Error (loc, err) as exc ->
193
      eprintf "Parsing error %a%a@."
194
	Corelang.pp_error err
195
	Location.pp_loc loc;
196
      raise exc
197
  in
198

  
199
  (* Extracting dependencies *)
200
  Log.report ~level:1 (fun fmt -> fprintf fmt "@[<v 2>.. extracting dependencies@,");
201
  let dependencies = 
202
    List.fold_right 
203
      (fun d accu -> match d.top_decl_desc with 
204
      | Open (local, s) -> (s, local)::accu 
205
      | _ -> accu) 
206
      prog [] 
207
  in
208
  let dependencies, type_env, clock_env =
209
    List.fold_left (fun (compilation_dep, type_env, clock_env) (s, local) -> 
210
      try
211
	let basename = (if local then s else Version.prefix ^ "/include/lustrec/" ^ s ) ^ ".lusi" in 
212
	Log.report ~level:1 (fun fmt -> fprintf fmt "@[<v 0>Library %s@," basename);
213
	let comp_dep, lusi_type_env, lusi_clock_env = check_lusi (load_lusi false basename) in 
214
	Log.report ~level:1 (fun fmt -> fprintf fmt "@]@ ");
215
	
216
	(s, local, comp_dep)::compilation_dep,
217
	Env.overwrite type_env lusi_type_env,
218
	Env.overwrite clock_env lusi_clock_env      
219
      with Sys_error msg -> (
220
	eprintf "Failure: impossible to load library %s.@.%s@." s msg;
221
	exit 1
222
      )
223
    )  ([], Basic_library.type_env, Basic_library.clock_env) dependencies
224
  in
225
  Log.report ~level:1 (fun fmt -> fprintf fmt "@]@ ");
226
  
227
  (* Sorting nodes *)
228
  let prog = SortProg.sort prog in
229

  
230
  (* Typing *)
231
  let computed_types_env = type_decls type_env prog in
232
  
233
  (* Clock calculus *)
234
  let computed_clocks_env = clock_decls clock_env prog in
235

  
236
  (* Checking stateless/stateful status *)
237
  check_stateless_decls prog;
238

  
239
  (* Perform global inlining *)
240
  let prog =
241
    if !Options.global_inline && 
242
      (match !Options.main_node with | "" -> false | _ -> true) then
243
      Inliner.global_inline basename prog type_env clock_env
244
    else
245
      prog
246
  in
247

  
248
  (* Delay calculus *)
249
  (*
250
    if(!Options.delay_calculus)
251
    then
252
    begin
253
    Log.report ~level:1 (fun fmt -> fprintf fmt ".. initialisation analysis@?");
254
    try
255
    Delay_calculus.delay_prog Basic_library.delay_env prog
256
    with (Delay.Error (loc,err)) as exc ->
257
    Location.print loc;
258
    eprintf "%a" Delay.pp_error err;
259
    Utils.track_exception ();
260
    raise exc
261
    end;
262
  *)
263
  (*
264
    eprintf "Causality analysis@.@?";
265
    (* Causality analysis *)
266
    begin
267
    try
268
    Causality.check_causal_prog prog
269
    with (Causality.Cycle v) as exc ->
270
    Causality.pp_error err_formatter v;
271
    raise exc
272
    end;
273
  *)
274

  
275
  (* Compatibility with Lusi *)
276
  (* Checking the existence of a lusi (Lustre Interface file) *)
277
  let lusi_name = basename ^ ".lusi" in
278
  load_n_check_lusi source_name lusi_name prog computed_types_env computed_clocks_env;
279

  
280
  (* Computes and stores generic calls for each node,
281
     only useful for ANSI C90 compliant generic node compilation *)
282
  if !Options.ansi then Causality.NodeDep.compute_generic_calls prog;
283
  (*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;*)
284

  
285
  (* Normalization phase *)
286
  Log.report ~level:1 (fun fmt -> fprintf fmt ".. normalization@,");
287
  (* Special treatment of arrows in lustre backend. We want to keep them *)
288
  if !Options.output = "lustre" then
289
    Normalization.unfold_arrow_active := false;
290
  let prog = Normalization.normalize_prog prog in
291
  Log.report ~level:2 (fun fmt -> fprintf fmt "@[<v 2>@ %a@]@," Printers.pp_prog prog);
292

  
293
  (* Checking array accesses *)
294
  if !Options.check then
295
    begin
296
      Log.report ~level:1 (fun fmt -> fprintf fmt ".. array access checks@,");
297
      Access.check_prog prog;
298
    end;
299

  
300
  (* Computation of node equation scheduling. It also breaks dependency cycles
301
     and warns about unused input or memory variables *)
302
  Log.report ~level:1 (fun fmt -> fprintf fmt ".. scheduling@,");
303
  let prog, node_schs = Scheduling.schedule_prog prog in
304
  Log.report ~level:3 (fun fmt -> fprintf fmt "@[<v 2>@ %a@]@," Scheduling.pp_warning_unused node_schs);
305
  Log.report ~level:3 (fun fmt -> fprintf fmt "@[<v 2>@ %a@]@," Scheduling.pp_schedule node_schs);
306
  Log.report ~level:3 (fun fmt -> fprintf fmt "@[<v 2>@ %a@]@," Scheduling.pp_fanin_table node_schs);
307
  Log.report ~level:3 (fun fmt -> fprintf fmt "@[<v 2>@ %a@]@," Printers.pp_prog prog);
308

  
309
 (* Optimization of prog: 
310
    - Unfold consts 
311
    - eliminate trivial expressions
312
 *)
313
  let prog = 
314
    if !Options.optimization >= 2 then 
315
      Optimize_prog.prog_unfold_consts prog 
316
    else
317
      prog
318
  in
319

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

  
324
  (* Optimize machine code *)
325
  let machine_code = 
326
    if !Options.optimization >= 3 && !Options.output <> "horn" then
327
      begin
328
	Log.report ~level:1 (fun fmt -> fprintf fmt ".. machines optimization@,");
329
	Optimize_machine.machines_reuse_variables machine_code node_schs
330
      end
331
    else
332
      machine_code
333
 in  
334
  let machine_code = 
335
    if !Options.optimization >= 3 && !Options.output <> "horn" then
336
      begin
337
	Log.report ~level:1 (fun fmt -> fprintf fmt ".. machines optimization@,");
338
	Optimize_machine.machines_fusion machine_code
339
      end
340
    else
341
      machine_code
342
 in
343
  Log.report ~level:3 (fun fmt -> fprintf fmt "@[<v 2>@ %a@]@,"
344
  (Utils.fprintf_list ~sep:"@ " Machine_code.pp_machine)
345
  machine_code);
346

  
347
  (* Creating destination directory if needed *)
348
  if not (Sys.file_exists !Options.dest_dir) then (
349
    Log.report ~level:1 (fun fmt -> fprintf fmt ".. creating destination directory@,");
350
    Unix.mkdir !Options.dest_dir (Unix.stat ".").Unix.st_perm
351
  );
352
  if (Unix.stat !Options.dest_dir).Unix.st_kind <> Unix.S_DIR then (
353
    eprintf "Failure: destination %s is not a directory.@.@." !Options.dest_dir;
354
    exit 1
355
  );
356
  (* Printing code *)
357
  let basename    =  Filename.basename basename in
358
  let destname = !Options.dest_dir ^ "/" ^ basename in
359
  let _ = match !Options.output with
360
      "C" -> 
361
	begin
362
	  let header_file = destname ^ ".h" in (* Could be changed *)
363
	  let source_lib_file = destname ^ ".c" in (* Could be changed *)
364
	  let source_main_file = destname ^ "_main.c" in (* Could be changed *)
365
	  let makefile_file = destname ^ ".makefile" in (* Could be changed *)
366
	  (* let spec_file_opt = if !Options.c_spec then  *)
367
	  (*     ( *)
368
	  (* 	let spec_file = basename ^ "_spec.c" in *)
369
	  (* 	Log.report ~level:1 (fun fmt -> fprintf fmt ".. opening files %s, %s and %s@," header_file source_file spec_file); *)
370
	  (* 	Some spec_file  *)
371
	  (*     ) else ( *)
372
	  (* 	Log.report ~level:1 (fun fmt -> fprintf fmt ".. opening files %s and %s@," header_file source_file); *)
373
	  (* 	None  *)
374
	  (*      ) *)
375
	  (* in  *)
376
	  Log.report ~level:1 (fun fmt -> fprintf fmt ".. C code generation@,");
377
	  C_backend.translate_to_c 
378
	    header_file source_lib_file source_main_file makefile_file
379
	    basename prog machine_code dependencies
380
	end
381
    | "java" ->
382
      begin
383
	failwith "Sorry, but not yet supported !"
384
    (*let source_file = basename ^ ".java" in
385
      Log.report ~level:1 (fun fmt -> fprintf fmt ".. opening file %s@,@?" source_file);
386
      let source_out = open_out source_file in
387
      let source_fmt = formatter_of_out_channel source_out in
388
      Log.report ~level:1 (fun fmt -> fprintf fmt ".. java code generation@,@?");
389
      Java_backend.translate_to_java source_fmt basename normalized_prog machine_code;*)
390
      end
391
    | "horn" ->
392
      begin
393
	let source_file = destname ^ ".smt2" in (* Could be changed *)
394
	let source_out = open_out source_file in
395
	let fmt = formatter_of_out_channel source_out in
396
	Horn_backend.translate fmt basename prog machine_code;
397
	(* Tracability file if option is activated *)
398
	if !Options.horntraces then (
399
	let traces_file = destname ^ ".traces" in (* Could be changed *)
400
	let traces_out = open_out traces_file in
401
	let fmt = formatter_of_out_channel traces_out in
402
	Horn_backend.traces_file fmt basename prog machine_code	  
403
	)
404
      end
405
    | "lustre" -> 
406
      begin
407
	let source_file = destname ^ ".lustrec.lus" in (* Could be changed *)
408
	let source_out = open_out source_file in
409
	let fmt = formatter_of_out_channel source_out in
410
	Printers.pp_prog fmt prog;
411
(*	Lustre_backend.translate fmt basename normalized_prog machine_code *)
412
	()
413
      end
414

  
415
    | _ -> assert false
416
  in
417
  begin
418
    Log.report ~level:1 (fun fmt -> fprintf fmt ".. done !@ @]@.");
419
  (* We stop the process here *)
420
    exit 0
421
  end
422
  
423
let anonymous filename =
424
  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
425
  if ok_ext then
426
    let basename = Filename.chop_suffix filename ext in
427
    compile basename ext
428
  else
429
    raise (Arg.Bad ("Can only compile *.lusi, *.lus or *.ec files"))
430

  
431
let _ =
432
  Corelang.add_internal_funs ();
433
  try
434
    Printexc.record_backtrace true;
435
    Arg.parse Options.options anonymous usage
436
  with
437
  | Parse.Syntax_err _ | Lexer_lustre.Error _ 
438
  | Types.Error (_,_) | Clocks.Error (_,_)
439
  | Corelang.Error _ (*| Task_set.Error _*) 
440
  | Causality.Cycle _ -> exit 1
441
  | exc -> (Utils.track_exception (); raise exc)
442

  
443
(* Local Variables: *)
444
(* compile-command:"make -C .." *)
445
(* End: *)
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 header_name = basename ^ extension in
36
   let lusic_ext = extension ^ "c" in
37
   begin
38
     Log.report ~level:1 (fun fmt -> fprintf fmt "@[<v>");
39
     let header = parse_header true header_name in
40
     ignore (check_top_decls header);
41
     create_dest_dir ();
42
     Log.report ~level:1 (fun fmt -> fprintf fmt ".. generating compiled header file %sc@," header_name);
43
     Lusic.write_lusic true header basename lusic_ext;
44
     Lusic.print_lusic_to_h basename lusic_ext;
45
     Log.report ~level:1 (fun fmt -> fprintf fmt ".. done !@ @]@.")
46
   end
47

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

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

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

  
87
  let prog = parse_source source_name in
88

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

Also available in: Unified diff