From 58e6a951e4ed3c5cbfd2ce9241a1cf0fe09b934f Mon Sep 17 00:00:00 2001 From: Tom Barthe Date: Fri, 25 Dec 2020 21:54:58 +0100 Subject: [PATCH] Declaration of static variables in Cfundef --- compiler/obc/c/async_avr.ml | 6 ++++-- compiler/obc/c/c.ml | 32 +++++++++++++++++++++++++++++--- compiler/obc/c/cgen.ml | 10 +++++----- compiler/obc/c/cmain.ml | 4 ++-- 4 files changed, 40 insertions(+), 12 deletions(-) diff --git a/compiler/obc/c/async_avr.ml b/compiler/obc/c/async_avr.ml index 22c32a6..cf8bb21 100644 --- a/compiler/obc/c/async_avr.ml +++ b/compiler/obc/c/async_avr.ml @@ -68,12 +68,12 @@ struct (fun (_, ack) -> match ack with Timer_ms ms -> ms) trans in + let tick = "mod_ticks" in let body = match timers with | [] -> [] | _ -> let base_timer = fold_gcd timers in let max_timer = fold_lcm timers in - let tick = "mod_ticks" in List.map (call_step_async base_timer (Cvar tick)) trans in (* run_timers is declared in avr.h (because of the ISR macro which @@ -84,7 +84,9 @@ struct f_retty = Cty_void; f_args = []; f_body = { - var_decls = []; + var_decls = [ + mk_vardecl_val ~static:true tick Cty_int (Cconst (Ccint 0)) + ]; block_body = body } } diff --git a/compiler/obc/c/c.ml b/compiler/obc/c/c.ml index 38cb78c..d3eaf7f 100644 --- a/compiler/obc/c/c.ml +++ b/compiler/obc/c/c.ml @@ -69,11 +69,18 @@ type cty = | Cty_arr of int * cty (** A static array of the specified size. *) | Cty_void (** Well, [void] is not really a C type. *) +type cvardecl = { + vd_name : string; + vd_ty : cty; + vd_static : bool; + vd_value : cexpr option +} + (** A C block: declarations and statements. In source code form, it begins with variable declarations before a list of semicolon-separated statements, the whole thing being enclosed in curly braces. *) -type cblock = { - var_decls : (string * cty) list; +and cblock = { + var_decls : cvardecl list; (** Variable declarations, where each declaration consists of a variable name and the associated C type. *) block_body : cstm list; @@ -169,6 +176,15 @@ and cfile_desc = (******************************) +let mk_vardecl ?(static = false) ?(value = None) name ty = + { vd_name = name; vd_ty = ty; vd_static = static; vd_value = value } + +let mk_vardecl_val ?(static = false) name ty value = + mk_vardecl ~static:static ~value:(Some value) name ty + +let vardecl_of_cvars cvars = + List.map (fun (name, ty) -> mk_vardecl name ty) cvars + (** {3 Pretty-printing of the C ast.} *) (** [pp_list1 f sep fmt l] pretty-prints into the Format.formatter [fmt] @@ -235,7 +251,7 @@ and pp_param_list fmt l = pp_list1 pp_vardecl "," fmt l and pp_var_list fmt l = pp_list pp_vardecl ";" fmt l let rec pp_cblock fmt cb = - let pp_varlist = pp_list pp_vardecl ";" in + let pp_varlist = pp_list pp_cvardecl ";" in fprintf fmt "%a%a" pp_varlist cb.var_decls pp_cstm_list cb.block_body and pp_cstm_list fmt stml = pp_list pp_cstm ";" fmt stml and pp_cstm fmt stm = match stm with @@ -308,6 +324,16 @@ and pp_cconst fmt cconst = match cconst with | Ctag t -> pp_string fmt t | Cstrlit t -> fprintf fmt "\"%s\"" (String.escaped t) +and pp_cvardecl fmt vd = + if vd.vd_static then + fprintf fmt "static "; + fprintf fmt "%a" pp_vardecl (vd.vd_name, vd.vd_ty); + match vd.vd_value with + | Some e -> fprintf fmt " = %a" pp_cexpr e + | None -> () + +and pp_cvardecl_list fmt l = pp_list1 pp_vardecl "," fmt l + let pp_cdecl fmt cdecl = match cdecl with | Cdecl_enum (s, sl) -> fprintf fmt "@[@[typedef enum {@ %a@]@ } %a;@ @]@\n" diff --git a/compiler/obc/c/cgen.ml b/compiler/obc/c/cgen.ml index c91f73c..95e4d18 100644 --- a/compiler/obc/c/cgen.ml +++ b/compiler/obc/c/cgen.ml @@ -693,8 +693,8 @@ and cstm_of_act_list vr var_env obj_env b = in match l with | [] -> cstm - | _ -> - [Csblock { var_decls = l; block_body = cstm }] + | _ -> [Csblock { var_decls = vardecl_of_cvars l; + block_body = cstm }] (* TODO needed only because of renaming phase *) let global_name = ref "";; @@ -753,7 +753,7 @@ let fun_def_of_step_fun n obj_env mem objs md = f_retty = Cty_void; f_args = args; f_body = { - var_decls = out_vars; + var_decls = vardecl_of_cvars out_vars; block_body = body } } @@ -786,7 +786,7 @@ let fun_stub_def_of_step_fun n md copy_in copy_out = f_retty = Cty_void; f_args = args; f_body = { - var_decls = out_vars; + var_decls = vardecl_of_cvars out_vars; block_body = prologue @ body } } @@ -854,7 +854,7 @@ let async_fun_def_of_step_fun n obj_env mem objs md copy_in copy_out = f_retty = Cty_void; f_args = args; f_body = { - var_decls = out_vars; + var_decls = vardecl_of_cvars out_vars; block_body = prologue @ body @ epilogue } } diff --git a/compiler/obc/c/cmain.ml b/compiler/obc/c/cmain.ml index c78f39b..828ea2b 100644 --- a/compiler/obc/c/cmain.ml +++ b/compiler/obc/c/cmain.ml @@ -317,8 +317,8 @@ let main_skel var_list prologue body = f_retty = Cty_int; f_args = [("argc", Cty_int); ("argv", Cty_ptr (Cty_ptr Cty_char))]; f_body = { - var_decls = - (step_counter, Cty_int) :: (max_step, Cty_int) :: var_list; + var_decls = vardecl_of_cvars + ((step_counter, Cty_int) :: (max_step, Cty_int) :: var_list); block_body = [ (*