Project

General

Profile

Statistics
| Branch: | Tag: | Revision:

lustrec / src / backends / C / c_backend_common.ml @ 8446bf03

History | View | Annotate | Download (26.3 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
16

    
17

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

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

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

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

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

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

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

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

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

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

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

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

    
110
let rec pp_c_dimension fmt dim =
111
  match dim.Dimension.dim_desc with
112
  | Dimension.Dident id       ->
113
    fprintf fmt "%s" id
114
  | Dimension.Dint i          ->
115
    fprintf fmt "%d" i
116
  | Dimension.Dbool b         ->
117
    fprintf fmt "%B" b
118
  | Dimension.Dite (i, t, e)  ->
119
    fprintf fmt "((%a)?%a:%a)"
120
       pp_c_dimension i pp_c_dimension t pp_c_dimension e
121
 | Dimension.Dappl (f, args) ->
122
     fprintf fmt "%a" (Basic_library.pp_c f pp_c_dimension) args
123
 | Dimension.Dlink dim' -> fprintf fmt "%a" pp_c_dimension dim'
124
 | Dimension.Dvar       -> fprintf fmt "_%s" (Utils.name_of_dimension dim.Dimension.dim_id)
125
 | Dimension.Dunivar    -> fprintf fmt "'%s" (Utils.name_of_dimension dim.Dimension.dim_id)
126

    
127
let is_basic_c_type t =
128
  Types.is_int_type t || Types.is_real_type t || Types.is_bool_type t
129

    
130
let pp_c_basic_type_desc t_desc =
131
  if Types.is_bool_type t_desc then
132
    if !Options.cpp then "bool" else "_Bool"
133
  else if Types.is_int_type t_desc then !Options.int_type
134
  else if Types.is_real_type t_desc then
135
    if !Options.mpfr then Mpfr.mpfr_t else !Options.real_type
136
  else
137
    assert false (* Not a basic C type. Do not handle arrays or pointers *)
138

    
139
let pp_basic_c_type ?(var_opt=None) fmt t =
140
  match var_opt with
141
  | Some v when Machine_types.is_exportable v ->
142
     Machine_types.pp_c_var_type fmt v
143
  | _ ->
144
     fprintf fmt "%s" (pp_c_basic_type_desc t)
145

    
146
let pp_c_type ?(var_opt=None) var_id fmt t =
147
  let rec aux t pp_suffix =
148
    if is_basic_c_type  t then
149
       fprintf fmt "%a %s%a"
150
	 (pp_basic_c_type ~var_opt) t
151
	 var_id
152
	 pp_suffix ()
153
    else
154
      match (Types.repr t).Types.tdesc with
155
      | Types.Tclock t'       -> aux t' pp_suffix
156
      | Types.Tarray (d, t')  ->
157
	 let pp_suffix' fmt () = fprintf fmt "%a[%a]" pp_suffix () pp_c_dimension d in
158
	 aux t' pp_suffix'
159
      | Types.Tstatic (_, t') -> fprintf fmt "const "; aux t' pp_suffix
160
      | Types.Tconst ty       -> fprintf fmt "%s %s" ty var_id
161
      | Types.Tarrow (_, _)   -> fprintf fmt "void (*%s)()" var_id
162
      | _                     -> eprintf "internal error: C_backend_common.pp_c_type %a@." Types.print_ty t; assert false
163
  in aux t (fun fmt () -> ())
164
(*
165
let rec pp_c_initialize fmt t = 
166
  match (Types.repr t).Types.tdesc with
167
  | Types.Tint -> pp_print_string fmt "0"
168
  | Types.Tclock t' -> pp_c_initialize fmt t'
169
  | Types.Tbool -> pp_print_string fmt "0" 
170
  | Types.Treal when not !Options.mpfr -> pp_print_string fmt "0."
171
  | Types.Tarray (d, t') when Dimension.is_dimension_const d ->
172
    fprintf fmt "{%a}"
173
      (Utils.fprintf_list ~sep:"," (fun fmt _ -> pp_c_initialize fmt t'))
174
      (Utils.duplicate 0 (Dimension.size_const_dimension d))
175
  | _ -> assert false
176
 *)
177
let pp_c_tag fmt t =
178
 pp_print_string fmt (if t = tag_true then "1" else if t = tag_false then "0" else t)
179

    
180

    
181
(* Prints a constant value *)
182
let rec pp_c_const fmt c =
183
  match c with
184
    | Const_int i     -> pp_print_int fmt i
185
    | Const_real (c,e,s)-> pp_print_string fmt s (* Format.fprintf fmt "%ie%i" c e*)
186
    (* | Const_float r   -> pp_print_float fmt r *)
187
    | Const_tag t     -> pp_c_tag fmt t
188
    | Const_array ca  -> fprintf fmt "{%a }" (Utils.fprintf_list ~sep:", " pp_c_const) ca
189
    | Const_struct fl -> fprintf fmt "{%a }" (Utils.fprintf_list ~sep:", " (fun fmt (f, c) -> pp_c_const fmt c)) fl
190
    | Const_string _ -> assert false (* string occurs in annotations not in C *)
191

    
192
(* Prints a value expression [v], with internal function calls only.
193
   [pp_var] is a printer for variables (typically [pp_c_var_read]),
194
   but an offset suffix may be added for array variables
195
*)
196
let rec pp_c_val self pp_var fmt v =
197
  let open Machine_code_types in
198
  match v.value_desc with
199
  | Cst c         -> pp_c_const fmt c
200
  | Array vl      -> fprintf fmt "{%a}" (Utils.fprintf_list ~sep:", " (pp_c_val self pp_var)) vl
201
  | Access (t, i) -> fprintf fmt "%a[%a]" (pp_c_val self pp_var) t (pp_c_val self pp_var) i
202
  | Power (v, n)  -> (Format.eprintf "internal error: C_backend_common.pp_c_val %a@." pp_val v; assert false)
203
  | LocalVar v    -> pp_var fmt v
204
  | StateVar v    ->
205
    (* array memory vars are represented by an indirection to a local var with the right type,
206
       in order to avoid casting everywhere. *)
207
    if Types.is_array_type v.var_type && not (Types.is_real_type v.var_type && !Options.mpfr)
208
    then fprintf fmt "%a" pp_var v
209
    else fprintf fmt "%s->_reg.%a" self pp_var v
210
  | Fun (n, vl)   -> Basic_library.pp_c n (pp_c_val self pp_var) fmt vl
211

    
212
(* Access to the value of a variable:
213
   - if it's not a scalar output, then its name is enough
214
   - otherwise, dereference it (it has been declared as a pointer,
215
     despite its scalar Lustre type)
216
   - moreover, dereference memory array variables.
217
*)
218
let pp_c_var_read m fmt id =
219
  (* mpfr_t is a static array, not treated as general arrays *)
220
  if Types.is_address_type id.var_type
221
  then
222
    if is_memory m id && not (Types.is_real_type id.var_type && !Options.mpfr)
223
    then fprintf fmt "(*%s)" id.var_id
224
    else fprintf fmt "%s" id.var_id
225
  else
226
    if is_output m id
227
    then fprintf fmt "*%s" id.var_id
228
    else fprintf fmt "%s" id.var_id
229

    
230
(* Addressable value of a variable, the one that is passed around in calls:
231
   - if it's not a scalar non-output, then its name is enough
232
   - otherwise, reference it (it must be passed as a pointer,
233
     despite its scalar Lustre type)
234
*)
235
let pp_c_var_write m fmt id =
236
  if Types.is_address_type id.var_type
237
  then
238
    fprintf fmt "%s" id.var_id
239
  else
240
    if is_output m id
241
    then
242
      fprintf fmt "%s" id.var_id
243
    else
244
      fprintf fmt "&%s" id.var_id
245

    
246
(* Declaration of an input variable:
247
   - if its type is array/matrix/etc, then declare it as a mere pointer,
248
     in order to cope with unknown/parametric array dimensions, 
249
     as it is the case for generics
250
*)
251
let pp_c_decl_input_var fmt id =
252
  if !Options.ansi && Types.is_address_type id.var_type
253
  then pp_c_type ~var_opt:(Some id) (sprintf "(*%s)" id.var_id) fmt (Types.array_base_type id.var_type)
254
  else pp_c_type ~var_opt:(Some id) id.var_id fmt id.var_type
255

    
256
(* Declaration of an output variable:
257
   - if its type is scalar, then pass its address
258
   - if its type is array/matrix/struct/etc, then declare it as a mere pointer,
259
     in order to cope with unknown/parametric array dimensions, 
260
     as it is the case for generics
261
*)
262
let pp_c_decl_output_var fmt id =
263
  if (not !Options.ansi) && Types.is_address_type id.var_type
264
  then pp_c_type  ~var_opt:(Some id)                  id.var_id  fmt id.var_type
265
  else pp_c_type  ~var_opt:(Some id) (sprintf "(*%s)" id.var_id) fmt (Types.array_base_type id.var_type)
266

    
267
(* Declaration of a local/mem variable:
268
   - if it's an array/matrix/etc, its size(s) should be
269
     known in order to statically allocate memory, 
270
     so we print the full type
271
*)
272
let pp_c_decl_local_var m fmt id =
273
  if id.var_dec_const
274
  then
275
    Format.fprintf fmt "%a = %a"
276
      (pp_c_type  ~var_opt:(Some id) id.var_id) id.var_type
277
      (pp_c_val "" (pp_c_var_read m)) (get_const_assign m id)
278
  else
279
    Format.fprintf fmt "%a"
280
      (pp_c_type  ~var_opt:(Some id) id.var_id) id.var_type
281

    
282
let pp_c_decl_array_mem self fmt id =
283
  fprintf fmt "%a = (%a) (%s->_reg.%s)"
284
    (pp_c_type (sprintf "(*%s)" id.var_id)) id.var_type
285
    (pp_c_type "(*)") id.var_type
286
    self
287
    id.var_id
288

    
289
(* Declaration of a struct variable:
290
   - if it's an array/matrix/etc, we declare it as a pointer
291
*)
292
let pp_c_decl_struct_var fmt id =
293
  if Types.is_array_type id.var_type
294
  then pp_c_type (sprintf "(*%s)" id.var_id) fmt (Types.array_base_type id.var_type)
295
  else pp_c_type                  id.var_id  fmt id.var_type
296

    
297
let pp_c_decl_instance_var fmt (name, (node, static)) = 
298
  fprintf fmt "%a *%s" pp_machine_memtype_name (node_name node) name
299

    
300
let pp_c_checks self fmt m =
301
  Utils.fprintf_list ~sep:"" 
302
    (fun fmt (loc, check) -> 
303
      fprintf fmt 
304
	"@[<v>%a@,assert (%a);@]@," 
305
	Location.pp_c_loc loc
306
	(pp_c_val self (pp_c_var_read m)) check
307
    ) 
308
    fmt 
309
    m.mstep.step_checks
310

    
311
(********************************************************************************************)
312
(*                       Struct Printing functions                                          *)
313
(********************************************************************************************)
314

    
315
let pp_registers_struct fmt m =
316
  if m.mmemory <> []
317
  then
318
    fprintf fmt "@[%a {@[<v>%a;@ @]}@] _reg; "
319
      pp_machine_regtype_name m.mname.node_id
320
      (Utils.fprintf_list ~sep:";@ " pp_c_decl_struct_var) m.mmemory
321
  else
322
    ()
323

    
324
let print_machine_struct fmt m =
325
  if fst (get_stateless_status m) then
326
    begin
327
    end
328
  else
329
    begin
330
      (* Define struct *)
331
      fprintf fmt "@[%a {@[<v>%a%t%a%t@]};@]@."
332
	pp_machine_memtype_name m.mname.node_id
333
	pp_registers_struct m
334
	(Utils.pp_final_char_if_non_empty "@ " m.mmemory)
335
	(Utils.fprintf_list ~sep:";@ " pp_c_decl_instance_var) m.minstances
336
	(Utils.pp_final_char_if_non_empty ";@ " m.minstances)
337
    end
338

    
339
let print_machine_struct_from_header fmt inode =
340
  if inode.nodei_stateless then
341
    begin
342
    end
343
  else
344
    begin
345
      (* Declare struct *)
346
      fprintf fmt "@[%a;@]@."
347
	pp_machine_memtype_name inode.nodei_id
348
    end
349

    
350
(********************************************************************************************)
351
(*                      Prototype Printing functions                                        *)
352
(********************************************************************************************)
353

    
354
let print_global_init_prototype fmt baseNAME =
355
  fprintf fmt "void %a ()"
356
    pp_global_init_name baseNAME
357

    
358
let print_global_clear_prototype fmt baseNAME =
359
  fprintf fmt "void %a ()"
360
    pp_global_clear_name baseNAME
361

    
362
let print_alloc_prototype fmt (name, static) =
363
  fprintf fmt "%a * %a (%a)"
364
    pp_machine_memtype_name name
365
    pp_machine_alloc_name name
366
    (Utils.fprintf_list ~sep:",@ " pp_c_decl_input_var) static
367

    
368
let print_dealloc_prototype fmt name =
369
  fprintf fmt "void %a (%a * _alloc)"
370
    pp_machine_dealloc_name name
371
    pp_machine_memtype_name name
372
    
373
let print_reset_prototype self fmt (name, static) =
374
  fprintf fmt "void %a (@[<v>%a%t%a *%s@])"
375
    pp_machine_reset_name name
376
    (Utils.fprintf_list ~sep:",@ " pp_c_decl_input_var) static
377
    (Utils.pp_final_char_if_non_empty ",@," static) 
378
    pp_machine_memtype_name name
379
    self
380

    
381
let print_init_prototype self fmt (name, static) =
382
  fprintf fmt "void %a (@[<v>%a%t%a *%s@])"
383
    pp_machine_init_name name
384
    (Utils.fprintf_list ~sep:",@ " pp_c_decl_input_var) static
385
    (Utils.pp_final_char_if_non_empty ",@," static) 
386
    pp_machine_memtype_name name
387
    self
388

    
389
let print_clear_prototype self fmt (name, static) =
390
  fprintf fmt "void %a (@[<v>%a%t%a *%s@])"
391
    pp_machine_clear_name name
392
    (Utils.fprintf_list ~sep:",@ " pp_c_decl_input_var) static
393
    (Utils.pp_final_char_if_non_empty ",@," static) 
394
    pp_machine_memtype_name name
395
    self
396

    
397
let print_stateless_prototype fmt (name, inputs, outputs) =
398
  fprintf fmt "void %a (@[<v>@[%a%t@]@,@[%a@]@,@])"
399
    pp_machine_step_name name
400
    (Utils.fprintf_list ~sep:",@ " pp_c_decl_input_var) inputs
401
    (Utils.pp_final_char_if_non_empty ",@ " inputs) 
402
    (Utils.fprintf_list ~sep:",@ " pp_c_decl_output_var) outputs
403

    
404
let print_step_prototype self fmt (name, inputs, outputs) =
405
  fprintf fmt "void %a (@[<v>@[%a%t@]@,@[%a@]%t@[%a *%s@]@])"
406
    pp_machine_step_name name
407
    (Utils.fprintf_list ~sep:",@ " pp_c_decl_input_var) inputs
408
    (Utils.pp_final_char_if_non_empty ",@ " inputs) 
409
    (Utils.fprintf_list ~sep:",@ " pp_c_decl_output_var) outputs
410
    (Utils.pp_final_char_if_non_empty ",@," outputs) 
411
    pp_machine_memtype_name name
412
    self
413

    
414
let print_stateless_C_prototype fmt (name, inputs, outputs) =
415
  let output = 
416
    match outputs with
417
    | [hd] -> hd
418
    | _ -> assert false
419
  in
420
  fprintf fmt "%a %s (@[<v>@[%a@]@,@])"
421
    (pp_basic_c_type ~var_opt:None) output.var_type
422
    name
423
    (Utils.fprintf_list ~sep:",@ " pp_c_decl_input_var) inputs
424
    
425
let print_import_init fmt (Dep (local, basename, _, _)) =
426
  if local then
427
    let baseNAME = file_to_module_name basename in
428
    fprintf fmt "%a();" pp_global_init_name baseNAME
429
  else ()
430

    
431
let print_import_clear fmt (Dep (local, basename, _, _)) =
432
  if local then
433
    let baseNAME = file_to_module_name basename in
434
    fprintf fmt "%a();" pp_global_clear_name baseNAME
435
  else ()
436

    
437
let print_import_prototype fmt (Dep (_, s, _, _)) =
438
  fprintf fmt "#include \"%s.h\"@," s
439

    
440
let print_import_alloc_prototype fmt (Dep (_, s, _, stateful)) =
441
  if stateful then
442
    fprintf fmt "#include \"%s_alloc.h\"@," s
443

    
444
let print_extern_alloc_prototypes fmt (Dep (_,_, header,_)) =
445
  List.iter (fun decl -> match decl.top_decl_desc with
446
  | ImportedNode ind when not ind.nodei_stateless ->
447
    let static = List.filter (fun v -> v.var_dec_const) ind.nodei_inputs in
448
    begin
449
      fprintf fmt "extern %a;@.@." print_alloc_prototype (ind.nodei_id, static);
450
      fprintf fmt "extern %a;@.@." print_dealloc_prototype ind.nodei_id;
451
    end
452
  | _                -> ()
453
  ) header
454

    
455

    
456
let pp_c_main_var_input fmt id =  
457
  fprintf fmt "%s" id.var_id
458

    
459
let pp_c_main_var_output fmt id =
460
  if Types.is_address_type id.var_type
461
  then
462
    fprintf fmt "%s" id.var_id
463
  else
464
    fprintf fmt "&%s" id.var_id
465

    
466
let pp_main_call mname self fmt m (inputs: Machine_code_types.value_t list) (outputs: var_decl list) =
467
  if fst (get_stateless_status m)
468
  then
469
    fprintf fmt "%a (%a%t%a);"
470
      pp_machine_step_name mname
471
      (Utils.fprintf_list ~sep:", " (pp_c_val self pp_c_main_var_input)) inputs
472
      (Utils.pp_final_char_if_non_empty ", " inputs) 
473
      (Utils.fprintf_list ~sep:", " pp_c_main_var_output) outputs
474
  else
475
    fprintf fmt "%a (%a%t%a%t%s);"
476
      pp_machine_step_name mname
477
      (Utils.fprintf_list ~sep:", " (pp_c_val self pp_c_main_var_input)) inputs
478
      (Utils.pp_final_char_if_non_empty ", " inputs) 
479
      (Utils.fprintf_list ~sep:", " pp_c_main_var_output) outputs
480
      (Utils.pp_final_char_if_non_empty ", " outputs)
481
      self
482

    
483
let pp_c_var m self pp_var fmt var =
484
  let open Machine_code_types in
485
  if is_memory m var
486
  then
487
    pp_c_val self pp_var fmt (mk_val (StateVar var) var.var_type)
488
  else
489
    pp_c_val self pp_var fmt (mk_val (LocalVar var) var.var_type)
490
  
491

    
492
let pp_array_suffix fmt loop_vars =
493
  Utils.fprintf_list ~sep:"" (fun fmt v -> fprintf fmt "[%s]" v) fmt loop_vars
494

    
495
(* type directed initialization: useless wrt the lustre compilation model,
496
   except for MPFR injection, where values are dynamically allocated
497
*)
498
let pp_initialize m self pp_var fmt var =
499
  let rec aux indices fmt typ =
500
    if Types.is_array_type typ
501
    then
502
      let dim = Types.array_type_dimension typ in
503
      let idx = mk_loop_var m () in
504
      fprintf fmt "@[<v 2>{@,int %s;@,for(%s=0;%s<%a;%s++)@,%a @]@,}"
505
	idx idx idx pp_c_dimension dim idx
506
	(aux (idx::indices)) (Types.array_element_type typ)
507
    else
508
      let indices = List.rev indices in
509
      let pp_var_suffix fmt var =
510
	fprintf fmt "%a%a" (pp_c_var m self pp_var) var pp_array_suffix indices in
511
      Mpfr.pp_inject_init pp_var_suffix fmt var
512
  in
513
  if !Options.mpfr && Types.is_real_type (Types.array_base_type var.var_type)
514
  then
515
    begin
516
      reset_loop_counter ();
517
      aux [] fmt var.var_type
518
    end
519

    
520
let pp_const_initialize pp_var fmt const =
521
  let open Machine_code_types in
522
  let var = mk_val (LocalVar (Corelang.var_decl_of_const const)) const.const_type in
523
  let rec aux indices value fmt typ =
524
    if Types.is_array_type typ
525
    then
526
      let dim = Types.array_type_dimension typ in
527
      let szl = Utils.enumerate (Dimension.size_const_dimension dim) in
528
      let typ' = Types.array_element_type typ in
529
      let value = match value with
530
	| Const_array ca -> List.nth ca
531
	| _                      -> assert false in
532
      fprintf fmt "%a"
533
	(Utils.fprintf_list ~sep:"@," (fun fmt i -> aux (string_of_int i::indices) (value i) fmt typ')) szl
534
    else
535
      let indices = List.rev indices in
536
      let pp_var_suffix fmt var =
537
	fprintf fmt "%a%a" (pp_c_val "" pp_var) var pp_array_suffix indices in
538
      begin
539
	Mpfr.pp_inject_init pp_var_suffix fmt var;
540
	fprintf fmt "@,";
541
	Mpfr.pp_inject_real pp_var_suffix pp_c_const fmt var value
542
      end
543
  in
544
  if !Options.mpfr && Types.is_real_type (Types.array_base_type const.const_type)
545
  then
546
    begin
547
      reset_loop_counter ();
548
      aux [] const.const_value fmt const.const_type
549
    end
550

    
551
(* type directed clear: useless wrt the lustre compilation model,
552
   except for MPFR injection, where values are dynamically allocated
553
*)
554
let pp_clear m self pp_var fmt var =
555
  let rec aux indices fmt typ =
556
    if Types.is_array_type typ
557
    then
558
      let dim = Types.array_type_dimension typ in
559
      let idx = mk_loop_var m () in
560
      fprintf fmt "@[<v 2>{@,int %s;@,for(%s=0;%s<%a;%s++)@,%a @]@,}"
561
	idx idx idx pp_c_dimension dim idx
562
	(aux (idx::indices)) (Types.array_element_type typ)
563
    else
564
      let indices = List.rev indices in
565
      let pp_var_suffix fmt var =
566
	fprintf fmt "%a%a" (pp_c_var m self pp_var) var pp_array_suffix indices in
567
      Mpfr.pp_inject_clear pp_var_suffix fmt var
568
  in
569
  if !Options.mpfr && Types.is_real_type (Types.array_base_type var.var_type)
570
  then
571
    begin
572
      reset_loop_counter ();
573
      aux [] fmt var.var_type
574
    end
575

    
576
let pp_const_clear pp_var fmt const =
577
  let m = Machine_code.empty_machine in
578
  let var = Corelang.var_decl_of_const const in
579
  let rec aux indices fmt typ =
580
    if Types.is_array_type typ
581
    then
582
      let dim = Types.array_type_dimension typ in
583
      let idx = mk_loop_var m () in
584
      fprintf fmt "@[<v 2>{@,int %s;@,for(%s=0;%s<%a;%s++)@,%a @]@,}"
585
	idx idx idx pp_c_dimension dim idx
586
	(aux (idx::indices)) (Types.array_element_type typ)
587
    else
588
      let indices = List.rev indices in
589
      let pp_var_suffix fmt var =
590
	fprintf fmt "%a%a" (pp_c_var m "" pp_var) var pp_array_suffix indices in
591
      Mpfr.pp_inject_clear pp_var_suffix fmt var 
592
  in
593
  if !Options.mpfr && Types.is_real_type (Types.array_base_type var.var_type)
594
  then
595
    begin
596
      reset_loop_counter ();
597
      aux [] fmt var.var_type
598
    end
599

    
600
let pp_call m self pp_read pp_write fmt i (inputs: Machine_code_types.value_t list) (outputs: var_decl list) =
601
 try (* stateful node instance *)
602
   let (n,_) = List.assoc i m.minstances in
603
   fprintf fmt "%a (%a%t%a%t%s->%s);"
604
     pp_machine_step_name (node_name n)
605
     (Utils.fprintf_list ~sep:", " (pp_c_val self pp_read)) inputs
606
     (Utils.pp_final_char_if_non_empty ", " inputs) 
607
     (Utils.fprintf_list ~sep:", " pp_write) outputs
608
     (Utils.pp_final_char_if_non_empty ", " outputs)
609
     self
610
     i
611
 with Not_found -> (* stateless node instance *)
612
   let (n,_) = List.assoc i m.mcalls in
613
   fprintf fmt "%a (%a%t%a);"
614
     pp_machine_step_name (node_name n)
615
     (Utils.fprintf_list ~sep:", " (pp_c_val self pp_read)) inputs
616
     (Utils.pp_final_char_if_non_empty ", " inputs) 
617
     (Utils.fprintf_list ~sep:", " pp_write) outputs 
618

    
619
let pp_basic_instance_call m self fmt i (inputs: Machine_code_types.value_t list) (outputs: var_decl list) =
620
  pp_call m self (pp_c_var_read m) (pp_c_var_write m) fmt i inputs outputs
621
(*
622
 try (* stateful node instance *)
623
   let (n,_) = List.assoc i m.minstances in
624
   fprintf fmt "%a (%a%t%a%t%s->%s);"
625
     pp_machine_step_name (node_name n)
626
     (Utils.fprintf_list ~sep:", " (pp_c_val self (pp_c_var_read m))) inputs
627
     (Utils.pp_final_char_if_non_empty ", " inputs) 
628
     (Utils.fprintf_list ~sep:", " (pp_c_var_write m)) outputs
629
     (Utils.pp_final_char_if_non_empty ", " outputs)
630
     self
631
     i
632
 with Not_found -> (* stateless node instance *)
633
   let (n,_) = List.assoc i m.mcalls in
634
   fprintf fmt "%a (%a%t%a);"
635
     pp_machine_step_name (node_name n)
636
     (Utils.fprintf_list ~sep:", " (pp_c_val self (pp_c_var_read m))) inputs
637
     (Utils.pp_final_char_if_non_empty ", " inputs) 
638
     (Utils.fprintf_list ~sep:", " (pp_c_var_write m)) outputs 
639
*)
640

    
641
let pp_instance_call m self fmt i (inputs: Machine_code_types.value_t list) (outputs: var_decl list) =
642
  let pp_offset pp_var indices fmt var =
643
    match indices with
644
    | [] -> fprintf fmt "%a" pp_var var
645
    | _  -> fprintf fmt "%a[%a]" pp_var var (Utils.fprintf_list ~sep:"][" pp_print_string) indices in
646
  let rec aux indices fmt typ =
647
    if Types.is_array_type typ
648
    then
649
      let dim = Types.array_type_dimension typ in
650
      let idx = mk_loop_var m () in
651
      fprintf fmt "@[<v 2>{@,int %s;@,for(%s=0;%s<%a;%s++)@,%a @]@,}"
652
	idx idx idx pp_c_dimension dim idx
653
	(aux (idx::indices)) (Types.array_element_type typ)
654
    else
655
      let pp_read  = pp_offset (pp_c_var_read  m) indices in
656
      let pp_write = pp_offset (pp_c_var_write m) indices in
657
      pp_call m self pp_read pp_write fmt i inputs outputs
658
  in
659
  begin
660
    reset_loop_counter ();
661
    aux [] fmt (List.hd inputs).Machine_code_types.value_type
662
  end
663

    
664
  (*** Common functions for main ***)
665

    
666
let print_put_var fmt file_suffix name var_type var_id =
667
  let unclocked_t = Types.unclock_type var_type in
668
  if Types.is_int_type unclocked_t then
669
    fprintf fmt "_put_int(f_out%s, \"%s\", %s)" file_suffix name var_id
670
  else if Types.is_bool_type unclocked_t then
671
    fprintf fmt "_put_bool(f_out%s, \"%s\", %s)" file_suffix name var_id
672
  else if Types.is_real_type unclocked_t then
673
    if !Options.mpfr then
674
      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
675
    else
676
      fprintf fmt "_put_double(f_out%s, \"%s\", %s, %i)" file_suffix name var_id !Options.print_prec_double
677
  else
678
    (Format.eprintf "Impossible to print the _put_xx for type %a@.@?" Types.print_ty var_type; assert false)
679

    
680
      
681
let print_get_inputs fmt m =
682
  let pi fmt (id, v', v) =
683

    
684
    let unclocked_t = Types.unclock_type v.var_type in
685
    if Types.is_int_type unclocked_t then
686
      fprintf fmt "%s = _get_int(f_in%i, \"%s\")" v.var_id id v'.var_id
687
    else if Types.is_bool_type unclocked_t then
688
      fprintf fmt "%s = _get_bool(f_in%i, \"%s\")" v.var_id id v'.var_id
689
    else if Types.is_real_type unclocked_t then
690
      if !Options.mpfr then
691
	fprintf fmt "mpfr_set_d(%s, _get_double(f_in%i, \"%s\"), %i)" v.var_id id v'.var_id (Mpfr.mpfr_prec ())
692
      else
693
	fprintf fmt "%s = _get_double(f_in%i, \"%s\")" v.var_id id v'.var_id
694
    else
695
      begin
696
	Global.main_node := !Options.main_node;
697
	Format.eprintf "Code generation error: %a%a@."
698
	  Error.pp_error_msg Error.Main_wrong_kind
699
	  Location.pp_loc v'.var_loc;
700
	raise (Error (v'.var_loc, Error.Main_wrong_kind))
701
      end
702
  in
703
  Utils.List.iteri2 (fun idx v' v ->
704
    fprintf fmt "@ %a;" pi ((idx+1), v', v);
705
  ) m.mname.node_inputs m.mstep.step_inputs
706

    
707

    
708
(* Local Variables: *)
709
(* compile-command:"make -C ../../.." *)
710
(* End: *)