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:

include/io_frontend.c
6 6
int ISATTY;
7 7

  
8 8
/* Standard Input procedures **************/
9
_Bool _get_bool(FILE* file, char* n){
9
_Bool _get_bool(char* n){
10 10
   char b[512];
11 11
   int r = 0;
12 12
   int s = 1;
......
22 22
      if((c == '0') || (c == 'f') || (c == 'F')) r = 0;
23 23
      if((c == '1') || (c == 't') || (c == 'T')) r = 1;
24 24
   } while((s != 1) || (r == -1));
25
   fprintf(file, "%i\n",r);
26 25
   return (_Bool)r;
27 26
}
28 27

  
29
int _get_int(FILE* file, char* n){
28
int _get_int(char* n){
30 29
   char b[512];
31 30
   int r;
32 31
   int s = 1;
......
38 37
      if(scanf("%s", b)==EOF) exit(0);
39 38
      s = sscanf(b, "%d", &r);
40 39
   } while(s != 1);
41
   fprintf(file, "%d\n", r);
42 40
   return r;
43 41
}
44 42

  
45
double _get_double(FILE* file, char* n){
43
double _get_double(char* n){
46 44
   char b[512];
47 45
   double r;
48 46
   int s = 1;
......
54 52
      if(scanf("%s", b)==EOF) exit(0);
55 53
      s = sscanf(b, "%lf", &r);
56 54
   } while(s != 1);
57
   fprintf(file, "%f\n", r);
58 55
   return r;
59 56
}
60 57
/* Standard Output procedures **************/
61
void _put_bool(FILE* file, char* n, _Bool _V){
58
void _put_bool(char* n, _Bool _V){
62 59
  if(ISATTY) {
63 60
    printf("%s = ", n);
64 61
  } else {
......
66 63
  };
67 64
  printf("'%i' ", (_V)? 1 : 0);
68 65
  printf("\n");
69
  fprintf(file, "%i\n", _V);
70
  fflush(file);
71 66
}
72
void _put_int(FILE* file, char* n, int _V){
67
void _put_int(char* n, int _V){
73 68
  if(ISATTY) {
74 69
    printf("%s = ", n);
75 70
  } else {
......
77 72
  };
78 73
  printf("'%d' ", _V);
79 74
  printf("\n");
80
  fprintf(file, "%d\n", _V);
81
  fflush(file);
82 75
}
83 76

  
84
void _put_float(FILE* file, char* n, float _V, int PREC){
77
void _put_float(char* n, float _V, int PREC){
85 78
  if(ISATTY) {
86 79
    printf("%s = ", n);
87 80
  } else {
......
89 82
  };
90 83
  printf("'%.*f' ", PREC, _V);
91 84
  printf("\n");
92
  fprintf(file, "%.*f\n", PREC, _V);
93
  fflush(file);
94 85
}
95 86

  
96
void _put_double(FILE* file, char* n, double _V, int PREC){
87
void _put_double(char* n, double _V, int PREC){
97 88
  if(ISATTY) {
98 89
    printf("%s = ", n);
99 90
  } else {
......
101 92
  };
102 93
  printf("'%.*f' ", PREC, _V);
103 94
  printf("\n");
104
  fprintf(file, "%.*f\n", PREC, _V);
105
  fflush(file);
106 95
}
include/io_frontend.h
7 7
/* Standard Input procedures **************/
8 8

  
9 9
/*@ assigns *n; */
10
extern _Bool _get_bool(FILE* file, char* n);
10
extern _Bool _get_bool(char* n);
11 11

  
12 12
/*@ assigns *n; */
13
extern int _get_int(FILE* file, char* n);
13
extern int _get_int(char* n);
14 14

  
15 15
/*@ assigns *n; */
16
extern double _get_double(FILE* file, char* n);
16
extern double _get_double(char* n);
17 17

  
18 18
/* Standard Output procedures **************/
19 19
/*@ assigns \nothing; */
20
extern void _put_bool(FILE* file, char* n, _Bool _V);
20
extern void _put_bool(char* n, _Bool _V);
21 21

  
22 22
/*@ assigns \nothing; */
23
extern void _put_int(FILE* file, char* n, int _V);
23
extern void _put_int(char* n, int _V);
24 24

  
25 25
/*@ assigns \nothing; */
26
extern void _put_float(FILE* file, char* n, float _V, int PREC);
26
extern void _put_float(char* n, float _V, int PREC);
27 27

  
28 28
/*@ assigns \nothing; */
29
extern void _put_double(FILE* file, char* n, double _V, int PREC);
29
extern void _put_double(char* n, double _V, int PREC);
30 30

  
31 31
#endif
include/io_frontend.hpp
8 8
int ISATTY;
9 9

  
10 10
/* Standard Input procedures **************/
11
bool _get_bool(FILE* file, char* n){
11
bool _get_bool(char* n){
12 12
   char b[512];
13 13
   bool r = 0;
14 14
   int s = 1;
......
24 24
      if((c == '0') || (c == 'f') || (c == 'F')) r = 0;
25 25
      if((c == '1') || (c == 't') || (c == 'T')) r = 1;
26 26
   } while((s != 1) || (r == -1));
27
   fprintf(file, "%i\n",r);
28 27
   return r;
29 28
}
30 29

  
31
int _get_int(FILE* file, char* n){
30
int _get_int(char* n){
32 31
   char b[512];
33 32
   int r;
34 33
   int s = 1;
......
40 39
      if(scanf("%s", b)==EOF) exit(0);
41 40
      s = sscanf(b, "%d", &r);
42 41
   } while(s != 1);
43
   fprintf(file, "%d\n", r);
44 42
   return r;
45 43
}
46 44

  
47
double _get_double(FILE* file, char* n){
45
double _get_double(char* n){
48 46
   char b[512];
49 47
   double r;
50 48
   int s = 1;
......
56 54
      if(scanf("%s", b)==EOF) exit(0);
57 55
      s = sscanf(b, "%lf", &r);
58 56
   } while(s != 1);
59
   fprintf(file, "%f\n", r);
60 57
   return r;
61 58
}
62 59
/* Standard Output procedures **************/
63
void _put_bool(FILE* file, char* n, bool _V){
60
void _put_bool(char* n, bool _V){
64 61
  if(ISATTY) {
65 62
    printf("%s = ", n);
66 63
  } else {
67 64
    printf("'%s': ", n);
68 65
  };
69 66
  printf("'%i' ", (_V)? 1 : 0);
70
  printf("\n");
71
  fprintf(file, "%i\n", _V);
72 67
}
73
void _put_int(FILE* file, char* n, int _V){
68
void _put_int(char* n, int _V){
74 69
  if(ISATTY) {
75 70
    printf("%s = ", n);
76 71
  } else {
77 72
    printf("'%s': ", n);
78 73
  };
79 74
  printf("'%d' ", _V);
80
  printf("\n");
81
  fprintf(file, "%d\n", _V);
82 75
}
83 76

  
84
void _put_float(FILE* file, char* n, float _V, int PREC){
77
void _put_float(char* n, float _V, int PREC){
85 78
  if(ISATTY) {
86 79
    printf("%s = ", n);
87 80
  } else {
......
89 82
  };
90 83
  printf("'%.*f' ", PREC, _V);
91 84
  printf("\n");
92
  fprintf(file, "%.*f\n", PREC, _V);
93
  fflush(file);
94 85
}
95 86

  
96
void _put_double(FILE* file, char* n, double _V, int PREC){
87
void _put_double(char* n, double _V, int PREC){
97 88
  if(ISATTY) {
98 89
    printf("%s = ", n);
99 90
  } else {
......
101 92
  };
102 93
  printf("'%.*f' ", PREC, _V);
103 94
  printf("\n");
104
  fprintf(file, "%.*f\n", PREC, _V);
105
  fflush(file);
106 95
}
107 96

  
108 97

  
src/backends/C/c_backend_common.ml
705 705

  
706 706
  (*** Common functions for main ***)
707 707

  
708
let pp_print_file file_suffix fmt typ arg =
709
  fprintf fmt "@[<v 2>if (traces) {@ ";
710
  fprintf fmt "fprintf(f_%s, \"%%%s\\n\", %s);@ " file_suffix typ arg;
711
  fprintf fmt "fflush(f_%s);@ " file_suffix;
712
  fprintf fmt "@]}@ "
713
  
708 714
let print_put_var fmt file_suffix name var_type var_id =
715
  let pp_file = pp_print_file ("out" ^ file_suffix) in
709 716
  let unclocked_t = Types.unclock_type var_type in
710
  if Types.is_int_type unclocked_t then
711
    fprintf fmt "_put_int(f_out%s, \"%s\", %s)" file_suffix name var_id
712
  else if Types.is_bool_type unclocked_t then
713
    fprintf fmt "_put_bool(f_out%s, \"%s\", %s)" file_suffix name var_id
717
  if Types.is_int_type unclocked_t then (
718
    fprintf fmt "_put_int(\"%s\", %s);@ " name var_id;
719
    pp_file fmt "d" var_id
720
  )
721
  else if Types.is_bool_type unclocked_t then (
722
    fprintf fmt "_put_bool(\"%s\", %s);@ " name var_id;
723
    pp_file fmt "i" var_id
724
  )
714 725
  else if Types.is_real_type unclocked_t then
715
    if !Options.mpfr then
716
      fprintf fmt "_put_double(f_out%s, \"%s\", mpfr_get_d(%s, %s), %i)" file_suffix name var_id (Mpfr.mpfr_rnd ()) !Options.print_prec_double
717
    else
718
      fprintf fmt "_put_double(f_out%s, \"%s\", %s, %i)" file_suffix name var_id !Options.print_prec_double
726
    let _ =
727
      if !Options.mpfr then
728
        fprintf fmt "_put_double(\"%s\", mpfr_get_d(%s, %s), %i);@ " name var_id (Mpfr.mpfr_rnd ()) !Options.print_prec_double
729
      else
730
        fprintf fmt "_put_double(\"%s\", %s, %i);@ " name var_id !Options.print_prec_double
731
    in
732
    pp_file fmt ".*f" ((string_of_int !Options.print_prec_double) ^ ", " ^ var_id)
719 733
  else
720 734
    (Format.eprintf "Impossible to print the _put_xx for type %a@.@?" Types.print_ty var_type; assert false)
721 735

  
722 736
      
723 737
let print_get_inputs fmt m =
724 738
  let pi fmt (id, v', v) =
725

  
739
    let pp_file = pp_print_file ("in" ^ (string_of_int id)) in
726 740
    let unclocked_t = Types.unclock_type v.var_type in
727
    if Types.is_int_type unclocked_t then
728
      fprintf fmt "%s = _get_int(f_in%i, \"%s\")" v.var_id id v'.var_id
729
    else if Types.is_bool_type unclocked_t then
730
      fprintf fmt "%s = _get_bool(f_in%i, \"%s\")" v.var_id id v'.var_id
741
    if Types.is_int_type unclocked_t then (
742
      fprintf fmt "%s = _get_int(\"%s\");@ " v.var_id v'.var_id;
743
      pp_file fmt "d" v.var_id
744
    )
745
    else if Types.is_bool_type unclocked_t then (
746
      fprintf fmt "%s = _get_bool(\"%s\");@ " v.var_id v'.var_id;
747
      pp_file fmt "i" v.var_id
748
    )
731 749
    else if Types.is_real_type unclocked_t then
732
      if !Options.mpfr then
733
	fprintf fmt "mpfr_set_d(%s, _get_double(f_in%i, \"%s\"), %i)" v.var_id id v'.var_id (Mpfr.mpfr_prec ())
734
      else
735
	fprintf fmt "%s = _get_double(f_in%i, \"%s\")" v.var_id id v'.var_id
750
        if !Options.mpfr then (
751
	  fprintf fmt "double %s_tmp = _get_double(\"%s\");@ " v.var_id v'.var_id;
752
          pp_file fmt "f" (v.var_id ^ "_tmp");
753
          fprintf fmt "mpfr_set_d(%s, %s_tmp, %i);" v.var_id v.var_id (Mpfr.mpfr_prec ())
754
        )
755
        else (
756
	  fprintf fmt "%s = _get_double(\"%s\");@ " v.var_id v'.var_id;
757
          pp_file fmt "f" v.var_id
758
        )
736 759
    else
737 760
      begin
738 761
	Global.main_node := !Options.main_node;
......
743 766
      end
744 767
  in
745 768
  Utils.List.iteri2 (fun idx v' v ->
746
    fprintf fmt "@ %a;" pi ((idx+1), v', v);
769
    fprintf fmt "@ %a" pi ((idx+1), v', v);
747 770
  ) m.mname.node_inputs m.mstep.step_inputs
748 771

  
749 772

  
773
let pp_file_decl fmt inout idx =
774
  let idx = idx + 1 in (* we start from 1: in1, in2, ... *)
775
  fprintf fmt "FILE *f_%s%i;@ " inout idx 
776

  
777
let pp_file_open fmt inout idx =
778
  let idx = idx + 1 in (* we start from 1: in1, in2, ... *)
779
  fprintf fmt "const char* cst_char_suffix_%s%i = \"_simu.%s%i\";@ " inout idx inout idx;
780
  fprintf fmt "size_t l%s%i = strlen(dir) + strlen(prefix) + strlen(cst_char_suffix_%s%i);@ " inout idx inout idx;
781
  fprintf fmt "char* f_%s%i_name = malloc((l%s%i+2) * sizeof(char));@ " inout idx inout idx;
782
  fprintf fmt "strcpy (f_%s%i_name, dir);@ " inout idx;
783
  fprintf fmt "strcat(f_%s%i_name, \"/\");@ " inout idx;
784
  fprintf fmt "strcat(f_%s%i_name, prefix);@ " inout idx;
785
  fprintf fmt "strcat(f_%s%i_name, cst_char_suffix_%s%i);@ " inout idx inout idx;
786
  fprintf fmt "f_%s%i = fopen(f_%s%i_name, \"w\");@ " inout idx inout idx;
787
  fprintf fmt "free(f_%s%i_name);@ " inout idx;
788
  "f_" ^ inout ^ (string_of_int idx)
789

  
790

  
750 791
(* Local Variables: *)
751 792
(* compile-command:"make -C ../../.." *)
752 793
(* End: *)
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*) =
src/plugins/scopes/scopes.ml
175 175
let extract_scopes_defs scopes =
176 176
  let rec scope_path_name (path, flow) accu = 
177 177
    match path with 
178
    | [] -> accu ^ "_reg." ^ (scope_var_name flow.var_id), flow.var_type
178
    | [] -> accu ^ "_reg." ^ (scope_var_name flow.var_id), flow
179 179
    | (_, _, Some instance_id)::tl -> scope_path_name (tl, flow) ( accu ^ instance_id ^ "->" ) 
180 180
    | _ -> assert false
181 181
  in
......
189 189
  
190 190
let pp_scopes_files basename mname fmt scopes =
191 191
  let scopes_vars = extract_scopes_defs scopes in
192
  List.iteri (fun idx _ (* (id, (var, typ)) *) ->
193
      Format.fprintf fmt "FILE *f_out_scopes_%i;@ " (idx+1);
194
      (* we start from 1: in1, in2, ... *)
195
    Format.fprintf fmt
196
      "f_out_scopes_%i = fopen(\"%s_%s_simu.scope%i\", \"w\");@ "
197
      (idx+1) basename mname (idx+1);
198
  ) scopes_vars
199

  
192
  List.iteri (fun idx  _(*(id, (var_path, var))*)  ->
193
      C_backend_common.pp_file_decl fmt "out_scopes" idx)
194
    scopes_vars;
195
  Format.fprintf fmt "@[<v 2>if (traces) {@ ";
196
  List.iteri (fun idx  (id, (var_path, var))  ->
197
      let file = C_backend_common.pp_file_open fmt "out_scopes" idx in
198
      Format.fprintf fmt
199
        "fprintf(%s, \"# scope: %s\\n\");@ "
200
        file id;
201
      Format.fprintf fmt
202
        "fprintf(%s, \"# node: %s\\n\");@ "
203
        file (Utils.desome var.var_parent_nodeid);
204
      Format.fprintf fmt
205
        "fprintf(%s, \"# variable: %s\\n\");@ "
206
        file var.var_id
207
    ) scopes_vars;
208
  Format.fprintf fmt "@]}@ "
209
    
200 210
  
201 211
let pp_scopes fmt scopes = 
202 212
  let scopes_vars = extract_scopes_defs scopes in
203
  List.iteri (fun idx (id, (var, typ)) ->
213
  List.iteri (fun idx (id, (var_path, var)) ->
204 214
    Format.fprintf fmt "@ %t;" 
205 215
      (fun fmt -> C_backend_common.print_put_var fmt
206
                    ("_scopes_" ^ string_of_int (idx+1))
207
                    id (*var*) typ var)
216
                    ("_scopes" ^ string_of_int (idx+1))
217
                    id (*var*) var.var_type var_path)
208 218
  ) scopes_vars
209 219

  
210 220
(**********************************************************************)

Also available in: Unified diff