Project

General

Profile

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