Project

General

Profile

Statistics
| Branch: | Tag: | Revision:

lustrec / src / backends / C / c_backend_header.ml @ 990210f3

History | View | Annotate | Download (17.1 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
  if !Options.cpp then
43
    fprintf fmt "#include \"%s/arrow.hpp\"@.@." arrow_top_decl.top_decl_owner 
44
  else
45
    fprintf fmt "#include \"%s/arrow.h\"@.@." arrow_top_decl.top_decl_owner 
46

    
47
  end
48

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

    
56
let print_constant_decl (m, attr, inst) pp_var fmt v =
57
  Format.fprintf fmt "%s %a = %a"
58
    attr
59
    (pp_c_type (Format.sprintf "%s ## %s" inst v.var_id)) v.var_type
60
    (print_static_val pp_var) (Machine_code.get_const_assign m v)
61

    
62
let print_static_constant_decl (m, attr, inst) fmt const_locals =
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
  Format.fprintf fmt "%a%t"
70
    (Utils.fprintf_list ~sep:";\\@," (print_constant_decl (m, attr, inst) pp_var)) const_locals
71
    (Utils.pp_final_char_if_non_empty ";\\@," const_locals)
72

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

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

    
111
      
112
let print_static_link_instance fmt (i, (m, _)) =
113
 fprintf fmt "%a(%s)" pp_machine_static_link_name (node_name m) i
114

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

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

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

    
185
	    fprintf fmt "extern %a;@.@."
186
	      print_dealloc_prototype m.mname.node_id;
187
	  end;
188
	let self = mk_self m in
189
	fprintf fmt "extern %a;@.@."
190
	  (print_reset_prototype self) (m.mname.node_id, m.mstatic);
191

    
192
	fprintf fmt "extern %a;@.@."
193
	  (print_step_prototype self)
194
	  (m.mname.node_id, m.mstep.step_inputs, m.mstep.step_outputs);
195
	
196
	if !Options.mpfr then
197
	  begin
198
	    fprintf fmt "extern %a;@.@."
199
	      (print_init_prototype self) (m.mname.node_id, m.mstatic);
200

    
201
	    fprintf fmt "extern %a;@.@."
202
	      (print_clear_prototype self) (m.mname.node_id, m.mstatic);
203
	  end
204
      end
205
  end
206

    
207
let print_machine_alloc_decl fmt m =
208
  Mod.print_machine_decl_prefix fmt m;
209
  if fst (get_stateless_status m) then
210
    begin
211
    end
212
  else
213
    begin
214
      if !Options.static_mem
215
      then
216
	begin
217
	  (* Static allocation *)
218
	  let inst = mk_instance m in
219
	  let attr = mk_attribute m in
220
	  fprintf fmt "%a@.%a@.%a@."
221
		  print_static_declare_macro (m, attr, inst)
222
		  print_static_link_macro (m, attr, inst)
223
		  print_static_alloc_macro (m, attr, inst)
224
	end
225
      else
226
	begin 
227
          (* Dynamic allocation *)
228
	  fprintf fmt "extern %a;@.@."
229
	    print_alloc_prototype (m.mname.node_id, m.mstatic);
230

    
231
	  fprintf fmt "extern %a;@.@."
232
	    print_dealloc_prototype m.mname.node_id
233
	end
234
    end
235

    
236
let print_machine_decl_from_header fmt inode =
237
  (*Mod.print_machine_decl_prefix fmt m;*)
238
  if inode.nodei_prototype = Some "C" then
239
    if inode.nodei_stateless then
240
      begin
241
	fprintf fmt "extern %a;@.@."
242
	  print_stateless_C_prototype
243
	  (inode.nodei_id, inode.nodei_inputs, inode.nodei_outputs)
244
      end
245
    else (Format.eprintf "internal error: print_machine_decl_from_header"; assert false)
246
  else
247
    if inode.nodei_stateless then
248
    begin
249
      fprintf fmt "extern %a;@.@."
250
	print_stateless_prototype 
251
	(inode.nodei_id, inode.nodei_inputs, inode.nodei_outputs)
252
    end
253
    else 
254
      begin
255
	let static_inputs = List.filter (fun v -> v.var_dec_const) inode.nodei_inputs in
256
	let used name =
257
	  (List.exists (fun v -> v.var_id = name) inode.nodei_inputs)
258
	  || (List.exists (fun v -> v.var_id = name) inode.nodei_outputs) in
259
	let self = mk_new_name used "self" in
260
	fprintf fmt "extern %a;@.@."
261
	  (print_reset_prototype self) (inode.nodei_id, static_inputs);
262

    
263
	fprintf fmt "extern %a;@.@."
264
	  (print_init_prototype self) (inode.nodei_id, static_inputs);
265

    
266
	fprintf fmt "extern %a;@.@."
267
	  (print_clear_prototype self) (inode.nodei_id, static_inputs);
268

    
269
	fprintf fmt "extern %a;@.@."
270
	  (print_step_prototype self)
271
	  (inode.nodei_id, inode.nodei_inputs, inode.nodei_outputs)
272
      end
273

    
274
let print_const_decl fmt cdecl =
275
  if !Options.mpfr &&  Types.is_real_type (Types.array_base_type cdecl.const_type)
276
  then
277
    fprintf fmt "extern %a;@." 
278
      (pp_c_type cdecl.const_id) (Types.dynamic_type cdecl.const_type) 
279
  else
280
    fprintf fmt "extern %a;@." 
281
      (pp_c_type cdecl.const_id) cdecl.const_type
282

    
283
let rec pp_c_struct_type_field filename cpt fmt (label, tdesc) =
284
  fprintf fmt "%a;" (pp_c_type_decl filename cpt label) tdesc
285
and pp_c_type_decl filename cpt var fmt tdecl =
286
  match tdecl with
287
  | Tydec_any           -> assert false
288
  | Tydec_int           -> fprintf fmt "int %s" var
289
  | Tydec_real when !Options.mpfr
290
                        -> fprintf fmt "%s %s" Mpfr.mpfr_t var
291
  | Tydec_real          -> fprintf fmt "double %s" var
292
  (* | Tydec_float         -> fprintf fmt "float %s" var *)
293
  | Tydec_bool          -> fprintf fmt "_Bool %s" var
294
  | Tydec_clock ty      -> pp_c_type_decl filename cpt var fmt ty
295
  | Tydec_const c       -> fprintf fmt "%s %s" c var
296
  | Tydec_array (d, ty) -> fprintf fmt "%a[%a]" (pp_c_type_decl filename cpt var) ty pp_c_dimension d
297
  | Tydec_enum tl ->
298
    begin
299
      incr cpt;
300
      fprintf fmt "enum _enum_%s_%d { %a } %s" filename !cpt (Utils.fprintf_list ~sep:", " pp_print_string) tl var
301
    end
302
  | Tydec_struct fl ->
303
    begin
304
      incr cpt;
305
      fprintf fmt "struct _struct_%s_%d { %a } %s" filename !cpt (Utils.fprintf_list ~sep:" " (pp_c_struct_type_field filename cpt)) fl var
306
    end
307

    
308
let print_type_definitions fmt filename =
309
  let cpt_type = ref 0 in
310
  Hashtbl.iter (fun typ decl ->
311
		match typ with
312
		| Tydec_const var ->
313
		   (match decl.top_decl_desc with
314
		    | TypeDef tdef ->
315
		       fprintf fmt "typedef %a;@.@."
316
			       (pp_c_type_decl filename cpt_type var) tdef.tydef_desc
317
		    | _ -> assert false)
318
		| _        -> ()) type_table
319

    
320
let reset_type_definitions, print_type_definition_from_header =
321
  let cpt_type =ref 0 in
322
  ((fun () -> cpt_type := 0),
323
   (fun fmt typ filename ->
324
    fprintf fmt "typedef %a;@.@."
325
	(pp_c_type_decl filename cpt_type typ.tydef_id) typ.tydef_desc))
326

    
327
(********************************************************************************************)
328
(*                         MAIN Header Printing functions                                   *)
329
(********************************************************************************************)
330
let print_header header_fmt basename prog machines dependencies =
331
  (* Include once: start *)
332
  let baseNAME = file_to_module_name basename in
333
  begin
334
    (* Print the version number and the supported C standard (C90 or C99) *)
335
    print_version header_fmt;
336
    fprintf header_fmt "#ifndef _%s@.#define _%s@." baseNAME baseNAME;
337
    pp_print_newline header_fmt ();
338
    fprintf header_fmt "/* Imports standard library */@.";
339
    (* imports standard library definitions (arrow) *)
340
    print_import_standard header_fmt;
341
    pp_print_newline header_fmt ();
342
    (* imports dependencies *)
343
    fprintf header_fmt "/* Import dependencies */@.";
344
    fprintf header_fmt "@[<v>";
345
    List.iter (print_import_prototype header_fmt) dependencies;
346
    fprintf header_fmt "@]@.";
347
    fprintf header_fmt "/* Types definitions */@.";
348
    (* Print the type definitions from the type table *)
349
    print_type_definitions header_fmt basename;
350
    pp_print_newline header_fmt ();
351
    (* Print the global constant declarations. *)
352
    fprintf header_fmt "/* Global constant (declarations, definitions are in C file) */@.";
353
    List.iter (fun c -> print_const_decl header_fmt (const_of_top c)) (get_consts prog);
354
    pp_print_newline header_fmt ();
355
    if !Options.mpfr then
356
      begin
357
	fprintf header_fmt "/* Global initialization declaration */@.";
358
	fprintf header_fmt "extern %a;@.@."
359
	  print_global_init_prototype baseNAME;
360
	
361
	fprintf header_fmt "/* Global clear declaration */@.";
362
	fprintf header_fmt "extern %a;@.@."
363
	  print_global_clear_prototype baseNAME;
364
      end;
365
    (* Print the struct declarations of all machines. *)
366
    fprintf header_fmt "/* Structs declarations */@.";
367
    List.iter (print_machine_struct header_fmt) machines;
368
    pp_print_newline header_fmt ();
369
    (* Print the prototypes of all machines *)
370
    fprintf header_fmt "/* Nodes declarations */@.";
371
    List.iter (print_machine_decl header_fmt) machines;
372
    pp_print_newline header_fmt ();
373
    (* Include once: end *)
374
    fprintf header_fmt "#endif@.";
375
    pp_print_newline header_fmt ()
376
  end
377

    
378
let print_alloc_header header_fmt basename prog machines dependencies =
379
  (* Include once: start *)
380
  let baseNAME = file_to_module_name basename in
381
  begin
382
    (* Print the svn version number and the supported C standard (C90 or C99) *)
383
    print_version header_fmt;
384
    fprintf header_fmt "#ifndef _%s_alloc@.#define _%s_alloc@." baseNAME baseNAME;
385
    pp_print_newline header_fmt ();
386
    (* Import the header *)
387
    fprintf header_fmt "/* Import header from %s */@." basename;
388
    fprintf header_fmt "@[<v>";
389
    print_import_prototype header_fmt (Dep (true, basename, [], true (* assuming it is staful *) ));
390
    fprintf header_fmt "@]@.";
391
    fprintf header_fmt "/* Import dependencies */@.";
392
    fprintf header_fmt "@[<v>";
393
    List.iter (print_import_alloc_prototype header_fmt) dependencies;
394
    fprintf header_fmt "@]@.";
395
    (* Print the struct definitions of all machines. *)
396
    fprintf header_fmt "/* Struct definitions */@.";
397
    List.iter (print_machine_struct header_fmt) machines;
398
    pp_print_newline header_fmt ();
399
    (* Print the prototypes of all machines *)
400
    fprintf header_fmt "/* Node allocation function/macro prototypes */@.";
401
    List.iter (print_machine_alloc_decl header_fmt) machines;
402
    pp_print_newline header_fmt ();
403
    (* Include once: end *)
404
    fprintf header_fmt "#endif@.";
405
    pp_print_newline header_fmt ()
406
  end
407

    
408
(* Function called when compiling a lusi file and generating the associated C
409
   header. *)
410
let print_header_from_header header_fmt basename header =
411
  (* Include once: start *)
412
  let baseNAME = file_to_module_name basename in
413
  let types = get_typedefs header in
414
  let consts = get_consts header in
415
  let nodes = get_imported_nodes header in
416
  let dependencies = get_dependencies header in
417
  begin
418
    (* Print the version number and the supported C standard (C90 or C99) *)
419
    print_version header_fmt;
420
    fprintf header_fmt "#ifndef _%s@.#define _%s@." baseNAME baseNAME;
421
    pp_print_newline header_fmt ();
422
    fprintf header_fmt "/* Imports standard library */@.";
423
    (* imports standard library definitions (arrow) *)
424
    print_import_standard header_fmt;
425
    pp_print_newline header_fmt ();
426
    (* imports dependencies *)
427
    fprintf header_fmt "/* Import dependencies */@.";
428
    fprintf header_fmt "@[<v>";
429
    List.iter
430
      (fun dep -> 
431
	let (local, s) = dependency_of_top dep in 
432
	print_import_prototype header_fmt (Dep (local, s, [], true (* assuming it is stateful *))))
433
      dependencies;
434
    fprintf header_fmt "@]@.";
435
    fprintf header_fmt "/* Types definitions */@.";
436
    (* Print the type definitions from the type table *)
437
    reset_type_definitions ();
438
    List.iter (fun typ -> print_type_definition_from_header header_fmt (typedef_of_top typ) basename) types;
439
    pp_print_newline header_fmt ();
440
    (* Print the global constant declarations. *)
441
    fprintf header_fmt "/* Global constant (declarations, definitions are in C file) */@.";
442
    List.iter (fun c -> print_const_decl header_fmt (const_of_top c)) consts;
443
    pp_print_newline header_fmt ();
444
    if !Options.mpfr then
445
      begin
446
	fprintf header_fmt "/* Global initialization declaration */@.";
447
	fprintf header_fmt "extern %a;@.@."
448
	  print_global_init_prototype baseNAME;
449
	
450
	fprintf header_fmt "/* Global clear declaration */@.";
451
	fprintf header_fmt "extern %a;@.@."
452
	  print_global_clear_prototype baseNAME;
453
      end;
454
    (* Print the struct declarations of all machines. *)
455
    fprintf header_fmt "/* Structs declarations */@.";
456
    List.iter (fun node -> print_machine_struct_from_header header_fmt (imported_node_of_top node)) nodes;
457
    pp_print_newline header_fmt ();
458
    (* Print the prototypes of all machines *)
459
    fprintf header_fmt "/* Nodes declarations */@.";
460
    List.iter (fun node -> print_machine_decl_from_header header_fmt (imported_node_of_top node)) nodes;
461
    pp_print_newline header_fmt ();
462
    (* Include once: end *)
463
    fprintf header_fmt "#endif@.";
464
    pp_print_newline header_fmt ()
465
  end
466

    
467
end
468
(* Local Variables: *)
469
(* compile-command:"make -C ../../.." *)
470
(* End: *)