Project

General

Profile

Statistics
| Branch: | Tag: | Revision:

lustrec / src / main_lustre_compiler.ml @ 8b3afe43

History | View | Annotate | Download (10.8 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"]
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: Clocks.clock_expr Utils.IMap.t = 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 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
      Printers.pp_lusi_header lusi_fmt source_name prog
196
    )
197
    | (Types.Error (loc,err)) as exc ->
198
      Format.eprintf "Type mismatch between computed type and declared type in lustre interface file: %a@]@."
199
	Types.pp_error err;
200
      raise exc
201
  in
202

    
203
  (* Computes and stores generic calls for each node,
204
     only useful for ANSI C90 compliant generic node compilation *)
205
  if !Options.ansi then Causality.NodeDep.compute_generic_calls prog;
206
  (*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;*)
207

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

    
220
  (* DFS with modular code generation *)
221
  report ~level:1 (fun fmt -> fprintf fmt ".. machines generation@,@?");
222
  let machine_code = Machine_code.translate_prog normalized_prog in
223
  report ~level:2 (fun fmt -> fprintf fmt "@[<v 2>@ %a@]@,@?"
224
    (Utils.fprintf_list ~sep:"@ " Machine_code.pp_machine)
225
    machine_code);
226

    
227
  (* Printing code *)
228
  let basename    =  Filename.basename basename in
229
  let _ = match !Options.output with
230
      "C" -> 
231
	begin
232
	  let destname = !Options.dest_dir ^ "/" ^ basename in
233
	  let header_file = destname ^ ".h" in (* Could be changed *)
234
	  let source_file = destname ^ ".c" in (* Could be changed *)
235
	  let makefile_file = destname ^ ".makefile" in (* Could be changed *)
236
	  let spec_file_opt = if !Options.c_spec then 
237
	      (
238
		let spec_file = basename ^ "_spec.c" in
239
		report ~level:1 (fun fmt -> fprintf fmt ".. opening files %s, %s and %s@,@?" header_file source_file spec_file);
240
		Some spec_file 
241
	      ) else (
242
		report ~level:1 (fun fmt -> fprintf fmt ".. opening files %s and %s@,@?" header_file source_file);
243
		None 
244
	       )
245
	  in 
246
	  let header_out = open_out header_file in
247
	  let header_fmt = formatter_of_out_channel header_out in
248
	  let source_out = open_out source_file in
249
	  let source_fmt = formatter_of_out_channel source_out in
250
	  let makefile_out = open_out makefile_file in
251
	  let makefile_fmt = formatter_of_out_channel makefile_out in
252
	  let spec_fmt_opt = match spec_file_opt with
253
	      None -> None
254
	    | Some f -> Some (formatter_of_out_channel (open_out f))
255
	  in
256
	  report ~level:1 (fun fmt -> fprintf fmt ".. C code generation@,@?");
257
	  C_backend.translate_to_c header_fmt source_fmt makefile_fmt spec_fmt_opt basename normalized_prog machine_code
258
	end
259
    | "java" ->
260
      begin
261
	failwith "Sorry, but not yet supported !"
262
    (*let source_file = basename ^ ".java" in
263
      report ~level:1 (fun fmt -> fprintf fmt ".. opening file %s@,@?" source_file);
264
      let source_out = open_out source_file in
265
      let source_fmt = formatter_of_out_channel source_out in
266
      report ~level:1 (fun fmt -> fprintf fmt ".. java code generation@,@?");
267
      Java_backend.translate_to_java source_fmt basename normalized_prog machine_code;*)
268
      end
269
    | "horn" ->
270
      begin
271
	let source_file = basename ^ ".smt2" in (* Could be changed *)
272
	let source_out = open_out source_file in
273
	let fmt = formatter_of_out_channel source_out in
274
	Horn_backend.translate fmt basename normalized_prog machine_code
275
      end
276
    | _ -> assert false
277
  in
278
  report ~level:1 (fun fmt -> fprintf fmt ".. done !@ @]@.");
279
  (* We stop the process here *)
280
  exit 0
281
  
282
let anonymous filename =
283
  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
284
  if ok_ext then
285
    let basename = Filename.chop_suffix filename ext in
286
    compile basename ext
287
  else
288
    raise (Arg.Bad ("Can only compile *.lus or *.ec files"))
289

    
290
let _ =
291
  Corelang.add_internal_funs ();
292
  try
293
    Printexc.record_backtrace true;
294
    Arg.parse Options.options anonymous usage
295
  with
296
  | Parse.Syntax_err _ | Lexer_lustre.Error _ 
297
  | Types.Error (_,_) | Clocks.Error (_,_)
298
  | Corelang.Error _ (*| Task_set.Error _*) 
299
  | Causality.Cycle _ -> exit 1
300
  | exc -> (Utils.track_exception (); raise exc)
301

    
302
(* Local Variables: *)
303
(* compile-command:"make -C .." *)
304
(* End: *)