Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
rpkak committed Dec 2, 2023
1 parent bf99040 commit 77449a7
Show file tree
Hide file tree
Showing 10 changed files with 465 additions and 123 deletions.
14 changes: 14 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ set(PTLANG_MAIN_SRC_DIR ${PTLANG_SRC_DIR}/ptlang_main)
set(PTLANG_IR_BUILDER_SRC_DIR ${PTLANG_SRC_DIR}/ptlang_ir_builder)
set(PTLANG_VERIFY_SRC_DIR ${PTLANG_SRC_DIR}/ptlang_verify)
set(PTLANG_UTILS_SRC_DIR ${PTLANG_SRC_DIR}/ptlang_utils)
set(PTLANG_EVAL_SRC_DIR ${PTLANG_SRC_DIR}/ptlang_eval)

set(PTLANG_THIRD_PARTY_DIR ${PROJECT_SOURCE_DIR}/third-party)
set(PTLANG_STB_DIR ${PTLANG_THIRD_PARTY_DIR}/stb)
Expand Down Expand Up @@ -105,6 +106,19 @@ target_include_directories(ptlang_verify
PUBLIC ${PTLANG_STB_DIR}
)


add_library(ptlang_eval OBJECT
${PTLANG_EVAL_SRC_DIR}/ptlang_eval.c)


target_include_directories(ptlang_eval
PRIVATE ${PTLANG_AST_SRC_DIR}
# PRIVATE ${PTLANG_MAIN_SRC_DIR}
PRIVATE ${PTLANG_IR_BUILDER_SRC_DIR}
PRIVATE ${PTLANG_UTILS_SRC_DIR}
PUBLIC ${PTLANG_STB_DIR}
)

# llvm_map_components_to_libnames(llvm_libs support core irreader)


Expand Down
23 changes: 23 additions & 0 deletions src/ptlang_ast/ptlang_ast.c
Original file line number Diff line number Diff line change
Expand Up @@ -482,6 +482,18 @@ ptlang_ast_exp ptlang_ast_exp_reference_new(bool writable, ptlang_ast_exp value,

UNARY_OP(dereference, DEREFERENCE)

ptlang_ast_exp ptlang_ast_exp_binary_new(uint8_t *binary, ptlang_ast_exp prev)
{
ptlang_ast_exp exp = ptlang_malloc(sizeof(struct ptlang_ast_exp_s));
*exp = (struct ptlang_ast_exp_s){
.type = PTLANG_AST_EXP_BINARY,
.content.binary = binary,
.pos = prev->pos,
.ast_type = prev->ast_type,
};
return exp;
}

ptlang_ast_stmt ptlang_ast_stmt_block_new(ptlang_ast_code_position pos)
{
ptlang_ast_stmt stmt = ptlang_malloc(sizeof(struct ptlang_ast_stmt_s));
Expand Down Expand Up @@ -972,3 +984,14 @@ ptlang_ast_code_position ptlang_ast_code_position_copy(ptlang_ast_code_position
});
return new_pos;
}

ptlang_ast_ident ptlang_ast_ident_copy(ptlang_ast_ident ident)
{
size_t name_len = strlen(ident.name) + 1;
ptlang_ast_ident new_ident = {
.name = ptlang_malloc(name_len),
.pos = ptlang_ast_code_position_copy(ident.pos),
};
memcpy(new_ident.name, ident.name, name_len);
return new_ident;
}
1 change: 1 addition & 0 deletions src/ptlang_ast/ptlang_ast.h
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,7 @@ ptlang_ast_exp ptlang_ast_exp_array_element_new(ptlang_ast_exp array, ptlang_ast
ptlang_ast_exp ptlang_ast_exp_reference_new(bool writable, ptlang_ast_exp value,
ptlang_ast_code_position pos);
ptlang_ast_exp ptlang_ast_exp_dereference_new(ptlang_ast_exp value, ptlang_ast_code_position pos);
ptlang_ast_exp ptlang_ast_exp_binary_new(uint8_t* binary, ptlang_ast_exp prev);

ptlang_ast_stmt ptlang_ast_stmt_block_new(ptlang_ast_code_position pos);
void ptlang_ast_stmt_block_add_stmt(ptlang_ast_stmt block_stmt, ptlang_ast_stmt stmt);
Expand Down
59 changes: 59 additions & 0 deletions src/ptlang_eval/ptlang_eval.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
#include "ptlang_eval_impl.h"

ptlang_ast_exp ptlang_eval_const_exp(ptlang_ast_exp exp)
{
LLVMContextRef C = LLVMContextCreate();
LLVMModuleRef M = LLVMModuleCreateWithNameInContext("ptlang_eval", C);

LLVMTypeRef type = ptlang_ir_builder_type(exp->ast_type, NULL, C);

LLVMTypeRef func_type =
LLVMFunctionType(LLVMVoidTypeInContext(C), (LLVMTypeRef[]){LLVMPointerTypeInContext(C, 0)}, 1, false);

LLVMValueRef function = LLVMAddFunction(M, "main", func_type);

LLVMTypeRef size_func_type = LLVMFunctionType(LLVMInt64TypeInContext(C), NULL, 0, false);

LLVMValueRef size_func = LLVMAddFunction(M, "size", size_func_type);

LLVMBuilderRef B = LLVMCreateBuilderInContext(C);

LLVMBasicBlockRef size_entry = LLVMAppendBasicBlockInContext(C, size_func, "size_entry");

LLVMPositionBuilderAtEnd(B, size_entry);
LLVMValueRef size_value = LLVMSizeOf(type);
LLVMBuildRet(B, size_value);

LLVMBasicBlockRef entry = LLVMAppendBasicBlockInContext(C, function, "entry");

ptlang_ir_builder_build_context cxt = {
.builder = B,
.module = M,
.function = function,
};
LLVMPositionBuilderAtEnd(B, entry);

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

LLVMExecutionEngineRef ee;
LLVMCreateExecutionEngineForModule(&ee, M, NULL);

LLVMGenericValueRef in_llvm_size = LLVMRunFunction(ee, size_func, 0, NULL);
unsigned long long size = LLVMGenericValueToInt(in_llvm_size, false);

uint8_t *binary = NULL;

arrsetlen(binary, size);

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

LLVMDisposeModule(M);
LLVMContextDispose(C);

return ptlang_ast_exp_binary_new(binary, exp);
}
18 changes: 18 additions & 0 deletions src/ptlang_eval/ptlang_eval.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#pragma once

#include "ptlang_ast_impl.h"
#include <stdint.h>

// typedef ptlang_eval_value;

// typedef struct ptlang_eval_var_s
// {
// char *key;
// ptlang_eval_value value;
// } ptlang_eval_var;

// ptlang_eval_value ptlang_eval_const_exp(ptlang_ast_exp exp, ptlang_ast_type type, ptlang_eval_var *vars);

// LLVMValueRef ptlang_eval_byte_array_to_llvm(ptlang_eval_value val);

ptlang_ast_exp ptlang_eval_const_exp(ptlang_ast_exp exp);
14 changes: 14 additions & 0 deletions src/ptlang_eval/ptlang_eval_impl.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#pragma once

#include "ptlang_eval.h"
#include "ptlang_ir_builder.h"
#include "ptlang_utils.h"

#include <llvm-c/Core.h>
#include <llvm-c/ExecutionEngine.h>

// typedef struct ptlang_eval_value_s
// {
// uint8_t *data;
// ptlang_ast_type type;
// } ptlang_eval_value;
Loading

0 comments on commit 77449a7

Please sign in to comment.