From 2c7466e2de76d3a651579994bdf933974a8bd92d Mon Sep 17 00:00:00 2001 From: Tom Barthe Date: Wed, 30 Dec 2020 02:03:06 +0100 Subject: [PATCH] Reset only stateful async nodes --- compiler/obc/c/async.ml | 12 ++++++++++-- compiler/obc/c/async_avr.ml | 3 ++- compiler/obc/c/async_backend.mli | 1 + compiler/obc/c/cmain.ml | 8 ++++++++ 4 files changed, 21 insertions(+), 3 deletions(-) diff --git a/compiler/obc/c/async.ml b/compiler/obc/c/async.ml index 4004706..5e98090 100644 --- a/compiler/obc/c/async.ml +++ b/compiler/obc/c/async.ml @@ -1,7 +1,9 @@ open Obc open C +open Modules open Idents open Names +open Signature let async_global_var_name od = "g_async__" ^ (name od.o_ident) @@ -31,12 +33,18 @@ let async_global_objs_decls cd = (fun (name, ty) -> Cdecl_extern (name, ty)) (async_global_objs_vars cd) +let od_is_stateful od = + let sig_info = find_value od.o_class in + sig_info.node_stateful + let async_reset cd = + let async_objs = filter_async_objs cd in + let stateful = List.filter od_is_stateful async_objs in List.map (fun od -> let global = Cvar (async_global_var_name od) in - let field = Cfield (global, local_qn "mem") in + let field = Cfield (global, local_qn "self") in let reset = cname_of_qn od.o_class ^ "_reset" in Csexpr (Cfun_call (reset, [Caddrof field]))) - (filter_async_objs cd) + stateful diff --git a/compiler/obc/c/async_avr.ml b/compiler/obc/c/async_avr.ml index 8044e15..310d33f 100644 --- a/compiler/obc/c/async_avr.ml +++ b/compiler/obc/c/async_avr.ml @@ -11,7 +11,6 @@ struct | Timer_ms of int (* FIXME(Arduino): don't do a shallow copy *) - (* FIXME(Arduino): add a mutex… *) let gen_copy_func cd suffix = let func_name = (cname_of_qn cd.cd_name) ^ "_copy" ^ suffix in (* TODO(Arduino): add const qualifier *) @@ -109,4 +108,6 @@ struct ] in [], defs + let main_init = [Csexpr (Cfun_call ("init_timer1", []))] + end diff --git a/compiler/obc/c/async_backend.mli b/compiler/obc/c/async_backend.mli index 10d09f9..ed322b7 100644 --- a/compiler/obc/c/async_backend.mli +++ b/compiler/obc/c/async_backend.mli @@ -7,4 +7,5 @@ sig val gen_copy_func_out : class_def -> cdef val includes : string list val decls_and_defs : obj_dec list -> cdecl list * cdef list + val main_init : cstm list end diff --git a/compiler/obc/c/cmain.ml b/compiler/obc/c/cmain.ml index 1781270..7780abb 100644 --- a/compiler/obc/c/cmain.ml +++ b/compiler/obc/c/cmain.ml @@ -37,6 +37,7 @@ open Signature open C open Cgen open Async +open Async_avr open Compiler_utils (** {1 Main C function generation} *) @@ -412,6 +413,13 @@ let mk_main name p = classes in + (* + * We add these instructions at the end because otherwise the timer + * could be triggered while the initial reset of the async nodes is + * not yet terminated. + *) + let res_l = res_l @ AvrBackend.main_init in + [("_main.c", Csource (defs @ [main_skel var_l res_l step_l])); ("_main.h", Cheader ([name], []))]; ) else