Project

General

Profile

Download (11.4 KB) Statistics
| Branch: | Tag: | Revision:
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 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
(* Loading Lusi file and filing type tables with parsed
67
   functions/nodes *)
68
let load_lusi filename =
69
  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
  try
75
    Parse.prog Parser_lustre.header Lexer_lustre.token lexbuf
76
  with (Lexer_lustre.Error err) | (Parse.Syntax_err err) as exc -> 
77
    Parse.report_error err;
78
    raise exc
79

    
80
let check_lusi header =
81
  let new_tenv = type_decls Basic_library.type_env header in   (* Typing *)
82
  let new_cenv = clock_decls Basic_library.clock_env header in   (* Clock calculus *)
83
  header, new_tenv, new_cenv
84
    
85
let rec compile basename extension =
86
  (* Loading the input file *)
87
  let source_name = basename^extension in
88
  Location.input_name := source_name;
89
  let lexbuf = Lexing.from_channel (open_in source_name) in
90
  Location.init lexbuf source_name;
91
  (* Parsing *)
92
  report ~level:1 
93
    (fun fmt -> fprintf fmt "@[<v>.. parsing file %s@,@?" source_name);
94
  let prog =
95
    try
96
      Parse.prog Parser_lustre.prog Lexer_lustre.token lexbuf
97
    with (Lexer_lustre.Error err) | (Parse.Syntax_err err) as exc -> 
98
      Parse.report_error err;
99
      raise exc
100
  in
101
  (* Extracting dependencies *)
102
  report ~level:1 (fun fmt -> fprintf fmt ".. extracting dependencies@,@?");
103
  let dependencies = 
104
    List.fold_right 
105
      (fun d accu -> match d.Corelang.top_decl_desc with 
106
      | Corelang.Open s -> s::accu 
107
      | _ -> accu) 
108
      prog [] 
109
  in
110
  let type_env, clock_env =
111
    List.fold_left (fun (type_env, clock_env) s -> 
112
      try
113
	let basename = s ^ ".lusi" in 
114
	report ~level:1 (fun fmt -> fprintf fmt "@[<v 2>Library %s@ " s);
115
	let _, lusi_type_env, lusi_clock_env = check_lusi (load_lusi basename) in 
116
	report ~level:1 (fun fmt -> fprintf fmt "@]@,@?");
117
	Env.overwrite type_env lusi_type_env,
118
	Env.overwrite clock_env lusi_clock_env      
119
      with Sys_error msg -> (
120
	Format.eprintf "Failure: impossible to load library %s.@.%s@." s msg;
121
	exit 1
122
      )
123
    )  (Basic_library.type_env, Basic_library.clock_env) dependencies
124
  in
125
  
126
  (* Unfold consts *)
127
  (*let prog = Corelang.prog_unfold_consts prog in*)
128

    
129
  (* Sorting nodes *)
130
  let prog = SortProg.sort prog in
131
  
132
  (* Typing *)
133
  let computed_types_env = type_decls type_env prog in
134
  
135
  (* Clock calculus *)
136
  let computed_clocks_env = clock_decls clock_env prog in
137

    
138
  (* Perform global inlining *)
139
  let prog =
140
    if !Options.global_inline && 
141
      (match !Options.main_node with | "" -> false | _ -> true) then
142
      Inliner.global_inline basename prog type_env clock_env
143
    else
144
      prog
145
  in
146

    
147
  (* Delay calculus *)
148
  (*
149
    if(!Options.delay_calculus)
150
    then
151
    begin
152
    report ~level:1 (fun fmt -> fprintf fmt ".. initialisation analysis@?");
153
    try
154
    Delay_calculus.delay_prog Basic_library.delay_env prog
155
    with (Delay.Error (loc,err)) as exc ->
156
    Location.print loc;
157
    eprintf "%a" Delay.pp_error err;
158
    Utils.track_exception ();
159
    raise exc
160
    end;
161
  *)
162
  (*
163
    eprintf "Causality analysis@.@?";
164
    (* Causality analysis *)
165
    begin
166
    try
167
    Causality.check_causal_prog prog
168
    with (Causality.Cycle v) as exc ->
169
    Causality.pp_error err_formatter v;
170
    raise exc
171
    end;
172
  *)
173

    
174
  (* Checking the existence of a lusi (Lustre Interface file) *)
175
  let lusi_name = basename ^ ".lusi" in
176
  let _ = 
177
    try 
178
      let _ = open_in lusi_name in
179
      let header = load_lusi lusi_name in
180
      let _, declared_types_env, declared_clocks_env = check_lusi header in
181
      (* checking type compatibility with computed types*)
182
      Typing.check_env_compat header declared_types_env computed_types_env;
183
      (* checking clocks compatibilty with computed clocks*)
184
      Clock_calculus.check_env_compat header declared_clocks_env computed_clocks_env;
185
      Typing.uneval_prog_generics prog
186
    with Sys_error _ -> ( 
187
      (* Printing lusi file is necessary *)
188
      report ~level:1 
189
	(fun fmt -> 
190
	  fprintf fmt 
191
	    ".. generating lustre interface file %s@,@?" lusi_name);
192
      let lusi_out = open_out lusi_name in
193
      let lusi_fmt = formatter_of_out_channel lusi_out in
194
      Typing.uneval_prog_generics prog;
195
      Clock_calculus.uneval_prog_generics prog;
196
      Printers.pp_lusi_header lusi_fmt source_name prog
197
    )
198
    | (Types.Error (loc,err)) as exc ->
199
      Format.eprintf "Type mismatch between computed type and declared type in lustre interface file: %a@]@."
200
	Types.pp_error err;
201
      raise exc
202
    | Clocks.Error (loc, err) as exc ->
203
      Format.eprintf "Clock mismatch between computed clock and declared clock in lustre interface file: %a@]@."
204
	Clocks.pp_error err;
205
      raise exc
206
  in
207

    
208
  (* Computes and stores generic calls for each node,
209
     only useful for ANSI C90 compliant generic node compilation *)
210
  if !Options.ansi then Causality.NodeDep.compute_generic_calls prog;
211
  (*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;*)
212

    
213
  (* Normalization phase *)
214
  report ~level:1 (fun fmt -> fprintf fmt ".. normalization@,@?");
215
  let normalized_prog = Normalization.normalize_prog prog in
216
  (*Typing.uneval_prog_generics normalized_prog;*)
217
  report ~level:2 (fun fmt -> fprintf fmt "@[<v 2>@ %a@]@,@?" Printers.pp_prog normalized_prog);
218
  (* Checking array accesses *)
219
  if !Options.check then
220
    begin
221
      report ~level:1 (fun fmt -> fprintf fmt ".. array access checks@,@?");
222
      Access.check_prog normalized_prog;
223
    end;
224

    
225
  (* DFS with modular code generation *)
226
  report ~level:1 (fun fmt -> fprintf fmt ".. machines generation@,@?");
227
  let machine_code = Machine_code.translate_prog normalized_prog in
228
  report ~level:2 (fun fmt -> fprintf fmt "@[<v 2>@ %a@]@,@?"
229
    (Utils.fprintf_list ~sep:"@ " Machine_code.pp_machine)
230
    machine_code);
231
  
232
  (* Creating destination directory if needed *)
233
  if not (Sys.file_exists !Options.dest_dir) then (
234
    report ~level:1 (fun fmt -> fprintf fmt ".. creating destination directory@,@?");
235
    Unix.mkdir !Options.dest_dir (Unix.stat ".").Unix.st_perm
236
  );
237
  if (Unix.stat !Options.dest_dir).Unix.st_kind <> Unix.S_DIR then (
238
    Format.eprintf "Failure: destination %s is not a directory.@.@." !Options.dest_dir;
239
    exit 1
240
  );
241
  (* Printing code *)
242
  let basename    =  Filename.basename basename in
243
  let destname = !Options.dest_dir ^ "/" ^ basename in
244
  let _ = match !Options.output with
245
      "C" -> 
246
	begin
247
	  let header_file = destname ^ ".h" in (* Could be changed *)
248
	  let source_file = destname ^ ".c" in (* Could be changed *)
249
	  let makefile_file = destname ^ ".makefile" in (* Could be changed *)
250
	  let spec_file_opt = if !Options.c_spec then 
251
	      (
252
		let spec_file = basename ^ "_spec.c" in
253
		report ~level:1 (fun fmt -> fprintf fmt ".. opening files %s, %s and %s@,@?" header_file source_file spec_file);
254
		Some spec_file 
255
	      ) else (
256
		report ~level:1 (fun fmt -> fprintf fmt ".. opening files %s and %s@,@?" header_file source_file);
257
		None 
258
	       )
259
	  in 
260
	  let header_out = open_out header_file in
261
	  let header_fmt = formatter_of_out_channel header_out in
262
	  let source_out = open_out source_file in
263
	  let source_fmt = formatter_of_out_channel source_out in
264
	  let makefile_out = open_out makefile_file in
265
	  let makefile_fmt = formatter_of_out_channel makefile_out in
266
	  let spec_fmt_opt = match spec_file_opt with
267
	      None -> None
268
	    | Some f -> Some (formatter_of_out_channel (open_out f))
269
	  in
270
	  report ~level:1 (fun fmt -> fprintf fmt ".. C code generation@,@?");
271
	  C_backend.translate_to_c header_fmt source_fmt makefile_fmt spec_fmt_opt basename normalized_prog machine_code dependencies
272
	end
273
    | "java" ->
274
      begin
275
	failwith "Sorry, but not yet supported !"
276
    (*let source_file = basename ^ ".java" in
277
      report ~level:1 (fun fmt -> fprintf fmt ".. opening file %s@,@?" source_file);
278
      let source_out = open_out source_file in
279
      let source_fmt = formatter_of_out_channel source_out in
280
      report ~level:1 (fun fmt -> fprintf fmt ".. java code generation@,@?");
281
      Java_backend.translate_to_java source_fmt basename normalized_prog machine_code;*)
282
      end
283
    | "horn" ->
284
      begin
285
	let source_file = destname ^ ".smt2" in (* Could be changed *)
286
	let source_out = open_out source_file in
287
	let fmt = formatter_of_out_channel source_out in
288
	Horn_backend.translate fmt basename normalized_prog machine_code
289
      end
290
    | _ -> assert false
291
  in
292
  report ~level:1 (fun fmt -> fprintf fmt ".. done !@ @]@.");
293
  (* We stop the process here *)
294
  exit 0
295
  
296
let anonymous filename =
297
  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
298
  if ok_ext then
299
    let basename = Filename.chop_suffix filename ext in
300
    compile basename ext
301
  else
302
    raise (Arg.Bad ("Can only compile *.lusi, *.lus or *.ec files"))
303

    
304
let _ =
305
  Corelang.add_internal_funs ();
306
  try
307
    Printexc.record_backtrace true;
308
    Arg.parse Options.options anonymous usage
309
  with
310
  | Parse.Syntax_err _ | Lexer_lustre.Error _ 
311
  | Types.Error (_,_) | Clocks.Error (_,_)
312
  | Corelang.Error _ (*| Task_set.Error _*) 
313
  | Causality.Cycle _ -> exit 1
314
  | exc -> (Utils.track_exception (); raise exc)
315

    
316
(* Local Variables: *)
317
(* compile-command:"make -C .." *)
318
(* End: *)
(32-32/46)