Project

General

Profile

Revision 86ae18b7 src/backends/C/c_backend_main.ml

View differences:

src/backends/C/c_backend_main.ml
14 14
open Machine_code
15 15
open Format
16 16
open C_backend_common
17
open Utils
18 17

  
19 18
module type MODIFIERS_MAINSRC =
20 19
sig
......
32 31
(********************************************************************************************)
33 32

  
34 33
let print_get_inputs fmt m =
35
  let pi fmt (id, v', v) =
34
  let pi fmt (v', v) =
36 35
  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
36
    | Types.Tint -> fprintf fmt "%s = _get_int(\"%s\")" v.var_id v'.var_id
37
    | Types.Tbool -> fprintf fmt "%s = _get_bool(\"%s\")" v.var_id v'.var_id
38
    | Types.Treal when !Options.mpfr -> fprintf fmt "mpfr_set_d(%s, _get_double(\"%s\"), %i)" v.var_id v'.var_id (Mpfr.mpfr_prec ())
39
    | Types.Treal -> fprintf fmt "%s = _get_double(\"%s\")" v.var_id v'.var_id
41 40
    | _ ->
42 41
      begin
43 42
	Global.main_node := !Options.main_node;
......
47 46
	raise (Error (v'.var_loc, Main_wrong_kind))
48 47
      end
49 48
  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
49
  List.iter2 (fun v' v -> fprintf fmt "@ %a;" pi (v', v)) m.mname.node_inputs m.mstep.step_inputs
53 50

  
54 51
let print_put_outputs fmt m = 
55
  let po fmt (id, o', o) =
52
  let po fmt (o', o) =
56 53
    match (Types.unclock_type o.var_type).Types.tdesc with
57
    | Types.Tint -> fprintf fmt "_put_int(f_out%i, \"%s\", %s)" id o'.var_id o.var_id
58
    | Types.Tbool -> fprintf fmt "_put_bool(f_out%i, \"%s\", %s)" id o'.var_id o.var_id
59
    | Types.Treal when !Options.mpfr -> fprintf fmt "_put_double(f_out%i, \"%s\", mpfr_get_d(%s, %s))" id o'.var_id o.var_id (Mpfr.mpfr_rnd ())
60
    | Types.Treal -> fprintf fmt "_put_double(f_out%i, \"%s\", %s)" id o'.var_id o.var_id
54
    | Types.Tint -> fprintf fmt "_put_int(\"%s\", %s)" o'.var_id o.var_id
55
    | Types.Tbool -> fprintf fmt "_put_bool(\"%s\", %s)" o'.var_id o.var_id
56
    | Types.Treal when !Options.mpfr -> fprintf fmt "_put_double(\"%s\", mpfr_get_d(%s, %s))" o'.var_id o.var_id (Mpfr.mpfr_rnd ())
57
    | Types.Treal -> fprintf fmt "_put_double(\"%s\", %s)" o'.var_id o.var_id
61 58
    | _ -> assert false
62 59
  in
63
  Utils.List.iteri2 (fun idx v' v -> fprintf fmt "@ %a;" po ((idx+1), v', v)) m.mname.node_outputs m.mstep.step_outputs
60
  List.iter2 (fun v' v -> fprintf fmt "@ %a;" po (v', v)) m.mname.node_outputs m.mstep.step_outputs
61

  
62
let print_main_inout_declaration fmt m =
63
  begin
64
    fprintf fmt "/* Declaration of inputs/outputs variables */@ ";
65
    List.iter 
66
      (fun v -> fprintf fmt "%a;@ " (pp_c_type v.var_id) v.var_type
67
      ) m.mstep.step_inputs;
68
    List.iter 
69
      (fun v -> fprintf fmt "%a;@ " (pp_c_type v.var_id) v.var_type
70
      ) m.mstep.step_outputs
71
  end
64 72

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

  
81

  
82
  
83 73
let print_main_memory_allocation mname main_mem fmt m =
84 74
  if not (fst (get_stateless_status m)) then
85 75
  begin  
......
142 132
    fprintf fmt "@ /* Infinite loop */@ ";
143 133
    fprintf fmt "@[<v 2>while(1){@ ";
144 134
    fprintf fmt  "fflush(stdout);@ ";
145
    List.iteri (fun idx _ -> fprintf fmt "fflush(f_in%i);@ " (idx+1)) m.mstep.step_inputs;
146
    List.iteri (fun idx _ -> fprintf fmt "fflush(f_out%i);@ " (idx+1)) m.mstep.step_outputs;
147 135
    fprintf fmt "%a@ %t%a"
148 136
      print_get_inputs m
149 137
      (fun fmt -> pp_main_call mname main_mem fmt m input_values m.mstep.step_outputs)
......
157 145
    then "&main_mem"
158 146
    else "main_mem" in
159 147
  fprintf fmt "@[<v 2>int main (int argc, char *argv[]) {@ ";
160
  print_main_inout_declaration basename fmt m;
148
  print_main_inout_declaration fmt m;
161 149
  print_main_memory_allocation mname main_mem fmt m;
162 150
  if !Options.mpfr then
163 151
    begin
......
179 167
  fprintf fmt "@]@ }@."       
180 168

  
181 169
let print_main_header fmt =
182
  fprintf fmt "#include <stdio.h>@.#include <unistd.h>@.#include \"%s/io_frontend.h\"@." Version.include_path 
170
(*fprintf fmt "#include <stdio.h>@.#include <unistd.h>@.#include \"%s/io_frontend.h\"@." Version.include_path*)
171
fprintf fmt "#include <stdio.h>@.#include <unistd.h>@.#include \"%s/io_frontend.h\"@." !Options.include_dir
183 172

  
184 173

  
185 174
let print_main_c main_fmt main_machine basename prog machines _ (*dependencies*) =

Also available in: Unified diff