From cee3515218912233b7da74e649a7957a109e5b76 Mon Sep 17 00:00:00 2001 From: Matthew Leach Date: Wed, 10 Feb 2021 20:56:25 +0000 Subject: [PATCH] [lang] add a tree type that represents an FFI closure We will be using FFI closures for allowing function pointers to be created. Add a tree type that allows us to track the use of FFI allocated memory and GC it when needed. --- src/c.lang | 7 +++++++ src/gc.c | 4 ++++ src/tree-dump-primitives.h | 11 +++++++++++ src/tree.h | 1 + 4 files changed, 23 insertions(+) diff --git a/src/c.lang b/src/c.lang index 63458b2..361fa40 100644 --- a/src/c.lang +++ b/src/c.lang @@ -6,6 +6,8 @@ (ptr "void *") (sz "size_t") (flag "bool") + (ffi_cif "ffi_cif *") + (ffi_closure "ffi_closure *") (comp_decl_type "compound_type") (live_var_val "union value *")) @@ -184,6 +186,11 @@ (deftype T_FN_CALL "Function Call" "tFNCALL" ("ID" "ARGS")) +(deftype T_CLOSURE "Closure Call" "tCLOSURE" + ("ARG_TYPES" "RET_TYPE" "FN" + ("CIF" ffi_cif) + ("CLOSURE" ffi_closure))) + (deftype T_FN_ARG "Function Argument" "tFNARG" ("EXP")) diff --git a/src/gc.c b/src/gc.c index 2d00420..e68ce52 100644 --- a/src/gc.c +++ b/src/gc.c @@ -101,6 +101,10 @@ static void dealloc_object(gc_obj obj) case E_ALLOC: free(tALLOC_PTR(t)); break; + case T_CLOSURE: + ffi_closure_free(tCLOSURE_CLOSURE(t)); + free(tCLOSURE_CIF(t)); + break; default: /* All other types don't contain any other referencies to * dynamic memory. */ diff --git a/src/tree-dump-primitives.h b/src/tree-dump-primitives.h index 8af1c43..b2a8d93 100644 --- a/src/tree-dump-primitives.h +++ b/src/tree-dump-primitives.h @@ -68,6 +68,17 @@ static inline void tree_dump_integer(tree t, mpz_t val, enum DUMP_TYPE dt) gmp_fprintf(stderr, "%Zd", val); } +static inline void tree_dump_ffi_closure(tree t, ffi_closure *closure, enum DUMP_TYPE dt) +{ + return; +} + +static inline void tree_dump_ffi_cif(tree t, ffi_cif *closure, + enum DUMP_TYPE dt) +{ + return; +} + static inline void tree_dump_comp_decl_type(tree t, compound_type v, enum DUMP_TYPE dt) { switch(v) { diff --git a/src/tree.h b/src/tree.h index c3b5118..38238a1 100644 --- a/src/tree.h +++ b/src/tree.h @@ -8,6 +8,7 @@ #include #include #include +#include #include "list.h" #include "tree-access.h"