Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
rpkak committed Mar 30, 2024
1 parent 4965ca1 commit 781d5fb
Show file tree
Hide file tree
Showing 6 changed files with 60 additions and 32 deletions.
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ add_library(ptlang_eval OBJECT

target_include_directories(ptlang_eval
PRIVATE ${PTLANG_AST_SRC_DIR}
# PRIVATE ${PTLANG_MAIN_SRC_DIR}
PRIVATE ${PTLANG_MAIN_SRC_DIR}
PRIVATE ${PTLANG_IR_BUILDER_SRC_DIR}
PRIVATE ${PTLANG_UTILS_SRC_DIR}
PUBLIC ${PTLANG_STB_DIR}
Expand Down
5 changes: 4 additions & 1 deletion src/ptlang_eval/ptlang_eval.c
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#include "ptlang_eval_impl.h"

ptlang_ast_exp ptlang_eval_const_exp(ptlang_ast_exp exp)
ptlang_ast_exp ptlang_eval_const_exp(ptlang_ast_exp exp, ptlang_context *ctx)
{
LLVMContextRef C = LLVMContextCreate();
LLVMModuleRef M = LLVMModuleCreateWithNameInContext("ptlang_eval", C);
Expand All @@ -20,12 +20,15 @@ ptlang_ast_exp ptlang_eval_const_exp(ptlang_ast_exp exp)
.builder = B,
.module = M,
.function = function,
.target_info = ctx->target_data_layout,
};
LLVMPositionBuilderAtEnd(B, entry);

LLVMValueRef value = ptlang_ir_builder_exp(exp, &cxt);
LLVMBuildStore(B, value, LLVMGetParam(function, 0));
LLVMBuildRetVoid(B);

LLVMDumpModule(M);
LLVMLinkInInterpreter();

LLVMExecutionEngineRef ee;
Expand Down
3 changes: 2 additions & 1 deletion src/ptlang_eval/ptlang_eval.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#pragma once

#include "ptlang_ast_nodes.h"
#include "ptlang_context.h"
#include <stdint.h>

// typedef ptlang_eval_value;
Expand All @@ -15,5 +16,5 @@

// LLVMValueRef ptlang_eval_byte_array_to_llvm(ptlang_eval_value val);

ptlang_ast_exp ptlang_eval_const_exp(ptlang_ast_exp exp);
ptlang_ast_exp ptlang_eval_const_exp(ptlang_ast_exp exp, ptlang_context *ctx);
uint32_t ptlang_eval_calc_byte_size(ptlang_ast_type type);
40 changes: 32 additions & 8 deletions src/ptlang_ir_builder/ptlang_ir_builder.c
Original file line number Diff line number Diff line change
Expand Up @@ -1685,25 +1685,49 @@ LLVMValueRef ptlang_ir_builder_exp(ptlang_ast_exp exp, ptlang_ir_builder_build_c
: exp->ast_type->content.float_size;
uint32_t byte_size = (bit_size - 1) / 8 + 1;

LLVMValueRef *bytes = ptlang_malloc(sizeof(LLVMValueRef) * byte_size);
// LLVMValueRef *bytes = ptlang_malloc(sizeof(LLVMValueRef) * byte_size);

LLVMTypeRef byte = LLVMInt8Type();
// LLVMTypeRef byte = LLVMInt8Type();

LLVMTypeRef type = LLVMIntType(bit_size);

LLVMValueRef value = LLVMConstInt(type, 0, false);

enum LLVMByteOrdering byteOrdering = LLVMByteOrder(ctx->target_info);

for (uint32_t i = 0; i < byte_size; i++)
{
bytes[i] = LLVMConstInt(byte, exp->content.binary[i], false);
value = LLVMConstShl(value, LLVMConstInt(type, 1, false));
value = LLVMConstOr(
value,
LLVMConstInt(type, exp->content.binary[byteOrdering == LLVMBigEndian ? i : byte_size - i - 1],
false));
// LLVMConstString()
// bytes[i] = LLVMConstInt(type, exp->content.binary[i], false);
}

LLVMValueRef as_array = LLVMConstArray(LLVMArrayType(byte, byte_size), bytes, byte_size);
// LLVMValueRef as_array = LLVMConstArray(LLVMArrayType2(byte, byte_size), bytes, byte_size);

// // // // // // // // LLVMValueRef *bytes = ptlang_malloc(sizeof(LLVMValueRef) * byte_size);

ptlang_free(bytes);
// // // // // // // // LLVMTypeRef byte = LLVMInt8Type();

LLVMValueRef as_int = LLVMConstBitCast(as_array, LLVMIntType(byte_size * 8));
// // // // // // // // for (uint32_t i = 0; i < byte_size; i++)
// // // // // // // // {
// // // // // // // // bytes[i] = LLVMConstInt(byte, exp->content.binary[i], false);
// // // // // // // // }

// // // // // // // LLVMValueRef as_array = LLVMConstString("fsafd", 5, true);
// // // // // // // // LLVMConstArray(LLVMArrayType(byte, byte_size), bytes, byte_size);

// // // // // // // // ptlang_free(bytes);

// LLVMValueRef as_int = LLVMConstBitCast(as_array, LLVMIntType(byte_size * 8));
if (bit_size != byte_size * 8)
{
as_int = LLVMConstTrunc(as_int, LLVMIntType(bit_size));
value = LLVMConstTrunc(value, LLVMIntType(bit_size));
}
return as_int;
return value;
}
}
abort();
Expand Down
40 changes: 20 additions & 20 deletions src/ptlang_main/ptlang_rc.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,36 +45,36 @@

#endif

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


void use(void *anything);
void use(void *anything) { printf("%p\n", anything); }
// void use(void *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);
// void remove_int(int *a) { printf("remove int %d\n", *a); }

int main(void)
{
intref ref, otherref;
ptlang_rc_alloc(ref);
// int main(void)
// {
// 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);
// otherref = ptlang_rc_add_ref(ref);

// otherref = ((*ref)->ref_count += 1, *(uint8_t *)malloc(sizeof(*ref)) = *ref);
// // 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(otherref) = 1;
// ptlang_rc_deref(otherref) = 1;

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

return 0;
}
// return 0;
// }
2 changes: 1 addition & 1 deletion src/ptlang_verify/ptlang_verify.c
Original file line number Diff line number Diff line change
Expand Up @@ -2198,7 +2198,7 @@ static ptlang_ast_exp ptlang_verify_eval(ptlang_ast_exp exp, enum ptlang_verify_
{

if (evaluated == NULL)
evaluated = ptlang_eval_const_exp(substituted);
evaluated = ptlang_eval_const_exp(substituted, ctx);

if (substituted != NULL)
{
Expand Down

0 comments on commit 781d5fb

Please sign in to comment.