Project

General

Profile

Statistics
| Branch: | Tag: | Revision:

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

History | View | Annotate | Download (7.95 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
  fprintf fmt "/* Declaration of inputs/outputs variables */@ ";
64
  List.iteri 
65
    (fun idx v ->
66
      fprintf fmt "%a;@ " (pp_c_type v.var_id) v.var_type;
67
      fprintf fmt "FILE *f_in%i;@ " (idx+1); (* we start from 1: in1, in2, ... *)
68
      fprintf fmt "f_in%i = fopen(\"%s_%s_simu.in%i\", \"w\");@ " (idx+1) basename mname (idx+1);
69
    ) m.mstep.step_inputs;
70
  List.iteri 
71
    (fun idx v ->
72
      fprintf fmt "%a;@ " (pp_c_type v.var_id) v.var_type;
73
      fprintf fmt "FILE *f_out%i;@ " (idx+1); (* we start from 1: in1, in2, ... *)
74
      fprintf fmt "f_out%i = fopen(\"%s_%s_simu.out%i\", \"w\");@ " (idx+1) basename mname (idx+1);
75
    ) m.mstep.step_outputs
76

    
77

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

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

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

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

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

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

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

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

    
176
let print_main_header fmt =
177
  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\"@.")
178
    (Options_management.core_dependency "io_frontend")
179

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

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

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