From 6fa45cb6794f36aedec0323fea01255793c44f2c Mon Sep 17 00:00:00 2001 From: ploc <ploc@garoche.net> Date: Tue, 10 Jan 2017 13:35:14 +0100 Subject: [PATCH] Changed the generated C file to produce input and output csv files (named inXX and outXX) --- include/io_frontend.c | 20 +++++++---- include/io_frontend.h | 12 +++---- src/backends/C/c_backend_main.ml | 60 +++++++++++++++++++------------- src/utils.ml | 21 +++++++++++ 4 files changed, 77 insertions(+), 36 deletions(-) diff --git a/include/io_frontend.c b/include/io_frontend.c index 4872c75e..9c65e592 100644 --- a/include/io_frontend.c +++ b/include/io_frontend.c @@ -6,7 +6,7 @@ int ISATTY; /* Standard Input procedures **************/ -_Bool _get_bool(char* n){ +_Bool _get_bool(FILE* file, char* n){ char b[512]; _Bool r = 0; int s = 1; @@ -22,9 +22,11 @@ _Bool _get_bool(char* n){ if((c == '0') || (c == 'f') || (c == 'F')) r = 0; if((c == '1') || (c == 't') || (c == 'T')) r = 1; } while((s != 1) || (r == -1)); + fprintf(file, "%i\n",r); return r; } -int _get_int(char* n){ + +int _get_int(FILE* file, char* n){ char b[512]; int r; int s = 1; @@ -36,9 +38,11 @@ int _get_int(char* n){ if(scanf("%s", b)==EOF) exit(0); s = sscanf(b, "%d", &r); } while(s != 1); + fprintf(file, "%d\n", r); return r; } -double _get_double(char* n){ + +double _get_double(FILE* file, char* n){ char b[512]; double r; int s = 1; @@ -50,10 +54,11 @@ double _get_double(char* n){ if(scanf("%s", b)==EOF) exit(0); s = sscanf(b, "%lf", &r); } while(s != 1); + fprintf(file, "%f\n", r); return r; } /* Standard Output procedures **************/ -void _put_bool(char* n, _Bool _V){ +void _put_bool(FILE* file, char* n, _Bool _V){ if(ISATTY) { printf("%s = ", n); } else { @@ -61,8 +66,9 @@ void _put_bool(char* n, _Bool _V){ }; printf("'%i' ", (_V)? 1 : 0); printf("\n"); + fprintf(file, "%i\n", _V); } -void _put_int(char* n, int _V){ +void _put_int(FILE* file, char* n, int _V){ if(ISATTY) { printf("%s = ", n); } else { @@ -70,8 +76,9 @@ void _put_int(char* n, int _V){ }; printf("'%d' ", _V); printf("\n"); + fprintf(file, "%d\n", _V); } -void _put_double(char* n, double _V){ +void _put_double(FILE* file, char* n, double _V){ if(ISATTY) { printf("%s = ", n); } else { @@ -79,4 +86,5 @@ void _put_double(char* n, double _V){ }; printf("'%f' ", _V); printf("\n"); + fprintf(file, "%f\n", _V); } diff --git a/include/io_frontend.h b/include/io_frontend.h index b4528f38..0d515386 100644 --- a/include/io_frontend.h +++ b/include/io_frontend.h @@ -7,22 +7,22 @@ extern int ISATTY; /* Standard Input procedures **************/ /*@ assigns *n; */ -extern _Bool _get_bool(char* n); +extern _Bool _get_bool(FILE* file, char* n); /*@ assigns *n; */ -extern int _get_int(char* n); +extern int _get_int(FILE* file, char* n); /*@ assigns *n; */ -extern double _get_double(char* n); +extern double _get_double(FILE* file, char* n); /* Standard Output procedures **************/ /*@ assigns \nothing; */ -extern void _put_bool(char* n, _Bool _V); +extern void _put_bool(FILE* file, char* n, _Bool _V); /*@ assigns \nothing; */ -extern void _put_int(char* n, int _V); +extern void _put_int(FILE* file, char* n, int _V); /*@ assigns \nothing; */ -extern void _put_double(char* n, double _V); +extern void _put_double(FILE* file, char* n, double _V); #endif diff --git a/src/backends/C/c_backend_main.ml b/src/backends/C/c_backend_main.ml index 4cfa48ea..b844637e 100644 --- a/src/backends/C/c_backend_main.ml +++ b/src/backends/C/c_backend_main.ml @@ -14,6 +14,7 @@ open Corelang open Machine_code open Format open C_backend_common +open Utils module type MODIFIERS_MAINSRC = sig @@ -31,12 +32,12 @@ struct (********************************************************************************************) let print_get_inputs fmt m = - let pi fmt (v', v) = + let pi fmt (id, v', v) = match (Types.unclock_type v.var_type).Types.tdesc with - | Types.Tint -> fprintf fmt "%s = _get_int(\"%s\")" v.var_id v'.var_id - | Types.Tbool -> fprintf fmt "%s = _get_bool(\"%s\")" v.var_id v'.var_id - | Types.Treal when !Options.mpfr -> fprintf fmt "mpfr_set_d(%s, _get_double(\"%s\"), %i)" v.var_id v'.var_id (Mpfr.mpfr_prec ()) - | Types.Treal -> fprintf fmt "%s = _get_double(\"%s\")" v.var_id v'.var_id + | Types.Tint -> fprintf fmt "%s = _get_int(f_in%i, \"%s\")" v.var_id id v'.var_id + | Types.Tbool -> fprintf fmt "%s = _get_bool(f_in%i, \"%s\")" v.var_id id v'.var_id + | 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 ()) + | Types.Treal -> fprintf fmt "%s = _get_double(f_in%i, \"%s\")" v.var_id id v'.var_id | _ -> begin Global.main_node := !Options.main_node; @@ -46,30 +47,39 @@ let print_get_inputs fmt m = raise (Error (v'.var_loc, Main_wrong_kind)) end in - List.iter2 (fun v' v -> fprintf fmt "@ %a;" pi (v', v)) m.mname.node_inputs m.mstep.step_inputs + List.iteri2 (fun idx v' v -> + fprintf fmt "@ %a;" pi ((idx+1), v', v); + ) m.mname.node_inputs m.mstep.step_inputs let print_put_outputs fmt m = - let po fmt (o', o) = + let po fmt (id, o', o) = match (Types.unclock_type o.var_type).Types.tdesc with - | Types.Tint -> fprintf fmt "_put_int(\"%s\", %s)" o'.var_id o.var_id - | Types.Tbool -> fprintf fmt "_put_bool(\"%s\", %s)" o'.var_id o.var_id - | Types.Treal when !Options.mpfr -> fprintf fmt "_put_double(\"%s\", mpfr_get_d(%s, %s))" o'.var_id o.var_id (Mpfr.mpfr_rnd ()) - | Types.Treal -> fprintf fmt "_put_double(\"%s\", %s)" o'.var_id o.var_id + | Types.Tint -> fprintf fmt "_put_int(f_out%i, \"%s\", %s)" id o'.var_id o.var_id + | Types.Tbool -> fprintf fmt "_put_bool(f_out%i, \"%s\", %s)" id o'.var_id o.var_id + | 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 ()) + | Types.Treal -> fprintf fmt "_put_double(f_out%i, \"%s\", %s)" id o'.var_id o.var_id | _ -> assert false in - List.iter2 (fun v' v -> fprintf fmt "@ %a;" po (v', v)) m.mname.node_outputs m.mstep.step_outputs - -let print_main_inout_declaration fmt m = - begin - fprintf fmt "/* Declaration of inputs/outputs variables */@ "; - List.iter - (fun v -> fprintf fmt "%a;@ " (pp_c_type v.var_id) v.var_type - ) m.mstep.step_inputs; - List.iter - (fun v -> fprintf fmt "%a;@ " (pp_c_type v.var_id) v.var_type - ) m.mstep.step_outputs - end + Utils.List.iteri2 (fun idx v' v -> fprintf fmt "@ %a;" po ((idx+1), v', v)) m.mname.node_outputs m.mstep.step_outputs +let print_main_inout_declaration basename fmt m = + let mname = m.mname.node_id in + fprintf fmt "/* Declaration of inputs/outputs variables */@ "; + List.iteri + (fun idx v -> + fprintf fmt "%a;@ " (pp_c_type v.var_id) v.var_type; + fprintf fmt "FILE *f_in%i;@ " (idx+1); (* we start from 1: in1, in2, ... *) + fprintf fmt "f_in%i = fopen(\"%s_%s_simu.in%i\", \"w\");@ " (idx+1) basename mname (idx+1); + ) m.mstep.step_inputs; + List.iteri + (fun idx v -> + fprintf fmt "%a;@ " (pp_c_type v.var_id) v.var_type; + fprintf fmt "FILE *f_out%i;@ " (idx+1); (* we start from 1: in1, in2, ... *) + fprintf fmt "f_out%i = fopen(\"%s_%s_simu.out%i\", \"w\");@ " (idx+1) basename mname (idx+1); + ) m.mstep.step_outputs + + + let print_main_memory_allocation mname main_mem fmt m = if not (fst (get_stateless_status m)) then begin @@ -132,6 +142,8 @@ let print_main_loop mname main_mem fmt m = fprintf fmt "@ /* Infinite loop */@ "; fprintf fmt "@[<v 2>while(1){@ "; fprintf fmt "fflush(stdout);@ "; + List.iteri (fun idx _ -> fprintf fmt "fflush(f_in%i);@ " (idx+1)) m.mstep.step_inputs; + List.iteri (fun idx _ -> fprintf fmt "fflush(f_out%i);@ " (idx+1)) m.mstep.step_outputs; fprintf fmt "%a@ %t%a" print_get_inputs m (fun fmt -> pp_main_call mname main_mem fmt m input_values m.mstep.step_outputs) @@ -145,7 +157,7 @@ let print_main_code fmt basename m = then "&main_mem" else "main_mem" in fprintf fmt "@[<v 2>int main (int argc, char *argv[]) {@ "; - print_main_inout_declaration fmt m; + print_main_inout_declaration basename fmt m; print_main_memory_allocation mname main_mem fmt m; if !Options.mpfr then begin diff --git a/src/utils.ml b/src/utils.ml index ee8d9dd6..35b0c94c 100755 --- a/src/utils.ml +++ b/src/utils.ml @@ -361,6 +361,27 @@ let last_tag = ref (-1) let new_tag () = incr last_tag; !last_tag + +module List = +struct + include List + let iteri2 f l1 l2 = + if List.length l1 <> List.length l2 then + raise (Invalid_argument "iteri2: lists have different lengths") + else + let rec run idx l1 l2 = + match l1, l2 with + | [], [] -> () + | hd1::tl1, hd2::tl2 -> ( + f idx hd1 hd2; + run (idx+1) tl1 tl2 + ) + | _ -> assert false + in + run 0 l1 l2 +end + + (* Local Variables: *) (* compile-command:"make -C .." *) (* End: *) -- GitLab