Add basic copy functions

Warning: These functions are not yet atomic.
This commit is contained in:
jeltz 2020-12-22 00:03:08 +01:00
parent 5376d9993e
commit 34902b58f0
Signed by: jeltz
GPG key ID: 800882B66C0C3326
3 changed files with 48 additions and 1 deletions

View file

@ -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

View file

@ -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

View file

@ -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