Project

General

Profile

Statistics
| Branch: | Tag: | Revision:

lustrec / src / backends / C / c_backend_common.ml @ 01d48bb0

History | View | Annotate | Download (13.5 KB)

1
(********************************************************************)
2
(*                                                                  *)
3
(*  The LustreC compiler toolset   /  The LustreC Development Team  *)
4
(*  Copyright 2012 -    --   ONERA - CNRS - INPT                    *)
5
(*                                                                  *)
6
(*  LustreC is free software, distributed WITHOUT ANY WARRANTY      *)
7
(*  under the terms of the GNU Lesser General Public License        *)
8
(*  version 2.1.                                                    *)
9
(*                                                                  *)
10
(********************************************************************)
11

    
12
open Format
13
open LustreSpec
14
open Corelang
15
open Machine_code
16

    
17

    
18
let print_version fmt =
19
  Format.fprintf fmt 
20
    "/* @[<v>C code generated by %s@,SVN version number %s@,Code is %s compliant */@,@]@."
21
    (Filename.basename Sys.executable_name) 
22
    Version.number 
23
    (if !Options.ansi then "ANSI C90" else "C99")
24
 
25
(* Generation of a non-clashing name for the self memory variable (for step and reset functions) *)
26
let mk_self m =
27
  let used name =
28
       (List.exists (fun v -> v.var_id = name) m.mstep.step_inputs)
29
    || (List.exists (fun v -> v.var_id = name) m.mstep.step_outputs)
30
    || (List.exists (fun v -> v.var_id = name) m.mstep.step_locals)
31
    || (List.exists (fun v -> v.var_id = name) m.mmemory) in
32
  mk_new_name used "self"
33

    
34
(* Generation of a non-clashing name for the instance variable of static allocation macro *)
35
let mk_instance m =
36
  let used name =
37
       (List.exists (fun v -> v.var_id = name) m.mstep.step_inputs)
38
    || (List.exists (fun v -> v.var_id = name) m.mmemory) in
39
  mk_new_name used "inst"
40

    
41
(* Generation of a non-clashing name for the attribute variable of static allocation macro *)
42
let mk_attribute m =
43
  let used name =
44
       (List.exists (fun v -> v.var_id = name) m.mstep.step_inputs)
45
    || (List.exists (fun v -> v.var_id = name) m.mmemory) in
46
  mk_new_name used "attr"
47

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

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

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

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

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

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

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

    
98
let is_basic_c_type t =
99
  match (Types.repr t).Types.tdesc with
100
  | Types.Tbool | Types.Treal | Types.Tint  -> true
101
  | _                                       -> false
102

    
103
let pp_basic_c_type fmt t =
104
  match (Types.repr t).Types.tdesc with
105
  | Types.Tbool           -> fprintf fmt "_Bool"
106
  | Types.Treal           -> fprintf fmt "double"
107
  | Types.Tint            -> fprintf fmt "int"
108
  | _ -> assert false (* Not a basic C type. Do not handle arrays or pointers *)
109

    
110
let pp_c_type var fmt t =
111
  let rec aux t pp_suffix =
112
    match (Types.repr t).Types.tdesc with
113
    | Types.Tclock t'       -> aux t' pp_suffix
114
    | Types.Tbool | Types.Treal | Types.Tint 
115
                            -> fprintf fmt "%a %s%a" pp_basic_c_type t var pp_suffix ()
116
    | Types.Tarray (d, t')  ->
117
      let pp_suffix' fmt () = fprintf fmt "%a[%a]" pp_suffix () pp_c_dimension d in
118
      aux t' pp_suffix'
119
    | Types.Tstatic (_, t') -> fprintf fmt "const "; aux t' pp_suffix
120
    | Types.Tconst ty       -> fprintf fmt "%s %s" ty var
121
    | Types.Tarrow (_, _)   -> fprintf fmt "void (*%s)()" var
122
    | _                     -> eprintf "internal error: pp_c_type %a@." Types.print_ty t; assert false
123
  in aux t (fun fmt () -> ())
124

    
125
let rec pp_c_initialize fmt t = 
126
  match (Types.repr t).Types.tdesc with
127
  | Types.Tint -> pp_print_string fmt "0"
128
  | Types.Tclock t' -> pp_c_initialize fmt t'
129
  | Types.Tbool -> pp_print_string fmt "0" 
130
  | Types.Treal -> pp_print_string fmt "0."
131
  | Types.Tarray (d, t') when Dimension.is_dimension_const d ->
132
    fprintf fmt "{%a}"
133
      (Utils.fprintf_list ~sep:"," (fun fmt _ -> pp_c_initialize fmt t'))
134
      (Utils.duplicate 0 (Dimension.size_const_dimension d))
135
  | _ -> assert false
136

    
137

    
138
let pp_c_tag fmt t =
139
 pp_print_string fmt (if t = tag_true then "1" else if t = tag_false then "0" else t)
140

    
141
(* Prints a constant value *)
142
let rec pp_c_const fmt c =
143
  match c with
144
    | Const_int i     -> pp_print_int fmt i
145
    | Const_real r    -> pp_print_string fmt r
146
    | Const_float r   -> pp_print_float fmt r
147
    | Const_tag t     -> pp_c_tag fmt t
148
    | Const_array ca  -> fprintf fmt "{%a }" (Utils.fprintf_list ~sep:", " pp_c_const) ca
149
    | Const_struct fl -> fprintf fmt "{%a }" (Utils.fprintf_list ~sep:", " (fun fmt (f, c) -> pp_c_const fmt c)) fl
150
    | Const_string _ -> assert false (* string occurs in annotations not in C *)
151

    
152
(* Prints a value expression [v], with internal function calls only.
153
   [pp_var] is a printer for variables (typically [pp_c_var_read]),
154
   but an offset suffix may be added for array variables
155
*)
156
let rec pp_c_val self pp_var fmt v =
157
  match v with
158
  | Cst c         -> pp_c_const fmt c
159
  | Array vl      -> fprintf fmt "{%a}" (Utils.fprintf_list ~sep:", " (pp_c_val self pp_var)) vl
160
  | Access (t, i) -> fprintf fmt "%a[%a]" (pp_c_val self pp_var) t (pp_c_val self pp_var) i
161
  | Power (v, n)  -> assert false
162
  | LocalVar v    -> pp_var fmt v
163
  | StateVar v    ->
164
    (* array memory vars are represented by an indirection to a local var with the right type,
165
       in order to avoid casting everywhere. *)
166
    if Types.is_array_type v.var_type
167
    then fprintf fmt "%a" pp_var v
168
    else fprintf fmt "%s->_reg.%a" self pp_var v
169
  | Fun (n, vl)   -> Basic_library.pp_c n (pp_c_val self pp_var) fmt vl
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, dereference memory array variables.
176
*)
177
let pp_c_var_read m fmt id =
178
  if Types.is_address_type id.var_type
179
  then
180
    if is_memory m id
181
    then fprintf fmt "(*%s)" id.var_id
182
    else fprintf fmt "%s" id.var_id
183
  else
184
    if is_output m id
185
    then fprintf fmt "*%s" id.var_id
186
    else fprintf fmt "%s" id.var_id
187

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

    
204
(* Declaration of an input variable:
205
   - if its type is array/matrix/etc, then declare it as a mere pointer,
206
     in order to cope with unknown/parametric array dimensions, 
207
     as it is the case for generics
208
*)
209
let pp_c_decl_input_var fmt id =
210
  if !Options.ansi && Types.is_address_type id.var_type
211
  then pp_c_type (sprintf "(*%s)" id.var_id) fmt (Types.array_base_type id.var_type)
212
  else pp_c_type id.var_id fmt id.var_type
213

    
214
(* Declaration of an output variable:
215
   - if its type is scalar, then pass its address
216
   - if its type is array/matrix/struct/etc, then declare it as a mere pointer,
217
     in order to cope with unknown/parametric array dimensions, 
218
     as it is the case for generics
219
*)
220
let pp_c_decl_output_var fmt id =
221
  if (not !Options.ansi) && Types.is_address_type id.var_type
222
  then pp_c_type                  id.var_id  fmt id.var_type
223
  else pp_c_type (sprintf "(*%s)" id.var_id) fmt (Types.array_base_type id.var_type)
224

    
225
(* Declaration of a local/mem variable:
226
   - if it's an array/matrix/etc, its size(s) should be
227
     known in order to statically allocate memory, 
228
     so we print the full type
229
*)
230
let pp_c_decl_local_var m fmt id =
231
  if id.var_dec_const
232
  then
233
    Format.fprintf fmt "%a = %a"
234
      (pp_c_type id.var_id) id.var_type
235
      (pp_c_val "" (pp_c_var_read m)) (get_const_assign m id)
236
  else
237
    Format.fprintf fmt "%a"
238
      (pp_c_type id.var_id) id.var_type
239

    
240
let pp_c_decl_array_mem self fmt id =
241
  fprintf fmt "%a = (%a) (%s->_reg.%s)"
242
    (pp_c_type (sprintf "(*%s)" id.var_id)) id.var_type
243
    (pp_c_type "(*)") id.var_type
244
    self
245
    id.var_id
246

    
247
(* Declaration of a struct variable:
248
   - if it's an array/matrix/etc, we declare it as a pointer
249
*)
250
let pp_c_decl_struct_var fmt id =
251
  if Types.is_array_type id.var_type
252
  then pp_c_type (sprintf "(*%s)" id.var_id) fmt (Types.array_base_type id.var_type)
253
  else pp_c_type                  id.var_id  fmt id.var_type
254

    
255
let pp_c_decl_instance_var fmt (name, (node, static)) = 
256
  fprintf fmt "%a *%s" pp_machine_memtype_name (node_name node) name
257

    
258
let pp_c_checks self fmt m =
259
  Utils.fprintf_list ~sep:"" 
260
    (fun fmt (loc, check) -> 
261
      fprintf fmt 
262
	"@[<v>%a@,assert (%a);@]@," 
263
	Location.pp_c_loc loc
264
	(pp_c_val self (pp_c_var_read m)) check
265
    ) 
266
    fmt 
267
    m.mstep.step_checks
268

    
269
(********************************************************************************************)
270
(*                       Struct Printing functions                                          *)
271
(********************************************************************************************)
272

    
273
let pp_registers_struct fmt m =
274
  if m.mmemory <> []
275
  then
276
    fprintf fmt "@[%a {@[%a; @]}@] _reg; "
277
      pp_machine_regtype_name m.mname.node_id
278
      (Utils.fprintf_list ~sep:"; " pp_c_decl_struct_var) m.mmemory
279
  else
280
    ()
281

    
282
let print_machine_struct fmt m =
283
  if fst (get_stateless_status m) then
284
    begin
285
    end
286
  else
287
    begin
288
      (* Define struct *)
289
      fprintf fmt "@[%a {@[%a%a%t@]};@]@."
290
	pp_machine_memtype_name m.mname.node_id
291
	pp_registers_struct m
292
	(Utils.fprintf_list ~sep:"; " pp_c_decl_instance_var) m.minstances
293
	(Utils.pp_final_char_if_non_empty "; " m.minstances)
294
    end
295

    
296
let print_machine_struct_from_header fmt inode =
297
  if inode.nodei_stateless then
298
    begin
299
    end
300
  else
301
    begin
302
      (* Declare struct *)
303
      fprintf fmt "@[%a;@]@."
304
	pp_machine_memtype_name inode.nodei_id
305
    end
306

    
307
(********************************************************************************************)
308
(*                      Prototype Printing functions                                        *)
309
(********************************************************************************************)
310

    
311
let print_alloc_prototype fmt (name, static) =
312
  fprintf fmt "%a * %a (%a)"
313
    pp_machine_memtype_name name
314
    pp_machine_alloc_name name
315
    (Utils.fprintf_list ~sep:",@ " pp_c_decl_input_var) static
316

    
317
let print_reset_prototype self fmt (name, static) =
318
  fprintf fmt "void %a (@[<v>%a%t%a *%s@])"
319
    pp_machine_reset_name name
320
    (Utils.fprintf_list ~sep:",@ " pp_c_decl_input_var) static
321
    (Utils.pp_final_char_if_non_empty ",@," static) 
322
    pp_machine_memtype_name name
323
    self
324

    
325
let print_stateless_prototype fmt (name, inputs, outputs) =
326
  fprintf fmt "void %a (@[<v>@[%a%t@]@,@[%a@]@,@])"
327
    pp_machine_step_name name
328
    (Utils.fprintf_list ~sep:",@ " pp_c_decl_input_var) inputs
329
    (Utils.pp_final_char_if_non_empty ",@ " inputs) 
330
    (Utils.fprintf_list ~sep:",@ " pp_c_decl_output_var) outputs
331

    
332
let print_step_prototype self fmt (name, inputs, outputs) =
333
  fprintf fmt "void %a (@[<v>@[%a%t@]@,@[%a@]%t@[%a *%s@]@])"
334
    pp_machine_step_name name
335
    (Utils.fprintf_list ~sep:",@ " pp_c_decl_input_var) inputs
336
    (Utils.pp_final_char_if_non_empty ",@ " inputs) 
337
    (Utils.fprintf_list ~sep:",@ " pp_c_decl_output_var) outputs
338
    (Utils.pp_final_char_if_non_empty ",@," outputs) 
339
    pp_machine_memtype_name name
340
    self
341

    
342
let print_stateless_C_prototype fmt (name, inputs, outputs) =
343
  let output = 
344
    match outputs with
345
    | [hd] -> hd
346
    | _ -> assert false
347
  in
348
  fprintf fmt "%a %s (@[<v>@[%a@]@,@])"
349
    pp_basic_c_type output.var_type
350
    name
351
    (Utils.fprintf_list ~sep:",@ " pp_c_decl_input_var) inputs
352
    
353
    
354

    
355
let print_import_prototype fmt (Dep (_, s, _, _)) =
356
  fprintf fmt "#include \"%s.h\"@," s
357

    
358
let print_import_alloc_prototype fmt (Dep (_, s, _, stateful)) =
359
  if stateful then
360
    fprintf fmt "#include \"%s_alloc.h\"@," s
361

    
362
let print_extern_alloc_prototypes fmt (Dep (_,_, header,_)) =
363
  List.iter (fun decl -> match decl.top_decl_desc with
364
  | ImportedNode ind when not ind.nodei_stateless ->
365
    let static = List.filter (fun v -> v.var_dec_const) ind.nodei_inputs
366
    in fprintf fmt "extern %a;@." print_alloc_prototype (ind.nodei_id, static)
367
  | _                -> ()
368
  ) header
369

    
370
(* Local Variables: *)
371
(* compile-command:"make -C ../../.." *)
372
(* End: *)