Project

General

Profile

Statistics
| Branch: | Tag: | Revision:

lustrec / src / backends / C / c_backend_header.ml @ 1eda3e78

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
open C_backend_common
17

    
18
(********************************************************************************************)
19
(*                         Header Printing functions                                        *)
20
(********************************************************************************************)
21

    
22

    
23
module type MODIFIERS_HDR =
24
sig
25
  val print_machine_decl_prefix: Format.formatter -> Machine_code.machine_t -> unit
26
end
27

    
28
module EmptyMod =
29
struct
30
  let print_machine_decl_prefix = fun fmt x -> ()
31
end
32

    
33
module Main = functor (Mod: MODIFIERS_HDR) -> 
34
struct
35

    
36
let print_import_standard fmt =
37
  fprintf fmt "#include \"%s/include/lustrec/arrow.h\"@.@." Version.prefix
38

    
39
let print_static_declare_instance attr fmt (i, (m, static)) =
40
  fprintf fmt "%a(%s, %a%t%s)"
41
    pp_machine_static_declare_name (node_name m)
42
    attr
43
    (Utils.fprintf_list ~sep:", " Dimension.pp_dimension) static
44
    (Utils.pp_final_char_if_non_empty ", " static)
45
    i
46

    
47
let print_static_declare_macro fmt m =
48
  let array_mem = List.filter (fun v -> Types.is_array_type v.var_type) m.mmemory in
49
  let inst = mk_instance m in
50
  let attr = mk_attribute m in
51
  fprintf fmt "@[<v 2>#define %a(%s, %a%t%s)\\@,%s %a %s;\\@,%a%t%a;@,@]"
52
    pp_machine_static_declare_name m.mname.node_id
53
    attr
54
    (Utils.fprintf_list ~sep:", " (pp_c_var_read m)) m.mstatic
55
    (Utils.pp_final_char_if_non_empty ", " m.mstatic)
56
    inst
57
    attr
58
    pp_machine_memtype_name m.mname.node_id
59
    inst
60
    (Utils.fprintf_list ~sep:";\\@," pp_c_decl_local_var) array_mem
61
    (Utils.pp_final_char_if_non_empty ";\\@," array_mem)
62
    (Utils.fprintf_list ~sep:";\\@,"
63
       (fun fmt (i',m') ->
64
	 let path = sprintf "inst ## _%s" i' in
65
	 fprintf fmt "%a"
66
	   (print_static_declare_instance attr) (path,m')
67
       )) m.minstances
68

    
69
      
70
let print_static_link_instance fmt (i, (m, _)) =
71
 fprintf fmt "%a(%s)" pp_machine_static_link_name (node_name m) i
72

    
73
(* Allocation of a node struct:
74
   - if node memory is an array/matrix/etc, we cast it to a pointer (see pp_registers_struct)
75
*)
76
let print_static_link_macro fmt m =
77
  let array_mem = List.filter (fun v -> Types.is_array_type v.var_type) m.mmemory in
78
  fprintf fmt "@[<v>@[<v 2>#define %a(inst) do {\\@,%a%t%a;\\@]@,} while (0)@.@]"
79
    pp_machine_static_link_name m.mname.node_id
80
    (Utils.fprintf_list ~sep:";\\@,"
81
       (fun fmt v ->
82
	 fprintf fmt "inst._reg.%s = (%a*) &%s"
83
	   v.var_id
84
           (fun fmt v -> pp_c_type "" fmt (Types.array_base_type v.var_type)) v
85
	   v.var_id
86
       )) array_mem
87
    (Utils.pp_final_char_if_non_empty ";\\@," array_mem)
88
    (Utils.fprintf_list ~sep:";\\@,"
89
       (fun fmt (i',m') ->
90
	 let path = sprintf "inst ## _%s" i' in
91
	 fprintf fmt "%a;\\@,inst.%s = &%s"
92
	   print_static_link_instance (path,m')
93
	   i'
94
	   path
95
       )) m.minstances
96
      
97
let print_static_alloc_macro fmt m =
98
  fprintf fmt "@[<v>@[<v 2>#define %a(attr,%a%tinst)\\@,%a(attr,%a%tinst);\\@,%a(inst);@]@,@]@."
99
    pp_machine_static_alloc_name m.mname.node_id
100
    (Utils.fprintf_list ~sep:", " (pp_c_var_read m)) m.mstatic
101
    (Utils.pp_final_char_if_non_empty ", " m.mstatic)
102
    pp_machine_static_declare_name m.mname.node_id
103
    (Utils.fprintf_list ~sep:", " (pp_c_var_read m)) m.mstatic
104
    (Utils.pp_final_char_if_non_empty ", " m.mstatic)
105
    pp_machine_static_link_name m.mname.node_id
106

    
107
 
108
let print_machine_decl fmt m =
109
  Mod.print_machine_decl_prefix fmt m;
110
  if fst (get_stateless_status m) then
111
    begin
112
      fprintf fmt "extern %a;@.@."
113
	print_stateless_prototype
114
	(m.mname.node_id, m.mstep.step_inputs, m.mstep.step_outputs)
115
    end
116
  else
117
    begin
118
      (* Static allocation *)
119
      if !Options.static_mem
120
      then (
121
	fprintf fmt "%a@.%a@.%a@."
122
	  print_static_declare_macro m
123
	  print_static_link_macro m
124
	  print_static_alloc_macro m
125
      )
126
      else ( 
127
        (* Dynamic allocation *)
128
	fprintf fmt "extern %a;@.@."
129
	  print_alloc_prototype (m.mname.node_id, m.mstatic)
130
      );
131
      let self = mk_self m in
132
      fprintf fmt "extern %a;@.@."
133
	(print_reset_prototype self) (m.mname.node_id, m.mstatic);
134

    
135
      fprintf fmt "extern %a;@.@."
136
	(print_step_prototype self)
137
	(m.mname.node_id, m.mstep.step_inputs, m.mstep.step_outputs)
138
    end
139

    
140
let print_machine_alloc_decl fmt m =
141
  Mod.print_machine_decl_prefix fmt m;
142
  if fst (get_stateless_status m) then
143
    begin
144
    end
145
  else
146
    begin
147
      if !Options.static_mem
148
      then
149
	begin
150
	  (* Static allocation *)
151
	  fprintf fmt "%a@.%a@.%a@."
152
		  print_static_declare_macro m
153
		  print_static_link_macro m
154
		  print_static_alloc_macro m
155
	end
156
      else
157
	begin 
158
          (* Dynamic allocation *)
159
	  fprintf fmt "extern %a;@."
160
		  print_alloc_prototype (m.mname.node_id, m.mstatic)
161
	end
162
    end
163

    
164
let print_machine_decl_from_header fmt inode =
165
  (*Mod.print_machine_decl_prefix fmt m;*)
166
  if inode.nodei_stateless then
167
    begin
168
      fprintf fmt "extern %a;@.@."
169
	print_stateless_prototype
170
	(inode.nodei_id, inode.nodei_inputs, inode.nodei_outputs)
171
    end
172
  else
173
    begin
174
      let static_inputs = List.filter (fun v -> v.var_dec_const) inode.nodei_inputs in
175
      let used name =
176
	   (List.exists (fun v -> v.var_id = name) inode.nodei_inputs)
177
	|| (List.exists (fun v -> v.var_id = name) inode.nodei_outputs) in
178
      let self = mk_new_name used "self" in
179
      fprintf fmt "extern %a;@.@."
180
	(print_reset_prototype self) (inode.nodei_id, static_inputs);
181

    
182
      fprintf fmt "extern %a;@.@."
183
	(print_step_prototype self)
184
	(inode.nodei_id, inode.nodei_inputs, inode.nodei_outputs)
185
    end
186

    
187
let print_const_decl fmt cdecl =
188
  fprintf fmt "extern %a;@." 
189
    (pp_c_type cdecl.const_id) cdecl.const_type
190

    
191
let rec pp_c_struct_type_field filename cpt fmt (label, tdesc) =
192
  fprintf fmt "%a;" (pp_c_type_decl filename cpt label) tdesc
193
and pp_c_type_decl filename cpt var fmt tdecl =
194
  match tdecl with
195
  | Tydec_any           -> assert false
196
  | Tydec_int           -> fprintf fmt "int %s" var
197
  | Tydec_real          -> fprintf fmt "double %s" var
198
  | Tydec_float         -> fprintf fmt "float %s" var
199
  | Tydec_bool          -> fprintf fmt "_Bool %s" var
200
  | Tydec_clock ty      -> pp_c_type_decl filename cpt var fmt ty
201
  | Tydec_const c       -> fprintf fmt "%s %s" c var
202
  | Tydec_array (d, ty) -> fprintf fmt "%a[%a]" (pp_c_type_decl filename cpt var) ty pp_c_dimension d
203
  | Tydec_enum tl ->
204
    begin
205
      incr cpt;
206
      fprintf fmt "enum _enum_%s_%d { %a } %s" filename !cpt (Utils.fprintf_list ~sep:", " pp_print_string) tl var
207
    end
208
  | Tydec_struct fl ->
209
    begin
210
      incr cpt;
211
      fprintf fmt "struct _struct_%s_%d { %a } %s" filename !cpt (Utils.fprintf_list ~sep:" " (pp_c_struct_type_field filename cpt)) fl var
212
    end
213

    
214
let print_type_definitions fmt filename =
215
  let cpt_type = ref 0 in
216
  Hashtbl.iter (fun typ decl ->
217
		match typ with
218
		| Tydec_const var ->
219
		   (match decl.top_decl_desc with
220
		    | TypeDef tdef ->
221
		       fprintf fmt "typedef %a;@.@."
222
			       (pp_c_type_decl filename cpt_type var) tdef.tydef_desc
223
		    | _ -> assert false)
224
		| _        -> ()) type_table
225

    
226
let reset_type_definitions, print_type_definition_from_header =
227
  let cpt_type =ref 0 in
228
  ((fun () -> cpt_type := 0),
229
   (fun fmt typ filename ->
230
    fprintf fmt "typedef %a;@.@."
231
	(pp_c_type_decl filename cpt_type typ.tydef_id) typ.tydef_desc))
232

    
233
(********************************************************************************************)
234
(*                         MAIN Header Printing functions                                   *)
235
(********************************************************************************************)
236
let print_header header_fmt basename prog machines dependencies =
237
  (* Include once: start *)
238
  let baseNAME = String.uppercase basename in
239
  let baseNAME = Str.global_replace (Str.regexp "\\.\\|\\ ") "_" baseNAME in
240
  begin
241
    (* Print the svn version number and the supported C standard (C90 or C99) *)
242
    print_version header_fmt;
243
    fprintf header_fmt "#ifndef _%s@.#define _%s@." baseNAME baseNAME;
244
    pp_print_newline header_fmt ();
245
    fprintf header_fmt "/* Imports standard library */@.";
246
    (* imports standard library definitions (arrow) *)
247
    print_import_standard header_fmt;
248
    pp_print_newline header_fmt ();
249
    (* imports dependencies *)
250
    fprintf header_fmt "/* Import Dependencies */@.";
251
    fprintf header_fmt "@[<v>";
252
    List.iter (print_import_prototype header_fmt) dependencies;
253
    fprintf header_fmt "@]@.";
254
    fprintf header_fmt "/* Types definitions */@.";
255
    (* Print the type definitions from the type table *)
256
    print_type_definitions header_fmt basename;
257
    pp_print_newline header_fmt ();
258
    (* Print the global constant declarations. *)
259
    fprintf header_fmt "/* Global constant (declarations, definitions are in C file) */@.";
260
    List.iter (fun c -> print_const_decl header_fmt (const_of_top c)) (get_consts prog);
261
    pp_print_newline header_fmt ();
262
    (* Print the struct declarations of all machines. *)
263
    fprintf header_fmt "/* Struct declarations */@.";
264
    List.iter (print_machine_struct header_fmt) machines;
265
    pp_print_newline header_fmt ();
266
    (* Print the prototypes of all machines *)
267
    fprintf header_fmt "/* Nodes declarations */@.";
268
    List.iter (print_machine_decl header_fmt) machines;
269
    pp_print_newline header_fmt ();
270
    (* Include once: end *)
271
    fprintf header_fmt "#endif@.";
272
    pp_print_newline header_fmt ()
273
  end
274

    
275
let print_alloc_header header_fmt basename prog machines dependencies =
276
  (* Include once: start *)
277
  let baseNAME = String.uppercase basename in
278
  let baseNAME = Str.global_replace (Str.regexp "\\.\\|\\ ") "_" baseNAME in
279
  begin
280
    (* Print the svn version number and the supported C standard (C90 or C99) *)
281
    print_version header_fmt;
282
    fprintf header_fmt "#ifndef _%s_alloc@.#define _%s_alloc@." baseNAME baseNAME;
283
    pp_print_newline header_fmt ();
284
    (* Import the header *)
285
    fprintf header_fmt "/* Import header from %s */@." basename;
286
    fprintf header_fmt "@[<v>";
287
    print_import_prototype header_fmt (true, basename, []);
288
    fprintf header_fmt "@]@.";
289
    fprintf header_fmt "/* Import dependencies */@.";
290
    fprintf header_fmt "@[<v>";
291
    List.iter (print_import_alloc_prototype header_fmt) dependencies;
292
    fprintf header_fmt "@]@.";
293
    (* Print the struct definitions of all machines. *)
294
    fprintf header_fmt "/* Struct definitions */@.";
295
    List.iter (print_machine_struct header_fmt) machines;
296
    pp_print_newline header_fmt ();
297
    (* Print the prototypes of all machines *)
298
    fprintf header_fmt "/* Node allocation function/macro prototypes */@.";
299
    List.iter (print_machine_alloc_decl header_fmt) machines;
300
    pp_print_newline header_fmt ();
301
    (* Include once: end *)
302
    fprintf header_fmt "#endif@.";
303
    pp_print_newline header_fmt ()
304
  end
305

    
306
let print_header_from_header header_fmt basename header =
307
  (* Include once: start *)
308
  let baseNAME = String.uppercase basename in
309
  let baseNAME = Str.global_replace (Str.regexp "\\.\\|\\ ") "_" baseNAME in
310
  let types = get_typedefs header in
311
  let consts = get_consts header in
312
  let nodes = get_imported_nodes header in
313
  let dependencies = get_dependencies header in
314
  begin
315
    (* Print the svn version number and the supported C standard (C90 or C99) *)
316
    print_version header_fmt;
317
    fprintf header_fmt "#ifndef _%s@.#define _%s@." baseNAME baseNAME;
318
    pp_print_newline header_fmt ();
319
    fprintf header_fmt "/* Imports standard library */@.";
320
    (* imports standard library definitions (arrow) *)
321
    print_import_standard header_fmt;
322
    pp_print_newline header_fmt ();
323
    (* imports dependencies *)
324
    fprintf header_fmt "/* Import dependencies */@.";
325
    fprintf header_fmt "@[<v>";
326
    List.iter
327
      (fun dep -> let (local, s) = dependency_of_top dep in print_import_prototype header_fmt (local, s, []))
328
      dependencies;
329
    fprintf header_fmt "@]@.";
330
    fprintf header_fmt "/* Types definitions */@.";
331
    (* Print the type definitions from the type table *)
332
    reset_type_definitions ();
333
    List.iter (fun typ -> print_type_definition_from_header header_fmt (typedef_of_top typ) basename) types;
334
    pp_print_newline header_fmt ();
335
    (* Print the global constant declarations. *)
336
    fprintf header_fmt "/* Global constant (declarations, definitions are in C file) */@.";
337
    List.iter (fun c -> print_const_decl header_fmt (const_of_top c)) consts;
338
    pp_print_newline header_fmt ();
339
    (* Print the struct declarations of all machines. *)
340
    fprintf header_fmt "/* Struct declarations */@.";
341
    List.iter (fun node -> print_machine_struct_from_header header_fmt (imported_node_of_top node)) nodes;
342
    pp_print_newline header_fmt ();
343
    (* Print the prototypes of all machines *)
344
    fprintf header_fmt "/* Nodes declarations */@.";
345
    List.iter (fun node -> print_machine_decl_from_header header_fmt (imported_node_of_top node)) nodes;
346
    pp_print_newline header_fmt ();
347
    (* Include once: end *)
348
    fprintf header_fmt "#endif@.";
349
    pp_print_newline header_fmt ()
350
  end
351

    
352
end
353
(* Local Variables: *)
354
(* compile-command:"make -C ../../.." *)
355
(* End: *)