Project

General

Profile

Download (4.93 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 ".. Normalized program:@ %a@ "Printers.pp_prog prog);
81
  Log.report ~level:1 (fun fmt -> fprintf fmt "@]@,");
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 ".. Generated machines:@ %a@ " Machine_code_common.pp_machines machine_code);
90

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

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

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

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

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

    
152
(* Local Variables: *)
153
(* compile-command:"make -C .." *)
154
(* End: *)
(35-35/63)