Project

General

Profile

Statistics
| Branch: | Tag: | Revision:

lustrec / src / main_lustre_compiler.ml @ e8250987

History | View | Annotate | Download (4.74 KB)

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 Log
14
open Compiler_common
15

    
16
open Utils
17
open Lustre_types
18
 
19

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

    
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
    Typing.uneval_prog_generics header;
32
    Clock_calculus.uneval_prog_generics header;
33
    Printers.pp_lusi_header h_fmt basename header;
34
    close_out h_out
35
  end
36

    
37

    
38

    
39

    
40

    
41

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

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

    
48
  (* Parsing source *)
49
  let prog = parse source_name extension in
50

    
51
  let prog =
52
    if !Options.mpfr &&
53
         extension = ".lus" (* trying to avoid the injection of the module for lusi files *) 
54
    then
55
      Mpfr.mpfr_module::prog
56
    else
57
      prog
58
  in
59
  let params = Backends.get_normalization_params () in
60

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

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

    
83
  let machine_code = 
84
    Compiler_stages.stage2 prog 
85
  in
86

    
87
  Log.report ~level:3 (fun fmt -> fprintf fmt ".. Generated machines:@ %a@ " Machine_code_common.pp_machines machine_code);
88

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

    
109
let compile dirname basename extension =
110
  match extension with
111
  | ".lusi"  
112
  | ".lus"   -> compile dirname basename extension
113
  | _        -> assert false
114

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

    
130
let _ =
131
  Global.initialize ();
132
  Corelang.add_internal_funs ();
133
  try
134
    Printexc.record_backtrace true;
135

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

    
147
(* Local Variables: *)
148
(* compile-command:"make -C .." *)
149
(* End: *)