Project

General

Profile

Download (16.6 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 Lustre_types
14
open Corelang
15
open Machine_code_types
16
open Machine_code_common
17
open C_backend_common
18

    
19
(********************************************************************************************)
20
(*                         Header Printing functions                                        *)
21
(********************************************************************************************)
22

    
23

    
24
module type MODIFIERS_HDR =
25
sig
26
  val print_machine_decl_prefix: Format.formatter -> machine_t -> unit
27
end
28

    
29
module EmptyMod =
30
struct
31
  let print_machine_decl_prefix = fun fmt x -> ()
32
end
33

    
34
module Main = functor (Mod: MODIFIERS_HDR) -> 
35
struct
36

    
37
let print_import_standard fmt =
38
  begin
39
    (* if Machine_types.has_machine_type () then *)
40
    (*   begin *)
41
	fprintf fmt "#include <stdint.h>@.";
42
      (* end; *)
43
    if !Options.mpfr then
44
      begin
45
	fprintf fmt "#include <mpfr.h>@."
46
      end;
47
    if !Options.cpp then
48
      fprintf fmt "#include \"%s/arrow.hpp\"@.@." Arrow.arrow_top_decl.top_decl_owner 
49
    else
50
      fprintf fmt "#include \"%s/arrow.h\"@.@." Arrow.arrow_top_decl.top_decl_owner 
51
	
52
  end
53

    
54
let rec print_static_val pp_var fmt v =
55
  match v.value_desc with
56
  | Cst c         -> pp_c_const fmt c
57
  | Var v         -> pp_var fmt v
58
  | Fun (n, vl)   -> pp_basic_lib_fun (Types.is_int_type v.value_type) n (print_static_val pp_var) fmt vl
59
  | _             -> (Format.eprintf "Internal error: C_backend_header.print_static_val"; assert false)
60

    
61
let print_constant_decl (m, attr, inst) pp_var fmt v =
62
  Format.fprintf fmt "%s %a = %a"
63
    attr
64
    (pp_c_type (Format.sprintf "%s ## %s" inst v.var_id)) v.var_type
65
    (print_static_val pp_var) (get_const_assign m v)
66

    
67
let print_static_constant_decl (m, attr, inst) fmt const_locals =
68
  let pp_var fmt v =
69
    if List.mem v const_locals
70
    then
71
      Format.fprintf fmt "%s ## %s" inst v.var_id
72
    else 
73
      Format.fprintf fmt "%s" v.var_id in
74
  Format.fprintf fmt "%a%t"
75
    (Utils.fprintf_list ~sep:";\\@," (print_constant_decl (m, attr, inst) pp_var)) const_locals
76
    (Utils.pp_final_char_if_non_empty ";\\@," const_locals)
77

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

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

    
116
      
117
let print_static_link_instance fmt (i, (m, _)) =
118
 fprintf fmt "%a(%s)" pp_machine_static_link_name (node_name m) i
119

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

    
147
let print_static_alloc_macro fmt (m, attr, inst) =
148
  fprintf fmt "@[<v>@[<v 2>#define %a(%s, %a%t%s)\\@,%a(%s, %a%t%s);\\@,%a(%s);@]@,@]@."
149
    pp_machine_static_alloc_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_declare_name m.mname.node_id
155
    attr
156
    (Utils.fprintf_list ~sep:", " (pp_c_var_read m)) m.mstatic
157
    (Utils.pp_final_char_if_non_empty ", " m.mstatic)
158
    inst
159
    pp_machine_static_link_name m.mname.node_id
160
    inst
161

    
162
(* TODO: ACSL
163
we do multiple things:
164
- provide the semantics of the node as a predicate: function step and reset are associated to ACSL predicate
165
- the node is associated to a refinement contract, wrt its ACSL sem
166
- if the node is a regular node associated to a contract, print the contract as function contract.
167
- do not print anything if this is a contract node
168
*)
169
let print_machine_alloc_decl machines fmt m =
170
  Mod.print_machine_decl_prefix fmt m;
171
  if fst (get_stateless_status m) then
172
    begin
173
    end
174
  else
175
    begin
176
      if !Options.static_mem
177
      then
178
	begin
179
	  (* Static allocation *)
180
	  let inst = mk_instance m in
181
	  let attr = mk_attribute m in
182
	  fprintf fmt "%a@.%a@.%a@."
183
		  print_static_declare_macro (m, attr, inst)
184
		  print_static_link_macro (m, attr, inst)
185
		  print_static_alloc_macro (m, attr, inst)
186
	end
187
      else
188
	begin 
189
          (* Dynamic allocation *)
190
	  fprintf fmt "extern %a;@.@."
191
	    print_alloc_prototype (m.mname.node_id, m.mstatic);
192

    
193
	  fprintf fmt "extern %a;@.@."
194
	    print_dealloc_prototype m.mname.node_id
195
	end
196
    end
197

    
198
let print_machine_decl_from_header fmt inode =
199
  (*Mod.print_machine_decl_prefix fmt m;*)
200
  if inode.nodei_prototype = Some "C" then
201
    if inode.nodei_stateless then
202
      begin
203
	fprintf fmt "extern %a;@.@."
204
	  print_stateless_C_prototype
205
	  (inode.nodei_id, inode.nodei_inputs, inode.nodei_outputs)
206
      end
207
    else (Format.eprintf "internal error: print_machine_decl_from_header"; assert false)
208
  else
209
    if inode.nodei_stateless then
210
    begin
211
      fprintf fmt "extern %a;@.@."
212
	print_stateless_prototype 
213
	(inode.nodei_id, inode.nodei_inputs, inode.nodei_outputs)
214
    end
215
    else 
216
      begin
217
	let static_inputs = List.filter (fun v -> v.var_dec_const) inode.nodei_inputs in
218
	let used name =
219
	  (List.exists (fun v -> v.var_id = name) inode.nodei_inputs)
220
	  || (List.exists (fun v -> v.var_id = name) inode.nodei_outputs) in
221
	let self = mk_new_name used "self" in
222
	fprintf fmt "extern %a;@.@."
223
	  (print_reset_prototype self) (inode.nodei_id, static_inputs);
224

    
225
	fprintf fmt "extern %a;@.@."
226
	  (print_init_prototype self) (inode.nodei_id, static_inputs);
227

    
228
	fprintf fmt "extern %a;@.@."
229
	  (print_clear_prototype self) (inode.nodei_id, static_inputs);
230

    
231
	fprintf fmt "extern %a;@.@."
232
	  (print_step_prototype self)
233
	  (inode.nodei_id, inode.nodei_inputs, inode.nodei_outputs)
234
      end
235

    
236
let print_const_decl fmt cdecl =
237
  if !Options.mpfr &&  Types.is_real_type (Types.array_base_type cdecl.const_type)
238
  then
239
    fprintf fmt "extern %a;@." 
240
      (pp_c_type cdecl.const_id) (Types.dynamic_type cdecl.const_type) 
241
  else
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 when !Options.mpfr
252
                        -> fprintf fmt "%s %s" Mpfr.mpfr_t var
253
  | Tydec_real          -> fprintf fmt "double %s" var
254
  (* | Tydec_float         -> fprintf fmt "float %s" var *)
255
  | Tydec_bool          -> fprintf fmt "_Bool %s" var
256
  | Tydec_clock ty      -> pp_c_type_decl filename cpt var fmt ty
257
  | Tydec_const c       -> fprintf fmt "%s %s" c var
258
  | Tydec_array (d, ty) -> fprintf fmt "%a[%a]" (pp_c_type_decl filename cpt var) ty pp_c_dimension d
259
  | Tydec_enum tl ->
260
    begin
261
      incr cpt;
262
      fprintf fmt "enum _enum_%s_%d { %a } %s" (protect_filename filename) !cpt (Utils.fprintf_list ~sep:", " pp_print_string) tl var
263
    end
264
  | Tydec_struct fl ->
265
    begin
266
      incr cpt;
267
      fprintf fmt "struct _struct_%s_%d { %a } %s" (protect_filename filename) !cpt (Utils.fprintf_list ~sep:" " (pp_c_struct_type_field filename cpt)) fl var
268
    end
269

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

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

    
289
(********************************************************************************************)
290
(*                         MAIN Header Printing functions                                   *)
291
(********************************************************************************************)
292
(* Seems not used
293

    
294
let print_header header_fmt basename prog machines dependencies =
295
  (* Include once: start *)
296
  let baseNAME = file_to_module_name basename in
297
  begin
298
    (* Print the version number and the supported C standard (C90 or C99) *)
299
    print_version header_fmt;
300
    fprintf header_fmt "#ifndef _%s@.#define _%s@." baseNAME baseNAME;
301
    pp_print_newline header_fmt ();
302
    fprintf header_fmt "/* Imports standard library */@.";
303
    (* imports standard library definitions (arrow) *)
304
    print_import_standard header_fmt;
305
    pp_print_newline header_fmt ();
306
    (* imports dependencies *)
307
    fprintf header_fmt "/* Import dependencies */@.";
308
    fprintf header_fmt "@[<v>";
309
    List.iter (print_import_prototype header_fmt) dependencies;
310
    fprintf header_fmt "@]@.";
311
    fprintf header_fmt "/* Types definitions */@.";
312
    (* Print the type definitions from the type table *)
313
    print_type_definitions header_fmt basename;
314
    pp_print_newline header_fmt ();
315
    (* Print the global constant declarations. *)
316
    fprintf header_fmt "/* Global constant (declarations, definitions are in C file) */@.";
317
    List.iter (fun c -> print_const_decl header_fmt (const_of_top c)) (get_consts prog);
318
    pp_print_newline header_fmt ();
319
    if !Options.mpfr then
320
      begin
321
	fprintf header_fmt "/* Global initialization declaration */@.";
322
	fprintf header_fmt "extern %a;@.@."
323
	  print_global_init_prototype baseNAME;
324
	
325
	fprintf header_fmt "/* Global clear declaration */@.";
326
	fprintf header_fmt "extern %a;@.@."
327
	  print_global_clear_prototype baseNAME;
328
      end;
329
    (* Print the struct declarations of all machines. *)
330
    fprintf header_fmt "/* Structs declarations */@.";
331
    List.iter (print_machine_struct machines header_fmt) machines;
332
    pp_print_newline header_fmt ();
333
    (* Print the prototypes of all machines *)
334
    fprintf header_fmt "/* Nodes declarations */@.";
335
    List.iter (print_machine_decl header_fmt) machines;
336
    pp_print_newline header_fmt ();
337
    (* Include once: end *)
338
    fprintf header_fmt "#endif@.";
339
    pp_print_newline header_fmt ()
340
  end
341
  *)
342
let print_alloc_header header_fmt basename prog machines dependencies spec =
343
  (* Include once: start *)
344
  let baseNAME = file_to_module_name basename in
345
  begin
346
    (* Print the svn version number and the supported C standard (C90 or C99) *)
347
    print_version header_fmt;
348
    fprintf header_fmt "#ifndef _%s_alloc@.#define _%s_alloc@." baseNAME baseNAME;
349
    pp_print_newline header_fmt ();
350
    (* Import the header *)
351
    fprintf header_fmt "/* Import header from %s */@." basename;
352
    fprintf header_fmt "@[<v>";
353
    print_import_prototype header_fmt {local=true; name=basename; content=[]; is_stateful=true} (* assuming it is staful *);
354
    fprintf header_fmt "@]@.";
355
    fprintf header_fmt "/* Import dependencies */@.";
356
    fprintf header_fmt "@[<v>";
357
    List.iter (print_import_alloc_prototype header_fmt) dependencies;
358
    fprintf header_fmt "@]@.";
359
    (* Print the struct definitions of all machines. *)
360
    fprintf header_fmt "/* Struct definitions */@.";
361
    List.iter (print_machine_struct machines header_fmt) machines;
362
    pp_print_newline header_fmt ();
363
    fprintf header_fmt "/* Specification */@.%a@." C_backend_spec.pp_acsl_preamble spec;
364
    (* Print the prototypes of all machines *)
365
    fprintf header_fmt "/* Node allocation function/macro prototypes */@.";
366
    List.iter (print_machine_alloc_decl machines header_fmt) machines;
367
    pp_print_newline header_fmt ();
368
    (* Include once: end *)
369
    fprintf header_fmt "#endif@.";
370
    pp_print_newline header_fmt ()
371
  end
372

    
373
(* Function called when compiling a lusi file and generating the associated C
374
   header. *)
375
let print_header_from_header header_fmt basename header =
376
  (* Include once: start *)
377
  let baseNAME = file_to_module_name basename in
378
  let types = get_typedefs header in
379
  let consts = get_consts header in
380
  let nodes = get_imported_nodes header in
381
  let dependencies = get_dependencies header in
382
  begin
383
    (* Print the version number and the supported C standard (C90 or C99) *)
384
    print_version header_fmt;
385
    fprintf header_fmt "#ifndef _%s@.#define _%s@." baseNAME baseNAME;
386
    pp_print_newline header_fmt ();
387
    fprintf header_fmt "/* Imports standard library */@.";
388
    (* imports standard library definitions (arrow) *)
389
    print_import_standard header_fmt;
390
    pp_print_newline header_fmt ();
391
    (* imports dependencies *)
392
    fprintf header_fmt "/* Import dependencies */@.";
393
    fprintf header_fmt "@[<v>";
394
    List.iter
395
      (fun dep -> 
396
	let (local, s) = dependency_of_top dep in 
397
	print_import_prototype header_fmt {local=local; name=s; content=[]; is_stateful=true} (* assuming it is stateful *))
398
      dependencies;
399
    fprintf header_fmt "@]@.";
400
    fprintf header_fmt "/* Types definitions */@.";
401
    (* Print the type definitions from the type table *)
402
    reset_type_definitions ();
403
    List.iter (fun typ -> print_type_definition_from_header header_fmt (typedef_of_top typ) basename) types;
404
    pp_print_newline header_fmt ();
405
    (* Print the global constant declarations. *)
406
    fprintf header_fmt "/* Global constant (declarations, definitions are in C file) */@.";
407
    List.iter (fun c -> print_const_decl header_fmt (const_of_top c)) consts;
408
    pp_print_newline header_fmt ();
409
    if !Options.mpfr then
410
      begin
411
	fprintf header_fmt "/* Global initialization declaration */@.";
412
	fprintf header_fmt "extern %a;@.@."
413
	  print_global_init_prototype baseNAME;
414
	
415
	fprintf header_fmt "/* Global clear declaration */@.";
416
	fprintf header_fmt "extern %a;@.@."
417
	  print_global_clear_prototype baseNAME;
418
      end;
419
    (* Print the struct declarations of all machines. *)
420
    fprintf header_fmt "/* Structs 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: *)
(5-5/11)