Project

General

Profile

Statistics
| Branch: | Tag: | Revision:

lustrec / src / backends / C / c_backend_common.ml @ 5318ad88

History | View | Annotate | Download (28 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 Format
13
open Lustre_types
14
open Corelang
15
open Machine_code_types
16
open Machine_code_common
17

    
18

    
19
let print_version fmt =
20
  Format.fprintf fmt 
21
    "/* @[<v>C code generated by %s@,Version number %s@,Code is %s compliant@,Using %s numbers */@,@]@."
22
    (Filename.basename Sys.executable_name) 
23
    Version.number 
24
    (if !Options.ansi then "ANSI C90" else "C99")
25
    (if !Options.mpfr then "MPFR multi-precision" else "(double) floating-point")
26

    
27
let protect_filename s =
28
  Str.global_replace (Str.regexp "\\.\\|\\ ") "_" s
29

    
30
let file_to_module_name basename =
31
  let baseNAME = Ocaml_utils.uppercase basename in
32
  let baseNAME = protect_filename baseNAME in
33
  baseNAME
34

    
35
(* Generation of a non-clashing name for the self memory variable (for step and reset functions) *)
36
let mk_self m =
37
  let used name =
38
       (List.exists (fun v -> v.var_id = name) m.mstep.step_inputs)
39
    || (List.exists (fun v -> v.var_id = name) m.mstep.step_outputs)
40
    || (List.exists (fun v -> v.var_id = name) m.mstep.step_locals)
41
    || (List.exists (fun v -> v.var_id = name) m.mmemory) in
42
  mk_new_name used "self"
43

    
44
(* Generation of a non-clashing name for the instance variable of static allocation macro *)
45
let mk_instance m =
46
  let used name =
47
       (List.exists (fun v -> v.var_id = name) m.mstep.step_inputs)
48
    || (List.exists (fun v -> v.var_id = name) m.mmemory) in
49
  mk_new_name used "inst"
50

    
51
(* Generation of a non-clashing name for the attribute variable of static allocation macro *)
52
let mk_attribute m =
53
  let used name =
54
       (List.exists (fun v -> v.var_id = name) m.mstep.step_inputs)
55
    || (List.exists (fun v -> v.var_id = name) m.mmemory) in
56
  mk_new_name used "attr"
57

    
58
let mk_call_var_decl loc id =
59
  { var_id = id;
60
    var_orig = false;
61
    var_dec_type = mktyp Location.dummy_loc Tydec_any;
62
    var_dec_clock = mkclock Location.dummy_loc Ckdec_any;
63
    var_dec_const = false;
64
    var_dec_value = None;
65
    var_parent_nodeid = None;
66
    var_type = Type_predef.type_arrow (Types.new_var ()) (Types.new_var ());
67
    var_clock = Clocks.new_var true;
68
    var_loc = loc }
69

    
70
(* counter for loop variable creation *)
71
let loop_cpt = ref (-1)
72

    
73
let reset_loop_counter () =
74
 loop_cpt := -1
75

    
76
let mk_loop_var m () =
77
  let vars = m.mstep.step_inputs@m.mstep.step_outputs@m.mstep.step_locals@m.mmemory in
78
  let rec aux () =
79
    incr loop_cpt;
80
    let s = Printf.sprintf "__%s_%d" "i" !loop_cpt in
81
    if List.exists (fun v -> v.var_id = s) vars then aux () else s
82
  in aux ()
83
(*
84
let addr_cpt = ref (-1)
85

    
86
let reset_addr_counter () =
87
 addr_cpt := -1
88

    
89
let mk_addr_var m var =
90
  let vars = m.mmemory in
91
  let rec aux () =
92
    incr addr_cpt;
93
    let s = Printf.sprintf "%s_%s_%d" var "addr" !addr_cpt in
94
    if List.exists (fun v -> v.var_id = s) vars then aux () else s
95
  in aux ()
96
*)
97
let pp_global_init_name fmt id = fprintf fmt "%s_INIT" id
98
let pp_global_clear_name fmt id = fprintf fmt "%s_CLEAR" id
99
let pp_machine_memtype_name fmt id = fprintf fmt "struct %s_mem" id
100
let pp_machine_regtype_name fmt id = fprintf fmt "struct %s_reg" id
101
let pp_machine_alloc_name fmt id = fprintf fmt "%s_alloc" id
102
let pp_machine_dealloc_name fmt id = fprintf fmt "%s_dealloc" id
103
let pp_machine_static_declare_name fmt id = fprintf fmt "%s_DECLARE" id
104
let pp_machine_static_link_name fmt id = fprintf fmt "%s_LINK" id
105
let pp_machine_static_alloc_name fmt id = fprintf fmt "%s_ALLOC" id
106
let pp_machine_reset_name fmt id = fprintf fmt "%s_reset" id
107
let pp_machine_init_name fmt id = fprintf fmt "%s_init" id
108
let pp_machine_clear_name fmt id = fprintf fmt "%s_clear" id
109
let pp_machine_step_name fmt id = fprintf fmt "%s_step" id
110

    
111
let pp_mod pp_val v1 v2 fmt =
112
  if !Options.integer_div_euclidean then
113
    (* (a mod_C b) + (a < 0 ? abs(b) : 0) *)
114
    Format.fprintf fmt "((%a %% %a) + (%a < 0?(abs(%a)):0))"
115
      pp_val v1 pp_val v2
116
      pp_val v1 pp_val v2
117
  else (* Regular behavior: printing a % *)
118
    Format.fprintf fmt "(%a %% %a)" pp_val v1 pp_val v2
119

    
120
let pp_div pp_val v1 v2 fmt =
121
  if !Options.integer_div_euclidean then
122
    (* (a - ((a mod_C b) + (a < 0 ? abs(b) : 0))) div_C b *)
123
    Format.fprintf fmt "(%a - ((%a %% %a) + (%a < 0 ? abs(%a) : 0))) / %a"
124
      pp_val v1 pp_val v1 pp_val v2
125
      pp_val v1 pp_val v2 pp_val v2
126
  else (* Regular behavior: printing a / *)
127
    Format.fprintf fmt "(%a / %a)" pp_val v1 pp_val v2
128
  
129
let pp_basic_lib_fun is_int i pp_val fmt vl =
130
  match i, vl with
131
  (*  | "ite", [v1; v2; v3] -> Format.fprintf fmt "(%a?(%a):(%a))" pp_val v1 pp_val v2 pp_val v3 *)
132
  | "uminus", [v] -> Format.fprintf fmt "(- %a)" pp_val v
133
  | "not", [v] -> Format.fprintf fmt "(!%a)" pp_val v
134
  | "impl", [v1; v2] -> Format.fprintf fmt "(!%a || %a)" pp_val v1 pp_val v2
135
  | "=", [v1; v2] -> Format.fprintf fmt "(%a == %a)" pp_val v1 pp_val v2
136
  | "mod", [v1; v2] ->
137
     if is_int then
138
       pp_mod pp_val v1 v2 fmt 
139
     else
140
       Format.fprintf fmt "(%a %% %a)" pp_val v1 pp_val v2
141
  | "equi", [v1; v2] -> Format.fprintf fmt "(!%a == !%a)" pp_val v1 pp_val v2
142
  | "xor", [v1; v2] -> Format.fprintf fmt "(!%a != !%a)" pp_val v1 pp_val v2
143
  | "/", [v1; v2] ->
144
     if is_int then
145
       pp_div pp_val v1 v2 fmt
146
     else
147
       Format.fprintf fmt "(%a / %a)" pp_val v1 pp_val v2
148
  | _, [v1; v2] -> Format.fprintf fmt "(%a %s %a)" pp_val v1 i pp_val v2
149
  | _ -> (Format.eprintf "internal error: Basic_library.pp_c %s@." i; assert false)
150

    
151

    
152
let rec pp_c_dimension fmt dim =
153
  match dim.Dimension.dim_desc with
154
  | Dimension.Dident id       ->
155
     fprintf fmt "%s" id
156
  | Dimension.Dint i          ->
157
     fprintf fmt "%d" i
158
  | Dimension.Dbool b         ->
159
     fprintf fmt "%B" b
160
  | Dimension.Dite (i, t, e)  ->
161
     fprintf fmt "((%a)?%a:%a)"
162
       pp_c_dimension i pp_c_dimension t pp_c_dimension e
163
  | Dimension.Dappl (f, args) ->
164
     fprintf fmt "%a" (pp_basic_lib_fun (Basic_library.is_numeric_operator f) f pp_c_dimension) args
165
  | Dimension.Dlink dim' -> fprintf fmt "%a" pp_c_dimension dim'
166
  | Dimension.Dvar       -> fprintf fmt "_%s" (Utils.name_of_dimension dim.Dimension.dim_id)
167
  | Dimension.Dunivar    -> fprintf fmt "'%s" (Utils.name_of_dimension dim.Dimension.dim_id)
168

    
169
let is_basic_c_type t =
170
  Types.is_int_type t || Types.is_real_type t || Types.is_bool_type t
171

    
172
let pp_c_basic_type_desc t_desc =
173
  if Types.is_bool_type t_desc then
174
    if !Options.cpp then "bool" else "_Bool"
175
  else if Types.is_int_type t_desc then !Options.int_type
176
  else if Types.is_real_type t_desc then
177
    if !Options.mpfr then Mpfr.mpfr_t else !Options.real_type
178
  else
179
    assert false (* Not a basic C type. Do not handle arrays or pointers *)
180

    
181
let pp_basic_c_type ?(var_opt=None) fmt t =
182
  match var_opt with
183
  | Some v when Machine_types.is_exportable v ->
184
     Machine_types.pp_c_var_type fmt v
185
  | _ ->
186
     fprintf fmt "%s" (pp_c_basic_type_desc t)
187

    
188
let pp_c_type ?(var_opt=None) var_id fmt t =
189
  let rec aux t pp_suffix =
190
    if is_basic_c_type  t then
191
       fprintf fmt "%a %s%a"
192
	 (pp_basic_c_type ~var_opt) t
193
	 var_id
194
	 pp_suffix ()
195
    else
196
      match (Types.repr t).Types.tdesc with
197
      | Types.Tclock t'       -> aux t' pp_suffix
198
      | Types.Tarray (d, t')  ->
199
	 let pp_suffix' fmt () = fprintf fmt "%a[%a]" pp_suffix () pp_c_dimension d in
200
	 aux t' pp_suffix'
201
      | Types.Tstatic (_, t') -> fprintf fmt "const "; aux t' pp_suffix
202
      | Types.Tconst ty       -> fprintf fmt "%s %s" ty var_id
203
      | Types.Tarrow (_, _)   -> fprintf fmt "void (*%s)()" var_id
204
      | _                     -> eprintf "internal error: C_backend_common.pp_c_type %a@." Types.print_ty t; assert false
205
  in aux t (fun fmt () -> ())
206
(*
207
let rec pp_c_initialize fmt t = 
208
  match (Types.repr t).Types.tdesc with
209
  | Types.Tint -> pp_print_string fmt "0"
210
  | Types.Tclock t' -> pp_c_initialize fmt t'
211
  | Types.Tbool -> pp_print_string fmt "0" 
212
  | Types.Treal when not !Options.mpfr -> pp_print_string fmt "0."
213
  | Types.Tarray (d, t') when Dimension.is_dimension_const d ->
214
    fprintf fmt "{%a}"
215
      (Utils.fprintf_list ~sep:"," (fun fmt _ -> pp_c_initialize fmt t'))
216
      (Utils.duplicate 0 (Dimension.size_const_dimension d))
217
  | _ -> assert false
218
 *)
219
let pp_c_tag fmt t =
220
 pp_print_string fmt (if t = tag_true then "1" else if t = tag_false then "0" else t)
221

    
222

    
223
(* Prints a constant value *)
224
let rec pp_c_const fmt c =
225
  match c with
226
    | Const_int i     -> pp_print_int fmt i
227
    | Const_real (c,e,s)-> pp_print_string fmt s (* Format.fprintf fmt "%ie%i" c e*)
228
    (* | Const_float r   -> pp_print_float fmt r *)
229
    | Const_tag t     -> pp_c_tag fmt t
230
    | Const_array ca  -> fprintf fmt "{%a }" (Utils.fprintf_list ~sep:", " pp_c_const) ca
231
    | Const_struct fl -> fprintf fmt "{%a }" (Utils.fprintf_list ~sep:", " (fun fmt (f, c) -> pp_c_const fmt c)) fl
232
    | Const_string _ -> assert false (* string occurs in annotations not in C *)
233

    
234
       
235
(* Prints a value expression [v], with internal function calls only.
236
   [pp_var] is a printer for variables (typically [pp_c_var_read]),
237
   but an offset suffix may be added for array variables
238
*)
239
let rec pp_c_val self pp_var fmt v =
240
  match v.value_desc with
241
  | Cst c         -> pp_c_const fmt c
242
  | Array vl      -> fprintf fmt "{%a}" (Utils.fprintf_list ~sep:", " (pp_c_val self pp_var)) vl
243
  | Access (t, i) -> fprintf fmt "%a[%a]" (pp_c_val self pp_var) t (pp_c_val self pp_var) i
244
  | Power (v, n)  -> (Format.eprintf "internal error: C_backend_common.pp_c_val %a@." pp_val v; assert false)
245
  | LocalVar v    -> pp_var fmt v
246
  | StateVar v    ->
247
    (* array memory vars are represented by an indirection to a local var with the right type,
248
       in order to avoid casting everywhere. *)
249
    if Types.is_array_type v.var_type && not (Types.is_real_type v.var_type && !Options.mpfr)
250
    then fprintf fmt "%a" pp_var v
251
    else fprintf fmt "%s->_reg.%a" self pp_var v
252
  | Fun (n, vl)   -> pp_basic_lib_fun (Types.is_int_type v.value_type) n (pp_c_val self pp_var) fmt vl
253

    
254
(* Access to the value of a variable:
255
   - if it's not a scalar output, then its name is enough
256
   - otherwise, dereference it (it has been declared as a pointer,
257
     despite its scalar Lustre type)
258
   - moreover, dereference memory array variables.
259
*)
260
let pp_c_var_read m fmt id =
261
  (* mpfr_t is a static array, not treated as general arrays *)
262
  if Types.is_address_type id.var_type
263
  then
264
    if is_memory m id && not (Types.is_real_type id.var_type && !Options.mpfr)
265
    then fprintf fmt "(*%s)" id.var_id
266
    else fprintf fmt "%s" id.var_id
267
  else
268
    if is_output m id
269
    then fprintf fmt "*%s" id.var_id
270
    else fprintf fmt "%s" id.var_id
271

    
272
(* Addressable value of a variable, the one that is passed around in calls:
273
   - if it's not a scalar non-output, then its name is enough
274
   - otherwise, reference it (it must be passed as a pointer,
275
     despite its scalar Lustre type)
276
*)
277
let pp_c_var_write m fmt id =
278
  if Types.is_address_type id.var_type
279
  then
280
    fprintf fmt "%s" id.var_id
281
  else
282
    if is_output m id
283
    then
284
      fprintf fmt "%s" id.var_id
285
    else
286
      fprintf fmt "&%s" id.var_id
287

    
288
(* Declaration of an input variable:
289
   - if its type is array/matrix/etc, then declare it as a mere pointer,
290
     in order to cope with unknown/parametric array dimensions, 
291
     as it is the case for generics
292
*)
293
let pp_c_decl_input_var fmt id =
294
  if !Options.ansi && Types.is_address_type id.var_type
295
  then pp_c_type ~var_opt:(Some id) (sprintf "(*%s)" id.var_id) fmt (Types.array_base_type id.var_type)
296
  else pp_c_type ~var_opt:(Some id) id.var_id fmt id.var_type
297

    
298
(* Declaration of an output variable:
299
   - if its type is scalar, then pass its address
300
   - if its type is array/matrix/struct/etc, then declare it as a mere pointer,
301
     in order to cope with unknown/parametric array dimensions, 
302
     as it is the case for generics
303
*)
304
let pp_c_decl_output_var fmt id =
305
  if (not !Options.ansi) && Types.is_address_type id.var_type
306
  then pp_c_type  ~var_opt:(Some id)                  id.var_id  fmt id.var_type
307
  else pp_c_type  ~var_opt:(Some id) (sprintf "(*%s)" id.var_id) fmt (Types.array_base_type id.var_type)
308

    
309
(* Declaration of a local/mem variable:
310
   - if it's an array/matrix/etc, its size(s) should be
311
     known in order to statically allocate memory, 
312
     so we print the full type
313
*)
314
let pp_c_decl_local_var m fmt id =
315
  if id.var_dec_const
316
  then
317
    Format.fprintf fmt "%a = %a"
318
      (pp_c_type  ~var_opt:(Some id) id.var_id) id.var_type
319
      (pp_c_val "" (pp_c_var_read m)) (get_const_assign m id)
320
  else
321
    Format.fprintf fmt "%a"
322
      (pp_c_type  ~var_opt:(Some id) id.var_id) id.var_type
323

    
324
let pp_c_decl_array_mem self fmt id =
325
  fprintf fmt "%a = (%a) (%s->_reg.%s)"
326
    (pp_c_type (sprintf "(*%s)" id.var_id)) id.var_type
327
    (pp_c_type "(*)") id.var_type
328
    self
329
    id.var_id
330

    
331
(* Declaration of a struct variable:
332
   - if it's an array/matrix/etc, we declare it as a pointer
333
*)
334
let pp_c_decl_struct_var fmt id =
335
  if Types.is_array_type id.var_type
336
  then pp_c_type (sprintf "(*%s)" id.var_id) fmt (Types.array_base_type id.var_type)
337
  else pp_c_type                  id.var_id  fmt id.var_type
338

    
339
let pp_c_decl_instance_var fmt (name, (node, static)) = 
340
  fprintf fmt "%a *%s" pp_machine_memtype_name (node_name node) name
341

    
342
let pp_c_checks self fmt m =
343
  Utils.fprintf_list ~sep:"" 
344
    (fun fmt (loc, check) -> 
345
      fprintf fmt 
346
	"@[<v>%a@,assert (%a);@]@," 
347
	Location.pp_c_loc loc
348
	(pp_c_val self (pp_c_var_read m)) check
349
    ) 
350
    fmt 
351
    m.mstep.step_checks
352

    
353
(********************************************************************************************)
354
(*                       Struct Printing functions                                          *)
355
(********************************************************************************************)
356

    
357
let pp_registers_struct fmt m =
358
  if m.mmemory <> []
359
  then
360
    fprintf fmt "@[%a {@[<v>%a;@ @]}@] _reg; "
361
      pp_machine_regtype_name m.mname.node_id
362
      (Utils.fprintf_list ~sep:";@ " pp_c_decl_struct_var) m.mmemory
363
  else
364
    ()
365

    
366
let print_machine_struct fmt m =
367
  if fst (get_stateless_status m) then
368
    begin
369
    end
370
  else
371
    begin
372
      (* Define struct *)
373
      fprintf fmt "@[%a {@[<v>%a%t%a%t@]};@]@."
374
	pp_machine_memtype_name m.mname.node_id
375
	pp_registers_struct m
376
	(Utils.pp_final_char_if_non_empty "@ " m.mmemory)
377
	(Utils.fprintf_list ~sep:";@ " pp_c_decl_instance_var) m.minstances
378
	(Utils.pp_final_char_if_non_empty ";@ " m.minstances)
379
    end
380

    
381
let print_machine_struct_from_header fmt inode =
382
  if inode.nodei_stateless then
383
    begin
384
    end
385
  else
386
    begin
387
      (* Declare struct *)
388
      fprintf fmt "@[%a;@]@."
389
	pp_machine_memtype_name inode.nodei_id
390
    end
391

    
392
(********************************************************************************************)
393
(*                      Prototype Printing functions                                        *)
394
(********************************************************************************************)
395

    
396
let print_global_init_prototype fmt baseNAME =
397
  fprintf fmt "void %a ()"
398
    pp_global_init_name baseNAME
399

    
400
let print_global_clear_prototype fmt baseNAME =
401
  fprintf fmt "void %a ()"
402
    pp_global_clear_name baseNAME
403

    
404
let print_alloc_prototype fmt (name, static) =
405
  fprintf fmt "%a * %a (%a)"
406
    pp_machine_memtype_name name
407
    pp_machine_alloc_name name
408
    (Utils.fprintf_list ~sep:",@ " pp_c_decl_input_var) static
409

    
410
let print_dealloc_prototype fmt name =
411
  fprintf fmt "void %a (%a * _alloc)"
412
    pp_machine_dealloc_name name
413
    pp_machine_memtype_name name
414
    
415
let print_reset_prototype self fmt (name, static) =
416
  fprintf fmt "void %a (@[<v>%a%t%a *%s@])"
417
    pp_machine_reset_name name
418
    (Utils.fprintf_list ~sep:",@ " pp_c_decl_input_var) static
419
    (Utils.pp_final_char_if_non_empty ",@," static) 
420
    pp_machine_memtype_name name
421
    self
422

    
423
let print_init_prototype self fmt (name, static) =
424
  fprintf fmt "void %a (@[<v>%a%t%a *%s@])"
425
    pp_machine_init_name name
426
    (Utils.fprintf_list ~sep:",@ " pp_c_decl_input_var) static
427
    (Utils.pp_final_char_if_non_empty ",@," static) 
428
    pp_machine_memtype_name name
429
    self
430

    
431
let print_clear_prototype self fmt (name, static) =
432
  fprintf fmt "void %a (@[<v>%a%t%a *%s@])"
433
    pp_machine_clear_name name
434
    (Utils.fprintf_list ~sep:",@ " pp_c_decl_input_var) static
435
    (Utils.pp_final_char_if_non_empty ",@," static) 
436
    pp_machine_memtype_name name
437
    self
438

    
439
let print_stateless_prototype fmt (name, inputs, outputs) =
440
  fprintf fmt "void %a (@[<v>@[%a%t@]@,@[%a@]@,@])"
441
    pp_machine_step_name name
442
    (Utils.fprintf_list ~sep:",@ " pp_c_decl_input_var) inputs
443
    (Utils.pp_final_char_if_non_empty ",@ " inputs) 
444
    (Utils.fprintf_list ~sep:",@ " pp_c_decl_output_var) outputs
445

    
446
let print_step_prototype self fmt (name, inputs, outputs) =
447
  fprintf fmt "void %a (@[<v>@[%a%t@]@,@[%a@]%t@[%a *%s@]@])"
448
    pp_machine_step_name name
449
    (Utils.fprintf_list ~sep:",@ " pp_c_decl_input_var) inputs
450
    (Utils.pp_final_char_if_non_empty ",@ " inputs) 
451
    (Utils.fprintf_list ~sep:",@ " pp_c_decl_output_var) outputs
452
    (Utils.pp_final_char_if_non_empty ",@," outputs) 
453
    pp_machine_memtype_name name
454
    self
455

    
456
let print_stateless_C_prototype fmt (name, inputs, outputs) =
457
  let output = 
458
    match outputs with
459
    | [hd] -> hd
460
    | _ -> assert false
461
  in
462
  fprintf fmt "%a %s (@[<v>@[%a@]@,@])"
463
    (pp_basic_c_type ~var_opt:None) output.var_type
464
    name
465
    (Utils.fprintf_list ~sep:",@ " pp_c_decl_input_var) inputs
466
    
467
let print_import_init fmt (Dep (local, basename, _, _)) =
468
  if local then
469
    let baseNAME = file_to_module_name basename in
470
    fprintf fmt "%a();" pp_global_init_name baseNAME
471
  else ()
472

    
473
let print_import_clear fmt (Dep (local, basename, _, _)) =
474
  if local then
475
    let baseNAME = file_to_module_name basename in
476
    fprintf fmt "%a();" pp_global_clear_name baseNAME
477
  else ()
478

    
479
let print_import_prototype fmt (Dep (_, s, _, _)) =
480
  fprintf fmt "#include \"%s.h\"@," s
481

    
482
let print_import_alloc_prototype fmt (Dep (_, s, _, stateful)) =
483
  if stateful then
484
    fprintf fmt "#include \"%s_alloc.h\"@," s
485

    
486
let print_extern_alloc_prototypes fmt (Dep (_,_, header,_)) =
487
  List.iter (fun decl -> match decl.top_decl_desc with
488
  | ImportedNode ind when not ind.nodei_stateless ->
489
    let static = List.filter (fun v -> v.var_dec_const) ind.nodei_inputs in
490
    begin
491
      fprintf fmt "extern %a;@.@." print_alloc_prototype (ind.nodei_id, static);
492
      fprintf fmt "extern %a;@.@." print_dealloc_prototype ind.nodei_id;
493
    end
494
  | _                -> ()
495
  ) header
496

    
497

    
498
let pp_c_main_var_input fmt id =  
499
  fprintf fmt "%s" id.var_id
500

    
501
let pp_c_main_var_output fmt id =
502
  if Types.is_address_type id.var_type
503
  then
504
    fprintf fmt "%s" id.var_id
505
  else
506
    fprintf fmt "&%s" id.var_id
507

    
508
let pp_main_call mname self fmt m (inputs: value_t list) (outputs: var_decl list) =
509
  if fst (get_stateless_status m)
510
  then
511
    fprintf fmt "%a (%a%t%a);"
512
      pp_machine_step_name mname
513
      (Utils.fprintf_list ~sep:", " (pp_c_val self pp_c_main_var_input)) inputs
514
      (Utils.pp_final_char_if_non_empty ", " inputs) 
515
      (Utils.fprintf_list ~sep:", " pp_c_main_var_output) outputs
516
  else
517
    fprintf fmt "%a (%a%t%a%t%s);"
518
      pp_machine_step_name mname
519
      (Utils.fprintf_list ~sep:", " (pp_c_val self pp_c_main_var_input)) inputs
520
      (Utils.pp_final_char_if_non_empty ", " inputs) 
521
      (Utils.fprintf_list ~sep:", " pp_c_main_var_output) outputs
522
      (Utils.pp_final_char_if_non_empty ", " outputs)
523
      self
524

    
525
let pp_c_var m self pp_var fmt var =
526
  if is_memory m var
527
  then
528
    pp_c_val self pp_var fmt (mk_val (StateVar var) var.var_type)
529
  else
530
    pp_c_val self pp_var fmt (mk_val (LocalVar var) var.var_type)
531
  
532

    
533
let pp_array_suffix fmt loop_vars =
534
  Utils.fprintf_list ~sep:"" (fun fmt v -> fprintf fmt "[%s]" v) fmt loop_vars
535

    
536
(* type directed initialization: useless wrt the lustre compilation model,
537
   except for MPFR injection, where values are dynamically allocated
538
*)
539
let pp_initialize m self pp_var fmt var =
540
  let rec aux indices fmt typ =
541
    if Types.is_array_type typ
542
    then
543
      let dim = Types.array_type_dimension typ in
544
      let idx = mk_loop_var m () in
545
      fprintf fmt "@[<v 2>{@,int %s;@,for(%s=0;%s<%a;%s++)@,%a @]@,}"
546
	idx idx idx pp_c_dimension dim idx
547
	(aux (idx::indices)) (Types.array_element_type typ)
548
    else
549
      let indices = List.rev indices in
550
      let pp_var_suffix fmt var =
551
	fprintf fmt "%a%a" (pp_c_var m self pp_var) var pp_array_suffix indices in
552
      Mpfr.pp_inject_init pp_var_suffix fmt var
553
  in
554
  if !Options.mpfr && Types.is_real_type (Types.array_base_type var.var_type)
555
  then
556
    begin
557
      reset_loop_counter ();
558
      aux [] fmt var.var_type
559
    end
560

    
561
let pp_const_initialize pp_var fmt const =
562
  let var = mk_val (LocalVar (Corelang.var_decl_of_const const)) const.const_type in
563
  let rec aux indices value fmt typ =
564
    if Types.is_array_type typ
565
    then
566
      let dim = Types.array_type_dimension typ in
567
      let szl = Utils.enumerate (Dimension.size_const_dimension dim) in
568
      let typ' = Types.array_element_type typ in
569
      let value = match value with
570
	| Const_array ca -> List.nth ca
571
	| _                      -> assert false in
572
      fprintf fmt "%a"
573
	(Utils.fprintf_list ~sep:"@," (fun fmt i -> aux (string_of_int i::indices) (value i) fmt typ')) szl
574
    else
575
      let indices = List.rev indices in
576
      let pp_var_suffix fmt var =
577
	fprintf fmt "%a%a" (pp_c_val "" pp_var) var pp_array_suffix indices in
578
      begin
579
	Mpfr.pp_inject_init pp_var_suffix fmt var;
580
	fprintf fmt "@,";
581
	Mpfr.pp_inject_real pp_var_suffix pp_c_const fmt var value
582
      end
583
  in
584
  if !Options.mpfr && Types.is_real_type (Types.array_base_type const.const_type)
585
  then
586
    begin
587
      reset_loop_counter ();
588
      aux [] const.const_value fmt const.const_type
589
    end
590

    
591
(* type directed clear: useless wrt the lustre compilation model,
592
   except for MPFR injection, where values are dynamically allocated
593
*)
594
let pp_clear m self pp_var fmt var =
595
  let rec aux indices fmt typ =
596
    if Types.is_array_type typ
597
    then
598
      let dim = Types.array_type_dimension typ in
599
      let idx = mk_loop_var m () in
600
      fprintf fmt "@[<v 2>{@,int %s;@,for(%s=0;%s<%a;%s++)@,%a @]@,}"
601
	idx idx idx pp_c_dimension dim idx
602
	(aux (idx::indices)) (Types.array_element_type typ)
603
    else
604
      let indices = List.rev indices in
605
      let pp_var_suffix fmt var =
606
	fprintf fmt "%a%a" (pp_c_var m self pp_var) var pp_array_suffix indices in
607
      Mpfr.pp_inject_clear pp_var_suffix fmt var
608
  in
609
  if !Options.mpfr && Types.is_real_type (Types.array_base_type var.var_type)
610
  then
611
    begin
612
      reset_loop_counter ();
613
      aux [] fmt var.var_type
614
    end
615

    
616
let pp_const_clear pp_var fmt const =
617
  let m = empty_machine in
618
  let var = Corelang.var_decl_of_const const in
619
  let rec aux indices fmt typ =
620
    if Types.is_array_type typ
621
    then
622
      let dim = Types.array_type_dimension typ in
623
      let idx = mk_loop_var m () in
624
      fprintf fmt "@[<v 2>{@,int %s;@,for(%s=0;%s<%a;%s++)@,%a @]@,}"
625
	idx idx idx pp_c_dimension dim idx
626
	(aux (idx::indices)) (Types.array_element_type typ)
627
    else
628
      let indices = List.rev indices in
629
      let pp_var_suffix fmt var =
630
	fprintf fmt "%a%a" (pp_c_var m "" pp_var) var pp_array_suffix indices in
631
      Mpfr.pp_inject_clear pp_var_suffix fmt var 
632
  in
633
  if !Options.mpfr && Types.is_real_type (Types.array_base_type var.var_type)
634
  then
635
    begin
636
      reset_loop_counter ();
637
      aux [] fmt var.var_type
638
    end
639

    
640
let pp_call m self pp_read pp_write fmt i (inputs: Machine_code_types.value_t list) (outputs: var_decl list) =
641
 try (* stateful node instance *)
642
   let (n,_) = List.assoc i m.minstances in
643
   fprintf fmt "%a (%a%t%a%t%s->%s);"
644
     pp_machine_step_name (node_name n)
645
     (Utils.fprintf_list ~sep:", " (pp_c_val self pp_read)) inputs
646
     (Utils.pp_final_char_if_non_empty ", " inputs) 
647
     (Utils.fprintf_list ~sep:", " pp_write) outputs
648
     (Utils.pp_final_char_if_non_empty ", " outputs)
649
     self
650
     i
651
 with Not_found -> (* stateless node instance *)
652
   let (n,_) = List.assoc i m.mcalls in
653
   fprintf fmt "%a (%a%t%a);"
654
     pp_machine_step_name (node_name n)
655
     (Utils.fprintf_list ~sep:", " (pp_c_val self pp_read)) inputs
656
     (Utils.pp_final_char_if_non_empty ", " inputs) 
657
     (Utils.fprintf_list ~sep:", " pp_write) outputs 
658

    
659
let pp_basic_instance_call m self fmt i (inputs: Machine_code_types.value_t list) (outputs: var_decl list) =
660
  pp_call m self (pp_c_var_read m) (pp_c_var_write m) fmt i inputs outputs
661
(*
662
 try (* stateful node instance *)
663
   let (n,_) = List.assoc i m.minstances in
664
   fprintf fmt "%a (%a%t%a%t%s->%s);"
665
     pp_machine_step_name (node_name n)
666
     (Utils.fprintf_list ~sep:", " (pp_c_val self (pp_c_var_read m))) inputs
667
     (Utils.pp_final_char_if_non_empty ", " inputs) 
668
     (Utils.fprintf_list ~sep:", " (pp_c_var_write m)) outputs
669
     (Utils.pp_final_char_if_non_empty ", " outputs)
670
     self
671
     i
672
 with Not_found -> (* stateless node instance *)
673
   let (n,_) = List.assoc i m.mcalls in
674
   fprintf fmt "%a (%a%t%a);"
675
     pp_machine_step_name (node_name n)
676
     (Utils.fprintf_list ~sep:", " (pp_c_val self (pp_c_var_read m))) inputs
677
     (Utils.pp_final_char_if_non_empty ", " inputs) 
678
     (Utils.fprintf_list ~sep:", " (pp_c_var_write m)) outputs 
679
*)
680

    
681
let pp_instance_call m self fmt i (inputs: Machine_code_types.value_t list) (outputs: var_decl list) =
682
  let pp_offset pp_var indices fmt var =
683
    match indices with
684
    | [] -> fprintf fmt "%a" pp_var var
685
    | _  -> fprintf fmt "%a[%a]" pp_var var (Utils.fprintf_list ~sep:"][" pp_print_string) indices in
686
  let rec aux indices fmt typ =
687
    if Types.is_array_type typ
688
    then
689
      let dim = Types.array_type_dimension typ in
690
      let idx = mk_loop_var m () in
691
      fprintf fmt "@[<v 2>{@,int %s;@,for(%s=0;%s<%a;%s++)@,%a @]@,}"
692
	idx idx idx pp_c_dimension dim idx
693
	(aux (idx::indices)) (Types.array_element_type typ)
694
    else
695
      let pp_read  = pp_offset (pp_c_var_read  m) indices in
696
      let pp_write = pp_offset (pp_c_var_write m) indices in
697
      pp_call m self pp_read pp_write fmt i inputs outputs
698
  in
699
  begin
700
    reset_loop_counter ();
701
    aux [] fmt (List.hd inputs).Machine_code_types.value_type
702
  end
703

    
704
  (*** Common functions for main ***)
705

    
706
let print_put_var fmt file_suffix name var_type var_id =
707
  let unclocked_t = Types.unclock_type var_type in
708
  if Types.is_int_type unclocked_t then
709
    fprintf fmt "_put_int(f_out%s, \"%s\", %s)" file_suffix name var_id
710
  else if Types.is_bool_type unclocked_t then
711
    fprintf fmt "_put_bool(f_out%s, \"%s\", %s)" file_suffix name var_id
712
  else if Types.is_real_type unclocked_t then
713
    if !Options.mpfr then
714
      fprintf fmt "_put_double(f_out%s, \"%s\", mpfr_get_d(%s, %s), %i)" file_suffix name var_id (Mpfr.mpfr_rnd ()) !Options.print_prec_double
715
    else
716
      fprintf fmt "_put_double(f_out%s, \"%s\", %s, %i)" file_suffix name var_id !Options.print_prec_double
717
  else
718
    (Format.eprintf "Impossible to print the _put_xx for type %a@.@?" Types.print_ty var_type; assert false)
719

    
720
      
721
let print_get_inputs fmt m =
722
  let pi fmt (id, v', v) =
723

    
724
    let unclocked_t = Types.unclock_type v.var_type in
725
    if Types.is_int_type unclocked_t then
726
      fprintf fmt "%s = _get_int(f_in%i, \"%s\")" v.var_id id v'.var_id
727
    else if Types.is_bool_type unclocked_t then
728
      fprintf fmt "%s = _get_bool(f_in%i, \"%s\")" v.var_id id v'.var_id
729
    else if Types.is_real_type unclocked_t then
730
      if !Options.mpfr then
731
	fprintf fmt "mpfr_set_d(%s, _get_double(f_in%i, \"%s\"), %i)" v.var_id id v'.var_id (Mpfr.mpfr_prec ())
732
      else
733
	fprintf fmt "%s = _get_double(f_in%i, \"%s\")" v.var_id id v'.var_id
734
    else
735
      begin
736
	Global.main_node := !Options.main_node;
737
	Format.eprintf "Code generation error: %a%a@."
738
	  Error.pp_error_msg Error.Main_wrong_kind
739
	  Location.pp_loc v'.var_loc;
740
	raise (Error (v'.var_loc, Error.Main_wrong_kind))
741
      end
742
  in
743
  Utils.List.iteri2 (fun idx v' v ->
744
    fprintf fmt "@ %a;" pi ((idx+1), v', v);
745
  ) m.mname.node_inputs m.mstep.step_inputs
746

    
747

    
748
(* Local Variables: *)
749
(* compile-command:"make -C ../../.." *)
750
(* End: *)