Project

General

Profile

Download (41.4 KB) Statistics
| Branch: | Tag: | Revision:
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_address_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/struct/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_address_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_address_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_address_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
  fprintf fmt "void %s (@[<v>@[%a%t@]@,@[%a@]@,@])"
508
    name
509
    (Utils.fprintf_list ~sep:",@ " pp_c_decl_input_var) inputs
510
    (Utils.pp_final_char_if_non_empty ",@ " inputs) 
511
    (Utils.fprintf_list ~sep:",@ " pp_c_decl_output_var) outputs
512

    
513
let print_step_prototype self fmt (name, inputs, outputs) =
514
  fprintf fmt "void %a (@[<v>@[%a%t@]@,@[%a@]%t@[%a *%s@]@])"
515
    pp_machine_step_name name
516
    (Utils.fprintf_list ~sep:",@ " pp_c_decl_input_var) inputs
517
    (Utils.pp_final_char_if_non_empty ",@ " inputs) 
518
    (Utils.fprintf_list ~sep:",@ " pp_c_decl_output_var) outputs
519
    (Utils.pp_final_char_if_non_empty ",@," outputs) 
520
    pp_machine_memtype_name name
521
    self
522

    
523
(********************************************************************************************)
524
(*                         Header Printing functions                                        *)
525
(********************************************************************************************)
526

    
527

    
528
let print_import_standard fmt =
529
  fprintf fmt "#include \"%s/include/lustrec/arrow.h\"@.@." Version.prefix
530

    
531
let print_import_prototype fmt decl =
532
  match decl.top_decl_desc with
533
  | Open m -> fprintf fmt "#include \"%s.h\"@," m
534
  | _ -> () (* We don't do anything here *)
535
    
536
let pp_registers_struct fmt m =
537
  if m.mmemory <> []
538
  then
539
    fprintf fmt "@[%a {@[%a; @]}@] _reg; "
540
      pp_machine_regtype_name m.mname.node_id
541
      (Utils.fprintf_list ~sep:"; " pp_c_decl_struct_var) m.mmemory
542
  else
543
    ()
544

    
545
let print_machine_struct fmt m =
546
  if fst (get_stateless_status m) then
547
    begin
548
    end
549
  else
550
    begin
551
      (* Define struct *)
552
      fprintf fmt "@[%a {@[%a%a%t@]};@]@."
553
	pp_machine_memtype_name m.mname.node_id
554
	pp_registers_struct m
555
	(Utils.fprintf_list ~sep:"; " pp_c_decl_instance_var) m.minstances
556
	(Utils.pp_final_char_if_non_empty "; " m.minstances)
557
    end
558

    
559
let print_static_declare_instance attr fmt (i, (m, static)) =
560
  fprintf fmt "%a(%s, %a%t%s)"
561
    pp_machine_static_declare_name (node_name m)
562
    attr
563
    (Utils.fprintf_list ~sep:", " Dimension.pp_dimension) static
564
    (Utils.pp_final_char_if_non_empty ", " static)
565
    i
566

    
567
let print_static_declare_macro fmt m =
568
  let array_mem = List.filter (fun v -> Types.is_array_type v.var_type) m.mmemory in
569
  let inst = mk_instance m in
570
  let attr = mk_attribute m in
571
  fprintf fmt "@[<v 2>#define %a(%s, %a%t%s)\\@,%s %a %s;\\@,%a%t%a;@,@]"
572
    pp_machine_static_declare_name m.mname.node_id
573
    attr
574
    (Utils.fprintf_list ~sep:", " (pp_c_var_read m)) m.mstatic
575
    (Utils.pp_final_char_if_non_empty ", " m.mstatic)
576
    inst
577
    attr
578
    pp_machine_memtype_name m.mname.node_id
579
    inst
580
    (Utils.fprintf_list ~sep:";\\@," pp_c_decl_local_var) array_mem
581
    (Utils.pp_final_char_if_non_empty ";\\@," array_mem)
582
    (Utils.fprintf_list ~sep:";\\@,"
583
       (fun fmt (i',m') ->
584
	 let path = sprintf "inst ## _%s" i' in
585
	 fprintf fmt "%a"
586
	   (print_static_declare_instance attr) (path,m')
587
       )) m.minstances
588

    
589
      
590
let print_static_link_instance fmt (i, (m, _)) =
591
 fprintf fmt "%a(%s)" pp_machine_static_link_name (node_name m) i
592

    
593
let print_static_link_macro fmt m =
594
  let array_mem = List.filter (fun v -> Types.is_array_type v.var_type) m.mmemory in
595
  fprintf fmt "@[<v>@[<v 2>#define %a(inst) do {\\@,%a%t%a;\\@]@,} while (0)@.@]"
596
    pp_machine_static_link_name m.mname.node_id
597
    (Utils.fprintf_list ~sep:";\\@,"
598
       (fun fmt v ->
599
	 fprintf fmt "inst.%s = &%s"
600
	   v.var_id
601
	   v.var_id
602
       )) array_mem
603
    (Utils.pp_final_char_if_non_empty ";\\@," array_mem)
604
    (Utils.fprintf_list ~sep:";\\@,"
605
       (fun fmt (i',m') ->
606
	 let path = sprintf "inst ## _%s" i' in
607
	 fprintf fmt "%a;\\@,inst.%s = &%s"
608
	   print_static_link_instance (path,m')
609
	   i'
610
	   path
611
       )) m.minstances
612
      
613
let print_static_alloc_macro fmt m =
614
  fprintf fmt "@[<v>@[<v 2>#define %a(attr,%a%tinst)\\@,%a(attr,%a%tinst);\\@,%a(inst);@]@,@]@."
615
    pp_machine_static_alloc_name m.mname.node_id
616
    (Utils.fprintf_list ~sep:", " (pp_c_var_read m)) m.mstatic
617
    (Utils.pp_final_char_if_non_empty ", " m.mstatic)
618
    pp_machine_static_declare_name m.mname.node_id
619
    (Utils.fprintf_list ~sep:", " (pp_c_var_read m)) m.mstatic
620
    (Utils.pp_final_char_if_non_empty ", " m.mstatic)
621
    pp_machine_static_link_name m.mname.node_id
622

    
623
let print_machine_decl fmt m =
624
  if fst (get_stateless_status m) then
625
    begin
626
      (* Print specification if any *)
627
      (match m.mspec with
628
      | None -> ()
629
      | Some spec -> 
630
	pp_acsl_spec m.mstep.step_outputs fmt spec
631
      );
632
      fprintf fmt "extern %a;@.@."
633
	print_stateless_prototype
634
	(m.mname.node_id, m.mstep.step_inputs, m.mstep.step_outputs)
635
    end
636
  else
637
    begin
638
      (* Static allocation *)
639
      if !Options.static_mem
640
      then (
641
	fprintf fmt "%a@.%a@.%a@."
642
	  print_static_declare_macro m
643
	  print_static_link_macro m
644
	  print_static_alloc_macro m
645
      )
646
      else ( 
647
        (* Dynamic allocation *)
648
	fprintf fmt "extern %a;@.@."
649
	  print_alloc_prototype (m.mname.node_id, m.mstatic)
650
      );
651
      let self = mk_self m in
652
      fprintf fmt "extern %a;@.@."
653
	(print_reset_prototype self) (m.mname.node_id, m.mstatic);
654
      (* Print specification if any *)
655
      (match m.mspec with
656
      | None -> ()
657
      | Some spec -> 
658
	pp_acsl_spec m.mstep.step_outputs fmt spec
659
      );
660
      fprintf fmt "extern %a;@.@."
661
	(print_step_prototype self)
662
	(m.mname.node_id, m.mstep.step_inputs, m.mstep.step_outputs)
663
    end
664

    
665

    
666
(********************************************************************************************)
667
(*                         C file Printing functions                                        *)
668
(********************************************************************************************)
669

    
670
let print_const_def fmt cdecl =
671
  fprintf fmt "%a = %a;@." (pp_c_type cdecl.const_id) cdecl.const_type pp_c_const cdecl.const_value 
672

    
673
let print_const_decl fmt cdecl =
674
  fprintf fmt "extern %a;@." (pp_c_type cdecl.const_id) cdecl.const_type
675

    
676
let print_alloc_instance fmt (i, (m, static)) =
677
  fprintf fmt "_alloc->%s = %a (%a);@,"
678
    i
679
    pp_machine_alloc_name (node_name m)
680
    (Utils.fprintf_list ~sep:", " Dimension.pp_dimension) static
681

    
682
let print_alloc_array fmt vdecl =
683
  let base_type = Types.array_base_type vdecl.var_type in
684
  let size_types = Types.array_type_multi_dimension vdecl.var_type in
685
  let size_type = Dimension.multi_dimension_product vdecl.var_loc size_types in
686
  fprintf fmt "_alloc->%s = (%a*) malloc((%a)*sizeof(%a));@,assert(_alloc->%s);@,"
687
    vdecl.var_id
688
    (pp_c_type "") base_type
689
    Dimension.pp_dimension size_type
690
    (pp_c_type "") base_type
691
    vdecl.var_id
692

    
693
let print_alloc_code fmt m =
694
  let array_mem = List.filter (fun v -> Types.is_array_type v.var_type) m.mmemory in
695
  fprintf fmt "%a *_alloc;@,_alloc = (%a *) malloc(sizeof(%a));@,assert(_alloc);@,%a%areturn _alloc;"
696
    pp_machine_memtype_name m.mname.node_id
697
    pp_machine_memtype_name m.mname.node_id
698
    pp_machine_memtype_name m.mname.node_id
699
    (Utils.fprintf_list ~sep:"" print_alloc_array) array_mem
700
    (Utils.fprintf_list ~sep:"" print_alloc_instance) m.minstances
701

    
702
let print_stateless_code fmt m =
703
  let self = "__ERROR__" in
704
  if not (!Options.ansi && is_generic_node { top_decl_desc = Node m.mname; top_decl_loc = Location.dummy_loc })
705
  then
706
    (* C99 code *)
707
    fprintf fmt "@[<v 2>%a {@,%a%t@,%a%a%t%t@]@,}@.@."
708
      print_stateless_prototype (m.mname.node_id, m.mstep.step_inputs, m.mstep.step_outputs)
709
      (* locals *)
710
      (Utils.fprintf_list ~sep:";@," pp_c_decl_local_var) m.mstep.step_locals
711
      (Utils.pp_final_char_if_non_empty ";@," m.mstep.step_locals)
712
      (* check assertions *)
713
      (pp_c_checks self) m
714
      (* instrs *)
715
      (Utils.fprintf_list ~sep:"@," (pp_machine_instr m self)) m.mstep.step_instrs
716
      (Utils.pp_newline_if_non_empty m.mstep.step_instrs)
717
      (fun fmt -> fprintf fmt "return;")
718
  else
719
    (* C90 code *)
720
    let (gen_locals, base_locals) = List.partition (fun v -> Types.is_generic_type v.var_type) m.mstep.step_locals in
721
    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
722
    fprintf fmt "@[<v 2>%a {@,%a%t@,%a%a%t%t@]@,}@.@."
723
      print_stateless_prototype (m.mname.node_id, (m.mstep.step_inputs@gen_locals@gen_calls), m.mstep.step_outputs)
724
      (* locals *)
725
      (Utils.fprintf_list ~sep:";@," pp_c_decl_local_var) base_locals
726
      (Utils.pp_final_char_if_non_empty ";" base_locals)
727
      (* check assertions *)
728
      (pp_c_checks self) m
729
      (* instrs *)
730
      (Utils.fprintf_list ~sep:"@," (pp_machine_instr m self)) m.mstep.step_instrs
731
      (Utils.pp_newline_if_non_empty m.mstep.step_instrs)
732
      (fun fmt -> fprintf fmt "return;")
733

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

    
769
let print_machine fmt m =
770
  if fst (get_stateless_status m) then
771
    begin
772
      (* Step function *)
773
      print_stateless_code fmt m
774
    end
775
  else
776
    begin
777
      (* Alloc function, only if non static mode *)
778
      if (not !Options.static_mem) then  
779
	(
780
	  fprintf fmt "@[<v 2>%a {@,%a@]@,}@.@."
781
	    print_alloc_prototype (m.mname.node_id, m.mstatic)
782
	    print_alloc_code m;
783
	);
784
      let self = mk_self m in
785
      (* Reset function *)
786
      fprintf fmt "@[<v 2>%a {@,%a%treturn;@]@,}@.@."
787
	(print_reset_prototype self) (m.mname.node_id, m.mstatic)
788
	(Utils.fprintf_list ~sep:"@," (pp_machine_instr m self)) m.minit
789
	(Utils.pp_newline_if_non_empty m.minit);
790
      (* Step function *)
791
      print_step_code fmt m self
792
    end
793

    
794
(********************************************************************************************)
795
(*                         Main related functions                                           *)
796
(********************************************************************************************)
797

    
798
let print_get_input fmt v =
799
  match v.var_type.Types.tdesc with
800
    | Types.Tint -> fprintf fmt "_get_int(\"%s\")" v.var_id
801
    | Types.Tbool -> fprintf fmt "_get_bool(\"%s\")" v.var_id
802
    | Types.Treal -> fprintf fmt "_get_double(\"%s\")" v.var_id
803
    | _ -> assert false
804

    
805
let print_put_outputs fmt ol = 
806
  let po fmt o =
807
    match o.var_type.Types.tdesc with
808
    | Types.Tint -> fprintf fmt "_put_int(\"%s\", %s)" o.var_id o.var_id
809
    | Types.Tbool -> fprintf fmt "_put_bool(\"%s\", %s)" o.var_id o.var_id
810
    | Types.Treal -> fprintf fmt "_put_double(\"%s\", %s)" o.var_id o.var_id
811
    | _ -> assert false
812
  in
813
  List.iter (fprintf fmt "@ %a;" po) ol
814

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

    
874
let print_main_header fmt =
875
  fprintf fmt "#include <stdio.h>@.#include <unistd.h>@.#include \"%s/include/lustrec/io_frontend.h\"@." Version.prefix
876

    
877
let rec pp_c_struct_type_field filename cpt fmt (label, tdesc) =
878
  fprintf fmt "%a;" (pp_c_type_decl filename cpt label) tdesc
879
and pp_c_type_decl filename cpt var fmt tdecl =
880
  match tdecl with
881
  | Tydec_any           -> assert false
882
  | Tydec_int           -> fprintf fmt "int %s" var
883
  | Tydec_real          -> fprintf fmt "double %s" var
884
  | Tydec_float         -> fprintf fmt "float %s" var
885
  | Tydec_bool          -> fprintf fmt "_Bool %s" var
886
  | Tydec_clock ty      -> pp_c_type_decl filename cpt var fmt ty
887
  | Tydec_const c       -> fprintf fmt "%s %s" c var
888
  | Tydec_array (d, ty) -> fprintf fmt "%a[%a]" (pp_c_type_decl filename cpt var) ty pp_c_dimension d
889
  | Tydec_enum tl ->
890
    begin
891
      incr cpt;
892
      fprintf fmt "enum _enum_%s_%d { %a } %s" filename !cpt (Utils.fprintf_list ~sep:", " pp_print_string) tl var
893
    end
894
  | Tydec_struct fl ->
895
    begin
896
      incr cpt;
897
      fprintf fmt "struct _struct_%s_%d { %a } %s" filename !cpt (Utils.fprintf_list ~sep:" " (pp_c_struct_type_field filename cpt)) fl var
898
    end
899

    
900
let print_type_definitions fmt filename =
901
  let cpt_type = ref 0 in
902
  Hashtbl.iter (fun typ def ->
903
    match typ with
904
    | Tydec_const var ->
905
      fprintf fmt "typedef %a;@.@."
906
	(pp_c_type_decl filename cpt_type var) def
907
    | _        -> ()) type_table
908

    
909
let print_makefile basename nodename dependencies fmt =
910
  fprintf fmt "GCC=gcc@.";
911
  fprintf fmt "LUSTREC=%s@." Sys.executable_name;
912
  fprintf fmt "LUSTREC_BASE=%s@." (Filename.dirname (Filename.dirname Sys.executable_name));
913
  fprintf fmt "INC=${LUSTREC_BASE}/include/lustrec@.";
914
  fprintf fmt "@.";
915
  fprintf fmt "%s_%s:@." basename nodename;
916
  fprintf fmt "\t${GCC} -I${INC} -I. -c %s.c@." basename;    
917
  List.iter (fun s -> (* Format.eprintf "Adding dependency: %s@." s;  *)
918
    fprintf fmt "\t${GCC} -I${INC} -c %s@." s)
919
    (("${INC}/io_frontend.c")::(List.map (fun s -> s ^ ".c") dependencies));    
920
(*  fprintf fmt "\t${GCC} -I${INC} -c ${INC}/StdLibrary.c@."; *)
921
(*  fprintf fmt "\t${GCC} -o %s_%s io_frontend.o StdLibrary.o -lm %s.o@." basename nodename basename*)
922
  fprintf fmt "\t${GCC} -o %s_%s io_frontend.o %a -lm %s.o@." basename nodename 
923
    (Utils.fprintf_list ~sep:" " (fun fmt s -> Format.fprintf fmt "%s.o" s)) dependencies basename;
924
 fprintf fmt "@.";
925
 fprintf fmt "clean:@.";
926
 fprintf fmt "\t\\rm -f *.o %s_%s@." basename nodename
927

    
928

    
929

    
930

    
931
(********************************************************************************************)
932
(*                         Translation function                                             *)
933
(********************************************************************************************)
934

    
935
let translate_to_c header_fmt source_fmt makefile_fmt spec_fmt_opt basename prog machines dependencies =
936
  (* Generating H file *)
937

    
938
  (* Include once: start *)
939
  let baseNAME = String.uppercase basename in
940
  let baseNAME = Str.global_replace (Str.regexp "\\.\\|\\ ") "_" baseNAME in
941
  (* Print the svn version number and the supported C standard (C90 or C99) *)
942
  print_version header_fmt;
943
  fprintf header_fmt "#ifndef _%s@.#define _%s@." baseNAME baseNAME;
944
  pp_print_newline header_fmt ();
945
  fprintf header_fmt "/* Imports standard library */@.";
946
  (* imports standard library definitions (arrow) *)
947
  print_import_standard header_fmt;
948
  pp_print_newline header_fmt ();
949
  fprintf header_fmt "/* Types definitions */@.";
950
  (* Print the type definitions from the type table *)
951
  print_type_definitions header_fmt basename;
952
  pp_print_newline header_fmt ();
953
  (* Print the global constant declarations. *)
954
  fprintf header_fmt "/* Global constant (declarations, definitions are in C file) */@.";
955
  List.iter (fun c -> print_const_decl header_fmt c) (get_consts prog);
956
  pp_print_newline header_fmt ();
957
  (* Print the struct declarations of all machines. *)
958
  fprintf header_fmt "/* Struct declarations */@.";
959
  List.iter (print_machine_struct header_fmt) machines;
960
  pp_print_newline header_fmt ();
961
  (* Print the prototypes of all machines *)
962
  fprintf header_fmt "/* Nodes declarations */@.";
963
  List.iter (print_machine_decl header_fmt) machines;
964
  pp_print_newline header_fmt ();
965
  (* Include once: end *)
966
  fprintf header_fmt "#endif@.";
967
  pp_print_newline header_fmt ();
968

    
969
  (* Generating C file *)
970

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

    
998
  (* Generating Makefile *)
999
  main_makefile makefile_fmt
1000

    
1001
(* Local Variables: *)
1002
(* compile-command:"make -C .." *)
1003
(* End: *)
(5-5/47)