Project

General

Profile

Statistics
| Branch: | Tag: | Revision:

lustrec / src / backends / C / c_backend.ml @ 3b2bd83d

History | View | Annotate | Download (4.92 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
  let source_main_file = destname ^ "_main.c" in (* Could be changed *)
31
  let makefile_file = destname ^ ".makefile" in (* Could be changed *)
32
  
33
  let print_header, print_lib_c, print_main_c, print_makefile = funs in
34

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

    
49
  match !Options.main_node with
50
  | "" ->  () (* No main node: we do not generate main nor makefile *)
51
  | main_node -> (
52
    match Machine_code.get_machine_opt main_node machines with
53
    | None -> begin
54
      Global.main_node := main_node;
55
      Format.eprintf "Code generation error: %a@." Corelang.pp_error LustreSpec.Main_not_found;
56
      raise (Corelang.Error (Location.dummy_loc, LustreSpec.Main_not_found))
57
    end
58
    | Some m -> begin
59
      let source_main_out = open_out source_main_file in
60
      let source_main_fmt = formatter_of_out_channel source_main_out in
61
      let makefile_out = open_out makefile_file in
62
      let makefile_fmt = formatter_of_out_channel makefile_out in
63

    
64
      (* Generating Main C file *)
65
      print_main_c source_main_fmt m basename prog machines dependencies;
66
      
67
      (* Generating Makefile *)
68
      print_makefile basename main_node dependencies makefile_fmt;
69

    
70
     close_out source_main_out;
71
     close_out makefile_out
72

    
73
    end
74
  )
75

    
76
let translate_to_c basename prog machines dependencies =
77
  match !Options.spec with
78
  | "no" -> begin
79
    let module HeaderMod = C_backend_header.EmptyMod in
80
    let module SourceMod = C_backend_src.EmptyMod in
81
    let module SourceMainMod = C_backend_main.EmptyMod in
82
    let module MakefileMod = C_backend_makefile.EmptyMod in
83

    
84
    let module Header = C_backend_header.Main (HeaderMod) in
85
    let module Source = C_backend_src.Main (SourceMod) in
86
    let module SourceMain = C_backend_main.Main (SourceMainMod) in
87
    let module Makefile = C_backend_makefile.Main (MakefileMod) in
88

    
89
    let funs = 
90
      Header.print_alloc_header, 
91
      Source.print_lib_c, 
92
      SourceMain.print_main_c, 
93
      Makefile.print_makefile 
94
    in
95
    gen_files funs basename prog machines dependencies 
96

    
97
  end
98
  | "acsl" -> begin
99

    
100
    let module HeaderMod = C_backend_header.EmptyMod in
101
    let module SourceMod = C_backend_src.EmptyMod in
102
    let module SourceMainMod = C_backend_main.EmptyMod in
103
    let module MakefileMod = C_backend_spec.MakefileMod in
104

    
105
    let module Header = C_backend_header.Main (HeaderMod) in
106
    let module Source = C_backend_src.Main (SourceMod) in
107
    let module SourceMain = C_backend_main.Main (SourceMainMod) in
108
    let module Makefile = C_backend_makefile.Main (MakefileMod) in
109

    
110
    let funs = 
111
      Header.print_alloc_header, 
112
      Source.print_lib_c,
113
      SourceMain.print_main_c,
114
      Makefile.print_makefile 
115
    in
116
    gen_files funs basename prog machines dependencies 
117

    
118
  end
119
  | "c" -> begin
120
    assert false (* not implemented yet *)
121
  end
122
  | _ -> assert false
123
(* Local Variables: *)
124
(* compile-command:"make -C ../../.." *)
125
(* End: *)