Project

General

Profile

Statistics
| Branch: | Tag: | Revision:

lustrec / src / backends / C / c_backend_main.ml @ f76eae4f

History | View | Annotate | Download (8.16 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 LustreSpec
13
open Corelang
14
open Machine_code
15
open Format
16
open C_backend_common
17
open Utils
18

    
19
module type MODIFIERS_MAINSRC =
20
sig
21
end
22

    
23
module EmptyMod =
24
struct
25
end
26

    
27
module Main = functor (Mod: MODIFIERS_MAINSRC) -> 
28
struct
29

    
30
(********************************************************************************************)
31
(*                         Main related functions                                           *)
32
(********************************************************************************************)
33

    
34
let print_get_inputs fmt m =
35
  let pi fmt (id, v', v) =
36
  match (Types.unclock_type v.var_type).Types.tdesc with
37
    | Types.Tint -> fprintf fmt "%s = _get_int(f_in%i, \"%s\")" v.var_id id v'.var_id
38
    | Types.Tbool -> fprintf fmt "%s = _get_bool(f_in%i, \"%s\")" v.var_id id v'.var_id
39
    | Types.Treal when !Options.mpfr -> fprintf fmt "mpfr_set_d(%s, _get_double(f_in%i, \"%s\"), %i)" v.var_id id v'.var_id (Mpfr.mpfr_prec ())
40
    | Types.Treal -> fprintf fmt "%s = _get_double(f_in%i, \"%s\")" v.var_id id v'.var_id
41
    | _ ->
42
      begin
43
	Global.main_node := !Options.main_node;
44
	Format.eprintf "Code generation error: %a%a@."
45
	  Error.pp_error_msg Error.Main_wrong_kind
46
	  Location.pp_loc v'.var_loc;
47
	raise (Error (v'.var_loc, Error.Main_wrong_kind))
48
      end
49
  in
50
  List.iteri2 (fun idx v' v ->
51
    fprintf fmt "@ %a;" pi ((idx+1), v', v);
52
  ) m.mname.node_inputs m.mstep.step_inputs
53

    
54
let print_put_outputs fmt m = 
55
  let po fmt (id, o', o) =
56
    let suff = string_of_int id in
57
    print_put_var fmt suff o'.var_id o.var_type o.var_id
58
  in
59
  Utils.List.iteri2 (fun idx v' v -> fprintf fmt "@ %a;" po ((idx+1), v', v)) m.mname.node_outputs m.mstep.step_outputs
60

    
61
let print_main_inout_declaration basename fmt m =
62
  let mname = m.mname.node_id in
63
  (* TODO: find a proper way to shorthen long names. This causes segfault in the binary when trying to fprintf in them *)
64
  let mname = if String.length mname > 50 then string_of_int (Hashtbl.hash mname) else mname in
65
  fprintf fmt "/* Declaration of inputs/outputs variables */@ ";
66
  List.iteri 
67
    (fun idx v ->
68
      fprintf fmt "%a;@ " (pp_c_type v.var_id) v.var_type;
69
      fprintf fmt "FILE *f_in%i;@ " (idx+1); (* we start from 1: in1, in2, ... *)
70
      fprintf fmt "f_in%i = fopen(\"%s_%s_simu.in%i\", \"w\");@ " (idx+1) basename mname (idx+1);
71
    ) m.mstep.step_inputs;
72
  List.iteri 
73
    (fun idx v ->
74
      fprintf fmt "%a;@ " (pp_c_type v.var_id) v.var_type;
75
      fprintf fmt "FILE *f_out%i;@ " (idx+1); (* we start from 1: in1, in2, ... *)
76
      fprintf fmt "f_out%i = fopen(\"%s_%s_simu.out%i\", \"w\");@ " (idx+1) basename mname (idx+1);
77
    ) m.mstep.step_outputs
78

    
79

    
80
  
81
let print_main_memory_allocation mname main_mem fmt m =
82
  if not (fst (get_stateless_status m)) then
83
  begin  
84
    fprintf fmt "@ /* Main memory allocation */@ ";
85
    if (!Options.static_mem && !Options.main_node <> "")
86
    then (fprintf fmt "%a(static,main_mem);@ " pp_machine_static_alloc_name mname)
87
    else (fprintf fmt "%a *main_mem = %a();@ " pp_machine_memtype_name mname pp_machine_alloc_name mname);
88
    fprintf fmt "@ /* Initialize the main memory */@ ";
89
    fprintf fmt "%a(%s);@ " pp_machine_reset_name mname main_mem;
90
  end
91

    
92
let print_global_initialize fmt basename =
93
  let mNAME = file_to_module_name basename in
94
  fprintf fmt "@ /* Initialize global constants */@ %a();@ "
95
    pp_global_init_name mNAME
96

    
97
let print_global_clear fmt basename =
98
  let mNAME = file_to_module_name basename in
99
  fprintf fmt "@ /* Clear global constants */@ %a();@ "
100
    pp_global_clear_name mNAME
101

    
102
let print_main_initialize mname main_mem fmt m =
103
  if not (fst (get_stateless_status m))
104
  then
105
    fprintf fmt "@ /* Initialize inputs, outputs and memories */@ %a%t%a%t%a(%s);@ "
106
      (Utils.fprintf_list ~sep:"@ " (pp_initialize m main_mem (pp_c_var_read m))) m.mstep.step_inputs
107
      (Utils.pp_newline_if_non_empty m.mstep.step_inputs)
108
      (Utils.fprintf_list ~sep:"@ " (pp_initialize m main_mem (pp_c_var_read m))) m.mstep.step_outputs
109
      (Utils.pp_newline_if_non_empty m.mstep.step_inputs)
110
      pp_machine_init_name mname
111
      main_mem
112
  else
113
    fprintf fmt "@ /* Initialize inputs and outputs */@ %a%t%a@ "
114
      (Utils.fprintf_list ~sep:"@ " (pp_initialize m main_mem (pp_c_var_read m))) m.mstep.step_inputs
115
      (Utils.pp_newline_if_non_empty m.mstep.step_inputs)
116
      (Utils.fprintf_list ~sep:"@ " (pp_initialize m main_mem (pp_c_var_read m))) m.mstep.step_outputs
117

    
118
let print_main_clear mname main_mem fmt m =
119
  if not (fst (get_stateless_status m))
120
  then
121
    fprintf fmt "@ /* Clear inputs, outputs and memories */@ %a%t%a%t%a(%s);@ "
122
      (Utils.fprintf_list ~sep:"@ " (pp_clear m main_mem (pp_c_var_read m))) m.mstep.step_inputs
123
      (Utils.pp_newline_if_non_empty m.mstep.step_inputs)
124
      (Utils.fprintf_list ~sep:"@ " (pp_clear m main_mem (pp_c_var_read m))) m.mstep.step_outputs
125
      (Utils.pp_newline_if_non_empty m.mstep.step_inputs)
126
      pp_machine_clear_name mname
127
      main_mem
128
  else
129
    fprintf fmt "@ /* Clear inputs and outputs */@ %a%t%a@ "
130
      (Utils.fprintf_list ~sep:"@ " (pp_clear m main_mem (pp_c_var_read m))) m.mstep.step_inputs
131
      (Utils.pp_newline_if_non_empty m.mstep.step_inputs)
132
      (Utils.fprintf_list ~sep:"@ " (pp_clear m main_mem (pp_c_var_read m))) m.mstep.step_outputs
133

    
134
let print_main_loop mname main_mem fmt m =
135
  let input_values =
136
    List.map (fun v -> mk_val (LocalVar v) v.var_type)
137
      m.mstep.step_inputs in
138
  begin
139
    fprintf fmt "@ ISATTY = isatty(0);@ ";
140
    fprintf fmt "@ /* Infinite loop */@ ";
141
    fprintf fmt "@[<v 2>while(1){@ ";
142
    fprintf fmt  "fflush(stdout);@ ";
143
    List.iteri (fun idx _ -> fprintf fmt "fflush(f_in%i);@ " (idx+1)) m.mstep.step_inputs;
144
    List.iteri (fun idx _ -> fprintf fmt "fflush(f_out%i);@ " (idx+1)) m.mstep.step_outputs;
145
    fprintf fmt "%a@ %t%a"
146
      print_get_inputs m
147
      (fun fmt -> pp_main_call mname main_mem fmt m input_values m.mstep.step_outputs)
148
      print_put_outputs m
149
  end
150

    
151
let print_main_code fmt basename m =
152
  let mname = m.mname.node_id in
153
  let main_mem =
154
    if (!Options.static_mem && !Options.main_node <> "")
155
    then "&main_mem"
156
    else "main_mem" in
157
  fprintf fmt "@[<v 2>int main (int argc, char *argv[]) {@ ";
158
  print_main_inout_declaration basename fmt m;
159
  Plugins.c_backend_main_loop_body_prefix basename mname fmt ();
160
  print_main_memory_allocation mname main_mem fmt m;
161
  if !Options.mpfr then
162
    begin
163
      print_global_initialize fmt basename;
164
      print_main_initialize mname main_mem fmt m;
165
    end;
166
  print_main_loop mname main_mem fmt m;
167

    
168
  Plugins.c_backend_main_loop_body_suffix fmt ();
169
  fprintf fmt "@]@ }@ @ ";
170
  if !Options.mpfr then
171
    begin
172
      print_main_clear mname main_mem fmt m;
173
      print_global_clear fmt basename;
174
    end;
175
  fprintf fmt "@ return 1;";
176
  fprintf fmt "@]@ }@."       
177

    
178
let print_main_header fmt =
179
  fprintf fmt (if !Options.cpp then "#include <stdio.h>@.#include <unistd.h>@.#include \"%s/io_frontend.hpp\"@." else "#include <stdio.h>@.#include <unistd.h>@.#include \"%s/io_frontend.h\"@.")
180
    (Options_management.core_dependency "io_frontend")
181

    
182
let print_main_c main_fmt main_machine basename prog machines _ (*dependencies*) =
183
  print_main_header main_fmt;
184
  fprintf main_fmt "#include <stdlib.h>@.#include <assert.h>@.";
185
  print_import_alloc_prototype main_fmt (Dep (true, basename, [], true (* assuming it is stateful*) ));
186
  pp_print_newline main_fmt ();
187

    
188
  (* Print the svn version number and the supported C standard (C90 or C99) *)
189
  print_version main_fmt;
190
  print_main_code main_fmt basename main_machine
191
end  
192

    
193
(* Local Variables: *)
194
(* compile-command:"make -C ../../.." *)
195
(* End: *)