Project

General

Profile

Statistics
| Branch: | Tag: | Revision:

lustrec / src / main_lustre_compiler.ml @ a38c681e

History | View | Annotate | Download (14.3 KB)

1
(* ----------------------------------------------------------------------------
2
 * SchedMCore - A MultiCore Scheduling Framework
3
 * Copyright (C) 2009-2013, ONERA, Toulouse, FRANCE - LIFL, Lille, FRANCE
4
 * Copyright (C) 2012-2013, INPT, Toulouse, FRANCE
5
 *
6
 * This file is part of Prelude
7
 *
8
 * Prelude is free software; you can redistribute it and/or
9
 * modify it under the terms of the GNU Lesser General Public License
10
 * as published by the Free Software Foundation ; either version 2 of
11
 * the License, or (at your option) any later version.
12
 *
13
 * Prelude is distributed in the hope that it will be useful, but
14
 * WITHOUT ANY WARRANTY ; without even the implied warranty of
15
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16
 * Lesser General Public License for more details.
17
 *
18
 * You should have received a copy of the GNU Lesser General Public
19
 * License along with this program ; if not, write to the Free Software
20
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
21
 * USA
22
 *---------------------------------------------------------------------------- *)
23

    
24
(* This module is used for the lustre to C compiler *)
25

    
26
open Format
27
open Log
28

    
29
open LustreSpec
30

    
31
let usage = "Usage: lustrec [options] <source-file>"
32

    
33
let extensions = [".ec"; ".lus"; ".lusi"]
34

    
35
let check_stateless_decls decls =
36
  Log.report ~level:1 (fun fmt -> fprintf fmt ".. checking stateless/stateful status@ ");
37
  try
38
    Stateless.check_prog decls
39
  with (Stateless.Error (loc, err)) as exc ->
40
    eprintf "Stateless status error %a%a@."
41
      Stateless.pp_error err
42
      Location.pp_loc loc;
43
    raise exc
44

    
45
let type_decls env decls =  
46
  Log.report ~level:1 (fun fmt -> fprintf fmt ".. typing@ ");
47
  let new_env = 
48
    begin
49
      try
50
	Typing.type_prog env decls
51
      with (Types.Error (loc,err)) as exc ->
52
	eprintf "Typing error %a%a@."
53
	  Types.pp_error err
54
	  Location.pp_loc loc;
55
	raise exc
56
    end 
57
  in
58
  if !Options.print_types then
59
    Log.report ~level:1 (fun fmt -> fprintf fmt "@[<v 2>  %a@]@ " Corelang.pp_prog_type decls);
60
  new_env
61
      
62
let clock_decls env decls = 
63
  Log.report ~level:1 (fun fmt -> fprintf fmt ".. clock calculus@ ");
64
  let new_env =
65
    begin
66
      try
67
	Clock_calculus.clock_prog env decls
68
      with (Clocks.Error (loc,err)) as exc ->
69
	eprintf "Clock calculus error %a%a@." Clocks.pp_error err Location.pp_loc loc;
70
	raise exc
71
    end
72
  in
73
  if !Options.print_clocks then
74
    Log.report ~level:1 (fun fmt -> fprintf fmt "@[<v 2>  %a@]@ " Corelang.pp_prog_clock decls);
75
  new_env
76

    
77
(* Loading Lusi file and filling type tables with parsed
78
   functions/nodes *)
79
let load_lusi own filename =
80
  Location.input_name := filename;
81
  let lexbuf = Lexing.from_channel (open_in filename) in
82
  Location.init lexbuf filename;
83
  (* Parsing *)
84
  Log.report ~level:1 (fun fmt -> fprintf fmt ".. parsing header file %s@ " filename);
85
    try
86
      Parse.header own Parser_lustre.header Lexer_lustre.token lexbuf
87
    with
88
    | (Lexer_lustre.Error err) | (Parse.Syntax_err err) as exc -> 
89
      Parse.report_error err;
90
      raise exc
91
    | Corelang.Error (loc, err) as exc -> (
92
      eprintf "Parsing error %a%a@."
93
	Corelang.pp_error err
94
	Location.pp_loc loc;
95
      raise exc
96
    )
97

    
98

    
99
let check_lusi header =
100
  let new_tenv = type_decls Basic_library.type_env header in   (* Typing *)
101
  let new_cenv = clock_decls Basic_library.clock_env header in   (* Clock calculus *)
102
  header, new_tenv, new_cenv
103

    
104
let load_n_check_lusi source_name lusi_name prog computed_types_env computed_clocks_env= 
105
  try 
106
    let _ = open_in lusi_name in
107
    let header = load_lusi true lusi_name in
108
    let _, declared_types_env, declared_clocks_env = check_lusi header in
109
        
110

    
111
    (* checking type compatibility with computed types*)
112
    Typing.check_env_compat header declared_types_env computed_types_env;
113
    Typing.uneval_prog_generics prog;
114
    
115
    (* checking clocks compatibility with computed clocks*)
116
    Clock_calculus.check_env_compat header declared_clocks_env computed_clocks_env;
117
    Clock_calculus.uneval_prog_generics prog;
118

    
119
    (* checking stateless status compatibility *)
120
    Stateless.check_compat header
121

    
122
    with Sys_error _ -> ( 
123
      (* Printing lusi file is necessary *)
124
      Log.report ~level:1 
125
	(fun fmt -> 
126
	  fprintf fmt 
127
	    ".. generating lustre interface file %s@," lusi_name);
128
      let lusi_out = open_out lusi_name in
129
      let lusi_fmt = formatter_of_out_channel lusi_out in
130
      Typing.uneval_prog_generics prog;
131
      Clock_calculus.uneval_prog_generics prog;
132
      Printers.pp_lusi_header lusi_fmt source_name prog
133
    )
134
    | (Types.Error (loc,err)) as exc ->
135
      eprintf "Type mismatch between computed type and declared type in lustre interface file: %a@."
136
	Types.pp_error err;
137
      raise exc
138
    | Clocks.Error (loc, err) as exc ->
139
      eprintf "Clock mismatch between computed clock and declared clock in lustre interface file: %a@."
140
	Clocks.pp_error err;
141
      raise exc
142
    | Stateless.Error (loc, err) as exc ->
143
      eprintf "Stateless status mismatch between defined status and declared status in lustre interface file: %a@."
144
	Stateless.pp_error err;
145
      raise exc
146
    
147
let rec compile basename extension =
148

    
149
  (* Loading the input file *)
150
  let source_name = basename^extension in
151
  Location.input_name := source_name;
152
  let lexbuf = Lexing.from_channel (open_in source_name) in
153
  Location.init lexbuf source_name;
154

    
155
  (* Parsing *)
156
  Log.report ~level:1 
157
    (fun fmt -> fprintf fmt "@[<v>.. parsing file %s@," source_name);
158
  let prog =
159
    try
160
      Parse.prog Parser_lustre.prog Lexer_lustre.token lexbuf
161
    with
162
    | (Lexer_lustre.Error err) | (Parse.Syntax_err err) as exc -> 
163
      Parse.report_error err;
164
      raise exc
165
    | Corelang.Error (loc, err) as exc ->
166
      eprintf "Parsing error %a%a@."
167
	Corelang.pp_error err
168
	Location.pp_loc loc;
169
      raise exc
170
  in
171

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

    
203
  (* Typing *)
204
  let computed_types_env = type_decls type_env prog in
205
  
206
  (* Clock calculus *)
207
  let computed_clocks_env = clock_decls clock_env prog in
208

    
209
  (* Checking stateless/stateful status *)
210
  check_stateless_decls prog;
211

    
212
  (* Perform global inlining *)
213
  let prog =
214
    if !Options.global_inline && 
215
      (match !Options.main_node with | "" -> false | _ -> true) then
216
      Inliner.global_inline basename prog type_env clock_env
217
    else
218
      prog
219
  in
220

    
221
  (* Delay calculus *)
222
  (*
223
    if(!Options.delay_calculus)
224
    then
225
    begin
226
    Log.report ~level:1 (fun fmt -> fprintf fmt ".. initialisation analysis@?");
227
    try
228
    Delay_calculus.delay_prog Basic_library.delay_env prog
229
    with (Delay.Error (loc,err)) as exc ->
230
    Location.print loc;
231
    eprintf "%a" Delay.pp_error err;
232
    Utils.track_exception ();
233
    raise exc
234
    end;
235
  *)
236
  (*
237
    eprintf "Causality analysis@.@?";
238
    (* Causality analysis *)
239
    begin
240
    try
241
    Causality.check_causal_prog prog
242
    with (Causality.Cycle v) as exc ->
243
    Causality.pp_error err_formatter v;
244
    raise exc
245
    end;
246
  *)
247

    
248
  (* Compatibility with Lusi *)
249
  (* Checking the existence of a lusi (Lustre Interface file) *)
250
  let lusi_name = basename ^ ".lusi" in
251
  load_n_check_lusi source_name lusi_name prog computed_types_env computed_clocks_env;
252

    
253
  (* Computes and stores generic calls for each node,
254
     only useful for ANSI C90 compliant generic node compilation *)
255
  if !Options.ansi then Causality.NodeDep.compute_generic_calls prog;
256
  (*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;*)
257

    
258
  (* Normalization phase *)
259
  Log.report ~level:1 (fun fmt -> fprintf fmt ".. normalization@,");
260
  (* Special treatment of arrows in lustre backend. We want to keep them *)
261
  if !Options.output = "lustre" then
262
    Normalization.unfold_arrow_active := false;
263
  let prog = Normalization.normalize_prog prog in
264
  Log.report ~level:2 (fun fmt -> fprintf fmt "@[<v 2>@ %a@]@," Printers.pp_prog prog);
265

    
266
  (* Checking array accesses *)
267
  if !Options.check then
268
    begin
269
      Log.report ~level:1 (fun fmt -> fprintf fmt ".. array access checks@,");
270
      Access.check_prog prog;
271
    end;
272

    
273
  (* Computation of node equation scheduling. It also breaks dependency cycles
274
     and warns about unused input or memory variables *)
275
  Log.report ~level:1 (fun fmt -> fprintf fmt ".. scheduling@,");
276
  let prog, node_schs = Scheduling.schedule_prog prog in
277
  Log.report ~level:1 (fun fmt -> fprintf fmt "@[<v 2>@ %a@]@," Scheduling.pp_warning_unused node_schs);
278
  Log.report ~level:2 (fun fmt -> fprintf fmt "@[<v 2>@ %a@]@," Scheduling.pp_schedule node_schs);
279
  Log.report ~level:2 (fun fmt -> fprintf fmt "@[<v 2>@ %a@]@," Scheduling.pp_fanin_table node_schs);
280
  Log.report ~level:2 (fun fmt -> fprintf fmt "@[<v 2>@ %a@]@," Printers.pp_prog prog);
281

    
282
 (* Optimization of prog: 
283
    - Unfold consts 
284
    - eliminate trivial expressions
285
 *)
286
  let prog = 
287
    if !Options.optimization >= 2 then 
288
      Optimize_prog.prog_unfold_consts prog 
289
    else
290
      prog
291
  in
292

    
293
  (* DFS with modular code generation *)
294
  Log.report ~level:1 (fun fmt -> fprintf fmt ".. machines generation@,");
295
  let machine_code = Machine_code.translate_prog prog node_schs in
296
 (* experimental
297
  Log.report ~level:1 (fun fmt -> fprintf fmt ".. machines optimization@,");
298
  let machine_code = Machine_code.prog_reuse_var machine_code node_schs in
299
  *)
300
  Log.report ~level:2 (fun fmt -> fprintf fmt "@[<v 2>@ %a@]@,"
301
    (Utils.fprintf_list ~sep:"@ " Machine_code.pp_machine)
302
    machine_code);
303

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

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

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

    
407
(* Local Variables: *)
408
(* compile-command:"make -C .." *)
409
(* End: *)