Project

General

Profile

Download (24 KB) Statistics
| Branch: | Tag: | Revision:
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_ascii 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_dealloc_name fmt id = fprintf fmt "%s_dealloc" id
98
let pp_machine_static_declare_name fmt id = fprintf fmt "%s_DECLARE" id
99
let pp_machine_static_link_name fmt id = fprintf fmt "%s_LINK" id
100
let pp_machine_static_alloc_name fmt id = fprintf fmt "%s_ALLOC" id
101
let pp_machine_reset_name fmt id = fprintf fmt "%s_reset" id
102
let pp_machine_init_name fmt id = fprintf fmt "%s_init" id
103
let pp_machine_clear_name fmt id = fprintf fmt "%s_clear" id
104
let pp_machine_step_name fmt id = fprintf fmt "%s_step" id
105

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

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

    
128
let pp_basic_c_type fmt t =
129
  match (Types.repr t).Types.tdesc with
130
  | Types.Tbool                    -> fprintf fmt "_Bool"
131
  | Types.Treal when !Options.mpfr -> fprintf fmt "%s" Mpfr.mpfr_t
132
  | Types.Treal                    -> fprintf fmt "double"
133
  | Types.Tint                     -> fprintf fmt "int"
134
  | _ -> assert false (* Not a basic C type. Do not handle arrays or pointers *)
135

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

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

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

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

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

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

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

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

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

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

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

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

    
295
(********************************************************************************************)
296
(*                       Struct Printing functions                                          *)
297
(********************************************************************************************)
298

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

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

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

    
334
(********************************************************************************************)
335
(*                      Prototype Printing functions                                        *)
336
(********************************************************************************************)
337

    
338
let print_global_init_prototype fmt baseNAME =
339
  fprintf fmt "void %a ()"
340
    pp_global_init_name baseNAME
341

    
342
let print_global_clear_prototype fmt baseNAME =
343
  fprintf fmt "void %a ()"
344
    pp_global_clear_name baseNAME
345

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

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

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

    
373
let print_clear_prototype self fmt (name, static) =
374
  fprintf fmt "void %a (@[<v>%a%t%a *%s@])"
375
    pp_machine_clear_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_stateless_prototype fmt (name, inputs, outputs) =
382
  fprintf fmt "void %a (@[<v>@[%a%t@]@,@[%a@]@,@])"
383
    pp_machine_step_name name
384
    (Utils.fprintf_list ~sep:",@ " pp_c_decl_input_var) inputs
385
    (Utils.pp_final_char_if_non_empty ",@ " inputs) 
386
    (Utils.fprintf_list ~sep:",@ " pp_c_decl_output_var) outputs
387

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

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

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

    
421
let print_import_prototype fmt (Dep (_, s, _, _)) =
422
  fprintf fmt "#include \"%s.h\"@," s
423

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

    
428
let print_extern_alloc_prototypes fmt (Dep (_,_, header,_)) =
429
  List.iter (fun decl -> match decl.top_decl_desc with
430
  | ImportedNode ind when not ind.nodei_stateless ->
431
    let static = List.filter (fun v -> v.var_dec_const) ind.nodei_inputs in
432
    begin
433
      fprintf fmt "extern %a;@.@." print_alloc_prototype (ind.nodei_id, static);
434
      fprintf fmt "extern %a;@.@." print_dealloc_prototype ind.nodei_id;
435
    end
436
  | _                -> ()
437
  ) header
438

    
439

    
440
let pp_c_main_var_input fmt id =  
441
  fprintf fmt "%s" id.var_id
442

    
443
let pp_c_main_var_output fmt id =
444
  if Types.is_address_type id.var_type
445
  then
446
    fprintf fmt "%s" id.var_id
447
  else
448
    fprintf fmt "&%s" id.var_id
449

    
450
let pp_main_call mname self fmt m (inputs: value_t list) (outputs: var_decl list) =
451
  if fst (get_stateless_status m)
452
  then
453
    fprintf fmt "%a (%a%t%a);"
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
  else
459
    fprintf fmt "%a (%a%t%a%t%s);"
460
      pp_machine_step_name mname
461
      (Utils.fprintf_list ~sep:", " (pp_c_val self pp_c_main_var_input)) inputs
462
      (Utils.pp_final_char_if_non_empty ", " inputs) 
463
      (Utils.fprintf_list ~sep:", " pp_c_main_var_output) outputs
464
      (Utils.pp_final_char_if_non_empty ", " outputs)
465
      self
466

    
467
let pp_c_var m self pp_var fmt var =
468
  if is_memory m var
469
  then
470
    pp_c_val self pp_var fmt (mk_val (StateVar var) var.var_type)
471
  else
472
    pp_c_val self pp_var fmt (mk_val (LocalVar var) var.var_type)
473

    
474
let pp_array_suffix fmt loop_vars =
475
  Utils.fprintf_list ~sep:"" (fun fmt v -> fprintf fmt "[%s]" v) fmt loop_vars
476

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

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

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

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

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

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

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

    
645
(* Local Variables: *)
646
(* compile-command:"make -C ../../.." *)
647
(* End: *)
(2-2/7)