Project

General

Profile

Download (6.39 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 Main_not_found;
36
      raise (Error (Location.dummy, 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
    (* , print_cmake *) ) basename prog machines dependencies =
46
  let destname = !Options.dest_dir ^ "/" ^ basename in
47

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

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

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

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

    
79
  (* Generating Makefile *)
80
  (* Makefiles: - for the moment two cases 1. Classical Makefile, only when
81
     provided with a main node May contain additional framac eacsl targets 2.
82
     Cmake : 2 files - lustrec-basename.cmake describing all variables - the
83
     main CMakeLists.txt activating the first file - Later option 1 should be
84
     removed *)
85
  (* Case 1 *)
86
  if main_node <> "" then
87
    let makefile_file = destname ^ ".makefile" in
88
    (* Could be changed *)
89
    with_out_file makefile_file (fun makefile_fmt ->
90
        print_makefile basename main_node dependencies makefile_fmt)
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
let print_c_header basename =
103
  let open Options in
104
  let header_m =
105
    match !spec with
106
    | SpecNo ->
107
      C_backend_header.(module EmptyMod : MODIFIERS_HDR)
108
    | SpecACSL ->
109
      C_backend_header.(module C_backend_spec.HdrMod : MODIFIERS_HDR)
110
    | SpecC ->
111
      assert false (* not implemented yet *)
112
  in
113
  let module Header = C_backend_header.Main (val header_m) in
114
  let destname = !dest_dir ^ "/" ^ basename in
115
  (* Generating H file *)
116
  let lusic = Lusic.read_lusic destname ".lusic" in
117
  let header_file = destname ^ ".h" in
118
  with_out_file header_file (fun header_fmt ->
119
      assert (not lusic.obsolete);
120
      Header.print_header_from_header header_fmt basename lusic.contents)
121

    
122
let translate_to_c generate_c_header basename prog machines dependencies =
123
  let header_m, source_m, source_main_m, makefile_m =
124
    let open Options in
125
    match !spec with
126
    | SpecNo ->
127
      ( C_backend_header.((module EmptyMod : MODIFIERS_HDR)),
128
        C_backend_src.((module EmptyMod : MODIFIERS_SRC)),
129
        C_backend_main.((module EmptyMod : MODIFIERS_MAINSRC)),
130
        C_backend_makefile.((module EmptyMod : MODIFIERS_MKF)) )
131
    | SpecACSL ->
132
      let open C_backend_spec in
133
      ( C_backend_header.((module HdrMod : MODIFIERS_HDR)),
134
        C_backend_src.((module SrcMod : MODIFIERS_SRC)),
135
        C_backend_main.((module EmptyMod : MODIFIERS_MAINSRC)),
136
        C_backend_makefile.((module MakefileMod : MODIFIERS_MKF)) )
137
    | SpecC ->
138
      assert false (* not implemented yet *)
139
  in
140
  let module Header = C_backend_header.Main ((val header_m)) in
141
  let module Source = C_backend_src.Main ((val source_m)) in
142
  let module SourceMain = C_backend_main.Main ((val source_main_m)) in
143
  let module Makefile = C_backend_makefile.Main ((val makefile_m)) in
144
  (* let module CMakefile = C_backend_cmake.Main (MakefileMod) in *)
145
  let funs =
146
    ( Header.print_alloc_header,
147
      Source.print_lib_c,
148
      SourceMain.print_main_c,
149
      Makefile.print_makefile )
150
    (* CMakefile.print_makefile *)
151
  in
152
  if generate_c_header then print_c_header basename;
153
  gen_files funs basename prog machines dependencies
154

    
155
(* Local Variables: *)
156
(* compile-command:"make -C ../../.." *)
157
(* End: *)
(1-1/18)