Project

General

Profile

Download (6.59 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 Utils.Format
13
open C_backend_mauve
14

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

    
25
let c_or_cpp f = if !Options.cpp then f ^ ".cpp" else f ^ ".c"
26
(* Could be changed *)
27

    
28
let with_main_node machines node f =
29
  if node <> "" then
30
    (* looking for the main node *)
31
    match Machine_code_common.get_machine_opt machines node with
32
    | None ->
33
      let open Error in
34
      Global.main_node := node;
35
      Format.eprintf "Code generation error: %a@." pp_error_msg Main_not_found;
36
      raise (Error (Location.dummy_loc, Main_not_found))
37
    | Some m ->
38
      f m
39

    
40
let gen_files
41
    ( print_alloc_header,
42
      print_lib_c,
43
      print_main_c,
44
      print_makefile,
45
      preprocess
46
    (* , print_cmake *) ) basename prog machines dependencies =
47
  let destname = !Options.dest_dir ^ "/" ^ basename in
48

    
49
  let machines, spec = preprocess machines in
50

    
51
  (* Generating H alloc file *)
52
  let alloc_header_file = destname ^ "_alloc.h" in
53
  (* Could be changed *)
54
  with_out_file alloc_header_file (fun header_fmt ->
55
      print_alloc_header header_fmt basename prog machines dependencies spec);
56

    
57
  (* Generating Lib C file *)
58
  let source_lib_file = c_or_cpp destname in
59
  with_out_file source_lib_file (fun source_lib_fmt ->
60
      print_lib_c source_lib_fmt basename prog machines dependencies);
61

    
62
  (* Generating Main C file *)
63
  let main_node = !Options.main_node in
64
  with_main_node machines main_node (fun m ->
65
      let source_main_file = c_or_cpp (destname ^ "_main") in
66
      with_out_file source_main_file (fun source_main_fmt ->
67
          print_main_c source_main_fmt m basename prog machines dependencies));
68

    
69
  (* Generating Mauve files *)
70
  with_main_node machines !Options.mauve (fun m ->
71
      let source_mauve_file = destname ^ "_mauve.hpp" in
72
      with_out_file source_mauve_file (fun source_mauve_fmt ->
73
          (* Header *)
74
          print_mauve_header source_mauve_fmt basename;
75
          (* Shell *)
76
          print_mauve_shell source_mauve_fmt m;
77
          (* Core *)
78
          print_mauve_core source_mauve_fmt m;
79
          (* FSM *)
80
          print_mauve_fsm source_mauve_fmt m));
81

    
82
  (* Generating Makefile *)
83
  (* Makefiles: - for the moment two cases 1. Classical Makefile, only when
84
     provided with a main node May contain additional framac eacsl targets 2.
85
     Cmake : 2 files - lustrec-basename.cmake describing all variables - the
86
     main CMakeLists.txt activating the first file - Later option 1 should be
87
     removed *)
88
  (* Case 1 *)
89
  if main_node <> "" then
90
    let makefile_file = destname ^ ".makefile" in
91
    (* Could be changed *)
92
    with_out_file makefile_file (fun makefile_fmt ->
93
        print_makefile basename main_node dependencies makefile_fmt)
94

    
95
(* (\* Case 2 *\) *)
96
(* let cmake_file = "lustrec-" ^ basename ^ ".cmake" in *)
97
(* let cmake_file_full_path = !Options.dest_dir ^ "/" ^ cmake_file in *)
98
(* let cmake_out = open_out cmake_file_full_path in *)
99
(* let cmake_fmt = formatter_of_out_channel cmake_out in *)
100
(* (\* Generating Makefile *\) *)
101
(* print_cmake basename main_node dependencies makefile_fmt; *)
102

    
103
(* close_out makefile_out *)
104

    
105
let print_c_header basename =
106
  let header_m =
107
    match !Options.spec with
108
    | "no" ->
109
      C_backend_header.((module EmptyMod : MODIFIERS_HDR))
110
    | "acsl" ->
111
      C_backend_header.((module C_backend_spec.HdrMod : MODIFIERS_HDR))
112
    | "c" ->
113
      assert false (* not implemented yet *)
114
    | _ ->
115
      assert false
116
  in
117
  let module Header = C_backend_header.Main ((val header_m)) in
118
  let destname = !Options.dest_dir ^ "/" ^ basename in
119
  (* Generating H file *)
120
  let lusic = Lusic.read_lusic destname ".lusic" in
121
  let header_file = destname ^ ".h" in
122
  with_out_file header_file (fun header_fmt ->
123
      assert (not lusic.obsolete);
124
      Header.print_header_from_header header_fmt basename lusic.contents)
125

    
126
let translate_to_c generate_c_header basename prog machines dependencies =
127
  let header_m, source_m, source_main_m, makefile_m, preprocess =
128
    match !Options.spec with
129
    | "no" ->
130
      ( C_backend_header.((module EmptyMod : MODIFIERS_HDR)),
131
        C_backend_src.((module EmptyMod : MODIFIERS_SRC)),
132
        C_backend_main.((module EmptyMod : MODIFIERS_MAINSRC)),
133
        C_backend_makefile.((module EmptyMod : MODIFIERS_MKF)),
134
        fun m -> m, [] )
135
    | "acsl" ->
136
      let open C_backend_spec in
137
      ( C_backend_header.((module HdrMod : MODIFIERS_HDR)),
138
        C_backend_src.((module SrcMod : MODIFIERS_SRC)),
139
        C_backend_main.((module EmptyMod : MODIFIERS_MAINSRC)),
140
        C_backend_makefile.((module MakefileMod : MODIFIERS_MKF)),
141
        preprocess_acsl )
142
    | "c" ->
143
      assert false (* not implemented yet *)
144
    | _ ->
145
      assert false
146
  in
147
  let module Header = C_backend_header.Main ((val header_m)) in
148
  let module Source = C_backend_src.Main ((val source_m)) in
149
  let module SourceMain = C_backend_main.Main ((val source_main_m)) in
150
  let module Makefile = C_backend_makefile.Main ((val makefile_m)) in
151
  (* let module CMakefile = C_backend_cmake.Main (MakefileMod) in *)
152
  let funs =
153
    ( Header.print_alloc_header,
154
      Source.print_lib_c,
155
      SourceMain.print_main_c,
156
      Makefile.print_makefile,
157
      preprocess )
158
    (* CMakefile.print_makefile *)
159
  in
160
  if generate_c_header then print_c_header basename;
161
  gen_files funs basename prog machines dependencies
162

    
163
(* Local Variables: *)
164
(* compile-command:"make -C ../../.." *)
165
(* End: *)
(1-1/9)