Project

General

Profile

Statistics
| Branch: | Tag: | Revision:

lustrec / src / main_lustre_compiler.ml @ 1ca48e48

History | View | Annotate | Download (9.51 KB)

1 6efbcb73 xthirioux
(********************************************************************)
2
(*                                                                  *)
3
(*  The LustreC compiler toolset   /  The LustreC Development Team  *)
4
(*  Copyright 2012 -    --   ONERA - CNRS - INPT                    *)
5
(*                                                                  *)
6
(*  LustreC is free software, distributed WITHOUT ANY WARRANTY      *)
7
(*  under the terms of the GNU Lesser General Public License        *)
8
(*  version 2.1.                                                    *)
9
(*                                                                  *)
10
(********************************************************************)
11
12
open Format
13
open Log
14 04257b1e ploc
open Compiler_common
15 6efbcb73 xthirioux
16
open Utils
17
open LustreSpec
18 04a63d25 xthirioux
 
19 6efbcb73 xthirioux
20 04a63d25 xthirioux
let usage = "Usage: lustrec [options] \x1b[4msource file\x1b[0m"
21 6efbcb73 xthirioux
22
let extensions = [".ec"; ".lus"; ".lusi"]
23
24
(* print a .lusi header file from a source prog *)
25
let print_lusi prog dirname basename extension =
26
  let header = Lusic.extract_header dirname basename prog in
27
  let header_name = dirname ^ "/" ^ basename ^ extension in
28
  let h_out = open_out header_name in
29
  let h_fmt = formatter_of_out_channel h_out in
30
  begin
31 77a61575 xthirioux
    Typing.uneval_prog_generics header;
32
    Clock_calculus.uneval_prog_generics header;
33 6efbcb73 xthirioux
    Printers.pp_lusi_header h_fmt basename header;
34
    close_out h_out
35
  end
36
37
(* compile a .lusi header file *)
38
let compile_header dirname  basename extension =
39
  let destname = !Options.dest_dir ^ "/" ^ basename in
40
  let header_name = basename ^ extension in
41
  let lusic_ext = extension ^ "c" in
42
  begin
43
    Log.report ~level:1 (fun fmt -> fprintf fmt "@[<v>");
44
    let header = parse_header true (dirname ^ "/" ^ header_name) in
45
    ignore (Modules.load_header ISet.empty header);
46
    ignore (check_top_decls header);
47
    create_dest_dir ();
48
    Log.report ~level:1
49
      (fun fmt -> fprintf fmt ".. generating compiled header file %sc@," (destname ^ extension));
50
    Lusic.write_lusic true header destname lusic_ext;
51 e70326c9 ploc
    generate_lusic_header destname lusic_ext;
52 521e2a6b ploc
    Log.report ~level:1 (fun fmt -> fprintf fmt ".. done !@ @]@ ")
53 6efbcb73 xthirioux
  end
54
55 04a63d25 xthirioux
56 6efbcb73 xthirioux
57 04a63d25 xthirioux
58
(* from source to machine code, with optimization *)
59
let stage2 prog =    
60 6efbcb73 xthirioux
  (* Computation of node equation scheduling. It also breaks dependency cycles
61
     and warns about unused input or memory variables *)
62 264a4844 ploc
  Log.report ~level:1 (fun fmt -> fprintf fmt ".. @[<v 2>scheduling@ ");
63 e7cc5186 ploc
  let prog, node_schs =
64
    try 
65
      Scheduling.schedule_prog prog
66
    with Causality.Error _ -> (* Error is not kept. It is recomputed in a more
67
				 systemtic way in AlgebraicLoop module *)
68
      AlgebraicLoop.analyze prog
69
  in
70 04a63d25 xthirioux
  Log.report ~level:1 (fun fmt -> fprintf fmt "%a"              Scheduling.pp_warning_unused node_schs);
71 6efbcb73 xthirioux
  Log.report ~level:3 (fun fmt -> fprintf fmt "@[<v 2>@ %a@]@," Scheduling.pp_schedule node_schs);
72
  Log.report ~level:3 (fun fmt -> fprintf fmt "@[<v 2>@ %a@]@," Scheduling.pp_fanin_table node_schs);
73 04a63d25 xthirioux
  Log.report ~level:5 (fun fmt -> fprintf fmt "@[<v 2>@ %a@]@," Scheduling.pp_dep_graph node_schs);
74 6efbcb73 xthirioux
  Log.report ~level:3 (fun fmt -> fprintf fmt "@[<v 2>@ %a@]@," Printers.pp_prog prog);
75 264a4844 ploc
  Log.report ~level:1 (fun fmt -> fprintf fmt "@]@ ");
76 04a63d25 xthirioux
77
  (* TODO Salsa optimize prog: 
78
     - emits warning for programs with pre inside expressions
79
     - make sure each node arguments and memory is bounded by a local annotation
80
     - introduce fresh local variables for each real pure subexpression
81
  *)
82 6efbcb73 xthirioux
  (* DFS with modular code generation *)
83 521e2a6b ploc
  Log.report ~level:1 (fun fmt -> fprintf fmt ".. machines generation@,");
84 6efbcb73 xthirioux
  let machine_code = Machine_code.translate_prog prog node_schs in
85
86 521e2a6b ploc
  Log.report ~level:3 (fun fmt -> fprintf fmt ".. generated machines (unoptimized):@ %a@ "Machine_code.pp_machines machine_code);
87 2d179f5b xthirioux
88 6efbcb73 xthirioux
  (* Optimize machine code *)
89 13507742 ploc
  Optimize_machine.optimize prog node_schs machine_code
90 c287ba28 xthirioux
91 8deaa2dd tkahsai
92 04a63d25 xthirioux
(* printing code *)
93
let stage3 prog machine_code dependencies basename =
94 6efbcb73 xthirioux
  let basename    =  Filename.basename basename in
95 04a63d25 xthirioux
  match !Options.output with
96 dc893173 ploc
    "C" -> 
97 6efbcb73 xthirioux
      begin
98 dc893173 ploc
	Log.report ~level:1 (fun fmt -> fprintf fmt ".. C code generation@,");
99
	C_backend.translate_to_c
100
	  (* alloc_header_file source_lib_file source_main_file makefile_file *)
101
	  basename prog machine_code dependencies
102 6efbcb73 xthirioux
      end
103 dc893173 ploc
  | "java" ->
104
     begin
105
       (Format.eprintf "internal error: sorry, but not yet supported !"; assert false)
106
     (*let source_file = basename ^ ".java" in
107
       Log.report ~level:1 (fun fmt -> fprintf fmt ".. opening file %s@,@?" source_file);
108
       let source_out = open_out source_file in
109
       let source_fmt = formatter_of_out_channel source_out in
110
       Log.report ~level:1 (fun fmt -> fprintf fmt ".. java code generation@,@?");
111
       Java_backend.translate_to_java source_fmt basename normalized_prog machine_code;*)
112
     end
113
  | "horn" ->
114
     begin
115
       let destname = !Options.dest_dir ^ "/" ^ basename in
116
       let source_file = destname ^ ".smt2" in (* Could be changed *)
117
       let source_out = open_out source_file in
118
       let fmt = formatter_of_out_channel source_out in
119
       Log.report ~level:1 (fun fmt -> fprintf fmt ".. hornification@,");
120
       Horn_backend.translate fmt basename prog (Machine_code.arrow_machine::machine_code);
121
       (* Tracability file if option is activated *)
122
       if !Options.traces then (
123
	 let traces_file = destname ^ ".traces.xml" in (* Could be changed *)
124
	 let traces_out = open_out traces_file in
125
	 let fmt = formatter_of_out_channel traces_out in
126
         Log.report ~level:1 (fun fmt -> fprintf fmt ".. tracing info@,");
127
	 Horn_backend_traces.traces_file fmt basename prog machine_code;
128
       )
129
     end
130
  | "lustre" ->
131
     begin
132
       let destname = !Options.dest_dir ^ "/" ^ basename in
133
       let source_file = destname ^ ".lustrec.lus" in (* Could be changed *)
134
       let source_out = open_out source_file in
135
       let fmt = formatter_of_out_channel source_out in
136
       Printers.pp_prog fmt prog;
137 103f6f60 ploc
       Format.fprintf fmt "@.@?";
138 dc893173 ploc
       (*	Lustre_backend.translate fmt basename normalized_prog machine_code *)
139
       ()
140
     end
141 a6df3992 Ploc
  | "emf" ->
142
     begin
143
       let destname = !Options.dest_dir ^ "/" ^ basename in
144
       let source_file = destname ^ ".emf" in (* Could be changed *)
145
       let source_out = open_out source_file in
146
       let fmt = formatter_of_out_channel source_out in
147 3ca27bc7 ploc
       EMF_backend.translate fmt basename prog machine_code;
148 a6df3992 Ploc
       ()
149
     end
150 dc893173 ploc
151
  | _ -> assert false
152 04a63d25 xthirioux
153
(* compile a .lus source file *)
154
let rec compile_source dirname basename extension =
155
  let source_name = dirname ^ "/" ^ basename ^ extension in
156
157 521e2a6b ploc
  Log.report ~level:1 (fun fmt -> fprintf fmt "@[<v 0>");
158 04a63d25 xthirioux
159
  (* Parsing source *)
160
  let prog = parse_source source_name in
161
162
  let prog =
163
    if !Options.mpfr then
164
      Mpfr.mpfr_module::prog
165
    else
166
      prog
167
  in
168
  let prog, dependencies = 
169 521e2a6b ploc
    Log.report ~level:1 (fun fmt -> fprintf fmt "@[<v 2>.. Phase 1 : Normalisation@,");
170 04a63d25 xthirioux
    try 
171 04257b1e ploc
      Compiler_stages.stage1 prog dirname basename
172
    with Compiler_stages.StopPhase1 prog -> (
173 04a63d25 xthirioux
      if !Options.lusi then
174
	begin
175
	  let lusi_ext = extension ^ "i" in
176 521e2a6b ploc
	  Log.report ~level:1 (fun fmt -> fprintf fmt ".. generating interface file %s@ " (basename ^ lusi_ext));
177 04a63d25 xthirioux
	  print_lusi prog dirname basename lusi_ext;
178
	  Log.report ~level:1 (fun fmt -> fprintf fmt ".. done !@ @]@.");
179
	  exit 0
180
	end
181
      else
182
        assert false
183
    )
184 6efbcb73 xthirioux
  in
185 521e2a6b ploc
  Log.report ~level:1 (fun fmt -> fprintf fmt "@]@,");
186
  Log.report ~level:3 (fun fmt -> fprintf fmt ".. Normalized program:@ %a@ "Printers.pp_prog prog);
187
188
  Log.report ~level:1 (fun fmt -> fprintf fmt "@[<v 2>.. Phase 2 : Machines generation@,");
189 04a63d25 xthirioux
190
  let machine_code = 
191
    stage2 prog 
192
  in
193 521e2a6b ploc
194
  Log.report ~level:1 (fun fmt -> fprintf fmt "@]@ ");
195
  Log.report ~level:3 (fun fmt -> fprintf fmt ".. Generated machines:@ %a@ "Machine_code.pp_machines machine_code);
196
197 04a63d25 xthirioux
  if Scopes.Plugin.show_scopes () then
198
    begin
199
      let all_scopes = Scopes.compute_scopes prog !Options.main_node in
200
      (* Printing scopes *)
201
      if !Options.verbose_level >= 1 then
202
	Format.printf "Possible scopes are:@   ";
203 521e2a6b ploc
      Format.printf "@[<v>%a@ @]@ @?" Scopes.print_scopes all_scopes;
204 04a63d25 xthirioux
      exit 0
205
	
206
    end;
207
208 ca88e660 Ploc
  let machine_code = Plugins.refine_machine_code prog machine_code in
209 04a63d25 xthirioux
  
210
  stage3 prog machine_code dependencies basename;
211 f4acee4c xthirioux
  begin
212 ca88e660 Ploc
    Log.report ~level:1 (fun fmt -> fprintf fmt ".. done !@ @]@.");
213 04a63d25 xthirioux
    (* We stop the process here *)
214 f4acee4c xthirioux
    exit 0
215
  end
216 6efbcb73 xthirioux
217
let compile dirname basename extension =
218
  match extension with
219
  | ".lusi"  -> compile_header dirname basename extension
220
  | ".lus"   -> compile_source dirname basename extension
221
  | _        -> assert false
222
223
let anonymous filename =
224
  let ok_ext, ext = List.fold_left
225
    (fun (ok, ext) ext' ->
226
      if not ok && Filename.check_suffix filename ext' then
227
	true, ext'
228
      else
229
	ok, ext)
230
    (false, "") extensions in
231
  if ok_ext then
232
    let dirname = Filename.dirname filename in
233
    let basename = Filename.chop_suffix (Filename.basename filename) ext in
234
    compile dirname basename ext
235
  else
236
    raise (Arg.Bad ("Can only compile *.lusi, *.lus or *.ec files"))
237
238
let _ =
239 04a63d25 xthirioux
  Global.initialize ();
240 6efbcb73 xthirioux
  Corelang.add_internal_funs ();
241
  try
242
    Printexc.record_backtrace true;
243 04a63d25 xthirioux
244 1bff14ac ploc
    let options = Options_management.lustrec_options @ (Plugins.options ()) in
245 04a63d25 xthirioux
    
246
    Arg.parse options anonymous usage
247 6efbcb73 xthirioux
  with
248 04a63d25 xthirioux
  | Parse.Error _
249 e7cc5186 ploc
  | Types.Error (_,_) | Clocks.Error (_,_) -> exit 1
250
  | Corelang.Error (_ (* loc *), kind) (*| Task_set.Error _*) -> exit (Error.return_code kind)
251
  (* | Causality.Error _  -> exit (Error.return_code Error.AlgebraicLoop) *)
252 1ca48e48 ploc
  | Sys_error msg -> (eprintf "Failure: %s@." msg); exit 1
253 e7cc5186 ploc
  | exc -> (track_exception (); raise exc) 
254 6efbcb73 xthirioux
255
(* Local Variables: *)
256
(* compile-command:"make -C .." *)
257
(* End: *)