Project

General

Profile

Statistics
| Branch: | Tag: | Revision:

lustrec / src / c_backend.ml @ 12af4908

History | View | Annotate | Download (40.7 KB)

1
(* ----------------------------------------------------------------------------
2
 * SchedMCore - A MultiCore Scheduling Framework
3
 * Copyright (C) 2009-2013, ONERA, Toulouse, FRANCE - LIFL, Lille, FRANCE
4
 * Copyright (C) 2012-2013, INPT, Toulouse, FRANCE
5
 *
6
 * This file is part of Prelude
7
 *
8
 * Prelude is free software; you can redistribute it and/or
9
 * modify it under the terms of the GNU Lesser General Public License
10
 * as published by the Free Software Foundation ; either version 2 of
11
 * the License, or (at your option) any later version.
12
 *
13
 * Prelude is distributed in the hope that it will be useful, but
14
 * WITHOUT ANY WARRANTY ; without even the implied warranty of
15
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16
 * Lesser General Public License for more details.
17
 *
18
 * You should have received a copy of the GNU Lesser General Public
19
 * License along with this program ; if not, write to the Free Software
20
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
21
 * USA
22
 *---------------------------------------------------------------------------- *)
23

    
24
(* This module is used for the lustre to C compiler *)
25

    
26
open Format
27
open LustreSpec
28
open Corelang
29
open Machine_code
30

    
31
(********************************************************************************************)
32
(*                     Basic      Printing functions                                        *)
33
(********************************************************************************************)
34

    
35
let print_version fmt =
36
  Format.fprintf fmt "/* @[<v>C code generated by %s@,SVN version number %s@,Code is %s compliant */@,@]@."
37
    (Filename.basename Sys.executable_name) Version.number (if !Options.ansi then "ANSI C90" else "C99")
38

    
39
(* Generation of a non-clashing name for the self memory variable (for step and reset functions) *)
40
let mk_self m =
41
  mk_new_name (m.mstep.step_inputs@m.mstep.step_outputs@m.mstep.step_locals@m.mmemory) "self"
42

    
43
(* Generation of a non-clashing name for the instance variable of static allocation macro *)
44
let mk_instance m =
45
  mk_new_name (m.mstep.step_inputs@m.mmemory) "inst"
46

    
47
(* Generation of a non-clashing name for the attribute variable of static allocation macro *)
48
let mk_attribute m =
49
  mk_new_name (m.mstep.step_inputs@m.mmemory) "attr"
50

    
51
let mk_call_var_decl loc id =
52
  { var_id = id;
53
    var_dec_type = mktyp Location.dummy_loc Tydec_any;
54
    var_dec_clock = mkclock Location.dummy_loc Ckdec_any;
55
    var_dec_const = false;
56
    var_type = Type_predef.type_arrow (Types.new_var ()) (Types.new_var ());
57
    var_clock = Clocks.new_var true;
58
    var_loc = loc }
59

    
60
(* counter for loop variable creation *)
61
let loop_cpt = ref (-1)
62

    
63
let reset_loop_counter () =
64
 loop_cpt := -1
65

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

    
76
let reset_addr_counter () =
77
 addr_cpt := -1
78

    
79
let mk_addr_var m var =
80
  let vars = m.mmemory in
81
  let rec aux () =
82
    incr addr_cpt;
83
    let s = Printf.sprintf "%s_%s_%d" var "addr" !addr_cpt in
84
    if List.exists (fun v -> v.var_id = s) vars then aux () else s
85
  in aux ()
86
*)
87
let pp_machine_memtype_name fmt id = fprintf fmt "struct %s_mem" id
88
let pp_machine_regtype_name fmt id = fprintf fmt "struct %s_reg" id
89
let pp_machine_alloc_name fmt id = fprintf fmt "%s_alloc" id
90
let pp_machine_static_declare_name fmt id = fprintf fmt "%s_DECLARE" id
91
let pp_machine_static_link_name fmt id = fprintf fmt "%s_LINK" id
92
let pp_machine_static_alloc_name fmt id = fprintf fmt "%s_ALLOC" id
93
let pp_machine_reset_name fmt id = fprintf fmt "%s_reset" id
94
let pp_machine_step_name fmt id = fprintf fmt "%s_step" id
95

    
96
let pp_c_dimension fmt d =
97
 fprintf fmt "%a" Dimension.pp_dimension d
98

    
99
let pp_c_type var fmt t =
100
  let rec aux t pp_suffix =
101
  match (Types.repr t).Types.tdesc with
102
  | Types.Tclock t'       -> aux t' pp_suffix
103
  | Types.Tbool           -> fprintf fmt "_Bool %s%a" var pp_suffix ()
104
  | Types.Treal           -> fprintf fmt "double %s%a" var pp_suffix ()
105
  | Types.Tint            -> fprintf fmt "int %s%a" var pp_suffix ()
106
  | Types.Tarray (d, t')  ->
107
    let pp_suffix' fmt () = fprintf fmt "%a[%a]" pp_suffix () pp_c_dimension d in
108
    aux t' pp_suffix'
109
  | Types.Tstatic (_, t') -> fprintf fmt "const "; aux t' pp_suffix
110
  | Types.Tconst ty       -> fprintf fmt "%s %s" ty var
111
  | Types.Tarrow (_, _)   -> fprintf fmt "void (*%s)()" var
112
  | _                     -> eprintf "internal error: pp_c_type %a@." Types.print_ty t; assert false
113
  in aux t (fun fmt () -> ())
114

    
115
let rec pp_c_initialize fmt t = 
116
  match (Types.repr t).Types.tdesc with
117
  | Types.Tint -> pp_print_string fmt "0"
118
  | Types.Tclock t' -> pp_c_initialize fmt t'
119
  | Types.Tbool -> pp_print_string fmt "0" 
120
  | Types.Treal -> pp_print_string fmt "0."
121
  | Types.Tarray (d, t') when Dimension.is_dimension_const d ->
122
    fprintf fmt "{%a}"
123
      (Utils.fprintf_list ~sep:"," (fun fmt _ -> pp_c_initialize fmt t'))
124
      (Utils.duplicate 0 (Dimension.size_const_dimension d))
125
  | _ -> assert false
126

    
127
(* Declaration of an input variable:
128
   - if its type is array/matrix/etc, then declare it as a mere pointer,
129
     in order to cope with unknown/parametric array dimensions, 
130
     as it is the case for generics
131
*)
132
let pp_c_decl_input_var fmt id =
133
  if !Options.ansi && Types.is_array_type id.var_type
134
  then pp_c_type (sprintf "(*%s)" id.var_id) fmt (Types.array_base_type id.var_type)
135
  else pp_c_type id.var_id fmt id.var_type
136

    
137
(* Declaration of an output variable:
138
   - if its type is scalar, then pass its address
139
   - if its type is array/matrix/etc, then declare it as a mere pointer,
140
     in order to cope with unknown/parametric array dimensions, 
141
     as it is the case for generics
142
*)
143
let pp_c_decl_output_var fmt id =
144
  if (not !Options.ansi) && Types.is_array_type id.var_type
145
  then pp_c_type                  id.var_id  fmt id.var_type
146
  else pp_c_type (sprintf "(*%s)" id.var_id) fmt (Types.array_base_type id.var_type)
147

    
148
(* Declaration of a local/mem variable:
149
   - if it's an array/matrix/etc, its size(s) should be
150
     known in order to statically allocate memory, 
151
     so we print the full type
152
*)
153
let pp_c_decl_local_var fmt id =
154
  pp_c_type id.var_id fmt id.var_type
155

    
156
let pp_c_decl_array_mem self fmt id =
157
  fprintf fmt "%a = (%a) (%s->_reg.%s)"
158
    (pp_c_type (sprintf "(*%s)" id.var_id)) id.var_type
159
    (pp_c_type "(*)") id.var_type
160
    self
161
    id.var_id
162

    
163
(* Declaration of a struct variable:
164
   - if it's an array/matrix/etc, we declare it as a pointer
165
*)
166
let pp_c_decl_struct_var fmt id =
167
  if Types.is_array_type id.var_type
168
  then pp_c_type (sprintf "(*%s)" id.var_id) fmt (Types.array_base_type id.var_type)
169
  else pp_c_type                  id.var_id  fmt id.var_type
170

    
171
(* Access to the value of a variable:
172
   - if it's not a scalar output, then its name is enough
173
   - otherwise, dereference it (it has been declared as a pointer,
174
     despite its scalar Lustre type)
175
   - moreover, cast arrays variables into their original array type.
176
*)
177
let pp_c_var_read m fmt id =
178
  if Types.is_array_type id.var_type
179
  then
180
    fprintf fmt "%s" id.var_id
181
  else
182
    if List.exists (fun o -> o.var_id = id.var_id) m.mstep.step_outputs (* id is output *)
183
    then fprintf fmt "*%s" id.var_id
184
    else fprintf fmt "%s" id.var_id
185

    
186
(* Addressable value of a variable, the one that is passed around in calls:
187
   - if it's not a scalar non-output, then its name is enough
188
   - otherwise, reference it (it must be passed as a pointer,
189
     despite its scalar Lustre type)
190
*)
191
let pp_c_var_write m fmt id =
192
  if Types.is_array_type id.var_type
193
  then
194
    fprintf fmt "%s" id.var_id
195
  else
196
    if List.exists (fun o -> o.var_id = id.var_id) m.mstep.step_outputs (* id is output *)
197
    then
198
      fprintf fmt "%s" id.var_id
199
    else
200
      fprintf fmt "&%s" id.var_id
201

    
202
let pp_c_decl_instance_var fmt (name, (node, static)) = 
203
  fprintf fmt "%a *%s" pp_machine_memtype_name (node_name node) name
204

    
205
let pp_c_tag fmt t =
206
 pp_print_string fmt (if t = tag_true then "1" else if t = tag_false then "0" else t)
207

    
208
(* Prints a constant value *)
209
let rec pp_c_const fmt c =
210
  match c with
211
    | Const_int i     -> pp_print_int fmt i
212
    | Const_real r    -> pp_print_string fmt r
213
    | Const_float r   -> pp_print_float fmt r
214
    | Const_tag t     -> pp_c_tag fmt t
215
    | Const_array ca  -> fprintf fmt "{%a }" (Utils.fprintf_list ~sep:", " pp_c_const) ca
216
    | Const_struct fl -> fprintf fmt "{%a }" (Utils.fprintf_list ~sep:", " (fun fmt (f, c) -> pp_c_const fmt c)) fl
217

    
218
(* Prints a value expression [v], with internal function calls only.
219
   [pp_var] is a printer for variables (typically [pp_c_var_read]),
220
   but an offset suffix may be added for array variables
221
*)
222
let rec pp_c_val self pp_var fmt v =
223
  match v with
224
    | Cst c         -> pp_c_const fmt c
225
    | Array vl      -> fprintf fmt "{%a}" (Utils.fprintf_list ~sep:", " (pp_c_val self pp_var)) vl
226
    | Access (t, i) -> fprintf fmt "%a[%a]" (pp_c_val self pp_var) t (pp_c_val self pp_var) i
227
    | Power (v, n)  -> assert false
228
    | LocalVar v    -> pp_var fmt v
229
    | StateVar v    ->
230
      if Types.is_array_type v.var_type
231
      then fprintf fmt "*%a" pp_var v
232
      else fprintf fmt "%s->_reg.%a" self pp_var v
233
    | Fun (n, vl)   -> Basic_library.pp_c n (pp_c_val self pp_var) fmt vl
234

    
235
let pp_c_checks self fmt m =
236
  Utils.fprintf_list ~sep:"" (fun fmt (loc, check) -> fprintf fmt "@[<v>%a@,assert (%a);@]@," Location.pp_c_loc loc (pp_c_val self (pp_c_var_read m)) check) fmt m.mstep.step_checks
237

    
238

    
239
(********************************************************************************************)
240
(*                    Instruction Printing functions                                        *)
241
(********************************************************************************************)
242

    
243
(* Computes the depth to which multi-dimension array assignments should be expanded.
244
   It equals the maximum number of nested static array constructions accessible from root [v].
245
*)
246
let rec expansion_depth v =
247
 match v with
248
 | Cst (Const_array cl) -> 1 + List.fold_right (fun c -> max (expansion_depth (Cst c))) cl 0
249
 | Cst _
250
 | LocalVar _
251
 | StateVar _  -> 0
252
 | Fun (_, vl) -> List.fold_right (fun v -> max (expansion_depth v)) vl 0
253
 | Array vl    -> 1 + List.fold_right (fun v -> max (expansion_depth v)) vl 0
254
 | Access (v, i) -> max 0 (expansion_depth v - 1)
255
 | Power (v, n)  -> 0 (*1 + expansion_depth v*)
256

    
257
type loop_index = LVar of ident | LInt of int ref
258

    
259
(* Computes the list of nested loop variables together with their dimension bounds.
260
   - LInt r stands for loop expansion (no loop variable, but int loop index)
261
   - LVar v stands for loop variable v
262
*)
263
let rec mk_loop_variables m ty depth =
264
 match (Types.repr ty).Types.tdesc, depth with
265
 | Types.Tarray (d, ty'), 0       ->
266
   let v = mk_loop_var m () in
267
   (d, LVar v) :: mk_loop_variables m ty' 0
268
 | Types.Tarray (d, ty'), _       ->
269
   let r = ref (-1) in
270
   (d, LInt r) :: mk_loop_variables m ty' (depth - 1)
271
 | _                    , 0       -> []
272
 | _                              -> assert false
273

    
274
let reorder_loop_variables loop_vars =
275
  let (int_loops, var_loops) = List.partition (function (d, LInt _) -> true | _ -> false) loop_vars in
276
  var_loops @ int_loops
277

    
278
(* Prints a one loop variable suffix for arrays *)
279
let pp_loop_var fmt lv =
280
 match snd lv with
281
 | LVar v -> fprintf fmt "[%s]" v
282
 | LInt r -> fprintf fmt "[%d]" !r
283

    
284
(* Prints a suffix of loop variables for arrays *)
285
let pp_suffix fmt loop_vars =
286
 Utils.fprintf_list ~sep:"" pp_loop_var fmt loop_vars
287

    
288
(* Prints a [value] indexed by the suffix list [loop_vars] *)
289
let rec pp_value_suffix self loop_vars pp_value fmt value =
290
 match loop_vars, value with
291
 | (_, LInt r) :: q, Array vl     ->
292
   pp_value_suffix self q pp_value fmt (List.nth vl !r)
293
 | _           :: q, Power (v, n) ->
294
   pp_value_suffix self loop_vars pp_value fmt v
295
 | _               , Fun (n, vl)  ->
296
   Basic_library.pp_c n (pp_value_suffix self loop_vars pp_value) fmt vl
297
 | _               , _            ->
298
   let pp_var_suffix fmt v = fprintf fmt "%a%a" pp_value v pp_suffix loop_vars in
299
   pp_c_val self pp_var_suffix fmt value
300

    
301
(* type_directed assignment: array vs. statically sized type
302
   - [var_type]: type of variable to be assigned
303
   - [var_name]: name of variable to be assigned
304
   - [value]: assigned value
305
   - [pp_var]: printer for variables
306
*)
307
let pp_assign m self pp_var fmt var_type var_name value =
308
  let depth = expansion_depth value in
309
(*eprintf "pp_assign %a %a %d@." Types.print_ty var_type pp_val value depth;*)
310
  let loop_vars = mk_loop_variables m var_type depth in
311
  let reordered_loop_vars = reorder_loop_variables loop_vars in
312
  let rec aux fmt vars =
313
    match vars with
314
    | [] ->
315
      fprintf fmt "%a = %a;" (pp_value_suffix self loop_vars pp_var) var_name (pp_value_suffix self loop_vars pp_var) value
316
    | (d, LVar i) :: q ->
317
(*eprintf "pp_aux %a %s@." Dimension.pp_dimension d i;*)
318
      fprintf fmt "@[<v 2>{@,int %s;@,for(%s=0;%s<%a;%s++)@,%a @]@,}"
319
	i i i Dimension.pp_dimension d i
320
	aux q
321
    | (d, LInt r) :: q ->
322
(*eprintf "pp_aux %a %d@." Dimension.pp_dimension d (!r);*)
323
      let szl = Utils.enumerate (Dimension.size_const_dimension d) in
324
      fprintf fmt "@[<v 2>{@,%a@]@,}"
325
	(Utils.fprintf_list ~sep:"@," (fun fmt i -> r := i; aux fmt q)) szl
326
  in
327
  begin
328
    reset_loop_counter ();
329
    (*reset_addr_counter ();*)
330
    aux fmt reordered_loop_vars
331
  end
332

    
333
let pp_instance_call m self fmt i (inputs: value_t list) (outputs: var_decl list) =
334
 try (* stateful node instance *)
335
   let (n,_) = List.assoc i m.minstances in
336
   fprintf fmt "%s_step (%a%t%a%t%s->%s);"
337
     (node_name n)
338
     (Utils.fprintf_list ~sep:", " (pp_c_val self (pp_c_var_read m))) inputs
339
     (Utils.pp_final_char_if_non_empty ", " inputs) 
340
     (Utils.fprintf_list ~sep:", " (pp_c_var_write m)) outputs
341
     (Utils.pp_final_char_if_non_empty ", " outputs)
342
     self
343
     i
344
 with Not_found -> (* stateless node instance *)
345
   let (n,_) = List.assoc i m.mcalls in
346
   fprintf fmt "%s (%a%t%a);"
347
     (node_name n)
348
     (Utils.fprintf_list ~sep:", " (pp_c_val self (pp_c_var_read m))) inputs
349
     (Utils.pp_final_char_if_non_empty ", " inputs) 
350
     (Utils.fprintf_list ~sep:", " (pp_c_var_write m)) outputs 
351

    
352
let pp_machine_reset (m: machine_t) self fmt inst =
353
  let (node, static) = List.assoc inst m.minstances in
354
  fprintf fmt "%a(%a%t%s->%s);"
355
    pp_machine_reset_name (node_name node)
356
    (Utils.fprintf_list ~sep:", " Dimension.pp_dimension) static
357
    (Utils.pp_final_char_if_non_empty ", " static)
358
    self inst
359

    
360
let rec pp_conditional (m: machine_t) self fmt c tl el =
361
  fprintf fmt "@[<v 2>if (%a) {%t%a@]@,@[<v 2>} else {%t%a@]@,}"
362
    (pp_c_val self (pp_c_var_read m)) c
363
    (Utils.pp_newline_if_non_empty tl)
364
    (Utils.fprintf_list ~sep:"@," (pp_machine_instr m self)) tl
365
    (Utils.pp_newline_if_non_empty el)
366
    (Utils.fprintf_list ~sep:"@," (pp_machine_instr m self)) el
367

    
368
and pp_machine_instr (m: machine_t) self fmt instr =
369
  match instr with 
370
  | MReset i ->
371
    pp_machine_reset m self fmt i
372
  | MLocalAssign (i,v) ->
373
    pp_assign
374
      m self (pp_c_var_read m) fmt
375
      i.var_type (LocalVar i) v
376
  | MStateAssign (i,v) ->
377
    pp_assign
378
      m self (pp_c_var_read m) fmt
379
      i.var_type (StateVar i) v
380
  | MStep ([i0], i, vl) when Basic_library.is_internal_fun i  ->
381
    pp_machine_instr m self fmt (MLocalAssign (i0, Fun (i, vl)))
382
  | MStep (il, i, vl) ->
383
    pp_instance_call m self fmt i vl il
384
  | MBranch (g,hl) ->
385
    if hl <> [] && let t = fst (List.hd hl) in t = tag_true || t = tag_false
386
    then (* boolean case, needs special treatment in C because truth value is not unique *)
387
	 (* may disappear if we optimize code by replacing last branch test with default *)
388
      let tl = try List.assoc tag_true  hl with Not_found -> [] in
389
      let el = try List.assoc tag_false hl with Not_found -> [] in
390
      pp_conditional m self fmt g tl el
391
    else (* enum type case *)
392
      fprintf fmt "@[<v 2>switch(%a) {@,%a@,}@]"
393
	(pp_c_val self (pp_c_var_read m)) g
394
	(Utils.fprintf_list ~sep:"@," (pp_machine_branch m self)) hl
395

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

    
399

    
400
(**************************************************************************)
401
(*     Printing spec for c *)
402

    
403
(**************************************************************************)
404

    
405

    
406
let pp_econst fmt c = 
407
  match c with
408
    | EConst_int i -> pp_print_int fmt i
409
    | EConst_real r -> pp_print_string fmt r
410
    | EConst_float r -> pp_print_float fmt r
411
    | EConst_bool b -> pp_print_bool fmt b
412
    | EConst_string s -> pp_print_string fmt ("\"" ^ s ^ "\"")
413

    
414
let rec pp_eexpr is_output fmt eexpr = 
415
  let pp_eexpr = pp_eexpr is_output in
416
  match eexpr.eexpr_desc with
417
    | EExpr_const c -> pp_econst fmt c
418
    | EExpr_ident id -> 
419
      if is_output id then pp_print_string fmt ("*" ^ id) else pp_print_string fmt id
420
    | EExpr_tuple el -> Utils.fprintf_list ~sep:"," pp_eexpr fmt el
421
    | EExpr_arrow (e1, e2) -> fprintf fmt "%a -> %a" pp_eexpr e1 pp_eexpr e2
422
    | EExpr_fby (e1, e2) -> fprintf fmt "%a fby %a" pp_eexpr e1 pp_eexpr e2
423
    (* | EExpr_concat (e1, e2) -> fprintf fmt "%a::%a" pp_eexpr e1 pp_eexpr e2 *)
424
    (* | EExpr_tail e -> fprintf fmt "tail %a" pp_eexpr e *)
425
    | EExpr_pre e -> fprintf fmt "pre %a" pp_eexpr e
426
    | EExpr_when (e, id) -> fprintf fmt "%a when %s" pp_eexpr e id
427
    | EExpr_merge (id, e1, e2) -> 
428
      fprintf fmt "merge (%s, %a, %a)" id pp_eexpr e1 pp_eexpr e2
429
    | EExpr_appl (id, e, r) -> pp_eapp is_output fmt id e r
430
    | EExpr_forall (vars, e) -> fprintf fmt "forall %a; %a" Printers.pp_node_args vars pp_eexpr e 
431
    | EExpr_exists (vars, e) -> fprintf fmt "exists %a; %a" Printers.pp_node_args vars pp_eexpr e 
432

    
433

    
434
    (* | EExpr_whennot _ *)
435
    (* | EExpr_uclock _ *)
436
    (* | EExpr_dclock _ *)
437
    (* | EExpr_phclock _ -> assert false *)
438
and pp_eapp is_output fmt id e r =
439
  let pp_eexpr = pp_eexpr is_output in
440
  match r with
441
  | None ->
442
    (match id, e.eexpr_desc with
443
    | "+", EExpr_tuple([e1;e2]) -> fprintf fmt "(%a + %a)" pp_eexpr e1 pp_eexpr e2
444
    | "uminus", _ -> fprintf fmt "(- %a)" pp_eexpr e
445
    | "-", EExpr_tuple([e1;e2]) -> fprintf fmt "(%a - %a)" pp_eexpr e1 pp_eexpr e2
446
    | "*", EExpr_tuple([e1;e2]) -> fprintf fmt "(%a * %a)" pp_eexpr e1 pp_eexpr e2
447
    | "/", EExpr_tuple([e1;e2]) -> fprintf fmt "(%a / %a)" pp_eexpr e1 pp_eexpr e2
448
    | "mod", EExpr_tuple([e1;e2]) -> fprintf fmt "(%a mod %a)" pp_eexpr e1 pp_eexpr e2
449
    | "&&", EExpr_tuple([e1;e2]) -> fprintf fmt "(%a && %a)" pp_eexpr e1 pp_eexpr e2
450
    | "||", EExpr_tuple([e1;e2]) -> fprintf fmt "(%a || %a)" pp_eexpr e1 pp_eexpr e2
451
    | "xor", EExpr_tuple([e1;e2]) -> fprintf fmt "(%a ^^ %a)" pp_eexpr e1 pp_eexpr e2
452
    | "impl", EExpr_tuple([e1;e2]) -> fprintf fmt "(%a ==> %a)" pp_eexpr e1 pp_eexpr e2
453
    | "<", EExpr_tuple([e1;e2]) -> fprintf fmt "(%a < %a)" pp_eexpr e1 pp_eexpr e2
454
    | "<=", EExpr_tuple([e1;e2]) -> fprintf fmt "(%a <= %a)" pp_eexpr e1 pp_eexpr e2
455
    | ">", EExpr_tuple([e1;e2]) -> fprintf fmt "(%a > %a)" pp_eexpr e1 pp_eexpr e2
456
    | ">=", EExpr_tuple([e1;e2]) -> fprintf fmt "(%a >= %a)" pp_eexpr e1 pp_eexpr e2
457
    | "!=", EExpr_tuple([e1;e2]) -> fprintf fmt "(%a != %a)" pp_eexpr e1 pp_eexpr e2
458
    | "=", EExpr_tuple([e1;e2]) -> fprintf fmt "(%a == %a)" pp_eexpr e1 pp_eexpr e2
459
    | "not", _ -> fprintf fmt "(! %a)" pp_eexpr e
460
    | "ite", EExpr_tuple([e1;e2;e3]) -> fprintf fmt "(if %a then %a else %a)" pp_eexpr e1 pp_eexpr e2 pp_eexpr e3
461
    | _ -> fprintf fmt "%s (%a)" id pp_eexpr e)
462
  | Some x -> fprintf fmt "%s (%a) every %s" id pp_eexpr e x 
463

    
464
let pp_ensures is_output fmt e =
465
  match e with
466
    | EnsuresExpr e -> fprintf fmt "ensures %a;@ " (pp_eexpr is_output) e
467
    | SpecObserverNode (name, args) -> fprintf fmt "observer %s (%a);@ " name (Utils.fprintf_list ~sep:", " (pp_eexpr is_output)) args
468

    
469
let pp_acsl_spec outputs fmt spec =
470
  let is_output = fun oid -> List.exists (fun v -> v.var_id = oid) outputs in
471
  let pp_eexpr = pp_eexpr is_output in
472
  fprintf fmt "@[<v 2>/*@@ ";
473
  Utils.fprintf_list ~sep:"" (fun fmt r -> fprintf fmt "requires %a;@ " pp_eexpr r) fmt spec.requires;
474
  Utils.fprintf_list ~sep:"" (pp_ensures is_output) fmt spec.ensures;
475
  fprintf fmt "@ ";
476
  (* fprintf fmt "assigns *self%t%a;@ "  *)
477
  (*   (fun fmt -> if List.length outputs > 0 then fprintf fmt ", ") *)
478
  (*   (fprintf_list ~sep:"," (fun fmt v -> fprintf fmt "*%s" v.var_id)) outputs; *)
479
  Utils.fprintf_list ~sep:"@ " (fun fmt (name, assumes, requires) -> 
480
    fprintf fmt "behavior %s:@[@ %a@ %a@]" 
481
      name
482
      (Utils.fprintf_list ~sep:"@ " (fun fmt r -> fprintf fmt "assumes %a;" pp_eexpr r)) assumes
483
      (Utils.fprintf_list ~sep:"@ " (pp_ensures is_output)) requires
484
  ) fmt spec.behaviors;
485
  fprintf fmt "@]@ */@.";
486
  ()
487

    
488
(********************************************************************************************)
489
(*                      Prototype Printing functions                                        *)
490
(********************************************************************************************)
491

    
492
let print_alloc_prototype fmt (name, static) =
493
  fprintf fmt "%a * %a (%a)"
494
    pp_machine_memtype_name name
495
    pp_machine_alloc_name name
496
    (Utils.fprintf_list ~sep:",@ " pp_c_decl_input_var) static
497

    
498
let print_reset_prototype self fmt (name, static) =
499
  fprintf fmt "void %a (@[<v>%a%t%a *%s@])"
500
    pp_machine_reset_name name
501
    (Utils.fprintf_list ~sep:",@ " pp_c_decl_input_var) static
502
    (Utils.pp_final_char_if_non_empty ",@," static) 
503
    pp_machine_memtype_name name
504
    self
505

    
506
let print_stateless_prototype fmt (name, inputs, outputs) =
507
match outputs with
508
(* DOESN'T WORK FOR ARRAYS
509
  | [o] -> fprintf fmt "%a (@[<v>%a@])"
510
    (pp_c_type name) o.var_type
511
    (Utils.fprintf_list ~sep:",@ " pp_c_var) inputs
512
*)  
513
  | _ -> fprintf fmt "void %s (@[<v>@[%a%t@]@,@[%a@]@,@])"
514
    name
515
    (Utils.fprintf_list ~sep:",@ " pp_c_decl_input_var) inputs
516
    (Utils.pp_final_char_if_non_empty ",@ " inputs) 
517
    (Utils.fprintf_list ~sep:",@ " pp_c_decl_output_var) outputs
518

    
519
let print_step_prototype self fmt (name, inputs, outputs) =
520
  fprintf fmt "void %a (@[<v>@[%a%t@]@,@[%a@]%t@[%a *%s@]@])"
521
    pp_machine_step_name name
522
    (Utils.fprintf_list ~sep:",@ " pp_c_decl_input_var) inputs
523
    (Utils.pp_final_char_if_non_empty ",@ " inputs) 
524
    (Utils.fprintf_list ~sep:",@ " pp_c_decl_output_var) outputs
525
    (Utils.pp_final_char_if_non_empty ",@," outputs) 
526
    pp_machine_memtype_name name
527
    self
528

    
529
(********************************************************************************************)
530
(*                         Header Printing functions                                        *)
531
(********************************************************************************************)
532

    
533
(* Removed because of "open" constructs. No more extern functions *)
534
(*
535
let print_prototype fmt decl =
536
  match decl.top_decl_desc with
537
    | ImportedFun m -> (
538
        fprintf fmt "extern %a;@,"
539
	  print_stateless_prototype 
540
	  (m.fun_id, m.fun_inputs, m.fun_outputs)
541
    )
542
    | ImportedNode m -> (
543
      if m.nodei_stateless then (* It's a function not a node *)
544
        fprintf fmt "extern %a;@,"
545
	  print_stateless_prototype 
546
	  (m.nodei_id, m.nodei_inputs, m.nodei_outputs)
547
      else (
548
	let static = List.filter (fun v -> v.var_dec_const) m.nodei_inputs in
549
        fprintf fmt "extern %a;@,"
550
	  print_alloc_prototype (m.nodei_id, static);
551
	fprintf fmt "extern %a;@,"
552
	  (print_reset_prototype "self") (m.nodei_id, static);
553
	fprintf fmt "extern %a;@,"
554
	  (print_step_prototype "self") (m.nodei_id, m.nodei_inputs, m.nodei_outputs);
555
      )
556
    )
557
    | _ -> () (* We don't do anything here *)
558
      *)
559

    
560
let print_import_standard fmt =
561
  fprintf fmt "#include \"%s/include/lustrec/arrow.h\"@.@." Version.prefix
562

    
563
let print_prototype fmt decl =
564
  match decl.top_decl_desc with
565
  | Open m -> fprintf fmt "#include \"%s.h\"@," m
566
  | _ -> () (* We don't do anything here *)
567
    
568
let pp_registers_struct fmt m =
569
  if m.mmemory <> []
570
  then
571
    fprintf fmt "@[%a {@[%a; @]}@] _reg; "
572
      pp_machine_regtype_name m.mname.node_id
573
      (Utils.fprintf_list ~sep:"; " pp_c_decl_struct_var) m.mmemory
574
  else
575
    ()
576

    
577
let print_machine_struct fmt m =
578
  if m.mname.node_id != arrow_id
579
  then (
580
    (* We don't print arrow function *)
581
    (* Define struct *)
582
    fprintf fmt "@[%a {@[%a%a%t@]};@]@."
583
      pp_machine_memtype_name m.mname.node_id
584
      pp_registers_struct m
585
      (Utils.fprintf_list ~sep:"; " pp_c_decl_instance_var) m.minstances
586
      (Utils.pp_final_char_if_non_empty "; " m.minstances)
587
  )
588

    
589
(*
590
let pp_static_array_instance fmt m (v, m) =
591
 fprintf fmt "%s" (mk_addr_var m v)
592
*)
593
let print_static_declare_instance attr fmt (i, (m, static)) =
594
  fprintf fmt "%a(%s, %a%t%s)"
595
    pp_machine_static_declare_name (node_name m)
596
    attr
597
    (Utils.fprintf_list ~sep:", " Dimension.pp_dimension) static
598
    (Utils.pp_final_char_if_non_empty ", " static)
599
    i
600

    
601
let print_static_declare_macro fmt m =
602
  let array_mem = List.filter (fun v -> Types.is_array_type v.var_type) m.mmemory in
603
  let inst = mk_instance m in
604
  let attr = mk_attribute m in
605
  fprintf fmt "@[<v 2>#define %a(%s, %a%t%s)\\@,%s %a %s;\\@,%a%t%a;@,@]"
606
    pp_machine_static_declare_name m.mname.node_id
607
    attr
608
    (Utils.fprintf_list ~sep:", " (pp_c_var_read m)) m.mstatic
609
    (Utils.pp_final_char_if_non_empty ", " m.mstatic)
610
    inst
611
    attr
612
    pp_machine_memtype_name m.mname.node_id
613
    inst
614
    (Utils.fprintf_list ~sep:";\\@," pp_c_decl_local_var) array_mem
615
    (Utils.pp_final_char_if_non_empty ";\\@," array_mem)
616
    (Utils.fprintf_list ~sep:";\\@,"
617
       (fun fmt (i',m') ->
618
	 let path = sprintf "inst ## _%s" i' in
619
	 fprintf fmt "%a"
620
	   (print_static_declare_instance attr) (path,m')
621
       )) m.minstances
622

    
623
      
624
let print_static_link_instance fmt (i, (m, _)) =
625
 fprintf fmt "%a(%s)" pp_machine_static_link_name (node_name m) i
626

    
627
let print_static_link_macro fmt m =
628
  let array_mem = List.filter (fun v -> Types.is_array_type v.var_type) m.mmemory in
629
  fprintf fmt "@[<v>@[<v 2>#define %a(inst) do {\\@,%a%t%a;\\@]@,} while (0)@.@]"
630
    pp_machine_static_link_name m.mname.node_id
631
    (Utils.fprintf_list ~sep:";\\@,"
632
       (fun fmt v ->
633
	 fprintf fmt "inst.%s = &%s"
634
	   v.var_id
635
	   v.var_id
636
       )) array_mem
637
    (Utils.pp_final_char_if_non_empty ";\\@," array_mem)
638
    (Utils.fprintf_list ~sep:";\\@,"
639
       (fun fmt (i',m') ->
640
	 let path = sprintf "inst ## _%s" i' in
641
	 fprintf fmt "%a;\\@,inst.%s = &%s"
642
	   print_static_link_instance (path,m')
643
	   i'
644
	   path
645
       )) m.minstances
646
      
647
let print_static_alloc_macro fmt m =
648
  fprintf fmt "@[<v>@[<v 2>#define %a(attr,%a%tinst)\\@,%a(attr,%a%tinst);\\@,%a(inst);@]@,@]@."
649
    pp_machine_static_alloc_name m.mname.node_id
650
    (Utils.fprintf_list ~sep:", " (pp_c_var_read m)) m.mstatic
651
    (Utils.pp_final_char_if_non_empty ", " m.mstatic)
652
    pp_machine_static_declare_name m.mname.node_id
653
    (Utils.fprintf_list ~sep:", " (pp_c_var_read m)) m.mstatic
654
    (Utils.pp_final_char_if_non_empty ", " m.mstatic)
655
    pp_machine_static_link_name m.mname.node_id
656

    
657
let print_machine_decl fmt m =
658
  if m.mname.node_id <> arrow_id
659
  then (
660
    (* We don't print arrow function *)
661
    (* Static allocation *)
662
    if !Options.static_mem
663
    then (
664
      fprintf fmt "%a@.%a@.%a@."
665
	print_static_declare_macro m
666
	print_static_link_macro m
667
	print_static_alloc_macro m
668
    )
669
    else ( 
670
    (* Dynamic allocation *)
671
      fprintf fmt "extern %a;@.@."
672
	print_alloc_prototype (m.mname.node_id, m.mstatic)
673
    );
674
    let self = mk_self m in
675
    fprintf fmt "extern %a;@.@."
676
      (print_reset_prototype self) (m.mname.node_id, m.mstatic);
677
    (* Print specification if any *)
678
    (match m.mspec with
679
      | None -> ()
680
      | Some spec -> 
681
	pp_acsl_spec m.mstep.step_outputs fmt spec
682
    );
683
    fprintf fmt "extern %a;@.@."
684
      (print_step_prototype self)
685
      (m.mname.node_id, m.mstep.step_inputs, m.mstep.step_outputs)
686
  )
687

    
688

    
689
(********************************************************************************************)
690
(*                         C file Printing functions                                        *)
691
(********************************************************************************************)
692

    
693
let print_const_def fmt cdecl =
694
  fprintf fmt "%a = %a;@." (pp_c_type cdecl.const_id) cdecl.const_type pp_c_const cdecl.const_value 
695

    
696
let print_const_decl fmt cdecl =
697
  fprintf fmt "extern %a;@." (pp_c_type cdecl.const_id) cdecl.const_type
698

    
699
let print_alloc_instance fmt (i, (m, static)) =
700
  fprintf fmt "_alloc->%s = %a (%a);@,"
701
    i
702
    pp_machine_alloc_name (node_name m)
703
    (Utils.fprintf_list ~sep:", " Dimension.pp_dimension) static
704

    
705
let print_alloc_array fmt vdecl =
706
  let base_type = Types.array_base_type vdecl.var_type in
707
  let size_types = Types.array_type_multi_dimension vdecl.var_type in
708
  let size_type = Dimension.multi_dimension_product vdecl.var_loc size_types in
709
  fprintf fmt "_alloc->%s = (%a*) malloc((%a)*sizeof(%a));@,assert(_alloc->%s);@,"
710
    vdecl.var_id
711
    (pp_c_type "") base_type
712
    Dimension.pp_dimension size_type
713
    (pp_c_type "") base_type
714
    vdecl.var_id
715

    
716
let print_alloc_code fmt m =
717
  let array_mem = List.filter (fun v -> Types.is_array_type v.var_type) m.mmemory in
718
  fprintf fmt "%a *_alloc;@,_alloc = (%a *) malloc(sizeof(%a));@,assert(_alloc);@,%a%areturn _alloc;"
719
    pp_machine_memtype_name m.mname.node_id
720
    pp_machine_memtype_name m.mname.node_id
721
    pp_machine_memtype_name m.mname.node_id
722
    (Utils.fprintf_list ~sep:"" print_alloc_array) array_mem
723
    (Utils.fprintf_list ~sep:"" print_alloc_instance) m.minstances
724

    
725
let print_step_code fmt m self =
726
  if not (!Options.ansi && is_generic_node { top_decl_desc = Node m.mname; top_decl_loc = Location.dummy_loc })
727
  then
728
    (* C99 code *)
729
    let array_mems = List.filter (fun v -> Types.is_array_type v.var_type) m.mmemory in
730
    fprintf fmt "@[<v 2>%a {@,%a%t%a%t@,%a%a%t%t@]@,}@.@."
731
      (print_step_prototype self) (m.mname.node_id, m.mstep.step_inputs, m.mstep.step_outputs)
732
      (* locals *)
733
      (Utils.fprintf_list ~sep:";@," pp_c_decl_local_var) m.mstep.step_locals
734
      (Utils.pp_final_char_if_non_empty ";@," m.mstep.step_locals)
735
      (* array mems *)
736
      (Utils.fprintf_list ~sep:";@," (pp_c_decl_array_mem self)) array_mems
737
      (Utils.pp_final_char_if_non_empty ";@," array_mems)
738
      (* check assertions *)
739
      (pp_c_checks self) m
740
      (* instrs *)
741
      (Utils.fprintf_list ~sep:"@," (pp_machine_instr m self)) m.mstep.step_instrs
742
      (Utils.pp_newline_if_non_empty m.mstep.step_instrs)
743
      (fun fmt -> fprintf fmt "return;")
744
  else
745
    (* C90 code *)
746
    let (gen_locals, base_locals) = List.partition (fun v -> Types.is_generic_type v.var_type) m.mstep.step_locals in
747
    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
748
    fprintf fmt "@[<v 2>%a {@,%a%t@,%a%a%t%t@]@,}@.@."
749
      (print_step_prototype self) (m.mname.node_id, (m.mstep.step_inputs@gen_locals@gen_calls), m.mstep.step_outputs)
750
      (* locals *)
751
      (Utils.fprintf_list ~sep:";@," pp_c_decl_local_var) base_locals
752
      (Utils.pp_final_char_if_non_empty ";" base_locals)
753
      (* check assertions *)
754
      (pp_c_checks self) m
755
      (* instrs *)
756
      (Utils.fprintf_list ~sep:"@," (pp_machine_instr m self)) m.mstep.step_instrs
757
      (Utils.pp_newline_if_non_empty m.mstep.step_instrs)
758
      (fun fmt -> fprintf fmt "return;")
759

    
760
let print_machine fmt m =
761
  if m.mname.node_id <> arrow_id
762
  then (
763
  (* We don't print arrow function *)
764
  (* Alloc function, only if non static mode *)
765
    if (not !Options.static_mem) then  
766
      (
767
	fprintf fmt "@[<v 2>%a {@,%a@]@,}@.@."
768
	  print_alloc_prototype (m.mname.node_id, m.mstatic)
769
	  print_alloc_code m;
770
      );
771
    let self = mk_self m in
772
    (* Reset function *)
773
    fprintf fmt "@[<v 2>%a {@,%a%treturn;@]@,}@.@."
774
      (print_reset_prototype self) (m.mname.node_id, m.mstatic)
775
      (Utils.fprintf_list ~sep:"@," (pp_machine_instr m self)) m.minit
776
      (Utils.pp_newline_if_non_empty m.minit);
777
    (* Step function *)
778
    print_step_code fmt m self
779
  )
780

    
781
(********************************************************************************************)
782
(*                         Main related functions                                           *)
783
(********************************************************************************************)
784

    
785
let print_get_input fmt v =
786
  match v.var_type.Types.tdesc with
787
    | Types.Tint -> fprintf fmt "_get_int(\"%s\")" v.var_id
788
    | Types.Tbool -> fprintf fmt "_get_bool(\"%s\")" v.var_id
789
    | Types.Treal -> fprintf fmt "_get_double(\"%s\")" v.var_id
790
    | _ -> assert false
791

    
792
let print_put_outputs fmt ol = 
793
  let po fmt o =
794
    match o.var_type.Types.tdesc with
795
    | Types.Tint -> fprintf fmt "_put_int(\"%s\", %s)" o.var_id o.var_id
796
    | Types.Tbool -> fprintf fmt "_put_bool(\"%s\", %s)" o.var_id o.var_id
797
    | Types.Treal -> fprintf fmt "_put_double(\"%s\", %s)" o.var_id o.var_id
798
    | _ -> assert false
799
  in
800
  List.iter (fprintf fmt "@ %a;" po) ol
801

    
802
let print_main_fun machines m fmt =
803
  let mname = m.mname.node_id in
804
  let main_mem =
805
    if (!Options.static_mem && !Options.main_node <> "")
806
    then "&main_mem"
807
    else "main_mem" in
808
  fprintf fmt "@[<v 2>int main (int argc, char *argv[]) {@ ";
809
  fprintf fmt "/* Declaration of inputs/outputs variables */@ ";
810
  List.iter 
811
    (fun v -> fprintf fmt "%a = %a;@ " (pp_c_type v.var_id) v.var_type pp_c_initialize v.var_type
812
    ) m.mstep.step_inputs;
813
  List.iter 
814
    (fun v -> fprintf fmt "%a = %a;@ " (pp_c_type v.var_id) v.var_type pp_c_initialize v.var_type
815
    ) m.mstep.step_outputs;
816
  fprintf fmt "@ /* Main memory allocation */@ ";
817
  if (!Options.static_mem && !Options.main_node <> "")
818
  then (fprintf fmt "%a(static,main_mem);@ " pp_machine_static_alloc_name mname)
819
  else (fprintf fmt "%a *main_mem = %a();@ " pp_machine_memtype_name mname pp_machine_alloc_name mname);
820
  fprintf fmt "@ /* Initialize the main memory */@ ";
821
  fprintf fmt "%a(%s);@ " pp_machine_reset_name mname main_mem;
822
  fprintf fmt "@ ISATTY = isatty(0);@ ";
823
  fprintf fmt "@ /* Infinite loop */@ ";
824
  fprintf fmt "@[<v 2>while(1){@ ";
825
  fprintf fmt  "fflush(stdout);@ ";
826
  List.iter 
827
    (fun v -> fprintf fmt "%s = %a;@ "
828
      v.var_id
829
      print_get_input v
830
    ) m.mstep.step_inputs;
831
  (match m.mstep.step_outputs with
832
    (* | [] -> ( *)
833
    (*   fprintf fmt "%a(%a%t%s);@ "  *)
834
    (* 	pp_machine_step_name mname *)
835
    (* 	(Utils.fprintf_list ~sep:", " (fun fmt v -> pp_print_string fmt v.var_id)) m.mstep.step_inputs *)
836
    (* 	(pp_final_char_if_non_empty ", " m.mstep.step_inputs) *)
837
    (* 	main_mem *)
838
    (* ) *)
839
    (* | [o] -> ( *)
840
    (*   fprintf fmt "%s = %a(%a%t%a, %s);%a" *)
841
    (* 	o.var_id *)
842
    (* 	pp_machine_step_name mname *)
843
    (* 	(Utils.fprintf_list ~sep:", " (fun fmt v -> pp_print_string fmt v.var_id)) m.mstep.step_inputs *)
844
    (* 	(pp_final_char_if_non_empty ", " m.mstep.step_inputs) *)
845
    (* 	(Utils.fprintf_list ~sep:", " (fun fmt v -> fprintf fmt "&%s" v.var_id)) m.mstep.step_outputs *)
846
    (* 	main_mem *)
847
    (* 	print_put_outputs [o]) *)
848
    | _ -> (
849
      fprintf fmt "%a(%a%t%a, %s);%a"
850
	pp_machine_step_name mname
851
	(Utils.fprintf_list ~sep:", " (fun fmt v -> pp_print_string fmt v.var_id)) m.mstep.step_inputs
852
	(Utils.pp_final_char_if_non_empty ", " m.mstep.step_inputs)
853
	(Utils.fprintf_list ~sep:", " (fun fmt v -> fprintf fmt "&%s" v.var_id)) m.mstep.step_outputs
854
	main_mem
855
	print_put_outputs m.mstep.step_outputs)
856
  );
857
  fprintf fmt "@]@ }@ ";
858
  fprintf fmt "return 1;";
859
  fprintf fmt "@]@ }@."       
860

    
861
let print_main_header fmt =
862
  fprintf fmt "#include <stdio.h>@.#include <unistd.h>@.#include \"%s/include/lustrec/io_frontend.h\"@." Version.prefix
863

    
864
let rec pp_c_struct_type_field filename cpt var fmt (label, tdesc) =
865
  fprintf fmt "%a %a" (pp_c_type_decl filename cpt var) tdesc pp_print_string label
866
and pp_c_type_decl filename cpt var fmt tdecl =
867
  match tdecl with
868
  | Tydec_any           -> assert false
869
  | Tydec_int           -> fprintf fmt "int %s" var
870
  | Tydec_real          -> fprintf fmt "double %s" var
871
  | Tydec_float         -> fprintf fmt "float %s" var
872
  | Tydec_bool          -> fprintf fmt "_Bool %s" var
873
  | Tydec_clock ty      -> pp_c_type_decl filename cpt var fmt ty
874
  | Tydec_const c       -> fprintf fmt "%s %s" c var
875
  | Tydec_array (d, ty) -> fprintf fmt "%a[%a]" (pp_c_type_decl filename cpt var) ty pp_c_dimension d
876
  | Tydec_enum tl ->
877
    begin
878
      incr cpt;
879
      fprintf fmt "enum _enum_%s_%d { %a } %s" filename !cpt (Utils.fprintf_list ~sep:", " pp_print_string) tl var
880
    end
881
  | Tydec_struct fl ->
882
    begin
883
      incr cpt;
884
      fprintf fmt "struct _struct_%s_%d { %a } %s" filename !cpt (Utils.fprintf_list ~sep:"; " (pp_c_struct_type_field filename cpt var)) fl var
885
    end
886

    
887
let print_type_definitions fmt filename =
888
  let cpt_type = ref 0 in
889
  Hashtbl.iter (fun typ def ->
890
    match typ with
891
    | Tydec_const var ->
892
      fprintf fmt "typedef %a;@.@."
893
	(pp_c_type_decl filename cpt_type var) def
894
    | _        -> ()) type_table
895

    
896
let print_makefile basename nodename dependencies fmt =
897
  fprintf fmt "GCC=gcc@.";
898
  fprintf fmt "LUSTREC=%s@." Sys.executable_name;
899
  fprintf fmt "LUSTREC_BASE=%s@." (Filename.dirname (Filename.dirname Sys.executable_name));
900
  fprintf fmt "INC=${LUSTREC_BASE}/include/lustrec@.";
901
  fprintf fmt "@.";
902
  fprintf fmt "%s_%s:@." basename nodename;
903
  fprintf fmt "\t${GCC} -I${INC} -I. -c %s.c@." basename;    
904
  List.iter (fun s -> (* Format.eprintf "Adding dependency: %s@." s;  *)
905
    fprintf fmt "\t${GCC} -I${INC} -c %s@." s)
906
    (("${INC}/io_frontend.c")::(List.map (fun s -> s ^ ".c") dependencies));    
907
(*  fprintf fmt "\t${GCC} -I${INC} -c ${INC}/StdLibrary.c@."; *)
908
(*  fprintf fmt "\t${GCC} -o %s_%s io_frontend.o StdLibrary.o -lm %s.o@." basename nodename basename*)
909
  fprintf fmt "\t${GCC} -o %s_%s io_frontend.o %a -lm %s.o@." basename nodename 
910
    (Utils.fprintf_list ~sep:" " (fun fmt s -> Format.fprintf fmt "%s.o" s)) dependencies basename;
911
 fprintf fmt "@.";
912
 fprintf fmt "clean:@.";
913
 fprintf fmt "\t\\rm -f *.o %s_%s@." basename nodename
914

    
915

    
916

    
917

    
918
(********************************************************************************************)
919
(*                         Translation function                                             *)
920
(********************************************************************************************)
921

    
922
let translate_to_c header_fmt source_fmt makefile_fmt spec_fmt_opt basename prog machines dependencies =
923
  (* Generating H file *)
924

    
925
  (* Include once: start *)
926
  let baseNAME = String.uppercase basename in
927
  let baseNAME = Str.global_replace (Str.regexp "\\.\\|\\ ") "_" baseNAME in
928
  (* Print the svn version number and the supported C standard (C90 or C99) *)
929
  print_version header_fmt;
930
  fprintf header_fmt "#ifndef _%s@.#define _%s@." baseNAME baseNAME;
931
  pp_print_newline header_fmt ();
932
  fprintf header_fmt "/* Imports standard library */@.";
933
  (* imports standard library definitions (arrow) *)
934
  print_import_standard header_fmt;
935
  pp_print_newline header_fmt ();
936
  fprintf header_fmt "/* Types definitions */@.";
937
  (* Print the type definitions from the type table *)
938
  print_type_definitions header_fmt basename;
939
  pp_print_newline header_fmt ();
940
  (* Print the global constant declarations. *)
941
  fprintf header_fmt "/* Global constant (declarations, definitions are in C file) */@.";
942
  List.iter (fun c -> print_const_decl header_fmt c) (get_consts prog);
943
  pp_print_newline header_fmt ();
944
  (* Print the struct declarations of all machines. *)
945
  fprintf header_fmt "/* Struct declarations */@.";
946
  List.iter (print_machine_struct header_fmt) machines;
947
  pp_print_newline header_fmt ();
948
  (* Print the prototypes of all machines *)
949
  fprintf header_fmt "/* Nodes declarations */@.";
950
  List.iter (print_machine_decl header_fmt) machines;
951
  pp_print_newline header_fmt ();
952
  (* Include once: end *)
953
  fprintf header_fmt "#endif@.";
954
  pp_print_newline header_fmt ();
955

    
956
  (* Generating C file *)
957

    
958
  (* If a main node is identified, generate a main function for it *)
959
  let main_include, main_print, main_makefile =
960
    match !Options.main_node with
961
      | "" -> (fun _ -> ()), (fun _ -> ()), (fun _ -> ())
962
      | main_node -> (
963
	match Machine_code.get_machine_opt main_node machines with
964
	| None -> eprintf "Unable to find a main node named %s@.@?" main_node; (fun _ -> ()), (fun _ -> ()), (fun _ -> ())
965
	| Some m -> print_main_header, print_main_fun machines m, print_makefile basename !Options.main_node dependencies
966
      )
967
  in
968
  main_include source_fmt;
969
  fprintf source_fmt "#include <stdlib.h>@.#include <assert.h>@.#include \"%s\"@.@." (basename^".h");
970
  (* Print the svn version number and the supported C standard (C90 or C99) *)
971
  print_version source_fmt;
972
  (* Print the prototype of imported nodes *)
973
  fprintf source_fmt "/* Imported nodes declarations */@.";
974
  fprintf source_fmt "@[<v>";
975
  List.iter (print_prototype source_fmt) prog;
976
  fprintf source_fmt "@]@.";
977
  (* Print consts *)
978
  fprintf source_fmt "/* Global constants (definitions) */@.";
979
  List.iter (fun c -> print_const_def source_fmt c) (get_consts prog);
980
  pp_print_newline source_fmt ();
981
  (* Print nodes one by one (in the previous order) *)
982
  List.iter (print_machine source_fmt) machines;
983
  main_print source_fmt;
984

    
985
  (* Generating Makefile *)
986
  main_makefile makefile_fmt
987

    
988
(* Local Variables: *)
989
(* compile-command:"make -C .." *)
990
(* End: *)