From 34902b58f0f4afc99e564f73de6e02d85200e0ca Mon Sep 17 00:00:00 2001 From: Tom Barthe Date: Tue, 22 Dec 2020 00:03:08 +0100 Subject: [PATCH] Add basic copy functions Warning: These functions are not yet atomic. --- compiler/obc/c/async_backend.mli | 8 ++++++++ compiler/obc/c/async_posix.ml | 32 ++++++++++++++++++++++++++++++++ compiler/obc/c/cgen.ml | 9 ++++++++- 3 files changed, 48 insertions(+), 1 deletion(-) create mode 100644 compiler/obc/c/async_backend.mli create mode 100644 compiler/obc/c/async_posix.ml diff --git a/compiler/obc/c/async_backend.mli b/compiler/obc/c/async_backend.mli new file mode 100644 index 0000000..90ce29b --- /dev/null +++ b/compiler/obc/c/async_backend.mli @@ -0,0 +1,8 @@ +open C +open Obc + +module type AsyncBackend = +sig + val gen_copy_func_in : class_def -> cdef + val gen_copy_func_out : class_def -> cdef +end diff --git a/compiler/obc/c/async_posix.ml b/compiler/obc/c/async_posix.ml new file mode 100644 index 0000000..a9d4f69 --- /dev/null +++ b/compiler/obc/c/async_posix.ml @@ -0,0 +1,32 @@ +open Names +open C +open Obc +open Async_backend + +module PosixBackend : AsyncBackend = +struct + let qn_append q suffix = + { qual = q.qual; name = q.name ^ suffix } + + (* 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 *) + 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 + Cfundef { + C.f_name = func_name; + f_retty = Cty_void; + f_args = [("dest", arg_ty); ("src", arg_ty)]; + f_body = { + var_decls = []; + block_body = [Csexpr memcpy] } + } + + let gen_copy_func_in cd = gen_copy_func cd "_in" + + let gen_copy_func_out cd = gen_copy_func cd "_out" + +end diff --git a/compiler/obc/c/cgen.ml b/compiler/obc/c/cgen.ml index 7be6e8d..03cf0cd 100644 --- a/compiler/obc/c/cgen.ml +++ b/compiler/obc/c/cgen.ml @@ -34,6 +34,7 @@ open Idents open Obc open Obc_utils open Types +open Async_posix open Modules open Signature @@ -801,13 +802,19 @@ let cdefs_and_cdecls_of_class_def cd = let out_struct_decl = out_decl_of_class_def cd in 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 (* C function for resetting our memory structure. *) let reset_fun_def = reset_fun_def_of_class_def cd in let res_fun_decl = cdecl_of_cfundef reset_fun_def in let step_fun_decl = cdecl_of_cfundef step_fun_def in + let copy_in_decl = cdecl_of_cfundef copy_in_def in + let copy_out_decl = cdecl_of_cfundef copy_out_def in let (decls, defs) = if is_stateful cd.cd_name then - ([res_fun_decl; step_fun_decl], [reset_fun_def; step_fun_def]) + ([res_fun_decl; step_fun_decl; copy_in_decl; copy_out_decl], + [reset_fun_def; step_fun_def; copy_in_def; copy_out_def]) else ([step_fun_decl], [step_fun_def]) in