Project

General

Profile

Statistics
| Branch: | Tag: | Revision:

lustrec / src / backends / C / c_backend_header.ml @ 14c56a07

History | View | Annotate | Download (15.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
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/arrow.h\"@.@." Version.include_path
38

    
39
let rec print_static_val pp_var fmt v =
40
  match v with
41
  | Cst c         -> pp_c_const fmt c
42
  | LocalVar v    -> pp_var fmt v
43
  | Fun (n, vl)   -> Basic_library.pp_c n (print_static_val pp_var) fmt vl
44
  | _             -> (Format.eprintf "Internal error: C_backend_header.print_static_val"; assert false)
45

    
46
let print_constant_decl (m, attr, inst) pp_var fmt v =
47
  Format.fprintf fmt "%s %a = %a"
48
    attr
49
    (pp_c_type (Format.sprintf "%s ## %s" inst v.var_id)) v.var_type
50
    (print_static_val pp_var) (Machine_code.get_const_assign m v)
51

    
52
let print_static_constant_decl (m, attr, inst) fmt const_locals =
53
  let pp_var fmt v =
54
    if List.mem v const_locals
55
    then
56
      Format.fprintf fmt "%s ## %s" inst v.var_id
57
    else 
58
      Format.fprintf fmt "%s" v.var_id in
59
  Format.fprintf fmt "%a%t"
60
    (Utils.fprintf_list ~sep:";\\@," (print_constant_decl (m, attr, inst) pp_var)) const_locals
61
    (Utils.pp_final_char_if_non_empty ";\\@," const_locals)
62

    
63
let print_static_declare_instance (m, attr, inst) const_locals fmt (i, (n, static)) =
64
  let pp_var fmt v =
65
    if List.mem v const_locals
66
    then
67
      Format.fprintf fmt "%s ## %s" inst v.var_id
68
    else 
69
      Format.fprintf fmt "%s" v.var_id in
70
  let values = List.map (Machine_code.value_of_dimension m) static in
71
  fprintf fmt "%a(%s, %a%t%s)"
72
    pp_machine_static_declare_name (node_name n)
73
    attr
74
    (Utils.fprintf_list ~sep:", " (print_static_val pp_var)) values
75
    (Utils.pp_final_char_if_non_empty ", " static)
76
    i
77

    
78
let print_static_declare_macro fmt (m, attr, inst) =
79
  let const_locals = List.filter (fun vdecl -> vdecl.var_dec_const) m.mstep.step_locals in
80
  let array_mem = List.filter (fun v -> Types.is_array_type v.var_type) m.mmemory in
81
  fprintf fmt "@[<v 2>#define %a(%s, %a%t%s)\\@,%a%s %a %s;\\@,%a%t%a;@,@]"
82
    pp_machine_static_declare_name m.mname.node_id
83
    attr
84
    (Utils.fprintf_list ~sep:", " (pp_c_var_read m)) m.mstatic
85
    (Utils.pp_final_char_if_non_empty ", " m.mstatic)
86
    inst
87
    (* constants *)
88
    (print_static_constant_decl (m, attr, inst)) const_locals
89
    attr
90
    pp_machine_memtype_name m.mname.node_id
91
    inst
92
    (Utils.fprintf_list ~sep:";\\@," (pp_c_decl_local_var m)) array_mem
93
    (Utils.pp_final_char_if_non_empty ";\\@," array_mem)
94
    (Utils.fprintf_list ~sep:";\\@,"
95
       (fun fmt (i',m') ->
96
	 let path = sprintf "%s ## _%s" inst i' in
97
	 fprintf fmt "%a"
98
	   (print_static_declare_instance (m, attr, inst) const_locals) (path, m')
99
       )) m.minstances
100

    
101
      
102
let print_static_link_instance fmt (i, (m, _)) =
103
 fprintf fmt "%a(%s)" pp_machine_static_link_name (node_name m) i
104

    
105
(* Allocation of a node struct:
106
   - if node memory is an array/matrix/etc, we cast it to a pointer (see pp_registers_struct)
107
*)
108
let print_static_link_macro fmt (m, attr, inst) =
109
  let array_mem = List.filter (fun v -> Types.is_array_type v.var_type) m.mmemory in
110
  fprintf fmt "@[<v>@[<v 2>#define %a(%s) do {\\@,%a%t%a;\\@]@,} while (0)@.@]"
111
    pp_machine_static_link_name m.mname.node_id
112
    inst
113
    (Utils.fprintf_list ~sep:";\\@,"
114
       (fun fmt v ->
115
	 fprintf fmt "%s._reg.%s = (%a*) &%s"
116
	   inst
117
	   v.var_id
118
           (fun fmt v -> pp_c_type "" fmt (Types.array_base_type v.var_type)) v
119
	   v.var_id
120
       )) array_mem
121
    (Utils.pp_final_char_if_non_empty ";\\@," array_mem)
122
    (Utils.fprintf_list ~sep:";\\@,"
123
       (fun fmt (i',m') ->
124
	 let path = sprintf "%s ## _%s" inst i' in
125
	 fprintf fmt "%a;\\@,%s.%s = &%s"
126
	   print_static_link_instance (path,m')
127
	   inst
128
	   i'
129
	   path
130
       )) m.minstances
131

    
132
let print_static_alloc_macro fmt (m, attr, inst) =
133
  fprintf fmt "@[<v>@[<v 2>#define %a(%s, %a%t%s)\\@,%a(%s, %a%t%s);\\@,%a(%s);@]@,@]@."
134
    pp_machine_static_alloc_name m.mname.node_id
135
    attr
136
    (Utils.fprintf_list ~sep:", " (pp_c_var_read m)) m.mstatic
137
    (Utils.pp_final_char_if_non_empty ", " m.mstatic)
138
    inst
139
    pp_machine_static_declare_name m.mname.node_id
140
    attr
141
    (Utils.fprintf_list ~sep:", " (pp_c_var_read m)) m.mstatic
142
    (Utils.pp_final_char_if_non_empty ", " m.mstatic)
143
    inst
144
    pp_machine_static_link_name m.mname.node_id
145
    inst
146

    
147
let print_machine_decl fmt m =
148
  Mod.print_machine_decl_prefix fmt m;
149
  if fst (get_stateless_status m) then
150
    begin
151
      fprintf fmt "extern %a;@.@."
152
	print_stateless_prototype
153
	(m.mname.node_id, m.mstep.step_inputs, m.mstep.step_outputs)
154
    end
155
  else
156
    begin
157
      (* Static allocation *)
158
      if !Options.static_mem
159
      then
160
	begin
161
	  let inst = mk_instance m in
162
	  let attr = mk_attribute m in
163
	  fprintf fmt "%a@.%a@.%a@."
164
	    print_static_declare_macro (m, attr, inst)
165
	    print_static_link_macro (m, attr, inst)
166
	    print_static_alloc_macro (m, attr, inst)
167
	end
168
      else
169
	begin 
170
        (* Dynamic allocation *)
171
	  fprintf fmt "extern %a;@.@."
172
	    print_alloc_prototype (m.mname.node_id, m.mstatic)
173
	end;
174
      let self = mk_self m in
175
      fprintf fmt "extern %a;@.@."
176
	(print_reset_prototype self) (m.mname.node_id, m.mstatic);
177

    
178
      fprintf fmt "extern %a;@.@."
179
	(print_step_prototype self)
180
	(m.mname.node_id, m.mstep.step_inputs, m.mstep.step_outputs)
181
    end
182

    
183
let print_machine_alloc_decl fmt m =
184
  Mod.print_machine_decl_prefix fmt m;
185
  if fst (get_stateless_status m) then
186
    begin
187
    end
188
  else
189
    begin
190
      if !Options.static_mem
191
      then
192
	begin
193
	  (* Static allocation *)
194
	  let inst = mk_instance m in
195
	  let attr = mk_attribute m in
196
	  fprintf fmt "%a@.%a@.%a@."
197
		  print_static_declare_macro (m, attr, inst)
198
		  print_static_link_macro (m, attr, inst)
199
		  print_static_alloc_macro (m, attr, inst)
200
	end
201
      else
202
	begin 
203
          (* Dynamic allocation *)
204
	  fprintf fmt "extern %a;@."
205
		  print_alloc_prototype (m.mname.node_id, m.mstatic)
206
	end
207
    end
208

    
209
let print_machine_decl_from_header fmt inode =
210
  (*Mod.print_machine_decl_prefix fmt m;*)
211
  if inode.nodei_prototype = Some "C" then
212
    if inode.nodei_stateless then
213
      begin
214
	fprintf fmt "extern %a;@.@."
215
	  print_stateless_C_prototype
216
	  (inode.nodei_id, inode.nodei_inputs, inode.nodei_outputs)
217
      end
218
    else (
219
      raise (Invalid_argument ("A node with declared prototype C cannot be stateful, it has to be a function")))
220
  else
221
    if inode.nodei_stateless then
222
    begin
223
      fprintf fmt "extern %a;@.@."
224
	print_stateless_prototype 
225
	(inode.nodei_id, inode.nodei_inputs, inode.nodei_outputs)
226
    end
227
    else 
228
      begin
229
	let static_inputs = List.filter (fun v -> v.var_dec_const) inode.nodei_inputs in
230
	let used name =
231
	  (List.exists (fun v -> v.var_id = name) inode.nodei_inputs)
232
	  || (List.exists (fun v -> v.var_id = name) inode.nodei_outputs) in
233
	let self = mk_new_name used "self" in
234
	fprintf fmt "extern %a;@.@."
235
	  (print_reset_prototype self) (inode.nodei_id, static_inputs);
236
	
237
	fprintf fmt "extern %a;@.@."
238
	  (print_step_prototype self)
239
	  (inode.nodei_id, inode.nodei_inputs, inode.nodei_outputs)
240
      end
241

    
242
let print_const_decl fmt cdecl =
243
  fprintf fmt "extern %a;@." 
244
    (pp_c_type cdecl.const_id) cdecl.const_type
245

    
246
let rec pp_c_struct_type_field filename cpt fmt (label, tdesc) =
247
  fprintf fmt "%a;" (pp_c_type_decl filename cpt label) tdesc
248
and pp_c_type_decl filename cpt var fmt tdecl =
249
  match tdecl with
250
  | Tydec_any           -> assert false
251
  | Tydec_int           -> fprintf fmt "int %s" var
252
  | Tydec_real          -> fprintf fmt "double %s" var
253
  | Tydec_float         -> fprintf fmt "float %s" var
254
  | Tydec_bool          -> fprintf fmt "_Bool %s" var
255
  | Tydec_clock ty      -> pp_c_type_decl filename cpt var fmt ty
256
  | Tydec_const c       -> fprintf fmt "%s %s" c var
257
  | Tydec_array (d, ty) -> fprintf fmt "%a[%a]" (pp_c_type_decl filename cpt var) ty pp_c_dimension d
258
  | Tydec_enum tl ->
259
    begin
260
      incr cpt;
261
      fprintf fmt "enum _enum_%s_%d { %a } %s" filename !cpt (Utils.fprintf_list ~sep:", " pp_print_string) tl var
262
    end
263
  | Tydec_struct fl ->
264
    begin
265
      incr cpt;
266
      fprintf fmt "struct _struct_%s_%d { %a } %s" filename !cpt (Utils.fprintf_list ~sep:" " (pp_c_struct_type_field filename cpt)) fl var
267
    end
268

    
269
let print_type_definitions fmt filename =
270
  let cpt_type = ref 0 in
271
  Hashtbl.iter (fun typ decl ->
272
		match typ with
273
		| Tydec_const var ->
274
		   (match decl.top_decl_desc with
275
		    | TypeDef tdef ->
276
		       fprintf fmt "typedef %a;@.@."
277
			       (pp_c_type_decl filename cpt_type var) tdef.tydef_desc
278
		    | _ -> assert false)
279
		| _        -> ()) type_table
280

    
281
let reset_type_definitions, print_type_definition_from_header =
282
  let cpt_type =ref 0 in
283
  ((fun () -> cpt_type := 0),
284
   (fun fmt typ filename ->
285
    fprintf fmt "typedef %a;@.@."
286
	(pp_c_type_decl filename cpt_type typ.tydef_id) typ.tydef_desc))
287

    
288
(********************************************************************************************)
289
(*                         MAIN Header Printing functions                                   *)
290
(********************************************************************************************)
291
let print_header header_fmt basename prog machines dependencies =
292
  (* Include once: start *)
293
  let baseNAME = String.uppercase basename in
294
  let baseNAME = Str.global_replace (Str.regexp "\\.\\|\\ ") "_" baseNAME in
295
  begin
296
    (* Print the svn version number and the supported C standard (C90 or C99) *)
297
    print_version header_fmt;
298
    fprintf header_fmt "#ifndef _%s@.#define _%s@." baseNAME baseNAME;
299
    pp_print_newline header_fmt ();
300
    fprintf header_fmt "/* Imports standard library */@.";
301
    (* imports standard library definitions (arrow) *)
302
    print_import_standard header_fmt;
303
    pp_print_newline header_fmt ();
304
    (* imports dependencies *)
305
    fprintf header_fmt "/* Import Dependencies */@.";
306
    fprintf header_fmt "@[<v>";
307
    List.iter (print_import_prototype header_fmt) dependencies;
308
    fprintf header_fmt "@]@.";
309
    fprintf header_fmt "/* Types definitions */@.";
310
    (* Print the type definitions from the type table *)
311
    print_type_definitions header_fmt basename;
312
    pp_print_newline header_fmt ();
313
    (* Print the global constant declarations. *)
314
    fprintf header_fmt "/* Global constant (declarations, definitions are in C file) */@.";
315
    List.iter (fun c -> print_const_decl header_fmt (const_of_top c)) (get_consts prog);
316
    pp_print_newline header_fmt ();
317
    (* Print the struct declarations of all machines. *)
318
    fprintf header_fmt "/* Struct declarations */@.";
319
    List.iter (print_machine_struct header_fmt) machines;
320
    pp_print_newline header_fmt ();
321
    (* Print the prototypes of all machines *)
322
    fprintf header_fmt "/* Nodes declarations */@.";
323
    List.iter (print_machine_decl header_fmt) machines;
324
    pp_print_newline header_fmt ();
325
    (* Include once: end *)
326
    fprintf header_fmt "#endif@.";
327
    pp_print_newline header_fmt ()
328
  end
329

    
330
let print_alloc_header header_fmt basename prog machines dependencies =
331
  (* Include once: start *)
332
  let baseNAME = String.uppercase basename in
333
  let baseNAME = Str.global_replace (Str.regexp "\\.\\|\\ ") "_" baseNAME in
334
  begin
335
    (* Print the svn version number and the supported C standard (C90 or C99) *)
336
    print_version header_fmt;
337
    fprintf header_fmt "#ifndef _%s_alloc@.#define _%s_alloc@." baseNAME baseNAME;
338
    pp_print_newline header_fmt ();
339
    (* Import the header *)
340
    fprintf header_fmt "/* Import header from %s */@." basename;
341
    fprintf header_fmt "@[<v>";
342
    print_import_prototype header_fmt (Dep (true, basename, [], true (* assuming it is staful *) ));
343
    fprintf header_fmt "@]@.";
344
    fprintf header_fmt "/* Import dependencies */@.";
345
    fprintf header_fmt "@[<v>";
346
    List.iter (print_import_alloc_prototype header_fmt) dependencies;
347
    fprintf header_fmt "@]@.";
348
    (* Print the struct definitions of all machines. *)
349
    fprintf header_fmt "/* Struct definitions */@.";
350
    List.iter (print_machine_struct header_fmt) machines;
351
    pp_print_newline header_fmt ();
352
    (* Print the prototypes of all machines *)
353
    fprintf header_fmt "/* Node allocation function/macro prototypes */@.";
354
    List.iter (print_machine_alloc_decl header_fmt) machines;
355
    pp_print_newline header_fmt ();
356
    (* Include once: end *)
357
    fprintf header_fmt "#endif@.";
358
    pp_print_newline header_fmt ()
359
  end
360

    
361
(* Function called when compiling a lusi file and generating the associated C
362
   header. *)
363
let print_header_from_header header_fmt basename header =
364
  (* Include once: start *)
365
  let baseNAME = String.uppercase basename in
366
  let baseNAME = Str.global_replace (Str.regexp "\\.\\|\\ ") "_" baseNAME in
367
  let types = get_typedefs header in
368
  let consts = get_consts header in
369
  let nodes = get_imported_nodes header in
370
  let dependencies = get_dependencies header in
371
  begin
372
    (* Print the svn version number and the supported C standard (C90 or C99) *)
373
    print_version header_fmt;
374
    fprintf header_fmt "#ifndef _%s@.#define _%s@." baseNAME baseNAME;
375
    pp_print_newline header_fmt ();
376
    fprintf header_fmt "/* Imports standard library */@.";
377
    (* imports standard library definitions (arrow) *)
378
    print_import_standard header_fmt;
379
    pp_print_newline header_fmt ();
380
    (* imports dependencies *)
381
    fprintf header_fmt "/* Import dependencies */@.";
382
    fprintf header_fmt "@[<v>";
383
    List.iter
384
      (fun dep -> 
385
	let (local, s) = dependency_of_top dep in 
386
	print_import_prototype header_fmt (Dep (local, s, [], true (* assuming it is stateful *))))
387
      dependencies;
388
    fprintf header_fmt "@]@.";
389
    fprintf header_fmt "/* Types definitions */@.";
390
    (* Print the type definitions from the type table *)
391
    reset_type_definitions ();
392
    List.iter (fun typ -> print_type_definition_from_header header_fmt (typedef_of_top typ) basename) types;
393
    pp_print_newline header_fmt ();
394
    (* Print the global constant declarations. *)
395
    fprintf header_fmt "/* Global constant (declarations, definitions are in C file) */@.";
396
    List.iter (fun c -> print_const_decl header_fmt (const_of_top c)) consts;
397
    pp_print_newline header_fmt ();
398
    (* Print the struct declarations of all machines. *)
399
    fprintf header_fmt "/* Struct declarations */@.";
400
    List.iter (fun node -> print_machine_struct_from_header header_fmt (imported_node_of_top node)) nodes;
401
    pp_print_newline header_fmt ();
402
    (* Print the prototypes of all machines *)
403
    fprintf header_fmt "/* Nodes declarations */@.";
404
    List.iter (fun node -> print_machine_decl_from_header header_fmt (imported_node_of_top node)) nodes;
405
    pp_print_newline header_fmt ();
406
    (* Include once: end *)
407
    fprintf header_fmt "#endif@.";
408
    pp_print_newline header_fmt ()
409
  end
410

    
411
end
412
(* Local Variables: *)
413
(* compile-command:"make -C ../../.." *)
414
(* End: *)