Project

General

Profile

Statistics
| Branch: | Tag: | Revision:

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

History | View | Annotate | Download (15.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/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 m pp_var fmt v =
47
  Format.fprintf fmt "inst ## %s = %a"
48
    v.var_id
49
    (print_static_val pp_var) (Machine_code.get_const_assign m v)
50

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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