Project

General

Profile

Statistics
| Branch: | Tag: | Revision:

lustrec / src / main_lustre_compiler.ml @ 5fccce23

History | View | Annotate | Download (5.55 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 8446bf03 ploc
open Lustre_types
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 5fccce23 ploc
    (* Disbaled today, should be done anyway when following the regular compilation 
46
ignore (Modules.load ~is_header:true ISet.empty header); *)
47 2d27eedd ploc
    ignore (check_top_decls header); (* typing/clocking with an empty env *)
48 6efbcb73 xthirioux
    create_dest_dir ();
49
    Log.report ~level:1
50
      (fun fmt -> fprintf fmt ".. generating compiled header file %sc@," (destname ^ extension));
51
    Lusic.write_lusic true header destname lusic_ext;
52 e70326c9 ploc
    generate_lusic_header destname lusic_ext;
53 5fccce23 ploc
54
55
56 521e2a6b ploc
    Log.report ~level:1 (fun fmt -> fprintf fmt ".. done !@ @]@ ")
57 6efbcb73 xthirioux
  end
58
59 04a63d25 xthirioux
60 6efbcb73 xthirioux
61 04a63d25 xthirioux
62
63
(* compile a .lus source file *)
64
let rec compile_source dirname basename extension =
65
  let source_name = dirname ^ "/" ^ basename ^ extension in
66
67 521e2a6b ploc
  Log.report ~level:1 (fun fmt -> fprintf fmt "@[<v 0>");
68 04a63d25 xthirioux
69
  (* Parsing source *)
70
  let prog = parse_source source_name in
71
72
  let prog =
73
    if !Options.mpfr then
74
      Mpfr.mpfr_module::prog
75
    else
76
      prog
77
  in
78 ad4774b0 ploc
  let params = Backends.get_normalization_params () in
79
80 04a63d25 xthirioux
  let prog, dependencies = 
81 521e2a6b ploc
    Log.report ~level:1 (fun fmt -> fprintf fmt "@[<v 2>.. Phase 1 : Normalisation@,");
82 04a63d25 xthirioux
    try 
83 ad4774b0 ploc
      Compiler_stages.stage1 params prog dirname basename
84 04257b1e ploc
    with Compiler_stages.StopPhase1 prog -> (
85 04a63d25 xthirioux
      if !Options.lusi then
86
	begin
87
	  let lusi_ext = extension ^ "i" in
88 521e2a6b ploc
	  Log.report ~level:1 (fun fmt -> fprintf fmt ".. generating interface file %s@ " (basename ^ lusi_ext));
89 04a63d25 xthirioux
	  print_lusi prog dirname basename lusi_ext;
90
	  Log.report ~level:1 (fun fmt -> fprintf fmt ".. done !@ @]@.");
91
	  exit 0
92
	end
93
      else
94
        assert false
95
    )
96 6efbcb73 xthirioux
  in
97 521e2a6b ploc
  Log.report ~level:3 (fun fmt -> fprintf fmt ".. Normalized program:@ %a@ "Printers.pp_prog prog);
98 8e6cab20 ploc
  Log.report ~level:1 (fun fmt -> fprintf fmt "@]@,");
99 521e2a6b ploc
100
  Log.report ~level:1 (fun fmt -> fprintf fmt "@[<v 2>.. Phase 2 : Machines generation@,");
101 04a63d25 xthirioux
102
  let machine_code = 
103 66359a5e ploc
    Compiler_stages.stage2 prog 
104 04a63d25 xthirioux
  in
105 521e2a6b ploc
106 2863281f ploc
  Log.report ~level:3 (fun fmt -> fprintf fmt ".. Generated machines:@ %a@ " Machine_code_common.pp_machines machine_code);
107 521e2a6b ploc
108 04a63d25 xthirioux
  if Scopes.Plugin.show_scopes () then
109
    begin
110
      let all_scopes = Scopes.compute_scopes prog !Options.main_node in
111
      (* Printing scopes *)
112
      if !Options.verbose_level >= 1 then
113
	Format.printf "Possible scopes are:@   ";
114 521e2a6b ploc
      Format.printf "@[<v>%a@ @]@ @?" Scopes.print_scopes all_scopes;
115 04a63d25 xthirioux
      exit 0
116
	
117
    end;
118 ca88e660 Ploc
  let machine_code = Plugins.refine_machine_code prog machine_code in
119 bc9fd714 ploc
  Log.report ~level:1 (fun fmt -> fprintf fmt "@]@ @ ");
120 04a63d25 xthirioux
  
121 66359a5e ploc
  Compiler_stages.stage3 prog machine_code dependencies basename;
122 f4acee4c xthirioux
  begin
123 ca88e660 Ploc
    Log.report ~level:1 (fun fmt -> fprintf fmt ".. done !@ @]@.");
124 04a63d25 xthirioux
    (* We stop the process here *)
125 f4acee4c xthirioux
    exit 0
126
  end
127 6efbcb73 xthirioux
128
let compile dirname basename extension =
129
  match extension with
130
  | ".lusi"  -> compile_header dirname basename extension
131
  | ".lus"   -> compile_source dirname basename extension
132
  | _        -> assert false
133
134
let anonymous filename =
135
  let ok_ext, ext = List.fold_left
136
    (fun (ok, ext) ext' ->
137
      if not ok && Filename.check_suffix filename ext' then
138
	true, ext'
139
      else
140
	ok, ext)
141
    (false, "") extensions in
142
  if ok_ext then
143
    let dirname = Filename.dirname filename in
144
    let basename = Filename.chop_suffix (Filename.basename filename) ext in
145
    compile dirname basename ext
146
  else
147
    raise (Arg.Bad ("Can only compile *.lusi, *.lus or *.ec files"))
148
149
let _ =
150 04a63d25 xthirioux
  Global.initialize ();
151 6efbcb73 xthirioux
  Corelang.add_internal_funs ();
152
  try
153
    Printexc.record_backtrace true;
154 04a63d25 xthirioux
155 1bff14ac ploc
    let options = Options_management.lustrec_options @ (Plugins.options ()) in
156 04a63d25 xthirioux
    
157
    Arg.parse options anonymous usage
158 6efbcb73 xthirioux
  with
159 04a63d25 xthirioux
  | Parse.Error _
160 e7cc5186 ploc
  | Types.Error (_,_) | Clocks.Error (_,_) -> exit 1
161
  | Corelang.Error (_ (* loc *), kind) (*| Task_set.Error _*) -> exit (Error.return_code kind)
162
  (* | Causality.Error _  -> exit (Error.return_code Error.AlgebraicLoop) *)
163 1ca48e48 ploc
  | Sys_error msg -> (eprintf "Failure: %s@." msg); exit 1
164 e7cc5186 ploc
  | exc -> (track_exception (); raise exc) 
165 6efbcb73 xthirioux
166
(* Local Variables: *)
167
(* compile-command:"make -C .." *)
168
(* End: *)