Project

General

Profile

Statistics
| Branch: | Tag: | Revision:

lustrec / src / backends / C / c_backend_common.ml @ 52c5ba00

History | View | Annotate | Download (23.8 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 LustreSpec
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 file_to_module_name basename =
27
  let baseNAME = String.uppercase basename in
28
  let baseNAME = Str.global_replace (Str.regexp "\\.\\|\\ ") "_" baseNAME in
29
  baseNAME
30

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

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

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

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

    
65
(* counter for loop variable creation *)
66
let loop_cpt = ref (-1)
67

    
68
let reset_loop_counter () =
69
 loop_cpt := -1
70

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

    
81
let reset_addr_counter () =
82
 addr_cpt := -1
83

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

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

    
122
let is_basic_c_type t =
123
  match (Types.repr t).Types.tdesc with
124
  | Types.Tbool | Types.Treal | Types.Tint  -> true
125
  | _                                       -> false
126

    
127
let pp_c_basic_type_desc t_dsec =
128
  match (t_dsec) with
129
  | Types.Tbool when !Options.cpp  -> "bool"
130
  | Types.Tbool                    -> "_Bool"
131
  | Types.Tint                     -> !Options.int_type
132
  | Types.Treal when !Options.mpfr -> Mpfr.mpfr_t
133
  | Types.Treal                    -> !Options.real_type
134
  | _ -> assert false (* Not a basic C type. Do not handle arrays or pointers *)
135

    
136
let pp_basic_c_type fmt t = fprintf fmt "%s" (pp_c_basic_type_desc (Types.repr t).Types.tdesc)
137

    
138
let pp_c_type var fmt t =
139
  let rec aux t pp_suffix =
140
    match (Types.repr t).Types.tdesc with
141
    | Types.Tclock t'       -> aux t' pp_suffix
142
    | Types.Tbool | Types.Tint | Types.Treal
143
                            -> fprintf fmt "%a %s%a" pp_basic_c_type t var pp_suffix ()
144
    | Types.Tarray (d, t')  ->
145
      let pp_suffix' fmt () = fprintf fmt "%a[%a]" pp_suffix () pp_c_dimension d in
146
      aux t' pp_suffix'
147
    | Types.Tstatic (_, t') -> fprintf fmt "const "; aux t' pp_suffix
148
    | Types.Tconst ty       -> fprintf fmt "%s %s" ty var
149
    | Types.Tarrow (_, _)   -> fprintf fmt "void (*%s)()" var
150
    | _                     -> eprintf "internal error: C_backend_common.pp_c_type %a@." Types.print_ty t; assert false
151
  in aux t (fun fmt () -> ())
152
(*
153
let rec pp_c_initialize fmt t = 
154
  match (Types.repr t).Types.tdesc with
155
  | Types.Tint -> pp_print_string fmt "0"
156
  | Types.Tclock t' -> pp_c_initialize fmt t'
157
  | Types.Tbool -> pp_print_string fmt "0" 
158
  | Types.Treal when not !Options.mpfr -> pp_print_string fmt "0."
159
  | Types.Tarray (d, t') when Dimension.is_dimension_const d ->
160
    fprintf fmt "{%a}"
161
      (Utils.fprintf_list ~sep:"," (fun fmt _ -> pp_c_initialize fmt t'))
162
      (Utils.duplicate 0 (Dimension.size_const_dimension d))
163
  | _ -> assert false
164
 *)
165
let pp_c_tag fmt t =
166
 pp_print_string fmt (if t = tag_true then "1" else if t = tag_false then "0" else t)
167

    
168
(* Prints a constant value *)
169
let rec pp_c_const fmt c =
170
  match c with
171
    | Const_int i     -> pp_print_int fmt i
172
    | Const_real (c,e,s)-> pp_print_string fmt s (* Format.fprintf fmt "%ie%i" c e*)
173
    (* | Const_float r   -> pp_print_float fmt r *)
174
    | Const_tag t     -> pp_c_tag fmt t
175
    | Const_array ca  -> fprintf fmt "{%a }" (Utils.fprintf_list ~sep:", " pp_c_const) ca
176
    | Const_struct fl -> fprintf fmt "{%a }" (Utils.fprintf_list ~sep:", " (fun fmt (f, c) -> pp_c_const fmt c)) fl
177
    | Const_string _ -> assert false (* string occurs in annotations not in C *)
178

    
179
(* Prints a value expression [v], with internal function calls only.
180
   [pp_var] is a printer for variables (typically [pp_c_var_read]),
181
   but an offset suffix may be added for array variables
182
*)
183
let rec pp_c_val self pp_var fmt v =
184
  match v.value_desc with
185
  | Cst c         -> pp_c_const fmt c
186
  | Array vl      -> fprintf fmt "{%a}" (Utils.fprintf_list ~sep:", " (pp_c_val self pp_var)) vl
187
  | Access (t, i) -> fprintf fmt "%a[%a]" (pp_c_val self pp_var) t (pp_c_val self pp_var) i
188
  | Power (v, n)  -> (Format.eprintf "internal error: C_backend_common.pp_c_val %a@." pp_val v; assert false)
189
  | LocalVar v    -> pp_var fmt v
190
  | StateVar v    ->
191
    (* array memory vars are represented by an indirection to a local var with the right type,
192
       in order to avoid casting everywhere. *)
193
    if Types.is_array_type v.var_type && not (Types.is_real_type v.var_type && !Options.mpfr)
194
    then fprintf fmt "%a" pp_var v
195
    else fprintf fmt "%s->_reg.%a" self pp_var v
196
  | Fun (n, vl)   -> Basic_library.pp_c n (pp_c_val self pp_var) fmt vl
197

    
198
(* Access to the value of a variable:
199
   - if it's not a scalar output, then its name is enough
200
   - otherwise, dereference it (it has been declared as a pointer,
201
     despite its scalar Lustre type)
202
   - moreover, dereference memory array variables.
203
*)
204
let pp_c_var_read m fmt id =
205
  (* mpfr_t is a static array, not treated as general arrays *)
206
  if Types.is_address_type id.var_type
207
  then
208
    if is_memory m id && not (Types.is_real_type id.var_type && !Options.mpfr)
209
    then fprintf fmt "(*%s)" id.var_id
210
    else fprintf fmt "%s" id.var_id
211
  else
212
    if is_output m id
213
    then fprintf fmt "*%s" id.var_id
214
    else fprintf fmt "%s" id.var_id
215

    
216
(* Addressable value of a variable, the one that is passed around in calls:
217
   - if it's not a scalar non-output, then its name is enough
218
   - otherwise, reference it (it must be passed as a pointer,
219
     despite its scalar Lustre type)
220
*)
221
let pp_c_var_write m fmt id =
222
  if Types.is_address_type id.var_type
223
  then
224
    fprintf fmt "%s" id.var_id
225
  else
226
    if is_output m id
227
    then
228
      fprintf fmt "%s" id.var_id
229
    else
230
      fprintf fmt "&%s" id.var_id
231

    
232
(* Declaration of an input variable:
233
   - if its type is array/matrix/etc, then declare it as a mere pointer,
234
     in order to cope with unknown/parametric array dimensions, 
235
     as it is the case for generics
236
*)
237
let pp_c_decl_input_var fmt id =
238
  if !Options.ansi && Types.is_address_type id.var_type
239
  then pp_c_type (sprintf "(*%s)" id.var_id) fmt (Types.array_base_type id.var_type)
240
  else pp_c_type id.var_id fmt id.var_type
241

    
242
(* Declaration of an output variable:
243
   - if its type is scalar, then pass its address
244
   - if its type is array/matrix/struct/etc, then declare it as a mere pointer,
245
     in order to cope with unknown/parametric array dimensions, 
246
     as it is the case for generics
247
*)
248
let pp_c_decl_output_var fmt id =
249
  if (not !Options.ansi) && Types.is_address_type id.var_type
250
  then pp_c_type                  id.var_id  fmt id.var_type
251
  else pp_c_type (sprintf "(*%s)" id.var_id) fmt (Types.array_base_type id.var_type)
252

    
253
(* Declaration of a local/mem variable:
254
   - if it's an array/matrix/etc, its size(s) should be
255
     known in order to statically allocate memory, 
256
     so we print the full type
257
*)
258
let pp_c_decl_local_var m fmt id =
259
  if id.var_dec_const
260
  then
261
    Format.fprintf fmt "%a = %a"
262
      (pp_c_type id.var_id) id.var_type
263
      (pp_c_val "" (pp_c_var_read m)) (get_const_assign m id)
264
  else
265
    Format.fprintf fmt "%a"
266
      (pp_c_type id.var_id) id.var_type
267

    
268
let pp_c_decl_array_mem self fmt id =
269
  fprintf fmt "%a = (%a) (%s->_reg.%s)"
270
    (pp_c_type (sprintf "(*%s)" id.var_id)) id.var_type
271
    (pp_c_type "(*)") id.var_type
272
    self
273
    id.var_id
274

    
275
(* Declaration of a struct variable:
276
   - if it's an array/matrix/etc, we declare it as a pointer
277
*)
278
let pp_c_decl_struct_var fmt id =
279
  if Types.is_array_type id.var_type
280
  then pp_c_type (sprintf "(*%s)" id.var_id) fmt (Types.array_base_type id.var_type)
281
  else pp_c_type                  id.var_id  fmt id.var_type
282

    
283
let pp_c_decl_instance_var fmt (name, (node, static)) = 
284
  fprintf fmt "%a *%s" pp_machine_memtype_name (node_name node) name
285

    
286
let pp_c_checks self fmt m =
287
  Utils.fprintf_list ~sep:"" 
288
    (fun fmt (loc, check) -> 
289
      fprintf fmt 
290
	"@[<v>%a@,assert (%a);@]@," 
291
	Location.pp_c_loc loc
292
	(pp_c_val self (pp_c_var_read m)) check
293
    ) 
294
    fmt 
295
    m.mstep.step_checks
296

    
297
(********************************************************************************************)
298
(*                       Struct Printing functions                                          *)
299
(********************************************************************************************)
300

    
301
let pp_registers_struct fmt m =
302
  if m.mmemory <> []
303
  then
304
    fprintf fmt "@[%a {@[<v>%a;@ @]}@] _reg; "
305
      pp_machine_regtype_name m.mname.node_id
306
      (Utils.fprintf_list ~sep:";@ " pp_c_decl_struct_var) m.mmemory
307
  else
308
    ()
309

    
310
let print_machine_struct fmt m =
311
  if fst (get_stateless_status m) then
312
    begin
313
    end
314
  else
315
    begin
316
      (* Define struct *)
317
      fprintf fmt "@[%a {@[<v>%a%t%a%t@]};@]@."
318
	pp_machine_memtype_name m.mname.node_id
319
	pp_registers_struct m
320
	(Utils.pp_final_char_if_non_empty "@ " m.mmemory)
321
	(Utils.fprintf_list ~sep:";@ " pp_c_decl_instance_var) m.minstances
322
	(Utils.pp_final_char_if_non_empty ";@ " m.minstances)
323
    end
324

    
325
let print_machine_struct_from_header fmt inode =
326
  if inode.nodei_stateless then
327
    begin
328
    end
329
  else
330
    begin
331
      (* Declare struct *)
332
      fprintf fmt "@[%a;@]@."
333
	pp_machine_memtype_name inode.nodei_id
334
    end
335

    
336
(********************************************************************************************)
337
(*                      Prototype Printing functions                                        *)
338
(********************************************************************************************)
339

    
340
let print_global_init_prototype fmt baseNAME =
341
  fprintf fmt "void %a ()"
342
    pp_global_init_name baseNAME
343

    
344
let print_global_clear_prototype fmt baseNAME =
345
  fprintf fmt "void %a ()"
346
    pp_global_clear_name baseNAME
347

    
348
let print_alloc_prototype fmt (name, static) =
349
  fprintf fmt "%a * %a (%a)"
350
    pp_machine_memtype_name name
351
    pp_machine_alloc_name name
352
    (Utils.fprintf_list ~sep:",@ " pp_c_decl_input_var) static
353

    
354
let print_reset_prototype self fmt (name, static) =
355
  fprintf fmt "void %a (@[<v>%a%t%a *%s@])"
356
    pp_machine_reset_name name
357
    (Utils.fprintf_list ~sep:",@ " pp_c_decl_input_var) static
358
    (Utils.pp_final_char_if_non_empty ",@," static) 
359
    pp_machine_memtype_name name
360
    self
361

    
362
let print_init_prototype self fmt (name, static) =
363
  fprintf fmt "void %a (@[<v>%a%t%a *%s@])"
364
    pp_machine_init_name name
365
    (Utils.fprintf_list ~sep:",@ " pp_c_decl_input_var) static
366
    (Utils.pp_final_char_if_non_empty ",@," static) 
367
    pp_machine_memtype_name name
368
    self
369

    
370
let print_clear_prototype self fmt (name, static) =
371
  fprintf fmt "void %a (@[<v>%a%t%a *%s@])"
372
    pp_machine_clear_name name
373
    (Utils.fprintf_list ~sep:",@ " pp_c_decl_input_var) static
374
    (Utils.pp_final_char_if_non_empty ",@," static) 
375
    pp_machine_memtype_name name
376
    self
377

    
378
let print_stateless_prototype fmt (name, inputs, outputs) =
379
  fprintf fmt "void %a (@[<v>@[%a%t@]@,@[%a@]@,@])"
380
    pp_machine_step_name name
381
    (Utils.fprintf_list ~sep:",@ " pp_c_decl_input_var) inputs
382
    (Utils.pp_final_char_if_non_empty ",@ " inputs) 
383
    (Utils.fprintf_list ~sep:",@ " pp_c_decl_output_var) outputs
384

    
385
let print_step_prototype self fmt (name, inputs, outputs) =
386
  fprintf fmt "void %a (@[<v>@[%a%t@]@,@[%a@]%t@[%a *%s@]@])"
387
    pp_machine_step_name name
388
    (Utils.fprintf_list ~sep:",@ " pp_c_decl_input_var) inputs
389
    (Utils.pp_final_char_if_non_empty ",@ " inputs) 
390
    (Utils.fprintf_list ~sep:",@ " pp_c_decl_output_var) outputs
391
    (Utils.pp_final_char_if_non_empty ",@," outputs) 
392
    pp_machine_memtype_name name
393
    self
394

    
395
let print_stateless_C_prototype fmt (name, inputs, outputs) =
396
  let output = 
397
    match outputs with
398
    | [hd] -> hd
399
    | _ -> assert false
400
  in
401
  fprintf fmt "%a %s (@[<v>@[%a@]@,@])"
402
    pp_basic_c_type output.var_type
403
    name
404
    (Utils.fprintf_list ~sep:",@ " pp_c_decl_input_var) inputs
405
    
406
let print_import_init fmt (Dep (local, basename, _, _)) =
407
  if local then
408
    let baseNAME = file_to_module_name basename in
409
    fprintf fmt "%a();" pp_global_init_name baseNAME
410
  else ()
411

    
412
let print_import_clear fmt (Dep (local, basename, _, _)) =
413
  if local then
414
    let baseNAME = file_to_module_name basename in
415
    fprintf fmt "%a();" pp_global_clear_name baseNAME
416
  else ()
417

    
418
let print_import_prototype fmt (Dep (_, s, _, _)) =
419
  fprintf fmt "#include \"%s.h\"@," s
420

    
421
let print_import_alloc_prototype fmt (Dep (_, s, _, stateful)) =
422
  if stateful then
423
    fprintf fmt "#include \"%s_alloc.h\"@," s
424

    
425
let print_extern_alloc_prototypes fmt (Dep (_,_, header,_)) =
426
  List.iter (fun decl -> match decl.top_decl_desc with
427
  | ImportedNode ind when not ind.nodei_stateless ->
428
    let static = List.filter (fun v -> v.var_dec_const) ind.nodei_inputs
429
    in fprintf fmt "extern %a;@." print_alloc_prototype (ind.nodei_id, static)
430
  | _                -> ()
431
  ) header
432

    
433

    
434
let pp_c_main_var_input fmt id =  
435
  fprintf fmt "%s" id.var_id
436

    
437
let pp_c_main_var_output fmt id =
438
  if Types.is_address_type id.var_type
439
  then
440
    fprintf fmt "%s" id.var_id
441
  else
442
    fprintf fmt "&%s" id.var_id
443

    
444
let pp_main_call mname self fmt m (inputs: value_t list) (outputs: var_decl list) =
445
  if fst (get_stateless_status m)
446
  then
447
    fprintf fmt "%a (%a%t%a);"
448
      pp_machine_step_name mname
449
      (Utils.fprintf_list ~sep:", " (pp_c_val self pp_c_main_var_input)) inputs
450
      (Utils.pp_final_char_if_non_empty ", " inputs) 
451
      (Utils.fprintf_list ~sep:", " pp_c_main_var_output) outputs
452
  else
453
    fprintf fmt "%a (%a%t%a%t%s);"
454
      pp_machine_step_name mname
455
      (Utils.fprintf_list ~sep:", " (pp_c_val self pp_c_main_var_input)) inputs
456
      (Utils.pp_final_char_if_non_empty ", " inputs) 
457
      (Utils.fprintf_list ~sep:", " pp_c_main_var_output) outputs
458
      (Utils.pp_final_char_if_non_empty ", " outputs)
459
      self
460

    
461
let pp_c_var m self pp_var fmt var =
462
  if is_memory m var
463
  then
464
    pp_c_val self pp_var fmt (mk_val (StateVar var) var.var_type)
465
  else
466
    pp_c_val self pp_var fmt (mk_val (LocalVar var) var.var_type)
467

    
468
let pp_array_suffix fmt loop_vars =
469
  Utils.fprintf_list ~sep:"" (fun fmt v -> fprintf fmt "[%s]" v) fmt loop_vars
470

    
471
(* type directed initialization: useless wrt the lustre compilation model,
472
   except for MPFR injection, where values are dynamically allocated
473
*)
474
let pp_initialize m self pp_var fmt var =
475
  let rec aux indices fmt typ =
476
    if Types.is_array_type typ
477
    then
478
      let dim = Types.array_type_dimension typ in
479
      let idx = mk_loop_var m () in
480
      fprintf fmt "@[<v 2>{@,int %s;@,for(%s=0;%s<%a;%s++)@,%a @]@,}"
481
	idx idx idx pp_c_dimension dim idx
482
	(aux (idx::indices)) (Types.array_element_type typ)
483
    else
484
      let indices = List.rev indices in
485
      let pp_var_suffix fmt var =
486
	fprintf fmt "%a%a" (pp_c_var m self pp_var) var pp_array_suffix indices in
487
      Mpfr.pp_inject_init pp_var_suffix fmt var
488
  in
489
  if !Options.mpfr && Types.is_real_type (Types.array_base_type var.var_type)
490
  then
491
    begin
492
      reset_loop_counter ();
493
      aux [] fmt var.var_type
494
    end
495

    
496
let pp_const_initialize pp_var fmt const =
497
  let var = mk_val (LocalVar (Corelang.var_decl_of_const const)) const.const_type in
498
  let rec aux indices value fmt typ =
499
    if Types.is_array_type typ
500
    then
501
      let dim = Types.array_type_dimension typ in
502
      let szl = Utils.enumerate (Dimension.size_const_dimension dim) in
503
      let typ' = Types.array_element_type typ in
504
      let value = match value with
505
	| Const_array ca -> List.nth ca
506
	| _                      -> assert false in
507
      fprintf fmt "%a"
508
	(Utils.fprintf_list ~sep:"@," (fun fmt i -> aux (string_of_int i::indices) (value i) fmt typ')) szl
509
    else
510
      let indices = List.rev indices in
511
      let pp_var_suffix fmt var =
512
	fprintf fmt "%a%a" (pp_c_val "" pp_var) var pp_array_suffix indices in
513
      begin
514
	Mpfr.pp_inject_init pp_var_suffix fmt var;
515
	fprintf fmt "@,";
516
	Mpfr.pp_inject_real pp_var_suffix pp_c_const fmt var value
517
      end
518
  in
519
  if !Options.mpfr && Types.is_real_type (Types.array_base_type const.const_type)
520
  then
521
    begin
522
      reset_loop_counter ();
523
      aux [] const.const_value fmt const.const_type
524
    end
525

    
526
(* type directed clear: useless wrt the lustre compilation model,
527
   except for MPFR injection, where values are dynamically allocated
528
*)
529
let pp_clear m self pp_var fmt var =
530
  let rec aux indices fmt typ =
531
    if Types.is_array_type typ
532
    then
533
      let dim = Types.array_type_dimension typ in
534
      let idx = mk_loop_var m () in
535
      fprintf fmt "@[<v 2>{@,int %s;@,for(%s=0;%s<%a;%s++)@,%a @]@,}"
536
	idx idx idx pp_c_dimension dim idx
537
	(aux (idx::indices)) (Types.array_element_type typ)
538
    else
539
      let indices = List.rev indices in
540
      let pp_var_suffix fmt var =
541
	fprintf fmt "%a%a" (pp_c_var m self pp_var) var pp_array_suffix indices in
542
      Mpfr.pp_inject_clear pp_var_suffix fmt var
543
  in
544
  if !Options.mpfr && Types.is_real_type (Types.array_base_type var.var_type)
545
  then
546
    begin
547
      reset_loop_counter ();
548
      aux [] fmt var.var_type
549
    end
550

    
551
let pp_const_clear pp_var fmt const =
552
  let m = Machine_code.empty_machine in
553
  let var = Corelang.var_decl_of_const const in
554
  let rec aux indices fmt typ =
555
    if Types.is_array_type typ
556
    then
557
      let dim = Types.array_type_dimension typ in
558
      let idx = mk_loop_var m () in
559
      fprintf fmt "@[<v 2>{@,int %s;@,for(%s=0;%s<%a;%s++)@,%a @]@,}"
560
	idx idx idx pp_c_dimension dim idx
561
	(aux (idx::indices)) (Types.array_element_type typ)
562
    else
563
      let indices = List.rev indices in
564
      let pp_var_suffix fmt var =
565
	fprintf fmt "%a%a" (pp_c_var m "" pp_var) var pp_array_suffix indices in
566
      Mpfr.pp_inject_clear pp_var_suffix fmt var 
567
  in
568
  if !Options.mpfr && Types.is_real_type (Types.array_base_type var.var_type)
569
  then
570
    begin
571
      reset_loop_counter ();
572
      aux [] fmt var.var_type
573
    end
574

    
575
let pp_call m self pp_read pp_write fmt i (inputs: value_t list) (outputs: var_decl list) =
576
 try (* stateful node instance *)
577
   let (n,_) = List.assoc i m.minstances in
578
   fprintf fmt "%a (%a%t%a%t%s->%s);"
579
     pp_machine_step_name (node_name n)
580
     (Utils.fprintf_list ~sep:", " (pp_c_val self pp_read)) inputs
581
     (Utils.pp_final_char_if_non_empty ", " inputs) 
582
     (Utils.fprintf_list ~sep:", " pp_write) outputs
583
     (Utils.pp_final_char_if_non_empty ", " outputs)
584
     self
585
     i
586
 with Not_found -> (* stateless node instance *)
587
   let (n,_) = List.assoc i m.mcalls in
588
   fprintf fmt "%a (%a%t%a);"
589
     pp_machine_step_name (node_name n)
590
     (Utils.fprintf_list ~sep:", " (pp_c_val self pp_read)) inputs
591
     (Utils.pp_final_char_if_non_empty ", " inputs) 
592
     (Utils.fprintf_list ~sep:", " pp_write) outputs 
593

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

    
616
let pp_instance_call m self fmt i (inputs: value_t list) (outputs: var_decl list) =
617
  let pp_offset pp_var indices fmt var =
618
    match indices with
619
    | [] -> fprintf fmt "%a" pp_var var
620
    | _  -> fprintf fmt "%a[%a]" pp_var var (Utils.fprintf_list ~sep:"][" pp_print_string) indices in
621
  let rec aux indices fmt typ =
622
    if Types.is_array_type typ
623
    then
624
      let dim = Types.array_type_dimension typ in
625
      let idx = mk_loop_var m () in
626
      fprintf fmt "@[<v 2>{@,int %s;@,for(%s=0;%s<%a;%s++)@,%a @]@,}"
627
	idx idx idx pp_c_dimension dim idx
628
	(aux (idx::indices)) (Types.array_element_type typ)
629
    else
630
      let pp_read  = pp_offset (pp_c_var_read  m) indices in
631
      let pp_write = pp_offset (pp_c_var_write m) indices in
632
      pp_call m self pp_read pp_write fmt i inputs outputs
633
  in
634
  begin
635
    reset_loop_counter ();
636
    aux [] fmt (List.hd inputs).value_type
637
  end
638

    
639
(* Local Variables: *)
640
(* compile-command:"make -C ../../.." *)
641
(* End: *)