Project

General

Profile

Download (4.96 KB) Statistics
| Branch: | Tag: | Revision:
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
38 04a63d25 xthirioux
39 6efbcb73 xthirioux
40 04a63d25 xthirioux
41
42
(* compile a .lus source file *)
43 217837e2 ploc
let rec compile dirname basename extension =
44 04a63d25 xthirioux
  let source_name = dirname ^ "/" ^ basename ^ extension in
45
46 521e2a6b ploc
  Log.report ~level:1 (fun fmt -> fprintf fmt "@[<v 0>");
47 04a63d25 xthirioux
48
  (* Parsing source *)
49 217837e2 ploc
  let prog = parse source_name extension in
50 a0c92fa8 ploc
  
51 04a63d25 xthirioux
  let prog =
52 217837e2 ploc
    if !Options.mpfr &&
53
         extension = ".lus" (* trying to avoid the injection of the module for lusi files *) 
54
    then
55 f0195e96 ploc
      Lustrec_mpfr.mpfr_module::prog
56 04a63d25 xthirioux
    else
57
      prog
58
  in
59 ad4774b0 ploc
  let params = Backends.get_normalization_params () in
60
61 04a63d25 xthirioux
  let prog, dependencies = 
62 521e2a6b ploc
    Log.report ~level:1 (fun fmt -> fprintf fmt "@[<v 2>.. Phase 1 : Normalisation@,");
63 04a63d25 xthirioux
    try 
64 217837e2 ploc
      Compiler_stages.stage1 params prog dirname basename extension
65 04257b1e ploc
    with Compiler_stages.StopPhase1 prog -> (
66 04a63d25 xthirioux
      if !Options.lusi then
67
	begin
68
	  let lusi_ext = extension ^ "i" in
69 521e2a6b ploc
	  Log.report ~level:1 (fun fmt -> fprintf fmt ".. generating interface file %s@ " (basename ^ lusi_ext));
70 04a63d25 xthirioux
	  print_lusi prog dirname basename lusi_ext;
71
	  Log.report ~level:1 (fun fmt -> fprintf fmt ".. done !@ @]@.");
72
	  exit 0
73
	end
74 a0c92fa8 ploc
      else if !Options.print_nodes then (
75
        Format.printf "%a@.@?" Printers.pp_node_list prog;
76
        exit 0
77
      )
78 04a63d25 xthirioux
      else
79
        assert false
80
    )
81 6efbcb73 xthirioux
  in
82 521e2a6b ploc
  Log.report ~level:3 (fun fmt -> fprintf fmt ".. Normalized program:@ %a@ "Printers.pp_prog prog);
83 8e6cab20 ploc
  Log.report ~level:1 (fun fmt -> fprintf fmt "@]@,");
84 521e2a6b ploc
85
  Log.report ~level:1 (fun fmt -> fprintf fmt "@[<v 2>.. Phase 2 : Machines generation@,");
86 04a63d25 xthirioux
87 e8f55c25 ploc
  let prog, machine_code = 
88
    Compiler_stages.stage2 params prog 
89 04a63d25 xthirioux
  in
90 521e2a6b ploc
91 2863281f ploc
  Log.report ~level:3 (fun fmt -> fprintf fmt ".. Generated machines:@ %a@ " Machine_code_common.pp_machines machine_code);
92 521e2a6b ploc
93 04a63d25 xthirioux
  if Scopes.Plugin.show_scopes () then
94
    begin
95
      let all_scopes = Scopes.compute_scopes prog !Options.main_node in
96
      (* Printing scopes *)
97
      if !Options.verbose_level >= 1 then
98
	Format.printf "Possible scopes are:@   ";
99 521e2a6b ploc
      Format.printf "@[<v>%a@ @]@ @?" Scopes.print_scopes all_scopes;
100 04a63d25 xthirioux
      exit 0
101
	
102
    end;
103 ca88e660 Ploc
  let machine_code = Plugins.refine_machine_code prog machine_code in
104 bc9fd714 ploc
  Log.report ~level:1 (fun fmt -> fprintf fmt "@]@ @ ");
105 04a63d25 xthirioux
  
106 217837e2 ploc
  Compiler_stages.stage3 prog machine_code dependencies basename extension;
107 f4acee4c xthirioux
  begin
108 ca88e660 Ploc
    Log.report ~level:1 (fun fmt -> fprintf fmt ".. done !@ @]@.");
109 04a63d25 xthirioux
    (* We stop the process here *)
110 f4acee4c xthirioux
    exit 0
111
  end
112 6efbcb73 xthirioux
113
let compile dirname basename extension =
114 61df3cb9 ploc
  Plugins.init ();
115 6efbcb73 xthirioux
  match extension with
116 217837e2 ploc
  | ".lusi"  
117
  | ".lus"   -> compile dirname basename extension
118 6efbcb73 xthirioux
  | _        -> assert false
119
120
let anonymous filename =
121
  let ok_ext, ext = List.fold_left
122
    (fun (ok, ext) ext' ->
123
      if not ok && Filename.check_suffix filename ext' then
124
	true, ext'
125
      else
126
	ok, ext)
127
    (false, "") extensions in
128
  if ok_ext then
129
    let dirname = Filename.dirname filename in
130
    let basename = Filename.chop_suffix (Filename.basename filename) ext in
131
    compile dirname basename ext
132
  else
133
    raise (Arg.Bad ("Can only compile *.lusi, *.lus or *.ec files"))
134
135
let _ =
136 04a63d25 xthirioux
  Global.initialize ();
137 6efbcb73 xthirioux
  Corelang.add_internal_funs ();
138
  try
139
    Printexc.record_backtrace true;
140 04a63d25 xthirioux
141 1bff14ac ploc
    let options = Options_management.lustrec_options @ (Plugins.options ()) in
142 04a63d25 xthirioux
    
143
    Arg.parse options anonymous usage
144 6efbcb73 xthirioux
  with
145 04a63d25 xthirioux
  | Parse.Error _
146 e7cc5186 ploc
  | Types.Error (_,_) | Clocks.Error (_,_) -> exit 1
147 04a188ec ploc
  | Error.Error (loc , kind) (*| Task_set.Error _*) ->
148 94a9e2c3 ploc
     Error.pp_error loc (fun fmt -> Error.pp_error_msg fmt kind);
149
     exit (Error.return_code kind)
150 e7cc5186 ploc
  (* | Causality.Error _  -> exit (Error.return_code Error.AlgebraicLoop) *)
151 1ca48e48 ploc
  | Sys_error msg -> (eprintf "Failure: %s@." msg); exit 1
152 e7cc5186 ploc
  | exc -> (track_exception (); raise exc) 
153 6efbcb73 xthirioux
154
(* Local Variables: *)
155
(* compile-command:"make -C .." *)
156
(* End: *)