From e536ec17d685c27888b6e428100c5f2817f3316f Mon Sep 17 00:00:00 2001 From: Tom Barthe Date: Sat, 26 Dec 2020 04:28:39 +0100 Subject: [PATCH] Use external linkage for global async variables --- compiler/obc/c/async.ml | 13 +++++++++++-- compiler/obc/c/c.ml | 4 ++++ compiler/obc/c/cgen.ml | 3 ++- 3 files changed, 17 insertions(+), 3 deletions(-) diff --git a/compiler/obc/c/async.ml b/compiler/obc/c/async.ml index e9153a5..99cee0c 100644 --- a/compiler/obc/c/async.ml +++ b/compiler/obc/c/async.ml @@ -13,12 +13,21 @@ let filter_async_objs cd = | None -> false) cd.cd_objs -let async_global_objs_defs cd = +let async_global_objs_vars cd = List.map (fun od -> let name = async_global_var_name od in let ty = Cty_id (qn_append od.o_class "_async") in - Cvardef (name, ty)) + name, ty) (filter_async_objs cd) +let async_global_objs_defs cd = + List.map + (fun (name, ty) -> Cvardef (name, ty)) + (async_global_objs_vars cd) + +let async_global_objs_decls cd = + List.map + (fun (name, ty) -> Cdecl_extern (name, ty)) + (async_global_objs_vars cd) diff --git a/compiler/obc/c/c.ml b/compiler/obc/c/c.ml index d3eaf7f..83931e7 100644 --- a/compiler/obc/c/c.ml +++ b/compiler/obc/c/c.ml @@ -143,6 +143,7 @@ type cdecl = (** C function declaration. *) | Cdecl_constant of string * cty * cexpr (** C constant declaration (alias, name)*) + | Cdecl_extern of string * cty (** C function definitions *) type cfundef = { @@ -353,6 +354,9 @@ let pp_cdecl fmt cdecl = match cdecl with | Cdecl_constant (n, cty, ce) -> fprintf fmt "@[static const %a = %a;@ @]@\n" pp_vardecl (n, cty) pp_cconst_expr ce + | Cdecl_extern (n, cty) -> + fprintf fmt "@[extern %a;@ @]@\n" + pp_vardecl (n, cty) let pp_cdef fmt cdef = match cdef with | Cfundef cfd -> diff --git a/compiler/obc/c/cgen.ml b/compiler/obc/c/cgen.ml index bd7793d..cb6adbc 100644 --- a/compiler/obc/c/cgen.ml +++ b/compiler/obc/c/cgen.ml @@ -947,6 +947,7 @@ let cdefs_and_cdecls_of_class_def cd = let out_struct_decl = out_decl_of_class_def cd in let async_struct_decl = async_decl_of_class_def cd in let objs = async_global_objs_defs cd in + let objs_decls = async_global_objs_decls cd in let step = fun_def_of_step_fun cd.cd_name cd.cd_objs cd.cd_mems cd.cd_objs step_m in (* TODO(Arduino): let the user choose the backend they want *) @@ -973,7 +974,7 @@ let cdefs_and_cdecls_of_class_def cd = let decls = List.map cdecl_of_cfundef defs in memory_struct_decl @ in_struct_decl @ out_struct_decl @ async_struct_decl - @ decls, objs @ defs + @ objs_decls @ decls, objs @ defs (** {2 Type translation} *)