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 Utils.Format
|
16
|
open C_backend_common
|
17
|
open Utils
|
18
|
|
19
|
module type MODIFIERS_MAINSRC = sig
|
20
|
end
|
21
|
|
22
|
module EmptyMod = struct
|
23
|
end
|
24
|
|
25
|
module Main = functor (Mod: MODIFIERS_MAINSRC) -> struct
|
26
|
|
27
|
(********************************************************************************************)
|
28
|
(* Main related functions *)
|
29
|
(********************************************************************************************)
|
30
|
|
31
|
let pp_c_main_var_input fmt id =
|
32
|
fprintf fmt "%s" id.var_id
|
33
|
|
34
|
let pp_c_main_var_output fmt id =
|
35
|
if Types.is_address_type id.var_type
|
36
|
then
|
37
|
fprintf fmt "%s" id.var_id
|
38
|
else
|
39
|
fprintf fmt "&%s" id.var_id
|
40
|
|
41
|
let print_put_output fmt id o' o =
|
42
|
let suff = string_of_int (id + 1) in
|
43
|
print_put_var fmt suff o'.var_id o.var_type o.var_id
|
44
|
|
45
|
let print_main_inout_declaration fmt m =
|
46
|
fprintf fmt
|
47
|
"/* Declaration of inputs/outputs variables */@,\
|
48
|
%a%a\
|
49
|
@[<v 2>if (traces) {@,\
|
50
|
%a%a@]@,\
|
51
|
}"
|
52
|
(pp_print_list_i
|
53
|
~pp_open_box:pp_open_vbox0
|
54
|
~pp_epilogue:pp_print_cut
|
55
|
(fun fmt idx v ->
|
56
|
fprintf fmt "%a; %a"
|
57
|
(pp_c_type v.var_id) v.var_type
|
58
|
(fun fmt () -> pp_file_decl fmt "in" idx) ())) m.mstep.step_inputs
|
59
|
(pp_print_list_i
|
60
|
~pp_open_box:pp_open_vbox0
|
61
|
~pp_epilogue:pp_print_cut
|
62
|
(fun fmt idx v ->
|
63
|
fprintf fmt "%a; %a"
|
64
|
(pp_c_type v.var_id) v.var_type
|
65
|
(fun fmt () -> pp_file_decl fmt "out" idx) ())) m.mstep.step_outputs
|
66
|
(pp_print_list_i
|
67
|
~pp_epilogue:pp_print_cut
|
68
|
(fun fmt idx _ ->
|
69
|
ignore (pp_file_open fmt "in" idx))) m.mstep.step_inputs
|
70
|
(pp_print_list_i
|
71
|
(fun fmt idx _ ->
|
72
|
ignore (pp_file_open fmt "out" idx))) m.mstep.step_outputs
|
73
|
|
74
|
let print_main_memory_allocation mname main_mem fmt m =
|
75
|
if not (fst (get_stateless_status m)) then
|
76
|
fprintf fmt
|
77
|
"@[<v>/* Main memory allocation */@,\
|
78
|
%a@,\
|
79
|
@,\
|
80
|
/* Initialize the main memory */@,\
|
81
|
%a(%s);@]"
|
82
|
(fun fmt () ->
|
83
|
if !Options.static_mem && !Options.main_node <> "" then
|
84
|
fprintf fmt "%a(static,main_mem);"
|
85
|
pp_machine_static_alloc_name mname
|
86
|
else
|
87
|
fprintf fmt "%a *main_mem = %a();"
|
88
|
pp_machine_memtype_name mname
|
89
|
pp_machine_alloc_name mname) ()
|
90
|
pp_machine_reset_name mname
|
91
|
main_mem
|
92
|
|
93
|
let print_global_initialize fmt basename =
|
94
|
let mNAME = file_to_module_name basename in
|
95
|
fprintf fmt "/* Initialize global constants */@,%a();"
|
96
|
pp_global_init_name mNAME
|
97
|
|
98
|
let print_global_clear fmt basename =
|
99
|
let mNAME = file_to_module_name basename in
|
100
|
fprintf fmt "/* Clear global constants */@,%a();"
|
101
|
pp_global_clear_name mNAME
|
102
|
|
103
|
let print_main_initialize mname main_mem fmt m =
|
104
|
let inputs = mpfr_vars m.mstep.step_inputs in
|
105
|
let outputs = mpfr_vars m.mstep.step_outputs in
|
106
|
if not (fst (get_stateless_status m))
|
107
|
then
|
108
|
fprintf fmt
|
109
|
"/* Initialize inputs, outputs and memories */@,\
|
110
|
%a%a%a(%s);"
|
111
|
(pp_print_list
|
112
|
~pp_open_box:pp_open_vbox0
|
113
|
~pp_eol:pp_print_cut
|
114
|
(pp_initialize m main_mem (pp_c_var_read m))) inputs
|
115
|
(pp_print_list
|
116
|
~pp_open_box:pp_open_vbox0
|
117
|
~pp_eol:pp_print_cut
|
118
|
(pp_initialize m main_mem (pp_c_var_read m))) outputs
|
119
|
pp_machine_init_name mname
|
120
|
main_mem
|
121
|
else
|
122
|
fprintf fmt
|
123
|
"/* Initialize inputs and outputs */@,\
|
124
|
%a%a@ "
|
125
|
(pp_print_list
|
126
|
~pp_open_box:pp_open_vbox0
|
127
|
~pp_eol:pp_print_cut
|
128
|
(pp_initialize m main_mem (pp_c_var_read m))) inputs
|
129
|
(pp_print_list
|
130
|
~pp_open_box:pp_open_vbox0
|
131
|
(pp_initialize m main_mem (pp_c_var_read m))) outputs
|
132
|
|
133
|
let print_main_clear mname main_mem fmt m =
|
134
|
let inputs = mpfr_vars m.mstep.step_inputs in
|
135
|
let outputs = mpfr_vars m.mstep.step_outputs in
|
136
|
if not (fst (get_stateless_status m))
|
137
|
then
|
138
|
fprintf fmt
|
139
|
"@[<v>/* Clear inputs, outputs and memories */@,\
|
140
|
%a%a%a(%s);@]"
|
141
|
(pp_print_list
|
142
|
~pp_open_box:pp_open_vbox0
|
143
|
~pp_eol:pp_print_cut
|
144
|
(pp_clear m main_mem (pp_c_var_read m))) inputs
|
145
|
(pp_print_list
|
146
|
~pp_open_box:pp_open_vbox0
|
147
|
~pp_eol:pp_print_cut
|
148
|
(pp_clear m main_mem (pp_c_var_read m))) outputs
|
149
|
pp_machine_clear_name mname
|
150
|
main_mem
|
151
|
else
|
152
|
fprintf fmt
|
153
|
"@[<v>/* Clear inputs and outputs */@,\
|
154
|
%a%a@]"
|
155
|
(pp_print_list
|
156
|
~pp_open_box:pp_open_vbox0
|
157
|
~pp_eol:pp_print_cut
|
158
|
(pp_clear m main_mem (pp_c_var_read m))) inputs
|
159
|
(pp_print_list
|
160
|
~pp_open_box:pp_open_vbox0
|
161
|
(pp_clear m main_mem (pp_c_var_read m))) outputs
|
162
|
|
163
|
let print_get_input fmt id v' v =
|
164
|
let pp_file = pp_print_file ("in" ^ string_of_int (id + 1)) in
|
165
|
let unclocked_t = Types.unclock_type v.var_type in
|
166
|
fprintf fmt "@[<v>%a@]"
|
167
|
(fun fmt () ->
|
168
|
if Types.is_int_type unclocked_t then
|
169
|
fprintf fmt "%s = _get_int(\"%s\");@,%a"
|
170
|
v.var_id v'.var_id
|
171
|
pp_file ("d", v.var_id)
|
172
|
else if Types.is_bool_type unclocked_t then
|
173
|
fprintf fmt "%s = _get_bool(\"%s\");@,%a"
|
174
|
v.var_id v'.var_id
|
175
|
pp_file ("i", v.var_id)
|
176
|
else if Types.is_real_type unclocked_t then
|
177
|
if !Options.mpfr then
|
178
|
fprintf fmt "double %s_tmp = _get_double(\"%s\");@,\
|
179
|
%a@,\
|
180
|
mpfr_set_d(%s, %s_tmp, %i);"
|
181
|
v.var_id v'.var_id
|
182
|
pp_file ("f", v.var_id ^ "_tmp")
|
183
|
v.var_id v.var_id (Mpfr.mpfr_prec ())
|
184
|
else
|
185
|
fprintf fmt "%s = _get_double(\"%s\");@,%a"
|
186
|
v.var_id v'.var_id
|
187
|
pp_file ("f", v.var_id)
|
188
|
else begin
|
189
|
Global.main_node := !Options.main_node;
|
190
|
eprintf "Code generation error: %a%a@."
|
191
|
Error.pp_error_msg Error.Main_wrong_kind
|
192
|
Location.pp_loc v'.var_loc;
|
193
|
raise (Error.Error (v'.var_loc, Error.Main_wrong_kind))
|
194
|
end) ()
|
195
|
|
196
|
let pp_main_call mname self fmt m (inputs: value_t list) (outputs: var_decl list) =
|
197
|
if fst (Machine_code_common.get_stateless_status m)
|
198
|
then
|
199
|
fprintf fmt "%a (%a%a);"
|
200
|
pp_machine_step_name mname
|
201
|
(pp_print_list ~pp_sep:pp_print_comma ~pp_eol:pp_print_comma
|
202
|
(pp_c_val m self pp_c_main_var_input)) inputs
|
203
|
(pp_print_list ~pp_sep:pp_print_comma pp_c_main_var_output) outputs
|
204
|
else
|
205
|
fprintf fmt "%a (%a%a%s);"
|
206
|
pp_machine_step_name mname
|
207
|
(pp_print_list ~pp_sep:pp_print_comma ~pp_eol:pp_print_comma
|
208
|
(pp_c_val m self pp_c_main_var_input)) inputs
|
209
|
(pp_print_list ~pp_sep:pp_print_comma ~pp_eol:pp_print_comma
|
210
|
pp_c_main_var_output) outputs
|
211
|
self
|
212
|
|
213
|
let print_main_loop mname main_mem fmt m =
|
214
|
let input_values = List.map (fun v ->
|
215
|
mk_val (Var v) v.var_type) m.mstep.step_inputs in
|
216
|
fprintf fmt
|
217
|
"ISATTY = isatty(0);@,\
|
218
|
@,\
|
219
|
/* Infinite loop */@,\
|
220
|
@[<v 2>while(1){@,\
|
221
|
fflush(stdout);@,\
|
222
|
@[<v 2>if (traces) {@,\
|
223
|
%a%a\
|
224
|
@]@,\
|
225
|
}@,\
|
226
|
%a%a%a"
|
227
|
|
228
|
(pp_print_list_i
|
229
|
~pp_open_box:pp_open_vbox0
|
230
|
~pp_epilogue:pp_print_cut
|
231
|
(fun fmt idx _ -> fprintf fmt "fflush(f_in%i);" (idx + 1)))
|
232
|
m.mstep.step_inputs
|
233
|
|
234
|
(pp_print_list_i
|
235
|
~pp_open_box:pp_open_vbox0
|
236
|
(fun fmt idx _ -> fprintf fmt "fflush(f_out%i);" (idx + 1)))
|
237
|
m.mstep.step_outputs
|
238
|
|
239
|
(pp_print_list_i2
|
240
|
~pp_open_box:pp_open_vbox0
|
241
|
~pp_epilogue:pp_print_cut
|
242
|
print_get_input)
|
243
|
(m.mname.node_inputs, m.mstep.step_inputs)
|
244
|
|
245
|
(fun fmt () ->
|
246
|
pp_main_call mname main_mem fmt m input_values m.mstep.step_outputs) ()
|
247
|
|
248
|
(pp_print_list_i2
|
249
|
~pp_open_box:pp_open_vbox0
|
250
|
~pp_prologue:pp_print_cut
|
251
|
print_put_output)
|
252
|
(m.mname.node_outputs, m.mstep.step_outputs)
|
253
|
|
254
|
let print_usage fmt () =
|
255
|
fprintf fmt
|
256
|
"@[<v 2>\
|
257
|
void usage(char *argv[]) {@,\
|
258
|
printf(\"Usage: %%s\\n\", argv[0]);@,\
|
259
|
printf(\" -t: produce trace files for input/output flows\\n\");@,\
|
260
|
printf(\" -d<dir>: directory containing traces (default: _traces)\\n\");@,\
|
261
|
printf(\" -p<prefix>: prefix_simu.scope<id> (default: file_node)\\n\");@,\
|
262
|
exit (8);@]@,\
|
263
|
}"
|
264
|
|
265
|
let print_options fmt name =
|
266
|
fprintf fmt
|
267
|
"@[<v>int traces = 0;@,\
|
268
|
char* prefix = \"%s\";@,\
|
269
|
char* dir = \".\";@,\
|
270
|
@[<v 2>while ((argc > 1) && (argv[1][0] == '-')) {@,\
|
271
|
@[<v 2>switch (argv[1][1]) {@,\
|
272
|
@[<v 2>case 't':@,\
|
273
|
traces = 1;@,\
|
274
|
break;@,\
|
275
|
@]@,\
|
276
|
@[<v 2>case 'd':@,\
|
277
|
dir = &argv[1][2];@,\
|
278
|
break;@,\
|
279
|
@]@,\
|
280
|
@[<v 2>case 'p':@,\
|
281
|
prefix = &argv[1][2];@,\
|
282
|
break;@,\
|
283
|
@]@,\
|
284
|
@[<v 2>default:@,\
|
285
|
printf(\"Wrong Argument: %%s\\n\", argv[1]);@,\
|
286
|
usage(argv);@]@]@,\
|
287
|
}@,\
|
288
|
++argv;@,\
|
289
|
--argc;@]@,\
|
290
|
}@]"
|
291
|
name
|
292
|
|
293
|
let print_main_code fmt (basename, m) =
|
294
|
let mname = m.mname.node_id in
|
295
|
(* TODO: find a proper way to shorthen long names. This causes segfault in the binary when trying to fprintf in them *)
|
296
|
let mname = if String.length mname > 50
|
297
|
then string_of_int (Hashtbl.hash mname) else mname in
|
298
|
let main_mem =
|
299
|
if !Options.static_mem && !Options.main_node <> ""
|
300
|
then "&main_mem"
|
301
|
else "main_mem" in
|
302
|
|
303
|
fprintf fmt
|
304
|
"@[<v>\
|
305
|
%a@,\
|
306
|
@,\
|
307
|
@[<v 2>int main (int argc, char *argv[]) {@,\
|
308
|
%a@,\
|
309
|
@,\
|
310
|
%a@,\
|
311
|
%a@,\
|
312
|
%a@,\
|
313
|
%a@,\
|
314
|
%a@,\
|
315
|
%a@]@,\
|
316
|
}@,\
|
317
|
%areturn 1;@]@,}@]@."
|
318
|
|
319
|
print_usage ()
|
320
|
|
321
|
print_options (basename ^ "_" ^ mname)
|
322
|
|
323
|
print_main_inout_declaration m
|
324
|
|
325
|
(Plugins.c_backend_main_loop_body_prefix basename mname) ()
|
326
|
|
327
|
(print_main_memory_allocation mname main_mem) m
|
328
|
|
329
|
(fun fmt () ->
|
330
|
if !Options.mpfr then
|
331
|
fprintf fmt "@[<v>%a@,%a@]@,"
|
332
|
print_global_initialize basename
|
333
|
(print_main_initialize mname main_mem) m) ()
|
334
|
|
335
|
(print_main_loop mname main_mem) m
|
336
|
|
337
|
Plugins.c_backend_main_loop_body_suffix ()
|
338
|
|
339
|
(fun fmt () ->
|
340
|
if !Options.mpfr then
|
341
|
fprintf fmt "@[<v>%a@,%a@]@,"
|
342
|
(print_main_clear mname main_mem) m
|
343
|
print_global_clear basename) ()
|
344
|
|
345
|
let print_main_header fmt () =
|
346
|
fprintf fmt
|
347
|
"@[<v>#include <stdio.h>@,\
|
348
|
#include <unistd.h>@,\
|
349
|
%a@]"
|
350
|
(fun fmt () -> fprintf fmt
|
351
|
(if !Options.cpp then
|
352
|
"#include \"%s/io_frontend.hpp\""
|
353
|
else
|
354
|
"#include <string.h>@,\
|
355
|
#include \"%s/io_frontend.h\"")
|
356
|
(Options_management.core_dependency "io_frontend")) ()
|
357
|
|
358
|
let print_main_c main_fmt main_machine basename _prog _machines _dependencies =
|
359
|
fprintf main_fmt
|
360
|
"@[<v>\
|
361
|
%a@,\
|
362
|
#include <stdlib.h>@,\
|
363
|
#include <assert.h>@,\
|
364
|
%a@,\
|
365
|
@,\
|
366
|
%a@,\
|
367
|
%a
|
368
|
@]@."
|
369
|
print_main_header ()
|
370
|
print_import_alloc_prototype
|
371
|
{
|
372
|
local = true;
|
373
|
name = basename;
|
374
|
content = [];
|
375
|
is_stateful = true (* assuming it is stateful*)
|
376
|
}
|
377
|
|
378
|
(* Print the svn version number and the supported C standard (C90 or C99) *)
|
379
|
pp_print_version ()
|
380
|
|
381
|
print_main_code (basename, main_machine)
|
382
|
|
383
|
end
|
384
|
|
385
|
(* Local Variables: *)
|
386
|
(* compile-command:"make -C ../../.." *)
|
387
|
(* End: *)
|