Project

General

Profile

Download (12.5 KB) Statistics
| Branch: | Tag: | Revision:
1
open Format
2
open Compiler_common
3
open Lustre_types
4
module Mpfr = Lustrec_mpfr
5

    
6
exception StopPhase1 of program_t
7

    
8
let dynamic_checks () =
9
  match !Options.output, !Options.spec with "C", "C" -> true | _ -> false
10

    
11
let generate_c_header = ref false
12

    
13
(* check whether a source file has a compiled header, if not, generate the
14
   compiled header *)
15
let compile_source_to_header prog computed_types_env computed_clocks_env dirname
16
    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
  let from_lusi = extension = ".lusi" in
21
  if
22
    (* Generating the lusic file *)
23
    (* because input is a lusi *)
24
    from_lusi
25
    (* or because it is a lus but no lusic exists *)
26
    || (extension = ".lus" && not (Sys.file_exists header_name))
27
    (* or the lusic exists but is not generated from a lusi, hence it has te be
28
       regenerated *)
29
    ||
30
    let lusic = Lusic.read_lusic destname lusic_ext in
31
    not lusic.Lusic.from_lusi
32
  then (
33
    Log.report ~level:1 (fun fmt ->
34
        fprintf fmt "@ .. generating compiled header file %s@," header_name);
35
    Lusic.write_lusic from_lusi
36
      (* is it a lusi file ? *)
37
      (if from_lusi then prog else Lusic.extract_header dirname basename prog)
38
      destname lusic_ext;
39
    generate_c_header := !Options.output = "C")
40
  else (
41
    (* Lusic exists and is usable. Checking compatibility *)
42
    Log.report ~level:1 (fun fmt ->
43
        fprintf fmt "@ .. loading compiled header file %s@," header_name);
44
    let lusic = Lusic.read_lusic destname lusic_ext in
45
    Lusic.check_obsolete lusic destname;
46
    let header = lusic.Lusic.contents in
47
    let declared_types_env, declared_clocks_env =
48
      Modules.get_envs_from_top_decls header
49
    in
50
    check_compatibility
51
      (prog, computed_types_env, computed_clocks_env)
52
      (header, declared_types_env, declared_clocks_env))
53

    
54
(* From prog to prog *)
55
let stage1 params prog dirname basename extension =
56
  (* Updating parent node information for variables *)
57
  Compiler_common.update_vdecl_parents_prog prog;
58

    
59
  (* Removing automata *)
60
  let prog = expand_automata prog in
61
  Log.report ~level:4 (fun fmt ->
62
      fprintf fmt "@[<v 2>.. after automata expansion:@ %a@]@ " Printers.pp_prog
63
        prog
64
      (* Utils.Format.pp_print_nothing () *));
65

    
66
  (* Importing source *)
67
  let prog, dependencies, (typ_env, clk_env) =
68
    Modules.load ~is_header:(extension = ".lusi") prog
69
  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 2>.. contracts resolved:@ %a@ @]@ "
84
        Printers.pp_prog prog);
85

    
86
  (* Consolidating main node *)
87
  let _ =
88
    match !Options.main_node with
89
    | "" ->
90
      ()
91
    | main_node -> (
92
      Global.main_node := main_node;
93
      try ignore (Corelang.node_from_name main_node)
94
      with Not_found ->
95
        Format.eprintf "Code generation error: %a@." Error.pp_error_msg
96
          Error.Main_not_found;
97
        raise (Error.Error (Location.dummy_loc, Error.Main_not_found)))
98
  in
99

    
100
  (* Perform inlining before any analysis *)
101
  let _, prog =
102
    if !Options.global_inline && !Global.main_node <> "" then
103
      (if !Options.witnesses then prog else []), Inliner.global_inline prog
104
    else (* if !Option.has_local_inline *)
105
      [], Inliner.local_inline prog
106
    (* type_env clock_env *)
107
  in
108

    
109
  (* Checking stateless/stateful status *)
110
  if Plugins.check_force_stateful () then force_stateful_decls prog
111
  else check_stateless_decls prog;
112

    
113
  (* Typing *)
114
  Global.type_env := type_decls !Global.type_env prog;
115

    
116
  (* Clock calculus *)
117
  Global.clock_env := clock_decls !Global.clock_env prog;
118

    
119
  (* Registering and checking machine types *)
120
  if Machine_types.is_active then Machine_types.load prog;
121

    
122
  (* Generating a .lusi header file only *)
123
  if !Options.lusi || !Options.print_nodes then
124
    (* We stop here the processing and produce the current prog. It will be
125
       exported as a lusi *)
126
    raise (StopPhase1 prog);
127

    
128
  (* Optimization of prog: - Unfold consts - eliminate trivial expressions *)
129
  let prog =
130
    if !Options.const_unfold || !Options.optimization >= 5 then (
131
      Log.report ~level:1 (fun fmt ->
132
          fprintf fmt ".. eliminating constants and aliases@,");
133
      Optimize_prog.prog_unfold_consts prog)
134
    else prog
135
  in
136

    
137
  (* Delay calculus *)
138
  (* TO BE DONE LATER (Xavier) if(!Options.delay_calculus) then begin Log.report
139
     ~level:1 (fun fmt -> fprintf fmt ".. initialisation analysis@?"); try
140
     Delay_calculus.delay_prog Basic_library.delay_env prog with (Delay.Error
141
     (loc,err)) as exc -> Location.print loc; eprintf "%a" Delay.pp_error err;
142
     Utils.track_exception (); raise exc end; *)
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
  (* Disabling witness option. Could but reactivated later if
151
     !Options.global_inline && !Options.main_node <> "" && !Options.witnesses
152
     then begin let orig = Corelang.copy_prog orig in Log.report ~level:1 (fun
153
     fmt -> fprintf fmt ".. generating witness file@,"); check_stateless_decls
154
     orig; let _ = Typing.type_prog type_env orig in let _ =
155
     Clock_calculus.clock_prog clock_env orig in Typing.uneval_prog_generics
156
     orig; Clock_calculus.uneval_prog_generics orig; Inliner.witness basename
157
     !Options.main_node orig prog type_env clock_env end; *)
158

    
159
  (* Computes and stores generic calls for each node, only useful for ANSI C90
160
     compliant generic node compilation *)
161
  if !Options.ansi then Causality.NodeDep.compute_generic_calls prog;
162

    
163
  (*Hashtbl.iter (fun id td -> match td.Corelang.top_decl_desc with
164
    Corelang.Node nd -> Format.eprintf "%s calls %a" id
165
    Causality.NodeDep.pp_generic_calls nd | _ -> ()) Corelang.node_table;*)
166

    
167
  (* If some backend involving dynamic checks are active, then node annotations
168
     become runtime checks *)
169
  let prog = if dynamic_checks () then Spec.enforce_spec_prog prog else prog in
170

    
171
  (* (\* Registering and checking machine types *\) *)
172
  (* Machine_types.load prog; *)
173

    
174
  (* Normalization phase *)
175
  Log.report ~level:1 (fun fmt -> fprintf fmt "@ .. normalization@ ");
176
  let prog = Normalization.normalize_prog params prog in
177
  Log.report ~level:2 (fun fmt ->
178
      fprintf fmt "@[<v 2>@ %a@]@ " Printers.pp_prog_short prog);
179
  Log.report ~level:3 (fun fmt ->
180
      fprintf fmt "@[<v 2>@ %a@]@ " Printers.pp_prog prog);
181

    
182
  (* Compatibility with Lusi *)
183
  (* If compiling a lusi, generate the lusic. If this is a lus file, Check the
184
     existence of a lusi (Lustre Interface file) *)
185
  if !Options.compile_header then
186
    compile_source_to_header prog !Global.type_env !Global.clock_env dirname
187
      basename extension;
188

    
189
  let prog =
190
    if !Options.mpfr then (
191
      Log.report ~level:1 (fun fmt ->
192
          fprintf fmt "@ .. targetting MPFR library@,");
193
      Mpfr.inject_prog prog)
194
    else (
195
      Log.report ~level:1 (fun fmt ->
196
          fprintf fmt "@ .. keeping floating-point numbers@,");
197
      prog)
198
  in
199
  Log.report ~level:3 (fun fmt ->
200
      fprintf fmt "@[<v 2>@ %a@]@," Printers.pp_prog prog);
201

    
202
  (* Checking array accesses *)
203
  if !Options.check then (
204
    Log.report ~level:1 (fun fmt ->
205
        fprintf fmt "@ .. checking array accesses@,");
206
    Access.check_prog prog);
207

    
208
  let prog = SortProg.sort_nodes_locals prog in
209

    
210
  prog, dependencies
211

    
212
(* from source to machine code, with optimization *)
213
let stage2 params prog =
214
  (* Computation of node equation scheduling. It also breaks dependency cycles
215
     and warns about unused input or memory variables *)
216
  Log.report ~level:1 (fun fmt -> fprintf fmt "@[<v 2>.. scheduling@ ");
217
  let prog, node_schs =
218
    try Scheduling.schedule_prog prog
219
    with Causality.Error _ ->
220
      (* Error is not kept. It is recomputed in a more systemtic way in
221
         AlgebraicLoop module *)
222
      AlgebraicLoop.analyze prog
223
  in
224
  Scheduling.(
225
    Log.report ~level:1 (fun fmt -> pp_warning_unused fmt node_schs);
226
    Log.report ~level:3 (fun fmt -> fprintf fmt "@ %a" pp_schedule node_schs);
227
    Log.report ~level:3 (fun fmt -> fprintf fmt "@ %a" pp_fanin_table node_schs);
228
    Log.report ~level:5 (fun fmt -> fprintf fmt "@ %a" pp_dep_graph node_schs);
229
    Log.report ~level:3 (fun fmt -> fprintf fmt "@ %a" Printers.pp_prog prog);
230
    Log.report ~level:1 (fun fmt -> fprintf fmt "@]@ "));
231

    
232
  (* TODO Salsa optimize prog: - emits warning for programs with pre inside
233
     expressions - make sure each node arguments and memory is bounded by a
234
     local annotation - introduce fresh local variables for each real pure
235
     subexpression *)
236
  (* DFS with modular code generation *)
237
  Log.report ~level:1 (fun fmt ->
238
      fprintf fmt "@ @[<v 2>.. machines generation@ ");
239
  let machine_code = Machine_code.translate_prog prog node_schs in
240
  Log.report ~level:1 (fun fmt -> fprintf fmt "@]");
241

    
242
  Log.report ~level:3 (fun fmt ->
243
      fprintf fmt "@ @[<v 2>.. generated machines (unoptimized):@ %a@]@ "
244
        Machine_code_common.pp_machines machine_code);
245

    
246
  (* Optimize machine code *)
247
  Optimize_machine.optimize params prog node_schs machine_code
248

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