Project

General

Profile

Statistics
| Branch: | Tag: | Revision:

lustrec / src / backends / C / c_backend_main.ml @ ef34b4ae

History | View | Annotate | Download (4.94 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 LustreSpec
13
open Corelang
14
open Machine_code
15
open Format
16
open C_backend_common
17

    
18
module type MODIFIERS_MAINSRC =
19
sig
20
end
21

    
22
module EmptyMod =
23
struct
24
end
25

    
26
module Main = functor (Mod: MODIFIERS_MAINSRC) -> 
27
struct
28

    
29
(********************************************************************************************)
30
(*                         Main related functions                                           *)
31
(********************************************************************************************)
32

    
33
let print_get_input fmt v =
34
  match v.var_type.Types.tdesc with
35
    | Types.Tint -> fprintf fmt "_get_int(\"%s\")" v.var_id
36
    | Types.Tbool -> fprintf fmt "_get_bool(\"%s\")" v.var_id
37
    | Types.Treal -> fprintf fmt "_get_double(\"%s\")" v.var_id
38
    | _ -> assert false
39

    
40
let print_put_outputs fmt ol = 
41
  let po fmt o =
42
    match o.var_type.Types.tdesc with
43
    | Types.Tint -> fprintf fmt "_put_int(\"%s\", %s)" o.var_id o.var_id
44
    | Types.Tbool -> fprintf fmt "_put_bool(\"%s\", %s)" o.var_id o.var_id
45
    | Types.Treal -> fprintf fmt "_put_double(\"%s\", %s)" o.var_id o.var_id
46
    | _ -> assert false
47
  in
48
  List.iter (fprintf fmt "@ %a;" po) ol
49

    
50
let print_main_fun machines m fmt =
51
  let mname = m.mname.node_id in
52
  let main_mem =
53
    if (!Options.static_mem && !Options.main_node <> "")
54
    then "&main_mem"
55
    else "main_mem" in
56
  fprintf fmt "@[<v 2>int main (int argc, char *argv[]) {@ ";
57
  fprintf fmt "/* Declaration of inputs/outputs variables */@ ";
58
  List.iter 
59
    (fun v -> fprintf fmt "%a = %a;@ " (pp_c_type v.var_id) v.var_type pp_c_initialize v.var_type
60
    ) m.mstep.step_inputs;
61
  List.iter 
62
    (fun v -> fprintf fmt "%a = %a;@ " (pp_c_type v.var_id) v.var_type pp_c_initialize v.var_type
63
    ) m.mstep.step_outputs;
64
  fprintf fmt "@ /* Main memory allocation */@ ";
65
  if (!Options.static_mem && !Options.main_node <> "")
66
  then (fprintf fmt "%a(static,main_mem);@ " pp_machine_static_alloc_name mname)
67
  else (fprintf fmt "%a *main_mem = %a();@ " pp_machine_memtype_name mname pp_machine_alloc_name mname);
68
  fprintf fmt "@ /* Initialize the main memory */@ ";
69
  fprintf fmt "%a(%s);@ " pp_machine_reset_name mname main_mem;
70
  fprintf fmt "@ ISATTY = isatty(0);@ ";
71
  fprintf fmt "@ /* Infinite loop */@ ";
72
  fprintf fmt "@[<v 2>while(1){@ ";
73
  fprintf fmt  "fflush(stdout);@ ";
74
  List.iter 
75
    (fun v -> fprintf fmt "%s = %a;@ "
76
      v.var_id
77
      print_get_input v
78
    ) m.mstep.step_inputs;
79
  (match m.mstep.step_outputs with
80
    (* | [] -> ( *)
81
    (*   fprintf fmt "%a(%a%t%s);@ "  *)
82
    (* 	pp_machine_step_name mname *)
83
    (* 	(Utils.fprintf_list ~sep:", " (fun fmt v -> pp_print_string fmt v.var_id)) m.mstep.step_inputs *)
84
    (* 	(pp_final_char_if_non_empty ", " m.mstep.step_inputs) *)
85
    (* 	main_mem *)
86
    (* ) *)
87
    (* | [o] -> ( *)
88
    (*   fprintf fmt "%s = %a(%a%t%a, %s);%a" *)
89
    (* 	o.var_id *)
90
    (* 	pp_machine_step_name mname *)
91
    (* 	(Utils.fprintf_list ~sep:", " (fun fmt v -> pp_print_string fmt v.var_id)) m.mstep.step_inputs *)
92
    (* 	(pp_final_char_if_non_empty ", " m.mstep.step_inputs) *)
93
    (* 	(Utils.fprintf_list ~sep:", " (fun fmt v -> fprintf fmt "&%s" v.var_id)) m.mstep.step_outputs *)
94
    (* 	main_mem *)
95
    (* 	print_put_outputs [o]) *)
96
    | _ -> (
97
      fprintf fmt "%a(%a%t%a, %s);%a"
98
	pp_machine_step_name mname
99
	(Utils.fprintf_list ~sep:", " (fun fmt v -> pp_print_string fmt v.var_id)) m.mstep.step_inputs
100
	(Utils.pp_final_char_if_non_empty ", " m.mstep.step_inputs)
101
	(Utils.fprintf_list ~sep:", " (fun fmt v -> fprintf fmt "&%s" v.var_id)) m.mstep.step_outputs
102
	main_mem
103
	print_put_outputs m.mstep.step_outputs)
104
  );
105
  fprintf fmt "@]@ }@ ";
106
  fprintf fmt "return 1;";
107
  fprintf fmt "@]@ }@."       
108

    
109
let print_main_header fmt =
110
  fprintf fmt "#include <stdio.h>@.#include <unistd.h>@.#include \"%s/include/lustrec/io_frontend.h\"@." Version.prefix
111

    
112

    
113
let print_main_c main_fmt main_machine basename prog machines dependencies =
114
  print_main_header main_fmt;
115
  fprintf main_fmt "#include <stdlib.h>@.#include <assert.h>@.";
116
  print_import_alloc_prototype main_fmt (true, basename, []);
117
  pp_print_newline main_fmt ();
118

    
119
  (* Print the svn version number and the supported C standard (C90 or C99) *)
120
  print_version main_fmt;
121
  print_main_fun machines main_machine main_fmt
122
end  
123

    
124
(* Local Variables: *)
125
(* compile-command:"make -C ../../.." *)
126
(* End: *)