Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
rpkak committed Apr 13, 2024
1 parent e962877 commit bb47d22
Show file tree
Hide file tree
Showing 4 changed files with 99 additions and 53 deletions.
8 changes: 6 additions & 2 deletions src/ptlang_ast/ptlang_ast.c
Original file line number Diff line number Diff line change
Expand Up @@ -515,8 +515,8 @@ ptlang_ast_exp ptlang_ast_exp_binary_new(uint8_t *binary, ptlang_ast_exp prev)
ptlang_rc_deref(exp) = (struct ptlang_ast_exp_s){
.type = PTLANG_AST_EXP_BINARY,
.content.binary = binary,
.pos = ptlang_rc_deref(prev).pos,
.ast_type = ptlang_rc_deref(prev).ast_type,
.pos = ptlang_rc_add_ref(ptlang_rc_deref(prev).pos),
.ast_type = ptlang_rc_add_ref(ptlang_rc_deref(prev).ast_type),
};
return exp;
}
Expand Down Expand Up @@ -946,7 +946,11 @@ void ptlang_ast_decl_destroy(struct ptlang_ast_decl_s *decl)
if (decl->pos != NULL)
ptlang_rc_remove_ref(decl->pos);
if (decl->type != NULL)
{

printf("refco: %p %d\n",*decl->type, (*(decl->type))->ref_count);
ptlang_rc_remove_ref(decl->type, ptlang_ast_type_destroy);
}
if (decl->init != NULL)
{
ptlang_rc_remove_ref(decl->init, ptlang_ast_exp_destroy);
Expand Down
2 changes: 1 addition & 1 deletion src/ptlang_ir_builder/ptlang_ir_builder.c
Original file line number Diff line number Diff line change
Expand Up @@ -411,7 +411,7 @@ static void ptlang_ir_builder_free(LLVMValueRef heap_arr, LLVMTypeRef element_ty

static ptlang_ast_type ptlang_ir_builder_exp_type(ptlang_ast_exp exp, ptlang_ir_builder_build_context *ctx)
{
return ptlang_rc_deref(exp).ast_type;
return ptlang_rc_add_ref(ptlang_rc_deref(exp).ast_type);
switch (ptlang_rc_deref(exp).type)
{
case PTLANG_AST_EXP_ASSIGNMENT:
Expand Down
31 changes: 27 additions & 4 deletions src/ptlang_main/ptlang_rc.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,23 +26,46 @@
# define ptlang_rc_deref(ref) ((ref)->content)

#else
# include <stdint.h>

void **ptlang_rc_add_ref_func(char *function, char *file, uint64_t line, void **ref);

# define PTLANG_RC_DEFINE_REF_TYPE_ONLY(name) typedef struct name##_ref_struct **name

# define ptlang_rc_alloc(ref) \
(void)((ref) = ptlang_malloc(sizeof(*(ref))), *(ref) = ptlang_malloc(sizeof(**(ref))), \
(*(ref))->ref_count = 0)
(*(ref))->ref_count = 0, \
printf("allo ar inner: %p outer: %p method: %s file: " __FILE__ ":%d\n", *ref, ref, \
__FUNCTION__, __LINE__))

// # define ptlang_rc_add_ref(ref) \
// (printf("ar inner: %p outer: %p method: %s file: " __FILE__ ":%d\n", *ref, ref, __FUNCTION__, \
// __LINE__), \
// (*(ref))->ref_count += 1, memcpy(ptlang_malloc(sizeof(*(ref))), (ref), sizeof(*(ref))))

# define ptlang_rc_add_ref(ref) \
((*(ref))->ref_count += 1, memcpy(ptlang_malloc(sizeof(*(ref))), (ref), sizeof(*(ref))))
# define ptlang_rc_add_ref(ref) ptlang_rc_add_ref_func(__FUNCTION__, __FILE__, __LINE__, (ref))

# define ptlang_rc_remove_ref_with_fn(ref, destroy_fn, ...) \
(((*(ref))->ref_count == 0) ? (destroy_fn(&(*(ref))->content), ptlang_free(*(ref))) \
(printf("%srr inner: %p outer: %p method: %s file: " __FILE__ ":%d\n", \
((*(ref))->ref_count == 0) ? "del " : "", *ref, ref, __FUNCTION__, __LINE__), \
((*(ref))->ref_count == 0) ? (destroy_fn(&(*(ref))->content), ptlang_free(*(ref))) \
: (void)((*(ref))->ref_count -= 1), \
ptlang_free(ref))

# define ptlang_rc_deref(ref) ((*(ref))->content)

# ifdef asdf
# include <stdio.h>
void **ptlang_rc_add_ref_func(char *function, char *file, uint64_t line, void **ref)
{
void **new_ref = memcpy(ptlang_malloc(sizeof(*(ref))), (ref), sizeof(*(ref)));
*((size_t *)(*(ref))) += 1;
;
printf("ar inner: %p outer: %p method: %s file: %s:%d\n", *ref, new_ref, function, file, line);
return new_ref;
}
# endif

#endif

#define ptlang_rc_remove_ref(...) ptlang_rc_remove_ref_with_fn(__VA_ARGS__, (void), i r g e n d e t w a s)
Expand Down
Loading

0 comments on commit bb47d22

Please sign in to comment.