Project

General

Profile

« Previous | Next » 

Revision f0a067e9

Added by Pierre-Loïc Garoche almost 3 years ago

Better production of trace files.
By default traces are not produced. Requires the option -t to produce them

View differences:

src/backends/C/c_backend_main.ml
40 40
  in
41 41
  List.iteri2 (fun idx v' v -> fprintf fmt "@ %a;" po ((idx+1), v', v)) m.mname.node_outputs m.mstep.step_outputs
42 42

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

  
52
      ignore (pp_file_decl fmt "out" idx)
53
    ) m.mstep.step_outputs;
54
  fprintf fmt "@[<v 2>if (traces) {@ ";
55
  List.iteri (fun idx _ ->
56
      ignore (pp_file_open fmt "in" idx) 
57
    ) m.mstep.step_inputs;
58
  List.iteri (fun idx _ ->
59
      ignore (pp_file_open fmt "out" idx)
60
    ) m.mstep.step_outputs;
61
  fprintf fmt "@]}@ "
61 62

  
62 63
  
63 64
let print_main_memory_allocation mname main_mem fmt m =
......
122 123
    fprintf fmt "@ /* Infinite loop */@ ";
123 124
    fprintf fmt "@[<v 2>while(1){@ ";
124 125
    fprintf fmt  "fflush(stdout);@ ";
126
    fprintf fmt "@[<v 2>if (traces) {@ ";
125 127
    List.iteri (fun idx _ -> fprintf fmt "fflush(f_in%i);@ " (idx+1)) m.mstep.step_inputs;
126 128
    List.iteri (fun idx _ -> fprintf fmt "fflush(f_out%i);@ " (idx+1)) m.mstep.step_outputs;
129
    fprintf fmt "@]}@ ";
127 130
    fprintf fmt "%a@ %t%a"
128 131
      print_get_inputs m
129 132
      (fun fmt -> pp_main_call mname main_mem fmt m input_values m.mstep.step_outputs)
130 133
      print_put_outputs m
131 134
  end
132 135

  
136
let print_usage fmt =
137
  fprintf fmt "@[<v 2>void usage(char *argv[]) {@ ";
138
  fprintf fmt "printf(\"Usage: %%s\\n\", argv[0]);@ ";
139
  fprintf fmt "printf(\" -t: produce trace files for input/output flows\\n\");@ ";
140
  fprintf fmt "printf(\" -d<dir>: directory containing traces (default: _traces)\\n\");@ ";
141
  fprintf fmt "printf(\" -p<prefix>: prefix_simu.scope<id> (default: file_node)\\n\");@ ";
142
  fprintf fmt "exit (8);@ ";
143
  fprintf fmt "@]}@ "
144

  
145
let print_options fmt name =
146
  fprintf fmt "int traces = 0;@ ";
147
  fprintf fmt "char* prefix = \"%s\";@ " name;
148
  fprintf fmt "char* dir = \".\";@ ";
149
  fprintf fmt "@[<v 2>while ((argc > 1) && (argv[1][0] == '-')) {@ ";
150
  fprintf fmt "@[<v 2>switch (argv[1][1]) {@ ";
151
  fprintf fmt "@[<v 2>case 't':@ ";
152
  fprintf fmt "traces = 1;@ ";
153
  fprintf fmt "break;@ ";
154
  fprintf fmt "@]@ ";
155
  fprintf fmt "@[<v 2>case 'd':@ ";
156
  fprintf fmt "dir = &argv[1][2];@ ";
157
  fprintf fmt "break;@ ";
158
  fprintf fmt "@]@ ";
159
  fprintf fmt "@[<v 2>case 'p':@ ";
160
  fprintf fmt "prefix = &argv[1][2];@ ";
161
  fprintf fmt "break;@ ";
162
  fprintf fmt "@]@ ";
163
  fprintf fmt "@[<v 2>default:@ ";
164
  fprintf fmt "printf(\"Wrong Argument: %%s\\n\", argv[1]);@ ";
165
  fprintf fmt "usage(argv);@ ";
166
  fprintf fmt "@]@ ";
167
  fprintf fmt "@]}@ ";
168
  fprintf fmt "++argv;@ ";
169
  fprintf fmt "--argc;@ ";
170
  fprintf fmt "@]}@ "
171
  
133 172
let print_main_code fmt basename m =
134 173
  let mname = m.mname.node_id in
174
  (* TODO: find a proper way to shorthen long names. This causes segfault in the binary when trying to fprintf in them *)
175
  let mname = if String.length mname > 50 then string_of_int (Hashtbl.hash mname) else mname in
176
  
135 177
  let main_mem =
136 178
    if (!Options.static_mem && !Options.main_node <> "")
137 179
    then "&main_mem"
138 180
    else "main_mem" in
181
  print_usage fmt;
182
  
139 183
  fprintf fmt "@[<v 2>int main (int argc, char *argv[]) {@ ";
140
  print_main_inout_declaration basename fmt m;
184
  print_options fmt (basename ^ "_" ^ mname);
185
  print_main_inout_declaration m fmt;
141 186
  Plugins.c_backend_main_loop_body_prefix basename mname fmt ();
142 187
  print_main_memory_allocation mname main_mem fmt m;
143 188
  if !Options.mpfr then
......
158 203
  fprintf fmt "@]@ }@."       
159 204

  
160 205
let print_main_header fmt =
161
  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\"@.")
206
  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 <string.h>@.#include \"%s/io_frontend.h\"@.")
162 207
    (Options_management.core_dependency "io_frontend")
163 208

  
164 209
let print_main_c main_fmt main_machine basename prog machines _ (*dependencies*) =

Also available in: Unified diff