From fbad83a61d1ab265f784c6d657dfa732de200a86 Mon Sep 17 00:00:00 2001 From: Tom Barthe Date: Tue, 22 Dec 2020 21:30:04 +0100 Subject: [PATCH] Add and use atomic_memcpy for the AVR backend --- compiler/obc/c/{async_posix.ml => async_avr.ml} | 6 ++++-- compiler/obc/c/cgen.ml | 6 +++--- lib/c/pervasives.h | 12 ++++++++++++ 3 files changed, 19 insertions(+), 5 deletions(-) rename compiler/obc/c/{async_posix.ml => async_avr.ml} (86%) diff --git a/compiler/obc/c/async_posix.ml b/compiler/obc/c/async_avr.ml similarity index 86% rename from compiler/obc/c/async_posix.ml rename to compiler/obc/c/async_avr.ml index a9d4f69..ff7baab 100644 --- a/compiler/obc/c/async_posix.ml +++ b/compiler/obc/c/async_avr.ml @@ -3,7 +3,7 @@ open C open Obc open Async_backend -module PosixBackend : AsyncBackend = +module AvrBackend : AsyncBackend = struct let qn_append q suffix = { qual = q.qual; name = q.name ^ suffix } @@ -15,7 +15,9 @@ struct (* TODO(Arduino): add const qualifier *) let arg_ty = Cty_ptr (Cty_id (qn_append cd.cd_name suffix)) in let sizeof = Cfun_call ("sizeof", [Cderef (Cvar "src")]) in - let memcpy = Cfun_call ("memcpy", [Cvar "dest"; Cvar "src"; sizeof]) in + let memcpy = + Cfun_call ("atomic_memcpy", [Cvar "dest"; Cvar "src"; sizeof]) + in Cfundef { C.f_name = func_name; f_retty = Cty_void; diff --git a/compiler/obc/c/cgen.ml b/compiler/obc/c/cgen.ml index 87e4327..953a934 100644 --- a/compiler/obc/c/cgen.ml +++ b/compiler/obc/c/cgen.ml @@ -34,7 +34,7 @@ open Idents open Obc open Obc_utils open Types -open Async_posix +open Async_avr open Modules open Signature @@ -891,8 +891,8 @@ let cdefs_and_cdecls_of_class_def cd = let step_fun_def = 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 *) - let copy_in_def = PosixBackend.gen_copy_func_in cd in - let copy_out_def = PosixBackend.gen_copy_func_out cd in + let copy_in_def = AvrBackend.gen_copy_func_in cd in + let copy_out_def = AvrBackend.gen_copy_func_out cd in let async_step_fun_def = async_fun_def_of_step_fun cd.cd_name cd.cd_objs cd.cd_mems cd.cd_objs step_m (cdef_name copy_in_def) (cdef_name copy_out_def) in diff --git a/lib/c/pervasives.h b/lib/c/pervasives.h index f6d197e..0b4db76 100644 --- a/lib/c/pervasives.h +++ b/lib/c/pervasives.h @@ -31,6 +31,10 @@ #ifndef DECADES_PERVASIVES_H #define DECADES_PERVASIVES_H +#include +#ifdef __AVR__ +#include +#endif typedef float real; @@ -41,5 +45,13 @@ static inline int between(int idx, int n) return o; } +#ifdef __AVR__ +static inline void atomic_memcpy(void *dest, const void *src, size_t size) { + ATOMIC_BLOCK(ATOMIC_FORCEON) { + memcpy(dest, src, size); + } +} +#endif + #endif