Project

General

Profile

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

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

    
91
  (* Checking stateless/stateful status *)
92
  if Plugins.check_force_stateful () then
93
    force_stateful_decls prog
94
  else
95
    check_stateless_decls prog;
96

    
97
  (* Typing *)
98
  Global.type_env := type_decls !Global.type_env prog;
99

    
100
  (* Clock calculus *)
101
  Global.clock_env := clock_decls !Global.clock_env prog;
102

    
103
  (* Registering and checking machine types *)
104
  if Machine_types.is_active then Machine_types.load prog;
105
  
106

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

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

    
144
  (* Creating destination directory if needed *)
145
  create_dest_dir ();
146
     
147
  Typing.uneval_prog_generics prog;
148
  Clock_calculus.uneval_prog_generics prog;
149

    
150

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

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

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

    
183

    
184
  (* (\* Registering and checking machine types *\) *)
185
  (* Machine_types.load prog; *)
186

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

    
197

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

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

    
219
  
220
  let prog = SortProg.sort_nodes_locals prog in
221
  
222
  prog, dependencies
223

    
224

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

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

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

    
255
  (* Optimize machine code *)
256
  Optimize_machine.optimize prog node_schs machine_code   
257

    
258

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

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