Project

General

Profile

Download (13.1 KB) Statistics
| Branch: | Tag: | Revision:
1
open Format
2
open Compiler_common
3
open Lustre_types
4
module Mpfr = Lustrec_mpfr
5

    
6
exception StopPhase1 of program_t
7

    
8
let dynamic_checks () =
9
  match !Options.output, !Options.spec with
10
  | "C", "C" -> true
11
  | _ -> false
12

    
13

    
14
let generate_c_header = ref false
15

    
16
(* check whether a source file has a compiled header, if not, generate the
17
   compiled header *)
18
let compile_source_to_header prog computed_types_env computed_clocks_env dirname basename extension =
19
  let destname = !Options.dest_dir ^ "/" ^ basename in
20
  let lusic_ext = ".lusic" in
21
  let header_name = destname ^ lusic_ext in
22
  let from_lusi = extension = ".lusi" in
23
  begin
24
    if (* Generating the lusic file *)
25
      (* because input is a lusi *)
26
      from_lusi
27
      (* or because it is a lus but no lusic exists *)
28
      || (extension = ".lus" && not (Sys.file_exists header_name))
29
      (* or the lusic exists but is not generated from a lusi, hence it
30
         has te be regenerated *)
31
      || (let lusic = Lusic.read_lusic destname lusic_ext in
32
          not lusic.Lusic.from_lusi)
33
    then
34
      begin
35
        Log.report ~level:1 (fun fmt -> fprintf fmt "@ .. generating compiled header file %s@,"
36
                                header_name);
37
        Lusic.write_lusic
38
          from_lusi (* is it a lusi file ? *)
39
          (if from_lusi then prog else Lusic.extract_header dirname basename prog)
40
          destname
41
          lusic_ext;
42
        generate_c_header := !Options.output = "C";
43
      end
44
    else (* Lusic exists and is usable. Checking compatibility *)
45
      begin
46
        Log.report ~level:1 (fun fmt -> fprintf fmt "@ .. loading compiled header file %s@,"
47
                                header_name);
48
        let lusic = Lusic.read_lusic destname lusic_ext in
49
        Lusic.check_obsolete lusic destname;
50
        let header = lusic.Lusic.contents in
51
        let (declared_types_env, declared_clocks_env) = Modules.get_envs_from_top_decls header in
52
        check_compatibility
53
          (prog, computed_types_env, computed_clocks_env)
54
          (header, declared_types_env, declared_clocks_env)
55
      end
56
  end
57

    
58

    
59
(* From prog to prog *)
60
let stage1 params prog dirname basename extension =
61
  (* Updating parent node information for variables *)
62
  Compiler_common.update_vdecl_parents_prog prog;
63

    
64
  (* Removing automata *)
65
  let prog = expand_automata prog in
66
  Log.report ~level:4 (fun fmt ->
67
      fprintf fmt "@[<v 2>.. after automata expansion:@ %a@]@ "
68
        Printers.pp_prog prog
69
        (* Utils.Format.pp_print_nothing () *)
70
    );
71

    
72
  (* Importing source *)
73
  let prog, dependencies, (typ_env, clk_env) = Modules.load ~is_header:(extension = ".lusi") prog in
74
  (* Registering types and clocks for future checks *)
75
  Global.type_env := Env.overwrite !Global.type_env typ_env;
76
  Global.clock_env := Env.overwrite !Global.clock_env clk_env;
77
  
78
  (* (\* Extracting dependencies (and updating Global.(type_env/clock_env) *\)
79
   * let dependencies = import_dependencies prog in *)
80

    
81
  (* Sorting nodes *)
82
  let prog = SortProg.sort prog in
83
  (* Consolidating contracts *)
84
  let prog = resolve_contracts prog in
85
  let prog = SortProg.sort prog in
86
  Log.report ~level:3 (fun fmt ->
87
      Format.fprintf fmt "@ @[<v 2>.. contracts resolved:@ %a@ @]@ " Printers.pp_prog prog);
88

    
89
  (* Consolidating main node *)
90
  let _ =
91
    match !Options.main_node with
92
    | "" -> ()
93
    | main_node -> (
94
      Global.main_node := main_node;
95
      try
96
        ignore (Corelang.node_from_name main_node)
97
      with Not_found -> (
98
        Format.eprintf "Code generation error: %a@." Error.pp_error_msg Error.Main_not_found;
99
        raise (Error.Error (Location.dummy_loc, Error.Main_not_found))
100
    ))
101
  in
102
  
103
  (* Perform inlining before any analysis *)
104
  let _, prog =
105
    if !Options.global_inline && !Global.main_node <> "" then
106
      (if !Options.witnesses then prog else []),
107
      Inliner.global_inline prog
108
    else (* if !Option.has_local_inline *)
109
      [],
110
      Inliner.local_inline prog (* type_env clock_env *)
111
  in
112

    
113
  (* Checking stateless/stateful status *)
114
  if Plugins.check_force_stateful () then
115
    force_stateful_decls prog
116
  else
117
    check_stateless_decls prog;
118

    
119
  (* Typing *)
120
  Global.type_env := type_decls !Global.type_env prog;
121

    
122
  (* Clock calculus *)
123
  Global.clock_env := clock_decls !Global.clock_env prog;
124

    
125
  (* Registering and checking machine types *)
126
  if Machine_types.is_active then Machine_types.load prog;
127

    
128

    
129
  (* Generating a .lusi header file only *)
130
  if !Options.lusi || !Options.print_nodes then
131
    (* We stop here the processing and produce the current prog. It will be
132
       exported as a lusi *)
133
    raise (StopPhase1 prog);
134

    
135
  (* Optimization of prog:
136
     - Unfold consts
137
     - eliminate trivial expressions
138
   *)
139
  
140
  let prog =
141
    if !Options.const_unfold || !Options.optimization >= 5 then
142
      begin
143
        Log.report ~level:1 (fun fmt -> fprintf fmt ".. eliminating constants and aliases@,");
144
        Optimize_prog.prog_unfold_consts prog
145
      end
146
    else
147
      prog
148
  in
149
  
150
  (* Delay calculus *)
151
  (* TO BE DONE LATER (Xavier)
152
     if(!Options.delay_calculus)
153
     then
154
     begin
155
     Log.report ~level:1 (fun fmt -> fprintf fmt ".. initialisation analysis@?");
156
     try
157
     Delay_calculus.delay_prog Basic_library.delay_env prog
158
     with (Delay.Error (loc,err)) as exc ->
159
     Location.print loc;
160
     eprintf "%a" Delay.pp_error err;
161
     Utils.track_exception ();
162
     raise exc
163
     end;
164
   *)
165

    
166
  (* Creating destination directory if needed *)
167
  create_dest_dir ();
168
  
169
  Typing.uneval_prog_generics prog;
170
  Clock_calculus.uneval_prog_generics prog;
171

    
172

    
173
  (* Disabling witness option. Could but reactivated later
174
  if !Options.global_inline && !Options.main_node <> "" && !Options.witnesses then
175
    begin
176
      let orig = Corelang.copy_prog orig in
177
      Log.report ~level:1 (fun fmt -> fprintf fmt ".. generating witness file@,");
178
      check_stateless_decls orig;
179
      let _ = Typing.type_prog type_env orig in
180
      let _ = Clock_calculus.clock_prog clock_env orig in
181
      Typing.uneval_prog_generics orig;
182
      Clock_calculus.uneval_prog_generics orig;
183
      Inliner.witness
184
	basename
185
	!Options.main_node
186
	orig prog type_env clock_env
187
    end;
188
   *)
189

    
190
  (* Computes and stores generic calls for each node,
191
     only useful for ANSI C90 compliant generic node compilation *)
192
  if !Options.ansi then Causality.NodeDep.compute_generic_calls prog;
193
  (*Hashtbl.iter (fun id td -> match td.Corelang.top_decl_desc with
194
    Corelang.Node nd -> Format.eprintf "%s calls %a" id
195
    Causality.NodeDep.pp_generic_calls nd | _ -> ()) Corelang.node_table;*)
196

    
197
  (* If some backend involving dynamic checks are active, then node annotations become runtime checks *)
198
  let prog =
199
    if dynamic_checks () then
200
      Spec.enforce_spec_prog prog
201
    else
202
      prog
203
  in
204

    
205

    
206
  (* (\* Registering and checking machine types *\) *)
207
  (* Machine_types.load prog; *)
208

    
209
  (* Normalization phase *)
210
  Log.report ~level:1 (fun fmt -> fprintf fmt "@ .. normalization@ ");
211
  let prog = Normalization.normalize_prog params prog in
212
  Log.report ~level:2 (fun fmt -> fprintf fmt "@[<v 2>@ %a@]@ "
213
                          Printers.pp_prog_short prog);
214
  Log.report ~level:3  (fun fmt -> fprintf fmt "@[<v 2>@ %a@]@ "
215
                           Printers.pp_prog prog);
216
  
217
  (* Compatibility with Lusi *)
218
  (* If compiling a lusi, generate the lusic. If this is a lus file, Check the existence of a lusi (Lustre Interface file) *)
219
  if !Options.compile_header then
220
    compile_source_to_header
221
      prog !Global.type_env !Global.clock_env dirname basename extension;
222

    
223

    
224
  let prog =
225
    if !Options.mpfr
226
    then
227
      begin
228
	Log.report ~level:1 (fun fmt -> fprintf fmt "@ .. targetting MPFR library@,");
229
	Mpfr.inject_prog prog
230
      end
231
    else
232
      begin
233
	Log.report ~level:1 (fun fmt -> fprintf fmt "@ .. keeping floating-point numbers@,");
234
	prog
235
      end in
236
  Log.report ~level:3 (fun fmt -> fprintf fmt "@[<v 2>@ %a@]@," Printers.pp_prog prog);
237

    
238
  (* Checking array accesses *)
239
  if !Options.check then
240
    begin
241
      Log.report ~level:1 (fun fmt -> fprintf fmt "@ .. checking array accesses@,");
242
      Access.check_prog prog;
243
    end;
244

    
245

    
246
  let prog = SortProg.sort_nodes_locals prog in
247

    
248
  prog, dependencies
249

    
250

    
251
    (* from source to machine code, with optimization *)
252
let stage2 params prog =
253
  (* Computation of node equation scheduling. It also breaks dependency cycles
254
     and warns about unused input or memory variables *)
255
  Log.report ~level:1 (fun fmt -> fprintf fmt "@[<v 2>.. scheduling@ ");
256
  let prog, node_schs =
257
    try
258
      Scheduling.schedule_prog prog
259
    with Causality.Error _ -> (* Error is not kept. It is recomputed in a more
260
				 systemtic way in AlgebraicLoop module *)
261
      AlgebraicLoop.analyze prog
262
  in
263
  Scheduling.(
264
    Log.report ~level:1 (fun fmt -> pp_warning_unused fmt node_schs);
265
    Log.report ~level:3 (fun fmt -> fprintf fmt "@ %a" pp_schedule node_schs);
266
    Log.report ~level:3 (fun fmt -> fprintf fmt "@ %a" pp_fanin_table node_schs);
267
    Log.report ~level:5 (fun fmt -> fprintf fmt "@ %a" pp_dep_graph node_schs);
268
    Log.report ~level:3 (fun fmt -> fprintf fmt "@ %a" Printers.pp_prog prog);
269
    Log.report ~level:1 (fun fmt -> fprintf fmt "@]@ "));
270

    
271
  (* TODO Salsa optimize prog:
272
     - emits warning for programs with pre inside expressions
273
     - make sure each node arguments and memory is bounded by a local annotation
274
     - introduce fresh local variables for each real pure subexpression
275
  *)
276
  (* DFS with modular code generation *)
277
  Log.report ~level:1 (fun fmt -> fprintf fmt "@ @[<v 2>.. machines generation@ ");
278
  let machine_code = Machine_code.translate_prog prog node_schs in
279
  Log.report ~level:1 (fun fmt -> fprintf fmt "@]");
280

    
281
  Log.report ~level:3 (fun fmt -> fprintf fmt "@ @[<v 2>.. generated machines (unoptimized):@ %a@]@ "
282
                          Machine_code_common.pp_machines machine_code);
283

    
284
  (* Optimize machine code *)
285
  Optimize_machine.optimize params prog node_schs machine_code
286

    
287

    
288
(* printing code *)
289
let stage3 prog machine_code dependencies basename extension =
290
  let basename    =  Filename.basename basename in
291
  match !Options.output, extension with
292
    "C", ".lus" -> 
293
     begin
294
       Log.report ~level:1 (fun fmt -> fprintf fmt ".. C code generation@,");
295
       C_backend.translate_to_c !generate_c_header
296
	 (* alloc_header_file source_lib_file source_main_file makefile_file *)
297
	 basename prog machine_code dependencies
298
     end
299
(*
300
  |  "acsl", ".lus" -> 
301
     begin
302
       Log.report ~level:1 (fun fmt -> fprintf fmt ".. ACSL annotations generation@,");
303
       ACSL_backend.translate_to_acsl
304
	 (* alloc_header_file source_lib_file source_main_file makefile_file *)
305
	 basename prog machine_code dependencies
306
     end
307
*)
308
  |  "C", _ -> 
309
      begin
310
        C_backend.print_c_header basename;
311
      	Log.report ~level:1 (fun fmt -> fprintf fmt ".. no C code generation for lusi@,");
312
      end
313
  | "java", _ ->
314
     begin
315
       (Format.eprintf "internal error: sorry, but not yet supported !"; assert false)
316
     (*let source_file = basename ^ ".java" in
317
       Log.report ~level:1 (fun fmt -> fprintf fmt ".. opening file %s@,@?" source_file);
318
       let source_out = open_out source_file in
319
       let source_fmt = formatter_of_out_channel source_out in
320
       Log.report ~level:1 (fun fmt -> fprintf fmt ".. java code generation@,@?");
321
       Java_backend.translate_to_java source_fmt basename normalized_prog machine_code;*)
322
     end
323
  | "Ada", _ ->
324
    begin
325
      Log.report ~level:1 (fun fmt -> fprintf fmt ".. Ada code generation@.");
326
      Ada_backend.translate_to_ada
327
        basename (Machine_code_common.arrow_machine::machine_code)
328
    end
329
  | "horn", _ ->
330
     begin
331
       let destname = !Options.dest_dir ^ "/" ^ basename in
332
       let source_file = destname ^ ".smt2" in (* Could be changed *)
333
       let source_out = open_out source_file in
334
       let fmt = formatter_of_out_channel source_out in
335
       Log.report ~level:1 (fun fmt -> fprintf fmt ".. hornification@,");
336
       Horn_backend.translate fmt prog (Machine_code_common.arrow_machine::machine_code);
337
       (* Tracability file if option is activated *)
338
       if !Options.traces then (
339
	 let traces_file = destname ^ ".traces.xml" in (* Could be changed *)
340
	 let traces_out = open_out traces_file in
341
	 let fmt = formatter_of_out_channel traces_out in
342
         Log.report ~level:1 (fun fmt -> fprintf fmt ".. tracing info@,");
343
	 Horn_backend_traces.traces_file fmt machine_code;
344
       )
345
     end
346
  | "lustre", _ ->
347
     begin
348
       let destname = !Options.dest_dir ^ "/" ^ basename in
349
       let source_file = destname ^ ".lustrec" ^ extension  in (* Could be changed *)
350
       Log.report ~level:1 (fun fmt -> fprintf fmt ".. exporting processed file as %s@," source_file);
351
       let source_out = open_out source_file in
352
       let fmt = formatter_of_out_channel source_out in
353
       Printers.pp_prog fmt prog;
354
       Format.fprintf fmt "@.@?";
355
       (*	Lustre_backend.translate fmt basename normalized_prog machine_code *)
356
       ()
357
     end
358
  | "emf", _ ->
359
     begin
360
       let destname = !Options.dest_dir ^ "/" ^ basename in
361
       let source_file = destname ^ ".json" in (* Could be changed *)
362
       let source_out = open_out source_file in
363
       let fmt = formatter_of_out_channel source_out in
364
       EMF_backend.translate fmt basename prog machine_code;
365
       ()
366
     end
367

    
368
  | _ -> assert false
(14-14/63)