Created
March 26, 2015 19:39
-
-
Save schancel/e74db684efad0dcc1047 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
string evalString(Expression e) in { | |
// FIXME: newtype | |
// assert(cast(SliceType) peelAlias(e.type).type, "this only CTFE strings."); | |
} body { | |
// Create a global variable that recieve the string. | |
auto stringType = codeGen.visit(e.type); | |
auto receiver = LLVMAddGlobal(codeGen.dmodule, stringType, "__ctString"); | |
scope(exit) LLVMDeleteGlobal(receiver); | |
LLVMValueRef[2] constInit = [ LLVMConstInt( LLVMInt64TypeInContext( codeGen.llvmCtx), 0, false), | |
LLVMConstNull( LLVMPointerType (LLVMInt8TypeInContext(codeGen.llvmCtx),0)) ]; | |
LLVMDumpValue(LLVMGetUndef(stringType)); | |
LLVMDumpValue(LLVMConstStruct( constInit.ptr, 2, false )); | |
LLVMDumpValue(receiver); | |
LLVMSetInitializer(receiver, LLVMConstStructInContext( codeGen.llvmCtx, constInit.ptr, 2, false )); | |
LLVMDumpValue(receiver); | |
LLVMTypeRef[1] paramTypes = [LLVMPointerType(LLVMVoidType(),0)]; | |
auto funType = LLVMFunctionType(LLVMVoidTypeInContext(codeGen.llvmCtx), paramTypes.ptr, 1, false); | |
auto fun = LLVMAddFunction(codeGen.dmodule, "__ctfe", funType); | |
scope(exit) LLVMDeleteFunction(fun); | |
auto backupCurrentBB = LLVMGetInsertBlock(codeGen.builder); | |
scope(exit) { | |
if(backupCurrentBB) { | |
LLVMPositionBuilderAtEnd(codeGen.builder, backupCurrentBB); | |
} else { | |
LLVMClearInsertionPosition(codeGen.builder); | |
} | |
} | |
auto bodyBB = LLVMAppendBasicBlockInContext(codeGen.llvmCtx, fun, ""); | |
LLVMPositionBuilderAtEnd(codeGen.builder, bodyBB); | |
// Generate function's body. | |
import d.llvm.expression; | |
auto eg = ExpressionGen(codeGen); | |
LLVMBuildStore(codeGen.builder, | |
LLVMBuildPointerCast(codeGen.builder, eg.visit(e), LLVMPointerType(LLVMVoidType(),0), ""), | |
LLVMGetFirstParam(fun)); | |
LLVMBuildRetVoid(codeGen.builder); | |
codeGen.checkModule(codeGen.dmodule); | |
import std.stdio; | |
auto jitModule = LLVMCloneModule( codeGen.dmodule ); | |
//scope(exit) LLVMDisposeModule(jitModule); | |
//scope(failure) LLVMDumpModule(jitModule); | |
auto executionEngine = createExecutionEngine(jitModule); | |
//scope(exit) LLVMDisposeExecutionEngine(executionEngine); | |
auto jitFun = LLVMGetNamedFunction(jitModule, "__ctfe"); | |
LLVMDumpValue(jitFun); | |
string foo; | |
auto ret = LLVMRunFunction(executionEngine, jitFun, 1, [LLVMCreateGenericValueOfPointer(cast(void*)&foo)].ptr); | |
writeln("\"",foo, "\""); | |
return foo.idup; //return s.idup; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment