Project

General

Profile

Download (12.3 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
  if !Options.compile_header then
199
    compile_source_to_header
200
      prog !Global.type_env !Global.clock_env dirname basename extension;
201

    
202

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

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

    
224

    
225
  let prog = SortProg.sort_nodes_locals prog in
226

    
227
  prog, dependencies
228

    
229

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

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

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

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

    
263

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

    
343
  | _ -> assert false
(15-15/67)