Project

General

Profile

Download (10.2 KB) Statistics
| Branch: | Tag: | Revision:
1
open Format
2
open LustreSpec
3
open Corelang
4
open Machine_code
5

    
6
let c1 f g x = f x;g x
7
let c2 f g x y = f x y;g x y
8
let c3 f g x y z = f x y z;g x y z
9
let c4 f g w x y z = f w x y z;g w x y z
10

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

    
18

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

    
23
(* Generation of a non-clashing name for the instance variable of static allocation macro *)
24
let mk_instance m =
25
  mk_new_name (m.mstep.step_inputs@m.mmemory) "inst"
26

    
27
(* Generation of a non-clashing name for the attribute variable of static allocation macro *)
28
let mk_attribute m =
29
  mk_new_name (m.mstep.step_inputs@m.mmemory) "attr"
30

    
31
let mk_call_var_decl loc id =
32
  { var_id = id;
33
    var_dec_type = mktyp Location.dummy_loc Tydec_any;
34
    var_dec_clock = mkclock Location.dummy_loc Ckdec_any;
35
    var_dec_const = false;
36
    var_type = Type_predef.type_arrow (Types.new_var ()) (Types.new_var ());
37
    var_clock = Clocks.new_var true;
38
    var_loc = loc }
39

    
40
(* counter for loop variable creation *)
41
let loop_cpt = ref (-1)
42

    
43
let reset_loop_counter () =
44
 loop_cpt := -1
45

    
46
let mk_loop_var m () =
47
  let vars = m.mstep.step_inputs@m.mstep.step_outputs@m.mstep.step_locals@m.mmemory in
48
  let rec aux () =
49
    incr loop_cpt;
50
    let s = Printf.sprintf "__%s_%d" "i" !loop_cpt in
51
    if List.exists (fun v -> v.var_id = s) vars then aux () else s
52
  in aux ()
53
(*
54
let addr_cpt = ref (-1)
55

    
56
let reset_addr_counter () =
57
 addr_cpt := -1
58

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

    
76
let pp_c_dimension fmt d =
77
 fprintf fmt "%a" Dimension.pp_dimension d
78

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

    
95
let rec pp_c_initialize fmt t = 
96
  match (Types.repr t).Types.tdesc with
97
  | Types.Tint -> pp_print_string fmt "0"
98
  | Types.Tclock t' -> pp_c_initialize fmt t'
99
  | Types.Tbool -> pp_print_string fmt "0" 
100
  | Types.Treal -> pp_print_string fmt "0."
101
  | Types.Tarray (d, t') when Dimension.is_dimension_const d ->
102
    fprintf fmt "{%a}"
103
      (Utils.fprintf_list ~sep:"," (fun fmt _ -> pp_c_initialize fmt t'))
104
      (Utils.duplicate 0 (Dimension.size_const_dimension d))
105
  | _ -> assert false
106

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

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

    
128
(* Declaration of a local/mem variable:
129
   - if it's an array/matrix/etc, its size(s) should be
130
     known in order to statically allocate memory, 
131
     so we print the full type
132
*)
133
let pp_c_decl_local_var fmt id =
134
  pp_c_type id.var_id fmt id.var_type
135

    
136
let pp_c_decl_array_mem self fmt id =
137
  fprintf fmt "%a = (%a) (%s->_reg.%s)"
138
    (pp_c_type (sprintf "(*%s)" id.var_id)) id.var_type
139
    (pp_c_type "(*)") id.var_type
140
    self
141
    id.var_id
142

    
143
(* Declaration of a struct variable:
144
   - if it's an array/matrix/etc, we declare it as a pointer
145
*)
146
let pp_c_decl_struct_var fmt id =
147
  if Types.is_array_type id.var_type
148
  then fprintf fmt "%a;" (pp_c_type (sprintf "(*%s)" id.var_id)) (Types.array_base_type id.var_type)
149
  else fprintf fmt "%a;" (pp_c_type                  id.var_id)  id.var_type
150

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

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

    
184
let pp_c_decl_instance_var fmt (name, (node, static)) = 
185
  fprintf fmt "%a %s%s" pp_machine_memtype_name (node_name node) (if !Options.no_pointer then "" else "*") name
186

    
187
let pp_c_tag fmt t =
188
 pp_print_string fmt (if t = tag_true then "1" else if t = tag_false then "0" else t)
189

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

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

    
220
let pp_c_checks self fmt m =
221
  Utils.fprintf_list ~sep:"" 
222
    (fun fmt (loc, check) -> 
223
      fprintf fmt 
224
	"@[<v>%a@,assert (%a);@]@," 
225
	Location.pp_c_loc loc
226
	(pp_c_val self (pp_c_var_read m)) check
227
    ) 
228
    fmt 
229
    m.mstep.step_checks
230

    
231

    
232
(********************************************************************************************)
233
(*                      Prototype Printing functions                                        *)
234
(********************************************************************************************)
235

    
236
let print_alloc_prototype fmt (name, static) =
237
  fprintf fmt "%a * %a (%s%s%a)"
238
    pp_machine_memtype_name name
239
    pp_machine_alloc_name name
240
    (if !Options.no_pointer then "int allocStruct" else "")
241
    (if !Options.no_pointer && List.length static != 0 then ", " else "")
242
    (Utils.fprintf_list ~sep:",@ " pp_c_decl_input_var) static
243

    
244
let print_reset_prototype self fmt (name, static) =
245
  fprintf fmt "void %a (@[<v>%a%t%a *%s@])"
246
    pp_machine_reset_name name
247
    (Utils.fprintf_list ~sep:",@ " pp_c_decl_input_var) static
248
    (Utils.pp_final_char_if_non_empty ",@," static) 
249
    pp_machine_memtype_name name
250
    self
251

    
252
let print_stateless_prototype fmt (name, inputs, outputs) =
253
  fprintf fmt "void %a (@[<v>@[%a%t@]@,@[%a@]@,@])"
254
    pp_machine_step_name name
255
    (Utils.fprintf_list ~sep:",@ " pp_c_decl_input_var) inputs
256
    (Utils.pp_final_char_if_non_empty ",@ " inputs) 
257
    (Utils.fprintf_list ~sep:",@ " pp_c_decl_output_var) outputs
258

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

    
269
let print_import_prototype fmt (s, _, _) =
270
  fprintf fmt "#include \"%s.h\"@," s
271

    
272
(* Local Variables: *)
273
(* compile-command:"make -C ../../.." *)
274
(* End: *)
(2-2/9)