Project

General

Profile

Revision 70e1006b src/backends/C/c_backend_header.ml

View differences:

src/backends/C/c_backend_header.ml
36 36
let print_import_standard fmt =
37 37
  fprintf fmt "#include \"%s/include/lustrec/arrow.h\"@.@." Version.prefix
38 38

  
39
    
40
let pp_registers_struct fmt m =
41
  if m.mmemory <> []
42
  then
43
    fprintf fmt "@[%a {@[%a; @]}@] _reg; "
44
      pp_machine_regtype_name m.mname.node_id
45
      (Utils.fprintf_list ~sep:"; " pp_c_decl_struct_var) m.mmemory
46
  else
47
    ()
48

  
49
let print_machine_struct fmt m =
50
  if fst (get_stateless_status m) then
51
    begin
52
    end
53
  else
54
    begin
55
      (* Define struct *)
56
      fprintf fmt "@[%a {@[%a%a%t@]};@]@."
57
	pp_machine_memtype_name m.mname.node_id
58
	pp_registers_struct m
59
	(Utils.fprintf_list ~sep:"; " pp_c_decl_instance_var) m.minstances
60
	(Utils.pp_final_char_if_non_empty "; " m.minstances)
61
    end
62

  
63 39
let print_static_declare_instance attr fmt (i, (m, static)) =
64 40
  fprintf fmt "%a(%s, %a%t%s)"
65 41
    pp_machine_static_declare_name (node_name m)
......
161 137
	(m.mname.node_id, m.mstep.step_inputs, m.mstep.step_outputs)
162 138
    end
163 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_stateless then
167
    begin
168
      fprintf fmt "extern %a;@.@."
169
	print_stateless_prototype
170
	(inode.nodei_id, inode.nodei_inputs, inode.nodei_outputs)
171
    end
172
  else
173
    begin
174
      let static_inputs = List.filter (fun v -> v.var_dec_const) inode.nodei_inputs in
175
      let self = mk_new_name (inode.nodei_inputs@inode.nodei_outputs) "self" in
176
      fprintf fmt "extern %a;@.@."
177
	(print_reset_prototype self) (inode.nodei_id, static_inputs);
178

  
179
      fprintf fmt "extern %a;@.@."
180
	(print_step_prototype self)
181
	(inode.nodei_id, inode.nodei_inputs, inode.nodei_outputs)
182
    end
183

  
164 184
let print_const_decl fmt cdecl =
165 185
  fprintf fmt "extern %a;@." 
166 186
    (pp_c_type cdecl.const_id) cdecl.const_type
167 187

  
168
(********************************************************************************************)
169
(*                      Struct/TypeDef Printing functions                                   *)
170
(********************************************************************************************)
171

  
172

  
173 188
let rec pp_c_struct_type_field filename cpt fmt (label, tdesc) =
174 189
  fprintf fmt "%a;" (pp_c_type_decl filename cpt label) tdesc
175 190
and pp_c_type_decl filename cpt var fmt tdecl =
......
195 210

  
196 211
let print_type_definitions fmt filename =
197 212
  let cpt_type = ref 0 in
198
  Hashtbl.iter (fun typ def ->
199
    match typ with
200
    | Tydec_const var ->
201
      fprintf fmt "typedef %a;@.@."
202
	(pp_c_type_decl filename cpt_type var) def
203
    | _        -> ()) type_table
213
  Hashtbl.iter (fun typ decl ->
214
		match typ with
215
		| Tydec_const var ->
216
		   (match decl.top_decl_desc with
217
		    | TypeDef tdef ->
218
		       fprintf fmt "typedef %a;@.@."
219
			       (pp_c_type_decl filename cpt_type var) tdef.tydef_desc
220
		    | _ -> assert false)
221
		| _        -> ()) type_table
222

  
223
let reset_type_definitions, print_type_definition_from_header =
224
  let cpt_type =ref 0 in
225
  ((fun () -> cpt_type := 0),
226
   (fun fmt typ filename ->
227
    fprintf fmt "typedef %a;@.@."
228
	(pp_c_type_decl filename cpt_type typ.tydef_id) typ.tydef_desc))
204 229

  
205 230
(********************************************************************************************)
206 231
(*                         MAIN Header Printing functions                                   *)
207 232
(********************************************************************************************)
208
let print_header header_fmt basename prog machines =
233
let print_header header_fmt basename prog machines dependencies =
209 234
  (* Include once: start *)
210 235
  let baseNAME = String.uppercase basename in
211 236
  let baseNAME = Str.global_replace (Str.regexp "\\.\\|\\ ") "_" baseNAME in
212
  (* Print the svn version number and the supported C standard (C90 or C99) *)
213
  print_version header_fmt;
214
  fprintf header_fmt "#ifndef _%s@.#define _%s@." baseNAME baseNAME;
215
  pp_print_newline header_fmt ();
216
  fprintf header_fmt "/* Imports standard library */@.";
217
  (* imports standard library definitions (arrow) *)
218
  print_import_standard header_fmt;
219
  pp_print_newline header_fmt ();
220
  fprintf header_fmt "/* Types definitions */@.";
221
  (* Print the type definitions from the type table *)
222
  print_type_definitions header_fmt basename;
223
  pp_print_newline header_fmt ();
224
  (* Print the global constant declarations. *)
225
  fprintf header_fmt "/* Global constant (declarations, definitions are in C file) */@.";
226
  List.iter (fun c -> print_const_decl header_fmt c) (get_consts prog);
227
  pp_print_newline header_fmt ();
228
  (* Print the struct declarations of all machines. *)
229
  fprintf header_fmt "/* Struct declarations */@.";
230
  List.iter (print_machine_struct header_fmt) machines;
231
  pp_print_newline header_fmt ();
232
  (* Print the prototypes of all machines *)
233
  fprintf header_fmt "/* Nodes declarations */@.";
234
  List.iter (print_machine_decl header_fmt) machines;
235
  pp_print_newline header_fmt ();
236
  (* Include once: end *)
237
  fprintf header_fmt "#endif@.";
238
  pp_print_newline header_fmt ()
239
end
237
  begin
238
    (* Print the svn version number and the supported C standard (C90 or C99) *)
239
    print_version header_fmt;
240
    fprintf header_fmt "#ifndef _%s@.#define _%s@." baseNAME baseNAME;
241
    pp_print_newline header_fmt ();
242
    fprintf header_fmt "/* Imports standard library */@.";
243
    (* imports standard library definitions (arrow) *)
244
    print_import_standard header_fmt;
245
    pp_print_newline header_fmt ();
246
    (* imports dependencies *)
247
    fprintf header_fmt "/* Import Dependencies */@.";
248
    fprintf header_fmt "@[<v>";
249
    List.iter (print_import_prototype header_fmt) dependencies;
250
    fprintf header_fmt "@]@.";
251
    fprintf header_fmt "/* Types definitions */@.";
252
    (* Print the type definitions from the type table *)
253
    print_type_definitions header_fmt basename;
254
    pp_print_newline header_fmt ();
255
    (* Print the global constant declarations. *)
256
    fprintf header_fmt "/* Global constant (declarations, definitions are in C file) */@.";
257
    List.iter (fun c -> print_const_decl header_fmt (const_of_top c)) (get_consts prog);
258
    pp_print_newline header_fmt ();
259
    (* Print the struct declarations of all machines. *)
260
    fprintf header_fmt "/* Struct declarations */@.";
261
    List.iter (print_machine_struct header_fmt) machines;
262
    pp_print_newline header_fmt ();
263
    (* Print the prototypes of all machines *)
264
    fprintf header_fmt "/* Nodes declarations */@.";
265
    List.iter (print_machine_decl header_fmt) machines;
266
    pp_print_newline header_fmt ();
267
    (* Include once: end *)
268
    fprintf header_fmt "#endif@.";
269
    pp_print_newline header_fmt ()
270
  end
271

  
272
let print_alloc_header header_fmt basename prog machines dependencies =
273
  (* Include once: start *)
274
  let baseNAME = String.uppercase basename in
275
  let baseNAME = Str.global_replace (Str.regexp "\\.\\|\\ ") "_" baseNAME in
276
  begin
277
    (* Print the svn version number and the supported C standard (C90 or C99) *)
278
    print_version header_fmt;
279
    fprintf header_fmt "#ifndef _%s_alloc@.#define _%s_alloc@." baseNAME baseNAME;
280
    pp_print_newline header_fmt ();
281
    (* Import the header *)
282
    fprintf header_fmt "/* Import header from %s */@." basename;
283
    fprintf header_fmt "@[<v>";
284
    print_import_prototype header_fmt (true, basename, []);
285
    fprintf header_fmt "@]@.";
286
    fprintf header_fmt "/* Import dependencies */@.";
287
    fprintf header_fmt "@[<v>";
288
    List.iter (print_import_alloc_prototype header_fmt) dependencies;
289
    fprintf header_fmt "@]@.";
290
    (* Print the struct definitions of all machines. *)
291
    fprintf header_fmt "/* Struct definitions */@.";
292
    List.iter (print_machine_struct header_fmt) machines;
293
    pp_print_newline header_fmt ();
294
    (* Print the prototypes of all machines *)
295
    fprintf header_fmt "/* Node allocation function/macro prototypes */@.";
296
    List.iter (print_machine_alloc_decl header_fmt) machines;
297
    pp_print_newline header_fmt ();
298
    (* Include once: end *)
299
    fprintf header_fmt "#endif@.";
300
    pp_print_newline header_fmt ()
301
  end
240 302

  
303
let print_header_from_header header_fmt basename header =
304
  (* Include once: start *)
305
  let baseNAME = String.uppercase basename in
306
  let baseNAME = Str.global_replace (Str.regexp "\\.\\|\\ ") "_" baseNAME in
307
  let types = get_typedefs header in
308
  let consts = get_consts header in
309
  let nodes = get_imported_nodes header in
310
  let dependencies = get_dependencies header in
311
  begin
312
    (* Print the svn version number and the supported C standard (C90 or C99) *)
313
    print_version header_fmt;
314
    fprintf header_fmt "#ifndef _%s@.#define _%s@." baseNAME baseNAME;
315
    pp_print_newline header_fmt ();
316
    fprintf header_fmt "/* Imports standard library */@.";
317
    (* imports standard library definitions (arrow) *)
318
    print_import_standard header_fmt;
319
    pp_print_newline header_fmt ();
320
    (* imports dependencies *)
321
    fprintf header_fmt "/* Import dependencies */@.";
322
    fprintf header_fmt "@[<v>";
323
    List.iter
324
      (fun dep -> let (local, s) = dependency_of_top dep in print_import_prototype header_fmt (local, s, []))
325
      dependencies;
326
    fprintf header_fmt "@]@.";
327
    fprintf header_fmt "/* Types definitions */@.";
328
    (* Print the type definitions from the type table *)
329
    reset_type_definitions ();
330
    List.iter (fun typ -> print_type_definition_from_header header_fmt (typedef_of_top typ) basename) types;
331
    pp_print_newline header_fmt ();
332
    (* Print the global constant declarations. *)
333
    fprintf header_fmt "/* Global constant (declarations, definitions are in C file) */@.";
334
    List.iter (fun c -> print_const_decl header_fmt (const_of_top c)) consts;
335
    pp_print_newline header_fmt ();
336
    (* Print the struct declarations of all machines. *)
337
    fprintf header_fmt "/* Struct declarations */@.";
338
    List.iter (fun node -> print_machine_struct_from_header header_fmt (imported_node_of_top node)) nodes;
339
    pp_print_newline header_fmt ();
340
    (* Print the prototypes of all machines *)
341
    fprintf header_fmt "/* Nodes declarations */@.";
342
    List.iter (fun node -> print_machine_decl_from_header header_fmt (imported_node_of_top node)) nodes;
343
    pp_print_newline header_fmt ();
344
    (* Include once: end *)
345
    fprintf header_fmt "#endif@.";
346
    pp_print_newline header_fmt ()
347
  end
348

  
349
end
241 350
(* Local Variables: *)
242 351
(* compile-command:"make -C ../../.." *)
243 352
(* End: *)

Also available in: Unified diff