Project

General

Profile

Download (16.9 KB) Statistics
| Branch: | Tag: | Revision:
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
let print_machine_decl fmt m =
154
  begin
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_step_prototype self)
187
	  (m.mname.node_id, m.mstep.step_inputs, m.mstep.step_outputs);
188
	
189
	if !Options.mpfr then
190
	  begin
191
	    fprintf fmt "extern %a;@.@."
192
	      (print_init_prototype self) (m.mname.node_id, m.mstatic);
193

    
194
	    fprintf fmt "extern %a;@.@."
195
	      (print_clear_prototype self) (m.mname.node_id, m.mstatic);
196
	  end
197
      end
198
  end
199

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

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

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

    
256
	fprintf fmt "extern %a;@.@."
257
	  (print_clear_prototype self) (inode.nodei_id, static_inputs);
258

    
259
	fprintf fmt "extern %a;@.@."
260
	  (print_step_prototype self)
261
	  (inode.nodei_id, inode.nodei_inputs, inode.nodei_outputs)
262
      end
263

    
264
let print_const_decl fmt cdecl =
265
  if !Options.mpfr &&  Types.is_real_type (Types.array_base_type cdecl.const_type)
266
  then
267
    fprintf fmt "extern %a;@." 
268
      (pp_c_type cdecl.const_id) (Types.dynamic_type cdecl.const_type) 
269
  else
270
    fprintf fmt "extern %a;@." 
271
      (pp_c_type cdecl.const_id) cdecl.const_type
272

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

    
298
let print_type_definitions fmt filename =
299
  let cpt_type = ref 0 in
300
  Hashtbl.iter (fun typ decl ->
301
		match typ with
302
		| Tydec_const var ->
303
		   (match decl.top_decl_desc with
304
		    | TypeDef tdef ->
305
		       fprintf fmt "typedef %a;@.@."
306
			       (pp_c_type_decl filename cpt_type var) tdef.tydef_desc
307
		    | _ -> assert false)
308
		| _        -> ()) type_table
309

    
310
let reset_type_definitions, print_type_definition_from_header =
311
  let cpt_type =ref 0 in
312
  ((fun () -> cpt_type := 0),
313
   (fun fmt typ filename ->
314
    fprintf fmt "typedef %a;@.@."
315
	(pp_c_type_decl filename cpt_type typ.tydef_id) typ.tydef_desc))
316

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

    
368
let print_alloc_header header_fmt basename prog machines dependencies =
369
  (* Include once: start *)
370
  let baseNAME = file_to_module_name basename 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_alloc@.#define _%s_alloc@." baseNAME baseNAME;
375
    pp_print_newline header_fmt ();
376
    (* Import the header *)
377
    fprintf header_fmt "/* Import header from %s */@." basename;
378
    fprintf header_fmt "@[<v>";
379
    print_import_prototype header_fmt (Dep (true, basename, [], true (* assuming it is staful *) ));
380
    fprintf header_fmt "@]@.";
381
    fprintf header_fmt "/* Import dependencies */@.";
382
    fprintf header_fmt "@[<v>";
383
    List.iter (print_import_alloc_prototype header_fmt) dependencies;
384
    fprintf header_fmt "@]@.";
385
    (* Print the struct definitions of all machines. *)
386
    fprintf header_fmt "/* Struct definitions */@.";
387
    List.iter (print_machine_struct header_fmt) machines;
388
    pp_print_newline header_fmt ();
389
    (* Print the prototypes of all machines *)
390
    fprintf header_fmt "/* Node allocation function/macro prototypes */@.";
391
    List.iter (print_machine_alloc_decl header_fmt) machines;
392
    pp_print_newline header_fmt ();
393
    (* Include once: end *)
394
    fprintf header_fmt "#endif@.";
395
    pp_print_newline header_fmt ()
396
  end
397

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

    
457
end
458
(* Local Variables: *)
459
(* compile-command:"make -C ../../.." *)
460
(* End: *)
(3-3/7)