Project

General

Profile

Statistics
| Branch: | Tag: | Revision:

lustrec / src / backends / C / c_backend_common.ml @ b38ffff3

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

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

    
30
(* Generation of a non-clashing name for the instance variable of static allocation macro *)
31
let mk_instance m =
32
  mk_new_name (m.mstep.step_inputs@m.mmemory) "inst"
33

    
34
(* Generation of a non-clashing name for the attribute variable of static allocation macro *)
35
let mk_attribute m =
36
  mk_new_name (m.mstep.step_inputs@m.mmemory) "attr"
37

    
38
let mk_call_var_decl loc id =
39
  { var_id = id;
40
    var_dec_type = mktyp Location.dummy_loc Tydec_any;
41
    var_dec_clock = mkclock Location.dummy_loc Ckdec_any;
42
    var_dec_const = false;
43
    var_type = Type_predef.type_arrow (Types.new_var ()) (Types.new_var ());
44
    var_clock = Clocks.new_var true;
45
    var_loc = loc }
46

    
47
(* counter for loop variable creation *)
48
let loop_cpt = ref (-1)
49

    
50
let reset_loop_counter () =
51
 loop_cpt := -1
52

    
53
let mk_loop_var m () =
54
  let vars = m.mstep.step_inputs@m.mstep.step_outputs@m.mstep.step_locals@m.mmemory in
55
  let rec aux () =
56
    incr loop_cpt;
57
    let s = Printf.sprintf "__%s_%d" "i" !loop_cpt in
58
    if List.exists (fun v -> v.var_id = s) vars then aux () else s
59
  in aux ()
60
(*
61
let addr_cpt = ref (-1)
62

    
63
let reset_addr_counter () =
64
 addr_cpt := -1
65

    
66
let mk_addr_var m var =
67
  let vars = m.mmemory in
68
  let rec aux () =
69
    incr addr_cpt;
70
    let s = Printf.sprintf "%s_%s_%d" var "addr" !addr_cpt in
71
    if List.exists (fun v -> v.var_id = s) vars then aux () else s
72
  in aux ()
73
*)
74
let pp_machine_memtype_name fmt id = fprintf fmt "struct %s_mem" id
75
let pp_machine_regtype_name fmt id = fprintf fmt "struct %s_reg" id
76
let pp_machine_alloc_name fmt id = fprintf fmt "%s_alloc" id
77
let pp_machine_static_declare_name fmt id = fprintf fmt "%s_DECLARE" id
78
let pp_machine_static_link_name fmt id = fprintf fmt "%s_LINK" id
79
let pp_machine_static_alloc_name fmt id = fprintf fmt "%s_ALLOC" id
80
let pp_machine_reset_name fmt id = fprintf fmt "%s_reset" id
81
let pp_machine_step_name fmt id = fprintf fmt "%s_step" id
82

    
83
let pp_c_dimension fmt d =
84
 fprintf fmt "%a" Dimension.pp_dimension d
85

    
86
let pp_c_type var fmt t =
87
  let rec aux t pp_suffix =
88
  match (Types.repr t).Types.tdesc with
89
  | Types.Tclock t'       -> aux t' pp_suffix
90
  | Types.Tbool           -> fprintf fmt "_Bool %s%a" var pp_suffix ()
91
  | Types.Treal           -> fprintf fmt "double %s%a" var pp_suffix ()
92
  | Types.Tint            -> fprintf fmt "int %s%a" var pp_suffix ()
93
  | Types.Tarray (d, t')  ->
94
    let pp_suffix' fmt () = fprintf fmt "%a[%a]" pp_suffix () pp_c_dimension d in
95
    aux t' pp_suffix'
96
  | Types.Tstatic (_, t') -> fprintf fmt "const "; aux t' pp_suffix
97
  | Types.Tconst ty       -> fprintf fmt "%s %s" ty var
98
  | Types.Tarrow (_, _)   -> fprintf fmt "void (*%s)()" var
99
  | _                     -> eprintf "internal error: pp_c_type %a@." Types.print_ty t; assert false
100
  in aux t (fun fmt () -> ())
101

    
102
let rec pp_c_initialize fmt t = 
103
  match (Types.repr t).Types.tdesc with
104
  | Types.Tint -> pp_print_string fmt "0"
105
  | Types.Tclock t' -> pp_c_initialize fmt t'
106
  | Types.Tbool -> pp_print_string fmt "0" 
107
  | Types.Treal -> pp_print_string fmt "0."
108
  | Types.Tarray (d, t') when Dimension.is_dimension_const d ->
109
    fprintf fmt "{%a}"
110
      (Utils.fprintf_list ~sep:"," (fun fmt _ -> pp_c_initialize fmt t'))
111
      (Utils.duplicate 0 (Dimension.size_const_dimension d))
112
  | _ -> assert false
113

    
114
(* Declaration of an input variable:
115
   - if its type is array/matrix/etc, then declare it as a mere pointer,
116
     in order to cope with unknown/parametric array dimensions, 
117
     as it is the case for generics
118
*)
119
let pp_c_decl_input_var fmt id =
120
  if !Options.ansi && Types.is_address_type id.var_type
121
  then pp_c_type (sprintf "(*%s)" id.var_id) fmt (Types.array_base_type id.var_type)
122
  else pp_c_type id.var_id fmt id.var_type
123

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

    
135
(* Declaration of a local/mem variable:
136
   - if it's an array/matrix/etc, its size(s) should be
137
     known in order to statically allocate memory, 
138
     so we print the full type
139
*)
140
let pp_c_decl_local_var fmt id =
141
  pp_c_type id.var_id fmt id.var_type
142

    
143
let pp_c_decl_array_mem self fmt id =
144
  fprintf fmt "%a = (%a) (%s->_reg.%s)"
145
    (pp_c_type (sprintf "(*%s)" id.var_id)) id.var_type
146
    (pp_c_type "(*)") id.var_type
147
    self
148
    id.var_id
149

    
150
(* Declaration of a struct variable:
151
   - if it's an array/matrix/etc, we declare it as a pointer
152
*)
153
let pp_c_decl_struct_var fmt id =
154
  if Types.is_array_type id.var_type
155
  then pp_c_type (sprintf "(*%s)" id.var_id) fmt (Types.array_base_type id.var_type)
156
  else pp_c_type                  id.var_id  fmt id.var_type
157

    
158
(* Access to the value of a variable:
159
   - if it's not a scalar output, then its name is enough
160
   - otherwise, dereference it (it has been declared as a pointer,
161
     despite its scalar Lustre type)
162
   - moreover, dereference memory array variables.
163
*)
164
let pp_c_var_read m fmt id =
165
  if Types.is_address_type id.var_type
166
  then
167
    if is_memory m id
168
    then fprintf fmt "(*%s)" id.var_id
169
    else fprintf fmt "%s" id.var_id
170
  else
171
    if is_output m id
172
    then fprintf fmt "*%s" id.var_id
173
    else fprintf fmt "%s" id.var_id
174

    
175
(* Addressable value of a variable, the one that is passed around in calls:
176
   - if it's not a scalar non-output, then its name is enough
177
   - otherwise, reference it (it must be passed as a pointer,
178
     despite its scalar Lustre type)
179
*)
180
let pp_c_var_write m fmt id =
181
  if Types.is_address_type id.var_type
182
  then
183
    fprintf fmt "%s" id.var_id
184
  else
185
    if is_output m id
186
    then
187
      fprintf fmt "%s" id.var_id
188
    else
189
      fprintf fmt "&%s" id.var_id
190

    
191
let pp_c_decl_instance_var fmt (name, (node, static)) = 
192
  fprintf fmt "%a *%s" pp_machine_memtype_name (node_name node) name
193

    
194
let pp_c_tag fmt t =
195
 pp_print_string fmt (if t = tag_true then "1" else if t = tag_false then "0" else t)
196

    
197
(* Prints a constant value *)
198
let rec pp_c_const fmt c =
199
  match c with
200
    | Const_int i     -> pp_print_int fmt i
201
    | Const_real r    -> pp_print_string fmt r
202
    | Const_float r   -> pp_print_float fmt r
203
    | Const_tag t     -> pp_c_tag fmt t
204
    | Const_array ca  -> fprintf fmt "{%a }" (Utils.fprintf_list ~sep:", " pp_c_const) ca
205
    | Const_struct fl -> fprintf fmt "{%a }" (Utils.fprintf_list ~sep:", " (fun fmt (f, c) -> pp_c_const fmt c)) fl
206
    | Const_string _ -> assert false (* string occurs in annotations not in C *)
207

    
208
(* Prints a value expression [v], with internal function calls only.
209
   [pp_var] is a printer for variables (typically [pp_c_var_read]),
210
   but an offset suffix may be added for array variables
211
*)
212
let rec pp_c_val self pp_var fmt v =
213
  match v with
214
    | Cst c         -> pp_c_const fmt c
215
    | Array vl      -> fprintf fmt "{%a}" (Utils.fprintf_list ~sep:", " (pp_c_val self pp_var)) vl
216
    | Access (t, i) -> fprintf fmt "%a[%a]" (pp_c_val self pp_var) t (pp_c_val self pp_var) i
217
    | Power (v, n)  -> assert false
218
    | LocalVar v    -> pp_var fmt v
219
    | StateVar v    ->
220
    (* array memory vars are represented by an indirection to a local var with the right type,
221
       in order to avoid casting everywhere. *)
222
      if Types.is_array_type v.var_type
223
      then fprintf fmt "%a" pp_var v
224
      else fprintf fmt "%s->_reg.%a" self pp_var v
225
    | Fun (n, vl)   -> Basic_library.pp_c n (pp_c_val self pp_var) fmt vl
226

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

    
238

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

    
243
let print_alloc_prototype fmt (name, static) =
244
  fprintf fmt "%a * %a (%a)"
245
    pp_machine_memtype_name name
246
    pp_machine_alloc_name name
247
    (Utils.fprintf_list ~sep:",@ " pp_c_decl_input_var) static
248

    
249
let print_reset_prototype self fmt (name, static) =
250
  fprintf fmt "void %a (@[<v>%a%t%a *%s@])"
251
    pp_machine_reset_name name
252
    (Utils.fprintf_list ~sep:",@ " pp_c_decl_input_var) static
253
    (Utils.pp_final_char_if_non_empty ",@," static) 
254
    pp_machine_memtype_name name
255
    self
256

    
257
let print_stateless_prototype fmt (name, inputs, outputs) =
258
  fprintf fmt "void %a (@[<v>@[%a%t@]@,@[%a@]@,@])"
259
    pp_machine_step_name name
260
    (Utils.fprintf_list ~sep:",@ " pp_c_decl_input_var) inputs
261
    (Utils.pp_final_char_if_non_empty ",@ " inputs) 
262
    (Utils.fprintf_list ~sep:",@ " pp_c_decl_output_var) outputs
263

    
264
let print_step_prototype self fmt (name, inputs, outputs) =
265
  fprintf fmt "void %a (@[<v>@[%a%t@]@,@[%a@]%t@[%a *%s@]@])"
266
    pp_machine_step_name name
267
    (Utils.fprintf_list ~sep:",@ " pp_c_decl_input_var) inputs
268
    (Utils.pp_final_char_if_non_empty ",@ " inputs) 
269
    (Utils.fprintf_list ~sep:",@ " pp_c_decl_output_var) outputs
270
    (Utils.pp_final_char_if_non_empty ",@," outputs) 
271
    pp_machine_memtype_name name
272
    self
273

    
274
let print_import_prototype fmt (s, _, _) =
275
  fprintf fmt "#include \"%s.h\"@," s
276

    
277
(* Local Variables: *)
278
(* compile-command:"make -C ../../.." *)
279
(* End: *)