Project

General

Profile

Download (7.72 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 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
let print_get_inputs fmt m =
35
  let pi fmt (id, v', v) =
36
  match (Types.unclock_type v.var_type).Types.tdesc with
37
    | Types.Tint -> fprintf fmt "%s = _get_int(f_in%i, \"%s\")" v.var_id id v'.var_id
38
    | Types.Tbool -> fprintf fmt "%s = _get_bool(f_in%i, \"%s\")" v.var_id id v'.var_id
39
    | Types.Treal when !Options.mpfr -> fprintf fmt "mpfr_set_d(%s, _get_double(f_in%i, \"%s\"), %i)" v.var_id id v'.var_id (Mpfr.mpfr_prec ())
40
    | Types.Treal -> fprintf fmt "%s = _get_double(f_in%i, \"%s\")" v.var_id id v'.var_id
41
    | _ ->
42
      begin
43
	Global.main_node := !Options.main_node;
44
	Format.eprintf "Code generation error: %a%a@."
45
	  pp_error Main_wrong_kind
46
	  Location.pp_loc v'.var_loc;
47
	raise (Error (v'.var_loc, Main_wrong_kind))
48
      end
49
  in
50
  List.iteri2 (fun idx v' v ->
51
    fprintf fmt "@ %a;" pi ((idx+1), v', v);
52
  ) m.mname.node_inputs m.mstep.step_inputs
53

    
54
    
55
let print_put_outputs fmt m = 
56
  let po fmt (id, o', o) =
57
    let suff = string_of_int id in
58
    print_put_var fmt suff o'.var_id o.var_type o.var_id
59
  in
60
  Utils.List.iteri2 (fun idx v' v -> fprintf fmt "@ %a;" po ((idx+1), v', v)) m.mname.node_outputs m.mstep.step_outputs
61

    
62
let print_main_inout_declaration basename fmt m =
63
  let mname = m.mname.node_id in
64
  fprintf fmt "/* Declaration of inputs/outputs variables */@ ";
65
  List.iteri 
66
    (fun idx v ->
67
      fprintf fmt "%a;@ " (pp_c_type v.var_id) v.var_type;
68
      fprintf fmt "FILE *f_in%i;@ " (idx+1); (* we start from 1: in1, in2, ... *)
69
      fprintf fmt "f_in%i = fopen(\"%s_%s_simu.in%i\", \"w\");@ " (idx+1) basename mname (idx+1);
70
    ) m.mstep.step_inputs;
71
  List.iteri 
72
    (fun idx v ->
73
      fprintf fmt "%a;@ " (pp_c_type v.var_id) v.var_type;
74
      fprintf fmt "FILE *f_out%i;@ " (idx+1); (* we start from 1: in1, in2, ... *)
75
      fprintf fmt "f_out%i = fopen(\"%s_%s_simu.out%i\", \"w\");@ " (idx+1) basename mname (idx+1);
76
    ) m.mstep.step_outputs
77

    
78

    
79
  
80
let print_main_memory_allocation mname main_mem fmt m =
81
  if not (fst (get_stateless_status m)) then
82
  begin  
83
    fprintf fmt "@ /* Main memory allocation */@ ";
84
    if (!Options.static_mem && !Options.main_node <> "")
85
    then (fprintf fmt "%a(static,main_mem);@ " pp_machine_static_alloc_name mname)
86
    else (fprintf fmt "%a *main_mem = %a();@ " pp_machine_memtype_name mname pp_machine_alloc_name mname);
87
    fprintf fmt "@ /* Initialize the main memory */@ ";
88
    fprintf fmt "%a(%s);@ " pp_machine_reset_name mname main_mem;
89
  end
90

    
91
let print_global_initialize fmt basename =
92
  let mNAME = file_to_module_name basename in
93
  fprintf fmt "@ /* Initialize global constants */@ %a();@ "
94
    pp_global_init_name mNAME
95

    
96
let print_global_clear fmt basename =
97
  let mNAME = file_to_module_name basename in
98
  fprintf fmt "@ /* Clear global constants */@ %a();@ "
99
    pp_global_clear_name mNAME
100

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

    
117
let print_main_clear mname main_mem fmt m =
118
  if not (fst (get_stateless_status m))
119
  then
120
    fprintf fmt "@ /* Clear inputs, outputs and memories */@ %a%t%a%t%a(%s);@ "
121
      (Utils.fprintf_list ~sep:"@ " (pp_clear m main_mem (pp_c_var_read m))) m.mstep.step_inputs
122
      (Utils.pp_newline_if_non_empty m.mstep.step_inputs)
123
      (Utils.fprintf_list ~sep:"@ " (pp_clear m main_mem (pp_c_var_read m))) m.mstep.step_outputs
124
      (Utils.pp_newline_if_non_empty m.mstep.step_inputs)
125
      pp_machine_clear_name mname
126
      main_mem
127
  else
128
    fprintf fmt "@ /* Clear inputs and outputs */@ %a%t%a@ "
129
      (Utils.fprintf_list ~sep:"@ " (pp_clear m main_mem (pp_c_var_read m))) m.mstep.step_inputs
130
      (Utils.pp_newline_if_non_empty m.mstep.step_inputs)
131
      (Utils.fprintf_list ~sep:"@ " (pp_clear m main_mem (pp_c_var_read m))) m.mstep.step_outputs
132

    
133
let print_main_loop mname main_mem fmt m =
134
  let input_values =
135
    List.map (fun v -> mk_val (LocalVar v) v.var_type)
136
      m.mstep.step_inputs in
137
  begin
138
    fprintf fmt "@ ISATTY = isatty(0);@ ";
139
    fprintf fmt "@ /* Infinite loop */@ ";
140
    fprintf fmt "@[<v 2>while(1){@ ";
141
    fprintf fmt  "fflush(stdout);@ ";
142
    fprintf fmt "%a@ %t%a"
143
      print_get_inputs m
144
      (fun fmt -> pp_main_call mname main_mem fmt m input_values m.mstep.step_outputs)
145
      print_put_outputs m
146
  end
147

    
148
let print_main_code fmt basename m =
149
  let mname = m.mname.node_id in
150
  let main_mem =
151
    if (!Options.static_mem && !Options.main_node <> "")
152
    then "&main_mem"
153
    else "main_mem" in
154
  fprintf fmt "@[<v 2>int main (int argc, char *argv[]) {@ ";
155
  print_main_inout_declaration basename fmt m;
156
  Plugins.c_backend_main_loop_body_prefix basename mname fmt ();
157
  print_main_memory_allocation mname main_mem fmt m;
158
  if !Options.mpfr then
159
    begin
160
      print_global_initialize fmt basename;
161
      print_main_initialize mname main_mem fmt m;
162
    end;
163
  print_main_loop mname main_mem fmt m;
164

    
165
  Plugins.c_backend_main_loop_body_suffix fmt ();
166
  fprintf fmt "@]@ }@ @ ";
167
  if !Options.mpfr then
168
    begin
169
      print_main_clear mname main_mem fmt m;
170
      print_global_clear fmt basename;
171
    end;
172
  fprintf fmt "@ return 1;";
173
  fprintf fmt "@]@ }@."       
174

    
175
let print_main_header fmt =
176
  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\"@.")
177
    !Options.include_dir
178

    
179
let print_main_c main_fmt main_machine basename prog machines _ (*dependencies*) =
180
  print_main_header main_fmt;
181
  fprintf main_fmt "#include <stdlib.h>@.#include <assert.h>@.";
182
  print_import_alloc_prototype main_fmt (Dep (true, basename, [], true (* assuming it is stateful*) ));
183
  pp_print_newline main_fmt ();
184

    
185
  (* Print the svn version number and the supported C standard (C90 or C99) *)
186
  print_version main_fmt;
187
  print_main_code main_fmt basename main_machine
188
end  
189

    
190
(* Local Variables: *)
191
(* compile-command:"make -C ../../.." *)
192
(* End: *)
(5-5/9)