Project

General

Profile

Statistics
| Branch: | Tag: | Revision:

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

History | View | Annotate | Download (3.7 KB)

1
open LustreSpec
2
open Corelang
3
open Machine_code
4
open Format
5
open C_backend_common
6

    
7
(********************************************************************************************)
8
(*                         Main related functions                                           *)
9
(********************************************************************************************)
10

    
11
let print_get_input fmt v =
12
  match v.var_type.Types.tdesc with
13
    | Types.Tint -> fprintf fmt "_get_int(\"%s\")" v.var_id
14
    | Types.Tbool -> fprintf fmt "_get_bool(\"%s\")" v.var_id
15
    | Types.Treal -> fprintf fmt "_get_double(\"%s\")" v.var_id
16
    | _ -> assert false
17

    
18
let print_put_outputs fmt ol = 
19
  let po fmt o =
20
    match o.var_type.Types.tdesc with
21
    | Types.Tint -> fprintf fmt "_put_int(\"%s\", %s)" o.var_id o.var_id
22
    | Types.Tbool -> fprintf fmt "_put_bool(\"%s\", %s)" o.var_id o.var_id
23
    | Types.Treal -> fprintf fmt "_put_double(\"%s\", %s)" o.var_id o.var_id
24
    | _ -> assert false
25
  in
26
  List.iter (fprintf fmt "@ %a;" po) ol
27

    
28
let print_main_fun machines m fmt =
29
  let mname = m.mname.node_id in
30
  let main_mem =
31
    if (!Options.static_mem && !Options.main_node <> "")
32
    then "&main_mem"
33
    else "main_mem" in
34
  fprintf fmt "@[<v 2>int main (int argc, char *argv[]) {@ ";
35
  fprintf fmt "/* Declaration of inputs/outputs variables */@ ";
36
  List.iter 
37
    (fun v -> fprintf fmt "%a = %a;@ " (pp_c_type v.var_id) v.var_type pp_c_initialize v.var_type
38
    ) m.mstep.step_inputs;
39
  List.iter 
40
    (fun v -> fprintf fmt "%a = %a;@ " (pp_c_type v.var_id) v.var_type pp_c_initialize v.var_type
41
    ) m.mstep.step_outputs;
42
  fprintf fmt "@ /* Main memory allocation */@ ";
43
  if (!Options.static_mem && !Options.main_node <> "")
44
  then (fprintf fmt "%a(static,main_mem);@ " pp_machine_static_alloc_name mname)
45
  else (fprintf fmt "%a *main_mem = %a();@ " pp_machine_memtype_name mname pp_machine_alloc_name mname);
46
  fprintf fmt "@ /* Initialize the main memory */@ ";
47
  fprintf fmt "%a(%s);@ " pp_machine_reset_name mname main_mem;
48
  fprintf fmt "@ ISATTY = isatty(0);@ ";
49
  fprintf fmt "@ /* Infinite loop */@ ";
50
  fprintf fmt "@[<v 2>while(1){@ ";
51
  fprintf fmt  "fflush(stdout);@ ";
52
  List.iter 
53
    (fun v -> fprintf fmt "%s = %a;@ "
54
      v.var_id
55
      print_get_input v
56
    ) m.mstep.step_inputs;
57
  (match m.mstep.step_outputs with
58
    (* | [] -> ( *)
59
    (*   fprintf fmt "%a(%a%t%s);@ "  *)
60
    (* 	pp_machine_step_name mname *)
61
    (* 	(Utils.fprintf_list ~sep:", " (fun fmt v -> pp_print_string fmt v.var_id)) m.mstep.step_inputs *)
62
    (* 	(pp_final_char_if_non_empty ", " m.mstep.step_inputs) *)
63
    (* 	main_mem *)
64
    (* ) *)
65
    (* | [o] -> ( *)
66
    (*   fprintf fmt "%s = %a(%a%t%a, %s);%a" *)
67
    (* 	o.var_id *)
68
    (* 	pp_machine_step_name mname *)
69
    (* 	(Utils.fprintf_list ~sep:", " (fun fmt v -> pp_print_string fmt v.var_id)) m.mstep.step_inputs *)
70
    (* 	(pp_final_char_if_non_empty ", " m.mstep.step_inputs) *)
71
    (* 	(Utils.fprintf_list ~sep:", " (fun fmt v -> fprintf fmt "&%s" v.var_id)) m.mstep.step_outputs *)
72
    (* 	main_mem *)
73
    (* 	print_put_outputs [o]) *)
74
    | _ -> (
75
      fprintf fmt "%a(%a%t%a, %s);%a"
76
	pp_machine_step_name mname
77
	(Utils.fprintf_list ~sep:", " (fun fmt v -> pp_print_string fmt v.var_id)) m.mstep.step_inputs
78
	(Utils.pp_final_char_if_non_empty ", " m.mstep.step_inputs)
79
	(Utils.fprintf_list ~sep:", " (fun fmt v -> fprintf fmt "&%s" v.var_id)) m.mstep.step_outputs
80
	main_mem
81
	print_put_outputs m.mstep.step_outputs)
82
  );
83
  fprintf fmt "@]@ }@ ";
84
  fprintf fmt "return 1;";
85
  fprintf fmt "@]@ }@."       
86

    
87
let print_main_header fmt =
88
  fprintf fmt "#include <stdio.h>@.#include <unistd.h>@.#include \"%s/include/lustrec/io_frontend.h\"@." Version.prefix
89

    
90

    
91

    
92
(* Local Variables: *)
93
(* compile-command:"make -C ../../.." *)
94
(* End: *)