Project

General

Profile

Statistics
| Branch: | Tag: | Revision:

lustrec / src / main_lustre_compiler.ml @ f9f06e7d

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