Project

General

Profile

Statistics
| Branch: | Tag: | Revision:

lustrec / src / main_lustre_compiler.ml @ c02d255e

History | View | Annotate | Download (10.3 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
  (* Perform global inlining *)
141
  let prog =
142
    if !Options.global_inline && 
143
      (match !Options.main_node with | "" -> false | _ -> true) then
144
      Inliner.global_inline prog type_env clock_env
145
    else
146
      prog
147
  in
148

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

    
180
  (* Normalization phase *)
181
  report ~level:1 (fun fmt -> fprintf fmt ".. normalization@,@?");
182
  let normalized_prog = Normalization.normalize_prog prog in
183
  Typing.uneval_prog_generics normalized_prog;
184
  report ~level:2 (fun fmt -> fprintf fmt "@[<v 2>@ %a@]@,@?" Printers.pp_prog normalized_prog);
185
  (* Checking array accesses *)
186
  if !Options.check then
187
    begin
188
      report ~level:1 (fun fmt -> fprintf fmt ".. array access checks@,@?");
189
      Access.check_prog normalized_prog;
190
    end;
191

    
192
  (* DFS with modular code generation *)
193
  report ~level:1 (fun fmt -> fprintf fmt ".. machines generation@,@?");
194
  let machine_code = Machine_code.translate_prog normalized_prog in
195
  report ~level:2 (fun fmt -> fprintf fmt "@[<v 2>@ %a@]@,@?"
196
    (Utils.fprintf_list ~sep:"@ " Machine_code.pp_machine)
197
    machine_code);
198

    
199
  (* Checking the existence of a lusi (Lustre Interface file) *)
200
  let lusi_name = basename ^ ".lusi" in
201
  let _ = 
202
    try 
203
      let _ = open_in lusi_name in
204
      let _, declared_types_env, declared_clocks_env = load_lusi lusi_name in
205
      (* checking type compatibilty with computed types*)
206
      Typing.check_env_compat declared_types_env computed_types_env;
207
      (* checking clocks compatibilty with computed clocks*)
208
      Clock_calculus.check_env_compat declared_clocks_env computed_clocks_env;
209
      
210
    with Sys_error _ -> ( 
211
      (* Printing lusi file is necessary *)
212
      report ~level:1 
213
	(fun fmt -> 
214
	  fprintf fmt 
215
	    ".. generating lustre interface file %s@,@?" lusi_name);
216
      let lusi_out = open_out lusi_name in
217
      let lusi_fmt = formatter_of_out_channel lusi_out in
218
      Printers.pp_lusi_header lusi_fmt source_name normalized_prog
219
    )
220
    | (Types.Error (loc,err)) as exc ->
221
      Format.eprintf "Type mismatch between computed type and declared type in lustre interface file: %a@]@."
222
	Types.pp_error err;
223
      raise exc
224
  in
225

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

    
275
let _ =
276
  Corelang.add_internal_funs ();
277
  try
278
    Printexc.record_backtrace true;
279
    Arg.parse Options.options anonymous usage
280
  with
281
  | Parse.Syntax_err _ | Lexer_lustre.Error _ 
282
  | Types.Error (_,_) | Clocks.Error (_,_)
283
  | Corelang.Error _ (*| Task_set.Error _*) 
284
  | Causality.Cycle _ -> exit 1
285
  | exc -> (Utils.track_exception (); raise exc)
286

    
287
(* Local Variables: *)
288
(* compile-command:"make -C .." *)
289
(* End: *)