Project

General

Profile

Statistics
| Branch: | Tag: | Revision:

lustrec / src / backends / C / c_backend_header.ml @ 53206908

History | View | Annotate | Download (16.2 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
  begin
38
    if !Options.mpfr then
39
      begin
40
	fprintf fmt "#include <mpfr.h>@."
41
      end;
42
    fprintf fmt "#include \"%s/arrow.h\"@.@." Version.include_path
43
  end
44

    
45
let rec print_static_val pp_var fmt v =
46
  match v.value_desc with
47
  | Cst c         -> pp_c_const fmt c
48
  | LocalVar v    -> pp_var fmt v
49
  | Fun (n, vl)   -> Basic_library.pp_c n (print_static_val pp_var) fmt vl
50
  | _             -> (Format.eprintf "Internal error: C_backend_header.print_static_val"; assert false)
51

    
52
let print_constant_decl (m, attr, inst) pp_var fmt v =
53
  Format.fprintf fmt "%s %a = %a"
54
    attr
55
    (pp_c_type (Format.sprintf "%s ## %s" inst v.var_id)) v.var_type
56
    (print_static_val pp_var) (Machine_code.get_const_assign m v)
57

    
58
let print_static_constant_decl (m, attr, inst) fmt const_locals =
59
  let pp_var fmt v =
60
    if List.mem v const_locals
61
    then
62
      Format.fprintf fmt "%s ## %s" inst v.var_id
63
    else 
64
      Format.fprintf fmt "%s" v.var_id in
65
  Format.fprintf fmt "%a%t"
66
    (Utils.fprintf_list ~sep:";\\@," (print_constant_decl (m, attr, inst) pp_var)) const_locals
67
    (Utils.pp_final_char_if_non_empty ";\\@," const_locals)
68

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

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

    
107
      
108
let print_static_link_instance fmt (i, (m, _)) =
109
 fprintf fmt "%a(%s)" pp_machine_static_link_name (node_name m) i
110

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

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

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

    
185
     fprintf fmt "extern %a;@.@."
186
	(print_init_prototype self) (m.mname.node_id, m.mstatic);
187

    
188
     fprintf fmt "extern %a;@.@."
189
	(print_clear_prototype self) (m.mname.node_id, m.mstatic);
190

    
191
      fprintf fmt "extern %a;@.@."
192
	(print_step_prototype self)
193
	(m.mname.node_id, m.mstep.step_inputs, m.mstep.step_outputs)
194
    end
195

    
196
let print_machine_alloc_decl fmt m =
197
  Mod.print_machine_decl_prefix fmt m;
198
  if fst (get_stateless_status m) then
199
    begin
200
    end
201
  else
202
    begin
203
      if !Options.static_mem
204
      then
205
	begin
206
	  (* Static allocation *)
207
	  let inst = mk_instance m in
208
	  let attr = mk_attribute m in
209
	  fprintf fmt "%a@.%a@.%a@."
210
		  print_static_declare_macro (m, attr, inst)
211
		  print_static_link_macro (m, attr, inst)
212
		  print_static_alloc_macro (m, attr, inst)
213
	end
214
      else
215
	begin 
216
          (* Dynamic allocation *)
217
	  fprintf fmt "extern %a;@."
218
		  print_alloc_prototype (m.mname.node_id, m.mstatic)
219
	end
220
    end
221

    
222
let print_machine_decl_from_header fmt inode =
223
  (*Mod.print_machine_decl_prefix fmt m;*)
224
  if inode.nodei_prototype = Some "C" then
225
    if inode.nodei_stateless then
226
      begin
227
	fprintf fmt "extern %a;@.@."
228
	  print_stateless_C_prototype
229
	  (inode.nodei_id, inode.nodei_inputs, inode.nodei_outputs)
230
      end
231
    else (
232
      raise (Invalid_argument ("A node with declared prototype C cannot be stateful, it has to be a function")))
233
  else
234
    if inode.nodei_stateless then
235
    begin
236
      fprintf fmt "extern %a;@.@."
237
	print_stateless_prototype 
238
	(inode.nodei_id, inode.nodei_inputs, inode.nodei_outputs)
239
    end
240
    else 
241
      begin
242
	let static_inputs = List.filter (fun v -> v.var_dec_const) inode.nodei_inputs in
243
	let used name =
244
	  (List.exists (fun v -> v.var_id = name) inode.nodei_inputs)
245
	  || (List.exists (fun v -> v.var_id = name) inode.nodei_outputs) in
246
	let self = mk_new_name used "self" in
247
	fprintf fmt "extern %a;@.@."
248
	  (print_reset_prototype self) (inode.nodei_id, static_inputs);
249

    
250
	fprintf fmt "extern %a;@.@."
251
	  (print_init_prototype self) (inode.nodei_id, static_inputs);
252

    
253
	fprintf fmt "extern %a;@.@."
254
	  (print_clear_prototype self) (inode.nodei_id, static_inputs);
255

    
256
	fprintf fmt "extern %a;@.@."
257
	  (print_step_prototype self)
258
	  (inode.nodei_id, inode.nodei_inputs, inode.nodei_outputs)
259
      end
260

    
261
let print_const_decl fmt cdecl =
262
  fprintf fmt "extern %a;@." 
263
    (pp_c_type cdecl.const_id) cdecl.const_type
264

    
265
let rec pp_c_struct_type_field filename cpt fmt (label, tdesc) =
266
  fprintf fmt "%a;" (pp_c_type_decl filename cpt label) tdesc
267
and pp_c_type_decl filename cpt var fmt tdecl =
268
  match tdecl with
269
  | Tydec_any           -> assert false
270
  | Tydec_int           -> fprintf fmt "int %s" var
271
  | Tydec_real when !Options.mpfr
272
                        -> fprintf fmt "%s %s" Mpfr.mpfr_t var
273
  | Tydec_real          -> fprintf fmt "double %s" var
274
  (* | Tydec_float         -> fprintf fmt "float %s" var *)
275
  | Tydec_bool          -> fprintf fmt "_Bool %s" var
276
  | Tydec_clock ty      -> pp_c_type_decl filename cpt var fmt ty
277
  | Tydec_const c       -> fprintf fmt "%s %s" c var
278
  | Tydec_array (d, ty) -> fprintf fmt "%a[%a]" (pp_c_type_decl filename cpt var) ty pp_c_dimension d
279
  | Tydec_enum tl ->
280
    begin
281
      incr cpt;
282
      fprintf fmt "enum _enum_%s_%d { %a } %s" filename !cpt (Utils.fprintf_list ~sep:", " pp_print_string) tl var
283
    end
284
  | Tydec_struct fl ->
285
    begin
286
      incr cpt;
287
      fprintf fmt "struct _struct_%s_%d { %a } %s" filename !cpt (Utils.fprintf_list ~sep:" " (pp_c_struct_type_field filename cpt)) fl var
288
    end
289

    
290
let print_type_definitions fmt filename =
291
  let cpt_type = ref 0 in
292
  Hashtbl.iter (fun typ decl ->
293
		match typ with
294
		| Tydec_const var ->
295
		   (match decl.top_decl_desc with
296
		    | TypeDef tdef ->
297
		       fprintf fmt "typedef %a;@.@."
298
			       (pp_c_type_decl filename cpt_type var) tdef.tydef_desc
299
		    | _ -> assert false)
300
		| _        -> ()) type_table
301

    
302
let reset_type_definitions, print_type_definition_from_header =
303
  let cpt_type =ref 0 in
304
  ((fun () -> cpt_type := 0),
305
   (fun fmt typ filename ->
306
    fprintf fmt "typedef %a;@.@."
307
	(pp_c_type_decl filename cpt_type typ.tydef_id) typ.tydef_desc))
308

    
309
(********************************************************************************************)
310
(*                         MAIN Header Printing functions                                   *)
311
(********************************************************************************************)
312
let print_header header_fmt basename prog machines dependencies =
313
  (* Include once: start *)
314
  let baseNAME = String.uppercase basename in
315
  let baseNAME = Str.global_replace (Str.regexp "\\.\\|\\ ") "_" baseNAME in
316
  begin
317
    (* Print the svn version number and the supported C standard (C90 or C99) *)
318
    print_version header_fmt;
319
    fprintf header_fmt "#ifndef _%s@.#define _%s@." baseNAME baseNAME;
320
    pp_print_newline header_fmt ();
321
    fprintf header_fmt "/* Imports standard library */@.";
322
    (* imports standard library definitions (arrow) *)
323
    print_import_standard header_fmt;
324
    pp_print_newline header_fmt ();
325
    (* imports dependencies *)
326
    fprintf header_fmt "/* Import Dependencies */@.";
327
    fprintf header_fmt "@[<v>";
328
    List.iter (print_import_prototype header_fmt) dependencies;
329
    fprintf header_fmt "@]@.";
330
    fprintf header_fmt "/* Types definitions */@.";
331
    (* Print the type definitions from the type table *)
332
    print_type_definitions header_fmt basename;
333
    pp_print_newline header_fmt ();
334
    (* Print the global constant declarations. *)
335
    fprintf header_fmt "/* Global constant (declarations, definitions are in C file) */@.";
336
    List.iter (fun c -> print_const_decl header_fmt (const_of_top c)) (get_consts prog);
337
    pp_print_newline header_fmt ();
338
    (* Print the struct declarations of all machines. *)
339
    fprintf header_fmt "/* Struct declarations */@.";
340
    List.iter (print_machine_struct header_fmt) machines;
341
    pp_print_newline header_fmt ();
342
    (* Print the prototypes of all machines *)
343
    fprintf header_fmt "/* Nodes declarations */@.";
344
    List.iter (print_machine_decl header_fmt) machines;
345
    pp_print_newline header_fmt ();
346
    (* Include once: end *)
347
    fprintf header_fmt "#endif@.";
348
    pp_print_newline header_fmt ()
349
  end
350

    
351
let print_alloc_header header_fmt basename prog machines dependencies =
352
  (* Include once: start *)
353
  let baseNAME = String.uppercase basename in
354
  let baseNAME = Str.global_replace (Str.regexp "\\.\\|\\ ") "_" baseNAME in
355
  begin
356
    (* Print the svn version number and the supported C standard (C90 or C99) *)
357
    print_version header_fmt;
358
    fprintf header_fmt "#ifndef _%s_alloc@.#define _%s_alloc@." baseNAME baseNAME;
359
    pp_print_newline header_fmt ();
360
    (* Import the header *)
361
    fprintf header_fmt "/* Import header from %s */@." basename;
362
    fprintf header_fmt "@[<v>";
363
    print_import_prototype header_fmt (Dep (true, basename, [], true (* assuming it is staful *) ));
364
    fprintf header_fmt "@]@.";
365
    fprintf header_fmt "/* Import dependencies */@.";
366
    fprintf header_fmt "@[<v>";
367
    List.iter (print_import_alloc_prototype header_fmt) dependencies;
368
    fprintf header_fmt "@]@.";
369
    (* Print the struct definitions of all machines. *)
370
    fprintf header_fmt "/* Struct definitions */@.";
371
    List.iter (print_machine_struct header_fmt) machines;
372
    pp_print_newline header_fmt ();
373
    (* Print the prototypes of all machines *)
374
    fprintf header_fmt "/* Node allocation function/macro prototypes */@.";
375
    List.iter (print_machine_alloc_decl header_fmt) machines;
376
    pp_print_newline header_fmt ();
377
    (* Include once: end *)
378
    fprintf header_fmt "#endif@.";
379
    pp_print_newline header_fmt ()
380
  end
381

    
382
(* Function called when compiling a lusi file and generating the associated C
383
   header. *)
384
let print_header_from_header header_fmt basename header =
385
  (* Include once: start *)
386
  let baseNAME = String.uppercase basename in
387
  let baseNAME = Str.global_replace (Str.regexp "\\.\\|\\ ") "_" baseNAME in
388
  let types = get_typedefs header in
389
  let consts = get_consts header in
390
  let nodes = get_imported_nodes header in
391
  let dependencies = get_dependencies header in
392
  begin
393
    (* Print the svn version number and the supported C standard (C90 or C99) *)
394
    print_version header_fmt;
395
    fprintf header_fmt "#ifndef _%s@.#define _%s@." baseNAME baseNAME;
396
    pp_print_newline header_fmt ();
397
    fprintf header_fmt "/* Imports standard library */@.";
398
    (* imports standard library definitions (arrow) *)
399
    print_import_standard header_fmt;
400
    pp_print_newline header_fmt ();
401
    (* imports dependencies *)
402
    fprintf header_fmt "/* Import dependencies */@.";
403
    fprintf header_fmt "@[<v>";
404
    List.iter
405
      (fun dep -> 
406
	let (local, s) = dependency_of_top dep in 
407
	print_import_prototype header_fmt (Dep (local, s, [], true (* assuming it is stateful *))))
408
      dependencies;
409
    fprintf header_fmt "@]@.";
410
    fprintf header_fmt "/* Types definitions */@.";
411
    (* Print the type definitions from the type table *)
412
    reset_type_definitions ();
413
    List.iter (fun typ -> print_type_definition_from_header header_fmt (typedef_of_top typ) basename) types;
414
    pp_print_newline header_fmt ();
415
    (* Print the global constant declarations. *)
416
    fprintf header_fmt "/* Global constant (declarations, definitions are in C file) */@.";
417
    List.iter (fun c -> print_const_decl header_fmt (const_of_top c)) consts;
418
    pp_print_newline header_fmt ();
419
    (* Print the struct declarations of all machines. *)
420
    fprintf header_fmt "/* Struct declarations */@.";
421
    List.iter (fun node -> print_machine_struct_from_header header_fmt (imported_node_of_top node)) nodes;
422
    pp_print_newline header_fmt ();
423
    (* Print the prototypes of all machines *)
424
    fprintf header_fmt "/* Nodes declarations */@.";
425
    List.iter (fun node -> print_machine_decl_from_header header_fmt (imported_node_of_top node)) nodes;
426
    pp_print_newline header_fmt ();
427
    (* Include once: end *)
428
    fprintf header_fmt "#endif@.";
429
    pp_print_newline header_fmt ()
430
  end
431

    
432
end
433
(* Local Variables: *)
434
(* compile-command:"make -C ../../.." *)
435
(* End: *)