Project

General

Profile

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

    
123
let translate_to_c generate_c_header basename prog machines dependencies =
124
  let header_m, source_m, source_main_m, makefile_m =
125
    match !Options.spec with
126
    | "no" ->
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
    | "acsl" ->
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
    | "c" ->
138
      assert false (* not implemented yet *)
139
    | _ ->
140
      assert false
141
  in
142
  let module Header = C_backend_header.Main ((val header_m)) in
143
  let module Source = C_backend_src.Main ((val source_m)) in
144
  let module SourceMain = C_backend_main.Main ((val source_main_m)) in
145
  let module Makefile = C_backend_makefile.Main ((val makefile_m)) in
146
  (* let module CMakefile = C_backend_cmake.Main (MakefileMod) in *)
147
  let funs =
148
    ( Header.print_alloc_header,
149
      Source.print_lib_c,
150
      SourceMain.print_main_c,
151
      Makefile.print_makefile )
152
    (* CMakefile.print_makefile *)
153
  in
154
  if generate_c_header then print_c_header basename;
155
  gen_files funs basename prog machines dependencies
156

    
157
(* Local Variables: *)
158
(* compile-command:"make -C ../../.." *)
159
(* End: *)
(1-1/9)