Project

General

Profile

Statistics
| Branch: | Tag: | Revision:

lustrec / src / main_lustre_compiler.ml @ 89b9e25c

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