Skip to content

Commit

Permalink
[lang] add a tree type that represents an FFI closure
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
hexagonal-sun committed Feb 10, 2021
1 parent 183f498 commit cee3515
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 0 deletions.
7 changes: 7 additions & 0 deletions src/c.lang
Original file line number Diff line number Diff line change
Expand Up @@ -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 *"))

Expand Down Expand Up @@ -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"))

Expand Down
4 changes: 4 additions & 0 deletions src/gc.c
Original file line number Diff line number Diff line change
Expand Up @@ -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. */
Expand Down
11 changes: 11 additions & 0 deletions src/tree-dump-primitives.h
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
1 change: 1 addition & 0 deletions src/tree.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#include <string.h>
#include <stdbool.h>
#include <setjmp.h>
#include <ffi.h>
#include "list.h"
#include "tree-access.h"

Expand Down

0 comments on commit cee3515

Please sign in to comment.