Project

General

Profile

Statistics
| Branch: | Tag: | Revision:

lustrec / src / backends / C / c_backend_src.ml @ 79614a15

History | View | Annotate | Download (22.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 LustreSpec
14
open Corelang
15
open Machine_code
16
open C_backend_common
17

    
18
module type MODIFIERS_SRC =
19
sig
20
end
21

    
22
module EmptyMod =
23
struct
24
end
25

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

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

    
33

    
34
(* Computes the depth to which multi-dimension array assignments should be expanded.
35
   It equals the maximum number of nested static array constructions accessible from root [v].
36
*)
37
let rec expansion_depth v =
38
 match v with
39
 | Cst (Const_array cl) -> 1 + List.fold_right (fun c -> max (expansion_depth (Cst c))) cl 0
40
 | 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

    
48
let rec merge_static_loop_profiles lp1 lp2 =
49
  match lp1, lp2 with
50
  | []      , _        -> lp2
51
  | _       , []       -> lp1
52
  | p1 :: q1, p2 :: q2 -> (p1 || p2) :: merge_static_loop_profiles q1 q2
53

    
54
(* Returns a list of bool values, indicating whether the indices must be static or not *)
55
let rec static_loop_profile v =
56
 match v with
57
 | Cst (Const_array cl) ->
58
   List.fold_right (fun c lp -> merge_static_loop_profiles lp (static_loop_profile (Cst c))) cl []
59
 | Cst _
60
 | LocalVar _
61
 | StateVar _  -> []
62
 | Fun (_, vl) -> List.fold_right (fun v lp -> merge_static_loop_profiles lp (static_loop_profile v)) vl []
63
 | Array vl    -> true :: List.fold_right (fun v lp -> merge_static_loop_profiles lp (static_loop_profile v)) vl []
64
 | Access (v, i) -> (match (static_loop_profile v) with [] -> [] | _ :: q -> q)
65
 | Power (v, n)  -> false :: static_loop_profile v
66

    
67
let rec is_const_index v =
68
  match v with
69
  | Cst (Const_int _) -> true
70
  | Fun (_, vl)       -> List.for_all is_const_index vl
71
  | _                 -> false
72

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

    
100
let reorder_loop_variables loop_vars =
101
  let (int_loops, var_loops) = 
102
    List.partition (function (d, LInt _) -> true | _ -> false) loop_vars 
103
  in
104
  var_loops @ int_loops
105

    
106
(* Prints a one loop variable suffix for arrays *)
107
let pp_loop_var pp_value fmt lv =
108
 match snd lv with
109
 | LVar v -> fprintf fmt "[%s]" v
110
 | LInt r -> fprintf fmt "[%d]" !r
111
 | LAcc i -> fprintf fmt "[%a]" (pp_c_val "" pp_value) i
112

    
113
(* Prints a suffix of loop variables for arrays *)
114
let pp_suffix pp_value fmt loop_vars =
115
 Utils.fprintf_list ~sep:"" (pp_loop_var pp_value) fmt loop_vars
116

    
117
(* Prints a value expression [v], with internal function calls only.
118
   [pp_var] is a printer for variables (typically [pp_c_var_read]),
119
   but an offset suffix may be added for array variables
120
*)
121
(* Prints a constant value before a suffix (needs casting) *)
122
let rec pp_c_const_suffix var_type fmt c =
123
  match c with
124
    | Const_int i     -> pp_print_int fmt i
125
    | Const_real r    -> pp_print_string fmt r
126
    | Const_float r   -> pp_print_float fmt r
127
    | Const_tag t     -> pp_c_tag fmt t
128
    | Const_array ca  -> let var_type = Types.array_element_type var_type in
129
                         fprintf fmt "(%a[]){%a }" (pp_c_type "") var_type (Utils.fprintf_list ~sep:", " (pp_c_const_suffix var_type)) ca
130
    | 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
131
    | Const_string _ -> assert false (* string occurs in annotations not in C *)
132

    
133

    
134
(* Prints a [value] of type [var_type] indexed by the suffix list [loop_vars] *)
135
let rec pp_value_suffix self var_type loop_vars pp_value fmt value =
136
(*Format.eprintf "pp_value_suffix: %a %a %a@." Types.print_ty var_type Machine_code.pp_val value pp_suffix loop_vars;*)
137
 match loop_vars, value with
138
 | (x, LAcc i) :: q, _ when is_const_index i ->
139
   let r = ref (Dimension.size_const_dimension (Machine_code.dimension_of_value i)) in
140
   pp_value_suffix self var_type ((x, LInt r)::q) pp_value fmt value
141
 | (_, LInt r) :: q, Cst (Const_array cl) ->
142
   let var_type = Types.array_element_type var_type in
143
   pp_value_suffix self var_type q pp_value fmt (Cst (List.nth cl !r))
144
 | (_, LInt r) :: q, Array vl      ->
145
   let var_type = Types.array_element_type var_type in
146
   pp_value_suffix self var_type q pp_value fmt (List.nth vl !r)
147
 | loop_var    :: q, Array vl      ->
148
   let var_type = Types.array_element_type var_type in
149
   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 pp_value) [loop_var]
150
 | _           :: q, Power (v, n)  ->
151
   pp_value_suffix self var_type q pp_value fmt v
152
 | _               , Fun (n, vl)   ->
153
   Basic_library.pp_c n (pp_value_suffix self var_type loop_vars pp_value) fmt vl
154
 | _               , Access (v, i) ->
155
   let var_type = Type_predef.type_array (Dimension.mkdim_var ()) var_type in
156
   pp_value_suffix self var_type ((Dimension.mkdim_var (), LAcc i) :: loop_vars) pp_value fmt v
157
 | _               , LocalVar v    -> Format.fprintf fmt "%a%a" pp_value v (pp_suffix pp_value) loop_vars
158
 | _               , StateVar v    ->
159
    (* array memory vars are represented by an indirection to a local var with the right type,
160
       in order to avoid casting everywhere. *)
161
   if Types.is_array_type v.var_type
162
   then Format.fprintf fmt "%a%a" pp_value v (pp_suffix pp_value) loop_vars
163
   else Format.fprintf fmt "%s->_reg.%a%a" self pp_value v (pp_suffix pp_value) loop_vars
164
 | _               , Cst cst       -> pp_c_const_suffix var_type fmt cst
165
 | _               , _             -> (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 pp_value) loop_vars; assert false)
166

    
167
(* type_directed assignment: array vs. statically sized type
168
   - [var_type]: type of variable to be assigned
169
   - [var_name]: name of variable to be assigned
170
   - [value]: assigned value
171
   - [pp_var]: printer for variables
172
*)
173
(*
174
let pp_assign_rec pp_var var_type var_name value =
175
  match (Types.repr var_type).Types.tdesc, value with
176
  | Types.Tarray (d, ty'), Array vl     ->
177
    let szl = Utils.enumerate (Dimension.size_const_dimension d) in
178
    fprintf fmt "@[<v 2>{@,%a@]@,}"
179
      (Utils.fprintf_list ~sep:"@," (fun fmt i -> r := i; aux fmt q)) szl
180
  | Types.Tarray (d, ty'), Power (v, _) -> 
181
  | Types.Tarray (d, ty'), _            ->
182
  | _                    , _            ->
183
    fprintf fmt "%a = %a;" 
184
      pp_var var_name
185
      (pp_value_suffix self loop_vars pp_var) value
186
*)
187
let pp_assign m self pp_var fmt var_type var_name value =
188
  let depth = expansion_depth value in
189
(*Format.eprintf "pp_assign %a %a %a %d@." Types.print_ty var_type pp_val var_name pp_val value depth;*)
190
  let loop_vars = mk_loop_variables m var_type depth in
191
  let reordered_loop_vars = reorder_loop_variables loop_vars in
192
  let rec aux fmt vars =
193
    match vars with
194
    | [] ->
195
      fprintf fmt "%a = %a;" 
196
	(pp_value_suffix self var_type loop_vars pp_var) var_name
197
	(pp_value_suffix self var_type loop_vars pp_var) value
198
    | (d, LVar i) :: q ->
199
(*eprintf "pp_aux %a %s@." Dimension.pp_dimension d i;*)
200
      fprintf fmt "@[<v 2>{@,int %s;@,for(%s=0;%s<%a;%s++)@,%a @]@,}"
201
	i i i Dimension.pp_dimension d i
202
	aux q
203
    | (d, LInt r) :: q ->
204
(*eprintf "pp_aux %a %d@." Dimension.pp_dimension d (!r);*)
205
      let szl = Utils.enumerate (Dimension.size_const_dimension d) in
206
      fprintf fmt "@[<v 2>{@,%a@]@,}"
207
	(Utils.fprintf_list ~sep:"@," (fun fmt i -> r := i; aux fmt q)) szl
208
    | _ -> assert false
209
  in
210
  begin
211
    reset_loop_counter ();
212
    (*reset_addr_counter ();*)
213
    aux fmt reordered_loop_vars
214
  end
215

    
216
let pp_instance_call m self fmt i (inputs: value_t list) (outputs: var_decl list) =
217
 try (* stateful node instance *)
218
   let (n,_) = List.assoc i m.minstances in
219
   fprintf fmt "%a (%a%t%a%t%s->%s);"
220
     pp_machine_step_name (node_name n)
221
     (Utils.fprintf_list ~sep:", " (pp_c_val self (pp_c_var_read m))) inputs
222
     (Utils.pp_final_char_if_non_empty ", " inputs) 
223
     (Utils.fprintf_list ~sep:", " (pp_c_var_write m)) outputs
224
     (Utils.pp_final_char_if_non_empty ", " outputs)
225
     self
226
     i
227
 with Not_found -> (* stateless node instance *)
228
   let (n,_) = List.assoc i m.mcalls in
229
   fprintf fmt "%a (%a%t%a);"
230
     pp_machine_step_name (node_name n)
231
     (Utils.fprintf_list ~sep:", " (pp_c_val self (pp_c_var_read m))) inputs
232
     (Utils.pp_final_char_if_non_empty ", " inputs) 
233
     (Utils.fprintf_list ~sep:", " (pp_c_var_write m)) outputs 
234

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

    
246
let has_c_prototype funname dependencies =
247
  let imported_node_opt = (* We select the last imported node with the name funname.
248
			       The order of evaluation of dependencies should be
249
			       compatible with overloading. (Not checked yet) *) 
250
      List.fold_left
251
	(fun res (Dep (_, _, decls, _)) -> 
252
	  match res with
253
	  | Some _ -> res
254
	  | None -> 
255
	    let matched = fun t -> match t.top_decl_desc with 
256
	      | ImportedNode nd -> nd.nodei_id = funname 
257
	      | _ -> false
258
	    in
259
	    if List.exists matched decls then (
260
	      match (List.find matched decls).top_decl_desc with
261
	      | ImportedNode nd -> Some nd
262
	      | _ -> assert false
263
	    )
264
	    else
265
	      None
266
	) None dependencies in
267
    match imported_node_opt with
268
    | None -> false
269
    | Some nd -> (match nd.nodei_prototype with Some "C" -> true | _ -> false)
270

    
271
let rec pp_conditional dependencies (m: machine_t) self fmt c tl el =
272
  fprintf fmt "@[<v 2>if (%a) {%t%a@]@,@[<v 2>} else {%t%a@]@,}"
273
    (pp_c_val self (pp_c_var_read m)) c
274
    (Utils.pp_newline_if_non_empty tl)
275
    (Utils.fprintf_list ~sep:"@," (pp_machine_instr dependencies m self)) tl
276
    (Utils.pp_newline_if_non_empty el)
277
    (Utils.fprintf_list ~sep:"@," (pp_machine_instr dependencies m self)) el
278

    
279
and pp_machine_instr dependencies (m: machine_t) self fmt instr =
280
  match instr with 
281
  | MReset i ->
282
    pp_machine_reset m self fmt i
283
  | MLocalAssign (i,v) ->
284
    pp_assign
285
      m self (pp_c_var_read m) fmt
286
      i.var_type (LocalVar i) v
287
  | MStateAssign (i,v) ->
288
    pp_assign
289
      m self (pp_c_var_read m) fmt
290
      i.var_type (StateVar i) v
291
  | MStep ([i0], i, vl) when Basic_library.is_internal_fun i  ->
292
    pp_machine_instr dependencies m self fmt (MLocalAssign (i0, Fun (i, vl)))
293
  | MStep ([i0], i, vl) when has_c_prototype i dependencies -> 
294
    fprintf fmt "%a = %s(%a);" 
295
      (pp_c_val self (pp_c_var_read m)) (LocalVar i0) 
296
      i
297
      (Utils.fprintf_list ~sep:", " (pp_c_val self (pp_c_var_read m))) vl
298
  | MStep (il, i, vl) ->
299
    pp_instance_call m self fmt i vl il
300
  | MBranch (g,hl) ->
301
    if hl <> [] && let t = fst (List.hd hl) in t = tag_true || t = tag_false
302
    then (* boolean case, needs special treatment in C because truth value is not unique *)
303
	 (* may disappear if we optimize code by replacing last branch test with default *)
304
      let tl = try List.assoc tag_true  hl with Not_found -> [] in
305
      let el = try List.assoc tag_false hl with Not_found -> [] in
306
      pp_conditional dependencies m self fmt g tl el
307
    else (* enum type case *)
308
      fprintf fmt "@[<v 2>switch(%a) {@,%a@,}@]"
309
	(pp_c_val self (pp_c_var_read m)) g
310
	(Utils.fprintf_list ~sep:"@," (pp_machine_branch dependencies m self)) hl
311

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

    
315

    
316
(********************************************************************************************)
317
(*                         C file Printing functions                                        *)
318
(********************************************************************************************)
319

    
320
let print_const_def fmt cdecl =
321
  fprintf fmt "%a = %a;@." 
322
    (pp_c_type cdecl.const_id) cdecl.const_type
323
    pp_c_const cdecl.const_value 
324

    
325

    
326
let print_alloc_instance fmt (i, (m, static)) =
327
  fprintf fmt "_alloc->%s = %a (%a);@,"
328
    i
329
    pp_machine_alloc_name (node_name m)
330
    (Utils.fprintf_list ~sep:", " Dimension.pp_dimension) static
331

    
332
let print_alloc_const fmt m =
333
  let const_locals = List.filter (fun vdecl -> vdecl.var_dec_const) m.mstep.step_locals in
334
  fprintf fmt "%a%t"
335
    (Utils.fprintf_list ~sep:";@," (pp_c_decl_local_var m)) const_locals
336
    (Utils.pp_final_char_if_non_empty ";@," const_locals)
337

    
338
let print_alloc_array fmt vdecl =
339
  let base_type = Types.array_base_type vdecl.var_type in
340
  let size_types = Types.array_type_multi_dimension vdecl.var_type in
341
  let size_type = Dimension.multi_dimension_product vdecl.var_loc size_types in
342
  fprintf fmt "_alloc->_reg.%s = (%a*) malloc((%a)*sizeof(%a));@,assert(_alloc->%s);@,"
343
    vdecl.var_id
344
    (pp_c_type "") base_type
345
    Dimension.pp_dimension size_type
346
    (pp_c_type "") base_type
347
    vdecl.var_id
348

    
349
let print_alloc_code fmt m =
350
  let array_mem = List.filter (fun v -> Types.is_array_type v.var_type) m.mmemory in
351
  fprintf fmt "%a *_alloc;@,_alloc = (%a *) malloc(sizeof(%a));@,assert(_alloc);@,%a%areturn _alloc;"
352
    pp_machine_memtype_name m.mname.node_id
353
    pp_machine_memtype_name m.mname.node_id
354
    pp_machine_memtype_name m.mname.node_id
355
    (Utils.fprintf_list ~sep:"" print_alloc_array) array_mem
356
    (Utils.fprintf_list ~sep:"" print_alloc_instance) m.minstances
357

    
358
let print_stateless_code dependencies fmt m =
359
  let self = "__ERROR__" in
360
  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 })
361
  then
362
    (* C99 code *)
363
    fprintf fmt "@[<v 2>%a {@,%a%t@,%a%a%t%t@]@,}@.@."
364
      print_stateless_prototype (m.mname.node_id, m.mstep.step_inputs, m.mstep.step_outputs)
365
      (* locals *)
366
      (Utils.fprintf_list ~sep:";@," (pp_c_decl_local_var m)) m.mstep.step_locals
367
      (Utils.pp_final_char_if_non_empty ";@," m.mstep.step_locals)
368
      (* check assertions *)
369
      (pp_c_checks self) m
370
      (* instrs *)
371
      (Utils.fprintf_list ~sep:"@," (pp_machine_instr dependencies m self)) m.mstep.step_instrs
372
      (Utils.pp_newline_if_non_empty m.mstep.step_instrs)
373
      (fun fmt -> fprintf fmt "return;")
374
  else
375
    (* C90 code *)
376
    let (gen_locals, base_locals) = List.partition (fun v -> Types.is_generic_type v.var_type) m.mstep.step_locals in
377
    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
378
    fprintf fmt "@[<v 2>%a {@,%a%t@,%a%a%t%t@]@,}@.@."
379
      print_stateless_prototype (m.mname.node_id, (m.mstep.step_inputs@gen_locals@gen_calls), m.mstep.step_outputs)
380
      (* locals *)
381
      (Utils.fprintf_list ~sep:";@," (pp_c_decl_local_var m)) base_locals
382
      (Utils.pp_final_char_if_non_empty ";" base_locals)
383
      (* check assertions *)
384
      (pp_c_checks self) m
385
      (* instrs *)
386
      (Utils.fprintf_list ~sep:"@," (pp_machine_instr dependencies m self)) m.mstep.step_instrs
387
      (Utils.pp_newline_if_non_empty m.mstep.step_instrs)
388
      (fun fmt -> fprintf fmt "return;")
389

    
390
let print_reset_code dependencies fmt m self =
391
  let const_locals = List.filter (fun vdecl -> vdecl.var_dec_const) m.mstep.step_locals in
392
  fprintf fmt "@[<v 2>%a {@,%a%t@,%a%treturn;@]@,}@.@."
393
    (print_reset_prototype self) (m.mname.node_id, m.mstatic)
394
    (* constant locals decl *)
395
    (Utils.fprintf_list ~sep:";@," (pp_c_decl_local_var m)) const_locals
396
    (Utils.pp_final_char_if_non_empty ";" const_locals)
397
    (* instrs *)
398
    (Utils.fprintf_list ~sep:"@," (pp_machine_instr dependencies m self)) m.minit
399
    (Utils.pp_newline_if_non_empty m.minit)
400

    
401
let print_step_code dependencies fmt m self =
402
  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 })
403
  then
404
    (* C99 code *)
405
    let array_mems = List.filter (fun v -> Types.is_array_type v.var_type) m.mmemory in
406
    fprintf fmt "@[<v 2>%a {@,%a%t%a%t@,%a%a%t%t@]@,}@.@."
407
      (print_step_prototype self) (m.mname.node_id, m.mstep.step_inputs, m.mstep.step_outputs)
408
      (* locals *)
409
      (Utils.fprintf_list ~sep:";@," (pp_c_decl_local_var m)) m.mstep.step_locals
410
      (Utils.pp_final_char_if_non_empty ";@," m.mstep.step_locals)
411
      (* array mems *)
412
      (Utils.fprintf_list ~sep:";@," (pp_c_decl_array_mem self)) array_mems
413
      (Utils.pp_final_char_if_non_empty ";@," array_mems)
414
      (* check assertions *)
415
      (pp_c_checks self) m
416
      (* instrs *)
417
      (Utils.fprintf_list ~sep:"@," (pp_machine_instr dependencies m self)) m.mstep.step_instrs
418
      (Utils.pp_newline_if_non_empty m.mstep.step_instrs)
419
      (fun fmt -> fprintf fmt "return;")
420
  else
421
    (* C90 code *)
422
    let (gen_locals, base_locals) = List.partition (fun v -> Types.is_generic_type v.var_type) m.mstep.step_locals in
423
    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
424
    fprintf fmt "@[<v 2>%a {@,%a%t@,%a%a%t%t@]@,}@.@."
425
      (print_step_prototype self) (m.mname.node_id, (m.mstep.step_inputs@gen_locals@gen_calls), m.mstep.step_outputs)
426
      (* locals *)
427
      (Utils.fprintf_list ~sep:";@," (pp_c_decl_local_var m)) base_locals
428
      (Utils.pp_final_char_if_non_empty ";" base_locals)
429
      (* check assertions *)
430
      (pp_c_checks self) m
431
      (* instrs *)
432
      (Utils.fprintf_list ~sep:"@," (pp_machine_instr dependencies m self)) m.mstep.step_instrs
433
      (Utils.pp_newline_if_non_empty m.mstep.step_instrs)
434
      (fun fmt -> fprintf fmt "return;")
435

    
436

    
437
(********************************************************************************************)
438
(*                     MAIN C file Printing functions                                       *)
439
(********************************************************************************************)
440

    
441
let print_machine dependencies fmt m =
442
  if fst (get_stateless_status m) then
443
    begin
444
      (* Step function *)
445
      print_stateless_code dependencies fmt m
446
    end
447
  else
448
    begin
449
      (* Alloc function, only if non static mode *)
450
      if (not !Options.static_mem) then  
451
	begin
452
	  fprintf fmt "@[<v 2>%a {@,%a%a@]@,}@.@."
453
	    print_alloc_prototype (m.mname.node_id, m.mstatic)
454
	    print_alloc_const m
455
	    print_alloc_code m;
456
	end;
457
      let self = mk_self m in
458
      (* Reset function *)
459
      print_reset_code dependencies fmt m self;
460
      (* Step function *)
461
      print_step_code dependencies fmt m self
462
    end
463

    
464

    
465
let print_lib_c source_fmt basename prog machines dependencies =
466

    
467
  fprintf source_fmt "#include <assert.h>@.";
468
  if not !Options.static_mem then
469
    begin
470
      fprintf source_fmt "#include <stdlib.h>@.";
471
    end;
472
  print_import_prototype source_fmt (Dep (true, basename, [], true (* assuming it is stateful *)));
473
  pp_print_newline source_fmt ();
474
  (* Print the svn version number and the supported C standard (C90 or C99) *)
475
  print_version source_fmt;
476
  (* Print the prototype of imported nodes *)
477
  fprintf source_fmt "/* Import dependencies */@.";
478
  fprintf source_fmt "@[<v>";
479
  List.iter (print_import_prototype source_fmt) dependencies;
480
  fprintf source_fmt "@]@.";
481
  (* Print consts *)
482
  fprintf source_fmt "/* Global constants (definitions) */@.";
483
  fprintf source_fmt "@[<v>";
484
  List.iter (fun c -> print_const_def source_fmt (const_of_top c)) (get_consts prog);
485
  fprintf source_fmt "@]@.";
486

    
487
  if not !Options.static_mem then
488
    begin
489
      fprintf source_fmt "/* External allocation function prototypes */@.";
490
      fprintf source_fmt "@[<v>";
491
      List.iter (print_extern_alloc_prototypes source_fmt) dependencies;
492
      fprintf source_fmt "@]@.";
493
      fprintf source_fmt "/* Node allocation function prototypes */@.";
494
      fprintf source_fmt "@[<v>";
495
      List.iter (fun m -> fprintf source_fmt "%a;@." print_alloc_prototype (m.mname.node_id, m.mstatic)) machines;
496
      fprintf source_fmt "@]@.";
497
    end;
498

    
499
  (* Print the struct definitions of all machines. *)
500
  fprintf source_fmt "/* Struct definitions */@.";
501
  fprintf source_fmt "@[<v>";
502
  List.iter (print_machine_struct source_fmt) machines;
503
  fprintf source_fmt "@]@.";
504
  pp_print_newline source_fmt ();
505
  (* Print nodes one by one (in the previous order) *)
506
  List.iter (print_machine dependencies source_fmt) machines;
507
 end
508

    
509
(* Local Variables: *)
510
(* compile-command:"make -C ../../.." *)
511
(* End: *)