Project

General

Profile

Download (8.69 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 Lustre_types
13
open Machine_code_types
14
open Machine_code_common
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

    
35
let print_put_outputs fmt m = 
36
  let po fmt (id, o', o) =
37
    let suff = string_of_int id in
38
    print_put_var fmt suff o'.var_id o.var_type o.var_id
39
  in
40
  List.iteri2 (fun idx v' v -> fprintf fmt "@ %a;" po ((idx+1), v', v)) m.mname.node_outputs m.mstep.step_outputs
41

    
42
  
43
let print_main_inout_declaration m fmt =
44
  fprintf fmt "/* Declaration of inputs/outputs variables */@ ";
45
  List.iteri (fun idx v ->
46
      fprintf fmt "%a;@ " (pp_c_type v.var_id) v.var_type;
47
      ignore (pp_file_decl fmt "in" idx) 
48
    ) m.mstep.step_inputs;
49
  List.iteri (fun idx v ->
50
      fprintf fmt "%a;@ " (pp_c_type v.var_id) v.var_type;
51
      ignore (pp_file_decl fmt "out" idx)
52
    ) m.mstep.step_outputs;
53
  fprintf fmt "@[<v 2>if (traces) {@ ";
54
  List.iteri (fun idx _ ->
55
      ignore (pp_file_open fmt "in" idx) 
56
    ) m.mstep.step_inputs;
57
  List.iteri (fun idx _ ->
58
      ignore (pp_file_open fmt "out" idx)
59
    ) m.mstep.step_outputs;
60
  fprintf fmt "@]}@ "
61

    
62
  
63
let print_main_memory_allocation mname main_mem fmt m =
64
  if not (fst (get_stateless_status m)) then
65
  begin  
66
    fprintf fmt "@ /* Main memory allocation */@ ";
67
    if (!Options.static_mem && !Options.main_node <> "")
68
    then (fprintf fmt "%a(static,main_mem);@ " pp_machine_static_alloc_name mname)
69
    else (fprintf fmt "%a *main_mem = %a();@ " pp_machine_memtype_name mname pp_machine_alloc_name mname);
70
    fprintf fmt "@ /* Initialize the main memory */@ ";
71
    fprintf fmt "%a(%s);@ " pp_machine_reset_name mname main_mem;
72
  end
73

    
74
let print_global_initialize fmt basename =
75
  let mNAME = file_to_module_name basename in
76
  fprintf fmt "@ /* Initialize global constants */@ %a();@ "
77
    pp_global_init_name mNAME
78

    
79
let print_global_clear fmt basename =
80
  let mNAME = file_to_module_name basename in
81
  fprintf fmt "@ /* Clear global constants */@ %a();@ "
82
    pp_global_clear_name mNAME
83

    
84
let print_main_initialize mname main_mem fmt m =
85
  if not (fst (get_stateless_status m))
86
  then
87
    fprintf fmt "@ /* Initialize inputs, outputs and memories */@ %a%t%a%t%a(%s);@ "
88
      (Utils.fprintf_list ~sep:"@ " (pp_initialize m main_mem (pp_c_var_read m))) m.mstep.step_inputs
89
      (Utils.pp_newline_if_non_empty m.mstep.step_inputs)
90
      (Utils.fprintf_list ~sep:"@ " (pp_initialize m main_mem (pp_c_var_read m))) m.mstep.step_outputs
91
      (Utils.pp_newline_if_non_empty m.mstep.step_inputs)
92
      pp_machine_init_name mname
93
      main_mem
94
  else
95
    fprintf fmt "@ /* Initialize inputs and outputs */@ %a%t%a@ "
96
      (Utils.fprintf_list ~sep:"@ " (pp_initialize m main_mem (pp_c_var_read m))) m.mstep.step_inputs
97
      (Utils.pp_newline_if_non_empty m.mstep.step_inputs)
98
      (Utils.fprintf_list ~sep:"@ " (pp_initialize m main_mem (pp_c_var_read m))) m.mstep.step_outputs
99

    
100
let print_main_clear mname main_mem fmt m =
101
  if not (fst (get_stateless_status m))
102
  then
103
    fprintf fmt "@ /* Clear inputs, outputs and memories */@ %a%t%a%t%a(%s);@ "
104
      (Utils.fprintf_list ~sep:"@ " (pp_clear 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_clear 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_clear_name mname
109
      main_mem
110
  else
111
    fprintf fmt "@ /* Clear inputs and outputs */@ %a%t%a@ "
112
      (Utils.fprintf_list ~sep:"@ " (pp_clear 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_clear m main_mem (pp_c_var_read m))) m.mstep.step_outputs
115

    
116
let print_main_loop mname main_mem fmt m =
117
  let input_values =
118
    List.map (fun v -> mk_val (Var v) v.var_type)
119
      m.mstep.step_inputs in
120
  begin
121
    fprintf fmt "@ ISATTY = isatty(0);@ ";
122
    fprintf fmt "@ /* Infinite loop */@ ";
123
    fprintf fmt "@[<v 2>while(1){@ ";
124
    fprintf fmt  "fflush(stdout);@ ";
125
    fprintf fmt "@[<v 2>if (traces) {@ ";
126
    List.iteri (fun idx _ -> fprintf fmt "fflush(f_in%i);@ " (idx+1)) m.mstep.step_inputs;
127
    List.iteri (fun idx _ -> fprintf fmt "fflush(f_out%i);@ " (idx+1)) m.mstep.step_outputs;
128
    fprintf fmt "@]}@ ";
129
    fprintf fmt "%a@ %t%a"
130
      print_get_inputs m
131
      (fun fmt -> pp_main_call mname main_mem fmt m input_values m.mstep.step_outputs)
132
      print_put_outputs m
133
  end
134

    
135
let print_usage fmt =
136
  fprintf fmt "@[<v 2>void usage(char *argv[]) {@ ";
137
  fprintf fmt "printf(\"Usage: %%s\\n\", argv[0]);@ ";
138
  fprintf fmt "printf(\" -t: produce trace files for input/output flows\\n\");@ ";
139
  fprintf fmt "printf(\" -d<dir>: directory containing traces (default: _traces)\\n\");@ ";
140
  fprintf fmt "printf(\" -p<prefix>: prefix_simu.scope<id> (default: file_node)\\n\");@ ";
141
  fprintf fmt "exit (8);@ ";
142
  fprintf fmt "@]}@ "
143

    
144
let print_options fmt name =
145
  fprintf fmt "int traces = 0;@ ";
146
  fprintf fmt "char* prefix = \"%s\";@ " name;
147
  fprintf fmt "char* dir = \".\";@ ";
148
  fprintf fmt "@[<v 2>while ((argc > 1) && (argv[1][0] == '-')) {@ ";
149
  fprintf fmt "@[<v 2>switch (argv[1][1]) {@ ";
150
  fprintf fmt "@[<v 2>case 't':@ ";
151
  fprintf fmt "traces = 1;@ ";
152
  fprintf fmt "break;@ ";
153
  fprintf fmt "@]@ ";
154
  fprintf fmt "@[<v 2>case 'd':@ ";
155
  fprintf fmt "dir = &argv[1][2];@ ";
156
  fprintf fmt "break;@ ";
157
  fprintf fmt "@]@ ";
158
  fprintf fmt "@[<v 2>case 'p':@ ";
159
  fprintf fmt "prefix = &argv[1][2];@ ";
160
  fprintf fmt "break;@ ";
161
  fprintf fmt "@]@ ";
162
  fprintf fmt "@[<v 2>default:@ ";
163
  fprintf fmt "printf(\"Wrong Argument: %%s\\n\", argv[1]);@ ";
164
  fprintf fmt "usage(argv);@ ";
165
  fprintf fmt "@]@ ";
166
  fprintf fmt "@]}@ ";
167
  fprintf fmt "++argv;@ ";
168
  fprintf fmt "--argc;@ ";
169
  fprintf fmt "@]}@ "
170
  
171
let print_main_code fmt basename m =
172
  let mname = m.mname.node_id in
173
  (* TODO: find a proper way to shorthen long names. This causes segfault in the binary when trying to fprintf in them *)
174
  let mname = if String.length mname > 50 then string_of_int (Hashtbl.hash mname) else mname in
175
  
176
  let main_mem =
177
    if (!Options.static_mem && !Options.main_node <> "")
178
    then "&main_mem"
179
    else "main_mem" in
180
  print_usage fmt;
181
  
182
  fprintf fmt "@[<v 2>int main (int argc, char *argv[]) {@ ";
183
  print_options fmt (basename ^ "_" ^ mname);
184
  print_main_inout_declaration m fmt;
185
  Plugins.c_backend_main_loop_body_prefix basename mname fmt ();
186
  print_main_memory_allocation mname main_mem fmt m;
187
  if !Options.mpfr then
188
    begin
189
      print_global_initialize fmt basename;
190
      print_main_initialize mname main_mem fmt m;
191
    end;
192
  print_main_loop mname main_mem fmt m;
193

    
194
  Plugins.c_backend_main_loop_body_suffix fmt ();
195
  fprintf fmt "@]@ }@ @ ";
196
  if !Options.mpfr then
197
    begin
198
      print_main_clear mname main_mem fmt m;
199
      print_global_clear fmt basename;
200
    end;
201
  fprintf fmt "@ return 1;";
202
  fprintf fmt "@]@ }@."       
203

    
204
let print_main_header fmt =
205
  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 <string.h>@.#include \"%s/io_frontend.h\"@.")
206
    (Options_management.core_dependency "io_frontend")
207

    
208
let print_main_c main_fmt main_machine basename _prog _machines _dependencies =
209
  print_main_header main_fmt;
210
  fprintf main_fmt "#include <stdlib.h>@.#include <assert.h>@.";
211
  print_import_alloc_prototype main_fmt {local=true; name=basename; content=[]; is_stateful=true} (* assuming it is stateful*) ;
212
  pp_print_newline main_fmt ();
213

    
214
  (* Print the svn version number and the supported C standard (C90 or C99) *)
215
  print_version main_fmt;
216
  print_main_code main_fmt basename main_machine
217
end  
218

    
219
(* Local Variables: *)
220
(* compile-command:"make -C ../../.." *)
221
(* End: *)
(6-6/10)