Project

General

Profile

Statistics
| Branch: | Tag: | Revision:

lustrec / src / main_lustre_compiler.ml @ 7ecdb0aa

History | View | Annotate | Download (13.9 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
let usage = "Usage: lustrec [options] <source-file>"
30

    
31
let extensions = [".ec"; ".lus"; ".lusi"]
32

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

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

    
75
(* Loading Lusi file and filling type tables with parsed
76
   functions/nodes *)
77
let load_lusi own filename =
78
  Location.input_name := filename;
79
  let lexbuf = Lexing.from_channel (open_in filename) in
80
  Location.init lexbuf filename;
81
  (* Parsing *)
82
  Log.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
      eprintf "Parsing error %a%a@."
91
	Corelang.pp_error err
92
	Location.pp_loc loc;
93
      raise exc
94
    )
95

    
96

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

    
102
let load_n_check_lusi source_name lusi_name prog computed_types_env computed_clocks_env= 
103
  try 
104
    let _ = open_in lusi_name in
105
    let header = load_lusi true lusi_name in
106
    let _, declared_types_env, declared_clocks_env = check_lusi header in
107
        
108
    (* checking stateless status compatibility *)
109
    Stateless.check_compat header;
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
    with Sys_error _ -> ( 
120
      (* Printing lusi file is necessary *)
121
      Log.report ~level:1 
122
	(fun fmt -> 
123
	  fprintf fmt 
124
	    ".. generating lustre interface file %s@," lusi_name);
125
      let lusi_out = open_out lusi_name in
126
      let lusi_fmt = formatter_of_out_channel lusi_out in
127
      Typing.uneval_prog_generics prog;
128
      Clock_calculus.uneval_prog_generics prog;
129
      Printers.pp_lusi_header lusi_fmt source_name prog
130
    )
131
    | (Types.Error (loc,err)) as exc ->
132
      eprintf "Type mismatch between computed type and declared type in lustre interface file: %a@."
133
	Types.pp_error err;
134
      raise exc
135
    | Clocks.Error (loc, err) as exc ->
136
      eprintf "Clock mismatch between computed clock and declared clock in lustre interface file: %a@."
137
	Clocks.pp_error err;
138
      raise exc
139
    | Stateless.Error (loc, err) as exc ->
140
      eprintf "Stateless status mismatch between defined status and declared status in lustre interface file: %a@."
141
	Stateless.pp_error err;
142
      raise exc
143
    
144
let rec compile basename extension =
145

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

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

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

    
200
  (* Checking stateless/stateful status *)
201
  check_stateless_decls prog;
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
  (* Perform global inlining *)
210
  let prog =
211
    if !Options.global_inline && 
212
      (match !Options.main_node with | "" -> false | _ -> true) then
213
      Inliner.global_inline basename prog type_env clock_env
214
    else
215
      prog
216
  in
217

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

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

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

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

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

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

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

    
290
  (* DFS with modular code generation *)
291
  Log.report ~level:1 (fun fmt -> fprintf fmt ".. machines generation@,");
292
  let machine_code = Machine_code.translate_prog prog node_schs in
293
  Log.report ~level:2 (fun fmt -> fprintf fmt "@[<v 2>@ %a@]@,"
294
    (Utils.fprintf_list ~sep:"@ " Machine_code.pp_machine)
295
    machine_code);
296

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

    
366
    | _ -> assert false
367
  in
368
  Log.report ~level:1 (fun fmt -> fprintf fmt ".. done !@ @]@.");
369
  (* We stop the process here *)
370
  exit 0
371
  
372
let anonymous filename =
373
  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
374
  if ok_ext then
375
    let basename = Filename.chop_suffix filename ext in
376
    compile basename ext
377
  else
378
    raise (Arg.Bad ("Can only compile *.lusi, *.lus or *.ec files"))
379

    
380
let _ =
381
  Corelang.add_internal_funs ();
382
  try
383
    Printexc.record_backtrace true;
384
    Arg.parse Options.options anonymous usage
385
  with
386
  | Parse.Syntax_err _ | Lexer_lustre.Error _ 
387
  | Types.Error (_,_) | Clocks.Error (_,_)
388
  | Corelang.Error _ (*| Task_set.Error _*) 
389
  | Causality.Cycle _ -> exit 1
390
  | exc -> (Utils.track_exception (); raise exc)
391

    
392
(* Local Variables: *)
393
(* compile-command:"make -C .." *)
394
(* End: *)