Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
rpkak committed Feb 17, 2024
1 parent f83b028 commit 285775a
Show file tree
Hide file tree
Showing 8 changed files with 317 additions and 125 deletions.
2 changes: 1 addition & 1 deletion build.zig
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
const std = @import("std");

const c_flags = [_][]const u8{};
const c_flags = [_][]const u8{ "-lasan", "-fsanitize=address" };

// Although this function looks imperative, note that its job is to
// declaratively construct a build graph that will be executed by an external
Expand Down
4 changes: 2 additions & 2 deletions src/ptlang_ast/ptlang_ast.c
Original file line number Diff line number Diff line change
Expand Up @@ -943,7 +943,7 @@ void ptlang_ast_type_list_destroy(ptlang_ast_type *type_list)

void ptlang_ast_exp_list_destroy(ptlang_ast_exp *exp_list)
{
for (size_t i = 0; i < arrlenu(exp_list); i++)
for (size_t i = 0; i < stbds_arrlenu(exp_list); i++)
{
ptlang_ast_exp_destroy(exp_list[i]);
}
Expand All @@ -963,7 +963,7 @@ void ptlang_ast_struct_member_list_destroy(ptlang_ast_struct_member_list member_

ptlang_ast_decl ptlang_decl_list_find_last(ptlang_ast_decl *decl_list, char *name)
{
for (size_t i = arrlenu(decl_list) - 1; i >= 0; i--)
for (size_t i = arrlenu(decl_list) - 1; i != -1; i--)
{
if (0 == strcmp(decl_list[i]->name.name, name))
{
Expand Down
20 changes: 16 additions & 4 deletions src/ptlang_eval/ptlang_eval.c
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,9 @@ ptlang_ast_exp ptlang_eval_const_exp(ptlang_ast_exp exp)
LLVMLinkInInterpreter();

LLVMExecutionEngineRef ee;
LLVMCreateExecutionEngineForModule(&ee, M, NULL);
// LLVMCreateExecutionEngineForModule(&ee, M, NULL);
// LLVMCreateJITCompilerForModule(&ee, M, )
LLVMCreateInterpreterForModule(&ee, M, NULL);

uint32_t bit_size = exp->ast_type->type == PTLANG_AST_TYPE_INTEGER ? exp->ast_type->content.integer.size
: exp->ast_type->content.float_size;
Expand All @@ -40,10 +42,20 @@ ptlang_ast_exp ptlang_eval_const_exp(ptlang_ast_exp exp)

LLVMGenericValueRef in_llvm_binary = LLVMCreateGenericValueOfPointer(binary);
LLVMRunFunction(ee, function, 1, &in_llvm_binary);
LLVMDisposeExecutionEngine(ee);
// LLVMDisposeExecutionEngine(ee);

LLVMDisposeModule(M);
LLVMContextDispose(C);
// LLVMDisposeModule(M);
// LLVMContextDispose(C);

return ptlang_ast_exp_binary_new(binary, exp);
}

uint32_t ptlang_eval_calc_byte_size(ptlang_ast_type type)
{
// uint32_t bit_size =
// type->type == PTLANG_AST_TYPE_INTEGER ? type->content.integer.size : type->content.float_size;
return (((type->type == PTLANG_AST_TYPE_INTEGER ? type->content.integer.size : type->content.float_size) -
1) >>
3) +
1;
}
1 change: 1 addition & 0 deletions src/ptlang_eval/ptlang_eval.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,4 @@
// LLVMValueRef ptlang_eval_byte_array_to_llvm(ptlang_eval_value val);

ptlang_ast_exp ptlang_eval_const_exp(ptlang_ast_exp exp);
uint32_t ptlang_eval_calc_byte_size(ptlang_ast_type type);
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 @@ -387,7 +387,7 @@ static ptlang_ast_type ptlang_ir_builder_type_copy_and_unname(ptlang_ast_type ty

static ptlang_ast_type ptlang_ir_builder_exp_type(ptlang_ast_exp exp, ptlang_ir_builder_build_context *ctx)
{
return exp->ast_type;
return ptlang_ast_type_copy(exp->ast_type);
switch (exp->type)
{
case PTLANG_AST_EXP_ASSIGNMENT:
Expand Down
107 changes: 107 additions & 0 deletions src/ptlang_main/ptlang_rc.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
#include "ptlang_utils.h"
#include <stddef.h>

#ifdef NDEBUG

# define PTLANG_RC_DEFINE_REF_TYPE(type, name) \
typedef struct \
{ \
size_t ref_count; \
type content; \
} *name;

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

# define ptlang_rc_add_ref(ref, new_ref) \
{ \
ref->ref_count += 1; \
new_ref = 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); \
} \
}

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

#else

# define PTLANG_RC_DEFINE_REF_TYPE(type, name) \
typedef struct \
{ \
size_t ref_count; \
type content; \
} **name;

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

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

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

#endif

PTLANG_RC_DEFINE_REF_TYPE(int, intref);

#include <stdio.h>

void use(void *anything) { printf("%p\n", anything); }

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

int main()
{
intref ref, otherref;
ptlang_rc_alloc(ref);

ptlang_rc_deref(ref) = 1;

use(&ptlang_rc_deref(ref));

ptlang_rc_add_ref(ref, otherref);



use(ref);

ptlang_rc_remove_ref(ref, remove_int);


ptlang_rc_deref(ref) = 1;

ptlang_rc_remove_ref(otherref, remove_int);


return 0;
}
Loading

0 comments on commit 285775a

Please sign in to comment.