Project

General

Profile

Statistics
| Branch: | Tag: | Revision:

lustrec / src / main_lustre_compiler.ml @ 54ae8ac7

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