Project

General

Profile

Download (12 KB) Statistics
| Branch: | Tag: | Revision:
1
open Format
2
open Utils
3
open Compiler_common
4
open Lustre_types
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
(* check whether a source file has a compiled header, if not, generate the
15
   compiled header *)
16
let compile_source_to_header prog computed_types_env computed_clocks_env dirname basename extension =
17
  let destname = !Options.dest_dir ^ "/" ^ basename in
18
  let lusic_ext = ".lusic" in
19
  let header_name = destname ^ lusic_ext in
20
  begin
21
    if (* Generating the lusic file *)
22
      extension = ".lusi" (* because input is a lusi *)
23
      || (extension = ".lus" &&
24
            not (Sys.file_exists header_name))
25
           (* or because it is a lus but not lusic exists *)
26
      || (let lusic = Lusic.read_lusic destname lusic_ext in
27
          not lusic.Lusic.from_lusi)
28
         (* or the lusic exists but is not generated from a lusi, hence it
29
            has te be regenerated *)
30
    then
31
      begin
32
	Log.report ~level:1 (fun fmt -> fprintf fmt ".. generating compiled header file %s@," header_name);
33
	Lusic.write_lusic
34
          (extension = ".lusi") (* is it a lusi file ? *)
35
          (if extension = ".lusi" then prog else Lusic.extract_header dirname basename prog)
36
          destname
37
          lusic_ext;
38
        let _ =
39
          match !Options.output with
40
          | "C" -> C_backend_lusic.print_lusic_to_h destname lusic_ext
41
          | _ -> ()
42
        in
43
        ()
44
      end
45
    else (* Lusic exists and is usable. Checking compatibility *)
46
      begin
47
	Log.report ~level:1 (fun fmt -> fprintf fmt ".. loading compiled header file %s@," 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 -> fprintf fmt ".. after automata expansion:@,  @[<v 2>@,%a@]@ " Printers.pp_prog prog);
67

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

    
77
  (* Sorting nodes *)
78
  let prog = SortProg.sort prog in
79
  (* Consolidating contracts *)
80
  let prog = resolve_contracts prog in
81
  let prog = SortProg.sort prog in
82
  Log.report ~level:3 (fun fmt ->
83
      Format.fprintf fmt "@[<v 0>Contracts resolved:@ %a@ @]@ " Printers.pp_prog prog);
84
  
85
  (* Perform inlining before any analysis *)
86
  let orig, prog =
87
    if !Options.global_inline && !Options.main_node <> "" then
88
      (if !Options.witnesses then prog else []),
89
      Inliner.global_inline basename prog
90
    else (* if !Option.has_local_inline *)
91
      [],
92
      Inliner.local_inline prog (* type_env clock_env *)
93
  in
94

    
95
  (* Checking stateless/stateful status *)
96
  if Plugins.check_force_stateful () then
97
    force_stateful_decls prog
98
  else
99
    check_stateless_decls prog;
100

    
101
  (* Typing *)
102
  Global.type_env := type_decls !Global.type_env prog;
103

    
104
  (* Clock calculus *)
105
  Global.clock_env := clock_decls !Global.clock_env prog;
106

    
107
  (* Registering and checking machine types *)
108
  if Machine_types.is_active then Machine_types.load prog;
109

    
110

    
111
  (* Generating a .lusi header file only *)
112
  if !Options.lusi then
113
    (* We stop here the processing and produce the current prog. It will be
114
       exported as a lusi *)
115
    raise (StopPhase1 prog);
116

    
117
  (* Optimization of prog:
118
     - Unfold consts
119
     - eliminate trivial expressions
120
  *)
121
  (*
122
    let prog =
123
    if !Options.const_unfold || !Options.optimization >= 5 then
124
    begin
125
    Log.report ~level:1 (fun fmt -> fprintf fmt ".. eliminating constants and aliases@,");
126
    Optimize_prog.prog_unfold_consts prog
127
    end
128
    else
129
    prog
130
    in
131
  *)
132
  (* Delay calculus *)
133
  (* TO BE DONE LATER (Xavier)
134
     if(!Options.delay_calculus)
135
     then
136
     begin
137
     Log.report ~level:1 (fun fmt -> fprintf fmt ".. initialisation analysis@?");
138
     try
139
     Delay_calculus.delay_prog Basic_library.delay_env prog
140
     with (Delay.Error (loc,err)) as exc ->
141
     Location.print loc;
142
     eprintf "%a" Delay.pp_error err;
143
     Utils.track_exception ();
144
     raise exc
145
     end;
146
  *)
147

    
148
  (* Creating destination directory if needed *)
149
  create_dest_dir ();
150
     
151
  Typing.uneval_prog_generics prog;
152
  Clock_calculus.uneval_prog_generics prog;
153

    
154

    
155
(* Disabling witness option. Could but reactivated later
156
  if !Options.global_inline && !Options.main_node <> "" && !Options.witnesses then
157
    begin
158
      let orig = Corelang.copy_prog orig in
159
      Log.report ~level:1 (fun fmt -> fprintf fmt ".. generating witness file@,");
160
      check_stateless_decls orig;
161
      let _ = Typing.type_prog type_env orig in
162
      let _ = Clock_calculus.clock_prog clock_env orig in
163
      Typing.uneval_prog_generics orig;
164
      Clock_calculus.uneval_prog_generics orig;
165
      Inliner.witness
166
	basename
167
	!Options.main_node
168
	orig prog type_env clock_env
169
    end;
170
*)
171

    
172
  (* Computes and stores generic calls for each node,
173
     only useful for ANSI C90 compliant generic node compilation *)
174
  if !Options.ansi then Causality.NodeDep.compute_generic_calls prog;
175
  (*Hashtbl.iter (fun id td -> match td.Corelang.top_decl_desc with
176
    Corelang.Node nd -> Format.eprintf "%s calls %a" id
177
    Causality.NodeDep.pp_generic_calls nd | _ -> ()) Corelang.node_table;*)
178

    
179
  (* If some backend involving dynamic checks are active, then node annotations become runtime checks *)
180
  let prog =
181
    if dynamic_checks () then
182
      Spec.enforce_spec_prog prog
183
    else
184
      prog
185
  in
186

    
187

    
188
  (* (\* Registering and checking machine types *\) *)
189
  (* Machine_types.load prog; *)
190

    
191
  (* Normalization phase *)
192
  Log.report ~level:1 (fun fmt -> fprintf fmt ".. normalization@,");
193
  let prog = Normalization.normalize_prog params prog in
194
  Log.report ~level:2 (fun fmt -> fprintf fmt "@[<v 2>@ %a@]@," Printers.pp_prog prog);
195
  
196
  (* Compatibility with Lusi *)
197
  (* If compiling a lusi, generate the lusic. If this is a lus file, Check the existence of a lusi (Lustre Interface file) *)
198
  compile_source_to_header
199
    prog !Global.type_env !Global.clock_env dirname basename extension;
200

    
201

    
202
  let prog =
203
    if !Options.mpfr
204
    then
205
      begin
206
	Log.report ~level:1 (fun fmt -> fprintf fmt ".. targetting MPFR library@,");
207
	Mpfr.inject_prog prog
208
      end
209
    else
210
      begin
211
	Log.report ~level:1 (fun fmt -> fprintf fmt ".. keeping floating-point numbers@,");
212
	prog
213
      end in
214
  Log.report ~level:2 (fun fmt -> fprintf fmt "@[<v 2>@ %a@]@," Printers.pp_prog prog);
215

    
216
  (* Checking array accesses *)
217
  if !Options.check then
218
    begin
219
      Log.report ~level:1 (fun fmt -> fprintf fmt ".. checking array accesses@,");
220
      Access.check_prog prog;
221
    end;
222

    
223

    
224
  let prog = SortProg.sort_nodes_locals prog in
225

    
226
  prog, dependencies
227

    
228

    
229
    (* from source to machine code, with optimization *)
230
let stage2 prog =
231
  (* Computation of node equation scheduling. It also breaks dependency cycles
232
     and warns about unused input or memory variables *)
233
  Log.report ~level:1 (fun fmt -> fprintf fmt ".. @[<v 2>scheduling@ ");
234
  let prog, node_schs =
235
    try
236
      Scheduling.schedule_prog prog
237
    with Causality.Error _ -> (* Error is not kept. It is recomputed in a more
238
				 systemtic way in AlgebraicLoop module *)
239
      AlgebraicLoop.analyze prog
240
  in
241
  Log.report ~level:1 (fun fmt -> fprintf fmt "%a"              Scheduling.pp_warning_unused node_schs);
242
  Log.report ~level:3 (fun fmt -> fprintf fmt "@[<v 2>@ %a@]@," Scheduling.pp_schedule node_schs);
243
  Log.report ~level:3 (fun fmt -> fprintf fmt "@[<v 2>@ %a@]@," Scheduling.pp_fanin_table node_schs);
244
  Log.report ~level:5 (fun fmt -> fprintf fmt "@[<v 2>@ %a@]@," Scheduling.pp_dep_graph node_schs);
245
  Log.report ~level:3 (fun fmt -> fprintf fmt "@[<v 2>@ %a@]@," Printers.pp_prog prog);
246
  Log.report ~level:1 (fun fmt -> fprintf fmt "@]@ ");
247

    
248
  (* TODO Salsa optimize prog:
249
     - emits warning for programs with pre inside expressions
250
     - make sure each node arguments and memory is bounded by a local annotation
251
     - introduce fresh local variables for each real pure subexpression
252
  *)
253
  (* DFS with modular code generation *)
254
  Log.report ~level:1 (fun fmt -> fprintf fmt ".. machines generation@,");
255
  let machine_code = Machine_code.translate_prog prog node_schs in
256

    
257
  Log.report ~level:3 (fun fmt -> fprintf fmt ".. generated machines (unoptimized):@ %a@ " Machine_code_common.pp_machines machine_code);
258

    
259
  (* Optimize machine code *)
260
  Optimize_machine.optimize prog node_schs machine_code
261

    
262

    
263
(* printing code *)
264
let stage3 prog machine_code dependencies basename extension =
265
  let basename    =  Filename.basename basename in
266
  match !Options.output, extension with
267
    "C", ".lus" -> 
268
     begin
269
       Log.report ~level:1 (fun fmt -> fprintf fmt ".. C code generation@,");
270
       C_backend.translate_to_c
271
	 (* alloc_header_file source_lib_file source_main_file makefile_file *)
272
	 basename prog machine_code dependencies
273
     end
274
  |  "C", _ -> 
275
      begin
276
      	Log.report ~level:1 (fun fmt -> fprintf fmt ".. no C code generation for lusi@,");
277
      end
278
  | "java", _ ->
279
     begin
280
       (Format.eprintf "internal error: sorry, but not yet supported !"; assert false)
281
     (*let source_file = basename ^ ".java" in
282
       Log.report ~level:1 (fun fmt -> fprintf fmt ".. opening file %s@,@?" source_file);
283
       let source_out = open_out source_file in
284
       let source_fmt = formatter_of_out_channel source_out in
285
       Log.report ~level:1 (fun fmt -> fprintf fmt ".. java code generation@,@?");
286
       Java_backend.translate_to_java source_fmt basename normalized_prog machine_code;*)
287
     end
288
  | "Ada", _ ->
289
    begin
290
      Log.report ~level:1 (fun fmt -> fprintf fmt ".. Ada code generation@.");
291
      Ada_backend.translate_to_ada
292
      basename prog (Machine_code_common.arrow_machine::machine_code) dependencies
293
    end
294
  | "horn", _ ->
295
     begin
296
       let destname = !Options.dest_dir ^ "/" ^ basename in
297
       let source_file = destname ^ ".smt2" in (* Could be changed *)
298
       let source_out = open_out source_file in
299
       let fmt = formatter_of_out_channel source_out in
300
       Log.report ~level:1 (fun fmt -> fprintf fmt ".. hornification@,");
301
       Horn_backend.translate fmt basename prog (Machine_code_common.arrow_machine::machine_code);
302
       (* Tracability file if option is activated *)
303
       if !Options.traces then (
304
	 let traces_file = destname ^ ".traces.xml" in (* Could be changed *)
305
	 let traces_out = open_out traces_file in
306
	 let fmt = formatter_of_out_channel traces_out in
307
         Log.report ~level:1 (fun fmt -> fprintf fmt ".. tracing info@,");
308
	 Horn_backend_traces.traces_file fmt basename prog machine_code;
309
       )
310
     end
311
  | "lustre", _ ->
312
     begin
313
       let destname = !Options.dest_dir ^ "/" ^ basename in
314
       let source_file = destname ^ ".lustrec" ^ extension  in (* Could be changed *)
315
       Log.report ~level:1 (fun fmt -> fprintf fmt ".. exporting processed file as %s@," source_file);
316
       let source_out = open_out source_file in
317
       let fmt = formatter_of_out_channel source_out in
318
       Printers.pp_prog fmt prog;
319
       Format.fprintf fmt "@.@?";
320
       (*	Lustre_backend.translate fmt basename normalized_prog machine_code *)
321
       ()
322
     end
323
  | "emf", _ ->
324
     begin
325
       let destname = !Options.dest_dir ^ "/" ^ basename in
326
       let source_file = destname ^ ".json" in (* Could be changed *)
327
       let source_out = open_out source_file in
328
       let fmt = formatter_of_out_channel source_out in
329
       EMF_backend.translate fmt basename prog machine_code;
330
       ()
331
     end
332

    
333
  | _ -> assert false
(15-15/66)