Project

General

Profile

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

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

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

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

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

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

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

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

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

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

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

  
369
(* Local Variables: *)
370
(* compile-command:"make -C .." *)
371
(* 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 Utils
16
open LustreSpec
17
open Compiler_common
18
 
19
exception StopPhase1 of program
20

  
21
let usage = "Usage: lustrec [options] \x1b[4msource file\x1b[0m"
22

  
23
let extensions = [".ec"; ".lus"; ".lusi"]
24

  
25
(* print a .lusi header file from a source prog *)
26
let print_lusi prog dirname basename extension =
27
  let header = Lusic.extract_header dirname basename prog in
28
  let header_name = dirname ^ "/" ^ basename ^ extension in
29
  let h_out = open_out header_name in
30
  let h_fmt = formatter_of_out_channel h_out in
31
  begin
32
    Typing.uneval_prog_generics header;
33
    Clock_calculus.uneval_prog_generics header;
34
    Printers.pp_lusi_header h_fmt basename header;
35
    close_out h_out
36
  end
37

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

  
56
(* check whether a source file has a compiled header,
57
   if not, generate the compiled header *)
58
let compile_source_to_header prog computed_types_env computed_clocks_env dirname basename extension =
59
  let destname = !Options.dest_dir ^ "/" ^ basename in
60
  let lusic_ext = extension ^ "c" in
61
  let header_name = destname ^ lusic_ext in
62
  begin
63
    if not (Sys.file_exists header_name) 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 dirname basename prog) destname lusic_ext;
67
	Lusic.print_lusic_to_h destname lusic_ext
68
      end
69
    else
70
      let lusic = Lusic.read_lusic destname lusic_ext in
71
      if not lusic.Lusic.from_lusi then
72
	begin
73
	  Log.report ~level:1 (fun fmt -> fprintf fmt ".. generating compiled header file %s@," header_name);
74
       	  Lusic.write_lusic false (Lusic.extract_header dirname basename prog) destname lusic_ext;
75
(*List.iter (fun top_decl -> Format.eprintf "lusic: %a@." Printers.pp_decl top_decl) lusic.Lusic.contents;*)
76
	  Lusic.print_lusic_to_h destname lusic_ext
77
	end
78
      else
79
	begin
80
	  Log.report ~level:1 (fun fmt -> fprintf fmt ".. loading compiled header file %s@," header_name);
81
	  Modules.check_dependency lusic destname;
82
	  let header = lusic.Lusic.contents in
83
	  let (declared_types_env, declared_clocks_env) = get_envs_from_top_decls header in
84
	  check_compatibility
85
	    (prog, computed_types_env, computed_clocks_env)
86
	    (header, declared_types_env, declared_clocks_env)
87
	end
88
  end
89

  
90
(* From prog to prog *)
91
let stage1 prog dirname basename =
92
  (* Removing automata *) 
93
  let prog = Automata.expand_decls prog in
94

  
95
  (* Importing source *)
96
  let _ = Modules.load_program ISet.empty prog in
97

  
98
  (* Extracting dependencies *)
99
  let dependencies, type_env, clock_env = import_dependencies prog in
100

  
101
  (* Sorting nodes *)
102
  let prog = SortProg.sort prog in
103

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

  
114
  (* Checking stateless/stateful status *)
115
  if Scopes.Plugin.is_active () then
116
    force_stateful_decls prog
117
  else
118
    check_stateless_decls prog;
119

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

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

  
126
  (* Generating a .lusi header file only *)
127
  if !Options.lusi then
128
    (* We stop here the processing and produce the current prog. It will be
129
       exported as a lusi *)
130
    raise (StopPhase1 prog);
131

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

  
148
  (* Creating destination directory if needed *)
149
  create_dest_dir ();
150

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

  
156
  Typing.uneval_prog_generics prog;
157
  Clock_calculus.uneval_prog_generics prog;
158

  
159
  if !Options.global_inline && !Options.main_node <> "" && !Options.witnesses then
160
    begin
161
      let orig = Corelang.copy_prog orig in
162
      Log.report ~level:1 (fun fmt -> fprintf fmt ".. generating witness file@,");
163
      check_stateless_decls orig;
164
      let _ = Typing.type_prog type_env orig in
165
      let _ = Clock_calculus.clock_prog clock_env orig in
166
      Typing.uneval_prog_generics orig;
167
      Clock_calculus.uneval_prog_generics orig;
168
      Inliner.witness
169
	basename
170
	!Options.main_node
171
	orig prog type_env clock_env
172
    end;
173

  
174
  (* Computes and stores generic calls for each node,
175
     only useful for ANSI C90 compliant generic node compilation *)
176
  if !Options.ansi then Causality.NodeDep.compute_generic_calls prog;
177
  (*Hashtbl.iter (fun id td -> match td.Corelang.top_decl_desc with
178
    Corelang.Node nd -> Format.eprintf "%s calls %a" id
179
    Causality.NodeDep.pp_generic_calls nd | _ -> ()) Corelang.node_table;*)
180

  
181
  (* Optimization of prog: 
182
     - Unfold consts 
183
     - eliminate trivial expressions
184
  *)
185
  let prog = 
186
    if !Options.const_unfold || !Options.optimization >= 4 then 
187
      Optimize_prog.prog_unfold_consts prog 
188
    else
189
      prog
190
  in
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
  Log.report ~level:2 (fun fmt -> fprintf fmt "@[<v 2>@ %a@]@," Printers.pp_prog prog);
199

  
200
  let prog =
201
    if !Options.mpfr
202
    then
203
      begin
204
	Log.report ~level:1 (fun fmt -> fprintf fmt ".. targetting MPFR library@,");
205
	Mpfr.inject_prog prog
206
      end
207
    else
208
      begin
209
	Log.report ~level:1 (fun fmt -> fprintf fmt ".. keeping FP numbers@,");
210
	prog
211
      end in
212
  Log.report ~level:2 (fun fmt -> fprintf fmt "@[<v 2>@ %a@]@," Printers.pp_prog prog);
213

  
214
  (* Checking array accesses *)
215
  if !Options.check then
216
    begin
217
      Log.report ~level:1 (fun fmt -> fprintf fmt ".. array access checks@,");
218
      Access.check_prog prog;
219
    end;
220

  
221
  prog, dependencies
222

  
223
(* prog -> machine *)
224

  
225
let stage2 prog =    
226

  
227
  (* Computation of node equation scheduling. It also breaks dependency cycles
228
     and warns about unused input or memory variables *)
229
  Log.report ~level:1 (fun fmt -> fprintf fmt ".. scheduling@,");
230
  let prog, node_schs = Scheduling.schedule_prog prog in
231
  Log.report ~level:1 (fun fmt -> fprintf fmt "%a"              Scheduling.pp_warning_unused node_schs);
232
  Log.report ~level:3 (fun fmt -> fprintf fmt "@[<v 2>@ %a@]@," Scheduling.pp_schedule node_schs);
233
  Log.report ~level:3 (fun fmt -> fprintf fmt "@[<v 2>@ %a@]@," Scheduling.pp_fanin_table node_schs);
234
  Log.report ~level:5 (fun fmt -> fprintf fmt "@[<v 2>@ %a@]@," Scheduling.pp_dep_graph node_schs);
235
  Log.report ~level:3 (fun fmt -> fprintf fmt "@[<v 2>@ %a@]@," Printers.pp_prog prog);
236

  
237

  
238
  (* TODO Salsa optimize prog: 
239
     - emits warning for programs with pre inside expressions
240
     - make sure each node arguments and memory is bounded by a local annotation
241
     - introduce fresh local variables for each real pure subexpression
242
  *)
243
 (*  let prog =  *)
244
 (*    if true then *)
245
 (*      Salsa.Prog.normalize prog *)
246
 (*    else *)
247
 (*      prog *)
248
 (* in *)
249

  
250
  (* DFS with modular code generation *)
251
  Log.report ~level:1 (fun fmt -> fprintf fmt ".. machines generation@ ");
252
  let machine_code = Machine_code.translate_prog prog node_schs in
253

  
254
  (* Optimize machine code *)
255
  let machine_code, removed_table = 
256
    if !Options.optimization >= 2 && !Options.output <> "horn" then
257
      begin
258
	Log.report ~level:1 (fun fmt -> fprintf fmt ".. machines optimization (phase 1)@ ");
259
	Optimize_machine.machines_unfold (Corelang.get_consts prog) node_schs machine_code
260
      end
261
    else
262
      machine_code, IMap.empty
263
  in  
264
  (* Optimize machine code *)
265
  let machine_code = 
266
    if !Options.optimization >= 3 && !Options.output <> "horn" then
267
      begin
268
	Log.report ~level:1 (fun fmt -> fprintf fmt ".. machines optimization (phase 2)@ ");
269
	let node_schs    = Scheduling.remove_prog_inlined_locals removed_table node_schs in
270
	let reuse_tables = Scheduling.compute_prog_reuse_table node_schs in
271
	Optimize_machine.machines_fusion (Optimize_machine.machines_reuse_variables machine_code reuse_tables)
272
      end
273
    else
274
      machine_code
275
  in
276
  
277
  (* Salsa optimize machine code *)
278
  let machine_code = 
279
    if !Options.salsa_enabled then
280
      begin
281
	check_main ();
282
	Log.report ~level:1 (fun fmt -> fprintf fmt ".. salsa machines optimization (phase 3)@ ");
283
	(* Selecting float constants for Salsa *)
284
	let constEnv = List.fold_left (
285
	  fun accu c_topdecl ->
286
	    match c_topdecl.top_decl_desc with
287
	    | Const c when Types.is_real_type c.const_type  ->
288
	      (c.const_id, c.const_value) :: accu
289
	    | _ -> accu
290
	) [] (Corelang.get_consts prog) 
291
	in
292
	List.map 
293
	  (Machine_salsa_opt.machine_t2machine_t_optimized_by_salsa constEnv) 
294
	  machine_code 
295
      end
296
    else
297
      machine_code
298
  in
299
  Log.report ~level:3 (fun fmt -> fprintf fmt "@[<v 2>@ %a@]@ "
300
    (Utils.fprintf_list ~sep:"@ " Machine_code.pp_machine)
301
    machine_code);
302

  
303
  machine_code
304

  
305

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

  
358
    | _ -> assert false
359

  
360
(* compile a .lus source file *)
361
let compile_source dirname basename extension =
362

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

  
365
  (* Parsing source *)
366
  let prog = parse_source (dirname ^ "/" ^ basename ^ extension) in
367

  
368
  let prog =
369
    if !Options.mpfr then
370
      Mpfr.mpfr_module::prog
371
    else
372
      prog
373
  in
374
  let prog, dependencies = 
375
    try 
376
      stage1 prog dirname basename
377
    with StopPhase1 prog -> (
378
      if !Options.lusi then
379
	begin
380
	  let lusi_ext = "lusi" (* extension ^ "i" *) in
381
	  Log.report ~level:1 (fun fmt -> fprintf fmt ".. generating interface file %s@," (dirname ^ "/" ^ basename ^ lusi_ext));
382
	  print_lusi prog dirname basename lusi_ext;
383
	  Log.report ~level:1 (fun fmt -> fprintf fmt ".. done !@ @]@.");
384
	  exit 0
385
	end
386
      else
387
        assert false
388
    )
389
  in
390

  
391
  let machine_code = 
392
    stage2 prog 
393
  in
394
  if Scopes.Plugin.show_scopes () then
395
    begin
396
      let all_scopes = Scopes.compute_scopes prog !Options.main_node in
397
      (* Printing scopes *)
398
      if !Options.verbose_level >= 1 then
399
	Format.printf "Possible scopes are:@   ";
400
      Format.printf "@[<v>%a@ @]@.@?" Scopes.print_scopes all_scopes;
401
      exit 0
402
	
403
    end;
404

  
405
  let machine_code = 
406
    if Scopes.Plugin.is_active () then
407
      Scopes.Plugin.process_scopes !Options.main_node prog machine_code
408
    else
409
      machine_code
410
  in
411
  
412
  stage3 prog machine_code dependencies basename;
413
  begin
414
    Log.report ~level:1 (fun fmt -> fprintf fmt ".. done !@ @]@.");
415
    (* We stop the process here *)
416
    exit 0
417
  end
418

  
419
let compile dirname basename extension =
420
  match extension with
421
  | ".lusi"  -> compile_header dirname basename extension
422
  | ".lus"   -> compile_source dirname basename extension
423
  | _        -> assert false
424

  
425
let anonymous filename =
426
  let ok_ext, ext = List.fold_left
427
    (fun (ok, ext) ext' ->
428
      if not ok && Filename.check_suffix filename ext' then
429
	true, ext'
430
      else
431
	ok, ext)
432
    (false, "") extensions in
433
  if ok_ext then
434
    let dirname = Filename.dirname filename in
435
    let basename = Filename.chop_suffix (Filename.basename filename) ext in
436
    compile dirname basename ext
437
  else
438
    raise (Arg.Bad ("Can only compile *.lusi, *.lus or *.ec files"))
439

  
440
let _ =
441
  Global.initialize ();
442
  Corelang.add_internal_funs ();
443
  try
444
    Printexc.record_backtrace true;
445

  
446
    let options = Options.options @ 
447
      List.flatten (
448
	List.map Options.plugin_opt [
449
	  Scopes.Plugin.name, Scopes.Plugin.activate, Scopes.Plugin.options
450
	]
451
      )
452
    in
453
    
454
    Arg.parse options anonymous usage
455
  with
456
  | Parse.Syntax_err _ | Lexer_lustre.Error _
457
  | Types.Error (_,_) | Clocks.Error (_,_)
458
  | Corelang.Error _ (*| Task_set.Error _*)
459
  | Causality.Cycle _ -> exit 1
460
  | Sys_error msg -> (eprintf "Failure: %s@." msg)
461
  | exc -> (Utils.track_exception (); raise exc)
462

  
463
(* Local Variables: *)
464
(* compile-command:"make -C .." *)
465
(* End: *)

Also available in: Unified diff