Project

General

Profile

Statistics
| Branch: | Tag: | Revision:

lustrec / src / compiler_stages.ml @ f4cba4b8

History | View | Annotate | Download (11.7 KB)

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

    
81
  (* Consolidating contracts *)
82
  let prog = resolve_contracts prog in
83
  
84
  (* Perform inlining before any analysis *)
85
  let orig, prog =
86
    if !Options.global_inline && !Options.main_node <> "" then
87
      (if !Options.witnesses then prog else []),
88
      Inliner.global_inline basename prog 
89
    else (* if !Option.has_local_inline *)
90
      [],
91
      Inliner.local_inline prog (* type_env clock_env *)
92
  in
93

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

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

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

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

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

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

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

    
153

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

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

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

    
186

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

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

    
200

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

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

    
222
  
223
  let prog = SortProg.sort_nodes_locals prog in
224
  
225
  prog, dependencies
226

    
227

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

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

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

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

    
261

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

    
326
  | _ -> assert false