Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- string evalString(Expression e) in {
- // FIXME: newtype
- // assert(cast(SliceType) peelAlias(e.type).type, "this only CTFE strings.");
- } body {
- import std.stdio;
- // 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);
- writeln("---1");
- 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);
- writeln("---2");
- LLVMTypeRef[1] paramTypes = [LLVMInt8TypeInContext(codeGen.llvmCtx)];
- writeln("---2.2");
- auto funType = LLVMFunctionType(LLVMVoidTypeInContext(codeGen.llvmCtx), paramTypes.ptr, 1, false);
- writeln("---3");
- auto fun = LLVMAddFunction(codeGen.dmodule, "__ctfe", funType);
- scope(exit) LLVMDeleteFunction(fun);
- writeln("---4");
- 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);
- writeln("---11");
- auto castToPointer = LLVMBuildPointerCast(codeGen.builder,
- LLVMGetFirstParam(fun),
- LLVMPointerType(LLVMInt8TypeInContext(codeGen.llvmCtx), 0), "");
- writeln("---12");
- auto casted = LLVMBuildPointerCast(codeGen.builder,
- castToPointer,
- LLVMPointerType(stringType,0), "");
- writeln("---22");
- LLVMBuildStore(codeGen.builder, eg.visit(e), casted);
- writeln("---33");
- LLVMBuildRetVoid(codeGen.builder);
- writeln("---4");
- 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,
- [LLVMCreateGenericValueOfInt(LLVMInt8TypeInContext(codeGen.llvmCtx), cast(uint)cast(void*)&foo, false)].ptr);
- writeln("\"",foo, "\"");
- return foo.idup; //return s.idup;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement