Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
rpkak committed Dec 14, 2024
1 parent 9945ad9 commit 25b4a98
Show file tree
Hide file tree
Showing 6 changed files with 83 additions and 59 deletions.
29 changes: 24 additions & 5 deletions src/ptlang_eval/ptlang_eval.cc
Original file line number Diff line number Diff line change
Expand Up @@ -37,13 +37,29 @@ extern "C"
// };
ptlang_ir_builder_fun_ctx fun_ctx = {&ir_ctx, function, ir_ctx.scope};

uint32_t byte_size = ptlang_eval_calc_byte_size(ptlang_rc_deref(exp).ast_type);

// LLVMPositionBuilderAtEnd(B, entry);
ir_ctx.builder.SetInsertPointPastAllocas(fun_ctx.func);

// LLVMValueRef value = ptlang_ir_builder_exp(exp, &cxt);
llvm::Value *value = ptlang_ir_builder_exp(exp, &fun_ctx);
// LLVMBuildStore(B, value, LLVMGetParam(function, 0));
ir_ctx.builder.CreateStore(value, fun_ctx.func->getArg(0));
llvm::Constant *dyn_casted = llvm::dyn_cast<llvm::Constant>(value);
if (dyn_casted != NULL)
{
llvm::Value *global = new llvm::GlobalVariable(
type, true, llvm::GlobalValue::LinkageTypes::InternalLinkage, dyn_casted, "evaled_const");
// ir_ctx.builder.CreateMemCpy(fun_ctx.func->getArg(0), std::nullopt , global, std::nullopt,
// llvm::ConstantInt::get(ir_ctx.integer_ptrsize_type, byte_size, false));
ir_ctx.builder.CreateMemCpy(fun_ctx.func->getArg(0), std::nullopt, global, std::nullopt,
byte_size);
}
else
{
// llvm::dyn_cast
// LLVMBuildStore(B, value, LLVMGetParam(function, 0));
ir_ctx.builder.CreateStore(value, fun_ctx.func->getArg(0));
}

// LLVMBuildRetVoid(B);
ir_ctx.builder.CreateRetVoid();
Expand All @@ -53,9 +69,14 @@ extern "C"
// LLVMExecutionEngineRef ee;
// llvm::ExecutionEngine ee = llvm::ExecutionEngine::

#if 0
#if 1
ir_ctx.module_.print(llvm::dbgs(), NULL, false, true);
#endif
#ifndef NDEBUG
bool broken_debug_info;
ptlang_assert(!llvm::verifyModule(ir_ctx.module_, &llvm::dbgs(), &broken_debug_info));
ptlang_assert(!broken_debug_info);
#endif

// llvm::clone_module

Expand All @@ -81,8 +102,6 @@ extern "C"
// ? ptlang_rc_deref(ptlang_rc_deref(exp).ast_type).content.integer.size
// : ptlang_rc_deref(ptlang_rc_deref(exp).ast_type).content.float_size;

uint32_t byte_size = ptlang_eval_calc_byte_size(ptlang_rc_deref(exp).ast_type);

uint8_t *binary = (uint8_t *)memset(ptlang_malloc(byte_size), 0x6b, byte_size);

// arrsetlen(binary, size);
Expand Down
3 changes: 3 additions & 0 deletions src/ptlang_eval/ptlang_eval_impl.h
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
#pragma once

#include "ptlang_eval.h"
#include <llvm/IR/NoFolder.h>
#define FOLDER llvm::NoFolder
#include "ptlang_ir_builder_llvm.h"
#include "ptlang_utils.h"

#include <llvm/Support/Casting.h>
#include <llvm/ExecutionEngine/ExecutionEngine.h>
#include <llvm/ExecutionEngine/GenericValue.h>
#include <llvm/ExecutionEngine/Interpreter.h>
Expand Down
60 changes: 52 additions & 8 deletions src/ptlang_ir_builder/ptlang_ir_builder.cc
Original file line number Diff line number Diff line change
Expand Up @@ -768,6 +768,7 @@ extern "C"
if (ptlang_rc_deref(ptlang_rc_deref(exp).content.binary_operator.left_value).type !=
ptlang_ast_exp_s::PTLANG_AST_EXP_LENGTH)
{

llvm::Value *ptr =
ptlang_ir_builder_exp_ptr(ptlang_rc_deref(exp).content.binary_operator.left_value, ctx);
ctx->ctx->builder.CreateStore(val, ptr);
Expand Down Expand Up @@ -1197,8 +1198,8 @@ extern "C"
}
case ptlang_ast_exp_s::PTLANG_AST_EXP_ARRAY:
{
llvm::Value *arr = llvm::PoisonValue::get(
ptlang_ir_builder_type(ptlang_rc_deref(exp).content.array.type, ctx->ctx));
llvm::Value *arr =
llvm::PoisonValue::get(ptlang_ir_builder_type(ptlang_rc_deref(exp).ast_type, ctx->ctx));
for (size_t i = 0; i < arrlenu(ptlang_rc_deref(exp).content.array.values); i++)
{
arr = ctx->ctx->builder.CreateInsertValue(
Expand Down Expand Up @@ -1347,25 +1348,68 @@ extern "C"
{
if (ptlang_rc_deref(from).content.integer.size > ptlang_rc_deref(to).content.integer.size)
{
return ctx->builder.CreateTrunc(input, to_llvm, "cast_int_trunc");
return ctx->builder.CreateTrunc(input, to_llvm, "cast");
}
else if (ptlang_rc_deref(from).content.integer.size < ptlang_rc_deref(to).content.integer.size)
{
if (ptlang_rc_deref(from).content.integer.is_signed)
{
return ctx->builder.CreateSExt(input, to_llvm, "cast_int_signed_ext");
return ctx->builder.CreateSExt(input, to_llvm, "cast");
}
else
{
printf("hi\n");
return ctx->builder.CreateZExt(input, to_llvm, "cast_int_unsigned_ext");
return ctx->builder.CreateZExt(input, to_llvm, "cast");
}
}
else
return input;
}
// TODO else if ... (see src/ptlang_ir_builder_old/ptlang_ir_builder.c:723)
return NULL;
else if (ptlang_rc_deref(from).type == ptlang_ast_type_s::PTLANG_AST_TYPE_INTEGER &&
ptlang_rc_deref(to).type == ptlang_ast_type_s::PTLANG_AST_TYPE_FLOAT)
{
if (ptlang_rc_deref(from).content.integer.is_signed)
{
return ctx->builder.CreateSIToFP(input, to_llvm, "cast");
}
else
{

return ctx->builder.CreateUIToFP(input, to_llvm, "cast");
}
}
else if (ptlang_rc_deref(from).type == ptlang_ast_type_s::PTLANG_AST_TYPE_FLOAT &&
ptlang_rc_deref(to).type == ptlang_ast_type_s::PTLANG_AST_TYPE_INTEGER)
{

if (ptlang_rc_deref(to).content.integer.is_signed)
{
return ctx->builder.CreateFPToSI(input, to_llvm, "cast");
}
else
{
return ctx->builder.CreateFPToUI(input, to_llvm, "cast");
}
}
else if (ptlang_rc_deref(from).type == ptlang_ast_type_s::PTLANG_AST_TYPE_FLOAT &&
ptlang_rc_deref(to).type == ptlang_ast_type_s::PTLANG_AST_TYPE_FLOAT)
{
if (ptlang_rc_deref(from).content.float_size < ptlang_rc_deref(to).content.float_size)
{
return ctx->builder.CreateFPExt(input, to_llvm, "cast");
}
else if (ptlang_rc_deref(from).content.float_size > ptlang_rc_deref(to).content.float_size)
{
return ctx->builder.CreateFPTrunc(input, to_llvm, "cast");
}
else
{
return input;
}
}
else
{
return input;
}
}

static llvm::Value *ptlang_ir_builder_exp_ptr(ptlang_ast_exp exp, ptlang_ir_builder_fun_ctx *ctx)
Expand Down
43 changes: 0 additions & 43 deletions src/ptlang_ir_builder/ptlang_ir_builder.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,46 +18,3 @@ typedef struct llvm_target_machine llvm_target_machine;
#ifdef __cplusplus
}
#endif

#define ptlang_ir_builder_make_ctx(variable, ptlang_context) \
llvm::LLVMContext llvm_ctx = llvm::LLVMContext(); \
\
ptlang_ir_builder_scope global_scope = {}; \
\
ptlang_ir_builder_context variable = { \
/*.builder =*/llvm::IRBuilder<>(llvm_ctx), \
/*.module_ =*/llvm::Module("name", llvm_ctx), \
/*.llvm_ctx =*/llvm_ctx, \
/*.ctx =*/ptlang_context, \
/*.scope =*/&global_scope, \
/*.di_file =*/llvm::DIFile::get(llvm_ctx, "test.ptl", "/tmp"), \
}; \
\
{ \
\
variable.module_.setDataLayout(ptlang_context->target_machine->createDataLayout()); \
variable.module_.setTargetTriple(ptlang_context->target_machine->getTargetTriple().str()); \
\
variable.di_scope = llvm::DICompileUnit::getDistinct( \
llvm_ctx, 0, variable.di_file, "ptlang 0.0.0", false, "", 0, "", \
llvm::DICompileUnit::DebugEmissionKind::FullDebug, llvm::DICompositeTypeArray(), \
llvm::DIScopeArray(), llvm::DIGlobalVariableExpressionArray(), llvm::DIImportedEntityArray(), \
llvm::DIMacroNodeArray(), 0, false, false, llvm::DICompileUnit::DebugNameTableKind::Default, \
false, "", ""); \
\
/*Add external(memory) functions*/ \
\
variable.integer_ptrsize_type = \
llvm::IntegerType::get(variable.llvm_ctx, variable.ctx->pointer_bytes >> 3); \
\
llvm::Type *ptr_type = llvm::PointerType::getUnqual(variable.llvm_ctx); \
\
variable.malloc_func = variable.module_.getOrInsertFunction( \
"malloc", llvm::FunctionType::get(ptr_type, variable.integer_ptrsize_type, false)); \
\
variable.realloc_func = variable.module_.getOrInsertFunction( \
"realloc", llvm::FunctionType::get(ptr_type, {ptr_type, variable.integer_ptrsize_type}, false)); \
\
variable.free_func = variable.module_.getOrInsertFunction( \
"free", llvm::FunctionType::get(llvm::Type::getVoidTy(variable.llvm_ctx), ptr_type, false)); \
}
2 changes: 1 addition & 1 deletion src/ptlang_ir_builder/ptlang_ir_builder_impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
#include <llvm/IR/LegacyPassManager.h>
#include <llvm/IR/Module.h>
#include <llvm/IR/PassManager.h>
#include <llvm/IR/Verifier.h>
#include <llvm/MC/TargetRegistry.h>
#include <llvm/Passes/PassBuilder.h>
#include <llvm/Support/Debug.h>
Expand All @@ -18,6 +17,7 @@

#include "stb_ds.h"

#define FOLDER llvm::ConstantFolder
#include "ptlang_ir_builder_llvm.h"
#include "ptlang_eval.h"

Expand Down
5 changes: 3 additions & 2 deletions src/ptlang_ir_builder/ptlang_ir_builder_llvm.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

#include <llvm/IR/DebugInfoMetadata.h>
#include <llvm/IR/IRBuilder.h>
#include <llvm/IR/Verifier.h>

extern "C"
{
Expand Down Expand Up @@ -41,7 +42,7 @@ extern "C"

typedef struct ptlang_ir_builder_context_s
{
llvm::IRBuilder<> builder;
llvm::IRBuilder<FOLDER> builder;
llvm::Module module_;
llvm::LLVMContext &llvm_ctx;

Expand Down Expand Up @@ -93,7 +94,7 @@ extern "C"
ptlang_ir_builder_scope global_scope = {}; \
\
ptlang_ir_builder_context variable = { \
/*.builder =*/llvm::IRBuilder<>(llvm_ctx), \
/*.builder =*/llvm::IRBuilder<FOLDER>(llvm_ctx), \
/*.module_ =*/llvm::Module("name", llvm_ctx), \
/*.llvm_ctx =*/llvm_ctx, \
/*.ctx =*/ptlang_context, \
Expand Down

0 comments on commit 25b4a98

Please sign in to comment.