Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
rpkak committed Mar 16, 2024
1 parent a9ac4d8 commit 4965ca1
Show file tree
Hide file tree
Showing 6 changed files with 492 additions and 397 deletions.
14 changes: 8 additions & 6 deletions src/ptlang_ast/ptlang_ast.c
Original file line number Diff line number Diff line change
Expand Up @@ -1067,22 +1067,24 @@ ptlang_ast_exp ptlang_ast_exp_copy(ptlang_ast_exp exp)
case PTLANG_AST_EXP_STRUCT:
{
copy->content.struct_.type = ptlang_ast_ident_copy(exp->content.struct_.type);
copy->content.struct_.members = NULL;
for (size_t i = 0; i < arrlenu(exp->content.struct_.members); i++)
{
arrpush(copy->content.struct_.members,
((struct ptlang_ast_struct_member_s){
.exp = ptlang_ast_exp_copy(exp->content.struct_.members[i].exp),
.pos = ptlang_ast_code_position_copy(exp->content.struct_.members[i].pos),
.str = ptlang_ast_ident_copy(exp->content.struct_.members[i].str)}));
arrput(copy->content.struct_.members,
((struct ptlang_ast_struct_member_s){
.exp = ptlang_ast_exp_copy(exp->content.struct_.members[i].exp),
.pos = ptlang_ast_code_position_copy(exp->content.struct_.members[i].pos),
.str = ptlang_ast_ident_copy(exp->content.struct_.members[i].str)}));
}
break;
}
case PTLANG_AST_EXP_ARRAY:
{
copy->content.array.type = ptlang_ast_type_copy(exp->content.array.type);
copy->content.array.values = NULL;
for (size_t i = 0; i < arrlenu(exp->content.array.values); i++)
{
arrpush(copy->content.array.values, ptlang_ast_exp_copy(exp->content.array.values[i]));
arrput(copy->content.array.values, ptlang_ast_exp_copy(exp->content.array.values[i]));
}
break;
}
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 @@ -745,7 +745,7 @@ static void ptlang_ir_builder_prepare_binary_op(ptlang_ast_exp exp, LLVMValueRef
// ptlang_ast_type right_type = ptlang_ir_builder_exp_type(exp->content.binary_operator.right_value, ctx);
ptlang_ast_type right_type = exp->content.binary_operator.right_value->ast_type;
// *ret_type = ptlang_ir_builder_combine_types(left_type, right_type, ctx->type_scope);
*ret_type = exp->ast_type;
*ret_type = ptlang_ast_type_copy(exp->ast_type);
LLVMValueRef left_value_uncasted = ptlang_ir_builder_exp(exp->content.binary_operator.left_value, ctx);
*left_value = ptlang_ir_builder_build_cast(left_value_uncasted, left_type, *ret_type, ctx);
LLVMValueRef right_value_uncasted = ptlang_ir_builder_exp(exp->content.binary_operator.right_value, ctx);
Expand Down
93 changes: 35 additions & 58 deletions src/ptlang_main/ptlang_rc.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#include "ptlang_utils.h"
#include <stddef.h>
#include <string.h>

#ifdef NDEBUG

Expand All @@ -10,29 +10,15 @@
type content; \
} *name;

# define ptlang_rc_alloc(ref) \
{ \
ref = ptlang_malloc(sizeof(*ref)); \
ref->ref_count = 1; \
}
# define ptlang_rc_alloc(ref) (void)((ref) = ptlang_malloc(sizeof(*(ref))), (ref)->ref_count = 0)

# define ptlang_rc_add_ref(ref, new_ref) \
{ \
ref->ref_count += 1; \
new_ref = ref; \
}
# define ptlang_rc_add_ref(ref) ((ref)->ref_count += 1, (ref))

# define ptlang_rc_remove_ref(ref, destroy_fn) \
{ \
ref->ref_count -= 1; \
if (ref->ref_count == 0) \
{ \
destroy_fn(&ref->content); \
ptlang_free(ref); \
} \
}
(((ref)->ref_count == 0) ? (destroy_fn(&(ref)->content), ptlang_free(ref)) \
: (void)((ref)->ref_count -= 1))

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

#else

Expand All @@ -44,60 +30,51 @@
} **name;

# define ptlang_rc_alloc(ref) \
{ \
ref = ptlang_malloc(sizeof(*ref)); \
*ref = ptlang_malloc(sizeof(**ref)); \
(*ref)->ref_count = 1; \
}
(void)((ref) = ptlang_malloc(sizeof(*(ref))), *(ref) = ptlang_malloc(sizeof(**(ref))), \
(*(ref))->ref_count = 0)

# define ptlang_rc_add_ref(ref, new_ref) \
{ \
(*ref)->ref_count += 1; \
new_ref = ptlang_malloc(sizeof(*new_ref)); \
*new_ref = *ref; \
}
# define ptlang_rc_add_ref(ref) \
((*(ref))->ref_count += 1, memcpy(ptlang_malloc(sizeof(*(ref))), (ref), sizeof(*(ref))))

# define ptlang_rc_remove_ref(ref, destroy_fn) \
{ \
(*ref)->ref_count -= 1; \
if ((*ref)->ref_count == 0) \
{ \
destroy_fn(&(*ref)->content); \
ptlang_free(*ref); \
} \
ptlang_free(ref); \
}
(((*(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)
# define ptlang_rc_deref(ref) ((*(ref))->content)

#endif

// PTLANG_RC_DEFINE_REF_TYPE(int, intref);
#include <stdio.h>
PTLANG_RC_DEFINE_REF_TYPE(int, intref)


// #include <stdio.h>
void use(void *anything);
void use(void *anything) { printf("%p\n", anything); }

// void use(void *anything) { printf("%p\n", anything); }
void remove_int(int *a);
void remove_int(int *a) { printf("remove int %d\n", *a); }

// void remove_int(int *a) { printf("remove int %d\n", *a); }
int main(void)
{
intref ref, otherref;
ptlang_rc_alloc(ref);

// int main()
// {
// intref ref, otherref;
// ptlang_rc_alloc(ref);
ptlang_rc_deref(ref) = 1;

// ptlang_rc_deref(ref) = 1;
use(&ptlang_rc_deref(ref));

// use(&ptlang_rc_deref(ref));
otherref = ptlang_rc_add_ref(ref);

// ptlang_rc_add_ref(ref, otherref);
// otherref = ((*ref)->ref_count += 1, *(uint8_t *)malloc(sizeof(*ref)) = *ref);

// use(ref);
use(ref);

// ptlang_rc_remove_ref(ref, remove_int);
ptlang_rc_remove_ref(ref, remove_int);

// ptlang_rc_deref(ref) = 1;
ptlang_rc_deref(otherref) = 1;

// ptlang_rc_remove_ref(otherref, remove_int);
ptlang_rc_remove_ref(otherref, remove_int);

// return 0;
// }
return 0;
}
6 changes: 3 additions & 3 deletions src/ptlang_utils/ptlang_utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@
#include <stdbool.h>
#include <stdlib.h>

#define ptlang_malloc(size) (size) == 0 ? NULL : malloc(size)
#define ptlang_malloc_zero(size) (size) == 0 ? NULL : memset(ptlang_malloc(size), 0, (size))
#define ptlang_malloc(size) ((size) == 0 ? NULL : malloc(size))
#define ptlang_malloc_zero(size) ((size) == 0 ? NULL : memset(ptlang_malloc(size), 0, (size)))
#define ptlang_free(ptr) free(ptr)
#define ptlang_realloc(ptr, size) (size) == 0 ? (free(ptr), NULL) : realloc(ptr, (size))
#define ptlang_realloc(ptr, size) ((size) == 0 ? (free(ptr), NULL) : realloc(ptr, (size)))

#ifdef NDEBUG
# define ptlang_assert(val) ((void)val)
Expand Down
Loading

0 comments on commit 4965ca1

Please sign in to comment.