Project

General

Profile

Statistics
| Branch: | Tag: | Revision:

lustrec / src / c_backend.ml @ 6a6abd76

History | View | Annotate | Download (40.5 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

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

    
234
let pp_c_checks self fmt m =
235
  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
236

    
237

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    
398

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

    
402
(**************************************************************************)
403

    
404

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

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

    
432

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    
687

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    
914

    
915

    
916

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

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

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

    
955
  (* Generating C file *)
956

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

    
984
  (* Generating Makefile *)
985
  main_makefile makefile_fmt
986

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