Project

General

Profile

Statistics
| Branch: | Tag: | Revision:

lustrec / src / backends / C / c_backend_main.ml @ 66359a5e

History | View | Annotate | Download (7.28 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
open Utils
18

    
19
module type MODIFIERS_MAINSRC =
20
sig
21
end
22

    
23
module EmptyMod =
24
struct
25
end
26

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

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

    
34

    
35
let print_put_outputs fmt m = 
36
  let po fmt (id, o', o) =
37
    let suff = string_of_int id in
38
    print_put_var fmt suff o'.var_id o.var_type o.var_id
39
  in
40
  List.iteri2 (fun idx v' v -> fprintf fmt "@ %a;" po ((idx+1), v', v)) m.mname.node_outputs m.mstep.step_outputs
41

    
42
let print_main_inout_declaration basename fmt m =
43
  let mname = m.mname.node_id in
44
  (* TODO: find a proper way to shorthen long names. This causes segfault in the binary when trying to fprintf in them *)
45
  let mname = if String.length mname > 50 then string_of_int (Hashtbl.hash mname) else mname in
46
  fprintf fmt "/* Declaration of inputs/outputs variables */@ ";
47
  List.iteri 
48
    (fun idx v ->
49
      fprintf fmt "%a;@ " (pp_c_type v.var_id) v.var_type;
50
      fprintf fmt "FILE *f_in%i;@ " (idx+1); (* we start from 1: in1, in2, ... *)
51
      fprintf fmt "f_in%i = fopen(\"%s_%s_simu.in%i\", \"w\");@ " (idx+1) basename mname (idx+1);
52
    ) m.mstep.step_inputs;
53
  List.iteri 
54
    (fun idx v ->
55
      fprintf fmt "%a;@ " (pp_c_type v.var_id) v.var_type;
56
      fprintf fmt "FILE *f_out%i;@ " (idx+1); (* we start from 1: in1, in2, ... *)
57
      fprintf fmt "f_out%i = fopen(\"%s_%s_simu.out%i\", \"w\");@ " (idx+1) basename mname (idx+1);
58
    ) m.mstep.step_outputs
59

    
60

    
61
  
62
let print_main_memory_allocation mname main_mem fmt m =
63
  if not (fst (get_stateless_status m)) then
64
  begin  
65
    fprintf fmt "@ /* Main memory allocation */@ ";
66
    if (!Options.static_mem && !Options.main_node <> "")
67
    then (fprintf fmt "%a(static,main_mem);@ " pp_machine_static_alloc_name mname)
68
    else (fprintf fmt "%a *main_mem = %a();@ " pp_machine_memtype_name mname pp_machine_alloc_name mname);
69
    fprintf fmt "@ /* Initialize the main memory */@ ";
70
    fprintf fmt "%a(%s);@ " pp_machine_reset_name mname main_mem;
71
  end
72

    
73
let print_global_initialize fmt basename =
74
  let mNAME = file_to_module_name basename in
75
  fprintf fmt "@ /* Initialize global constants */@ %a();@ "
76
    pp_global_init_name mNAME
77

    
78
let print_global_clear fmt basename =
79
  let mNAME = file_to_module_name basename in
80
  fprintf fmt "@ /* Clear global constants */@ %a();@ "
81
    pp_global_clear_name mNAME
82

    
83
let print_main_initialize mname main_mem fmt m =
84
  if not (fst (get_stateless_status m))
85
  then
86
    fprintf fmt "@ /* Initialize inputs, outputs and memories */@ %a%t%a%t%a(%s);@ "
87
      (Utils.fprintf_list ~sep:"@ " (pp_initialize m main_mem (pp_c_var_read m))) m.mstep.step_inputs
88
      (Utils.pp_newline_if_non_empty m.mstep.step_inputs)
89
      (Utils.fprintf_list ~sep:"@ " (pp_initialize m main_mem (pp_c_var_read m))) m.mstep.step_outputs
90
      (Utils.pp_newline_if_non_empty m.mstep.step_inputs)
91
      pp_machine_init_name mname
92
      main_mem
93
  else
94
    fprintf fmt "@ /* Initialize inputs and outputs */@ %a%t%a@ "
95
      (Utils.fprintf_list ~sep:"@ " (pp_initialize m main_mem (pp_c_var_read m))) m.mstep.step_inputs
96
      (Utils.pp_newline_if_non_empty m.mstep.step_inputs)
97
      (Utils.fprintf_list ~sep:"@ " (pp_initialize m main_mem (pp_c_var_read m))) m.mstep.step_outputs
98

    
99
let print_main_clear mname main_mem fmt m =
100
  if not (fst (get_stateless_status m))
101
  then
102
    fprintf fmt "@ /* Clear inputs, outputs and memories */@ %a%t%a%t%a(%s);@ "
103
      (Utils.fprintf_list ~sep:"@ " (pp_clear m main_mem (pp_c_var_read m))) m.mstep.step_inputs
104
      (Utils.pp_newline_if_non_empty m.mstep.step_inputs)
105
      (Utils.fprintf_list ~sep:"@ " (pp_clear m main_mem (pp_c_var_read m))) m.mstep.step_outputs
106
      (Utils.pp_newline_if_non_empty m.mstep.step_inputs)
107
      pp_machine_clear_name mname
108
      main_mem
109
  else
110
    fprintf fmt "@ /* Clear inputs and outputs */@ %a%t%a@ "
111
      (Utils.fprintf_list ~sep:"@ " (pp_clear m main_mem (pp_c_var_read m))) m.mstep.step_inputs
112
      (Utils.pp_newline_if_non_empty m.mstep.step_inputs)
113
      (Utils.fprintf_list ~sep:"@ " (pp_clear m main_mem (pp_c_var_read m))) m.mstep.step_outputs
114

    
115
let print_main_loop mname main_mem fmt m =
116
  let input_values =
117
    List.map (fun v -> mk_val (LocalVar v) v.var_type)
118
      m.mstep.step_inputs in
119
  begin
120
    fprintf fmt "@ ISATTY = isatty(0);@ ";
121
    fprintf fmt "@ /* Infinite loop */@ ";
122
    fprintf fmt "@[<v 2>while(1){@ ";
123
    fprintf fmt  "fflush(stdout);@ ";
124
    List.iteri (fun idx _ -> fprintf fmt "fflush(f_in%i);@ " (idx+1)) m.mstep.step_inputs;
125
    List.iteri (fun idx _ -> fprintf fmt "fflush(f_out%i);@ " (idx+1)) m.mstep.step_outputs;
126
    fprintf fmt "%a@ %t%a"
127
      print_get_inputs m
128
      (fun fmt -> pp_main_call mname main_mem fmt m input_values m.mstep.step_outputs)
129
      print_put_outputs m
130
  end
131

    
132
let print_main_code fmt basename m =
133
  let mname = m.mname.node_id in
134
  let main_mem =
135
    if (!Options.static_mem && !Options.main_node <> "")
136
    then "&main_mem"
137
    else "main_mem" in
138
  fprintf fmt "@[<v 2>int main (int argc, char *argv[]) {@ ";
139
  print_main_inout_declaration basename fmt m;
140
  Plugins.c_backend_main_loop_body_prefix basename mname fmt ();
141
  print_main_memory_allocation mname main_mem fmt m;
142
  if !Options.mpfr then
143
    begin
144
      print_global_initialize fmt basename;
145
      print_main_initialize mname main_mem fmt m;
146
    end;
147
  print_main_loop mname main_mem fmt m;
148

    
149
  Plugins.c_backend_main_loop_body_suffix fmt ();
150
  fprintf fmt "@]@ }@ @ ";
151
  if !Options.mpfr then
152
    begin
153
      print_main_clear mname main_mem fmt m;
154
      print_global_clear fmt basename;
155
    end;
156
  fprintf fmt "@ return 1;";
157
  fprintf fmt "@]@ }@."       
158

    
159
let print_main_header fmt =
160
  fprintf fmt (if !Options.cpp then "#include <stdio.h>@.#include <unistd.h>@.#include \"%s/io_frontend.hpp\"@." else "#include <stdio.h>@.#include <unistd.h>@.#include \"%s/io_frontend.h\"@.")
161
    (Options_management.core_dependency "io_frontend")
162

    
163
let print_main_c main_fmt main_machine basename prog machines _ (*dependencies*) =
164
  print_main_header main_fmt;
165
  fprintf main_fmt "#include <stdlib.h>@.#include <assert.h>@.";
166
  print_import_alloc_prototype main_fmt (Dep (true, basename, [], true (* assuming it is stateful*) ));
167
  pp_print_newline main_fmt ();
168

    
169
  (* Print the svn version number and the supported C standard (C90 or C99) *)
170
  print_version main_fmt;
171
  print_main_code main_fmt basename main_machine
172
end  
173

    
174
(* Local Variables: *)
175
(* compile-command:"make -C ../../.." *)
176
(* End: *)