Project

General

Profile

Download (5 KB) Statistics
| Branch: | Tag: | Revision:
1
(********************************************************************)
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 Compiler_common
14

    
15
open Utils
16

    
17

    
18
let usage = "Usage: lustrec [options] \x1b[4msource file\x1b[0m"
19

    
20
let extensions = [".ec"; ".lus"; ".lusi"]
21

    
22
(* print a .lusi header file from a source prog *)
23
let print_lusi prog dirname basename extension =
24
  let header = Lusic.extract_header dirname basename prog in
25
  let header_name = dirname ^ "/" ^ basename ^ extension in
26
  let h_out = open_out header_name in
27
  let h_fmt = formatter_of_out_channel h_out in
28
  begin
29
    Typing.uneval_prog_generics header;
30
    Clock_calculus.uneval_prog_generics header;
31
    Printers.pp_lusi_header h_fmt basename header;
32
    close_out h_out
33
  end
34

    
35

    
36

    
37

    
38

    
39

    
40
(* compile a .lus source file *)
41
let compile dirname basename extension =
42
  let source_name = dirname ^ "/" ^ basename ^ extension in
43

    
44
  Log.report ~level:1 (fun fmt -> fprintf fmt "@[<v 0>");
45

    
46
  (* Parsing source *)
47
  let prog = parse source_name extension in
48
  
49
  let prog =
50
    if !Options.mpfr &&
51
         extension = ".lus" (* trying to avoid the injection of the module for lusi files *) 
52
    then
53
      Lustrec_mpfr.mpfr_module::prog
54
    else
55
      prog
56
  in
57
  let params = Backends.get_normalization_params () in
58

    
59
  let prog, dependencies = 
60
    Log.report ~level:1 (fun fmt -> fprintf fmt "@[<v 2>.. Phase 1: Normalisation@,");
61
    try 
62
      Compiler_stages.stage1 params prog dirname basename extension
63
    with Compiler_stages.StopPhase1 prog -> (
64
      if !Options.lusi then
65
	begin
66
	  let lusi_ext = extension ^ "i" in
67
	  Log.report ~level:1 (fun fmt -> fprintf fmt ".. generating interface file %s@ " (basename ^ lusi_ext));
68
	  print_lusi prog dirname basename lusi_ext;
69
	  Log.report ~level:1 (fun fmt -> fprintf fmt ".. done !@ @]@.");
70
	  exit 0
71
	end
72
      else if !Options.print_nodes then (
73
        Format.printf "%a@.@?" Printers.pp_node_list prog;
74
        exit 0
75
      )
76
      else
77
        assert false
78
    )
79
  in
80
  Log.report ~level:3 (fun fmt -> fprintf fmt "@ @[<v 2>.. Normalized program:@ %a@]"
81
                          Printers.pp_prog prog);
82

    
83
  Log.report ~level:1 (fun fmt -> fprintf fmt "@]@ @ @[<v 2>.. Phase 2 : Machines generation@,");
84

    
85
  let prog, machine_code = 
86
    Compiler_stages.stage2 params prog 
87
  in
88

    
89
  Log.report ~level:3 (fun fmt -> fprintf fmt "@ @[<v 2>.. Generated machines:@ %a@]"
90
                          Machine_code_common.pp_machines machine_code);
91

    
92
  if Scopes.Plugin.show_scopes () then
93
    begin
94
      let all_scopes = Scopes.compute_scopes prog !Options.main_node in
95
      (* Printing scopes *)
96
      if !Options.verbose_level >= 1 then
97
	Format.printf "Possible scopes are:@   ";
98
      Format.printf "@[<v>%a@ @]@ @?" Scopes.print_scopes all_scopes;
99
      exit 0
100
	
101
    end;
102
  let machine_code = Plugins.refine_machine_code prog machine_code in
103
  Log.report ~level:1 (fun fmt -> fprintf fmt "@]@ @ ");
104
  
105
  Compiler_stages.stage3 prog machine_code dependencies basename extension;
106
  begin
107
    Log.report ~level:1 (fun fmt -> fprintf fmt "@ .. done !@]@.");
108
    (* We stop the process here *)
109
    exit 0
110
  end
111

    
112
let compile dirname basename extension =
113
  Plugins.init ();
114
  match extension with
115
  | ".lusi"  
116
  | ".lus"   -> compile dirname basename extension
117
  | _        -> assert false
118

    
119
let anonymous filename =
120
  let ok_ext, ext = List.fold_left
121
    (fun (ok, ext) ext' ->
122
      if not ok && Filename.check_suffix filename ext' then
123
	true, ext'
124
      else
125
	ok, ext)
126
    (false, "") extensions in
127
  if ok_ext then begin
128
    Options_management.setup();
129
    let dirname = Filename.dirname filename in
130
    let basename = Filename.chop_suffix (Filename.basename filename) ext in
131
    compile dirname basename ext
132
  end else
133
    raise (Arg.Bad ("Can only compile *.lusi, *.lus or *.ec files"))
134

    
135
let _ =
136
  Global.initialize ();
137
  Corelang.add_internal_funs ();
138
  try
139
    Printexc.record_backtrace true;
140

    
141
    let options = Options_management.lustrec_options @ (Plugins.options ()) in
142
    
143
    Arg.parse options anonymous usage
144
  with
145
  | Parse.Error | Types.Error (_,_) | Clocks.Error (_,_) ->
146
    exit 1
147
  | Error.Error (loc , kind) (*| Task_set.Error _*) ->
148
    Error.pp_error loc (fun fmt -> Error.pp_error_msg fmt kind);
149
    exit (Error.return_code kind)
150
  (* | Causality.Error _  -> exit (Error.return_code Error.AlgebraicLoop) *)
151
  | Sys_error msg ->
152
    (eprintf "Failure: %s@." msg); exit 1
153
  | exc ->
154
    (track_exception (); raise exc)
155

    
156
(* Local Variables: *)
157
(* compile-command:"make -C .." *)
158
(* End: *)
(36-36/66)