Project

General

Profile

Statistics
| Branch: | Tag: | Revision:

lustrec / src / main_lustre_compiler.ml @ b1655a21

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

    
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 rec compile basename extension =
136

    
137
  (* Loading the input file *)
138
  let source_name = basename^extension in
139
  Location.input_name := source_name;
140
  let lexbuf = Lexing.from_channel (open_in source_name) in
141
  Location.init lexbuf source_name;
142

    
143
  (* Parsing *)
144
  Log.report ~level:1 
145
    (fun fmt -> fprintf fmt "@[<v>.. parsing file %s@," source_name);
146
  let prog =
147
    try
148
      Parse.prog Parser_lustre.prog Lexer_lustre.token lexbuf
149
    with
150
    | (Lexer_lustre.Error err) | (Parse.Syntax_err err) as exc -> 
151
      Parse.report_error err;
152
      raise exc
153
    | Corelang.Error (loc, err) as exc ->
154
      eprintf "Parsing error %a%a@."
155
	Corelang.pp_error err
156
	Location.pp_loc loc;
157
      raise exc
158
  in
159

    
160
  (* Extracting dependencies *)
161
  Log.report ~level:1 (fun fmt -> fprintf fmt "@[<v 2>.. extracting dependencies@,");
162
  let dependencies = 
163
    List.fold_right 
164
      (fun d accu -> match d.top_decl_desc with 
165
      | Open (local, s) -> (s, local)::accu 
166
      | _ -> accu) 
167
      prog [] 
168
  in
169
  let dependencies, type_env, clock_env =
170
    List.fold_left (fun (compilation_dep, type_env, clock_env) (s, local) -> 
171
      try
172
	let basename = (if local then s else Version.prefix ^ "/include/lustrec/" ^ s ) ^ ".lusi" in 
173
	Log.report ~level:1 (fun fmt -> fprintf fmt "@[<v 0>Library %s@," basename);
174
	let comp_dep, lusi_type_env, lusi_clock_env = check_lusi (load_lusi false basename) in 
175
	Log.report ~level:1 (fun fmt -> fprintf fmt "@]@ ");
176
	
177
	(s, local, comp_dep)::compilation_dep,
178
	Env.overwrite type_env lusi_type_env,
179
	Env.overwrite clock_env lusi_clock_env      
180
      with Sys_error msg -> (
181
	eprintf "Failure: impossible to load library %s.@.%s@." s msg;
182
	exit 1
183
      )
184
    )  ([], Basic_library.type_env, Basic_library.clock_env) dependencies
185
  in
186
  Log.report ~level:1 (fun fmt -> fprintf fmt "@]@ ");
187
  
188
  (* Sorting nodes *)
189
  let prog = SortProg.sort prog in
190

    
191
  (* Typing *)
192
  let computed_types_env = type_decls type_env prog in
193
  
194
  (* Clock calculus *)
195
  let computed_clocks_env = clock_decls clock_env prog in
196

    
197
  (* Checking stateless/stateful status *)
198
  check_stateless_decls prog;
199

    
200
  (* Perform global inlining *)
201
  let prog =
202
    if !Options.global_inline && 
203
      (match !Options.main_node with | "" -> false | _ -> true) then
204
      Inliner.global_inline basename prog type_env clock_env
205
    else
206
      prog
207
  in
208

    
209
  (* Delay calculus *)
210
  (*
211
    if(!Options.delay_calculus)
212
    then
213
    begin
214
    Log.report ~level:1 (fun fmt -> fprintf fmt ".. initialisation analysis@?");
215
    try
216
    Delay_calculus.delay_prog Basic_library.delay_env prog
217
    with (Delay.Error (loc,err)) as exc ->
218
    Location.print loc;
219
    eprintf "%a" Delay.pp_error err;
220
    Utils.track_exception ();
221
    raise exc
222
    end;
223
  *)
224
  (*
225
    eprintf "Causality analysis@.@?";
226
    (* Causality analysis *)
227
    begin
228
    try
229
    Causality.check_causal_prog prog
230
    with (Causality.Cycle v) as exc ->
231
    Causality.pp_error err_formatter v;
232
    raise exc
233
    end;
234
  *)
235

    
236
  (* Compatibility with Lusi *)
237
  (* Checking the existence of a lusi (Lustre Interface file) *)
238
  let lusi_name = basename ^ ".lusi" in
239
  load_n_check_lusi source_name lusi_name prog computed_types_env computed_clocks_env;
240

    
241
  (* Computes and stores generic calls for each node,
242
     only useful for ANSI C90 compliant generic node compilation *)
243
  if !Options.ansi then Causality.NodeDep.compute_generic_calls prog;
244
  (*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;*)
245

    
246
  (* Normalization phase *)
247
  Log.report ~level:1 (fun fmt -> fprintf fmt ".. normalization@,");
248
  (* Special treatment of arrows in lustre backend. We want to keep them *)
249
  if !Options.output = "lustre" then
250
    Normalization.unfold_arrow_active := false;
251
  let prog = Normalization.normalize_prog prog in
252
  Log.report ~level:2 (fun fmt -> fprintf fmt "@[<v 2>@ %a@]@," Printers.pp_prog prog);
253

    
254
  (* Checking array accesses *)
255
  if !Options.check then
256
    begin
257
      Log.report ~level:1 (fun fmt -> fprintf fmt ".. array access checks@,");
258
      Access.check_prog prog;
259
    end;
260

    
261
  (* Computation of node equation scheduling. It also breaks dependency cycles
262
     and warns about unused input or memory variables *)
263
  Log.report ~level:1 (fun fmt -> fprintf fmt ".. scheduling@,");
264
  let prog, node_schs = Scheduling.schedule_prog prog in
265
  Log.report ~level:3 (fun fmt -> fprintf fmt "@[<v 2>@ %a@]@," Scheduling.pp_warning_unused node_schs);
266
  Log.report ~level:3 (fun fmt -> fprintf fmt "@[<v 2>@ %a@]@," Scheduling.pp_schedule node_schs);
267
  Log.report ~level:3 (fun fmt -> fprintf fmt "@[<v 2>@ %a@]@," Scheduling.pp_fanin_table node_schs);
268
  Log.report ~level:3 (fun fmt -> fprintf fmt "@[<v 2>@ %a@]@," Printers.pp_prog prog);
269

    
270
 (* Optimization of prog: 
271
    - Unfold consts 
272
    - eliminate trivial expressions
273
 *)
274
  let prog = 
275
    if !Options.optimization >= 2 then 
276
      Optimize_prog.prog_unfold_consts prog 
277
    else
278
      prog
279
  in
280

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

    
285
  (* Optimize machine code *)
286
  let machine_code = 
287
    if !Options.optimization >= 3 && !Options.output <> "horn" then
288
      begin
289
	Log.report ~level:1 (fun fmt -> fprintf fmt ".. machines optimization@,");
290
	Optimize_machine.machines_reuse_variables machine_code node_schs
291
      end
292
    else
293
      machine_code
294
 in  
295
  let machine_code = 
296
    if !Options.optimization >= 3 && !Options.output <> "horn" then
297
      begin
298
	Log.report ~level:1 (fun fmt -> fprintf fmt ".. machines optimization@,");
299
	Optimize_machine.machines_fusion machine_code
300
      end
301
    else
302
      machine_code
303
 in
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
  (* Creating destination directory if needed *)
309
  if not (Sys.file_exists !Options.dest_dir) then (
310
    Log.report ~level:1 (fun fmt -> fprintf fmt ".. creating destination directory@,");
311
    Unix.mkdir !Options.dest_dir (Unix.stat ".").Unix.st_perm
312
  );
313
  if (Unix.stat !Options.dest_dir).Unix.st_kind <> Unix.S_DIR then (
314
    eprintf "Failure: destination %s is not a directory.@.@." !Options.dest_dir;
315
    exit 1
316
  );
317
  (* Printing code *)
318
  let basename    =  Filename.basename basename in
319
  let destname = !Options.dest_dir ^ "/" ^ basename in
320
  let _ = match !Options.output with
321
      "C" -> 
322
	begin
323
	  let header_file = destname ^ ".h" in (* Could be changed *)
324
	  let source_lib_file = destname ^ ".c" in (* Could be changed *)
325
	  let source_main_file = destname ^ "_main.c" in (* Could be changed *)
326
	  let makefile_file = destname ^ ".makefile" in (* Could be changed *)
327
	  (* let spec_file_opt = if !Options.c_spec then  *)
328
	  (*     ( *)
329
	  (* 	let spec_file = basename ^ "_spec.c" in *)
330
	  (* 	Log.report ~level:1 (fun fmt -> fprintf fmt ".. opening files %s, %s and %s@," header_file source_file spec_file); *)
331
	  (* 	Some spec_file  *)
332
	  (*     ) else ( *)
333
	  (* 	Log.report ~level:1 (fun fmt -> fprintf fmt ".. opening files %s and %s@," header_file source_file); *)
334
	  (* 	None  *)
335
	  (*      ) *)
336
	  (* in  *)
337
	  Log.report ~level:1 (fun fmt -> fprintf fmt ".. C code generation@,");
338
	  C_backend.translate_to_c 
339
	    header_file source_lib_file source_main_file makefile_file
340
	    basename prog machine_code dependencies
341
	end
342
    | "java" ->
343
      begin
344
	failwith "Sorry, but not yet supported !"
345
    (*let source_file = basename ^ ".java" in
346
      Log.report ~level:1 (fun fmt -> fprintf fmt ".. opening file %s@,@?" source_file);
347
      let source_out = open_out source_file in
348
      let source_fmt = formatter_of_out_channel source_out in
349
      Log.report ~level:1 (fun fmt -> fprintf fmt ".. java code generation@,@?");
350
      Java_backend.translate_to_java source_fmt basename normalized_prog machine_code;*)
351
      end
352
    | "horn" ->
353
      begin
354
	let source_file = destname ^ ".smt2" in (* Could be changed *)
355
	let source_out = open_out source_file in
356
	let fmt = formatter_of_out_channel source_out in
357
	Horn_backend.translate fmt basename prog machine_code;
358
	(* Tracability file if option is activated *)
359
	if !Options.horntraces then (
360
	let traces_file = destname ^ ".traces" in (* Could be changed *)
361
	let traces_out = open_out traces_file in
362
	let fmt = formatter_of_out_channel traces_out in
363
	Horn_backend.traces_file fmt basename prog machine_code	  
364
	)
365
      end
366
    | "lustre" -> 
367
      begin
368
	let source_file = destname ^ ".lustrec.lus" in (* Could be changed *)
369
	let source_out = open_out source_file in
370
	let fmt = formatter_of_out_channel source_out in
371
	Printers.pp_prog fmt prog;
372
(*	Lustre_backend.translate fmt basename normalized_prog machine_code *)
373
	()
374
      end
375

    
376
    | _ -> assert false
377
  in
378
  Log.report ~level:1 (fun fmt -> fprintf fmt ".. done !@ @]@.");
379
  (* We stop the process here *)
380
  exit 0
381
  
382
let anonymous filename =
383
  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
384
  if ok_ext then
385
    let basename = Filename.chop_suffix filename ext in
386
    compile basename ext
387
  else
388
    raise (Arg.Bad ("Can only compile *.lusi, *.lus or *.ec files"))
389

    
390
let _ =
391
  Corelang.add_internal_funs ();
392
  try
393
    Printexc.record_backtrace true;
394
    Arg.parse Options.options anonymous usage
395
  with
396
  | Parse.Syntax_err _ | Lexer_lustre.Error _ 
397
  | Types.Error (_,_) | Clocks.Error (_,_)
398
  | Corelang.Error _ (*| Task_set.Error _*) 
399
  | Causality.Cycle _ -> exit 1
400
  | exc -> (Utils.track_exception (); raise exc)
401

    
402
(* Local Variables: *)
403
(* compile-command:"make -C .." *)
404
(* End: *)