Project

General

Profile

Download (12.1 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

    
71
  (* Registering types and clocks for future checks *)
72
  Global.type_env := Env.overwrite !Global.type_env typ_env;
73
  Global.clock_env := Env.overwrite !Global.clock_env clk_env;
74
  
75
  (* (\* Extracting dependencies (and updating Global.(type_env/clock_env) *\)
76
   * let dependencies = import_dependencies prog in *)
77

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

    
99
  (* Checking stateless/stateful status *)
100
  if Plugins.check_force_stateful () then
101
    force_stateful_decls prog
102
  else
103
    check_stateless_decls prog;
104

    
105
  (* Typing *)
106
  Global.type_env := type_decls !Global.type_env prog;
107

    
108
  (* Clock calculus *)
109
  Global.clock_env := clock_decls !Global.clock_env prog;
110

    
111
  (* Registering and checking machine types *)
112
  if Machine_types.is_active then Machine_types.load prog;
113

    
114

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

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

    
152
  (* Creating destination directory if needed *)
153
  create_dest_dir ();
154
     
155
  Typing.uneval_prog_generics prog;
156
  Clock_calculus.uneval_prog_generics prog;
157

    
158

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

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

    
183
  (* If some backend involving dynamic checks are active, then node annotations become runtime checks *)
184
  let prog =
185
    if dynamic_checks () then
186
      Spec.enforce_spec_prog prog
187
    else
188
      prog
189
  in
190

    
191

    
192
  (* (\* Registering and checking machine types *\) *)
193
  (* Machine_types.load prog; *)
194

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

    
205

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

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

    
227

    
228
  let prog = SortProg.sort_nodes_locals prog in
229

    
230
  prog, dependencies
231

    
232

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

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

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

    
263
  (* Optimize machine code *)
264
  Optimize_machine.optimize prog node_schs machine_code
265

    
266

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

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