-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
465 additions
and
123 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
Oops, something went wrong.