Project

General

Profile

Statistics
| Branch: | Tag: | Revision:

lustrec / src / main_lustre_compiler.ml @ 5c1184ad

History | View | Annotate | Download (10.1 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
    (*Typing.uneval_prog_generics prog*)
40
      with (Types.Error (loc,err)) as exc ->
41
	Format.eprintf "Typing error at loc %a: %a@]@."
42
	  Location.pp_loc loc
43
	  Types.pp_error err;
44
	raise exc
45
    end 
46
  in
47
  if !Options.print_types then
48
    report ~level:1 (fun fmt -> fprintf fmt "@[<v 2>@ %a@]@,@?" Corelang.pp_prog_type decls);
49
  new_env
50
      
51
let clock_decls env decls = 
52
  report ~level:1 (fun fmt -> fprintf fmt ".. clock calculus@,@?");
53
  let new_env =
54
    begin
55
      try
56
	Clock_calculus.clock_prog env decls
57
      with (Clocks.Error (loc,err)) as exc ->
58
	Location.print loc;
59
	eprintf "Clock calculus error at loc %a: %a@]@." Location.pp_loc loc Clocks.pp_error err;
60
	raise exc
61
    end
62
  in
63
  if !Options.print_clocks then
64
    report ~level:1 (fun fmt -> fprintf fmt "@[<v 2>@ %a@]@,@?" Corelang.pp_prog_clock decls);
65
  new_env
66

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

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

    
140
  (* Delay calculus *)
141
  (*
142
    if(!Options.delay_calculus)
143
    then
144
    begin
145
    report ~level:1 (fun fmt -> fprintf fmt ".. initialisation analysis@?");
146
    try
147
    Delay_calculus.delay_prog Basic_library.delay_env prog
148
    with (Delay.Error (loc,err)) as exc ->
149
    Location.print loc;
150
    eprintf "%a" Delay.pp_error err;
151
    Utils.track_exception ();
152
    raise exc
153
    end;
154
  *)
155
  (*
156
    eprintf "Causality analysis@.@?";
157
    (* Causality analysis *)
158
    begin
159
    try
160
    Causality.check_causal_prog prog
161
    with (Causality.Cycle v) as exc ->
162
    Causality.pp_error err_formatter v;
163
    raise exc
164
    end;
165
  *)
166
  (* Computes and stores generic calls for each node,
167
     only useful for ANSI C90 compliant generic node compilation *)
168
  if !Options.ansi then Causality.NodeDep.compute_generic_calls prog;
169
  (*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;*)
170

    
171
  (* Normalization phase *)
172
  report ~level:1 (fun fmt -> fprintf fmt ".. normalization@,@?");
173
  let normalized_prog = Normalization.normalize_prog prog in
174
  Typing.uneval_prog_generics normalized_prog;
175
  report ~level:2 (fun fmt -> fprintf fmt "@[<v 2>@ %a@]@,@?" Printers.pp_prog normalized_prog);
176
  (* Checking array accesses *)
177
  if !Options.check then
178
    begin
179
      report ~level:1 (fun fmt -> fprintf fmt ".. array access checks@,@?");
180
      Access.check_prog normalized_prog;
181
    end;
182

    
183
  (* DFS with modular code generation *)
184
  report ~level:1 (fun fmt -> fprintf fmt ".. machines generation@,@?");
185
  let machine_code = Machine_code.translate_prog normalized_prog in
186
  report ~level:2 (fun fmt -> fprintf fmt "@[<v 2>@ %a@]@,@?"
187
    (Utils.fprintf_list ~sep:"@ " Machine_code.pp_machine)
188
    machine_code);
189

    
190
  (* Checking the existence of a lusi (Lustre Interface file) *)
191
  let lusi_name = basename ^ ".lusi" in
192
  let _ = 
193
    try 
194
      let _ = open_in lusi_name in
195
      let _, declared_types_env, declared_clocks_env = load_lusi lusi_name in
196
      (* checking type compatibilty with computed types*)
197
      Typing.check_env_compat declared_types_env computed_types_env;
198
      (* checking clocks compatibilty with computed clocks*)
199
      Clock_calculus.check_env_compat declared_clocks_env computed_clocks_env;
200
      
201
    with Sys_error _ -> ( 
202
      (* Printing lusi file is necessary *)
203
      report ~level:1 
204
	(fun fmt -> 
205
	  fprintf fmt 
206
	    ".. generating lustre interface file %s@,@?" lusi_name);
207
      let lusi_out = open_out lusi_name in
208
      let lusi_fmt = formatter_of_out_channel lusi_out in
209
      Printers.pp_lusi_header lusi_fmt source_name normalized_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
  in
216

    
217
  (* Printing code *)
218
  let basename    = Filename.basename basename in
219
  if !Options.java then
220
    failwith "Sorry, but not yet supported !"
221
    (*let source_file = basename ^ ".java" in
222
      report ~level:1 (fun fmt -> fprintf fmt ".. opening file %s@,@?" source_file);
223
      let source_out = open_out source_file in
224
      let source_fmt = formatter_of_out_channel source_out in
225
      report ~level:1 (fun fmt -> fprintf fmt ".. java code generation@,@?");
226
      Java_backend.translate_to_java source_fmt basename normalized_prog machine_code;*)
227
  else begin
228
    let header_file = basename ^ ".h" in (* Could be changed *)
229
    let source_file = basename ^ ".c" in (* Could be changed *)
230
    let makefile_file = basename ^ ".makefile" in (* Could be changed *)
231
    let spec_file_opt = if !Options.c_spec then 
232
	(
233
	  let spec_file = basename ^ "_spec.c" in
234
	  report ~level:1 (fun fmt -> fprintf fmt ".. opening files %s, %s and %s@,@?" header_file source_file spec_file);
235
	  Some spec_file 
236
	) else (
237
	  report ~level:1 (fun fmt -> fprintf fmt ".. opening files %s and %s@,@?" header_file source_file);
238
	  None 
239
	 )
240
    in 
241
    let header_out = open_out header_file in
242
    let header_fmt = formatter_of_out_channel header_out in
243
    let source_out = open_out source_file in
244
    let source_fmt = formatter_of_out_channel source_out in
245
    let makefile_out = open_out makefile_file in
246
    let makefile_fmt = formatter_of_out_channel makefile_out in
247
    let spec_fmt_opt = match spec_file_opt with
248
	None -> None
249
      | Some f -> Some (formatter_of_out_channel (open_out f))
250
    in
251
    report ~level:1 (fun fmt -> fprintf fmt ".. C code generation@,@?");
252
    C_backend.translate_to_c header_fmt source_fmt makefile_fmt spec_fmt_opt basename normalized_prog machine_code;
253
  end;
254
  report ~level:1 (fun fmt -> fprintf fmt ".. done !@ @]@.");
255
  (* We stop the process here *)
256
  exit 0
257
  
258
let anonymous filename =
259
  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
260
  if ok_ext then
261
    let basename = Filename.chop_suffix filename ext in
262
    compile basename ext
263
  else
264
    raise (Arg.Bad ("Can only compile *.lus or *.ec files"))
265

    
266
let _ =
267
  Corelang.add_internal_funs ();
268
  try
269
    Printexc.record_backtrace true;
270
    Arg.parse Options.options anonymous usage
271
  with
272
  | Parse.Syntax_err _ | Lexer_lustre.Error _ 
273
  | Types.Error (_,_) | Clocks.Error (_,_)
274
  | Corelang.Error _ (*| Task_set.Error _*) 
275
  | Causality.Cycle _ -> exit 1
276
  | exc -> (Utils.track_exception (); raise exc)
277

    
278
(* Local Variables: *)
279
(* compile-command:"make -C .." *)
280
(* End: *)