Project

General

Profile

Statistics
| Branch: | Tag: | Revision:

lustrec / src / backends / C / c_backend.ml @ 52c5ba00

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

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

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

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

    
61
      (* Generating Main C file *)
62
      print_main_c source_main_fmt m basename prog machines dependencies;
63

    
64
      close_out source_main_out;
65
    end
66
  ));
67

    
68

    
69
  (* Makefiles:
70
     - for the moment two cases
71
     1. Classical Makefile, only when provided with a main node
72
     May contain additional framac eacsl targets
73
     2. Cmake : 2 files
74
         - lustrec-basename.cmake describing all variables
75
         - the main CMakeLists.txt activating the first file
76
     - Later option 1 should be removed
77
  *)
78
  (* Case 1 *)
79
  (match !Options.main_node with
80
  | "" ->  () (* No main node: we do not generate main *)
81
  | main_node -> (
82
    let makefile_file = destname ^ ".makefile" in (* Could be changed *)
83
    let makefile_out = open_out makefile_file in
84
    let makefile_fmt = formatter_of_out_channel makefile_out in
85
    
86
    (* Generating Makefile *)
87
    print_makefile basename main_node dependencies makefile_fmt;
88
    
89
    close_out makefile_out
90
  ))(* ; *)
91
  
92
  (* (\* Case 2 *\) *)
93
  (* let cmake_file = "lustrec-" ^ basename ^ ".cmake" in *)
94
  (* let cmake_file_full_path = !Options.dest_dir ^ "/" ^ cmake_file in *)
95
  (* let cmake_out = open_out cmake_file_full_path in *)
96
  (* let cmake_fmt = formatter_of_out_channel cmake_out in *)
97
  (* (\* Generating Makefile *\) *)
98
  (* print_cmake basename main_node dependencies makefile_fmt; *)
99
    
100
  (*   close_out makefile_out *)
101
  
102

    
103
let translate_to_c basename prog machines dependencies =
104
  match !Options.spec with
105
  | "no" -> begin
106
    let module HeaderMod = C_backend_header.EmptyMod in
107
    let module SourceMod = C_backend_src.EmptyMod in
108
    let module SourceMainMod = C_backend_main.EmptyMod in
109
    let module MakefileMod = C_backend_makefile.EmptyMod in
110

    
111
    let module Header = C_backend_header.Main (HeaderMod) in
112
    let module Source = C_backend_src.Main (SourceMod) in
113
    let module SourceMain = C_backend_main.Main (SourceMainMod) in
114
    let module Makefile = C_backend_makefile.Main (MakefileMod) in
115
    (* let module CMakefile = C_backend_cmake.Main (MakefileMod) in *)
116
    
117
    let funs = 
118
      Header.print_alloc_header, 
119
      Source.print_lib_c, 
120
      SourceMain.print_main_c, 
121
      Makefile.print_makefile(* , *)
122
      (* CMakefile.print_makefile *)
123
    in
124
    gen_files funs basename prog machines dependencies 
125

    
126
  end
127
  | "acsl" -> begin
128

    
129
    let module HeaderMod = C_backend_header.EmptyMod in
130
    let module SourceMod = C_backend_src.EmptyMod in
131
    let module SourceMainMod = C_backend_main.EmptyMod in
132
    let module MakefileMod = C_backend_spec.MakefileMod in
133

    
134
    let module Header = C_backend_header.Main (HeaderMod) in
135
    let module Source = C_backend_src.Main (SourceMod) in
136
    let module SourceMain = C_backend_main.Main (SourceMainMod) in
137
    let module Makefile = C_backend_makefile.Main (MakefileMod) in
138
    (* let module CMakefile = C_backend_cmake.Main (MakefileMod) in *)
139
    
140
    let funs = 
141
      Header.print_alloc_header, 
142
      Source.print_lib_c,
143
      SourceMain.print_main_c,
144
      Makefile.print_makefile(* , *)
145
      (* CMakefile.print_makefile  *)
146
    in
147
    gen_files funs basename prog machines dependencies 
148

    
149
  end
150
  | "c" -> begin
151
    assert false (* not implemented yet *)
152
  end
153
  | _ -> assert false
154
(* Local Variables: *)
155
(* compile-command:"make -C ../../.." *)
156
(* End: *)