Project

General

Profile

Statistics
| Branch: | Tag: | Revision:

lustrec / src / backends / C / c_backend_header.ml @ 80f93e0a

History | View | Annotate | Download (17 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\"@.@." !Options.include_dir
43

    
44
  end
45

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

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

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

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

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

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

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

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

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

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

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

    
198
	    fprintf fmt "extern %a;@.@."
199
	      (print_clear_prototype self) (m.mname.node_id, m.mstatic);
200
	  end
201
      end
202
  end
203

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

    
228
	  fprintf fmt "extern %a;@.@."
229
	    print_dealloc_prototype m.mname.node_id
230
	end
231
    end
232

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

    
260
	fprintf fmt "extern %a;@.@."
261
	  (print_init_prototype self) (inode.nodei_id, static_inputs);
262

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

    
266
	fprintf fmt "extern %a;@.@."
267
	  (print_step_prototype self)
268
	  (inode.nodei_id, inode.nodei_inputs, inode.nodei_outputs)
269
      end
270

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

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

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

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

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

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

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

    
464
end
465
(* Local Variables: *)
466
(* compile-command:"make -C ../../.." *)
467
(* End: *)