Project

General

Profile

Statistics
| Branch: | Tag: | Revision:

lustrec / src / backends / C / c_backend.ml @ 04a188ec

History | View | Annotate | Download (7.28 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 C_backend_mauve
14
(********************************************************************************************)
15
(*                         Translation function                                             *)
16
(********************************************************************************************)
17
(* USELESS
18
let makefile_opt print basename dependencies makefile_fmt machines =
19
  (* If a main node is identified, generate a main target for it *)
20
  match !Options.main_node with
21
  | "" ->  ()
22
  | main_node -> (
23
    match Machine_code.get_machine_opt main_node machines with
24
    | None -> Format.eprintf "Unable to find a main node named %s@.@?" main_node; ()
25
    | Some _ -> print basename !Options.main_node dependencies makefile_fmt
26
  )
27
*)
28

    
29
let gen_files funs basename prog machines dependencies =
30
  let destname = !Options.dest_dir ^ "/" ^ basename in
31
  
32
  let print_header, print_lib_c, print_main_c, print_makefile, preprocess (* , print_cmake *) = funs in
33

    
34
  let machines, spec = preprocess machines in
35
  
36
  (* Generating H file *)
37
  let alloc_header_file = destname ^ "_alloc.h" in (* Could be changed *)
38
  let header_out = open_out alloc_header_file in
39
  let header_fmt = formatter_of_out_channel header_out in
40
  print_header header_fmt basename prog machines dependencies spec;
41
  close_out header_out;
42
  
43
  (* Generating Lib C file *)
44
  let source_lib_file = (if !Options.cpp then destname ^ ".cpp" else destname ^ ".c") in (* Could be changed *)
45
  let source_lib_out = open_out source_lib_file in
46
  let source_lib_fmt = formatter_of_out_channel source_lib_out in
47
  print_lib_c source_lib_fmt basename prog machines dependencies;
48
  close_out source_lib_out;
49

    
50
  (match !Options.main_node with
51
  | "" ->  () (* No main node: we do not generate main *)
52
  | main_node -> (
53
    match Machine_code_common.get_machine_opt machines main_node with
54
    | None -> begin
55
      Global.main_node := main_node;
56
      Format.eprintf "Code generation error: %a@." Error.pp_error_msg Error.Main_not_found;
57
      raise (Error.Error (Location.dummy_loc, Error.Main_not_found))
58
    end
59
    | Some m -> begin
60
      let source_main_file = (if !Options.cpp then destname ^ "_main.cpp" else destname ^ "_main.c") in (* Could be changed *)
61
      let source_main_out = open_out source_main_file in
62
      let source_main_fmt = formatter_of_out_channel source_main_out in
63

    
64
      (* Generating Main C file *)
65
      print_main_c source_main_fmt m basename prog machines dependencies;
66

    
67
      close_out source_main_out;
68
    end
69
  ));
70

    
71
  (match !Options.mauve with
72
  | "" ->  ()
73
  | mauve -> (
74
    (* looking for the main node *)
75
    match Machine_code_common.get_machine_opt machines mauve  with
76
    | None -> begin
77
      Global.main_node := mauve;
78
      Format.eprintf "Code generation error: %a@." Error.pp_error_msg Error.Main_not_found;
79
      raise (Error.Error (Location.dummy_loc, Error.Main_not_found))
80
    end
81
    | Some m -> begin
82
      let source_mauve_file = destname ^ "_mauve.hpp" in
83
      let source_mauve_out = open_out source_mauve_file in
84
      let source_mauve_fmt = formatter_of_out_channel source_mauve_out in
85
      (* Header *)
86
      print_mauve_header source_mauve_fmt m basename prog machines dependencies;
87
      (* Shell *)
88
      print_mauve_shell source_mauve_fmt m basename prog machines dependencies;
89
      (* Core *)
90
      print_mauve_core source_mauve_fmt m basename prog machines dependencies;
91
      (* FSM *)
92
      print_mauve_fsm source_mauve_fmt m basename prog machines dependencies;
93

    
94
      close_out source_mauve_out;
95
    end
96
  ));
97

    
98

    
99
  (* Makefiles:
100
     - for the moment two cases
101
     1. Classical Makefile, only when provided with a main node
102
     May contain additional framac eacsl targets
103
     2. Cmake : 2 files
104
         - lustrec-basename.cmake describing all variables
105
         - the main CMakeLists.txt activating the first file
106
     - Later option 1 should be removed
107
  *)
108
  (* Case 1 *)
109
  (match !Options.main_node with
110
  | "" ->  () (* No main node: we do not generate main *)
111
  | main_node -> (
112
    let makefile_file = destname ^ ".makefile" in (* Could be changed *)
113
    let makefile_out = open_out makefile_file in
114
    let makefile_fmt = formatter_of_out_channel makefile_out in
115
    
116
    (* Generating Makefile *)
117
    print_makefile basename main_node dependencies makefile_fmt;
118
    
119
    close_out makefile_out
120
  ))(* ; *)
121
  
122
  (* (\* Case 2 *\) *)
123
  (* let cmake_file = "lustrec-" ^ basename ^ ".cmake" in *)
124
  (* let cmake_file_full_path = !Options.dest_dir ^ "/" ^ cmake_file in *)
125
  (* let cmake_out = open_out cmake_file_full_path in *)
126
  (* let cmake_fmt = formatter_of_out_channel cmake_out in *)
127
  (* (\* Generating Makefile *\) *)
128
  (* print_cmake basename main_node dependencies makefile_fmt; *)
129
    
130
  (*   close_out makefile_out *)
131
  
132

    
133
let translate_to_c basename prog machines dependencies =
134
  match !Options.spec with
135
  | "no" -> begin
136
    let module HeaderMod = C_backend_header.EmptyMod in
137
    let module SourceMod = C_backend_src.EmptyMod in
138
    let module SourceMainMod = C_backend_main.EmptyMod in
139
    let module MakefileMod = C_backend_makefile.EmptyMod in
140

    
141
    let module Header = C_backend_header.Main (HeaderMod) in
142
    let module Source = C_backend_src.Main (SourceMod) in
143
    let module SourceMain = C_backend_main.Main (SourceMainMod) in
144
    let module Makefile = C_backend_makefile.Main (MakefileMod) in
145
    (* let module CMakefile = C_backend_cmake.Main (MakefileMod) in *)
146
    
147
    let funs = 
148
      Header.print_alloc_header, 
149
      Source.print_lib_c, 
150
      SourceMain.print_main_c, 
151
      Makefile.print_makefile,
152
      (fun m -> m, [])
153
      (* CMakefile.print_makefile *)
154
    in
155
    gen_files funs basename prog machines dependencies 
156

    
157
  end
158
  | "acsl" -> begin
159

    
160
    let module HeaderMod = C_backend_header.EmptyMod in
161
    let module SourceMod = C_backend_src.EmptyMod in
162
    let module SourceMainMod = C_backend_main.EmptyMod in
163
    let module MakefileMod = C_backend_spec.MakefileMod in
164

    
165
    let module Header = C_backend_header.Main (HeaderMod) in
166
    let module Source = C_backend_src.Main (SourceMod) in
167
    let module SourceMain = C_backend_main.Main (SourceMainMod) in
168
    let module Makefile = C_backend_makefile.Main (MakefileMod) in
169
    (* let module CMakefile = C_backend_cmake.Main (MakefileMod) in *)
170
    
171
    let funs = 
172
      Header.print_alloc_header, 
173
      Source.print_lib_c,
174
      SourceMain.print_main_c,
175
      Makefile.print_makefile,
176
      C_backend_spec.preprocess_acsl
177
      (* CMakefile.print_makefile  *)
178
    in
179
    gen_files funs basename prog machines dependencies 
180

    
181
  end
182
  | "c" -> begin
183
    assert false (* not implemented yet *)
184
  end
185
  | _ -> assert false
186
(* Local Variables: *)
187
(* compile-command:"make -C ../../.." *)
188
(* End: *)