Project

General

Profile

Download (14 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
  fprintf fmt "#include \"%s/arrow.h\"@.@." Version.include_path
38

    
39
let print_static_declare_instance attr fmt (i, (m, static)) =
40
  fprintf fmt "%a(%s, %a%t%s)"
41
    pp_machine_static_declare_name (node_name m)
42
    attr
43
    (Utils.fprintf_list ~sep:", " Dimension.pp_dimension) static
44
    (Utils.pp_final_char_if_non_empty ", " static)
45
    i
46

    
47
let print_static_declare_macro fmt m =
48
  let array_mem = List.filter (fun v -> Types.is_array_type v.var_type) m.mmemory in
49
  let inst = mk_instance m in
50
  let attr = mk_attribute m in
51
  fprintf fmt "@[<v 2>#define %a(%s, %a%t%s)\\@,%s %a %s;\\@,%a%t%a;@,@]"
52
    pp_machine_static_declare_name m.mname.node_id
53
    attr
54
    (Utils.fprintf_list ~sep:", " (pp_c_var_read m)) m.mstatic
55
    (Utils.pp_final_char_if_non_empty ", " m.mstatic)
56
    inst
57
    attr
58
    pp_machine_memtype_name m.mname.node_id
59
    inst
60
    (Utils.fprintf_list ~sep:";\\@," pp_c_decl_local_var) array_mem
61
    (Utils.pp_final_char_if_non_empty ";\\@," array_mem)
62
    (Utils.fprintf_list ~sep:";\\@,"
63
       (fun fmt (i',m') ->
64
	 let path = sprintf "inst ## _%s" i' in
65
	 fprintf fmt "%a"
66
	   (print_static_declare_instance attr) (path,m')
67
       )) m.minstances
68

    
69
      
70
let print_static_link_instance fmt (i, (m, _)) =
71
 fprintf fmt "%a(%s)" pp_machine_static_link_name (node_name m) i
72

    
73
(* Allocation of a node struct:
74
   - if node memory is an array/matrix/etc, we cast it to a pointer (see pp_registers_struct)
75
*)
76
let print_static_link_macro fmt m =
77
  let array_mem = List.filter (fun v -> Types.is_array_type v.var_type) m.mmemory in
78
  fprintf fmt "@[<v>@[<v 2>#define %a(inst) do {\\@,%a%t%a;\\@]@,} while (0)@.@]"
79
    pp_machine_static_link_name m.mname.node_id
80
    (Utils.fprintf_list ~sep:";\\@,"
81
       (fun fmt v ->
82
	 fprintf fmt "inst._reg.%s = (%a*) &%s"
83
	   v.var_id
84
           (fun fmt v -> pp_c_type "" fmt (Types.array_base_type v.var_type)) v
85
	   v.var_id
86
       )) array_mem
87
    (Utils.pp_final_char_if_non_empty ";\\@," array_mem)
88
    (Utils.fprintf_list ~sep:";\\@,"
89
       (fun fmt (i',m') ->
90
	 let path = sprintf "inst ## _%s" i' in
91
	 fprintf fmt "%a;\\@,inst.%s = &%s"
92
	   print_static_link_instance (path,m')
93
	   i'
94
	   path
95
       )) m.minstances
96
      
97
let print_static_alloc_macro fmt m =
98
  fprintf fmt "@[<v>@[<v 2>#define %a(attr,%a%tinst)\\@,%a(attr,%a%tinst);\\@,%a(inst);@]@,@]@."
99
    pp_machine_static_alloc_name m.mname.node_id
100
    (Utils.fprintf_list ~sep:", " (pp_c_var_read m)) m.mstatic
101
    (Utils.pp_final_char_if_non_empty ", " m.mstatic)
102
    pp_machine_static_declare_name m.mname.node_id
103
    (Utils.fprintf_list ~sep:", " (pp_c_var_read m)) m.mstatic
104
    (Utils.pp_final_char_if_non_empty ", " m.mstatic)
105
    pp_machine_static_link_name m.mname.node_id
106

    
107
 
108
let print_machine_decl fmt m =
109
  Mod.print_machine_decl_prefix fmt m;
110
  if fst (get_stateless_status m) then
111
    begin
112
      fprintf fmt "extern %a;@.@."
113
	print_stateless_prototype
114
	(m.mname.node_id, m.mstep.step_inputs, m.mstep.step_outputs)
115
    end
116
  else
117
    begin
118
      (* Static allocation *)
119
      if !Options.static_mem
120
      then (
121
	fprintf fmt "%a@.%a@.%a@."
122
	  print_static_declare_macro m
123
	  print_static_link_macro m
124
	  print_static_alloc_macro m
125
      )
126
      else ( 
127
        (* Dynamic allocation *)
128
	fprintf fmt "extern %a;@.@."
129
	  print_alloc_prototype (m.mname.node_id, m.mstatic)
130
      );
131
      let self = mk_self m in
132
      fprintf fmt "extern %a;@.@."
133
	(print_reset_prototype self) (m.mname.node_id, m.mstatic);
134

    
135
      fprintf fmt "extern %a;@.@."
136
	(print_step_prototype self)
137
	(m.mname.node_id, m.mstep.step_inputs, m.mstep.step_outputs)
138
    end
139

    
140
let print_machine_alloc_decl fmt m =
141
  Mod.print_machine_decl_prefix fmt m;
142
  if fst (get_stateless_status m) then
143
    begin
144
    end
145
  else
146
    begin
147
      if !Options.static_mem
148
      then
149
	begin
150
	  (* Static allocation *)
151
	  fprintf fmt "%a@.%a@.%a@."
152
		  print_static_declare_macro m
153
		  print_static_link_macro m
154
		  print_static_alloc_macro m
155
	end
156
      else
157
	begin 
158
          (* Dynamic allocation *)
159
	  fprintf fmt "extern %a;@."
160
		  print_alloc_prototype (m.mname.node_id, m.mstatic)
161
	end
162
    end
163

    
164
let print_machine_decl_from_header fmt inode =
165
  (*Mod.print_machine_decl_prefix fmt m;*)
166
  if inode.nodei_prototype = Some "C" then
167
    if inode.nodei_stateless then
168
      begin
169
	fprintf fmt "extern %a;@.@."
170
	  print_stateless_C_prototype
171
	  (inode.nodei_id, inode.nodei_inputs, inode.nodei_outputs)
172
      end
173
    else (
174
      raise (Invalid_argument ("A node with declared prototype C cannot be stateful, it has to be a function")))
175
  else
176
    if inode.nodei_stateless then
177
    begin
178
      fprintf fmt "extern %a;@.@."
179
	print_stateless_prototype 
180
	(inode.nodei_id, inode.nodei_inputs, inode.nodei_outputs)
181
    end
182
    else 
183
      begin
184
	let static_inputs = List.filter (fun v -> v.var_dec_const) inode.nodei_inputs in
185
	let used name =
186
	  (List.exists (fun v -> v.var_id = name) inode.nodei_inputs)
187
	  || (List.exists (fun v -> v.var_id = name) inode.nodei_outputs) in
188
	let self = mk_new_name used "self" in
189
	fprintf fmt "extern %a;@.@."
190
	  (print_reset_prototype self) (inode.nodei_id, static_inputs);
191
	
192
	fprintf fmt "extern %a;@.@."
193
	  (print_step_prototype self)
194
	  (inode.nodei_id, inode.nodei_inputs, inode.nodei_outputs)
195
      end
196

    
197
let print_const_decl fmt cdecl =
198
  fprintf fmt "extern %a;@." 
199
    (pp_c_type cdecl.const_id) cdecl.const_type
200

    
201
let rec pp_c_struct_type_field filename cpt fmt (label, tdesc) =
202
  fprintf fmt "%a;" (pp_c_type_decl filename cpt label) tdesc
203
and pp_c_type_decl filename cpt var fmt tdecl =
204
  match tdecl with
205
  | Tydec_any           -> assert false
206
  | Tydec_int           -> fprintf fmt "int %s" var
207
  | Tydec_real          -> fprintf fmt "double %s" var
208
  | Tydec_float         -> fprintf fmt "float %s" var
209
  | Tydec_bool          -> fprintf fmt "_Bool %s" var
210
  | Tydec_clock ty      -> pp_c_type_decl filename cpt var fmt ty
211
  | Tydec_const c       -> fprintf fmt "%s %s" c var
212
  | Tydec_array (d, ty) -> fprintf fmt "%a[%a]" (pp_c_type_decl filename cpt var) ty pp_c_dimension d
213
  | Tydec_enum tl ->
214
    begin
215
      incr cpt;
216
      fprintf fmt "enum _enum_%s_%d { %a } %s" filename !cpt (Utils.fprintf_list ~sep:", " pp_print_string) tl var
217
    end
218
  | Tydec_struct fl ->
219
    begin
220
      incr cpt;
221
      fprintf fmt "struct _struct_%s_%d { %a } %s" filename !cpt (Utils.fprintf_list ~sep:" " (pp_c_struct_type_field filename cpt)) fl var
222
    end
223

    
224
let print_type_definitions fmt filename =
225
  let cpt_type = ref 0 in
226
  Hashtbl.iter (fun typ decl ->
227
		match typ with
228
		| Tydec_const var ->
229
		   (match decl.top_decl_desc with
230
		    | TypeDef tdef ->
231
		       fprintf fmt "typedef %a;@.@."
232
			       (pp_c_type_decl filename cpt_type var) tdef.tydef_desc
233
		    | _ -> assert false)
234
		| _        -> ()) type_table
235

    
236
let reset_type_definitions, print_type_definition_from_header =
237
  let cpt_type =ref 0 in
238
  ((fun () -> cpt_type := 0),
239
   (fun fmt typ filename ->
240
    fprintf fmt "typedef %a;@.@."
241
	(pp_c_type_decl filename cpt_type typ.tydef_id) typ.tydef_desc))
242

    
243
(********************************************************************************************)
244
(*                         MAIN Header Printing functions                                   *)
245
(********************************************************************************************)
246
let print_header header_fmt basename prog machines dependencies =
247
  (* Include once: start *)
248
  let baseNAME = String.uppercase basename in
249
  let baseNAME = Str.global_replace (Str.regexp "\\.\\|\\ ") "_" baseNAME in
250
  begin
251
    (* Print the svn version number and the supported C standard (C90 or C99) *)
252
    print_version header_fmt;
253
    fprintf header_fmt "#ifndef _%s@.#define _%s@." baseNAME baseNAME;
254
    pp_print_newline header_fmt ();
255
    fprintf header_fmt "/* Imports standard library */@.";
256
    (* imports standard library definitions (arrow) *)
257
    print_import_standard header_fmt;
258
    pp_print_newline header_fmt ();
259
    (* imports dependencies *)
260
    fprintf header_fmt "/* Import Dependencies */@.";
261
    fprintf header_fmt "@[<v>";
262
    List.iter (print_import_prototype header_fmt) dependencies;
263
    fprintf header_fmt "@]@.";
264
    fprintf header_fmt "/* Types definitions */@.";
265
    (* Print the type definitions from the type table *)
266
    print_type_definitions header_fmt basename;
267
    pp_print_newline header_fmt ();
268
    (* Print the global constant declarations. *)
269
    fprintf header_fmt "/* Global constant (declarations, definitions are in C file) */@.";
270
    List.iter (fun c -> print_const_decl header_fmt (const_of_top c)) (get_consts prog);
271
    pp_print_newline header_fmt ();
272
    (* Print the struct declarations of all machines. *)
273
    fprintf header_fmt "/* Struct declarations */@.";
274
    List.iter (print_machine_struct header_fmt) machines;
275
    pp_print_newline header_fmt ();
276
    (* Print the prototypes of all machines *)
277
    fprintf header_fmt "/* Nodes declarations */@.";
278
    List.iter (print_machine_decl header_fmt) machines;
279
    pp_print_newline header_fmt ();
280
    (* Include once: end *)
281
    fprintf header_fmt "#endif@.";
282
    pp_print_newline header_fmt ()
283
  end
284

    
285
let print_alloc_header header_fmt basename prog machines dependencies =
286
  (* Include once: start *)
287
  let baseNAME = String.uppercase basename in
288
  let baseNAME = Str.global_replace (Str.regexp "\\.\\|\\ ") "_" baseNAME in
289
  begin
290
    (* Print the svn version number and the supported C standard (C90 or C99) *)
291
    print_version header_fmt;
292
    fprintf header_fmt "#ifndef _%s_alloc@.#define _%s_alloc@." baseNAME baseNAME;
293
    pp_print_newline header_fmt ();
294
    (* Import the header *)
295
    fprintf header_fmt "/* Import header from %s */@." basename;
296
    fprintf header_fmt "@[<v>";
297
    print_import_prototype header_fmt (Dep (true, basename, [], true (* assuming it is staful *) ));
298
    fprintf header_fmt "@]@.";
299
    fprintf header_fmt "/* Import dependencies */@.";
300
    fprintf header_fmt "@[<v>";
301
    List.iter (print_import_alloc_prototype header_fmt) dependencies;
302
    fprintf header_fmt "@]@.";
303
    (* Print the struct definitions of all machines. *)
304
    fprintf header_fmt "/* Struct definitions */@.";
305
    List.iter (print_machine_struct header_fmt) machines;
306
    pp_print_newline header_fmt ();
307
    (* Print the prototypes of all machines *)
308
    fprintf header_fmt "/* Node allocation function/macro prototypes */@.";
309
    List.iter (print_machine_alloc_decl header_fmt) machines;
310
    pp_print_newline header_fmt ();
311
    (* Include once: end *)
312
    fprintf header_fmt "#endif@.";
313
    pp_print_newline header_fmt ()
314
  end
315

    
316
(* Function called when compiling a lusi file and generating the associated C
317
   header. *)
318
let print_header_from_header header_fmt basename header =
319
  (* Include once: start *)
320
  let baseNAME = String.uppercase basename in
321
  let baseNAME = Str.global_replace (Str.regexp "\\.\\|\\ ") "_" baseNAME in
322
  let types = get_typedefs header in
323
  let consts = get_consts header in
324
  let nodes = get_imported_nodes header in
325
  let dependencies = get_dependencies header in
326
  begin
327
    (* Print the svn version number and the supported C standard (C90 or C99) *)
328
    print_version header_fmt;
329
    fprintf header_fmt "#ifndef _%s@.#define _%s@." baseNAME baseNAME;
330
    pp_print_newline header_fmt ();
331
    fprintf header_fmt "/* Imports standard library */@.";
332
    (* imports standard library definitions (arrow) *)
333
    print_import_standard header_fmt;
334
    pp_print_newline header_fmt ();
335
    (* imports dependencies *)
336
    fprintf header_fmt "/* Import dependencies */@.";
337
    fprintf header_fmt "@[<v>";
338
    List.iter
339
      (fun dep -> 
340
	let (local, s) = dependency_of_top dep in 
341
	print_import_prototype header_fmt (Dep (local, s, [], true (* assuming it is stateful *))))
342
      dependencies;
343
    fprintf header_fmt "@]@.";
344
    fprintf header_fmt "/* Types definitions */@.";
345
    (* Print the type definitions from the type table *)
346
    reset_type_definitions ();
347
    List.iter (fun typ -> print_type_definition_from_header header_fmt (typedef_of_top typ) basename) types;
348
    pp_print_newline header_fmt ();
349
    (* Print the global constant declarations. *)
350
    fprintf header_fmt "/* Global constant (declarations, definitions are in C file) */@.";
351
    List.iter (fun c -> print_const_decl header_fmt (const_of_top c)) consts;
352
    pp_print_newline header_fmt ();
353
    (* Print the struct declarations of all machines. *)
354
    fprintf header_fmt "/* Struct declarations */@.";
355
    List.iter (fun node -> print_machine_struct_from_header header_fmt (imported_node_of_top node)) nodes;
356
    pp_print_newline header_fmt ();
357
    (* Print the prototypes of all machines *)
358
    fprintf header_fmt "/* Nodes declarations */@.";
359
    List.iter (fun node -> print_machine_decl_from_header header_fmt (imported_node_of_top node)) nodes;
360
    pp_print_newline header_fmt ();
361
    (* Include once: end *)
362
    fprintf header_fmt "#endif@.";
363
    pp_print_newline header_fmt ()
364
  end
365

    
366
end
367
(* Local Variables: *)
368
(* compile-command:"make -C ../../.." *)
369
(* End: *)
(3-3/7)