Project

General

Profile

Statistics
| Branch: | Tag: | Revision:

lustrec / src / main_lustre_compiler.ml @ 01c7d5e1

History | View | Annotate | Download (13.9 KB)

1 22fe1c93 ploc
(* ----------------------------------------------------------------------------
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 01c7d5e1 ploc
open LustreSpec
30
31 22fe1c93 ploc
let usage = "Usage: lustrec [options] <source-file>"
32
33 8f1c7e91 xthirioux
let extensions = [".ec"; ".lus"; ".lusi"]
34 22fe1c93 ploc
35 52cfee34 xthirioux
let check_stateless_decls decls =
36 3bfed7f9 xthirioux
  Log.report ~level:1 (fun fmt -> fprintf fmt ".. checking stateless/stateful status@ ");
37 52cfee34 xthirioux
  try
38 e135421f xthirioux
    Stateless.check_prog decls
39
  with (Stateless.Error (loc, err)) as exc ->
40 6e7164b1 ploc
    eprintf "Stateless status error %a%a@."
41
      Stateless.pp_error err
42
      Location.pp_loc loc;
43 52cfee34 xthirioux
    raise exc
44
45 5c1184ad ploc
let type_decls env decls =  
46 3bfed7f9 xthirioux
  Log.report ~level:1 (fun fmt -> fprintf fmt ".. typing@ ");
47 5c1184ad ploc
  let new_env = 
48
    begin
49
      try
50
	Typing.type_prog env decls
51
      with (Types.Error (loc,err)) as exc ->
52 6e7164b1 ploc
	eprintf "Typing error %a%a@."
53
	  Types.pp_error err
54
	  Location.pp_loc loc;
55 5c1184ad ploc
	raise exc
56
    end 
57
  in
58
  if !Options.print_types then
59 3bfed7f9 xthirioux
    Log.report ~level:1 (fun fmt -> fprintf fmt "@[<v 2>  %a@]@ " Corelang.pp_prog_type decls);
60 5c1184ad ploc
  new_env
61
      
62
let clock_decls env decls = 
63 3bfed7f9 xthirioux
  Log.report ~level:1 (fun fmt -> fprintf fmt ".. clock calculus@ ");
64 5c1184ad ploc
  let new_env =
65
    begin
66
      try
67
	Clock_calculus.clock_prog env decls
68
      with (Clocks.Error (loc,err)) as exc ->
69 6e7164b1 ploc
	eprintf "Clock calculus error %a%a@." Clocks.pp_error err Location.pp_loc loc;
70 5c1184ad ploc
	raise exc
71
    end
72
  in
73
  if !Options.print_clocks then
74 3bfed7f9 xthirioux
    Log.report ~level:1 (fun fmt -> fprintf fmt "@[<v 2>  %a@]@ " Corelang.pp_prog_clock decls);
75 5c1184ad ploc
  new_env
76
77 21485807 xthirioux
(* Loading Lusi file and filling type tables with parsed
78 5c1184ad ploc
   functions/nodes *)
79 89b9e25c xthirioux
let load_lusi own filename =
80 5c1184ad ploc
  Location.input_name := filename;
81
  let lexbuf = Lexing.from_channel (open_in filename) in
82
  Location.init lexbuf filename;
83
  (* Parsing *)
84 3bfed7f9 xthirioux
  Log.report ~level:1 (fun fmt -> fprintf fmt ".. parsing header file %s@ " filename);
85 54ae8ac7 ploc
    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 6e7164b1 ploc
      eprintf "Parsing error %a%a@."
93
	Corelang.pp_error err
94
	Location.pp_loc loc;
95 54ae8ac7 ploc
      raise exc
96
    )
97
98 7291cb80 xthirioux
99
let check_lusi header =
100 5c1184ad ploc
  let new_tenv = type_decls Basic_library.type_env header in   (* Typing *)
101 8f1c7e91 xthirioux
  let new_cenv = clock_decls Basic_library.clock_env header in   (* Clock calculus *)
102 5c1184ad ploc
  header, new_tenv, new_cenv
103 54ae8ac7 ploc
104 c1adf235 ploc
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 add75bcb xthirioux
        
110
    (* checking stateless status compatibility *)
111
    Stateless.check_compat header;
112
113
    (* checking type compatibility with computed types*)
114 c1adf235 ploc
    Typing.check_env_compat header declared_types_env computed_types_env;
115
    Typing.uneval_prog_generics prog;
116
    
117 add75bcb xthirioux
    (* checking clocks compatibility with computed clocks*)
118
    Clock_calculus.check_env_compat header declared_clocks_env computed_clocks_env;
119
    Clock_calculus.uneval_prog_generics prog
120 c1adf235 ploc
121
    with Sys_error _ -> ( 
122
      (* Printing lusi file is necessary *)
123 3bfed7f9 xthirioux
      Log.report ~level:1 
124 c1adf235 ploc
	(fun fmt -> 
125
	  fprintf fmt 
126
	    ".. generating lustre interface file %s@," lusi_name);
127
      let lusi_out = open_out lusi_name in
128
      let lusi_fmt = formatter_of_out_channel lusi_out in
129
      Typing.uneval_prog_generics prog;
130
      Clock_calculus.uneval_prog_generics prog;
131
      Printers.pp_lusi_header lusi_fmt source_name prog
132
    )
133
    | (Types.Error (loc,err)) as exc ->
134
      eprintf "Type mismatch between computed type and declared type in lustre interface file: %a@."
135
	Types.pp_error err;
136
      raise exc
137
    | Clocks.Error (loc, err) as exc ->
138
      eprintf "Clock mismatch between computed clock and declared clock in lustre interface file: %a@."
139
	Clocks.pp_error err;
140
      raise exc
141
    | Stateless.Error (loc, err) as exc ->
142
      eprintf "Stateless status mismatch between defined status and declared status in lustre interface file: %a@."
143
	Stateless.pp_error err;
144
      raise exc
145 5c1184ad ploc
    
146 22fe1c93 ploc
let rec compile basename extension =
147 c1adf235 ploc
148 5c1184ad ploc
  (* Loading the input file *)
149 22fe1c93 ploc
  let source_name = basename^extension in
150
  Location.input_name := source_name;
151
  let lexbuf = Lexing.from_channel (open_in source_name) in
152
  Location.init lexbuf source_name;
153 c1adf235 ploc
154 22fe1c93 ploc
  (* Parsing *)
155 3bfed7f9 xthirioux
  Log.report ~level:1 
156 54ae8ac7 ploc
    (fun fmt -> fprintf fmt "@[<v>.. parsing file %s@," source_name);
157 22fe1c93 ploc
  let prog =
158
    try
159
      Parse.prog Parser_lustre.prog Lexer_lustre.token lexbuf
160 21485807 xthirioux
    with
161
    | (Lexer_lustre.Error err) | (Parse.Syntax_err err) as exc -> 
162 22fe1c93 ploc
      Parse.report_error err;
163 21485807 xthirioux
      raise exc
164 52cfee34 xthirioux
    | Corelang.Error (loc, err) as exc ->
165 6e7164b1 ploc
      eprintf "Parsing error %a%a@."
166
	Corelang.pp_error err
167
	Location.pp_loc loc;
168 22fe1c93 ploc
      raise exc
169
  in
170 c1adf235 ploc
171 5c1184ad ploc
  (* Extracting dependencies *)
172 3bfed7f9 xthirioux
  Log.report ~level:1 (fun fmt -> fprintf fmt "@[<v 2>.. extracting dependencies@,");
173 5c1184ad ploc
  let dependencies = 
174 22fe1c93 ploc
    List.fold_right 
175 01c7d5e1 ploc
      (fun d accu -> match d.top_decl_desc with 
176
      | Open (local, s) -> (s, local)::accu 
177 5c1184ad ploc
      | _ -> accu) 
178 22fe1c93 ploc
      prog [] 
179
  in
180 54ae8ac7 ploc
  let dependencies, type_env, clock_env =
181
    List.fold_left (fun (compilation_dep, type_env, clock_env) (s, local) -> 
182 5c1184ad ploc
      try
183 54ae8ac7 ploc
	let basename = (if local then s else Version.prefix ^ "/include/lustrec/" ^ s ) ^ ".lusi" in 
184 3bfed7f9 xthirioux
	Log.report ~level:1 (fun fmt -> fprintf fmt "@[<v 0>Library %s@," basename);
185 c1adf235 ploc
	let comp_dep, lusi_type_env, lusi_clock_env = check_lusi (load_lusi false basename) in 
186 3bfed7f9 xthirioux
	Log.report ~level:1 (fun fmt -> fprintf fmt "@]@ ");
187 3826f8cb ploc
	
188 c1adf235 ploc
	(s, local, comp_dep)::compilation_dep,
189
	Env.overwrite type_env lusi_type_env,
190
	Env.overwrite clock_env lusi_clock_env      
191 5c1184ad ploc
      with Sys_error msg -> (
192 6e7164b1 ploc
	eprintf "Failure: impossible to load library %s.@.%s@." s msg;
193 5c1184ad ploc
	exit 1
194
      )
195 54ae8ac7 ploc
    )  ([], Basic_library.type_env, Basic_library.clock_env) dependencies
196 5c1184ad ploc
  in
197 3bfed7f9 xthirioux
  Log.report ~level:1 (fun fmt -> fprintf fmt "@]@ ");
198 c1adf235 ploc
  
199 22fe1c93 ploc
  (* Sorting nodes *)
200
  let prog = SortProg.sort prog in
201 add75bcb xthirioux
202
  (* Checking stateless/stateful status *)
203
  check_stateless_decls prog;
204
205 22fe1c93 ploc
  (* Typing *)
206 5c1184ad ploc
  let computed_types_env = type_decls type_env prog in
207 22fe1c93 ploc
  
208
  (* Clock calculus *)
209 5c1184ad ploc
  let computed_clocks_env = clock_decls clock_env prog in
210 22fe1c93 ploc
211 e2380d4d ploc
  (* Perform global inlining *)
212
  let prog =
213
    if !Options.global_inline && 
214
      (match !Options.main_node with | "" -> false | _ -> true) then
215 592f508c ploc
      Inliner.global_inline basename prog type_env clock_env
216 e2380d4d ploc
    else
217
      prog
218
  in
219
220 22fe1c93 ploc
  (* Delay calculus *)
221 5c1184ad ploc
  (*
222
    if(!Options.delay_calculus)
223
    then
224 22fe1c93 ploc
    begin
225 3bfed7f9 xthirioux
    Log.report ~level:1 (fun fmt -> fprintf fmt ".. initialisation analysis@?");
226 5c1184ad ploc
    try
227
    Delay_calculus.delay_prog Basic_library.delay_env prog
228
    with (Delay.Error (loc,err)) as exc ->
229
    Location.print loc;
230
    eprintf "%a" Delay.pp_error err;
231
    Utils.track_exception ();
232
    raise exc
233 22fe1c93 ploc
    end;
234 5c1184ad ploc
  *)
235 22fe1c93 ploc
  (*
236
    eprintf "Causality analysis@.@?";
237 5c1184ad ploc
    (* Causality analysis *)
238 22fe1c93 ploc
    begin
239
    try
240
    Causality.check_causal_prog prog
241
    with (Causality.Cycle v) as exc ->
242
    Causality.pp_error err_formatter v;
243
    raise exc
244
    end;
245
  *)
246 7291cb80 xthirioux
247 c1adf235 ploc
  (* Compatibility with Lusi *)
248 7291cb80 xthirioux
  (* Checking the existence of a lusi (Lustre Interface file) *)
249
  let lusi_name = basename ^ ".lusi" in
250 c1adf235 ploc
  load_n_check_lusi source_name lusi_name prog computed_types_env computed_clocks_env;
251 7291cb80 xthirioux
252 22fe1c93 ploc
  (* Computes and stores generic calls for each node,
253
     only useful for ANSI C90 compliant generic node compilation *)
254
  if !Options.ansi then Causality.NodeDep.compute_generic_calls prog;
255
  (*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;*)
256
257
  (* Normalization phase *)
258 3bfed7f9 xthirioux
  Log.report ~level:1 (fun fmt -> fprintf fmt ".. normalization@,");
259 b84a138e ploc
  (* Special treatment of arrows in lustre backend. We want to keep them *)
260
  if !Options.output = "lustre" then
261
    Normalization.unfold_arrow_active := false;
262 c1adf235 ploc
  let prog = Normalization.normalize_prog prog in
263 3bfed7f9 xthirioux
  Log.report ~level:2 (fun fmt -> fprintf fmt "@[<v 2>@ %a@]@," Printers.pp_prog prog);
264 c1adf235 ploc
265 22fe1c93 ploc
  (* Checking array accesses *)
266
  if !Options.check then
267
    begin
268 3bfed7f9 xthirioux
      Log.report ~level:1 (fun fmt -> fprintf fmt ".. array access checks@,");
269 c1adf235 ploc
      Access.check_prog prog;
270 22fe1c93 ploc
    end;
271
272 3bfed7f9 xthirioux
  (* Computation of node equation scheduling. It also breaks dependency cycles
273
     and warns about unused input or memory variables *)
274
  Log.report ~level:1 (fun fmt -> fprintf fmt ".. scheduling@,");
275
  let prog, node_schs = Scheduling.schedule_prog prog in
276
  Log.report ~level:1 (fun fmt -> fprintf fmt "@[<v 2>@ %a@]@," Scheduling.pp_warning_unused node_schs);
277 8a183477 xthirioux
  Log.report ~level:2 (fun fmt -> fprintf fmt "@[<v 2>@ %a@]@," Scheduling.pp_schedule node_schs);
278
  Log.report ~level:2 (fun fmt -> fprintf fmt "@[<v 2>@ %a@]@," Scheduling.pp_fanin_table node_schs);
279 3bfed7f9 xthirioux
  Log.report ~level:2 (fun fmt -> fprintf fmt "@[<v 2>@ %a@]@," Printers.pp_prog prog);
280 c1adf235 ploc
281
 (* Optimization of prog: 
282
    - Unfold consts 
283
    - eliminate trivial expressions
284
 *)
285
  let prog = 
286
    if !Options.optimization >= 2 then 
287
      Optimize_prog.prog_unfold_consts prog 
288
    else
289
      prog
290
  in
291
292 22fe1c93 ploc
  (* DFS with modular code generation *)
293 3bfed7f9 xthirioux
  Log.report ~level:1 (fun fmt -> fprintf fmt ".. machines generation@,");
294 c1adf235 ploc
  let machine_code = Machine_code.translate_prog prog node_schs in
295 3bfed7f9 xthirioux
  Log.report ~level:2 (fun fmt -> fprintf fmt "@[<v 2>@ %a@]@,"
296 22fe1c93 ploc
    (Utils.fprintf_list ~sep:"@ " Machine_code.pp_machine)
297
    machine_code);
298 c1adf235 ploc
299
  (* Optimize machine code *)
300
  let machine_code = 
301
    if !Options.optimization >= 2 then
302
      Optimize_machine.optimize_machines machine_code
303
    else
304
      machine_code
305
  in
306 fa090c4e xthirioux
  
307
  (* Creating destination directory if needed *)
308
  if not (Sys.file_exists !Options.dest_dir) then (
309 3bfed7f9 xthirioux
    Log.report ~level:1 (fun fmt -> fprintf fmt ".. creating destination directory@,");
310 fa090c4e xthirioux
    Unix.mkdir !Options.dest_dir (Unix.stat ".").Unix.st_perm
311
  );
312
  if (Unix.stat !Options.dest_dir).Unix.st_kind <> Unix.S_DIR then (
313 6e7164b1 ploc
    eprintf "Failure: destination %s is not a directory.@.@." !Options.dest_dir;
314 fa090c4e xthirioux
    exit 1
315
  );
316 22fe1c93 ploc
  (* Printing code *)
317 7291cb80 xthirioux
  let basename    =  Filename.basename basename in
318 8f1c7e91 xthirioux
  let destname = !Options.dest_dir ^ "/" ^ basename in
319 3ee1012f ploc
  let _ = match !Options.output with
320
      "C" -> 
321
	begin
322 7291cb80 xthirioux
	  let header_file = destname ^ ".h" in (* Could be changed *)
323 d4107cf2 ploc
	  let source_lib_file = destname ^ ".c" in (* Could be changed *)
324
	  let source_main_file = destname ^ "_main.c" in (* Could be changed *)
325 7291cb80 xthirioux
	  let makefile_file = destname ^ ".makefile" in (* Could be changed *)
326 d4107cf2 ploc
	  (* let spec_file_opt = if !Options.c_spec then  *)
327
	  (*     ( *)
328
	  (* 	let spec_file = basename ^ "_spec.c" in *)
329 3bfed7f9 xthirioux
	  (* 	Log.report ~level:1 (fun fmt -> fprintf fmt ".. opening files %s, %s and %s@," header_file source_file spec_file); *)
330 d4107cf2 ploc
	  (* 	Some spec_file  *)
331
	  (*     ) else ( *)
332 3bfed7f9 xthirioux
	  (* 	Log.report ~level:1 (fun fmt -> fprintf fmt ".. opening files %s and %s@," header_file source_file); *)
333 d4107cf2 ploc
	  (* 	None  *)
334
	  (*      ) *)
335
	  (* in  *)
336 3bfed7f9 xthirioux
	  Log.report ~level:1 (fun fmt -> fprintf fmt ".. C code generation@,");
337 d4107cf2 ploc
	  C_backend.translate_to_c 
338
	    header_file source_lib_file source_main_file makefile_file
339
	    basename prog machine_code dependencies
340 3ee1012f ploc
	end
341 7291cb80 xthirioux
    | "java" ->
342
      begin
343
	failwith "Sorry, but not yet supported !"
344 22fe1c93 ploc
    (*let source_file = basename ^ ".java" in
345 3bfed7f9 xthirioux
      Log.report ~level:1 (fun fmt -> fprintf fmt ".. opening file %s@,@?" source_file);
346 5c1184ad ploc
      let source_out = open_out source_file in
347
      let source_fmt = formatter_of_out_channel source_out in
348 3bfed7f9 xthirioux
      Log.report ~level:1 (fun fmt -> fprintf fmt ".. java code generation@,@?");
349 5c1184ad ploc
      Java_backend.translate_to_java source_fmt basename normalized_prog machine_code;*)
350 7291cb80 xthirioux
      end
351
    | "horn" ->
352
      begin
353 592f508c ploc
	let source_file = destname ^ ".smt2" in (* Could be changed *)
354 7291cb80 xthirioux
	let source_out = open_out source_file in
355
	let fmt = formatter_of_out_channel source_out in
356 c1adf235 ploc
	Horn_backend.translate fmt basename prog machine_code
357 7291cb80 xthirioux
      end
358 b84a138e ploc
    | "lustre" -> 
359 c1adf235 ploc
      begin
360
	let source_file = destname ^ ".lustrec.lus" in (* Could be changed *)
361
	let source_out = open_out source_file in
362
	let fmt = formatter_of_out_channel source_out in
363 b84a138e ploc
	Printers.pp_prog fmt prog;
364 c1adf235 ploc
(*	Lustre_backend.translate fmt basename normalized_prog machine_code *)
365
	()
366 b84a138e ploc
      end
367 c1adf235 ploc
368 2f44a4cc ploc
    | _ -> assert false
369 3ee1012f ploc
  in
370 3bfed7f9 xthirioux
  Log.report ~level:1 (fun fmt -> fprintf fmt ".. done !@ @]@.");
371 22fe1c93 ploc
  (* We stop the process here *)
372
  exit 0
373
  
374
let anonymous filename =
375
  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
376
  if ok_ext then
377
    let basename = Filename.chop_suffix filename ext in
378
    compile basename ext
379
  else
380 8f1c7e91 xthirioux
    raise (Arg.Bad ("Can only compile *.lusi, *.lus or *.ec files"))
381 22fe1c93 ploc
382
let _ =
383
  Corelang.add_internal_funs ();
384
  try
385
    Printexc.record_backtrace true;
386
    Arg.parse Options.options anonymous usage
387
  with
388
  | Parse.Syntax_err _ | Lexer_lustre.Error _ 
389
  | Types.Error (_,_) | Clocks.Error (_,_)
390
  | Corelang.Error _ (*| Task_set.Error _*) 
391
  | Causality.Cycle _ -> exit 1
392
  | exc -> (Utils.track_exception (); raise exc)
393
394
(* Local Variables: *)
395
(* compile-command:"make -C .." *)
396
(* End: *)