Project

General

Profile

Statistics
| Branch: | Tag: | Revision:

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

History | View | Annotate | Download (32.6 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 Machine_code_types
15
open Corelang
16
open Machine_code
17
open C_backend_common
18

    
19
module type MODIFIERS_SRC =
20
sig
21
end
22

    
23
module EmptyMod =
24
struct
25
end
26

    
27
module Main = functor (Mod: MODIFIERS_SRC) -> 
28
struct
29

    
30
(********************************************************************************************)
31
(*                    Instruction Printing functions                                        *)
32
(********************************************************************************************)
33

    
34

    
35
(* Computes the depth to which multi-dimension array assignments should be expanded.
36
   It equals the maximum number of nested static array constructions accessible from root [v].
37
*)
38
  let rec expansion_depth v =
39
    match v.value_desc with
40
    | Cst cst -> expansion_depth_cst cst
41
    | LocalVar _
42
    | StateVar _  -> 0
43
    | Fun (_, vl) -> List.fold_right (fun v -> max (expansion_depth v)) vl 0
44
    | Array vl    -> 1 + List.fold_right (fun v -> max (expansion_depth v)) vl 0
45
    | Access (v, i) -> max 0 (expansion_depth v - 1)
46
    | Power (v, n)  -> 0 (*1 + expansion_depth v*)
47
  and expansion_depth_cst c = 
48
    match c with
49
      Const_array cl -> 1 + List.fold_right (fun c -> max (expansion_depth_cst c)) cl 0
50
    | _ -> 0
51
  
52
  let rec merge_static_loop_profiles lp1 lp2 =
53
    match lp1, lp2 with
54
    | []      , _        -> lp2
55
    | _       , []       -> lp1
56
    | p1 :: q1, p2 :: q2 -> (p1 || p2) :: merge_static_loop_profiles q1 q2
57

    
58
(* Returns a list of bool values, indicating whether the indices must be static or not *)
59
  let rec static_loop_profile v =
60
    match v.value_desc with
61
    | Cst cst  -> static_loop_profile_cst cst
62
    | LocalVar _
63
    | StateVar _  -> []
64
    | Fun (_, vl) -> List.fold_right (fun v lp -> merge_static_loop_profiles lp (static_loop_profile v)) vl []
65
    | Array vl    -> true :: List.fold_right (fun v lp -> merge_static_loop_profiles lp (static_loop_profile v)) vl []
66
    | Access (v, i) -> (match (static_loop_profile v) with [] -> [] | _ :: q -> q)
67
    | Power (v, n)  -> false :: static_loop_profile v
68
  and static_loop_profile_cst cst =
69
    match cst with
70
      Const_array cl -> List.fold_right 
71
	(fun c lp -> merge_static_loop_profiles lp (static_loop_profile_cst c))
72
	cl 
73
	[]
74
    | _ -> [] 
75
  
76
  
77
let rec is_const_index v =
78
  match v.value_desc with
79
  | Cst (Const_int _) -> true
80
  | Fun (_, vl)       -> List.for_all is_const_index vl
81
  | _                 -> false
82

    
83
type loop_index = LVar of ident | LInt of int ref | LAcc of value_t
84
(*
85
let rec value_offsets v offsets =
86
 match v, offsets with
87
 | _                        , []          -> v
88
 | Power (v, n)             , _ :: q      -> value_offsets v q
89
 | Array vl                 , LInt r :: q -> value_offsets (List.nth vl !r) q
90
 | Cst (Const_array cl)     , LInt r :: q -> value_offsets (Cst (List.nth cl !r)) q
91
 | Fun (f, vl)              , _           -> Fun (f, List.map (fun v -> value_offsets v offsets) vl)
92
 | _                        , LInt r :: q -> value_offsets (Access (v, Cst (Const_int !r))) q
93
 | _                        , LVar i :: q -> value_offsets (Access (v, LocalVar i)) q
94
*)
95
(* Computes the list of nested loop variables together with their dimension bounds.
96
   - LInt r stands for loop expansion (no loop variable, but int loop index)
97
   - LVar v stands for loop variable v
98
*)
99
let rec mk_loop_variables m ty depth =
100
 match (Types.repr ty).Types.tdesc, depth with
101
 | Types.Tarray (d, ty'), 0       ->
102
   let v = mk_loop_var m () in
103
   (d, LVar v) :: mk_loop_variables m ty' 0
104
 | Types.Tarray (d, ty'), _       ->
105
   let r = ref (-1) in
106
   (d, LInt r) :: mk_loop_variables m ty' (depth - 1)
107
 | _                    , 0       -> []
108
 | _                              -> assert false
109

    
110
let reorder_loop_variables loop_vars =
111
  let (int_loops, var_loops) = 
112
    List.partition (function (d, LInt _) -> true | _ -> false) loop_vars 
113
  in
114
  var_loops @ int_loops
115

    
116
(* Prints a one loop variable suffix for arrays *)
117
let pp_loop_var fmt lv =
118
 match snd lv with
119
 | LVar v -> fprintf fmt "[%s]" v
120
 | LInt r -> fprintf fmt "[%d]" !r
121
 | LAcc i -> fprintf fmt "[%a]" pp_val i
122

    
123
(* Prints a suffix of loop variables for arrays *)
124
let pp_suffix fmt loop_vars =
125
 Utils.fprintf_list ~sep:"" pp_loop_var fmt loop_vars
126

    
127
(* Prints a value expression [v], with internal function calls only.
128
   [pp_var] is a printer for variables (typically [pp_c_var_read]),
129
   but an offset suffix may be added for array variables
130
*)
131
(* Prints a constant value before a suffix (needs casting) *)
132
let rec pp_c_const_suffix var_type fmt c =
133
  match c with
134
    | Const_int i          -> pp_print_int fmt i
135
    | Const_real (_, _, s) -> pp_print_string fmt s
136
    | Const_tag t          -> pp_c_tag fmt t
137
    | Const_array ca       -> let var_type = Types.array_element_type var_type in
138
                              fprintf fmt "(%a[]){%a }" (pp_c_type "") var_type (Utils.fprintf_list ~sep:", " (pp_c_const_suffix var_type)) ca
139
    | Const_struct fl       -> fprintf fmt "{%a }" (Utils.fprintf_list ~sep:", " (fun fmt (f, c) -> (pp_c_const_suffix (Types.struct_field_type var_type f)) fmt c)) fl
140
    | Const_string _        -> assert false (* string occurs in annotations not in C *)
141

    
142

    
143
(* Prints a [value] of type [var_type] indexed by the suffix list [loop_vars] *)
144
let rec pp_value_suffix self var_type loop_vars pp_value fmt value =
145
  (*Format.eprintf "pp_value_suffix: %a %a %a@." Types.print_ty var_type Machine_code.pp_val value pp_suffix loop_vars;*)
146
  (
147
    match loop_vars, value.value_desc with
148
    | (x, LAcc i) :: q, _ when is_const_index i ->
149
       let r = ref (Dimension.size_const_dimension (Machine_code.dimension_of_value i)) in
150
       pp_value_suffix self var_type ((x, LInt r)::q) pp_value fmt value
151
    | (_, LInt r) :: q, Cst (Const_array cl) ->
152
       let var_type = Types.array_element_type var_type in
153
       pp_value_suffix self var_type q pp_value fmt (mk_val (Cst (List.nth cl !r)) Type_predef.type_int)
154
    | (_, LInt r) :: q, Array vl      ->
155
       let var_type = Types.array_element_type var_type in
156
       pp_value_suffix self var_type q pp_value fmt (List.nth vl !r)
157
    | loop_var    :: q, Array vl      ->
158
       let var_type = Types.array_element_type var_type in
159
       Format.fprintf fmt "(%a[]){%a }%a" (pp_c_type "") var_type (Utils.fprintf_list ~sep:", " (pp_value_suffix self var_type q pp_value)) vl pp_suffix [loop_var]
160
    | []              , Array vl      ->
161
       let var_type = Types.array_element_type var_type in
162
       Format.fprintf fmt "(%a[]){%a }" (pp_c_type "") var_type (Utils.fprintf_list ~sep:", " (pp_value_suffix self var_type [] pp_value)) vl
163
    | _           :: q, Power (v, n)  ->
164
       pp_value_suffix self var_type q pp_value fmt v
165
    | _               , Fun (n, vl)   ->
166
       Basic_library.pp_c n (pp_value_suffix self var_type loop_vars pp_value) fmt vl
167
    | _               , Access (v, i) ->
168
       let var_type = Type_predef.type_array (Dimension.mkdim_var ()) var_type in
169
       pp_value_suffix self var_type ((Dimension.mkdim_var (), LAcc i) :: loop_vars) pp_value fmt v
170
    | _               , LocalVar v    -> Format.fprintf fmt "%a%a" pp_value v pp_suffix loop_vars
171
    | _               , StateVar v    ->
172
       (* array memory vars are represented by an indirection to a local var with the right type,
173
	  in order to avoid casting everywhere. *)
174
       if Types.is_array_type v.var_type
175
       then Format.fprintf fmt "%a%a" pp_value v pp_suffix loop_vars
176
       else Format.fprintf fmt "%s->_reg.%a%a" self pp_value v pp_suffix loop_vars
177
    | _               , Cst cst       -> pp_c_const_suffix var_type fmt cst
178
    | _               , _             -> (Format.eprintf "internal error: C_backend_src.pp_value_suffix %a %a %a@." Types.print_ty var_type Machine_code.pp_val value pp_suffix loop_vars; assert false)
179
  )
180
   
181
(* Subsumes C_backend_common.pp_c_val to cope with aggressive substitution
182
   which may yield constant arrays in expressions.
183
   Type is needed to correctly print constant arrays.
184
 *)
185
let pp_c_val self pp_var fmt v =
186
  pp_value_suffix self v.value_type [] pp_var fmt v
187

    
188
let pp_basic_assign pp_var fmt typ var_name value =
189
  if Types.is_real_type typ && !Options.mpfr
190
  then
191
    Mpfr.pp_inject_assign pp_var fmt var_name value
192
  else
193
    fprintf fmt "%a = %a;" 
194
      pp_var var_name
195
      pp_var value
196

    
197
(* type_directed assignment: array vs. statically sized type
198
   - [var_type]: type of variable to be assigned
199
   - [var_name]: name of variable to be assigned
200
   - [value]: assigned value
201
   - [pp_var]: printer for variables
202
*)
203
let pp_assign m self pp_var fmt var_type var_name value =
204
  let depth = expansion_depth value in
205
  (*Format.eprintf "pp_assign %a %a %a %d@." Types.print_ty var_type pp_val var_name pp_val value depth;*)
206
  let loop_vars = mk_loop_variables m var_type depth in
207
  let reordered_loop_vars = reorder_loop_variables loop_vars in
208
  let rec aux typ fmt vars =
209
    match vars with
210
    | [] ->
211
       pp_basic_assign (pp_value_suffix self var_type loop_vars pp_var) fmt typ var_name value
212
    | (d, LVar i) :: q ->
213
       let typ' = Types.array_element_type typ in
214
      (*eprintf "pp_aux %a %s@." Dimension.pp_dimension d i;*)
215
      fprintf fmt "@[<v 2>{@,int %s;@,for(%s=0;%s<%a;%s++)@,%a @]@,}"
216
	i i i pp_c_dimension d i
217
	(aux typ') q
218
    | (d, LInt r) :: q ->
219
       (*eprintf "pp_aux %a %d@." Dimension.pp_dimension d (!r);*)
220
       let typ' = Types.array_element_type typ in
221
       let szl = Utils.enumerate (Dimension.size_const_dimension d) in
222
       fprintf fmt "@[<v 2>{@,%a@]@,}"
223
	       (Utils.fprintf_list ~sep:"@," (fun fmt i -> r := i; aux typ' fmt q)) szl
224
    | _ -> assert false
225
  in
226
  begin
227
    reset_loop_counter ();
228
    (*reset_addr_counter ();*)
229
    aux var_type fmt reordered_loop_vars;
230
    (*Format.eprintf "end pp_assign@.";*)
231
  end
232

    
233
let pp_machine_reset (m: machine_t) self fmt inst =
234
  let (node, static) =
235
    try
236
      List.assoc inst m.minstances
237
    with Not_found -> (Format.eprintf "internal error: pp_machine_reset %s %s %s:@." m.mname.node_id self inst; raise Not_found) in
238
  fprintf fmt "%a(%a%t%s->%s);"
239
    pp_machine_reset_name (node_name node)
240
    (Utils.fprintf_list ~sep:", " Dimension.pp_dimension) static
241
    (Utils.pp_final_char_if_non_empty ", " static)
242
    self inst
243

    
244
let pp_machine_init (m: machine_t) self fmt inst =
245
  let (node, static) =
246
    try
247
      List.assoc inst m.minstances
248
    with Not_found -> (Format.eprintf "internal error: pp_machine_init %s %s %s@." m.mname.node_id self inst; raise Not_found) in
249
  fprintf fmt "%a(%a%t%s->%s);"
250
    pp_machine_init_name (node_name node)
251
    (Utils.fprintf_list ~sep:", " Dimension.pp_dimension) static
252
    (Utils.pp_final_char_if_non_empty ", " static)
253
    self inst
254

    
255
let pp_machine_clear (m: machine_t) self fmt inst =
256
  let (node, static) =
257
    try
258
      List.assoc inst m.minstances
259
    with Not_found -> (Format.eprintf "internal error: pp_machine_clear %s %s %s@." m.mname.node_id self inst; raise Not_found) in
260
  fprintf fmt "%a(%a%t%s->%s);"
261
    pp_machine_clear_name (node_name node)
262
    (Utils.fprintf_list ~sep:", " Dimension.pp_dimension) static
263
    (Utils.pp_final_char_if_non_empty ", " static)
264
    self inst
265

    
266
let has_c_prototype funname dependencies =
267
  let imported_node_opt = (* We select the last imported node with the name funname.
268
			       The order of evaluation of dependencies should be
269
			       compatible with overloading. (Not checked yet) *) 
270
      List.fold_left
271
	(fun res (Dep (_, _, decls, _)) -> 
272
	  match res with
273
	  | Some _ -> res
274
	  | None -> 
275
	    let matched = fun t -> match t.top_decl_desc with 
276
	      | ImportedNode nd -> nd.nodei_id = funname 
277
	      | _ -> false
278
	    in
279
	    if List.exists matched decls then (
280
	      match (List.find matched decls).top_decl_desc with
281
	      | ImportedNode nd -> Some nd
282
	      | _ -> assert false
283
	    )
284
	    else
285
	      None
286
	) None dependencies in
287
    match imported_node_opt with
288
    | None -> false
289
    | Some nd -> (match nd.nodei_prototype with Some "C" -> true | _ -> false)
290
(*
291
let pp_instance_call dependencies m self fmt i (inputs: value_t list) (outputs: var_decl list) =
292
  try (* stateful node instance *)
293
    let (n,_) = List.assoc i m.minstances in
294
    let (input_types, _) = Typing.get_type_of_call n in
295
    let inputs = List.combine input_types inputs in
296
    fprintf fmt "%a (%a%t%a%t%s->%s);"
297
      pp_machine_step_name (node_name n)
298
      (Utils.fprintf_list ~sep:", " (pp_c_val self (pp_c_var_read m))) inputs
299
      (Utils.pp_final_char_if_non_empty ", " inputs) 
300
      (Utils.fprintf_list ~sep:", " (pp_c_var_write m)) outputs
301
      (Utils.pp_final_char_if_non_empty ", " outputs)
302
      self
303
      i
304
  with Not_found -> (* stateless node instance *)
305
    let (n,_) = List.assoc i m.mcalls in
306
    let (input_types, output_types) = Typing.get_type_of_call n in
307
    let inputs = List.combine input_types inputs in
308
    if has_c_prototype i dependencies
309
    then (* external C function *)
310
      let outputs = List.map2 (fun t v -> t, LocalVar v) output_types outputs in
311
      fprintf fmt "%a = %s(%a);"
312
	(Utils.fprintf_list ~sep:", " (pp_c_val self (pp_c_var_read m))) outputs
313
	i
314
	(Utils.fprintf_list ~sep:", " (pp_c_val self (pp_c_var_read m))) inputs
315
    else
316
      fprintf fmt "%a (%a%t%a);"
317
	pp_machine_step_name (node_name n)
318
	(Utils.fprintf_list ~sep:", " (pp_c_val self (pp_c_var_read m))) inputs
319
	(Utils.pp_final_char_if_non_empty ", " inputs) 
320
	(Utils.fprintf_list ~sep:", " (pp_c_var_write m)) outputs 
321
*)
322
let rec pp_conditional dependencies (m: machine_t) self fmt c tl el =
323
  fprintf fmt "@[<v 2>if (%a) {%t%a@]@,@[<v 2>} else {%t%a@]@,}"
324
    (pp_c_val self (pp_c_var_read m)) c
325
    (Utils.pp_newline_if_non_empty tl)
326
    (Utils.fprintf_list ~sep:"@," (pp_machine_instr dependencies m self)) tl
327
    (Utils.pp_newline_if_non_empty el)
328
    (Utils.fprintf_list ~sep:"@," (pp_machine_instr dependencies m self)) el
329

    
330
and pp_machine_instr dependencies (m: machine_t) self fmt instr =
331
  match get_instr_desc instr with 
332
  | MNoReset _ -> ()
333
  | MReset i ->
334
    pp_machine_reset m self fmt i
335
  | MLocalAssign (i,v) ->
336
    pp_assign
337
      m self (pp_c_var_read m) fmt
338
      i.var_type (mk_val (LocalVar i) i.var_type) v
339
  | MStateAssign (i,v) ->
340
    pp_assign
341
      m self (pp_c_var_read m) fmt
342
      i.var_type (mk_val (StateVar i) i.var_type) v
343
  | MStep ([i0], i, vl) when Basic_library.is_value_internal_fun (mk_val (Fun (i, vl)) i0.var_type)  ->
344
    pp_machine_instr dependencies m self fmt 
345
      (update_instr_desc instr (MLocalAssign (i0, mk_val (Fun (i, vl)) i0.var_type)))
346
  | MStep ([i0], i, vl) when has_c_prototype i dependencies -> 
347
    fprintf fmt "%a = %s(%a);" 
348
      (pp_c_val self (pp_c_var_read m)) (mk_val (LocalVar i0) i0.var_type)
349
      i
350
      (Utils.fprintf_list ~sep:", " (pp_c_val self (pp_c_var_read m))) vl
351
  | MStep (il, i, vl) when Mpfr.is_homomorphic_fun i ->
352
    pp_instance_call m self fmt i vl il
353
  | MStep (il, i, vl) ->
354
    pp_basic_instance_call m self fmt i vl il
355
  | MBranch (_, []) -> (Format.eprintf "internal error: C_backend_src.pp_machine_instr %a@." pp_instr instr; assert false)
356
  | MBranch (g, hl) ->
357
    if let t = fst (List.hd hl) in t = tag_true || t = tag_false
358
    then (* boolean case, needs special treatment in C because truth value is not unique *)
359
	 (* may disappear if we optimize code by replacing last branch test with default *)
360
      let tl = try List.assoc tag_true  hl with Not_found -> [] in
361
      let el = try List.assoc tag_false hl with Not_found -> [] in
362
      pp_conditional dependencies m self fmt g tl el
363
    else (* enum type case *)
364
      (*let g_typ = Typing.type_const Location.dummy_loc (Const_tag (fst (List.hd hl))) in*)
365
      fprintf fmt "@[<v 2>switch(%a) {@,%a@,}@]"
366
	(pp_c_val self (pp_c_var_read m)) g
367
	(Utils.fprintf_list ~sep:"@," (pp_machine_branch dependencies m self)) hl
368
  | MComment s  -> 
369
      fprintf fmt "/*%s*/@ " s
370

    
371

    
372
and pp_machine_branch dependencies m self fmt (t, h) =
373
  fprintf fmt "@[<v 2>case %a:@,%a@,break;@]"
374
    pp_c_tag t
375
    (Utils.fprintf_list ~sep:"@," (pp_machine_instr dependencies m self)) h
376

    
377

    
378
(********************************************************************************************)
379
(*                         C file Printing functions                                        *)
380
(********************************************************************************************)
381

    
382
let print_const_def fmt cdecl =
383
  if !Options.mpfr && Types.is_real_type (Types.array_base_type cdecl.const_type)
384
  then
385
    fprintf fmt "%a;@." 
386
      (pp_c_type cdecl.const_id) (Types.dynamic_type cdecl.const_type) 
387
  else
388
    fprintf fmt "%a = %a;@." 
389
      (pp_c_type cdecl.const_id) cdecl.const_type
390
      pp_c_const cdecl.const_value 
391

    
392

    
393
let print_alloc_instance fmt (i, (m, static)) =
394
  fprintf fmt "_alloc->%s = %a (%a);@,"
395
    i
396
    pp_machine_alloc_name (node_name m)
397
    (Utils.fprintf_list ~sep:", " Dimension.pp_dimension) static
398

    
399
let print_dealloc_instance fmt (i, (m, _)) =
400
  fprintf fmt "%a (_alloc->%s);@,"
401
    pp_machine_dealloc_name (node_name m)
402
    i
403

    
404
let print_alloc_const fmt m =
405
  let const_locals = List.filter (fun vdecl -> vdecl.var_dec_const) m.mstep.step_locals in
406
  fprintf fmt "%a%t"
407
    (Utils.fprintf_list ~sep:";@," (pp_c_decl_local_var m)) const_locals
408
    (Utils.pp_final_char_if_non_empty ";@," const_locals)
409

    
410
let print_alloc_array fmt vdecl =
411
  let base_type = Types.array_base_type vdecl.var_type in
412
  let size_types = Types.array_type_multi_dimension vdecl.var_type in
413
  let size_type = Dimension.multi_dimension_product vdecl.var_loc size_types in
414
  fprintf fmt "_alloc->_reg.%s = (%a*) malloc((%a)*sizeof(%a));@,assert(_alloc->%s);@,"
415
    vdecl.var_id
416
    (pp_c_type "") base_type
417
    Dimension.pp_dimension size_type
418
    (pp_c_type "") base_type
419
    vdecl.var_id
420

    
421
let print_dealloc_array fmt vdecl =
422
  fprintf fmt "free (_alloc->_reg.%s);@,"
423
    vdecl.var_id
424

    
425
let print_alloc_code fmt m =
426
  let array_mem = List.filter (fun v -> Types.is_array_type v.var_type) m.mmemory in
427
  fprintf fmt "%a *_alloc;@,_alloc = (%a *) malloc(sizeof(%a));@,assert(_alloc);@,%a%areturn _alloc;"
428
    pp_machine_memtype_name m.mname.node_id
429
    pp_machine_memtype_name m.mname.node_id
430
    pp_machine_memtype_name m.mname.node_id
431
    (Utils.fprintf_list ~sep:"" print_alloc_array) array_mem
432
    (Utils.fprintf_list ~sep:"" print_alloc_instance) m.minstances
433

    
434
let print_dealloc_code fmt m =
435
  let array_mem = List.filter (fun v -> Types.is_array_type v.var_type) m.mmemory in
436
  fprintf fmt "%a%afree (_alloc);@,return;"
437
    (Utils.fprintf_list ~sep:"" print_dealloc_array) array_mem
438
    (Utils.fprintf_list ~sep:"" print_dealloc_instance) m.minstances
439

    
440
let print_stateless_init_code dependencies fmt m self =
441
  let minit = List.map (fun i -> match get_instr_desc i with MReset i -> i | _ -> assert false) m.minit in
442
  let array_mems = List.filter (fun v -> Types.is_array_type v.var_type) m.mmemory in
443
  fprintf fmt "@[<v 2>%a {@,%a%t%a%t%a%treturn;@]@,}@.@."
444
    (print_init_prototype self) (m.mname.node_id, m.mstatic)
445
    (* array mems *) 
446
    (Utils.fprintf_list ~sep:";@," (pp_c_decl_array_mem self)) array_mems
447
    (Utils.pp_final_char_if_non_empty ";@," array_mems)
448
    (* memory initialization *)
449
    (Utils.fprintf_list ~sep:"@," (pp_initialize m self (pp_c_var_read m))) m.mmemory
450
    (Utils.pp_newline_if_non_empty m.mmemory)
451
    (* sub-machines initialization *)
452
    (Utils.fprintf_list ~sep:"@," (pp_machine_init m self)) minit
453
    (Utils.pp_newline_if_non_empty m.minit)
454

    
455
let print_stateless_clear_code dependencies fmt m self =
456
  let minit = List.map (fun i -> match get_instr_desc i with MReset i -> i | _ -> assert false) m.minit in
457
  let array_mems = List.filter (fun v -> Types.is_array_type v.var_type) m.mmemory in
458
  fprintf fmt "@[<v 2>%a {@,%a%t%a%t%a%treturn;@]@,}@.@."
459
    (print_clear_prototype self) (m.mname.node_id, m.mstatic)
460
    (* array mems *)
461
    (Utils.fprintf_list ~sep:";@," (pp_c_decl_array_mem self)) array_mems
462
    (Utils.pp_final_char_if_non_empty ";@," array_mems)
463
    (* memory clear *)
464
    (Utils.fprintf_list ~sep:"@," (pp_clear m self (pp_c_var_read m))) m.mmemory
465
    (Utils.pp_newline_if_non_empty m.mmemory)
466
    (* sub-machines clear*)
467
    (Utils.fprintf_list ~sep:"@," (pp_machine_clear m self)) minit
468
    (Utils.pp_newline_if_non_empty m.minit)
469

    
470
let print_stateless_code dependencies fmt m =
471
  let self = "__ERROR__" in
472
  if not (!Options.ansi && is_generic_node { top_decl_desc = Node m.mname; top_decl_loc = Location.dummy_loc; top_decl_owner = ""; top_decl_itf = false })
473
  then
474
    (* C99 code *)
475
    fprintf fmt "@[<v 2>%a {@,%a%t%a%t@,%a%a%t%a%t%t@]@,}@.@."
476
      print_stateless_prototype (m.mname.node_id, m.mstep.step_inputs, m.mstep.step_outputs)
477
      (* locals *)
478
      (Utils.fprintf_list ~sep:";@," (pp_c_decl_local_var m)) m.mstep.step_locals
479
      (Utils.pp_final_char_if_non_empty ";@," m.mstep.step_locals)
480
      (* locals initialization *)
481
      (Utils.fprintf_list ~sep:"@," (pp_initialize m self (pp_c_var_read m))) m.mstep.step_locals
482
      (Utils.pp_newline_if_non_empty m.mstep.step_locals)
483
      (* check assertions *)
484
      (pp_c_checks self) m
485
      (* instrs *)
486
      (Utils.fprintf_list ~sep:"@," (pp_machine_instr dependencies m self)) m.mstep.step_instrs
487
      (Utils.pp_newline_if_non_empty m.mstep.step_instrs)
488
      (* locals clear *)
489
      (Utils.fprintf_list ~sep:"@," (pp_clear m self (pp_c_var_read m))) m.mstep.step_locals
490
      (Utils.pp_newline_if_non_empty m.mstep.step_locals)
491
      (fun fmt -> fprintf fmt "return;")
492
  else
493
    (* C90 code *)
494
    let (gen_locals, base_locals) = List.partition (fun v -> Types.is_generic_type v.var_type) m.mstep.step_locals in
495
    let gen_calls = List.map (fun e -> let (id, _, _) = call_of_expr e in mk_call_var_decl e.expr_loc id) m.mname.node_gencalls in
496
    fprintf fmt "@[<v 2>%a {@,%a%t%a%t@,%a%a%t%a%t%t@]@,}@.@."
497
      print_stateless_prototype (m.mname.node_id, (m.mstep.step_inputs@gen_locals@gen_calls), m.mstep.step_outputs)
498
      (* locals *)
499
      (Utils.fprintf_list ~sep:";@," (pp_c_decl_local_var m)) base_locals
500
      (Utils.pp_final_char_if_non_empty ";" base_locals)
501
      (* locals initialization *)
502
      (Utils.fprintf_list ~sep:"@," (pp_initialize m self (pp_c_var_read m))) m.mstep.step_locals
503
      (Utils.pp_newline_if_non_empty m.mstep.step_locals)
504
      (* check assertions *)
505
      (pp_c_checks self) m
506
      (* instrs *)
507
      (Utils.fprintf_list ~sep:"@," (pp_machine_instr dependencies m self)) m.mstep.step_instrs
508
      (Utils.pp_newline_if_non_empty m.mstep.step_instrs)
509
      (* locals clear *)
510
      (Utils.fprintf_list ~sep:"@," (pp_clear m self (pp_c_var_read m))) m.mstep.step_locals
511
      (Utils.pp_newline_if_non_empty m.mstep.step_locals)
512
      (fun fmt -> fprintf fmt "return;")
513

    
514
let print_reset_code dependencies fmt m self =
515
  let const_locals = List.filter (fun vdecl -> vdecl.var_dec_const) m.mstep.step_locals in
516
  fprintf fmt "@[<v 2>%a {@,%a%t@,%a%treturn;@]@,}@.@."
517
    (print_reset_prototype self) (m.mname.node_id, m.mstatic)
518
    (* constant locals decl *)
519
    (Utils.fprintf_list ~sep:";@," (pp_c_decl_local_var m)) const_locals
520
    (Utils.pp_final_char_if_non_empty ";" const_locals)
521
    (* instrs *)
522
    (Utils.fprintf_list ~sep:"@," (pp_machine_instr dependencies m self)) m.minit
523
    (Utils.pp_newline_if_non_empty m.minit)
524

    
525
let print_init_code dependencies fmt m self =
526
  let minit = List.map (fun i -> match get_instr_desc i with MReset i -> i | _ -> assert false) m.minit in
527
  let array_mems = List.filter (fun v -> Types.is_array_type v.var_type) m.mmemory in
528
  fprintf fmt "@[<v 2>%a {@,%a%t%a%t%a%treturn;@]@,}@.@."
529
    (print_init_prototype self) (m.mname.node_id, m.mstatic)
530
    (* array mems *) 
531
    (Utils.fprintf_list ~sep:";@," (pp_c_decl_array_mem self)) array_mems
532
    (Utils.pp_final_char_if_non_empty ";@," array_mems)
533
    (* memory initialization *)
534
    (Utils.fprintf_list ~sep:"@," (pp_initialize m self (pp_c_var_read m))) m.mmemory
535
    (Utils.pp_newline_if_non_empty m.mmemory)
536
    (* sub-machines initialization *)
537
    (Utils.fprintf_list ~sep:"@," (pp_machine_init m self)) minit
538
    (Utils.pp_newline_if_non_empty m.minit)
539

    
540
let print_clear_code dependencies fmt m self =
541
  let minit = List.map (fun i -> match get_instr_desc i with MReset i -> i | _ -> assert false) m.minit in
542
  let array_mems = List.filter (fun v -> Types.is_array_type v.var_type) m.mmemory in
543
  fprintf fmt "@[<v 2>%a {@,%a%t%a%t%a%treturn;@]@,}@.@."
544
    (print_clear_prototype self) (m.mname.node_id, m.mstatic)
545
    (* array mems *)
546
    (Utils.fprintf_list ~sep:";@," (pp_c_decl_array_mem self)) array_mems
547
    (Utils.pp_final_char_if_non_empty ";@," array_mems)
548
    (* memory clear *)
549
    (Utils.fprintf_list ~sep:"@," (pp_clear m self (pp_c_var_read m))) m.mmemory
550
    (Utils.pp_newline_if_non_empty m.mmemory)
551
    (* sub-machines clear*)
552
    (Utils.fprintf_list ~sep:"@," (pp_machine_clear m self)) minit
553
    (Utils.pp_newline_if_non_empty m.minit)
554

    
555
let print_step_code dependencies fmt m self =
556
  if not (!Options.ansi && is_generic_node { top_decl_desc = Node m.mname; top_decl_loc = Location.dummy_loc; top_decl_owner = ""; top_decl_itf = false })
557
  then
558
    (* C99 code *)
559
    let array_mems = List.filter (fun v -> Types.is_array_type v.var_type) m.mmemory in
560
    fprintf fmt "@[<v 2>%a {@,%a%t%a%t%a%t@,%a%a%t%a%t%t@]@,}@.@."
561
      (print_step_prototype self) (m.mname.node_id, m.mstep.step_inputs, m.mstep.step_outputs)
562
      (* locals *)
563
      (Utils.fprintf_list ~sep:";@," (pp_c_decl_local_var m)) m.mstep.step_locals
564
      (Utils.pp_final_char_if_non_empty ";@," m.mstep.step_locals)
565
      (* array mems *)
566
      (Utils.fprintf_list ~sep:";@," (pp_c_decl_array_mem self)) array_mems
567
      (Utils.pp_final_char_if_non_empty ";@," array_mems)
568
      (* locals initialization *)
569
      (Utils.fprintf_list ~sep:"@," (pp_initialize m self (pp_c_var_read m))) m.mstep.step_locals
570
      (Utils.pp_newline_if_non_empty m.mstep.step_locals)
571
      (* check assertions *)
572
      (pp_c_checks self) m
573
      (* instrs *)
574
      (Utils.fprintf_list ~sep:"@," (pp_machine_instr dependencies m self)) m.mstep.step_instrs
575
      (Utils.pp_newline_if_non_empty m.mstep.step_instrs)
576
      (* locals clear *)
577
      (Utils.fprintf_list ~sep:"@," (pp_clear m self (pp_c_var_read m))) m.mstep.step_locals
578
      (Utils.pp_newline_if_non_empty m.mstep.step_locals)
579
      (fun fmt -> fprintf fmt "return;")
580
  else
581
    (* C90 code *)
582
    let (gen_locals, base_locals) = List.partition (fun v -> Types.is_generic_type v.var_type) m.mstep.step_locals in
583
    let gen_calls = List.map (fun e -> let (id, _, _) = call_of_expr e in mk_call_var_decl e.expr_loc id) m.mname.node_gencalls in
584
    fprintf fmt "@[<v 2>%a {@,%a%t%a%t@,%a%a%t%a%t%t@]@,}@.@."
585
      (print_step_prototype self) (m.mname.node_id, (m.mstep.step_inputs@gen_locals@gen_calls), m.mstep.step_outputs)
586
      (* locals *)
587
      (Utils.fprintf_list ~sep:";@," (pp_c_decl_local_var m)) base_locals
588
      (Utils.pp_final_char_if_non_empty ";" base_locals)
589
      (* locals initialization *)
590
      (Utils.fprintf_list ~sep:"@," (pp_initialize m self (pp_c_var_read m))) m.mstep.step_locals
591
      (Utils.pp_newline_if_non_empty m.mstep.step_locals)
592
      (* check assertions *)
593
      (pp_c_checks self) m
594
      (* instrs *)
595
      (Utils.fprintf_list ~sep:"@," (pp_machine_instr dependencies m self)) m.mstep.step_instrs
596
      (Utils.pp_newline_if_non_empty m.mstep.step_instrs)
597
      (* locals clear *)
598
      (Utils.fprintf_list ~sep:"@," (pp_clear m self (pp_c_var_read m))) m.mstep.step_locals
599
      (Utils.pp_newline_if_non_empty m.mstep.step_locals)
600
      (fun fmt -> fprintf fmt "return;")
601

    
602

    
603
(********************************************************************************************)
604
(*                     MAIN C file Printing functions                                       *)
605
(********************************************************************************************)
606

    
607
let print_global_init_code fmt basename prog dependencies =
608
  let baseNAME = file_to_module_name basename in
609
  let constants = List.map const_of_top (get_consts prog) in
610
  fprintf fmt "@[<v 2>%a {@,static %s init = 0;@,@[<v 2>if (!init) { @,init = 1;@,%a%t%a@]@,}@,return;@]@,}@.@."
611
    print_global_init_prototype baseNAME
612
    (pp_c_basic_type_desc Type_predef.type_bool)
613
    (* constants *) 
614
    (Utils.fprintf_list ~sep:"@," (pp_const_initialize (pp_c_var_read Machine_code.empty_machine))) constants
615
    (Utils.pp_final_char_if_non_empty "@," dependencies)
616
    (* dependencies initialization *)
617
    (Utils.fprintf_list ~sep:"@," print_import_init) dependencies
618

    
619
let print_global_clear_code  fmt basename prog dependencies =
620
  let baseNAME = file_to_module_name basename in
621
  let constants = List.map const_of_top (get_consts prog) in
622
  fprintf fmt "@[<v 2>%a {@,static %s clear = 0;@,@[<v 2>if (!clear) { @,clear = 1;@,%a%t%a@]@,}@,return;@]@,}@.@."
623
    print_global_clear_prototype baseNAME
624
    (pp_c_basic_type_desc Type_predef.type_bool)
625
    (* constants *) 
626
    (Utils.fprintf_list ~sep:"@," (pp_const_clear (pp_c_var_read Machine_code.empty_machine))) constants
627
    (Utils.pp_final_char_if_non_empty "@," dependencies)
628
    (* dependencies initialization *)
629
    (Utils.fprintf_list ~sep:"@," print_import_clear) dependencies
630

    
631
let print_machine dependencies fmt m =
632
  if fst (get_stateless_status m) then
633
    begin
634
      (* Step function *)
635
      print_stateless_code dependencies fmt m
636
    end
637
  else
638
    begin
639
      (* Alloc functions, only if non static mode *)
640
      if (not !Options.static_mem) then  
641
	begin
642
	  fprintf fmt "@[<v 2>%a {@,%a%a@]@,}@.@."
643
	    print_alloc_prototype (m.mname.node_id, m.mstatic)
644
	    print_alloc_const m
645
	    print_alloc_code m;
646

    
647
	  fprintf fmt "@[<v 2>%a {@,%a%a@]@,}@.@."
648
	    print_dealloc_prototype m.mname.node_id
649
	    print_alloc_const m
650
	    print_dealloc_code m;
651
	end;
652
      let self = mk_self m in
653
      (* Reset function *)
654
      print_reset_code dependencies fmt m self;
655
      (* Step function *)
656
      print_step_code dependencies fmt m self;
657
      
658
      if !Options.mpfr then
659
	begin
660
          (* Init function *)
661
	  print_init_code dependencies fmt m self;
662
          (* Clear function *)
663
	  print_clear_code dependencies fmt m self;
664
	end
665
    end
666

    
667
let print_import_standard source_fmt =
668
  begin
669
    fprintf source_fmt "#include <assert.h>@.";
670
    if Machine_types.has_machine_type () then
671
      begin
672
	fprintf source_fmt "#include <stdint.h>@."
673
      end;
674
    if not !Options.static_mem then
675
      begin
676
	fprintf source_fmt "#include <stdlib.h>@.";
677
      end;
678
    if !Options.mpfr then
679
      begin
680
	fprintf source_fmt "#include <mpfr.h>@.";
681
      end
682
  end
683

    
684
let print_lib_c source_fmt basename prog machines dependencies =
685
  print_import_standard source_fmt;
686
  print_import_prototype source_fmt (Dep (true, basename, [], true (* assuming it is stateful *)));
687
  pp_print_newline source_fmt ();
688
  (* Print the svn version number and the supported C standard (C90 or C99) *)
689
  print_version source_fmt;
690
  (* Print the prototype of imported nodes *)
691
  fprintf source_fmt "/* Import dependencies */@.";
692
  fprintf source_fmt "@[<v>";
693
  List.iter (print_import_prototype source_fmt) dependencies;
694
  fprintf source_fmt "@]@.";
695
  (* Print consts *)
696
  fprintf source_fmt "/* Global constants (definitions) */@.";
697
  fprintf source_fmt "@[<v>";
698
  List.iter (fun c -> print_const_def source_fmt (const_of_top c)) (get_consts prog);
699
  fprintf source_fmt "@]@.";
700
  if !Options.mpfr then
701
    begin
702
      fprintf source_fmt "/* Global constants initialization */@.";
703
      print_global_init_code source_fmt basename prog dependencies;
704
      fprintf source_fmt "/* Global constants clearing */@.";
705
      print_global_clear_code source_fmt basename prog dependencies;
706
    end;
707
  if not !Options.static_mem then
708
    begin
709
      fprintf source_fmt "/* External allocation function prototypes */@.";
710
      fprintf source_fmt "@[<v>";
711
      List.iter (print_extern_alloc_prototypes source_fmt) dependencies;
712
      fprintf source_fmt "@]@.";
713
      fprintf source_fmt "/* Node allocation function prototypes */@.";
714
      fprintf source_fmt "@[<v>";
715
      List.iter
716
	(fun m -> fprintf source_fmt "%a;@.@.%a;@.@."
717
	  print_alloc_prototype (m.mname.node_id, m.mstatic)
718
	  print_dealloc_prototype m.mname.node_id
719
	)
720
	machines;
721
      fprintf source_fmt "@]@.";
722
    end;
723

    
724
  (* Print the struct definitions of all machines. *)
725
  fprintf source_fmt "/* Struct definitions */@.";
726
  fprintf source_fmt "@[<v>";
727
  List.iter (print_machine_struct source_fmt) machines;
728
  fprintf source_fmt "@]@.";
729
  pp_print_newline source_fmt ();
730
  (* Print nodes one by one (in the previous order) *)
731
  List.iter (print_machine dependencies source_fmt) machines;
732
 end
733

    
734
(* Local Variables: *)
735
(* compile-command:"make -C ../../.." *)
736
(* End: *)