Project

General

Profile

Download (6.59 KB) Statistics
| Branch: | Tag: | Revision:
1 a2d97a3e ploc
(********************************************************************)
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 22fe1c93 ploc
12 90cc3b8e Lélio Brun
open Utils.Format
13 d1b9423d David Doose
open C_backend_mauve
14 ca7ff3f7 Lélio Brun
15 1df55e58 Lélio Brun
(******************************************************************************)
16
(*                        Translation function                                *)
17
(******************************************************************************)
18 ca7ff3f7 Lélio Brun
(* 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 1df55e58 Lélio Brun
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 ca7ff3f7 Lélio Brun
    ( print_alloc_header,
42
      print_lib_c,
43
      print_main_c,
44
      print_makefile,
45
      preprocess
46
    (* , print_cmake *) ) basename prog machines dependencies =
47 dc893173 ploc
  let destname = !Options.dest_dir ^ "/" ^ basename in
48 ca7ff3f7 Lélio Brun
49 71999483 ploc
  let machines, spec = preprocess machines in
50 efcc8d7f Lélio Brun
51
  (* Generating H alloc file *)
52 ca7ff3f7 Lélio Brun
  let alloc_header_file = destname ^ "_alloc.h" in
53
  (* Could be changed *)
54 1df55e58 Lélio Brun
  with_out_file alloc_header_file (fun header_fmt ->
55 efcc8d7f Lélio Brun
      print_alloc_header header_fmt basename prog machines dependencies spec);
56 1df55e58 Lélio Brun
57 d4107cf2 ploc
  (* Generating Lib C file *)
58 1df55e58 Lélio Brun
  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 d1b9423d David Doose
      let source_mauve_file = destname ^ "_mauve.hpp" in
72 1df55e58 Lélio Brun
      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 ca7ff3f7 Lélio Brun
  (* 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 bb1b5e04 Ploc
  (* Case 1 *)
89 1df55e58 Lélio Brun
  if main_node <> "" then
90 ca7ff3f7 Lélio Brun
    let makefile_file = destname ^ ".makefile" in
91
    (* Could be changed *)
92 1df55e58 Lélio Brun
    with_out_file makefile_file (fun makefile_fmt ->
93
        print_makefile basename main_node dependencies makefile_fmt)
94
95 ca7ff3f7 Lélio Brun
(* (\* 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 efcc8d7f Lélio Brun
let print_c_header basename =
106 ca7ff3f7 Lélio Brun
  let header_m =
107
    match !Options.spec with
108 efcc8d7f Lélio Brun
    | "no" ->
109 ca7ff3f7 Lélio Brun
      C_backend_header.((module EmptyMod : MODIFIERS_HDR))
110 efcc8d7f Lélio Brun
    | "acsl" ->
111 ca7ff3f7 Lélio Brun
      C_backend_header.((module C_backend_spec.HdrMod : MODIFIERS_HDR))
112
    | "c" ->
113
      assert false (* not implemented yet *)
114
    | _ ->
115
      assert false
116 efcc8d7f Lélio Brun
  in
117 ca7ff3f7 Lélio Brun
  let module Header = C_backend_header.Main ((val header_m)) in
118 efcc8d7f Lélio Brun
  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 1df55e58 Lélio Brun
  let header_m, source_m, source_main_m, makefile_m, preprocess =
128
    match !Options.spec with
129
    | "no" ->
130 ca7ff3f7 Lélio Brun
      ( 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 1df55e58 Lélio Brun
    | "acsl" ->
136 efcc8d7f Lélio Brun
      let open C_backend_spec in
137 ca7ff3f7 Lélio Brun
      ( 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 1df55e58 Lélio Brun
  in
147 ca7ff3f7 Lélio Brun
  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 1df55e58 Lélio Brun
  (* let module CMakefile = C_backend_cmake.Main (MakefileMod) in *)
152
  let funs =
153 ca7ff3f7 Lélio Brun
    ( Header.print_alloc_header,
154
      Source.print_lib_c,
155
      SourceMain.print_main_c,
156
      Makefile.print_makefile,
157
      preprocess )
158 1df55e58 Lélio Brun
    (* CMakefile.print_makefile *)
159
  in
160 efcc8d7f Lélio Brun
  if generate_c_header then print_c_header basename;
161 1df55e58 Lélio Brun
  gen_files funs basename prog machines dependencies
162
163 22fe1c93 ploc
(* Local Variables: *)
164 cd670fe1 ploc
(* compile-command:"make -C ../../.." *)
165 22fe1c93 ploc
(* End: *)