Project

General

Profile

Statistics
| Branch: | Tag: | Revision:

lustrec / src / backends / C / c_backend_common.ml @ 14da5302

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 mod_C b < 0 ? abs(b) : 0) *)
114
    Format.fprintf fmt "((%a %% %a) + ((%a %% %a) < 0?(abs(%a)):0))"
115
      pp_val v1 pp_val v2
116
      pp_val v1 pp_val v2
117
      pp_val v2
118
  else (* Regular behavior: printing a % *)
119
    Format.fprintf fmt "(%a %% %a)" pp_val v1 pp_val v2
120

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

    
155

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

    
173
let is_basic_c_type t =
174
  Types.is_int_type t || Types.is_real_type t || Types.is_bool_type t
175

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

    
185
let pp_basic_c_type ?(var_opt=None) fmt t =
186
  match var_opt with
187
  | Some v when Machine_types.is_exportable v ->
188
     Machine_types.pp_c_var_type fmt v
189
  | _ ->
190
     fprintf fmt "%s" (pp_c_basic_type_desc t)
191

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

    
226

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

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

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

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

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

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

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

    
328
let pp_c_decl_array_mem self fmt id =
329
  fprintf fmt "%a = (%a) (%s->_reg.%s)"
330
    (pp_c_type (sprintf "(*%s)" id.var_id)) id.var_type
331
    (pp_c_type "(*)") id.var_type
332
    self
333
    id.var_id
334

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

    
343
let pp_c_decl_instance_var fmt (name, (node, static)) = 
344
  fprintf fmt "%a *%s" pp_machine_memtype_name (node_name node) name
345

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

    
357
(********************************************************************************************)
358
(*                       Struct Printing functions                                          *)
359
(********************************************************************************************)
360

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

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

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

    
396
(********************************************************************************************)
397
(*                      Prototype Printing functions                                        *)
398
(********************************************************************************************)
399

    
400
let print_global_init_prototype fmt baseNAME =
401
  fprintf fmt "void %a ()"
402
    pp_global_init_name baseNAME
403

    
404
let print_global_clear_prototype fmt baseNAME =
405
  fprintf fmt "void %a ()"
406
    pp_global_clear_name baseNAME
407

    
408
let print_alloc_prototype fmt (name, static) =
409
  fprintf fmt "%a * %a (%a)"
410
    pp_machine_memtype_name name
411
    pp_machine_alloc_name name
412
    (Utils.fprintf_list ~sep:",@ " pp_c_decl_input_var) static
413

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

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

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

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

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

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

    
477
let print_import_clear fmt (Dep (local, basename, _, _)) =
478
  if local then
479
    let baseNAME = file_to_module_name basename in
480
    fprintf fmt "%a();" pp_global_clear_name baseNAME
481
  else ()
482

    
483
let print_import_prototype fmt (Dep (_, s, _, _)) =
484
  fprintf fmt "#include \"%s.h\"@," s
485

    
486
let print_import_alloc_prototype fmt (Dep (_, s, _, stateful)) =
487
  if stateful then
488
    fprintf fmt "#include \"%s_alloc.h\"@," s
489

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

    
501

    
502
let pp_c_main_var_input fmt id =  
503
  fprintf fmt "%s" id.var_id
504

    
505
let pp_c_main_var_output fmt id =
506
  if Types.is_address_type id.var_type
507
  then
508
    fprintf fmt "%s" id.var_id
509
  else
510
    fprintf fmt "&%s" id.var_id
511

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

    
529
let pp_c_var m self pp_var fmt var =
530
  if is_memory m var
531
  then
532
    pp_c_val self pp_var fmt (mk_val (StateVar var) var.var_type)
533
  else
534
    pp_c_val self pp_var fmt (mk_val (LocalVar var) var.var_type)
535
  
536

    
537
let pp_array_suffix fmt loop_vars =
538
  Utils.fprintf_list ~sep:"" (fun fmt v -> fprintf fmt "[%s]" v) fmt loop_vars
539

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

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

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

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

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

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

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

    
708
  (*** Common functions for main ***)
709

    
710
let print_put_var fmt file_suffix name var_type var_id =
711
  let unclocked_t = Types.unclock_type var_type in
712
  if Types.is_int_type unclocked_t then
713
    fprintf fmt "_put_int(f_out%s, \"%s\", %s)" file_suffix name var_id
714
  else if Types.is_bool_type unclocked_t then
715
    fprintf fmt "_put_bool(f_out%s, \"%s\", %s)" file_suffix name var_id
716
  else if Types.is_real_type unclocked_t then
717
    if !Options.mpfr then
718
      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
719
    else
720
      fprintf fmt "_put_double(f_out%s, \"%s\", %s, %i)" file_suffix name var_id !Options.print_prec_double
721
  else
722
    (Format.eprintf "Impossible to print the _put_xx for type %a@.@?" Types.print_ty var_type; assert false)
723

    
724
      
725
let print_get_inputs fmt m =
726
  let pi fmt (id, v', v) =
727

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

    
751

    
752
(* Local Variables: *)
753
(* compile-command:"make -C ../../.." *)
754
(* End: *)