Project

General

Profile

Download (4.88 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
open Utils
15

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

    
18
let extensions = [ ".ec"; ".lus"; ".lusi" ]
19

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

    
31
(* compile a .lus source file *)
32
let compile dirname basename extension =
33
  let source_name = dirname ^ "/" ^ basename ^ extension in
34

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

    
37
  (* Parsing source *)
38
  let prog = parse source_name extension in
39

    
40
  let prog =
41
    if
42
      !Options.mpfr && extension = ".lus"
43
      (* trying to avoid the injection of the module for lusi files *)
44
    then Lustrec_mpfr.mpfr_module :: prog
45
    else prog
46
  in
47
  let params = Backends.get_normalization_params () in
48

    
49
  let prog, dependencies =
50
    Log.report ~level:1 (fun fmt ->
51
        fprintf fmt "@[<v 2>.. Phase 1: Normalisation@,");
52
    try Compiler_stages.stage1 params prog dirname basename extension
53
    with Compiler_stages.StopPhase1 prog ->
54
      if !Options.lusi then (
55
        let lusi_ext = extension ^ "i" in
56
        Log.report ~level:1 (fun fmt ->
57
            fprintf fmt ".. generating interface file %s@ " (basename ^ lusi_ext));
58
        print_lusi prog dirname basename lusi_ext;
59
        Log.report ~level:1 (fun fmt -> fprintf fmt ".. done !@ @]@.");
60
        exit 0)
61
      else if !Options.print_nodes then (
62
        Format.printf "%a@.@?" Printers.pp_node_list prog;
63
        exit 0)
64
      else assert false
65
  in
66
  Log.report ~level:3 (fun fmt ->
67
      fprintf fmt "@ @[<v 2>.. Normalized program:@ %a@]" Printers.pp_prog prog);
68

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

    
72
  let prog, machine_code = Compiler_stages.stage2 params prog in
73

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

    
78
  if Scopes.Plugin.show_scopes () then (
79
    let all_scopes = Scopes.compute_scopes prog !Options.main_node in
80
    (* Printing scopes *)
81
    if !Options.verbose_level >= 1 then Format.printf "Possible scopes are:@   ";
82
    Format.printf "@[<v>%a@ @]@ @?" Scopes.print_scopes all_scopes;
83
    exit 0);
84
  let machine_code = Plugins.refine_machine_code prog machine_code in
85
  Log.report ~level:1 (fun fmt -> fprintf fmt "@]@ @ ");
86

    
87
  Compiler_stages.stage3 prog machine_code dependencies basename extension;
88
  Log.report ~level:1 (fun fmt -> fprintf fmt "@ .. done !@]@.");
89
  (* We stop the process here *)
90
  exit 0
91

    
92
let compile dirname basename extension =
93
  Plugins.init ();
94
  match extension with
95
  | ".lusi" | ".lus" ->
96
    compile dirname basename extension
97
  | _ ->
98
    assert false
99

    
100
let anonymous filename =
101
  let ok_ext, ext =
102
    List.fold_left
103
      (fun (ok, ext) ext' ->
104
        if (not ok) && Filename.check_suffix filename ext' then true, ext'
105
        else ok, ext)
106
      (false, "") extensions
107
  in
108
  if ok_ext then (
109
    Options_management.setup ();
110
    let dirname = Filename.dirname filename in
111
    let basename = Filename.chop_suffix (Filename.basename filename) ext in
112
    compile dirname basename ext)
113
  else raise (Arg.Bad "Can only compile *.lusi, *.lus or *.ec files")
114

    
115
let _ =
116
  Global.initialize ();
117
  Corelang.add_internal_funs ();
118
  try
119
    Printexc.record_backtrace true;
120

    
121
    let options = Options_management.lustrec_options @ Plugins.options () in
122

    
123
    Arg.parse options anonymous usage
124
  with
125
  | Parse.Error | Types.Error (_, _) | Clocks.Error (_, _) ->
126
    exit 1
127
  | Error.Error (loc, kind) (*| Task_set.Error _*) ->
128
    Error.pp_error loc (fun fmt -> Error.pp_error_msg fmt kind);
129
    exit (Error.return_code kind)
130
  (* | Causality.Error _ -> exit (Error.return_code Error.AlgebraicLoop) *)
131
  | Sys_error msg ->
132
    eprintf "Failure: %s@." msg;
133
    exit 1
134
  | exc ->
135
    track_exception ();
136
    raise exc
137

    
138
(* Local Variables: *)
139
(* compile-command:"make -C .." *)
140
(* End: *)
(36-36/66)